Line 0
Link Here
|
|
|
1 |
--- misc.cc.orig 2020-05-08 09:31:59 UTC |
2 |
+++ misc.cc |
3 |
@@ -57,6 +57,7 @@ |
4 |
#include <sys/types.h> |
5 |
#include <pwd.h> |
6 |
#include <grp.h> |
7 |
+#include <limits.h> |
8 |
#ifdef __FreeBSD__ |
9 |
# include <pthread_np.h> |
10 |
#endif |
11 |
@@ -1562,4 +1563,40 @@ bool setPipeBufferSize(int fd, size_t size) |
12 |
errno = ENOSYS; |
13 |
return false; |
14 |
#endif /* F_SETPIPE_SZ */ |
15 |
+} |
16 |
+ |
17 |
+static size_t getMaxHostNameSize() |
18 |
+{ |
19 |
+#if defined(HOST_NAME_MAX) |
20 |
+ return HOST_NAME_MAX; |
21 |
+#endif |
22 |
+ |
23 |
+#if defined(_SC_HOST_NAME_MAX) |
24 |
+ auto tmp = sysconf(_SC_HOST_NAME_MAX); |
25 |
+ if (tmp != -1) { |
26 |
+ return tmp; |
27 |
+ } |
28 |
+#endif |
29 |
+ |
30 |
+ /* _POSIX_HOST_NAME_MAX */ |
31 |
+ return 255; |
32 |
+} |
33 |
+ |
34 |
+std::string getCarbonHostName() |
35 |
+{ |
36 |
+ std::string hostname; |
37 |
+ hostname.resize(getMaxHostNameSize() + 1, 0); |
38 |
+ |
39 |
+ if (gethostname(const_cast<char*>(hostname.c_str()), hostname.size()) != 0) { |
40 |
+ throw std::runtime_error(stringerror()); |
41 |
+ } |
42 |
+ |
43 |
+ auto pos = hostname.find("."); |
44 |
+ if (pos != std::string::npos) { |
45 |
+ hostname.resize(pos); |
46 |
+ } |
47 |
+ |
48 |
+ boost::replace_all(hostname, ".", "_"); |
49 |
+ |
50 |
+ return hostname; |
51 |
} |
52 |
--- misc.hh.orig 2020-05-08 09:31:59 UTC |
53 |
+++ misc.hh |
54 |
@@ -604,6 +604,8 @@ gid_t strToGID(const string &str); |
55 |
unsigned int pdns_stou(const std::string& str, size_t * idx = 0, int base = 10); |
56 |
|
57 |
bool isSettingThreadCPUAffinitySupported(); |
58 |
+ |
59 |
+std::string getCarbonHostName(); |
60 |
int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus); |
61 |
|
62 |
std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath); |
63 |
--- rec-carbon.cc.orig 2020-05-08 09:31:59 UTC |
64 |
+++ rec-carbon.cc |
65 |
@@ -32,17 +32,13 @@ try |
66 |
if(namespace_name.empty()) { |
67 |
namespace_name="pdns"; |
68 |
} |
69 |
- if(hostname.empty()) { |
70 |
- char tmp[HOST_NAME_MAX+1]; |
71 |
- memset(tmp, 0, sizeof(tmp)); |
72 |
- if (gethostname(tmp, sizeof(tmp)) != 0) { |
73 |
- throw std::runtime_error("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: " + stringerror()); |
74 |
+ if (hostname.empty()) { |
75 |
+ try { |
76 |
+ hostname = getCarbonHostName(); |
77 |
} |
78 |
- char *p = strchr(tmp, '.'); |
79 |
- if(p) *p=0; |
80 |
- |
81 |
- hostname=tmp; |
82 |
- boost::replace_all(hostname, ".", "_"); |
83 |
+ catch(const std::exception& e) { |
84 |
+ 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()); |
85 |
+ } |
86 |
} |
87 |
if(instance_name.empty()) { |
88 |
instance_name="recursor"; |