-
Notifications
You must be signed in to change notification settings - Fork 13
/
Signal.hs
55 lines (42 loc) · 1.72 KB
/
Signal.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE CPP #-}
module Snap.Loader.Dynamic.Signal (protectHandlers) where
#ifdef mingw32_HOST_OS
-------------
-- windows --
-------------
------------------------------------------------------------------------------
import GHC.ConsoleHandler as C
saveHandlers :: IO C.Handler
saveHandlers = C.installHandler Ignore
restoreHandlers :: C.Handler -> IO ()
restoreHandlers h = C.installHandler h >> return ()
------------------------------------------------------------------------------
#else
-----------
-- posix --
-----------
------------------------------------------------------------------------------
import qualified System.Posix.Signals as S
helper :: S.Handler -> S.Signal -> IO S.Handler
helper handler signal = S.installHandler signal handler Nothing
signals :: [S.Signal]
signals = [ S.sigQUIT
, S.sigINT
, S.sigHUP
, S.sigTERM
]
saveHandlers :: IO [S.Handler]
saveHandlers = mapM (helper S.Ignore) signals
restoreHandlers :: [S.Handler] -> IO ()
restoreHandlers h = sequence_ $ zipWith helper h signals
------------------------------------------------------------------------------
#endif
----------
-- both --
----------
------------------------------------------------------------------------------
protectHandlers :: IO (IO ())
protectHandlers = do
h <- saveHandlers
return $ restoreHandlers h
------------------------------------------------------------------------------