diff --git a/app/Cardano/Launcher.hs b/app/Cardano/Launcher.hs index 9aa446daa4a..aaaf81edf23 100644 --- a/app/Cardano/Launcher.hs +++ b/app/Cardano/Launcher.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE CPP #-} + -- | -- Copyright: © 2018-2019 IOHK -- License: MIT @@ -10,6 +12,7 @@ module Cardano.Launcher ( Command (..) , ProcessHasExited(..) , launch + , installSignalHandlers ) where import Prelude @@ -27,6 +30,13 @@ import System.Exit import System.Process ( proc, waitForProcess, withCreateProcess ) +#ifdef mingw32_HOST_OS +import Cardano.Launcher.Windows + ( installSignalHandlers ) +#else +import Cardano.Launcher.POSIX + ( installSignalHandlers ) +#endif data Command = Command { cmdName :: String @@ -73,6 +83,6 @@ launch cmds = do throwIO $ ProcessHasExited name code case res of Left e -> return e - Right _ -> error - "Unreachable. Supervising threads should never finish. \ - \They should stay running or throw @ProcessHasExited@." + Right _ -> error $ + "Unreachable. Supervising threads should never finish. " <> + "They should stay running or throw @ProcessHasExited@." diff --git a/app/Cardano/Launcher/POSIX.hs b/app/Cardano/Launcher/POSIX.hs new file mode 100644 index 00000000000..6cee08cd596 --- /dev/null +++ b/app/Cardano/Launcher/POSIX.hs @@ -0,0 +1,33 @@ +-- | +-- Copyright: © 2018-2019 IOHK +-- License: MIT +-- Portability: POSIX +-- + +module Cardano.Launcher.POSIX + ( installSignalHandlers + ) where + +import Prelude + +import Control.Monad + ( void ) +import Say + ( sayErr ) +import System.Posix.Signals + ( Handler (..) + , installHandler + , keyboardSignal + , raiseSignal + , softwareTermination + ) + +-- | Convert any SIGTERM received to SIGINT, for which the runtime system has +-- handlers that will correctly clean up sub-processes. +installSignalHandlers :: IO () +installSignalHandlers = void $ + installHandler softwareTermination termHandler Nothing + where + termHandler = CatchOnce $ do + sayErr "Terminated by signal." + raiseSignal keyboardSignal diff --git a/app/Cardano/Launcher/Windows.hs b/app/Cardano/Launcher/Windows.hs new file mode 100644 index 00000000000..50e0a01e127 --- /dev/null +++ b/app/Cardano/Launcher/Windows.hs @@ -0,0 +1,15 @@ +-- | +-- Copyright: © 2018-2019 IOHK +-- License: MIT +-- Portability: Windows +-- + +module Cardano.Launcher.Windows + ( installSignalHandlers + ) where + +import Prelude + +-- | Stub function for windows. +installSignalHandlers :: IO () +installSignalHandlers = pure () diff --git a/app/launcher/Main.hs b/app/launcher/Main.hs index a1a77c2f8ea..85c9eb495ce 100644 --- a/app/launcher/Main.hs +++ b/app/launcher/Main.hs @@ -8,7 +8,8 @@ import Prelude import Cardano.CLI ( Network, Port, decode, encode, getArg ) import Cardano.Launcher - ( Command (Command), ProcessHasExited (ProcessHasExited), launch ) + ( Command (Command), ProcessHasExited (ProcessHasExited), launch + , installSignalHandlers ) import Control.Concurrent ( threadDelay ) import Control.Monad @@ -59,6 +60,7 @@ main = do network <- getArg args cli (longOption "network") decode sayErr "Starting..." + installSignalHandlers let commands = [ nodeHttpBridgeOn bridgePort , walletOn walletPort bridgePort network diff --git a/cardano-wallet.cabal b/cardano-wallet.cabal index 8f96943ee52..449ed0fec16 100644 --- a/cardano-wallet.cabal +++ b/cardano-wallet.cabal @@ -183,6 +183,13 @@ executable cardano-wallet-launcher other-modules: Cardano.CLI Cardano.Launcher + if os(windows) + build-depends: Win32 + other-modules: Cardano.Launcher.Windows + else + build-depends: unix + other-modules: Cardano.Launcher.POSIX + main-is: Main.hs