From b076b34a1fde0f0948ff6da26164137f166d2cb0 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Fri, 28 Jun 2013 16:19:56 +0200 Subject: [PATCH] add syslog to dnsdist, plus make it daemonize itself --- pdns/dnsdist.cc | 86 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 8376501f1..f0b0d278e 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -40,7 +40,14 @@ po::variables_map g_vm; bool g_verbose; AtomicCounter g_pos; uint16_t g_maxOutstanding; +bool g_console; +#define infolog(X,Y) if(g_verbose) { syslog(LOG_INFO, "%s", (boost::format((X)) % Y).str().c_str()); \ + if(g_console) cout << boost::format((X)) %Y << endl; } do{}while(0) +#define warnlog(X,Y) { syslog(LOG_WARNING, "%s", (boost::format((X)) % Y).str().c_str()); \ + if(g_console) cout << boost::format((X)) %Y << endl; } do{}while(0) +#define errlog(X,Y) {syslog(LOG_ERR, "%s", (boost::format((X)) % Y).str().c_str()); \ + if(g_console) cout << boost::format((X)) %Y << endl; }do{}while(0) /* UDP: the grand design. Per socket we listen on for incoming queries there is one thread. Then we have a bunch of connected sockets for talking to downstream servers. @@ -108,8 +115,7 @@ void* responderThread(void *p) dh->id = ids->origID; sendto(ids->origFD, packet, len, 0, (struct sockaddr*)&ids->origRemote, ids->origRemote.getSocklen()); - if(g_verbose) - cout << "Got answer from "<remote.toStringWithPort()<<", relayed to "<origRemote.toStringWithPort()<remote.toStringWithPort() % ids->origRemote.toStringWithPort()); ids->origFD = -1; } @@ -136,6 +142,25 @@ DownstreamState& getBestDownstream() return g_dstates[chosen]; } +static void daemonize(void) +{ + if(fork()) + _exit(0); // bye bye + + setsid(); + + int i=open("/dev/null",O_RDWR); /* open stdin */ + if(i < 0) + ; // L<remote.toStringWithPort()<remote.toStringWithPort()); int sock = SSocket((*ds)->remote.sin4.sin_family, SOCK_STREAM, 0); SConnect(sock, (*ds)->remote); return sock; @@ -261,8 +284,7 @@ public: // Should not be called simultaneously! void addTCPClientThread() { - if(g_verbose) - cout<<"Adding TCP Client thread"<remote.toStringWithPort()); } uint16_t qlen, rlen; @@ -313,8 +334,7 @@ void* tcpClientThread(void* p) retry:; if(!putMsgLen(dsock, qlen)) { - if(g_verbose) - cout<<"Downstream connection died on us, getting a new one!"<remote.toStringWithPort()); close(dsock); dsock=getTCPDownstream(&ds); goto retry; @@ -323,8 +343,7 @@ void* tcpClientThread(void* p) writen2(dsock, query, qlen); if(!getMsgLen(dsock, &rlen)) { - if(g_verbose) - cout<<"Downstream connection died on us phase 2, getting a new one!"<remote.toStringWithPort()); close(dsock); dsock=getTCPDownstream(&ds); goto retry; @@ -338,8 +357,7 @@ void* tcpClientThread(void* p) } } catch(...){} - if(g_verbose) - cout<<"Closing client connection"<outstanding; @@ -364,8 +382,7 @@ void* tcpAcceptorThread(void* p) try { ConnectionInfo* ci = new ConnectionInfo; ci->fd = SAccept(cs->tcpFD, remote); - if(g_verbose) - cout << "Got connection from "<remote = remote; writen2(g_tcpclientthreads.getThread(), &ci, sizeof(ci)); @@ -396,8 +413,8 @@ void* statThread(void*) uint64_t numQueries=0; for(unsigned int n=0; n < g_numdownstreams; ++n) { DownstreamState& dss = g_dstates[n]; - if(g_verbose) - cout<<'\t'<()->default_value(true), "run in background") ("local", po::value >(), "Listen on which address") ("max-outstanding", po::value()->default_value(65535), "maximum outstanding queries per downstream") ("verbose,v", "be verbose"); @@ -454,6 +474,14 @@ try cout<()) + daemonize(); + else { + infolog("Running in the %s", "foreground"); + g_console=true; + } + vector remotes = g_vm["remotes"].as >(); g_numdownstreams = remotes.size(); @@ -469,8 +497,8 @@ try dss.idStates.resize(g_maxOutstanding); - if(g_verbose) - cout << "Added downstream server "<udpFD, IPPROTO_IPV6, IPV6_V6ONLY, 1); } SBind(cs->udpFD, cs->local); - if(g_verbose) - cout<<"Listening on "<local.toStringWithPort()<tcpFD, cs->local); SListen(cs->tcpFD, 64); + warnlog("Listening on %s",cs->local.toStringWithPort()); + pthread_create(&tid, 0, tcpAcceptorThread, (void*) cs); } pthread_t stattid; pthread_create(&stattid, 0, statThread, 0); void* status; - pthread_join(tid, &status); + pthread_join(tid, &status); } catch(std::exception &e) { - cerr<<"Fatal: "<