2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)kern_event.c 1.0 (3/31/2000)
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/filedesc.h>
62 #include <sys/kernel.h>
63 #include <sys/proc_internal.h>
64 #include <sys/kauth.h>
65 #include <sys/malloc.h>
66 #include <sys/unistd.h>
67 #include <sys/file_internal.h>
68 #include <sys/fcntl.h>
69 #include <sys/select.h>
70 #include <sys/queue.h>
71 #include <sys/event.h>
72 #include <sys/eventvar.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/sysctl.h>
79 #include <sys/sysproto.h>
83 #include <kern/lock.h>
84 #include <kern/clock.h>
85 #include <kern/thread_call.h>
86 #include <kern/sched_prim.h>
87 #include <kern/zalloc.h>
88 #include <kern/assert.h>
90 #include <libkern/libkern.h>
92 extern void unix_syscall_return(int);
94 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system");
96 static inline void kqlock(struct kqueue
*kq
);
97 static inline void kqunlock(struct kqueue
*kq
);
99 static int kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
);
100 static int kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
);
101 static int kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
);
102 static int knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
);
104 static void kqueue_wakeup(struct kqueue
*kq
);
105 static int kqueue_read(struct fileproc
*fp
, struct uio
*uio
,
106 kauth_cred_t cred
, int flags
, struct proc
*p
);
107 static int kqueue_write(struct fileproc
*fp
, struct uio
*uio
,
108 kauth_cred_t cred
, int flags
, struct proc
*p
);
109 static int kqueue_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
111 static int kqueue_select(struct fileproc
*fp
, int which
, void *wql
,
113 static int kqueue_close(struct fileglob
*fp
, struct proc
*p
);
114 static int kqueue_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
115 extern int kqueue_stat(struct fileproc
*fp
, struct stat
*st
, struct proc
*p
);
117 static struct fileops kqueueops
= {
127 static int kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
);
128 static int kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
);
130 static int kevent_callback(struct kqueue
*kq
, struct kevent
*kevp
, void *data
);
131 static void kevent_continue(struct kqueue
*kq
, void *data
, int error
);
132 static void kevent_scan_continue(void *contp
, wait_result_t wait_result
);
133 static int kevent_process(struct kqueue
*kq
, kevent_callback_t callback
,
134 void *data
, int *countp
, struct proc
*p
);
135 static void knote_put(struct knote
*kn
);
136 static int knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
);
137 static void knote_drop(struct knote
*kn
, struct proc
*p
);
138 static void knote_activate(struct knote
*kn
);
139 static void knote_deactivate(struct knote
*kn
);
140 static void knote_enqueue(struct knote
*kn
);
141 static void knote_dequeue(struct knote
*kn
);
142 static struct knote
*knote_alloc(void);
143 static void knote_free(struct knote
*kn
);
144 extern void knote_init(void);
146 static int filt_fileattach(struct knote
*kn
);
147 static struct filterops file_filtops
=
148 { 1, filt_fileattach
, NULL
, NULL
};
150 static void filt_kqdetach(struct knote
*kn
);
151 static int filt_kqueue(struct knote
*kn
, long hint
);
152 static struct filterops kqread_filtops
=
153 { 1, NULL
, filt_kqdetach
, filt_kqueue
};
156 * placeholder for not-yet-implemented filters
158 static int filt_badattach(struct knote
*kn
);
159 static struct filterops bad_filtops
=
160 { 0, filt_badattach
, 0 , 0 };
162 static int filt_procattach(struct knote
*kn
);
163 static void filt_procdetach(struct knote
*kn
);
164 static int filt_proc(struct knote
*kn
, long hint
);
166 static struct filterops proc_filtops
=
167 { 0, filt_procattach
, filt_procdetach
, filt_proc
};
169 extern struct filterops fs_filtops
;
171 extern struct filterops sig_filtops
;
175 static int filt_timercompute(struct knote
*kn
, uint64_t *abs_time
);
176 static void filt_timerexpire(void *knx
, void *param1
);
177 static int filt_timerattach(struct knote
*kn
);
178 static void filt_timerdetach(struct knote
*kn
);
179 static int filt_timer(struct knote
*kn
, long hint
);
181 static struct filterops timer_filtops
=
182 { 0, filt_timerattach
, filt_timerdetach
, filt_timer
};
184 /* to avoid arming timers that fire quicker than we can handle */
185 static uint64_t filt_timerfloor
= 0;
187 static lck_mtx_t _filt_timerlock
;
188 static void filt_timerlock(void);
189 static void filt_timerunlock(void);
192 * Sentinel marker for a thread scanning through the list of
195 static struct filterops threadmarker_filtops
=
196 { 0, filt_badattach
, 0, 0 };
198 static zone_t knote_zone
;
200 #define KN_HASHSIZE 64 /* XXX should be tunable */
201 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
204 extern struct filterops aio_filtops
;
208 * Table for for all system-defined filters.
210 static struct filterops
*sysfilt_ops
[] = {
211 &file_filtops
, /* EVFILT_READ */
212 &file_filtops
, /* EVFILT_WRITE */
214 &aio_filtops
, /* EVFILT_AIO */
216 &bad_filtops
, /* EVFILT_AIO */
218 &file_filtops
, /* EVFILT_VNODE */
219 &proc_filtops
, /* EVFILT_PROC */
220 &sig_filtops
, /* EVFILT_SIGNAL */
221 &timer_filtops
, /* EVFILT_TIMER */
222 &bad_filtops
, /* EVFILT_MACHPORT */
223 &fs_filtops
/* EVFILT_FS */
227 * kqueue/note lock attributes and implementations
229 * kqueues have locks, while knotes have use counts
230 * Most of the knote state is guarded by the object lock.
231 * the knote "inuse" count and status use the kqueue lock.
233 lck_grp_attr_t
* kq_lck_grp_attr
;
234 lck_grp_t
* kq_lck_grp
;
235 lck_attr_t
* kq_lck_attr
;
238 kqlock(struct kqueue
*kq
)
240 lck_spin_lock(&kq
->kq_lock
);
244 kqunlock(struct kqueue
*kq
)
246 lck_spin_unlock(&kq
->kq_lock
);
250 * Convert a kq lock to a knote use referece.
252 * If the knote is being dropped, we can't get
253 * a use reference, so just return with it
256 * - kq locked at entry
257 * - unlock on exit if we get the use reference
260 kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
)
262 if (kn
->kn_status
& KN_DROPPING
)
270 * Convert a kq lock to a knote use referece.
272 * If the knote is being dropped, we can't get
273 * a use reference, so just return with it
276 * - kq locked at entry
277 * - kq always unlocked on exit
280 kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
)
282 if (!kqlock2knoteuse(kq
, kn
)) {
283 kn
->kn_status
|= KN_DROPWAIT
;
284 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
286 thread_block(THREAD_CONTINUE_NULL
);
293 * Convert from a knote use reference back to kq lock.
295 * Drop a use reference and wake any waiters if
296 * this is the last one.
298 * The exit return indicates if the knote is
299 * still alive - but the kqueue lock is taken
303 knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
)
306 if ((--kn
->kn_inuse
== 0) &&
307 (kn
->kn_status
& KN_USEWAIT
)) {
308 kn
->kn_status
&= ~KN_USEWAIT
;
309 thread_wakeup(&kn
->kn_inuse
);
311 return ((kn
->kn_status
& KN_DROPPING
) == 0);
315 * Convert a kq lock to a knote drop referece.
317 * If the knote is in use, wait for the use count
318 * to subside. We first mark our intention to drop
319 * it - keeping other users from "piling on."
320 * If we are too late, we have to wait for the
321 * other drop to complete.
323 * - kq locked at entry
324 * - always unlocked on exit.
325 * - caller can't hold any locks that would prevent
326 * the other dropper from completing.
329 kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
)
332 if ((kn
->kn_status
& KN_DROPPING
) == 0) {
333 kn
->kn_status
|= KN_DROPPING
;
334 if (kn
->kn_inuse
> 0) {
335 kn
->kn_status
|= KN_USEWAIT
;
336 assert_wait(&kn
->kn_inuse
, THREAD_UNINT
);
338 thread_block(THREAD_CONTINUE_NULL
);
343 kn
->kn_status
|= KN_DROPWAIT
;
344 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
346 thread_block(THREAD_CONTINUE_NULL
);
352 * Release a knote use count reference.
355 knote_put(struct knote
*kn
)
357 struct kqueue
*kq
= kn
->kn_kq
;
360 if ((--kn
->kn_inuse
== 0) &&
361 (kn
->kn_status
& KN_USEWAIT
)) {
362 kn
->kn_status
&= ~KN_USEWAIT
;
363 thread_wakeup(&kn
->kn_inuse
);
371 filt_fileattach(struct knote
*kn
)
374 return (fo_kqfilter(kn
->kn_fp
, kn
, current_proc()));
377 #define f_flag f_fglob->fg_flag
378 #define f_type f_fglob->fg_type
379 #define f_msgcount f_fglob->fg_msgcount
380 #define f_cred f_fglob->fg_cred
381 #define f_ops f_fglob->fg_ops
382 #define f_offset f_fglob->fg_offset
383 #define f_data f_fglob->fg_data
386 filt_kqdetach(struct knote
*kn
)
388 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
391 KNOTE_DETACH(&kq
->kq_sel
.si_note
, kn
);
397 filt_kqueue(struct knote
*kn
, __unused
long hint
)
399 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
401 kn
->kn_data
= kq
->kq_count
;
402 return (kn
->kn_data
> 0);
406 filt_procattach(struct knote
*kn
)
411 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
413 p
= pfind(kn
->kn_id
);
415 thread_funnel_set(kernel_flock
, funnel_state
);
419 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
422 * internal flag indicating registration done by kernel
424 if (kn
->kn_flags
& EV_FLAG1
) {
425 kn
->kn_data
= (int)kn
->kn_sdata
; /* ppid */
426 kn
->kn_fflags
= NOTE_CHILD
;
427 kn
->kn_flags
&= ~EV_FLAG1
;
430 /* XXX lock the proc here while adding to the list? */
431 KNOTE_ATTACH(&p
->p_klist
, kn
);
433 thread_funnel_set(kernel_flock
, funnel_state
);
439 * The knote may be attached to a different process, which may exit,
440 * leaving nothing for the knote to be attached to. So when the process
441 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
442 * it will be deleted when read out. However, as part of the knote deletion,
443 * this routine is called, so a check is needed to avoid actually performing
444 * a detach, because the original process does not exist any more.
447 filt_procdetach(struct knote
*kn
)
452 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
453 p
= pfind(kn
->kn_id
);
455 if (p
!= (struct proc
*)NULL
)
456 KNOTE_DETACH(&p
->p_klist
, kn
);
458 thread_funnel_set(kernel_flock
, funnel_state
);
462 filt_proc(struct knote
*kn
, long hint
)
467 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
470 * mask off extra data
472 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
475 * if the user is interested in this event, record it.
477 if (kn
->kn_sfflags
& event
)
478 kn
->kn_fflags
|= event
;
481 * process is gone, so flag the event as finished.
483 if (event
== NOTE_EXIT
) {
484 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
485 thread_funnel_set(kernel_flock
, funnel_state
);
490 * process forked, and user wants to track the new process,
491 * so attach a new knote to it, and immediately report an
492 * event with the parent's pid.
494 if ((event
== NOTE_FORK
) && (kn
->kn_sfflags
& NOTE_TRACK
)) {
499 * register knote with new process.
501 kev
.ident
= hint
& NOTE_PDATAMASK
; /* pid */
502 kev
.filter
= kn
->kn_filter
;
503 kev
.flags
= kn
->kn_flags
| EV_ADD
| EV_ENABLE
| EV_FLAG1
;
504 kev
.fflags
= kn
->kn_sfflags
;
505 kev
.data
= kn
->kn_id
; /* parent */
506 kev
.udata
= kn
->kn_kevent
.udata
; /* preserve udata */
507 error
= kevent_register(kn
->kn_kq
, &kev
, NULL
);
509 kn
->kn_fflags
|= NOTE_TRACKERR
;
511 event
= kn
->kn_fflags
;
512 thread_funnel_set(kernel_flock
, funnel_state
);
518 * filt_timercompute - compute absolute timeout
520 * The saved-data field in the knote contains the
521 * time value. The saved filter-flags indicates
522 * the unit of measurement.
524 * If the timeout is not absolute, adjust it for
528 filt_timercompute(struct knote
*kn
, uint64_t *abs_time
)
533 switch (kn
->kn_sfflags
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) {
535 multiplier
= NSEC_PER_SEC
;
538 multiplier
= NSEC_PER_USEC
;
543 case 0: /* milliseconds (default) */
544 multiplier
= NSEC_PER_SEC
/ 1000;
549 nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata
* multiplier
, &raw
);
550 if (raw
<= filt_timerfloor
) {
554 if ((kn
->kn_sfflags
& NOTE_ABSOLUTE
) == NOTE_ABSOLUTE
) {
555 uint32_t seconds
, nanoseconds
;
558 clock_get_calendar_nanotime(&seconds
, &nanoseconds
);
559 nanoseconds_to_absolutetime((uint64_t)seconds
* NSEC_PER_SEC
+ nanoseconds
,
561 if (now
>= raw
+ filt_timerfloor
) {
567 clock_absolutetime_interval_to_deadline(raw
, abs_time
);
572 * filt_timerexpire - the timer callout routine
574 * Just propagate the timer event into the knote
575 * filter routine (by going through the knote
576 * synchronization point). Pass a hint to
577 * indicate this is a real event, not just a
581 filt_timerexpire(void *knx
, __unused
void *spare
)
583 struct klist timer_list
;
584 struct knote
*kn
= knx
;
586 /* no "object" for timers, so fake a list */
587 SLIST_INIT(&timer_list
);
588 SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);
589 KNOTE(&timer_list
, 1);
593 * data contains amount of time to sleep, in milliseconds,
594 * or a pointer to a timespec structure.
597 filt_timerattach(struct knote
*kn
)
599 thread_call_t callout
;
603 error
= filt_timercompute(kn
, &deadline
);
608 callout
= thread_call_allocate(filt_timerexpire
, kn
);
612 /* handle as immediate */
618 kn
->kn_hook
= (caddr_t
)callout
;
620 /* absolute=EV_ONESHOT */
621 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
)
622 kn
->kn_flags
|= EV_ONESHOT
;
625 /* all others - if not faking immediate */
626 kn
->kn_flags
|= EV_CLEAR
;
627 thread_call_enter_delayed(callout
, deadline
);
638 filt_timerdetach(struct knote
*kn
)
640 thread_call_t callout
;
643 callout
= (thread_call_t
)kn
->kn_hook
;
644 if (callout
!= NULL
) {
647 /* cancel the callout if we can */
648 cancelled
= thread_call_cancel(callout
);
650 /* got it, just free it */
653 thread_call_free(callout
);
656 /* we have to wait for the expire routine. */
657 kn
->kn_hookid
= -1; /* we are detaching */
658 assert_wait(&kn
->kn_hook
, THREAD_UNINT
);
660 thread_block(THREAD_CONTINUE_NULL
);
661 assert(kn
->kn_hook
== NULL
);
671 filt_timer(struct knote
*kn
, __unused
long hint
)
677 thread_call_t callout
;
684 detaching
= (kn
->kn_hookid
< 0);
685 callout
= (thread_call_t
)kn
->kn_hook
;
687 if (!detaching
&& (kn
->kn_flags
& EV_ONESHOT
) == 0) {
691 /* user input data may have changed - deal */
692 error
= filt_timercompute(kn
, &deadline
);
694 kn
->kn_flags
|= EV_ERROR
;
696 } else if (deadline
== 0) {
697 /* revert to fake immediate */
698 kn
->kn_flags
&= ~EV_CLEAR
;
702 /* keep the callout and re-arm */
703 thread_call_enter_delayed(callout
, deadline
);
710 thread_call_free(callout
);
712 /* if someone is waiting for timer to pop */
714 thread_wakeup(&kn
->kn_hook
);
722 /* change fake timer to real if needed */
723 while (kn
->kn_hookid
> 0 && kn
->kn_sdata
> 0) {
726 /* update the fake timer (make real) */
730 error
= filt_timerattach(kn
);
733 kn
->kn_flags
|= EV_ERROR
;
740 /* if still fake, pretend it fired */
741 if (kn
->kn_hookid
> 0)
744 result
= (kn
->kn_data
!= 0);
752 lck_mtx_lock(&_filt_timerlock
);
756 filt_timerunlock(void)
758 lck_mtx_unlock(&_filt_timerlock
);
762 * JMM - placeholder for not-yet-implemented filters
765 filt_badattach(__unused
struct knote
*kn
)
772 kqueue_alloc(struct proc
*p
)
774 struct filedesc
*fdp
= p
->p_fd
;
777 MALLOC_ZONE(kq
, struct kqueue
*, sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
);
779 bzero(kq
, sizeof(struct kqueue
));
780 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
);
781 TAILQ_INIT(&kq
->kq_head
);
782 TAILQ_INIT(&kq
->kq_inprocess
);
786 if (fdp
->fd_knlistsize
< 0) {
788 if (fdp
->fd_knlistsize
< 0)
789 fdp
->fd_knlistsize
= 0; /* this process has had a kq */
798 * kqueue_dealloc - detach all knotes from a kqueue and free it
800 * We walk each list looking for knotes referencing this
801 * this kqueue. If we find one, we try to drop it. But
802 * if we fail to get a drop reference, that will wait
803 * until it is dropped. So, we can just restart again
804 * safe in the assumption that the list will eventually
805 * not contain any more references to this kqueue (either
806 * we dropped them all, or someone else did).
808 * Assumes no new events are being added to the kqueue.
809 * Nothing locked on entry or exit.
812 kqueue_dealloc(struct kqueue
*kq
, struct proc
*p
)
814 struct filedesc
*fdp
= p
->p_fd
;
819 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
820 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
822 if (kq
== kn
->kn_kq
) {
825 /* drop it ourselves or wait */
826 if (kqlock2knotedrop(kq
, kn
)) {
827 kn
->kn_fop
->f_detach(kn
);
831 /* start over at beginning of list */
832 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
835 kn
= SLIST_NEXT(kn
, kn_link
);
838 if (fdp
->fd_knhashmask
!= 0) {
839 for (i
= 0; i
< (int)fdp
->fd_knhashmask
+ 1; i
++) {
840 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
842 if (kq
== kn
->kn_kq
) {
845 /* drop it ourselves or wait */
846 if (kqlock2knotedrop(kq
, kn
)) {
847 kn
->kn_fop
->f_detach(kn
);
851 /* start over at beginning of list */
852 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
855 kn
= SLIST_NEXT(kn
, kn_link
);
860 lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
);
861 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
865 kqueue(struct proc
*p
, __unused
struct kqueue_args
*uap
, register_t
*retval
)
871 error
= falloc(p
, &fp
, &fd
);
876 kq
= kqueue_alloc(p
);
882 fp
->f_flag
= FREAD
| FWRITE
;
883 fp
->f_type
= DTYPE_KQUEUE
;
884 fp
->f_ops
= &kqueueops
;
885 fp
->f_data
= (caddr_t
)kq
;
888 *fdflags(p
, fd
) &= ~UF_RESERVED
;
889 fp_drop(p
, fd
, fp
, 1);
897 kqueue_portset_np(__unused
struct proc
*p
,
898 __unused
struct kqueue_portset_np_args
*uap
,
899 __unused register_t
*retval
)
901 /* JMM - Placeholder for now */
906 kqueue_from_portset_np(__unused
struct proc
*p
,
907 __unused
struct kqueue_from_portset_np_args
*uap
,
908 __unused register_t
*retval
)
910 /* JMM - Placeholder for now */
915 kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
)
920 if (IS_64BIT_PROCESS(p
)) {
921 struct user_kevent kev64
;
923 advance
= sizeof(kev64
);
924 error
= copyin(*addrp
, (caddr_t
)&kev64
, advance
);
927 kevp
->ident
= CAST_DOWN(uintptr_t, kev64
.ident
);
928 kevp
->filter
= kev64
.filter
;
929 kevp
->flags
= kev64
.flags
;
930 kevp
->fflags
= kev64
.fflags
;
931 kevp
->data
= CAST_DOWN(intptr_t, kev64
.data
);
932 kevp
->udata
= kev64
.udata
;
935 * compensate for legacy in-kernel kevent layout
936 * where the udata field is alredy 64-bit.
938 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
939 error
= copyin(*addrp
, (caddr_t
)kevp
, advance
);
947 kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
)
952 if (IS_64BIT_PROCESS(p
)) {
953 struct user_kevent kev64
;
955 kev64
.ident
= (uint64_t) kevp
->ident
;
956 kev64
.filter
= kevp
->filter
;
957 kev64
.flags
= kevp
->flags
;
958 kev64
.fflags
= kevp
->fflags
;
959 kev64
.data
= (int64_t) kevp
->data
;
960 kev64
.udata
= kevp
->udata
;
961 advance
= sizeof(kev64
);
962 error
= copyout((caddr_t
)&kev64
, *addrp
, advance
);
965 * compensate for legacy in-kernel kevent layout
966 * where the udata field is alredy 64-bit.
968 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
969 error
= copyout((caddr_t
)kevp
, *addrp
, advance
);
977 * kevent_continue - continue a kevent syscall after blocking
979 * assume we inherit a use count on the kq fileglob.
983 kevent_continue(__unused
struct kqueue
*kq
, void *data
, int error
)
985 struct _kevent
*cont_args
;
990 struct proc
*p
= current_proc();
992 cont_args
= (struct _kevent
*)data
;
993 noutputs
= cont_args
->eventout
;
994 retval
= cont_args
->retval
;
998 fp_drop(p
, fd
, fp
, 0);
1000 /* don't restart after signals... */
1001 if (error
== ERESTART
)
1003 else if (error
== EWOULDBLOCK
)
1007 unix_syscall_return(error
);
1011 * kevent - [syscall] register and wait for kernel events
1016 kevent(struct proc
*p
, struct kevent_args
*uap
, register_t
*retval
)
1018 user_addr_t changelist
= uap
->changelist
;
1019 user_addr_t ueventlist
= uap
->eventlist
;
1020 int nchanges
= uap
->nchanges
;
1021 int nevents
= uap
->nevents
;
1024 struct _kevent
*cont_args
;
1027 struct fileproc
*fp
;
1029 int error
, noutputs
;
1032 /* convert timeout to absolute - if we have one */
1033 if (uap
->timeout
!= USER_ADDR_NULL
) {
1035 if ( IS_64BIT_PROCESS(p
) ) {
1036 struct user_timespec ts
;
1037 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1038 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ull
) != 0)
1041 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1044 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1045 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1049 if (itimerfix(&rtv
))
1051 getmicrouptime(&atv
);
1052 timevaladd(&atv
, &rtv
);
1058 /* get a usecount for the kq itself */
1059 if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0)
1062 /* register all the change requests the user provided... */
1064 while (nchanges
> 0 && error
== 0) {
1065 error
= kevent_copyin(&changelist
, &kev
, p
);
1069 kev
.flags
&= ~EV_SYSFLAGS
;
1070 error
= kevent_register(kq
, &kev
, p
);
1071 if (error
&& nevents
> 0) {
1072 kev
.flags
= EV_ERROR
;
1074 error
= kevent_copyout(&kev
, &ueventlist
, p
);
1083 /* store the continuation/completion data in the uthread */
1084 ut
= (uthread_t
)get_bsdthread_info(current_thread());
1085 cont_args
= (struct _kevent
*)&ut
->uu_state
.ss_kevent
;
1088 cont_args
->retval
= retval
;
1089 cont_args
->eventlist
= ueventlist
;
1090 cont_args
->eventcount
= nevents
;
1091 cont_args
->eventout
= noutputs
;
1093 if (nevents
> 0 && noutputs
== 0 && error
== 0)
1094 error
= kevent_scan(kq
, kevent_callback
,
1095 kevent_continue
, cont_args
,
1097 kevent_continue(kq
, cont_args
, error
);
1104 * kevent_callback - callback for each individual event
1106 * called with nothing locked
1107 * caller holds a reference on the kqueue
1111 kevent_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1113 struct _kevent
*cont_args
;
1116 cont_args
= (struct _kevent
*)data
;
1117 assert(cont_args
->eventout
< cont_arg
->eventcount
);
1120 * Copy out the appropriate amount of event data for this user.
1122 error
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc());
1125 * If there isn't space for additional events, return
1126 * a harmless error to stop the processing here
1128 if (error
== 0 && ++cont_args
->eventout
== cont_args
->eventcount
)
1129 error
= EWOULDBLOCK
;
1134 * kevent_register - add a new event to a kqueue
1136 * Creates a mapping between the event source and
1137 * the kqueue via a knote data structure.
1139 * Because many/most the event sources are file
1140 * descriptor related, the knote is linked off
1141 * the filedescriptor table for quick access.
1143 * called with nothing locked
1144 * caller holds a reference on the kqueue
1148 kevent_register(struct kqueue
*kq
, struct kevent
*kev
, struct proc
*p
)
1150 struct filedesc
*fdp
= kq
->kq_fdp
;
1151 struct filterops
*fops
;
1152 struct fileproc
*fp
= NULL
;
1153 struct knote
*kn
= NULL
;
1156 if (kev
->filter
< 0) {
1157 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
1159 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
1163 * filter attach routine is responsible for insuring that
1164 * the identifier can be attached to it.
1166 printf("unknown filter: %d\n", kev
->filter
);
1170 /* this iocount needs to be dropped if it is not registered */
1171 if (fops
->f_isfd
&& (error
= fp_lookup(p
, kev
->ident
, &fp
, 0)) != 0)
1177 /* fd-based knotes are linked off the fd table */
1178 if (kev
->ident
< (u_int
)fdp
->fd_knlistsize
) {
1179 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
1180 if (kq
== kn
->kn_kq
&&
1181 kev
->filter
== kn
->kn_filter
)
1185 /* hash non-fd knotes here too */
1186 if (fdp
->fd_knhashmask
!= 0) {
1189 list
= &fdp
->fd_knhash
[
1190 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
1191 SLIST_FOREACH(kn
, list
, kn_link
)
1192 if (kev
->ident
== kn
->kn_id
&&
1194 kev
->filter
== kn
->kn_filter
)
1200 * kn now contains the matching knote, or NULL if no match
1203 if ((kev
->flags
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) {
1212 kn
->kn_tq
= &kq
->kq_head
;
1214 kn
->kn_sfflags
= kev
->fflags
;
1215 kn
->kn_sdata
= kev
->data
;
1218 kn
->kn_kevent
= *kev
;
1219 kn
->kn_inuse
= 1; /* for f_attach() */
1222 /* before anyone can find it */
1223 if (kev
->flags
& EV_DISABLE
)
1224 kn
->kn_status
|= KN_DISABLED
;
1226 error
= knote_fdpattach(kn
, fdp
, p
);
1235 * apply reference count to knote structure, and
1236 * do not release it at the end of this routine.
1241 * If the attach fails here, we can drop it knowing
1242 * that nobody else has a reference to the knote.
1244 if ((error
= fops
->f_attach(kn
)) != 0) {
1254 /* existing knote - get kqueue lock */
1258 if (kev
->flags
& EV_DELETE
) {
1260 kn
->kn_status
|= KN_DISABLED
;
1261 if (kqlock2knotedrop(kq
, kn
)) {
1262 kn
->kn_fop
->f_detach(kn
);
1268 /* update status flags for existing knote */
1269 if (kev
->flags
& EV_DISABLE
) {
1271 kn
->kn_status
|= KN_DISABLED
;
1272 } else if (kev
->flags
& EV_ENABLE
) {
1273 kn
->kn_status
&= ~KN_DISABLED
;
1274 if (kn
->kn_status
& KN_ACTIVE
)
1279 * If somebody is in the middle of dropping this
1280 * knote - go find/insert a new one. But we have
1281 * wait for this one to go away first.
1283 if (!kqlock2knoteusewait(kq
, kn
))
1284 /* kqueue unlocked */
1288 * The user may change some filter values after the
1289 * initial EV_ADD, but doing so will not reset any
1290 * filter which have already been triggered.
1292 kn
->kn_sfflags
= kev
->fflags
;
1293 kn
->kn_sdata
= kev
->data
;
1294 kn
->kn_kevent
.udata
= kev
->udata
;
1297 /* still have use ref on knote */
1298 if (kn
->kn_fop
->f_event(kn
, 0)) {
1299 if (knoteuse2kqlock(kq
, kn
))
1308 fp_drop(p
, kev
->ident
, fp
, 0);
1313 * kevent_process - process the triggered events in a kqueue
1315 * Walk the queued knotes and validate that they are
1316 * really still triggered events by calling the filter
1317 * routines (if necessary). Hold a use reference on
1318 * the knote to avoid it being detached. For each event
1319 * that is still considered triggered, invoke the
1320 * callback routine provided.
1322 * caller holds a reference on the kqueue.
1323 * kqueue locked on entry and exit - but may be dropped
1327 kevent_process(struct kqueue
*kq
,
1328 kevent_callback_t callback
,
1339 if (kq
->kq_count
== 0) {
1344 /* if someone else is processing the queue, wait */
1345 if (!TAILQ_EMPTY(&kq
->kq_inprocess
)) {
1346 assert_wait(&kq
->kq_inprocess
, THREAD_UNINT
);
1347 kq
->kq_state
|= KQ_PROCWAIT
;
1349 thread_block(THREAD_CONTINUE_NULL
);
1356 while (error
== 0 &&
1357 (kn
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
1360 * move knote to the processed queue.
1361 * this is also protected by the kq lock.
1363 assert(kn
->kn_tq
== &kq
->kq_head
);
1364 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1365 kn
->kn_tq
= &kq
->kq_inprocess
;
1366 TAILQ_INSERT_TAIL(&kq
->kq_inprocess
, kn
, kn_tqe
);
1369 * Non-EV_ONESHOT events must be re-validated.
1371 * Convert our lock to a use-count and call the event's
1372 * filter routine to update.
1374 * If the event is dropping (or no longer valid), we
1375 * already have it off the active queue, so just
1376 * finish the job of deactivating it.
1378 if ((kn
->kn_flags
& EV_ONESHOT
) == 0) {
1381 if (kqlock2knoteuse(kq
, kn
)) {
1383 /* call the filter with just a ref */
1384 result
= kn
->kn_fop
->f_event(kn
, 0);
1386 if (!knoteuse2kqlock(kq
, kn
) || result
== 0) {
1387 knote_deactivate(kn
);
1391 knote_deactivate(kn
);
1397 * Got a valid triggered knote with the kqueue
1398 * still locked. Snapshot the data, and determine
1399 * how to dispatch the knote for future events.
1401 kev
= kn
->kn_kevent
;
1403 /* now what happens to it? */
1404 if (kn
->kn_flags
& EV_ONESHOT
) {
1405 knote_deactivate(kn
);
1406 if (kqlock2knotedrop(kq
, kn
)) {
1407 kn
->kn_fop
->f_detach(kn
);
1410 } else if (kn
->kn_flags
& EV_CLEAR
) {
1411 knote_deactivate(kn
);
1417 * leave on in-process queue. We'll
1418 * move all the remaining ones back
1419 * the kq queue and wakeup any
1420 * waiters when we are done.
1425 /* callback to handle each event as we find it */
1426 error
= (callback
)(kq
, &kev
, data
);
1433 * With the kqueue still locked, move any knotes
1434 * remaining on the in-process queue back to the
1435 * kq's queue and wake up any waiters.
1437 while ((kn
= TAILQ_FIRST(&kq
->kq_inprocess
)) != NULL
) {
1438 assert(kn
->kn_tq
== &kq
->kq_inprocess
);
1439 TAILQ_REMOVE(&kq
->kq_inprocess
, kn
, kn_tqe
);
1440 kn
->kn_tq
= &kq
->kq_head
;
1441 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1443 if (kq
->kq_state
& KQ_PROCWAIT
) {
1444 kq
->kq_state
&= ~KQ_PROCWAIT
;
1445 thread_wakeup(&kq
->kq_inprocess
);
1454 kevent_scan_continue(void *data
, wait_result_t wait_result
)
1456 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1457 struct _kevent_scan
* cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1458 struct kqueue
*kq
= (struct kqueue
*)data
;
1462 /* convert the (previous) wait_result to a proper error */
1463 switch (wait_result
) {
1464 case THREAD_AWAKENED
:
1466 error
= kevent_process(kq
, cont_args
->call
, cont_args
, &count
, current_proc());
1467 if (error
== 0 && count
== 0) {
1468 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, cont_args
->deadline
);
1469 kq
->kq_state
|= KQ_SLEEP
;
1471 thread_block_parameter(kevent_scan_continue
, kq
);
1476 case THREAD_TIMED_OUT
:
1477 error
= EWOULDBLOCK
;
1479 case THREAD_INTERRUPTED
:
1483 panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result
);
1487 /* call the continuation with the results */
1488 assert(cont_args
->cont
!= NULL
);
1489 (cont_args
->cont
)(kq
, cont_args
->data
, error
);
1494 * kevent_scan - scan and wait for events in a kqueue
1496 * Process the triggered events in a kqueue.
1498 * If there are no events triggered arrange to
1499 * wait for them. If the caller provided a
1500 * continuation routine, then kevent_scan will
1503 * The callback routine must be valid.
1504 * The caller must hold a use-count reference on the kq.
1508 kevent_scan(struct kqueue
*kq
,
1509 kevent_callback_t callback
,
1510 kevent_continue_t continuation
,
1512 struct timeval
*atvp
,
1515 thread_continue_t cont
= THREAD_CONTINUE_NULL
;
1520 assert(callback
!= NULL
);
1524 wait_result_t wait_result
;
1528 * Make a pass through the kq to find events already
1532 error
= kevent_process(kq
, callback
, data
, &count
, p
);
1534 break; /* lock still held */
1536 /* looks like we have to consider blocking */
1539 /* convert the timeout to a deadline once */
1540 if (atvp
->tv_sec
|| atvp
->tv_usec
) {
1541 uint32_t seconds
, nanoseconds
;
1544 clock_get_uptime(&now
);
1545 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec
* NSEC_PER_SEC
+
1546 atvp
->tv_usec
* NSEC_PER_USEC
,
1548 if (now
>= deadline
) {
1549 /* non-blocking call */
1550 error
= EWOULDBLOCK
;
1551 break; /* lock still held */
1554 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
1556 deadline
= 0; /* block forever */
1560 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1561 struct _kevent_scan
*cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1563 cont_args
->call
= callback
;
1564 cont_args
->cont
= continuation
;
1565 cont_args
->deadline
= deadline
;
1566 cont_args
->data
= data
;
1567 cont
= kevent_scan_continue
;
1571 /* go ahead and wait */
1572 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, deadline
);
1573 kq
->kq_state
|= KQ_SLEEP
;
1575 wait_result
= thread_block_parameter(cont
, kq
);
1576 /* NOTREACHED if (continuation != NULL) */
1578 switch (wait_result
) {
1579 case THREAD_AWAKENED
:
1581 case THREAD_TIMED_OUT
:
1583 case THREAD_INTERRUPTED
:
1586 panic("kevent_scan - bad wait_result (%d)",
1598 * This could be expanded to call kqueue_scan, if desired.
1602 kqueue_read(__unused
struct fileproc
*fp
,
1603 __unused
struct uio
*uio
,
1604 __unused kauth_cred_t cred
,
1606 __unused
struct proc
*p
)
1613 kqueue_write(__unused
struct fileproc
*fp
,
1614 __unused
struct uio
*uio
,
1615 __unused kauth_cred_t cred
,
1617 __unused
struct proc
*p
)
1624 kqueue_ioctl(__unused
struct fileproc
*fp
,
1625 __unused u_long com
,
1626 __unused caddr_t data
,
1627 __unused
struct proc
*p
)
1634 kqueue_select(struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
)
1636 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1639 if (which
== FREAD
) {
1644 selrecord(p
, &kq
->kq_sel
, wql
);
1645 kq
->kq_state
|= KQ_SEL
;
1657 kqueue_close(struct fileglob
*fg
, struct proc
*p
)
1659 struct kqueue
*kq
= (struct kqueue
*)fg
->fg_data
;
1661 kqueue_dealloc(kq
, p
);
1668 * The callers has taken a use-count reference on this kqueue and will donate it
1669 * to the kqueue we are being added to. This keeps the kqueue from closing until
1670 * that relationship is torn down.
1673 kqueue_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
1675 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
1677 if (kn
->kn_filter
!= EVFILT_READ
)
1680 kn
->kn_fop
= &kqread_filtops
;
1682 KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
);
1689 kqueue_stat(struct fileproc
*fp
, struct stat
*st
, __unused
struct proc
*p
)
1691 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1693 bzero((void *)st
, sizeof(*st
));
1694 st
->st_size
= kq
->kq_count
;
1695 st
->st_blksize
= sizeof(struct kevent
);
1696 st
->st_mode
= S_IFIFO
;
1701 * Called with the kqueue locked
1704 kqueue_wakeup(struct kqueue
*kq
)
1707 if (kq
->kq_state
& KQ_SLEEP
) {
1708 kq
->kq_state
&= ~KQ_SLEEP
;
1711 if (kq
->kq_state
& KQ_SEL
) {
1712 kq
->kq_state
&= ~KQ_SEL
;
1713 selwakeup(&kq
->kq_sel
);
1715 KNOTE(&kq
->kq_sel
.si_note
, 0);
1719 klist_init(struct klist
*list
)
1726 * Query/Post each knote in the object's list
1728 * The object lock protects the list. It is assumed
1729 * that the filter/event routine for the object can
1730 * determine that the object is already locked (via
1731 * the hind) and not deadlock itself.
1733 * The object lock should also hold off pending
1734 * detach/drop operations. But we'll prevent it here
1735 * too - just in case.
1738 knote(struct klist
*list
, long hint
)
1742 SLIST_FOREACH(kn
, list
, kn_selnext
) {
1743 struct kqueue
*kq
= kn
->kn_kq
;
1746 if (kqlock2knoteuse(kq
, kn
)) {
1749 /* call the event with only a use count */
1750 result
= kn
->kn_fop
->f_event(kn
, hint
);
1752 /* if its not going away and triggered */
1753 if (knoteuse2kqlock(kq
, kn
) && result
)
1755 /* lock held again */
1762 * attach a knote to the specified list. Return true if this is the first entry.
1763 * The list is protected by whatever lock the object it is associated with uses.
1766 knote_attach(struct klist
*list
, struct knote
*kn
)
1768 int ret
= SLIST_EMPTY(list
);
1769 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
1774 * detach a knote from the specified list. Return true if that was the last entry.
1775 * The list is protected by whatever lock the object it is associated with uses.
1778 knote_detach(struct klist
*list
, struct knote
*kn
)
1780 SLIST_REMOVE(list
, kn
, knote
, kn_selnext
);
1781 return SLIST_EMPTY(list
);
1785 * remove all knotes referencing a specified fd
1787 * Essentially an inlined knote_remove & knote_drop
1788 * when we know for sure that the thing is a file
1790 * Entered with the proc_fd lock already held.
1791 * It returns the same way, but may drop it temporarily.
1794 knote_fdclose(struct proc
*p
, int fd
)
1796 struct filedesc
*fdp
= p
->p_fd
;
1800 list
= &fdp
->fd_knlist
[fd
];
1801 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
1802 struct kqueue
*kq
= kn
->kn_kq
;
1808 * Convert the lock to a drop ref.
1809 * If we get it, go ahead and drop it.
1810 * Otherwise, we waited for it to
1811 * be dropped by the other guy, so
1812 * it is safe to move on in the list.
1814 if (kqlock2knotedrop(kq
, kn
)) {
1815 kn
->kn_fop
->f_detach(kn
);
1821 /* the fd tables may have changed - start over */
1822 list
= &fdp
->fd_knlist
[fd
];
1826 /* proc_fdlock held on entry (and exit) */
1828 knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, __unused
struct proc
*p
)
1830 struct klist
*list
= NULL
;
1832 if (! kn
->kn_fop
->f_isfd
) {
1833 if (fdp
->fd_knhashmask
== 0)
1834 fdp
->fd_knhash
= hashinit(KN_HASHSIZE
, M_KQUEUE
,
1835 &fdp
->fd_knhashmask
);
1836 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1838 if ((u_int
)fdp
->fd_knlistsize
<= kn
->kn_id
) {
1841 /* have to grow the fd_knlist */
1842 size
= fdp
->fd_knlistsize
;
1843 while (size
<= kn
->kn_id
)
1845 MALLOC(list
, struct klist
*,
1846 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
1850 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
1851 fdp
->fd_knlistsize
* sizeof(struct klist
*));
1852 bzero((caddr_t
)list
+
1853 fdp
->fd_knlistsize
* sizeof(struct klist
*),
1854 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
1855 FREE(fdp
->fd_knlist
, M_KQUEUE
);
1856 fdp
->fd_knlist
= list
;
1857 fdp
->fd_knlistsize
= size
;
1859 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1861 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
1868 * should be called at spl == 0, since we don't want to hold spl
1869 * while calling fdrop and free.
1872 knote_drop(struct knote
*kn
, struct proc
*p
)
1874 struct filedesc
*fdp
= p
->p_fd
;
1875 struct kqueue
*kq
= kn
->kn_kq
;
1879 if (kn
->kn_fop
->f_isfd
)
1880 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1882 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1884 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
1887 if (kn
->kn_status
& KN_DROPWAIT
)
1888 thread_wakeup(&kn
->kn_status
);
1892 if (kn
->kn_fop
->f_isfd
)
1893 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0);
1898 /* called with kqueue lock held */
1900 knote_activate(struct knote
*kn
)
1902 struct kqueue
*kq
= kn
->kn_kq
;
1904 kn
->kn_status
|= KN_ACTIVE
;
1909 /* called with kqueue lock held */
1911 knote_deactivate(struct knote
*kn
)
1913 kn
->kn_status
&= ~KN_ACTIVE
;
1917 /* called with kqueue lock held */
1919 knote_enqueue(struct knote
*kn
)
1921 struct kqueue
*kq
= kn
->kn_kq
;
1923 if ((kn
->kn_status
& (KN_QUEUED
| KN_DISABLED
)) == 0) {
1924 struct kqtailq
*tq
= kn
->kn_tq
;
1926 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);
1927 kn
->kn_status
|= KN_QUEUED
;
1932 /* called with kqueue lock held */
1934 knote_dequeue(struct knote
*kn
)
1936 struct kqueue
*kq
= kn
->kn_kq
;
1938 assert((kn
->kn_status
& KN_DISABLED
) == 0);
1939 if ((kn
->kn_status
& KN_QUEUED
) == KN_QUEUED
) {
1940 struct kqtailq
*tq
= kn
->kn_tq
;
1942 TAILQ_REMOVE(tq
, kn
, kn_tqe
);
1943 kn
->kn_tq
= &kq
->kq_head
;
1944 kn
->kn_status
&= ~KN_QUEUED
;
1952 knote_zone
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
), 8192, "knote zone");
1954 /* allocate kq lock group attribute and group */
1955 kq_lck_grp_attr
= lck_grp_attr_alloc_init();
1956 lck_grp_attr_setstat(kq_lck_grp_attr
);
1958 kq_lck_grp
= lck_grp_alloc_init("kqueue", kq_lck_grp_attr
);
1960 /* Allocate kq lock attribute */
1961 kq_lck_attr
= lck_attr_alloc_init();
1962 lck_attr_setdefault(kq_lck_attr
);
1964 /* Initialize the timer filter lock */
1965 lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
);
1967 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
1969 static struct knote
*
1972 return ((struct knote
*)zalloc(knote_zone
));
1976 knote_free(struct knote
*kn
)
1978 zfree(knote_zone
, kn
);
1981 #include <sys/param.h>
1982 #include <sys/socket.h>
1983 #include <sys/protosw.h>
1984 #include <sys/domain.h>
1985 #include <sys/mbuf.h>
1986 #include <sys/kern_event.h>
1987 #include <sys/malloc.h>
1988 #include <sys/sys_domain.h>
1989 #include <sys/syslog.h>
1992 static int kev_attach(struct socket
*so
, int proto
, struct proc
*p
);
1993 static int kev_detach(struct socket
*so
);
1994 static int kev_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
);
1996 struct pr_usrreqs event_usrreqs
= {
1997 pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
,
1998 pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
,
1999 pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
2000 pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
2001 pru_sosend_notsupp
, soreceive
, pru_sopoll_notsupp
2004 struct protosw eventsw
[] = {
2006 SOCK_RAW
, &systemdomain
, SYSPROTO_EVENT
, PR_ATOMIC
,
2022 struct kern_event_head kern_event_head
;
2024 static u_long static_event_id
= 0;
2025 struct domain
*sysdom
= &systemdomain
;
2027 static lck_grp_t
*evt_mtx_grp
;
2028 static lck_attr_t
*evt_mtx_attr
;
2029 static lck_grp_attr_t
*evt_mtx_grp_attr
;
2030 lck_mtx_t
*evt_mutex
;
2032 * Install the protosw's for the NKE manager. Invoked at
2033 * extension load time
2036 kern_event_init(void)
2040 if ((retval
= net_add_proto(eventsw
, &systemdomain
)) != 0) {
2041 log(LOG_WARNING
, "Can't install kernel events protocol (%d)\n", retval
);
2046 * allocate lock group attribute and group for kern event
2048 evt_mtx_grp_attr
= lck_grp_attr_alloc_init();
2050 evt_mtx_grp
= lck_grp_alloc_init("eventlist", evt_mtx_grp_attr
);
2053 * allocate the lock attribute for mutexes
2055 evt_mtx_attr
= lck_attr_alloc_init();
2056 lck_attr_setdefault(evt_mtx_attr
);
2057 evt_mutex
= lck_mtx_alloc_init(evt_mtx_grp
, evt_mtx_attr
);
2058 if (evt_mutex
== NULL
)
2061 return(KERN_SUCCESS
);
2065 kev_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
2068 struct kern_event_pcb
*ev_pcb
;
2070 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
2074 MALLOC(ev_pcb
, struct kern_event_pcb
*, sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
);
2078 ev_pcb
->ev_socket
= so
;
2079 ev_pcb
->vendor_code_filter
= 0xffffffff;
2081 so
->so_pcb
= (caddr_t
) ev_pcb
;
2082 lck_mtx_lock(evt_mutex
);
2083 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
);
2084 lck_mtx_unlock(evt_mutex
);
2091 kev_detach(struct socket
*so
)
2093 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2096 lck_mtx_lock(evt_mutex
);
2097 LIST_REMOVE(ev_pcb
, ev_link
);
2098 lck_mtx_unlock(evt_mutex
);
2099 FREE(ev_pcb
, M_PCB
);
2101 so
->so_flags
|= SOF_PCBCLEARING
;
2108 * For now, kev_vender_code and mbuf_tags use the same
2111 extern errno_t
mbuf_tag_id_find_internal(const char *string
, u_long
*out_id
,
2114 errno_t
kev_vendor_code_find(
2116 u_long
*out_vender_code
)
2118 if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) {
2121 return mbuf_tag_id_find_internal(string
, out_vender_code
, 1);
2124 extern void mbuf_tag_id_first_last(u_long
*first
, u_long
*last
);
2126 errno_t
kev_msg_post(struct kev_msg
*event_msg
)
2128 u_long min_vendor
, max_vendor
;
2130 mbuf_tag_id_first_last(&min_vendor
, &max_vendor
);
2132 if (event_msg
== NULL
)
2135 /* Limit third parties to posting events for registered vendor codes only */
2136 if (event_msg
->vendor_code
< min_vendor
||
2137 event_msg
->vendor_code
> max_vendor
)
2142 return kev_post_msg(event_msg
);
2146 int kev_post_msg(struct kev_msg
*event_msg
)
2148 struct mbuf
*m
, *m2
;
2149 struct kern_event_pcb
*ev_pcb
;
2150 struct kern_event_msg
*ev
;
2152 unsigned long total_size
;
2155 /* Verify the message is small enough to fit in one mbuf w/o cluster */
2156 total_size
= KEV_MSG_HEADER_SIZE
;
2158 for (i
= 0; i
< 5; i
++) {
2159 if (event_msg
->dv
[i
].data_length
== 0)
2161 total_size
+= event_msg
->dv
[i
].data_length
;
2164 if (total_size
> MLEN
) {
2168 m
= m_get(M_DONTWAIT
, MT_DATA
);
2172 ev
= mtod(m
, struct kern_event_msg
*);
2173 total_size
= KEV_MSG_HEADER_SIZE
;
2175 tmp
= (char *) &ev
->event_data
[0];
2176 for (i
= 0; i
< 5; i
++) {
2177 if (event_msg
->dv
[i
].data_length
== 0)
2180 total_size
+= event_msg
->dv
[i
].data_length
;
2181 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
2182 event_msg
->dv
[i
].data_length
);
2183 tmp
+= event_msg
->dv
[i
].data_length
;
2186 ev
->id
= ++static_event_id
;
2187 ev
->total_size
= total_size
;
2188 ev
->vendor_code
= event_msg
->vendor_code
;
2189 ev
->kev_class
= event_msg
->kev_class
;
2190 ev
->kev_subclass
= event_msg
->kev_subclass
;
2191 ev
->event_code
= event_msg
->event_code
;
2193 m
->m_len
= total_size
;
2194 lck_mtx_lock(evt_mutex
);
2195 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
2197 ev_pcb
= LIST_NEXT(ev_pcb
, ev_link
)) {
2199 if (ev_pcb
->vendor_code_filter
!= KEV_ANY_VENDOR
) {
2200 if (ev_pcb
->vendor_code_filter
!= ev
->vendor_code
)
2203 if (ev_pcb
->class_filter
!= KEV_ANY_CLASS
) {
2204 if (ev_pcb
->class_filter
!= ev
->kev_class
)
2207 if ((ev_pcb
->subclass_filter
!= KEV_ANY_SUBCLASS
) &&
2208 (ev_pcb
->subclass_filter
!= ev
->kev_subclass
))
2213 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
2216 lck_mtx_unlock(evt_mutex
);
2219 socket_lock(ev_pcb
->ev_socket
, 1);
2220 if (sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
))
2221 sorwakeup(ev_pcb
->ev_socket
);
2222 socket_unlock(ev_pcb
->ev_socket
, 1);
2226 lck_mtx_unlock(evt_mutex
);
2231 kev_control(struct socket
*so
,
2234 __unused
struct ifnet
*ifp
,
2235 __unused
struct proc
*p
)
2237 struct kev_request
*kev_req
= (struct kev_request
*) data
;
2238 struct kern_event_pcb
*ev_pcb
;
2239 struct kev_vendor_code
*kev_vendor
;
2240 u_long
*id_value
= (u_long
*) data
;
2246 *id_value
= static_event_id
;
2250 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2251 ev_pcb
->vendor_code_filter
= kev_req
->vendor_code
;
2252 ev_pcb
->class_filter
= kev_req
->kev_class
;
2253 ev_pcb
->subclass_filter
= kev_req
->kev_subclass
;
2257 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2258 kev_req
->vendor_code
= ev_pcb
->vendor_code_filter
;
2259 kev_req
->kev_class
= ev_pcb
->class_filter
;
2260 kev_req
->kev_subclass
= ev_pcb
->subclass_filter
;
2263 case SIOCGKEVVENDOR
:
2264 kev_vendor
= (struct kev_vendor_code
*)data
;
2266 /* Make sure string is NULL terminated */
2267 kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0;
2269 return mbuf_tag_id_find_internal(kev_vendor
->vendor_string
,
2270 &kev_vendor
->vendor_code
, 0);