[commit: packages/process] master: Change exit code encoding of `waitForProcess` yet again (ffe7773)

git at git.haskell.org git at git.haskell.org
Tue Nov 12 22:54:56 UTC 2013


Repository : ssh://git@git.haskell.org/process

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/ffe7773c7fd222a946d17f489020098a45b5c8af/process

>---------------------------------------------------------------

commit ffe7773c7fd222a946d17f489020098a45b5c8af
Author: Herbert Valerio Riedel <hvr at gnu.org>
Date:   Tue Nov 12 23:45:34 2013 +0100

    Change exit code encoding of `waitForProcess` yet again
    
    This changes the exit code encoding from `(128+signum)`
    (as introduced via 5403824028) to
    
    {{{#!hs
    if coredump then 0x8000 else 0 .|. signum `shiftL` 8 .|. exitstatus
    }}}
    
    in order to address the `process`-package part of #7229
    
    Signed-off-by: Herbert Valerio Riedel <hvr at gnu.org>


>---------------------------------------------------------------

ffe7773c7fd222a946d17f489020098a45b5c8af
 System/Process.hs       |   29 +++++++++++++++++++----------
 cbits/runProcess.c      |   22 ++++++++++++++--------
 changelog               |    3 ++-
 tests/process009.stdout |    6 +++---
 4 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/System/Process.hs b/System/Process.hs
index 91351e8..0eb8aeb 100644
--- a/System/Process.hs
+++ b/System/Process.hs
@@ -320,16 +320,25 @@ runInteractiveProcess1 fun cmd = do
 
 {- | Waits for the specified process to terminate, and returns its exit code.
 
-     GHC Note: in order to call @waitForProcess@ without blocking all the
-     other threads in the system, you must compile the program with
-     @-threaded at .
-
-     On Unix systems, if the process died as the result of a signal,
-     then the exit code returned is @ExitFailure (128 + signal)@ where
-     @signal@ is the signal number.  The signal numbers are
-     platform-specific, so to test for a specific signal use the
-     constants provided by @System.Posix.Signals@ in the @unix@
-     package.
+GHC Note: in order to call @waitForProcess@ without blocking all the
+other threads in the system, you must compile the program with
+ at -threaded@.
+
+(/Since: 1.2.0.0/) On Unix systems, if the process died as the result
+of a signal, then the exit code returned is
+
+@
+'ExitFailure' ((if /coredump/ then 0x8000 else 0) .|. /signum/ `shiftL` 8)
+@
+
+where @/coredump/@ is @True@ if a core file was created and @/signum/@
+is the signal number.  The signal numbers are platform-specific, so to
+test for a specific signal use the constants provided by
+ at System.Posix.Signals@ in the @unix@ package.  This encoding avoids to
+overlap with non-signal exit codes, as the exit codes reported for
+normal (other than 'ExitSuccess') process termination are in the range
+ at 1-255@.
+
 -}
 waitForProcess
   :: ProcessHandle
diff --git a/cbits/runProcess.c b/cbits/runProcess.c
index 3462cfc..796b697 100644
--- a/cbits/runProcess.c
+++ b/cbits/runProcess.c
@@ -22,12 +22,18 @@
    UNIX versions
    ------------------------------------------------------------------------- */
 
-//
-// If a process terminates with a signal, the exit status we return to
-// via the System.Process API follows the Unix shell convention of
-// (128 + signal).
-//
-#define TERMSIG_STATUS(r) ((r) | 0x80)
+// If a process was terminated by a signal, the exit status we return
+// via the System.Process API is (signum << 8), and if a core-file has
+// been generated (and reported by the OS) the 16th bit (i.e. 0x8000)
+// is additionally set; this encoding avoids collision with normal
+// process termination status codes, as according to
+// http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
+// WEXITSTATUS(s) returns an 8-bit value. See also #7229.
+#if defined(WCOREDUMP)
+#define TERMSIG_EXITSTATUS(s) ((WCOREDUMP(s) ? 0x8000 : 0) | (WTERMSIG(s) << 8))
+#else
+#define TERMSIG_EXITSTATUS(s) (WTERMSIG(s) << 8)
+#endif
 
 static long max_fd = 0;
 
@@ -342,7 +348,7 @@ getProcessExitCode (ProcHandle handle, int *pExitCode)
         else
             if (WIFSIGNALED(wstat))
             {
-                *pExitCode = TERMSIG_STATUS(WTERMSIG(wstat));
+                *pExitCode = TERMSIG_EXITSTATUS(wstat);
                 return 1;
             }
             else
@@ -378,7 +384,7 @@ int waitForProcess (ProcHandle handle, int *pret)
     else {
         if (WIFSIGNALED(wstat))
         {
-            *pret = TERMSIG_STATUS(WTERMSIG(wstat));
+            *pret = TERMSIG_EXITSTATUS(wstat);
             return 0;
         }
         else
diff --git a/changelog b/changelog
index fc6a9a1..dbe7ef2 100644
--- a/changelog
+++ b/changelog
@@ -4,7 +4,8 @@
         * Remove NHC specific code
         * Add support for `base-4.7.0.0`
         * Improve `showCommandForUser` to reduce redundant quoting
-        * Use `ExitFailure (128+signal)` on Unix when a proc terminates due to a signal
+        * Use `ExitFailure (shiftL signum 8)` on Unix when a proc is terminated due to a signal;
+          sets also `bit 15` if a core file was generated.
         * Deprecate `module System.Cmd`
         * On non-Windows, the child thread now comunicates any errors back
         to the parent thread via pipes.
diff --git a/tests/process009.stdout b/tests/process009.stdout
index ccc35c1..b7c54b9 100644
--- a/tests/process009.stdout
+++ b/tests/process009.stdout
@@ -1,3 +1,3 @@
-ExitFailure 129
-Just (ExitFailure 129)
-Just (ExitFailure 129)
+ExitFailure 256
+Just (ExitFailure 256)
+Just (ExitFailure 256)



More information about the ghc-commits mailing list