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

Re: [Xen-devel] Re: [Xen-changelog] Detach Xend from terminal, courtesy

To: Ewan Mellor <ewan@xxxxxxxxxxxxx>
Subject: Re: [Xen-devel] Re: [Xen-changelog] Detach Xend from terminal, courtesy of Horms <horms@xxxxxxxxxxxx>.
From: Anthony Liguori <aliguori@xxxxxxxxxx>
Date: Fri, 09 Dec 2005 15:35:27 -0600
Cc: xen-devel@xxxxxxxxxxxxxxxxxxx
Delivery-date: Fri, 09 Dec 2005 21:36:30 +0000
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
In-reply-to: <20051209211622.GA12700@xxxxxxxxxxxxxxxxxxxxxx>
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: <E1EkjTm-0004PC-NQ@xxxxxxxxxxxxxxxxxxxxx> <4399ED6F.1050500@xxxxxxxxxx> <20051209211622.GA12700@xxxxxxxxxxxxxxxxxxxxxx>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
User-agent: Mozilla Thunderbird 1.0.7 (X11/20051013)
Ewan Mellor wrote:

I don't quite understand why two fork()s would be required to properly daemonize. I don't know of any other daemons that do that (certainly, xenstored and xenconsoled don't).

It's a standard technique on Unixen.  You need to fork before calling setsid
to ensure that you are not a process group leader, then afterwards to ensure
that children cannot regain the terminal.  See
I see, I've only ever needed this behavior once and did something embarrassingly more complex to achieve it :-) While not necessary for xenconsoled and xenstored, it certainly doesn't hurt.

http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

(If xenstored and xenconsoled aren't doing this, then they ought to.)
Patch is attached.

We have preserved the behaviour whereby the Xend process does not exit until it
is ready to accept connections -- it's just one fork further on (and there's
a patch with another fork on its way, for your amusement, but that's a different
story ;-)
So that Xend can restart itself? Yeah, I saw that one :-) I'll reserve a commen there. Thanks for the explaination!

Regards,

Anthony Liguori

Ewan.

# HG changeset patch
# User Anthony Liguori <anthony@xxxxxxxxxxxxx>
# Node ID e55633c669d11b48cf16d0ddaebbb836d7b3f5f6
# Parent  53cff3f88e45cb5230da39f86d84b6606da0cdbb
Make sure to fork again after setsid() so that child cannot regain CTTY.

Signed-off-by: Anthony Liguori <aliguori@xxxxxxxxxx>

diff -r 53cff3f88e45 -r e55633c669d1 tools/console/daemon/utils.c
--- a/tools/console/daemon/utils.c      Fri Dec  9 11:05:06 2005 +0000
+++ b/tools/console/daemon/utils.c      Fri Dec  9 16:33:01 2005 -0500
@@ -90,6 +90,12 @@
 
        setsid();
 
+       if ((pid = fork()) > 0) {
+               exit(0);
+       } else if (pid == -1) {
+               err(errno, "fork() failed");
+       }
+
        /* redirect fd 0,1,2 to /dev/null */
        if ((fd = open("/dev/null",O_RDWR)) == -1) {
                exit(1);
diff -r 53cff3f88e45 -r e55633c669d1 tools/xenstore/xenstored_core.c
--- a/tools/xenstore/xenstored_core.c   Fri Dec  9 11:05:06 2005 +0000
+++ b/tools/xenstore/xenstored_core.c   Fri Dec  9 16:33:01 2005 -0500
@@ -1491,6 +1491,13 @@
 
        /* Session leader so ^C doesn't whack us. */
        setsid();
+
+       /* Let session leader exit so child cannot regain CTTY */
+       if ((pid = fork()) < 0)
+               barf_perror("Failed to fork daemon");
+       if (pid != 0)
+               exit(0);
+
 #ifndef TESTING        /* Relative paths for socket names */
        /* Move off any mount points we might be in. */
        chdir("/");
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel