]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/auth-carbon.cc
Don't read potentially uninitalized memory if gethostname() failed
[thirdparty/pdns.git] / pdns / auth-carbon.cc
CommitLineData
12471842
PL
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
952d3fcb 25#include "statbag.hh"
26#include "logger.hh"
519f5484 27#include "threadname.hh"
952d3fcb 28#include "iputils.hh"
29#include "sstuff.hh"
30#include "arguments.hh"
31#include "common_startup.hh"
fa8fd4d2 32
2449694c 33#include "namespaces.hh"
952d3fcb 34
c2826d2e 35void carbonDumpThread()
952d3fcb 36try
37{
519f5484 38 setThreadName("pdns/carbonDump");
952d3fcb 39 extern StatBag S;
40
b0d4aa0b 41 string namespace_name=arg()["carbon-namespace"];
486ce518
PL
42 string hostname=arg()["carbon-ourname"];
43 if(hostname.empty()) {
e7a0f37c 44 char tmp[HOST_NAME_MAX+1];
486ce518 45 memset(tmp, 0, sizeof(tmp));
e7a0f37c
RG
46 if (gethostname(tmp, sizeof(tmp)) != 0) {
47 throw std::runtime_error("The carbon-ourname setting has not been set and we are unable to determine the system's hostname: " + stringerror());
48 }
486ce518
PL
49 char *p = strchr(tmp, '.');
50 if(p) *p=0;
51 hostname=tmp;
52 boost::replace_all(hostname, ".", "_");
53 }
f7a645ec 54 string instance_name=arg()["carbon-instance"];
486ce518
PL
55
56 vector<string> carbonServers;
57 stringtok(carbonServers, arg()["carbon-server"], ", ");
58
59 for(;;) {
60 if(carbonServers.empty()) {
952d3fcb 61 sleep(1);
62 continue;
63 }
952d3fcb 64
486ce518
PL
65 string msg;
66 vector<string> entries = S.getEntries();
67 ostringstream str;
68 time_t now=time(0);
69 for(const string& entry : entries) {
b0d4aa0b 70 str<<namespace_name<<'.'<<hostname<<'.'<<instance_name<<'.'<<entry<<' '<<S.read(entry)<<' '<<now<<"\r\n";
486ce518
PL
71 }
72 msg = str.str();
73
74 for (const auto& carbonServer : carbonServers) {
75 ComboAddress remote(carbonServer, 2003);
486ce518
PL
76
77 try {
67180942
RG
78 Socket s(remote.sin4.sin_family, SOCK_STREAM);
79 s.setNonBlocking();
80 s.connect(remote, 2);
81
486ce518
PL
82 writen2WithTimeout(s.getHandle(), msg.c_str(), msg.length(), 2);
83 } catch (runtime_error &e){
e6a9dde5 84 g_log<<Logger::Warning<<"Unable to write data to carbon server at "<<remote.toStringWithPort()<<": "<<e.what()<<endl;
486ce518 85 continue;
952d3fcb 86 }
952d3fcb 87 }
486ce518 88 sleep(arg().asNum("carbon-interval"));
952d3fcb 89 }
952d3fcb 90}
2449694c 91catch(std::exception& e)
952d3fcb 92{
e6a9dde5 93 g_log<<Logger::Error<<"Carbon thread died: "<<e.what()<<endl;
952d3fcb 94}
95catch(PDNSException& e)
96{
e6a9dde5 97 g_log<<Logger::Error<<"Carbon thread died, PDNSException: "<<e.reason<<endl;
952d3fcb 98}
99catch(...)
100{
e6a9dde5 101 g_log<<Logger::Error<<"Carbon thread died"<<endl;
952d3fcb 102}