annotate lib/wait-process.c @ 40186:8964917f9574

autoupdate
author Karl Berry <karl@freefriends.org>
date Mon, 18 Feb 2019 08:02:49 -0800
parents b06060465f09
children 3ac749aa0041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Waiting for a subprocess to finish.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19595
diff changeset
2 Copyright (C) 2001-2003, 2005-2019 Free Software Foundation, Inc.
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
5 This program is free software: you can redistribute it and/or modify
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
8 (at your option) any later version.
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18882
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7028
diff changeset
19 #include <config.h>
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "wait-process.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <errno.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdlib.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <string.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <signal.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <sys/types.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <sys/wait.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include "error.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #include "fatal-signal.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #include "xalloc.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #include "gettext.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #define _(str) gettext (str)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
42 #if defined _WIN32 && ! defined __CYGWIN__
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
43
18882
91cd2080c9ae wait-process: Adjust native Windows support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
44 # define WIN32_LEAN_AND_MEAN
91cd2080c9ae wait-process: Adjust native Windows support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
45 # include <windows.h>
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
46
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
47 /* The return value of spawnvp() is really a process handle as returned
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
48 by CreateProcess(). Therefore we can kill it using TerminateProcess. */
18882
91cd2080c9ae wait-process: Adjust native Windows support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
49 # define kill(pid,sig) TerminateProcess ((HANDLE) (pid), sig)
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
50
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
51 #endif
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
52
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
53
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 /* Type of an entry in the slaves array.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 The 'used' bit determines whether this entry is currently in use.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 (If pid_t was an atomic type like sig_atomic_t, we could just set the
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 'child' field to 0 when unregistering a slave process, and wouldn't need
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 the 'used' field.)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 The 'used' and 'child' fields are accessed from within the cleanup_slaves()
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 action, therefore we mark them as 'volatile'. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 typedef struct
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 volatile sig_atomic_t used;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 volatile pid_t child;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 slaves_entry_t;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 /* The registered slave subprocesses. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 static slaves_entry_t static_slaves[32];
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 static slaves_entry_t * volatile slaves = static_slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 static sig_atomic_t volatile slaves_count = 0;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 static size_t slaves_allocated = SIZEOF (static_slaves);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 /* The termination signal for slave subprocesses.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 2003-10-07: Terminator becomes Governator. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 #ifdef SIGHUP
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 # define TERMINATOR SIGHUP
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 #else
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 # define TERMINATOR SIGTERM
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 #endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 /* The cleanup action. It gets called asynchronously. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 static void
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
84 cleanup_slaves (void)
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 for (;;)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 /* Get the last registered slave. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 size_t n = slaves_count;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 if (n == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
91 break;
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 n--;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 slaves_count = n;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 /* Skip unused entries in the slaves array. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 if (slaves[n].used)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
96 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
97 pid_t slave = slaves[n].child;
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
99 /* Kill the slave. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
100 kill (slave, TERMINATOR);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
101 }
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 /* Register a subprocess as being a slave process. This means that the
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 subprocess will be terminated when its creator receives a catchable fatal
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 signal or exits normally. Registration ends when wait_subprocess()
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 notices that the subprocess has exited. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 void
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 register_slave_subprocess (pid_t child)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 static bool cleanup_slaves_registered = false;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 if (!cleanup_slaves_registered)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 atexit (cleanup_slaves);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 at_fatal_signal (cleanup_slaves);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 cleanup_slaves_registered = true;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 /* Try to store the new slave in an unused entry of the slaves array. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 slaves_entry_t *s = slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 slaves_entry_t *s_end = s + slaves_count;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 for (; s < s_end; s++)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 if (!s->used)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
127 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
128 /* The two uses of 'volatile' in the slaves_entry_t type above
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
129 (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
130 entry as used only after the child pid has been written to the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
131 memory location s->child. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
132 s->child = child;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
133 s->used = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
134 return;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
135 }
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 if (slaves_count == slaves_allocated)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 /* Extend the slaves array. Note that we cannot use xrealloc(),
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
141 because then the cleanup_slaves() function could access an already
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
142 deallocated array. */
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 slaves_entry_t *old_slaves = slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 size_t new_slaves_allocated = 2 * slaves_allocated;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 slaves_entry_t *new_slaves =
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
146 (slaves_entry_t *)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
147 malloc (new_slaves_allocated * sizeof (slaves_entry_t));
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (new_slaves == NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
149 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
150 /* xalloc_die() will call exit() which will invoke cleanup_slaves().
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
151 Additionally we need to kill child, because it's not yet among
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
152 the slaves list. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
153 kill (child, TERMINATOR);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
154 xalloc_die ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
155 }
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 memcpy (new_slaves, old_slaves,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
157 slaves_allocated * sizeof (slaves_entry_t));
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 slaves = new_slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 slaves_allocated = new_slaves_allocated;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 /* Now we can free the old slaves array. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 if (old_slaves != static_slaves)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
162 free (old_slaves);
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 /* The three uses of 'volatile' in the types above (and ISO C 99 section
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 5.1.2.3.(5)) ensure that we increment the slaves_count only after the
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 new slave and its 'used' bit have been written to the memory locations
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 that make up slaves[slaves_count]. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 slaves[slaves_count].child = child;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 slaves[slaves_count].used = 1;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 slaves_count++;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 /* Unregister a child from the list of slave subprocesses. */
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
174 static void
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 unregister_slave_subprocess (pid_t child)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 /* The easiest way to remove an entry from a list that can be used by
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 an asynchronous signal handler is just to mark it as unused. For this,
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 we rely on sig_atomic_t. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 slaves_entry_t *s = slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 slaves_entry_t *s_end = s + slaves_count;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 for (; s < s_end; s++)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 if (s->used && s->child == child)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 s->used = 0;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 /* Wait for a subprocess to finish. Return its exit code.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 return 127. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 int
6761
14eb5491c867 * lib/wait-process.c, lib/wait-process.h, lib/csharpcomp.c,
Derek R. Price <derek@ximbiot.com>
parents: 6759
diff changeset
193 wait_subprocess (pid_t child, const char *progname,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
194 bool ignore_sigpipe, bool null_stderr,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
195 bool slave_process, bool exit_on_error,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
196 int *termsigp)
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 {
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
198 #if HAVE_WAITID && defined WNOWAIT && 0
10193
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
199 /* Commented out because waitid() without WEXITED and with WNOWAIT doesn't
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
200 work: On Solaris 7 and OSF/1 4.0, it returns -1 and sets errno = ECHILD,
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
201 and on HP-UX 10.20 it just hangs. */
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
202 /* Use of waitid() with WNOWAIT avoids a race condition: If slave_process is
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
203 true, and this process sleeps a very long time between the return from
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
204 waitpid() and the execution of unregister_slave_subprocess(), and
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
205 meanwhile another process acquires the same PID as child, and then - still
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
206 before unregister_slave_subprocess() - this process gets a fatal signal,
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
207 it would kill the other totally unrelated process. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
208 siginfo_t info;
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
209
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
210 if (termsigp != NULL)
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
211 *termsigp = 0;
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
212 for (;;)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
213 {
10193
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
214 if (waitid (P_PID, child, &info, WEXITED | (slave_process ? WNOWAIT : 0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
215 < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
216 {
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
217 # ifdef EINTR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
218 if (errno == EINTR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
219 continue;
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
220 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
221 if (exit_on_error || !null_stderr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
222 error (exit_on_error ? EXIT_FAILURE : 0, errno,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
223 _("%s subprocess"), progname);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
224 return 127;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
225 }
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
226
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
227 /* info.si_code is set to one of CLD_EXITED, CLD_KILLED, CLD_DUMPED,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
228 CLD_TRAPPED, CLD_STOPPED, CLD_CONTINUED. Loop until the program
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
229 terminates. */
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
230 if (info.si_code == CLD_EXITED
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
231 || info.si_code == CLD_KILLED || info.si_code == CLD_DUMPED)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
232 break;
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
233 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
234
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
235 /* The child process has exited or was signalled. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
236
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
237 if (slave_process)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
238 {
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
239 /* Unregister the child from the list of slave subprocesses, so that
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
240 later, when we exit, we don't kill a totally unrelated process which
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
241 may have acquired the same pid. */
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
242 unregister_slave_subprocess (child);
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
243
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
244 /* Now remove the zombie from the process list. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
245 for (;;)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
246 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
247 if (waitid (P_PID, child, &info, WEXITED) < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
248 {
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
249 # ifdef EINTR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
250 if (errno == EINTR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
251 continue;
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
252 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
253 if (exit_on_error || !null_stderr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
254 error (exit_on_error ? EXIT_FAILURE : 0, errno,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
255 _("%s subprocess"), progname);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
256 return 127;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
257 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
258 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
259 }
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
260 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
261
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
262 switch (info.si_code)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
263 {
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
264 case CLD_KILLED:
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
265 case CLD_DUMPED:
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
266 if (termsigp != NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
267 *termsigp = info.si_status; /* TODO: or info.si_signo? */
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
268 # ifdef SIGPIPE
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
269 if (info.si_status == SIGPIPE && ignore_sigpipe)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
270 return 0;
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
271 # endif
11248
04d6b677b5e4 Omit an error message that the caller can do better.
Bruno Haible <bruno@clisp.org>
parents: 11247
diff changeset
272 if (exit_on_error || (!null_stderr && termsigp == NULL))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
273 error (exit_on_error ? EXIT_FAILURE : 0, 0,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
274 _("%s subprocess got fatal signal %d"),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
275 progname, info.si_status);
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
276 return 127;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
277 case CLD_EXITED:
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
278 if (info.si_status == 127)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
279 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
280 if (exit_on_error || !null_stderr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
281 error (exit_on_error ? EXIT_FAILURE : 0, 0,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
282 _("%s subprocess failed"), progname);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
283 return 127;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
284 }
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
285 return info.si_status;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
286 default:
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
287 abort ();
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
288 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
289 #else
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 /* waitpid() is just as portable as wait() nowadays. */
10674
fbb2ccfb180d Assume that waitpid() fills an 'int' status, not a 'union wait'.
Bruno Haible <bruno@clisp.org>
parents: 10197
diff changeset
291 int status;
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
293 if (termsigp != NULL)
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
294 *termsigp = 0;
11247
baa92ee42663 Remove unnecessary cast.
Eric Blake <ebb9@byu.net>
parents: 10677
diff changeset
295 status = 0;
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 for (;;)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 int result = waitpid (child, &status, 0);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 if (result != child)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
301 {
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
302 # ifdef EINTR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
303 if (errno == EINTR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
304 continue;
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
305 # endif
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
306 # if 0 /* defined ECHILD */
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
307 if (errno == ECHILD)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
308 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
309 /* Child process nonexistent?! Assume it terminated
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
310 successfully. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
311 status = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
312 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
313 }
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
314 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
315 if (exit_on_error || !null_stderr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
316 error (exit_on_error ? EXIT_FAILURE : 0, errno,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
317 _("%s subprocess"), progname);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
318 return 127;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
319 }
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
322 must always be true, since we did not specify WCONTINUED in the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
323 waitpid() call. Loop until the program terminates. */
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 if (!WIFSTOPPED (status))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
325 break;
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 /* The child process has exited or was signalled. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 if (slave_process)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 /* Unregister the child from the list of slave subprocesses, so that
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 later, when we exit, we don't kill a totally unrelated process which
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 may have acquired the same pid. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 unregister_slave_subprocess (child);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 if (WIFSIGNALED (status))
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 {
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
338 if (termsigp != NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
339 *termsigp = WTERMSIG (status);
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
340 # ifdef SIGPIPE
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
341 if (WTERMSIG (status) == SIGPIPE && ignore_sigpipe)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
342 return 0;
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
343 # endif
11248
04d6b677b5e4 Omit an error message that the caller can do better.
Bruno Haible <bruno@clisp.org>
parents: 11247
diff changeset
344 if (exit_on_error || (!null_stderr && termsigp == NULL))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
345 error (exit_on_error ? EXIT_FAILURE : 0, 0,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
346 _("%s subprocess got fatal signal %d"),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
347 progname, (int) WTERMSIG (status));
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 return 127;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 }
10194
8297e6235e2d Add an assertion.
Bruno Haible <bruno@clisp.org>
parents: 10193
diff changeset
350 if (!WIFEXITED (status))
8297e6235e2d Add an assertion.
Bruno Haible <bruno@clisp.org>
parents: 10193
diff changeset
351 abort ();
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 if (WEXITSTATUS (status) == 127)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 if (exit_on_error || !null_stderr)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
355 error (exit_on_error ? EXIT_FAILURE : 0, 0,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11248
diff changeset
356 _("%s subprocess failed"), progname);
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 return 127;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 return WEXITSTATUS (status);
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
360 #endif
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 }