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