From a5e7de209582e9b37f08b040a562f1ffc4ef3f72 Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 2 May 2022 14:53:12 +0300 Subject: [PATCH] add SIGINT handler --- main.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 9c37ea7..40bf94d 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,28 @@ #include "net.h" #include "irc.h" +#ifndef _WIN32 +#include +static sig_atomic_t is_running = 1; +static void on_sigint(int signum) +{ + (void)signum; + is_running = 0; +} +static void set_handler() +{ + struct sigaction sa; + sa.sa_handler = on_sigint; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESETHAND; // only run it once + if (sigaction(SIGINT, &sa, NULL) == -1) + perror("sigaction"); +} +#else +#define is_running 1 +#define set_handler() +#endif + // Client linked list struct client_data *clients[CLIENTS]; // FDSET List @@ -18,6 +40,7 @@ fd_set readfds; #endif int main() { + set_handler(); int opt = 1; SOCKET serverSocketV4 = 0; SOCKET serverSocketV6 = 0; @@ -119,7 +142,7 @@ int main() { // Main loop - while(1){ + while(is_running){ FD_ZERO(&readfds); // Add listening SOCKETS to FDSET #ifdef SUPPORT_IPV4 @@ -142,6 +165,10 @@ int main() { // Wait until activity on sockets if(select(highestFD+1,&readfds,NULL,NULL,NULL) == -1) { +#ifndef _WIN32 + if (errno == EINTR) + continue; +#endif net_perror("ERROR SELECT"); } -- 2.56.0