2013-05-01 16:03:48 +02:00
|
|
|
/*
|
2016-01-04 12:58:51 +01:00
|
|
|
* Copyright (c) 2013-2016 Joris Vink <joris@coders.se>
|
2013-05-01 16:03:48 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
static TAILQ_HEAD(, kore_module) modules;
|
2013-09-02 08:52:16 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
void
|
|
|
|
kore_module_init(void)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&modules);
|
|
|
|
}
|
2013-05-01 16:03:48 +02:00
|
|
|
|
|
|
|
void
|
2014-08-04 12:40:21 +02:00
|
|
|
kore_module_load(const char *path, const char *onload)
|
2013-05-01 16:03:48 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
2013-12-15 01:11:56 +01:00
|
|
|
struct kore_module *module;
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
kore_debug("kore_module_load(%s, %s)", path, onload);
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
if (stat(path, &st) == -1)
|
|
|
|
fatal("stat(%s): %s", path, errno_s);
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
module = kore_malloc(sizeof(struct kore_module));
|
|
|
|
module->path = kore_strdup(path);
|
|
|
|
module->mtime = st.st_mtime;
|
|
|
|
module->onload = NULL;
|
2014-07-22 10:47:12 +02:00
|
|
|
module->ocb = NULL;
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
module->handle = dlopen(module->path, RTLD_NOW | RTLD_GLOBAL);
|
|
|
|
if (module->handle == NULL)
|
|
|
|
fatal("%s: %s", path, dlerror());
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
if (onload != NULL) {
|
|
|
|
module->onload = kore_strdup(onload);
|
2016-02-01 15:33:40 +01:00
|
|
|
*(void **)&(module->ocb) = dlsym(module->handle, onload);
|
2013-12-21 13:37:34 +01:00
|
|
|
if (module->ocb == NULL)
|
2013-12-15 01:11:56 +01:00
|
|
|
fatal("%s: onload '%s' not present", path, onload);
|
2013-05-30 21:26:39 +02:00
|
|
|
}
|
2013-09-02 08:52:16 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
TAILQ_INSERT_TAIL(&modules, module, list);
|
2013-05-03 00:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-29 21:15:23 +02:00
|
|
|
kore_module_onload(void)
|
|
|
|
{
|
|
|
|
struct kore_module *module;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(module, &modules, list) {
|
|
|
|
if (module->ocb == NULL)
|
|
|
|
continue;
|
|
|
|
|
2015-05-07 13:03:10 +02:00
|
|
|
(void)module->ocb(KORE_MODULE_LOAD);
|
2014-06-29 21:15:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_module_reload(int cbs)
|
2013-05-03 00:04:06 +02:00
|
|
|
{
|
2013-12-15 01:11:56 +01:00
|
|
|
struct stat st;
|
2013-06-24 11:32:45 +02:00
|
|
|
struct kore_domain *dom;
|
2013-05-03 00:04:06 +02:00
|
|
|
struct kore_module_handle *hdlr;
|
2013-12-15 01:11:56 +01:00
|
|
|
struct kore_module *module;
|
2013-05-03 00:04:06 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
TAILQ_FOREACH(module, &modules, list) {
|
|
|
|
if (stat(module->path, &st) == -1) {
|
|
|
|
kore_log(LOG_NOTICE, "stat(%s): %s, skipping reload",
|
|
|
|
module->path, errno_s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module->mtime == st.st_mtime)
|
|
|
|
continue;
|
|
|
|
|
2015-05-07 13:03:10 +02:00
|
|
|
if (module->ocb != NULL && cbs == 1) {
|
|
|
|
if (!module->ocb(KORE_MODULE_UNLOAD)) {
|
|
|
|
kore_log(LOG_NOTICE,
|
|
|
|
"not reloading %s", module->path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 13:37:34 +01:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
module->mtime = st.st_mtime;
|
|
|
|
if (dlclose(module->handle))
|
|
|
|
fatal("cannot close existing module: %s", dlerror());
|
|
|
|
|
|
|
|
module->handle = dlopen(module->path, RTLD_NOW | RTLD_GLOBAL);
|
|
|
|
if (module->handle == NULL)
|
|
|
|
fatal("kore_module_reload(): %s", dlerror());
|
|
|
|
|
|
|
|
if (module->onload != NULL) {
|
2016-02-01 15:33:40 +01:00
|
|
|
*(void **)&(module->ocb) =
|
|
|
|
dlsym(module->handle, module->onload);
|
2013-12-21 13:37:34 +01:00
|
|
|
if (module->ocb == NULL) {
|
2013-12-15 01:11:56 +01:00
|
|
|
fatal("%s: onload '%s' not present",
|
|
|
|
module->path, module->onload);
|
|
|
|
}
|
|
|
|
|
2014-06-29 21:15:23 +02:00
|
|
|
if (cbs)
|
2015-05-07 13:03:10 +02:00
|
|
|
(void)module->ocb(KORE_MODULE_LOAD);
|
2013-12-15 01:11:56 +01:00
|
|
|
}
|
2013-05-03 00:04:06 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
kore_log(LOG_NOTICE, "reloaded '%s' module", module->path);
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:29:46 +02:00
|
|
|
TAILQ_FOREACH(dom, &domains, list) {
|
|
|
|
TAILQ_FOREACH(hdlr, &(dom->handlers), list) {
|
2013-12-15 01:11:56 +01:00
|
|
|
hdlr->addr = kore_module_getsym(hdlr->func);
|
2013-05-29 14:29:46 +02:00
|
|
|
if (hdlr->func == NULL)
|
|
|
|
fatal("no function '%s' found", hdlr->func);
|
2013-12-15 01:11:56 +01:00
|
|
|
hdlr->errors = 0;
|
2013-05-29 14:29:46 +02:00
|
|
|
}
|
2013-05-03 00:04:06 +02:00
|
|
|
}
|
|
|
|
|
2015-11-27 16:22:50 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2013-11-09 16:21:52 +01:00
|
|
|
kore_validator_reload();
|
2015-11-27 16:22:50 +01:00
|
|
|
#endif
|
2013-05-01 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kore_module_loaded(void)
|
|
|
|
{
|
2013-12-15 01:11:56 +01:00
|
|
|
if (TAILQ_EMPTY(&modules))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (1);
|
2013-05-01 16:03:48 +02:00
|
|
|
}
|
|
|
|
|
2015-11-27 16:22:50 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2013-05-01 16:03:48 +02:00
|
|
|
int
|
2014-08-04 12:40:21 +02:00
|
|
|
kore_module_handler_new(const char *path, const char *domain,
|
|
|
|
const char *func, const char *auth, int type)
|
2013-05-01 16:03:48 +02:00
|
|
|
{
|
2014-01-22 22:55:10 +01:00
|
|
|
struct kore_auth *ap;
|
2013-05-01 16:03:48 +02:00
|
|
|
void *addr;
|
2013-06-24 11:32:45 +02:00
|
|
|
struct kore_domain *dom;
|
2013-05-01 16:03:48 +02:00
|
|
|
struct kore_module_handle *hdlr;
|
|
|
|
|
2014-01-22 22:55:10 +01:00
|
|
|
kore_debug("kore_module_handler_new(%s, %s, %s, %s, %d)", path,
|
|
|
|
domain, func, auth, type);
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
addr = kore_module_getsym(func);
|
2013-05-01 16:03:48 +02:00
|
|
|
if (addr == NULL) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("function '%s' not found", func);
|
2013-05-01 16:03:48 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if ((dom = kore_domain_lookup(domain)) == NULL)
|
2013-05-29 14:29:46 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
2013-05-02 13:30:13 +02:00
|
|
|
|
2014-01-22 22:55:10 +01:00
|
|
|
if (auth != NULL) {
|
|
|
|
if ((ap = kore_auth_lookup(auth)) == NULL)
|
|
|
|
fatal("no authentication block '%s' found", auth);
|
|
|
|
} else {
|
|
|
|
ap = NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-13 21:08:55 +02:00
|
|
|
hdlr = kore_malloc(sizeof(*hdlr));
|
2014-01-22 22:55:10 +01:00
|
|
|
hdlr->auth = ap;
|
2014-07-30 09:11:21 +02:00
|
|
|
hdlr->dom = dom;
|
2013-07-07 14:48:32 +02:00
|
|
|
hdlr->errors = 0;
|
2013-05-03 00:04:06 +02:00
|
|
|
hdlr->addr = addr;
|
2013-05-01 16:03:48 +02:00
|
|
|
hdlr->type = type;
|
2013-11-10 15:17:15 +01:00
|
|
|
TAILQ_INIT(&(hdlr->params));
|
2013-05-29 14:29:46 +02:00
|
|
|
hdlr->path = kore_strdup(path);
|
2013-05-03 00:04:06 +02:00
|
|
|
hdlr->func = kore_strdup(func);
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2013-05-29 14:29:46 +02:00
|
|
|
if (hdlr->type == HANDLER_TYPE_DYNAMIC) {
|
2013-12-11 19:13:50 +01:00
|
|
|
if (regcomp(&(hdlr->rctx), hdlr->path,
|
|
|
|
REG_EXTENDED | REG_NOSUB)) {
|
2016-02-01 12:30:20 +01:00
|
|
|
kore_module_handler_free(hdlr);
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("regcomp() on %s failed", path);
|
2013-05-29 14:29:46 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&(dom->handlers), hdlr, list);
|
2013-05-01 16:03:48 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2016-02-01 12:30:20 +01:00
|
|
|
void
|
|
|
|
kore_module_handler_free(struct kore_module_handle *hdlr)
|
|
|
|
{
|
|
|
|
struct kore_handler_params *param;
|
|
|
|
|
|
|
|
if (hdlr != NULL) {
|
|
|
|
if (hdlr->func != NULL) {
|
|
|
|
kore_mem_free(hdlr->func);
|
|
|
|
}
|
|
|
|
if (hdlr->path != NULL) {
|
|
|
|
kore_mem_free(hdlr->path);
|
|
|
|
}
|
|
|
|
if (hdlr->dom != NULL) {
|
|
|
|
TAILQ_REMOVE(&(hdlr->dom->handlers), hdlr, list);
|
|
|
|
}
|
|
|
|
regfree(&(hdlr->rctx));
|
|
|
|
|
|
|
|
/* Drop all validators associated with this handler */
|
|
|
|
while ((param=TAILQ_FIRST(&(hdlr->params))) != NULL) {
|
|
|
|
TAILQ_REMOVE(&(hdlr->params), param, list);
|
|
|
|
if (param->name != NULL) {
|
|
|
|
kore_mem_free(param->name);
|
|
|
|
}
|
|
|
|
kore_mem_free(param);
|
|
|
|
}
|
|
|
|
kore_mem_free(hdlr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-07 14:48:32 +02:00
|
|
|
struct kore_module_handle *
|
2014-08-04 12:40:21 +02:00
|
|
|
kore_module_handler_find(const char *domain, const char *path)
|
2013-05-01 16:03:48 +02:00
|
|
|
{
|
2013-06-24 11:32:45 +02:00
|
|
|
struct kore_domain *dom;
|
2013-05-01 16:11:10 +02:00
|
|
|
struct kore_module_handle *hdlr;
|
2013-05-02 13:30:13 +02:00
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if ((dom = kore_domain_lookup(domain)) == NULL)
|
2013-05-29 14:29:46 +02:00
|
|
|
return (NULL);
|
2013-05-01 16:11:10 +02:00
|
|
|
|
2013-05-29 14:29:46 +02:00
|
|
|
TAILQ_FOREACH(hdlr, &(dom->handlers), list) {
|
2013-05-29 13:33:32 +02:00
|
|
|
if (hdlr->type == HANDLER_TYPE_STATIC) {
|
2013-05-29 14:29:46 +02:00
|
|
|
if (!strcmp(hdlr->path, path))
|
2013-07-07 14:48:32 +02:00
|
|
|
return (hdlr);
|
2013-05-29 13:33:32 +02:00
|
|
|
} else {
|
2013-05-29 14:29:46 +02:00
|
|
|
if (!regexec(&(hdlr->rctx), path, 0, NULL, 0))
|
2013-07-07 14:48:32 +02:00
|
|
|
return (hdlr);
|
2013-05-29 13:33:32 +02:00
|
|
|
}
|
2013-05-01 16:11:10 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 16:03:48 +02:00
|
|
|
return (NULL);
|
|
|
|
}
|
2013-11-09 16:21:52 +01:00
|
|
|
|
2015-11-27 16:22:50 +01:00
|
|
|
#endif /* !KORE_NO_HTTP */
|
|
|
|
|
2013-11-09 16:21:52 +01:00
|
|
|
void *
|
2014-08-04 12:40:21 +02:00
|
|
|
kore_module_getsym(const char *symbol)
|
2013-11-09 16:21:52 +01:00
|
|
|
{
|
2013-12-15 01:11:56 +01:00
|
|
|
void *ptr;
|
|
|
|
struct kore_module *module;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(module, &modules, list) {
|
|
|
|
ptr = dlsym(module->handle, symbol);
|
|
|
|
if (ptr != NULL)
|
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
2013-11-09 16:21:52 +01:00
|
|
|
}
|