Add isnanf module.
[pspp] / lib / wait-process.c
index f39b41bf0939ff0a77d180f06300899d40967f51..585272682db2d31ff44818447aca89b2e5086c0e 100644 (file)
@@ -1,11 +1,11 @@
 /* Waiting for a subprocess to finish.
-   Copyright (C) 2001-2003, 2005-2006 Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2005-2008 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
-   This program is free software; you can redistribute it and/or modify
+   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,8 +13,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
 #include <config.h>
@@ -89,7 +88,6 @@
 #endif
 
 #include "error.h"
-#include "exit.h"
 #include "fatal-signal.h"
 #include "xalloc.h"
 #include "gettext.h"
@@ -252,12 +250,13 @@ unregister_slave_subprocess (pid_t child)
 int
 wait_subprocess (pid_t child, const char *progname,
                 bool ignore_sigpipe, bool null_stderr,
-                bool slave_process, bool exit_on_error)
+                bool slave_process, bool exit_on_error,
+                int *termsigp)
 {
 #if HAVE_WAITID && defined WNOWAIT && 0
-  /* Commented out because waitid() with WNOWAIT doesn't work: On Solaris 7
-     and OSF/1 4.0, it returns -1 and sets errno = ECHILD, and on HP-UX 10.20
-     it just hangs.  */
+  /* Commented out because waitid() without WEXITED and with WNOWAIT doesn't
+     work: On Solaris 7 and OSF/1 4.0, it returns -1 and sets errno = ECHILD,
+     and on HP-UX 10.20 it just hangs.  */
   /* Use of waitid() with WNOWAIT avoids a race condition: If slave_process is
      true, and this process sleeps a very long time between the return from
      waitpid() and the execution of unregister_slave_subprocess(), and
@@ -265,9 +264,13 @@ wait_subprocess (pid_t child, const char *progname,
      before unregister_slave_subprocess() - this process gets a fatal signal,
      it would kill the other totally unrelated process.  */
   siginfo_t info;
+
+  if (termsigp != NULL)
+    *termsigp = 0;
   for (;;)
     {
-      if (waitid (P_PID, child, &info, slave_process ? WNOWAIT : 0) < 0)
+      if (waitid (P_PID, child, &info, WEXITED | (slave_process ? WNOWAIT : 0))
+         < 0)
        {
 # ifdef EINTR
          if (errno == EINTR)
@@ -299,7 +302,7 @@ wait_subprocess (pid_t child, const char *progname,
       /* Now remove the zombie from the process list.  */
       for (;;)
        {
-         if (waitid (P_PID, child, &info, 0) < 0)
+         if (waitid (P_PID, child, &info, WEXITED) < 0)
            {
 # ifdef EINTR
              if (errno == EINTR)
@@ -318,6 +321,8 @@ wait_subprocess (pid_t child, const char *progname,
     {
     case CLD_KILLED:
     case CLD_DUMPED:
+      if (termsigp != NULL)
+       *termsigp = info.si_status; /* TODO: or info.si_signo? */
 # ifdef SIGPIPE
       if (info.si_status == SIGPIPE && ignore_sigpipe)
        return 0;
@@ -343,6 +348,8 @@ wait_subprocess (pid_t child, const char *progname,
   /* waitpid() is just as portable as wait() nowadays.  */
   WAIT_T status;
 
+  if (termsigp != NULL)
+    *termsigp = 0;
   *(int *) &status = 0;
   for (;;)
     {
@@ -370,7 +377,8 @@ wait_subprocess (pid_t child, const char *progname,
        }
 
       /* One of WIFSIGNALED (status), WIFEXITED (status), WIFSTOPPED (status)
-        must always be true.  Loop until the program terminates.  */
+        must always be true, since we did not specify WCONTINUED in the
+        waitpid() call.  Loop until the program terminates.  */
       if (!WIFSTOPPED (status))
        break;
     }
@@ -385,6 +393,8 @@ wait_subprocess (pid_t child, const char *progname,
 
   if (WIFSIGNALED (status))
     {
+      if (termsigp != NULL)
+       *termsigp = WTERMSIG (status);
 # ifdef SIGPIPE
       if (WTERMSIG (status) == SIGPIPE && ignore_sigpipe)
        return 0;
@@ -395,6 +405,8 @@ wait_subprocess (pid_t child, const char *progname,
               progname, (int) WTERMSIG (status));
       return 127;
     }
+  if (!WIFEXITED (status))
+    abort ();
   if (WEXITSTATUS (status) == 127)
     {
       if (exit_on_error || !null_stderr)