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 Right (Settings {..}) -> do
sock <- bindSocket setSocketPath sock <- bindSocket setSocketPath
dropSocketOnSigint sock terminateSocketOnSigint sock
Net.listen sock 1024 Net.listen sock 1024
putStrLn "Bound and listening to socket:" putStrLn "Bound and listening to socket:"
print =<< getSocketName sock print =<< getSocketName sock

View file

@ -10,18 +10,25 @@ import System.Posix.Signals
import Server.Util 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 bindSocket path = do
let sockAddr = SockAddrUnix path let sockAddr = SockAddrUnix path
unless (isSupportedSockAddr sockAddr) unless (isSupportedSockAddr sockAddr)
(error $ "invalid socket path " <> path) (error $ "invalid socket path " <> path)
removeIfExists path -- aremoveIfExists path
sock <- socket AF_UNIX Stream 0 sock <- socket AF_UNIX Stream 0
bind sock sockAddr bind sock sockAddr
pure sock pure sock
dropSocketOnSigint :: Socket -> IO () -- | Function that installs a handler on SIGINT to close and remove the given socket
dropSocketOnSigint sock = terminateSocketOnSigint
:: Socket -- ^ Socket to terminate on termination
-> IO ()
terminateSocketOnSigint sock =
void $ installHandler void $ installHandler
keyboardSignal keyboardSignal
(CatchOnce $ do (CatchOnce $ do

View file

@ -6,7 +6,10 @@ import System.Directory
import System.IO.Error 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 removeIfExists fileName = removeFile fileName `catch` handleExists
where handleExists e where handleExists e
| isDoesNotExistError e = return () | isDoesNotExistError e = return ()