]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 A |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | * |
28 | */ | |
55e303ae A |
29 | /*- |
30 | * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org> | |
31 | * All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
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. | |
41 | * | |
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 | |
52 | * SUCH DAMAGE. | |
53 | */ | |
1c79356b A |
54 | /* |
55 | * @(#)kern_event.c 1.0 (3/31/2000) | |
56 | */ | |
91447636 | 57 | #include <stdint.h> |
1c79356b | 58 | |
55e303ae A |
59 | #include <sys/param.h> |
60 | #include <sys/systm.h> | |
61 | #include <sys/filedesc.h> | |
62 | #include <sys/kernel.h> | |
91447636 A |
63 | #include <sys/proc_internal.h> |
64 | #include <sys/kauth.h> | |
55e303ae A |
65 | #include <sys/malloc.h> |
66 | #include <sys/unistd.h> | |
91447636 | 67 | #include <sys/file_internal.h> |
55e303ae A |
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> | |
76 | #include <sys/stat.h> | |
77 | #include <sys/sysctl.h> | |
78 | #include <sys/uio.h> | |
91447636 A |
79 | #include <sys/sysproto.h> |
80 | #include <sys/user.h> | |
81 | #include <string.h> | |
82 | ||
83 | #include <kern/lock.h> | |
84 | #include <kern/clock.h> | |
85 | #include <kern/thread_call.h> | |
86 | #include <kern/sched_prim.h> | |
55e303ae | 87 | #include <kern/zalloc.h> |
91447636 A |
88 | #include <kern/assert.h> |
89 | ||
90 | #include <libkern/libkern.h> | |
91 | ||
92 | extern void unix_syscall_return(int); | |
55e303ae A |
93 | |
94 | MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system"); | |
95 | ||
91447636 A |
96 | static inline void kqlock(struct kqueue *kq); |
97 | static inline void kqunlock(struct kqueue *kq); | |
98 | ||
99 | static int kqlock2knoteuse(struct kqueue *kq, struct knote *kn); | |
100 | static int kqlock2knoteusewait(struct kqueue *kq, struct knote *kn); | |
101 | static int kqlock2knotedrop(struct kqueue *kq, struct knote *kn); | |
102 | static int knoteuse2kqlock(struct kqueue *kq, struct knote *kn); | |
55e303ae | 103 | |
91447636 A |
104 | static void kqueue_wakeup(struct kqueue *kq); |
105 | static int kqueue_read(struct fileproc *fp, struct uio *uio, | |
106 | kauth_cred_t cred, int flags, struct proc *p); | |
107 | static int kqueue_write(struct fileproc *fp, struct uio *uio, | |
108 | kauth_cred_t cred, int flags, struct proc *p); | |
109 | static int kqueue_ioctl(struct fileproc *fp, u_long com, caddr_t data, | |
110 | struct proc *p); | |
111 | static int kqueue_select(struct fileproc *fp, int which, void *wql, | |
112 | struct proc *p); | |
113 | static int kqueue_close(struct fileglob *fp, struct proc *p); | |
114 | static int kqueue_kqfilter(struct fileproc *fp, struct knote *kn, struct proc *p); | |
115 | extern int kqueue_stat(struct fileproc *fp, struct stat *st, struct proc *p); | |
55e303ae A |
116 | |
117 | static struct fileops kqueueops = { | |
118 | kqueue_read, | |
119 | kqueue_write, | |
120 | kqueue_ioctl, | |
121 | kqueue_select, | |
122 | kqueue_close, | |
91447636 A |
123 | kqueue_kqfilter, |
124 | 0 | |
55e303ae A |
125 | }; |
126 | ||
91447636 A |
127 | static int kevent_copyin(user_addr_t *addrp, struct kevent *kevp, struct proc *p); |
128 | static int kevent_copyout(struct kevent *kevp, user_addr_t *addrp, struct proc *p); | |
129 | ||
130 | static int kevent_callback(struct kqueue *kq, struct kevent *kevp, void *data); | |
131 | static void kevent_continue(struct kqueue *kq, void *data, int error); | |
132 | static void kevent_scan_continue(void *contp, wait_result_t wait_result); | |
133 | static int kevent_process(struct kqueue *kq, kevent_callback_t callback, | |
134 | void *data, int *countp, struct proc *p); | |
135 | static void knote_put(struct knote *kn); | |
136 | static int knote_fdpattach(struct knote *kn, struct filedesc *fdp, struct proc *p); | |
55e303ae | 137 | static void knote_drop(struct knote *kn, struct proc *p); |
91447636 A |
138 | static void knote_activate(struct knote *kn); |
139 | static void knote_deactivate(struct knote *kn); | |
55e303ae A |
140 | static void knote_enqueue(struct knote *kn); |
141 | static void knote_dequeue(struct knote *kn); | |
142 | static struct knote *knote_alloc(void); | |
143 | static void knote_free(struct knote *kn); | |
91447636 | 144 | extern void knote_init(void); |
55e303ae A |
145 | |
146 | static int filt_fileattach(struct knote *kn); | |
147 | static struct filterops file_filtops = | |
148 | { 1, filt_fileattach, NULL, NULL }; | |
149 | ||
150 | static void filt_kqdetach(struct knote *kn); | |
151 | static int filt_kqueue(struct knote *kn, long hint); | |
152 | static struct filterops kqread_filtops = | |
153 | { 1, NULL, filt_kqdetach, filt_kqueue }; | |
154 | ||
155 | /* | |
91447636 | 156 | * placeholder for not-yet-implemented filters |
55e303ae A |
157 | */ |
158 | static int filt_badattach(struct knote *kn); | |
159 | static struct filterops bad_filtops = | |
160 | { 0, filt_badattach, 0 , 0 }; | |
161 | ||
162 | static int filt_procattach(struct knote *kn); | |
163 | static void filt_procdetach(struct knote *kn); | |
164 | static int filt_proc(struct knote *kn, long hint); | |
165 | ||
166 | static struct filterops proc_filtops = | |
167 | { 0, filt_procattach, filt_procdetach, filt_proc }; | |
168 | ||
169 | extern struct filterops fs_filtops; | |
170 | ||
171 | extern struct filterops sig_filtops; | |
172 | ||
91447636 A |
173 | |
174 | /* Timer filter */ | |
175 | static int filt_timercompute(struct knote *kn, uint64_t *abs_time); | |
176 | static void filt_timerexpire(void *knx, void *param1); | |
55e303ae A |
177 | static int filt_timerattach(struct knote *kn); |
178 | static void filt_timerdetach(struct knote *kn); | |
179 | static int filt_timer(struct knote *kn, long hint); | |
180 | ||
181 | static struct filterops timer_filtops = | |
182 | { 0, filt_timerattach, filt_timerdetach, filt_timer }; | |
183 | ||
91447636 A |
184 | /* to avoid arming timers that fire quicker than we can handle */ |
185 | static uint64_t filt_timerfloor = 0; | |
55e303ae | 186 | |
91447636 A |
187 | static lck_mtx_t _filt_timerlock; |
188 | static void filt_timerlock(void); | |
189 | static void filt_timerunlock(void); | |
55e303ae | 190 | |
91447636 A |
191 | /* |
192 | * Sentinel marker for a thread scanning through the list of | |
193 | * active knotes. | |
194 | */ | |
195 | static struct filterops threadmarker_filtops = | |
196 | { 0, filt_badattach, 0, 0 }; | |
55e303ae | 197 | |
91447636 | 198 | static zone_t knote_zone; |
55e303ae A |
199 | |
200 | #define KN_HASHSIZE 64 /* XXX should be tunable */ | |
201 | #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask)) | |
202 | ||
203 | #if 0 | |
204 | extern struct filterops aio_filtops; | |
205 | #endif | |
206 | ||
207 | /* | |
208 | * Table for for all system-defined filters. | |
209 | */ | |
210 | static struct filterops *sysfilt_ops[] = { | |
211 | &file_filtops, /* EVFILT_READ */ | |
212 | &file_filtops, /* EVFILT_WRITE */ | |
213 | #if 0 | |
214 | &aio_filtops, /* EVFILT_AIO */ | |
215 | #else | |
216 | &bad_filtops, /* EVFILT_AIO */ | |
217 | #endif | |
218 | &file_filtops, /* EVFILT_VNODE */ | |
219 | &proc_filtops, /* EVFILT_PROC */ | |
220 | &sig_filtops, /* EVFILT_SIGNAL */ | |
55e303ae | 221 | &timer_filtops, /* EVFILT_TIMER */ |
55e303ae | 222 | &bad_filtops, /* EVFILT_MACHPORT */ |
91447636 | 223 | &fs_filtops /* EVFILT_FS */ |
55e303ae A |
224 | }; |
225 | ||
91447636 A |
226 | /* |
227 | * kqueue/note lock attributes and implementations | |
228 | * | |
229 | * kqueues have locks, while knotes have use counts | |
230 | * Most of the knote state is guarded by the object lock. | |
231 | * the knote "inuse" count and status use the kqueue lock. | |
232 | */ | |
233 | lck_grp_attr_t * kq_lck_grp_attr; | |
234 | lck_grp_t * kq_lck_grp; | |
235 | lck_attr_t * kq_lck_attr; | |
236 | ||
237 | static inline void | |
238 | kqlock(struct kqueue *kq) | |
239 | { | |
240 | lck_spin_lock(&kq->kq_lock); | |
241 | } | |
242 | ||
243 | static inline void | |
244 | kqunlock(struct kqueue *kq) | |
245 | { | |
246 | lck_spin_unlock(&kq->kq_lock); | |
247 | } | |
248 | ||
249 | /* | |
250 | * Convert a kq lock to a knote use referece. | |
251 | * | |
252 | * If the knote is being dropped, we can't get | |
253 | * a use reference, so just return with it | |
254 | * still locked. | |
255 | * | |
256 | * - kq locked at entry | |
257 | * - unlock on exit if we get the use reference | |
258 | */ | |
259 | static int | |
260 | kqlock2knoteuse(struct kqueue *kq, struct knote *kn) | |
261 | { | |
262 | if (kn->kn_status & KN_DROPPING) | |
263 | return 0; | |
264 | kn->kn_inuse++; | |
265 | kqunlock(kq); | |
266 | return 1; | |
267 | } | |
268 | ||
269 | /* | |
270 | * Convert a kq lock to a knote use referece. | |
271 | * | |
272 | * If the knote is being dropped, we can't get | |
273 | * a use reference, so just return with it | |
274 | * still locked. | |
275 | * | |
276 | * - kq locked at entry | |
277 | * - kq always unlocked on exit | |
278 | */ | |
279 | static int | |
280 | kqlock2knoteusewait(struct kqueue *kq, struct knote *kn) | |
281 | { | |
282 | if (!kqlock2knoteuse(kq, kn)) { | |
283 | kn->kn_status |= KN_DROPWAIT; | |
284 | assert_wait(&kn->kn_status, THREAD_UNINT); | |
285 | kqunlock(kq); | |
286 | thread_block(THREAD_CONTINUE_NULL); | |
287 | return 0; | |
288 | } | |
289 | return 1; | |
290 | } | |
291 | ||
292 | /* | |
293 | * Convert from a knote use reference back to kq lock. | |
294 | * | |
295 | * Drop a use reference and wake any waiters if | |
296 | * this is the last one. | |
297 | * | |
298 | * The exit return indicates if the knote is | |
299 | * still alive - but the kqueue lock is taken | |
300 | * unconditionally. | |
301 | */ | |
302 | static int | |
303 | knoteuse2kqlock(struct kqueue *kq, struct knote *kn) | |
304 | { | |
305 | kqlock(kq); | |
306 | if ((--kn->kn_inuse == 0) && | |
307 | (kn->kn_status & KN_USEWAIT)) { | |
308 | kn->kn_status &= ~KN_USEWAIT; | |
309 | thread_wakeup(&kn->kn_inuse); | |
310 | } | |
311 | return ((kn->kn_status & KN_DROPPING) == 0); | |
312 | } | |
313 | ||
314 | /* | |
315 | * Convert a kq lock to a knote drop referece. | |
316 | * | |
317 | * If the knote is in use, wait for the use count | |
318 | * to subside. We first mark our intention to drop | |
319 | * it - keeping other users from "piling on." | |
320 | * If we are too late, we have to wait for the | |
321 | * other drop to complete. | |
322 | * | |
323 | * - kq locked at entry | |
324 | * - always unlocked on exit. | |
325 | * - caller can't hold any locks that would prevent | |
326 | * the other dropper from completing. | |
327 | */ | |
328 | static int | |
329 | kqlock2knotedrop(struct kqueue *kq, struct knote *kn) | |
330 | { | |
331 | ||
332 | if ((kn->kn_status & KN_DROPPING) == 0) { | |
333 | kn->kn_status |= KN_DROPPING; | |
334 | if (kn->kn_inuse > 0) { | |
335 | kn->kn_status |= KN_USEWAIT; | |
336 | assert_wait(&kn->kn_inuse, THREAD_UNINT); | |
337 | kqunlock(kq); | |
338 | thread_block(THREAD_CONTINUE_NULL); | |
339 | } else | |
340 | kqunlock(kq); | |
341 | return 1; | |
342 | } else { | |
343 | kn->kn_status |= KN_DROPWAIT; | |
344 | assert_wait(&kn->kn_status, THREAD_UNINT); | |
345 | kqunlock(kq); | |
346 | thread_block(THREAD_CONTINUE_NULL); | |
347 | return 0; | |
348 | } | |
349 | } | |
350 | ||
351 | /* | |
352 | * Release a knote use count reference. | |
353 | */ | |
354 | static void | |
355 | knote_put(struct knote *kn) | |
356 | { | |
357 | struct kqueue *kq = kn->kn_kq; | |
358 | ||
359 | kqlock(kq); | |
360 | if ((--kn->kn_inuse == 0) && | |
361 | (kn->kn_status & KN_USEWAIT)) { | |
362 | kn->kn_status &= ~KN_USEWAIT; | |
363 | thread_wakeup(&kn->kn_inuse); | |
364 | } | |
365 | kqunlock(kq); | |
366 | } | |
367 | ||
368 | ||
369 | ||
55e303ae A |
370 | static int |
371 | filt_fileattach(struct knote *kn) | |
372 | { | |
373 | ||
374 | return (fo_kqfilter(kn->kn_fp, kn, current_proc())); | |
375 | } | |
376 | ||
91447636 A |
377 | #define f_flag f_fglob->fg_flag |
378 | #define f_type f_fglob->fg_type | |
379 | #define f_msgcount f_fglob->fg_msgcount | |
380 | #define f_cred f_fglob->fg_cred | |
381 | #define f_ops f_fglob->fg_ops | |
382 | #define f_offset f_fglob->fg_offset | |
383 | #define f_data f_fglob->fg_data | |
384 | ||
55e303ae A |
385 | static void |
386 | filt_kqdetach(struct knote *kn) | |
387 | { | |
388 | struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data; | |
389 | ||
91447636 | 390 | kqlock(kq); |
55e303ae | 391 | KNOTE_DETACH(&kq->kq_sel.si_note, kn); |
91447636 | 392 | kqunlock(kq); |
55e303ae A |
393 | } |
394 | ||
395 | /*ARGSUSED*/ | |
396 | static int | |
91447636 | 397 | filt_kqueue(struct knote *kn, __unused long hint) |
55e303ae A |
398 | { |
399 | struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data; | |
400 | ||
401 | kn->kn_data = kq->kq_count; | |
402 | return (kn->kn_data > 0); | |
403 | } | |
404 | ||
405 | static int | |
406 | filt_procattach(struct knote *kn) | |
407 | { | |
408 | struct proc *p; | |
91447636 A |
409 | int funnel_state; |
410 | ||
411 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
55e303ae A |
412 | |
413 | p = pfind(kn->kn_id); | |
91447636 A |
414 | if (p == NULL) { |
415 | thread_funnel_set(kernel_flock, funnel_state); | |
55e303ae | 416 | return (ESRCH); |
91447636 | 417 | } |
55e303ae | 418 | |
55e303ae A |
419 | kn->kn_flags |= EV_CLEAR; /* automatically set */ |
420 | ||
421 | /* | |
422 | * internal flag indicating registration done by kernel | |
423 | */ | |
424 | if (kn->kn_flags & EV_FLAG1) { | |
91447636 | 425 | kn->kn_data = (int)kn->kn_sdata; /* ppid */ |
55e303ae A |
426 | kn->kn_fflags = NOTE_CHILD; |
427 | kn->kn_flags &= ~EV_FLAG1; | |
428 | } | |
429 | ||
430 | /* XXX lock the proc here while adding to the list? */ | |
431 | KNOTE_ATTACH(&p->p_klist, kn); | |
432 | ||
91447636 A |
433 | thread_funnel_set(kernel_flock, funnel_state); |
434 | ||
55e303ae A |
435 | return (0); |
436 | } | |
437 | ||
438 | /* | |
439 | * The knote may be attached to a different process, which may exit, | |
21362eb3 A |
440 | * leaving nothing for the knote to be attached to. So when the process |
441 | * exits, the knote is marked as DETACHED and also flagged as ONESHOT so | |
442 | * it will be deleted when read out. However, as part of the knote deletion, | |
443 | * this routine is called, so a check is needed to avoid actually performing | |
444 | * a detach, because the original process does not exist any more. | |
55e303ae A |
445 | */ |
446 | static void | |
447 | filt_procdetach(struct knote *kn) | |
448 | { | |
91447636 A |
449 | struct proc *p; |
450 | int funnel_state; | |
55e303ae | 451 | |
91447636 A |
452 | funnel_state = thread_funnel_set(kernel_flock, TRUE); |
453 | p = pfind(kn->kn_id); | |
454 | ||
21362eb3 | 455 | if (p != (struct proc *)NULL) |
91447636 | 456 | KNOTE_DETACH(&p->p_klist, kn); |
21362eb3 | 457 | |
91447636 | 458 | thread_funnel_set(kernel_flock, funnel_state); |
55e303ae A |
459 | } |
460 | ||
461 | static int | |
462 | filt_proc(struct knote *kn, long hint) | |
463 | { | |
21362eb3 A |
464 | u_int event; |
465 | int funnel_state; | |
91447636 | 466 | |
21362eb3 | 467 | funnel_state = thread_funnel_set(kernel_flock, TRUE); |
55e303ae | 468 | |
21362eb3 A |
469 | /* |
470 | * mask off extra data | |
471 | */ | |
472 | event = (u_int)hint & NOTE_PCTRLMASK; | |
55e303ae | 473 | |
21362eb3 A |
474 | /* |
475 | * if the user is interested in this event, record it. | |
476 | */ | |
477 | if (kn->kn_sfflags & event) | |
478 | kn->kn_fflags |= event; | |
55e303ae | 479 | |
21362eb3 A |
480 | /* |
481 | * process is gone, so flag the event as finished. | |
482 | */ | |
483 | if (event == NOTE_EXIT) { | |
484 | kn->kn_flags |= (EV_EOF | EV_ONESHOT); | |
485 | thread_funnel_set(kernel_flock, funnel_state); | |
486 | return (1); | |
487 | } | |
55e303ae | 488 | |
21362eb3 A |
489 | /* |
490 | * process forked, and user wants to track the new process, | |
491 | * so attach a new knote to it, and immediately report an | |
492 | * event with the parent's pid. | |
493 | */ | |
494 | if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) { | |
495 | struct kevent kev; | |
496 | int error; | |
55e303ae A |
497 | |
498 | /* | |
21362eb3 | 499 | * register knote with new process. |
55e303ae | 500 | */ |
21362eb3 A |
501 | kev.ident = hint & NOTE_PDATAMASK; /* pid */ |
502 | kev.filter = kn->kn_filter; | |
503 | kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1; | |
504 | kev.fflags = kn->kn_sfflags; | |
505 | kev.data = kn->kn_id; /* parent */ | |
506 | kev.udata = kn->kn_kevent.udata; /* preserve udata */ | |
507 | error = kevent_register(kn->kn_kq, &kev, NULL); | |
508 | if (error) | |
509 | kn->kn_fflags |= NOTE_TRACKERR; | |
55e303ae | 510 | } |
21362eb3 A |
511 | event = kn->kn_fflags; |
512 | thread_funnel_set(kernel_flock, funnel_state); | |
55e303ae | 513 | |
21362eb3 | 514 | return (event != 0); |
55e303ae A |
515 | } |
516 | ||
91447636 A |
517 | /* |
518 | * filt_timercompute - compute absolute timeout | |
519 | * | |
520 | * The saved-data field in the knote contains the | |
521 | * time value. The saved filter-flags indicates | |
522 | * the unit of measurement. | |
523 | * | |
524 | * If the timeout is not absolute, adjust it for | |
525 | * the current time. | |
526 | */ | |
527 | static int | |
528 | filt_timercompute(struct knote *kn, uint64_t *abs_time) | |
529 | { | |
530 | uint64_t multiplier; | |
531 | uint64_t raw; | |
532 | ||
533 | switch (kn->kn_sfflags & (NOTE_SECONDS|NOTE_USECONDS|NOTE_NSECONDS)) { | |
534 | case NOTE_SECONDS: | |
535 | multiplier = NSEC_PER_SEC; | |
536 | break; | |
537 | case NOTE_USECONDS: | |
538 | multiplier = NSEC_PER_USEC; | |
539 | break; | |
540 | case NOTE_NSECONDS: | |
541 | multiplier = 1; | |
542 | break; | |
543 | case 0: /* milliseconds (default) */ | |
544 | multiplier = NSEC_PER_SEC / 1000; | |
545 | break; | |
546 | default: | |
547 | return EINVAL; | |
548 | } | |
549 | nanoseconds_to_absolutetime((uint64_t)kn->kn_sdata * multiplier, &raw); | |
550 | if (raw <= filt_timerfloor) { | |
551 | *abs_time = 0; | |
552 | return 0; | |
553 | } | |
554 | if ((kn->kn_sfflags & NOTE_ABSOLUTE) == NOTE_ABSOLUTE) { | |
555 | uint32_t seconds, nanoseconds; | |
556 | uint64_t now; | |
557 | ||
558 | clock_get_calendar_nanotime(&seconds, &nanoseconds); | |
559 | nanoseconds_to_absolutetime((uint64_t)seconds * NSEC_PER_SEC + nanoseconds, | |
560 | &now); | |
561 | if (now >= raw + filt_timerfloor) { | |
562 | *abs_time = 0; | |
563 | return 0; | |
564 | } | |
565 | raw -= now; | |
566 | } | |
567 | clock_absolutetime_interval_to_deadline(raw, abs_time); | |
568 | return 0; | |
569 | } | |
570 | ||
571 | /* | |
572 | * filt_timerexpire - the timer callout routine | |
573 | * | |
574 | * Just propagate the timer event into the knote | |
575 | * filter routine (by going through the knote | |
576 | * synchronization point). Pass a hint to | |
577 | * indicate this is a real event, not just a | |
578 | * query from above. | |
579 | */ | |
55e303ae | 580 | static void |
91447636 | 581 | filt_timerexpire(void *knx, __unused void *spare) |
55e303ae | 582 | { |
91447636 | 583 | struct klist timer_list; |
55e303ae | 584 | struct knote *kn = knx; |
91447636 A |
585 | |
586 | /* no "object" for timers, so fake a list */ | |
587 | SLIST_INIT(&timer_list); | |
588 | SLIST_INSERT_HEAD(&timer_list, kn, kn_selnext); | |
589 | KNOTE(&timer_list, 1); | |
55e303ae A |
590 | } |
591 | ||
592 | /* | |
91447636 A |
593 | * data contains amount of time to sleep, in milliseconds, |
594 | * or a pointer to a timespec structure. | |
55e303ae A |
595 | */ |
596 | static int | |
597 | filt_timerattach(struct knote *kn) | |
598 | { | |
91447636 A |
599 | thread_call_t callout; |
600 | uint64_t deadline; | |
601 | int error; | |
55e303ae | 602 | |
91447636 A |
603 | error = filt_timercompute(kn, &deadline); |
604 | if (error) | |
605 | return (error); | |
55e303ae | 606 | |
91447636 A |
607 | if (deadline) { |
608 | callout = thread_call_allocate(filt_timerexpire, kn); | |
609 | if (NULL == callout) | |
610 | return (ENOMEM); | |
611 | } else { | |
612 | /* handle as immediate */ | |
613 | kn->kn_sdata = 0; | |
614 | callout = NULL; | |
615 | } | |
55e303ae | 616 | |
91447636 A |
617 | filt_timerlock(); |
618 | kn->kn_hook = (caddr_t)callout; | |
55e303ae | 619 | |
91447636 A |
620 | /* absolute=EV_ONESHOT */ |
621 | if (kn->kn_sfflags & NOTE_ABSOLUTE) | |
622 | kn->kn_flags |= EV_ONESHOT; | |
623 | ||
624 | if (deadline) { | |
625 | /* all others - if not faking immediate */ | |
626 | kn->kn_flags |= EV_CLEAR; | |
627 | thread_call_enter_delayed(callout, deadline); | |
628 | kn->kn_hookid = 0; | |
629 | } else { | |
630 | /* fake immediate */ | |
631 | kn->kn_hookid = 1; | |
632 | } | |
633 | filt_timerunlock(); | |
55e303ae A |
634 | return (0); |
635 | } | |
636 | ||
637 | static void | |
638 | filt_timerdetach(struct knote *kn) | |
639 | { | |
91447636 A |
640 | thread_call_t callout; |
641 | ||
642 | filt_timerlock(); | |
643 | callout = (thread_call_t)kn->kn_hook; | |
644 | if (callout != NULL) { | |
645 | boolean_t cancelled; | |
646 | ||
647 | /* cancel the callout if we can */ | |
648 | cancelled = thread_call_cancel(callout); | |
649 | if (cancelled) { | |
650 | /* got it, just free it */ | |
651 | kn->kn_hook = NULL; | |
652 | filt_timerunlock(); | |
653 | thread_call_free(callout); | |
654 | return; | |
655 | } | |
656 | /* we have to wait for the expire routine. */ | |
657 | kn->kn_hookid = -1; /* we are detaching */ | |
658 | assert_wait(&kn->kn_hook, THREAD_UNINT); | |
659 | filt_timerunlock(); | |
660 | thread_block(THREAD_CONTINUE_NULL); | |
661 | assert(kn->kn_hook == NULL); | |
662 | return; | |
663 | } | |
664 | /* nothing to do */ | |
665 | filt_timerunlock(); | |
55e303ae A |
666 | } |
667 | ||
91447636 A |
668 | |
669 | ||
55e303ae | 670 | static int |
91447636 | 671 | filt_timer(struct knote *kn, __unused long hint) |
55e303ae | 672 | { |
91447636 A |
673 | int result; |
674 | ||
675 | if (hint) { | |
676 | /* real timer pop */ | |
677 | thread_call_t callout; | |
678 | boolean_t detaching; | |
679 | ||
680 | filt_timerlock(); | |
681 | ||
682 | kn->kn_data++; | |
683 | ||
684 | detaching = (kn->kn_hookid < 0); | |
685 | callout = (thread_call_t)kn->kn_hook; | |
55e303ae | 686 | |
91447636 A |
687 | if (!detaching && (kn->kn_flags & EV_ONESHOT) == 0) { |
688 | uint64_t deadline; | |
689 | int error; | |
690 | ||
691 | /* user input data may have changed - deal */ | |
692 | error = filt_timercompute(kn, &deadline); | |
693 | if (error) { | |
694 | kn->kn_flags |= EV_ERROR; | |
695 | kn->kn_data = error; | |
696 | } else if (deadline == 0) { | |
697 | /* revert to fake immediate */ | |
698 | kn->kn_flags &= ~EV_CLEAR; | |
699 | kn->kn_sdata = 0; | |
700 | kn->kn_hookid = 1; | |
701 | } else { | |
702 | /* keep the callout and re-arm */ | |
703 | thread_call_enter_delayed(callout, deadline); | |
704 | filt_timerunlock(); | |
705 | return 1; | |
706 | } | |
707 | } | |
708 | kn->kn_hook = NULL; | |
709 | filt_timerunlock(); | |
710 | thread_call_free(callout); | |
711 | ||
712 | /* if someone is waiting for timer to pop */ | |
713 | if (detaching) | |
714 | thread_wakeup(&kn->kn_hook); | |
715 | ||
716 | return 1; | |
717 | } | |
718 | ||
719 | /* user-query */ | |
720 | filt_timerlock(); | |
721 | ||
722 | /* change fake timer to real if needed */ | |
723 | while (kn->kn_hookid > 0 && kn->kn_sdata > 0) { | |
724 | int error; | |
725 | ||
726 | /* update the fake timer (make real) */ | |
727 | kn->kn_hookid = 0; | |
728 | kn->kn_data = 0; | |
729 | filt_timerunlock(); | |
730 | error = filt_timerattach(kn); | |
731 | filt_timerlock(); | |
732 | if (error) { | |
733 | kn->kn_flags |= EV_ERROR; | |
734 | kn->kn_data = error; | |
735 | filt_timerunlock(); | |
736 | return 1; | |
737 | } | |
738 | } | |
739 | ||
740 | /* if still fake, pretend it fired */ | |
741 | if (kn->kn_hookid > 0) | |
742 | kn->kn_data = 1; | |
743 | ||
744 | result = (kn->kn_data != 0); | |
745 | filt_timerunlock(); | |
746 | return result; | |
747 | } | |
748 | ||
749 | static void | |
750 | filt_timerlock(void) | |
751 | { | |
752 | lck_mtx_lock(&_filt_timerlock); | |
753 | } | |
754 | ||
755 | static void | |
756 | filt_timerunlock(void) | |
757 | { | |
758 | lck_mtx_unlock(&_filt_timerlock); | |
55e303ae | 759 | } |
55e303ae A |
760 | |
761 | /* | |
762 | * JMM - placeholder for not-yet-implemented filters | |
763 | */ | |
764 | static int | |
91447636 | 765 | filt_badattach(__unused struct knote *kn) |
55e303ae | 766 | { |
91447636 | 767 | return(ENOTSUP); |
55e303ae A |
768 | } |
769 | ||
55e303ae | 770 | |
91447636 A |
771 | struct kqueue * |
772 | kqueue_alloc(struct proc *p) | |
773 | { | |
774 | struct filedesc *fdp = p->p_fd; | |
775 | struct kqueue *kq; | |
776 | ||
777 | MALLOC_ZONE(kq, struct kqueue *, sizeof(struct kqueue), M_KQUEUE, M_WAITOK); | |
778 | if (kq != NULL) { | |
779 | bzero(kq, sizeof(struct kqueue)); | |
780 | lck_spin_init(&kq->kq_lock, kq_lck_grp, kq_lck_attr); | |
781 | TAILQ_INIT(&kq->kq_head); | |
782 | TAILQ_INIT(&kq->kq_inprocess); | |
783 | kq->kq_fdp = fdp; | |
784 | } | |
785 | ||
786 | if (fdp->fd_knlistsize < 0) { | |
787 | proc_fdlock(p); | |
788 | if (fdp->fd_knlistsize < 0) | |
789 | fdp->fd_knlistsize = 0; /* this process has had a kq */ | |
790 | proc_fdunlock(p); | |
791 | } | |
792 | ||
793 | return kq; | |
794 | } | |
795 | ||
796 | ||
797 | /* | |
798 | * kqueue_dealloc - detach all knotes from a kqueue and free it | |
799 | * | |
800 | * We walk each list looking for knotes referencing this | |
801 | * this kqueue. If we find one, we try to drop it. But | |
802 | * if we fail to get a drop reference, that will wait | |
803 | * until it is dropped. So, we can just restart again | |
804 | * safe in the assumption that the list will eventually | |
805 | * not contain any more references to this kqueue (either | |
806 | * we dropped them all, or someone else did). | |
807 | * | |
808 | * Assumes no new events are being added to the kqueue. | |
809 | * Nothing locked on entry or exit. | |
810 | */ | |
811 | void | |
812 | kqueue_dealloc(struct kqueue *kq, struct proc *p) | |
55e303ae A |
813 | { |
814 | struct filedesc *fdp = p->p_fd; | |
91447636 A |
815 | struct knote *kn; |
816 | int i; | |
817 | ||
818 | proc_fdlock(p); | |
819 | for (i = 0; i < fdp->fd_knlistsize; i++) { | |
820 | kn = SLIST_FIRST(&fdp->fd_knlist[i]); | |
821 | while (kn != NULL) { | |
822 | if (kq == kn->kn_kq) { | |
823 | kqlock(kq); | |
824 | proc_fdunlock(p); | |
825 | /* drop it ourselves or wait */ | |
826 | if (kqlock2knotedrop(kq, kn)) { | |
827 | kn->kn_fop->f_detach(kn); | |
828 | knote_drop(kn, p); | |
829 | } | |
830 | proc_fdlock(p); | |
831 | /* start over at beginning of list */ | |
832 | kn = SLIST_FIRST(&fdp->fd_knlist[i]); | |
833 | continue; | |
834 | } | |
835 | kn = SLIST_NEXT(kn, kn_link); | |
836 | } | |
837 | } | |
838 | if (fdp->fd_knhashmask != 0) { | |
839 | for (i = 0; i < (int)fdp->fd_knhashmask + 1; i++) { | |
840 | kn = SLIST_FIRST(&fdp->fd_knhash[i]); | |
841 | while (kn != NULL) { | |
842 | if (kq == kn->kn_kq) { | |
843 | kqlock(kq); | |
844 | proc_fdunlock(p); | |
845 | /* drop it ourselves or wait */ | |
846 | if (kqlock2knotedrop(kq, kn)) { | |
847 | kn->kn_fop->f_detach(kn); | |
848 | knote_drop(kn, p); | |
849 | } | |
850 | proc_fdlock(p); | |
851 | /* start over at beginning of list */ | |
852 | kn = SLIST_FIRST(&fdp->fd_knhash[i]); | |
853 | continue; | |
854 | } | |
855 | kn = SLIST_NEXT(kn, kn_link); | |
856 | } | |
857 | } | |
858 | } | |
859 | proc_fdunlock(p); | |
860 | lck_spin_destroy(&kq->kq_lock, kq_lck_grp); | |
861 | FREE_ZONE(kq, sizeof(struct kqueue), M_KQUEUE); | |
862 | } | |
863 | ||
864 | int | |
865 | kqueue(struct proc *p, __unused struct kqueue_args *uap, register_t *retval) | |
866 | { | |
55e303ae | 867 | struct kqueue *kq; |
91447636 | 868 | struct fileproc *fp; |
55e303ae A |
869 | int fd, error; |
870 | ||
871 | error = falloc(p, &fp, &fd); | |
91447636 | 872 | if (error) { |
55e303ae | 873 | return (error); |
91447636 A |
874 | } |
875 | ||
876 | kq = kqueue_alloc(p); | |
877 | if (kq == NULL) { | |
878 | fp_free(p, fd, fp); | |
879 | return (ENOMEM); | |
880 | } | |
881 | ||
55e303ae A |
882 | fp->f_flag = FREAD | FWRITE; |
883 | fp->f_type = DTYPE_KQUEUE; | |
884 | fp->f_ops = &kqueueops; | |
55e303ae | 885 | fp->f_data = (caddr_t)kq; |
91447636 A |
886 | |
887 | proc_fdlock(p); | |
888 | *fdflags(p, fd) &= ~UF_RESERVED; | |
889 | fp_drop(p, fd, fp, 1); | |
890 | proc_fdunlock(p); | |
891 | ||
55e303ae | 892 | *retval = fd; |
55e303ae A |
893 | return (error); |
894 | } | |
895 | ||
55e303ae | 896 | int |
91447636 A |
897 | kqueue_portset_np(__unused struct proc *p, |
898 | __unused struct kqueue_portset_np_args *uap, | |
899 | __unused register_t *retval) | |
55e303ae A |
900 | { |
901 | /* JMM - Placeholder for now */ | |
91447636 | 902 | return (ENOTSUP); |
55e303ae A |
903 | } |
904 | ||
55e303ae | 905 | int |
91447636 A |
906 | kqueue_from_portset_np(__unused struct proc *p, |
907 | __unused struct kqueue_from_portset_np_args *uap, | |
908 | __unused register_t *retval) | |
55e303ae A |
909 | { |
910 | /* JMM - Placeholder for now */ | |
91447636 | 911 | return (ENOTSUP); |
55e303ae A |
912 | } |
913 | ||
91447636 A |
914 | static int |
915 | kevent_copyin(user_addr_t *addrp, struct kevent *kevp, struct proc *p) | |
55e303ae | 916 | { |
91447636 A |
917 | int advance; |
918 | int error; | |
55e303ae | 919 | |
91447636 A |
920 | if (IS_64BIT_PROCESS(p)) { |
921 | struct user_kevent kev64; | |
922 | ||
923 | advance = sizeof(kev64); | |
924 | error = copyin(*addrp, (caddr_t)&kev64, advance); | |
55e303ae | 925 | if (error) |
91447636 A |
926 | return error; |
927 | kevp->ident = CAST_DOWN(uintptr_t, kev64.ident); | |
928 | kevp->filter = kev64.filter; | |
929 | kevp->flags = kev64.flags; | |
930 | kevp->fflags = kev64.fflags; | |
931 | kevp->data = CAST_DOWN(intptr_t, kev64.data); | |
932 | kevp->udata = kev64.udata; | |
933 | } else { | |
934 | /* | |
935 | * compensate for legacy in-kernel kevent layout | |
936 | * where the udata field is alredy 64-bit. | |
937 | */ | |
938 | advance = sizeof(*kevp) + sizeof(void *) - sizeof(user_addr_t); | |
939 | error = copyin(*addrp, (caddr_t)kevp, advance); | |
55e303ae | 940 | } |
91447636 A |
941 | if (!error) |
942 | *addrp += advance; | |
943 | return error; | |
944 | } | |
55e303ae | 945 | |
91447636 A |
946 | static int |
947 | kevent_copyout(struct kevent *kevp, user_addr_t *addrp, struct proc *p) | |
948 | { | |
949 | int advance; | |
950 | int error; | |
951 | ||
952 | if (IS_64BIT_PROCESS(p)) { | |
953 | struct user_kevent kev64; | |
954 | ||
955 | kev64.ident = (uint64_t) kevp->ident; | |
956 | kev64.filter = kevp->filter; | |
957 | kev64.flags = kevp->flags; | |
958 | kev64.fflags = kevp->fflags; | |
959 | kev64.data = (int64_t) kevp->data; | |
960 | kev64.udata = kevp->udata; | |
961 | advance = sizeof(kev64); | |
962 | error = copyout((caddr_t)&kev64, *addrp, advance); | |
963 | } else { | |
964 | /* | |
965 | * compensate for legacy in-kernel kevent layout | |
966 | * where the udata field is alredy 64-bit. | |
967 | */ | |
968 | advance = sizeof(*kevp) + sizeof(void *) - sizeof(user_addr_t); | |
969 | error = copyout((caddr_t)kevp, *addrp, advance); | |
970 | } | |
971 | if (!error) | |
972 | *addrp += advance; | |
973 | return error; | |
974 | } | |
55e303ae | 975 | |
91447636 A |
976 | /* |
977 | * kevent_continue - continue a kevent syscall after blocking | |
978 | * | |
979 | * assume we inherit a use count on the kq fileglob. | |
980 | */ | |
55e303ae | 981 | |
91447636 A |
982 | static void |
983 | kevent_continue(__unused struct kqueue *kq, void *data, int error) | |
984 | { | |
985 | struct _kevent *cont_args; | |
986 | struct fileproc *fp; | |
987 | register_t *retval; | |
988 | int noutputs; | |
989 | int fd; | |
990 | struct proc *p = current_proc(); | |
991 | ||
992 | cont_args = (struct _kevent *)data; | |
993 | noutputs = cont_args->eventout; | |
994 | retval = cont_args->retval; | |
995 | fd = cont_args->fd; | |
996 | fp = cont_args->fp; | |
997 | ||
998 | fp_drop(p, fd, fp, 0); | |
999 | ||
1000 | /* don't restart after signals... */ | |
1001 | if (error == ERESTART) | |
1002 | error = EINTR; | |
1003 | else if (error == EWOULDBLOCK) | |
1004 | error = 0; | |
1005 | if (error == 0) | |
1006 | *retval = noutputs; | |
1007 | unix_syscall_return(error); | |
1008 | } | |
55e303ae | 1009 | |
91447636 A |
1010 | /* |
1011 | * kevent - [syscall] register and wait for kernel events | |
1012 | * | |
1013 | */ | |
55e303ae | 1014 | |
91447636 A |
1015 | int |
1016 | kevent(struct proc *p, struct kevent_args *uap, register_t *retval) | |
1017 | { | |
1018 | user_addr_t changelist = uap->changelist; | |
1019 | user_addr_t ueventlist = uap->eventlist; | |
1020 | int nchanges = uap->nchanges; | |
1021 | int nevents = uap->nevents; | |
1022 | int fd = uap->fd; | |
1023 | ||
1024 | struct _kevent *cont_args; | |
1025 | uthread_t ut; | |
1026 | struct kqueue *kq; | |
1027 | struct fileproc *fp; | |
1028 | struct kevent kev; | |
1029 | int error, noutputs; | |
1030 | struct timeval atv; | |
1031 | ||
1032 | /* convert timeout to absolute - if we have one */ | |
1033 | if (uap->timeout != USER_ADDR_NULL) { | |
1034 | struct timeval rtv; | |
1035 | if ( IS_64BIT_PROCESS(p) ) { | |
1036 | struct user_timespec ts; | |
1037 | error = copyin( uap->timeout, &ts, sizeof(ts) ); | |
1038 | if ((ts.tv_sec & 0xFFFFFFFF00000000ull) != 0) | |
1039 | error = EINVAL; | |
1040 | else | |
1041 | TIMESPEC_TO_TIMEVAL(&rtv, &ts); | |
1042 | } else { | |
1043 | struct timespec ts; | |
1044 | error = copyin( uap->timeout, &ts, sizeof(ts) ); | |
1045 | TIMESPEC_TO_TIMEVAL(&rtv, &ts); | |
1046 | } | |
55e303ae | 1047 | if (error) |
91447636 A |
1048 | return error; |
1049 | if (itimerfix(&rtv)) | |
1050 | return EINVAL; | |
1051 | getmicrouptime(&atv); | |
1052 | timevaladd(&atv, &rtv); | |
1053 | } else { | |
1054 | atv.tv_sec = 0; | |
1055 | atv.tv_usec = 0; | |
1056 | } | |
55e303ae | 1057 | |
91447636 A |
1058 | /* get a usecount for the kq itself */ |
1059 | if ((error = fp_getfkq(p, fd, &fp, &kq)) != 0) | |
1060 | return(error); | |
1061 | ||
1062 | /* register all the change requests the user provided... */ | |
1063 | noutputs = 0; | |
3a60a9f5 | 1064 | while (nchanges > 0 && error == 0) { |
91447636 A |
1065 | error = kevent_copyin(&changelist, &kev, p); |
1066 | if (error) | |
1067 | break; | |
1068 | ||
1069 | kev.flags &= ~EV_SYSFLAGS; | |
1070 | error = kevent_register(kq, &kev, p); | |
3a60a9f5 | 1071 | if (error && nevents > 0) { |
91447636 A |
1072 | kev.flags = EV_ERROR; |
1073 | kev.data = error; | |
3a60a9f5 A |
1074 | error = kevent_copyout(&kev, &ueventlist, p); |
1075 | if (error == 0) { | |
1076 | nevents--; | |
1077 | noutputs++; | |
1078 | } | |
55e303ae | 1079 | } |
91447636 | 1080 | nchanges--; |
55e303ae A |
1081 | } |
1082 | ||
91447636 A |
1083 | /* store the continuation/completion data in the uthread */ |
1084 | ut = (uthread_t)get_bsdthread_info(current_thread()); | |
1085 | cont_args = (struct _kevent *)&ut->uu_state.ss_kevent; | |
1086 | cont_args->fp = fp; | |
1087 | cont_args->fd = fd; | |
1088 | cont_args->retval = retval; | |
1089 | cont_args->eventlist = ueventlist; | |
1090 | cont_args->eventcount = nevents; | |
1091 | cont_args->eventout = noutputs; | |
1092 | ||
1093 | if (nevents > 0 && noutputs == 0 && error == 0) | |
1094 | error = kevent_scan(kq, kevent_callback, | |
1095 | kevent_continue, cont_args, | |
1096 | &atv, p); | |
1097 | kevent_continue(kq, cont_args, error); | |
1098 | /* NOTREACHED */ | |
1099 | return error; | |
1100 | } | |
1101 | ||
1102 | ||
1103 | /* | |
1104 | * kevent_callback - callback for each individual event | |
1105 | * | |
1106 | * called with nothing locked | |
1107 | * caller holds a reference on the kqueue | |
1108 | */ | |
1109 | ||
1110 | static int | |
1111 | kevent_callback(__unused struct kqueue *kq, struct kevent *kevp, void *data) | |
1112 | { | |
1113 | struct _kevent *cont_args; | |
1114 | int error; | |
1115 | ||
1116 | cont_args = (struct _kevent *)data; | |
1117 | assert(cont_args->eventout < cont_arg->eventcount); | |
1118 | ||
1119 | /* | |
1120 | * Copy out the appropriate amount of event data for this user. | |
1121 | */ | |
1122 | error = kevent_copyout(kevp, &cont_args->eventlist, current_proc()); | |
1123 | ||
1124 | /* | |
1125 | * If there isn't space for additional events, return | |
1126 | * a harmless error to stop the processing here | |
1127 | */ | |
1128 | if (error == 0 && ++cont_args->eventout == cont_args->eventcount) | |
1129 | error = EWOULDBLOCK; | |
1130 | return error; | |
55e303ae A |
1131 | } |
1132 | ||
91447636 A |
1133 | /* |
1134 | * kevent_register - add a new event to a kqueue | |
1135 | * | |
1136 | * Creates a mapping between the event source and | |
1137 | * the kqueue via a knote data structure. | |
1138 | * | |
1139 | * Because many/most the event sources are file | |
1140 | * descriptor related, the knote is linked off | |
1141 | * the filedescriptor table for quick access. | |
1142 | * | |
1143 | * called with nothing locked | |
1144 | * caller holds a reference on the kqueue | |
1145 | */ | |
1146 | ||
55e303ae | 1147 | int |
91447636 | 1148 | kevent_register(struct kqueue *kq, struct kevent *kev, struct proc *p) |
55e303ae A |
1149 | { |
1150 | struct filedesc *fdp = kq->kq_fdp; | |
1151 | struct filterops *fops; | |
91447636 | 1152 | struct fileproc *fp = NULL; |
55e303ae | 1153 | struct knote *kn = NULL; |
91447636 | 1154 | int error = 0; |
55e303ae A |
1155 | |
1156 | if (kev->filter < 0) { | |
1157 | if (kev->filter + EVFILT_SYSCOUNT < 0) | |
1158 | return (EINVAL); | |
1159 | fops = sysfilt_ops[~kev->filter]; /* to 0-base index */ | |
1160 | } else { | |
1161 | /* | |
1162 | * XXX | |
1163 | * filter attach routine is responsible for insuring that | |
1164 | * the identifier can be attached to it. | |
1165 | */ | |
1166 | printf("unknown filter: %d\n", kev->filter); | |
1167 | return (EINVAL); | |
1168 | } | |
1169 | ||
91447636 A |
1170 | /* this iocount needs to be dropped if it is not registered */ |
1171 | if (fops->f_isfd && (error = fp_lookup(p, kev->ident, &fp, 0)) != 0) | |
1172 | return(error); | |
55e303ae | 1173 | |
91447636 A |
1174 | restart: |
1175 | proc_fdlock(p); | |
1176 | if (fops->f_isfd) { | |
1177 | /* fd-based knotes are linked off the fd table */ | |
1178 | if (kev->ident < (u_int)fdp->fd_knlistsize) { | |
55e303ae A |
1179 | SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link) |
1180 | if (kq == kn->kn_kq && | |
1181 | kev->filter == kn->kn_filter) | |
1182 | break; | |
1183 | } | |
1184 | } else { | |
91447636 | 1185 | /* hash non-fd knotes here too */ |
55e303ae A |
1186 | if (fdp->fd_knhashmask != 0) { |
1187 | struct klist *list; | |
1188 | ||
1189 | list = &fdp->fd_knhash[ | |
1190 | KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)]; | |
1191 | SLIST_FOREACH(kn, list, kn_link) | |
1192 | if (kev->ident == kn->kn_id && | |
1193 | kq == kn->kn_kq && | |
1194 | kev->filter == kn->kn_filter) | |
1195 | break; | |
1196 | } | |
1197 | } | |
1198 | ||
91447636 A |
1199 | /* |
1200 | * kn now contains the matching knote, or NULL if no match | |
1201 | */ | |
1202 | if (kn == NULL) { | |
1203 | if ((kev->flags & (EV_ADD|EV_DELETE)) == EV_ADD) { | |
1204 | kn = knote_alloc(); | |
1205 | if (kn == NULL) { | |
1206 | proc_fdunlock(p); | |
1207 | error = ENOMEM; | |
1208 | goto done; | |
1209 | } | |
1210 | kn->kn_fp = fp; | |
1211 | kn->kn_kq = kq; | |
1212 | kn->kn_tq = &kq->kq_head; | |
1213 | kn->kn_fop = fops; | |
1214 | kn->kn_sfflags = kev->fflags; | |
1215 | kn->kn_sdata = kev->data; | |
1216 | kev->fflags = 0; | |
1217 | kev->data = 0; | |
1218 | kn->kn_kevent = *kev; | |
1219 | kn->kn_inuse = 1; /* for f_attach() */ | |
1220 | kn->kn_status = 0; | |
1221 | ||
1222 | /* before anyone can find it */ | |
1223 | if (kev->flags & EV_DISABLE) | |
1224 | kn->kn_status |= KN_DISABLED; | |
1225 | ||
1226 | error = knote_fdpattach(kn, fdp, p); | |
1227 | proc_fdunlock(p); | |
1228 | ||
1229 | if (error) { | |
1230 | knote_free(kn); | |
1231 | goto done; | |
1232 | } | |
1233 | ||
1234 | /* | |
1235 | * apply reference count to knote structure, and | |
1236 | * do not release it at the end of this routine. | |
1237 | */ | |
1238 | fp = NULL; | |
1239 | ||
1240 | /* | |
1241 | * If the attach fails here, we can drop it knowing | |
1242 | * that nobody else has a reference to the knote. | |
1243 | */ | |
1244 | if ((error = fops->f_attach(kn)) != 0) { | |
1245 | knote_drop(kn, p); | |
1246 | goto done; | |
1247 | } | |
1248 | } else { | |
1249 | proc_fdunlock(p); | |
1250 | error = ENOENT; | |
1251 | goto done; | |
1252 | } | |
1253 | } else { | |
1254 | /* existing knote - get kqueue lock */ | |
1255 | kqlock(kq); | |
1256 | proc_fdunlock(p); | |
1257 | ||
1258 | if (kev->flags & EV_DELETE) { | |
1259 | knote_dequeue(kn); | |
1260 | kn->kn_status |= KN_DISABLED; | |
1261 | if (kqlock2knotedrop(kq, kn)) { | |
1262 | kn->kn_fop->f_detach(kn); | |
1263 | knote_drop(kn, p); | |
1264 | } | |
1265 | goto done; | |
1266 | } | |
1267 | ||
1268 | /* update status flags for existing knote */ | |
1269 | if (kev->flags & EV_DISABLE) { | |
1270 | knote_dequeue(kn); | |
1271 | kn->kn_status |= KN_DISABLED; | |
1272 | } else if (kev->flags & EV_ENABLE) { | |
1273 | kn->kn_status &= ~KN_DISABLED; | |
1274 | if (kn->kn_status & KN_ACTIVE) | |
1275 | knote_enqueue(kn); | |
1276 | } | |
1277 | ||
1278 | /* | |
1279 | * If somebody is in the middle of dropping this | |
1280 | * knote - go find/insert a new one. But we have | |
1281 | * wait for this one to go away first. | |
1282 | */ | |
1283 | if (!kqlock2knoteusewait(kq, kn)) | |
1284 | /* kqueue unlocked */ | |
1285 | goto restart; | |
1286 | ||
1287 | /* | |
1288 | * The user may change some filter values after the | |
1289 | * initial EV_ADD, but doing so will not reset any | |
1290 | * filter which have already been triggered. | |
1291 | */ | |
1292 | kn->kn_sfflags = kev->fflags; | |
1293 | kn->kn_sdata = kev->data; | |
1294 | kn->kn_kevent.udata = kev->udata; | |
1295 | } | |
1296 | ||
1297 | /* still have use ref on knote */ | |
1298 | if (kn->kn_fop->f_event(kn, 0)) { | |
1299 | if (knoteuse2kqlock(kq, kn)) | |
1300 | knote_activate(kn); | |
1301 | kqunlock(kq); | |
1302 | } else { | |
1303 | knote_put(kn); | |
1304 | } | |
1305 | ||
1306 | done: | |
1307 | if (fp != NULL) | |
1308 | fp_drop(p, kev->ident, fp, 0); | |
1309 | return (error); | |
1310 | } | |
1311 | ||
1312 | /* | |
1313 | * kevent_process - process the triggered events in a kqueue | |
1314 | * | |
1315 | * Walk the queued knotes and validate that they are | |
1316 | * really still triggered events by calling the filter | |
1317 | * routines (if necessary). Hold a use reference on | |
1318 | * the knote to avoid it being detached. For each event | |
1319 | * that is still considered triggered, invoke the | |
1320 | * callback routine provided. | |
1321 | * | |
1322 | * caller holds a reference on the kqueue. | |
1323 | * kqueue locked on entry and exit - but may be dropped | |
1324 | */ | |
1325 | ||
1326 | static int | |
1327 | kevent_process(struct kqueue *kq, | |
1328 | kevent_callback_t callback, | |
1329 | void *data, | |
1330 | int *countp, | |
1331 | struct proc *p) | |
1332 | { | |
1333 | struct knote *kn; | |
1334 | struct kevent kev; | |
1335 | int nevents; | |
1336 | int error; | |
1337 | ||
1338 | restart: | |
1339 | if (kq->kq_count == 0) { | |
1340 | *countp = 0; | |
1341 | return 0; | |
1342 | } | |
1343 | ||
1344 | /* if someone else is processing the queue, wait */ | |
1345 | if (!TAILQ_EMPTY(&kq->kq_inprocess)) { | |
1346 | assert_wait(&kq->kq_inprocess, THREAD_UNINT); | |
1347 | kq->kq_state |= KQ_PROCWAIT; | |
1348 | kqunlock(kq); | |
1349 | thread_block(THREAD_CONTINUE_NULL); | |
1350 | kqlock(kq); | |
1351 | goto restart; | |
55e303ae A |
1352 | } |
1353 | ||
91447636 A |
1354 | error = 0; |
1355 | nevents = 0; | |
1356 | while (error == 0 && | |
1357 | (kn = TAILQ_FIRST(&kq->kq_head)) != NULL) { | |
55e303ae | 1358 | |
91447636 | 1359 | /* |
21362eb3 A |
1360 | * move knote to the processed queue. |
1361 | * this is also protected by the kq lock. | |
1362 | */ | |
1363 | assert(kn->kn_tq == &kq->kq_head); | |
1364 | TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); | |
1365 | kn->kn_tq = &kq->kq_inprocess; | |
1366 | TAILQ_INSERT_TAIL(&kq->kq_inprocess, kn, kn_tqe); | |
1367 | ||
1368 | /* | |
91447636 A |
1369 | * Non-EV_ONESHOT events must be re-validated. |
1370 | * | |
1371 | * Convert our lock to a use-count and call the event's | |
1372 | * filter routine to update. | |
1373 | * | |
21362eb3 A |
1374 | * If the event is dropping (or no longer valid), we |
1375 | * already have it off the active queue, so just | |
1376 | * finish the job of deactivating it. | |
91447636 A |
1377 | */ |
1378 | if ((kn->kn_flags & EV_ONESHOT) == 0) { | |
1379 | int result; | |
55e303ae | 1380 | |
91447636 A |
1381 | if (kqlock2knoteuse(kq, kn)) { |
1382 | ||
1383 | /* call the filter with just a ref */ | |
1384 | result = kn->kn_fop->f_event(kn, 0); | |
55e303ae | 1385 | |
21362eb3 A |
1386 | if (!knoteuse2kqlock(kq, kn) || result == 0) { |
1387 | knote_deactivate(kn); | |
91447636 A |
1388 | continue; |
1389 | } | |
1390 | } else { | |
21362eb3 | 1391 | knote_deactivate(kn); |
91447636 A |
1392 | continue; |
1393 | } | |
1394 | } | |
1395 | ||
1396 | /* | |
1397 | * Got a valid triggered knote with the kqueue | |
1398 | * still locked. Snapshot the data, and determine | |
1399 | * how to dispatch the knote for future events. | |
1400 | */ | |
1401 | kev = kn->kn_kevent; | |
1402 | ||
1403 | /* now what happens to it? */ | |
1404 | if (kn->kn_flags & EV_ONESHOT) { | |
1405 | knote_deactivate(kn); | |
1406 | if (kqlock2knotedrop(kq, kn)) { | |
1407 | kn->kn_fop->f_detach(kn); | |
55e303ae | 1408 | knote_drop(kn, p); |
55e303ae | 1409 | } |
91447636 A |
1410 | } else if (kn->kn_flags & EV_CLEAR) { |
1411 | knote_deactivate(kn); | |
1412 | kn->kn_data = 0; | |
1413 | kn->kn_fflags = 0; | |
1414 | kqunlock(kq); | |
55e303ae A |
1415 | } else { |
1416 | /* | |
91447636 A |
1417 | * leave on in-process queue. We'll |
1418 | * move all the remaining ones back | |
1419 | * the kq queue and wakeup any | |
1420 | * waiters when we are done. | |
55e303ae | 1421 | */ |
91447636 | 1422 | kqunlock(kq); |
55e303ae A |
1423 | } |
1424 | ||
91447636 A |
1425 | /* callback to handle each event as we find it */ |
1426 | error = (callback)(kq, &kev, data); | |
1427 | nevents++; | |
55e303ae | 1428 | |
91447636 | 1429 | kqlock(kq); |
55e303ae A |
1430 | } |
1431 | ||
91447636 A |
1432 | /* |
1433 | * With the kqueue still locked, move any knotes | |
1434 | * remaining on the in-process queue back to the | |
1435 | * kq's queue and wake up any waiters. | |
1436 | */ | |
1437 | while ((kn = TAILQ_FIRST(&kq->kq_inprocess)) != NULL) { | |
1438 | assert(kn->kn_tq == &kq->kq_inprocess); | |
1439 | TAILQ_REMOVE(&kq->kq_inprocess, kn, kn_tqe); | |
1440 | kn->kn_tq = &kq->kq_head; | |
1441 | TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); | |
55e303ae | 1442 | } |
91447636 A |
1443 | if (kq->kq_state & KQ_PROCWAIT) { |
1444 | kq->kq_state &= ~KQ_PROCWAIT; | |
1445 | thread_wakeup(&kq->kq_inprocess); | |
55e303ae A |
1446 | } |
1447 | ||
91447636 A |
1448 | *countp = nevents; |
1449 | return error; | |
55e303ae A |
1450 | } |
1451 | ||
91447636 A |
1452 | |
1453 | static void | |
1454 | kevent_scan_continue(void *data, wait_result_t wait_result) | |
55e303ae | 1455 | { |
91447636 A |
1456 | uthread_t ut = (uthread_t)get_bsdthread_info(current_thread()); |
1457 | struct _kevent_scan * cont_args = &ut->uu_state.ss_kevent_scan; | |
1458 | struct kqueue *kq = (struct kqueue *)data; | |
1459 | int error; | |
1460 | int count; | |
1461 | ||
1462 | /* convert the (previous) wait_result to a proper error */ | |
1463 | switch (wait_result) { | |
1464 | case THREAD_AWAKENED: | |
1465 | kqlock(kq); | |
1466 | error = kevent_process(kq, cont_args->call, cont_args, &count, current_proc()); | |
1467 | if (error == 0 && count == 0) { | |
1468 | assert_wait_deadline(kq, THREAD_ABORTSAFE, cont_args->deadline); | |
1469 | kq->kq_state |= KQ_SLEEP; | |
1470 | kqunlock(kq); | |
1471 | thread_block_parameter(kevent_scan_continue, kq); | |
1472 | /* NOTREACHED */ | |
55e303ae | 1473 | } |
91447636 A |
1474 | kqunlock(kq); |
1475 | break; | |
1476 | case THREAD_TIMED_OUT: | |
1477 | error = EWOULDBLOCK; | |
1478 | break; | |
1479 | case THREAD_INTERRUPTED: | |
1480 | error = EINTR; | |
1481 | break; | |
1482 | default: | |
1483 | panic("kevent_scan_cont() - invalid wait_result (%d)", wait_result); | |
1484 | error = 0; | |
55e303ae | 1485 | } |
91447636 A |
1486 | |
1487 | /* call the continuation with the results */ | |
1488 | assert(cont_args->cont != NULL); | |
1489 | (cont_args->cont)(kq, cont_args->data, error); | |
1490 | } | |
55e303ae | 1491 | |
55e303ae | 1492 | |
91447636 A |
1493 | /* |
1494 | * kevent_scan - scan and wait for events in a kqueue | |
1495 | * | |
1496 | * Process the triggered events in a kqueue. | |
1497 | * | |
1498 | * If there are no events triggered arrange to | |
1499 | * wait for them. If the caller provided a | |
1500 | * continuation routine, then kevent_scan will | |
1501 | * also. | |
1502 | * | |
1503 | * The callback routine must be valid. | |
1504 | * The caller must hold a use-count reference on the kq. | |
1505 | */ | |
55e303ae | 1506 | |
91447636 A |
1507 | int |
1508 | kevent_scan(struct kqueue *kq, | |
1509 | kevent_callback_t callback, | |
1510 | kevent_continue_t continuation, | |
1511 | void *data, | |
1512 | struct timeval *atvp, | |
1513 | struct proc *p) | |
1514 | { | |
1515 | thread_continue_t cont = THREAD_CONTINUE_NULL; | |
1516 | uint64_t deadline; | |
1517 | int error; | |
1518 | int first; | |
55e303ae | 1519 | |
91447636 | 1520 | assert(callback != NULL); |
55e303ae | 1521 | |
91447636 A |
1522 | first = 1; |
1523 | for (;;) { | |
1524 | wait_result_t wait_result; | |
1525 | int count; | |
1526 | ||
1527 | /* | |
1528 | * Make a pass through the kq to find events already | |
1529 | * triggered. | |
1530 | */ | |
1531 | kqlock(kq); | |
1532 | error = kevent_process(kq, callback, data, &count, p); | |
1533 | if (error || count) | |
1534 | break; /* lock still held */ | |
1535 | ||
1536 | /* looks like we have to consider blocking */ | |
1537 | if (first) { | |
1538 | first = 0; | |
1539 | /* convert the timeout to a deadline once */ | |
1540 | if (atvp->tv_sec || atvp->tv_usec) { | |
1541 | uint32_t seconds, nanoseconds; | |
1542 | uint64_t now; | |
1543 | ||
1544 | clock_get_uptime(&now); | |
1545 | nanoseconds_to_absolutetime((uint64_t)atvp->tv_sec * NSEC_PER_SEC + | |
1546 | atvp->tv_usec * NSEC_PER_USEC, | |
1547 | &deadline); | |
1548 | if (now >= deadline) { | |
1549 | /* non-blocking call */ | |
1550 | error = EWOULDBLOCK; | |
1551 | break; /* lock still held */ | |
1552 | } | |
1553 | deadline -= now; | |
1554 | clock_absolutetime_interval_to_deadline(deadline, &deadline); | |
55e303ae | 1555 | } else { |
91447636 A |
1556 | deadline = 0; /* block forever */ |
1557 | } | |
1558 | ||
1559 | if (continuation) { | |
1560 | uthread_t ut = (uthread_t)get_bsdthread_info(current_thread()); | |
1561 | struct _kevent_scan *cont_args = &ut->uu_state.ss_kevent_scan; | |
1562 | ||
1563 | cont_args->call = callback; | |
1564 | cont_args->cont = continuation; | |
1565 | cont_args->deadline = deadline; | |
1566 | cont_args->data = data; | |
1567 | cont = kevent_scan_continue; | |
55e303ae A |
1568 | } |
1569 | } | |
91447636 A |
1570 | |
1571 | /* go ahead and wait */ | |
1572 | assert_wait_deadline(kq, THREAD_ABORTSAFE, deadline); | |
1573 | kq->kq_state |= KQ_SLEEP; | |
1574 | kqunlock(kq); | |
1575 | wait_result = thread_block_parameter(cont, kq); | |
1576 | /* NOTREACHED if (continuation != NULL) */ | |
1577 | ||
1578 | switch (wait_result) { | |
1579 | case THREAD_AWAKENED: | |
1580 | continue; | |
1581 | case THREAD_TIMED_OUT: | |
1582 | return EWOULDBLOCK; | |
1583 | case THREAD_INTERRUPTED: | |
1584 | return EINTR; | |
1585 | default: | |
1586 | panic("kevent_scan - bad wait_result (%d)", | |
1587 | wait_result); | |
1588 | error = 0; | |
1589 | } | |
55e303ae | 1590 | } |
91447636 A |
1591 | kqunlock(kq); |
1592 | return error; | |
55e303ae A |
1593 | } |
1594 | ||
91447636 | 1595 | |
55e303ae A |
1596 | /* |
1597 | * XXX | |
1598 | * This could be expanded to call kqueue_scan, if desired. | |
1599 | */ | |
1600 | /*ARGSUSED*/ | |
1601 | static int | |
91447636 A |
1602 | kqueue_read(__unused struct fileproc *fp, |
1603 | __unused struct uio *uio, | |
1604 | __unused kauth_cred_t cred, | |
1605 | __unused int flags, | |
1606 | __unused struct proc *p) | |
55e303ae A |
1607 | { |
1608 | return (ENXIO); | |
1609 | } | |
1610 | ||
1611 | /*ARGSUSED*/ | |
1612 | static int | |
91447636 A |
1613 | kqueue_write(__unused struct fileproc *fp, |
1614 | __unused struct uio *uio, | |
1615 | __unused kauth_cred_t cred, | |
1616 | __unused int flags, | |
1617 | __unused struct proc *p) | |
55e303ae A |
1618 | { |
1619 | return (ENXIO); | |
1620 | } | |
1621 | ||
1622 | /*ARGSUSED*/ | |
1623 | static int | |
91447636 A |
1624 | kqueue_ioctl(__unused struct fileproc *fp, |
1625 | __unused u_long com, | |
1626 | __unused caddr_t data, | |
1627 | __unused struct proc *p) | |
55e303ae A |
1628 | { |
1629 | return (ENOTTY); | |
1630 | } | |
1631 | ||
1632 | /*ARGSUSED*/ | |
1633 | static int | |
91447636 | 1634 | kqueue_select(struct fileproc *fp, int which, void *wql, struct proc *p) |
55e303ae A |
1635 | { |
1636 | struct kqueue *kq = (struct kqueue *)fp->f_data; | |
1637 | int retnum = 0; | |
55e303ae A |
1638 | |
1639 | if (which == FREAD) { | |
91447636 | 1640 | kqlock(kq); |
55e303ae A |
1641 | if (kq->kq_count) { |
1642 | retnum = 1; | |
1643 | } else { | |
91447636 | 1644 | selrecord(p, &kq->kq_sel, wql); |
55e303ae A |
1645 | kq->kq_state |= KQ_SEL; |
1646 | } | |
91447636 | 1647 | kqunlock(kq); |
55e303ae | 1648 | } |
55e303ae A |
1649 | return (retnum); |
1650 | } | |
1651 | ||
91447636 A |
1652 | /* |
1653 | * kqueue_close - | |
1654 | */ | |
55e303ae A |
1655 | /*ARGSUSED*/ |
1656 | static int | |
91447636 | 1657 | kqueue_close(struct fileglob *fg, struct proc *p) |
55e303ae | 1658 | { |
91447636 | 1659 | struct kqueue *kq = (struct kqueue *)fg->fg_data; |
55e303ae | 1660 | |
91447636 A |
1661 | kqueue_dealloc(kq, p); |
1662 | fg->fg_data = NULL; | |
55e303ae A |
1663 | return (0); |
1664 | } | |
1665 | ||
1666 | /*ARGSUSED*/ | |
91447636 A |
1667 | /* |
1668 | * The callers has taken a use-count reference on this kqueue and will donate it | |
1669 | * to the kqueue we are being added to. This keeps the kqueue from closing until | |
1670 | * that relationship is torn down. | |
1671 | */ | |
55e303ae | 1672 | static int |
91447636 | 1673 | kqueue_kqfilter(__unused struct fileproc *fp, struct knote *kn, __unused struct proc *p) |
55e303ae A |
1674 | { |
1675 | struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data; | |
1676 | ||
91447636 | 1677 | if (kn->kn_filter != EVFILT_READ) |
55e303ae A |
1678 | return (1); |
1679 | ||
1680 | kn->kn_fop = &kqread_filtops; | |
91447636 | 1681 | kqlock(kq); |
55e303ae | 1682 | KNOTE_ATTACH(&kq->kq_sel.si_note, kn); |
91447636 | 1683 | kqunlock(kq); |
55e303ae A |
1684 | return (0); |
1685 | } | |
1686 | ||
1687 | /*ARGSUSED*/ | |
1688 | int | |
91447636 | 1689 | kqueue_stat(struct fileproc *fp, struct stat *st, __unused struct proc *p) |
55e303ae A |
1690 | { |
1691 | struct kqueue *kq = (struct kqueue *)fp->f_data; | |
1692 | ||
1693 | bzero((void *)st, sizeof(*st)); | |
1694 | st->st_size = kq->kq_count; | |
1695 | st->st_blksize = sizeof(struct kevent); | |
1696 | st->st_mode = S_IFIFO; | |
1697 | return (0); | |
1698 | } | |
1699 | ||
91447636 A |
1700 | /* |
1701 | * Called with the kqueue locked | |
1702 | */ | |
55e303ae A |
1703 | static void |
1704 | kqueue_wakeup(struct kqueue *kq) | |
1705 | { | |
1706 | ||
1707 | if (kq->kq_state & KQ_SLEEP) { | |
1708 | kq->kq_state &= ~KQ_SLEEP; | |
91447636 | 1709 | thread_wakeup(kq); |
55e303ae A |
1710 | } |
1711 | if (kq->kq_state & KQ_SEL) { | |
91447636 | 1712 | kq->kq_state &= ~KQ_SEL; |
55e303ae | 1713 | selwakeup(&kq->kq_sel); |
91447636 A |
1714 | } |
1715 | KNOTE(&kq->kq_sel.si_note, 0); | |
55e303ae A |
1716 | } |
1717 | ||
1718 | void | |
1719 | klist_init(struct klist *list) | |
1720 | { | |
1721 | SLIST_INIT(list); | |
1722 | } | |
1723 | ||
91447636 | 1724 | |
55e303ae | 1725 | /* |
91447636 A |
1726 | * Query/Post each knote in the object's list |
1727 | * | |
1728 | * The object lock protects the list. It is assumed | |
1729 | * that the filter/event routine for the object can | |
1730 | * determine that the object is already locked (via | |
1731 | * the hind) and not deadlock itself. | |
1732 | * | |
1733 | * The object lock should also hold off pending | |
1734 | * detach/drop operations. But we'll prevent it here | |
1735 | * too - just in case. | |
55e303ae A |
1736 | */ |
1737 | void | |
1738 | knote(struct klist *list, long hint) | |
1739 | { | |
1740 | struct knote *kn; | |
1741 | ||
91447636 A |
1742 | SLIST_FOREACH(kn, list, kn_selnext) { |
1743 | struct kqueue *kq = kn->kn_kq; | |
1744 | ||
1745 | kqlock(kq); | |
1746 | if (kqlock2knoteuse(kq, kn)) { | |
1747 | int result; | |
1748 | ||
1749 | /* call the event with only a use count */ | |
1750 | result = kn->kn_fop->f_event(kn, hint); | |
1751 | ||
1752 | /* if its not going away and triggered */ | |
1753 | if (knoteuse2kqlock(kq, kn) && result) | |
1754 | knote_activate(kn); | |
1755 | /* lock held again */ | |
1756 | } | |
1757 | kqunlock(kq); | |
1758 | } | |
55e303ae A |
1759 | } |
1760 | ||
1761 | /* | |
1762 | * attach a knote to the specified list. Return true if this is the first entry. | |
91447636 | 1763 | * The list is protected by whatever lock the object it is associated with uses. |
55e303ae A |
1764 | */ |
1765 | int | |
1766 | knote_attach(struct klist *list, struct knote *kn) | |
1767 | { | |
1768 | int ret = SLIST_EMPTY(list); | |
1769 | SLIST_INSERT_HEAD(list, kn, kn_selnext); | |
1770 | return ret; | |
1771 | } | |
1772 | ||
1773 | /* | |
1774 | * detach a knote from the specified list. Return true if that was the last entry. | |
91447636 | 1775 | * The list is protected by whatever lock the object it is associated with uses. |
55e303ae A |
1776 | */ |
1777 | int | |
1778 | knote_detach(struct klist *list, struct knote *kn) | |
1779 | { | |
1780 | SLIST_REMOVE(list, kn, knote, kn_selnext); | |
1781 | return SLIST_EMPTY(list); | |
1782 | } | |
1783 | ||
1784 | /* | |
91447636 A |
1785 | * remove all knotes referencing a specified fd |
1786 | * | |
1787 | * Essentially an inlined knote_remove & knote_drop | |
1788 | * when we know for sure that the thing is a file | |
1789 | * | |
1790 | * Entered with the proc_fd lock already held. | |
1791 | * It returns the same way, but may drop it temporarily. | |
55e303ae A |
1792 | */ |
1793 | void | |
91447636 | 1794 | knote_fdclose(struct proc *p, int fd) |
55e303ae | 1795 | { |
91447636 A |
1796 | struct filedesc *fdp = p->p_fd; |
1797 | struct klist *list; | |
55e303ae A |
1798 | struct knote *kn; |
1799 | ||
91447636 | 1800 | list = &fdp->fd_knlist[fd]; |
55e303ae | 1801 | while ((kn = SLIST_FIRST(list)) != NULL) { |
91447636 | 1802 | struct kqueue *kq = kn->kn_kq; |
55e303ae | 1803 | |
91447636 A |
1804 | kqlock(kq); |
1805 | proc_fdunlock(p); | |
1806 | ||
1807 | /* | |
1808 | * Convert the lock to a drop ref. | |
1809 | * If we get it, go ahead and drop it. | |
1810 | * Otherwise, we waited for it to | |
1811 | * be dropped by the other guy, so | |
1812 | * it is safe to move on in the list. | |
1813 | */ | |
1814 | if (kqlock2knotedrop(kq, kn)) { | |
1815 | kn->kn_fop->f_detach(kn); | |
1816 | knote_drop(kn, p); | |
1817 | } | |
1818 | ||
1819 | proc_fdlock(p); | |
55e303ae | 1820 | |
91447636 A |
1821 | /* the fd tables may have changed - start over */ |
1822 | list = &fdp->fd_knlist[fd]; | |
1823 | } | |
55e303ae A |
1824 | } |
1825 | ||
91447636 A |
1826 | /* proc_fdlock held on entry (and exit) */ |
1827 | static int | |
1828 | knote_fdpattach(struct knote *kn, struct filedesc *fdp, __unused struct proc *p) | |
55e303ae | 1829 | { |
91447636 | 1830 | struct klist *list = NULL; |
55e303ae A |
1831 | |
1832 | if (! kn->kn_fop->f_isfd) { | |
1833 | if (fdp->fd_knhashmask == 0) | |
1834 | fdp->fd_knhash = hashinit(KN_HASHSIZE, M_KQUEUE, | |
1835 | &fdp->fd_knhashmask); | |
1836 | list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)]; | |
91447636 A |
1837 | } else { |
1838 | if ((u_int)fdp->fd_knlistsize <= kn->kn_id) { | |
1839 | u_int size = 0; | |
1840 | ||
1841 | /* have to grow the fd_knlist */ | |
1842 | size = fdp->fd_knlistsize; | |
1843 | while (size <= kn->kn_id) | |
1844 | size += KQEXTENT; | |
1845 | MALLOC(list, struct klist *, | |
1846 | size * sizeof(struct klist *), M_KQUEUE, M_WAITOK); | |
1847 | if (list == NULL) | |
1848 | return (ENOMEM); | |
1849 | ||
1850 | bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list, | |
1851 | fdp->fd_knlistsize * sizeof(struct klist *)); | |
1852 | bzero((caddr_t)list + | |
1853 | fdp->fd_knlistsize * sizeof(struct klist *), | |
1854 | (size - fdp->fd_knlistsize) * sizeof(struct klist *)); | |
55e303ae | 1855 | FREE(fdp->fd_knlist, M_KQUEUE); |
91447636 A |
1856 | fdp->fd_knlist = list; |
1857 | fdp->fd_knlistsize = size; | |
1858 | } | |
1859 | list = &fdp->fd_knlist[kn->kn_id]; | |
55e303ae | 1860 | } |
55e303ae | 1861 | SLIST_INSERT_HEAD(list, kn, kn_link); |
91447636 | 1862 | return (0); |
55e303ae A |
1863 | } |
1864 | ||
91447636 A |
1865 | |
1866 | ||
55e303ae A |
1867 | /* |
1868 | * should be called at spl == 0, since we don't want to hold spl | |
1869 | * while calling fdrop and free. | |
1870 | */ | |
1871 | static void | |
1872 | knote_drop(struct knote *kn, struct proc *p) | |
1873 | { | |
1874 | struct filedesc *fdp = p->p_fd; | |
91447636 | 1875 | struct kqueue *kq = kn->kn_kq; |
55e303ae A |
1876 | struct klist *list; |
1877 | ||
91447636 | 1878 | proc_fdlock(p); |
55e303ae A |
1879 | if (kn->kn_fop->f_isfd) |
1880 | list = &fdp->fd_knlist[kn->kn_id]; | |
1881 | else | |
1882 | list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)]; | |
1883 | ||
1884 | SLIST_REMOVE(list, kn, knote, kn_link); | |
91447636 A |
1885 | kqlock(kq); |
1886 | knote_dequeue(kn); | |
1887 | if (kn->kn_status & KN_DROPWAIT) | |
1888 | thread_wakeup(&kn->kn_status); | |
1889 | kqunlock(kq); | |
1890 | proc_fdunlock(p); | |
1891 | ||
55e303ae | 1892 | if (kn->kn_fop->f_isfd) |
91447636 A |
1893 | fp_drop(p, kn->kn_id, kn->kn_fp, 0); |
1894 | ||
55e303ae A |
1895 | knote_free(kn); |
1896 | } | |
1897 | ||
91447636 A |
1898 | /* called with kqueue lock held */ |
1899 | static void | |
1900 | knote_activate(struct knote *kn) | |
1901 | { | |
1902 | struct kqueue *kq = kn->kn_kq; | |
1903 | ||
1904 | kn->kn_status |= KN_ACTIVE; | |
1905 | knote_enqueue(kn); | |
1906 | kqueue_wakeup(kq); | |
1907 | } | |
1908 | ||
1909 | /* called with kqueue lock held */ | |
1910 | static void | |
1911 | knote_deactivate(struct knote *kn) | |
1912 | { | |
1913 | kn->kn_status &= ~KN_ACTIVE; | |
1914 | knote_dequeue(kn); | |
1915 | } | |
55e303ae | 1916 | |
91447636 | 1917 | /* called with kqueue lock held */ |
55e303ae A |
1918 | static void |
1919 | knote_enqueue(struct knote *kn) | |
1920 | { | |
1921 | struct kqueue *kq = kn->kn_kq; | |
55e303ae | 1922 | |
91447636 A |
1923 | if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) { |
1924 | struct kqtailq *tq = kn->kn_tq; | |
55e303ae | 1925 | |
91447636 A |
1926 | TAILQ_INSERT_TAIL(tq, kn, kn_tqe); |
1927 | kn->kn_status |= KN_QUEUED; | |
1928 | kq->kq_count++; | |
1929 | } | |
55e303ae A |
1930 | } |
1931 | ||
91447636 | 1932 | /* called with kqueue lock held */ |
55e303ae A |
1933 | static void |
1934 | knote_dequeue(struct knote *kn) | |
1935 | { | |
1936 | struct kqueue *kq = kn->kn_kq; | |
55e303ae | 1937 | |
91447636 A |
1938 | assert((kn->kn_status & KN_DISABLED) == 0); |
1939 | if ((kn->kn_status & KN_QUEUED) == KN_QUEUED) { | |
1940 | struct kqtailq *tq = kn->kn_tq; | |
55e303ae | 1941 | |
91447636 A |
1942 | TAILQ_REMOVE(tq, kn, kn_tqe); |
1943 | kn->kn_tq = &kq->kq_head; | |
1944 | kn->kn_status &= ~KN_QUEUED; | |
1945 | kq->kq_count--; | |
1946 | } | |
55e303ae A |
1947 | } |
1948 | ||
1949 | void | |
1950 | knote_init(void) | |
1951 | { | |
1952 | knote_zone = zinit(sizeof(struct knote), 8192*sizeof(struct knote), 8192, "knote zone"); | |
91447636 A |
1953 | |
1954 | /* allocate kq lock group attribute and group */ | |
1955 | kq_lck_grp_attr= lck_grp_attr_alloc_init(); | |
21362eb3 | 1956 | lck_grp_attr_setstat(kq_lck_grp_attr); |
91447636 A |
1957 | |
1958 | kq_lck_grp = lck_grp_alloc_init("kqueue", kq_lck_grp_attr); | |
1959 | ||
1960 | /* Allocate kq lock attribute */ | |
1961 | kq_lck_attr = lck_attr_alloc_init(); | |
21362eb3 | 1962 | lck_attr_setdefault(kq_lck_attr); |
91447636 A |
1963 | |
1964 | /* Initialize the timer filter lock */ | |
1965 | lck_mtx_init(&_filt_timerlock, kq_lck_grp, kq_lck_attr); | |
55e303ae A |
1966 | } |
1967 | SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL) | |
1968 | ||
1969 | static struct knote * | |
1970 | knote_alloc(void) | |
1971 | { | |
1972 | return ((struct knote *)zalloc(knote_zone)); | |
1973 | } | |
1974 | ||
1975 | static void | |
1976 | knote_free(struct knote *kn) | |
1977 | { | |
91447636 | 1978 | zfree(knote_zone, kn); |
55e303ae A |
1979 | } |
1980 | ||
1c79356b A |
1981 | #include <sys/param.h> |
1982 | #include <sys/socket.h> | |
1983 | #include <sys/protosw.h> | |
1984 | #include <sys/domain.h> | |
1985 | #include <sys/mbuf.h> | |
1986 | #include <sys/kern_event.h> | |
1987 | #include <sys/malloc.h> | |
9bccf70c A |
1988 | #include <sys/sys_domain.h> |
1989 | #include <sys/syslog.h> | |
1c79356b A |
1990 | |
1991 | ||
91447636 A |
1992 | static int kev_attach(struct socket *so, int proto, struct proc *p); |
1993 | static int kev_detach(struct socket *so); | |
1994 | static int kev_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, struct proc *p); | |
1995 | ||
1996 | struct pr_usrreqs event_usrreqs = { | |
1997 | pru_abort_notsupp, pru_accept_notsupp, kev_attach, pru_bind_notsupp, pru_connect_notsupp, | |
1998 | pru_connect2_notsupp, kev_control, kev_detach, pru_disconnect_notsupp, | |
1999 | pru_listen_notsupp, pru_peeraddr_notsupp, pru_rcvd_notsupp, pru_rcvoob_notsupp, | |
2000 | pru_send_notsupp, pru_sense_null, pru_shutdown_notsupp, pru_sockaddr_notsupp, | |
2001 | pru_sosend_notsupp, soreceive, pru_sopoll_notsupp | |
2002 | }; | |
1c79356b A |
2003 | |
2004 | struct protosw eventsw[] = { | |
2005 | { | |
2006 | SOCK_RAW, &systemdomain, SYSPROTO_EVENT, PR_ATOMIC, | |
2007 | 0, 0, 0, 0, | |
2008 | 0, | |
2009 | 0, 0, 0, 0, | |
91447636 A |
2010 | #if __APPLE__ |
2011 | 0, | |
2012 | #endif | |
2013 | &event_usrreqs, | |
2014 | 0, 0, 0, | |
2015 | #if __APPLE__ | |
2016 | {0, 0}, 0, {0} | |
2017 | #endif | |
1c79356b A |
2018 | } |
2019 | }; | |
2020 | ||
2021 | static | |
2022 | struct kern_event_head kern_event_head; | |
2023 | ||
2024 | static u_long static_event_id = 0; | |
91447636 | 2025 | struct domain *sysdom = &systemdomain; |
1c79356b | 2026 | |
91447636 A |
2027 | static lck_grp_t *evt_mtx_grp; |
2028 | static lck_attr_t *evt_mtx_attr; | |
2029 | static lck_grp_attr_t *evt_mtx_grp_attr; | |
2030 | lck_mtx_t *evt_mutex; | |
9bccf70c A |
2031 | /* |
2032 | * Install the protosw's for the NKE manager. Invoked at | |
2033 | * extension load time | |
2034 | */ | |
2035 | int | |
2036 | kern_event_init(void) | |
2037 | { | |
2038 | int retval; | |
2039 | ||
91447636 A |
2040 | if ((retval = net_add_proto(eventsw, &systemdomain)) != 0) { |
2041 | log(LOG_WARNING, "Can't install kernel events protocol (%d)\n", retval); | |
2042 | return(retval); | |
2043 | } | |
9bccf70c | 2044 | |
91447636 A |
2045 | /* |
2046 | * allocate lock group attribute and group for kern event | |
2047 | */ | |
2048 | evt_mtx_grp_attr = lck_grp_attr_alloc_init(); | |
2049 | ||
2050 | evt_mtx_grp = lck_grp_alloc_init("eventlist", evt_mtx_grp_attr); | |
2051 | ||
2052 | /* | |
2053 | * allocate the lock attribute for mutexes | |
2054 | */ | |
2055 | evt_mtx_attr = lck_attr_alloc_init(); | |
21362eb3 | 2056 | lck_attr_setdefault(evt_mtx_attr); |
91447636 A |
2057 | evt_mutex = lck_mtx_alloc_init(evt_mtx_grp, evt_mtx_attr); |
2058 | if (evt_mutex == NULL) | |
2059 | return (ENOMEM); | |
2060 | ||
2061 | return(KERN_SUCCESS); | |
9bccf70c A |
2062 | } |
2063 | ||
91447636 A |
2064 | static int |
2065 | kev_attach(struct socket *so, __unused int proto, __unused struct proc *p) | |
1c79356b A |
2066 | { |
2067 | int error; | |
2068 | struct kern_event_pcb *ev_pcb; | |
2069 | ||
55e303ae A |
2070 | error = soreserve(so, KEV_SNDSPACE, KEV_RECVSPACE); |
2071 | if (error) | |
2072 | return error; | |
2073 | ||
91447636 | 2074 | MALLOC(ev_pcb, struct kern_event_pcb *, sizeof(struct kern_event_pcb), M_PCB, M_WAITOK); |
1c79356b A |
2075 | if (ev_pcb == 0) |
2076 | return ENOBUFS; | |
2077 | ||
2078 | ev_pcb->ev_socket = so; | |
2079 | ev_pcb->vendor_code_filter = 0xffffffff; | |
2080 | ||
2081 | so->so_pcb = (caddr_t) ev_pcb; | |
91447636 | 2082 | lck_mtx_lock(evt_mutex); |
1c79356b | 2083 | LIST_INSERT_HEAD(&kern_event_head, ev_pcb, ev_link); |
91447636 | 2084 | lck_mtx_unlock(evt_mutex); |
1c79356b A |
2085 | |
2086 | return 0; | |
2087 | } | |
2088 | ||
2089 | ||
91447636 A |
2090 | static int |
2091 | kev_detach(struct socket *so) | |
1c79356b A |
2092 | { |
2093 | struct kern_event_pcb *ev_pcb = (struct kern_event_pcb *) so->so_pcb; | |
2094 | ||
55e303ae | 2095 | if (ev_pcb != 0) { |
91447636 A |
2096 | lck_mtx_lock(evt_mutex); |
2097 | LIST_REMOVE(ev_pcb, ev_link); | |
2098 | lck_mtx_unlock(evt_mutex); | |
2099 | FREE(ev_pcb, M_PCB); | |
2100 | so->so_pcb = 0; | |
2101 | so->so_flags |= SOF_PCBCLEARING; | |
55e303ae | 2102 | } |
1c79356b A |
2103 | |
2104 | return 0; | |
2105 | } | |
2106 | ||
91447636 A |
2107 | /* |
2108 | * For now, kev_vender_code and mbuf_tags use the same | |
2109 | * mechanism. | |
2110 | */ | |
2111 | extern errno_t mbuf_tag_id_find_internal(const char *string, u_long *out_id, | |
2112 | int create); | |
2113 | ||
2114 | errno_t kev_vendor_code_find( | |
2115 | const char *string, | |
2116 | u_long *out_vender_code) | |
2117 | { | |
2118 | if (strlen(string) >= KEV_VENDOR_CODE_MAX_STR_LEN) { | |
2119 | return EINVAL; | |
2120 | } | |
2121 | return mbuf_tag_id_find_internal(string, out_vender_code, 1); | |
2122 | } | |
2123 | ||
2124 | extern void mbuf_tag_id_first_last(u_long *first, u_long *last); | |
2125 | ||
2126 | errno_t kev_msg_post(struct kev_msg *event_msg) | |
2127 | { | |
2128 | u_long min_vendor, max_vendor; | |
2129 | ||
2130 | mbuf_tag_id_first_last(&min_vendor, &max_vendor); | |
2131 | ||
2132 | if (event_msg == NULL) | |
2133 | return EINVAL; | |
2134 | ||
2135 | /* Limit third parties to posting events for registered vendor codes only */ | |
2136 | if (event_msg->vendor_code < min_vendor || | |
2137 | event_msg->vendor_code > max_vendor) | |
2138 | { | |
2139 | return EINVAL; | |
2140 | } | |
2141 | ||
2142 | return kev_post_msg(event_msg); | |
2143 | } | |
2144 | ||
1c79356b A |
2145 | |
2146 | int kev_post_msg(struct kev_msg *event_msg) | |
2147 | { | |
2148 | struct mbuf *m, *m2; | |
2149 | struct kern_event_pcb *ev_pcb; | |
2150 | struct kern_event_msg *ev; | |
2151 | char *tmp; | |
91447636 | 2152 | unsigned long total_size; |
1c79356b A |
2153 | int i; |
2154 | ||
91447636 A |
2155 | /* Verify the message is small enough to fit in one mbuf w/o cluster */ |
2156 | total_size = KEV_MSG_HEADER_SIZE; | |
2157 | ||
2158 | for (i = 0; i < 5; i++) { | |
2159 | if (event_msg->dv[i].data_length == 0) | |
2160 | break; | |
2161 | total_size += event_msg->dv[i].data_length; | |
2162 | } | |
2163 | ||
2164 | if (total_size > MLEN) { | |
2165 | return EMSGSIZE; | |
2166 | } | |
1c79356b A |
2167 | |
2168 | m = m_get(M_DONTWAIT, MT_DATA); | |
2169 | if (m == 0) | |
2170 | return ENOBUFS; | |
2171 | ||
2172 | ev = mtod(m, struct kern_event_msg *); | |
2173 | total_size = KEV_MSG_HEADER_SIZE; | |
2174 | ||
2175 | tmp = (char *) &ev->event_data[0]; | |
2176 | for (i = 0; i < 5; i++) { | |
2177 | if (event_msg->dv[i].data_length == 0) | |
2178 | break; | |
2179 | ||
2180 | total_size += event_msg->dv[i].data_length; | |
2181 | bcopy(event_msg->dv[i].data_ptr, tmp, | |
2182 | event_msg->dv[i].data_length); | |
2183 | tmp += event_msg->dv[i].data_length; | |
2184 | } | |
2185 | ||
1c79356b A |
2186 | ev->id = ++static_event_id; |
2187 | ev->total_size = total_size; | |
2188 | ev->vendor_code = event_msg->vendor_code; | |
2189 | ev->kev_class = event_msg->kev_class; | |
2190 | ev->kev_subclass = event_msg->kev_subclass; | |
2191 | ev->event_code = event_msg->event_code; | |
2192 | ||
2193 | m->m_len = total_size; | |
91447636 | 2194 | lck_mtx_lock(evt_mutex); |
1c79356b A |
2195 | for (ev_pcb = LIST_FIRST(&kern_event_head); |
2196 | ev_pcb; | |
2197 | ev_pcb = LIST_NEXT(ev_pcb, ev_link)) { | |
2198 | ||
2199 | if (ev_pcb->vendor_code_filter != KEV_ANY_VENDOR) { | |
2200 | if (ev_pcb->vendor_code_filter != ev->vendor_code) | |
2201 | continue; | |
2202 | ||
2203 | if (ev_pcb->class_filter != KEV_ANY_CLASS) { | |
2204 | if (ev_pcb->class_filter != ev->kev_class) | |
2205 | continue; | |
2206 | ||
2207 | if ((ev_pcb->subclass_filter != KEV_ANY_SUBCLASS) && | |
2208 | (ev_pcb->subclass_filter != ev->kev_subclass)) | |
2209 | continue; | |
2210 | } | |
2211 | } | |
2212 | ||
2213 | m2 = m_copym(m, 0, m->m_len, M_NOWAIT); | |
2214 | if (m2 == 0) { | |
2215 | m_free(m); | |
91447636 | 2216 | lck_mtx_unlock(evt_mutex); |
1c79356b A |
2217 | return ENOBUFS; |
2218 | } | |
91447636 A |
2219 | socket_lock(ev_pcb->ev_socket, 1); |
2220 | if (sbappendrecord(&ev_pcb->ev_socket->so_rcv, m2)) | |
2221 | sorwakeup(ev_pcb->ev_socket); | |
2222 | socket_unlock(ev_pcb->ev_socket, 1); | |
1c79356b A |
2223 | } |
2224 | ||
1c79356b | 2225 | m_free(m); |
91447636 | 2226 | lck_mtx_unlock(evt_mutex); |
1c79356b A |
2227 | return 0; |
2228 | } | |
2229 | ||
91447636 A |
2230 | static int |
2231 | kev_control(struct socket *so, | |
2232 | u_long cmd, | |
2233 | caddr_t data, | |
2234 | __unused struct ifnet *ifp, | |
2235 | __unused struct proc *p) | |
1c79356b | 2236 | { |
91447636 A |
2237 | struct kev_request *kev_req = (struct kev_request *) data; |
2238 | struct kern_event_pcb *ev_pcb; | |
2239 | struct kev_vendor_code *kev_vendor; | |
2240 | u_long *id_value = (u_long *) data; | |
2241 | ||
2242 | ||
2243 | switch (cmd) { | |
2244 | ||
2245 | case SIOCGKEVID: | |
2246 | *id_value = static_event_id; | |
2247 | break; | |
2248 | ||
2249 | case SIOCSKEVFILT: | |
2250 | ev_pcb = (struct kern_event_pcb *) so->so_pcb; | |
2251 | ev_pcb->vendor_code_filter = kev_req->vendor_code; | |
2252 | ev_pcb->class_filter = kev_req->kev_class; | |
2253 | ev_pcb->subclass_filter = kev_req->kev_subclass; | |
2254 | break; | |
2255 | ||
2256 | case SIOCGKEVFILT: | |
2257 | ev_pcb = (struct kern_event_pcb *) so->so_pcb; | |
2258 | kev_req->vendor_code = ev_pcb->vendor_code_filter; | |
2259 | kev_req->kev_class = ev_pcb->class_filter; | |
2260 | kev_req->kev_subclass = ev_pcb->subclass_filter; | |
2261 | break; | |
2262 | ||
2263 | case SIOCGKEVVENDOR: | |
2264 | kev_vendor = (struct kev_vendor_code*)data; | |
2265 | ||
2266 | /* Make sure string is NULL terminated */ | |
2267 | kev_vendor->vendor_string[KEV_VENDOR_CODE_MAX_STR_LEN-1] = 0; | |
2268 | ||
2269 | return mbuf_tag_id_find_internal(kev_vendor->vendor_string, | |
2270 | &kev_vendor->vendor_code, 0); | |
2271 | ||
2272 | default: | |
2273 | return ENOTSUP; | |
2274 | } | |
2275 | ||
2276 | return 0; | |
1c79356b A |
2277 | } |
2278 | ||
2279 | ||
1c79356b A |
2280 | |
2281 |