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.16 2005/07/13 01:38:50 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/thread2.h>
51 #include <sys/file2.h>
53 #include <vm/vm_zone.h>
55 MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
57 static int kqueue_scan(struct file *fp, int maxevents,
58 struct kevent *ulistp, const struct timespec *timeout,
59 struct proc *p, int *res);
60 static int kqueue_read(struct file *fp, struct uio *uio,
61 struct ucred *cred, int flags, struct thread *td);
62 static int kqueue_write(struct file *fp, struct uio *uio,
63 struct ucred *cred, int flags, struct thread *td);
64 static int kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
66 static int kqueue_poll(struct file *fp, int events, struct ucred *cred,
68 static int kqueue_kqfilter(struct file *fp, struct knote *kn);
69 static int kqueue_stat(struct file *fp, struct stat *st, struct thread *td);
70 static int kqueue_close(struct file *fp, struct thread *td);
71 static void kqueue_wakeup(struct kqueue *kq);
73 static struct fileops kqueueops = {
86 static void knote_attach(struct knote *kn, struct filedesc *fdp);
87 static void knote_drop(struct knote *kn, struct thread *td);
88 static void knote_enqueue(struct knote *kn);
89 static void knote_dequeue(struct knote *kn);
90 static void knote_init(void);
91 static struct knote *knote_alloc(void);
92 static void knote_free(struct knote *kn);
94 static void filt_kqdetach(struct knote *kn);
95 static int filt_kqueue(struct knote *kn, long hint);
96 static int filt_procattach(struct knote *kn);
97 static void filt_procdetach(struct knote *kn);
98 static int filt_proc(struct knote *kn, long hint);
99 static int filt_fileattach(struct knote *kn);
100 static void filt_timerexpire(void *knx);
101 static int filt_timerattach(struct knote *kn);
102 static void filt_timerdetach(struct knote *kn);
103 static int filt_timer(struct knote *kn, long hint);
105 static struct filterops file_filtops =
106 { 1, filt_fileattach, NULL, NULL };
107 static struct filterops kqread_filtops =
108 { 1, NULL, filt_kqdetach, filt_kqueue };
109 static struct filterops proc_filtops =
110 { 0, filt_procattach, filt_procdetach, filt_proc };
111 static struct filterops timer_filtops =
112 { 0, filt_timerattach, filt_timerdetach, filt_timer };
114 static vm_zone_t knote_zone;
115 static int kq_ncallouts = 0;
116 static int kq_calloutmax = (4 * 1024);
117 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
118 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
120 #define KNOTE_ACTIVATE(kn) do { \
121 kn->kn_status |= KN_ACTIVE; \
122 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
126 #define KN_HASHSIZE 64 /* XXX should be tunable */
127 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
129 extern struct filterops aio_filtops;
130 extern struct filterops sig_filtops;
133 * Table for for all system-defined filters.
135 static struct filterops *sysfilt_ops[] = {
136 &file_filtops, /* EVFILT_READ */
137 &file_filtops, /* EVFILT_WRITE */
138 &aio_filtops, /* EVFILT_AIO */
139 &file_filtops, /* EVFILT_VNODE */
140 &proc_filtops, /* EVFILT_PROC */
141 &sig_filtops, /* EVFILT_SIGNAL */
142 &timer_filtops, /* EVFILT_TIMER */
146 filt_fileattach(struct knote *kn)
148 return (fo_kqfilter(kn->kn_fp, kn));
153 kqueue_kqfilter(struct file *fp, struct knote *kn)
155 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
157 if (kn->kn_filter != EVFILT_READ)
160 kn->kn_fop = &kqread_filtops;
161 SLIST_INSERT_HEAD(&kq->kq_sel.si_note, kn, kn_selnext);
166 filt_kqdetach(struct knote *kn)
168 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
170 SLIST_REMOVE(&kq->kq_sel.si_note, kn, knote, kn_selnext);
175 filt_kqueue(struct knote *kn, long hint)
177 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
179 kn->kn_data = kq->kq_count;
180 return (kn->kn_data > 0);
184 filt_procattach(struct knote *kn)
190 p = pfind(kn->kn_id);
191 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
192 p = zpfind(kn->kn_id);
197 if (! PRISON_CHECK(curproc->p_ucred, p->p_ucred))
200 kn->kn_ptr.p_proc = p;
201 kn->kn_flags |= EV_CLEAR; /* automatically set */
204 * internal flag indicating registration done by kernel
206 if (kn->kn_flags & EV_FLAG1) {
207 kn->kn_data = kn->kn_sdata; /* ppid */
208 kn->kn_fflags = NOTE_CHILD;
209 kn->kn_flags &= ~EV_FLAG1;
212 /* XXX lock the proc here while adding to the list? */
213 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
216 * Immediately activate any exit notes if the target process is a
217 * zombie. This is necessary to handle the case where the target
218 * process, e.g. a child, dies before the kevent is registered.
220 if (immediate && filt_proc(kn, NOTE_EXIT))
227 * The knote may be attached to a different process, which may exit,
228 * leaving nothing for the knote to be attached to. So when the process
229 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
230 * it will be deleted when read out. However, as part of the knote deletion,
231 * this routine is called, so a check is needed to avoid actually performing
232 * a detach, because the original process does not exist any more.
235 filt_procdetach(struct knote *kn)
237 struct proc *p = kn->kn_ptr.p_proc;
239 if (kn->kn_status & KN_DETACHED)
242 /* XXX locking? this might modify another process. */
243 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
247 filt_proc(struct knote *kn, long hint)
252 * mask off extra data
254 event = (u_int)hint & NOTE_PCTRLMASK;
257 * if the user is interested in this event, record it.
259 if (kn->kn_sfflags & event)
260 kn->kn_fflags |= event;
263 * process is gone, so flag the event as finished.
265 if (event == NOTE_EXIT) {
266 kn->kn_status |= KN_DETACHED;
267 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
272 * process forked, and user wants to track the new process,
273 * so attach a new knote to it, and immediately report an
274 * event with the parent's pid.
276 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
281 * register knote with new process.
283 kev.ident = hint & NOTE_PDATAMASK; /* pid */
284 kev.filter = kn->kn_filter;
285 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
286 kev.fflags = kn->kn_sfflags;
287 kev.data = kn->kn_id; /* parent */
288 kev.udata = kn->kn_kevent.udata; /* preserve udata */
289 error = kqueue_register(kn->kn_kq, &kev, NULL);
291 kn->kn_fflags |= NOTE_TRACKERR;
294 return (kn->kn_fflags != 0);
298 filt_timerexpire(void *knx)
300 struct knote *kn = knx;
301 struct callout *calloutp;
308 if ((kn->kn_flags & EV_ONESHOT) == 0) {
309 tv.tv_sec = kn->kn_sdata / 1000;
310 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
311 tticks = tvtohz_high(&tv);
312 calloutp = (struct callout *)kn->kn_hook;
313 callout_reset(calloutp, tticks, filt_timerexpire, kn);
318 * data contains amount of time to sleep, in milliseconds
321 filt_timerattach(struct knote *kn)
323 struct callout *calloutp;
327 if (kq_ncallouts >= kq_calloutmax)
331 tv.tv_sec = kn->kn_sdata / 1000;
332 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
333 tticks = tvtohz_high(&tv);
335 kn->kn_flags |= EV_CLEAR; /* automatically set */
336 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
338 callout_init(calloutp);
339 kn->kn_hook = (caddr_t)calloutp;
340 callout_reset(calloutp, tticks, filt_timerexpire, kn);
346 filt_timerdetach(struct knote *kn)
348 struct callout *calloutp;
350 calloutp = (struct callout *)kn->kn_hook;
351 callout_stop(calloutp);
352 FREE(calloutp, M_KQUEUE);
357 filt_timer(struct knote *kn, long hint)
360 return (kn->kn_data != 0);
364 kqueue(struct kqueue_args *uap)
366 struct proc *p = curproc;
367 struct filedesc *fdp = p->p_fd;
372 error = falloc(p, &fp, &fd);
375 fp->f_flag = FREAD | FWRITE;
376 fp->f_type = DTYPE_KQUEUE;
377 fp->f_ops = &kqueueops;
378 kq = malloc(sizeof(struct kqueue), M_KQUEUE, M_WAITOK | M_ZERO);
379 TAILQ_INIT(&kq->kq_head);
380 fp->f_data = (caddr_t)kq;
381 uap->sysmsg_result = fd;
382 fdrop(fp, curthread);
383 if (fdp->fd_knlistsize < 0)
384 fdp->fd_knlistsize = 0; /* this process has a kq */
390 kevent(struct kevent_args *uap)
392 struct thread *td = curthread;
393 struct proc *p = td->td_proc;
394 struct filedesc *fdp;
397 struct file *fp = NULL;
399 int i, n, nerrors, error;
404 if (((u_int)uap->fd) >= fdp->fd_nfiles ||
405 (fp = fdp->fd_files[uap->fd].fp) == NULL ||
406 (fp->f_type != DTYPE_KQUEUE))
411 if (uap->timeout != NULL) {
412 error = copyin(uap->timeout, &ts, sizeof(ts));
418 kq = (struct kqueue *)fp->f_data;
421 while (uap->nchanges > 0) {
422 n = uap->nchanges > KQ_NEVENTS ? KQ_NEVENTS : uap->nchanges;
423 error = copyin(uap->changelist, kq->kq_kev,
424 n * sizeof(struct kevent));
427 for (i = 0; i < n; i++) {
428 kevp = &kq->kq_kev[i];
429 kevp->flags &= ~EV_SYSFLAGS;
430 error = kqueue_register(kq, kevp, td);
432 if (uap->nevents != 0) {
433 kevp->flags = EV_ERROR;
435 (void) copyout((caddr_t)kevp,
436 (caddr_t)uap->eventlist,
447 uap->changelist += n;
450 uap->sysmsg_result = nerrors;
455 error = kqueue_scan(fp, uap->nevents, uap->eventlist, uap->timeout, p, &uap->sysmsg_result);
458 fdrop(fp, p->p_thread);
463 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td)
465 struct filedesc *fdp = kq->kq_fdp;
466 struct filterops *fops;
467 struct file *fp = NULL;
468 struct knote *kn = NULL;
471 if (kev->filter < 0) {
472 if (kev->filter + EVFILT_SYSCOUNT < 0)
474 fops = sysfilt_ops[~kev->filter]; /* to 0-base index */
478 * filter attach routine is responsible for insuring that
479 * the identifier can be attached to it.
481 printf("unknown filter: %d\n", kev->filter);
486 /* validate descriptor */
487 if ((u_int)kev->ident >= fdp->fd_nfiles ||
488 (fp = fdp->fd_files[kev->ident].fp) == NULL)
492 if (kev->ident < fdp->fd_knlistsize) {
493 SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link)
494 if (kq == kn->kn_kq &&
495 kev->filter == kn->kn_filter)
499 if (fdp->fd_knhashmask != 0) {
502 list = &fdp->fd_knhash[
503 KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
504 SLIST_FOREACH(kn, list, kn_link)
505 if (kev->ident == kn->kn_id &&
507 kev->filter == kn->kn_filter)
512 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
518 * kn now contains the matching knote, or NULL if no match
520 if (kev->flags & EV_ADD) {
533 * apply reference count to knote structure, and
534 * do not release it at the end of this routine.
538 kn->kn_sfflags = kev->fflags;
539 kn->kn_sdata = kev->data;
542 kn->kn_kevent = *kev;
544 knote_attach(kn, fdp);
545 if ((error = fops->f_attach(kn)) != 0) {
551 * The user may change some filter values after the
552 * initial EV_ADD, but doing so will not reset any
553 * filter which have already been triggered.
555 kn->kn_sfflags = kev->fflags;
556 kn->kn_sdata = kev->data;
557 kn->kn_kevent.udata = kev->udata;
561 if (kn->kn_fop->f_event(kn, 0))
564 } else if (kev->flags & EV_DELETE) {
565 kn->kn_fop->f_detach(kn);
570 if ((kev->flags & EV_DISABLE) &&
571 ((kn->kn_status & KN_DISABLED) == 0)) {
573 kn->kn_status |= KN_DISABLED;
577 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
579 kn->kn_status &= ~KN_DISABLED;
580 if ((kn->kn_status & KN_ACTIVE) &&
581 ((kn->kn_status & KN_QUEUED) == 0))
593 kqueue_scan(struct file *fp, int maxevents, struct kevent *ulistp,
594 const struct timespec *tsp, struct proc *p, int *res)
596 struct thread *td = p->p_thread;
597 struct kqueue *kq = (struct kqueue *)fp->f_data;
599 struct timeval atv, rtv, ttv;
600 struct knote *kn, marker;
601 int count, timeout, nkev = 0, error = 0;
608 TIMESPEC_TO_TIMEVAL(&atv, tsp);
609 if (itimerfix(&atv)) {
613 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
616 timeout = atv.tv_sec > 24 * 60 * 60 ?
617 24 * 60 * 60 * hz : tvtohz_high(&atv);
618 getmicrouptime(&rtv);
619 timevaladd(&atv, &rtv);
628 if (atv.tv_sec || atv.tv_usec) {
629 getmicrouptime(&rtv);
630 if (timevalcmp(&rtv, &atv, >=))
633 timevalsub(&ttv, &rtv);
634 timeout = ttv.tv_sec > 24 * 60 * 60 ?
635 24 * 60 * 60 * hz : tvtohz_high(&ttv);
641 if (kq->kq_count == 0) {
645 kq->kq_state |= KQ_SLEEP;
646 error = tsleep(kq, PCATCH, "kqread", timeout);
651 /* don't restart after signals... */
652 if (error == ERESTART)
654 else if (error == EWOULDBLOCK)
659 TAILQ_INSERT_TAIL(&kq->kq_head, &marker, kn_tqe);
661 kn = TAILQ_FIRST(&kq->kq_head);
662 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
665 if (count == maxevents)
669 if (kn->kn_status & KN_DISABLED) {
670 kn->kn_status &= ~KN_QUEUED;
674 if ((kn->kn_flags & EV_ONESHOT) == 0 &&
675 kn->kn_fop->f_event(kn, 0) == 0) {
676 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
680 *kevp = kn->kn_kevent;
683 if (kn->kn_flags & EV_ONESHOT) {
684 kn->kn_status &= ~KN_QUEUED;
687 kn->kn_fop->f_detach(kn);
690 } else if (kn->kn_flags & EV_CLEAR) {
693 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
696 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
699 if (nkev == KQ_NEVENTS) {
701 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
702 sizeof(struct kevent) * nkev);
711 TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe);
715 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
716 sizeof(struct kevent) * nkev);
717 *res = maxevents - count;
723 * This could be expanded to call kqueue_scan, if desired.
727 kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred,
728 int flags, struct thread *td)
735 kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred,
736 int flags, struct thread *td)
743 kqueue_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
750 kqueue_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
752 struct kqueue *kq = (struct kqueue *)fp->f_data;
756 if (events & (POLLIN | POLLRDNORM)) {
758 revents |= events & (POLLIN | POLLRDNORM);
760 selrecord(td, &kq->kq_sel);
761 kq->kq_state |= KQ_SEL;
770 kqueue_stat(struct file *fp, struct stat *st, struct thread *td)
772 struct kqueue *kq = (struct kqueue *)fp->f_data;
774 bzero((void *)st, sizeof(*st));
775 st->st_size = kq->kq_count;
776 st->st_blksize = sizeof(struct kevent);
777 st->st_mode = S_IFIFO;
783 kqueue_close(struct file *fp, struct thread *td)
785 struct proc *p = td->td_proc;
786 struct kqueue *kq = (struct kqueue *)fp->f_data;
787 struct filedesc *fdp;
788 struct knote **knp, *kn, *kn0;
793 for (i = 0; i < fdp->fd_knlistsize; i++) {
794 knp = &SLIST_FIRST(&fdp->fd_knlist[i]);
797 kn0 = SLIST_NEXT(kn, kn_link);
798 if (kq == kn->kn_kq) {
799 kn->kn_fop->f_detach(kn);
800 fdrop(kn->kn_fp, td);
804 knp = &SLIST_NEXT(kn, kn_link);
809 if (fdp->fd_knhashmask != 0) {
810 for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
811 knp = &SLIST_FIRST(&fdp->fd_knhash[i]);
814 kn0 = SLIST_NEXT(kn, kn_link);
815 if (kq == kn->kn_kq) {
816 kn->kn_fop->f_detach(kn);
817 /* XXX non-fd release of kn->kn_ptr */
821 knp = &SLIST_NEXT(kn, kn_link);
834 kqueue_wakeup(struct kqueue *kq)
837 if (kq->kq_state & KQ_SLEEP) {
838 kq->kq_state &= ~KQ_SLEEP;
841 if (kq->kq_state & KQ_SEL) {
842 kq->kq_state &= ~KQ_SEL;
843 selwakeup(&kq->kq_sel);
845 KNOTE(&kq->kq_sel.si_note, 0);
849 * walk down a list of knotes, activating them if their event has triggered.
852 knote(struct klist *list, long hint)
856 SLIST_FOREACH(kn, list, kn_selnext)
857 if (kn->kn_fop->f_event(kn, hint))
862 * remove all knotes from a specified klist
865 knote_remove(struct thread *td, struct klist *list)
869 while ((kn = SLIST_FIRST(list)) != NULL) {
870 kn->kn_fop->f_detach(kn);
876 * remove all knotes referencing a specified fd
879 knote_fdclose(struct proc *p, int fd)
881 struct filedesc *fdp = p->p_fd;
882 struct klist *list = &fdp->fd_knlist[fd];
884 knote_remove(p->p_thread, list);
888 knote_attach(struct knote *kn, struct filedesc *fdp)
893 if (! kn->kn_fop->f_isfd) {
894 if (fdp->fd_knhashmask == 0)
895 fdp->fd_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
896 &fdp->fd_knhashmask);
897 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
901 if (fdp->fd_knlistsize <= kn->kn_id) {
902 size = fdp->fd_knlistsize;
903 while (size <= kn->kn_id)
905 MALLOC(list, struct klist *,
906 size * sizeof(struct klist *), M_KQUEUE, M_WAITOK);
907 bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list,
908 fdp->fd_knlistsize * sizeof(struct klist *));
909 bzero((caddr_t)list +
910 fdp->fd_knlistsize * sizeof(struct klist *),
911 (size - fdp->fd_knlistsize) * sizeof(struct klist *));
912 if (fdp->fd_knlist != NULL)
913 FREE(fdp->fd_knlist, M_KQUEUE);
914 fdp->fd_knlistsize = size;
915 fdp->fd_knlist = list;
917 list = &fdp->fd_knlist[kn->kn_id];
919 SLIST_INSERT_HEAD(list, kn, kn_link);
924 * should be called outside of a critical section, since we don't want to
925 * hold a critical section while calling fdrop and free.
928 knote_drop(struct knote *kn, struct thread *td)
930 struct filedesc *fdp;
933 KKASSERT(td->td_proc);
934 fdp = td->td_proc->p_fd;
935 if (kn->kn_fop->f_isfd)
936 list = &fdp->fd_knlist[kn->kn_id];
938 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
940 SLIST_REMOVE(list, kn, knote, kn_link);
941 if (kn->kn_status & KN_QUEUED)
943 if (kn->kn_fop->f_isfd)
944 fdrop(kn->kn_fp, td);
950 knote_enqueue(struct knote *kn)
952 struct kqueue *kq = kn->kn_kq;
955 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
957 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
958 kn->kn_status |= KN_QUEUED;
965 knote_dequeue(struct knote *kn)
967 struct kqueue *kq = kn->kn_kq;
969 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
972 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
973 kn->kn_status &= ~KN_QUEUED;
981 knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
983 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
985 static struct knote *
988 return ((struct knote *)zalloc(knote_zone));
992 knote_free(struct knote *kn)
994 zfree(knote_zone, kn);