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