All of lore.kernel.org
 help / color / mirror / Atom feed
From: Horms <horms@verge.net.au>
To: xen-devel@lists.xensource.com
Subject: Re: [PATCH] setsid() exception in xend
Date: Mon, 28 Nov 2005 12:53:17 +0000 (UTC)	[thread overview]
Message-ID: <dmeujs$b27$1@sea.gmane.org> (raw)
In-Reply-To: 20051128113140.GB8909@leeni.uk.xensource.com

Ewan Mellor <ewan@xensource.com> wrote:
> On Mon, Nov 28, 2005 at 04:29:04AM +0000, Horms wrote:
> 
>> [Re: forking / setsid'ing patch]
> 
> Hi Horms,
> 
> How are you running Xend?  There's a call to fork in fork_pid, and no-one's
> had a problem with this or setsid before.

Hi Ewan,

at the time that I noticed the problem, my machine was doing some very
strange things that I won't go into. I can't actually reproduce the
exception now that I fixed the box up.

However, I do think that there is a minor problem. My previous patch
seemed to solve it, but I think it was slightly wrong, as you point out
by the time daemonize() is called, the code is already running as a
child.

The thing that I think is missing is that after calling setsid(),
fork() needs to be called again. This allows the session leader
(the process that called setsid()) to exit, and this its children
have no way of regaining the terminal.

Here is a slightly revised patch that puts a second fork() after
setsid() (rather than before as the previous incarnation did).
If you look at the output of ps you should with and without this
patch, and see that the assosiation with the terminal disapears.

As for why the previous patch worked, I'm not entirely sure.

# HG changeset patch
# User Horms <horms@verge.net.au>
# Node ID 86339c0ea955b837c3185d500d4ebbb3a5f448c3
# Parent  ddd958718cde22f20371a58359e173fd21c5da2e
[xend] Detach from terminal

* For setsid to effectivley 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 not that it actually disconnects
  from the terminal, and thus the prevailing process, the trace 
  looses the processes created in self.run().

diff -r ddd958718cde -r 86339c0ea955 tools/python/xen/xend/server/SrvDaemon.py
--- a/tools/python/xen/xend/server/SrvDaemon.py	Mon Nov 28 12:35:11 2005
+++ b/tools/python/xen/xend/server/SrvDaemon.py	Mon Nov 28 12:45:57 2005
@@ -121,10 +121,34 @@
 
         return self.child
 
-    def daemonize(self):
+    def daemonize(self, status):
         if not XEND_DAEMONIZE: return
+ 
         # Detach from TTY.
+
+	# Become the group leader (already a child process)
         os.setsid()
+ 
+        # Fork, this allows the group leader to exit,
+	# which means the child can never again regain control of the
+	# terminal
+        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 standard file descriptors, and redirect them to
         # /dev/null or the log as appropriate.
@@ -140,6 +164,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 +190,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 +204,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 +301,6 @@
 
             relocate.listenRelocation()
             servers = SrvServer.create()
-            self.daemonize()
             servers.start(status)
         except Exception, ex:
             print >>sys.stderr, 'Exception starting xend:', ex

  reply	other threads:[~2005-11-28 12:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-26 11:43 [PATCH] setsid() exception in xend Horms
2005-11-28  4:29 ` Horms
2005-11-28 11:31   ` Ewan Mellor
2005-11-28 12:53     ` Horms [this message]
2005-11-28 13:37       ` Ewan Mellor
2005-11-28 13:58         ` Horms
2005-11-28 15:57           ` Ewan Mellor
2005-11-29  1:51             ` Horms
2005-11-30  2:34             ` Horms

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='dmeujs$b27$1@sea.gmane.org' \
    --to=horms@verge.net.au \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.