From d53b465c08ba7c9e9769c52424d9b1c610d5e1dc Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 19 May 2020 16:46:33 +0200 Subject: [PATCH] Fix compilation on systems that do not define HOST_NAME_MAX 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 | 15 +++++++-------- pdns/dnsdist-carbon.cc | 15 +++++++-------- pdns/misc.cc | 37 +++++++++++++++++++++++++++++++++++++ pdns/misc.hh | 2 ++ pdns/rec-carbon.cc | 16 ++++++---------- 5 files changed, 59 insertions(+), 26 deletions(-) diff --git a/pdns/auth-carbon.cc b/pdns/auth-carbon.cc index 4d0cfcb782..65fbe84451 100644 --- a/pdns/auth-carbon.cc +++ b/pdns/auth-carbon.cc @@ -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"]; diff --git a/pdns/dnsdist-carbon.cc b/pdns/dnsdist-carbon.cc index 14b72e048e..c6d7888e68 100644 --- a/pdns/dnsdist-carbon.cc +++ b/pdns/dnsdist-carbon.cc @@ -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; diff --git a/pdns/misc.cc b/pdns/misc.cc index a051b7e20c..32a22b53b8 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -57,6 +57,7 @@ #include #include #include +#include #ifdef __FreeBSD__ # include #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(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; +} diff --git a/pdns/misc.hh b/pdns/misc.hh index ee255e9b90..9c6d6ae333 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -603,3 +603,5 @@ bool isSettingThreadCPUAffinitySupported(); int mapThreadToCPUList(pthread_t tid, const std::set& cpus); std::vector getResolvers(const std::string& resolvConfPath); + +std::string getCarbonHostName(); diff --git a/pdns/rec-carbon.cc b/pdns/rec-carbon.cc index 58a9d82c06..a240701136 100644 --- a/pdns/rec-carbon.cc +++ b/pdns/rec-carbon.cc @@ -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"; -- 2.47.2