Debian Codegraph @

src/Lambdabot/Util/Signals.hs

raw
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleContexts #-}
4
5 -- | The signal story.
6 -- Posix signals are external events that invoke signal handlers in
7 -- Haskell. The signal handlers in turn throw dynamic exceptions. Our
8 -- instance of MonadError for LB maps the dynamic exceptions to
9 -- SignalCaughts, which can then be caught by a normal catchError
10
11 -- Here's where we do that.
12 module Lambdabot.Util.Signals
13 ( Signal
14 , SignalException(..)
15 , ircSignalMessage
16 , withIrcSignalCatch
17 ) where
18
19 import Data.Typeable
20 import Control.Exception (Exception)
21
22 #ifdef mingw32_HOST_OS
23
24 import Control.Monad.Trans.Control
25
26 type Signal = String
27 newtype SignalException = SignalException Signal deriving (Show, Typeable)
28 instance Exception SignalException
29
30 ircSignalMessage :: Signal -> [Char]
31 ircSignalMessage s = s
32
33 withIrcSignalCatch :: MonadBaseControl IO m => m a -> m a
34 withIrcSignalCatch m = m
35
36 #else
37
38 import Control.Concurrent.Lifted (myThreadId, newEmptyMVar, putMVar, MVar, ThreadId)
39 import Control.Exception.Lifted (bracket, throwTo)
40 import Control.Monad
41 import Control.Monad.Base
42 import Control.Monad.Trans.Control
43
44 import System.IO.Unsafe
45 import System.Posix.Signals
46
47 newtype SignalException = SignalException Signal deriving (Show, Typeable)
48 instance Exception SignalException
49
50 --
51 -- A bit of sugar for installing a new handler
52 --
53 withHandler :: MonadBaseControl IO m => Signal -> Handler -> m a -> m a
54 withHandler s h m = bracket
55 (liftBase (installHandler s h Nothing))
56 (liftBase . flip (installHandler s) Nothing)
57 (const m)
58
59 -- And more sugar for installing a list of handlers
60 withHandlerList :: MonadBaseControl IO m => [Signal] -> (Signal -> Handler) -> m a -> m a
61 withHandlerList sl h m = foldr (withHandler `ap` h) m sl
62
63 --
64 -- Signals we care about. They're all fatal.
65 --
66 -- Be careful adding signals, some signals can't be caught and
67 -- installHandler just raises an exception if you try
68 --
69 ircSignalsToCatch :: [(Signal, String)]
70 ircSignalsToCatch =
71 [ (busError, "SIGBUS" )
72 , (segmentationViolation, "SIGSEGV" )
73 , (keyboardSignal, "SIGINT" )
74 , (softwareTermination, "SIGTERM" )
75 , (keyboardTermination, "SIGQUIT" )
76 , (lostConnection, "SIGHUP" )
77 , (internalAbort, "SIGABRT" )
78 ]
79
80 --
81 -- User friendly names for the signals that we can catch
82 --
83 ircSignalMessage :: Signal -> String
84 ircSignalMessage sig = case lookup sig ircSignalsToCatch of
85 Just sigName -> sigName
86 Nothing -> "killed by unknown signal"
87
88 --
89 -- The actual signal handler. It is this function we register for each
90 -- signal flavour. On receiving a signal, the signal handler maps the
91 -- signal to a a dynamic exception, and throws it out to the main
92 -- thread. The LB MonadError instance can then do its trickery to catch
93 -- it in handler/catchError
94 --
95 ircSignalHandler :: ThreadId -> Signal -> Handler
96 ircSignalHandler threadid s
97 = CatchOnce $ do
98 putMVar catchLock ()
99 releaseSignals
100 throwTo threadid $ SignalException s
101
102 --
103 -- | Release all signal handlers
104 --
105 releaseSignals :: IO ()
106 releaseSignals = sequence_
107 [ installHandler sig Default Nothing
108 | (sig, _) <- ircSignalsToCatch
109 ]
110
111 --
112 -- Mututally exclusive signal handlers
113 --
114 -- This is clearly a hack, but I have no idea how to accomplish the same
115 -- thing correctly. The main problem is that signals are often thrown
116 -- multiple times, and the threads start killing each other if we allow
117 -- the SignalException to be thrown more than once.
118 {-# NOINLINE catchLock #-}
119 catchLock :: MVar ()
120 catchLock = unsafePerformIO newEmptyMVar
121
122 --
123 -- | Register signal handlers to catch external signals
124 --
125 withIrcSignalCatch :: MonadBaseControl IO m => m a -> m a
126 withIrcSignalCatch m = do
127 _ <- liftBase $ installHandler sigPIPE Ignore Nothing
128 _ <- liftBase $ installHandler sigALRM Ignore Nothing
129 threadid <- myThreadId
130 withHandlerList (map fst ircSignalsToCatch) (ircSignalHandler threadid) m
131 #endif