summaryrefslogtreecommitdiffstats
path: root/src/Flameshot/Internal
diff options
context:
space:
mode:
Diffstat (limited to 'src/Flameshot/Internal')
-rw-r--r--src/Flameshot/Internal/Process.hs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/Flameshot/Internal/Process.hs b/src/Flameshot/Internal/Process.hs
index c435d48..47220a3 100644
--- a/src/Flameshot/Internal/Process.hs
+++ b/src/Flameshot/Internal/Process.hs
@@ -3,12 +3,14 @@
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
-module Flameshot.Internal.Process (run,Callbacks(..)) where
+module Flameshot.Internal.Process (run,Callbacks(..),writeDaemon) where
import Control.Concurrent (forkIO,threadDelay)
import Control.Concurrent.Async (race)
import Control.Exception
-import Control.Monad.Extended (untilM_,unless)
+import Control.Monad.Extended (untilM_,unless,void)
+import Data.ByteString (ByteString)
+import qualified Data.ByteString as BS
import Data.Text (Text)
import qualified Data.Text.IO as T
import System.Exit
@@ -16,10 +18,16 @@ import System.IO (BufferMode(LineBuffering),hSetBuffering)
import System.IO (Handle,hClose,hPutStr,hIsEOF)
import System.IO.Error (catchIOError,isDoesNotExistError)
import System.Process
+import System.Posix.Directory (changeWorkingDirectory)
+import System.Posix.Files (setFileCreationMask)
+import System.Posix.IO (closeFd,dupTo,openFd,stdError,stdInput,stdOutput)
+import System.Posix.IO (defaultFileFlags,OpenMode(WriteOnly))
+import System.Posix.Process (createSession,forkProcess,getProcessStatus)
import System.Posix.Process (getProcessGroupIDOf)
import System.Posix.Signals (Signal,signalProcessGroup,killProcess)
import System.Posix.Types (ProcessGroupID)
+
data Callbacks = Callbacks
{ onOutLine :: Pid -> Text -> IO ()
, onErrLine :: Pid -> Text -> IO ()
@@ -73,6 +81,20 @@ run path args cwd env input hTimeout Callbacks{..} =
}
+writeDaemon :: FilePath -> [String] -> ByteString -> IO ()
+writeDaemon path args input = do
+ pid <- forkProcess $ do
+ _ <- setFileCreationMask 0o0027
+ _ <- createSession
+ changeWorkingDirectory "/"
+ closeFd stdInput
+ bracket (openFd "/dev/null" WriteOnly Nothing defaultFileFlags) closeFd
+ $ \hnull -> mapM_ (dupTo hnull) [stdOutput, stdError]
+ (Just hin, _, _, _) <-
+ createProcess (proc path args) { std_in = CreatePipe }
+ BS.hPut hin input
+ hClose hin
+ void (getProcessStatus True False pid)
killProcessGroup :: ProcessGroupID -> IO ()
killProcessGroup = signalProcessGroup' killProcess