2  * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * The contents of this file constitute Original Code as defined in and 
   7  * are subject to the Apple Public Source License Version 1.1 (the 
   8  * "License").  You may not use this file except in compliance with the 
   9  * License.  Please obtain a copy of the License at 
  10  * http://www.apple.com/publicsource and read it before using this file. 
  12  * This Original Code and all software distributed under the License are 
  13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the 
  17  * License for the specific language governing rights and limitations 
  20  * @APPLE_LICENSE_HEADER_END@ 
  24  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org> 
  25  * All rights reserved. 
  27  * Redistribution and use in source and binary forms, with or without 
  28  * modification, are permitted provided that the following conditions 
  30  * 1. Redistributions of source code must retain the above copyright 
  31  *    notice, this list of conditions and the following disclaimer. 
  32  * 2. Redistributions in binary form must reproduce the above copyright 
  33  *    notice, this list of conditions and the following disclaimer in the 
  34  *    documentation and/or other materials provided with the distribution. 
  36  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
  37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
  40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  49  *      @(#)kern_event.c       1.0 (3/31/2000) 
  53 #include <sys/param.h> 
  54 #include <sys/systm.h> 
  55 #include <sys/filedesc.h> 
  56 #include <sys/kernel.h> 
  57 #include <sys/proc_internal.h> 
  58 #include <sys/kauth.h> 
  59 #include <sys/malloc.h>  
  60 #include <sys/unistd.h> 
  61 #include <sys/file_internal.h> 
  62 #include <sys/fcntl.h> 
  63 #include <sys/select.h> 
  64 #include <sys/queue.h> 
  65 #include <sys/event.h> 
  66 #include <sys/eventvar.h> 
  67 #include <sys/protosw.h> 
  68 #include <sys/socket.h> 
  69 #include <sys/socketvar.h> 
  71 #include <sys/sysctl.h> 
  73 #include <sys/sysproto.h> 
  76 #include <sys/proc_info.h> 
  78 #include <kern/lock.h> 
  79 #include <kern/clock.h> 
  80 #include <kern/thread_call.h> 
  81 #include <kern/sched_prim.h> 
  82 #include <kern/zalloc.h> 
  83 #include <kern/assert.h> 
  85 #include <libkern/libkern.h> 
  87 extern void unix_syscall_return(int); 
  89 MALLOC_DEFINE(M_KQUEUE
, "kqueue", "memory for kqueue system"); 
  91 static inline void kqlock(struct kqueue 
*kq
); 
  92 static inline void kqunlock(struct kqueue 
*kq
); 
  94 static int      kqlock2knoteuse(struct kqueue 
*kq
, struct knote 
*kn
); 
  95 static int      kqlock2knoteusewait(struct kqueue 
*kq
, struct knote 
*kn
); 
  96 static int      kqlock2knotedrop(struct kqueue 
*kq
, struct knote 
*kn
); 
  97 static int      knoteuse2kqlock(struct kqueue 
*kq
, struct knote 
*kn
); 
  99 static void     kqueue_wakeup(struct kqueue 
*kq
); 
 100 static int      kqueue_read(struct fileproc 
*fp
, struct uio 
*uio
, 
 101                     kauth_cred_t cred
, int flags
, struct proc 
*p
); 
 102 static int      kqueue_write(struct fileproc 
*fp
, struct uio 
*uio
, 
 103                     kauth_cred_t cred
, int flags
, struct proc 
*p
); 
 104 static int      kqueue_ioctl(struct fileproc 
*fp
, u_long com
, caddr_t data
, 
 106 static int      kqueue_select(struct fileproc 
*fp
, int which
, void *wql
,  
 108 static int      kqueue_close(struct fileglob 
*fp
, struct proc 
*p
); 
 109 static int      kqueue_kqfilter(struct fileproc 
*fp
, struct knote 
*kn
, struct proc 
*p
); 
 110 extern int      kqueue_stat(struct fileproc 
*fp
, struct stat 
*st
, struct proc 
*p
); 
 112 static struct fileops kqueueops 
= { 
 122 static int kevent_copyin(user_addr_t 
*addrp
, struct kevent 
*kevp
, struct proc 
*p
); 
 123 static int kevent_copyout(struct kevent 
*kevp
, user_addr_t 
*addrp
, struct proc 
*p
); 
 125 static int      kevent_callback(struct kqueue 
*kq
, struct kevent 
*kevp
, void *data
); 
 126 static void     kevent_continue(struct kqueue 
*kq
, void *data
, int error
); 
 127 static void     kevent_scan_continue(void *contp
, wait_result_t wait_result
); 
 128 static int      kevent_process(struct kqueue 
*kq
, kevent_callback_t callback
, 
 129                                void *data
, int *countp
, struct proc 
*p
); 
 130 static void     knote_put(struct knote 
*kn
); 
 131 static int      knote_fdpattach(struct knote 
*kn
, struct filedesc 
*fdp
, struct proc 
*p
); 
 132 static void     knote_drop(struct knote 
*kn
, struct proc 
*p
); 
 133 static void     knote_activate(struct knote 
*kn
); 
 134 static void     knote_deactivate(struct knote 
*kn
); 
 135 static void     knote_enqueue(struct knote 
*kn
); 
 136 static void     knote_dequeue(struct knote 
*kn
); 
 137 static struct   knote 
*knote_alloc(void); 
 138 static void     knote_free(struct knote 
*kn
); 
 139 extern void     knote_init(void); 
 141 static int      filt_fileattach(struct knote 
*kn
); 
 142 static struct filterops file_filtops 
= 
 143         { 1, filt_fileattach
, NULL
, NULL 
}; 
 145 static void     filt_kqdetach(struct knote 
*kn
); 
 146 static int      filt_kqueue(struct knote 
*kn
, long hint
); 
 147 static struct filterops kqread_filtops 
= 
 148         { 1, NULL
, filt_kqdetach
, filt_kqueue 
}; 
 151  * placeholder for not-yet-implemented filters 
 153 static int      filt_badattach(struct knote 
*kn
); 
 154 static struct filterops bad_filtops 
= 
 155         { 0, filt_badattach
, 0 , 0 }; 
 157 static int      filt_procattach(struct knote 
*kn
); 
 158 static void     filt_procdetach(struct knote 
*kn
); 
 159 static int      filt_proc(struct knote 
*kn
, long hint
); 
 161 static struct filterops proc_filtops 
= 
 162         { 0, filt_procattach
, filt_procdetach
, filt_proc 
}; 
 164 extern struct filterops fs_filtops
; 
 166 extern struct filterops sig_filtops
; 
 170 static int      filt_timercompute(struct knote 
*kn
, uint64_t *abs_time
); 
 171 static void     filt_timerexpire(void *knx
, void *param1
); 
 172 static int      filt_timerattach(struct knote 
*kn
); 
 173 static void     filt_timerdetach(struct knote 
*kn
); 
 174 static int      filt_timer(struct knote 
*kn
, long hint
); 
 176 static struct filterops timer_filtops 
= 
 177         { 0, filt_timerattach
, filt_timerdetach
, filt_timer 
}; 
 179 /* to avoid arming timers that fire quicker than we can handle */ 
 180 static uint64_t filt_timerfloor 
= 0;  
 182 static lck_mtx_t _filt_timerlock
; 
 183 static void     filt_timerlock(void); 
 184 static void     filt_timerunlock(void); 
 187  * Sentinel marker for a thread scanning through the list of 
 190 static struct filterops threadmarker_filtops 
= 
 191         { 0, filt_badattach
, 0, 0 }; 
 193 static zone_t   knote_zone
; 
 195 #define KN_HASHSIZE             64              /* XXX should be tunable */ 
 196 #define KN_HASH(val, mask)      (((val) ^ (val >> 8)) & (mask)) 
 199 extern struct filterops aio_filtops
; 
 203  * Table for for all system-defined filters. 
 205 static struct filterops 
*sysfilt_ops
[] = { 
 206         &file_filtops
,                  /* EVFILT_READ */ 
 207         &file_filtops
,                  /* EVFILT_WRITE */ 
 209         &aio_filtops
,                   /* EVFILT_AIO */ 
 211         &bad_filtops
,                   /* EVFILT_AIO */ 
 213         &file_filtops
,                  /* EVFILT_VNODE */ 
 214         &proc_filtops
,                  /* EVFILT_PROC */ 
 215         &sig_filtops
,                   /* EVFILT_SIGNAL */ 
 216         &timer_filtops
,                 /* EVFILT_TIMER */ 
 217         &bad_filtops
,                   /* EVFILT_MACHPORT */ 
 218         &fs_filtops                     
/* EVFILT_FS */ 
 222  * kqueue/note lock attributes and implementations 
 224  *      kqueues have locks, while knotes have use counts 
 225  *      Most of the knote state is guarded by the object lock. 
 226  *      the knote "inuse" count and status use the kqueue lock. 
 228 lck_grp_attr_t 
* kq_lck_grp_attr
; 
 229 lck_grp_t 
* kq_lck_grp
; 
 230 lck_attr_t 
* kq_lck_attr
; 
 233 kqlock(struct kqueue 
*kq
) 
 235         lck_spin_lock(&kq
->kq_lock
); 
 239 kqunlock(struct kqueue 
*kq
) 
 241         lck_spin_unlock(&kq
->kq_lock
); 
 245  * Convert a kq lock to a knote use referece. 
 247  *      If the knote is being dropped, we can't get 
 248  *      a use reference, so just return with it 
 251  *      - kq locked at entry 
 252  *      - unlock on exit if we get the use reference 
 255 kqlock2knoteuse(struct kqueue 
*kq
, struct knote 
*kn
) 
 257         if (kn
->kn_status 
& KN_DROPPING
) 
 265  * Convert a kq lock to a knote use referece. 
 267  *      If the knote is being dropped, we can't get 
 268  *      a use reference, so just return with it 
 271  *      - kq locked at entry 
 272  *      - kq always unlocked on exit 
 275 kqlock2knoteusewait(struct kqueue 
*kq
, struct knote 
*kn
) 
 277         if (!kqlock2knoteuse(kq
, kn
)) { 
 278                 kn
->kn_status 
|= KN_DROPWAIT
; 
 279                 assert_wait(&kn
->kn_status
, THREAD_UNINT
); 
 281                 thread_block(THREAD_CONTINUE_NULL
); 
 288  * Convert from a knote use reference back to kq lock. 
 290  *      Drop a use reference and wake any waiters if 
 291  *      this is the last one. 
 293  *      The exit return indicates if the knote is 
 294  *      still alive - but the kqueue lock is taken 
 298 knoteuse2kqlock(struct kqueue 
*kq
, struct knote 
*kn
) 
 301         if ((--kn
->kn_inuse 
== 0) && 
 302             (kn
->kn_status 
& KN_USEWAIT
)) { 
 303                 kn
->kn_status 
&= ~KN_USEWAIT
; 
 304                 thread_wakeup(&kn
->kn_inuse
); 
 306         return ((kn
->kn_status 
& KN_DROPPING
) == 0); 
 310  * Convert a kq lock to a knote drop referece. 
 312  *      If the knote is in use, wait for the use count 
 313  *      to subside.  We first mark our intention to drop 
 314  *      it - keeping other users from "piling on." 
 315  *      If we are too late, we have to wait for the 
 316  *      other drop to complete. 
 318  *      - kq locked at entry 
 319  *      - always unlocked on exit. 
 320  *      - caller can't hold any locks that would prevent 
 321  *        the other dropper from completing. 
 324 kqlock2knotedrop(struct kqueue 
*kq
, struct knote 
*kn
) 
 327         if ((kn
->kn_status 
& KN_DROPPING
) == 0) { 
 328                 kn
->kn_status 
|= KN_DROPPING
; 
 329                 if (kn
->kn_inuse 
> 0) { 
 330                         kn
->kn_status 
|= KN_USEWAIT
; 
 331                         assert_wait(&kn
->kn_inuse
, THREAD_UNINT
); 
 333                         thread_block(THREAD_CONTINUE_NULL
); 
 338                 kn
->kn_status 
|= KN_DROPWAIT
; 
 339                 assert_wait(&kn
->kn_status
, THREAD_UNINT
); 
 341                 thread_block(THREAD_CONTINUE_NULL
); 
 347  * Release a knote use count reference. 
 350 knote_put(struct knote 
*kn
) 
 352         struct kqueue 
*kq 
= kn
->kn_kq
; 
 355         if ((--kn
->kn_inuse 
== 0) &&  
 356             (kn
->kn_status 
& KN_USEWAIT
)) { 
 357                 kn
->kn_status 
&= ~KN_USEWAIT
; 
 358                 thread_wakeup(&kn
->kn_inuse
); 
 366 filt_fileattach(struct knote 
*kn
) 
 369         return (fo_kqfilter(kn
->kn_fp
, kn
, current_proc())); 
 372 #define f_flag f_fglob->fg_flag 
 373 #define f_type f_fglob->fg_type 
 374 #define f_msgcount f_fglob->fg_msgcount 
 375 #define f_cred f_fglob->fg_cred 
 376 #define f_ops f_fglob->fg_ops 
 377 #define f_offset f_fglob->fg_offset 
 378 #define f_data f_fglob->fg_data 
 381 filt_kqdetach(struct knote 
*kn
) 
 383         struct kqueue 
*kq 
= (struct kqueue 
*)kn
->kn_fp
->f_data
; 
 386         KNOTE_DETACH(&kq
->kq_sel
.si_note
, kn
); 
 392 filt_kqueue(struct knote 
*kn
, __unused 
long hint
) 
 394         struct kqueue 
*kq 
= (struct kqueue 
*)kn
->kn_fp
->f_data
; 
 396         kn
->kn_data 
= kq
->kq_count
; 
 397         return (kn
->kn_data 
> 0); 
 401 filt_procattach(struct knote 
*kn
) 
 406         funnel_state 
= thread_funnel_set(kernel_flock
, TRUE
); 
 408         p 
= pfind(kn
->kn_id
); 
 410                 thread_funnel_set(kernel_flock
, funnel_state
); 
 414         kn
->kn_flags 
|= EV_CLEAR
;               /* automatically set */ 
 415         kn
->kn_hookid 
= 1;                      /* mark exit not seen */ 
 418          * internal flag indicating registration done by kernel 
 420         if (kn
->kn_flags 
& EV_FLAG1
) { 
 421                 kn
->kn_data 
= (int)kn
->kn_sdata
;        /* ppid */ 
 422                 kn
->kn_fflags 
= NOTE_CHILD
; 
 423                 kn
->kn_flags 
&= ~EV_FLAG1
; 
 426         /* XXX lock the proc here while adding to the list? */ 
 427         KNOTE_ATTACH(&p
->p_klist
, kn
); 
 429         thread_funnel_set(kernel_flock
, funnel_state
); 
 435  * The knote may be attached to a different process, which may exit, 
 436  * leaving nothing for the knote to be attached to.  In that case, 
 437  * we wont be able to find the process from its pid.  But the exit 
 438  * code may still be processing the knote list for the target process. 
 439  * We may have to wait for that processing to complete before we can 
 440  * return (and presumably free the knote) without actually removing 
 441  * it from the dead process' knote list. 
 444 filt_procdetach(struct knote 
*kn
) 
 449         funnel_state 
= thread_funnel_set(kernel_flock
, TRUE
); 
 450         p 
= pfind(kn
->kn_id
); 
 452         if (p 
!= (struct proc 
*)NULL
) { 
 453                 KNOTE_DETACH(&p
->p_klist
, kn
); 
 454         } else if (kn
->kn_hookid 
!= 0) {        /* if not NOTE_EXIT yet */ 
 455                 kn
->kn_hookid 
= -1;     /* we are detaching but... */ 
 456                 assert_wait(&kn
->kn_hook
, THREAD_UNINT
); /* have to wait */ 
 457                 thread_block(THREAD_CONTINUE_NULL
); 
 459         thread_funnel_set(kernel_flock
, funnel_state
); 
 463 filt_proc(struct knote 
*kn
, long hint
) 
 469                 /* must hold the funnel when coming from below */ 
 470                 assert(thread_funnel_get() != (funnel_t
)0); 
 473                  * mask off extra data 
 475                 event 
= (u_int
)hint 
& NOTE_PCTRLMASK
; 
 478                  * if the user is interested in this event, record it. 
 480                 if (kn
->kn_sfflags 
& event
) 
 481                         kn
->kn_fflags 
|= event
; 
 484                  * process is gone, so flag the event as finished. 
 486                  * If someone was trying to detach, but couldn't 
 487                  * find the proc to complete the detach, wake them 
 488                  * up (nothing will ever need to walk the per-proc 
 489                  * knote list again - so its safe for them to dump 
 492                 if (event 
== NOTE_EXIT
) { 
 493                         boolean_t detaching 
= (kn
->kn_hookid 
== -1); 
 496                         kn
->kn_flags 
|= (EV_EOF 
| EV_ONESHOT
);  
 498                                 thread_wakeup(&kn
->kn_hookid
); 
 503                  * process forked, and user wants to track the new process, 
 504                  * so attach a new knote to it, and immediately report an 
 505                  * event with the parent's pid. 
 507                 if ((event 
== NOTE_FORK
) && (kn
->kn_sfflags 
& NOTE_TRACK
)) { 
 512                          * register knote with new process. 
 514                         kev
.ident 
= hint 
& NOTE_PDATAMASK
;      /* pid */ 
 515                         kev
.filter 
= kn
->kn_filter
; 
 516                         kev
.flags 
= kn
->kn_flags 
| EV_ADD 
| EV_ENABLE 
| EV_FLAG1
; 
 517                         kev
.fflags 
= kn
->kn_sfflags
; 
 518                         kev
.data 
= kn
->kn_id
;                   /* parent */ 
 519                         kev
.udata 
= kn
->kn_kevent
.udata
;        /* preserve udata */ 
 520                         error 
= kevent_register(kn
->kn_kq
, &kev
, NULL
); 
 522                                 kn
->kn_fflags 
|= NOTE_TRACKERR
; 
 526         return (kn
->kn_fflags 
!= 0); /* atomic check - no funnel needed from above */ 
 530  * filt_timercompute - compute absolute timeout 
 532  *      The saved-data field in the knote contains the 
 533  *      time value.  The saved filter-flags indicates 
 534  *      the unit of measurement. 
 536  *      If the timeout is not absolute, adjust it for 
 540 filt_timercompute(struct knote 
*kn
, uint64_t *abs_time
) 
 545         switch (kn
->kn_sfflags 
& (NOTE_SECONDS
|NOTE_USECONDS
|NOTE_NSECONDS
)) { 
 547                 multiplier 
= NSEC_PER_SEC
; 
 550                 multiplier 
= NSEC_PER_USEC
; 
 555         case 0: /* milliseconds (default) */ 
 556                 multiplier 
= NSEC_PER_SEC 
/ 1000; 
 561         nanoseconds_to_absolutetime((uint64_t)kn
->kn_sdata 
* multiplier
, &raw
); 
 562         if (raw 
<= filt_timerfloor
) { 
 566         if ((kn
->kn_sfflags 
& NOTE_ABSOLUTE
) == NOTE_ABSOLUTE
) { 
 567                 uint32_t seconds
, nanoseconds
; 
 570                 clock_get_calendar_nanotime(&seconds
, &nanoseconds
); 
 571                 nanoseconds_to_absolutetime((uint64_t)seconds 
* NSEC_PER_SEC 
+ nanoseconds
, 
 573                 if (now 
>= raw 
+ filt_timerfloor
) { 
 579         clock_absolutetime_interval_to_deadline(raw
, abs_time
); 
 584  * filt_timerexpire - the timer callout routine 
 586  *      Just propagate the timer event into the knote 
 587  *      filter routine (by going through the knote 
 588  *      synchronization point).  Pass a hint to 
 589  *      indicate this is a real event, not just a 
 593 filt_timerexpire(void *knx
, __unused 
void *spare
) 
 595         struct klist timer_list
; 
 596         struct knote 
*kn 
= knx
; 
 598         /* no "object" for timers, so fake a list */ 
 599         SLIST_INIT(&timer_list
); 
 600         SLIST_INSERT_HEAD(&timer_list
, kn
, kn_selnext
);  
 601         KNOTE(&timer_list
, 1); 
 605  * data contains amount of time to sleep, in milliseconds, 
 606  * or a pointer to a timespec structure. 
 609 filt_timerattach(struct knote 
*kn
) 
 611         thread_call_t callout
; 
 615         error 
= filt_timercompute(kn
, &deadline
); 
 620                 callout 
= thread_call_allocate(filt_timerexpire
, kn
); 
 624                 /* handle as immediate */ 
 630         kn
->kn_hook 
= (caddr_t
)callout
; 
 632         /* absolute=EV_ONESHOT */ 
 633         if (kn
->kn_sfflags 
& NOTE_ABSOLUTE
) 
 634                 kn
->kn_flags 
|= EV_ONESHOT
;  
 637                 /* all others - if not faking immediate */ 
 638                 kn
->kn_flags 
|= EV_CLEAR
; 
 639                 thread_call_enter_delayed(callout
, deadline
); 
 650 filt_timerdetach(struct knote 
*kn
) 
 652         thread_call_t callout
; 
 655         callout 
= (thread_call_t
)kn
->kn_hook
; 
 656         if (callout 
!= NULL
) { 
 659                 /* cancel the callout if we can */ 
 660                 cancelled 
= thread_call_cancel(callout
); 
 662                         /* got it, just free it */ 
 665                         thread_call_free(callout
); 
 668                 /* we have to wait for the expire routine.  */ 
 669                 kn
->kn_hookid 
= -1;     /* we are detaching */ 
 670                 assert_wait(&kn
->kn_hook
, THREAD_UNINT
); 
 672                 thread_block(THREAD_CONTINUE_NULL
); 
 673                 assert(kn
->kn_hook 
== NULL
); 
 683 filt_timer(struct knote 
*kn
, __unused 
long hint
) 
 689                 thread_call_t callout
; 
 696                 detaching 
= (kn
->kn_hookid 
< 0); 
 697                 callout 
= (thread_call_t
)kn
->kn_hook
; 
 699                 if (!detaching 
&& (kn
->kn_flags 
& EV_ONESHOT
) == 0) { 
 703                         /* user input data may have changed - deal */ 
 704                         error 
= filt_timercompute(kn
, &deadline
); 
 706                                 kn
->kn_flags 
|= EV_ERROR
; 
 708                         } else if (deadline 
== 0) { 
 709                                 /* revert to fake immediate */ 
 710                                 kn
->kn_flags 
&= ~EV_CLEAR
; 
 714                                 /* keep the callout and re-arm */ 
 715                                 thread_call_enter_delayed(callout
, deadline
); 
 722                 thread_call_free(callout
); 
 724                 /* if someone is waiting for timer to pop */ 
 726                         thread_wakeup(&kn
->kn_hook
); 
 734         /* change fake timer to real if needed */ 
 735         while (kn
->kn_hookid 
> 0 && kn
->kn_sdata 
> 0) { 
 738                 /* update the fake timer (make real) */ 
 742                 error 
= filt_timerattach(kn
); 
 745                         kn
->kn_flags 
|= EV_ERROR
; 
 752         /* if still fake, pretend it fired */ 
 753         if (kn
->kn_hookid 
> 0) 
 756         result 
= (kn
->kn_data 
!= 0); 
 764         lck_mtx_lock(&_filt_timerlock
); 
 768 filt_timerunlock(void) 
 770         lck_mtx_unlock(&_filt_timerlock
); 
 774  * JMM - placeholder for not-yet-implemented filters 
 777 filt_badattach(__unused 
struct knote 
*kn
) 
 784 kqueue_alloc(struct proc 
*p
) 
 786         struct filedesc 
*fdp 
= p
->p_fd
; 
 789         MALLOC_ZONE(kq
, struct kqueue 
*, sizeof(struct kqueue
), M_KQUEUE
, M_WAITOK
); 
 791                 bzero(kq
, sizeof(struct kqueue
)); 
 792                 lck_spin_init(&kq
->kq_lock
, kq_lck_grp
, kq_lck_attr
); 
 793                 TAILQ_INIT(&kq
->kq_head
); 
 794                 TAILQ_INIT(&kq
->kq_inprocess
); 
 798         if (fdp
->fd_knlistsize 
< 0) { 
 800                 if (fdp
->fd_knlistsize 
< 0) 
 801                         fdp
->fd_knlistsize 
= 0;         /* this process has had a kq */ 
 810  * kqueue_dealloc - detach all knotes from a kqueue and free it 
 812  *      We walk each list looking for knotes referencing this 
 813  *      this kqueue.  If we find one, we try to drop it.  But 
 814  *      if we fail to get a drop reference, that will wait 
 815  *      until it is dropped.  So, we can just restart again 
 816  *      safe in the assumption that the list will eventually 
 817  *      not contain any more references to this kqueue (either 
 818  *      we dropped them all, or someone else did). 
 820  *      Assumes no new events are being added to the kqueue. 
 821  *      Nothing locked on entry or exit. 
 824 kqueue_dealloc(struct kqueue 
*kq
, struct proc 
*p
) 
 826         struct filedesc 
*fdp 
= p
->p_fd
; 
 831         for (i 
= 0; i 
< fdp
->fd_knlistsize
; i
++) { 
 832                 kn 
= SLIST_FIRST(&fdp
->fd_knlist
[i
]); 
 834                         if (kq 
== kn
->kn_kq
) { 
 837                                 /* drop it ourselves or wait */ 
 838                                 if (kqlock2knotedrop(kq
, kn
)) { 
 839                                         kn
->kn_fop
->f_detach(kn
); 
 843                                 /* start over at beginning of list */ 
 844                                 kn 
= SLIST_FIRST(&fdp
->fd_knlist
[i
]); 
 847                         kn 
= SLIST_NEXT(kn
, kn_link
); 
 850         if (fdp
->fd_knhashmask 
!= 0) { 
 851                 for (i 
= 0; i 
< (int)fdp
->fd_knhashmask 
+ 1; i
++) { 
 852                         kn 
= SLIST_FIRST(&fdp
->fd_knhash
[i
]); 
 854                                 if (kq 
== kn
->kn_kq
) { 
 857                                         /* drop it ourselves or wait */ 
 858                                         if (kqlock2knotedrop(kq
, kn
)) { 
 859                                                 kn
->kn_fop
->f_detach(kn
); 
 863                                         /* start over at beginning of list */ 
 864                                         kn 
= SLIST_FIRST(&fdp
->fd_knhash
[i
]); 
 867                                 kn 
= SLIST_NEXT(kn
, kn_link
); 
 872         lck_spin_destroy(&kq
->kq_lock
, kq_lck_grp
); 
 873         FREE_ZONE(kq
, sizeof(struct kqueue
), M_KQUEUE
); 
 877 kqueue(struct proc 
*p
, __unused 
struct kqueue_args 
*uap
, register_t 
*retval
) 
 883         error 
= falloc(p
, &fp
, &fd
); 
 888         kq 
= kqueue_alloc(p
); 
 894         fp
->f_flag 
= FREAD 
| FWRITE
; 
 895         fp
->f_type 
= DTYPE_KQUEUE
; 
 896         fp
->f_ops 
= &kqueueops
; 
 897         fp
->f_data 
= (caddr_t
)kq
; 
 900         *fdflags(p
, fd
) &= ~UF_RESERVED
; 
 901         fp_drop(p
, fd
, fp
, 1); 
 909 kqueue_portset_np(__unused 
struct proc 
*p
,  
 910                                   __unused 
struct kqueue_portset_np_args 
*uap
,  
 911                                   __unused register_t 
*retval
) 
 913                 /* JMM - Placeholder for now */ 
 918 kqueue_from_portset_np(__unused 
struct proc 
*p
,  
 919                                            __unused 
struct kqueue_from_portset_np_args 
*uap
,  
 920                                            __unused register_t 
*retval
) 
 922                 /* JMM - Placeholder for now */ 
 927 kevent_copyin(user_addr_t 
*addrp
, struct kevent 
*kevp
, struct proc 
*p
) 
 932         if (IS_64BIT_PROCESS(p
)) { 
 933                 struct user_kevent kev64
; 
 935                 advance 
= sizeof(kev64
); 
 936                 error 
= copyin(*addrp
, (caddr_t
)&kev64
, advance
); 
 939                 kevp
->ident 
= CAST_DOWN(uintptr_t, kev64
.ident
); 
 940                 kevp
->filter 
= kev64
.filter
; 
 941                 kevp
->flags 
= kev64
.flags
; 
 942                 kevp
->fflags 
= kev64
.fflags
; 
 943                 kevp
->data 
= CAST_DOWN(intptr_t, kev64
.data
); 
 944                 kevp
->udata 
= kev64
.udata
; 
 947                  * compensate for legacy in-kernel kevent layout 
 948                  * where the udata field is alredy 64-bit. 
 950                 advance 
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
); 
 951                 error 
= copyin(*addrp
, (caddr_t
)kevp
, advance
); 
 959 kevent_copyout(struct kevent 
*kevp
, user_addr_t 
*addrp
, struct proc 
*p
) 
 964         if (IS_64BIT_PROCESS(p
)) { 
 965                 struct user_kevent kev64
; 
 967                 kev64
.ident 
= (uint64_t) kevp
->ident
; 
 968                 kev64
.filter 
= kevp
->filter
; 
 969                 kev64
.flags 
= kevp
->flags
; 
 970                 kev64
.fflags 
= kevp
->fflags
; 
 971                 kev64
.data 
= (int64_t) kevp
->data
; 
 972                 kev64
.udata 
= kevp
->udata
; 
 973                 advance 
= sizeof(kev64
); 
 974                 error 
= copyout((caddr_t
)&kev64
, *addrp
, advance
); 
 977                  * compensate for legacy in-kernel kevent layout 
 978                  * where the udata field is alredy 64-bit. 
 980                 advance 
= sizeof(*kevp
) + sizeof(void *) - sizeof(user_addr_t
); 
 981                 error 
= copyout((caddr_t
)kevp
, *addrp
, advance
); 
 989  * kevent_continue - continue a kevent syscall after blocking 
 991  *      assume we inherit a use count on the kq fileglob. 
 995 kevent_continue(__unused 
struct kqueue 
*kq
, void *data
, int error
) 
 997         struct _kevent 
*cont_args
; 
1002         struct proc 
*p 
= current_proc(); 
1004         cont_args 
= (struct _kevent 
*)data
; 
1005         noutputs 
= cont_args
->eventout
; 
1006         retval 
= cont_args
->retval
; 
1010         fp_drop(p
, fd
, fp
, 0); 
1012         /* don't restart after signals... */ 
1013         if (error 
== ERESTART
) 
1015         else if (error 
== EWOULDBLOCK
) 
1019         unix_syscall_return(error
); 
1023  * kevent - [syscall] register and wait for kernel events 
1028 kevent(struct proc 
*p
, struct kevent_args 
*uap
, register_t 
*retval
) 
1030         user_addr_t changelist 
= uap
->changelist
; 
1031         user_addr_t ueventlist 
= uap
->eventlist
; 
1032         int nchanges 
= uap
->nchanges
; 
1033         int nevents 
= uap
->nevents
; 
1036         struct _kevent 
*cont_args
; 
1039         struct fileproc 
*fp
; 
1041         int error
, noutputs
; 
1044         /* convert timeout to absolute - if we have one */ 
1045         if (uap
->timeout 
!= USER_ADDR_NULL
) { 
1047                 if ( IS_64BIT_PROCESS(p
) ) { 
1048                         struct user_timespec ts
; 
1049                         error 
= copyin( uap
->timeout
, &ts
, sizeof(ts
) ); 
1050                         if ((ts
.tv_sec 
& 0xFFFFFFFF00000000ull
) != 0) 
1053                                 TIMESPEC_TO_TIMEVAL(&rtv
, &ts
); 
1056                         error 
= copyin( uap
->timeout
, &ts
, sizeof(ts
) ); 
1057                         TIMESPEC_TO_TIMEVAL(&rtv
, &ts
); 
1061                 if (itimerfix(&rtv
)) 
1063                 getmicrouptime(&atv
); 
1064                 timevaladd(&atv
, &rtv
); 
1070         /* get a usecount for the kq itself */ 
1071         if ((error 
= fp_getfkq(p
, fd
, &fp
, &kq
)) != 0) 
1074         /* register all the change requests the user provided... */ 
1076         while (nchanges 
> 0 && error 
== 0) { 
1077                 error 
= kevent_copyin(&changelist
, &kev
, p
); 
1081                 kev
.flags 
&= ~EV_SYSFLAGS
; 
1082                 error 
= kevent_register(kq
, &kev
, p
); 
1083                 if (error 
&& nevents 
> 0) { 
1084                         kev
.flags 
= EV_ERROR
; 
1086                         error 
= kevent_copyout(&kev
, &ueventlist
, p
); 
1095         /* store the continuation/completion data in the uthread */ 
1096         ut 
= (uthread_t
)get_bsdthread_info(current_thread()); 
1097         cont_args 
= (struct _kevent 
*)&ut
->uu_state
.ss_kevent
; 
1100         cont_args
->retval 
= retval
; 
1101         cont_args
->eventlist 
= ueventlist
; 
1102         cont_args
->eventcount 
= nevents
; 
1103         cont_args
->eventout 
= noutputs
; 
1105         if (nevents 
> 0 && noutputs 
== 0 && error 
== 0) 
1106                 error 
= kevent_scan(kq
, kevent_callback
, 
1107                                     kevent_continue
, cont_args
, 
1109         kevent_continue(kq
, cont_args
, error
); 
1116  * kevent_callback - callback for each individual event 
1118  *      called with nothing locked 
1119  *      caller holds a reference on the kqueue 
1123 kevent_callback(__unused 
struct kqueue 
*kq
, struct kevent 
*kevp
, void *data
) 
1125         struct _kevent 
*cont_args
; 
1128         cont_args 
= (struct _kevent 
*)data
; 
1129         assert(cont_args
->eventout 
< cont_arg
->eventcount
); 
1132          * Copy out the appropriate amount of event data for this user. 
1134         error 
= kevent_copyout(kevp
, &cont_args
->eventlist
, current_proc()); 
1137          * If there isn't space for additional events, return 
1138          * a harmless error to stop the processing here 
1140         if (error 
== 0 && ++cont_args
->eventout 
== cont_args
->eventcount
) 
1141                         error 
= EWOULDBLOCK
; 
1146  * kevent_register - add a new event to a kqueue 
1148  *      Creates a mapping between the event source and 
1149  *      the kqueue via a knote data structure. 
1151  *      Because many/most the event sources are file 
1152  *      descriptor related, the knote is linked off 
1153  *      the filedescriptor table for quick access. 
1155  *      called with nothing locked 
1156  *      caller holds a reference on the kqueue 
1160 kevent_register(struct kqueue 
*kq
, struct kevent 
*kev
, struct proc 
*p
) 
1162         struct filedesc 
*fdp 
= kq
->kq_fdp
; 
1163         struct filterops 
*fops
; 
1164         struct fileproc 
*fp 
= NULL
; 
1165         struct knote 
*kn 
= NULL
; 
1168         if (kev
->filter 
< 0) { 
1169                 if (kev
->filter 
+ EVFILT_SYSCOUNT 
< 0) 
1171                 fops 
= sysfilt_ops
[~kev
->filter
];       /* to 0-base index */ 
1175                  * filter attach routine is responsible for insuring that 
1176                  * the identifier can be attached to it. 
1178                 printf("unknown filter: %d\n", kev
->filter
); 
1182         /* this iocount needs to be dropped if it is not registered */ 
1183         if (fops
->f_isfd 
&& (error 
= fp_lookup(p
, kev
->ident
, &fp
, 0)) != 0) 
1189                 /* fd-based knotes are linked off the fd table */ 
1190                 if (kev
->ident 
< (u_int
)fdp
->fd_knlistsize
) { 
1191                         SLIST_FOREACH(kn
, &fdp
->fd_knlist
[kev
->ident
], kn_link
) 
1192                                 if (kq 
== kn
->kn_kq 
&& 
1193                                     kev
->filter 
== kn
->kn_filter
) 
1197                 /* hash non-fd knotes here too */ 
1198                 if (fdp
->fd_knhashmask 
!= 0) { 
1201                         list 
= &fdp
->fd_knhash
[ 
1202                             KN_HASH((u_long
)kev
->ident
, fdp
->fd_knhashmask
)]; 
1203                         SLIST_FOREACH(kn
, list
, kn_link
) 
1204                                 if (kev
->ident 
== kn
->kn_id 
&& 
1206                                     kev
->filter 
== kn
->kn_filter
) 
1212          * kn now contains the matching knote, or NULL if no match 
1215                 if ((kev
->flags 
& (EV_ADD
|EV_DELETE
)) == EV_ADD
) { 
1224                         kn
->kn_tq 
= &kq
->kq_head
; 
1226                         kn
->kn_sfflags 
= kev
->fflags
; 
1227                         kn
->kn_sdata 
= kev
->data
; 
1230                         kn
->kn_kevent 
= *kev
; 
1231                         kn
->kn_inuse 
= 1;  /* for f_attach() */ 
1234                         /* before anyone can find it */ 
1235                         if (kev
->flags 
& EV_DISABLE
) 
1236                                 kn
->kn_status 
|= KN_DISABLED
; 
1238                         error 
= knote_fdpattach(kn
, fdp
, p
); 
1247                          * apply reference count to knote structure, and 
1248                          * do not release it at the end of this routine. 
1253                          * If the attach fails here, we can drop it knowing 
1254                          * that nobody else has a reference to the knote. 
1256                         if ((error 
= fops
->f_attach(kn
)) != 0) { 
1266                 /* existing knote - get kqueue lock */ 
1270                 if (kev
->flags 
& EV_DELETE
) { 
1272                         kn
->kn_status 
|= KN_DISABLED
; 
1273                         if (kqlock2knotedrop(kq
, kn
)) { 
1274                                 kn
->kn_fop
->f_detach(kn
); 
1280                 /* update status flags for existing knote */ 
1281                 if (kev
->flags 
& EV_DISABLE
) { 
1283                         kn
->kn_status 
|= KN_DISABLED
; 
1284                 } else if (kev
->flags 
& EV_ENABLE
) { 
1285                         kn
->kn_status 
&= ~KN_DISABLED
; 
1286                         if (kn
->kn_status 
& KN_ACTIVE
) 
1291                  * If somebody is in the middle of dropping this 
1292                  * knote - go find/insert a new one.  But we have 
1293                  * wait for this one to go away first. 
1295                 if (!kqlock2knoteusewait(kq
, kn
)) 
1296                         /* kqueue unlocked */ 
1300                  * The user may change some filter values after the 
1301                  * initial EV_ADD, but doing so will not reset any  
1302                  * filter which have already been triggered. 
1304                 kn
->kn_sfflags 
= kev
->fflags
; 
1305                 kn
->kn_sdata 
= kev
->data
; 
1306                 kn
->kn_kevent
.udata 
= kev
->udata
; 
1309         /* still have use ref on knote */ 
1310         if (kn
->kn_fop
->f_event(kn
, 0)) { 
1311                 if (knoteuse2kqlock(kq
, kn
)) 
1320                 fp_drop(p
, kev
->ident
, fp
, 0); 
1325  * kevent_process - process the triggered events in a kqueue 
1327  *      Walk the queued knotes and validate that they are 
1328  *      really still triggered events by calling the filter 
1329  *      routines (if necessary).  Hold a use reference on 
1330  *      the knote to avoid it being detached. For each event 
1331  *      that is still considered triggered, invoke the 
1332  *      callback routine provided. 
1334  *      caller holds a reference on the kqueue. 
1335  *      kqueue locked on entry and exit - but may be dropped 
1339 kevent_process(struct kqueue 
*kq
, 
1340                kevent_callback_t callback
, 
1351         if (kq
->kq_count 
== 0) { 
1356         /* if someone else is processing the queue, wait */ 
1357         if (!TAILQ_EMPTY(&kq
->kq_inprocess
)) { 
1358                 assert_wait(&kq
->kq_inprocess
, THREAD_UNINT
); 
1359                 kq
->kq_state 
|= KQ_PROCWAIT
; 
1361                 thread_block(THREAD_CONTINUE_NULL
); 
1368         while (error 
== 0 && 
1369                (kn 
= TAILQ_FIRST(&kq
->kq_head
)) != NULL
) { 
1372                  * move knote to the processed queue. 
1373                  * this is also protected by the kq lock. 
1375                 assert(kn
->kn_tq 
== &kq
->kq_head
); 
1376                 TAILQ_REMOVE(&kq
->kq_head
, kn
, kn_tqe
); 
1377                 kn
->kn_tq 
= &kq
->kq_inprocess
; 
1378                 TAILQ_INSERT_TAIL(&kq
->kq_inprocess
, kn
, kn_tqe
); 
1381                  * Non-EV_ONESHOT events must be re-validated. 
1383                  * Convert our lock to a use-count and call the event's 
1384                  * filter routine to update. 
1386                  * If the event is dropping (or no longer valid), we 
1387                  * already have it off the active queue, so just 
1388                  * finish the job of deactivating it. 
1390                 if ((kn
->kn_flags 
& EV_ONESHOT
) == 0) { 
1393                         if (kqlock2knoteuse(kq
, kn
)) { 
1395                                 /* call the filter with just a ref */ 
1396                                 result 
= kn
->kn_fop
->f_event(kn
, 0); 
1398                                 if (!knoteuse2kqlock(kq
, kn
) || result 
== 0) { 
1399                                         knote_deactivate(kn
); 
1403                                 knote_deactivate(kn
); 
1409                  * Got a valid triggered knote with the kqueue 
1410                  * still locked.  Snapshot the data, and determine 
1411                  * how to dispatch the knote for future events. 
1413                 kev 
= kn
->kn_kevent
; 
1415                 /* now what happens to it? */ 
1416                 if (kn
->kn_flags 
& EV_ONESHOT
) { 
1417                         knote_deactivate(kn
); 
1418                         if (kqlock2knotedrop(kq
, kn
)) { 
1419                                 kn
->kn_fop
->f_detach(kn
); 
1422                 } else if (kn
->kn_flags 
& EV_CLEAR
) { 
1423                         knote_deactivate(kn
); 
1429                          * leave on in-process queue.  We'll 
1430                          * move all the remaining ones back 
1431                          * the kq queue and wakeup any 
1432                          * waiters when we are done. 
1437                 /* callback to handle each event as we find it */ 
1438                 error 
= (callback
)(kq
, &kev
, data
); 
1445          * With the kqueue still locked, move any knotes 
1446          * remaining on the in-process queue back to the 
1447          * kq's queue and wake up any waiters. 
1449         while ((kn 
= TAILQ_FIRST(&kq
->kq_inprocess
)) != NULL
) { 
1450                 assert(kn
->kn_tq 
== &kq
->kq_inprocess
); 
1451                 TAILQ_REMOVE(&kq
->kq_inprocess
, kn
, kn_tqe
); 
1452                 kn
->kn_tq 
= &kq
->kq_head
; 
1453                 TAILQ_INSERT_TAIL(&kq
->kq_head
, kn
, kn_tqe
); 
1455         if (kq
->kq_state 
& KQ_PROCWAIT
) { 
1456                 kq
->kq_state 
&= ~KQ_PROCWAIT
; 
1457                 thread_wakeup(&kq
->kq_inprocess
); 
1466 kevent_scan_continue(void *data
, wait_result_t wait_result
) 
1468         uthread_t ut 
= (uthread_t
)get_bsdthread_info(current_thread()); 
1469         struct _kevent_scan 
* cont_args 
= &ut
->uu_state
.ss_kevent_scan
; 
1470         struct kqueue 
*kq 
= (struct kqueue 
*)data
; 
1474         /* convert the (previous) wait_result to a proper error */ 
1475         switch (wait_result
) { 
1476         case THREAD_AWAKENED
: 
1478                 error 
= kevent_process(kq
, cont_args
->call
, cont_args
, &count
, current_proc()); 
1479                 if (error 
== 0 && count 
== 0) { 
1480                         assert_wait_deadline(kq
, THREAD_ABORTSAFE
, cont_args
->deadline
); 
1481                         kq
->kq_state 
|= KQ_SLEEP
; 
1483                         thread_block_parameter(kevent_scan_continue
, kq
); 
1488         case THREAD_TIMED_OUT
: 
1489                 error 
= EWOULDBLOCK
;  
1491         case THREAD_INTERRUPTED
: 
1495                 panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result
); 
1499         /* call the continuation with the results */ 
1500         assert(cont_args
->cont 
!= NULL
); 
1501         (cont_args
->cont
)(kq
, cont_args
->data
, error
); 
1506  * kevent_scan - scan and wait for events in a kqueue 
1508  *      Process the triggered events in a kqueue. 
1510  *      If there are no events triggered arrange to 
1511  *      wait for them. If the caller provided a 
1512  *      continuation routine, then kevent_scan will 
1515  *      The callback routine must be valid. 
1516  *      The caller must hold a use-count reference on the kq. 
1520 kevent_scan(struct kqueue 
*kq
,  
1521             kevent_callback_t callback
, 
1522             kevent_continue_t continuation
, 
1524             struct timeval 
*atvp
, 
1527         thread_continue_t cont 
= THREAD_CONTINUE_NULL
; 
1532         assert(callback 
!= NULL
); 
1536                 wait_result_t wait_result
; 
1540                  * Make a pass through the kq to find events already 
1544                 error 
= kevent_process(kq
, callback
, data
, &count
, p
); 
1546                         break; /* lock still held */ 
1548                 /* looks like we have to consider blocking */ 
1551                         /* convert the timeout to a deadline once */ 
1552                         if (atvp
->tv_sec 
|| atvp
->tv_usec
) { 
1553                                 uint32_t seconds
, nanoseconds
; 
1556                                 clock_get_uptime(&now
); 
1557                                 nanoseconds_to_absolutetime((uint64_t)atvp
->tv_sec 
* NSEC_PER_SEC 
+ 
1558                                                             atvp
->tv_usec 
* NSEC_PER_USEC
, 
1560                                 if (now 
>= deadline
) { 
1561                                         /* non-blocking call */ 
1562                                         error 
= EWOULDBLOCK
; 
1563                                         break; /* lock still held */ 
1566                                 clock_absolutetime_interval_to_deadline(deadline
, &deadline
); 
1568                                 deadline 
= 0;   /* block forever */ 
1572                                 uthread_t ut 
= (uthread_t
)get_bsdthread_info(current_thread()); 
1573                                 struct _kevent_scan 
*cont_args 
= &ut
->uu_state
.ss_kevent_scan
; 
1575                                 cont_args
->call 
= callback
; 
1576                                 cont_args
->cont 
= continuation
; 
1577                                 cont_args
->deadline 
= deadline
; 
1578                                 cont_args
->data 
= data
; 
1579                                 cont 
= kevent_scan_continue
; 
1583                 /* go ahead and wait */ 
1584                 assert_wait_deadline(kq
, THREAD_ABORTSAFE
, deadline
); 
1585                 kq
->kq_state 
|= KQ_SLEEP
; 
1587                 wait_result 
= thread_block_parameter(cont
, kq
); 
1588                 /* NOTREACHED if (continuation != NULL) */ 
1590                 switch (wait_result
) { 
1591                 case THREAD_AWAKENED
: 
1593                 case THREAD_TIMED_OUT
: 
1595                 case THREAD_INTERRUPTED
: 
1598                         panic("kevent_scan - bad wait_result (%d)", 
1610  * This could be expanded to call kqueue_scan, if desired. 
1614 kqueue_read(__unused 
struct fileproc 
*fp
,  
1615                         __unused 
struct uio 
*uio
,  
1616                         __unused kauth_cred_t cred
, 
1618                         __unused 
struct proc 
*p
) 
1625 kqueue_write(__unused 
struct fileproc 
*fp
,  
1626                          __unused 
struct uio 
*uio
,  
1627                          __unused kauth_cred_t cred
, 
1629                          __unused 
struct proc 
*p
) 
1636 kqueue_ioctl(__unused 
struct fileproc 
*fp
,  
1637                          __unused u_long com
,  
1638                          __unused caddr_t data
,  
1639                          __unused 
struct proc 
*p
) 
1646 kqueue_select(struct fileproc 
*fp
, int which
, void *wql
, struct proc 
*p
) 
1648         struct kqueue 
*kq 
= (struct kqueue 
*)fp
->f_data
; 
1651         if (which 
== FREAD
) { 
1656                         selrecord(p
, &kq
->kq_sel
, wql
); 
1657                         kq
->kq_state 
|= KQ_SEL
; 
1669 kqueue_close(struct fileglob 
*fg
, struct proc 
*p
) 
1671         struct kqueue 
*kq 
= (struct kqueue 
*)fg
->fg_data
; 
1673         kqueue_dealloc(kq
, p
); 
1680  * The callers has taken a use-count reference on this kqueue and will donate it 
1681  * to the kqueue we are being added to.  This keeps the kqueue from closing until 
1682  * that relationship is torn down. 
1685 kqueue_kqfilter(__unused 
struct fileproc 
*fp
, struct knote 
*kn
, __unused 
struct proc 
*p
) 
1687         struct kqueue 
*kq 
= (struct kqueue 
*)kn
->kn_fp
->f_data
; 
1689         if (kn
->kn_filter 
!= EVFILT_READ
) 
1692         kn
->kn_fop 
= &kqread_filtops
; 
1694         KNOTE_ATTACH(&kq
->kq_sel
.si_note
, kn
); 
1701 kqueue_stat(struct fileproc 
*fp
, struct stat 
*st
, __unused 
struct proc 
*p
) 
1703         struct kqueue 
*kq 
= (struct kqueue 
*)fp
->f_data
; 
1705         bzero((void *)st
, sizeof(*st
)); 
1706         st
->st_size 
= kq
->kq_count
; 
1707         st
->st_blksize 
= sizeof(struct kevent
); 
1708         st
->st_mode 
= S_IFIFO
; 
1713  * Called with the kqueue locked 
1716 kqueue_wakeup(struct kqueue 
*kq
) 
1719         if (kq
->kq_state 
& KQ_SLEEP
) { 
1720                 kq
->kq_state 
&= ~KQ_SLEEP
; 
1723         if (kq
->kq_state 
& KQ_SEL
) { 
1724                 kq
->kq_state 
&= ~KQ_SEL
; 
1725                 selwakeup(&kq
->kq_sel
); 
1727         KNOTE(&kq
->kq_sel
.si_note
, 0); 
1731 klist_init(struct klist 
*list
) 
1738  * Query/Post each knote in the object's list 
1740  *      The object lock protects the list. It is assumed 
1741  *      that the filter/event routine for the object can 
1742  *      determine that the object is already locked (via 
1743  *      the hind) and not deadlock itself. 
1745  *      The object lock should also hold off pending 
1746  *      detach/drop operations.  But we'll prevent it here 
1747  *      too - just in case. 
1750 knote(struct klist 
*list
, long hint
) 
1754         SLIST_FOREACH(kn
, list
, kn_selnext
) { 
1755                 struct kqueue 
*kq 
= kn
->kn_kq
; 
1758                 if (kqlock2knoteuse(kq
, kn
)) { 
1761                         /* call the event with only a use count */ 
1762                         result 
= kn
->kn_fop
->f_event(kn
, hint
); 
1764                         /* if its not going away and triggered */ 
1765                         if (knoteuse2kqlock(kq
, kn
) && result
) 
1767                         /* lock held again */ 
1774  * attach a knote to the specified list.  Return true if this is the first entry. 
1775  * The list is protected by whatever lock the object it is associated with uses. 
1778 knote_attach(struct klist 
*list
, struct knote 
*kn
) 
1780         int ret 
= SLIST_EMPTY(list
); 
1781         SLIST_INSERT_HEAD(list
, kn
, kn_selnext
); 
1786  * detach a knote from the specified list.  Return true if that was the last entry. 
1787  * The list is protected by whatever lock the object it is associated with uses. 
1790 knote_detach(struct klist 
*list
, struct knote 
*kn
) 
1792         SLIST_REMOVE(list
, kn
, knote
, kn_selnext
); 
1793         return SLIST_EMPTY(list
); 
1797  * remove all knotes referencing a specified fd 
1799  * Essentially an inlined knote_remove & knote_drop 
1800  * when we know for sure that the thing is a file 
1802  * Entered with the proc_fd lock already held. 
1803  * It returns the same way, but may drop it temporarily. 
1806 knote_fdclose(struct proc 
*p
, int fd
) 
1808         struct filedesc 
*fdp 
= p
->p_fd
; 
1812         list 
= &fdp
->fd_knlist
[fd
]; 
1813         while ((kn 
= SLIST_FIRST(list
)) != NULL
) { 
1814                 struct kqueue 
*kq 
= kn
->kn_kq
; 
1820                  * Convert the lock to a drop ref. 
1821                  * If we get it, go ahead and drop it. 
1822                  * Otherwise, we waited for it to 
1823                  * be dropped by the other guy, so 
1824                  * it is safe to move on in the list. 
1826                 if (kqlock2knotedrop(kq
, kn
)) { 
1827                         kn
->kn_fop
->f_detach(kn
); 
1833                 /* the fd tables may have changed - start over */ 
1834                 list 
= &fdp
->fd_knlist
[fd
]; 
1838 /* proc_fdlock held on entry (and exit) */ 
1840 knote_fdpattach(struct knote 
*kn
, struct filedesc 
*fdp
, __unused 
struct proc 
*p
) 
1842         struct klist 
*list 
= NULL
; 
1844         if (! kn
->kn_fop
->f_isfd
) { 
1845                 if (fdp
->fd_knhashmask 
== 0) 
1846                         fdp
->fd_knhash 
= hashinit(KN_HASHSIZE
, M_KQUEUE
, 
1847                             &fdp
->fd_knhashmask
); 
1848                 list 
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)]; 
1850                 if ((u_int
)fdp
->fd_knlistsize 
<= kn
->kn_id
) { 
1853                         /* have to grow the fd_knlist */ 
1854                         size 
= fdp
->fd_knlistsize
; 
1855                         while (size 
<= kn
->kn_id
) 
1857                         MALLOC(list
, struct klist 
*, 
1858                                size 
* sizeof(struct klist 
*), M_KQUEUE
, M_WAITOK
); 
1862                         bcopy((caddr_t
)fdp
->fd_knlist
, (caddr_t
)list
, 
1863                               fdp
->fd_knlistsize 
* sizeof(struct klist 
*)); 
1864                         bzero((caddr_t
)list 
+ 
1865                               fdp
->fd_knlistsize 
* sizeof(struct klist 
*), 
1866                               (size 
- fdp
->fd_knlistsize
) * sizeof(struct klist 
*)); 
1867                         FREE(fdp
->fd_knlist
, M_KQUEUE
); 
1868                         fdp
->fd_knlist 
= list
; 
1869                         fdp
->fd_knlistsize 
= size
; 
1871                 list 
= &fdp
->fd_knlist
[kn
->kn_id
]; 
1873         SLIST_INSERT_HEAD(list
, kn
, kn_link
); 
1880  * should be called at spl == 0, since we don't want to hold spl 
1881  * while calling fdrop and free. 
1884 knote_drop(struct knote 
*kn
, struct proc 
*p
) 
1886         struct filedesc 
*fdp 
= p
->p_fd
; 
1887         struct kqueue 
*kq 
= kn
->kn_kq
; 
1891         if (kn
->kn_fop
->f_isfd
) 
1892                 list 
= &fdp
->fd_knlist
[kn
->kn_id
]; 
1894                 list 
= &fdp
->fd_knhash
[KN_HASH(kn
->kn_id
, fdp
->fd_knhashmask
)]; 
1896         SLIST_REMOVE(list
, kn
, knote
, kn_link
); 
1899         if (kn
->kn_status 
& KN_DROPWAIT
) 
1900                 thread_wakeup(&kn
->kn_status
); 
1904         if (kn
->kn_fop
->f_isfd
) 
1905                 fp_drop(p
, kn
->kn_id
, kn
->kn_fp
, 0); 
1910 /* called with kqueue lock held */ 
1912 knote_activate(struct knote 
*kn
) 
1914         struct kqueue 
*kq 
= kn
->kn_kq
; 
1916         kn
->kn_status 
|= KN_ACTIVE
; 
1921 /* called with kqueue lock held */ 
1923 knote_deactivate(struct knote 
*kn
) 
1925         kn
->kn_status 
&= ~KN_ACTIVE
; 
1929 /* called with kqueue lock held */ 
1931 knote_enqueue(struct knote 
*kn
) 
1933         struct kqueue 
*kq 
= kn
->kn_kq
; 
1935         if ((kn
->kn_status 
& (KN_QUEUED 
| KN_DISABLED
)) == 0) { 
1936                 struct kqtailq 
*tq 
= kn
->kn_tq
; 
1938                 TAILQ_INSERT_TAIL(tq
, kn
, kn_tqe
);  
1939                 kn
->kn_status 
|= KN_QUEUED
; 
1944 /* called with kqueue lock held */ 
1946 knote_dequeue(struct knote 
*kn
) 
1948         struct kqueue 
*kq 
= kn
->kn_kq
; 
1950         assert((kn
->kn_status 
& KN_DISABLED
) == 0); 
1951         if ((kn
->kn_status 
& KN_QUEUED
) == KN_QUEUED
) { 
1952                 struct kqtailq 
*tq 
= kn
->kn_tq
; 
1954                 TAILQ_REMOVE(tq
, kn
, kn_tqe
);  
1955                 kn
->kn_tq 
= &kq
->kq_head
; 
1956                 kn
->kn_status 
&= ~KN_QUEUED
; 
1964         knote_zone 
= zinit(sizeof(struct knote
), 8192*sizeof(struct knote
), 8192, "knote zone"); 
1966         /* allocate kq lock group attribute and group */ 
1967         kq_lck_grp_attr
= lck_grp_attr_alloc_init(); 
1969         kq_lck_grp 
= lck_grp_alloc_init("kqueue",  kq_lck_grp_attr
); 
1971         /* Allocate kq lock attribute */ 
1972         kq_lck_attr 
= lck_attr_alloc_init(); 
1974         /* Initialize the timer filter lock */ 
1975         lck_mtx_init(&_filt_timerlock
, kq_lck_grp
, kq_lck_attr
); 
1977 SYSINIT(knote
, SI_SUB_PSEUDO
, SI_ORDER_ANY
, knote_init
, NULL
) 
1979 static struct knote 
* 
1982         return ((struct knote 
*)zalloc(knote_zone
)); 
1986 knote_free(struct knote 
*kn
) 
1988         zfree(knote_zone
, kn
); 
1991 #include <sys/param.h> 
1992 #include <sys/socket.h> 
1993 #include <sys/protosw.h> 
1994 #include <sys/domain.h> 
1995 #include <sys/mbuf.h> 
1996 #include <sys/kern_event.h> 
1997 #include <sys/malloc.h> 
1998 #include <sys/sys_domain.h> 
1999 #include <sys/syslog.h> 
2002 static int kev_attach(struct socket 
*so
, int proto
, struct proc 
*p
); 
2003 static int kev_detach(struct socket 
*so
); 
2004 static int kev_control(struct socket 
*so
, u_long cmd
, caddr_t data
, struct ifnet 
*ifp
, struct proc 
*p
); 
2006 struct pr_usrreqs event_usrreqs 
= { 
2007      pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
, 
2008      pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
, 
2009      pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
, 
2010      pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
, 
2011      pru_sosend_notsupp
, soreceive
, pru_sopoll_notsupp
 
2014 struct protosw eventsw
[] = { 
2016           SOCK_RAW
,             &systemdomain
,  SYSPROTO_EVENT
,         PR_ATOMIC
, 
2032 struct kern_event_head kern_event_head
; 
2034 static u_long static_event_id 
= 0; 
2035 struct domain 
*sysdom 
= &systemdomain
; 
2037 static lck_grp_t                
*evt_mtx_grp
; 
2038 static lck_attr_t               
*evt_mtx_attr
; 
2039 static lck_grp_attr_t   
*evt_mtx_grp_attr
; 
2040 lck_mtx_t                               
*evt_mutex
; 
2042  * Install the protosw's for the NKE manager.  Invoked at 
2043  *  extension load time 
2046 kern_event_init(void) 
2050     if ((retval 
= net_add_proto(eventsw
, &systemdomain
)) != 0) { 
2051             log(LOG_WARNING
, "Can't install kernel events protocol (%d)\n", retval
); 
2056          * allocate lock group attribute and group for kern event  
2058         evt_mtx_grp_attr 
= lck_grp_attr_alloc_init(); 
2060         evt_mtx_grp 
= lck_grp_alloc_init("eventlist", evt_mtx_grp_attr
); 
2063          * allocate the lock attribute for mutexes 
2065         evt_mtx_attr 
= lck_attr_alloc_init(); 
2066         evt_mutex 
= lck_mtx_alloc_init(evt_mtx_grp
, evt_mtx_attr
); 
2067         if (evt_mutex 
== NULL
) 
2070     return(KERN_SUCCESS
); 
2074 kev_attach(struct socket 
*so
, __unused 
int proto
, __unused 
struct proc 
*p
) 
2077      struct kern_event_pcb  
*ev_pcb
; 
2079      error 
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
); 
2083      MALLOC(ev_pcb
, struct kern_event_pcb 
*, sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
); 
2087      ev_pcb
->ev_socket 
= so
; 
2088      ev_pcb
->vendor_code_filter 
= 0xffffffff; 
2090      so
->so_pcb 
= (caddr_t
) ev_pcb
; 
2091          lck_mtx_lock(evt_mutex
); 
2092      LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
); 
2093          lck_mtx_unlock(evt_mutex
); 
2100 kev_detach(struct socket 
*so
) 
2102      struct kern_event_pcb 
*ev_pcb 
= (struct kern_event_pcb 
*) so
->so_pcb
; 
2105                 lck_mtx_lock(evt_mutex
); 
2106                 LIST_REMOVE(ev_pcb
, ev_link
); 
2107                 lck_mtx_unlock(evt_mutex
); 
2108                 FREE(ev_pcb
, M_PCB
); 
2110                 so
->so_flags 
|= SOF_PCBCLEARING
; 
2117  * For now, kev_vender_code and mbuf_tags use the same 
2120 extern errno_t 
mbuf_tag_id_find_internal(const char *string
, u_long 
*out_id
, 
2123 errno_t 
kev_vendor_code_find( 
2125         u_long          
*out_vender_code
) 
2127         if (strlen(string
) >= KEV_VENDOR_CODE_MAX_STR_LEN
) { 
2130         return mbuf_tag_id_find_internal(string
, out_vender_code
, 1); 
2133 extern void mbuf_tag_id_first_last(u_long 
*first
, u_long 
*last
); 
2135 errno_t  
kev_msg_post(struct kev_msg 
*event_msg
) 
2137         u_long  min_vendor
, max_vendor
; 
2139         mbuf_tag_id_first_last(&min_vendor
, &max_vendor
); 
2141         if (event_msg 
== NULL
) 
2144         /* Limit third parties to posting events for registered vendor codes only */ 
2145         if (event_msg
->vendor_code 
< min_vendor 
|| 
2146                 event_msg
->vendor_code 
> max_vendor
) 
2151         return kev_post_msg(event_msg
); 
2155 int  kev_post_msg(struct kev_msg 
*event_msg
) 
2157      struct mbuf 
*m
, *m2
; 
2158      struct kern_event_pcb  
*ev_pcb
; 
2159      struct kern_event_msg  
*ev
; 
2161      unsigned long     total_size
; 
2164         /* Verify the message is small enough to fit in one mbuf w/o cluster */ 
2165         total_size 
= KEV_MSG_HEADER_SIZE
; 
2167         for (i 
= 0; i 
< 5; i
++) { 
2168                 if (event_msg
->dv
[i
].data_length 
== 0) 
2170                 total_size 
+= event_msg
->dv
[i
].data_length
; 
2173         if (total_size 
> MLEN
) { 
2177      m 
= m_get(M_DONTWAIT
, MT_DATA
); 
2181      ev 
= mtod(m
, struct kern_event_msg 
*); 
2182      total_size 
= KEV_MSG_HEADER_SIZE
; 
2184      tmp 
= (char *) &ev
->event_data
[0]; 
2185      for (i 
= 0; i 
< 5; i
++) { 
2186           if (event_msg
->dv
[i
].data_length 
== 0) 
2189           total_size 
+= event_msg
->dv
[i
].data_length
; 
2190           bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,  
2191                 event_msg
->dv
[i
].data_length
); 
2192           tmp 
+= event_msg
->dv
[i
].data_length
; 
2195      ev
->id 
= ++static_event_id
; 
2196      ev
->total_size   
= total_size
; 
2197      ev
->vendor_code  
= event_msg
->vendor_code
; 
2198      ev
->kev_class    
= event_msg
->kev_class
; 
2199      ev
->kev_subclass 
= event_msg
->kev_subclass
; 
2200      ev
->event_code   
= event_msg
->event_code
; 
2202      m
->m_len 
= total_size
; 
2203      lck_mtx_lock(evt_mutex
); 
2204      for (ev_pcb 
= LIST_FIRST(&kern_event_head
);  
2206           ev_pcb 
= LIST_NEXT(ev_pcb
, ev_link
)) { 
2208           if (ev_pcb
->vendor_code_filter 
!= KEV_ANY_VENDOR
) { 
2209                if (ev_pcb
->vendor_code_filter 
!= ev
->vendor_code
) 
2212                if (ev_pcb
->class_filter 
!= KEV_ANY_CLASS
) { 
2213                     if (ev_pcb
->class_filter 
!= ev
->kev_class
) 
2216                     if ((ev_pcb
->subclass_filter 
!= KEV_ANY_SUBCLASS
) && 
2217                         (ev_pcb
->subclass_filter 
!= ev
->kev_subclass
)) 
2222           m2 
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
); 
2225                    lck_mtx_unlock(evt_mutex
); 
2228           socket_lock(ev_pcb
->ev_socket
, 1); 
2229           if (sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
)) 
2230                   sorwakeup(ev_pcb
->ev_socket
); 
2231           socket_unlock(ev_pcb
->ev_socket
, 1); 
2235      lck_mtx_unlock(evt_mutex
); 
2240 kev_control(struct socket 
*so
,  
2243                         __unused 
struct ifnet 
*ifp
,  
2244                         __unused 
struct proc 
*p
) 
2246         struct kev_request 
*kev_req 
= (struct kev_request 
*) data
; 
2247         struct kern_event_pcb  
*ev_pcb
; 
2248         struct kev_vendor_code 
*kev_vendor
; 
2249         u_long  
*id_value 
= (u_long 
*) data
; 
2255                         *id_value 
= static_event_id
; 
2259                         ev_pcb 
= (struct kern_event_pcb 
*) so
->so_pcb
; 
2260                         ev_pcb
->vendor_code_filter 
= kev_req
->vendor_code
; 
2261                         ev_pcb
->class_filter     
= kev_req
->kev_class
; 
2262                         ev_pcb
->subclass_filter  
= kev_req
->kev_subclass
; 
2266                         ev_pcb 
= (struct kern_event_pcb 
*) so
->so_pcb
; 
2267                         kev_req
->vendor_code 
= ev_pcb
->vendor_code_filter
; 
2268                         kev_req
->kev_class   
= ev_pcb
->class_filter
; 
2269                         kev_req
->kev_subclass 
= ev_pcb
->subclass_filter
; 
2272                 case SIOCGKEVVENDOR
: 
2273                         kev_vendor 
= (struct kev_vendor_code
*)data
; 
2275                         /* Make sure string is NULL terminated */ 
2276                         kev_vendor
->vendor_string
[KEV_VENDOR_CODE_MAX_STR_LEN
-1] = 0; 
2278                         return mbuf_tag_id_find_internal(kev_vendor
->vendor_string
, 
2279                                                                                          &kev_vendor
->vendor_code
, 0); 
2291 fill_kqueueinfo(struct kqueue 
*kq
, struct kqueue_info 
* kinfo
) 
2295         /* No need for the funnel as fd is kept alive */ 
2297         st 
= &kinfo
->kq_stat
; 
2299         st
->st_size 
= kq
->kq_count
; 
2300         st
->st_blksize 
= sizeof(struct kevent
); 
2301         st
->st_mode 
= S_IFIFO
; 
2302         if (kq
->kq_state 
& KQ_SEL
) 
2303                 kinfo
->kq_state 
|=  PROC_KQUEUE_SELECT
; 
2304         if (kq
->kq_state 
& KQ_SLEEP
) 
2305                 kinfo
->kq_state 
|= PROC_KQUEUE_SLEEP
;