From 1cad8f1308e4af86027f9ae25523b8a8b3f6592c Mon Sep 17 00:00:00 2001 From: "info@mode42.com" Date: Sun, 9 Aug 2026 05:27:00 +0000 Subject: Next development steps --- src/main.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 670de7a..c91982e 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -215,6 +216,42 @@ static void setup_install_root(const char *config_path) } } + +/* ── Signal handling ────────────────────────────────────────── */ + +static volatile sig_atomic_t g_signal_received; + +static void signal_handler(int sig) +{ + g_signal_received = sig; + /* SIGPIPE: just ignore — send() with MSG_NOSIGNAL handles most cases */ + if (sig == SIGPIPE) { + return; + } + /* SIGTERM/SIGINT: main loop will see g_signal_received and exit */ +} + +static void install_signal_handlers(void) +{ + struct sigaction sa; + + memset(&sa, 0, sizeof(sa)); + + /* SIGPIPE: ignore — prevents silent death on broken socket writes */ + sa.sa_handler = SIG_IGN; + sa.sa_flags = 0; + sigaction(SIGPIPE, &sa, NULL); + + /* SIGTERM: graceful shutdown */ + sa.sa_handler = signal_handler; + sigaction(SIGTERM, &sa, NULL); + + /* SIGINT: graceful shutdown (Ctrl+C) */ + sigaction(SIGINT, &sa, NULL); + + g_signal_received = 0; +} + int main(int argc, char *argv[]) { hybbx_service_t *service; @@ -355,8 +392,22 @@ int main(int argc, char *argv[]) hybbx_service_set_launch_binary(service, argv[0]); + install_signal_handlers(); + + printf("HyBBX %s running (%s). Signals: SIGPIPE=ignored, SIGTERM/SIGINT=graceful stop.\n", + HYBBX_VERSION_STRING, hybbx_instance_binary_name()); + hybbx_service_run(service); + if (g_signal_received) { + printf("HyBBX: received signal %d (%s) — shutting down.\n", + g_signal_received, + g_signal_received == SIGTERM ? "SIGTERM" : + g_signal_received == SIGINT ? "SIGINT" : "unknown"); + } else { + printf("HyBBX: service loop exited normally.\n"); + } + if (hybbx_service_shutdown_mode(service) == HYBBX_SHUTDOWN_RESTART) { hybbx_service_restart_exec(service); } @@ -367,5 +418,7 @@ int main(int argc, char *argv[]) hybbx_commands_registry_shutdown(); hybbx_service_destroy(service); + printf("HyBBX: shutdown complete (exit %d, signal=%d).\n", + EXIT_SUCCESS, (int)g_signal_received); return EXIT_SUCCESS; } -- cgit v1.3.1