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