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.9 2003/05/08 07:47:16 kbyanc Exp $
27 * $DragonFly: src/sys/kern/kern_event.c,v 1.7 2003/07/26 18:12:44 dillon 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>
37 #include <sys/fcntl.h>
38 #include <sys/select.h>
39 #include <sys/queue.h>
40 #include <sys/event.h>
41 #include <sys/eventvar.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysproto.h>
50 #include <sys/file2.h>
52 #include <vm/vm_zone.h>
54 MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
56 static int kqueue_scan(struct file *fp, int maxevents,
57 struct kevent *ulistp, const struct timespec *timeout,
58 struct proc *p, int *res);
59 static int kqueue_read(struct file *fp, struct uio *uio,
60 struct ucred *cred, int flags, struct thread *td);
61 static int kqueue_write(struct file *fp, struct uio *uio,
62 struct ucred *cred, int flags, struct thread *td);
63 static int kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
65 static int kqueue_poll(struct file *fp, int events, struct ucred *cred,
67 static int kqueue_kqfilter(struct file *fp, struct knote *kn);
68 static int kqueue_stat(struct file *fp, struct stat *st, struct thread *td);
69 static int kqueue_close(struct file *fp, struct thread *td);
70 static void kqueue_wakeup(struct kqueue *kq);
72 static struct fileops kqueueops = {
82 static void knote_attach(struct knote *kn, struct filedesc *fdp);
83 static void knote_drop(struct knote *kn, struct thread *td);
84 static void knote_enqueue(struct knote *kn);
85 static void knote_dequeue(struct knote *kn);
86 static void knote_init(void);
87 static struct knote *knote_alloc(void);
88 static void knote_free(struct knote *kn);
90 static void filt_kqdetach(struct knote *kn);
91 static int filt_kqueue(struct knote *kn, long hint);
92 static int filt_procattach(struct knote *kn);
93 static void filt_procdetach(struct knote *kn);
94 static int filt_proc(struct knote *kn, long hint);
95 static int filt_fileattach(struct knote *kn);
96 static void filt_timerexpire(void *knx);
97 static int filt_timerattach(struct knote *kn);
98 static void filt_timerdetach(struct knote *kn);
99 static int filt_timer(struct knote *kn, long hint);
101 static struct filterops file_filtops =
102 { 1, filt_fileattach, NULL, NULL };
103 static struct filterops kqread_filtops =
104 { 1, NULL, filt_kqdetach, filt_kqueue };
105 static struct filterops proc_filtops =
106 { 0, filt_procattach, filt_procdetach, filt_proc };
107 static struct filterops timer_filtops =
108 { 0, filt_timerattach, filt_timerdetach, filt_timer };
110 static vm_zone_t knote_zone;
111 static int kq_ncallouts = 0;
112 static int kq_calloutmax = (4 * 1024);
113 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
114 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
116 #define KNOTE_ACTIVATE(kn) do { \
117 kn->kn_status |= KN_ACTIVE; \
118 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
122 #define KN_HASHSIZE 64 /* XXX should be tunable */
123 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
125 extern struct filterops aio_filtops;
126 extern struct filterops sig_filtops;
129 * Table for for all system-defined filters.
131 static struct filterops *sysfilt_ops[] = {
132 &file_filtops, /* EVFILT_READ */
133 &file_filtops, /* EVFILT_WRITE */
134 &aio_filtops, /* EVFILT_AIO */
135 &file_filtops, /* EVFILT_VNODE */
136 &proc_filtops, /* EVFILT_PROC */
137 &sig_filtops, /* EVFILT_SIGNAL */
138 &timer_filtops, /* EVFILT_TIMER */
142 filt_fileattach(struct knote *kn)
144 return (fo_kqfilter(kn->kn_fp, kn));
149 kqueue_kqfilter(struct file *fp, struct knote *kn)
151 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
153 if (kn->kn_filter != EVFILT_READ)
156 kn->kn_fop = &kqread_filtops;
157 SLIST_INSERT_HEAD(&kq->kq_sel.si_note, kn, kn_selnext);
162 filt_kqdetach(struct knote *kn)
164 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
166 SLIST_REMOVE(&kq->kq_sel.si_note, kn, knote, kn_selnext);
171 filt_kqueue(struct knote *kn, long hint)
173 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
175 kn->kn_data = kq->kq_count;
176 return (kn->kn_data > 0);
180 filt_procattach(struct knote *kn)
186 p = pfind(kn->kn_id);
187 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
188 p = zpfind(kn->kn_id);
193 if (! PRISON_CHECK(curproc->p_ucred, p->p_ucred))
196 kn->kn_ptr.p_proc = p;
197 kn->kn_flags |= EV_CLEAR; /* automatically set */
200 * internal flag indicating registration done by kernel
202 if (kn->kn_flags & EV_FLAG1) {
203 kn->kn_data = kn->kn_sdata; /* ppid */
204 kn->kn_fflags = NOTE_CHILD;
205 kn->kn_flags &= ~EV_FLAG1;
208 /* XXX lock the proc here while adding to the list? */
209 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
212 * Immediately activate any exit notes if the target process is a
213 * zombie. This is necessary to handle the case where the target
214 * process, e.g. a child, dies before the kevent is registered.
216 if (immediate && filt_proc(kn, NOTE_EXIT))
223 * The knote may be attached to a different process, which may exit,
224 * leaving nothing for the knote to be attached to. So when the process
225 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
226 * it will be deleted when read out. However, as part of the knote deletion,
227 * this routine is called, so a check is needed to avoid actually performing
228 * a detach, because the original process does not exist any more.
231 filt_procdetach(struct knote *kn)
233 struct proc *p = kn->kn_ptr.p_proc;
235 if (kn->kn_status & KN_DETACHED)
238 /* XXX locking? this might modify another process. */
239 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
243 filt_proc(struct knote *kn, long hint)
248 * mask off extra data
250 event = (u_int)hint & NOTE_PCTRLMASK;
253 * if the user is interested in this event, record it.
255 if (kn->kn_sfflags & event)
256 kn->kn_fflags |= event;
259 * process is gone, so flag the event as finished.
261 if (event == NOTE_EXIT) {
262 kn->kn_status |= KN_DETACHED;
263 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
268 * process forked, and user wants to track the new process,
269 * so attach a new knote to it, and immediately report an
270 * event with the parent's pid.
272 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
277 * register knote with new process.
279 kev.ident = hint & NOTE_PDATAMASK; /* pid */
280 kev.filter = kn->kn_filter;
281 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
282 kev.fflags = kn->kn_sfflags;
283 kev.data = kn->kn_id; /* parent */
284 kev.udata = kn->kn_kevent.udata; /* preserve udata */
285 error = kqueue_register(kn->kn_kq, &kev, NULL);
287 kn->kn_fflags |= NOTE_TRACKERR;
290 return (kn->kn_fflags != 0);
294 filt_timerexpire(void *knx)
296 struct knote *kn = knx;
297 struct callout *calloutp;
304 if ((kn->kn_flags & EV_ONESHOT) == 0) {
305 tv.tv_sec = kn->kn_sdata / 1000;
306 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
307 tticks = tvtohz(&tv);
308 calloutp = (struct callout *)kn->kn_hook;
309 callout_reset(calloutp, tticks, filt_timerexpire, kn);
314 * data contains amount of time to sleep, in milliseconds
317 filt_timerattach(struct knote *kn)
319 struct callout *calloutp;
323 if (kq_ncallouts >= kq_calloutmax)
327 tv.tv_sec = kn->kn_sdata / 1000;
328 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
329 tticks = tvtohz(&tv);
331 kn->kn_flags |= EV_CLEAR; /* automatically set */
332 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
334 callout_init(calloutp);
335 callout_reset(calloutp, tticks, filt_timerexpire, kn);
336 kn->kn_hook = (caddr_t)calloutp;
342 filt_timerdetach(struct knote *kn)
344 struct callout *calloutp;
346 calloutp = (struct callout *)kn->kn_hook;
347 callout_stop(calloutp);
348 FREE(calloutp, M_KQUEUE);
353 filt_timer(struct knote *kn, long hint)
356 return (kn->kn_data != 0);
360 kqueue(struct kqueue_args *uap)
362 struct proc *p = curproc;
363 struct filedesc *fdp = p->p_fd;
368 error = falloc(p, &fp, &fd);
371 fp->f_flag = FREAD | FWRITE;
372 fp->f_type = DTYPE_KQUEUE;
373 fp->f_ops = &kqueueops;
374 kq = malloc(sizeof(struct kqueue), M_KQUEUE, M_WAITOK | M_ZERO);
375 TAILQ_INIT(&kq->kq_head);
376 fp->f_data = (caddr_t)kq;
377 uap->lmsg.u.ms_result = fd;
378 if (fdp->fd_knlistsize < 0)
379 fdp->fd_knlistsize = 0; /* this process has a kq */
385 kevent(struct kevent_args *uap)
387 struct thread *td = curthread;
388 struct proc *p = td->td_proc;
389 struct filedesc *fdp;
392 struct file *fp = NULL;
394 int i, n, nerrors, error;
399 if (((u_int)uap->fd) >= fdp->fd_nfiles ||
400 (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
401 (fp->f_type != DTYPE_KQUEUE))
406 if (uap->timeout != NULL) {
407 error = copyin(uap->timeout, &ts, sizeof(ts));
413 kq = (struct kqueue *)fp->f_data;
416 while (uap->nchanges > 0) {
417 n = uap->nchanges > KQ_NEVENTS ? KQ_NEVENTS : uap->nchanges;
418 error = copyin(uap->changelist, kq->kq_kev,
419 n * sizeof(struct kevent));
422 for (i = 0; i < n; i++) {
423 kevp = &kq->kq_kev[i];
424 kevp->flags &= ~EV_SYSFLAGS;
425 error = kqueue_register(kq, kevp, td);
427 if (uap->nevents != 0) {
428 kevp->flags = EV_ERROR;
430 (void) copyout((caddr_t)kevp,
431 (caddr_t)uap->eventlist,
442 uap->changelist += n;
445 uap->lmsg.u.ms_result = nerrors;
450 error = kqueue_scan(fp, uap->nevents, uap->eventlist, uap->timeout, p, &uap->lmsg.u.ms_result);
453 fdrop(fp, p->p_thread);
458 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td)
460 struct filedesc *fdp = kq->kq_fdp;
461 struct filterops *fops;
462 struct file *fp = NULL;
463 struct knote *kn = NULL;
466 if (kev->filter < 0) {
467 if (kev->filter + EVFILT_SYSCOUNT < 0)
469 fops = sysfilt_ops[~kev->filter]; /* to 0-base index */
473 * filter attach routine is responsible for insuring that
474 * the identifier can be attached to it.
476 printf("unknown filter: %d\n", kev->filter);
481 /* validate descriptor */
482 if ((u_int)kev->ident >= fdp->fd_nfiles ||
483 (fp = fdp->fd_ofiles[kev->ident]) == NULL)
487 if (kev->ident < fdp->fd_knlistsize) {
488 SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link)
489 if (kq == kn->kn_kq &&
490 kev->filter == kn->kn_filter)
494 if (fdp->fd_knhashmask != 0) {
497 list = &fdp->fd_knhash[
498 KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
499 SLIST_FOREACH(kn, list, kn_link)
500 if (kev->ident == kn->kn_id &&
502 kev->filter == kn->kn_filter)
507 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
513 * kn now contains the matching knote, or NULL if no match
515 if (kev->flags & EV_ADD) {
528 * apply reference count to knote structure, and
529 * do not release it at the end of this routine.
533 kn->kn_sfflags = kev->fflags;
534 kn->kn_sdata = kev->data;
537 kn->kn_kevent = *kev;
539 knote_attach(kn, fdp);
540 if ((error = fops->f_attach(kn)) != 0) {
546 * The user may change some filter values after the
547 * initial EV_ADD, but doing so will not reset any
548 * filter which have already been triggered.
550 kn->kn_sfflags = kev->fflags;
551 kn->kn_sdata = kev->data;
552 kn->kn_kevent.udata = kev->udata;
556 if (kn->kn_fop->f_event(kn, 0))
560 } else if (kev->flags & EV_DELETE) {
561 kn->kn_fop->f_detach(kn);
566 if ((kev->flags & EV_DISABLE) &&
567 ((kn->kn_status & KN_DISABLED) == 0)) {
569 kn->kn_status |= KN_DISABLED;
573 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
575 kn->kn_status &= ~KN_DISABLED;
576 if ((kn->kn_status & KN_ACTIVE) &&
577 ((kn->kn_status & KN_QUEUED) == 0))
589 kqueue_scan(struct file *fp, int maxevents, struct kevent *ulistp,
590 const struct timespec *tsp, struct proc *p, int *res)
592 struct thread *td = p->p_thread;
593 struct kqueue *kq = (struct kqueue *)fp->f_data;
595 struct timeval atv, rtv, ttv;
596 struct knote *kn, marker;
597 int s, count, timeout, nkev = 0, error = 0;
604 TIMESPEC_TO_TIMEVAL(&atv, tsp);
605 if (itimerfix(&atv)) {
609 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
612 timeout = atv.tv_sec > 24 * 60 * 60 ?
613 24 * 60 * 60 * hz : tvtohz(&atv);
614 getmicrouptime(&rtv);
615 timevaladd(&atv, &rtv);
624 if (atv.tv_sec || atv.tv_usec) {
625 getmicrouptime(&rtv);
626 if (timevalcmp(&rtv, &atv, >=))
629 timevalsub(&ttv, &rtv);
630 timeout = ttv.tv_sec > 24 * 60 * 60 ?
631 24 * 60 * 60 * hz : tvtohz(&ttv);
637 if (kq->kq_count == 0) {
641 kq->kq_state |= KQ_SLEEP;
642 error = tsleep(kq, PCATCH, "kqread", timeout);
647 /* don't restart after signals... */
648 if (error == ERESTART)
650 else if (error == EWOULDBLOCK)
655 TAILQ_INSERT_TAIL(&kq->kq_head, &marker, kn_tqe);
657 kn = TAILQ_FIRST(&kq->kq_head);
658 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
661 if (count == maxevents)
665 if (kn->kn_status & KN_DISABLED) {
666 kn->kn_status &= ~KN_QUEUED;
670 if ((kn->kn_flags & EV_ONESHOT) == 0 &&
671 kn->kn_fop->f_event(kn, 0) == 0) {
672 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
676 *kevp = kn->kn_kevent;
679 if (kn->kn_flags & EV_ONESHOT) {
680 kn->kn_status &= ~KN_QUEUED;
683 kn->kn_fop->f_detach(kn);
686 } else if (kn->kn_flags & EV_CLEAR) {
689 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
692 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
695 if (nkev == KQ_NEVENTS) {
697 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
698 sizeof(struct kevent) * nkev);
707 TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe);
711 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
712 sizeof(struct kevent) * nkev);
713 *res = maxevents - count;
719 * This could be expanded to call kqueue_scan, if desired.
723 kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred,
724 int flags, struct thread *td)
731 kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred,
732 int flags, struct thread *td)
739 kqueue_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
746 kqueue_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
748 struct kqueue *kq = (struct kqueue *)fp->f_data;
752 if (events & (POLLIN | POLLRDNORM)) {
754 revents |= events & (POLLIN | POLLRDNORM);
756 selrecord(td, &kq->kq_sel);
757 kq->kq_state |= KQ_SEL;
766 kqueue_stat(struct file *fp, struct stat *st, struct thread *td)
768 struct kqueue *kq = (struct kqueue *)fp->f_data;
770 bzero((void *)st, sizeof(*st));
771 st->st_size = kq->kq_count;
772 st->st_blksize = sizeof(struct kevent);
773 st->st_mode = S_IFIFO;
779 kqueue_close(struct file *fp, struct thread *td)
781 struct proc *p = td->td_proc;
782 struct kqueue *kq = (struct kqueue *)fp->f_data;
783 struct filedesc *fdp;
784 struct knote **knp, *kn, *kn0;
789 for (i = 0; i < fdp->fd_knlistsize; i++) {
790 knp = &SLIST_FIRST(&fdp->fd_knlist[i]);
793 kn0 = SLIST_NEXT(kn, kn_link);
794 if (kq == kn->kn_kq) {
795 kn->kn_fop->f_detach(kn);
796 fdrop(kn->kn_fp, td);
800 knp = &SLIST_NEXT(kn, kn_link);
805 if (fdp->fd_knhashmask != 0) {
806 for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
807 knp = &SLIST_FIRST(&fdp->fd_knhash[i]);
810 kn0 = SLIST_NEXT(kn, kn_link);
811 if (kq == kn->kn_kq) {
812 kn->kn_fop->f_detach(kn);
813 /* XXX non-fd release of kn->kn_ptr */
817 knp = &SLIST_NEXT(kn, kn_link);
830 kqueue_wakeup(struct kqueue *kq)
833 if (kq->kq_state & KQ_SLEEP) {
834 kq->kq_state &= ~KQ_SLEEP;
837 if (kq->kq_state & KQ_SEL) {
838 kq->kq_state &= ~KQ_SEL;
839 selwakeup(&kq->kq_sel);
841 KNOTE(&kq->kq_sel.si_note, 0);
845 * walk down a list of knotes, activating them if their event has triggered.
848 knote(struct klist *list, long hint)
852 SLIST_FOREACH(kn, list, kn_selnext)
853 if (kn->kn_fop->f_event(kn, hint))
858 * remove all knotes from a specified klist
861 knote_remove(struct thread *td, struct klist *list)
865 while ((kn = SLIST_FIRST(list)) != NULL) {
866 kn->kn_fop->f_detach(kn);
872 * remove all knotes referencing a specified fd
875 knote_fdclose(struct proc *p, int fd)
877 struct filedesc *fdp = p->p_fd;
878 struct klist *list = &fdp->fd_knlist[fd];
880 knote_remove(p->p_thread, list);
884 knote_attach(struct knote *kn, struct filedesc *fdp)
889 if (! kn->kn_fop->f_isfd) {
890 if (fdp->fd_knhashmask == 0)
891 fdp->fd_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
892 &fdp->fd_knhashmask);
893 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
897 if (fdp->fd_knlistsize <= kn->kn_id) {
898 size = fdp->fd_knlistsize;
899 while (size <= kn->kn_id)
901 MALLOC(list, struct klist *,
902 size * sizeof(struct klist *), M_KQUEUE, M_WAITOK);
903 bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list,
904 fdp->fd_knlistsize * sizeof(struct klist *));
905 bzero((caddr_t)list +
906 fdp->fd_knlistsize * sizeof(struct klist *),
907 (size - fdp->fd_knlistsize) * sizeof(struct klist *));
908 if (fdp->fd_knlist != NULL)
909 FREE(fdp->fd_knlist, M_KQUEUE);
910 fdp->fd_knlistsize = size;
911 fdp->fd_knlist = list;
913 list = &fdp->fd_knlist[kn->kn_id];
915 SLIST_INSERT_HEAD(list, kn, kn_link);
920 * should be called at spl == 0, since we don't want to hold spl
921 * while calling fdrop and free.
924 knote_drop(struct knote *kn, struct thread *td)
926 struct filedesc *fdp;
929 KKASSERT(td->td_proc);
930 fdp = td->td_proc->p_fd;
931 if (kn->kn_fop->f_isfd)
932 list = &fdp->fd_knlist[kn->kn_id];
934 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
936 SLIST_REMOVE(list, kn, knote, kn_link);
937 if (kn->kn_status & KN_QUEUED)
939 if (kn->kn_fop->f_isfd)
940 fdrop(kn->kn_fp, td);
946 knote_enqueue(struct knote *kn)
948 struct kqueue *kq = kn->kn_kq;
951 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
953 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
954 kn->kn_status |= KN_QUEUED;
961 knote_dequeue(struct knote *kn)
963 struct kqueue *kq = kn->kn_kq;
966 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
968 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
969 kn->kn_status &= ~KN_QUEUED;
977 knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
979 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
981 static struct knote *
984 return ((struct knote *)zalloc(knote_zone));
988 knote_free(struct knote *kn)
990 zfree(knote_zone, kn);