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