2 * Copyright (c) 2000-2014 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>
84 #include <sys/codesign.h>
86 #include <kern/locks.h>
87 #include <kern/clock.h>
88 #include <kern/thread_call.h>
89 #include <kern/sched_prim.h>
90 #include <kern/zalloc.h>
91 #include <kern/assert.h>
93 #include <libkern/libkern.h>
94 #include "net/net_str_id.h"
96 #include <mach/task.h>
98 #if VM_PRESSURE_EVENTS
99 #include <kern/vm_pressure.h>
102 #if CONFIG_MEMORYSTATUS
103 #include <sys/kern_memorystatus.h>
106 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system");
108 #define KQ_EVENT NULL
110 static inline void kqlock(struct kqueue
*kq
);
111 static inline void kqunlock(struct kqueue
*kq
);
113 static int kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
);
114 static int kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
);
115 static int kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
);
116 static int knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
);
118 static void kqueue_wakeup(struct kqueue
*kq
, int closed
);
119 static int kqueue_read(struct fileproc
*fp
, struct uio
*uio
,
120 int flags
, vfs_context_t ctx
);
121 static int kqueue_write(struct fileproc
*fp
, struct uio
*uio
,
122 int flags
, vfs_context_t ctx
);
123 static int kqueue_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
,
125 static int kqueue_select(struct fileproc
*fp
, int which
, void *wql
,
127 static int kqueue_close(struct fileglob
*fg
, vfs_context_t ctx
);
128 static int kqueue_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
130 static int kqueue_drain(struct fileproc
*fp
, vfs_context_t ctx
);
132 static const struct fileops kqueueops
= {
133 .fo_type
= DTYPE_KQUEUE
,
134 .fo_read
= kqueue_read
,
135 .fo_write
= kqueue_write
,
136 .fo_ioctl
= kqueue_ioctl
,
137 .fo_select
= kqueue_select
,
138 .fo_close
= kqueue_close
,
139 .fo_kqfilter
= kqueue_kqfilter
,
140 .fo_drain
= kqueue_drain
,
143 static int kevent_internal(struct proc
*p
, int iskev64
, user_addr_t changelist
,
144 int nchanges
, user_addr_t eventlist
, int nevents
, int fd
,
145 user_addr_t utimeout
, unsigned int flags
, int32_t *retval
);
146 static int kevent_copyin(user_addr_t
*addrp
, struct kevent64_s
*kevp
,
147 struct proc
*p
, int iskev64
);
148 static int kevent_copyout(struct kevent64_s
*kevp
, user_addr_t
*addrp
,
149 struct proc
*p
, int iskev64
);
150 char * kevent_description(struct kevent64_s
*kevp
, char *s
, size_t n
);
152 static int kevent_callback(struct kqueue
*kq
, struct kevent64_s
*kevp
,
154 static void kevent_continue(struct kqueue
*kq
, void *data
, int error
);
155 static void kqueue_scan_continue(void *contp
, wait_result_t wait_result
);
156 static int kqueue_process(struct kqueue
*kq
, kevent_callback_t callback
,
157 void *data
, int *countp
, struct proc
*p
);
158 static int kqueue_begin_processing(struct kqueue
*kq
);
159 static void kqueue_end_processing(struct kqueue
*kq
);
160 static int knote_process(struct knote
*kn
, kevent_callback_t callback
,
161 void *data
, struct kqtailq
*inprocessp
, struct proc
*p
);
162 static void knote_put(struct knote
*kn
);
163 static int knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
,
165 static void knote_drop(struct knote
*kn
, struct proc
*p
);
166 static void knote_activate(struct knote
*kn
, int);
167 static void knote_deactivate(struct knote
*kn
);
168 static void knote_enqueue(struct knote
*kn
);
169 static void knote_dequeue(struct knote
*kn
);
170 static struct knote
*knote_alloc(void);
171 static void knote_free(struct knote
*kn
);
173 static int filt_fileattach(struct knote
*kn
);
174 static struct filterops file_filtops
= {
176 .f_attach
= filt_fileattach
,
179 static void filt_kqdetach(struct knote
*kn
);
180 static int filt_kqueue(struct knote
*kn
, long hint
);
181 static struct filterops kqread_filtops
= {
183 .f_detach
= filt_kqdetach
,
184 .f_event
= filt_kqueue
,
187 /* placeholder for not-yet-implemented filters */
188 static int filt_badattach(struct knote
*kn
);
189 static struct filterops bad_filtops
= {
190 .f_attach
= filt_badattach
,
193 static int filt_procattach(struct knote
*kn
);
194 static void filt_procdetach(struct knote
*kn
);
195 static int filt_proc(struct knote
*kn
, long hint
);
196 static struct filterops proc_filtops
= {
197 .f_attach
= filt_procattach
,
198 .f_detach
= filt_procdetach
,
199 .f_event
= filt_proc
,
202 #if VM_PRESSURE_EVENTS
203 static int filt_vmattach(struct knote
*kn
);
204 static void filt_vmdetach(struct knote
*kn
);
205 static int filt_vm(struct knote
*kn
, long hint
);
206 static struct filterops vm_filtops
= {
207 .f_attach
= filt_vmattach
,
208 .f_detach
= filt_vmdetach
,
211 #endif /* VM_PRESSURE_EVENTS */
213 #if CONFIG_MEMORYSTATUS
214 extern struct filterops memorystatus_filtops
;
215 #endif /* CONFIG_MEMORYSTATUS */
217 extern struct filterops fs_filtops
;
219 extern struct filterops sig_filtops
;
222 static int filt_timerattach(struct knote
*kn
);
223 static void filt_timerdetach(struct knote
*kn
);
224 static int filt_timer(struct knote
*kn
, long hint
);
225 static void filt_timertouch(struct knote
*kn
, struct kevent64_s
*kev
,
227 static struct filterops timer_filtops
= {
228 .f_attach
= filt_timerattach
,
229 .f_detach
= filt_timerdetach
,
230 .f_event
= filt_timer
,
231 .f_touch
= filt_timertouch
,
235 static void filt_timerexpire(void *knx
, void *param1
);
236 static int filt_timervalidate(struct knote
*kn
);
237 static void filt_timerupdate(struct knote
*kn
);
238 static void filt_timercancel(struct knote
*kn
);
240 #define TIMER_RUNNING 0x1
241 #define TIMER_CANCELWAIT 0x2
243 static lck_mtx_t _filt_timerlock
;
244 static void filt_timerlock(void);
245 static void filt_timerunlock(void);
247 static zone_t knote_zone
;
249 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
252 extern struct filterops aio_filtops
;
255 /* Mach portset filter */
256 extern struct filterops machport_filtops
;
259 static int filt_userattach(struct knote
*kn
);
260 static void filt_userdetach(struct knote
*kn
);
261 static int filt_user(struct knote
*kn
, long hint
);
262 static void filt_usertouch(struct knote
*kn
, struct kevent64_s
*kev
,
264 static struct filterops user_filtops
= {
265 .f_attach
= filt_userattach
,
266 .f_detach
= filt_userdetach
,
267 .f_event
= filt_user
,
268 .f_touch
= filt_usertouch
,
272 * Table for all system-defined filters.
274 static struct filterops
*sysfilt_ops
[] = {
275 &file_filtops
, /* EVFILT_READ */
276 &file_filtops
, /* EVFILT_WRITE */
278 &aio_filtops
, /* EVFILT_AIO */
280 &bad_filtops
, /* EVFILT_AIO */
282 &file_filtops
, /* EVFILT_VNODE */
283 &proc_filtops
, /* EVFILT_PROC */
284 &sig_filtops
, /* EVFILT_SIGNAL */
285 &timer_filtops
, /* EVFILT_TIMER */
286 &machport_filtops
, /* EVFILT_MACHPORT */
287 &fs_filtops
, /* EVFILT_FS */
288 &user_filtops
, /* EVFILT_USER */
289 &bad_filtops
, /* unused */
290 #if VM_PRESSURE_EVENTS
291 &vm_filtops
, /* EVFILT_VM */
293 &bad_filtops
, /* EVFILT_VM */
295 &file_filtops
, /* EVFILT_SOCK */
296 #if CONFIG_MEMORYSTATUS
297 &memorystatus_filtops
, /* EVFILT_MEMORYSTATUS */
299 &bad_filtops
, /* EVFILT_MEMORYSTATUS */
304 * kqueue/note lock attributes and implementations
306 * kqueues have locks, while knotes have use counts
307 * Most of the knote state is guarded by the object lock.
308 * the knote "inuse" count and status use the kqueue lock.
310 lck_grp_attr_t
* kq_lck_grp_attr
;
311 lck_grp_t
* kq_lck_grp
;
312 lck_attr_t
* kq_lck_attr
;
315 kqlock(struct kqueue
*kq
)
317 lck_spin_lock(&kq
->kq_lock
);
321 kqunlock(struct kqueue
*kq
)
323 lck_spin_unlock(&kq
->kq_lock
);
327 * Convert a kq lock to a knote use referece.
329 * If the knote is being dropped, we can't get
330 * a use reference, so just return with it
332 * - kq locked at entry
333 * - unlock on exit if we get the use reference
336 kqlock2knoteuse(struct kqueue
*kq
, struct knote
*kn
)
338 if (kn
->kn_status
& KN_DROPPING
)
346 * Convert a kq lock to a knote use referece,
347 * but wait for attach and drop events to complete.
349 * If the knote is being dropped, we can't get
350 * a use reference, so just return with it
352 * - kq locked at entry
353 * - kq always unlocked on exit
356 kqlock2knoteusewait(struct kqueue
*kq
, struct knote
*kn
)
358 if ((kn
->kn_status
& (KN_DROPPING
| KN_ATTACHING
)) != 0) {
359 kn
->kn_status
|= KN_USEWAIT
;
360 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
,
361 &kn
->kn_status
, THREAD_UNINT
, 0);
363 thread_block(THREAD_CONTINUE_NULL
);
372 * Convert from a knote use reference back to kq lock.
374 * Drop a use reference and wake any waiters if
375 * this is the last one.
377 * The exit return indicates if the knote is
378 * still alive - but the kqueue lock is taken
382 knoteuse2kqlock(struct kqueue
*kq
, struct knote
*kn
)
385 if (--kn
->kn_inuse
== 0) {
386 if ((kn
->kn_status
& KN_ATTACHING
) != 0) {
387 kn
->kn_status
&= ~KN_ATTACHING
;
389 if ((kn
->kn_status
& KN_USEWAIT
) != 0) {
390 kn
->kn_status
&= ~KN_USEWAIT
;
391 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
,
392 &kn
->kn_status
, THREAD_AWAKENED
);
395 return ((kn
->kn_status
& KN_DROPPING
) == 0);
399 * Convert a kq lock to a knote drop reference.
401 * If the knote is in use, wait for the use count
402 * to subside. We first mark our intention to drop
403 * it - keeping other users from "piling on."
404 * If we are too late, we have to wait for the
405 * other drop to complete.
407 * - kq locked at entry
408 * - always unlocked on exit.
409 * - caller can't hold any locks that would prevent
410 * the other dropper from completing.
413 kqlock2knotedrop(struct kqueue
*kq
, struct knote
*kn
)
417 oktodrop
= ((kn
->kn_status
& (KN_DROPPING
| KN_ATTACHING
)) == 0);
418 kn
->kn_status
|= KN_DROPPING
;
420 if (kn
->kn_inuse
== 0) {
425 kn
->kn_status
|= KN_USEWAIT
;
426 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
,
429 thread_block(THREAD_CONTINUE_NULL
);
434 * Release a knote use count reference.
437 knote_put(struct knote
*kn
)
439 struct kqueue
*kq
= kn
->kn_kq
;
442 if (--kn
->kn_inuse
== 0) {
443 if ((kn
->kn_status
& KN_USEWAIT
) != 0) {
444 kn
->kn_status
&= ~KN_USEWAIT
;
445 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
,
446 &kn
->kn_status
, THREAD_AWAKENED
);
453 filt_fileattach(struct knote
*kn
)
455 return (fo_kqfilter(kn
->kn_fp
, kn
, vfs_context_current()));
458 #define f_flag f_fglob->fg_flag
459 #define f_msgcount f_fglob->fg_msgcount
460 #define f_cred f_fglob->fg_cred
461 #define f_ops f_fglob->fg_ops
462 #define f_offset f_fglob->fg_offset
463 #define f_data f_fglob->fg_data
466 filt_kqdetach(struct knote
*kn
)
468 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
471 KNOTE_DETACH(&kq
->kq_sel
.si_note
, kn
);
477 filt_kqueue(struct knote
*kn
, __unused
long hint
)
479 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
481 kn
->kn_data
= kq
->kq_count
;
482 return (kn
->kn_data
> 0);
486 filt_procattach(struct knote
*kn
)
490 assert(PID_MAX
< NOTE_PDATAMASK
);
492 if ((kn
->kn_sfflags
& (NOTE_TRACK
| NOTE_TRACKERR
| NOTE_CHILD
)) != 0)
495 p
= proc_find(kn
->kn_id
);
500 const int NoteExitStatusBits
= NOTE_EXIT
| NOTE_EXITSTATUS
;
502 if ((kn
->kn_sfflags
& NoteExitStatusBits
) == NoteExitStatusBits
)
504 pid_t selfpid
= proc_selfpid();
506 if (p
->p_ppid
== selfpid
)
507 break; /* parent => ok */
509 if ((p
->p_lflag
& P_LTRACED
) != 0 &&
510 (p
->p_oppid
== selfpid
))
511 break; /* parent-in-waiting => ok */
519 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
520 kn
->kn_ptr
.p_proc
= p
; /* store the proc handle */
522 KNOTE_ATTACH(&p
->p_klist
, kn
);
532 * The knote may be attached to a different process, which may exit,
533 * leaving nothing for the knote to be attached to. In that case,
534 * the pointer to the process will have already been nulled out.
537 filt_procdetach(struct knote
*kn
)
543 p
= kn
->kn_ptr
.p_proc
;
544 if (p
!= PROC_NULL
) {
545 kn
->kn_ptr
.p_proc
= PROC_NULL
;
546 KNOTE_DETACH(&p
->p_klist
, kn
);
553 filt_proc(struct knote
*kn
, long hint
)
556 * Note: a lot of bits in hint may be obtained from the knote
557 * To free some of those bits, see <rdar://problem/12592988> Freeing up
558 * bits in hint for filt_proc
560 /* hint is 0 when called from above */
564 /* ALWAYS CALLED WITH proc_klist_lock when (hint != 0) */
567 * mask off extra data
569 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
572 * termination lifecycle events can happen while a debugger
573 * has reparented a process, in which case notifications
574 * should be quashed except to the tracing parent. When
575 * the debugger reaps the child (either via wait4(2) or
576 * process exit), the child will be reparented to the original
577 * parent and these knotes re-fired.
579 if (event
& NOTE_EXIT
) {
580 if ((kn
->kn_ptr
.p_proc
->p_oppid
!= 0)
581 && (kn
->kn_kq
->kq_p
->p_pid
!= kn
->kn_ptr
.p_proc
->p_ppid
)) {
583 * This knote is not for the current ptrace(2) parent, ignore.
590 * if the user is interested in this event, record it.
592 if (kn
->kn_sfflags
& event
)
593 kn
->kn_fflags
|= event
;
595 #pragma clang diagnostic push
596 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
597 if ((event
== NOTE_REAP
) || ((event
== NOTE_EXIT
) && !(kn
->kn_sfflags
& NOTE_REAP
))) {
598 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
600 #pragma clang diagnostic pop
604 * The kernel has a wrapper in place that returns the same data
605 * as is collected here, in kn_data. Any changes to how
606 * NOTE_EXITSTATUS and NOTE_EXIT_DETAIL are collected
607 * should also be reflected in the proc_pidnoteexit() wrapper.
609 if (event
== NOTE_EXIT
) {
611 if ((kn
->kn_sfflags
& NOTE_EXITSTATUS
) != 0) {
612 kn
->kn_fflags
|= NOTE_EXITSTATUS
;
613 kn
->kn_data
|= (hint
& NOTE_PDATAMASK
);
615 if ((kn
->kn_sfflags
& NOTE_EXIT_DETAIL
) != 0) {
616 kn
->kn_fflags
|= NOTE_EXIT_DETAIL
;
617 if ((kn
->kn_ptr
.p_proc
->p_lflag
&
618 P_LTERM_DECRYPTFAIL
) != 0) {
619 kn
->kn_data
|= NOTE_EXIT_DECRYPTFAIL
;
621 if ((kn
->kn_ptr
.p_proc
->p_lflag
&
622 P_LTERM_JETSAM
) != 0) {
623 kn
->kn_data
|= NOTE_EXIT_MEMORY
;
624 switch (kn
->kn_ptr
.p_proc
->p_lflag
&
626 case P_JETSAM_VMPAGESHORTAGE
:
627 kn
->kn_data
|= NOTE_EXIT_MEMORY_VMPAGESHORTAGE
;
629 case P_JETSAM_VMTHRASHING
:
630 kn
->kn_data
|= NOTE_EXIT_MEMORY_VMTHRASHING
;
632 case P_JETSAM_FCTHRASHING
:
633 kn
->kn_data
|= NOTE_EXIT_MEMORY_FCTHRASHING
;
636 kn
->kn_data
|= NOTE_EXIT_MEMORY_VNODE
;
639 kn
->kn_data
|= NOTE_EXIT_MEMORY_HIWAT
;
642 kn
->kn_data
|= NOTE_EXIT_MEMORY_PID
;
644 case P_JETSAM_IDLEEXIT
:
645 kn
->kn_data
|= NOTE_EXIT_MEMORY_IDLE
;
649 if ((kn
->kn_ptr
.p_proc
->p_csflags
&
651 kn
->kn_data
|= NOTE_EXIT_CSERROR
;
657 /* atomic check, no locking need when called from above */
658 return (kn
->kn_fflags
!= 0);
661 #if VM_PRESSURE_EVENTS
663 * Virtual memory kevents
665 * author: Matt Jacobson [matthew_jacobson@apple.com]
669 filt_vmattach(struct knote
*kn
)
672 * The note will be cleared once the information has been flushed to
673 * the client. If there is still pressure, we will be re-alerted.
675 kn
->kn_flags
|= EV_CLEAR
;
676 return (vm_knote_register(kn
));
680 filt_vmdetach(struct knote
*kn
)
682 vm_knote_unregister(kn
);
686 filt_vm(struct knote
*kn
, long hint
)
688 /* hint == 0 means this is just an alive? check (always true) */
690 const pid_t pid
= (pid_t
)hint
;
691 if ((kn
->kn_sfflags
& NOTE_VM_PRESSURE
) &&
692 (kn
->kn_kq
->kq_p
->p_pid
== pid
)) {
693 kn
->kn_fflags
|= NOTE_VM_PRESSURE
;
697 return (kn
->kn_fflags
!= 0);
699 #endif /* VM_PRESSURE_EVENTS */
702 * filt_timervalidate - process data from user
704 * Converts to either interval or deadline format.
706 * The saved-data field in the knote contains the
707 * time value. The saved filter-flags indicates
708 * the unit of measurement.
710 * After validation, either the saved-data field
711 * contains the interval in absolute time, or ext[0]
712 * contains the expected deadline. If that deadline
713 * is in the past, ext[0] is 0.
715 * Returns EINVAL for unrecognized units of time.
717 * Timer filter lock is held.
721 filt_timervalidate(struct knote
*kn
)
726 switch (kn
->kn_sfflags
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) {
728 multiplier
= NSEC_PER_SEC
;
731 multiplier
= NSEC_PER_USEC
;
736 case 0: /* milliseconds (default) */
737 multiplier
= NSEC_PER_SEC
/ 1000;
743 /* transform the slop delta(leeway) in kn_ext[1] if passed to same time scale */
744 if(kn
->kn_sfflags
& NOTE_LEEWAY
){
745 nanoseconds_to_absolutetime((uint64_t)kn
->kn_ext
[1] * multiplier
, &raw
);
749 nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata
* multiplier
, &raw
);
754 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
) {
756 clock_nsec_t nanoseconds
;
759 clock_get_calendar_nanotime(&seconds
, &nanoseconds
);
760 nanoseconds_to_absolutetime((uint64_t)seconds
* NSEC_PER_SEC
+
764 /* time has already passed */
768 clock_absolutetime_interval_to_deadline(raw
,
779 * filt_timerupdate - compute the next deadline
781 * Repeating timers store their interval in kn_sdata. Absolute
782 * timers have already calculated the deadline, stored in ext[0].
784 * On return, the next deadline (or zero if no deadline is needed)
785 * is stored in kn_ext[0].
787 * Timer filter lock is held.
790 filt_timerupdate(struct knote
*kn
)
792 /* if there's no interval, deadline is just in kn_ext[0] */
793 if (kn
->kn_sdata
== 0)
796 /* if timer hasn't fired before, fire in interval nsecs */
797 if (kn
->kn_ext
[0] == 0) {
798 clock_absolutetime_interval_to_deadline(kn
->kn_sdata
,
802 * If timer has fired before, schedule the next pop
803 * relative to the last intended deadline.
805 * We could check for whether the deadline has expired,
806 * but the thread call layer can handle that.
808 kn
->kn_ext
[0] += kn
->kn_sdata
;
813 * filt_timerexpire - the timer callout routine
815 * Just propagate the timer event into the knote
816 * filter routine (by going through the knote
817 * synchronization point). Pass a hint to
818 * indicate this is a real event, not just a
822 filt_timerexpire(void *knx
, __unused
void *spare
)
824 struct klist timer_list
;
825 struct knote
*kn
= knx
;
829 kn
->kn_hookid
&= ~TIMER_RUNNING
;
831 /* no "object" for timers, so fake a list */
832 SLIST_INIT(&timer_list
);
833 SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);
834 KNOTE(&timer_list
, 1);
836 /* if someone is waiting for timer to pop */
837 if (kn
->kn_hookid
& TIMER_CANCELWAIT
) {
838 struct kqueue
*kq
= kn
->kn_kq
;
839 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_hook
,
847 * Cancel a running timer (or wait for the pop).
848 * Timer filter lock is held.
851 filt_timercancel(struct knote
*kn
)
853 struct kqueue
*kq
= kn
->kn_kq
;
854 thread_call_t callout
= kn
->kn_hook
;
857 if (kn
->kn_hookid
& TIMER_RUNNING
) {
858 /* cancel the callout if we can */
859 cancelled
= thread_call_cancel(callout
);
861 kn
->kn_hookid
&= ~TIMER_RUNNING
;
863 /* we have to wait for the expire routine. */
864 kn
->kn_hookid
|= TIMER_CANCELWAIT
;
865 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
,
866 &kn
->kn_hook
, THREAD_UNINT
, 0);
868 thread_block(THREAD_CONTINUE_NULL
);
870 assert((kn
->kn_hookid
& TIMER_RUNNING
) == 0);
876 * Allocate a thread call for the knote's lifetime, and kick off the timer.
879 filt_timerattach(struct knote
*kn
)
881 thread_call_t callout
;
884 callout
= thread_call_allocate(filt_timerexpire
, kn
);
889 error
= filt_timervalidate(kn
);
895 kn
->kn_hook
= (void*)callout
;
898 /* absolute=EV_ONESHOT */
899 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
)
900 kn
->kn_flags
|= EV_ONESHOT
;
902 filt_timerupdate(kn
);
904 kn
->kn_flags
|= EV_CLEAR
;
905 unsigned int timer_flags
= 0;
906 if (kn
->kn_sfflags
& NOTE_CRITICAL
)
907 timer_flags
|= THREAD_CALL_DELAY_USER_CRITICAL
;
908 else if (kn
->kn_sfflags
& NOTE_BACKGROUND
)
909 timer_flags
|= THREAD_CALL_DELAY_USER_BACKGROUND
;
911 timer_flags
|= THREAD_CALL_DELAY_USER_NORMAL
;
913 if (kn
->kn_sfflags
& NOTE_LEEWAY
)
914 timer_flags
|= THREAD_CALL_DELAY_LEEWAY
;
916 thread_call_enter_delayed_with_leeway(callout
, NULL
,
917 kn
->kn_ext
[0], kn
->kn_ext
[1], timer_flags
);
919 kn
->kn_hookid
|= TIMER_RUNNING
;
930 * Shut down the timer if it's running, and free the callout.
933 filt_timerdetach(struct knote
*kn
)
935 thread_call_t callout
;
939 callout
= (thread_call_t
)kn
->kn_hook
;
940 filt_timercancel(kn
);
944 thread_call_free(callout
);
950 filt_timer(struct knote
*kn
, long hint
)
955 /* real timer pop -- timer lock held by filt_timerexpire */
958 if (((kn
->kn_hookid
& TIMER_CANCELWAIT
) == 0) &&
959 ((kn
->kn_flags
& EV_ONESHOT
) == 0)) {
961 /* evaluate next time to fire */
962 filt_timerupdate(kn
);
965 unsigned int timer_flags
= 0;
967 /* keep the callout and re-arm */
968 if (kn
->kn_sfflags
& NOTE_CRITICAL
)
969 timer_flags
|= THREAD_CALL_DELAY_USER_CRITICAL
;
970 else if (kn
->kn_sfflags
& NOTE_BACKGROUND
)
971 timer_flags
|= THREAD_CALL_DELAY_USER_BACKGROUND
;
973 timer_flags
|= THREAD_CALL_DELAY_USER_NORMAL
;
975 if (kn
->kn_sfflags
& NOTE_LEEWAY
)
976 timer_flags
|= THREAD_CALL_DELAY_LEEWAY
;
978 thread_call_enter_delayed_with_leeway(kn
->kn_hook
, NULL
,
979 kn
->kn_ext
[0], kn
->kn_ext
[1], timer_flags
);
981 kn
->kn_hookid
|= TIMER_RUNNING
;
991 result
= (kn
->kn_data
!= 0);
1000 * filt_timertouch - update knote with new user input
1002 * Cancel and restart the timer based on new user data. When
1003 * the user picks up a knote, clear the count of how many timer
1004 * pops have gone off (in kn_data).
1007 filt_timertouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
1013 case EVENT_REGISTER
:
1014 /* cancel current call */
1015 filt_timercancel(kn
);
1017 /* recalculate deadline */
1018 kn
->kn_sdata
= kev
->data
;
1019 kn
->kn_sfflags
= kev
->fflags
;
1020 kn
->kn_ext
[0] = kev
->ext
[0];
1021 kn
->kn_ext
[1] = kev
->ext
[1];
1023 error
= filt_timervalidate(kn
);
1025 /* no way to report error, so mark it in the knote */
1026 kn
->kn_flags
|= EV_ERROR
;
1027 kn
->kn_data
= error
;
1031 /* start timer if necessary */
1032 filt_timerupdate(kn
);
1034 if (kn
->kn_ext
[0]) {
1035 unsigned int timer_flags
= 0;
1036 if (kn
->kn_sfflags
& NOTE_CRITICAL
)
1037 timer_flags
|= THREAD_CALL_DELAY_USER_CRITICAL
;
1038 else if (kn
->kn_sfflags
& NOTE_BACKGROUND
)
1039 timer_flags
|= THREAD_CALL_DELAY_USER_BACKGROUND
;
1041 timer_flags
|= THREAD_CALL_DELAY_USER_NORMAL
;
1043 if (kn
->kn_sfflags
& NOTE_LEEWAY
)
1044 timer_flags
|= THREAD_CALL_DELAY_LEEWAY
;
1046 thread_call_enter_delayed_with_leeway(kn
->kn_hook
, NULL
,
1047 kn
->kn_ext
[0], kn
->kn_ext
[1], timer_flags
);
1049 kn
->kn_hookid
|= TIMER_RUNNING
;
1051 /* pretend the timer has fired */
1058 /* reset the timer pop count in kn_data */
1059 *kev
= kn
->kn_kevent
;
1062 if (kn
->kn_flags
& EV_CLEAR
)
1066 panic("%s: - invalid type (%ld)", __func__
, type
);
1074 filt_timerlock(void)
1076 lck_mtx_lock(&_filt_timerlock
);
1080 filt_timerunlock(void)
1082 lck_mtx_unlock(&_filt_timerlock
);
1086 filt_userattach(struct knote
*kn
)
1088 /* EVFILT_USER knotes are not attached to anything in the kernel */
1090 if (kn
->kn_fflags
& NOTE_TRIGGER
) {
1099 filt_userdetach(__unused
struct knote
*kn
)
1101 /* EVFILT_USER knotes are not attached to anything in the kernel */
1105 filt_user(struct knote
*kn
, __unused
long hint
)
1107 return (kn
->kn_hookid
);
1111 filt_usertouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
1115 case EVENT_REGISTER
:
1116 if (kev
->fflags
& NOTE_TRIGGER
) {
1120 ffctrl
= kev
->fflags
& NOTE_FFCTRLMASK
;
1121 kev
->fflags
&= NOTE_FFLAGSMASK
;
1126 OSBitAndAtomic(kev
->fflags
, &kn
->kn_sfflags
);
1129 OSBitOrAtomic(kev
->fflags
, &kn
->kn_sfflags
);
1132 kn
->kn_sfflags
= kev
->fflags
;
1135 kn
->kn_sdata
= kev
->data
;
1138 *kev
= kn
->kn_kevent
;
1139 kev
->fflags
= (volatile UInt32
)kn
->kn_sfflags
;
1140 kev
->data
= kn
->kn_sdata
;
1141 if (kn
->kn_flags
& EV_CLEAR
) {
1148 panic("%s: - invalid type (%ld)", __func__
, type
);
1154 * JMM - placeholder for not-yet-implemented filters
1157 filt_badattach(__unused
struct knote
*kn
)
1163 kqueue_alloc(struct proc
*p
)
1165 struct filedesc
*fdp
= p
->p_fd
;
1168 MALLOC_ZONE(kq
, struct kqueue
*, sizeof (struct kqueue
), M_KQUEUE
,
1171 wait_queue_set_t wqs
;
1173 wqs
= wait_queue_set_alloc(SYNC_POLICY_FIFO
|
1174 SYNC_POLICY_PREPOST
);
1176 bzero(kq
, sizeof (struct kqueue
));
1177 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
);
1178 TAILQ_INIT(&kq
->kq_head
);
1182 FREE_ZONE(kq
, sizeof (struct kqueue
), M_KQUEUE
);
1186 if (fdp
->fd_knlistsize
< 0) {
1188 if (fdp
->fd_knlistsize
< 0)
1189 fdp
->fd_knlistsize
= 0; /* this process has had a kq */
1197 * kqueue_dealloc - detach all knotes from a kqueue and free it
1199 * We walk each list looking for knotes referencing this
1200 * this kqueue. If we find one, we try to drop it. But
1201 * if we fail to get a drop reference, that will wait
1202 * until it is dropped. So, we can just restart again
1203 * safe in the assumption that the list will eventually
1204 * not contain any more references to this kqueue (either
1205 * we dropped them all, or someone else did).
1207 * Assumes no new events are being added to the kqueue.
1208 * Nothing locked on entry or exit.
1211 kqueue_dealloc(struct kqueue
*kq
)
1213 struct proc
*p
= kq
->kq_p
;
1214 struct filedesc
*fdp
= p
->p_fd
;
1219 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
1220 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
1221 while (kn
!= NULL
) {
1222 if (kq
== kn
->kn_kq
) {
1225 /* drop it ourselves or wait */
1226 if (kqlock2knotedrop(kq
, kn
)) {
1227 kn
->kn_fop
->f_detach(kn
);
1231 /* start over at beginning of list */
1232 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
1235 kn
= SLIST_NEXT(kn
, kn_link
);
1238 if (fdp
->fd_knhashmask
!= 0) {
1239 for (i
= 0; i
< (int)fdp
->fd_knhashmask
+ 1; i
++) {
1240 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
1241 while (kn
!= NULL
) {
1242 if (kq
== kn
->kn_kq
) {
1245 /* drop it ourselves or wait */
1246 if (kqlock2knotedrop(kq
, kn
)) {
1247 kn
->kn_fop
->f_detach(kn
);
1251 /* start over at beginning of list */
1252 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
1255 kn
= SLIST_NEXT(kn
, kn_link
);
1262 * before freeing the wait queue set for this kqueue,
1263 * make sure it is unlinked from all its containing (select) sets.
1265 wait_queue_unlink_all((wait_queue_t
)kq
->kq_wqs
);
1266 wait_queue_set_free(kq
->kq_wqs
);
1267 lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
);
1268 FREE_ZONE(kq
, sizeof (struct kqueue
), M_KQUEUE
);
1272 kqueue_body(struct proc
*p
, fp_allocfn_t fp_zalloc
, void *cra
, int32_t *retval
)
1275 struct fileproc
*fp
;
1278 error
= falloc_withalloc(p
,
1279 &fp
, &fd
, vfs_context_current(), fp_zalloc
, cra
);
1284 kq
= kqueue_alloc(p
);
1290 fp
->f_flag
= FREAD
| FWRITE
;
1291 fp
->f_ops
= &kqueueops
;
1295 *fdflags(p
, fd
) |= UF_EXCLOSE
;
1296 procfdtbl_releasefd(p
, fd
, NULL
);
1297 fp_drop(p
, fd
, fp
, 1);
1305 kqueue(struct proc
*p
, __unused
struct kqueue_args
*uap
, int32_t *retval
)
1307 return (kqueue_body(p
, fileproc_alloc_init
, NULL
, retval
));
1311 kevent_copyin(user_addr_t
*addrp
, struct kevent64_s
*kevp
, struct proc
*p
,
1318 advance
= sizeof (struct kevent64_s
);
1319 error
= copyin(*addrp
, (caddr_t
)kevp
, advance
);
1320 } else if (IS_64BIT_PROCESS(p
)) {
1321 struct user64_kevent kev64
;
1322 bzero(kevp
, sizeof (struct kevent64_s
));
1324 advance
= sizeof (kev64
);
1325 error
= copyin(*addrp
, (caddr_t
)&kev64
, advance
);
1328 kevp
->ident
= kev64
.ident
;
1329 kevp
->filter
= kev64
.filter
;
1330 kevp
->flags
= kev64
.flags
;
1331 kevp
->fflags
= kev64
.fflags
;
1332 kevp
->data
= kev64
.data
;
1333 kevp
->udata
= kev64
.udata
;
1335 struct user32_kevent kev32
;
1336 bzero(kevp
, sizeof (struct kevent64_s
));
1338 advance
= sizeof (kev32
);
1339 error
= copyin(*addrp
, (caddr_t
)&kev32
, advance
);
1342 kevp
->ident
= (uintptr_t)kev32
.ident
;
1343 kevp
->filter
= kev32
.filter
;
1344 kevp
->flags
= kev32
.flags
;
1345 kevp
->fflags
= kev32
.fflags
;
1346 kevp
->data
= (intptr_t)kev32
.data
;
1347 kevp
->udata
= CAST_USER_ADDR_T(kev32
.udata
);
1355 kevent_copyout(struct kevent64_s
*kevp
, user_addr_t
*addrp
, struct proc
*p
,
1362 advance
= sizeof (struct kevent64_s
);
1363 error
= copyout((caddr_t
)kevp
, *addrp
, advance
);
1364 } else if (IS_64BIT_PROCESS(p
)) {
1365 struct user64_kevent kev64
;
1368 * deal with the special case of a user-supplied
1369 * value of (uintptr_t)-1.
1371 kev64
.ident
= (kevp
->ident
== (uintptr_t)-1) ?
1372 (uint64_t)-1LL : (uint64_t)kevp
->ident
;
1374 kev64
.filter
= kevp
->filter
;
1375 kev64
.flags
= kevp
->flags
;
1376 kev64
.fflags
= kevp
->fflags
;
1377 kev64
.data
= (int64_t) kevp
->data
;
1378 kev64
.udata
= kevp
->udata
;
1379 advance
= sizeof (kev64
);
1380 error
= copyout((caddr_t
)&kev64
, *addrp
, advance
);
1382 struct user32_kevent kev32
;
1384 kev32
.ident
= (uint32_t)kevp
->ident
;
1385 kev32
.filter
= kevp
->filter
;
1386 kev32
.flags
= kevp
->flags
;
1387 kev32
.fflags
= kevp
->fflags
;
1388 kev32
.data
= (int32_t)kevp
->data
;
1389 kev32
.udata
= kevp
->udata
;
1390 advance
= sizeof (kev32
);
1391 error
= copyout((caddr_t
)&kev32
, *addrp
, advance
);
1399 * kevent_continue - continue a kevent syscall after blocking
1401 * assume we inherit a use count on the kq fileglob.
1405 kevent_continue(__unused
struct kqueue
*kq
, void *data
, int error
)
1407 struct _kevent
*cont_args
;
1408 struct fileproc
*fp
;
1412 struct proc
*p
= current_proc();
1414 cont_args
= (struct _kevent
*)data
;
1415 noutputs
= cont_args
->eventout
;
1416 retval
= cont_args
->retval
;
1420 fp_drop(p
, fd
, fp
, 0);
1422 /* don't restart after signals... */
1423 if (error
== ERESTART
)
1425 else if (error
== EWOULDBLOCK
)
1429 unix_syscall_return(error
);
1433 * kevent - [syscall] register and wait for kernel events
1437 kevent(struct proc
*p
, struct kevent_args
*uap
, int32_t *retval
)
1439 return (kevent_internal(p
,
1447 0, /* no flags from old kevent() call */
1452 kevent64(struct proc
*p
, struct kevent64_args
*uap
, int32_t *retval
)
1454 return (kevent_internal(p
,
1467 kevent_internal(struct proc
*p
, int iskev64
, user_addr_t changelist
,
1468 int nchanges
, user_addr_t ueventlist
, int nevents
, int fd
,
1469 user_addr_t utimeout
, __unused
unsigned int flags
,
1472 struct _kevent
*cont_args
;
1475 struct fileproc
*fp
;
1476 struct kevent64_s kev
;
1477 int error
, noutputs
;
1480 /* convert timeout to absolute - if we have one */
1481 if (utimeout
!= USER_ADDR_NULL
) {
1483 if (IS_64BIT_PROCESS(p
)) {
1484 struct user64_timespec ts
;
1485 error
= copyin(utimeout
, &ts
, sizeof(ts
));
1486 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ull
) != 0)
1489 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1491 struct user32_timespec ts
;
1492 error
= copyin(utimeout
, &ts
, sizeof(ts
));
1493 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1497 if (itimerfix(&rtv
))
1499 getmicrouptime(&atv
);
1500 timevaladd(&atv
, &rtv
);
1506 /* get a usecount for the kq itself */
1507 if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0)
1510 /* each kq should only be used for events of one type */
1512 if (kq
->kq_state
& (KQ_KEV32
| KQ_KEV64
)) {
1513 if (((iskev64
&& (kq
->kq_state
& KQ_KEV32
)) ||
1514 (!iskev64
&& (kq
->kq_state
& KQ_KEV64
)))) {
1520 kq
->kq_state
|= (iskev64
? KQ_KEV64
: KQ_KEV32
);
1524 /* register all the change requests the user provided... */
1526 while (nchanges
> 0 && error
== 0) {
1527 error
= kevent_copyin(&changelist
, &kev
, p
, iskev64
);
1531 kev
.flags
&= ~EV_SYSFLAGS
;
1532 error
= kevent_register(kq
, &kev
, p
);
1533 if ((error
|| (kev
.flags
& EV_RECEIPT
)) && nevents
> 0) {
1534 kev
.flags
= EV_ERROR
;
1536 error
= kevent_copyout(&kev
, &ueventlist
, p
, iskev64
);
1545 /* store the continuation/completion data in the uthread */
1546 ut
= (uthread_t
)get_bsdthread_info(current_thread());
1547 cont_args
= &ut
->uu_kevent
.ss_kevent
;
1550 cont_args
->retval
= retval
;
1551 cont_args
->eventlist
= ueventlist
;
1552 cont_args
->eventcount
= nevents
;
1553 cont_args
->eventout
= noutputs
;
1554 cont_args
->eventsize
= iskev64
;
1556 if (nevents
> 0 && noutputs
== 0 && error
== 0)
1557 error
= kqueue_scan(kq
, kevent_callback
,
1558 kevent_continue
, cont_args
,
1560 kevent_continue(kq
, cont_args
, error
);
1563 fp_drop(p
, fd
, fp
, 0);
1569 * kevent_callback - callback for each individual event
1571 * called with nothing locked
1572 * caller holds a reference on the kqueue
1575 kevent_callback(__unused
struct kqueue
*kq
, struct kevent64_s
*kevp
,
1578 struct _kevent
*cont_args
;
1582 cont_args
= (struct _kevent
*)data
;
1583 assert(cont_args
->eventout
< cont_args
->eventcount
);
1585 iskev64
= cont_args
->eventsize
;
1588 * Copy out the appropriate amount of event data for this user.
1590 error
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc(),
1594 * If there isn't space for additional events, return
1595 * a harmless error to stop the processing here
1597 if (error
== 0 && ++cont_args
->eventout
== cont_args
->eventcount
)
1598 error
= EWOULDBLOCK
;
1603 * kevent_description - format a description of a kevent for diagnostic output
1605 * called with a 128-byte string buffer
1609 kevent_description(struct kevent64_s
*kevp
, char *s
, size_t n
)
1613 "{.ident=%#llx, .filter=%d, .flags=%#x, .fflags=%#x, .data=%#llx, .udata=%#llx, .ext[0]=%#llx, .ext[1]=%#llx}",
1627 * kevent_register - add a new event to a kqueue
1629 * Creates a mapping between the event source and
1630 * the kqueue via a knote data structure.
1632 * Because many/most the event sources are file
1633 * descriptor related, the knote is linked off
1634 * the filedescriptor table for quick access.
1636 * called with nothing locked
1637 * caller holds a reference on the kqueue
1641 kevent_register(struct kqueue
*kq
, struct kevent64_s
*kev
,
1642 __unused
struct proc
*ctxp
)
1644 struct proc
*p
= kq
->kq_p
;
1645 struct filedesc
*fdp
= p
->p_fd
;
1646 struct filterops
*fops
;
1647 struct fileproc
*fp
= NULL
;
1648 struct knote
*kn
= NULL
;
1651 if (kev
->filter
< 0) {
1652 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
1654 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
1658 * filter attach routine is responsible for insuring that
1659 * the identifier can be attached to it.
1661 printf("unknown filter: %d\n", kev
->filter
);
1666 /* this iocount needs to be dropped if it is not registered */
1668 if (fops
->f_isfd
&& (error
= fp_lookup(p
, kev
->ident
, &fp
, 1)) != 0) {
1674 /* fd-based knotes are linked off the fd table */
1675 if (kev
->ident
< (u_int
)fdp
->fd_knlistsize
) {
1676 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
1677 if (kq
== kn
->kn_kq
&&
1678 kev
->filter
== kn
->kn_filter
)
1682 /* hash non-fd knotes here too */
1683 if (fdp
->fd_knhashmask
!= 0) {
1686 list
= &fdp
->fd_knhash
[
1687 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
1688 SLIST_FOREACH(kn
, list
, kn_link
)
1689 if (kev
->ident
== kn
->kn_id
&&
1691 kev
->filter
== kn
->kn_filter
)
1697 * kn now contains the matching knote, or NULL if no match
1700 if ((kev
->flags
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) {
1709 kn
->kn_tq
= &kq
->kq_head
;
1711 kn
->kn_sfflags
= kev
->fflags
;
1712 kn
->kn_sdata
= kev
->data
;
1715 kn
->kn_kevent
= *kev
;
1716 kn
->kn_inuse
= 1; /* for f_attach() */
1717 kn
->kn_status
= KN_ATTACHING
;
1719 /* before anyone can find it */
1720 if (kev
->flags
& EV_DISABLE
)
1721 kn
->kn_status
|= KN_DISABLED
;
1723 error
= knote_fdpattach(kn
, fdp
, p
);
1732 * apply reference count to knote structure, and
1733 * do not release it at the end of this routine.
1737 error
= fops
->f_attach(kn
);
1743 * Failed to attach correctly, so drop.
1744 * All other possible users/droppers
1745 * have deferred to us.
1747 kn
->kn_status
|= KN_DROPPING
;
1751 } else if (kn
->kn_status
& KN_DROPPING
) {
1753 * Attach succeeded, but someone else
1754 * deferred their drop - now we have
1755 * to do it for them (after detaching).
1758 kn
->kn_fop
->f_detach(kn
);
1762 kn
->kn_status
&= ~KN_ATTACHING
;
1770 /* existing knote - get kqueue lock */
1774 if (kev
->flags
& EV_DELETE
) {
1776 kn
->kn_status
|= KN_DISABLED
;
1777 if (kqlock2knotedrop(kq
, kn
)) {
1778 kn
->kn_fop
->f_detach(kn
);
1784 /* update status flags for existing knote */
1785 if (kev
->flags
& EV_DISABLE
) {
1787 kn
->kn_status
|= KN_DISABLED
;
1788 } else if (kev
->flags
& EV_ENABLE
) {
1789 kn
->kn_status
&= ~KN_DISABLED
;
1790 if (kn
->kn_status
& KN_ACTIVE
)
1795 * The user may change some filter values after the
1796 * initial EV_ADD, but doing so will not reset any
1797 * filter which have already been triggered.
1799 kn
->kn_kevent
.udata
= kev
->udata
;
1800 if (fops
->f_isfd
|| fops
->f_touch
== NULL
) {
1801 kn
->kn_sfflags
= kev
->fflags
;
1802 kn
->kn_sdata
= kev
->data
;
1806 * If somebody is in the middle of dropping this
1807 * knote - go find/insert a new one. But we have
1808 * wait for this one to go away first. Attaches
1809 * running in parallel may also drop/modify the
1810 * knote. Wait for those to complete as well and
1811 * then start over if we encounter one.
1813 if (!kqlock2knoteusewait(kq
, kn
)) {
1814 /* kqueue, proc_fdlock both unlocked */
1819 * Call touch routine to notify filter of changes
1822 if (!fops
->f_isfd
&& fops
->f_touch
!= NULL
)
1823 fops
->f_touch(kn
, kev
, EVENT_REGISTER
);
1825 /* still have use ref on knote */
1828 * If the knote is not marked to always stay enqueued,
1829 * invoke the filter routine to see if it should be
1832 if ((kn
->kn_status
& KN_STAYQUEUED
) == 0 && kn
->kn_fop
->f_event(kn
, 0)) {
1833 if (knoteuse2kqlock(kq
, kn
))
1834 knote_activate(kn
, 1);
1842 fp_drop(p
, kev
->ident
, fp
, 0);
1848 * knote_process - process a triggered event
1850 * Validate that it is really still a triggered event
1851 * by calling the filter routines (if necessary). Hold
1852 * a use reference on the knote to avoid it being detached.
1853 * If it is still considered triggered, invoke the callback
1854 * routine provided and move it to the provided inprocess
1857 * caller holds a reference on the kqueue.
1858 * kqueue locked on entry and exit - but may be dropped
1861 knote_process(struct knote
*kn
,
1862 kevent_callback_t callback
,
1864 struct kqtailq
*inprocessp
,
1867 struct kqueue
*kq
= kn
->kn_kq
;
1868 struct kevent64_s kev
;
1874 * Determine the kevent state we want to return.
1876 * Some event states need to be revalidated before returning
1877 * them, others we take the snapshot at the time the event
1880 * Events with non-NULL f_touch operations must be touched.
1881 * Triggered events must fill in kev for the callback.
1883 * Convert our lock to a use-count and call the event's
1884 * filter routine(s) to update.
1886 if ((kn
->kn_status
& KN_DISABLED
) != 0) {
1893 revalidate
= ((kn
->kn_status
& KN_STAYQUEUED
) != 0 ||
1894 (kn
->kn_flags
& EV_ONESHOT
) == 0);
1895 touch
= (!kn
->kn_fop
->f_isfd
&& kn
->kn_fop
->f_touch
!= NULL
);
1897 if (revalidate
|| touch
) {
1899 knote_deactivate(kn
);
1901 /* call the filter/touch routines with just a ref */
1902 if (kqlock2knoteuse(kq
, kn
)) {
1903 /* if we have to revalidate, call the filter */
1905 result
= kn
->kn_fop
->f_event(kn
, 0);
1909 * capture the kevent data - using touch if
1912 if (result
&& touch
) {
1913 kn
->kn_fop
->f_touch(kn
, &kev
,
1918 * convert back to a kqlock - bail if the knote
1921 if (!knoteuse2kqlock(kq
, kn
)) {
1922 return (EJUSTRETURN
);
1923 } else if (result
) {
1925 * if revalidated as alive, make sure
1928 if (!(kn
->kn_status
& KN_ACTIVE
)) {
1929 knote_activate(kn
, 0);
1933 * capture all events that occurred
1937 kev
= kn
->kn_kevent
;
1940 } else if ((kn
->kn_status
& KN_STAYQUEUED
) == 0) {
1942 * was already dequeued, so just bail on
1945 return (EJUSTRETURN
);
1948 return (EJUSTRETURN
);
1951 kev
= kn
->kn_kevent
;
1955 /* move knote onto inprocess queue */
1956 assert(kn
->kn_tq
== &kq
->kq_head
);
1957 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1958 kn
->kn_tq
= inprocessp
;
1959 TAILQ_INSERT_TAIL(inprocessp
, kn
, kn_tqe
);
1962 * Determine how to dispatch the knote for future event handling.
1963 * not-fired: just return (do not callout).
1964 * One-shot: deactivate it.
1965 * Clear: deactivate and clear the state.
1966 * Dispatch: don't clear state, just deactivate it and mark it disabled.
1967 * All others: just leave where they are.
1971 return (EJUSTRETURN
);
1972 } else if ((kn
->kn_flags
& EV_ONESHOT
) != 0) {
1973 knote_deactivate(kn
);
1974 if (kqlock2knotedrop(kq
, kn
)) {
1975 kn
->kn_fop
->f_detach(kn
);
1978 } else if ((kn
->kn_flags
& (EV_CLEAR
| EV_DISPATCH
)) != 0) {
1979 if ((kn
->kn_flags
& EV_DISPATCH
) != 0) {
1980 /* deactivate and disable all dispatch knotes */
1981 knote_deactivate(kn
);
1982 kn
->kn_status
|= KN_DISABLED
;
1983 } else if (!touch
|| kn
->kn_fflags
== 0) {
1984 /* only deactivate if nothing since the touch */
1985 knote_deactivate(kn
);
1987 if (!touch
&& (kn
->kn_flags
& EV_CLEAR
) != 0) {
1988 /* manually clear non-touch knotes */
1995 * leave on inprocess queue. We'll
1996 * move all the remaining ones back
1997 * the kq queue and wakeup any
1998 * waiters when we are done.
2003 /* callback to handle each event as we find it */
2004 error
= (callback
)(kq
, &kev
, data
);
2011 * Return 0 to indicate that processing should proceed,
2012 * -1 if there is nothing to process.
2014 * Called with kqueue locked and returns the same way,
2015 * but may drop lock temporarily.
2018 kqueue_begin_processing(struct kqueue
*kq
)
2021 if (kq
->kq_count
== 0) {
2025 /* if someone else is processing the queue, wait */
2026 if (kq
->kq_nprocess
!= 0) {
2027 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
,
2028 &kq
->kq_nprocess
, THREAD_UNINT
, 0);
2029 kq
->kq_state
|= KQ_PROCWAIT
;
2031 thread_block(THREAD_CONTINUE_NULL
);
2034 kq
->kq_nprocess
= 1;
2041 * Called with kqueue lock held.
2044 kqueue_end_processing(struct kqueue
*kq
)
2046 kq
->kq_nprocess
= 0;
2047 if (kq
->kq_state
& KQ_PROCWAIT
) {
2048 kq
->kq_state
&= ~KQ_PROCWAIT
;
2049 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
,
2050 &kq
->kq_nprocess
, THREAD_AWAKENED
);
2055 * kqueue_process - process the triggered events in a kqueue
2057 * Walk the queued knotes and validate that they are
2058 * really still triggered events by calling the filter
2059 * routines (if necessary). Hold a use reference on
2060 * the knote to avoid it being detached. For each event
2061 * that is still considered triggered, invoke the
2062 * callback routine provided.
2064 * caller holds a reference on the kqueue.
2065 * kqueue locked on entry and exit - but may be dropped
2066 * kqueue list locked (held for duration of call)
2070 kqueue_process(struct kqueue
*kq
,
2071 kevent_callback_t callback
,
2076 struct kqtailq inprocess
;
2081 TAILQ_INIT(&inprocess
);
2083 if (kqueue_begin_processing(kq
) == -1) {
2085 /* Nothing to process */
2090 * Clear any pre-posted status from previous runs, so we
2091 * only detect events that occur during this run.
2093 wait_queue_sub_clearrefs(kq
->kq_wqs
);
2096 * loop through the enqueued knotes, processing each one and
2097 * revalidating those that need it. As they are processed,
2098 * they get moved to the inprocess queue (so the loop can end).
2103 while (error
== 0 &&
2104 (kn
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
2105 error
= knote_process(kn
, callback
, data
, &inprocess
, p
);
2106 if (error
== EJUSTRETURN
)
2113 * With the kqueue still locked, move any knotes
2114 * remaining on the inprocess queue back to the
2115 * kq's queue and wake up any waiters.
2117 while ((kn
= TAILQ_FIRST(&inprocess
)) != NULL
) {
2118 assert(kn
->kn_tq
== &inprocess
);
2119 TAILQ_REMOVE(&inprocess
, kn
, kn_tqe
);
2120 kn
->kn_tq
= &kq
->kq_head
;
2121 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
2124 kqueue_end_processing(kq
);
2132 kqueue_scan_continue(void *data
, wait_result_t wait_result
)
2134 thread_t self
= current_thread();
2135 uthread_t ut
= (uthread_t
)get_bsdthread_info(self
);
2136 struct _kqueue_scan
* cont_args
= &ut
->uu_kevent
.ss_kqueue_scan
;
2137 struct kqueue
*kq
= (struct kqueue
*)data
;
2141 /* convert the (previous) wait_result to a proper error */
2142 switch (wait_result
) {
2143 case THREAD_AWAKENED
:
2145 error
= kqueue_process(kq
, cont_args
->call
, cont_args
, &count
,
2147 if (error
== 0 && count
== 0) {
2148 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
,
2149 KQ_EVENT
, THREAD_ABORTSAFE
, cont_args
->deadline
);
2150 kq
->kq_state
|= KQ_SLEEP
;
2152 thread_block_parameter(kqueue_scan_continue
, kq
);
2157 case THREAD_TIMED_OUT
:
2158 error
= EWOULDBLOCK
;
2160 case THREAD_INTERRUPTED
:
2164 panic("%s: - invalid wait_result (%d)", __func__
,
2169 /* call the continuation with the results */
2170 assert(cont_args
->cont
!= NULL
);
2171 (cont_args
->cont
)(kq
, cont_args
->data
, error
);
2176 * kqueue_scan - scan and wait for events in a kqueue
2178 * Process the triggered events in a kqueue.
2180 * If there are no events triggered arrange to
2181 * wait for them. If the caller provided a
2182 * continuation routine, then kevent_scan will
2185 * The callback routine must be valid.
2186 * The caller must hold a use-count reference on the kq.
2190 kqueue_scan(struct kqueue
*kq
,
2191 kevent_callback_t callback
,
2192 kqueue_continue_t continuation
,
2194 struct timeval
*atvp
,
2197 thread_continue_t cont
= THREAD_CONTINUE_NULL
;
2202 assert(callback
!= NULL
);
2206 wait_result_t wait_result
;
2210 * Make a pass through the kq to find events already
2214 error
= kqueue_process(kq
, callback
, data
, &count
, p
);
2216 break; /* lock still held */
2218 /* looks like we have to consider blocking */
2221 /* convert the timeout to a deadline once */
2222 if (atvp
->tv_sec
|| atvp
->tv_usec
) {
2225 clock_get_uptime(&now
);
2226 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec
* NSEC_PER_SEC
+
2227 atvp
->tv_usec
* (long)NSEC_PER_USEC
,
2229 if (now
>= deadline
) {
2230 /* non-blocking call */
2231 error
= EWOULDBLOCK
;
2232 break; /* lock still held */
2235 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
2237 deadline
= 0; /* block forever */
2241 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
2242 struct _kqueue_scan
*cont_args
= &ut
->uu_kevent
.ss_kqueue_scan
;
2244 cont_args
->call
= callback
;
2245 cont_args
->cont
= continuation
;
2246 cont_args
->deadline
= deadline
;
2247 cont_args
->data
= data
;
2248 cont
= kqueue_scan_continue
;
2252 /* go ahead and wait */
2253 wait_queue_assert_wait_with_leeway((wait_queue_t
)kq
->kq_wqs
,
2254 KQ_EVENT
, THREAD_ABORTSAFE
, TIMEOUT_URGENCY_USER_NORMAL
,
2256 kq
->kq_state
|= KQ_SLEEP
;
2258 wait_result
= thread_block_parameter(cont
, kq
);
2259 /* NOTREACHED if (continuation != NULL) */
2261 switch (wait_result
) {
2262 case THREAD_AWAKENED
:
2264 case THREAD_TIMED_OUT
:
2265 return (EWOULDBLOCK
);
2266 case THREAD_INTERRUPTED
:
2269 panic("%s: - bad wait_result (%d)", __func__
,
2281 * This could be expanded to call kqueue_scan, if desired.
2285 kqueue_read(__unused
struct fileproc
*fp
,
2286 __unused
struct uio
*uio
,
2288 __unused vfs_context_t ctx
)
2295 kqueue_write(__unused
struct fileproc
*fp
,
2296 __unused
struct uio
*uio
,
2298 __unused vfs_context_t ctx
)
2305 kqueue_ioctl(__unused
struct fileproc
*fp
,
2306 __unused u_long com
,
2307 __unused caddr_t data
,
2308 __unused vfs_context_t ctx
)
2315 kqueue_select(struct fileproc
*fp
, int which
, void *wql
,
2316 __unused vfs_context_t ctx
)
2318 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
2320 struct kqtailq inprocessq
;
2326 TAILQ_INIT(&inprocessq
);
2330 * If this is the first pass, link the wait queue associated with the
2331 * the kqueue onto the wait queue set for the select(). Normally we
2332 * use selrecord() for this, but it uses the wait queue within the
2333 * selinfo structure and we need to use the main one for the kqueue to
2334 * catch events from KN_STAYQUEUED sources. So we do the linkage manually.
2335 * (The select() call will unlink them when it ends).
2338 thread_t cur_act
= current_thread();
2339 struct uthread
* ut
= get_bsdthread_info(cur_act
);
2341 kq
->kq_state
|= KQ_SEL
;
2342 wait_queue_link_noalloc((wait_queue_t
)kq
->kq_wqs
, ut
->uu_wqset
,
2343 (wait_queue_link_t
)wql
);
2346 if (kqueue_begin_processing(kq
) == -1) {
2351 if (kq
->kq_count
!= 0) {
2353 * there is something queued - but it might be a
2354 * KN_STAYQUEUED knote, which may or may not have
2355 * any events pending. So, we have to walk the
2356 * list of knotes to see, and peek at the stay-
2357 * queued ones to be really sure.
2359 while ((kn
= (struct knote
*)TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
2360 if ((kn
->kn_status
& KN_STAYQUEUED
) == 0) {
2365 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
2366 TAILQ_INSERT_TAIL(&inprocessq
, kn
, kn_tqe
);
2368 if (kqlock2knoteuse(kq
, kn
)) {
2371 peek
= kn
->kn_fop
->f_peek(kn
);
2372 if (knoteuse2kqlock(kq
, kn
)) {
2385 /* Return knotes to active queue */
2386 while ((kn
= TAILQ_FIRST(&inprocessq
)) != NULL
) {
2387 TAILQ_REMOVE(&inprocessq
, kn
, kn_tqe
);
2388 kn
->kn_tq
= &kq
->kq_head
;
2389 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
2392 kqueue_end_processing(kq
);
2402 kqueue_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
2404 struct kqueue
*kq
= (struct kqueue
*)fg
->fg_data
;
2413 * The callers has taken a use-count reference on this kqueue and will donate it
2414 * to the kqueue we are being added to. This keeps the kqueue from closing until
2415 * that relationship is torn down.
2418 kqueue_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused vfs_context_t ctx
)
2420 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
2421 struct kqueue
*parentkq
= kn
->kn_kq
;
2423 if (parentkq
== kq
||
2424 kn
->kn_filter
!= EVFILT_READ
)
2428 * We have to avoid creating a cycle when nesting kqueues
2429 * inside another. Rather than trying to walk the whole
2430 * potential DAG of nested kqueues, we just use a simple
2431 * ceiling protocol. When a kqueue is inserted into another,
2432 * we check that the (future) parent is not already nested
2433 * into another kqueue at a lower level than the potenial
2434 * child (because it could indicate a cycle). If that test
2435 * passes, we just mark the nesting levels accordingly.
2439 if (parentkq
->kq_level
> 0 &&
2440 parentkq
->kq_level
< kq
->kq_level
)
2445 /* set parent level appropriately */
2446 if (parentkq
->kq_level
== 0)
2447 parentkq
->kq_level
= 2;
2448 if (parentkq
->kq_level
< kq
->kq_level
+ 1)
2449 parentkq
->kq_level
= kq
->kq_level
+ 1;
2452 kn
->kn_fop
= &kqread_filtops
;
2454 KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
);
2455 /* indicate nesting in child, if needed */
2456 if (kq
->kq_level
== 0)
2464 * kqueue_drain - called when kq is closed
2468 kqueue_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2470 struct kqueue
*kq
= (struct kqueue
*)fp
->f_fglob
->fg_data
;
2472 kqueue_wakeup(kq
, 1);
2479 kqueue_stat(struct kqueue
*kq
, void *ub
, int isstat64
, proc_t p
)
2482 if (isstat64
!= 0) {
2483 struct stat64
*sb64
= (struct stat64
*)ub
;
2485 bzero((void *)sb64
, sizeof(*sb64
));
2486 sb64
->st_size
= kq
->kq_count
;
2487 if (kq
->kq_state
& KQ_KEV64
)
2488 sb64
->st_blksize
= sizeof(struct kevent64_s
);
2490 sb64
->st_blksize
= IS_64BIT_PROCESS(p
) ? sizeof(struct user64_kevent
) : sizeof(struct user32_kevent
);
2491 sb64
->st_mode
= S_IFIFO
;
2493 struct stat
*sb
= (struct stat
*)ub
;
2495 bzero((void *)sb
, sizeof(*sb
));
2496 sb
->st_size
= kq
->kq_count
;
2497 if (kq
->kq_state
& KQ_KEV64
)
2498 sb
->st_blksize
= sizeof(struct kevent64_s
);
2500 sb
->st_blksize
= IS_64BIT_PROCESS(p
) ? sizeof(struct user64_kevent
) : sizeof(struct user32_kevent
);
2501 sb
->st_mode
= S_IFIFO
;
2508 * Called with the kqueue locked
2511 kqueue_wakeup(struct kqueue
*kq
, int closed
)
2513 if ((kq
->kq_state
& (KQ_SLEEP
| KQ_SEL
)) != 0 || kq
->kq_nprocess
> 0) {
2514 kq
->kq_state
&= ~(KQ_SLEEP
| KQ_SEL
);
2515 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, KQ_EVENT
,
2516 (closed
) ? THREAD_INTERRUPTED
: THREAD_AWAKENED
);
2521 klist_init(struct klist
*list
)
2528 * Query/Post each knote in the object's list
2530 * The object lock protects the list. It is assumed
2531 * that the filter/event routine for the object can
2532 * determine that the object is already locked (via
2533 * the hint) and not deadlock itself.
2535 * The object lock should also hold off pending
2536 * detach/drop operations. But we'll prevent it here
2537 * too - just in case.
2540 knote(struct klist
*list
, long hint
)
2544 SLIST_FOREACH(kn
, list
, kn_selnext
) {
2545 struct kqueue
*kq
= kn
->kn_kq
;
2548 if (kqlock2knoteuse(kq
, kn
)) {
2551 /* call the event with only a use count */
2552 result
= kn
->kn_fop
->f_event(kn
, hint
);
2554 /* if its not going away and triggered */
2555 if (knoteuse2kqlock(kq
, kn
) && result
)
2556 knote_activate(kn
, 1);
2557 /* lock held again */
2564 * attach a knote to the specified list. Return true if this is the first entry.
2565 * The list is protected by whatever lock the object it is associated with uses.
2568 knote_attach(struct klist
*list
, struct knote
*kn
)
2570 int ret
= SLIST_EMPTY(list
);
2571 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
2576 * detach a knote from the specified list. Return true if that was the last entry.
2577 * The list is protected by whatever lock the object it is associated with uses.
2580 knote_detach(struct klist
*list
, struct knote
*kn
)
2582 SLIST_REMOVE(list
, kn
, knote
, kn_selnext
);
2583 return (SLIST_EMPTY(list
));
2587 * For a given knote, link a provided wait queue directly with the kqueue.
2588 * Wakeups will happen via recursive wait queue support. But nothing will move
2589 * the knote to the active list at wakeup (nothing calls knote()). Instead,
2590 * we permanently enqueue them here.
2592 * kqueue and knote references are held by caller.
2594 * caller provides the wait queue link structure.
2597 knote_link_wait_queue(struct knote
*kn
, struct wait_queue
*wq
, wait_queue_link_t wql
)
2599 struct kqueue
*kq
= kn
->kn_kq
;
2602 kr
= wait_queue_link_noalloc(wq
, kq
->kq_wqs
, wql
);
2603 if (kr
== KERN_SUCCESS
) {
2604 knote_markstayqueued(kn
);
2612 * Unlink the provided wait queue from the kqueue associated with a knote.
2613 * Also remove it from the magic list of directly attached knotes.
2615 * Note that the unlink may have already happened from the other side, so
2616 * ignore any failures to unlink and just remove it from the kqueue list.
2618 * On success, caller is responsible for the link structure
2621 knote_unlink_wait_queue(struct knote
*kn
, struct wait_queue
*wq
, wait_queue_link_t
*wqlp
)
2623 struct kqueue
*kq
= kn
->kn_kq
;
2626 kr
= wait_queue_unlink_nofree(wq
, kq
->kq_wqs
, wqlp
);
2628 kn
->kn_status
&= ~KN_STAYQUEUED
;
2631 return ((kr
!= KERN_SUCCESS
) ? EINVAL
: 0);
2635 * remove all knotes referencing a specified fd
2637 * Essentially an inlined knote_remove & knote_drop
2638 * when we know for sure that the thing is a file
2640 * Entered with the proc_fd lock already held.
2641 * It returns the same way, but may drop it temporarily.
2644 knote_fdclose(struct proc
*p
, int fd
)
2646 struct filedesc
*fdp
= p
->p_fd
;
2650 list
= &fdp
->fd_knlist
[fd
];
2651 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
2652 struct kqueue
*kq
= kn
->kn_kq
;
2655 panic("%s: proc mismatch (kq->kq_p=%p != p=%p)",
2656 __func__
, kq
->kq_p
, p
);
2662 * Convert the lock to a drop ref.
2663 * If we get it, go ahead and drop it.
2664 * Otherwise, we waited for it to
2665 * be dropped by the other guy, so
2666 * it is safe to move on in the list.
2668 if (kqlock2knotedrop(kq
, kn
)) {
2669 kn
->kn_fop
->f_detach(kn
);
2675 /* the fd tables may have changed - start over */
2676 list
= &fdp
->fd_knlist
[fd
];
2680 /* proc_fdlock held on entry (and exit) */
2682 knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
)
2684 struct klist
*list
= NULL
;
2686 if (! kn
->kn_fop
->f_isfd
) {
2687 if (fdp
->fd_knhashmask
== 0)
2688 fdp
->fd_knhash
= hashinit(CONFIG_KN_HASHSIZE
, M_KQUEUE
,
2689 &fdp
->fd_knhashmask
);
2690 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
2692 if ((u_int
)fdp
->fd_knlistsize
<= kn
->kn_id
) {
2695 if (kn
->kn_id
>= (uint64_t)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
2696 || kn
->kn_id
>= (uint64_t)maxfiles
)
2699 /* have to grow the fd_knlist */
2700 size
= fdp
->fd_knlistsize
;
2701 while (size
<= kn
->kn_id
)
2704 if (size
>= (UINT_MAX
/sizeof(struct klist
*)))
2707 MALLOC(list
, struct klist
*,
2708 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
2712 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
2713 fdp
->fd_knlistsize
* sizeof(struct klist
*));
2714 bzero((caddr_t
)list
+
2715 fdp
->fd_knlistsize
* sizeof(struct klist
*),
2716 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
2717 FREE(fdp
->fd_knlist
, M_KQUEUE
);
2718 fdp
->fd_knlist
= list
;
2719 fdp
->fd_knlistsize
= size
;
2721 list
= &fdp
->fd_knlist
[kn
->kn_id
];
2723 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
2730 * should be called at spl == 0, since we don't want to hold spl
2731 * while calling fdrop and free.
2734 knote_drop(struct knote
*kn
, __unused
struct proc
*ctxp
)
2736 struct kqueue
*kq
= kn
->kn_kq
;
2737 struct proc
*p
= kq
->kq_p
;
2738 struct filedesc
*fdp
= p
->p_fd
;
2743 if (kn
->kn_fop
->f_isfd
)
2744 list
= &fdp
->fd_knlist
[kn
->kn_id
];
2746 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
2748 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
2751 needswakeup
= (kn
->kn_status
& KN_USEWAIT
);
2756 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
,
2759 if (kn
->kn_fop
->f_isfd
)
2760 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0);
2765 /* called with kqueue lock held */
2767 knote_activate(struct knote
*kn
, int propagate
)
2769 struct kqueue
*kq
= kn
->kn_kq
;
2771 kn
->kn_status
|= KN_ACTIVE
;
2773 kqueue_wakeup(kq
, 0);
2775 /* this is a real event: wake up the parent kq, too */
2777 KNOTE(&kq
->kq_sel
.si_note
, 0);
2780 /* called with kqueue lock held */
2782 knote_deactivate(struct knote
*kn
)
2784 kn
->kn_status
&= ~KN_ACTIVE
;
2788 /* called with kqueue lock held */
2790 knote_enqueue(struct knote
*kn
)
2792 if ((kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
)) == KN_STAYQUEUED
||
2793 (kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
| KN_DISABLED
)) == 0) {
2794 struct kqtailq
*tq
= kn
->kn_tq
;
2795 struct kqueue
*kq
= kn
->kn_kq
;
2797 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);
2798 kn
->kn_status
|= KN_QUEUED
;
2803 /* called with kqueue lock held */
2805 knote_dequeue(struct knote
*kn
)
2807 struct kqueue
*kq
= kn
->kn_kq
;
2809 if ((kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
)) == KN_QUEUED
) {
2810 struct kqtailq
*tq
= kn
->kn_tq
;
2812 TAILQ_REMOVE(tq
, kn
, kn_tqe
);
2813 kn
->kn_tq
= &kq
->kq_head
;
2814 kn
->kn_status
&= ~KN_QUEUED
;
2822 knote_zone
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
),
2823 8192, "knote zone");
2825 /* allocate kq lock group attribute and group */
2826 kq_lck_grp_attr
= lck_grp_attr_alloc_init();
2828 kq_lck_grp
= lck_grp_alloc_init("kqueue", kq_lck_grp_attr
);
2830 /* Allocate kq lock attribute */
2831 kq_lck_attr
= lck_attr_alloc_init();
2833 /* Initialize the timer filter lock */
2834 lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
);
2836 #if VM_PRESSURE_EVENTS
2837 /* Initialize the vm pressure list lock */
2838 vm_pressure_init(kq_lck_grp
, kq_lck_attr
);
2841 #if CONFIG_MEMORYSTATUS
2842 /* Initialize the memorystatus list lock */
2843 memorystatus_kevent_init(kq_lck_grp
, kq_lck_attr
);
2846 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
2848 static struct knote
*
2851 return ((struct knote
*)zalloc(knote_zone
));
2855 knote_free(struct knote
*kn
)
2857 zfree(knote_zone
, kn
);
2861 #include <sys/param.h>
2862 #include <sys/socket.h>
2863 #include <sys/protosw.h>
2864 #include <sys/domain.h>
2865 #include <sys/mbuf.h>
2866 #include <sys/kern_event.h>
2867 #include <sys/malloc.h>
2868 #include <sys/sys_domain.h>
2869 #include <sys/syslog.h>
2872 #define ROUNDUP64(x) P2ROUNDUP((x), sizeof (u_int64_t))
2876 #define ADVANCE64(p, n) (void*)((char *)(p) + ROUNDUP64(n))
2879 static lck_grp_attr_t
*kev_lck_grp_attr
;
2880 static lck_attr_t
*kev_lck_attr
;
2881 static lck_grp_t
*kev_lck_grp
;
2882 static decl_lck_rw_data(,kev_lck_data
);
2883 static lck_rw_t
*kev_rwlock
= &kev_lck_data
;
2885 static int kev_attach(struct socket
*so
, int proto
, struct proc
*p
);
2886 static int kev_detach(struct socket
*so
);
2887 static int kev_control(struct socket
*so
, u_long cmd
, caddr_t data
,
2888 struct ifnet
*ifp
, struct proc
*p
);
2889 static lck_mtx_t
* event_getlock(struct socket
*, int);
2890 static int event_lock(struct socket
*, int, void *);
2891 static int event_unlock(struct socket
*, int, void *);
2893 static int event_sofreelastref(struct socket
*);
2894 static void kev_delete(struct kern_event_pcb
*);
2896 static struct pr_usrreqs event_usrreqs
= {
2897 .pru_attach
= kev_attach
,
2898 .pru_control
= kev_control
,
2899 .pru_detach
= kev_detach
,
2900 .pru_soreceive
= soreceive
,
2903 static struct protosw eventsw
[] = {
2905 .pr_type
= SOCK_RAW
,
2906 .pr_protocol
= SYSPROTO_EVENT
,
2907 .pr_flags
= PR_ATOMIC
,
2908 .pr_usrreqs
= &event_usrreqs
,
2909 .pr_lock
= event_lock
,
2910 .pr_unlock
= event_unlock
,
2911 .pr_getlock
= event_getlock
,
2915 __private_extern__
int kevt_getstat SYSCTL_HANDLER_ARGS
;
2916 __private_extern__
int kevt_pcblist SYSCTL_HANDLER_ARGS
;
2918 SYSCTL_NODE(_net_systm
, OID_AUTO
, kevt
,
2919 CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Kernel event family");
2921 struct kevtstat kevtstat
;
2922 SYSCTL_PROC(_net_systm_kevt
, OID_AUTO
, stats
,
2923 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
2924 kevt_getstat
, "S,kevtstat", "");
2926 SYSCTL_PROC(_net_systm_kevt
, OID_AUTO
, pcblist
,
2927 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
2928 kevt_pcblist
, "S,xkevtpcb", "");
2931 event_getlock(struct socket
*so
, int locktype
)
2933 #pragma unused(locktype)
2934 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*)so
->so_pcb
;
2936 if (so
->so_pcb
!= NULL
) {
2937 if (so
->so_usecount
< 0)
2938 panic("%s: so=%p usecount=%d lrh= %s\n", __func__
,
2939 so
, so
->so_usecount
, solockhistory_nr(so
));
2942 panic("%s: so=%p NULL NO so_pcb %s\n", __func__
,
2943 so
, solockhistory_nr(so
));
2946 return (&ev_pcb
->evp_mtx
);
2950 event_lock(struct socket
*so
, int refcount
, void *lr
)
2955 lr_saved
= __builtin_return_address(0);
2959 if (so
->so_pcb
!= NULL
) {
2960 lck_mtx_lock(&((struct kern_event_pcb
*)so
->so_pcb
)->evp_mtx
);
2962 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__
,
2963 so
, lr_saved
, solockhistory_nr(so
));
2967 if (so
->so_usecount
< 0) {
2968 panic("%s: so=%p so_pcb=%p lr=%p ref=%d lrh= %s\n", __func__
,
2969 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
,
2970 solockhistory_nr(so
));
2977 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2978 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2983 event_unlock(struct socket
*so
, int refcount
, void *lr
)
2986 lck_mtx_t
*mutex_held
;
2989 lr_saved
= __builtin_return_address(0);
2996 if (so
->so_usecount
< 0) {
2997 panic("%s: so=%p usecount=%d lrh= %s\n", __func__
,
2998 so
, so
->so_usecount
, solockhistory_nr(so
));
3001 if (so
->so_pcb
== NULL
) {
3002 panic("%s: so=%p NO PCB usecount=%d lr=%p lrh= %s\n", __func__
,
3003 so
, so
->so_usecount
, (void *)lr_saved
,
3004 solockhistory_nr(so
));
3007 mutex_held
= (&((struct kern_event_pcb
*)so
->so_pcb
)->evp_mtx
);
3009 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
3010 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
3011 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
3013 if (so
->so_usecount
== 0) {
3014 VERIFY(so
->so_flags
& SOF_PCBCLEARING
);
3015 event_sofreelastref(so
);
3017 lck_mtx_unlock(mutex_held
);
3024 event_sofreelastref(struct socket
*so
)
3026 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*)so
->so_pcb
;
3028 lck_mtx_assert(&(ev_pcb
->evp_mtx
), LCK_MTX_ASSERT_OWNED
);
3033 * Disable upcall in the event another thread is in kev_post_msg()
3034 * appending record to the receive socket buffer, since sbwakeup()
3035 * may release the socket lock otherwise.
3037 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
3038 so
->so_snd
.sb_flags
&= ~SB_UPCALL
;
3039 so
->so_event
= sonullevent
;
3040 lck_mtx_unlock(&(ev_pcb
->evp_mtx
));
3042 lck_mtx_assert(&(ev_pcb
->evp_mtx
), LCK_MTX_ASSERT_NOTOWNED
);
3043 lck_rw_lock_exclusive(kev_rwlock
);
3044 LIST_REMOVE(ev_pcb
, evp_link
);
3045 kevtstat
.kes_pcbcount
--;
3046 kevtstat
.kes_gencnt
++;
3047 lck_rw_done(kev_rwlock
);
3050 sofreelastref(so
, 1);
3054 static int event_proto_count
= (sizeof (eventsw
) / sizeof (struct protosw
));
3057 struct kern_event_head kern_event_head
;
3059 static u_int32_t static_event_id
= 0;
3061 #define EVPCB_ZONE_MAX 65536
3062 #define EVPCB_ZONE_NAME "kerneventpcb"
3063 static struct zone
*ev_pcb_zone
;
3066 * Install the protosw's for the NKE manager. Invoked at extension load time
3069 kern_event_init(struct domain
*dp
)
3074 VERIFY(!(dp
->dom_flags
& DOM_INITIALIZED
));
3075 VERIFY(dp
== systemdomain
);
3077 kev_lck_grp_attr
= lck_grp_attr_alloc_init();
3078 if (kev_lck_grp_attr
== NULL
) {
3079 panic("%s: lck_grp_attr_alloc_init failed\n", __func__
);
3083 kev_lck_grp
= lck_grp_alloc_init("Kernel Event Protocol",
3085 if (kev_lck_grp
== NULL
) {
3086 panic("%s: lck_grp_alloc_init failed\n", __func__
);
3090 kev_lck_attr
= lck_attr_alloc_init();
3091 if (kev_lck_attr
== NULL
) {
3092 panic("%s: lck_attr_alloc_init failed\n", __func__
);
3096 lck_rw_init(kev_rwlock
, kev_lck_grp
, kev_lck_attr
);
3097 if (kev_rwlock
== NULL
) {
3098 panic("%s: lck_mtx_alloc_init failed\n", __func__
);
3102 for (i
= 0, pr
= &eventsw
[0]; i
< event_proto_count
; i
++, pr
++)
3103 net_add_proto(pr
, dp
, 1);
3105 ev_pcb_zone
= zinit(sizeof(struct kern_event_pcb
),
3106 EVPCB_ZONE_MAX
* sizeof(struct kern_event_pcb
), 0, EVPCB_ZONE_NAME
);
3107 if (ev_pcb_zone
== NULL
) {
3108 panic("%s: failed allocating ev_pcb_zone", __func__
);
3111 zone_change(ev_pcb_zone
, Z_EXPAND
, TRUE
);
3112 zone_change(ev_pcb_zone
, Z_CALLERACCT
, TRUE
);
3116 kev_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
3119 struct kern_event_pcb
*ev_pcb
;
3121 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
3125 if ((ev_pcb
= (struct kern_event_pcb
*)zalloc(ev_pcb_zone
)) == NULL
) {
3128 bzero(ev_pcb
, sizeof(struct kern_event_pcb
));
3129 lck_mtx_init(&ev_pcb
->evp_mtx
, kev_lck_grp
, kev_lck_attr
);
3131 ev_pcb
->evp_socket
= so
;
3132 ev_pcb
->evp_vendor_code_filter
= 0xffffffff;
3134 so
->so_pcb
= (caddr_t
) ev_pcb
;
3135 lck_rw_lock_exclusive(kev_rwlock
);
3136 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, evp_link
);
3137 kevtstat
.kes_pcbcount
++;
3138 kevtstat
.kes_gencnt
++;
3139 lck_rw_done(kev_rwlock
);
3145 kev_delete(struct kern_event_pcb
*ev_pcb
)
3147 VERIFY(ev_pcb
!= NULL
);
3148 lck_mtx_destroy(&ev_pcb
->evp_mtx
, kev_lck_grp
);
3149 zfree(ev_pcb_zone
, ev_pcb
);
3153 kev_detach(struct socket
*so
)
3155 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
3157 if (ev_pcb
!= NULL
) {
3158 soisdisconnected(so
);
3159 so
->so_flags
|= SOF_PCBCLEARING
;
3166 * For now, kev_vendor_code and mbuf_tags use the same
3169 errno_t
kev_vendor_code_find(
3171 u_int32_t
*out_vendor_code
)
3173 if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) {
3176 return (net_str_id_find_internal(string
, out_vendor_code
,
3177 NSI_VENDOR_CODE
, 1));
3181 kev_msg_post(struct kev_msg
*event_msg
)
3183 mbuf_tag_id_t min_vendor
, max_vendor
;
3185 net_str_id_first_last(&min_vendor
, &max_vendor
, NSI_VENDOR_CODE
);
3187 if (event_msg
== NULL
)
3191 * Limit third parties to posting events for registered vendor codes
3194 if (event_msg
->vendor_code
< min_vendor
||
3195 event_msg
->vendor_code
> max_vendor
) {
3196 OSIncrementAtomic64((SInt64
*)&kevtstat
.kes_badvendor
);
3199 return (kev_post_msg(event_msg
));
3203 kev_post_msg(struct kev_msg
*event_msg
)
3205 struct mbuf
*m
, *m2
;
3206 struct kern_event_pcb
*ev_pcb
;
3207 struct kern_event_msg
*ev
;
3209 u_int32_t total_size
;
3212 /* Verify the message is small enough to fit in one mbuf w/o cluster */
3213 total_size
= KEV_MSG_HEADER_SIZE
;
3215 for (i
= 0; i
< 5; i
++) {
3216 if (event_msg
->dv
[i
].data_length
== 0)
3218 total_size
+= event_msg
->dv
[i
].data_length
;
3221 if (total_size
> MLEN
) {
3222 OSIncrementAtomic64((SInt64
*)&kevtstat
.kes_toobig
);
3226 m
= m_get(M_DONTWAIT
, MT_DATA
);
3228 OSIncrementAtomic64((SInt64
*)&kevtstat
.kes_nomem
);
3231 ev
= mtod(m
, struct kern_event_msg
*);
3232 total_size
= KEV_MSG_HEADER_SIZE
;
3234 tmp
= (char *) &ev
->event_data
[0];
3235 for (i
= 0; i
< 5; i
++) {
3236 if (event_msg
->dv
[i
].data_length
== 0)
3239 total_size
+= event_msg
->dv
[i
].data_length
;
3240 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
3241 event_msg
->dv
[i
].data_length
);
3242 tmp
+= event_msg
->dv
[i
].data_length
;
3245 ev
->id
= ++static_event_id
;
3246 ev
->total_size
= total_size
;
3247 ev
->vendor_code
= event_msg
->vendor_code
;
3248 ev
->kev_class
= event_msg
->kev_class
;
3249 ev
->kev_subclass
= event_msg
->kev_subclass
;
3250 ev
->event_code
= event_msg
->event_code
;
3252 m
->m_len
= total_size
;
3253 lck_rw_lock_shared(kev_rwlock
);
3254 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
3256 ev_pcb
= LIST_NEXT(ev_pcb
, evp_link
)) {
3257 lck_mtx_lock(&ev_pcb
->evp_mtx
);
3258 if (ev_pcb
->evp_socket
->so_pcb
== NULL
) {
3259 lck_mtx_unlock(&ev_pcb
->evp_mtx
);
3262 if (ev_pcb
->evp_vendor_code_filter
!= KEV_ANY_VENDOR
) {
3263 if (ev_pcb
->evp_vendor_code_filter
!= ev
->vendor_code
) {
3264 lck_mtx_unlock(&ev_pcb
->evp_mtx
);
3268 if (ev_pcb
->evp_class_filter
!= KEV_ANY_CLASS
) {
3269 if (ev_pcb
->evp_class_filter
!= ev
->kev_class
) {
3270 lck_mtx_unlock(&ev_pcb
->evp_mtx
);
3274 if ((ev_pcb
->evp_subclass_filter
!=
3275 KEV_ANY_SUBCLASS
) &&
3276 (ev_pcb
->evp_subclass_filter
!=
3277 ev
->kev_subclass
)) {
3278 lck_mtx_unlock(&ev_pcb
->evp_mtx
);
3284 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
3286 OSIncrementAtomic64((SInt64
*)&kevtstat
.kes_nomem
);
3288 lck_mtx_unlock(&ev_pcb
->evp_mtx
);
3289 lck_rw_done(kev_rwlock
);
3292 if (sbappendrecord(&ev_pcb
->evp_socket
->so_rcv
, m2
)) {
3294 * We use "m" for the socket stats as it would be
3295 * unsafe to use "m2"
3297 so_inc_recv_data_stat(ev_pcb
->evp_socket
,
3298 1, m
->m_len
, SO_TC_BE
);
3300 sorwakeup(ev_pcb
->evp_socket
);
3301 OSIncrementAtomic64((SInt64
*)&kevtstat
.kes_posted
);
3303 OSIncrementAtomic64((SInt64
*)&kevtstat
.kes_fullsock
);
3305 lck_mtx_unlock(&ev_pcb
->evp_mtx
);
3308 lck_rw_done(kev_rwlock
);
3314 kev_control(struct socket
*so
,
3317 __unused
struct ifnet
*ifp
,
3318 __unused
struct proc
*p
)
3320 struct kev_request
*kev_req
= (struct kev_request
*) data
;
3321 struct kern_event_pcb
*ev_pcb
;
3322 struct kev_vendor_code
*kev_vendor
;
3323 u_int32_t
*id_value
= (u_int32_t
*) data
;
3327 *id_value
= static_event_id
;
3330 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
3331 ev_pcb
->evp_vendor_code_filter
= kev_req
->vendor_code
;
3332 ev_pcb
->evp_class_filter
= kev_req
->kev_class
;
3333 ev_pcb
->evp_subclass_filter
= kev_req
->kev_subclass
;
3336 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
3337 kev_req
->vendor_code
= ev_pcb
->evp_vendor_code_filter
;
3338 kev_req
->kev_class
= ev_pcb
->evp_class_filter
;
3339 kev_req
->kev_subclass
= ev_pcb
->evp_subclass_filter
;
3341 case SIOCGKEVVENDOR
:
3342 kev_vendor
= (struct kev_vendor_code
*)data
;
3343 /* Make sure string is NULL terminated */
3344 kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0;
3345 return (net_str_id_find_internal(kev_vendor
->vendor_string
,
3346 &kev_vendor
->vendor_code
, NSI_VENDOR_CODE
, 0));
3355 kevt_getstat SYSCTL_HANDLER_ARGS
3357 #pragma unused(oidp, arg1, arg2)
3360 lck_rw_lock_shared(kev_rwlock
);
3362 if (req
->newptr
!= USER_ADDR_NULL
) {
3366 if (req
->oldptr
== USER_ADDR_NULL
) {
3367 req
->oldidx
= sizeof(struct kevtstat
);
3371 error
= SYSCTL_OUT(req
, &kevtstat
,
3372 MIN(sizeof(struct kevtstat
), req
->oldlen
));
3374 lck_rw_done(kev_rwlock
);
3379 __private_extern__
int
3380 kevt_pcblist SYSCTL_HANDLER_ARGS
3382 #pragma unused(oidp, arg1, arg2)
3385 struct xsystmgen xsg
;
3387 size_t item_size
= ROUNDUP64(sizeof (struct xkevtpcb
)) +
3388 ROUNDUP64(sizeof (struct xsocket_n
)) +
3389 2 * ROUNDUP64(sizeof (struct xsockbuf_n
)) +
3390 ROUNDUP64(sizeof (struct xsockstat_n
));
3391 struct kern_event_pcb
*ev_pcb
;
3393 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
3397 lck_rw_lock_shared(kev_rwlock
);
3399 n
= kevtstat
.kes_pcbcount
;
3401 if (req
->oldptr
== USER_ADDR_NULL
) {
3402 req
->oldidx
= (n
+ n
/8) * item_size
;
3405 if (req
->newptr
!= USER_ADDR_NULL
) {
3409 bzero(&xsg
, sizeof (xsg
));
3410 xsg
.xg_len
= sizeof (xsg
);
3412 xsg
.xg_gen
= kevtstat
.kes_gencnt
;
3413 xsg
.xg_sogen
= so_gencnt
;
3414 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
3419 * We are done if there is no pcb
3426 for (i
= 0, ev_pcb
= LIST_FIRST(&kern_event_head
);
3427 i
< n
&& ev_pcb
!= NULL
;
3428 i
++, ev_pcb
= LIST_NEXT(ev_pcb
, evp_link
)) {
3429 struct xkevtpcb
*xk
= (struct xkevtpcb
*)buf
;
3430 struct xsocket_n
*xso
= (struct xsocket_n
*)
3431 ADVANCE64(xk
, sizeof (*xk
));
3432 struct xsockbuf_n
*xsbrcv
= (struct xsockbuf_n
*)
3433 ADVANCE64(xso
, sizeof (*xso
));
3434 struct xsockbuf_n
*xsbsnd
= (struct xsockbuf_n
*)
3435 ADVANCE64(xsbrcv
, sizeof (*xsbrcv
));
3436 struct xsockstat_n
*xsostats
= (struct xsockstat_n
*)
3437 ADVANCE64(xsbsnd
, sizeof (*xsbsnd
));
3439 bzero(buf
, item_size
);
3441 lck_mtx_lock(&ev_pcb
->evp_mtx
);
3443 xk
->kep_len
= sizeof(struct xkevtpcb
);
3444 xk
->kep_kind
= XSO_EVT
;
3445 xk
->kep_evtpcb
= (uint64_t)VM_KERNEL_ADDRPERM(ev_pcb
);
3446 xk
->kep_vendor_code_filter
= ev_pcb
->evp_vendor_code_filter
;
3447 xk
->kep_class_filter
= ev_pcb
->evp_class_filter
;
3448 xk
->kep_subclass_filter
= ev_pcb
->evp_subclass_filter
;
3450 sotoxsocket_n(ev_pcb
->evp_socket
, xso
);
3451 sbtoxsockbuf_n(ev_pcb
->evp_socket
?
3452 &ev_pcb
->evp_socket
->so_rcv
: NULL
, xsbrcv
);
3453 sbtoxsockbuf_n(ev_pcb
->evp_socket
?
3454 &ev_pcb
->evp_socket
->so_snd
: NULL
, xsbsnd
);
3455 sbtoxsockstat_n(ev_pcb
->evp_socket
, xsostats
);
3457 lck_mtx_unlock(&ev_pcb
->evp_mtx
);
3459 error
= SYSCTL_OUT(req
, buf
, item_size
);
3464 * Give the user an updated idea of our state.
3465 * If the generation differs from what we told
3466 * her before, she knows that something happened
3467 * while we were processing this request, and it
3468 * might be necessary to retry.
3470 bzero(&xsg
, sizeof (xsg
));
3471 xsg
.xg_len
= sizeof (xsg
);
3473 xsg
.xg_gen
= kevtstat
.kes_gencnt
;
3474 xsg
.xg_sogen
= so_gencnt
;
3475 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
3482 lck_rw_done(kev_rwlock
);
3487 #endif /* SOCKETS */
3491 fill_kqueueinfo(struct kqueue
*kq
, struct kqueue_info
* kinfo
)
3493 struct vinfo_stat
* st
;
3495 st
= &kinfo
->kq_stat
;
3497 st
->vst_size
= kq
->kq_count
;
3498 if (kq
->kq_state
& KQ_KEV64
)
3499 st
->vst_blksize
= sizeof(struct kevent64_s
);
3501 st
->vst_blksize
= sizeof(struct kevent
);
3502 st
->vst_mode
= S_IFIFO
;
3503 if (kq
->kq_state
& KQ_SEL
)
3504 kinfo
->kq_state
|= PROC_KQUEUE_SELECT
;
3505 if (kq
->kq_state
& KQ_SLEEP
)
3506 kinfo
->kq_state
|= PROC_KQUEUE_SLEEP
;
3513 knote_markstayqueued(struct knote
*kn
)
3516 kn
->kn_status
|= KN_STAYQUEUED
;
3518 kqunlock(kn
->kn_kq
);