diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-05-13 20:35:39 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-05-13 20:35:39 +0200 |
commit | 635802489282cea52220569fed8297d75c98a774 (patch) | |
tree | 07002032a0969ca09da94be22601d11466c026ba | |
parent | 24de2cd600a4e108c24f6f77a5a4d30111fd9918 (diff) |
server: Better SIGINT handling
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | runloop.c | 20 |
2 files changed, 20 insertions, 2 deletions
@@ -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(); } @@ -1,5 +1,8 @@ +#include <stdio.h> +#include <stdbool.h> #include <string.h> #include <errno.h> +#include <signal.h> #include <sys/select.h> #include <sys/time.h> #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"); } |