Graceful termination in Windows

This commit is contained in:
emanuele-f 2019-07-01 00:25:41 +02:00
parent 47a298cb4d
commit a70641897d
2 changed files with 40 additions and 13 deletions

19
edge.c
View File

@ -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");

View File

@ -1292,11 +1292,17 @@ static void readFromTAPSocket(n2n_edge_t * eee) {
/* ************************************** */
#ifdef WIN32
static DWORD* tunReadThread(LPVOID lpArg) {
n2n_edge_t *eee = (n2n_edge_t*)lpArg;
while(1)
readFromTAPSocket(eee);
struct tunread_arg {
n2n_edge_t *eee;
int *keep_running;
};
static DWORD* tunReadThread(LPVOID lpArg) {
struct tunread_arg *arg = (struct tunread_arg*)lpArg;
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);