1 /* Waiting for a subprocess to finish.
2 Copyright (C) 2001-2003 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "wait-process.h"
32 #include <sys/types.h>
34 #if defined _MSC_VER || defined __MINGW32__
36 /* Native Woe32 API. */
38 #define waitpid(pid,statusp,options) _cwait (statusp, pid, WAIT_CHILD)
40 #define WTERMSIG(x) ((x) & 0xff) /* or: SIGABRT ?? */
41 #define WCOREDUMP(x) 0
42 #define WEXITSTATUS(x) (((x) >> 8) & 0xff) /* or: (x) ?? */
43 #define WIFSIGNALED(x) (WTERMSIG (x) != 0) /* or: ((x) == 3) ?? */
44 #define WIFEXITED(x) (WTERMSIG (x) == 0) /* or: ((x) != 3) ?? */
45 #define WIFSTOPPED(x) 0
51 /* On Linux, WEXITSTATUS are bits 15..8 and WTERMSIG are bits 7..0, while
52 BeOS uses the contrary. Therefore we use the abstract macros. */
54 # define WAIT_T union wait
56 # define WTERMSIG(x) ((x).w_termsig)
59 # define WCOREDUMP(x) ((x).w_coredump)
62 # define WEXITSTATUS(x) ((x).w_retcode)
67 # define WTERMSIG(x) ((x) & 0x7f)
70 # define WCOREDUMP(x) ((x) & 0x80)
73 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
76 /* For valid x, exactly one of WIFSIGNALED(x), WIFEXITED(x), WIFSTOPPED(x)
79 # define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
82 # define WIFEXITED(x) (WTERMSIG (x) == 0)
85 # define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
87 /* Note that portable applications may access
88 WTERMSIG(x) only if WIFSIGNALED(x) is true, and
89 WEXITSTATUS(x) only if WIFEXITED(x) is true. */
95 #include "fatal-signal.h"
99 #define _(str) gettext (str)
101 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
104 #if defined _MSC_VER || defined __MINGW32__
106 #define WIN32_LEAN_AND_MEAN
109 /* The return value of spawnvp() is really a process handle as returned
110 by CreateProcess(). Therefore we can kill it using TerminateProcess. */
111 #define kill(pid,sig) TerminateProcess ((HANDLE) (pid), sig)
116 /* Type of an entry in the slaves array.
117 The 'used' bit determines whether this entry is currently in use.
118 (If pid_t was an atomic type like sig_atomic_t, we could just set the
119 'child' field to 0 when unregistering a slave process, and wouldn't need
121 The 'used' and 'child' fields are accessed from within the cleanup_slaves()
122 action, therefore we mark them as 'volatile'. */
125 volatile sig_atomic_t used
;
126 volatile pid_t child
;
130 /* The registered slave subprocesses. */
131 static slaves_entry_t static_slaves
[32];
132 static slaves_entry_t
* volatile slaves
= static_slaves
;
133 static sig_atomic_t volatile slaves_count
= 0;
134 static size_t slaves_allocated
= SIZEOF (static_slaves
);
136 /* The termination signal for slave subprocesses.
137 2003-10-07: Terminator becomes Governator. */
139 # define TERMINATOR SIGHUP
141 # define TERMINATOR SIGTERM
144 /* The cleanup action. It gets called asynchronously. */
146 cleanup_slaves (void)
150 /* Get the last registered slave. */
151 size_t n
= slaves_count
;
156 /* Skip unused entries in the slaves array. */
159 pid_t slave
= slaves
[n
].child
;
161 /* Kill the slave. */
162 kill (slave
, TERMINATOR
);
167 /* Register a subprocess as being a slave process. This means that the
168 subprocess will be terminated when its creator receives a catchable fatal
169 signal or exits normally. Registration ends when wait_subprocess()
170 notices that the subprocess has exited. */
172 register_slave_subprocess (pid_t child
)
174 static bool cleanup_slaves_registered
= false;
175 if (!cleanup_slaves_registered
)
177 atexit (cleanup_slaves
);
178 at_fatal_signal (cleanup_slaves
);
179 cleanup_slaves_registered
= true;
182 /* Try to store the new slave in an unused entry of the slaves array. */
184 slaves_entry_t
*s
= slaves
;
185 slaves_entry_t
*s_end
= s
+ slaves_count
;
187 for (; s
< s_end
; s
++)
190 /* The two uses of 'volatile' in the slaves_entry_t type above
191 (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
192 entry as used only after the child pid has been written to the
193 memory location s->child. */
200 if (slaves_count
== slaves_allocated
)
202 /* Extend the slaves array. Note that we cannot use xrealloc(),
203 because then the cleanup_slaves() function could access an already
204 deallocated array. */
205 slaves_entry_t
*old_slaves
= slaves
;
206 size_t new_slaves_allocated
= 2 * slaves_allocated
;
207 slaves_entry_t
*new_slaves
=
208 malloc (new_slaves_allocated
* sizeof (slaves_entry_t
));
209 if (new_slaves
== NULL
)
211 /* xalloc_die() will call exit() which will invoke cleanup_slaves().
212 Additionally we need to kill child, because it's not yet among
214 kill (child
, TERMINATOR
);
217 memcpy (new_slaves
, old_slaves
,
218 slaves_allocated
* sizeof (slaves_entry_t
));
220 slaves_allocated
= new_slaves_allocated
;
221 /* Now we can free the old slaves array. */
222 if (old_slaves
!= static_slaves
)
225 /* The three uses of 'volatile' in the types above (and ISO C 99 section
226 5.1.2.3.(5)) ensure that we increment the slaves_count only after the
227 new slave and its 'used' bit have been written to the memory locations
228 that make up slaves[slaves_count]. */
229 slaves
[slaves_count
].child
= child
;
230 slaves
[slaves_count
].used
= 1;
234 /* Unregister a child from the list of slave subprocesses. */
236 unregister_slave_subprocess (pid_t child
)
238 /* The easiest way to remove an entry from a list that can be used by
239 an asynchronous signal handler is just to mark it as unused. For this,
240 we rely on sig_atomic_t. */
241 slaves_entry_t
*s
= slaves
;
242 slaves_entry_t
*s_end
= s
+ slaves_count
;
244 for (; s
< s_end
; s
++)
245 if (s
->used
&& s
->child
== child
)
250 /* Wait for a subprocess to finish. Return its exit code.
251 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
254 wait_subprocess (pid_t child
, const char *progname
,
255 bool ignore_sigpipe
, bool null_stderr
,
256 bool slave_process
, bool exit_on_error
)
258 #if HAVE_WAITID && defined WNOWAIT && 0
259 /* Commented out because waitid() with WNOWAIT doesn't work: On Solaris 7
260 and OSF/1 4.0, it returns -1 and sets errno = ECHILD, and on HP-UX 10.20
262 /* Use of waitid() with WNOWAIT avoids a race condition: If slave_process is
263 true, and this process sleeps a very long time between the return from
264 waitpid() and the execution of unregister_slave_subprocess(), and
265 meanwhile another process acquires the same PID as child, and then - still
266 before unregister_slave_subprocess() - this process gets a fatal signal,
267 it would kill the other totally unrelated process. */
271 if (waitid (P_PID
, child
, &info
, slave_process
? WNOWAIT
: 0) < 0)
277 if (exit_on_error
|| !null_stderr
)
278 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
279 _("%s subprocess"), progname
);
283 /* info.si_code is set to one of CLD_EXITED, CLD_KILLED, CLD_DUMPED,
284 CLD_TRAPPED, CLD_STOPPED, CLD_CONTINUED. Loop until the program
286 if (info
.si_code
== CLD_EXITED
287 || info
.si_code
== CLD_KILLED
|| info
.si_code
== CLD_DUMPED
)
291 /* The child process has exited or was signalled. */
295 /* Unregister the child from the list of slave subprocesses, so that
296 later, when we exit, we don't kill a totally unrelated process which
297 may have acquired the same pid. */
298 unregister_slave_subprocess (child
);
300 /* Now remove the zombie from the process list. */
303 if (waitid (P_PID
, child
, &info
, 0) < 0)
309 if (exit_on_error
|| !null_stderr
)
310 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
311 _("%s subprocess"), progname
);
318 switch (info
.si_code
)
323 if (info
.si_status
== SIGPIPE
&& ignore_sigpipe
)
326 if (exit_on_error
|| !null_stderr
)
327 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
328 _("%s subprocess got fatal signal %d"),
329 progname
, info
.si_status
);
332 if (info
.si_status
== 127)
334 if (exit_on_error
|| !null_stderr
)
335 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
336 _("%s subprocess failed"), progname
);
339 return info
.si_status
;
344 /* waitpid() is just as portable as wait() nowadays. */
347 *(int *) &status
= 0;
350 int result
= waitpid (child
, &status
, 0);
358 # if 0 /* defined ECHILD */
361 /* Child process nonexistent?! Assume it terminated
363 *(int *) &status
= 0;
367 if (exit_on_error
|| !null_stderr
)
368 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
369 _("%s subprocess"), progname
);
373 /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
374 must always be true. Loop until the program terminates. */
375 if (!WIFSTOPPED (status
))
379 /* The child process has exited or was signalled. */
382 /* Unregister the child from the list of slave subprocesses, so that
383 later, when we exit, we don't kill a totally unrelated process which
384 may have acquired the same pid. */
385 unregister_slave_subprocess (child
);
387 if (WIFSIGNALED (status
))
390 if (WTERMSIG (status
) == SIGPIPE
&& ignore_sigpipe
)
393 if (exit_on_error
|| !null_stderr
)
394 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
395 _("%s subprocess got fatal signal %d"),
396 progname
, (int) WTERMSIG (status
));
399 if (WEXITSTATUS (status
) == 127)
401 if (exit_on_error
|| !null_stderr
)
402 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
403 _("%s subprocess failed"), progname
);
406 return WEXITSTATUS (status
);