cabal init -i should sanitize suggested package name (fix #8404) (#8561)
[cabal.git] / cabal-install / src / Distribution / Client / Signal.hs
blob726fd8cbcc3d60a4b99936b5d8d299deb87b38e5
1 {-# LANGUAGE CPP #-}
2 module Distribution.Client.Signal
3 ( installTerminationHandler
4 , Terminated(..)
6 where
8 import qualified Control.Exception as Exception
10 #ifndef mingw32_HOST_OS
11 import Control.Concurrent (myThreadId)
12 import Control.Monad (void)
13 import qualified System.Posix.Signals as Signals
14 #endif
16 -- | Terminated is an asynchronous exception, thrown when
17 -- SIGTERM is received. It's to 'kill' what 'UserInterrupt'
18 -- is to Ctrl-C.
19 data Terminated = Terminated
21 instance Exception.Exception Terminated where
22 toException = Exception.asyncExceptionToException
23 fromException = Exception.asyncExceptionFromException
25 instance Show Terminated where
26 show Terminated = "terminated"
28 -- | Install a signal handler that initiates a controlled shutdown on receiving
29 -- SIGTERM by throwing an asynchronous exception at the main thread. Must be
30 -- called from the main thread.
32 -- It is a noop on Windows.
34 installTerminationHandler :: IO ()
36 #ifdef mingw32_HOST_OS
38 installTerminationHandler = return ()
40 #else
42 installTerminationHandler = do
43 mainThreadId <- myThreadId
44 void $ Signals.installHandler
45 Signals.sigTERM
46 (Signals.CatchOnce $ Exception.throwTo mainThreadId Terminated)
47 Nothing
49 #endif