mirror of https://gitee.com/openkylin/libvirt.git
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.
This commit is contained in:
parent
0e1404d444
commit
633b7592d6
|
@ -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)
|
||||
|
|
37
src/driver.c
37
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");
|
||||
|
||||
|
@ -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;
|
||||
return 1;
|
||||
|
||||
if (VIR_STRDUP_QUIET(fixedname, name) < 0) {
|
||||
VIR_ERROR(_("out of memory"));
|
||||
goto cleanup;
|
||||
}
|
||||
ret = virDriverLoadModuleFull(modfile, regfunc, NULL);
|
||||
|
||||
/* convert something_like_this into somethingLikeThis */
|
||||
while ((tmp = strchr(fixedname, '_'))) {
|
||||
memmove(tmp, tmp + 1, strlen(tmp));
|
||||
*tmp = c_toupper(*tmp);
|
||||
}
|
||||
|
||||
if (virAsprintfQuiet(®func, "%sRegister", fixedname) < 0)
|
||||
goto cleanup;
|
||||
|
||||
virDriverLoadModuleFull(modfile, regfunc, &handle);
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(modfile);
|
||||
VIR_FREE(fixedname);
|
||||
VIR_FREE(regfunc);
|
||||
return handle;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue