From 0931bdcbdd9a137e544737288a84deea5e167723 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 1 Mar 2025 22:01:02 +0200 Subject: [PATCH] move signal handling to job, add libuv support --- src/job.cpp | 46 +++++++++++++++++++++++++++++++++------------- src/main.cpp | 22 +--------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/job.cpp b/src/job.cpp index 9323da4..336d876 100644 --- a/src/job.cpp +++ b/src/job.cpp @@ -17,7 +17,16 @@ static int joblen = 0, jobcap = 0; static pid_t current_child = -1; static int current_result = 0; -static void job_child(int signum) + +#ifdef NETPOLL_USE_LIBUV +#include +static uv_signal_t uvsigchld, uvsigint; +#define HANDLE_ARG [[maybe_unused]] uv_signal_t *handle, +#else +#define HANDLE_ARG +#endif + +static void job_child(HANDLE_ARG [[maybe_unused]] int signum) { (void)signum; int old_errno = errno; @@ -31,33 +40,44 @@ static void job_child(int signum) } errno = old_errno; } -#ifdef NETPOLL_USE_LIBUV -#include -static uv_signal_t uvsig; -static void job_child_cb(uv_signal_t *handle, int signum) +static void job_int(HANDLE_ARG [[maybe_unused]] int signum) { - (void)handle; - job_child(signum); + extern volatile sig_atomic_t is_siginted; + is_siginted = 1; } -#endif void job_init(void) { -#ifndef NETPOLL_USE_LIBUV struct sigaction sa; - sa.sa_handler = job_child; sigemptyset(&sa.sa_mask); + + sa.sa_handler = SIG_IGN; + sa.sa_flags = SA_RESTART; + if (sigaction(SIGPIPE, &sa, NULL) == -1) + perror("sigaction"); + +#ifndef NETPOLL_USE_LIBUV + sa.sa_handler = job_child; sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) perror("sigaction"); + + sa.sa_handler = job_int; + /* Note that SA_RESTART doesn't affect select/poll/epoll_wait. */ + sa.sa_flags = SA_RESETHAND | SA_RESTART; + if (sigaction(SIGINT, &sa, NULL) == -1) + perror("sigaction"); #else - uv_signal_init(uv_default_loop(), &uvsig); - uv_signal_start(&uvsig, job_child_cb, SIGCHLD); + uv_signal_init(uv_default_loop(), &uvsigchld); + uv_signal_start(&uvsigchld, job_child, SIGCHLD); + uv_signal_init(uv_default_loop(), &uvsigint); + uv_signal_start_oneshot(&uvsigint, job_int, SIGINT); #endif } void job_fini(void) { #ifndef NETPOLL_USE_LIBUV - uv_signal_stop(&uvsig); + uv_signal_stop(&uvsigchld); + uv_signal_stop(&uvsigint); #endif } static void job_process(void) diff --git a/src/main.cpp b/src/main.cpp index 68be53e..6ae8fdb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,26 +7,7 @@ static int is_running = 1; #include -static volatile sig_atomic_t is_siginted = 0; -static void on_sigint(int signum) -{ - (void)signum; - is_siginted = 1; -} -static void set_handler(void) -{ - struct sigaction sa; - sa.sa_handler = on_sigint; - sigemptyset(&sa.sa_mask); - /* Note that SA_RESTART doesn't affect select/poll/epoll_wait. */ - sa.sa_flags = SA_RESETHAND | SA_RESTART; - if (sigaction(SIGINT, &sa, NULL) == -1) - perror("sigaction"); - sa.sa_handler = SIG_IGN; - sa.sa_flags = SA_RESTART; - if (sigaction(SIGPIPE, &sa, NULL) == -1) - perror("sigaction"); -} +volatile sig_atomic_t is_siginted = 0; // Client linked list struct client_data *clients = NULL, *dyingclients = NULL; @@ -192,7 +173,6 @@ int main(int argc, char *argv[]) #ifdef HAS_LUA admin_init(); #endif - set_handler(); databuf.buf = (char *)malloc(sizeof(char) * 512); databuf.cap = 512; -- 2.56.0