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 e2fcedfaecb6f006ed4f4a4e17dd5246950304c3
parent 7316f372d149c046a8e06572c7bcd0fe592ea1a8
Author: Joris Vink <joris@coders.se>
Date:   Mon,  8 Aug 2022 11:02:27 +0200

Differentiate between normal shutdown and fatal.

The parent process never differentiated between a worker process
asking for a shutdown or a worker process calling fatalx() when
it came to its exit code.

I made some changes here so the parent process will exit with
an exit code 1 if anything worker related went wrong (fatalx/death policy).

Diffstat:
include/kore/kore.h | 6++++++
src/kore.c | 8++++----
src/msg.c | 6+++++-
src/utils.c | 4++--
src/worker.c | 8++++----
5 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/include/kore/kore.h b/include/kore/kore.h @@ -79,6 +79,11 @@ typedef void KORE_PRIVATE_KEY; #define KORE_RSAKEY_BITS 4096 +/* Kore quit reasons. */ +#define KORE_QUIT_NONE -1 +#define KORE_QUIT_NORMAL 0 +#define KORE_QUIT_FATAL 1 + #define KORE_RESULT_ERROR 0 #define KORE_RESULT_OK 1 #define KORE_RESULT_RETRY 2 @@ -672,6 +677,7 @@ struct kore_timer { #define KORE_MSG_ACCEPT_AVAILABLE 10 #define KORE_PYTHON_SEND_OBJ 11 #define KORE_MSG_WORKER_LOG 12 +#define KORE_MSG_FATALX 13 #define KORE_MSG_ACME_BASE 100 /* messages for applications should start at 201. */ diff --git a/src/kore.c b/src/kore.c @@ -54,7 +54,6 @@ volatile sig_atomic_t sig_recv; struct kore_server_list kore_servers; u_int8_t nlisteners; int kore_argc = 0; -int kore_quit = 0; pid_t kore_pid = -1; u_int16_t cpu_count = 1; int kore_debug = 0; @@ -66,6 +65,7 @@ char **kore_argv = NULL; int kore_foreground = 0; char *kore_progname = NULL; u_int32_t kore_socket_backlog = 5000; +int kore_quit = KORE_QUIT_NONE; char *kore_pidfile = KORE_PIDFILE_DEFAULT; struct kore_privsep worker_privsep; @@ -295,7 +295,7 @@ main(int argc, char *argv[]) kore_mem_cleanup(); - return (0); + return (kore_quit); } void @@ -910,7 +910,7 @@ kore_server_start(int argc, char *argv[]) kore_msg_unregister(KORE_PYTHON_SEND_OBJ); #endif - while (kore_quit != 1) { + while (kore_quit == KORE_QUIT_NONE) { last_sig = sig_recv; if (last_sig != 0) { @@ -922,7 +922,7 @@ kore_server_start(int argc, char *argv[]) case SIGINT: case SIGQUIT: case SIGTERM: - kore_quit = 1; + kore_quit = KORE_QUIT_NORMAL; kore_worker_dispatch_signal(last_sig); continue; case SIGUSR1: diff --git a/src/msg.c b/src/msg.c @@ -64,6 +64,7 @@ kore_msg_parent_init(void) kore_msg_parent_add(kw); } + kore_msg_register(KORE_MSG_FATALX, msg_type_shutdown); kore_msg_register(KORE_MSG_SHUTDOWN, msg_type_shutdown); } @@ -273,7 +274,10 @@ msg_type_shutdown(struct kore_msg *msg, const void *data) "shutdown requested by worker %u, going down", msg->src); } - kore_quit = 1; + if (msg->id == KORE_MSG_FATALX) + kore_quit = KORE_QUIT_FATAL; + else + kore_quit = KORE_QUIT_NORMAL; } #if !defined(KORE_NO_HTTP) diff --git a/src/utils.c b/src/utils.c @@ -560,7 +560,7 @@ fatalx(const char *fmt, ...) /* In case people call fatalx() from the parent context. */ if (worker != NULL) - kore_msg_send(KORE_MSG_PARENT, KORE_MSG_SHUTDOWN, NULL, 0); + kore_msg_send(KORE_MSG_PARENT, KORE_MSG_FATALX, NULL, 0); va_start(args, fmt); fatal_log(fmt, args); @@ -575,7 +575,7 @@ fatal_log(const char *fmt, va_list args) char buf[2048]; (void)vsnprintf(buf, sizeof(buf), fmt, args); - kore_log(LOG_ERR, "FATAL: %s", buf); + kore_log(LOG_ERR, "fatal: %s", buf); if (worker != NULL && worker->id == KORE_WORKER_KEYMGR) kore_keymgr_cleanup(1); diff --git a/src/worker.c b/src/worker.c @@ -897,7 +897,7 @@ worker_reaper(pid_t pid, int status) kore_log(LOG_CRIT, "keymgr or acme process gone, stopping"); kw->pid = 0; - kore_quit = 1; + kore_quit = KORE_QUIT_FATAL; break; } @@ -919,17 +919,17 @@ worker_reaper(pid_t pid, int status) kw->pid = 0; kore_log(LOG_NOTICE, "worker policy is 'terminate', stopping"); - kore_quit = 1; + kore_quit = KORE_QUIT_FATAL; break; } - if (kore_quit == 0) { + if (kore_quit == KORE_QUIT_NONE) { kore_log(LOG_NOTICE, "restarting worker %d", kw->id); kw->restarted = 1; kore_msg_parent_remove(kw); if (!kore_worker_spawn(idx, kw->id, kw->cpu)) { - kore_quit = 1; + kore_quit = KORE_QUIT_FATAL; kore_log(LOG_ERR, "failed to restart worker"); } else { kore_msg_parent_add(kw);