]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/devpollmplexer.cc
Avoid throwing an exception in Logger::log().
[thirdparty/pdns.git] / pdns / devpollmplexer.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 * NOTE: sys/devpoll.h relies on sigset_t being already defined so we need
27 * to include sys/signal.h *before* including sys/devpoll.h.
28 */
29 #include <sys/signal.h>
30 #include <sys/devpoll.h>
31 #include "mplexer.hh"
32 #include "sstuff.hh"
33 #include <iostream>
34 #include <unistd.h>
35 #include "misc.hh"
36
37 #include "namespaces.hh"
38
39 class DevPollFDMultiplexer : public FDMultiplexer
40 {
41 public:
42 DevPollFDMultiplexer();
43 virtual ~DevPollFDMultiplexer()
44 {
45 close(d_devpollfd);
46 }
47
48 virtual int run(struct timeval* tv, int timeout=500) override;
49 virtual void getAvailableFDs(std::vector<int>& fds, int timeout) override;
50
51 virtual void addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, const funcparam_t& parameter, const struct timeval* ttd=nullptr) override;
52 virtual void removeFD(callbackmap_t& cbmap, int fd) override;
53 string getName() const override
54 {
55 return "/dev/poll";
56 }
57 private:
58 int d_devpollfd;
59 };
60
61
62 static FDMultiplexer* makeDevPoll()
63 {
64 return new DevPollFDMultiplexer();
65 }
66
67 static struct DevPollRegisterOurselves
68 {
69 DevPollRegisterOurselves() {
70 FDMultiplexer::getMultiplexerMap().insert(make_pair(0, &makeDevPoll)); // priority 0!
71 }
72 } doItDevPoll;
73
74
75 //int DevPollFDMultiplexer::s_maxevents=1024;
76 DevPollFDMultiplexer::DevPollFDMultiplexer()
77 {
78 d_devpollfd=open("/dev/poll", O_RDWR);
79 if(d_devpollfd < 0)
80 throw FDMultiplexerException("Setting up /dev/poll: "+stringerror());
81
82 }
83
84 void DevPollFDMultiplexer::addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, const funcparam_t& parameter, const struct timeval* ttd)
85 {
86 accountingAddFD(cbmap, fd, toDo, parameter, ttd);
87
88 struct pollfd devent;
89 devent.fd=fd;
90 devent.events= (&cbmap == &d_readCallbacks) ? POLLIN : POLLOUT;
91 devent.revents = 0;
92
93 if(write(d_devpollfd, &devent, sizeof(devent)) != sizeof(devent)) {
94 cbmap.erase(fd);
95 throw FDMultiplexerException("Adding fd to /dev/poll/ set: "+stringerror());
96 }
97 }
98
99 void DevPollFDMultiplexer::removeFD(callbackmap_t& cbmap, int fd)
100 {
101 if(!cbmap.erase(fd))
102 throw FDMultiplexerException("Tried to remove unlisted fd "+std::to_string(fd)+ " from multiplexer");
103
104 struct pollfd devent;
105 devent.fd=fd;
106 devent.events= POLLREMOVE;
107 devent.revents = 0;
108
109 if(write(d_devpollfd, &devent, sizeof(devent)) != sizeof(devent)) {
110 cbmap.erase(fd);
111 throw FDMultiplexerException("Removing fd from epoll set: "+stringerror());
112 }
113 }
114
115 void DevPollFDMultiplexer::getAvailableFDs(std::vector<int>& fds, int timeout)
116 {
117 std::vector<struct pollfd> pollfds(d_readCallbacks.size() + d_writeCallbacks.size());
118 struct dvpoll dvp;
119 dvp.dp_nfds = d_readCallbacks.size() + d_writeCallbacks.size();
120 dvp.dp_fds = pollfds.data();
121 dvp.dp_timeout = timeout;
122 int ret=ioctl(d_devpollfd, DP_POLL, &dvp);
123
124 if(ret < 0 && errno!=EINTR) {
125 throw FDMultiplexerException("/dev/poll returned error: "+stringerror());
126 }
127
128 for(int n=0; n < ret; ++n) {
129 fds.push_back(pollfds.at(n).fd);
130 }
131 }
132
133 int DevPollFDMultiplexer::run(struct timeval* now, int timeout)
134 {
135 if(d_inrun) {
136 throw FDMultiplexerException("FDMultiplexer::run() is not reentrant!\n");
137 }
138 std::vector<struct pollfd> fds(d_readCallbacks.size() + d_writeCallbacks.size());
139 struct dvpoll dvp;
140 dvp.dp_nfds = d_readCallbacks.size() + d_writeCallbacks.size();
141 dvp.dp_fds = fds.data();
142 dvp.dp_timeout = timeout;
143 int ret=ioctl(d_devpollfd, DP_POLL, &dvp);
144 int err = errno;
145 gettimeofday(now,0); // MANDATORY!
146
147 if(ret < 0 && err!=EINTR) {
148 throw FDMultiplexerException("/dev/poll returned error: "+stringerror(err));
149 }
150
151 if(ret < 1) { // thanks AB!
152 return 0;
153 }
154
155 d_inrun=true;
156 for(int n=0; n < ret; ++n) {
157 d_iter=d_readCallbacks.find(fds.at(n).fd);
158
159 if(d_iter != d_readCallbacks.end()) {
160 d_iter->d_callback(d_iter->d_fd, d_iter->d_parameter);
161 continue; // so we don't refind ourselves as writable!
162 }
163 d_iter=d_writeCallbacks.find(fds.at(n).fd);
164
165 if(d_iter != d_writeCallbacks.end()) {
166 d_iter->d_callback(d_iter->d_fd, d_iter->d_parameter);
167 }
168 }
169
170 d_inrun=false;
171 return ret;
172 }
173
174 #if 0
175 void acceptData(int fd, funcparam_t& parameter)
176 {
177 cout<<"Have data on fd "<<fd<<endl;
178 Socket* sock=funcparam_t_cast<Socket*>(parameter);
179 string packet;
180 IPEndpoint rem;
181 sock->recvFrom(packet, rem);
182 cout<<"Received "<<packet.size()<<" bytes!\n";
183 }
184
185
186 int main()
187 {
188 Socket s(AF_INET, SOCK_DGRAM);
189
190 IPEndpoint loc("0.0.0.0", 2000);
191 s.bind(loc);
192
193 DevPollFDMultiplexer sfm;
194
195 sfm.addReadFD(s.getHandle(), &acceptData, &s);
196
197 for(int n=0; n < 100 ; ++n) {
198 sfm.run();
199 }
200 sfm.removeReadFD(s.getHandle());
201 sfm.removeReadFD(s.getHandle());
202 }
203 #endif
204
205