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
)
474 pid_t selfpid
= (pid_t
)0;
476 assert(PID_MAX
< NOTE_PDATAMASK
);
478 if ((kn
->kn_sfflags
& (NOTE_TRACK
| NOTE_TRACKERR
| NOTE_CHILD
)) != 0)
481 p
= proc_find(kn
->kn_id
);
486 if ((kn
->kn_sfflags
& NOTE_EXIT
) != 0) {
487 selfpid
= proc_selfpid();
488 /* check for validity of NOTE_EXISTATUS */
489 if (((kn
->kn_sfflags
& NOTE_EXITSTATUS
) != 0) &&
490 ((p
->p_ppid
!= selfpid
) && (((p
->p_lflag
& P_LTRACED
) == 0) || (p
->p_oppid
!= selfpid
)))) {
498 kn
->kn_flags
|= EV_CLEAR
; /* automatically set */
499 kn
->kn_ptr
.p_proc
= p
; /* store the proc handle */
501 KNOTE_ATTACH(&p
->p_klist
, kn
);
511 * The knote may be attached to a different process, which may exit,
512 * leaving nothing for the knote to be attached to. In that case,
513 * the pointer to the process will have already been nulled out.
516 filt_procdetach(struct knote
*kn
)
522 p
= kn
->kn_ptr
.p_proc
;
523 if (p
!= PROC_NULL
) {
524 kn
->kn_ptr
.p_proc
= PROC_NULL
;
525 KNOTE_DETACH(&p
->p_klist
, kn
);
532 filt_proc(struct knote
*kn
, long hint
)
534 /* hint is 0 when called from above */
538 /* ALWAYS CALLED WITH proc_klist_lock when (hint != 0) */
541 * mask off extra data
543 event
= (u_int
)hint
& NOTE_PCTRLMASK
;
546 * if the user is interested in this event, record it.
548 if (kn
->kn_sfflags
& event
)
549 kn
->kn_fflags
|= event
;
551 if (event
== NOTE_REAP
|| (event
== NOTE_EXIT
&& !(kn
->kn_sfflags
& NOTE_REAP
))) {
552 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
554 if ((event
== NOTE_EXIT
) && ((kn
->kn_sfflags
& NOTE_EXITSTATUS
) != 0)) {
555 kn
->kn_fflags
|= NOTE_EXITSTATUS
;
556 kn
->kn_data
= (hint
& NOTE_PDATAMASK
);
558 if ((event
== NOTE_RESOURCEEND
) && ((kn
->kn_sfflags
& NOTE_RESOURCEEND
) != 0)) {
559 kn
->kn_fflags
|= NOTE_RESOURCEEND
;
560 kn
->kn_data
= (hint
& NOTE_PDATAMASK
);
563 /* If the event is one of the APPSTATE events,remove the rest */
564 if (((event
& NOTE_APPALLSTATES
) != 0) && ((kn
->kn_sfflags
& NOTE_APPALLSTATES
) != 0)) {
565 /* only one state at a time */
566 kn
->kn_fflags
&= ~NOTE_APPALLSTATES
;
567 kn
->kn_fflags
|= event
;
569 #endif /* CONFIG_EMBEDDED */
572 /* atomic check, no locking need when called from above */
573 return (kn
->kn_fflags
!= 0);
576 #if VM_PRESSURE_EVENTS
578 * Virtual memory kevents
580 * author: Matt Jacobson [matthew_jacobson@apple.com]
584 filt_vmattach(struct knote
*kn
)
587 * The note will be cleared once the information has been flushed to the client.
588 * If there is still pressure, we will be re-alerted.
590 kn
->kn_flags
|= EV_CLEAR
;
592 return vm_knote_register(kn
);
596 filt_vmdetach(struct knote
*kn
)
598 vm_knote_unregister(kn
);
602 filt_vm(struct knote
*kn
, long hint
)
604 /* hint == 0 means this is just an alive? check (always true) */
606 const pid_t pid
= (pid_t
)hint
;
607 if ((kn
->kn_sfflags
& NOTE_VM_PRESSURE
) && (kn
->kn_kq
->kq_p
->p_pid
== pid
)) {
608 kn
->kn_fflags
|= NOTE_VM_PRESSURE
;
612 return (kn
->kn_fflags
!= 0);
614 #endif /* VM_PRESSURE_EVENTS */
617 * filt_timervalidate - process data from user
619 * Converts to either interval or deadline format.
621 * The saved-data field in the knote contains the
622 * time value. The saved filter-flags indicates
623 * the unit of measurement.
625 * After validation, either the saved-data field
626 * contains the interval in absolute time, or ext[0]
627 * contains the expected deadline. If that deadline
628 * is in the past, ext[0] is 0.
630 * Returns EINVAL for unrecognized units of time.
632 * Timer filter lock is held.
636 filt_timervalidate(struct knote
*kn
)
641 switch (kn
->kn_sfflags
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) {
643 multiplier
= NSEC_PER_SEC
;
646 multiplier
= NSEC_PER_USEC
;
651 case 0: /* milliseconds (default) */
652 multiplier
= NSEC_PER_SEC
/ 1000;
658 nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata
* multiplier
, &raw
);
663 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
) {
665 clock_nsec_t nanoseconds
;
668 clock_get_calendar_nanotime(&seconds
, &nanoseconds
);
669 nanoseconds_to_absolutetime((uint64_t)seconds
* NSEC_PER_SEC
+
673 /* time has already passed */
677 clock_absolutetime_interval_to_deadline(raw
,
688 * filt_timerupdate - compute the next deadline
690 * Repeating timers store their interval in kn_sdata. Absolute
691 * timers have already calculated the deadline, stored in ext[0].
693 * On return, the next deadline (or zero if no deadline is needed)
694 * is stored in kn_ext[0].
696 * Timer filter lock is held.
699 filt_timerupdate(struct knote
*kn
)
701 /* if there's no interval, deadline is just in kn_ext[0] */
702 if (kn
->kn_sdata
== 0)
705 /* if timer hasn't fired before, fire in interval nsecs */
706 if (kn
->kn_ext
[0] == 0) {
707 clock_absolutetime_interval_to_deadline(kn
->kn_sdata
,
711 * If timer has fired before, schedule the next pop
712 * relative to the last intended deadline.
714 * We could check for whether the deadline has expired,
715 * but the thread call layer can handle that.
717 kn
->kn_ext
[0] += kn
->kn_sdata
;
722 * filt_timerexpire - the timer callout routine
724 * Just propagate the timer event into the knote
725 * filter routine (by going through the knote
726 * synchronization point). Pass a hint to
727 * indicate this is a real event, not just a
731 filt_timerexpire(void *knx
, __unused
void *spare
)
733 struct klist timer_list
;
734 struct knote
*kn
= knx
;
738 kn
->kn_hookid
&= ~TIMER_RUNNING
;
740 /* no "object" for timers, so fake a list */
741 SLIST_INIT(&timer_list
);
742 SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);
743 KNOTE(&timer_list
, 1);
745 /* if someone is waiting for timer to pop */
746 if (kn
->kn_hookid
& TIMER_CANCELWAIT
) {
747 struct kqueue
*kq
= kn
->kn_kq
;
748 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_hook
,
756 * Cancel a running timer (or wait for the pop).
757 * Timer filter lock is held.
760 filt_timercancel(struct knote
*kn
)
762 struct kqueue
*kq
= kn
->kn_kq
;
763 thread_call_t callout
= kn
->kn_hook
;
766 if (kn
->kn_hookid
& TIMER_RUNNING
) {
767 /* cancel the callout if we can */
768 cancelled
= thread_call_cancel(callout
);
770 kn
->kn_hookid
&= ~TIMER_RUNNING
;
772 /* we have to wait for the expire routine. */
773 kn
->kn_hookid
|= TIMER_CANCELWAIT
;
774 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
,
775 &kn
->kn_hook
, THREAD_UNINT
, 0);
777 thread_block(THREAD_CONTINUE_NULL
);
779 assert((kn
->kn_hookid
& TIMER_RUNNING
) == 0);
785 * Allocate a thread call for the knote's lifetime, and kick off the timer.
788 filt_timerattach(struct knote
*kn
)
790 thread_call_t callout
;
793 callout
= thread_call_allocate(filt_timerexpire
, kn
);
798 error
= filt_timervalidate(kn
);
804 kn
->kn_hook
= (void*)callout
;
807 /* absolute=EV_ONESHOT */
808 if (kn
->kn_sfflags
& NOTE_ABSOLUTE
)
809 kn
->kn_flags
|= EV_ONESHOT
;
811 filt_timerupdate(kn
);
813 kn
->kn_flags
|= EV_CLEAR
;
814 thread_call_enter_delayed(callout
, kn
->kn_ext
[0]);
815 kn
->kn_hookid
|= TIMER_RUNNING
;
826 * Shut down the timer if it's running, and free the callout.
829 filt_timerdetach(struct knote
*kn
)
831 thread_call_t callout
;
835 callout
= (thread_call_t
)kn
->kn_hook
;
836 filt_timercancel(kn
);
840 thread_call_free(callout
);
846 filt_timer(struct knote
*kn
, long hint
)
851 /* real timer pop -- timer lock held by filt_timerexpire */
855 if (((kn
->kn_hookid
& TIMER_CANCELWAIT
) == 0) &&
856 ((kn
->kn_flags
& EV_ONESHOT
) == 0)) {
858 /* evaluate next time to fire */
859 filt_timerupdate(kn
);
862 /* keep the callout and re-arm */
863 thread_call_enter_delayed(kn
->kn_hook
,
865 kn
->kn_hookid
|= TIMER_RUNNING
;
875 result
= (kn
->kn_data
!= 0);
883 * filt_timertouch - update knote with new user input
885 * Cancel and restart the timer based on new user data. When
886 * the user picks up a knote, clear the count of how many timer
887 * pops have gone off (in kn_data).
890 filt_timertouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
897 /* cancel current call */
898 filt_timercancel(kn
);
900 /* recalculate deadline */
901 kn
->kn_sdata
= kev
->data
;
902 kn
->kn_sfflags
= kev
->fflags
;
904 error
= filt_timervalidate(kn
);
906 /* no way to report error, so mark it in the knote */
907 kn
->kn_flags
|= EV_ERROR
;
912 /* start timer if necessary */
913 filt_timerupdate(kn
);
915 thread_call_enter_delayed(kn
->kn_hook
, kn
->kn_ext
[0]);
916 kn
->kn_hookid
|= TIMER_RUNNING
;
918 /* pretend the timer has fired */
925 /* reset the timer pop count in kn_data */
926 *kev
= kn
->kn_kevent
;
929 if (kn
->kn_flags
& EV_CLEAR
)
933 panic("filt_timertouch() - invalid type (%ld)", type
);
943 lck_mtx_lock(&_filt_timerlock
);
947 filt_timerunlock(void)
949 lck_mtx_unlock(&_filt_timerlock
);
953 filt_userattach(struct knote
*kn
)
955 /* EVFILT_USER knotes are not attached to anything in the kernel */
957 if (kn
->kn_fflags
& NOTE_TRIGGER
) {
966 filt_userdetach(__unused
struct knote
*kn
)
968 /* EVFILT_USER knotes are not attached to anything in the kernel */
972 filt_user(struct knote
*kn
, __unused
long hint
)
974 return kn
->kn_hookid
;
978 filt_usertouch(struct knote
*kn
, struct kevent64_s
*kev
, long type
)
983 if (kev
->fflags
& NOTE_TRIGGER
) {
987 ffctrl
= kev
->fflags
& NOTE_FFCTRLMASK
;
988 kev
->fflags
&= NOTE_FFLAGSMASK
;
993 OSBitAndAtomic(kev
->fflags
, &kn
->kn_sfflags
);
996 OSBitOrAtomic(kev
->fflags
, &kn
->kn_sfflags
);
999 kn
->kn_sfflags
= kev
->fflags
;
1002 kn
->kn_sdata
= kev
->data
;
1005 *kev
= kn
->kn_kevent
;
1006 kev
->fflags
= (volatile UInt32
)kn
->kn_sfflags
;
1007 kev
->data
= kn
->kn_sdata
;
1008 if (kn
->kn_flags
& EV_CLEAR
) {
1015 panic("filt_usertouch() - invalid type (%ld)", type
);
1021 * JMM - placeholder for not-yet-implemented filters
1024 filt_badattach(__unused
struct knote
*kn
)
1031 kqueue_alloc(struct proc
*p
)
1033 struct filedesc
*fdp
= p
->p_fd
;
1036 MALLOC_ZONE(kq
, struct kqueue
*, sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
);
1038 wait_queue_set_t wqs
;
1040 wqs
= wait_queue_set_alloc(SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
);
1042 bzero(kq
, sizeof(struct kqueue
));
1043 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
);
1044 TAILQ_INIT(&kq
->kq_head
);
1048 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
1052 if (fdp
->fd_knlistsize
< 0) {
1054 if (fdp
->fd_knlistsize
< 0)
1055 fdp
->fd_knlistsize
= 0; /* this process has had a kq */
1064 * kqueue_dealloc - detach all knotes from a kqueue and free it
1066 * We walk each list looking for knotes referencing this
1067 * this kqueue. If we find one, we try to drop it. But
1068 * if we fail to get a drop reference, that will wait
1069 * until it is dropped. So, we can just restart again
1070 * safe in the assumption that the list will eventually
1071 * not contain any more references to this kqueue (either
1072 * we dropped them all, or someone else did).
1074 * Assumes no new events are being added to the kqueue.
1075 * Nothing locked on entry or exit.
1078 kqueue_dealloc(struct kqueue
*kq
)
1080 struct proc
*p
= kq
->kq_p
;
1081 struct filedesc
*fdp
= p
->p_fd
;
1086 for (i
= 0; i
< fdp
->fd_knlistsize
; i
++) {
1087 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
1088 while (kn
!= NULL
) {
1089 if (kq
== kn
->kn_kq
) {
1092 /* drop it ourselves or wait */
1093 if (kqlock2knotedrop(kq
, kn
)) {
1094 kn
->kn_fop
->f_detach(kn
);
1098 /* start over at beginning of list */
1099 kn
= SLIST_FIRST(&fdp
->fd_knlist
[i
]);
1102 kn
= SLIST_NEXT(kn
, kn_link
);
1105 if (fdp
->fd_knhashmask
!= 0) {
1106 for (i
= 0; i
< (int)fdp
->fd_knhashmask
+ 1; i
++) {
1107 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
1108 while (kn
!= NULL
) {
1109 if (kq
== kn
->kn_kq
) {
1112 /* drop it ourselves or wait */
1113 if (kqlock2knotedrop(kq
, kn
)) {
1114 kn
->kn_fop
->f_detach(kn
);
1118 /* start over at beginning of list */
1119 kn
= SLIST_FIRST(&fdp
->fd_knhash
[i
]);
1122 kn
= SLIST_NEXT(kn
, kn_link
);
1129 * before freeing the wait queue set for this kqueue,
1130 * make sure it is unlinked from all its containing (select) sets.
1132 wait_queue_unlink_all((wait_queue_t
)kq
->kq_wqs
);
1133 wait_queue_set_free(kq
->kq_wqs
);
1134 lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
);
1135 FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
);
1139 kqueue(struct proc
*p
, __unused
struct kqueue_args
*uap
, int32_t *retval
)
1142 struct fileproc
*fp
;
1145 error
= falloc(p
, &fp
, &fd
, vfs_context_current());
1150 kq
= kqueue_alloc(p
);
1156 fp
->f_flag
= FREAD
| FWRITE
;
1157 fp
->f_type
= DTYPE_KQUEUE
;
1158 fp
->f_ops
= &kqueueops
;
1159 fp
->f_data
= (caddr_t
)kq
;
1162 procfdtbl_releasefd(p
, fd
, NULL
);
1163 fp_drop(p
, fd
, fp
, 1);
1171 kevent_copyin(user_addr_t
*addrp
, struct kevent64_s
*kevp
, struct proc
*p
, int iskev64
)
1177 advance
= sizeof(struct kevent64_s
);
1178 error
= copyin(*addrp
, (caddr_t
)kevp
, advance
);
1179 } else if (IS_64BIT_PROCESS(p
)) {
1180 struct user64_kevent kev64
;
1181 bzero(kevp
, sizeof(struct kevent64_s
));
1183 advance
= sizeof(kev64
);
1184 error
= copyin(*addrp
, (caddr_t
)&kev64
, advance
);
1187 kevp
->ident
= kev64
.ident
;
1188 kevp
->filter
= kev64
.filter
;
1189 kevp
->flags
= kev64
.flags
;
1190 kevp
->fflags
= kev64
.fflags
;
1191 kevp
->data
= kev64
.data
;
1192 kevp
->udata
= kev64
.udata
;
1194 struct user32_kevent kev32
;
1195 bzero(kevp
, sizeof(struct kevent64_s
));
1197 advance
= sizeof(kev32
);
1198 error
= copyin(*addrp
, (caddr_t
)&kev32
, advance
);
1201 kevp
->ident
= (uintptr_t)kev32
.ident
;
1202 kevp
->filter
= kev32
.filter
;
1203 kevp
->flags
= kev32
.flags
;
1204 kevp
->fflags
= kev32
.fflags
;
1205 kevp
->data
= (intptr_t)kev32
.data
;
1206 kevp
->udata
= CAST_USER_ADDR_T(kev32
.udata
);
1214 kevent_copyout(struct kevent64_s
*kevp
, user_addr_t
*addrp
, struct proc
*p
, int iskev64
)
1220 advance
= sizeof(struct kevent64_s
);
1221 error
= copyout((caddr_t
)kevp
, *addrp
, advance
);
1222 } else if (IS_64BIT_PROCESS(p
)) {
1223 struct user64_kevent kev64
;
1226 * deal with the special case of a user-supplied
1227 * value of (uintptr_t)-1.
1229 kev64
.ident
= (kevp
->ident
== (uintptr_t)-1) ?
1230 (uint64_t)-1LL : (uint64_t)kevp
->ident
;
1232 kev64
.filter
= kevp
->filter
;
1233 kev64
.flags
= kevp
->flags
;
1234 kev64
.fflags
= kevp
->fflags
;
1235 kev64
.data
= (int64_t) kevp
->data
;
1236 kev64
.udata
= kevp
->udata
;
1237 advance
= sizeof(kev64
);
1238 error
= copyout((caddr_t
)&kev64
, *addrp
, advance
);
1240 struct user32_kevent kev32
;
1242 kev32
.ident
= (uint32_t)kevp
->ident
;
1243 kev32
.filter
= kevp
->filter
;
1244 kev32
.flags
= kevp
->flags
;
1245 kev32
.fflags
= kevp
->fflags
;
1246 kev32
.data
= (int32_t)kevp
->data
;
1247 kev32
.udata
= kevp
->udata
;
1248 advance
= sizeof(kev32
);
1249 error
= copyout((caddr_t
)&kev32
, *addrp
, advance
);
1257 * kevent_continue - continue a kevent syscall after blocking
1259 * assume we inherit a use count on the kq fileglob.
1263 kevent_continue(__unused
struct kqueue
*kq
, void *data
, int error
)
1265 struct _kevent
*cont_args
;
1266 struct fileproc
*fp
;
1270 struct proc
*p
= current_proc();
1272 cont_args
= (struct _kevent
*)data
;
1273 noutputs
= cont_args
->eventout
;
1274 retval
= cont_args
->retval
;
1278 fp_drop(p
, fd
, fp
, 0);
1280 /* don't restart after signals... */
1281 if (error
== ERESTART
)
1283 else if (error
== EWOULDBLOCK
)
1287 unix_syscall_return(error
);
1291 * kevent - [syscall] register and wait for kernel events
1295 kevent(struct proc
*p
, struct kevent_args
*uap
, int32_t *retval
)
1297 return kevent_internal(p
,
1305 0, /* no flags from old kevent() call */
1310 kevent64(struct proc
*p
, struct kevent64_args
*uap
, int32_t *retval
)
1312 return kevent_internal(p
,
1325 kevent_internal(struct proc
*p
, int iskev64
, user_addr_t changelist
,
1326 int nchanges
, user_addr_t ueventlist
, int nevents
, int fd
,
1327 user_addr_t utimeout
, __unused
unsigned int flags
,
1330 struct _kevent
*cont_args
;
1333 struct fileproc
*fp
;
1334 struct kevent64_s kev
;
1335 int error
, noutputs
;
1338 /* convert timeout to absolute - if we have one */
1339 if (utimeout
!= USER_ADDR_NULL
) {
1341 if (IS_64BIT_PROCESS(p
)) {
1342 struct user64_timespec ts
;
1343 error
= copyin(utimeout
, &ts
, sizeof(ts
));
1344 if ((ts
.tv_sec
& 0xFFFFFFFF00000000ull
) != 0)
1347 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1349 struct user32_timespec ts
;
1350 error
= copyin(utimeout
, &ts
, sizeof(ts
));
1351 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
);
1355 if (itimerfix(&rtv
))
1357 getmicrouptime(&atv
);
1358 timevaladd(&atv
, &rtv
);
1364 /* get a usecount for the kq itself */
1365 if ((error
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0)
1368 /* each kq should only be used for events of one type */
1370 if (kq
->kq_state
& (KQ_KEV32
| KQ_KEV64
)) {
1371 if (((iskev64
&& (kq
->kq_state
& KQ_KEV32
)) ||
1372 (!iskev64
&& (kq
->kq_state
& KQ_KEV64
)))) {
1378 kq
->kq_state
|= (iskev64
? KQ_KEV64
: KQ_KEV32
);
1382 /* register all the change requests the user provided... */
1384 while (nchanges
> 0 && error
== 0) {
1385 error
= kevent_copyin(&changelist
, &kev
, p
, iskev64
);
1389 kev
.flags
&= ~EV_SYSFLAGS
;
1390 error
= kevent_register(kq
, &kev
, p
);
1391 if ((error
|| (kev
.flags
& EV_RECEIPT
)) && nevents
> 0) {
1392 kev
.flags
= EV_ERROR
;
1394 error
= kevent_copyout(&kev
, &ueventlist
, p
, iskev64
);
1403 /* store the continuation/completion data in the uthread */
1404 ut
= (uthread_t
)get_bsdthread_info(current_thread());
1405 cont_args
= &ut
->uu_kevent
.ss_kevent
;
1408 cont_args
->retval
= retval
;
1409 cont_args
->eventlist
= ueventlist
;
1410 cont_args
->eventcount
= nevents
;
1411 cont_args
->eventout
= noutputs
;
1412 cont_args
->eventsize
= iskev64
;
1414 if (nevents
> 0 && noutputs
== 0 && error
== 0)
1415 error
= kqueue_scan(kq
, kevent_callback
,
1416 kevent_continue
, cont_args
,
1418 kevent_continue(kq
, cont_args
, error
);
1421 fp_drop(p
, fd
, fp
, 0);
1427 * kevent_callback - callback for each individual event
1429 * called with nothing locked
1430 * caller holds a reference on the kqueue
1434 kevent_callback(__unused
struct kqueue
*kq
, struct kevent64_s
*kevp
,
1437 struct _kevent
*cont_args
;
1441 cont_args
= (struct _kevent
*)data
;
1442 assert(cont_args
->eventout
< cont_args
->eventcount
);
1444 iskev64
= cont_args
->eventsize
;
1447 * Copy out the appropriate amount of event data for this user.
1449 error
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc(), iskev64
);
1452 * If there isn't space for additional events, return
1453 * a harmless error to stop the processing here
1455 if (error
== 0 && ++cont_args
->eventout
== cont_args
->eventcount
)
1456 error
= EWOULDBLOCK
;
1461 * kevent_description - format a description of a kevent for diagnostic output
1463 * called with a 128-byte string buffer
1467 kevent_description(struct kevent64_s
*kevp
, char *s
, size_t n
)
1471 "{.ident=%#llx, .filter=%d, .flags=%#x, .fflags=%#x, .data=%#llx, .udata=%#llx, .ext[0]=%#llx, .ext[1]=%#llx}",
1484 * kevent_register - add a new event to a kqueue
1486 * Creates a mapping between the event source and
1487 * the kqueue via a knote data structure.
1489 * Because many/most the event sources are file
1490 * descriptor related, the knote is linked off
1491 * the filedescriptor table for quick access.
1493 * called with nothing locked
1494 * caller holds a reference on the kqueue
1498 kevent_register(struct kqueue
*kq
, struct kevent64_s
*kev
, __unused
struct proc
*ctxp
)
1500 struct proc
*p
= kq
->kq_p
;
1501 struct filedesc
*fdp
= p
->p_fd
;
1502 struct filterops
*fops
;
1503 struct fileproc
*fp
= NULL
;
1504 struct knote
*kn
= NULL
;
1507 if (kev
->filter
< 0) {
1508 if (kev
->filter
+ EVFILT_SYSCOUNT
< 0)
1510 fops
= sysfilt_ops
[~kev
->filter
]; /* to 0-base index */
1514 * filter attach routine is responsible for insuring that
1515 * the identifier can be attached to it.
1517 printf("unknown filter: %d\n", kev
->filter
);
1522 /* this iocount needs to be dropped if it is not registered */
1524 if (fops
->f_isfd
&& (error
= fp_lookup(p
, kev
->ident
, &fp
, 1)) != 0) {
1530 /* fd-based knotes are linked off the fd table */
1531 if (kev
->ident
< (u_int
)fdp
->fd_knlistsize
) {
1532 SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
)
1533 if (kq
== kn
->kn_kq
&&
1534 kev
->filter
== kn
->kn_filter
)
1538 /* hash non-fd knotes here too */
1539 if (fdp
->fd_knhashmask
!= 0) {
1542 list
= &fdp
->fd_knhash
[
1543 KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)];
1544 SLIST_FOREACH(kn
, list
, kn_link
)
1545 if (kev
->ident
== kn
->kn_id
&&
1547 kev
->filter
== kn
->kn_filter
)
1553 * kn now contains the matching knote, or NULL if no match
1556 if ((kev
->flags
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) {
1565 kn
->kn_tq
= &kq
->kq_head
;
1567 kn
->kn_sfflags
= kev
->fflags
;
1568 kn
->kn_sdata
= kev
->data
;
1571 kn
->kn_kevent
= *kev
;
1572 kn
->kn_inuse
= 1; /* for f_attach() */
1573 kn
->kn_status
= KN_ATTACHING
;
1575 /* before anyone can find it */
1576 if (kev
->flags
& EV_DISABLE
)
1577 kn
->kn_status
|= KN_DISABLED
;
1579 error
= knote_fdpattach(kn
, fdp
, p
);
1588 * apply reference count to knote structure, and
1589 * do not release it at the end of this routine.
1593 error
= fops
->f_attach(kn
);
1599 * Failed to attach correctly, so drop.
1600 * All other possible users/droppers
1601 * have deferred to us.
1603 kn
->kn_status
|= KN_DROPPING
;
1607 } else if (kn
->kn_status
& KN_DROPPING
) {
1609 * Attach succeeded, but someone else
1610 * deferred their drop - now we have
1611 * to do it for them (after detaching).
1614 kn
->kn_fop
->f_detach(kn
);
1618 kn
->kn_status
&= ~KN_ATTACHING
;
1626 /* existing knote - get kqueue lock */
1630 if (kev
->flags
& EV_DELETE
) {
1632 kn
->kn_status
|= KN_DISABLED
;
1633 if (kqlock2knotedrop(kq
, kn
)) {
1634 kn
->kn_fop
->f_detach(kn
);
1640 /* update status flags for existing knote */
1641 if (kev
->flags
& EV_DISABLE
) {
1643 kn
->kn_status
|= KN_DISABLED
;
1644 } else if (kev
->flags
& EV_ENABLE
) {
1645 kn
->kn_status
&= ~KN_DISABLED
;
1646 if (kn
->kn_status
& KN_ACTIVE
)
1651 * The user may change some filter values after the
1652 * initial EV_ADD, but doing so will not reset any
1653 * filter which have already been triggered.
1655 kn
->kn_kevent
.udata
= kev
->udata
;
1656 if (fops
->f_isfd
|| fops
->f_touch
== NULL
) {
1657 kn
->kn_sfflags
= kev
->fflags
;
1658 kn
->kn_sdata
= kev
->data
;
1662 * If somebody is in the middle of dropping this
1663 * knote - go find/insert a new one. But we have
1664 * wait for this one to go away first. Attaches
1665 * running in parallel may also drop/modify the
1666 * knote. Wait for those to complete as well and
1667 * then start over if we encounter one.
1669 if (!kqlock2knoteusewait(kq
, kn
)) {
1670 /* kqueue, proc_fdlock both unlocked */
1675 * Call touch routine to notify filter of changes
1678 if (!fops
->f_isfd
&& fops
->f_touch
!= NULL
)
1679 fops
->f_touch(kn
, kev
, EVENT_REGISTER
);
1681 /* still have use ref on knote */
1684 * If the knote is not marked to always stay enqueued,
1685 * invoke the filter routine to see if it should be
1688 if ((kn
->kn_status
& KN_STAYQUEUED
) == 0 && kn
->kn_fop
->f_event(kn
, 0)) {
1689 if (knoteuse2kqlock(kq
, kn
))
1690 knote_activate(kn
, 1);
1698 fp_drop(p
, kev
->ident
, fp
, 0);
1704 * knote_process - process a triggered event
1706 * Validate that it is really still a triggered event
1707 * by calling the filter routines (if necessary). Hold
1708 * a use reference on the knote to avoid it being detached.
1709 * If it is still considered triggered, invoke the callback
1710 * routine provided and move it to the provided inprocess
1713 * caller holds a reference on the kqueue.
1714 * kqueue locked on entry and exit - but may be dropped
1717 knote_process(struct knote
*kn
,
1718 kevent_callback_t callback
,
1720 struct kqtailq
*inprocessp
,
1723 struct kqueue
*kq
= kn
->kn_kq
;
1724 struct kevent64_s kev
;
1730 * Determine the kevent state we want to return.
1732 * Some event states need to be revalidated before returning
1733 * them, others we take the snapshot at the time the event
1736 * Events with non-NULL f_touch operations must be touched.
1737 * Triggered events must fill in kev for the callback.
1739 * Convert our lock to a use-count and call the event's
1740 * filter routine(s) to update.
1742 if ((kn
->kn_status
& KN_DISABLED
) != 0) {
1749 revalidate
= ((kn
->kn_status
& KN_STAYQUEUED
) != 0 ||
1750 (kn
->kn_flags
& EV_ONESHOT
) == 0);
1751 touch
= (!kn
->kn_fop
->f_isfd
&& kn
->kn_fop
->f_touch
!= NULL
);
1753 if (revalidate
|| touch
) {
1755 knote_deactivate(kn
);
1757 /* call the filter/touch routines with just a ref */
1758 if (kqlock2knoteuse(kq
, kn
)) {
1760 /* if we have to revalidate, call the filter */
1762 result
= kn
->kn_fop
->f_event(kn
, 0);
1765 /* capture the kevent data - using touch if specified */
1766 if (result
&& touch
) {
1767 kn
->kn_fop
->f_touch(kn
, &kev
, EVENT_PROCESS
);
1770 /* convert back to a kqlock - bail if the knote went away */
1771 if (!knoteuse2kqlock(kq
, kn
)) {
1773 } else if (result
) {
1774 /* if revalidated as alive, make sure it's active */
1775 if (!(kn
->kn_status
& KN_ACTIVE
)) {
1776 knote_activate(kn
, 0);
1779 /* capture all events that occurred during filter */
1781 kev
= kn
->kn_kevent
;
1784 } else if ((kn
->kn_status
& KN_STAYQUEUED
) == 0) {
1785 /* was already dequeued, so just bail on this one */
1792 kev
= kn
->kn_kevent
;
1796 /* move knote onto inprocess queue */
1797 assert(kn
->kn_tq
== &kq
->kq_head
);
1798 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
1799 kn
->kn_tq
= inprocessp
;
1800 TAILQ_INSERT_TAIL(inprocessp
, kn
, kn_tqe
);
1803 * Determine how to dispatch the knote for future event handling.
1804 * not-fired: just return (do not callout).
1805 * One-shot: deactivate it.
1806 * Clear: deactivate and clear the state.
1807 * Dispatch: don't clear state, just deactivate it and mark it disabled.
1808 * All others: just leave where they are.
1813 } else if ((kn
->kn_flags
& EV_ONESHOT
) != 0) {
1814 knote_deactivate(kn
);
1815 if (kqlock2knotedrop(kq
, kn
)) {
1816 kn
->kn_fop
->f_detach(kn
);
1819 } else if ((kn
->kn_flags
& (EV_CLEAR
| EV_DISPATCH
)) != 0) {
1820 if ((kn
->kn_flags
& EV_DISPATCH
) != 0) {
1821 /* deactivate and disable all dispatch knotes */
1822 knote_deactivate(kn
);
1823 kn
->kn_status
|= KN_DISABLED
;
1824 } else if (!touch
|| kn
->kn_fflags
== 0) {
1825 /* only deactivate if nothing since the touch */
1826 knote_deactivate(kn
);
1828 if (!touch
&& (kn
->kn_flags
& EV_CLEAR
) != 0) {
1829 /* manually clear non-touch knotes */
1836 * leave on inprocess queue. We'll
1837 * move all the remaining ones back
1838 * the kq queue and wakeup any
1839 * waiters when we are done.
1844 /* callback to handle each event as we find it */
1845 error
= (callback
)(kq
, &kev
, data
);
1852 * Return 0 to indicate that processing should proceed,
1853 * -1 if there is nothing to process.
1855 * Called with kqueue locked and returns the same way,
1856 * but may drop lock temporarily.
1859 kqueue_begin_processing(struct kqueue
*kq
)
1862 if (kq
->kq_count
== 0) {
1866 /* if someone else is processing the queue, wait */
1867 if (kq
->kq_nprocess
!= 0) {
1868 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, &kq
->kq_nprocess
, THREAD_UNINT
, 0);
1869 kq
->kq_state
|= KQ_PROCWAIT
;
1871 thread_block(THREAD_CONTINUE_NULL
);
1874 kq
->kq_nprocess
= 1;
1881 * Called with kqueue lock held.
1884 kqueue_end_processing(struct kqueue
*kq
)
1886 kq
->kq_nprocess
= 0;
1887 if (kq
->kq_state
& KQ_PROCWAIT
) {
1888 kq
->kq_state
&= ~KQ_PROCWAIT
;
1889 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kq
->kq_nprocess
, THREAD_AWAKENED
);
1894 * kqueue_process - process the triggered events in a kqueue
1896 * Walk the queued knotes and validate that they are
1897 * really still triggered events by calling the filter
1898 * routines (if necessary). Hold a use reference on
1899 * the knote to avoid it being detached. For each event
1900 * that is still considered triggered, invoke the
1901 * callback routine provided.
1903 * caller holds a reference on the kqueue.
1904 * kqueue locked on entry and exit - but may be dropped
1905 * kqueue list locked (held for duration of call)
1909 kqueue_process(struct kqueue
*kq
,
1910 kevent_callback_t callback
,
1915 struct kqtailq inprocess
;
1920 TAILQ_INIT(&inprocess
);
1922 if (kqueue_begin_processing(kq
) == -1) {
1924 /* Nothing to process */
1929 * Clear any pre-posted status from previous runs, so we only
1930 * detect events that occur during this run.
1932 wait_queue_sub_clearrefs(kq
->kq_wqs
);
1935 * loop through the enqueued knotes, processing each one and
1936 * revalidating those that need it. As they are processed,
1937 * they get moved to the inprocess queue (so the loop can end).
1942 while (error
== 0 &&
1943 (kn
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
1944 error
= knote_process(kn
, callback
, data
, &inprocess
, p
);
1945 if (error
== EJUSTRETURN
)
1952 * With the kqueue still locked, move any knotes
1953 * remaining on the inprocess queue back to the
1954 * kq's queue and wake up any waiters.
1956 while ((kn
= TAILQ_FIRST(&inprocess
)) != NULL
) {
1957 assert(kn
->kn_tq
== &inprocess
);
1958 TAILQ_REMOVE(&inprocess
, kn
, kn_tqe
);
1959 kn
->kn_tq
= &kq
->kq_head
;
1960 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
1963 kqueue_end_processing(kq
);
1971 kqueue_scan_continue(void *data
, wait_result_t wait_result
)
1973 thread_t self
= current_thread();
1974 uthread_t ut
= (uthread_t
)get_bsdthread_info(self
);
1975 struct _kqueue_scan
* cont_args
= &ut
->uu_kevent
.ss_kqueue_scan
;
1976 struct kqueue
*kq
= (struct kqueue
*)data
;
1980 /* convert the (previous) wait_result to a proper error */
1981 switch (wait_result
) {
1982 case THREAD_AWAKENED
:
1984 error
= kqueue_process(kq
, cont_args
->call
, cont_args
, &count
, current_proc());
1985 if (error
== 0 && count
== 0) {
1986 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, KQ_EVENT
,
1987 THREAD_ABORTSAFE
, cont_args
->deadline
);
1988 kq
->kq_state
|= KQ_SLEEP
;
1990 thread_block_parameter(kqueue_scan_continue
, kq
);
1995 case THREAD_TIMED_OUT
:
1996 error
= EWOULDBLOCK
;
1998 case THREAD_INTERRUPTED
:
2002 panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result
);
2006 /* call the continuation with the results */
2007 assert(cont_args
->cont
!= NULL
);
2008 (cont_args
->cont
)(kq
, cont_args
->data
, error
);
2013 * kqueue_scan - scan and wait for events in a kqueue
2015 * Process the triggered events in a kqueue.
2017 * If there are no events triggered arrange to
2018 * wait for them. If the caller provided a
2019 * continuation routine, then kevent_scan will
2022 * The callback routine must be valid.
2023 * The caller must hold a use-count reference on the kq.
2027 kqueue_scan(struct kqueue
*kq
,
2028 kevent_callback_t callback
,
2029 kqueue_continue_t continuation
,
2031 struct timeval
*atvp
,
2034 thread_continue_t cont
= THREAD_CONTINUE_NULL
;
2039 assert(callback
!= NULL
);
2043 wait_result_t wait_result
;
2047 * Make a pass through the kq to find events already
2051 error
= kqueue_process(kq
, callback
, data
, &count
, p
);
2053 break; /* lock still held */
2055 /* looks like we have to consider blocking */
2058 /* convert the timeout to a deadline once */
2059 if (atvp
->tv_sec
|| atvp
->tv_usec
) {
2062 clock_get_uptime(&now
);
2063 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec
* NSEC_PER_SEC
+
2064 atvp
->tv_usec
* NSEC_PER_USEC
,
2066 if (now
>= deadline
) {
2067 /* non-blocking call */
2068 error
= EWOULDBLOCK
;
2069 break; /* lock still held */
2072 clock_absolutetime_interval_to_deadline(deadline
, &deadline
);
2074 deadline
= 0; /* block forever */
2078 uthread_t ut
= (uthread_t
)get_bsdthread_info(current_thread());
2079 struct _kqueue_scan
*cont_args
= &ut
->uu_kevent
.ss_kqueue_scan
;
2081 cont_args
->call
= callback
;
2082 cont_args
->cont
= continuation
;
2083 cont_args
->deadline
= deadline
;
2084 cont_args
->data
= data
;
2085 cont
= kqueue_scan_continue
;
2089 /* go ahead and wait */
2090 wait_queue_assert_wait((wait_queue_t
)kq
->kq_wqs
, KQ_EVENT
, THREAD_ABORTSAFE
, deadline
);
2091 kq
->kq_state
|= KQ_SLEEP
;
2093 wait_result
= thread_block_parameter(cont
, kq
);
2094 /* NOTREACHED if (continuation != NULL) */
2096 switch (wait_result
) {
2097 case THREAD_AWAKENED
:
2099 case THREAD_TIMED_OUT
:
2101 case THREAD_INTERRUPTED
:
2104 panic("kevent_scan - bad wait_result (%d)",
2116 * This could be expanded to call kqueue_scan, if desired.
2120 kqueue_read(__unused
struct fileproc
*fp
,
2121 __unused
struct uio
*uio
,
2123 __unused vfs_context_t ctx
)
2130 kqueue_write(__unused
struct fileproc
*fp
,
2131 __unused
struct uio
*uio
,
2133 __unused vfs_context_t ctx
)
2140 kqueue_ioctl(__unused
struct fileproc
*fp
,
2141 __unused u_long com
,
2142 __unused caddr_t data
,
2143 __unused vfs_context_t ctx
)
2150 kqueue_select(struct fileproc
*fp
, int which
, void *wql
, __unused vfs_context_t ctx
)
2152 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
2154 struct kqtailq inprocessq
;
2160 TAILQ_INIT(&inprocessq
);
2164 * If this is the first pass, link the wait queue associated with the
2165 * the kqueue onto the wait queue set for the select(). Normally we
2166 * use selrecord() for this, but it uses the wait queue within the
2167 * selinfo structure and we need to use the main one for the kqueue to
2168 * catch events from KN_STAYQUEUED sources. So we do the linkage manually.
2169 * (The select() call will unlink them when it ends).
2172 thread_t cur_act
= current_thread();
2173 struct uthread
* ut
= get_bsdthread_info(cur_act
);
2175 kq
->kq_state
|= KQ_SEL
;
2176 wait_queue_link_noalloc((wait_queue_t
)kq
->kq_wqs
, ut
->uu_wqset
,
2177 (wait_queue_link_t
)wql
);
2180 if (kqueue_begin_processing(kq
) == -1) {
2185 if (kq
->kq_count
!= 0) {
2187 * there is something queued - but it might be a
2188 * KN_STAYQUEUED knote, which may or may not have
2189 * any events pending. So, we have to walk the
2190 * list of knotes to see, and peek at the stay-
2191 * queued ones to be really sure.
2193 while ((kn
= (struct knote
*)TAILQ_FIRST(&kq
->kq_head
)) != NULL
) {
2194 if ((kn
->kn_status
& KN_STAYQUEUED
) == 0) {
2199 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
);
2200 TAILQ_INSERT_TAIL(&inprocessq
, kn
, kn_tqe
);
2202 if (kqlock2knoteuse(kq
, kn
)) {
2205 peek
= kn
->kn_fop
->f_peek(kn
);
2206 if (knoteuse2kqlock(kq
, kn
)) {
2219 /* Return knotes to active queue */
2220 while ((kn
= TAILQ_FIRST(&inprocessq
)) != NULL
) {
2221 TAILQ_REMOVE(&inprocessq
, kn
, kn_tqe
);
2222 kn
->kn_tq
= &kq
->kq_head
;
2223 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
);
2226 kqueue_end_processing(kq
);
2236 kqueue_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
2238 struct kqueue
*kq
= (struct kqueue
*)fg
->fg_data
;
2247 * The callers has taken a use-count reference on this kqueue and will donate it
2248 * to the kqueue we are being added to. This keeps the kqueue from closing until
2249 * that relationship is torn down.
2252 kqueue_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused vfs_context_t ctx
)
2254 struct kqueue
*kq
= (struct kqueue
*)kn
->kn_fp
->f_data
;
2255 struct kqueue
*parentkq
= kn
->kn_kq
;
2257 if (parentkq
== kq
||
2258 kn
->kn_filter
!= EVFILT_READ
)
2262 * We have to avoid creating a cycle when nesting kqueues
2263 * inside another. Rather than trying to walk the whole
2264 * potential DAG of nested kqueues, we just use a simple
2265 * ceiling protocol. When a kqueue is inserted into another,
2266 * we check that the (future) parent is not already nested
2267 * into another kqueue at a lower level than the potenial
2268 * child (because it could indicate a cycle). If that test
2269 * passes, we just mark the nesting levels accordingly.
2273 if (parentkq
->kq_level
> 0 &&
2274 parentkq
->kq_level
< kq
->kq_level
)
2279 /* set parent level appropriately */
2280 if (parentkq
->kq_level
== 0)
2281 parentkq
->kq_level
= 2;
2282 if (parentkq
->kq_level
< kq
->kq_level
+ 1)
2283 parentkq
->kq_level
= kq
->kq_level
+ 1;
2286 kn
->kn_fop
= &kqread_filtops
;
2288 KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
);
2289 /* indicate nesting in child, if needed */
2290 if (kq
->kq_level
== 0)
2298 * kqueue_drain - called when kq is closed
2302 kqueue_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2304 struct kqueue
*kq
= (struct kqueue
*)fp
->f_fglob
->fg_data
;
2306 kqueue_wakeup(kq
, 1);
2313 kqueue_stat(struct fileproc
*fp
, void *ub
, int isstat64
, __unused vfs_context_t ctx
)
2316 struct kqueue
*kq
= (struct kqueue
*)fp
->f_data
;
2317 if (isstat64
!= 0) {
2318 struct stat64
*sb64
= (struct stat64
*)ub
;
2320 bzero((void *)sb64
, sizeof(*sb64
));
2321 sb64
->st_size
= kq
->kq_count
;
2322 if (kq
->kq_state
& KQ_KEV64
)
2323 sb64
->st_blksize
= sizeof(struct kevent64_s
);
2325 sb64
->st_blksize
= sizeof(struct kevent
);
2326 sb64
->st_mode
= S_IFIFO
;
2328 struct stat
*sb
= (struct stat
*)ub
;
2330 bzero((void *)sb
, sizeof(*sb
));
2331 sb
->st_size
= kq
->kq_count
;
2332 if (kq
->kq_state
& KQ_KEV64
)
2333 sb
->st_blksize
= sizeof(struct kevent64_s
);
2335 sb
->st_blksize
= sizeof(struct kevent
);
2336 sb
->st_mode
= S_IFIFO
;
2343 * Called with the kqueue locked
2346 kqueue_wakeup(struct kqueue
*kq
, int closed
)
2348 if ((kq
->kq_state
& (KQ_SLEEP
| KQ_SEL
)) != 0 || kq
->kq_nprocess
> 0) {
2349 kq
->kq_state
&= ~(KQ_SLEEP
| KQ_SEL
);
2350 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, KQ_EVENT
,
2351 (closed
) ? THREAD_INTERRUPTED
: THREAD_AWAKENED
);
2356 klist_init(struct klist
*list
)
2363 * Query/Post each knote in the object's list
2365 * The object lock protects the list. It is assumed
2366 * that the filter/event routine for the object can
2367 * determine that the object is already locked (via
2368 * the hint) and not deadlock itself.
2370 * The object lock should also hold off pending
2371 * detach/drop operations. But we'll prevent it here
2372 * too - just in case.
2375 knote(struct klist
*list
, long hint
)
2379 SLIST_FOREACH(kn
, list
, kn_selnext
) {
2380 struct kqueue
*kq
= kn
->kn_kq
;
2383 if (kqlock2knoteuse(kq
, kn
)) {
2386 /* call the event with only a use count */
2387 result
= kn
->kn_fop
->f_event(kn
, hint
);
2389 /* if its not going away and triggered */
2390 if (knoteuse2kqlock(kq
, kn
) && result
)
2391 knote_activate(kn
, 1);
2392 /* lock held again */
2399 * attach a knote to the specified list. Return true if this is the first entry.
2400 * The list is protected by whatever lock the object it is associated with uses.
2403 knote_attach(struct klist
*list
, struct knote
*kn
)
2405 int ret
= SLIST_EMPTY(list
);
2406 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
2411 * detach a knote from the specified list. Return true if that was the last entry.
2412 * The list is protected by whatever lock the object it is associated with uses.
2415 knote_detach(struct klist
*list
, struct knote
*kn
)
2417 SLIST_REMOVE(list
, kn
, knote
, kn_selnext
);
2418 return SLIST_EMPTY(list
);
2422 * For a given knote, link a provided wait queue directly with the kqueue.
2423 * Wakeups will happen via recursive wait queue support. But nothing will move
2424 * the knote to the active list at wakeup (nothing calls knote()). Instead,
2425 * we permanently enqueue them here.
2427 * kqueue and knote references are held by caller.
2429 * caller provides the wait queue link structure.
2432 knote_link_wait_queue(struct knote
*kn
, struct wait_queue
*wq
, wait_queue_link_t wql
)
2434 struct kqueue
*kq
= kn
->kn_kq
;
2437 kr
= wait_queue_link_noalloc(wq
, kq
->kq_wqs
, wql
);
2438 if (kr
== KERN_SUCCESS
) {
2439 knote_markstayqueued(kn
);
2447 * Unlink the provided wait queue from the kqueue associated with a knote.
2448 * Also remove it from the magic list of directly attached knotes.
2450 * Note that the unlink may have already happened from the other side, so
2451 * ignore any failures to unlink and just remove it from the kqueue list.
2453 * On success, caller is responsible for the link structure
2456 knote_unlink_wait_queue(struct knote
*kn
, struct wait_queue
*wq
, wait_queue_link_t
*wqlp
)
2458 struct kqueue
*kq
= kn
->kn_kq
;
2461 kr
= wait_queue_unlink_nofree(wq
, kq
->kq_wqs
, wqlp
);
2463 kn
->kn_status
&= ~KN_STAYQUEUED
;
2466 return (kr
!= KERN_SUCCESS
) ? EINVAL
: 0;
2470 * remove all knotes referencing a specified fd
2472 * Essentially an inlined knote_remove & knote_drop
2473 * when we know for sure that the thing is a file
2475 * Entered with the proc_fd lock already held.
2476 * It returns the same way, but may drop it temporarily.
2479 knote_fdclose(struct proc
*p
, int fd
)
2481 struct filedesc
*fdp
= p
->p_fd
;
2485 list
= &fdp
->fd_knlist
[fd
];
2486 while ((kn
= SLIST_FIRST(list
)) != NULL
) {
2487 struct kqueue
*kq
= kn
->kn_kq
;
2490 panic("knote_fdclose: proc mismatch (kq->kq_p=%p != p=%p)", kq
->kq_p
, p
);
2496 * Convert the lock to a drop ref.
2497 * If we get it, go ahead and drop it.
2498 * Otherwise, we waited for it to
2499 * be dropped by the other guy, so
2500 * it is safe to move on in the list.
2502 if (kqlock2knotedrop(kq
, kn
)) {
2503 kn
->kn_fop
->f_detach(kn
);
2509 /* the fd tables may have changed - start over */
2510 list
= &fdp
->fd_knlist
[fd
];
2514 /* proc_fdlock held on entry (and exit) */
2516 knote_fdpattach(struct knote
*kn
, struct filedesc
*fdp
, struct proc
*p
)
2518 struct klist
*list
= NULL
;
2520 if (! kn
->kn_fop
->f_isfd
) {
2521 if (fdp
->fd_knhashmask
== 0)
2522 fdp
->fd_knhash
= hashinit(CONFIG_KN_HASHSIZE
, M_KQUEUE
,
2523 &fdp
->fd_knhashmask
);
2524 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
2526 if ((u_int
)fdp
->fd_knlistsize
<= kn
->kn_id
) {
2529 if (kn
->kn_id
>= (uint64_t)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
2530 || kn
->kn_id
>= (uint64_t)maxfiles
)
2533 /* have to grow the fd_knlist */
2534 size
= fdp
->fd_knlistsize
;
2535 while (size
<= kn
->kn_id
)
2538 if (size
>= (UINT_MAX
/sizeof(struct klist
*)))
2541 MALLOC(list
, struct klist
*,
2542 size
* sizeof(struct klist
*), M_KQUEUE
, M_WAITOK
);
2546 bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
,
2547 fdp
->fd_knlistsize
* sizeof(struct klist
*));
2548 bzero((caddr_t
)list
+
2549 fdp
->fd_knlistsize
* sizeof(struct klist
*),
2550 (size
- fdp
->fd_knlistsize
) * sizeof(struct klist
*));
2551 FREE(fdp
->fd_knlist
, M_KQUEUE
);
2552 fdp
->fd_knlist
= list
;
2553 fdp
->fd_knlistsize
= size
;
2555 list
= &fdp
->fd_knlist
[kn
->kn_id
];
2557 SLIST_INSERT_HEAD(list
, kn
, kn_link
);
2564 * should be called at spl == 0, since we don't want to hold spl
2565 * while calling fdrop and free.
2568 knote_drop(struct knote
*kn
, __unused
struct proc
*ctxp
)
2570 struct kqueue
*kq
= kn
->kn_kq
;
2571 struct proc
*p
= kq
->kq_p
;
2572 struct filedesc
*fdp
= p
->p_fd
;
2577 if (kn
->kn_fop
->f_isfd
)
2578 list
= &fdp
->fd_knlist
[kn
->kn_id
];
2580 list
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)];
2582 SLIST_REMOVE(list
, kn
, knote
, kn_link
);
2585 needswakeup
= (kn
->kn_status
& KN_USEWAIT
);
2590 wait_queue_wakeup_all((wait_queue_t
)kq
->kq_wqs
, &kn
->kn_status
, THREAD_AWAKENED
);
2592 if (kn
->kn_fop
->f_isfd
)
2593 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0);
2598 /* called with kqueue lock held */
2600 knote_activate(struct knote
*kn
, int propagate
)
2602 struct kqueue
*kq
= kn
->kn_kq
;
2604 kn
->kn_status
|= KN_ACTIVE
;
2606 kqueue_wakeup(kq
, 0);
2608 /* this is a real event: wake up the parent kq, too */
2610 KNOTE(&kq
->kq_sel
.si_note
, 0);
2613 /* called with kqueue lock held */
2615 knote_deactivate(struct knote
*kn
)
2617 kn
->kn_status
&= ~KN_ACTIVE
;
2621 /* called with kqueue lock held */
2623 knote_enqueue(struct knote
*kn
)
2625 if ((kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
)) == KN_STAYQUEUED
||
2626 (kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
| KN_DISABLED
)) == 0) {
2627 struct kqtailq
*tq
= kn
->kn_tq
;
2628 struct kqueue
*kq
= kn
->kn_kq
;
2630 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);
2631 kn
->kn_status
|= KN_QUEUED
;
2636 /* called with kqueue lock held */
2638 knote_dequeue(struct knote
*kn
)
2640 struct kqueue
*kq
= kn
->kn_kq
;
2642 if ((kn
->kn_status
& (KN_QUEUED
| KN_STAYQUEUED
)) == KN_QUEUED
) {
2643 struct kqtailq
*tq
= kn
->kn_tq
;
2645 TAILQ_REMOVE(tq
, kn
, kn_tqe
);
2646 kn
->kn_tq
= &kq
->kq_head
;
2647 kn
->kn_status
&= ~KN_QUEUED
;
2655 knote_zone
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
), 8192, "knote zone");
2657 /* allocate kq lock group attribute and group */
2658 kq_lck_grp_attr
= lck_grp_attr_alloc_init();
2660 kq_lck_grp
= lck_grp_alloc_init("kqueue", kq_lck_grp_attr
);
2662 /* Allocate kq lock attribute */
2663 kq_lck_attr
= lck_attr_alloc_init();
2665 /* Initialize the timer filter lock */
2666 lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
);
2668 #if VM_PRESSURE_EVENTS
2669 /* Initialize the vm pressure list lock */
2670 vm_pressure_init(kq_lck_grp
, kq_lck_attr
);
2673 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
)
2675 static struct knote
*
2678 return ((struct knote
*)zalloc(knote_zone
));
2682 knote_free(struct knote
*kn
)
2684 zfree(knote_zone
, kn
);
2688 #include <sys/param.h>
2689 #include <sys/socket.h>
2690 #include <sys/protosw.h>
2691 #include <sys/domain.h>
2692 #include <sys/mbuf.h>
2693 #include <sys/kern_event.h>
2694 #include <sys/malloc.h>
2695 #include <sys/sys_domain.h>
2696 #include <sys/syslog.h>
2699 static int kev_attach(struct socket
*so
, int proto
, struct proc
*p
);
2700 static int kev_detach(struct socket
*so
);
2701 static int kev_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
, struct proc
*p
);
2703 struct pr_usrreqs event_usrreqs
= {
2704 pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
,
2705 pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
,
2706 pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
2707 pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
2708 pru_sosend_notsupp
, soreceive
, pru_sopoll_notsupp
2711 struct protosw eventsw
[] = {
2713 .pr_type
= SOCK_RAW
,
2714 .pr_domain
= &systemdomain
,
2715 .pr_protocol
= SYSPROTO_EVENT
,
2716 .pr_flags
= PR_ATOMIC
,
2717 .pr_usrreqs
= &event_usrreqs
,
2722 struct kern_event_head kern_event_head
;
2724 static u_int32_t static_event_id
= 0;
2725 struct domain
*sysdom
= &systemdomain
;
2726 static lck_mtx_t
*sys_mtx
;
2729 * Install the protosw's for the NKE manager. Invoked at
2730 * extension load time
2733 kern_event_init(void)
2737 if ((retval
= net_add_proto(eventsw
, &systemdomain
)) != 0) {
2738 log(LOG_WARNING
, "Can't install kernel events protocol (%d)\n", retval
);
2743 * Use the domain mutex for all system event sockets
2745 sys_mtx
= sysdom
->dom_mtx
;
2747 return(KERN_SUCCESS
);
2751 kev_attach(struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
2754 struct kern_event_pcb
*ev_pcb
;
2756 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
2760 MALLOC(ev_pcb
, struct kern_event_pcb
*, sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
);
2764 ev_pcb
->ev_socket
= so
;
2765 ev_pcb
->vendor_code_filter
= 0xffffffff;
2767 so
->so_pcb
= (caddr_t
) ev_pcb
;
2768 lck_mtx_lock(sys_mtx
);
2769 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
);
2770 lck_mtx_unlock(sys_mtx
);
2777 kev_detach(struct socket
*so
)
2779 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2782 LIST_REMOVE(ev_pcb
, ev_link
);
2783 FREE(ev_pcb
, M_PCB
);
2785 so
->so_flags
|= SOF_PCBCLEARING
;
2792 * For now, kev_vendor_code and mbuf_tags use the same
2796 errno_t
kev_vendor_code_find(
2798 u_int32_t
*out_vendor_code
)
2800 if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) {
2803 return net_str_id_find_internal(string
, out_vendor_code
, NSI_VENDOR_CODE
, 1);
2806 errno_t
kev_msg_post(struct kev_msg
*event_msg
)
2808 mbuf_tag_id_t min_vendor
, max_vendor
;
2810 net_str_id_first_last(&min_vendor
, &max_vendor
, NSI_VENDOR_CODE
);
2812 if (event_msg
== NULL
)
2815 /* Limit third parties to posting events for registered vendor codes only */
2816 if (event_msg
->vendor_code
< min_vendor
||
2817 event_msg
->vendor_code
> max_vendor
)
2822 return kev_post_msg(event_msg
);
2826 int kev_post_msg(struct kev_msg
*event_msg
)
2828 struct mbuf
*m
, *m2
;
2829 struct kern_event_pcb
*ev_pcb
;
2830 struct kern_event_msg
*ev
;
2832 u_int32_t total_size
;
2835 /* Verify the message is small enough to fit in one mbuf w/o cluster */
2836 total_size
= KEV_MSG_HEADER_SIZE
;
2838 for (i
= 0; i
< 5; i
++) {
2839 if (event_msg
->dv
[i
].data_length
== 0)
2841 total_size
+= event_msg
->dv
[i
].data_length
;
2844 if (total_size
> MLEN
) {
2848 m
= m_get(M_DONTWAIT
, MT_DATA
);
2852 ev
= mtod(m
, struct kern_event_msg
*);
2853 total_size
= KEV_MSG_HEADER_SIZE
;
2855 tmp
= (char *) &ev
->event_data
[0];
2856 for (i
= 0; i
< 5; i
++) {
2857 if (event_msg
->dv
[i
].data_length
== 0)
2860 total_size
+= event_msg
->dv
[i
].data_length
;
2861 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
2862 event_msg
->dv
[i
].data_length
);
2863 tmp
+= event_msg
->dv
[i
].data_length
;
2866 ev
->id
= ++static_event_id
;
2867 ev
->total_size
= total_size
;
2868 ev
->vendor_code
= event_msg
->vendor_code
;
2869 ev
->kev_class
= event_msg
->kev_class
;
2870 ev
->kev_subclass
= event_msg
->kev_subclass
;
2871 ev
->event_code
= event_msg
->event_code
;
2873 m
->m_len
= total_size
;
2874 lck_mtx_lock(sys_mtx
);
2875 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
2877 ev_pcb
= LIST_NEXT(ev_pcb
, ev_link
)) {
2879 if (ev_pcb
->vendor_code_filter
!= KEV_ANY_VENDOR
) {
2880 if (ev_pcb
->vendor_code_filter
!= ev
->vendor_code
)
2883 if (ev_pcb
->class_filter
!= KEV_ANY_CLASS
) {
2884 if (ev_pcb
->class_filter
!= ev
->kev_class
)
2887 if ((ev_pcb
->subclass_filter
!= KEV_ANY_SUBCLASS
) &&
2888 (ev_pcb
->subclass_filter
!= ev
->kev_subclass
))
2893 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
2896 lck_mtx_unlock(sys_mtx
);
2899 /* the socket is already locked because we hold the sys_mtx here */
2900 if (sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
))
2901 sorwakeup(ev_pcb
->ev_socket
);
2905 lck_mtx_unlock(sys_mtx
);
2910 kev_control(struct socket
*so
,
2913 __unused
struct ifnet
*ifp
,
2914 __unused
struct proc
*p
)
2916 struct kev_request
*kev_req
= (struct kev_request
*) data
;
2917 struct kern_event_pcb
*ev_pcb
;
2918 struct kev_vendor_code
*kev_vendor
;
2919 u_int32_t
*id_value
= (u_int32_t
*) data
;
2925 *id_value
= static_event_id
;
2929 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2930 ev_pcb
->vendor_code_filter
= kev_req
->vendor_code
;
2931 ev_pcb
->class_filter
= kev_req
->kev_class
;
2932 ev_pcb
->subclass_filter
= kev_req
->kev_subclass
;
2936 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
2937 kev_req
->vendor_code
= ev_pcb
->vendor_code_filter
;
2938 kev_req
->kev_class
= ev_pcb
->class_filter
;
2939 kev_req
->kev_subclass
= ev_pcb
->subclass_filter
;
2942 case SIOCGKEVVENDOR
:
2943 kev_vendor
= (struct kev_vendor_code
*)data
;
2945 /* Make sure string is NULL terminated */
2946 kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0;
2948 return net_str_id_find_internal(kev_vendor
->vendor_string
,
2949 &kev_vendor
->vendor_code
, NSI_VENDOR_CODE
, 0);
2958 #endif /* SOCKETS */
2962 fill_kqueueinfo(struct kqueue
*kq
, struct kqueue_info
* kinfo
)
2964 struct vinfo_stat
* st
;
2966 /* No need for the funnel as fd is kept alive */
2968 st
= &kinfo
->kq_stat
;
2970 st
->vst_size
= kq
->kq_count
;
2971 if (kq
->kq_state
& KQ_KEV64
)
2972 st
->vst_blksize
= sizeof(struct kevent64_s
);
2974 st
->vst_blksize
= sizeof(struct kevent
);
2975 st
->vst_mode
= S_IFIFO
;
2976 if (kq
->kq_state
& KQ_SEL
)
2977 kinfo
->kq_state
|= PROC_KQUEUE_SELECT
;
2978 if (kq
->kq_state
& KQ_SLEEP
)
2979 kinfo
->kq_state
|= PROC_KQUEUE_SLEEP
;
2986 knote_markstayqueued(struct knote
*kn
)
2989 kn
->kn_status
|= KN_STAYQUEUED
;
2991 kqunlock(kn
->kn_kq
);