Only register exit function if a PID file was created

The function only deletes the PID file.
This commit is contained in:
Unrud 2017-05-30 07:41:50 +02:00
parent 6762fc1cad
commit 9cd9ad9a02

View File

@ -116,7 +116,6 @@ def run():
def daemonize(configuration, logger): def daemonize(configuration, logger):
"""Fork and decouple if Radicale is configured as daemon.""" """Fork and decouple if Radicale is configured as daemon."""
if configuration.getboolean("server", "daemon"):
# Check and create PID file in a race-free manner # Check and create PID file in a race-free manner
if configuration.get("server", "pid"): if configuration.get("server", "pid"):
try: try:
@ -136,6 +135,14 @@ def daemonize(configuration, logger):
sys.exit() sys.exit()
if configuration.get("server", "pid"): if configuration.get("server", "pid"):
os.close(pid_fd) os.close(pid_fd)
# Register exit function
def cleanup():
"""Remove the PID files."""
logger.debug("Cleaning up")
# Remove PID file
os.unlink(pid_path)
atexit.register(cleanup)
# Decouple environment # Decouple environment
os.chdir("/") os.chdir("/")
os.setsid() os.setsid()
@ -145,17 +152,6 @@ def daemonize(configuration, logger):
os.dup2(null_out.fileno(), sys.stdout.fileno()) os.dup2(null_out.fileno(), sys.stdout.fileno())
os.dup2(null_out.fileno(), sys.stderr.fileno()) os.dup2(null_out.fileno(), sys.stderr.fileno())
# Register exit function
def cleanup():
"""Remove the PID files."""
logger.debug("Cleaning up")
# Remove PID file
if (configuration.get("server", "pid") and
configuration.getboolean("server", "daemon")):
os.unlink(pid_path)
atexit.register(cleanup)
def serve(configuration, logger): def serve(configuration, logger):
"""Serve radicale from configuration.""" """Serve radicale from configuration."""
@ -234,6 +230,7 @@ def serve(configuration, logger):
else: else:
# Fallback to busy waiting # Fallback to busy waiting
select_timeout = 1.0 select_timeout = 1.0
if configuration.getboolean("server", "daemon"):
daemonize(configuration, logger) daemonize(configuration, logger)
logger.debug("Radicale server ready") logger.debug("Radicale server ready")
while not shutdown_program: while not shutdown_program: