From 0d0ac0253b8e51858233260fcead76a00e4bca4b Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Thu, 23 Aug 2012 22:53:20 +0200 Subject: [PATCH] Cleanup query socket before attempting to bind This patch adds a simple function to ensure a socket doesn't exist before binding to it. Additionally, we cleanup the exports of Queryd.hs. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Agata Murawska <agatamurawska@google.com> --- htools/Ganeti/Daemon.hs | 9 +++++++++ htools/Ganeti/Queryd.hs | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/htools/Ganeti/Daemon.hs b/htools/Ganeti/Daemon.hs index d6ce50b40..80f74f143 100644 --- a/htools/Ganeti/Daemon.hs +++ b/htools/Ganeti/Daemon.hs @@ -37,6 +37,7 @@ module Ganeti.Daemon , oSyslogUsage , parseArgs , parseAddress + , cleanupSocket , writePidFile , genericMain ) where @@ -54,6 +55,7 @@ import System.Exit import System.Environment import System.Info import System.IO +import System.IO.Error (isDoesNotExistError) import System.Posix.Directory import System.Posix.Files import System.Posix.IO @@ -221,6 +223,13 @@ writePidFile path = do catch (fmap Ok $ _writePidFile path) (return . Bad . formatIOError "Failure during writing of the pid file") +-- | Helper function to ensure a socket doesn't exist. Should only be +-- called once we have locked the pid file successfully. +cleanupSocket :: FilePath -> IO () +cleanupSocket socketPath = do + catchJust (guard . isDoesNotExistError) (removeLink socketPath) + (const $ return ()) + -- | Sets up a daemon's environment. setupDaemonEnv :: FilePath -> FileMode -> IO () setupDaemonEnv cwd umask = do diff --git a/htools/Ganeti/Queryd.hs b/htools/Ganeti/Queryd.hs index eeeef7ac6..c7d417a7e 100644 --- a/htools/Ganeti/Queryd.hs +++ b/htools/Ganeti/Queryd.hs @@ -26,8 +26,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -} module Ganeti.Queryd - -where + ( ConfigReader + , runQueryD + ) where import Control.Applicative import Control.Concurrent @@ -41,6 +42,7 @@ import Text.JSON.Pretty (pp_value) import System.Info (arch) import qualified Ganeti.Constants as C +import Ganeti.Daemon import Ganeti.Objects import qualified Ganeti.Config as Config import Ganeti.BasicTypes @@ -183,6 +185,7 @@ mainLoop creader socket = do runQueryD :: Maybe FilePath -> ConfigReader -> IO () runQueryD fpath creader = do let socket_path = fromMaybe C.querySocket fpath + cleanupSocket socket_path bracket (getServer socket_path) (closeServer socket_path) -- GitLab