9 pid_t pipespawnv_passwd(char *prog, int pipedef, int need_root,
10 int *stdinfd, int *stdoutfd, int *stderrfd,
15 * this used to be a function in it's own write but became a wrapper around
16 * pipespawnv to eliminate redundancy...
34 arglist_start(ap, stderrfd);
35 while(arglist_val(ap, char *) != NULL) {
41 * Create the argument vector.
43 arglist_start(ap, stderrfd);
44 argv = (char **)alloc((argc + 1) * SIZEOF(*argv));
46 while((argv[i] = arglist_val(ap, char *)) != NULL) {
47 if (argv[i] != skip_argument) {
53 pid = pipespawnv_passwd(prog, pipedef, need_root,
54 stdinfd, stdoutfd, stderrfd, argv);
69 return pipespawnv_passwd(prog, pipedef, need_root,
70 stdinfd, stdoutfd, stderrfd,
86 int i, inpipe[2], outpipe[2], errpipe[2], passwdpipe[2];
87 char number[NUM_STR_SIZE];
94 char *passwdvar = NULL;
98 * Log the command line and count the args.
100 if ((pipedef & PASSWD_PIPE) != 0) {
101 passwdvar = *my_argv++;
102 passwdfd = (int *)*my_argv++;
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));
111 cmdline = stralloc(prog);
112 for(arg = my_argv; *arg != NULL; arg++) {
113 if (*arg != skip_argument) {
115 quoted = quote_string(*arg);
116 cmdline = vstrextend(&cmdline, " ", quoted, NULL);
120 dbprintf(_("Spawning \"%s\" in pipeline\n"), cmdline);
125 if ((pipedef & STDIN_PIPE) != 0) {
126 if(pipe(inpipe) == -1) {
127 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
131 if ((pipedef & STDOUT_PIPE) != 0) {
132 if(pipe(outpipe) == -1) {
133 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
137 if ((pipedef & STDERR_PIPE) != 0) {
138 if(pipe(errpipe) == -1) {
139 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
143 if ((pipedef & PASSWD_PIPE) != 0) {
144 if(pipe(passwdpipe) == -1) {
145 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
151 * Fork and set up the return or run the program.
153 switch(pid = fork()) {
156 error(_("error [fork %s: %s]"), prog, e);
159 default: /* parent process */
160 if ((pipedef & STDIN_PIPE) != 0) {
161 aclose(inpipe[0]); /* close input side of pipe */
162 *stdinfd = inpipe[1];
164 if ((pipedef & STDOUT_PIPE) != 0) {
165 aclose(outpipe[1]); /* close output side of pipe */
166 *stdoutfd = outpipe[0];
168 if ((pipedef & STDERR_PIPE) != 0) {
169 aclose(errpipe[1]); /* close output side of pipe */
170 *stderrfd = errpipe[0];
172 if ((pipedef & PASSWD_PIPE) != 0) {
173 aclose(passwdpipe[0]); /* close input side of pipe */
174 *passwdfd = passwdpipe[1];
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 */
182 inpipe[0] = *stdinfd;
184 if ((pipedef & STDOUT_PIPE) != 0) {
185 aclose(outpipe[0]); /* close input side of pipe */
187 outpipe[1] = *stdoutfd;
189 if ((pipedef & STDERR_PIPE) != 0) {
190 aclose(errpipe[0]); /* close input side of pipe */
192 errpipe[1] = *stderrfd;
194 if ((pipedef & PASSWD_PIPE) != 0) {
195 aclose(passwdpipe[1]); /* close output side of pipe */
199 * Shift the pipes to the standard file descriptors as requested.
201 if(dup2(inpipe[0], 0) == -1) {
202 g_fprintf(stderr, "error [spawn %s: dup2 in: %s]", prog, strerror(errno));
206 if(dup2(outpipe[1], 1) == -1) {
207 g_fprintf(stderr, "error [spawn %s: dup2 out: %s]", prog, strerror(errno));
211 if(dup2(errpipe[1], 2) == -1) {
212 g_fprintf(stderr, "error [spawn %s: dup2 err: %s]", prog, strerror(errno));
218 * Get the "safe" environment. If we are sending a password to
219 * the child via a pipe, add the environment variable for that.
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;
233 safe_fd(passwdpipe[0], 1);
240 execve(prog, my_argv, env);
242 error(_("error [exec %s: %s]"), prog, e);