...
authoralex <[email protected]>
Fri, 29 Apr 2022 01:38:42 +0000 (18:38 -0700)
committeralex <[email protected]>
Fri, 29 Apr 2022 01:38:42 +0000 (18:38 -0700)
Makefile.am
inc/init.h
inc/sighandler.h [new file with mode: 0644]
src/init.c
src/sighandler.c [new file with mode: 0644]
test/integration/basic.tests.c

index cb77b55f2a432657d966e4cec64870fcecdd66c4..84b645766fc9c60873cb207bfd0b36f0b7b2f47f 100644 (file)
@@ -81,6 +81,7 @@ seederd_SOURCES = \
        src/rss/info.c \
        src/rss/init.c \
        src/session.c \
+       src/sighandler.c \
        src/torrent/add.c \
        src/torrent/file.c \
        src/torrent/free.c \
@@ -115,6 +116,7 @@ seederd_SOURCES += \
        inc/pqueue.h \
        inc/rss.h \
        inc/session.h \
+       inc/sighandler.h \
        inc/torrent.h \
        inc/tree.h \
        inc/usage.h \
index eb3007e39f3da65b771ad4d1aab307117849eb69..63a3ae019657fde37b422b7dfcb4bd6403d50615 100644 (file)
@@ -2,11 +2,11 @@
 #define __INIT_H_
 
 #include<getopt.h>
-#include<signal.h>
 
 #include<default.h>
 #include<log.h>
 #include<session.h>
+#include<sighandler.h>
 #include<usage.h>
 
 extern struct option long_options[];
diff --git a/inc/sighandler.h b/inc/sighandler.h
new file mode 100644 (file)
index 0000000..e0d3323
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef __SIGHANDLER_H_
+#define __SIGHANDLER_H_
+
+#include<signal.h>
+#include<stdio.h>
+#include<stdlib.h>
+
+#include<log.h>
+
+#define SIGHANDLER_MESSAGE_GRACEFUL_SHUTDOWN "shutdown requested; exiting...\n"
+#define SIGHANDLER_MESSAGE_SHUTDOWN_COMPLETE "shutdown complete.\n"
+
+int signal_handler();
+
+#endif
index 7b03d6ca3f2ec95f6ee45f8f8e52af07b9f6d795..5dd2ce49b91bef1b7b6d075ed1841127a35bae8d 100644 (file)
@@ -19,8 +19,6 @@ struct option long_options[] = {
        {0,0,0,0}
 };
 
-static int signal_handler();
-
 int init(int argc, char **argv) {
        int c;
 
@@ -81,24 +79,3 @@ int init(int argc, char **argv) {
 
        return 1;
 }
-
-static void handle_interrupt() {
-       /*
-        * call exit explicitly will call functions registered
-        * with atexit and do appropriate cleanup.
-        */
-       exit(EXIT_FAILURE);
-}
-
-static int signal_handler() {
-       struct sigaction action;
-
-       action.sa_handler = &handle_interrupt;
-       action.sa_flags = SA_RESETHAND;
-       if(sigaction(SIGINT,&action,NULL)!=0) {
-               perror("sigaction");
-               return -1;
-       }
-
-       return 1;
-}
diff --git a/src/sighandler.c b/src/sighandler.c
new file mode 100644 (file)
index 0000000..a46f902
--- /dev/null
@@ -0,0 +1,37 @@
+#include<sighandler.h>
+
+static void handle_interrupt();
+static void shutdown_success();
+
+static void handle_interrupt() {
+       log_info(SIGHANDLER_MESSAGE_GRACEFUL_SHUTDOWN);
+       /*
+        * call exit explicitly will call functions registered
+        * with atexit and do appropriate cleanup.
+        */
+       exit(EXIT_SUCCESS);
+}
+
+static void shutdown_success() {
+       log_info(SIGHANDLER_MESSAGE_SHUTDOWN_COMPLETE);
+}
+
+int signal_handler() {
+       struct sigaction action;
+
+       action.sa_handler = &handle_interrupt;
+       action.sa_flags = SA_RESETHAND;
+       sigemptyset(&action.sa_mask);
+
+       if(sigaction(SIGINT,&action,NULL)!=0) {
+               perror("sigaction");
+               return -1;
+       }
+
+       if(0!=atexit(&shutdown_success)) {
+               perror("atexit");
+               return -1;
+       }
+
+       return 1;
+}
index 0bf86c1102d8a7680b655ee693b4632c531a4719..34c416ab9f0825e5f1e4f53445578af8cd77776c 100644 (file)
@@ -24,5 +24,6 @@ void basic_test() {
        
        run_and_exit_successfully(opts);
 
+       assert(0);
        reset_env();
 }