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 7f56c7dbf2688e665bfbef32ed30bce46564dd9e
parent 8661aee2f4738c62ed202642adeb5db35a2d4c73
Author: Joris Vink <joris@coders.se>
Date:   Mon,  6 Sep 2021 13:28:38 +0200

Change how worker processes do logging.

Before each worker process would either directly print to stdout if
Kore was running in foreground mode, or syslog otherwise.

With this commit the workers will submit their log messages to the
parent process who will either put it onto stdout or syslog.

This change in completely under the hood and users shouldn't care about it.

Diffstat:
Makefile | 6+++---
include/kore/kore.h | 1+
src/kore.c | 3++-
src/log.c | 113+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/utils.c | 40----------------------------------------
src/worker.c | 3+++
6 files changed, 122 insertions(+), 44 deletions(-)

diff --git a/Makefile b/Makefile @@ -21,9 +21,9 @@ VERSION=$(OBJDIR)/version.c PYTHON_CURLOPT=misc/curl/python_curlopt.h S_SRC= src/kore.c src/buf.c src/config.c src/connection.c \ - src/domain.c src/filemap.c src/fileref.c src/json.c src/mem.c \ - src/msg.c src/module.c src/net.c src/pool.c src/runtime.c src/timer.c \ - src/utils.c src/worker.c src/keymgr.c + src/domain.c src/filemap.c src/fileref.c src/json.c src/log.c \ + src/mem.c src/msg.c src/module.c src/net.c src/pool.c src/runtime.c \ + src/timer.c src/utils.c src/worker.c src/keymgr.c FEATURES= FEATURES_INC= diff --git a/include/kore/kore.h b/include/kore/kore.h @@ -659,6 +659,7 @@ struct kore_timer { #define KORE_MSG_CRL 9 #define KORE_MSG_ACCEPT_AVAILABLE 10 #define KORE_PYTHON_SEND_OBJ 11 +#define KORE_MSG_WORKER_LOG 12 #define KORE_MSG_ACME_BASE 100 /* messages for applications should start at 201. */ diff --git a/src/kore.c b/src/kore.c @@ -205,8 +205,8 @@ main(int argc, char *argv[]) LIST_INIT(&kore_servers); kore_platform_init(); - kore_log_init(); kore_msg_init(); + kore_log_init(); #if !defined(KORE_NO_HTTP) http_parent_init(); #if defined(KORE_USE_CURL) @@ -866,6 +866,7 @@ kore_server_start(int argc, char *argv[]) struct kore_runtime_call *rcall; #endif + printf("kore_foreground = %d\n", kore_foreground); if (kore_foreground == 0) { if (daemon(1, 0) == -1) fatal("cannot daemon(): %s", errno_s); diff --git a/src/log.c b/src/log.c @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2021 Joris Vink <joris@coders.se> + * + * 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/types.h> + +#include <syslog.h> + +#include "kore.h" + +struct kore_wlog { + int prio; + u_int16_t wid; + size_t loglen; + char logmsg[]; +}; + +static void log_from_worker(struct kore_msg *, const void *); + +void +kore_log_init(void) +{ +#if defined(KORE_SINGLE_BINARY) + extern const char *__progname; + const char *name = kore_strdup(__progname); +#else + const char *name = "kore"; +#endif + + if (!kore_foreground) + openlog(name, LOG_NDELAY | LOG_PID, LOG_DAEMON); + + kore_msg_register(KORE_MSG_WORKER_LOG, log_from_worker); +} + +void +kore_log(int prio, const char *fmt, ...) +{ + va_list args; + const char *str; + struct kore_wlog wlog; + struct kore_buf buf, pkt; + + kore_buf_init(&buf, 128); + + va_start(args, fmt); + kore_buf_appendv(&buf, fmt, args); + va_end(args); + + if (worker != NULL) { + kore_buf_init(&pkt, sizeof(wlog) + buf.offset); + + memset(&wlog, 0, sizeof(wlog)); + + wlog.prio = prio; + wlog.wid = worker->id; + wlog.loglen = buf.offset; + + kore_buf_append(&pkt, &wlog, sizeof(wlog)); + kore_buf_append(&pkt, buf.data, buf.offset); + + kore_msg_send(KORE_MSG_PARENT, KORE_MSG_WORKER_LOG, + pkt.data, pkt.offset); + + kore_buf_cleanup(&pkt); + } else { + str = kore_buf_stringify(&buf, NULL); + + if (kore_foreground) + printf("[parent]: %s\n", str); + else + syslog(prio, "[parent]: %s", str); + } + + kore_buf_cleanup(&buf); +} + +static void +log_from_worker(struct kore_msg *msg, const void *data) +{ + const char *name; + const struct kore_wlog *wlog; + + if (msg->length < sizeof(*wlog)) { + kore_log(LOG_NOTICE, + "too short worker log received (%zu < %zu)", + msg->length, sizeof(*wlog)); + return; + } + + wlog = data; + name = kore_worker_name(wlog->wid); + + if (kore_foreground) { + printf("%s: %.*s\n", + name, (int)wlog->loglen, wlog->logmsg); + } else { + syslog(wlog->prio, "%s: %.*s", + name, (int)wlog->loglen, wlog->logmsg); + } +} diff --git a/src/utils.c b/src/utils.c @@ -79,46 +79,6 @@ kore_debug_internal(char *file, int line, const char *fmt, ...) } #endif -void -kore_log_init(void) -{ -#if defined(KORE_SINGLE_BINARY) - extern const char *__progname; - const char *name = kore_strdup(__progname); -#else - const char *name = "kore"; -#endif - - if (!kore_foreground) - openlog(name, LOG_NDELAY | LOG_PID, LOG_DAEMON); -} - -void -kore_log(int prio, const char *fmt, ...) -{ - va_list args; - const char *name; - char buf[2048]; - - va_start(args, fmt); - (void)vsnprintf(buf, sizeof(buf), fmt, args); - va_end(args); - - if (worker != NULL) { - name = kore_worker_name(worker->id); - - if (kore_foreground) - printf("%s: %s\n", name, buf); - else - syslog(prio, "%s: %s", name, buf); - } else { - if (kore_foreground) - printf("[parent]: %s\n", buf); - else - syslog(prio, "[parent]: %s", buf); - } -} - size_t kore_strlcpy(char *dst, const char *src, const size_t len) { diff --git a/src/worker.c b/src/worker.c @@ -357,6 +357,9 @@ kore_worker_entry(struct kore_worker *kw) worker = kw; + if (!kore_foreground) + closelog(); + #if defined(__linux__) kore_seccomp_traceme(); #endif