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