debian experimental exim-daemon-heavy config
[exim.git] / src / src / child.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
3386088d 5/* Copyright (c) University of Cambridge 1995 - 2015 */
1e1ddfac 6/* Copyright (c) The Exim Maintainers 2020 */
059ec3d9
PH
7/* See the file NOTICE for conditions of use and distribution. */
8
9
10#include "exim.h"
11
12static void (*oldsignal)(int);
13
14
15/*************************************************
16* Ensure an fd has a given value *
17*************************************************/
18
19/* This function is called when we want to ensure that a certain fd has a
20specific value (one of 0, 1, 2). If it hasn't got it already, close the value
21we want, duplicate the fd, then close the old one.
22
23Arguments:
24 oldfd original fd
25 newfd the fd we want
26
27Returns: nothing
28*/
29
30static void
31force_fd(int oldfd, int newfd)
32{
33if (oldfd == newfd) return;
f1e894f3
PH
34(void)close(newfd);
35(void)dup2(oldfd, newfd);
36(void)close(oldfd);
059ec3d9
PH
37}
38
39
e7726cbf 40#ifndef STAND_ALONE
059ec3d9
PH
41/*************************************************
42* Build argv list and optionally re-exec Exim *
43*************************************************/
44
45/* This function is called when Exim wants to re-exec (overlay) itself in the
46current process. This is different to child_open_exim(), which runs another
47Exim process in parallel (but it then calls this function). The function's
48basic job is to build the argv list according to the values of current options
49settings. There is a basic list that all calls require, and an additional list
50that some do not require. Further additions can be given as additional
51arguments. An option specifies whether the exec() is actually to happen, and if
52so, what is to be done if it fails.
53
54Arguments:
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
61 number of slots used
62 minimal TRUE if only minimal argv is required
63 acount number of additional arguments
64 ... further values to add to argv
65
66Returns: if CEE_RETURN_ARGV is given, returns a pointer to argv;
67 otherwise, does not return
68*/
69
70uschar **
71child_exec_exim(int exec_type, BOOL kill_v, int *pcount, BOOL minimal,
72 int acount, ...)
73{
74int first_special = -1;
75int n = 0;
e37f8a84 76int extra = pcount ? *pcount : 0;
8ac90765
JH
77uschar **argv;
78
eb24befc 79argv = store_get((extra + acount + MAX_CLMACROS + 21) * sizeof(char *), FALSE);
059ec3d9
PH
80
81/* In all case, the list starts out with the path, any macros, and a changed
82config file. */
83
84argv[n++] = exim_path;
85if (clmacro_count > 0)
86 {
87 memcpy(argv + n, clmacros, clmacro_count * sizeof(uschar *));
88 n += clmacro_count;
89 }
8768d548 90if (f.config_changed)
059ec3d9
PH
91 {
92 argv[n++] = US"-C";
93 argv[n++] = config_main_filename;
94 }
95
96/* These values are added only for non-minimal cases. If debug_selector is
97precisely D_v, we have to assume this was started by a non-admin user, and
98we suppress the flag when requested. (This happens when passing on an SMTP
99connection, and after ETRN.) If there's more debugging going on, an admin user
100was involved, so we do pass it on. */
101
102if (!minimal)
103 {
104 if (debug_selector == D_v)
105 {
106 if (!kill_v) argv[n++] = US"-v";
107 }
108 else
109 {
110 if (debug_selector != 0)
111 argv[n++] = string_sprintf("-d=0x%x", debug_selector);
112 }
32393657
JH
113 DEBUG(D_any)
114 {
115 argv[n++] = US"-MCd";
116 argv[n++] = US process_purpose;
117 }
ff966302 118 if (!f.testsuite_delays) argv[n++] = US"-odd";
8768d548
JH
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";
059ec3d9
PH
122 if (connection_max_messages >= 0)
123 argv[n++] = string_sprintf("-oB%d", connection_max_messages);
e37f8a84
JH
124 if (*queue_name)
125 {
126 argv[n++] = US"-MCG";
127 argv[n++] = queue_name;
128 }
059ec3d9
PH
129 }
130
131/* Now add in any others that are in the call. Remember which they were,
132for more helpful diagnosis on failure. */
133
134if (acount > 0)
135 {
136 va_list ap;
137 va_start(ap, acount);
138 first_special = n;
139 while (acount-- > 0)
140 argv[n++] = va_arg(ap, uschar *);
141 va_end(ap);
142 }
143
144/* Terminate the list, and return it, if that is what is wanted. */
145
146argv[n] = NULL;
147if (exec_type == CEE_RETURN_ARGV)
148 {
4b01271f 149 if (pcount) *pcount = n;
059ec3d9
PH
150 return argv;
151 }
152
153/* Otherwise, do the exec() here, and handle the consequences of an unexpected
154failure. We know that there will always be at least one extra option in the
155call when exec() is done here, so it can be used to add to the panic data. */
156
55414b25 157DEBUG(D_exec) debug_print_argv(CUSS argv);
059ec3d9
PH
158exim_nullstd(); /* Make sure std{in,out,err} exist */
159execv(CS argv[0], (char *const *)argv);
160
161log_write(0,
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],
164 strerror(errno));
165
166/* Get here if exec_type == CEE_EXEC_EXIT.
167Note: this must be _exit(), not exit(). */
168
169_exit(EX_EXECFAILED);
170
171return NULL; /* To keep compilers happy */
172}
173
174
175
176
177/*************************************************
178* Create a child Exim process *
179*************************************************/
180
181/* This function is called when Exim wants to run a parallel instance of itself
182in order to inject a message via the standard input. The function creates a
183child process and runs Exim in it. It sets up a pipe to the standard input of
184the new process, and returns that to the caller via fdptr. The function returns
185the pid of the new process, or -1 if things go wrong. If debug_fd is
186non-negative, it is passed as stderr.
187
5977a0b3
PH
188This interface is now a just wrapper for the more complicated function
189child_open_exim2(), which has additional arguments. The wrapper must continue
190to exist, even if all calls from within Exim are changed, because it is
191documented for use from local_scan().
192
059ec3d9 193Argument: fdptr pointer to int for the stdin fd
8e9fdd63 194 purpose of the child process, for debug
059ec3d9
PH
195Returns: pid of the created process or -1 if anything has gone wrong
196*/
197
198pid_t
8e9fdd63 199child_open_exim_function(int * fdptr, const uschar * purpose)
059ec3d9 200{
8e9fdd63
JH
201return child_open_exim2_function(fdptr, US"<>", bounce_sender_authentication,
202 purpose);
5977a0b3
PH
203}
204
205
206/* This is a more complicated function for creating a child Exim process, with
207more arguments.
208
209Arguments:
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
8e9fdd63 213 purpose of the child process, for debug
5977a0b3
PH
214
215Returns: pid of the created process or -1 if anything has gone wrong
216*/
217
218pid_t
8e9fdd63
JH
219child_open_exim2_function(int * fdptr, uschar * sender,
220 uschar * sender_authentication, const uschar * purpose)
5977a0b3 221{
059ec3d9
PH
222int pfd[2];
223int save_errno;
224pid_t pid;
225
226/* Create the pipe and fork the process. Ensure that SIGCHLD is set to
227SIG_DFL before forking, so that the child process can be waited for. We
228sometimes get here with it set otherwise. Save the old state for resetting
229on the wait. */
230
231if (pipe(pfd) != 0) return (pid_t)(-1);
232oldsignal = signal(SIGCHLD, SIG_DFL);
8e9fdd63 233pid = exim_fork(purpose);
059ec3d9
PH
234
235/* Child process: make the reading end of the pipe into the standard input and
236close the writing end. If debugging, pass debug_fd as stderr. Then re-exec
4c04137d 237Exim with appropriate options. In the test harness, use -odi unless queue_only
75e0e026
PH
238is set, so that the bounce is fully delivered before returning. Failure is
239signalled with EX_EXECFAILED (specified by CEE_EXEC_EXIT), but this shouldn't
240occur. */
059ec3d9
PH
241
242if (pid == 0)
243 {
244 force_fd(pfd[pipe_read], 0);
f1e894f3 245 (void)close(pfd[pipe_write]);
059ec3d9 246 if (debug_fd > 0) force_fd(debug_fd, 2);
8768d548 247 if (f.running_in_test_harness && !queue_only)
75e0e026 248 {
96c81511 249 if (sender_authentication)
75e0e026
PH
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);
253 else
254 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 7,
255 US "-odi", US"-t", US"-oem", US"-oi", US"-f", sender,
256 message_id_option);
257 /* Control does not return here. */
258 }
259 else /* Not test harness */
260 {
96c81511 261 if (sender_authentication)
75e0e026
PH
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);
265 else
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. */
269 }
059ec3d9
PH
270 }
271
272/* Parent process. Save fork() errno and close the reading end of the stdin
273pipe. */
274
275save_errno = errno;
f1e894f3 276(void)close(pfd[pipe_read]);
059ec3d9
PH
277
278/* Fork succeeded */
279
280if (pid > 0)
281 {
282 *fdptr = pfd[pipe_write]; /* return writing end of stdin pipe */
283 return pid; /* and pid of new process */
284 }
285
286/* Fork failed */
287
f1e894f3 288(void)close(pfd[pipe_write]);
059ec3d9
PH
289errno = save_errno;
290return (pid_t)(-1);
291}
75e0e026 292#endif /* STAND_ALONE */
059ec3d9
PH
293
294
295
296/*************************************************
297* Create a non-Exim child process *
298*************************************************/
299
300/* This function creates a child process and runs the given command in it. It
301sets up pipes to the standard input and output of the new process, and returns
302them to the caller. The standard error is cloned to the output. If there are
303any file descriptors "in the way" in the new process, they are closed. A new
304umask is supplied for the process, and an optional new uid and gid are also
305available. These are used by the queryprogram router to set an unprivileged id.
b668c215
PH
306SIGUSR1 is always disabled in the new process, as it is not going to be running
307Exim (the function child_open_exim() is provided for that). This function
308returns the pid of the new process, or -1 if things go wrong.
059ec3d9
PH
309
310Arguments:
311 argv the argv for exec in the new process
312 envp the envp for exec in the new process
313 newumask umask to set in the new process
314 newuid point to uid for the new process or NULL for no change
315 newgid point to gid for the new process or NULL for no change
316 infdptr pointer to int into which the fd of the stdin of the new process
317 is placed
318 outfdptr pointer to int into which the fd of the stdout/stderr of the new
319 process is placed
320 wd if not NULL, a path to be handed to chdir() in the new process
321 make_leader if TRUE, make the new process a process group leader
eb24befc 322 purpose for debug: reason for running the task
8e669ac1 323
059ec3d9
PH
324Returns: the pid of the created process or -1 if anything has gone wrong
325*/
326
327pid_t
55414b25
JH
328child_open_uid(const uschar **argv, const uschar **envp, int newumask,
329 uid_t *newuid, gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd,
eb24befc 330 BOOL make_leader, const uschar * purpose)
059ec3d9
PH
331{
332int save_errno;
333int inpfd[2], outpfd[2];
334pid_t pid;
335
336/* Create the pipes. */
337
338if (pipe(inpfd) != 0) return (pid_t)(-1);
339if (pipe(outpfd) != 0)
340 {
f1e894f3
PH
341 (void)close(inpfd[pipe_read]);
342 (void)close(inpfd[pipe_write]);
059ec3d9
PH
343 return (pid_t)(-1);
344 }
345
346/* Fork the process. Ensure that SIGCHLD is set to SIG_DFL before forking, so
347that the child process can be waited for. We sometimes get here with it set
348otherwise. Save the old state for resetting on the wait. */
349
350oldsignal = signal(SIGCHLD, SIG_DFL);
eb24befc 351pid = exim_fork(purpose);
059ec3d9 352
59e82a2a
PH
353/* Handle the child process. First, set the required environment. We must do
354this before messing with the pipes, in order to be able to write debugging
355output when things go wrong. */
059ec3d9
PH
356
357if (pid == 0)
358 {
59e82a2a 359 signal(SIGUSR1, SIG_IGN);
605021fc 360 signal(SIGPIPE, SIG_DFL);
59e82a2a 361
96c81511 362 if (newgid && setgid(*newgid) < 0)
59e82a2a
PH
363 {
364 DEBUG(D_any) debug_printf("failed to set gid=%ld in subprocess: %s\n",
365 (long int)(*newgid), strerror(errno));
366 goto CHILD_FAILED;
367 }
368
96c81511 369 if (newuid && setuid(*newuid) < 0)
59e82a2a
PH
370 {
371 DEBUG(D_any) debug_printf("failed to set uid=%ld in subprocess: %s\n",
372 (long int)(*newuid), strerror(errno));
373 goto CHILD_FAILED;
374 }
375
376 (void)umask(newumask);
377
96c81511 378 if (wd && Uchdir(wd) < 0)
59e82a2a
PH
379 {
380 DEBUG(D_any) debug_printf("failed to chdir to %s: %s\n", wd,
381 strerror(errno));
382 goto CHILD_FAILED;
383 }
384
385 /* Becomes a process group leader if requested, and then organize the pipes.
386 Any unexpected failure is signalled with EX_EXECFAILED; these are all "should
387 never occur" failures, except for exec failing because the command doesn't
388 exist. */
389
390 if (make_leader && setpgid(0,0) < 0)
391 {
392 DEBUG(D_any) debug_printf("failed to set group leader in subprocess: %s\n",
393 strerror(errno));
394 goto CHILD_FAILED;
395 }
059ec3d9 396
f1e894f3 397 (void)close(inpfd[pipe_write]);
059ec3d9
PH
398 force_fd(inpfd[pipe_read], 0);
399
f1e894f3 400 (void)close(outpfd[pipe_read]);
059ec3d9
PH
401 force_fd(outpfd[pipe_write], 1);
402
f1e894f3
PH
403 (void)close(2);
404 (void)dup2(1, 2);
059ec3d9 405
059ec3d9
PH
406 /* Now do the exec */
407
96c81511
JH
408 if (envp) execve(CS argv[0], (char *const *)argv, (char *const *)envp);
409 else execv(CS argv[0], (char *const *)argv);
059ec3d9
PH
410
411 /* Failed to execv. Signal this failure using EX_EXECFAILED. We are
412 losing the actual errno we got back, because there is no way to return
59e82a2a 413 this information. */
059ec3d9
PH
414
415 CHILD_FAILED:
416 _exit(EX_EXECFAILED); /* Note: must be _exit(), NOT exit() */
417 }
418
419/* Parent process. Save any fork failure code, and close the reading end of the
420stdin pipe, and the writing end of the stdout pipe. */
421
422save_errno = errno;
f1e894f3
PH
423(void)close(inpfd[pipe_read]);
424(void)close(outpfd[pipe_write]);
059ec3d9
PH
425
426/* Fork succeeded; return the input/output pipes and the pid */
427
428if (pid > 0)
429 {
430 *infdptr = inpfd[pipe_write];
431 *outfdptr = outpfd[pipe_read];
432 return pid;
433 }
434
435/* Fork failed; reset fork errno before returning */
436
f1e894f3
PH
437(void)close(inpfd[pipe_write]);
438(void)close(outpfd[pipe_read]);
059ec3d9
PH
439errno = save_errno;
440return (pid_t)(-1);
441}
442
443
444
445
446/*************************************************
447* Create child process without uid change *
448*************************************************/
449
450/* This function is a wrapper for child_open_uid() that doesn't have the uid,
b668c215
PH
451gid and working directory changing arguments. The function is provided so as to
452have a clean interface for use from local_scan(), but also saves writing NULL
453arguments several calls that would otherwise use child_open_uid().
059ec3d9
PH
454
455Arguments:
456 argv the argv for exec in the new process
457 envp the envp for exec in the new process
458 newumask umask to set in the new process
459 infdptr pointer to int into which the fd of the stdin of the new process
460 is placed
461 outfdptr pointer to int into which the fd of the stdout/stderr of the new
462 process is placed
463 make_leader if TRUE, make the new process a process group leader
eb24befc 464 purpose for debug: reason for running the task
059ec3d9
PH
465
466Returns: the pid of the created process or -1 if anything has gone wrong
467*/
468
469pid_t
eb24befc
JH
470child_open_function(uschar **argv, uschar **envp, int newumask, int *infdptr,
471 int *outfdptr, BOOL make_leader, const uschar * purpose)
059ec3d9 472{
55414b25 473return child_open_uid(CUSS argv, CUSS envp, newumask, NULL, NULL,
eb24befc 474 infdptr, outfdptr, NULL, make_leader, purpose);
059ec3d9
PH
475}
476
477
478
479
480/*************************************************
481* Close down child process *
482*************************************************/
483
484/* Wait for the given process to finish, with optional timeout.
485
486Arguments
487 pid: the pid to wait for
488 timeout: maximum time to wait; 0 means for as long as it takes
489
490Returns: >= 0 process terminated by exiting; value is process
491 ending status; if an execve() failed, the value
492 is typically 127 (defined as EX_EXECFAILED)
493 < 0 & > -256 process was terminated by a signal; value is the
494 negation of the signal number
495 -256 timed out
496 -257 other error in wait(); errno still set
497*/
498
499int
500child_close(pid_t pid, int timeout)
501{
502int yield;
503
504if (timeout > 0)
505 {
506 sigalrm_seen = FALSE;
c2a1bba0 507 ALARM(timeout);
059ec3d9
PH
508 }
509
510for(;;)
511 {
512 int status;
513 pid_t rc = waitpid(pid, &status, 0);
514 if (rc == pid)
515 {
516 int lowbyte = status & 255;
d7625a4d 517 yield = lowbyte == 0 ? (status >> 8) & 255 : -lowbyte;
059ec3d9
PH
518 break;
519 }
520 if (rc < 0)
521 {
d7625a4d
JH
522 /* This "shouldn't happen" test does happen on MacOS: for some reason
523 I do not understand we seems to get an alarm signal despite not having
524 an active alarm set. There seems to be only one, so just go round again. */
525
526 if (errno == EINTR && sigalrm_seen && timeout <= 0) continue;
527
528 yield = (errno == EINTR && sigalrm_seen) ? -256 : -257;
059ec3d9
PH
529 break;
530 }
531 }
532
c2a1bba0 533if (timeout > 0) ALARM_CLR(0);
059ec3d9
PH
534
535signal(SIGCHLD, oldsignal); /* restore */
536return yield;
537}
538
539/* End of child.c */