]> granicus.if.org Git - pdns/commitdiff
Move thread name setting to a single point
authorPieter Lexis <pieter.lexis@powerdns.com>
Thu, 27 Sep 2018 15:08:38 +0000 (17:08 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Fri, 28 Sep 2018 09:02:45 +0000 (11:02 +0200)
38 files changed:
configure.ac
m4/ac_pthread_set_name.m4 [new file with mode: 0644]
pdns/Makefile.am
pdns/auth-carbon.cc
pdns/common_startup.cc
pdns/communicator.cc
pdns/delaypipe.cc
pdns/distributor.hh
pdns/dnsdist-carbon.cc
pdns/dnsdist-console.cc
pdns/dnsdist-lua-actions.cc
pdns/dnsdist-tcp.cc
pdns/dnsdist-web.cc
pdns/dnsdist.cc
pdns/dnsdistdist/Makefile.am
pdns/dnsdistdist/configure.ac
pdns/dnsdistdist/m4/ac_pthread_set_name.m4 [new symlink]
pdns/dnsdistdist/threadname.cc [new symlink]
pdns/dnsdistdist/threadname.hh [new symlink]
pdns/dnsproxy.cc
pdns/dnstcpbench.cc
pdns/dynlistener.cc
pdns/ixfrdist.cc
pdns/nod.cc
pdns/pdns_recursor.cc
pdns/recursordist/Makefile.am
pdns/recursordist/configure.ac
pdns/recursordist/m4/ac_pthread_set_name.m4 [new symlink]
pdns/recursordist/threadname.cc [new symlink]
pdns/recursordist/threadname.hh [new symlink]
pdns/remote_logger.cc
pdns/rpzloader.cc
pdns/snmp-agent.cc
pdns/tcpreceiver.cc
pdns/threadname.cc [new file with mode: 0644]
pdns/threadname.hh [new file with mode: 0644]
pdns/webserver.cc
pdns/ws-auth.cc

index 3317d0d4fe2b8a9d5a69ba3d32d328affa215477..e9b2a17a00a92467f38c6d848eb797d6aee0590d 100644 (file)
@@ -42,6 +42,7 @@ AC_DEFINE([_GNU_SOURCE], [1],
 m4_pattern_forbid([^_?PKG_[A-Z_]+$], [*** pkg.m4 missing, please install pkg-config])
 
 PDNS_CHECK_OS
+PTHREAD_SET_NAME
 
 PDNS_WITH_LUA
 PDNS_CHECK_LUA_HPP
diff --git a/m4/ac_pthread_set_name.m4 b/m4/ac_pthread_set_name.m4
new file mode 100644 (file)
index 0000000..4b2cb23
--- /dev/null
@@ -0,0 +1,71 @@
+# PTHREAD_SET_NAME();
+# Check which variant (if any) of pthread_set_name_np we have.
+# -----------------------------------------------------------------------------
+AC_DEFUN([PTHREAD_SET_NAME],
+[
+  stored_LIBS="$LIBS"
+  LIBS="-lpthread"
+  # pthread setname (4 non-portable variants...)
+  AC_CHECK_HEADERS([pthread_np.h], [], [], [#include <pthread.h>])
+  define(pthread_np_preamble,[
+    #include <pthread.h>
+    #if HAVE_PTHREAD_NP_H
+    #  include <pthread_np.h>
+    #endif
+  ])
+  # 2-arg setname (e.g. Linux/glibc, QNX, IBM)
+  AC_MSG_CHECKING([for 2-arg pthread_setname_np])
+  AC_LINK_IFELSE([AC_LANG_PROGRAM(pthread_np_preamble, [
+      pthread_setname_np(pthread_self(), "foo")
+  ])], [
+    AC_DEFINE(HAVE_PTHREAD_SETNAME_NP_2, 1, [2-arg pthread_setname_np])
+    AC_MSG_RESULT([yes])
+  ], [
+    AC_MSG_RESULT([no])
+
+    # 2-arg set_name (e.g. FreeBSD, OpenBSD)
+    AC_MSG_CHECKING([for 2-arg pthread_set_name_np])
+    AC_LINK_IFELSE([AC_LANG_PROGRAM(pthread_np_preamble, [
+        return pthread_set_name_np(pthread_self(), "foo");
+    ])], [
+      AC_DEFINE(HAVE_PTHREAD_SET_NAME_NP_2, 1, [2-arg pthread_set_name_np])
+      AC_MSG_RESULT([yes])
+    ], [
+      AC_MSG_RESULT([no])
+
+      # 2-arg void set_name (e.g. FreeBSD, OpenBSD)
+      AC_MSG_CHECKING([for 2-arg void pthread_set_name_np])
+      AC_LINK_IFELSE([AC_LANG_PROGRAM(pthread_np_preamble, [
+          pthread_set_name_np(pthread_self(), "foo");
+      ])], [
+        AC_DEFINE(HAVE_PTHREAD_SET_NAME_NP_2_VOID, 1, [2-arg void pthread_set_name_np])
+        AC_MSG_RESULT([yes])
+      ], [
+        AC_MSG_RESULT([no])
+
+        # 1-arg setname (e.g. Darwin)
+        AC_MSG_CHECKING([for 1-arg pthread_setname_np])
+        AC_LINK_IFELSE([AC_LANG_PROGRAM(pthread_np_preamble, [
+            return pthread_setname_np("foo");
+        ])], [
+          AC_DEFINE(HAVE_PTHREAD_SETNAME_NP_1, 1, [1-arg pthread_setname_np])
+          AC_MSG_RESULT([yes])
+        ], [
+          AC_MSG_RESULT([no])
+
+          # 3-arg setname (e.g. NetBSD)
+          AC_MSG_CHECKING([for 3-arg pthread_setname_np])
+          AC_LINK_IFELSE([AC_LANG_PROGRAM(pthread_np_preamble, [
+              return pthread_setname_np(pthread_self(), "foo", NULL);
+          ])], [
+            AC_DEFINE(HAVE_PTHREAD_SETNAME_NP_3, 1, [3-arg pthread_setname_np])
+            AC_MSG_RESULT([yes])
+          ], [
+            AC_MSG_RESULT([no])
+          ])
+        ])
+      ])
+    ])
+  ])
+  LIBS=$stored_LIBS
+])
index dfb1940859ee6f9605c166ec45180cc8eab073fa..05dbd8adbf4dca59c35fc6807b5c041c50988254 100644 (file)
@@ -219,6 +219,7 @@ pdns_server_SOURCES = \
        statbag.cc statbag.hh \
        stubresolver.cc stubresolver.hh \
        tcpreceiver.cc tcpreceiver.hh \
+       threadname.hh threadname.cc \
        tsigverifier.cc tsigverifier.hh \
        tkey.cc \
        ueberbackend.cc ueberbackend.hh \
@@ -328,6 +329,7 @@ pdnsutil_SOURCES = \
        sstuff.hh \
        statbag.cc \
        stubresolver.cc stubresolver.hh \
+       threadname.hh threadname.cc \
        ueberbackend.cc \
        unix_utility.cc \
        zoneparser-tng.cc
@@ -632,6 +634,7 @@ ixfrdist_SOURCES = \
        sillyrecords.cc \
        sstuff.hh \
        statbag.cc \
+       threadname.hh threadname.cc \
        tsigverifier.cc tsigverifier.hh \
        unix_utility.cc zoneparser-tng.cc
 
@@ -715,6 +718,7 @@ dnstcpbench_SOURCES = \
        sillyrecords.cc \
        sstuff.hh \
        statbag.cc \
+       threadname.hh threadname.cc \
        unix_utility.cc
 
 dnstcpbench_LDFLAGS = \
index f27e6ddb9020bdc762392bc7b66587ef5b878bde..325bac326e71bbd7656d8edadf2ce4aa4183f103 100644 (file)
@@ -24,6 +24,7 @@
 #endif
 #include "statbag.hh"
 #include "logger.hh"
+#include "threadname.hh"
 #include "iputils.hh"
 #include "sstuff.hh"
 #include "arguments.hh"
 void* carbonDumpThread(void*)
 try
 {
-  string threadName = "pdns/carbonDump";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for Carbon dump thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/carbonDump");
   extern StatBag S;
 
   string hostname=arg()["carbon-ourname"];
index 892cd963d7d327f967373dd3bbe152b3b9a17667..e4270e290107e727e364429aa8c91565ea3eac46 100644 (file)
@@ -29,6 +29,7 @@
 #include <sys/resource.h>
 #include "dynhandler.hh"
 #include "dnsseckeeper.hh"
+#include "threadname.hh"
 
 #ifdef HAVE_SYSTEMD
 #include <systemd/sd-daemon.h>
@@ -367,11 +368,7 @@ void sendout(DNSPacket* a)
 void *qthread(void *number)
 try
 {
-  string threadName = "pdns/receiver";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for receiver thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/receiver");
 
   DNSPacket *P;
   DNSDistributor *distributor = DNSDistributor::Create(::arg().asNum("distributor-threads", 1)); // the big dispatcher!
index 52f1aafdb85e211aae86b40396c50d89da81c4b8..709b8d05fa449850ee53409207f7a9064db17541 100644 (file)
 #include "dns.hh"
 #include "arguments.hh"
 #include "packetcache.hh"
+#include "threadname.hh"
 
 // there can be MANY OF THESE
 void CommunicatorClass::retrievalLoopThread(void)
 {
-  string threadName = "pdns/comm-retre";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for retrieval thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/comm-retre");
   for(;;) {
     d_suck_sem.wait();
     SuckRequest sr;
@@ -109,12 +106,7 @@ void CommunicatorClass::go()
 void CommunicatorClass::mainloop(void)
 {
   try {
-    string threadName = "pdns/comm-main";
-    auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-    if (retval != 0) {
-      g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for communicator main thread: "<<strerror(retval)<<endl;
-    }
-
+    setThreadName("pdns/comm-main");
     signal(SIGPIPE,SIG_IGN);
     g_log<<Logger::Error<<"Master/slave communicator launching"<<endl;
     PacketHandler P;
index e4e557bc7b9f83fee7b04a52500fba43ad43a8de..076b104830b8ccc457a9ffa2b839b34d30cb2a8e 100644 (file)
@@ -23,6 +23,7 @@
 #include "misc.hh"
 #include "gettime.hh"
 #include <thread>
+#include "threadname.hh"
 #include "dolog.hh"
 
 template<class T>
@@ -139,11 +140,7 @@ DelayPipe<T>::~DelayPipe()
 template<class T>
 void DelayPipe<T>::worker()
 {
-  string threadName = "dnsdist/delayPi";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for DelayPipe worker thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/delayPi");
   Combo c;
   for(;;) {
     /* this code is slightly too subtle, but I don't see how it could be any simpler.
index 2abd21684e1fec64c616a8c351bd030fb27646ac..cf005127ca12d671f7ce4564befe7e7fe6f3890e 100644 (file)
@@ -27,6 +27,7 @@
 #include <queue>
 #include <vector>
 #include <pthread.h>
+#include "threadname.hh"
 #include <unistd.h>
 #include "logger.hh"
 #include "dns.hh"
@@ -173,11 +174,7 @@ template<class Answer, class Question, class Backend>MultiThreadDistributor<Answ
 // start of a new thread
 template<class Answer, class Question, class Backend>void *MultiThreadDistributor<Answer,Question,Backend>::makeThread(void *p)
 {
-  string threadName = "pdns/distributo";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for distributor thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/distributo");
   pthread_detach(pthread_self());
   MultiThreadDistributor *us=static_cast<MultiThreadDistributor *>(p);
   int ournum=us->d_running++;
index c880289d50407f2bc511e22ce988ef90bbc3eb5d..da1e0fd7df2cc08b08c4fa19efb039d9cebf634b 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "namespaces.hh"
 #include "dnsdist.hh"
+#include "threadname.hh"
 
 GlobalStateHolder<vector<CarbonConfig> > g_carbon;
 static time_t s_start=time(0);
@@ -39,11 +40,7 @@ uint64_t uptimeOfProcess(const std::string& str)
 void* carbonDumpThread()
 try
 {
-  string threadName = "dnsdist/carbon";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for carbon thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/carbon");
   auto localCarbon = g_carbon.getLocal();
   for(int numloops=0;;++numloops) {
     if(localCarbon->empty()) {
index c9fe6f4f97c40481215e455a67b725737b42d6c2..1a8419abe06a26fea088b96f9e0dbd54a4112821 100644 (file)
@@ -37,6 +37,7 @@
 #include "dnsdist.hh"
 #include "dnsdist-console.hh"
 #include "sodcrypto.hh"
+#include "threadname.hh"
 
 GlobalStateHolder<NetmaskGroup> g_consoleACL;
 vector<pair<struct timeval, string> > g_confDelta;
@@ -535,11 +536,7 @@ char** my_completion( const char * text , int start,  int end)
 static void controlClientThread(int fd, ComboAddress client)
 try
 {
-  string threadname = "dnsdist/conscli";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadname.c_str()));
-  if (retval != 0) {
-    warnlog("could not set thread name %s for control client thread: %s", threadname, strerror(retval));
-  }
+  setThreadName("dnsdist/conscli");
   setTCPNoDelay(fd);
   SodiumNonce theirs, ours, readingNonce, writingNonce;
   ours.init();
@@ -670,11 +667,7 @@ catch(std::exception& e)
 void controlThread(int fd, ComboAddress local)
 try
 {
-  string threadName = "dnsdist/control";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for control console thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/control");
   ComboAddress client;
   int sock;
   auto localACL = g_consoleACL.getLocal();
index 4dd98797d1766fce76a9218e16844e46a95186f6..ac9705f67fcb40c72d607cd97ebce6204f95898a 100644 (file)
@@ -19,7 +19,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
-#include <pthread.h>
+#include "threadname.hh"
 #include "dnsdist.hh"
 #include "dnsdist-ecs.hh"
 #include "dnsdist-lua.hh"
@@ -212,11 +212,7 @@ std::map<string,double> TeeAction::getStats() const
 
 void TeeAction::worker()
 {
-  string threadName = "dnsdist/TeeWork";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for TeeAction worker thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/TeeWork");
   char packet[1500];
   int res=0;
   struct dnsheader* dh=(struct dnsheader*)packet;
index 69d653d4bbf74e6cafcefbb15bd72f92c1dbd2c4..6519cbba5fdb604e035d34d1bd910d2eff5e3089 100644 (file)
@@ -29,6 +29,7 @@
 #include "lock.hh"
 #include "gettime.hh"
 #include "tcpiohandler.hh"
+#include "threadname.hh"
 #include <thread>
 #include <atomic>
 
@@ -237,11 +238,7 @@ void* tcpClientThread(int pipefd)
   /* we get launched with a pipe on which we receive file descriptors from clients that we own
      from that point on */
 
-  string threadName = "dnsdist/tcpClie";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for TCP Client thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/tcpClie");
 
   bool outstanding = false;
   time_t lastTCPCleanup = time(nullptr);
@@ -676,11 +673,7 @@ void* tcpClientThread(int pipefd)
 */
 void* tcpAcceptorThread(void* p)
 {
-  string threadName = "dnsdist/tcpAcce";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for TCP acceptor thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/tcpAcce");
   ClientState* cs = (ClientState*) p;
   bool tcpClientCountIncremented = false;
   ComboAddress remote;
index 4be381195883c2d33a5d8e46ed11265cd3307dbe..12cc28aea0e087565f7ffaeaf68294e183eb435f 100644 (file)
@@ -25,7 +25,7 @@
 #include "ext/incbin/incbin.h"
 #include "dolog.hh"
 #include <thread>
-#include <pthread.h>
+#include "threadname.hh"
 #include <sstream>
 #include <yahttp/yahttp.hpp>
 #include "namespaces.hh"
@@ -243,11 +243,7 @@ static json11::Json::array someResponseRulesToJson(GlobalStateHolder<vector<T>>*
 
 static void connectionThread(int sock, ComboAddress remote, string password, string apiKey, const boost::optional<std::map<std::string, std::string> >& customHeaders)
 {
-  string threadName = "dnsdist/webConn";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for webserver connection thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/webConn");
 
   using namespace json11;
   vinfolog("Webserver handling connection from %s", remote.toStringWithPort());
@@ -820,11 +816,7 @@ static void connectionThread(int sock, ComboAddress remote, string password, str
 }
 void dnsdistWebserverThread(int sock, const ComboAddress& local, const std::string& password, const std::string& apiKey, const boost::optional<std::map<std::string, std::string> >& customHeaders)
 {
-  string threadName = "dnsdist/webserv";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for webserver thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/webserv");
   warnlog("Webserver launched on %s", local.toStringWithPort());
   for(;;) {
     try {
index 7cf206ffb10407e4de7eca9c78474da83312aeff..9e985bab1a76aa4c66b6de3f166742fda091788b 100644 (file)
@@ -60,6 +60,7 @@
 #include "misc.hh"
 #include "sodcrypto.hh"
 #include "sstuff.hh"
+#include "threadname.hh"
 #include "xpf.hh"
 
 thread_local boost::uuids::random_generator t_uuidGenerator;
@@ -426,11 +427,7 @@ static void pickBackendSocketsReadyForReceiving(const std::shared_ptr<Downstream
 // listens on a dedicated socket, lobs answers from downstream servers to original requestors
 void* responderThread(std::shared_ptr<DownstreamState> dss)
 try {
-  string threadName = "dnsdist/respond";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for responder thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/respond");
   auto localRespRulactions = g_resprulactions.getLocal();
 #ifdef HAVE_DNSCRYPT
   char packet[4096 + DNSCRYPT_MAX_RESPONSE_PADDING_AND_MAC_SIZE];
@@ -1660,11 +1657,7 @@ static void MultipleMessagesUDPClientThread(ClientState* cs, LocalHolders& holde
 static void* udpClientThread(ClientState* cs)
 try
 {
-  string threadName = "dnsdist/udpClie";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for UDP client thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/udpClie");
   LocalHolders holders;
 
 #if defined(HAVE_RECVMMSG) && defined(HAVE_SENDMMSG) && defined(MSG_WAITFORONE)
@@ -1860,11 +1853,7 @@ std::atomic<uint16_t> g_cacheCleaningPercentage{100};
 
 void* maintThread()
 {
-  string threadName = "dnsdist/main";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for main thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/main");
   int interval = 1;
   size_t counter = 0;
   int32_t secondsToWaitLog = 0;
@@ -1911,11 +1900,7 @@ void* maintThread()
 
 void* healthChecksThread()
 {
-  string threadName = "dnsdist/healthC";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    warnlog("Could not set thread name %s for health check thread: %s", threadName, strerror(retval));
-  }
+  setThreadName("dnsdist/healthC");
 
   int interval = 1;
 
index c7c563c7daef2b7ed41911f8bb30aca7246c30c8..0c8cee1e28d0887ea14a3de2585fb77392fd61f3 100644 (file)
@@ -138,6 +138,7 @@ dnsdist_SOURCES = \
        sstuff.hh \
        statnode.cc statnode.hh \
        tcpiohandler.cc tcpiohandler.hh \
+       threadname.hh threadname.cc \
        xpf.cc xpf.hh \
        ext/luawrapper/include/LuaContext.hpp \
        ext/json11/json11.cpp \
index 9bc010e3ff7a939f85c8b206c46bdf2a9a1777a9..e1ae7e9accc9509e52c1aad584900505aa0ebf07 100644 (file)
@@ -10,6 +10,10 @@ AC_PROG_CXX
 AC_LANG([C++])
 AC_GNU_SOURCE
 
+AC_DEFINE([DNSDIST], [1],
+  [This is dnsdist]
+)
+
 LT_PREREQ([2.2.2])
 LT_INIT([disable-static])
 
@@ -20,6 +24,7 @@ PDNS_CHECK_LIBEDIT
 PDNS_CHECK_CLOCK_GETTIME
 
 PDNS_CHECK_OS
+PTHREAD_SET_NAME
 PDNS_CHECK_NETWORK_LIBS
 PDNS_CHECK_PTHREAD_NP
 PDNS_CHECK_SECURE_MEMSET
diff --git a/pdns/dnsdistdist/m4/ac_pthread_set_name.m4 b/pdns/dnsdistdist/m4/ac_pthread_set_name.m4
new file mode 120000 (symlink)
index 0000000..a0aa102
--- /dev/null
@@ -0,0 +1 @@
+../../../m4/ac_pthread_set_name.m4
\ No newline at end of file
diff --git a/pdns/dnsdistdist/threadname.cc b/pdns/dnsdistdist/threadname.cc
new file mode 120000 (symlink)
index 0000000..b850aaf
--- /dev/null
@@ -0,0 +1 @@
+../threadname.cc
\ No newline at end of file
diff --git a/pdns/dnsdistdist/threadname.hh b/pdns/dnsdistdist/threadname.hh
new file mode 120000 (symlink)
index 0000000..df5c412
--- /dev/null
@@ -0,0 +1 @@
+../threadname.hh
\ No newline at end of file
index 18c3d4c4b2cd756bc825710b6b8bf4fe15761351..41ec9d84acf2d0e54d5348e305e0867b91412bff 100644 (file)
@@ -34,6 +34,7 @@
 #include "dns_random.hh"
 #include "stubresolver.hh"
 #include "arguments.hh"
+#include "threadname.hh"
 
 extern StatBag S;
 
@@ -175,11 +176,7 @@ int DNSProxy::getID_locked()
 
 void DNSProxy::mainloop(void)
 {
-  string threadName = "pdns/dnsproxy";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for DNS proxy thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/dnsproxy");
   try {
     char buffer[1500];
     ssize_t len;
index 675731c701a4991ae7402a22ae730f2c8a65b8d7..5f326ede126fd840935e9024b38d21048962bcbd 100644 (file)
@@ -34,6 +34,7 @@
 #include "dnswriter.hh"
 #include "dnsrecords.hh"
 #include "statbag.hh"
+#include "threadname.hh"
 #include <netinet/tcp.h>
 #include <boost/array.hpp>
 #include <boost/program_options.hpp>
@@ -174,11 +175,7 @@ vector<BenchQuery> g_queries;
 
 static void* worker(void*)
 {
-  string threadName = "dnstcpb/worker";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    cerr<<"Could not set thread name "<<threadName<<" for wthread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("dnstcpb/worker");
   for(;;) {
     unsigned int pos = g_pos++; 
     if(pos >= g_queries.size())
index 05036c803b09d27d04b2b3b84c74f4b23a270cd3..381054c87ebf22756ddbf04049fb943c32b570f2 100644 (file)
@@ -52,6 +52,7 @@
 #include "dnspacket.hh"
 #include "logger.hh"
 #include "statbag.hh"
+#include "threadname.hh"
 
 extern StatBag S;
 
@@ -216,11 +217,7 @@ void DynListener::go()
 
 void *DynListener::theListenerHelper(void *p)
 {
-  string threadName = "pdns/ctrlListen";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for control socket listener thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/ctrlListen");
   DynListener *us=static_cast<DynListener *>(p);
   us->theListener();
   g_log<<Logger::Error<<"Control listener aborted, please file a bug!"<<endl;
index cee9377b5babb33a04ea348534710ab23564e90a..f3a04646ae0f948abe1a81064a5c058133b8c2cb 100644 (file)
@@ -30,7 +30,7 @@
 #include <sys/stat.h>
 #include <mutex>
 #include <thread>
-#include <pthread.h>
+#include "threadname.hh"
 #include <dirent.h>
 #include <queue>
 #include <condition_variable>
@@ -235,11 +235,7 @@ static void updateCurrentZoneInfo(const DNSName& domain, std::shared_ptr<ixfrinf
 }
 
 void updateThread(const string& workdir, const uint16_t& keep, const uint16_t& axfrTimeout) {
-  string threadName = "ixfrdist/update";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for update thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("ixfrdist/update");
   std::map<DNSName, time_t> lastCheck;
 
   // Initialize the serials we have
@@ -774,11 +770,7 @@ static void handleTCPRequest(int fd, boost::any&) {
 /* Thread to handle TCP traffic
  */
 static void tcpWorker(int tid) {
-  string threadName = "ixfrdist/tcpWor";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for update thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("ixfrdist/tcpWor");
   string prefix = "TCP Worker " + std::to_string(tid) + ": ";
 
   while(true) {
index ef3c8a3d67d53ea1e2f1b325768daf2015ff3cd4..3a00b48bcb962ceaf67a2072022ba4584f8a6840 100644 (file)
@@ -27,6 +27,7 @@
 #include <iomanip>
 #include <ctime>
 #include <thread>
+#include "threadname.hh"
 #include <unistd.h>
 #include <boost/filesystem.hpp>
 #include "logger.hh"
@@ -99,11 +100,7 @@ bool NODDB::init(bool ignore_pid) {
 
 void NODDB::housekeepingThread()
 {
-  string threadName = "pdns-r/NOD-hk";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for NOD housekeeping thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns-r/NOD-hk");
   for (;;) {
     sleep(d_snapshot_interval);
     {
index aeab8c5b69db91d47dcee82318607d4e636a6dbe..4d346fc0e06fab4e6e72f74e1e150316030b73ca 100644 (file)
@@ -31,7 +31,7 @@
 #endif
 #include "ws-recursor.hh"
 #include <thread>
-#include <pthread.h>
+#include "threadname.hh"
 #include "recpacketcache.hh"
 #include "utility.hh"
 #include "dns_random.hh"
@@ -3735,11 +3735,7 @@ try
   auto& threadInfo = s_threadInfos.at(t_id);
 
   static string threadPrefix = "pdns-r/";
-
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>((threadPrefix + threadName).c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadPrefix<<threadName<<" for thread number "<<n<<": "<<strerror(retval)<<endl;
-  }
+  setThreadName(threadPrefix + threadName);
 
   SyncRes tmp(g_now); // make sure it allocates tsstorage before we do anything, like primeHints or so..
   SyncRes::setDomainMap(g_initialDomainMap);
index 09901dde98f6ffbeb738938fdeb0e868bfc9f5a6..190a04e7874dd546e7dd28ec31fba9b954a7a90b 100644 (file)
@@ -161,6 +161,7 @@ pdns_recursor_SOURCES = \
        sstuff.hh \
        stable-bloom.hh \
        syncres.cc syncres.hh \
+       threadname.hh threadname.cc \
        tsigverifier.cc tsigverifier.hh \
        ueberbackend.hh \
        unix_utility.cc \
index 3b38e9fd505a4c3ef544e7647ed97e3d86a39fa7..54e085f828aecdca25fe33fad4c6a36b821db341 100644 (file)
@@ -37,6 +37,7 @@ AC_PROG_LIBTOOL
 
 PDNS_CHECK_OS
 PDNS_CHECK_NETWORK_LIBS
+PTHREAD_SET_NAME
 
 # Boost Context was introduced in 1.51 (Aug 2012), but there was an immediate
 # API break in 1.52 (Nov 2012), so we only support that, and later.
diff --git a/pdns/recursordist/m4/ac_pthread_set_name.m4 b/pdns/recursordist/m4/ac_pthread_set_name.m4
new file mode 120000 (symlink)
index 0000000..a0aa102
--- /dev/null
@@ -0,0 +1 @@
+../../../m4/ac_pthread_set_name.m4
\ No newline at end of file
diff --git a/pdns/recursordist/threadname.cc b/pdns/recursordist/threadname.cc
new file mode 120000 (symlink)
index 0000000..b850aaf
--- /dev/null
@@ -0,0 +1 @@
+../threadname.cc
\ No newline at end of file
diff --git a/pdns/recursordist/threadname.hh b/pdns/recursordist/threadname.hh
new file mode 120000 (symlink)
index 0000000..df5c412
--- /dev/null
@@ -0,0 +1 @@
+../threadname.hh
\ No newline at end of file
index 657c493b7339b7b3d78bb47310e72ea3a38c3b2c..50e54248d2eb0308977ddbde202727c2d7ede92f 100644 (file)
@@ -1,7 +1,9 @@
 #include <unistd.h>
-#include <pthread.h>
+#include "threadname.hh"
 #include "remote_logger.hh"
+#ifdef HAVE_CONFIG_H
 #include "config.h"
+#endif
 #ifdef PDNS_CONFIG_ARGS
 #include "logger.hh"
 #define WE_ARE_RECURSOR
@@ -47,14 +49,7 @@ void RemoteLogger::worker()
 #else
   string threadName = "dnsdist/remLog";
 #endif
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-#ifdef WE_ARE_RECURSOR
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for Remote Logger thread: "<<strerror(retval)<<endl;
-#else
-    warnlog("Could not set thread name %s for Remote Logger thread: %s", threadName, strerror(retval));
-#endif
-  }
+  setThreadName(threadName);
   while(true) {
     std::string data;
     {
index 5e863dde504e2a187584e8ff3f56686f12db61b1..1ef58622cb34070bda69db9aa8af625e49310c66 100644 (file)
@@ -1,4 +1,3 @@
-#include <pthread.h>
 #include "dnsparser.hh"
 #include "dnsrecords.hh"
 #include "ixfr.hh"
@@ -8,6 +7,7 @@
 #include "rec-lua-conf.hh"
 #include "rpzloader.hh"
 #include "zoneparser-tng.hh"
+#include "threadname.hh"
 
 static Netmask makeNetmaskFromRPZ(const DNSName& name)
 {
@@ -340,12 +340,7 @@ static bool dumpZoneToDisk(const DNSName& zoneName, const std::shared_ptr<DNSFil
 
 void RPZIXFRTracker(const std::vector<ComboAddress> masters, boost::optional<DNSFilterEngine::Policy> defpol, uint32_t maxTTL, size_t zoneIdx, const TSIGTriplet& tt, size_t maxReceivedBytes, const ComboAddress& localAddress, const uint16_t axfrTimeout, std::shared_ptr<SOARecordContent> sr, std::string dumpZoneFileName, uint64_t configGeneration)
 {
-  string threadName = "pdns-r/RPZIXFR";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for RPZ IXFRTracker thread: "<<strerror(retval)<<endl;
-  }
-
+  setThreadName("pdns-r/RPZIXFR");
   bool isPreloaded = sr != nullptr;
   auto luaconfsLocal = g_luaconfs.getLocal();
   /* we can _never_ modify this zone directly, we need to do a full copy then replace the existing zone */
index 3c739ffb2d1b2255c6d518fe02c5d19e88f2f5d8..0ac33ed8bb154c2ffa1302cb09da2ae87860432a 100644 (file)
@@ -1,5 +1,6 @@
 #include "snmp-agent.hh"
 #include "misc.hh"
+#include "threadname.hh"
 #ifdef RECURSOR
 #include "logger.hh"
 #else
@@ -110,14 +111,7 @@ void SNMPAgent::worker()
 #else
   string threadName = "dnsdist/SNMP";
 #endif
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-#ifdef RECURSOR
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for SNMP thread: "<<strerror(retval)<<endl;
-#else
-    warnlog("Could not set thread name %s for SNMP thread: %s", threadName, strerror(retval));
-#endif
-  }
+  setThreadName(threadName);
 
   int maxfd = 0;
   int block = 1;
index 1307ae75a98471a7ed08af29f100bff44d9b4be9..9b45cdf39ea8179b7e25142df02f3c7b58bf021b 100644 (file)
@@ -25,6 +25,7 @@
 #include <boost/algorithm/string.hpp>
 #include "auth-packetcache.hh"
 #include "utility.hh"
+#include "threadname.hh"
 #include "dnssecinfra.hh"
 #include "dnsseckeeper.hh"
 #include <cstdio>
@@ -252,11 +253,7 @@ void TCPNameserver::decrementClientCount(const ComboAddress& remote)
 
 void *TCPNameserver::doConnection(void *data)
 {
-  string threadName = "pdns/tcpConnect";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for TCP nameserver connection thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/tcpConnect");
   shared_ptr<DNSPacket> packet;
   // Fix gcc-4.0 error (on AMD64)
   int fd=(int)(long)data; // gotta love C (generates a harmless warning on opteron)
@@ -1340,11 +1337,7 @@ TCPNameserver::TCPNameserver()
 //! Start of TCP operations thread, we launch a new thread for each incoming TCP question
 void TCPNameserver::thread()
 {
-  string threadName = "pdns/tcpnameser";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for TCP nameserver thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns/tcpnameser");
   try {
     for(;;) {
       int fd;
diff --git a/pdns/threadname.cc b/pdns/threadname.cc
new file mode 100644 (file)
index 0000000..e20c013
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include <string.h>
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <pthread.h>
+#ifdef DNSDIST
+#include "dolog.hh"
+#else
+#include "logger.hh"
+#endif
+#include "threadname.hh"
+
+void setThreadName(const std::string& threadName) {
+  int retval = 0;
+
+#ifdef HAVE_PTHREAD_SETNAME_NP_2
+  retval = pthread_setname_np(pthread_self(), threadName.c_str());
+#endif
+#ifdef HAVE_PTHREAD_SET_NAME_NP_2
+  retval = pthread_set_name_np(pthread_self(), threadName.c_str());
+#endif
+#ifdef HAVE_PTHREAD_SET_NAME_NP_2_VOID
+  pthread_set_name_np(pthread_self(), threadName.c_str());
+#endif
+#ifdef HAVE_PTHREAD_SETNAME_NP_1
+  retval = pthread_setname_np(threadName.c_str());
+#endif
+#ifdef HAVE_PTHREAD_SETNAME_NP_3
+  retval = pthread_setname_np(pthread_self(), threadname.c_str(), nullptr);
+#endif
+
+  if (retval != 0) {
+#ifdef DNSDIST
+    warnlog("Could not set thread name %s for thread: %s", threadName, strerror(retval));
+#else
+    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for thread: "<<strerror(retval)<<endl;
+#endif
+  }
+}
+
diff --git a/pdns/threadname.hh b/pdns/threadname.hh
new file mode 100644 (file)
index 0000000..685116c
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#pragma once
+#include <string>
+
+void setThreadName(const std::string& threadName);
index d63da01ed689a0b83d26fe4bbf280dea24988714..1b2706c990fb2a0b3895fd8373ecb5f31e38080f 100644 (file)
@@ -26,7 +26,7 @@
 #include "webserver.hh"
 #include "misc.hh"
 #include <thread>
-#include <pthread.h>
+#include "threadname.hh"
 #include <vector>
 #include "logger.hh"
 #include <stdio.h>
@@ -196,11 +196,7 @@ void WebServer::registerWebHandler(const string& url, HandlerFunction handler) {
 }
 
 static void *WebServerConnectionThreadStart(const WebServer* webServer, std::shared_ptr<Socket> client) {
-  string threadName = "pdns-r/webhndlr";
-  auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-  if (retval != 0) {
-    g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for webserver handler thread: "<<strerror(retval)<<endl;
-  }
+  setThreadName("pdns-r/webhndlr");
   webServer->serveConnection(client);
   return nullptr;
 }
index abc16e2faee0ece336ceb4b71ff9f28886249de9..e025d2b4349f074f6d481839601dd2b30e228378 100644 (file)
@@ -44,6 +44,7 @@
 #include "zoneparser-tng.hh"
 #include "common_startup.hh"
 #include "auth-caches.hh"
+#include "threadname.hh"
 
 using json11::Json;
 
@@ -75,11 +76,7 @@ void AuthWebServer::go()
 void AuthWebServer::statThread()
 {
   try {
-    string threadName = "pdns/statHelper";
-    auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-    if (retval != 0) {
-      g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for stat helper thread: "<<strerror(retval)<<endl;
-    }
+    setThreadName("pdns/statHelper");
     for(;;) {
       d_queries.submit(S.read("udp-queries"));
       d_cachehits.submit(S.read("packetcache-hit"));
@@ -1954,11 +1951,7 @@ void AuthWebServer::cssfunction(HttpRequest* req, HttpResponse* resp)
 void AuthWebServer::webThread()
 {
   try {
-    string threadName = "pdns/webserver";
-    auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
-    if (retval != 0) {
-      g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for webserver thread: "<<strerror(retval)<<endl;
-    }
+    setThreadName("pdns/webserver");
     if(::arg().mustDo("api")) {
       d_ws->registerApiHandler("/api/v1/servers/localhost/cache/flush", &apiServerCacheFlush);
       d_ws->registerApiHandler("/api/v1/servers/localhost/config", &apiServerConfig);