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
This commit is contained in:
Andrew Hutchings 2012-11-16 18:50:23 +00:00
parent d01c2a82c1
commit dbda529e81
3 changed files with 9 additions and 1 deletions

3
README
View File

@ -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.

View File

@ -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

View File

@ -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