2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-03 15:55:19 +00:00

daemon.py: Whitespace cleanup.

The python style guide requires two newlines between top level
definitions.  This patch also removes some trailing whitespace.
This commit is contained in:
Ethan Jackson
2011-09-16 16:03:31 -07:00
parent 50e97486ca
commit cadc9871c2

View File

@@ -54,6 +54,7 @@ _daemonize_fd = None
RESTART_EXIT_CODE = 5
def make_pidfile_name(name):
"""Returns the file name that would be used for a pidfile if 'name' were
provided to set_pidfile()."""
@@ -62,6 +63,7 @@ def make_pidfile_name(name):
else:
return ovs.util.abs_file_name(ovs.dirs.RUNDIR, name)
def set_pidfile(name):
"""Sets up a following call to daemonize() to create a pidfile named
'name'. If 'name' begins with '/', then it is treated as an absolute path.
@@ -73,20 +75,24 @@ def set_pidfile(name):
global _pidfile
_pidfile = make_pidfile_name(name)
def get_pidfile():
"""Returns an absolute path to the configured pidfile, or None if no
pidfile is configured."""
return _pidfile
def set_no_chdir():
"""Sets that we do not chdir to "/"."""
global _chdir
_chdir = False
def is_chdir_enabled():
"""Will we chdir to "/" as part of daemonizing?"""
return _chdir
def ignore_existing_pidfile():
"""Normally, daemonize() or daemonize_start() will terminate the program
with a message if a locked pidfile already exists. If this function is
@@ -94,27 +100,32 @@ def ignore_existing_pidfile():
global _overwrite_pidfile
_overwrite_pidfile = True
def set_detach():
"""Sets up a following call to daemonize() to detach from the foreground
session, running this process in the background."""
global _detach
_detach = True
def get_detach():
"""Will daemonize() really detach?"""
return _detach
def set_monitor():
"""Sets up a following call to daemonize() to fork a supervisory process to
monitor the daemon and restart it if it dies due to an error signal."""
global _monitor
_monitor = True
def _fatal(msg):
logging.error(msg)
sys.stderr.write("%s\n" % msg)
sys.exit(1)
def _make_pidfile():
"""If a pidfile has been configured, creates it and stores the running
process's pid in it. Ensures that the pidfile will be deleted when the
@@ -172,7 +183,6 @@ def _make_pidfile():
_fatal("failed to link \"%s\" as \"%s\" (%s)"
% (tmpfile, _pidfile, os.strerror(error)))
# Ensure that the pidfile will get deleted on exit.
ovs.fatal_signal.add_file_to_unlink(_pidfile)
@@ -187,12 +197,14 @@ def _make_pidfile():
_pidfile_dev = s.st_dev
_pidfile_ino = s.st_ino
def daemonize():
"""If configured with set_pidfile() or set_detach(), creates the pid file
and detaches from the foreground session."""
daemonize_start()
daemonize_complete()
def _waitpid(pid, options):
while True:
try:
@@ -202,6 +214,7 @@ def _waitpid(pid, options):
pass
return -e.errno, 0
def _fork_and_wait_for_startup():
try:
rfd, wfd = os.pipe()
@@ -250,6 +263,7 @@ def _fork_and_wait_for_startup():
_daemonize_fd = wfd
return pid
def _fork_notify_startup(fd):
if fd is not None:
error, bytes_written = ovs.socket_util.write_fully(fd, "0")
@@ -258,6 +272,7 @@ def _fork_notify_startup(fd):
sys.exit(1)
os.close(fd)
def _should_restart(status):
global RESTART_EXIT_CODE
@@ -271,6 +286,7 @@ def _should_restart(status):
return True
return False
def _monitor_daemon(daemon_pid):
# XXX should log daemon's stderr output at startup time
# XXX should use setproctitle module if available
@@ -316,6 +332,7 @@ def _monitor_daemon(daemon_pid):
# Running in new daemon process.
def _close_standard_fds():
"""Close stdin, stdout, stderr. If we're started from e.g. an SSH session,
then this keeps us from holding that session open artificially."""
@@ -325,6 +342,7 @@ def _close_standard_fds():
os.dup2(null_fd, 1)
os.dup2(null_fd, 2)
def daemonize_start():
"""If daemonization is configured, then starts daemonization, by forking
and returning in the child process. The parent process hangs around until
@@ -351,6 +369,7 @@ def daemonize_start():
if _pidfile:
_make_pidfile()
def daemonize_complete():
"""If daemonization is configured, then this function notifies the parent
process that the child process has completed startup successfully."""
@@ -362,6 +381,7 @@ def daemonize_complete():
os.chdir("/")
_close_standard_fds()
def usage():
sys.stdout.write("""
Daemon options:
@@ -371,6 +391,7 @@ Daemon options:
--overwrite-pidfile with --pidfile, start even if already running
""" % (ovs.dirs.RUNDIR, ovs.util.PROGRAM_NAME))
def __read_pidfile(pidfile, delete_if_stale):
if _pidfile_dev is not None:
try:
@@ -449,11 +470,13 @@ def __read_pidfile(pidfile, delete_if_stale):
except IOError:
pass
def read_pidfile(pidfile):
"""Opens and reads a PID from 'pidfile'. Returns the positive PID if
successful, otherwise a negative errno value."""
return __read_pidfile(pidfile, False)
def _check_already_running():
pid = __read_pidfile(_pidfile, True)
if pid > 0:
@@ -468,6 +491,7 @@ def _check_already_running():
LONG_OPTIONS = ["detach", "no-chdir", "pidfile", "pidfile-name=",
"overwrite-pidfile", "monitor"]
def parse_opt(option, arg):
if option == '--detach':
set_detach()