From: alex Date: Fri, 29 Apr 2022 01:38:42 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=ab0954dc5d31dd5091df1e5f13bb5edffd0b2541;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index cb77b55..84b6457 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/inc/init.h b/inc/init.h index eb3007e..63a3ae0 100644 --- a/inc/init.h +++ b/inc/init.h @@ -2,11 +2,11 @@ #define __INIT_H_ #include -#include #include #include #include +#include #include extern struct option long_options[]; diff --git a/inc/sighandler.h b/inc/sighandler.h new file mode 100644 index 0000000..e0d3323 --- /dev/null +++ b/inc/sighandler.h @@ -0,0 +1,15 @@ +#ifndef __SIGHANDLER_H_ +#define __SIGHANDLER_H_ + +#include +#include +#include + +#include + +#define SIGHANDLER_MESSAGE_GRACEFUL_SHUTDOWN "shutdown requested; exiting...\n" +#define SIGHANDLER_MESSAGE_SHUTDOWN_COMPLETE "shutdown complete.\n" + +int signal_handler(); + +#endif diff --git a/src/init.c b/src/init.c index 7b03d6c..5dd2ce4 100644 --- a/src/init.c +++ b/src/init.c @@ -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 index 0000000..a46f902 --- /dev/null +++ b/src/sighandler.c @@ -0,0 +1,37 @@ +#include + +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; +} diff --git a/test/integration/basic.tests.c b/test/integration/basic.tests.c index 0bf86c1..34c416a 100644 --- a/test/integration/basic.tests.c +++ b/test/integration/basic.tests.c @@ -24,5 +24,6 @@ void basic_test() { run_and_exit_successfully(opts); + assert(0); reset_env(); }