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