1*2b15cb3dSCy Schubert /*
2*2b15cb3dSCy Schubert * Copyright 2000-2009 Niels Provos <provos@citi.umich.edu>
3*2b15cb3dSCy Schubert * Copyright 2009-2012 Niels Provos and Nick Mathewson
4*2b15cb3dSCy Schubert *
5*2b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
6*2b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
7*2b15cb3dSCy Schubert * are met:
8*2b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
9*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
10*2b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
11*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
12*2b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
13*2b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
14*2b15cb3dSCy Schubert * derived from this software without specific prior written permission.
15*2b15cb3dSCy Schubert *
16*2b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*2b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*2b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*2b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*2b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*2b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*2b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*2b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*2b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*2b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*2b15cb3dSCy Schubert */
27*2b15cb3dSCy Schubert #include "event2/event-config.h"
28*2b15cb3dSCy Schubert #include "evconfig-private.h"
29*2b15cb3dSCy Schubert
30*2b15cb3dSCy Schubert #ifdef EVENT__HAVE_DEVPOLL
31*2b15cb3dSCy Schubert
32*2b15cb3dSCy Schubert #include <sys/types.h>
33*2b15cb3dSCy Schubert #include <sys/resource.h>
34*2b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
35*2b15cb3dSCy Schubert #include <sys/time.h>
36*2b15cb3dSCy Schubert #endif
37*2b15cb3dSCy Schubert #include <sys/queue.h>
38*2b15cb3dSCy Schubert #include <sys/devpoll.h>
39*2b15cb3dSCy Schubert #include <signal.h>
40*2b15cb3dSCy Schubert #include <stdio.h>
41*2b15cb3dSCy Schubert #include <stdlib.h>
42*2b15cb3dSCy Schubert #include <string.h>
43*2b15cb3dSCy Schubert #include <unistd.h>
44*2b15cb3dSCy Schubert #include <fcntl.h>
45*2b15cb3dSCy Schubert #include <errno.h>
46*2b15cb3dSCy Schubert
47*2b15cb3dSCy Schubert #include "event2/event.h"
48*2b15cb3dSCy Schubert #include "event2/event_struct.h"
49*2b15cb3dSCy Schubert #include "event2/thread.h"
50*2b15cb3dSCy Schubert #include "event-internal.h"
51*2b15cb3dSCy Schubert #include "evsignal-internal.h"
52*2b15cb3dSCy Schubert #include "log-internal.h"
53*2b15cb3dSCy Schubert #include "evmap-internal.h"
54*2b15cb3dSCy Schubert #include "evthread-internal.h"
55*2b15cb3dSCy Schubert
56*2b15cb3dSCy Schubert struct devpollop {
57*2b15cb3dSCy Schubert struct pollfd *events;
58*2b15cb3dSCy Schubert int nevents;
59*2b15cb3dSCy Schubert int dpfd;
60*2b15cb3dSCy Schubert struct pollfd *changes;
61*2b15cb3dSCy Schubert int nchanges;
62*2b15cb3dSCy Schubert };
63*2b15cb3dSCy Schubert
64*2b15cb3dSCy Schubert static void *devpoll_init(struct event_base *);
65*2b15cb3dSCy Schubert static int devpoll_add(struct event_base *, int fd, short old, short events, void *);
66*2b15cb3dSCy Schubert static int devpoll_del(struct event_base *, int fd, short old, short events, void *);
67*2b15cb3dSCy Schubert static int devpoll_dispatch(struct event_base *, struct timeval *);
68*2b15cb3dSCy Schubert static void devpoll_dealloc(struct event_base *);
69*2b15cb3dSCy Schubert
70*2b15cb3dSCy Schubert const struct eventop devpollops = {
71*2b15cb3dSCy Schubert "devpoll",
72*2b15cb3dSCy Schubert devpoll_init,
73*2b15cb3dSCy Schubert devpoll_add,
74*2b15cb3dSCy Schubert devpoll_del,
75*2b15cb3dSCy Schubert devpoll_dispatch,
76*2b15cb3dSCy Schubert devpoll_dealloc,
77*2b15cb3dSCy Schubert 1, /* need reinit */
78*2b15cb3dSCy Schubert EV_FEATURE_FDS|EV_FEATURE_O1,
79*2b15cb3dSCy Schubert 0
80*2b15cb3dSCy Schubert };
81*2b15cb3dSCy Schubert
82*2b15cb3dSCy Schubert #define NEVENT 32000
83*2b15cb3dSCy Schubert
84*2b15cb3dSCy Schubert static int
devpoll_commit(struct devpollop * devpollop)85*2b15cb3dSCy Schubert devpoll_commit(struct devpollop *devpollop)
86*2b15cb3dSCy Schubert {
87*2b15cb3dSCy Schubert /*
88*2b15cb3dSCy Schubert * Due to a bug in Solaris, we have to use pwrite with an offset of 0.
89*2b15cb3dSCy Schubert * Write is limited to 2GB of data, until it will fail.
90*2b15cb3dSCy Schubert */
91*2b15cb3dSCy Schubert if (pwrite(devpollop->dpfd, devpollop->changes,
92*2b15cb3dSCy Schubert sizeof(struct pollfd) * devpollop->nchanges, 0) == -1)
93*2b15cb3dSCy Schubert return (-1);
94*2b15cb3dSCy Schubert
95*2b15cb3dSCy Schubert devpollop->nchanges = 0;
96*2b15cb3dSCy Schubert return (0);
97*2b15cb3dSCy Schubert }
98*2b15cb3dSCy Schubert
99*2b15cb3dSCy Schubert static int
devpoll_queue(struct devpollop * devpollop,int fd,int events)100*2b15cb3dSCy Schubert devpoll_queue(struct devpollop *devpollop, int fd, int events) {
101*2b15cb3dSCy Schubert struct pollfd *pfd;
102*2b15cb3dSCy Schubert
103*2b15cb3dSCy Schubert if (devpollop->nchanges >= devpollop->nevents) {
104*2b15cb3dSCy Schubert /*
105*2b15cb3dSCy Schubert * Change buffer is full, must commit it to /dev/poll before
106*2b15cb3dSCy Schubert * adding more
107*2b15cb3dSCy Schubert */
108*2b15cb3dSCy Schubert if (devpoll_commit(devpollop) != 0)
109*2b15cb3dSCy Schubert return (-1);
110*2b15cb3dSCy Schubert }
111*2b15cb3dSCy Schubert
112*2b15cb3dSCy Schubert pfd = &devpollop->changes[devpollop->nchanges++];
113*2b15cb3dSCy Schubert pfd->fd = fd;
114*2b15cb3dSCy Schubert pfd->events = events;
115*2b15cb3dSCy Schubert pfd->revents = 0;
116*2b15cb3dSCy Schubert
117*2b15cb3dSCy Schubert return (0);
118*2b15cb3dSCy Schubert }
119*2b15cb3dSCy Schubert
120*2b15cb3dSCy Schubert static void *
devpoll_init(struct event_base * base)121*2b15cb3dSCy Schubert devpoll_init(struct event_base *base)
122*2b15cb3dSCy Schubert {
123*2b15cb3dSCy Schubert int dpfd, nfiles = NEVENT;
124*2b15cb3dSCy Schubert struct rlimit rl;
125*2b15cb3dSCy Schubert struct devpollop *devpollop;
126*2b15cb3dSCy Schubert
127*2b15cb3dSCy Schubert if (!(devpollop = mm_calloc(1, sizeof(struct devpollop))))
128*2b15cb3dSCy Schubert return (NULL);
129*2b15cb3dSCy Schubert
130*2b15cb3dSCy Schubert if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
131*2b15cb3dSCy Schubert rl.rlim_cur != RLIM_INFINITY)
132*2b15cb3dSCy Schubert nfiles = rl.rlim_cur;
133*2b15cb3dSCy Schubert
134*2b15cb3dSCy Schubert /* Initialize the kernel queue */
135*2b15cb3dSCy Schubert if ((dpfd = evutil_open_closeonexec_("/dev/poll", O_RDWR, 0)) == -1) {
136*2b15cb3dSCy Schubert event_warn("open: /dev/poll");
137*2b15cb3dSCy Schubert mm_free(devpollop);
138*2b15cb3dSCy Schubert return (NULL);
139*2b15cb3dSCy Schubert }
140*2b15cb3dSCy Schubert
141*2b15cb3dSCy Schubert devpollop->dpfd = dpfd;
142*2b15cb3dSCy Schubert
143*2b15cb3dSCy Schubert /* Initialize fields */
144*2b15cb3dSCy Schubert /* FIXME: allocating 'nfiles' worth of space here can be
145*2b15cb3dSCy Schubert * expensive and unnecessary. See how epoll.c does it instead. */
146*2b15cb3dSCy Schubert devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd));
147*2b15cb3dSCy Schubert if (devpollop->events == NULL) {
148*2b15cb3dSCy Schubert mm_free(devpollop);
149*2b15cb3dSCy Schubert close(dpfd);
150*2b15cb3dSCy Schubert return (NULL);
151*2b15cb3dSCy Schubert }
152*2b15cb3dSCy Schubert devpollop->nevents = nfiles;
153*2b15cb3dSCy Schubert
154*2b15cb3dSCy Schubert devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd));
155*2b15cb3dSCy Schubert if (devpollop->changes == NULL) {
156*2b15cb3dSCy Schubert mm_free(devpollop->events);
157*2b15cb3dSCy Schubert mm_free(devpollop);
158*2b15cb3dSCy Schubert close(dpfd);
159*2b15cb3dSCy Schubert return (NULL);
160*2b15cb3dSCy Schubert }
161*2b15cb3dSCy Schubert
162*2b15cb3dSCy Schubert evsig_init_(base);
163*2b15cb3dSCy Schubert
164*2b15cb3dSCy Schubert return (devpollop);
165*2b15cb3dSCy Schubert }
166*2b15cb3dSCy Schubert
167*2b15cb3dSCy Schubert static int
devpoll_dispatch(struct event_base * base,struct timeval * tv)168*2b15cb3dSCy Schubert devpoll_dispatch(struct event_base *base, struct timeval *tv)
169*2b15cb3dSCy Schubert {
170*2b15cb3dSCy Schubert struct devpollop *devpollop = base->evbase;
171*2b15cb3dSCy Schubert struct pollfd *events = devpollop->events;
172*2b15cb3dSCy Schubert struct dvpoll dvp;
173*2b15cb3dSCy Schubert int i, res, timeout = -1;
174*2b15cb3dSCy Schubert
175*2b15cb3dSCy Schubert if (devpollop->nchanges)
176*2b15cb3dSCy Schubert devpoll_commit(devpollop);
177*2b15cb3dSCy Schubert
178*2b15cb3dSCy Schubert if (tv != NULL)
179*2b15cb3dSCy Schubert timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
180*2b15cb3dSCy Schubert
181*2b15cb3dSCy Schubert dvp.dp_fds = devpollop->events;
182*2b15cb3dSCy Schubert dvp.dp_nfds = devpollop->nevents;
183*2b15cb3dSCy Schubert dvp.dp_timeout = timeout;
184*2b15cb3dSCy Schubert
185*2b15cb3dSCy Schubert EVBASE_RELEASE_LOCK(base, th_base_lock);
186*2b15cb3dSCy Schubert
187*2b15cb3dSCy Schubert res = ioctl(devpollop->dpfd, DP_POLL, &dvp);
188*2b15cb3dSCy Schubert
189*2b15cb3dSCy Schubert EVBASE_ACQUIRE_LOCK(base, th_base_lock);
190*2b15cb3dSCy Schubert
191*2b15cb3dSCy Schubert if (res == -1) {
192*2b15cb3dSCy Schubert if (errno != EINTR) {
193*2b15cb3dSCy Schubert event_warn("ioctl: DP_POLL");
194*2b15cb3dSCy Schubert return (-1);
195*2b15cb3dSCy Schubert }
196*2b15cb3dSCy Schubert
197*2b15cb3dSCy Schubert return (0);
198*2b15cb3dSCy Schubert }
199*2b15cb3dSCy Schubert
200*2b15cb3dSCy Schubert event_debug(("%s: devpoll_wait reports %d", __func__, res));
201*2b15cb3dSCy Schubert
202*2b15cb3dSCy Schubert for (i = 0; i < res; i++) {
203*2b15cb3dSCy Schubert int which = 0;
204*2b15cb3dSCy Schubert int what = events[i].revents;
205*2b15cb3dSCy Schubert
206*2b15cb3dSCy Schubert if (what & POLLHUP)
207*2b15cb3dSCy Schubert what |= POLLIN | POLLOUT;
208*2b15cb3dSCy Schubert else if (what & POLLERR)
209*2b15cb3dSCy Schubert what |= POLLIN | POLLOUT;
210*2b15cb3dSCy Schubert
211*2b15cb3dSCy Schubert if (what & POLLIN)
212*2b15cb3dSCy Schubert which |= EV_READ;
213*2b15cb3dSCy Schubert if (what & POLLOUT)
214*2b15cb3dSCy Schubert which |= EV_WRITE;
215*2b15cb3dSCy Schubert
216*2b15cb3dSCy Schubert if (!which)
217*2b15cb3dSCy Schubert continue;
218*2b15cb3dSCy Schubert
219*2b15cb3dSCy Schubert /* XXX(niels): not sure if this works for devpoll */
220*2b15cb3dSCy Schubert evmap_io_active_(base, events[i].fd, which);
221*2b15cb3dSCy Schubert }
222*2b15cb3dSCy Schubert
223*2b15cb3dSCy Schubert return (0);
224*2b15cb3dSCy Schubert }
225*2b15cb3dSCy Schubert
226*2b15cb3dSCy Schubert
227*2b15cb3dSCy Schubert static int
devpoll_add(struct event_base * base,int fd,short old,short events,void * p)228*2b15cb3dSCy Schubert devpoll_add(struct event_base *base, int fd, short old, short events, void *p)
229*2b15cb3dSCy Schubert {
230*2b15cb3dSCy Schubert struct devpollop *devpollop = base->evbase;
231*2b15cb3dSCy Schubert int res;
232*2b15cb3dSCy Schubert (void)p;
233*2b15cb3dSCy Schubert
234*2b15cb3dSCy Schubert /*
235*2b15cb3dSCy Schubert * It's not necessary to OR the existing read/write events that we
236*2b15cb3dSCy Schubert * are currently interested in with the new event we are adding.
237*2b15cb3dSCy Schubert * The /dev/poll driver ORs any new events with the existing events
238*2b15cb3dSCy Schubert * that it has cached for the fd.
239*2b15cb3dSCy Schubert */
240*2b15cb3dSCy Schubert
241*2b15cb3dSCy Schubert res = 0;
242*2b15cb3dSCy Schubert if (events & EV_READ)
243*2b15cb3dSCy Schubert res |= POLLIN;
244*2b15cb3dSCy Schubert if (events & EV_WRITE)
245*2b15cb3dSCy Schubert res |= POLLOUT;
246*2b15cb3dSCy Schubert
247*2b15cb3dSCy Schubert if (devpoll_queue(devpollop, fd, res) != 0)
248*2b15cb3dSCy Schubert return (-1);
249*2b15cb3dSCy Schubert
250*2b15cb3dSCy Schubert return (0);
251*2b15cb3dSCy Schubert }
252*2b15cb3dSCy Schubert
253*2b15cb3dSCy Schubert static int
devpoll_del(struct event_base * base,int fd,short old,short events,void * p)254*2b15cb3dSCy Schubert devpoll_del(struct event_base *base, int fd, short old, short events, void *p)
255*2b15cb3dSCy Schubert {
256*2b15cb3dSCy Schubert struct devpollop *devpollop = base->evbase;
257*2b15cb3dSCy Schubert int res;
258*2b15cb3dSCy Schubert (void)p;
259*2b15cb3dSCy Schubert
260*2b15cb3dSCy Schubert res = 0;
261*2b15cb3dSCy Schubert if (events & EV_READ)
262*2b15cb3dSCy Schubert res |= POLLIN;
263*2b15cb3dSCy Schubert if (events & EV_WRITE)
264*2b15cb3dSCy Schubert res |= POLLOUT;
265*2b15cb3dSCy Schubert
266*2b15cb3dSCy Schubert /*
267*2b15cb3dSCy Schubert * The only way to remove an fd from the /dev/poll monitored set is
268*2b15cb3dSCy Schubert * to use POLLREMOVE by itself. This removes ALL events for the fd
269*2b15cb3dSCy Schubert * provided so if we care about two events and are only removing one
270*2b15cb3dSCy Schubert * we must re-add the other event after POLLREMOVE.
271*2b15cb3dSCy Schubert */
272*2b15cb3dSCy Schubert
273*2b15cb3dSCy Schubert if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0)
274*2b15cb3dSCy Schubert return (-1);
275*2b15cb3dSCy Schubert
276*2b15cb3dSCy Schubert if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
277*2b15cb3dSCy Schubert /*
278*2b15cb3dSCy Schubert * We're not deleting all events, so we must resubmit the
279*2b15cb3dSCy Schubert * event that we are still interested in if one exists.
280*2b15cb3dSCy Schubert */
281*2b15cb3dSCy Schubert
282*2b15cb3dSCy Schubert if ((res & POLLIN) && (old & EV_WRITE)) {
283*2b15cb3dSCy Schubert /* Deleting read, still care about write */
284*2b15cb3dSCy Schubert devpoll_queue(devpollop, fd, POLLOUT);
285*2b15cb3dSCy Schubert } else if ((res & POLLOUT) && (old & EV_READ)) {
286*2b15cb3dSCy Schubert /* Deleting write, still care about read */
287*2b15cb3dSCy Schubert devpoll_queue(devpollop, fd, POLLIN);
288*2b15cb3dSCy Schubert }
289*2b15cb3dSCy Schubert }
290*2b15cb3dSCy Schubert
291*2b15cb3dSCy Schubert return (0);
292*2b15cb3dSCy Schubert }
293*2b15cb3dSCy Schubert
294*2b15cb3dSCy Schubert static void
devpoll_dealloc(struct event_base * base)295*2b15cb3dSCy Schubert devpoll_dealloc(struct event_base *base)
296*2b15cb3dSCy Schubert {
297*2b15cb3dSCy Schubert struct devpollop *devpollop = base->evbase;
298*2b15cb3dSCy Schubert
299*2b15cb3dSCy Schubert evsig_dealloc_(base);
300*2b15cb3dSCy Schubert if (devpollop->events)
301*2b15cb3dSCy Schubert mm_free(devpollop->events);
302*2b15cb3dSCy Schubert if (devpollop->changes)
303*2b15cb3dSCy Schubert mm_free(devpollop->changes);
304*2b15cb3dSCy Schubert if (devpollop->dpfd >= 0)
305*2b15cb3dSCy Schubert close(devpollop->dpfd);
306*2b15cb3dSCy Schubert
307*2b15cb3dSCy Schubert memset(devpollop, 0, sizeof(struct devpollop));
308*2b15cb3dSCy Schubert mm_free(devpollop);
309*2b15cb3dSCy Schubert }
310*2b15cb3dSCy Schubert
311*2b15cb3dSCy Schubert #endif /* EVENT__HAVE_DEVPOLL */
312