Clean up the module handling functions

Cleaned up the loading and unloading of modules. Declared variables closer
to where they are used.
This commit is contained in:
Markus Mäkelä
2017-01-03 11:21:56 +02:00
parent bbee47ee13
commit d3907882d6

View File

@ -111,30 +111,16 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
* @param type Type of module, used purely for registration * @param type Type of module, used purely for registration
* @return The module specific entry point structure or NULL * @return The module specific entry point structure or NULL
*/ */
void * void *load_module(const char *module, const char *type)
load_module(const char *module, const char *type)
{ {
char *home, *version; ss_dassert(module && type);
char fname[MAXPATHLEN + 1]; void *modobj;
void *dlhandle, *sym;
char *(*ver)();
void *(*ep)(), *modobj;
MODULES *mod; MODULES *mod;
MODULE_INFO *mod_info = NULL;
if (NULL == module || NULL == type)
{
return NULL;
}
if ((mod = find_module(module)) == NULL) if ((mod = find_module(module)) == NULL)
{ {
/*< /** The module is not already loaded, search for the shared object */
* The module is not already loaded char fname[MAXPATHLEN + 1];
*
* Search of the shared object.
*/
snprintf(fname, MAXPATHLEN + 1, "%s/lib%s.so", get_libdir(), module); snprintf(fname, MAXPATHLEN + 1, "%s/lib%s.so", get_libdir(), module);
if (access(fname, F_OK) == -1) if (access(fname, F_OK) == -1)
@ -145,6 +131,8 @@ load_module(const char *module, const char *type)
return NULL; return NULL;
} }
void *dlhandle;
if ((dlhandle = dlopen(fname, RTLD_NOW | RTLD_LOCAL)) == NULL) if ((dlhandle = dlopen(fname, RTLD_NOW | RTLD_LOCAL)) == NULL)
{ {
MXS_ERROR("Unable to load library for module: " MXS_ERROR("Unable to load library for module: "
@ -155,6 +143,8 @@ load_module(const char *module, const char *type)
return NULL; return NULL;
} }
void *sym;
if ((sym = dlsym(dlhandle, "version")) == NULL) if ((sym = dlsym(dlhandle, "version")) == NULL)
{ {
MXS_ERROR("Version interface not supported by " MXS_ERROR("Version interface not supported by "
@ -164,8 +154,9 @@ load_module(const char *module, const char *type)
dlclose(dlhandle); dlclose(dlhandle);
return NULL; return NULL;
} }
ver = sym;
version = ver(); char *(*ver)() = sym;
char *version = ver();
/* /*
* If the module has a ModuleInit function cal it now. * If the module has a ModuleInit function cal it now.
@ -176,45 +167,48 @@ load_module(const char *module, const char *type)
ModuleInit(); ModuleInit();
} }
MODULE_INFO *mod_info = NULL;
if ((sym = dlsym(dlhandle, "info")) != NULL) if ((sym = dlsym(dlhandle, "info")) != NULL)
{ {
int fatal = 0;
mod_info = sym; mod_info = sym;
bool fatal = false;
if (strcmp(type, MODULE_PROTOCOL) == 0 if (strcmp(type, MODULE_PROTOCOL) == 0
&& mod_info->modapi != MODULE_API_PROTOCOL) && mod_info->modapi != MODULE_API_PROTOCOL)
{ {
MXS_ERROR("Module '%s' does not implement the protocol API.", module); MXS_ERROR("Module '%s' does not implement the protocol API.", module);
fatal = 1; fatal = true;
} }
if (strcmp(type, MODULE_AUTHENTICATOR) == 0 if (strcmp(type, MODULE_AUTHENTICATOR) == 0
&& mod_info->modapi != MODULE_API_AUTHENTICATOR) && mod_info->modapi != MODULE_API_AUTHENTICATOR)
{ {
MXS_ERROR("Module '%s' does not implement the authenticator API.", module); MXS_ERROR("Module '%s' does not implement the authenticator API.", module);
fatal = 1; fatal = true;
} }
if (strcmp(type, MODULE_ROUTER) == 0 if (strcmp(type, MODULE_ROUTER) == 0
&& mod_info->modapi != MODULE_API_ROUTER) && mod_info->modapi != MODULE_API_ROUTER)
{ {
MXS_ERROR("Module '%s' does not implement the router API.", module); MXS_ERROR("Module '%s' does not implement the router API.", module);
fatal = 1; fatal = true;
} }
if (strcmp(type, MODULE_MONITOR) == 0 if (strcmp(type, MODULE_MONITOR) == 0
&& mod_info->modapi != MODULE_API_MONITOR) && mod_info->modapi != MODULE_API_MONITOR)
{ {
MXS_ERROR("Module '%s' does not implement the monitor API.", module); MXS_ERROR("Module '%s' does not implement the monitor API.", module);
fatal = 1; fatal = true;
} }
if (strcmp(type, MODULE_FILTER) == 0 if (strcmp(type, MODULE_FILTER) == 0
&& mod_info->modapi != MODULE_API_FILTER) && mod_info->modapi != MODULE_API_FILTER)
{ {
MXS_ERROR("Module '%s' does not implement the filter API.", module); MXS_ERROR("Module '%s' does not implement the filter API.", module);
fatal = 1; fatal = true;
} }
if (strcmp(type, MODULE_QUERY_CLASSIFIER) == 0 if (strcmp(type, MODULE_QUERY_CLASSIFIER) == 0
&& mod_info->modapi != MODULE_API_QUERY_CLASSIFIER) && mod_info->modapi != MODULE_API_QUERY_CLASSIFIER)
{ {
MXS_ERROR("Module '%s' does not implement the query classifier API.", module); MXS_ERROR("Module '%s' does not implement the query classifier API.", module);
fatal = 1; fatal = true;
} }
if (fatal) if (fatal)
{ {
@ -232,13 +226,11 @@ load_module(const char *module, const char *type)
dlclose(dlhandle); dlclose(dlhandle);
return NULL; return NULL;
} }
ep = sym;
modobj = ep();
MXS_NOTICE("Loaded module %s: %s from %s", void *(*entry_point)() = sym;
module, modobj = entry_point();
version,
fname); MXS_NOTICE("Loaded module %s: %s from %s", module, version, fname);
register_module(module, type, dlhandle, version, modobj, mod_info); register_module(module, type, dlhandle, version, modobj, mod_info);
} }
else else
@ -265,16 +257,14 @@ void
unload_module(const char *module) unload_module(const char *module)
{ {
MODULES *mod = find_module(module); MODULES *mod = find_module(module);
void *handle;
if (!mod) if (mod)
{ {
return; void *handle = mod->handle;
}
handle = mod->handle;
unregister_module(module); unregister_module(module);
dlclose(handle); dlclose(handle);
} }
}
/** /**
* Find a module that has been previously loaded and return the handle for that * Find a module that has been previously loaded and return the handle for that