1 /* Waiting for a subprocess to finish.
2 Copyright (C) 2001-2003, 2005-2025 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 3 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
22 #include "wait-process.h"
29 #include <sys/types.h>
33 #include "fatal-signal.h"
37 #define _(msgid) dgettext ("gnulib", msgid)
39 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
42 #if defined _WIN32 && ! defined __CYGWIN__
44 # define WIN32_LEAN_AND_MEAN
47 /* The return value of _spawnvp() is really a process handle as returned
48 by CreateProcess(). Therefore we can kill it using TerminateProcess. */
49 # define kill(pid,sig) TerminateProcess ((HANDLE) (pid), sig)
58 /* Replacement of waitpid() to support spawn2() of LIBCx which is the kLIBC
59 extension library. See for details:
60 <https://github.com/bitwiseworks/libcx/blob/master/src/spawn/libcx/spawn2.h#L194>.
63 klibc_waitpid (pid_t pid
, int *statusp
, int options
)
65 static pid_t (*waitpid_pfn
) (pid_t
, int *, int) = NULL
;
67 if (waitpid_pfn
== NULL
)
71 /* Try to use waitpid() of LIBCx first if available because it can
72 process the return value of spawn-family of kLIBC as well as spawn2()
74 libcx_handle
= dlopen ("libcx0", RTLD_LAZY
);
75 if (libcx_handle
!= NULL
)
76 waitpid_pfn
= dlsym (libcx_handle
, "_waitpid");
77 /* If not available, falls back to waitpid() of kLIBC. */
78 if (waitpid_pfn
== NULL
)
79 waitpid_pfn
= waitpid
;
82 return waitpid_pfn (pid
, statusp
, options
);
85 # define waitpid klibc_waitpid
89 /* Type of an entry in the slaves array.
90 The 'used' bit determines whether this entry is currently in use.
91 (If pid_t was an atomic type like sig_atomic_t, we could just set the
92 'child' field to 0 when unregistering a slave process, and wouldn't need
94 The 'used' and 'child' fields are accessed from within the cleanup_slaves()
95 action, therefore we mark them as 'volatile'. */
98 volatile sig_atomic_t used
;
103 /* The registered slave subprocesses. */
104 static slaves_entry_t static_slaves
[32];
105 static slaves_entry_t
* volatile slaves
= static_slaves
;
106 static sig_atomic_t volatile slaves_count
= 0;
107 static size_t slaves_allocated
= SIZEOF (static_slaves
);
109 /* The termination signal for slave subprocesses.
110 2003-10-07: Terminator becomes Governator. */
112 # define TERMINATOR SIGHUP
114 # define TERMINATOR SIGTERM
117 /* The cleanup action. It gets called asynchronously. */
118 static _GL_ASYNC_SAFE
void
119 cleanup_slaves (void)
123 /* Get the last registered slave. */
124 size_t n
= slaves_count
;
129 /* Skip unused entries in the slaves array. */
132 pid_t slave
= slaves
[n
].child
;
134 /* Kill the slave. */
135 kill (slave
, TERMINATOR
);
140 /* The cleanup action, taking a signal argument.
141 It gets called asynchronously. */
142 static _GL_ASYNC_SAFE
void
143 cleanup_slaves_action (_GL_UNUSED
int sig
)
148 /* Register a subprocess as being a slave process. This means that the
149 subprocess will be terminated when its creator receives a catchable fatal
150 signal or exits normally. Registration ends when wait_subprocess()
151 notices that the subprocess has exited. */
153 register_slave_subprocess (pid_t child
)
155 static bool cleanup_slaves_registered
= false;
156 if (!cleanup_slaves_registered
)
158 atexit (cleanup_slaves
);
159 if (at_fatal_signal (cleanup_slaves_action
) < 0)
161 cleanup_slaves_registered
= true;
164 /* Try to store the new slave in an unused entry of the slaves array. */
166 slaves_entry_t
*s
= slaves
;
167 slaves_entry_t
*s_end
= s
+ slaves_count
;
169 for (; s
< s_end
; s
++)
172 /* The two uses of 'volatile' in the slaves_entry_t type above
173 (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
174 entry as used only after the child pid has been written to the
175 memory location s->child. */
182 if (slaves_count
== slaves_allocated
)
184 /* Extend the slaves array. Note that we cannot use xrealloc(),
185 because then the cleanup_slaves() function could access an already
186 deallocated array. */
187 slaves_entry_t
*old_slaves
= slaves
;
188 size_t new_slaves_allocated
= 2 * slaves_allocated
;
189 slaves_entry_t
*new_slaves
=
191 malloc (new_slaves_allocated
* sizeof (slaves_entry_t
));
192 if (new_slaves
== NULL
)
194 /* xalloc_die() will call exit() which will invoke cleanup_slaves().
195 Additionally we need to kill child, because it's not yet among
197 kill (child
, TERMINATOR
);
200 memcpy (new_slaves
, old_slaves
,
201 slaves_allocated
* sizeof (slaves_entry_t
));
203 slaves_allocated
= new_slaves_allocated
;
204 /* Now we can free the old slaves array. */
205 if (old_slaves
!= static_slaves
)
208 /* The three uses of 'volatile' in the types above (and ISO C 99 section
209 5.1.2.3.(5)) ensure that we increment the slaves_count only after the
210 new slave and its 'used' bit have been written to the memory locations
211 that make up slaves[slaves_count]. */
212 slaves
[slaves_count
].child
= child
;
213 slaves
[slaves_count
].used
= 1;
217 /* Unregister a child from the list of slave subprocesses. */
219 unregister_slave_subprocess (pid_t child
)
221 /* The easiest way to remove an entry from a list that can be used by
222 an asynchronous signal handler is just to mark it as unused. For this,
223 we rely on sig_atomic_t. */
224 slaves_entry_t
*s
= slaves
;
225 slaves_entry_t
*s_end
= s
+ slaves_count
;
227 for (; s
< s_end
; s
++)
228 if (s
->used
&& s
->child
== child
)
233 /* Wait for a subprocess to finish. Return its exit code.
234 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
237 wait_subprocess (pid_t child
, const char *progname
,
238 bool ignore_sigpipe
, bool null_stderr
,
239 bool slave_process
, bool exit_on_error
,
242 #if HAVE_WAITID && defined WNOWAIT && 0
243 /* Commented out because waitid() without WEXITED and with WNOWAIT doesn't
244 work: On Solaris 7 and OSF/1 4.0, it returns -1 and sets errno = ECHILD,
245 and on HP-UX 10.20 it just hangs. */
246 /* Use of waitid() with WNOWAIT avoids a race condition: If slave_process is
247 true, and this process sleeps a very long time between the return from
248 waitpid() and the execution of unregister_slave_subprocess(), and
249 meanwhile another process acquires the same PID as child, and then - still
250 before unregister_slave_subprocess() - this process gets a fatal signal,
251 it would kill the other totally unrelated process. */
254 if (termsigp
!= NULL
)
258 if (waitid (P_PID
, child
, &info
, WEXITED
| (slave_process
? WNOWAIT
: 0))
265 if (exit_on_error
|| !null_stderr
)
266 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
267 _("%s subprocess"), progname
);
271 /* info.si_code is set to one of CLD_EXITED, CLD_KILLED, CLD_DUMPED,
272 CLD_TRAPPED, CLD_STOPPED, CLD_CONTINUED. Loop until the program
274 if (info
.si_code
== CLD_EXITED
275 || info
.si_code
== CLD_KILLED
|| info
.si_code
== CLD_DUMPED
)
279 /* The child process has exited or was signalled. */
283 /* Unregister the child from the list of slave subprocesses, so that
284 later, when we exit, we don't kill a totally unrelated process which
285 may have acquired the same pid. */
286 unregister_slave_subprocess (child
);
288 /* Now remove the zombie from the process list. */
291 if (waitid (P_PID
, child
, &info
, WEXITED
) < 0)
297 if (exit_on_error
|| !null_stderr
)
298 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
299 _("%s subprocess"), progname
);
306 switch (info
.si_code
)
310 if (termsigp
!= NULL
)
311 *termsigp
= info
.si_status
; /* TODO: or info.si_signo? */
313 if (info
.si_status
== SIGPIPE
&& ignore_sigpipe
)
316 if (exit_on_error
|| (!null_stderr
&& termsigp
== NULL
))
317 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
318 _("%s subprocess got fatal signal %d"),
319 progname
, info
.si_status
);
322 if (info
.si_status
== 127)
324 if (exit_on_error
|| !null_stderr
)
325 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
326 _("%s subprocess failed"), progname
);
329 return info
.si_status
;
334 /* waitpid() is just as portable as wait() nowadays. */
337 if (termsigp
!= NULL
)
342 int result
= waitpid (child
, &status
, 0);
350 # if 0 /* defined ECHILD */
353 /* Child process nonexistent?! Assume it terminated
359 if (exit_on_error
|| !null_stderr
)
360 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
361 _("%s subprocess"), progname
);
365 /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
366 must always be true, since we did not specify WCONTINUED in the
367 waitpid() call. Loop until the program terminates. */
368 if (!WIFSTOPPED (status
))
372 /* The child process has exited or was signalled. */
375 /* Unregister the child from the list of slave subprocesses, so that
376 later, when we exit, we don't kill a totally unrelated process which
377 may have acquired the same pid. */
378 unregister_slave_subprocess (child
);
380 if (WIFSIGNALED (status
))
382 if (termsigp
!= NULL
)
383 *termsigp
= WTERMSIG (status
);
385 if (WTERMSIG (status
) == SIGPIPE
&& ignore_sigpipe
)
388 if (exit_on_error
|| (!null_stderr
&& termsigp
== NULL
))
389 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
390 _("%s subprocess got fatal signal %d"),
391 progname
, (int) WTERMSIG (status
));
394 if (!WIFEXITED (status
))
396 if (WEXITSTATUS (status
) == 127)
398 if (exit_on_error
|| !null_stderr
)
399 error (exit_on_error
? EXIT_FAILURE
: 0, 0,
400 _("%s subprocess failed"), progname
);
403 return WEXITSTATUS (status
);