Skip to content

Commit

Permalink
disable SIGHUP restart behavior in foreground
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Sean Dague authored and Doug Hellmann committed Dec 20, 2013
1 parent bef520f commit 94e7603
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions openstack/common/service.py
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 94e7603

Please sign in to comment.