9119d9
From 9a8acfe35e869f387c09a7495ef9bebcc00657cc Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <9a8acfe35e869f387c09a7495ef9bebcc00657cc@dist-git>
9119d9
From: Martin Kletzander <mkletzan@redhat.com>
9119d9
Date: Wed, 17 Sep 2014 17:11:01 +0200
9119d9
Subject: [PATCH] remove redundant pidfile path constructions
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=927369
9119d9
9119d9
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
9119d9
(cherry picked from commit 8035f2e6f2db7fc0b74b639deb7eff64957692bc)
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 daemon/libvirtd.c         | 41 ++++-----------------------------------
9119d9
 src/libvirt_private.syms  |  1 +
9119d9
 src/locking/lock_daemon.c | 42 ++++------------------------------------
9119d9
 src/util/virpidfile.c     | 49 ++++++++++++++++++++++++++++++++++++++++++++++-
9119d9
 src/util/virpidfile.h     |  7 ++++++-
9119d9
 5 files changed, 63 insertions(+), 77 deletions(-)
9119d9
9119d9
diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
9119d9
index 05ee50e..9ad8ff5 100644
9119d9
--- a/daemon/libvirtd.c
9119d9
+++ b/daemon/libvirtd.c
9119d9
@@ -251,41 +251,6 @@ static int daemonForkIntoBackground(const char *argv0)
9119d9
 
9119d9
 
9119d9
 static int
9119d9
-daemonPidFilePath(bool privileged,
9119d9
-                  char **pidfile)
9119d9
-{
9119d9
-    if (privileged) {
9119d9
-        if (VIR_STRDUP(*pidfile, LOCALSTATEDIR "/run/libvirtd.pid") < 0)
9119d9
-            goto error;
9119d9
-    } else {
9119d9
-        char *rundir = NULL;
9119d9
-        mode_t old_umask;
9119d9
-
9119d9
-        if (!(rundir = virGetUserRuntimeDirectory()))
9119d9
-            goto error;
9119d9
-
9119d9
-        old_umask = umask(077);
9119d9
-        if (virFileMakePath(rundir) < 0) {
9119d9
-            umask(old_umask);
9119d9
-            goto error;
9119d9
-        }
9119d9
-        umask(old_umask);
9119d9
-
9119d9
-        if (virAsprintf(pidfile, "%s/libvirtd.pid", rundir) < 0) {
9119d9
-            VIR_FREE(rundir);
9119d9
-            goto error;
9119d9
-        }
9119d9
-
9119d9
-        VIR_FREE(rundir);
9119d9
-    }
9119d9
-
9119d9
-    return 0;
9119d9
-
9119d9
- error:
9119d9
-    return -1;
9119d9
-}
9119d9
-
9119d9
-static int
9119d9
 daemonUnixSocketPaths(struct daemonConfig *config,
9119d9
                       bool privileged,
9119d9
                       char **sockfile,
9119d9
@@ -1313,8 +1278,10 @@ int main(int argc, char **argv) {
9119d9
     }
9119d9
 
9119d9
     if (!pid_file &&
9119d9
-        daemonPidFilePath(privileged,
9119d9
-                          &pid_file) < 0) {
9119d9
+        virPidFileConstructPath(privileged,
9119d9
+                                LOCALSTATEDIR,
9119d9
+                                "libvirtd",
9119d9
+                                &pid_file) < 0) {
9119d9
         VIR_ERROR(_("Can't determine pid file path."));
9119d9
         exit(EXIT_FAILURE);
9119d9
     }
9119d9
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
9119d9
index b4b13f9..18cf0c2 100644
9119d9
--- a/src/libvirt_private.syms
9119d9
+++ b/src/libvirt_private.syms
9119d9
@@ -1776,6 +1776,7 @@ virPCIIsVirtualFunction;
9119d9
 virPidFileAcquire;
9119d9
 virPidFileAcquirePath;
9119d9
 virPidFileBuildPath;
9119d9
+virPidFileConstructPath;
9119d9
 virPidFileDelete;
9119d9
 virPidFileDeletePath;
9119d9
 virPidFileRead;
9119d9
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
9119d9
index 02d77e3..fe7cfb8 100644
9119d9
--- a/src/locking/lock_daemon.c
9119d9
+++ b/src/locking/lock_daemon.c
9119d9
@@ -366,42 +366,6 @@ virLockDaemonForkIntoBackground(const char *argv0)
9119d9
 
9119d9
 
9119d9
 static int
9119d9
-virLockDaemonPidFilePath(bool privileged,
9119d9
-                         char **pidfile)
9119d9
-{
9119d9
-    if (privileged) {
9119d9
-        if (VIR_STRDUP(*pidfile, LOCALSTATEDIR "/run/virtlockd.pid") < 0)
9119d9
-            goto error;
9119d9
-    } else {
9119d9
-        char *rundir = NULL;
9119d9
-        mode_t old_umask;
9119d9
-
9119d9
-        if (!(rundir = virGetUserRuntimeDirectory()))
9119d9
-            goto error;
9119d9
-
9119d9
-        old_umask = umask(077);
9119d9
-        if (virFileMakePath(rundir) < 0) {
9119d9
-            umask(old_umask);
9119d9
-            goto error;
9119d9
-        }
9119d9
-        umask(old_umask);
9119d9
-
9119d9
-        if (virAsprintf(pidfile, "%s/virtlockd.pid", rundir) < 0) {
9119d9
-            VIR_FREE(rundir);
9119d9
-            goto error;
9119d9
-        }
9119d9
-
9119d9
-        VIR_FREE(rundir);
9119d9
-    }
9119d9
-
9119d9
-    return 0;
9119d9
-
9119d9
- error:
9119d9
-    return -1;
9119d9
-}
9119d9
-
9119d9
-
9119d9
-static int
9119d9
 virLockDaemonUnixSocketPaths(bool privileged,
9119d9
                              char **sockfile)
9119d9
 {
9119d9
@@ -1283,8 +1247,10 @@ int main(int argc, char **argv) {
9119d9
     }
9119d9
 
9119d9
     if (!pid_file &&
9119d9
-        virLockDaemonPidFilePath(privileged,
9119d9
-                                 &pid_file) < 0) {
9119d9
+        virPidFileConstructPath(privileged,
9119d9
+                                LOCALSTATEDIR,
9119d9
+                                "virtlockd",
9119d9
+                                &pid_file) < 0) {
9119d9
         VIR_ERROR(_("Can't determine pid file path."));
9119d9
         exit(EXIT_FAILURE);
9119d9
     }
9119d9
diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
9119d9
index 1d9a1c5..19ec103 100644
9119d9
--- a/src/util/virpidfile.c
9119d9
+++ b/src/util/virpidfile.c
9119d9
@@ -1,7 +1,7 @@
9119d9
 /*
9119d9
  * virpidfile.c: manipulation of pidfiles
9119d9
  *
9119d9
- * Copyright (C) 2010-2012 Red Hat, Inc.
9119d9
+ * Copyright (C) 2010-2012, 2014 Red Hat, Inc.
9119d9
  * Copyright (C) 2006, 2007 Binary Karma
9119d9
  * Copyright (C) 2006 Shuveb Hussain
9119d9
  *
9119d9
@@ -521,3 +521,50 @@ int virPidFileRelease(const char *dir,
9119d9
     VIR_FREE(pidfile);
9119d9
     return rc;
9119d9
 }
9119d9
+
9119d9
+
9119d9
+int
9119d9
+virPidFileConstructPath(bool privileged,
9119d9
+                        const char *statedir,
9119d9
+                        const char *progname,
9119d9
+                        char **pidfile)
9119d9
+{
9119d9
+    if (privileged) {
9119d9
+        /*
9119d9
+         * This is here just to allow calling this function with
9119d9
+         * statedir == NULL; of course only when !privileged.
9119d9
+         */
9119d9
+        if (!statedir) {
9119d9
+            virReportError(VIR_ERR_INTERNAL_ERROR,
9119d9
+                           "%s", _("No statedir specified"));
9119d9
+            goto cleanup;
9119d9
+        }
9119d9
+        if (virAsprintf(pidfile, "%s/run/%s.pid", statedir, progname) < 0)
9119d9
+            goto cleanup;
9119d9
+    } else {
9119d9
+        char *rundir = NULL;
9119d9
+        mode_t old_umask;
9119d9
+
9119d9
+        if (!(rundir = virGetUserRuntimeDirectory()))
9119d9
+            goto error;
9119d9
+
9119d9
+        old_umask = umask(077);
9119d9
+        if (virFileMakePath(rundir) < 0) {
9119d9
+            umask(old_umask);
9119d9
+            goto error;
9119d9
+        }
9119d9
+        umask(old_umask);
9119d9
+
9119d9
+        if (virAsprintf(pidfile, "%s/%s.pid", rundir, progname) < 0) {
9119d9
+            VIR_FREE(rundir);
9119d9
+            goto error;
9119d9
+        }
9119d9
+
9119d9
+        VIR_FREE(rundir);
9119d9
+    }
9119d9
+
9119d9
+    return 0;
9119d9
+
9119d9
+ error:
9119d9
+    return -1;
9119d9
+}
9119d9
diff --git a/src/util/virpidfile.h b/src/util/virpidfile.h
9119d9
index 2720206..ca1dbff 100644
9119d9
--- a/src/util/virpidfile.h
9119d9
+++ b/src/util/virpidfile.h
9119d9
@@ -1,7 +1,7 @@
9119d9
 /*
9119d9
  * virpidfile.h: manipulation of pidfiles
9119d9
  *
9119d9
- * Copyright (C) 2010-2011 Red Hat, Inc.
9119d9
+ * Copyright (C) 2010-2011, 2014 Red Hat, Inc.
9119d9
  * Copyright (C) 2006, 2007 Binary Karma
9119d9
  * Copyright (C) 2006 Shuveb Hussain
9119d9
  *
9119d9
@@ -69,4 +69,9 @@ int virPidFileRelease(const char *dir,
9119d9
                       const char *name,
9119d9
                       int fd);
9119d9
 
9119d9
+int virPidFileConstructPath(bool privileged,
9119d9
+                            const char *statedir,
9119d9
+                            const char *progname,
9119d9
+                            char **pidfile);
9119d9
+
9119d9
 #endif /* __VIR_PIDFILE_H__ */
9119d9
-- 
9119d9
2.1.0
9119d9