mirror of
https://github.com/ntop/n2n.git
synced 2024-09-20 00:51:10 +02:00
Graceful termination in Windows
This commit is contained in:
parent
47a298cb4d
commit
a70641897d
19
edge.c
19
edge.c
|
@ -558,8 +558,6 @@ static void daemonize() {
|
||||||
|
|
||||||
static int keep_on_running;
|
static int keep_on_running;
|
||||||
|
|
||||||
#ifdef __linux__
|
|
||||||
|
|
||||||
static void term_handler(int sig) {
|
static void term_handler(int sig) {
|
||||||
static int called = 0;
|
static int called = 0;
|
||||||
|
|
||||||
|
@ -573,6 +571,20 @@ static void term_handler(int sig) {
|
||||||
|
|
||||||
keep_on_running = 0;
|
keep_on_running = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* *************************************************** */
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
|
||||||
|
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) {
|
||||||
|
if(fdwCtrlType == CTRL_C_EVENT) {
|
||||||
|
term_handler(0);
|
||||||
|
return(TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return(FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* *************************************************** */
|
/* *************************************************** */
|
||||||
|
@ -690,6 +702,9 @@ int main(int argc, char* argv[]) {
|
||||||
signal(SIGTERM, term_handler);
|
signal(SIGTERM, term_handler);
|
||||||
signal(SIGINT, term_handler);
|
signal(SIGINT, term_handler);
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef WIN32
|
||||||
|
SetConsoleCtrlHandler(CtrlHandler, TRUE);
|
||||||
|
#endif
|
||||||
|
|
||||||
keep_on_running = 1;
|
keep_on_running = 1;
|
||||||
traceEvent(TRACE_NORMAL, "edge started");
|
traceEvent(TRACE_NORMAL, "edge started");
|
||||||
|
|
32
edge_utils.c
32
edge_utils.c
|
@ -1292,11 +1292,17 @@ static void readFromTAPSocket(n2n_edge_t * eee) {
|
||||||
/* ************************************** */
|
/* ************************************** */
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
static DWORD* tunReadThread(LPVOID lpArg) {
|
|
||||||
n2n_edge_t *eee = (n2n_edge_t*)lpArg;
|
|
||||||
|
|
||||||
while(1)
|
struct tunread_arg {
|
||||||
readFromTAPSocket(eee);
|
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);
|
return((DWORD*)NULL);
|
||||||
}
|
}
|
||||||
|
@ -1305,16 +1311,15 @@ static DWORD* tunReadThread(LPVOID lpArg) {
|
||||||
|
|
||||||
/** Start a second thread in Windows because TUNTAP interfaces do not expose
|
/** Start a second thread in Windows because TUNTAP interfaces do not expose
|
||||||
* file descriptors. */
|
* file descriptors. */
|
||||||
static void startTunReadThread(n2n_edge_t *eee) {
|
static HANDLE startTunReadThread(struct tunread_arg *arg) {
|
||||||
HANDLE hThread;
|
|
||||||
DWORD dwThreadId;
|
DWORD dwThreadId;
|
||||||
|
|
||||||
hThread = CreateThread(NULL, /* security attributes */
|
return(CreateThread(NULL, /* security attributes */
|
||||||
0, /* use default stack size */
|
0, /* use default stack size */
|
||||||
(LPTHREAD_START_ROUTINE)tunReadThread, /* thread function */
|
(LPTHREAD_START_ROUTINE)tunReadThread, /* thread function */
|
||||||
(void*)eee, /* argument to thread function */
|
(void*)arg, /* argument to thread function */
|
||||||
0, /* thread creation flags */
|
0, /* thread creation flags */
|
||||||
&dwThreadId); /* thread id out */
|
&dwThreadId)); /* thread id out */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1580,7 +1585,10 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
startTunReadThread(eee);
|
struct tunread_arg arg;
|
||||||
|
arg.eee = eee;
|
||||||
|
arg.keep_running = keep_running;
|
||||||
|
HANDLE tun_read_thread = startTunReadThread(&arg);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
*keep_running = 1;
|
*keep_running = 1;
|
||||||
|
@ -1695,6 +1703,10 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) {
|
||||||
#endif /* #ifdef __ANDROID_NDK__ */
|
#endif /* #ifdef __ANDROID_NDK__ */
|
||||||
} /* while */
|
} /* while */
|
||||||
|
|
||||||
|
#ifndef WIN32
|
||||||
|
WaitForSingleObject(tun_read_thread, INFINITE);
|
||||||
|
#endif
|
||||||
|
|
||||||
send_deregister(eee, &(eee->supernode));
|
send_deregister(eee, &(eee->supernode));
|
||||||
|
|
||||||
closesocket(eee->udp_sock);
|
closesocket(eee->udp_sock);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user