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 file
*fp
, int maxevents
,
63 struct kevent
*ulistp
, const struct timespec
*timeout
,
64 struct thread
*td
, int *res
);
65 static int kqueue_read(struct file
*fp
, struct uio
*uio
,
66 struct ucred
*cred
, int flags
);
67 static int kqueue_write(struct file
*fp
, struct uio
*uio
,
68 struct ucred
*cred
, int flags
);
69 static int kqueue_ioctl(struct file
*fp
, u_long com
, caddr_t data
,
70 struct ucred
*cred
, struct sysmsg
*msg
);
71 static int kqueue_poll(struct file
*fp
, int events
, struct ucred
*cred
);
72 static int kqueue_kqfilter(struct file
*fp
, struct knote
*kn
);
73 static int kqueue_stat(struct file
*fp
, struct stat
*st
,
75 static int kqueue_close(struct file
*fp
);
76 static void kqueue_wakeup(struct kqueue
*kq
);
81 static struct fileops kqueueops
= {
82 .fo_read
= kqueue_read
,
83 .fo_write
= kqueue_write
,
84 .fo_ioctl
= kqueue_ioctl
,
85 .fo_poll
= kqueue_poll
,
86 .fo_kqfilter
= kqueue_kqfilter
,
87 .fo_stat
= kqueue_stat
,
88 .fo_close
= kqueue_close
,
89 .fo_shutdown
= nofo_shutdown
92 static void knote_attach(struct knote
*kn
, struct filedesc
*fdp
);
93 static void knote_drop(struct knote
*kn
, struct thread
*td
);
94 static void knote_enqueue(struct knote
*kn
);
95 static void knote_dequeue(struct knote
*kn
);
96 static void knote_init(void);
97 static struct knote
*knote_alloc(void);
98 static void knote_free(struct knote
*kn
);
100 static void filt_kqdetach(struct knote
*kn
);
101 static int filt_kqueue(struct knote
*kn
, long hint
);
102 static int filt_procattach(struct knote
*kn
);
103 static void filt_procdetach(struct knote
*kn
);
104 static int filt_proc(struct knote
*kn
, long hint
);
105 static int filt_fileattach(struct knote
*kn
);
106 static void filt_timerexpire(void *knx
);
107 static int filt_timerattach(struct knote
*kn
);
108 static void filt_timerdetach(struct knote
*kn
);
109 static int filt_timer(struct knote
*kn
, long hint
);
111 static struct filterops file_filtops
=
112 { 1, filt_fileattach
, NULL
, NULL
};
113 static struct filterops kqread_filtops
=
114 { 1, NULL
, filt_kqdetach
, filt_kqueue
};
115 static struct filterops proc_filtops
=
116 { 0, filt_procattach
, filt_procdetach
, filt_proc
};
117 static struct filterops timer_filtops
=
118 { 0, filt_timerattach
, filt_timerdetach
, filt_timer
};
120 static vm_zone_t knote_zone
;
121 static int kq_ncallouts
= 0;
122 static int kq_calloutmax
= (4 * 1024);
123 SYSCTL_INT(_kern
, OID_AUTO
, kq_calloutmax
, CTLFLAG_RW
,
124 &kq_calloutmax
, 0, "Maximum number of callouts allocated for kqueue");
126 #define KNOTE_ACTIVATE(kn) do { \
127 kn->kn_status |= KN_ACTIVE; \
128 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
132 #define KN_HASHSIZE 64 /* XXX should be tunable */
133 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
135 extern struct filterops aio_filtops
;
136 extern struct filterops sig_filtops
;
139 * Table for for all system-defined filters.
141 static struct filterops
*sysfilt_ops
[] = {
142 &file_filtops
, /* EVFILT_READ */
143 &file_filtops
, /* EVFILT_WRITE */
144 &aio_filtops
, /* EVFILT_AIO */
145 &file_filtops
, /* EVFILT_VNODE */
146 &proc_filtops
, /* EVFILT_PROC */
147 &sig_filtops
, /* EVFILT_SIGNAL */
148 &timer_filtops
, /* EVFILT_TIMER */
152 filt_fileattach(struct knote
*kn
)
154 return (fo_kqfilter(kn
->kn_fp
, kn
));
158 * MPALMOSTSAFE - acquires mplock
161 kqueue_kqfilter(struct file
*fp
, struct knote
*kn
)
163 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
166 if (kn
->kn_filter
!= EVFILT_READ
) {
171 kn
->kn_fop
= &kqread_filtops
;
172 SLIST_INSERT_HEAD(&kq
->kq_sel
.si_note
, kn
, kn_selnext
);
178 filt_kqdetach(struct knote
*kn
)
180 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
182 SLIST_REMOVE(&kq
->kq_sel
.si_note
, kn
, knote
, kn_selnext
);
187 filt_kqueue(struct knote
*kn
, long hint
)
189 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
191 kn
->kn_data
= kq
->kq_count
;
192 return (kn
->kn_data
> 0);
196 filt_procattach(struct knote
*kn
)
202 p
= pfind(kn
->kn_id
);
203 if (p
== NULL
&& (kn
->kn_sfflags
& NOTE_EXIT
)) {
204 p
= zpfind(kn
->kn_id
);
209 if (!PRISON_CHECK(curthread
->td_ucred
, p
->p_ucred
))
212 kn
->kn_ptr
.p_proc
= p
;
213 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
216 * internal flag indicating registration done by kernel
218 if (kn
->kn_flags
& EV_FLAG1
) {
219 kn
->kn_data
= kn
->kn_sdata
; /* ppid */
220 kn
->kn_fflags
= NOTE_CHILD
;
221 kn
->kn_flags
&= ~EV_FLAG1
;
224 /* XXX lock the proc here while adding to the list? */
225 SLIST_INSERT_HEAD(&p
->p_klist
, kn
, kn_selnext
);
228 * Immediately activate any exit notes if the target process is a
229 * zombie. This is necessary to handle the case where the target
230 * process, e.g. a child, dies before the kevent is registered.
232 if (immediate
&& filt_proc(kn
, NOTE_EXIT
))
239 * The knote may be attached to a different process, which may exit,
240 * leaving nothing for the knote to be attached to. So when the process
241 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
242 * it will be deleted when read out. However, as part of the knote deletion,
243 * this routine is called, so a check is needed to avoid actually performing
244 * a detach, because the original process does not exist any more.
247 filt_procdetach(struct knote
*kn
)
251 if (kn
->kn_status
& KN_DETACHED
)
253 /* XXX locking? this might modify another process. */
254 p
= kn
->kn_ptr
.p_proc
;
255 SLIST_REMOVE(&p
->p_klist
, kn
, knote
, kn_selnext
);
259 filt_proc(struct knote
*kn
, long hint
)
264 * mask off extra data
266 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
269 * if the user is interested in this event, record it.
271 if (kn
->kn_sfflags
& event
)
272 kn
->kn_fflags
|= event
;
275 * Process is gone, so flag the event as finished. Detach the
276 * knote from the process now because the process will be poof,
279 if (event
== NOTE_EXIT
) {
280 struct proc
*p
= kn
->kn_ptr
.p_proc
;
281 if ((kn
->kn_status
& KN_DETACHED
) == 0) {
282 SLIST_REMOVE(&p
->p_klist
, kn
, knote
, kn_selnext
);
283 kn
->kn_status
|= KN_DETACHED
;
284 kn
->kn_data
= p
->p_xstat
;
285 kn
->kn_ptr
.p_proc
= NULL
;
287 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
292 * process forked, and user wants to track the new process,
293 * so attach a new knote to it, and immediately report an
294 * event with the parent's pid.
296 if ((event
== NOTE_FORK
) && (kn
->kn_sfflags
& NOTE_TRACK
)) {
301 * register knote with new process.
303 kev
.ident
= hint
& NOTE_PDATAMASK
; /* pid */
304 kev
.filter
= kn
->kn_filter
;
305 kev
.flags
= kn
->kn_flags
| EV_ADD
| EV_ENABLE
| EV_FLAG1
;
306 kev
.fflags
= kn
->kn_sfflags
;
307 kev
.data
= kn
->kn_id
; /* parent */
308 kev
.udata
= kn
->kn_kevent
.udata
; /* preserve udata */
309 error
= kqueue_register(kn
->kn_kq
, &kev
, NULL
);
311 kn
->kn_fflags
|= NOTE_TRACKERR
;
314 return (kn
->kn_fflags
!= 0);
318 filt_timerexpire(void *knx
)
320 struct knote
*kn
= knx
;
321 struct callout
*calloutp
;
328 if ((kn
->kn_flags
& EV_ONESHOT
) == 0) {
329 tv
.tv_sec
= kn
->kn_sdata
/ 1000;
330 tv
.tv_usec
= (kn
->kn_sdata
% 1000) * 1000;
331 tticks
= tvtohz_high(&tv
);
332 calloutp
= (struct callout
*)kn
->kn_hook
;
333 callout_reset(calloutp
, tticks
, filt_timerexpire
, kn
);
338 * data contains amount of time to sleep, in milliseconds
341 filt_timerattach(struct knote
*kn
)
343 struct callout
*calloutp
;
347 if (kq_ncallouts
>= kq_calloutmax
)
351 tv
.tv_sec
= kn
->kn_sdata
/ 1000;
352 tv
.tv_usec
= (kn
->kn_sdata
% 1000) * 1000;
353 tticks
= tvtohz_high(&tv
);
355 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
356 MALLOC(calloutp
, struct callout
*, sizeof(*calloutp
),
358 callout_init(calloutp
);
359 kn
->kn_hook
= (caddr_t
)calloutp
;
360 callout_reset(calloutp
, tticks
, filt_timerexpire
, kn
);
366 filt_timerdetach(struct knote
*kn
)
368 struct callout
*calloutp
;
370 calloutp
= (struct callout
*)kn
->kn_hook
;
371 callout_stop(calloutp
);
372 FREE(calloutp
, M_KQUEUE
);
377 filt_timer(struct knote
*kn
, long hint
)
380 return (kn
->kn_data
!= 0);
387 sys_kqueue(struct kqueue_args
*uap
)
389 struct thread
*td
= curthread
;
394 error
= falloc(td
->td_lwp
, &fp
, &fd
);
397 fp
->f_flag
= FREAD
| FWRITE
;
398 fp
->f_type
= DTYPE_KQUEUE
;
399 fp
->f_ops
= &kqueueops
;
401 kq
= kmalloc(sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
| M_ZERO
);
402 TAILQ_INIT(&kq
->kq_head
);
403 kq
->kq_fdp
= td
->td_proc
->p_fd
;
406 fsetfd(kq
->kq_fdp
, fp
, fd
);
407 uap
->sysmsg_result
= fd
;
416 sys_kevent(struct kevent_args
*uap
)
418 struct thread
*td
= curthread
;
419 struct proc
*p
= td
->td_proc
;
422 struct file
*fp
= NULL
;
424 int i
, n
, nerrors
, error
;
426 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
429 if (fp
->f_type
!= DTYPE_KQUEUE
) {
434 if (uap
->timeout
!= NULL
) {
435 error
= copyin(uap
->timeout
, &ts
, sizeof(ts
));
441 kq
= (struct kqueue
*)fp
->f_data
;
445 while (uap
->nchanges
> 0) {
446 n
= uap
->nchanges
> KQ_NEVENTS
? KQ_NEVENTS
: uap
->nchanges
;
447 error
= copyin(uap
->changelist
, kq
->kq_kev
,
448 n
* sizeof(struct kevent
));
451 for (i
= 0; i
< n
; i
++) {
452 kevp
= &kq
->kq_kev
[i
];
453 kevp
->flags
&= ~EV_SYSFLAGS
;
454 error
= kqueue_register(kq
, kevp
, td
);
456 if (uap
->nevents
!= 0) {
457 kevp
->flags
= EV_ERROR
;
459 (void) copyout((caddr_t
)kevp
,
460 (caddr_t
)uap
->eventlist
,
471 uap
->changelist
+= n
;
474 uap
->sysmsg_result
= nerrors
;
479 error
= kqueue_scan(fp
, uap
->nevents
, uap
->eventlist
,
480 uap
->timeout
, td
, &uap
->sysmsg_result
);
489 kqueue_register(struct kqueue
*kq
, struct kevent
*kev
, struct thread
*td
)
491 struct filedesc
*fdp
= kq
->kq_fdp
;
492 struct filterops
*fops
;
493 struct file
*fp
= NULL
;
494 struct knote
*kn
= NULL
;
497 if (kev
->filter
< 0) {
498 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
500 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
504 * filter attach routine is responsible for insuring that
505 * the identifier can be attached to it.
507 kprintf("unknown filter: %d\n", kev
->filter
);
512 /* validate descriptor */
513 fp
= holdfp(fdp
, kev
->ident
, -1);
517 if (kev
->ident
< fdp
->fd_knlistsize
) {
518 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
519 if (kq
== kn
->kn_kq
&&
520 kev
->filter
== kn
->kn_filter
)
524 if (fdp
->fd_knhashmask
!= 0) {
527 list
= &fdp
->fd_knhash
[
528 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
529 SLIST_FOREACH(kn
, list
, kn_link
)
530 if (kev
->ident
== kn
->kn_id
&&
532 kev
->filter
== kn
->kn_filter
)
537 if (kn
== NULL
&& ((kev
->flags
& EV_ADD
) == 0)) {
543 * kn now contains the matching knote, or NULL if no match
545 if (kev
->flags
& EV_ADD
) {
558 * apply reference count to knote structure, and
559 * do not release it at the end of this routine.
563 kn
->kn_sfflags
= kev
->fflags
;
564 kn
->kn_sdata
= kev
->data
;
567 kn
->kn_kevent
= *kev
;
569 knote_attach(kn
, fdp
);
570 if ((error
= fops
->f_attach(kn
)) != 0) {
576 * The user may change some filter values after the
577 * initial EV_ADD, but doing so will not reset any
578 * filter which have already been triggered.
580 kn
->kn_sfflags
= kev
->fflags
;
581 kn
->kn_sdata
= kev
->data
;
582 kn
->kn_kevent
.udata
= kev
->udata
;
586 if (kn
->kn_fop
->f_event(kn
, 0))
589 } else if (kev
->flags
& EV_DELETE
) {
590 kn
->kn_fop
->f_detach(kn
);
595 if ((kev
->flags
& EV_DISABLE
) &&
596 ((kn
->kn_status
& KN_DISABLED
) == 0)) {
598 kn
->kn_status
|= KN_DISABLED
;
602 if ((kev
->flags
& EV_ENABLE
) && (kn
->kn_status
& KN_DISABLED
)) {
604 kn
->kn_status
&= ~KN_DISABLED
;
605 if ((kn
->kn_status
& KN_ACTIVE
) &&
606 ((kn
->kn_status
& KN_QUEUED
) == 0))
618 kqueue_scan(struct file
*fp
, int maxevents
, struct kevent
*ulistp
,
619 const struct timespec
*tsp
, struct thread
*td
, int *res
)
621 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
623 struct timeval atv
, rtv
, ttv
;
624 struct knote
*kn
, marker
;
625 int count
, timeout
, nkev
= 0, error
= 0;
632 TIMESPEC_TO_TIMEVAL(&atv
, tsp
);
633 if (itimerfix(&atv
)) {
637 if (tsp
->tv_sec
== 0 && tsp
->tv_nsec
== 0)
640 timeout
= atv
.tv_sec
> 24 * 60 * 60 ?
641 24 * 60 * 60 * hz
: tvtohz_high(&atv
);
642 getmicrouptime(&rtv
);
643 timevaladd(&atv
, &rtv
);
652 if (atv
.tv_sec
|| atv
.tv_usec
) {
653 getmicrouptime(&rtv
);
654 if (timevalcmp(&rtv
, &atv
, >=))
657 timevalsub(&ttv
, &rtv
);
658 timeout
= ttv
.tv_sec
> 24 * 60 * 60 ?
659 24 * 60 * 60 * hz
: tvtohz_high(&ttv
);
665 if (kq
->kq_count
== 0) {
669 kq
->kq_state
|= KQ_SLEEP
;
670 error
= tsleep(kq
, PCATCH
, "kqread", timeout
);
675 /* don't restart after signals... */
676 if (error
== ERESTART
)
678 else if (error
== EWOULDBLOCK
)
683 TAILQ_INSERT_TAIL(&kq
->kq_head
, &marker
, kn_tqe
);
685 kn
= TAILQ_FIRST(&kq
->kq_head
);
686 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
689 if (count
== maxevents
)
693 if (kn
->kn_status
& KN_DISABLED
) {
694 kn
->kn_status
&= ~KN_QUEUED
;
698 if ((kn
->kn_flags
& EV_ONESHOT
) == 0 &&
699 kn
->kn_fop
->f_event(kn
, 0) == 0) {
700 kn
->kn_status
&= ~(KN_QUEUED
| KN_ACTIVE
);
704 *kevp
= kn
->kn_kevent
;
707 if (kn
->kn_flags
& EV_ONESHOT
) {
708 kn
->kn_status
&= ~KN_QUEUED
;
711 kn
->kn_fop
->f_detach(kn
);
714 } else if (kn
->kn_flags
& EV_CLEAR
) {
717 kn
->kn_status
&= ~(KN_QUEUED
| KN_ACTIVE
);
720 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
723 if (nkev
== KQ_NEVENTS
) {
725 error
= copyout((caddr_t
)&kq
->kq_kev
, (caddr_t
)ulistp
,
726 sizeof(struct kevent
) * nkev
);
735 TAILQ_REMOVE(&kq
->kq_head
, &marker
, kn_tqe
);
739 error
= copyout((caddr_t
)&kq
->kq_kev
, (caddr_t
)ulistp
,
740 sizeof(struct kevent
) * nkev
);
741 *res
= maxevents
- count
;
747 * This could be expanded to call kqueue_scan, if desired.
752 kqueue_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int flags
)
761 kqueue_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int flags
)
770 kqueue_ioctl(struct file
*fp
, u_long com
, caddr_t data
,
771 struct ucred
*cred
, struct sysmsg
*msg
)
777 kq
= (struct kqueue
*)fp
->f_data
;
782 kq
->kq_state
|= KQ_ASYNC
;
784 kq
->kq_state
&= ~KQ_ASYNC
;
788 error
= fsetown(*(int *)data
, &kq
->kq_sigio
);
799 * MPALMOSTSAFE - acquires mplock
802 kqueue_poll(struct file
*fp
, int events
, struct ucred
*cred
)
804 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
809 if (events
& (POLLIN
| POLLRDNORM
)) {
811 revents
|= events
& (POLLIN
| POLLRDNORM
);
813 selrecord(curthread
, &kq
->kq_sel
);
814 kq
->kq_state
|= KQ_SEL
;
826 kqueue_stat(struct file
*fp
, struct stat
*st
, struct ucred
*cred
)
828 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
830 bzero((void *)st
, sizeof(*st
));
831 st
->st_size
= kq
->kq_count
;
832 st
->st_blksize
= sizeof(struct kevent
);
833 st
->st_mode
= S_IFIFO
;
838 * MPALMOSTSAFE - acquires mplock
841 kqueue_close(struct file
*fp
)
843 struct thread
*td
= curthread
;
844 struct proc
*p
= td
->td_proc
;
845 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
846 struct filedesc
*fdp
;
847 struct knote
**knp
, *kn
, *kn0
;
853 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
854 knp
= &SLIST_FIRST(&fdp
->fd_knlist
[i
]);
857 kn0
= SLIST_NEXT(kn
, kn_link
);
858 if (kq
== kn
->kn_kq
) {
859 kn
->kn_fop
->f_detach(kn
);
864 knp
= &SLIST_NEXT(kn
, kn_link
);
869 if (fdp
->fd_knhashmask
!= 0) {
870 for (i
= 0; i
< fdp
->fd_knhashmask
+ 1; i
++) {
871 knp
= &SLIST_FIRST(&fdp
->fd_knhash
[i
]);
874 kn0
= SLIST_NEXT(kn
, kn_link
);
875 if (kq
== kn
->kn_kq
) {
876 kn
->kn_fop
->f_detach(kn
);
877 /* XXX non-fd release of kn->kn_ptr */
881 knp
= &SLIST_NEXT(kn
, kn_link
);
888 funsetown(kq
->kq_sigio
);
896 kqueue_wakeup(struct kqueue
*kq
)
898 if (kq
->kq_state
& KQ_SLEEP
) {
899 kq
->kq_state
&= ~KQ_SLEEP
;
902 if (kq
->kq_state
& KQ_SEL
) {
903 kq
->kq_state
&= ~KQ_SEL
;
904 selwakeup(&kq
->kq_sel
);
906 KNOTE(&kq
->kq_sel
.si_note
, 0);
910 * walk down a list of knotes, activating them if their event has triggered.
913 knote(struct klist
*list
, long hint
)
917 SLIST_FOREACH(kn
, list
, kn_selnext
)
918 if (kn
->kn_fop
->f_event(kn
, hint
))
923 * remove all knotes from a specified klist
926 knote_remove(struct thread
*td
, struct klist
*list
)
930 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
931 kn
->kn_fop
->f_detach(kn
);
937 * remove all knotes referencing a specified fd
940 knote_fdclose(struct proc
*p
, int fd
)
942 struct filedesc
*fdp
= p
->p_fd
;
943 struct klist
*list
= &fdp
->fd_knlist
[fd
];
944 /* Take any thread of p */
945 struct thread
*td
= FIRST_LWP_IN_PROC(p
)->lwp_thread
;
947 knote_remove(td
, list
);
951 knote_attach(struct knote
*kn
, struct filedesc
*fdp
)
956 if (! kn
->kn_fop
->f_isfd
) {
957 if (fdp
->fd_knhashmask
== 0)
958 fdp
->fd_knhash
= hashinit(KN_HASHSIZE
, M_KQUEUE
,
959 &fdp
->fd_knhashmask
);
960 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
964 if (fdp
->fd_knlistsize
<= kn
->kn_id
) {
965 size
= fdp
->fd_knlistsize
;
966 while (size
<= kn
->kn_id
)
968 MALLOC(list
, struct klist
*,
969 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
970 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
971 fdp
->fd_knlistsize
* sizeof(struct klist
*));
972 bzero((caddr_t
)list
+
973 fdp
->fd_knlistsize
* sizeof(struct klist
*),
974 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
975 if (fdp
->fd_knlist
!= NULL
)
976 FREE(fdp
->fd_knlist
, M_KQUEUE
);
977 fdp
->fd_knlistsize
= size
;
978 fdp
->fd_knlist
= list
;
980 list
= &fdp
->fd_knlist
[kn
->kn_id
];
982 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
987 * should be called outside of a critical section, since we don't want to
988 * hold a critical section while calling fdrop and free.
991 knote_drop(struct knote
*kn
, struct thread
*td
)
993 struct filedesc
*fdp
;
996 KKASSERT(td
->td_proc
);
997 fdp
= td
->td_proc
->p_fd
;
998 if (kn
->kn_fop
->f_isfd
)
999 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1001 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1003 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
1004 if (kn
->kn_status
& KN_QUEUED
)
1006 if (kn
->kn_fop
->f_isfd
)
1013 knote_enqueue(struct knote
*kn
)
1015 struct kqueue
*kq
= kn
->kn_kq
;
1018 KASSERT((kn
->kn_status
& KN_QUEUED
) == 0, ("knote already queued"));
1020 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1021 kn
->kn_status
|= KN_QUEUED
;
1025 * Send SIGIO on request (typically set up as a mailbox signal)
1027 if (kq
->kq_sigio
&& (kq
->kq_state
& KQ_ASYNC
) && kq
->kq_count
== 1)
1028 pgsigio(kq
->kq_sigio
, SIGIO
, 0);
1034 knote_dequeue(struct knote
*kn
)
1036 struct kqueue
*kq
= kn
->kn_kq
;
1038 KASSERT(kn
->kn_status
& KN_QUEUED
, ("knote not queued"));
1041 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1042 kn
->kn_status
&= ~KN_QUEUED
;
1050 knote_zone
= zinit("KNOTE", sizeof(struct knote
), 0, 0, 1);
1052 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
1054 static struct knote
*
1057 return ((struct knote
*)zalloc(knote_zone
));
1061 knote_free(struct knote
*kn
)
1063 zfree(knote_zone
, kn
);