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

xuhj at linux.vnet.ibm.com xuhj at linux.vnet.ibm.com
Fri Aug 26 09:51:40 UTC 2011


From: soulxu <soulxu at soulxu-ThinkPad-T410.(none)>


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 |   52 ++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 423c3d7..1ce8acd 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -1249,6 +1249,8 @@ int main(int argc, char **argv) {
     bool privileged = geteuid() == 0 ? true : false;
     bool implicit_conf = false;
     bool use_polkit_dbus;
+    char *run_dir = NULL;
+    mode_t old_umask;
 
     struct option opts[] = {
         { "verbose", no_argument, &verbose, 1},
@@ -1403,23 +1405,42 @@ 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);
     }
 
+    old_umask = umask(022);
+    if (mkdir (run_dir, 0755)) {
+        if (errno != EEXIST) {
+            char ebuf[1024];
+            VIR_ERROR(_("unable to create rundir %s: %s"), run_dir,
+                      virStrerror(errno, ebuf, sizeof(ebuf)));
+            ret = VIR_DAEMON_ERR_RUNDIR;
+            umask(old_umask);
+            goto cleanup;
+        }
+     }
+     umask(old_umask);
+
     /* Try to claim the pidfile, exiting if we can't */
     if ((pid_file_fd = virPidFileAcquirePath(pid_file, getpid())) < 0) {
         ret = VIR_DAEMON_ERR_PIDFILE;
@@ -1571,6 +1592,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