mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-07-30 13:37:17 +00:00
driver: Split/refactor driver module loading
Split the convoluted driver loader function into simpler parts which will potentially allow reuse.
This commit is contained in:
parent
2e4e6affdd
commit
0e1404d444
129
src/driver.c
129
src/driver.c
@ -43,13 +43,103 @@ VIR_LOG_INIT("driver");
|
|||||||
# include <dlfcn.h>
|
# include <dlfcn.h>
|
||||||
# define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
|
# define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver"
|
||||||
|
|
||||||
|
|
||||||
|
static void *
|
||||||
|
virDriverLoadModuleFile(const char *file)
|
||||||
|
{
|
||||||
|
void *handle = NULL;
|
||||||
|
|
||||||
|
VIR_DEBUG("Load module file '%s'", file);
|
||||||
|
|
||||||
|
if (access(file, R_OK) < 0) {
|
||||||
|
VIR_INFO("Module %s not accessible", file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
virUpdateSelfLastChanged(file);
|
||||||
|
|
||||||
|
if (!(handle = dlopen(file, RTLD_NOW | RTLD_GLOBAL)))
|
||||||
|
VIR_ERROR(_("failed to load module %s %s"), file, dlerror());
|
||||||
|
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void *
|
||||||
|
virDriverLoadModuleFunc(void *handle,
|
||||||
|
const char *funcname)
|
||||||
|
{
|
||||||
|
void *regsym;
|
||||||
|
|
||||||
|
VIR_DEBUG("Lookup function '%s'", funcname);
|
||||||
|
|
||||||
|
if (!(regsym = dlsym(handle, funcname)))
|
||||||
|
VIR_ERROR(_("Missing module registration symbol %s"), funcname);
|
||||||
|
|
||||||
|
return regsym;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virDriverLoadModuleFull:
|
||||||
|
* @path: filename of module to load
|
||||||
|
* @regfunc: name of the function that registers the module
|
||||||
|
* @handle: Returns handle of the loaded library if not NULL
|
||||||
|
*
|
||||||
|
* Loads a loadable module named @path and calls the
|
||||||
|
* registration function @regfunc. If @handle is not NULL the handle is returned
|
||||||
|
* in the variable. Otherwise the handle is leaked so that the module stays
|
||||||
|
* loaded forever.
|
||||||
|
*
|
||||||
|
* The module is automatically looked up in the appropriate place (git or
|
||||||
|
* installed directory).
|
||||||
|
*
|
||||||
|
* Returns 0 on success, 1 if the module was not found and -1 on any error.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
virDriverLoadModuleFull(const char *path,
|
||||||
|
const char *regfunc,
|
||||||
|
void **handle)
|
||||||
|
{
|
||||||
|
void *rethandle = NULL;
|
||||||
|
int (*regsym)(void);
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if (!(rethandle = virDriverLoadModuleFile(path))) {
|
||||||
|
ret = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(regsym = virDriverLoadModuleFunc(rethandle, regfunc)))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if ((*regsym)() < 0) {
|
||||||
|
VIR_ERROR(_("Failed module registration %s"), regfunc);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handle)
|
||||||
|
VIR_STEAL_PTR(*handle, rethandle);
|
||||||
|
else
|
||||||
|
rethandle = NULL;
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (rethandle)
|
||||||
|
dlclose(rethandle);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
virDriverLoadModule(const char *name)
|
virDriverLoadModule(const char *name)
|
||||||
{
|
{
|
||||||
char *modfile = NULL, *regfunc = NULL, *fixedname = NULL;
|
char *modfile = NULL;
|
||||||
|
char *fixedname = NULL;
|
||||||
|
char *regfunc = NULL;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
void *handle = NULL;
|
void *handle = NULL;
|
||||||
int (*regsym)(void);
|
|
||||||
|
|
||||||
VIR_DEBUG("Module load %s", name);
|
VIR_DEBUG("Module load %s", name);
|
||||||
|
|
||||||
@ -61,19 +151,6 @@ virDriverLoadModule(const char *name)
|
|||||||
"LIBVIRT_DRIVER_DIR")))
|
"LIBVIRT_DRIVER_DIR")))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (access(modfile, R_OK) < 0) {
|
|
||||||
VIR_INFO("Module %s not accessible", modfile);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
virUpdateSelfLastChanged(modfile);
|
|
||||||
|
|
||||||
handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
|
|
||||||
if (!handle) {
|
|
||||||
VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (VIR_STRDUP_QUIET(fixedname, name) < 0) {
|
if (VIR_STRDUP_QUIET(fixedname, name) < 0) {
|
||||||
VIR_ERROR(_("out of memory"));
|
VIR_ERROR(_("out of memory"));
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -88,29 +165,13 @@ virDriverLoadModule(const char *name)
|
|||||||
if (virAsprintfQuiet(®func, "%sRegister", fixedname) < 0)
|
if (virAsprintfQuiet(®func, "%sRegister", fixedname) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
regsym = dlsym(handle, regfunc);
|
virDriverLoadModuleFull(modfile, regfunc, &handle);
|
||||||
if (!regsym) {
|
|
||||||
VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((*regsym)() < 0) {
|
|
||||||
VIR_ERROR(_("Failed module registration %s"), regfunc);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
VIR_FREE(modfile);
|
|
||||||
VIR_FREE(regfunc);
|
|
||||||
VIR_FREE(fixedname);
|
|
||||||
return handle;
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(modfile);
|
VIR_FREE(modfile);
|
||||||
VIR_FREE(regfunc);
|
|
||||||
VIR_FREE(fixedname);
|
VIR_FREE(fixedname);
|
||||||
if (handle)
|
VIR_FREE(regfunc);
|
||||||
dlclose(handle);
|
return handle;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,5 +100,8 @@ 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);
|
void *virDriverLoadModule(const char *name);
|
||||||
|
int virDriverLoadModuleFull(const char *name,
|
||||||
|
const char *regfunc,
|
||||||
|
void **handle);
|
||||||
|
|
||||||
#endif /* __VIR_DRIVER_H__ */
|
#endif /* __VIR_DRIVER_H__ */
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
# driver.h
|
# driver.h
|
||||||
virDriverLoadModule;
|
virDriverLoadModule;
|
||||||
|
virDriverLoadModuleFull;
|
||||||
|
|
||||||
# Let emacs know we want case-insensitive sorting
|
# Let emacs know we want case-insensitive sorting
|
||||||
# Local Variables:
|
# Local Variables:
|
||||||
|
Loading…
Reference in New Issue
Block a user