Add a callback that Kore can call in your module every given interval.

The callback is run from the parent process (which runs as root).

Adds kore_cb and kore_cb_interval configuration options.
This commit is contained in:
Joris Vink 2013-09-02 08:52:16 +02:00
parent 9fa9fd7402
commit 95c8b8e126
5 changed files with 79 additions and 0 deletions

View File

@ -229,6 +229,7 @@ extern char *runas_user;
extern char *kore_module_onload;
extern char *kore_pidfile;
extern char *config_file;
extern char *kore_cb_name;
extern char *kore_ssl_cipher_list;
extern DH *ssl_dhparam;
extern int ssl_no_compression;
@ -237,8 +238,10 @@ extern u_int8_t nlisteners;
extern u_int64_t spdy_idle_time;
extern u_int16_t cpu_count;
extern u_int8_t worker_count;
extern u_int64_t kore_cb_interval;
extern u_int32_t worker_max_connections;
extern u_int32_t worker_active_connections;
extern void (*kore_cb)(void);
extern struct listener_head listeners;
extern struct kore_worker *worker;

View File

@ -24,6 +24,12 @@ workers 4
# The onload function is called everytime the module is loaded or reloaded.
#onload myinit
# You can define a callback Kore calls from its parent process everytime
# the kore_cb_interval timer (in milliseconds) is reached.
# NOTE: Remember that the parent process runs as root and is not chroot().
#kore_cb my_callback
#kore_cb_interval 1000
# Specifies what module to be loaded.
load modules/example/example.module

View File

@ -15,6 +15,7 @@
*/
#include <ctype.h>
#include <limits.h>
#include <fcntl.h>
#include <pwd.h>
@ -37,6 +38,8 @@ static int configure_ssl_cipher(char **);
static int configure_ssl_dhparam(char **);
static int configure_ssl_no_compression(char **);
static int configure_spdy_idle_time(char **);
static int configure_kore_cb(char **);
static int configure_kore_cb_interval(char **);
static void domain_sslstart(void);
static struct {
@ -61,6 +64,8 @@ static struct {
{ "accesslog", configure_accesslog },
{ "certfile", configure_certfile },
{ "certkey", configure_certkey },
{ "kore_cb", configure_kore_cb },
{ "kore_cb_interval", configure_kore_cb_interval },
{ NULL, NULL },
};
@ -434,6 +439,43 @@ configure_max_connections(char **argv)
return (KORE_RESULT_OK);
}
static int
configure_kore_cb(char **argv)
{
if (argv[1] == NULL)
return (KORE_RESULT_ERROR);
if (kore_cb_name != NULL) {
kore_debug("kore_cb was already set");
return (KORE_RESULT_ERROR);
}
kore_cb_name = kore_strdup(argv[1]);
return (KORE_RESULT_OK);
}
static int
configure_kore_cb_interval(char **argv)
{
int err;
if (argv[1] == NULL)
return (KORE_RESULT_ERROR);
if (kore_cb_interval != 0) {
kore_debug("kore_cb_interval already given");
return (KORE_RESULT_ERROR);
}
kore_cb_interval = kore_strtonum(argv[1], 10, 1, LLONG_MAX, &err);
if (err != KORE_RESULT_OK) {
kore_debug("invalid value for kore_cb_interval");
return (KORE_RESULT_ERROR);
}
return (KORE_RESULT_OK);
}
static void
domain_sslstart(void)
{

View File

@ -29,9 +29,11 @@ struct passwd *pw = NULL;
pid_t kore_pid = -1;
u_int16_t cpu_count = 1;
int kore_debug = 0;
void (*kore_cb)(void);
u_int8_t worker_count = 0;
char *runas_user = NULL;
char *chroot_path = NULL;
u_int64_t kore_cb_interval = 0;
char *kore_pidfile = KORE_PIDFILE_DEFAULT;
char *kore_ssl_cipher_list = KORE_DEFAULT_CIPHER_LIST;
@ -220,6 +222,8 @@ kore_server_sslstart(void)
static void
kore_server_start(void)
{
u_int64_t now, last_cb_run;
kore_mem_free(runas_user);
if (daemon(1, 1) == -1)
@ -232,6 +236,9 @@ kore_server_start(void)
kore_platform_proctitle("kore [parent]");
kore_worker_init();
now = kore_time_ms();
last_cb_run = now;
for (;;) {
if (sig_recv != 0) {
if (sig_recv == SIGHUP || sig_recv == SIGQUIT) {
@ -246,6 +253,13 @@ kore_server_start(void)
if (!kore_accesslog_wait())
break;
now = kore_time_ms();
if ((now - last_cb_run) >= kore_cb_interval) {
last_cb_run = now;
kore_cb();
}
kore_worker_wait(0);
}
}

View File

@ -23,6 +23,8 @@
static void *mod_handle = NULL;
static char *mod_name = NULL;
static time_t mod_last_mtime = 0;
char *kore_cb_name = NULL;
char *kore_module_onload = NULL;
void
@ -53,6 +55,12 @@ kore_module_load(char *module_name)
fatal("onload '%s' not present", kore_module_onload);
onload();
}
if (kore_cb_name != NULL) {
kore_cb = dlsym(mod_handle, kore_cb_name);
if (kore_cb == NULL)
fatal("kore_cb '%s' not present", kore_cb_name);
}
}
void
@ -85,6 +93,12 @@ kore_module_reload(void)
onload();
}
if (kore_cb_name != NULL) {
kore_cb = dlsym(mod_handle, kore_cb_name);
if (kore_cb == NULL)
fatal("kore_cb '%s' not present", kore_cb_name);
}
kore_log(LOG_NOTICE, "reloaded '%s' module", mod_name);
}