This commit is contained in:
nek0 2023-12-09 14:18:10 +01:00
parent 1a925eaa6d
commit 734511b5f9
3 changed files with 16 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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 ()