From 28c68097681b46ef7bb10fcf12bdf7b59159f2a4 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 14 Aug 2025 21:35:14 +0200 Subject: [PATCH] server: Fix error handling in main loop --- src/server.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/server.c b/src/server.c index bec2496..c9fb752 100644 --- a/src/server.c +++ b/src/server.c @@ -40,6 +40,8 @@ static SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG]; static client_ctx_t **clients; static sem_t sem_clients_lock; +static void terminate_gracefully(int sig); + static void clean(void) { notice("Cleaning sesimos cache and metadata files..."); @@ -121,7 +123,7 @@ static void accept_cb(void *arg) { client_ctx_t *client_ctx = malloc(sizeof(client_ctx_t)); if (client_ctx == NULL) { critical("Unable to allocate memory for client context"); - errno = 0; + terminate_gracefully(0); return; } sock *client = &client_ctx->socket; @@ -132,6 +134,7 @@ static void accept_cb(void *arg) { if (client_fd < 0) { critical("Unable to accept connection"); free(client_ctx); + terminate_gracefully(0); return; } @@ -146,6 +149,7 @@ static void accept_cb(void *arg) { continue; } else { critical("Unable to lock clients list"); + terminate_gracefully(0); return; } } @@ -154,8 +158,9 @@ static void accept_cb(void *arg) { clients = list_append(clients, &client_ctx); if (clients == NULL) { critical("Unable to add client context to list"); + sem_post(&sem_clients_lock); free(client_ctx); - errno = 0; + terminate_gracefully(0); return; }