]>
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>
38 #include "namespaces.hh"
39 #include "namespaces.hh"
41 class DevPollFDMultiplexer
: public FDMultiplexer
44 DevPollFDMultiplexer();
45 virtual ~DevPollFDMultiplexer()
50 virtual int run(struct timeval
* tv
);
52 virtual void addFD(callbackmap_t
& cbmap
, int fd
, callbackfunc_t toDo
, const funcparam_t
& parameter
);
53 virtual void removeFD(callbackmap_t
& cbmap
, int fd
);
63 static FDMultiplexer
* makeDevPoll()
65 return new DevPollFDMultiplexer();
68 static struct DevPollRegisterOurselves
70 DevPollRegisterOurselves() {
71 FDMultiplexer::getMultiplexerMap().insert(make_pair(0, &makeDevPoll
)); // priority 0!
76 //int DevPollFDMultiplexer::s_maxevents=1024;
77 DevPollFDMultiplexer::DevPollFDMultiplexer()
79 d_devpollfd
=open("/dev/poll", O_RDWR
);
81 throw FDMultiplexerException("Setting up /dev/poll: "+stringerror());
85 void DevPollFDMultiplexer::addFD(callbackmap_t
& cbmap
, int fd
, callbackfunc_t toDo
, const funcparam_t
& parameter
)
87 accountingAddFD(cbmap
, fd
, toDo
, parameter
);
91 devent
.events
= (&cbmap
== &d_readCallbacks
) ? POLLIN
: POLLOUT
;
94 if(write(d_devpollfd
, &devent
, sizeof(devent
)) != sizeof(devent
)) {
96 throw FDMultiplexerException("Adding fd to /dev/poll/ set: "+stringerror());
100 void DevPollFDMultiplexer::removeFD(callbackmap_t
& cbmap
, int fd
)
103 throw FDMultiplexerException("Tried to remove unlisted fd "+std::to_string(fd
)+ " from multiplexer");
105 struct pollfd devent
;
107 devent
.events
= POLLREMOVE
;
110 if(write(d_devpollfd
, &devent
, sizeof(devent
)) != sizeof(devent
)) {
112 throw FDMultiplexerException("Removing fd from epoll set: "+stringerror());
116 int DevPollFDMultiplexer::run(struct timeval
* now
)
119 throw FDMultiplexerException("FDMultiplexer::run() is not reentrant!\n");
122 dvp
.dp_nfds
= d_readCallbacks
.size() + d_writeCallbacks
.size();
123 dvp
.dp_fds
= new pollfd
[dvp
.dp_nfds
];
124 dvp
.dp_timeout
= 500;
125 int ret
=ioctl(d_devpollfd
, DP_POLL
, &dvp
);
126 gettimeofday(now
,0); // MANDATORY!
128 if(ret
< 0 && errno
!=EINTR
) {
130 throw FDMultiplexerException("/dev/poll returned error: "+stringerror());
133 if(ret
< 1) { // thanks AB!
139 for(int n
=0; n
< ret
; ++n
) {
140 d_iter
=d_readCallbacks
.find(dvp
.dp_fds
[n
].fd
);
142 if(d_iter
!= d_readCallbacks
.end()) {
143 d_iter
->second
.d_callback(d_iter
->first
, d_iter
->second
.d_parameter
);
144 continue; // so we don't refind ourselves as writable!
146 d_iter
=d_writeCallbacks
.find(dvp
.dp_fds
[n
].fd
);
148 if(d_iter
!= d_writeCallbacks
.end()) {
149 d_iter
->second
.d_callback(d_iter
->first
, d_iter
->second
.d_parameter
);
158 void acceptData(int fd
, funcparam_t
& parameter
)
160 cout
<<"Have data on fd "<<fd
<<endl
;
161 Socket
* sock
=funcparam_t_cast
<Socket
*>(parameter
);
164 sock
->recvFrom(packet
, rem
);
165 cout
<<"Received "<<packet
.size()<<" bytes!\n";
171 Socket
s(AF_INET
, SOCK_DGRAM
);
173 IPEndpoint
loc("0.0.0.0", 2000);
176 DevPollFDMultiplexer sfm
;
178 sfm
.addReadFD(s
.getHandle(), &acceptData
, &s
);
180 for(int n
=0; n
< 100 ; ++n
) {
183 sfm
.removeReadFD(s
.getHandle());
184 sfm
.removeReadFD(s
.getHandle());