xref: /libevent-2.1.12/evport.c (revision 8ac3c4c2)
100bc7e37SNiels Provos /*
200bc7e37SNiels Provos  * Submitted by David Pacheco ([email protected])
300bc7e37SNiels Provos  *
4e49e2891SNick Mathewson  * Copyright 2006-2007 Niels Provos
5e49e2891SNick Mathewson  * Copyright 2007-2012 Niels Provos and Nick Mathewson
6e49e2891SNick Mathewson  *
700bc7e37SNiels Provos  * Redistribution and use in source and binary forms, with or without
800bc7e37SNiels Provos  * modification, are permitted provided that the following conditions
900bc7e37SNiels Provos  * are met:
1000bc7e37SNiels Provos  * 1. Redistributions of source code must retain the above copyright
1100bc7e37SNiels Provos  *    notice, this list of conditions and the following disclaimer.
1200bc7e37SNiels Provos  * 2. Redistributions in binary form must reproduce the above copyright
1300bc7e37SNiels Provos  *    notice, this list of conditions and the following disclaimer in the
1400bc7e37SNiels Provos  *    documentation and/or other materials provided with the distribution.
1500bc7e37SNiels Provos  * 3. The name of the author may not be used to endorse or promote products
1600bc7e37SNiels Provos  *    derived from this software without specific prior written permission.
1700bc7e37SNiels Provos  *
1818ac9248SNiels Provos  * THIS SOFTWARE IS PROVIDED BY SUN MICROSYSTEMS, INC. ``AS IS'' AND ANY
1918ac9248SNiels Provos  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2018ac9248SNiels Provos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2118ac9248SNiels Provos  * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY
2218ac9248SNiels Provos  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2318ac9248SNiels Provos  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2418ac9248SNiels Provos  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2518ac9248SNiels Provos  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2618ac9248SNiels Provos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2718ac9248SNiels Provos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2800bc7e37SNiels Provos  */
2900bc7e37SNiels Provos 
3000bc7e37SNiels Provos /*
3118ac9248SNiels Provos  * Copyright (c) 2007 Sun Microsystems. All rights reserved.
3249ef242fSNiels Provos  * Use is subject to license terms.
3349ef242fSNiels Provos  */
3449ef242fSNiels Provos 
3549ef242fSNiels Provos /*
3600bc7e37SNiels Provos  * evport.c: event backend using Solaris 10 event ports. See port_create(3C).
3700bc7e37SNiels Provos  * This implementation is loosely modeled after the one used for select(2) (in
3800bc7e37SNiels Provos  * select.c).
3900bc7e37SNiels Provos  *
4000bc7e37SNiels Provos  * The outstanding events are tracked in a data structure called evport_data.
4100bc7e37SNiels Provos  * Each entry in the ed_fds array corresponds to a file descriptor, and contains
4200bc7e37SNiels Provos  * pointers to the read and write events that correspond to that fd. (That is,
4300bc7e37SNiels Provos  * when the file is readable, the "read" event should handle it, etc.)
4400bc7e37SNiels Provos  *
4500bc7e37SNiels Provos  * evport_add and evport_del update this data structure. evport_dispatch uses it
4600bc7e37SNiels Provos  * to determine where to callback when an event occurs (which it gets from
4700bc7e37SNiels Provos  * port_getn).
4800bc7e37SNiels Provos  *
4900bc7e37SNiels Provos  * Helper functions are used: grow() grows the file descriptor array as
5000bc7e37SNiels Provos  * necessary when large fd's come in. reassociate() takes care of maintaining
5100bc7e37SNiels Provos  * the proper file-descriptor/event-port associations.
5200bc7e37SNiels Provos  *
53fbe24f43SNiels Provos  * As in the select(2) implementation, signals are handled by evsignal.
5400bc7e37SNiels Provos  */
5500bc7e37SNiels Provos 
56ec347b92SNick Mathewson #include "event2/event-config.h"
570915ca0aSKevin Bowling #include "evconfig-private.h"
5800bc7e37SNiels Provos 
5968120d9bSNick Mathewson #ifdef EVENT__HAVE_EVENT_PORTS
6076d4c929SRoss Lagerwall 
6100bc7e37SNiels Provos #include <sys/time.h>
6200bc7e37SNiels Provos #include <sys/queue.h>
6300bc7e37SNiels Provos #include <errno.h>
6400bc7e37SNiels Provos #include <poll.h>
6500bc7e37SNiels Provos #include <port.h>
6600bc7e37SNiels Provos #include <signal.h>
6700bc7e37SNiels Provos #include <stdio.h>
6800bc7e37SNiels Provos #include <stdlib.h>
6900bc7e37SNiels Provos #include <string.h>
7000bc7e37SNiels Provos #include <time.h>
7100bc7e37SNiels Provos #include <unistd.h>
7200bc7e37SNiels Provos 
73fbe64f21SEvan Jones #include "event2/thread.h"
748111fac0SNick Mathewson 
758111fac0SNick Mathewson #include "evthread-internal.h"
7600bc7e37SNiels Provos #include "event-internal.h"
77169321c9SNick Mathewson #include "log-internal.h"
78169321c9SNick Mathewson #include "evsignal-internal.h"
79169321c9SNick Mathewson #include "evmap-internal.h"
8000bc7e37SNiels Provos 
81c04d9276SNick Mathewson #define INITIAL_EVENTS_PER_GETN 8
82c04d9276SNick Mathewson #define MAX_EVENTS_PER_GETN 4096
8300bc7e37SNiels Provos 
8400bc7e37SNiels Provos /*
8500bc7e37SNiels Provos  * Per-file-descriptor information about what events we're subscribed to. These
8600bc7e37SNiels Provos  * fields are NULL if no event is subscribed to either of them.
8700bc7e37SNiels Provos  */
8800bc7e37SNiels Provos 
8900bc7e37SNiels Provos struct fd_info {
900f77efefSNick Mathewson 	/* combinations of EV_READ and EV_WRITE */
910f77efefSNick Mathewson 	short fdi_what;
920f77efefSNick Mathewson 	/* Index of this fd within ed_pending, plus 1.  Zero if this fd is
930f77efefSNick Mathewson 	 * not in ed_pending.  (The +1 is a hack so that memset(0) will set
940f77efefSNick Mathewson 	 * it to a nil index. */
950f77efefSNick Mathewson 	int pending_idx_plus_1;
9600bc7e37SNiels Provos };
9700bc7e37SNiels Provos 
9802b2b4d1SNiels Provos #define FDI_HAS_READ(fdi)  ((fdi)->fdi_what & EV_READ)
99ddf3ee97SNiels Provos #define FDI_HAS_WRITE(fdi) ((fdi)->fdi_what & EV_WRITE)
10000bc7e37SNiels Provos #define FDI_HAS_EVENTS(fdi) (FDI_HAS_READ(fdi) || FDI_HAS_WRITE(fdi))
10100bc7e37SNiels Provos #define FDI_TO_SYSEVENTS(fdi) (FDI_HAS_READ(fdi) ? POLLIN : 0) | \
10200bc7e37SNiels Provos     (FDI_HAS_WRITE(fdi) ? POLLOUT : 0)
10300bc7e37SNiels Provos 
10400bc7e37SNiels Provos struct evport_data {
10500bc7e37SNiels Provos 	int		ed_port;	/* event port for system events  */
106849a5cffSNick Mathewson 	/* How many elements of ed_pending should we look at? */
107849a5cffSNick Mathewson 	int ed_npending;
108c04d9276SNick Mathewson 	/* How many elements are allocated in ed_pending and pevtlist? */
109c04d9276SNick Mathewson 	int ed_maxevents;
11000bc7e37SNiels Provos 	/* fdi's that we need to reassoc */
111c04d9276SNick Mathewson 	int *ed_pending;
112c04d9276SNick Mathewson 	/* storage space for incoming events. */
113c04d9276SNick Mathewson 	port_event_t *ed_pevtlist;
114c04d9276SNick Mathewson 
11500bc7e37SNiels Provos };
11600bc7e37SNiels Provos 
11741b7cbc3SNiels Provos static void*	evport_init(struct event_base *);
118554e1493SNick Mathewson static int evport_add(struct event_base *, int fd, short old, short events, void *);
119554e1493SNick Mathewson static int evport_del(struct event_base *, int fd, short old, short events, void *);
12002b2b4d1SNiels Provos static int	evport_dispatch(struct event_base *, struct timeval *);
12102b2b4d1SNiels Provos static void	evport_dealloc(struct event_base *);
122c04d9276SNick Mathewson static int	grow(struct evport_data *, int min_events);
12300bc7e37SNiels Provos 
12400bc7e37SNiels Provos const struct eventop evportops = {
125fa1c9a6dSNiels Provos 	"evport",
12600bc7e37SNiels Provos 	evport_init,
12700bc7e37SNiels Provos 	evport_add,
12800bc7e37SNiels Provos 	evport_del,
129aa5c8068SNiels Provos 	evport_dispatch,
130f38aec8bSNiels Provos 	evport_dealloc,
131554e1493SNick Mathewson 	1, /* need reinit */
132c44de06cSNick Mathewson 	0, /* features */
1334687ce4fSNick Mathewson 	sizeof(struct fd_info), /* fdinfo length */
13400bc7e37SNiels Provos };
13500bc7e37SNiels Provos 
13600bc7e37SNiels Provos /*
13700bc7e37SNiels Provos  * Initialize the event port implementation.
13800bc7e37SNiels Provos  */
13900bc7e37SNiels Provos 
14000bc7e37SNiels Provos static void*
evport_init(struct event_base * base)14141b7cbc3SNiels Provos evport_init(struct event_base *base)
14200bc7e37SNiels Provos {
14300bc7e37SNiels Provos 	struct evport_data *evpd;
14400bc7e37SNiels Provos 
14549868b61SNick Mathewson 	if (!(evpd = mm_calloc(1, sizeof(struct evport_data))))
14600bc7e37SNiels Provos 		return (NULL);
14700bc7e37SNiels Provos 
14800bc7e37SNiels Provos 	if ((evpd->ed_port = port_create()) == -1) {
14949868b61SNick Mathewson 		mm_free(evpd);
15000bc7e37SNiels Provos 		return (NULL);
15100bc7e37SNiels Provos 	}
15200bc7e37SNiels Provos 
153c04d9276SNick Mathewson 	if (grow(evpd, INITIAL_EVENTS_PER_GETN) < 0) {
154c04d9276SNick Mathewson 		close(evpd->ed_port);
155c04d9276SNick Mathewson 		mm_free(evpd);
156c04d9276SNick Mathewson 		return NULL;
157c04d9276SNick Mathewson 	}
158c04d9276SNick Mathewson 
159849a5cffSNick Mathewson 	evpd->ed_npending = 0;
16000bc7e37SNiels Provos 
161*8ac3c4c2SNick Mathewson 	evsig_init_(base);
16200bc7e37SNiels Provos 
16300bc7e37SNiels Provos 	return (evpd);
16400bc7e37SNiels Provos }
16500bc7e37SNiels Provos 
166c04d9276SNick Mathewson static int
grow(struct evport_data * data,int min_events)167c04d9276SNick Mathewson grow(struct evport_data *data, int min_events)
168c04d9276SNick Mathewson {
169c04d9276SNick Mathewson 	int newsize;
170c04d9276SNick Mathewson 	int *new_pending;
171c04d9276SNick Mathewson 	port_event_t *new_pevtlist;
172c04d9276SNick Mathewson 	if (data->ed_maxevents) {
173c04d9276SNick Mathewson 		newsize = data->ed_maxevents;
174c04d9276SNick Mathewson 		do {
175c04d9276SNick Mathewson 			newsize *= 2;
176c04d9276SNick Mathewson 		} while (newsize < min_events);
177c04d9276SNick Mathewson 	} else {
178c04d9276SNick Mathewson 		newsize = min_events;
179c04d9276SNick Mathewson 	}
180c04d9276SNick Mathewson 
181c04d9276SNick Mathewson 	new_pending = mm_realloc(data->ed_pending, sizeof(int)*newsize);
182c04d9276SNick Mathewson 	if (new_pending == NULL)
183c04d9276SNick Mathewson 		return -1;
184c04d9276SNick Mathewson 	data->ed_pending = new_pending;
185c04d9276SNick Mathewson 	new_pevtlist = mm_realloc(data->ed_pevtlist, sizeof(port_event_t)*newsize);
186c04d9276SNick Mathewson 	if (new_pevtlist == NULL)
187c04d9276SNick Mathewson 		return -1;
188c04d9276SNick Mathewson 	data->ed_pevtlist = new_pevtlist;
189c04d9276SNick Mathewson 
190c04d9276SNick Mathewson 	data->ed_maxevents = newsize;
191c04d9276SNick Mathewson 	return 0;
192c04d9276SNick Mathewson }
193c04d9276SNick Mathewson 
19400bc7e37SNiels Provos #ifdef CHECK_INVARIANTS
19500bc7e37SNiels Provos /*
19600bc7e37SNiels Provos  * Checks some basic properties about the evport_data structure. Because it
19700bc7e37SNiels Provos  * checks all file descriptors, this function can be expensive when the maximum
19800bc7e37SNiels Provos  * file descriptor ever used is rather large.
19900bc7e37SNiels Provos  */
20000bc7e37SNiels Provos 
20100bc7e37SNiels Provos static void
check_evportop(struct evport_data * evpd)20200bc7e37SNiels Provos check_evportop(struct evport_data *evpd)
20300bc7e37SNiels Provos {
2042e36dbe1SNick Mathewson 	EVUTIL_ASSERT(evpd);
2052e36dbe1SNick Mathewson 	EVUTIL_ASSERT(evpd->ed_port > 0);
20600bc7e37SNiels Provos }
20700bc7e37SNiels Provos 
20800bc7e37SNiels Provos /*
20900bc7e37SNiels Provos  * Verifies very basic integrity of a given port_event.
21000bc7e37SNiels Provos  */
21100bc7e37SNiels Provos static void
check_event(port_event_t * pevt)21200bc7e37SNiels Provos check_event(port_event_t* pevt)
21300bc7e37SNiels Provos {
21400bc7e37SNiels Provos 	/*
21500bc7e37SNiels Provos 	 * We've only registered for PORT_SOURCE_FD events. The only
21600bc7e37SNiels Provos 	 * other thing we can legitimately receive is PORT_SOURCE_ALERT,
21700bc7e37SNiels Provos 	 * but since we're not using port_alert either, we can assume
21800bc7e37SNiels Provos 	 * PORT_SOURCE_FD.
21900bc7e37SNiels Provos 	 */
2202e36dbe1SNick Mathewson 	EVUTIL_ASSERT(pevt->portev_source == PORT_SOURCE_FD);
22100bc7e37SNiels Provos }
22200bc7e37SNiels Provos 
22300bc7e37SNiels Provos #else
22400bc7e37SNiels Provos #define check_evportop(epop)
22500bc7e37SNiels Provos #define check_event(pevt)
22600bc7e37SNiels Provos #endif /* CHECK_INVARIANTS */
22700bc7e37SNiels Provos 
22800bc7e37SNiels Provos /*
22900bc7e37SNiels Provos  * (Re)associates the given file descriptor with the event port. The OS events
23000bc7e37SNiels Provos  * are specified (implicitly) from the fd_info struct.
23100bc7e37SNiels Provos  */
23200bc7e37SNiels Provos static int
reassociate(struct evport_data * epdp,struct fd_info * fdip,int fd)23300bc7e37SNiels Provos reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd)
23400bc7e37SNiels Provos {
23500bc7e37SNiels Provos 	int sysevents = FDI_TO_SYSEVENTS(fdip);
23600bc7e37SNiels Provos 
23700bc7e37SNiels Provos 	if (sysevents != 0) {
23818ac9248SNiels Provos 		if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
239276ec0efSNick Mathewson 				   fd, sysevents, fdip) == -1) {
24018ac9248SNiels Provos 			event_warn("port_associate");
24100bc7e37SNiels Provos 			return (-1);
24200bc7e37SNiels Provos 		}
24300bc7e37SNiels Provos 	}
24400bc7e37SNiels Provos 
24500bc7e37SNiels Provos 	check_evportop(epdp);
24600bc7e37SNiels Provos 
24700bc7e37SNiels Provos 	return (0);
24800bc7e37SNiels Provos }
24900bc7e37SNiels Provos 
25000bc7e37SNiels Provos /*
25100bc7e37SNiels Provos  * Main event loop - polls port_getn for some number of events, and processes
25200bc7e37SNiels Provos  * them.
25300bc7e37SNiels Provos  */
25400bc7e37SNiels Provos 
25500bc7e37SNiels Provos static int
evport_dispatch(struct event_base * base,struct timeval * tv)25602b2b4d1SNiels Provos evport_dispatch(struct event_base *base, struct timeval *tv)
25700bc7e37SNiels Provos {
25800bc7e37SNiels Provos 	int i, res;
25902b2b4d1SNiels Provos 	struct evport_data *epdp = base->evbase;
260c04d9276SNick Mathewson 	port_event_t *pevtlist = epdp->ed_pevtlist;
26100bc7e37SNiels Provos 
26200bc7e37SNiels Provos 	/*
26300bc7e37SNiels Provos 	 * port_getn will block until it has at least nevents events. It will
26400bc7e37SNiels Provos 	 * also return how many it's given us (which may be more than we asked
265c04d9276SNick Mathewson 	 * for, as long as it's less than our maximum (ed_maxevents)) in
26600bc7e37SNiels Provos 	 * nevents.
26700bc7e37SNiels Provos 	 */
26800bc7e37SNiels Provos 	int nevents = 1;
26900bc7e37SNiels Provos 
27000bc7e37SNiels Provos 	/*
27100bc7e37SNiels Provos 	 * We have to convert a struct timeval to a struct timespec
272bfd27f58SNick Mathewson 	 * (only difference is nanoseconds vs. microseconds). If no time-based
273bfd27f58SNick Mathewson 	 * events are active, we should wait for I/O (and tv == NULL).
27400bc7e37SNiels Provos 	 */
275bfd27f58SNick Mathewson 	struct timespec ts;
276bfd27f58SNick Mathewson 	struct timespec *ts_p = NULL;
277bfd27f58SNick Mathewson 	if (tv != NULL) {
278bfd27f58SNick Mathewson 		ts.tv_sec = tv->tv_sec;
279bfd27f58SNick Mathewson 		ts.tv_nsec = tv->tv_usec * 1000;
280bfd27f58SNick Mathewson 		ts_p = &ts;
281bfd27f58SNick Mathewson 	}
28200bc7e37SNiels Provos 
28300bc7e37SNiels Provos 	/*
28400bc7e37SNiels Provos 	 * Before doing anything else, we need to reassociate the events we hit
28500bc7e37SNiels Provos 	 * last time which need reassociation. See comment at the end of the
28600bc7e37SNiels Provos 	 * loop below.
28700bc7e37SNiels Provos 	 */
288849a5cffSNick Mathewson 	for (i = 0; i < epdp->ed_npending; ++i) {
28918ac9248SNiels Provos 		struct fd_info *fdi = NULL;
2904687ce4fSNick Mathewson 		const int fd = epdp->ed_pending[i];
2914687ce4fSNick Mathewson 		if (fd != -1) {
292849a5cffSNick Mathewson 			/* We might have cleared out this event; we need
293849a5cffSNick Mathewson 			 * to be sure that it's still set. */
294*8ac3c4c2SNick Mathewson 			fdi = evmap_io_get_fdinfo_(&base->io, fd);
29518ac9248SNiels Provos 		}
29600bc7e37SNiels Provos 
29700bc7e37SNiels Provos 		if (fdi != NULL && FDI_HAS_EVENTS(fdi)) {
29800bc7e37SNiels Provos 			reassociate(epdp, fdi, fd);
2994dee4cc7SNick Mathewson 			/* epdp->ed_pending[i] = -1; */
3000f77efefSNick Mathewson 			fdi->pending_idx_plus_1 = 0;
30100bc7e37SNiels Provos 		}
30200bc7e37SNiels Provos 	}
30300bc7e37SNiels Provos 
30476cd2b70SNick Mathewson 	EVBASE_RELEASE_LOCK(base, th_base_lock);
3056b22e74aSNick Mathewson 
306c04d9276SNick Mathewson 	res = port_getn(epdp->ed_port, pevtlist, epdp->ed_maxevents,
3076b22e74aSNick Mathewson 	    (unsigned int *) &nevents, ts_p);
3086b22e74aSNick Mathewson 
30976cd2b70SNick Mathewson 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3106b22e74aSNick Mathewson 
3116b22e74aSNick Mathewson 	if (res == -1) {
3126baff522SNiels Provos 		if (errno == EINTR || errno == EAGAIN) {
31300bc7e37SNiels Provos 			return (0);
31400bc7e37SNiels Provos 		} else if (errno == ETIME) {
31500bc7e37SNiels Provos 			if (nevents == 0)
31600bc7e37SNiels Provos 				return (0);
31700bc7e37SNiels Provos 		} else {
31818ac9248SNiels Provos 			event_warn("port_getn");
31900bc7e37SNiels Provos 			return (-1);
32000bc7e37SNiels Provos 		}
32100bc7e37SNiels Provos 	}
32200bc7e37SNiels Provos 
32300bc7e37SNiels Provos 	event_debug(("%s: port_getn reports %d events", __func__, nevents));
32400bc7e37SNiels Provos 
32500bc7e37SNiels Provos 	for (i = 0; i < nevents; ++i) {
32600bc7e37SNiels Provos 		port_event_t *pevt = &pevtlist[i];
32700bc7e37SNiels Provos 		int fd = (int) pevt->portev_object;
328276ec0efSNick Mathewson 		struct fd_info *fdi = pevt->portev_user;
329*8ac3c4c2SNick Mathewson 		/*EVUTIL_ASSERT(evmap_io_get_fdinfo_(&base->io, fd) == fdi);*/
33000bc7e37SNiels Provos 
33100bc7e37SNiels Provos 		check_evportop(epdp);
33200bc7e37SNiels Provos 		check_event(pevt);
33318ac9248SNiels Provos 		epdp->ed_pending[i] = fd;
3340f77efefSNick Mathewson 		fdi->pending_idx_plus_1 = i + 1;
33500bc7e37SNiels Provos 
33600bc7e37SNiels Provos 		/*
33700bc7e37SNiels Provos 		 * Figure out what kind of event it was
33800bc7e37SNiels Provos 		 * (because we have to pass this to the callback)
33900bc7e37SNiels Provos 		 */
34000bc7e37SNiels Provos 		res = 0;
341b42ce4bfSNick Mathewson 		if (pevt->portev_events & (POLLERR|POLLHUP)) {
342b42ce4bfSNick Mathewson 			res = EV_READ | EV_WRITE;
343b42ce4bfSNick Mathewson 		} else {
34400bc7e37SNiels Provos 			if (pevt->portev_events & POLLIN)
34500bc7e37SNiels Provos 				res |= EV_READ;
34600bc7e37SNiels Provos 			if (pevt->portev_events & POLLOUT)
34700bc7e37SNiels Provos 				res |= EV_WRITE;
348b42ce4bfSNick Mathewson 		}
34900bc7e37SNiels Provos 
3500144886eSTrond Norbye 		/*
3510144886eSTrond Norbye 		 * Check for the error situations or a hangup situation
3520144886eSTrond Norbye 		 */
3530144886eSTrond Norbye 		if (pevt->portev_events & (POLLERR|POLLHUP|POLLNVAL))
3540144886eSTrond Norbye 			res |= EV_READ|EV_WRITE;
3550144886eSTrond Norbye 
356*8ac3c4c2SNick Mathewson 		evmap_io_active_(base, fd, res);
35700bc7e37SNiels Provos 	} /* end of all events gotten */
358849a5cffSNick Mathewson 	epdp->ed_npending = nevents;
35900bc7e37SNiels Provos 
360c04d9276SNick Mathewson 	if (nevents == epdp->ed_maxevents &&
361c04d9276SNick Mathewson 	    epdp->ed_maxevents < MAX_EVENTS_PER_GETN) {
362c04d9276SNick Mathewson 		/* we used all the space this time.  We should be ready
363c04d9276SNick Mathewson 		 * for more events next time around. */
364c04d9276SNick Mathewson 		grow(epdp, epdp->ed_maxevents * 2);
365c04d9276SNick Mathewson 	}
366c04d9276SNick Mathewson 
36700bc7e37SNiels Provos 	check_evportop(epdp);
36800bc7e37SNiels Provos 
36900bc7e37SNiels Provos 	return (0);
37000bc7e37SNiels Provos }
37100bc7e37SNiels Provos 
37200bc7e37SNiels Provos 
37300bc7e37SNiels Provos /*
37400bc7e37SNiels Provos  * Adds the given event (so that you will be notified when it happens via
37500bc7e37SNiels Provos  * the callback function).
37600bc7e37SNiels Provos  */
37700bc7e37SNiels Provos 
37800bc7e37SNiels Provos static int
evport_add(struct event_base * base,int fd,short old,short events,void * p)379554e1493SNick Mathewson evport_add(struct event_base *base, int fd, short old, short events, void *p)
38000bc7e37SNiels Provos {
38102b2b4d1SNiels Provos 	struct evport_data *evpd = base->evbase;
3824687ce4fSNick Mathewson 	struct fd_info *fdi = p;
38300bc7e37SNiels Provos 
38400bc7e37SNiels Provos 	check_evportop(evpd);
38500bc7e37SNiels Provos 
386ddf3ee97SNiels Provos 	fdi->fdi_what |= events;
38700bc7e37SNiels Provos 
38802b2b4d1SNiels Provos 	return reassociate(evpd, fdi, fd);
38900bc7e37SNiels Provos }
39000bc7e37SNiels Provos 
39100bc7e37SNiels Provos /*
39200bc7e37SNiels Provos  * Removes the given event from the list of events to wait for.
39300bc7e37SNiels Provos  */
39400bc7e37SNiels Provos 
39500bc7e37SNiels Provos static int
evport_del(struct event_base * base,int fd,short old,short events,void * p)396554e1493SNick Mathewson evport_del(struct event_base *base, int fd, short old, short events, void *p)
39700bc7e37SNiels Provos {
39802b2b4d1SNiels Provos 	struct evport_data *evpd = base->evbase;
3994687ce4fSNick Mathewson 	struct fd_info *fdi = p;
4000f77efefSNick Mathewson 	int associated = ! fdi->pending_idx_plus_1;
40100bc7e37SNiels Provos 
40200bc7e37SNiels Provos 	check_evportop(evpd);
40300bc7e37SNiels Provos 
4044687ce4fSNick Mathewson 	fdi->fdi_what &= ~(events &(EV_READ|EV_WRITE));
40500bc7e37SNiels Provos 
40618ac9248SNiels Provos 	if (associated) {
40718ac9248SNiels Provos 		if (!FDI_HAS_EVENTS(fdi) &&
40802b2b4d1SNiels Provos 		    port_dissociate(evpd->ed_port, PORT_SOURCE_FD, fd) == -1) {
40918ac9248SNiels Provos 			/*
410e3fd294aSNick Mathewson 			 * Ignore EBADFD error the fd could have been closed
41118ac9248SNiels Provos 			 * before event_del() was called.
41218ac9248SNiels Provos 			 */
41318ac9248SNiels Provos 			if (errno != EBADFD) {
41418ac9248SNiels Provos 				event_warn("port_dissociate");
41518ac9248SNiels Provos 				return (-1);
41618ac9248SNiels Provos 			}
41718ac9248SNiels Provos 		} else {
41818ac9248SNiels Provos 			if (FDI_HAS_EVENTS(fdi)) {
41902b2b4d1SNiels Provos 				return (reassociate(evpd, fdi, fd));
42018ac9248SNiels Provos 			}
42118ac9248SNiels Provos 		}
42218ac9248SNiels Provos 	} else {
423ddf3ee97SNiels Provos 		if ((fdi->fdi_what & (EV_READ|EV_WRITE)) == 0) {
4240f77efefSNick Mathewson 			const int i = fdi->pending_idx_plus_1 - 1;
4250f77efefSNick Mathewson 			EVUTIL_ASSERT(evpd->ed_pending[i] == fd);
42618ac9248SNiels Provos 			evpd->ed_pending[i] = -1;
4270f77efefSNick Mathewson 			fdi->pending_idx_plus_1 = 0;
42818ac9248SNiels Provos 		}
42918ac9248SNiels Provos 	}
43018ac9248SNiels Provos 	return 0;
43100bc7e37SNiels Provos }
43200bc7e37SNiels Provos 
43300bc7e37SNiels Provos 
434aa5c8068SNiels Provos static void
evport_dealloc(struct event_base * base)43502b2b4d1SNiels Provos evport_dealloc(struct event_base *base)
436aa5c8068SNiels Provos {
43702b2b4d1SNiels Provos 	struct evport_data *evpd = base->evbase;
438aa5c8068SNiels Provos 
439*8ac3c4c2SNick Mathewson 	evsig_dealloc_(base);
440aa5c8068SNiels Provos 
441aa5c8068SNiels Provos 	close(evpd->ed_port);
442aa5c8068SNiels Provos 
443c04d9276SNick Mathewson 	if (evpd->ed_pending)
444c04d9276SNick Mathewson 		mm_free(evpd->ed_pending);
445c04d9276SNick Mathewson 	if (evpd->ed_pevtlist)
446c04d9276SNick Mathewson 		mm_free(evpd->ed_pevtlist);
447c04d9276SNick Mathewson 
44849868b61SNick Mathewson 	mm_free(evpd);
449aa5c8068SNiels Provos }
45076d4c929SRoss Lagerwall 
45168120d9bSNick Mathewson #endif /* EVENT__HAVE_EVENT_PORTS */
452