2014-03-30 03:10:52 +00:00
|
|
|
{- System.Process enhancements, including additional ways of running
|
|
|
|
- processes, and logging.
|
|
|
|
-
|
2015-05-30 15:05:34 +00:00
|
|
|
- Copyright 2012-2015 Joey Hess <id@joeyh.name>
|
2014-03-30 03:10:52 +00:00
|
|
|
-
|
2014-05-10 14:05:28 +00:00
|
|
|
- License: BSD-2-clause
|
2014-03-30 03:10:52 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP, Rank2Types #-}
|
2015-05-27 18:55:31 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
2014-03-30 03:10:52 +00:00
|
|
|
|
|
|
|
module Utility.Process (
|
|
|
|
module X,
|
2014-11-19 04:54:35 +00:00
|
|
|
CreateProcess(..),
|
2014-03-30 03:10:52 +00:00
|
|
|
StdHandle(..),
|
|
|
|
readProcess,
|
2014-11-19 05:02:13 +00:00
|
|
|
readProcess',
|
2014-03-30 03:10:52 +00:00
|
|
|
readProcessEnv,
|
|
|
|
writeReadProcessEnv,
|
|
|
|
forceSuccessProcess,
|
|
|
|
checkSuccessProcess,
|
|
|
|
ignoreFailureProcess,
|
|
|
|
createProcessSuccess,
|
|
|
|
createProcessChecked,
|
|
|
|
createBackgroundProcess,
|
|
|
|
processTranscript,
|
|
|
|
processTranscript',
|
|
|
|
withHandle,
|
2015-04-29 18:26:13 +00:00
|
|
|
withIOHandles,
|
|
|
|
withOEHandles,
|
2014-03-30 03:10:52 +00:00
|
|
|
withQuietOutput,
|
2015-04-29 18:26:13 +00:00
|
|
|
feedWithQuietOutput,
|
2014-03-30 03:10:52 +00:00
|
|
|
createProcess,
|
|
|
|
startInteractiveProcess,
|
|
|
|
stdinHandle,
|
|
|
|
stdoutHandle,
|
|
|
|
stderrHandle,
|
2015-04-29 18:26:13 +00:00
|
|
|
ioHandles,
|
2014-05-10 13:46:36 +00:00
|
|
|
processHandle,
|
2014-03-30 03:10:52 +00:00
|
|
|
devNull,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified System.Process
|
2015-01-04 16:52:03 +00:00
|
|
|
import qualified System.Process as X hiding (CreateProcess(..), createProcess, runInteractiveProcess, readProcess, readProcessWithExitCode, system, rawSystem, runInteractiveCommand, runProcess)
|
2014-03-30 03:10:52 +00:00
|
|
|
import System.Process hiding (createProcess, readProcess)
|
|
|
|
import System.Exit
|
|
|
|
import System.IO
|
|
|
|
import System.Log.Logger
|
|
|
|
import Control.Concurrent
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
import Control.Monad
|
|
|
|
#ifndef mingw32_HOST_OS
|
2015-01-04 16:52:03 +00:00
|
|
|
import qualified System.Posix.IO
|
2014-03-30 03:10:52 +00:00
|
|
|
#else
|
|
|
|
import Control.Applicative
|
|
|
|
#endif
|
|
|
|
import Data.Maybe
|
2015-05-27 18:55:31 +00:00
|
|
|
import Prelude
|
2014-03-30 03:10:52 +00:00
|
|
|
|
|
|
|
import Utility.Misc
|
|
|
|
import Utility.Exception
|
|
|
|
|
|
|
|
type CreateProcessRunner = forall a. CreateProcess -> ((Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO a) -> IO a
|
|
|
|
|
|
|
|
data StdHandle = StdinHandle | StdoutHandle | StderrHandle
|
|
|
|
deriving (Eq)
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Normally, when reading from a process, it does not need to be fed any
|
|
|
|
-- standard input.
|
2014-03-30 03:10:52 +00:00
|
|
|
readProcess :: FilePath -> [String] -> IO String
|
|
|
|
readProcess cmd args = readProcessEnv cmd args Nothing
|
|
|
|
|
|
|
|
readProcessEnv :: FilePath -> [String] -> Maybe [(String, String)] -> IO String
|
2014-11-19 05:02:13 +00:00
|
|
|
readProcessEnv cmd args environ = readProcess' p
|
2014-03-30 03:10:52 +00:00
|
|
|
where
|
|
|
|
p = (proc cmd args)
|
|
|
|
{ std_out = CreatePipe
|
|
|
|
, env = environ
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:02:13 +00:00
|
|
|
readProcess' :: CreateProcess -> IO String
|
|
|
|
readProcess' p = withHandle StdoutHandle createProcessSuccess p $ \h -> do
|
|
|
|
output <- hGetContentsStrict h
|
|
|
|
hClose h
|
|
|
|
return output
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Runs an action to write to a process on its stdin,
|
|
|
|
-- returns its output, and also allows specifying the environment.
|
2014-03-30 03:10:52 +00:00
|
|
|
writeReadProcessEnv
|
|
|
|
:: FilePath
|
|
|
|
-> [String]
|
|
|
|
-> Maybe [(String, String)]
|
|
|
|
-> (Maybe (Handle -> IO ()))
|
|
|
|
-> (Maybe (Handle -> IO ()))
|
|
|
|
-> IO String
|
|
|
|
writeReadProcessEnv cmd args environ writestdin adjusthandle = do
|
|
|
|
(Just inh, Just outh, _, pid) <- createProcess p
|
|
|
|
|
|
|
|
maybe (return ()) (\a -> a inh) adjusthandle
|
|
|
|
maybe (return ()) (\a -> a outh) adjusthandle
|
|
|
|
|
|
|
|
-- fork off a thread to start consuming the output
|
|
|
|
output <- hGetContents outh
|
|
|
|
outMVar <- newEmptyMVar
|
|
|
|
_ <- forkIO $ E.evaluate (length output) >> putMVar outMVar ()
|
|
|
|
|
|
|
|
-- now write and flush any input
|
|
|
|
maybe (return ()) (\a -> a inh >> hFlush inh) writestdin
|
|
|
|
hClose inh -- done with stdin
|
|
|
|
|
|
|
|
-- wait on the output
|
|
|
|
takeMVar outMVar
|
|
|
|
hClose outh
|
|
|
|
|
|
|
|
-- wait on the process
|
|
|
|
forceSuccessProcess p pid
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
where
|
|
|
|
p = (proc cmd args)
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = Inherit
|
|
|
|
, env = environ
|
|
|
|
}
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Waits for a ProcessHandle, and throws an IOError if the process
|
|
|
|
-- did not exit successfully.
|
2014-03-30 03:10:52 +00:00
|
|
|
forceSuccessProcess :: CreateProcess -> ProcessHandle -> IO ()
|
|
|
|
forceSuccessProcess p pid = do
|
|
|
|
code <- waitForProcess pid
|
|
|
|
case code of
|
|
|
|
ExitSuccess -> return ()
|
|
|
|
ExitFailure n -> fail $ showCmd p ++ " exited " ++ show n
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Waits for a ProcessHandle and returns True if it exited successfully.
|
|
|
|
-- Note that using this with createProcessChecked will throw away
|
|
|
|
-- the Bool, and is only useful to ignore the exit code of a process,
|
|
|
|
-- while still waiting for it. -}
|
2014-03-30 03:10:52 +00:00
|
|
|
checkSuccessProcess :: ProcessHandle -> IO Bool
|
|
|
|
checkSuccessProcess pid = do
|
|
|
|
code <- waitForProcess pid
|
|
|
|
return $ code == ExitSuccess
|
|
|
|
|
|
|
|
ignoreFailureProcess :: ProcessHandle -> IO Bool
|
|
|
|
ignoreFailureProcess pid = do
|
|
|
|
void $ waitForProcess pid
|
|
|
|
return True
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Runs createProcess, then an action on its handles, and then
|
|
|
|
-- forceSuccessProcess.
|
2014-03-30 03:10:52 +00:00
|
|
|
createProcessSuccess :: CreateProcessRunner
|
|
|
|
createProcessSuccess p a = createProcessChecked (forceSuccessProcess p) p a
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Runs createProcess, then an action on its handles, and then
|
|
|
|
-- a checker action on its exit code, which must wait for the process.
|
2014-03-30 03:10:52 +00:00
|
|
|
createProcessChecked :: (ProcessHandle -> IO b) -> CreateProcessRunner
|
|
|
|
createProcessChecked checker p a = do
|
|
|
|
t@(_, _, _, pid) <- createProcess p
|
|
|
|
r <- tryNonAsync $ a t
|
|
|
|
_ <- checker pid
|
|
|
|
either E.throw return r
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Leaves the process running, suitable for lazy streaming.
|
|
|
|
-- Note: Zombies will result, and must be waited on.
|
2014-03-30 03:10:52 +00:00
|
|
|
createBackgroundProcess :: CreateProcessRunner
|
|
|
|
createBackgroundProcess p a = a =<< createProcess p
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Runs a process, optionally feeding it some input, and
|
|
|
|
-- returns a transcript combining its stdout and stderr, and
|
|
|
|
-- whether it succeeded or failed.
|
2014-03-30 03:10:52 +00:00
|
|
|
processTranscript :: String -> [String] -> (Maybe String) -> IO (String, Bool)
|
|
|
|
processTranscript cmd opts input = processTranscript' cmd opts Nothing input
|
|
|
|
|
|
|
|
processTranscript' :: String -> [String] -> Maybe [(String, String)] -> (Maybe String) -> IO (String, Bool)
|
2014-05-29 18:29:23 +00:00
|
|
|
processTranscript' cmd opts environ input = do
|
2014-03-30 03:10:52 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
{- This implementation interleves stdout and stderr in exactly the order
|
|
|
|
- the process writes them. -}
|
2015-01-04 16:52:03 +00:00
|
|
|
(readf, writef) <- System.Posix.IO.createPipe
|
|
|
|
readh <- System.Posix.IO.fdToHandle readf
|
|
|
|
writeh <- System.Posix.IO.fdToHandle writef
|
2014-03-30 03:10:52 +00:00
|
|
|
p@(_, _, _, pid) <- createProcess $
|
|
|
|
(proc cmd opts)
|
|
|
|
{ std_in = if isJust input then CreatePipe else Inherit
|
|
|
|
, std_out = UseHandle writeh
|
|
|
|
, std_err = UseHandle writeh
|
|
|
|
, env = environ
|
|
|
|
}
|
|
|
|
hClose writeh
|
|
|
|
|
|
|
|
get <- mkreader readh
|
2014-05-29 18:29:23 +00:00
|
|
|
writeinput input p
|
2014-03-30 03:10:52 +00:00
|
|
|
transcript <- get
|
|
|
|
|
|
|
|
ok <- checkSuccessProcess pid
|
|
|
|
return (transcript, ok)
|
|
|
|
#else
|
|
|
|
{- This implementation for Windows puts stderr after stdout. -}
|
|
|
|
p@(_, _, _, pid) <- createProcess $
|
|
|
|
(proc cmd opts)
|
|
|
|
{ std_in = if isJust input then CreatePipe else Inherit
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = CreatePipe
|
|
|
|
, env = environ
|
|
|
|
}
|
|
|
|
|
|
|
|
getout <- mkreader (stdoutHandle p)
|
|
|
|
geterr <- mkreader (stderrHandle p)
|
2014-05-29 18:29:23 +00:00
|
|
|
writeinput input p
|
2014-03-30 03:10:52 +00:00
|
|
|
transcript <- (++) <$> getout <*> geterr
|
2014-05-29 18:29:23 +00:00
|
|
|
|
2014-03-30 03:10:52 +00:00
|
|
|
ok <- checkSuccessProcess pid
|
|
|
|
return (transcript, ok)
|
|
|
|
#endif
|
|
|
|
where
|
|
|
|
mkreader h = do
|
|
|
|
s <- hGetContents h
|
|
|
|
v <- newEmptyMVar
|
|
|
|
void $ forkIO $ do
|
|
|
|
void $ E.evaluate (length s)
|
|
|
|
putMVar v ()
|
|
|
|
return $ do
|
|
|
|
takeMVar v
|
|
|
|
return s
|
|
|
|
|
2014-05-29 18:29:23 +00:00
|
|
|
writeinput (Just s) p = do
|
|
|
|
let inh = stdinHandle p
|
|
|
|
unless (null s) $ do
|
|
|
|
hPutStr inh s
|
|
|
|
hFlush inh
|
|
|
|
hClose inh
|
|
|
|
writeinput Nothing _ = return ()
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Runs a CreateProcessRunner, on a CreateProcess structure, that
|
|
|
|
-- is adjusted to pipe only from/to a single StdHandle, and passes
|
|
|
|
-- the resulting Handle to an action.
|
2014-03-30 03:10:52 +00:00
|
|
|
withHandle
|
|
|
|
:: StdHandle
|
|
|
|
-> CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> (Handle -> IO a)
|
|
|
|
-> IO a
|
|
|
|
withHandle h creator p a = creator p' $ a . select
|
|
|
|
where
|
|
|
|
base = p
|
|
|
|
{ std_in = Inherit
|
|
|
|
, std_out = Inherit
|
|
|
|
, std_err = Inherit
|
|
|
|
}
|
|
|
|
(select, p')
|
|
|
|
| h == StdinHandle =
|
|
|
|
(stdinHandle, base { std_in = CreatePipe })
|
|
|
|
| h == StdoutHandle =
|
|
|
|
(stdoutHandle, base { std_out = CreatePipe })
|
|
|
|
| h == StderrHandle =
|
|
|
|
(stderrHandle, base { std_err = CreatePipe })
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Like withHandle, but passes (stdin, stdout) handles to the action.
|
2015-04-29 18:26:13 +00:00
|
|
|
withIOHandles
|
2014-03-30 03:10:52 +00:00
|
|
|
:: CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> ((Handle, Handle) -> IO a)
|
|
|
|
-> IO a
|
2015-04-29 18:26:13 +00:00
|
|
|
withIOHandles creator p a = creator p' $ a . ioHandles
|
2014-03-30 03:10:52 +00:00
|
|
|
where
|
|
|
|
p' = p
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = Inherit
|
|
|
|
}
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Like withHandle, but passes (stdout, stderr) handles to the action.
|
2015-04-29 18:26:13 +00:00
|
|
|
withOEHandles
|
|
|
|
:: CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> ((Handle, Handle) -> IO a)
|
|
|
|
-> IO a
|
|
|
|
withOEHandles creator p a = creator p' $ a . oeHandles
|
|
|
|
where
|
|
|
|
p' = p
|
|
|
|
{ std_in = Inherit
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = CreatePipe
|
|
|
|
}
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Forces the CreateProcessRunner to run quietly;
|
|
|
|
-- both stdout and stderr are discarded.
|
2014-03-30 03:10:52 +00:00
|
|
|
withQuietOutput
|
|
|
|
:: CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> IO ()
|
|
|
|
withQuietOutput creator p = withFile devNull WriteMode $ \nullh -> do
|
|
|
|
let p' = p
|
|
|
|
{ std_out = UseHandle nullh
|
|
|
|
, std_err = UseHandle nullh
|
|
|
|
}
|
|
|
|
creator p' $ const $ return ()
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Stdout and stderr are discarded, while the process is fed stdin
|
|
|
|
-- from the handle.
|
2015-04-29 18:26:13 +00:00
|
|
|
feedWithQuietOutput
|
|
|
|
:: CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> (Handle -> IO a)
|
|
|
|
-> IO a
|
|
|
|
feedWithQuietOutput creator p a = withFile devNull WriteMode $ \nullh -> do
|
|
|
|
let p' = p
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = UseHandle nullh
|
|
|
|
, std_err = UseHandle nullh
|
|
|
|
}
|
|
|
|
creator p' $ a . stdinHandle
|
|
|
|
|
2014-03-30 03:10:52 +00:00
|
|
|
devNull :: FilePath
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
devNull = "/dev/null"
|
|
|
|
#else
|
|
|
|
devNull = "NUL"
|
|
|
|
#endif
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Extract a desired handle from createProcess's tuple.
|
|
|
|
-- These partial functions are safe as long as createProcess is run
|
|
|
|
-- with appropriate parameters to set up the desired handle.
|
|
|
|
-- Get it wrong and the runtime crash will always happen, so should be
|
|
|
|
-- easily noticed.
|
2014-03-30 03:10:52 +00:00
|
|
|
type HandleExtractor = (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> Handle
|
|
|
|
stdinHandle :: HandleExtractor
|
|
|
|
stdinHandle (Just h, _, _, _) = h
|
|
|
|
stdinHandle _ = error "expected stdinHandle"
|
|
|
|
stdoutHandle :: HandleExtractor
|
|
|
|
stdoutHandle (_, Just h, _, _) = h
|
|
|
|
stdoutHandle _ = error "expected stdoutHandle"
|
|
|
|
stderrHandle :: HandleExtractor
|
|
|
|
stderrHandle (_, _, Just h, _) = h
|
|
|
|
stderrHandle _ = error "expected stderrHandle"
|
2015-04-29 18:26:13 +00:00
|
|
|
ioHandles :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> (Handle, Handle)
|
|
|
|
ioHandles (Just hin, Just hout, _, _) = (hin, hout)
|
|
|
|
ioHandles _ = error "expected ioHandles"
|
|
|
|
oeHandles :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> (Handle, Handle)
|
|
|
|
oeHandles (_, Just hout, Just herr, _) = (hout, herr)
|
|
|
|
oeHandles _ = error "expected oeHandles"
|
2014-03-30 03:10:52 +00:00
|
|
|
|
2014-05-10 13:46:36 +00:00
|
|
|
processHandle :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> ProcessHandle
|
|
|
|
processHandle (_, _, _, pid) = pid
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Debugging trace for a CreateProcess.
|
2014-03-30 03:10:52 +00:00
|
|
|
debugProcess :: CreateProcess -> IO ()
|
|
|
|
debugProcess p = do
|
|
|
|
debugM "Utility.Process" $ unwords
|
|
|
|
[ action ++ ":"
|
|
|
|
, showCmd p
|
|
|
|
]
|
|
|
|
where
|
|
|
|
action
|
|
|
|
| piped (std_in p) && piped (std_out p) = "chat"
|
|
|
|
| piped (std_in p) = "feed"
|
|
|
|
| piped (std_out p) = "read"
|
|
|
|
| otherwise = "call"
|
|
|
|
piped Inherit = False
|
|
|
|
piped _ = True
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Shows the command that a CreateProcess will run.
|
2014-03-30 03:10:52 +00:00
|
|
|
showCmd :: CreateProcess -> String
|
|
|
|
showCmd = go . cmdspec
|
|
|
|
where
|
|
|
|
go (ShellCommand s) = s
|
|
|
|
go (RawCommand c ps) = c ++ " " ++ show ps
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Starts an interactive process. Unlike runInteractiveProcess in
|
|
|
|
-- System.Process, stderr is inherited.
|
2014-03-30 03:10:52 +00:00
|
|
|
startInteractiveProcess
|
|
|
|
:: FilePath
|
|
|
|
-> [String]
|
|
|
|
-> Maybe [(String, String)]
|
|
|
|
-> IO (ProcessHandle, Handle, Handle)
|
|
|
|
startInteractiveProcess cmd args environ = do
|
|
|
|
let p = (proc cmd args)
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = Inherit
|
|
|
|
, env = environ
|
|
|
|
}
|
|
|
|
(Just from, Just to, _, pid) <- createProcess p
|
|
|
|
return (pid, to, from)
|
|
|
|
|
2015-05-30 15:05:34 +00:00
|
|
|
-- | Wrapper around 'System.Process.createProcess' from System.Process,
|
|
|
|
-- that does debug logging.
|
2014-03-30 03:10:52 +00:00
|
|
|
createProcess :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
|
|
|
|
createProcess p = do
|
|
|
|
debugProcess p
|
|
|
|
System.Process.createProcess p
|