diff --git a/deluge/core/daemon_entry.py b/deluge/core/daemon_entry.py index 1c00586a9..e828fdc8a 100644 --- a/deluge/core/daemon_entry.py +++ b/deluge/core/daemon_entry.py @@ -13,7 +13,7 @@ import os import sys from logging import FileHandler, getLogger -from deluge.common import run_profiled, windows_check +from deluge.common import run_profiled from deluge.configmanager import get_config_dir from deluge.error import DaemonRunningError from deluge.ui.baseargparser import BaseArgParser @@ -28,17 +28,9 @@ def add_daemon_options(parser): help=_("Port to listen for UI connections on")) group.add_argument("-i", "--interface", metavar="", dest="listen_interface", action="store", help=_("IP address to listen for BitTorrent connections")) - group.add_argument("-P", "--pidfile", metavar="", action="store", - help=_("Pidfile to store the process id")) - if not windows_check(): - group.add_argument("-d", "--do-not-daemonize", dest="donot", action="store_true", - help=_("Do not daemonize (fork) this process")) - group.add_argument("-U", "--user", metavar="", action="store", - help=_("Change to this user on startup (Requires root)")) - group.add_argument("-g", "--group", metavar="", action="store", - help=_("Change to this group on startup (Requires root)")) group.add_argument("--read-only-config-keys", metavar="", action="store", help=_("Config keys to be unmodified by `set_config` RPC"), type=str, default="") + parser.add_process_arg_group() def start_daemon(skip_start=False): @@ -78,34 +70,6 @@ def start_daemon(skip_start=False): file_handler = FileHandler(options.logfile) log.addHandler(file_handler) - # If the donot daemonize is set, then we just skip the forking - if not (windows_check() or options.donot): - if os.fork(): - os._exit(0) - os.setsid() - # Do second fork - if os.fork(): - os._exit(0) - # Ensure process doesn't keep any directory in use that may prevent a filesystem unmount. - os.chdir(get_config_dir()) - - # Write pid file before chuid - if options.pidfile: - with open(options.pidfile, "wb") as _file: - _file.write("%d\n" % os.getpid()) - - if not windows_check(): - if options.user: - if not options.user.isdigit(): - import pwd - options.user = pwd.getpwnam(options.user)[2] - os.setuid(options.user) - if options.group: - if not options.group.isdigit(): - import grp - options.group = grp.getgrnam(options.group)[2] - os.setuid(options.group) - def run_daemon(options): try: from deluge.core.daemon import Daemon diff --git a/deluge/ui/baseargparser.py b/deluge/ui/baseargparser.py index 6996a5695..46dc38c5f 100644 --- a/deluge/ui/baseargparser.py +++ b/deluge/ui/baseargparser.py @@ -14,9 +14,9 @@ import platform import sys import textwrap -import deluge.configmanager import deluge.log from deluge import common +from deluge.configmanager import get_config_dir, set_config_dir from deluge.log import setup_logger @@ -139,7 +139,7 @@ class BaseArgParser(argparse.ArgumentParser): logrotate=logrotate) if options.config: - if not deluge.configmanager.set_config_dir(options.config): + if not set_config_dir(options.config): log = logging.getLogger(__name__) log.error("There was an error setting the config dir! Exiting..") sys.exit(1) @@ -147,4 +147,50 @@ class BaseArgParser(argparse.ArgumentParser): if not os.path.exists(common.get_default_config_dir()): os.makedirs(common.get_default_config_dir()) + if self.process_group: + # If donotdaemonize is set, skip process forking. + if not (common.windows_check() or options.donotdaemonize): + if os.fork(): + os._exit(0) + os.setsid() + # Do second fork + if os.fork(): + os._exit(0) + # Ensure process doesn't keep any directory in use that may prevent a filesystem unmount. + os.chdir(get_config_dir()) + + # Write pid file before chuid + if options.pidfile: + with open(options.pidfile, "wb") as _file: + _file.write("%d\n" % os.getpid()) + + if not common.windows_check(): + if options.user: + if not options.user.isdigit(): + import pwd + options.user = pwd.getpwnam(options.user)[2] + os.setuid(options.user) + if options.group: + if not options.group.isdigit(): + import grp + options.group = grp.getgrnam(options.group)[2] + os.setuid(options.group) + return options + + def add_process_arg_group(self): + """Adds a grouping of common process args to control a daemon to the parser""" + + self.process_group = True + self.group = self.add_argument_group(_("Process Control Options")) + self.group.add_argument("-P", "--pidfile", metavar="", action="store", + help=_("Pidfile to store the process id")) + if not common.windows_check(): + self.group.add_argument("-d", "--do-not-daemonize", dest="donotdaemonize", action="store_true", + help=_("Do not daemonize (fork) this process")) + self.group.add_argument("-f", "--fork", dest="donotdaemonize", action="store_false", + help=argparse.SUPPRESS) # Deprecated arg + self.group.add_argument("-U", "--user", metavar="", action="store", + help=_("Change to this user on startup (Requires root)")) + self.group.add_argument("-g", "--group", metavar="", action="store", + help=_("Change to this group on startup (Requires root)")) diff --git a/deluge/ui/web/web.py b/deluge/ui/web/web.py index 3652f5a2a..48ad9b7c2 100644 --- a/deluge/ui/web/web.py +++ b/deluge/ui/web/web.py @@ -9,12 +9,9 @@ from __future__ import print_function -import argparse import logging -import os -from deluge.common import run_profiled, windows_check -from deluge.configmanager import get_config_dir +from deluge.common import run_profiled from deluge.ui.ui import UI log = logging.getLogger(__name__) @@ -41,17 +38,6 @@ class Web(UI): help=_("IP address for web server to listen on")) group.add_argument("-p", "--port", metavar="", type=int, action="store", help=_("Port for web server to listen on")) - group.add_argument("-P", "--pidfile", metavar="", action="store", - help=_("Pidfile to store the process id")) - if not windows_check(): - group.add_argument("-d", "--do-not-daemonize", dest="donotdaemonize", action="store_true", - help=_("Do not daemonize (fork) this process")) - group.add_argument("-f", "--fork", dest="donotdaemonize", action="store_false", - help=argparse.SUPPRESS) # Deprecated arg - group.add_argument("-U", "--user", metavar="", action="store", - help=_("Change to this user on startup (Requires root)")) - group.add_argument("-g", "--group", metavar="", action="store", - help=_("Change to this group on startup (Requires root)")) group.add_argument("-b", "--base", metavar="", action="store", help=_("Set the base path that the ui is running on")) try: @@ -62,6 +48,7 @@ class Web(UI): else: group.add_argument("--ssl", action="store_true", help=_("Force the web server to use SSL")) group.add_argument("--no-ssl", action="store_true", help=_("Force the web server to disable SSL")) + self.parser.add_process_arg_group() @property def server(self): @@ -70,34 +57,6 @@ class Web(UI): def start(self, args=None): super(Web, self).start(args) - # If donotdaemonize is set, skip process forking. - if not (windows_check() or self.options.donotdaemonize): - if os.fork(): - os._exit(0) - os.setsid() - # Do second fork - if os.fork(): - os._exit(0) - # Ensure process doesn't keep any directory in use that may prevent a filesystem unmount. - os.chdir(get_config_dir()) - - # Write pid file before chuid - if self.options.pidfile: - with open(self.options.pidfile, "wb") as _file: - _file.write("%d\n" % os.getpid()) - - if not windows_check(): - if self.options.user: - if not self.options.user.isdigit(): - import pwd - self.options.user = pwd.getpwnam(self.options.user)[2] - os.setuid(self.options.user) - if self.options.group: - if not self.options.group.isdigit(): - import grp - self.options.group = grp.getgrnam(self.options.group)[2] - os.setuid(self.options.group) - from deluge.ui.web import server self.__server = server.DelugeWeb(options=self.options)