2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
36 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * @(#)kern_event.c 1.0 (3/31/2000)
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/filedesc.h>
56 #include <sys/kernel.h>
57 #include <sys/proc_internal.h>
58 #include <sys/kauth.h>
59 #include <sys/malloc.h>
60 #include <sys/unistd.h>
61 #include <sys/file_internal.h>
62 #include <sys/fcntl.h>
63 #include <sys/select.h>
64 #include <sys/queue.h>
65 #include <sys/event.h>
66 #include <sys/eventvar.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
71 #include <sys/sysctl.h>
73 #include <sys/sysproto.h>
77 #include <kern/lock.h>
78 #include <kern/clock.h>
79 #include <kern/thread_call.h>
80 #include <kern/sched_prim.h>
81 #include <kern/zalloc.h>
82 #include <kern/assert.h>
84 #include <libkern/libkern.h>
86 extern void unix_syscall_return(int);
88 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system");
90 static inline void kqlock(struct kqueue
*kq
);
91 static inline void kqunlock(struct kqueue
*kq
);
93 static int kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
);
94 static int kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
);
95 static int kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
);
96 static int knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
);
98 static void kqueue_wakeup(struct kqueue
*kq
);
99 static int kqueue_read(struct fileproc
*fp
, struct uio
*uio
,
100 kauth_cred_t cred
, int flags
, struct proc
*p
);
101 static int kqueue_write(struct fileproc
*fp
, struct uio
*uio
,
102 kauth_cred_t cred
, int flags
, struct proc
*p
);
103 static int kqueue_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
105 static int kqueue_select(struct fileproc
*fp
, int which
, void *wql
,
107 static int kqueue_close(struct fileglob
*fp
, struct proc
*p
);
108 static int kqueue_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
109 extern int kqueue_stat(struct fileproc
*fp
, struct stat
*st
, struct proc
*p
);
111 static struct fileops kqueueops
= {
121 static int kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
);
122 static int kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
);
124 static int kevent_callback(struct kqueue
*kq
, struct kevent
*kevp
, void *data
);
125 static void kevent_continue(struct kqueue
*kq
, void *data
, int error
);
126 static void kevent_scan_continue(void *contp
, wait_result_t wait_result
);
127 static int kevent_process(struct kqueue
*kq
, kevent_callback_t callback
,
128 void *data
, int *countp
, struct proc
*p
);
129 static void knote_put(struct knote
*kn
);
130 static int knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
);
131 static void knote_drop(struct knote
*kn
, struct proc
*p
);
132 static void knote_activate(struct knote
*kn
);
133 static void knote_deactivate(struct knote
*kn
);
134 static void knote_enqueue(struct knote
*kn
);
135 static void knote_dequeue(struct knote
*kn
);
136 static struct knote
*knote_alloc(void);
137 static void knote_free(struct knote
*kn
);
138 extern void knote_init(void);
140 static int filt_fileattach(struct knote
*kn
);
141 static struct filterops file_filtops
=
142 { 1, filt_fileattach
, NULL
, NULL
};
144 static void filt_kqdetach(struct knote
*kn
);
145 static int filt_kqueue(struct knote
*kn
, long hint
);
146 static struct filterops kqread_filtops
=
147 { 1, NULL
, filt_kqdetach
, filt_kqueue
};
150 * placeholder for not-yet-implemented filters
152 static int filt_badattach(struct knote
*kn
);
153 static struct filterops bad_filtops
=
154 { 0, filt_badattach
, 0 , 0 };
156 static int filt_procattach(struct knote
*kn
);
157 static void filt_procdetach(struct knote
*kn
);
158 static int filt_proc(struct knote
*kn
, long hint
);
160 static struct filterops proc_filtops
=
161 { 0, filt_procattach
, filt_procdetach
, filt_proc
};
163 extern struct filterops fs_filtops
;
165 extern struct filterops sig_filtops
;
169 static int filt_timercompute(struct knote
*kn
, uint64_t *abs_time
);
170 static void filt_timerexpire(void *knx
, void *param1
);
171 static int filt_timerattach(struct knote
*kn
);
172 static void filt_timerdetach(struct knote
*kn
);
173 static int filt_timer(struct knote
*kn
, long hint
);
175 static struct filterops timer_filtops
=
176 { 0, filt_timerattach
, filt_timerdetach
, filt_timer
};
178 /* to avoid arming timers that fire quicker than we can handle */
179 static uint64_t filt_timerfloor
= 0;
181 static lck_mtx_t _filt_timerlock
;
182 static void filt_timerlock(void);
183 static void filt_timerunlock(void);
186 * Sentinel marker for a thread scanning through the list of
189 static struct filterops threadmarker_filtops
=
190 { 0, filt_badattach
, 0, 0 };
192 static zone_t knote_zone
;
194 #define KN_HASHSIZE 64 /* XXX should be tunable */
195 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
198 extern struct filterops aio_filtops
;
202 * Table for for all system-defined filters.
204 static struct filterops
*sysfilt_ops
[] = {
205 &file_filtops
, /* EVFILT_READ */
206 &file_filtops
, /* EVFILT_WRITE */
208 &aio_filtops
, /* EVFILT_AIO */
210 &bad_filtops
, /* EVFILT_AIO */
212 &file_filtops
, /* EVFILT_VNODE */
213 &proc_filtops
, /* EVFILT_PROC */
214 &sig_filtops
, /* EVFILT_SIGNAL */
215 &timer_filtops
, /* EVFILT_TIMER */
216 &bad_filtops
, /* EVFILT_MACHPORT */
217 &fs_filtops
/* EVFILT_FS */
221 * kqueue/note lock attributes and implementations
223 * kqueues have locks, while knotes have use counts
224 * Most of the knote state is guarded by the object lock.
225 * the knote "inuse" count and status use the kqueue lock.
227 lck_grp_attr_t
* kq_lck_grp_attr
;
228 lck_grp_t
* kq_lck_grp
;
229 lck_attr_t
* kq_lck_attr
;
232 kqlock(struct kqueue
*kq
)
234 lck_spin_lock(&kq
->kq_lock
);
238 kqunlock(struct kqueue
*kq
)
240 lck_spin_unlock(&kq
->kq_lock
);
244 * Convert a kq lock to a knote use referece.
246 * If the knote is being dropped, we can't get
247 * a use reference, so just return with it
250 * - kq locked at entry
251 * - unlock on exit if we get the use reference
254 kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
)
256 if (kn
->kn_status
& KN_DROPPING
)
264 * Convert a kq lock to a knote use referece.
266 * If the knote is being dropped, we can't get
267 * a use reference, so just return with it
270 * - kq locked at entry
271 * - kq always unlocked on exit
274 kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
)
276 if (!kqlock2knoteuse(kq
, kn
)) {
277 kn
->kn_status
|= KN_DROPWAIT
;
278 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
280 thread_block(THREAD_CONTINUE_NULL
);
287 * Convert from a knote use reference back to kq lock.
289 * Drop a use reference and wake any waiters if
290 * this is the last one.
292 * The exit return indicates if the knote is
293 * still alive - but the kqueue lock is taken
297 knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
)
300 if ((--kn
->kn_inuse
== 0) &&
301 (kn
->kn_status
& KN_USEWAIT
)) {
302 kn
->kn_status
&= ~KN_USEWAIT
;
303 thread_wakeup(&kn
->kn_inuse
);
305 return ((kn
->kn_status
& KN_DROPPING
) == 0);
309 * Convert a kq lock to a knote drop referece.
311 * If the knote is in use, wait for the use count
312 * to subside. We first mark our intention to drop
313 * it - keeping other users from "piling on."
314 * If we are too late, we have to wait for the
315 * other drop to complete.
317 * - kq locked at entry
318 * - always unlocked on exit.
319 * - caller can't hold any locks that would prevent
320 * the other dropper from completing.
323 kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
)
326 if ((kn
->kn_status
& KN_DROPPING
) == 0) {
327 kn
->kn_status
|= KN_DROPPING
;
328 if (kn
->kn_inuse
> 0) {
329 kn
->kn_status
|= KN_USEWAIT
;
330 assert_wait(&kn
->kn_inuse
, THREAD_UNINT
);
332 thread_block(THREAD_CONTINUE_NULL
);
337 kn
->kn_status
|= KN_DROPWAIT
;
338 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
340 thread_block(THREAD_CONTINUE_NULL
);
346 * Release a knote use count reference.
349 knote_put(struct knote
*kn
)
351 struct kqueue
*kq
= kn
->kn_kq
;
354 if ((--kn
->kn_inuse
== 0) &&
355 (kn
->kn_status
& KN_USEWAIT
)) {
356 kn
->kn_status
&= ~KN_USEWAIT
;
357 thread_wakeup(&kn
->kn_inuse
);
365 filt_fileattach(struct knote
*kn
)
368 return (fo_kqfilter(kn
->kn_fp
, kn
, current_proc()));
371 #define f_flag f_fglob->fg_flag
372 #define f_type f_fglob->fg_type
373 #define f_msgcount f_fglob->fg_msgcount
374 #define f_cred f_fglob->fg_cred
375 #define f_ops f_fglob->fg_ops
376 #define f_offset f_fglob->fg_offset
377 #define f_data f_fglob->fg_data
380 filt_kqdetach(struct knote
*kn
)
382 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
385 KNOTE_DETACH(&kq
->kq_sel
.si_note
, kn
);
391 filt_kqueue(struct knote
*kn
, __unused
long hint
)
393 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
395 kn
->kn_data
= kq
->kq_count
;
396 return (kn
->kn_data
> 0);
400 filt_procattach(struct knote
*kn
)
405 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
407 p
= pfind(kn
->kn_id
);
409 thread_funnel_set(kernel_flock
, funnel_state
);
413 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
416 * internal flag indicating registration done by kernel
418 if (kn
->kn_flags
& EV_FLAG1
) {
419 kn
->kn_data
= (int)kn
->kn_sdata
; /* ppid */
420 kn
->kn_fflags
= NOTE_CHILD
;
421 kn
->kn_flags
&= ~EV_FLAG1
;
424 /* XXX lock the proc here while adding to the list? */
425 KNOTE_ATTACH(&p
->p_klist
, kn
);
427 thread_funnel_set(kernel_flock
, funnel_state
);
433 * The knote may be attached to a different process, which may exit,
434 * leaving nothing for the knote to be attached to. So when the process
435 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
436 * it will be deleted when read out. However, as part of the knote deletion,
437 * this routine is called, so a check is needed to avoid actually performing
438 * a detach, because the original process does not exist any more.
441 filt_procdetach(struct knote
*kn
)
446 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
447 p
= pfind(kn
->kn_id
);
449 if (p
!= (struct proc
*)NULL
)
450 KNOTE_DETACH(&p
->p_klist
, kn
);
452 thread_funnel_set(kernel_flock
, funnel_state
);
456 filt_proc(struct knote
*kn
, long hint
)
461 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
464 * mask off extra data
466 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
469 * if the user is interested in this event, record it.
471 if (kn
->kn_sfflags
& event
)
472 kn
->kn_fflags
|= event
;
475 * process is gone, so flag the event as finished.
477 if (event
== NOTE_EXIT
) {
478 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
479 thread_funnel_set(kernel_flock
, funnel_state
);
484 * process forked, and user wants to track the new process,
485 * so attach a new knote to it, and immediately report an
486 * event with the parent's pid.
488 if ((event
== NOTE_FORK
) && (kn
->kn_sfflags
& NOTE_TRACK
)) {
493 * register knote with new process.
495 kev
.ident
= hint
& NOTE_PDATAMASK
; /* pid */
496 kev
.filter
= kn
->kn_filter
;
497 kev
.flags
= kn
->kn_flags
| EV_ADD
| EV_ENABLE
| EV_FLAG1
;
498 kev
.fflags
= kn
->kn_sfflags
;
499 kev
.data
= kn
->kn_id
; /* parent */
500 kev
.udata
= kn
->kn_kevent
.udata
; /* preserve udata */
501 error
= kevent_register(kn
->kn_kq
, &kev
, NULL
);
503 kn
->kn_fflags
|= NOTE_TRACKERR
;
505 event
= kn
->kn_fflags
;
506 thread_funnel_set(kernel_flock
, funnel_state
);
512 * filt_timercompute - compute absolute timeout
514 * The saved-data field in the knote contains the
515 * time value. The saved filter-flags indicates
516 * the unit of measurement.
518 * If the timeout is not absolute, adjust it for
522 filt_timercompute(struct knote
*kn
, uint64_t *abs_time
)
527 switch (kn
->kn_sfflags
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) {
529 multiplier
= NSEC_PER_SEC
;
532 multiplier
= NSEC_PER_USEC
;
537 case 0: /* milliseconds (default) */
538 multiplier
= NSEC_PER_SEC
/ 1000;
543 nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata
* multiplier
, &raw
);
544 if (raw
<= filt_timerfloor
) {
548 if ((kn
->kn_sfflags
& NOTE_ABSOLUTE
) == NOTE_ABSOLUTE
) {
549 uint32_t seconds
, nanoseconds
;
552 clock_get_calendar_nanotime(&seconds
, &nanoseconds
);
553 nanoseconds_to_absolutetime((uint64_t)seconds
* NSEC_PER_SEC
+ nanoseconds
,
555 if (now
>= raw
+ filt_timerfloor
) {
561 clock_absolutetime_interval_to_deadline(raw
, abs_time
);
566 * filt_timerexpire - the timer callout routine
568 * Just propagate the timer event into the knote
569 * filter routine (by going through the knote
570 * synchronization point). Pass a hint to
571 * indicate this is a real event, not just a
575 filt_timerexpire(void *knx
, __unused
void *spare
)
577 struct klist timer_list
;
578 struct knote
*kn
= knx
;
580 /* no "object" for timers, so fake a list */
581 SLIST_INIT(&timer_list
);
582 SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);
583 KNOTE(&timer_list
, 1);
587 * data contains amount of time to sleep, in milliseconds,
588 * or a pointer to a timespec structure.
591 filt_timerattach(struct knote
*kn
)
593 thread_call_t callout
;
597 error
= filt_timercompute(kn
, &deadline
);
602 callout
= thread_call_allocate(filt_timerexpire
, kn
);
606 /* handle as immediate */
612 kn
->kn_hook
= (caddr_t
)callout
;
614 /* absolute=EV_ONESHOT */
615 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
)
616 kn
->kn_flags
|= EV_ONESHOT
;
619 /* all others - if not faking immediate */
620 kn
->kn_flags
|= EV_CLEAR
;
621 thread_call_enter_delayed(callout
, deadline
);
632 filt_timerdetach(struct knote
*kn
)
634 thread_call_t callout
;
637 callout
= (thread_call_t
)kn
->kn_hook
;
638 if (callout
!= NULL
) {
641 /* cancel the callout if we can */
642 cancelled
= thread_call_cancel(callout
);
644 /* got it, just free it */
647 thread_call_free(callout
);
650 /* we have to wait for the expire routine. */
651 kn
->kn_hookid
= -1; /* we are detaching */
652 assert_wait(&kn
->kn_hook
, THREAD_UNINT
);
654 thread_block(THREAD_CONTINUE_NULL
);
655 assert(kn
->kn_hook
== NULL
);
665 filt_timer(struct knote
*kn
, __unused
long hint
)
671 thread_call_t callout
;
678 detaching
= (kn
->kn_hookid
< 0);
679 callout
= (thread_call_t
)kn
->kn_hook
;
681 if (!detaching
&& (kn
->kn_flags
& EV_ONESHOT
) == 0) {
685 /* user input data may have changed - deal */
686 error
= filt_timercompute(kn
, &deadline
);
688 kn
->kn_flags
|= EV_ERROR
;
690 } else if (deadline
== 0) {
691 /* revert to fake immediate */
692 kn
->kn_flags
&= ~EV_CLEAR
;
696 /* keep the callout and re-arm */
697 thread_call_enter_delayed(callout
, deadline
);
704 thread_call_free(callout
);
706 /* if someone is waiting for timer to pop */
708 thread_wakeup(&kn
->kn_hook
);
716 /* change fake timer to real if needed */
717 while (kn
->kn_hookid
> 0 && kn
->kn_sdata
> 0) {
720 /* update the fake timer (make real) */
724 error
= filt_timerattach(kn
);
727 kn
->kn_flags
|= EV_ERROR
;
734 /* if still fake, pretend it fired */
735 if (kn
->kn_hookid
> 0)
738 result
= (kn
->kn_data
!= 0);
746 lck_mtx_lock(&_filt_timerlock
);
750 filt_timerunlock(void)
752 lck_mtx_unlock(&_filt_timerlock
);
756 * JMM - placeholder for not-yet-implemented filters
759 filt_badattach(__unused
struct knote
*kn
)
766 kqueue_alloc(struct proc
*p
)
768 struct filedesc
*fdp
= p
->p_fd
;
771 MALLOC_ZONE(kq
, struct kqueue
*, sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
);
773 bzero(kq
, sizeof(struct kqueue
));
774 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
);
775 TAILQ_INIT(&kq
->kq_head
);
776 TAILQ_INIT(&kq
->kq_inprocess
);
780 if (fdp
->fd_knlistsize
< 0) {
782 if (fdp
->fd_knlistsize
< 0)
783 fdp
->fd_knlistsize
= 0; /* this process has had a kq */
792 * kqueue_dealloc - detach all knotes from a kqueue and free it
794 * We walk each list looking for knotes referencing this
795 * this kqueue. If we find one, we try to drop it. But
796 * if we fail to get a drop reference, that will wait
797 * until it is dropped. So, we can just restart again
798 * safe in the assumption that the list will eventually
799 * not contain any more references to this kqueue (either
800 * we dropped them all, or someone else did).
802 * Assumes no new events are being added to the kqueue.
803 * Nothing locked on entry or exit.
806 kqueue_dealloc(struct kqueue
*kq
, struct proc
*p
)
808 struct filedesc
*fdp
= p
->p_fd
;
813 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
814 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
816 if (kq
== kn
->kn_kq
) {
819 /* drop it ourselves or wait */
820 if (kqlock2knotedrop(kq
, kn
)) {
821 kn
->kn_fop
->f_detach(kn
);
825 /* start over at beginning of list */
826 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
829 kn
= SLIST_NEXT(kn
, kn_link
);
832 if (fdp
->fd_knhashmask
!= 0) {
833 for (i
= 0; i
< (int)fdp
->fd_knhashmask
+ 1; i
++) {
834 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
836 if (kq
== kn
->kn_kq
) {
839 /* drop it ourselves or wait */
840 if (kqlock2knotedrop(kq
, kn
)) {
841 kn
->kn_fop
->f_detach(kn
);
845 /* start over at beginning of list */
846 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
849 kn
= SLIST_NEXT(kn
, kn_link
);
854 lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
);
855 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
859 kqueue(struct proc
*p
, __unused
struct kqueue_args
*uap
, register_t
*retval
)
865 error
= falloc(p
, &fp
, &fd
);
870 kq
= kqueue_alloc(p
);
876 fp
->f_flag
= FREAD
| FWRITE
;
877 fp
->f_type
= DTYPE_KQUEUE
;
878 fp
->f_ops
= &kqueueops
;
879 fp
->f_data
= (caddr_t
)kq
;
882 *fdflags(p
, fd
) &= ~UF_RESERVED
;
883 fp_drop(p
, fd
, fp
, 1);
891 kqueue_portset_np(__unused
struct proc
*p
,
892 __unused
struct kqueue_portset_np_args
*uap
,
893 __unused register_t
*retval
)
895 /* JMM - Placeholder for now */
900 kqueue_from_portset_np(__unused
struct proc
*p
,
901 __unused
struct kqueue_from_portset_np_args
*uap
,
902 __unused register_t
*retval
)
904 /* JMM - Placeholder for now */
909 kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
)
914 if (IS_64BIT_PROCESS(p
)) {
915 struct user_kevent kev64
;
917 advance
= sizeof(kev64
);
918 error
= copyin(*addrp
, (caddr_t
)&kev64
, advance
);
921 kevp
->ident
= CAST_DOWN(uintptr_t, kev64
.ident
);
922 kevp
->filter
= kev64
.filter
;
923 kevp
->flags
= kev64
.flags
;
924 kevp
->fflags
= kev64
.fflags
;
925 kevp
->data
= CAST_DOWN(intptr_t, kev64
.data
);
926 kevp
->udata
= kev64
.udata
;
929 * compensate for legacy in-kernel kevent layout
930 * where the udata field is alredy 64-bit.
932 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
933 error
= copyin(*addrp
, (caddr_t
)kevp
, advance
);
941 kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
)
946 if (IS_64BIT_PROCESS(p
)) {
947 struct user_kevent kev64
;
949 kev64
.ident
= (uint64_t) kevp
->ident
;
950 kev64
.filter
= kevp
->filter
;
951 kev64
.flags
= kevp
->flags
;
952 kev64
.fflags
= kevp
->fflags
;
953 kev64
.data
= (int64_t) kevp
->data
;
954 kev64
.udata
= kevp
->udata
;
955 advance
= sizeof(kev64
);
956 error
= copyout((caddr_t
)&kev64
, *addrp
, advance
);
959 * compensate for legacy in-kernel kevent layout
960 * where the udata field is alredy 64-bit.
962 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
963 error
= copyout((caddr_t
)kevp
, *addrp
, advance
);
971 * kevent_continue - continue a kevent syscall after blocking
973 * assume we inherit a use count on the kq fileglob.
977 kevent_continue(__unused
struct kqueue
*kq
, void *data
, int error
)
979 struct _kevent
*cont_args
;
984 struct proc
*p
= current_proc();
986 cont_args
= (struct _kevent
*)data
;
987 noutputs
= cont_args
->eventout
;
988 retval
= cont_args
->retval
;
992 fp_drop(p
, fd
, fp
, 0);
994 /* don't restart after signals... */
995 if (error
== ERESTART
)
997 else if (error
== EWOULDBLOCK
)
1001 unix_syscall_return(error
);
1005 * kevent - [syscall] register and wait for kernel events
1010 kevent(struct proc
*p
, struct kevent_args
*uap
, register_t
*retval
)
1012 user_addr_t changelist
= uap
->changelist
;
1013 user_addr_t ueventlist
= uap
->eventlist
;
1014 int nchanges
= uap
->nchanges
;
1015 int nevents
= uap
->nevents
;
1018 struct _kevent
*cont_args
;
1021 struct fileproc
*fp
;
1023 int error
, noutputs
;
1026 /* convert timeout to absolute - if we have one */
1027 if (uap
->timeout
!= USER_ADDR_NULL
) {
1029 if ( IS_64BIT_PROCESS(p
) ) {
1030 struct user_timespec ts
;
1031 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1032 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ull
) != 0)
1035 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1038 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1039 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1043 if (itimerfix(&rtv
))
1045 getmicrouptime(&atv
);
1046 timevaladd(&atv
, &rtv
);
1052 /* get a usecount for the kq itself */
1053 if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0)
1056 /* register all the change requests the user provided... */
1058 while (nchanges
> 0 && error
== 0) {
1059 error
= kevent_copyin(&changelist
, &kev
, p
);
1063 kev
.flags
&= ~EV_SYSFLAGS
;
1064 error
= kevent_register(kq
, &kev
, p
);
1065 if (error
&& nevents
> 0) {
1066 kev
.flags
= EV_ERROR
;
1068 error
= kevent_copyout(&kev
, &ueventlist
, p
);
1077 /* store the continuation/completion data in the uthread */
1078 ut
= (uthread_t
)get_bsdthread_info(current_thread());
1079 cont_args
= (struct _kevent
*)&ut
->uu_state
.ss_kevent
;
1082 cont_args
->retval
= retval
;
1083 cont_args
->eventlist
= ueventlist
;
1084 cont_args
->eventcount
= nevents
;
1085 cont_args
->eventout
= noutputs
;
1087 if (nevents
> 0 && noutputs
== 0 && error
== 0)
1088 error
= kevent_scan(kq
, kevent_callback
,
1089 kevent_continue
, cont_args
,
1091 kevent_continue(kq
, cont_args
, error
);
1098 * kevent_callback - callback for each individual event
1100 * called with nothing locked
1101 * caller holds a reference on the kqueue
1105 kevent_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1107 struct _kevent
*cont_args
;
1110 cont_args
= (struct _kevent
*)data
;
1111 assert(cont_args
->eventout
< cont_arg
->eventcount
);
1114 * Copy out the appropriate amount of event data for this user.
1116 error
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc());
1119 * If there isn't space for additional events, return
1120 * a harmless error to stop the processing here
1122 if (error
== 0 && ++cont_args
->eventout
== cont_args
->eventcount
)
1123 error
= EWOULDBLOCK
;
1128 * kevent_register - add a new event to a kqueue
1130 * Creates a mapping between the event source and
1131 * the kqueue via a knote data structure.
1133 * Because many/most the event sources are file
1134 * descriptor related, the knote is linked off
1135 * the filedescriptor table for quick access.
1137 * called with nothing locked
1138 * caller holds a reference on the kqueue
1142 kevent_register(struct kqueue
*kq
, struct kevent
*kev
, struct proc
*p
)
1144 struct filedesc
*fdp
= kq
->kq_fdp
;
1145 struct filterops
*fops
;
1146 struct fileproc
*fp
= NULL
;
1147 struct knote
*kn
= NULL
;
1150 if (kev
->filter
< 0) {
1151 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
1153 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
1157 * filter attach routine is responsible for insuring that
1158 * the identifier can be attached to it.
1160 printf("unknown filter: %d\n", kev
->filter
);
1164 /* this iocount needs to be dropped if it is not registered */
1165 if (fops
->f_isfd
&& (error
= fp_lookup(p
, kev
->ident
, &fp
, 0)) != 0)
1171 /* fd-based knotes are linked off the fd table */
1172 if (kev
->ident
< (u_int
)fdp
->fd_knlistsize
) {
1173 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
1174 if (kq
== kn
->kn_kq
&&
1175 kev
->filter
== kn
->kn_filter
)
1179 /* hash non-fd knotes here too */
1180 if (fdp
->fd_knhashmask
!= 0) {
1183 list
= &fdp
->fd_knhash
[
1184 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
1185 SLIST_FOREACH(kn
, list
, kn_link
)
1186 if (kev
->ident
== kn
->kn_id
&&
1188 kev
->filter
== kn
->kn_filter
)
1194 * kn now contains the matching knote, or NULL if no match
1197 if ((kev
->flags
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) {
1206 kn
->kn_tq
= &kq
->kq_head
;
1208 kn
->kn_sfflags
= kev
->fflags
;
1209 kn
->kn_sdata
= kev
->data
;
1212 kn
->kn_kevent
= *kev
;
1213 kn
->kn_inuse
= 1; /* for f_attach() */
1216 /* before anyone can find it */
1217 if (kev
->flags
& EV_DISABLE
)
1218 kn
->kn_status
|= KN_DISABLED
;
1220 error
= knote_fdpattach(kn
, fdp
, p
);
1229 * apply reference count to knote structure, and
1230 * do not release it at the end of this routine.
1235 * If the attach fails here, we can drop it knowing
1236 * that nobody else has a reference to the knote.
1238 if ((error
= fops
->f_attach(kn
)) != 0) {
1248 /* existing knote - get kqueue lock */
1252 if (kev
->flags
& EV_DELETE
) {
1254 kn
->kn_status
|= KN_DISABLED
;
1255 if (kqlock2knotedrop(kq
, kn
)) {
1256 kn
->kn_fop
->f_detach(kn
);
1262 /* update status flags for existing knote */
1263 if (kev
->flags
& EV_DISABLE
) {
1265 kn
->kn_status
|= KN_DISABLED
;
1266 } else if (kev
->flags
& EV_ENABLE
) {
1267 kn
->kn_status
&= ~KN_DISABLED
;
1268 if (kn
->kn_status
& KN_ACTIVE
)
1273 * If somebody is in the middle of dropping this
1274 * knote - go find/insert a new one. But we have
1275 * wait for this one to go away first.
1277 if (!kqlock2knoteusewait(kq
, kn
))
1278 /* kqueue unlocked */
1282 * The user may change some filter values after the
1283 * initial EV_ADD, but doing so will not reset any
1284 * filter which have already been triggered.
1286 kn
->kn_sfflags
= kev
->fflags
;
1287 kn
->kn_sdata
= kev
->data
;
1288 kn
->kn_kevent
.udata
= kev
->udata
;
1291 /* still have use ref on knote */
1292 if (kn
->kn_fop
->f_event(kn
, 0)) {
1293 if (knoteuse2kqlock(kq
, kn
))
1302 fp_drop(p
, kev
->ident
, fp
, 0);
1307 * kevent_process - process the triggered events in a kqueue
1309 * Walk the queued knotes and validate that they are
1310 * really still triggered events by calling the filter
1311 * routines (if necessary). Hold a use reference on
1312 * the knote to avoid it being detached. For each event
1313 * that is still considered triggered, invoke the
1314 * callback routine provided.
1316 * caller holds a reference on the kqueue.
1317 * kqueue locked on entry and exit - but may be dropped
1321 kevent_process(struct kqueue
*kq
,
1322 kevent_callback_t callback
,
1333 if (kq
->kq_count
== 0) {
1338 /* if someone else is processing the queue, wait */
1339 if (!TAILQ_EMPTY(&kq
->kq_inprocess
)) {
1340 assert_wait(&kq
->kq_inprocess
, THREAD_UNINT
);
1341 kq
->kq_state
|= KQ_PROCWAIT
;
1343 thread_block(THREAD_CONTINUE_NULL
);
1350 while (error
== 0 &&
1351 (kn
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
1354 * move knote to the processed queue.
1355 * this is also protected by the kq lock.
1357 assert(kn
->kn_tq
== &kq
->kq_head
);
1358 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1359 kn
->kn_tq
= &kq
->kq_inprocess
;
1360 TAILQ_INSERT_TAIL(&kq
->kq_inprocess
, kn
, kn_tqe
);
1363 * Non-EV_ONESHOT events must be re-validated.
1365 * Convert our lock to a use-count and call the event's
1366 * filter routine to update.
1368 * If the event is dropping (or no longer valid), we
1369 * already have it off the active queue, so just
1370 * finish the job of deactivating it.
1372 if ((kn
->kn_flags
& EV_ONESHOT
) == 0) {
1375 if (kqlock2knoteuse(kq
, kn
)) {
1377 /* call the filter with just a ref */
1378 result
= kn
->kn_fop
->f_event(kn
, 0);
1380 if (!knoteuse2kqlock(kq
, kn
) || result
== 0) {
1381 knote_deactivate(kn
);
1385 knote_deactivate(kn
);
1391 * Got a valid triggered knote with the kqueue
1392 * still locked. Snapshot the data, and determine
1393 * how to dispatch the knote for future events.
1395 kev
= kn
->kn_kevent
;
1397 /* now what happens to it? */
1398 if (kn
->kn_flags
& EV_ONESHOT
) {
1399 knote_deactivate(kn
);
1400 if (kqlock2knotedrop(kq
, kn
)) {
1401 kn
->kn_fop
->f_detach(kn
);
1404 } else if (kn
->kn_flags
& EV_CLEAR
) {
1405 knote_deactivate(kn
);
1411 * leave on in-process queue. We'll
1412 * move all the remaining ones back
1413 * the kq queue and wakeup any
1414 * waiters when we are done.
1419 /* callback to handle each event as we find it */
1420 error
= (callback
)(kq
, &kev
, data
);
1427 * With the kqueue still locked, move any knotes
1428 * remaining on the in-process queue back to the
1429 * kq's queue and wake up any waiters.
1431 while ((kn
= TAILQ_FIRST(&kq
->kq_inprocess
)) != NULL
) {
1432 assert(kn
->kn_tq
== &kq
->kq_inprocess
);
1433 TAILQ_REMOVE(&kq
->kq_inprocess
, kn
, kn_tqe
);
1434 kn
->kn_tq
= &kq
->kq_head
;
1435 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1437 if (kq
->kq_state
& KQ_PROCWAIT
) {
1438 kq
->kq_state
&= ~KQ_PROCWAIT
;
1439 thread_wakeup(&kq
->kq_inprocess
);
1448 kevent_scan_continue(void *data
, wait_result_t wait_result
)
1450 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1451 struct _kevent_scan
* cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1452 struct kqueue
*kq
= (struct kqueue
*)data
;
1456 /* convert the (previous) wait_result to a proper error */
1457 switch (wait_result
) {
1458 case THREAD_AWAKENED
:
1460 error
= kevent_process(kq
, cont_args
->call
, cont_args
, &count
, current_proc());
1461 if (error
== 0 && count
== 0) {
1462 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, cont_args
->deadline
);
1463 kq
->kq_state
|= KQ_SLEEP
;
1465 thread_block_parameter(kevent_scan_continue
, kq
);
1470 case THREAD_TIMED_OUT
:
1471 error
= EWOULDBLOCK
;
1473 case THREAD_INTERRUPTED
:
1477 panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result
);
1481 /* call the continuation with the results */
1482 assert(cont_args
->cont
!= NULL
);
1483 (cont_args
->cont
)(kq
, cont_args
->data
, error
);
1488 * kevent_scan - scan and wait for events in a kqueue
1490 * Process the triggered events in a kqueue.
1492 * If there are no events triggered arrange to
1493 * wait for them. If the caller provided a
1494 * continuation routine, then kevent_scan will
1497 * The callback routine must be valid.
1498 * The caller must hold a use-count reference on the kq.
1502 kevent_scan(struct kqueue
*kq
,
1503 kevent_callback_t callback
,
1504 kevent_continue_t continuation
,
1506 struct timeval
*atvp
,
1509 thread_continue_t cont
= THREAD_CONTINUE_NULL
;
1514 assert(callback
!= NULL
);
1518 wait_result_t wait_result
;
1522 * Make a pass through the kq to find events already
1526 error
= kevent_process(kq
, callback
, data
, &count
, p
);
1528 break; /* lock still held */
1530 /* looks like we have to consider blocking */
1533 /* convert the timeout to a deadline once */
1534 if (atvp
->tv_sec
|| atvp
->tv_usec
) {
1535 uint32_t seconds
, nanoseconds
;
1538 clock_get_uptime(&now
);
1539 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec
* NSEC_PER_SEC
+
1540 atvp
->tv_usec
* NSEC_PER_USEC
,
1542 if (now
>= deadline
) {
1543 /* non-blocking call */
1544 error
= EWOULDBLOCK
;
1545 break; /* lock still held */
1548 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
1550 deadline
= 0; /* block forever */
1554 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1555 struct _kevent_scan
*cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1557 cont_args
->call
= callback
;
1558 cont_args
->cont
= continuation
;
1559 cont_args
->deadline
= deadline
;
1560 cont_args
->data
= data
;
1561 cont
= kevent_scan_continue
;
1565 /* go ahead and wait */
1566 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, deadline
);
1567 kq
->kq_state
|= KQ_SLEEP
;
1569 wait_result
= thread_block_parameter(cont
, kq
);
1570 /* NOTREACHED if (continuation != NULL) */
1572 switch (wait_result
) {
1573 case THREAD_AWAKENED
:
1575 case THREAD_TIMED_OUT
:
1577 case THREAD_INTERRUPTED
:
1580 panic("kevent_scan - bad wait_result (%d)",
1592 * This could be expanded to call kqueue_scan, if desired.
1596 kqueue_read(__unused
struct fileproc
*fp
,
1597 __unused
struct uio
*uio
,
1598 __unused kauth_cred_t cred
,
1600 __unused
struct proc
*p
)
1607 kqueue_write(__unused
struct fileproc
*fp
,
1608 __unused
struct uio
*uio
,
1609 __unused kauth_cred_t cred
,
1611 __unused
struct proc
*p
)
1618 kqueue_ioctl(__unused
struct fileproc
*fp
,
1619 __unused u_long com
,
1620 __unused caddr_t data
,
1621 __unused
struct proc
*p
)
1628 kqueue_select(struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
)
1630 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1633 if (which
== FREAD
) {
1638 selrecord(p
, &kq
->kq_sel
, wql
);
1639 kq
->kq_state
|= KQ_SEL
;
1651 kqueue_close(struct fileglob
*fg
, struct proc
*p
)
1653 struct kqueue
*kq
= (struct kqueue
*)fg
->fg_data
;
1655 kqueue_dealloc(kq
, p
);
1662 * The callers has taken a use-count reference on this kqueue and will donate it
1663 * to the kqueue we are being added to. This keeps the kqueue from closing until
1664 * that relationship is torn down.
1667 kqueue_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
1669 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
1671 if (kn
->kn_filter
!= EVFILT_READ
)
1674 kn
->kn_fop
= &kqread_filtops
;
1676 KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
);
1683 kqueue_stat(struct fileproc
*fp
, struct stat
*st
, __unused
struct proc
*p
)
1685 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1687 bzero((void *)st
, sizeof(*st
));
1688 st
->st_size
= kq
->kq_count
;
1689 st
->st_blksize
= sizeof(struct kevent
);
1690 st
->st_mode
= S_IFIFO
;
1695 * Called with the kqueue locked
1698 kqueue_wakeup(struct kqueue
*kq
)
1701 if (kq
->kq_state
& KQ_SLEEP
) {
1702 kq
->kq_state
&= ~KQ_SLEEP
;
1705 if (kq
->kq_state
& KQ_SEL
) {
1706 kq
->kq_state
&= ~KQ_SEL
;
1707 selwakeup(&kq
->kq_sel
);
1709 KNOTE(&kq
->kq_sel
.si_note
, 0);
1713 klist_init(struct klist
*list
)
1720 * Query/Post each knote in the object's list
1722 * The object lock protects the list. It is assumed
1723 * that the filter/event routine for the object can
1724 * determine that the object is already locked (via
1725 * the hind) and not deadlock itself.
1727 * The object lock should also hold off pending
1728 * detach/drop operations. But we'll prevent it here
1729 * too - just in case.
1732 knote(struct klist
*list
, long hint
)
1736 SLIST_FOREACH(kn
, list
, kn_selnext
) {
1737 struct kqueue
*kq
= kn
->kn_kq
;
1740 if (kqlock2knoteuse(kq
, kn
)) {
1743 /* call the event with only a use count */
1744 result
= kn
->kn_fop
->f_event(kn
, hint
);
1746 /* if its not going away and triggered */
1747 if (knoteuse2kqlock(kq
, kn
) && result
)
1749 /* lock held again */
1756 * attach a knote to the specified list. Return true if this is the first entry.
1757 * The list is protected by whatever lock the object it is associated with uses.
1760 knote_attach(struct klist
*list
, struct knote
*kn
)
1762 int ret
= SLIST_EMPTY(list
);
1763 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
1768 * detach a knote from the specified list. Return true if that was the last entry.
1769 * The list is protected by whatever lock the object it is associated with uses.
1772 knote_detach(struct klist
*list
, struct knote
*kn
)
1774 SLIST_REMOVE(list
, kn
, knote
, kn_selnext
);
1775 return SLIST_EMPTY(list
);
1779 * remove all knotes referencing a specified fd
1781 * Essentially an inlined knote_remove & knote_drop
1782 * when we know for sure that the thing is a file
1784 * Entered with the proc_fd lock already held.
1785 * It returns the same way, but may drop it temporarily.
1788 knote_fdclose(struct proc
*p
, int fd
)
1790 struct filedesc
*fdp
= p
->p_fd
;
1794 list
= &fdp
->fd_knlist
[fd
];
1795 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
1796 struct kqueue
*kq
= kn
->kn_kq
;
1802 * Convert the lock to a drop ref.
1803 * If we get it, go ahead and drop it.
1804 * Otherwise, we waited for it to
1805 * be dropped by the other guy, so
1806 * it is safe to move on in the list.
1808 if (kqlock2knotedrop(kq
, kn
)) {
1809 kn
->kn_fop
->f_detach(kn
);
1815 /* the fd tables may have changed - start over */
1816 list
= &fdp
->fd_knlist
[fd
];
1820 /* proc_fdlock held on entry (and exit) */
1822 knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, __unused
struct proc
*p
)
1824 struct klist
*list
= NULL
;
1826 if (! kn
->kn_fop
->f_isfd
) {
1827 if (fdp
->fd_knhashmask
== 0)
1828 fdp
->fd_knhash
= hashinit(KN_HASHSIZE
, M_KQUEUE
,
1829 &fdp
->fd_knhashmask
);
1830 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1832 if ((u_int
)fdp
->fd_knlistsize
<= kn
->kn_id
) {
1835 /* have to grow the fd_knlist */
1836 size
= fdp
->fd_knlistsize
;
1837 while (size
<= kn
->kn_id
)
1839 MALLOC(list
, struct klist
*,
1840 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
1844 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
1845 fdp
->fd_knlistsize
* sizeof(struct klist
*));
1846 bzero((caddr_t
)list
+
1847 fdp
->fd_knlistsize
* sizeof(struct klist
*),
1848 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
1849 FREE(fdp
->fd_knlist
, M_KQUEUE
);
1850 fdp
->fd_knlist
= list
;
1851 fdp
->fd_knlistsize
= size
;
1853 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1855 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
1862 * should be called at spl == 0, since we don't want to hold spl
1863 * while calling fdrop and free.
1866 knote_drop(struct knote
*kn
, struct proc
*p
)
1868 struct filedesc
*fdp
= p
->p_fd
;
1869 struct kqueue
*kq
= kn
->kn_kq
;
1873 if (kn
->kn_fop
->f_isfd
)
1874 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1876 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1878 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
1881 if (kn
->kn_status
& KN_DROPWAIT
)
1882 thread_wakeup(&kn
->kn_status
);
1886 if (kn
->kn_fop
->f_isfd
)
1887 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0);
1892 /* called with kqueue lock held */
1894 knote_activate(struct knote
*kn
)
1896 struct kqueue
*kq
= kn
->kn_kq
;
1898 kn
->kn_status
|= KN_ACTIVE
;
1903 /* called with kqueue lock held */
1905 knote_deactivate(struct knote
*kn
)
1907 kn
->kn_status
&= ~KN_ACTIVE
;
1911 /* called with kqueue lock held */
1913 knote_enqueue(struct knote
*kn
)
1915 struct kqueue
*kq
= kn
->kn_kq
;
1917 if ((kn
->kn_status
& (KN_QUEUED
| KN_DISABLED
)) == 0) {
1918 struct kqtailq
*tq
= kn
->kn_tq
;
1920 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);
1921 kn
->kn_status
|= KN_QUEUED
;
1926 /* called with kqueue lock held */
1928 knote_dequeue(struct knote
*kn
)
1930 struct kqueue
*kq
= kn
->kn_kq
;
1932 assert((kn
->kn_status
& KN_DISABLED
) == 0);
1933 if ((kn
->kn_status
& KN_QUEUED
) == KN_QUEUED
) {
1934 struct kqtailq
*tq
= kn
->kn_tq
;
1936 TAILQ_REMOVE(tq
, kn
, kn_tqe
);
1937 kn
->kn_tq
= &kq
->kq_head
;
1938 kn
->kn_status
&= ~KN_QUEUED
;
1946 knote_zone
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
), 8192, "knote zone");
1948 /* allocate kq lock group attribute and group */
1949 kq_lck_grp_attr
= lck_grp_attr_alloc_init();
1950 lck_grp_attr_setstat(kq_lck_grp_attr
);
1952 kq_lck_grp
= lck_grp_alloc_init("kqueue", kq_lck_grp_attr
);
1954 /* Allocate kq lock attribute */
1955 kq_lck_attr
= lck_attr_alloc_init();
1956 lck_attr_setdefault(kq_lck_attr
);
1958 /* Initialize the timer filter lock */
1959 lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
);
1961 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
1963 static struct knote
*
1966 return ((struct knote
*)zalloc(knote_zone
));
1970 knote_free(struct knote
*kn
)
1972 zfree(knote_zone
, kn
);
1975 #include <sys/param.h>
1976 #include <sys/socket.h>
1977 #include <sys/protosw.h>
1978 #include <sys/domain.h>
1979 #include <sys/mbuf.h>
1980 #include <sys/kern_event.h>
1981 #include <sys/malloc.h>
1982 #include <sys/sys_domain.h>
1983 #include <sys/syslog.h>
1986 static int kev_attach(struct socket
*so
, int proto
, struct proc
*p
);
1987 static int kev_detach(struct socket
*so
);
1988 static int kev_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
);
1990 struct pr_usrreqs event_usrreqs
= {
1991 pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
,
1992 pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
,
1993 pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
1994 pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
1995 pru_sosend_notsupp
, soreceive
, pru_sopoll_notsupp
1998 struct protosw eventsw
[] = {
2000 SOCK_RAW
, &systemdomain
, SYSPROTO_EVENT
, PR_ATOMIC
,
2016 struct kern_event_head kern_event_head
;
2018 static u_long static_event_id
= 0;
2019 struct domain
*sysdom
= &systemdomain
;
2021 static lck_grp_t
*evt_mtx_grp
;
2022 static lck_attr_t
*evt_mtx_attr
;
2023 static lck_grp_attr_t
*evt_mtx_grp_attr
;
2024 lck_mtx_t
*evt_mutex
;
2026 * Install the protosw's for the NKE manager. Invoked at
2027 * extension load time
2030 kern_event_init(void)
2034 if ((retval
= net_add_proto(eventsw
, &systemdomain
)) != 0) {
2035 log(LOG_WARNING
, "Can't install kernel events protocol (%d)\n", retval
);
2040 * allocate lock group attribute and group for kern event
2042 evt_mtx_grp_attr
= lck_grp_attr_alloc_init();
2044 evt_mtx_grp
= lck_grp_alloc_init("eventlist", evt_mtx_grp_attr
);
2047 * allocate the lock attribute for mutexes
2049 evt_mtx_attr
= lck_attr_alloc_init();
2050 lck_attr_setdefault(evt_mtx_attr
);
2051 evt_mutex
= lck_mtx_alloc_init(evt_mtx_grp
, evt_mtx_attr
);
2052 if (evt_mutex
== NULL
)
2055 return(KERN_SUCCESS
);
2059 kev_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
2062 struct kern_event_pcb
*ev_pcb
;
2064 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
2068 MALLOC(ev_pcb
, struct kern_event_pcb
*, sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
);
2072 ev_pcb
->ev_socket
= so
;
2073 ev_pcb
->vendor_code_filter
= 0xffffffff;
2075 so
->so_pcb
= (caddr_t
) ev_pcb
;
2076 lck_mtx_lock(evt_mutex
);
2077 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
);
2078 lck_mtx_unlock(evt_mutex
);
2085 kev_detach(struct socket
*so
)
2087 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2090 lck_mtx_lock(evt_mutex
);
2091 LIST_REMOVE(ev_pcb
, ev_link
);
2092 lck_mtx_unlock(evt_mutex
);
2093 FREE(ev_pcb
, M_PCB
);
2095 so
->so_flags
|= SOF_PCBCLEARING
;
2102 * For now, kev_vender_code and mbuf_tags use the same
2105 extern errno_t
mbuf_tag_id_find_internal(const char *string
, u_long
*out_id
,
2108 errno_t
kev_vendor_code_find(
2110 u_long
*out_vender_code
)
2112 if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) {
2115 return mbuf_tag_id_find_internal(string
, out_vender_code
, 1);
2118 extern void mbuf_tag_id_first_last(u_long
*first
, u_long
*last
);
2120 errno_t
kev_msg_post(struct kev_msg
*event_msg
)
2122 u_long min_vendor
, max_vendor
;
2124 mbuf_tag_id_first_last(&min_vendor
, &max_vendor
);
2126 if (event_msg
== NULL
)
2129 /* Limit third parties to posting events for registered vendor codes only */
2130 if (event_msg
->vendor_code
< min_vendor
||
2131 event_msg
->vendor_code
> max_vendor
)
2136 return kev_post_msg(event_msg
);
2140 int kev_post_msg(struct kev_msg
*event_msg
)
2142 struct mbuf
*m
, *m2
;
2143 struct kern_event_pcb
*ev_pcb
;
2144 struct kern_event_msg
*ev
;
2146 unsigned long total_size
;
2149 /* Verify the message is small enough to fit in one mbuf w/o cluster */
2150 total_size
= KEV_MSG_HEADER_SIZE
;
2152 for (i
= 0; i
< 5; i
++) {
2153 if (event_msg
->dv
[i
].data_length
== 0)
2155 total_size
+= event_msg
->dv
[i
].data_length
;
2158 if (total_size
> MLEN
) {
2162 m
= m_get(M_DONTWAIT
, MT_DATA
);
2166 ev
= mtod(m
, struct kern_event_msg
*);
2167 total_size
= KEV_MSG_HEADER_SIZE
;
2169 tmp
= (char *) &ev
->event_data
[0];
2170 for (i
= 0; i
< 5; i
++) {
2171 if (event_msg
->dv
[i
].data_length
== 0)
2174 total_size
+= event_msg
->dv
[i
].data_length
;
2175 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
2176 event_msg
->dv
[i
].data_length
);
2177 tmp
+= event_msg
->dv
[i
].data_length
;
2180 ev
->id
= ++static_event_id
;
2181 ev
->total_size
= total_size
;
2182 ev
->vendor_code
= event_msg
->vendor_code
;
2183 ev
->kev_class
= event_msg
->kev_class
;
2184 ev
->kev_subclass
= event_msg
->kev_subclass
;
2185 ev
->event_code
= event_msg
->event_code
;
2187 m
->m_len
= total_size
;
2188 lck_mtx_lock(evt_mutex
);
2189 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
2191 ev_pcb
= LIST_NEXT(ev_pcb
, ev_link
)) {
2193 if (ev_pcb
->vendor_code_filter
!= KEV_ANY_VENDOR
) {
2194 if (ev_pcb
->vendor_code_filter
!= ev
->vendor_code
)
2197 if (ev_pcb
->class_filter
!= KEV_ANY_CLASS
) {
2198 if (ev_pcb
->class_filter
!= ev
->kev_class
)
2201 if ((ev_pcb
->subclass_filter
!= KEV_ANY_SUBCLASS
) &&
2202 (ev_pcb
->subclass_filter
!= ev
->kev_subclass
))
2207 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
2210 lck_mtx_unlock(evt_mutex
);
2213 socket_lock(ev_pcb
->ev_socket
, 1);
2214 if (sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
))
2215 sorwakeup(ev_pcb
->ev_socket
);
2216 socket_unlock(ev_pcb
->ev_socket
, 1);
2220 lck_mtx_unlock(evt_mutex
);
2225 kev_control(struct socket
*so
,
2228 __unused
struct ifnet
*ifp
,
2229 __unused
struct proc
*p
)
2231 struct kev_request
*kev_req
= (struct kev_request
*) data
;
2232 struct kern_event_pcb
*ev_pcb
;
2233 struct kev_vendor_code
*kev_vendor
;
2234 u_long
*id_value
= (u_long
*) data
;
2240 *id_value
= static_event_id
;
2244 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2245 ev_pcb
->vendor_code_filter
= kev_req
->vendor_code
;
2246 ev_pcb
->class_filter
= kev_req
->kev_class
;
2247 ev_pcb
->subclass_filter
= kev_req
->kev_subclass
;
2251 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2252 kev_req
->vendor_code
= ev_pcb
->vendor_code_filter
;
2253 kev_req
->kev_class
= ev_pcb
->class_filter
;
2254 kev_req
->kev_subclass
= ev_pcb
->subclass_filter
;
2257 case SIOCGKEVVENDOR
:
2258 kev_vendor
= (struct kev_vendor_code
*)data
;
2260 /* Make sure string is NULL terminated */
2261 kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0;
2263 return mbuf_tag_id_find_internal(kev_vendor
->vendor_string
,
2264 &kev_vendor
->vendor_code
, 0);