[libvirt] [PATCH] libvirtd: create run dir when running at non-root user

xuhj at linux.vnet.ibm.com xuhj at linux.vnet.ibm.com
Mon Aug 29 02:52:23 UTC 2011


From: Xu He Jie <xuhj at linux.vnet.ibm.com>


Signed-off-by: Xu He Jie <xuhj at linux.vnet.ibm.com>

When libvirtd is running at non-root user, it won't create ${HOME}/.libvirt.

It will show error message:
17:44:16.838: 7035: error : virPidFileAcquirePath:322 : Failed to open pid file

---
 daemon/libvirtd.c |   46 ++++++++++++++++++++++++++++++++--------------
 1 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 423c3d7..e0004c7 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -1249,6 +1249,7 @@ int main(int argc, char **argv) {
     bool privileged = geteuid() == 0 ? true : false;
     bool implicit_conf = false;
     bool use_polkit_dbus;
+    char *run_dir = NULL;
 
     struct option opts[] = {
         { "verbose", no_argument, &verbose, 1},
@@ -1403,21 +1404,35 @@ int main(int argc, char **argv) {
 
     /* Ensure the rundir exists (on tmpfs on some systems) */
     if (privileged) {
-        const char *rundir = LOCALSTATEDIR "/run/libvirt";
-        mode_t old_umask;
-
-        old_umask = umask(022);
-        if (mkdir (rundir, 0755)) {
-            if (errno != EEXIST) {
-                char ebuf[1024];
-                VIR_ERROR(_("unable to create rundir %s: %s"), rundir,
-                          virStrerror(errno, ebuf, sizeof(ebuf)));
-                ret = VIR_DAEMON_ERR_RUNDIR;
-                umask(old_umask);
-                goto cleanup;
-            }
+        run_dir = strdup(LOCALSTATEDIR "/run/libvirt");
+        if (!run_dir) {
+            VIR_ERROR(_("Can't allocate memory"));
+            goto cleanup;
+        }
+    }
+    else {
+        char *user_dir = NULL;
+
+        if (!(user_dir = virGetUserDirectory(geteuid()))) {
+            VIR_ERROR(_("Can't determine user directory"));
+            goto cleanup;
+        }
+
+        if (virAsprintf(&run_dir, "%s/.libvirt/", user_dir) < 0) {
+            VIR_ERROR(_("Can't allocate memory"));
+            VIR_FREE(user_dir);
+            goto cleanup;
         }
-        umask(old_umask);
+
+        VIR_FREE(user_dir);
+    }
+
+    if (virFileMakePath(run_dir) < 0) {
+        char ebuf[1024];
+        VIR_ERROR(_("unable to create rundir %s: %s"), run_dir,
+                  virStrerror(errno, ebuf, sizeof(ebuf)));
+        ret = VIR_DAEMON_ERR_RUNDIR;
+        goto cleanup;
     }
 
     /* Try to claim the pidfile, exiting if we can't */
@@ -1571,6 +1586,9 @@ cleanup:
     VIR_FREE(sock_file_ro);
     VIR_FREE(pid_file);
     VIR_FREE(remote_config_file);
+    if (run_dir)
+        VIR_FREE(run_dir);
+
     daemonConfigFree(config);
     virLogShutdown();
 
-- 
1.7.4.1




More information about the libvir-list mailing list