[PATCH 03/14] Refactor creation of lock manager plugins

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Refactor virLockManagerPluginNew() so that the caller does
not need to pass in the config file path itself - just the
config directory and driver name.

Fix QEMU to actually pass in a config file when creating the
default lock manager plugin, rather than NULL.

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 src/locking/lock_manager.c | 20 +++++++++++++++++---
 src/locking/lock_manager.h |  3 ++-
 src/qemu/qemu_conf.c       | 12 +++++-------
 src/qemu/qemu_conf.h       |  3 ++-
 src/qemu/qemu_driver.c     | 24 ++++++++++--------------
 5 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/src/locking/lock_manager.c b/src/locking/lock_manager.c
index 423997b..3f483b7 100644
--- a/src/locking/lock_manager.c
+++ b/src/locking/lock_manager.c
@@ -128,7 +128,8 @@ static void virLockManagerLogParams(size_t nparams,
  */
 #if HAVE_DLFCN_H
 virLockManagerPluginPtr virLockManagerPluginNew(const char *name,
-                                                const char *configFile,
+                                                const char *driverName,
+                                                const char *configDir,
                                                 unsigned int flags)
 {
     void *handle = NULL;
@@ -136,6 +137,16 @@ virLockManagerPluginPtr virLockManagerPluginNew(const char *name,
     virLockManagerPluginPtr plugin = NULL;
     const char *moddir = getenv("LIBVIRT_LOCK_MANAGER_PLUGIN_DIR");
     char *modfile = NULL;
+    char *configFile = NULL;
+
+    VIR_DEBUG("name=%s driverName=%s configDir=%s flags=%x",
+              name, driverName, NULLSTR(configDir), flags);
+
+    if (virAsprintf(&configFile, "%s/%s-%s.conf",
+                    configDir, driverName, name) < 0) {
+        virReportOOMError();
+        return NULL;
+    }
 
     if (STREQ(name, "nop")) {
         driver = &virLockDriverNop;
@@ -147,7 +158,7 @@ virLockManagerPluginPtr virLockManagerPluginNew(const char *name,
 
         if (virAsprintf(&modfile, "%s/%s.so", moddir, name) < 0) {
             virReportOOMError();
-            return NULL;
+            goto cleanup;
         }
 
         if (access(modfile, R_OK) < 0) {
@@ -188,10 +199,12 @@ virLockManagerPluginPtr virLockManagerPluginNew(const char *name,
         goto cleanup;
     }
 
+    VIR_FREE(configFile);
     VIR_FREE(modfile);
     return plugin;
 
 cleanup:
+    VIR_FREE(configFile);
     VIR_FREE(plugin);
     VIR_FREE(modfile);
     if (handle)
@@ -201,7 +214,8 @@ cleanup:
 #else /* !HAVE_DLFCN_H */
 virLockManagerPluginPtr
 virLockManagerPluginNew(const char *name ATTRIBUTE_UNUSED,
-                        const char *configFile ATTRIBUTE_UNUSED,
+                        const char *driverName ATTRIBUTE_UNUSED,
+                        const char *configDir ATTRIBUTE_UNUSED,
                         unsigned int flags_unused ATTRIBUTE_UNUSED)
 {
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/src/locking/lock_manager.h b/src/locking/lock_manager.h
index 4fee12d..fea9db8 100644
--- a/src/locking/lock_manager.h
+++ b/src/locking/lock_manager.h
@@ -30,7 +30,8 @@ typedef virLockManagerPlugin *virLockManagerPluginPtr;
 
 void virLockManagerSetPluginDir(const char *dir);
 virLockManagerPluginPtr virLockManagerPluginNew(const char *name,
-                                                const char *configFile,
+                                                const char *driverName,
+                                                const char *configDir,
                                                 unsigned int flags);
 void virLockManagerPluginRef(virLockManagerPluginPtr plugin);
 void virLockManagerPluginUnref(virLockManagerPluginPtr plugin);
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 8d380a1..4c506a0 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -117,7 +117,10 @@ int qemuLoadDriverConfig(virQEMUDriverPtr driver,
     }
 #endif
 
-    if (!(driver->lockManager = virLockManagerPluginNew("nop", NULL, 0)))
+    if (!(driver->lockManager = virLockManagerPluginNew("nop",
+                                                        "qemu",
+                                                        driver->configBaseDir,
+                                                        0)))
         goto cleanup;
 
     driver->keepAliveInterval = 5;
@@ -359,15 +362,10 @@ int qemuLoadDriverConfig(virQEMUDriverPtr driver,
     p = virConfGetValue(conf, "lock_manager");
     CHECK_TYPE("lock_manager", VIR_CONF_STRING);
     if (p && p->str) {
-        char *lockConf;
         virLockManagerPluginUnref(driver->lockManager);
-        if (virAsprintf(&lockConf, "%s/libvirt/qemu-%s.conf", SYSCONFDIR, p->str) < 0)
-            goto no_memory;
-
         if (!(driver->lockManager =
-              virLockManagerPluginNew(p->str, lockConf, 0)))
+              virLockManagerPluginNew(p->str, "qemu", driver->configBaseDir, 0)))
             VIR_ERROR(_("Failed to load lock manager %s"), p->str);
-        VIR_FREE(lockConf);
     }
 
     GET_VALUE_LONG("max_queued", driver->max_queued);
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index d0d25ce..1a39946 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -79,8 +79,9 @@ struct _virQEMUDriver {
 
     virDomainObjList domains;
 
-    /* These four directories are ones libvirtd uses (so must be root:root
+    /* These five directories are ones libvirtd uses (so must be root:root
      * to avoid security risk from QEMU processes */
+    char *configBaseDir;
     char *configDir;
     char *autostartDir;
     char *logDir;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 2fda44e..00f6b00 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -614,7 +614,6 @@ qemuStartup(bool privileged,
             virStateInhibitCallback callback,
             void *opaque)
 {
-    char *base = NULL;
     char *driverConf = NULL;
     int rc;
     virConnectPtr conn = NULL;
@@ -657,7 +656,7 @@ qemuStartup(bool privileged,
                         "%s/log/libvirt/qemu", LOCALSTATEDIR) == -1)
             goto out_of_memory;
 
-        if ((base = strdup(SYSCONFDIR "/libvirt")) == NULL)
+        if ((qemu_driver->configBaseDir = strdup(SYSCONFDIR "/libvirt")) == NULL)
             goto out_of_memory;
 
         if (virAsprintf(&qemu_driver->stateDir,
@@ -708,16 +707,15 @@ qemuStartup(bool privileged,
         }
         VIR_FREE(rundir);
 
-        base = virGetUserConfigDirectory();
-        if (!base)
+        if (!(qemu_driver->configBaseDir = virGetUserConfigDirectory()))
             goto error;
-        if (virAsprintf(&qemu_driver->libDir, "%s/qemu/lib", base) == -1)
+        if (virAsprintf(&qemu_driver->libDir, "%s/qemu/lib", qemu_driver->configBaseDir) == -1)
             goto out_of_memory;
-        if (virAsprintf(&qemu_driver->saveDir, "%s/qemu/save", base) == -1)
+        if (virAsprintf(&qemu_driver->saveDir, "%s/qemu/save", qemu_driver->configBaseDir) == -1)
             goto out_of_memory;
-        if (virAsprintf(&qemu_driver->snapshotDir, "%s/qemu/snapshot", base) == -1)
+        if (virAsprintf(&qemu_driver->snapshotDir, "%s/qemu/snapshot", qemu_driver->configBaseDir) == -1)
             goto out_of_memory;
-        if (virAsprintf(&qemu_driver->autoDumpPath, "%s/qemu/dump", base) == -1)
+        if (virAsprintf(&qemu_driver->autoDumpPath, "%s/qemu/dump", qemu_driver->configBaseDir) == -1)
             goto out_of_memory;
     }
 
@@ -755,13 +753,11 @@ qemuStartup(bool privileged,
     /* Configuration paths are either ~/.libvirt/qemu/... (session) or
      * /etc/libvirt/qemu/... (system).
      */
-    if (virAsprintf(&driverConf, "%s/qemu.conf", base) < 0 ||
-        virAsprintf(&qemu_driver->configDir, "%s/qemu", base) < 0 ||
-        virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) < 0)
+    if (virAsprintf(&driverConf, "%s/qemu.conf", qemu_driver->configBaseDir) < 0 ||
+        virAsprintf(&qemu_driver->configDir, "%s/qemu", qemu_driver->configBaseDir) < 0 ||
+        virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", qemu_driver->configBaseDir) < 0)
         goto out_of_memory;
 
-    VIR_FREE(base);
-
     rc = virCgroupForDriver("qemu", &qemu_driver->cgroup, privileged, 1);
     if (rc < 0) {
         VIR_INFO("Unable to create cgroup for driver: %s",
@@ -934,7 +930,6 @@ error:
         qemuDriverUnlock(qemu_driver);
     if (conn)
         virConnectClose(conn);
-    VIR_FREE(base);
     VIR_FREE(driverConf);
     VIR_FREE(membase);
     VIR_FREE(mempath);
@@ -1074,6 +1069,7 @@ qemuShutdown(void) {
 
     qemuDriverCloseCallbackShutdown(qemu_driver);
 
+    VIR_FREE(qemu_driver->configBaseDir);
     VIR_FREE(qemu_driver->configDir);
     VIR_FREE(qemu_driver->autostartDir);
     VIR_FREE(qemu_driver->logDir);
-- 
1.7.11.7

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[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]