f24beff3ee8e5befd0a85f6471f0b3913d6e884d
[debian/amanda] / common-src / pipespawn.c
1 #include "amanda.h"
2 #include "pipespawn.h"
3 #include "arglist.h"
4 #include "clock.h"
5 #include "util.h"
6
7 char skip_argument[1];
8
9 pid_t pipespawnv_passwd(char *prog, int pipedef, int need_root,
10                   int *stdinfd, int *stdoutfd, int *stderrfd,
11                   char **my_argv);
12
13
14 /*
15  * this used to be a function in it's own write but became a wrapper around
16  * pipespawnv to eliminate redundancy...
17  */
18 pid_t
19 pipespawn(
20     char *      prog,
21     int         pipedef,
22     int         need_root,
23     int *       stdinfd,
24     int *       stdoutfd,
25     int *       stderrfd,
26     ...)
27 {
28     va_list ap;
29     int argc = 0, i;
30     pid_t pid;
31     char **argv;
32
33     /* count args */
34     arglist_start(ap, stderrfd);
35     while(arglist_val(ap, char *) != NULL) {
36         argc++;
37     }
38     arglist_end(ap);
39
40     /*
41      * Create the argument vector.
42      */
43     arglist_start(ap, stderrfd);
44     argv = (char **)alloc((argc + 1) * SIZEOF(*argv));
45     i = 0;
46     while((argv[i] = arglist_val(ap, char *)) != NULL) {
47         if (argv[i] != skip_argument) {
48             i++;
49         }
50     }
51     arglist_end(ap);
52
53     pid = pipespawnv_passwd(prog, pipedef, need_root,
54                             stdinfd, stdoutfd, stderrfd, argv);
55     amfree(argv);
56     return pid;
57 }
58
59 pid_t
60 pipespawnv(
61     char *      prog,
62     int         pipedef,
63     int         need_root,
64     int *       stdinfd,
65     int *       stdoutfd,
66     int *       stderrfd,
67     char **     my_argv)
68 {
69     return pipespawnv_passwd(prog, pipedef, need_root,
70                              stdinfd, stdoutfd, stderrfd,
71         my_argv);
72 }
73
74 pid_t
75 pipespawnv_passwd(
76     char *      prog,
77     int         pipedef,
78     int         need_root,
79     int *       stdinfd,
80     int *       stdoutfd,
81     int *       stderrfd,
82     char **     my_argv)
83 {
84     int argc;
85     pid_t pid;
86     int i, inpipe[2], outpipe[2], errpipe[2], passwdpipe[2];
87     char number[NUM_STR_SIZE];
88     char **arg;
89     char *e;
90     char **env;
91     char *cmdline;
92     char *quoted;
93     char **newenv;
94     char *passwdvar = NULL;
95     int  *passwdfd = NULL;
96
97     /*
98      * Log the command line and count the args.
99      */
100     if ((pipedef & PASSWD_PIPE) != 0) {
101         passwdvar = *my_argv++;
102         passwdfd  = (int *)*my_argv++;
103     }
104     g_debug("pipespawnv: stdoutfd is %d", *stdoutfd);
105     memset(inpipe, -1, SIZEOF(inpipe));
106     memset(outpipe, -1, SIZEOF(outpipe));
107     memset(errpipe, -1, SIZEOF(errpipe));
108     memset(passwdpipe, -1, SIZEOF(passwdpipe));
109     argc = 0;
110
111     cmdline = stralloc(prog);
112     for(arg = my_argv; *arg != NULL; arg++) {
113         if (*arg != skip_argument) {
114             argc++;
115             quoted = quote_string(*arg);
116             cmdline = vstrextend(&cmdline, " ", quoted, NULL);
117             amfree(quoted);
118         }
119     }
120     dbprintf(_("Spawning \"%s\" in pipeline\n"), cmdline);
121
122     /*
123      * Create the pipes
124      */
125     if ((pipedef & STDIN_PIPE) != 0) {
126         if(pipe(inpipe) == -1) {
127             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
128             /*NOTREACHED*/
129         }
130     }
131     if ((pipedef & STDOUT_PIPE) != 0) {
132         if(pipe(outpipe) == -1) {
133             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
134             /*NOTREACHED*/
135         }
136     }
137     if ((pipedef & STDERR_PIPE) != 0) {
138         if(pipe(errpipe) == -1) {
139             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
140             /*NOTREACHED*/
141         }
142     }
143     if ((pipedef & PASSWD_PIPE) != 0) {
144         if(pipe(passwdpipe) == -1) {
145             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
146             /*NOTREACHED*/
147         }
148     }
149
150     /*
151      * Fork and set up the return or run the program.
152      */
153     switch(pid = fork()) {
154     case -1:
155         e = strerror(errno);
156         error(_("error [fork %s: %s]"), prog, e);
157         /*NOTREACHED*/
158
159     default:    /* parent process */
160         if ((pipedef & STDIN_PIPE) != 0) {
161             aclose(inpipe[0]);          /* close input side of pipe */
162             *stdinfd = inpipe[1];
163         }
164         if ((pipedef & STDOUT_PIPE) != 0) {
165             aclose(outpipe[1]);         /* close output side of pipe */
166             *stdoutfd = outpipe[0];
167         }
168         if ((pipedef & STDERR_PIPE) != 0) {
169             aclose(errpipe[1]);         /* close output side of pipe */
170             *stderrfd = errpipe[0];
171         }
172         if ((pipedef & PASSWD_PIPE) != 0) {
173             aclose(passwdpipe[0]);      /* close input side of pipe */
174             *passwdfd = passwdpipe[1];
175         }
176         break;
177     case 0:             /* child process */
178         debug_dup_stderr_to_debug();
179         if ((pipedef & STDIN_PIPE) != 0) {
180             aclose(inpipe[1]);          /* close output side of pipe */
181         } else {
182             inpipe[0] = *stdinfd;
183         }
184         if ((pipedef & STDOUT_PIPE) != 0) {
185             aclose(outpipe[0]);         /* close input side of pipe */
186         } else {
187             outpipe[1] = *stdoutfd;
188         }
189         if ((pipedef & STDERR_PIPE) != 0) {
190             aclose(errpipe[0]);         /* close input side of pipe */
191         } else {
192             errpipe[1] = *stderrfd;
193         }
194         if ((pipedef & PASSWD_PIPE) != 0) { 
195             aclose(passwdpipe[1]);      /* close output side of pipe */
196         }
197
198         /*
199          * Shift the pipes to the standard file descriptors as requested.
200          */
201         if(dup2(inpipe[0], 0) == -1) {
202             g_fprintf(stderr, "error [spawn %s: dup2 in: %s]", prog, strerror(errno));
203             exit(1);
204             /*NOTREACHED*/
205         }
206         if(dup2(outpipe[1], 1) == -1) {
207             g_fprintf(stderr, "error [spawn %s: dup2 out: %s]", prog, strerror(errno));
208             exit(1);
209             /*NOTREACHED*/
210         }
211         if(dup2(errpipe[1], 2) == -1) {
212             g_fprintf(stderr, "error [spawn %s: dup2 err: %s]", prog, strerror(errno));
213             exit(1);
214             /*NOTREACHED*/
215         }
216
217         /*
218          * Get the "safe" environment.  If we are sending a password to
219          * the child via a pipe, add the environment variable for that.
220          */
221         env = safe_env();
222         if ((pipedef & PASSWD_PIPE) != 0) {
223             for (i = 0; env[i] != NULL; i++)
224                 (void)i; /* make lint happy and do nothing */   
225             newenv = (char **)alloc((i + 1 + 1) * SIZEOF(*newenv));
226             g_snprintf(number, SIZEOF(number), "%d", passwdpipe[0]);
227             newenv[0] = vstralloc(passwdvar, "=", number, NULL);
228             for(i = 0; env[i] != NULL; i++)
229                 newenv[i + 1] = env[i];
230             newenv[i + 1] = NULL;
231             amfree(env);
232             env = newenv;
233             safe_fd(passwdpipe[0], 1);
234         } else {
235             safe_fd(-1, 0);
236         }
237
238         if (need_root)
239             become_root();
240         execve(prog, my_argv, env);
241         e = strerror(errno);
242         error(_("error [exec %s: %s]"), prog, e);
243         /*NOTREACHED*/
244     }
245     amfree(cmdline);
246     return pid;
247 }