]> xenbits.xensource.com Git - libvirt.git/commitdiff
daemon: Refactor connection driver module loading
authorPeter Krempa <pkrempa@redhat.com>
Thu, 26 Jan 2017 13:57:41 +0000 (14:57 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 21 Feb 2017 08:24:33 +0000 (09:24 +0100)
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
src/driver.c
src/driver.h
tests/virdrivermoduletest.c

index 5c30c9e746145a2403b3c436206e908254ccce23..9b98f337353e30cd0b8fba5d5aef13ccfbffc236 100644 (file)
@@ -342,6 +342,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)
 {
     /*
@@ -351,99 +359,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)
index 38f2f6c312db6c14aaa145826c6385bf47a9f6cc..f6acfd669bc7786a0a9322fdaf0a1b98b94fe8a9 100644 (file)
 #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");
 
@@ -132,14 +129,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;
 
     VIR_DEBUG("Module load %s", name);
 
@@ -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;
 }
 
 
index 885e8843e52f30afeb8691fe44fae0fd602965ec..420f6455d3891260b7eff7ea576edad9572f3413 100644 (file)
@@ -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);
index 09c1a614b70c096dd260434d1302620b066e44b6..e440350c20eefe7ce5d1676b0273279be8d7f992 100644 (file)
 
 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