ant-http/plugin_manager.c

251 lines
7.0 KiB
C
Raw Normal View History

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>
2015-10-22 11:39:11 +02:00
#include "plugin_manager.h"
2020-08-25 16:40:24 +02:00
#include "lib/utils.h"
#include "lib/handle.h"
#include "http_server.h"
2023-01-15 18:21:42 +01:00
static void unload_plugin_by_name(const char *);
static void *plugin_from_file(char *name, char *path, dictionary_t conf);
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
*/
struct plugin_entry *plugin_lookup(char *s)
{
struct plugin_entry *np;
2015-11-24 17:58:32 +01:00
for (np = plugin_table[hash(s, HASHSIZE)]; np != NULL; np = np->next)
2015-10-22 11:39:11 +02:00
if (strcmp(s, np->pname) == 0)
2023-01-15 18:21:42 +01:00
return np; /* found */
return NULL; /* not found */
2020-01-03 10:14:04 +01:00
}
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
* @param config: plugin configuration
2015-10-22 11:39:11 +02:00
* @return pointer to the loaded plugin
*/
2023-01-15 18:21:42 +01:00
struct plugin_entry *plugin_load(char *name, dictionary_t pconf)
2015-10-22 11:39:11 +02:00
{
2023-01-15 18:21:42 +01:00
char *pname = NULL;
char path[BUFFLEN];
2015-10-22 11:39:11 +02:00
struct plugin_entry *np;
unsigned hashval;
2023-01-15 18:21:42 +01:00
plugin_header_t *(*metafn)();
2023-01-11 22:18:45 +01:00
plugin_header_t *meta = NULL;
2023-01-15 18:21:42 +01:00
config_t *sconf = config();
int fromfd, tofd;
char *error;
struct stat st;
int is_tmp = 0;
if (pconf)
{
pname = dvalue(pconf, "name");
}
if ((np = plugin_lookup(name)) == NULL)
{ /* not found */
LOG("Loading plugin: %s...", name);
np = (struct plugin_entry *)malloc(sizeof(*np));
2020-01-08 22:04:10 +01:00
if (np == NULL || name == NULL)
{
2023-01-15 18:21:42 +01:00
if (np)
free(np);
return NULL;
}
(void)snprintf(path, sizeof(path), "%s/%s%s", sconf->plugins_dir, name, sconf->plugins_ext);
if (pname && strcmp(name, pname) != 0)
{
// copy plugin file to tmpdir
(void)snprintf(path, sizeof(path), "%s/%s%s", sconf->plugins_dir, pname, sconf->plugins_ext);
LOG("Original plugin file: %s", path);
if ((fromfd = open(path, O_RDONLY)) < 0)
{
ERROR("Unable to open file for reading %s: %s", path, strerror(errno));
return NULL;
}
if (stat(path, &st) != 0)
{
close(fromfd);
ERROR("Unable to get file stat %s: %s", path, strerror(errno));
return NULL;
}
(void)snprintf(path, sizeof(path), "%s/%s%s", sconf->tmpdir, name, sconf->plugins_ext);
LOG("TMP plugin file: %s", path);
if ((tofd = open(path, O_WRONLY | O_CREAT, 0600)) < 0)
{
close(fromfd);
ERROR("Unable open file for reading %s: %s", path, strerror(errno));
return NULL;
}
if (sendfile(tofd, fromfd, NULL, st.st_size) != st.st_size)
{
close(fromfd);
close(tofd);
ERROR("Unable to copy file: %s", strerror(errno));
return NULL;
}
is_tmp = 1;
}
np->pname = strdup(name);
np->handle = plugin_from_file(name, path, pconf);
if (is_tmp)
{
(void)remove(path);
}
if (np->handle == NULL)
{
if (np->pname)
free(np->pname);
if (np)
free(np);
return NULL;
}
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
{
2023-01-15 18:21:42 +01:00
LOG("The plugin %s id already loaded", name);
2015-10-22 11:39:11 +02:00
}
2023-01-11 22:18:45 +01:00
2023-01-15 18:21:42 +01:00
// check if plugin is ready
metafn = (plugin_header_t * (*)()) dlsym(np->handle, "meta");
2023-01-11 22:18:45 +01:00
if ((error = dlerror()) != NULL)
{
2023-01-15 18:21:42 +01:00
ERROR("Unable to fetch plugin meta-data: [%s] %s", name, error);
2023-01-11 22:18:45 +01:00
unload_plugin_by_name(name);
2023-01-15 18:21:42 +01:00
free(np);
return NULL;
2023-01-11 22:18:45 +01:00
}
2023-01-15 18:21:42 +01:00
meta = metafn();
LOG("PLugin status: [%s] %d", name, meta->status);
if (!meta || meta->status != ANTD_PLUGIN_READY)
{
ERROR("Plugin is not ready or error: [%s].", name);
2023-01-11 22:18:45 +01:00
unload_plugin_by_name(name);
2023-01-15 18:21:42 +01:00
free(np);
return NULL;
}
2015-10-22 11:39:11 +02:00
return np;
}
/**
* Find a plugin in a file, and load it in to the plugin table
* @param name Name of the plugin
2023-01-15 18:21:42 +01:00
* @return
2015-10-22 11:39:11 +02:00
*/
2023-01-15 18:21:42 +01:00
static void *plugin_from_file(char *name, char *path, dictionary_t conf)
2015-10-22 11:39:11 +02:00
{
2023-01-15 18:21:42 +01:00
void *lib_handle;
char *error;
config_t *cnf = config();
void (*fn)(plugin_header_t *, dictionary_t);
lib_handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL /*| RTLD_NODELETE*/);
if (!lib_handle)
{
ERROR("Cannot load plugin '%s' : '%s'", name, dlerror());
return NULL;
}
// set database path
fn = (void (*)(plugin_header_t *, dictionary_t))dlsym(lib_handle, "__init_plugin__");
if ((error = dlerror()) != NULL)
ERROR("Problem when finding plugin init function for %s : %s", name, error);
else
{
plugin_header_t header;
strncpy(header.name, name, MAX_PATH_LEN - 1);
strncpy(header.dbpath, cnf->db_path, MAX_PATH_LEN - 1);
strncpy(header.tmpdir, cnf->tmpdir, MAX_PATH_LEN - 1);
strncpy(header.pdir, cnf->plugins_dir, MAX_PATH_LEN - 1);
header.config = conf;
header.raw_body = 0;
header.status = ANTD_PLUGIN_INIT;
(*fn)(&header, conf);
}
// trick libc that we close this lib, but it is not realy deleted
return lib_handle;
2015-10-22 11:39:11 +02:00
}
2023-01-15 18:21:42 +01:00
void unload_plugin(struct plugin_entry *np)
2016-03-04 11:38:08 +01:00
{
2023-01-15 18:21:42 +01:00
char *error;
void (*fn)() = NULL;
// find and execute the exit function
fn = (void (*)())dlsym(np->handle, "__release__");
if ((error = dlerror()) != NULL)
{
ERROR("Cant not release plugin %s : %s", np->pname, error);
2018-03-02 19:04:00 +01:00
}
2023-01-15 18:21:42 +01:00
if (fn)
{
(*fn)();
}
dlclose(np->handle);
// free((void *) np->handle);
if (np->pname)
free((void *)np->pname);
2016-03-04 11:38:08 +01:00
}
/*
2023-01-15 18:21:42 +01:00
Unload a plugin by its name
2016-03-04 11:38:08 +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];
if (strcmp(np->pname, name) == 0)
{
unload_plugin(np);
plugin_table[hasval] = np->next;
}
else
{
for (np = plugin_table[hasval]; np != NULL; np = np->next)
{
if (np->next != NULL && strcmp(name, np->next->pname) == 0)
{
break;
}
}
if (np == NULL)
return; // the plugin is is not loaded
unload_plugin(np->next);
np->next = np->next->next;
}
2016-03-04 11:38:08 +01:00
}
2015-10-22 11:39:11 +02:00
/**
* Unload all the plugin loaded on the plugin table
*/
void unload_all_plugin()
{
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;
unload_plugin(curr);
free(curr);
}
2015-10-22 11:39:11 +02:00
plugin_table[i] = NULL;
2023-01-15 18:21:42 +01:00
}
exit(0);
2015-10-22 11:39:11 +02:00
}