WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] Re: [PATCH] setsid() exception in xend

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] Re: [PATCH] setsid() exception in xend
From: Horms <horms@xxxxxxxxxxxx>
Date: Mon, 28 Nov 2005 04:29:04 +0000 (UTC)
Delivery-date: Mon, 28 Nov 2005 04:31:34 +0000
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
References: <dm9hpf$jhr$1@xxxxxxxxxxxxx>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
User-agent: tin/1.7.10-20050815 ("Grimsay") (UNIX) (Linux/2.6.12.6-xen0 (i686))
The previous patch was bogus, please ignore it.
This should be a bit better:

# HG changeset patch
# User Horms <horms@xxxxxxxxxxxx>
# Node ID 81daa5bcf2ee0a463755e7662606bb6650038f69
# Parent  ed749e5935bd8dc283852f4064569415118deeaf
[xend] Detach from terminal

* For setsid to effectivley detach a process from the terminal,
  the process must have forked.

  Setsid is run in the child process. 
  
  The parent process manages the status fd, as per its behaviour in 
  self.start(), and exits when the fd handling is complete.

  The output of ps axf verifies that xend and its subsequenbtly
  created child processes are detached from the terminal.

* The call to self.daemonize(), which now forks, is moved to
  run before self.tracing(), as not that it actually disconnects
  from the terminal, and thus the prevailing process, the trace 
  looses the processes created in self.run().

diff -r ed749e5935bd -r 81daa5bcf2ee tools/python/xen/xend/server/SrvDaemon.py
--- a/tools/python/xen/xend/server/SrvDaemon.py Mon Nov 28 04:06:14 2005
+++ b/tools/python/xen/xend/server/SrvDaemon.py Mon Nov 28 04:16:03 2005
@@ -121,8 +121,28 @@
 
         return self.child
 
-    def daemonize(self):
+    def daemonize(self, status):
         if not XEND_DAEMONIZE: return
+ 
+        # Fork to allow disconnection from TTY
+        r, w = os.pipe()
+        if self.fork_pid(XEND_PID_FILE):
+            os.close(w)
+            r = os.fdopen(r, 'r')
+            try:
+                s = r.read()
+            finally:
+                r.close()
+                if len(s):
+                    status.write(s)
+                    status.close()
+                self.exit()
+ 
+        # Child
+        os.close(r);
+        status.close();
+        status = os.fdopen(w, 'w')
+ 
         # Detach from TTY.
         os.setsid()
 
@@ -140,6 +160,8 @@
             os.dup(0)
             os.open(XEND_DEBUG_LOG, os.O_WRONLY|os.O_CREAT)
 
+       return status
+
         
     def start(self, trace=0):
         """Attempts to start the daemons.
@@ -164,7 +186,7 @@
         # we can avoid a race condition during startup
         
         r,w = os.pipe()
-        if self.fork_pid(XEND_PID_FILE):
+        if os.fork():
             os.close(w)
             r = os.fdopen(r, 'r')
             try:
@@ -178,8 +200,9 @@
         else:
             os.close(r)
             # Child
+            status = self.daemonize(os.fdopen(w, 'w'))
             self.tracing(trace)
-            self.run(os.fdopen(w, 'w'))
+            self.run(status)
 
         return ret
 
@@ -274,7 +297,6 @@
 
             relocate.listenRelocation()
             servers = SrvServer.create()
-            self.daemonize()
             servers.start(status)
         except Exception, ex:
             print >>sys.stderr, 'Exception starting xend:', ex


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel