summaryrefslogtreecommitdiffstats
path: root/src/Flameshot/Internal
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2019-02-09 02:13:06 +0100
committertv <tv@krebsco.de>2019-02-09 02:33:04 +0100
commit9d688b6ffad14912bd1afe42555747cb3d213d95 (patch)
treebfe639002a97508784e1c52135f566f4399d0dbb /src/Flameshot/Internal
parent03623ce6c011c1e85df7d91aed4458c098ff22ff (diff)
Main.app: copy capture to clipboard with xclipv1.1.0
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