FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_event.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
5 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
6 * Copyright (c) 2009 Apple, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: releng/12.0/sys/kern/kern_event.c 341159 2018-11-28 18:06:16Z markj $");
33
34 #include "opt_ktrace.h"
35 #include "opt_kqueue.h"
36
37 #ifdef COMPAT_FREEBSD11
38 #define _WANT_FREEBSD11_KEVENT
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/rwlock.h>
48 #include <sys/proc.h>
49 #include <sys/malloc.h>
50 #include <sys/unistd.h>
51 #include <sys/file.h>
52 #include <sys/filedesc.h>
53 #include <sys/filio.h>
54 #include <sys/fcntl.h>
55 #include <sys/kthread.h>
56 #include <sys/selinfo.h>
57 #include <sys/queue.h>
58 #include <sys/event.h>
59 #include <sys/eventvar.h>
60 #include <sys/poll.h>
61 #include <sys/protosw.h>
62 #include <sys/resourcevar.h>
63 #include <sys/sigio.h>
64 #include <sys/signalvar.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/stat.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysproto.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/taskqueue.h>
72 #include <sys/uio.h>
73 #include <sys/user.h>
74 #ifdef KTRACE
75 #include <sys/ktrace.h>
76 #endif
77 #include <machine/atomic.h>
78
79 #include <vm/uma.h>
80
81 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
82
83 /*
84 * This lock is used if multiple kq locks are required. This possibly
85 * should be made into a per proc lock.
86 */
87 static struct mtx kq_global;
88 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
89 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
90 if (!haslck) \
91 mtx_lock(lck); \
92 haslck = 1; \
93 } while (0)
94 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
95 if (haslck) \
96 mtx_unlock(lck); \
97 haslck = 0; \
98 } while (0)
99
100 TASKQUEUE_DEFINE_THREAD(kqueue_ctx);
101
102 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
103 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
104 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
105 struct thread *td, int waitok);
106 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
107 static void kqueue_release(struct kqueue *kq, int locked);
108 static void kqueue_destroy(struct kqueue *kq);
109 static void kqueue_drain(struct kqueue *kq, struct thread *td);
110 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
111 uintptr_t ident, int waitok);
112 static void kqueue_task(void *arg, int pending);
113 static int kqueue_scan(struct kqueue *kq, int maxevents,
114 struct kevent_copyops *k_ops,
115 const struct timespec *timeout,
116 struct kevent *keva, struct thread *td);
117 static void kqueue_wakeup(struct kqueue *kq);
118 static struct filterops *kqueue_fo_find(int filt);
119 static void kqueue_fo_release(int filt);
120 struct g_kevent_args;
121 static int kern_kevent_generic(struct thread *td,
122 struct g_kevent_args *uap,
123 struct kevent_copyops *k_ops, const char *struct_name);
124
125 static fo_ioctl_t kqueue_ioctl;
126 static fo_poll_t kqueue_poll;
127 static fo_kqfilter_t kqueue_kqfilter;
128 static fo_stat_t kqueue_stat;
129 static fo_close_t kqueue_close;
130 static fo_fill_kinfo_t kqueue_fill_kinfo;
131
132 static struct fileops kqueueops = {
133 .fo_read = invfo_rdwr,
134 .fo_write = invfo_rdwr,
135 .fo_truncate = invfo_truncate,
136 .fo_ioctl = kqueue_ioctl,
137 .fo_poll = kqueue_poll,
138 .fo_kqfilter = kqueue_kqfilter,
139 .fo_stat = kqueue_stat,
140 .fo_close = kqueue_close,
141 .fo_chmod = invfo_chmod,
142 .fo_chown = invfo_chown,
143 .fo_sendfile = invfo_sendfile,
144 .fo_fill_kinfo = kqueue_fill_kinfo,
145 };
146
147 static int knote_attach(struct knote *kn, struct kqueue *kq);
148 static void knote_drop(struct knote *kn, struct thread *td);
149 static void knote_drop_detached(struct knote *kn, struct thread *td);
150 static void knote_enqueue(struct knote *kn);
151 static void knote_dequeue(struct knote *kn);
152 static void knote_init(void);
153 static struct knote *knote_alloc(int waitok);
154 static void knote_free(struct knote *kn);
155
156 static void filt_kqdetach(struct knote *kn);
157 static int filt_kqueue(struct knote *kn, long hint);
158 static int filt_procattach(struct knote *kn);
159 static void filt_procdetach(struct knote *kn);
160 static int filt_proc(struct knote *kn, long hint);
161 static int filt_fileattach(struct knote *kn);
162 static void filt_timerexpire(void *knx);
163 static int filt_timerattach(struct knote *kn);
164 static void filt_timerdetach(struct knote *kn);
165 static void filt_timerstart(struct knote *kn, sbintime_t to);
166 static void filt_timertouch(struct knote *kn, struct kevent *kev,
167 u_long type);
168 static int filt_timervalidate(struct knote *kn, sbintime_t *to);
169 static int filt_timer(struct knote *kn, long hint);
170 static int filt_userattach(struct knote *kn);
171 static void filt_userdetach(struct knote *kn);
172 static int filt_user(struct knote *kn, long hint);
173 static void filt_usertouch(struct knote *kn, struct kevent *kev,
174 u_long type);
175
176 static struct filterops file_filtops = {
177 .f_isfd = 1,
178 .f_attach = filt_fileattach,
179 };
180 static struct filterops kqread_filtops = {
181 .f_isfd = 1,
182 .f_detach = filt_kqdetach,
183 .f_event = filt_kqueue,
184 };
185 /* XXX - move to kern_proc.c? */
186 static struct filterops proc_filtops = {
187 .f_isfd = 0,
188 .f_attach = filt_procattach,
189 .f_detach = filt_procdetach,
190 .f_event = filt_proc,
191 };
192 static struct filterops timer_filtops = {
193 .f_isfd = 0,
194 .f_attach = filt_timerattach,
195 .f_detach = filt_timerdetach,
196 .f_event = filt_timer,
197 .f_touch = filt_timertouch,
198 };
199 static struct filterops user_filtops = {
200 .f_attach = filt_userattach,
201 .f_detach = filt_userdetach,
202 .f_event = filt_user,
203 .f_touch = filt_usertouch,
204 };
205
206 static uma_zone_t knote_zone;
207 static unsigned int kq_ncallouts = 0;
208 static unsigned int kq_calloutmax = 4 * 1024;
209 SYSCTL_UINT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
210 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
211
212 /* XXX - ensure not influx ? */
213 #define KNOTE_ACTIVATE(kn, islock) do { \
214 if ((islock)) \
215 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
216 else \
217 KQ_LOCK((kn)->kn_kq); \
218 (kn)->kn_status |= KN_ACTIVE; \
219 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
220 knote_enqueue((kn)); \
221 if (!(islock)) \
222 KQ_UNLOCK((kn)->kn_kq); \
223 } while(0)
224 #define KQ_LOCK(kq) do { \
225 mtx_lock(&(kq)->kq_lock); \
226 } while (0)
227 #define KQ_FLUX_WAKEUP(kq) do { \
228 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
229 (kq)->kq_state &= ~KQ_FLUXWAIT; \
230 wakeup((kq)); \
231 } \
232 } while (0)
233 #define KQ_UNLOCK_FLUX(kq) do { \
234 KQ_FLUX_WAKEUP(kq); \
235 mtx_unlock(&(kq)->kq_lock); \
236 } while (0)
237 #define KQ_UNLOCK(kq) do { \
238 mtx_unlock(&(kq)->kq_lock); \
239 } while (0)
240 #define KQ_OWNED(kq) do { \
241 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
242 } while (0)
243 #define KQ_NOTOWNED(kq) do { \
244 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
245 } while (0)
246
247 static struct knlist *
248 kn_list_lock(struct knote *kn)
249 {
250 struct knlist *knl;
251
252 knl = kn->kn_knlist;
253 if (knl != NULL)
254 knl->kl_lock(knl->kl_lockarg);
255 return (knl);
256 }
257
258 static void
259 kn_list_unlock(struct knlist *knl)
260 {
261 bool do_free;
262
263 if (knl == NULL)
264 return;
265 do_free = knl->kl_autodestroy && knlist_empty(knl);
266 knl->kl_unlock(knl->kl_lockarg);
267 if (do_free) {
268 knlist_destroy(knl);
269 free(knl, M_KQUEUE);
270 }
271 }
272
273 static bool
274 kn_in_flux(struct knote *kn)
275 {
276
277 return (kn->kn_influx > 0);
278 }
279
280 static void
281 kn_enter_flux(struct knote *kn)
282 {
283
284 KQ_OWNED(kn->kn_kq);
285 MPASS(kn->kn_influx < INT_MAX);
286 kn->kn_influx++;
287 }
288
289 static bool
290 kn_leave_flux(struct knote *kn)
291 {
292
293 KQ_OWNED(kn->kn_kq);
294 MPASS(kn->kn_influx > 0);
295 kn->kn_influx--;
296 return (kn->kn_influx == 0);
297 }
298
299 #define KNL_ASSERT_LOCK(knl, islocked) do { \
300 if (islocked) \
301 KNL_ASSERT_LOCKED(knl); \
302 else \
303 KNL_ASSERT_UNLOCKED(knl); \
304 } while (0)
305 #ifdef INVARIANTS
306 #define KNL_ASSERT_LOCKED(knl) do { \
307 knl->kl_assert_locked((knl)->kl_lockarg); \
308 } while (0)
309 #define KNL_ASSERT_UNLOCKED(knl) do { \
310 knl->kl_assert_unlocked((knl)->kl_lockarg); \
311 } while (0)
312 #else /* !INVARIANTS */
313 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
314 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
315 #endif /* INVARIANTS */
316
317 #ifndef KN_HASHSIZE
318 #define KN_HASHSIZE 64 /* XXX should be tunable */
319 #endif
320
321 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
322
323 static int
324 filt_nullattach(struct knote *kn)
325 {
326
327 return (ENXIO);
328 };
329
330 struct filterops null_filtops = {
331 .f_isfd = 0,
332 .f_attach = filt_nullattach,
333 };
334
335 /* XXX - make SYSINIT to add these, and move into respective modules. */
336 extern struct filterops sig_filtops;
337 extern struct filterops fs_filtops;
338
339 /*
340 * Table for for all system-defined filters.
341 */
342 static struct mtx filterops_lock;
343 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
344 MTX_DEF);
345 static struct {
346 struct filterops *for_fop;
347 int for_nolock;
348 int for_refcnt;
349 } sysfilt_ops[EVFILT_SYSCOUNT] = {
350 { &file_filtops, 1 }, /* EVFILT_READ */
351 { &file_filtops, 1 }, /* EVFILT_WRITE */
352 { &null_filtops }, /* EVFILT_AIO */
353 { &file_filtops, 1 }, /* EVFILT_VNODE */
354 { &proc_filtops, 1 }, /* EVFILT_PROC */
355 { &sig_filtops, 1 }, /* EVFILT_SIGNAL */
356 { &timer_filtops, 1 }, /* EVFILT_TIMER */
357 { &file_filtops, 1 }, /* EVFILT_PROCDESC */
358 { &fs_filtops, 1 }, /* EVFILT_FS */
359 { &null_filtops }, /* EVFILT_LIO */
360 { &user_filtops, 1 }, /* EVFILT_USER */
361 { &null_filtops }, /* EVFILT_SENDFILE */
362 { &file_filtops, 1 }, /* EVFILT_EMPTY */
363 };
364
365 /*
366 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
367 * method.
368 */
369 static int
370 filt_fileattach(struct knote *kn)
371 {
372
373 return (fo_kqfilter(kn->kn_fp, kn));
374 }
375
376 /*ARGSUSED*/
377 static int
378 kqueue_kqfilter(struct file *fp, struct knote *kn)
379 {
380 struct kqueue *kq = kn->kn_fp->f_data;
381
382 if (kn->kn_filter != EVFILT_READ)
383 return (EINVAL);
384
385 kn->kn_status |= KN_KQUEUE;
386 kn->kn_fop = &kqread_filtops;
387 knlist_add(&kq->kq_sel.si_note, kn, 0);
388
389 return (0);
390 }
391
392 static void
393 filt_kqdetach(struct knote *kn)
394 {
395 struct kqueue *kq = kn->kn_fp->f_data;
396
397 knlist_remove(&kq->kq_sel.si_note, kn, 0);
398 }
399
400 /*ARGSUSED*/
401 static int
402 filt_kqueue(struct knote *kn, long hint)
403 {
404 struct kqueue *kq = kn->kn_fp->f_data;
405
406 kn->kn_data = kq->kq_count;
407 return (kn->kn_data > 0);
408 }
409
410 /* XXX - move to kern_proc.c? */
411 static int
412 filt_procattach(struct knote *kn)
413 {
414 struct proc *p;
415 int error;
416 bool exiting, immediate;
417
418 exiting = immediate = false;
419 if (kn->kn_sfflags & NOTE_EXIT)
420 p = pfind_any(kn->kn_id);
421 else
422 p = pfind(kn->kn_id);
423 if (p == NULL)
424 return (ESRCH);
425 if (p->p_flag & P_WEXIT)
426 exiting = true;
427
428 if ((error = p_cansee(curthread, p))) {
429 PROC_UNLOCK(p);
430 return (error);
431 }
432
433 kn->kn_ptr.p_proc = p;
434 kn->kn_flags |= EV_CLEAR; /* automatically set */
435
436 /*
437 * Internal flag indicating registration done by kernel for the
438 * purposes of getting a NOTE_CHILD notification.
439 */
440 if (kn->kn_flags & EV_FLAG2) {
441 kn->kn_flags &= ~EV_FLAG2;
442 kn->kn_data = kn->kn_sdata; /* ppid */
443 kn->kn_fflags = NOTE_CHILD;
444 kn->kn_sfflags &= ~(NOTE_EXIT | NOTE_EXEC | NOTE_FORK);
445 immediate = true; /* Force immediate activation of child note. */
446 }
447 /*
448 * Internal flag indicating registration done by kernel (for other than
449 * NOTE_CHILD).
450 */
451 if (kn->kn_flags & EV_FLAG1) {
452 kn->kn_flags &= ~EV_FLAG1;
453 }
454
455 knlist_add(p->p_klist, kn, 1);
456
457 /*
458 * Immediately activate any child notes or, in the case of a zombie
459 * target process, exit notes. The latter is necessary to handle the
460 * case where the target process, e.g. a child, dies before the kevent
461 * is registered.
462 */
463 if (immediate || (exiting && filt_proc(kn, NOTE_EXIT)))
464 KNOTE_ACTIVATE(kn, 0);
465
466 PROC_UNLOCK(p);
467
468 return (0);
469 }
470
471 /*
472 * The knote may be attached to a different process, which may exit,
473 * leaving nothing for the knote to be attached to. So when the process
474 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
475 * it will be deleted when read out. However, as part of the knote deletion,
476 * this routine is called, so a check is needed to avoid actually performing
477 * a detach, because the original process does not exist any more.
478 */
479 /* XXX - move to kern_proc.c? */
480 static void
481 filt_procdetach(struct knote *kn)
482 {
483
484 knlist_remove(kn->kn_knlist, kn, 0);
485 kn->kn_ptr.p_proc = NULL;
486 }
487
488 /* XXX - move to kern_proc.c? */
489 static int
490 filt_proc(struct knote *kn, long hint)
491 {
492 struct proc *p;
493 u_int event;
494
495 p = kn->kn_ptr.p_proc;
496 if (p == NULL) /* already activated, from attach filter */
497 return (0);
498
499 /* Mask off extra data. */
500 event = (u_int)hint & NOTE_PCTRLMASK;
501
502 /* If the user is interested in this event, record it. */
503 if (kn->kn_sfflags & event)
504 kn->kn_fflags |= event;
505
506 /* Process is gone, so flag the event as finished. */
507 if (event == NOTE_EXIT) {
508 kn->kn_flags |= EV_EOF | EV_ONESHOT;
509 kn->kn_ptr.p_proc = NULL;
510 if (kn->kn_fflags & NOTE_EXIT)
511 kn->kn_data = KW_EXITCODE(p->p_xexit, p->p_xsig);
512 if (kn->kn_fflags == 0)
513 kn->kn_flags |= EV_DROP;
514 return (1);
515 }
516
517 return (kn->kn_fflags != 0);
518 }
519
520 /*
521 * Called when the process forked. It mostly does the same as the
522 * knote(), activating all knotes registered to be activated when the
523 * process forked. Additionally, for each knote attached to the
524 * parent, check whether user wants to track the new process. If so
525 * attach a new knote to it, and immediately report an event with the
526 * child's pid.
527 */
528 void
529 knote_fork(struct knlist *list, int pid)
530 {
531 struct kqueue *kq;
532 struct knote *kn;
533 struct kevent kev;
534 int error;
535
536 if (list == NULL)
537 return;
538
539 memset(&kev, 0, sizeof(kev));
540 list->kl_lock(list->kl_lockarg);
541 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
542 kq = kn->kn_kq;
543 KQ_LOCK(kq);
544 if (kn_in_flux(kn) && (kn->kn_status & KN_SCAN) == 0) {
545 KQ_UNLOCK(kq);
546 continue;
547 }
548
549 /*
550 * The same as knote(), activate the event.
551 */
552 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
553 kn->kn_status |= KN_HASKQLOCK;
554 if (kn->kn_fop->f_event(kn, NOTE_FORK))
555 KNOTE_ACTIVATE(kn, 1);
556 kn->kn_status &= ~KN_HASKQLOCK;
557 KQ_UNLOCK(kq);
558 continue;
559 }
560
561 /*
562 * The NOTE_TRACK case. In addition to the activation
563 * of the event, we need to register new events to
564 * track the child. Drop the locks in preparation for
565 * the call to kqueue_register().
566 */
567 kn_enter_flux(kn);
568 KQ_UNLOCK(kq);
569 list->kl_unlock(list->kl_lockarg);
570
571 /*
572 * Activate existing knote and register tracking knotes with
573 * new process.
574 *
575 * First register a knote to get just the child notice. This
576 * must be a separate note from a potential NOTE_EXIT
577 * notification since both NOTE_CHILD and NOTE_EXIT are defined
578 * to use the data field (in conflicting ways).
579 */
580 kev.ident = pid;
581 kev.filter = kn->kn_filter;
582 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_ONESHOT |
583 EV_FLAG2;
584 kev.fflags = kn->kn_sfflags;
585 kev.data = kn->kn_id; /* parent */
586 kev.udata = kn->kn_kevent.udata;/* preserve udata */
587 error = kqueue_register(kq, &kev, NULL, 0);
588 if (error)
589 kn->kn_fflags |= NOTE_TRACKERR;
590
591 /*
592 * Then register another knote to track other potential events
593 * from the new process.
594 */
595 kev.ident = pid;
596 kev.filter = kn->kn_filter;
597 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
598 kev.fflags = kn->kn_sfflags;
599 kev.data = kn->kn_id; /* parent */
600 kev.udata = kn->kn_kevent.udata;/* preserve udata */
601 error = kqueue_register(kq, &kev, NULL, 0);
602 if (error)
603 kn->kn_fflags |= NOTE_TRACKERR;
604 if (kn->kn_fop->f_event(kn, NOTE_FORK))
605 KNOTE_ACTIVATE(kn, 0);
606 list->kl_lock(list->kl_lockarg);
607 KQ_LOCK(kq);
608 kn_leave_flux(kn);
609 KQ_UNLOCK_FLUX(kq);
610 }
611 list->kl_unlock(list->kl_lockarg);
612 }
613
614 /*
615 * XXX: EVFILT_TIMER should perhaps live in kern_time.c beside the
616 * interval timer support code.
617 */
618
619 #define NOTE_TIMER_PRECMASK \
620 (NOTE_SECONDS | NOTE_MSECONDS | NOTE_USECONDS | NOTE_NSECONDS)
621
622 static sbintime_t
623 timer2sbintime(intptr_t data, int flags)
624 {
625 int64_t secs;
626
627 /*
628 * Macros for converting to the fractional second portion of an
629 * sbintime_t using 64bit multiplication to improve precision.
630 */
631 #define NS_TO_SBT(ns) (((ns) * (((uint64_t)1 << 63) / 500000000)) >> 32)
632 #define US_TO_SBT(us) (((us) * (((uint64_t)1 << 63) / 500000)) >> 32)
633 #define MS_TO_SBT(ms) (((ms) * (((uint64_t)1 << 63) / 500)) >> 32)
634 switch (flags & NOTE_TIMER_PRECMASK) {
635 case NOTE_SECONDS:
636 #ifdef __LP64__
637 if (data > (SBT_MAX / SBT_1S))
638 return (SBT_MAX);
639 #endif
640 return ((sbintime_t)data << 32);
641 case NOTE_MSECONDS: /* FALLTHROUGH */
642 case 0:
643 if (data >= 1000) {
644 secs = data / 1000;
645 #ifdef __LP64__
646 if (secs > (SBT_MAX / SBT_1S))
647 return (SBT_MAX);
648 #endif
649 return (secs << 32 | MS_TO_SBT(data % 1000));
650 }
651 return (MS_TO_SBT(data));
652 case NOTE_USECONDS:
653 if (data >= 1000000) {
654 secs = data / 1000000;
655 #ifdef __LP64__
656 if (secs > (SBT_MAX / SBT_1S))
657 return (SBT_MAX);
658 #endif
659 return (secs << 32 | US_TO_SBT(data % 1000000));
660 }
661 return (US_TO_SBT(data));
662 case NOTE_NSECONDS:
663 if (data >= 1000000000) {
664 secs = data / 1000000000;
665 #ifdef __LP64__
666 if (secs > (SBT_MAX / SBT_1S))
667 return (SBT_MAX);
668 #endif
669 return (secs << 32 | US_TO_SBT(data % 1000000000));
670 }
671 return (NS_TO_SBT(data));
672 default:
673 break;
674 }
675 return (-1);
676 }
677
678 struct kq_timer_cb_data {
679 struct callout c;
680 sbintime_t next; /* next timer event fires at */
681 sbintime_t to; /* precalculated timer period, 0 for abs */
682 };
683
684 static void
685 filt_timerexpire(void *knx)
686 {
687 struct knote *kn;
688 struct kq_timer_cb_data *kc;
689
690 kn = knx;
691 kn->kn_data++;
692 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
693
694 if ((kn->kn_flags & EV_ONESHOT) != 0)
695 return;
696 kc = kn->kn_ptr.p_v;
697 if (kc->to == 0)
698 return;
699 kc->next += kc->to;
700 callout_reset_sbt_on(&kc->c, kc->next, 0, filt_timerexpire, kn,
701 PCPU_GET(cpuid), C_ABSOLUTE);
702 }
703
704 /*
705 * data contains amount of time to sleep
706 */
707 static int
708 filt_timervalidate(struct knote *kn, sbintime_t *to)
709 {
710 struct bintime bt;
711 sbintime_t sbt;
712
713 if (kn->kn_sdata < 0)
714 return (EINVAL);
715 if (kn->kn_sdata == 0 && (kn->kn_flags & EV_ONESHOT) == 0)
716 kn->kn_sdata = 1;
717 /*
718 * The only fflags values supported are the timer unit
719 * (precision) and the absolute time indicator.
720 */
721 if ((kn->kn_sfflags & ~(NOTE_TIMER_PRECMASK | NOTE_ABSTIME)) != 0)
722 return (EINVAL);
723
724 *to = timer2sbintime(kn->kn_sdata, kn->kn_sfflags);
725 if ((kn->kn_sfflags & NOTE_ABSTIME) != 0) {
726 getboottimebin(&bt);
727 sbt = bttosbt(bt);
728 *to -= sbt;
729 }
730 if (*to < 0)
731 return (EINVAL);
732 return (0);
733 }
734
735 static int
736 filt_timerattach(struct knote *kn)
737 {
738 struct kq_timer_cb_data *kc;
739 sbintime_t to;
740 unsigned int ncallouts;
741 int error;
742
743 error = filt_timervalidate(kn, &to);
744 if (error != 0)
745 return (error);
746
747 do {
748 ncallouts = kq_ncallouts;
749 if (ncallouts >= kq_calloutmax)
750 return (ENOMEM);
751 } while (!atomic_cmpset_int(&kq_ncallouts, ncallouts, ncallouts + 1));
752
753 if ((kn->kn_sfflags & NOTE_ABSTIME) == 0)
754 kn->kn_flags |= EV_CLEAR; /* automatically set */
755 kn->kn_status &= ~KN_DETACHED; /* knlist_add clears it */
756 kn->kn_ptr.p_v = kc = malloc(sizeof(*kc), M_KQUEUE, M_WAITOK);
757 callout_init(&kc->c, 1);
758 filt_timerstart(kn, to);
759
760 return (0);
761 }
762
763 static void
764 filt_timerstart(struct knote *kn, sbintime_t to)
765 {
766 struct kq_timer_cb_data *kc;
767
768 kc = kn->kn_ptr.p_v;
769 if ((kn->kn_sfflags & NOTE_ABSTIME) != 0) {
770 kc->next = to;
771 kc->to = 0;
772 } else {
773 kc->next = to + sbinuptime();
774 kc->to = to;
775 }
776 callout_reset_sbt_on(&kc->c, kc->next, 0, filt_timerexpire, kn,
777 PCPU_GET(cpuid), C_ABSOLUTE);
778 }
779
780 static void
781 filt_timerdetach(struct knote *kn)
782 {
783 struct kq_timer_cb_data *kc;
784 unsigned int old __unused;
785
786 kc = kn->kn_ptr.p_v;
787 callout_drain(&kc->c);
788 free(kc, M_KQUEUE);
789 old = atomic_fetchadd_int(&kq_ncallouts, -1);
790 KASSERT(old > 0, ("Number of callouts cannot become negative"));
791 kn->kn_status |= KN_DETACHED; /* knlist_remove sets it */
792 }
793
794 static void
795 filt_timertouch(struct knote *kn, struct kevent *kev, u_long type)
796 {
797 struct kq_timer_cb_data *kc;
798 struct kqueue *kq;
799 sbintime_t to;
800 int error;
801
802 switch (type) {
803 case EVENT_REGISTER:
804 /* Handle re-added timers that update data/fflags */
805 if (kev->flags & EV_ADD) {
806 kc = kn->kn_ptr.p_v;
807
808 /* Drain any existing callout. */
809 callout_drain(&kc->c);
810
811 /* Throw away any existing undelivered record
812 * of the timer expiration. This is done under
813 * the presumption that if a process is
814 * re-adding this timer with new parameters,
815 * it is no longer interested in what may have
816 * happened under the old parameters. If it is
817 * interested, it can wait for the expiration,
818 * delete the old timer definition, and then
819 * add the new one.
820 *
821 * This has to be done while the kq is locked:
822 * - if enqueued, dequeue
823 * - make it no longer active
824 * - clear the count of expiration events
825 */
826 kq = kn->kn_kq;
827 KQ_LOCK(kq);
828 if (kn->kn_status & KN_QUEUED)
829 knote_dequeue(kn);
830
831 kn->kn_status &= ~KN_ACTIVE;
832 kn->kn_data = 0;
833 KQ_UNLOCK(kq);
834
835 /* Reschedule timer based on new data/fflags */
836 kn->kn_sfflags = kev->fflags;
837 kn->kn_sdata = kev->data;
838 error = filt_timervalidate(kn, &to);
839 if (error != 0) {
840 kn->kn_flags |= EV_ERROR;
841 kn->kn_data = error;
842 } else
843 filt_timerstart(kn, to);
844 }
845 break;
846
847 case EVENT_PROCESS:
848 *kev = kn->kn_kevent;
849 if (kn->kn_flags & EV_CLEAR) {
850 kn->kn_data = 0;
851 kn->kn_fflags = 0;
852 }
853 break;
854
855 default:
856 panic("filt_timertouch() - invalid type (%ld)", type);
857 break;
858 }
859 }
860
861 static int
862 filt_timer(struct knote *kn, long hint)
863 {
864
865 return (kn->kn_data != 0);
866 }
867
868 static int
869 filt_userattach(struct knote *kn)
870 {
871
872 /*
873 * EVFILT_USER knotes are not attached to anything in the kernel.
874 */
875 kn->kn_hook = NULL;
876 if (kn->kn_fflags & NOTE_TRIGGER)
877 kn->kn_hookid = 1;
878 else
879 kn->kn_hookid = 0;
880 return (0);
881 }
882
883 static void
884 filt_userdetach(__unused struct knote *kn)
885 {
886
887 /*
888 * EVFILT_USER knotes are not attached to anything in the kernel.
889 */
890 }
891
892 static int
893 filt_user(struct knote *kn, __unused long hint)
894 {
895
896 return (kn->kn_hookid);
897 }
898
899 static void
900 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
901 {
902 u_int ffctrl;
903
904 switch (type) {
905 case EVENT_REGISTER:
906 if (kev->fflags & NOTE_TRIGGER)
907 kn->kn_hookid = 1;
908
909 ffctrl = kev->fflags & NOTE_FFCTRLMASK;
910 kev->fflags &= NOTE_FFLAGSMASK;
911 switch (ffctrl) {
912 case NOTE_FFNOP:
913 break;
914
915 case NOTE_FFAND:
916 kn->kn_sfflags &= kev->fflags;
917 break;
918
919 case NOTE_FFOR:
920 kn->kn_sfflags |= kev->fflags;
921 break;
922
923 case NOTE_FFCOPY:
924 kn->kn_sfflags = kev->fflags;
925 break;
926
927 default:
928 /* XXX Return error? */
929 break;
930 }
931 kn->kn_sdata = kev->data;
932 if (kev->flags & EV_CLEAR) {
933 kn->kn_hookid = 0;
934 kn->kn_data = 0;
935 kn->kn_fflags = 0;
936 }
937 break;
938
939 case EVENT_PROCESS:
940 *kev = kn->kn_kevent;
941 kev->fflags = kn->kn_sfflags;
942 kev->data = kn->kn_sdata;
943 if (kn->kn_flags & EV_CLEAR) {
944 kn->kn_hookid = 0;
945 kn->kn_data = 0;
946 kn->kn_fflags = 0;
947 }
948 break;
949
950 default:
951 panic("filt_usertouch() - invalid type (%ld)", type);
952 break;
953 }
954 }
955
956 int
957 sys_kqueue(struct thread *td, struct kqueue_args *uap)
958 {
959
960 return (kern_kqueue(td, 0, NULL));
961 }
962
963 static void
964 kqueue_init(struct kqueue *kq)
965 {
966
967 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF | MTX_DUPOK);
968 TAILQ_INIT(&kq->kq_head);
969 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
970 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
971 }
972
973 int
974 kern_kqueue(struct thread *td, int flags, struct filecaps *fcaps)
975 {
976 struct filedesc *fdp;
977 struct kqueue *kq;
978 struct file *fp;
979 struct ucred *cred;
980 int fd, error;
981
982 fdp = td->td_proc->p_fd;
983 cred = td->td_ucred;
984 if (!chgkqcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_KQUEUES)))
985 return (ENOMEM);
986
987 error = falloc_caps(td, &fp, &fd, flags, fcaps);
988 if (error != 0) {
989 chgkqcnt(cred->cr_ruidinfo, -1, 0);
990 return (error);
991 }
992
993 /* An extra reference on `fp' has been held for us by falloc(). */
994 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
995 kqueue_init(kq);
996 kq->kq_fdp = fdp;
997 kq->kq_cred = crhold(cred);
998
999 FILEDESC_XLOCK(fdp);
1000 TAILQ_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
1001 FILEDESC_XUNLOCK(fdp);
1002
1003 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
1004 fdrop(fp, td);
1005
1006 td->td_retval[0] = fd;
1007 return (0);
1008 }
1009
1010 struct g_kevent_args {
1011 int fd;
1012 void *changelist;
1013 int nchanges;
1014 void *eventlist;
1015 int nevents;
1016 const struct timespec *timeout;
1017 };
1018
1019 int
1020 sys_kevent(struct thread *td, struct kevent_args *uap)
1021 {
1022 struct kevent_copyops k_ops = {
1023 .arg = uap,
1024 .k_copyout = kevent_copyout,
1025 .k_copyin = kevent_copyin,
1026 .kevent_size = sizeof(struct kevent),
1027 };
1028 struct g_kevent_args gk_args = {
1029 .fd = uap->fd,
1030 .changelist = uap->changelist,
1031 .nchanges = uap->nchanges,
1032 .eventlist = uap->eventlist,
1033 .nevents = uap->nevents,
1034 .timeout = uap->timeout,
1035 };
1036
1037 return (kern_kevent_generic(td, &gk_args, &k_ops, "kevent"));
1038 }
1039
1040 static int
1041 kern_kevent_generic(struct thread *td, struct g_kevent_args *uap,
1042 struct kevent_copyops *k_ops, const char *struct_name)
1043 {
1044 struct timespec ts, *tsp;
1045 #ifdef KTRACE
1046 struct kevent *eventlist = uap->eventlist;
1047 #endif
1048 int error;
1049
1050 if (uap->timeout != NULL) {
1051 error = copyin(uap->timeout, &ts, sizeof(ts));
1052 if (error)
1053 return (error);
1054 tsp = &ts;
1055 } else
1056 tsp = NULL;
1057
1058 #ifdef KTRACE
1059 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
1060 ktrstructarray(struct_name, UIO_USERSPACE, uap->changelist,
1061 uap->nchanges, k_ops->kevent_size);
1062 #endif
1063
1064 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
1065 k_ops, tsp);
1066
1067 #ifdef KTRACE
1068 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
1069 ktrstructarray(struct_name, UIO_USERSPACE, eventlist,
1070 td->td_retval[0], k_ops->kevent_size);
1071 #endif
1072
1073 return (error);
1074 }
1075
1076 /*
1077 * Copy 'count' items into the destination list pointed to by uap->eventlist.
1078 */
1079 static int
1080 kevent_copyout(void *arg, struct kevent *kevp, int count)
1081 {
1082 struct kevent_args *uap;
1083 int error;
1084
1085 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1086 uap = (struct kevent_args *)arg;
1087
1088 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
1089 if (error == 0)
1090 uap->eventlist += count;
1091 return (error);
1092 }
1093
1094 /*
1095 * Copy 'count' items from the list pointed to by uap->changelist.
1096 */
1097 static int
1098 kevent_copyin(void *arg, struct kevent *kevp, int count)
1099 {
1100 struct kevent_args *uap;
1101 int error;
1102
1103 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1104 uap = (struct kevent_args *)arg;
1105
1106 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
1107 if (error == 0)
1108 uap->changelist += count;
1109 return (error);
1110 }
1111
1112 #ifdef COMPAT_FREEBSD11
1113 static int
1114 kevent11_copyout(void *arg, struct kevent *kevp, int count)
1115 {
1116 struct freebsd11_kevent_args *uap;
1117 struct kevent_freebsd11 kev11;
1118 int error, i;
1119
1120 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1121 uap = (struct freebsd11_kevent_args *)arg;
1122
1123 for (i = 0; i < count; i++) {
1124 kev11.ident = kevp->ident;
1125 kev11.filter = kevp->filter;
1126 kev11.flags = kevp->flags;
1127 kev11.fflags = kevp->fflags;
1128 kev11.data = kevp->data;
1129 kev11.udata = kevp->udata;
1130 error = copyout(&kev11, uap->eventlist, sizeof(kev11));
1131 if (error != 0)
1132 break;
1133 uap->eventlist++;
1134 kevp++;
1135 }
1136 return (error);
1137 }
1138
1139 /*
1140 * Copy 'count' items from the list pointed to by uap->changelist.
1141 */
1142 static int
1143 kevent11_copyin(void *arg, struct kevent *kevp, int count)
1144 {
1145 struct freebsd11_kevent_args *uap;
1146 struct kevent_freebsd11 kev11;
1147 int error, i;
1148
1149 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1150 uap = (struct freebsd11_kevent_args *)arg;
1151
1152 for (i = 0; i < count; i++) {
1153 error = copyin(uap->changelist, &kev11, sizeof(kev11));
1154 if (error != 0)
1155 break;
1156 kevp->ident = kev11.ident;
1157 kevp->filter = kev11.filter;
1158 kevp->flags = kev11.flags;
1159 kevp->fflags = kev11.fflags;
1160 kevp->data = (uintptr_t)kev11.data;
1161 kevp->udata = kev11.udata;
1162 bzero(&kevp->ext, sizeof(kevp->ext));
1163 uap->changelist++;
1164 kevp++;
1165 }
1166 return (error);
1167 }
1168
1169 int
1170 freebsd11_kevent(struct thread *td, struct freebsd11_kevent_args *uap)
1171 {
1172 struct kevent_copyops k_ops = {
1173 .arg = uap,
1174 .k_copyout = kevent11_copyout,
1175 .k_copyin = kevent11_copyin,
1176 .kevent_size = sizeof(struct kevent_freebsd11),
1177 };
1178 struct g_kevent_args gk_args = {
1179 .fd = uap->fd,
1180 .changelist = uap->changelist,
1181 .nchanges = uap->nchanges,
1182 .eventlist = uap->eventlist,
1183 .nevents = uap->nevents,
1184 .timeout = uap->timeout,
1185 };
1186
1187 return (kern_kevent_generic(td, &gk_args, &k_ops, "kevent_freebsd11"));
1188 }
1189 #endif
1190
1191 int
1192 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
1193 struct kevent_copyops *k_ops, const struct timespec *timeout)
1194 {
1195 cap_rights_t rights;
1196 struct file *fp;
1197 int error;
1198
1199 cap_rights_init(&rights);
1200 if (nchanges > 0)
1201 cap_rights_set(&rights, CAP_KQUEUE_CHANGE);
1202 if (nevents > 0)
1203 cap_rights_set(&rights, CAP_KQUEUE_EVENT);
1204 error = fget(td, fd, &rights, &fp);
1205 if (error != 0)
1206 return (error);
1207
1208 error = kern_kevent_fp(td, fp, nchanges, nevents, k_ops, timeout);
1209 fdrop(fp, td);
1210
1211 return (error);
1212 }
1213
1214 static int
1215 kqueue_kevent(struct kqueue *kq, struct thread *td, int nchanges, int nevents,
1216 struct kevent_copyops *k_ops, const struct timespec *timeout)
1217 {
1218 struct kevent keva[KQ_NEVENTS];
1219 struct kevent *kevp, *changes;
1220 int i, n, nerrors, error;
1221
1222 nerrors = 0;
1223 while (nchanges > 0) {
1224 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
1225 error = k_ops->k_copyin(k_ops->arg, keva, n);
1226 if (error)
1227 return (error);
1228 changes = keva;
1229 for (i = 0; i < n; i++) {
1230 kevp = &changes[i];
1231 if (!kevp->filter)
1232 continue;
1233 kevp->flags &= ~EV_SYSFLAGS;
1234 error = kqueue_register(kq, kevp, td, 1);
1235 if (error || (kevp->flags & EV_RECEIPT)) {
1236 if (nevents == 0)
1237 return (error);
1238 kevp->flags = EV_ERROR;
1239 kevp->data = error;
1240 (void)k_ops->k_copyout(k_ops->arg, kevp, 1);
1241 nevents--;
1242 nerrors++;
1243 }
1244 }
1245 nchanges -= n;
1246 }
1247 if (nerrors) {
1248 td->td_retval[0] = nerrors;
1249 return (0);
1250 }
1251
1252 return (kqueue_scan(kq, nevents, k_ops, timeout, keva, td));
1253 }
1254
1255 int
1256 kern_kevent_fp(struct thread *td, struct file *fp, int nchanges, int nevents,
1257 struct kevent_copyops *k_ops, const struct timespec *timeout)
1258 {
1259 struct kqueue *kq;
1260 int error;
1261
1262 error = kqueue_acquire(fp, &kq);
1263 if (error != 0)
1264 return (error);
1265 error = kqueue_kevent(kq, td, nchanges, nevents, k_ops, timeout);
1266 kqueue_release(kq, 0);
1267 return (error);
1268 }
1269
1270 /*
1271 * Performs a kevent() call on a temporarily created kqueue. This can be
1272 * used to perform one-shot polling, similar to poll() and select().
1273 */
1274 int
1275 kern_kevent_anonymous(struct thread *td, int nevents,
1276 struct kevent_copyops *k_ops)
1277 {
1278 struct kqueue kq = {};
1279 int error;
1280
1281 kqueue_init(&kq);
1282 kq.kq_refcnt = 1;
1283 error = kqueue_kevent(&kq, td, nevents, nevents, k_ops, NULL);
1284 kqueue_drain(&kq, td);
1285 kqueue_destroy(&kq);
1286 return (error);
1287 }
1288
1289 int
1290 kqueue_add_filteropts(int filt, struct filterops *filtops)
1291 {
1292 int error;
1293
1294 error = 0;
1295 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
1296 printf(
1297 "trying to add a filterop that is out of range: %d is beyond %d\n",
1298 ~filt, EVFILT_SYSCOUNT);
1299 return EINVAL;
1300 }
1301 mtx_lock(&filterops_lock);
1302 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
1303 sysfilt_ops[~filt].for_fop != NULL)
1304 error = EEXIST;
1305 else {
1306 sysfilt_ops[~filt].for_fop = filtops;
1307 sysfilt_ops[~filt].for_refcnt = 0;
1308 }
1309 mtx_unlock(&filterops_lock);
1310
1311 return (error);
1312 }
1313
1314 int
1315 kqueue_del_filteropts(int filt)
1316 {
1317 int error;
1318
1319 error = 0;
1320 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
1321 return EINVAL;
1322
1323 mtx_lock(&filterops_lock);
1324 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
1325 sysfilt_ops[~filt].for_fop == NULL)
1326 error = EINVAL;
1327 else if (sysfilt_ops[~filt].for_refcnt != 0)
1328 error = EBUSY;
1329 else {
1330 sysfilt_ops[~filt].for_fop = &null_filtops;
1331 sysfilt_ops[~filt].for_refcnt = 0;
1332 }
1333 mtx_unlock(&filterops_lock);
1334
1335 return error;
1336 }
1337
1338 static struct filterops *
1339 kqueue_fo_find(int filt)
1340 {
1341
1342 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
1343 return NULL;
1344
1345 if (sysfilt_ops[~filt].for_nolock)
1346 return sysfilt_ops[~filt].for_fop;
1347
1348 mtx_lock(&filterops_lock);
1349 sysfilt_ops[~filt].for_refcnt++;
1350 if (sysfilt_ops[~filt].for_fop == NULL)
1351 sysfilt_ops[~filt].for_fop = &null_filtops;
1352 mtx_unlock(&filterops_lock);
1353
1354 return sysfilt_ops[~filt].for_fop;
1355 }
1356
1357 static void
1358 kqueue_fo_release(int filt)
1359 {
1360
1361 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
1362 return;
1363
1364 if (sysfilt_ops[~filt].for_nolock)
1365 return;
1366
1367 mtx_lock(&filterops_lock);
1368 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
1369 ("filter object refcount not valid on release"));
1370 sysfilt_ops[~filt].for_refcnt--;
1371 mtx_unlock(&filterops_lock);
1372 }
1373
1374 /*
1375 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
1376 * influence if memory allocation should wait. Make sure it is 0 if you
1377 * hold any mutexes.
1378 */
1379 static int
1380 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
1381 {
1382 struct filterops *fops;
1383 struct file *fp;
1384 struct knote *kn, *tkn;
1385 struct knlist *knl;
1386 int error, filt, event;
1387 int haskqglobal, filedesc_unlock;
1388
1389 if ((kev->flags & (EV_ENABLE | EV_DISABLE)) == (EV_ENABLE | EV_DISABLE))
1390 return (EINVAL);
1391
1392 fp = NULL;
1393 kn = NULL;
1394 knl = NULL;
1395 error = 0;
1396 haskqglobal = 0;
1397 filedesc_unlock = 0;
1398
1399 filt = kev->filter;
1400 fops = kqueue_fo_find(filt);
1401 if (fops == NULL)
1402 return EINVAL;
1403
1404 if (kev->flags & EV_ADD) {
1405 /*
1406 * Prevent waiting with locks. Non-sleepable
1407 * allocation failures are handled in the loop, only
1408 * if the spare knote appears to be actually required.
1409 */
1410 tkn = knote_alloc(waitok);
1411 } else {
1412 tkn = NULL;
1413 }
1414
1415 findkn:
1416 if (fops->f_isfd) {
1417 KASSERT(td != NULL, ("td is NULL"));
1418 if (kev->ident > INT_MAX)
1419 error = EBADF;
1420 else
1421 error = fget(td, kev->ident, &cap_event_rights, &fp);
1422 if (error)
1423 goto done;
1424
1425 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
1426 kev->ident, 0) != 0) {
1427 /* try again */
1428 fdrop(fp, td);
1429 fp = NULL;
1430 error = kqueue_expand(kq, fops, kev->ident, waitok);
1431 if (error)
1432 goto done;
1433 goto findkn;
1434 }
1435
1436 if (fp->f_type == DTYPE_KQUEUE) {
1437 /*
1438 * If we add some intelligence about what we are doing,
1439 * we should be able to support events on ourselves.
1440 * We need to know when we are doing this to prevent
1441 * getting both the knlist lock and the kq lock since
1442 * they are the same thing.
1443 */
1444 if (fp->f_data == kq) {
1445 error = EINVAL;
1446 goto done;
1447 }
1448
1449 /*
1450 * Pre-lock the filedesc before the global
1451 * lock mutex, see the comment in
1452 * kqueue_close().
1453 */
1454 FILEDESC_XLOCK(td->td_proc->p_fd);
1455 filedesc_unlock = 1;
1456 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1457 }
1458
1459 KQ_LOCK(kq);
1460 if (kev->ident < kq->kq_knlistsize) {
1461 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1462 if (kev->filter == kn->kn_filter)
1463 break;
1464 }
1465 } else {
1466 if ((kev->flags & EV_ADD) == EV_ADD) {
1467 error = kqueue_expand(kq, fops, kev->ident, waitok);
1468 if (error != 0)
1469 goto done;
1470 }
1471
1472 KQ_LOCK(kq);
1473
1474 /*
1475 * If possible, find an existing knote to use for this kevent.
1476 */
1477 if (kev->filter == EVFILT_PROC &&
1478 (kev->flags & (EV_FLAG1 | EV_FLAG2)) != 0) {
1479 /* This is an internal creation of a process tracking
1480 * note. Don't attempt to coalesce this with an
1481 * existing note.
1482 */
1483 ;
1484 } else if (kq->kq_knhashmask != 0) {
1485 struct klist *list;
1486
1487 list = &kq->kq_knhash[
1488 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1489 SLIST_FOREACH(kn, list, kn_link)
1490 if (kev->ident == kn->kn_id &&
1491 kev->filter == kn->kn_filter)
1492 break;
1493 }
1494 }
1495
1496 /* knote is in the process of changing, wait for it to stabilize. */
1497 if (kn != NULL && kn_in_flux(kn)) {
1498 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1499 if (filedesc_unlock) {
1500 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1501 filedesc_unlock = 0;
1502 }
1503 kq->kq_state |= KQ_FLUXWAIT;
1504 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1505 if (fp != NULL) {
1506 fdrop(fp, td);
1507 fp = NULL;
1508 }
1509 goto findkn;
1510 }
1511
1512 /*
1513 * kn now contains the matching knote, or NULL if no match
1514 */
1515 if (kn == NULL) {
1516 if (kev->flags & EV_ADD) {
1517 kn = tkn;
1518 tkn = NULL;
1519 if (kn == NULL) {
1520 KQ_UNLOCK(kq);
1521 error = ENOMEM;
1522 goto done;
1523 }
1524 kn->kn_fp = fp;
1525 kn->kn_kq = kq;
1526 kn->kn_fop = fops;
1527 /*
1528 * apply reference counts to knote structure, and
1529 * do not release it at the end of this routine.
1530 */
1531 fops = NULL;
1532 fp = NULL;
1533
1534 kn->kn_sfflags = kev->fflags;
1535 kn->kn_sdata = kev->data;
1536 kev->fflags = 0;
1537 kev->data = 0;
1538 kn->kn_kevent = *kev;
1539 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1540 EV_ENABLE | EV_DISABLE | EV_FORCEONESHOT);
1541 kn->kn_status = KN_DETACHED;
1542 if ((kev->flags & EV_DISABLE) != 0)
1543 kn->kn_status |= KN_DISABLED;
1544 kn_enter_flux(kn);
1545
1546 error = knote_attach(kn, kq);
1547 KQ_UNLOCK(kq);
1548 if (error != 0) {
1549 tkn = kn;
1550 goto done;
1551 }
1552
1553 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1554 knote_drop_detached(kn, td);
1555 goto done;
1556 }
1557 knl = kn_list_lock(kn);
1558 goto done_ev_add;
1559 } else {
1560 /* No matching knote and the EV_ADD flag is not set. */
1561 KQ_UNLOCK(kq);
1562 error = ENOENT;
1563 goto done;
1564 }
1565 }
1566
1567 if (kev->flags & EV_DELETE) {
1568 kn_enter_flux(kn);
1569 KQ_UNLOCK(kq);
1570 knote_drop(kn, td);
1571 goto done;
1572 }
1573
1574 if (kev->flags & EV_FORCEONESHOT) {
1575 kn->kn_flags |= EV_ONESHOT;
1576 KNOTE_ACTIVATE(kn, 1);
1577 }
1578
1579 if ((kev->flags & EV_ENABLE) != 0)
1580 kn->kn_status &= ~KN_DISABLED;
1581 else if ((kev->flags & EV_DISABLE) != 0)
1582 kn->kn_status |= KN_DISABLED;
1583
1584 /*
1585 * The user may change some filter values after the initial EV_ADD,
1586 * but doing so will not reset any filter which has already been
1587 * triggered.
1588 */
1589 kn->kn_status |= KN_SCAN;
1590 kn_enter_flux(kn);
1591 KQ_UNLOCK(kq);
1592 knl = kn_list_lock(kn);
1593 kn->kn_kevent.udata = kev->udata;
1594 if (!fops->f_isfd && fops->f_touch != NULL) {
1595 fops->f_touch(kn, kev, EVENT_REGISTER);
1596 } else {
1597 kn->kn_sfflags = kev->fflags;
1598 kn->kn_sdata = kev->data;
1599 }
1600
1601 done_ev_add:
1602 /*
1603 * We can get here with kn->kn_knlist == NULL. This can happen when
1604 * the initial attach event decides that the event is "completed"
1605 * already, e.g., filt_procattach() is called on a zombie process. It
1606 * will call filt_proc() which will remove it from the list, and NULL
1607 * kn_knlist.
1608 *
1609 * KN_DISABLED will be stable while the knote is in flux, so the
1610 * unlocked read will not race with an update.
1611 */
1612 if ((kn->kn_status & KN_DISABLED) == 0)
1613 event = kn->kn_fop->f_event(kn, 0);
1614 else
1615 event = 0;
1616
1617 KQ_LOCK(kq);
1618 if (event)
1619 kn->kn_status |= KN_ACTIVE;
1620 if ((kn->kn_status & (KN_ACTIVE | KN_DISABLED | KN_QUEUED)) ==
1621 KN_ACTIVE)
1622 knote_enqueue(kn);
1623 kn->kn_status &= ~KN_SCAN;
1624 kn_leave_flux(kn);
1625 kn_list_unlock(knl);
1626 KQ_UNLOCK_FLUX(kq);
1627
1628 done:
1629 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1630 if (filedesc_unlock)
1631 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1632 if (fp != NULL)
1633 fdrop(fp, td);
1634 knote_free(tkn);
1635 if (fops != NULL)
1636 kqueue_fo_release(filt);
1637 return (error);
1638 }
1639
1640 static int
1641 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1642 {
1643 int error;
1644 struct kqueue *kq;
1645
1646 error = 0;
1647
1648 kq = fp->f_data;
1649 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1650 return (EBADF);
1651 *kqp = kq;
1652 KQ_LOCK(kq);
1653 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1654 KQ_UNLOCK(kq);
1655 return (EBADF);
1656 }
1657 kq->kq_refcnt++;
1658 KQ_UNLOCK(kq);
1659
1660 return error;
1661 }
1662
1663 static void
1664 kqueue_release(struct kqueue *kq, int locked)
1665 {
1666 if (locked)
1667 KQ_OWNED(kq);
1668 else
1669 KQ_LOCK(kq);
1670 kq->kq_refcnt--;
1671 if (kq->kq_refcnt == 1)
1672 wakeup(&kq->kq_refcnt);
1673 if (!locked)
1674 KQ_UNLOCK(kq);
1675 }
1676
1677 static void
1678 kqueue_schedtask(struct kqueue *kq)
1679 {
1680
1681 KQ_OWNED(kq);
1682 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1683 ("scheduling kqueue task while draining"));
1684
1685 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1686 taskqueue_enqueue(taskqueue_kqueue_ctx, &kq->kq_task);
1687 kq->kq_state |= KQ_TASKSCHED;
1688 }
1689 }
1690
1691 /*
1692 * Expand the kq to make sure we have storage for fops/ident pair.
1693 *
1694 * Return 0 on success (or no work necessary), return errno on failure.
1695 *
1696 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1697 * If kqueue_register is called from a non-fd context, there usually/should
1698 * be no locks held.
1699 */
1700 static int
1701 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1702 int waitok)
1703 {
1704 struct klist *list, *tmp_knhash, *to_free;
1705 u_long tmp_knhashmask;
1706 int error, fd, size;
1707 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1708
1709 KQ_NOTOWNED(kq);
1710
1711 error = 0;
1712 to_free = NULL;
1713 if (fops->f_isfd) {
1714 fd = ident;
1715 if (kq->kq_knlistsize <= fd) {
1716 size = kq->kq_knlistsize;
1717 while (size <= fd)
1718 size += KQEXTENT;
1719 list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1720 if (list == NULL)
1721 return ENOMEM;
1722 KQ_LOCK(kq);
1723 if ((kq->kq_state & KQ_CLOSING) != 0) {
1724 to_free = list;
1725 error = EBADF;
1726 } else if (kq->kq_knlistsize > fd) {
1727 to_free = list;
1728 } else {
1729 if (kq->kq_knlist != NULL) {
1730 bcopy(kq->kq_knlist, list,
1731 kq->kq_knlistsize * sizeof(*list));
1732 to_free = kq->kq_knlist;
1733 kq->kq_knlist = NULL;
1734 }
1735 bzero((caddr_t)list +
1736 kq->kq_knlistsize * sizeof(*list),
1737 (size - kq->kq_knlistsize) * sizeof(*list));
1738 kq->kq_knlistsize = size;
1739 kq->kq_knlist = list;
1740 }
1741 KQ_UNLOCK(kq);
1742 }
1743 } else {
1744 if (kq->kq_knhashmask == 0) {
1745 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1746 &tmp_knhashmask);
1747 if (tmp_knhash == NULL)
1748 return (ENOMEM);
1749 KQ_LOCK(kq);
1750 if ((kq->kq_state & KQ_CLOSING) != 0) {
1751 to_free = tmp_knhash;
1752 error = EBADF;
1753 } else if (kq->kq_knhashmask == 0) {
1754 kq->kq_knhash = tmp_knhash;
1755 kq->kq_knhashmask = tmp_knhashmask;
1756 } else {
1757 to_free = tmp_knhash;
1758 }
1759 KQ_UNLOCK(kq);
1760 }
1761 }
1762 free(to_free, M_KQUEUE);
1763
1764 KQ_NOTOWNED(kq);
1765 return (error);
1766 }
1767
1768 static void
1769 kqueue_task(void *arg, int pending)
1770 {
1771 struct kqueue *kq;
1772 int haskqglobal;
1773
1774 haskqglobal = 0;
1775 kq = arg;
1776
1777 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1778 KQ_LOCK(kq);
1779
1780 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1781
1782 kq->kq_state &= ~KQ_TASKSCHED;
1783 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1784 wakeup(&kq->kq_state);
1785 }
1786 KQ_UNLOCK(kq);
1787 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1788 }
1789
1790 /*
1791 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1792 * We treat KN_MARKER knotes as if they are in flux.
1793 */
1794 static int
1795 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1796 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1797 {
1798 struct kevent *kevp;
1799 struct knote *kn, *marker;
1800 struct knlist *knl;
1801 sbintime_t asbt, rsbt;
1802 int count, error, haskqglobal, influx, nkev, touch;
1803
1804 count = maxevents;
1805 nkev = 0;
1806 error = 0;
1807 haskqglobal = 0;
1808
1809 if (maxevents == 0)
1810 goto done_nl;
1811
1812 rsbt = 0;
1813 if (tsp != NULL) {
1814 if (tsp->tv_sec < 0 || tsp->tv_nsec < 0 ||
1815 tsp->tv_nsec >= 1000000000) {
1816 error = EINVAL;
1817 goto done_nl;
1818 }
1819 if (timespecisset(tsp)) {
1820 if (tsp->tv_sec <= INT32_MAX) {
1821 rsbt = tstosbt(*tsp);
1822 if (TIMESEL(&asbt, rsbt))
1823 asbt += tc_tick_sbt;
1824 if (asbt <= SBT_MAX - rsbt)
1825 asbt += rsbt;
1826 else
1827 asbt = 0;
1828 rsbt >>= tc_precexp;
1829 } else
1830 asbt = 0;
1831 } else
1832 asbt = -1;
1833 } else
1834 asbt = 0;
1835 marker = knote_alloc(1);
1836 marker->kn_status = KN_MARKER;
1837 KQ_LOCK(kq);
1838
1839 retry:
1840 kevp = keva;
1841 if (kq->kq_count == 0) {
1842 if (asbt == -1) {
1843 error = EWOULDBLOCK;
1844 } else {
1845 kq->kq_state |= KQ_SLEEP;
1846 error = msleep_sbt(kq, &kq->kq_lock, PSOCK | PCATCH,
1847 "kqread", asbt, rsbt, C_ABSOLUTE);
1848 }
1849 if (error == 0)
1850 goto retry;
1851 /* don't restart after signals... */
1852 if (error == ERESTART)
1853 error = EINTR;
1854 else if (error == EWOULDBLOCK)
1855 error = 0;
1856 goto done;
1857 }
1858
1859 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1860 influx = 0;
1861 while (count) {
1862 KQ_OWNED(kq);
1863 kn = TAILQ_FIRST(&kq->kq_head);
1864
1865 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1866 kn_in_flux(kn)) {
1867 if (influx) {
1868 influx = 0;
1869 KQ_FLUX_WAKEUP(kq);
1870 }
1871 kq->kq_state |= KQ_FLUXWAIT;
1872 error = msleep(kq, &kq->kq_lock, PSOCK,
1873 "kqflxwt", 0);
1874 continue;
1875 }
1876
1877 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1878 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1879 kn->kn_status &= ~KN_QUEUED;
1880 kq->kq_count--;
1881 continue;
1882 }
1883 if (kn == marker) {
1884 KQ_FLUX_WAKEUP(kq);
1885 if (count == maxevents)
1886 goto retry;
1887 goto done;
1888 }
1889 KASSERT(!kn_in_flux(kn),
1890 ("knote %p is unexpectedly in flux", kn));
1891
1892 if ((kn->kn_flags & EV_DROP) == EV_DROP) {
1893 kn->kn_status &= ~KN_QUEUED;
1894 kn_enter_flux(kn);
1895 kq->kq_count--;
1896 KQ_UNLOCK(kq);
1897 /*
1898 * We don't need to lock the list since we've
1899 * marked it as in flux.
1900 */
1901 knote_drop(kn, td);
1902 KQ_LOCK(kq);
1903 continue;
1904 } else if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1905 kn->kn_status &= ~KN_QUEUED;
1906 kn_enter_flux(kn);
1907 kq->kq_count--;
1908 KQ_UNLOCK(kq);
1909 /*
1910 * We don't need to lock the list since we've
1911 * marked the knote as being in flux.
1912 */
1913 *kevp = kn->kn_kevent;
1914 knote_drop(kn, td);
1915 KQ_LOCK(kq);
1916 kn = NULL;
1917 } else {
1918 kn->kn_status |= KN_SCAN;
1919 kn_enter_flux(kn);
1920 KQ_UNLOCK(kq);
1921 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1922 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1923 knl = kn_list_lock(kn);
1924 if (kn->kn_fop->f_event(kn, 0) == 0) {
1925 KQ_LOCK(kq);
1926 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1927 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE |
1928 KN_SCAN);
1929 kn_leave_flux(kn);
1930 kq->kq_count--;
1931 kn_list_unlock(knl);
1932 influx = 1;
1933 continue;
1934 }
1935 touch = (!kn->kn_fop->f_isfd &&
1936 kn->kn_fop->f_touch != NULL);
1937 if (touch)
1938 kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
1939 else
1940 *kevp = kn->kn_kevent;
1941 KQ_LOCK(kq);
1942 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1943 if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
1944 /*
1945 * Manually clear knotes who weren't
1946 * 'touch'ed.
1947 */
1948 if (touch == 0 && kn->kn_flags & EV_CLEAR) {
1949 kn->kn_data = 0;
1950 kn->kn_fflags = 0;
1951 }
1952 if (kn->kn_flags & EV_DISPATCH)
1953 kn->kn_status |= KN_DISABLED;
1954 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1955 kq->kq_count--;
1956 } else
1957 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1958
1959 kn->kn_status &= ~KN_SCAN;
1960 kn_leave_flux(kn);
1961 kn_list_unlock(knl);
1962 influx = 1;
1963 }
1964
1965 /* we are returning a copy to the user */
1966 kevp++;
1967 nkev++;
1968 count--;
1969
1970 if (nkev == KQ_NEVENTS) {
1971 influx = 0;
1972 KQ_UNLOCK_FLUX(kq);
1973 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1974 nkev = 0;
1975 kevp = keva;
1976 KQ_LOCK(kq);
1977 if (error)
1978 break;
1979 }
1980 }
1981 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1982 done:
1983 KQ_OWNED(kq);
1984 KQ_UNLOCK_FLUX(kq);
1985 knote_free(marker);
1986 done_nl:
1987 KQ_NOTOWNED(kq);
1988 if (nkev != 0)
1989 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1990 td->td_retval[0] = maxevents - count;
1991 return (error);
1992 }
1993
1994 /*ARGSUSED*/
1995 static int
1996 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1997 struct ucred *active_cred, struct thread *td)
1998 {
1999 /*
2000 * Enabling sigio causes two major problems:
2001 * 1) infinite recursion:
2002 * Synopsys: kevent is being used to track signals and have FIOASYNC
2003 * set. On receipt of a signal this will cause a kqueue to recurse
2004 * into itself over and over. Sending the sigio causes the kqueue
2005 * to become ready, which in turn posts sigio again, forever.
2006 * Solution: this can be solved by setting a flag in the kqueue that
2007 * we have a SIGIO in progress.
2008 * 2) locking problems:
2009 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
2010 * us above the proc and pgrp locks.
2011 * Solution: Post a signal using an async mechanism, being sure to
2012 * record a generation count in the delivery so that we do not deliver
2013 * a signal to the wrong process.
2014 *
2015 * Note, these two mechanisms are somewhat mutually exclusive!
2016 */
2017 #if 0
2018 struct kqueue *kq;
2019
2020 kq = fp->f_data;
2021 switch (cmd) {
2022 case FIOASYNC:
2023 if (*(int *)data) {
2024 kq->kq_state |= KQ_ASYNC;
2025 } else {
2026 kq->kq_state &= ~KQ_ASYNC;
2027 }
2028 return (0);
2029
2030 case FIOSETOWN:
2031 return (fsetown(*(int *)data, &kq->kq_sigio));
2032
2033 case FIOGETOWN:
2034 *(int *)data = fgetown(&kq->kq_sigio);
2035 return (0);
2036 }
2037 #endif
2038
2039 return (ENOTTY);
2040 }
2041
2042 /*ARGSUSED*/
2043 static int
2044 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
2045 struct thread *td)
2046 {
2047 struct kqueue *kq;
2048 int revents = 0;
2049 int error;
2050
2051 if ((error = kqueue_acquire(fp, &kq)))
2052 return POLLERR;
2053
2054 KQ_LOCK(kq);
2055 if (events & (POLLIN | POLLRDNORM)) {
2056 if (kq->kq_count) {
2057 revents |= events & (POLLIN | POLLRDNORM);
2058 } else {
2059 selrecord(td, &kq->kq_sel);
2060 if (SEL_WAITING(&kq->kq_sel))
2061 kq->kq_state |= KQ_SEL;
2062 }
2063 }
2064 kqueue_release(kq, 1);
2065 KQ_UNLOCK(kq);
2066 return (revents);
2067 }
2068
2069 /*ARGSUSED*/
2070 static int
2071 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2072 struct thread *td)
2073 {
2074
2075 bzero((void *)st, sizeof *st);
2076 /*
2077 * We no longer return kq_count because the unlocked value is useless.
2078 * If you spent all this time getting the count, why not spend your
2079 * syscall better by calling kevent?
2080 *
2081 * XXX - This is needed for libc_r.
2082 */
2083 st->st_mode = S_IFIFO;
2084 return (0);
2085 }
2086
2087 static void
2088 kqueue_drain(struct kqueue *kq, struct thread *td)
2089 {
2090 struct knote *kn;
2091 int i;
2092
2093 KQ_LOCK(kq);
2094
2095 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
2096 ("kqueue already closing"));
2097 kq->kq_state |= KQ_CLOSING;
2098 if (kq->kq_refcnt > 1)
2099 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
2100
2101 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
2102
2103 KASSERT(knlist_empty(&kq->kq_sel.si_note),
2104 ("kqueue's knlist not empty"));
2105
2106 for (i = 0; i < kq->kq_knlistsize; i++) {
2107 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
2108 if (kn_in_flux(kn)) {
2109 kq->kq_state |= KQ_FLUXWAIT;
2110 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
2111 continue;
2112 }
2113 kn_enter_flux(kn);
2114 KQ_UNLOCK(kq);
2115 knote_drop(kn, td);
2116 KQ_LOCK(kq);
2117 }
2118 }
2119 if (kq->kq_knhashmask != 0) {
2120 for (i = 0; i <= kq->kq_knhashmask; i++) {
2121 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
2122 if (kn_in_flux(kn)) {
2123 kq->kq_state |= KQ_FLUXWAIT;
2124 msleep(kq, &kq->kq_lock, PSOCK,
2125 "kqclo2", 0);
2126 continue;
2127 }
2128 kn_enter_flux(kn);
2129 KQ_UNLOCK(kq);
2130 knote_drop(kn, td);
2131 KQ_LOCK(kq);
2132 }
2133 }
2134 }
2135
2136 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
2137 kq->kq_state |= KQ_TASKDRAIN;
2138 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
2139 }
2140
2141 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
2142 selwakeuppri(&kq->kq_sel, PSOCK);
2143 if (!SEL_WAITING(&kq->kq_sel))
2144 kq->kq_state &= ~KQ_SEL;
2145 }
2146
2147 KQ_UNLOCK(kq);
2148 }
2149
2150 static void
2151 kqueue_destroy(struct kqueue *kq)
2152 {
2153
2154 KASSERT(kq->kq_fdp == NULL,
2155 ("kqueue still attached to a file descriptor"));
2156 seldrain(&kq->kq_sel);
2157 knlist_destroy(&kq->kq_sel.si_note);
2158 mtx_destroy(&kq->kq_lock);
2159
2160 if (kq->kq_knhash != NULL)
2161 free(kq->kq_knhash, M_KQUEUE);
2162 if (kq->kq_knlist != NULL)
2163 free(kq->kq_knlist, M_KQUEUE);
2164
2165 funsetown(&kq->kq_sigio);
2166 }
2167
2168 /*ARGSUSED*/
2169 static int
2170 kqueue_close(struct file *fp, struct thread *td)
2171 {
2172 struct kqueue *kq = fp->f_data;
2173 struct filedesc *fdp;
2174 int error;
2175 int filedesc_unlock;
2176
2177 if ((error = kqueue_acquire(fp, &kq)))
2178 return error;
2179 kqueue_drain(kq, td);
2180
2181 /*
2182 * We could be called due to the knote_drop() doing fdrop(),
2183 * called from kqueue_register(). In this case the global
2184 * lock is owned, and filedesc sx is locked before, to not
2185 * take the sleepable lock after non-sleepable.
2186 */
2187 fdp = kq->kq_fdp;
2188 kq->kq_fdp = NULL;
2189 if (!sx_xlocked(FILEDESC_LOCK(fdp))) {
2190 FILEDESC_XLOCK(fdp);
2191 filedesc_unlock = 1;
2192 } else
2193 filedesc_unlock = 0;
2194 TAILQ_REMOVE(&fdp->fd_kqlist, kq, kq_list);
2195 if (filedesc_unlock)
2196 FILEDESC_XUNLOCK(fdp);
2197
2198 kqueue_destroy(kq);
2199 chgkqcnt(kq->kq_cred->cr_ruidinfo, -1, 0);
2200 crfree(kq->kq_cred);
2201 free(kq, M_KQUEUE);
2202 fp->f_data = NULL;
2203
2204 return (0);
2205 }
2206
2207 static int
2208 kqueue_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
2209 {
2210
2211 kif->kf_type = KF_TYPE_KQUEUE;
2212 return (0);
2213 }
2214
2215 static void
2216 kqueue_wakeup(struct kqueue *kq)
2217 {
2218 KQ_OWNED(kq);
2219
2220 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
2221 kq->kq_state &= ~KQ_SLEEP;
2222 wakeup(kq);
2223 }
2224 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
2225 selwakeuppri(&kq->kq_sel, PSOCK);
2226 if (!SEL_WAITING(&kq->kq_sel))
2227 kq->kq_state &= ~KQ_SEL;
2228 }
2229 if (!knlist_empty(&kq->kq_sel.si_note))
2230 kqueue_schedtask(kq);
2231 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
2232 pgsigio(&kq->kq_sigio, SIGIO, 0);
2233 }
2234 }
2235
2236 /*
2237 * Walk down a list of knotes, activating them if their event has triggered.
2238 *
2239 * There is a possibility to optimize in the case of one kq watching another.
2240 * Instead of scheduling a task to wake it up, you could pass enough state
2241 * down the chain to make up the parent kqueue. Make this code functional
2242 * first.
2243 */
2244 void
2245 knote(struct knlist *list, long hint, int lockflags)
2246 {
2247 struct kqueue *kq;
2248 struct knote *kn, *tkn;
2249 int error;
2250
2251 if (list == NULL)
2252 return;
2253
2254 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
2255
2256 if ((lockflags & KNF_LISTLOCKED) == 0)
2257 list->kl_lock(list->kl_lockarg);
2258
2259 /*
2260 * If we unlock the list lock (and enter influx), we can
2261 * eliminate the kqueue scheduling, but this will introduce
2262 * four lock/unlock's for each knote to test. Also, marker
2263 * would be needed to keep iteration position, since filters
2264 * or other threads could remove events.
2265 */
2266 SLIST_FOREACH_SAFE(kn, &list->kl_list, kn_selnext, tkn) {
2267 kq = kn->kn_kq;
2268 KQ_LOCK(kq);
2269 if (kn_in_flux(kn) && (kn->kn_status & KN_SCAN) == 0) {
2270 /*
2271 * Do not process the influx notes, except for
2272 * the influx coming from the kq unlock in the
2273 * kqueue_scan(). In the later case, we do
2274 * not interfere with the scan, since the code
2275 * fragment in kqueue_scan() locks the knlist,
2276 * and cannot proceed until we finished.
2277 */
2278 KQ_UNLOCK(kq);
2279 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
2280 kn_enter_flux(kn);
2281 KQ_UNLOCK(kq);
2282 error = kn->kn_fop->f_event(kn, hint);
2283 KQ_LOCK(kq);
2284 kn_leave_flux(kn);
2285 if (error)
2286 KNOTE_ACTIVATE(kn, 1);
2287 KQ_UNLOCK_FLUX(kq);
2288 } else {
2289 kn->kn_status |= KN_HASKQLOCK;
2290 if (kn->kn_fop->f_event(kn, hint))
2291 KNOTE_ACTIVATE(kn, 1);
2292 kn->kn_status &= ~KN_HASKQLOCK;
2293 KQ_UNLOCK(kq);
2294 }
2295 }
2296 if ((lockflags & KNF_LISTLOCKED) == 0)
2297 list->kl_unlock(list->kl_lockarg);
2298 }
2299
2300 /*
2301 * add a knote to a knlist
2302 */
2303 void
2304 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
2305 {
2306
2307 KNL_ASSERT_LOCK(knl, islocked);
2308 KQ_NOTOWNED(kn->kn_kq);
2309 KASSERT(kn_in_flux(kn), ("knote %p not in flux", kn));
2310 KASSERT((kn->kn_status & KN_DETACHED) != 0,
2311 ("knote %p was not detached", kn));
2312 if (!islocked)
2313 knl->kl_lock(knl->kl_lockarg);
2314 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
2315 if (!islocked)
2316 knl->kl_unlock(knl->kl_lockarg);
2317 KQ_LOCK(kn->kn_kq);
2318 kn->kn_knlist = knl;
2319 kn->kn_status &= ~KN_DETACHED;
2320 KQ_UNLOCK(kn->kn_kq);
2321 }
2322
2323 static void
2324 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked,
2325 int kqislocked)
2326 {
2327
2328 KASSERT(!kqislocked || knlislocked, ("kq locked w/o knl locked"));
2329 KNL_ASSERT_LOCK(knl, knlislocked);
2330 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
2331 KASSERT(kqislocked || kn_in_flux(kn), ("knote %p not in flux", kn));
2332 KASSERT((kn->kn_status & KN_DETACHED) == 0,
2333 ("knote %p was already detached", kn));
2334 if (!knlislocked)
2335 knl->kl_lock(knl->kl_lockarg);
2336 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
2337 kn->kn_knlist = NULL;
2338 if (!knlislocked)
2339 kn_list_unlock(knl);
2340 if (!kqislocked)
2341 KQ_LOCK(kn->kn_kq);
2342 kn->kn_status |= KN_DETACHED;
2343 if (!kqislocked)
2344 KQ_UNLOCK(kn->kn_kq);
2345 }
2346
2347 /*
2348 * remove knote from the specified knlist
2349 */
2350 void
2351 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
2352 {
2353
2354 knlist_remove_kq(knl, kn, islocked, 0);
2355 }
2356
2357 int
2358 knlist_empty(struct knlist *knl)
2359 {
2360
2361 KNL_ASSERT_LOCKED(knl);
2362 return (SLIST_EMPTY(&knl->kl_list));
2363 }
2364
2365 static struct mtx knlist_lock;
2366 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
2367 MTX_DEF);
2368 static void knlist_mtx_lock(void *arg);
2369 static void knlist_mtx_unlock(void *arg);
2370
2371 static void
2372 knlist_mtx_lock(void *arg)
2373 {
2374
2375 mtx_lock((struct mtx *)arg);
2376 }
2377
2378 static void
2379 knlist_mtx_unlock(void *arg)
2380 {
2381
2382 mtx_unlock((struct mtx *)arg);
2383 }
2384
2385 static void
2386 knlist_mtx_assert_locked(void *arg)
2387 {
2388
2389 mtx_assert((struct mtx *)arg, MA_OWNED);
2390 }
2391
2392 static void
2393 knlist_mtx_assert_unlocked(void *arg)
2394 {
2395
2396 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
2397 }
2398
2399 static void
2400 knlist_rw_rlock(void *arg)
2401 {
2402
2403 rw_rlock((struct rwlock *)arg);
2404 }
2405
2406 static void
2407 knlist_rw_runlock(void *arg)
2408 {
2409
2410 rw_runlock((struct rwlock *)arg);
2411 }
2412
2413 static void
2414 knlist_rw_assert_locked(void *arg)
2415 {
2416
2417 rw_assert((struct rwlock *)arg, RA_LOCKED);
2418 }
2419
2420 static void
2421 knlist_rw_assert_unlocked(void *arg)
2422 {
2423
2424 rw_assert((struct rwlock *)arg, RA_UNLOCKED);
2425 }
2426
2427 void
2428 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
2429 void (*kl_unlock)(void *),
2430 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
2431 {
2432
2433 if (lock == NULL)
2434 knl->kl_lockarg = &knlist_lock;
2435 else
2436 knl->kl_lockarg = lock;
2437
2438 if (kl_lock == NULL)
2439 knl->kl_lock = knlist_mtx_lock;
2440 else
2441 knl->kl_lock = kl_lock;
2442 if (kl_unlock == NULL)
2443 knl->kl_unlock = knlist_mtx_unlock;
2444 else
2445 knl->kl_unlock = kl_unlock;
2446 if (kl_assert_locked == NULL)
2447 knl->kl_assert_locked = knlist_mtx_assert_locked;
2448 else
2449 knl->kl_assert_locked = kl_assert_locked;
2450 if (kl_assert_unlocked == NULL)
2451 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
2452 else
2453 knl->kl_assert_unlocked = kl_assert_unlocked;
2454
2455 knl->kl_autodestroy = 0;
2456 SLIST_INIT(&knl->kl_list);
2457 }
2458
2459 void
2460 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
2461 {
2462
2463 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
2464 }
2465
2466 struct knlist *
2467 knlist_alloc(struct mtx *lock)
2468 {
2469 struct knlist *knl;
2470
2471 knl = malloc(sizeof(struct knlist), M_KQUEUE, M_WAITOK);
2472 knlist_init_mtx(knl, lock);
2473 return (knl);
2474 }
2475
2476 void
2477 knlist_init_rw_reader(struct knlist *knl, struct rwlock *lock)
2478 {
2479
2480 knlist_init(knl, lock, knlist_rw_rlock, knlist_rw_runlock,
2481 knlist_rw_assert_locked, knlist_rw_assert_unlocked);
2482 }
2483
2484 void
2485 knlist_destroy(struct knlist *knl)
2486 {
2487
2488 KASSERT(KNLIST_EMPTY(knl),
2489 ("destroying knlist %p with knotes on it", knl));
2490 }
2491
2492 void
2493 knlist_detach(struct knlist *knl)
2494 {
2495
2496 KNL_ASSERT_LOCKED(knl);
2497 knl->kl_autodestroy = 1;
2498 if (knlist_empty(knl)) {
2499 knlist_destroy(knl);
2500 free(knl, M_KQUEUE);
2501 }
2502 }
2503
2504 /*
2505 * Even if we are locked, we may need to drop the lock to allow any influx
2506 * knotes time to "settle".
2507 */
2508 void
2509 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
2510 {
2511 struct knote *kn, *kn2;
2512 struct kqueue *kq;
2513
2514 KASSERT(!knl->kl_autodestroy, ("cleardel for autodestroy %p", knl));
2515 if (islocked)
2516 KNL_ASSERT_LOCKED(knl);
2517 else {
2518 KNL_ASSERT_UNLOCKED(knl);
2519 again: /* need to reacquire lock since we have dropped it */
2520 knl->kl_lock(knl->kl_lockarg);
2521 }
2522
2523 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
2524 kq = kn->kn_kq;
2525 KQ_LOCK(kq);
2526 if (kn_in_flux(kn)) {
2527 KQ_UNLOCK(kq);
2528 continue;
2529 }
2530 knlist_remove_kq(knl, kn, 1, 1);
2531 if (killkn) {
2532 kn_enter_flux(kn);
2533 KQ_UNLOCK(kq);
2534 knote_drop_detached(kn, td);
2535 } else {
2536 /* Make sure cleared knotes disappear soon */
2537 kn->kn_flags |= EV_EOF | EV_ONESHOT;
2538 KQ_UNLOCK(kq);
2539 }
2540 kq = NULL;
2541 }
2542
2543 if (!SLIST_EMPTY(&knl->kl_list)) {
2544 /* there are still in flux knotes remaining */
2545 kn = SLIST_FIRST(&knl->kl_list);
2546 kq = kn->kn_kq;
2547 KQ_LOCK(kq);
2548 KASSERT(kn_in_flux(kn), ("knote removed w/o list lock"));
2549 knl->kl_unlock(knl->kl_lockarg);
2550 kq->kq_state |= KQ_FLUXWAIT;
2551 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2552 kq = NULL;
2553 goto again;
2554 }
2555
2556 if (islocked)
2557 KNL_ASSERT_LOCKED(knl);
2558 else {
2559 knl->kl_unlock(knl->kl_lockarg);
2560 KNL_ASSERT_UNLOCKED(knl);
2561 }
2562 }
2563
2564 /*
2565 * Remove all knotes referencing a specified fd must be called with FILEDESC
2566 * lock. This prevents a race where a new fd comes along and occupies the
2567 * entry and we attach a knote to the fd.
2568 */
2569 void
2570 knote_fdclose(struct thread *td, int fd)
2571 {
2572 struct filedesc *fdp = td->td_proc->p_fd;
2573 struct kqueue *kq;
2574 struct knote *kn;
2575 int influx;
2576
2577 FILEDESC_XLOCK_ASSERT(fdp);
2578
2579 /*
2580 * We shouldn't have to worry about new kevents appearing on fd
2581 * since filedesc is locked.
2582 */
2583 TAILQ_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2584 KQ_LOCK(kq);
2585
2586 again:
2587 influx = 0;
2588 while (kq->kq_knlistsize > fd &&
2589 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2590 if (kn_in_flux(kn)) {
2591 /* someone else might be waiting on our knote */
2592 if (influx)
2593 wakeup(kq);
2594 kq->kq_state |= KQ_FLUXWAIT;
2595 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2596 goto again;
2597 }
2598 kn_enter_flux(kn);
2599 KQ_UNLOCK(kq);
2600 influx = 1;
2601 knote_drop(kn, td);
2602 KQ_LOCK(kq);
2603 }
2604 KQ_UNLOCK_FLUX(kq);
2605 }
2606 }
2607
2608 static int
2609 knote_attach(struct knote *kn, struct kqueue *kq)
2610 {
2611 struct klist *list;
2612
2613 KASSERT(kn_in_flux(kn), ("knote %p not marked influx", kn));
2614 KQ_OWNED(kq);
2615
2616 if ((kq->kq_state & KQ_CLOSING) != 0)
2617 return (EBADF);
2618 if (kn->kn_fop->f_isfd) {
2619 if (kn->kn_id >= kq->kq_knlistsize)
2620 return (ENOMEM);
2621 list = &kq->kq_knlist[kn->kn_id];
2622 } else {
2623 if (kq->kq_knhash == NULL)
2624 return (ENOMEM);
2625 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2626 }
2627 SLIST_INSERT_HEAD(list, kn, kn_link);
2628 return (0);
2629 }
2630
2631 static void
2632 knote_drop(struct knote *kn, struct thread *td)
2633 {
2634
2635 if ((kn->kn_status & KN_DETACHED) == 0)
2636 kn->kn_fop->f_detach(kn);
2637 knote_drop_detached(kn, td);
2638 }
2639
2640 static void
2641 knote_drop_detached(struct knote *kn, struct thread *td)
2642 {
2643 struct kqueue *kq;
2644 struct klist *list;
2645
2646 kq = kn->kn_kq;
2647
2648 KASSERT((kn->kn_status & KN_DETACHED) != 0,
2649 ("knote %p still attached", kn));
2650 KQ_NOTOWNED(kq);
2651
2652 KQ_LOCK(kq);
2653 KASSERT(kn->kn_influx == 1,
2654 ("knote_drop called on %p with influx %d", kn, kn->kn_influx));
2655
2656 if (kn->kn_fop->f_isfd)
2657 list = &kq->kq_knlist[kn->kn_id];
2658 else
2659 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2660
2661 if (!SLIST_EMPTY(list))
2662 SLIST_REMOVE(list, kn, knote, kn_link);
2663 if (kn->kn_status & KN_QUEUED)
2664 knote_dequeue(kn);
2665 KQ_UNLOCK_FLUX(kq);
2666
2667 if (kn->kn_fop->f_isfd) {
2668 fdrop(kn->kn_fp, td);
2669 kn->kn_fp = NULL;
2670 }
2671 kqueue_fo_release(kn->kn_kevent.filter);
2672 kn->kn_fop = NULL;
2673 knote_free(kn);
2674 }
2675
2676 static void
2677 knote_enqueue(struct knote *kn)
2678 {
2679 struct kqueue *kq = kn->kn_kq;
2680
2681 KQ_OWNED(kn->kn_kq);
2682 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2683
2684 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2685 kn->kn_status |= KN_QUEUED;
2686 kq->kq_count++;
2687 kqueue_wakeup(kq);
2688 }
2689
2690 static void
2691 knote_dequeue(struct knote *kn)
2692 {
2693 struct kqueue *kq = kn->kn_kq;
2694
2695 KQ_OWNED(kn->kn_kq);
2696 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2697
2698 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2699 kn->kn_status &= ~KN_QUEUED;
2700 kq->kq_count--;
2701 }
2702
2703 static void
2704 knote_init(void)
2705 {
2706
2707 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2708 NULL, NULL, UMA_ALIGN_PTR, 0);
2709 }
2710 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2711
2712 static struct knote *
2713 knote_alloc(int waitok)
2714 {
2715
2716 return (uma_zalloc(knote_zone, (waitok ? M_WAITOK : M_NOWAIT) |
2717 M_ZERO));
2718 }
2719
2720 static void
2721 knote_free(struct knote *kn)
2722 {
2723
2724 uma_zfree(knote_zone, kn);
2725 }
2726
2727 /*
2728 * Register the kev w/ the kq specified by fd.
2729 */
2730 int
2731 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2732 {
2733 struct kqueue *kq;
2734 struct file *fp;
2735 cap_rights_t rights;
2736 int error;
2737
2738 error = fget(td, fd, cap_rights_init(&rights, CAP_KQUEUE_CHANGE), &fp);
2739 if (error != 0)
2740 return (error);
2741 if ((error = kqueue_acquire(fp, &kq)) != 0)
2742 goto noacquire;
2743
2744 error = kqueue_register(kq, kev, td, waitok);
2745 kqueue_release(kq, 0);
2746
2747 noacquire:
2748 fdrop(fp, td);
2749 return (error);
2750 }
Cache object: d6a94a06cb6bf058d019303c73e48a9c
|