packages feed

posix-waitpid (empty) → 0.1

raw patch · 5 files changed

+93/−0 lines, 5 filesdep +basedep +unixsetup-changed

Dependencies added: base, unix

Files

+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2010 Richard Smith++Permission is hereby granted, free of charge, to any person+obtaining a copy of this software and associated documentation+files (the "Software"), to deal in the Software without+restriction, including without limitation the rights to use,+copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the+Software is furnished to do so, subject to the following+conditions:++The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR+OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ System/Posix/Waitpid.hs view
@@ -0,0 +1,32 @@+{-# LANGUAGE ForeignFunctionInterface #-}+{-# CFILES System/Posix/waitpid.c #-}++module System.Posix.Waitpid where++import Control.Monad+import Data.List+import Foreign+import Foreign.C+import System.Posix.Signals (Signal)+import System.Posix.Types (CPid)++foreign import ccall unsafe "SystemPosixWaitpid_waitpid" c_waitpid :: CPid -> Ptr CInt -> CInt -> IO CPid++data Flag = NoHang | IncludeUntraced | IncludeContinued deriving Show+data Status = Exited Int | Signaled Signal | Stopped Signal | Continued deriving (Show, Eq)++waitpid :: CPid -> [Flag] -> IO (Maybe (CPid, Status))+waitpid pid flags = alloca $ \status -> do+  child <- throwErrnoIfMinus1 "waitpid" $ c_waitpid pid status options+  stat <- peek status+  return $ guard (child /= 0) >> return (child, extractStatus stat)+ where+  options = foldl' (.|.) 0 (map flagValue flags)+  flagValue NoHang = 1+  flagValue IncludeUntraced = 2+  flagValue IncludeContinued = 4+  extractStatus stat | stat < 0x10000 = Exited (fromIntegral stat)+                     | stat < 0x20000 = Signaled (stat - 0x10000)+                     | stat < 0x30000 = Stopped (stat - 0x20000)+                     | stat == 0x30000 = Continued+                     | otherwise = error $ "waitpid: unexpected status " ++ show stat
+ System/Posix/waitpid.c view
@@ -0,0 +1,22 @@+#include <sys/types.h>+#include <sys/wait.h>++pid_t SystemPosixWaitpid_waitpid(pid_t pid, int *result, int opts)+{+  int options = 0;+  if (opts & 1) options |= WNOHANG;+  if (opts & 2) options |= WUNTRACED;+  if (opts & 4) options |= WCONTINUED;++  int status = 0;+  int retval = waitpid(pid, result ? &status : 0, options);+  if (result)+  {+    if (WIFEXITED(status)) { *result = WEXITSTATUS(status); }+    else if (WIFSIGNALED(status)) { *result = 0x10000 + WTERMSIG(status); }+    else if (WIFSTOPPED(status)) { *result = 0x20000 + WSTOPSIG(status); }+    else if (WIFCONTINUED(status)) { *result = 0x30000; }+    else { *result = 0; }+  }+  return retval;+}
+ posix-waitpid.cabal view
@@ -0,0 +1,15 @@+name:                posix-waitpid+version:             0.1+synopsis:            Low-level wrapping of POSIX waitpid(2).+description:         A low-level wrapping of POSIX waitpid(2).+category:            System+license:             MIT+license-file:        LICENSE+author:              Richard Smith+maintainer:          zygoloid@metafoo.co.uk+build-type:          Simple+cabal-version:       >= 1.6+build-depends:       base == 4.*, unix+c-sources:           System/Posix/waitpid.c+exposed-modules:     System.Posix.Waitpid+extensions:          ForeignFunctionInterface