]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Fix compilation on systems that do not define HOST_NAME_MAX 9133/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 19 May 2020 14:46:33 +0000 (16:46 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 19 May 2020 16:01:55 +0000 (18:01 +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)

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

index 4d0cfcb7826932e7430459b3169ef3389f9e1a50..65fbe844511101cdb8ff82ca48d4ae296d082f87 100644 (file)
@@ -40,14 +40,13 @@ try
 
   string namespace_name=arg()["carbon-namespace"];
   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());
+    }
   }
   string instance_name=arg()["carbon-instance"];
 
index 14b72e048e676645c7aa47c339bbd16ea494f2cf..c6d7888e687eb7071822e072442e92c5fb506a70 100644 (file)
@@ -58,14 +58,13 @@ try
       const auto& server = conf.server;
       const std::string& namespace_name = conf.namespace_name;
       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());
+        }
       }
       const std::string& instance_name = conf.instance_name;
 
index a051b7e20c3c790adec75193880bb78a65627e0b..32a22b53b8ae74049ec4e31977f522175769d0c9 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
@@ -1522,3 +1523,39 @@ bool setPipeBufferSize(int fd, size_t size)
   return false;
 #endif /* F_SETPIPE_SZ */
 }
+
+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 ee255e9b90930a0d7972e1527529a805929386a4..9c6d6ae3333ecbd5511d99ec1c5943f409b7194f 100644 (file)
@@ -603,3 +603,5 @@ bool isSettingThreadCPUAffinitySupported();
 int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus);
 
 std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath);
+
+std::string getCarbonHostName();
index 58a9d82c06e4ad4df01d981f84b81aff69a3135e..a240701136e8d8d5485b19f11a3da8f7e26bc3e6 100644 (file)
@@ -32,17 +32,13 @@ try
   if(namespace_name.empty()) {
     namespace_name="pdns";
   }
-  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, ".", "_");    
   }
   if(instance_name.empty()) {
     instance_name="recursor";