commit-gnue
[Top][All Lists]
Advanced

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

gnue/gnue-common/src GServerApp.py


From: Jason Cater
Subject: gnue/gnue-common/src GServerApp.py
Date: Wed, 24 Oct 2001 14:39:59 -0400

CVSROOT:        /home/cvs
Module name:    gnue
Changes by:     Jason Cater <address@hidden>    01/10/24 14:39:58

Modified files:
        gnue-common/src: GServerApp.py 

Log message:
        added daemon/forking code to GServerApp; misc other fixes

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/GServerApp.py.diff?cvsroot=OldCVS&tr1=1.2&tr2=1.3&r1=text&r2=text

Patches:
Index: gnue/gnue-common/src/GServerApp.py
diff -u gnue/gnue-common/src/GServerApp.py:1.2 
gnue/gnue-common/src/GServerApp.py:1.3
--- gnue/gnue-common/src/GServerApp.py:1.2      Thu Sep 27 00:32:52 2001
+++ gnue/gnue-common/src/GServerApp.py  Wed Oct 24 14:39:58 2001
@@ -31,14 +31,25 @@
 #
 
 from GBaseApp import GBaseApp
+from GLogger import Logger
+import sys, os
 
 # TODO: Implement RPC abstraction layer
-# TODO: Implement spawning/daemon support
 # TODO: Implement either multi-threading or asynchronous IO support
 
 class GServerApp(GBaseApp):
 
+  def __init__(self, connections=None, application=None):
+    self.COMMAND_OPTIONS.append (
+         [ 'foreground','Z','no-detach',0,0, None,
+           'Do not send the server into the background. For a POSIX system, '
+           'this option keeps the server process from forking and detaching '
+           'from its controlling terminal.'],
+    )
+    GBaseApp.__init__(self, connections, application)
 
+
+
   # This can be overwritten by code necessary
   # for startup.  If overwritten, do not first
   # call the original GServerApp.run(self) as
@@ -48,8 +59,75 @@
   # startup code and are ready to go to server
   # mode.
   def run(self):
+
+    # Fork, if applicable/possible, and send to background
+    if not self.OPTIONS["foreground"]: 
+      self.daemonize()
 
-    # TODO: Fork(?) and send to background
+
+  # Called when a request to shutdown the server is received
+  def shutdown(self): 
     pass
+
+
+  # Turn ourselves into a daemon/service/etc. 
+  # Returns 1 if program successfully converted,
+  # 0 otherwise. 
+  # 
+  def daemonize(self): 
+
+    try: 
+      # Fork #1
+      pid = os.fork()
+      if pid != 0:
+        # Close main process
+        sys.exit(0)
+
+      # Open new filehandles for stdin, stdout, stderr
+      sin = open('/dev/null','r')
+      sout = open('/dev/null','w')
+      serr = open('/dev/null','w')
+
+    except AttributeError: 
+      return 0
+    except IOError: 
+      return 0
+    except OSError:
+      return 0
+
+
+    # Disassociate ourselves
+    try: 
+      os.chdir('/')
+      os.setsid()
+      os.umask(0)
+    except AttributeError: 
+      pass
+    except OSError:
+      pass
+
+
+    try: 
+      # Fork #2
+      pid = os.fork()
+      if pid != 0: 
+        sys.exit(0)
+    except OSError: 
+      pass
+
+
+    try: 
+      sys.stdin.close()
+      sys.stdin = sin
+
+      sys.stdout.close()
+      sys.stdout = sout
+
+      sys.stderr.close()
+      sys.stderr = serr
+    except AttributeError: 
+      pass
+
+    return 1
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]