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
 |