2013-05-01 16:03:48 +02:00
|
|
|
/*
|
2021-01-11 23:46:08 +01:00
|
|
|
* Copyright (c) 2013-2021 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.
|
|
|
|
*/
|
|
|
|
|
2019-03-06 09:29:46 +01:00
|
|
|
#include <sys/types.h>
|
2013-05-01 16:03:48 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
#include "http.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
#include "python_api.h"
|
|
|
|
#endif
|
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
static TAILQ_HEAD(, kore_module) modules;
|
2013-09-02 08:52:16 +02:00
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
static void native_free(struct kore_module *);
|
2018-02-14 13:48:49 +01:00
|
|
|
static void native_load(struct kore_module *);
|
2017-01-12 23:38:51 +01:00
|
|
|
static void native_reload(struct kore_module *);
|
|
|
|
static void *native_getsym(struct kore_module *, const char *);
|
|
|
|
|
|
|
|
struct kore_module_functions kore_native_module = {
|
|
|
|
.free = native_free,
|
|
|
|
.load = native_load,
|
|
|
|
.getsym = native_getsym,
|
|
|
|
.reload = native_reload,
|
|
|
|
};
|
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
void
|
|
|
|
kore_module_init(void)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&modules);
|
|
|
|
}
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2016-06-08 13:55:14 +02:00
|
|
|
void
|
|
|
|
kore_module_cleanup(void)
|
|
|
|
{
|
|
|
|
struct kore_module *module, *next;
|
|
|
|
|
2017-02-07 22:44:20 +01:00
|
|
|
for (module = TAILQ_FIRST(&modules); module != NULL; module = next) {
|
|
|
|
next = TAILQ_NEXT(module, list);
|
2016-06-08 13:55:14 +02:00
|
|
|
TAILQ_REMOVE(&modules, module, list);
|
2017-01-12 23:38:51 +01:00
|
|
|
module->fun->free(module);
|
2016-06-08 13:55:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:16:36 +02:00
|
|
|
struct kore_module *
|
2017-01-12 23:38:51 +01:00
|
|
|
kore_module_load(const char *path, const char *onload, int type)
|
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
|
|
|
|
2016-07-06 16:16:15 +02:00
|
|
|
module = kore_malloc(sizeof(struct kore_module));
|
|
|
|
module->ocb = NULL;
|
2017-01-12 23:38:51 +01:00
|
|
|
module->type = type;
|
|
|
|
module->onload = NULL;
|
|
|
|
module->handle = NULL;
|
2016-07-06 16:16:15 +02:00
|
|
|
|
2017-01-26 13:13:23 +01:00
|
|
|
if (path != NULL) {
|
|
|
|
if (stat(path, &st) == -1)
|
|
|
|
fatal("stat(%s): %s", path, errno_s);
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2017-01-26 13:13:23 +01:00
|
|
|
module->path = kore_strdup(path);
|
|
|
|
} else {
|
|
|
|
module->path = NULL;
|
|
|
|
}
|
2013-05-01 16:03:48 +02:00
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
switch (module->type) {
|
|
|
|
case KORE_MODULE_NATIVE:
|
|
|
|
module->fun = &kore_native_module;
|
|
|
|
module->runtime = &kore_native_runtime;
|
|
|
|
break;
|
|
|
|
#if defined(KORE_USE_PYTHON)
|
|
|
|
case KORE_MODULE_PYTHON:
|
|
|
|
module->fun = &kore_python_module;
|
|
|
|
module->runtime = &kore_python_runtime;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
fatal("kore_module_load: unknown type %d", type);
|
2013-05-30 21:26:39 +02:00
|
|
|
}
|
2013-09-02 08:52:16 +02:00
|
|
|
|
2018-02-14 13:48:49 +01:00
|
|
|
module->fun->load(module);
|
2013-12-15 01:11:56 +01:00
|
|
|
TAILQ_INSERT_TAIL(&modules, module, list);
|
2017-01-12 23:38:51 +01:00
|
|
|
|
|
|
|
if (onload != NULL) {
|
2017-08-30 15:11:07 +02:00
|
|
|
module->onload = kore_strdup(onload);
|
2017-01-12 23:38:51 +01:00
|
|
|
module->ocb = kore_malloc(sizeof(*module->ocb));
|
|
|
|
module->ocb->runtime = module->runtime;
|
|
|
|
module->ocb->addr = module->fun->getsym(module, onload);
|
|
|
|
|
|
|
|
if (module->ocb->addr == NULL) {
|
|
|
|
fatal("%s: onload '%s' not present",
|
|
|
|
module->path, onload);
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 13:16:36 +02:00
|
|
|
|
|
|
|
return (module);
|
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) {
|
2017-01-26 13:13:23 +01:00
|
|
|
if (module->path == NULL || module->ocb == NULL)
|
2014-06-29 21:15:23 +02:00
|
|
|
continue;
|
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
kore_runtime_onload(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;
|
2017-01-12 23:38:51 +01:00
|
|
|
int ret;
|
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) {
|
2017-01-26 13:13:23 +01:00
|
|
|
if (module->path == NULL)
|
|
|
|
continue;
|
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
if (stat(module->path, &st) == -1) {
|
|
|
|
kore_log(LOG_NOTICE, "stat(%s): %s, skipping reload",
|
|
|
|
module->path, errno_s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-07 13:03:10 +02:00
|
|
|
if (module->ocb != NULL && cbs == 1) {
|
2017-01-12 23:38:51 +01:00
|
|
|
ret = kore_runtime_onload(module->ocb,
|
|
|
|
KORE_MODULE_UNLOAD);
|
|
|
|
if (ret == KORE_RESULT_ERROR) {
|
2015-05-07 13:03:10 +02:00
|
|
|
kore_log(LOG_NOTICE,
|
2017-02-22 21:48:06 +01:00
|
|
|
"%s forced no reloaded", module->path);
|
2015-05-07 13:03:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 13:37:34 +01:00
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
module->fun->reload(module);
|
2013-12-15 01:11:56 +01:00
|
|
|
|
|
|
|
if (module->onload != NULL) {
|
2017-01-12 23:38:51 +01:00
|
|
|
kore_free(module->ocb);
|
|
|
|
module->ocb = kore_malloc(sizeof(*module->ocb));
|
|
|
|
module->ocb->runtime = module->runtime;
|
|
|
|
module->ocb->addr =
|
|
|
|
module->fun->getsym(module, module->onload);
|
|
|
|
if (module->ocb->addr == NULL) {
|
2013-12-15 01:11:56 +01:00
|
|
|
fatal("%s: onload '%s' not present",
|
|
|
|
module->path, module->onload);
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 00:04:06 +02:00
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
if (module->ocb != NULL && cbs == 1)
|
|
|
|
kore_runtime_onload(module->ocb, KORE_MODULE_LOAD);
|
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
kore_log(LOG_NOTICE, "reloaded '%s' module", module->path);
|
|
|
|
}
|
|
|
|
|
2018-07-17 14:28:43 +02:00
|
|
|
#if !defined(KORE_NO_HTTP)
|
2021-09-15 11:09:52 +02:00
|
|
|
kore_route_reload();
|
2017-08-31 17:11:24 +02:00
|
|
|
kore_validator_reload();
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
2013-11-09 16:21:52 +01:00
|
|
|
void *
|
2017-01-12 23:38:51 +01:00
|
|
|
kore_module_getsym(const char *symbol, struct kore_runtime **runtime)
|
2013-11-09 16:21:52 +01:00
|
|
|
{
|
2013-12-15 01:11:56 +01:00
|
|
|
void *ptr;
|
|
|
|
struct kore_module *module;
|
|
|
|
|
2017-01-12 23:38:51 +01:00
|
|
|
if (runtime != NULL)
|
|
|
|
*runtime = NULL;
|
|
|
|
|
2013-12-15 01:11:56 +01:00
|
|
|
TAILQ_FOREACH(module, &modules, list) {
|
2017-01-12 23:38:51 +01:00
|
|
|
ptr = module->fun->getsym(module, symbol);
|
|
|
|
if (ptr != NULL) {
|
|
|
|
if (runtime != NULL)
|
|
|
|
*runtime = module->runtime;
|
2013-12-15 01:11:56 +01:00
|
|
|
return (ptr);
|
2017-01-12 23:38:51 +01:00
|
|
|
}
|
2013-12-15 01:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
2013-11-09 16:21:52 +01:00
|
|
|
}
|
2017-01-12 23:38:51 +01:00
|
|
|
|
|
|
|
static void *
|
|
|
|
native_getsym(struct kore_module *module, const char *symbol)
|
|
|
|
{
|
|
|
|
return (dlsym(module->handle, symbol));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
native_free(struct kore_module *module)
|
|
|
|
{
|
|
|
|
kore_free(module->path);
|
|
|
|
(void)dlclose(module->handle);
|
|
|
|
kore_free(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
native_reload(struct kore_module *module)
|
|
|
|
{
|
|
|
|
if (dlclose(module->handle))
|
|
|
|
fatal("cannot close existing module: %s", dlerror());
|
2018-02-14 13:48:49 +01:00
|
|
|
module->fun->load(module);
|
2017-01-12 23:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-02-14 13:48:49 +01:00
|
|
|
native_load(struct kore_module *module)
|
2017-01-12 23:38:51 +01:00
|
|
|
{
|
|
|
|
module->handle = dlopen(module->path, RTLD_NOW | RTLD_GLOBAL);
|
|
|
|
if (module->handle == NULL)
|
|
|
|
fatal("%s: %s", module->path, dlerror());
|
|
|
|
}
|