mirror of
https://github.com/jorisvink/kore
synced 2025-03-10 04:59:02 -04:00
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:
parent
ddf23ef65b
commit
d22405cea7
@ -171,10 +171,15 @@ struct kore_handler_params {
|
|||||||
#define HANDLER_TYPE_STATIC 1
|
#define HANDLER_TYPE_STATIC 1
|
||||||
#define HANDLER_TYPE_DYNAMIC 2
|
#define HANDLER_TYPE_DYNAMIC 2
|
||||||
|
|
||||||
|
#define KORE_MODULE_LOAD 1
|
||||||
|
#define KORE_MODULE_UNLOAD 2
|
||||||
|
|
||||||
struct kore_module {
|
struct kore_module {
|
||||||
void *handle;
|
void *handle;
|
||||||
char *path;
|
char *path;
|
||||||
char *onload;
|
char *onload;
|
||||||
|
void (*ocb)(int);
|
||||||
|
|
||||||
time_t mtime;
|
time_t mtime;
|
||||||
|
|
||||||
TAILQ_ENTRY(kore_module) list;
|
TAILQ_ENTRY(kore_module) list;
|
||||||
|
@ -50,7 +50,7 @@ workers 4
|
|||||||
# Load modules (you can load multiple at the same time).
|
# Load modules (you can load multiple at the same time).
|
||||||
# An additional parameter can be specified as the "onload" function
|
# An additional parameter can be specified as the "onload" function
|
||||||
# which Kore will call when the module is loaded/reloaded.
|
# which Kore will call when the module is loaded/reloaded.
|
||||||
load modules/example/example.module
|
load modules/example/example.module example_load
|
||||||
|
|
||||||
# Validators
|
# Validators
|
||||||
# validator name type regex|function
|
# validator name type regex|function
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include "static.h"
|
#include "static.h"
|
||||||
|
|
||||||
|
void example_load(int);
|
||||||
int serve_style_css(struct http_request *);
|
int serve_style_css(struct http_request *);
|
||||||
int serve_index(struct http_request *);
|
int serve_index(struct http_request *);
|
||||||
int serve_intro(struct http_request *);
|
int serve_intro(struct http_request *);
|
||||||
@ -44,6 +45,22 @@ char *b64tests[] = {
|
|||||||
NULL
|
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
|
int
|
||||||
serve_style_css(struct http_request *req)
|
serve_style_css(struct http_request *req)
|
||||||
{
|
{
|
||||||
|
16
src/module.c
16
src/module.c
@ -35,7 +35,6 @@ kore_module_load(char *path, char *onload)
|
|||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
struct kore_module *module;
|
struct kore_module *module;
|
||||||
void (*cb)(void);
|
|
||||||
|
|
||||||
kore_debug("kore_module_load(%s, %s)", path, onload);
|
kore_debug("kore_module_load(%s, %s)", path, onload);
|
||||||
|
|
||||||
@ -53,10 +52,10 @@ kore_module_load(char *path, char *onload)
|
|||||||
|
|
||||||
if (onload != NULL) {
|
if (onload != NULL) {
|
||||||
module->onload = kore_strdup(onload);
|
module->onload = kore_strdup(onload);
|
||||||
cb = dlsym(module->handle, onload);
|
module->ocb = dlsym(module->handle, onload);
|
||||||
if (cb == NULL)
|
if (module->ocb == NULL)
|
||||||
fatal("%s: onload '%s' not present", path, onload);
|
fatal("%s: onload '%s' not present", path, onload);
|
||||||
cb();
|
module->ocb(KORE_MODULE_LOAD);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kore_cb_name != NULL && kore_cb == NULL)
|
if (kore_cb_name != NULL && kore_cb == NULL)
|
||||||
@ -72,7 +71,6 @@ kore_module_reload(void)
|
|||||||
struct kore_domain *dom;
|
struct kore_domain *dom;
|
||||||
struct kore_module_handle *hdlr;
|
struct kore_module_handle *hdlr;
|
||||||
struct kore_module *module;
|
struct kore_module *module;
|
||||||
void (*onload)(void);
|
|
||||||
|
|
||||||
kore_cb = NULL;
|
kore_cb = NULL;
|
||||||
|
|
||||||
@ -86,6 +84,8 @@ kore_module_reload(void)
|
|||||||
if (module->mtime == st.st_mtime)
|
if (module->mtime == st.st_mtime)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
module->ocb(KORE_MODULE_UNLOAD);
|
||||||
|
|
||||||
module->mtime = st.st_mtime;
|
module->mtime = st.st_mtime;
|
||||||
if (dlclose(module->handle))
|
if (dlclose(module->handle))
|
||||||
fatal("cannot close existing module: %s", dlerror());
|
fatal("cannot close existing module: %s", dlerror());
|
||||||
@ -95,13 +95,13 @@ kore_module_reload(void)
|
|||||||
fatal("kore_module_reload(): %s", dlerror());
|
fatal("kore_module_reload(): %s", dlerror());
|
||||||
|
|
||||||
if (module->onload != NULL) {
|
if (module->onload != NULL) {
|
||||||
onload = dlsym(module->handle, module->onload);
|
module->ocb = dlsym(module->handle, module->onload);
|
||||||
if (onload == NULL) {
|
if (module->ocb == NULL) {
|
||||||
fatal("%s: onload '%s' not present",
|
fatal("%s: onload '%s' not present",
|
||||||
module->path, module->onload);
|
module->path, module->onload);
|
||||||
}
|
}
|
||||||
|
|
||||||
onload();
|
module->ocb(KORE_MODULE_LOAD);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kore_cb_name != NULL && kore_cb == NULL)
|
if (kore_cb_name != NULL && kore_cb == NULL)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user