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