]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/devpollmplexer.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
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.
29 #include <sys/signal.h>
30 #include <sys/devpoll.h>
37 #include "namespaces.hh"
39 class DevPollFDMultiplexer
: public FDMultiplexer
42 DevPollFDMultiplexer();
43 virtual ~DevPollFDMultiplexer()
48 virtual int run(struct timeval
* tv
, int timeout
=500) override
;
49 virtual void getAvailableFDs(std::vector
<int>& fds
, int timeout
) override
;
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
62 static FDMultiplexer
* makeDevPoll()
64 return new DevPollFDMultiplexer();
67 static struct DevPollRegisterOurselves
69 DevPollRegisterOurselves() {
70 FDMultiplexer::getMultiplexerMap().insert(make_pair(0, &makeDevPoll
)); // priority 0!
75 //int DevPollFDMultiplexer::s_maxevents=1024;
76 DevPollFDMultiplexer::DevPollFDMultiplexer()
78 d_devpollfd
=open("/dev/poll", O_RDWR
);
80 throw FDMultiplexerException("Setting up /dev/poll: "+stringerror());
84 void DevPollFDMultiplexer::addFD(callbackmap_t
& cbmap
, int fd
, callbackfunc_t toDo
, const funcparam_t
& parameter
, const struct timeval
* ttd
)
86 accountingAddFD(cbmap
, fd
, toDo
, parameter
, ttd
);
90 devent
.events
= (&cbmap
== &d_readCallbacks
) ? POLLIN
: POLLOUT
;
93 if(write(d_devpollfd
, &devent
, sizeof(devent
)) != sizeof(devent
)) {
95 throw FDMultiplexerException("Adding fd to /dev/poll/ set: "+stringerror());
99 void DevPollFDMultiplexer::removeFD(callbackmap_t
& cbmap
, int fd
)
102 throw FDMultiplexerException("Tried to remove unlisted fd "+std::to_string(fd
)+ " from multiplexer");
104 struct pollfd devent
;
106 devent
.events
= POLLREMOVE
;
109 if(write(d_devpollfd
, &devent
, sizeof(devent
)) != sizeof(devent
)) {
111 throw FDMultiplexerException("Removing fd from epoll set: "+stringerror());
115 void DevPollFDMultiplexer::getAvailableFDs(std::vector
<int>& fds
, int timeout
)
117 std::vector
<struct pollfd
> pollfds(d_readCallbacks
.size() + d_writeCallbacks
.size());
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
);
124 if(ret
< 0 && errno
!=EINTR
) {
125 throw FDMultiplexerException("/dev/poll returned error: "+stringerror());
128 for(int n
=0; n
< ret
; ++n
) {
129 fds
.push_back(pollfds
.at(n
).fd
);
133 int DevPollFDMultiplexer::run(struct timeval
* now
, int timeout
)
136 throw FDMultiplexerException("FDMultiplexer::run() is not reentrant!\n");
138 std::vector
<struct pollfd
> fds(d_readCallbacks
.size() + d_writeCallbacks
.size());
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
);
145 gettimeofday(now
,0); // MANDATORY!
147 if(ret
< 0 && err
!=EINTR
) {
148 throw FDMultiplexerException("/dev/poll returned error: "+stringerror(err
));
151 if(ret
< 1) { // thanks AB!
156 for(int n
=0; n
< ret
; ++n
) {
157 d_iter
=d_readCallbacks
.find(fds
.at(n
).fd
);
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!
163 d_iter
=d_writeCallbacks
.find(fds
.at(n
).fd
);
165 if(d_iter
!= d_writeCallbacks
.end()) {
166 d_iter
->d_callback(d_iter
->d_fd
, d_iter
->d_parameter
);
175 void acceptData(int fd
, funcparam_t
& parameter
)
177 cout
<<"Have data on fd "<<fd
<<endl
;
178 Socket
* sock
=funcparam_t_cast
<Socket
*>(parameter
);
181 sock
->recvFrom(packet
, rem
);
182 cout
<<"Received "<<packet
.size()<<" bytes!\n";
188 Socket
s(AF_INET
, SOCK_DGRAM
);
190 IPEndpoint
loc("0.0.0.0", 2000);
193 DevPollFDMultiplexer sfm
;
195 sfm
.addReadFD(s
.getHandle(), &acceptData
, &s
);
197 for(int n
=0; n
< 100 ; ++n
) {
200 sfm
.removeReadFD(s
.getHandle());
201 sfm
.removeReadFD(s
.getHandle());