| 1 |
{-# LANGUAGE CPP #-} |
| 2 |
{-# LANGUAGE TypeFamilies #-} |
| 3 |
{-# LANGUAGE TypeOperators #-} |
| 4 |
{-# LANGUAGE DeriveDataTypeable #-} |
| 5 |
{-# LANGUAGE RecordWildCards #-} |
| 6 |
{-# LANGUAGE DataKinds #-} |
| 7 |
{-# LANGUAGE DeriveFunctor #-} |
| 8 |
{-# LANGUAGE RankNTypes #-} |
| 9 |
{-# LANGUAGE ScopedTypeVariables #-} |
| 10 |
-- | This module is __internal__ and its contents may change without a warning |
| 11 |
-- or announcement. It is not subject to the PVP. |
| 12 |
module System.Process.Typed.Internal where |
| 13 |
|
| 14 |
import qualified Data.ByteString as S |
| 15 |
import Data.ByteString.Lazy.Internal (defaultChunkSize) |
| 16 |
import qualified Control.Exception as E |
| 17 |
import Control.Exception hiding (bracket, finally, handle) |
| 18 |
import Control.Monad (void) |
| 19 |
import qualified System.Process as P |
| 20 |
import Data.Typeable (Typeable) |
| 21 |
import System.IO (Handle, hClose, IOMode(ReadWriteMode), withBinaryFile) |
| 22 |
import Control.Concurrent.Async (async) |
| 23 |
import Control.Concurrent.STM (newEmptyTMVarIO, atomically, putTMVar, readTMVar, STM, tryPutTMVar, throwSTM) |
| 24 |
import System.Exit (ExitCode) |
| 25 |
import qualified Data.ByteString.Lazy as L |
| 26 |
import Data.String (IsString (fromString)) |
| 27 |
import Control.Monad.IO.Unlift |
| 28 |
import qualified Data.Text.Encoding.Error as TEE |
| 29 |
import qualified Data.Text.Lazy as TL |
| 30 |
import qualified Data.Text.Lazy.Encoding as TLE |
| 31 |
|
| 32 |
#if MIN_VERSION_process(1, 4, 0) && !WINDOWS |
| 33 |
import System.Posix.Types (GroupID, UserID) |
| 34 |
#endif |
| 35 |
|
| 36 |
#if !MIN_VERSION_base(4, 8, 0) |
| 37 |
import Control.Applicative (Applicative (..), (<$>), (<$)) |
| 38 |
#endif |
| 39 |
|
| 40 |
#if !MIN_VERSION_process(1, 3, 0) |
| 41 |
import qualified System.Process.Internals as P (createProcess_) |
| 42 |
#endif |
| 43 |
|
| 44 |
-- | An abstract configuration for a process, which can then be |
| 45 |
-- launched into an actual running 'Process'. Takes three type |
| 46 |
-- parameters, providing the types of standard input, standard output, |
| 47 |
-- and standard error, respectively. |
| 48 |
-- |
| 49 |
-- There are three ways to construct a value of this type: |
| 50 |
-- |
| 51 |
-- * With the 'proc' smart constructor, which takes a command name and |
| 52 |
-- a list of arguments. |
| 53 |
-- |
| 54 |
-- * With the 'shell' smart constructor, which takes a shell string |
| 55 |
-- |
| 56 |
-- * With the 'IsString' instance via OverloadedStrings. If you |
| 57 |
-- provide it a string with no spaces (e.g., @"date"@), it will |
| 58 |
-- treat it as a raw command with no arguments (e.g., @proc "date" |
| 59 |
-- []@). If it has spaces, it will use @shell@. |
| 60 |
-- |
| 61 |
-- In all cases, the default for all three streams is to inherit the |
| 62 |
-- streams from the parent process. For other settings, see the |
| 63 |
-- [setters below](#processconfigsetters) for default values. |
| 64 |
-- |
| 65 |
-- Once you have a @ProcessConfig@ you can launch a process from it |
| 66 |
-- using the functions in the section [Launch a |
| 67 |
-- process](#launchaprocess). |
| 68 |
-- |
| 69 |
-- @since 0.1.0.0 |
| 70 |
data ProcessConfig stdin stdout stderr = ProcessConfig |
| 71 |
{ pcCmdSpec :: !P.CmdSpec |
| 72 |
, pcStdin :: !(StreamSpec 'STInput stdin) |
| 73 |
, pcStdout :: !(StreamSpec 'STOutput stdout) |
| 74 |
, pcStderr :: !(StreamSpec 'STOutput stderr) |
| 75 |
, pcWorkingDir :: !(Maybe FilePath) |
| 76 |
, pcEnv :: !(Maybe [(String, String)]) |
| 77 |
, pcCloseFds :: !Bool |
| 78 |
, pcCreateGroup :: !Bool |
| 79 |
, pcDelegateCtlc :: !Bool |
| 80 |
|
| 81 |
#if MIN_VERSION_process(1, 3, 0) |
| 82 |
, pcDetachConsole :: !Bool |
| 83 |
, pcCreateNewConsole :: !Bool |
| 84 |
, pcNewSession :: !Bool |
| 85 |
#endif |
| 86 |
|
| 87 |
#if MIN_VERSION_process(1, 4, 0) && !WINDOWS |
| 88 |
, pcChildGroup :: !(Maybe GroupID) |
| 89 |
, pcChildUser :: !(Maybe UserID) |
| 90 |
#endif |
| 91 |
} |
| 92 |
instance Show (ProcessConfig stdin stdout stderr) where |
| 93 |
show pc = concat |
| 94 |
[ case pcCmdSpec pc of |
| 95 |
P.ShellCommand s -> "Shell command: " ++ s |
| 96 |
P.RawCommand x xs -> "Raw command: " ++ unwords (map escape (x:xs)) |
| 97 |
, "\n" |
| 98 |
, case pcWorkingDir pc of |
| 99 |
Nothing -> "" |
| 100 |
Just wd -> concat |
| 101 |
[ "Run from: " |
| 102 |
, wd |
| 103 |
, "\n" |
| 104 |
] |
| 105 |
, case pcEnv pc of |
| 106 |
Nothing -> "" |
| 107 |
Just e -> unlines |
| 108 |
$ "Modified environment:" |
| 109 |
: map (\(k, v) -> concat [k, "=", v]) e |
| 110 |
] |
| 111 |
where |
| 112 |
escape x |
| 113 |
| any (`elem` " \\\"'") x = show x |
| 114 |
| x == "" = "\"\"" |
| 115 |
| otherwise = x |
| 116 |
instance (stdin ~ (), stdout ~ (), stderr ~ ()) |
| 117 |
=> IsString (ProcessConfig stdin stdout stderr) where |
| 118 |
fromString s |
| 119 |
| any (== ' ') s = shell s |
| 120 |
| otherwise = proc s [] |
| 121 |
|
| 122 |
-- | Whether a stream is an input stream or output stream. Note that |
| 123 |
-- this is from the perspective of the /child process/, so that a |
| 124 |
-- child's standard input stream is an @STInput@, even though the |
| 125 |
-- parent process will be writing to it. |
| 126 |
-- |
| 127 |
-- @since 0.1.0.0 |
| 128 |
data StreamType = STInput | STOutput |
| 129 |
|
| 130 |
-- | A specification for how to create one of the three standard child |
| 131 |
-- streams, @stdin@, @stdout@ and @stderr@. A 'StreamSpec' can be |
| 132 |
-- thought of as containing |
| 133 |
-- |
| 134 |
-- 1. A type safe version of 'P.StdStream' from "System.Process". |
| 135 |
-- This determines whether the stream should be inherited from the |
| 136 |
-- parent process, piped to or from a 'Handle', etc. |
| 137 |
-- |
| 138 |
-- 2. A means of accessing the stream as a value of type @a@ |
| 139 |
-- |
| 140 |
-- 3. A cleanup action which will be run on the stream once the |
| 141 |
-- process terminates |
| 142 |
-- |
| 143 |
-- To create a @StreamSpec@ see the section [Stream |
| 144 |
-- specs](#streamspecs). |
| 145 |
-- |
| 146 |
-- @since 0.1.0.0 |
| 147 |
data StreamSpec (streamType :: StreamType) a = StreamSpec |
| 148 |
{ ssStream :: !(forall b. (P.StdStream -> IO b) -> IO b) |
| 149 |
, ssCreate :: !(ProcessConfig () () () -> Maybe Handle -> Cleanup a) |
| 150 |
} |
| 151 |
deriving Functor |
| 152 |
|
| 153 |
-- | This instance uses 'byteStringInput' to convert a raw string into |
| 154 |
-- a stream of input for a child process. |
| 155 |
-- |
| 156 |
-- @since 0.1.0.0 |
| 157 |
instance (streamType ~ 'STInput, res ~ ()) |
| 158 |
=> IsString (StreamSpec streamType res) where |
| 159 |
fromString = byteStringInput . fromString |
| 160 |
|
| 161 |
-- | Internal type, to make for easier composition of cleanup actions. |
| 162 |
-- |
| 163 |
-- @since 0.1.0.0 |
| 164 |
newtype Cleanup a = Cleanup { runCleanup :: IO (a, IO ()) } |
| 165 |
deriving Functor |
| 166 |
instance Applicative Cleanup where |
| 167 |
pure x = Cleanup (return (x, return ())) |
| 168 |
Cleanup f <*> Cleanup x = Cleanup $ do |
| 169 |
(f', c1) <- f |
| 170 |
(`onException` c1) $ do |
| 171 |
(x', c2) <- x |
| 172 |
return (f' x', c1 `finally` c2) |
| 173 |
|
| 174 |
-- | Internal helper |
| 175 |
defaultProcessConfig :: ProcessConfig () () () |
| 176 |
defaultProcessConfig = ProcessConfig |
| 177 |
{ pcCmdSpec = P.ShellCommand "" |
| 178 |
, pcStdin = inherit |
| 179 |
, pcStdout = inherit |
| 180 |
, pcStderr = inherit |
| 181 |
, pcWorkingDir = Nothing |
| 182 |
, pcEnv = Nothing |
| 183 |
, pcCloseFds = False |
| 184 |
, pcCreateGroup = False |
| 185 |
, pcDelegateCtlc = False |
| 186 |
|
| 187 |
#if MIN_VERSION_process(1, 3, 0) |
| 188 |
, pcDetachConsole = False |
| 189 |
, pcCreateNewConsole = False |
| 190 |
, pcNewSession = False |
| 191 |
#endif |
| 192 |
|
| 193 |
#if MIN_VERSION_process(1, 4, 0) && !WINDOWS |
| 194 |
, pcChildGroup = Nothing |
| 195 |
, pcChildUser = Nothing |
| 196 |
#endif |
| 197 |
} |
| 198 |
|
| 199 |
-- | Create a 'ProcessConfig' from the given command and arguments. |
| 200 |
-- |
| 201 |
-- @since 0.1.0.0 |
| 202 |
proc :: FilePath -> [String] -> ProcessConfig () () () |
| 203 |
proc cmd args = setProc cmd args defaultProcessConfig |
| 204 |
|
| 205 |
-- | Internal helper |
| 206 |
setProc :: FilePath -> [String] |
| 207 |
-> ProcessConfig stdin stdout stderr |
| 208 |
-> ProcessConfig stdin stdout stderr |
| 209 |
setProc cmd args p = p { pcCmdSpec = P.RawCommand cmd args } |
| 210 |
|
| 211 |
-- | Create a 'ProcessConfig' from the given shell command. |
| 212 |
-- |
| 213 |
-- @since 0.1.0.0 |
| 214 |
shell :: String -> ProcessConfig () () () |
| 215 |
shell cmd = setShell cmd defaultProcessConfig |
| 216 |
|
| 217 |
-- | Internal helper |
| 218 |
setShell :: String |
| 219 |
-> ProcessConfig stdin stdout stderr |
| 220 |
-> ProcessConfig stdin stdout stderr |
| 221 |
setShell cmd p = p { pcCmdSpec = P.ShellCommand cmd } |
| 222 |
|
| 223 |
-- | Set the child's standard input stream to the given 'StreamSpec'. |
| 224 |
-- |
| 225 |
-- Default: 'inherit' |
| 226 |
-- |
| 227 |
-- @since 0.1.0.0 |
| 228 |
setStdin :: StreamSpec 'STInput stdin |
| 229 |
-- ^ |
| 230 |
-> ProcessConfig stdin0 stdout stderr |
| 231 |
-- ^ |
| 232 |
-> ProcessConfig stdin stdout stderr |
| 233 |
setStdin spec pc = pc { pcStdin = spec } |
| 234 |
|
| 235 |
-- | Set the child's standard output stream to the given 'StreamSpec'. |
| 236 |
-- |
| 237 |
-- Default: 'inherit' |
| 238 |
-- |
| 239 |
-- @since 0.1.0.0 |
| 240 |
setStdout :: StreamSpec 'STOutput stdout |
| 241 |
-- ^ |
| 242 |
-> ProcessConfig stdin stdout0 stderr |
| 243 |
-- ^ |
| 244 |
-> ProcessConfig stdin stdout stderr |
| 245 |
setStdout spec pc = pc { pcStdout = spec } |
| 246 |
|
| 247 |
-- | Set the child's standard error stream to the given 'StreamSpec'. |
| 248 |
-- |
| 249 |
-- Default: 'inherit' |
| 250 |
-- |
| 251 |
-- @since 0.1.0.0 |
| 252 |
setStderr :: StreamSpec 'STOutput stderr |
| 253 |
-- ^ |
| 254 |
-> ProcessConfig stdin stdout stderr0 |
| 255 |
-- ^ |
| 256 |
-> ProcessConfig stdin stdout stderr |
| 257 |
setStderr spec pc = pc { pcStderr = spec } |
| 258 |
|
| 259 |
-- | Set the working directory of the child process. |
| 260 |
-- |
| 261 |
-- Default: current process's working directory. |
| 262 |
-- |
| 263 |
-- @since 0.1.0.0 |
| 264 |
setWorkingDir :: FilePath |
| 265 |
-- ^ |
| 266 |
-> ProcessConfig stdin stdout stderr |
| 267 |
-- ^ |
| 268 |
-> ProcessConfig stdin stdout stderr |
| 269 |
setWorkingDir dir pc = pc { pcWorkingDir = Just dir } |
| 270 |
|
| 271 |
-- | Inherit the working directory from the parent process. |
| 272 |
-- |
| 273 |
-- @since 0.2.2.0 |
| 274 |
setWorkingDirInherit |
| 275 |
:: ProcessConfig stdin stdout stderr |
| 276 |
-- ^ |
| 277 |
-> ProcessConfig stdin stdout stderr |
| 278 |
setWorkingDirInherit pc = pc { pcWorkingDir = Nothing } |
| 279 |
|
| 280 |
-- | Set the environment variables of the child process. |
| 281 |
-- |
| 282 |
-- Default: current process's environment. |
| 283 |
-- |
| 284 |
-- @since 0.1.0.0 |
| 285 |
setEnv :: [(String, String)] |
| 286 |
-- ^ |
| 287 |
-> ProcessConfig stdin stdout stderr |
| 288 |
-- ^ |
| 289 |
-> ProcessConfig stdin stdout stderr |
| 290 |
setEnv env pc = pc { pcEnv = Just env } |
| 291 |
|
| 292 |
-- | Inherit the environment variables from the parent process. |
| 293 |
-- |
| 294 |
-- @since 0.2.2.0 |
| 295 |
setEnvInherit |
| 296 |
:: ProcessConfig stdin stdout stderr |
| 297 |
-- ^ |
| 298 |
-> ProcessConfig stdin stdout stderr |
| 299 |
setEnvInherit pc = pc { pcEnv = Nothing } |
| 300 |
|
| 301 |
-- | Should we close all file descriptors besides stdin, stdout, and |
| 302 |
-- stderr? See 'P.close_fds' for more information. |
| 303 |
-- |
| 304 |
-- Default: False |
| 305 |
-- |
| 306 |
-- @since 0.1.0.0 |
| 307 |
setCloseFds |
| 308 |
:: Bool |
| 309 |
-- ^ |
| 310 |
-> ProcessConfig stdin stdout stderr |
| 311 |
-- ^ |
| 312 |
-> ProcessConfig stdin stdout stderr |
| 313 |
setCloseFds x pc = pc { pcCloseFds = x } |
| 314 |
|
| 315 |
-- | Should we create a new process group? |
| 316 |
-- |
| 317 |
-- Default: False |
| 318 |
-- |
| 319 |
-- @since 0.1.0.0 |
| 320 |
setCreateGroup |
| 321 |
:: Bool |
| 322 |
-- ^ |
| 323 |
-> ProcessConfig stdin stdout stderr |
| 324 |
-- ^ |
| 325 |
-> ProcessConfig stdin stdout stderr |
| 326 |
setCreateGroup x pc = pc { pcCreateGroup = x } |
| 327 |
|
| 328 |
-- | Delegate handling of Ctrl-C to the child. For more information, |
| 329 |
-- see 'P.delegate_ctlc'. |
| 330 |
-- |
| 331 |
-- Default: False |
| 332 |
-- |
| 333 |
-- @since 0.1.0.0 |
| 334 |
setDelegateCtlc |
| 335 |
:: Bool |
| 336 |
-- ^ |
| 337 |
-> ProcessConfig stdin stdout stderr |
| 338 |
-- ^ |
| 339 |
-> ProcessConfig stdin stdout stderr |
| 340 |
setDelegateCtlc x pc = pc { pcDelegateCtlc = x } |
| 341 |
|
| 342 |
#if MIN_VERSION_process(1, 3, 0) |
| 343 |
|
| 344 |
-- | Detach console on Windows, see 'P.detach_console'. |
| 345 |
-- |
| 346 |
-- Default: False |
| 347 |
-- |
| 348 |
-- @since 0.1.0.0 |
| 349 |
setDetachConsole |
| 350 |
:: Bool |
| 351 |
-- ^ |
| 352 |
-> ProcessConfig stdin stdout stderr |
| 353 |
-- ^ |
| 354 |
-> ProcessConfig stdin stdout stderr |
| 355 |
setDetachConsole x pc = pc { pcDetachConsole = x } |
| 356 |
|
| 357 |
-- | Create new console on Windows, see 'P.create_new_console'. |
| 358 |
-- |
| 359 |
-- Default: False |
| 360 |
-- |
| 361 |
-- @since 0.1.0.0 |
| 362 |
setCreateNewConsole |
| 363 |
:: Bool |
| 364 |
-- ^ |
| 365 |
-> ProcessConfig stdin stdout stderr |
| 366 |
-- ^ |
| 367 |
-> ProcessConfig stdin stdout stderr |
| 368 |
setCreateNewConsole x pc = pc { pcCreateNewConsole = x } |
| 369 |
|
| 370 |
-- | Set a new session with the POSIX @setsid@ syscall, does nothing |
| 371 |
-- on non-POSIX. See 'P.new_session'. |
| 372 |
-- |
| 373 |
-- Default: False |
| 374 |
-- |
| 375 |
-- @since 0.1.0.0 |
| 376 |
setNewSession |
| 377 |
:: Bool |
| 378 |
-- ^ |
| 379 |
-> ProcessConfig stdin stdout stderr |
| 380 |
-- ^ |
| 381 |
-> ProcessConfig stdin stdout stderr |
| 382 |
setNewSession x pc = pc { pcNewSession = x } |
| 383 |
#endif |
| 384 |
|
| 385 |
#if MIN_VERSION_process(1, 4, 0) && !WINDOWS |
| 386 |
-- | Set the child process's group ID with the POSIX @setgid@ syscall, |
| 387 |
-- does nothing on non-POSIX. See 'P.child_group'. |
| 388 |
-- |
| 389 |
-- Default: False |
| 390 |
-- |
| 391 |
-- @since 0.1.0.0 |
| 392 |
setChildGroup |
| 393 |
:: GroupID |
| 394 |
-- ^ |
| 395 |
-> ProcessConfig stdin stdout stderr |
| 396 |
-- ^ |
| 397 |
-> ProcessConfig stdin stdout stderr |
| 398 |
setChildGroup x pc = pc { pcChildGroup = Just x } |
| 399 |
|
| 400 |
-- | Inherit the group from the parent process. |
| 401 |
-- |
| 402 |
-- @since 0.2.2.0 |
| 403 |
setChildGroupInherit |
| 404 |
:: ProcessConfig stdin stdout stderr |
| 405 |
-- ^ |
| 406 |
-> ProcessConfig stdin stdout stderr |
| 407 |
setChildGroupInherit pc = pc { pcChildGroup = Nothing } |
| 408 |
|
| 409 |
-- | Set the child process's user ID with the POSIX @setuid@ syscall, |
| 410 |
-- does nothing on non-POSIX. See 'P.child_user'. |
| 411 |
-- |
| 412 |
-- Default: False |
| 413 |
-- |
| 414 |
-- @since 0.1.0.0 |
| 415 |
setChildUser |
| 416 |
:: UserID |
| 417 |
-- ^ |
| 418 |
-> ProcessConfig stdin stdout stderr |
| 419 |
-- ^ |
| 420 |
-> ProcessConfig stdin stdout stderr |
| 421 |
setChildUser x pc = pc { pcChildUser = Just x } |
| 422 |
|
| 423 |
-- | Inherit the user from the parent process. |
| 424 |
-- |
| 425 |
-- @since 0.2.2.0 |
| 426 |
setChildUserInherit |
| 427 |
:: ProcessConfig stdin stdout stderr |
| 428 |
-- ^ |
| 429 |
-> ProcessConfig stdin stdout stderr |
| 430 |
setChildUserInherit pc = pc { pcChildUser = Nothing } |
| 431 |
#endif |
| 432 |
|
| 433 |
-- | Create a new 'StreamSpec' from the given 'P.StdStream' and a |
| 434 |
-- helper function. This function: |
| 435 |
-- |
| 436 |
-- * Takes as input the raw @Maybe Handle@ returned by the |
| 437 |
-- 'P.createProcess' function. The handle will be @Just@ 'Handle' if the |
| 438 |
-- 'P.StdStream' argument is 'P.CreatePipe' and @Nothing@ otherwise. |
| 439 |
-- See 'P.createProcess' for more details. |
| 440 |
-- |
| 441 |
-- * Returns the actual stream value @a@, as well as a cleanup |
| 442 |
-- function to be run when calling 'stopProcess'. |
| 443 |
-- |
| 444 |
-- If making a 'StreamSpec' with 'P.CreatePipe', prefer 'mkPipeStreamSpec', |
| 445 |
-- which encodes the invariant that a 'Handle' is created. |
| 446 |
-- |
| 447 |
-- @since 0.1.0.0 |
| 448 |
mkStreamSpec :: P.StdStream |
| 449 |
-- ^ |
| 450 |
-> (ProcessConfig () () () -> Maybe Handle -> IO (a, IO ())) |
| 451 |
-- ^ |
| 452 |
-> StreamSpec streamType a |
| 453 |
mkStreamSpec ss f = mkManagedStreamSpec ($ ss) f |
| 454 |
|
| 455 |
-- | Create a new 'P.CreatePipe' 'StreamSpec' from the given function. |
| 456 |
-- This function: |
| 457 |
-- |
| 458 |
-- * Takes as input the @Handle@ returned by the 'P.createProcess' function. |
| 459 |
-- See 'P.createProcess' for more details. |
| 460 |
-- |
| 461 |
-- * Returns the actual stream value @a@, as well as a cleanup |
| 462 |
-- function to be run when calling 'stopProcess'. |
| 463 |
-- |
| 464 |
-- @since 0.2.10.0 |
| 465 |
mkPipeStreamSpec :: (ProcessConfig () () () -> Handle -> IO (a, IO ())) |
| 466 |
-- ^ |
| 467 |
-> StreamSpec streamType a |
| 468 |
-- ^ |
| 469 |
mkPipeStreamSpec f = mkStreamSpec P.CreatePipe $ \pc mh -> |
| 470 |
case mh of |
| 471 |
Just h -> f pc h |
| 472 |
Nothing -> error "Invariant violation: making StreamSpec with CreatePipe unexpectedly did not return a Handle" |
| 473 |
|
| 474 |
-- | Create a new 'StreamSpec' from a function that accepts a |
| 475 |
-- 'P.StdStream' and a helper function. This function is the same as |
| 476 |
-- the helper in 'mkStreamSpec' |
| 477 |
mkManagedStreamSpec :: (forall b. (P.StdStream -> IO b) -> IO b) |
| 478 |
-- ^ |
| 479 |
-> (ProcessConfig () () () -> Maybe Handle -> IO (a, IO ())) |
| 480 |
-- ^ |
| 481 |
-> StreamSpec streamType a |
| 482 |
mkManagedStreamSpec ss f = StreamSpec ss (\pc mh -> Cleanup (f pc mh)) |
| 483 |
|
| 484 |
-- | A stream spec which simply inherits the stream of the parent |
| 485 |
-- process. |
| 486 |
-- |
| 487 |
-- @since 0.1.0.0 |
| 488 |
inherit :: StreamSpec anyStreamType () |
| 489 |
inherit = mkStreamSpec P.Inherit (\_ _ -> pure ((), return ())) |
| 490 |
|
| 491 |
-- | A stream spec which is empty when used for for input and discards |
| 492 |
-- output. Note this requires your platform's null device to be |
| 493 |
-- available when the process is started. |
| 494 |
-- |
| 495 |
-- @since 0.2.5.0 |
| 496 |
nullStream :: StreamSpec anyStreamType () |
| 497 |
nullStream = mkManagedStreamSpec opener cleanup |
| 498 |
where |
| 499 |
opener f = |
| 500 |
withBinaryFile nullDevice ReadWriteMode $ \handle -> |
| 501 |
f (P.UseHandle handle) |
| 502 |
cleanup _ _ = |
| 503 |
pure ((), return ()) |
| 504 |
|
| 505 |
-- | A stream spec which will close the stream for the child process. |
| 506 |
-- You usually do not want to use this, as it will leave the |
| 507 |
-- corresponding file descriptor unassigned and hence available for |
| 508 |
-- re-use in the child process. Prefer 'nullStream' unless you're |
| 509 |
-- certain you want this behavior. |
| 510 |
-- |
| 511 |
-- @since 0.1.0.0 |
| 512 |
closed :: StreamSpec anyStreamType () |
| 513 |
#if MIN_VERSION_process(1, 4, 0) |
| 514 |
closed = mkStreamSpec P.NoStream (\_ _ -> pure ((), return ())) |
| 515 |
#else |
| 516 |
closed = mkPipeStreamSpec (\_ h -> ((), return ()) <$ hClose h) |
| 517 |
#endif |
| 518 |
|
| 519 |
-- | An input stream spec which sets the input to the given |
| 520 |
-- 'L.ByteString'. A separate thread will be forked to write the |
| 521 |
-- contents to the child process. |
| 522 |
-- |
| 523 |
-- @since 0.1.0.0 |
| 524 |
byteStringInput :: L.ByteString -> StreamSpec 'STInput () |
| 525 |
byteStringInput lbs = mkPipeStreamSpec $ \_ h -> do |
| 526 |
void $ async $ do |
| 527 |
L.hPut h lbs |
| 528 |
hClose h |
| 529 |
return ((), hClose h) |
| 530 |
|
| 531 |
-- | Capture the output of a process in a 'L.ByteString'. |
| 532 |
-- |
| 533 |
-- This function will fork a separate thread to consume all input from |
| 534 |
-- the process, and will only make the results available when the |
| 535 |
-- underlying 'Handle' is closed. As this is provided as an 'STM' |
| 536 |
-- action, you can either check if the result is available, or block |
| 537 |
-- until it's ready. |
| 538 |
-- |
| 539 |
-- In the event of any exception occurring when reading from the |
| 540 |
-- 'Handle', the 'STM' action will throw a |
| 541 |
-- 'ByteStringOutputException'. |
| 542 |
-- |
| 543 |
-- @since 0.1.0.0 |
| 544 |
byteStringOutput :: StreamSpec 'STOutput (STM L.ByteString) |
| 545 |
byteStringOutput = mkPipeStreamSpec $ \pc h -> byteStringFromHandle pc h |
| 546 |
|
| 547 |
-- | Helper function (not exposed) for both 'byteStringOutput' and |
| 548 |
-- 'withProcessInterleave'. This will consume all of the output from |
| 549 |
-- the given 'Handle' in a separate thread and provide access to the |
| 550 |
-- resulting 'L.ByteString' via STM. Second action will close the |
| 551 |
-- reader handle. |
| 552 |
byteStringFromHandle |
| 553 |
:: ProcessConfig () () () |
| 554 |
-> Handle -- ^ reader handle |
| 555 |
-> IO (STM L.ByteString, IO ()) |
| 556 |
byteStringFromHandle pc h = do |
| 557 |
mvar <- newEmptyTMVarIO |
| 558 |
|
| 559 |
void $ async $ do |
| 560 |
let loop front = do |
| 561 |
bs <- S.hGetSome h defaultChunkSize |
| 562 |
if S.null bs |
| 563 |
then atomically $ putTMVar mvar $ Right $ L.fromChunks $ front [] |
| 564 |
else loop $ front . (bs:) |
| 565 |
loop id `catch` \e -> do |
| 566 |
atomically $ void $ tryPutTMVar mvar $ Left $ ByteStringOutputException e pc |
| 567 |
throwIO e |
| 568 |
|
| 569 |
return (readTMVar mvar >>= either throwSTM return, hClose h) |
| 570 |
|
| 571 |
-- | Create a new pipe between this process and the child, and return |
| 572 |
-- a 'Handle' to communicate with the child. |
| 573 |
-- |
| 574 |
-- @since 0.1.0.0 |
| 575 |
createPipe :: StreamSpec anyStreamType Handle |
| 576 |
createPipe = mkPipeStreamSpec $ \_ h -> return (h, hClose h) |
| 577 |
|
| 578 |
-- | Use the provided 'Handle' for the child process, and when the |
| 579 |
-- process exits, do /not/ close it. This is useful if, for example, |
| 580 |
-- you want to have multiple processes write to the same log file |
| 581 |
-- sequentially. |
| 582 |
-- |
| 583 |
-- @since 0.1.0.0 |
| 584 |
useHandleOpen :: Handle -> StreamSpec anyStreamType () |
| 585 |
useHandleOpen h = mkStreamSpec (P.UseHandle h) $ \_ _ -> return ((), return ()) |
| 586 |
|
| 587 |
-- | Use the provided 'Handle' for the child process, and when the |
| 588 |
-- process exits, close it. If you have no reason to keep the 'Handle' |
| 589 |
-- open, you should use this over 'useHandleOpen'. |
| 590 |
-- |
| 591 |
-- @since 0.1.0.0 |
| 592 |
useHandleClose :: Handle -> StreamSpec anyStreamType () |
| 593 |
useHandleClose h = mkStreamSpec (P.UseHandle h) $ \_ _ -> return ((), hClose h) |
| 594 |
|
| 595 |
-- | Exception thrown by 'System.Process.Typed.checkExitCode' in the event of a |
| 596 |
-- non-success exit code. Note that 'System.Process.Typed.checkExitCode' is |
| 597 |
-- called by other functions as well, like 'System.Process.Typed.runProcess_' |
| 598 |
-- or 'System.Process.Typed.readProcess_'. |
| 599 |
-- |
| 600 |
-- Note that several functions that throw an 'ExitCodeException' intentionally do not populate 'eceStdout' or 'eceStderr'. |
| 601 |
-- This prevents unbounded memory usage for large stdout and stderrs. |
| 602 |
-- |
| 603 |
-- Functions which do include 'eceStdout' or 'eceStderr' (like |
| 604 |
-- 'System.Process.Typed.readProcess_') state so in their documentation. |
| 605 |
-- |
| 606 |
-- @since 0.1.0.0 |
| 607 |
data ExitCodeException = ExitCodeException |
| 608 |
{ eceExitCode :: ExitCode |
| 609 |
, eceProcessConfig :: ProcessConfig () () () |
| 610 |
, eceStdout :: L.ByteString |
| 611 |
, eceStderr :: L.ByteString |
| 612 |
} |
| 613 |
deriving Typeable |
| 614 |
instance Exception ExitCodeException |
| 615 |
instance Show ExitCodeException where |
| 616 |
show ece = concat |
| 617 |
[ "Received " |
| 618 |
, show (eceExitCode ece) |
| 619 |
, " when running\n" |
| 620 |
-- Too much output for an exception if we show the modified |
| 621 |
-- environment, so hide it |
| 622 |
, show (eceProcessConfig ece) { pcEnv = Nothing } |
| 623 |
, if L.null (eceStdout ece) |
| 624 |
then "" |
| 625 |
else "Standard output:\n\n" ++ unpack (eceStdout ece) |
| 626 |
, if L.null (eceStderr ece) |
| 627 |
then "" |
| 628 |
else "Standard error:\n\n" ++ unpack (eceStderr ece) |
| 629 |
] |
| 630 |
where |
| 631 |
-- Format with UTF-8, because we have to choose some encoding, |
| 632 |
-- and UTF-8 is the least likely to be wrong in general. |
| 633 |
unpack = TL.unpack . TLE.decodeUtf8With TEE.lenientDecode |
| 634 |
|
| 635 |
-- | Wrapper for when an exception is thrown when reading from a child |
| 636 |
-- process, used by 'byteStringOutput'. |
| 637 |
-- |
| 638 |
-- @since 0.1.0.0 |
| 639 |
data ByteStringOutputException = ByteStringOutputException SomeException (ProcessConfig () () ()) |
| 640 |
deriving (Show, Typeable) |
| 641 |
instance Exception ByteStringOutputException |
| 642 |
|
| 643 |
bracket :: MonadUnliftIO m => IO a -> (a -> IO b) -> (a -> m c) -> m c |
| 644 |
bracket before after thing = withRunInIO $ \run -> E.bracket before after (run . thing) |
| 645 |
|
| 646 |
finally :: MonadUnliftIO m => m a -> IO () -> m a |
| 647 |
finally thing after = withRunInIO $ \run -> E.finally (run thing) after |
| 648 |
|
| 649 |
-- | The name of the system null device |
| 650 |
nullDevice :: FilePath |
| 651 |
#if WINDOWS |
| 652 |
nullDevice = "\\\\.\\NUL" |
| 653 |
#else |
| 654 |
nullDevice = "/dev/null" |
| 655 |
#endif |