2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)kern_event.c 1.0 (3/31/2000)
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/filedesc.h>
64 #include <sys/kernel.h>
65 #include <sys/proc_internal.h>
66 #include <sys/kauth.h>
67 #include <sys/malloc.h>
68 #include <sys/unistd.h>
69 #include <sys/file_internal.h>
70 #include <sys/fcntl.h>
71 #include <sys/select.h>
72 #include <sys/queue.h>
73 #include <sys/event.h>
74 #include <sys/eventvar.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
79 #include <sys/sysctl.h>
81 #include <sys/sysproto.h>
85 #include <kern/lock.h>
86 #include <kern/clock.h>
87 #include <kern/thread_call.h>
88 #include <kern/sched_prim.h>
89 #include <kern/zalloc.h>
90 #include <kern/assert.h>
92 #include <libkern/libkern.h>
94 extern void unix_syscall_return(int);
96 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system");
98 static inline void kqlock(struct kqueue
*kq
);
99 static inline void kqunlock(struct kqueue
*kq
);
101 static int kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
);
102 static int kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
);
103 static int kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
);
104 static int knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
);
106 static void kqueue_wakeup(struct kqueue
*kq
);
107 static int kqueue_read(struct fileproc
*fp
, struct uio
*uio
,
108 kauth_cred_t cred
, int flags
, struct proc
*p
);
109 static int kqueue_write(struct fileproc
*fp
, struct uio
*uio
,
110 kauth_cred_t cred
, int flags
, struct proc
*p
);
111 static int kqueue_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
113 static int kqueue_select(struct fileproc
*fp
, int which
, void *wql
,
115 static int kqueue_close(struct fileglob
*fp
, struct proc
*p
);
116 static int kqueue_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
117 extern int kqueue_stat(struct fileproc
*fp
, struct stat
*st
, struct proc
*p
);
119 static struct fileops kqueueops
= {
129 static int kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
);
130 static int kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
);
132 static int kevent_callback(struct kqueue
*kq
, struct kevent
*kevp
, void *data
);
133 static void kevent_continue(struct kqueue
*kq
, void *data
, int error
);
134 static void kevent_scan_continue(void *contp
, wait_result_t wait_result
);
135 static int kevent_process(struct kqueue
*kq
, kevent_callback_t callback
,
136 void *data
, int *countp
, struct proc
*p
);
137 static void knote_put(struct knote
*kn
);
138 static int knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
);
139 static void knote_drop(struct knote
*kn
, struct proc
*p
);
140 static void knote_activate(struct knote
*kn
);
141 static void knote_deactivate(struct knote
*kn
);
142 static void knote_enqueue(struct knote
*kn
);
143 static void knote_dequeue(struct knote
*kn
);
144 static struct knote
*knote_alloc(void);
145 static void knote_free(struct knote
*kn
);
146 extern void knote_init(void);
148 static int filt_fileattach(struct knote
*kn
);
149 static struct filterops file_filtops
=
150 { 1, filt_fileattach
, NULL
, NULL
};
152 static void filt_kqdetach(struct knote
*kn
);
153 static int filt_kqueue(struct knote
*kn
, long hint
);
154 static struct filterops kqread_filtops
=
155 { 1, NULL
, filt_kqdetach
, filt_kqueue
};
158 * placeholder for not-yet-implemented filters
160 static int filt_badattach(struct knote
*kn
);
161 static struct filterops bad_filtops
=
162 { 0, filt_badattach
, 0 , 0 };
164 static int filt_procattach(struct knote
*kn
);
165 static void filt_procdetach(struct knote
*kn
);
166 static int filt_proc(struct knote
*kn
, long hint
);
168 static struct filterops proc_filtops
=
169 { 0, filt_procattach
, filt_procdetach
, filt_proc
};
171 extern struct filterops fs_filtops
;
173 extern struct filterops sig_filtops
;
177 static int filt_timercompute(struct knote
*kn
, uint64_t *abs_time
);
178 static void filt_timerexpire(void *knx
, void *param1
);
179 static int filt_timerattach(struct knote
*kn
);
180 static void filt_timerdetach(struct knote
*kn
);
181 static int filt_timer(struct knote
*kn
, long hint
);
183 static struct filterops timer_filtops
=
184 { 0, filt_timerattach
, filt_timerdetach
, filt_timer
};
186 /* to avoid arming timers that fire quicker than we can handle */
187 static uint64_t filt_timerfloor
= 0;
189 static lck_mtx_t _filt_timerlock
;
190 static void filt_timerlock(void);
191 static void filt_timerunlock(void);
194 * Sentinel marker for a thread scanning through the list of
197 static struct filterops threadmarker_filtops
=
198 { 0, filt_badattach
, 0, 0 };
200 static zone_t knote_zone
;
202 #define KN_HASHSIZE 64 /* XXX should be tunable */
203 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
206 extern struct filterops aio_filtops
;
210 * Table for for all system-defined filters.
212 static struct filterops
*sysfilt_ops
[] = {
213 &file_filtops
, /* EVFILT_READ */
214 &file_filtops
, /* EVFILT_WRITE */
216 &aio_filtops
, /* EVFILT_AIO */
218 &bad_filtops
, /* EVFILT_AIO */
220 &file_filtops
, /* EVFILT_VNODE */
221 &proc_filtops
, /* EVFILT_PROC */
222 &sig_filtops
, /* EVFILT_SIGNAL */
223 &timer_filtops
, /* EVFILT_TIMER */
224 &bad_filtops
, /* EVFILT_MACHPORT */
225 &fs_filtops
/* EVFILT_FS */
229 * kqueue/note lock attributes and implementations
231 * kqueues have locks, while knotes have use counts
232 * Most of the knote state is guarded by the object lock.
233 * the knote "inuse" count and status use the kqueue lock.
235 lck_grp_attr_t
* kq_lck_grp_attr
;
236 lck_grp_t
* kq_lck_grp
;
237 lck_attr_t
* kq_lck_attr
;
240 kqlock(struct kqueue
*kq
)
242 lck_spin_lock(&kq
->kq_lock
);
246 kqunlock(struct kqueue
*kq
)
248 lck_spin_unlock(&kq
->kq_lock
);
252 * Convert a kq lock to a knote use referece.
254 * If the knote is being dropped, we can't get
255 * a use reference, so just return with it
258 * - kq locked at entry
259 * - unlock on exit if we get the use reference
262 kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
)
264 if (kn
->kn_status
& KN_DROPPING
)
272 * Convert a kq lock to a knote use referece.
274 * If the knote is being dropped, we can't get
275 * a use reference, so just return with it
278 * - kq locked at entry
279 * - kq always unlocked on exit
282 kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
)
284 if (!kqlock2knoteuse(kq
, kn
)) {
285 kn
->kn_status
|= KN_DROPWAIT
;
286 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
288 thread_block(THREAD_CONTINUE_NULL
);
295 * Convert from a knote use reference back to kq lock.
297 * Drop a use reference and wake any waiters if
298 * this is the last one.
300 * The exit return indicates if the knote is
301 * still alive - but the kqueue lock is taken
305 knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
)
308 if ((--kn
->kn_inuse
== 0) &&
309 (kn
->kn_status
& KN_USEWAIT
)) {
310 kn
->kn_status
&= ~KN_USEWAIT
;
311 thread_wakeup(&kn
->kn_inuse
);
313 return ((kn
->kn_status
& KN_DROPPING
) == 0);
317 * Convert a kq lock to a knote drop referece.
319 * If the knote is in use, wait for the use count
320 * to subside. We first mark our intention to drop
321 * it - keeping other users from "piling on."
322 * If we are too late, we have to wait for the
323 * other drop to complete.
325 * - kq locked at entry
326 * - always unlocked on exit.
327 * - caller can't hold any locks that would prevent
328 * the other dropper from completing.
331 kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
)
334 if ((kn
->kn_status
& KN_DROPPING
) == 0) {
335 kn
->kn_status
|= KN_DROPPING
;
336 if (kn
->kn_inuse
> 0) {
337 kn
->kn_status
|= KN_USEWAIT
;
338 assert_wait(&kn
->kn_inuse
, THREAD_UNINT
);
340 thread_block(THREAD_CONTINUE_NULL
);
345 kn
->kn_status
|= KN_DROPWAIT
;
346 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
348 thread_block(THREAD_CONTINUE_NULL
);
354 * Release a knote use count reference.
357 knote_put(struct knote
*kn
)
359 struct kqueue
*kq
= kn
->kn_kq
;
362 if ((--kn
->kn_inuse
== 0) &&
363 (kn
->kn_status
& KN_USEWAIT
)) {
364 kn
->kn_status
&= ~KN_USEWAIT
;
365 thread_wakeup(&kn
->kn_inuse
);
373 filt_fileattach(struct knote
*kn
)
376 return (fo_kqfilter(kn
->kn_fp
, kn
, current_proc()));
379 #define f_flag f_fglob->fg_flag
380 #define f_type f_fglob->fg_type
381 #define f_msgcount f_fglob->fg_msgcount
382 #define f_cred f_fglob->fg_cred
383 #define f_ops f_fglob->fg_ops
384 #define f_offset f_fglob->fg_offset
385 #define f_data f_fglob->fg_data
388 filt_kqdetach(struct knote
*kn
)
390 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
393 KNOTE_DETACH(&kq
->kq_sel
.si_note
, kn
);
399 filt_kqueue(struct knote
*kn
, __unused
long hint
)
401 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
403 kn
->kn_data
= kq
->kq_count
;
404 return (kn
->kn_data
> 0);
408 filt_procattach(struct knote
*kn
)
413 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
415 p
= pfind(kn
->kn_id
);
417 thread_funnel_set(kernel_flock
, funnel_state
);
421 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
424 * internal flag indicating registration done by kernel
426 if (kn
->kn_flags
& EV_FLAG1
) {
427 kn
->kn_data
= (int)kn
->kn_sdata
; /* ppid */
428 kn
->kn_fflags
= NOTE_CHILD
;
429 kn
->kn_flags
&= ~EV_FLAG1
;
432 /* XXX lock the proc here while adding to the list? */
433 KNOTE_ATTACH(&p
->p_klist
, kn
);
435 thread_funnel_set(kernel_flock
, funnel_state
);
441 * The knote may be attached to a different process, which may exit,
442 * leaving nothing for the knote to be attached to. So when the process
443 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
444 * it will be deleted when read out. However, as part of the knote deletion,
445 * this routine is called, so a check is needed to avoid actually performing
446 * a detach, because the original process does not exist any more.
449 filt_procdetach(struct knote
*kn
)
454 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
455 p
= pfind(kn
->kn_id
);
457 if (p
!= (struct proc
*)NULL
)
458 KNOTE_DETACH(&p
->p_klist
, kn
);
460 thread_funnel_set(kernel_flock
, funnel_state
);
464 filt_proc(struct knote
*kn
, long hint
)
469 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
472 * mask off extra data
474 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
477 * if the user is interested in this event, record it.
479 if (kn
->kn_sfflags
& event
)
480 kn
->kn_fflags
|= event
;
483 * process is gone, so flag the event as finished.
485 if (event
== NOTE_EXIT
) {
486 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
487 thread_funnel_set(kernel_flock
, funnel_state
);
492 * process forked, and user wants to track the new process,
493 * so attach a new knote to it, and immediately report an
494 * event with the parent's pid.
496 if ((event
== NOTE_FORK
) && (kn
->kn_sfflags
& NOTE_TRACK
)) {
501 * register knote with new process.
503 kev
.ident
= hint
& NOTE_PDATAMASK
; /* pid */
504 kev
.filter
= kn
->kn_filter
;
505 kev
.flags
= kn
->kn_flags
| EV_ADD
| EV_ENABLE
| EV_FLAG1
;
506 kev
.fflags
= kn
->kn_sfflags
;
507 kev
.data
= kn
->kn_id
; /* parent */
508 kev
.udata
= kn
->kn_kevent
.udata
; /* preserve udata */
509 error
= kevent_register(kn
->kn_kq
, &kev
, NULL
);
511 kn
->kn_fflags
|= NOTE_TRACKERR
;
513 event
= kn
->kn_fflags
;
514 thread_funnel_set(kernel_flock
, funnel_state
);
520 * filt_timercompute - compute absolute timeout
522 * The saved-data field in the knote contains the
523 * time value. The saved filter-flags indicates
524 * the unit of measurement.
526 * If the timeout is not absolute, adjust it for
530 filt_timercompute(struct knote
*kn
, uint64_t *abs_time
)
535 switch (kn
->kn_sfflags
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) {
537 multiplier
= NSEC_PER_SEC
;
540 multiplier
= NSEC_PER_USEC
;
545 case 0: /* milliseconds (default) */
546 multiplier
= NSEC_PER_SEC
/ 1000;
551 nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata
* multiplier
, &raw
);
552 if (raw
<= filt_timerfloor
) {
556 if ((kn
->kn_sfflags
& NOTE_ABSOLUTE
) == NOTE_ABSOLUTE
) {
557 uint32_t seconds
, nanoseconds
;
560 clock_get_calendar_nanotime(&seconds
, &nanoseconds
);
561 nanoseconds_to_absolutetime((uint64_t)seconds
* NSEC_PER_SEC
+ nanoseconds
,
563 if (now
>= raw
+ filt_timerfloor
) {
569 clock_absolutetime_interval_to_deadline(raw
, abs_time
);
574 * filt_timerexpire - the timer callout routine
576 * Just propagate the timer event into the knote
577 * filter routine (by going through the knote
578 * synchronization point). Pass a hint to
579 * indicate this is a real event, not just a
583 filt_timerexpire(void *knx
, __unused
void *spare
)
585 struct klist timer_list
;
586 struct knote
*kn
= knx
;
588 /* no "object" for timers, so fake a list */
589 SLIST_INIT(&timer_list
);
590 SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);
591 KNOTE(&timer_list
, 1);
595 * data contains amount of time to sleep, in milliseconds,
596 * or a pointer to a timespec structure.
599 filt_timerattach(struct knote
*kn
)
601 thread_call_t callout
;
605 error
= filt_timercompute(kn
, &deadline
);
610 callout
= thread_call_allocate(filt_timerexpire
, kn
);
614 /* handle as immediate */
620 kn
->kn_hook
= (caddr_t
)callout
;
622 /* absolute=EV_ONESHOT */
623 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
)
624 kn
->kn_flags
|= EV_ONESHOT
;
627 /* all others - if not faking immediate */
628 kn
->kn_flags
|= EV_CLEAR
;
629 thread_call_enter_delayed(callout
, deadline
);
640 filt_timerdetach(struct knote
*kn
)
642 thread_call_t callout
;
645 callout
= (thread_call_t
)kn
->kn_hook
;
646 if (callout
!= NULL
) {
649 /* cancel the callout if we can */
650 cancelled
= thread_call_cancel(callout
);
652 /* got it, just free it */
655 thread_call_free(callout
);
658 /* we have to wait for the expire routine. */
659 kn
->kn_hookid
= -1; /* we are detaching */
660 assert_wait(&kn
->kn_hook
, THREAD_UNINT
);
662 thread_block(THREAD_CONTINUE_NULL
);
663 assert(kn
->kn_hook
== NULL
);
673 filt_timer(struct knote
*kn
, __unused
long hint
)
679 thread_call_t callout
;
686 detaching
= (kn
->kn_hookid
< 0);
687 callout
= (thread_call_t
)kn
->kn_hook
;
689 if (!detaching
&& (kn
->kn_flags
& EV_ONESHOT
) == 0) {
693 /* user input data may have changed - deal */
694 error
= filt_timercompute(kn
, &deadline
);
696 kn
->kn_flags
|= EV_ERROR
;
698 } else if (deadline
== 0) {
699 /* revert to fake immediate */
700 kn
->kn_flags
&= ~EV_CLEAR
;
704 /* keep the callout and re-arm */
705 thread_call_enter_delayed(callout
, deadline
);
712 thread_call_free(callout
);
714 /* if someone is waiting for timer to pop */
716 thread_wakeup(&kn
->kn_hook
);
724 /* change fake timer to real if needed */
725 while (kn
->kn_hookid
> 0 && kn
->kn_sdata
> 0) {
728 /* update the fake timer (make real) */
732 error
= filt_timerattach(kn
);
735 kn
->kn_flags
|= EV_ERROR
;
742 /* if still fake, pretend it fired */
743 if (kn
->kn_hookid
> 0)
746 result
= (kn
->kn_data
!= 0);
754 lck_mtx_lock(&_filt_timerlock
);
758 filt_timerunlock(void)
760 lck_mtx_unlock(&_filt_timerlock
);
764 * JMM - placeholder for not-yet-implemented filters
767 filt_badattach(__unused
struct knote
*kn
)
774 kqueue_alloc(struct proc
*p
)
776 struct filedesc
*fdp
= p
->p_fd
;
779 MALLOC_ZONE(kq
, struct kqueue
*, sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
);
781 bzero(kq
, sizeof(struct kqueue
));
782 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
);
783 TAILQ_INIT(&kq
->kq_head
);
784 TAILQ_INIT(&kq
->kq_inprocess
);
788 if (fdp
->fd_knlistsize
< 0) {
790 if (fdp
->fd_knlistsize
< 0)
791 fdp
->fd_knlistsize
= 0; /* this process has had a kq */
800 * kqueue_dealloc - detach all knotes from a kqueue and free it
802 * We walk each list looking for knotes referencing this
803 * this kqueue. If we find one, we try to drop it. But
804 * if we fail to get a drop reference, that will wait
805 * until it is dropped. So, we can just restart again
806 * safe in the assumption that the list will eventually
807 * not contain any more references to this kqueue (either
808 * we dropped them all, or someone else did).
810 * Assumes no new events are being added to the kqueue.
811 * Nothing locked on entry or exit.
814 kqueue_dealloc(struct kqueue
*kq
, struct proc
*p
)
816 struct filedesc
*fdp
= p
->p_fd
;
821 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
822 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
824 if (kq
== kn
->kn_kq
) {
827 /* drop it ourselves or wait */
828 if (kqlock2knotedrop(kq
, kn
)) {
829 kn
->kn_fop
->f_detach(kn
);
833 /* start over at beginning of list */
834 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
837 kn
= SLIST_NEXT(kn
, kn_link
);
840 if (fdp
->fd_knhashmask
!= 0) {
841 for (i
= 0; i
< (int)fdp
->fd_knhashmask
+ 1; i
++) {
842 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
844 if (kq
== kn
->kn_kq
) {
847 /* drop it ourselves or wait */
848 if (kqlock2knotedrop(kq
, kn
)) {
849 kn
->kn_fop
->f_detach(kn
);
853 /* start over at beginning of list */
854 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
857 kn
= SLIST_NEXT(kn
, kn_link
);
862 lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
);
863 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
867 kqueue(struct proc
*p
, __unused
struct kqueue_args
*uap
, register_t
*retval
)
873 error
= falloc(p
, &fp
, &fd
);
878 kq
= kqueue_alloc(p
);
884 fp
->f_flag
= FREAD
| FWRITE
;
885 fp
->f_type
= DTYPE_KQUEUE
;
886 fp
->f_ops
= &kqueueops
;
887 fp
->f_data
= (caddr_t
)kq
;
890 *fdflags(p
, fd
) &= ~UF_RESERVED
;
891 fp_drop(p
, fd
, fp
, 1);
899 kqueue_portset_np(__unused
struct proc
*p
,
900 __unused
struct kqueue_portset_np_args
*uap
,
901 __unused register_t
*retval
)
903 /* JMM - Placeholder for now */
908 kqueue_from_portset_np(__unused
struct proc
*p
,
909 __unused
struct kqueue_from_portset_np_args
*uap
,
910 __unused register_t
*retval
)
912 /* JMM - Placeholder for now */
917 kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
)
922 if (IS_64BIT_PROCESS(p
)) {
923 struct user_kevent kev64
;
925 advance
= sizeof(kev64
);
926 error
= copyin(*addrp
, (caddr_t
)&kev64
, advance
);
929 kevp
->ident
= CAST_DOWN(uintptr_t, kev64
.ident
);
930 kevp
->filter
= kev64
.filter
;
931 kevp
->flags
= kev64
.flags
;
932 kevp
->fflags
= kev64
.fflags
;
933 kevp
->data
= CAST_DOWN(intptr_t, kev64
.data
);
934 kevp
->udata
= kev64
.udata
;
937 * compensate for legacy in-kernel kevent layout
938 * where the udata field is alredy 64-bit.
940 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
941 error
= copyin(*addrp
, (caddr_t
)kevp
, advance
);
949 kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
)
954 if (IS_64BIT_PROCESS(p
)) {
955 struct user_kevent kev64
;
957 kev64
.ident
= (uint64_t) kevp
->ident
;
958 kev64
.filter
= kevp
->filter
;
959 kev64
.flags
= kevp
->flags
;
960 kev64
.fflags
= kevp
->fflags
;
961 kev64
.data
= (int64_t) kevp
->data
;
962 kev64
.udata
= kevp
->udata
;
963 advance
= sizeof(kev64
);
964 error
= copyout((caddr_t
)&kev64
, *addrp
, advance
);
967 * compensate for legacy in-kernel kevent layout
968 * where the udata field is alredy 64-bit.
970 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
971 error
= copyout((caddr_t
)kevp
, *addrp
, advance
);
979 * kevent_continue - continue a kevent syscall after blocking
981 * assume we inherit a use count on the kq fileglob.
985 kevent_continue(__unused
struct kqueue
*kq
, void *data
, int error
)
987 struct _kevent
*cont_args
;
992 struct proc
*p
= current_proc();
994 cont_args
= (struct _kevent
*)data
;
995 noutputs
= cont_args
->eventout
;
996 retval
= cont_args
->retval
;
1000 fp_drop(p
, fd
, fp
, 0);
1002 /* don't restart after signals... */
1003 if (error
== ERESTART
)
1005 else if (error
== EWOULDBLOCK
)
1009 unix_syscall_return(error
);
1013 * kevent - [syscall] register and wait for kernel events
1018 kevent(struct proc
*p
, struct kevent_args
*uap
, register_t
*retval
)
1020 user_addr_t changelist
= uap
->changelist
;
1021 user_addr_t ueventlist
= uap
->eventlist
;
1022 int nchanges
= uap
->nchanges
;
1023 int nevents
= uap
->nevents
;
1026 struct _kevent
*cont_args
;
1029 struct fileproc
*fp
;
1031 int error
, noutputs
;
1034 /* convert timeout to absolute - if we have one */
1035 if (uap
->timeout
!= USER_ADDR_NULL
) {
1037 if ( IS_64BIT_PROCESS(p
) ) {
1038 struct user_timespec ts
;
1039 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1040 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ull
) != 0)
1043 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1046 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1047 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1051 if (itimerfix(&rtv
))
1053 getmicrouptime(&atv
);
1054 timevaladd(&atv
, &rtv
);
1060 /* get a usecount for the kq itself */
1061 if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0)
1064 /* register all the change requests the user provided... */
1066 while (nchanges
> 0 && error
== 0) {
1067 error
= kevent_copyin(&changelist
, &kev
, p
);
1071 kev
.flags
&= ~EV_SYSFLAGS
;
1072 error
= kevent_register(kq
, &kev
, p
);
1073 if (error
&& nevents
> 0) {
1074 kev
.flags
= EV_ERROR
;
1076 error
= kevent_copyout(&kev
, &ueventlist
, p
);
1085 /* store the continuation/completion data in the uthread */
1086 ut
= (uthread_t
)get_bsdthread_info(current_thread());
1087 cont_args
= (struct _kevent
*)&ut
->uu_state
.ss_kevent
;
1090 cont_args
->retval
= retval
;
1091 cont_args
->eventlist
= ueventlist
;
1092 cont_args
->eventcount
= nevents
;
1093 cont_args
->eventout
= noutputs
;
1095 if (nevents
> 0 && noutputs
== 0 && error
== 0)
1096 error
= kevent_scan(kq
, kevent_callback
,
1097 kevent_continue
, cont_args
,
1099 kevent_continue(kq
, cont_args
, error
);
1106 * kevent_callback - callback for each individual event
1108 * called with nothing locked
1109 * caller holds a reference on the kqueue
1113 kevent_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1115 struct _kevent
*cont_args
;
1118 cont_args
= (struct _kevent
*)data
;
1119 assert(cont_args
->eventout
< cont_arg
->eventcount
);
1122 * Copy out the appropriate amount of event data for this user.
1124 error
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc());
1127 * If there isn't space for additional events, return
1128 * a harmless error to stop the processing here
1130 if (error
== 0 && ++cont_args
->eventout
== cont_args
->eventcount
)
1131 error
= EWOULDBLOCK
;
1136 * kevent_register - add a new event to a kqueue
1138 * Creates a mapping between the event source and
1139 * the kqueue via a knote data structure.
1141 * Because many/most the event sources are file
1142 * descriptor related, the knote is linked off
1143 * the filedescriptor table for quick access.
1145 * called with nothing locked
1146 * caller holds a reference on the kqueue
1150 kevent_register(struct kqueue
*kq
, struct kevent
*kev
, struct proc
*p
)
1152 struct filedesc
*fdp
= kq
->kq_fdp
;
1153 struct filterops
*fops
;
1154 struct fileproc
*fp
= NULL
;
1155 struct knote
*kn
= NULL
;
1158 if (kev
->filter
< 0) {
1159 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
1161 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
1165 * filter attach routine is responsible for insuring that
1166 * the identifier can be attached to it.
1168 printf("unknown filter: %d\n", kev
->filter
);
1172 /* this iocount needs to be dropped if it is not registered */
1173 if (fops
->f_isfd
&& (error
= fp_lookup(p
, kev
->ident
, &fp
, 0)) != 0)
1179 /* fd-based knotes are linked off the fd table */
1180 if (kev
->ident
< (u_int
)fdp
->fd_knlistsize
) {
1181 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
1182 if (kq
== kn
->kn_kq
&&
1183 kev
->filter
== kn
->kn_filter
)
1187 /* hash non-fd knotes here too */
1188 if (fdp
->fd_knhashmask
!= 0) {
1191 list
= &fdp
->fd_knhash
[
1192 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
1193 SLIST_FOREACH(kn
, list
, kn_link
)
1194 if (kev
->ident
== kn
->kn_id
&&
1196 kev
->filter
== kn
->kn_filter
)
1202 * kn now contains the matching knote, or NULL if no match
1205 if ((kev
->flags
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) {
1214 kn
->kn_tq
= &kq
->kq_head
;
1216 kn
->kn_sfflags
= kev
->fflags
;
1217 kn
->kn_sdata
= kev
->data
;
1220 kn
->kn_kevent
= *kev
;
1221 kn
->kn_inuse
= 1; /* for f_attach() */
1224 /* before anyone can find it */
1225 if (kev
->flags
& EV_DISABLE
)
1226 kn
->kn_status
|= KN_DISABLED
;
1228 error
= knote_fdpattach(kn
, fdp
, p
);
1237 * apply reference count to knote structure, and
1238 * do not release it at the end of this routine.
1243 * If the attach fails here, we can drop it knowing
1244 * that nobody else has a reference to the knote.
1246 if ((error
= fops
->f_attach(kn
)) != 0) {
1256 /* existing knote - get kqueue lock */
1260 if (kev
->flags
& EV_DELETE
) {
1262 kn
->kn_status
|= KN_DISABLED
;
1263 if (kqlock2knotedrop(kq
, kn
)) {
1264 kn
->kn_fop
->f_detach(kn
);
1270 /* update status flags for existing knote */
1271 if (kev
->flags
& EV_DISABLE
) {
1273 kn
->kn_status
|= KN_DISABLED
;
1274 } else if (kev
->flags
& EV_ENABLE
) {
1275 kn
->kn_status
&= ~KN_DISABLED
;
1276 if (kn
->kn_status
& KN_ACTIVE
)
1281 * If somebody is in the middle of dropping this
1282 * knote - go find/insert a new one. But we have
1283 * wait for this one to go away first.
1285 if (!kqlock2knoteusewait(kq
, kn
))
1286 /* kqueue unlocked */
1290 * The user may change some filter values after the
1291 * initial EV_ADD, but doing so will not reset any
1292 * filter which have already been triggered.
1294 kn
->kn_sfflags
= kev
->fflags
;
1295 kn
->kn_sdata
= kev
->data
;
1296 kn
->kn_kevent
.udata
= kev
->udata
;
1299 /* still have use ref on knote */
1300 if (kn
->kn_fop
->f_event(kn
, 0)) {
1301 if (knoteuse2kqlock(kq
, kn
))
1310 fp_drop(p
, kev
->ident
, fp
, 0);
1315 * kevent_process - process the triggered events in a kqueue
1317 * Walk the queued knotes and validate that they are
1318 * really still triggered events by calling the filter
1319 * routines (if necessary). Hold a use reference on
1320 * the knote to avoid it being detached. For each event
1321 * that is still considered triggered, invoke the
1322 * callback routine provided.
1324 * caller holds a reference on the kqueue.
1325 * kqueue locked on entry and exit - but may be dropped
1329 kevent_process(struct kqueue
*kq
,
1330 kevent_callback_t callback
,
1341 if (kq
->kq_count
== 0) {
1346 /* if someone else is processing the queue, wait */
1347 if (!TAILQ_EMPTY(&kq
->kq_inprocess
)) {
1348 assert_wait(&kq
->kq_inprocess
, THREAD_UNINT
);
1349 kq
->kq_state
|= KQ_PROCWAIT
;
1351 thread_block(THREAD_CONTINUE_NULL
);
1358 while (error
== 0 &&
1359 (kn
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
1362 * move knote to the processed queue.
1363 * this is also protected by the kq lock.
1365 assert(kn
->kn_tq
== &kq
->kq_head
);
1366 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1367 kn
->kn_tq
= &kq
->kq_inprocess
;
1368 TAILQ_INSERT_TAIL(&kq
->kq_inprocess
, kn
, kn_tqe
);
1371 * Non-EV_ONESHOT events must be re-validated.
1373 * Convert our lock to a use-count and call the event's
1374 * filter routine to update.
1376 * If the event is dropping (or no longer valid), we
1377 * already have it off the active queue, so just
1378 * finish the job of deactivating it.
1380 if ((kn
->kn_flags
& EV_ONESHOT
) == 0) {
1383 if (kqlock2knoteuse(kq
, kn
)) {
1385 /* call the filter with just a ref */
1386 result
= kn
->kn_fop
->f_event(kn
, 0);
1388 if (!knoteuse2kqlock(kq
, kn
) || result
== 0) {
1389 knote_deactivate(kn
);
1393 knote_deactivate(kn
);
1399 * Got a valid triggered knote with the kqueue
1400 * still locked. Snapshot the data, and determine
1401 * how to dispatch the knote for future events.
1403 kev
= kn
->kn_kevent
;
1405 /* now what happens to it? */
1406 if (kn
->kn_flags
& EV_ONESHOT
) {
1407 knote_deactivate(kn
);
1408 if (kqlock2knotedrop(kq
, kn
)) {
1409 kn
->kn_fop
->f_detach(kn
);
1412 } else if (kn
->kn_flags
& EV_CLEAR
) {
1413 knote_deactivate(kn
);
1419 * leave on in-process queue. We'll
1420 * move all the remaining ones back
1421 * the kq queue and wakeup any
1422 * waiters when we are done.
1427 /* callback to handle each event as we find it */
1428 error
= (callback
)(kq
, &kev
, data
);
1435 * With the kqueue still locked, move any knotes
1436 * remaining on the in-process queue back to the
1437 * kq's queue and wake up any waiters.
1439 while ((kn
= TAILQ_FIRST(&kq
->kq_inprocess
)) != NULL
) {
1440 assert(kn
->kn_tq
== &kq
->kq_inprocess
);
1441 TAILQ_REMOVE(&kq
->kq_inprocess
, kn
, kn_tqe
);
1442 kn
->kn_tq
= &kq
->kq_head
;
1443 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1445 if (kq
->kq_state
& KQ_PROCWAIT
) {
1446 kq
->kq_state
&= ~KQ_PROCWAIT
;
1447 thread_wakeup(&kq
->kq_inprocess
);
1456 kevent_scan_continue(void *data
, wait_result_t wait_result
)
1458 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1459 struct _kevent_scan
* cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1460 struct kqueue
*kq
= (struct kqueue
*)data
;
1464 /* convert the (previous) wait_result to a proper error */
1465 switch (wait_result
) {
1466 case THREAD_AWAKENED
:
1468 error
= kevent_process(kq
, cont_args
->call
, cont_args
, &count
, current_proc());
1469 if (error
== 0 && count
== 0) {
1470 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, cont_args
->deadline
);
1471 kq
->kq_state
|= KQ_SLEEP
;
1473 thread_block_parameter(kevent_scan_continue
, kq
);
1478 case THREAD_TIMED_OUT
:
1479 error
= EWOULDBLOCK
;
1481 case THREAD_INTERRUPTED
:
1485 panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result
);
1489 /* call the continuation with the results */
1490 assert(cont_args
->cont
!= NULL
);
1491 (cont_args
->cont
)(kq
, cont_args
->data
, error
);
1496 * kevent_scan - scan and wait for events in a kqueue
1498 * Process the triggered events in a kqueue.
1500 * If there are no events triggered arrange to
1501 * wait for them. If the caller provided a
1502 * continuation routine, then kevent_scan will
1505 * The callback routine must be valid.
1506 * The caller must hold a use-count reference on the kq.
1510 kevent_scan(struct kqueue
*kq
,
1511 kevent_callback_t callback
,
1512 kevent_continue_t continuation
,
1514 struct timeval
*atvp
,
1517 thread_continue_t cont
= THREAD_CONTINUE_NULL
;
1522 assert(callback
!= NULL
);
1526 wait_result_t wait_result
;
1530 * Make a pass through the kq to find events already
1534 error
= kevent_process(kq
, callback
, data
, &count
, p
);
1536 break; /* lock still held */
1538 /* looks like we have to consider blocking */
1541 /* convert the timeout to a deadline once */
1542 if (atvp
->tv_sec
|| atvp
->tv_usec
) {
1543 uint32_t seconds
, nanoseconds
;
1546 clock_get_uptime(&now
);
1547 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec
* NSEC_PER_SEC
+
1548 atvp
->tv_usec
* NSEC_PER_USEC
,
1550 if (now
>= deadline
) {
1551 /* non-blocking call */
1552 error
= EWOULDBLOCK
;
1553 break; /* lock still held */
1556 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
1558 deadline
= 0; /* block forever */
1562 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1563 struct _kevent_scan
*cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1565 cont_args
->call
= callback
;
1566 cont_args
->cont
= continuation
;
1567 cont_args
->deadline
= deadline
;
1568 cont_args
->data
= data
;
1569 cont
= kevent_scan_continue
;
1573 /* go ahead and wait */
1574 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, deadline
);
1575 kq
->kq_state
|= KQ_SLEEP
;
1577 wait_result
= thread_block_parameter(cont
, kq
);
1578 /* NOTREACHED if (continuation != NULL) */
1580 switch (wait_result
) {
1581 case THREAD_AWAKENED
:
1583 case THREAD_TIMED_OUT
:
1585 case THREAD_INTERRUPTED
:
1588 panic("kevent_scan - bad wait_result (%d)",
1600 * This could be expanded to call kqueue_scan, if desired.
1604 kqueue_read(__unused
struct fileproc
*fp
,
1605 __unused
struct uio
*uio
,
1606 __unused kauth_cred_t cred
,
1608 __unused
struct proc
*p
)
1615 kqueue_write(__unused
struct fileproc
*fp
,
1616 __unused
struct uio
*uio
,
1617 __unused kauth_cred_t cred
,
1619 __unused
struct proc
*p
)
1626 kqueue_ioctl(__unused
struct fileproc
*fp
,
1627 __unused u_long com
,
1628 __unused caddr_t data
,
1629 __unused
struct proc
*p
)
1636 kqueue_select(struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
)
1638 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1641 if (which
== FREAD
) {
1646 selrecord(p
, &kq
->kq_sel
, wql
);
1647 kq
->kq_state
|= KQ_SEL
;
1659 kqueue_close(struct fileglob
*fg
, struct proc
*p
)
1661 struct kqueue
*kq
= (struct kqueue
*)fg
->fg_data
;
1663 kqueue_dealloc(kq
, p
);
1670 * The callers has taken a use-count reference on this kqueue and will donate it
1671 * to the kqueue we are being added to. This keeps the kqueue from closing until
1672 * that relationship is torn down.
1675 kqueue_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
1677 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
1679 if (kn
->kn_filter
!= EVFILT_READ
)
1682 kn
->kn_fop
= &kqread_filtops
;
1684 KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
);
1691 kqueue_stat(struct fileproc
*fp
, struct stat
*st
, __unused
struct proc
*p
)
1693 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1695 bzero((void *)st
, sizeof(*st
));
1696 st
->st_size
= kq
->kq_count
;
1697 st
->st_blksize
= sizeof(struct kevent
);
1698 st
->st_mode
= S_IFIFO
;
1703 * Called with the kqueue locked
1706 kqueue_wakeup(struct kqueue
*kq
)
1709 if (kq
->kq_state
& KQ_SLEEP
) {
1710 kq
->kq_state
&= ~KQ_SLEEP
;
1713 if (kq
->kq_state
& KQ_SEL
) {
1714 kq
->kq_state
&= ~KQ_SEL
;
1715 selwakeup(&kq
->kq_sel
);
1717 KNOTE(&kq
->kq_sel
.si_note
, 0);
1721 klist_init(struct klist
*list
)
1728 * Query/Post each knote in the object's list
1730 * The object lock protects the list. It is assumed
1731 * that the filter/event routine for the object can
1732 * determine that the object is already locked (via
1733 * the hind) and not deadlock itself.
1735 * The object lock should also hold off pending
1736 * detach/drop operations. But we'll prevent it here
1737 * too - just in case.
1740 knote(struct klist
*list
, long hint
)
1744 SLIST_FOREACH(kn
, list
, kn_selnext
) {
1745 struct kqueue
*kq
= kn
->kn_kq
;
1748 if (kqlock2knoteuse(kq
, kn
)) {
1751 /* call the event with only a use count */
1752 result
= kn
->kn_fop
->f_event(kn
, hint
);
1754 /* if its not going away and triggered */
1755 if (knoteuse2kqlock(kq
, kn
) && result
)
1757 /* lock held again */
1764 * attach a knote to the specified list. Return true if this is the first entry.
1765 * The list is protected by whatever lock the object it is associated with uses.
1768 knote_attach(struct klist
*list
, struct knote
*kn
)
1770 int ret
= SLIST_EMPTY(list
);
1771 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
1776 * detach a knote from the specified list. Return true if that was the last entry.
1777 * The list is protected by whatever lock the object it is associated with uses.
1780 knote_detach(struct klist
*list
, struct knote
*kn
)
1782 SLIST_REMOVE(list
, kn
, knote
, kn_selnext
);
1783 return SLIST_EMPTY(list
);
1787 * remove all knotes referencing a specified fd
1789 * Essentially an inlined knote_remove & knote_drop
1790 * when we know for sure that the thing is a file
1792 * Entered with the proc_fd lock already held.
1793 * It returns the same way, but may drop it temporarily.
1796 knote_fdclose(struct proc
*p
, int fd
)
1798 struct filedesc
*fdp
= p
->p_fd
;
1802 list
= &fdp
->fd_knlist
[fd
];
1803 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
1804 struct kqueue
*kq
= kn
->kn_kq
;
1810 * Convert the lock to a drop ref.
1811 * If we get it, go ahead and drop it.
1812 * Otherwise, we waited for it to
1813 * be dropped by the other guy, so
1814 * it is safe to move on in the list.
1816 if (kqlock2knotedrop(kq
, kn
)) {
1817 kn
->kn_fop
->f_detach(kn
);
1823 /* the fd tables may have changed - start over */
1824 list
= &fdp
->fd_knlist
[fd
];
1828 /* proc_fdlock held on entry (and exit) */
1830 knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, __unused
struct proc
*p
)
1832 struct klist
*list
= NULL
;
1834 if (! kn
->kn_fop
->f_isfd
) {
1835 if (fdp
->fd_knhashmask
== 0)
1836 fdp
->fd_knhash
= hashinit(KN_HASHSIZE
, M_KQUEUE
,
1837 &fdp
->fd_knhashmask
);
1838 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1840 if ((u_int
)fdp
->fd_knlistsize
<= kn
->kn_id
) {
1843 /* have to grow the fd_knlist */
1844 size
= fdp
->fd_knlistsize
;
1845 while (size
<= kn
->kn_id
)
1847 MALLOC(list
, struct klist
*,
1848 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
1852 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
1853 fdp
->fd_knlistsize
* sizeof(struct klist
*));
1854 bzero((caddr_t
)list
+
1855 fdp
->fd_knlistsize
* sizeof(struct klist
*),
1856 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
1857 FREE(fdp
->fd_knlist
, M_KQUEUE
);
1858 fdp
->fd_knlist
= list
;
1859 fdp
->fd_knlistsize
= size
;
1861 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1863 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
1870 * should be called at spl == 0, since we don't want to hold spl
1871 * while calling fdrop and free.
1874 knote_drop(struct knote
*kn
, struct proc
*p
)
1876 struct filedesc
*fdp
= p
->p_fd
;
1877 struct kqueue
*kq
= kn
->kn_kq
;
1881 if (kn
->kn_fop
->f_isfd
)
1882 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1884 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1886 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
1889 if (kn
->kn_status
& KN_DROPWAIT
)
1890 thread_wakeup(&kn
->kn_status
);
1894 if (kn
->kn_fop
->f_isfd
)
1895 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0);
1900 /* called with kqueue lock held */
1902 knote_activate(struct knote
*kn
)
1904 struct kqueue
*kq
= kn
->kn_kq
;
1906 kn
->kn_status
|= KN_ACTIVE
;
1911 /* called with kqueue lock held */
1913 knote_deactivate(struct knote
*kn
)
1915 kn
->kn_status
&= ~KN_ACTIVE
;
1919 /* called with kqueue lock held */
1921 knote_enqueue(struct knote
*kn
)
1923 struct kqueue
*kq
= kn
->kn_kq
;
1925 if ((kn
->kn_status
& (KN_QUEUED
| KN_DISABLED
)) == 0) {
1926 struct kqtailq
*tq
= kn
->kn_tq
;
1928 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);
1929 kn
->kn_status
|= KN_QUEUED
;
1934 /* called with kqueue lock held */
1936 knote_dequeue(struct knote
*kn
)
1938 struct kqueue
*kq
= kn
->kn_kq
;
1940 assert((kn
->kn_status
& KN_DISABLED
) == 0);
1941 if ((kn
->kn_status
& KN_QUEUED
) == KN_QUEUED
) {
1942 struct kqtailq
*tq
= kn
->kn_tq
;
1944 TAILQ_REMOVE(tq
, kn
, kn_tqe
);
1945 kn
->kn_tq
= &kq
->kq_head
;
1946 kn
->kn_status
&= ~KN_QUEUED
;
1954 knote_zone
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
), 8192, "knote zone");
1956 /* allocate kq lock group attribute and group */
1957 kq_lck_grp_attr
= lck_grp_attr_alloc_init();
1958 lck_grp_attr_setstat(kq_lck_grp_attr
);
1960 kq_lck_grp
= lck_grp_alloc_init("kqueue", kq_lck_grp_attr
);
1962 /* Allocate kq lock attribute */
1963 kq_lck_attr
= lck_attr_alloc_init();
1964 lck_attr_setdefault(kq_lck_attr
);
1966 /* Initialize the timer filter lock */
1967 lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
);
1969 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
1971 static struct knote
*
1974 return ((struct knote
*)zalloc(knote_zone
));
1978 knote_free(struct knote
*kn
)
1980 zfree(knote_zone
, kn
);
1983 #include <sys/param.h>
1984 #include <sys/socket.h>
1985 #include <sys/protosw.h>
1986 #include <sys/domain.h>
1987 #include <sys/mbuf.h>
1988 #include <sys/kern_event.h>
1989 #include <sys/malloc.h>
1990 #include <sys/sys_domain.h>
1991 #include <sys/syslog.h>
1994 static int kev_attach(struct socket
*so
, int proto
, struct proc
*p
);
1995 static int kev_detach(struct socket
*so
);
1996 static int kev_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
);
1998 struct pr_usrreqs event_usrreqs
= {
1999 pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
,
2000 pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
,
2001 pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
2002 pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
2003 pru_sosend_notsupp
, soreceive
, pru_sopoll_notsupp
2006 struct protosw eventsw
[] = {
2008 SOCK_RAW
, &systemdomain
, SYSPROTO_EVENT
, PR_ATOMIC
,
2024 struct kern_event_head kern_event_head
;
2026 static u_long static_event_id
= 0;
2027 struct domain
*sysdom
= &systemdomain
;
2029 static lck_grp_t
*evt_mtx_grp
;
2030 static lck_attr_t
*evt_mtx_attr
;
2031 static lck_grp_attr_t
*evt_mtx_grp_attr
;
2032 lck_mtx_t
*evt_mutex
;
2034 * Install the protosw's for the NKE manager. Invoked at
2035 * extension load time
2038 kern_event_init(void)
2042 if ((retval
= net_add_proto(eventsw
, &systemdomain
)) != 0) {
2043 log(LOG_WARNING
, "Can't install kernel events protocol (%d)\n", retval
);
2048 * allocate lock group attribute and group for kern event
2050 evt_mtx_grp_attr
= lck_grp_attr_alloc_init();
2052 evt_mtx_grp
= lck_grp_alloc_init("eventlist", evt_mtx_grp_attr
);
2055 * allocate the lock attribute for mutexes
2057 evt_mtx_attr
= lck_attr_alloc_init();
2058 lck_attr_setdefault(evt_mtx_attr
);
2059 evt_mutex
= lck_mtx_alloc_init(evt_mtx_grp
, evt_mtx_attr
);
2060 if (evt_mutex
== NULL
)
2063 return(KERN_SUCCESS
);
2067 kev_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
2070 struct kern_event_pcb
*ev_pcb
;
2072 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
2076 MALLOC(ev_pcb
, struct kern_event_pcb
*, sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
);
2080 ev_pcb
->ev_socket
= so
;
2081 ev_pcb
->vendor_code_filter
= 0xffffffff;
2083 so
->so_pcb
= (caddr_t
) ev_pcb
;
2084 lck_mtx_lock(evt_mutex
);
2085 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
);
2086 lck_mtx_unlock(evt_mutex
);
2093 kev_detach(struct socket
*so
)
2095 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2098 lck_mtx_lock(evt_mutex
);
2099 LIST_REMOVE(ev_pcb
, ev_link
);
2100 lck_mtx_unlock(evt_mutex
);
2101 FREE(ev_pcb
, M_PCB
);
2103 so
->so_flags
|= SOF_PCBCLEARING
;
2110 * For now, kev_vender_code and mbuf_tags use the same
2113 extern errno_t
mbuf_tag_id_find_internal(const char *string
, u_long
*out_id
,
2116 errno_t
kev_vendor_code_find(
2118 u_long
*out_vender_code
)
2120 if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) {
2123 return mbuf_tag_id_find_internal(string
, out_vender_code
, 1);
2126 extern void mbuf_tag_id_first_last(u_long
*first
, u_long
*last
);
2128 errno_t
kev_msg_post(struct kev_msg
*event_msg
)
2130 u_long min_vendor
, max_vendor
;
2132 mbuf_tag_id_first_last(&min_vendor
, &max_vendor
);
2134 if (event_msg
== NULL
)
2137 /* Limit third parties to posting events for registered vendor codes only */
2138 if (event_msg
->vendor_code
< min_vendor
||
2139 event_msg
->vendor_code
> max_vendor
)
2144 return kev_post_msg(event_msg
);
2148 int kev_post_msg(struct kev_msg
*event_msg
)
2150 struct mbuf
*m
, *m2
;
2151 struct kern_event_pcb
*ev_pcb
;
2152 struct kern_event_msg
*ev
;
2154 unsigned long total_size
;
2157 /* Verify the message is small enough to fit in one mbuf w/o cluster */
2158 total_size
= KEV_MSG_HEADER_SIZE
;
2160 for (i
= 0; i
< 5; i
++) {
2161 if (event_msg
->dv
[i
].data_length
== 0)
2163 total_size
+= event_msg
->dv
[i
].data_length
;
2166 if (total_size
> MLEN
) {
2170 m
= m_get(M_DONTWAIT
, MT_DATA
);
2174 ev
= mtod(m
, struct kern_event_msg
*);
2175 total_size
= KEV_MSG_HEADER_SIZE
;
2177 tmp
= (char *) &ev
->event_data
[0];
2178 for (i
= 0; i
< 5; i
++) {
2179 if (event_msg
->dv
[i
].data_length
== 0)
2182 total_size
+= event_msg
->dv
[i
].data_length
;
2183 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
2184 event_msg
->dv
[i
].data_length
);
2185 tmp
+= event_msg
->dv
[i
].data_length
;
2188 ev
->id
= ++static_event_id
;
2189 ev
->total_size
= total_size
;
2190 ev
->vendor_code
= event_msg
->vendor_code
;
2191 ev
->kev_class
= event_msg
->kev_class
;
2192 ev
->kev_subclass
= event_msg
->kev_subclass
;
2193 ev
->event_code
= event_msg
->event_code
;
2195 m
->m_len
= total_size
;
2196 lck_mtx_lock(evt_mutex
);
2197 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
2199 ev_pcb
= LIST_NEXT(ev_pcb
, ev_link
)) {
2201 if (ev_pcb
->vendor_code_filter
!= KEV_ANY_VENDOR
) {
2202 if (ev_pcb
->vendor_code_filter
!= ev
->vendor_code
)
2205 if (ev_pcb
->class_filter
!= KEV_ANY_CLASS
) {
2206 if (ev_pcb
->class_filter
!= ev
->kev_class
)
2209 if ((ev_pcb
->subclass_filter
!= KEV_ANY_SUBCLASS
) &&
2210 (ev_pcb
->subclass_filter
!= ev
->kev_subclass
))
2215 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
2218 lck_mtx_unlock(evt_mutex
);
2221 socket_lock(ev_pcb
->ev_socket
, 1);
2222 if (sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
))
2223 sorwakeup(ev_pcb
->ev_socket
);
2224 socket_unlock(ev_pcb
->ev_socket
, 1);
2228 lck_mtx_unlock(evt_mutex
);
2233 kev_control(struct socket
*so
,
2236 __unused
struct ifnet
*ifp
,
2237 __unused
struct proc
*p
)
2239 struct kev_request
*kev_req
= (struct kev_request
*) data
;
2240 struct kern_event_pcb
*ev_pcb
;
2241 struct kev_vendor_code
*kev_vendor
;
2242 u_long
*id_value
= (u_long
*) data
;
2248 *id_value
= static_event_id
;
2252 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2253 ev_pcb
->vendor_code_filter
= kev_req
->vendor_code
;
2254 ev_pcb
->class_filter
= kev_req
->kev_class
;
2255 ev_pcb
->subclass_filter
= kev_req
->kev_subclass
;
2259 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2260 kev_req
->vendor_code
= ev_pcb
->vendor_code_filter
;
2261 kev_req
->kev_class
= ev_pcb
->class_filter
;
2262 kev_req
->kev_subclass
= ev_pcb
->subclass_filter
;
2265 case SIOCGKEVVENDOR
:
2266 kev_vendor
= (struct kev_vendor_code
*)data
;
2268 /* Make sure string is NULL terminated */
2269 kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0;
2271 return mbuf_tag_id_find_internal(kev_vendor
->vendor_string
,
2272 &kev_vendor
->vendor_code
, 0);