diff options
| author | info@mode42.com <info@mode42.com> | 2026-08-09 05:27:00 +0000 |
|---|---|---|
| committer | info@mode42.com <info@mode42.com> | 2026-08-09 05:27:00 +0000 |
| commit | 1cad8f1308e4af86027f9ae25523b8a8b3f6592c (patch) | |
| tree | 7fa911b052986a69728343a09f8dde3d1c206c9b /src/main.c | |
| parent | 20cb29c2f8c5c87bc590896854a20b1473ceb358 (diff) | |
Next development steps
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -23,6 +23,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <signal.h> #include <unistd.h> #include <libgen.h> @@ -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; } |
