2020-08-25 16:40:24 +02:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <string.h>
|
2023-01-15 18:21:42 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <unistd.h>
|
2024-03-12 21:59:50 +01:00
|
|
|
#include <stdio.h>
|
2020-08-25 16:40:24 +02:00
|
|
|
#include "lib/utils.h"
|
|
|
|
#include "lib/handle.h"
|
2024-03-12 21:59:50 +01:00
|
|
|
#include "config.h"
|
2024-03-13 18:07:07 +01:00
|
|
|
#include "lib/plugin_ctx.h"
|
|
|
|
#include "plugin_manager.h"
|
2024-03-12 21:59:50 +01:00
|
|
|
|
2024-03-13 18:07:07 +01:00
|
|
|
struct plugin_entry {
|
|
|
|
struct plugin_entry *next;
|
|
|
|
char *name;
|
|
|
|
void *handle;
|
|
|
|
dictionary_t instances;
|
|
|
|
};
|
2024-03-12 21:59:50 +01:00
|
|
|
|
2024-03-13 18:07:07 +01:00
|
|
|
extern config_t g_server_config;
|
2023-01-09 16:31:59 +01:00
|
|
|
|
2015-10-22 11:39:11 +02:00
|
|
|
/**
|
|
|
|
* Plugin table to store the loaded plugin
|
|
|
|
*/
|
2023-01-15 18:21:42 +01:00
|
|
|
static struct plugin_entry *plugin_table[HASHSIZE];
|
2015-10-22 11:39:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Locate a plugin in the plugin table
|
|
|
|
* @param s plugin name
|
|
|
|
* @return a plugin entry in the plugin table
|
|
|
|
*/
|
2024-03-13 18:07:07 +01:00
|
|
|
static struct plugin_entry *plugin_lookup(const char *s)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
if(!s)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
struct plugin_entry *np;
|
2015-11-24 17:58:32 +01:00
|
|
|
for (np = plugin_table[hash(s, HASHSIZE)]; np != NULL; np = np->next)
|
2024-03-12 21:59:50 +01:00
|
|
|
if (strcmp(s, np->name) == 0)
|
2023-01-15 18:21:42 +01:00
|
|
|
return np; /* found */
|
|
|
|
return NULL; /* not found */
|
2020-01-03 10:14:04 +01:00
|
|
|
}
|
|
|
|
|
2024-03-13 18:07:07 +01:00
|
|
|
static void antd_plugin_ctx_drop(struct plugin_entry* np, antd_plugin_ctx_t* ctx)
|
|
|
|
{
|
|
|
|
if(!ctx)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(ctx->drop)
|
|
|
|
{
|
|
|
|
if(ctx->name)
|
|
|
|
LOG("Release user resource for context: %s", ctx->name);
|
|
|
|
ctx->drop((void*)ctx);
|
|
|
|
}
|
|
|
|
if(ctx->name)
|
|
|
|
{
|
|
|
|
LOG("Dropping plugin context: %s", ctx->name);
|
|
|
|
if(np->instances)
|
|
|
|
{
|
|
|
|
dput(np->instances, ctx->name, NULL);
|
|
|
|
}
|
|
|
|
free(ctx->name);
|
|
|
|
}
|
|
|
|
if(ctx->confdir)
|
|
|
|
{
|
|
|
|
free(ctx->confdir);
|
|
|
|
}
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static antd_plugin_ctx_t* antd_plugin_ctx_lookup(struct plugin_entry* np, const char* name)
|
|
|
|
{
|
|
|
|
if(!np || !np->instances)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
LOG("Looking for plugin context: %s", name);
|
|
|
|
antd_plugin_ctx_t* ctx = dvalue(np->instances, name);
|
|
|
|
if(ctx == NULL)
|
|
|
|
{
|
|
|
|
char *error;
|
|
|
|
LOG("Create new plugin context: %s", name);
|
|
|
|
ctx = (antd_plugin_ctx_t *)malloc(sizeof(antd_plugin_ctx_t));
|
|
|
|
if(!ctx)
|
|
|
|
{
|
|
|
|
ERROR("Unable to allocate memory for plugin context `%s`: %s", name, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// init the context
|
|
|
|
ctx->basedir = g_server_config.plugins_dir;
|
|
|
|
ctx->tmpdir = g_server_config.tmpdir;
|
|
|
|
ctx->name = strdup(name);
|
|
|
|
ctx->confdir = NULL;
|
|
|
|
ctx->raw_body = 0;
|
|
|
|
ctx->status = ANTD_PLUGIN_INIT;
|
|
|
|
ctx->config=dvalue(g_server_config.plugins, name);
|
|
|
|
ctx->data = NULL;
|
|
|
|
ctx->handle = NULL;
|
|
|
|
ctx->create = NULL;
|
|
|
|
ctx->drop = NULL;
|
|
|
|
// look for handle function
|
|
|
|
ctx->handle = (void* (*)(void *))dlsym(np->handle, PLUGIN_HANDLE);
|
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
ERROR("Problem when finding plugin handle function for %s : %s", name, error);
|
|
|
|
ctx->handle = NULL;
|
|
|
|
antd_plugin_ctx_drop(np, ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// look for drop function
|
|
|
|
ctx->drop = (void (*)(antd_plugin_ctx_t *))dlsym(np->handle, PLUGIN_DROP);
|
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
ERROR("Problem when finding plugin drop function for %s : %s", name, error);
|
|
|
|
ctx->drop = NULL;
|
|
|
|
antd_plugin_ctx_drop(np, ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// look for init function
|
|
|
|
ctx->create = (void* (*)(antd_plugin_ctx_t *))dlsym(np->handle, PLUGIN_INIT);
|
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
ERROR("Problem when finding plugin init function for %s : %s.", name, error);
|
|
|
|
ctx->create = NULL;
|
|
|
|
antd_plugin_ctx_drop(np, ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// run the init function
|
|
|
|
ctx->data = ctx->create(ctx);
|
|
|
|
if(ctx->status == ANTD_PLUGIN_PANNIC)
|
|
|
|
{
|
|
|
|
ERROR("PANIC happens when init plugin context %s. drop it", name);
|
|
|
|
antd_plugin_ctx_drop(np, ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ctx->status = ANTD_PLUGIN_READY;
|
|
|
|
}
|
|
|
|
dput(np->instances, name, (void*)ctx);
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void antd_plugin_entry_drop(struct plugin_entry* np)
|
|
|
|
{
|
|
|
|
if(!np)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(np->name)
|
|
|
|
{
|
|
|
|
LOG("Unloaded %s", np->name);
|
|
|
|
free(np->name);
|
|
|
|
}
|
|
|
|
if(np->instances)
|
|
|
|
{
|
|
|
|
chain_t it;
|
|
|
|
for_each_assoc(it, np->instances)
|
|
|
|
{
|
|
|
|
antd_plugin_ctx_drop(np,(antd_plugin_ctx_t*)it->value);
|
|
|
|
}
|
|
|
|
freedict(np->instances);
|
|
|
|
}
|
|
|
|
if(np->handle)
|
|
|
|
{
|
|
|
|
dlclose(np->handle);
|
|
|
|
}
|
|
|
|
free(np);
|
|
|
|
}
|
|
|
|
|
2015-10-22 11:39:11 +02:00
|
|
|
/**
|
|
|
|
* Load a plugin to the plugin table
|
|
|
|
* Only load when not available in the plugin table
|
|
|
|
* @param name plugin name
|
2023-01-09 16:31:59 +01:00
|
|
|
* @param config: plugin configuration
|
2015-10-22 11:39:11 +02:00
|
|
|
* @return pointer to the loaded plugin
|
|
|
|
*/
|
2024-03-13 18:07:07 +01:00
|
|
|
antd_plugin_ctx_t* antd_plugin_load(const char *name)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
const char *plugin_file_name = NULL;
|
2023-01-15 18:21:42 +01:00
|
|
|
char path[BUFFLEN];
|
2015-10-22 11:39:11 +02:00
|
|
|
struct plugin_entry *np;
|
|
|
|
unsigned hashval;
|
2024-03-13 18:07:07 +01:00
|
|
|
antd_plugin_ctx_t *ctx;
|
|
|
|
dictionary_t pconf = dvalue(g_server_config.plugins, name);
|
2023-01-15 18:21:42 +01:00
|
|
|
if (pconf)
|
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
plugin_file_name = dvalue(pconf, "name");
|
2023-01-15 18:21:42 +01:00
|
|
|
}
|
2024-03-13 18:07:07 +01:00
|
|
|
if(plugin_file_name == NULL)
|
|
|
|
{
|
|
|
|
plugin_file_name = name;
|
|
|
|
}
|
|
|
|
if(plugin_file_name == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((np = plugin_lookup(plugin_file_name)) == NULL)
|
2023-01-15 18:21:42 +01:00
|
|
|
{ /* not found */
|
2024-03-13 18:07:07 +01:00
|
|
|
LOG("Loading plugin: %s...", plugin_file_name);
|
|
|
|
np = (struct plugin_entry *)malloc(sizeof(struct plugin_entry));
|
|
|
|
np->instances = NULL;
|
|
|
|
if (np == NULL)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2023-01-15 18:21:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-03-13 18:07:07 +01:00
|
|
|
(void)snprintf(path, sizeof(path), "%s/%s%s", g_server_config.plugins_dir, plugin_file_name, g_server_config.plugins_ext);
|
|
|
|
np->name = strdup(plugin_file_name);
|
|
|
|
// now load it from file
|
|
|
|
np->handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL /*| RTLD_NODELETE*/);
|
|
|
|
if (!np->handle)
|
2023-01-15 18:21:42 +01:00
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
ERROR("Cannot load plugin '%s' : '%s'", plugin_file_name, dlerror());
|
|
|
|
antd_plugin_entry_drop(np);
|
2023-01-15 18:21:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2024-03-13 18:07:07 +01:00
|
|
|
np->instances = dict();
|
2023-01-15 18:21:42 +01:00
|
|
|
hashval = hash(name, HASHSIZE);
|
2015-10-22 11:39:11 +02:00
|
|
|
np->next = plugin_table[hashval];
|
|
|
|
plugin_table[hashval] = np;
|
2023-01-15 18:21:42 +01:00
|
|
|
}
|
|
|
|
else /* already there */
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
LOG("The plugin %s id already loaded", plugin_file_name);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2024-03-13 18:07:07 +01:00
|
|
|
// now look for the plugin context
|
|
|
|
ctx = antd_plugin_ctx_lookup(np, name);
|
2023-01-15 18:21:42 +01:00
|
|
|
// check if plugin is ready
|
2024-03-13 18:07:07 +01:00
|
|
|
if (ctx == NULL)
|
2023-01-11 22:18:45 +01:00
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
ERROR("Unable to fetch plugin context for: [%s] %s", plugin_file_name, name);
|
2023-01-15 18:21:42 +01:00
|
|
|
return NULL;
|
2023-01-11 22:18:45 +01:00
|
|
|
}
|
2024-03-13 18:07:07 +01:00
|
|
|
LOG("PLugin instance status: [%s] %d", name, ctx->status);
|
|
|
|
if (ctx->status != ANTD_PLUGIN_READY)
|
2023-01-15 18:21:42 +01:00
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
ERROR("Plugin instance is not ready or error: [%s].", name);
|
|
|
|
antd_plugin_ctx_drop(np, ctx);
|
2023-01-15 18:21:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2024-03-13 18:07:07 +01:00
|
|
|
return ctx;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-04 11:38:08 +01:00
|
|
|
/*
|
2023-01-15 18:21:42 +01:00
|
|
|
Unload a plugin by its name
|
2024-03-13 18:07:07 +01:00
|
|
|
|
2023-01-15 18:21:42 +01:00
|
|
|
void unload_plugin_by_name(const char *name)
|
2016-03-04 11:38:08 +01:00
|
|
|
{
|
2023-01-15 18:21:42 +01:00
|
|
|
struct plugin_entry *np;
|
|
|
|
int hasval = hash(name, HASHSIZE);
|
|
|
|
np = plugin_table[hasval];
|
2024-03-12 21:59:50 +01:00
|
|
|
if (strcmp(np->name, name) == 0)
|
2023-01-15 18:21:42 +01:00
|
|
|
{
|
2024-03-13 18:07:07 +01:00
|
|
|
antd_plugin_entry_drop(np);
|
2023-01-15 18:21:42 +01:00
|
|
|
plugin_table[hasval] = np->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (np = plugin_table[hasval]; np != NULL; np = np->next)
|
|
|
|
{
|
2024-03-12 21:59:50 +01:00
|
|
|
if (np->next != NULL && strcmp(name, np->next->name) == 0)
|
2023-01-15 18:21:42 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (np == NULL)
|
|
|
|
return; // the plugin is is not loaded
|
2024-03-13 18:07:07 +01:00
|
|
|
antd_plugin_entry_drop(np->next);
|
2023-01-15 18:21:42 +01:00
|
|
|
np->next = np->next->next;
|
|
|
|
}
|
2016-03-04 11:38:08 +01:00
|
|
|
}
|
2024-03-13 18:07:07 +01:00
|
|
|
*/
|
|
|
|
|
2015-10-22 11:39:11 +02:00
|
|
|
/**
|
|
|
|
* Unload all the plugin loaded on the plugin table
|
|
|
|
*/
|
2024-03-13 18:07:07 +01:00
|
|
|
void antd_unload_all_plugin()
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2023-01-15 18:21:42 +01:00
|
|
|
LOG("Unload all plugins");
|
|
|
|
for (int i = 0; i < HASHSIZE; i++)
|
|
|
|
{
|
|
|
|
struct plugin_entry **np, *curr;
|
|
|
|
np = &plugin_table[i];
|
2018-03-02 19:04:00 +01:00
|
|
|
|
2023-01-15 18:21:42 +01:00
|
|
|
while ((curr = *np) != NULL)
|
|
|
|
{
|
|
|
|
(*np) = (*np)->next;
|
2024-03-13 18:07:07 +01:00
|
|
|
antd_plugin_entry_drop(curr);
|
|
|
|
//free(curr);
|
2023-01-15 18:21:42 +01:00
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
plugin_table[i] = NULL;
|
2023-01-15 18:21:42 +01:00
|
|
|
}
|
2024-03-13 18:07:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
antd_plugin_handle_t antd_get_ctx_handle(antd_plugin_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
return ctx->handle;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|