xref: /netbsd-src/external/bsd/libevent/dist/devpoll.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: devpoll.c,v 1.1.1.2 2017/01/31 21:14:52 christos Exp $	*/
2 /*
3  * Copyright 2000-2009 Niels Provos <provos@citi.umich.edu>
4  * Copyright 2009-2012 Niels Provos and Nick Mathewson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include "event2/event-config.h"
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: devpoll.c,v 1.1.1.2 2017/01/31 21:14:52 christos Exp $");
31 #include "evconfig-private.h"
32 
33 #ifdef EVENT__HAVE_DEVPOLL
34 
35 #include <sys/types.h>
36 #include <sys/resource.h>
37 #ifdef EVENT__HAVE_SYS_TIME_H
38 #include <sys/time.h>
39 #endif
40 #include <sys/queue.h>
41 #include <sys/devpoll.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 
50 #include "event2/event.h"
51 #include "event2/event_struct.h"
52 #include "event2/thread.h"
53 #include "event-internal.h"
54 #include "evsignal-internal.h"
55 #include "log-internal.h"
56 #include "evmap-internal.h"
57 #include "evthread-internal.h"
58 
59 struct devpollop {
60 	struct pollfd *events;
61 	int nevents;
62 	int dpfd;
63 	struct pollfd *changes;
64 	int nchanges;
65 };
66 
67 static void *devpoll_init(struct event_base *);
68 static int devpoll_add(struct event_base *, int fd, short old, short events, void *);
69 static int devpoll_del(struct event_base *, int fd, short old, short events, void *);
70 static int devpoll_dispatch(struct event_base *, struct timeval *);
71 static void devpoll_dealloc(struct event_base *);
72 
73 const struct eventop devpollops = {
74 	"devpoll",
75 	devpoll_init,
76 	devpoll_add,
77 	devpoll_del,
78 	devpoll_dispatch,
79 	devpoll_dealloc,
80 	1, /* need reinit */
81 	EV_FEATURE_FDS|EV_FEATURE_O1,
82 	0
83 };
84 
85 #define NEVENT	32000
86 
87 static int
88 devpoll_commit(struct devpollop *devpollop)
89 {
90 	/*
91 	 * Due to a bug in Solaris, we have to use pwrite with an offset of 0.
92 	 * Write is limited to 2GB of data, until it will fail.
93 	 */
94 	if (pwrite(devpollop->dpfd, devpollop->changes,
95 		sizeof(struct pollfd) * devpollop->nchanges, 0) == -1)
96 		return (-1);
97 
98 	devpollop->nchanges = 0;
99 	return (0);
100 }
101 
102 static int
103 devpoll_queue(struct devpollop *devpollop, int fd, int events) {
104 	struct pollfd *pfd;
105 
106 	if (devpollop->nchanges >= devpollop->nevents) {
107 		/*
108 		 * Change buffer is full, must commit it to /dev/poll before
109 		 * adding more
110 		 */
111 		if (devpoll_commit(devpollop) != 0)
112 			return (-1);
113 	}
114 
115 	pfd = &devpollop->changes[devpollop->nchanges++];
116 	pfd->fd = fd;
117 	pfd->events = events;
118 	pfd->revents = 0;
119 
120 	return (0);
121 }
122 
123 static void *
124 devpoll_init(struct event_base *base)
125 {
126 	int dpfd, nfiles = NEVENT;
127 	struct rlimit rl;
128 	struct devpollop *devpollop;
129 
130 	if (!(devpollop = mm_calloc(1, sizeof(struct devpollop))))
131 		return (NULL);
132 
133 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
134 	    rl.rlim_cur != RLIM_INFINITY)
135 		nfiles = rl.rlim_cur;
136 
137 	/* Initialize the kernel queue */
138 	if ((dpfd = evutil_open_closeonexec_("/dev/poll", O_RDWR, 0)) == -1) {
139 		event_warn("open: /dev/poll");
140 		mm_free(devpollop);
141 		return (NULL);
142 	}
143 
144 	devpollop->dpfd = dpfd;
145 
146 	/* Initialize fields */
147 	/* FIXME: allocating 'nfiles' worth of space here can be
148 	 * expensive and unnecessary.  See how epoll.c does it instead. */
149 	devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd));
150 	if (devpollop->events == NULL) {
151 		mm_free(devpollop);
152 		close(dpfd);
153 		return (NULL);
154 	}
155 	devpollop->nevents = nfiles;
156 
157 	devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd));
158 	if (devpollop->changes == NULL) {
159 		mm_free(devpollop->events);
160 		mm_free(devpollop);
161 		close(dpfd);
162 		return (NULL);
163 	}
164 
165 	evsig_init_(base);
166 
167 	return (devpollop);
168 }
169 
170 static int
171 devpoll_dispatch(struct event_base *base, struct timeval *tv)
172 {
173 	struct devpollop *devpollop = base->evbase;
174 	struct pollfd *events = devpollop->events;
175 	struct dvpoll dvp;
176 	int i, res, timeout = -1;
177 
178 	if (devpollop->nchanges)
179 		devpoll_commit(devpollop);
180 
181 	if (tv != NULL)
182 		timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
183 
184 	dvp.dp_fds = devpollop->events;
185 	dvp.dp_nfds = devpollop->nevents;
186 	dvp.dp_timeout = timeout;
187 
188 	EVBASE_RELEASE_LOCK(base, th_base_lock);
189 
190 	res = ioctl(devpollop->dpfd, DP_POLL, &dvp);
191 
192 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
193 
194 	if (res == -1) {
195 		if (errno != EINTR) {
196 			event_warn("ioctl: DP_POLL");
197 			return (-1);
198 		}
199 
200 		return (0);
201 	}
202 
203 	event_debug(("%s: devpoll_wait reports %d", __func__, res));
204 
205 	for (i = 0; i < res; i++) {
206 		int which = 0;
207 		int what = events[i].revents;
208 
209 		if (what & POLLHUP)
210 			what |= POLLIN | POLLOUT;
211 		else if (what & POLLERR)
212 			what |= POLLIN | POLLOUT;
213 
214 		if (what & POLLIN)
215 			which |= EV_READ;
216 		if (what & POLLOUT)
217 			which |= EV_WRITE;
218 
219 		if (!which)
220 			continue;
221 
222 		/* XXX(niels): not sure if this works for devpoll */
223 		evmap_io_active_(base, events[i].fd, which);
224 	}
225 
226 	return (0);
227 }
228 
229 
230 static int
231 devpoll_add(struct event_base *base, int fd, short old, short events, void *p)
232 {
233 	struct devpollop *devpollop = base->evbase;
234 	int res;
235 	(void)p;
236 
237 	/*
238 	 * It's not necessary to OR the existing read/write events that we
239 	 * are currently interested in with the new event we are adding.
240 	 * The /dev/poll driver ORs any new events with the existing events
241 	 * that it has cached for the fd.
242 	 */
243 
244 	res = 0;
245 	if (events & EV_READ)
246 		res |= POLLIN;
247 	if (events & EV_WRITE)
248 		res |= POLLOUT;
249 
250 	if (devpoll_queue(devpollop, fd, res) != 0)
251 		return (-1);
252 
253 	return (0);
254 }
255 
256 static int
257 devpoll_del(struct event_base *base, int fd, short old, short events, void *p)
258 {
259 	struct devpollop *devpollop = base->evbase;
260 	int res;
261 	(void)p;
262 
263 	res = 0;
264 	if (events & EV_READ)
265 		res |= POLLIN;
266 	if (events & EV_WRITE)
267 		res |= POLLOUT;
268 
269 	/*
270 	 * The only way to remove an fd from the /dev/poll monitored set is
271 	 * to use POLLREMOVE by itself.  This removes ALL events for the fd
272 	 * provided so if we care about two events and are only removing one
273 	 * we must re-add the other event after POLLREMOVE.
274 	 */
275 
276 	if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0)
277 		return (-1);
278 
279 	if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
280 		/*
281 		 * We're not deleting all events, so we must resubmit the
282 		 * event that we are still interested in if one exists.
283 		 */
284 
285 		if ((res & POLLIN) && (old & EV_WRITE)) {
286 			/* Deleting read, still care about write */
287 			devpoll_queue(devpollop, fd, POLLOUT);
288 		} else if ((res & POLLOUT) && (old & EV_READ)) {
289 			/* Deleting write, still care about read */
290 			devpoll_queue(devpollop, fd, POLLIN);
291 		}
292 	}
293 
294 	return (0);
295 }
296 
297 static void
298 devpoll_dealloc(struct event_base *base)
299 {
300 	struct devpollop *devpollop = base->evbase;
301 
302 	evsig_dealloc_(base);
303 	if (devpollop->events)
304 		mm_free(devpollop->events);
305 	if (devpollop->changes)
306 		mm_free(devpollop->changes);
307 	if (devpollop->dpfd >= 0)
308 		close(devpollop->dpfd);
309 
310 	memset(devpollop, 0, sizeof(struct devpollop));
311 	mm_free(devpollop);
312 }
313 
314 #endif /* EVENT__HAVE_DEVPOLL */
315