From 0fa7906e996be06601ae5b99f57db6774eb06d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 12 Jan 2019 22:36:45 +0100 Subject: [PATCH] plugin: add plugin_load_particle() function --- plugin.c | 33 +++++++++++++++++++++++++-------- plugin.h | 2 ++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/plugin.c b/plugin.c index 7f59e55..3c3c3bc 100644 --- a/plugin.c +++ b/plugin.c @@ -9,8 +9,12 @@ #include "config.h" #include "tllist.h" +enum plugin_type { PLUGIN_MODULE, PLUGIN_PARTICLE }; + struct plugin { char *name; + enum plugin_type type; + void *lib; const void *sym; }; @@ -36,21 +40,21 @@ fini(void) tll_free_and_free(plugins, free_plugin); } -const struct module_info * -plugin_load_module(const char *name) +static const void * +_load_plugin(const char *name, enum plugin_type type) { - char path[128]; - snprintf(path, sizeof(path), "lib%s.so", name); - /* Have we already loaded it? */ tll_foreach(plugins, plug) { - if (strcmp(plug->item.name, name) == 0) { + if (plug->item.type == type && strcmp(plug->item.name, name) == 0) { LOG_DBG("%s already loaded: %p", name, plug->item.lib); assert(plug->item.sym != NULL); return plug->item.sym; } } + char path[128]; + snprintf(path, sizeof(path), "lib%s.so", name); + /* Not loaded - do it now */ void *lib = dlopen(path, RTLD_LOCAL | RTLD_NOW); LOG_DBG("%s: dlopened to %p", name, lib); @@ -60,11 +64,12 @@ plugin_load_module(const char *name) return NULL; } - tll_push_back(plugins, ((struct plugin){strdup(name), lib})); + tll_push_back(plugins, ((struct plugin){strdup(name), type, lib, NULL})); struct plugin *plug = &tll_back(plugins); + /* TODO: rename to plugin_info or so, in both modules and particles */ dlerror(); /* Clear previous error */ - plug->sym = dlsym(lib, "module_info"); + plug->sym = dlsym(lib, type == PLUGIN_MODULE ? "module_info" : "particle_info"); const char *dlsym_error = dlerror(); if (dlsym_error != NULL) { @@ -75,3 +80,15 @@ plugin_load_module(const char *name) assert(plug->sym != NULL); return plug->sym; } + +const struct module_info * +plugin_load_module(const char *name) +{ + return _load_plugin(name, PLUGIN_MODULE); +} + +const struct particle_info * +plugin_load_particle(const char *name) +{ + return _load_plugin(name, PLUGIN_PARTICLE); +} diff --git a/plugin.h b/plugin.h index 6d829e2..58e4421 100644 --- a/plugin.h +++ b/plugin.h @@ -1,5 +1,7 @@ #pragma once #include "module.h" +#include "particle.h" const struct module_info *plugin_load_module(const char *name); +const struct particle_info *plugin_load_particle(const char *name);