123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <assert.h>
- #include <dlfcn.h>
- #include <faux/str.h>
- #include <faux/list.h>
- #include <faux/conv.h>
- #include <faux/error.h>
- #include <klish/khelper.h>
- #include <klish/kplugin.h>
- #include <klish/ksym.h>
- struct kplugin_s {
- char *name;
- char *id;
- char *file;
- bool_t global;
- char *conf;
- uint8_t major;
- uint8_t minor;
- void *dlhan; // dlopen() handler
- void *init_fn;
- void *fini_fn;
- void *udata; // User data
- faux_list_t *syms;
- };
- // Simple methods
- // Name
- KGET_STR(plugin, name);
- KSET_STR_ONCE(plugin, name);
- // ID
- KGET_STR(plugin, id);
- KSET_STR(plugin, id);
- // File
- KGET_STR(plugin, file);
- KSET_STR(plugin, file);
- // Global
- KGET_BOOL(plugin, global);
- KSET_BOOL(plugin, global);
- // Conf
- KGET_STR(plugin, conf);
- KSET_STR(plugin, conf);
- // Version major number
- KGET(plugin, uint8_t, major);
- KSET(plugin, uint8_t, major);
- // Version minor number
- KGET(plugin, uint8_t, minor);
- KSET(plugin, uint8_t, minor);
- // User data
- KGET(plugin, void *, udata);
- KSET(plugin, void *, udata);
- // COMMAND list
- static KCMP_NESTED(plugin, sym, name);
- static KCMP_NESTED_BY_KEY(plugin, sym, name);
- KADD_NESTED(plugin, sym);
- KFIND_NESTED(plugin, sym);
- static kplugin_t *kplugin_new_empty(void)
- {
- kplugin_t *plugin = NULL;
- plugin = faux_zmalloc(sizeof(*plugin));
- assert(plugin);
- if (!plugin)
- return NULL;
- // Initialize
- plugin->name = NULL;
- plugin->id = NULL;
- plugin->file = NULL;
- plugin->global = BOOL_FALSE;
- plugin->conf = NULL;
- plugin->major = 0;
- plugin->minor = 0;
- plugin->dlhan = NULL;
- plugin->init_fn = NULL;
- plugin->fini_fn = NULL;
- plugin->udata = NULL;
- // SYM list
- plugin->syms = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
- kplugin_sym_compare, kplugin_sym_kcompare,
- (void (*)(void *))ksym_free);
- assert(plugin->syms);
- return plugin;
- }
- kplugin_t *kplugin_new(const iplugin_t *info, kplugin_error_e *error)
- {
- kplugin_t *plugin = NULL;
- plugin = kplugin_new_empty();
- assert(plugin);
- if (!plugin) {
- if (error)
- *error = KPLUGIN_ERROR_ALLOC;
- return NULL;
- }
- if (!info)
- return plugin;
- if (!kplugin_parse(plugin, info, error)) {
- kplugin_free(plugin);
- return NULL;
- }
- return plugin;
- }
- void kplugin_free(kplugin_t *plugin)
- {
- if (!plugin)
- return;
- faux_str_free(plugin->name);
- faux_str_free(plugin->id);
- faux_str_free(plugin->file);
- faux_str_free(plugin->conf);
- faux_list_free(plugin->syms);
- if (plugin->dlhan)
- dlclose(plugin->dlhan);
- faux_free(plugin);
- }
- const char *kplugin_strerror(kplugin_error_e error)
- {
- const char *str = NULL;
- switch (error) {
- case KPLUGIN_ERROR_OK:
- str = "Ok";
- break;
- case KPLUGIN_ERROR_INTERNAL:
- str = "Internal error";
- break;
- case KPLUGIN_ERROR_ALLOC:
- str = "Memory allocation error";
- break;
- case KPLUGIN_ERROR_ATTR_NAME:
- str = "Illegal 'name' attribute";
- break;
- case KPLUGIN_ERROR_ATTR_ID:
- str = "Illegal 'id' attribute";
- break;
- case KPLUGIN_ERROR_ATTR_FILE:
- str = "Illegal 'file' attribute";
- break;
- case KPLUGIN_ERROR_ATTR_GLOBAL:
- str = "Illegal 'global' attribute";
- break;
- case KPLUGIN_ERROR_ATTR_CONF:
- str = "Illegal conf";
- break;
- default:
- str = "Unknown error";
- break;
- }
- return str;
- }
- bool_t kplugin_parse(kplugin_t *plugin, const iplugin_t *info, kplugin_error_e *error)
- {
- // Name [mandatory]
- if (faux_str_is_empty(info->name)) {
- if (error)
- *error = KPLUGIN_ERROR_ATTR_NAME;
- return BOOL_FALSE;
- } else {
- if (!kplugin_set_name(plugin, info->name)) {
- if (error)
- *error = KPLUGIN_ERROR_ATTR_NAME;
- return BOOL_FALSE;
- }
- }
- // ID
- if (!faux_str_is_empty(info->id)) {
- if (!kplugin_set_id(plugin, info->id)) {
- if (error)
- *error = KPLUGIN_ERROR_ATTR_ID;
- return BOOL_FALSE;
- }
- }
- // File
- if (!faux_str_is_empty(info->file)) {
- if (!kplugin_set_file(plugin, info->file)) {
- if (error)
- *error = KPLUGIN_ERROR_ATTR_FILE;
- return BOOL_FALSE;
- }
- }
- // Global
- if (!faux_str_is_empty(info->global)) {
- bool_t b = BOOL_FALSE;
- if (!faux_conv_str2bool(info->global, &b) ||
- !kplugin_set_global(plugin, b)) {
- if (error)
- *error = KPLUGIN_ERROR_ATTR_GLOBAL;
- return BOOL_FALSE;
- }
- }
- // Conf
- if (!faux_str_is_empty(info->conf)) {
- if (!kplugin_set_conf(plugin, info->conf)) {
- if (error)
- *error = KPLUGIN_ERROR_ATTR_CONF;
- return BOOL_FALSE;
- }
- }
- return BOOL_TRUE;
- }
- kplugin_t *kplugin_from_iplugin(iplugin_t *iplugin, faux_error_t *error_stack)
- {
- kplugin_t *kplugin = NULL;
- kplugin_error_e kplugin_error = KPLUGIN_ERROR_OK;
- kplugin = kplugin_new(iplugin, &kplugin_error);
- if (!kplugin) {
- faux_error_sprintf(error_stack, "PLUGIN \"%s\": %s",
- iplugin->name ? iplugin->name : "(null)",
- kplugin_strerror(kplugin_error));
- return NULL;
- }
- return kplugin;
- }
- bool_t kplugin_load(kplugin_t *plugin)
- {
- char *file_name = NULL;
- char *init_name = NULL;
- char *fini_name = NULL;
- char *major_name = NULL;
- char *minor_name = NULL;
- int flag = RTLD_NOW;
- const char *id = NULL;
- bool_t retcode = BOOL_FALSE;
- uint8_t *ver = NULL;
- assert(plugin);
- if (!plugin)
- return BOOL_FALSE;
- if (kplugin_id(plugin))
- id = kplugin_id(plugin);
- else
- id = kplugin_name(plugin);
- // Shared object file name
- if (kplugin_file(plugin))
- file_name = faux_str_dup(kplugin_file(plugin));
- else
- file_name = faux_str_sprintf(KPLUGIN_SONAME_FMT, id);
- // Symbol names
- major_name = faux_str_sprintf(KPLUGIN_MAJOR_FMT, id);
- minor_name = faux_str_sprintf(KPLUGIN_MINOR_FMT, id);
- init_name = faux_str_sprintf(KPLUGIN_INIT_FMT, id);
- fini_name = faux_str_sprintf(KPLUGIN_FINI_FMT, id);
- // SO flags
- if (kplugin_global(plugin))
- flag |= RTLD_GLOBAL;
- else
- flag |= RTLD_LOCAL;
- // Open shared object
- plugin->dlhan = dlopen(file_name, flag);
- if (!plugin->dlhan) {
- // fprintf(stderr, "Error: Can't open plugin \"%s\": %s\n",
- // this->name, dlerror());
- goto err;
- }
- // Get plugin version
- ver = (uint8_t *)dlsym(plugin->dlhan, major_name);
- if (!ver)
- goto err;
- kplugin_set_major(plugin, *ver);
- ver = (uint8_t *)dlsym(plugin->dlhan, minor_name);
- if (!ver)
- goto err;
- kplugin_set_minor(plugin, *ver);
- // Get plugin init function
- plugin->init_fn = dlsym(plugin->dlhan, init_name);
- if (!plugin->init_fn) {
- // fprintf(stderr, "Error: Can't get plugin \"%s\" init function: %s\n",
- // this->name, dlerror());
- goto err;
- }
- // Get plugin fini function
- plugin->fini_fn = dlsym(plugin->dlhan, fini_name);
- retcode = BOOL_TRUE;
- err:
- faux_str_free(file_name);
- faux_str_free(major_name);
- faux_str_free(minor_name);
- faux_str_free(init_name);
- faux_str_free(fini_name);
- if (!retcode && plugin->dlhan)
- dlclose(plugin->dlhan);
- return retcode;
- }
|