aboutsummaryrefslogtreecommitdiff
path: root/runloop.c
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-05-13 20:35:39 +0200
committertomsmeding <tom.smeding@gmail.com>2017-05-13 20:35:39 +0200
commit635802489282cea52220569fed8297d75c98a774 (patch)
tree07002032a0969ca09da94be22601d11466c026ba /runloop.c
parent24de2cd600a4e108c24f6f77a5a4d30111fd9918 (diff)
server: Better SIGINT handling
Diffstat (limited to 'runloop.c')
-rw-r--r--runloop.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/runloop.c b/runloop.c
index f95975c..6584098 100644
--- a/runloop.c
+++ b/runloop.c
@@ -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");
}