]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Fri Feb 23 12:46:44 IST 2007 Mark McLoughlin <markmc@redhat.com>
authorMark McLoughlin <markmc@redhat.com>
Fri, 23 Feb 2007 12:48:36 +0000 (12:48 +0000)
committerMark McLoughlin <markmc@redhat.com>
Fri, 23 Feb 2007 12:48:36 +0000 (12:48 +0000)
        * qemud/qemud.c: add --pid-file and default to writing
        out a PID file to /var/run/libvirt_qemud.pid in daemon
        mode.

        * configure.in: add --with-pid-file arg

ChangeLog
configure.in
qemud/qemud.c

index b021522163b230a8d95a2d33544eceaf2fa7dbbd..761f806295d0c69fd9aa4f8926da4af1da59cc96 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Fri Feb 23 12:46:44 IST 2007 Mark McLoughlin <markmc@redhat.com>
+
+       * qemud/qemud.c: add --pid-file and default to writing
+       out a PID file to /var/run/libvirt_qemud.pid in daemon
+       mode.
+
+       * configure.in: add --with-pid-file arg
+       
 Fri Feb 23 12:45:08 IST 2007 Mark McLoughlin <markmc@redhat.com>
 
        * qemud/Makefile.am: install libvirt_qemud in /usr/sbin
index 20f9b914ad73c7a3d0573ba219098e9502262216..618bbe73c329787bdadc19f748fc6b3f85d6f633 100644 (file)
@@ -86,6 +86,21 @@ if test x"$enable_debug" = x"yes"; then
    AC_DEFINE(ENABLE_DEBUG, [], [whether debugging is enabled])
 fi
 
+dnl
+dnl PID file
+dnl
+AC_MSG_CHECKING([where to write libvirt_qemud PID file])
+AC_ARG_WITH(pid-file, AC_HELP_STRING([--with-qemud-pid-file=[pidfile|none]], [PID file for libvirt_qemud]))
+if test "x$with_qemud_pid_file" == "x" ; then
+   QEMUD_PID_FILE="$localstatedir/run/libvirt_qemud.pid"
+elif test "x$with_qemud_pid_file" == "xnone" ; then
+   QEMUD_PID_FILE=""
+else
+   QEMUD_PID_FILE="$with_qemud_pid_file"
+fi
+AC_DEFINE_UNQUOTED(QEMUD_PID_FILE, "$QEMUD_PID_FILE", [PID file path for qemud])
+AC_MSG_RESULT($QEMUD_PID_FILE)
+
 dnl
 dnl allow the creation of iptables rules in chains with a
 dnl specific prefix rather than in the standard toplevel chains
index a66a0dd6c60f8e2487f90996f4bd70a33c781a68..bcfc613b1d285ead7aeb67ff1c7bd0789cf1abb5 100644 (file)
@@ -301,6 +301,42 @@ static int qemudGoDaemon(void) {
     }
 }
 
+static int qemudWritePidFile(const char *pidFile) {
+    int fd;
+    FILE *fh;
+
+    if (pidFile[0] == '\0')
+        return 0;
+
+    if ((fd = open(pidFile, O_WRONLY|O_CREAT|O_EXCL, 0644)) < 0) {
+        qemudLog(QEMUD_ERR, "Failed to open pid file '%s' : %s",
+                 pidFile, strerror(errno));
+        return -1;
+    }
+
+    if (!(fh = fdopen(fd, "w"))) {
+        qemudLog(QEMUD_ERR, "Failed to fdopen pid file '%s' : %s",
+                 pidFile, strerror(errno));
+        close(fd);
+        return -1;
+    }
+
+    if (fprintf(fh, "%lu\n", (unsigned long)getpid()) < 0) {
+        qemudLog(QEMUD_ERR, "Failed to write to pid file '%s' : %s",
+                 pidFile, strerror(errno));
+        close(fd);
+        return -1;
+    }
+
+    if (fclose(fh) == EOF) {
+        qemudLog(QEMUD_ERR, "Failed to close pid file '%s' : %s",
+                 pidFile, strerror(errno));
+        return -1;
+    }
+
+    return 0;
+}
+
 static int qemudListenUnix(struct qemud_server *server,
                            const char *path, int readonly) {
     struct qemud_socket *sock = calloc(1, sizeof(struct qemud_socket));
@@ -1483,12 +1519,15 @@ int main(int argc, char **argv) {
     struct qemud_server *server;
     struct sigaction sig_action;
     int sigpipe[2];
+    char *pid_file = NULL;
+    int ret = 1;
 
     struct option opts[] = {
         { "verbose", no_argument, &verbose, 1},
         { "daemon", no_argument, &godaemon, 1},
         { "system", no_argument, &sys, 1},
         { "timeout", required_argument, 0, 't'},
+        { "pid-file", required_argument, 0, 'p'},
         {0, 0, 0, 0}
     };
 
@@ -1497,7 +1536,7 @@ int main(int argc, char **argv) {
         int c;
         char *tmp;
 
-        c = getopt_long(argc, argv, "vsdt:", opts, &optidx);
+        c = getopt_long(argc, argv, "vsdt:p:", opts, &optidx);
 
         if (c == -1) {
             break;
@@ -1524,6 +1563,11 @@ int main(int argc, char **argv) {
             if (timeout <= 0)
                 timeout = -1;
             break;
+
+        case 'p':
+            pid_file = strdup(optarg);
+            break;
+
         case '?':
             return 2;
             break;
@@ -1541,7 +1585,7 @@ int main(int argc, char **argv) {
         qemudSetNonBlock(sigpipe[1]) < 0) {
         qemudLog(QEMUD_ERR, "Failed to create pipe: %s",
                  strerror(errno));
-        return 1;
+        goto error1;
     }
 
     sigwrite = sigpipe[1];
@@ -1564,14 +1608,19 @@ int main(int argc, char **argv) {
         if (pid < 0) {
             qemudLog(QEMUD_ERR, "Failed to fork as daemon: %s",
                      strerror(errno));
-            return 1;
+            goto error1;
         }
         if (pid > 0)
-            return 0;
+            goto out;
+
+        if (qemudWritePidFile(pid_file ? pid_file : QEMUD_PID_FILE) < 0)
+            goto error1;
     }
 
-    if (!(server = qemudInitialize(sys, sigpipe[0])))
-        return 2;
+    if (!(server = qemudInitialize(sys, sigpipe[0]))) {
+        ret = 2;
+        goto error2;
+    }
 
     qemudRunLoop(server, timeout);
 
@@ -1582,7 +1631,18 @@ int main(int argc, char **argv) {
     if (godaemon)
         closelog();
 
-    return 0;
+ out:
+    ret = 0;
+
+ error2:
+    if (godaemon)
+        unlink(pid_file ? pid_file : QEMUD_PID_FILE);
+
+ error1:
+    if (pid_file)
+        free(pid_file);
+
+    return ret;
 }
 
 /*