Home | History | Annotate | Line # | Download | only in kern
kern_event.c revision 1.150
      1  1.150   msaitoh /*	$NetBSD: kern_event.c,v 1.150 2023/09/21 09:31:50 msaitoh Exp $	*/
      2   1.49        ad 
      3   1.49        ad /*-
      4  1.129   thorpej  * Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc.
      5   1.49        ad  * All rights reserved.
      6   1.49        ad  *
      7   1.64        ad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.64        ad  * by Andrew Doran.
      9   1.64        ad  *
     10   1.49        ad  * Redistribution and use in source and binary forms, with or without
     11   1.49        ad  * modification, are permitted provided that the following conditions
     12   1.49        ad  * are met:
     13   1.49        ad  * 1. Redistributions of source code must retain the above copyright
     14   1.49        ad  *    notice, this list of conditions and the following disclaimer.
     15   1.49        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.49        ad  *    notice, this list of conditions and the following disclaimer in the
     17   1.49        ad  *    documentation and/or other materials provided with the distribution.
     18   1.49        ad  *
     19   1.49        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.49        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.49        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.49        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.49        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.49        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.49        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.49        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.49        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.49        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.49        ad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.49        ad  */
     31   1.28    kardel 
     32    1.1     lukem /*-
     33    1.1     lukem  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon (at) FreeBSD.org>
     34  1.108  christos  * Copyright (c) 2009 Apple, Inc
     35    1.1     lukem  * All rights reserved.
     36    1.1     lukem  *
     37    1.1     lukem  * Redistribution and use in source and binary forms, with or without
     38    1.1     lukem  * modification, are permitted provided that the following conditions
     39    1.1     lukem  * are met:
     40    1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     41    1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     42    1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     43    1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     44    1.1     lukem  *    documentation and/or other materials provided with the distribution.
     45    1.1     lukem  *
     46    1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47    1.1     lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48    1.1     lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49    1.1     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50    1.1     lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51    1.1     lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52    1.1     lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53    1.1     lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54    1.1     lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55    1.1     lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56    1.1     lukem  * SUCH DAMAGE.
     57    1.1     lukem  *
     58   1.49        ad  * FreeBSD: src/sys/kern/kern_event.c,v 1.27 2001/07/05 17:10:44 rwatson Exp
     59    1.1     lukem  */
     60   1.14  jdolecek 
     61  1.130   thorpej #ifdef _KERNEL_OPT
     62  1.129   thorpej #include "opt_ddb.h"
     63  1.130   thorpej #endif /* _KERNEL_OPT */
     64  1.129   thorpej 
     65   1.14  jdolecek #include <sys/cdefs.h>
     66  1.150   msaitoh __KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.150 2023/09/21 09:31:50 msaitoh Exp $");
     67    1.1     lukem 
     68    1.1     lukem #include <sys/param.h>
     69    1.1     lukem #include <sys/systm.h>
     70    1.1     lukem #include <sys/kernel.h>
     71   1.86  christos #include <sys/wait.h>
     72    1.1     lukem #include <sys/proc.h>
     73    1.1     lukem #include <sys/file.h>
     74    1.3  jdolecek #include <sys/select.h>
     75    1.1     lukem #include <sys/queue.h>
     76    1.1     lukem #include <sys/event.h>
     77    1.1     lukem #include <sys/eventvar.h>
     78    1.1     lukem #include <sys/poll.h>
     79   1.49        ad #include <sys/kmem.h>
     80    1.1     lukem #include <sys/stat.h>
     81    1.3  jdolecek #include <sys/filedesc.h>
     82    1.3  jdolecek #include <sys/syscallargs.h>
     83   1.27      elad #include <sys/kauth.h>
     84   1.40        ad #include <sys/conf.h>
     85   1.49        ad #include <sys/atomic.h>
     86    1.1     lukem 
     87   1.49        ad static int	kqueue_scan(file_t *, size_t, struct kevent *,
     88   1.49        ad 			    const struct timespec *, register_t *,
     89   1.49        ad 			    const struct kevent_ops *, struct kevent *,
     90   1.49        ad 			    size_t);
     91   1.49        ad static int	kqueue_ioctl(file_t *, u_long, void *);
     92   1.49        ad static int	kqueue_fcntl(file_t *, u_int, void *);
     93   1.49        ad static int	kqueue_poll(file_t *, int);
     94   1.49        ad static int	kqueue_kqfilter(file_t *, struct knote *);
     95   1.49        ad static int	kqueue_stat(file_t *, struct stat *);
     96   1.49        ad static int	kqueue_close(file_t *);
     97  1.118  jdolecek static void	kqueue_restart(file_t *);
     98  1.148  riastrad static int	kqueue_fpathconf(file_t *, int, register_t *);
     99   1.49        ad static int	kqueue_register(struct kqueue *, struct kevent *);
    100   1.49        ad static void	kqueue_doclose(struct kqueue *, struct klist *, int);
    101   1.49        ad 
    102   1.49        ad static void	knote_detach(struct knote *, filedesc_t *fdp, bool);
    103   1.49        ad static void	knote_enqueue(struct knote *);
    104   1.49        ad static void	knote_activate(struct knote *);
    105  1.133   thorpej static void	knote_activate_locked(struct knote *);
    106  1.136   thorpej static void	knote_deactivate_locked(struct knote *);
    107   1.49        ad 
    108   1.49        ad static void	filt_kqdetach(struct knote *);
    109   1.49        ad static int	filt_kqueue(struct knote *, long hint);
    110   1.49        ad static int	filt_procattach(struct knote *);
    111   1.49        ad static void	filt_procdetach(struct knote *);
    112   1.49        ad static int	filt_proc(struct knote *, long hint);
    113   1.49        ad static int	filt_fileattach(struct knote *);
    114   1.49        ad static void	filt_timerexpire(void *x);
    115   1.49        ad static int	filt_timerattach(struct knote *);
    116   1.49        ad static void	filt_timerdetach(struct knote *);
    117   1.49        ad static int	filt_timer(struct knote *, long hint);
    118  1.136   thorpej static int	filt_timertouch(struct knote *, struct kevent *, long type);
    119  1.108  christos static int	filt_userattach(struct knote *);
    120  1.108  christos static void	filt_userdetach(struct knote *);
    121  1.108  christos static int	filt_user(struct knote *, long hint);
    122  1.135   thorpej static int	filt_usertouch(struct knote *, struct kevent *, long type);
    123    1.1     lukem 
    124  1.144   thorpej /*
    125  1.144   thorpej  * Private knote state that should never be exposed outside
    126  1.144   thorpej  * of kern_event.c
    127  1.144   thorpej  *
    128  1.144   thorpej  * Field locking:
    129  1.144   thorpej  *
    130  1.144   thorpej  * q	kn_kq->kq_lock
    131  1.144   thorpej  */
    132  1.144   thorpej struct knote_impl {
    133  1.144   thorpej 	struct knote	ki_knote;
    134  1.144   thorpej 	unsigned int	ki_influx;	/* q: in-flux counter */
    135  1.145   thorpej 	kmutex_t	ki_foplock;	/* for kn_filterops */
    136  1.144   thorpej };
    137  1.144   thorpej 
    138  1.144   thorpej #define	KIMPL_TO_KNOTE(kip)	(&(kip)->ki_knote)
    139  1.144   thorpej #define	KNOTE_TO_KIMPL(knp)	container_of((knp), struct knote_impl, ki_knote)
    140  1.144   thorpej 
    141  1.144   thorpej static inline struct knote *
    142  1.144   thorpej knote_alloc(bool sleepok)
    143  1.144   thorpej {
    144  1.144   thorpej 	struct knote_impl *ki;
    145  1.144   thorpej 
    146  1.144   thorpej 	ki = kmem_zalloc(sizeof(*ki), sleepok ? KM_SLEEP : KM_NOSLEEP);
    147  1.145   thorpej 	mutex_init(&ki->ki_foplock, MUTEX_DEFAULT, IPL_NONE);
    148  1.144   thorpej 
    149  1.144   thorpej 	return KIMPL_TO_KNOTE(ki);
    150  1.144   thorpej }
    151  1.144   thorpej 
    152  1.144   thorpej static inline void
    153  1.144   thorpej knote_free(struct knote *kn)
    154  1.144   thorpej {
    155  1.144   thorpej 	struct knote_impl *ki = KNOTE_TO_KIMPL(kn);
    156  1.144   thorpej 
    157  1.145   thorpej 	mutex_destroy(&ki->ki_foplock);
    158  1.144   thorpej 	kmem_free(ki, sizeof(*ki));
    159  1.144   thorpej }
    160  1.144   thorpej 
    161  1.145   thorpej static inline void
    162  1.145   thorpej knote_foplock_enter(struct knote *kn)
    163  1.145   thorpej {
    164  1.145   thorpej 	mutex_enter(&KNOTE_TO_KIMPL(kn)->ki_foplock);
    165  1.145   thorpej }
    166  1.145   thorpej 
    167  1.145   thorpej static inline void
    168  1.145   thorpej knote_foplock_exit(struct knote *kn)
    169  1.145   thorpej {
    170  1.145   thorpej 	mutex_exit(&KNOTE_TO_KIMPL(kn)->ki_foplock);
    171  1.145   thorpej }
    172  1.145   thorpej 
    173  1.146  riastrad static inline bool __diagused
    174  1.145   thorpej knote_foplock_owned(struct knote *kn)
    175  1.145   thorpej {
    176  1.145   thorpej 	return mutex_owned(&KNOTE_TO_KIMPL(kn)->ki_foplock);
    177  1.145   thorpej }
    178  1.145   thorpej 
    179   1.21  christos static const struct fileops kqueueops = {
    180  1.101  christos 	.fo_name = "kqueue",
    181   1.64        ad 	.fo_read = (void *)enxio,
    182   1.64        ad 	.fo_write = (void *)enxio,
    183   1.64        ad 	.fo_ioctl = kqueue_ioctl,
    184   1.64        ad 	.fo_fcntl = kqueue_fcntl,
    185   1.64        ad 	.fo_poll = kqueue_poll,
    186   1.64        ad 	.fo_stat = kqueue_stat,
    187   1.64        ad 	.fo_close = kqueue_close,
    188   1.64        ad 	.fo_kqfilter = kqueue_kqfilter,
    189  1.118  jdolecek 	.fo_restart = kqueue_restart,
    190  1.148  riastrad 	.fo_fpathconf = kqueue_fpathconf,
    191    1.1     lukem };
    192    1.1     lukem 
    193  1.145   thorpej static void
    194  1.145   thorpej filt_nopdetach(struct knote *kn __unused)
    195  1.145   thorpej {
    196  1.145   thorpej }
    197  1.145   thorpej 
    198  1.145   thorpej static int
    199  1.145   thorpej filt_nopevent(struct knote *kn __unused, long hint __unused)
    200  1.145   thorpej {
    201  1.145   thorpej 	return 0;
    202  1.145   thorpej }
    203  1.145   thorpej 
    204  1.145   thorpej static const struct filterops nop_fd_filtops = {
    205  1.145   thorpej 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
    206  1.145   thorpej 	.f_attach = NULL,
    207  1.145   thorpej 	.f_detach = filt_nopdetach,
    208  1.145   thorpej 	.f_event = filt_nopevent,
    209  1.145   thorpej };
    210  1.145   thorpej 
    211  1.145   thorpej static const struct filterops nop_filtops = {
    212  1.145   thorpej 	.f_flags = FILTEROP_MPSAFE,
    213  1.145   thorpej 	.f_attach = NULL,
    214  1.145   thorpej 	.f_detach = filt_nopdetach,
    215  1.145   thorpej 	.f_event = filt_nopevent,
    216  1.145   thorpej };
    217  1.145   thorpej 
    218   1.96      maya static const struct filterops kqread_filtops = {
    219  1.123   thorpej 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
    220   1.96      maya 	.f_attach = NULL,
    221   1.96      maya 	.f_detach = filt_kqdetach,
    222   1.96      maya 	.f_event = filt_kqueue,
    223   1.96      maya };
    224   1.96      maya 
    225   1.96      maya static const struct filterops proc_filtops = {
    226  1.129   thorpej 	.f_flags = FILTEROP_MPSAFE,
    227   1.96      maya 	.f_attach = filt_procattach,
    228   1.96      maya 	.f_detach = filt_procdetach,
    229   1.96      maya 	.f_event = filt_proc,
    230   1.96      maya };
    231   1.96      maya 
    232  1.122   thorpej /*
    233  1.122   thorpej  * file_filtops is not marked MPSAFE because it's going to call
    234  1.122   thorpej  * fileops::fo_kqfilter(), which might not be.  That function,
    235  1.122   thorpej  * however, will override the knote's filterops, and thus will
    236  1.122   thorpej  * inherit the MPSAFE-ness of the back-end at that time.
    237  1.122   thorpej  */
    238   1.96      maya static const struct filterops file_filtops = {
    239  1.121   thorpej 	.f_flags = FILTEROP_ISFD,
    240   1.96      maya 	.f_attach = filt_fileattach,
    241   1.96      maya 	.f_detach = NULL,
    242   1.96      maya 	.f_event = NULL,
    243   1.96      maya };
    244   1.96      maya 
    245   1.96      maya static const struct filterops timer_filtops = {
    246  1.125   thorpej 	.f_flags = FILTEROP_MPSAFE,
    247   1.96      maya 	.f_attach = filt_timerattach,
    248   1.96      maya 	.f_detach = filt_timerdetach,
    249   1.96      maya 	.f_event = filt_timer,
    250  1.136   thorpej 	.f_touch = filt_timertouch,
    251   1.96      maya };
    252    1.1     lukem 
    253  1.108  christos static const struct filterops user_filtops = {
    254  1.123   thorpej 	.f_flags = FILTEROP_MPSAFE,
    255  1.108  christos 	.f_attach = filt_userattach,
    256  1.108  christos 	.f_detach = filt_userdetach,
    257  1.108  christos 	.f_event = filt_user,
    258  1.108  christos 	.f_touch = filt_usertouch,
    259  1.108  christos };
    260  1.108  christos 
    261   1.49        ad static u_int	kq_ncallouts = 0;
    262    1.8  jdolecek static int	kq_calloutmax = (4 * 1024);
    263    1.7   thorpej 
    264    1.1     lukem #define	KN_HASHSIZE		64		/* XXX should be tunable */
    265    1.3  jdolecek #define	KN_HASH(val, mask)	(((val) ^ (val >> 8)) & (mask))
    266    1.1     lukem 
    267  1.124   thorpej extern const struct filterops fs_filtops;	/* vfs_syscalls.c */
    268  1.124   thorpej extern const struct filterops sig_filtops;	/* kern_sig.c */
    269    1.1     lukem 
    270    1.1     lukem /*
    271  1.150   msaitoh  * Table for all system-defined filters.
    272    1.3  jdolecek  * These should be listed in the numeric order of the EVFILT_* defines.
    273    1.3  jdolecek  * If filtops is NULL, the filter isn't implemented in NetBSD.
    274    1.3  jdolecek  * End of list is when name is NULL.
    275   1.93  riastrad  *
    276   1.49        ad  * Note that 'refcnt' is meaningless for built-in filters.
    277    1.1     lukem  */
    278    1.3  jdolecek struct kfilter {
    279   1.49        ad 	const char	*name;		/* name of filter */
    280   1.49        ad 	uint32_t	filter;		/* id of filter */
    281   1.49        ad 	unsigned	refcnt;		/* reference count */
    282    1.3  jdolecek 	const struct filterops *filtops;/* operations for filter */
    283   1.49        ad 	size_t		namelen;	/* length of name string */
    284    1.3  jdolecek };
    285    1.3  jdolecek 
    286   1.49        ad /* System defined filters */
    287   1.49        ad static struct kfilter sys_kfilters[] = {
    288   1.49        ad 	{ "EVFILT_READ",	EVFILT_READ,	0, &file_filtops, 0 },
    289   1.49        ad 	{ "EVFILT_WRITE",	EVFILT_WRITE,	0, &file_filtops, 0, },
    290   1.49        ad 	{ "EVFILT_AIO",		EVFILT_AIO,	0, NULL, 0 },
    291   1.49        ad 	{ "EVFILT_VNODE",	EVFILT_VNODE,	0, &file_filtops, 0 },
    292   1.49        ad 	{ "EVFILT_PROC",	EVFILT_PROC,	0, &proc_filtops, 0 },
    293   1.49        ad 	{ "EVFILT_SIGNAL",	EVFILT_SIGNAL,	0, &sig_filtops, 0 },
    294   1.49        ad 	{ "EVFILT_TIMER",	EVFILT_TIMER,	0, &timer_filtops, 0 },
    295  1.102  christos 	{ "EVFILT_FS",		EVFILT_FS,	0, &fs_filtops, 0 },
    296  1.108  christos 	{ "EVFILT_USER",	EVFILT_USER,	0, &user_filtops, 0 },
    297  1.137   thorpej 	{ "EVFILT_EMPTY",	EVFILT_EMPTY,	0, &file_filtops, 0 },
    298   1.49        ad 	{ NULL,			0,		0, NULL, 0 },
    299    1.1     lukem };
    300    1.1     lukem 
    301   1.49        ad /* User defined kfilters */
    302    1.3  jdolecek static struct kfilter	*user_kfilters;		/* array */
    303    1.3  jdolecek static int		user_kfilterc;		/* current offset */
    304    1.3  jdolecek static int		user_kfiltermaxc;	/* max size so far */
    305   1.49        ad static size_t		user_kfiltersz;		/* size of allocated memory */
    306   1.49        ad 
    307   1.95  riastrad /*
    308   1.95  riastrad  * Global Locks.
    309   1.95  riastrad  *
    310   1.95  riastrad  * Lock order:
    311   1.95  riastrad  *
    312   1.95  riastrad  *	kqueue_filter_lock
    313   1.95  riastrad  *	-> kn_kq->kq_fdp->fd_lock
    314  1.145   thorpej  *	-> knote foplock (if taken)
    315  1.125   thorpej  *	-> object lock (e.g., device driver lock, &c.)
    316   1.95  riastrad  *	-> kn_kq->kq_lock
    317   1.95  riastrad  *
    318  1.145   thorpej  * Locking rules.  ==> indicates the lock is acquired by the backing
    319  1.145   thorpej  * object, locks prior are acquired before calling filter ops:
    320  1.145   thorpej  *
    321  1.145   thorpej  *	f_attach: fdp->fd_lock -> knote foplock ->
    322  1.145   thorpej  *	  (maybe) KERNEL_LOCK ==> backing object lock
    323   1.95  riastrad  *
    324  1.145   thorpej  *	f_detach: fdp->fd_lock -> knote foplock ->
    325  1.145   thorpej  *	   (maybe) KERNEL_LOCK ==> backing object lock
    326  1.145   thorpej  *
    327  1.145   thorpej  *	f_event via kevent: fdp->fd_lock -> knote foplock ->
    328  1.145   thorpej  *	   (maybe) KERNEL_LOCK ==> backing object lock
    329  1.145   thorpej  *	   N.B. NOTE_SUBMIT will never be set in the "hint" argument
    330  1.145   thorpej  *	   in this case.
    331  1.145   thorpej  *
    332  1.145   thorpej  *	f_event via knote (via backing object: Whatever caller guarantees.
    333  1.145   thorpej  *	Typically:
    334  1.145   thorpej  *		f_event(NOTE_SUBMIT): caller has already acquired backing
    335  1.145   thorpej  *		    object lock.
    336  1.145   thorpej  *		f_event(!NOTE_SUBMIT): caller has not acquired backing object,
    337  1.145   thorpej  *		    lock or has possibly acquired KERNEL_LOCK.  Backing object
    338  1.145   thorpej  *		    lock may or may not be acquired as-needed.
    339  1.145   thorpej  *	N.B. the knote foplock will **not** be acquired in this case.  The
    340  1.145   thorpej  *	caller guarantees that klist_fini() will not be called concurrently
    341  1.145   thorpej  *	with knote().
    342  1.145   thorpej  *
    343  1.145   thorpej  *	f_touch: fdp->fd_lock -> kn_kq->kq_lock (spin lock)
    344  1.145   thorpej  *	    N.B. knote foplock is **not** acquired in this case and
    345  1.145   thorpej  *	    the caller must guarantee that klist_fini() will never
    346  1.145   thorpej  *	    be called.  kevent_register() restricts filters that
    347  1.145   thorpej  *	    provide f_touch to known-safe cases.
    348  1.145   thorpej  *
    349  1.145   thorpej  *	klist_fini(): Caller must guarantee that no more knotes can
    350  1.145   thorpej  *	    be attached to the klist, and must **not** hold the backing
    351  1.145   thorpej  *	    object's lock; klist_fini() itself will acquire the foplock
    352  1.145   thorpej  *	    of each knote on the klist.
    353  1.129   thorpej  *
    354  1.129   thorpej  * Locking rules when detaching knotes:
    355  1.129   thorpej  *
    356  1.129   thorpej  * There are some situations where knote submission may require dropping
    357  1.129   thorpej  * locks (see knote_proc_fork()).  In order to support this, it's possible
    358  1.129   thorpej  * to mark a knote as being 'in-flux'.  Such a knote is guaranteed not to
    359  1.129   thorpej  * be detached while it remains in-flux.  Because it will not be detached,
    360  1.129   thorpej  * locks can be dropped so e.g. memory can be allocated, locks on other
    361  1.129   thorpej  * data structures can be acquired, etc.  During this time, any attempt to
    362  1.129   thorpej  * detach an in-flux knote must wait until the knote is no longer in-flux.
    363  1.129   thorpej  * When this happens, the knote is marked for death (KN_WILLDETACH) and the
    364  1.129   thorpej  * LWP who gets to finish the detach operation is recorded in the knote's
    365  1.129   thorpej  * 'udata' field (which is no longer required for its original purpose once
    366  1.129   thorpej  * a knote is so marked).  Code paths that lead to knote_detach() must ensure
    367  1.129   thorpej  * that their LWP is the one tasked with its final demise after waiting for
    368  1.129   thorpej  * the in-flux status of the knote to clear.  Note that once a knote is
    369  1.129   thorpej  * marked KN_WILLDETACH, no code paths may put it into an in-flux state.
    370  1.129   thorpej  *
    371  1.129   thorpej  * Once the special circumstances have been handled, the locks are re-
    372  1.129   thorpej  * acquired in the proper order (object lock -> kq_lock), the knote taken
    373  1.129   thorpej  * out of flux, and any waiters are notified.  Because waiters must have
    374  1.129   thorpej  * also dropped *their* locks in order to safely block, they must re-
    375  1.129   thorpej  * validate all of their assumptions; see knote_detach_quiesce().  See also
    376  1.129   thorpej  * the kqueue_register() (EV_ADD, EV_DELETE) and kqueue_scan() (EV_ONESHOT)
    377  1.129   thorpej  * cases.
    378  1.129   thorpej  *
    379  1.129   thorpej  * When kqueue_scan() encounters an in-flux knote, the situation is
    380  1.129   thorpej  * treated like another LWP's list marker.
    381  1.129   thorpej  *
    382  1.129   thorpej  * LISTEN WELL: It is important to not hold knotes in flux for an
    383  1.129   thorpej  * extended period of time! In-flux knotes effectively block any
    384  1.129   thorpej  * progress of the kqueue_scan() operation.  Any code paths that place
    385  1.129   thorpej  * knotes in-flux should be careful to not block for indefinite periods
    386  1.129   thorpej  * of time, such as for memory allocation (i.e. KM_NOSLEEP is OK, but
    387  1.129   thorpej  * KM_SLEEP is not).
    388   1.95  riastrad  */
    389   1.49        ad static krwlock_t	kqueue_filter_lock;	/* lock on filter lists */
    390   1.49        ad 
    391  1.129   thorpej #define	KQ_FLUX_WAIT(kq)	(void)cv_wait(&kq->kq_cv, &kq->kq_lock)
    392  1.129   thorpej #define	KQ_FLUX_WAKEUP(kq)	cv_broadcast(&kq->kq_cv)
    393  1.129   thorpej 
    394  1.129   thorpej static inline bool
    395  1.129   thorpej kn_in_flux(struct knote *kn)
    396  1.129   thorpej {
    397  1.129   thorpej 	KASSERT(mutex_owned(&kn->kn_kq->kq_lock));
    398  1.144   thorpej 	return KNOTE_TO_KIMPL(kn)->ki_influx != 0;
    399  1.129   thorpej }
    400  1.129   thorpej 
    401  1.129   thorpej static inline bool
    402  1.129   thorpej kn_enter_flux(struct knote *kn)
    403  1.129   thorpej {
    404  1.129   thorpej 	KASSERT(mutex_owned(&kn->kn_kq->kq_lock));
    405  1.129   thorpej 
    406  1.129   thorpej 	if (kn->kn_status & KN_WILLDETACH) {
    407  1.129   thorpej 		return false;
    408  1.129   thorpej 	}
    409  1.129   thorpej 
    410  1.144   thorpej 	struct knote_impl *ki = KNOTE_TO_KIMPL(kn);
    411  1.144   thorpej 	KASSERT(ki->ki_influx < UINT_MAX);
    412  1.144   thorpej 	ki->ki_influx++;
    413  1.129   thorpej 
    414  1.129   thorpej 	return true;
    415  1.129   thorpej }
    416  1.129   thorpej 
    417  1.129   thorpej static inline bool
    418  1.129   thorpej kn_leave_flux(struct knote *kn)
    419  1.129   thorpej {
    420  1.129   thorpej 	KASSERT(mutex_owned(&kn->kn_kq->kq_lock));
    421  1.144   thorpej 
    422  1.144   thorpej 	struct knote_impl *ki = KNOTE_TO_KIMPL(kn);
    423  1.144   thorpej 	KASSERT(ki->ki_influx > 0);
    424  1.144   thorpej 	ki->ki_influx--;
    425  1.144   thorpej 	return ki->ki_influx == 0;
    426  1.129   thorpej }
    427  1.129   thorpej 
    428  1.129   thorpej static void
    429  1.129   thorpej kn_wait_flux(struct knote *kn, bool can_loop)
    430  1.129   thorpej {
    431  1.144   thorpej 	struct knote_impl *ki = KNOTE_TO_KIMPL(kn);
    432  1.129   thorpej 	bool loop;
    433  1.129   thorpej 
    434  1.129   thorpej 	KASSERT(mutex_owned(&kn->kn_kq->kq_lock));
    435  1.129   thorpej 
    436  1.129   thorpej 	/*
    437  1.129   thorpej 	 * It may not be safe for us to touch the knote again after
    438  1.129   thorpej 	 * dropping the kq_lock.  The caller has let us know in
    439  1.129   thorpej 	 * 'can_loop'.
    440  1.129   thorpej 	 */
    441  1.144   thorpej 	for (loop = true; loop && ki->ki_influx != 0; loop = can_loop) {
    442  1.129   thorpej 		KQ_FLUX_WAIT(kn->kn_kq);
    443  1.129   thorpej 	}
    444  1.129   thorpej }
    445  1.129   thorpej 
    446  1.129   thorpej #define	KNOTE_WILLDETACH(kn)						\
    447  1.129   thorpej do {									\
    448  1.129   thorpej 	(kn)->kn_status |= KN_WILLDETACH;				\
    449  1.129   thorpej 	(kn)->kn_kevent.udata = curlwp;					\
    450  1.129   thorpej } while (/*CONSTCOND*/0)
    451  1.129   thorpej 
    452  1.129   thorpej /*
    453  1.129   thorpej  * Wait until the specified knote is in a quiescent state and
    454  1.129   thorpej  * safe to detach.  Returns true if we potentially blocked (and
    455  1.129   thorpej  * thus dropped our locks).
    456  1.129   thorpej  */
    457  1.129   thorpej static bool
    458  1.129   thorpej knote_detach_quiesce(struct knote *kn)
    459  1.129   thorpej {
    460  1.129   thorpej 	struct kqueue *kq = kn->kn_kq;
    461  1.129   thorpej 	filedesc_t *fdp = kq->kq_fdp;
    462  1.129   thorpej 
    463  1.129   thorpej 	KASSERT(mutex_owned(&fdp->fd_lock));
    464  1.129   thorpej 
    465  1.129   thorpej 	mutex_spin_enter(&kq->kq_lock);
    466  1.129   thorpej 	/*
    467  1.129   thorpej 	 * There are two cases where we might see KN_WILLDETACH here:
    468  1.129   thorpej 	 *
    469  1.129   thorpej 	 * 1. Someone else has already started detaching the knote but
    470  1.129   thorpej 	 *    had to wait for it to settle first.
    471  1.129   thorpej 	 *
    472  1.129   thorpej 	 * 2. We had to wait for it to settle, and had to come back
    473  1.129   thorpej 	 *    around after re-acquiring the locks.
    474  1.129   thorpej 	 *
    475  1.129   thorpej 	 * When KN_WILLDETACH is set, we also set the LWP that claimed
    476  1.129   thorpej 	 * the prize of finishing the detach in the 'udata' field of the
    477  1.129   thorpej 	 * knote (which will never be used again for its usual purpose
    478  1.129   thorpej 	 * once the note is in this state).  If it doesn't point to us,
    479  1.129   thorpej 	 * we must drop the locks and let them in to finish the job.
    480  1.129   thorpej 	 *
    481  1.129   thorpej 	 * Otherwise, once we have claimed the knote for ourselves, we
    482  1.129   thorpej 	 * can finish waiting for it to settle.  The is the only scenario
    483  1.129   thorpej 	 * where touching a detaching knote is safe after dropping the
    484  1.129   thorpej 	 * locks.
    485  1.129   thorpej 	 */
    486  1.129   thorpej 	if ((kn->kn_status & KN_WILLDETACH) != 0 &&
    487  1.129   thorpej 	    kn->kn_kevent.udata != curlwp) {
    488  1.129   thorpej 		/*
    489  1.129   thorpej 		 * N.B. it is NOT safe for us to touch the knote again
    490  1.129   thorpej 		 * after dropping the locks here.  The caller must go
    491  1.129   thorpej 		 * back around and re-validate everything.  However, if
    492  1.129   thorpej 		 * the knote is in-flux, we want to block to minimize
    493  1.129   thorpej 		 * busy-looping.
    494  1.129   thorpej 		 */
    495  1.129   thorpej 		mutex_exit(&fdp->fd_lock);
    496  1.129   thorpej 		if (kn_in_flux(kn)) {
    497  1.129   thorpej 			kn_wait_flux(kn, false);
    498  1.129   thorpej 			mutex_spin_exit(&kq->kq_lock);
    499  1.129   thorpej 			return true;
    500  1.129   thorpej 		}
    501  1.129   thorpej 		mutex_spin_exit(&kq->kq_lock);
    502  1.129   thorpej 		preempt_point();
    503  1.129   thorpej 		return true;
    504  1.129   thorpej 	}
    505  1.129   thorpej 	/*
    506  1.129   thorpej 	 * If we get here, we know that we will be claiming the
    507  1.129   thorpej 	 * detach responsibilies, or that we already have and
    508  1.129   thorpej 	 * this is the second attempt after re-validation.
    509  1.129   thorpej 	 */
    510  1.129   thorpej 	KASSERT((kn->kn_status & KN_WILLDETACH) == 0 ||
    511  1.129   thorpej 		kn->kn_kevent.udata == curlwp);
    512  1.129   thorpej 	/*
    513  1.129   thorpej 	 * Similarly, if we get here, either we are just claiming it
    514  1.129   thorpej 	 * and may have to wait for it to settle, or if this is the
    515  1.129   thorpej 	 * second attempt after re-validation that no other code paths
    516  1.129   thorpej 	 * have put it in-flux.
    517  1.129   thorpej 	 */
    518  1.129   thorpej 	KASSERT((kn->kn_status & KN_WILLDETACH) == 0 ||
    519  1.129   thorpej 		kn_in_flux(kn) == false);
    520  1.129   thorpej 	KNOTE_WILLDETACH(kn);
    521  1.129   thorpej 	if (kn_in_flux(kn)) {
    522  1.129   thorpej 		mutex_exit(&fdp->fd_lock);
    523  1.129   thorpej 		kn_wait_flux(kn, true);
    524  1.129   thorpej 		/*
    525  1.129   thorpej 		 * It is safe for us to touch the knote again after
    526  1.129   thorpej 		 * dropping the locks, but the caller must still
    527  1.129   thorpej 		 * re-validate everything because other aspects of
    528  1.129   thorpej 		 * the environment may have changed while we blocked.
    529  1.129   thorpej 		 */
    530  1.129   thorpej 		KASSERT(kn_in_flux(kn) == false);
    531  1.129   thorpej 		mutex_spin_exit(&kq->kq_lock);
    532  1.129   thorpej 		return true;
    533  1.129   thorpej 	}
    534  1.129   thorpej 	mutex_spin_exit(&kq->kq_lock);
    535  1.129   thorpej 
    536  1.129   thorpej 	return false;
    537  1.129   thorpej }
    538  1.129   thorpej 
    539  1.145   thorpej /*
    540  1.145   thorpej  * Calls into the filterops need to be resilient against things which
    541  1.145   thorpej  * destroy a klist, e.g. device detach, freeing a vnode, etc., to avoid
    542  1.145   thorpej  * chasing garbage pointers (to data, or even potentially code in a
    543  1.145   thorpej  * module about to be unloaded).  To that end, we acquire the
    544  1.145   thorpej  * knote foplock before calling into the filter ops.  When a driver
    545  1.145   thorpej  * (or anything else) is tearing down its klist, klist_fini() enumerates
    546  1.145   thorpej  * each knote, acquires its foplock, and replaces the filterops with a
    547  1.145   thorpej  * nop stub, allowing knote detach (when descriptors are closed) to safely
    548  1.145   thorpej  * proceed.
    549  1.145   thorpej  */
    550  1.145   thorpej 
    551  1.122   thorpej static int
    552  1.122   thorpej filter_attach(struct knote *kn)
    553  1.122   thorpej {
    554  1.122   thorpej 	int rv;
    555  1.122   thorpej 
    556  1.145   thorpej 	KASSERT(knote_foplock_owned(kn));
    557  1.122   thorpej 	KASSERT(kn->kn_fop != NULL);
    558  1.122   thorpej 	KASSERT(kn->kn_fop->f_attach != NULL);
    559  1.122   thorpej 
    560  1.122   thorpej 	/*
    561  1.122   thorpej 	 * N.B. that kn->kn_fop may change as the result of calling
    562  1.145   thorpej 	 * f_attach().  After f_attach() returns, kn->kn_fop may not
    563  1.145   thorpej 	 * be modified by code outside of klist_fini().
    564  1.122   thorpej 	 */
    565  1.122   thorpej 	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
    566  1.122   thorpej 		rv = kn->kn_fop->f_attach(kn);
    567  1.122   thorpej 	} else {
    568  1.122   thorpej 		KERNEL_LOCK(1, NULL);
    569  1.122   thorpej 		rv = kn->kn_fop->f_attach(kn);
    570  1.122   thorpej 		KERNEL_UNLOCK_ONE(NULL);
    571  1.122   thorpej 	}
    572  1.122   thorpej 
    573  1.122   thorpej 	return rv;
    574  1.122   thorpej }
    575  1.122   thorpej 
    576  1.122   thorpej static void
    577  1.122   thorpej filter_detach(struct knote *kn)
    578  1.122   thorpej {
    579  1.145   thorpej 
    580  1.145   thorpej 	KASSERT(knote_foplock_owned(kn));
    581  1.122   thorpej 	KASSERT(kn->kn_fop != NULL);
    582  1.122   thorpej 	KASSERT(kn->kn_fop->f_detach != NULL);
    583  1.122   thorpej 
    584  1.122   thorpej 	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
    585  1.122   thorpej 		kn->kn_fop->f_detach(kn);
    586  1.122   thorpej 	} else {
    587  1.122   thorpej 		KERNEL_LOCK(1, NULL);
    588  1.122   thorpej 		kn->kn_fop->f_detach(kn);
    589  1.122   thorpej 		KERNEL_UNLOCK_ONE(NULL);
    590  1.122   thorpej 	}
    591  1.122   thorpej }
    592  1.122   thorpej 
    593  1.122   thorpej static int
    594  1.145   thorpej filter_event(struct knote *kn, long hint, bool submitting)
    595  1.122   thorpej {
    596  1.122   thorpej 	int rv;
    597  1.122   thorpej 
    598  1.145   thorpej 	/* See knote(). */
    599  1.145   thorpej 	KASSERT(submitting || knote_foplock_owned(kn));
    600  1.122   thorpej 	KASSERT(kn->kn_fop != NULL);
    601  1.122   thorpej 	KASSERT(kn->kn_fop->f_event != NULL);
    602  1.122   thorpej 
    603  1.122   thorpej 	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
    604  1.122   thorpej 		rv = kn->kn_fop->f_event(kn, hint);
    605  1.122   thorpej 	} else {
    606  1.122   thorpej 		KERNEL_LOCK(1, NULL);
    607  1.122   thorpej 		rv = kn->kn_fop->f_event(kn, hint);
    608  1.122   thorpej 		KERNEL_UNLOCK_ONE(NULL);
    609  1.122   thorpej 	}
    610  1.122   thorpej 
    611  1.122   thorpej 	return rv;
    612  1.122   thorpej }
    613  1.122   thorpej 
    614  1.135   thorpej static int
    615  1.122   thorpej filter_touch(struct knote *kn, struct kevent *kev, long type)
    616  1.122   thorpej {
    617  1.145   thorpej 
    618  1.145   thorpej 	/*
    619  1.145   thorpej 	 * XXX We cannot assert that the knote foplock is held here
    620  1.145   thorpej 	 * XXX beause we cannot safely acquire it in all cases
    621  1.145   thorpej 	 * XXX where "touch" will be used in kqueue_scan().  We just
    622  1.145   thorpej 	 * XXX have to assume that f_touch will always be safe to call,
    623  1.145   thorpej 	 * XXX and kqueue_register() allows only the two known-safe
    624  1.145   thorpej 	 * XXX users of that op.
    625  1.145   thorpej 	 */
    626  1.145   thorpej 
    627  1.145   thorpej 	KASSERT(kn->kn_fop != NULL);
    628  1.145   thorpej 	KASSERT(kn->kn_fop->f_touch != NULL);
    629  1.145   thorpej 
    630  1.135   thorpej 	return kn->kn_fop->f_touch(kn, kev, type);
    631  1.122   thorpej }
    632  1.122   thorpej 
    633   1.66      elad static kauth_listener_t	kqueue_listener;
    634   1.66      elad 
    635   1.66      elad static int
    636   1.66      elad kqueue_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    637   1.66      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    638   1.66      elad {
    639   1.66      elad 	struct proc *p;
    640   1.66      elad 	int result;
    641   1.66      elad 
    642   1.66      elad 	result = KAUTH_RESULT_DEFER;
    643   1.66      elad 	p = arg0;
    644   1.66      elad 
    645   1.66      elad 	if (action != KAUTH_PROCESS_KEVENT_FILTER)
    646   1.66      elad 		return result;
    647   1.66      elad 
    648   1.66      elad 	if ((kauth_cred_getuid(p->p_cred) != kauth_cred_getuid(cred) ||
    649   1.66      elad 	    ISSET(p->p_flag, PK_SUGID)))
    650   1.66      elad 		return result;
    651   1.66      elad 
    652   1.66      elad 	result = KAUTH_RESULT_ALLOW;
    653   1.66      elad 
    654   1.66      elad 	return result;
    655   1.66      elad }
    656   1.66      elad 
    657   1.49        ad /*
    658   1.49        ad  * Initialize the kqueue subsystem.
    659   1.49        ad  */
    660   1.49        ad void
    661   1.49        ad kqueue_init(void)
    662   1.49        ad {
    663   1.49        ad 
    664   1.49        ad 	rw_init(&kqueue_filter_lock);
    665   1.66      elad 
    666   1.66      elad 	kqueue_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    667   1.66      elad 	    kqueue_listener_cb, NULL);
    668   1.49        ad }
    669    1.3  jdolecek 
    670    1.3  jdolecek /*
    671    1.3  jdolecek  * Find kfilter entry by name, or NULL if not found.
    672    1.3  jdolecek  */
    673   1.49        ad static struct kfilter *
    674    1.3  jdolecek kfilter_byname_sys(const char *name)
    675    1.3  jdolecek {
    676    1.3  jdolecek 	int i;
    677    1.3  jdolecek 
    678   1.49        ad 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    679   1.49        ad 
    680    1.3  jdolecek 	for (i = 0; sys_kfilters[i].name != NULL; i++) {
    681    1.3  jdolecek 		if (strcmp(name, sys_kfilters[i].name) == 0)
    682   1.49        ad 			return &sys_kfilters[i];
    683    1.3  jdolecek 	}
    684   1.49        ad 	return NULL;
    685    1.3  jdolecek }
    686    1.3  jdolecek 
    687    1.3  jdolecek static struct kfilter *
    688    1.3  jdolecek kfilter_byname_user(const char *name)
    689    1.3  jdolecek {
    690    1.3  jdolecek 	int i;
    691    1.3  jdolecek 
    692   1.49        ad 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    693   1.49        ad 
    694   1.31     seanb 	/* user filter slots have a NULL name if previously deregistered */
    695   1.31     seanb 	for (i = 0; i < user_kfilterc ; i++) {
    696   1.31     seanb 		if (user_kfilters[i].name != NULL &&
    697    1.3  jdolecek 		    strcmp(name, user_kfilters[i].name) == 0)
    698   1.49        ad 			return &user_kfilters[i];
    699    1.3  jdolecek 	}
    700   1.49        ad 	return NULL;
    701    1.3  jdolecek }
    702    1.3  jdolecek 
    703   1.49        ad static struct kfilter *
    704    1.3  jdolecek kfilter_byname(const char *name)
    705    1.3  jdolecek {
    706   1.49        ad 	struct kfilter *kfilter;
    707   1.49        ad 
    708   1.49        ad 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    709    1.3  jdolecek 
    710    1.3  jdolecek 	if ((kfilter = kfilter_byname_sys(name)) != NULL)
    711   1.49        ad 		return kfilter;
    712    1.3  jdolecek 
    713   1.49        ad 	return kfilter_byname_user(name);
    714    1.3  jdolecek }
    715    1.3  jdolecek 
    716    1.3  jdolecek /*
    717    1.3  jdolecek  * Find kfilter entry by filter id, or NULL if not found.
    718    1.3  jdolecek  * Assumes entries are indexed in filter id order, for speed.
    719    1.3  jdolecek  */
    720   1.49        ad static struct kfilter *
    721    1.3  jdolecek kfilter_byfilter(uint32_t filter)
    722    1.3  jdolecek {
    723   1.49        ad 	struct kfilter *kfilter;
    724   1.49        ad 
    725   1.49        ad 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    726    1.3  jdolecek 
    727    1.3  jdolecek 	if (filter < EVFILT_SYSCOUNT)	/* it's a system filter */
    728    1.3  jdolecek 		kfilter = &sys_kfilters[filter];
    729    1.3  jdolecek 	else if (user_kfilters != NULL &&
    730    1.3  jdolecek 	    filter < EVFILT_SYSCOUNT + user_kfilterc)
    731    1.3  jdolecek 					/* it's a user filter */
    732    1.3  jdolecek 		kfilter = &user_kfilters[filter - EVFILT_SYSCOUNT];
    733    1.3  jdolecek 	else
    734    1.3  jdolecek 		return (NULL);		/* out of range */
    735    1.3  jdolecek 	KASSERT(kfilter->filter == filter);	/* sanity check! */
    736    1.3  jdolecek 	return (kfilter);
    737    1.3  jdolecek }
    738    1.3  jdolecek 
    739    1.3  jdolecek /*
    740    1.3  jdolecek  * Register a new kfilter. Stores the entry in user_kfilters.
    741    1.3  jdolecek  * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
    742    1.3  jdolecek  * If retfilter != NULL, the new filterid is returned in it.
    743    1.3  jdolecek  */
    744    1.3  jdolecek int
    745    1.3  jdolecek kfilter_register(const char *name, const struct filterops *filtops,
    746   1.49        ad 		 int *retfilter)
    747    1.1     lukem {
    748    1.3  jdolecek 	struct kfilter *kfilter;
    749   1.49        ad 	size_t len;
    750   1.31     seanb 	int i;
    751    1.3  jdolecek 
    752    1.3  jdolecek 	if (name == NULL || name[0] == '\0' || filtops == NULL)
    753    1.3  jdolecek 		return (EINVAL);	/* invalid args */
    754   1.49        ad 
    755   1.49        ad 	rw_enter(&kqueue_filter_lock, RW_WRITER);
    756   1.49        ad 	if (kfilter_byname(name) != NULL) {
    757   1.49        ad 		rw_exit(&kqueue_filter_lock);
    758    1.3  jdolecek 		return (EEXIST);	/* already exists */
    759   1.49        ad 	}
    760   1.49        ad 	if (user_kfilterc > 0xffffffff - EVFILT_SYSCOUNT) {
    761   1.49        ad 		rw_exit(&kqueue_filter_lock);
    762    1.3  jdolecek 		return (EINVAL);	/* too many */
    763   1.49        ad 	}
    764    1.3  jdolecek 
    765   1.31     seanb 	for (i = 0; i < user_kfilterc; i++) {
    766   1.31     seanb 		kfilter = &user_kfilters[i];
    767   1.31     seanb 		if (kfilter->name == NULL) {
    768   1.31     seanb 			/* Previously deregistered slot.  Reuse. */
    769   1.31     seanb 			goto reuse;
    770   1.31     seanb 		}
    771   1.31     seanb 	}
    772   1.31     seanb 
    773    1.3  jdolecek 	/* check if need to grow user_kfilters */
    774    1.3  jdolecek 	if (user_kfilterc + 1 > user_kfiltermaxc) {
    775   1.49        ad 		/* Grow in KFILTER_EXTENT chunks. */
    776    1.3  jdolecek 		user_kfiltermaxc += KFILTER_EXTENT;
    777   1.69       dsl 		len = user_kfiltermaxc * sizeof(*kfilter);
    778   1.49        ad 		kfilter = kmem_alloc(len, KM_SLEEP);
    779   1.49        ad 		memset((char *)kfilter + user_kfiltersz, 0, len - user_kfiltersz);
    780   1.49        ad 		if (user_kfilters != NULL) {
    781   1.49        ad 			memcpy(kfilter, user_kfilters, user_kfiltersz);
    782   1.49        ad 			kmem_free(user_kfilters, user_kfiltersz);
    783   1.49        ad 		}
    784   1.49        ad 		user_kfiltersz = len;
    785    1.3  jdolecek 		user_kfilters = kfilter;
    786    1.3  jdolecek 	}
    787   1.31     seanb 	/* Adding new slot */
    788   1.31     seanb 	kfilter = &user_kfilters[user_kfilterc++];
    789   1.31     seanb reuse:
    790   1.97  christos 	kfilter->name = kmem_strdupsize(name, &kfilter->namelen, KM_SLEEP);
    791    1.3  jdolecek 
    792   1.31     seanb 	kfilter->filter = (kfilter - user_kfilters) + EVFILT_SYSCOUNT;
    793    1.3  jdolecek 
    794   1.49        ad 	kfilter->filtops = kmem_alloc(sizeof(*filtops), KM_SLEEP);
    795   1.49        ad 	memcpy(__UNCONST(kfilter->filtops), filtops, sizeof(*filtops));
    796    1.3  jdolecek 
    797    1.3  jdolecek 	if (retfilter != NULL)
    798   1.31     seanb 		*retfilter = kfilter->filter;
    799   1.49        ad 	rw_exit(&kqueue_filter_lock);
    800   1.49        ad 
    801    1.3  jdolecek 	return (0);
    802    1.1     lukem }
    803    1.1     lukem 
    804    1.3  jdolecek /*
    805    1.3  jdolecek  * Unregister a kfilter previously registered with kfilter_register.
    806    1.3  jdolecek  * This retains the filter id, but clears the name and frees filtops (filter
    807    1.3  jdolecek  * operations), so that the number isn't reused during a boot.
    808    1.3  jdolecek  * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
    809    1.3  jdolecek  */
    810    1.3  jdolecek int
    811    1.3  jdolecek kfilter_unregister(const char *name)
    812    1.1     lukem {
    813    1.3  jdolecek 	struct kfilter *kfilter;
    814    1.3  jdolecek 
    815    1.3  jdolecek 	if (name == NULL || name[0] == '\0')
    816    1.3  jdolecek 		return (EINVAL);	/* invalid name */
    817    1.3  jdolecek 
    818   1.49        ad 	rw_enter(&kqueue_filter_lock, RW_WRITER);
    819   1.49        ad 	if (kfilter_byname_sys(name) != NULL) {
    820   1.49        ad 		rw_exit(&kqueue_filter_lock);
    821    1.3  jdolecek 		return (EINVAL);	/* can't detach system filters */
    822   1.49        ad 	}
    823    1.1     lukem 
    824    1.3  jdolecek 	kfilter = kfilter_byname_user(name);
    825   1.49        ad 	if (kfilter == NULL) {
    826   1.49        ad 		rw_exit(&kqueue_filter_lock);
    827    1.3  jdolecek 		return (ENOENT);
    828   1.49        ad 	}
    829   1.49        ad 	if (kfilter->refcnt != 0) {
    830   1.49        ad 		rw_exit(&kqueue_filter_lock);
    831   1.49        ad 		return (EBUSY);
    832   1.49        ad 	}
    833    1.1     lukem 
    834   1.49        ad 	/* Cast away const (but we know it's safe. */
    835   1.49        ad 	kmem_free(__UNCONST(kfilter->name), kfilter->namelen);
    836   1.31     seanb 	kfilter->name = NULL;	/* mark as `not implemented' */
    837   1.31     seanb 
    838    1.3  jdolecek 	if (kfilter->filtops != NULL) {
    839   1.49        ad 		/* Cast away const (but we know it's safe. */
    840   1.49        ad 		kmem_free(__UNCONST(kfilter->filtops),
    841   1.49        ad 		    sizeof(*kfilter->filtops));
    842    1.3  jdolecek 		kfilter->filtops = NULL; /* mark as `not implemented' */
    843    1.3  jdolecek 	}
    844   1.49        ad 	rw_exit(&kqueue_filter_lock);
    845   1.49        ad 
    846    1.1     lukem 	return (0);
    847    1.1     lukem }
    848    1.1     lukem 
    849    1.3  jdolecek 
    850    1.3  jdolecek /*
    851    1.3  jdolecek  * Filter attach method for EVFILT_READ and EVFILT_WRITE on normal file
    852   1.49        ad  * descriptors. Calls fileops kqfilter method for given file descriptor.
    853    1.3  jdolecek  */
    854    1.3  jdolecek static int
    855    1.3  jdolecek filt_fileattach(struct knote *kn)
    856    1.3  jdolecek {
    857   1.49        ad 	file_t *fp;
    858   1.49        ad 
    859   1.49        ad 	fp = kn->kn_obj;
    860    1.3  jdolecek 
    861   1.49        ad 	return (*fp->f_ops->fo_kqfilter)(fp, kn);
    862    1.3  jdolecek }
    863    1.3  jdolecek 
    864    1.3  jdolecek /*
    865    1.3  jdolecek  * Filter detach method for EVFILT_READ on kqueue descriptor.
    866    1.3  jdolecek  */
    867    1.1     lukem static void
    868    1.1     lukem filt_kqdetach(struct knote *kn)
    869    1.1     lukem {
    870    1.3  jdolecek 	struct kqueue *kq;
    871    1.1     lukem 
    872   1.82      matt 	kq = ((file_t *)kn->kn_obj)->f_kqueue;
    873   1.49        ad 
    874   1.49        ad 	mutex_spin_enter(&kq->kq_lock);
    875  1.109   thorpej 	selremove_knote(&kq->kq_sel, kn);
    876   1.49        ad 	mutex_spin_exit(&kq->kq_lock);
    877    1.1     lukem }
    878    1.1     lukem 
    879    1.3  jdolecek /*
    880    1.3  jdolecek  * Filter event method for EVFILT_READ on kqueue descriptor.
    881    1.3  jdolecek  */
    882    1.1     lukem /*ARGSUSED*/
    883    1.1     lukem static int
    884   1.33      yamt filt_kqueue(struct knote *kn, long hint)
    885    1.1     lukem {
    886    1.3  jdolecek 	struct kqueue *kq;
    887   1.49        ad 	int rv;
    888   1.49        ad 
    889   1.82      matt 	kq = ((file_t *)kn->kn_obj)->f_kqueue;
    890    1.1     lukem 
    891   1.49        ad 	if (hint != NOTE_SUBMIT)
    892   1.49        ad 		mutex_spin_enter(&kq->kq_lock);
    893  1.118  jdolecek 	kn->kn_data = KQ_COUNT(kq);
    894   1.49        ad 	rv = (kn->kn_data > 0);
    895   1.49        ad 	if (hint != NOTE_SUBMIT)
    896   1.49        ad 		mutex_spin_exit(&kq->kq_lock);
    897   1.49        ad 
    898   1.49        ad 	return rv;
    899    1.1     lukem }
    900    1.1     lukem 
    901    1.3  jdolecek /*
    902    1.3  jdolecek  * Filter attach method for EVFILT_PROC.
    903    1.3  jdolecek  */
    904    1.1     lukem static int
    905    1.1     lukem filt_procattach(struct knote *kn)
    906    1.1     lukem {
    907   1.78     pooka 	struct proc *p;
    908    1.1     lukem 
    909  1.107        ad 	mutex_enter(&proc_lock);
    910  1.129   thorpej 	p = proc_find(kn->kn_id);
    911   1.49        ad 	if (p == NULL) {
    912  1.107        ad 		mutex_exit(&proc_lock);
    913   1.49        ad 		return ESRCH;
    914   1.49        ad 	}
    915    1.3  jdolecek 
    916    1.3  jdolecek 	/*
    917    1.3  jdolecek 	 * Fail if it's not owned by you, or the last exec gave us
    918    1.3  jdolecek 	 * setuid/setgid privs (unless you're root).
    919    1.3  jdolecek 	 */
    920   1.57        ad 	mutex_enter(p->p_lock);
    921  1.107        ad 	mutex_exit(&proc_lock);
    922  1.129   thorpej 	if (kauth_authorize_process(curlwp->l_cred,
    923  1.119  christos 	    KAUTH_PROCESS_KEVENT_FILTER, p, NULL, NULL, NULL) != 0) {
    924   1.57        ad 	    	mutex_exit(p->p_lock);
    925   1.49        ad 		return EACCES;
    926   1.49        ad 	}
    927    1.1     lukem 
    928   1.49        ad 	kn->kn_obj = p;
    929    1.3  jdolecek 	kn->kn_flags |= EV_CLEAR;	/* automatically set */
    930    1.1     lukem 
    931    1.1     lukem 	/*
    932  1.129   thorpej 	 * NOTE_CHILD is only ever generated internally; don't let it
    933  1.129   thorpej 	 * leak in from user-space.  See knote_proc_fork_track().
    934    1.1     lukem 	 */
    935  1.129   thorpej 	kn->kn_sfflags &= ~NOTE_CHILD;
    936  1.129   thorpej 
    937  1.140   thorpej 	klist_insert(&p->p_klist, kn);
    938   1.57        ad     	mutex_exit(p->p_lock);
    939    1.1     lukem 
    940   1.49        ad 	return 0;
    941    1.1     lukem }
    942    1.1     lukem 
    943    1.1     lukem /*
    944    1.3  jdolecek  * Filter detach method for EVFILT_PROC.
    945    1.3  jdolecek  *
    946    1.1     lukem  * The knote may be attached to a different process, which may exit,
    947    1.1     lukem  * leaving nothing for the knote to be attached to.  So when the process
    948    1.1     lukem  * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
    949    1.1     lukem  * it will be deleted when read out.  However, as part of the knote deletion,
    950    1.1     lukem  * this routine is called, so a check is needed to avoid actually performing
    951    1.3  jdolecek  * a detach, because the original process might not exist any more.
    952    1.1     lukem  */
    953    1.1     lukem static void
    954    1.1     lukem filt_procdetach(struct knote *kn)
    955    1.1     lukem {
    956  1.129   thorpej 	struct kqueue *kq = kn->kn_kq;
    957    1.3  jdolecek 	struct proc *p;
    958    1.1     lukem 
    959  1.129   thorpej 	/*
    960  1.129   thorpej 	 * We have to synchronize with knote_proc_exit(), but we
    961  1.129   thorpej 	 * are forced to acquire the locks in the wrong order here
    962  1.129   thorpej 	 * because we can't be sure kn->kn_obj is valid unless
    963  1.129   thorpej 	 * KN_DETACHED is not set.
    964  1.129   thorpej 	 */
    965  1.129   thorpej  again:
    966  1.129   thorpej 	mutex_spin_enter(&kq->kq_lock);
    967  1.129   thorpej 	if ((kn->kn_status & KN_DETACHED) == 0) {
    968  1.129   thorpej 		p = kn->kn_obj;
    969  1.129   thorpej 		if (!mutex_tryenter(p->p_lock)) {
    970  1.129   thorpej 			mutex_spin_exit(&kq->kq_lock);
    971  1.129   thorpej 			preempt_point();
    972  1.129   thorpej 			goto again;
    973  1.129   thorpej 		}
    974  1.129   thorpej 		kn->kn_status |= KN_DETACHED;
    975  1.140   thorpej 		klist_remove(&p->p_klist, kn);
    976  1.129   thorpej 		mutex_exit(p->p_lock);
    977  1.129   thorpej 	}
    978  1.129   thorpej 	mutex_spin_exit(&kq->kq_lock);
    979    1.1     lukem }
    980    1.1     lukem 
    981    1.3  jdolecek /*
    982    1.3  jdolecek  * Filter event method for EVFILT_PROC.
    983  1.129   thorpej  *
    984  1.129   thorpej  * Due to some of the complexities of process locking, we have special
    985  1.129   thorpej  * entry points for delivering knote submissions.  filt_proc() is used
    986  1.129   thorpej  * only to check for activation from kqueue_register() and kqueue_scan().
    987    1.3  jdolecek  */
    988    1.1     lukem static int
    989    1.1     lukem filt_proc(struct knote *kn, long hint)
    990    1.1     lukem {
    991  1.129   thorpej 	struct kqueue *kq = kn->kn_kq;
    992  1.129   thorpej 	uint32_t fflags;
    993  1.129   thorpej 
    994  1.129   thorpej 	/*
    995  1.129   thorpej 	 * Because we share the same klist with signal knotes, just
    996  1.129   thorpej 	 * ensure that we're not being invoked for the proc-related
    997  1.129   thorpej 	 * submissions.
    998  1.129   thorpej 	 */
    999  1.129   thorpej 	KASSERT((hint & (NOTE_EXEC | NOTE_EXIT | NOTE_FORK)) == 0);
   1000  1.129   thorpej 
   1001  1.129   thorpej 	mutex_spin_enter(&kq->kq_lock);
   1002  1.129   thorpej 	fflags = kn->kn_fflags;
   1003  1.129   thorpej 	mutex_spin_exit(&kq->kq_lock);
   1004    1.1     lukem 
   1005  1.129   thorpej 	return fflags != 0;
   1006  1.129   thorpej }
   1007    1.1     lukem 
   1008  1.129   thorpej void
   1009  1.129   thorpej knote_proc_exec(struct proc *p)
   1010  1.129   thorpej {
   1011  1.129   thorpej 	struct knote *kn, *tmpkn;
   1012  1.129   thorpej 	struct kqueue *kq;
   1013  1.129   thorpej 	uint32_t fflags;
   1014    1.1     lukem 
   1015  1.129   thorpej 	mutex_enter(p->p_lock);
   1016   1.83  christos 
   1017  1.129   thorpej 	SLIST_FOREACH_SAFE(kn, &p->p_klist, kn_selnext, tmpkn) {
   1018  1.129   thorpej 		/* N.B. EVFILT_SIGNAL knotes are on this same list. */
   1019  1.129   thorpej 		if (kn->kn_fop == &sig_filtops) {
   1020  1.129   thorpej 			continue;
   1021  1.129   thorpej 		}
   1022  1.129   thorpej 		KASSERT(kn->kn_fop == &proc_filtops);
   1023   1.49        ad 
   1024  1.129   thorpej 		kq = kn->kn_kq;
   1025   1.49        ad 		mutex_spin_enter(&kq->kq_lock);
   1026  1.129   thorpej 		fflags = (kn->kn_fflags |= (kn->kn_sfflags & NOTE_EXEC));
   1027  1.129   thorpej 		if (fflags) {
   1028  1.133   thorpej 			knote_activate_locked(kn);
   1029  1.129   thorpej 		}
   1030  1.133   thorpej 		mutex_spin_exit(&kq->kq_lock);
   1031  1.129   thorpej 	}
   1032  1.129   thorpej 
   1033  1.129   thorpej 	mutex_exit(p->p_lock);
   1034  1.129   thorpej }
   1035  1.129   thorpej 
   1036  1.129   thorpej static int __noinline
   1037  1.129   thorpej knote_proc_fork_track(struct proc *p1, struct proc *p2, struct knote *okn)
   1038  1.129   thorpej {
   1039  1.129   thorpej 	struct kqueue *kq = okn->kn_kq;
   1040  1.129   thorpej 
   1041  1.129   thorpej 	KASSERT(mutex_owned(&kq->kq_lock));
   1042  1.129   thorpej 	KASSERT(mutex_owned(p1->p_lock));
   1043  1.129   thorpej 
   1044  1.129   thorpej 	/*
   1045  1.129   thorpej 	 * We're going to put this knote into flux while we drop
   1046  1.129   thorpej 	 * the locks and create and attach a new knote to track the
   1047  1.129   thorpej 	 * child.  If we are not able to enter flux, then this knote
   1048  1.129   thorpej 	 * is about to go away, so skip the notification.
   1049  1.129   thorpej 	 */
   1050  1.129   thorpej 	if (!kn_enter_flux(okn)) {
   1051  1.129   thorpej 		return 0;
   1052  1.129   thorpej 	}
   1053  1.129   thorpej 
   1054  1.129   thorpej 	mutex_spin_exit(&kq->kq_lock);
   1055  1.129   thorpej 	mutex_exit(p1->p_lock);
   1056   1.49        ad 
   1057  1.129   thorpej 	/*
   1058  1.129   thorpej 	 * We actually have to register *two* new knotes:
   1059  1.129   thorpej 	 *
   1060  1.129   thorpej 	 * ==> One for the NOTE_CHILD notification.  This is a forced
   1061  1.129   thorpej 	 *     ONESHOT note.
   1062  1.129   thorpej 	 *
   1063  1.129   thorpej 	 * ==> One to actually track the child process as it subsequently
   1064  1.129   thorpej 	 *     forks, execs, and, ultimately, exits.
   1065  1.129   thorpej 	 *
   1066  1.129   thorpej 	 * If we only register a single knote, then it's possible for
   1067  1.129   thorpej 	 * for the NOTE_CHILD and NOTE_EXIT to be collapsed into a single
   1068  1.129   thorpej 	 * notification if the child exits before the tracking process
   1069  1.129   thorpej 	 * has received the NOTE_CHILD notification, which applications
   1070  1.129   thorpej 	 * aren't expecting (the event's 'data' field would be clobbered,
   1071  1.141    andvar 	 * for example).
   1072  1.129   thorpej 	 *
   1073  1.129   thorpej 	 * To do this, what we have here is an **extremely** stripped-down
   1074  1.129   thorpej 	 * version of kqueue_register() that has the following properties:
   1075  1.129   thorpej 	 *
   1076  1.129   thorpej 	 * ==> Does not block to allocate memory.  If we are unable
   1077  1.129   thorpej 	 *     to allocate memory, we return ENOMEM.
   1078  1.129   thorpej 	 *
   1079  1.129   thorpej 	 * ==> Does not search for existing knotes; we know there
   1080  1.129   thorpej 	 *     are not any because this is a new process that isn't
   1081  1.129   thorpej 	 *     even visible to other processes yet.
   1082  1.129   thorpej 	 *
   1083  1.129   thorpej 	 * ==> Assumes that the knhash for our kq's descriptor table
   1084  1.129   thorpej 	 *     already exists (after all, we're already tracking
   1085  1.129   thorpej 	 *     processes with knotes if we got here).
   1086  1.129   thorpej 	 *
   1087  1.129   thorpej 	 * ==> Directly attaches the new tracking knote to the child
   1088  1.129   thorpej 	 *     process.
   1089  1.129   thorpej 	 *
   1090  1.129   thorpej 	 * The whole point is to do the minimum amount of work while the
   1091  1.129   thorpej 	 * knote is held in-flux, and to avoid doing extra work in general
   1092  1.129   thorpej 	 * (we already have the new child process; why bother looking it
   1093  1.129   thorpej 	 * up again?).
   1094  1.129   thorpej 	 */
   1095  1.129   thorpej 	filedesc_t *fdp = kq->kq_fdp;
   1096  1.129   thorpej 	struct knote *knchild, *kntrack;
   1097  1.129   thorpej 	int error = 0;
   1098  1.129   thorpej 
   1099  1.142   thorpej 	knchild = knote_alloc(false);
   1100  1.142   thorpej 	kntrack = knote_alloc(false);
   1101  1.129   thorpej 	if (__predict_false(knchild == NULL || kntrack == NULL)) {
   1102  1.129   thorpej 		error = ENOMEM;
   1103  1.129   thorpej 		goto out;
   1104  1.129   thorpej 	}
   1105  1.129   thorpej 
   1106  1.129   thorpej 	kntrack->kn_obj = p2;
   1107  1.129   thorpej 	kntrack->kn_id = p2->p_pid;
   1108  1.129   thorpej 	kntrack->kn_kq = kq;
   1109  1.129   thorpej 	kntrack->kn_fop = okn->kn_fop;
   1110  1.129   thorpej 	kntrack->kn_kfilter = okn->kn_kfilter;
   1111  1.129   thorpej 	kntrack->kn_sfflags = okn->kn_sfflags;
   1112  1.129   thorpej 	kntrack->kn_sdata = p1->p_pid;
   1113  1.129   thorpej 
   1114  1.129   thorpej 	kntrack->kn_kevent.ident = p2->p_pid;
   1115  1.129   thorpej 	kntrack->kn_kevent.filter = okn->kn_filter;
   1116  1.129   thorpej 	kntrack->kn_kevent.flags =
   1117  1.129   thorpej 	    okn->kn_flags | EV_ADD | EV_ENABLE | EV_CLEAR;
   1118  1.129   thorpej 	kntrack->kn_kevent.fflags = 0;
   1119  1.129   thorpej 	kntrack->kn_kevent.data = 0;
   1120  1.129   thorpej 	kntrack->kn_kevent.udata = okn->kn_kevent.udata; /* preserve udata */
   1121  1.129   thorpej 
   1122  1.129   thorpej 	/*
   1123  1.129   thorpej 	 * The child note does not need to be attached to the
   1124  1.129   thorpej 	 * new proc's klist at all.
   1125  1.129   thorpej 	 */
   1126  1.129   thorpej 	*knchild = *kntrack;
   1127  1.129   thorpej 	knchild->kn_status = KN_DETACHED;
   1128  1.129   thorpej 	knchild->kn_sfflags = 0;
   1129  1.129   thorpej 	knchild->kn_kevent.flags |= EV_ONESHOT;
   1130  1.129   thorpej 	knchild->kn_kevent.fflags = NOTE_CHILD;
   1131  1.129   thorpej 	knchild->kn_kevent.data = p1->p_pid;		 /* parent */
   1132  1.129   thorpej 
   1133  1.129   thorpej 	mutex_enter(&fdp->fd_lock);
   1134  1.129   thorpej 
   1135  1.129   thorpej 	/*
   1136  1.129   thorpej 	 * We need to check to see if the kq is closing, and skip
   1137  1.129   thorpej 	 * attaching the knote if so.  Normally, this isn't necessary
   1138  1.129   thorpej 	 * when coming in the front door because the file descriptor
   1139  1.129   thorpej 	 * layer will synchronize this.
   1140  1.129   thorpej 	 *
   1141  1.129   thorpej 	 * It's safe to test KQ_CLOSING without taking the kq_lock
   1142  1.129   thorpej 	 * here because that flag is only ever set when the fd_lock
   1143  1.129   thorpej 	 * is also held.
   1144  1.129   thorpej 	 */
   1145  1.129   thorpej 	if (__predict_false(kq->kq_count & KQ_CLOSING)) {
   1146  1.129   thorpej 		mutex_exit(&fdp->fd_lock);
   1147  1.129   thorpej 		goto out;
   1148    1.1     lukem 	}
   1149    1.1     lukem 
   1150  1.129   thorpej 	/*
   1151  1.129   thorpej 	 * We do the "insert into FD table" and "attach to klist" steps
   1152  1.129   thorpej 	 * in the opposite order of kqueue_register() here to avoid
   1153  1.129   thorpej 	 * having to take p2->p_lock twice.  But this is OK because we
   1154  1.129   thorpej 	 * hold fd_lock across the entire operation.
   1155  1.129   thorpej 	 */
   1156  1.129   thorpej 
   1157  1.129   thorpej 	mutex_enter(p2->p_lock);
   1158  1.129   thorpej 	error = kauth_authorize_process(curlwp->l_cred,
   1159  1.129   thorpej 	    KAUTH_PROCESS_KEVENT_FILTER, p2, NULL, NULL, NULL);
   1160  1.129   thorpej 	if (__predict_false(error != 0)) {
   1161  1.129   thorpej 		mutex_exit(p2->p_lock);
   1162  1.129   thorpej 		mutex_exit(&fdp->fd_lock);
   1163  1.129   thorpej 		error = EACCES;
   1164  1.129   thorpej 		goto out;
   1165  1.129   thorpej 	}
   1166  1.140   thorpej 	klist_insert(&p2->p_klist, kntrack);
   1167  1.129   thorpej 	mutex_exit(p2->p_lock);
   1168  1.129   thorpej 
   1169  1.129   thorpej 	KASSERT(fdp->fd_knhashmask != 0);
   1170  1.129   thorpej 	KASSERT(fdp->fd_knhash != NULL);
   1171  1.129   thorpej 	struct klist *list = &fdp->fd_knhash[KN_HASH(kntrack->kn_id,
   1172  1.129   thorpej 	    fdp->fd_knhashmask)];
   1173  1.129   thorpej 	SLIST_INSERT_HEAD(list, kntrack, kn_link);
   1174  1.129   thorpej 	SLIST_INSERT_HEAD(list, knchild, kn_link);
   1175  1.129   thorpej 
   1176  1.129   thorpej 	/* This adds references for knchild *and* kntrack. */
   1177  1.129   thorpej 	atomic_add_int(&kntrack->kn_kfilter->refcnt, 2);
   1178  1.129   thorpej 
   1179  1.129   thorpej 	knote_activate(knchild);
   1180  1.129   thorpej 
   1181  1.129   thorpej 	kntrack = NULL;
   1182  1.129   thorpej 	knchild = NULL;
   1183  1.129   thorpej 
   1184  1.129   thorpej 	mutex_exit(&fdp->fd_lock);
   1185  1.129   thorpej 
   1186  1.129   thorpej  out:
   1187  1.129   thorpej 	if (__predict_false(knchild != NULL)) {
   1188  1.142   thorpej 		knote_free(knchild);
   1189  1.129   thorpej 	}
   1190  1.129   thorpej 	if (__predict_false(kntrack != NULL)) {
   1191  1.142   thorpej 		knote_free(kntrack);
   1192  1.129   thorpej 	}
   1193  1.129   thorpej 	mutex_enter(p1->p_lock);
   1194   1.49        ad 	mutex_spin_enter(&kq->kq_lock);
   1195  1.129   thorpej 
   1196  1.129   thorpej 	if (kn_leave_flux(okn)) {
   1197  1.129   thorpej 		KQ_FLUX_WAKEUP(kq);
   1198  1.129   thorpej 	}
   1199  1.129   thorpej 
   1200  1.129   thorpej 	return error;
   1201  1.129   thorpej }
   1202  1.129   thorpej 
   1203  1.129   thorpej void
   1204  1.129   thorpej knote_proc_fork(struct proc *p1, struct proc *p2)
   1205  1.129   thorpej {
   1206  1.129   thorpej 	struct knote *kn;
   1207  1.129   thorpej 	struct kqueue *kq;
   1208  1.129   thorpej 	uint32_t fflags;
   1209  1.129   thorpej 
   1210  1.129   thorpej 	mutex_enter(p1->p_lock);
   1211  1.129   thorpej 
   1212  1.129   thorpej 	/*
   1213  1.129   thorpej 	 * N.B. We DO NOT use SLIST_FOREACH_SAFE() here because we
   1214  1.129   thorpej 	 * don't want to pre-fetch the next knote; in the event we
   1215  1.129   thorpej 	 * have to drop p_lock, we will have put the knote in-flux,
   1216  1.129   thorpej 	 * meaning that no one will be able to detach it until we
   1217  1.129   thorpej 	 * have taken the knote out of flux.  However, that does
   1218  1.129   thorpej 	 * NOT stop someone else from detaching the next note in the
   1219  1.129   thorpej 	 * list while we have it unlocked.  Thus, we want to fetch
   1220  1.129   thorpej 	 * the next note in the list only after we have re-acquired
   1221  1.129   thorpej 	 * the lock, and using SLIST_FOREACH() will satisfy that.
   1222  1.129   thorpej 	 */
   1223  1.129   thorpej 	SLIST_FOREACH(kn, &p1->p_klist, kn_selnext) {
   1224  1.129   thorpej 		/* N.B. EVFILT_SIGNAL knotes are on this same list. */
   1225  1.129   thorpej 		if (kn->kn_fop == &sig_filtops) {
   1226  1.129   thorpej 			continue;
   1227  1.129   thorpej 		}
   1228  1.129   thorpej 		KASSERT(kn->kn_fop == &proc_filtops);
   1229  1.129   thorpej 
   1230  1.129   thorpej 		kq = kn->kn_kq;
   1231  1.129   thorpej 		mutex_spin_enter(&kq->kq_lock);
   1232  1.129   thorpej 		kn->kn_fflags |= (kn->kn_sfflags & NOTE_FORK);
   1233  1.129   thorpej 		if (__predict_false(kn->kn_sfflags & NOTE_TRACK)) {
   1234  1.129   thorpej 			/*
   1235  1.129   thorpej 			 * This will drop kq_lock and p_lock and
   1236  1.129   thorpej 			 * re-acquire them before it returns.
   1237  1.129   thorpej 			 */
   1238  1.129   thorpej 			if (knote_proc_fork_track(p1, p2, kn)) {
   1239  1.129   thorpej 				kn->kn_fflags |= NOTE_TRACKERR;
   1240  1.129   thorpej 			}
   1241  1.129   thorpej 			KASSERT(mutex_owned(p1->p_lock));
   1242  1.129   thorpej 			KASSERT(mutex_owned(&kq->kq_lock));
   1243  1.129   thorpej 		}
   1244  1.129   thorpej 		fflags = kn->kn_fflags;
   1245  1.129   thorpej 		if (fflags) {
   1246  1.133   thorpej 			knote_activate_locked(kn);
   1247  1.129   thorpej 		}
   1248  1.133   thorpej 		mutex_spin_exit(&kq->kq_lock);
   1249  1.129   thorpej 	}
   1250  1.129   thorpej 
   1251  1.129   thorpej 	mutex_exit(p1->p_lock);
   1252  1.129   thorpej }
   1253  1.129   thorpej 
   1254  1.129   thorpej void
   1255  1.129   thorpej knote_proc_exit(struct proc *p)
   1256  1.129   thorpej {
   1257  1.129   thorpej 	struct knote *kn;
   1258  1.129   thorpej 	struct kqueue *kq;
   1259  1.129   thorpej 
   1260  1.129   thorpej 	KASSERT(mutex_owned(p->p_lock));
   1261  1.129   thorpej 
   1262  1.129   thorpej 	while (!SLIST_EMPTY(&p->p_klist)) {
   1263  1.129   thorpej 		kn = SLIST_FIRST(&p->p_klist);
   1264  1.129   thorpej 		kq = kn->kn_kq;
   1265  1.129   thorpej 
   1266  1.129   thorpej 		KASSERT(kn->kn_obj == p);
   1267  1.129   thorpej 
   1268  1.129   thorpej 		mutex_spin_enter(&kq->kq_lock);
   1269  1.129   thorpej 		kn->kn_data = P_WAITSTATUS(p);
   1270  1.129   thorpej 		/*
   1271  1.129   thorpej 		 * Mark as ONESHOT, so that the knote is g/c'ed
   1272  1.129   thorpej 		 * when read.
   1273  1.129   thorpej 		 */
   1274  1.129   thorpej 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
   1275  1.129   thorpej 		kn->kn_fflags |= kn->kn_sfflags & NOTE_EXIT;
   1276  1.129   thorpej 
   1277    1.1     lukem 		/*
   1278  1.129   thorpej 		 * Detach the knote from the process and mark it as such.
   1279  1.129   thorpej 		 * N.B. EVFILT_SIGNAL are also on p_klist, but by the
   1280  1.129   thorpej 		 * time we get here, all open file descriptors for this
   1281  1.129   thorpej 		 * process have been released, meaning that signal knotes
   1282  1.129   thorpej 		 * will have already been detached.
   1283  1.129   thorpej 		 *
   1284  1.129   thorpej 		 * We need to synchronize this with filt_procdetach().
   1285    1.1     lukem 		 */
   1286  1.129   thorpej 		KASSERT(kn->kn_fop == &proc_filtops);
   1287  1.129   thorpej 		if ((kn->kn_status & KN_DETACHED) == 0) {
   1288  1.129   thorpej 			kn->kn_status |= KN_DETACHED;
   1289  1.129   thorpej 			SLIST_REMOVE_HEAD(&p->p_klist, kn_selnext);
   1290  1.129   thorpej 		}
   1291  1.129   thorpej 
   1292  1.129   thorpej 		/*
   1293  1.129   thorpej 		 * Always activate the knote for NOTE_EXIT regardless
   1294  1.129   thorpej 		 * of whether or not the listener cares about it.
   1295  1.129   thorpej 		 * This matches historical behavior.
   1296  1.129   thorpej 		 */
   1297  1.133   thorpej 		knote_activate_locked(kn);
   1298  1.133   thorpej 		mutex_spin_exit(&kq->kq_lock);
   1299    1.1     lukem 	}
   1300    1.8  jdolecek }
   1301    1.8  jdolecek 
   1302  1.133   thorpej #define	FILT_TIMER_NOSCHED	((uintptr_t)-1)
   1303  1.133   thorpej 
   1304    1.8  jdolecek static int
   1305  1.134   thorpej filt_timercompute(struct kevent *kev, uintptr_t *tticksp)
   1306    1.8  jdolecek {
   1307  1.132   thorpej 	struct timespec ts;
   1308  1.134   thorpej 	uintptr_t tticks;
   1309  1.132   thorpej 
   1310  1.134   thorpej 	if (kev->fflags & ~(NOTE_TIMER_UNITMASK | NOTE_ABSTIME)) {
   1311  1.132   thorpej 		return EINVAL;
   1312  1.132   thorpej 	}
   1313  1.132   thorpej 
   1314  1.132   thorpej 	/*
   1315  1.132   thorpej 	 * Convert the event 'data' to a timespec, then convert the
   1316  1.132   thorpej 	 * timespec to callout ticks.
   1317  1.132   thorpej 	 */
   1318  1.134   thorpej 	switch (kev->fflags & NOTE_TIMER_UNITMASK) {
   1319  1.132   thorpej 	case NOTE_SECONDS:
   1320  1.134   thorpej 		ts.tv_sec = kev->data;
   1321  1.132   thorpej 		ts.tv_nsec = 0;
   1322  1.132   thorpej 		break;
   1323  1.132   thorpej 
   1324  1.132   thorpej 	case NOTE_MSECONDS:		/* == historical value 0 */
   1325  1.134   thorpej 		ts.tv_sec = kev->data / 1000;
   1326  1.134   thorpej 		ts.tv_nsec = (kev->data % 1000) * 1000000;
   1327  1.132   thorpej 		break;
   1328  1.132   thorpej 
   1329  1.132   thorpej 	case NOTE_USECONDS:
   1330  1.134   thorpej 		ts.tv_sec = kev->data / 1000000;
   1331  1.134   thorpej 		ts.tv_nsec = (kev->data % 1000000) * 1000;
   1332  1.132   thorpej 		break;
   1333  1.132   thorpej 
   1334  1.132   thorpej 	case NOTE_NSECONDS:
   1335  1.134   thorpej 		ts.tv_sec = kev->data / 1000000000;
   1336  1.134   thorpej 		ts.tv_nsec = kev->data % 1000000000;
   1337  1.132   thorpej 		break;
   1338  1.132   thorpej 
   1339  1.132   thorpej 	default:
   1340  1.132   thorpej 		return EINVAL;
   1341  1.132   thorpej 	}
   1342  1.132   thorpej 
   1343  1.134   thorpej 	if (kev->fflags & NOTE_ABSTIME) {
   1344  1.132   thorpej 		struct timespec deadline = ts;
   1345  1.132   thorpej 
   1346  1.132   thorpej 		/*
   1347  1.132   thorpej 		 * Get current time.
   1348  1.132   thorpej 		 *
   1349  1.132   thorpej 		 * XXX This is CLOCK_REALTIME.  There is no way to
   1350  1.132   thorpej 		 * XXX specify CLOCK_MONOTONIC.
   1351  1.132   thorpej 		 */
   1352  1.132   thorpej 		nanotime(&ts);
   1353    1.8  jdolecek 
   1354  1.134   thorpej 		/* Absolute timers do not repeat. */
   1355  1.134   thorpej 		kev->data = FILT_TIMER_NOSCHED;
   1356  1.134   thorpej 
   1357  1.132   thorpej 		/* If we're past the deadline, then the event will fire. */
   1358  1.132   thorpej 		if (timespeccmp(&deadline, &ts, <=)) {
   1359  1.134   thorpej 			tticks = FILT_TIMER_NOSCHED;
   1360  1.134   thorpej 			goto out;
   1361  1.132   thorpej 		}
   1362  1.132   thorpej 
   1363  1.132   thorpej 		/* Calculate how much time is left. */
   1364  1.132   thorpej 		timespecsub(&deadline, &ts, &ts);
   1365  1.132   thorpej 	} else {
   1366  1.132   thorpej 		/* EV_CLEAR automatically set for relative timers. */
   1367  1.134   thorpej 		kev->flags |= EV_CLEAR;
   1368  1.132   thorpej 	}
   1369  1.132   thorpej 
   1370  1.132   thorpej 	tticks = tstohz(&ts);
   1371    1.8  jdolecek 
   1372    1.8  jdolecek 	/* if the supplied value is under our resolution, use 1 tick */
   1373    1.8  jdolecek 	if (tticks == 0) {
   1374  1.134   thorpej 		if (kev->data == 0)
   1375   1.49        ad 			return EINVAL;
   1376    1.8  jdolecek 		tticks = 1;
   1377  1.134   thorpej 	} else if (tticks > INT_MAX) {
   1378  1.134   thorpej 		return EINVAL;
   1379    1.8  jdolecek 	}
   1380    1.8  jdolecek 
   1381  1.134   thorpej 	if ((kev->flags & EV_ONESHOT) != 0) {
   1382  1.132   thorpej 		/* Timer does not repeat. */
   1383  1.134   thorpej 		kev->data = FILT_TIMER_NOSCHED;
   1384  1.132   thorpej 	} else {
   1385  1.133   thorpej 		KASSERT((uintptr_t)tticks != FILT_TIMER_NOSCHED);
   1386  1.134   thorpej 		kev->data = tticks;
   1387  1.134   thorpej 	}
   1388  1.134   thorpej 
   1389  1.134   thorpej  out:
   1390  1.134   thorpej 	*tticksp = tticks;
   1391  1.134   thorpej 
   1392  1.134   thorpej 	return 0;
   1393  1.134   thorpej }
   1394  1.134   thorpej 
   1395  1.134   thorpej static void
   1396  1.134   thorpej filt_timerexpire(void *knx)
   1397  1.134   thorpej {
   1398  1.134   thorpej 	struct knote *kn = knx;
   1399  1.134   thorpej 	struct kqueue *kq = kn->kn_kq;
   1400  1.134   thorpej 
   1401  1.134   thorpej 	mutex_spin_enter(&kq->kq_lock);
   1402  1.134   thorpej 	kn->kn_data++;
   1403  1.134   thorpej 	knote_activate_locked(kn);
   1404  1.134   thorpej 	if (kn->kn_sdata != FILT_TIMER_NOSCHED) {
   1405  1.147  riastrad 		KASSERT(kn->kn_sdata > 0);
   1406  1.147  riastrad 		KASSERT(kn->kn_sdata <= INT_MAX);
   1407  1.134   thorpej 		callout_schedule((callout_t *)kn->kn_hook,
   1408  1.134   thorpej 		    (int)kn->kn_sdata);
   1409  1.134   thorpej 	}
   1410  1.134   thorpej 	mutex_spin_exit(&kq->kq_lock);
   1411  1.134   thorpej }
   1412  1.134   thorpej 
   1413  1.136   thorpej static inline void
   1414  1.136   thorpej filt_timerstart(struct knote *kn, uintptr_t tticks)
   1415  1.136   thorpej {
   1416  1.136   thorpej 	callout_t *calloutp = kn->kn_hook;
   1417  1.136   thorpej 
   1418  1.136   thorpej 	KASSERT(mutex_owned(&kn->kn_kq->kq_lock));
   1419  1.136   thorpej 	KASSERT(!callout_pending(calloutp));
   1420  1.136   thorpej 
   1421  1.136   thorpej 	if (__predict_false(tticks == FILT_TIMER_NOSCHED)) {
   1422  1.136   thorpej 		kn->kn_data = 1;
   1423  1.136   thorpej 	} else {
   1424  1.136   thorpej 		KASSERT(tticks <= INT_MAX);
   1425  1.136   thorpej 		callout_reset(calloutp, (int)tticks, filt_timerexpire, kn);
   1426  1.136   thorpej 	}
   1427  1.136   thorpej }
   1428  1.136   thorpej 
   1429  1.134   thorpej static int
   1430  1.134   thorpej filt_timerattach(struct knote *kn)
   1431  1.134   thorpej {
   1432  1.134   thorpej 	callout_t *calloutp;
   1433  1.134   thorpej 	struct kqueue *kq;
   1434  1.134   thorpej 	uintptr_t tticks;
   1435  1.134   thorpej 	int error;
   1436  1.134   thorpej 
   1437  1.134   thorpej 	struct kevent kev = {
   1438  1.134   thorpej 		.flags = kn->kn_flags,
   1439  1.134   thorpej 		.fflags = kn->kn_sfflags,
   1440  1.134   thorpej 		.data = kn->kn_sdata,
   1441  1.134   thorpej 	};
   1442  1.134   thorpej 
   1443  1.134   thorpej 	error = filt_timercompute(&kev, &tticks);
   1444  1.134   thorpej 	if (error) {
   1445  1.134   thorpej 		return error;
   1446  1.132   thorpej 	}
   1447  1.132   thorpej 
   1448   1.49        ad 	if (atomic_inc_uint_nv(&kq_ncallouts) >= kq_calloutmax ||
   1449   1.49        ad 	    (calloutp = kmem_alloc(sizeof(*calloutp), KM_NOSLEEP)) == NULL) {
   1450   1.49        ad 		atomic_dec_uint(&kq_ncallouts);
   1451   1.49        ad 		return ENOMEM;
   1452   1.49        ad 	}
   1453   1.54        ad 	callout_init(calloutp, CALLOUT_MPSAFE);
   1454   1.49        ad 
   1455   1.49        ad 	kq = kn->kn_kq;
   1456   1.49        ad 	mutex_spin_enter(&kq->kq_lock);
   1457  1.134   thorpej 
   1458  1.134   thorpej 	kn->kn_sdata = kev.data;
   1459  1.134   thorpej 	kn->kn_flags = kev.flags;
   1460  1.134   thorpej 	KASSERT(kn->kn_sfflags == kev.fflags);
   1461   1.49        ad 	kn->kn_hook = calloutp;
   1462  1.134   thorpej 
   1463  1.136   thorpej 	filt_timerstart(kn, tticks);
   1464  1.134   thorpej 
   1465   1.49        ad 	mutex_spin_exit(&kq->kq_lock);
   1466   1.49        ad 
   1467    1.8  jdolecek 	return (0);
   1468    1.8  jdolecek }
   1469    1.8  jdolecek 
   1470    1.8  jdolecek static void
   1471    1.8  jdolecek filt_timerdetach(struct knote *kn)
   1472    1.8  jdolecek {
   1473   1.39        ad 	callout_t *calloutp;
   1474  1.103  christos 	struct kqueue *kq = kn->kn_kq;
   1475  1.103  christos 
   1476  1.133   thorpej 	/* prevent rescheduling when we expire */
   1477  1.103  christos 	mutex_spin_enter(&kq->kq_lock);
   1478  1.133   thorpej 	kn->kn_sdata = FILT_TIMER_NOSCHED;
   1479  1.103  christos 	mutex_spin_exit(&kq->kq_lock);
   1480    1.8  jdolecek 
   1481   1.39        ad 	calloutp = (callout_t *)kn->kn_hook;
   1482  1.125   thorpej 
   1483  1.125   thorpej 	/*
   1484  1.125   thorpej 	 * Attempt to stop the callout.  This will block if it's
   1485  1.125   thorpej 	 * already running.
   1486  1.125   thorpej 	 */
   1487   1.55        ad 	callout_halt(calloutp, NULL);
   1488  1.125   thorpej 
   1489   1.39        ad 	callout_destroy(calloutp);
   1490   1.49        ad 	kmem_free(calloutp, sizeof(*calloutp));
   1491   1.49        ad 	atomic_dec_uint(&kq_ncallouts);
   1492    1.8  jdolecek }
   1493    1.8  jdolecek 
   1494    1.8  jdolecek static int
   1495  1.136   thorpej filt_timertouch(struct knote *kn, struct kevent *kev, long type)
   1496  1.136   thorpej {
   1497  1.136   thorpej 	struct kqueue *kq = kn->kn_kq;
   1498  1.136   thorpej 	callout_t *calloutp;
   1499  1.136   thorpej 	uintptr_t tticks;
   1500  1.136   thorpej 	int error;
   1501  1.136   thorpej 
   1502  1.136   thorpej 	KASSERT(mutex_owned(&kq->kq_lock));
   1503  1.136   thorpej 
   1504  1.136   thorpej 	switch (type) {
   1505  1.136   thorpej 	case EVENT_REGISTER:
   1506  1.136   thorpej 		/* Only relevant for EV_ADD. */
   1507  1.136   thorpej 		if ((kev->flags & EV_ADD) == 0) {
   1508  1.136   thorpej 			return 0;
   1509  1.136   thorpej 		}
   1510  1.136   thorpej 
   1511  1.136   thorpej 		/*
   1512  1.136   thorpej 		 * Stop the timer, under the assumption that if
   1513  1.136   thorpej 		 * an application is re-configuring the timer,
   1514  1.136   thorpej 		 * they no longer care about the old one.  We
   1515  1.136   thorpej 		 * can safely drop the kq_lock while we wait
   1516  1.136   thorpej 		 * because fdp->fd_lock will be held throughout,
   1517  1.136   thorpej 		 * ensuring that no one can sneak in with an
   1518  1.136   thorpej 		 * EV_DELETE or close the kq.
   1519  1.136   thorpej 		 */
   1520  1.136   thorpej 		KASSERT(mutex_owned(&kq->kq_fdp->fd_lock));
   1521  1.136   thorpej 
   1522  1.136   thorpej 		calloutp = kn->kn_hook;
   1523  1.136   thorpej 		callout_halt(calloutp, &kq->kq_lock);
   1524  1.136   thorpej 		KASSERT(mutex_owned(&kq->kq_lock));
   1525  1.136   thorpej 		knote_deactivate_locked(kn);
   1526  1.136   thorpej 		kn->kn_data = 0;
   1527  1.136   thorpej 
   1528  1.136   thorpej 		error = filt_timercompute(kev, &tticks);
   1529  1.136   thorpej 		if (error) {
   1530  1.136   thorpej 			return error;
   1531  1.136   thorpej 		}
   1532  1.136   thorpej 		kn->kn_sdata = kev->data;
   1533  1.136   thorpej 		kn->kn_flags = kev->flags;
   1534  1.136   thorpej 		kn->kn_sfflags = kev->fflags;
   1535  1.136   thorpej 		filt_timerstart(kn, tticks);
   1536  1.136   thorpej 		break;
   1537  1.136   thorpej 
   1538  1.136   thorpej 	case EVENT_PROCESS:
   1539  1.136   thorpej 		*kev = kn->kn_kevent;
   1540  1.136   thorpej 		break;
   1541  1.136   thorpej 
   1542  1.136   thorpej 	default:
   1543  1.136   thorpej 		panic("%s: invalid type (%ld)", __func__, type);
   1544  1.136   thorpej 	}
   1545  1.136   thorpej 
   1546  1.136   thorpej 	return 0;
   1547  1.136   thorpej }
   1548  1.136   thorpej 
   1549  1.136   thorpej static int
   1550   1.33      yamt filt_timer(struct knote *kn, long hint)
   1551    1.8  jdolecek {
   1552  1.133   thorpej 	struct kqueue *kq = kn->kn_kq;
   1553   1.49        ad 	int rv;
   1554   1.49        ad 
   1555  1.133   thorpej 	mutex_spin_enter(&kq->kq_lock);
   1556   1.49        ad 	rv = (kn->kn_data != 0);
   1557  1.133   thorpej 	mutex_spin_exit(&kq->kq_lock);
   1558   1.49        ad 
   1559   1.49        ad 	return rv;
   1560    1.1     lukem }
   1561    1.1     lukem 
   1562  1.108  christos static int
   1563  1.108  christos filt_userattach(struct knote *kn)
   1564  1.108  christos {
   1565  1.108  christos 	struct kqueue *kq = kn->kn_kq;
   1566  1.108  christos 
   1567  1.108  christos 	/*
   1568  1.108  christos 	 * EVFILT_USER knotes are not attached to anything in the kernel.
   1569  1.108  christos 	 */
   1570  1.108  christos 	mutex_spin_enter(&kq->kq_lock);
   1571  1.108  christos 	kn->kn_hook = NULL;
   1572  1.108  christos 	if (kn->kn_fflags & NOTE_TRIGGER)
   1573  1.108  christos 		kn->kn_hookid = 1;
   1574  1.108  christos 	else
   1575  1.108  christos 		kn->kn_hookid = 0;
   1576  1.108  christos 	mutex_spin_exit(&kq->kq_lock);
   1577  1.108  christos 	return (0);
   1578  1.108  christos }
   1579  1.108  christos 
   1580  1.108  christos static void
   1581  1.108  christos filt_userdetach(struct knote *kn)
   1582  1.108  christos {
   1583  1.108  christos 
   1584  1.108  christos 	/*
   1585  1.108  christos 	 * EVFILT_USER knotes are not attached to anything in the kernel.
   1586  1.108  christos 	 */
   1587  1.108  christos }
   1588  1.108  christos 
   1589  1.108  christos static int
   1590  1.108  christos filt_user(struct knote *kn, long hint)
   1591  1.108  christos {
   1592  1.108  christos 	struct kqueue *kq = kn->kn_kq;
   1593  1.108  christos 	int hookid;
   1594  1.108  christos 
   1595  1.108  christos 	mutex_spin_enter(&kq->kq_lock);
   1596  1.108  christos 	hookid = kn->kn_hookid;
   1597  1.108  christos 	mutex_spin_exit(&kq->kq_lock);
   1598  1.108  christos 
   1599  1.108  christos 	return hookid;
   1600  1.108  christos }
   1601  1.108  christos 
   1602  1.135   thorpej static int
   1603  1.108  christos filt_usertouch(struct knote *kn, struct kevent *kev, long type)
   1604  1.108  christos {
   1605  1.108  christos 	int ffctrl;
   1606  1.108  christos 
   1607  1.117     skrll 	KASSERT(mutex_owned(&kn->kn_kq->kq_lock));
   1608  1.116  jdolecek 
   1609  1.108  christos 	switch (type) {
   1610  1.108  christos 	case EVENT_REGISTER:
   1611  1.108  christos 		if (kev->fflags & NOTE_TRIGGER)
   1612  1.108  christos 			kn->kn_hookid = 1;
   1613  1.108  christos 
   1614  1.108  christos 		ffctrl = kev->fflags & NOTE_FFCTRLMASK;
   1615  1.108  christos 		kev->fflags &= NOTE_FFLAGSMASK;
   1616  1.108  christos 		switch (ffctrl) {
   1617  1.108  christos 		case NOTE_FFNOP:
   1618  1.108  christos 			break;
   1619  1.108  christos 
   1620  1.108  christos 		case NOTE_FFAND:
   1621  1.108  christos 			kn->kn_sfflags &= kev->fflags;
   1622  1.108  christos 			break;
   1623  1.108  christos 
   1624  1.108  christos 		case NOTE_FFOR:
   1625  1.108  christos 			kn->kn_sfflags |= kev->fflags;
   1626  1.108  christos 			break;
   1627  1.108  christos 
   1628  1.108  christos 		case NOTE_FFCOPY:
   1629  1.108  christos 			kn->kn_sfflags = kev->fflags;
   1630  1.108  christos 			break;
   1631  1.108  christos 
   1632  1.108  christos 		default:
   1633  1.108  christos 			/* XXX Return error? */
   1634  1.108  christos 			break;
   1635  1.108  christos 		}
   1636  1.108  christos 		kn->kn_sdata = kev->data;
   1637  1.108  christos 		if (kev->flags & EV_CLEAR) {
   1638  1.108  christos 			kn->kn_hookid = 0;
   1639  1.108  christos 			kn->kn_data = 0;
   1640  1.108  christos 			kn->kn_fflags = 0;
   1641  1.108  christos 		}
   1642  1.108  christos 		break;
   1643  1.108  christos 
   1644  1.108  christos 	case EVENT_PROCESS:
   1645  1.108  christos 		*kev = kn->kn_kevent;
   1646  1.108  christos 		kev->fflags = kn->kn_sfflags;
   1647  1.108  christos 		kev->data = kn->kn_sdata;
   1648  1.108  christos 		if (kn->kn_flags & EV_CLEAR) {
   1649  1.108  christos 			kn->kn_hookid = 0;
   1650  1.108  christos 			kn->kn_data = 0;
   1651  1.108  christos 			kn->kn_fflags = 0;
   1652  1.108  christos 		}
   1653  1.108  christos 		break;
   1654  1.108  christos 
   1655  1.108  christos 	default:
   1656  1.108  christos 		panic("filt_usertouch() - invalid type (%ld)", type);
   1657  1.108  christos 		break;
   1658  1.108  christos 	}
   1659  1.135   thorpej 
   1660  1.135   thorpej 	return 0;
   1661  1.108  christos }
   1662  1.108  christos 
   1663  1.102  christos /*
   1664    1.3  jdolecek  * filt_seltrue:
   1665    1.3  jdolecek  *
   1666    1.3  jdolecek  *	This filter "event" routine simulates seltrue().
   1667    1.3  jdolecek  */
   1668    1.1     lukem int
   1669   1.33      yamt filt_seltrue(struct knote *kn, long hint)
   1670    1.1     lukem {
   1671    1.1     lukem 
   1672    1.3  jdolecek 	/*
   1673    1.3  jdolecek 	 * We don't know how much data can be read/written,
   1674    1.3  jdolecek 	 * but we know that it *can* be.  This is about as
   1675    1.3  jdolecek 	 * good as select/poll does as well.
   1676    1.3  jdolecek 	 */
   1677    1.3  jdolecek 	kn->kn_data = 0;
   1678    1.3  jdolecek 	return (1);
   1679    1.3  jdolecek }
   1680    1.3  jdolecek 
   1681    1.3  jdolecek /*
   1682    1.3  jdolecek  * This provides full kqfilter entry for device switch tables, which
   1683    1.3  jdolecek  * has same effect as filter using filt_seltrue() as filter method.
   1684    1.3  jdolecek  */
   1685    1.3  jdolecek static void
   1686   1.33      yamt filt_seltruedetach(struct knote *kn)
   1687    1.3  jdolecek {
   1688    1.3  jdolecek 	/* Nothing to do */
   1689    1.3  jdolecek }
   1690    1.3  jdolecek 
   1691   1.96      maya const struct filterops seltrue_filtops = {
   1692  1.123   thorpej 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
   1693   1.96      maya 	.f_attach = NULL,
   1694   1.96      maya 	.f_detach = filt_seltruedetach,
   1695   1.96      maya 	.f_event = filt_seltrue,
   1696   1.96      maya };
   1697    1.3  jdolecek 
   1698    1.3  jdolecek int
   1699   1.33      yamt seltrue_kqfilter(dev_t dev, struct knote *kn)
   1700    1.3  jdolecek {
   1701    1.3  jdolecek 	switch (kn->kn_filter) {
   1702    1.3  jdolecek 	case EVFILT_READ:
   1703    1.3  jdolecek 	case EVFILT_WRITE:
   1704    1.3  jdolecek 		kn->kn_fop = &seltrue_filtops;
   1705    1.3  jdolecek 		break;
   1706    1.3  jdolecek 	default:
   1707   1.43     pooka 		return (EINVAL);
   1708    1.3  jdolecek 	}
   1709    1.3  jdolecek 
   1710    1.3  jdolecek 	/* Nothing more to do */
   1711    1.3  jdolecek 	return (0);
   1712    1.3  jdolecek }
   1713    1.3  jdolecek 
   1714    1.3  jdolecek /*
   1715    1.3  jdolecek  * kqueue(2) system call.
   1716    1.3  jdolecek  */
   1717   1.72  christos static int
   1718   1.72  christos kqueue1(struct lwp *l, int flags, register_t *retval)
   1719    1.3  jdolecek {
   1720   1.49        ad 	struct kqueue *kq;
   1721   1.49        ad 	file_t *fp;
   1722   1.49        ad 	int fd, error;
   1723    1.3  jdolecek 
   1724   1.49        ad 	if ((error = fd_allocfile(&fp, &fd)) != 0)
   1725   1.49        ad 		return error;
   1726   1.75  christos 	fp->f_flag = FREAD | FWRITE | (flags & (FNONBLOCK|FNOSIGPIPE));
   1727    1.1     lukem 	fp->f_type = DTYPE_KQUEUE;
   1728    1.1     lukem 	fp->f_ops = &kqueueops;
   1729   1.49        ad 	kq = kmem_zalloc(sizeof(*kq), KM_SLEEP);
   1730   1.49        ad 	mutex_init(&kq->kq_lock, MUTEX_DEFAULT, IPL_SCHED);
   1731   1.49        ad 	cv_init(&kq->kq_cv, "kqueue");
   1732   1.49        ad 	selinit(&kq->kq_sel);
   1733    1.1     lukem 	TAILQ_INIT(&kq->kq_head);
   1734   1.82      matt 	fp->f_kqueue = kq;
   1735    1.3  jdolecek 	*retval = fd;
   1736   1.49        ad 	kq->kq_fdp = curlwp->l_fd;
   1737   1.72  christos 	fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
   1738   1.49        ad 	fd_affix(curproc, fp, fd);
   1739   1.49        ad 	return error;
   1740    1.1     lukem }
   1741    1.1     lukem 
   1742    1.3  jdolecek /*
   1743   1.72  christos  * kqueue(2) system call.
   1744   1.72  christos  */
   1745   1.72  christos int
   1746   1.72  christos sys_kqueue(struct lwp *l, const void *v, register_t *retval)
   1747   1.72  christos {
   1748   1.72  christos 	return kqueue1(l, 0, retval);
   1749   1.72  christos }
   1750   1.72  christos 
   1751   1.72  christos int
   1752   1.72  christos sys_kqueue1(struct lwp *l, const struct sys_kqueue1_args *uap,
   1753   1.72  christos     register_t *retval)
   1754   1.72  christos {
   1755   1.72  christos 	/* {
   1756   1.72  christos 		syscallarg(int) flags;
   1757   1.72  christos 	} */
   1758   1.72  christos 	return kqueue1(l, SCARG(uap, flags), retval);
   1759   1.72  christos }
   1760   1.72  christos 
   1761   1.72  christos /*
   1762    1.3  jdolecek  * kevent(2) system call.
   1763    1.3  jdolecek  */
   1764   1.61  christos int
   1765   1.81      matt kevent_fetch_changes(void *ctx, const struct kevent *changelist,
   1766   1.61  christos     struct kevent *changes, size_t index, int n)
   1767   1.24      cube {
   1768   1.49        ad 
   1769   1.24      cube 	return copyin(changelist + index, changes, n * sizeof(*changes));
   1770   1.24      cube }
   1771   1.24      cube 
   1772   1.61  christos int
   1773   1.81      matt kevent_put_events(void *ctx, struct kevent *events,
   1774   1.61  christos     struct kevent *eventlist, size_t index, int n)
   1775   1.24      cube {
   1776   1.49        ad 
   1777   1.24      cube 	return copyout(events, eventlist + index, n * sizeof(*events));
   1778   1.24      cube }
   1779   1.24      cube 
   1780   1.24      cube static const struct kevent_ops kevent_native_ops = {
   1781   1.60  gmcgarry 	.keo_private = NULL,
   1782   1.60  gmcgarry 	.keo_fetch_timeout = copyin,
   1783   1.60  gmcgarry 	.keo_fetch_changes = kevent_fetch_changes,
   1784   1.60  gmcgarry 	.keo_put_events = kevent_put_events,
   1785   1.24      cube };
   1786   1.24      cube 
   1787    1.1     lukem int
   1788  1.149  christos sys___kevent100(struct lwp *l, const struct sys___kevent100_args *uap,
   1789   1.61  christos     register_t *retval)
   1790    1.1     lukem {
   1791   1.44       dsl 	/* {
   1792    1.3  jdolecek 		syscallarg(int) fd;
   1793    1.3  jdolecek 		syscallarg(const struct kevent *) changelist;
   1794    1.3  jdolecek 		syscallarg(size_t) nchanges;
   1795    1.3  jdolecek 		syscallarg(struct kevent *) eventlist;
   1796    1.3  jdolecek 		syscallarg(size_t) nevents;
   1797    1.3  jdolecek 		syscallarg(const struct timespec *) timeout;
   1798   1.44       dsl 	} */
   1799   1.24      cube 
   1800   1.49        ad 	return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
   1801   1.24      cube 	    SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
   1802   1.24      cube 	    SCARG(uap, timeout), &kevent_native_ops);
   1803   1.24      cube }
   1804   1.24      cube 
   1805   1.24      cube int
   1806   1.49        ad kevent1(register_t *retval, int fd,
   1807   1.49        ad 	const struct kevent *changelist, size_t nchanges,
   1808   1.49        ad 	struct kevent *eventlist, size_t nevents,
   1809   1.49        ad 	const struct timespec *timeout,
   1810   1.49        ad 	const struct kevent_ops *keops)
   1811   1.24      cube {
   1812   1.49        ad 	struct kevent *kevp;
   1813   1.49        ad 	struct kqueue *kq;
   1814    1.3  jdolecek 	struct timespec	ts;
   1815   1.49        ad 	size_t i, n, ichange;
   1816   1.49        ad 	int nerrors, error;
   1817   1.80      maxv 	struct kevent kevbuf[KQ_NEVENTS];	/* approx 300 bytes on 64-bit */
   1818   1.49        ad 	file_t *fp;
   1819    1.3  jdolecek 
   1820    1.3  jdolecek 	/* check that we're dealing with a kq */
   1821   1.49        ad 	fp = fd_getfile(fd);
   1822   1.10        pk 	if (fp == NULL)
   1823    1.1     lukem 		return (EBADF);
   1824   1.10        pk 
   1825   1.10        pk 	if (fp->f_type != DTYPE_KQUEUE) {
   1826   1.49        ad 		fd_putfile(fd);
   1827   1.10        pk 		return (EBADF);
   1828   1.10        pk 	}
   1829    1.1     lukem 
   1830   1.24      cube 	if (timeout != NULL) {
   1831   1.24      cube 		error = (*keops->keo_fetch_timeout)(timeout, &ts, sizeof(ts));
   1832    1.1     lukem 		if (error)
   1833    1.1     lukem 			goto done;
   1834   1.24      cube 		timeout = &ts;
   1835    1.1     lukem 	}
   1836    1.1     lukem 
   1837   1.82      matt 	kq = fp->f_kqueue;
   1838    1.1     lukem 	nerrors = 0;
   1839   1.24      cube 	ichange = 0;
   1840    1.1     lukem 
   1841    1.3  jdolecek 	/* traverse list of events to register */
   1842   1.24      cube 	while (nchanges > 0) {
   1843   1.49        ad 		n = MIN(nchanges, __arraycount(kevbuf));
   1844   1.24      cube 		error = (*keops->keo_fetch_changes)(keops->keo_private,
   1845   1.49        ad 		    changelist, kevbuf, ichange, n);
   1846    1.1     lukem 		if (error)
   1847    1.1     lukem 			goto done;
   1848    1.1     lukem 		for (i = 0; i < n; i++) {
   1849   1.49        ad 			kevp = &kevbuf[i];
   1850    1.1     lukem 			kevp->flags &= ~EV_SYSFLAGS;
   1851    1.3  jdolecek 			/* register each knote */
   1852   1.49        ad 			error = kqueue_register(kq, kevp);
   1853   1.89   abhinav 			if (!error && !(kevp->flags & EV_RECEIPT))
   1854   1.89   abhinav 				continue;
   1855   1.89   abhinav 			if (nevents == 0)
   1856   1.89   abhinav 				goto done;
   1857   1.89   abhinav 			kevp->flags = EV_ERROR;
   1858   1.89   abhinav 			kevp->data = error;
   1859   1.89   abhinav 			error = (*keops->keo_put_events)
   1860   1.89   abhinav 				(keops->keo_private, kevp,
   1861   1.89   abhinav 				 eventlist, nerrors, 1);
   1862   1.89   abhinav 			if (error)
   1863   1.89   abhinav 				goto done;
   1864   1.89   abhinav 			nevents--;
   1865   1.89   abhinav 			nerrors++;
   1866    1.1     lukem 		}
   1867   1.24      cube 		nchanges -= n;	/* update the results */
   1868   1.24      cube 		ichange += n;
   1869    1.1     lukem 	}
   1870    1.1     lukem 	if (nerrors) {
   1871    1.3  jdolecek 		*retval = nerrors;
   1872    1.1     lukem 		error = 0;
   1873    1.1     lukem 		goto done;
   1874    1.1     lukem 	}
   1875    1.1     lukem 
   1876    1.3  jdolecek 	/* actually scan through the events */
   1877   1.49        ad 	error = kqueue_scan(fp, nevents, eventlist, timeout, retval, keops,
   1878   1.49        ad 	    kevbuf, __arraycount(kevbuf));
   1879    1.3  jdolecek  done:
   1880   1.49        ad 	fd_putfile(fd);
   1881    1.1     lukem 	return (error);
   1882    1.1     lukem }
   1883    1.1     lukem 
   1884    1.3  jdolecek /*
   1885    1.3  jdolecek  * Register a given kevent kev onto the kqueue
   1886    1.3  jdolecek  */
   1887   1.49        ad static int
   1888   1.49        ad kqueue_register(struct kqueue *kq, struct kevent *kev)
   1889    1.1     lukem {
   1890   1.49        ad 	struct kfilter *kfilter;
   1891   1.49        ad 	filedesc_t *fdp;
   1892   1.49        ad 	file_t *fp;
   1893   1.49        ad 	fdfile_t *ff;
   1894   1.49        ad 	struct knote *kn, *newkn;
   1895   1.49        ad 	struct klist *list;
   1896   1.49        ad 	int error, fd, rv;
   1897    1.3  jdolecek 
   1898    1.3  jdolecek 	fdp = kq->kq_fdp;
   1899    1.3  jdolecek 	fp = NULL;
   1900    1.3  jdolecek 	kn = NULL;
   1901    1.3  jdolecek 	error = 0;
   1902   1.49        ad 	fd = 0;
   1903   1.49        ad 
   1904  1.142   thorpej 	newkn = knote_alloc(true);
   1905   1.49        ad 
   1906   1.49        ad 	rw_enter(&kqueue_filter_lock, RW_READER);
   1907    1.3  jdolecek 	kfilter = kfilter_byfilter(kev->filter);
   1908    1.3  jdolecek 	if (kfilter == NULL || kfilter->filtops == NULL) {
   1909    1.3  jdolecek 		/* filter not found nor implemented */
   1910   1.49        ad 		rw_exit(&kqueue_filter_lock);
   1911  1.142   thorpej 		knote_free(newkn);
   1912    1.1     lukem 		return (EINVAL);
   1913    1.1     lukem 	}
   1914    1.1     lukem 
   1915    1.3  jdolecek 	/* search if knote already exists */
   1916  1.121   thorpej 	if (kfilter->filtops->f_flags & FILTEROP_ISFD) {
   1917    1.3  jdolecek 		/* monitoring a file descriptor */
   1918   1.87  christos 		/* validate descriptor */
   1919   1.88  christos 		if (kev->ident > INT_MAX
   1920   1.88  christos 		    || (fp = fd_getfile(fd = kev->ident)) == NULL) {
   1921   1.49        ad 			rw_exit(&kqueue_filter_lock);
   1922  1.142   thorpej 			knote_free(newkn);
   1923   1.49        ad 			return EBADF;
   1924   1.49        ad 		}
   1925   1.74     rmind 		mutex_enter(&fdp->fd_lock);
   1926   1.65        ad 		ff = fdp->fd_dt->dt_ff[fd];
   1927   1.98  christos 		if (ff->ff_refcnt & FR_CLOSING) {
   1928   1.98  christos 			error = EBADF;
   1929   1.98  christos 			goto doneunlock;
   1930   1.98  christos 		}
   1931   1.49        ad 		if (fd <= fdp->fd_lastkqfile) {
   1932   1.49        ad 			SLIST_FOREACH(kn, &ff->ff_knlist, kn_link) {
   1933    1.1     lukem 				if (kq == kn->kn_kq &&
   1934    1.1     lukem 				    kev->filter == kn->kn_filter)
   1935    1.1     lukem 					break;
   1936   1.49        ad 			}
   1937    1.1     lukem 		}
   1938    1.1     lukem 	} else {
   1939    1.3  jdolecek 		/*
   1940    1.3  jdolecek 		 * not monitoring a file descriptor, so
   1941    1.3  jdolecek 		 * lookup knotes in internal hash table
   1942    1.3  jdolecek 		 */
   1943   1.74     rmind 		mutex_enter(&fdp->fd_lock);
   1944    1.1     lukem 		if (fdp->fd_knhashmask != 0) {
   1945    1.1     lukem 			list = &fdp->fd_knhash[
   1946    1.1     lukem 			    KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
   1947   1.49        ad 			SLIST_FOREACH(kn, list, kn_link) {
   1948    1.1     lukem 				if (kev->ident == kn->kn_id &&
   1949    1.1     lukem 				    kq == kn->kn_kq &&
   1950    1.1     lukem 				    kev->filter == kn->kn_filter)
   1951    1.1     lukem 					break;
   1952   1.49        ad 			}
   1953    1.1     lukem 		}
   1954    1.1     lukem 	}
   1955    1.1     lukem 
   1956  1.129   thorpej 	/* It's safe to test KQ_CLOSING while holding only the fd_lock. */
   1957  1.129   thorpej 	KASSERT(mutex_owned(&fdp->fd_lock));
   1958  1.129   thorpej 	KASSERT((kq->kq_count & KQ_CLOSING) == 0);
   1959  1.129   thorpej 
   1960    1.1     lukem 	/*
   1961    1.1     lukem 	 * kn now contains the matching knote, or NULL if no match
   1962    1.1     lukem 	 */
   1963  1.108  christos 	if (kn == NULL) {
   1964  1.108  christos 		if (kev->flags & EV_ADD) {
   1965    1.3  jdolecek 			/* create new knote */
   1966   1.49        ad 			kn = newkn;
   1967   1.49        ad 			newkn = NULL;
   1968   1.49        ad 			kn->kn_obj = fp;
   1969   1.79  christos 			kn->kn_id = kev->ident;
   1970    1.1     lukem 			kn->kn_kq = kq;
   1971    1.3  jdolecek 			kn->kn_fop = kfilter->filtops;
   1972   1.49        ad 			kn->kn_kfilter = kfilter;
   1973   1.49        ad 			kn->kn_sfflags = kev->fflags;
   1974   1.49        ad 			kn->kn_sdata = kev->data;
   1975   1.49        ad 			kev->fflags = 0;
   1976   1.49        ad 			kev->data = 0;
   1977   1.49        ad 			kn->kn_kevent = *kev;
   1978    1.1     lukem 
   1979   1.85  christos 			KASSERT(kn->kn_fop != NULL);
   1980    1.1     lukem 			/*
   1981  1.145   thorpej 			 * XXX Allow only known-safe users of f_touch.
   1982  1.145   thorpej 			 * XXX See filter_touch() for details.
   1983  1.145   thorpej 			 */
   1984  1.145   thorpej 			if (kn->kn_fop->f_touch != NULL &&
   1985  1.145   thorpej 			    kn->kn_fop != &timer_filtops &&
   1986  1.145   thorpej 			    kn->kn_fop != &user_filtops) {
   1987  1.145   thorpej 				error = ENOTSUP;
   1988  1.145   thorpej 				goto fail_ev_add;
   1989  1.145   thorpej 			}
   1990  1.145   thorpej 
   1991  1.145   thorpej 			/*
   1992    1.1     lukem 			 * apply reference count to knote structure, and
   1993    1.1     lukem 			 * do not release it at the end of this routine.
   1994    1.1     lukem 			 */
   1995    1.1     lukem 			fp = NULL;
   1996    1.1     lukem 
   1997  1.121   thorpej 			if (!(kn->kn_fop->f_flags & FILTEROP_ISFD)) {
   1998   1.49        ad 				/*
   1999   1.49        ad 				 * If knote is not on an fd, store on
   2000   1.49        ad 				 * internal hash table.
   2001   1.49        ad 				 */
   2002   1.49        ad 				if (fdp->fd_knhashmask == 0) {
   2003   1.49        ad 					/* XXXAD can block with fd_lock held */
   2004   1.49        ad 					fdp->fd_knhash = hashinit(KN_HASHSIZE,
   2005   1.59        ad 					    HASH_LIST, true,
   2006   1.49        ad 					    &fdp->fd_knhashmask);
   2007   1.49        ad 				}
   2008   1.49        ad 				list = &fdp->fd_knhash[KN_HASH(kn->kn_id,
   2009   1.49        ad 				    fdp->fd_knhashmask)];
   2010   1.49        ad 			} else {
   2011   1.49        ad 				/* Otherwise, knote is on an fd. */
   2012   1.49        ad 				list = (struct klist *)
   2013   1.65        ad 				    &fdp->fd_dt->dt_ff[kn->kn_id]->ff_knlist;
   2014   1.49        ad 				if ((int)kn->kn_id > fdp->fd_lastkqfile)
   2015   1.49        ad 					fdp->fd_lastkqfile = kn->kn_id;
   2016   1.49        ad 			}
   2017   1.49        ad 			SLIST_INSERT_HEAD(list, kn, kn_link);
   2018    1.1     lukem 
   2019  1.122   thorpej 			/*
   2020  1.122   thorpej 			 * N.B. kn->kn_fop may change as the result
   2021  1.122   thorpej 			 * of filter_attach()!
   2022  1.122   thorpej 			 */
   2023  1.145   thorpej 			knote_foplock_enter(kn);
   2024  1.122   thorpej 			error = filter_attach(kn);
   2025   1.49        ad 			if (error != 0) {
   2026  1.100  christos #ifdef DEBUG
   2027  1.105  christos 				struct proc *p = curlwp->l_proc;
   2028  1.101  christos 				const file_t *ft = kn->kn_obj;
   2029  1.105  christos 				printf("%s: %s[%d]: event type %d not "
   2030  1.105  christos 				    "supported for file type %d/%s "
   2031  1.105  christos 				    "(error %d)\n", __func__,
   2032  1.105  christos 				    p->p_comm, p->p_pid,
   2033  1.101  christos 				    kn->kn_filter, ft ? ft->f_type : -1,
   2034  1.101  christos 				    ft ? ft->f_ops->fo_name : "?", error);
   2035  1.100  christos #endif
   2036  1.100  christos 
   2037  1.145   thorpej  fail_ev_add:
   2038  1.129   thorpej 				/*
   2039  1.129   thorpej 				 * N.B. no need to check for this note to
   2040  1.129   thorpej 				 * be in-flux, since it was never visible
   2041  1.129   thorpej 				 * to the monitored object.
   2042  1.129   thorpej 				 *
   2043  1.129   thorpej 				 * knote_detach() drops fdp->fd_lock
   2044  1.129   thorpej 				 */
   2045  1.145   thorpej 				knote_foplock_exit(kn);
   2046  1.129   thorpej 				mutex_enter(&kq->kq_lock);
   2047  1.129   thorpej 				KNOTE_WILLDETACH(kn);
   2048  1.129   thorpej 				KASSERT(kn_in_flux(kn) == false);
   2049  1.129   thorpej 				mutex_exit(&kq->kq_lock);
   2050   1.49        ad 				knote_detach(kn, fdp, false);
   2051    1.1     lukem 				goto done;
   2052    1.1     lukem 			}
   2053   1.49        ad 			atomic_inc_uint(&kfilter->refcnt);
   2054  1.108  christos 			goto done_ev_add;
   2055    1.1     lukem 		} else {
   2056  1.108  christos 			/* No matching knote and the EV_ADD flag is not set. */
   2057  1.108  christos 			error = ENOENT;
   2058  1.108  christos 			goto doneunlock;
   2059    1.1     lukem 		}
   2060  1.108  christos 	}
   2061  1.108  christos 
   2062  1.108  christos 	if (kev->flags & EV_DELETE) {
   2063  1.129   thorpej 		/*
   2064  1.129   thorpej 		 * Let the world know that this knote is about to go
   2065  1.129   thorpej 		 * away, and wait for it to settle if it's currently
   2066  1.129   thorpej 		 * in-flux.
   2067  1.129   thorpej 		 */
   2068  1.129   thorpej 		mutex_spin_enter(&kq->kq_lock);
   2069  1.129   thorpej 		if (kn->kn_status & KN_WILLDETACH) {
   2070  1.129   thorpej 			/*
   2071  1.129   thorpej 			 * This knote is already on its way out,
   2072  1.129   thorpej 			 * so just be done.
   2073  1.129   thorpej 			 */
   2074  1.129   thorpej 			mutex_spin_exit(&kq->kq_lock);
   2075  1.129   thorpej 			goto doneunlock;
   2076  1.129   thorpej 		}
   2077  1.129   thorpej 		KNOTE_WILLDETACH(kn);
   2078  1.129   thorpej 		if (kn_in_flux(kn)) {
   2079  1.129   thorpej 			mutex_exit(&fdp->fd_lock);
   2080  1.129   thorpej 			/*
   2081  1.129   thorpej 			 * It's safe for us to conclusively wait for
   2082  1.129   thorpej 			 * this knote to settle because we know we'll
   2083  1.129   thorpej 			 * be completing the detach.
   2084  1.129   thorpej 			 */
   2085  1.129   thorpej 			kn_wait_flux(kn, true);
   2086  1.129   thorpej 			KASSERT(kn_in_flux(kn) == false);
   2087  1.129   thorpej 			mutex_spin_exit(&kq->kq_lock);
   2088  1.129   thorpej 			mutex_enter(&fdp->fd_lock);
   2089  1.129   thorpej 		} else {
   2090  1.129   thorpej 			mutex_spin_exit(&kq->kq_lock);
   2091  1.129   thorpej 		}
   2092  1.129   thorpej 
   2093  1.108  christos 		/* knote_detach() drops fdp->fd_lock */
   2094  1.108  christos 		knote_detach(kn, fdp, true);
   2095  1.108  christos 		goto done;
   2096  1.108  christos 	}
   2097  1.108  christos 
   2098  1.108  christos 	/*
   2099  1.108  christos 	 * The user may change some filter values after the
   2100  1.108  christos 	 * initial EV_ADD, but doing so will not reset any
   2101  1.108  christos 	 * filter which have already been triggered.
   2102  1.108  christos 	 */
   2103  1.145   thorpej 	knote_foplock_enter(kn);
   2104  1.108  christos 	kn->kn_kevent.udata = kev->udata;
   2105  1.108  christos 	KASSERT(kn->kn_fop != NULL);
   2106  1.121   thorpej 	if (!(kn->kn_fop->f_flags & FILTEROP_ISFD) &&
   2107  1.121   thorpej 	    kn->kn_fop->f_touch != NULL) {
   2108  1.116  jdolecek 		mutex_spin_enter(&kq->kq_lock);
   2109  1.135   thorpej 		error = filter_touch(kn, kev, EVENT_REGISTER);
   2110  1.116  jdolecek 		mutex_spin_exit(&kq->kq_lock);
   2111  1.135   thorpej 		if (__predict_false(error != 0)) {
   2112  1.135   thorpej 			/* Never a new knote (which would consume newkn). */
   2113  1.135   thorpej 			KASSERT(newkn != NULL);
   2114  1.145   thorpej 			knote_foplock_exit(kn);
   2115  1.135   thorpej 			goto doneunlock;
   2116  1.135   thorpej 		}
   2117   1.49        ad 	} else {
   2118  1.108  christos 		kn->kn_sfflags = kev->fflags;
   2119  1.108  christos 		kn->kn_sdata = kev->data;
   2120    1.1     lukem 	}
   2121    1.1     lukem 
   2122  1.108  christos 	/*
   2123  1.108  christos 	 * We can get here if we are trying to attach
   2124  1.108  christos 	 * an event to a file descriptor that does not
   2125  1.108  christos 	 * support events, and the attach routine is
   2126  1.108  christos 	 * broken and does not return an error.
   2127  1.108  christos 	 */
   2128  1.135   thorpej  done_ev_add:
   2129  1.145   thorpej 	rv = filter_event(kn, 0, false);
   2130  1.108  christos 	if (rv)
   2131  1.108  christos 		knote_activate(kn);
   2132  1.108  christos 
   2133  1.145   thorpej 	knote_foplock_exit(kn);
   2134  1.145   thorpej 
   2135    1.3  jdolecek 	/* disable knote */
   2136   1.49        ad 	if ((kev->flags & EV_DISABLE)) {
   2137   1.49        ad 		mutex_spin_enter(&kq->kq_lock);
   2138   1.49        ad 		if ((kn->kn_status & KN_DISABLED) == 0)
   2139   1.49        ad 			kn->kn_status |= KN_DISABLED;
   2140   1.49        ad 		mutex_spin_exit(&kq->kq_lock);
   2141    1.1     lukem 	}
   2142    1.1     lukem 
   2143    1.3  jdolecek 	/* enable knote */
   2144   1.49        ad 	if ((kev->flags & EV_ENABLE)) {
   2145   1.49        ad 		knote_enqueue(kn);
   2146    1.1     lukem 	}
   2147  1.135   thorpej  doneunlock:
   2148   1.49        ad 	mutex_exit(&fdp->fd_lock);
   2149    1.3  jdolecek  done:
   2150   1.49        ad 	rw_exit(&kqueue_filter_lock);
   2151   1.49        ad 	if (newkn != NULL)
   2152  1.142   thorpej 		knote_free(newkn);
   2153    1.1     lukem 	if (fp != NULL)
   2154   1.49        ad 		fd_putfile(fd);
   2155    1.1     lukem 	return (error);
   2156    1.1     lukem }
   2157    1.1     lukem 
   2158   1.94  christos #define KN_FMT(buf, kn) \
   2159   1.94  christos     (snprintb((buf), sizeof(buf), __KN_FLAG_BITS, (kn)->kn_status), buf)
   2160   1.94  christos 
   2161  1.129   thorpej #if defined(DDB)
   2162  1.129   thorpej void
   2163  1.129   thorpej kqueue_printit(struct kqueue *kq, bool full, void (*pr)(const char *, ...))
   2164  1.129   thorpej {
   2165  1.129   thorpej 	const struct knote *kn;
   2166  1.129   thorpej 	u_int count;
   2167  1.129   thorpej 	int nmarker;
   2168  1.129   thorpej 	char buf[128];
   2169  1.129   thorpej 
   2170  1.129   thorpej 	count = 0;
   2171  1.129   thorpej 	nmarker = 0;
   2172  1.129   thorpej 
   2173  1.129   thorpej 	(*pr)("kqueue %p (restart=%d count=%u):\n", kq,
   2174  1.129   thorpej 	    !!(kq->kq_count & KQ_RESTART), KQ_COUNT(kq));
   2175  1.129   thorpej 	(*pr)("  Queued knotes:\n");
   2176  1.129   thorpej 	TAILQ_FOREACH(kn, &kq->kq_head, kn_tqe) {
   2177  1.129   thorpej 		if (kn->kn_status & KN_MARKER) {
   2178  1.129   thorpej 			nmarker++;
   2179  1.129   thorpej 		} else {
   2180  1.129   thorpej 			count++;
   2181  1.129   thorpej 		}
   2182  1.129   thorpej 		(*pr)("    knote %p: kq=%p status=%s\n",
   2183  1.129   thorpej 		    kn, kn->kn_kq, KN_FMT(buf, kn));
   2184  1.129   thorpej 		(*pr)("      id=0x%lx (%lu) filter=%d\n",
   2185  1.129   thorpej 		    (u_long)kn->kn_id, (u_long)kn->kn_id, kn->kn_filter);
   2186  1.129   thorpej 		if (kn->kn_kq != kq) {
   2187  1.129   thorpej 			(*pr)("      !!! kn->kn_kq != kq\n");
   2188  1.129   thorpej 		}
   2189  1.129   thorpej 	}
   2190  1.129   thorpej 	if (count != KQ_COUNT(kq)) {
   2191  1.129   thorpej 		(*pr)("  !!! count(%u) != KQ_COUNT(%u)\n",
   2192  1.129   thorpej 		    count, KQ_COUNT(kq));
   2193  1.129   thorpej 	}
   2194  1.129   thorpej }
   2195  1.129   thorpej #endif /* DDB */
   2196  1.129   thorpej 
   2197  1.129   thorpej #if defined(DEBUG)
   2198   1.52      yamt static void
   2199   1.94  christos kqueue_check(const char *func, size_t line, const struct kqueue *kq)
   2200   1.52      yamt {
   2201   1.52      yamt 	const struct knote *kn;
   2202  1.118  jdolecek 	u_int count;
   2203   1.52      yamt 	int nmarker;
   2204   1.94  christos 	char buf[128];
   2205   1.52      yamt 
   2206   1.52      yamt 	KASSERT(mutex_owned(&kq->kq_lock));
   2207   1.52      yamt 
   2208   1.52      yamt 	count = 0;
   2209   1.52      yamt 	nmarker = 0;
   2210   1.52      yamt 	TAILQ_FOREACH(kn, &kq->kq_head, kn_tqe) {
   2211   1.52      yamt 		if ((kn->kn_status & (KN_MARKER | KN_QUEUED)) == 0) {
   2212   1.94  christos 			panic("%s,%zu: kq=%p kn=%p !(MARKER|QUEUED) %s",
   2213   1.94  christos 			    func, line, kq, kn, KN_FMT(buf, kn));
   2214   1.52      yamt 		}
   2215   1.52      yamt 		if ((kn->kn_status & KN_MARKER) == 0) {
   2216   1.52      yamt 			if (kn->kn_kq != kq) {
   2217   1.94  christos 				panic("%s,%zu: kq=%p kn(%p) != kn->kq(%p): %s",
   2218   1.94  christos 				    func, line, kq, kn, kn->kn_kq,
   2219   1.94  christos 				    KN_FMT(buf, kn));
   2220   1.52      yamt 			}
   2221   1.52      yamt 			if ((kn->kn_status & KN_ACTIVE) == 0) {
   2222   1.94  christos 				panic("%s,%zu: kq=%p kn=%p: !ACTIVE %s",
   2223   1.94  christos 				    func, line, kq, kn, KN_FMT(buf, kn));
   2224   1.52      yamt 			}
   2225   1.52      yamt 			count++;
   2226  1.118  jdolecek 			if (count > KQ_COUNT(kq)) {
   2227  1.129   thorpej 				panic("%s,%zu: kq=%p kq->kq_count(%u) != "
   2228  1.112  jdolecek 				    "count(%d), nmarker=%d",
   2229  1.118  jdolecek 		    		    func, line, kq, KQ_COUNT(kq), count,
   2230  1.112  jdolecek 				    nmarker);
   2231   1.52      yamt 			}
   2232   1.52      yamt 		} else {
   2233   1.52      yamt 			nmarker++;
   2234   1.52      yamt 		}
   2235   1.52      yamt 	}
   2236   1.52      yamt }
   2237   1.94  christos #define kq_check(a) kqueue_check(__func__, __LINE__, (a))
   2238   1.52      yamt #else /* defined(DEBUG) */
   2239   1.52      yamt #define	kq_check(a)	/* nothing */
   2240   1.52      yamt #endif /* defined(DEBUG) */
   2241   1.52      yamt 
   2242  1.118  jdolecek static void
   2243  1.118  jdolecek kqueue_restart(file_t *fp)
   2244  1.118  jdolecek {
   2245  1.118  jdolecek 	struct kqueue *kq = fp->f_kqueue;
   2246  1.118  jdolecek 	KASSERT(kq != NULL);
   2247  1.118  jdolecek 
   2248  1.118  jdolecek 	mutex_spin_enter(&kq->kq_lock);
   2249  1.118  jdolecek 	kq->kq_count |= KQ_RESTART;
   2250  1.118  jdolecek 	cv_broadcast(&kq->kq_cv);
   2251  1.118  jdolecek 	mutex_spin_exit(&kq->kq_lock);
   2252  1.118  jdolecek }
   2253  1.118  jdolecek 
   2254  1.148  riastrad static int
   2255  1.148  riastrad kqueue_fpathconf(struct file *fp, int name, register_t *retval)
   2256  1.148  riastrad {
   2257  1.148  riastrad 
   2258  1.148  riastrad 	return EINVAL;
   2259  1.148  riastrad }
   2260  1.148  riastrad 
   2261    1.3  jdolecek /*
   2262    1.3  jdolecek  * Scan through the list of events on fp (for a maximum of maxevents),
   2263    1.3  jdolecek  * returning the results in to ulistp. Timeout is determined by tsp; if
   2264    1.3  jdolecek  * NULL, wait indefinitely, if 0 valued, perform a poll, otherwise wait
   2265    1.3  jdolecek  * as appropriate.
   2266    1.3  jdolecek  */
   2267    1.1     lukem static int
   2268   1.49        ad kqueue_scan(file_t *fp, size_t maxevents, struct kevent *ulistp,
   2269   1.49        ad 	    const struct timespec *tsp, register_t *retval,
   2270   1.49        ad 	    const struct kevent_ops *keops, struct kevent *kevbuf,
   2271   1.49        ad 	    size_t kevcnt)
   2272    1.1     lukem {
   2273    1.3  jdolecek 	struct kqueue	*kq;
   2274    1.3  jdolecek 	struct kevent	*kevp;
   2275   1.62  christos 	struct timespec	ats, sleepts;
   2276  1.144   thorpej 	struct knote	*kn, *marker;
   2277  1.144   thorpej 	struct knote_impl morker;
   2278   1.24      cube 	size_t		count, nkev, nevents;
   2279  1.111  jdolecek 	int		timeout, error, touch, rv, influx;
   2280   1.49        ad 	filedesc_t	*fdp;
   2281    1.1     lukem 
   2282   1.49        ad 	fdp = curlwp->l_fd;
   2283   1.82      matt 	kq = fp->f_kqueue;
   2284    1.1     lukem 	count = maxevents;
   2285   1.24      cube 	nkev = nevents = error = 0;
   2286   1.49        ad 	if (count == 0) {
   2287   1.49        ad 		*retval = 0;
   2288   1.49        ad 		return 0;
   2289   1.49        ad 	}
   2290    1.1     lukem 
   2291    1.9  jdolecek 	if (tsp) {				/* timeout supplied */
   2292   1.63  christos 		ats = *tsp;
   2293   1.62  christos 		if (inittimeleft(&ats, &sleepts) == -1) {
   2294   1.49        ad 			*retval = maxevents;
   2295   1.49        ad 			return EINVAL;
   2296    1.1     lukem 		}
   2297   1.62  christos 		timeout = tstohz(&ats);
   2298    1.9  jdolecek 		if (timeout <= 0)
   2299   1.29    kardel 			timeout = -1;           /* do poll */
   2300    1.1     lukem 	} else {
   2301    1.9  jdolecek 		/* no timeout, wait forever */
   2302    1.1     lukem 		timeout = 0;
   2303   1.93  riastrad 	}
   2304    1.1     lukem 
   2305   1.85  christos 	memset(&morker, 0, sizeof(morker));
   2306  1.144   thorpej 	marker = &morker.ki_knote;
   2307  1.129   thorpej 	marker->kn_kq = kq;
   2308   1.49        ad 	marker->kn_status = KN_MARKER;
   2309   1.49        ad 	mutex_spin_enter(&kq->kq_lock);
   2310    1.3  jdolecek  retry:
   2311   1.49        ad 	kevp = kevbuf;
   2312  1.118  jdolecek 	if (KQ_COUNT(kq) == 0) {
   2313   1.49        ad 		if (timeout >= 0) {
   2314   1.49        ad 			error = cv_timedwait_sig(&kq->kq_cv,
   2315   1.49        ad 			    &kq->kq_lock, timeout);
   2316   1.49        ad 			if (error == 0) {
   2317  1.118  jdolecek 				if (KQ_COUNT(kq) == 0 &&
   2318  1.118  jdolecek 				    (kq->kq_count & KQ_RESTART)) {
   2319  1.118  jdolecek 					/* return to clear file reference */
   2320  1.118  jdolecek 					error = ERESTART;
   2321  1.118  jdolecek 				} else if (tsp == NULL || (timeout =
   2322  1.118  jdolecek 				    gettimeleft(&ats, &sleepts)) > 0) {
   2323   1.49        ad 					goto retry;
   2324  1.118  jdolecek 				}
   2325   1.49        ad 			} else {
   2326   1.49        ad 				/* don't restart after signals... */
   2327   1.49        ad 				if (error == ERESTART)
   2328   1.49        ad 					error = EINTR;
   2329   1.49        ad 				if (error == EWOULDBLOCK)
   2330   1.49        ad 					error = 0;
   2331   1.49        ad 			}
   2332    1.1     lukem 		}
   2333   1.92  christos 		mutex_spin_exit(&kq->kq_lock);
   2334  1.110  jdolecek 		goto done;
   2335  1.110  jdolecek 	}
   2336  1.110  jdolecek 
   2337  1.110  jdolecek 	/* mark end of knote list */
   2338  1.110  jdolecek 	TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
   2339  1.111  jdolecek 	influx = 0;
   2340    1.1     lukem 
   2341  1.110  jdolecek 	/*
   2342  1.110  jdolecek 	 * Acquire the fdp->fd_lock interlock to avoid races with
   2343  1.110  jdolecek 	 * file creation/destruction from other threads.
   2344  1.110  jdolecek 	 */
   2345  1.129   thorpej 	mutex_spin_exit(&kq->kq_lock);
   2346  1.111  jdolecek relock:
   2347  1.110  jdolecek 	mutex_enter(&fdp->fd_lock);
   2348  1.110  jdolecek 	mutex_spin_enter(&kq->kq_lock);
   2349   1.92  christos 
   2350  1.110  jdolecek 	while (count != 0) {
   2351  1.129   thorpej 		/*
   2352  1.129   thorpej 		 * Get next knote.  We are guaranteed this will never
   2353  1.129   thorpej 		 * be NULL because of the marker we inserted above.
   2354  1.129   thorpej 		 */
   2355  1.129   thorpej 		kn = TAILQ_FIRST(&kq->kq_head);
   2356  1.111  jdolecek 
   2357  1.129   thorpej 		bool kn_is_other_marker =
   2358  1.129   thorpej 		    (kn->kn_status & KN_MARKER) != 0 && kn != marker;
   2359  1.129   thorpej 		bool kn_is_detaching = (kn->kn_status & KN_WILLDETACH) != 0;
   2360  1.129   thorpej 		bool kn_is_in_flux = kn_in_flux(kn);
   2361  1.129   thorpej 
   2362  1.129   thorpej 		/*
   2363  1.129   thorpej 		 * If we found a marker that's not ours, or this knote
   2364  1.129   thorpej 		 * is in a state of flux, then wait for everything to
   2365  1.129   thorpej 		 * settle down and go around again.
   2366  1.129   thorpej 		 */
   2367  1.129   thorpej 		if (kn_is_other_marker || kn_is_detaching || kn_is_in_flux) {
   2368  1.111  jdolecek 			if (influx) {
   2369  1.111  jdolecek 				influx = 0;
   2370  1.111  jdolecek 				KQ_FLUX_WAKEUP(kq);
   2371  1.111  jdolecek 			}
   2372  1.111  jdolecek 			mutex_exit(&fdp->fd_lock);
   2373  1.129   thorpej 			if (kn_is_other_marker || kn_is_in_flux) {
   2374  1.129   thorpej 				KQ_FLUX_WAIT(kq);
   2375  1.129   thorpej 				mutex_spin_exit(&kq->kq_lock);
   2376  1.129   thorpej 			} else {
   2377  1.129   thorpej 				/*
   2378  1.129   thorpej 				 * Detaching but not in-flux?  Someone is
   2379  1.129   thorpej 				 * actively trying to finish the job; just
   2380  1.129   thorpej 				 * go around and try again.
   2381  1.129   thorpej 				 */
   2382  1.129   thorpej 				KASSERT(kn_is_detaching);
   2383  1.129   thorpej 				mutex_spin_exit(&kq->kq_lock);
   2384  1.129   thorpej 				preempt_point();
   2385  1.129   thorpej 			}
   2386  1.111  jdolecek 			goto relock;
   2387  1.111  jdolecek 		}
   2388  1.111  jdolecek 
   2389  1.111  jdolecek 		TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
   2390  1.111  jdolecek 		if (kn == marker) {
   2391  1.111  jdolecek 			/* it's our marker, stop */
   2392  1.111  jdolecek 			KQ_FLUX_WAKEUP(kq);
   2393  1.111  jdolecek 			if (count == maxevents) {
   2394  1.110  jdolecek 				mutex_exit(&fdp->fd_lock);
   2395  1.110  jdolecek 				goto retry;
   2396   1.49        ad 			}
   2397  1.111  jdolecek 			break;
   2398  1.110  jdolecek 		}
   2399  1.111  jdolecek 		KASSERT((kn->kn_status & KN_BUSY) == 0);
   2400  1.111  jdolecek 
   2401  1.110  jdolecek 		kq_check(kq);
   2402  1.115  jdolecek 		kn->kn_status &= ~KN_QUEUED;
   2403  1.110  jdolecek 		kn->kn_status |= KN_BUSY;
   2404  1.110  jdolecek 		kq_check(kq);
   2405  1.110  jdolecek 		if (kn->kn_status & KN_DISABLED) {
   2406  1.115  jdolecek 			kn->kn_status &= ~KN_BUSY;
   2407  1.111  jdolecek 			kq->kq_count--;
   2408  1.110  jdolecek 			/* don't want disabled events */
   2409  1.110  jdolecek 			continue;
   2410  1.110  jdolecek 		}
   2411  1.110  jdolecek 		if ((kn->kn_flags & EV_ONESHOT) == 0) {
   2412  1.110  jdolecek 			mutex_spin_exit(&kq->kq_lock);
   2413  1.110  jdolecek 			KASSERT(mutex_owned(&fdp->fd_lock));
   2414  1.145   thorpej 			knote_foplock_enter(kn);
   2415  1.145   thorpej 			rv = filter_event(kn, 0, false);
   2416  1.145   thorpej 			knote_foplock_exit(kn);
   2417  1.110  jdolecek 			mutex_spin_enter(&kq->kq_lock);
   2418  1.115  jdolecek 			/* Re-poll if note was re-enqueued. */
   2419  1.115  jdolecek 			if ((kn->kn_status & KN_QUEUED) != 0) {
   2420  1.115  jdolecek 				kn->kn_status &= ~KN_BUSY;
   2421  1.115  jdolecek 				/* Re-enqueue raised kq_count, lower it again */
   2422  1.115  jdolecek 				kq->kq_count--;
   2423  1.115  jdolecek 				influx = 1;
   2424  1.115  jdolecek 				continue;
   2425  1.115  jdolecek 			}
   2426  1.110  jdolecek 			if (rv == 0) {
   2427  1.110  jdolecek 				/*
   2428  1.129   thorpej 				 * non-ONESHOT event that hasn't triggered
   2429  1.129   thorpej 				 * again, so it will remain de-queued.
   2430  1.110  jdolecek 				 */
   2431  1.115  jdolecek 				kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
   2432  1.111  jdolecek 				kq->kq_count--;
   2433  1.111  jdolecek 				influx = 1;
   2434  1.110  jdolecek 				continue;
   2435   1.49        ad 			}
   2436  1.129   thorpej 		} else {
   2437  1.129   thorpej 			/*
   2438  1.138   thorpej 			 * Must NOT drop kq_lock until we can do
   2439  1.138   thorpej 			 * the KNOTE_WILLDETACH() below.
   2440  1.129   thorpej 			 */
   2441  1.110  jdolecek 		}
   2442  1.110  jdolecek 		KASSERT(kn->kn_fop != NULL);
   2443  1.121   thorpej 		touch = (!(kn->kn_fop->f_flags & FILTEROP_ISFD) &&
   2444  1.110  jdolecek 				kn->kn_fop->f_touch != NULL);
   2445  1.110  jdolecek 		/* XXXAD should be got from f_event if !oneshot. */
   2446  1.138   thorpej 		KASSERT((kn->kn_status & KN_WILLDETACH) == 0);
   2447  1.110  jdolecek 		if (touch) {
   2448  1.135   thorpej 			(void)filter_touch(kn, kevp, EVENT_PROCESS);
   2449  1.110  jdolecek 		} else {
   2450  1.110  jdolecek 			*kevp = kn->kn_kevent;
   2451  1.110  jdolecek 		}
   2452  1.110  jdolecek 		kevp++;
   2453  1.110  jdolecek 		nkev++;
   2454  1.111  jdolecek 		influx = 1;
   2455  1.110  jdolecek 		if (kn->kn_flags & EV_ONESHOT) {
   2456  1.110  jdolecek 			/* delete ONESHOT events after retrieval */
   2457  1.138   thorpej 			KNOTE_WILLDETACH(kn);
   2458  1.115  jdolecek 			kn->kn_status &= ~KN_BUSY;
   2459  1.111  jdolecek 			kq->kq_count--;
   2460  1.129   thorpej 			KASSERT(kn_in_flux(kn) == false);
   2461  1.147  riastrad 			KASSERT((kn->kn_status & KN_WILLDETACH) != 0);
   2462  1.147  riastrad 			KASSERT(kn->kn_kevent.udata == curlwp);
   2463  1.110  jdolecek 			mutex_spin_exit(&kq->kq_lock);
   2464  1.110  jdolecek 			knote_detach(kn, fdp, true);
   2465  1.110  jdolecek 			mutex_enter(&fdp->fd_lock);
   2466  1.110  jdolecek 			mutex_spin_enter(&kq->kq_lock);
   2467  1.110  jdolecek 		} else if (kn->kn_flags & EV_CLEAR) {
   2468  1.110  jdolecek 			/* clear state after retrieval */
   2469  1.110  jdolecek 			kn->kn_data = 0;
   2470  1.110  jdolecek 			kn->kn_fflags = 0;
   2471  1.110  jdolecek 			/*
   2472  1.110  jdolecek 			 * Manually clear knotes who weren't
   2473  1.110  jdolecek 			 * 'touch'ed.
   2474  1.110  jdolecek 			 */
   2475  1.110  jdolecek 			if (touch == 0) {
   2476   1.49        ad 				kn->kn_data = 0;
   2477   1.49        ad 				kn->kn_fflags = 0;
   2478   1.49        ad 			}
   2479  1.115  jdolecek 			kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
   2480  1.111  jdolecek 			kq->kq_count--;
   2481  1.110  jdolecek 		} else if (kn->kn_flags & EV_DISPATCH) {
   2482  1.110  jdolecek 			kn->kn_status |= KN_DISABLED;
   2483  1.115  jdolecek 			kn->kn_status &= ~(KN_ACTIVE|KN_BUSY);
   2484  1.111  jdolecek 			kq->kq_count--;
   2485  1.110  jdolecek 		} else {
   2486  1.110  jdolecek 			/* add event back on list */
   2487  1.110  jdolecek 			kq_check(kq);
   2488  1.115  jdolecek 			kn->kn_status |= KN_QUEUED;
   2489  1.110  jdolecek 			kn->kn_status &= ~KN_BUSY;
   2490  1.110  jdolecek 			TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
   2491  1.110  jdolecek 			kq_check(kq);
   2492  1.110  jdolecek 		}
   2493  1.111  jdolecek 
   2494  1.110  jdolecek 		if (nkev == kevcnt) {
   2495  1.110  jdolecek 			/* do copyouts in kevcnt chunks */
   2496  1.111  jdolecek 			influx = 0;
   2497  1.111  jdolecek 			KQ_FLUX_WAKEUP(kq);
   2498  1.110  jdolecek 			mutex_spin_exit(&kq->kq_lock);
   2499  1.110  jdolecek 			mutex_exit(&fdp->fd_lock);
   2500  1.110  jdolecek 			error = (*keops->keo_put_events)
   2501  1.110  jdolecek 			    (keops->keo_private,
   2502  1.110  jdolecek 			    kevbuf, ulistp, nevents, nkev);
   2503  1.110  jdolecek 			mutex_enter(&fdp->fd_lock);
   2504  1.110  jdolecek 			mutex_spin_enter(&kq->kq_lock);
   2505  1.110  jdolecek 			nevents += nkev;
   2506  1.110  jdolecek 			nkev = 0;
   2507  1.110  jdolecek 			kevp = kevbuf;
   2508  1.110  jdolecek 		}
   2509  1.110  jdolecek 		count--;
   2510  1.110  jdolecek 		if (error != 0 || count == 0) {
   2511  1.110  jdolecek 			/* remove marker */
   2512  1.110  jdolecek 			TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
   2513  1.110  jdolecek 			break;
   2514    1.1     lukem 		}
   2515    1.1     lukem 	}
   2516  1.111  jdolecek 	KQ_FLUX_WAKEUP(kq);
   2517  1.110  jdolecek 	mutex_spin_exit(&kq->kq_lock);
   2518  1.110  jdolecek 	mutex_exit(&fdp->fd_lock);
   2519  1.110  jdolecek 
   2520  1.110  jdolecek done:
   2521   1.49        ad 	if (nkev != 0) {
   2522    1.3  jdolecek 		/* copyout remaining events */
   2523   1.24      cube 		error = (*keops->keo_put_events)(keops->keo_private,
   2524   1.49        ad 		    kevbuf, ulistp, nevents, nkev);
   2525   1.49        ad 	}
   2526    1.3  jdolecek 	*retval = maxevents - count;
   2527    1.3  jdolecek 
   2528   1.49        ad 	return error;
   2529    1.1     lukem }
   2530    1.1     lukem 
   2531    1.1     lukem /*
   2532   1.49        ad  * fileops ioctl method for a kqueue descriptor.
   2533    1.3  jdolecek  *
   2534    1.3  jdolecek  * Two ioctls are currently supported. They both use struct kfilter_mapping:
   2535    1.3  jdolecek  *	KFILTER_BYNAME		find name for filter, and return result in
   2536    1.3  jdolecek  *				name, which is of size len.
   2537    1.3  jdolecek  *	KFILTER_BYFILTER	find filter for name. len is ignored.
   2538    1.3  jdolecek  */
   2539    1.1     lukem /*ARGSUSED*/
   2540    1.1     lukem static int
   2541   1.49        ad kqueue_ioctl(file_t *fp, u_long com, void *data)
   2542    1.1     lukem {
   2543    1.3  jdolecek 	struct kfilter_mapping	*km;
   2544    1.3  jdolecek 	const struct kfilter	*kfilter;
   2545    1.3  jdolecek 	char			*name;
   2546    1.3  jdolecek 	int			error;
   2547    1.3  jdolecek 
   2548   1.49        ad 	km = data;
   2549    1.3  jdolecek 	error = 0;
   2550   1.49        ad 	name = kmem_alloc(KFILTER_MAXNAME, KM_SLEEP);
   2551    1.3  jdolecek 
   2552    1.3  jdolecek 	switch (com) {
   2553    1.3  jdolecek 	case KFILTER_BYFILTER:	/* convert filter -> name */
   2554   1.49        ad 		rw_enter(&kqueue_filter_lock, RW_READER);
   2555    1.3  jdolecek 		kfilter = kfilter_byfilter(km->filter);
   2556   1.49        ad 		if (kfilter != NULL) {
   2557   1.49        ad 			strlcpy(name, kfilter->name, KFILTER_MAXNAME);
   2558   1.49        ad 			rw_exit(&kqueue_filter_lock);
   2559   1.49        ad 			error = copyoutstr(name, km->name, km->len, NULL);
   2560   1.49        ad 		} else {
   2561   1.49        ad 			rw_exit(&kqueue_filter_lock);
   2562    1.3  jdolecek 			error = ENOENT;
   2563   1.49        ad 		}
   2564    1.3  jdolecek 		break;
   2565    1.3  jdolecek 
   2566    1.3  jdolecek 	case KFILTER_BYNAME:	/* convert name -> filter */
   2567    1.3  jdolecek 		error = copyinstr(km->name, name, KFILTER_MAXNAME, NULL);
   2568    1.3  jdolecek 		if (error) {
   2569    1.3  jdolecek 			break;
   2570    1.3  jdolecek 		}
   2571   1.49        ad 		rw_enter(&kqueue_filter_lock, RW_READER);
   2572    1.3  jdolecek 		kfilter = kfilter_byname(name);
   2573    1.3  jdolecek 		if (kfilter != NULL)
   2574    1.3  jdolecek 			km->filter = kfilter->filter;
   2575    1.3  jdolecek 		else
   2576    1.3  jdolecek 			error = ENOENT;
   2577   1.49        ad 		rw_exit(&kqueue_filter_lock);
   2578    1.3  jdolecek 		break;
   2579    1.3  jdolecek 
   2580    1.3  jdolecek 	default:
   2581    1.3  jdolecek 		error = ENOTTY;
   2582   1.49        ad 		break;
   2583    1.3  jdolecek 
   2584    1.3  jdolecek 	}
   2585   1.49        ad 	kmem_free(name, KFILTER_MAXNAME);
   2586    1.3  jdolecek 	return (error);
   2587    1.3  jdolecek }
   2588    1.3  jdolecek 
   2589    1.3  jdolecek /*
   2590   1.49        ad  * fileops fcntl method for a kqueue descriptor.
   2591    1.3  jdolecek  */
   2592    1.3  jdolecek static int
   2593   1.49        ad kqueue_fcntl(file_t *fp, u_int com, void *data)
   2594    1.3  jdolecek {
   2595    1.3  jdolecek 
   2596    1.1     lukem 	return (ENOTTY);
   2597    1.1     lukem }
   2598    1.1     lukem 
   2599    1.3  jdolecek /*
   2600   1.49        ad  * fileops poll method for a kqueue descriptor.
   2601    1.3  jdolecek  * Determine if kqueue has events pending.
   2602    1.3  jdolecek  */
   2603    1.1     lukem static int
   2604   1.49        ad kqueue_poll(file_t *fp, int events)
   2605    1.1     lukem {
   2606    1.3  jdolecek 	struct kqueue	*kq;
   2607    1.3  jdolecek 	int		revents;
   2608    1.3  jdolecek 
   2609   1.82      matt 	kq = fp->f_kqueue;
   2610   1.49        ad 
   2611    1.3  jdolecek 	revents = 0;
   2612    1.3  jdolecek 	if (events & (POLLIN | POLLRDNORM)) {
   2613   1.49        ad 		mutex_spin_enter(&kq->kq_lock);
   2614  1.118  jdolecek 		if (KQ_COUNT(kq) != 0) {
   2615    1.3  jdolecek 			revents |= events & (POLLIN | POLLRDNORM);
   2616    1.1     lukem 		} else {
   2617   1.49        ad 			selrecord(curlwp, &kq->kq_sel);
   2618    1.1     lukem 		}
   2619   1.52      yamt 		kq_check(kq);
   2620   1.49        ad 		mutex_spin_exit(&kq->kq_lock);
   2621    1.1     lukem 	}
   2622   1.49        ad 
   2623   1.49        ad 	return revents;
   2624    1.1     lukem }
   2625    1.1     lukem 
   2626    1.3  jdolecek /*
   2627   1.49        ad  * fileops stat method for a kqueue descriptor.
   2628    1.3  jdolecek  * Returns dummy info, with st_size being number of events pending.
   2629    1.3  jdolecek  */
   2630    1.1     lukem static int
   2631   1.49        ad kqueue_stat(file_t *fp, struct stat *st)
   2632    1.1     lukem {
   2633   1.49        ad 	struct kqueue *kq;
   2634   1.49        ad 
   2635   1.82      matt 	kq = fp->f_kqueue;
   2636    1.1     lukem 
   2637   1.49        ad 	memset(st, 0, sizeof(*st));
   2638  1.118  jdolecek 	st->st_size = KQ_COUNT(kq);
   2639    1.1     lukem 	st->st_blksize = sizeof(struct kevent);
   2640  1.128   thorpej 	st->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
   2641  1.128   thorpej 	st->st_blocks = 1;
   2642  1.128   thorpej 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
   2643  1.128   thorpej 	st->st_gid = kauth_cred_getegid(fp->f_cred);
   2644   1.49        ad 
   2645   1.49        ad 	return 0;
   2646   1.49        ad }
   2647   1.49        ad 
   2648   1.49        ad static void
   2649   1.49        ad kqueue_doclose(struct kqueue *kq, struct klist *list, int fd)
   2650   1.49        ad {
   2651   1.49        ad 	struct knote *kn;
   2652   1.49        ad 	filedesc_t *fdp;
   2653   1.49        ad 
   2654   1.49        ad 	fdp = kq->kq_fdp;
   2655   1.49        ad 
   2656   1.49        ad 	KASSERT(mutex_owned(&fdp->fd_lock));
   2657   1.49        ad 
   2658  1.129   thorpej  again:
   2659   1.49        ad 	for (kn = SLIST_FIRST(list); kn != NULL;) {
   2660   1.49        ad 		if (kq != kn->kn_kq) {
   2661   1.49        ad 			kn = SLIST_NEXT(kn, kn_link);
   2662   1.49        ad 			continue;
   2663   1.49        ad 		}
   2664  1.129   thorpej 		if (knote_detach_quiesce(kn)) {
   2665  1.129   thorpej 			mutex_enter(&fdp->fd_lock);
   2666  1.129   thorpej 			goto again;
   2667  1.129   thorpej 		}
   2668   1.49        ad 		knote_detach(kn, fdp, true);
   2669   1.49        ad 		mutex_enter(&fdp->fd_lock);
   2670   1.49        ad 		kn = SLIST_FIRST(list);
   2671   1.49        ad 	}
   2672    1.1     lukem }
   2673    1.1     lukem 
   2674    1.3  jdolecek /*
   2675   1.49        ad  * fileops close method for a kqueue descriptor.
   2676    1.3  jdolecek  */
   2677    1.1     lukem static int
   2678   1.49        ad kqueue_close(file_t *fp)
   2679    1.1     lukem {
   2680   1.49        ad 	struct kqueue *kq;
   2681   1.49        ad 	filedesc_t *fdp;
   2682   1.49        ad 	fdfile_t *ff;
   2683   1.49        ad 	int i;
   2684   1.49        ad 
   2685   1.82      matt 	kq = fp->f_kqueue;
   2686   1.82      matt 	fp->f_kqueue = NULL;
   2687   1.79  christos 	fp->f_type = 0;
   2688   1.49        ad 	fdp = curlwp->l_fd;
   2689    1.1     lukem 
   2690  1.129   thorpej 	KASSERT(kq->kq_fdp == fdp);
   2691  1.129   thorpej 
   2692   1.49        ad 	mutex_enter(&fdp->fd_lock);
   2693  1.129   thorpej 
   2694  1.129   thorpej 	/*
   2695  1.129   thorpej 	 * We're doing to drop the fd_lock multiple times while
   2696  1.129   thorpej 	 * we detach knotes.  During this time, attempts to register
   2697  1.129   thorpej 	 * knotes via the back door (e.g. knote_proc_fork_track())
   2698  1.129   thorpej 	 * need to fail, lest they sneak in to attach a knote after
   2699  1.129   thorpej 	 * we've already drained the list it's destined for.
   2700  1.129   thorpej 	 *
   2701  1.139   msaitoh 	 * We must acquire kq_lock here to set KQ_CLOSING (to serialize
   2702  1.129   thorpej 	 * with other code paths that modify kq_count without holding
   2703  1.129   thorpej 	 * the fd_lock), but once this bit is set, it's only safe to
   2704  1.129   thorpej 	 * test it while holding the fd_lock, and holding kq_lock while
   2705  1.129   thorpej 	 * doing so is not necessary.
   2706  1.129   thorpej 	 */
   2707  1.129   thorpej 	mutex_enter(&kq->kq_lock);
   2708  1.129   thorpej 	kq->kq_count |= KQ_CLOSING;
   2709  1.129   thorpej 	mutex_exit(&kq->kq_lock);
   2710  1.129   thorpej 
   2711   1.49        ad 	for (i = 0; i <= fdp->fd_lastkqfile; i++) {
   2712   1.65        ad 		if ((ff = fdp->fd_dt->dt_ff[i]) == NULL)
   2713   1.49        ad 			continue;
   2714   1.49        ad 		kqueue_doclose(kq, (struct klist *)&ff->ff_knlist, i);
   2715    1.1     lukem 	}
   2716    1.1     lukem 	if (fdp->fd_knhashmask != 0) {
   2717    1.1     lukem 		for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
   2718   1.49        ad 			kqueue_doclose(kq, &fdp->fd_knhash[i], -1);
   2719    1.1     lukem 		}
   2720    1.1     lukem 	}
   2721  1.129   thorpej 
   2722   1.49        ad 	mutex_exit(&fdp->fd_lock);
   2723   1.49        ad 
   2724  1.129   thorpej #if defined(DEBUG)
   2725  1.129   thorpej 	mutex_enter(&kq->kq_lock);
   2726  1.129   thorpej 	kq_check(kq);
   2727  1.129   thorpej 	mutex_exit(&kq->kq_lock);
   2728  1.129   thorpej #endif /* DEBUG */
   2729  1.129   thorpej 	KASSERT(TAILQ_EMPTY(&kq->kq_head));
   2730  1.118  jdolecek 	KASSERT(KQ_COUNT(kq) == 0);
   2731   1.49        ad 	mutex_destroy(&kq->kq_lock);
   2732   1.49        ad 	cv_destroy(&kq->kq_cv);
   2733   1.48     rmind 	seldestroy(&kq->kq_sel);
   2734   1.49        ad 	kmem_free(kq, sizeof(*kq));
   2735    1.1     lukem 
   2736    1.1     lukem 	return (0);
   2737    1.1     lukem }
   2738    1.1     lukem 
   2739    1.3  jdolecek /*
   2740    1.3  jdolecek  * struct fileops kqfilter method for a kqueue descriptor.
   2741    1.3  jdolecek  * Event triggered when monitored kqueue changes.
   2742    1.3  jdolecek  */
   2743    1.3  jdolecek static int
   2744   1.49        ad kqueue_kqfilter(file_t *fp, struct knote *kn)
   2745    1.3  jdolecek {
   2746    1.3  jdolecek 	struct kqueue *kq;
   2747   1.49        ad 
   2748   1.82      matt 	kq = ((file_t *)kn->kn_obj)->f_kqueue;
   2749   1.49        ad 
   2750   1.49        ad 	KASSERT(fp == kn->kn_obj);
   2751    1.3  jdolecek 
   2752    1.3  jdolecek 	if (kn->kn_filter != EVFILT_READ)
   2753  1.126   thorpej 		return EINVAL;
   2754   1.49        ad 
   2755    1.3  jdolecek 	kn->kn_fop = &kqread_filtops;
   2756   1.49        ad 	mutex_enter(&kq->kq_lock);
   2757  1.109   thorpej 	selrecord_knote(&kq->kq_sel, kn);
   2758   1.49        ad 	mutex_exit(&kq->kq_lock);
   2759   1.49        ad 
   2760   1.49        ad 	return 0;
   2761    1.3  jdolecek }
   2762    1.3  jdolecek 
   2763    1.3  jdolecek 
   2764    1.3  jdolecek /*
   2765   1.49        ad  * Walk down a list of knotes, activating them if their event has
   2766   1.49        ad  * triggered.  The caller's object lock (e.g. device driver lock)
   2767   1.49        ad  * must be held.
   2768    1.1     lukem  */
   2769    1.1     lukem void
   2770    1.1     lukem knote(struct klist *list, long hint)
   2771    1.1     lukem {
   2772   1.71  drochner 	struct knote *kn, *tmpkn;
   2773    1.1     lukem 
   2774   1.71  drochner 	SLIST_FOREACH_SAFE(kn, list, kn_selnext, tmpkn) {
   2775  1.145   thorpej 		/*
   2776  1.145   thorpej 		 * We assume here that the backing object's lock is
   2777  1.145   thorpej 		 * already held if we're traversing the klist, and
   2778  1.145   thorpej 		 * so acquiring the knote foplock would create a
   2779  1.145   thorpej 		 * deadlock scenario.  But we also know that the klist
   2780  1.145   thorpej 		 * won't disappear on us while we're here, so not
   2781  1.145   thorpej 		 * acquiring it is safe.
   2782  1.145   thorpej 		 */
   2783  1.145   thorpej 		if (filter_event(kn, hint, true)) {
   2784   1.49        ad 			knote_activate(kn);
   2785  1.127   thorpej 		}
   2786   1.49        ad 	}
   2787    1.1     lukem }
   2788    1.1     lukem 
   2789    1.1     lukem /*
   2790   1.49        ad  * Remove all knotes referencing a specified fd
   2791    1.1     lukem  */
   2792    1.1     lukem void
   2793   1.49        ad knote_fdclose(int fd)
   2794    1.1     lukem {
   2795   1.49        ad 	struct klist *list;
   2796    1.1     lukem 	struct knote *kn;
   2797   1.49        ad 	filedesc_t *fdp;
   2798    1.1     lukem 
   2799  1.129   thorpej  again:
   2800   1.49        ad 	fdp = curlwp->l_fd;
   2801  1.106  riastrad 	mutex_enter(&fdp->fd_lock);
   2802   1.65        ad 	list = (struct klist *)&fdp->fd_dt->dt_ff[fd]->ff_knlist;
   2803    1.1     lukem 	while ((kn = SLIST_FIRST(list)) != NULL) {
   2804  1.129   thorpej 		if (knote_detach_quiesce(kn)) {
   2805  1.129   thorpej 			goto again;
   2806  1.129   thorpej 		}
   2807   1.49        ad 		knote_detach(kn, fdp, true);
   2808   1.49        ad 		mutex_enter(&fdp->fd_lock);
   2809    1.1     lukem 	}
   2810   1.49        ad 	mutex_exit(&fdp->fd_lock);
   2811    1.1     lukem }
   2812    1.1     lukem 
   2813    1.1     lukem /*
   2814   1.49        ad  * Drop knote.  Called with fdp->fd_lock held, and will drop before
   2815   1.49        ad  * returning.
   2816    1.3  jdolecek  */
   2817    1.1     lukem static void
   2818   1.49        ad knote_detach(struct knote *kn, filedesc_t *fdp, bool dofop)
   2819    1.1     lukem {
   2820   1.49        ad 	struct klist *list;
   2821   1.53        ad 	struct kqueue *kq;
   2822   1.53        ad 
   2823   1.53        ad 	kq = kn->kn_kq;
   2824    1.1     lukem 
   2825   1.49        ad 	KASSERT((kn->kn_status & KN_MARKER) == 0);
   2826  1.129   thorpej 	KASSERT((kn->kn_status & KN_WILLDETACH) != 0);
   2827  1.129   thorpej 	KASSERT(kn->kn_fop != NULL);
   2828   1.49        ad 	KASSERT(mutex_owned(&fdp->fd_lock));
   2829    1.3  jdolecek 
   2830   1.53        ad 	/* Remove from monitored object. */
   2831   1.49        ad 	if (dofop) {
   2832  1.145   thorpej 		knote_foplock_enter(kn);
   2833  1.122   thorpej 		filter_detach(kn);
   2834  1.145   thorpej 		knote_foplock_exit(kn);
   2835    1.1     lukem 	}
   2836    1.3  jdolecek 
   2837   1.53        ad 	/* Remove from descriptor table. */
   2838  1.121   thorpej 	if (kn->kn_fop->f_flags & FILTEROP_ISFD)
   2839   1.65        ad 		list = (struct klist *)&fdp->fd_dt->dt_ff[kn->kn_id]->ff_knlist;
   2840    1.1     lukem 	else
   2841    1.1     lukem 		list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
   2842    1.1     lukem 
   2843    1.1     lukem 	SLIST_REMOVE(list, kn, knote, kn_link);
   2844   1.53        ad 
   2845   1.53        ad 	/* Remove from kqueue. */
   2846   1.85  christos again:
   2847   1.53        ad 	mutex_spin_enter(&kq->kq_lock);
   2848  1.129   thorpej 	KASSERT(kn_in_flux(kn) == false);
   2849   1.53        ad 	if ((kn->kn_status & KN_QUEUED) != 0) {
   2850   1.53        ad 		kq_check(kq);
   2851  1.129   thorpej 		KASSERT(KQ_COUNT(kq) != 0);
   2852   1.85  christos 		kq->kq_count--;
   2853   1.53        ad 		TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
   2854   1.53        ad 		kn->kn_status &= ~KN_QUEUED;
   2855   1.53        ad 		kq_check(kq);
   2856   1.85  christos 	} else if (kn->kn_status & KN_BUSY) {
   2857   1.85  christos 		mutex_spin_exit(&kq->kq_lock);
   2858   1.85  christos 		goto again;
   2859   1.53        ad 	}
   2860   1.53        ad 	mutex_spin_exit(&kq->kq_lock);
   2861   1.53        ad 
   2862   1.49        ad 	mutex_exit(&fdp->fd_lock);
   2863  1.121   thorpej 	if (kn->kn_fop->f_flags & FILTEROP_ISFD)
   2864   1.49        ad 		fd_putfile(kn->kn_id);
   2865   1.49        ad 	atomic_dec_uint(&kn->kn_kfilter->refcnt);
   2866  1.142   thorpej 	knote_free(kn);
   2867    1.1     lukem }
   2868    1.1     lukem 
   2869    1.3  jdolecek /*
   2870    1.3  jdolecek  * Queue new event for knote.
   2871    1.3  jdolecek  */
   2872    1.1     lukem static void
   2873    1.1     lukem knote_enqueue(struct knote *kn)
   2874    1.1     lukem {
   2875   1.49        ad 	struct kqueue *kq;
   2876   1.49        ad 
   2877   1.49        ad 	KASSERT((kn->kn_status & KN_MARKER) == 0);
   2878    1.1     lukem 
   2879    1.3  jdolecek 	kq = kn->kn_kq;
   2880    1.1     lukem 
   2881   1.49        ad 	mutex_spin_enter(&kq->kq_lock);
   2882  1.129   thorpej 	if (__predict_false(kn->kn_status & KN_WILLDETACH)) {
   2883  1.129   thorpej 		/* Don't bother enqueueing a dying knote. */
   2884  1.129   thorpej 		goto out;
   2885  1.129   thorpej 	}
   2886   1.49        ad 	if ((kn->kn_status & KN_DISABLED) != 0) {
   2887   1.49        ad 		kn->kn_status &= ~KN_DISABLED;
   2888   1.49        ad 	}
   2889   1.49        ad 	if ((kn->kn_status & (KN_ACTIVE | KN_QUEUED)) == KN_ACTIVE) {
   2890   1.52      yamt 		kq_check(kq);
   2891   1.85  christos 		kn->kn_status |= KN_QUEUED;
   2892   1.49        ad 		TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
   2893  1.129   thorpej 		KASSERT(KQ_COUNT(kq) < KQ_MAXCOUNT);
   2894   1.49        ad 		kq->kq_count++;
   2895   1.52      yamt 		kq_check(kq);
   2896   1.49        ad 		cv_broadcast(&kq->kq_cv);
   2897   1.49        ad 		selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
   2898   1.49        ad 	}
   2899  1.129   thorpej  out:
   2900   1.49        ad 	mutex_spin_exit(&kq->kq_lock);
   2901    1.1     lukem }
   2902   1.49        ad /*
   2903   1.49        ad  * Queue new event for knote.
   2904   1.49        ad  */
   2905   1.49        ad static void
   2906  1.133   thorpej knote_activate_locked(struct knote *kn)
   2907   1.49        ad {
   2908   1.49        ad 	struct kqueue *kq;
   2909   1.49        ad 
   2910   1.49        ad 	KASSERT((kn->kn_status & KN_MARKER) == 0);
   2911    1.1     lukem 
   2912    1.3  jdolecek 	kq = kn->kn_kq;
   2913   1.12        pk 
   2914  1.129   thorpej 	if (__predict_false(kn->kn_status & KN_WILLDETACH)) {
   2915  1.129   thorpej 		/* Don't bother enqueueing a dying knote. */
   2916  1.133   thorpej 		return;
   2917  1.129   thorpej 	}
   2918   1.49        ad 	kn->kn_status |= KN_ACTIVE;
   2919   1.49        ad 	if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) {
   2920   1.52      yamt 		kq_check(kq);
   2921   1.85  christos 		kn->kn_status |= KN_QUEUED;
   2922   1.49        ad 		TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
   2923  1.129   thorpej 		KASSERT(KQ_COUNT(kq) < KQ_MAXCOUNT);
   2924   1.49        ad 		kq->kq_count++;
   2925   1.52      yamt 		kq_check(kq);
   2926   1.49        ad 		cv_broadcast(&kq->kq_cv);
   2927   1.49        ad 		selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
   2928   1.49        ad 	}
   2929  1.133   thorpej }
   2930  1.133   thorpej 
   2931  1.133   thorpej static void
   2932  1.133   thorpej knote_activate(struct knote *kn)
   2933  1.133   thorpej {
   2934  1.133   thorpej 	struct kqueue *kq = kn->kn_kq;
   2935  1.133   thorpej 
   2936  1.133   thorpej 	mutex_spin_enter(&kq->kq_lock);
   2937  1.133   thorpej 	knote_activate_locked(kn);
   2938   1.49        ad 	mutex_spin_exit(&kq->kq_lock);
   2939    1.1     lukem }
   2940  1.131   thorpej 
   2941  1.136   thorpej static void
   2942  1.136   thorpej knote_deactivate_locked(struct knote *kn)
   2943  1.136   thorpej {
   2944  1.136   thorpej 	struct kqueue *kq = kn->kn_kq;
   2945  1.136   thorpej 
   2946  1.136   thorpej 	if (kn->kn_status & KN_QUEUED) {
   2947  1.136   thorpej 		kq_check(kq);
   2948  1.136   thorpej 		kn->kn_status &= ~KN_QUEUED;
   2949  1.136   thorpej 		TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
   2950  1.136   thorpej 		KASSERT(KQ_COUNT(kq) > 0);
   2951  1.136   thorpej 		kq->kq_count--;
   2952  1.136   thorpej 		kq_check(kq);
   2953  1.136   thorpej 	}
   2954  1.136   thorpej 	kn->kn_status &= ~KN_ACTIVE;
   2955  1.136   thorpej }
   2956  1.136   thorpej 
   2957  1.131   thorpej /*
   2958  1.131   thorpej  * Set EV_EOF on the specified knote.  Also allows additional
   2959  1.131   thorpej  * EV_* flags to be set (e.g. EV_ONESHOT).
   2960  1.131   thorpej  */
   2961  1.131   thorpej void
   2962  1.131   thorpej knote_set_eof(struct knote *kn, uint32_t flags)
   2963  1.131   thorpej {
   2964  1.131   thorpej 	struct kqueue *kq = kn->kn_kq;
   2965  1.131   thorpej 
   2966  1.131   thorpej 	mutex_spin_enter(&kq->kq_lock);
   2967  1.131   thorpej 	kn->kn_flags |= EV_EOF | flags;
   2968  1.131   thorpej 	mutex_spin_exit(&kq->kq_lock);
   2969  1.131   thorpej }
   2970  1.131   thorpej 
   2971  1.131   thorpej /*
   2972  1.131   thorpej  * Clear EV_EOF on the specified knote.
   2973  1.131   thorpej  */
   2974  1.131   thorpej void
   2975  1.131   thorpej knote_clear_eof(struct knote *kn)
   2976  1.131   thorpej {
   2977  1.131   thorpej 	struct kqueue *kq = kn->kn_kq;
   2978  1.131   thorpej 
   2979  1.131   thorpej 	mutex_spin_enter(&kq->kq_lock);
   2980  1.131   thorpej 	kn->kn_flags &= ~EV_EOF;
   2981  1.131   thorpej 	mutex_spin_exit(&kq->kq_lock);
   2982  1.131   thorpej }
   2983  1.143   thorpej 
   2984  1.143   thorpej /*
   2985  1.143   thorpej  * Initialize a klist.
   2986  1.143   thorpej  */
   2987  1.143   thorpej void
   2988  1.143   thorpej klist_init(struct klist *list)
   2989  1.143   thorpej {
   2990  1.143   thorpej 	SLIST_INIT(list);
   2991  1.143   thorpej }
   2992  1.143   thorpej 
   2993  1.143   thorpej /*
   2994  1.143   thorpej  * Finalize a klist.
   2995  1.143   thorpej  */
   2996  1.143   thorpej void
   2997  1.143   thorpej klist_fini(struct klist *list)
   2998  1.143   thorpej {
   2999  1.145   thorpej 	struct knote *kn;
   3000  1.145   thorpej 
   3001  1.145   thorpej 	/*
   3002  1.145   thorpej 	 * Neuter all existing knotes on the klist because the list is
   3003  1.145   thorpej 	 * being destroyed.  The caller has guaranteed that no additional
   3004  1.145   thorpej 	 * knotes will be added to the list, that the backing object's
   3005  1.145   thorpej 	 * locks are not held (otherwise there is a locking order issue
   3006  1.145   thorpej 	 * with acquiring the knote foplock ), and that we can traverse
   3007  1.145   thorpej 	 * the list safely in this state.
   3008  1.145   thorpej 	 */
   3009  1.145   thorpej 	SLIST_FOREACH(kn, list, kn_selnext) {
   3010  1.145   thorpej 		knote_foplock_enter(kn);
   3011  1.145   thorpej 		KASSERT(kn->kn_fop != NULL);
   3012  1.145   thorpej 		if (kn->kn_fop->f_flags & FILTEROP_ISFD) {
   3013  1.145   thorpej 			kn->kn_fop = &nop_fd_filtops;
   3014  1.145   thorpej 		} else {
   3015  1.145   thorpej 			kn->kn_fop = &nop_filtops;
   3016  1.145   thorpej 		}
   3017  1.145   thorpej 		knote_foplock_exit(kn);
   3018  1.145   thorpej 	}
   3019  1.143   thorpej }
   3020  1.143   thorpej 
   3021  1.143   thorpej /*
   3022  1.143   thorpej  * Insert a knote into a klist.
   3023  1.143   thorpej  */
   3024  1.143   thorpej void
   3025  1.143   thorpej klist_insert(struct klist *list, struct knote *kn)
   3026  1.143   thorpej {
   3027  1.143   thorpej 	SLIST_INSERT_HEAD(list, kn, kn_selnext);
   3028  1.143   thorpej }
   3029  1.143   thorpej 
   3030  1.143   thorpej /*
   3031  1.143   thorpej  * Remove a knote from a klist.  Returns true if the last
   3032  1.143   thorpej  * knote was removed and the list is now empty.
   3033  1.143   thorpej  */
   3034  1.143   thorpej bool
   3035  1.143   thorpej klist_remove(struct klist *list, struct knote *kn)
   3036  1.143   thorpej {
   3037  1.143   thorpej 	SLIST_REMOVE(list, kn, knote, kn_selnext);
   3038  1.143   thorpej 	return SLIST_EMPTY(list);
   3039  1.143   thorpej }
   3040