Call the onload function whenever a module is loaded/reload.

Allows one to teardown whatever they setup properly when
the module gets a full reload. See example module for how it works.
This commit is contained in:
Joris Vink 2013-12-21 13:37:34 +01:00
parent ddf23ef65b
commit d22405cea7
4 changed files with 31 additions and 9 deletions

View File

@ -171,10 +171,15 @@ struct kore_handler_params {
#define HANDLER_TYPE_STATIC 1
#define HANDLER_TYPE_DYNAMIC 2
#define KORE_MODULE_LOAD 1
#define KORE_MODULE_UNLOAD 2
struct kore_module {
void *handle;
char *path;
char *onload;
void (*ocb)(int);
time_t mtime;
TAILQ_ENTRY(kore_module) list;

View File

@ -50,7 +50,7 @@ workers 4
# Load modules (you can load multiple at the same time).
# An additional parameter can be specified as the "onload" function
# which Kore will call when the module is loaded/reloaded.
load modules/example/example.module
load modules/example/example.module example_load
# Validators
# validator name type regex|function

View File

@ -19,6 +19,7 @@
#include "static.h"
void example_load(int);
int serve_style_css(struct http_request *);
int serve_index(struct http_request *);
int serve_intro(struct http_request *);
@ -44,6 +45,22 @@ char *b64tests[] = {
NULL
};
void
example_load(int state)
{
switch (state) {
case KORE_MODULE_LOAD:
kore_log(LOG_NOTICE, "module loading");
break;
case KORE_MODULE_UNLOAD:
kore_log(LOG_NOTICE, "module unloading");
break;
default:
kore_log(LOG_NOTICE, "state %d unknown!", state);
break;
}
}
int
serve_style_css(struct http_request *req)
{

View File

@ -35,7 +35,6 @@ kore_module_load(char *path, char *onload)
{
struct stat st;
struct kore_module *module;
void (*cb)(void);
kore_debug("kore_module_load(%s, %s)", path, onload);
@ -53,10 +52,10 @@ kore_module_load(char *path, char *onload)
if (onload != NULL) {
module->onload = kore_strdup(onload);
cb = dlsym(module->handle, onload);
if (cb == NULL)
module->ocb = dlsym(module->handle, onload);
if (module->ocb == NULL)
fatal("%s: onload '%s' not present", path, onload);
cb();
module->ocb(KORE_MODULE_LOAD);
}
if (kore_cb_name != NULL && kore_cb == NULL)
@ -72,7 +71,6 @@ kore_module_reload(void)
struct kore_domain *dom;
struct kore_module_handle *hdlr;
struct kore_module *module;
void (*onload)(void);
kore_cb = NULL;
@ -86,6 +84,8 @@ kore_module_reload(void)
if (module->mtime == st.st_mtime)
continue;
module->ocb(KORE_MODULE_UNLOAD);
module->mtime = st.st_mtime;
if (dlclose(module->handle))
fatal("cannot close existing module: %s", dlerror());
@ -95,13 +95,13 @@ kore_module_reload(void)
fatal("kore_module_reload(): %s", dlerror());
if (module->onload != NULL) {
onload = dlsym(module->handle, module->onload);
if (onload == NULL) {
module->ocb = dlsym(module->handle, module->onload);
if (module->ocb == NULL) {
fatal("%s: onload '%s' not present",
module->path, module->onload);
}
onload();
module->ocb(KORE_MODULE_LOAD);
}
if (kore_cb_name != NULL && kore_cb == NULL)