deluge/main.py | 49 ++++++++++++++++++++++++++++++++----------------- 1 files changed, 32 insertions(+), 17 deletions(-) diff --git a/deluge/main.py b/deluge/main.py index 12959a1..0a07f70 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -42,6 +42,8 @@ import os import sys +import pwd +import grp from optparse import OptionParser import deluge.log @@ -157,6 +159,10 @@ def start_daemon(): help="Set the logfile location", action="store", type="str") parser.add_option("-P", "--pidfile", dest="pidfile", help="Use pidfile to store process id", action="store", type="str") + parser.add_option("-U", "--user", dest="user", + help="User to switch to. Only use it when starting as root", action="store", type="str") + parser.add_option("-g", "--group", dest="group", + help="Group to switch to. Only use it when starting as root", action="store", type="str") parser.add_option("-L", "--loglevel", dest="loglevel", help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str") parser.add_option("-q", "--quiet", dest="quiet", @@ -196,25 +202,34 @@ def write_pidfile(): if options.pidfile: open(options.pidfile, "wb").write("%d\n" % os.getpid()) + # Disable fork on systems that don't support them + if deluge.common.windows_check() or deluge.common.osx_check(): + options.donot = true + # If the donot daemonize is set, then we just skip the forking if not options.donot: - # Windows check, we log to the config folder by default - if deluge.common.windows_check() or deluge.common.osx_check(): - open_logfile() - write_pidfile() - else: - if os.fork() == 0: - os.setsid() - if os.fork() == 0: - open_logfile() - write_pidfile() - else: - os._exit(0) - else: - os._exit(0) - else: - # Do not daemonize - write_pidfile() + if os.fork(): + # We've forked and this is now the parent process, so die! + os._exit(0) + os.setsid() + # Kill second fork + if os.fork(): + os._exit(0) + + # Write pid file before chuid + write_pidfile() + + if not deluge.common.windows_check(): + if options.user: + if not options.user.isdigit(): + options.user = pwd.getpwnam(options.user)[2] + os.setuid(options.user) + if options.group: + if not options.group.isdigit(): + options.group = grp.getgrnam(options.group)[2] + os.setuid(options.group) + + open_logfile() # Setup the logger try: