From 94e76035c627e003f601c6501c0f414a13160f1f Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Fri, 20 Dec 2013 11:17:18 -0500 Subject: [PATCH] disable SIGHUP restart behavior in foreground SIGHUP should only be a restart action for a daemonized processs, when used on a console process SIGHUP should == SIGTERM. Add detection for whether this service is actually daemonized. Change-Id: I2a73d3872a302fcfac1a6ee844a44faa9569b3dd Fixes-Bug: #1263122 --- openstack/common/service.py | 42 ++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/openstack/common/service.py b/openstack/common/service.py index 5d84beaa..c26cd34d 100644 --- a/openstack/common/service.py +++ b/openstack/common/service.py @@ -25,6 +25,14 @@ import sys import time +try: + # Importing just the symbol here because the io module does not + # exist in Python 2.6. + from io import UnsupportedOperation # noqa +except ImportError: + # Python 2.6 + UnsupportedOperation = None + import eventlet from eventlet import event from oslo.config import cfg @@ -45,8 +53,32 @@ def _sighup_supported(): return hasattr(signal, 'SIGHUP') -def _is_sighup(signo): - return _sighup_supported() and signo == signal.SIGHUP +def _is_daemon(): + # The process group for a foreground process will match the + # process group of the controlling terminal. If those values do + # not match, or ioctl() fails on the stdout file handle, we assume + # the process is running in the background as a daemon. + # http://www.gnu.org/software/bash/manual/bashref.html#Job-Control-Basics + try: + is_daemon = os.getpgrp() != os.tcgetpgrp(sys.stdout.fileno()) + except OSError as err: + if err.errno == errno.ENOTTY: + # Assume we are a daemon because there is no terminal. + is_daemon = True + else: + raise + except UnsupportedOperation: + # Could not get the fileno for stdout, so we must be a daemon. + is_daemon = True + return is_daemon + + +def _is_sighup_and_daemon(signo): + if not (_sighup_supported() and signo == signal.SIGHUP): + # Avoid checking if we are a daemon, because the signal isn't + # SIGHUP. + return False + return _is_daemon() def _signo_to_signame(signo): @@ -160,7 +192,7 @@ def wait(self, ready_callback=None): while True: self.handle_signal() status, signo = self._wait_for_exit_or_signal(ready_callback) - if not _is_sighup(signo): + if not _is_sighup_and_daemon(signo): return status self.restart() @@ -280,7 +312,7 @@ def _start_child(self, wrap): while True: self._child_process_handle_signal() status, signo = self._child_wait_for_exit_or_signal(launcher) - if not _is_sighup(signo): + if not _is_sighup_and_daemon(signo): break launcher.restart() @@ -352,7 +384,7 @@ def wait(self): if self.sigcaught: signame = _signo_to_signame(self.sigcaught) LOG.info(_('Caught %s, stopping children'), signame) - if not _is_sighup(self.sigcaught): + if not _is_sighup_and_daemon(self.sigcaught): break for pid in self.children: