2012-07-19 04:43:36 +00:00
|
|
|
{- System.Process enhancements, including additional ways of running
|
|
|
|
- processes, and logging.
|
2012-07-18 19:30:26 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-05-10 20:08:53 +00:00
|
|
|
{-# LANGUAGE CPP, Rank2Types #-}
|
2012-07-18 19:30:26 +00:00
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
module Utility.Process (
|
|
|
|
module X,
|
|
|
|
CreateProcess,
|
|
|
|
StdHandle(..),
|
2012-08-25 00:50:39 +00:00
|
|
|
readProcess,
|
2012-07-19 04:43:36 +00:00
|
|
|
readProcessEnv,
|
2012-08-25 00:50:39 +00:00
|
|
|
writeReadProcessEnv,
|
2012-07-19 04:43:36 +00:00
|
|
|
forceSuccessProcess,
|
|
|
|
checkSuccessProcess,
|
2012-09-22 03:25:06 +00:00
|
|
|
ignoreFailureProcess,
|
2012-07-19 04:43:36 +00:00
|
|
|
createProcessSuccess,
|
|
|
|
createProcessChecked,
|
|
|
|
createBackgroundProcess,
|
2013-02-26 17:04:37 +00:00
|
|
|
processTranscript,
|
2012-07-19 04:43:36 +00:00
|
|
|
withHandle,
|
|
|
|
withBothHandles,
|
2012-10-28 17:51:14 +00:00
|
|
|
withQuietOutput,
|
2012-07-19 04:43:36 +00:00
|
|
|
createProcess,
|
|
|
|
runInteractiveProcess,
|
2012-09-22 03:25:06 +00:00
|
|
|
stdinHandle,
|
|
|
|
stdoutHandle,
|
|
|
|
stderrHandle,
|
2012-07-19 04:43:36 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified System.Process
|
|
|
|
import System.Process as X hiding (CreateProcess(..), createProcess, runInteractiveProcess, readProcess, readProcessWithExitCode, system, rawSystem, runInteractiveCommand, runProcess)
|
2012-07-19 05:02:22 +00:00
|
|
|
import System.Process hiding (createProcess, runInteractiveProcess, readProcess)
|
2012-07-18 19:30:26 +00:00
|
|
|
import System.Exit
|
|
|
|
import System.IO
|
2012-07-19 04:43:36 +00:00
|
|
|
import System.Log.Logger
|
2012-08-25 00:50:39 +00:00
|
|
|
import Control.Concurrent
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
import Control.Monad
|
2013-02-26 17:04:37 +00:00
|
|
|
import Data.Maybe
|
2013-05-10 21:57:21 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-02-26 17:04:37 +00:00
|
|
|
import System.Posix.IO
|
2013-05-10 20:08:53 +00:00
|
|
|
#endif
|
2012-07-18 19:30:26 +00:00
|
|
|
|
|
|
|
import Utility.Misc
|
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
type CreateProcessRunner = forall a. CreateProcess -> ((Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO a) -> IO a
|
|
|
|
|
|
|
|
data StdHandle = StdinHandle | StdoutHandle | StderrHandle
|
|
|
|
deriving (Eq)
|
|
|
|
|
2012-08-25 00:50:39 +00:00
|
|
|
{- Normally, when reading from a process, it does not need to be fed any
|
|
|
|
- standard input. -}
|
|
|
|
readProcess :: FilePath -> [String] -> IO String
|
|
|
|
readProcess cmd args = readProcessEnv cmd args Nothing
|
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
readProcessEnv :: FilePath -> [String] -> Maybe [(String, String)] -> IO String
|
|
|
|
readProcessEnv cmd args environ =
|
|
|
|
withHandle StdoutHandle createProcessSuccess p $ \h -> do
|
|
|
|
output <- hGetContentsStrict h
|
|
|
|
hClose h
|
|
|
|
return output
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
p = (proc cmd args)
|
|
|
|
{ std_out = CreatePipe
|
|
|
|
, env = environ
|
|
|
|
}
|
2012-07-19 04:43:36 +00:00
|
|
|
|
2012-11-25 17:27:20 +00:00
|
|
|
{- Writes a string to a process on its stdin,
|
2012-10-12 16:19:30 +00:00
|
|
|
- returns its output, and also allows specifying the environment.
|
|
|
|
-}
|
2012-08-25 00:50:39 +00:00
|
|
|
writeReadProcessEnv
|
|
|
|
:: FilePath
|
|
|
|
-> [String]
|
|
|
|
-> Maybe [(String, String)]
|
2012-10-12 16:19:30 +00:00
|
|
|
-> String
|
|
|
|
-> (Maybe (Handle -> IO ()))
|
2012-08-25 00:50:39 +00:00
|
|
|
-> IO String
|
2012-10-12 16:19:30 +00:00
|
|
|
writeReadProcessEnv cmd args environ input adjusthandle = do
|
2012-08-25 00:50:39 +00:00
|
|
|
(Just inh, Just outh, _, pid) <- createProcess p
|
|
|
|
|
2012-10-12 16:19:30 +00:00
|
|
|
maybe (return ()) (\a -> a inh) adjusthandle
|
|
|
|
maybe (return ()) (\a -> a outh) adjusthandle
|
|
|
|
|
2012-08-25 00:50:39 +00:00
|
|
|
-- 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
|
|
|
|
when (not (null input)) $ do hPutStr inh input; hFlush inh
|
|
|
|
hClose inh -- done with stdin
|
|
|
|
|
|
|
|
-- wait on the output
|
|
|
|
takeMVar outMVar
|
|
|
|
hClose outh
|
|
|
|
|
|
|
|
-- wait on the process
|
|
|
|
forceSuccessProcess p pid
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
p = (proc cmd args)
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = Inherit
|
|
|
|
, env = environ
|
|
|
|
}
|
2012-08-25 00:50:39 +00:00
|
|
|
|
2012-09-26 02:48:17 +00:00
|
|
|
{- Waits for a ProcessHandle, and throws an IOError if the process
|
2012-07-18 19:30:26 +00:00
|
|
|
- did not exit successfully. -}
|
2012-07-19 04:43:36 +00:00
|
|
|
forceSuccessProcess :: CreateProcess -> ProcessHandle -> IO ()
|
|
|
|
forceSuccessProcess p pid = do
|
2012-07-18 19:30:26 +00:00
|
|
|
code <- waitForProcess pid
|
|
|
|
case code of
|
|
|
|
ExitSuccess -> return ()
|
2012-09-26 02:48:17 +00:00
|
|
|
ExitFailure n -> fail $ showCmd p ++ " exited " ++ show n
|
2012-07-18 19:30:26 +00:00
|
|
|
|
2013-02-26 17:04:37 +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. -}
|
2012-07-19 04:43:36 +00:00
|
|
|
checkSuccessProcess :: ProcessHandle -> IO Bool
|
|
|
|
checkSuccessProcess pid = do
|
|
|
|
code <- waitForProcess pid
|
|
|
|
return $ code == ExitSuccess
|
|
|
|
|
2012-10-04 22:04:09 +00:00
|
|
|
ignoreFailureProcess :: ProcessHandle -> IO Bool
|
|
|
|
ignoreFailureProcess pid = do
|
|
|
|
void $ waitForProcess pid
|
|
|
|
return True
|
2012-09-22 03:25:06 +00:00
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
{- Runs createProcess, then an action on its handles, and then
|
|
|
|
- forceSuccessProcess. -}
|
|
|
|
createProcessSuccess :: CreateProcessRunner
|
|
|
|
createProcessSuccess p a = createProcessChecked (forceSuccessProcess p) p a
|
|
|
|
|
|
|
|
{- Runs createProcess, then an action on its handles, and then
|
|
|
|
- an action on its exit code. -}
|
|
|
|
createProcessChecked :: (ProcessHandle -> IO b) -> CreateProcessRunner
|
|
|
|
createProcessChecked checker p a = do
|
|
|
|
t@(_, _, _, pid) <- createProcess p
|
|
|
|
r <- a t
|
|
|
|
_ <- checker pid
|
|
|
|
return r
|
|
|
|
|
|
|
|
{- Leaves the process running, suitable for lazy streaming.
|
|
|
|
- Note: Zombies will result, and must be waited on. -}
|
|
|
|
createBackgroundProcess :: CreateProcessRunner
|
|
|
|
createBackgroundProcess p a = a =<< createProcess p
|
|
|
|
|
2013-02-26 17:04:37 +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. -}
|
|
|
|
processTranscript :: String -> [String] -> (Maybe String) -> IO (String, Bool)
|
2013-05-10 21:57:21 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-02-26 17:04:37 +00:00
|
|
|
processTranscript cmd opts input = do
|
|
|
|
(readf, writef) <- createPipe
|
|
|
|
readh <- fdToHandle readf
|
|
|
|
writeh <- fdToHandle writef
|
|
|
|
p@(_, _, _, pid) <- createProcess $
|
|
|
|
(proc cmd opts)
|
|
|
|
{ std_in = if isJust input then CreatePipe else Inherit
|
|
|
|
, std_out = UseHandle writeh
|
|
|
|
, std_err = UseHandle writeh
|
|
|
|
}
|
|
|
|
hClose writeh
|
|
|
|
|
|
|
|
-- fork off a thread to start consuming the output
|
|
|
|
transcript <- hGetContents readh
|
|
|
|
outMVar <- newEmptyMVar
|
|
|
|
_ <- forkIO $ E.evaluate (length transcript) >> putMVar outMVar ()
|
|
|
|
|
|
|
|
-- now write and flush any input
|
|
|
|
case input of
|
|
|
|
Just s -> do
|
|
|
|
let inh = stdinHandle p
|
|
|
|
unless (null s) $ do
|
|
|
|
hPutStr inh s
|
|
|
|
hFlush inh
|
|
|
|
hClose inh
|
|
|
|
Nothing -> return ()
|
|
|
|
|
|
|
|
-- wait on the output
|
|
|
|
takeMVar outMVar
|
|
|
|
hClose readh
|
|
|
|
|
|
|
|
ok <- checkSuccessProcess pid
|
|
|
|
return (transcript, ok)
|
2013-05-10 20:08:53 +00:00
|
|
|
#else
|
|
|
|
processTranscript = error "processTranscript TODO"
|
|
|
|
#endif
|
2013-02-26 17:04:37 +00:00
|
|
|
|
2012-07-19 04:43:36 +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. -}
|
|
|
|
withHandle
|
|
|
|
:: StdHandle
|
|
|
|
-> CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> (Handle -> IO a)
|
|
|
|
-> IO a
|
|
|
|
withHandle h creator p a = creator p' $ a . select
|
2012-12-13 04:24:19 +00:00
|
|
|
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 })
|
2012-07-19 04:43:36 +00:00
|
|
|
|
|
|
|
{- Like withHandle, but passes (stdin, stdout) handles to the action. -}
|
|
|
|
withBothHandles
|
|
|
|
:: CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> ((Handle, Handle) -> IO a)
|
|
|
|
-> IO a
|
|
|
|
withBothHandles creator p a = creator p' $ a . bothHandles
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
p' = p
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = Inherit
|
|
|
|
}
|
2012-07-19 04:43:36 +00:00
|
|
|
|
2012-10-28 17:51:14 +00:00
|
|
|
{- Forces the CreateProcessRunner to run quietly;
|
|
|
|
- both stdout and stderr are discarded. -}
|
|
|
|
withQuietOutput
|
|
|
|
:: CreateProcessRunner
|
|
|
|
-> CreateProcess
|
|
|
|
-> IO ()
|
|
|
|
withQuietOutput creator p = withFile "/dev/null" WriteMode $ \devnull -> do
|
|
|
|
let p' = p
|
|
|
|
{ std_out = UseHandle devnull
|
|
|
|
, std_err = UseHandle devnull
|
|
|
|
}
|
|
|
|
creator p' $ const $ return ()
|
|
|
|
|
2012-07-19 04:43:36 +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. -}
|
|
|
|
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"
|
|
|
|
bothHandles :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> (Handle, Handle)
|
|
|
|
bothHandles (Just hin, Just hout, _, _) = (hin, hout)
|
|
|
|
bothHandles _ = error "expected bothHandles"
|
|
|
|
|
|
|
|
{- Debugging trace for a CreateProcess. -}
|
|
|
|
debugProcess :: CreateProcess -> IO ()
|
|
|
|
debugProcess p = do
|
|
|
|
debugM "Utility.Process" $ unwords
|
|
|
|
[ action ++ ":"
|
|
|
|
, showCmd p
|
|
|
|
]
|
2012-12-13 04:24:19 +00:00
|
|
|
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
|
2012-07-19 04:43:36 +00:00
|
|
|
|
|
|
|
{- Shows the command that a CreateProcess will run. -}
|
|
|
|
showCmd :: CreateProcess -> String
|
|
|
|
showCmd = go . cmdspec
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
go (ShellCommand s) = s
|
|
|
|
go (RawCommand c ps) = c ++ " " ++ show ps
|
2012-07-19 04:43:36 +00:00
|
|
|
|
|
|
|
{- Wrappers for System.Process functions that do debug logging.
|
|
|
|
-
|
|
|
|
- More could be added, but these are the only ones I usually need.
|
|
|
|
-}
|
|
|
|
|
|
|
|
createProcess :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
|
|
|
|
createProcess p = do
|
|
|
|
debugProcess p
|
|
|
|
System.Process.createProcess p
|
|
|
|
|
|
|
|
runInteractiveProcess
|
|
|
|
:: FilePath
|
|
|
|
-> [String]
|
|
|
|
-> Maybe FilePath
|
|
|
|
-> Maybe [(String, String)]
|
|
|
|
-> IO (Handle, Handle, Handle, ProcessHandle)
|
|
|
|
runInteractiveProcess f args c e = do
|
|
|
|
debugProcess $ (proc f args)
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = CreatePipe
|
2012-09-15 21:25:05 +00:00
|
|
|
, env = e
|
2012-07-19 04:43:36 +00:00
|
|
|
}
|
|
|
|
System.Process.runInteractiveProcess f args c e
|