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-tools

[Xen-tools] [PATCH] xenstored: fix pid writing

To: Xen Tools <xen-tools@xxxxxxxxxxxxxxxxxxx>
Subject: [Xen-tools] [PATCH] xenstored: fix pid writing
From: Rusty Russell <rusty@xxxxxxxxxxxxxxx>
Date: Fri, 05 Aug 2005 14:21:56 +1000
Delivery-date: Fri, 05 Aug 2005 04:20:18 +0000
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-tools-request@lists.xensource.com?subject=help>
List-id: Xen control tools developers <xen-tools.lists.xensource.com>
List-post: <mailto:xen-tools@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-tools>, <mailto:xen-tools-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-tools>, <mailto:xen-tools-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-tools-bounces@xxxxxxxxxxxxxxxxxxx
When pid writing fails, we've already closed stderr, so all we can do is
fail silently.  We should do the pid writing first.

Secondly, we shouldn't do it for xenstored_test, the test version of the
daemon.

Finally, we should hand the permissions to open() since we're using
O_CREAT.

Signed-off-by: Rusty Russell <rusty@xxxxxxxxxxxxxxx>


diff -r 61cbf8f977ef tools/xenstore/utils.c
--- a/tools/xenstore/utils.c    Thu Aug  4 18:51:55 2005
+++ b/tools/xenstore/utils.c    Fri Aug  5 14:20:08 2005
@@ -84,9 +84,6 @@
 void daemonize(void)
 {
        pid_t pid;
-       int fd;
-       size_t len;
-       char buf[100];
 
        /* Separate from our parent via fork, so init inherits us. */
        if ((pid = fork()) < 0)
@@ -104,18 +101,6 @@
        chdir("/");
        /* Discard our parent's old-fashioned umask prejudices. */
        umask(0);
-
-       fd = open("/var/run/xenstored.pid", O_RDWR | O_CREAT);
-       if (fd == -1) {
-               exit(1);
-       }
-
-       if (lockf(fd, F_TLOCK, 0) == -1) {
-               exit(1);
-       }
-
-       len = sprintf(buf, "%d\n", getpid());
-       write(fd, buf, len);
 }
 
 
diff -r 61cbf8f977ef tools/xenstore/xenstored_core.c
--- a/tools/xenstore/xenstored_core.c   Thu Aug  4 18:51:55 2005
+++ b/tools/xenstore/xenstored_core.c   Fri Aug  5 14:20:08 2005
@@ -1612,6 +1612,19 @@
        /* Debugging: daemonize() closes standard fds, so dup here. */
        tmpout = dup(STDOUT_FILENO);
        if (dofork) {
+#ifndef TESTING
+               size_t len;
+               char buf[100];
+               int fd = open("/var/run/xenstored.pid", O_RDWR|O_CREAT, 0600);
+               if (fd == -1)
+                       barf_perror("Failed to open /var/run/xenstored.pid");
+
+               if (lockf(fd, F_TLOCK, 0) == -1)
+                       barf("Pidfile locked: daemon already running?");
+
+               len = sprintf(buf, "%d\n", getpid());
+               write(fd, buf, len);
+#endif
                openlog("xenstored", 0, LOG_DAEMON);
                daemonize();
        }

-- 
A bad analogy is like a leaky screwdriver -- Richard Braakman


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

<Prev in Thread] Current Thread [Next in Thread>