commit d22405cea7c809fc70cd91305dd495ae39a3648f
parent ddf23ef65bf81c81862da38bdb7f90a4aa030b0a
Author: Joris Vink <joris@coders.se>
Date: Sat, 21 Dec 2013 13:37:34 +0100
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.
Diffstat:
4 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/includes/kore.h b/includes/kore.h
@@ -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;
diff --git a/modules/example/module.conf b/modules/example/module.conf
@@ -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
diff --git a/modules/example/src/example.c b/modules/example/src/example.c
@@ -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)
{
diff --git a/src/module.c b/src/module.c
@@ -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)