Skip to content

Commit

Permalink
Add getPid
Browse files Browse the repository at this point in the history
This allows fetching a process' ID without using `unsafeProcessHandle`.
  • Loading branch information
9999years committed Aug 16, 2024
1 parent d5e9fb3 commit 07a9807
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/System/Process/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ module System.Process.Typed
, checkExitCode
, checkExitCodeSTM

-- ** Process ID
, getPid

-- ** Process streams
, getStdin
, getStdout
Expand All @@ -121,6 +124,7 @@ module System.Process.Typed
-- * Re-exports
, ExitCode (..)
, P.StdStream (..)
, P.Pid

-- * Unsafe functions
, unsafeProcessHandle
Expand All @@ -138,6 +142,7 @@ import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (asyncWithUnmask, cancel, waitCatch)
import Control.Concurrent.STM (newEmptyTMVarIO, atomically, putTMVar, TMVar, readTMVar, tryReadTMVar, STM, tryPutTMVar, throwSTM, catchSTM)
import System.Exit (ExitCode (ExitSuccess, ExitFailure))
import System.Posix.Types (ProcessID)
import System.Process.Typed.Internal
import qualified Data.ByteString.Lazy as L
import GHC.RTS.Flags (getConcFlags, ctxtSwitchTime)
Expand Down Expand Up @@ -641,6 +646,17 @@ checkExitCodeSTM p = do
, eceStderr = L.empty
}

-- | Returns the PID (process ID) of a subprocess.
--
-- 'Nothing' is returned if the underlying 'P.ProcessHandle' was already closed.
-- Otherwise a PID is returned that remains valid as long as the handle is
-- open. The operating system may reuse the PID as soon as the last handle to
-- the process is closed.
--
-- @since 0.2.12.0
getPid :: Process stdin stdout stderr -> IO (Maybe P.Pid)
getPid = P.getPid . pHandle

-- | Internal
clearStreams :: ProcessConfig stdin stdout stderr -> ProcessConfig () () ()
clearStreams pc = pc
Expand Down

0 comments on commit 07a9807

Please sign in to comment.