Add support for SIGHUP handling in Haskell daemons
authorIustin Pop <iustin@google.com>
Fri, 25 May 2012 16:00:56 +0000 (18:00 +0200)
committerIustin Pop <iustin@google.com>
Tue, 29 May 2012 09:09:32 +0000 (11:09 +0200)
This patch adds support for handling SIGHUP and reopening the
stdout/stderr logfile in daemon mode (but not in foreground mode).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

htools/Ganeti/Daemon.hs

index 1d0e3d4..c6708f1 100644 (file)
@@ -59,6 +59,7 @@ import System.Posix.Files
 import System.Posix.IO
 import System.Posix.Process
 import System.Posix.Types
+import System.Posix.Signals
 import Text.Printf
 
 import Ganeti.Logging
@@ -228,6 +229,12 @@ setupDaemonEnv cwd umask = do
   _ <- createSession
   return ()
 
+-- | Signal handler for reopening log files.
+handleSigHup :: FilePath -> IO ()
+handleSigHup path = do
+  setupDaemonFDs (Just path)
+  logInfo "Reopening log files after receiving SIGHUP"
+
 -- | Sets up a daemon's standard file descriptors.
 setupDaemonFDs :: Maybe FilePath -> IO ()
 setupDaemonFDs logfile = do
@@ -294,6 +301,7 @@ daemonize logfile action = do
     -- in the child
     setupDaemonEnv "/" (unionFileModes groupModes otherModes)
     setupDaemonFDs $ Just logfile
+    _ <- installHandler lostConnection (Catch (handleSigHup logfile)) Nothing
     _ <- forkProcess action
     exitImmediately ExitSuccess
   exitImmediately ExitSuccess