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