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/thread2.h>
52 #include <sys/signalvar.h>
53 #include <sys/filio.h>
54 #include <sys/file2.h>
56 #include <vm/vm_zone.h>
58 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system");
60 static int kqueue_scan(struct file
*fp
, int maxevents
,
61 struct kevent
*ulistp
, const struct timespec
*timeout
,
62 struct thread
*td
, int *res
);
63 static int kqueue_read(struct file
*fp
, struct uio
*uio
,
64 struct ucred
*cred
, int flags
);
65 static int kqueue_write(struct file
*fp
, struct uio
*uio
,
66 struct ucred
*cred
, int flags
);
67 static int kqueue_ioctl(struct file
*fp
, u_long com
, caddr_t data
,
68 struct ucred
*cred
, struct sysmsg
*msg
);
69 static int kqueue_poll(struct file
*fp
, int events
, struct ucred
*cred
);
70 static int kqueue_kqfilter(struct file
*fp
, struct knote
*kn
);
71 static int kqueue_stat(struct file
*fp
, struct stat
*st
,
73 static int kqueue_close(struct file
*fp
);
74 static void kqueue_wakeup(struct kqueue
*kq
);
79 static struct fileops kqueueops
= {
80 .fo_read
= kqueue_read
,
81 .fo_write
= kqueue_write
,
82 .fo_ioctl
= kqueue_ioctl
,
83 .fo_poll
= kqueue_poll
,
84 .fo_kqfilter
= kqueue_kqfilter
,
85 .fo_stat
= kqueue_stat
,
86 .fo_close
= kqueue_close
,
87 .fo_shutdown
= nofo_shutdown
90 static void knote_attach(struct knote
*kn
, struct filedesc
*fdp
);
91 static void knote_drop(struct knote
*kn
, struct thread
*td
);
92 static void knote_enqueue(struct knote
*kn
);
93 static void knote_dequeue(struct knote
*kn
);
94 static void knote_init(void);
95 static struct knote
*knote_alloc(void);
96 static void knote_free(struct knote
*kn
);
98 static void filt_kqdetach(struct knote
*kn
);
99 static int filt_kqueue(struct knote
*kn
, long hint
);
100 static int filt_procattach(struct knote
*kn
);
101 static void filt_procdetach(struct knote
*kn
);
102 static int filt_proc(struct knote
*kn
, long hint
);
103 static int filt_fileattach(struct knote
*kn
);
104 static void filt_timerexpire(void *knx
);
105 static int filt_timerattach(struct knote
*kn
);
106 static void filt_timerdetach(struct knote
*kn
);
107 static int filt_timer(struct knote
*kn
, long hint
);
109 static struct filterops file_filtops
=
110 { 1, filt_fileattach
, NULL
, NULL
};
111 static struct filterops kqread_filtops
=
112 { 1, NULL
, filt_kqdetach
, filt_kqueue
};
113 static struct filterops proc_filtops
=
114 { 0, filt_procattach
, filt_procdetach
, filt_proc
};
115 static struct filterops timer_filtops
=
116 { 0, filt_timerattach
, filt_timerdetach
, filt_timer
};
118 static vm_zone_t knote_zone
;
119 static int kq_ncallouts
= 0;
120 static int kq_calloutmax
= (4 * 1024);
121 SYSCTL_INT(_kern
, OID_AUTO
, kq_calloutmax
, CTLFLAG_RW
,
122 &kq_calloutmax
, 0, "Maximum number of callouts allocated for kqueue");
124 #define KNOTE_ACTIVATE(kn) do { \
125 kn->kn_status |= KN_ACTIVE; \
126 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
130 #define KN_HASHSIZE 64 /* XXX should be tunable */
131 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
133 extern struct filterops aio_filtops
;
134 extern struct filterops sig_filtops
;
137 * Table for for all system-defined filters.
139 static struct filterops
*sysfilt_ops
[] = {
140 &file_filtops
, /* EVFILT_READ */
141 &file_filtops
, /* EVFILT_WRITE */
142 &aio_filtops
, /* EVFILT_AIO */
143 &file_filtops
, /* EVFILT_VNODE */
144 &proc_filtops
, /* EVFILT_PROC */
145 &sig_filtops
, /* EVFILT_SIGNAL */
146 &timer_filtops
, /* EVFILT_TIMER */
150 filt_fileattach(struct knote
*kn
)
152 return (fo_kqfilter(kn
->kn_fp
, kn
));
156 * MPALMOSTSAFE - acquires mplock
159 kqueue_kqfilter(struct file
*fp
, struct knote
*kn
)
161 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
164 if (kn
->kn_filter
!= EVFILT_READ
) {
169 kn
->kn_fop
= &kqread_filtops
;
170 SLIST_INSERT_HEAD(&kq
->kq_sel
.si_note
, kn
, kn_selnext
);
176 filt_kqdetach(struct knote
*kn
)
178 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
180 SLIST_REMOVE(&kq
->kq_sel
.si_note
, kn
, knote
, kn_selnext
);
185 filt_kqueue(struct knote
*kn
, long hint
)
187 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
189 kn
->kn_data
= kq
->kq_count
;
190 return (kn
->kn_data
> 0);
194 filt_procattach(struct knote
*kn
)
200 p
= pfind(kn
->kn_id
);
201 if (p
== NULL
&& (kn
->kn_sfflags
& NOTE_EXIT
)) {
202 p
= zpfind(kn
->kn_id
);
207 if (! PRISON_CHECK(curproc
->p_ucred
, p
->p_ucred
))
210 kn
->kn_ptr
.p_proc
= p
;
211 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
214 * internal flag indicating registration done by kernel
216 if (kn
->kn_flags
& EV_FLAG1
) {
217 kn
->kn_data
= kn
->kn_sdata
; /* ppid */
218 kn
->kn_fflags
= NOTE_CHILD
;
219 kn
->kn_flags
&= ~EV_FLAG1
;
222 /* XXX lock the proc here while adding to the list? */
223 SLIST_INSERT_HEAD(&p
->p_klist
, kn
, kn_selnext
);
226 * Immediately activate any exit notes if the target process is a
227 * zombie. This is necessary to handle the case where the target
228 * process, e.g. a child, dies before the kevent is registered.
230 if (immediate
&& filt_proc(kn
, NOTE_EXIT
))
237 * The knote may be attached to a different process, which may exit,
238 * leaving nothing for the knote to be attached to. So when the process
239 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
240 * it will be deleted when read out. However, as part of the knote deletion,
241 * this routine is called, so a check is needed to avoid actually performing
242 * a detach, because the original process does not exist any more.
245 filt_procdetach(struct knote
*kn
)
249 if (kn
->kn_status
& KN_DETACHED
)
251 /* XXX locking? this might modify another process. */
252 p
= kn
->kn_ptr
.p_proc
;
253 SLIST_REMOVE(&p
->p_klist
, kn
, knote
, kn_selnext
);
257 filt_proc(struct knote
*kn
, long hint
)
262 * mask off extra data
264 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
267 * if the user is interested in this event, record it.
269 if (kn
->kn_sfflags
& event
)
270 kn
->kn_fflags
|= event
;
273 * Process is gone, so flag the event as finished. Detach the
274 * knote from the process now because the process will be poof,
277 if (event
== NOTE_EXIT
) {
278 struct proc
*p
= kn
->kn_ptr
.p_proc
;
279 if ((kn
->kn_status
& KN_DETACHED
) == 0) {
280 SLIST_REMOVE(&p
->p_klist
, kn
, knote
, kn_selnext
);
281 kn
->kn_status
|= KN_DETACHED
;
282 kn
->kn_data
= p
->p_xstat
;
283 kn
->kn_ptr
.p_proc
= NULL
;
285 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
290 * process forked, and user wants to track the new process,
291 * so attach a new knote to it, and immediately report an
292 * event with the parent's pid.
294 if ((event
== NOTE_FORK
) && (kn
->kn_sfflags
& NOTE_TRACK
)) {
299 * register knote with new process.
301 kev
.ident
= hint
& NOTE_PDATAMASK
; /* pid */
302 kev
.filter
= kn
->kn_filter
;
303 kev
.flags
= kn
->kn_flags
| EV_ADD
| EV_ENABLE
| EV_FLAG1
;
304 kev
.fflags
= kn
->kn_sfflags
;
305 kev
.data
= kn
->kn_id
; /* parent */
306 kev
.udata
= kn
->kn_kevent
.udata
; /* preserve udata */
307 error
= kqueue_register(kn
->kn_kq
, &kev
, NULL
);
309 kn
->kn_fflags
|= NOTE_TRACKERR
;
312 return (kn
->kn_fflags
!= 0);
316 filt_timerexpire(void *knx
)
318 struct knote
*kn
= knx
;
319 struct callout
*calloutp
;
326 if ((kn
->kn_flags
& EV_ONESHOT
) == 0) {
327 tv
.tv_sec
= kn
->kn_sdata
/ 1000;
328 tv
.tv_usec
= (kn
->kn_sdata
% 1000) * 1000;
329 tticks
= tvtohz_high(&tv
);
330 calloutp
= (struct callout
*)kn
->kn_hook
;
331 callout_reset(calloutp
, tticks
, filt_timerexpire
, kn
);
336 * data contains amount of time to sleep, in milliseconds
339 filt_timerattach(struct knote
*kn
)
341 struct callout
*calloutp
;
345 if (kq_ncallouts
>= kq_calloutmax
)
349 tv
.tv_sec
= kn
->kn_sdata
/ 1000;
350 tv
.tv_usec
= (kn
->kn_sdata
% 1000) * 1000;
351 tticks
= tvtohz_high(&tv
);
353 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
354 MALLOC(calloutp
, struct callout
*, sizeof(*calloutp
),
356 callout_init(calloutp
);
357 kn
->kn_hook
= (caddr_t
)calloutp
;
358 callout_reset(calloutp
, tticks
, filt_timerexpire
, kn
);
364 filt_timerdetach(struct knote
*kn
)
366 struct callout
*calloutp
;
368 calloutp
= (struct callout
*)kn
->kn_hook
;
369 callout_stop(calloutp
);
370 FREE(calloutp
, M_KQUEUE
);
375 filt_timer(struct knote
*kn
, long hint
)
378 return (kn
->kn_data
!= 0);
385 sys_kqueue(struct kqueue_args
*uap
)
387 struct proc
*p
= curproc
;
388 struct filedesc
*fdp
= p
->p_fd
;
393 error
= falloc(p
, &fp
, &fd
);
396 fp
->f_flag
= FREAD
| FWRITE
;
397 fp
->f_type
= DTYPE_KQUEUE
;
398 fp
->f_ops
= &kqueueops
;
400 kq
= kmalloc(sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
| M_ZERO
);
401 TAILQ_INIT(&kq
->kq_head
);
406 uap
->sysmsg_result
= fd
;
415 sys_kevent(struct kevent_args
*uap
)
417 struct thread
*td
= curthread
;
418 struct proc
*p
= td
->td_proc
;
421 struct file
*fp
= NULL
;
423 int i
, n
, nerrors
, error
;
425 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
428 if (fp
->f_type
!= DTYPE_KQUEUE
) {
433 if (uap
->timeout
!= NULL
) {
434 error
= copyin(uap
->timeout
, &ts
, sizeof(ts
));
440 kq
= (struct kqueue
*)fp
->f_data
;
444 while (uap
->nchanges
> 0) {
445 n
= uap
->nchanges
> KQ_NEVENTS
? KQ_NEVENTS
: uap
->nchanges
;
446 error
= copyin(uap
->changelist
, kq
->kq_kev
,
447 n
* sizeof(struct kevent
));
450 for (i
= 0; i
< n
; i
++) {
451 kevp
= &kq
->kq_kev
[i
];
452 kevp
->flags
&= ~EV_SYSFLAGS
;
453 error
= kqueue_register(kq
, kevp
, td
);
455 if (uap
->nevents
!= 0) {
456 kevp
->flags
= EV_ERROR
;
458 (void) copyout((caddr_t
)kevp
,
459 (caddr_t
)uap
->eventlist
,
470 uap
->changelist
+= n
;
473 uap
->sysmsg_result
= nerrors
;
478 error
= kqueue_scan(fp
, uap
->nevents
, uap
->eventlist
,
479 uap
->timeout
, td
, &uap
->sysmsg_result
);
488 kqueue_register(struct kqueue
*kq
, struct kevent
*kev
, struct thread
*td
)
490 struct filedesc
*fdp
= kq
->kq_fdp
;
491 struct filterops
*fops
;
492 struct file
*fp
= NULL
;
493 struct knote
*kn
= NULL
;
496 if (kev
->filter
< 0) {
497 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
499 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
503 * filter attach routine is responsible for insuring that
504 * the identifier can be attached to it.
506 kprintf("unknown filter: %d\n", kev
->filter
);
511 /* validate descriptor */
512 fp
= holdfp(fdp
, kev
->ident
, -1);
516 if (kev
->ident
< fdp
->fd_knlistsize
) {
517 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
518 if (kq
== kn
->kn_kq
&&
519 kev
->filter
== kn
->kn_filter
)
523 if (fdp
->fd_knhashmask
!= 0) {
526 list
= &fdp
->fd_knhash
[
527 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
528 SLIST_FOREACH(kn
, list
, kn_link
)
529 if (kev
->ident
== kn
->kn_id
&&
531 kev
->filter
== kn
->kn_filter
)
536 if (kn
== NULL
&& ((kev
->flags
& EV_ADD
) == 0)) {
542 * kn now contains the matching knote, or NULL if no match
544 if (kev
->flags
& EV_ADD
) {
557 * apply reference count to knote structure, and
558 * do not release it at the end of this routine.
562 kn
->kn_sfflags
= kev
->fflags
;
563 kn
->kn_sdata
= kev
->data
;
566 kn
->kn_kevent
= *kev
;
568 knote_attach(kn
, fdp
);
569 if ((error
= fops
->f_attach(kn
)) != 0) {
575 * The user may change some filter values after the
576 * initial EV_ADD, but doing so will not reset any
577 * filter which have already been triggered.
579 kn
->kn_sfflags
= kev
->fflags
;
580 kn
->kn_sdata
= kev
->data
;
581 kn
->kn_kevent
.udata
= kev
->udata
;
585 if (kn
->kn_fop
->f_event(kn
, 0))
588 } else if (kev
->flags
& EV_DELETE
) {
589 kn
->kn_fop
->f_detach(kn
);
594 if ((kev
->flags
& EV_DISABLE
) &&
595 ((kn
->kn_status
& KN_DISABLED
) == 0)) {
597 kn
->kn_status
|= KN_DISABLED
;
601 if ((kev
->flags
& EV_ENABLE
) && (kn
->kn_status
& KN_DISABLED
)) {
603 kn
->kn_status
&= ~KN_DISABLED
;
604 if ((kn
->kn_status
& KN_ACTIVE
) &&
605 ((kn
->kn_status
& KN_QUEUED
) == 0))
617 kqueue_scan(struct file
*fp
, int maxevents
, struct kevent
*ulistp
,
618 const struct timespec
*tsp
, struct thread
*td
, int *res
)
620 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
622 struct timeval atv
, rtv
, ttv
;
623 struct knote
*kn
, marker
;
624 int count
, timeout
, nkev
= 0, error
= 0;
631 TIMESPEC_TO_TIMEVAL(&atv
, tsp
);
632 if (itimerfix(&atv
)) {
636 if (tsp
->tv_sec
== 0 && tsp
->tv_nsec
== 0)
639 timeout
= atv
.tv_sec
> 24 * 60 * 60 ?
640 24 * 60 * 60 * hz
: tvtohz_high(&atv
);
641 getmicrouptime(&rtv
);
642 timevaladd(&atv
, &rtv
);
651 if (atv
.tv_sec
|| atv
.tv_usec
) {
652 getmicrouptime(&rtv
);
653 if (timevalcmp(&rtv
, &atv
, >=))
656 timevalsub(&ttv
, &rtv
);
657 timeout
= ttv
.tv_sec
> 24 * 60 * 60 ?
658 24 * 60 * 60 * hz
: tvtohz_high(&ttv
);
664 if (kq
->kq_count
== 0) {
668 kq
->kq_state
|= KQ_SLEEP
;
669 error
= tsleep(kq
, PCATCH
, "kqread", timeout
);
674 /* don't restart after signals... */
675 if (error
== ERESTART
)
677 else if (error
== EWOULDBLOCK
)
682 TAILQ_INSERT_TAIL(&kq
->kq_head
, &marker
, kn_tqe
);
684 kn
= TAILQ_FIRST(&kq
->kq_head
);
685 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
688 if (count
== maxevents
)
692 if (kn
->kn_status
& KN_DISABLED
) {
693 kn
->kn_status
&= ~KN_QUEUED
;
697 if ((kn
->kn_flags
& EV_ONESHOT
) == 0 &&
698 kn
->kn_fop
->f_event(kn
, 0) == 0) {
699 kn
->kn_status
&= ~(KN_QUEUED
| KN_ACTIVE
);
703 *kevp
= kn
->kn_kevent
;
706 if (kn
->kn_flags
& EV_ONESHOT
) {
707 kn
->kn_status
&= ~KN_QUEUED
;
710 kn
->kn_fop
->f_detach(kn
);
713 } else if (kn
->kn_flags
& EV_CLEAR
) {
716 kn
->kn_status
&= ~(KN_QUEUED
| KN_ACTIVE
);
719 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
722 if (nkev
== KQ_NEVENTS
) {
724 error
= copyout((caddr_t
)&kq
->kq_kev
, (caddr_t
)ulistp
,
725 sizeof(struct kevent
) * nkev
);
734 TAILQ_REMOVE(&kq
->kq_head
, &marker
, kn_tqe
);
738 error
= copyout((caddr_t
)&kq
->kq_kev
, (caddr_t
)ulistp
,
739 sizeof(struct kevent
) * nkev
);
740 *res
= maxevents
- count
;
746 * This could be expanded to call kqueue_scan, if desired.
751 kqueue_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int flags
)
760 kqueue_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int flags
)
769 kqueue_ioctl(struct file
*fp
, u_long com
, caddr_t data
,
770 struct ucred
*cred
, struct sysmsg
*msg
)
776 kq
= (struct kqueue
*)fp
->f_data
;
781 kq
->kq_state
|= KQ_ASYNC
;
783 kq
->kq_state
&= ~KQ_ASYNC
;
787 error
= fsetown(*(int *)data
, &kq
->kq_sigio
);
798 * MPALMOSTSAFE - acquires mplock
801 kqueue_poll(struct file
*fp
, int events
, struct ucred
*cred
)
803 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
808 if (events
& (POLLIN
| POLLRDNORM
)) {
810 revents
|= events
& (POLLIN
| POLLRDNORM
);
812 selrecord(curthread
, &kq
->kq_sel
);
813 kq
->kq_state
|= KQ_SEL
;
825 kqueue_stat(struct file
*fp
, struct stat
*st
, struct ucred
*cred
)
827 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
829 bzero((void *)st
, sizeof(*st
));
830 st
->st_size
= kq
->kq_count
;
831 st
->st_blksize
= sizeof(struct kevent
);
832 st
->st_mode
= S_IFIFO
;
837 * MPALMOSTSAFE - acquires mplock
840 kqueue_close(struct file
*fp
)
842 struct thread
*td
= curthread
;
843 struct proc
*p
= td
->td_proc
;
844 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
845 struct filedesc
*fdp
;
846 struct knote
**knp
, *kn
, *kn0
;
852 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
853 knp
= &SLIST_FIRST(&fdp
->fd_knlist
[i
]);
856 kn0
= SLIST_NEXT(kn
, kn_link
);
857 if (kq
== kn
->kn_kq
) {
858 kn
->kn_fop
->f_detach(kn
);
863 knp
= &SLIST_NEXT(kn
, kn_link
);
868 if (fdp
->fd_knhashmask
!= 0) {
869 for (i
= 0; i
< fdp
->fd_knhashmask
+ 1; i
++) {
870 knp
= &SLIST_FIRST(&fdp
->fd_knhash
[i
]);
873 kn0
= SLIST_NEXT(kn
, kn_link
);
874 if (kq
== kn
->kn_kq
) {
875 kn
->kn_fop
->f_detach(kn
);
876 /* XXX non-fd release of kn->kn_ptr */
880 knp
= &SLIST_NEXT(kn
, kn_link
);
887 funsetown(kq
->kq_sigio
);
895 kqueue_wakeup(struct kqueue
*kq
)
897 if (kq
->kq_state
& KQ_SLEEP
) {
898 kq
->kq_state
&= ~KQ_SLEEP
;
901 if (kq
->kq_state
& KQ_SEL
) {
902 kq
->kq_state
&= ~KQ_SEL
;
903 selwakeup(&kq
->kq_sel
);
905 KNOTE(&kq
->kq_sel
.si_note
, 0);
909 * walk down a list of knotes, activating them if their event has triggered.
912 knote(struct klist
*list
, long hint
)
916 SLIST_FOREACH(kn
, list
, kn_selnext
)
917 if (kn
->kn_fop
->f_event(kn
, hint
))
922 * remove all knotes from a specified klist
925 knote_remove(struct thread
*td
, struct klist
*list
)
929 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
930 kn
->kn_fop
->f_detach(kn
);
936 * remove all knotes referencing a specified fd
939 knote_fdclose(struct proc
*p
, int fd
)
941 struct filedesc
*fdp
= p
->p_fd
;
942 struct klist
*list
= &fdp
->fd_knlist
[fd
];
943 /* Take any thread of p */
944 struct thread
*td
= FIRST_LWP_IN_PROC(p
)->lwp_thread
;
946 knote_remove(td
, list
);
950 knote_attach(struct knote
*kn
, struct filedesc
*fdp
)
955 if (! kn
->kn_fop
->f_isfd
) {
956 if (fdp
->fd_knhashmask
== 0)
957 fdp
->fd_knhash
= hashinit(KN_HASHSIZE
, M_KQUEUE
,
958 &fdp
->fd_knhashmask
);
959 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
963 if (fdp
->fd_knlistsize
<= kn
->kn_id
) {
964 size
= fdp
->fd_knlistsize
;
965 while (size
<= kn
->kn_id
)
967 MALLOC(list
, struct klist
*,
968 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
969 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
970 fdp
->fd_knlistsize
* sizeof(struct klist
*));
971 bzero((caddr_t
)list
+
972 fdp
->fd_knlistsize
* sizeof(struct klist
*),
973 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
974 if (fdp
->fd_knlist
!= NULL
)
975 FREE(fdp
->fd_knlist
, M_KQUEUE
);
976 fdp
->fd_knlistsize
= size
;
977 fdp
->fd_knlist
= list
;
979 list
= &fdp
->fd_knlist
[kn
->kn_id
];
981 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
986 * should be called outside of a critical section, since we don't want to
987 * hold a critical section while calling fdrop and free.
990 knote_drop(struct knote
*kn
, struct thread
*td
)
992 struct filedesc
*fdp
;
995 KKASSERT(td
->td_proc
);
996 fdp
= td
->td_proc
->p_fd
;
997 if (kn
->kn_fop
->f_isfd
)
998 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1000 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1002 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
1003 if (kn
->kn_status
& KN_QUEUED
)
1005 if (kn
->kn_fop
->f_isfd
)
1012 knote_enqueue(struct knote
*kn
)
1014 struct kqueue
*kq
= kn
->kn_kq
;
1017 KASSERT((kn
->kn_status
& KN_QUEUED
) == 0, ("knote already queued"));
1019 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1020 kn
->kn_status
|= KN_QUEUED
;
1024 * Send SIGIO on request (typically set up as a mailbox signal)
1026 if (kq
->kq_sigio
&& (kq
->kq_state
& KQ_ASYNC
) && kq
->kq_count
== 1)
1027 pgsigio(kq
->kq_sigio
, SIGIO
, 0);
1033 knote_dequeue(struct knote
*kn
)
1035 struct kqueue
*kq
= kn
->kn_kq
;
1037 KASSERT(kn
->kn_status
& KN_QUEUED
, ("knote not queued"));
1040 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1041 kn
->kn_status
&= ~KN_QUEUED
;
1049 knote_zone
= zinit("KNOTE", sizeof(struct knote
), 0, 0, 1);
1051 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
1053 static struct knote
*
1056 return ((struct knote
*)zalloc(knote_zone
));
1060 knote_free(struct knote
*kn
)
1062 zfree(knote_zone
, kn
);