1/*
2 * Copyright 2000-2009 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2009-2012 Niels Provos and Nick Mathewson
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27#include "event2/event-config.h"
28
29#include <sys/types.h>
30#include <sys/resource.h>
31#ifdef _EVENT_HAVE_SYS_TIME_H
32#include <sys/time.h>
33#endif
34#include <sys/queue.h>
35#include <sys/devpoll.h>
36#include <signal.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41#include <fcntl.h>
42#include <errno.h>
43
44#include "event2/event.h"
45#include "event2/event_struct.h"
46#include "event2/thread.h"
47#include "event-internal.h"
48#include "evsignal-internal.h"
49#include "log-internal.h"
50#include "evmap-internal.h"
51#include "evthread-internal.h"
52
53struct devpollop {
54	struct pollfd *events;
55	int nevents;
56	int dpfd;
57	struct pollfd *changes;
58	int nchanges;
59};
60
61static void *devpoll_init(struct event_base *);
62static int devpoll_add(struct event_base *, int fd, short old, short events, void *);
63static int devpoll_del(struct event_base *, int fd, short old, short events, void *);
64static int devpoll_dispatch(struct event_base *, struct timeval *);
65static void devpoll_dealloc(struct event_base *);
66
67const struct eventop devpollops = {
68	"devpoll",
69	devpoll_init,
70	devpoll_add,
71	devpoll_del,
72	devpoll_dispatch,
73	devpoll_dealloc,
74	1, /* need reinit */
75	EV_FEATURE_FDS|EV_FEATURE_O1,
76	0
77};
78
79#define NEVENT	32000
80
81static int
82devpoll_commit(struct devpollop *devpollop)
83{
84	/*
85	 * Due to a bug in Solaris, we have to use pwrite with an offset of 0.
86	 * Write is limited to 2GB of data, until it will fail.
87	 */
88	if (pwrite(devpollop->dpfd, devpollop->changes,
89		sizeof(struct pollfd) * devpollop->nchanges, 0) == -1)
90		return (-1);
91
92	devpollop->nchanges = 0;
93	return (0);
94}
95
96static int
97devpoll_queue(struct devpollop *devpollop, int fd, int events) {
98	struct pollfd *pfd;
99
100	if (devpollop->nchanges >= devpollop->nevents) {
101		/*
102		 * Change buffer is full, must commit it to /dev/poll before
103		 * adding more
104		 */
105		if (devpoll_commit(devpollop) != 0)
106			return (-1);
107	}
108
109	pfd = &devpollop->changes[devpollop->nchanges++];
110	pfd->fd = fd;
111	pfd->events = events;
112	pfd->revents = 0;
113
114	return (0);
115}
116
117static void *
118devpoll_init(struct event_base *base)
119{
120	int dpfd, nfiles = NEVENT;
121	struct rlimit rl;
122	struct devpollop *devpollop;
123
124	if (!(devpollop = mm_calloc(1, sizeof(struct devpollop))))
125		return (NULL);
126
127	if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
128	    rl.rlim_cur != RLIM_INFINITY)
129		nfiles = rl.rlim_cur;
130
131	/* Initialize the kernel queue */
132	if ((dpfd = evutil_open_closeonexec("/dev/poll", O_RDWR, 0)) == -1) {
133		event_warn("open: /dev/poll");
134		mm_free(devpollop);
135		return (NULL);
136	}
137
138	devpollop->dpfd = dpfd;
139
140	/* Initialize fields */
141	/* FIXME: allocating 'nfiles' worth of space here can be
142	 * expensive and unnecessary.  See how epoll.c does it instead. */
143	devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd));
144	if (devpollop->events == NULL) {
145		mm_free(devpollop);
146		close(dpfd);
147		return (NULL);
148	}
149	devpollop->nevents = nfiles;
150
151	devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd));
152	if (devpollop->changes == NULL) {
153		mm_free(devpollop->events);
154		mm_free(devpollop);
155		close(dpfd);
156		return (NULL);
157	}
158
159	evsig_init(base);
160
161	return (devpollop);
162}
163
164static int
165devpoll_dispatch(struct event_base *base, struct timeval *tv)
166{
167	struct devpollop *devpollop = base->evbase;
168	struct pollfd *events = devpollop->events;
169	struct dvpoll dvp;
170	int i, res, timeout = -1;
171
172	if (devpollop->nchanges)
173		devpoll_commit(devpollop);
174
175	if (tv != NULL)
176		timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
177
178	dvp.dp_fds = devpollop->events;
179	dvp.dp_nfds = devpollop->nevents;
180	dvp.dp_timeout = timeout;
181
182	EVBASE_RELEASE_LOCK(base, th_base_lock);
183
184	res = ioctl(devpollop->dpfd, DP_POLL, &dvp);
185
186	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
187
188	if (res == -1) {
189		if (errno != EINTR) {
190			event_warn("ioctl: DP_POLL");
191			return (-1);
192		}
193
194		return (0);
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
224static int
225devpoll_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
250static int
251devpoll_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
291static void
292devpoll_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