xref: /freebsd/sys/kern/kern_event.c (revision 7d99ab9fd0cc2c1ce2ecef0ed6d0672c2a50b0cb)
1 /*-
2  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3  * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
4  * Copyright (c) 2009 Apple, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ktrace.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/capability.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/rwlock.h>
41 #include <sys/proc.h>
42 #include <sys/malloc.h>
43 #include <sys/unistd.h>
44 #include <sys/file.h>
45 #include <sys/filedesc.h>
46 #include <sys/filio.h>
47 #include <sys/fcntl.h>
48 #include <sys/kthread.h>
49 #include <sys/selinfo.h>
50 #include <sys/queue.h>
51 #include <sys/event.h>
52 #include <sys/eventvar.h>
53 #include <sys/poll.h>
54 #include <sys/protosw.h>
55 #include <sys/sigio.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/stat.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/taskqueue.h>
64 #include <sys/uio.h>
65 #ifdef KTRACE
66 #include <sys/ktrace.h>
67 #endif
68 
69 #include <vm/uma.h>
70 
71 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
72 
73 /*
74  * This lock is used if multiple kq locks are required.  This possibly
75  * should be made into a per proc lock.
76  */
77 static struct mtx	kq_global;
78 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
79 #define KQ_GLOBAL_LOCK(lck, haslck)	do {	\
80 	if (!haslck)				\
81 		mtx_lock(lck);			\
82 	haslck = 1;				\
83 } while (0)
84 #define KQ_GLOBAL_UNLOCK(lck, haslck)	do {	\
85 	if (haslck)				\
86 		mtx_unlock(lck);			\
87 	haslck = 0;				\
88 } while (0)
89 
90 TASKQUEUE_DEFINE_THREAD(kqueue);
91 
92 static int	kevent_copyout(void *arg, struct kevent *kevp, int count);
93 static int	kevent_copyin(void *arg, struct kevent *kevp, int count);
94 static int	kqueue_register(struct kqueue *kq, struct kevent *kev,
95 		    struct thread *td, int waitok);
96 static int	kqueue_acquire(struct file *fp, struct kqueue **kqp);
97 static void	kqueue_release(struct kqueue *kq, int locked);
98 static int	kqueue_expand(struct kqueue *kq, struct filterops *fops,
99 		    uintptr_t ident, int waitok);
100 static void	kqueue_task(void *arg, int pending);
101 static int	kqueue_scan(struct kqueue *kq, int maxevents,
102 		    struct kevent_copyops *k_ops,
103 		    const struct timespec *timeout,
104 		    struct kevent *keva, struct thread *td);
105 static void 	kqueue_wakeup(struct kqueue *kq);
106 static struct filterops *kqueue_fo_find(int filt);
107 static void	kqueue_fo_release(int filt);
108 
109 static fo_rdwr_t	kqueue_read;
110 static fo_rdwr_t	kqueue_write;
111 static fo_truncate_t	kqueue_truncate;
112 static fo_ioctl_t	kqueue_ioctl;
113 static fo_poll_t	kqueue_poll;
114 static fo_kqfilter_t	kqueue_kqfilter;
115 static fo_stat_t	kqueue_stat;
116 static fo_close_t	kqueue_close;
117 
118 static struct fileops kqueueops = {
119 	.fo_read = kqueue_read,
120 	.fo_write = kqueue_write,
121 	.fo_truncate = kqueue_truncate,
122 	.fo_ioctl = kqueue_ioctl,
123 	.fo_poll = kqueue_poll,
124 	.fo_kqfilter = kqueue_kqfilter,
125 	.fo_stat = kqueue_stat,
126 	.fo_close = kqueue_close,
127 	.fo_chmod = invfo_chmod,
128 	.fo_chown = invfo_chown,
129 };
130 
131 static int 	knote_attach(struct knote *kn, struct kqueue *kq);
132 static void 	knote_drop(struct knote *kn, struct thread *td);
133 static void 	knote_enqueue(struct knote *kn);
134 static void 	knote_dequeue(struct knote *kn);
135 static void 	knote_init(void);
136 static struct 	knote *knote_alloc(int waitok);
137 static void 	knote_free(struct knote *kn);
138 
139 static void	filt_kqdetach(struct knote *kn);
140 static int	filt_kqueue(struct knote *kn, long hint);
141 static int	filt_procattach(struct knote *kn);
142 static void	filt_procdetach(struct knote *kn);
143 static int	filt_proc(struct knote *kn, long hint);
144 static int	filt_fileattach(struct knote *kn);
145 static void	filt_timerexpire(void *knx);
146 static int	filt_timerattach(struct knote *kn);
147 static void	filt_timerdetach(struct knote *kn);
148 static int	filt_timer(struct knote *kn, long hint);
149 static int	filt_userattach(struct knote *kn);
150 static void	filt_userdetach(struct knote *kn);
151 static int	filt_user(struct knote *kn, long hint);
152 static void	filt_usertouch(struct knote *kn, struct kevent *kev,
153 		    u_long type);
154 
155 static struct filterops file_filtops = {
156 	.f_isfd = 1,
157 	.f_attach = filt_fileattach,
158 };
159 static struct filterops kqread_filtops = {
160 	.f_isfd = 1,
161 	.f_detach = filt_kqdetach,
162 	.f_event = filt_kqueue,
163 };
164 /* XXX - move to kern_proc.c?  */
165 static struct filterops proc_filtops = {
166 	.f_isfd = 0,
167 	.f_attach = filt_procattach,
168 	.f_detach = filt_procdetach,
169 	.f_event = filt_proc,
170 };
171 static struct filterops timer_filtops = {
172 	.f_isfd = 0,
173 	.f_attach = filt_timerattach,
174 	.f_detach = filt_timerdetach,
175 	.f_event = filt_timer,
176 };
177 static struct filterops user_filtops = {
178 	.f_attach = filt_userattach,
179 	.f_detach = filt_userdetach,
180 	.f_event = filt_user,
181 	.f_touch = filt_usertouch,
182 };
183 
184 static uma_zone_t	knote_zone;
185 static int 		kq_ncallouts = 0;
186 static int 		kq_calloutmax = (4 * 1024);
187 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
188     &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
189 
190 /* XXX - ensure not KN_INFLUX?? */
191 #define KNOTE_ACTIVATE(kn, islock) do { 				\
192 	if ((islock))							\
193 		mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED);		\
194 	else								\
195 		KQ_LOCK((kn)->kn_kq);					\
196 	(kn)->kn_status |= KN_ACTIVE;					\
197 	if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0)		\
198 		knote_enqueue((kn));					\
199 	if (!(islock))							\
200 		KQ_UNLOCK((kn)->kn_kq);					\
201 } while(0)
202 #define KQ_LOCK(kq) do {						\
203 	mtx_lock(&(kq)->kq_lock);					\
204 } while (0)
205 #define KQ_FLUX_WAKEUP(kq) do {						\
206 	if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) {		\
207 		(kq)->kq_state &= ~KQ_FLUXWAIT;				\
208 		wakeup((kq));						\
209 	}								\
210 } while (0)
211 #define KQ_UNLOCK_FLUX(kq) do {						\
212 	KQ_FLUX_WAKEUP(kq);						\
213 	mtx_unlock(&(kq)->kq_lock);					\
214 } while (0)
215 #define KQ_UNLOCK(kq) do {						\
216 	mtx_unlock(&(kq)->kq_lock);					\
217 } while (0)
218 #define KQ_OWNED(kq) do {						\
219 	mtx_assert(&(kq)->kq_lock, MA_OWNED);				\
220 } while (0)
221 #define KQ_NOTOWNED(kq) do {						\
222 	mtx_assert(&(kq)->kq_lock, MA_NOTOWNED);			\
223 } while (0)
224 #define KN_LIST_LOCK(kn) do {						\
225 	if (kn->kn_knlist != NULL)					\
226 		kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg);	\
227 } while (0)
228 #define KN_LIST_UNLOCK(kn) do {						\
229 	if (kn->kn_knlist != NULL) 					\
230 		kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg);	\
231 } while (0)
232 #define	KNL_ASSERT_LOCK(knl, islocked) do {				\
233 	if (islocked)							\
234 		KNL_ASSERT_LOCKED(knl);				\
235 	else								\
236 		KNL_ASSERT_UNLOCKED(knl);				\
237 } while (0)
238 #ifdef INVARIANTS
239 #define	KNL_ASSERT_LOCKED(knl) do {					\
240 	knl->kl_assert_locked((knl)->kl_lockarg);			\
241 } while (0)
242 #define	KNL_ASSERT_UNLOCKED(knl) do {					\
243 	knl->kl_assert_unlocked((knl)->kl_lockarg);			\
244 } while (0)
245 #else /* !INVARIANTS */
246 #define	KNL_ASSERT_LOCKED(knl) do {} while(0)
247 #define	KNL_ASSERT_UNLOCKED(knl) do {} while (0)
248 #endif /* INVARIANTS */
249 
250 #define	KN_HASHSIZE		64		/* XXX should be tunable */
251 #define KN_HASH(val, mask)	(((val) ^ (val >> 8)) & (mask))
252 
253 static int
254 filt_nullattach(struct knote *kn)
255 {
256 
257 	return (ENXIO);
258 };
259 
260 struct filterops null_filtops = {
261 	.f_isfd = 0,
262 	.f_attach = filt_nullattach,
263 };
264 
265 /* XXX - make SYSINIT to add these, and move into respective modules. */
266 extern struct filterops sig_filtops;
267 extern struct filterops fs_filtops;
268 
269 /*
270  * Table for for all system-defined filters.
271  */
272 static struct mtx	filterops_lock;
273 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
274 	MTX_DEF);
275 static struct {
276 	struct filterops *for_fop;
277 	int for_refcnt;
278 } sysfilt_ops[EVFILT_SYSCOUNT] = {
279 	{ &file_filtops },			/* EVFILT_READ */
280 	{ &file_filtops },			/* EVFILT_WRITE */
281 	{ &null_filtops },			/* EVFILT_AIO */
282 	{ &file_filtops },			/* EVFILT_VNODE */
283 	{ &proc_filtops },			/* EVFILT_PROC */
284 	{ &sig_filtops },			/* EVFILT_SIGNAL */
285 	{ &timer_filtops },			/* EVFILT_TIMER */
286 	{ &null_filtops },			/* former EVFILT_NETDEV */
287 	{ &fs_filtops },			/* EVFILT_FS */
288 	{ &null_filtops },			/* EVFILT_LIO */
289 	{ &user_filtops },			/* EVFILT_USER */
290 };
291 
292 /*
293  * Simple redirection for all cdevsw style objects to call their fo_kqfilter
294  * method.
295  */
296 static int
297 filt_fileattach(struct knote *kn)
298 {
299 
300 	return (fo_kqfilter(kn->kn_fp, kn));
301 }
302 
303 /*ARGSUSED*/
304 static int
305 kqueue_kqfilter(struct file *fp, struct knote *kn)
306 {
307 	struct kqueue *kq = kn->kn_fp->f_data;
308 
309 	if (kn->kn_filter != EVFILT_READ)
310 		return (EINVAL);
311 
312 	kn->kn_status |= KN_KQUEUE;
313 	kn->kn_fop = &kqread_filtops;
314 	knlist_add(&kq->kq_sel.si_note, kn, 0);
315 
316 	return (0);
317 }
318 
319 static void
320 filt_kqdetach(struct knote *kn)
321 {
322 	struct kqueue *kq = kn->kn_fp->f_data;
323 
324 	knlist_remove(&kq->kq_sel.si_note, kn, 0);
325 }
326 
327 /*ARGSUSED*/
328 static int
329 filt_kqueue(struct knote *kn, long hint)
330 {
331 	struct kqueue *kq = kn->kn_fp->f_data;
332 
333 	kn->kn_data = kq->kq_count;
334 	return (kn->kn_data > 0);
335 }
336 
337 /* XXX - move to kern_proc.c?  */
338 static int
339 filt_procattach(struct knote *kn)
340 {
341 	struct proc *p;
342 	int immediate;
343 	int error;
344 
345 	immediate = 0;
346 	p = pfind(kn->kn_id);
347 	if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
348 		p = zpfind(kn->kn_id);
349 		immediate = 1;
350 	} else if (p != NULL && (p->p_flag & P_WEXIT)) {
351 		immediate = 1;
352 	}
353 
354 	if (p == NULL)
355 		return (ESRCH);
356 	if ((error = p_cansee(curthread, p))) {
357 		PROC_UNLOCK(p);
358 		return (error);
359 	}
360 
361 	kn->kn_ptr.p_proc = p;
362 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
363 
364 	/*
365 	 * internal flag indicating registration done by kernel
366 	 */
367 	if (kn->kn_flags & EV_FLAG1) {
368 		kn->kn_data = kn->kn_sdata;		/* ppid */
369 		kn->kn_fflags = NOTE_CHILD;
370 		kn->kn_flags &= ~EV_FLAG1;
371 	}
372 
373 	if (immediate == 0)
374 		knlist_add(&p->p_klist, kn, 1);
375 
376 	/*
377 	 * Immediately activate any exit notes if the target process is a
378 	 * zombie.  This is necessary to handle the case where the target
379 	 * process, e.g. a child, dies before the kevent is registered.
380 	 */
381 	if (immediate && filt_proc(kn, NOTE_EXIT))
382 		KNOTE_ACTIVATE(kn, 0);
383 
384 	PROC_UNLOCK(p);
385 
386 	return (0);
387 }
388 
389 /*
390  * The knote may be attached to a different process, which may exit,
391  * leaving nothing for the knote to be attached to.  So when the process
392  * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
393  * it will be deleted when read out.  However, as part of the knote deletion,
394  * this routine is called, so a check is needed to avoid actually performing
395  * a detach, because the original process does not exist any more.
396  */
397 /* XXX - move to kern_proc.c?  */
398 static void
399 filt_procdetach(struct knote *kn)
400 {
401 	struct proc *p;
402 
403 	p = kn->kn_ptr.p_proc;
404 	knlist_remove(&p->p_klist, kn, 0);
405 	kn->kn_ptr.p_proc = NULL;
406 }
407 
408 /* XXX - move to kern_proc.c?  */
409 static int
410 filt_proc(struct knote *kn, long hint)
411 {
412 	struct proc *p = kn->kn_ptr.p_proc;
413 	u_int event;
414 
415 	/*
416 	 * mask off extra data
417 	 */
418 	event = (u_int)hint & NOTE_PCTRLMASK;
419 
420 	/*
421 	 * if the user is interested in this event, record it.
422 	 */
423 	if (kn->kn_sfflags & event)
424 		kn->kn_fflags |= event;
425 
426 	/*
427 	 * process is gone, so flag the event as finished.
428 	 */
429 	if (event == NOTE_EXIT) {
430 		if (!(kn->kn_status & KN_DETACHED))
431 			knlist_remove_inevent(&p->p_klist, kn);
432 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
433 		kn->kn_data = p->p_xstat;
434 		kn->kn_ptr.p_proc = NULL;
435 		return (1);
436 	}
437 
438 	return (kn->kn_fflags != 0);
439 }
440 
441 /*
442  * Called when the process forked. It mostly does the same as the
443  * knote(), activating all knotes registered to be activated when the
444  * process forked. Additionally, for each knote attached to the
445  * parent, check whether user wants to track the new process. If so
446  * attach a new knote to it, and immediately report an event with the
447  * child's pid.
448  */
449 void
450 knote_fork(struct knlist *list, int pid)
451 {
452 	struct kqueue *kq;
453 	struct knote *kn;
454 	struct kevent kev;
455 	int error;
456 
457 	if (list == NULL)
458 		return;
459 	list->kl_lock(list->kl_lockarg);
460 
461 	SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
462 		if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
463 			continue;
464 		kq = kn->kn_kq;
465 		KQ_LOCK(kq);
466 		if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
467 			KQ_UNLOCK(kq);
468 			continue;
469 		}
470 
471 		/*
472 		 * The same as knote(), activate the event.
473 		 */
474 		if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
475 			kn->kn_status |= KN_HASKQLOCK;
476 			if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
477 				KNOTE_ACTIVATE(kn, 1);
478 			kn->kn_status &= ~KN_HASKQLOCK;
479 			KQ_UNLOCK(kq);
480 			continue;
481 		}
482 
483 		/*
484 		 * The NOTE_TRACK case. In addition to the activation
485 		 * of the event, we need to register new event to
486 		 * track the child. Drop the locks in preparation for
487 		 * the call to kqueue_register().
488 		 */
489 		kn->kn_status |= KN_INFLUX;
490 		KQ_UNLOCK(kq);
491 		list->kl_unlock(list->kl_lockarg);
492 
493 		/*
494 		 * Activate existing knote and register a knote with
495 		 * new process.
496 		 */
497 		kev.ident = pid;
498 		kev.filter = kn->kn_filter;
499 		kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
500 		kev.fflags = kn->kn_sfflags;
501 		kev.data = kn->kn_id;		/* parent */
502 		kev.udata = kn->kn_kevent.udata;/* preserve udata */
503 		error = kqueue_register(kq, &kev, NULL, 0);
504 		if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
505 			KNOTE_ACTIVATE(kn, 0);
506 		if (error)
507 			kn->kn_fflags |= NOTE_TRACKERR;
508 		KQ_LOCK(kq);
509 		kn->kn_status &= ~KN_INFLUX;
510 		KQ_UNLOCK_FLUX(kq);
511 		list->kl_lock(list->kl_lockarg);
512 	}
513 	list->kl_unlock(list->kl_lockarg);
514 }
515 
516 /*
517  * XXX: EVFILT_TIMER should perhaps live in kern_time.c beside the
518  * interval timer support code.
519  */
520 static __inline sbintime_t
521 timer2sbintime(intptr_t data)
522 {
523 
524 	return (SBT_1MS * data);
525 }
526 
527 static void
528 filt_timerexpire(void *knx)
529 {
530 	struct callout *calloutp;
531 	struct knote *kn;
532 
533 	kn = knx;
534 	kn->kn_data++;
535 	KNOTE_ACTIVATE(kn, 0);	/* XXX - handle locking */
536 
537 	if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
538 		calloutp = (struct callout *)kn->kn_hook;
539 		callout_reset_sbt_on(calloutp,
540 		    timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
541 		    filt_timerexpire, kn, PCPU_GET(cpuid), 0);
542 	}
543 }
544 
545 /*
546  * data contains amount of time to sleep, in milliseconds
547  */
548 static int
549 filt_timerattach(struct knote *kn)
550 {
551 	struct callout *calloutp;
552 
553 	atomic_add_int(&kq_ncallouts, 1);
554 
555 	if (kq_ncallouts >= kq_calloutmax) {
556 		atomic_add_int(&kq_ncallouts, -1);
557 		return (ENOMEM);
558 	}
559 
560 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
561 	kn->kn_status &= ~KN_DETACHED;		/* knlist_add usually sets it */
562 	calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
563 	callout_init(calloutp, CALLOUT_MPSAFE);
564 	kn->kn_hook = calloutp;
565 	callout_reset_sbt_on(calloutp,
566 	    timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
567 	    filt_timerexpire, kn, PCPU_GET(cpuid), 0);
568 
569 	return (0);
570 }
571 
572 static void
573 filt_timerdetach(struct knote *kn)
574 {
575 	struct callout *calloutp;
576 
577 	calloutp = (struct callout *)kn->kn_hook;
578 	callout_drain(calloutp);
579 	free(calloutp, M_KQUEUE);
580 	atomic_add_int(&kq_ncallouts, -1);
581 	kn->kn_status |= KN_DETACHED;	/* knlist_remove usually clears it */
582 }
583 
584 static int
585 filt_timer(struct knote *kn, long hint)
586 {
587 
588 	return (kn->kn_data != 0);
589 }
590 
591 static int
592 filt_userattach(struct knote *kn)
593 {
594 
595 	/*
596 	 * EVFILT_USER knotes are not attached to anything in the kernel.
597 	 */
598 	kn->kn_hook = NULL;
599 	if (kn->kn_fflags & NOTE_TRIGGER)
600 		kn->kn_hookid = 1;
601 	else
602 		kn->kn_hookid = 0;
603 	return (0);
604 }
605 
606 static void
607 filt_userdetach(__unused struct knote *kn)
608 {
609 
610 	/*
611 	 * EVFILT_USER knotes are not attached to anything in the kernel.
612 	 */
613 }
614 
615 static int
616 filt_user(struct knote *kn, __unused long hint)
617 {
618 
619 	return (kn->kn_hookid);
620 }
621 
622 static void
623 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
624 {
625 	u_int ffctrl;
626 
627 	switch (type) {
628 	case EVENT_REGISTER:
629 		if (kev->fflags & NOTE_TRIGGER)
630 			kn->kn_hookid = 1;
631 
632 		ffctrl = kev->fflags & NOTE_FFCTRLMASK;
633 		kev->fflags &= NOTE_FFLAGSMASK;
634 		switch (ffctrl) {
635 		case NOTE_FFNOP:
636 			break;
637 
638 		case NOTE_FFAND:
639 			kn->kn_sfflags &= kev->fflags;
640 			break;
641 
642 		case NOTE_FFOR:
643 			kn->kn_sfflags |= kev->fflags;
644 			break;
645 
646 		case NOTE_FFCOPY:
647 			kn->kn_sfflags = kev->fflags;
648 			break;
649 
650 		default:
651 			/* XXX Return error? */
652 			break;
653 		}
654 		kn->kn_sdata = kev->data;
655 		if (kev->flags & EV_CLEAR) {
656 			kn->kn_hookid = 0;
657 			kn->kn_data = 0;
658 			kn->kn_fflags = 0;
659 		}
660 		break;
661 
662         case EVENT_PROCESS:
663 		*kev = kn->kn_kevent;
664 		kev->fflags = kn->kn_sfflags;
665 		kev->data = kn->kn_sdata;
666 		if (kn->kn_flags & EV_CLEAR) {
667 			kn->kn_hookid = 0;
668 			kn->kn_data = 0;
669 			kn->kn_fflags = 0;
670 		}
671 		break;
672 
673 	default:
674 		panic("filt_usertouch() - invalid type (%ld)", type);
675 		break;
676 	}
677 }
678 
679 int
680 sys_kqueue(struct thread *td, struct kqueue_args *uap)
681 {
682 	struct filedesc *fdp;
683 	struct kqueue *kq;
684 	struct file *fp;
685 	int fd, error;
686 
687 	fdp = td->td_proc->p_fd;
688 	error = falloc(td, &fp, &fd, 0);
689 	if (error)
690 		goto done2;
691 
692 	/* An extra reference on `fp' has been held for us by falloc(). */
693 	kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
694 	mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
695 	TAILQ_INIT(&kq->kq_head);
696 	kq->kq_fdp = fdp;
697 	knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
698 	TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
699 
700 	FILEDESC_XLOCK(fdp);
701 	SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
702 	FILEDESC_XUNLOCK(fdp);
703 
704 	finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
705 	fdrop(fp, td);
706 
707 	td->td_retval[0] = fd;
708 done2:
709 	return (error);
710 }
711 
712 #ifndef _SYS_SYSPROTO_H_
713 struct kevent_args {
714 	int	fd;
715 	const struct kevent *changelist;
716 	int	nchanges;
717 	struct	kevent *eventlist;
718 	int	nevents;
719 	const struct timespec *timeout;
720 };
721 #endif
722 int
723 sys_kevent(struct thread *td, struct kevent_args *uap)
724 {
725 	struct timespec ts, *tsp;
726 	struct kevent_copyops k_ops = { uap,
727 					kevent_copyout,
728 					kevent_copyin};
729 	int error;
730 #ifdef KTRACE
731 	struct uio ktruio;
732 	struct iovec ktriov;
733 	struct uio *ktruioin = NULL;
734 	struct uio *ktruioout = NULL;
735 #endif
736 
737 	if (uap->timeout != NULL) {
738 		error = copyin(uap->timeout, &ts, sizeof(ts));
739 		if (error)
740 			return (error);
741 		tsp = &ts;
742 	} else
743 		tsp = NULL;
744 
745 #ifdef KTRACE
746 	if (KTRPOINT(td, KTR_GENIO)) {
747 		ktriov.iov_base = uap->changelist;
748 		ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
749 		ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
750 		    .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
751 		    .uio_td = td };
752 		ktruioin = cloneuio(&ktruio);
753 		ktriov.iov_base = uap->eventlist;
754 		ktriov.iov_len = uap->nevents * sizeof(struct kevent);
755 		ktruioout = cloneuio(&ktruio);
756 	}
757 #endif
758 
759 	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
760 	    &k_ops, tsp);
761 
762 #ifdef KTRACE
763 	if (ktruioin != NULL) {
764 		ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
765 		ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
766 		ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
767 		ktrgenio(uap->fd, UIO_READ, ktruioout, error);
768 	}
769 #endif
770 
771 	return (error);
772 }
773 
774 /*
775  * Copy 'count' items into the destination list pointed to by uap->eventlist.
776  */
777 static int
778 kevent_copyout(void *arg, struct kevent *kevp, int count)
779 {
780 	struct kevent_args *uap;
781 	int error;
782 
783 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
784 	uap = (struct kevent_args *)arg;
785 
786 	error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
787 	if (error == 0)
788 		uap->eventlist += count;
789 	return (error);
790 }
791 
792 /*
793  * Copy 'count' items from the list pointed to by uap->changelist.
794  */
795 static int
796 kevent_copyin(void *arg, struct kevent *kevp, int count)
797 {
798 	struct kevent_args *uap;
799 	int error;
800 
801 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
802 	uap = (struct kevent_args *)arg;
803 
804 	error = copyin(uap->changelist, kevp, count * sizeof *kevp);
805 	if (error == 0)
806 		uap->changelist += count;
807 	return (error);
808 }
809 
810 int
811 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
812     struct kevent_copyops *k_ops, const struct timespec *timeout)
813 {
814 	struct kevent keva[KQ_NEVENTS];
815 	struct kevent *kevp, *changes;
816 	struct kqueue *kq;
817 	struct file *fp;
818 	int i, n, nerrors, error;
819 
820 	if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
821 		return (error);
822 	if ((error = kqueue_acquire(fp, &kq)) != 0)
823 		goto done_norel;
824 
825 	nerrors = 0;
826 
827 	while (nchanges > 0) {
828 		n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
829 		error = k_ops->k_copyin(k_ops->arg, keva, n);
830 		if (error)
831 			goto done;
832 		changes = keva;
833 		for (i = 0; i < n; i++) {
834 			kevp = &changes[i];
835 			if (!kevp->filter)
836 				continue;
837 			kevp->flags &= ~EV_SYSFLAGS;
838 			error = kqueue_register(kq, kevp, td, 1);
839 			if (error || (kevp->flags & EV_RECEIPT)) {
840 				if (nevents != 0) {
841 					kevp->flags = EV_ERROR;
842 					kevp->data = error;
843 					(void) k_ops->k_copyout(k_ops->arg,
844 					    kevp, 1);
845 					nevents--;
846 					nerrors++;
847 				} else {
848 					goto done;
849 				}
850 			}
851 		}
852 		nchanges -= n;
853 	}
854 	if (nerrors) {
855 		td->td_retval[0] = nerrors;
856 		error = 0;
857 		goto done;
858 	}
859 
860 	error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
861 done:
862 	kqueue_release(kq, 0);
863 done_norel:
864 	fdrop(fp, td);
865 	return (error);
866 }
867 
868 int
869 kqueue_add_filteropts(int filt, struct filterops *filtops)
870 {
871 	int error;
872 
873 	error = 0;
874 	if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
875 		printf(
876 "trying to add a filterop that is out of range: %d is beyond %d\n",
877 		    ~filt, EVFILT_SYSCOUNT);
878 		return EINVAL;
879 	}
880 	mtx_lock(&filterops_lock);
881 	if (sysfilt_ops[~filt].for_fop != &null_filtops &&
882 	    sysfilt_ops[~filt].for_fop != NULL)
883 		error = EEXIST;
884 	else {
885 		sysfilt_ops[~filt].for_fop = filtops;
886 		sysfilt_ops[~filt].for_refcnt = 0;
887 	}
888 	mtx_unlock(&filterops_lock);
889 
890 	return (error);
891 }
892 
893 int
894 kqueue_del_filteropts(int filt)
895 {
896 	int error;
897 
898 	error = 0;
899 	if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
900 		return EINVAL;
901 
902 	mtx_lock(&filterops_lock);
903 	if (sysfilt_ops[~filt].for_fop == &null_filtops ||
904 	    sysfilt_ops[~filt].for_fop == NULL)
905 		error = EINVAL;
906 	else if (sysfilt_ops[~filt].for_refcnt != 0)
907 		error = EBUSY;
908 	else {
909 		sysfilt_ops[~filt].for_fop = &null_filtops;
910 		sysfilt_ops[~filt].for_refcnt = 0;
911 	}
912 	mtx_unlock(&filterops_lock);
913 
914 	return error;
915 }
916 
917 static struct filterops *
918 kqueue_fo_find(int filt)
919 {
920 
921 	if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
922 		return NULL;
923 
924 	mtx_lock(&filterops_lock);
925 	sysfilt_ops[~filt].for_refcnt++;
926 	if (sysfilt_ops[~filt].for_fop == NULL)
927 		sysfilt_ops[~filt].for_fop = &null_filtops;
928 	mtx_unlock(&filterops_lock);
929 
930 	return sysfilt_ops[~filt].for_fop;
931 }
932 
933 static void
934 kqueue_fo_release(int filt)
935 {
936 
937 	if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
938 		return;
939 
940 	mtx_lock(&filterops_lock);
941 	KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
942 	    ("filter object refcount not valid on release"));
943 	sysfilt_ops[~filt].for_refcnt--;
944 	mtx_unlock(&filterops_lock);
945 }
946 
947 /*
948  * A ref to kq (obtained via kqueue_acquire) must be held.  waitok will
949  * influence if memory allocation should wait.  Make sure it is 0 if you
950  * hold any mutexes.
951  */
952 static int
953 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
954 {
955 	struct filterops *fops;
956 	struct file *fp;
957 	struct knote *kn, *tkn;
958 	int error, filt, event;
959 	int haskqglobal;
960 
961 	fp = NULL;
962 	kn = NULL;
963 	error = 0;
964 	haskqglobal = 0;
965 
966 	filt = kev->filter;
967 	fops = kqueue_fo_find(filt);
968 	if (fops == NULL)
969 		return EINVAL;
970 
971 	tkn = knote_alloc(waitok);		/* prevent waiting with locks */
972 
973 findkn:
974 	if (fops->f_isfd) {
975 		KASSERT(td != NULL, ("td is NULL"));
976 		error = fget(td, kev->ident, CAP_POLL_EVENT, &fp);
977 		if (error)
978 			goto done;
979 
980 		if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
981 		    kev->ident, 0) != 0) {
982 			/* try again */
983 			fdrop(fp, td);
984 			fp = NULL;
985 			error = kqueue_expand(kq, fops, kev->ident, waitok);
986 			if (error)
987 				goto done;
988 			goto findkn;
989 		}
990 
991 		if (fp->f_type == DTYPE_KQUEUE) {
992 			/*
993 			 * if we add some inteligence about what we are doing,
994 			 * we should be able to support events on ourselves.
995 			 * We need to know when we are doing this to prevent
996 			 * getting both the knlist lock and the kq lock since
997 			 * they are the same thing.
998 			 */
999 			if (fp->f_data == kq) {
1000 				error = EINVAL;
1001 				goto done;
1002 			}
1003 
1004 			KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1005 		}
1006 
1007 		KQ_LOCK(kq);
1008 		if (kev->ident < kq->kq_knlistsize) {
1009 			SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1010 				if (kev->filter == kn->kn_filter)
1011 					break;
1012 		}
1013 	} else {
1014 		if ((kev->flags & EV_ADD) == EV_ADD)
1015 			kqueue_expand(kq, fops, kev->ident, waitok);
1016 
1017 		KQ_LOCK(kq);
1018 		if (kq->kq_knhashmask != 0) {
1019 			struct klist *list;
1020 
1021 			list = &kq->kq_knhash[
1022 			    KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1023 			SLIST_FOREACH(kn, list, kn_link)
1024 				if (kev->ident == kn->kn_id &&
1025 				    kev->filter == kn->kn_filter)
1026 					break;
1027 		}
1028 	}
1029 
1030 	/* knote is in the process of changing, wait for it to stablize. */
1031 	if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1032 		KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1033 		kq->kq_state |= KQ_FLUXWAIT;
1034 		msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1035 		if (fp != NULL) {
1036 			fdrop(fp, td);
1037 			fp = NULL;
1038 		}
1039 		goto findkn;
1040 	}
1041 
1042 	/*
1043 	 * kn now contains the matching knote, or NULL if no match
1044 	 */
1045 	if (kn == NULL) {
1046 		if (kev->flags & EV_ADD) {
1047 			kn = tkn;
1048 			tkn = NULL;
1049 			if (kn == NULL) {
1050 				KQ_UNLOCK(kq);
1051 				error = ENOMEM;
1052 				goto done;
1053 			}
1054 			kn->kn_fp = fp;
1055 			kn->kn_kq = kq;
1056 			kn->kn_fop = fops;
1057 			/*
1058 			 * apply reference counts to knote structure, and
1059 			 * do not release it at the end of this routine.
1060 			 */
1061 			fops = NULL;
1062 			fp = NULL;
1063 
1064 			kn->kn_sfflags = kev->fflags;
1065 			kn->kn_sdata = kev->data;
1066 			kev->fflags = 0;
1067 			kev->data = 0;
1068 			kn->kn_kevent = *kev;
1069 			kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1070 			    EV_ENABLE | EV_DISABLE);
1071 			kn->kn_status = KN_INFLUX|KN_DETACHED;
1072 
1073 			error = knote_attach(kn, kq);
1074 			KQ_UNLOCK(kq);
1075 			if (error != 0) {
1076 				tkn = kn;
1077 				goto done;
1078 			}
1079 
1080 			if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1081 				knote_drop(kn, td);
1082 				goto done;
1083 			}
1084 			KN_LIST_LOCK(kn);
1085 			goto done_ev_add;
1086 		} else {
1087 			/* No matching knote and the EV_ADD flag is not set. */
1088 			KQ_UNLOCK(kq);
1089 			error = ENOENT;
1090 			goto done;
1091 		}
1092 	}
1093 
1094 	if (kev->flags & EV_DELETE) {
1095 		kn->kn_status |= KN_INFLUX;
1096 		KQ_UNLOCK(kq);
1097 		if (!(kn->kn_status & KN_DETACHED))
1098 			kn->kn_fop->f_detach(kn);
1099 		knote_drop(kn, td);
1100 		goto done;
1101 	}
1102 
1103 	/*
1104 	 * The user may change some filter values after the initial EV_ADD,
1105 	 * but doing so will not reset any filter which has already been
1106 	 * triggered.
1107 	 */
1108 	kn->kn_status |= KN_INFLUX;
1109 	KQ_UNLOCK(kq);
1110 	KN_LIST_LOCK(kn);
1111 	kn->kn_kevent.udata = kev->udata;
1112 	if (!fops->f_isfd && fops->f_touch != NULL) {
1113 		fops->f_touch(kn, kev, EVENT_REGISTER);
1114 	} else {
1115 		kn->kn_sfflags = kev->fflags;
1116 		kn->kn_sdata = kev->data;
1117 	}
1118 
1119 	/*
1120 	 * We can get here with kn->kn_knlist == NULL.  This can happen when
1121 	 * the initial attach event decides that the event is "completed"
1122 	 * already.  i.e. filt_procattach is called on a zombie process.  It
1123 	 * will call filt_proc which will remove it from the list, and NULL
1124 	 * kn_knlist.
1125 	 */
1126 done_ev_add:
1127 	event = kn->kn_fop->f_event(kn, 0);
1128 	KQ_LOCK(kq);
1129 	if (event)
1130 		KNOTE_ACTIVATE(kn, 1);
1131 	kn->kn_status &= ~KN_INFLUX;
1132 	KN_LIST_UNLOCK(kn);
1133 
1134 	if ((kev->flags & EV_DISABLE) &&
1135 	    ((kn->kn_status & KN_DISABLED) == 0)) {
1136 		kn->kn_status |= KN_DISABLED;
1137 	}
1138 
1139 	if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1140 		kn->kn_status &= ~KN_DISABLED;
1141 		if ((kn->kn_status & KN_ACTIVE) &&
1142 		    ((kn->kn_status & KN_QUEUED) == 0))
1143 			knote_enqueue(kn);
1144 	}
1145 	KQ_UNLOCK_FLUX(kq);
1146 
1147 done:
1148 	KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1149 	if (fp != NULL)
1150 		fdrop(fp, td);
1151 	if (tkn != NULL)
1152 		knote_free(tkn);
1153 	if (fops != NULL)
1154 		kqueue_fo_release(filt);
1155 	return (error);
1156 }
1157 
1158 static int
1159 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1160 {
1161 	int error;
1162 	struct kqueue *kq;
1163 
1164 	error = 0;
1165 
1166 	kq = fp->f_data;
1167 	if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1168 		return (EBADF);
1169 	*kqp = kq;
1170 	KQ_LOCK(kq);
1171 	if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1172 		KQ_UNLOCK(kq);
1173 		return (EBADF);
1174 	}
1175 	kq->kq_refcnt++;
1176 	KQ_UNLOCK(kq);
1177 
1178 	return error;
1179 }
1180 
1181 static void
1182 kqueue_release(struct kqueue *kq, int locked)
1183 {
1184 	if (locked)
1185 		KQ_OWNED(kq);
1186 	else
1187 		KQ_LOCK(kq);
1188 	kq->kq_refcnt--;
1189 	if (kq->kq_refcnt == 1)
1190 		wakeup(&kq->kq_refcnt);
1191 	if (!locked)
1192 		KQ_UNLOCK(kq);
1193 }
1194 
1195 static void
1196 kqueue_schedtask(struct kqueue *kq)
1197 {
1198 
1199 	KQ_OWNED(kq);
1200 	KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1201 	    ("scheduling kqueue task while draining"));
1202 
1203 	if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1204 		taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1205 		kq->kq_state |= KQ_TASKSCHED;
1206 	}
1207 }
1208 
1209 /*
1210  * Expand the kq to make sure we have storage for fops/ident pair.
1211  *
1212  * Return 0 on success (or no work necessary), return errno on failure.
1213  *
1214  * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1215  * If kqueue_register is called from a non-fd context, there usually/should
1216  * be no locks held.
1217  */
1218 static int
1219 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1220 	int waitok)
1221 {
1222 	struct klist *list, *tmp_knhash, *to_free;
1223 	u_long tmp_knhashmask;
1224 	int size;
1225 	int fd;
1226 	int mflag = waitok ? M_WAITOK : M_NOWAIT;
1227 
1228 	KQ_NOTOWNED(kq);
1229 
1230 	to_free = NULL;
1231 	if (fops->f_isfd) {
1232 		fd = ident;
1233 		if (kq->kq_knlistsize <= fd) {
1234 			size = kq->kq_knlistsize;
1235 			while (size <= fd)
1236 				size += KQEXTENT;
1237 			list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1238 			if (list == NULL)
1239 				return ENOMEM;
1240 			KQ_LOCK(kq);
1241 			if (kq->kq_knlistsize > fd) {
1242 				to_free = list;
1243 				list = NULL;
1244 			} else {
1245 				if (kq->kq_knlist != NULL) {
1246 					bcopy(kq->kq_knlist, list,
1247 					    kq->kq_knlistsize * sizeof(*list));
1248 					to_free = kq->kq_knlist;
1249 					kq->kq_knlist = NULL;
1250 				}
1251 				bzero((caddr_t)list +
1252 				    kq->kq_knlistsize * sizeof(*list),
1253 				    (size - kq->kq_knlistsize) * sizeof(*list));
1254 				kq->kq_knlistsize = size;
1255 				kq->kq_knlist = list;
1256 			}
1257 			KQ_UNLOCK(kq);
1258 		}
1259 	} else {
1260 		if (kq->kq_knhashmask == 0) {
1261 			tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1262 			    &tmp_knhashmask);
1263 			if (tmp_knhash == NULL)
1264 				return ENOMEM;
1265 			KQ_LOCK(kq);
1266 			if (kq->kq_knhashmask == 0) {
1267 				kq->kq_knhash = tmp_knhash;
1268 				kq->kq_knhashmask = tmp_knhashmask;
1269 			} else {
1270 				to_free = tmp_knhash;
1271 			}
1272 			KQ_UNLOCK(kq);
1273 		}
1274 	}
1275 	free(to_free, M_KQUEUE);
1276 
1277 	KQ_NOTOWNED(kq);
1278 	return 0;
1279 }
1280 
1281 static void
1282 kqueue_task(void *arg, int pending)
1283 {
1284 	struct kqueue *kq;
1285 	int haskqglobal;
1286 
1287 	haskqglobal = 0;
1288 	kq = arg;
1289 
1290 	KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1291 	KQ_LOCK(kq);
1292 
1293 	KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1294 
1295 	kq->kq_state &= ~KQ_TASKSCHED;
1296 	if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1297 		wakeup(&kq->kq_state);
1298 	}
1299 	KQ_UNLOCK(kq);
1300 	KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1301 }
1302 
1303 /*
1304  * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1305  * We treat KN_MARKER knotes as if they are INFLUX.
1306  */
1307 static int
1308 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1309     const struct timespec *tsp, struct kevent *keva, struct thread *td)
1310 {
1311 	struct kevent *kevp;
1312 	struct knote *kn, *marker;
1313 	sbintime_t asbt, rsbt;
1314 	int count, error, haskqglobal, influx, nkev, touch;
1315 
1316 	count = maxevents;
1317 	nkev = 0;
1318 	error = 0;
1319 	haskqglobal = 0;
1320 
1321 	if (maxevents == 0)
1322 		goto done_nl;
1323 
1324 	rsbt = 0;
1325 	if (tsp != NULL) {
1326 		if (tsp->tv_sec < 0 || tsp->tv_nsec < 0 ||
1327 		    tsp->tv_nsec >= 1000000000) {
1328 			error = EINVAL;
1329 			goto done_nl;
1330 		}
1331 		if (timespecisset(tsp)) {
1332 			if (tsp->tv_sec <= INT32_MAX) {
1333 				rsbt = tstosbt(*tsp);
1334 				if (TIMESEL(&asbt, rsbt))
1335 					asbt += tc_tick_sbt;
1336 				if (asbt <= INT64_MAX - rsbt)
1337 					asbt += rsbt;
1338 				else
1339 					asbt = 0;
1340 				rsbt >>= tc_precexp;
1341 			} else
1342 				asbt = 0;
1343 		} else
1344 			asbt = -1;
1345 	} else
1346 		asbt = 0;
1347 	marker = knote_alloc(1);
1348 	if (marker == NULL) {
1349 		error = ENOMEM;
1350 		goto done_nl;
1351 	}
1352 	marker->kn_status = KN_MARKER;
1353 	KQ_LOCK(kq);
1354 
1355 retry:
1356 	kevp = keva;
1357 	if (kq->kq_count == 0) {
1358 		if (asbt == -1) {
1359 			error = EWOULDBLOCK;
1360 		} else {
1361 			kq->kq_state |= KQ_SLEEP;
1362 			error = msleep_sbt(kq, &kq->kq_lock, PSOCK | PCATCH,
1363 			    "kqread", asbt, rsbt, C_ABSOLUTE);
1364 		}
1365 		if (error == 0)
1366 			goto retry;
1367 		/* don't restart after signals... */
1368 		if (error == ERESTART)
1369 			error = EINTR;
1370 		else if (error == EWOULDBLOCK)
1371 			error = 0;
1372 		goto done;
1373 	}
1374 
1375 	TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1376 	influx = 0;
1377 	while (count) {
1378 		KQ_OWNED(kq);
1379 		kn = TAILQ_FIRST(&kq->kq_head);
1380 
1381 		if ((kn->kn_status == KN_MARKER && kn != marker) ||
1382 		    (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1383 			if (influx) {
1384 				influx = 0;
1385 				KQ_FLUX_WAKEUP(kq);
1386 			}
1387 			kq->kq_state |= KQ_FLUXWAIT;
1388 			error = msleep(kq, &kq->kq_lock, PSOCK,
1389 			    "kqflxwt", 0);
1390 			continue;
1391 		}
1392 
1393 		TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1394 		if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1395 			kn->kn_status &= ~KN_QUEUED;
1396 			kq->kq_count--;
1397 			continue;
1398 		}
1399 		if (kn == marker) {
1400 			KQ_FLUX_WAKEUP(kq);
1401 			if (count == maxevents)
1402 				goto retry;
1403 			goto done;
1404 		}
1405 		KASSERT((kn->kn_status & KN_INFLUX) == 0,
1406 		    ("KN_INFLUX set when not suppose to be"));
1407 
1408 		if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1409 			kn->kn_status &= ~KN_QUEUED;
1410 			kn->kn_status |= KN_INFLUX;
1411 			kq->kq_count--;
1412 			KQ_UNLOCK(kq);
1413 			/*
1414 			 * We don't need to lock the list since we've marked
1415 			 * it _INFLUX.
1416 			 */
1417 			*kevp = kn->kn_kevent;
1418 			if (!(kn->kn_status & KN_DETACHED))
1419 				kn->kn_fop->f_detach(kn);
1420 			knote_drop(kn, td);
1421 			KQ_LOCK(kq);
1422 			kn = NULL;
1423 		} else {
1424 			kn->kn_status |= KN_INFLUX;
1425 			KQ_UNLOCK(kq);
1426 			if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1427 				KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1428 			KN_LIST_LOCK(kn);
1429 			if (kn->kn_fop->f_event(kn, 0) == 0) {
1430 				KQ_LOCK(kq);
1431 				KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1432 				kn->kn_status &=
1433 				    ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1434 				kq->kq_count--;
1435 				KN_LIST_UNLOCK(kn);
1436 				influx = 1;
1437 				continue;
1438 			}
1439 			touch = (!kn->kn_fop->f_isfd &&
1440 			    kn->kn_fop->f_touch != NULL);
1441 			if (touch)
1442 				kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
1443 			else
1444 				*kevp = kn->kn_kevent;
1445 			KQ_LOCK(kq);
1446 			KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1447 			if (kn->kn_flags & (EV_CLEAR |  EV_DISPATCH)) {
1448 				/*
1449 				 * Manually clear knotes who weren't
1450 				 * 'touch'ed.
1451 				 */
1452 				if (touch == 0 && kn->kn_flags & EV_CLEAR) {
1453 					kn->kn_data = 0;
1454 					kn->kn_fflags = 0;
1455 				}
1456 				if (kn->kn_flags & EV_DISPATCH)
1457 					kn->kn_status |= KN_DISABLED;
1458 				kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1459 				kq->kq_count--;
1460 			} else
1461 				TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1462 
1463 			kn->kn_status &= ~(KN_INFLUX);
1464 			KN_LIST_UNLOCK(kn);
1465 			influx = 1;
1466 		}
1467 
1468 		/* we are returning a copy to the user */
1469 		kevp++;
1470 		nkev++;
1471 		count--;
1472 
1473 		if (nkev == KQ_NEVENTS) {
1474 			influx = 0;
1475 			KQ_UNLOCK_FLUX(kq);
1476 			error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1477 			nkev = 0;
1478 			kevp = keva;
1479 			KQ_LOCK(kq);
1480 			if (error)
1481 				break;
1482 		}
1483 	}
1484 	TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1485 done:
1486 	KQ_OWNED(kq);
1487 	KQ_UNLOCK_FLUX(kq);
1488 	knote_free(marker);
1489 done_nl:
1490 	KQ_NOTOWNED(kq);
1491 	if (nkev != 0)
1492 		error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1493 	td->td_retval[0] = maxevents - count;
1494 	return (error);
1495 }
1496 
1497 /*
1498  * XXX
1499  * This could be expanded to call kqueue_scan, if desired.
1500  */
1501 /*ARGSUSED*/
1502 static int
1503 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1504 	int flags, struct thread *td)
1505 {
1506 	return (ENXIO);
1507 }
1508 
1509 /*ARGSUSED*/
1510 static int
1511 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1512 	 int flags, struct thread *td)
1513 {
1514 	return (ENXIO);
1515 }
1516 
1517 /*ARGSUSED*/
1518 static int
1519 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1520 	struct thread *td)
1521 {
1522 
1523 	return (EINVAL);
1524 }
1525 
1526 /*ARGSUSED*/
1527 static int
1528 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1529 	struct ucred *active_cred, struct thread *td)
1530 {
1531 	/*
1532 	 * Enabling sigio causes two major problems:
1533 	 * 1) infinite recursion:
1534 	 * Synopsys: kevent is being used to track signals and have FIOASYNC
1535 	 * set.  On receipt of a signal this will cause a kqueue to recurse
1536 	 * into itself over and over.  Sending the sigio causes the kqueue
1537 	 * to become ready, which in turn posts sigio again, forever.
1538 	 * Solution: this can be solved by setting a flag in the kqueue that
1539 	 * we have a SIGIO in progress.
1540 	 * 2) locking problems:
1541 	 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1542 	 * us above the proc and pgrp locks.
1543 	 * Solution: Post a signal using an async mechanism, being sure to
1544 	 * record a generation count in the delivery so that we do not deliver
1545 	 * a signal to the wrong process.
1546 	 *
1547 	 * Note, these two mechanisms are somewhat mutually exclusive!
1548 	 */
1549 #if 0
1550 	struct kqueue *kq;
1551 
1552 	kq = fp->f_data;
1553 	switch (cmd) {
1554 	case FIOASYNC:
1555 		if (*(int *)data) {
1556 			kq->kq_state |= KQ_ASYNC;
1557 		} else {
1558 			kq->kq_state &= ~KQ_ASYNC;
1559 		}
1560 		return (0);
1561 
1562 	case FIOSETOWN:
1563 		return (fsetown(*(int *)data, &kq->kq_sigio));
1564 
1565 	case FIOGETOWN:
1566 		*(int *)data = fgetown(&kq->kq_sigio);
1567 		return (0);
1568 	}
1569 #endif
1570 
1571 	return (ENOTTY);
1572 }
1573 
1574 /*ARGSUSED*/
1575 static int
1576 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1577 	struct thread *td)
1578 {
1579 	struct kqueue *kq;
1580 	int revents = 0;
1581 	int error;
1582 
1583 	if ((error = kqueue_acquire(fp, &kq)))
1584 		return POLLERR;
1585 
1586 	KQ_LOCK(kq);
1587 	if (events & (POLLIN | POLLRDNORM)) {
1588 		if (kq->kq_count) {
1589 			revents |= events & (POLLIN | POLLRDNORM);
1590 		} else {
1591 			selrecord(td, &kq->kq_sel);
1592 			if (SEL_WAITING(&kq->kq_sel))
1593 				kq->kq_state |= KQ_SEL;
1594 		}
1595 	}
1596 	kqueue_release(kq, 1);
1597 	KQ_UNLOCK(kq);
1598 	return (revents);
1599 }
1600 
1601 /*ARGSUSED*/
1602 static int
1603 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1604 	struct thread *td)
1605 {
1606 
1607 	bzero((void *)st, sizeof *st);
1608 	/*
1609 	 * We no longer return kq_count because the unlocked value is useless.
1610 	 * If you spent all this time getting the count, why not spend your
1611 	 * syscall better by calling kevent?
1612 	 *
1613 	 * XXX - This is needed for libc_r.
1614 	 */
1615 	st->st_mode = S_IFIFO;
1616 	return (0);
1617 }
1618 
1619 /*ARGSUSED*/
1620 static int
1621 kqueue_close(struct file *fp, struct thread *td)
1622 {
1623 	struct kqueue *kq = fp->f_data;
1624 	struct filedesc *fdp;
1625 	struct knote *kn;
1626 	int i;
1627 	int error;
1628 
1629 	if ((error = kqueue_acquire(fp, &kq)))
1630 		return error;
1631 
1632 	KQ_LOCK(kq);
1633 
1634 	KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1635 	    ("kqueue already closing"));
1636 	kq->kq_state |= KQ_CLOSING;
1637 	if (kq->kq_refcnt > 1)
1638 		msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1639 
1640 	KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1641 	fdp = kq->kq_fdp;
1642 
1643 	KASSERT(knlist_empty(&kq->kq_sel.si_note),
1644 	    ("kqueue's knlist not empty"));
1645 
1646 	for (i = 0; i < kq->kq_knlistsize; i++) {
1647 		while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1648 			if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1649 				kq->kq_state |= KQ_FLUXWAIT;
1650 				msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1651 				continue;
1652 			}
1653 			kn->kn_status |= KN_INFLUX;
1654 			KQ_UNLOCK(kq);
1655 			if (!(kn->kn_status & KN_DETACHED))
1656 				kn->kn_fop->f_detach(kn);
1657 			knote_drop(kn, td);
1658 			KQ_LOCK(kq);
1659 		}
1660 	}
1661 	if (kq->kq_knhashmask != 0) {
1662 		for (i = 0; i <= kq->kq_knhashmask; i++) {
1663 			while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1664 				if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1665 					kq->kq_state |= KQ_FLUXWAIT;
1666 					msleep(kq, &kq->kq_lock, PSOCK,
1667 					       "kqclo2", 0);
1668 					continue;
1669 				}
1670 				kn->kn_status |= KN_INFLUX;
1671 				KQ_UNLOCK(kq);
1672 				if (!(kn->kn_status & KN_DETACHED))
1673 					kn->kn_fop->f_detach(kn);
1674 				knote_drop(kn, td);
1675 				KQ_LOCK(kq);
1676 			}
1677 		}
1678 	}
1679 
1680 	if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1681 		kq->kq_state |= KQ_TASKDRAIN;
1682 		msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1683 	}
1684 
1685 	if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1686 		selwakeuppri(&kq->kq_sel, PSOCK);
1687 		if (!SEL_WAITING(&kq->kq_sel))
1688 			kq->kq_state &= ~KQ_SEL;
1689 	}
1690 
1691 	KQ_UNLOCK(kq);
1692 
1693 	FILEDESC_XLOCK(fdp);
1694 	SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1695 	FILEDESC_XUNLOCK(fdp);
1696 
1697 	seldrain(&kq->kq_sel);
1698 	knlist_destroy(&kq->kq_sel.si_note);
1699 	mtx_destroy(&kq->kq_lock);
1700 	kq->kq_fdp = NULL;
1701 
1702 	if (kq->kq_knhash != NULL)
1703 		free(kq->kq_knhash, M_KQUEUE);
1704 	if (kq->kq_knlist != NULL)
1705 		free(kq->kq_knlist, M_KQUEUE);
1706 
1707 	funsetown(&kq->kq_sigio);
1708 	free(kq, M_KQUEUE);
1709 	fp->f_data = NULL;
1710 
1711 	return (0);
1712 }
1713 
1714 static void
1715 kqueue_wakeup(struct kqueue *kq)
1716 {
1717 	KQ_OWNED(kq);
1718 
1719 	if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1720 		kq->kq_state &= ~KQ_SLEEP;
1721 		wakeup(kq);
1722 	}
1723 	if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1724 		selwakeuppri(&kq->kq_sel, PSOCK);
1725 		if (!SEL_WAITING(&kq->kq_sel))
1726 			kq->kq_state &= ~KQ_SEL;
1727 	}
1728 	if (!knlist_empty(&kq->kq_sel.si_note))
1729 		kqueue_schedtask(kq);
1730 	if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1731 		pgsigio(&kq->kq_sigio, SIGIO, 0);
1732 	}
1733 }
1734 
1735 /*
1736  * Walk down a list of knotes, activating them if their event has triggered.
1737  *
1738  * There is a possibility to optimize in the case of one kq watching another.
1739  * Instead of scheduling a task to wake it up, you could pass enough state
1740  * down the chain to make up the parent kqueue.  Make this code functional
1741  * first.
1742  */
1743 void
1744 knote(struct knlist *list, long hint, int lockflags)
1745 {
1746 	struct kqueue *kq;
1747 	struct knote *kn;
1748 	int error;
1749 
1750 	if (list == NULL)
1751 		return;
1752 
1753 	KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1754 
1755 	if ((lockflags & KNF_LISTLOCKED) == 0)
1756 		list->kl_lock(list->kl_lockarg);
1757 
1758 	/*
1759 	 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1760 	 * the kqueue scheduling, but this will introduce four
1761 	 * lock/unlock's for each knote to test.  If we do, continue to use
1762 	 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1763 	 * only safe if you want to remove the current item, which we are
1764 	 * not doing.
1765 	 */
1766 	SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1767 		kq = kn->kn_kq;
1768 		if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1769 			KQ_LOCK(kq);
1770 			if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1771 				KQ_UNLOCK(kq);
1772 			} else if ((lockflags & KNF_NOKQLOCK) != 0) {
1773 				kn->kn_status |= KN_INFLUX;
1774 				KQ_UNLOCK(kq);
1775 				error = kn->kn_fop->f_event(kn, hint);
1776 				KQ_LOCK(kq);
1777 				kn->kn_status &= ~KN_INFLUX;
1778 				if (error)
1779 					KNOTE_ACTIVATE(kn, 1);
1780 				KQ_UNLOCK_FLUX(kq);
1781 			} else {
1782 				kn->kn_status |= KN_HASKQLOCK;
1783 				if (kn->kn_fop->f_event(kn, hint))
1784 					KNOTE_ACTIVATE(kn, 1);
1785 				kn->kn_status &= ~KN_HASKQLOCK;
1786 				KQ_UNLOCK(kq);
1787 			}
1788 		}
1789 		kq = NULL;
1790 	}
1791 	if ((lockflags & KNF_LISTLOCKED) == 0)
1792 		list->kl_unlock(list->kl_lockarg);
1793 }
1794 
1795 /*
1796  * add a knote to a knlist
1797  */
1798 void
1799 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1800 {
1801 	KNL_ASSERT_LOCK(knl, islocked);
1802 	KQ_NOTOWNED(kn->kn_kq);
1803 	KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1804 	    (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1805 	if (!islocked)
1806 		knl->kl_lock(knl->kl_lockarg);
1807 	SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1808 	if (!islocked)
1809 		knl->kl_unlock(knl->kl_lockarg);
1810 	KQ_LOCK(kn->kn_kq);
1811 	kn->kn_knlist = knl;
1812 	kn->kn_status &= ~KN_DETACHED;
1813 	KQ_UNLOCK(kn->kn_kq);
1814 }
1815 
1816 static void
1817 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1818 {
1819 	KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1820 	KNL_ASSERT_LOCK(knl, knlislocked);
1821 	mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1822 	if (!kqislocked)
1823 		KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1824     ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1825 	if (!knlislocked)
1826 		knl->kl_lock(knl->kl_lockarg);
1827 	SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1828 	kn->kn_knlist = NULL;
1829 	if (!knlislocked)
1830 		knl->kl_unlock(knl->kl_lockarg);
1831 	if (!kqislocked)
1832 		KQ_LOCK(kn->kn_kq);
1833 	kn->kn_status |= KN_DETACHED;
1834 	if (!kqislocked)
1835 		KQ_UNLOCK(kn->kn_kq);
1836 }
1837 
1838 /*
1839  * remove all knotes from a specified klist
1840  */
1841 void
1842 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1843 {
1844 
1845 	knlist_remove_kq(knl, kn, islocked, 0);
1846 }
1847 
1848 /*
1849  * remove knote from a specified klist while in f_event handler.
1850  */
1851 void
1852 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1853 {
1854 
1855 	knlist_remove_kq(knl, kn, 1,
1856 	    (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1857 }
1858 
1859 int
1860 knlist_empty(struct knlist *knl)
1861 {
1862 
1863 	KNL_ASSERT_LOCKED(knl);
1864 	return SLIST_EMPTY(&knl->kl_list);
1865 }
1866 
1867 static struct mtx	knlist_lock;
1868 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1869 	MTX_DEF);
1870 static void knlist_mtx_lock(void *arg);
1871 static void knlist_mtx_unlock(void *arg);
1872 
1873 static void
1874 knlist_mtx_lock(void *arg)
1875 {
1876 
1877 	mtx_lock((struct mtx *)arg);
1878 }
1879 
1880 static void
1881 knlist_mtx_unlock(void *arg)
1882 {
1883 
1884 	mtx_unlock((struct mtx *)arg);
1885 }
1886 
1887 static void
1888 knlist_mtx_assert_locked(void *arg)
1889 {
1890 
1891 	mtx_assert((struct mtx *)arg, MA_OWNED);
1892 }
1893 
1894 static void
1895 knlist_mtx_assert_unlocked(void *arg)
1896 {
1897 
1898 	mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1899 }
1900 
1901 static void
1902 knlist_rw_rlock(void *arg)
1903 {
1904 
1905 	rw_rlock((struct rwlock *)arg);
1906 }
1907 
1908 static void
1909 knlist_rw_runlock(void *arg)
1910 {
1911 
1912 	rw_runlock((struct rwlock *)arg);
1913 }
1914 
1915 static void
1916 knlist_rw_assert_locked(void *arg)
1917 {
1918 
1919 	rw_assert((struct rwlock *)arg, RA_LOCKED);
1920 }
1921 
1922 static void
1923 knlist_rw_assert_unlocked(void *arg)
1924 {
1925 
1926 	rw_assert((struct rwlock *)arg, RA_UNLOCKED);
1927 }
1928 
1929 void
1930 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1931     void (*kl_unlock)(void *),
1932     void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1933 {
1934 
1935 	if (lock == NULL)
1936 		knl->kl_lockarg = &knlist_lock;
1937 	else
1938 		knl->kl_lockarg = lock;
1939 
1940 	if (kl_lock == NULL)
1941 		knl->kl_lock = knlist_mtx_lock;
1942 	else
1943 		knl->kl_lock = kl_lock;
1944 	if (kl_unlock == NULL)
1945 		knl->kl_unlock = knlist_mtx_unlock;
1946 	else
1947 		knl->kl_unlock = kl_unlock;
1948 	if (kl_assert_locked == NULL)
1949 		knl->kl_assert_locked = knlist_mtx_assert_locked;
1950 	else
1951 		knl->kl_assert_locked = kl_assert_locked;
1952 	if (kl_assert_unlocked == NULL)
1953 		knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1954 	else
1955 		knl->kl_assert_unlocked = kl_assert_unlocked;
1956 
1957 	SLIST_INIT(&knl->kl_list);
1958 }
1959 
1960 void
1961 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1962 {
1963 
1964 	knlist_init(knl, lock, NULL, NULL, NULL, NULL);
1965 }
1966 
1967 void
1968 knlist_init_rw_reader(struct knlist *knl, struct rwlock *lock)
1969 {
1970 
1971 	knlist_init(knl, lock, knlist_rw_rlock, knlist_rw_runlock,
1972 	    knlist_rw_assert_locked, knlist_rw_assert_unlocked);
1973 }
1974 
1975 void
1976 knlist_destroy(struct knlist *knl)
1977 {
1978 
1979 #ifdef INVARIANTS
1980 	/*
1981 	 * if we run across this error, we need to find the offending
1982 	 * driver and have it call knlist_clear.
1983 	 */
1984 	if (!SLIST_EMPTY(&knl->kl_list))
1985 		printf("WARNING: destroying knlist w/ knotes on it!\n");
1986 #endif
1987 
1988 	knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1989 	SLIST_INIT(&knl->kl_list);
1990 }
1991 
1992 /*
1993  * Even if we are locked, we may need to drop the lock to allow any influx
1994  * knotes time to "settle".
1995  */
1996 void
1997 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1998 {
1999 	struct knote *kn, *kn2;
2000 	struct kqueue *kq;
2001 
2002 	if (islocked)
2003 		KNL_ASSERT_LOCKED(knl);
2004 	else {
2005 		KNL_ASSERT_UNLOCKED(knl);
2006 again:		/* need to reacquire lock since we have dropped it */
2007 		knl->kl_lock(knl->kl_lockarg);
2008 	}
2009 
2010 	SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
2011 		kq = kn->kn_kq;
2012 		KQ_LOCK(kq);
2013 		if ((kn->kn_status & KN_INFLUX)) {
2014 			KQ_UNLOCK(kq);
2015 			continue;
2016 		}
2017 		knlist_remove_kq(knl, kn, 1, 1);
2018 		if (killkn) {
2019 			kn->kn_status |= KN_INFLUX | KN_DETACHED;
2020 			KQ_UNLOCK(kq);
2021 			knote_drop(kn, td);
2022 		} else {
2023 			/* Make sure cleared knotes disappear soon */
2024 			kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2025 			KQ_UNLOCK(kq);
2026 		}
2027 		kq = NULL;
2028 	}
2029 
2030 	if (!SLIST_EMPTY(&knl->kl_list)) {
2031 		/* there are still KN_INFLUX remaining */
2032 		kn = SLIST_FIRST(&knl->kl_list);
2033 		kq = kn->kn_kq;
2034 		KQ_LOCK(kq);
2035 		KASSERT(kn->kn_status & KN_INFLUX,
2036 		    ("knote removed w/o list lock"));
2037 		knl->kl_unlock(knl->kl_lockarg);
2038 		kq->kq_state |= KQ_FLUXWAIT;
2039 		msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2040 		kq = NULL;
2041 		goto again;
2042 	}
2043 
2044 	if (islocked)
2045 		KNL_ASSERT_LOCKED(knl);
2046 	else {
2047 		knl->kl_unlock(knl->kl_lockarg);
2048 		KNL_ASSERT_UNLOCKED(knl);
2049 	}
2050 }
2051 
2052 /*
2053  * Remove all knotes referencing a specified fd must be called with FILEDESC
2054  * lock.  This prevents a race where a new fd comes along and occupies the
2055  * entry and we attach a knote to the fd.
2056  */
2057 void
2058 knote_fdclose(struct thread *td, int fd)
2059 {
2060 	struct filedesc *fdp = td->td_proc->p_fd;
2061 	struct kqueue *kq;
2062 	struct knote *kn;
2063 	int influx;
2064 
2065 	FILEDESC_XLOCK_ASSERT(fdp);
2066 
2067 	/*
2068 	 * We shouldn't have to worry about new kevents appearing on fd
2069 	 * since filedesc is locked.
2070 	 */
2071 	SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2072 		KQ_LOCK(kq);
2073 
2074 again:
2075 		influx = 0;
2076 		while (kq->kq_knlistsize > fd &&
2077 		    (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2078 			if (kn->kn_status & KN_INFLUX) {
2079 				/* someone else might be waiting on our knote */
2080 				if (influx)
2081 					wakeup(kq);
2082 				kq->kq_state |= KQ_FLUXWAIT;
2083 				msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2084 				goto again;
2085 			}
2086 			kn->kn_status |= KN_INFLUX;
2087 			KQ_UNLOCK(kq);
2088 			if (!(kn->kn_status & KN_DETACHED))
2089 				kn->kn_fop->f_detach(kn);
2090 			knote_drop(kn, td);
2091 			influx = 1;
2092 			KQ_LOCK(kq);
2093 		}
2094 		KQ_UNLOCK_FLUX(kq);
2095 	}
2096 }
2097 
2098 static int
2099 knote_attach(struct knote *kn, struct kqueue *kq)
2100 {
2101 	struct klist *list;
2102 
2103 	KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
2104 	KQ_OWNED(kq);
2105 
2106 	if (kn->kn_fop->f_isfd) {
2107 		if (kn->kn_id >= kq->kq_knlistsize)
2108 			return ENOMEM;
2109 		list = &kq->kq_knlist[kn->kn_id];
2110 	} else {
2111 		if (kq->kq_knhash == NULL)
2112 			return ENOMEM;
2113 		list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2114 	}
2115 
2116 	SLIST_INSERT_HEAD(list, kn, kn_link);
2117 
2118 	return 0;
2119 }
2120 
2121 /*
2122  * knote must already have been detached using the f_detach method.
2123  * no lock need to be held, it is assumed that the KN_INFLUX flag is set
2124  * to prevent other removal.
2125  */
2126 static void
2127 knote_drop(struct knote *kn, struct thread *td)
2128 {
2129 	struct kqueue *kq;
2130 	struct klist *list;
2131 
2132 	kq = kn->kn_kq;
2133 
2134 	KQ_NOTOWNED(kq);
2135 	KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
2136 	    ("knote_drop called without KN_INFLUX set in kn_status"));
2137 
2138 	KQ_LOCK(kq);
2139 	if (kn->kn_fop->f_isfd)
2140 		list = &kq->kq_knlist[kn->kn_id];
2141 	else
2142 		list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2143 
2144 	if (!SLIST_EMPTY(list))
2145 		SLIST_REMOVE(list, kn, knote, kn_link);
2146 	if (kn->kn_status & KN_QUEUED)
2147 		knote_dequeue(kn);
2148 	KQ_UNLOCK_FLUX(kq);
2149 
2150 	if (kn->kn_fop->f_isfd) {
2151 		fdrop(kn->kn_fp, td);
2152 		kn->kn_fp = NULL;
2153 	}
2154 	kqueue_fo_release(kn->kn_kevent.filter);
2155 	kn->kn_fop = NULL;
2156 	knote_free(kn);
2157 }
2158 
2159 static void
2160 knote_enqueue(struct knote *kn)
2161 {
2162 	struct kqueue *kq = kn->kn_kq;
2163 
2164 	KQ_OWNED(kn->kn_kq);
2165 	KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2166 
2167 	TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2168 	kn->kn_status |= KN_QUEUED;
2169 	kq->kq_count++;
2170 	kqueue_wakeup(kq);
2171 }
2172 
2173 static void
2174 knote_dequeue(struct knote *kn)
2175 {
2176 	struct kqueue *kq = kn->kn_kq;
2177 
2178 	KQ_OWNED(kn->kn_kq);
2179 	KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2180 
2181 	TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2182 	kn->kn_status &= ~KN_QUEUED;
2183 	kq->kq_count--;
2184 }
2185 
2186 static void
2187 knote_init(void)
2188 {
2189 
2190 	knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2191 	    NULL, NULL, UMA_ALIGN_PTR, 0);
2192 }
2193 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2194 
2195 static struct knote *
2196 knote_alloc(int waitok)
2197 {
2198 	return ((struct knote *)uma_zalloc(knote_zone,
2199 	    (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2200 }
2201 
2202 static void
2203 knote_free(struct knote *kn)
2204 {
2205 	if (kn != NULL)
2206 		uma_zfree(knote_zone, kn);
2207 }
2208 
2209 /*
2210  * Register the kev w/ the kq specified by fd.
2211  */
2212 int
2213 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2214 {
2215 	struct kqueue *kq;
2216 	struct file *fp;
2217 	int error;
2218 
2219 	if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
2220 		return (error);
2221 	if ((error = kqueue_acquire(fp, &kq)) != 0)
2222 		goto noacquire;
2223 
2224 	error = kqueue_register(kq, kev, td, waitok);
2225 
2226 	kqueue_release(kq, 0);
2227 
2228 noacquire:
2229 	fdrop(fp, td);
2230 
2231 	return error;
2232 }
2233