1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
12 static void (*oldsignal)(int);
15 /*************************************************
16 * Ensure an fd has a given value *
17 *************************************************/
19 /* This function is called when we want to ensure that a certain fd has a
20 specific value (one of 0, 1, 2). If it hasn't got it already, close the value
21 we want, duplicate the fd, then close the old one.
31 force_fd(int oldfd, int newfd)
33 if (oldfd == newfd) return;
35 (void)dup2(oldfd, newfd);
41 /*************************************************
42 * Build argv list and optionally re-exec Exim *
43 *************************************************/
45 /* This function is called when Exim wants to re-exec (overlay) itself in the
46 current process. This is different to child_open_exim(), which runs another
47 Exim process in parallel (but it then calls this function). The function's
48 basic job is to build the argv list according to the values of current options
49 settings. There is a basic list that all calls require, and an additional list
50 that some do not require. Further additions can be given as additional
51 arguments. An option specifies whether the exec() is actually to happen, and if
52 so, what is to be done if it fails.
55 exec_type CEE_RETURN_ARGV => don't exec; return the argv list
56 CEE_EXEC_EXIT => just exit() on exec failure
57 CEE_EXEC_PANIC => panic-die on exec failure
58 kill_v if TRUE, don't pass on the D_v flag
59 pcount if not NULL, points to extra size of argv required, and if
60 CEE_RETURN_ARGV is specified, it is updated to give the
62 minimal TRUE if only minimal argv is required
63 acount number of additional arguments
64 ... further values to add to argv
66 Returns: if CEE_RETURN_ARGV is given, returns a pointer to argv;
67 otherwise, does not return
71 child_exec_exim(int exec_type, BOOL kill_v, int *pcount, BOOL minimal,
74 int first_special = -1;
76 int extra = pcount ? *pcount : 0;
79 argv = store_get((extra + acount + MAX_CLMACROS + 21) * sizeof(char *), FALSE);
81 /* In all case, the list starts out with the path, any macros, and a changed
84 argv[n++] = exim_path;
85 if (clmacro_count > 0)
87 memcpy(argv + n, clmacros, clmacro_count * sizeof(uschar *));
93 argv[n++] = config_main_filename;
96 /* These values are added only for non-minimal cases. If debug_selector is
97 precisely D_v, we have to assume this was started by a non-admin user, and
98 we suppress the flag when requested. (This happens when passing on an SMTP
99 connection, and after ETRN.) If there's more debugging going on, an admin user
100 was involved, so we do pass it on. */
104 if (debug_selector == D_v)
106 if (!kill_v) argv[n++] = US"-v";
110 if (debug_selector != 0)
111 argv[n++] = string_sprintf("-d=0x%x", debug_selector);
115 argv[n++] = US"-MCd";
116 argv[n++] = US process_purpose;
118 if (!f.testsuite_delays) argv[n++] = US"-odd";
119 if (f.dont_deliver) argv[n++] = US"-N";
120 if (f.queue_smtp) argv[n++] = US"-odqs";
121 if (f.synchronous_delivery) argv[n++] = US"-odi";
122 if (connection_max_messages >= 0)
123 argv[n++] = string_sprintf("-oB%d", connection_max_messages);
126 argv[n++] = US"-MCG";
127 argv[n++] = queue_name;
131 /* Now add in any others that are in the call. Remember which they were,
132 for more helpful diagnosis on failure. */
137 va_start(ap, acount);
140 argv[n++] = va_arg(ap, uschar *);
144 /* Terminate the list, and return it, if that is what is wanted. */
147 if (exec_type == CEE_RETURN_ARGV)
149 if (pcount) *pcount = n;
153 /* Otherwise, do the exec() here, and handle the consequences of an unexpected
154 failure. We know that there will always be at least one extra option in the
155 call when exec() is done here, so it can be used to add to the panic data. */
157 DEBUG(D_exec) debug_print_argv(CUSS argv);
158 exim_nullstd(); /* Make sure std{in,out,err} exist */
159 execv(CS argv[0], (char *const *)argv);
162 LOG_MAIN | ((exec_type == CEE_EXEC_EXIT)? LOG_PANIC : LOG_PANIC_DIE),
163 "re-exec of exim (%s) with %s failed: %s", exim_path, argv[first_special],
166 /* Get here if exec_type == CEE_EXEC_EXIT.
167 Note: this must be _exit(), not exit(). */
169 _exit(EX_EXECFAILED);
171 return NULL; /* To keep compilers happy */
177 /*************************************************
178 * Create a child Exim process *
179 *************************************************/
181 /* This function is called when Exim wants to run a parallel instance of itself
182 in order to inject a message via the standard input. The function creates a
183 child process and runs Exim in it. It sets up a pipe to the standard input of
184 the new process, and returns that to the caller via fdptr. The function returns
185 the pid of the new process, or -1 if things go wrong. If debug_fd is
186 non-negative, it is passed as stderr.
188 This interface is now a just wrapper for the more complicated function
189 child_open_exim2(), which has additional arguments. The wrapper must continue
190 to exist, even if all calls from within Exim are changed, because it is
191 documented for use from local_scan().
193 Argument: fdptr pointer to int for the stdin fd
194 purpose of the child process, for debug
195 Returns: pid of the created process or -1 if anything has gone wrong
199 child_open_exim_function(int * fdptr, const uschar * purpose)
201 return child_open_exim2_function(fdptr, US"<>", bounce_sender_authentication,
206 /* This is a more complicated function for creating a child Exim process, with
210 fdptr pointer to int for the stdin fd
211 sender for a sender address (data for -f)
212 sender_authentication authenticated sender address or NULL
213 purpose of the child process, for debug
215 Returns: pid of the created process or -1 if anything has gone wrong
219 child_open_exim2_function(int * fdptr, uschar * sender,
220 uschar * sender_authentication, const uschar * purpose)
226 /* Create the pipe and fork the process. Ensure that SIGCHLD is set to
227 SIG_DFL before forking, so that the child process can be waited for. We
228 sometimes get here with it set otherwise. Save the old state for resetting
231 if (pipe(pfd) != 0) return (pid_t)(-1);
232 oldsignal = signal(SIGCHLD, SIG_DFL);
233 pid = exim_fork(purpose);
235 /* Child process: make the reading end of the pipe into the standard input and
236 close the writing end. If debugging, pass debug_fd as stderr. Then re-exec
237 Exim with appropriate options. In the test harness, use -odi unless queue_only
238 is set, so that the bounce is fully delivered before returning. Failure is
239 signalled with EX_EXECFAILED (specified by CEE_EXEC_EXIT), but this shouldn't
244 force_fd(pfd[pipe_read], 0);
245 (void)close(pfd[pipe_write]);
246 if (debug_fd > 0) force_fd(debug_fd, 2);
247 if (f.running_in_test_harness && !queue_only)
249 if (sender_authentication)
250 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 9,
251 US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
252 sender_authentication, message_id_option);
254 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 7,
255 US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender,
257 /* Control does not return here. */
259 else /* Not test harness */
261 if (sender_authentication)
262 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 8,
263 US"-t", US"-oem", US"-oi", US"-f", sender, US"-oMas",
264 sender_authentication, message_id_option);
266 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 6,
267 US"-t", US"-oem", US"-oi", US"-f", sender, message_id_option);
268 /* Control does not return here. */
272 testharness_pause_ms(100); /* let child work even longer, for exec */
274 /* Parent process. Save fork() errno and close the reading end of the stdin
278 (void)close(pfd[pipe_read]);
284 *fdptr = pfd[pipe_write]; /* return writing end of stdin pipe */
285 return pid; /* and pid of new process */
290 (void)close(pfd[pipe_write]);
294 #endif /* STAND_ALONE */
298 /*************************************************
299 * Create a non-Exim child process *
300 *************************************************/
302 /* This function creates a child process and runs the given command in it. It
303 sets up pipes to the standard input and output of the new process, and returns
304 them to the caller. The standard error is cloned to the output. If there are
305 any file descriptors "in the way" in the new process, they are closed. A new
306 umask is supplied for the process, and an optional new uid and gid are also
307 available. These are used by the queryprogram router to set an unprivileged id.
308 SIGUSR1 is always disabled in the new process, as it is not going to be running
309 Exim (the function child_open_exim() is provided for that). This function
310 returns the pid of the new process, or -1 if things go wrong.
313 argv the argv for exec in the new process
314 envp the envp for exec in the new process
315 newumask umask to set in the new process
316 newuid point to uid for the new process or NULL for no change
317 newgid point to gid for the new process or NULL for no change
318 infdptr pointer to int into which the fd of the stdin of the new process
320 outfdptr pointer to int into which the fd of the stdout/stderr of the new
322 wd if not NULL, a path to be handed to chdir() in the new process
323 make_leader if TRUE, make the new process a process group leader
324 purpose for debug: reason for running the task
326 Returns: the pid of the created process or -1 if anything has gone wrong
330 child_open_uid(const uschar **argv, const uschar **envp, int newumask,
331 uid_t *newuid, gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd,
332 BOOL make_leader, const uschar * purpose)
335 int inpfd[2], outpfd[2];
338 /* Create the pipes. */
340 if (pipe(inpfd) != 0) return (pid_t)(-1);
341 if (pipe(outpfd) != 0)
343 (void)close(inpfd[pipe_read]);
344 (void)close(inpfd[pipe_write]);
348 /* Fork the process. Ensure that SIGCHLD is set to SIG_DFL before forking, so
349 that the child process can be waited for. We sometimes get here with it set
350 otherwise. Save the old state for resetting on the wait. */
352 oldsignal = signal(SIGCHLD, SIG_DFL);
353 pid = exim_fork(purpose);
355 /* Handle the child process. First, set the required environment. We must do
356 this before messing with the pipes, in order to be able to write debugging
357 output when things go wrong. */
361 signal(SIGUSR1, SIG_IGN);
362 signal(SIGPIPE, SIG_DFL);
364 if (newgid && setgid(*newgid) < 0)
366 DEBUG(D_any) debug_printf("failed to set gid=%ld in subprocess: %s\n",
367 (long int)(*newgid), strerror(errno));
371 if (newuid && setuid(*newuid) < 0)
373 DEBUG(D_any) debug_printf("failed to set uid=%ld in subprocess: %s\n",
374 (long int)(*newuid), strerror(errno));
378 (void)umask(newumask);
380 if (wd && Uchdir(wd) < 0)
382 DEBUG(D_any) debug_printf("failed to chdir to %s: %s\n", wd,
387 /* Becomes a process group leader if requested, and then organize the pipes.
388 Any unexpected failure is signalled with EX_EXECFAILED; these are all "should
389 never occur" failures, except for exec failing because the command doesn't
392 if (make_leader && setpgid(0,0) < 0)
394 DEBUG(D_any) debug_printf("failed to set group leader in subprocess: %s\n",
399 (void)close(inpfd[pipe_write]);
400 force_fd(inpfd[pipe_read], 0);
402 (void)close(outpfd[pipe_read]);
403 force_fd(outpfd[pipe_write], 1);
408 /* Now do the exec */
410 if (envp) execve(CS argv[0], (char *const *)argv, (char *const *)envp);
411 else execv(CS argv[0], (char *const *)argv);
413 /* Failed to execv. Signal this failure using EX_EXECFAILED. We are
414 losing the actual errno we got back, because there is no way to return
418 _exit(EX_EXECFAILED); /* Note: must be _exit(), NOT exit() */
421 /* Parent process. Save any fork failure code, and close the reading end of the
422 stdin pipe, and the writing end of the stdout pipe. */
425 (void)close(inpfd[pipe_read]);
426 (void)close(outpfd[pipe_write]);
428 /* Fork succeeded; return the input/output pipes and the pid */
432 *infdptr = inpfd[pipe_write];
433 *outfdptr = outpfd[pipe_read];
437 /* Fork failed; reset fork errno before returning */
439 (void)close(inpfd[pipe_write]);
440 (void)close(outpfd[pipe_read]);
448 /*************************************************
449 * Create child process without uid change *
450 *************************************************/
452 /* This function is a wrapper for child_open_uid() that doesn't have the uid,
453 gid and working directory changing arguments. The function is provided so as to
454 have a clean interface for use from local_scan(), but also saves writing NULL
455 arguments several calls that would otherwise use child_open_uid().
458 argv the argv for exec in the new process
459 envp the envp for exec in the new process
460 newumask umask to set in the new process
461 infdptr pointer to int into which the fd of the stdin of the new process
463 outfdptr pointer to int into which the fd of the stdout/stderr of the new
465 make_leader if TRUE, make the new process a process group leader
466 purpose for debug: reason for running the task
468 Returns: the pid of the created process or -1 if anything has gone wrong
472 child_open_function(uschar **argv, uschar **envp, int newumask, int *infdptr,
473 int *outfdptr, BOOL make_leader, const uschar * purpose)
475 return child_open_uid(CUSS argv, CUSS envp, newumask, NULL, NULL,
476 infdptr, outfdptr, NULL, make_leader, purpose);
482 /*************************************************
483 * Close down child process *
484 *************************************************/
486 /* Wait for the given process to finish, with optional timeout.
489 pid: the pid to wait for
490 timeout: maximum time to wait; 0 means for as long as it takes
492 Returns: >= 0 process terminated by exiting; value is process
493 ending status; if an execve() failed, the value
494 is typically 127 (defined as EX_EXECFAILED)
495 < 0 & > -256 process was terminated by a signal; value is the
496 negation of the signal number
498 -257 other error in wait(); errno still set
502 child_close(pid_t pid, int timeout)
508 sigalrm_seen = FALSE;
515 pid_t rc = waitpid(pid, &status, 0);
518 int lowbyte = status & 255;
519 yield = lowbyte == 0 ? (status >> 8) & 255 : -lowbyte;
524 /* This "shouldn't happen" test does happen on MacOS: for some reason
525 I do not understand we seems to get an alarm signal despite not having
526 an active alarm set. There seems to be only one, so just go round again. */
528 if (errno == EINTR && sigalrm_seen && timeout <= 0) continue;
530 yield = (errno == EINTR && sigalrm_seen) ? -256 : -257;
535 if (timeout > 0) ALARM_CLR(0);
537 signal(SIGCHLD, oldsignal); /* restore */