commit cf6a6550f071bf390cf856c490780fba8f15c71b
parent ec5ac40706da9c0e78e234597b1bdb31d8c22e62
Author: Joris Vink <joris@coders.se>
Date: Thu, 30 May 2013 21:26:39 +0200
allow onload to be given in the config file.
onload specifies what function in your module to call when the module has been loaded or reloaded.
Diffstat:
3 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/includes/kore.h b/includes/kore.h
@@ -135,6 +135,7 @@ extern int server_port;
extern char *server_ip;
extern char *chroot_path;
extern char *runas_user;
+extern char *kore_module_onload;
extern u_int8_t worker_count;
void *kore_malloc(size_t);
diff --git a/src/config.c b/src/config.c
@@ -40,6 +40,7 @@
static int configure_bind(char **);
static int configure_load(char **);
+static int configure_onload(char **);
static int configure_handler(char **);
static int configure_domain(char **);
static int configure_chroot(char **);
@@ -52,6 +53,7 @@ static struct {
} config_names[] = {
{ "bind", configure_bind },
{ "load", configure_load },
+ { "onload", configure_onload },
{ "static", configure_handler },
{ "dynamic", configure_handler },
{ "domain", configure_domain },
@@ -138,6 +140,21 @@ configure_load(char **argv)
}
static int
+configure_onload(char **argv)
+{
+ if (argv[1] == NULL)
+ return (KORE_RESULT_ERROR);
+
+ if (kore_module_onload != NULL) {
+ kore_log("duplicate onload directive found");
+ return (KORE_RESULT_ERROR);
+ }
+
+ kore_module_onload = kore_strdup(argv[1]);
+ return (KORE_RESULT_OK);
+}
+
+static int
configure_domain(char **argv)
{
if (argv[1] == NULL)
diff --git a/src/module.c b/src/module.c
@@ -43,6 +43,7 @@
static void *mod_handle = NULL;
static char *mod_name = NULL;
static time_t mod_last_mtime = 0;
+char *kore_module_onload = NULL;
struct module_domain {
char *domain;
@@ -57,6 +58,7 @@ void
kore_module_load(char *module_name)
{
struct stat st;
+ void (*onload)(void);
kore_log("kore_module_load(%s)", module_name);
@@ -73,6 +75,13 @@ kore_module_load(char *module_name)
TAILQ_INIT(&domains);
mod_name = kore_strdup(module_name);
+
+ if (kore_module_onload != NULL) {
+ onload = dlsym(mod_handle, kore_module_onload);
+ if (onload == NULL)
+ fatal("onload '%s' not present", kore_module_onload);
+ onload();
+ }
}
void
@@ -80,6 +89,7 @@ kore_module_reload(void)
{
struct module_domain *dom;
struct kore_module_handle *hdlr;
+ void (*onload)(void);
if (dlclose(mod_handle))
fatal("cannot close existing module: %s", dlerror());
@@ -96,6 +106,13 @@ kore_module_reload(void)
}
}
+ if (kore_module_onload != NULL) {
+ onload = dlsym(mod_handle, kore_module_onload);
+ if (onload == NULL)
+ fatal("onload '%s' not present", kore_module_onload);
+ onload();
+ }
+
kore_log("reloaded '%s' module", mod_name);
}