Home | History | Annotate | Line # | Download | only in kern
kern_event.c revision 1.47.6.4
      1  1.47.6.1       mjf /*	$NetBSD: kern_event.c,v 1.47.6.4 2009/01/17 13:29:18 mjf Exp $	*/
      2  1.47.6.1       mjf 
      3  1.47.6.1       mjf /*-
      4  1.47.6.1       mjf  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.47.6.1       mjf  * All rights reserved.
      6  1.47.6.1       mjf  *
      7  1.47.6.1       mjf  * Redistribution and use in source and binary forms, with or without
      8  1.47.6.1       mjf  * modification, are permitted provided that the following conditions
      9  1.47.6.1       mjf  * are met:
     10  1.47.6.1       mjf  * 1. Redistributions of source code must retain the above copyright
     11  1.47.6.1       mjf  *    notice, this list of conditions and the following disclaimer.
     12  1.47.6.1       mjf  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.47.6.1       mjf  *    notice, this list of conditions and the following disclaimer in the
     14  1.47.6.1       mjf  *    documentation and/or other materials provided with the distribution.
     15  1.47.6.1       mjf  *
     16  1.47.6.1       mjf  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.47.6.1       mjf  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.47.6.1       mjf  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.47.6.1       mjf  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.47.6.1       mjf  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.47.6.1       mjf  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.47.6.1       mjf  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.47.6.1       mjf  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.47.6.1       mjf  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.47.6.1       mjf  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.47.6.1       mjf  * POSSIBILITY OF SUCH DAMAGE.
     27  1.47.6.1       mjf  */
     28      1.28    kardel 
     29       1.1     lukem /*-
     30       1.1     lukem  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon (at) FreeBSD.org>
     31       1.1     lukem  * All rights reserved.
     32       1.1     lukem  *
     33       1.1     lukem  * Redistribution and use in source and binary forms, with or without
     34       1.1     lukem  * modification, are permitted provided that the following conditions
     35       1.1     lukem  * are met:
     36       1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     37       1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     38       1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     39       1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     40       1.1     lukem  *    documentation and/or other materials provided with the distribution.
     41       1.1     lukem  *
     42       1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     43       1.1     lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     44       1.1     lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     45       1.1     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     46       1.1     lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     47       1.1     lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     48       1.1     lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     49       1.1     lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     50       1.1     lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     51       1.1     lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     52       1.1     lukem  * SUCH DAMAGE.
     53       1.1     lukem  *
     54  1.47.6.1       mjf  * FreeBSD: src/sys/kern/kern_event.c,v 1.27 2001/07/05 17:10:44 rwatson Exp
     55       1.1     lukem  */
     56      1.14  jdolecek 
     57      1.14  jdolecek #include <sys/cdefs.h>
     58  1.47.6.1       mjf __KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.47.6.4 2009/01/17 13:29:18 mjf Exp $");
     59       1.1     lukem 
     60       1.1     lukem #include <sys/param.h>
     61       1.1     lukem #include <sys/systm.h>
     62       1.1     lukem #include <sys/kernel.h>
     63       1.1     lukem #include <sys/proc.h>
     64       1.1     lukem #include <sys/file.h>
     65       1.3  jdolecek #include <sys/select.h>
     66       1.1     lukem #include <sys/queue.h>
     67       1.1     lukem #include <sys/event.h>
     68       1.1     lukem #include <sys/eventvar.h>
     69       1.1     lukem #include <sys/poll.h>
     70  1.47.6.1       mjf #include <sys/kmem.h>
     71       1.1     lukem #include <sys/stat.h>
     72       1.3  jdolecek #include <sys/filedesc.h>
     73       1.3  jdolecek #include <sys/syscallargs.h>
     74      1.27      elad #include <sys/kauth.h>
     75      1.40        ad #include <sys/conf.h>
     76  1.47.6.1       mjf #include <sys/atomic.h>
     77       1.1     lukem 
     78  1.47.6.1       mjf static int	kqueue_scan(file_t *, size_t, struct kevent *,
     79  1.47.6.1       mjf 			    const struct timespec *, register_t *,
     80  1.47.6.1       mjf 			    const struct kevent_ops *, struct kevent *,
     81  1.47.6.1       mjf 			    size_t);
     82  1.47.6.1       mjf static int	kqueue_ioctl(file_t *, u_long, void *);
     83  1.47.6.1       mjf static int	kqueue_fcntl(file_t *, u_int, void *);
     84  1.47.6.1       mjf static int	kqueue_poll(file_t *, int);
     85  1.47.6.1       mjf static int	kqueue_kqfilter(file_t *, struct knote *);
     86  1.47.6.1       mjf static int	kqueue_stat(file_t *, struct stat *);
     87  1.47.6.1       mjf static int	kqueue_close(file_t *);
     88  1.47.6.1       mjf static int	kqueue_register(struct kqueue *, struct kevent *);
     89  1.47.6.1       mjf static void	kqueue_doclose(struct kqueue *, struct klist *, int);
     90  1.47.6.1       mjf 
     91  1.47.6.1       mjf static void	knote_detach(struct knote *, filedesc_t *fdp, bool);
     92  1.47.6.1       mjf static void	knote_enqueue(struct knote *);
     93  1.47.6.1       mjf static void	knote_activate(struct knote *);
     94  1.47.6.1       mjf 
     95  1.47.6.1       mjf static void	filt_kqdetach(struct knote *);
     96  1.47.6.1       mjf static int	filt_kqueue(struct knote *, long hint);
     97  1.47.6.1       mjf static int	filt_procattach(struct knote *);
     98  1.47.6.1       mjf static void	filt_procdetach(struct knote *);
     99  1.47.6.1       mjf static int	filt_proc(struct knote *, long hint);
    100  1.47.6.1       mjf static int	filt_fileattach(struct knote *);
    101  1.47.6.1       mjf static void	filt_timerexpire(void *x);
    102  1.47.6.1       mjf static int	filt_timerattach(struct knote *);
    103  1.47.6.1       mjf static void	filt_timerdetach(struct knote *);
    104  1.47.6.1       mjf static int	filt_timer(struct knote *, long hint);
    105       1.1     lukem 
    106      1.21  christos static const struct fileops kqueueops = {
    107  1.47.6.1       mjf 	(void *)enxio, (void *)enxio, kqueue_ioctl, kqueue_fcntl, kqueue_poll,
    108       1.3  jdolecek 	kqueue_stat, kqueue_close, kqueue_kqfilter
    109       1.1     lukem };
    110       1.1     lukem 
    111       1.3  jdolecek static const struct filterops kqread_filtops =
    112       1.1     lukem 	{ 1, NULL, filt_kqdetach, filt_kqueue };
    113       1.3  jdolecek static const struct filterops proc_filtops =
    114       1.1     lukem 	{ 0, filt_procattach, filt_procdetach, filt_proc };
    115       1.3  jdolecek static const struct filterops file_filtops =
    116       1.1     lukem 	{ 1, filt_fileattach, NULL, NULL };
    117      1.26      yamt static const struct filterops timer_filtops =
    118       1.8  jdolecek 	{ 0, filt_timerattach, filt_timerdetach, filt_timer };
    119       1.1     lukem 
    120  1.47.6.1       mjf static u_int	kq_ncallouts = 0;
    121       1.8  jdolecek static int	kq_calloutmax = (4 * 1024);
    122       1.7   thorpej 
    123       1.1     lukem #define	KN_HASHSIZE		64		/* XXX should be tunable */
    124       1.3  jdolecek #define	KN_HASH(val, mask)	(((val) ^ (val >> 8)) & (mask))
    125       1.1     lukem 
    126       1.3  jdolecek extern const struct filterops sig_filtops;
    127       1.1     lukem 
    128       1.1     lukem /*
    129       1.1     lukem  * Table for for all system-defined filters.
    130       1.3  jdolecek  * These should be listed in the numeric order of the EVFILT_* defines.
    131       1.3  jdolecek  * If filtops is NULL, the filter isn't implemented in NetBSD.
    132       1.3  jdolecek  * End of list is when name is NULL.
    133  1.47.6.1       mjf  *
    134  1.47.6.1       mjf  * Note that 'refcnt' is meaningless for built-in filters.
    135       1.1     lukem  */
    136       1.3  jdolecek struct kfilter {
    137  1.47.6.1       mjf 	const char	*name;		/* name of filter */
    138  1.47.6.1       mjf 	uint32_t	filter;		/* id of filter */
    139  1.47.6.1       mjf 	unsigned	refcnt;		/* reference count */
    140       1.3  jdolecek 	const struct filterops *filtops;/* operations for filter */
    141  1.47.6.1       mjf 	size_t		namelen;	/* length of name string */
    142       1.3  jdolecek };
    143       1.3  jdolecek 
    144  1.47.6.1       mjf /* System defined filters */
    145  1.47.6.1       mjf static struct kfilter sys_kfilters[] = {
    146  1.47.6.1       mjf 	{ "EVFILT_READ",	EVFILT_READ,	0, &file_filtops, 0 },
    147  1.47.6.1       mjf 	{ "EVFILT_WRITE",	EVFILT_WRITE,	0, &file_filtops, 0, },
    148  1.47.6.1       mjf 	{ "EVFILT_AIO",		EVFILT_AIO,	0, NULL, 0 },
    149  1.47.6.1       mjf 	{ "EVFILT_VNODE",	EVFILT_VNODE,	0, &file_filtops, 0 },
    150  1.47.6.1       mjf 	{ "EVFILT_PROC",	EVFILT_PROC,	0, &proc_filtops, 0 },
    151  1.47.6.1       mjf 	{ "EVFILT_SIGNAL",	EVFILT_SIGNAL,	0, &sig_filtops, 0 },
    152  1.47.6.1       mjf 	{ "EVFILT_TIMER",	EVFILT_TIMER,	0, &timer_filtops, 0 },
    153  1.47.6.1       mjf 	{ NULL,			0,		0, NULL, 0 },
    154       1.1     lukem };
    155       1.1     lukem 
    156  1.47.6.1       mjf /* User defined kfilters */
    157       1.3  jdolecek static struct kfilter	*user_kfilters;		/* array */
    158       1.3  jdolecek static int		user_kfilterc;		/* current offset */
    159       1.3  jdolecek static int		user_kfiltermaxc;	/* max size so far */
    160  1.47.6.1       mjf static size_t		user_kfiltersz;		/* size of allocated memory */
    161  1.47.6.1       mjf 
    162  1.47.6.1       mjf /* Locks */
    163  1.47.6.1       mjf static krwlock_t	kqueue_filter_lock;	/* lock on filter lists */
    164  1.47.6.1       mjf static kmutex_t		kqueue_misc_lock;	/* miscellaneous */
    165  1.47.6.1       mjf 
    166  1.47.6.1       mjf /*
    167  1.47.6.1       mjf  * Initialize the kqueue subsystem.
    168  1.47.6.1       mjf  */
    169  1.47.6.1       mjf void
    170  1.47.6.1       mjf kqueue_init(void)
    171  1.47.6.1       mjf {
    172  1.47.6.1       mjf 
    173  1.47.6.1       mjf 	rw_init(&kqueue_filter_lock);
    174  1.47.6.1       mjf 	mutex_init(&kqueue_misc_lock, MUTEX_DEFAULT, IPL_NONE);
    175  1.47.6.1       mjf }
    176       1.3  jdolecek 
    177       1.3  jdolecek /*
    178       1.3  jdolecek  * Find kfilter entry by name, or NULL if not found.
    179       1.3  jdolecek  */
    180  1.47.6.1       mjf static struct kfilter *
    181       1.3  jdolecek kfilter_byname_sys(const char *name)
    182       1.3  jdolecek {
    183       1.3  jdolecek 	int i;
    184       1.3  jdolecek 
    185  1.47.6.1       mjf 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    186  1.47.6.1       mjf 
    187       1.3  jdolecek 	for (i = 0; sys_kfilters[i].name != NULL; i++) {
    188       1.3  jdolecek 		if (strcmp(name, sys_kfilters[i].name) == 0)
    189  1.47.6.1       mjf 			return &sys_kfilters[i];
    190       1.3  jdolecek 	}
    191  1.47.6.1       mjf 	return NULL;
    192       1.3  jdolecek }
    193       1.3  jdolecek 
    194       1.3  jdolecek static struct kfilter *
    195       1.3  jdolecek kfilter_byname_user(const char *name)
    196       1.3  jdolecek {
    197       1.3  jdolecek 	int i;
    198       1.3  jdolecek 
    199  1.47.6.1       mjf 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    200  1.47.6.1       mjf 
    201      1.31     seanb 	/* user filter slots have a NULL name if previously deregistered */
    202      1.31     seanb 	for (i = 0; i < user_kfilterc ; i++) {
    203      1.31     seanb 		if (user_kfilters[i].name != NULL &&
    204       1.3  jdolecek 		    strcmp(name, user_kfilters[i].name) == 0)
    205  1.47.6.1       mjf 			return &user_kfilters[i];
    206       1.3  jdolecek 	}
    207  1.47.6.1       mjf 	return NULL;
    208       1.3  jdolecek }
    209       1.3  jdolecek 
    210  1.47.6.1       mjf static struct kfilter *
    211       1.3  jdolecek kfilter_byname(const char *name)
    212       1.3  jdolecek {
    213  1.47.6.1       mjf 	struct kfilter *kfilter;
    214  1.47.6.1       mjf 
    215  1.47.6.1       mjf 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    216       1.3  jdolecek 
    217       1.3  jdolecek 	if ((kfilter = kfilter_byname_sys(name)) != NULL)
    218  1.47.6.1       mjf 		return kfilter;
    219       1.3  jdolecek 
    220  1.47.6.1       mjf 	return kfilter_byname_user(name);
    221       1.3  jdolecek }
    222       1.3  jdolecek 
    223       1.3  jdolecek /*
    224       1.3  jdolecek  * Find kfilter entry by filter id, or NULL if not found.
    225       1.3  jdolecek  * Assumes entries are indexed in filter id order, for speed.
    226       1.3  jdolecek  */
    227  1.47.6.1       mjf static struct kfilter *
    228       1.3  jdolecek kfilter_byfilter(uint32_t filter)
    229       1.3  jdolecek {
    230  1.47.6.1       mjf 	struct kfilter *kfilter;
    231  1.47.6.1       mjf 
    232  1.47.6.1       mjf 	KASSERT(rw_lock_held(&kqueue_filter_lock));
    233       1.3  jdolecek 
    234       1.3  jdolecek 	if (filter < EVFILT_SYSCOUNT)	/* it's a system filter */
    235       1.3  jdolecek 		kfilter = &sys_kfilters[filter];
    236       1.3  jdolecek 	else if (user_kfilters != NULL &&
    237       1.3  jdolecek 	    filter < EVFILT_SYSCOUNT + user_kfilterc)
    238       1.3  jdolecek 					/* it's a user filter */
    239       1.3  jdolecek 		kfilter = &user_kfilters[filter - EVFILT_SYSCOUNT];
    240       1.3  jdolecek 	else
    241       1.3  jdolecek 		return (NULL);		/* out of range */
    242       1.3  jdolecek 	KASSERT(kfilter->filter == filter);	/* sanity check! */
    243       1.3  jdolecek 	return (kfilter);
    244       1.3  jdolecek }
    245       1.3  jdolecek 
    246       1.3  jdolecek /*
    247       1.3  jdolecek  * Register a new kfilter. Stores the entry in user_kfilters.
    248       1.3  jdolecek  * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
    249       1.3  jdolecek  * If retfilter != NULL, the new filterid is returned in it.
    250       1.3  jdolecek  */
    251       1.3  jdolecek int
    252       1.3  jdolecek kfilter_register(const char *name, const struct filterops *filtops,
    253  1.47.6.1       mjf 		 int *retfilter)
    254       1.1     lukem {
    255       1.3  jdolecek 	struct kfilter *kfilter;
    256  1.47.6.1       mjf 	size_t len;
    257      1.31     seanb 	int i;
    258       1.3  jdolecek 
    259       1.3  jdolecek 	if (name == NULL || name[0] == '\0' || filtops == NULL)
    260       1.3  jdolecek 		return (EINVAL);	/* invalid args */
    261  1.47.6.1       mjf 
    262  1.47.6.1       mjf 	rw_enter(&kqueue_filter_lock, RW_WRITER);
    263  1.47.6.1       mjf 	if (kfilter_byname(name) != NULL) {
    264  1.47.6.1       mjf 		rw_exit(&kqueue_filter_lock);
    265       1.3  jdolecek 		return (EEXIST);	/* already exists */
    266  1.47.6.1       mjf 	}
    267  1.47.6.1       mjf 	if (user_kfilterc > 0xffffffff - EVFILT_SYSCOUNT) {
    268  1.47.6.1       mjf 		rw_exit(&kqueue_filter_lock);
    269       1.3  jdolecek 		return (EINVAL);	/* too many */
    270  1.47.6.1       mjf 	}
    271       1.3  jdolecek 
    272      1.31     seanb 	for (i = 0; i < user_kfilterc; i++) {
    273      1.31     seanb 		kfilter = &user_kfilters[i];
    274      1.31     seanb 		if (kfilter->name == NULL) {
    275      1.31     seanb 			/* Previously deregistered slot.  Reuse. */
    276      1.31     seanb 			goto reuse;
    277      1.31     seanb 		}
    278      1.31     seanb 	}
    279      1.31     seanb 
    280       1.3  jdolecek 	/* check if need to grow user_kfilters */
    281       1.3  jdolecek 	if (user_kfilterc + 1 > user_kfiltermaxc) {
    282  1.47.6.1       mjf 		/* Grow in KFILTER_EXTENT chunks. */
    283       1.3  jdolecek 		user_kfiltermaxc += KFILTER_EXTENT;
    284  1.47.6.1       mjf 		len = user_kfiltermaxc * sizeof(struct filter *);
    285  1.47.6.1       mjf 		kfilter = kmem_alloc(len, KM_SLEEP);
    286  1.47.6.1       mjf 		memset((char *)kfilter + user_kfiltersz, 0, len - user_kfiltersz);
    287  1.47.6.1       mjf 		if (user_kfilters != NULL) {
    288  1.47.6.1       mjf 			memcpy(kfilter, user_kfilters, user_kfiltersz);
    289  1.47.6.1       mjf 			kmem_free(user_kfilters, user_kfiltersz);
    290  1.47.6.1       mjf 		}
    291  1.47.6.1       mjf 		user_kfiltersz = len;
    292       1.3  jdolecek 		user_kfilters = kfilter;
    293       1.3  jdolecek 	}
    294      1.31     seanb 	/* Adding new slot */
    295      1.31     seanb 	kfilter = &user_kfilters[user_kfilterc++];
    296      1.31     seanb reuse:
    297  1.47.6.1       mjf 	kfilter->namelen = strlen(name) + 1;
    298  1.47.6.1       mjf 	kfilter->name = kmem_alloc(kfilter->namelen, KM_SLEEP);
    299  1.47.6.1       mjf 	memcpy(__UNCONST(kfilter->name), name, kfilter->namelen);
    300       1.3  jdolecek 
    301      1.31     seanb 	kfilter->filter = (kfilter - user_kfilters) + EVFILT_SYSCOUNT;
    302       1.3  jdolecek 
    303  1.47.6.1       mjf 	kfilter->filtops = kmem_alloc(sizeof(*filtops), KM_SLEEP);
    304  1.47.6.1       mjf 	memcpy(__UNCONST(kfilter->filtops), filtops, sizeof(*filtops));
    305       1.3  jdolecek 
    306       1.3  jdolecek 	if (retfilter != NULL)
    307      1.31     seanb 		*retfilter = kfilter->filter;
    308  1.47.6.1       mjf 	rw_exit(&kqueue_filter_lock);
    309  1.47.6.1       mjf 
    310       1.3  jdolecek 	return (0);
    311       1.1     lukem }
    312       1.1     lukem 
    313       1.3  jdolecek /*
    314       1.3  jdolecek  * Unregister a kfilter previously registered with kfilter_register.
    315       1.3  jdolecek  * This retains the filter id, but clears the name and frees filtops (filter
    316       1.3  jdolecek  * operations), so that the number isn't reused during a boot.
    317       1.3  jdolecek  * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
    318       1.3  jdolecek  */
    319       1.3  jdolecek int
    320       1.3  jdolecek kfilter_unregister(const char *name)
    321       1.1     lukem {
    322       1.3  jdolecek 	struct kfilter *kfilter;
    323       1.3  jdolecek 
    324       1.3  jdolecek 	if (name == NULL || name[0] == '\0')
    325       1.3  jdolecek 		return (EINVAL);	/* invalid name */
    326       1.3  jdolecek 
    327  1.47.6.1       mjf 	rw_enter(&kqueue_filter_lock, RW_WRITER);
    328  1.47.6.1       mjf 	if (kfilter_byname_sys(name) != NULL) {
    329  1.47.6.1       mjf 		rw_exit(&kqueue_filter_lock);
    330       1.3  jdolecek 		return (EINVAL);	/* can't detach system filters */
    331  1.47.6.1       mjf 	}
    332       1.1     lukem 
    333       1.3  jdolecek 	kfilter = kfilter_byname_user(name);
    334  1.47.6.1       mjf 	if (kfilter == NULL) {
    335  1.47.6.1       mjf 		rw_exit(&kqueue_filter_lock);
    336       1.3  jdolecek 		return (ENOENT);
    337  1.47.6.1       mjf 	}
    338  1.47.6.1       mjf 	if (kfilter->refcnt != 0) {
    339  1.47.6.1       mjf 		rw_exit(&kqueue_filter_lock);
    340  1.47.6.1       mjf 		return (EBUSY);
    341  1.47.6.1       mjf 	}
    342       1.1     lukem 
    343  1.47.6.1       mjf 	/* Cast away const (but we know it's safe. */
    344  1.47.6.1       mjf 	kmem_free(__UNCONST(kfilter->name), kfilter->namelen);
    345      1.31     seanb 	kfilter->name = NULL;	/* mark as `not implemented' */
    346      1.31     seanb 
    347       1.3  jdolecek 	if (kfilter->filtops != NULL) {
    348  1.47.6.1       mjf 		/* Cast away const (but we know it's safe. */
    349  1.47.6.1       mjf 		kmem_free(__UNCONST(kfilter->filtops),
    350  1.47.6.1       mjf 		    sizeof(*kfilter->filtops));
    351       1.3  jdolecek 		kfilter->filtops = NULL; /* mark as `not implemented' */
    352       1.3  jdolecek 	}
    353  1.47.6.1       mjf 	rw_exit(&kqueue_filter_lock);
    354  1.47.6.1       mjf 
    355       1.1     lukem 	return (0);
    356       1.1     lukem }
    357       1.1     lukem 
    358       1.3  jdolecek 
    359       1.3  jdolecek /*
    360       1.3  jdolecek  * Filter attach method for EVFILT_READ and EVFILT_WRITE on normal file
    361  1.47.6.1       mjf  * descriptors. Calls fileops kqfilter method for given file descriptor.
    362       1.3  jdolecek  */
    363       1.3  jdolecek static int
    364       1.3  jdolecek filt_fileattach(struct knote *kn)
    365       1.3  jdolecek {
    366  1.47.6.1       mjf 	file_t *fp;
    367  1.47.6.1       mjf 
    368  1.47.6.1       mjf 	fp = kn->kn_obj;
    369       1.3  jdolecek 
    370  1.47.6.1       mjf 	return (*fp->f_ops->fo_kqfilter)(fp, kn);
    371       1.3  jdolecek }
    372       1.3  jdolecek 
    373       1.3  jdolecek /*
    374       1.3  jdolecek  * Filter detach method for EVFILT_READ on kqueue descriptor.
    375       1.3  jdolecek  */
    376       1.1     lukem static void
    377       1.1     lukem filt_kqdetach(struct knote *kn)
    378       1.1     lukem {
    379       1.3  jdolecek 	struct kqueue *kq;
    380       1.1     lukem 
    381  1.47.6.1       mjf 	kq = ((file_t *)kn->kn_obj)->f_data;
    382  1.47.6.1       mjf 
    383  1.47.6.1       mjf 	mutex_spin_enter(&kq->kq_lock);
    384       1.5  christos 	SLIST_REMOVE(&kq->kq_sel.sel_klist, kn, knote, kn_selnext);
    385  1.47.6.1       mjf 	mutex_spin_exit(&kq->kq_lock);
    386       1.1     lukem }
    387       1.1     lukem 
    388       1.3  jdolecek /*
    389       1.3  jdolecek  * Filter event method for EVFILT_READ on kqueue descriptor.
    390       1.3  jdolecek  */
    391       1.1     lukem /*ARGSUSED*/
    392       1.1     lukem static int
    393      1.33      yamt filt_kqueue(struct knote *kn, long hint)
    394       1.1     lukem {
    395       1.3  jdolecek 	struct kqueue *kq;
    396  1.47.6.1       mjf 	int rv;
    397  1.47.6.1       mjf 
    398  1.47.6.1       mjf 	kq = ((file_t *)kn->kn_obj)->f_data;
    399       1.1     lukem 
    400  1.47.6.1       mjf 	if (hint != NOTE_SUBMIT)
    401  1.47.6.1       mjf 		mutex_spin_enter(&kq->kq_lock);
    402       1.1     lukem 	kn->kn_data = kq->kq_count;
    403  1.47.6.1       mjf 	rv = (kn->kn_data > 0);
    404  1.47.6.1       mjf 	if (hint != NOTE_SUBMIT)
    405  1.47.6.1       mjf 		mutex_spin_exit(&kq->kq_lock);
    406  1.47.6.1       mjf 
    407  1.47.6.1       mjf 	return rv;
    408       1.1     lukem }
    409       1.1     lukem 
    410       1.3  jdolecek /*
    411       1.3  jdolecek  * Filter attach method for EVFILT_PROC.
    412       1.3  jdolecek  */
    413       1.1     lukem static int
    414       1.1     lukem filt_procattach(struct knote *kn)
    415       1.1     lukem {
    416      1.30        ad 	struct proc *p, *curp;
    417      1.30        ad 	struct lwp *curl;
    418      1.30        ad 
    419      1.30        ad 	curl = curlwp;
    420      1.30        ad 	curp = curl->l_proc;
    421       1.1     lukem 
    422  1.47.6.2       mjf 	mutex_enter(proc_lock);
    423  1.47.6.1       mjf 	p = p_find(kn->kn_id, PFIND_LOCKED);
    424  1.47.6.1       mjf 	if (p == NULL) {
    425  1.47.6.2       mjf 		mutex_exit(proc_lock);
    426  1.47.6.1       mjf 		return ESRCH;
    427  1.47.6.1       mjf 	}
    428       1.3  jdolecek 
    429       1.3  jdolecek 	/*
    430       1.3  jdolecek 	 * Fail if it's not owned by you, or the last exec gave us
    431       1.3  jdolecek 	 * setuid/setgid privs (unless you're root).
    432       1.3  jdolecek 	 */
    433  1.47.6.2       mjf 	mutex_enter(p->p_lock);
    434  1.47.6.2       mjf 	mutex_exit(proc_lock);
    435      1.46      elad 	if (kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_KEVENT_FILTER,
    436  1.47.6.1       mjf 	    p, NULL, NULL, NULL) != 0) {
    437  1.47.6.2       mjf 	    	mutex_exit(p->p_lock);
    438  1.47.6.1       mjf 		return EACCES;
    439  1.47.6.1       mjf 	}
    440       1.1     lukem 
    441  1.47.6.1       mjf 	kn->kn_obj = p;
    442       1.3  jdolecek 	kn->kn_flags |= EV_CLEAR;	/* automatically set */
    443       1.1     lukem 
    444       1.1     lukem 	/*
    445       1.1     lukem 	 * internal flag indicating registration done by kernel
    446       1.1     lukem 	 */
    447       1.1     lukem 	if (kn->kn_flags & EV_FLAG1) {
    448       1.3  jdolecek 		kn->kn_data = kn->kn_sdata;	/* ppid */
    449       1.1     lukem 		kn->kn_fflags = NOTE_CHILD;
    450       1.1     lukem 		kn->kn_flags &= ~EV_FLAG1;
    451       1.1     lukem 	}
    452       1.1     lukem 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
    453  1.47.6.2       mjf     	mutex_exit(p->p_lock);
    454       1.1     lukem 
    455  1.47.6.1       mjf 	return 0;
    456       1.1     lukem }
    457       1.1     lukem 
    458       1.1     lukem /*
    459       1.3  jdolecek  * Filter detach method for EVFILT_PROC.
    460       1.3  jdolecek  *
    461       1.1     lukem  * The knote may be attached to a different process, which may exit,
    462       1.1     lukem  * leaving nothing for the knote to be attached to.  So when the process
    463       1.1     lukem  * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
    464       1.1     lukem  * it will be deleted when read out.  However, as part of the knote deletion,
    465       1.1     lukem  * this routine is called, so a check is needed to avoid actually performing
    466       1.3  jdolecek  * a detach, because the original process might not exist any more.
    467       1.1     lukem  */
    468       1.1     lukem static void
    469       1.1     lukem filt_procdetach(struct knote *kn)
    470       1.1     lukem {
    471       1.3  jdolecek 	struct proc *p;
    472       1.1     lukem 
    473       1.1     lukem 	if (kn->kn_status & KN_DETACHED)
    474       1.1     lukem 		return;
    475       1.1     lukem 
    476  1.47.6.1       mjf 	p = kn->kn_obj;
    477       1.3  jdolecek 
    478  1.47.6.2       mjf 	mutex_enter(p->p_lock);
    479       1.1     lukem 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
    480  1.47.6.2       mjf 	mutex_exit(p->p_lock);
    481       1.1     lukem }
    482       1.1     lukem 
    483       1.3  jdolecek /*
    484       1.3  jdolecek  * Filter event method for EVFILT_PROC.
    485       1.3  jdolecek  */
    486       1.1     lukem static int
    487       1.1     lukem filt_proc(struct knote *kn, long hint)
    488       1.1     lukem {
    489  1.47.6.1       mjf 	u_int event, fflag;
    490  1.47.6.1       mjf 	struct kevent kev;
    491  1.47.6.1       mjf 	struct kqueue *kq;
    492  1.47.6.1       mjf 	int error;
    493       1.1     lukem 
    494       1.1     lukem 	event = (u_int)hint & NOTE_PCTRLMASK;
    495  1.47.6.1       mjf 	kq = kn->kn_kq;
    496  1.47.6.1       mjf 	fflag = 0;
    497       1.1     lukem 
    498  1.47.6.1       mjf 	/* If the user is interested in this event, record it. */
    499       1.1     lukem 	if (kn->kn_sfflags & event)
    500  1.47.6.1       mjf 		fflag |= event;
    501       1.1     lukem 
    502       1.1     lukem 	if (event == NOTE_EXIT) {
    503       1.3  jdolecek 		/*
    504  1.47.6.1       mjf 		 * Process is gone, so flag the event as finished.
    505  1.47.6.1       mjf 		 *
    506       1.3  jdolecek 		 * Detach the knote from watched process and mark
    507       1.3  jdolecek 		 * it as such. We can't leave this to kqueue_scan(),
    508       1.3  jdolecek 		 * since the process might not exist by then. And we
    509       1.3  jdolecek 		 * have to do this now, since psignal KNOTE() is called
    510       1.3  jdolecek 		 * also for zombies and we might end up reading freed
    511       1.3  jdolecek 		 * memory if the kevent would already be picked up
    512      1.22     perry 		 * and knote g/c'ed.
    513       1.3  jdolecek 		 */
    514  1.47.6.1       mjf 		filt_procdetach(kn);
    515       1.3  jdolecek 
    516  1.47.6.1       mjf 		mutex_spin_enter(&kq->kq_lock);
    517  1.47.6.1       mjf 		kn->kn_status |= KN_DETACHED;
    518       1.3  jdolecek 		/* Mark as ONESHOT, so that the knote it g/c'ed when read */
    519      1.22     perry 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
    520  1.47.6.1       mjf 		kn->kn_fflags |= fflag;
    521  1.47.6.1       mjf 		mutex_spin_exit(&kq->kq_lock);
    522  1.47.6.1       mjf 
    523  1.47.6.1       mjf 		return 1;
    524       1.1     lukem 	}
    525       1.1     lukem 
    526  1.47.6.1       mjf 	mutex_spin_enter(&kq->kq_lock);
    527       1.1     lukem 	if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
    528       1.1     lukem 		/*
    529  1.47.6.1       mjf 		 * Process forked, and user wants to track the new process,
    530  1.47.6.1       mjf 		 * so attach a new knote to it, and immediately report an
    531  1.47.6.1       mjf 		 * event with the parent's pid.  Register knote with new
    532  1.47.6.1       mjf 		 * process.
    533       1.1     lukem 		 */
    534       1.1     lukem 		kev.ident = hint & NOTE_PDATAMASK;	/* pid */
    535       1.1     lukem 		kev.filter = kn->kn_filter;
    536       1.1     lukem 		kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
    537       1.1     lukem 		kev.fflags = kn->kn_sfflags;
    538       1.1     lukem 		kev.data = kn->kn_id;			/* parent */
    539       1.1     lukem 		kev.udata = kn->kn_kevent.udata;	/* preserve udata */
    540  1.47.6.1       mjf 		mutex_spin_exit(&kq->kq_lock);
    541  1.47.6.1       mjf 		error = kqueue_register(kq, &kev);
    542  1.47.6.1       mjf 		mutex_spin_enter(&kq->kq_lock);
    543  1.47.6.1       mjf 		if (error != 0)
    544       1.1     lukem 			kn->kn_fflags |= NOTE_TRACKERR;
    545       1.1     lukem 	}
    546  1.47.6.1       mjf 	kn->kn_fflags |= fflag;
    547  1.47.6.1       mjf 	fflag = kn->kn_fflags;
    548  1.47.6.1       mjf 	mutex_spin_exit(&kq->kq_lock);
    549       1.1     lukem 
    550  1.47.6.1       mjf 	return fflag != 0;
    551       1.8  jdolecek }
    552       1.8  jdolecek 
    553       1.8  jdolecek static void
    554       1.8  jdolecek filt_timerexpire(void *knx)
    555       1.8  jdolecek {
    556       1.8  jdolecek 	struct knote *kn = knx;
    557       1.8  jdolecek 	int tticks;
    558       1.8  jdolecek 
    559  1.47.6.1       mjf 	mutex_enter(&kqueue_misc_lock);
    560       1.8  jdolecek 	kn->kn_data++;
    561  1.47.6.1       mjf 	knote_activate(kn);
    562       1.8  jdolecek 	if ((kn->kn_flags & EV_ONESHOT) == 0) {
    563       1.8  jdolecek 		tticks = mstohz(kn->kn_sdata);
    564      1.39        ad 		callout_schedule((callout_t *)kn->kn_hook, tticks);
    565       1.8  jdolecek 	}
    566  1.47.6.1       mjf 	mutex_exit(&kqueue_misc_lock);
    567       1.8  jdolecek }
    568       1.8  jdolecek 
    569       1.8  jdolecek /*
    570       1.8  jdolecek  * data contains amount of time to sleep, in milliseconds
    571      1.22     perry  */
    572       1.8  jdolecek static int
    573       1.8  jdolecek filt_timerattach(struct knote *kn)
    574       1.8  jdolecek {
    575      1.39        ad 	callout_t *calloutp;
    576  1.47.6.1       mjf 	struct kqueue *kq;
    577       1.8  jdolecek 	int tticks;
    578       1.8  jdolecek 
    579       1.8  jdolecek 	tticks = mstohz(kn->kn_sdata);
    580       1.8  jdolecek 
    581       1.8  jdolecek 	/* if the supplied value is under our resolution, use 1 tick */
    582       1.8  jdolecek 	if (tticks == 0) {
    583       1.8  jdolecek 		if (kn->kn_sdata == 0)
    584  1.47.6.1       mjf 			return EINVAL;
    585       1.8  jdolecek 		tticks = 1;
    586       1.8  jdolecek 	}
    587       1.8  jdolecek 
    588  1.47.6.1       mjf 	if (atomic_inc_uint_nv(&kq_ncallouts) >= kq_calloutmax ||
    589  1.47.6.1       mjf 	    (calloutp = kmem_alloc(sizeof(*calloutp), KM_NOSLEEP)) == NULL) {
    590  1.47.6.1       mjf 		atomic_dec_uint(&kq_ncallouts);
    591  1.47.6.1       mjf 		return ENOMEM;
    592  1.47.6.1       mjf 	}
    593  1.47.6.2       mjf 	callout_init(calloutp, CALLOUT_MPSAFE);
    594  1.47.6.1       mjf 
    595  1.47.6.1       mjf 	kq = kn->kn_kq;
    596  1.47.6.1       mjf 	mutex_spin_enter(&kq->kq_lock);
    597  1.47.6.1       mjf 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
    598       1.8  jdolecek 	kn->kn_hook = calloutp;
    599  1.47.6.1       mjf 	mutex_spin_exit(&kq->kq_lock);
    600  1.47.6.1       mjf 
    601  1.47.6.1       mjf 	callout_reset(calloutp, tticks, filt_timerexpire, kn);
    602       1.8  jdolecek 
    603       1.8  jdolecek 	return (0);
    604       1.8  jdolecek }
    605       1.8  jdolecek 
    606       1.8  jdolecek static void
    607       1.8  jdolecek filt_timerdetach(struct knote *kn)
    608       1.8  jdolecek {
    609      1.39        ad 	callout_t *calloutp;
    610       1.8  jdolecek 
    611      1.39        ad 	calloutp = (callout_t *)kn->kn_hook;
    612  1.47.6.2       mjf 	callout_halt(calloutp, NULL);
    613      1.39        ad 	callout_destroy(calloutp);
    614  1.47.6.1       mjf 	kmem_free(calloutp, sizeof(*calloutp));
    615  1.47.6.1       mjf 	atomic_dec_uint(&kq_ncallouts);
    616       1.8  jdolecek }
    617       1.8  jdolecek 
    618       1.8  jdolecek static int
    619      1.33      yamt filt_timer(struct knote *kn, long hint)
    620       1.8  jdolecek {
    621  1.47.6.1       mjf 	int rv;
    622  1.47.6.1       mjf 
    623  1.47.6.1       mjf 	mutex_enter(&kqueue_misc_lock);
    624  1.47.6.1       mjf 	rv = (kn->kn_data != 0);
    625  1.47.6.1       mjf 	mutex_exit(&kqueue_misc_lock);
    626  1.47.6.1       mjf 
    627  1.47.6.1       mjf 	return rv;
    628       1.1     lukem }
    629       1.1     lukem 
    630       1.3  jdolecek /*
    631       1.3  jdolecek  * filt_seltrue:
    632       1.3  jdolecek  *
    633       1.3  jdolecek  *	This filter "event" routine simulates seltrue().
    634       1.3  jdolecek  */
    635       1.1     lukem int
    636      1.33      yamt filt_seltrue(struct knote *kn, long hint)
    637       1.1     lukem {
    638       1.1     lukem 
    639       1.3  jdolecek 	/*
    640       1.3  jdolecek 	 * We don't know how much data can be read/written,
    641       1.3  jdolecek 	 * but we know that it *can* be.  This is about as
    642       1.3  jdolecek 	 * good as select/poll does as well.
    643       1.3  jdolecek 	 */
    644       1.3  jdolecek 	kn->kn_data = 0;
    645       1.3  jdolecek 	return (1);
    646       1.3  jdolecek }
    647       1.3  jdolecek 
    648       1.3  jdolecek /*
    649       1.3  jdolecek  * This provides full kqfilter entry for device switch tables, which
    650       1.3  jdolecek  * has same effect as filter using filt_seltrue() as filter method.
    651       1.3  jdolecek  */
    652       1.3  jdolecek static void
    653      1.33      yamt filt_seltruedetach(struct knote *kn)
    654       1.3  jdolecek {
    655       1.3  jdolecek 	/* Nothing to do */
    656       1.3  jdolecek }
    657       1.3  jdolecek 
    658      1.42     pooka const struct filterops seltrue_filtops =
    659       1.3  jdolecek 	{ 1, NULL, filt_seltruedetach, filt_seltrue };
    660       1.3  jdolecek 
    661       1.3  jdolecek int
    662      1.33      yamt seltrue_kqfilter(dev_t dev, struct knote *kn)
    663       1.3  jdolecek {
    664       1.3  jdolecek 	switch (kn->kn_filter) {
    665       1.3  jdolecek 	case EVFILT_READ:
    666       1.3  jdolecek 	case EVFILT_WRITE:
    667       1.3  jdolecek 		kn->kn_fop = &seltrue_filtops;
    668       1.3  jdolecek 		break;
    669       1.3  jdolecek 	default:
    670      1.43     pooka 		return (EINVAL);
    671       1.3  jdolecek 	}
    672       1.3  jdolecek 
    673       1.3  jdolecek 	/* Nothing more to do */
    674       1.3  jdolecek 	return (0);
    675       1.3  jdolecek }
    676       1.3  jdolecek 
    677       1.3  jdolecek /*
    678       1.3  jdolecek  * kqueue(2) system call.
    679       1.3  jdolecek  */
    680       1.3  jdolecek int
    681      1.44       dsl sys_kqueue(struct lwp *l, const void *v, register_t *retval)
    682       1.3  jdolecek {
    683  1.47.6.1       mjf 	struct kqueue *kq;
    684  1.47.6.1       mjf 	file_t *fp;
    685  1.47.6.1       mjf 	int fd, error;
    686       1.3  jdolecek 
    687  1.47.6.1       mjf 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    688  1.47.6.1       mjf 		return error;
    689       1.1     lukem 	fp->f_flag = FREAD | FWRITE;
    690       1.1     lukem 	fp->f_type = DTYPE_KQUEUE;
    691       1.1     lukem 	fp->f_ops = &kqueueops;
    692  1.47.6.1       mjf 	kq = kmem_zalloc(sizeof(*kq), KM_SLEEP);
    693  1.47.6.1       mjf 	mutex_init(&kq->kq_lock, MUTEX_DEFAULT, IPL_SCHED);
    694  1.47.6.1       mjf 	cv_init(&kq->kq_cv, "kqueue");
    695  1.47.6.1       mjf 	selinit(&kq->kq_sel);
    696       1.1     lukem 	TAILQ_INIT(&kq->kq_head);
    697  1.47.6.1       mjf 	fp->f_data = kq;
    698       1.3  jdolecek 	*retval = fd;
    699  1.47.6.1       mjf 	kq->kq_fdp = curlwp->l_fd;
    700  1.47.6.1       mjf 	fd_affix(curproc, fp, fd);
    701  1.47.6.1       mjf 	return error;
    702       1.1     lukem }
    703       1.1     lukem 
    704       1.3  jdolecek /*
    705       1.3  jdolecek  * kevent(2) system call.
    706       1.3  jdolecek  */
    707  1.47.6.4       mjf int
    708      1.33      yamt kevent_fetch_changes(void *private, const struct kevent *changelist,
    709  1.47.6.4       mjf     struct kevent *changes, size_t index, int n)
    710      1.24      cube {
    711  1.47.6.1       mjf 
    712      1.24      cube 	return copyin(changelist + index, changes, n * sizeof(*changes));
    713      1.24      cube }
    714      1.24      cube 
    715  1.47.6.4       mjf int
    716      1.33      yamt kevent_put_events(void *private, struct kevent *events,
    717  1.47.6.4       mjf     struct kevent *eventlist, size_t index, int n)
    718      1.24      cube {
    719  1.47.6.1       mjf 
    720      1.24      cube 	return copyout(events, eventlist + index, n * sizeof(*events));
    721      1.24      cube }
    722      1.24      cube 
    723      1.24      cube static const struct kevent_ops kevent_native_ops = {
    724  1.47.6.3       mjf 	.keo_private = NULL,
    725  1.47.6.3       mjf 	.keo_fetch_timeout = copyin,
    726  1.47.6.3       mjf 	.keo_fetch_changes = kevent_fetch_changes,
    727  1.47.6.3       mjf 	.keo_put_events = kevent_put_events,
    728      1.24      cube };
    729      1.24      cube 
    730       1.1     lukem int
    731  1.47.6.4       mjf sys___kevent50(struct lwp *l, const struct sys___kevent50_args *uap,
    732  1.47.6.4       mjf     register_t *retval)
    733       1.1     lukem {
    734      1.44       dsl 	/* {
    735       1.3  jdolecek 		syscallarg(int) fd;
    736       1.3  jdolecek 		syscallarg(const struct kevent *) changelist;
    737       1.3  jdolecek 		syscallarg(size_t) nchanges;
    738       1.3  jdolecek 		syscallarg(struct kevent *) eventlist;
    739       1.3  jdolecek 		syscallarg(size_t) nevents;
    740       1.3  jdolecek 		syscallarg(const struct timespec *) timeout;
    741      1.44       dsl 	} */
    742      1.24      cube 
    743  1.47.6.1       mjf 	return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
    744      1.24      cube 	    SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
    745      1.24      cube 	    SCARG(uap, timeout), &kevent_native_ops);
    746      1.24      cube }
    747      1.24      cube 
    748      1.24      cube int
    749  1.47.6.1       mjf kevent1(register_t *retval, int fd,
    750  1.47.6.1       mjf 	const struct kevent *changelist, size_t nchanges,
    751  1.47.6.1       mjf 	struct kevent *eventlist, size_t nevents,
    752  1.47.6.1       mjf 	const struct timespec *timeout,
    753  1.47.6.1       mjf 	const struct kevent_ops *keops)
    754      1.24      cube {
    755  1.47.6.1       mjf 	struct kevent *kevp;
    756  1.47.6.1       mjf 	struct kqueue *kq;
    757       1.3  jdolecek 	struct timespec	ts;
    758  1.47.6.1       mjf 	size_t i, n, ichange;
    759  1.47.6.1       mjf 	int nerrors, error;
    760  1.47.6.1       mjf 	struct kevent kevbuf[8];	/* approx 300 bytes on 64-bit */
    761  1.47.6.1       mjf 	file_t *fp;
    762       1.3  jdolecek 
    763       1.3  jdolecek 	/* check that we're dealing with a kq */
    764  1.47.6.1       mjf 	fp = fd_getfile(fd);
    765      1.10        pk 	if (fp == NULL)
    766       1.1     lukem 		return (EBADF);
    767      1.10        pk 
    768      1.10        pk 	if (fp->f_type != DTYPE_KQUEUE) {
    769  1.47.6.1       mjf 		fd_putfile(fd);
    770      1.10        pk 		return (EBADF);
    771      1.10        pk 	}
    772       1.1     lukem 
    773      1.24      cube 	if (timeout != NULL) {
    774      1.24      cube 		error = (*keops->keo_fetch_timeout)(timeout, &ts, sizeof(ts));
    775       1.1     lukem 		if (error)
    776       1.1     lukem 			goto done;
    777      1.24      cube 		timeout = &ts;
    778       1.1     lukem 	}
    779       1.1     lukem 
    780       1.1     lukem 	kq = (struct kqueue *)fp->f_data;
    781       1.1     lukem 	nerrors = 0;
    782      1.24      cube 	ichange = 0;
    783       1.1     lukem 
    784       1.3  jdolecek 	/* traverse list of events to register */
    785      1.24      cube 	while (nchanges > 0) {
    786  1.47.6.1       mjf 		n = MIN(nchanges, __arraycount(kevbuf));
    787      1.24      cube 		error = (*keops->keo_fetch_changes)(keops->keo_private,
    788  1.47.6.1       mjf 		    changelist, kevbuf, ichange, n);
    789       1.1     lukem 		if (error)
    790       1.1     lukem 			goto done;
    791       1.1     lukem 		for (i = 0; i < n; i++) {
    792  1.47.6.1       mjf 			kevp = &kevbuf[i];
    793       1.1     lukem 			kevp->flags &= ~EV_SYSFLAGS;
    794       1.3  jdolecek 			/* register each knote */
    795  1.47.6.1       mjf 			error = kqueue_register(kq, kevp);
    796       1.1     lukem 			if (error) {
    797      1.24      cube 				if (nevents != 0) {
    798       1.1     lukem 					kevp->flags = EV_ERROR;
    799       1.1     lukem 					kevp->data = error;
    800      1.24      cube 					error = (*keops->keo_put_events)
    801      1.24      cube 					    (keops->keo_private, kevp,
    802      1.24      cube 					    eventlist, nerrors, 1);
    803       1.3  jdolecek 					if (error)
    804       1.3  jdolecek 						goto done;
    805      1.24      cube 					nevents--;
    806       1.1     lukem 					nerrors++;
    807       1.1     lukem 				} else {
    808       1.1     lukem 					goto done;
    809       1.1     lukem 				}
    810       1.1     lukem 			}
    811       1.1     lukem 		}
    812      1.24      cube 		nchanges -= n;	/* update the results */
    813      1.24      cube 		ichange += n;
    814       1.1     lukem 	}
    815       1.1     lukem 	if (nerrors) {
    816       1.3  jdolecek 		*retval = nerrors;
    817       1.1     lukem 		error = 0;
    818       1.1     lukem 		goto done;
    819       1.1     lukem 	}
    820       1.1     lukem 
    821       1.3  jdolecek 	/* actually scan through the events */
    822  1.47.6.1       mjf 	error = kqueue_scan(fp, nevents, eventlist, timeout, retval, keops,
    823  1.47.6.1       mjf 	    kevbuf, __arraycount(kevbuf));
    824       1.3  jdolecek  done:
    825  1.47.6.1       mjf 	fd_putfile(fd);
    826       1.1     lukem 	return (error);
    827       1.1     lukem }
    828       1.1     lukem 
    829       1.3  jdolecek /*
    830       1.3  jdolecek  * Register a given kevent kev onto the kqueue
    831       1.3  jdolecek  */
    832  1.47.6.1       mjf static int
    833  1.47.6.1       mjf kqueue_register(struct kqueue *kq, struct kevent *kev)
    834       1.1     lukem {
    835  1.47.6.1       mjf 	struct kfilter *kfilter;
    836  1.47.6.1       mjf 	filedesc_t *fdp;
    837  1.47.6.1       mjf 	file_t *fp;
    838  1.47.6.1       mjf 	fdfile_t *ff;
    839  1.47.6.1       mjf 	struct knote *kn, *newkn;
    840  1.47.6.1       mjf 	struct klist *list;
    841  1.47.6.1       mjf 	int error, fd, rv;
    842       1.3  jdolecek 
    843       1.3  jdolecek 	fdp = kq->kq_fdp;
    844       1.3  jdolecek 	fp = NULL;
    845       1.3  jdolecek 	kn = NULL;
    846       1.3  jdolecek 	error = 0;
    847  1.47.6.1       mjf 	fd = 0;
    848  1.47.6.1       mjf 
    849  1.47.6.1       mjf 	newkn = kmem_zalloc(sizeof(*newkn), KM_SLEEP);
    850  1.47.6.1       mjf 
    851  1.47.6.1       mjf 	rw_enter(&kqueue_filter_lock, RW_READER);
    852       1.3  jdolecek 	kfilter = kfilter_byfilter(kev->filter);
    853       1.3  jdolecek 	if (kfilter == NULL || kfilter->filtops == NULL) {
    854       1.3  jdolecek 		/* filter not found nor implemented */
    855  1.47.6.1       mjf 		rw_exit(&kqueue_filter_lock);
    856  1.47.6.1       mjf 		kmem_free(newkn, sizeof(*newkn));
    857       1.1     lukem 		return (EINVAL);
    858       1.1     lukem 	}
    859       1.1     lukem 
    860  1.47.6.1       mjf  	mutex_enter(&fdp->fd_lock);
    861  1.47.6.1       mjf 
    862       1.3  jdolecek 	/* search if knote already exists */
    863       1.3  jdolecek 	if (kfilter->filtops->f_isfd) {
    864       1.3  jdolecek 		/* monitoring a file descriptor */
    865  1.47.6.1       mjf 		fd = kev->ident;
    866  1.47.6.1       mjf 		if ((fp = fd_getfile(fd)) == NULL) {
    867  1.47.6.1       mjf 		 	mutex_exit(&fdp->fd_lock);
    868  1.47.6.1       mjf 			rw_exit(&kqueue_filter_lock);
    869  1.47.6.1       mjf 			kmem_free(newkn, sizeof(*newkn));
    870  1.47.6.1       mjf 			return EBADF;
    871  1.47.6.1       mjf 		}
    872  1.47.6.1       mjf 		ff = fdp->fd_ofiles[fd];
    873  1.47.6.1       mjf 		if (fd <= fdp->fd_lastkqfile) {
    874  1.47.6.1       mjf 			SLIST_FOREACH(kn, &ff->ff_knlist, kn_link) {
    875       1.1     lukem 				if (kq == kn->kn_kq &&
    876       1.1     lukem 				    kev->filter == kn->kn_filter)
    877       1.1     lukem 					break;
    878  1.47.6.1       mjf 			}
    879       1.1     lukem 		}
    880       1.1     lukem 	} else {
    881       1.3  jdolecek 		/*
    882       1.3  jdolecek 		 * not monitoring a file descriptor, so
    883       1.3  jdolecek 		 * lookup knotes in internal hash table
    884       1.3  jdolecek 		 */
    885       1.1     lukem 		if (fdp->fd_knhashmask != 0) {
    886       1.1     lukem 			list = &fdp->fd_knhash[
    887       1.1     lukem 			    KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
    888  1.47.6.1       mjf 			SLIST_FOREACH(kn, list, kn_link) {
    889       1.1     lukem 				if (kev->ident == kn->kn_id &&
    890       1.1     lukem 				    kq == kn->kn_kq &&
    891       1.1     lukem 				    kev->filter == kn->kn_filter)
    892       1.1     lukem 					break;
    893  1.47.6.1       mjf 			}
    894       1.1     lukem 		}
    895       1.1     lukem 	}
    896       1.1     lukem 
    897       1.1     lukem 	/*
    898       1.1     lukem 	 * kn now contains the matching knote, or NULL if no match
    899       1.1     lukem 	 */
    900       1.1     lukem 	if (kev->flags & EV_ADD) {
    901       1.1     lukem 		if (kn == NULL) {
    902       1.3  jdolecek 			/* create new knote */
    903  1.47.6.1       mjf 			kn = newkn;
    904  1.47.6.1       mjf 			newkn = NULL;
    905  1.47.6.1       mjf 			kn->kn_obj = fp;
    906       1.1     lukem 			kn->kn_kq = kq;
    907       1.3  jdolecek 			kn->kn_fop = kfilter->filtops;
    908  1.47.6.1       mjf 			kn->kn_kfilter = kfilter;
    909  1.47.6.1       mjf 			kn->kn_sfflags = kev->fflags;
    910  1.47.6.1       mjf 			kn->kn_sdata = kev->data;
    911  1.47.6.1       mjf 			kev->fflags = 0;
    912  1.47.6.1       mjf 			kev->data = 0;
    913  1.47.6.1       mjf 			kn->kn_kevent = *kev;
    914       1.1     lukem 
    915       1.1     lukem 			/*
    916       1.1     lukem 			 * apply reference count to knote structure, and
    917       1.1     lukem 			 * do not release it at the end of this routine.
    918       1.1     lukem 			 */
    919       1.1     lukem 			fp = NULL;
    920       1.1     lukem 
    921  1.47.6.1       mjf 			if (!kn->kn_fop->f_isfd) {
    922  1.47.6.1       mjf 				/*
    923  1.47.6.1       mjf 				 * If knote is not on an fd, store on
    924  1.47.6.1       mjf 				 * internal hash table.
    925  1.47.6.1       mjf 				 */
    926  1.47.6.1       mjf 				if (fdp->fd_knhashmask == 0) {
    927  1.47.6.1       mjf 					/* XXXAD can block with fd_lock held */
    928  1.47.6.1       mjf 					fdp->fd_knhash = hashinit(KN_HASHSIZE,
    929  1.47.6.2       mjf 					    HASH_LIST, true,
    930  1.47.6.1       mjf 					    &fdp->fd_knhashmask);
    931  1.47.6.1       mjf 				}
    932  1.47.6.1       mjf 				list = &fdp->fd_knhash[KN_HASH(kn->kn_id,
    933  1.47.6.1       mjf 				    fdp->fd_knhashmask)];
    934  1.47.6.1       mjf 			} else {
    935  1.47.6.1       mjf 				/* Otherwise, knote is on an fd. */
    936  1.47.6.1       mjf 				list = (struct klist *)
    937  1.47.6.1       mjf 				    &fdp->fd_ofiles[kn->kn_id]->ff_knlist;
    938  1.47.6.1       mjf 				if ((int)kn->kn_id > fdp->fd_lastkqfile)
    939  1.47.6.1       mjf 					fdp->fd_lastkqfile = kn->kn_id;
    940  1.47.6.1       mjf 			}
    941  1.47.6.1       mjf 			SLIST_INSERT_HEAD(list, kn, kn_link);
    942       1.1     lukem 
    943  1.47.6.1       mjf 			KERNEL_LOCK(1, NULL);		/* XXXSMP */
    944  1.47.6.1       mjf 			error = (*kfilter->filtops->f_attach)(kn);
    945  1.47.6.1       mjf 			KERNEL_UNLOCK_ONE(NULL);	/* XXXSMP */
    946  1.47.6.1       mjf 			if (error != 0) {
    947  1.47.6.1       mjf 				/* knote_detach() drops fdp->fd_lock */
    948  1.47.6.1       mjf 				knote_detach(kn, fdp, false);
    949       1.1     lukem 				goto done;
    950       1.1     lukem 			}
    951  1.47.6.1       mjf 			atomic_inc_uint(&kfilter->refcnt);
    952       1.1     lukem 		} else {
    953       1.1     lukem 			/*
    954       1.1     lukem 			 * The user may change some filter values after the
    955      1.22     perry 			 * initial EV_ADD, but doing so will not reset any
    956       1.1     lukem 			 * filter which have already been triggered.
    957       1.1     lukem 			 */
    958       1.1     lukem 			kn->kn_sfflags = kev->fflags;
    959       1.1     lukem 			kn->kn_sdata = kev->data;
    960       1.1     lukem 			kn->kn_kevent.udata = kev->udata;
    961       1.1     lukem 		}
    962  1.47.6.1       mjf 		KERNEL_LOCK(1, NULL);			/* XXXSMP */
    963  1.47.6.1       mjf 		rv = (*kn->kn_fop->f_event)(kn, 0);
    964  1.47.6.1       mjf 		KERNEL_UNLOCK_ONE(NULL);		/* XXXSMP */
    965  1.47.6.1       mjf 		if (rv)
    966  1.47.6.1       mjf 			knote_activate(kn);
    967  1.47.6.1       mjf 	} else {
    968  1.47.6.1       mjf 		if (kn == NULL) {
    969  1.47.6.1       mjf 			error = ENOENT;
    970  1.47.6.1       mjf 		 	mutex_exit(&fdp->fd_lock);
    971  1.47.6.1       mjf 			goto done;
    972  1.47.6.1       mjf 		}
    973  1.47.6.1       mjf 		if (kev->flags & EV_DELETE) {
    974  1.47.6.1       mjf 			/* knote_detach() drops fdp->fd_lock */
    975  1.47.6.1       mjf 			knote_detach(kn, fdp, true);
    976  1.47.6.1       mjf 			goto done;
    977  1.47.6.1       mjf 		}
    978       1.1     lukem 	}
    979       1.1     lukem 
    980       1.3  jdolecek 	/* disable knote */
    981  1.47.6.1       mjf 	if ((kev->flags & EV_DISABLE)) {
    982  1.47.6.1       mjf 		mutex_spin_enter(&kq->kq_lock);
    983  1.47.6.1       mjf 		if ((kn->kn_status & KN_DISABLED) == 0)
    984  1.47.6.1       mjf 			kn->kn_status |= KN_DISABLED;
    985  1.47.6.1       mjf 		mutex_spin_exit(&kq->kq_lock);
    986       1.1     lukem 	}
    987       1.1     lukem 
    988       1.3  jdolecek 	/* enable knote */
    989  1.47.6.1       mjf 	if ((kev->flags & EV_ENABLE)) {
    990  1.47.6.1       mjf 		knote_enqueue(kn);
    991       1.1     lukem 	}
    992  1.47.6.1       mjf 	mutex_exit(&fdp->fd_lock);
    993       1.3  jdolecek  done:
    994  1.47.6.1       mjf 	rw_exit(&kqueue_filter_lock);
    995  1.47.6.1       mjf 	if (newkn != NULL)
    996  1.47.6.1       mjf 		kmem_free(newkn, sizeof(*newkn));
    997       1.1     lukem 	if (fp != NULL)
    998  1.47.6.1       mjf 		fd_putfile(fd);
    999       1.1     lukem 	return (error);
   1000       1.1     lukem }
   1001       1.1     lukem 
   1002  1.47.6.1       mjf #if defined(DEBUG)
   1003  1.47.6.1       mjf static void
   1004  1.47.6.1       mjf kq_check(struct kqueue *kq)
   1005  1.47.6.1       mjf {
   1006  1.47.6.1       mjf 	const struct knote *kn;
   1007  1.47.6.1       mjf 	int count;
   1008  1.47.6.1       mjf 	int nmarker;
   1009  1.47.6.1       mjf 
   1010  1.47.6.1       mjf 	KASSERT(mutex_owned(&kq->kq_lock));
   1011  1.47.6.1       mjf 	KASSERT(kq->kq_count >= 0);
   1012  1.47.6.1       mjf 
   1013  1.47.6.1       mjf 	count = 0;
   1014  1.47.6.1       mjf 	nmarker = 0;
   1015  1.47.6.1       mjf 	TAILQ_FOREACH(kn, &kq->kq_head, kn_tqe) {
   1016  1.47.6.1       mjf 		if ((kn->kn_status & (KN_MARKER | KN_QUEUED)) == 0) {
   1017  1.47.6.1       mjf 			panic("%s: kq=%p kn=%p inconsist 1", __func__, kq, kn);
   1018  1.47.6.1       mjf 		}
   1019  1.47.6.1       mjf 		if ((kn->kn_status & KN_MARKER) == 0) {
   1020  1.47.6.1       mjf 			if (kn->kn_kq != kq) {
   1021  1.47.6.1       mjf 				panic("%s: kq=%p kn=%p inconsist 2",
   1022  1.47.6.1       mjf 				    __func__, kq, kn);
   1023  1.47.6.1       mjf 			}
   1024  1.47.6.1       mjf 			if ((kn->kn_status & KN_ACTIVE) == 0) {
   1025  1.47.6.1       mjf 				panic("%s: kq=%p kn=%p: not active",
   1026  1.47.6.1       mjf 				    __func__, kq, kn);
   1027  1.47.6.1       mjf 			}
   1028  1.47.6.1       mjf 			count++;
   1029  1.47.6.1       mjf 			if (count > kq->kq_count) {
   1030  1.47.6.1       mjf 				goto bad;
   1031  1.47.6.1       mjf 			}
   1032  1.47.6.1       mjf 		} else {
   1033  1.47.6.1       mjf 			nmarker++;
   1034  1.47.6.1       mjf #if 0
   1035  1.47.6.1       mjf 			if (nmarker > 10000) {
   1036  1.47.6.1       mjf 				panic("%s: kq=%p too many markers: %d != %d, "
   1037  1.47.6.1       mjf 				    "nmarker=%d",
   1038  1.47.6.1       mjf 				    __func__, kq, kq->kq_count, count, nmarker);
   1039  1.47.6.1       mjf 			}
   1040  1.47.6.1       mjf #endif
   1041  1.47.6.1       mjf 		}
   1042  1.47.6.1       mjf 	}
   1043  1.47.6.1       mjf 	if (kq->kq_count != count) {
   1044  1.47.6.1       mjf bad:
   1045  1.47.6.1       mjf 		panic("%s: kq=%p inconsist 3: %d != %d, nmarker=%d",
   1046  1.47.6.1       mjf 		    __func__, kq, kq->kq_count, count, nmarker);
   1047  1.47.6.1       mjf 	}
   1048  1.47.6.1       mjf }
   1049  1.47.6.1       mjf #else /* defined(DEBUG) */
   1050  1.47.6.1       mjf #define	kq_check(a)	/* nothing */
   1051  1.47.6.1       mjf #endif /* defined(DEBUG) */
   1052  1.47.6.1       mjf 
   1053       1.3  jdolecek /*
   1054       1.3  jdolecek  * Scan through the list of events on fp (for a maximum of maxevents),
   1055       1.3  jdolecek  * returning the results in to ulistp. Timeout is determined by tsp; if
   1056       1.3  jdolecek  * NULL, wait indefinitely, if 0 valued, perform a poll, otherwise wait
   1057       1.3  jdolecek  * as appropriate.
   1058       1.3  jdolecek  */
   1059       1.1     lukem static int
   1060  1.47.6.1       mjf kqueue_scan(file_t *fp, size_t maxevents, struct kevent *ulistp,
   1061  1.47.6.1       mjf 	    const struct timespec *tsp, register_t *retval,
   1062  1.47.6.1       mjf 	    const struct kevent_ops *keops, struct kevent *kevbuf,
   1063  1.47.6.1       mjf 	    size_t kevcnt)
   1064       1.1     lukem {
   1065       1.3  jdolecek 	struct kqueue	*kq;
   1066       1.3  jdolecek 	struct kevent	*kevp;
   1067      1.29    kardel 	struct timeval	atv, sleeptv;
   1068  1.47.6.1       mjf 	struct knote	*kn, *marker;
   1069      1.24      cube 	size_t		count, nkev, nevents;
   1070  1.47.6.1       mjf 	int		timeout, error, rv;
   1071  1.47.6.1       mjf 	filedesc_t	*fdp;
   1072       1.1     lukem 
   1073  1.47.6.1       mjf 	fdp = curlwp->l_fd;
   1074  1.47.6.1       mjf 	kq = fp->f_data;
   1075       1.1     lukem 	count = maxevents;
   1076      1.24      cube 	nkev = nevents = error = 0;
   1077  1.47.6.1       mjf 	if (count == 0) {
   1078  1.47.6.1       mjf 		*retval = 0;
   1079  1.47.6.1       mjf 		return 0;
   1080  1.47.6.1       mjf 	}
   1081       1.1     lukem 
   1082       1.9  jdolecek 	if (tsp) {				/* timeout supplied */
   1083       1.1     lukem 		TIMESPEC_TO_TIMEVAL(&atv, tsp);
   1084      1.29    kardel 		if (inittimeleft(&atv, &sleeptv) == -1) {
   1085  1.47.6.1       mjf 			*retval = maxevents;
   1086  1.47.6.1       mjf 			return EINVAL;
   1087       1.1     lukem 		}
   1088      1.28    kardel 		timeout = tvtohz(&atv);
   1089       1.9  jdolecek 		if (timeout <= 0)
   1090      1.29    kardel 			timeout = -1;           /* do poll */
   1091       1.1     lukem 	} else {
   1092       1.9  jdolecek 		/* no timeout, wait forever */
   1093       1.1     lukem 		timeout = 0;
   1094  1.47.6.1       mjf 	}
   1095       1.1     lukem 
   1096  1.47.6.1       mjf 	marker = kmem_zalloc(sizeof(*marker), KM_SLEEP);
   1097  1.47.6.1       mjf 	marker->kn_status = KN_MARKER;
   1098  1.47.6.1       mjf 	mutex_spin_enter(&kq->kq_lock);
   1099       1.3  jdolecek  retry:
   1100  1.47.6.1       mjf 	kevp = kevbuf;
   1101       1.1     lukem 	if (kq->kq_count == 0) {
   1102  1.47.6.1       mjf 		if (timeout >= 0) {
   1103  1.47.6.1       mjf 			error = cv_timedwait_sig(&kq->kq_cv,
   1104  1.47.6.1       mjf 			    &kq->kq_lock, timeout);
   1105  1.47.6.1       mjf 			if (error == 0) {
   1106  1.47.6.1       mjf 				 if (tsp == NULL || (timeout =
   1107  1.47.6.1       mjf 				     gettimeleft(&atv, &sleeptv)) > 0)
   1108  1.47.6.1       mjf 					goto retry;
   1109  1.47.6.1       mjf 			} else {
   1110  1.47.6.1       mjf 				/* don't restart after signals... */
   1111  1.47.6.1       mjf 				if (error == ERESTART)
   1112  1.47.6.1       mjf 					error = EINTR;
   1113  1.47.6.1       mjf 				if (error == EWOULDBLOCK)
   1114  1.47.6.1       mjf 					error = 0;
   1115  1.47.6.1       mjf 			}
   1116       1.1     lukem 		}
   1117  1.47.6.1       mjf 	} else {
   1118  1.47.6.1       mjf 		/* mark end of knote list */
   1119  1.47.6.1       mjf 		TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
   1120      1.12        pk 
   1121  1.47.6.1       mjf 		while (count != 0) {
   1122  1.47.6.1       mjf 			kn = TAILQ_FIRST(&kq->kq_head);	/* get next knote */
   1123  1.47.6.1       mjf 			while ((kn->kn_status & KN_MARKER) != 0) {
   1124  1.47.6.1       mjf 				if (kn == marker) {
   1125  1.47.6.1       mjf 					/* it's our marker, stop */
   1126  1.47.6.1       mjf 					TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
   1127  1.47.6.1       mjf 					if (count < maxevents || (tsp != NULL &&
   1128  1.47.6.1       mjf 					    (timeout = gettimeleft(&atv,
   1129  1.47.6.1       mjf 					    &sleeptv)) <= 0))
   1130  1.47.6.1       mjf 						goto done;
   1131  1.47.6.1       mjf 					goto retry;
   1132  1.47.6.1       mjf 				}
   1133  1.47.6.1       mjf 				/* someone else's marker. */
   1134  1.47.6.1       mjf 				kn = TAILQ_NEXT(kn, kn_tqe);
   1135  1.47.6.1       mjf 			}
   1136  1.47.6.1       mjf 			kq_check(kq);
   1137  1.47.6.1       mjf 			TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
   1138  1.47.6.1       mjf 			kq->kq_count--;
   1139       1.1     lukem 			kn->kn_status &= ~KN_QUEUED;
   1140  1.47.6.1       mjf 			kq_check(kq);
   1141  1.47.6.1       mjf 			if (kn->kn_status & KN_DISABLED) {
   1142  1.47.6.1       mjf 				/* don't want disabled events */
   1143  1.47.6.1       mjf 				continue;
   1144  1.47.6.1       mjf 			}
   1145  1.47.6.1       mjf 			if ((kn->kn_flags & EV_ONESHOT) == 0) {
   1146  1.47.6.1       mjf 				mutex_spin_exit(&kq->kq_lock);
   1147  1.47.6.1       mjf 				KERNEL_LOCK(1, NULL);		/* XXXSMP */
   1148  1.47.6.1       mjf 				rv = (*kn->kn_fop->f_event)(kn, 0);
   1149  1.47.6.1       mjf 				KERNEL_UNLOCK_ONE(NULL);	/* XXXSMP */
   1150  1.47.6.1       mjf 				mutex_spin_enter(&kq->kq_lock);
   1151  1.47.6.1       mjf 				/* Re-poll if note was re-enqueued. */
   1152  1.47.6.1       mjf 				if ((kn->kn_status & KN_QUEUED) != 0)
   1153  1.47.6.1       mjf 					continue;
   1154  1.47.6.1       mjf 				if (rv == 0) {
   1155  1.47.6.1       mjf 					/*
   1156  1.47.6.1       mjf 					 * non-ONESHOT event that hasn't
   1157  1.47.6.1       mjf 					 * triggered again, so de-queue.
   1158  1.47.6.1       mjf 					 */
   1159  1.47.6.1       mjf 					kn->kn_status &= ~KN_ACTIVE;
   1160  1.47.6.1       mjf 					continue;
   1161  1.47.6.1       mjf 				}
   1162  1.47.6.1       mjf 			}
   1163  1.47.6.1       mjf 			/* XXXAD should be got from f_event if !oneshot. */
   1164  1.47.6.1       mjf 			*kevp++ = kn->kn_kevent;
   1165  1.47.6.1       mjf 			nkev++;
   1166  1.47.6.1       mjf 			if (kn->kn_flags & EV_ONESHOT) {
   1167  1.47.6.1       mjf 				/* delete ONESHOT events after retrieval */
   1168  1.47.6.1       mjf 				mutex_spin_exit(&kq->kq_lock);
   1169  1.47.6.1       mjf 				mutex_enter(&fdp->fd_lock);
   1170  1.47.6.1       mjf 				knote_detach(kn, fdp, true);
   1171  1.47.6.1       mjf 				mutex_spin_enter(&kq->kq_lock);
   1172  1.47.6.1       mjf 			} else if (kn->kn_flags & EV_CLEAR) {
   1173  1.47.6.1       mjf 				/* clear state after retrieval */
   1174  1.47.6.1       mjf 				kn->kn_data = 0;
   1175  1.47.6.1       mjf 				kn->kn_fflags = 0;
   1176  1.47.6.1       mjf 				kn->kn_status &= ~KN_ACTIVE;
   1177  1.47.6.1       mjf 			} else {
   1178  1.47.6.1       mjf 				/* add event back on list */
   1179  1.47.6.1       mjf 				kq_check(kq);
   1180  1.47.6.1       mjf 				TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
   1181  1.47.6.1       mjf 				kq->kq_count++;
   1182  1.47.6.1       mjf 				kn->kn_status |= KN_QUEUED;
   1183  1.47.6.1       mjf 				kq_check(kq);
   1184  1.47.6.1       mjf 			}
   1185  1.47.6.1       mjf 			if (nkev == kevcnt) {
   1186  1.47.6.1       mjf 				/* do copyouts in kevcnt chunks */
   1187  1.47.6.1       mjf 				mutex_spin_exit(&kq->kq_lock);
   1188  1.47.6.1       mjf 				error = (*keops->keo_put_events)
   1189  1.47.6.1       mjf 				    (keops->keo_private,
   1190  1.47.6.1       mjf 				    kevbuf, ulistp, nevents, nkev);
   1191  1.47.6.1       mjf 				mutex_spin_enter(&kq->kq_lock);
   1192  1.47.6.1       mjf 				nevents += nkev;
   1193  1.47.6.1       mjf 				nkev = 0;
   1194  1.47.6.1       mjf 				kevp = kevbuf;
   1195  1.47.6.1       mjf 			}
   1196  1.47.6.1       mjf 			count--;
   1197  1.47.6.1       mjf 			if (error != 0 || count == 0) {
   1198  1.47.6.1       mjf 				/* remove marker */
   1199  1.47.6.1       mjf 				TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
   1200       1.1     lukem 				break;
   1201  1.47.6.1       mjf 			}
   1202       1.1     lukem 		}
   1203       1.1     lukem 	}
   1204       1.3  jdolecek  done:
   1205  1.47.6.1       mjf  	mutex_spin_exit(&kq->kq_lock);
   1206  1.47.6.1       mjf 	if (marker != NULL)
   1207  1.47.6.1       mjf 		kmem_free(marker, sizeof(*marker));
   1208  1.47.6.1       mjf 	if (nkev != 0) {
   1209       1.3  jdolecek 		/* copyout remaining events */
   1210      1.24      cube 		error = (*keops->keo_put_events)(keops->keo_private,
   1211  1.47.6.1       mjf 		    kevbuf, ulistp, nevents, nkev);
   1212  1.47.6.1       mjf 	}
   1213       1.3  jdolecek 	*retval = maxevents - count;
   1214       1.3  jdolecek 
   1215  1.47.6.1       mjf 	return error;
   1216       1.1     lukem }
   1217       1.1     lukem 
   1218       1.3  jdolecek /*
   1219  1.47.6.1       mjf  * fileops ioctl method for a kqueue descriptor.
   1220       1.3  jdolecek  *
   1221       1.3  jdolecek  * Two ioctls are currently supported. They both use struct kfilter_mapping:
   1222       1.3  jdolecek  *	KFILTER_BYNAME		find name for filter, and return result in
   1223       1.3  jdolecek  *				name, which is of size len.
   1224       1.3  jdolecek  *	KFILTER_BYFILTER	find filter for name. len is ignored.
   1225       1.3  jdolecek  */
   1226       1.1     lukem /*ARGSUSED*/
   1227       1.1     lukem static int
   1228  1.47.6.1       mjf kqueue_ioctl(file_t *fp, u_long com, void *data)
   1229       1.1     lukem {
   1230       1.3  jdolecek 	struct kfilter_mapping	*km;
   1231       1.3  jdolecek 	const struct kfilter	*kfilter;
   1232       1.3  jdolecek 	char			*name;
   1233       1.3  jdolecek 	int			error;
   1234       1.3  jdolecek 
   1235  1.47.6.1       mjf 	km = data;
   1236       1.3  jdolecek 	error = 0;
   1237  1.47.6.1       mjf 	name = kmem_alloc(KFILTER_MAXNAME, KM_SLEEP);
   1238       1.3  jdolecek 
   1239       1.3  jdolecek 	switch (com) {
   1240       1.3  jdolecek 	case KFILTER_BYFILTER:	/* convert filter -> name */
   1241  1.47.6.1       mjf 		rw_enter(&kqueue_filter_lock, RW_READER);
   1242       1.3  jdolecek 		kfilter = kfilter_byfilter(km->filter);
   1243  1.47.6.1       mjf 		if (kfilter != NULL) {
   1244  1.47.6.1       mjf 			strlcpy(name, kfilter->name, KFILTER_MAXNAME);
   1245  1.47.6.1       mjf 			rw_exit(&kqueue_filter_lock);
   1246  1.47.6.1       mjf 			error = copyoutstr(name, km->name, km->len, NULL);
   1247  1.47.6.1       mjf 		} else {
   1248  1.47.6.1       mjf 			rw_exit(&kqueue_filter_lock);
   1249       1.3  jdolecek 			error = ENOENT;
   1250  1.47.6.1       mjf 		}
   1251       1.3  jdolecek 		break;
   1252       1.3  jdolecek 
   1253       1.3  jdolecek 	case KFILTER_BYNAME:	/* convert name -> filter */
   1254       1.3  jdolecek 		error = copyinstr(km->name, name, KFILTER_MAXNAME, NULL);
   1255       1.3  jdolecek 		if (error) {
   1256       1.3  jdolecek 			break;
   1257       1.3  jdolecek 		}
   1258  1.47.6.1       mjf 		rw_enter(&kqueue_filter_lock, RW_READER);
   1259       1.3  jdolecek 		kfilter = kfilter_byname(name);
   1260       1.3  jdolecek 		if (kfilter != NULL)
   1261       1.3  jdolecek 			km->filter = kfilter->filter;
   1262       1.3  jdolecek 		else
   1263       1.3  jdolecek 			error = ENOENT;
   1264  1.47.6.1       mjf 		rw_exit(&kqueue_filter_lock);
   1265       1.3  jdolecek 		break;
   1266       1.3  jdolecek 
   1267       1.3  jdolecek 	default:
   1268       1.3  jdolecek 		error = ENOTTY;
   1269  1.47.6.1       mjf 		break;
   1270       1.3  jdolecek 
   1271       1.3  jdolecek 	}
   1272  1.47.6.1       mjf 	kmem_free(name, KFILTER_MAXNAME);
   1273       1.3  jdolecek 	return (error);
   1274       1.3  jdolecek }
   1275       1.3  jdolecek 
   1276       1.3  jdolecek /*
   1277  1.47.6.1       mjf  * fileops fcntl method for a kqueue descriptor.
   1278       1.3  jdolecek  */
   1279       1.3  jdolecek static int
   1280  1.47.6.1       mjf kqueue_fcntl(file_t *fp, u_int com, void *data)
   1281       1.3  jdolecek {
   1282       1.3  jdolecek 
   1283       1.1     lukem 	return (ENOTTY);
   1284       1.1     lukem }
   1285       1.1     lukem 
   1286       1.3  jdolecek /*
   1287  1.47.6.1       mjf  * fileops poll method for a kqueue descriptor.
   1288       1.3  jdolecek  * Determine if kqueue has events pending.
   1289       1.3  jdolecek  */
   1290       1.1     lukem static int
   1291  1.47.6.1       mjf kqueue_poll(file_t *fp, int events)
   1292       1.1     lukem {
   1293       1.3  jdolecek 	struct kqueue	*kq;
   1294       1.3  jdolecek 	int		revents;
   1295       1.3  jdolecek 
   1296  1.47.6.1       mjf 	kq = fp->f_data;
   1297  1.47.6.1       mjf 
   1298       1.3  jdolecek 	revents = 0;
   1299       1.3  jdolecek 	if (events & (POLLIN | POLLRDNORM)) {
   1300  1.47.6.1       mjf 		mutex_spin_enter(&kq->kq_lock);
   1301  1.47.6.1       mjf 		if (kq->kq_count != 0) {
   1302       1.3  jdolecek 			revents |= events & (POLLIN | POLLRDNORM);
   1303       1.1     lukem 		} else {
   1304  1.47.6.1       mjf 			selrecord(curlwp, &kq->kq_sel);
   1305       1.1     lukem 		}
   1306  1.47.6.1       mjf 		kq_check(kq);
   1307  1.47.6.1       mjf 		mutex_spin_exit(&kq->kq_lock);
   1308       1.1     lukem 	}
   1309  1.47.6.1       mjf 
   1310  1.47.6.1       mjf 	return revents;
   1311       1.1     lukem }
   1312       1.1     lukem 
   1313       1.3  jdolecek /*
   1314  1.47.6.1       mjf  * fileops stat method for a kqueue descriptor.
   1315       1.3  jdolecek  * Returns dummy info, with st_size being number of events pending.
   1316       1.3  jdolecek  */
   1317       1.1     lukem static int
   1318  1.47.6.1       mjf kqueue_stat(file_t *fp, struct stat *st)
   1319       1.1     lukem {
   1320  1.47.6.1       mjf 	struct kqueue *kq;
   1321       1.1     lukem 
   1322  1.47.6.1       mjf 	kq = fp->f_data;
   1323  1.47.6.1       mjf 
   1324  1.47.6.1       mjf 	memset(st, 0, sizeof(*st));
   1325       1.1     lukem 	st->st_size = kq->kq_count;
   1326       1.1     lukem 	st->st_blksize = sizeof(struct kevent);
   1327       1.1     lukem 	st->st_mode = S_IFIFO;
   1328  1.47.6.1       mjf 
   1329  1.47.6.1       mjf 	return 0;
   1330  1.47.6.1       mjf }
   1331  1.47.6.1       mjf 
   1332  1.47.6.1       mjf static void
   1333  1.47.6.1       mjf kqueue_doclose(struct kqueue *kq, struct klist *list, int fd)
   1334  1.47.6.1       mjf {
   1335  1.47.6.1       mjf 	struct knote *kn;
   1336  1.47.6.1       mjf 	filedesc_t *fdp;
   1337  1.47.6.1       mjf 
   1338  1.47.6.1       mjf 	fdp = kq->kq_fdp;
   1339  1.47.6.1       mjf 
   1340  1.47.6.1       mjf 	KASSERT(mutex_owned(&fdp->fd_lock));
   1341  1.47.6.1       mjf 
   1342  1.47.6.1       mjf 	for (kn = SLIST_FIRST(list); kn != NULL;) {
   1343  1.47.6.1       mjf 		if (kq != kn->kn_kq) {
   1344  1.47.6.1       mjf 			kn = SLIST_NEXT(kn, kn_link);
   1345  1.47.6.1       mjf 			continue;
   1346  1.47.6.1       mjf 		}
   1347  1.47.6.1       mjf 		knote_detach(kn, fdp, true);
   1348  1.47.6.1       mjf 		mutex_enter(&fdp->fd_lock);
   1349  1.47.6.1       mjf 		kn = SLIST_FIRST(list);
   1350  1.47.6.1       mjf 	}
   1351       1.1     lukem }
   1352       1.1     lukem 
   1353  1.47.6.1       mjf 
   1354       1.3  jdolecek /*
   1355  1.47.6.1       mjf  * fileops close method for a kqueue descriptor.
   1356       1.3  jdolecek  */
   1357       1.1     lukem static int
   1358  1.47.6.1       mjf kqueue_close(file_t *fp)
   1359       1.1     lukem {
   1360  1.47.6.1       mjf 	struct kqueue *kq;
   1361  1.47.6.1       mjf 	filedesc_t *fdp;
   1362  1.47.6.1       mjf 	fdfile_t *ff;
   1363  1.47.6.1       mjf 	int i;
   1364       1.1     lukem 
   1365  1.47.6.1       mjf 	kq = fp->f_data;
   1366  1.47.6.1       mjf 	fdp = curlwp->l_fd;
   1367  1.47.6.1       mjf 
   1368  1.47.6.1       mjf 	mutex_enter(&fdp->fd_lock);
   1369  1.47.6.1       mjf 	for (i = 0; i <= fdp->fd_lastkqfile; i++) {
   1370  1.47.6.1       mjf 		if ((ff = fdp->fd_ofiles[i]) == NULL)
   1371  1.47.6.1       mjf 			continue;
   1372  1.47.6.1       mjf 		kqueue_doclose(kq, (struct klist *)&ff->ff_knlist, i);
   1373       1.1     lukem 	}
   1374       1.1     lukem 	if (fdp->fd_knhashmask != 0) {
   1375       1.1     lukem 		for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
   1376  1.47.6.1       mjf 			kqueue_doclose(kq, &fdp->fd_knhash[i], -1);
   1377       1.1     lukem 		}
   1378       1.1     lukem 	}
   1379  1.47.6.1       mjf 	mutex_exit(&fdp->fd_lock);
   1380  1.47.6.1       mjf 
   1381  1.47.6.1       mjf 	KASSERT(kq->kq_count == 0);
   1382  1.47.6.1       mjf 	mutex_destroy(&kq->kq_lock);
   1383  1.47.6.1       mjf 	cv_destroy(&kq->kq_cv);
   1384  1.47.6.1       mjf 	seldestroy(&kq->kq_sel);
   1385  1.47.6.1       mjf 	kmem_free(kq, sizeof(*kq));
   1386       1.1     lukem 	fp->f_data = NULL;
   1387       1.1     lukem 
   1388       1.1     lukem 	return (0);
   1389       1.1     lukem }
   1390       1.1     lukem 
   1391       1.3  jdolecek /*
   1392       1.3  jdolecek  * struct fileops kqfilter method for a kqueue descriptor.
   1393       1.3  jdolecek  * Event triggered when monitored kqueue changes.
   1394       1.3  jdolecek  */
   1395       1.3  jdolecek static int
   1396  1.47.6.1       mjf kqueue_kqfilter(file_t *fp, struct knote *kn)
   1397       1.3  jdolecek {
   1398       1.3  jdolecek 	struct kqueue *kq;
   1399  1.47.6.1       mjf 	filedesc_t *fdp;
   1400  1.47.6.1       mjf 
   1401  1.47.6.1       mjf 	kq = ((file_t *)kn->kn_obj)->f_data;
   1402  1.47.6.1       mjf 
   1403  1.47.6.1       mjf 	KASSERT(fp == kn->kn_obj);
   1404       1.3  jdolecek 
   1405       1.3  jdolecek 	if (kn->kn_filter != EVFILT_READ)
   1406  1.47.6.1       mjf 		return 1;
   1407  1.47.6.1       mjf 
   1408       1.3  jdolecek 	kn->kn_fop = &kqread_filtops;
   1409  1.47.6.1       mjf 	fdp = curlwp->l_fd;
   1410  1.47.6.1       mjf 	mutex_enter(&kq->kq_lock);
   1411       1.5  christos 	SLIST_INSERT_HEAD(&kq->kq_sel.sel_klist, kn, kn_selnext);
   1412  1.47.6.1       mjf 	mutex_exit(&kq->kq_lock);
   1413  1.47.6.1       mjf 
   1414  1.47.6.1       mjf 	return 0;
   1415       1.3  jdolecek }
   1416       1.3  jdolecek 
   1417       1.3  jdolecek 
   1418       1.3  jdolecek /*
   1419  1.47.6.1       mjf  * Walk down a list of knotes, activating them if their event has
   1420  1.47.6.1       mjf  * triggered.  The caller's object lock (e.g. device driver lock)
   1421  1.47.6.1       mjf  * must be held.
   1422       1.1     lukem  */
   1423       1.1     lukem void
   1424       1.1     lukem knote(struct klist *list, long hint)
   1425       1.1     lukem {
   1426       1.1     lukem 	struct knote *kn;
   1427       1.1     lukem 
   1428  1.47.6.1       mjf 	SLIST_FOREACH(kn, list, kn_selnext) {
   1429  1.47.6.1       mjf 		if ((*kn->kn_fop->f_event)(kn, hint))
   1430  1.47.6.1       mjf 			knote_activate(kn);
   1431  1.47.6.1       mjf 	}
   1432       1.1     lukem }
   1433       1.1     lukem 
   1434       1.1     lukem /*
   1435  1.47.6.1       mjf  * Remove all knotes referencing a specified fd
   1436       1.1     lukem  */
   1437       1.1     lukem void
   1438  1.47.6.1       mjf knote_fdclose(int fd)
   1439       1.1     lukem {
   1440  1.47.6.1       mjf 	struct klist *list;
   1441       1.1     lukem 	struct knote *kn;
   1442  1.47.6.1       mjf 	filedesc_t *fdp;
   1443       1.1     lukem 
   1444  1.47.6.1       mjf 	fdp = curlwp->l_fd;
   1445  1.47.6.1       mjf 	list = (struct klist *)&fdp->fd_ofiles[fd]->ff_knlist;
   1446  1.47.6.1       mjf 	mutex_enter(&fdp->fd_lock);
   1447       1.1     lukem 	while ((kn = SLIST_FIRST(list)) != NULL) {
   1448  1.47.6.1       mjf 		knote_detach(kn, fdp, true);
   1449  1.47.6.1       mjf 		mutex_enter(&fdp->fd_lock);
   1450       1.1     lukem 	}
   1451  1.47.6.1       mjf 	mutex_exit(&fdp->fd_lock);
   1452       1.1     lukem }
   1453       1.1     lukem 
   1454       1.1     lukem /*
   1455  1.47.6.1       mjf  * Drop knote.  Called with fdp->fd_lock held, and will drop before
   1456  1.47.6.1       mjf  * returning.
   1457       1.3  jdolecek  */
   1458       1.1     lukem static void
   1459  1.47.6.1       mjf knote_detach(struct knote *kn, filedesc_t *fdp, bool dofop)
   1460       1.1     lukem {
   1461  1.47.6.1       mjf 	struct klist *list;
   1462  1.47.6.1       mjf 	struct kqueue *kq;
   1463       1.1     lukem 
   1464  1.47.6.1       mjf 	kq = kn->kn_kq;
   1465       1.1     lukem 
   1466  1.47.6.1       mjf 	KASSERT((kn->kn_status & KN_MARKER) == 0);
   1467  1.47.6.1       mjf 	KASSERT(mutex_owned(&fdp->fd_lock));
   1468       1.3  jdolecek 
   1469  1.47.6.1       mjf 	/* Remove from monitored object. */
   1470  1.47.6.1       mjf 	if (dofop) {
   1471  1.47.6.1       mjf 		KERNEL_LOCK(1, NULL);		/* XXXSMP */
   1472  1.47.6.1       mjf 		(*kn->kn_fop->f_detach)(kn);
   1473  1.47.6.1       mjf 		KERNEL_UNLOCK_ONE(NULL);	/* XXXSMP */
   1474       1.1     lukem 	}
   1475       1.3  jdolecek 
   1476  1.47.6.1       mjf 	/* Remove from descriptor table. */
   1477       1.1     lukem 	if (kn->kn_fop->f_isfd)
   1478  1.47.6.1       mjf 		list = (struct klist *)&fdp->fd_ofiles[kn->kn_id]->ff_knlist;
   1479       1.1     lukem 	else
   1480       1.1     lukem 		list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
   1481       1.1     lukem 
   1482       1.1     lukem 	SLIST_REMOVE(list, kn, knote, kn_link);
   1483       1.1     lukem 
   1484  1.47.6.1       mjf 	/* Remove from kqueue. */
   1485  1.47.6.1       mjf 	/* XXXAD should verify not in use by kqueue_scan. */
   1486  1.47.6.1       mjf 	mutex_spin_enter(&kq->kq_lock);
   1487  1.47.6.1       mjf 	if ((kn->kn_status & KN_QUEUED) != 0) {
   1488  1.47.6.1       mjf 		kq_check(kq);
   1489  1.47.6.1       mjf 		TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
   1490  1.47.6.1       mjf 		kn->kn_status &= ~KN_QUEUED;
   1491  1.47.6.1       mjf 		kq->kq_count--;
   1492  1.47.6.1       mjf 		kq_check(kq);
   1493  1.47.6.1       mjf 	}
   1494  1.47.6.1       mjf 	mutex_spin_exit(&kq->kq_lock);
   1495  1.47.6.1       mjf 
   1496  1.47.6.1       mjf 	mutex_exit(&fdp->fd_lock);
   1497  1.47.6.1       mjf 	if (kn->kn_fop->f_isfd)
   1498  1.47.6.1       mjf 		fd_putfile(kn->kn_id);
   1499  1.47.6.1       mjf 	atomic_dec_uint(&kn->kn_kfilter->refcnt);
   1500  1.47.6.1       mjf 	kmem_free(kn, sizeof(*kn));
   1501  1.47.6.1       mjf }
   1502       1.1     lukem 
   1503       1.3  jdolecek /*
   1504       1.3  jdolecek  * Queue new event for knote.
   1505       1.3  jdolecek  */
   1506       1.1     lukem static void
   1507       1.1     lukem knote_enqueue(struct knote *kn)
   1508       1.1     lukem {
   1509  1.47.6.1       mjf 	struct kqueue *kq;
   1510  1.47.6.1       mjf 
   1511  1.47.6.1       mjf 	KASSERT((kn->kn_status & KN_MARKER) == 0);
   1512       1.1     lukem 
   1513       1.3  jdolecek 	kq = kn->kn_kq;
   1514       1.1     lukem 
   1515  1.47.6.1       mjf 	mutex_spin_enter(&kq->kq_lock);
   1516  1.47.6.1       mjf 	if ((kn->kn_status & KN_DISABLED) != 0) {
   1517  1.47.6.1       mjf 		kn->kn_status &= ~KN_DISABLED;
   1518  1.47.6.1       mjf 	}
   1519  1.47.6.1       mjf 	if ((kn->kn_status & (KN_ACTIVE | KN_QUEUED)) == KN_ACTIVE) {
   1520  1.47.6.1       mjf 		kq_check(kq);
   1521  1.47.6.1       mjf 		TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
   1522  1.47.6.1       mjf 		kn->kn_status |= KN_QUEUED;
   1523  1.47.6.1       mjf 		kq->kq_count++;
   1524  1.47.6.1       mjf 		kq_check(kq);
   1525  1.47.6.1       mjf 		cv_broadcast(&kq->kq_cv);
   1526  1.47.6.1       mjf 		selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
   1527  1.47.6.1       mjf 	}
   1528  1.47.6.1       mjf 	mutex_spin_exit(&kq->kq_lock);
   1529       1.1     lukem }
   1530       1.3  jdolecek /*
   1531  1.47.6.1       mjf  * Queue new event for knote.
   1532       1.3  jdolecek  */
   1533       1.1     lukem static void
   1534  1.47.6.1       mjf knote_activate(struct knote *kn)
   1535       1.1     lukem {
   1536  1.47.6.1       mjf 	struct kqueue *kq;
   1537  1.47.6.1       mjf 
   1538  1.47.6.1       mjf 	KASSERT((kn->kn_status & KN_MARKER) == 0);
   1539       1.1     lukem 
   1540       1.3  jdolecek 	kq = kn->kn_kq;
   1541      1.12        pk 
   1542  1.47.6.1       mjf 	mutex_spin_enter(&kq->kq_lock);
   1543  1.47.6.1       mjf 	kn->kn_status |= KN_ACTIVE;
   1544  1.47.6.1       mjf 	if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) {
   1545  1.47.6.1       mjf 		kq_check(kq);
   1546  1.47.6.1       mjf 		TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
   1547  1.47.6.1       mjf 		kn->kn_status |= KN_QUEUED;
   1548  1.47.6.1       mjf 		kq->kq_count++;
   1549  1.47.6.1       mjf 		kq_check(kq);
   1550  1.47.6.1       mjf 		cv_broadcast(&kq->kq_cv);
   1551  1.47.6.1       mjf 		selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
   1552  1.47.6.1       mjf 	}
   1553  1.47.6.1       mjf 	mutex_spin_exit(&kq->kq_lock);
   1554       1.1     lukem }
   1555