]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Rename static, global and thread_local vars to follow naming conventions 11175/head
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 12 Jan 2022 08:25:24 +0000 (09:25 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 12 Jan 2022 08:31:11 +0000 (09:31 +0100)
The thread_local vars in houseKeeping() should be reviewed, as some of them are only relevant for the handler thread,
so they do not need to be thread_local.

pdns/pdns_recursor.cc
pdns/rec_channel_rec.cc
pdns/recursordist/rec-main.cc
pdns/recursordist/rec-main.hh
pdns/recursordist/rec-tcp.cc

index fc5d5156b3fa73ba274c7ac7dc38f0ea876e2421..5f0414013d1e6477e0c88050bf7ef0d8e16abf1f 100644 (file)
@@ -68,7 +68,7 @@ static set<int> g_fromtosockets; // listen sockets that use 'sendfromto()' mecha
 NetmaskGroup g_XPFAcl;
 NetmaskGroup g_paddingFrom;
 size_t g_proxyProtocolMaximumSize;
-size_t s_maxUDPQueriesPerRound;
+size_t g_maxUDPQueriesPerRound;
 unsigned int g_maxMThreads;
 unsigned int g_paddingTag;
 PaddingMode g_paddingMode;
@@ -80,13 +80,13 @@ bool g_gettagNeedsEDNSOptions{false};
 bool g_useKernelTimestamp;
 std::atomic<uint32_t> g_maxCacheEntries, g_maxPacketCacheEntries;
 #ifdef HAVE_BOOST_CONTAINER_FLAT_SET_HPP
-boost::container::flat_set<uint16_t> s_avoidUdpSourcePorts;
+boost::container::flat_set<uint16_t> g_avoidUdpSourcePorts;
 #else
-std::set<uint16_t> s_avoidUdpSourcePorts;
+std::set<uint16_t> g_avoidUdpSourcePorts;
 #endif
-uint16_t s_minUdpSourcePort;
-uint16_t s_maxUdpSourcePort;
-double s_balancingFactor;
+uint16_t g_minUdpSourcePort;
+uint16_t g_maxUdpSourcePort;
+double g_balancingFactor;
 
 RecursorStats g_stats;
 bool g_lowercaseOutgoing;
@@ -176,8 +176,8 @@ int UDPClientSocks::makeClientSocket(int family)
     }
     else {
       do {
-        port = s_minUdpSourcePort + dns_random(s_maxUdpSourcePort - s_minUdpSourcePort + 1);
-      } while (s_avoidUdpSourcePorts.count(port));
+        port = g_minUdpSourcePort + dns_random(g_maxUdpSourcePort - g_minUdpSourcePort + 1);
+      } while (g_avoidUdpSourcePorts.count(port));
     }
 
     sin = pdns::getQueryLocalAddress(family, port); // does htons for us
@@ -1342,7 +1342,7 @@ void startDoResolve(void* p)
 
     if (haveEDNS) {
       auto state = sr.getValidationState();
-      if (dc->d_extendedErrorCode || (s_addExtendedResolutionDNSErrors && vStateIsBogus(state))) {
+      if (dc->d_extendedErrorCode || (g_addExtendedResolutionDNSErrors && vStateIsBogus(state))) {
         EDNSExtendedError::code code;
         std::string extra;
 
@@ -1757,7 +1757,7 @@ void requestWipeCaches(const DNSName& canon)
   ThreadMSG* tmsg = new ThreadMSG();
   tmsg->func = [=] { return pleaseWipeCaches(canon, true, 0xffff); };
   tmsg->wantAnswer = false;
-  if (write(s_threadInfos[0].pipes.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg)) {
+  if (write(g_threadInfos[0].pipes.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg)) {
     delete tmsg;
 
     unixDie("write to thread pipe returned wrong size or error");
@@ -1771,7 +1771,7 @@ bool expectProxyProtocol(const ComboAddress& from)
 
 static string* doProcessUDPQuestion(const std::string& question, const ComboAddress& fromaddr, const ComboAddress& destaddr, ComboAddress source, ComboAddress destination, struct timeval tv, int fd, std::vector<ProxyProtocolValue>& proxyProtocolValues, RecEventTrace& eventTrace)
 {
-  ++s_threadInfos[t_id].numberOfDistributedQueries;
+  ++g_threadInfos[t_id].numberOfDistributedQueries;
   gettimeofday(&g_now, nullptr);
   if (tv.tv_sec) {
     struct timeval diff = g_now - tv;
@@ -2042,7 +2042,7 @@ static void handleNewUDPQuestion(int fd, FDMultiplexer::funcparam_t& var)
   std::vector<ProxyProtocolValue> proxyProtocolValues;
   RecEventTrace eventTrace;
 
-  for (size_t queriesCounter = 0; queriesCounter < s_maxUDPQueriesPerRound; queriesCounter++) {
+  for (size_t queriesCounter = 0; queriesCounter < g_maxUDPQueriesPerRound; queriesCounter++) {
     bool proxyProto = false;
     proxyProtocolValues.clear();
     data.resize(maxIncomingQuerySize);
@@ -2323,7 +2323,7 @@ void makeUDPServerSockets(deferredAdd_t& deferredAdds)
 
 static bool trySendingQueryToWorker(unsigned int target, ThreadMSG* tmsg)
 {
-  auto& targetInfo = s_threadInfos[target];
+  auto& targetInfo = g_threadInfos[target];
   if (!targetInfo.isWorker) {
     g_log << Logger::Error << "distributeAsyncFunction() tried to assign a query to a non-worker thread" << endl;
     _exit(1);
@@ -2354,7 +2354,7 @@ static bool trySendingQueryToWorker(unsigned int target, ThreadMSG* tmsg)
 
 static unsigned int getWorkerLoad(size_t workerIdx)
 {
-  const auto mt = s_threadInfos[/* skip handler */ 1 + g_numDistributorThreads + workerIdx].mt;
+  const auto mt = g_threadInfos[/* skip handler */ 1 + g_numDistributorThreads + workerIdx].mt;
   if (mt != nullptr) {
     return mt->numProcesses();
   }
@@ -2363,7 +2363,7 @@ static unsigned int getWorkerLoad(size_t workerIdx)
 
 static unsigned int selectWorker(unsigned int hash)
 {
-  if (s_balancingFactor == 0) {
+  if (g_balancingFactor == 0) {
     return /* skip handler */ 1 + g_numDistributorThreads + (hash % g_numWorkerThreads);
   }
 
@@ -2376,7 +2376,7 @@ static unsigned int selectWorker(unsigned int hash)
     // cerr<<"load for worker "<<idx<<" is "<<load[idx]<<endl;
   }
 
-  double targetLoad = (currentLoad / g_numWorkerThreads) * s_balancingFactor;
+  double targetLoad = (currentLoad / g_numWorkerThreads) * g_balancingFactor;
   // cerr<<"total load is "<<currentLoad<<", number of workers is "<<g_numWorkerThreads<<", target load is "<<targetLoad<<endl;
 
   unsigned int worker = hash % g_numWorkerThreads;
index 8f325ae428fc023f1f3b503d50a76fb3942e3b27..0cac23188a5a7a39376b6044df36cdd0ccae5f3e 100644 (file)
@@ -1160,8 +1160,6 @@ static StatsMap toRPZStatsMap(const string& name, LockGuarded<std::unordered_map
   return entries;
 }
 
-extern ResponseStats g_rs;
-
 static void registerAllStats1()
 {
   addGetStat("questions", &g_stats.qcounter);
@@ -1445,12 +1443,10 @@ void registerAllStats()
 void doExitGeneric(bool nicely)
 {
   g_log << Logger::Error << "Exiting on user request" << endl;
-  extern RecursorControlChannel s_rcc;
-  s_rcc.~RecursorControlChannel();
+  g_rcc.~RecursorControlChannel();
 
-  extern string s_pidfname;
-  if (!s_pidfname.empty())
-    unlink(s_pidfname.c_str()); // we can at least try..
+  if (!g_pidfname.empty())
+    unlink(g_pidfname.c_str()); // we can at least try..
   if (nicely) {
     RecursorControlChannel::stop = true;
   }
index 0a5895f9272aacef17dc2ec93851e6e474b4f666..21f6f8b0f3dc21038a8b1c33e34db51d039a5ab8 100644 (file)
@@ -60,9 +60,9 @@ thread_local std::shared_ptr<std::vector<std::unique_ptr<FrameStreamLogger>>> t_
 thread_local uint64_t t_frameStreamServersGeneration;
 #endif /* HAVE_FSTRM */
 
-string s_programname = "pdns_recursor";
-string s_pidfname;
-RecursorControlChannel s_rcc; // only active in the handler thread
+string g_programname = "pdns_recursor";
+string g_pidfname;
+RecursorControlChannel g_rcc; // only active in the handler thread
 
 #ifdef NOD_ENABLED
 bool g_nodEnabled;
@@ -93,9 +93,9 @@ std::shared_ptr<notifyset_t> g_initialAllowNotifyFor; // new threads need this t
 bool g_logRPZChanges{false};
 unsigned int g_numDistributorThreads;
 unsigned int g_numThreads;
-static time_t g_statisticsInterval;
-bool s_addExtendedResolutionDNSErrors;
-static std::atomic<uint32_t> counter;
+static time_t s_statisticsInterval;
+bool g_addExtendedResolutionDNSErrors;
+static std::atomic<uint32_t> s_counter;
 int g_argc;
 char** g_argv;
 
@@ -106,7 +106,7 @@ deferredAdd_t g_deferredAdds;
    helper threads like SNMP might have t_id == 0 as well)
    then the distributor threads if any
    and finally the workers */
-std::vector<RecThreadInfo> s_threadInfos;
+std::vector<RecThreadInfo> g_threadInfos;
 
 ArgvMap& arg()
 {
@@ -383,11 +383,11 @@ bool checkFrameStreamExport(LocalStateHolder<LuaConfigItems>& luaconfsLocal)
 
 static void makeControlChannelSocket(int processNum = -1)
 {
-  string sockname = ::arg()["socket-dir"] + "/" + s_programname;
+  string sockname = ::arg()["socket-dir"] + "/" + g_programname;
   if (processNum >= 0)
     sockname += "." + std::to_string(processNum);
   sockname += ".controlsocket";
-  s_rcc.listen(sockname);
+  g_rcc.listen(sockname);
 
   int sockowner = -1;
   int sockgroup = -1;
@@ -416,12 +416,12 @@ static void writePid(void)
 {
   if (!::arg().mustDo("write-pid"))
     return;
-  ofstream of(s_pidfname.c_str(), std::ios_base::app);
+  ofstream of(g_pidfname.c_str(), std::ios_base::app);
   if (of)
     of << Utility::getpid() << endl;
   else {
     int err = errno;
-    g_log << Logger::Error << "Writing pid for " << Utility::getpid() << " to " << s_pidfname << " failed: "
+    g_log << Logger::Error << "Writing pid for " << Utility::getpid() << " to " << g_pidfname << " failed: "
           << stringerror(err) << endl;
   }
 }
@@ -625,6 +625,7 @@ static void checkLinuxIPv6Limits()
   }
 #endif
 }
+
 static void checkOrFixFDS()
 {
   unsigned int availFDs = getFilenumLimit();
@@ -645,6 +646,7 @@ static void checkOrFixFDS()
     }
   }
 }
+
 // static std::string s_timestampFormat = "%m-%dT%H:%M:%S";
 static std::string s_timestampFormat = "%s";
 
@@ -686,6 +688,7 @@ static void loggerBackend(const Logging::Entry& entry)
   Logger::Urgency u = entry.d_priority ? Logger::Urgency(entry.d_priority) : Logger::Info;
   g_log << u << buf.str() << endl;
 }
+
 void makeThreadPipes()
 {
   auto pipeBufferSize = ::arg().asNum("distribution-pipe-buffer-size");
@@ -695,7 +698,7 @@ void makeThreadPipes()
 
   /* thread 0 is the handler / SNMP, worker threads start at 1 */
   for (unsigned int n = 0; n <= (g_numWorkerThreads + g_numDistributorThreads); ++n) {
-    auto& threadInfos = s_threadInfos.at(n);
+    auto& threadInfos = g_threadInfos.at(n);
 
     int fd[2];
     if (pipe(fd) < 0)
@@ -779,7 +782,7 @@ static void doStats(void)
     g_log << Logger::Notice << "stats: " << pcSize << " packet cache entries, " << ratePercentage(pcHits, SyncRes::s_queries) << "% packet cache hits" << endl;
 
     size_t idx = 0;
-    for (const auto& threadInfo : s_threadInfos) {
+    for (const auto& threadInfo : g_threadInfos) {
       if (threadInfo.isWorker) {
         g_log << Logger::Notice << "stats: thread " << idx << " has been distributed " << threadInfo.numberOfDistributedQueries << " queries" << endl;
         ++idx;
@@ -933,9 +936,9 @@ void broadcastFunction(const pipefunc_t& func)
      for the initialization of ACLs and domain maps. After that it should only
      be called by the handler. */
 
-  if (s_threadInfos.empty() && isHandlerThread()) {
+  if (g_threadInfos.empty() && isHandlerThread()) {
     /* the handler and  distributors will call themselves below, but
-       during startup we get called while s_threadInfos has not been
+       during startup we get called while g_threadInfos has not been
        populated yet to update the ACL or domain maps, so we need to
        handle that case.
     */
@@ -943,7 +946,7 @@ void broadcastFunction(const pipefunc_t& func)
   }
 
   unsigned int n = 0;
-  for (const auto& threadInfo : s_threadInfos) {
+  for (const auto& threadInfo : g_threadInfos) {
     if (n++ == t_id) {
       func(); // don't write to ourselves!
       continue;
@@ -1001,7 +1004,7 @@ T broadcastAccFunction(const boost::function<T*()>& func)
 
   unsigned int n = 0;
   T ret = T();
-  for (const auto& threadInfo : s_threadInfos) {
+  for (const auto& threadInfo : g_threadInfos) {
     if (n++ == t_id) {
       continue;
     }
@@ -1042,7 +1045,7 @@ static int serviceMain(int argc, char* argv[])
 
   g_slogStructured = ::arg().mustDo("structured-logging");
 
-  g_log.setName(s_programname);
+  g_log.setName(g_programname);
   g_log.disableSyslog(::arg().mustDo("disable-syslog"));
   g_log.setTimestamps(::arg().mustDo("log-timestamp"));
 
@@ -1347,9 +1350,9 @@ static int serviceMain(int argc, char* argv[])
 
   g_gettagNeedsEDNSOptions = ::arg().mustDo("gettag-needs-edns-options");
 
-  g_statisticsInterval = ::arg().asNum("statistics-interval");
+  s_statisticsInterval = ::arg().asNum("statistics-interval");
 
-  s_addExtendedResolutionDNSErrors = ::arg().mustDo("extended-resolution-errors");
+  g_addExtendedResolutionDNSErrors = ::arg().mustDo("extended-resolution-errors");
 
   if (::arg().asNum("aggressive-nsec-cache-size") > 0) {
     if (g_dnssecmode == DNSSECMode::ValidateAll || g_dnssecmode == DNSSECMode::ValidateForLog || g_dnssecmode == DNSSECMode::Process) {
@@ -1412,9 +1415,9 @@ static int serviceMain(int argc, char* argv[])
     g_carbonConfig.setState(std::move(config));
   }
 
-  s_balancingFactor = ::arg().asDouble("distribution-load-factor");
-  if (s_balancingFactor != 0.0 && s_balancingFactor < 1.0) {
-    s_balancingFactor = 0.0;
+  g_balancingFactor = ::arg().asDouble("distribution-load-factor");
+  if (g_balancingFactor != 0.0 && g_balancingFactor < 1.0) {
+    g_balancingFactor = 0.0;
     g_log << Logger::Warning << "Asked to run with a distribution-load-factor below 1.0, disabling it instead" << endl;
   }
 
@@ -1422,14 +1425,14 @@ static int serviceMain(int argc, char* argv[])
   g_reusePort = ::arg().mustDo("reuseport");
 #endif
 
-  s_threadInfos.resize(g_numDistributorThreads + g_numWorkerThreads + /* handler */ 1);
+  g_threadInfos.resize(g_numDistributorThreads + g_numWorkerThreads + /* handler */ 1);
 
   if (g_reusePort) {
     if (g_weDistributeQueries) {
       /* first thread is the handler, then distributors */
       for (unsigned int threadId = 1; threadId <= g_numDistributorThreads; threadId++) {
-        auto& deferredAdds = s_threadInfos.at(threadId).deferredAdds;
-        auto& tcpSockets = s_threadInfos.at(threadId).tcpSockets;
+        auto& deferredAdds = g_threadInfos.at(threadId).deferredAdds;
+        auto& tcpSockets = g_threadInfos.at(threadId).tcpSockets;
         makeUDPServerSockets(deferredAdds);
         makeTCPServerSockets(deferredAdds, tcpSockets);
       }
@@ -1437,8 +1440,8 @@ static int serviceMain(int argc, char* argv[])
     else {
       /* first thread is the handler, there is no distributor here and workers are accepting queries */
       for (unsigned int threadId = 1; threadId <= g_numWorkerThreads; threadId++) {
-        auto& deferredAdds = s_threadInfos.at(threadId).deferredAdds;
-        auto& tcpSockets = s_threadInfos.at(threadId).tcpSockets;
+        auto& deferredAdds = g_threadInfos.at(threadId).deferredAdds;
+        auto& tcpSockets = g_threadInfos.at(threadId).tcpSockets;
         makeUDPServerSockets(deferredAdds);
         makeTCPServerSockets(deferredAdds, tcpSockets);
       }
@@ -1456,13 +1459,13 @@ static int serviceMain(int argc, char* argv[])
     if (g_weDistributeQueries) {
       /* first thread is the handler, then distributors */
       for (unsigned int threadId = 1; threadId <= g_numDistributorThreads; threadId++) {
-        s_threadInfos.at(threadId).tcpSockets = tcpSockets;
+        g_threadInfos.at(threadId).tcpSockets = tcpSockets;
       }
     }
     else {
       /* first thread is the handler, there is no distributor here and workers are accepting queries */
       for (unsigned int threadId = 1; threadId <= g_numWorkerThreads; threadId++) {
-        s_threadInfos.at(threadId).tcpSockets = tcpSockets;
+        g_threadInfos.at(threadId).tcpSockets = tcpSockets;
       }
     }
   }
@@ -1552,9 +1555,9 @@ static int serviceMain(int argc, char* argv[])
 
   checkSocketDir();
 
-  s_pidfname = ::arg()["socket-dir"] + "/" + s_programname + ".pid";
-  if (!s_pidfname.empty())
-    unlink(s_pidfname.c_str()); // remove possible old pid file
+  g_pidfname = ::arg()["socket-dir"] + "/" + g_programname + ".pid";
+  if (!g_pidfname.empty())
+    unlink(g_pidfname.c_str()); // remove possible old pid file
   writePid();
 
   makeControlChannelSocket(::arg().asNum("processes") > 1 ? forks : -1);
@@ -1580,7 +1583,7 @@ static int serviceMain(int argc, char* argv[])
   g_tcpTimeout = ::arg().asNum("client-tcp-timeout");
   g_maxTCPPerClient = ::arg().asNum("max-tcp-per-client");
   g_tcpMaxQueriesPerConn = ::arg().asNum("max-tcp-queries-per-connection");
-  s_maxUDPQueriesPerRound = ::arg().asNum("max-udp-queries-per-round");
+  g_maxUDPQueriesPerRound = ::arg().asNum("max-udp-queries-per-round");
 
   g_useKernelTimestamp = ::arg().mustDo("protobuf-use-kernel-timestamp");
 
@@ -1608,13 +1611,13 @@ static int serviceMain(int argc, char* argv[])
     g_log << Logger::Error << "Unable to launch, udp-source-port-min is not a valid port number" << endl;
     exit(99); // this isn't going to fix itself either
   }
-  s_minUdpSourcePort = port;
+  g_minUdpSourcePort = port;
   port = ::arg().asNum("udp-source-port-max");
-  if (port < 1024 || port > 65535 || port < s_minUdpSourcePort) {
+  if (port < 1024 || port > 65535 || port < g_minUdpSourcePort) {
     g_log << Logger::Error << "Unable to launch, udp-source-port-max is not a valid port number or is smaller than udp-source-port-min" << endl;
     exit(99); // this isn't going to fix itself either
   }
-  s_maxUdpSourcePort = port;
+  g_maxUdpSourcePort = port;
   std::vector<string> parts{};
   stringtok(parts, ::arg()["udp-source-port-avoid"], ", ");
   for (const auto& part : parts) {
@@ -1623,7 +1626,7 @@ static int serviceMain(int argc, char* argv[])
       g_log << Logger::Error << "Unable to launch, udp-source-port-avoid contains an invalid port number: " << part << endl;
       exit(99); // this isn't going to fix itself either
     }
-    s_avoidUdpSourcePorts.insert(port);
+    g_avoidUdpSourcePorts.insert(port);
   }
 
   unsigned int currentThreadId = 1;
@@ -1636,13 +1639,13 @@ static int serviceMain(int argc, char* argv[])
 #endif
 
     /* This thread handles the web server, carbon, statistics and the control channel */
-    auto& handlerInfos = s_threadInfos.at(0);
+    auto& handlerInfos = g_threadInfos.at(0);
     handlerInfos.isHandler = true;
     handlerInfos.thread = std::thread(recursorThread, 0, "web+stat");
 
     setCPUMap(cpusMap, currentThreadId, pthread_self());
 
-    auto& infos = s_threadInfos.at(currentThreadId);
+    auto& infos = g_threadInfos.at(currentThreadId);
     infos.isListener = true;
     infos.isWorker = true;
     recursorThread(currentThreadId++, "worker");
@@ -1656,12 +1659,12 @@ static int serviceMain(int argc, char* argv[])
 
     if (g_weDistributeQueries) {
       for (unsigned int n = 0; n < g_numDistributorThreads; ++n) {
-        auto& infos = s_threadInfos.at(currentThreadId + n);
+        auto& infos = g_threadInfos.at(currentThreadId + n);
         infos.isListener = true;
       }
     }
     for (unsigned int n = 0; n < g_numWorkerThreads; ++n) {
-      auto& infos = s_threadInfos.at(currentThreadId + (g_weDistributeQueries ? g_numDistributorThreads : 0) + n);
+      auto& infos = g_threadInfos.at(currentThreadId + (g_weDistributeQueries ? g_numDistributorThreads : 0) + n);
       infos.isListener = !g_weDistributeQueries;
       infos.isWorker = true;
     }
@@ -1669,7 +1672,7 @@ static int serviceMain(int argc, char* argv[])
     if (g_weDistributeQueries) {
       g_log << Logger::Warning << "Launching " << g_numDistributorThreads << " distributor threads" << endl;
       for (unsigned int n = 0; n < g_numDistributorThreads; ++n) {
-        auto& infos = s_threadInfos.at(currentThreadId);
+        auto& infos = g_threadInfos.at(currentThreadId);
         infos.thread = std::thread(recursorThread, currentThreadId++, "distr");
         setCPUMap(cpusMap, currentThreadId, infos.thread.native_handle());
       }
@@ -1678,7 +1681,7 @@ static int serviceMain(int argc, char* argv[])
     g_log << Logger::Warning << "Launching " << g_numWorkerThreads << " worker threads" << endl;
 
     for (unsigned int n = 0; n < g_numWorkerThreads; ++n) {
-      auto& infos = s_threadInfos.at(currentThreadId);
+      auto& infos = g_threadInfos.at(currentThreadId);
       infos.thread = std::thread(recursorThread, currentThreadId++, "worker");
       setCPUMap(cpusMap, currentThreadId, infos.thread.native_handle());
     }
@@ -1688,11 +1691,11 @@ static int serviceMain(int argc, char* argv[])
 #endif
 
     /* This thread handles the web server, carbon, statistics and the control channel */
-    auto& infos = s_threadInfos.at(0);
+    auto& infos = g_threadInfos.at(0);
     infos.isHandler = true;
     infos.thread = std::thread(recursorThread, 0, "web+stat");
 
-    for (auto& ti : s_threadInfos) {
+    for (auto& ti : g_threadInfos) {
       ti.thread.join();
       if (ti.exitCode != 0) {
         ret = ti.exitCode;
@@ -1724,7 +1727,7 @@ static void handlePipeRequest(int fd, FDMultiplexer::funcparam_t& var)
       g_log << Logger::Error << "PIPE function we executed created PDNS exception: " << e.reason << endl; // but what if they wanted an answer.. we send 0
   }
   if (tmsg->wantAnswer) {
-    const auto& threadInfo = s_threadInfos.at(t_id);
+    const auto& threadInfo = g_threadInfos.at(t_id);
     if (write(threadInfo.pipes.writeFromThread, &resp, sizeof(resp)) != sizeof(resp)) {
       delete tmsg;
       unixDie("write to thread pipe returned wrong size or error");
@@ -1741,14 +1744,14 @@ static void handleRCC(int fd, FDMultiplexer::funcparam_t& var)
     if (clientfd == -1) {
       throw PDNSException("accept failed");
     }
-    string msg = s_rcc.recv(clientfd).d_str;
+    string msg = g_rcc.recv(clientfd).d_str;
     g_log << Logger::Info << "Received rec_control command '" << msg << "' via controlsocket" << endl;
 
     RecursorControlParser rcp;
     RecursorControlParser::func_t* command;
     auto answer = rcp.getAnswer(clientfd, msg, &command);
 
-    s_rcc.send(clientfd, answer);
+    g_rcc.send(clientfd, answer);
     command();
   }
   catch (const std::exception& e) {
@@ -1758,26 +1761,27 @@ static void handleRCC(int fd, FDMultiplexer::funcparam_t& var)
     g_log << Logger::Error << "Error dealing with control socket request: " << ae.reason << endl;
   }
 }
+
 static void houseKeeping(void*)
 {
-  static thread_local time_t last_rootupdate, last_secpoll, last_trustAnchorUpdate{0};
-  static thread_local struct timeval last_prune;
+  static thread_local time_t t_last_rootupdate, t_last_secpoll, t_last_trustAnchorUpdate{0};
+  static thread_local struct timeval t_last_prune;
 
-  static thread_local int cleanCounter = 0;
-  static thread_local bool s_running; // houseKeeping can get suspended in secpoll, and be restarted, which makes us do duplicate work
-  static time_t last_RC_prune = 0;
+  static thread_local int t_cleanCounter = 0;
+  static thread_local bool t_running; // houseKeeping can get suspended in secpoll, and be restarted, which makes us do duplicate work
+  static time_t s_last_RC_prune = 0;
   auto luaconfsLocal = g_luaconfs.getLocal();
 
-  if (last_trustAnchorUpdate == 0 && !luaconfsLocal->trustAnchorFileInfo.fname.empty() && luaconfsLocal->trustAnchorFileInfo.interval != 0) {
+  if (t_last_trustAnchorUpdate == 0 && !luaconfsLocal->trustAnchorFileInfo.fname.empty() && luaconfsLocal->trustAnchorFileInfo.interval != 0) {
     // Loading the Lua config file already "refreshed" the TAs
-    last_trustAnchorUpdate = g_now.tv_sec + luaconfsLocal->trustAnchorFileInfo.interval * 3600;
+    t_last_trustAnchorUpdate = g_now.tv_sec + luaconfsLocal->trustAnchorFileInfo.interval * 3600;
   }
 
   try {
-    if (s_running) {
+    if (t_running) {
       return;
     }
-    s_running = true;
+    t_running = true;
 
     runTaskOnce(g_logCommonErrors);
 
@@ -1785,11 +1789,11 @@ static void houseKeeping(void*)
     Utility::gettimeofday(&now, nullptr);
     past = now;
     past.tv_sec -= 5;
-    if (last_prune < past) {
+    if (t_last_prune < past) {
       t_packetCache->doPruneTo(g_maxPacketCacheEntries / (g_numWorkerThreads + g_numDistributorThreads));
 
       time_t limit;
-      if (!((cleanCounter++) % 40)) { // this is a full scan!
+      if (!((t_cleanCounter++) % 40)) { // this is a full scan!
         limit = now.tv_sec - 300;
         SyncRes::pruneNSSpeeds(limit);
       }
@@ -1799,24 +1803,24 @@ static void houseKeeping(void*)
       SyncRes::pruneEDNSStatuses(limit);
       SyncRes::pruneThrottledServers();
       SyncRes::pruneNonResolving(now.tv_sec - SyncRes::s_nonresolvingnsthrottletime);
-      Utility::gettimeofday(&last_prune, nullptr);
+      Utility::gettimeofday(&t_last_prune, nullptr);
       t_tcp_manager.cleanup(now);
     }
 
     if (isHandlerThread()) {
-      if (now.tv_sec - last_RC_prune > 5) {
+      if (now.tv_sec - s_last_RC_prune > 5) {
         g_recCache->doPrune(g_maxCacheEntries);
         g_negCache->prune(g_maxCacheEntries / 10);
         if (g_aggressiveNSECCache) {
           g_aggressiveNSECCache->prune(now.tv_sec);
         }
-        last_RC_prune = now.tv_sec;
+        s_last_RC_prune = now.tv_sec;
       }
       // Divide by 12 to get the original 2 hour cycle if s_maxcachettl is default (1 day)
-      if (now.tv_sec - last_rootupdate > max(SyncRes::s_maxcachettl / 12, 10U)) {
+      if (now.tv_sec - t_last_rootupdate > max(SyncRes::s_maxcachettl / 12, 10U)) {
         int res = SyncRes::getRootNS(g_now, nullptr, 0);
         if (!res) {
-          last_rootupdate = now.tv_sec;
+          t_last_rootupdate = now.tv_sec;
           try {
             primeRootNSZones(g_dnssecmode != DNSSECMode::Off, 0);
           }
@@ -1838,9 +1842,9 @@ static void houseKeeping(void*)
         }
       }
 
-      if (now.tv_sec - last_secpoll >= 3600) {
+      if (now.tv_sec - t_last_secpoll >= 3600) {
         try {
-          doSecPoll(&last_secpoll);
+          doSecPoll(&t_last_secpoll);
         }
         catch (const std::exception& e) {
           g_log << Logger::Error << "Exception while performing security poll: " << e.what() << endl;
@@ -1859,7 +1863,7 @@ static void houseKeeping(void*)
         }
       }
 
-      if (!luaconfsLocal->trustAnchorFileInfo.fname.empty() && luaconfsLocal->trustAnchorFileInfo.interval != 0 && g_now.tv_sec - last_trustAnchorUpdate >= (luaconfsLocal->trustAnchorFileInfo.interval * 3600)) {
+      if (!luaconfsLocal->trustAnchorFileInfo.fname.empty() && luaconfsLocal->trustAnchorFileInfo.interval != 0 && g_now.tv_sec - t_last_trustAnchorUpdate >= (luaconfsLocal->trustAnchorFileInfo.interval * 3600)) {
         g_log << Logger::Debug << "Refreshing Trust Anchors from file" << endl;
         try {
           map<DNSName, dsmap_t> dsAnchors;
@@ -1868,22 +1872,22 @@ static void houseKeeping(void*)
               lci.dsAnchors = dsAnchors;
             });
           }
-          last_trustAnchorUpdate = now.tv_sec;
+          t_last_trustAnchorUpdate = now.tv_sec;
         }
         catch (const PDNSException& pe) {
           g_log << Logger::Error << "Unable to update Trust Anchors: " << pe.reason << endl;
         }
       }
     }
-    s_running = false;
+    t_running = false;
   }
   catch (const PDNSException& ae) {
-    s_running = false;
+    t_running = false;
     g_log << Logger::Error << "Fatal error in housekeeping thread: " << ae.reason << endl;
     throw;
   }
   catch (...) {
-    s_running = false;
+    t_running = false;
     g_log << Logger::Error << "Uncaught exception in housekeeping thread" << endl;
     throw;
   }
@@ -1892,7 +1896,7 @@ static void houseKeeping(void*)
 void* recursorThread(unsigned int n, const string& threadName)
 try {
   t_id = n;
-  auto& threadInfo = s_threadInfos.at(t_id);
+  auto& threadInfo = g_threadInfos.at(t_id);
 
   static string threadPrefix = "pdns-r/";
   setThreadName(threadPrefix + threadName);
@@ -2014,7 +2018,7 @@ try {
   registerAllStats();
 
   if (threadInfo.isHandler) {
-    t_fdm->addReadFD(s_rcc.d_fd, handleRCC); // control channel
+    t_fdm->addReadFD(g_rcc.d_fd, handleRCC); // control channel
   }
 
   unsigned int maxTcpClients = ::arg().asNum("max-tcp-clients");
@@ -2025,7 +2029,7 @@ try {
   time_t last_carbon = 0, last_lua_maintenance = 0;
   time_t carbonInterval = ::arg().asNum("carbon-interval");
   time_t luaMaintenanceInterval = ::arg().asNum("lua-maintenance-interval");
-  counter.store(0); // used to periodically execute certain tasks
+  s_counter.store(0); // used to periodically execute certain tasks
 
   while (!RecursorControlChannel::stop) {
     while (MT->schedule(&g_now))
@@ -2033,11 +2037,11 @@ try {
 
     // Use primes, it avoid not being scheduled in cases where the counter has a regular pattern.
     // We want to call handler thread often, it gets scheduled about 2 times per second
-    if ((threadInfo.isHandler && counter % 11 == 0) || counter % 499 == 0) {
+    if ((threadInfo.isHandler && s_counter % 11 == 0) || s_counter % 499 == 0) {
       MT->makeThread(houseKeeping, 0);
     }
 
-    if (!(counter % 55)) {
+    if (!(s_counter % 55)) {
       typedef vector<pair<int, FDMultiplexer::funcparam_t>> expired_t;
       expired_t expired = t_fdm->getTimeouts(g_now);
 
@@ -2049,10 +2053,10 @@ try {
       }
     }
 
-    counter++;
+    s_counter++;
 
     if (threadInfo.isHandler) {
-      if (statsWanted || (g_statisticsInterval > 0 && (g_now.tv_sec - last_stat) >= g_statisticsInterval)) {
+      if (statsWanted || (s_statisticsInterval > 0 && (g_now.tv_sec - last_stat) >= s_statisticsInterval)) {
         doStats();
         last_stat = g_now.tv_sec;
       }
@@ -2392,7 +2396,7 @@ int main(int argc, char** argv)
     string configname = ::arg()["config-dir"] + "/recursor.conf";
     if (::arg()["config-name"] != "") {
       configname = ::arg()["config-dir"] + "/recursor-" + ::arg()["config-name"] + ".conf";
-      s_programname += "-" + ::arg()["config-name"];
+      g_programname += "-" + ::arg()["config-name"];
     }
     cleanSlashes(configname);
 
index 2e86539d2b3e19aa53ccbada2caaa2fd85579e6a..a5328bd35a95ac5076815e1bb9c24f5b233494f2 100644 (file)
@@ -34,6 +34,7 @@
 #include "rec-protozero.hh"
 #include "syncres.hh"
 #include "rec-snmp.hh"
+#include "rec_channel.hh"
 
 #ifdef NOD_ENABLED
 #include "nod.hh"
@@ -138,8 +139,8 @@ struct DNSComboWriter
 };
 
 extern thread_local FDMultiplexer* t_fdm;
-extern uint16_t s_minUdpSourcePort;
-extern uint16_t s_maxUdpSourcePort;
+extern uint16_t g_minUdpSourcePort;
+extern uint16_t g_maxUdpSourcePort;
 
 // you can ask this class for a UDP socket to send a query from
 // this socket is not yours, don't even think about deleting it
@@ -191,8 +192,8 @@ extern size_t g_tcpMaxQueriesPerConn;
 extern unsigned int g_maxTCPPerClient;
 extern int g_tcpTimeout;
 extern uint16_t g_udpTruncationThreshold;
-extern double s_balancingFactor;
-extern size_t s_maxUDPQueriesPerRound;
+extern double g_balancingFactor;
+extern size_t g_maxUDPQueriesPerRound;
 extern bool g_useKernelTimestamp;
 extern thread_local std::shared_ptr<NetmaskGroup> t_allowFrom;
 extern thread_local std::shared_ptr<NetmaskGroup> t_allowNotifyFrom;
@@ -203,10 +204,10 @@ extern bool g_useIncomingECS;
 extern boost::optional<ComboAddress> g_dns64Prefix;
 extern DNSName g_dns64PrefixReverse;
 extern uint64_t g_latencyStatSize;
-extern bool s_addExtendedResolutionDNSErrors;
+extern bool g_addExtendedResolutionDNSErrors;
 extern uint16_t g_xpfRRCode;
 extern NetmaskGroup g_proxyProtocolACL;
-extern std::atomic<bool> statsWanted;
+extern std::atomic<bool> g_statsWanted;
 extern unsigned int g_numDistributorThreads;
 extern unsigned int g_numWorkerThreads;
 extern uint32_t g_disthashseed;
@@ -217,6 +218,9 @@ extern std::shared_ptr<NetmaskGroup> g_initialAllowFrom; // new thread needs to
 extern std::shared_ptr<NetmaskGroup> g_initialAllowNotifyFrom; // new threads need this to be setup
 extern std::shared_ptr<notifyset_t> g_initialAllowNotifyFor; // new threads need this to be setup
 extern thread_local std::shared_ptr<Regex> t_traceRegex;
+extern string g_programname;
+extern string g_pidfname;
+extern RecursorControlChannel g_rcc; // only active in the handler thread
 
 #ifdef NOD_ENABLED
 extern bool g_nodEnabled;
@@ -237,9 +241,9 @@ extern thread_local uint64_t t_frameStreamServersGeneration;
 #endif /* HAVE_FSTRM */
 
 #ifdef HAVE_BOOST_CONTAINER_FLAT_SET_HPP
-extern boost::container::flat_set<uint16_t> s_avoidUdpSourcePorts;
+extern boost::container::flat_set<uint16_t> g_avoidUdpSourcePorts;
 #else
-extern std::set<uint16_t> s_avoidUdpSourcePorts;
+extern std::set<uint16_t> g_avoidUdpSourcePorts;
 #endif
 
 /* without reuseport, all listeners share the same sockets */
@@ -341,7 +345,7 @@ struct ThreadMSG
    helper threads like SNMP might have t_id == 0 as well)
    then the distributor threads if any
    and finally the workers */
-extern std::vector<RecThreadInfo> s_threadInfos;
+extern std::vector<RecThreadInfo> g_threadInfos;
 
 inline bool isDistributorThread()
 {
@@ -349,7 +353,7 @@ inline bool isDistributorThread()
     return false;
   }
 
-  return g_weDistributeQueries && s_threadInfos.at(t_id).isListener;
+  return g_weDistributeQueries && g_threadInfos.at(t_id).isListener;
 }
 
 inline bool isHandlerThread()
@@ -358,7 +362,7 @@ inline bool isHandlerThread()
     return true;
   }
 
-  return s_threadInfos.at(t_id).isHandler;
+  return g_threadInfos.at(t_id).isHandler;
 }
 
 PacketBuffer GenUDPQueryResponse(const ComboAddress& dest, const string& query);
index d369b9c15a6ddfa646b6f316252448081cea0955..14fd9ae1cef251b075d37bef3ae225a2715541dd 100644 (file)
@@ -33,9 +33,7 @@ int g_tcpTimeout;
 bool g_anyToTcp;
 
 uint16_t TCPConnection::s_maxInFlight;
-std::atomic<uint32_t> TCPConnection::s_currentConnections;
 
-typedef map<ComboAddress, uint32_t, ComboAddress::addressOnlyLessThan> tcpClientCounts_t;
 thread_local std::unique_ptr<tcpClientCounts_t> t_tcpClientCounts;
 
 static void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var);
@@ -49,6 +47,8 @@ static void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var);
 #define TCPLOG(pid, x)
 #endif
 
+std::atomic<uint32_t> TCPConnection::s_currentConnections;
+
 TCPConnection::TCPConnection(int fd, const ComboAddress& addr) :
   data(2, 0), d_remote(addr), d_fd(fd)
 {