diff --git a/src-server/Main.hs b/src-server/Main.hs index cc4a522..bf00a5c 100644 --- a/src-server/Main.hs +++ b/src-server/Main.hs @@ -39,7 +39,7 @@ main = do ) Right (Settings {..}) -> do sock <- bindSocket setSocketPath - dropSocketOnSigint sock + terminateSocketOnSigint sock Net.listen sock 1024 putStrLn "Bound and listening to socket:" print =<< getSocketName sock diff --git a/src-server/Server/Communication.hs b/src-server/Server/Communication.hs index fc448f9..c185474 100644 --- a/src-server/Server/Communication.hs +++ b/src-server/Server/Communication.hs @@ -10,18 +10,25 @@ import System.Posix.Signals import Server.Util -bindSocket :: FilePath -> IO Socket +-- | 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) - removeIfExists path + -- aremoveIfExists path sock <- socket AF_UNIX Stream 0 bind sock sockAddr pure sock -dropSocketOnSigint :: Socket -> IO () -dropSocketOnSigint 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 diff --git a/src-server/Server/Util.hs b/src-server/Server/Util.hs index 57577ce..8442ed9 100644 --- a/src-server/Server/Util.hs +++ b/src-server/Server/Util.hs @@ -6,7 +6,10 @@ import System.Directory import System.IO.Error -removeIfExists :: FilePath -> IO () +-- | Remove a file if it exists +removeIfExists + :: FilePath -- File to remove + -> IO () removeIfExists fileName = removeFile fileName `catch` handleExists where handleExists e | isDoesNotExistError e = return ()