2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/kern/kern_event.c,v 1.2.2.10 2004/04/04 07:03:14 cperciva Exp $
27 * $DragonFly: src/sys/kern/kern_event.c,v 1.33 2007/02/03 17:05:57 corecode Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/unistd.h>
38 #include <sys/fcntl.h>
39 #include <sys/select.h>
40 #include <sys/queue.h>
41 #include <sys/event.h>
42 #include <sys/eventvar.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysproto.h>
51 #include <sys/signalvar.h>
52 #include <sys/filio.h>
54 #include <sys/thread2.h>
55 #include <sys/file2.h>
56 #include <sys/mplock2.h>
58 #include <vm/vm_zone.h>
60 MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
62 static int kqueue_scan(struct kqueue *kq, struct kevent *kevp, int count,
63 struct timespec *tsp, int *errorp);
64 static int kqueue_read(struct file *fp, struct uio *uio,
65 struct ucred *cred, int flags);
66 static int kqueue_write(struct file *fp, struct uio *uio,
67 struct ucred *cred, int flags);
68 static int kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
69 struct ucred *cred, struct sysmsg *msg);
70 static int kqueue_poll(struct file *fp, int events, struct ucred *cred);
71 static int kqueue_kqfilter(struct file *fp, struct knote *kn);
72 static int kqueue_stat(struct file *fp, struct stat *st,
74 static int kqueue_close(struct file *fp);
75 static void kqueue_wakeup(struct kqueue *kq);
80 static struct fileops kqueueops = {
81 .fo_read = kqueue_read,
82 .fo_write = kqueue_write,
83 .fo_ioctl = kqueue_ioctl,
84 .fo_poll = kqueue_poll,
85 .fo_kqfilter = kqueue_kqfilter,
86 .fo_stat = kqueue_stat,
87 .fo_close = kqueue_close,
88 .fo_shutdown = nofo_shutdown
91 static void knote_attach(struct knote *kn);
92 static void knote_drop(struct knote *kn);
93 static void knote_enqueue(struct knote *kn);
94 static void knote_dequeue(struct knote *kn);
95 static void knote_init(void);
96 static struct knote *knote_alloc(void);
97 static void knote_free(struct knote *kn);
99 static void filt_kqdetach(struct knote *kn);
100 static int filt_kqueue(struct knote *kn, long hint);
101 static int filt_procattach(struct knote *kn);
102 static void filt_procdetach(struct knote *kn);
103 static int filt_proc(struct knote *kn, long hint);
104 static int filt_fileattach(struct knote *kn);
105 static void filt_timerexpire(void *knx);
106 static int filt_timerattach(struct knote *kn);
107 static void filt_timerdetach(struct knote *kn);
108 static int filt_timer(struct knote *kn, long hint);
110 static struct filterops file_filtops =
111 { 1, filt_fileattach, NULL, NULL };
112 static struct filterops kqread_filtops =
113 { 1, NULL, filt_kqdetach, filt_kqueue };
114 static struct filterops proc_filtops =
115 { 0, filt_procattach, filt_procdetach, filt_proc };
116 static struct filterops timer_filtops =
117 { 0, filt_timerattach, filt_timerdetach, filt_timer };
119 static vm_zone_t knote_zone;
120 static int kq_ncallouts = 0;
121 static int kq_calloutmax = (4 * 1024);
122 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
123 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
125 #define KNOTE_ACTIVATE(kn) do { \
126 kn->kn_status |= KN_ACTIVE; \
127 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
131 #define KN_HASHSIZE 64 /* XXX should be tunable */
132 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
134 extern struct filterops aio_filtops;
135 extern struct filterops sig_filtops;
138 * Table for for all system-defined filters.
140 static struct filterops *sysfilt_ops[] = {
141 &file_filtops, /* EVFILT_READ */
142 &file_filtops, /* EVFILT_WRITE */
143 &aio_filtops, /* EVFILT_AIO */
144 &file_filtops, /* EVFILT_VNODE */
145 &proc_filtops, /* EVFILT_PROC */
146 &sig_filtops, /* EVFILT_SIGNAL */
147 &timer_filtops, /* EVFILT_TIMER */
151 filt_fileattach(struct knote *kn)
153 return (fo_kqfilter(kn->kn_fp, kn));
157 * MPALMOSTSAFE - acquires mplock
160 kqueue_kqfilter(struct file *fp, struct knote *kn)
162 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
165 if (kn->kn_filter != EVFILT_READ) {
170 kn->kn_fop = &kqread_filtops;
171 SLIST_INSERT_HEAD(&kq->kq_sel.si_note, kn, kn_selnext);
177 filt_kqdetach(struct knote *kn)
179 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
181 SLIST_REMOVE(&kq->kq_sel.si_note, kn, knote, kn_selnext);
186 filt_kqueue(struct knote *kn, long hint)
188 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
190 kn->kn_data = kq->kq_count;
191 return (kn->kn_data > 0);
195 filt_procattach(struct knote *kn)
201 p = pfind(kn->kn_id);
202 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
203 p = zpfind(kn->kn_id);
208 if (!PRISON_CHECK(curthread->td_ucred, p->p_ucred))
211 kn->kn_ptr.p_proc = p;
212 kn->kn_flags |= EV_CLEAR; /* automatically set */
215 * internal flag indicating registration done by kernel
217 if (kn->kn_flags & EV_FLAG1) {
218 kn->kn_data = kn->kn_sdata; /* ppid */
219 kn->kn_fflags = NOTE_CHILD;
220 kn->kn_flags &= ~EV_FLAG1;
223 /* XXX lock the proc here while adding to the list? */
224 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
227 * Immediately activate any exit notes if the target process is a
228 * zombie. This is necessary to handle the case where the target
229 * process, e.g. a child, dies before the kevent is registered.
231 if (immediate && filt_proc(kn, NOTE_EXIT))
238 * The knote may be attached to a different process, which may exit,
239 * leaving nothing for the knote to be attached to. So when the process
240 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
241 * it will be deleted when read out. However, as part of the knote deletion,
242 * this routine is called, so a check is needed to avoid actually performing
243 * a detach, because the original process does not exist any more.
246 filt_procdetach(struct knote *kn)
250 if (kn->kn_status & KN_DETACHED)
252 /* XXX locking? this might modify another process. */
253 p = kn->kn_ptr.p_proc;
254 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
258 filt_proc(struct knote *kn, long hint)
263 * mask off extra data
265 event = (u_int)hint & NOTE_PCTRLMASK;
268 * if the user is interested in this event, record it.
270 if (kn->kn_sfflags & event)
271 kn->kn_fflags |= event;
274 * Process is gone, so flag the event as finished. Detach the
275 * knote from the process now because the process will be poof,
278 if (event == NOTE_EXIT) {
279 struct proc *p = kn->kn_ptr.p_proc;
280 if ((kn->kn_status & KN_DETACHED) == 0) {
281 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
282 kn->kn_status |= KN_DETACHED;
283 kn->kn_data = p->p_xstat;
284 kn->kn_ptr.p_proc = NULL;
286 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
291 * process forked, and user wants to track the new process,
292 * so attach a new knote to it, and immediately report an
293 * event with the parent's pid.
295 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
300 * register knote with new process.
302 kev.ident = hint & NOTE_PDATAMASK; /* pid */
303 kev.filter = kn->kn_filter;
304 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
305 kev.fflags = kn->kn_sfflags;
306 kev.data = kn->kn_id; /* parent */
307 kev.udata = kn->kn_kevent.udata; /* preserve udata */
308 error = kqueue_register(kn->kn_kq, &kev);
310 kn->kn_fflags |= NOTE_TRACKERR;
313 return (kn->kn_fflags != 0);
317 filt_timerexpire(void *knx)
319 struct knote *kn = knx;
320 struct callout *calloutp;
327 if ((kn->kn_flags & EV_ONESHOT) == 0) {
328 tv.tv_sec = kn->kn_sdata / 1000;
329 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
330 tticks = tvtohz_high(&tv);
331 calloutp = (struct callout *)kn->kn_hook;
332 callout_reset(calloutp, tticks, filt_timerexpire, kn);
337 * data contains amount of time to sleep, in milliseconds
340 filt_timerattach(struct knote *kn)
342 struct callout *calloutp;
346 if (kq_ncallouts >= kq_calloutmax)
350 tv.tv_sec = kn->kn_sdata / 1000;
351 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
352 tticks = tvtohz_high(&tv);
354 kn->kn_flags |= EV_CLEAR; /* automatically set */
355 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
357 callout_init(calloutp);
358 kn->kn_hook = (caddr_t)calloutp;
359 callout_reset(calloutp, tticks, filt_timerexpire, kn);
365 filt_timerdetach(struct knote *kn)
367 struct callout *calloutp;
369 calloutp = (struct callout *)kn->kn_hook;
370 callout_stop(calloutp);
371 FREE(calloutp, M_KQUEUE);
376 filt_timer(struct knote *kn, long hint)
379 return (kn->kn_data != 0);
383 * Initialize a kqueue.
385 * NOTE: The lwp/proc code initializes a kqueue for select/poll ops.
390 kqueue_init(struct kqueue *kq, struct filedesc *fdp)
392 TAILQ_INIT(&kq->kq_knpend);
393 TAILQ_INIT(&kq->kq_knlist);
398 * Terminate a kqueue. Freeing the actual kq itself is left up to the
399 * caller (it might be embedded in a lwp so we don't do it here).
402 kqueue_terminate(struct kqueue *kq)
408 while ((kn = TAILQ_FIRST(&kq->kq_knlist)) != NULL) {
409 kn->kn_fop->f_detach(kn);
410 if (kn->kn_fop->f_isfd) {
411 list = &kn->kn_fp->f_klist;
412 SLIST_REMOVE(list, kn, knote, kn_link);
416 hv = KN_HASH(kn->kn_id, kq->kq_knhashmask);
417 list = &kq->kq_knhash[hv];
418 SLIST_REMOVE(list, kn, knote, kn_link);
420 TAILQ_REMOVE(&kq->kq_knlist, kn, kn_kqlink);
421 if (kn->kn_status & KN_QUEUED)
427 kfree(kq->kq_knhash, M_KQUEUE);
428 kq->kq_knhash = NULL;
429 kq->kq_knhashmask = 0;
437 sys_kqueue(struct kqueue_args *uap)
439 struct thread *td = curthread;
444 error = falloc(td->td_lwp, &fp, &fd);
447 fp->f_flag = FREAD | FWRITE;
448 fp->f_type = DTYPE_KQUEUE;
449 fp->f_ops = &kqueueops;
451 kq = kmalloc(sizeof(struct kqueue), M_KQUEUE, M_WAITOK | M_ZERO);
452 kqueue_init(kq, td->td_proc->p_fd);
455 fsetfd(kq->kq_fdp, fp, fd);
456 uap->sysmsg_result = fd;
462 * Copy 'count' items into the destination list pointed to by uap->eventlist.
465 kevent_copyout(void *arg, struct kevent *kevp, int count)
467 struct kevent_args *uap;
470 uap = (struct kevent_args *)arg;
472 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
474 uap->eventlist += count;
479 * Copy 'count' items from the list pointed to by uap->changelist.
482 kevent_copyin(void *arg, struct kevent *kevp, int count)
484 struct kevent_args *uap;
487 uap = (struct kevent_args *)arg;
489 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
491 uap->changelist += count;
499 kern_kevent(int fd, int nchanges, int nevents, struct kevent_args *uap,
500 k_copyin_fn kevent_copyinfn, k_copyout_fn kevent_copyoutfn,
501 struct timespec *tsp_in)
503 struct thread *td = curthread;
504 struct proc *p = td->td_proc;
507 struct file *fp = NULL;
509 struct timespec *tsp;
510 int i, n, total, nerrors, error;
511 struct kevent kev[KQ_NEVENTS];
515 fp = holdfp(p->p_fd, fd, -1);
518 if (fp->f_type != DTYPE_KQUEUE) {
523 kq = (struct kqueue *)fp->f_data;
527 while (nchanges > 0) {
528 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
529 error = kevent_copyinfn(uap, kev, n);
532 for (i = 0; i < n; i++) {
534 kevp->flags &= ~EV_SYSFLAGS;
535 error = kqueue_register(kq, kevp);
538 kevp->flags = EV_ERROR;
540 kevent_copyoutfn(uap, kevp, 1);
551 uap->sysmsg_result = nerrors;
557 * Acquire/wait for events - setup timeout
562 if (tsp->tv_sec || tsp->tv_nsec) {
564 timespecadd(tsp, &ats); /* tsp = target time */
571 * Collect as many events as we can. The timeout on successive
572 * loops is disabled (kqueue_scan() becomes non-blocking).
576 while ((n = nevents - total) > 0) {
579 i = kqueue_scan(kq, kev, n, tsp, &error);
582 error = kevent_copyoutfn(uap, kev, i);
586 tsp = &ts; /* successive loops non-blocking */
590 uap->sysmsg_result = total;
602 sys_kevent(struct kevent_args *uap)
604 struct timespec ts, *tsp;
608 error = copyin(uap->timeout, &ts, sizeof(ts));
616 error = kern_kevent(uap->fd, uap->nchanges, uap->nevents,
617 uap, kevent_copyin, kevent_copyout, tsp);
623 kqueue_register(struct kqueue *kq, struct kevent *kev)
625 struct filedesc *fdp = kq->kq_fdp;
626 struct filterops *fops;
627 struct file *fp = NULL;
628 struct knote *kn = NULL;
631 if (kev->filter < 0) {
632 if (kev->filter + EVFILT_SYSCOUNT < 0)
634 fops = sysfilt_ops[~kev->filter]; /* to 0-base index */
638 * filter attach routine is responsible for insuring that
639 * the identifier can be attached to it.
641 kprintf("unknown filter: %d\n", kev->filter);
646 /* validate descriptor */
647 fp = holdfp(fdp, kev->ident, -1);
651 SLIST_FOREACH(kn, &fp->f_klist, kn_link) {
652 if (kn->kn_kq == kq &&
653 kn->kn_filter == kev->filter &&
654 kn->kn_id == kev->ident) {
659 if (kq->kq_knhashmask) {
662 list = &kq->kq_knhash[
663 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
664 SLIST_FOREACH(kn, list, kn_link) {
665 if (kn->kn_id == kev->ident &&
666 kn->kn_filter == kev->filter)
672 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
678 * kn now contains the matching knote, or NULL if no match
680 if (kev->flags & EV_ADD) {
692 * apply reference count to knote structure, and
693 * do not release it at the end of this routine.
697 kn->kn_sfflags = kev->fflags;
698 kn->kn_sdata = kev->data;
701 kn->kn_kevent = *kev;
704 if ((error = fops->f_attach(kn)) != 0) {
710 * The user may change some filter values after the
711 * initial EV_ADD, but doing so will not reset any
712 * filter which have already been triggered.
714 kn->kn_sfflags = kev->fflags;
715 kn->kn_sdata = kev->data;
716 kn->kn_kevent.udata = kev->udata;
720 if (kn->kn_fop->f_event(kn, 0))
723 } else if (kev->flags & EV_DELETE) {
724 kn->kn_fop->f_detach(kn);
729 if ((kev->flags & EV_DISABLE) &&
730 ((kn->kn_status & KN_DISABLED) == 0)) {
732 kn->kn_status |= KN_DISABLED;
736 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
738 kn->kn_status &= ~KN_DISABLED;
739 if ((kn->kn_status & KN_ACTIVE) &&
740 ((kn->kn_status & KN_QUEUED) == 0))
752 * Scan the kqueue, blocking if necessary until the target time is reached.
753 * If tsp is NULL we block indefinitely. If tsp->ts_secs/nsecs are both
754 * 0 we do not block at all.
757 kqueue_scan(struct kqueue *kq, struct kevent *kevp, int count,
758 struct timespec *tsp, int *errorp)
760 struct knote *kn, marker;
766 if (kq->kq_count == 0) {
768 kq->kq_state |= KQ_SLEEP;
769 *errorp = tsleep(kq, PCATCH, "kqread", 0);
770 } else if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) {
771 *errorp = EWOULDBLOCK;
774 struct timespec atx = *tsp;
778 timespecsub(&atx, &ats);
779 if (ats.tv_sec < 0) {
780 *errorp = EWOULDBLOCK;
782 timeout = atx.tv_sec > 24 * 60 * 60 ?
783 24 * 60 * 60 * hz : tstohz_high(&atx);
784 kq->kq_state |= KQ_SLEEP;
785 *errorp = tsleep(kq, PCATCH, "kqread", timeout);
791 /* don't restart after signals... */
792 if (*errorp == ERESTART)
794 else if (*errorp == EWOULDBLOCK)
800 * Collect events. Continuous mode events may get recycled
801 * past the marker so we stop when we hit it unless no events
802 * have been collected.
804 TAILQ_INSERT_TAIL(&kq->kq_knpend, &marker, kn_tqe);
806 kn = TAILQ_FIRST(&kq->kq_knpend);
809 TAILQ_REMOVE(&kq->kq_knpend, kn, kn_tqe);
810 if (kn->kn_status & KN_DISABLED) {
811 kn->kn_status &= ~KN_QUEUED;
815 if ((kn->kn_flags & EV_ONESHOT) == 0 &&
816 kn->kn_fop->f_event(kn, 0) == 0) {
817 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
821 *kevp++ = kn->kn_kevent;
826 * Post-event action on the note
828 if (kn->kn_flags & EV_ONESHOT) {
829 kn->kn_status &= ~KN_QUEUED;
832 kn->kn_fop->f_detach(kn);
835 } else if (kn->kn_flags & EV_CLEAR) {
838 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
841 TAILQ_INSERT_TAIL(&kq->kq_knpend, kn, kn_tqe);
844 TAILQ_REMOVE(&kq->kq_knpend, &marker, kn_tqe);
854 * This could be expanded to call kqueue_scan, if desired.
859 kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
868 kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
877 kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
878 struct ucred *cred, struct sysmsg *msg)
884 kq = (struct kqueue *)fp->f_data;
889 kq->kq_state |= KQ_ASYNC;
891 kq->kq_state &= ~KQ_ASYNC;
895 error = fsetown(*(int *)data, &kq->kq_sigio);
906 * MPALMOSTSAFE - acquires mplock
909 kqueue_poll(struct file *fp, int events, struct ucred *cred)
911 struct kqueue *kq = (struct kqueue *)fp->f_data;
916 if (events & (POLLIN | POLLRDNORM)) {
918 revents |= events & (POLLIN | POLLRDNORM);
920 selrecord(curthread, &kq->kq_sel);
921 kq->kq_state |= KQ_SEL;
933 kqueue_stat(struct file *fp, struct stat *st, struct ucred *cred)
935 struct kqueue *kq = (struct kqueue *)fp->f_data;
937 bzero((void *)st, sizeof(*st));
938 st->st_size = kq->kq_count;
939 st->st_blksize = sizeof(struct kevent);
940 st->st_mode = S_IFIFO;
945 * MPALMOSTSAFE - acquires mplock
948 kqueue_close(struct file *fp)
950 struct kqueue *kq = (struct kqueue *)fp->f_data;
954 kqueue_terminate(kq);
957 funsetown(kq->kq_sigio);
965 kqueue_wakeup(struct kqueue *kq)
967 if (kq->kq_state & KQ_SLEEP) {
968 kq->kq_state &= ~KQ_SLEEP;
971 if (kq->kq_state & KQ_SEL) {
972 kq->kq_state &= ~KQ_SEL;
973 selwakeup(&kq->kq_sel);
975 KNOTE(&kq->kq_sel.si_note, 0);
979 * walk down a list of knotes, activating them if their event has triggered.
982 knote(struct klist *list, long hint)
986 SLIST_FOREACH(kn, list, kn_selnext)
987 if (kn->kn_fop->f_event(kn, hint))
992 * remove all knotes from a specified klist
995 knote_remove(struct klist *list)
999 while ((kn = SLIST_FIRST(list)) != NULL) {
1000 kn->kn_fop->f_detach(kn);
1006 * remove all knotes referencing a specified fd
1009 knote_fdclose(struct file *fp, struct filedesc *fdp, int fd)
1014 SLIST_FOREACH(kn, &fp->f_klist, kn_link) {
1015 if (kn->kn_kq->kq_fdp == fdp && kn->kn_id == fd) {
1016 kn->kn_fop->f_detach(kn);
1024 knote_attach(struct knote *kn)
1027 struct kqueue *kq = kn->kn_kq;
1029 if (kn->kn_fop->f_isfd) {
1030 KKASSERT(kn->kn_fp);
1031 list = &kn->kn_fp->f_klist;
1033 if (kq->kq_knhashmask == 0)
1034 kq->kq_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1035 &kq->kq_knhashmask);
1036 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1038 SLIST_INSERT_HEAD(list, kn, kn_link);
1039 TAILQ_INSERT_HEAD(&kq->kq_knlist, kn, kn_kqlink);
1044 * should be called outside of a critical section, since we don't want to
1045 * hold a critical section while calling fdrop and free.
1048 knote_drop(struct knote *kn)
1055 if (kn->kn_fop->f_isfd)
1056 list = &kn->kn_fp->f_klist;
1058 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1060 SLIST_REMOVE(list, kn, knote, kn_link);
1061 TAILQ_REMOVE(&kq->kq_knlist, kn, kn_kqlink);
1062 if (kn->kn_status & KN_QUEUED)
1064 if (kn->kn_fop->f_isfd)
1071 knote_enqueue(struct knote *kn)
1073 struct kqueue *kq = kn->kn_kq;
1076 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1078 TAILQ_INSERT_TAIL(&kq->kq_knpend, kn, kn_tqe);
1079 kn->kn_status |= KN_QUEUED;
1083 * Send SIGIO on request (typically set up as a mailbox signal)
1085 if (kq->kq_sigio && (kq->kq_state & KQ_ASYNC) && kq->kq_count == 1)
1086 pgsigio(kq->kq_sigio, SIGIO, 0);
1092 knote_dequeue(struct knote *kn)
1094 struct kqueue *kq = kn->kn_kq;
1096 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1099 TAILQ_REMOVE(&kq->kq_knpend, kn, kn_tqe);
1100 kn->kn_status &= ~KN_QUEUED;
1108 knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
1110 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
1112 static struct knote *
1115 return ((struct knote *)zalloc(knote_zone));
1119 knote_free(struct knote *kn)
1121 zfree(knote_zone, kn);