2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)kern_event.c 1.0 (3/31/2000)
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/filedesc.h>
62 #include <sys/kernel.h>
63 #include <sys/proc_internal.h>
64 #include <sys/kauth.h>
65 #include <sys/malloc.h>
66 #include <sys/unistd.h>
67 #include <sys/file_internal.h>
68 #include <sys/fcntl.h>
69 #include <sys/select.h>
70 #include <sys/queue.h>
71 #include <sys/event.h>
72 #include <sys/eventvar.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/sysctl.h>
79 #include <sys/sysproto.h>
81 #include <sys/vnode_internal.h>
83 #include <sys/proc_info.h>
85 #include <kern/lock.h>
86 #include <kern/clock.h>
87 #include <kern/thread_call.h>
88 #include <kern/sched_prim.h>
89 #include <kern/zalloc.h>
90 #include <kern/assert.h>
92 #include <libkern/libkern.h>
93 #include "net/net_str_id.h"
95 #include <mach/task.h>
97 #if VM_PRESSURE_EVENTS
98 #include <kern/vm_pressure.h>
101 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system");
103 #define KQ_EVENT NULL
105 static inline void kqlock(struct kqueue
*kq
);
106 static inline void kqunlock(struct kqueue
*kq
);
108 static int kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
);
109 static int kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
);
110 static int kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
);
111 static int knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
);
113 static void kqueue_wakeup(struct kqueue
*kq
, int closed
);
114 static int kqueue_read(struct fileproc
*fp
, struct uio
*uio
,
115 int flags
, vfs_context_t ctx
);
116 static int kqueue_write(struct fileproc
*fp
, struct uio
*uio
,
117 int flags
, vfs_context_t ctx
);
118 static int kqueue_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
120 static int kqueue_select(struct fileproc
*fp
, int which
, void *wql
,
122 static int kqueue_close(struct fileglob
*fg
, vfs_context_t ctx
);
123 static int kqueue_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
);
124 static int kqueue_drain(struct fileproc
*fp
, vfs_context_t ctx
);
125 extern int kqueue_stat(struct fileproc
*fp
, void *ub
, int isstat64
, vfs_context_t ctx
);
127 static struct fileops kqueueops
= {
128 .fo_read
= kqueue_read
,
129 .fo_write
= kqueue_write
,
130 .fo_ioctl
= kqueue_ioctl
,
131 .fo_select
= kqueue_select
,
132 .fo_close
= kqueue_close
,
133 .fo_kqfilter
= kqueue_kqfilter
,
134 .fo_drain
= kqueue_drain
,
137 static int kevent_internal(struct proc
*p
, int iskev64
, user_addr_t changelist
,
138 int nchanges
, user_addr_t eventlist
, int nevents
, int fd
,
139 user_addr_t utimeout
, unsigned int flags
, int32_t *retval
);
140 static int kevent_copyin(user_addr_t
*addrp
, struct kevent64_s
*kevp
, struct proc
*p
, int iskev64
);
141 static int kevent_copyout(struct kevent64_s
*kevp
, user_addr_t
*addrp
, struct proc
*p
, int iskev64
);
142 char * kevent_description(struct kevent64_s
*kevp
, char *s
, size_t n
);
144 static int kevent_callback(struct kqueue
*kq
, struct kevent64_s
*kevp
, void *data
);
145 static void kevent_continue(struct kqueue
*kq
, void *data
, int error
);
146 static void kqueue_scan_continue(void *contp
, wait_result_t wait_result
);
147 static int kqueue_process(struct kqueue
*kq
, kevent_callback_t callback
,
148 void *data
, int *countp
, struct proc
*p
);
149 static int kqueue_begin_processing(struct kqueue
*kq
);
150 static void kqueue_end_processing(struct kqueue
*kq
);
151 static int knote_process(struct knote
*kn
, kevent_callback_t callback
,
152 void *data
, struct kqtailq
*inprocessp
, struct proc
*p
);
153 static void knote_put(struct knote
*kn
);
154 static int knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
);
155 static void knote_drop(struct knote
*kn
, struct proc
*p
);
156 static void knote_activate(struct knote
*kn
, int);
157 static void knote_deactivate(struct knote
*kn
);
158 static void knote_enqueue(struct knote
*kn
);
159 static void knote_dequeue(struct knote
*kn
);
160 static struct knote
*knote_alloc(void);
161 static void knote_free(struct knote
*kn
);
163 static int filt_fileattach(struct knote
*kn
);
164 static struct filterops file_filtops
= {
166 .f_attach
= filt_fileattach
,
169 static void filt_kqdetach(struct knote
*kn
);
170 static int filt_kqueue(struct knote
*kn
, long hint
);
171 static struct filterops kqread_filtops
= {
173 .f_detach
= filt_kqdetach
,
174 .f_event
= filt_kqueue
,
178 * placeholder for not-yet-implemented filters
180 static int filt_badattach(struct knote
*kn
);
181 static struct filterops bad_filtops
= {
182 .f_attach
= filt_badattach
,
185 static int filt_procattach(struct knote
*kn
);
186 static void filt_procdetach(struct knote
*kn
);
187 static int filt_proc(struct knote
*kn
, long hint
);
188 static struct filterops proc_filtops
= {
189 .f_attach
= filt_procattach
,
190 .f_detach
= filt_procdetach
,
191 .f_event
= filt_proc
,
194 #if VM_PRESSURE_EVENTS
195 static int filt_vmattach(struct knote
*kn
);
196 static void filt_vmdetach(struct knote
*kn
);
197 static int filt_vm(struct knote
*kn
, long hint
);
198 static struct filterops vm_filtops
= {
199 .f_attach
= filt_vmattach
,
200 .f_detach
= filt_vmdetach
,
203 #endif /* VM_PRESSURE_EVENTS */
205 extern struct filterops fs_filtops
;
207 extern struct filterops sig_filtops
;
210 static int filt_timerattach(struct knote
*kn
);
211 static void filt_timerdetach(struct knote
*kn
);
212 static int filt_timer(struct knote
*kn
, long hint
);
213 static void filt_timertouch(struct knote
*kn
, struct kevent64_s
*kev
,
215 static struct filterops timer_filtops
= {
216 .f_attach
= filt_timerattach
,
217 .f_detach
= filt_timerdetach
,
218 .f_event
= filt_timer
,
219 .f_touch
= filt_timertouch
,
224 static void filt_timerexpire(void *knx
, void *param1
);
225 static int filt_timervalidate(struct knote
*kn
);
226 static void filt_timerupdate(struct knote
*kn
);
227 static void filt_timercancel(struct knote
*kn
);
229 #define TIMER_RUNNING 0x1
230 #define TIMER_CANCELWAIT 0x2
232 static lck_mtx_t _filt_timerlock
;
233 static void filt_timerlock(void);
234 static void filt_timerunlock(void);
236 static zone_t knote_zone
;
238 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
241 extern struct filterops aio_filtops
;
244 /* Mach portset filter */
245 extern struct filterops machport_filtops
;
248 static int filt_userattach(struct knote
*kn
);
249 static void filt_userdetach(struct knote
*kn
);
250 static int filt_user(struct knote
*kn
, long hint
);
251 static void filt_usertouch(struct knote
*kn
, struct kevent64_s
*kev
,
253 static struct filterops user_filtops
= {
254 .f_attach
= filt_userattach
,
255 .f_detach
= filt_userdetach
,
256 .f_event
= filt_user
,
257 .f_touch
= filt_usertouch
,
261 * Table for for all system-defined filters.
263 static struct filterops
*sysfilt_ops
[] = {
264 &file_filtops
, /* EVFILT_READ */
265 &file_filtops
, /* EVFILT_WRITE */
267 &aio_filtops
, /* EVFILT_AIO */
269 &bad_filtops
, /* EVFILT_AIO */
271 &file_filtops
, /* EVFILT_VNODE */
272 &proc_filtops
, /* EVFILT_PROC */
273 &sig_filtops
, /* EVFILT_SIGNAL */
274 &timer_filtops
, /* EVFILT_TIMER */
275 &machport_filtops
, /* EVFILT_MACHPORT */
276 &fs_filtops
, /* EVFILT_FS */
277 &user_filtops
, /* EVFILT_USER */
278 &bad_filtops
, /* unused */
279 #if VM_PRESSURE_EVENTS
280 &vm_filtops
, /* EVFILT_VM */
282 &bad_filtops
, /* EVFILT_VM */
284 &file_filtops
, /* EVFILT_SOCK */
288 * kqueue/note lock attributes and implementations
290 * kqueues have locks, while knotes have use counts
291 * Most of the knote state is guarded by the object lock.
292 * the knote "inuse" count and status use the kqueue lock.
294 lck_grp_attr_t
* kq_lck_grp_attr
;
295 lck_grp_t
* kq_lck_grp
;
296 lck_attr_t
* kq_lck_attr
;
299 kqlock(struct kqueue
*kq
)
301 lck_spin_lock(&kq
->kq_lock
);
305 kqunlock(struct kqueue
*kq
)
307 lck_spin_unlock(&kq
->kq_lock
);
311 * Convert a kq lock to a knote use referece.
313 * If the knote is being dropped, we can't get
314 * a use reference, so just return with it
317 * - kq locked at entry
318 * - unlock on exit if we get the use reference
321 kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
)
323 if (kn
->kn_status
& KN_DROPPING
)
331 * Convert a kq lock to a knote use referece,
332 * but wait for attach and drop events to complete.
334 * If the knote is being dropped, we can't get
335 * a use reference, so just return with it
338 * - kq locked at entry
339 * - kq always unlocked on exit
342 kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
)
344 if ((kn
->kn_status
& (KN_DROPPING
| KN_ATTACHING
)) != 0) {
345 kn
->kn_status
|= KN_USEWAIT
;
346 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
, THREAD_UNINT
, 0);
348 thread_block(THREAD_CONTINUE_NULL
);
358 * Convert from a knote use reference back to kq lock.
360 * Drop a use reference and wake any waiters if
361 * this is the last one.
363 * The exit return indicates if the knote is
364 * still alive - but the kqueue lock is taken
368 knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
)
371 if (--kn
->kn_inuse
== 0) {
372 if ((kn
->kn_status
& KN_ATTACHING
) != 0) {
373 kn
->kn_status
&= ~KN_ATTACHING
;
375 if ((kn
->kn_status
& KN_USEWAIT
) != 0) {
376 kn
->kn_status
&= ~KN_USEWAIT
;
377 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
, THREAD_AWAKENED
);
380 return ((kn
->kn_status
& KN_DROPPING
) == 0);
384 * Convert a kq lock to a knote drop referece.
386 * If the knote is in use, wait for the use count
387 * to subside. We first mark our intention to drop
388 * it - keeping other users from "piling on."
389 * If we are too late, we have to wait for the
390 * other drop to complete.
392 * - kq locked at entry
393 * - always unlocked on exit.
394 * - caller can't hold any locks that would prevent
395 * the other dropper from completing.
398 kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
)
402 oktodrop
= ((kn
->kn_status
& (KN_DROPPING
| KN_ATTACHING
)) == 0);
403 kn
->kn_status
|= KN_DROPPING
;
405 if (kn
->kn_inuse
== 0) {
410 kn
->kn_status
|= KN_USEWAIT
;
411 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
, THREAD_UNINT
, 0);
413 thread_block(THREAD_CONTINUE_NULL
);
418 * Release a knote use count reference.
421 knote_put(struct knote
*kn
)
423 struct kqueue
*kq
= kn
->kn_kq
;
426 if (--kn
->kn_inuse
== 0) {
427 if ((kn
->kn_status
& KN_USEWAIT
) != 0) {
428 kn
->kn_status
&= ~KN_USEWAIT
;
429 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
, THREAD_AWAKENED
);
436 filt_fileattach(struct knote
*kn
)
439 return (fo_kqfilter(kn
->kn_fp
, kn
, vfs_context_current()));
442 #define f_flag f_fglob->fg_flag
443 #define f_type f_fglob->fg_type
444 #define f_msgcount f_fglob->fg_msgcount
445 #define f_cred f_fglob->fg_cred
446 #define f_ops f_fglob->fg_ops
447 #define f_offset f_fglob->fg_offset
448 #define f_data f_fglob->fg_data
451 filt_kqdetach(struct knote
*kn
)
453 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
456 KNOTE_DETACH(&kq
->kq_sel
.si_note
, kn
);
462 filt_kqueue(struct knote
*kn
, __unused
long hint
)
464 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
466 kn
->kn_data
= kq
->kq_count
;
467 return (kn
->kn_data
> 0);
471 filt_procattach(struct knote
*kn
)
475 assert(PID_MAX
< NOTE_PDATAMASK
);
477 if ((kn
->kn_sfflags
& (NOTE_TRACK
| NOTE_TRACKERR
| NOTE_CHILD
)) != 0)
480 p
= proc_find(kn
->kn_id
);
485 const int NoteExitStatusBits
= NOTE_EXIT
| NOTE_EXITSTATUS
;
487 if ((kn
->kn_sfflags
& NoteExitStatusBits
) == NoteExitStatusBits
)
489 pid_t selfpid
= proc_selfpid();
491 if (p
->p_ppid
== selfpid
)
492 break; /* parent => ok */
494 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
495 (p
->p_oppid
== selfpid
))
496 break; /* parent-in-waiting => ok */
504 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
505 kn
->kn_ptr
.p_proc
= p
; /* store the proc handle */
507 KNOTE_ATTACH(&p
->p_klist
, kn
);
517 * The knote may be attached to a different process, which may exit,
518 * leaving nothing for the knote to be attached to. In that case,
519 * the pointer to the process will have already been nulled out.
522 filt_procdetach(struct knote
*kn
)
528 p
= kn
->kn_ptr
.p_proc
;
529 if (p
!= PROC_NULL
) {
530 kn
->kn_ptr
.p_proc
= PROC_NULL
;
531 KNOTE_DETACH(&p
->p_klist
, kn
);
538 filt_proc(struct knote
*kn
, long hint
)
540 /* hint is 0 when called from above */
544 /* ALWAYS CALLED WITH proc_klist_lock when (hint != 0) */
547 * mask off extra data
549 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
552 * if the user is interested in this event, record it.
554 if (kn
->kn_sfflags
& event
)
555 kn
->kn_fflags
|= event
;
557 if (event
== NOTE_REAP
|| (event
== NOTE_EXIT
&& !(kn
->kn_sfflags
& NOTE_REAP
))) {
558 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
560 if ((event
== NOTE_EXIT
) && ((kn
->kn_sfflags
& NOTE_EXITSTATUS
) != 0)) {
561 kn
->kn_fflags
|= NOTE_EXITSTATUS
;
562 kn
->kn_data
= (hint
& NOTE_PDATAMASK
);
564 if ((event
== NOTE_RESOURCEEND
) && ((kn
->kn_sfflags
& NOTE_RESOURCEEND
) != 0)) {
565 kn
->kn_fflags
|= NOTE_RESOURCEEND
;
566 kn
->kn_data
= (hint
& NOTE_PDATAMASK
);
569 /* If the event is one of the APPSTATE events,remove the rest */
570 if (((event
& NOTE_APPALLSTATES
) != 0) && ((kn
->kn_sfflags
& NOTE_APPALLSTATES
) != 0)) {
571 /* only one state at a time */
572 kn
->kn_fflags
&= ~NOTE_APPALLSTATES
;
573 kn
->kn_fflags
|= event
;
575 #endif /* CONFIG_EMBEDDED */
578 /* atomic check, no locking need when called from above */
579 return (kn
->kn_fflags
!= 0);
582 #if VM_PRESSURE_EVENTS
584 * Virtual memory kevents
586 * author: Matt Jacobson [matthew_jacobson@apple.com]
590 filt_vmattach(struct knote
*kn
)
593 * The note will be cleared once the information has been flushed to the client.
594 * If there is still pressure, we will be re-alerted.
596 kn
->kn_flags
|= EV_CLEAR
;
598 return vm_knote_register(kn
);
602 filt_vmdetach(struct knote
*kn
)
604 vm_knote_unregister(kn
);
608 filt_vm(struct knote
*kn
, long hint
)
610 /* hint == 0 means this is just an alive? check (always true) */
612 const pid_t pid
= (pid_t
)hint
;
613 if ((kn
->kn_sfflags
& NOTE_VM_PRESSURE
) && (kn
->kn_kq
->kq_p
->p_pid
== pid
)) {
614 kn
->kn_fflags
|= NOTE_VM_PRESSURE
;
618 return (kn
->kn_fflags
!= 0);
620 #endif /* VM_PRESSURE_EVENTS */
623 * filt_timervalidate - process data from user
625 * Converts to either interval or deadline format.
627 * The saved-data field in the knote contains the
628 * time value. The saved filter-flags indicates
629 * the unit of measurement.
631 * After validation, either the saved-data field
632 * contains the interval in absolute time, or ext[0]
633 * contains the expected deadline. If that deadline
634 * is in the past, ext[0] is 0.
636 * Returns EINVAL for unrecognized units of time.
638 * Timer filter lock is held.
642 filt_timervalidate(struct knote
*kn
)
647 switch (kn
->kn_sfflags
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) {
649 multiplier
= NSEC_PER_SEC
;
652 multiplier
= NSEC_PER_USEC
;
657 case 0: /* milliseconds (default) */
658 multiplier
= NSEC_PER_SEC
/ 1000;
664 nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata
* multiplier
, &raw
);
669 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
) {
671 clock_nsec_t nanoseconds
;
674 clock_get_calendar_nanotime(&seconds
, &nanoseconds
);
675 nanoseconds_to_absolutetime((uint64_t)seconds
* NSEC_PER_SEC
+
679 /* time has already passed */
683 clock_absolutetime_interval_to_deadline(raw
,
694 * filt_timerupdate - compute the next deadline
696 * Repeating timers store their interval in kn_sdata. Absolute
697 * timers have already calculated the deadline, stored in ext[0].
699 * On return, the next deadline (or zero if no deadline is needed)
700 * is stored in kn_ext[0].
702 * Timer filter lock is held.
705 filt_timerupdate(struct knote
*kn
)
707 /* if there's no interval, deadline is just in kn_ext[0] */
708 if (kn
->kn_sdata
== 0)
711 /* if timer hasn't fired before, fire in interval nsecs */
712 if (kn
->kn_ext
[0] == 0) {
713 clock_absolutetime_interval_to_deadline(kn
->kn_sdata
,
717 * If timer has fired before, schedule the next pop
718 * relative to the last intended deadline.
720 * We could check for whether the deadline has expired,
721 * but the thread call layer can handle that.
723 kn
->kn_ext
[0] += kn
->kn_sdata
;
728 * filt_timerexpire - the timer callout routine
730 * Just propagate the timer event into the knote
731 * filter routine (by going through the knote
732 * synchronization point). Pass a hint to
733 * indicate this is a real event, not just a
737 filt_timerexpire(void *knx
, __unused
void *spare
)
739 struct klist timer_list
;
740 struct knote
*kn
= knx
;
744 kn
->kn_hookid
&= ~TIMER_RUNNING
;
746 /* no "object" for timers, so fake a list */
747 SLIST_INIT(&timer_list
);
748 SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);
749 KNOTE(&timer_list
, 1);
751 /* if someone is waiting for timer to pop */
752 if (kn
->kn_hookid
& TIMER_CANCELWAIT
) {
753 struct kqueue
*kq
= kn
->kn_kq
;
754 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_hook
,
762 * Cancel a running timer (or wait for the pop).
763 * Timer filter lock is held.
766 filt_timercancel(struct knote
*kn
)
768 struct kqueue
*kq
= kn
->kn_kq
;
769 thread_call_t callout
= kn
->kn_hook
;
772 if (kn
->kn_hookid
& TIMER_RUNNING
) {
773 /* cancel the callout if we can */
774 cancelled
= thread_call_cancel(callout
);
776 kn
->kn_hookid
&= ~TIMER_RUNNING
;
778 /* we have to wait for the expire routine. */
779 kn
->kn_hookid
|= TIMER_CANCELWAIT
;
780 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
,
781 &kn
->kn_hook
, THREAD_UNINT
, 0);
783 thread_block(THREAD_CONTINUE_NULL
);
785 assert((kn
->kn_hookid
& TIMER_RUNNING
) == 0);
791 * Allocate a thread call for the knote's lifetime, and kick off the timer.
794 filt_timerattach(struct knote
*kn
)
796 thread_call_t callout
;
799 callout
= thread_call_allocate(filt_timerexpire
, kn
);
804 error
= filt_timervalidate(kn
);
810 kn
->kn_hook
= (void*)callout
;
813 /* absolute=EV_ONESHOT */
814 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
)
815 kn
->kn_flags
|= EV_ONESHOT
;
817 filt_timerupdate(kn
);
819 kn
->kn_flags
|= EV_CLEAR
;
820 thread_call_enter_delayed(callout
, kn
->kn_ext
[0]);
821 kn
->kn_hookid
|= TIMER_RUNNING
;
832 * Shut down the timer if it's running, and free the callout.
835 filt_timerdetach(struct knote
*kn
)
837 thread_call_t callout
;
841 callout
= (thread_call_t
)kn
->kn_hook
;
842 filt_timercancel(kn
);
846 thread_call_free(callout
);
852 filt_timer(struct knote
*kn
, long hint
)
857 /* real timer pop -- timer lock held by filt_timerexpire */
861 if (((kn
->kn_hookid
& TIMER_CANCELWAIT
) == 0) &&
862 ((kn
->kn_flags
& EV_ONESHOT
) == 0)) {
864 /* evaluate next time to fire */
865 filt_timerupdate(kn
);
868 /* keep the callout and re-arm */
869 thread_call_enter_delayed(kn
->kn_hook
,
871 kn
->kn_hookid
|= TIMER_RUNNING
;
881 result
= (kn
->kn_data
!= 0);
889 * filt_timertouch - update knote with new user input
891 * Cancel and restart the timer based on new user data. When
892 * the user picks up a knote, clear the count of how many timer
893 * pops have gone off (in kn_data).
896 filt_timertouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
903 /* cancel current call */
904 filt_timercancel(kn
);
906 /* recalculate deadline */
907 kn
->kn_sdata
= kev
->data
;
908 kn
->kn_sfflags
= kev
->fflags
;
910 error
= filt_timervalidate(kn
);
912 /* no way to report error, so mark it in the knote */
913 kn
->kn_flags
|= EV_ERROR
;
918 /* start timer if necessary */
919 filt_timerupdate(kn
);
921 thread_call_enter_delayed(kn
->kn_hook
, kn
->kn_ext
[0]);
922 kn
->kn_hookid
|= TIMER_RUNNING
;
924 /* pretend the timer has fired */
931 /* reset the timer pop count in kn_data */
932 *kev
= kn
->kn_kevent
;
935 if (kn
->kn_flags
& EV_CLEAR
)
939 panic("filt_timertouch() - invalid type (%ld)", type
);
949 lck_mtx_lock(&_filt_timerlock
);
953 filt_timerunlock(void)
955 lck_mtx_unlock(&_filt_timerlock
);
959 filt_userattach(struct knote
*kn
)
961 /* EVFILT_USER knotes are not attached to anything in the kernel */
963 if (kn
->kn_fflags
& NOTE_TRIGGER
) {
972 filt_userdetach(__unused
struct knote
*kn
)
974 /* EVFILT_USER knotes are not attached to anything in the kernel */
978 filt_user(struct knote
*kn
, __unused
long hint
)
980 return kn
->kn_hookid
;
984 filt_usertouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
989 if (kev
->fflags
& NOTE_TRIGGER
) {
993 ffctrl
= kev
->fflags
& NOTE_FFCTRLMASK
;
994 kev
->fflags
&= NOTE_FFLAGSMASK
;
999 OSBitAndAtomic(kev
->fflags
, &kn
->kn_sfflags
);
1002 OSBitOrAtomic(kev
->fflags
, &kn
->kn_sfflags
);
1005 kn
->kn_sfflags
= kev
->fflags
;
1008 kn
->kn_sdata
= kev
->data
;
1011 *kev
= kn
->kn_kevent
;
1012 kev
->fflags
= (volatile UInt32
)kn
->kn_sfflags
;
1013 kev
->data
= kn
->kn_sdata
;
1014 if (kn
->kn_flags
& EV_CLEAR
) {
1021 panic("filt_usertouch() - invalid type (%ld)", type
);
1027 * JMM - placeholder for not-yet-implemented filters
1030 filt_badattach(__unused
struct knote
*kn
)
1037 kqueue_alloc(struct proc
*p
)
1039 struct filedesc
*fdp
= p
->p_fd
;
1042 MALLOC_ZONE(kq
, struct kqueue
*, sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
);
1044 wait_queue_set_t wqs
;
1046 wqs
= wait_queue_set_alloc(SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
);
1048 bzero(kq
, sizeof(struct kqueue
));
1049 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
);
1050 TAILQ_INIT(&kq
->kq_head
);
1054 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
1058 if (fdp
->fd_knlistsize
< 0) {
1060 if (fdp
->fd_knlistsize
< 0)
1061 fdp
->fd_knlistsize
= 0; /* this process has had a kq */
1070 * kqueue_dealloc - detach all knotes from a kqueue and free it
1072 * We walk each list looking for knotes referencing this
1073 * this kqueue. If we find one, we try to drop it. But
1074 * if we fail to get a drop reference, that will wait
1075 * until it is dropped. So, we can just restart again
1076 * safe in the assumption that the list will eventually
1077 * not contain any more references to this kqueue (either
1078 * we dropped them all, or someone else did).
1080 * Assumes no new events are being added to the kqueue.
1081 * Nothing locked on entry or exit.
1084 kqueue_dealloc(struct kqueue
*kq
)
1086 struct proc
*p
= kq
->kq_p
;
1087 struct filedesc
*fdp
= p
->p_fd
;
1092 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
1093 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
1094 while (kn
!= NULL
) {
1095 if (kq
== kn
->kn_kq
) {
1098 /* drop it ourselves or wait */
1099 if (kqlock2knotedrop(kq
, kn
)) {
1100 kn
->kn_fop
->f_detach(kn
);
1104 /* start over at beginning of list */
1105 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
1108 kn
= SLIST_NEXT(kn
, kn_link
);
1111 if (fdp
->fd_knhashmask
!= 0) {
1112 for (i
= 0; i
< (int)fdp
->fd_knhashmask
+ 1; i
++) {
1113 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
1114 while (kn
!= NULL
) {
1115 if (kq
== kn
->kn_kq
) {
1118 /* drop it ourselves or wait */
1119 if (kqlock2knotedrop(kq
, kn
)) {
1120 kn
->kn_fop
->f_detach(kn
);
1124 /* start over at beginning of list */
1125 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
1128 kn
= SLIST_NEXT(kn
, kn_link
);
1135 * before freeing the wait queue set for this kqueue,
1136 * make sure it is unlinked from all its containing (select) sets.
1138 wait_queue_unlink_all((wait_queue_t
)kq
->kq_wqs
);
1139 wait_queue_set_free(kq
->kq_wqs
);
1140 lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
);
1141 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
1145 kqueue(struct proc
*p
, __unused
struct kqueue_args
*uap
, int32_t *retval
)
1148 struct fileproc
*fp
;
1151 error
= falloc(p
, &fp
, &fd
, vfs_context_current());
1156 kq
= kqueue_alloc(p
);
1162 fp
->f_flag
= FREAD
| FWRITE
;
1163 fp
->f_type
= DTYPE_KQUEUE
;
1164 fp
->f_ops
= &kqueueops
;
1165 fp
->f_data
= (caddr_t
)kq
;
1168 procfdtbl_releasefd(p
, fd
, NULL
);
1169 fp_drop(p
, fd
, fp
, 1);
1177 kevent_copyin(user_addr_t
*addrp
, struct kevent64_s
*kevp
, struct proc
*p
, int iskev64
)
1183 advance
= sizeof(struct kevent64_s
);
1184 error
= copyin(*addrp
, (caddr_t
)kevp
, advance
);
1185 } else if (IS_64BIT_PROCESS(p
)) {
1186 struct user64_kevent kev64
;
1187 bzero(kevp
, sizeof(struct kevent64_s
));
1189 advance
= sizeof(kev64
);
1190 error
= copyin(*addrp
, (caddr_t
)&kev64
, advance
);
1193 kevp
->ident
= kev64
.ident
;
1194 kevp
->filter
= kev64
.filter
;
1195 kevp
->flags
= kev64
.flags
;
1196 kevp
->fflags
= kev64
.fflags
;
1197 kevp
->data
= kev64
.data
;
1198 kevp
->udata
= kev64
.udata
;
1200 struct user32_kevent kev32
;
1201 bzero(kevp
, sizeof(struct kevent64_s
));
1203 advance
= sizeof(kev32
);
1204 error
= copyin(*addrp
, (caddr_t
)&kev32
, advance
);
1207 kevp
->ident
= (uintptr_t)kev32
.ident
;
1208 kevp
->filter
= kev32
.filter
;
1209 kevp
->flags
= kev32
.flags
;
1210 kevp
->fflags
= kev32
.fflags
;
1211 kevp
->data
= (intptr_t)kev32
.data
;
1212 kevp
->udata
= CAST_USER_ADDR_T(kev32
.udata
);
1220 kevent_copyout(struct kevent64_s
*kevp
, user_addr_t
*addrp
, struct proc
*p
, int iskev64
)
1226 advance
= sizeof(struct kevent64_s
);
1227 error
= copyout((caddr_t
)kevp
, *addrp
, advance
);
1228 } else if (IS_64BIT_PROCESS(p
)) {
1229 struct user64_kevent kev64
;
1232 * deal with the special case of a user-supplied
1233 * value of (uintptr_t)-1.
1235 kev64
.ident
= (kevp
->ident
== (uintptr_t)-1) ?
1236 (uint64_t)-1LL : (uint64_t)kevp
->ident
;
1238 kev64
.filter
= kevp
->filter
;
1239 kev64
.flags
= kevp
->flags
;
1240 kev64
.fflags
= kevp
->fflags
;
1241 kev64
.data
= (int64_t) kevp
->data
;
1242 kev64
.udata
= kevp
->udata
;
1243 advance
= sizeof(kev64
);
1244 error
= copyout((caddr_t
)&kev64
, *addrp
, advance
);
1246 struct user32_kevent kev32
;
1248 kev32
.ident
= (uint32_t)kevp
->ident
;
1249 kev32
.filter
= kevp
->filter
;
1250 kev32
.flags
= kevp
->flags
;
1251 kev32
.fflags
= kevp
->fflags
;
1252 kev32
.data
= (int32_t)kevp
->data
;
1253 kev32
.udata
= kevp
->udata
;
1254 advance
= sizeof(kev32
);
1255 error
= copyout((caddr_t
)&kev32
, *addrp
, advance
);
1263 * kevent_continue - continue a kevent syscall after blocking
1265 * assume we inherit a use count on the kq fileglob.
1269 kevent_continue(__unused
struct kqueue
*kq
, void *data
, int error
)
1271 struct _kevent
*cont_args
;
1272 struct fileproc
*fp
;
1276 struct proc
*p
= current_proc();
1278 cont_args
= (struct _kevent
*)data
;
1279 noutputs
= cont_args
->eventout
;
1280 retval
= cont_args
->retval
;
1284 fp_drop(p
, fd
, fp
, 0);
1286 /* don't restart after signals... */
1287 if (error
== ERESTART
)
1289 else if (error
== EWOULDBLOCK
)
1293 unix_syscall_return(error
);
1297 * kevent - [syscall] register and wait for kernel events
1301 kevent(struct proc
*p
, struct kevent_args
*uap
, int32_t *retval
)
1303 return kevent_internal(p
,
1311 0, /* no flags from old kevent() call */
1316 kevent64(struct proc
*p
, struct kevent64_args
*uap
, int32_t *retval
)
1318 return kevent_internal(p
,
1331 kevent_internal(struct proc
*p
, int iskev64
, user_addr_t changelist
,
1332 int nchanges
, user_addr_t ueventlist
, int nevents
, int fd
,
1333 user_addr_t utimeout
, __unused
unsigned int flags
,
1336 struct _kevent
*cont_args
;
1339 struct fileproc
*fp
;
1340 struct kevent64_s kev
;
1341 int error
, noutputs
;
1344 /* convert timeout to absolute - if we have one */
1345 if (utimeout
!= USER_ADDR_NULL
) {
1347 if (IS_64BIT_PROCESS(p
)) {
1348 struct user64_timespec ts
;
1349 error
= copyin(utimeout
, &ts
, sizeof(ts
));
1350 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ull
) != 0)
1353 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1355 struct user32_timespec ts
;
1356 error
= copyin(utimeout
, &ts
, sizeof(ts
));
1357 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1361 if (itimerfix(&rtv
))
1363 getmicrouptime(&atv
);
1364 timevaladd(&atv
, &rtv
);
1370 /* get a usecount for the kq itself */
1371 if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0)
1374 /* each kq should only be used for events of one type */
1376 if (kq
->kq_state
& (KQ_KEV32
| KQ_KEV64
)) {
1377 if (((iskev64
&& (kq
->kq_state
& KQ_KEV32
)) ||
1378 (!iskev64
&& (kq
->kq_state
& KQ_KEV64
)))) {
1384 kq
->kq_state
|= (iskev64
? KQ_KEV64
: KQ_KEV32
);
1388 /* register all the change requests the user provided... */
1390 while (nchanges
> 0 && error
== 0) {
1391 error
= kevent_copyin(&changelist
, &kev
, p
, iskev64
);
1395 kev
.flags
&= ~EV_SYSFLAGS
;
1396 error
= kevent_register(kq
, &kev
, p
);
1397 if ((error
|| (kev
.flags
& EV_RECEIPT
)) && nevents
> 0) {
1398 kev
.flags
= EV_ERROR
;
1400 error
= kevent_copyout(&kev
, &ueventlist
, p
, iskev64
);
1409 /* store the continuation/completion data in the uthread */
1410 ut
= (uthread_t
)get_bsdthread_info(current_thread());
1411 cont_args
= &ut
->uu_kevent
.ss_kevent
;
1414 cont_args
->retval
= retval
;
1415 cont_args
->eventlist
= ueventlist
;
1416 cont_args
->eventcount
= nevents
;
1417 cont_args
->eventout
= noutputs
;
1418 cont_args
->eventsize
= iskev64
;
1420 if (nevents
> 0 && noutputs
== 0 && error
== 0)
1421 error
= kqueue_scan(kq
, kevent_callback
,
1422 kevent_continue
, cont_args
,
1424 kevent_continue(kq
, cont_args
, error
);
1427 fp_drop(p
, fd
, fp
, 0);
1433 * kevent_callback - callback for each individual event
1435 * called with nothing locked
1436 * caller holds a reference on the kqueue
1440 kevent_callback(__unused
struct kqueue
*kq
, struct kevent64_s
*kevp
,
1443 struct _kevent
*cont_args
;
1447 cont_args
= (struct _kevent
*)data
;
1448 assert(cont_args
->eventout
< cont_args
->eventcount
);
1450 iskev64
= cont_args
->eventsize
;
1453 * Copy out the appropriate amount of event data for this user.
1455 error
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc(), iskev64
);
1458 * If there isn't space for additional events, return
1459 * a harmless error to stop the processing here
1461 if (error
== 0 && ++cont_args
->eventout
== cont_args
->eventcount
)
1462 error
= EWOULDBLOCK
;
1467 * kevent_description - format a description of a kevent for diagnostic output
1469 * called with a 128-byte string buffer
1473 kevent_description(struct kevent64_s
*kevp
, char *s
, size_t n
)
1477 "{.ident=%#llx, .filter=%d, .flags=%#x, .fflags=%#x, .data=%#llx, .udata=%#llx, .ext[0]=%#llx, .ext[1]=%#llx}",
1490 * kevent_register - add a new event to a kqueue
1492 * Creates a mapping between the event source and
1493 * the kqueue via a knote data structure.
1495 * Because many/most the event sources are file
1496 * descriptor related, the knote is linked off
1497 * the filedescriptor table for quick access.
1499 * called with nothing locked
1500 * caller holds a reference on the kqueue
1504 kevent_register(struct kqueue
*kq
, struct kevent64_s
*kev
, __unused
struct proc
*ctxp
)
1506 struct proc
*p
= kq
->kq_p
;
1507 struct filedesc
*fdp
= p
->p_fd
;
1508 struct filterops
*fops
;
1509 struct fileproc
*fp
= NULL
;
1510 struct knote
*kn
= NULL
;
1513 if (kev
->filter
< 0) {
1514 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
1516 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
1520 * filter attach routine is responsible for insuring that
1521 * the identifier can be attached to it.
1523 printf("unknown filter: %d\n", kev
->filter
);
1528 /* this iocount needs to be dropped if it is not registered */
1530 if (fops
->f_isfd
&& (error
= fp_lookup(p
, kev
->ident
, &fp
, 1)) != 0) {
1536 /* fd-based knotes are linked off the fd table */
1537 if (kev
->ident
< (u_int
)fdp
->fd_knlistsize
) {
1538 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
1539 if (kq
== kn
->kn_kq
&&
1540 kev
->filter
== kn
->kn_filter
)
1544 /* hash non-fd knotes here too */
1545 if (fdp
->fd_knhashmask
!= 0) {
1548 list
= &fdp
->fd_knhash
[
1549 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
1550 SLIST_FOREACH(kn
, list
, kn_link
)
1551 if (kev
->ident
== kn
->kn_id
&&
1553 kev
->filter
== kn
->kn_filter
)
1559 * kn now contains the matching knote, or NULL if no match
1562 if ((kev
->flags
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) {
1571 kn
->kn_tq
= &kq
->kq_head
;
1573 kn
->kn_sfflags
= kev
->fflags
;
1574 kn
->kn_sdata
= kev
->data
;
1577 kn
->kn_kevent
= *kev
;
1578 kn
->kn_inuse
= 1; /* for f_attach() */
1579 kn
->kn_status
= KN_ATTACHING
;
1581 /* before anyone can find it */
1582 if (kev
->flags
& EV_DISABLE
)
1583 kn
->kn_status
|= KN_DISABLED
;
1585 error
= knote_fdpattach(kn
, fdp
, p
);
1594 * apply reference count to knote structure, and
1595 * do not release it at the end of this routine.
1599 error
= fops
->f_attach(kn
);
1605 * Failed to attach correctly, so drop.
1606 * All other possible users/droppers
1607 * have deferred to us.
1609 kn
->kn_status
|= KN_DROPPING
;
1613 } else if (kn
->kn_status
& KN_DROPPING
) {
1615 * Attach succeeded, but someone else
1616 * deferred their drop - now we have
1617 * to do it for them (after detaching).
1620 kn
->kn_fop
->f_detach(kn
);
1624 kn
->kn_status
&= ~KN_ATTACHING
;
1632 /* existing knote - get kqueue lock */
1636 if (kev
->flags
& EV_DELETE
) {
1638 kn
->kn_status
|= KN_DISABLED
;
1639 if (kqlock2knotedrop(kq
, kn
)) {
1640 kn
->kn_fop
->f_detach(kn
);
1646 /* update status flags for existing knote */
1647 if (kev
->flags
& EV_DISABLE
) {
1649 kn
->kn_status
|= KN_DISABLED
;
1650 } else if (kev
->flags
& EV_ENABLE
) {
1651 kn
->kn_status
&= ~KN_DISABLED
;
1652 if (kn
->kn_status
& KN_ACTIVE
)
1657 * The user may change some filter values after the
1658 * initial EV_ADD, but doing so will not reset any
1659 * filter which have already been triggered.
1661 kn
->kn_kevent
.udata
= kev
->udata
;
1662 if (fops
->f_isfd
|| fops
->f_touch
== NULL
) {
1663 kn
->kn_sfflags
= kev
->fflags
;
1664 kn
->kn_sdata
= kev
->data
;
1668 * If somebody is in the middle of dropping this
1669 * knote - go find/insert a new one. But we have
1670 * wait for this one to go away first. Attaches
1671 * running in parallel may also drop/modify the
1672 * knote. Wait for those to complete as well and
1673 * then start over if we encounter one.
1675 if (!kqlock2knoteusewait(kq
, kn
)) {
1676 /* kqueue, proc_fdlock both unlocked */
1681 * Call touch routine to notify filter of changes
1684 if (!fops
->f_isfd
&& fops
->f_touch
!= NULL
)
1685 fops
->f_touch(kn
, kev
, EVENT_REGISTER
);
1687 /* still have use ref on knote */
1690 * If the knote is not marked to always stay enqueued,
1691 * invoke the filter routine to see if it should be
1694 if ((kn
->kn_status
& KN_STAYQUEUED
) == 0 && kn
->kn_fop
->f_event(kn
, 0)) {
1695 if (knoteuse2kqlock(kq
, kn
))
1696 knote_activate(kn
, 1);
1704 fp_drop(p
, kev
->ident
, fp
, 0);
1710 * knote_process - process a triggered event
1712 * Validate that it is really still a triggered event
1713 * by calling the filter routines (if necessary). Hold
1714 * a use reference on the knote to avoid it being detached.
1715 * If it is still considered triggered, invoke the callback
1716 * routine provided and move it to the provided inprocess
1719 * caller holds a reference on the kqueue.
1720 * kqueue locked on entry and exit - but may be dropped
1723 knote_process(struct knote
*kn
,
1724 kevent_callback_t callback
,
1726 struct kqtailq
*inprocessp
,
1729 struct kqueue
*kq
= kn
->kn_kq
;
1730 struct kevent64_s kev
;
1736 * Determine the kevent state we want to return.
1738 * Some event states need to be revalidated before returning
1739 * them, others we take the snapshot at the time the event
1742 * Events with non-NULL f_touch operations must be touched.
1743 * Triggered events must fill in kev for the callback.
1745 * Convert our lock to a use-count and call the event's
1746 * filter routine(s) to update.
1748 if ((kn
->kn_status
& KN_DISABLED
) != 0) {
1755 revalidate
= ((kn
->kn_status
& KN_STAYQUEUED
) != 0 ||
1756 (kn
->kn_flags
& EV_ONESHOT
) == 0);
1757 touch
= (!kn
->kn_fop
->f_isfd
&& kn
->kn_fop
->f_touch
!= NULL
);
1759 if (revalidate
|| touch
) {
1761 knote_deactivate(kn
);
1763 /* call the filter/touch routines with just a ref */
1764 if (kqlock2knoteuse(kq
, kn
)) {
1766 /* if we have to revalidate, call the filter */
1768 result
= kn
->kn_fop
->f_event(kn
, 0);
1771 /* capture the kevent data - using touch if specified */
1772 if (result
&& touch
) {
1773 kn
->kn_fop
->f_touch(kn
, &kev
, EVENT_PROCESS
);
1776 /* convert back to a kqlock - bail if the knote went away */
1777 if (!knoteuse2kqlock(kq
, kn
)) {
1779 } else if (result
) {
1780 /* if revalidated as alive, make sure it's active */
1781 if (!(kn
->kn_status
& KN_ACTIVE
)) {
1782 knote_activate(kn
, 0);
1785 /* capture all events that occurred during filter */
1787 kev
= kn
->kn_kevent
;
1790 } else if ((kn
->kn_status
& KN_STAYQUEUED
) == 0) {
1791 /* was already dequeued, so just bail on this one */
1798 kev
= kn
->kn_kevent
;
1802 /* move knote onto inprocess queue */
1803 assert(kn
->kn_tq
== &kq
->kq_head
);
1804 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1805 kn
->kn_tq
= inprocessp
;
1806 TAILQ_INSERT_TAIL(inprocessp
, kn
, kn_tqe
);
1809 * Determine how to dispatch the knote for future event handling.
1810 * not-fired: just return (do not callout).
1811 * One-shot: deactivate it.
1812 * Clear: deactivate and clear the state.
1813 * Dispatch: don't clear state, just deactivate it and mark it disabled.
1814 * All others: just leave where they are.
1819 } else if ((kn
->kn_flags
& EV_ONESHOT
) != 0) {
1820 knote_deactivate(kn
);
1821 if (kqlock2knotedrop(kq
, kn
)) {
1822 kn
->kn_fop
->f_detach(kn
);
1825 } else if ((kn
->kn_flags
& (EV_CLEAR
| EV_DISPATCH
)) != 0) {
1826 if ((kn
->kn_flags
& EV_DISPATCH
) != 0) {
1827 /* deactivate and disable all dispatch knotes */
1828 knote_deactivate(kn
);
1829 kn
->kn_status
|= KN_DISABLED
;
1830 } else if (!touch
|| kn
->kn_fflags
== 0) {
1831 /* only deactivate if nothing since the touch */
1832 knote_deactivate(kn
);
1834 if (!touch
&& (kn
->kn_flags
& EV_CLEAR
) != 0) {
1835 /* manually clear non-touch knotes */
1842 * leave on inprocess queue. We'll
1843 * move all the remaining ones back
1844 * the kq queue and wakeup any
1845 * waiters when we are done.
1850 /* callback to handle each event as we find it */
1851 error
= (callback
)(kq
, &kev
, data
);
1858 * Return 0 to indicate that processing should proceed,
1859 * -1 if there is nothing to process.
1861 * Called with kqueue locked and returns the same way,
1862 * but may drop lock temporarily.
1865 kqueue_begin_processing(struct kqueue
*kq
)
1868 if (kq
->kq_count
== 0) {
1872 /* if someone else is processing the queue, wait */
1873 if (kq
->kq_nprocess
!= 0) {
1874 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, &kq
->kq_nprocess
, THREAD_UNINT
, 0);
1875 kq
->kq_state
|= KQ_PROCWAIT
;
1877 thread_block(THREAD_CONTINUE_NULL
);
1880 kq
->kq_nprocess
= 1;
1887 * Called with kqueue lock held.
1890 kqueue_end_processing(struct kqueue
*kq
)
1892 kq
->kq_nprocess
= 0;
1893 if (kq
->kq_state
& KQ_PROCWAIT
) {
1894 kq
->kq_state
&= ~KQ_PROCWAIT
;
1895 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kq
->kq_nprocess
, THREAD_AWAKENED
);
1900 * kqueue_process - process the triggered events in a kqueue
1902 * Walk the queued knotes and validate that they are
1903 * really still triggered events by calling the filter
1904 * routines (if necessary). Hold a use reference on
1905 * the knote to avoid it being detached. For each event
1906 * that is still considered triggered, invoke the
1907 * callback routine provided.
1909 * caller holds a reference on the kqueue.
1910 * kqueue locked on entry and exit - but may be dropped
1911 * kqueue list locked (held for duration of call)
1915 kqueue_process(struct kqueue
*kq
,
1916 kevent_callback_t callback
,
1921 struct kqtailq inprocess
;
1926 TAILQ_INIT(&inprocess
);
1928 if (kqueue_begin_processing(kq
) == -1) {
1930 /* Nothing to process */
1935 * Clear any pre-posted status from previous runs, so we only
1936 * detect events that occur during this run.
1938 wait_queue_sub_clearrefs(kq
->kq_wqs
);
1941 * loop through the enqueued knotes, processing each one and
1942 * revalidating those that need it. As they are processed,
1943 * they get moved to the inprocess queue (so the loop can end).
1948 while (error
== 0 &&
1949 (kn
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
1950 error
= knote_process(kn
, callback
, data
, &inprocess
, p
);
1951 if (error
== EJUSTRETURN
)
1958 * With the kqueue still locked, move any knotes
1959 * remaining on the inprocess queue back to the
1960 * kq's queue and wake up any waiters.
1962 while ((kn
= TAILQ_FIRST(&inprocess
)) != NULL
) {
1963 assert(kn
->kn_tq
== &inprocess
);
1964 TAILQ_REMOVE(&inprocess
, kn
, kn_tqe
);
1965 kn
->kn_tq
= &kq
->kq_head
;
1966 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1969 kqueue_end_processing(kq
);
1977 kqueue_scan_continue(void *data
, wait_result_t wait_result
)
1979 thread_t self
= current_thread();
1980 uthread_t ut
= (uthread_t
)get_bsdthread_info(self
);
1981 struct _kqueue_scan
* cont_args
= &ut
->uu_kevent
.ss_kqueue_scan
;
1982 struct kqueue
*kq
= (struct kqueue
*)data
;
1986 /* convert the (previous) wait_result to a proper error */
1987 switch (wait_result
) {
1988 case THREAD_AWAKENED
:
1990 error
= kqueue_process(kq
, cont_args
->call
, cont_args
, &count
, current_proc());
1991 if (error
== 0 && count
== 0) {
1992 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, KQ_EVENT
,
1993 THREAD_ABORTSAFE
, cont_args
->deadline
);
1994 kq
->kq_state
|= KQ_SLEEP
;
1996 thread_block_parameter(kqueue_scan_continue
, kq
);
2001 case THREAD_TIMED_OUT
:
2002 error
= EWOULDBLOCK
;
2004 case THREAD_INTERRUPTED
:
2008 panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result
);
2012 /* call the continuation with the results */
2013 assert(cont_args
->cont
!= NULL
);
2014 (cont_args
->cont
)(kq
, cont_args
->data
, error
);
2019 * kqueue_scan - scan and wait for events in a kqueue
2021 * Process the triggered events in a kqueue.
2023 * If there are no events triggered arrange to
2024 * wait for them. If the caller provided a
2025 * continuation routine, then kevent_scan will
2028 * The callback routine must be valid.
2029 * The caller must hold a use-count reference on the kq.
2033 kqueue_scan(struct kqueue
*kq
,
2034 kevent_callback_t callback
,
2035 kqueue_continue_t continuation
,
2037 struct timeval
*atvp
,
2040 thread_continue_t cont
= THREAD_CONTINUE_NULL
;
2045 assert(callback
!= NULL
);
2049 wait_result_t wait_result
;
2053 * Make a pass through the kq to find events already
2057 error
= kqueue_process(kq
, callback
, data
, &count
, p
);
2059 break; /* lock still held */
2061 /* looks like we have to consider blocking */
2064 /* convert the timeout to a deadline once */
2065 if (atvp
->tv_sec
|| atvp
->tv_usec
) {
2068 clock_get_uptime(&now
);
2069 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec
* NSEC_PER_SEC
+
2070 atvp
->tv_usec
* NSEC_PER_USEC
,
2072 if (now
>= deadline
) {
2073 /* non-blocking call */
2074 error
= EWOULDBLOCK
;
2075 break; /* lock still held */
2078 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
2080 deadline
= 0; /* block forever */
2084 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
2085 struct _kqueue_scan
*cont_args
= &ut
->uu_kevent
.ss_kqueue_scan
;
2087 cont_args
->call
= callback
;
2088 cont_args
->cont
= continuation
;
2089 cont_args
->deadline
= deadline
;
2090 cont_args
->data
= data
;
2091 cont
= kqueue_scan_continue
;
2095 /* go ahead and wait */
2096 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, KQ_EVENT
, THREAD_ABORTSAFE
, deadline
);
2097 kq
->kq_state
|= KQ_SLEEP
;
2099 wait_result
= thread_block_parameter(cont
, kq
);
2100 /* NOTREACHED if (continuation != NULL) */
2102 switch (wait_result
) {
2103 case THREAD_AWAKENED
:
2105 case THREAD_TIMED_OUT
:
2107 case THREAD_INTERRUPTED
:
2110 panic("kevent_scan - bad wait_result (%d)",
2122 * This could be expanded to call kqueue_scan, if desired.
2126 kqueue_read(__unused
struct fileproc
*fp
,
2127 __unused
struct uio
*uio
,
2129 __unused vfs_context_t ctx
)
2136 kqueue_write(__unused
struct fileproc
*fp
,
2137 __unused
struct uio
*uio
,
2139 __unused vfs_context_t ctx
)
2146 kqueue_ioctl(__unused
struct fileproc
*fp
,
2147 __unused u_long com
,
2148 __unused caddr_t data
,
2149 __unused vfs_context_t ctx
)
2156 kqueue_select(struct fileproc
*fp
, int which
, void *wql
, __unused vfs_context_t ctx
)
2158 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
2160 struct kqtailq inprocessq
;
2166 TAILQ_INIT(&inprocessq
);
2170 * If this is the first pass, link the wait queue associated with the
2171 * the kqueue onto the wait queue set for the select(). Normally we
2172 * use selrecord() for this, but it uses the wait queue within the
2173 * selinfo structure and we need to use the main one for the kqueue to
2174 * catch events from KN_STAYQUEUED sources. So we do the linkage manually.
2175 * (The select() call will unlink them when it ends).
2178 thread_t cur_act
= current_thread();
2179 struct uthread
* ut
= get_bsdthread_info(cur_act
);
2181 kq
->kq_state
|= KQ_SEL
;
2182 wait_queue_link_noalloc((wait_queue_t
)kq
->kq_wqs
, ut
->uu_wqset
,
2183 (wait_queue_link_t
)wql
);
2186 if (kqueue_begin_processing(kq
) == -1) {
2191 if (kq
->kq_count
!= 0) {
2193 * there is something queued - but it might be a
2194 * KN_STAYQUEUED knote, which may or may not have
2195 * any events pending. So, we have to walk the
2196 * list of knotes to see, and peek at the stay-
2197 * queued ones to be really sure.
2199 while ((kn
= (struct knote
*)TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
2200 if ((kn
->kn_status
& KN_STAYQUEUED
) == 0) {
2205 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
2206 TAILQ_INSERT_TAIL(&inprocessq
, kn
, kn_tqe
);
2208 if (kqlock2knoteuse(kq
, kn
)) {
2211 peek
= kn
->kn_fop
->f_peek(kn
);
2212 if (knoteuse2kqlock(kq
, kn
)) {
2225 /* Return knotes to active queue */
2226 while ((kn
= TAILQ_FIRST(&inprocessq
)) != NULL
) {
2227 TAILQ_REMOVE(&inprocessq
, kn
, kn_tqe
);
2228 kn
->kn_tq
= &kq
->kq_head
;
2229 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
2232 kqueue_end_processing(kq
);
2242 kqueue_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
2244 struct kqueue
*kq
= (struct kqueue
*)fg
->fg_data
;
2253 * The callers has taken a use-count reference on this kqueue and will donate it
2254 * to the kqueue we are being added to. This keeps the kqueue from closing until
2255 * that relationship is torn down.
2258 kqueue_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused vfs_context_t ctx
)
2260 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
2261 struct kqueue
*parentkq
= kn
->kn_kq
;
2263 if (parentkq
== kq
||
2264 kn
->kn_filter
!= EVFILT_READ
)
2268 * We have to avoid creating a cycle when nesting kqueues
2269 * inside another. Rather than trying to walk the whole
2270 * potential DAG of nested kqueues, we just use a simple
2271 * ceiling protocol. When a kqueue is inserted into another,
2272 * we check that the (future) parent is not already nested
2273 * into another kqueue at a lower level than the potenial
2274 * child (because it could indicate a cycle). If that test
2275 * passes, we just mark the nesting levels accordingly.
2279 if (parentkq
->kq_level
> 0 &&
2280 parentkq
->kq_level
< kq
->kq_level
)
2285 /* set parent level appropriately */
2286 if (parentkq
->kq_level
== 0)
2287 parentkq
->kq_level
= 2;
2288 if (parentkq
->kq_level
< kq
->kq_level
+ 1)
2289 parentkq
->kq_level
= kq
->kq_level
+ 1;
2292 kn
->kn_fop
= &kqread_filtops
;
2294 KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
);
2295 /* indicate nesting in child, if needed */
2296 if (kq
->kq_level
== 0)
2304 * kqueue_drain - called when kq is closed
2308 kqueue_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2310 struct kqueue
*kq
= (struct kqueue
*)fp
->f_fglob
->fg_data
;
2312 kqueue_wakeup(kq
, 1);
2319 kqueue_stat(struct fileproc
*fp
, void *ub
, int isstat64
, __unused vfs_context_t ctx
)
2322 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
2323 if (isstat64
!= 0) {
2324 struct stat64
*sb64
= (struct stat64
*)ub
;
2326 bzero((void *)sb64
, sizeof(*sb64
));
2327 sb64
->st_size
= kq
->kq_count
;
2328 if (kq
->kq_state
& KQ_KEV64
)
2329 sb64
->st_blksize
= sizeof(struct kevent64_s
);
2331 sb64
->st_blksize
= sizeof(struct kevent
);
2332 sb64
->st_mode
= S_IFIFO
;
2334 struct stat
*sb
= (struct stat
*)ub
;
2336 bzero((void *)sb
, sizeof(*sb
));
2337 sb
->st_size
= kq
->kq_count
;
2338 if (kq
->kq_state
& KQ_KEV64
)
2339 sb
->st_blksize
= sizeof(struct kevent64_s
);
2341 sb
->st_blksize
= sizeof(struct kevent
);
2342 sb
->st_mode
= S_IFIFO
;
2349 * Called with the kqueue locked
2352 kqueue_wakeup(struct kqueue
*kq
, int closed
)
2354 if ((kq
->kq_state
& (KQ_SLEEP
| KQ_SEL
)) != 0 || kq
->kq_nprocess
> 0) {
2355 kq
->kq_state
&= ~(KQ_SLEEP
| KQ_SEL
);
2356 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, KQ_EVENT
,
2357 (closed
) ? THREAD_INTERRUPTED
: THREAD_AWAKENED
);
2362 klist_init(struct klist
*list
)
2369 * Query/Post each knote in the object's list
2371 * The object lock protects the list. It is assumed
2372 * that the filter/event routine for the object can
2373 * determine that the object is already locked (via
2374 * the hint) and not deadlock itself.
2376 * The object lock should also hold off pending
2377 * detach/drop operations. But we'll prevent it here
2378 * too - just in case.
2381 knote(struct klist
*list
, long hint
)
2385 SLIST_FOREACH(kn
, list
, kn_selnext
) {
2386 struct kqueue
*kq
= kn
->kn_kq
;
2389 if (kqlock2knoteuse(kq
, kn
)) {
2392 /* call the event with only a use count */
2393 result
= kn
->kn_fop
->f_event(kn
, hint
);
2395 /* if its not going away and triggered */
2396 if (knoteuse2kqlock(kq
, kn
) && result
)
2397 knote_activate(kn
, 1);
2398 /* lock held again */
2405 * attach a knote to the specified list. Return true if this is the first entry.
2406 * The list is protected by whatever lock the object it is associated with uses.
2409 knote_attach(struct klist
*list
, struct knote
*kn
)
2411 int ret
= SLIST_EMPTY(list
);
2412 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
2417 * detach a knote from the specified list. Return true if that was the last entry.
2418 * The list is protected by whatever lock the object it is associated with uses.
2421 knote_detach(struct klist
*list
, struct knote
*kn
)
2423 SLIST_REMOVE(list
, kn
, knote
, kn_selnext
);
2424 return SLIST_EMPTY(list
);
2428 * For a given knote, link a provided wait queue directly with the kqueue.
2429 * Wakeups will happen via recursive wait queue support. But nothing will move
2430 * the knote to the active list at wakeup (nothing calls knote()). Instead,
2431 * we permanently enqueue them here.
2433 * kqueue and knote references are held by caller.
2435 * caller provides the wait queue link structure.
2438 knote_link_wait_queue(struct knote
*kn
, struct wait_queue
*wq
, wait_queue_link_t wql
)
2440 struct kqueue
*kq
= kn
->kn_kq
;
2443 kr
= wait_queue_link_noalloc(wq
, kq
->kq_wqs
, wql
);
2444 if (kr
== KERN_SUCCESS
) {
2445 knote_markstayqueued(kn
);
2453 * Unlink the provided wait queue from the kqueue associated with a knote.
2454 * Also remove it from the magic list of directly attached knotes.
2456 * Note that the unlink may have already happened from the other side, so
2457 * ignore any failures to unlink and just remove it from the kqueue list.
2459 * On success, caller is responsible for the link structure
2462 knote_unlink_wait_queue(struct knote
*kn
, struct wait_queue
*wq
, wait_queue_link_t
*wqlp
)
2464 struct kqueue
*kq
= kn
->kn_kq
;
2467 kr
= wait_queue_unlink_nofree(wq
, kq
->kq_wqs
, wqlp
);
2469 kn
->kn_status
&= ~KN_STAYQUEUED
;
2472 return (kr
!= KERN_SUCCESS
) ? EINVAL
: 0;
2476 * remove all knotes referencing a specified fd
2478 * Essentially an inlined knote_remove & knote_drop
2479 * when we know for sure that the thing is a file
2481 * Entered with the proc_fd lock already held.
2482 * It returns the same way, but may drop it temporarily.
2485 knote_fdclose(struct proc
*p
, int fd
)
2487 struct filedesc
*fdp
= p
->p_fd
;
2491 list
= &fdp
->fd_knlist
[fd
];
2492 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
2493 struct kqueue
*kq
= kn
->kn_kq
;
2496 panic("knote_fdclose: proc mismatch (kq->kq_p=%p != p=%p)", kq
->kq_p
, p
);
2502 * Convert the lock to a drop ref.
2503 * If we get it, go ahead and drop it.
2504 * Otherwise, we waited for it to
2505 * be dropped by the other guy, so
2506 * it is safe to move on in the list.
2508 if (kqlock2knotedrop(kq
, kn
)) {
2509 kn
->kn_fop
->f_detach(kn
);
2515 /* the fd tables may have changed - start over */
2516 list
= &fdp
->fd_knlist
[fd
];
2520 /* proc_fdlock held on entry (and exit) */
2522 knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
)
2524 struct klist
*list
= NULL
;
2526 if (! kn
->kn_fop
->f_isfd
) {
2527 if (fdp
->fd_knhashmask
== 0)
2528 fdp
->fd_knhash
= hashinit(CONFIG_KN_HASHSIZE
, M_KQUEUE
,
2529 &fdp
->fd_knhashmask
);
2530 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
2532 if ((u_int
)fdp
->fd_knlistsize
<= kn
->kn_id
) {
2535 if (kn
->kn_id
>= (uint64_t)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
2536 || kn
->kn_id
>= (uint64_t)maxfiles
)
2539 /* have to grow the fd_knlist */
2540 size
= fdp
->fd_knlistsize
;
2541 while (size
<= kn
->kn_id
)
2544 if (size
>= (UINT_MAX
/sizeof(struct klist
*)))
2547 MALLOC(list
, struct klist
*,
2548 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
2552 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
2553 fdp
->fd_knlistsize
* sizeof(struct klist
*));
2554 bzero((caddr_t
)list
+
2555 fdp
->fd_knlistsize
* sizeof(struct klist
*),
2556 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
2557 FREE(fdp
->fd_knlist
, M_KQUEUE
);
2558 fdp
->fd_knlist
= list
;
2559 fdp
->fd_knlistsize
= size
;
2561 list
= &fdp
->fd_knlist
[kn
->kn_id
];
2563 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
2570 * should be called at spl == 0, since we don't want to hold spl
2571 * while calling fdrop and free.
2574 knote_drop(struct knote
*kn
, __unused
struct proc
*ctxp
)
2576 struct kqueue
*kq
= kn
->kn_kq
;
2577 struct proc
*p
= kq
->kq_p
;
2578 struct filedesc
*fdp
= p
->p_fd
;
2583 if (kn
->kn_fop
->f_isfd
)
2584 list
= &fdp
->fd_knlist
[kn
->kn_id
];
2586 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
2588 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
2591 needswakeup
= (kn
->kn_status
& KN_USEWAIT
);
2596 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
, THREAD_AWAKENED
);
2598 if (kn
->kn_fop
->f_isfd
)
2599 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0);
2604 /* called with kqueue lock held */
2606 knote_activate(struct knote
*kn
, int propagate
)
2608 struct kqueue
*kq
= kn
->kn_kq
;
2610 kn
->kn_status
|= KN_ACTIVE
;
2612 kqueue_wakeup(kq
, 0);
2614 /* this is a real event: wake up the parent kq, too */
2616 KNOTE(&kq
->kq_sel
.si_note
, 0);
2619 /* called with kqueue lock held */
2621 knote_deactivate(struct knote
*kn
)
2623 kn
->kn_status
&= ~KN_ACTIVE
;
2627 /* called with kqueue lock held */
2629 knote_enqueue(struct knote
*kn
)
2631 if ((kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
)) == KN_STAYQUEUED
||
2632 (kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
| KN_DISABLED
)) == 0) {
2633 struct kqtailq
*tq
= kn
->kn_tq
;
2634 struct kqueue
*kq
= kn
->kn_kq
;
2636 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);
2637 kn
->kn_status
|= KN_QUEUED
;
2642 /* called with kqueue lock held */
2644 knote_dequeue(struct knote
*kn
)
2646 struct kqueue
*kq
= kn
->kn_kq
;
2648 if ((kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
)) == KN_QUEUED
) {
2649 struct kqtailq
*tq
= kn
->kn_tq
;
2651 TAILQ_REMOVE(tq
, kn
, kn_tqe
);
2652 kn
->kn_tq
= &kq
->kq_head
;
2653 kn
->kn_status
&= ~KN_QUEUED
;
2661 knote_zone
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
), 8192, "knote zone");
2663 /* allocate kq lock group attribute and group */
2664 kq_lck_grp_attr
= lck_grp_attr_alloc_init();
2666 kq_lck_grp
= lck_grp_alloc_init("kqueue", kq_lck_grp_attr
);
2668 /* Allocate kq lock attribute */
2669 kq_lck_attr
= lck_attr_alloc_init();
2671 /* Initialize the timer filter lock */
2672 lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
);
2674 #if VM_PRESSURE_EVENTS
2675 /* Initialize the vm pressure list lock */
2676 vm_pressure_init(kq_lck_grp
, kq_lck_attr
);
2679 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
2681 static struct knote
*
2684 return ((struct knote
*)zalloc(knote_zone
));
2688 knote_free(struct knote
*kn
)
2690 zfree(knote_zone
, kn
);
2694 #include <sys/param.h>
2695 #include <sys/socket.h>
2696 #include <sys/protosw.h>
2697 #include <sys/domain.h>
2698 #include <sys/mbuf.h>
2699 #include <sys/kern_event.h>
2700 #include <sys/malloc.h>
2701 #include <sys/sys_domain.h>
2702 #include <sys/syslog.h>
2705 static int kev_attach(struct socket
*so
, int proto
, struct proc
*p
);
2706 static int kev_detach(struct socket
*so
);
2707 static int kev_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
);
2709 struct pr_usrreqs event_usrreqs
= {
2710 pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
,
2711 pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
,
2712 pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
2713 pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
2714 pru_sosend_notsupp
, soreceive
, pru_sopoll_notsupp
2717 struct protosw eventsw
[] = {
2719 .pr_type
= SOCK_RAW
,
2720 .pr_domain
= &systemdomain
,
2721 .pr_protocol
= SYSPROTO_EVENT
,
2722 .pr_flags
= PR_ATOMIC
,
2723 .pr_usrreqs
= &event_usrreqs
,
2728 struct kern_event_head kern_event_head
;
2730 static u_int32_t static_event_id
= 0;
2731 struct domain
*sysdom
= &systemdomain
;
2732 static lck_mtx_t
*sys_mtx
;
2735 * Install the protosw's for the NKE manager. Invoked at
2736 * extension load time
2739 kern_event_init(void)
2743 if ((retval
= net_add_proto(eventsw
, &systemdomain
)) != 0) {
2744 log(LOG_WARNING
, "Can't install kernel events protocol (%d)\n", retval
);
2749 * Use the domain mutex for all system event sockets
2751 sys_mtx
= sysdom
->dom_mtx
;
2753 return(KERN_SUCCESS
);
2757 kev_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
2760 struct kern_event_pcb
*ev_pcb
;
2762 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
2766 MALLOC(ev_pcb
, struct kern_event_pcb
*, sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
);
2770 ev_pcb
->ev_socket
= so
;
2771 ev_pcb
->vendor_code_filter
= 0xffffffff;
2773 so
->so_pcb
= (caddr_t
) ev_pcb
;
2774 lck_mtx_lock(sys_mtx
);
2775 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
);
2776 lck_mtx_unlock(sys_mtx
);
2783 kev_detach(struct socket
*so
)
2785 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2788 LIST_REMOVE(ev_pcb
, ev_link
);
2789 FREE(ev_pcb
, M_PCB
);
2791 so
->so_flags
|= SOF_PCBCLEARING
;
2798 * For now, kev_vendor_code and mbuf_tags use the same
2802 errno_t
kev_vendor_code_find(
2804 u_int32_t
*out_vendor_code
)
2806 if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) {
2809 return net_str_id_find_internal(string
, out_vendor_code
, NSI_VENDOR_CODE
, 1);
2812 errno_t
kev_msg_post(struct kev_msg
*event_msg
)
2814 mbuf_tag_id_t min_vendor
, max_vendor
;
2816 net_str_id_first_last(&min_vendor
, &max_vendor
, NSI_VENDOR_CODE
);
2818 if (event_msg
== NULL
)
2821 /* Limit third parties to posting events for registered vendor codes only */
2822 if (event_msg
->vendor_code
< min_vendor
||
2823 event_msg
->vendor_code
> max_vendor
)
2828 return kev_post_msg(event_msg
);
2832 int kev_post_msg(struct kev_msg
*event_msg
)
2834 struct mbuf
*m
, *m2
;
2835 struct kern_event_pcb
*ev_pcb
;
2836 struct kern_event_msg
*ev
;
2838 u_int32_t total_size
;
2841 /* Verify the message is small enough to fit in one mbuf w/o cluster */
2842 total_size
= KEV_MSG_HEADER_SIZE
;
2844 for (i
= 0; i
< 5; i
++) {
2845 if (event_msg
->dv
[i
].data_length
== 0)
2847 total_size
+= event_msg
->dv
[i
].data_length
;
2850 if (total_size
> MLEN
) {
2854 m
= m_get(M_DONTWAIT
, MT_DATA
);
2858 ev
= mtod(m
, struct kern_event_msg
*);
2859 total_size
= KEV_MSG_HEADER_SIZE
;
2861 tmp
= (char *) &ev
->event_data
[0];
2862 for (i
= 0; i
< 5; i
++) {
2863 if (event_msg
->dv
[i
].data_length
== 0)
2866 total_size
+= event_msg
->dv
[i
].data_length
;
2867 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
2868 event_msg
->dv
[i
].data_length
);
2869 tmp
+= event_msg
->dv
[i
].data_length
;
2872 ev
->id
= ++static_event_id
;
2873 ev
->total_size
= total_size
;
2874 ev
->vendor_code
= event_msg
->vendor_code
;
2875 ev
->kev_class
= event_msg
->kev_class
;
2876 ev
->kev_subclass
= event_msg
->kev_subclass
;
2877 ev
->event_code
= event_msg
->event_code
;
2879 m
->m_len
= total_size
;
2880 lck_mtx_lock(sys_mtx
);
2881 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
2883 ev_pcb
= LIST_NEXT(ev_pcb
, ev_link
)) {
2885 if (ev_pcb
->vendor_code_filter
!= KEV_ANY_VENDOR
) {
2886 if (ev_pcb
->vendor_code_filter
!= ev
->vendor_code
)
2889 if (ev_pcb
->class_filter
!= KEV_ANY_CLASS
) {
2890 if (ev_pcb
->class_filter
!= ev
->kev_class
)
2893 if ((ev_pcb
->subclass_filter
!= KEV_ANY_SUBCLASS
) &&
2894 (ev_pcb
->subclass_filter
!= ev
->kev_subclass
))
2899 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
2902 lck_mtx_unlock(sys_mtx
);
2905 /* the socket is already locked because we hold the sys_mtx here */
2906 if (sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
))
2907 sorwakeup(ev_pcb
->ev_socket
);
2911 lck_mtx_unlock(sys_mtx
);
2916 kev_control(struct socket
*so
,
2919 __unused
struct ifnet
*ifp
,
2920 __unused
struct proc
*p
)
2922 struct kev_request
*kev_req
= (struct kev_request
*) data
;
2923 struct kern_event_pcb
*ev_pcb
;
2924 struct kev_vendor_code
*kev_vendor
;
2925 u_int32_t
*id_value
= (u_int32_t
*) data
;
2931 *id_value
= static_event_id
;
2935 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2936 ev_pcb
->vendor_code_filter
= kev_req
->vendor_code
;
2937 ev_pcb
->class_filter
= kev_req
->kev_class
;
2938 ev_pcb
->subclass_filter
= kev_req
->kev_subclass
;
2942 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2943 kev_req
->vendor_code
= ev_pcb
->vendor_code_filter
;
2944 kev_req
->kev_class
= ev_pcb
->class_filter
;
2945 kev_req
->kev_subclass
= ev_pcb
->subclass_filter
;
2948 case SIOCGKEVVENDOR
:
2949 kev_vendor
= (struct kev_vendor_code
*)data
;
2951 /* Make sure string is NULL terminated */
2952 kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0;
2954 return net_str_id_find_internal(kev_vendor
->vendor_string
,
2955 &kev_vendor
->vendor_code
, NSI_VENDOR_CODE
, 0);
2964 #endif /* SOCKETS */
2968 fill_kqueueinfo(struct kqueue
*kq
, struct kqueue_info
* kinfo
)
2970 struct vinfo_stat
* st
;
2972 /* No need for the funnel as fd is kept alive */
2974 st
= &kinfo
->kq_stat
;
2976 st
->vst_size
= kq
->kq_count
;
2977 if (kq
->kq_state
& KQ_KEV64
)
2978 st
->vst_blksize
= sizeof(struct kevent64_s
);
2980 st
->vst_blksize
= sizeof(struct kevent
);
2981 st
->vst_mode
= S_IFIFO
;
2982 if (kq
->kq_state
& KQ_SEL
)
2983 kinfo
->kq_state
|= PROC_KQUEUE_SELECT
;
2984 if (kq
->kq_state
& KQ_SLEEP
)
2985 kinfo
->kq_state
|= PROC_KQUEUE_SLEEP
;
2992 knote_markstayqueued(struct knote
*kn
)
2995 kn
->kn_status
|= KN_STAYQUEUED
;
2997 kqunlock(kn
->kn_kq
);