]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Fix compilation on systems that do not define HOST_NAME_MAX
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 19 May 2020 14:46:33 +0000 (16:46 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 25 May 2020 14:20:18 +0000 (16:20 +0200)
On FreeBSD at least, HOST_NAME_MAX is not defined and we need to
use sysconf() to get the value at runtime instead.
Based on a work done by @RvdE to make the recursor compile on
FreeBSD (many thanks!).

(cherry picked from commit 4c990a1b82e091d887d873c7da5254de84aabebb)
(cherry picked from commit 5c21b47fbc35ddcb8d939eb8541c6c3bad1080a8)
(cherry picked from commit d53b465c08ba7c9e9769c52424d9b1c610d5e1dc)

pdns/auth-carbon.cc
pdns/dnsdist-carbon.cc
pdns/misc.cc
pdns/misc.hh
pdns/rec-carbon.cc

index e1025f1692f66cf5e416c810a4d23fc34b2cf00f..e2bd4f48b4cd33532b2e32605af997a02df11322 100644 (file)
@@ -37,14 +37,13 @@ try
   extern StatBag S;
 
   string hostname=arg()["carbon-ourname"];
-  if(hostname.empty()) {
-    char tmp[80];
-    memset(tmp, 0, sizeof(tmp));
-    gethostname(tmp, sizeof(tmp));
-    char *p = strchr(tmp, '.');
-    if(p) *p=0;
-    hostname=tmp;
-    boost::replace_all(hostname, ".", "_");
+  if (hostname.empty()) {
+    try {
+      hostname = getCarbonHostName();
+    }
+    catch(const std::exception& e) {
+      throw std::runtime_error(std::string("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: ") + e.what());
+    }
   }
 
   vector<string> carbonServers;
index caae0bb941d49adfce8ba8b263a95b63cbe45fd8..e239a10ab53190598e40f97be76c90b377b179a9 100644 (file)
@@ -56,14 +56,13 @@ try
     for (const auto& conf : *localCarbon) {
       const auto& server = conf.server;
       std::string hostname = conf.ourname;
-      if(hostname.empty()) {
-        char tmp[80];
-        memset(tmp, 0, sizeof(tmp));
-        gethostname(tmp, sizeof(tmp));
-        char *p = strchr(tmp, '.');
-        if(p) *p=0;
-        hostname=tmp;
-        boost::replace_all(hostname, ".", "_");
+      if (hostname.empty()) {
+        try {
+          hostname = getCarbonHostName();
+        }
+        catch(const std::exception& e) {
+          throw std::runtime_error(std::string("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: ") + e.what());
+        }
       }
 
       try {
index 5f4b991f3254588f62b0629af3a935296deecb6c..ca258ddb4639d5b6edd1464e64489a55d23f71aa 100644 (file)
@@ -57,6 +57,7 @@
 #include <sys/types.h>
 #include <pwd.h>
 #include <grp.h>
+#include <limits.h>
 #ifdef __FreeBSD__
 #  include <pthread_np.h>
 #endif
@@ -1337,3 +1338,39 @@ int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus)
 #endif /* HAVE_PTHREAD_SETAFFINITY_NP */
   return ENOSYS;
 }
+
+static size_t getMaxHostNameSize()
+{
+#if defined(HOST_NAME_MAX)
+  return HOST_NAME_MAX;
+#endif
+
+#if defined(_SC_HOST_NAME_MAX)
+  auto tmp = sysconf(_SC_HOST_NAME_MAX);
+  if (tmp != -1) {
+    return tmp;
+  }
+#endif
+
+  /* _POSIX_HOST_NAME_MAX */
+  return 255;
+}
+
+std::string getCarbonHostName()
+{
+  std::string hostname;
+  hostname.resize(getMaxHostNameSize() + 1, 0);
+
+  if (gethostname(const_cast<char*>(hostname.c_str()), hostname.size()) != 0) {
+    throw std::runtime_error(stringerror());
+  }
+
+  auto pos = hostname.find(".");
+  if (pos != std::string::npos) {
+    hostname.resize(pos);
+  }
+
+  boost::replace_all(hostname, ".", "_");
+
+  return hostname;
+}
index f9db931a7cf0ae1260eb8d6f03f18b6ca8a3b737..0b0e01123e4d167f29dcc4101d05be30d2025f49 100644 (file)
@@ -597,3 +597,5 @@ unsigned int pdns_stou(const std::string& str, size_t * idx = 0, int base = 10);
 
 bool isSettingThreadCPUAffinitySupported();
 int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus);
+
+std::string getCarbonHostName();
index 3ffde289659caf0fcf3d307702c6f0263561da86..55154b8fc15fb287e5cf5e718d76db4c97d8aa14 100644 (file)
@@ -25,17 +25,13 @@ try
   if(carbonServers.empty())
     return;
 
-  if(hostname.empty()) {
-    char tmp[HOST_NAME_MAX+1];
-    memset(tmp, 0, sizeof(tmp));
-    if (gethostname(tmp, sizeof(tmp)) != 0) {
-      throw std::runtime_error("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: " + stringerror());
+  if (hostname.empty()) {
+    try {
+      hostname = getCarbonHostName();
+    }
+    catch(const std::exception& e) {
+      throw std::runtime_error(std::string("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: ") + e.what());
     }
-    char *p = strchr(tmp, '.');
-    if(p) *p=0;
-
-    hostname=tmp;
-    boost::replace_all(hostname, ".", "_");    
   }
 
   registerAllStats();