diff --git a/src/local.c b/src/local.c index 13c86206..322eefe1 100644 --- a/src/local.c +++ b/src/local.c @@ -1392,7 +1392,7 @@ main(int argc, char **argv) plugin_host = "127.0.0.1"; plugin_port = tmp_port; - LOGI("plugin %s enabled", plugin); + LOGI("plugin \"%s\" enabled", plugin); } if (method == NULL) { @@ -1571,7 +1571,6 @@ main(int argc, char **argv) } // Clean up - if (plugin != NULL) { stop_plugin(); } diff --git a/src/redir.c b/src/redir.c index 60c1eb95..36e76cb6 100644 --- a/src/redir.c +++ b/src/redir.c @@ -748,11 +748,19 @@ accept_cb(EV_P_ ev_io *w, int revents) ev_io_start(EV_A_ & server->recv_ctx->io); } -void -signal_cb(int dummy) +static void +signal_cb(EV_P_ ev_signal *w, int revents) { - keep_resolving = 0; - exit(-1); + if (revents & EV_SIGNAL) { + switch (w->signum) { + case SIGCHLD: + LOGE("plugin service exit unexpectedly"); + case SIGINT: + case SIGTERM: + keep_resolving = 0; + ev_unloop(EV_A_ EVUNLOOP_ALL); + } + } } int @@ -950,7 +958,7 @@ main(int argc, char **argv) plugin_host = "127.0.0.1"; plugin_port = tmp_port; - LOGI("plugin %s enabled", plugin); + LOGI("plugin \"%s\" enabled", plugin); } if (method == NULL) { @@ -1015,9 +1023,16 @@ main(int argc, char **argv) // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); - signal(SIGINT, signal_cb); - signal(SIGTERM, signal_cb); - signal(SIGCHLD, signal_cb); + + 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); + ev_signal_start(EV_DEFAULT, &sigint_watcher); + ev_signal_start(EV_DEFAULT, &sigterm_watcher); + ev_signal_start(EV_DEFAULT, &sigchld_watcher); // Setup keys LOGI("initializing ciphers... %s", method); @@ -1091,5 +1106,9 @@ 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 95733af7..563d71aa 100644 --- a/src/server.c +++ b/src/server.c @@ -1814,7 +1814,7 @@ main(int argc, char **argv) } if (plugin != NULL) { - LOGI("plugin %s enabled", plugin); + LOGI("plugin \"%s\" enabled", plugin); } if (mode != TCP_ONLY) { diff --git a/src/tunnel.c b/src/tunnel.c index 5bc40f54..6750eb72 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -710,11 +710,19 @@ accept_cb(EV_P_ ev_io *w, int revents) ev_timer_start(EV_A_ & remote->send_ctx->watcher); } -void -signal_cb(int dummy) +static void +signal_cb(EV_P_ ev_signal *w, int revents) { - keep_resolving = 0; - exit(-1); + if (revents & EV_SIGNAL) { + switch (w->signum) { + case SIGCHLD: + LOGE("plugin service exit unexpectedly"); + case SIGINT: + case SIGTERM: + keep_resolving = 0; + ev_unloop(EV_A_ EVUNLOOP_ALL); + } + } } int @@ -941,7 +949,7 @@ main(int argc, char **argv) plugin_host = "127.0.0.1"; plugin_port = tmp_port; - LOGI("plugin %s enabled", plugin); + LOGI("plugin \"%s\" enabled", plugin); } if (method == NULL) { @@ -1016,9 +1024,16 @@ main(int argc, char **argv) // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); - signal(SIGINT, signal_cb); - signal(SIGTERM, signal_cb); - signal(SIGCHLD, signal_cb); + + 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); + ev_signal_start(EV_DEFAULT, &sigint_watcher); + ev_signal_start(EV_DEFAULT, &sigterm_watcher); + ev_signal_start(EV_DEFAULT, &sigchld_watcher); #endif // Setup keys @@ -1099,6 +1114,10 @@ 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