From 5e8f82e436c03e1c59e69d5c9eb0e5a14284dd87 Mon Sep 17 00:00:00 2001 From: joe Date: Sun, 12 Nov 2017 20:35:00 -0500 Subject: We no longer require root. (ConsoleWriter is disabled without). --- Presence/FGConsole.hs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'Presence/FGConsole.hs') diff --git a/Presence/FGConsole.hs b/Presence/FGConsole.hs index 623fb493..03aaebf2 100644 --- a/Presence/FGConsole.hs +++ b/Presence/FGConsole.hs @@ -11,7 +11,6 @@ import Unsafe.Coerce import Control.Exception as E -- import Prelude as E import Control.Monad -import Foreign.C.Error import Foreign.C import Logging @@ -19,12 +18,13 @@ import System.Posix.Signals -- c_monitorTTY fd = trace "c_monitorTTY" (return ()) -- (trace "WTF" todo) -foreign import ccall "monitorTTY" c_monitorTTY :: Fd -> IO () +foreign import ccall "monitorTTY" c_monitorTTY :: Fd -> IO CInt foreign import ccall "closeTTY" c_closeTTY :: IO () -monitorTTY :: (Word8 -> IO ()) -> IO (Fd,ThreadId) -monitorTTY handler = do +forkTTYMonitor :: (Word8 -> IO ()) -> IO (Maybe (Fd,ThreadId)) +forkTTYMonitor handler = do (rfd,wfd) <- createPipe + retvar <- newEmptyMVar thread <- forkIO $ do let cleanup = do trace "quitting monitorTTY thread." (return ()) @@ -32,8 +32,10 @@ monitorTTY handler = do closeFd rfd `E.catch` \(e::IOException) -> return () c_closeTTY -- rh <- fdToHandle rfd - c_monitorTTY wfd - let monitor = + didfork <- c_monitorTTY wfd + putMVar retvar didfork + when (didfork == 0) $ do + let monitor = (do threadWaitRead rfd (cs,cnt) <- fdRead rfd 1 @@ -45,13 +47,16 @@ monitorTTY handler = do case () of _ | err==eAGAIN -> monitor _ | otherwise -> cleanup - `E.catch` + `E.catch` \(e :: AsyncException) -> cleanup monitor - return (rfd,thread) + didfork <- takeMVar retvar + if didfork == 0 + then return $! Just (rfd,thread) + else return $! Nothing -unmonitorTTY :: (Fd, ThreadId) -> IO () -unmonitorTTY (rfd,thread) = do +killTTYMonitor :: (Fd, ThreadId) -> IO () +killTTYMonitor (rfd,thread) = do closeFd rfd yield killThread thread -- cgit v1.2.3