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