Debian Codegraph @

lib/wait-process.c

raw
1 /* Waiting for a subprocess to finish.
2 Copyright (C) 2001-2003, 2005-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 2001.
4
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.
9
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.
14
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/>. */
17
18
19 #include <config.h>
20
21 /* Specification. */
22 #include "wait-process.h"
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <signal.h>
28
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 #include <error.h>
33 #include "fatal-signal.h"
34 #include "xalloc.h"
35 #include "gettext.h"
36
37 #define _(str) gettext (str)
38
39 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
40
41
42 #if defined _WIN32 && ! defined __CYGWIN__
43
44 # define WIN32_LEAN_AND_MEAN
45 # include <windows.h>
46
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)
50
51 #endif
52
53 #ifdef __KLIBC__
54 # include <dlfcn.h>
55
56 # undef waitpid
57
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>.
61 */
62 static pid_t
63 klibc_waitpid (pid_t pid, int *statusp, int options)
64 {
65 static pid_t (*waitpid_pfn) (pid_t, int *, int) = NULL;
66
67 if (waitpid_pfn == NULL)
68 {
69 void *libcx_handle;
70
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()
73 of LIBCx. */
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;
80 }
81
82 return waitpid_pfn (pid, statusp, options);
83 }
84
85 # define waitpid klibc_waitpid
86 #endif
87
88
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
93 the 'used' field.)
94 The 'used' and 'child' fields are accessed from within the cleanup_slaves()
95 action, therefore we mark them as 'volatile'. */
96 typedef struct
97 {
98 volatile sig_atomic_t used;
99 volatile pid_t child;
100 }
101 slaves_entry_t;
102
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);
108
109 /* The termination signal for slave subprocesses.
110 2003-10-07: Terminator becomes Governator. */
111 #ifdef SIGHUP
112 # define TERMINATOR SIGHUP
113 #else
114 # define TERMINATOR SIGTERM
115 #endif
116
117 /* The cleanup action. It gets called asynchronously. */
118 static _GL_ASYNC_SAFE void
119 cleanup_slaves (void)
120 {
121 for (;;)
122 {
123 /* Get the last registered slave. */
124 size_t n = slaves_count;
125 if (n == 0)
126 break;
127 n--;
128 slaves_count = n;
129 /* Skip unused entries in the slaves array. */
130 if (slaves[n].used)
131 {
132 pid_t slave = slaves[n].child;
133
134 /* Kill the slave. */
135 kill (slave, TERMINATOR);
136 }
137 }
138 }
139
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)
144 {
145 cleanup_slaves ();
146 }
147
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. */
152 void
153 register_slave_subprocess (pid_t child)
154 {
155 static bool cleanup_slaves_registered = false;
156 if (!cleanup_slaves_registered)
157 {
158 atexit (cleanup_slaves);
159 if (at_fatal_signal (cleanup_slaves_action) < 0)
160 xalloc_die ();
161 cleanup_slaves_registered = true;
162 }
163
164 /* Try to store the new slave in an unused entry of the slaves array. */
165 {
166 slaves_entry_t *s = slaves;
167 slaves_entry_t *s_end = s + slaves_count;
168
169 for (; s < s_end; s++)
170 if (!s->used)
171 {
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. */
176 s->child = child;
177 s->used = 1;
178 return;
179 }
180 }
181
182 if (slaves_count == slaves_allocated)
183 {
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 =
190 (slaves_entry_t *)
191 malloc (new_slaves_allocated * sizeof (slaves_entry_t));
192 if (new_slaves == NULL)
193 {
194 /* xalloc_die() will call exit() which will invoke cleanup_slaves().
195 Additionally we need to kill child, because it's not yet among
196 the slaves list. */
197 kill (child, TERMINATOR);
198 xalloc_die ();
199 }
200 memcpy (new_slaves, old_slaves,
201 slaves_allocated * sizeof (slaves_entry_t));
202 slaves = new_slaves;
203 slaves_allocated = new_slaves_allocated;
204 /* Now we can free the old slaves array. */
205 if (old_slaves != static_slaves)
206 free (old_slaves);
207 }
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;
214 slaves_count++;
215 }
216
217 /* Unregister a child from the list of slave subprocesses. */
218 static void
219 unregister_slave_subprocess (pid_t child)
220 {
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;
226
227 for (; s < s_end; s++)
228 if (s->used && s->child == child)
229 s->used = 0;
230 }
231
232
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
235 return 127. */
236 int
237 wait_subprocess (pid_t child, const char *progname,
238 bool ignore_sigpipe, bool null_stderr,
239 bool slave_process, bool exit_on_error,
240 int *termsigp)
241 {
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. */
252 siginfo_t info;
253
254 if (termsigp != NULL)
255 *termsigp = 0;
256 for (;;)
257 {
258 if (waitid (P_PID, child, &info, WEXITED | (slave_process ? WNOWAIT : 0))
259 < 0)
260 {
261 # ifdef EINTR
262 if (errno == EINTR)
263 continue;
264 # endif
265 if (exit_on_error || !null_stderr)
266 error (exit_on_error ? EXIT_FAILURE : 0, errno,
267 _("%s subprocess"), progname);
268 return 127;
269 }
270
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
273 terminates. */
274 if (info.si_code == CLD_EXITED
275 || info.si_code == CLD_KILLED || info.si_code == CLD_DUMPED)
276 break;
277 }
278
279 /* The child process has exited or was signalled. */
280
281 if (slave_process)
282 {
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);
287
288 /* Now remove the zombie from the process list. */
289 for (;;)
290 {
291 if (waitid (P_PID, child, &info, WEXITED) < 0)
292 {
293 # ifdef EINTR
294 if (errno == EINTR)
295 continue;
296 # endif
297 if (exit_on_error || !null_stderr)
298 error (exit_on_error ? EXIT_FAILURE : 0, errno,
299 _("%s subprocess"), progname);
300 return 127;
301 }
302 break;
303 }
304 }
305
306 switch (info.si_code)
307 {
308 case CLD_KILLED:
309 case CLD_DUMPED:
310 if (termsigp != NULL)
311 *termsigp = info.si_status; /* TODO: or info.si_signo? */
312 # ifdef SIGPIPE
313 if (info.si_status == SIGPIPE && ignore_sigpipe)
314 return 0;
315 # endif
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);
320 return 127;
321 case CLD_EXITED:
322 if (info.si_status == 127)
323 {
324 if (exit_on_error || !null_stderr)
325 error (exit_on_error ? EXIT_FAILURE : 0, 0,
326 _("%s subprocess failed"), progname);
327 return 127;
328 }
329 return info.si_status;
330 default:
331 abort ();
332 }
333 #else
334 /* waitpid() is just as portable as wait() nowadays. */
335 int status;
336
337 if (termsigp != NULL)
338 *termsigp = 0;
339 status = 0;
340 for (;;)
341 {
342 int result = waitpid (child, &status, 0);
343
344 if (result != child)
345 {
346 # ifdef EINTR
347 if (errno == EINTR)
348 continue;
349 # endif
350 # if 0 /* defined ECHILD */
351 if (errno == ECHILD)
352 {
353 /* Child process nonexistent?! Assume it terminated
354 successfully. */
355 status = 0;
356 break;
357 }
358 # endif
359 if (exit_on_error || !null_stderr)
360 error (exit_on_error ? EXIT_FAILURE : 0, errno,
361 _("%s subprocess"), progname);
362 return 127;
363 }
364
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))
369 break;
370 }
371
372 /* The child process has exited or was signalled. */
373
374 if (slave_process)
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);
379
380 if (WIFSIGNALED (status))
381 {
382 if (termsigp != NULL)
383 *termsigp = WTERMSIG (status);
384 # ifdef SIGPIPE
385 if (WTERMSIG (status) == SIGPIPE && ignore_sigpipe)
386 return 0;
387 # endif
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));
392 return 127;
393 }
394 if (!WIFEXITED (status))
395 abort ();
396 if (WEXITSTATUS (status) == 127)
397 {
398 if (exit_on_error || !null_stderr)
399 error (exit_on_error ? EXIT_FAILURE : 0, 0,
400 _("%s subprocess failed"), progname);
401 return 127;
402 }
403 return WEXITSTATUS (status);
404 #endif
405 }