2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * @(#)kern_event.c 1.0 (3/31/2000)
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/proc_internal.h>
59 #include <sys/kauth.h>
60 #include <sys/malloc.h>
61 #include <sys/unistd.h>
62 #include <sys/file_internal.h>
63 #include <sys/fcntl.h>
64 #include <sys/select.h>
65 #include <sys/queue.h>
66 #include <sys/event.h>
67 #include <sys/eventvar.h>
68 #include <sys/protosw.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
72 #include <sys/sysctl.h>
74 #include <sys/sysproto.h>
78 #include <kern/lock.h>
79 #include <kern/clock.h>
80 #include <kern/thread_call.h>
81 #include <kern/sched_prim.h>
82 #include <kern/zalloc.h>
83 #include <kern/assert.h>
85 #include <libkern/libkern.h>
87 extern void unix_syscall_return(int);
89 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system");
91 static inline void kqlock(struct kqueue
*kq
);
92 static inline void kqunlock(struct kqueue
*kq
);
94 static int kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
);
95 static int kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
);
96 static int kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
);
97 static int knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
);
99 static void kqueue_wakeup(struct kqueue
*kq
);
100 static int kqueue_read(struct fileproc
*fp
, struct uio
*uio
,
101 kauth_cred_t cred
, int flags
, struct proc
*p
);
102 static int kqueue_write(struct fileproc
*fp
, struct uio
*uio
,
103 kauth_cred_t cred
, int flags
, struct proc
*p
);
104 static int kqueue_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
106 static int kqueue_select(struct fileproc
*fp
, int which
, void *wql
,
108 static int kqueue_close(struct fileglob
*fp
, struct proc
*p
);
109 static int kqueue_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
);
110 extern int kqueue_stat(struct fileproc
*fp
, struct stat
*st
, struct proc
*p
);
112 static struct fileops kqueueops
= {
122 static int kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
);
123 static int kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
);
125 static int kevent_callback(struct kqueue
*kq
, struct kevent
*kevp
, void *data
);
126 static void kevent_continue(struct kqueue
*kq
, void *data
, int error
);
127 static void kevent_scan_continue(void *contp
, wait_result_t wait_result
);
128 static int kevent_process(struct kqueue
*kq
, kevent_callback_t callback
,
129 void *data
, int *countp
, struct proc
*p
);
130 static void knote_put(struct knote
*kn
);
131 static int knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
);
132 static void knote_drop(struct knote
*kn
, struct proc
*p
);
133 static void knote_activate(struct knote
*kn
);
134 static void knote_deactivate(struct knote
*kn
);
135 static void knote_enqueue(struct knote
*kn
);
136 static void knote_dequeue(struct knote
*kn
);
137 static struct knote
*knote_alloc(void);
138 static void knote_free(struct knote
*kn
);
139 extern void knote_init(void);
141 static int filt_fileattach(struct knote
*kn
);
142 static struct filterops file_filtops
=
143 { 1, filt_fileattach
, NULL
, NULL
};
145 static void filt_kqdetach(struct knote
*kn
);
146 static int filt_kqueue(struct knote
*kn
, long hint
);
147 static struct filterops kqread_filtops
=
148 { 1, NULL
, filt_kqdetach
, filt_kqueue
};
151 * placeholder for not-yet-implemented filters
153 static int filt_badattach(struct knote
*kn
);
154 static struct filterops bad_filtops
=
155 { 0, filt_badattach
, 0 , 0 };
157 static int filt_procattach(struct knote
*kn
);
158 static void filt_procdetach(struct knote
*kn
);
159 static int filt_proc(struct knote
*kn
, long hint
);
161 static struct filterops proc_filtops
=
162 { 0, filt_procattach
, filt_procdetach
, filt_proc
};
164 extern struct filterops fs_filtops
;
166 extern struct filterops sig_filtops
;
170 static int filt_timercompute(struct knote
*kn
, uint64_t *abs_time
);
171 static void filt_timerexpire(void *knx
, void *param1
);
172 static int filt_timerattach(struct knote
*kn
);
173 static void filt_timerdetach(struct knote
*kn
);
174 static int filt_timer(struct knote
*kn
, long hint
);
176 static struct filterops timer_filtops
=
177 { 0, filt_timerattach
, filt_timerdetach
, filt_timer
};
179 /* to avoid arming timers that fire quicker than we can handle */
180 static uint64_t filt_timerfloor
= 0;
182 static lck_mtx_t _filt_timerlock
;
183 static void filt_timerlock(void);
184 static void filt_timerunlock(void);
187 * Sentinel marker for a thread scanning through the list of
190 static struct filterops threadmarker_filtops
=
191 { 0, filt_badattach
, 0, 0 };
193 static zone_t knote_zone
;
195 #define KN_HASHSIZE 64 /* XXX should be tunable */
196 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
199 extern struct filterops aio_filtops
;
203 * Table for for all system-defined filters.
205 static struct filterops
*sysfilt_ops
[] = {
206 &file_filtops
, /* EVFILT_READ */
207 &file_filtops
, /* EVFILT_WRITE */
209 &aio_filtops
, /* EVFILT_AIO */
211 &bad_filtops
, /* EVFILT_AIO */
213 &file_filtops
, /* EVFILT_VNODE */
214 &proc_filtops
, /* EVFILT_PROC */
215 &sig_filtops
, /* EVFILT_SIGNAL */
216 &timer_filtops
, /* EVFILT_TIMER */
217 &bad_filtops
, /* EVFILT_MACHPORT */
218 &fs_filtops
/* EVFILT_FS */
222 * kqueue/note lock attributes and implementations
224 * kqueues have locks, while knotes have use counts
225 * Most of the knote state is guarded by the object lock.
226 * the knote "inuse" count and status use the kqueue lock.
228 lck_grp_attr_t
* kq_lck_grp_attr
;
229 lck_grp_t
* kq_lck_grp
;
230 lck_attr_t
* kq_lck_attr
;
233 kqlock(struct kqueue
*kq
)
235 lck_spin_lock(&kq
->kq_lock
);
239 kqunlock(struct kqueue
*kq
)
241 lck_spin_unlock(&kq
->kq_lock
);
245 * Convert a kq lock to a knote use referece.
247 * If the knote is being dropped, we can't get
248 * a use reference, so just return with it
251 * - kq locked at entry
252 * - unlock on exit if we get the use reference
255 kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
)
257 if (kn
->kn_status
& KN_DROPPING
)
265 * Convert a kq lock to a knote use referece.
267 * If the knote is being dropped, we can't get
268 * a use reference, so just return with it
271 * - kq locked at entry
272 * - kq always unlocked on exit
275 kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
)
277 if (!kqlock2knoteuse(kq
, kn
)) {
278 kn
->kn_status
|= KN_DROPWAIT
;
279 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
281 thread_block(THREAD_CONTINUE_NULL
);
288 * Convert from a knote use reference back to kq lock.
290 * Drop a use reference and wake any waiters if
291 * this is the last one.
293 * The exit return indicates if the knote is
294 * still alive - but the kqueue lock is taken
298 knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
)
301 if ((--kn
->kn_inuse
== 0) &&
302 (kn
->kn_status
& KN_USEWAIT
)) {
303 kn
->kn_status
&= ~KN_USEWAIT
;
304 thread_wakeup(&kn
->kn_inuse
);
306 return ((kn
->kn_status
& KN_DROPPING
) == 0);
310 * Convert a kq lock to a knote drop referece.
312 * If the knote is in use, wait for the use count
313 * to subside. We first mark our intention to drop
314 * it - keeping other users from "piling on."
315 * If we are too late, we have to wait for the
316 * other drop to complete.
318 * - kq locked at entry
319 * - always unlocked on exit.
320 * - caller can't hold any locks that would prevent
321 * the other dropper from completing.
324 kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
)
327 if ((kn
->kn_status
& KN_DROPPING
) == 0) {
328 kn
->kn_status
|= KN_DROPPING
;
329 if (kn
->kn_inuse
> 0) {
330 kn
->kn_status
|= KN_USEWAIT
;
331 assert_wait(&kn
->kn_inuse
, THREAD_UNINT
);
333 thread_block(THREAD_CONTINUE_NULL
);
338 kn
->kn_status
|= KN_DROPWAIT
;
339 assert_wait(&kn
->kn_status
, THREAD_UNINT
);
341 thread_block(THREAD_CONTINUE_NULL
);
347 * Release a knote use count reference.
350 knote_put(struct knote
*kn
)
352 struct kqueue
*kq
= kn
->kn_kq
;
355 if ((--kn
->kn_inuse
== 0) &&
356 (kn
->kn_status
& KN_USEWAIT
)) {
357 kn
->kn_status
&= ~KN_USEWAIT
;
358 thread_wakeup(&kn
->kn_inuse
);
366 filt_fileattach(struct knote
*kn
)
369 return (fo_kqfilter(kn
->kn_fp
, kn
, current_proc()));
372 #define f_flag f_fglob->fg_flag
373 #define f_type f_fglob->fg_type
374 #define f_msgcount f_fglob->fg_msgcount
375 #define f_cred f_fglob->fg_cred
376 #define f_ops f_fglob->fg_ops
377 #define f_offset f_fglob->fg_offset
378 #define f_data f_fglob->fg_data
381 filt_kqdetach(struct knote
*kn
)
383 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
386 KNOTE_DETACH(&kq
->kq_sel
.si_note
, kn
);
392 filt_kqueue(struct knote
*kn
, __unused
long hint
)
394 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
396 kn
->kn_data
= kq
->kq_count
;
397 return (kn
->kn_data
> 0);
401 filt_procattach(struct knote
*kn
)
406 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
408 p
= pfind(kn
->kn_id
);
410 thread_funnel_set(kernel_flock
, funnel_state
);
414 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
417 * internal flag indicating registration done by kernel
419 if (kn
->kn_flags
& EV_FLAG1
) {
420 kn
->kn_data
= (int)kn
->kn_sdata
; /* ppid */
421 kn
->kn_fflags
= NOTE_CHILD
;
422 kn
->kn_flags
&= ~EV_FLAG1
;
425 /* XXX lock the proc here while adding to the list? */
426 KNOTE_ATTACH(&p
->p_klist
, kn
);
428 thread_funnel_set(kernel_flock
, funnel_state
);
434 * The knote may be attached to a different process, which may exit,
435 * leaving nothing for the knote to be attached to. So when the process
436 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
437 * it will be deleted when read out. However, as part of the knote deletion,
438 * this routine is called, so a check is needed to avoid actually performing
439 * a detach, because the original process does not exist any more.
442 filt_procdetach(struct knote
*kn
)
447 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
448 p
= pfind(kn
->kn_id
);
450 if (p
!= (struct proc
*)NULL
)
451 KNOTE_DETACH(&p
->p_klist
, kn
);
453 thread_funnel_set(kernel_flock
, funnel_state
);
457 filt_proc(struct knote
*kn
, long hint
)
462 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
465 * mask off extra data
467 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
470 * if the user is interested in this event, record it.
472 if (kn
->kn_sfflags
& event
)
473 kn
->kn_fflags
|= event
;
476 * process is gone, so flag the event as finished.
478 if (event
== NOTE_EXIT
) {
479 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
480 thread_funnel_set(kernel_flock
, funnel_state
);
485 * process forked, and user wants to track the new process,
486 * so attach a new knote to it, and immediately report an
487 * event with the parent's pid.
489 if ((event
== NOTE_FORK
) && (kn
->kn_sfflags
& NOTE_TRACK
)) {
494 * register knote with new process.
496 kev
.ident
= hint
& NOTE_PDATAMASK
; /* pid */
497 kev
.filter
= kn
->kn_filter
;
498 kev
.flags
= kn
->kn_flags
| EV_ADD
| EV_ENABLE
| EV_FLAG1
;
499 kev
.fflags
= kn
->kn_sfflags
;
500 kev
.data
= kn
->kn_id
; /* parent */
501 kev
.udata
= kn
->kn_kevent
.udata
; /* preserve udata */
502 error
= kevent_register(kn
->kn_kq
, &kev
, NULL
);
504 kn
->kn_fflags
|= NOTE_TRACKERR
;
506 event
= kn
->kn_fflags
;
507 thread_funnel_set(kernel_flock
, funnel_state
);
513 * filt_timercompute - compute absolute timeout
515 * The saved-data field in the knote contains the
516 * time value. The saved filter-flags indicates
517 * the unit of measurement.
519 * If the timeout is not absolute, adjust it for
523 filt_timercompute(struct knote
*kn
, uint64_t *abs_time
)
528 switch (kn
->kn_sfflags
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) {
530 multiplier
= NSEC_PER_SEC
;
533 multiplier
= NSEC_PER_USEC
;
538 case 0: /* milliseconds (default) */
539 multiplier
= NSEC_PER_SEC
/ 1000;
544 nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata
* multiplier
, &raw
);
545 if (raw
<= filt_timerfloor
) {
549 if ((kn
->kn_sfflags
& NOTE_ABSOLUTE
) == NOTE_ABSOLUTE
) {
550 uint32_t seconds
, nanoseconds
;
553 clock_get_calendar_nanotime(&seconds
, &nanoseconds
);
554 nanoseconds_to_absolutetime((uint64_t)seconds
* NSEC_PER_SEC
+ nanoseconds
,
556 if (now
>= raw
+ filt_timerfloor
) {
562 clock_absolutetime_interval_to_deadline(raw
, abs_time
);
567 * filt_timerexpire - the timer callout routine
569 * Just propagate the timer event into the knote
570 * filter routine (by going through the knote
571 * synchronization point). Pass a hint to
572 * indicate this is a real event, not just a
576 filt_timerexpire(void *knx
, __unused
void *spare
)
578 struct klist timer_list
;
579 struct knote
*kn
= knx
;
581 /* no "object" for timers, so fake a list */
582 SLIST_INIT(&timer_list
);
583 SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);
584 KNOTE(&timer_list
, 1);
588 * data contains amount of time to sleep, in milliseconds,
589 * or a pointer to a timespec structure.
592 filt_timerattach(struct knote
*kn
)
594 thread_call_t callout
;
598 error
= filt_timercompute(kn
, &deadline
);
603 callout
= thread_call_allocate(filt_timerexpire
, kn
);
607 /* handle as immediate */
613 kn
->kn_hook
= (caddr_t
)callout
;
615 /* absolute=EV_ONESHOT */
616 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
)
617 kn
->kn_flags
|= EV_ONESHOT
;
620 /* all others - if not faking immediate */
621 kn
->kn_flags
|= EV_CLEAR
;
622 thread_call_enter_delayed(callout
, deadline
);
633 filt_timerdetach(struct knote
*kn
)
635 thread_call_t callout
;
638 callout
= (thread_call_t
)kn
->kn_hook
;
639 if (callout
!= NULL
) {
642 /* cancel the callout if we can */
643 cancelled
= thread_call_cancel(callout
);
645 /* got it, just free it */
648 thread_call_free(callout
);
651 /* we have to wait for the expire routine. */
652 kn
->kn_hookid
= -1; /* we are detaching */
653 assert_wait(&kn
->kn_hook
, THREAD_UNINT
);
655 thread_block(THREAD_CONTINUE_NULL
);
656 assert(kn
->kn_hook
== NULL
);
666 filt_timer(struct knote
*kn
, __unused
long hint
)
672 thread_call_t callout
;
679 detaching
= (kn
->kn_hookid
< 0);
680 callout
= (thread_call_t
)kn
->kn_hook
;
682 if (!detaching
&& (kn
->kn_flags
& EV_ONESHOT
) == 0) {
686 /* user input data may have changed - deal */
687 error
= filt_timercompute(kn
, &deadline
);
689 kn
->kn_flags
|= EV_ERROR
;
691 } else if (deadline
== 0) {
692 /* revert to fake immediate */
693 kn
->kn_flags
&= ~EV_CLEAR
;
697 /* keep the callout and re-arm */
698 thread_call_enter_delayed(callout
, deadline
);
705 thread_call_free(callout
);
707 /* if someone is waiting for timer to pop */
709 thread_wakeup(&kn
->kn_hook
);
717 /* change fake timer to real if needed */
718 while (kn
->kn_hookid
> 0 && kn
->kn_sdata
> 0) {
721 /* update the fake timer (make real) */
725 error
= filt_timerattach(kn
);
728 kn
->kn_flags
|= EV_ERROR
;
735 /* if still fake, pretend it fired */
736 if (kn
->kn_hookid
> 0)
739 result
= (kn
->kn_data
!= 0);
747 lck_mtx_lock(&_filt_timerlock
);
751 filt_timerunlock(void)
753 lck_mtx_unlock(&_filt_timerlock
);
757 * JMM - placeholder for not-yet-implemented filters
760 filt_badattach(__unused
struct knote
*kn
)
767 kqueue_alloc(struct proc
*p
)
769 struct filedesc
*fdp
= p
->p_fd
;
772 MALLOC_ZONE(kq
, struct kqueue
*, sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
);
774 bzero(kq
, sizeof(struct kqueue
));
775 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
);
776 TAILQ_INIT(&kq
->kq_head
);
777 TAILQ_INIT(&kq
->kq_inprocess
);
781 if (fdp
->fd_knlistsize
< 0) {
783 if (fdp
->fd_knlistsize
< 0)
784 fdp
->fd_knlistsize
= 0; /* this process has had a kq */
793 * kqueue_dealloc - detach all knotes from a kqueue and free it
795 * We walk each list looking for knotes referencing this
796 * this kqueue. If we find one, we try to drop it. But
797 * if we fail to get a drop reference, that will wait
798 * until it is dropped. So, we can just restart again
799 * safe in the assumption that the list will eventually
800 * not contain any more references to this kqueue (either
801 * we dropped them all, or someone else did).
803 * Assumes no new events are being added to the kqueue.
804 * Nothing locked on entry or exit.
807 kqueue_dealloc(struct kqueue
*kq
, struct proc
*p
)
809 struct filedesc
*fdp
= p
->p_fd
;
814 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
815 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
817 if (kq
== kn
->kn_kq
) {
820 /* drop it ourselves or wait */
821 if (kqlock2knotedrop(kq
, kn
)) {
822 kn
->kn_fop
->f_detach(kn
);
826 /* start over at beginning of list */
827 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
830 kn
= SLIST_NEXT(kn
, kn_link
);
833 if (fdp
->fd_knhashmask
!= 0) {
834 for (i
= 0; i
< (int)fdp
->fd_knhashmask
+ 1; i
++) {
835 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
837 if (kq
== kn
->kn_kq
) {
840 /* drop it ourselves or wait */
841 if (kqlock2knotedrop(kq
, kn
)) {
842 kn
->kn_fop
->f_detach(kn
);
846 /* start over at beginning of list */
847 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
850 kn
= SLIST_NEXT(kn
, kn_link
);
855 lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
);
856 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
860 kqueue(struct proc
*p
, __unused
struct kqueue_args
*uap
, register_t
*retval
)
866 error
= falloc(p
, &fp
, &fd
);
871 kq
= kqueue_alloc(p
);
877 fp
->f_flag
= FREAD
| FWRITE
;
878 fp
->f_type
= DTYPE_KQUEUE
;
879 fp
->f_ops
= &kqueueops
;
880 fp
->f_data
= (caddr_t
)kq
;
883 *fdflags(p
, fd
) &= ~UF_RESERVED
;
884 fp_drop(p
, fd
, fp
, 1);
892 kqueue_portset_np(__unused
struct proc
*p
,
893 __unused
struct kqueue_portset_np_args
*uap
,
894 __unused register_t
*retval
)
896 /* JMM - Placeholder for now */
901 kqueue_from_portset_np(__unused
struct proc
*p
,
902 __unused
struct kqueue_from_portset_np_args
*uap
,
903 __unused register_t
*retval
)
905 /* JMM - Placeholder for now */
910 kevent_copyin(user_addr_t
*addrp
, struct kevent
*kevp
, struct proc
*p
)
915 if (IS_64BIT_PROCESS(p
)) {
916 struct user_kevent kev64
;
918 advance
= sizeof(kev64
);
919 error
= copyin(*addrp
, (caddr_t
)&kev64
, advance
);
922 kevp
->ident
= CAST_DOWN(uintptr_t, kev64
.ident
);
923 kevp
->filter
= kev64
.filter
;
924 kevp
->flags
= kev64
.flags
;
925 kevp
->fflags
= kev64
.fflags
;
926 kevp
->data
= CAST_DOWN(intptr_t, kev64
.data
);
927 kevp
->udata
= kev64
.udata
;
930 * compensate for legacy in-kernel kevent layout
931 * where the udata field is alredy 64-bit.
933 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
934 error
= copyin(*addrp
, (caddr_t
)kevp
, advance
);
942 kevent_copyout(struct kevent
*kevp
, user_addr_t
*addrp
, struct proc
*p
)
947 if (IS_64BIT_PROCESS(p
)) {
948 struct user_kevent kev64
;
950 kev64
.ident
= (uint64_t) kevp
->ident
;
951 kev64
.filter
= kevp
->filter
;
952 kev64
.flags
= kevp
->flags
;
953 kev64
.fflags
= kevp
->fflags
;
954 kev64
.data
= (int64_t) kevp
->data
;
955 kev64
.udata
= kevp
->udata
;
956 advance
= sizeof(kev64
);
957 error
= copyout((caddr_t
)&kev64
, *addrp
, advance
);
960 * compensate for legacy in-kernel kevent layout
961 * where the udata field is alredy 64-bit.
963 advance
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
);
964 error
= copyout((caddr_t
)kevp
, *addrp
, advance
);
972 * kevent_continue - continue a kevent syscall after blocking
974 * assume we inherit a use count on the kq fileglob.
978 kevent_continue(__unused
struct kqueue
*kq
, void *data
, int error
)
980 struct _kevent
*cont_args
;
985 struct proc
*p
= current_proc();
987 cont_args
= (struct _kevent
*)data
;
988 noutputs
= cont_args
->eventout
;
989 retval
= cont_args
->retval
;
993 fp_drop(p
, fd
, fp
, 0);
995 /* don't restart after signals... */
996 if (error
== ERESTART
)
998 else if (error
== EWOULDBLOCK
)
1002 unix_syscall_return(error
);
1006 * kevent - [syscall] register and wait for kernel events
1011 kevent(struct proc
*p
, struct kevent_args
*uap
, register_t
*retval
)
1013 user_addr_t changelist
= uap
->changelist
;
1014 user_addr_t ueventlist
= uap
->eventlist
;
1015 int nchanges
= uap
->nchanges
;
1016 int nevents
= uap
->nevents
;
1019 struct _kevent
*cont_args
;
1022 struct fileproc
*fp
;
1024 int error
, noutputs
;
1027 /* convert timeout to absolute - if we have one */
1028 if (uap
->timeout
!= USER_ADDR_NULL
) {
1030 if ( IS_64BIT_PROCESS(p
) ) {
1031 struct user_timespec ts
;
1032 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1033 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ull
) != 0)
1036 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1039 error
= copyin( uap
->timeout
, &ts
, sizeof(ts
) );
1040 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1044 if (itimerfix(&rtv
))
1046 getmicrouptime(&atv
);
1047 timevaladd(&atv
, &rtv
);
1053 /* get a usecount for the kq itself */
1054 if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0)
1057 /* register all the change requests the user provided... */
1059 while (nchanges
> 0 && error
== 0) {
1060 error
= kevent_copyin(&changelist
, &kev
, p
);
1064 kev
.flags
&= ~EV_SYSFLAGS
;
1065 error
= kevent_register(kq
, &kev
, p
);
1066 if (error
&& nevents
> 0) {
1067 kev
.flags
= EV_ERROR
;
1069 error
= kevent_copyout(&kev
, &ueventlist
, p
);
1078 /* store the continuation/completion data in the uthread */
1079 ut
= (uthread_t
)get_bsdthread_info(current_thread());
1080 cont_args
= (struct _kevent
*)&ut
->uu_state
.ss_kevent
;
1083 cont_args
->retval
= retval
;
1084 cont_args
->eventlist
= ueventlist
;
1085 cont_args
->eventcount
= nevents
;
1086 cont_args
->eventout
= noutputs
;
1088 if (nevents
> 0 && noutputs
== 0 && error
== 0)
1089 error
= kevent_scan(kq
, kevent_callback
,
1090 kevent_continue
, cont_args
,
1092 kevent_continue(kq
, cont_args
, error
);
1099 * kevent_callback - callback for each individual event
1101 * called with nothing locked
1102 * caller holds a reference on the kqueue
1106 kevent_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1108 struct _kevent
*cont_args
;
1111 cont_args
= (struct _kevent
*)data
;
1112 assert(cont_args
->eventout
< cont_arg
->eventcount
);
1115 * Copy out the appropriate amount of event data for this user.
1117 error
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc());
1120 * If there isn't space for additional events, return
1121 * a harmless error to stop the processing here
1123 if (error
== 0 && ++cont_args
->eventout
== cont_args
->eventcount
)
1124 error
= EWOULDBLOCK
;
1129 * kevent_register - add a new event to a kqueue
1131 * Creates a mapping between the event source and
1132 * the kqueue via a knote data structure.
1134 * Because many/most the event sources are file
1135 * descriptor related, the knote is linked off
1136 * the filedescriptor table for quick access.
1138 * called with nothing locked
1139 * caller holds a reference on the kqueue
1143 kevent_register(struct kqueue
*kq
, struct kevent
*kev
, struct proc
*p
)
1145 struct filedesc
*fdp
= kq
->kq_fdp
;
1146 struct filterops
*fops
;
1147 struct fileproc
*fp
= NULL
;
1148 struct knote
*kn
= NULL
;
1151 if (kev
->filter
< 0) {
1152 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
1154 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
1158 * filter attach routine is responsible for insuring that
1159 * the identifier can be attached to it.
1161 printf("unknown filter: %d\n", kev
->filter
);
1165 /* this iocount needs to be dropped if it is not registered */
1166 if (fops
->f_isfd
&& (error
= fp_lookup(p
, kev
->ident
, &fp
, 0)) != 0)
1172 /* fd-based knotes are linked off the fd table */
1173 if (kev
->ident
< (u_int
)fdp
->fd_knlistsize
) {
1174 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
1175 if (kq
== kn
->kn_kq
&&
1176 kev
->filter
== kn
->kn_filter
)
1180 /* hash non-fd knotes here too */
1181 if (fdp
->fd_knhashmask
!= 0) {
1184 list
= &fdp
->fd_knhash
[
1185 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
1186 SLIST_FOREACH(kn
, list
, kn_link
)
1187 if (kev
->ident
== kn
->kn_id
&&
1189 kev
->filter
== kn
->kn_filter
)
1195 * kn now contains the matching knote, or NULL if no match
1198 if ((kev
->flags
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) {
1207 kn
->kn_tq
= &kq
->kq_head
;
1209 kn
->kn_sfflags
= kev
->fflags
;
1210 kn
->kn_sdata
= kev
->data
;
1213 kn
->kn_kevent
= *kev
;
1214 kn
->kn_inuse
= 1; /* for f_attach() */
1217 /* before anyone can find it */
1218 if (kev
->flags
& EV_DISABLE
)
1219 kn
->kn_status
|= KN_DISABLED
;
1221 error
= knote_fdpattach(kn
, fdp
, p
);
1230 * apply reference count to knote structure, and
1231 * do not release it at the end of this routine.
1236 * If the attach fails here, we can drop it knowing
1237 * that nobody else has a reference to the knote.
1239 if ((error
= fops
->f_attach(kn
)) != 0) {
1249 /* existing knote - get kqueue lock */
1253 if (kev
->flags
& EV_DELETE
) {
1255 kn
->kn_status
|= KN_DISABLED
;
1256 if (kqlock2knotedrop(kq
, kn
)) {
1257 kn
->kn_fop
->f_detach(kn
);
1263 /* update status flags for existing knote */
1264 if (kev
->flags
& EV_DISABLE
) {
1266 kn
->kn_status
|= KN_DISABLED
;
1267 } else if (kev
->flags
& EV_ENABLE
) {
1268 kn
->kn_status
&= ~KN_DISABLED
;
1269 if (kn
->kn_status
& KN_ACTIVE
)
1274 * If somebody is in the middle of dropping this
1275 * knote - go find/insert a new one. But we have
1276 * wait for this one to go away first.
1278 if (!kqlock2knoteusewait(kq
, kn
))
1279 /* kqueue unlocked */
1283 * The user may change some filter values after the
1284 * initial EV_ADD, but doing so will not reset any
1285 * filter which have already been triggered.
1287 kn
->kn_sfflags
= kev
->fflags
;
1288 kn
->kn_sdata
= kev
->data
;
1289 kn
->kn_kevent
.udata
= kev
->udata
;
1292 /* still have use ref on knote */
1293 if (kn
->kn_fop
->f_event(kn
, 0)) {
1294 if (knoteuse2kqlock(kq
, kn
))
1303 fp_drop(p
, kev
->ident
, fp
, 0);
1308 * kevent_process - process the triggered events in a kqueue
1310 * Walk the queued knotes and validate that they are
1311 * really still triggered events by calling the filter
1312 * routines (if necessary). Hold a use reference on
1313 * the knote to avoid it being detached. For each event
1314 * that is still considered triggered, invoke the
1315 * callback routine provided.
1317 * caller holds a reference on the kqueue.
1318 * kqueue locked on entry and exit - but may be dropped
1322 kevent_process(struct kqueue
*kq
,
1323 kevent_callback_t callback
,
1334 if (kq
->kq_count
== 0) {
1339 /* if someone else is processing the queue, wait */
1340 if (!TAILQ_EMPTY(&kq
->kq_inprocess
)) {
1341 assert_wait(&kq
->kq_inprocess
, THREAD_UNINT
);
1342 kq
->kq_state
|= KQ_PROCWAIT
;
1344 thread_block(THREAD_CONTINUE_NULL
);
1351 while (error
== 0 &&
1352 (kn
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
1355 * move knote to the processed queue.
1356 * this is also protected by the kq lock.
1358 assert(kn
->kn_tq
== &kq
->kq_head
);
1359 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1360 kn
->kn_tq
= &kq
->kq_inprocess
;
1361 TAILQ_INSERT_TAIL(&kq
->kq_inprocess
, kn
, kn_tqe
);
1364 * Non-EV_ONESHOT events must be re-validated.
1366 * Convert our lock to a use-count and call the event's
1367 * filter routine to update.
1369 * If the event is dropping (or no longer valid), we
1370 * already have it off the active queue, so just
1371 * finish the job of deactivating it.
1373 if ((kn
->kn_flags
& EV_ONESHOT
) == 0) {
1376 if (kqlock2knoteuse(kq
, kn
)) {
1378 /* call the filter with just a ref */
1379 result
= kn
->kn_fop
->f_event(kn
, 0);
1381 if (!knoteuse2kqlock(kq
, kn
) || result
== 0) {
1382 knote_deactivate(kn
);
1386 knote_deactivate(kn
);
1392 * Got a valid triggered knote with the kqueue
1393 * still locked. Snapshot the data, and determine
1394 * how to dispatch the knote for future events.
1396 kev
= kn
->kn_kevent
;
1398 /* now what happens to it? */
1399 if (kn
->kn_flags
& EV_ONESHOT
) {
1400 knote_deactivate(kn
);
1401 if (kqlock2knotedrop(kq
, kn
)) {
1402 kn
->kn_fop
->f_detach(kn
);
1405 } else if (kn
->kn_flags
& EV_CLEAR
) {
1406 knote_deactivate(kn
);
1412 * leave on in-process queue. We'll
1413 * move all the remaining ones back
1414 * the kq queue and wakeup any
1415 * waiters when we are done.
1420 /* callback to handle each event as we find it */
1421 error
= (callback
)(kq
, &kev
, data
);
1428 * With the kqueue still locked, move any knotes
1429 * remaining on the in-process queue back to the
1430 * kq's queue and wake up any waiters.
1432 while ((kn
= TAILQ_FIRST(&kq
->kq_inprocess
)) != NULL
) {
1433 assert(kn
->kn_tq
== &kq
->kq_inprocess
);
1434 TAILQ_REMOVE(&kq
->kq_inprocess
, kn
, kn_tqe
);
1435 kn
->kn_tq
= &kq
->kq_head
;
1436 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1438 if (kq
->kq_state
& KQ_PROCWAIT
) {
1439 kq
->kq_state
&= ~KQ_PROCWAIT
;
1440 thread_wakeup(&kq
->kq_inprocess
);
1449 kevent_scan_continue(void *data
, wait_result_t wait_result
)
1451 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1452 struct _kevent_scan
* cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1453 struct kqueue
*kq
= (struct kqueue
*)data
;
1457 /* convert the (previous) wait_result to a proper error */
1458 switch (wait_result
) {
1459 case THREAD_AWAKENED
:
1461 error
= kevent_process(kq
, cont_args
->call
, cont_args
, &count
, current_proc());
1462 if (error
== 0 && count
== 0) {
1463 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, cont_args
->deadline
);
1464 kq
->kq_state
|= KQ_SLEEP
;
1466 thread_block_parameter(kevent_scan_continue
, kq
);
1471 case THREAD_TIMED_OUT
:
1472 error
= EWOULDBLOCK
;
1474 case THREAD_INTERRUPTED
:
1478 panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result
);
1482 /* call the continuation with the results */
1483 assert(cont_args
->cont
!= NULL
);
1484 (cont_args
->cont
)(kq
, cont_args
->data
, error
);
1489 * kevent_scan - scan and wait for events in a kqueue
1491 * Process the triggered events in a kqueue.
1493 * If there are no events triggered arrange to
1494 * wait for them. If the caller provided a
1495 * continuation routine, then kevent_scan will
1498 * The callback routine must be valid.
1499 * The caller must hold a use-count reference on the kq.
1503 kevent_scan(struct kqueue
*kq
,
1504 kevent_callback_t callback
,
1505 kevent_continue_t continuation
,
1507 struct timeval
*atvp
,
1510 thread_continue_t cont
= THREAD_CONTINUE_NULL
;
1515 assert(callback
!= NULL
);
1519 wait_result_t wait_result
;
1523 * Make a pass through the kq to find events already
1527 error
= kevent_process(kq
, callback
, data
, &count
, p
);
1529 break; /* lock still held */
1531 /* looks like we have to consider blocking */
1534 /* convert the timeout to a deadline once */
1535 if (atvp
->tv_sec
|| atvp
->tv_usec
) {
1536 uint32_t seconds
, nanoseconds
;
1539 clock_get_uptime(&now
);
1540 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec
* NSEC_PER_SEC
+
1541 atvp
->tv_usec
* NSEC_PER_USEC
,
1543 if (now
>= deadline
) {
1544 /* non-blocking call */
1545 error
= EWOULDBLOCK
;
1546 break; /* lock still held */
1549 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
1551 deadline
= 0; /* block forever */
1555 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
1556 struct _kevent_scan
*cont_args
= &ut
->uu_state
.ss_kevent_scan
;
1558 cont_args
->call
= callback
;
1559 cont_args
->cont
= continuation
;
1560 cont_args
->deadline
= deadline
;
1561 cont_args
->data
= data
;
1562 cont
= kevent_scan_continue
;
1566 /* go ahead and wait */
1567 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, deadline
);
1568 kq
->kq_state
|= KQ_SLEEP
;
1570 wait_result
= thread_block_parameter(cont
, kq
);
1571 /* NOTREACHED if (continuation != NULL) */
1573 switch (wait_result
) {
1574 case THREAD_AWAKENED
:
1576 case THREAD_TIMED_OUT
:
1578 case THREAD_INTERRUPTED
:
1581 panic("kevent_scan - bad wait_result (%d)",
1593 * This could be expanded to call kqueue_scan, if desired.
1597 kqueue_read(__unused
struct fileproc
*fp
,
1598 __unused
struct uio
*uio
,
1599 __unused kauth_cred_t cred
,
1601 __unused
struct proc
*p
)
1608 kqueue_write(__unused
struct fileproc
*fp
,
1609 __unused
struct uio
*uio
,
1610 __unused kauth_cred_t cred
,
1612 __unused
struct proc
*p
)
1619 kqueue_ioctl(__unused
struct fileproc
*fp
,
1620 __unused u_long com
,
1621 __unused caddr_t data
,
1622 __unused
struct proc
*p
)
1629 kqueue_select(struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
)
1631 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1634 if (which
== FREAD
) {
1639 selrecord(p
, &kq
->kq_sel
, wql
);
1640 kq
->kq_state
|= KQ_SEL
;
1652 kqueue_close(struct fileglob
*fg
, struct proc
*p
)
1654 struct kqueue
*kq
= (struct kqueue
*)fg
->fg_data
;
1656 kqueue_dealloc(kq
, p
);
1663 * The callers has taken a use-count reference on this kqueue and will donate it
1664 * to the kqueue we are being added to. This keeps the kqueue from closing until
1665 * that relationship is torn down.
1668 kqueue_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused
struct proc
*p
)
1670 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
1672 if (kn
->kn_filter
!= EVFILT_READ
)
1675 kn
->kn_fop
= &kqread_filtops
;
1677 KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
);
1684 kqueue_stat(struct fileproc
*fp
, struct stat
*st
, __unused
struct proc
*p
)
1686 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
1688 bzero((void *)st
, sizeof(*st
));
1689 st
->st_size
= kq
->kq_count
;
1690 st
->st_blksize
= sizeof(struct kevent
);
1691 st
->st_mode
= S_IFIFO
;
1696 * Called with the kqueue locked
1699 kqueue_wakeup(struct kqueue
*kq
)
1702 if (kq
->kq_state
& KQ_SLEEP
) {
1703 kq
->kq_state
&= ~KQ_SLEEP
;
1706 if (kq
->kq_state
& KQ_SEL
) {
1707 kq
->kq_state
&= ~KQ_SEL
;
1708 selwakeup(&kq
->kq_sel
);
1710 KNOTE(&kq
->kq_sel
.si_note
, 0);
1714 klist_init(struct klist
*list
)
1721 * Query/Post each knote in the object's list
1723 * The object lock protects the list. It is assumed
1724 * that the filter/event routine for the object can
1725 * determine that the object is already locked (via
1726 * the hind) and not deadlock itself.
1728 * The object lock should also hold off pending
1729 * detach/drop operations. But we'll prevent it here
1730 * too - just in case.
1733 knote(struct klist
*list
, long hint
)
1737 SLIST_FOREACH(kn
, list
, kn_selnext
) {
1738 struct kqueue
*kq
= kn
->kn_kq
;
1741 if (kqlock2knoteuse(kq
, kn
)) {
1744 /* call the event with only a use count */
1745 result
= kn
->kn_fop
->f_event(kn
, hint
);
1747 /* if its not going away and triggered */
1748 if (knoteuse2kqlock(kq
, kn
) && result
)
1750 /* lock held again */
1757 * attach a knote to the specified list. Return true if this is the first entry.
1758 * The list is protected by whatever lock the object it is associated with uses.
1761 knote_attach(struct klist
*list
, struct knote
*kn
)
1763 int ret
= SLIST_EMPTY(list
);
1764 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
1769 * detach a knote from the specified list. Return true if that was the last entry.
1770 * The list is protected by whatever lock the object it is associated with uses.
1773 knote_detach(struct klist
*list
, struct knote
*kn
)
1775 SLIST_REMOVE(list
, kn
, knote
, kn_selnext
);
1776 return SLIST_EMPTY(list
);
1780 * remove all knotes referencing a specified fd
1782 * Essentially an inlined knote_remove & knote_drop
1783 * when we know for sure that the thing is a file
1785 * Entered with the proc_fd lock already held.
1786 * It returns the same way, but may drop it temporarily.
1789 knote_fdclose(struct proc
*p
, int fd
)
1791 struct filedesc
*fdp
= p
->p_fd
;
1795 list
= &fdp
->fd_knlist
[fd
];
1796 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
1797 struct kqueue
*kq
= kn
->kn_kq
;
1803 * Convert the lock to a drop ref.
1804 * If we get it, go ahead and drop it.
1805 * Otherwise, we waited for it to
1806 * be dropped by the other guy, so
1807 * it is safe to move on in the list.
1809 if (kqlock2knotedrop(kq
, kn
)) {
1810 kn
->kn_fop
->f_detach(kn
);
1816 /* the fd tables may have changed - start over */
1817 list
= &fdp
->fd_knlist
[fd
];
1821 /* proc_fdlock held on entry (and exit) */
1823 knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, __unused
struct proc
*p
)
1825 struct klist
*list
= NULL
;
1827 if (! kn
->kn_fop
->f_isfd
) {
1828 if (fdp
->fd_knhashmask
== 0)
1829 fdp
->fd_knhash
= hashinit(KN_HASHSIZE
, M_KQUEUE
,
1830 &fdp
->fd_knhashmask
);
1831 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1833 if ((u_int
)fdp
->fd_knlistsize
<= kn
->kn_id
) {
1836 /* have to grow the fd_knlist */
1837 size
= fdp
->fd_knlistsize
;
1838 while (size
<= kn
->kn_id
)
1840 MALLOC(list
, struct klist
*,
1841 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
1845 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
1846 fdp
->fd_knlistsize
* sizeof(struct klist
*));
1847 bzero((caddr_t
)list
+
1848 fdp
->fd_knlistsize
* sizeof(struct klist
*),
1849 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
1850 FREE(fdp
->fd_knlist
, M_KQUEUE
);
1851 fdp
->fd_knlist
= list
;
1852 fdp
->fd_knlistsize
= size
;
1854 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1856 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
1863 * should be called at spl == 0, since we don't want to hold spl
1864 * while calling fdrop and free.
1867 knote_drop(struct knote
*kn
, struct proc
*p
)
1869 struct filedesc
*fdp
= p
->p_fd
;
1870 struct kqueue
*kq
= kn
->kn_kq
;
1874 if (kn
->kn_fop
->f_isfd
)
1875 list
= &fdp
->fd_knlist
[kn
->kn_id
];
1877 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
1879 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
1882 if (kn
->kn_status
& KN_DROPWAIT
)
1883 thread_wakeup(&kn
->kn_status
);
1887 if (kn
->kn_fop
->f_isfd
)
1888 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0);
1893 /* called with kqueue lock held */
1895 knote_activate(struct knote
*kn
)
1897 struct kqueue
*kq
= kn
->kn_kq
;
1899 kn
->kn_status
|= KN_ACTIVE
;
1904 /* called with kqueue lock held */
1906 knote_deactivate(struct knote
*kn
)
1908 kn
->kn_status
&= ~KN_ACTIVE
;
1912 /* called with kqueue lock held */
1914 knote_enqueue(struct knote
*kn
)
1916 struct kqueue
*kq
= kn
->kn_kq
;
1918 if ((kn
->kn_status
& (KN_QUEUED
| KN_DISABLED
)) == 0) {
1919 struct kqtailq
*tq
= kn
->kn_tq
;
1921 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);
1922 kn
->kn_status
|= KN_QUEUED
;
1927 /* called with kqueue lock held */
1929 knote_dequeue(struct knote
*kn
)
1931 struct kqueue
*kq
= kn
->kn_kq
;
1933 assert((kn
->kn_status
& KN_DISABLED
) == 0);
1934 if ((kn
->kn_status
& KN_QUEUED
) == KN_QUEUED
) {
1935 struct kqtailq
*tq
= kn
->kn_tq
;
1937 TAILQ_REMOVE(tq
, kn
, kn_tqe
);
1938 kn
->kn_tq
= &kq
->kq_head
;
1939 kn
->kn_status
&= ~KN_QUEUED
;
1947 knote_zone
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
), 8192, "knote zone");
1949 /* allocate kq lock group attribute and group */
1950 kq_lck_grp_attr
= lck_grp_attr_alloc_init();
1951 lck_grp_attr_setstat(kq_lck_grp_attr
);
1953 kq_lck_grp
= lck_grp_alloc_init("kqueue", kq_lck_grp_attr
);
1955 /* Allocate kq lock attribute */
1956 kq_lck_attr
= lck_attr_alloc_init();
1957 lck_attr_setdefault(kq_lck_attr
);
1959 /* Initialize the timer filter lock */
1960 lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
);
1962 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
1964 static struct knote
*
1967 return ((struct knote
*)zalloc(knote_zone
));
1971 knote_free(struct knote
*kn
)
1973 zfree(knote_zone
, kn
);
1976 #include <sys/param.h>
1977 #include <sys/socket.h>
1978 #include <sys/protosw.h>
1979 #include <sys/domain.h>
1980 #include <sys/mbuf.h>
1981 #include <sys/kern_event.h>
1982 #include <sys/malloc.h>
1983 #include <sys/sys_domain.h>
1984 #include <sys/syslog.h>
1987 static int kev_attach(struct socket
*so
, int proto
, struct proc
*p
);
1988 static int kev_detach(struct socket
*so
);
1989 static int kev_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
);
1991 struct pr_usrreqs event_usrreqs
= {
1992 pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
,
1993 pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
,
1994 pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
1995 pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
1996 pru_sosend_notsupp
, soreceive
, pru_sopoll_notsupp
1999 struct protosw eventsw
[] = {
2001 SOCK_RAW
, &systemdomain
, SYSPROTO_EVENT
, PR_ATOMIC
,
2017 struct kern_event_head kern_event_head
;
2019 static u_long static_event_id
= 0;
2020 struct domain
*sysdom
= &systemdomain
;
2022 static lck_grp_t
*evt_mtx_grp
;
2023 static lck_attr_t
*evt_mtx_attr
;
2024 static lck_grp_attr_t
*evt_mtx_grp_attr
;
2025 lck_mtx_t
*evt_mutex
;
2027 * Install the protosw's for the NKE manager. Invoked at
2028 * extension load time
2031 kern_event_init(void)
2035 if ((retval
= net_add_proto(eventsw
, &systemdomain
)) != 0) {
2036 log(LOG_WARNING
, "Can't install kernel events protocol (%d)\n", retval
);
2041 * allocate lock group attribute and group for kern event
2043 evt_mtx_grp_attr
= lck_grp_attr_alloc_init();
2045 evt_mtx_grp
= lck_grp_alloc_init("eventlist", evt_mtx_grp_attr
);
2048 * allocate the lock attribute for mutexes
2050 evt_mtx_attr
= lck_attr_alloc_init();
2051 lck_attr_setdefault(evt_mtx_attr
);
2052 evt_mutex
= lck_mtx_alloc_init(evt_mtx_grp
, evt_mtx_attr
);
2053 if (evt_mutex
== NULL
)
2056 return(KERN_SUCCESS
);
2060 kev_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
2063 struct kern_event_pcb
*ev_pcb
;
2065 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
2069 MALLOC(ev_pcb
, struct kern_event_pcb
*, sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
);
2073 ev_pcb
->ev_socket
= so
;
2074 ev_pcb
->vendor_code_filter
= 0xffffffff;
2076 so
->so_pcb
= (caddr_t
) ev_pcb
;
2077 lck_mtx_lock(evt_mutex
);
2078 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
);
2079 lck_mtx_unlock(evt_mutex
);
2086 kev_detach(struct socket
*so
)
2088 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2091 lck_mtx_lock(evt_mutex
);
2092 LIST_REMOVE(ev_pcb
, ev_link
);
2093 lck_mtx_unlock(evt_mutex
);
2094 FREE(ev_pcb
, M_PCB
);
2096 so
->so_flags
|= SOF_PCBCLEARING
;
2103 * For now, kev_vender_code and mbuf_tags use the same
2106 extern errno_t
mbuf_tag_id_find_internal(const char *string
, u_long
*out_id
,
2109 errno_t
kev_vendor_code_find(
2111 u_long
*out_vender_code
)
2113 if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) {
2116 return mbuf_tag_id_find_internal(string
, out_vender_code
, 1);
2119 extern void mbuf_tag_id_first_last(u_long
*first
, u_long
*last
);
2121 errno_t
kev_msg_post(struct kev_msg
*event_msg
)
2123 u_long min_vendor
, max_vendor
;
2125 mbuf_tag_id_first_last(&min_vendor
, &max_vendor
);
2127 if (event_msg
== NULL
)
2130 /* Limit third parties to posting events for registered vendor codes only */
2131 if (event_msg
->vendor_code
< min_vendor
||
2132 event_msg
->vendor_code
> max_vendor
)
2137 return kev_post_msg(event_msg
);
2141 int kev_post_msg(struct kev_msg
*event_msg
)
2143 struct mbuf
*m
, *m2
;
2144 struct kern_event_pcb
*ev_pcb
;
2145 struct kern_event_msg
*ev
;
2147 unsigned long total_size
;
2150 /* Verify the message is small enough to fit in one mbuf w/o cluster */
2151 total_size
= KEV_MSG_HEADER_SIZE
;
2153 for (i
= 0; i
< 5; i
++) {
2154 if (event_msg
->dv
[i
].data_length
== 0)
2156 total_size
+= event_msg
->dv
[i
].data_length
;
2159 if (total_size
> MLEN
) {
2163 m
= m_get(M_DONTWAIT
, MT_DATA
);
2167 ev
= mtod(m
, struct kern_event_msg
*);
2168 total_size
= KEV_MSG_HEADER_SIZE
;
2170 tmp
= (char *) &ev
->event_data
[0];
2171 for (i
= 0; i
< 5; i
++) {
2172 if (event_msg
->dv
[i
].data_length
== 0)
2175 total_size
+= event_msg
->dv
[i
].data_length
;
2176 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
2177 event_msg
->dv
[i
].data_length
);
2178 tmp
+= event_msg
->dv
[i
].data_length
;
2181 ev
->id
= ++static_event_id
;
2182 ev
->total_size
= total_size
;
2183 ev
->vendor_code
= event_msg
->vendor_code
;
2184 ev
->kev_class
= event_msg
->kev_class
;
2185 ev
->kev_subclass
= event_msg
->kev_subclass
;
2186 ev
->event_code
= event_msg
->event_code
;
2188 m
->m_len
= total_size
;
2189 lck_mtx_lock(evt_mutex
);
2190 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
2192 ev_pcb
= LIST_NEXT(ev_pcb
, ev_link
)) {
2194 if (ev_pcb
->vendor_code_filter
!= KEV_ANY_VENDOR
) {
2195 if (ev_pcb
->vendor_code_filter
!= ev
->vendor_code
)
2198 if (ev_pcb
->class_filter
!= KEV_ANY_CLASS
) {
2199 if (ev_pcb
->class_filter
!= ev
->kev_class
)
2202 if ((ev_pcb
->subclass_filter
!= KEV_ANY_SUBCLASS
) &&
2203 (ev_pcb
->subclass_filter
!= ev
->kev_subclass
))
2208 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
2211 lck_mtx_unlock(evt_mutex
);
2214 socket_lock(ev_pcb
->ev_socket
, 1);
2215 if (sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
))
2216 sorwakeup(ev_pcb
->ev_socket
);
2217 socket_unlock(ev_pcb
->ev_socket
, 1);
2221 lck_mtx_unlock(evt_mutex
);
2226 kev_control(struct socket
*so
,
2229 __unused
struct ifnet
*ifp
,
2230 __unused
struct proc
*p
)
2232 struct kev_request
*kev_req
= (struct kev_request
*) data
;
2233 struct kern_event_pcb
*ev_pcb
;
2234 struct kev_vendor_code
*kev_vendor
;
2235 u_long
*id_value
= (u_long
*) data
;
2241 *id_value
= static_event_id
;
2245 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2246 ev_pcb
->vendor_code_filter
= kev_req
->vendor_code
;
2247 ev_pcb
->class_filter
= kev_req
->kev_class
;
2248 ev_pcb
->subclass_filter
= kev_req
->kev_subclass
;
2252 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2253 kev_req
->vendor_code
= ev_pcb
->vendor_code_filter
;
2254 kev_req
->kev_class
= ev_pcb
->class_filter
;
2255 kev_req
->kev_subclass
= ev_pcb
->subclass_filter
;
2258 case SIOCGKEVVENDOR
:
2259 kev_vendor
= (struct kev_vendor_code
*)data
;
2261 /* Make sure string is NULL terminated */
2262 kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0;
2264 return mbuf_tag_id_find_internal(kev_vendor
->vendor_string
,
2265 &kev_vendor
->vendor_code
, 0);