From f16bdbdf73593f4a19c737287fc215a215eeb01d Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 28 Nov 2025 14:03:51 +0100 Subject: [PATCH] fix: signal handling for SIGINT, default logs to stdout --- httpd/Makefile | 12 +++++++++-- httpd/src/logger/errors.c | 6 +++++- httpd/src/logger/errors.h | 4 +++- httpd/src/logger/logs.c | 19 ++++++++++++----- httpd/src/server/server.c | 44 ++++++++++++++++++++++++++++++++++++--- httpd/src/server/server.h | 10 +++++++-- 6 files changed, 81 insertions(+), 14 deletions(-) diff --git a/httpd/Makefile b/httpd/Makefile index 70108ef..0682316 100644 --- a/httpd/Makefile +++ b/httpd/Makefile @@ -31,8 +31,14 @@ TARGET=httpd $(TARGET): $(OBJS) $(CC) -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS) -check: - dash tests/run.sh +check: $(TARGET) + cp $(TARGET) tests/$(TARGET) + cd tests + python3 -m venv env + env/bin/python -m pip install requests + env/bin/python -m pip install pytest + env/bin/python -m pip install pytest-timeout + - env/bin/pytest debug: CFLAGS += $(CFLAGS_DBG) debug: $(OBJS) @@ -45,5 +51,7 @@ asan: $(OBJS) $(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LDLIBS) clean: + - pkill -9 $(TARGET) + $(RM) tests/$(TARGET) tests/out.log $(RM) $(TARGET) $(RM) $(OBJS) diff --git a/httpd/src/logger/errors.c b/httpd/src/logger/errors.c index c49a0a7..3f96b65 100644 --- a/httpd/src/logger/errors.c +++ b/httpd/src/logger/errors.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "../utils/time/fmt_time.h" #include "logs.h" @@ -16,10 +17,13 @@ static struct logs_config config; // === Functions -void errlog_init(bool enabled, int logfile_fd) +void errlog_init(bool enabled, int logfile_fd, struct server_config *serv_cfg) { config.enabled = enabled; + if (logfile_fd == STDOUT_FILENO) + config.logfile_fd = STDERR_FILENO; config.logfile_fd = logfile_fd; + config.server_cfg = serv_cfg; } void print_err() diff --git a/httpd/src/logger/errors.h b/httpd/src/logger/errors.h index bc225d8..a6f9be7 100644 --- a/httpd/src/logger/errors.h +++ b/httpd/src/logger/errors.h @@ -4,10 +4,12 @@ #include #include +#include "../config/config.h" + /* @brief Initialize the error logging submodule * @warning Do not use 'as is', use log_init() instead */ -void errlog_init(bool enabled, int logfile_fd); +void errlog_init(bool enabled, int logfile_fd, struct server_config *serv_cfg); /* @brief Retrieves the last error with errno and prints the corresponding * error message in the logs and stderr diff --git a/httpd/src/logger/logs.c b/httpd/src/logger/logs.c index f01cde6..118dd9b 100644 --- a/httpd/src/logger/logs.c +++ b/httpd/src/logger/logs.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "../utils/time/fmt_time.h" #include "errors.h" @@ -20,13 +21,21 @@ int log_init(struct config *global_config) int return_value = 0; config.enabled = global_config->log; config.server_cfg = global_config->servers; - config.logfile_fd = open(global_config->log_file, O_WRONLY); - if (config.logfile_fd <= 0) + if (global_config->log_file != NULL) { - config.enabled = false; - return_value = 1; + config.logfile_fd = open(global_config->log_file, O_WRONLY); + if (config.logfile_fd <= 0) + { + config.enabled = false; + return_value = 1; + } } - errlog_init(config.enabled, config.logfile_fd); + else + { + config.logfile_fd = STDOUT_FILENO; + } + + errlog_init(config.enabled, config.logfile_fd, config.server_cfg); return return_value; } diff --git a/httpd/src/server/server.c b/httpd/src/server/server.c index e772298..b9a4c24 100644 --- a/httpd/src/server/server.c +++ b/httpd/src/server/server.c @@ -1,23 +1,30 @@ // === Definitions -#include #define _POSIX_C_SOURCE 200112L #define BUFFER_SIZE 1024 // === Includes +#include "server.h" + #include #include +#include #include #include +#include #include #include #include "../http/http.h" #include "../logger/errors.h" -#include "server.h" +#include "../logger/logs.h" // #include "../logger/logs.h" +// === Static variables + +int server_socket = 0; + // === Static functions // Creates and bind the server communication socket @@ -102,11 +109,26 @@ static void get_ip(struct sockaddr *client_addr, char *res) inet_ntop(AF_INET, &ipAddr, res, INET_ADDRSTRLEN); } +static void signal_handler(int signal) +{ + print_log("signal received: %d", signal); + switch (signal) + { + case SIGINT: { + print_log("Stopping server..."); + stop_server(); + exit(0); + } + default: + return; + } +} + // === Functions void start_server(const char *host, const char *port) { - int server_socket = get_socket(host, port); + server_socket = get_socket(host, port); if (server_socket == -1) // TODO log that return; @@ -115,6 +137,17 @@ void start_server(const char *host, const char *port) if (err == -1) return; + // Signal handling + struct sigaction siga; + siga.sa_flags = 0; + siga.sa_handler = signal_handler; + // initialize mask + if (sigemptyset(&siga.sa_mask) < 0) + // TODO log that + return; + if (sigaction(SIGINT, &siga, NULL) == -1) + return; + // Main loop while (1) { @@ -134,5 +167,10 @@ void start_server(const char *host, const char *port) close(client_fd); } + stop_server(); +} + +void stop_server(void) +{ close(server_socket); } diff --git a/httpd/src/server/server.h b/httpd/src/server/server.h index fe95e52..10f6423 100644 --- a/httpd/src/server/server.h +++ b/httpd/src/server/server.h @@ -3,11 +3,17 @@ // #define _POSIX_C_SOURCE 200112L -/* @brief +/* @brief Starts the HTTP server * - * @param hpstname + * @warn Make sure to initialize modules before calling + * + * @param hostname * @param port */ void start_server(const char *host, const char *port); +/* @brief Stops the currently running HTTP server + */ +void stop_server(void); + #endif // ! SERVER_H