From 4c1b2b65f3e21f064cb7a6f8d9a43d690f8d9d56 Mon Sep 17 00:00:00 2001 From: bol-van Date: Sun, 11 Jan 2026 21:01:18 +0300 Subject: [PATCH] nfqws2: gracefully shutdown on SIGINT and SIGTERM --- docs/changes.txt | 1 + nfq2/nfqws.c | 49 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 6532457..0f2dd83 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -177,3 +177,4 @@ v0.8.1 * nfqws2: move ctx from lightuserdata to userdata. prevents crashes on specific ARM cpus * nfqws2: alternative representation of payload filter in execution_plan item * nfqws2: --payload-disable +* nfqws2: gracefully shutdown on SIGINT and SIGTERM diff --git a/nfq2/nfqws.c b/nfq2/nfqws.c index cdcc041..ae7325e 100644 --- a/nfq2/nfqws.c +++ b/nfq2/nfqws.c @@ -52,9 +52,7 @@ struct params_s params; static volatile sig_atomic_t bReload = false; -#ifdef __CYGWIN__ bool bQuit = false; -#endif static void onhup(int sig) { @@ -102,12 +100,37 @@ static void onusr2(int sig) ipcachePrint(¶ms.ipcache); printf("\n"); } +static void onint(int sig) +{ + const char *msg = "INT received !\n"; + size_t wr = write(1, msg, strlen(msg)); + bQuit = true; +} +static void onterm(int sig) +{ + const char *msg = "TERM received !\n"; + size_t wr = write(1, msg, strlen(msg)); + bQuit = true; +} static void pre_desync(void) { - signal(SIGHUP, onhup); - signal(SIGUSR1, onusr1); - signal(SIGUSR2, onusr2); + struct sigaction sa; + + sigemptyset(&sa.sa_mask); + sa.sa_sigaction = NULL; + sa.sa_flags = 0; + + sa.sa_handler = onhup; + sigaction(SIGHUP, &sa, NULL); + sa.sa_handler = onusr1; + sigaction(SIGUSR1, &sa, NULL); + sa.sa_handler = onusr2; + sigaction(SIGUSR2, &sa, NULL); + sa.sa_handler = onint; + sigaction(SIGINT, &sa, NULL); + sa.sa_handler = onterm; + sigaction(SIGTERM, &sa, NULL); } @@ -364,6 +387,15 @@ static int nfq_main(void) else DLOG("recv from nfq returned 0 !\n"); } + if (errno==EINTR) + { + if (bQuit) + { + DLOG_CONDUP("quit requested\n"); + goto exok; + } + continue; + } e = errno; DLOG_ERR("recv: recv=%zd errno %d\n", rd, e); errno = e; @@ -371,6 +403,7 @@ static int nfq_main(void) // do not fail on ENOBUFS } while (e == ENOBUFS); +exok: res=0; ex: nfq_deinit(&h, &qh); @@ -487,6 +520,11 @@ static int dvt_main(void) { if (errno == EINTR) { + if (bQuit) + { + DLOG_CONDUP("quit requested\n"); + goto exitok; + } // a signal received continue; } @@ -567,6 +605,7 @@ static int dvt_main(void) } } +exitok: res = 0; exiterr: if (Fpid) fclose(Fpid);