diff options
author | joe <joe@jerkface.net> | 2017-11-12 20:35:00 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-11-12 20:35:00 -0500 |
commit | 5e8f82e436c03e1c59e69d5c9eb0e5a14284dd87 (patch) | |
tree | 1f5e8816e162e570131b8f4cfd90f7cb0b4c5fa7 /Presence/FGConsole.hs | |
parent | d4288f5a9f87e3889a50a347ebad0a812f52938c (diff) |
We no longer require root. (ConsoleWriter is disabled without).
Diffstat (limited to 'Presence/FGConsole.hs')
-rw-r--r-- | Presence/FGConsole.hs | 25 |
1 files changed, 15 insertions, 10 deletions
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 | |||
11 | import Control.Exception as E | 11 | import Control.Exception as E |
12 | -- import Prelude as E | 12 | -- import Prelude as E |
13 | import Control.Monad | 13 | import Control.Monad |
14 | import Foreign.C.Error | ||
15 | import Foreign.C | 14 | import Foreign.C |
16 | 15 | ||
17 | import Logging | 16 | import Logging |
@@ -19,12 +18,13 @@ import System.Posix.Signals | |||
19 | 18 | ||
20 | -- c_monitorTTY fd = trace "c_monitorTTY" (return ()) -- (trace "WTF" todo) | 19 | -- c_monitorTTY fd = trace "c_monitorTTY" (return ()) -- (trace "WTF" todo) |
21 | 20 | ||
22 | foreign import ccall "monitorTTY" c_monitorTTY :: Fd -> IO () | 21 | foreign import ccall "monitorTTY" c_monitorTTY :: Fd -> IO CInt |
23 | foreign import ccall "closeTTY" c_closeTTY :: IO () | 22 | foreign import ccall "closeTTY" c_closeTTY :: IO () |
24 | 23 | ||
25 | monitorTTY :: (Word8 -> IO ()) -> IO (Fd,ThreadId) | 24 | forkTTYMonitor :: (Word8 -> IO ()) -> IO (Maybe (Fd,ThreadId)) |
26 | monitorTTY handler = do | 25 | forkTTYMonitor handler = do |
27 | (rfd,wfd) <- createPipe | 26 | (rfd,wfd) <- createPipe |
27 | retvar <- newEmptyMVar | ||
28 | thread <- forkIO $ do | 28 | thread <- forkIO $ do |
29 | let cleanup = do | 29 | let cleanup = do |
30 | trace "quitting monitorTTY thread." (return ()) | 30 | trace "quitting monitorTTY thread." (return ()) |
@@ -32,8 +32,10 @@ monitorTTY handler = do | |||
32 | closeFd rfd `E.catch` \(e::IOException) -> return () | 32 | closeFd rfd `E.catch` \(e::IOException) -> return () |
33 | c_closeTTY | 33 | c_closeTTY |
34 | -- rh <- fdToHandle rfd | 34 | -- rh <- fdToHandle rfd |
35 | c_monitorTTY wfd | 35 | didfork <- c_monitorTTY wfd |
36 | let monitor = | 36 | putMVar retvar didfork |
37 | when (didfork == 0) $ do | ||
38 | let monitor = | ||
37 | (do | 39 | (do |
38 | threadWaitRead rfd | 40 | threadWaitRead rfd |
39 | (cs,cnt) <- fdRead rfd 1 | 41 | (cs,cnt) <- fdRead rfd 1 |
@@ -45,13 +47,16 @@ monitorTTY handler = do | |||
45 | case () of | 47 | case () of |
46 | _ | err==eAGAIN -> monitor | 48 | _ | err==eAGAIN -> monitor |
47 | _ | otherwise -> cleanup | 49 | _ | otherwise -> cleanup |
48 | `E.catch` | 50 | `E.catch` |
49 | \(e :: AsyncException) -> cleanup | 51 | \(e :: AsyncException) -> cleanup |
50 | monitor | 52 | monitor |
51 | return (rfd,thread) | 53 | didfork <- takeMVar retvar |
54 | if didfork == 0 | ||
55 | then return $! Just (rfd,thread) | ||
56 | else return $! Nothing | ||
52 | 57 | ||
53 | unmonitorTTY :: (Fd, ThreadId) -> IO () | 58 | killTTYMonitor :: (Fd, ThreadId) -> IO () |
54 | unmonitorTTY (rfd,thread) = do | 59 | killTTYMonitor (rfd,thread) = do |
55 | closeFd rfd | 60 | closeFd rfd |
56 | yield | 61 | yield |
57 | killThread thread | 62 | killThread thread |