[patch 2/3] Add ability to write PID file to qemud

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a --pid-file arg to libvirt_qemud and, in daemon mode,
default to writing a PID file. Passing --pid-file="" disables
writing a PID file.

Also add a --with-qemud-pid-file arg to configure. Passing
--with-pid-file=none disables the default PID file path.

The default path is /var/run/libvirt_qemud.pid in order
to match the service name.

Signed-off-by: Mark McLoughlin <markmc@xxxxxxxxxx>

Index: libvirt/configure.in
===================================================================
--- libvirt.orig/configure.in
+++ libvirt/configure.in
@@ -87,6 +87,21 @@ if test x"$enable_debug" = x"yes"; then
 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
 dnl
Index: libvirt/qemud/qemud.c
===================================================================
--- libvirt.orig/qemud/qemud.c
+++ libvirt/qemud/qemud.c
@@ -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));
@@ -1481,12 +1517,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}
     };
 
@@ -1495,7 +1534,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;
@@ -1522,6 +1561,11 @@ int main(int argc, char **argv) {
             if (timeout <= 0)
                 timeout = -1;
             break;
+
+        case 'p':
+            pid_file = strdup(optarg);
+            break;
+
         case '?':
             return 2;
             break;
@@ -1539,7 +1583,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];
@@ -1562,14 +1606,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);
 
@@ -1580,7 +1629,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;
 }
 
 /*

-- 


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]