]>
Commit | Line | Data |
---|---|---|
aa7929a3 | 1 | #include <unistd.h> |
519f5484 | 2 | #include "threadname.hh" |
aa7929a3 | 3 | #include "remote_logger.hh" |
519f5484 | 4 | #ifdef HAVE_CONFIG_H |
49cfc104 | 5 | #include "config.h" |
519f5484 | 6 | #endif |
49cfc104 | 7 | #ifdef PDNS_CONFIG_ARGS |
8 | #include "logger.hh" | |
9 | #define WE_ARE_RECURSOR | |
10 | #else | |
11 | #include "dolog.hh" | |
12 | #endif | |
aa7929a3 RG |
13 | |
14 | bool RemoteLogger::reconnect() | |
15 | { | |
16 | if (d_socket >= 0) { | |
17 | close(d_socket); | |
754f300f | 18 | d_socket = -1; |
aa7929a3 | 19 | } |
c2d1d4ba | 20 | d_connected = false; |
aa7929a3 RG |
21 | try { |
22 | d_socket = SSocket(d_remote.sin4.sin_family, SOCK_STREAM, 0); | |
aa7929a3 | 23 | setNonBlocking(d_socket); |
51959320 | 24 | SConnectWithTimeout(d_socket, d_remote, d_timeout); |
aa7929a3 RG |
25 | } |
26 | catch(const std::exception& e) { | |
49cfc104 | 27 | #ifdef WE_ARE_RECURSOR |
e6a9dde5 | 28 | g_log<<Logger::Warning<<"Error connecting to remote logger "<<d_remote.toStringWithPort()<<": "<<e.what()<<std::endl; |
49cfc104 | 29 | #else |
30 | warnlog("Error connecting to remote logger %s: %s", d_remote.toStringWithPort(), e.what()); | |
31 | #endif | |
aa7929a3 RG |
32 | return false; |
33 | } | |
c2d1d4ba | 34 | d_connected = true; |
aa7929a3 RG |
35 | return true; |
36 | } | |
37 | ||
c2d1d4ba RG |
38 | void RemoteLogger::busyReconnectLoop() |
39 | { | |
40 | while (!reconnect()) { | |
41 | sleep(d_reconnectWaitTime); | |
42 | } | |
43 | } | |
44 | ||
aa7929a3 RG |
45 | void RemoteLogger::worker() |
46 | { | |
77c9bc9a | 47 | #ifdef WE_ARE_RECURSOR |
c390b2da | 48 | string threadName = "pdns-r/remLog"; |
77c9bc9a PL |
49 | #else |
50 | string threadName = "dnsdist/remLog"; | |
51 | #endif | |
519f5484 | 52 | setThreadName(threadName); |
aa7929a3 RG |
53 | while(true) { |
54 | std::string data; | |
55 | { | |
56 | std::unique_lock<std::mutex> lock(d_writeMutex); | |
57 | d_queueCond.wait(lock, [this]{return (!d_writeQueue.empty()) || d_exiting;}); | |
58 | if (d_exiting) { | |
59 | return; | |
60 | } | |
61 | data = d_writeQueue.front(); | |
62 | d_writeQueue.pop(); | |
63 | } | |
64 | ||
c2d1d4ba RG |
65 | if (!d_connected) { |
66 | busyReconnectLoop(); | |
67 | } | |
68 | ||
aa7929a3 | 69 | try { |
2c570263 RG |
70 | uint16_t len = static_cast<uint16_t>(data.length()); |
71 | sendSizeAndMsgWithTimeout(d_socket, len, data.c_str(), static_cast<int>(d_timeout), nullptr, nullptr, 0, 0, 0); | |
aa7929a3 RG |
72 | } |
73 | catch(const std::runtime_error& e) { | |
49cfc104 | 74 | #ifdef WE_ARE_RECURSOR |
e6a9dde5 | 75 | g_log<<Logger::Info<<"Error sending data to remote logger "<<d_remote.toStringWithPort()<<": "<< e.what()<<endl; |
49cfc104 | 76 | #else |
77 | vinfolog("Error sending data to remote logger (%s): %s", d_remote.toStringWithPort(), e.what()); | |
78 | #endif | |
c2d1d4ba | 79 | busyReconnectLoop(); |
aa7929a3 RG |
80 | } |
81 | } | |
82 | } | |
83 | ||
84 | void RemoteLogger::queueData(const std::string& data) | |
85 | { | |
86 | { | |
63341e8d | 87 | std::lock_guard<std::mutex> lock(d_writeMutex); |
aa7929a3 RG |
88 | if (d_writeQueue.size() >= d_maxQueuedEntries) { |
89 | d_writeQueue.pop(); | |
90 | } | |
91 | d_writeQueue.push(data); | |
92 | } | |
93 | d_queueCond.notify_one(); | |
94 | } | |
95 | ||
7af84c28 | 96 | RemoteLogger::RemoteLogger(const ComboAddress& remote, uint16_t timeout, uint64_t maxQueuedEntries, uint8_t reconnectWaitTime, bool asyncConnect): d_remote(remote), d_maxQueuedEntries(maxQueuedEntries), d_timeout(timeout), d_reconnectWaitTime(reconnectWaitTime), d_asyncConnect(asyncConnect), d_thread(&RemoteLogger::worker, this) |
aa7929a3 | 97 | { |
7af84c28 RG |
98 | if (!d_asyncConnect) { |
99 | reconnect(); | |
100 | } | |
aa7929a3 RG |
101 | } |
102 | ||
103 | RemoteLogger::~RemoteLogger() | |
104 | { | |
105 | d_exiting = true; | |
754f300f | 106 | if (d_socket >= 0) { |
aa7929a3 | 107 | close(d_socket); |
754f300f | 108 | d_socket = -1; |
c2d1d4ba | 109 | d_connected = false; |
754f300f | 110 | } |
12aff2e5 | 111 | d_queueCond.notify_one(); |
aa7929a3 RG |
112 | d_thread.join(); |
113 | } |