Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 312 lines (256 sloc) 7.889 kb
5de7f4e @provos devpoll support. weeh
provos authored
1 /*
b85b710 Update copyright statements to reflect the facts that:
Nick Mathewson authored
2 * Copyright 2000-2009 Niels Provos <provos@citi.umich.edu>
e49e289 Update copyright notices to 2012
Nick Mathewson authored
3 * Copyright 2009-2012 Niels Provos and Nick Mathewson
5de7f4e @provos devpoll support. weeh
provos authored
4 *
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.
15 *
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.
26 */
ec347b9 Move event-config.h to include/event2
Nick Mathewson authored
27 #include "event2/event-config.h"
0915ca0 @kev009 Include evconfig-private.h in internal files for great good.
kev009 authored
28 #include "evconfig-private.h"
5de7f4e @provos devpoll support. weeh
provos authored
29
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
30 #ifdef EVENT__HAVE_DEVPOLL
76d4c92 @rosslagerwall Put #ifdef around some files to support alternate build systems.
authored
31
5de7f4e @provos devpoll support. weeh
provos authored
32 #include <sys/types.h>
33 #include <sys/resource.h>
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
34 #ifdef EVENT__HAVE_SYS_TIME_H
5de7f4e @provos devpoll support. weeh
provos authored
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>
46
a2d4a06 r15228@tombo: nickm | 2008-04-17 15:27:39 -0400
Nick Mathewson authored
47 #include "event2/event.h"
48 #include "event2/event_struct.h"
fee2c77 Fix compilation of devpoll.c by adding missing thread includes.
Dagobert Michelsen authored
49 #include "event2/thread.h"
aa5c806 @provos make it compile on solaris; from Andrei Nigmatulin
provos authored
50 #include "event-internal.h"
169321c Rename four internal headers to follow the -internal.h convention.
Nick Mathewson authored
51 #include "evsignal-internal.h"
52 #include "log-internal.h"
53 #include "evmap-internal.h"
fee2c77 Fix compilation of devpoll.c by adding missing thread includes.
Dagobert Michelsen authored
54 #include "evthread-internal.h"
5de7f4e @provos devpoll support. weeh
provos authored
55
56 struct devpollop {
57 struct pollfd *events;
58 int nevents;
59 int dpfd;
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
60 struct pollfd *changes;
61 int nchanges;
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
62 };
5de7f4e @provos devpoll support. weeh
provos authored
63
2c2618d more whitespace normalization
Nick Mathewson authored
64 static void *devpoll_init(struct event_base *);
554e149 Move per-fd info from eventops into evmap. Not done for win32.c yet.
Nick Mathewson authored
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 *);
2c2618d more whitespace normalization
Nick Mathewson authored
67 static int devpoll_dispatch(struct event_base *, struct timeval *);
68 static void devpoll_dealloc(struct event_base *);
5de7f4e @provos devpoll support. weeh
provos authored
69
808f00e @provos constify structs; from Andrei Nigmatulin
provos authored
70 const struct eventop devpollops = {
5de7f4e @provos devpoll support. weeh
provos authored
71 "devpoll",
72 devpoll_init,
73 devpoll_add,
74 devpoll_del,
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
75 devpoll_dispatch,
f38aec8 @provos devpoll and evport need reinit; tested by W.C.A. Wijngaards
provos authored
76 devpoll_dealloc,
0596592 Add new functions to access backends by their features and to query the ...
Nick Mathewson authored
77 1, /* need reinit */
78 EV_FEATURE_FDS|EV_FEATURE_O1,
554e149 Move per-fd info from eventops into evmap. Not done for win32.c yet.
Nick Mathewson authored
79 0
5de7f4e @provos devpoll support. weeh
provos authored
80 };
81
82 #define NEVENT 32000
83
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
84 static int
85 devpoll_commit(struct devpollop *devpollop)
86 {
87 /*
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.
90 */
91 if (pwrite(devpollop->dpfd, devpollop->changes,
92 sizeof(struct pollfd) * devpollop->nchanges, 0) == -1)
4faeaea Clean up formatting: function/keyword spacing consistency.
Nick Mathewson authored
93 return (-1);
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
94
95 devpollop->nchanges = 0;
4faeaea Clean up formatting: function/keyword spacing consistency.
Nick Mathewson authored
96 return (0);
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
97 }
98
99 static int
100 devpoll_queue(struct devpollop *devpollop, int fd, int events) {
101 struct pollfd *pfd;
102
103 if (devpollop->nchanges >= devpollop->nevents) {
104 /*
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
105 * Change buffer is full, must commit it to /dev/poll before
106 * adding more
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
107 */
108 if (devpoll_commit(devpollop) != 0)
2c2618d more whitespace normalization
Nick Mathewson authored
109 return (-1);
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
110 }
111
112 pfd = &devpollop->changes[devpollop->nchanges++];
113 pfd->fd = fd;
114 pfd->events = events;
115 pfd->revents = 0;
116
4faeaea Clean up formatting: function/keyword spacing consistency.
Nick Mathewson authored
117 return (0);
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
118 }
119
ca42671 @provos make event methods static so that they are not exported; from Andrei Nig...
provos authored
120 static void *
41b7cbc @provos more the signal base into the event base; this removes global state and ...
provos authored
121 devpoll_init(struct event_base *base)
5de7f4e @provos devpoll support. weeh
provos authored
122 {
123 int dpfd, nfiles = NEVENT;
124 struct rlimit rl;
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
125 struct devpollop *devpollop;
5de7f4e @provos devpoll support. weeh
provos authored
126
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
127 if (!(devpollop = mm_calloc(1, sizeof(struct devpollop))))
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
128 return (NULL);
5de7f4e @provos devpoll support. weeh
provos authored
129
130 if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
131 rl.rlim_cur != RLIM_INFINITY)
1aa6826 @provos fix off by one errors in devpoll; from Ian Bell
provos authored
132 nfiles = rl.rlim_cur;
5de7f4e @provos devpoll support. weeh
provos authored
133
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
134 /* Initialize the kernel queue */
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
135 if ((dpfd = evutil_open_closeonexec_("/dev/poll", O_RDWR, 0)) == -1) {
e5bbd40 Clean up formatting: use tabs, not 8-spaces, to indent.
Nick Mathewson authored
136 event_warn("open: /dev/poll");
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
137 mm_free(devpollop);
5de7f4e @provos devpoll support. weeh
provos authored
138 return (NULL);
139 }
140
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
141 devpollop->dpfd = dpfd;
5de7f4e @provos devpoll support. weeh
provos authored
142
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
143 /* Initialize fields */
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
144 /* FIXME: allocating 'nfiles' worth of space here can be
145 * expensive and unnecessary. See how epoll.c does it instead. */
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
146 devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd));
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
147 if (devpollop->events == NULL) {
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
148 mm_free(devpollop);
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
149 close(dpfd);
5de7f4e @provos devpoll support. weeh
provos authored
150 return (NULL);
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
151 }
152 devpollop->nevents = nfiles;
5de7f4e @provos devpoll support. weeh
provos authored
153
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
154 devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd));
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
155 if (devpollop->changes == NULL) {
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
156 mm_free(devpollop->events);
157 mm_free(devpollop);
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
158 close(dpfd);
159 return (NULL);
160 }
161
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
162 evsig_init_(base);
5de7f4e @provos devpoll support. weeh
provos authored
163
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
164 return (devpollop);
5de7f4e @provos devpoll support. weeh
provos authored
165 }
166
ca42671 @provos make event methods static so that they are not exported; from Andrei Nig...
provos authored
167 static int
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
168 devpoll_dispatch(struct event_base *base, struct timeval *tv)
5de7f4e @provos devpoll support. weeh
provos authored
169 {
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
170 struct devpollop *devpollop = base->evbase;
5de7f4e @provos devpoll support. weeh
provos authored
171 struct pollfd *events = devpollop->events;
172 struct dvpoll dvp;
3ad6b47 @provos make clock_monotonic work; do not use default timeout;
provos authored
173 int i, res, timeout = -1;
5de7f4e @provos devpoll support. weeh
provos authored
174
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
175 if (devpollop->nchanges)
176 devpoll_commit(devpollop);
177
3ad6b47 @provos make clock_monotonic work; do not use default timeout;
provos authored
178 if (tv != NULL)
179 timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
5de7f4e @provos devpoll support. weeh
provos authored
180
181 dvp.dp_fds = devpollop->events;
182 dvp.dp_nfds = devpollop->nevents;
183 dvp.dp_timeout = timeout;
184
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored
185 EVBASE_RELEASE_LOCK(base, th_base_lock);
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
186
5de7f4e @provos devpoll support. weeh
provos authored
187 res = ioctl(devpollop->dpfd, DP_POLL, &dvp);
188
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored
189 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
190
5de7f4e @provos devpoll support. weeh
provos authored
191 if (res == -1) {
192 if (errno != EINTR) {
fbdaf3a @provos debugging callbacks from Nick Mathewson <nickm@freehaven.net>
provos authored
193 event_warn("ioctl: DP_POLL");
5de7f4e @provos devpoll support. weeh
provos authored
194 return (-1);
195 }
196
197 return (0);
41b7cbc @provos more the signal base into the event base; this removes global state and ...
provos authored
198 }
5de7f4e @provos devpoll support. weeh
provos authored
199
fbdaf3a @provos debugging callbacks from Nick Mathewson <nickm@freehaven.net>
provos authored
200 event_debug(("%s: devpoll_wait reports %d", __func__, res));
5de7f4e @provos devpoll support. weeh
provos authored
201
202 for (i = 0; i < res; i++) {
203 int which = 0;
204 int what = events[i].revents;
205
e5bbd40 Clean up formatting: use tabs, not 8-spaces, to indent.
Nick Mathewson authored
206 if (what & POLLHUP)
207 what |= POLLIN | POLLOUT;
208 else if (what & POLLERR)
209 what |= POLLIN | POLLOUT;
5de7f4e @provos devpoll support. weeh
provos authored
210
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
211 if (what & POLLIN)
5de7f4e @provos devpoll support. weeh
provos authored
212 which |= EV_READ;
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
213 if (what & POLLOUT)
5de7f4e @provos devpoll support. weeh
provos authored
214 which |= EV_WRITE;
215
216 if (!which)
217 continue;
218
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
219 /* XXX(niels): not sure if this works for devpoll */
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
220 evmap_io_active_(base, events[i].fd, which);
5de7f4e @provos devpoll support. weeh
provos authored
221 }
222
223 return (0);
224 }
225
226
ca42671 @provos make event methods static so that they are not exported; from Andrei Nig...
provos authored
227 static int
554e149 Move per-fd info from eventops into evmap. Not done for win32.c yet.
Nick Mathewson authored
228 devpoll_add(struct event_base *base, int fd, short old, short events, void *p)
5de7f4e @provos devpoll support. weeh
provos authored
229 {
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
230 struct devpollop *devpollop = base->evbase;
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
231 int res;
554e149 Move per-fd info from eventops into evmap. Not done for win32.c yet.
Nick Mathewson authored
232 (void)p;
5de7f4e @provos devpoll support. weeh
provos authored
233
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
234 /*
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
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.
239 */
240
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
241 res = 0;
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
242 if (events & EV_READ)
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
243 res |= POLLIN;
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
244 if (events & EV_WRITE)
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
245 res |= POLLOUT;
5de7f4e @provos devpoll support. weeh
provos authored
246
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
247 if (devpoll_queue(devpollop, fd, res) != 0)
2c2618d more whitespace normalization
Nick Mathewson authored
248 return (-1);
5de7f4e @provos devpoll support. weeh
provos authored
249
250 return (0);
251 }
252
ca42671 @provos make event methods static so that they are not exported; from Andrei Nig...
provos authored
253 static int
554e149 Move per-fd info from eventops into evmap. Not done for win32.c yet.
Nick Mathewson authored
254 devpoll_del(struct event_base *base, int fd, short old, short events, void *p)
5de7f4e @provos devpoll support. weeh
provos authored
255 {
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
256 struct devpollop *devpollop = base->evbase;
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
257 int res;
554e149 Move per-fd info from eventops into evmap. Not done for win32.c yet.
Nick Mathewson authored
258 (void)p;
5de7f4e @provos devpoll support. weeh
provos authored
259
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
260 res = 0;
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
261 if (events & EV_READ)
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
262 res |= POLLIN;
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
263 if (events & EV_WRITE)
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
264 res |= POLLOUT;
5de7f4e @provos devpoll support. weeh
provos authored
265
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
266 /*
267 * The only way to remove an fd from the /dev/poll monitored set is
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
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
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
270 * we must re-add the other event after POLLREMOVE.
271 */
272
273 if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0)
2c2618d more whitespace normalization
Nick Mathewson authored
274 return (-1);
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
275
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
276 if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
277 /*
278 * We're not deleting all events, so we must resubmit the
279 * event that we are still interested in if one exists.
280 */
281
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
282 if ((res & POLLIN) && (old & EV_WRITE)) {
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
283 /* Deleting read, still care about write */
284 devpoll_queue(devpollop, fd, POLLOUT);
fed2fd0 @provos try to make devpoll work again; i dont have access to a machine
provos authored
285 } else if ((res & POLLOUT) && (old & EV_READ)) {
d85d47f @provos devpoll improvements from Andrew Danforth <adanforth@gmail.com>
provos authored
286 /* Deleting write, still care about read */
287 devpoll_queue(devpollop, fd, POLLIN);
5de7f4e @provos devpoll support. weeh
provos authored
288 }
289 }
290
291 return (0);
292 }
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
293
ca42671 @provos make event methods static so that they are not exported; from Andrei Nig...
provos authored
294 static void
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
295 devpoll_dealloc(struct event_base *base)
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
296 {
02b2b4d @provos Restructure the event backends so that they do not need to keep track of...
provos authored
297 struct devpollop *devpollop = base->evbase;
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
298
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
299 evsig_dealloc_(base);
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
300 if (devpollop->events)
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
301 mm_free(devpollop->events);
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
302 if (devpollop->changes)
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
303 mm_free(devpollop->changes);
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
304 if (devpollop->dpfd >= 0)
305 close(devpollop->dpfd);
306
307 memset(devpollop, 0, sizeof(struct devpollop));
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
308 mm_free(devpollop);
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven.ne...
provos authored
309 }
76d4c92 @rosslagerwall Put #ifdef around some files to support alternate build systems.
authored
310
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
311 #endif /* EVENT__HAVE_DEVPOLL */
Something went wrong with that request. Please try again.