[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[minion-cvs] Modified patch from Peter Palfrader: Avoid becoming ses...



Update of /home/minion/cvsroot/src/minion/lib/mixminion/server
In directory moria.mit.edu:/tmp/cvs-serv5754/lib/mixminion/server

Modified Files:
	ServerMain.py 
Log Message:
Modified patch from Peter Palfrader: Avoid becoming session group leader, and really close FDs on Daemonize.

Index: ServerMain.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/ServerMain.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- ServerMain.py	6 Jan 2003 03:29:46 -0000	1.19
+++ ServerMain.py	6 Jan 2003 22:09:24 -0000	1.20
@@ -392,18 +392,27 @@
     if hasattr(os, 'setsid'):
         # Setsid is not available everywhere.
         os.setsid()
+        # Fork again so the parent, (the session group leader), can exit. This
+        # means that we, as a non-session group leader, can never regain a
+        # controlling terminal.
+        pid = os.fork()
+        if pid != 0:
+            os._exit(0)
     # Chdir to / so that we don't hold the CWD unnecessarily.
     os.chdir(os.path.normpath("/")) #???? Is this right on Win32?
     # Set umask to 000 so that we drop any (possibly nutty) umasks that
     # our users had before.
     os.umask(0000)
     # Close all unused fds.
-    sys.stderr.close()
-    sys.stdout.close()
-    sys.stdin.close()
+    # (We could try to do this via sys.stdin.close() etc., but then we
+    #  would miss the magic copies in sys.__stdin__, sys.__stdout__, etc.
+    #  Using os.close instead just nukes the FD for us.)
+    os.close(sys.stdin.fileno())
+    os.close(sys.stdout.fileno())
+    os.close(sys.stderr.fileno())
     # Override stdout and stderr in case some code tries to use them
-    sys.stdout = LogStream("STDOUT", "WARN")
-    sys.stderr = LogStream("STDERR", "WARN")
+    sys.stdout = sys.__stdout__ = LogStream("STDOUT", "WARN")
+    sys.stderr = sys.__stderr__ = LogStream("STDERR", "WARN")
 
 _SERVER_USAGE = """\
 Usage: %s [options]
@@ -462,7 +471,13 @@
 
     if config['Server'].get("Daemon",1):
         print >>sys.stderr, "Starting server in the background"
-        daemonize()
+        try:
+            daemonize()
+        except:
+            info = sys.exc_info()
+            LOG.fatal_exc(info,
+                          "Exception while starting server in the background")
+            os._exit(0)
 
     LOG.info("Starting server")
     try: