direct-io.hg

changeset 8304:efc71a3e9f6f

Detach Xend from terminal, courtesy of Horms <horms@verge.net.au>.

* For setsid to effectively detach a process from the terminal,
the following needs to occur:

fork () Return control to the shell
setsid () New session with no controlling terminal
fork () The session leader (parent) exits and thus
the resulting child process can never regain the terminal

This patch adds the second fork

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

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Dec 08 16:17:53 2005 +0000 (2005-12-08)
parents 52f214d983fb
children 1aaa1abab953
files tools/python/xen/xend/server/SrvDaemon.py
line diff
     1.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Thu Dec 08 16:11:48 2005 +0000
     1.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Thu Dec 08 16:17:53 2005 +0000
     1.3 @@ -123,9 +123,18 @@ class Daemon:
     1.4  
     1.5      def daemonize(self):
     1.6          if not XEND_DAEMONIZE: return
     1.7 + 
     1.8          # Detach from TTY.
     1.9 +
    1.10 +        # Become the group leader (already a child process)
    1.11          os.setsid()
    1.12  
    1.13 +        # Fork, this allows the group leader to exit,
    1.14 +        # which means the child can never again regain control of the
    1.15 +        # terminal
    1.16 +        if self.fork_pid(XEND_PID_FILE):
    1.17 +            self.exit()
    1.18 + 
    1.19          # Detach from standard file descriptors, and redirect them to
    1.20          # /dev/null or the log as appropriate.
    1.21          os.close(0)
    1.22 @@ -164,7 +173,7 @@ class Daemon:
    1.23          # we can avoid a race condition during startup
    1.24          
    1.25          r,w = os.pipe()
    1.26 -        if self.fork_pid(XEND_PID_FILE):
    1.27 +        if os.fork():
    1.28              os.close(w)
    1.29              r = os.fdopen(r, 'r')
    1.30              try:
    1.31 @@ -178,6 +187,7 @@ class Daemon:
    1.32          else:
    1.33              os.close(r)
    1.34              # Child
    1.35 +            self.daemonize()
    1.36              self.tracing(trace)
    1.37              self.run(os.fdopen(w, 'w'))
    1.38  
    1.39 @@ -274,7 +284,6 @@ class Daemon:
    1.40  
    1.41              relocate.listenRelocation()
    1.42              servers = SrvServer.create()
    1.43 -            self.daemonize()
    1.44              servers.start(status)
    1.45          except Exception, ex:
    1.46              print >>sys.stderr, 'Exception starting xend:', ex