module RawFilePath.Process.Basic where
import RawFilePath.Import hiding (ClosedHandle)
import RawFilePath.Process.Common
import RawFilePath.Process.Internal
import RawFilePath.Process.Posix
startProcess
:: (StreamType stdin, StreamType stdout, StreamType stderr)
=> ProcessConf stdin stdout stderr
-> IO (Process stdin stdout stderr)
startProcess = createProcessInternal
stopProcess :: Process stdin stdout stderr -> IO ExitCode
stopProcess p = do
terminateProcess p
waitForProcess p
waitForProcess
:: Process stdin stdout stderr
-> IO ExitCode
waitForProcess ph = lockWaitpid $ do
p_ <- modifyProcessHandle ph $ \ p_ -> return (p_,p_)
case p_ of
ClosedHandle e -> return e
OpenHandle h -> do
e <- alloca $ \ pret -> do
throwErrnoIfMinus1Retry_ "waitForProcess" (c_waitForProcess h pret)
modifyProcessHandle ph $ \ p_' ->
case p_' of
ClosedHandle e -> return (p_', e)
OpenExtHandle{} -> return (p_', ExitFailure (1))
OpenHandle ph' -> do
closePHANDLE ph'
code <- peek pret
let e = if code == 0
then ExitSuccess
else ExitFailure (fromIntegral code)
return (ClosedHandle e, e)
when delegatingCtlc $
endDelegateControlC e
return e
OpenExtHandle _ _job _iocp ->
return $ ExitFailure (1)
where
lockWaitpid m = withMVar (waitpidLock ph) $ \ () -> m
delegatingCtlc = mbDelegateCtlc ph
terminateProcess :: Process stdin stdout stderr -> IO ()
terminateProcess p = withProcessHandle p $ \ case
ClosedHandle _ -> return ()
OpenExtHandle{} -> error
"terminateProcess with OpenExtHandle should not happen on POSIX."
OpenHandle h -> do
throwErrnoIfMinus1Retry_ "terminateProcess" $ c_terminateProcess h
return ()