• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 18837422702

27 Oct 2025 10:15AM UTC coverage: 73.025% (+0.02%) from 73.004%
18837422702

Pull #16375

github

web-flow
Merge 2f23fc90d into 82ea647b4
Pull Request #16375: dnsdist: Include a Date: response header for rejected HTTP1 requests

38279 of 63122 branches covered (60.64%)

Branch coverage included in aggregate %.

0 of 11 new or added lines in 1 file covered. (0.0%)

10680 existing lines in 98 files now uncovered.

127481 of 163870 relevant lines covered (77.79%)

5384548.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.27
/pdns/pollmplexer.cc
1
#ifdef HAVE_CONFIG_H
2
#include "config.h"
3
#endif
4
#include "mplexer.hh"
5
#include "sstuff.hh"
6
#include <iostream>
7
#include <poll.h>
8
#include <unordered_map>
9
#include "misc.hh"
10
#include "namespaces.hh"
11

12
FDMultiplexer* FDMultiplexer::getMultiplexerSilent(unsigned int maxEventsHint)
13
{
6,436✔
14
  FDMultiplexer* ret = nullptr;
6,436✔
15
  for (const auto& i : FDMultiplexer::getMultiplexerMap()) {
6,436✔
16
    try {
6,434✔
17
      ret = i.second(std::min(maxEventsHint, FDMultiplexer::s_maxevents));
6,434✔
18
      return ret;
6,434✔
19
    }
6,434✔
20
    catch (const FDMultiplexerException& fe) {
6,434✔
UNCOV
21
    }
6✔
22
    catch (...) {
6,434✔
23
    }
×
24
  }
6,434✔
UNCOV
25
  return ret;
8✔
26
}
6,436✔
27

28
class PollFDMultiplexer : public FDMultiplexer
29
{
30
public:
31
  PollFDMultiplexer(unsigned int /* maxEventsHint */)
32
  {}
20✔
33

34
  int run(struct timeval* tv, int timeout = 500) override;
35
  void getAvailableFDs(std::vector<int>& fds, int timeout) override;
36

37
  void addFD(int fd, FDMultiplexer::EventKind) override;
38
  void removeFD(int fd, FDMultiplexer::EventKind) override;
39

40
  string getName() const override
41
  {
×
42
    return "poll";
×
43
  }
×
44

45
private:
46
  std::unordered_map<int, struct pollfd> d_pollfds;
47
  vector<struct pollfd> preparePollFD() const;
48
};
49

50
static FDMultiplexer* make(unsigned int maxEventsHint)
51
{
20✔
52
  return new PollFDMultiplexer(maxEventsHint);
20✔
53
}
20✔
54

55
static struct RegisterOurselves
56
{
57
  RegisterOurselves()
58
  {
997✔
59
    FDMultiplexer::getMultiplexerMap().emplace(2, &make);
997✔
60
  }
997✔
61
} doIt;
62

63
static int convertEventKind(FDMultiplexer::EventKind kind)
64
{
41✔
65
  switch (kind) {
41!
66
  case FDMultiplexer::EventKind::Read:
16✔
67
    return POLLIN;
16✔
68
  case FDMultiplexer::EventKind::Write:
20✔
69
    return POLLOUT;
20✔
70
  case FDMultiplexer::EventKind::Both:
5✔
71
    return POLLIN | POLLOUT;
5✔
72
  }
41✔
73
  throw std::runtime_error("Unhandled event kind in the ports multiplexer");
×
74
}
41✔
75

76
void PollFDMultiplexer::addFD(int fd, FDMultiplexer::EventKind kind)
77
{
41✔
78
  if (d_pollfds.count(fd) == 0) {
41✔
79
    auto& pollfd = d_pollfds[fd];
36✔
80
    pollfd.fd = fd;
36✔
81
    pollfd.events = 0;
36✔
82
  }
36✔
83
  auto& pollfd = d_pollfds.at(fd);
41✔
84
  pollfd.events |= convertEventKind(kind);
41✔
85
}
41✔
86

87
void PollFDMultiplexer::removeFD(int fd, FDMultiplexer::EventKind)
88
{
30✔
89
  d_pollfds.erase(fd);
30✔
90
}
30✔
91

92
vector<struct pollfd> PollFDMultiplexer::preparePollFD() const
93
{
169✔
94
  std::vector<struct pollfd> result;
169✔
95
  result.reserve(d_pollfds.size());
169✔
96
  for (const auto& entry : d_pollfds) {
252✔
97
    result.push_back(entry.second);
241✔
98
  }
241✔
99

100
  return result;
169✔
101
}
169✔
102

103
void PollFDMultiplexer::getAvailableFDs(std::vector<int>& fds, int timeout)
104
{
45✔
105
  auto pollfds = preparePollFD();
45✔
106
  if (pollfds.empty()) {
45✔
107
    return;
10✔
108
  }
10✔
109

110
  int ret = poll(&pollfds[0], pollfds.size(), timeout);
35✔
111

112
  if (ret < 0 && errno != EINTR) {
35!
113
    throw FDMultiplexerException("poll returned error: " + stringerror());
×
114
  }
×
115

116
  for (const auto& pollfd : pollfds) {
40✔
117
    if (pollfd.revents & POLLIN || pollfd.revents & POLLOUT || pollfd.revents & POLLERR || pollfd.revents & POLLHUP) {
40!
118
      fds.push_back(pollfd.fd);
35✔
119
    }
35✔
120
  }
40✔
121
}
35✔
122

123
int PollFDMultiplexer::run(struct timeval* now, int timeout)
124
{
124✔
125
  InRun guard(d_inrun);
124✔
126

127
  auto pollfds = preparePollFD();
124✔
128
  if (pollfds.empty()) {
124✔
129
    gettimeofday(now, nullptr); // MANDATORY!
11✔
130
    return 0;
11✔
131
  }
11✔
132

133
  int ret = poll(&pollfds[0], pollfds.size(), timeout);
113✔
134
  gettimeofday(now, nullptr); // MANDATORY!
113✔
135

136
  if (ret < 0 && errno != EINTR) {
113!
137
    throw FDMultiplexerException("poll returned error: " + stringerror());
×
138
  }
×
139

140
  int count = 0;
113✔
141
  for (const auto& pollfd : pollfds) {
201✔
142
    if (pollfd.revents & POLLIN || pollfd.revents & POLLERR || pollfd.revents & POLLHUP) {
201!
143
      const auto& iter = d_readCallbacks.find(pollfd.fd);
61✔
144
      if (iter != d_readCallbacks.end()) {
61!
145
        iter->d_callback(iter->d_fd, iter->d_parameter);
61✔
146
        count++;
61✔
147
      }
61✔
148
    }
61✔
149

150
    if (pollfd.revents & POLLOUT || pollfd.revents & POLLERR) {
201!
151
      const auto& iter = d_writeCallbacks.find(pollfd.fd);
20✔
152
      if (iter != d_writeCallbacks.end()) {
20!
153
        iter->d_callback(iter->d_fd, iter->d_parameter);
20✔
154
        count++;
20✔
155
      }
20✔
156
    }
20✔
157
  }
201✔
158

159
  return count;
113✔
160
}
113✔
161

162
#if 0
163

164
void acceptData(int fd, boost::any& parameter)
165
{
166
  cout<<"Have data on fd "<<fd<<endl;
167
  Socket* sock=boost::any_cast<Socket*>(parameter);
168
  string packet;
169
  IPEndpoint rem;
170
  sock->recvFrom(packet, rem);
171
  cout<<"Received "<<packet.size()<<" bytes!\n";
172
}
173

174

175
int main()
176
{
177
  Socket s(AF_INET, SOCK_DGRAM);
178

179
  IPEndpoint loc("0.0.0.0", 2000);
180
  s.bind(loc);
181

182
  PollFDMultiplexer sfm;
183

184
  sfm.addReadFD(s.getHandle(), &acceptData, &s);
185

186
  for(int n=0; n < 100 ; ++n) {
187
    sfm.run();
188
  }
189
  sfm.removeReadFD(s.getHandle());
190
  sfm.removeReadFD(s.getHandle());
191
}
192
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc