Do some signal handling

This commit is contained in:
Thomas Kolb 2013-01-20 17:47:13 +01:00
parent eb16317dff
commit fc1562467a

37
main.c
View file

@ -8,6 +8,7 @@
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <linux/limits.h>
@ -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;
}