Fix "(during SMTP connection from NULL)" bug.
[exim.git] / src / src / child.c
CommitLineData
59e82a2a 1/* $Cambridge: exim/src/src/child.c,v 1.8 2006/02/07 14:05:17 ph10 Exp $ */
059ec3d9
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
d7d7b7b9 7/* Copyright (c) University of Cambridge 1995 - 2006 */
059ec3d9
PH
8/* See the file NOTICE for conditions of use and distribution. */
9
10
11#include "exim.h"
12
13static 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
21specific value (one of 0, 1, 2). If it hasn't got it already, close the value
22we want, duplicate the fd, then close the old one.
23
24Arguments:
25 oldfd original fd
26 newfd the fd we want
27
28Returns: nothing
29*/
30
31static void
32force_fd(int oldfd, int newfd)
33{
34if (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
47current process. This is different to child_open_exim(), which runs another
48Exim process in parallel (but it then calls this function). The function's
49basic job is to build the argv list according to the values of current options
50settings. There is a basic list that all calls require, and an additional list
51that some do not require. Further additions can be given as additional
52arguments. An option specifies whether the exec() is actually to happen, and if
53so, what is to be done if it fails.
54
55Arguments:
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
67Returns: if CEE_RETURN_ARGV is given, returns a pointer to argv;
68 otherwise, does not return
69*/
70
71uschar **
72child_exec_exim(int exec_type, BOOL kill_v, int *pcount, BOOL minimal,
73 int acount, ...)
74{
75int first_special = -1;
76int n = 0;
77int extra = (pcount != NULL)? *pcount : 0;
78uschar **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
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 }
90if (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
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 }
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,
121for more helpful diagnosis on failure. */
122
123if (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
135argv[n] = NULL;
136if (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
143failure. We know that there will always be at least one extra option in the
144call when exec() is done here, so it can be used to add to the panic data. */
145
146DEBUG(D_exec) debug_print_argv(argv);
147exim_nullstd(); /* Make sure std{in,out,err} exist */
148execv(CS argv[0], (char *const *)argv);
149
150log_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.
156Note: this must be _exit(), not exit(). */
157
158_exit(EX_EXECFAILED);
159
160return 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
171in order to inject a message via the standard input. The function creates a
172child process and runs Exim in it. It sets up a pipe to the standard input of
173the new process, and returns that to the caller via fdptr. The function returns
174the pid of the new process, or -1 if things go wrong. If debug_fd is
175non-negative, it is passed as stderr.
176
177Argument: fdptr pointer to int for the stdin fd
178Returns: pid of the created process or -1 if anything has gone wrong
179*/
180
181pid_t
182child_open_exim(int *fdptr)
183{
184int pfd[2];
185int save_errno;
186pid_t pid;
187
188/* Create the pipe and fork the process. Ensure that SIGCHLD is set to
189SIG_DFL before forking, so that the child process can be waited for. We
190sometimes get here with it set otherwise. Save the old state for resetting
191on the wait. */
192
193if (pipe(pfd) != 0) return (pid_t)(-1);
194oldsignal = signal(SIGCHLD, SIG_DFL);
195pid = fork();
196
197/* Child process: make the reading end of the pipe into the standard input and
198close the writing end. If debugging, pass debug_fd as stderr. Then re-exec
199Exim. Failure is signalled with EX_EXECFAILED, but this shouldn't occur! */
200
201if (pid == 0)
202 {
203 force_fd(pfd[pipe_read], 0);
f1e894f3 204 (void)close(pfd[pipe_write]);
059ec3d9
PH
205 if (debug_fd > 0) force_fd(debug_fd, 2);
206 if (bounce_sender_authentication != NULL)
207 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 8,
208 US"-t", US"-oem", US"-oi", US"-f", US"<>", US"-oMas",
209 bounce_sender_authentication, message_id_option);
210 else
211 child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 6,
212 US"-t", US"-oem", US"-oi", US"-f", US"<>", message_id_option);
213 /* Control does not return here. */
214 }
215
216/* Parent process. Save fork() errno and close the reading end of the stdin
217pipe. */
218
219save_errno = errno;
f1e894f3 220(void)close(pfd[pipe_read]);
059ec3d9
PH
221
222/* Fork succeeded */
223
224if (pid > 0)
225 {
226 *fdptr = pfd[pipe_write]; /* return writing end of stdin pipe */
227 return pid; /* and pid of new process */
228 }
229
230/* Fork failed */
231
f1e894f3 232(void)close(pfd[pipe_write]);
059ec3d9
PH
233errno = save_errno;
234return (pid_t)(-1);
235}
e7726cbf 236#endif
059ec3d9
PH
237
238
239
240/*************************************************
241* Create a non-Exim child process *
242*************************************************/
243
244/* This function creates a child process and runs the given command in it. It
245sets up pipes to the standard input and output of the new process, and returns
246them to the caller. The standard error is cloned to the output. If there are
247any file descriptors "in the way" in the new process, they are closed. A new
248umask is supplied for the process, and an optional new uid and gid are also
249available. These are used by the queryprogram router to set an unprivileged id.
b668c215
PH
250SIGUSR1 is always disabled in the new process, as it is not going to be running
251Exim (the function child_open_exim() is provided for that). This function
252returns the pid of the new process, or -1 if things go wrong.
059ec3d9
PH
253
254Arguments:
255 argv the argv for exec in the new process
256 envp the envp for exec in the new process
257 newumask umask to set in the new process
258 newuid point to uid for the new process or NULL for no change
259 newgid point to gid for the new process or NULL for no change
260 infdptr pointer to int into which the fd of the stdin of the new process
261 is placed
262 outfdptr pointer to int into which the fd of the stdout/stderr of the new
263 process is placed
264 wd if not NULL, a path to be handed to chdir() in the new process
265 make_leader if TRUE, make the new process a process group leader
8e669ac1 266
059ec3d9
PH
267Returns: the pid of the created process or -1 if anything has gone wrong
268*/
269
270pid_t
271child_open_uid(uschar **argv, uschar **envp, int newumask, uid_t *newuid,
272 gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd, BOOL make_leader)
273{
274int save_errno;
275int inpfd[2], outpfd[2];
276pid_t pid;
277
278/* Create the pipes. */
279
280if (pipe(inpfd) != 0) return (pid_t)(-1);
281if (pipe(outpfd) != 0)
282 {
f1e894f3
PH
283 (void)close(inpfd[pipe_read]);
284 (void)close(inpfd[pipe_write]);
059ec3d9
PH
285 return (pid_t)(-1);
286 }
287
288/* Fork the process. Ensure that SIGCHLD is set to SIG_DFL before forking, so
289that the child process can be waited for. We sometimes get here with it set
290otherwise. Save the old state for resetting on the wait. */
291
292oldsignal = signal(SIGCHLD, SIG_DFL);
293pid = fork();
294
59e82a2a
PH
295/* Handle the child process. First, set the required environment. We must do
296this before messing with the pipes, in order to be able to write debugging
297output when things go wrong. */
059ec3d9
PH
298
299if (pid == 0)
300 {
59e82a2a
PH
301 signal(SIGUSR1, SIG_IGN);
302
303 if (newgid != NULL && setgid(*newgid) < 0)
304 {
305 DEBUG(D_any) debug_printf("failed to set gid=%ld in subprocess: %s\n",
306 (long int)(*newgid), strerror(errno));
307 goto CHILD_FAILED;
308 }
309
310 if (newuid != NULL && setuid(*newuid) < 0)
311 {
312 DEBUG(D_any) debug_printf("failed to set uid=%ld in subprocess: %s\n",
313 (long int)(*newuid), strerror(errno));
314 goto CHILD_FAILED;
315 }
316
317 (void)umask(newumask);
318
319 if (wd != NULL && Uchdir(wd) < 0)
320 {
321 DEBUG(D_any) debug_printf("failed to chdir to %s: %s\n", wd,
322 strerror(errno));
323 goto CHILD_FAILED;
324 }
325
326 /* Becomes a process group leader if requested, and then organize the pipes.
327 Any unexpected failure is signalled with EX_EXECFAILED; these are all "should
328 never occur" failures, except for exec failing because the command doesn't
329 exist. */
330
331 if (make_leader && setpgid(0,0) < 0)
332 {
333 DEBUG(D_any) debug_printf("failed to set group leader in subprocess: %s\n",
334 strerror(errno));
335 goto CHILD_FAILED;
336 }
059ec3d9 337
f1e894f3 338 (void)close(inpfd[pipe_write]);
059ec3d9
PH
339 force_fd(inpfd[pipe_read], 0);
340
f1e894f3 341 (void)close(outpfd[pipe_read]);
059ec3d9
PH
342 force_fd(outpfd[pipe_write], 1);
343
f1e894f3
PH
344 (void)close(2);
345 (void)dup2(1, 2);
059ec3d9 346
059ec3d9
PH
347 /* Now do the exec */
348
349 if (envp == NULL) execv(CS argv[0], (char *const *)argv);
350 else execve(CS argv[0], (char *const *)argv, (char *const *)envp);
351
352 /* Failed to execv. Signal this failure using EX_EXECFAILED. We are
353 losing the actual errno we got back, because there is no way to return
59e82a2a 354 this information. */
059ec3d9
PH
355
356 CHILD_FAILED:
357 _exit(EX_EXECFAILED); /* Note: must be _exit(), NOT exit() */
358 }
359
360/* Parent process. Save any fork failure code, and close the reading end of the
361stdin pipe, and the writing end of the stdout pipe. */
362
363save_errno = errno;
f1e894f3
PH
364(void)close(inpfd[pipe_read]);
365(void)close(outpfd[pipe_write]);
059ec3d9
PH
366
367/* Fork succeeded; return the input/output pipes and the pid */
368
369if (pid > 0)
370 {
371 *infdptr = inpfd[pipe_write];
372 *outfdptr = outpfd[pipe_read];
373 return pid;
374 }
375
376/* Fork failed; reset fork errno before returning */
377
f1e894f3
PH
378(void)close(inpfd[pipe_write]);
379(void)close(outpfd[pipe_read]);
059ec3d9
PH
380errno = save_errno;
381return (pid_t)(-1);
382}
383
384
385
386
387/*************************************************
388* Create child process without uid change *
389*************************************************/
390
391/* This function is a wrapper for child_open_uid() that doesn't have the uid,
b668c215
PH
392gid and working directory changing arguments. The function is provided so as to
393have a clean interface for use from local_scan(), but also saves writing NULL
394arguments several calls that would otherwise use child_open_uid().
059ec3d9
PH
395
396Arguments:
397 argv the argv for exec in the new process
398 envp the envp for exec in the new process
399 newumask umask to set in the new process
400 infdptr pointer to int into which the fd of the stdin of the new process
401 is placed
402 outfdptr pointer to int into which the fd of the stdout/stderr of the new
403 process is placed
404 make_leader if TRUE, make the new process a process group leader
405
406Returns: the pid of the created process or -1 if anything has gone wrong
407*/
408
409pid_t
410child_open(uschar **argv, uschar **envp, int newumask, int *infdptr,
411 int *outfdptr, BOOL make_leader)
412{
413return child_open_uid(argv, envp, newumask, NULL, NULL, infdptr, outfdptr,
414 NULL, make_leader);
415}
416
417
418
419
420/*************************************************
421* Close down child process *
422*************************************************/
423
424/* Wait for the given process to finish, with optional timeout.
425
426Arguments
427 pid: the pid to wait for
428 timeout: maximum time to wait; 0 means for as long as it takes
429
430Returns: >= 0 process terminated by exiting; value is process
431 ending status; if an execve() failed, the value
432 is typically 127 (defined as EX_EXECFAILED)
433 < 0 & > -256 process was terminated by a signal; value is the
434 negation of the signal number
435 -256 timed out
436 -257 other error in wait(); errno still set
437*/
438
439int
440child_close(pid_t pid, int timeout)
441{
442int yield;
443
444if (timeout > 0)
445 {
446 sigalrm_seen = FALSE;
447 alarm(timeout);
448 }
449
450for(;;)
451 {
452 int status;
453 pid_t rc = waitpid(pid, &status, 0);
454 if (rc == pid)
455 {
456 int lowbyte = status & 255;
457 if (lowbyte == 0) yield = (status >> 8) & 255;
458 else yield = -lowbyte;
459 break;
460 }
461 if (rc < 0)
462 {
463 yield = (errno == EINTR && sigalrm_seen)? -256 : -257;
464 break;
465 }
466 }
467
468if (timeout > 0) alarm(0);
469
470signal(SIGCHLD, oldsignal); /* restore */
471return yield;
472}
473
474/* End of child.c */