From a70641897da90ce81f404b9882de09df506c1d11 Mon Sep 17 00:00:00 2001 From: emanuele-f Date: Mon, 1 Jul 2019 00:25:41 +0200 Subject: [PATCH] Graceful termination in Windows --- edge.c | 19 +++++++++++++++++-- edge_utils.c | 32 ++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/edge.c b/edge.c index 12843f3..36edb8d 100644 --- a/edge.c +++ b/edge.c @@ -558,8 +558,6 @@ static void daemonize() { static int keep_on_running; -#ifdef __linux__ - static void term_handler(int sig) { static int called = 0; @@ -573,6 +571,20 @@ static void term_handler(int sig) { keep_on_running = 0; } + +/* *************************************************** */ + +#ifdef WIN32 + +BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) { + if(fdwCtrlType == CTRL_C_EVENT) { + term_handler(0); + return(TRUE); + } + + return(FALSE); +} + #endif /* *************************************************** */ @@ -690,6 +702,9 @@ int main(int argc, char* argv[]) { signal(SIGTERM, term_handler); signal(SIGINT, term_handler); #endif +#ifdef WIN32 + SetConsoleCtrlHandler(CtrlHandler, TRUE); +#endif keep_on_running = 1; traceEvent(TRACE_NORMAL, "edge started"); diff --git a/edge_utils.c b/edge_utils.c index 789246c..817ded5 100644 --- a/edge_utils.c +++ b/edge_utils.c @@ -1266,7 +1266,7 @@ static void readFromTAPSocket(n2n_edge_t * eee) { if((len <= 0) || (len > N2N_PKT_BUF_SIZE)) { traceEvent(TRACE_WARNING, "read()=%d [%d/%s]", - (signed int)len, errno, strerror(errno)); + (signed int)len, errno, strerror(errno)); } else { @@ -1292,11 +1292,17 @@ static void readFromTAPSocket(n2n_edge_t * eee) { /* ************************************** */ #ifdef WIN32 + +struct tunread_arg { + n2n_edge_t *eee; + int *keep_running; +}; + static DWORD* tunReadThread(LPVOID lpArg) { - n2n_edge_t *eee = (n2n_edge_t*)lpArg; + struct tunread_arg *arg = (struct tunread_arg*)lpArg; - while(1) - readFromTAPSocket(eee); + while(*arg->keep_running) + readFromTAPSocket(arg->eee); return((DWORD*)NULL); } @@ -1305,16 +1311,15 @@ static DWORD* tunReadThread(LPVOID lpArg) { /** Start a second thread in Windows because TUNTAP interfaces do not expose * file descriptors. */ -static void startTunReadThread(n2n_edge_t *eee) { - HANDLE hThread; +static HANDLE startTunReadThread(struct tunread_arg *arg) { DWORD dwThreadId; - hThread = CreateThread(NULL, /* security attributes */ + return(CreateThread(NULL, /* security attributes */ 0, /* use default stack size */ (LPTHREAD_START_ROUTINE)tunReadThread, /* thread function */ - (void*)eee, /* argument to thread function */ + (void*)arg, /* argument to thread function */ 0, /* thread creation flags */ - &dwThreadId); /* thread id out */ + &dwThreadId)); /* thread id out */ } #endif @@ -1580,7 +1585,10 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) { #endif #ifdef WIN32 - startTunReadThread(eee); + struct tunread_arg arg; + arg.eee = eee; + arg.keep_running = keep_running; + HANDLE tun_read_thread = startTunReadThread(&arg); #endif *keep_running = 1; @@ -1695,6 +1703,10 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) { #endif /* #ifdef __ANDROID_NDK__ */ } /* while */ +#ifndef WIN32 + WaitForSingleObject(tun_read_thread, INFINITE); +#endif + send_deregister(eee, &(eee->supernode)); closesocket(eee->udp_sock);