1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* See the file NOTICE for conditions of use and distribution. */
11 static void (*oldsignal)(int);
14 /*************************************************
15 * Ensure an fd has a given value *
16 *************************************************/
18 /* This function is called when we want to ensure that a certain fd has a
19 specific value (one of 0, 1, 2). If it hasn't got it already, close the value
20 we want, duplicate the fd, then close the old one.
30 force_fd(int oldfd, int newfd)
32 if (oldfd == newfd) return;
34 (void)dup2(oldfd, newfd);
40 /*************************************************
41 * Build argv list and optionally re-exec Exim *
42 *************************************************/
44 /* This function is called when Exim wants to re-exec (overlay) itself in the
45 current process. This is different to child_open_exim(), which runs another
46 Exim process in parallel (but it then calls this function). The function's
47 basic job is to build the argv list according to the values of current options
48 settings. There is a basic list that all calls require, and an additional list
49 that some do not require. Further additions can be given as additional
50 arguments. An option specifies whether the exec() is actually to happen, and if
51 so, what is to be done if it fails.
54 exec_type CEE_RETURN_ARGV => don't exec; return the argv list
55 CEE_EXEC_EXIT => just exit() on exec failure
56 CEE_EXEC_PANIC => panic-die on exec failure
57 kill_v if TRUE, don't pass on the D_v flag
58 pcount if not NULL, points to extra size of argv required, and if
59 CEE_RETURN_ARGV is specified, it is updated to give the
61 minimal TRUE if only minimal argv is required
62 acount number of additional arguments
63 ... further values to add to argv
65 Returns: if CEE_RETURN_ARGV is given, returns a pointer to argv;
66 otherwise, does not return
70 child_exec_exim(int exec_type, BOOL kill_v, int *pcount, BOOL minimal,
73 int first_special = -1;
75 int extra = (pcount != NULL)? *pcount : 0;
77 store_get((extra + acount + MAX_CLMACROS + 16) * sizeof(char *));
79 /* In all case, the list starts out with the path, any macros, and a changed
82 argv[n++] = exim_path;
83 if (clmacro_count > 0)
85 memcpy(argv + n, clmacros, clmacro_count * sizeof(uschar *));
91 argv[n++] = config_main_filename;
94 /* These values are added only for non-minimal cases. If debug_selector is
95 precisely D_v, we have to assume this was started by a non-admin user, and
96 we suppress the flag when requested. (This happens when passing on an SMTP
97 connection, and after ETRN.) If there's more debugging going on, an admin user
98 was involved, so we do pass it on. */
102 if (debug_selector == D_v)
104 if (!kill_v) argv[n++] = US"-v";
108 if (debug_selector != 0)
109 argv[n++] = string_sprintf("-d=0x%x", debug_selector);
111 if (dont_deliver) argv[n++] = US"-N";
112 if (queue_smtp) argv[n++] = US"-odqs";
113 if (synchronous_delivery) argv[n++] = US"-odi";
114 if (connection_max_messages >= 0)
115 argv[n++] = string_sprintf("-oB%d", connection_max_messages);
118 /* Now add in any others that are in the call. Remember which they were,
119 for more helpful diagnosis on failure. */
124 va_start(ap, acount);
127 argv[n++] = va_arg(ap, uschar *);
131 /* Terminate the list, and return it, if that is what is wanted. */
134 if (exec_type == CEE_RETURN_ARGV)
136 if (pcount != NULL) *pcount = n;
140 /* Otherwise, do the exec() here, and handle the consequences of an unexpected
141 failure. We know that there will always be at least one extra option in the
142 call when exec() is done here, so it can be used to add to the panic data. */
144 DEBUG(D_exec) debug_print_argv(CUSS argv);
145 exim_nullstd(); /* Make sure std{in,out,err} exist */
146 execv(CS argv[0], (char *const *)argv);
149 LOG_MAIN | ((exec_type == CEE_EXEC_EXIT)? LOG_PANIC : LOG_PANIC_DIE),
150 "re-exec of exim (%s) with %s failed: %s", exim_path, argv[first_special],
153 /* Get here if exec_type == CEE_EXEC_EXIT.
154 Note: this must be _exit(), not exit(). */
156 _exit(EX_EXECFAILED);
158 return NULL; /* To keep compilers happy */
164 /*************************************************
165 * Create a child Exim process *
166 *************************************************/
168 /* This function is called when Exim wants to run a parallel instance of itself
169 in order to inject a message via the standard input. The function creates a
170 child process and runs Exim in it. It sets up a pipe to the standard input of
171 the new process, and returns that to the caller via fdptr. The function returns
172 the pid of the new process, or -1 if things go wrong. If debug_fd is
173 non-negative, it is passed as stderr.
175 This interface is now a just wrapper for the more complicated function
176 child_open_exim2(), which has additional arguments. The wrapper must continue
177 to exist, even if all calls from within Exim are changed, because it is
178 documented for use from local_scan().
180 Argument: fdptr pointer to int for the stdin fd
181 Returns: pid of the created process or -1 if anything has gone wrong
185 child_open_exim(int *fdptr)
187 return child_open_exim2(fdptr, US"<>", bounce_sender_authentication);
191 /* This is a more complicated function for creating a child Exim process, with
195 fdptr pointer to int for the stdin fd
196 sender for a sender address (data for -f)
197 sender_authentication authenticated sender address or NULL
199 Returns: pid of the created process or -1 if anything has gone wrong
203 child_open_exim2(int *fdptr, uschar *sender, uschar *sender_authentication)
209 /* Create the pipe and fork the process. Ensure that SIGCHLD is set to
210 SIG_DFL before forking, so that the child process can be waited for. We
211 sometimes get here with it set otherwise. Save the old state for resetting
214 if (pipe(pfd) != 0) return (pid_t)(-1);
215 oldsignal = signal(SIGCHLD, SIG_DFL);
218 /* Child process: make the reading end of the pipe into the standard input and
219 close the writing end. If debugging, pass debug_fd as stderr. Then re-exec
220 Exim with appropriat options. In the test harness, use -odi unless queue_only
221 is set, so that the bounce is fully delivered before returning. Failure is
222 signalled with EX_EXECFAILED (specified by CEE_EXEC_EXIT), but this shouldn't
227 force_fd(pfd[pipe_read], 0);
228 (void)close(pfd[pipe_write]);
229 if (debug_fd > 0) force_fd(debug_fd, 2);
230 if (running_in_test_harness && !queue_only)
232 if (sender_authentication != NULL)
233 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 9,
234 US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
235 sender_authentication, message_id_option);
237 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 7,
238 US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender,
240 /* Control does not return here. */
242 else /* Not test harness */
244 if (sender_authentication != NULL)
245 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 8,
246 US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
247 sender_authentication, message_id_option);
249 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 6,
250 US"-t", US"-oem", US"-oi", US"-f", sender, message_id_option);
251 /* Control does not return here. */
255 /* Parent process. Save fork() errno and close the reading end of the stdin
259 (void)close(pfd[pipe_read]);
265 *fdptr = pfd[pipe_write]; /* return writing end of stdin pipe */
266 return pid; /* and pid of new process */
271 (void)close(pfd[pipe_write]);
275 #endif /* STAND_ALONE */
279 /*************************************************
280 * Create a non-Exim child process *
281 *************************************************/
283 /* This function creates a child process and runs the given command in it. It
284 sets up pipes to the standard input and output of the new process, and returns
285 them to the caller. The standard error is cloned to the output. If there are
286 any file descriptors "in the way" in the new process, they are closed. A new
287 umask is supplied for the process, and an optional new uid and gid are also
288 available. These are used by the queryprogram router to set an unprivileged id.
289 SIGUSR1 is always disabled in the new process, as it is not going to be running
290 Exim (the function child_open_exim() is provided for that). This function
291 returns the pid of the new process, or -1 if things go wrong.
294 argv the argv for exec in the new process
295 envp the envp for exec in the new process
296 newumask umask to set in the new process
297 newuid point to uid for the new process or NULL for no change
298 newgid point to gid for the new process or NULL for no change
299 infdptr pointer to int into which the fd of the stdin of the new process
301 outfdptr pointer to int into which the fd of the stdout/stderr of the new
303 wd if not NULL, a path to be handed to chdir() in the new process
304 make_leader if TRUE, make the new process a process group leader
306 Returns: the pid of the created process or -1 if anything has gone wrong
310 child_open_uid(const uschar **argv, const uschar **envp, int newumask,
311 uid_t *newuid, gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd,
315 int inpfd[2], outpfd[2];
318 /* Create the pipes. */
320 if (pipe(inpfd) != 0) return (pid_t)(-1);
321 if (pipe(outpfd) != 0)
323 (void)close(inpfd[pipe_read]);
324 (void)close(inpfd[pipe_write]);
328 /* Fork the process. Ensure that SIGCHLD is set to SIG_DFL before forking, so
329 that the child process can be waited for. We sometimes get here with it set
330 otherwise. Save the old state for resetting on the wait. */
332 oldsignal = signal(SIGCHLD, SIG_DFL);
335 /* Handle the child process. First, set the required environment. We must do
336 this before messing with the pipes, in order to be able to write debugging
337 output when things go wrong. */
341 signal(SIGUSR1, SIG_IGN);
342 signal(SIGPIPE, SIG_DFL);
344 if (newgid != NULL && setgid(*newgid) < 0)
346 DEBUG(D_any) debug_printf("failed to set gid=%ld in subprocess: %s\n",
347 (long int)(*newgid), strerror(errno));
351 if (newuid != NULL && setuid(*newuid) < 0)
353 DEBUG(D_any) debug_printf("failed to set uid=%ld in subprocess: %s\n",
354 (long int)(*newuid), strerror(errno));
358 (void)umask(newumask);
360 if (wd != NULL && Uchdir(wd) < 0)
362 DEBUG(D_any) debug_printf("failed to chdir to %s: %s\n", wd,
367 /* Becomes a process group leader if requested, and then organize the pipes.
368 Any unexpected failure is signalled with EX_EXECFAILED; these are all "should
369 never occur" failures, except for exec failing because the command doesn't
372 if (make_leader && setpgid(0,0) < 0)
374 DEBUG(D_any) debug_printf("failed to set group leader in subprocess: %s\n",
379 (void)close(inpfd[pipe_write]);
380 force_fd(inpfd[pipe_read], 0);
382 (void)close(outpfd[pipe_read]);
383 force_fd(outpfd[pipe_write], 1);
388 /* Now do the exec */
390 if (envp == NULL) execv(CS argv[0], (char *const *)argv);
391 else execve(CS argv[0], (char *const *)argv, (char *const *)envp);
393 /* Failed to execv. Signal this failure using EX_EXECFAILED. We are
394 losing the actual errno we got back, because there is no way to return
398 _exit(EX_EXECFAILED); /* Note: must be _exit(), NOT exit() */
401 /* Parent process. Save any fork failure code, and close the reading end of the
402 stdin pipe, and the writing end of the stdout pipe. */
405 (void)close(inpfd[pipe_read]);
406 (void)close(outpfd[pipe_write]);
408 /* Fork succeeded; return the input/output pipes and the pid */
412 *infdptr = inpfd[pipe_write];
413 *outfdptr = outpfd[pipe_read];
417 /* Fork failed; reset fork errno before returning */
419 (void)close(inpfd[pipe_write]);
420 (void)close(outpfd[pipe_read]);
428 /*************************************************
429 * Create child process without uid change *
430 *************************************************/
432 /* This function is a wrapper for child_open_uid() that doesn't have the uid,
433 gid and working directory changing arguments. The function is provided so as to
434 have a clean interface for use from local_scan(), but also saves writing NULL
435 arguments several calls that would otherwise use child_open_uid().
438 argv the argv for exec in the new process
439 envp the envp for exec in the new process
440 newumask umask to set in the new process
441 infdptr pointer to int into which the fd of the stdin of the new process
443 outfdptr pointer to int into which the fd of the stdout/stderr of the new
445 make_leader if TRUE, make the new process a process group leader
447 Returns: the pid of the created process or -1 if anything has gone wrong
451 child_open(uschar **argv, uschar **envp, int newumask, int *infdptr,
452 int *outfdptr, BOOL make_leader)
454 return child_open_uid(CUSS argv, CUSS envp, newumask, NULL, NULL,
455 infdptr, outfdptr, NULL, make_leader);
461 /*************************************************
462 * Close down child process *
463 *************************************************/
465 /* Wait for the given process to finish, with optional timeout.
468 pid: the pid to wait for
469 timeout: maximum time to wait; 0 means for as long as it takes
471 Returns: >= 0 process terminated by exiting; value is process
472 ending status; if an execve() failed, the value
473 is typically 127 (defined as EX_EXECFAILED)
474 < 0 & > -256 process was terminated by a signal; value is the
475 negation of the signal number
477 -257 other error in wait(); errno still set
481 child_close(pid_t pid, int timeout)
487 sigalrm_seen = FALSE;
494 pid_t rc = waitpid(pid, &status, 0);
497 int lowbyte = status & 255;
498 if (lowbyte == 0) yield = (status >> 8) & 255;
499 else yield = -lowbyte;
504 yield = (errno == EINTR && sigalrm_seen)? -256 : -257;
509 if (timeout > 0) alarm(0);
511 signal(SIGCHLD, oldsignal); /* restore */