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