diff --git a/plugin.c b/plugin.c index 439ec91..9b9e58c 100644 --- a/plugin.c +++ b/plugin.c @@ -12,6 +12,7 @@ struct plugin { char *name; void *lib; + const void *sym; }; static tll(struct plugin) libs = tll_init(); @@ -39,37 +40,33 @@ plugin_load_module(const char *name) char path[128]; snprintf(path, sizeof(path), "./modules/lib%s.so", name); - void *lib = NULL; - /* Have we already loaded it? */ tll_foreach(libs, plug) { if (strcmp(plug->item.name, name) == 0) { - lib = plug->item.lib; - LOG_DBG("%s already loaded: %p", name, lib); - break; + LOG_DBG("%s already loaded: %p", name, plug->item.lib); + assert(plug->item.sym != NULL); + return plug->item.sym; } } + /* Not loaded - do it now */ + void *lib = dlopen(path, RTLD_LOCAL | RTLD_NOW); + LOG_DBG("%s: dlopened to %p", name, lib); + if (lib == NULL) { - /* Not loaded - do it now */ - lib = dlopen(path, RTLD_LOCAL | RTLD_NOW); - LOG_DBG("%s: dlopened to %p", name, lib); - - if (lib == NULL) { - LOG_ERR("%s: dlopen: %s", name, dlerror()); - return NULL; - } - - tll_push_back(libs, ((struct plugin){strdup(name), lib})); + LOG_ERR("%s: dlopen: %s", name, dlerror()); + return NULL; } + tll_push_back(libs, ((struct plugin){strdup(name), lib})); + struct plugin *plug = &tll_back(libs); + /* TODO: use same name in all modules */ char sym[128]; snprintf(sym, sizeof(sym), "module_%s", name); - /* TODO: cache symbol */ dlerror(); /* Clear previous error */ - const struct module_info *info = dlsym(lib, sym); + plug->sym = dlsym(lib, sym); const char *dlsym_error = dlerror(); if (dlsym_error != NULL) { @@ -77,6 +74,6 @@ plugin_load_module(const char *name) return NULL; } - assert(info != NULL); - return info; + assert(plug->sym != NULL); + return plug->sym; }