From dbda529e81fbdd4952570f0762315ed1288433f0 Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Fri, 16 Nov 2012 18:50:23 +0000 Subject: [PATCH] Fix log rotation in daemon mode When daemon mode is started as root and the user is changed to a lower privilege user the log rotation breaks. This is because the log file is still owned by root. Now just before we switch user the log file user is changed to the new user. Fixes bug# 1079743 Change-Id: Ia35e44bf9c891ce85a66e64989f97d3195520891 --- README | 3 ++- libra/mgm/mgm.py | 3 +++ libra/worker/main.py | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README b/README index c2fb3e82..f0613e73 100644 --- a/README +++ b/README @@ -72,7 +72,8 @@ Basic commands: $ libra_worker --debug --nodaemon NOTE: When running the worker in daemon mode, you must make sure that the -directory where the PID file will be (-p/--pid option) exists and is writable +directory where the PID file will be (-p/--pid option) and the directory where +the log files will be written (-l/--logfile option) exists and is writable by the user/group specified with the --user and --group options. Also, the Python module used to start the daemon process does not like it when the PID file already exists at startup. diff --git a/libra/mgm/mgm.py b/libra/mgm/mgm.py index c2a895c5..66a77b16 100644 --- a/libra/mgm/mgm.py +++ b/libra/mgm/mgm.py @@ -265,6 +265,9 @@ def main(): except KeyError: logger.critical("Invalid user: %s" % args.user) return 1 + # NOTE(LinuxJedi): we are switching user so need to switch + # the ownership of the log file for rotation + os.chown(logger.handlers[0].baseFilename, context.uid, -1) if args.group: try: context.gid = grp.getgrnam(args.group).gr_gid diff --git a/libra/worker/main.py b/libra/worker/main.py index 904395a2..0dac3dfe 100644 --- a/libra/worker/main.py +++ b/libra/worker/main.py @@ -16,6 +16,7 @@ import eventlet eventlet.monkey_patch() import daemon +import os import daemon.pidfile import grp import pwd @@ -137,6 +138,9 @@ def main(): except KeyError: logger.critical("Invalid user: %s" % args.user) return 1 + # NOTE(LinuxJedi): we are switching user so need to switch + # the ownership of the log file for rotation + os.chown(logger.handlers[0].baseFilename, context.uid, -1) if args.group: try: context.gid = grp.getgrnam(args.group).gr_gid