Commit 110f49ef authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Reopen daemon's stdio on SIGHUP



Before this patch daemons would continue to refer to an old logfile for
their standard I/O if they had been asked to reopen the log (SIGHUP).
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent ad88650c
...@@ -544,15 +544,17 @@ def _BeautifyError(err): ...@@ -544,15 +544,17 @@ def _BeautifyError(err):
return "%s" % str(err) return "%s" % str(err)
def _HandleSigHup(reopen_cb, signum, frame): # pylint: disable-msg=W0613 def _HandleSigHup(reopen_fn, signum, frame): # pylint: disable-msg=W0613
"""Handler for SIGHUP. """Handler for SIGHUP.
@param reopen_cb: Callback function for reopening log files @param reopen_fn: List of callback functions for reopening log files
""" """
assert callable(reopen_cb)
logging.info("Reopening log files after receiving SIGHUP") logging.info("Reopening log files after receiving SIGHUP")
reopen_cb()
for fn in reopen_fn:
if fn:
fn()
def GenericMain(daemon_name, optionparser, def GenericMain(daemon_name, optionparser,
...@@ -668,9 +670,10 @@ def GenericMain(daemon_name, optionparser, ...@@ -668,9 +670,10 @@ def GenericMain(daemon_name, optionparser,
if options.fork: if options.fork:
utils.CloseFDs() utils.CloseFDs()
wpipe = utils.Daemonize(logfile=constants.DAEMONS_LOGFILES[daemon_name]) (wpipe, stdio_reopen_fn) = \
utils.Daemonize(logfile=constants.DAEMONS_LOGFILES[daemon_name])
else: else:
wpipe = None (wpipe, stdio_reopen_fn) = (None, None)
log_reopen_fn = \ log_reopen_fn = \
utils.SetupLogging(constants.DAEMONS_LOGFILES[daemon_name], daemon_name, utils.SetupLogging(constants.DAEMONS_LOGFILES[daemon_name], daemon_name,
...@@ -681,7 +684,8 @@ def GenericMain(daemon_name, optionparser, ...@@ -681,7 +684,8 @@ def GenericMain(daemon_name, optionparser,
console_logging=console_logging) console_logging=console_logging)
# Reopen log file(s) on SIGHUP # Reopen log file(s) on SIGHUP
signal.signal(signal.SIGHUP, compat.partial(_HandleSigHup, log_reopen_fn)) signal.signal(signal.SIGHUP,
compat.partial(_HandleSigHup, [log_reopen_fn, stdio_reopen_fn]))
utils.WritePidFile(utils.DaemonPidFileName(daemon_name)) utils.WritePidFile(utils.DaemonPidFileName(daemon_name))
try: try:
......
...@@ -36,6 +36,7 @@ from cStringIO import StringIO ...@@ -36,6 +36,7 @@ from cStringIO import StringIO
from ganeti import errors from ganeti import errors
from ganeti import constants from ganeti import constants
from ganeti import compat
from ganeti.utils import retry as utils_retry from ganeti.utils import retry as utils_retry
from ganeti.utils import wrapper as utils_wrapper from ganeti.utils import wrapper as utils_wrapper
...@@ -835,8 +836,9 @@ def Daemonize(logfile): ...@@ -835,8 +836,9 @@ def Daemonize(logfile):
@type logfile: str @type logfile: str
@param logfile: the logfile to which we should redirect stdout/stderr @param logfile: the logfile to which we should redirect stdout/stderr
@rtype: int @rtype: tuple; (int, callable)
@return: the value zero @return: File descriptor of pipe(2) which must be closed to notify parent
process and a callable to reopen log files
""" """
# pylint: disable-msg=W0212 # pylint: disable-msg=W0212
...@@ -872,8 +874,12 @@ def Daemonize(logfile): ...@@ -872,8 +874,12 @@ def Daemonize(logfile):
rcode = 0 rcode = 0
os._exit(rcode) # Exit parent of the first child. os._exit(rcode) # Exit parent of the first child.
SetupDaemonFDs(logfile, None) reopen_fn = compat.partial(SetupDaemonFDs, logfile, None)
return wpipe
# Open logs for the first time
reopen_fn()
return (wpipe, reopen_fn)
def KillProcess(pid, signal_=signal.SIGTERM, timeout=30, def KillProcess(pid, signal_=signal.SIGTERM, timeout=30,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment