From 36691f08c2ba880fbd4c19c62ef0b45c14995ee8 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 25 May 2012 18:00:56 +0200
Subject: [PATCH] Add support for SIGHUP handling in Haskell daemons
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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 | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/htools/Ganeti/Daemon.hs b/htools/Ganeti/Daemon.hs
index 1d0e3d415..c6708f13f 100644
--- a/htools/Ganeti/Daemon.hs
+++ b/htools/Ganeti/Daemon.hs
@@ -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
-- 
GitLab