Fork/exec in Haskell
- Magnus Therning
Here’s some simple code I put together. I’m mostly posting it so I won’t have any problems finding it in the future.
module Main where
import Control.Monad
import System.Exit
import System.Posix.IO
import System.Posix.Process
= do
executeChild mapM_ closeFd [stdInput, stdOutput, stdError]
<- openFd "/dev/null" ReadWrite Nothing defaultFileFlags
devnull
dup devnull; dup devnull"./Child" False [] Nothing
executeFile
= do
main <- forkProcess executeChild
child putStrLn "ForkExec: main - forked, going to wait"
<- getProcessStatus True True child
s case s of
Nothing -> -- this shouldn't happen, ever
print s >> exitFailure
Just s -> do
print s
case s of
Exited _ -> putStrLn "Child exited properly, though possibly unsuccessfully"
Terminated _ -> putStrLn "Terminated!"
Stopped _ -> putStrLn "Stopped (only SIGSTOP?)"
exitSuccess exitFailure
It’d be really nice to be able to, after the fork, close all open file descriptors in the child. But how can I find all the open file descriptors in a process? Ideally it should be fairly portable, though portability to major Unix/Linux systems is enough for me.