diff --git a/src/main.cpp b/src/main.cpp index c5d6181..49034c8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,14 +36,20 @@ #include #include +static Worker *worker = NULL; + static void sig_term_handler(int) { syslog(LOG_INFO, "SIGTERM received"); + if (worker) + worker->stop(); } static void sig_int_handler(int) { syslog(LOG_INFO, "SIGINT received"); + if (worker) + worker->stop(); } static void usage() @@ -93,7 +99,6 @@ int main(int argc, char *argv[]) bool changeEchoId = false; bool changeEchoSeq = false; bool verbose = false; - Worker *worker = NULL; openlog(argv[0], LOG_PERROR, LOG_DAEMON); diff --git a/src/worker.cpp b/src/worker.cpp index eed6a63..d183d2b 100644 --- a/src/worker.cpp +++ b/src/worker.cpp @@ -127,7 +127,12 @@ void Worker::run() // wait for data or timeout int result = select(maxFd + 1 , &fs, NULL, NULL, nextTimeout != Time::ZERO ? &timeout.getTimeval() : NULL); if (result == -1) - throw Exception("select", true); + { + if (alive) + throw Exception("select", true); + else + return; + } now = Time::now(); // timeout @@ -183,6 +188,11 @@ void Worker::run() } } +void Worker::stop() +{ + alive = false; +} + void Worker::dropPrivileges() { #ifndef WIN32 diff --git a/src/worker.h b/src/worker.h index 5e4aba7..7c3ffb1 100644 --- a/src/worker.h +++ b/src/worker.h @@ -34,6 +34,7 @@ public: virtual ~Worker(); virtual void run(); + virtual void stop(); static int headerSize() { return sizeof(TunnelHeader); }