]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/communicator.cc
Meson: Add systemd feature support for service files
[thirdparty/pdns.git] / pdns / communicator.cc
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 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <set>
27 #include <thread>
28 #include <boost/utility.hpp>
29
30 #include "packetcache.hh"
31 #include "utility.hh"
32 #include "communicator.hh"
33 #include "dnsbackend.hh"
34 #include "ueberbackend.hh"
35 #include "packethandler.hh"
36 #include "logger.hh"
37 #include "dns.hh"
38 #include "arguments.hh"
39 #include "packetcache.hh"
40 #include "threadname.hh"
41
42 // there can be MANY OF THESE
43 void CommunicatorClass::retrievalLoopThread()
44 {
45 setThreadName("pdns/comm-retre");
46 for (;;) {
47 d_suck_sem.wait();
48 SuckRequest sr;
49 {
50 auto data = d_data.lock();
51 if (data->d_suckdomains.empty()) {
52 continue;
53 }
54
55 auto firstItem = data->d_suckdomains.begin();
56
57 sr = *firstItem;
58 data->d_suckdomains.erase(firstItem);
59 if (data->d_suckdomains.empty()) {
60 data->d_sorthelper = 0;
61 }
62 }
63 suck(sr.domain, sr.primary, sr.force);
64 }
65 }
66
67 void CommunicatorClass::loadArgsIntoSet(const char* listname, set<string>& listset)
68 {
69 vector<string> parts;
70 stringtok(parts, ::arg()[listname], ", \t");
71 for (const auto& part : parts) {
72 try {
73 ComboAddress caIp(part, 53);
74 listset.insert(caIp.toStringWithPort());
75 }
76 catch (PDNSException& e) {
77 g_log << Logger::Error << "Unparseable IP in " << listname << ". Error: " << e.reason << endl;
78 _exit(1);
79 }
80 }
81 }
82
83 void CommunicatorClass::go()
84 {
85 try {
86 PacketHandler::s_allowNotifyFrom.toMasks(::arg()["allow-notify-from"]);
87 }
88 catch (PDNSException& e) {
89 g_log << Logger::Error << "Unparseable IP in allow-notify-from. Error: " << e.reason << endl;
90 _exit(1);
91 }
92
93 std::thread mainT([this]() { mainloop(); });
94 mainT.detach();
95
96 for (int nthreads = 0; nthreads < ::arg().asNum("retrieval-threads", 1); ++nthreads) {
97 std::thread retrieve([this]() { retrievalLoopThread(); });
98 retrieve.detach();
99 }
100
101 d_preventSelfNotification = ::arg().mustDo("prevent-self-notification");
102
103 auto delay = ::arg().asNum("delay-notifications");
104 if (delay > 0) {
105 d_delayNotifications = static_cast<time_t>(delay);
106 }
107
108 try {
109 d_onlyNotify.toMasks(::arg()["only-notify"]);
110 }
111 catch (PDNSException& e) {
112 g_log << Logger::Error << "Unparseable IP in only-notify. Error: " << e.reason << endl;
113 _exit(1);
114 }
115
116 loadArgsIntoSet("also-notify", d_alsoNotify);
117
118 loadArgsIntoSet("forward-notify", PacketHandler::s_forwardNotify);
119 }
120
121 void CommunicatorClass::mainloop()
122 {
123 try {
124 setThreadName("pdns/comm-main");
125 signal(SIGPIPE, SIG_IGN);
126 g_log << Logger::Warning << "Primary/secondary communicator launching" << endl;
127
128 d_tickinterval = ::arg().asNum("xfr-cycle-interval");
129
130 int rc;
131 time_t next;
132 PacketHandler P;
133
134 makeNotifySockets();
135
136 for (;;) {
137 secondaryRefresh(&P);
138 primaryUpdateCheck(&P);
139 doNotifications(&P); // this processes any notification acknowledgements and actually send out our own notifications
140
141 next = time(nullptr) + d_tickinterval;
142
143 while (time(nullptr) < next) {
144 rc = d_any_sem.tryWait();
145
146 if (rc != 0) {
147 bool extraSecondaryRefresh = false;
148 Utility::sleep(1);
149 {
150 auto data = d_data.lock();
151 if (data->d_tocheck.size()) {
152 extraSecondaryRefresh = true;
153 }
154 }
155 if (extraSecondaryRefresh)
156 secondaryRefresh(&P);
157 }
158 else {
159 // eat up extra posts to avoid busy looping if many posts were done
160 while (d_any_sem.tryWait() == 0) {
161 }
162 break; // something happened
163 }
164 // this gets executed about once per second
165 doNotifications(&P);
166 }
167 }
168 }
169 catch (PDNSException& ae) {
170 g_log << Logger::Error << "Exiting because communicator thread died with error: " << ae.reason << endl;
171 Utility::sleep(1);
172 _exit(1);
173 }
174 catch (std::exception& e) {
175 g_log << Logger::Error << "Exiting because communicator thread died with STL error: " << e.what() << endl;
176 _exit(1);
177 }
178 catch (...) {
179 g_log << Logger::Error << "Exiting because communicator caught unknown exception." << endl;
180 _exit(1);
181 }
182 }