From 94a341fe2c94b5224e3a9e6b1ee9e47fa2c8da5b Mon Sep 17 00:00:00 2001 From: Max Lv Date: Mon, 9 Jan 2017 17:56:56 +0800 Subject: [PATCH] Refine SIGCHLD handling --- src/local.c | 32 ++++++++++++++------------------ src/redir.c | 15 ++++++++------- src/server.c | 14 +++++++------- src/tunnel.c | 15 ++++++++------- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/local.c b/src/local.c index 322eefe1..476d9e41 100644 --- a/src/local.c +++ b/src/local.c @@ -108,6 +108,13 @@ static int mode = TCP_ONLY; static int ipv6first = 0; static int fast_open = 0; +static struct ev_signal sigint_watcher; +static struct ev_signal sigterm_watcher; +static struct ev_signal sigchld_watcher; +#ifndef __MINGW32__ + struct ev_signal sigusr1_watcher; +#endif + #ifdef HAVE_SETRLIMIT #ifndef LIB_ONLY static int nofile = 0; @@ -1118,12 +1125,6 @@ create_remote(listen_ctx_t *listener, return remote; } -void -resolve_int_cb(int dummy) -{ - keep_resolving = 0; -} - static void signal_cb(EV_P_ ev_signal *w, int revents) { @@ -1136,8 +1137,14 @@ signal_cb(EV_P_ ev_signal *w, int revents) #ifndef __MINGW32__ case SIGUSR1: #endif + ev_signal_stop(EV_DEFAULT, &sigint_watcher); + ev_signal_stop(EV_DEFAULT, &sigterm_watcher); + ev_signal_stop(EV_DEFAULT, &sigchld_watcher); +#ifndef __MINGW32__ + ev_signal_stop(EV_DEFAULT, &sigusr1_watcher); +#endif + keep_resolving = 0; ev_unloop(EV_A_ EVUNLOOP_ALL); - resolve_int_cb(revents); } } } @@ -1647,9 +1654,6 @@ start_ss_local_server(profile_t profile) signal(SIGABRT, SIG_IGN); #endif - struct ev_signal sigint_watcher; - struct ev_signal sigterm_watcher; - struct ev_signal sigchld_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_init(&sigchld_watcher, signal_cb, SIGCHLD); @@ -1657,7 +1661,6 @@ start_ss_local_server(profile_t profile) ev_signal_start(EV_DEFAULT, &sigterm_watcher); ev_signal_start(EV_DEFAULT, &sigchld_watcher); #ifndef __MINGW32__ - struct ev_signal sigusr1_watcher; ev_signal_init(&sigusr1_watcher, signal_cb, SIGUSR1); ev_signal_start(EV_DEFAULT, &sigusr1_watcher); #endif @@ -1746,13 +1749,6 @@ start_ss_local_server(profile_t profile) winsock_cleanup(); #endif - ev_signal_stop(EV_DEFAULT, &sigint_watcher); - ev_signal_stop(EV_DEFAULT, &sigterm_watcher); - ev_signal_stop(EV_DEFAULT, &sigchld_watcher); -#ifndef __MINGW32__ - ev_signal_stop(EV_DEFAULT, &sigusr1_watcher); -#endif - // cannot reach here return 0; } diff --git a/src/redir.c b/src/redir.c index b9cb5358..87010b96 100644 --- a/src/redir.c +++ b/src/redir.c @@ -96,6 +96,10 @@ static int auth = 0; static int nofile = 0; #endif +static struct ev_signal sigint_watcher; +static struct ev_signal sigterm_watcher; +static struct ev_signal sigchld_watcher; + int getdestaddr(int fd, struct sockaddr_storage *destaddr) { @@ -758,6 +762,10 @@ signal_cb(EV_P_ ev_signal *w, int revents) LOGE("plugin service exit unexpectedly"); case SIGINT: case SIGTERM: + ev_signal_stop(EV_DEFAULT, &sigint_watcher); + ev_signal_stop(EV_DEFAULT, &sigterm_watcher); + ev_signal_stop(EV_DEFAULT, &sigchld_watcher); + keep_resolving = 0; ev_unloop(EV_A_ EVUNLOOP_ALL); } @@ -1025,9 +1033,6 @@ main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); - struct ev_signal sigint_watcher; - struct ev_signal sigterm_watcher; - struct ev_signal sigchld_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_init(&sigchld_watcher, signal_cb, SIGCHLD); @@ -1107,9 +1112,5 @@ main(int argc, char **argv) stop_plugin(); } - ev_signal_stop(EV_DEFAULT, &sigint_watcher); - ev_signal_stop(EV_DEFAULT, &sigterm_watcher); - ev_signal_stop(EV_DEFAULT, &sigchld_watcher); - return 0; } diff --git a/src/server.c b/src/server.c index 2af10d37..ae8d8ad2 100644 --- a/src/server.c +++ b/src/server.c @@ -126,6 +126,10 @@ uint64_t rx = 0; ev_timer stat_update_watcher; ev_timer block_list_watcher; +static struct ev_signal sigint_watcher; +static struct ev_signal sigterm_watcher; +static struct ev_signal sigchld_watcher; + static struct cork_dllist connections; static void @@ -1490,6 +1494,9 @@ signal_cb(EV_P_ ev_signal *w, int revents) LOGE("plugin service exit unexpectedly"); case SIGINT: case SIGTERM: + ev_signal_stop(EV_DEFAULT, &sigint_watcher); + ev_signal_stop(EV_DEFAULT, &sigterm_watcher); + ev_signal_stop(EV_DEFAULT, &sigchld_watcher); ev_unloop(EV_A_ EVUNLOOP_ALL); } } @@ -1833,9 +1840,6 @@ main(int argc, char **argv) signal(SIGABRT, SIG_IGN); #endif - struct ev_signal sigint_watcher; - struct ev_signal sigterm_watcher; - struct ev_signal sigchld_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_init(&sigchld_watcher, signal_cb, SIGCHLD); @@ -2003,9 +2007,5 @@ main(int argc, char **argv) winsock_cleanup(); #endif - ev_signal_stop(EV_DEFAULT, &sigint_watcher); - ev_signal_stop(EV_DEFAULT, &sigterm_watcher); - ev_signal_stop(EV_DEFAULT, &sigchld_watcher); - return 0; } diff --git a/src/tunnel.c b/src/tunnel.c index 6750eb72..2380bb8d 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -101,6 +101,10 @@ static int auth = 0; static int nofile = 0; #endif +static struct ev_signal sigint_watcher; +static struct ev_signal sigterm_watcher; +static struct ev_signal sigchld_watcher; + #ifndef __MINGW32__ static int setnonblocking(int fd) @@ -719,6 +723,10 @@ signal_cb(EV_P_ ev_signal *w, int revents) LOGE("plugin service exit unexpectedly"); case SIGINT: case SIGTERM: + ev_signal_stop(EV_DEFAULT, &sigint_watcher); + ev_signal_stop(EV_DEFAULT, &sigterm_watcher); + ev_signal_stop(EV_DEFAULT, &sigchld_watcher); + keep_resolving = 0; ev_unloop(EV_A_ EVUNLOOP_ALL); } @@ -1025,9 +1033,6 @@ main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); - struct ev_signal sigint_watcher; - struct ev_signal sigterm_watcher; - struct ev_signal sigchld_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_init(&sigchld_watcher, signal_cb, SIGCHLD); @@ -1114,10 +1119,6 @@ main(int argc, char **argv) stop_plugin(); } - ev_signal_stop(EV_DEFAULT, &sigint_watcher); - ev_signal_stop(EV_DEFAULT, &sigterm_watcher); - ev_signal_stop(EV_DEFAULT, &sigchld_watcher); - #ifdef __MINGW32__ winsock_cleanup(); #endif