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