]> PatchyGit - patchyircd.git/commitdiff
add SIGINT handler
authorEgor <egor@patchysicp.xyz>
Mon, 2 May 2022 11:53:12 +0000 (14:53 +0300)
committerEgor <egor@patchysicp.xyz>
Mon, 2 May 2022 11:53:12 +0000 (14:53 +0300)
main.c

diff --git a/main.c b/main.c
index 9c37ea7..40bf94d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,6 +2,28 @@
 #include "net.h"
 #include "irc.h"
 
+#ifndef _WIN32
+#include <signal.h>
+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");
                }