提交 633b7592 编写于 作者: P Peter Krempa

daemon: Refactor connection driver module loading

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.
上级 0e1404d4
...@@ -342,6 +342,14 @@ static int daemonErrorLogFilter(virErrorPtr err, int priority) ...@@ -342,6 +342,14 @@ static int daemonErrorLogFilter(virErrorPtr err, int priority)
return 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) static void daemonInitialize(void)
{ {
/* /*
...@@ -351,99 +359,55 @@ static void daemonInitialize(void) ...@@ -351,99 +359,55 @@ static void daemonInitialize(void)
* driver, since their resources must be auto-started before any * driver, since their resources must be auto-started before any
* domains can be auto-started. * domains can be auto-started.
*/ */
#ifdef WITH_DRIVER_MODULES
/* We don't care if any of these fail, because the whole point /* 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. * is to allow users to only install modules they want to use.
* If they try to open a connection for a module that * If they try to open a connection for a module that
* is not loaded they'll get a suitable error at that point * is not loaded they'll get a suitable error at that point
*/ */
# ifdef WITH_NETWORK #ifdef WITH_NETWORK
virDriverLoadModule("network"); VIR_DAEMON_LOAD_MODULE(networkRegister, "network");
# endif #endif
# ifdef WITH_INTERFACE #ifdef WITH_INTERFACE
virDriverLoadModule("interface"); VIR_DAEMON_LOAD_MODULE(interfaceRegister, "interface");
# endif #endif
# ifdef WITH_STORAGE #ifdef WITH_STORAGE
virDriverLoadModule("storage"); VIR_DAEMON_LOAD_MODULE(storageRegister, "storage");
# endif #endif
# ifdef WITH_NODE_DEVICES #ifdef WITH_NODE_DEVICES
virDriverLoadModule("nodedev"); VIR_DAEMON_LOAD_MODULE(nodedevRegister, "nodedev");
# endif #endif
# ifdef WITH_SECRETS #ifdef WITH_SECRETS
virDriverLoadModule("secret"); VIR_DAEMON_LOAD_MODULE(secretRegister, "secret");
# endif #endif
# ifdef WITH_NWFILTER #ifdef WITH_NWFILTER
virDriverLoadModule("nwfilter"); VIR_DAEMON_LOAD_MODULE(nwfilterRegister, "nwfilter");
# endif #endif
# ifdef WITH_XEN #ifdef WITH_XEN
virDriverLoadModule("xen"); VIR_DAEMON_LOAD_MODULE(xenRegister, "xen");
# endif #endif
# ifdef WITH_LIBXL #ifdef WITH_LIBXL
virDriverLoadModule("libxl"); VIR_DAEMON_LOAD_MODULE(libxlRegister, "libxl");
# endif #endif
# ifdef WITH_QEMU #ifdef WITH_QEMU
virDriverLoadModule("qemu"); VIR_DAEMON_LOAD_MODULE(qemuRegister, "qemu");
# endif #endif
# ifdef WITH_LXC #ifdef WITH_LXC
virDriverLoadModule("lxc"); VIR_DAEMON_LOAD_MODULE(lxcRegister, "lxc");
# endif #endif
# ifdef WITH_UML #ifdef WITH_UML
virDriverLoadModule("uml"); VIR_DAEMON_LOAD_MODULE(umlRegister, "uml");
# endif #endif
# ifdef WITH_VBOX #ifdef WITH_VBOX
virDriverLoadModule("vbox"); VIR_DAEMON_LOAD_MODULE(vboxRegister, "vbox");
# endif #endif
# ifdef WITH_BHYVE #ifdef WITH_BHYVE
virDriverLoadModule("bhyve"); VIR_DAEMON_LOAD_MODULE(bhyveRegister, "bhyve");
# endif #endif
# ifdef WITH_VZ #ifdef WITH_VZ
virDriverLoadModule("vz"); VIR_DAEMON_LOAD_MODULE(vzRegister, "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
#endif #endif
} }
#undef VIR_DAEMON_LOAD_MODULE
static int ATTRIBUTE_NONNULL(3) static int ATTRIBUTE_NONNULL(3)
......
...@@ -23,15 +23,12 @@ ...@@ -23,15 +23,12 @@
#include <config.h> #include <config.h>
#include <unistd.h> #include <unistd.h>
#include <c-ctype.h>
#include "driver.h" #include "driver.h"
#include "viralloc.h" #include "viralloc.h"
#include "virfile.h" #include "virfile.h"
#include "virlog.h" #include "virlog.h"
#include "virutil.h"
#include "configmake.h" #include "configmake.h"
#include "virstring.h"
VIR_LOG_INIT("driver"); VIR_LOG_INIT("driver");
...@@ -132,14 +129,12 @@ virDriverLoadModuleFull(const char *path, ...@@ -132,14 +129,12 @@ virDriverLoadModuleFull(const char *path,
} }
void * int
virDriverLoadModule(const char *name) virDriverLoadModule(const char *name,
const char *regfunc)
{ {
char *modfile = NULL; char *modfile = NULL;
char *fixedname = NULL; int ret;
char *regfunc = NULL;
char *tmp;
void *handle = NULL;
VIR_DEBUG("Module load %s", name); VIR_DEBUG("Module load %s", name);
...@@ -149,29 +144,13 @@ virDriverLoadModule(const char *name) ...@@ -149,29 +144,13 @@ virDriverLoadModule(const char *name)
abs_topbuilddir "/src/.libs", abs_topbuilddir "/src/.libs",
DEFAULT_DRIVER_DIR, DEFAULT_DRIVER_DIR,
"LIBVIRT_DRIVER_DIR"))) "LIBVIRT_DRIVER_DIR")))
return NULL; return 1;
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;
virDriverLoadModuleFull(modfile, regfunc, &handle); ret = virDriverLoadModuleFull(modfile, regfunc, NULL);
cleanup:
VIR_FREE(modfile); VIR_FREE(modfile);
VIR_FREE(fixedname);
VIR_FREE(regfunc); return ret;
return handle;
} }
......
...@@ -99,7 +99,8 @@ int virSetSharedNWFilterDriver(virNWFilterDriverPtr driver) ATTRIBUTE_RETURN_CHE ...@@ -99,7 +99,8 @@ int virSetSharedNWFilterDriver(virNWFilterDriverPtr driver) ATTRIBUTE_RETURN_CHE
int virSetSharedSecretDriver(virSecretDriverPtr driver) ATTRIBUTE_RETURN_CHECK; int virSetSharedSecretDriver(virSecretDriverPtr driver) ATTRIBUTE_RETURN_CHECK;
int virSetSharedStorageDriver(virStorageDriverPtr 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, int virDriverLoadModuleFull(const char *name,
const char *regfunc, const char *regfunc,
void **handle); void **handle);
......
...@@ -30,12 +30,18 @@ ...@@ -30,12 +30,18 @@
VIR_LOG_INIT("tests.drivermoduletest"); VIR_LOG_INIT("tests.drivermoduletest");
struct testDriverModuleData {
const char *module;
const char *regfunc;
};
static int testDriverModule(const void *args) static int testDriverModule(const void *args)
{ {
const char *name = args; const struct testDriverModuleData *data = args;
/* coverity[leaked_storage] */ /* coverity[leaked_storage] */
if (!virDriverLoadModule(name)) if (virDriverLoadModule(data->module, data->regfunc) != 0)
return -1; return -1;
return 0; return 0;
...@@ -46,13 +52,18 @@ static int ...@@ -46,13 +52,18 @@ static int
mymain(void) mymain(void)
{ {
int ret = 0; int ret = 0;
struct testDriverModuleData data;
#define TEST(name) \ #define TEST_FULL(name, fnc) \
do { \ do { \
if (virTestRun("Test driver " # name, testDriverModule, name) < 0) \ data.module = name; \
ret = -1; \ data.regfunc = fnc; \
if (virTestRun("Test driver " # name, testDriverModule, &data) < 0) \
ret = -1; \
} while (0) } while (0)
#define TEST(name) TEST_FULL(name, name "Register")
#ifdef WITH_NETWORK #ifdef WITH_NETWORK
TEST("network"); TEST("network");
#endif #endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册