Add termsigp argument to execute() and wait_process().
[gnulib.git] / lib / wait-process.c
1 /* Waiting for a subprocess to finish.
2    Copyright (C) 2001-2003, 2005-2008 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "wait-process.h"
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <signal.h>
28
29 #include <sys/types.h>
30
31 #if defined _MSC_VER || defined __MINGW32__
32
33 /* Native Woe32 API.  */
34 #include <process.h>
35 #define waitpid(pid,statusp,options) _cwait (statusp, pid, WAIT_CHILD)
36 #define WAIT_T int
37 #define WTERMSIG(x) ((x) & 0xff) /* or: SIGABRT ?? */
38 #define WCOREDUMP(x) 0
39 #define WEXITSTATUS(x) (((x) >> 8) & 0xff) /* or: (x) ?? */
40 #define WIFSIGNALED(x) (WTERMSIG (x) != 0) /* or: ((x) == 3) ?? */
41 #define WIFEXITED(x) (WTERMSIG (x) == 0) /* or: ((x) != 3) ?? */
42 #define WIFSTOPPED(x) 0
43
44 #else
45
46 /* Unix API.  */
47 #include <sys/wait.h>
48 /* On Linux, WEXITSTATUS are bits 15..8 and WTERMSIG are bits 7..0, while
49    BeOS uses the contrary.  Therefore we use the abstract macros.  */
50 #if HAVE_UNION_WAIT
51 # define WAIT_T union wait
52 # ifndef WTERMSIG
53 #  define WTERMSIG(x) ((x).w_termsig)
54 # endif
55 # ifndef WCOREDUMP
56 #  define WCOREDUMP(x) ((x).w_coredump)
57 # endif
58 # ifndef WEXITSTATUS
59 #  define WEXITSTATUS(x) ((x).w_retcode)
60 # endif
61 #else
62 # define WAIT_T int
63 # ifndef WTERMSIG
64 #  define WTERMSIG(x) ((x) & 0x7f)
65 # endif
66 # ifndef WCOREDUMP
67 #  define WCOREDUMP(x) ((x) & 0x80)
68 # endif
69 # ifndef WEXITSTATUS
70 #  define WEXITSTATUS(x) (((x) >> 8) & 0xff)
71 # endif
72 #endif
73 /* For valid x, exactly one of WIFSIGNALED(x), WIFEXITED(x), WIFSTOPPED(x)
74    is true.  */
75 #ifndef WIFSIGNALED
76 # define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
77 #endif
78 #ifndef WIFEXITED
79 # define WIFEXITED(x) (WTERMSIG (x) == 0)
80 #endif
81 #ifndef WIFSTOPPED
82 # define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
83 #endif
84 /* Note that portable applications may access
85    WTERMSIG(x) only if WIFSIGNALED(x) is true, and
86    WEXITSTATUS(x) only if WIFEXITED(x) is true.  */
87
88 #endif
89
90 #include "error.h"
91 #include "fatal-signal.h"
92 #include "xalloc.h"
93 #include "gettext.h"
94
95 #define _(str) gettext (str)
96
97 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
98
99
100 #if defined _MSC_VER || defined __MINGW32__
101
102 #define WIN32_LEAN_AND_MEAN
103 #include <windows.h>
104
105 /* The return value of spawnvp() is really a process handle as returned
106    by CreateProcess().  Therefore we can kill it using TerminateProcess.  */
107 #define kill(pid,sig) TerminateProcess ((HANDLE) (pid), sig)
108
109 #endif
110
111
112 /* Type of an entry in the slaves array.
113    The 'used' bit determines whether this entry is currently in use.
114    (If pid_t was an atomic type like sig_atomic_t, we could just set the
115    'child' field to 0 when unregistering a slave process, and wouldn't need
116    the 'used' field.)
117    The 'used' and 'child' fields are accessed from within the cleanup_slaves()
118    action, therefore we mark them as 'volatile'.  */
119 typedef struct
120 {
121   volatile sig_atomic_t used;
122   volatile pid_t child;
123 }
124 slaves_entry_t;
125
126 /* The registered slave subprocesses.  */
127 static slaves_entry_t static_slaves[32];
128 static slaves_entry_t * volatile slaves = static_slaves;
129 static sig_atomic_t volatile slaves_count = 0;
130 static size_t slaves_allocated = SIZEOF (static_slaves);
131
132 /* The termination signal for slave subprocesses.
133    2003-10-07:  Terminator becomes Governator.  */
134 #ifdef SIGHUP
135 # define TERMINATOR SIGHUP
136 #else
137 # define TERMINATOR SIGTERM
138 #endif
139
140 /* The cleanup action.  It gets called asynchronously.  */
141 static void
142 cleanup_slaves (void)
143 {
144   for (;;)
145     {
146       /* Get the last registered slave.  */
147       size_t n = slaves_count;
148       if (n == 0)
149         break;
150       n--;
151       slaves_count = n;
152       /* Skip unused entries in the slaves array.  */
153       if (slaves[n].used)
154         {
155           pid_t slave = slaves[n].child;
156
157           /* Kill the slave.  */
158           kill (slave, TERMINATOR);
159         }
160     }
161 }
162
163 /* Register a subprocess as being a slave process.  This means that the
164    subprocess will be terminated when its creator receives a catchable fatal
165    signal or exits normally.  Registration ends when wait_subprocess()
166    notices that the subprocess has exited.  */
167 void
168 register_slave_subprocess (pid_t child)
169 {
170   static bool cleanup_slaves_registered = false;
171   if (!cleanup_slaves_registered)
172     {
173       atexit (cleanup_slaves);
174       at_fatal_signal (cleanup_slaves);
175       cleanup_slaves_registered = true;
176     }
177
178   /* Try to store the new slave in an unused entry of the slaves array.  */
179   {
180     slaves_entry_t *s = slaves;
181     slaves_entry_t *s_end = s + slaves_count;
182
183     for (; s < s_end; s++)
184       if (!s->used)
185         {
186           /* The two uses of 'volatile' in the slaves_entry_t type above
187              (and ISO C 99 section 5.1.2.3.(5)) ensure that we mark the
188              entry as used only after the child pid has been written to the
189              memory location s->child.  */
190           s->child = child;
191           s->used = 1;
192           return;
193         }
194   }
195
196   if (slaves_count == slaves_allocated)
197     {
198       /* Extend the slaves array.  Note that we cannot use xrealloc(),
199          because then the cleanup_slaves() function could access an already
200          deallocated array.  */
201       slaves_entry_t *old_slaves = slaves;
202       size_t new_slaves_allocated = 2 * slaves_allocated;
203       slaves_entry_t *new_slaves =
204         (slaves_entry_t *)
205         malloc (new_slaves_allocated * sizeof (slaves_entry_t));
206       if (new_slaves == NULL)
207         {
208           /* xalloc_die() will call exit() which will invoke cleanup_slaves().
209              Additionally we need to kill child, because it's not yet among
210              the slaves list.  */
211           kill (child, TERMINATOR);
212           xalloc_die ();
213         }
214       memcpy (new_slaves, old_slaves,
215               slaves_allocated * sizeof (slaves_entry_t));
216       slaves = new_slaves;
217       slaves_allocated = new_slaves_allocated;
218       /* Now we can free the old slaves array.  */
219       if (old_slaves != static_slaves)
220         free (old_slaves);
221     }
222   /* The three uses of 'volatile' in the types above (and ISO C 99 section
223      5.1.2.3.(5)) ensure that we increment the slaves_count only after the
224      new slave and its 'used' bit have been written to the memory locations
225      that make up slaves[slaves_count].  */
226   slaves[slaves_count].child = child;
227   slaves[slaves_count].used = 1;
228   slaves_count++;
229 }
230
231 /* Unregister a child from the list of slave subprocesses.  */
232 static inline void
233 unregister_slave_subprocess (pid_t child)
234 {
235   /* The easiest way to remove an entry from a list that can be used by
236      an asynchronous signal handler is just to mark it as unused.  For this,
237      we rely on sig_atomic_t.  */
238   slaves_entry_t *s = slaves;
239   slaves_entry_t *s_end = s + slaves_count;
240
241   for (; s < s_end; s++)
242     if (s->used && s->child == child)
243       s->used = 0;
244 }
245
246
247 /* Wait for a subprocess to finish.  Return its exit code.
248    If it didn't terminate correctly, exit if exit_on_error is true, otherwise
249    return 127.  */
250 int
251 wait_subprocess (pid_t child, const char *progname,
252                  bool ignore_sigpipe, bool null_stderr,
253                  bool slave_process, bool exit_on_error,
254                  int *termsigp)
255 {
256 #if HAVE_WAITID && defined WNOWAIT && 0
257   /* Commented out because waitid() without WEXITED and with WNOWAIT doesn't
258      work: On Solaris 7 and OSF/1 4.0, it returns -1 and sets errno = ECHILD,
259      and on HP-UX 10.20 it just hangs.  */
260   /* Use of waitid() with WNOWAIT avoids a race condition: If slave_process is
261      true, and this process sleeps a very long time between the return from
262      waitpid() and the execution of unregister_slave_subprocess(), and
263      meanwhile another process acquires the same PID as child, and then - still
264      before unregister_slave_subprocess() - this process gets a fatal signal,
265      it would kill the other totally unrelated process.  */
266   siginfo_t info;
267
268   if (termsigp != NULL)
269     *termsigp = 0;
270   for (;;)
271     {
272       if (waitid (P_PID, child, &info, WEXITED | (slave_process ? WNOWAIT : 0))
273           < 0)
274         {
275 # ifdef EINTR
276           if (errno == EINTR)
277             continue;
278 # endif
279           if (exit_on_error || !null_stderr)
280             error (exit_on_error ? EXIT_FAILURE : 0, errno,
281                    _("%s subprocess"), progname);
282           return 127;
283         }
284
285       /* info.si_code is set to one of CLD_EXITED, CLD_KILLED, CLD_DUMPED,
286          CLD_TRAPPED, CLD_STOPPED, CLD_CONTINUED.  Loop until the program
287          terminates.  */
288       if (info.si_code == CLD_EXITED
289           || info.si_code == CLD_KILLED || info.si_code == CLD_DUMPED)
290         break;
291     }
292
293   /* The child process has exited or was signalled.  */
294
295   if (slave_process)
296     {
297       /* Unregister the child from the list of slave subprocesses, so that
298          later, when we exit, we don't kill a totally unrelated process which
299          may have acquired the same pid.  */
300       unregister_slave_subprocess (child);
301
302       /* Now remove the zombie from the process list.  */
303       for (;;)
304         {
305           if (waitid (P_PID, child, &info, WEXITED) < 0)
306             {
307 # ifdef EINTR
308               if (errno == EINTR)
309                 continue;
310 # endif
311               if (exit_on_error || !null_stderr)
312                 error (exit_on_error ? EXIT_FAILURE : 0, errno,
313                        _("%s subprocess"), progname);
314               return 127;
315             }
316           break;
317         }
318     }
319
320   switch (info.si_code)
321     {
322     case CLD_KILLED:
323     case CLD_DUMPED:
324       if (termsigp != NULL)
325         *termsigp = info.si_status; /* TODO: or info.si_signo? */
326 # ifdef SIGPIPE
327       if (info.si_status == SIGPIPE && ignore_sigpipe)
328         return 0;
329 # endif
330       if (exit_on_error || !null_stderr)
331         error (exit_on_error ? EXIT_FAILURE : 0, 0,
332                _("%s subprocess got fatal signal %d"),
333                progname, info.si_status);
334       return 127;
335     case CLD_EXITED:
336       if (info.si_status == 127)
337         {
338           if (exit_on_error || !null_stderr)
339             error (exit_on_error ? EXIT_FAILURE : 0, 0,
340                    _("%s subprocess failed"), progname);
341           return 127;
342         }
343       return info.si_status;
344     default:
345       abort ();
346     }
347 #else
348   /* waitpid() is just as portable as wait() nowadays.  */
349   WAIT_T status;
350
351   if (termsigp != NULL)
352     *termsigp = 0;
353   *(int *) &status = 0;
354   for (;;)
355     {
356       int result = waitpid (child, &status, 0);
357
358       if (result != child)
359         {
360 # ifdef EINTR
361           if (errno == EINTR)
362             continue;
363 # endif
364 # if 0 /* defined ECHILD */
365           if (errno == ECHILD)
366             {
367               /* Child process nonexistent?! Assume it terminated
368                  successfully.  */
369               *(int *) &status = 0;
370               break;
371             }
372 # endif
373           if (exit_on_error || !null_stderr)
374             error (exit_on_error ? EXIT_FAILURE : 0, errno,
375                    _("%s subprocess"), progname);
376           return 127;
377         }
378
379       /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
380          must always be true, since we did not specify WCONTINUED in the
381          waitpid() call.  Loop until the program terminates.  */
382       if (!WIFSTOPPED (status))
383         break;
384     }
385
386   /* The child process has exited or was signalled.  */
387
388   if (slave_process)
389     /* Unregister the child from the list of slave subprocesses, so that
390        later, when we exit, we don't kill a totally unrelated process which
391        may have acquired the same pid.  */
392     unregister_slave_subprocess (child);
393
394   if (WIFSIGNALED (status))
395     {
396       if (termsigp != NULL)
397         *termsigp = WTERMSIG (status);
398 # ifdef SIGPIPE
399       if (WTERMSIG (status) == SIGPIPE && ignore_sigpipe)
400         return 0;
401 # endif
402       if (exit_on_error || !null_stderr)
403         error (exit_on_error ? EXIT_FAILURE : 0, 0,
404                _("%s subprocess got fatal signal %d"),
405                progname, (int) WTERMSIG (status));
406       return 127;
407     }
408   if (!WIFEXITED (status))
409     abort ();
410   if (WEXITSTATUS (status) == 127)
411     {
412       if (exit_on_error || !null_stderr)
413         error (exit_on_error ? EXIT_FAILURE : 0, 0,
414                _("%s subprocess failed"), progname);
415       return 127;
416     }
417   return WEXITSTATUS (status);
418 #endif
419 }