blob: c32cbb24531c583b01f36c25029b7b4b5a3da288 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
$NetBSD: patch-misc.cc,v 1.1.2.2 2020/06/25 18:21:29 bsiegert Exp $
backport changes based on PR #9127 from
https://github.com/PowerDNS/pdns/pull/9127
--- misc.cc.orig 2020-05-08 09:31:59.000000000 +0000
+++ misc.cc
@@ -57,6 +57,7 @@
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
+#include <limits.h>
#ifdef __FreeBSD__
# include <pthread_np.h>
#endif
@@ -1563,3 +1564,39 @@ bool setPipeBufferSize(int fd, size_t si
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;
+}
|