kern_event.c revision 1.60.6.2.2.1 1 /* $NetBSD: kern_event.c,v 1.60.6.2.2.1 2011/11/19 22:22:56 sborrill Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon (at) FreeBSD.org>
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * FreeBSD: src/sys/kern/kern_event.c,v 1.27 2001/07/05 17:10:44 rwatson Exp
58 */
59
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.60.6.2.2.1 2011/11/19 22:22:56 sborrill Exp $");
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/proc.h>
67 #include <sys/file.h>
68 #include <sys/select.h>
69 #include <sys/queue.h>
70 #include <sys/event.h>
71 #include <sys/eventvar.h>
72 #include <sys/poll.h>
73 #include <sys/kmem.h>
74 #include <sys/stat.h>
75 #include <sys/filedesc.h>
76 #include <sys/syscallargs.h>
77 #include <sys/kauth.h>
78 #include <sys/conf.h>
79 #include <sys/atomic.h>
80
81 static int kqueue_scan(file_t *, size_t, struct kevent *,
82 const struct timespec *, register_t *,
83 const struct kevent_ops *, struct kevent *,
84 size_t);
85 static int kqueue_ioctl(file_t *, u_long, void *);
86 static int kqueue_fcntl(file_t *, u_int, void *);
87 static int kqueue_poll(file_t *, int);
88 static int kqueue_kqfilter(file_t *, struct knote *);
89 static int kqueue_stat(file_t *, struct stat *);
90 static int kqueue_close(file_t *);
91 static int kqueue_register(struct kqueue *, struct kevent *);
92 static void kqueue_doclose(struct kqueue *, struct klist *, int);
93
94 static void knote_detach(struct knote *, filedesc_t *fdp, bool);
95 static void knote_enqueue(struct knote *);
96 static void knote_activate(struct knote *);
97
98 static void filt_kqdetach(struct knote *);
99 static int filt_kqueue(struct knote *, long hint);
100 static int filt_procattach(struct knote *);
101 static void filt_procdetach(struct knote *);
102 static int filt_proc(struct knote *, long hint);
103 static int filt_fileattach(struct knote *);
104 static void filt_timerexpire(void *x);
105 static int filt_timerattach(struct knote *);
106 static void filt_timerdetach(struct knote *);
107 static int filt_timer(struct knote *, long hint);
108
109 static const struct fileops kqueueops = {
110 .fo_read = (void *)enxio,
111 .fo_write = (void *)enxio,
112 .fo_ioctl = kqueue_ioctl,
113 .fo_fcntl = kqueue_fcntl,
114 .fo_poll = kqueue_poll,
115 .fo_stat = kqueue_stat,
116 .fo_close = kqueue_close,
117 .fo_kqfilter = kqueue_kqfilter,
118 .fo_drain = fnullop_drain,
119 };
120
121 static const struct filterops kqread_filtops =
122 { 1, NULL, filt_kqdetach, filt_kqueue };
123 static const struct filterops proc_filtops =
124 { 0, filt_procattach, filt_procdetach, filt_proc };
125 static const struct filterops file_filtops =
126 { 1, filt_fileattach, NULL, NULL };
127 static const struct filterops timer_filtops =
128 { 0, filt_timerattach, filt_timerdetach, filt_timer };
129
130 static u_int kq_ncallouts = 0;
131 static int kq_calloutmax = (4 * 1024);
132
133 #define KN_HASHSIZE 64 /* XXX should be tunable */
134 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
135
136 extern const struct filterops sig_filtops;
137
138 /*
139 * Table for for all system-defined filters.
140 * These should be listed in the numeric order of the EVFILT_* defines.
141 * If filtops is NULL, the filter isn't implemented in NetBSD.
142 * End of list is when name is NULL.
143 *
144 * Note that 'refcnt' is meaningless for built-in filters.
145 */
146 struct kfilter {
147 const char *name; /* name of filter */
148 uint32_t filter; /* id of filter */
149 unsigned refcnt; /* reference count */
150 const struct filterops *filtops;/* operations for filter */
151 size_t namelen; /* length of name string */
152 };
153
154 /* System defined filters */
155 static struct kfilter sys_kfilters[] = {
156 { "EVFILT_READ", EVFILT_READ, 0, &file_filtops, 0 },
157 { "EVFILT_WRITE", EVFILT_WRITE, 0, &file_filtops, 0, },
158 { "EVFILT_AIO", EVFILT_AIO, 0, NULL, 0 },
159 { "EVFILT_VNODE", EVFILT_VNODE, 0, &file_filtops, 0 },
160 { "EVFILT_PROC", EVFILT_PROC, 0, &proc_filtops, 0 },
161 { "EVFILT_SIGNAL", EVFILT_SIGNAL, 0, &sig_filtops, 0 },
162 { "EVFILT_TIMER", EVFILT_TIMER, 0, &timer_filtops, 0 },
163 { NULL, 0, 0, NULL, 0 },
164 };
165
166 /* User defined kfilters */
167 static struct kfilter *user_kfilters; /* array */
168 static int user_kfilterc; /* current offset */
169 static int user_kfiltermaxc; /* max size so far */
170 static size_t user_kfiltersz; /* size of allocated memory */
171
172 /* Locks */
173 static krwlock_t kqueue_filter_lock; /* lock on filter lists */
174 static kmutex_t kqueue_misc_lock; /* miscellaneous */
175
176 /*
177 * Initialize the kqueue subsystem.
178 */
179 void
180 kqueue_init(void)
181 {
182
183 rw_init(&kqueue_filter_lock);
184 mutex_init(&kqueue_misc_lock, MUTEX_DEFAULT, IPL_NONE);
185 }
186
187 /*
188 * Find kfilter entry by name, or NULL if not found.
189 */
190 static struct kfilter *
191 kfilter_byname_sys(const char *name)
192 {
193 int i;
194
195 KASSERT(rw_lock_held(&kqueue_filter_lock));
196
197 for (i = 0; sys_kfilters[i].name != NULL; i++) {
198 if (strcmp(name, sys_kfilters[i].name) == 0)
199 return &sys_kfilters[i];
200 }
201 return NULL;
202 }
203
204 static struct kfilter *
205 kfilter_byname_user(const char *name)
206 {
207 int i;
208
209 KASSERT(rw_lock_held(&kqueue_filter_lock));
210
211 /* user filter slots have a NULL name if previously deregistered */
212 for (i = 0; i < user_kfilterc ; i++) {
213 if (user_kfilters[i].name != NULL &&
214 strcmp(name, user_kfilters[i].name) == 0)
215 return &user_kfilters[i];
216 }
217 return NULL;
218 }
219
220 static struct kfilter *
221 kfilter_byname(const char *name)
222 {
223 struct kfilter *kfilter;
224
225 KASSERT(rw_lock_held(&kqueue_filter_lock));
226
227 if ((kfilter = kfilter_byname_sys(name)) != NULL)
228 return kfilter;
229
230 return kfilter_byname_user(name);
231 }
232
233 /*
234 * Find kfilter entry by filter id, or NULL if not found.
235 * Assumes entries are indexed in filter id order, for speed.
236 */
237 static struct kfilter *
238 kfilter_byfilter(uint32_t filter)
239 {
240 struct kfilter *kfilter;
241
242 KASSERT(rw_lock_held(&kqueue_filter_lock));
243
244 if (filter < EVFILT_SYSCOUNT) /* it's a system filter */
245 kfilter = &sys_kfilters[filter];
246 else if (user_kfilters != NULL &&
247 filter < EVFILT_SYSCOUNT + user_kfilterc)
248 /* it's a user filter */
249 kfilter = &user_kfilters[filter - EVFILT_SYSCOUNT];
250 else
251 return (NULL); /* out of range */
252 KASSERT(kfilter->filter == filter); /* sanity check! */
253 return (kfilter);
254 }
255
256 /*
257 * Register a new kfilter. Stores the entry in user_kfilters.
258 * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
259 * If retfilter != NULL, the new filterid is returned in it.
260 */
261 int
262 kfilter_register(const char *name, const struct filterops *filtops,
263 int *retfilter)
264 {
265 struct kfilter *kfilter;
266 size_t len;
267 int i;
268
269 if (name == NULL || name[0] == '\0' || filtops == NULL)
270 return (EINVAL); /* invalid args */
271
272 rw_enter(&kqueue_filter_lock, RW_WRITER);
273 if (kfilter_byname(name) != NULL) {
274 rw_exit(&kqueue_filter_lock);
275 return (EEXIST); /* already exists */
276 }
277 if (user_kfilterc > 0xffffffff - EVFILT_SYSCOUNT) {
278 rw_exit(&kqueue_filter_lock);
279 return (EINVAL); /* too many */
280 }
281
282 for (i = 0; i < user_kfilterc; i++) {
283 kfilter = &user_kfilters[i];
284 if (kfilter->name == NULL) {
285 /* Previously deregistered slot. Reuse. */
286 goto reuse;
287 }
288 }
289
290 /* check if need to grow user_kfilters */
291 if (user_kfilterc + 1 > user_kfiltermaxc) {
292 /* Grow in KFILTER_EXTENT chunks. */
293 user_kfiltermaxc += KFILTER_EXTENT;
294 len = user_kfiltermaxc * sizeof(*kfilter);
295 kfilter = kmem_alloc(len, KM_SLEEP);
296 memset((char *)kfilter + user_kfiltersz, 0, len - user_kfiltersz);
297 if (user_kfilters != NULL) {
298 memcpy(kfilter, user_kfilters, user_kfiltersz);
299 kmem_free(user_kfilters, user_kfiltersz);
300 }
301 user_kfiltersz = len;
302 user_kfilters = kfilter;
303 }
304 /* Adding new slot */
305 kfilter = &user_kfilters[user_kfilterc++];
306 reuse:
307 kfilter->namelen = strlen(name) + 1;
308 kfilter->name = kmem_alloc(kfilter->namelen, KM_SLEEP);
309 memcpy(__UNCONST(kfilter->name), name, kfilter->namelen);
310
311 kfilter->filter = (kfilter - user_kfilters) + EVFILT_SYSCOUNT;
312
313 kfilter->filtops = kmem_alloc(sizeof(*filtops), KM_SLEEP);
314 memcpy(__UNCONST(kfilter->filtops), filtops, sizeof(*filtops));
315
316 if (retfilter != NULL)
317 *retfilter = kfilter->filter;
318 rw_exit(&kqueue_filter_lock);
319
320 return (0);
321 }
322
323 /*
324 * Unregister a kfilter previously registered with kfilter_register.
325 * This retains the filter id, but clears the name and frees filtops (filter
326 * operations), so that the number isn't reused during a boot.
327 * Returns 0 if operation succeeded, or an appropriate errno(2) otherwise.
328 */
329 int
330 kfilter_unregister(const char *name)
331 {
332 struct kfilter *kfilter;
333
334 if (name == NULL || name[0] == '\0')
335 return (EINVAL); /* invalid name */
336
337 rw_enter(&kqueue_filter_lock, RW_WRITER);
338 if (kfilter_byname_sys(name) != NULL) {
339 rw_exit(&kqueue_filter_lock);
340 return (EINVAL); /* can't detach system filters */
341 }
342
343 kfilter = kfilter_byname_user(name);
344 if (kfilter == NULL) {
345 rw_exit(&kqueue_filter_lock);
346 return (ENOENT);
347 }
348 if (kfilter->refcnt != 0) {
349 rw_exit(&kqueue_filter_lock);
350 return (EBUSY);
351 }
352
353 /* Cast away const (but we know it's safe. */
354 kmem_free(__UNCONST(kfilter->name), kfilter->namelen);
355 kfilter->name = NULL; /* mark as `not implemented' */
356
357 if (kfilter->filtops != NULL) {
358 /* Cast away const (but we know it's safe. */
359 kmem_free(__UNCONST(kfilter->filtops),
360 sizeof(*kfilter->filtops));
361 kfilter->filtops = NULL; /* mark as `not implemented' */
362 }
363 rw_exit(&kqueue_filter_lock);
364
365 return (0);
366 }
367
368
369 /*
370 * Filter attach method for EVFILT_READ and EVFILT_WRITE on normal file
371 * descriptors. Calls fileops kqfilter method for given file descriptor.
372 */
373 static int
374 filt_fileattach(struct knote *kn)
375 {
376 file_t *fp;
377
378 fp = kn->kn_obj;
379
380 return (*fp->f_ops->fo_kqfilter)(fp, kn);
381 }
382
383 /*
384 * Filter detach method for EVFILT_READ on kqueue descriptor.
385 */
386 static void
387 filt_kqdetach(struct knote *kn)
388 {
389 struct kqueue *kq;
390
391 kq = ((file_t *)kn->kn_obj)->f_data;
392
393 mutex_spin_enter(&kq->kq_lock);
394 SLIST_REMOVE(&kq->kq_sel.sel_klist, kn, knote, kn_selnext);
395 mutex_spin_exit(&kq->kq_lock);
396 }
397
398 /*
399 * Filter event method for EVFILT_READ on kqueue descriptor.
400 */
401 /*ARGSUSED*/
402 static int
403 filt_kqueue(struct knote *kn, long hint)
404 {
405 struct kqueue *kq;
406 int rv;
407
408 kq = ((file_t *)kn->kn_obj)->f_data;
409
410 if (hint != NOTE_SUBMIT)
411 mutex_spin_enter(&kq->kq_lock);
412 kn->kn_data = kq->kq_count;
413 rv = (kn->kn_data > 0);
414 if (hint != NOTE_SUBMIT)
415 mutex_spin_exit(&kq->kq_lock);
416
417 return rv;
418 }
419
420 /*
421 * Filter attach method for EVFILT_PROC.
422 */
423 static int
424 filt_procattach(struct knote *kn)
425 {
426 struct proc *p, *curp;
427 struct lwp *curl;
428
429 curl = curlwp;
430 curp = curl->l_proc;
431
432 mutex_enter(proc_lock);
433 p = p_find(kn->kn_id, PFIND_LOCKED);
434 if (p == NULL) {
435 mutex_exit(proc_lock);
436 return ESRCH;
437 }
438
439 /*
440 * Fail if it's not owned by you, or the last exec gave us
441 * setuid/setgid privs (unless you're root).
442 */
443 mutex_enter(p->p_lock);
444 mutex_exit(proc_lock);
445 if (kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_KEVENT_FILTER,
446 p, NULL, NULL, NULL) != 0) {
447 mutex_exit(p->p_lock);
448 return EACCES;
449 }
450
451 kn->kn_obj = p;
452 kn->kn_flags |= EV_CLEAR; /* automatically set */
453
454 /*
455 * internal flag indicating registration done by kernel
456 */
457 if (kn->kn_flags & EV_FLAG1) {
458 kn->kn_data = kn->kn_sdata; /* ppid */
459 kn->kn_fflags = NOTE_CHILD;
460 kn->kn_flags &= ~EV_FLAG1;
461 }
462 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
463 mutex_exit(p->p_lock);
464
465 return 0;
466 }
467
468 /*
469 * Filter detach method for EVFILT_PROC.
470 *
471 * The knote may be attached to a different process, which may exit,
472 * leaving nothing for the knote to be attached to. So when the process
473 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
474 * it will be deleted when read out. However, as part of the knote deletion,
475 * this routine is called, so a check is needed to avoid actually performing
476 * a detach, because the original process might not exist any more.
477 */
478 static void
479 filt_procdetach(struct knote *kn)
480 {
481 struct proc *p;
482
483 if (kn->kn_status & KN_DETACHED)
484 return;
485
486 p = kn->kn_obj;
487
488 mutex_enter(p->p_lock);
489 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
490 mutex_exit(p->p_lock);
491 }
492
493 /*
494 * Filter event method for EVFILT_PROC.
495 */
496 static int
497 filt_proc(struct knote *kn, long hint)
498 {
499 u_int event, fflag;
500 struct kevent kev;
501 struct kqueue *kq;
502 int error;
503
504 event = (u_int)hint & NOTE_PCTRLMASK;
505 kq = kn->kn_kq;
506 fflag = 0;
507
508 /* If the user is interested in this event, record it. */
509 if (kn->kn_sfflags & event)
510 fflag |= event;
511
512 if (event == NOTE_EXIT) {
513 /*
514 * Process is gone, so flag the event as finished.
515 *
516 * Detach the knote from watched process and mark
517 * it as such. We can't leave this to kqueue_scan(),
518 * since the process might not exist by then. And we
519 * have to do this now, since psignal KNOTE() is called
520 * also for zombies and we might end up reading freed
521 * memory if the kevent would already be picked up
522 * and knote g/c'ed.
523 */
524 filt_procdetach(kn);
525
526 mutex_spin_enter(&kq->kq_lock);
527 kn->kn_status |= KN_DETACHED;
528 /* Mark as ONESHOT, so that the knote it g/c'ed when read */
529 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
530 kn->kn_fflags |= fflag;
531 mutex_spin_exit(&kq->kq_lock);
532
533 return 1;
534 }
535
536 mutex_spin_enter(&kq->kq_lock);
537 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
538 /*
539 * Process forked, and user wants to track the new process,
540 * so attach a new knote to it, and immediately report an
541 * event with the parent's pid. Register knote with new
542 * process.
543 */
544 kev.ident = hint & NOTE_PDATAMASK; /* pid */
545 kev.filter = kn->kn_filter;
546 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
547 kev.fflags = kn->kn_sfflags;
548 kev.data = kn->kn_id; /* parent */
549 kev.udata = kn->kn_kevent.udata; /* preserve udata */
550 mutex_spin_exit(&kq->kq_lock);
551 error = kqueue_register(kq, &kev);
552 mutex_spin_enter(&kq->kq_lock);
553 if (error != 0)
554 kn->kn_fflags |= NOTE_TRACKERR;
555 }
556 kn->kn_fflags |= fflag;
557 fflag = kn->kn_fflags;
558 mutex_spin_exit(&kq->kq_lock);
559
560 return fflag != 0;
561 }
562
563 static void
564 filt_timerexpire(void *knx)
565 {
566 struct knote *kn = knx;
567 int tticks;
568
569 mutex_enter(&kqueue_misc_lock);
570 kn->kn_data++;
571 knote_activate(kn);
572 if ((kn->kn_flags & EV_ONESHOT) == 0) {
573 tticks = mstohz(kn->kn_sdata);
574 callout_schedule((callout_t *)kn->kn_hook, tticks);
575 }
576 mutex_exit(&kqueue_misc_lock);
577 }
578
579 /*
580 * data contains amount of time to sleep, in milliseconds
581 */
582 static int
583 filt_timerattach(struct knote *kn)
584 {
585 callout_t *calloutp;
586 struct kqueue *kq;
587 int tticks;
588
589 tticks = mstohz(kn->kn_sdata);
590
591 /* if the supplied value is under our resolution, use 1 tick */
592 if (tticks == 0) {
593 if (kn->kn_sdata == 0)
594 return EINVAL;
595 tticks = 1;
596 }
597
598 if (atomic_inc_uint_nv(&kq_ncallouts) >= kq_calloutmax ||
599 (calloutp = kmem_alloc(sizeof(*calloutp), KM_NOSLEEP)) == NULL) {
600 atomic_dec_uint(&kq_ncallouts);
601 return ENOMEM;
602 }
603 callout_init(calloutp, CALLOUT_MPSAFE);
604
605 kq = kn->kn_kq;
606 mutex_spin_enter(&kq->kq_lock);
607 kn->kn_flags |= EV_CLEAR; /* automatically set */
608 kn->kn_hook = calloutp;
609 mutex_spin_exit(&kq->kq_lock);
610
611 callout_reset(calloutp, tticks, filt_timerexpire, kn);
612
613 return (0);
614 }
615
616 static void
617 filt_timerdetach(struct knote *kn)
618 {
619 callout_t *calloutp;
620
621 calloutp = (callout_t *)kn->kn_hook;
622 callout_halt(calloutp, NULL);
623 callout_destroy(calloutp);
624 kmem_free(calloutp, sizeof(*calloutp));
625 atomic_dec_uint(&kq_ncallouts);
626 }
627
628 static int
629 filt_timer(struct knote *kn, long hint)
630 {
631 int rv;
632
633 mutex_enter(&kqueue_misc_lock);
634 rv = (kn->kn_data != 0);
635 mutex_exit(&kqueue_misc_lock);
636
637 return rv;
638 }
639
640 /*
641 * filt_seltrue:
642 *
643 * This filter "event" routine simulates seltrue().
644 */
645 int
646 filt_seltrue(struct knote *kn, long hint)
647 {
648
649 /*
650 * We don't know how much data can be read/written,
651 * but we know that it *can* be. This is about as
652 * good as select/poll does as well.
653 */
654 kn->kn_data = 0;
655 return (1);
656 }
657
658 /*
659 * This provides full kqfilter entry for device switch tables, which
660 * has same effect as filter using filt_seltrue() as filter method.
661 */
662 static void
663 filt_seltruedetach(struct knote *kn)
664 {
665 /* Nothing to do */
666 }
667
668 const struct filterops seltrue_filtops =
669 { 1, NULL, filt_seltruedetach, filt_seltrue };
670
671 int
672 seltrue_kqfilter(dev_t dev, struct knote *kn)
673 {
674 switch (kn->kn_filter) {
675 case EVFILT_READ:
676 case EVFILT_WRITE:
677 kn->kn_fop = &seltrue_filtops;
678 break;
679 default:
680 return (EINVAL);
681 }
682
683 /* Nothing more to do */
684 return (0);
685 }
686
687 /*
688 * kqueue(2) system call.
689 */
690 int
691 sys_kqueue(struct lwp *l, const void *v, register_t *retval)
692 {
693 struct kqueue *kq;
694 file_t *fp;
695 int fd, error;
696
697 if ((error = fd_allocfile(&fp, &fd)) != 0)
698 return error;
699 fp->f_flag = FREAD | FWRITE;
700 fp->f_type = DTYPE_KQUEUE;
701 fp->f_ops = &kqueueops;
702 kq = kmem_zalloc(sizeof(*kq), KM_SLEEP);
703 mutex_init(&kq->kq_lock, MUTEX_DEFAULT, IPL_SCHED);
704 cv_init(&kq->kq_cv, "kqueue");
705 selinit(&kq->kq_sel);
706 TAILQ_INIT(&kq->kq_head);
707 fp->f_data = kq;
708 *retval = fd;
709 kq->kq_fdp = curlwp->l_fd;
710 fd_affix(curproc, fp, fd);
711 return error;
712 }
713
714 /*
715 * kevent(2) system call.
716 */
717 static int
718 kevent_fetch_changes(void *private, const struct kevent *changelist,
719 struct kevent *changes, size_t index, int n)
720 {
721
722 return copyin(changelist + index, changes, n * sizeof(*changes));
723 }
724
725 static int
726 kevent_put_events(void *private, struct kevent *events,
727 struct kevent *eventlist, size_t index, int n)
728 {
729
730 return copyout(events, eventlist + index, n * sizeof(*events));
731 }
732
733 static const struct kevent_ops kevent_native_ops = {
734 .keo_private = NULL,
735 .keo_fetch_timeout = copyin,
736 .keo_fetch_changes = kevent_fetch_changes,
737 .keo_put_events = kevent_put_events,
738 };
739
740 int
741 sys_kevent(struct lwp *l, const struct sys_kevent_args *uap, register_t *retval)
742 {
743 /* {
744 syscallarg(int) fd;
745 syscallarg(const struct kevent *) changelist;
746 syscallarg(size_t) nchanges;
747 syscallarg(struct kevent *) eventlist;
748 syscallarg(size_t) nevents;
749 syscallarg(const struct timespec *) timeout;
750 } */
751
752 return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
753 SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
754 SCARG(uap, timeout), &kevent_native_ops);
755 }
756
757 int
758 kevent1(register_t *retval, int fd,
759 const struct kevent *changelist, size_t nchanges,
760 struct kevent *eventlist, size_t nevents,
761 const struct timespec *timeout,
762 const struct kevent_ops *keops)
763 {
764 struct kevent *kevp;
765 struct kqueue *kq;
766 struct timespec ts;
767 size_t i, n, ichange;
768 int nerrors, error;
769 struct kevent kevbuf[8]; /* approx 300 bytes on 64-bit */
770 file_t *fp;
771
772 /* check that we're dealing with a kq */
773 fp = fd_getfile(fd);
774 if (fp == NULL)
775 return (EBADF);
776
777 if (fp->f_type != DTYPE_KQUEUE) {
778 fd_putfile(fd);
779 return (EBADF);
780 }
781
782 if (timeout != NULL) {
783 error = (*keops->keo_fetch_timeout)(timeout, &ts, sizeof(ts));
784 if (error)
785 goto done;
786 timeout = &ts;
787 }
788
789 kq = (struct kqueue *)fp->f_data;
790 nerrors = 0;
791 ichange = 0;
792
793 /* traverse list of events to register */
794 while (nchanges > 0) {
795 n = MIN(nchanges, __arraycount(kevbuf));
796 error = (*keops->keo_fetch_changes)(keops->keo_private,
797 changelist, kevbuf, ichange, n);
798 if (error)
799 goto done;
800 for (i = 0; i < n; i++) {
801 kevp = &kevbuf[i];
802 kevp->flags &= ~EV_SYSFLAGS;
803 /* register each knote */
804 error = kqueue_register(kq, kevp);
805 if (error) {
806 if (nevents != 0) {
807 kevp->flags = EV_ERROR;
808 kevp->data = error;
809 error = (*keops->keo_put_events)
810 (keops->keo_private, kevp,
811 eventlist, nerrors, 1);
812 if (error)
813 goto done;
814 nevents--;
815 nerrors++;
816 } else {
817 goto done;
818 }
819 }
820 }
821 nchanges -= n; /* update the results */
822 ichange += n;
823 }
824 if (nerrors) {
825 *retval = nerrors;
826 error = 0;
827 goto done;
828 }
829
830 /* actually scan through the events */
831 error = kqueue_scan(fp, nevents, eventlist, timeout, retval, keops,
832 kevbuf, __arraycount(kevbuf));
833 done:
834 fd_putfile(fd);
835 return (error);
836 }
837
838 /*
839 * Register a given kevent kev onto the kqueue
840 */
841 static int
842 kqueue_register(struct kqueue *kq, struct kevent *kev)
843 {
844 struct kfilter *kfilter;
845 filedesc_t *fdp;
846 file_t *fp;
847 fdfile_t *ff;
848 struct knote *kn, *newkn;
849 struct klist *list;
850 int error, fd, rv;
851
852 fdp = kq->kq_fdp;
853 fp = NULL;
854 kn = NULL;
855 error = 0;
856 fd = 0;
857
858 newkn = kmem_zalloc(sizeof(*newkn), KM_SLEEP);
859
860 rw_enter(&kqueue_filter_lock, RW_READER);
861 kfilter = kfilter_byfilter(kev->filter);
862 if (kfilter == NULL || kfilter->filtops == NULL) {
863 /* filter not found nor implemented */
864 rw_exit(&kqueue_filter_lock);
865 kmem_free(newkn, sizeof(*newkn));
866 return (EINVAL);
867 }
868
869 /* search if knote already exists */
870 if (kfilter->filtops->f_isfd) {
871 /* monitoring a file descriptor */
872 fd = kev->ident;
873 if ((fp = fd_getfile(fd)) == NULL) {
874 rw_exit(&kqueue_filter_lock);
875 kmem_free(newkn, sizeof(*newkn));
876 return EBADF;
877 }
878 mutex_enter(&fdp->fd_lock);
879 ff = fdp->fd_ofiles[fd];
880 if (fd <= fdp->fd_lastkqfile) {
881 SLIST_FOREACH(kn, &ff->ff_knlist, kn_link) {
882 if (kq == kn->kn_kq &&
883 kev->filter == kn->kn_filter)
884 break;
885 }
886 }
887 } else {
888 /*
889 * not monitoring a file descriptor, so
890 * lookup knotes in internal hash table
891 */
892 mutex_enter(&fdp->fd_lock);
893 if (fdp->fd_knhashmask != 0) {
894 list = &fdp->fd_knhash[
895 KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
896 SLIST_FOREACH(kn, list, kn_link) {
897 if (kev->ident == kn->kn_id &&
898 kq == kn->kn_kq &&
899 kev->filter == kn->kn_filter)
900 break;
901 }
902 }
903 }
904
905 /*
906 * kn now contains the matching knote, or NULL if no match
907 */
908 if (kev->flags & EV_ADD) {
909 if (kn == NULL) {
910 /* create new knote */
911 kn = newkn;
912 newkn = NULL;
913 kn->kn_obj = fp;
914 kn->kn_kq = kq;
915 kn->kn_fop = kfilter->filtops;
916 kn->kn_kfilter = kfilter;
917 kn->kn_sfflags = kev->fflags;
918 kn->kn_sdata = kev->data;
919 kev->fflags = 0;
920 kev->data = 0;
921 kn->kn_kevent = *kev;
922
923 /*
924 * apply reference count to knote structure, and
925 * do not release it at the end of this routine.
926 */
927 fp = NULL;
928
929 if (!kn->kn_fop->f_isfd) {
930 /*
931 * If knote is not on an fd, store on
932 * internal hash table.
933 */
934 if (fdp->fd_knhashmask == 0) {
935 /* XXXAD can block with fd_lock held */
936 fdp->fd_knhash = hashinit(KN_HASHSIZE,
937 HASH_LIST, true,
938 &fdp->fd_knhashmask);
939 }
940 list = &fdp->fd_knhash[KN_HASH(kn->kn_id,
941 fdp->fd_knhashmask)];
942 } else {
943 /* Otherwise, knote is on an fd. */
944 list = (struct klist *)
945 &fdp->fd_ofiles[kn->kn_id]->ff_knlist;
946 if ((int)kn->kn_id > fdp->fd_lastkqfile)
947 fdp->fd_lastkqfile = kn->kn_id;
948 }
949 SLIST_INSERT_HEAD(list, kn, kn_link);
950
951 KERNEL_LOCK(1, NULL); /* XXXSMP */
952 error = (*kfilter->filtops->f_attach)(kn);
953 KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
954 if (error != 0) {
955 /* knote_detach() drops fdp->fd_lock */
956 knote_detach(kn, fdp, false);
957 goto done;
958 }
959 atomic_inc_uint(&kfilter->refcnt);
960 } else {
961 /*
962 * The user may change some filter values after the
963 * initial EV_ADD, but doing so will not reset any
964 * filter which have already been triggered.
965 */
966 kn->kn_sfflags = kev->fflags;
967 kn->kn_sdata = kev->data;
968 kn->kn_kevent.udata = kev->udata;
969 }
970 KERNEL_LOCK(1, NULL); /* XXXSMP */
971 rv = (*kn->kn_fop->f_event)(kn, 0);
972 KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
973 if (rv)
974 knote_activate(kn);
975 } else {
976 if (kn == NULL) {
977 error = ENOENT;
978 mutex_exit(&fdp->fd_lock);
979 goto done;
980 }
981 if (kev->flags & EV_DELETE) {
982 /* knote_detach() drops fdp->fd_lock */
983 knote_detach(kn, fdp, true);
984 goto done;
985 }
986 }
987
988 /* disable knote */
989 if ((kev->flags & EV_DISABLE)) {
990 mutex_spin_enter(&kq->kq_lock);
991 if ((kn->kn_status & KN_DISABLED) == 0)
992 kn->kn_status |= KN_DISABLED;
993 mutex_spin_exit(&kq->kq_lock);
994 }
995
996 /* enable knote */
997 if ((kev->flags & EV_ENABLE)) {
998 knote_enqueue(kn);
999 }
1000 mutex_exit(&fdp->fd_lock);
1001 done:
1002 rw_exit(&kqueue_filter_lock);
1003 if (newkn != NULL)
1004 kmem_free(newkn, sizeof(*newkn));
1005 if (fp != NULL)
1006 fd_putfile(fd);
1007 return (error);
1008 }
1009
1010 #if defined(DEBUG)
1011 static void
1012 kq_check(struct kqueue *kq)
1013 {
1014 const struct knote *kn;
1015 int count;
1016 int nmarker;
1017
1018 KASSERT(mutex_owned(&kq->kq_lock));
1019 KASSERT(kq->kq_count >= 0);
1020
1021 count = 0;
1022 nmarker = 0;
1023 TAILQ_FOREACH(kn, &kq->kq_head, kn_tqe) {
1024 if ((kn->kn_status & (KN_MARKER | KN_QUEUED)) == 0) {
1025 panic("%s: kq=%p kn=%p inconsist 1", __func__, kq, kn);
1026 }
1027 if ((kn->kn_status & KN_MARKER) == 0) {
1028 if (kn->kn_kq != kq) {
1029 panic("%s: kq=%p kn=%p inconsist 2",
1030 __func__, kq, kn);
1031 }
1032 if ((kn->kn_status & KN_ACTIVE) == 0) {
1033 panic("%s: kq=%p kn=%p: not active",
1034 __func__, kq, kn);
1035 }
1036 count++;
1037 if (count > kq->kq_count) {
1038 goto bad;
1039 }
1040 } else {
1041 nmarker++;
1042 #if 0
1043 if (nmarker > 10000) {
1044 panic("%s: kq=%p too many markers: %d != %d, "
1045 "nmarker=%d",
1046 __func__, kq, kq->kq_count, count, nmarker);
1047 }
1048 #endif
1049 }
1050 }
1051 if (kq->kq_count != count) {
1052 bad:
1053 panic("%s: kq=%p inconsist 3: %d != %d, nmarker=%d",
1054 __func__, kq, kq->kq_count, count, nmarker);
1055 }
1056 }
1057 #else /* defined(DEBUG) */
1058 #define kq_check(a) /* nothing */
1059 #endif /* defined(DEBUG) */
1060
1061 /*
1062 * Scan through the list of events on fp (for a maximum of maxevents),
1063 * returning the results in to ulistp. Timeout is determined by tsp; if
1064 * NULL, wait indefinitely, if 0 valued, perform a poll, otherwise wait
1065 * as appropriate.
1066 */
1067 static int
1068 kqueue_scan(file_t *fp, size_t maxevents, struct kevent *ulistp,
1069 const struct timespec *tsp, register_t *retval,
1070 const struct kevent_ops *keops, struct kevent *kevbuf,
1071 size_t kevcnt)
1072 {
1073 struct kqueue *kq;
1074 struct kevent *kevp;
1075 struct timeval atv, sleeptv;
1076 struct knote *kn, *marker;
1077 size_t count, nkev, nevents;
1078 int timeout, error, rv;
1079 filedesc_t *fdp;
1080
1081 fdp = curlwp->l_fd;
1082 kq = fp->f_data;
1083 count = maxevents;
1084 nkev = nevents = error = 0;
1085 if (count == 0) {
1086 *retval = 0;
1087 return 0;
1088 }
1089
1090 if (tsp) { /* timeout supplied */
1091 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1092 if (inittimeleft(&atv, &sleeptv) == -1) {
1093 *retval = maxevents;
1094 return EINVAL;
1095 }
1096 timeout = tvtohz(&atv);
1097 if (timeout <= 0)
1098 timeout = -1; /* do poll */
1099 } else {
1100 /* no timeout, wait forever */
1101 timeout = 0;
1102 }
1103
1104 marker = kmem_zalloc(sizeof(*marker), KM_SLEEP);
1105 marker->kn_status = KN_MARKER;
1106 mutex_spin_enter(&kq->kq_lock);
1107 retry:
1108 kevp = kevbuf;
1109 if (kq->kq_count == 0) {
1110 if (timeout >= 0) {
1111 error = cv_timedwait_sig(&kq->kq_cv,
1112 &kq->kq_lock, timeout);
1113 if (error == 0) {
1114 if (tsp == NULL || (timeout =
1115 gettimeleft(&atv, &sleeptv)) > 0)
1116 goto retry;
1117 } else {
1118 /* don't restart after signals... */
1119 if (error == ERESTART)
1120 error = EINTR;
1121 if (error == EWOULDBLOCK)
1122 error = 0;
1123 }
1124 }
1125 } else {
1126 /* mark end of knote list */
1127 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1128
1129 while (count != 0) {
1130 kn = TAILQ_FIRST(&kq->kq_head); /* get next knote */
1131 while ((kn->kn_status & KN_MARKER) != 0) {
1132 if (kn == marker) {
1133 /* it's our marker, stop */
1134 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1135 if (count < maxevents || (tsp != NULL &&
1136 (timeout = gettimeleft(&atv,
1137 &sleeptv)) <= 0))
1138 goto done;
1139 goto retry;
1140 }
1141 /* someone else's marker. */
1142 kn = TAILQ_NEXT(kn, kn_tqe);
1143 }
1144 kq_check(kq);
1145 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1146 kq->kq_count--;
1147 kn->kn_status &= ~KN_QUEUED;
1148 kq_check(kq);
1149 if (kn->kn_status & KN_DISABLED) {
1150 /* don't want disabled events */
1151 continue;
1152 }
1153 if ((kn->kn_flags & EV_ONESHOT) == 0) {
1154 mutex_spin_exit(&kq->kq_lock);
1155 KERNEL_LOCK(1, NULL); /* XXXSMP */
1156 rv = (*kn->kn_fop->f_event)(kn, 0);
1157 KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
1158 mutex_spin_enter(&kq->kq_lock);
1159 /* Re-poll if note was re-enqueued. */
1160 if ((kn->kn_status & KN_QUEUED) != 0)
1161 continue;
1162 if (rv == 0) {
1163 /*
1164 * non-ONESHOT event that hasn't
1165 * triggered again, so de-queue.
1166 */
1167 kn->kn_status &= ~KN_ACTIVE;
1168 continue;
1169 }
1170 }
1171 /* XXXAD should be got from f_event if !oneshot. */
1172 *kevp++ = kn->kn_kevent;
1173 nkev++;
1174 if (kn->kn_flags & EV_ONESHOT) {
1175 /* delete ONESHOT events after retrieval */
1176 mutex_spin_exit(&kq->kq_lock);
1177 mutex_enter(&fdp->fd_lock);
1178 knote_detach(kn, fdp, true);
1179 mutex_spin_enter(&kq->kq_lock);
1180 } else if (kn->kn_flags & EV_CLEAR) {
1181 /* clear state after retrieval */
1182 kn->kn_data = 0;
1183 kn->kn_fflags = 0;
1184 kn->kn_status &= ~KN_ACTIVE;
1185 } else {
1186 /* add event back on list */
1187 kq_check(kq);
1188 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1189 kq->kq_count++;
1190 kn->kn_status |= KN_QUEUED;
1191 kq_check(kq);
1192 }
1193 if (nkev == kevcnt) {
1194 /* do copyouts in kevcnt chunks */
1195 mutex_spin_exit(&kq->kq_lock);
1196 error = (*keops->keo_put_events)
1197 (keops->keo_private,
1198 kevbuf, ulistp, nevents, nkev);
1199 mutex_spin_enter(&kq->kq_lock);
1200 nevents += nkev;
1201 nkev = 0;
1202 kevp = kevbuf;
1203 }
1204 count--;
1205 if (error != 0 || count == 0) {
1206 /* remove marker */
1207 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1208 break;
1209 }
1210 }
1211 }
1212 done:
1213 mutex_spin_exit(&kq->kq_lock);
1214 if (marker != NULL)
1215 kmem_free(marker, sizeof(*marker));
1216 if (nkev != 0) {
1217 /* copyout remaining events */
1218 error = (*keops->keo_put_events)(keops->keo_private,
1219 kevbuf, ulistp, nevents, nkev);
1220 }
1221 *retval = maxevents - count;
1222
1223 return error;
1224 }
1225
1226 /*
1227 * fileops ioctl method for a kqueue descriptor.
1228 *
1229 * Two ioctls are currently supported. They both use struct kfilter_mapping:
1230 * KFILTER_BYNAME find name for filter, and return result in
1231 * name, which is of size len.
1232 * KFILTER_BYFILTER find filter for name. len is ignored.
1233 */
1234 /*ARGSUSED*/
1235 static int
1236 kqueue_ioctl(file_t *fp, u_long com, void *data)
1237 {
1238 struct kfilter_mapping *km;
1239 const struct kfilter *kfilter;
1240 char *name;
1241 int error;
1242
1243 km = data;
1244 error = 0;
1245 name = kmem_alloc(KFILTER_MAXNAME, KM_SLEEP);
1246
1247 switch (com) {
1248 case KFILTER_BYFILTER: /* convert filter -> name */
1249 rw_enter(&kqueue_filter_lock, RW_READER);
1250 kfilter = kfilter_byfilter(km->filter);
1251 if (kfilter != NULL) {
1252 strlcpy(name, kfilter->name, KFILTER_MAXNAME);
1253 rw_exit(&kqueue_filter_lock);
1254 error = copyoutstr(name, km->name, km->len, NULL);
1255 } else {
1256 rw_exit(&kqueue_filter_lock);
1257 error = ENOENT;
1258 }
1259 break;
1260
1261 case KFILTER_BYNAME: /* convert name -> filter */
1262 error = copyinstr(km->name, name, KFILTER_MAXNAME, NULL);
1263 if (error) {
1264 break;
1265 }
1266 rw_enter(&kqueue_filter_lock, RW_READER);
1267 kfilter = kfilter_byname(name);
1268 if (kfilter != NULL)
1269 km->filter = kfilter->filter;
1270 else
1271 error = ENOENT;
1272 rw_exit(&kqueue_filter_lock);
1273 break;
1274
1275 default:
1276 error = ENOTTY;
1277 break;
1278
1279 }
1280 kmem_free(name, KFILTER_MAXNAME);
1281 return (error);
1282 }
1283
1284 /*
1285 * fileops fcntl method for a kqueue descriptor.
1286 */
1287 static int
1288 kqueue_fcntl(file_t *fp, u_int com, void *data)
1289 {
1290
1291 return (ENOTTY);
1292 }
1293
1294 /*
1295 * fileops poll method for a kqueue descriptor.
1296 * Determine if kqueue has events pending.
1297 */
1298 static int
1299 kqueue_poll(file_t *fp, int events)
1300 {
1301 struct kqueue *kq;
1302 int revents;
1303
1304 kq = fp->f_data;
1305
1306 revents = 0;
1307 if (events & (POLLIN | POLLRDNORM)) {
1308 mutex_spin_enter(&kq->kq_lock);
1309 if (kq->kq_count != 0) {
1310 revents |= events & (POLLIN | POLLRDNORM);
1311 } else {
1312 selrecord(curlwp, &kq->kq_sel);
1313 }
1314 kq_check(kq);
1315 mutex_spin_exit(&kq->kq_lock);
1316 }
1317
1318 return revents;
1319 }
1320
1321 /*
1322 * fileops stat method for a kqueue descriptor.
1323 * Returns dummy info, with st_size being number of events pending.
1324 */
1325 static int
1326 kqueue_stat(file_t *fp, struct stat *st)
1327 {
1328 struct kqueue *kq;
1329
1330 kq = fp->f_data;
1331
1332 memset(st, 0, sizeof(*st));
1333 st->st_size = kq->kq_count;
1334 st->st_blksize = sizeof(struct kevent);
1335 st->st_mode = S_IFIFO;
1336
1337 return 0;
1338 }
1339
1340 static void
1341 kqueue_doclose(struct kqueue *kq, struct klist *list, int fd)
1342 {
1343 struct knote *kn;
1344 filedesc_t *fdp;
1345
1346 fdp = kq->kq_fdp;
1347
1348 KASSERT(mutex_owned(&fdp->fd_lock));
1349
1350 for (kn = SLIST_FIRST(list); kn != NULL;) {
1351 if (kq != kn->kn_kq) {
1352 kn = SLIST_NEXT(kn, kn_link);
1353 continue;
1354 }
1355 knote_detach(kn, fdp, true);
1356 mutex_enter(&fdp->fd_lock);
1357 kn = SLIST_FIRST(list);
1358 }
1359 }
1360
1361
1362 /*
1363 * fileops close method for a kqueue descriptor.
1364 */
1365 static int
1366 kqueue_close(file_t *fp)
1367 {
1368 struct kqueue *kq;
1369 filedesc_t *fdp;
1370 fdfile_t *ff;
1371 int i;
1372
1373 kq = fp->f_data;
1374 fdp = curlwp->l_fd;
1375
1376 mutex_enter(&fdp->fd_lock);
1377 for (i = 0; i <= fdp->fd_lastkqfile; i++) {
1378 if ((ff = fdp->fd_ofiles[i]) == NULL)
1379 continue;
1380 kqueue_doclose(kq, (struct klist *)&ff->ff_knlist, i);
1381 }
1382 if (fdp->fd_knhashmask != 0) {
1383 for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
1384 kqueue_doclose(kq, &fdp->fd_knhash[i], -1);
1385 }
1386 }
1387 mutex_exit(&fdp->fd_lock);
1388
1389 KASSERT(kq->kq_count == 0);
1390 mutex_destroy(&kq->kq_lock);
1391 cv_destroy(&kq->kq_cv);
1392 seldestroy(&kq->kq_sel);
1393 kmem_free(kq, sizeof(*kq));
1394 fp->f_data = NULL;
1395
1396 return (0);
1397 }
1398
1399 /*
1400 * struct fileops kqfilter method for a kqueue descriptor.
1401 * Event triggered when monitored kqueue changes.
1402 */
1403 static int
1404 kqueue_kqfilter(file_t *fp, struct knote *kn)
1405 {
1406 struct kqueue *kq;
1407 filedesc_t *fdp;
1408
1409 kq = ((file_t *)kn->kn_obj)->f_data;
1410
1411 KASSERT(fp == kn->kn_obj);
1412
1413 if (kn->kn_filter != EVFILT_READ)
1414 return 1;
1415
1416 kn->kn_fop = &kqread_filtops;
1417 fdp = curlwp->l_fd;
1418 mutex_enter(&kq->kq_lock);
1419 SLIST_INSERT_HEAD(&kq->kq_sel.sel_klist, kn, kn_selnext);
1420 mutex_exit(&kq->kq_lock);
1421
1422 return 0;
1423 }
1424
1425
1426 /*
1427 * Walk down a list of knotes, activating them if their event has
1428 * triggered. The caller's object lock (e.g. device driver lock)
1429 * must be held.
1430 */
1431 void
1432 knote(struct klist *list, long hint)
1433 {
1434 struct knote *kn;
1435
1436 SLIST_FOREACH(kn, list, kn_selnext) {
1437 if ((*kn->kn_fop->f_event)(kn, hint))
1438 knote_activate(kn);
1439 }
1440 }
1441
1442 /*
1443 * Remove all knotes referencing a specified fd
1444 */
1445 void
1446 knote_fdclose(int fd)
1447 {
1448 struct klist *list;
1449 struct knote *kn;
1450 filedesc_t *fdp;
1451
1452 fdp = curlwp->l_fd;
1453 list = (struct klist *)&fdp->fd_ofiles[fd]->ff_knlist;
1454 mutex_enter(&fdp->fd_lock);
1455 while ((kn = SLIST_FIRST(list)) != NULL) {
1456 knote_detach(kn, fdp, true);
1457 mutex_enter(&fdp->fd_lock);
1458 }
1459 mutex_exit(&fdp->fd_lock);
1460 }
1461
1462 /*
1463 * Drop knote. Called with fdp->fd_lock held, and will drop before
1464 * returning.
1465 */
1466 static void
1467 knote_detach(struct knote *kn, filedesc_t *fdp, bool dofop)
1468 {
1469 struct klist *list;
1470 struct kqueue *kq;
1471
1472 kq = kn->kn_kq;
1473
1474 KASSERT((kn->kn_status & KN_MARKER) == 0);
1475 KASSERT(mutex_owned(&fdp->fd_lock));
1476
1477 /* Remove from monitored object. */
1478 if (dofop) {
1479 KERNEL_LOCK(1, NULL); /* XXXSMP */
1480 (*kn->kn_fop->f_detach)(kn);
1481 KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
1482 }
1483
1484 /* Remove from descriptor table. */
1485 if (kn->kn_fop->f_isfd)
1486 list = (struct klist *)&fdp->fd_ofiles[kn->kn_id]->ff_knlist;
1487 else
1488 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
1489
1490 SLIST_REMOVE(list, kn, knote, kn_link);
1491
1492 /* Remove from kqueue. */
1493 /* XXXAD should verify not in use by kqueue_scan. */
1494 mutex_spin_enter(&kq->kq_lock);
1495 if ((kn->kn_status & KN_QUEUED) != 0) {
1496 kq_check(kq);
1497 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1498 kn->kn_status &= ~KN_QUEUED;
1499 kq->kq_count--;
1500 kq_check(kq);
1501 }
1502 mutex_spin_exit(&kq->kq_lock);
1503
1504 mutex_exit(&fdp->fd_lock);
1505 if (kn->kn_fop->f_isfd)
1506 fd_putfile(kn->kn_id);
1507 atomic_dec_uint(&kn->kn_kfilter->refcnt);
1508 kmem_free(kn, sizeof(*kn));
1509 }
1510
1511 /*
1512 * Queue new event for knote.
1513 */
1514 static void
1515 knote_enqueue(struct knote *kn)
1516 {
1517 struct kqueue *kq;
1518
1519 KASSERT((kn->kn_status & KN_MARKER) == 0);
1520
1521 kq = kn->kn_kq;
1522
1523 mutex_spin_enter(&kq->kq_lock);
1524 if ((kn->kn_status & KN_DISABLED) != 0) {
1525 kn->kn_status &= ~KN_DISABLED;
1526 }
1527 if ((kn->kn_status & (KN_ACTIVE | KN_QUEUED)) == KN_ACTIVE) {
1528 kq_check(kq);
1529 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1530 kn->kn_status |= KN_QUEUED;
1531 kq->kq_count++;
1532 kq_check(kq);
1533 cv_broadcast(&kq->kq_cv);
1534 selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
1535 }
1536 mutex_spin_exit(&kq->kq_lock);
1537 }
1538 /*
1539 * Queue new event for knote.
1540 */
1541 static void
1542 knote_activate(struct knote *kn)
1543 {
1544 struct kqueue *kq;
1545
1546 KASSERT((kn->kn_status & KN_MARKER) == 0);
1547
1548 kq = kn->kn_kq;
1549
1550 mutex_spin_enter(&kq->kq_lock);
1551 kn->kn_status |= KN_ACTIVE;
1552 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) {
1553 kq_check(kq);
1554 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1555 kn->kn_status |= KN_QUEUED;
1556 kq->kq_count++;
1557 kq_check(kq);
1558 cv_broadcast(&kq->kq_cv);
1559 selnotify(&kq->kq_sel, 0, NOTE_SUBMIT);
1560 }
1561 mutex_spin_exit(&kq->kq_lock);
1562 }
1563