xref: /freebsd/contrib/libevent/evport.c (revision c43e99fd)
1*c43e99fdSEd Maste /*
2*c43e99fdSEd Maste  * Submitted by David Pacheco (dp.spambait@gmail.com)
3*c43e99fdSEd Maste  *
4*c43e99fdSEd Maste  * Copyright 2006-2007 Niels Provos
5*c43e99fdSEd Maste  * Copyright 2007-2012 Niels Provos and Nick Mathewson
6*c43e99fdSEd Maste  *
7*c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
8*c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
9*c43e99fdSEd Maste  * are met:
10*c43e99fdSEd Maste  * 1. Redistributions of source code must retain the above copyright
11*c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer.
12*c43e99fdSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
13*c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer in the
14*c43e99fdSEd Maste  *    documentation and/or other materials provided with the distribution.
15*c43e99fdSEd Maste  * 3. The name of the author may not be used to endorse or promote products
16*c43e99fdSEd Maste  *    derived from this software without specific prior written permission.
17*c43e99fdSEd Maste  *
18*c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY SUN MICROSYSTEMS, INC. ``AS IS'' AND ANY
19*c43e99fdSEd Maste  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20*c43e99fdSEd Maste  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21*c43e99fdSEd Maste  * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY
22*c43e99fdSEd Maste  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23*c43e99fdSEd Maste  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24*c43e99fdSEd Maste  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25*c43e99fdSEd Maste  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*c43e99fdSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27*c43e99fdSEd Maste  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*c43e99fdSEd Maste  */
29*c43e99fdSEd Maste 
30*c43e99fdSEd Maste /*
31*c43e99fdSEd Maste  * Copyright (c) 2007 Sun Microsystems. All rights reserved.
32*c43e99fdSEd Maste  * Use is subject to license terms.
33*c43e99fdSEd Maste  */
34*c43e99fdSEd Maste 
35*c43e99fdSEd Maste /*
36*c43e99fdSEd Maste  * evport.c: event backend using Solaris 10 event ports. See port_create(3C).
37*c43e99fdSEd Maste  * This implementation is loosely modeled after the one used for select(2) (in
38*c43e99fdSEd Maste  * select.c).
39*c43e99fdSEd Maste  *
40*c43e99fdSEd Maste  * The outstanding events are tracked in a data structure called evport_data.
41*c43e99fdSEd Maste  * Each entry in the ed_fds array corresponds to a file descriptor, and contains
42*c43e99fdSEd Maste  * pointers to the read and write events that correspond to that fd. (That is,
43*c43e99fdSEd Maste  * when the file is readable, the "read" event should handle it, etc.)
44*c43e99fdSEd Maste  *
45*c43e99fdSEd Maste  * evport_add and evport_del update this data structure. evport_dispatch uses it
46*c43e99fdSEd Maste  * to determine where to callback when an event occurs (which it gets from
47*c43e99fdSEd Maste  * port_getn).
48*c43e99fdSEd Maste  *
49*c43e99fdSEd Maste  * Helper functions are used: grow() grows the file descriptor array as
50*c43e99fdSEd Maste  * necessary when large fd's come in. reassociate() takes care of maintaining
51*c43e99fdSEd Maste  * the proper file-descriptor/event-port associations.
52*c43e99fdSEd Maste  *
53*c43e99fdSEd Maste  * As in the select(2) implementation, signals are handled by evsignal.
54*c43e99fdSEd Maste  */
55*c43e99fdSEd Maste 
56*c43e99fdSEd Maste #include "event2/event-config.h"
57*c43e99fdSEd Maste #include "evconfig-private.h"
58*c43e99fdSEd Maste 
59*c43e99fdSEd Maste #ifdef EVENT__HAVE_EVENT_PORTS
60*c43e99fdSEd Maste 
61*c43e99fdSEd Maste #include <sys/time.h>
62*c43e99fdSEd Maste #include <sys/queue.h>
63*c43e99fdSEd Maste #include <errno.h>
64*c43e99fdSEd Maste #include <poll.h>
65*c43e99fdSEd Maste #include <port.h>
66*c43e99fdSEd Maste #include <signal.h>
67*c43e99fdSEd Maste #include <stdio.h>
68*c43e99fdSEd Maste #include <stdlib.h>
69*c43e99fdSEd Maste #include <string.h>
70*c43e99fdSEd Maste #include <time.h>
71*c43e99fdSEd Maste #include <unistd.h>
72*c43e99fdSEd Maste 
73*c43e99fdSEd Maste #include "event2/thread.h"
74*c43e99fdSEd Maste 
75*c43e99fdSEd Maste #include "evthread-internal.h"
76*c43e99fdSEd Maste #include "event-internal.h"
77*c43e99fdSEd Maste #include "log-internal.h"
78*c43e99fdSEd Maste #include "evsignal-internal.h"
79*c43e99fdSEd Maste #include "evmap-internal.h"
80*c43e99fdSEd Maste 
81*c43e99fdSEd Maste #define INITIAL_EVENTS_PER_GETN 8
82*c43e99fdSEd Maste #define MAX_EVENTS_PER_GETN 4096
83*c43e99fdSEd Maste 
84*c43e99fdSEd Maste /*
85*c43e99fdSEd Maste  * Per-file-descriptor information about what events we're subscribed to. These
86*c43e99fdSEd Maste  * fields are NULL if no event is subscribed to either of them.
87*c43e99fdSEd Maste  */
88*c43e99fdSEd Maste 
89*c43e99fdSEd Maste struct fd_info {
90*c43e99fdSEd Maste 	/* combinations of EV_READ and EV_WRITE */
91*c43e99fdSEd Maste 	short fdi_what;
92*c43e99fdSEd Maste 	/* Index of this fd within ed_pending, plus 1.  Zero if this fd is
93*c43e99fdSEd Maste 	 * not in ed_pending.  (The +1 is a hack so that memset(0) will set
94*c43e99fdSEd Maste 	 * it to a nil index. */
95*c43e99fdSEd Maste 	int pending_idx_plus_1;
96*c43e99fdSEd Maste };
97*c43e99fdSEd Maste 
98*c43e99fdSEd Maste #define FDI_HAS_READ(fdi)  ((fdi)->fdi_what & EV_READ)
99*c43e99fdSEd Maste #define FDI_HAS_WRITE(fdi) ((fdi)->fdi_what & EV_WRITE)
100*c43e99fdSEd Maste #define FDI_HAS_EVENTS(fdi) (FDI_HAS_READ(fdi) || FDI_HAS_WRITE(fdi))
101*c43e99fdSEd Maste #define FDI_TO_SYSEVENTS(fdi) (FDI_HAS_READ(fdi) ? POLLIN : 0) | \
102*c43e99fdSEd Maste     (FDI_HAS_WRITE(fdi) ? POLLOUT : 0)
103*c43e99fdSEd Maste 
104*c43e99fdSEd Maste struct evport_data {
105*c43e99fdSEd Maste 	int		ed_port;	/* event port for system events  */
106*c43e99fdSEd Maste 	/* How many elements of ed_pending should we look at? */
107*c43e99fdSEd Maste 	int ed_npending;
108*c43e99fdSEd Maste 	/* How many elements are allocated in ed_pending and pevtlist? */
109*c43e99fdSEd Maste 	int ed_maxevents;
110*c43e99fdSEd Maste 	/* fdi's that we need to reassoc */
111*c43e99fdSEd Maste 	int *ed_pending;
112*c43e99fdSEd Maste 	/* storage space for incoming events. */
113*c43e99fdSEd Maste 	port_event_t *ed_pevtlist;
114*c43e99fdSEd Maste 
115*c43e99fdSEd Maste };
116*c43e99fdSEd Maste 
117*c43e99fdSEd Maste static void*	evport_init(struct event_base *);
118*c43e99fdSEd Maste static int evport_add(struct event_base *, int fd, short old, short events, void *);
119*c43e99fdSEd Maste static int evport_del(struct event_base *, int fd, short old, short events, void *);
120*c43e99fdSEd Maste static int	evport_dispatch(struct event_base *, struct timeval *);
121*c43e99fdSEd Maste static void	evport_dealloc(struct event_base *);
122*c43e99fdSEd Maste static int	grow(struct evport_data *, int min_events);
123*c43e99fdSEd Maste 
124*c43e99fdSEd Maste const struct eventop evportops = {
125*c43e99fdSEd Maste 	"evport",
126*c43e99fdSEd Maste 	evport_init,
127*c43e99fdSEd Maste 	evport_add,
128*c43e99fdSEd Maste 	evport_del,
129*c43e99fdSEd Maste 	evport_dispatch,
130*c43e99fdSEd Maste 	evport_dealloc,
131*c43e99fdSEd Maste 	1, /* need reinit */
132*c43e99fdSEd Maste 	0, /* features */
133*c43e99fdSEd Maste 	sizeof(struct fd_info), /* fdinfo length */
134*c43e99fdSEd Maste };
135*c43e99fdSEd Maste 
136*c43e99fdSEd Maste /*
137*c43e99fdSEd Maste  * Initialize the event port implementation.
138*c43e99fdSEd Maste  */
139*c43e99fdSEd Maste 
140*c43e99fdSEd Maste static void*
evport_init(struct event_base * base)141*c43e99fdSEd Maste evport_init(struct event_base *base)
142*c43e99fdSEd Maste {
143*c43e99fdSEd Maste 	struct evport_data *evpd;
144*c43e99fdSEd Maste 
145*c43e99fdSEd Maste 	if (!(evpd = mm_calloc(1, sizeof(struct evport_data))))
146*c43e99fdSEd Maste 		return (NULL);
147*c43e99fdSEd Maste 
148*c43e99fdSEd Maste 	if ((evpd->ed_port = port_create()) == -1) {
149*c43e99fdSEd Maste 		mm_free(evpd);
150*c43e99fdSEd Maste 		return (NULL);
151*c43e99fdSEd Maste 	}
152*c43e99fdSEd Maste 
153*c43e99fdSEd Maste 	if (grow(evpd, INITIAL_EVENTS_PER_GETN) < 0) {
154*c43e99fdSEd Maste 		close(evpd->ed_port);
155*c43e99fdSEd Maste 		mm_free(evpd);
156*c43e99fdSEd Maste 		return NULL;
157*c43e99fdSEd Maste 	}
158*c43e99fdSEd Maste 
159*c43e99fdSEd Maste 	evpd->ed_npending = 0;
160*c43e99fdSEd Maste 
161*c43e99fdSEd Maste 	evsig_init_(base);
162*c43e99fdSEd Maste 
163*c43e99fdSEd Maste 	return (evpd);
164*c43e99fdSEd Maste }
165*c43e99fdSEd Maste 
166*c43e99fdSEd Maste static int
grow(struct evport_data * data,int min_events)167*c43e99fdSEd Maste grow(struct evport_data *data, int min_events)
168*c43e99fdSEd Maste {
169*c43e99fdSEd Maste 	int newsize;
170*c43e99fdSEd Maste 	int *new_pending;
171*c43e99fdSEd Maste 	port_event_t *new_pevtlist;
172*c43e99fdSEd Maste 	if (data->ed_maxevents) {
173*c43e99fdSEd Maste 		newsize = data->ed_maxevents;
174*c43e99fdSEd Maste 		do {
175*c43e99fdSEd Maste 			newsize *= 2;
176*c43e99fdSEd Maste 		} while (newsize < min_events);
177*c43e99fdSEd Maste 	} else {
178*c43e99fdSEd Maste 		newsize = min_events;
179*c43e99fdSEd Maste 	}
180*c43e99fdSEd Maste 
181*c43e99fdSEd Maste 	new_pending = mm_realloc(data->ed_pending, sizeof(int)*newsize);
182*c43e99fdSEd Maste 	if (new_pending == NULL)
183*c43e99fdSEd Maste 		return -1;
184*c43e99fdSEd Maste 	data->ed_pending = new_pending;
185*c43e99fdSEd Maste 	new_pevtlist = mm_realloc(data->ed_pevtlist, sizeof(port_event_t)*newsize);
186*c43e99fdSEd Maste 	if (new_pevtlist == NULL)
187*c43e99fdSEd Maste 		return -1;
188*c43e99fdSEd Maste 	data->ed_pevtlist = new_pevtlist;
189*c43e99fdSEd Maste 
190*c43e99fdSEd Maste 	data->ed_maxevents = newsize;
191*c43e99fdSEd Maste 	return 0;
192*c43e99fdSEd Maste }
193*c43e99fdSEd Maste 
194*c43e99fdSEd Maste #ifdef CHECK_INVARIANTS
195*c43e99fdSEd Maste /*
196*c43e99fdSEd Maste  * Checks some basic properties about the evport_data structure. Because it
197*c43e99fdSEd Maste  * checks all file descriptors, this function can be expensive when the maximum
198*c43e99fdSEd Maste  * file descriptor ever used is rather large.
199*c43e99fdSEd Maste  */
200*c43e99fdSEd Maste 
201*c43e99fdSEd Maste static void
check_evportop(struct evport_data * evpd)202*c43e99fdSEd Maste check_evportop(struct evport_data *evpd)
203*c43e99fdSEd Maste {
204*c43e99fdSEd Maste 	EVUTIL_ASSERT(evpd);
205*c43e99fdSEd Maste 	EVUTIL_ASSERT(evpd->ed_port > 0);
206*c43e99fdSEd Maste }
207*c43e99fdSEd Maste 
208*c43e99fdSEd Maste /*
209*c43e99fdSEd Maste  * Verifies very basic integrity of a given port_event.
210*c43e99fdSEd Maste  */
211*c43e99fdSEd Maste static void
check_event(port_event_t * pevt)212*c43e99fdSEd Maste check_event(port_event_t* pevt)
213*c43e99fdSEd Maste {
214*c43e99fdSEd Maste 	/*
215*c43e99fdSEd Maste 	 * We've only registered for PORT_SOURCE_FD events. The only
216*c43e99fdSEd Maste 	 * other thing we can legitimately receive is PORT_SOURCE_ALERT,
217*c43e99fdSEd Maste 	 * but since we're not using port_alert either, we can assume
218*c43e99fdSEd Maste 	 * PORT_SOURCE_FD.
219*c43e99fdSEd Maste 	 */
220*c43e99fdSEd Maste 	EVUTIL_ASSERT(pevt->portev_source == PORT_SOURCE_FD);
221*c43e99fdSEd Maste }
222*c43e99fdSEd Maste 
223*c43e99fdSEd Maste #else
224*c43e99fdSEd Maste #define check_evportop(epop)
225*c43e99fdSEd Maste #define check_event(pevt)
226*c43e99fdSEd Maste #endif /* CHECK_INVARIANTS */
227*c43e99fdSEd Maste 
228*c43e99fdSEd Maste /*
229*c43e99fdSEd Maste  * (Re)associates the given file descriptor with the event port. The OS events
230*c43e99fdSEd Maste  * are specified (implicitly) from the fd_info struct.
231*c43e99fdSEd Maste  */
232*c43e99fdSEd Maste static int
reassociate(struct evport_data * epdp,struct fd_info * fdip,int fd)233*c43e99fdSEd Maste reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd)
234*c43e99fdSEd Maste {
235*c43e99fdSEd Maste 	int sysevents = FDI_TO_SYSEVENTS(fdip);
236*c43e99fdSEd Maste 
237*c43e99fdSEd Maste 	if (sysevents != 0) {
238*c43e99fdSEd Maste 		if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
239*c43e99fdSEd Maste 				   fd, sysevents, fdip) == -1) {
240*c43e99fdSEd Maste 			event_warn("port_associate");
241*c43e99fdSEd Maste 			return (-1);
242*c43e99fdSEd Maste 		}
243*c43e99fdSEd Maste 	}
244*c43e99fdSEd Maste 
245*c43e99fdSEd Maste 	check_evportop(epdp);
246*c43e99fdSEd Maste 
247*c43e99fdSEd Maste 	return (0);
248*c43e99fdSEd Maste }
249*c43e99fdSEd Maste 
250*c43e99fdSEd Maste /*
251*c43e99fdSEd Maste  * Main event loop - polls port_getn for some number of events, and processes
252*c43e99fdSEd Maste  * them.
253*c43e99fdSEd Maste  */
254*c43e99fdSEd Maste 
255*c43e99fdSEd Maste static int
evport_dispatch(struct event_base * base,struct timeval * tv)256*c43e99fdSEd Maste evport_dispatch(struct event_base *base, struct timeval *tv)
257*c43e99fdSEd Maste {
258*c43e99fdSEd Maste 	int i, res;
259*c43e99fdSEd Maste 	struct evport_data *epdp = base->evbase;
260*c43e99fdSEd Maste 	port_event_t *pevtlist = epdp->ed_pevtlist;
261*c43e99fdSEd Maste 
262*c43e99fdSEd Maste 	/*
263*c43e99fdSEd Maste 	 * port_getn will block until it has at least nevents events. It will
264*c43e99fdSEd Maste 	 * also return how many it's given us (which may be more than we asked
265*c43e99fdSEd Maste 	 * for, as long as it's less than our maximum (ed_maxevents)) in
266*c43e99fdSEd Maste 	 * nevents.
267*c43e99fdSEd Maste 	 */
268*c43e99fdSEd Maste 	int nevents = 1;
269*c43e99fdSEd Maste 
270*c43e99fdSEd Maste 	/*
271*c43e99fdSEd Maste 	 * We have to convert a struct timeval to a struct timespec
272*c43e99fdSEd Maste 	 * (only difference is nanoseconds vs. microseconds). If no time-based
273*c43e99fdSEd Maste 	 * events are active, we should wait for I/O (and tv == NULL).
274*c43e99fdSEd Maste 	 */
275*c43e99fdSEd Maste 	struct timespec ts;
276*c43e99fdSEd Maste 	struct timespec *ts_p = NULL;
277*c43e99fdSEd Maste 	if (tv != NULL) {
278*c43e99fdSEd Maste 		ts.tv_sec = tv->tv_sec;
279*c43e99fdSEd Maste 		ts.tv_nsec = tv->tv_usec * 1000;
280*c43e99fdSEd Maste 		ts_p = &ts;
281*c43e99fdSEd Maste 	}
282*c43e99fdSEd Maste 
283*c43e99fdSEd Maste 	/*
284*c43e99fdSEd Maste 	 * Before doing anything else, we need to reassociate the events we hit
285*c43e99fdSEd Maste 	 * last time which need reassociation. See comment at the end of the
286*c43e99fdSEd Maste 	 * loop below.
287*c43e99fdSEd Maste 	 */
288*c43e99fdSEd Maste 	for (i = 0; i < epdp->ed_npending; ++i) {
289*c43e99fdSEd Maste 		struct fd_info *fdi = NULL;
290*c43e99fdSEd Maste 		const int fd = epdp->ed_pending[i];
291*c43e99fdSEd Maste 		if (fd != -1) {
292*c43e99fdSEd Maste 			/* We might have cleared out this event; we need
293*c43e99fdSEd Maste 			 * to be sure that it's still set. */
294*c43e99fdSEd Maste 			fdi = evmap_io_get_fdinfo_(&base->io, fd);
295*c43e99fdSEd Maste 		}
296*c43e99fdSEd Maste 
297*c43e99fdSEd Maste 		if (fdi != NULL && FDI_HAS_EVENTS(fdi)) {
298*c43e99fdSEd Maste 			reassociate(epdp, fdi, fd);
299*c43e99fdSEd Maste 			/* epdp->ed_pending[i] = -1; */
300*c43e99fdSEd Maste 			fdi->pending_idx_plus_1 = 0;
301*c43e99fdSEd Maste 		}
302*c43e99fdSEd Maste 	}
303*c43e99fdSEd Maste 
304*c43e99fdSEd Maste 	EVBASE_RELEASE_LOCK(base, th_base_lock);
305*c43e99fdSEd Maste 
306*c43e99fdSEd Maste 	res = port_getn(epdp->ed_port, pevtlist, epdp->ed_maxevents,
307*c43e99fdSEd Maste 	    (unsigned int *) &nevents, ts_p);
308*c43e99fdSEd Maste 
309*c43e99fdSEd Maste 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
310*c43e99fdSEd Maste 
311*c43e99fdSEd Maste 	if (res == -1) {
312*c43e99fdSEd Maste 		if (errno == EINTR || errno == EAGAIN) {
313*c43e99fdSEd Maste 			return (0);
314*c43e99fdSEd Maste 		} else if (errno == ETIME) {
315*c43e99fdSEd Maste 			if (nevents == 0)
316*c43e99fdSEd Maste 				return (0);
317*c43e99fdSEd Maste 		} else {
318*c43e99fdSEd Maste 			event_warn("port_getn");
319*c43e99fdSEd Maste 			return (-1);
320*c43e99fdSEd Maste 		}
321*c43e99fdSEd Maste 	}
322*c43e99fdSEd Maste 
323*c43e99fdSEd Maste 	event_debug(("%s: port_getn reports %d events", __func__, nevents));
324*c43e99fdSEd Maste 
325*c43e99fdSEd Maste 	for (i = 0; i < nevents; ++i) {
326*c43e99fdSEd Maste 		port_event_t *pevt = &pevtlist[i];
327*c43e99fdSEd Maste 		int fd = (int) pevt->portev_object;
328*c43e99fdSEd Maste 		struct fd_info *fdi = pevt->portev_user;
329*c43e99fdSEd Maste 		/*EVUTIL_ASSERT(evmap_io_get_fdinfo_(&base->io, fd) == fdi);*/
330*c43e99fdSEd Maste 
331*c43e99fdSEd Maste 		check_evportop(epdp);
332*c43e99fdSEd Maste 		check_event(pevt);
333*c43e99fdSEd Maste 		epdp->ed_pending[i] = fd;
334*c43e99fdSEd Maste 		fdi->pending_idx_plus_1 = i + 1;
335*c43e99fdSEd Maste 
336*c43e99fdSEd Maste 		/*
337*c43e99fdSEd Maste 		 * Figure out what kind of event it was
338*c43e99fdSEd Maste 		 * (because we have to pass this to the callback)
339*c43e99fdSEd Maste 		 */
340*c43e99fdSEd Maste 		res = 0;
341*c43e99fdSEd Maste 		if (pevt->portev_events & (POLLERR|POLLHUP)) {
342*c43e99fdSEd Maste 			res = EV_READ | EV_WRITE;
343*c43e99fdSEd Maste 		} else {
344*c43e99fdSEd Maste 			if (pevt->portev_events & POLLIN)
345*c43e99fdSEd Maste 				res |= EV_READ;
346*c43e99fdSEd Maste 			if (pevt->portev_events & POLLOUT)
347*c43e99fdSEd Maste 				res |= EV_WRITE;
348*c43e99fdSEd Maste 		}
349*c43e99fdSEd Maste 
350*c43e99fdSEd Maste 		/*
351*c43e99fdSEd Maste 		 * Check for the error situations or a hangup situation
352*c43e99fdSEd Maste 		 */
353*c43e99fdSEd Maste 		if (pevt->portev_events & (POLLERR|POLLHUP|POLLNVAL))
354*c43e99fdSEd Maste 			res |= EV_READ|EV_WRITE;
355*c43e99fdSEd Maste 
356*c43e99fdSEd Maste 		evmap_io_active_(base, fd, res);
357*c43e99fdSEd Maste 	} /* end of all events gotten */
358*c43e99fdSEd Maste 	epdp->ed_npending = nevents;
359*c43e99fdSEd Maste 
360*c43e99fdSEd Maste 	if (nevents == epdp->ed_maxevents &&
361*c43e99fdSEd Maste 	    epdp->ed_maxevents < MAX_EVENTS_PER_GETN) {
362*c43e99fdSEd Maste 		/* we used all the space this time.  We should be ready
363*c43e99fdSEd Maste 		 * for more events next time around. */
364*c43e99fdSEd Maste 		grow(epdp, epdp->ed_maxevents * 2);
365*c43e99fdSEd Maste 	}
366*c43e99fdSEd Maste 
367*c43e99fdSEd Maste 	check_evportop(epdp);
368*c43e99fdSEd Maste 
369*c43e99fdSEd Maste 	return (0);
370*c43e99fdSEd Maste }
371*c43e99fdSEd Maste 
372*c43e99fdSEd Maste 
373*c43e99fdSEd Maste /*
374*c43e99fdSEd Maste  * Adds the given event (so that you will be notified when it happens via
375*c43e99fdSEd Maste  * the callback function).
376*c43e99fdSEd Maste  */
377*c43e99fdSEd Maste 
378*c43e99fdSEd Maste static int
evport_add(struct event_base * base,int fd,short old,short events,void * p)379*c43e99fdSEd Maste evport_add(struct event_base *base, int fd, short old, short events, void *p)
380*c43e99fdSEd Maste {
381*c43e99fdSEd Maste 	struct evport_data *evpd = base->evbase;
382*c43e99fdSEd Maste 	struct fd_info *fdi = p;
383*c43e99fdSEd Maste 
384*c43e99fdSEd Maste 	check_evportop(evpd);
385*c43e99fdSEd Maste 
386*c43e99fdSEd Maste 	fdi->fdi_what |= events;
387*c43e99fdSEd Maste 
388*c43e99fdSEd Maste 	return reassociate(evpd, fdi, fd);
389*c43e99fdSEd Maste }
390*c43e99fdSEd Maste 
391*c43e99fdSEd Maste /*
392*c43e99fdSEd Maste  * Removes the given event from the list of events to wait for.
393*c43e99fdSEd Maste  */
394*c43e99fdSEd Maste 
395*c43e99fdSEd Maste static int
evport_del(struct event_base * base,int fd,short old,short events,void * p)396*c43e99fdSEd Maste evport_del(struct event_base *base, int fd, short old, short events, void *p)
397*c43e99fdSEd Maste {
398*c43e99fdSEd Maste 	struct evport_data *evpd = base->evbase;
399*c43e99fdSEd Maste 	struct fd_info *fdi = p;
400*c43e99fdSEd Maste 	int associated = ! fdi->pending_idx_plus_1;
401*c43e99fdSEd Maste 
402*c43e99fdSEd Maste 	check_evportop(evpd);
403*c43e99fdSEd Maste 
404*c43e99fdSEd Maste 	fdi->fdi_what &= ~(events &(EV_READ|EV_WRITE));
405*c43e99fdSEd Maste 
406*c43e99fdSEd Maste 	if (associated) {
407*c43e99fdSEd Maste 		if (!FDI_HAS_EVENTS(fdi) &&
408*c43e99fdSEd Maste 		    port_dissociate(evpd->ed_port, PORT_SOURCE_FD, fd) == -1) {
409*c43e99fdSEd Maste 			/*
410*c43e99fdSEd Maste 			 * Ignore EBADFD error the fd could have been closed
411*c43e99fdSEd Maste 			 * before event_del() was called.
412*c43e99fdSEd Maste 			 */
413*c43e99fdSEd Maste 			if (errno != EBADFD) {
414*c43e99fdSEd Maste 				event_warn("port_dissociate");
415*c43e99fdSEd Maste 				return (-1);
416*c43e99fdSEd Maste 			}
417*c43e99fdSEd Maste 		} else {
418*c43e99fdSEd Maste 			if (FDI_HAS_EVENTS(fdi)) {
419*c43e99fdSEd Maste 				return (reassociate(evpd, fdi, fd));
420*c43e99fdSEd Maste 			}
421*c43e99fdSEd Maste 		}
422*c43e99fdSEd Maste 	} else {
423*c43e99fdSEd Maste 		if ((fdi->fdi_what & (EV_READ|EV_WRITE)) == 0) {
424*c43e99fdSEd Maste 			const int i = fdi->pending_idx_plus_1 - 1;
425*c43e99fdSEd Maste 			EVUTIL_ASSERT(evpd->ed_pending[i] == fd);
426*c43e99fdSEd Maste 			evpd->ed_pending[i] = -1;
427*c43e99fdSEd Maste 			fdi->pending_idx_plus_1 = 0;
428*c43e99fdSEd Maste 		}
429*c43e99fdSEd Maste 	}
430*c43e99fdSEd Maste 	return 0;
431*c43e99fdSEd Maste }
432*c43e99fdSEd Maste 
433*c43e99fdSEd Maste 
434*c43e99fdSEd Maste static void
evport_dealloc(struct event_base * base)435*c43e99fdSEd Maste evport_dealloc(struct event_base *base)
436*c43e99fdSEd Maste {
437*c43e99fdSEd Maste 	struct evport_data *evpd = base->evbase;
438*c43e99fdSEd Maste 
439*c43e99fdSEd Maste 	evsig_dealloc_(base);
440*c43e99fdSEd Maste 
441*c43e99fdSEd Maste 	close(evpd->ed_port);
442*c43e99fdSEd Maste 
443*c43e99fdSEd Maste 	if (evpd->ed_pending)
444*c43e99fdSEd Maste 		mm_free(evpd->ed_pending);
445*c43e99fdSEd Maste 	if (evpd->ed_pevtlist)
446*c43e99fdSEd Maste 		mm_free(evpd->ed_pevtlist);
447*c43e99fdSEd Maste 
448*c43e99fdSEd Maste 	mm_free(evpd);
449*c43e99fdSEd Maste }
450*c43e99fdSEd Maste 
451*c43e99fdSEd Maste #endif /* EVENT__HAVE_EVENT_PORTS */
452