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