module Server.Communication where import Control.Monad import Network.Socket import System.Posix.Signals -- internal imports import Server.Util -- | Function which determines whether the given filePath is a supported socket path and -- subsequently creates a socket in said location. bindSocket :: FilePath -- ^ File Path for socket to be created (e.g.: "/tmp/wizard.sock") -> IO Socket -- ^ resulting Socket bindSocket path = do let sockAddr = SockAddrUnix path unless (isSupportedSockAddr sockAddr) (error $ "invalid socket path " <> path) -- aremoveIfExists path sock <- socket AF_UNIX Stream 0 bind sock sockAddr pure sock -- | Function that installs a handler on SIGINT to close and remove the given socket terminateSocketOnSigint :: Socket -- ^ Socket to terminate on termination -> IO () terminateSocketOnSigint sock = void $ installHandler keyboardSignal (CatchOnce $ do (SockAddrUnix path) <- getSocketName sock close' sock removeIfExists path -- Raise SIGINT again so it does not get blocked raiseSignal keyboardSignal ) Nothing