[PATCH 05/11] daemon: Refactor connection driver module loading

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

 



Pass the registration function name to virDriverLoadModule so that we
can later call specific functions if necessary (e.g. for testing
purposes). This gets rid of the rather ugly automatic name generator and
unifies the code to load/initialize the modules.

It's also clear which registration function gets called.
---
 daemon/libvirtd.c           | 136 ++++++++++++++++----------------------------
 src/driver.c                |  37 +++---------
 src/driver.h                |   3 +-
 tests/virdrivermoduletest.c |  23 ++++++--
 4 files changed, 77 insertions(+), 122 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index b6d76ed84..2f9f5c77d 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -341,6 +341,14 @@ static int daemonErrorLogFilter(virErrorPtr err, int priority)
     return priority;
 }

+
+#ifdef WITH_DRIVER_MODULES
+# define VIR_DAEMON_LOAD_MODULE(func, module) \
+    virDriverLoadModule(module, #func)
+#else
+# define VIR_DAEMON_LOAD_MODULE(func, module) \
+    func()
+#endif
 static void daemonInitialize(void)
 {
     /*
@@ -350,99 +358,55 @@ static void daemonInitialize(void)
      * driver, since their resources must be auto-started before any
      * domains can be auto-started.
      */
-#ifdef WITH_DRIVER_MODULES
     /* We don't care if any of these fail, because the whole point
      * is to allow users to only install modules they want to use.
      * If they try to open a connection for a module that
      * is not loaded they'll get a suitable error at that point
      */
-# ifdef WITH_NETWORK
-    virDriverLoadModule("network");
-# endif
-# ifdef WITH_INTERFACE
-    virDriverLoadModule("interface");
-# endif
-# ifdef WITH_STORAGE
-    virDriverLoadModule("storage");
-# endif
-# ifdef WITH_NODE_DEVICES
-    virDriverLoadModule("nodedev");
-# endif
-# ifdef WITH_SECRETS
-    virDriverLoadModule("secret");
-# endif
-# ifdef WITH_NWFILTER
-    virDriverLoadModule("nwfilter");
-# endif
-# ifdef WITH_XEN
-    virDriverLoadModule("xen");
-# endif
-# ifdef WITH_LIBXL
-    virDriverLoadModule("libxl");
-# endif
-# ifdef WITH_QEMU
-    virDriverLoadModule("qemu");
-# endif
-# ifdef WITH_LXC
-    virDriverLoadModule("lxc");
-# endif
-# ifdef WITH_UML
-    virDriverLoadModule("uml");
-# endif
-# ifdef WITH_VBOX
-    virDriverLoadModule("vbox");
-# endif
-# ifdef WITH_BHYVE
-    virDriverLoadModule("bhyve");
-# endif
-# ifdef WITH_VZ
-    virDriverLoadModule("vz");
-# endif
-#else
-# ifdef WITH_NETWORK
-    networkRegister();
-# endif
-# ifdef WITH_INTERFACE
-    interfaceRegister();
-# endif
-# ifdef WITH_STORAGE
-    storageRegister();
-# endif
-# ifdef WITH_NODE_DEVICES
-    nodedevRegister();
-# endif
-# ifdef WITH_SECRETS
-    secretRegister();
-# endif
-# ifdef WITH_NWFILTER
-    nwfilterRegister();
-# endif
-# ifdef WITH_XEN
-    xenRegister();
-# endif
-# ifdef WITH_LIBXL
-    libxlRegister();
-# endif
-# ifdef WITH_QEMU
-    qemuRegister();
-# endif
-# ifdef WITH_LXC
-    lxcRegister();
-# endif
-# ifdef WITH_UML
-    umlRegister();
-# endif
-# ifdef WITH_VBOX
-    vboxRegister();
-# endif
-# ifdef WITH_BHYVE
-    bhyveRegister();
-# endif
-# ifdef WITH_VZ
-    vzRegister();
-# endif
+#ifdef WITH_NETWORK
+    VIR_DAEMON_LOAD_MODULE(networkRegister, "network");
+#endif
+#ifdef WITH_INTERFACE
+    VIR_DAEMON_LOAD_MODULE(interfaceRegister, "interface");
+#endif
+#ifdef WITH_STORAGE
+    VIR_DAEMON_LOAD_MODULE(storageRegister, "storage");
+#endif
+#ifdef WITH_NODE_DEVICES
+    VIR_DAEMON_LOAD_MODULE(nodedevRegister, "nodedev");
+#endif
+#ifdef WITH_SECRETS
+    VIR_DAEMON_LOAD_MODULE(secretRegister, "secret");
+#endif
+#ifdef WITH_NWFILTER
+    VIR_DAEMON_LOAD_MODULE(nwfilterRegister, "nwfilter");
+#endif
+#ifdef WITH_XEN
+    VIR_DAEMON_LOAD_MODULE(xenRegister, "xen");
+#endif
+#ifdef WITH_LIBXL
+    VIR_DAEMON_LOAD_MODULE(libxlRegister, "libxl");
+#endif
+#ifdef WITH_QEMU
+    VIR_DAEMON_LOAD_MODULE(qemuRegister, "qemu");
+#endif
+#ifdef WITH_LXC
+    VIR_DAEMON_LOAD_MODULE(lxcRegister, "lxc");
+#endif
+#ifdef WITH_UML
+    VIR_DAEMON_LOAD_MODULE(umlRegister, "uml");
+#endif
+#ifdef WITH_VBOX
+    VIR_DAEMON_LOAD_MODULE(vboxRegister, "vbox");
+#endif
+#ifdef WITH_BHYVE
+    VIR_DAEMON_LOAD_MODULE(bhyveRegister, "bhyve");
+#endif
+#ifdef WITH_VZ
+    VIR_DAEMON_LOAD_MODULE(vzRegister, "vz");
 #endif
 }
+#undef VIR_DAEMON_LOAD_MODULE


 static int ATTRIBUTE_NONNULL(3)
diff --git a/src/driver.c b/src/driver.c
index 783e08a28..f47bea0fb 100644
--- a/src/driver.c
+++ b/src/driver.c
@@ -23,15 +23,12 @@
 #include <config.h>

 #include <unistd.h>
-#include <c-ctype.h>

 #include "driver.h"
 #include "viralloc.h"
 #include "virfile.h"
 #include "virlog.h"
-#include "virutil.h"
 #include "configmake.h"
-#include "virstring.h"

 VIR_LOG_INIT("driver");

@@ -134,14 +131,12 @@ virDriverLoadModuleFull(const char *path,
 }


-void *
-virDriverLoadModule(const char *name)
+int
+virDriverLoadModule(const char *name,
+                    const char *regfunc)
 {
     char *modfile = NULL;
-    char *fixedname = NULL;
-    char *regfunc = NULL;
-    char *tmp;
-    void *handle = NULL;
+    int ret;

     if (!(modfile = virFileFindResourceFull(name,
                                             "libvirt_driver_",
@@ -149,29 +144,13 @@ virDriverLoadModule(const char *name)
                                             abs_topbuilddir "/src/.libs",
                                             DEFAULT_DRIVER_DIR,
                                             "LIBVIRT_DRIVER_DIR")))
-        return NULL;
-
-    if (VIR_STRDUP_QUIET(fixedname, name) < 0) {
-        VIR_ERROR(_("out of memory"));
-        goto cleanup;
-    }
-
-    /* convert something_like_this into somethingLikeThis */
-    while ((tmp = strchr(fixedname, '_'))) {
-        memmove(tmp, tmp + 1, strlen(tmp));
-        *tmp = c_toupper(*tmp);
-    }
-
-    if (virAsprintfQuiet(&regfunc, "%sRegister", fixedname) < 0)
-        goto cleanup;
+        return 1;

-    virDriverLoadModuleFull(modfile, regfunc, &handle);
+    ret = virDriverLoadModuleFull(modfile, regfunc, NULL);

- cleanup:
     VIR_FREE(modfile);
-    VIR_FREE(fixedname);
-    VIR_FREE(regfunc);
-    return handle;
+
+    return ret;
 }


diff --git a/src/driver.h b/src/driver.h
index 885e8843e..420f6455d 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -99,7 +99,8 @@ int virSetSharedNWFilterDriver(virNWFilterDriverPtr driver) ATTRIBUTE_RETURN_CHE
 int virSetSharedSecretDriver(virSecretDriverPtr driver) ATTRIBUTE_RETURN_CHECK;
 int virSetSharedStorageDriver(virStorageDriverPtr driver) ATTRIBUTE_RETURN_CHECK;

-void *virDriverLoadModule(const char *name);
+int virDriverLoadModule(const char *name,
+                        const char *regfunc);
 int virDriverLoadModuleFull(const char *name,
                             const char *regfunc,
                             void **handle);
diff --git a/tests/virdrivermoduletest.c b/tests/virdrivermoduletest.c
index 09c1a614b..e440350c2 100644
--- a/tests/virdrivermoduletest.c
+++ b/tests/virdrivermoduletest.c
@@ -30,12 +30,18 @@

 VIR_LOG_INIT("tests.drivermoduletest");

+struct testDriverModuleData {
+    const char *module;
+    const char *regfunc;
+};
+
+
 static int testDriverModule(const void *args)
 {
-    const char *name = args;
+    const struct testDriverModuleData *data = args;

     /* coverity[leaked_storage] */
-    if (!virDriverLoadModule(name))
+    if (virDriverLoadModule(data->module, data->regfunc) != 0)
         return -1;

     return 0;
@@ -46,13 +52,18 @@ static int
 mymain(void)
 {
     int ret = 0;
+    struct testDriverModuleData data;

-#define TEST(name)                                                         \
-    do  {                                                                  \
-        if (virTestRun("Test driver " # name, testDriverModule, name) < 0) \
-            ret = -1;                                                      \
+#define TEST_FULL(name, fnc)                                                   \
+    do  {                                                                      \
+        data.module = name;                                                    \
+        data.regfunc = fnc;                                                    \
+        if (virTestRun("Test driver " # name, testDriverModule, &data) < 0)    \
+            ret = -1;                                                          \
     } while (0)

+#define TEST(name) TEST_FULL(name, name "Register")
+
 #ifdef WITH_NETWORK
     TEST("network");
 #endif
-- 
2.11.0

--
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]
  Powered by Linux