kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 9c337ded1e1716aba1d7bfc01268c790181e25f3
parent cb4567683a2ca19c8f619654c67a43768da1114e
Author: Joris Vink <joris@coders.se>
Date:   Mon,  9 Apr 2018 12:51:20 +0200

Change kore_parent_configure() for single binaries.

This function now takes any remaining arguments passed on the command line
after kore parsed its own.

For C the new prototype looks like this:

void kore_parent_configure(int argc, char **argv);

For python code, kore will pass each argument to the function so you
can do things like:

def kore_parent_configure(arg1, arg2):

Diffstat:
include/kore/kore.h | 5++++-
src/config.c | 32+++++++++++++++-----------------
src/kore.c | 8++++++--
src/python.c | 35++++++++++++++++++++++++++++++++++-
src/runtime.c | 19++++++++++++++++++-
5 files changed, 77 insertions(+), 22 deletions(-)

diff --git a/include/kore/kore.h b/include/kore/kore.h @@ -228,6 +228,7 @@ struct kore_runtime { void (*execute)(void *); int (*onload)(void *, int); void (*connect)(void *, struct connection *); + void (*configure)(void *, int, char **); }; struct kore_runtime_call { @@ -557,6 +558,7 @@ void kore_log_init(void); void *kore_malloc(size_t); void kore_parse_config(void); +void kore_parse_config_file(FILE *); void *kore_calloc(size_t, size_t); void *kore_realloc(void *, size_t); void kore_free(void *); @@ -633,6 +635,7 @@ struct kore_runtime_call *kore_runtime_getcall(const char *); void kore_runtime_execute(struct kore_runtime_call *); int kore_runtime_onload(struct kore_runtime_call *, int); +void kore_runtime_configure(struct kore_runtime_call *, int, char **); void kore_runtime_connect(struct kore_runtime_call *, struct connection *); #if !defined(KORE_NO_HTTP) int kore_runtime_http_request(struct kore_runtime_call *, @@ -706,8 +709,8 @@ void kore_buf_replace_string(struct kore_buf *, char *, void *, size_t); void kore_keymgr_run(void); void kore_keymgr_cleanup(void); -void kore_parent_configure(void); void kore_worker_configure(void); +void kore_parent_configure(int, char **); #if defined(__cplusplus) } diff --git a/src/config.c b/src/config.c @@ -113,7 +113,6 @@ static int configure_python_import(char *); #endif static void domain_tls_init(void); -static void kore_parse_config_file(const char *); static struct { const char *name; @@ -195,12 +194,17 @@ static struct kore_domain *current_domain = NULL; void kore_parse_config(void) { + FILE *fp; + #if !defined(KORE_SINGLE_BINARY) - kore_parse_config_file(config_file); + if ((fp = fopen(config_file, "r")) == NULL) + fatal("configuration given cannot be opened: %s", config_file); #else - kore_parse_config_file(NULL); + fp = config_file_write(); #endif + kore_parse_config_file(fp); + if (!kore_module_loaded()) fatal("no application module was loaded"); @@ -221,23 +225,12 @@ kore_parse_config(void) } } -static void -kore_parse_config_file(const char *fpath) +void +kore_parse_config_file(FILE *fp) { - FILE *fp; int i, lineno; char buf[BUFSIZ], *p, *t; -#if !defined(KORE_SINGLE_BINARY) - if ((fp = fopen(fpath, "r")) == NULL) - fatal("configuration given cannot be opened: %s", fpath); -#else - (void)fpath; - fp = config_file_write(); -#endif - - kore_debug("parsing configuration file '%s'", fpath); - lineno = 1; while ((p = kore_read_line(fp, buf, sizeof(buf))) != NULL) { if (strlen(p) == 0) { @@ -309,7 +302,12 @@ kore_parse_config_file(const char *fpath) static int configure_include(char *path) { - kore_parse_config_file(path); + FILE *fp; + + if ((fp = fopen(path, "r")) == NULL) + fatal("failed to open include '%s'", path); + + kore_parse_config_file(fp); return (KORE_RESULT_OK); } diff --git a/src/kore.c b/src/kore.c @@ -169,8 +169,10 @@ main(int argc, char *argv[]) kore_mem_init(); +#if !defined(KORE_SINGLE_BINARY) if (argc > 0) fatal("did you mean to run `kodevĀ“ instead?"); +#endif kore_pid = getpid(); nlisteners = 0; @@ -191,16 +193,18 @@ main(int argc, char *argv[]) #if !defined(KORE_SINGLE_BINARY) if (config_file == NULL) usage(); + kore_parse_config(); #else kore_module_load(NULL, NULL, KORE_MODULE_NATIVE); + kore_parse_config(); + rcall = kore_runtime_getcall("kore_parent_configure"); if (rcall != NULL) { - kore_runtime_execute(rcall); + kore_runtime_configure(rcall, argc, argv); kore_free(rcall); } #endif - kore_parse_config(); kore_platform_init(); #if !defined(KORE_NO_HTTP) diff --git a/src/python.c b/src/python.c @@ -55,6 +55,7 @@ static void python_runtime_wsmessage(void *, struct connection *, u_int8_t, const void *, size_t); static void python_runtime_execute(void *); static int python_runtime_onload(void *, int); +static void python_runtime_configure(void *, int, char **); static void python_runtime_connect(void *, struct connection *); static void python_module_load(struct kore_module *); @@ -83,7 +84,8 @@ struct kore_runtime kore_python_runtime = { .wsdisconnect = python_runtime_connect, .onload = python_runtime_onload, .connect = python_runtime_connect, - .execute = python_runtime_execute + .execute = python_runtime_execute, + .configure = python_runtime_configure }; static struct { @@ -440,6 +442,37 @@ python_runtime_execute(void *addr) Py_DECREF(pyret); } +static void +python_runtime_configure(void *addr, int argc, char **argv) +{ + int i; + PyObject *callable, *args, *pyret, *pyarg; + + callable = (PyObject *)addr; + + if ((args = PyTuple_New(argc)) == NULL) + fatal("python_runtime_configure: PyTuple_New failed"); + + for (i = 0; i < argc; i++) { + if ((pyarg = PyUnicode_FromString(argv[i])) == NULL) + fatal("python_runtime_configure: PyUnicode_FromString"); + + if (PyTuple_SetItem(args, i, pyarg) != 0) + fatal("python_runtime_configure: PyTuple_SetItem"); + } + + PyErr_Clear(); + pyret = PyObject_Call(callable, args, NULL); + Py_DECREF(args); + + if (pyret == NULL) { + python_log_error("python_runtime_configure"); + fatal("failed to call configure method: wrong args?"); + } + + Py_DECREF(pyret); +} + static int python_runtime_onload(void *addr, int action) { diff --git a/src/runtime.c b/src/runtime.c @@ -29,6 +29,7 @@ static void native_runtime_execute(void *); static int native_runtime_onload(void *, int); static void native_runtime_connect(void *, struct connection *); +static void native_runtime_configure(void *, int, char **); #if !defined(KORE_NO_HTTP) static int native_runtime_http_request(void *, struct http_request *); static int native_runtime_validator(void *, struct http_request *, @@ -49,7 +50,8 @@ struct kore_runtime kore_native_runtime = { #endif .onload = native_runtime_onload, .connect = native_runtime_connect, - .execute = native_runtime_execute + .execute = native_runtime_execute, + .configure = native_runtime_configure }; struct kore_runtime_call * @@ -76,6 +78,12 @@ kore_runtime_execute(struct kore_runtime_call *rcall) rcall->runtime->execute(rcall->addr); } +void +kore_runtime_configure(struct kore_runtime_call *rcall, int argc, char **argv) +{ + rcall->runtime->configure(rcall->addr, argc, argv); +} + int kore_runtime_onload(struct kore_runtime_call *rcall, int action) { @@ -133,6 +141,15 @@ native_runtime_execute(void *addr) } static void +native_runtime_configure(void *addr, int argc, char **argv) +{ + void (*cb)(int, char **); + + *(void **)&(cb) = addr; + cb(argc, argv); +} + +static void native_runtime_connect(void *addr, struct connection *c) { void (*cb)(struct connection *);