Remove building with NOCRYPTO option
[minix.git] / external / bsd / libevent / dist / evport.c
blob2fa3534c0008906a5fd2a08561132c3e564c1098
1 /* $NetBSD: evport.c,v 1.1.1.1 2013/04/11 16:43:25 christos Exp $ */
2 /*
3 * Submitted by David Pacheco (dp.spambait@gmail.com)
5 * Copyright 2006-2007 Niels Provos
6 * Copyright 2007-2012 Niels Provos and Nick Mathewson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY SUN MICROSYSTEMS, INC. ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 2007 Sun Microsystems. All rights reserved.
33 * Use is subject to license terms.
37 * evport.c: event backend using Solaris 10 event ports. See port_create(3C).
38 * This implementation is loosely modeled after the one used for select(2) (in
39 * select.c).
41 * The outstanding events are tracked in a data structure called evport_data.
42 * Each entry in the ed_fds array corresponds to a file descriptor, and contains
43 * pointers to the read and write events that correspond to that fd. (That is,
44 * when the file is readable, the "read" event should handle it, etc.)
46 * evport_add and evport_del update this data structure. evport_dispatch uses it
47 * to determine where to callback when an event occurs (which it gets from
48 * port_getn).
50 * Helper functions are used: grow() grows the file descriptor array as
51 * necessary when large fd's come in. reassociate() takes care of maintaining
52 * the proper file-descriptor/event-port associations.
54 * As in the select(2) implementation, signals are handled by evsignal.
57 #include "event2/event-config.h"
58 #include <sys/cdefs.h>
59 __RCSID("$NetBSD: evport.c,v 1.1.1.1 2013/04/11 16:43:25 christos Exp $");
61 #include <sys/time.h>
62 #include <sys/queue.h>
63 #include <errno.h>
64 #include <poll.h>
65 #include <port.h>
66 #include <signal.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <time.h>
71 #include <unistd.h>
73 #include "event2/thread.h"
75 #include "evthread-internal.h"
76 #include "event-internal.h"
77 #include "log-internal.h"
78 #include "evsignal-internal.h"
79 #include "evmap-internal.h"
82 * Default value for ed_nevents, which is the maximum file descriptor number we
83 * can handle. If an event comes in for a file descriptor F > nevents, we will
84 * grow the array of file descriptors, doubling its size.
86 #define DEFAULT_NFDS 16
90 * EVENTS_PER_GETN is the maximum number of events to retrieve from port_getn on
91 * any particular call. You can speed things up by increasing this, but it will
92 * (obviously) require more memory.
94 #define EVENTS_PER_GETN 8
97 * Per-file-descriptor information about what events we're subscribed to. These
98 * fields are NULL if no event is subscribed to either of them.
101 struct fd_info {
102 short fdi_what; /* combinations of EV_READ and EV_WRITE */
105 #define FDI_HAS_READ(fdi) ((fdi)->fdi_what & EV_READ)
106 #define FDI_HAS_WRITE(fdi) ((fdi)->fdi_what & EV_WRITE)
107 #define FDI_HAS_EVENTS(fdi) (FDI_HAS_READ(fdi) || FDI_HAS_WRITE(fdi))
108 #define FDI_TO_SYSEVENTS(fdi) (FDI_HAS_READ(fdi) ? POLLIN : 0) | \
109 (FDI_HAS_WRITE(fdi) ? POLLOUT : 0)
111 struct evport_data {
112 int ed_port; /* event port for system events */
113 int ed_nevents; /* number of allocated fdi's */
114 struct fd_info *ed_fds; /* allocated fdi table */
115 /* fdi's that we need to reassoc */
116 int ed_pending[EVENTS_PER_GETN]; /* fd's with pending events */
119 static void* evport_init(struct event_base *);
120 static int evport_add(struct event_base *, int fd, short old, short events, void *);
121 static int evport_del(struct event_base *, int fd, short old, short events, void *);
122 static int evport_dispatch(struct event_base *, struct timeval *);
123 static void evport_dealloc(struct event_base *);
125 const struct eventop evportops = {
126 "evport",
127 evport_init,
128 evport_add,
129 evport_del,
130 evport_dispatch,
131 evport_dealloc,
132 1, /* need reinit */
133 0, /* features */
134 0, /* fdinfo length */
138 * Initialize the event port implementation.
141 static void*
142 evport_init(struct event_base *base)
144 struct evport_data *evpd;
145 int i;
147 if (!(evpd = mm_calloc(1, sizeof(struct evport_data))))
148 return (NULL);
150 if ((evpd->ed_port = port_create()) == -1) {
151 mm_free(evpd);
152 return (NULL);
156 * Initialize file descriptor structure
158 evpd->ed_fds = mm_calloc(DEFAULT_NFDS, sizeof(struct fd_info));
159 if (evpd->ed_fds == NULL) {
160 close(evpd->ed_port);
161 mm_free(evpd);
162 return (NULL);
164 evpd->ed_nevents = DEFAULT_NFDS;
165 for (i = 0; i < EVENTS_PER_GETN; i++)
166 evpd->ed_pending[i] = -1;
168 evsig_init(base);
170 return (evpd);
173 #ifdef CHECK_INVARIANTS
175 * Checks some basic properties about the evport_data structure. Because it
176 * checks all file descriptors, this function can be expensive when the maximum
177 * file descriptor ever used is rather large.
180 static void
181 check_evportop(struct evport_data *evpd)
183 EVUTIL_ASSERT(evpd);
184 EVUTIL_ASSERT(evpd->ed_nevents > 0);
185 EVUTIL_ASSERT(evpd->ed_port > 0);
186 EVUTIL_ASSERT(evpd->ed_fds > 0);
190 * Verifies very basic integrity of a given port_event.
192 static void
193 check_event(port_event_t* pevt)
196 * We've only registered for PORT_SOURCE_FD events. The only
197 * other thing we can legitimately receive is PORT_SOURCE_ALERT,
198 * but since we're not using port_alert either, we can assume
199 * PORT_SOURCE_FD.
201 EVUTIL_ASSERT(pevt->portev_source == PORT_SOURCE_FD);
202 EVUTIL_ASSERT(pevt->portev_user == NULL);
205 #else
206 #define check_evportop(epop)
207 #define check_event(pevt)
208 #endif /* CHECK_INVARIANTS */
211 * Doubles the size of the allocated file descriptor array.
213 static int
214 grow(struct evport_data *epdp, int factor)
216 struct fd_info *tmp;
217 int oldsize = epdp->ed_nevents;
218 int newsize = factor * oldsize;
219 EVUTIL_ASSERT(factor > 1);
221 check_evportop(epdp);
223 tmp = mm_realloc(epdp->ed_fds, sizeof(struct fd_info) * newsize);
224 if (NULL == tmp)
225 return -1;
226 epdp->ed_fds = tmp;
227 memset((char*) (epdp->ed_fds + oldsize), 0,
228 (newsize - oldsize)*sizeof(struct fd_info));
229 epdp->ed_nevents = newsize;
231 check_evportop(epdp);
233 return 0;
238 * (Re)associates the given file descriptor with the event port. The OS events
239 * are specified (implicitly) from the fd_info struct.
241 static int
242 reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd)
244 int sysevents = FDI_TO_SYSEVENTS(fdip);
246 if (sysevents != 0) {
247 if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
248 fd, sysevents, NULL) == -1) {
249 event_warn("port_associate");
250 return (-1);
254 check_evportop(epdp);
256 return (0);
260 * Main event loop - polls port_getn for some number of events, and processes
261 * them.
264 static int
265 evport_dispatch(struct event_base *base, struct timeval *tv)
267 int i, res;
268 struct evport_data *epdp = base->evbase;
269 port_event_t pevtlist[EVENTS_PER_GETN];
272 * port_getn will block until it has at least nevents events. It will
273 * also return how many it's given us (which may be more than we asked
274 * for, as long as it's less than our maximum (EVENTS_PER_GETN)) in
275 * nevents.
277 int nevents = 1;
280 * We have to convert a struct timeval to a struct timespec
281 * (only difference is nanoseconds vs. microseconds). If no time-based
282 * events are active, we should wait for I/O (and tv == NULL).
284 struct timespec ts;
285 struct timespec *ts_p = NULL;
286 if (tv != NULL) {
287 ts.tv_sec = tv->tv_sec;
288 ts.tv_nsec = tv->tv_usec * 1000;
289 ts_p = &ts;
293 * Before doing anything else, we need to reassociate the events we hit
294 * last time which need reassociation. See comment at the end of the
295 * loop below.
297 for (i = 0; i < EVENTS_PER_GETN; ++i) {
298 struct fd_info *fdi = NULL;
299 if (epdp->ed_pending[i] != -1) {
300 fdi = &(epdp->ed_fds[epdp->ed_pending[i]]);
303 if (fdi != NULL && FDI_HAS_EVENTS(fdi)) {
304 int fd = epdp->ed_pending[i];
305 reassociate(epdp, fdi, fd);
306 epdp->ed_pending[i] = -1;
310 EVBASE_RELEASE_LOCK(base, th_base_lock);
312 res = port_getn(epdp->ed_port, pevtlist, EVENTS_PER_GETN,
313 (unsigned int *) &nevents, ts_p);
315 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
317 if (res == -1) {
318 if (errno == EINTR || errno == EAGAIN) {
319 return (0);
320 } else if (errno == ETIME) {
321 if (nevents == 0)
322 return (0);
323 } else {
324 event_warn("port_getn");
325 return (-1);
329 event_debug(("%s: port_getn reports %d events", __func__, nevents));
331 for (i = 0; i < nevents; ++i) {
332 struct fd_info *fdi;
333 port_event_t *pevt = &pevtlist[i];
334 int fd = (int) pevt->portev_object;
336 check_evportop(epdp);
337 check_event(pevt);
338 epdp->ed_pending[i] = fd;
341 * Figure out what kind of event it was
342 * (because we have to pass this to the callback)
344 res = 0;
345 if (pevt->portev_events & (POLLERR|POLLHUP)) {
346 res = EV_READ | EV_WRITE;
347 } else {
348 if (pevt->portev_events & POLLIN)
349 res |= EV_READ;
350 if (pevt->portev_events & POLLOUT)
351 res |= EV_WRITE;
355 * Check for the error situations or a hangup situation
357 if (pevt->portev_events & (POLLERR|POLLHUP|POLLNVAL))
358 res |= EV_READ|EV_WRITE;
360 EVUTIL_ASSERT(epdp->ed_nevents > fd);
361 fdi = &(epdp->ed_fds[fd]);
363 evmap_io_active(base, fd, res);
364 } /* end of all events gotten */
366 check_evportop(epdp);
368 return (0);
373 * Adds the given event (so that you will be notified when it happens via
374 * the callback function).
377 static int
378 evport_add(struct event_base *base, int fd, short old, short events, void *p)
380 struct evport_data *evpd = base->evbase;
381 struct fd_info *fdi;
382 int factor;
383 (void)p;
385 check_evportop(evpd);
388 * If necessary, grow the file descriptor info table
391 factor = 1;
392 while (fd >= factor * evpd->ed_nevents)
393 factor *= 2;
395 if (factor > 1) {
396 if (-1 == grow(evpd, factor)) {
397 return (-1);
401 fdi = &evpd->ed_fds[fd];
402 fdi->fdi_what |= events;
404 return reassociate(evpd, fdi, fd);
408 * Removes the given event from the list of events to wait for.
411 static int
412 evport_del(struct event_base *base, int fd, short old, short events, void *p)
414 struct evport_data *evpd = base->evbase;
415 struct fd_info *fdi;
416 int i;
417 int associated = 1;
418 (void)p;
420 check_evportop(evpd);
422 if (evpd->ed_nevents < fd) {
423 return (-1);
426 for (i = 0; i < EVENTS_PER_GETN; ++i) {
427 if (evpd->ed_pending[i] == fd) {
428 associated = 0;
429 break;
433 fdi = &evpd->ed_fds[fd];
434 if (events & EV_READ)
435 fdi->fdi_what &= ~EV_READ;
436 if (events & EV_WRITE)
437 fdi->fdi_what &= ~EV_WRITE;
439 if (associated) {
440 if (!FDI_HAS_EVENTS(fdi) &&
441 port_dissociate(evpd->ed_port, PORT_SOURCE_FD, fd) == -1) {
443 * Ignore EBADFD error the fd could have been closed
444 * before event_del() was called.
446 if (errno != EBADFD) {
447 event_warn("port_dissociate");
448 return (-1);
450 } else {
451 if (FDI_HAS_EVENTS(fdi)) {
452 return (reassociate(evpd, fdi, fd));
455 } else {
456 if ((fdi->fdi_what & (EV_READ|EV_WRITE)) == 0) {
457 evpd->ed_pending[i] = -1;
460 return 0;
464 static void
465 evport_dealloc(struct event_base *base)
467 struct evport_data *evpd = base->evbase;
469 evsig_dealloc(base);
471 close(evpd->ed_port);
473 if (evpd->ed_fds)
474 mm_free(evpd->ed_fds);
475 mm_free(evpd);