annotate lib/wait-process.c @ 10556:d6619672e1a6

Make use of the modules 'thread', 'yield' in the 'tls' test.
author Bruno Haible <bruno@clisp.org>
date Wed, 01 Oct 2008 02:49:47 +0200
parents d079dd7b69bc
children fbb2ccfb180d
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.
10194
8297e6235e2d Add an assertion.
Bruno Haible <bruno@clisp.org>
parents: 10193
diff changeset
2 Copyright (C) 2001-2003, 2005-2008 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
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
16 along with this program. If not, see <http://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
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #if defined _MSC_VER || defined __MINGW32__
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 /* Native Woe32 API. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #include <process.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #define waitpid(pid,statusp,options) _cwait (statusp, pid, WAIT_CHILD)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #define WAIT_T int
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #define WTERMSIG(x) ((x) & 0xff) /* or: SIGABRT ?? */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #define WCOREDUMP(x) 0
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #define WEXITSTATUS(x) (((x) >> 8) & 0xff) /* or: (x) ?? */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #define WIFSIGNALED(x) (WTERMSIG (x) != 0) /* or: ((x) == 3) ?? */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #define WIFEXITED(x) (WTERMSIG (x) == 0) /* or: ((x) != 3) ?? */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #define WIFSTOPPED(x) 0
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #else
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 /* Unix API. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #include <sys/wait.h>
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /* On Linux, WEXITSTATUS are bits 15..8 and WTERMSIG are bits 7..0, while
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 BeOS uses the contrary. Therefore we use the abstract macros. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #if HAVE_UNION_WAIT
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 # define WAIT_T union wait
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 # ifndef WTERMSIG
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 # define WTERMSIG(x) ((x).w_termsig)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 # endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 # ifndef WCOREDUMP
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 # define WCOREDUMP(x) ((x).w_coredump)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 # endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 # ifndef WEXITSTATUS
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 # define WEXITSTATUS(x) ((x).w_retcode)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 # endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 #else
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 # define WAIT_T int
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 # ifndef WTERMSIG
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 # define WTERMSIG(x) ((x) & 0x7f)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 # endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 # ifndef WCOREDUMP
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 # define WCOREDUMP(x) ((x) & 0x80)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 # endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 # ifndef WEXITSTATUS
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 # endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 /* For valid x, exactly one of WIFSIGNALED(x), WIFEXITED(x), WIFSTOPPED(x)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 is true. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 #ifndef WIFSIGNALED
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 # define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 #endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 #ifndef WIFEXITED
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 # define WIFEXITED(x) (WTERMSIG (x) == 0)
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 #ifndef WIFSTOPPED
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 # define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 #endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 /* Note that portable applications may access
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 WTERMSIG(x) only if WIFSIGNALED(x) is true, and
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 WEXITSTATUS(x) only if WIFEXITED(x) is true. */
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 #endif
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 #include "error.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 #include "fatal-signal.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 #include "xalloc.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 #include "gettext.h"
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 #define _(str) gettext (str)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
100 #if defined _MSC_VER || defined __MINGW32__
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
101
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
102 #define WIN32_LEAN_AND_MEAN
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
103 #include <windows.h>
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
104
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
105 /* 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
106 by CreateProcess(). Therefore we can kill it using TerminateProcess. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
107 #define kill(pid,sig) TerminateProcess ((HANDLE) (pid), sig)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
108
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
109 #endif
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
110
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
111
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 /* Type of an entry in the slaves array.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 The 'used' bit determines whether this entry is currently in use.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 (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
115 '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
116 the 'used' field.)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 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
118 action, therefore we mark them as 'volatile'. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 typedef struct
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 volatile sig_atomic_t used;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 volatile pid_t child;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 slaves_entry_t;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 /* The registered slave subprocesses. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 static slaves_entry_t static_slaves[32];
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 static slaves_entry_t * volatile slaves = static_slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 static sig_atomic_t volatile slaves_count = 0;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 static size_t slaves_allocated = SIZEOF (static_slaves);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 /* The termination signal for slave subprocesses.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 2003-10-07: Terminator becomes Governator. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 #ifdef SIGHUP
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 # define TERMINATOR SIGHUP
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 #else
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 # define TERMINATOR SIGTERM
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 #endif
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 /* The cleanup action. It gets called asynchronously. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 static void
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
142 cleanup_slaves (void)
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 for (;;)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 /* Get the last registered slave. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 size_t n = slaves_count;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (n == 0)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 break;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 n--;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 slaves_count = n;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 /* Skip unused entries in the slaves array. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 if (slaves[n].used)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 pid_t slave = slaves[n].child;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 /* Kill the slave. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 kill (slave, TERMINATOR);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 /* 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
164 subprocess will be terminated when its creator receives a catchable fatal
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 signal or exits normally. Registration ends when wait_subprocess()
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 notices that the subprocess has exited. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 void
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 register_slave_subprocess (pid_t child)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 static bool cleanup_slaves_registered = false;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 if (!cleanup_slaves_registered)
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 atexit (cleanup_slaves);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 at_fatal_signal (cleanup_slaves);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 cleanup_slaves_registered = true;
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
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 /* 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
179 {
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)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 /* The two uses of 'volatile' in the slaves_entry_t type above
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 entry as used only after the child pid has been written to the
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 memory location s->child. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 s->child = child;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 s->used = 1;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 return;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 if (slaves_count == slaves_allocated)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 /* Extend the slaves array. Note that we cannot use xrealloc(),
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 because then the cleanup_slaves() function could access an already
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 deallocated array. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 slaves_entry_t *old_slaves = slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 size_t new_slaves_allocated = 2 * slaves_allocated;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 slaves_entry_t *new_slaves =
7586
4a8b5467d8b2 Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
204 (slaves_entry_t *)
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 malloc (new_slaves_allocated * sizeof (slaves_entry_t));
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 if (new_slaves == NULL)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 /* xalloc_die() will call exit() which will invoke cleanup_slaves().
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 Additionally we need to kill child, because it's not yet among
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 the slaves list. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 kill (child, TERMINATOR);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 xalloc_die ();
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 memcpy (new_slaves, old_slaves,
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 slaves_allocated * sizeof (slaves_entry_t));
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 slaves = new_slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 slaves_allocated = new_slaves_allocated;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 /* Now we can free the old slaves array. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 if (old_slaves != static_slaves)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 free (old_slaves);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 /* 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
223 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
224 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
225 that make up slaves[slaves_count]. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 slaves[slaves_count].child = child;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 slaves[slaves_count].used = 1;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 slaves_count++;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 /* Unregister a child from the list of slave subprocesses. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 static inline void
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 unregister_slave_subprocess (pid_t child)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 /* 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
236 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
237 we rely on sig_atomic_t. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 slaves_entry_t *s = slaves;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 slaves_entry_t *s_end = s + slaves_count;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 for (; s < s_end; s++)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 if (s->used && s->child == child)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 s->used = 0;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 /* Wait for a subprocess to finish. Return its exit code.
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 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
249 return 127. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 int
6761
14eb5491c867 * lib/wait-process.c, lib/wait-process.h, lib/csharpcomp.c,
Derek R. Price <derek@ximbiot.com>
parents: 6759
diff changeset
251 wait_subprocess (pid_t child, const char *progname,
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
252 bool ignore_sigpipe, bool null_stderr,
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
253 bool slave_process, bool exit_on_error,
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
254 int *termsigp)
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 {
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
256 #if HAVE_WAITID && defined WNOWAIT && 0
10193
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
257 /* 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
258 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
259 and on HP-UX 10.20 it just hangs. */
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
260 /* 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
261 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
262 waitpid() and the execution of unregister_slave_subprocess(), and
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
263 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
264 before unregister_slave_subprocess() - this process gets a fatal signal,
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
265 it would kill the other totally unrelated process. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
266 siginfo_t info;
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
267
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
268 if (termsigp != NULL)
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
269 *termsigp = 0;
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
270 for (;;)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
271 {
10193
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
272 if (waitid (P_PID, child, &info, WEXITED | (slave_process ? WNOWAIT : 0))
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
273 < 0)
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
274 {
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
275 # ifdef EINTR
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
276 if (errno == EINTR)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
277 continue;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
278 # endif
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
279 if (exit_on_error || !null_stderr)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
280 error (exit_on_error ? EXIT_FAILURE : 0, errno,
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
281 _("%s subprocess"), progname);
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
282 return 127;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
283 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
284
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
285 /* info.si_code is set to one of CLD_EXITED, CLD_KILLED, CLD_DUMPED,
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
286 CLD_TRAPPED, CLD_STOPPED, CLD_CONTINUED. Loop until the program
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
287 terminates. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
288 if (info.si_code == CLD_EXITED
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
289 || info.si_code == CLD_KILLED || info.si_code == CLD_DUMPED)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
290 break;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
291 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
292
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
293 /* The child process has exited or was signalled. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
294
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
295 if (slave_process)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
296 {
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
297 /* Unregister the child from the list of slave subprocesses, so that
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
298 later, when we exit, we don't kill a totally unrelated process which
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
299 may have acquired the same pid. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
300 unregister_slave_subprocess (child);
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
301
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
302 /* Now remove the zombie from the process list. */
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
303 for (;;)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
304 {
10193
b67d0f4f1513 Try to fix waitid() based code.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
305 if (waitid (P_PID, child, &info, WEXITED) < 0)
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
306 {
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
307 # ifdef EINTR
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
308 if (errno == EINTR)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
309 continue;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
310 # endif
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
311 if (exit_on_error || !null_stderr)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
312 error (exit_on_error ? EXIT_FAILURE : 0, errno,
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
313 _("%s subprocess"), progname);
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
314 return 127;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
315 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
316 break;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
317 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
318 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
319
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
320 switch (info.si_code)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
321 {
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
322 case CLD_KILLED:
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
323 case CLD_DUMPED:
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
324 if (termsigp != NULL)
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
325 *termsigp = info.si_status; /* TODO: or info.si_signo? */
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
326 # ifdef SIGPIPE
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
327 if (info.si_status == SIGPIPE && ignore_sigpipe)
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
328 return 0;
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
329 # endif
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
330 if (exit_on_error || !null_stderr)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
331 error (exit_on_error ? EXIT_FAILURE : 0, 0,
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
332 _("%s subprocess got fatal signal %d"),
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
333 progname, info.si_status);
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
334 return 127;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
335 case CLD_EXITED:
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
336 if (info.si_status == 127)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
337 {
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
338 if (exit_on_error || !null_stderr)
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
339 error (exit_on_error ? EXIT_FAILURE : 0, 0,
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
340 _("%s subprocess failed"), progname);
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
341 return 127;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
342 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
343 return info.si_status;
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
344 default:
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
345 abort ();
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
346 }
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
347 #else
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 /* waitpid() is just as portable as wait() nowadays. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 WAIT_T status;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
351 if (termsigp != NULL)
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
352 *termsigp = 0;
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 *(int *) &status = 0;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 for (;;)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 int result = waitpid (child, &status, 0);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 if (result != child)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 {
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
360 # ifdef EINTR
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 if (errno == EINTR)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 continue;
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
363 # endif
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
364 # if 0 /* defined ECHILD */
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 if (errno == ECHILD)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 /* Child process nonexistent?! Assume it terminated
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 successfully. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 *(int *) &status = 0;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 break;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 }
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
372 # endif
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 if (exit_on_error || !null_stderr)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 error (exit_on_error ? EXIT_FAILURE : 0, errno,
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 _("%s subprocess"), progname);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 return 127;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
10194
8297e6235e2d Add an assertion.
Bruno Haible <bruno@clisp.org>
parents: 10193
diff changeset
380 must always be true, since we did not specify WCONTINUED in the
8297e6235e2d Add an assertion.
Bruno Haible <bruno@clisp.org>
parents: 10193
diff changeset
381 waitpid() call. Loop until the program terminates. */
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 if (!WIFSTOPPED (status))
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 break;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386 /* The child process has exited or was signalled. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 if (slave_process)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 /* Unregister the child from the list of slave subprocesses, so that
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 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
391 may have acquired the same pid. */
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 unregister_slave_subprocess (child);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 if (WIFSIGNALED (status))
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 {
10197
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
396 if (termsigp != NULL)
d079dd7b69bc Add termsigp argument to execute() and wait_process().
Bruno Haible <bruno@clisp.org>
parents: 10194
diff changeset
397 *termsigp = WTERMSIG (status);
4927
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
398 # ifdef SIGPIPE
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
399 if (WTERMSIG (status) == SIGPIPE && ignore_sigpipe)
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
400 return 0;
bc5f62830252 Update from gettext.
Bruno Haible <bruno@clisp.org>
parents: 4847
diff changeset
401 # endif
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 if (exit_on_error || !null_stderr)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 error (exit_on_error ? EXIT_FAILURE : 0, 0,
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 _("%s subprocess got fatal signal %d"),
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405 progname, (int) WTERMSIG (status));
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 return 127;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 }
10194
8297e6235e2d Add an assertion.
Bruno Haible <bruno@clisp.org>
parents: 10193
diff changeset
408 if (!WIFEXITED (status))
8297e6235e2d Add an assertion.
Bruno Haible <bruno@clisp.org>
parents: 10193
diff changeset
409 abort ();
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 if (WEXITSTATUS (status) == 127)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411 {
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 if (exit_on_error || !null_stderr)
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 error (exit_on_error ? EXIT_FAILURE : 0, 0,
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 _("%s subprocess failed"), progname);
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 return 127;
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 }
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 return WEXITSTATUS (status);
4847
80786047b412 Use waitid if possible.
Bruno Haible <bruno@clisp.org>
parents: 4802
diff changeset
418 #endif
4802
16d77b789c30 New module 'wait-process'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 }