diff --git a/main.c b/main.c index 3859fef..6e8044b 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -186,6 +187,36 @@ static int connection_handler(void * cls, connection, MHD_HTTP_INTERNAL_SERVER_ERROR, error500Response); } +// signal handler for SIGTERM, SIGINT, etc. +// sets the flag for a clean shutdown +void sig_shutdown_handler(int sig) { + LOG(LVL_DEBUG, "Handling signal: %i", sig); + running = 0; +} + +void init_signal_handlers(void) { + struct sigaction sa; + sa.sa_handler = sig_shutdown_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_restorer = NULL; + + if(sigaction(SIGTERM, &sa, NULL) == -1) { + LOG(LVL_ERR, "sigaction [SIGTERM] failed: %s", strerror(errno)); + } + + if(sigaction(SIGINT, &sa, NULL) == -1) { + LOG(LVL_ERR, "sigaction [SIGINT] failed: %s", strerror(errno)); + } + + sa.sa_handler = SIG_IGN; + + if(sigaction(SIGPIPE, &sa, NULL) == -1) { + LOG(LVL_ERR, "sigaction [SIGPIPE] failed: %s", strerror(errno)); + } + +} + int main(int argc, char ** argv) { struct MHD_Daemon *d; struct stat sBuf; @@ -251,6 +282,9 @@ int main(int argc, char ** argv) { } #endif + // setup the signal handlers + init_signal_handlers(); + // create the static response for error pages error403Response = MHD_create_response_from_data( strlen(ERROR_403), @@ -289,7 +323,7 @@ int main(int argc, char ** argv) { } while(running) { - getc(stdin); + sleep(60); } LOG(LVL_INFO, "Shutting down..."); @@ -304,6 +338,7 @@ int main(int argc, char ** argv) { magic_close(magicCookie); #endif + LOG(LVL_INFO, "Thanks for using fileshare."); logger_shutdown(); return EXIT_SUCCESS; }