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