Module: deluge
Branch: master
Commit: 9a54beef78d8fbfc0eb55162541b6ca908879007

Author: cinderblock <>
Date:   Fri Feb  4 19:43:27 2011 +0000

add patch from #1473

---

 deluge/main.py       |   52 ++++++++++++++++++++++++++++++-------------------
 deluge/ui/web/web.py |   35 ++++++++++++++++++++++++++++----
 2 files changed, 62 insertions(+), 25 deletions(-)

diff --git a/deluge/main.py b/deluge/main.py
index dfb489d..b52988d 100644
--- a/deluge/main.py
+++ b/deluge/main.py
@@ -149,14 +149,20 @@ this should be an IP address", metavar="IFACE",
     parser.add_option("-u", "--ui-interface", dest="ui_interface",
         help="Interface daemon will listen for UI connections on, this should 
be\
  an IP address", metavar="IFACE", action="store", type="str")
-    parser.add_option("-d", "--do-not-daemonize", dest="donot",
-        help="Do not daemonize", action="store_true", default=False)
+    if not (deluge.common.windows_check() or deluge.common.osx_check()):
+        parser.add_option("-d", "--do-not-daemonize", dest="donot",
+            help="Do not daemonize", action="store_true", default=False)
     parser.add_option("-c", "--config", dest="config",
         help="Set the config location", action="store", type="str")
     parser.add_option("-l", "--logfile", dest="logfile",
         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")
+    if not deluge.common.windows_check():
+        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",
@@ -197,24 +203,30 @@ this should be an IP address", metavar="IFACE",
             open(options.pidfile, "wb").write("%s\n" % os.getpid())
 
     # 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 not (deluge.common.windows_check() or deluge.common.osx_check() or 
options.donot):
+        if os.fork():
+            # We've forked and this is now the parent process, so die!
+            os._exit(0)
+        os.setsid()
+        # Do second fork
+        if os.fork():
+            os._exit(0)
+
+    # Write pid file before chuid
+    write_pidfile()
+
+    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)
+
+    open_logfile()
 
     # Setup the logger
     try:
diff --git a/deluge/ui/web/web.py b/deluge/ui/web/web.py
index a2b5765..23cabcd 100644
--- a/deluge/ui/web/web.py
+++ b/deluge/ui/web/web.py
@@ -56,9 +56,20 @@ class Web(_UI):
         group.add_option("-b", "--base", dest="base",
             help="Set the base path that the ui is running on (proxying)",
             action="store", default=None)
-        group.add_option("-f", "--fork", dest="fork",
-            help="Fork the web interface process into the background",
-            action="store_true", default=False)
+        if not (deluge.common.windows_check() or deluge.common.osx_check()):
+            group.add_option("-f", "--fork", dest="fork",
+                help="Fork the web interface process into the background",
+                action="store_true", default=False)
+        group.add_option("-P", "--pidfile", dest="pidfile", type="str",
+            help="Use pidfile to store process id",
+            action="store", default=None)
+        if not deluge.common.windows_check():
+            group.add_option("-U", "--user", dest="user", type="str",
+                help="User to switch to. Only use it when starting as root",
+                action="store", default=None)
+            group.add_option("-g", "--group", dest="group", type="str",
+                help="Group to switch to. Only use it when starting as root",
+                action="store", default=None)
         group.add_option("-p", "--port", dest="port", type="int",
             help="Sets the port to be used for the webserver",
             action="store", default=None)
@@ -86,7 +97,7 @@ class Web(_UI):
         import deluge.common
         # Steps taken from http://www.faqs.org/faqs/unix-faq/programmer/faq/
         # Section 1.7
-        if self.options.fork and not deluge.common.windows_check():
+        if self.options.fork:
             # fork() so the parent can exit, returns control to the command 
line
             # or shell invoking the program.
             if os.fork():
@@ -103,7 +114,21 @@ class Web(_UI):
             # use that may prevent a filesystem unmount.
             import deluge.configmanager
             os.chdir(deluge.configmanager.get_config_dir())
-        
+
+        if self.options.pidfile:
+            open(self.options.pidfile, "wb").write("%d\n" % os.getpid())
+
+        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)
+        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)
+
         import server
         self.__server = server.DelugeWeb()
 

-- 
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en.

Reply via email to