]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/devpollmplexer.cc
rec: Set ecs-ipv4-cache-bits and ecs-ipv6-cache-bits in the tests
[thirdparty/pdns.git] / pdns / devpollmplexer.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
e22156da
JJS
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>
781d550a
BH
30#include <sys/devpoll.h>
31#include "mplexer.hh"
32#include "sstuff.hh"
33#include <iostream>
34#include <unistd.h>
35#include "misc.hh"
781d550a
BH
36#include "syncres.hh"
37
10f4eea8 38#include "namespaces.hh"
781d550a
BH
39
40class DevPollFDMultiplexer : public FDMultiplexer
41{
42public:
43 DevPollFDMultiplexer();
44 virtual ~DevPollFDMultiplexer()
45 {
46 close(d_devpollfd);
47 }
48
0e663c3b 49 virtual int run(struct timeval* tv, int timeout=500);
781d550a
BH
50
51 virtual void addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, const funcparam_t& parameter);
52 virtual void removeFD(callbackmap_t& cbmap, int fd);
53 string getName()
54 {
55 return "/dev/poll";
56 }
57private:
58 int d_devpollfd;
59};
60
61
62static FDMultiplexer* makeDevPoll()
63{
64 return new DevPollFDMultiplexer();
65}
66
67static 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;
76DevPollFDMultiplexer::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
84void DevPollFDMultiplexer::addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, const funcparam_t& parameter)
85{
86 accountingAddFD(cbmap, fd, toDo, parameter);
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
99void DevPollFDMultiplexer::removeFD(callbackmap_t& cbmap, int fd)
100{
101 if(!cbmap.erase(fd))
335da0ba 102 throw FDMultiplexerException("Tried to remove unlisted fd "+std::to_string(fd)+ " from multiplexer");
781d550a
BH
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
0e663c3b 115int DevPollFDMultiplexer::run(struct timeval* now, int timeout)
781d550a
BH
116{
117 if(d_inrun) {
118 throw FDMultiplexerException("FDMultiplexer::run() is not reentrant!\n");
119 }
120 struct dvpoll dvp;
121 dvp.dp_nfds = d_readCallbacks.size() + d_writeCallbacks.size();
122 dvp.dp_fds = new pollfd[dvp.dp_nfds];
0e663c3b 123 dvp.dp_timeout = timeout;
781d550a 124 int ret=ioctl(d_devpollfd, DP_POLL, &dvp);
55df795b 125 gettimeofday(now,0); // MANDATORY!
781d550a 126
6b1933ac
JJS
127 if(ret < 0 && errno!=EINTR) {
128 delete[] dvp.dp_fds;
781d550a 129 throw FDMultiplexerException("/dev/poll returned error: "+stringerror());
6b1933ac 130 }
781d550a 131
6b1933ac
JJS
132 if(ret < 1) { // thanks AB!
133 delete[] dvp.dp_fds;
781d550a 134 return 0;
6b1933ac 135 }
781d550a
BH
136
137 d_inrun=true;
138 for(int n=0; n < ret; ++n) {
139 d_iter=d_readCallbacks.find(dvp.dp_fds[n].fd);
140
141 if(d_iter != d_readCallbacks.end()) {
142 d_iter->second.d_callback(d_iter->first, d_iter->second.d_parameter);
143 continue; // so we don't refind ourselves as writable!
144 }
145 d_iter=d_writeCallbacks.find(dvp.dp_fds[n].fd);
146
147 if(d_iter != d_writeCallbacks.end()) {
148 d_iter->second.d_callback(d_iter->first, d_iter->second.d_parameter);
149 }
150 }
151 delete[] dvp.dp_fds;
152 d_inrun=false;
0e663c3b 153 return ret;
781d550a
BH
154}
155
156#if 0
157void acceptData(int fd, funcparam_t& parameter)
158{
159 cout<<"Have data on fd "<<fd<<endl;
160 Socket* sock=funcparam_t_cast<Socket*>(parameter);
161 string packet;
162 IPEndpoint rem;
163 sock->recvFrom(packet, rem);
164 cout<<"Received "<<packet.size()<<" bytes!\n";
165}
166
167
168int main()
169{
a5794017 170 Socket s(AF_INET, SOCK_DGRAM);
781d550a
BH
171
172 IPEndpoint loc("0.0.0.0", 2000);
173 s.bind(loc);
174
175 DevPollFDMultiplexer sfm;
176
177 sfm.addReadFD(s.getHandle(), &acceptData, &s);
178
179 for(int n=0; n < 100 ; ++n) {
180 sfm.run();
181 }
182 sfm.removeReadFD(s.getHandle());
183 sfm.removeReadFD(s.getHandle());
184}
185#endif
186
187