From de54e5484af00b23ade92d685debfdf205dd66b7 Mon Sep 17 00:00:00 2001 From: monacoremo <59358383+monacoremo@users.noreply.github.com> Date: Sat, 24 Apr 2021 16:06:10 +0200 Subject: [PATCH] fix: Panic when attempting to run with unix socket on non-unix host --- main/Main.hs | 6 +++--- src/PostgREST/App.hs | 12 ++++++++---- src/PostgREST/CLI.hs | 6 +++--- src/PostgREST/Unix.hs | 6 +++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/main/Main.hs b/main/Main.hs index c5e73bab16..15abf009c1 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -31,11 +31,11 @@ installSignalHandlers = Unix.installSignalHandlers installSignalHandlers _ = pass #endif -runAppInSocket :: App.SocketRunner +runAppInSocket :: Maybe App.SocketRunner #ifndef mingw32_HOST_OS -runAppInSocket = Unix.runAppInSocket +runAppInSocket = Just Unix.runAppWithSocket #else -runAppInSocket _ _ _ _ = pass +runAppInSocket = Nothing #endif setBuffering :: IO () diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 1066461c84..0e369d1681 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -101,8 +101,8 @@ type SignalHandlerInstaller = AppState -> IO() type SocketRunner = Warp.Settings -> Wai.Application -> FileMode -> FilePath -> IO() -run :: SignalHandlerInstaller -> SocketRunner -> AppState -> IO () -run installHandlers runInSocket appState = do +run :: SignalHandlerInstaller -> Maybe SocketRunner -> AppState -> IO () +run installHandlers maybeRunWithSocket appState = do conf@AppConfig{..} <- AppState.getConfig appState connectionWorker appState -- Loads the initial DbStructure installHandlers appState @@ -113,8 +113,12 @@ run installHandlers runInSocket appState = do case configServerUnixSocket of Just socket -> - -- run the postgrest application with user defined socket. Only for UNIX systems. - runInSocket (serverSettings conf) app configServerUnixSocketMode socket + -- run the postgrest application with user defined socket. Only for UNIX systems + case maybeRunWithSocket of + Just runWithSocket -> + runWithSocket (serverSettings conf) app configServerUnixSocketMode socket + Nothing -> + panic "Cannot run with socket on non-unix plattforms." Nothing -> do putStrLn $ ("Listening on port " :: Text) <> show configServerPort diff --git a/src/PostgREST/CLI.hs b/src/PostgREST/CLI.hs index 13a76df8d9..84a9611ce0 100644 --- a/src/PostgREST/CLI.hs +++ b/src/PostgREST/CLI.hs @@ -31,8 +31,8 @@ import qualified PostgREST.Config as Config import Protolude hiding (hPutStrLn) -main :: App.SignalHandlerInstaller -> App.SocketRunner -> CLI -> IO () -main installSignalHandlers runAppInSocket CLI{cliCommand, cliPath} = do +main :: App.SignalHandlerInstaller -> Maybe App.SocketRunner -> CLI -> IO () +main installSignalHandlers runAppWithSocket CLI{cliCommand, cliPath} = do conf@AppConfig{..} <- either panic identity <$> Config.readAppConfig mempty cliPath Nothing appState <- AppState.init conf @@ -47,7 +47,7 @@ main installSignalHandlers runAppInSocket CLI{cliCommand, cliPath} = do exec :: Command -> AppState -> IO () exec CmdDumpConfig appState = putStr . Config.toText =<< AppState.getConfig appState exec CmdDumpSchema appState = putStrLn =<< dumpSchema appState - exec CmdRun appState = App.run installSignalHandlers runAppInSocket appState + exec CmdRun appState = App.run installSignalHandlers runAppWithSocket appState -- | Dump DbStructure schema to JSON dumpSchema :: AppState -> IO LBS.ByteString diff --git a/src/PostgREST/Unix.hs b/src/PostgREST/Unix.hs index ddae782cc1..86d42bacc6 100644 --- a/src/PostgREST/Unix.hs +++ b/src/PostgREST/Unix.hs @@ -1,5 +1,5 @@ module PostgREST.Unix - ( runAppInSocket + ( runAppWithSocket , installSignalHandlers ) where @@ -20,8 +20,8 @@ import Protolude -- | Run the PostgREST application with user defined socket. -runAppInSocket :: Warp.Settings -> Application -> FileMode -> FilePath -> IO () -runAppInSocket settings app socketFileMode socketFilePath = +runAppWithSocket :: Warp.Settings -> Application -> FileMode -> FilePath -> IO () +runAppWithSocket settings app socketFileMode socketFilePath = bracket createAndBindSocket Socket.close $ \socket -> do putStrLn $ ("Listening on unix socket " :: Text) <> show socketFilePath Socket.listen socket Socket.maxListenQueue