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
|
|
|
-
|
2020-04-06 21:14:49 +00:00
|
|
|
- Copyright 2012-2020 Joey Hess <id@joeyh.name>
|
2012-07-18 19:30:26 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-07-18 19:30:26 +00:00
|
|
|
-}
|
|
|
|
|
2020-11-18 16:23:15 +00:00
|
|
|
{-# LANGUAGE CPP, Rank2Types, LambdaCase #-}
|
2021-04-05 17:40:31 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-05-10 20:31:50 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
2012-07-18 19:30:26 +00:00
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
module Utility.Process (
|
|
|
|
module X,
|
|
|
|
StdHandle(..),
|
2012-08-25 00:50:39 +00:00
|
|
|
readProcess,
|
2014-11-19 04:55:33 +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,
|
2016-03-07 00:07:38 +00:00
|
|
|
forceSuccessProcess',
|
2012-07-19 04:43:36 +00:00
|
|
|
checkSuccessProcess,
|
2018-03-08 18:02:18 +00:00
|
|
|
withNullHandle,
|
2012-07-19 04:43:36 +00:00
|
|
|
createProcess,
|
2020-06-11 20:43:24 +00:00
|
|
|
withCreateProcess,
|
2015-08-13 17:12:44 +00:00
|
|
|
waitForProcess,
|
2020-06-05 15:40:18 +00:00
|
|
|
cleanupProcess,
|
2020-11-18 16:23:15 +00:00
|
|
|
hGetLineUntilExitOrEOF,
|
2013-06-14 21:35:45 +00:00
|
|
|
startInteractiveProcess,
|
2012-09-22 03:25:06 +00:00
|
|
|
stdinHandle,
|
|
|
|
stdoutHandle,
|
|
|
|
stderrHandle,
|
2014-04-12 19:59:34 +00:00
|
|
|
processHandle,
|
2022-08-05 17:57:20 +00:00
|
|
|
showCmd,
|
2013-12-11 03:19:18 +00:00
|
|
|
devNull,
|
2012-07-19 04:43:36 +00:00
|
|
|
) where
|
|
|
|
|
2015-10-28 04:18:01 +00:00
|
|
|
import qualified Utility.Process.Shim
|
2020-12-08 19:22:18 +00:00
|
|
|
import Utility.Process.Shim as X (CreateProcess(..), ProcessHandle, StdStream(..), CmdSpec(..), proc, getPid, getProcessExitCode, shell, terminateProcess, interruptProcessGroupOf)
|
2015-10-28 04:18:01 +00:00
|
|
|
import Utility.Misc
|
|
|
|
import Utility.Exception
|
2020-06-04 16:57:22 +00:00
|
|
|
import Utility.Monad
|
2021-04-05 17:40:31 +00:00
|
|
|
import Utility.Debug
|
2015-10-28 04:18:01 +00:00
|
|
|
|
2012-07-18 19:30:26 +00:00
|
|
|
import System.Exit
|
|
|
|
import System.IO
|
2020-06-03 19:18:48 +00:00
|
|
|
import Control.Monad.IO.Class
|
2020-06-04 16:57:22 +00:00
|
|
|
import Control.Concurrent.Async
|
2020-04-06 21:14:49 +00:00
|
|
|
import qualified Data.ByteString as S
|
2012-07-18 19:30:26 +00:00
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
data StdHandle = StdinHandle | StdoutHandle | StderrHandle
|
|
|
|
deriving (Eq)
|
|
|
|
|
2015-05-30 15:27:40 +00:00
|
|
|
-- | Normally, when reading from a process, it does not need to be fed any
|
|
|
|
-- standard input.
|
2012-08-25 00:50:39 +00:00
|
|
|
readProcess :: FilePath -> [String] -> IO String
|
2020-06-04 19:36:34 +00:00
|
|
|
readProcess cmd args = readProcess' (proc cmd args)
|
2012-08-25 00:50:39 +00:00
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
readProcessEnv :: FilePath -> [String] -> Maybe [(String, String)] -> IO String
|
2020-06-04 19:36:34 +00:00
|
|
|
readProcessEnv cmd args environ =
|
|
|
|
readProcess' $ (proc cmd args) { env = environ }
|
2012-07-19 04:43:36 +00:00
|
|
|
|
2014-11-19 04:55:33 +00:00
|
|
|
readProcess' :: CreateProcess -> IO String
|
2020-06-04 19:36:34 +00:00
|
|
|
readProcess' p = withCreateProcess p' go
|
|
|
|
where
|
|
|
|
p' = p { std_out = CreatePipe }
|
|
|
|
go _ (Just h) _ pid = do
|
|
|
|
output <- hGetContentsStrict h
|
|
|
|
hClose h
|
|
|
|
forceSuccessProcess p' pid
|
|
|
|
return output
|
|
|
|
go _ _ _ _ = error "internal"
|
2014-11-19 04:55:33 +00:00
|
|
|
|
2015-05-30 15:27:40 +00:00
|
|
|
-- | Runs an action to write to a process on its stdin,
|
|
|
|
-- returns its output, and also allows specifying the environment.
|
2012-08-25 00:50:39 +00:00
|
|
|
writeReadProcessEnv
|
|
|
|
:: FilePath
|
|
|
|
-> [String]
|
|
|
|
-> Maybe [(String, String)]
|
2013-10-20 21:50:51 +00:00
|
|
|
-> (Maybe (Handle -> IO ()))
|
2020-04-06 21:14:49 +00:00
|
|
|
-> IO S.ByteString
|
2020-06-04 16:57:22 +00:00
|
|
|
writeReadProcessEnv cmd args environ writestdin = withCreateProcess p go
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
p = (proc cmd args)
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = Inherit
|
|
|
|
, env = environ
|
|
|
|
}
|
2020-06-04 16:57:22 +00:00
|
|
|
|
|
|
|
go (Just inh) (Just outh) _ pid = do
|
|
|
|
let reader = hClose outh `after` S.hGetContents outh
|
|
|
|
let writer = do
|
|
|
|
maybe (return ()) (\a -> a inh >> hFlush inh) writestdin
|
|
|
|
hClose inh
|
|
|
|
(output, ()) <- concurrently reader writer
|
|
|
|
|
|
|
|
forceSuccessProcess p pid
|
|
|
|
|
|
|
|
return output
|
|
|
|
go _ _ _ _ = error "internal"
|
2012-08-25 00:50:39 +00:00
|
|
|
|
2015-05-30 15:27:40 +00:00
|
|
|
-- | Waits for a ProcessHandle, and throws an IOError if the process
|
|
|
|
-- did not exit successfully.
|
2012-07-19 04:43:36 +00:00
|
|
|
forceSuccessProcess :: CreateProcess -> ProcessHandle -> IO ()
|
2016-03-07 00:07:38 +00:00
|
|
|
forceSuccessProcess p pid = waitForProcess pid >>= forceSuccessProcess' p
|
|
|
|
|
|
|
|
forceSuccessProcess' :: CreateProcess -> ExitCode -> IO ()
|
|
|
|
forceSuccessProcess' _ ExitSuccess = return ()
|
|
|
|
forceSuccessProcess' p (ExitFailure n) = fail $
|
|
|
|
showCmd p ++ " exited " ++ show n
|
2012-07-18 19:30:26 +00:00
|
|
|
|
2015-05-30 15:27:40 +00:00
|
|
|
-- | Waits for a ProcessHandle and returns True if it exited successfully.
|
2012-07-19 04:43:36 +00:00
|
|
|
checkSuccessProcess :: ProcessHandle -> IO Bool
|
|
|
|
checkSuccessProcess pid = do
|
|
|
|
code <- waitForProcess pid
|
|
|
|
return $ code == ExitSuccess
|
|
|
|
|
2020-06-03 19:18:48 +00:00
|
|
|
withNullHandle :: (MonadIO m, MonadMask m) => (Handle -> m a) -> m a
|
|
|
|
withNullHandle = bracket
|
|
|
|
(liftIO $ openFile devNull WriteMode)
|
|
|
|
(liftIO . hClose)
|
2018-03-08 18:02:18 +00:00
|
|
|
|
2013-12-11 03:19:18 +00:00
|
|
|
devNull :: FilePath
|
2013-06-18 01:26:06 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-12-11 03:19:18 +00:00
|
|
|
devNull = "/dev/null"
|
2013-06-18 01:26:06 +00:00
|
|
|
#else
|
2018-09-22 15:31:55 +00:00
|
|
|
-- Use device namespace to prevent GHC from rewriting path
|
2018-09-22 15:33:08 +00:00
|
|
|
devNull = "\\\\.\\NUL"
|
2013-06-18 01:26:06 +00:00
|
|
|
#endif
|
2012-10-28 17:51:14 +00:00
|
|
|
|
2015-05-30 15:27:40 +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.
|
2012-07-19 04:43:36 +00:00
|
|
|
type HandleExtractor = (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> Handle
|
2020-06-04 19:36:34 +00:00
|
|
|
|
2012-07-19 04:43:36 +00:00
|
|
|
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"
|
|
|
|
|
2014-04-12 19:59:34 +00:00
|
|
|
processHandle :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> ProcessHandle
|
|
|
|
processHandle (_, _, _, pid) = pid
|
|
|
|
|
2015-05-30 15:27:40 +00:00
|
|
|
-- | Shows the command that a CreateProcess will run.
|
2012-07-19 04:43:36 +00:00
|
|
|
showCmd :: CreateProcess -> String
|
2019-09-09 19:21:24 +00:00
|
|
|
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
|
|
|
|
2015-05-30 15:27:40 +00:00
|
|
|
-- | Starts an interactive process. Unlike runInteractiveProcess in
|
|
|
|
-- System.Process, stderr is inherited.
|
2013-06-14 21:35:45 +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)
|
2013-07-08 18:51:43 +00:00
|
|
|
|
2015-08-13 17:12:44 +00:00
|
|
|
-- | Wrapper around 'System.Process.createProcess' that does debug logging.
|
2013-07-08 18:51:43 +00:00
|
|
|
createProcess :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
|
|
|
|
createProcess p = do
|
2020-09-24 16:39:57 +00:00
|
|
|
r@(_, _, _, h) <- Utility.Process.Shim.createProcess p
|
|
|
|
debugProcess p h
|
|
|
|
return r
|
2015-08-13 17:12:44 +00:00
|
|
|
|
2020-06-11 20:43:24 +00:00
|
|
|
-- | Wrapper around 'System.Process.withCreateProcess' that does debug logging.
|
|
|
|
withCreateProcess :: CreateProcess -> (Maybe Handle -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a) -> IO a
|
|
|
|
withCreateProcess p action = bracket (createProcess p) cleanupProcess
|
|
|
|
(\(m_in, m_out, m_err, ph) -> action m_in m_out m_err ph)
|
|
|
|
|
2015-08-13 17:12:44 +00:00
|
|
|
-- | Debugging trace for a CreateProcess.
|
2020-09-24 16:39:57 +00:00
|
|
|
debugProcess :: CreateProcess -> ProcessHandle -> IO ()
|
|
|
|
debugProcess p h = do
|
|
|
|
pid <- getPid h
|
2023-05-15 19:35:29 +00:00
|
|
|
debug "Utility.Process" $ unwords $
|
2020-09-24 16:39:57 +00:00
|
|
|
[ describePid pid
|
|
|
|
, action ++ ":"
|
|
|
|
, showCmd p
|
2023-05-15 19:35:29 +00:00
|
|
|
] ++ case cwd p of
|
|
|
|
Nothing -> []
|
|
|
|
Just c -> ["in", show c]
|
2015-08-13 17:12:44 +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
|
|
|
|
|
2020-09-24 16:39:57 +00:00
|
|
|
describePid :: Maybe Utility.Process.Shim.Pid -> String
|
|
|
|
describePid Nothing = "process"
|
|
|
|
describePid (Just p) = "process [" ++ show p ++ "]"
|
|
|
|
|
2015-08-13 17:12:44 +00:00
|
|
|
-- | Wrapper around 'System.Process.waitForProcess' that does debug logging.
|
|
|
|
waitForProcess :: ProcessHandle -> IO ExitCode
|
|
|
|
waitForProcess h = do
|
2020-09-24 16:39:57 +00:00
|
|
|
-- Have to get pid before waiting, which closes the ProcessHandle.
|
|
|
|
pid <- getPid h
|
2015-10-28 04:18:01 +00:00
|
|
|
r <- Utility.Process.Shim.waitForProcess h
|
2021-04-05 17:40:31 +00:00
|
|
|
debug "Utility.Process" (describePid pid ++ " done " ++ show r)
|
2015-08-13 17:12:44 +00:00
|
|
|
return r
|
2020-06-05 15:40:18 +00:00
|
|
|
|
|
|
|
cleanupProcess :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
|
|
|
|
#if MIN_VERSION_process(1,6,4)
|
|
|
|
cleanupProcess = Utility.Process.Shim.cleanupProcess
|
|
|
|
#else
|
|
|
|
cleanupProcess (mb_stdin, mb_stdout, mb_stderr, pid) = do
|
|
|
|
-- Unlike the real cleanupProcess, this does not wait
|
|
|
|
-- for the process to finish in the background, so if
|
|
|
|
-- the process ignores SIGTERM, this can block until the process
|
|
|
|
-- gets around the exiting.
|
|
|
|
terminateProcess pid
|
|
|
|
let void _ = return ()
|
|
|
|
maybe (return ()) (void . tryNonAsync . hClose) mb_stdin
|
|
|
|
maybe (return ()) hClose mb_stdout
|
|
|
|
maybe (return ()) hClose mb_stderr
|
|
|
|
void $ waitForProcess pid
|
|
|
|
#endif
|
2020-11-17 21:31:08 +00:00
|
|
|
|
2020-11-18 18:54:02 +00:00
|
|
|
{- | Like hGetLine, reads a line from the Handle. Returns Nothing if end of
|
2020-11-18 19:10:35 +00:00
|
|
|
- file is reached, or the handle is closed, or if the process has exited
|
|
|
|
- and there is nothing more buffered to read from the handle.
|
2020-11-17 21:31:08 +00:00
|
|
|
-
|
|
|
|
- This is useful to protect against situations where the process might
|
|
|
|
- have transferred the handle being read to another process, and so
|
|
|
|
- the handle could remain open after the process has exited. That is a rare
|
|
|
|
- situation, but can happen. Consider a the process that started up a
|
|
|
|
- daemon, and the daemon inherited stderr from it, rather than the more
|
|
|
|
- usual behavior of closing the file descriptor. Reading from stderr
|
|
|
|
- would block past the exit of the process.
|
|
|
|
-
|
|
|
|
- In that situation, this will detect when the process has exited,
|
|
|
|
- and avoid blocking forever. But will still return anything the process
|
|
|
|
- buffered to the handle before exiting.
|
2020-11-18 18:48:50 +00:00
|
|
|
-
|
|
|
|
- Note on newline mode: This ignores whatever newline mode is configured
|
|
|
|
- for the handle, because there is no way to query that. On Windows,
|
|
|
|
- it will remove any \r coming before the \n. On other platforms,
|
|
|
|
- it does not treat \r specially.
|
2020-11-17 21:31:08 +00:00
|
|
|
-}
|
|
|
|
hGetLineUntilExitOrEOF :: ProcessHandle -> Handle -> IO (Maybe String)
|
2020-11-18 18:21:47 +00:00
|
|
|
hGetLineUntilExitOrEOF ph h = go []
|
2020-11-17 21:31:08 +00:00
|
|
|
where
|
2020-11-18 18:21:47 +00:00
|
|
|
go buf = do
|
|
|
|
ready <- waitforinputorerror smalldelay
|
|
|
|
if ready
|
|
|
|
then getloop buf go
|
|
|
|
else getProcessExitCode ph >>= \case
|
|
|
|
-- Process still running, wait longer.
|
|
|
|
Nothing -> go buf
|
|
|
|
-- Process is done. It's possible
|
|
|
|
-- that it output something and exited
|
|
|
|
-- since the prior hWaitForInput,
|
|
|
|
-- so check one more time for any buffered
|
|
|
|
-- output.
|
|
|
|
Just _ -> finalcheck buf
|
|
|
|
|
|
|
|
finalcheck buf = do
|
2020-11-18 19:03:30 +00:00
|
|
|
ready <- waitforinputorerror 0
|
2020-11-18 18:21:47 +00:00
|
|
|
if ready
|
|
|
|
then getloop buf finalcheck
|
|
|
|
-- No remaining buffered input, though the handle
|
|
|
|
-- may not be EOF if something else is keeping it
|
|
|
|
-- open. Treated the same as EOF.
|
|
|
|
else eofwithnolineend buf
|
|
|
|
|
|
|
|
-- On exception, proceed as if there was input;
|
|
|
|
-- EOF and any encoding issues are dealt with
|
|
|
|
-- when reading from the handle.
|
|
|
|
waitforinputorerror t = hWaitForInput h t
|
|
|
|
`catchNonAsync` const (pure True)
|
|
|
|
|
2020-11-18 19:10:35 +00:00
|
|
|
getchar =
|
|
|
|
catcherr EOF $
|
|
|
|
-- If the handle is closed, reading from it is
|
|
|
|
-- an IllegalOperation.
|
|
|
|
catcherr IllegalOperation $
|
|
|
|
Just <$> hGetChar h
|
|
|
|
where
|
|
|
|
catcherr t = catchIOErrorType t (const (pure Nothing))
|
2020-11-18 18:21:47 +00:00
|
|
|
|
|
|
|
getloop buf cont =
|
|
|
|
getchar >>= \case
|
|
|
|
Just c
|
2020-11-18 18:48:50 +00:00
|
|
|
| c == '\n' -> return (Just (gotline buf))
|
2020-11-18 18:21:47 +00:00
|
|
|
| otherwise -> cont (c:buf)
|
|
|
|
Nothing -> eofwithnolineend buf
|
|
|
|
|
2020-11-18 18:48:50 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
gotline buf = reverse buf
|
|
|
|
#else
|
|
|
|
gotline ('\r':buf) = reverse buf
|
|
|
|
gotline buf = reverse buf
|
|
|
|
#endif
|
|
|
|
|
2020-11-18 18:21:47 +00:00
|
|
|
eofwithnolineend buf = return $
|
|
|
|
if null buf
|
|
|
|
then Nothing -- no line read
|
|
|
|
else Just (reverse buf)
|
|
|
|
|
|
|
|
-- Tenth of a second delay. If the process exits with the FD being
|
|
|
|
-- held open, will wait up to twice this long before returning.
|
|
|
|
-- This delay could be made smaller. However, that is an unusual
|
|
|
|
-- case, and making it too small would cause lots of wakeups while
|
|
|
|
-- waiting for output. Bearing in mind that this could be run on
|
|
|
|
-- many processes at the same time.
|
|
|
|
smalldelay = 100 -- milliseconds
|