module System.Posix.Daemon.IORedirection
(
redirectStdout,
redirectStderr,
redirectStdin
)
where
import Control.Monad
import System.IO
import System.Posix
redirectStdout :: FilePath -> IO ()
redirectStdout newPath = do
redirectFd newPath stdOutput
disableBuffering stdout
redirectStderr :: FilePath -> IO ()
redirectStderr newPath = do
redirectFd newPath stdError
disableBuffering stderr
redirectStdin :: FilePath -> IO ()
redirectStdin newPath = do
redirectFd newPath stdInput
disableBuffering stdin
disableBuffering :: Handle -> IO ()
disableBuffering hdl = do
hSetBuffering hdl NoBuffering
seekable <- hIsSeekable hdl
when seekable $ hSeek hdl SeekFromEnd 0
safeOpenFd :: FilePath -> IO Fd
safeOpenFd p = do
exists <- fileExist p
when (not exists) $ writeFile p ""
openFd p ReadWrite Nothing defaultFileFlags
redirectFd :: FilePath -> Fd -> IO ()
redirectFd newPath oldFd = void $ do
newFd <- safeOpenFd newPath
closeFd oldFd >> dupTo newFd oldFd
closeFd newFd