xref: /netbsd-src/sys/kern/sys_epoll.c (revision 2c545067c78a4b84d16735051f9ff75bb33c88e8)
1*2c545067Schristos /*	$NetBSD: sys_epoll.c,v 1.4 2023/07/30 18:31:13 christos Exp $	*/
2d11110f4Schristos 
3d11110f4Schristos /*-
4d11110f4Schristos  * SPDX-License-Identifier: BSD-2-Clause
5d11110f4Schristos  *
6d11110f4Schristos  * Copyright (c) 2007 Roman Divacky
7d11110f4Schristos  * Copyright (c) 2014 Dmitry Chagin <dchagin@FreeBSD.org>
8d11110f4Schristos  *
9d11110f4Schristos  * Redistribution and use in source and binary forms, with or without
10d11110f4Schristos  * modification, are permitted provided that the following conditions
11d11110f4Schristos  * are met:
12d11110f4Schristos  * 1. Redistributions of source code must retain the above copyright
13d11110f4Schristos  *    notice, this list of conditions and the following disclaimer.
14d11110f4Schristos  * 2. Redistributions in binary form must reproduce the above copyright
15d11110f4Schristos  *    notice, this list of conditions and the following disclaimer in the
16d11110f4Schristos  *    documentation and/or other materials provided with the distribution.
17d11110f4Schristos  *
18d11110f4Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19d11110f4Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20d11110f4Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21d11110f4Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22d11110f4Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23d11110f4Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24d11110f4Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25d11110f4Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26d11110f4Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27d11110f4Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28d11110f4Schristos  * SUCH DAMAGE.
29d11110f4Schristos  */
30d11110f4Schristos #include <sys/cdefs.h>
31*2c545067Schristos __KERNEL_RCSID(0, "$NetBSD: sys_epoll.c,v 1.4 2023/07/30 18:31:13 christos Exp $");
32d11110f4Schristos 
33d11110f4Schristos 
34d11110f4Schristos #include <sys/param.h>
35d11110f4Schristos #include <sys/types.h>
36d11110f4Schristos #include <sys/bitops.h>
37d11110f4Schristos #include <sys/epoll.h>
38d11110f4Schristos #include <sys/event.h>
39d11110f4Schristos #include <sys/eventvar.h>
40d11110f4Schristos #include <sys/errno.h>
41d11110f4Schristos #include <sys/file.h>
42d11110f4Schristos #include <sys/filedesc.h>
43d11110f4Schristos #include <sys/fcntl.h>
44d11110f4Schristos #include <sys/proc.h>
45d11110f4Schristos #include <sys/signal.h>
46d11110f4Schristos #include <sys/vnode.h>
47d11110f4Schristos 
48d11110f4Schristos #include <sys/syscallargs.h>
49d11110f4Schristos 
50d11110f4Schristos #define	EPOLL_MAX_DEPTH		5
51d11110f4Schristos 
52d11110f4Schristos #define	EPOLL_EVRD	(EPOLLIN|EPOLLRDNORM)
53d11110f4Schristos #define	EPOLL_EVWR	(EPOLLOUT|EPOLLWRNORM)
54d11110f4Schristos #define	EPOLL_EVSUP	(EPOLLET|EPOLLONESHOT|EPOLLHUP|EPOLLERR|EPOLLPRI \
55d11110f4Schristos 			|EPOLL_EVRD|EPOLL_EVWR|EPOLLRDHUP)
56d11110f4Schristos 
57d11110f4Schristos #define	kext_data	ext[0]
58d11110f4Schristos #define	kext_epfd	ext[1]
59d11110f4Schristos #define	kext_fd		ext[2]
60d11110f4Schristos 
61d11110f4Schristos #if DEBUG
62d11110f4Schristos #define	DPRINTF(x) uprintf x
63d11110f4Schristos #else
64d11110f4Schristos #define	DPRINTF(x) __nothing
65d11110f4Schristos #endif
66d11110f4Schristos 
67d11110f4Schristos struct epoll_edge {
68d11110f4Schristos 	int epfd;
69d11110f4Schristos 	int fd;
70d11110f4Schristos };
71d11110f4Schristos 
72d11110f4Schristos __BITMAP_TYPE(epoll_seen, char, 1);
73d11110f4Schristos 
74d11110f4Schristos static int	epoll_to_kevent(int, int, struct epoll_event *, struct kevent *,
75d11110f4Schristos     int *);
76d11110f4Schristos static void	kevent_to_epoll(struct kevent *, struct epoll_event *);
77d11110f4Schristos static int      epoll_kev_put_events(void *, struct kevent *, struct kevent *,
78d11110f4Schristos     size_t, int);
79d11110f4Schristos static int	epoll_kev_fetch_changes(void *, const struct kevent *,
80d11110f4Schristos     struct kevent *, size_t, int);
81d11110f4Schristos static int	epoll_kev_fetch_timeout(const void *, void *, size_t);
82d11110f4Schristos static int	epoll_register_kevent(register_t *, int, int, int,
83d11110f4Schristos     unsigned int);
84d11110f4Schristos static int	epoll_fd_registered(register_t *, int, int);
85d11110f4Schristos static int	epoll_delete_all_events(register_t *, int, int);
86d11110f4Schristos static int	epoll_recover_watch_tree(struct epoll_edge *, size_t, size_t);
87d11110f4Schristos static int	epoll_dfs(struct epoll_edge *, size_t, struct epoll_seen *,
88d11110f4Schristos     size_t, int, int);
89d11110f4Schristos static int	epoll_check_loop_and_depth(struct lwp *, int, int);
90d11110f4Schristos 
91d11110f4Schristos /*
92d11110f4Schristos  * epoll_create1(2).  Parse the flags and then create a kqueue instance.
93d11110f4Schristos  */
94d11110f4Schristos int
sys_epoll_create1(struct lwp * l,const struct sys_epoll_create1_args * uap,register_t * retval)95d11110f4Schristos sys_epoll_create1(struct lwp *l, const struct sys_epoll_create1_args *uap,
96d11110f4Schristos     register_t *retval)
97d11110f4Schristos {
98d11110f4Schristos 	/* {
99d11110f4Schristos 		syscallarg(int) flags;
100d11110f4Schristos 	} */
101d11110f4Schristos 	struct sys_kqueue1_args kqa;
102d11110f4Schristos 
103*2c545067Schristos 	if ((SCARG(uap, flags) & ~(EPOLL_CLOEXEC)) != 0)
104d11110f4Schristos 		return EINVAL;
105d11110f4Schristos 
106*2c545067Schristos 	SCARG(&kqa, flags) = 0;
107*2c545067Schristos 	if (SCARG(uap, flags) & EPOLL_CLOEXEC)
108*2c545067Schristos 		SCARG(&kqa, flags) |= O_CLOEXEC;
109d11110f4Schristos 
110d11110f4Schristos 	return sys_kqueue1(l, &kqa, retval);
111d11110f4Schristos }
112d11110f4Schristos 
113d11110f4Schristos /*
114d11110f4Schristos  * Structure converting function from epoll to kevent.
115d11110f4Schristos  */
116d11110f4Schristos static int
epoll_to_kevent(int epfd,int fd,struct epoll_event * l_event,struct kevent * kevent,int * nkevents)117d11110f4Schristos epoll_to_kevent(int epfd, int fd, struct epoll_event *l_event,
118d11110f4Schristos     struct kevent *kevent, int *nkevents)
119d11110f4Schristos {
120d11110f4Schristos 	uint32_t levents = l_event->events;
121d11110f4Schristos 	uint32_t kev_flags = EV_ADD | EV_ENABLE;
122d11110f4Schristos 
123d11110f4Schristos 	/* flags related to how event is registered */
124d11110f4Schristos 	if ((levents & EPOLLONESHOT) != 0)
125d11110f4Schristos 		kev_flags |= EV_DISPATCH;
126d11110f4Schristos 	if ((levents & EPOLLET) != 0)
127d11110f4Schristos 		kev_flags |= EV_CLEAR;
128d11110f4Schristos 	if ((levents & EPOLLERR) != 0)
129d11110f4Schristos 		kev_flags |= EV_ERROR;
130d11110f4Schristos 	if ((levents & EPOLLRDHUP) != 0)
131d11110f4Schristos 		kev_flags |= EV_EOF;
132d11110f4Schristos 
133d11110f4Schristos 	/* flags related to what event is registered */
134d11110f4Schristos 	if ((levents & EPOLL_EVRD) != 0) {
135d11110f4Schristos 		EV_SET(kevent, fd, EVFILT_READ, kev_flags, 0, 0, 0);
136d11110f4Schristos 		kevent->kext_data = l_event->data;
137d11110f4Schristos 		kevent->kext_epfd = epfd;
138d11110f4Schristos 		kevent->kext_fd = fd;
139d11110f4Schristos 		++kevent;
140d11110f4Schristos 		++(*nkevents);
141d11110f4Schristos 	}
142d11110f4Schristos 	if ((levents & EPOLL_EVWR) != 0) {
143d11110f4Schristos 		EV_SET(kevent, fd, EVFILT_WRITE, kev_flags, 0, 0, 0);
144d11110f4Schristos 		kevent->kext_data = l_event->data;
145d11110f4Schristos 		kevent->kext_epfd = epfd;
146d11110f4Schristos 		kevent->kext_fd = fd;
147d11110f4Schristos 		++kevent;
148d11110f4Schristos 		++(*nkevents);
149d11110f4Schristos 	}
150d11110f4Schristos 	/* zero event mask is legal */
151d11110f4Schristos 	if ((levents & (EPOLL_EVRD | EPOLL_EVWR)) == 0) {
152d11110f4Schristos 		EV_SET(kevent++, fd, EVFILT_READ, EV_ADD|EV_DISABLE, 0, 0, 0);
153d11110f4Schristos 		++(*nkevents);
154d11110f4Schristos 	}
155d11110f4Schristos 
156d11110f4Schristos 	if ((levents & ~(EPOLL_EVSUP)) != 0) {
157d11110f4Schristos 		return EINVAL;
158d11110f4Schristos 	}
159d11110f4Schristos 
160d11110f4Schristos 	return 0;
161d11110f4Schristos }
162d11110f4Schristos 
163d11110f4Schristos /*
164d11110f4Schristos  * Structure converting function from kevent to epoll. In a case
165d11110f4Schristos  * this is called on error in registration we store the error in
166d11110f4Schristos  * event->data and pick it up later in sys_epoll_ctl().
167d11110f4Schristos  */
168d11110f4Schristos static void
kevent_to_epoll(struct kevent * kevent,struct epoll_event * l_event)169d11110f4Schristos kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
170d11110f4Schristos {
171d11110f4Schristos 
172d11110f4Schristos 	l_event->data = kevent->kext_data;
173d11110f4Schristos 
174d11110f4Schristos 	if ((kevent->flags & EV_ERROR) != 0) {
175d11110f4Schristos 		l_event->events = EPOLLERR;
176d11110f4Schristos 		return;
177d11110f4Schristos 	}
178d11110f4Schristos 
179d11110f4Schristos 	/* XXX EPOLLPRI, EPOLLHUP */
180d11110f4Schristos 	switch (kevent->filter) {
181d11110f4Schristos 	case EVFILT_READ:
182d11110f4Schristos 		l_event->events = EPOLLIN;
183d11110f4Schristos 		if ((kevent->flags & EV_EOF) != 0)
184d11110f4Schristos 			l_event->events |= EPOLLRDHUP;
185d11110f4Schristos 		break;
186d11110f4Schristos 	case EVFILT_WRITE:
187d11110f4Schristos 		l_event->events = EPOLLOUT;
188d11110f4Schristos 		break;
189d11110f4Schristos 	default:
190d11110f4Schristos 		DPRINTF(("%s: unhandled kevent filter %d\n", __func__,
191d11110f4Schristos 		    kevent->filter));
192d11110f4Schristos 		break;
193d11110f4Schristos 	}
194d11110f4Schristos }
195d11110f4Schristos 
196d11110f4Schristos /*
197d11110f4Schristos  * Copyout callback used by kevent.  This converts kevent events to
198d11110f4Schristos  * epoll events that are located in args->eventlist.
199d11110f4Schristos  */
200d11110f4Schristos static int
epoll_kev_put_events(void * ctx,struct kevent * events,struct kevent * eventlist,size_t index,int n)201d11110f4Schristos epoll_kev_put_events(void *ctx, struct kevent *events,
202d11110f4Schristos     struct kevent *eventlist, size_t index, int n)
203d11110f4Schristos {
204d11110f4Schristos 	int i;
205d11110f4Schristos 	struct epoll_event *eep = (struct epoll_event *)eventlist;
206d11110f4Schristos 
207d11110f4Schristos 	KASSERT(n >= 0 && n < EPOLL_MAX_EVENTS);
208d11110f4Schristos 
209d11110f4Schristos 	for (i = 0; i < n; i++)
210d11110f4Schristos 		kevent_to_epoll(events + i, eep + index + i);
211d11110f4Schristos 
212d11110f4Schristos 	return 0;
213d11110f4Schristos }
214d11110f4Schristos 
215d11110f4Schristos /*
216d11110f4Schristos  * Copyin callback used by kevent. This copies already
217d11110f4Schristos  * converted filters from kernel memory to the kevent
218d11110f4Schristos  * internal kernel memory. Hence the memcpy instead of
219d11110f4Schristos  * copyin.
220d11110f4Schristos  */
221d11110f4Schristos static int
epoll_kev_fetch_changes(void * ctx,const struct kevent * changelist,struct kevent * changes,size_t index,int n)222d11110f4Schristos epoll_kev_fetch_changes(void *ctx, const struct kevent *changelist,
223d11110f4Schristos     struct kevent *changes, size_t index, int n)
224d11110f4Schristos {
225d11110f4Schristos 	KASSERT(n >= 0 && n < EPOLL_MAX_EVENTS);
226d11110f4Schristos 
227d11110f4Schristos 	memcpy(changes, changelist + index, n * sizeof(*changes));
228d11110f4Schristos 
229d11110f4Schristos 	return 0;
230d11110f4Schristos }
231d11110f4Schristos 
232d11110f4Schristos /*
233d11110f4Schristos  * Timer copy callback used by kevent.  Copies a converted timeout
234d11110f4Schristos  * from kernel memory to kevent memory.  Hence the memcpy instead of
235d11110f4Schristos  * just using copyin.
236d11110f4Schristos  */
237d11110f4Schristos static int
epoll_kev_fetch_timeout(const void * src,void * dest,size_t size)238d11110f4Schristos epoll_kev_fetch_timeout(const void *src, void *dest, size_t size)
239d11110f4Schristos {
240d11110f4Schristos 	memcpy(dest, src, size);
241d11110f4Schristos 
242d11110f4Schristos 	return 0;
243d11110f4Schristos }
244d11110f4Schristos 
245d11110f4Schristos /*
246d11110f4Schristos  * Load epoll filter, convert it to kevent filter and load it into
247d11110f4Schristos  * kevent subsystem.
248d11110f4Schristos  *
249d11110f4Schristos  * event must point to kernel memory or be NULL.
250d11110f4Schristos  */
251d11110f4Schristos int
epoll_ctl_common(struct lwp * l,register_t * retval,int epfd,int op,int fd,struct epoll_event * event)252d11110f4Schristos epoll_ctl_common(struct lwp *l, register_t *retval, int epfd, int op, int fd,
253d11110f4Schristos     struct epoll_event *event)
254d11110f4Schristos {
255d11110f4Schristos 	struct kevent kev[2];
256d11110f4Schristos 	struct kevent_ops k_ops = {
257d11110f4Schristos 		.keo_private = NULL,
258d11110f4Schristos 		.keo_fetch_timeout = NULL,
259d11110f4Schristos 		.keo_fetch_changes = epoll_kev_fetch_changes,
260d11110f4Schristos 		.keo_put_events = NULL,
261d11110f4Schristos 	};
262d11110f4Schristos 	file_t *epfp, *fp;
263d11110f4Schristos 	int error = 0;
264d11110f4Schristos 	int nchanges = 0;
265d11110f4Schristos 
266d11110f4Schristos 	/*
267d11110f4Schristos 	 * Need to validate epfd and fd separately from kevent1 to match
268d11110f4Schristos 	 * Linux's errno behaviour.
269d11110f4Schristos 	 */
270d11110f4Schristos 	epfp = fd_getfile(epfd);
271d11110f4Schristos 	if (epfp == NULL)
272d11110f4Schristos 		return EBADF;
273d11110f4Schristos 	if (epfp->f_type != DTYPE_KQUEUE)
274d11110f4Schristos 		error = EINVAL;
275d11110f4Schristos 	fd_putfile(epfd);
276d11110f4Schristos 	if (error != 0)
277d11110f4Schristos 		return error;
278d11110f4Schristos 
279d11110f4Schristos 	fp = fd_getfile(fd);
280d11110f4Schristos 	if (fp == NULL)
281d11110f4Schristos 		return EBADF;
282d11110f4Schristos 	if (fp->f_type == DTYPE_VNODE) {
283d11110f4Schristos 		switch (fp->f_vnode->v_type) {
284d11110f4Schristos 		case VREG:
285d11110f4Schristos 		case VDIR:
286d11110f4Schristos 		case VBLK:
287d11110f4Schristos 		case VLNK:
288d11110f4Schristos 			error = EPERM;
289d11110f4Schristos 			break;
290d11110f4Schristos 
291d11110f4Schristos 		default:
292d11110f4Schristos 			break;
293d11110f4Schristos 		}
294d11110f4Schristos 	}
295d11110f4Schristos 	fd_putfile(fd);
296d11110f4Schristos 	if (error != 0)
297d11110f4Schristos 		return error;
298d11110f4Schristos 
299d11110f4Schristos 	/* Linux disallows spying on himself */
300d11110f4Schristos 	if (epfd == fd) {
301d11110f4Schristos 		return EINVAL;
302d11110f4Schristos 	}
303d11110f4Schristos 
304d11110f4Schristos 	if (op != EPOLL_CTL_DEL) {
305d11110f4Schristos 		error = epoll_to_kevent(epfd, fd, event, kev, &nchanges);
306d11110f4Schristos 		if (error != 0)
307d11110f4Schristos 			return error;
308d11110f4Schristos 	}
309d11110f4Schristos 
310d11110f4Schristos 	switch (op) {
311d11110f4Schristos 	case EPOLL_CTL_MOD:
312d11110f4Schristos 		error = epoll_delete_all_events(retval, epfd, fd);
313d11110f4Schristos 		if (error != 0)
314d11110f4Schristos 			return error;
315d11110f4Schristos 		break;
316d11110f4Schristos 
317d11110f4Schristos 	case EPOLL_CTL_ADD:
318d11110f4Schristos 		if (epoll_fd_registered(retval, epfd, fd))
319d11110f4Schristos 			return EEXIST;
320d11110f4Schristos 		error = epoll_check_loop_and_depth(l, epfd, fd);
321d11110f4Schristos 		if (error != 0)
322d11110f4Schristos 			return error;
323d11110f4Schristos 		break;
324d11110f4Schristos 
325d11110f4Schristos 	case EPOLL_CTL_DEL:
326d11110f4Schristos 		/* CTL_DEL means unregister this fd with this epoll */
327d11110f4Schristos 		return epoll_delete_all_events(retval, epfd, fd);
328d11110f4Schristos 
329d11110f4Schristos 	default:
3302e3b4288Spgoyette 		DPRINTF(("%s: invalid op %d\n", __func__, op));
331d11110f4Schristos 		return EINVAL;
332d11110f4Schristos 	}
333d11110f4Schristos 
334d11110f4Schristos 	error = kevent1(retval, epfd, kev, nchanges, NULL, 0, NULL, &k_ops);
335d11110f4Schristos 
336d11110f4Schristos 	if (error == EOPNOTSUPP) {
337d11110f4Schristos 		error = EPERM;
338d11110f4Schristos 	}
339d11110f4Schristos 
340d11110f4Schristos 	return error;
341d11110f4Schristos }
342d11110f4Schristos 
343d11110f4Schristos /*
344d11110f4Schristos  * epoll_ctl(2).  Copyin event if necessary and then call
345d11110f4Schristos  * epoll_ctl_common().
346d11110f4Schristos  */
347d11110f4Schristos int
sys_epoll_ctl(struct lwp * l,const struct sys_epoll_ctl_args * uap,register_t * retval)348d11110f4Schristos sys_epoll_ctl(struct lwp *l, const struct sys_epoll_ctl_args *uap,
349d11110f4Schristos     register_t *retval)
350d11110f4Schristos {
351d11110f4Schristos 	/* {
352d11110f4Schristos 		syscallarg(int) epfd;
353d11110f4Schristos 		syscallarg(int) op;
354d11110f4Schristos 		syscallarg(int) fd;
355d11110f4Schristos 		syscallarg(struct epoll_event *) event;
356d11110f4Schristos 	} */
357d11110f4Schristos 	struct epoll_event ee;
358d11110f4Schristos 	struct epoll_event *eep;
359d11110f4Schristos 	int error;
360d11110f4Schristos 
361d11110f4Schristos 	if (SCARG(uap, op) != EPOLL_CTL_DEL) {
362d11110f4Schristos 		error = copyin(SCARG(uap, event), &ee, sizeof(ee));
363d11110f4Schristos 		if (error != 0)
364d11110f4Schristos 			return error;
365d11110f4Schristos 
366d11110f4Schristos 		eep = &ee;
367d11110f4Schristos 	} else
368d11110f4Schristos 		eep = NULL;
369d11110f4Schristos 
370d11110f4Schristos 	return epoll_ctl_common(l, retval, SCARG(uap, epfd), SCARG(uap, op),
371d11110f4Schristos 	    SCARG(uap, fd), eep);
372d11110f4Schristos }
373d11110f4Schristos 
374d11110f4Schristos /*
375d11110f4Schristos  * Wait for a filter to be triggered on the epoll file descriptor.
376d11110f4Schristos  * All of the epoll_*wait* syscalls eventually end up here.
377d11110f4Schristos  *
378d11110f4Schristos  * events, nss, and ssp must point to kernel memory (or be NULL).
379d11110f4Schristos  */
380d11110f4Schristos int
epoll_wait_common(struct lwp * l,register_t * retval,int epfd,struct epoll_event * events,int maxevents,struct timespec * tsp,const sigset_t * nssp)381d11110f4Schristos epoll_wait_common(struct lwp *l, register_t *retval, int epfd,
382d11110f4Schristos     struct epoll_event *events, int maxevents, struct timespec *tsp,
383d11110f4Schristos     const sigset_t *nssp)
384d11110f4Schristos {
385d11110f4Schristos 	struct kevent_ops k_ops = {
386d11110f4Schristos 	        .keo_private = NULL,
387d11110f4Schristos 		.keo_fetch_timeout = epoll_kev_fetch_timeout,
388d11110f4Schristos 		.keo_fetch_changes = NULL,
389d11110f4Schristos 		.keo_put_events = epoll_kev_put_events,
390d11110f4Schristos 	};
391d11110f4Schristos 	struct proc *p = l->l_proc;
392d11110f4Schristos 	file_t *epfp;
393d11110f4Schristos 	sigset_t oss;
394d11110f4Schristos 	int error = 0;
395d11110f4Schristos 
396d11110f4Schristos 	if (maxevents <= 0 || maxevents > EPOLL_MAX_EVENTS)
397d11110f4Schristos 		return EINVAL;
398d11110f4Schristos 
399d11110f4Schristos 	/*
400d11110f4Schristos 	 * Need to validate epfd separately from kevent1 to match
401d11110f4Schristos 	 * Linux's errno behaviour.
402d11110f4Schristos 	 */
403d11110f4Schristos 	epfp = fd_getfile(epfd);
404d11110f4Schristos 	if (epfp == NULL)
405d11110f4Schristos 		return EBADF;
406d11110f4Schristos 	if (epfp->f_type != DTYPE_KQUEUE)
407d11110f4Schristos 		error = EINVAL;
408d11110f4Schristos 	fd_putfile(epfd);
409d11110f4Schristos 	if (error != 0)
410d11110f4Schristos 		return error;
411d11110f4Schristos 
412d11110f4Schristos 	if (nssp != NULL) {
413d11110f4Schristos 		mutex_enter(p->p_lock);
414d11110f4Schristos 		error = sigprocmask1(l, SIG_SETMASK, nssp, &oss);
415d11110f4Schristos 		mutex_exit(p->p_lock);
416d11110f4Schristos 		if (error != 0)
417d11110f4Schristos 			return error;
418d11110f4Schristos 	}
419d11110f4Schristos 
420d11110f4Schristos 	error = kevent1(retval, epfd, NULL, 0, (struct kevent *)events,
421d11110f4Schristos 	    maxevents, tsp, &k_ops);
422d11110f4Schristos 	/*
423d11110f4Schristos 	 * Since we're not registering nay events, ENOMEM should not
424d11110f4Schristos 	 * be possible for this specific kevent1 call.
425d11110f4Schristos 	 */
426d11110f4Schristos 	KASSERT(error != ENOMEM);
427d11110f4Schristos 
428d11110f4Schristos 	if (nssp != NULL) {
429d11110f4Schristos 	        mutex_enter(p->p_lock);
430d11110f4Schristos 		error = sigprocmask1(l, SIG_SETMASK, &oss, NULL);
431d11110f4Schristos 		mutex_exit(p->p_lock);
432d11110f4Schristos 	}
433d11110f4Schristos 
434d11110f4Schristos 	return error;
435d11110f4Schristos }
436d11110f4Schristos 
437d11110f4Schristos /*
438d11110f4Schristos  * epoll_pwait2(2).
439d11110f4Schristos  */
440d11110f4Schristos int
sys_epoll_pwait2(struct lwp * l,const struct sys_epoll_pwait2_args * uap,register_t * retval)441d11110f4Schristos sys_epoll_pwait2(struct lwp *l, const struct sys_epoll_pwait2_args *uap,
442d11110f4Schristos     register_t *retval)
443d11110f4Schristos {
444d11110f4Schristos 	/* {
445d11110f4Schristos 		syscallarg(int) epfd;
446d11110f4Schristos 		syscallarg(struct epoll_event *) events;
447d11110f4Schristos 		syscallarg(int) maxevents;
448d11110f4Schristos 		syscallarg(struct timespec *) timeout;
449d11110f4Schristos 		syscallarg(sigset_t *) sigmask;
450d11110f4Schristos 	} */
451d11110f4Schristos 	struct epoll_event *events;
452d11110f4Schristos 	struct timespec ts, *tsp;
453d11110f4Schristos 	sigset_t ss, *ssp;
454d11110f4Schristos 	int error;
455d11110f4Schristos 	const int maxevents = SCARG(uap, maxevents);
456d11110f4Schristos 
457d11110f4Schristos 	if (maxevents <= 0 || maxevents >= EPOLL_MAX_EVENTS)
458d11110f4Schristos 		return EINVAL;
459d11110f4Schristos 
460d11110f4Schristos 	if (SCARG(uap, timeout) != NULL) {
461d11110f4Schristos 		error = copyin(SCARG(uap, timeout), &ts, sizeof(ts));
462d11110f4Schristos 		if (error != 0)
463d11110f4Schristos 			return error;
464d11110f4Schristos 
465d11110f4Schristos 		tsp = &ts;
466d11110f4Schristos 	} else
467d11110f4Schristos 		tsp = NULL;
468d11110f4Schristos 
469d11110f4Schristos 	if (SCARG(uap, sigmask) != NULL) {
470d11110f4Schristos 		error = copyin(SCARG(uap, sigmask), &ss, sizeof(ss));
471d11110f4Schristos 		if (error != 0)
472d11110f4Schristos 			return error;
473d11110f4Schristos 
474d11110f4Schristos 		ssp = &ss;
475d11110f4Schristos 	} else
476d11110f4Schristos 		ssp = NULL;
477d11110f4Schristos 
478d11110f4Schristos 	events = kmem_alloc(maxevents * sizeof(*events), KM_SLEEP);
479d11110f4Schristos 
480d11110f4Schristos 	error = epoll_wait_common(l, retval, SCARG(uap, epfd), events,
481d11110f4Schristos 	    maxevents, tsp, ssp);
482d11110f4Schristos 	if (error == 0)
483d11110f4Schristos 		error = copyout(events, SCARG(uap, events),
484d11110f4Schristos 		    *retval * sizeof(*events));
485d11110f4Schristos 
486d11110f4Schristos 	kmem_free(events, maxevents * sizeof(*events));
487d11110f4Schristos 	return error;
488d11110f4Schristos }
489d11110f4Schristos 
490d11110f4Schristos /*
491d11110f4Schristos  * Helper that registers a single kevent.
492d11110f4Schristos  */
493d11110f4Schristos static int
epoll_register_kevent(register_t * retval,int epfd,int fd,int filter,unsigned int flags)494d11110f4Schristos epoll_register_kevent(register_t *retval, int epfd, int fd, int filter,
495d11110f4Schristos     unsigned int flags)
496d11110f4Schristos {
497d11110f4Schristos 	struct kevent kev;
498d11110f4Schristos 	struct kevent_ops k_ops = {
499d11110f4Schristos 		.keo_private = NULL,
500d11110f4Schristos 		.keo_fetch_timeout = NULL,
501d11110f4Schristos 		.keo_fetch_changes = epoll_kev_fetch_changes,
502d11110f4Schristos 		.keo_put_events = NULL,
503d11110f4Schristos 	};
504d11110f4Schristos 
505d11110f4Schristos 	EV_SET(&kev, fd, filter, flags, 0, 0, 0);
506d11110f4Schristos 
507d11110f4Schristos 	return kevent1(retval, epfd, &kev, 1, NULL, 0, NULL, &k_ops);
508d11110f4Schristos }
509d11110f4Schristos 
510d11110f4Schristos /*
511d11110f4Schristos  * Check if an fd is already registered in the kqueue referenced by epfd.
512d11110f4Schristos  */
513d11110f4Schristos static int
epoll_fd_registered(register_t * retval,int epfd,int fd)514d11110f4Schristos epoll_fd_registered(register_t *retval, int epfd, int fd)
515d11110f4Schristos {
516d11110f4Schristos 	/*
517d11110f4Schristos 	 * Set empty filter flags to avoid accidental modification of already
518d11110f4Schristos 	 * registered events. In the case of event re-registration:
519d11110f4Schristos 	 * 1. If event does not exists kevent() does nothing and returns ENOENT
520d11110f4Schristos 	 * 2. If event does exists, it's enabled/disabled state is preserved
521d11110f4Schristos 	 *    but fflags, data and udata fields are overwritten. So we can not
522d11110f4Schristos 	 *    set socket lowats and store user's context pointer in udata.
523d11110f4Schristos 	 */
524d11110f4Schristos 	if (epoll_register_kevent(retval, epfd, fd, EVFILT_READ, 0) != ENOENT ||
525d11110f4Schristos 	    epoll_register_kevent(retval, epfd, fd, EVFILT_WRITE, 0) != ENOENT)
526d11110f4Schristos 		return 1;
527d11110f4Schristos 
528d11110f4Schristos 	return 0;
529d11110f4Schristos }
530d11110f4Schristos 
531d11110f4Schristos /*
532d11110f4Schristos  * Remove all events in the kqueue referenced by epfd that depend on
533d11110f4Schristos  * fd.
534d11110f4Schristos  */
535d11110f4Schristos static int
epoll_delete_all_events(register_t * retval,int epfd,int fd)536d11110f4Schristos epoll_delete_all_events(register_t *retval, int epfd, int fd)
537d11110f4Schristos {
538d11110f4Schristos 	int error1, error2;
539d11110f4Schristos 
540d11110f4Schristos 	error1 = epoll_register_kevent(retval, epfd, fd, EVFILT_READ,
541d11110f4Schristos 	    EV_DELETE);
542d11110f4Schristos 	error2 = epoll_register_kevent(retval, epfd, fd, EVFILT_WRITE,
543d11110f4Schristos 	    EV_DELETE);
544d11110f4Schristos 
545d11110f4Schristos 	/* return 0 if at least one result positive */
546d11110f4Schristos 	return error1 == 0 ? 0 : error2;
547d11110f4Schristos }
548d11110f4Schristos 
549d11110f4Schristos /*
550d11110f4Schristos  * Interate through all the knotes and recover a directed graph on
551d11110f4Schristos  * which kqueues are watching each other.
552d11110f4Schristos  *
553d11110f4Schristos  * If edges is NULL, the number of edges is still counted but no graph
554d11110f4Schristos  * is assembled.
555d11110f4Schristos  */
556d11110f4Schristos static int
epoll_recover_watch_tree(struct epoll_edge * edges,size_t nedges,size_t nfds)557d11110f4Schristos epoll_recover_watch_tree(struct epoll_edge *edges, size_t nedges, size_t nfds) {
558d11110f4Schristos 	file_t *currfp, *targetfp;
559d11110f4Schristos 	struct knote *kn, *tmpkn;
560d11110f4Schristos 	size_t i, nedges_so_far = 0;
561d11110f4Schristos 
562d11110f4Schristos 	for (i = 0; i < nfds && (edges == NULL || nedges_so_far < nedges); i++)
563d11110f4Schristos 	{
564d11110f4Schristos 		currfp = fd_getfile(i);
565d11110f4Schristos 		if (currfp == NULL)
566d11110f4Schristos 			continue;
567d11110f4Schristos 		if (currfp->f_type != DTYPE_KQUEUE)
568d11110f4Schristos 			goto continue_count_outer;
569d11110f4Schristos 
570d11110f4Schristos 		SLIST_FOREACH_SAFE(kn, &currfp->f_kqueue->kq_sel.sel_klist,
571d11110f4Schristos 		    kn_selnext, tmpkn) {
572d11110f4Schristos 			targetfp = fd_getfile(kn->kn_kevent.kext_epfd);
573d11110f4Schristos 			if (targetfp == NULL)
574d11110f4Schristos 				continue;
575d11110f4Schristos 			if (targetfp->f_type == DTYPE_KQUEUE) {
576d11110f4Schristos 				if (edges != NULL) {
577d11110f4Schristos 					edges[nedges_so_far].epfd =
578d11110f4Schristos 					    kn->kn_kevent.kext_epfd;
579d11110f4Schristos 					edges[nedges_so_far].fd =
580d11110f4Schristos 					    kn->kn_kevent.kext_fd;
581d11110f4Schristos 				}
582d11110f4Schristos 				nedges_so_far++;
583d11110f4Schristos 			}
584d11110f4Schristos 
585d11110f4Schristos 			fd_putfile(kn->kn_kevent.kext_epfd);
586d11110f4Schristos 		}
587d11110f4Schristos 
588d11110f4Schristos continue_count_outer:
589d11110f4Schristos 		fd_putfile(i);
590d11110f4Schristos 	}
591d11110f4Schristos 
592d11110f4Schristos 	return nedges_so_far;
593d11110f4Schristos }
594d11110f4Schristos 
595d11110f4Schristos /*
596d11110f4Schristos  * Run dfs on the graph described by edges, checking for loops and a
597d11110f4Schristos  * depth greater than EPOLL_MAX_DEPTH.
598d11110f4Schristos  */
599d11110f4Schristos static int
epoll_dfs(struct epoll_edge * edges,size_t nedges,struct epoll_seen * seen,size_t nseen,int currfd,int depth)600d11110f4Schristos epoll_dfs(struct epoll_edge *edges, size_t nedges, struct epoll_seen *seen,
601d11110f4Schristos     size_t nseen, int currfd, int depth)
602d11110f4Schristos {
603d11110f4Schristos 	int error;
604d11110f4Schristos 	size_t i;
605d11110f4Schristos 
606d11110f4Schristos 	KASSERT(edges != NULL);
607d11110f4Schristos 	KASSERT(seen != NULL);
608d11110f4Schristos 	KASSERT(nedges > 0);
609d11110f4Schristos 	KASSERT(currfd < nseen);
610d11110f4Schristos 	KASSERT(0 <= depth && depth <= EPOLL_MAX_DEPTH + 1);
611d11110f4Schristos 
612d11110f4Schristos 	if (__BITMAP_ISSET(currfd, seen))
613d11110f4Schristos 		return ELOOP;
614d11110f4Schristos 
615d11110f4Schristos 	__BITMAP_SET(currfd, seen);
616d11110f4Schristos 
617d11110f4Schristos 	depth++;
618d11110f4Schristos 	if (depth > EPOLL_MAX_DEPTH)
619d11110f4Schristos 		return EINVAL;
620d11110f4Schristos 
621d11110f4Schristos 	for (i = 0; i < nedges; i++) {
622d11110f4Schristos 		if (edges[i].epfd != currfd)
623d11110f4Schristos 			continue;
624d11110f4Schristos 
625d11110f4Schristos 		error = epoll_dfs(edges, nedges, seen, nseen,
626d11110f4Schristos 		    edges[i].fd, depth);
627d11110f4Schristos 		if (error != 0)
628d11110f4Schristos 			return error;
629d11110f4Schristos 	}
630d11110f4Schristos 
631d11110f4Schristos 	return 0;
632d11110f4Schristos }
633d11110f4Schristos 
634d11110f4Schristos /*
635d11110f4Schristos  * Check if adding fd to epfd would violate the maximum depth or
636d11110f4Schristos  * create a loop.
637d11110f4Schristos  */
638d11110f4Schristos static int
epoll_check_loop_and_depth(struct lwp * l,int epfd,int fd)639d11110f4Schristos epoll_check_loop_and_depth(struct lwp *l, int epfd, int fd)
640d11110f4Schristos {
641d11110f4Schristos 	int error;
642d11110f4Schristos 	file_t *fp;
643d11110f4Schristos 	struct epoll_edge *edges;
644d11110f4Schristos 	struct epoll_seen *seen;
645d11110f4Schristos 	size_t nedges, nfds, seen_size;
646d11110f4Schristos 	bool fdirrelevant;
647d11110f4Schristos 
648d11110f4Schristos 	/* If the target isn't another kqueue, we can skip this check */
649d11110f4Schristos 	fp = fd_getfile(fd);
650d11110f4Schristos 	if (fp == NULL)
651d11110f4Schristos 		return 0;
652d11110f4Schristos 	fdirrelevant = fp->f_type != DTYPE_KQUEUE;
653d11110f4Schristos 	fd_putfile(fd);
654d11110f4Schristos 	if (fdirrelevant)
655d11110f4Schristos 		return 0;
656d11110f4Schristos 
657d11110f4Schristos 	nfds = l->l_proc->p_fd->fd_lastfile + 1;
658d11110f4Schristos 
659d11110f4Schristos 	/*
660d11110f4Schristos 	 * We call epoll_recover_watch_tree twice, once to find the
661d11110f4Schristos 	 * number of edges, and once to actually fill them in.  We add one
662d11110f4Schristos 	 * because we want to include the edge epfd->fd.
663d11110f4Schristos 	 */
664d11110f4Schristos 	nedges = 1 + epoll_recover_watch_tree(NULL, 0, nfds);
665d11110f4Schristos 
666d11110f4Schristos 	edges = kmem_zalloc(nedges * sizeof(*edges), KM_SLEEP);
667d11110f4Schristos 
668d11110f4Schristos 	epoll_recover_watch_tree(edges + 1, nedges - 1, nfds);
669d11110f4Schristos 
670d11110f4Schristos 	edges[0].epfd = epfd;
671d11110f4Schristos 	edges[0].fd = fd;
672d11110f4Schristos 
673d11110f4Schristos 	seen_size = __BITMAP_SIZE(char, nfds);
674d11110f4Schristos 	seen = kmem_zalloc(seen_size, KM_SLEEP);
675d11110f4Schristos 
676d11110f4Schristos 	error = epoll_dfs(edges, nedges, seen, nfds, epfd, 0);
677d11110f4Schristos 
678d11110f4Schristos 	kmem_free(seen, seen_size);
679d11110f4Schristos 	kmem_free(edges, nedges * sizeof(*edges));
680d11110f4Schristos 
681d11110f4Schristos 	return error;
682d11110f4Schristos }
683