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 /* Type of an entry in the slaves array.
105 The 'used' bit determines whether this entry is currently in use.
106 (If pid_t was an atomic type like sig_atomic_t, we could just set the
107 'child' field to 0 when unregistering a slave process, and wouldn't need
109 The 'used' and 'child' fields are accessed from within the cleanup_slaves()
110 action, therefore we mark them as 'volatile'. */
113 volatile sig_atomic_t used;
114 volatile pid_t child;
118 /* The registered slave subprocesses. */
119 static slaves_entry_t static_slaves[32];
120 static slaves_entry_t * volatile slaves = static_slaves;
121 static sig_atomic_t volatile slaves_count = 0;
122 static size_t slaves_allocated = SIZEOF (static_slaves);
124 /* The termination signal for slave subprocesses.
125 2003-10-07: Terminator becomes Governator. */
127 # define TERMINATOR SIGHUP
129 # define TERMINATOR SIGTERM
132 /* The cleanup action. It gets called asynchronously. */
138 /* Get the last registered slave. */
139 size_t n = slaves_count;
144 /* Skip unused entries in the slaves array. */
147 pid_t slave = slaves[n].child;
149 /* Kill the slave. */
150 kill (slave, TERMINATOR);
155 /* Register a subprocess as being a slave process. This means that the
156 subprocess will be terminated when its creator receives a catchable fatal
157 signal or exits normally. Registration ends when wait_subprocess()
158 notices that the subprocess has exited. */
160 register_slave_subprocess (pid_t child)
162 static bool cleanup_slaves_registered = false;
163 if (!cleanup_slaves_registered)
165 atexit (cleanup_slaves);
166 at_fatal_signal (cleanup_slaves);
167 cleanup_slaves_registered = true;
170 /* Try to store the new slave in an unused entry of the slaves array. */
172 slaves_entry_t *s = slaves;
173 slaves_entry_t *s_end = s + slaves_count;
175 for (; s < s_end; s++)
178 /* The two uses of 'volatile' in the slaves_entry_t type above
179 (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
180 entry as used only after the child pid has been written to the
181 memory location s->child. */
188 if (slaves_count == slaves_allocated)
190 /* Extend the slaves array. Note that we cannot use xrealloc(),
191 because then the cleanup_slaves() function could access an already
192 deallocated array. */
193 slaves_entry_t *old_slaves = slaves;
194 size_t new_slaves_allocated = 2 * slaves_allocated;
195 slaves_entry_t *new_slaves =
196 malloc (new_slaves_allocated * sizeof (slaves_entry_t));
197 if (new_slaves == NULL)
199 /* xalloc_die() will call exit() which will invoke cleanup_slaves().
200 Additionally we need to kill child, because it's not yet among
202 kill (child, TERMINATOR);
205 memcpy (new_slaves, old_slaves,
206 slaves_allocated * sizeof (slaves_entry_t));
208 slaves_allocated = new_slaves_allocated;
209 /* Now we can free the old slaves array. */
210 if (old_slaves != static_slaves)
213 /* The three uses of 'volatile' in the types above (and ISO C 99 section
214 5.1.2.3.(5)) ensure that we increment the slaves_count only after the
215 new slave and its 'used' bit have been written to the memory locations
216 that make up slaves[slaves_count]. */
217 slaves[slaves_count].child = child;
218 slaves[slaves_count].used = 1;
222 /* Unregister a child from the list of slave subprocesses. */
224 unregister_slave_subprocess (pid_t child)
226 /* The easiest way to remove an entry from a list that can be used by
227 an asynchronous signal handler is just to mark it as unused. For this,
228 we rely on sig_atomic_t. */
229 slaves_entry_t *s = slaves;
230 slaves_entry_t *s_end = s + slaves_count;
232 for (; s < s_end; s++)
233 if (s->used && s->child == child)
238 /* Wait for a subprocess to finish. Return its exit code.
239 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
242 wait_subprocess (pid_t child, const char *progname,
244 bool slave_process, bool exit_on_error)
246 /* waitpid() is just as portable as wait() nowadays. */
249 *(int *) &status = 0;
252 int result = waitpid (child, &status, 0);
260 #if 0 /* defined ECHILD */
263 /* Child process nonexistent?! Assume it terminated
265 *(int *) &status = 0;
269 if (exit_on_error || !null_stderr)
270 error (exit_on_error ? EXIT_FAILURE : 0, errno,
271 _("%s subprocess"), progname);
275 /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
276 must always be true. Loop until the program terminates. */
277 if (!WIFSTOPPED (status))
281 /* The child process has exited or was signalled. */
284 /* Unregister the child from the list of slave subprocesses, so that
285 later, when we exit, we don't kill a totally unrelated process which
286 may have acquired the same pid. */
287 unregister_slave_subprocess (child);
289 if (WIFSIGNALED (status))
291 if (exit_on_error || !null_stderr)
292 error (exit_on_error ? EXIT_FAILURE : 0, 0,
293 _("%s subprocess got fatal signal %d"),
294 progname, (int) WTERMSIG (status));
297 if (WEXITSTATUS (status) == 127)
299 if (exit_on_error || !null_stderr)
300 error (exit_on_error ? EXIT_FAILURE : 0, 0,
301 _("%s subprocess failed"), progname);
304 return WEXITSTATUS (status);