From d3856992710194330bae59210c4ca6154259ecdc Mon Sep 17 00:00:00 2001 From: Treeki Date: Wed, 12 Feb 2014 13:27:17 +0100 Subject: stop DNS thread cleanly when exiting --- bouncer/dns.cpp | 15 ++++++++++++++- bouncer/dns.h | 1 + bouncer/main.cpp | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/bouncer/dns.cpp b/bouncer/dns.cpp index 056bb09..f5bb75b 100644 --- a/bouncer/dns.cpp +++ b/bouncer/dns.cpp @@ -22,6 +22,7 @@ struct DNSQuery { }; static DNSQuery dnsQueue[DNS_QUERY_COUNT]; +static bool dnsQuit = false; static pthread_t dnsThread; static pthread_mutex_t dnsQueueMutex; static pthread_cond_t dnsQueueCond; @@ -41,6 +42,16 @@ void DNS::start() { } } +void DNS::stop() { + pthread_mutex_lock(&dnsQueueMutex); + dnsQuit = true; + pthread_mutex_unlock(&dnsQueueMutex); + pthread_cond_signal(&dnsQueueCond); + + pthread_join(dnsThread, NULL); + +} + int DNS::makeQuery(const char *name) { int id = -1; @@ -102,7 +113,9 @@ bool DNS::checkQuery(int id, in_addr *pResult, bool *pIsError) { void *dnsThreadProc(void *) { pthread_mutex_lock(&dnsQueueMutex); - for (;;) { + dnsQuit = false; + + while (!dnsQuit) { for (int i = 0; i < DNS_QUERY_COUNT; i++) { if (dnsQueue[i].status == DQS_WAITING) { char nameCopy[DNS_QUERY_NAME_SIZE]; diff --git a/bouncer/dns.h b/bouncer/dns.h index 78ec75f..90ba7dc 100644 --- a/bouncer/dns.h +++ b/bouncer/dns.h @@ -7,6 +7,7 @@ namespace DNS { void start(); + void stop(); int makeQuery(const char *name); void closeQuery(int id); bool checkQuery(int id, in_addr *pResult, bool *pIsError); diff --git a/bouncer/main.cpp b/bouncer/main.cpp index 6c248c8..b021120 100644 --- a/bouncer/main.cpp +++ b/bouncer/main.cpp @@ -53,6 +53,8 @@ int main(int argc, char **argv) { bounce.loadConfig(); int errcode = bounce.execute(); + + DNS::stop(); if (errcode < 0) { printf("(Bouncer::execute failed with %d)\n", errcode); return EXIT_FAILURE; -- cgit v1.2.3