From 635802489282cea52220569fed8297d75c98a774 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 13 May 2017 20:35:39 +0200 Subject: server: Better SIGINT handling --- main.c | 2 +- runloop.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 5028190..43a24d2 100644 --- a/main.c +++ b/main.c @@ -162,6 +162,6 @@ int main(int argc,char **argv){ runloop_set_timeout(60*1000000,timeout_callback); runloop_add_fd(sock,server_socket_callback,false); runloop_run(); - printf("Runloop empty, shutting down\n"); + printf("Shutting down because runloop stopped\n"); db_close(); } diff --git a/runloop.c b/runloop.c index f95975c..6584098 100644 --- a/runloop.c +++ b/runloop.c @@ -1,5 +1,8 @@ +#include +#include #include #include +#include #include #include #include "runloop.h" @@ -18,11 +21,21 @@ static size_t fd_list_len,fd_list_cap; static i64 timeout_usecs=0; static runloop_callback *timeout_callback=NULL; +static bool sigint_received=false; + +static void signal_handler(int sig){ + if(sig==SIGINT){ + sigint_received=true; + } +} + __attribute__((constructor)) static void constructor(void){ fd_list_cap=16; fd_list_len=0; fd_list=malloc(fd_list_cap,struct fd_list_item); + + signal(SIGINT,signal_handler); } @@ -44,6 +57,11 @@ void runloop_add_fd(int fd,runloop_callback *func,bool use_timeout){ void runloop_run(void){ while(fd_list_len>0){ + if(sigint_received){ + printf("runloop: SIGINT received\n"); + break; + } + fd_set inset; FD_ZERO(&inset); int maxfd=-1; @@ -79,7 +97,7 @@ void runloop_run(void){ continue; } if(ret<0){ - if(errno==EINTR)continue; + if(errno==EINTR)continue; // SIGINT goes here die_perror("select"); } -- cgit v1.2.3-70-g09d2