FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_event.c
1 /*-
2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_ktrace.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 #include <sys/malloc.h>
40 #include <sys/unistd.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
44 #include <sys/fcntl.h>
45 #include <sys/kthread.h>
46 #include <sys/selinfo.h>
47 #include <sys/queue.h>
48 #include <sys/event.h>
49 #include <sys/eventvar.h>
50 #include <sys/poll.h>
51 #include <sys/protosw.h>
52 #include <sys/sigio.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/stat.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysproto.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/taskqueue.h>
61 #include <sys/uio.h>
62 #ifdef KTRACE
63 #include <sys/ktrace.h>
64 #endif
65
66 #include <vm/uma.h>
67
68 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
69
70 /*
71 * This lock is used if multiple kq locks are required. This possibly
72 * should be made into a per proc lock.
73 */
74 static struct mtx kq_global;
75 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
76 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
77 if (!haslck) \
78 mtx_lock(lck); \
79 haslck = 1; \
80 } while (0)
81 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
82 if (haslck) \
83 mtx_unlock(lck); \
84 haslck = 0; \
85 } while (0)
86
87 TASKQUEUE_DEFINE_THREAD(kqueue);
88
89 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
90 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
91 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
92 struct thread *td, int waitok);
93 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
94 static void kqueue_release(struct kqueue *kq, int locked);
95 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
96 uintptr_t ident, int waitok);
97 static void kqueue_task(void *arg, int pending);
98 static int kqueue_scan(struct kqueue *kq, int maxevents,
99 struct kevent_copyops *k_ops,
100 const struct timespec *timeout,
101 struct kevent *keva, struct thread *td);
102 static void kqueue_wakeup(struct kqueue *kq);
103 static struct filterops *kqueue_fo_find(int filt);
104 static void kqueue_fo_release(int filt);
105
106 static fo_rdwr_t kqueue_read;
107 static fo_rdwr_t kqueue_write;
108 static fo_ioctl_t kqueue_ioctl;
109 static fo_poll_t kqueue_poll;
110 static fo_kqfilter_t kqueue_kqfilter;
111 static fo_stat_t kqueue_stat;
112 static fo_close_t kqueue_close;
113
114 static struct fileops kqueueops = {
115 .fo_read = kqueue_read,
116 .fo_write = kqueue_write,
117 .fo_ioctl = kqueue_ioctl,
118 .fo_poll = kqueue_poll,
119 .fo_kqfilter = kqueue_kqfilter,
120 .fo_stat = kqueue_stat,
121 .fo_close = kqueue_close,
122 };
123
124 static int knote_attach(struct knote *kn, struct kqueue *kq);
125 static void knote_drop(struct knote *kn, struct thread *td);
126 static void knote_enqueue(struct knote *kn);
127 static void knote_dequeue(struct knote *kn);
128 static void knote_init(void);
129 static struct knote *knote_alloc(int waitok);
130 static void knote_free(struct knote *kn);
131
132 static void filt_kqdetach(struct knote *kn);
133 static int filt_kqueue(struct knote *kn, long hint);
134 static int filt_procattach(struct knote *kn);
135 static void filt_procdetach(struct knote *kn);
136 static int filt_proc(struct knote *kn, long hint);
137 static int filt_fileattach(struct knote *kn);
138 static void filt_timerexpire(void *knx);
139 static int filt_timerattach(struct knote *kn);
140 static void filt_timerdetach(struct knote *kn);
141 static int filt_timer(struct knote *kn, long hint);
142
143 static struct filterops file_filtops =
144 { 1, filt_fileattach, NULL, NULL };
145 static struct filterops kqread_filtops =
146 { 1, NULL, filt_kqdetach, filt_kqueue };
147 /* XXX - move to kern_proc.c? */
148 static struct filterops proc_filtops =
149 { 0, filt_procattach, filt_procdetach, filt_proc };
150 static struct filterops timer_filtops =
151 { 0, filt_timerattach, filt_timerdetach, filt_timer };
152
153 static uma_zone_t knote_zone;
154 static int kq_ncallouts = 0;
155 static int kq_calloutmax = (4 * 1024);
156 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
157 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
158
159 /* XXX - ensure not KN_INFLUX?? */
160 #define KNOTE_ACTIVATE(kn, islock) do { \
161 if ((islock)) \
162 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
163 else \
164 KQ_LOCK((kn)->kn_kq); \
165 (kn)->kn_status |= KN_ACTIVE; \
166 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
167 knote_enqueue((kn)); \
168 if (!(islock)) \
169 KQ_UNLOCK((kn)->kn_kq); \
170 } while(0)
171 #define KQ_LOCK(kq) do { \
172 mtx_lock(&(kq)->kq_lock); \
173 } while (0)
174 #define KQ_FLUX_WAKEUP(kq) do { \
175 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
176 (kq)->kq_state &= ~KQ_FLUXWAIT; \
177 wakeup((kq)); \
178 } \
179 } while (0)
180 #define KQ_UNLOCK_FLUX(kq) do { \
181 KQ_FLUX_WAKEUP(kq); \
182 mtx_unlock(&(kq)->kq_lock); \
183 } while (0)
184 #define KQ_UNLOCK(kq) do { \
185 mtx_unlock(&(kq)->kq_lock); \
186 } while (0)
187 #define KQ_OWNED(kq) do { \
188 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
189 } while (0)
190 #define KQ_NOTOWNED(kq) do { \
191 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
192 } while (0)
193 #define KN_LIST_LOCK(kn) do { \
194 if (kn->kn_knlist != NULL) \
195 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
196 } while (0)
197 #define KN_LIST_UNLOCK(kn) do { \
198 if (kn->kn_knlist != NULL) \
199 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
200 } while (0)
201 #define KNL_ASSERT_LOCK(knl, islocked) do { \
202 if (islocked) \
203 KNL_ASSERT_LOCKED(knl); \
204 else \
205 KNL_ASSERT_UNLOCKED(knl); \
206 } while (0)
207 #ifdef INVARIANTS
208 #define KNL_ASSERT_LOCKED(knl) do { \
209 if (!knl->kl_locked((knl)->kl_lockarg)) \
210 panic("knlist not locked, but should be"); \
211 } while (0)
212 #define KNL_ASSERT_UNLOCKED(knl) do { \
213 if (knl->kl_locked((knl)->kl_lockarg)) \
214 panic("knlist locked, but should not be"); \
215 } while (0)
216 #else /* !INVARIANTS */
217 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
218 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
219 #endif /* INVARIANTS */
220
221 #define KN_HASHSIZE 64 /* XXX should be tunable */
222 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
223
224 static int
225 filt_nullattach(struct knote *kn)
226 {
227
228 return (ENXIO);
229 };
230
231 struct filterops null_filtops =
232 { 0, filt_nullattach, NULL, NULL };
233
234 /* XXX - make SYSINIT to add these, and move into respective modules. */
235 extern struct filterops sig_filtops;
236 extern struct filterops fs_filtops;
237
238 /*
239 * Table for for all system-defined filters.
240 */
241 static struct mtx filterops_lock;
242 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
243 MTX_DEF);
244 static struct {
245 struct filterops *for_fop;
246 int for_refcnt;
247 } sysfilt_ops[EVFILT_SYSCOUNT] = {
248 { &file_filtops }, /* EVFILT_READ */
249 { &file_filtops }, /* EVFILT_WRITE */
250 { &null_filtops }, /* EVFILT_AIO */
251 { &file_filtops }, /* EVFILT_VNODE */
252 { &proc_filtops }, /* EVFILT_PROC */
253 { &sig_filtops }, /* EVFILT_SIGNAL */
254 { &timer_filtops }, /* EVFILT_TIMER */
255 { &file_filtops }, /* EVFILT_NETDEV */
256 { &fs_filtops }, /* EVFILT_FS */
257 { &null_filtops }, /* EVFILT_LIO */
258 };
259
260 /*
261 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
262 * method.
263 */
264 static int
265 filt_fileattach(struct knote *kn)
266 {
267
268 return (fo_kqfilter(kn->kn_fp, kn));
269 }
270
271 /*ARGSUSED*/
272 static int
273 kqueue_kqfilter(struct file *fp, struct knote *kn)
274 {
275 struct kqueue *kq = kn->kn_fp->f_data;
276
277 if (kn->kn_filter != EVFILT_READ)
278 return (EINVAL);
279
280 kn->kn_status |= KN_KQUEUE;
281 kn->kn_fop = &kqread_filtops;
282 knlist_add(&kq->kq_sel.si_note, kn, 0);
283
284 return (0);
285 }
286
287 static void
288 filt_kqdetach(struct knote *kn)
289 {
290 struct kqueue *kq = kn->kn_fp->f_data;
291
292 knlist_remove(&kq->kq_sel.si_note, kn, 0);
293 }
294
295 /*ARGSUSED*/
296 static int
297 filt_kqueue(struct knote *kn, long hint)
298 {
299 struct kqueue *kq = kn->kn_fp->f_data;
300
301 kn->kn_data = kq->kq_count;
302 return (kn->kn_data > 0);
303 }
304
305 /* XXX - move to kern_proc.c? */
306 static int
307 filt_procattach(struct knote *kn)
308 {
309 struct proc *p;
310 int immediate;
311 int error;
312
313 immediate = 0;
314 p = pfind(kn->kn_id);
315 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
316 p = zpfind(kn->kn_id);
317 immediate = 1;
318 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
319 immediate = 1;
320 }
321
322 if (p == NULL)
323 return (ESRCH);
324 if ((error = p_cansee(curthread, p)))
325 return (error);
326
327 kn->kn_ptr.p_proc = p;
328 kn->kn_flags |= EV_CLEAR; /* automatically set */
329
330 /*
331 * internal flag indicating registration done by kernel
332 */
333 if (kn->kn_flags & EV_FLAG1) {
334 kn->kn_data = kn->kn_sdata; /* ppid */
335 kn->kn_fflags = NOTE_CHILD;
336 kn->kn_flags &= ~EV_FLAG1;
337 }
338
339 if (immediate == 0)
340 knlist_add(&p->p_klist, kn, 1);
341
342 /*
343 * Immediately activate any exit notes if the target process is a
344 * zombie. This is necessary to handle the case where the target
345 * process, e.g. a child, dies before the kevent is registered.
346 */
347 if (immediate && filt_proc(kn, NOTE_EXIT))
348 KNOTE_ACTIVATE(kn, 0);
349
350 PROC_UNLOCK(p);
351
352 return (0);
353 }
354
355 /*
356 * The knote may be attached to a different process, which may exit,
357 * leaving nothing for the knote to be attached to. So when the process
358 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
359 * it will be deleted when read out. However, as part of the knote deletion,
360 * this routine is called, so a check is needed to avoid actually performing
361 * a detach, because the original process does not exist any more.
362 */
363 /* XXX - move to kern_proc.c? */
364 static void
365 filt_procdetach(struct knote *kn)
366 {
367 struct proc *p;
368
369 p = kn->kn_ptr.p_proc;
370 knlist_remove(&p->p_klist, kn, 0);
371 kn->kn_ptr.p_proc = NULL;
372 }
373
374 /* XXX - move to kern_proc.c? */
375 static int
376 filt_proc(struct knote *kn, long hint)
377 {
378 struct proc *p = kn->kn_ptr.p_proc;
379 u_int event;
380
381 /*
382 * mask off extra data
383 */
384 event = (u_int)hint & NOTE_PCTRLMASK;
385
386 /*
387 * if the user is interested in this event, record it.
388 */
389 if (kn->kn_sfflags & event)
390 kn->kn_fflags |= event;
391
392 /*
393 * process is gone, so flag the event as finished.
394 */
395 if (event == NOTE_EXIT) {
396 if (!(kn->kn_status & KN_DETACHED))
397 knlist_remove_inevent(&p->p_klist, kn);
398 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
399 kn->kn_data = p->p_xstat;
400 kn->kn_ptr.p_proc = NULL;
401 return (1);
402 }
403
404 return (kn->kn_fflags != 0);
405 }
406
407 /*
408 * Called when the process forked. It mostly does the same as the
409 * knote(), activating all knotes registered to be activated when the
410 * process forked. Additionally, for each knote attached to the
411 * parent, check whether user wants to track the new process. If so
412 * attach a new knote to it, and immediately report an event with the
413 * child's pid.
414 */
415 void
416 knote_fork(struct knlist *list, int pid)
417 {
418 struct kqueue *kq;
419 struct knote *kn;
420 struct kevent kev;
421 int error;
422
423 if (list == NULL)
424 return;
425 list->kl_lock(list->kl_lockarg);
426
427 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
428 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
429 continue;
430 kq = kn->kn_kq;
431 KQ_LOCK(kq);
432 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
433 KQ_UNLOCK(kq);
434 continue;
435 }
436
437 /*
438 * The same as knote(), activate the event.
439 */
440 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
441 kn->kn_status |= KN_HASKQLOCK;
442 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
443 KNOTE_ACTIVATE(kn, 1);
444 kn->kn_status &= ~KN_HASKQLOCK;
445 KQ_UNLOCK(kq);
446 continue;
447 }
448
449 /*
450 * The NOTE_TRACK case. In addition to the activation
451 * of the event, we need to register new event to
452 * track the child. Drop the locks in preparation for
453 * the call to kqueue_register().
454 */
455 kn->kn_status |= KN_INFLUX;
456 KQ_UNLOCK(kq);
457 list->kl_unlock(list->kl_lockarg);
458
459 /*
460 * Activate existing knote and register a knote with
461 * new process.
462 */
463 kev.ident = pid;
464 kev.filter = kn->kn_filter;
465 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
466 kev.fflags = kn->kn_sfflags;
467 kev.data = kn->kn_id; /* parent */
468 kev.udata = kn->kn_kevent.udata;/* preserve udata */
469 error = kqueue_register(kq, &kev, NULL, 0);
470 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
471 KNOTE_ACTIVATE(kn, 0);
472 if (error)
473 kn->kn_fflags |= NOTE_TRACKERR;
474 KQ_LOCK(kq);
475 kn->kn_status &= ~KN_INFLUX;
476 KQ_UNLOCK_FLUX(kq);
477 list->kl_lock(list->kl_lockarg);
478 }
479 list->kl_unlock(list->kl_lockarg);
480 }
481
482 static int
483 timertoticks(intptr_t data)
484 {
485 struct timeval tv;
486 int tticks;
487
488 tv.tv_sec = data / 1000;
489 tv.tv_usec = (data % 1000) * 1000;
490 tticks = tvtohz(&tv);
491
492 return tticks;
493 }
494
495 /* XXX - move to kern_timeout.c? */
496 static void
497 filt_timerexpire(void *knx)
498 {
499 struct knote *kn = knx;
500 struct callout *calloutp;
501
502 kn->kn_data++;
503 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
504
505 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
506 calloutp = (struct callout *)kn->kn_hook;
507 callout_reset(calloutp, timertoticks(kn->kn_sdata),
508 filt_timerexpire, kn);
509 }
510 }
511
512 /*
513 * data contains amount of time to sleep, in milliseconds
514 */
515 /* XXX - move to kern_timeout.c? */
516 static int
517 filt_timerattach(struct knote *kn)
518 {
519 struct callout *calloutp;
520
521 atomic_add_int(&kq_ncallouts, 1);
522
523 if (kq_ncallouts >= kq_calloutmax) {
524 atomic_add_int(&kq_ncallouts, -1);
525 return (ENOMEM);
526 }
527
528 kn->kn_flags |= EV_CLEAR; /* automatically set */
529 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
530 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
531 M_KQUEUE, M_WAITOK);
532 callout_init(calloutp, CALLOUT_MPSAFE);
533 kn->kn_hook = calloutp;
534 callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire,
535 kn);
536
537 return (0);
538 }
539
540 /* XXX - move to kern_timeout.c? */
541 static void
542 filt_timerdetach(struct knote *kn)
543 {
544 struct callout *calloutp;
545
546 calloutp = (struct callout *)kn->kn_hook;
547 callout_drain(calloutp);
548 FREE(calloutp, M_KQUEUE);
549 atomic_add_int(&kq_ncallouts, -1);
550 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
551 }
552
553 /* XXX - move to kern_timeout.c? */
554 static int
555 filt_timer(struct knote *kn, long hint)
556 {
557
558 return (kn->kn_data != 0);
559 }
560
561 int
562 kqueue(struct thread *td, struct kqueue_args *uap)
563 {
564 struct filedesc *fdp;
565 struct kqueue *kq;
566 struct file *fp;
567 int fd, error;
568
569 fdp = td->td_proc->p_fd;
570 error = falloc(td, &fp, &fd);
571 if (error)
572 goto done2;
573
574 /* An extra reference on `nfp' has been held for us by falloc(). */
575 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
576 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
577 TAILQ_INIT(&kq->kq_head);
578 kq->kq_fdp = fdp;
579 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL);
580 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
581
582 FILEDESC_XLOCK(fdp);
583 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
584 FILEDESC_XUNLOCK(fdp);
585
586 FILE_LOCK(fp);
587 fp->f_flag = FREAD | FWRITE;
588 fp->f_type = DTYPE_KQUEUE;
589 fp->f_data = kq;
590 fp->f_ops = &kqueueops;
591 FILE_UNLOCK(fp);
592 fdrop(fp, td);
593
594 td->td_retval[0] = fd;
595 done2:
596 return (error);
597 }
598
599 #ifndef _SYS_SYSPROTO_H_
600 struct kevent_args {
601 int fd;
602 const struct kevent *changelist;
603 int nchanges;
604 struct kevent *eventlist;
605 int nevents;
606 const struct timespec *timeout;
607 };
608 #endif
609 int
610 kevent(struct thread *td, struct kevent_args *uap)
611 {
612 struct timespec ts, *tsp;
613 struct kevent_copyops k_ops = { uap,
614 kevent_copyout,
615 kevent_copyin};
616 int error;
617 #ifdef KTRACE
618 struct uio ktruio;
619 struct iovec ktriov;
620 struct uio *ktruioin = NULL;
621 struct uio *ktruioout = NULL;
622 #endif
623
624 if (uap->timeout != NULL) {
625 error = copyin(uap->timeout, &ts, sizeof(ts));
626 if (error)
627 return (error);
628 tsp = &ts;
629 } else
630 tsp = NULL;
631
632 #ifdef KTRACE
633 if (KTRPOINT(td, KTR_GENIO)) {
634 ktriov.iov_base = uap->changelist;
635 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
636 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
637 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
638 .uio_td = td };
639 ktruioin = cloneuio(&ktruio);
640 ktriov.iov_base = uap->eventlist;
641 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
642 ktruioout = cloneuio(&ktruio);
643 }
644 #endif
645
646 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
647 &k_ops, tsp);
648
649 #ifdef KTRACE
650 if (ktruioin != NULL) {
651 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
652 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
653 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
654 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
655 }
656 #endif
657
658 return (error);
659 }
660
661 /*
662 * Copy 'count' items into the destination list pointed to by uap->eventlist.
663 */
664 static int
665 kevent_copyout(void *arg, struct kevent *kevp, int count)
666 {
667 struct kevent_args *uap;
668 int error;
669
670 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
671 uap = (struct kevent_args *)arg;
672
673 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
674 if (error == 0)
675 uap->eventlist += count;
676 return (error);
677 }
678
679 /*
680 * Copy 'count' items from the list pointed to by uap->changelist.
681 */
682 static int
683 kevent_copyin(void *arg, struct kevent *kevp, int count)
684 {
685 struct kevent_args *uap;
686 int error;
687
688 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
689 uap = (struct kevent_args *)arg;
690
691 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
692 if (error == 0)
693 uap->changelist += count;
694 return (error);
695 }
696
697 int
698 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
699 struct kevent_copyops *k_ops, const struct timespec *timeout)
700 {
701 struct kevent keva[KQ_NEVENTS];
702 struct kevent *kevp, *changes;
703 struct kqueue *kq;
704 struct file *fp;
705 int i, n, nerrors, error;
706
707 if ((error = fget(td, fd, &fp)) != 0)
708 return (error);
709 if ((error = kqueue_acquire(fp, &kq)) != 0)
710 goto done_norel;
711
712 nerrors = 0;
713
714 while (nchanges > 0) {
715 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
716 error = k_ops->k_copyin(k_ops->arg, keva, n);
717 if (error)
718 goto done;
719 changes = keva;
720 for (i = 0; i < n; i++) {
721 kevp = &changes[i];
722 if (!kevp->filter)
723 continue;
724 kevp->flags &= ~EV_SYSFLAGS;
725 error = kqueue_register(kq, kevp, td, 1);
726 if (error) {
727 if (nevents != 0) {
728 kevp->flags = EV_ERROR;
729 kevp->data = error;
730 (void) k_ops->k_copyout(k_ops->arg,
731 kevp, 1);
732 nevents--;
733 nerrors++;
734 } else {
735 goto done;
736 }
737 }
738 }
739 nchanges -= n;
740 }
741 if (nerrors) {
742 td->td_retval[0] = nerrors;
743 error = 0;
744 goto done;
745 }
746
747 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
748 done:
749 kqueue_release(kq, 0);
750 done_norel:
751 fdrop(fp, td);
752 return (error);
753 }
754
755 int
756 kqueue_add_filteropts(int filt, struct filterops *filtops)
757 {
758 int error;
759
760 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
761 printf(
762 "trying to add a filterop that is out of range: %d is beyond %d\n",
763 ~filt, EVFILT_SYSCOUNT);
764 return EINVAL;
765 }
766 mtx_lock(&filterops_lock);
767 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
768 sysfilt_ops[~filt].for_fop != NULL)
769 error = EEXIST;
770 else {
771 sysfilt_ops[~filt].for_fop = filtops;
772 sysfilt_ops[~filt].for_refcnt = 0;
773 }
774 mtx_unlock(&filterops_lock);
775
776 return (0);
777 }
778
779 int
780 kqueue_del_filteropts(int filt)
781 {
782 int error;
783
784 error = 0;
785 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
786 return EINVAL;
787
788 mtx_lock(&filterops_lock);
789 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
790 sysfilt_ops[~filt].for_fop == NULL)
791 error = EINVAL;
792 else if (sysfilt_ops[~filt].for_refcnt != 0)
793 error = EBUSY;
794 else {
795 sysfilt_ops[~filt].for_fop = &null_filtops;
796 sysfilt_ops[~filt].for_refcnt = 0;
797 }
798 mtx_unlock(&filterops_lock);
799
800 return error;
801 }
802
803 static struct filterops *
804 kqueue_fo_find(int filt)
805 {
806
807 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
808 return NULL;
809
810 mtx_lock(&filterops_lock);
811 sysfilt_ops[~filt].for_refcnt++;
812 if (sysfilt_ops[~filt].for_fop == NULL)
813 sysfilt_ops[~filt].for_fop = &null_filtops;
814 mtx_unlock(&filterops_lock);
815
816 return sysfilt_ops[~filt].for_fop;
817 }
818
819 static void
820 kqueue_fo_release(int filt)
821 {
822
823 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
824 return;
825
826 mtx_lock(&filterops_lock);
827 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
828 ("filter object refcount not valid on release"));
829 sysfilt_ops[~filt].for_refcnt--;
830 mtx_unlock(&filterops_lock);
831 }
832
833 /*
834 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
835 * influence if memory allocation should wait. Make sure it is 0 if you
836 * hold any mutexes.
837 */
838 static int
839 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
840 {
841 struct filterops *fops;
842 struct file *fp;
843 struct knote *kn, *tkn;
844 int error, filt, event;
845 int haskqglobal;
846
847 fp = NULL;
848 kn = NULL;
849 error = 0;
850 haskqglobal = 0;
851
852 filt = kev->filter;
853 fops = kqueue_fo_find(filt);
854 if (fops == NULL)
855 return EINVAL;
856
857 tkn = knote_alloc(waitok); /* prevent waiting with locks */
858
859 findkn:
860 if (fops->f_isfd) {
861 KASSERT(td != NULL, ("td is NULL"));
862 error = fget(td, kev->ident, &fp);
863 if (error)
864 goto done;
865
866 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
867 kev->ident, 0) != 0) {
868 /* try again */
869 fdrop(fp, td);
870 fp = NULL;
871 error = kqueue_expand(kq, fops, kev->ident, waitok);
872 if (error)
873 goto done;
874 goto findkn;
875 }
876
877 if (fp->f_type == DTYPE_KQUEUE) {
878 /*
879 * if we add some inteligence about what we are doing,
880 * we should be able to support events on ourselves.
881 * We need to know when we are doing this to prevent
882 * getting both the knlist lock and the kq lock since
883 * they are the same thing.
884 */
885 if (fp->f_data == kq) {
886 error = EINVAL;
887 goto done;
888 }
889
890 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
891 }
892
893 KQ_LOCK(kq);
894 if (kev->ident < kq->kq_knlistsize) {
895 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
896 if (kev->filter == kn->kn_filter)
897 break;
898 }
899 } else {
900 if ((kev->flags & EV_ADD) == EV_ADD)
901 kqueue_expand(kq, fops, kev->ident, waitok);
902
903 KQ_LOCK(kq);
904 if (kq->kq_knhashmask != 0) {
905 struct klist *list;
906
907 list = &kq->kq_knhash[
908 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
909 SLIST_FOREACH(kn, list, kn_link)
910 if (kev->ident == kn->kn_id &&
911 kev->filter == kn->kn_filter)
912 break;
913 }
914 }
915
916 /* knote is in the process of changing, wait for it to stablize. */
917 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
918 if (fp != NULL) {
919 fdrop(fp, td);
920 fp = NULL;
921 }
922 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
923 kq->kq_state |= KQ_FLUXWAIT;
924 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
925 goto findkn;
926 }
927
928 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
929 KQ_UNLOCK(kq);
930 error = ENOENT;
931 goto done;
932 }
933
934 /*
935 * kn now contains the matching knote, or NULL if no match
936 */
937 if (kev->flags & EV_ADD) {
938 if (kn == NULL) {
939 kn = tkn;
940 tkn = NULL;
941 if (kn == NULL) {
942 KQ_UNLOCK(kq);
943 error = ENOMEM;
944 goto done;
945 }
946 kn->kn_fp = fp;
947 kn->kn_kq = kq;
948 kn->kn_fop = fops;
949 /*
950 * apply reference counts to knote structure, and
951 * do not release it at the end of this routine.
952 */
953 fops = NULL;
954 fp = NULL;
955
956 kn->kn_sfflags = kev->fflags;
957 kn->kn_sdata = kev->data;
958 kev->fflags = 0;
959 kev->data = 0;
960 kn->kn_kevent = *kev;
961 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
962 EV_ENABLE | EV_DISABLE);
963 kn->kn_status = KN_INFLUX|KN_DETACHED;
964
965 error = knote_attach(kn, kq);
966 KQ_UNLOCK(kq);
967 if (error != 0) {
968 tkn = kn;
969 goto done;
970 }
971
972 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
973 knote_drop(kn, td);
974 goto done;
975 }
976 KN_LIST_LOCK(kn);
977 } else {
978 /*
979 * The user may change some filter values after the
980 * initial EV_ADD, but doing so will not reset any
981 * filter which has already been triggered.
982 */
983 kn->kn_status |= KN_INFLUX;
984 KQ_UNLOCK(kq);
985 KN_LIST_LOCK(kn);
986 kn->kn_sfflags = kev->fflags;
987 kn->kn_sdata = kev->data;
988 kn->kn_kevent.udata = kev->udata;
989 }
990
991 /*
992 * We can get here with kn->kn_knlist == NULL.
993 * This can happen when the initial attach event decides that
994 * the event is "completed" already. i.e. filt_procattach
995 * is called on a zombie process. It will call filt_proc
996 * which will remove it from the list, and NULL kn_knlist.
997 */
998 event = kn->kn_fop->f_event(kn, 0);
999 KQ_LOCK(kq);
1000 if (event)
1001 KNOTE_ACTIVATE(kn, 1);
1002 kn->kn_status &= ~KN_INFLUX;
1003 KN_LIST_UNLOCK(kn);
1004 } else if (kev->flags & EV_DELETE) {
1005 kn->kn_status |= KN_INFLUX;
1006 KQ_UNLOCK(kq);
1007 if (!(kn->kn_status & KN_DETACHED))
1008 kn->kn_fop->f_detach(kn);
1009 knote_drop(kn, td);
1010 goto done;
1011 }
1012
1013 if ((kev->flags & EV_DISABLE) &&
1014 ((kn->kn_status & KN_DISABLED) == 0)) {
1015 kn->kn_status |= KN_DISABLED;
1016 }
1017
1018 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1019 kn->kn_status &= ~KN_DISABLED;
1020 if ((kn->kn_status & KN_ACTIVE) &&
1021 ((kn->kn_status & KN_QUEUED) == 0))
1022 knote_enqueue(kn);
1023 }
1024 KQ_UNLOCK_FLUX(kq);
1025
1026 done:
1027 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1028 if (fp != NULL)
1029 fdrop(fp, td);
1030 if (tkn != NULL)
1031 knote_free(tkn);
1032 if (fops != NULL)
1033 kqueue_fo_release(filt);
1034 return (error);
1035 }
1036
1037 static int
1038 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1039 {
1040 int error;
1041 struct kqueue *kq;
1042
1043 error = 0;
1044
1045 FILE_LOCK(fp);
1046 do {
1047 kq = fp->f_data;
1048 if (fp->f_type != DTYPE_KQUEUE || kq == NULL) {
1049 error = EBADF;
1050 break;
1051 }
1052 *kqp = kq;
1053 KQ_LOCK(kq);
1054 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1055 KQ_UNLOCK(kq);
1056 error = EBADF;
1057 break;
1058 }
1059 kq->kq_refcnt++;
1060 KQ_UNLOCK(kq);
1061 } while (0);
1062 FILE_UNLOCK(fp);
1063
1064 return error;
1065 }
1066
1067 static void
1068 kqueue_release(struct kqueue *kq, int locked)
1069 {
1070 if (locked)
1071 KQ_OWNED(kq);
1072 else
1073 KQ_LOCK(kq);
1074 kq->kq_refcnt--;
1075 if (kq->kq_refcnt == 1)
1076 wakeup(&kq->kq_refcnt);
1077 if (!locked)
1078 KQ_UNLOCK(kq);
1079 }
1080
1081 static void
1082 kqueue_schedtask(struct kqueue *kq)
1083 {
1084
1085 KQ_OWNED(kq);
1086 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1087 ("scheduling kqueue task while draining"));
1088
1089 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1090 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1091 kq->kq_state |= KQ_TASKSCHED;
1092 }
1093 }
1094
1095 /*
1096 * Expand the kq to make sure we have storage for fops/ident pair.
1097 *
1098 * Return 0 on success (or no work necessary), return errno on failure.
1099 *
1100 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1101 * If kqueue_register is called from a non-fd context, there usually/should
1102 * be no locks held.
1103 */
1104 static int
1105 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1106 int waitok)
1107 {
1108 struct klist *list, *tmp_knhash;
1109 u_long tmp_knhashmask;
1110 int size;
1111 int fd;
1112 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1113
1114 KQ_NOTOWNED(kq);
1115
1116 if (fops->f_isfd) {
1117 fd = ident;
1118 if (kq->kq_knlistsize <= fd) {
1119 size = kq->kq_knlistsize;
1120 while (size <= fd)
1121 size += KQEXTENT;
1122 MALLOC(list, struct klist *,
1123 size * sizeof list, M_KQUEUE, mflag);
1124 if (list == NULL)
1125 return ENOMEM;
1126 KQ_LOCK(kq);
1127 if (kq->kq_knlistsize > fd) {
1128 FREE(list, M_KQUEUE);
1129 list = NULL;
1130 } else {
1131 if (kq->kq_knlist != NULL) {
1132 bcopy(kq->kq_knlist, list,
1133 kq->kq_knlistsize * sizeof list);
1134 FREE(kq->kq_knlist, M_KQUEUE);
1135 kq->kq_knlist = NULL;
1136 }
1137 bzero((caddr_t)list +
1138 kq->kq_knlistsize * sizeof list,
1139 (size - kq->kq_knlistsize) * sizeof list);
1140 kq->kq_knlistsize = size;
1141 kq->kq_knlist = list;
1142 }
1143 KQ_UNLOCK(kq);
1144 }
1145 } else {
1146 if (kq->kq_knhashmask == 0) {
1147 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1148 &tmp_knhashmask);
1149 if (tmp_knhash == NULL)
1150 return ENOMEM;
1151 KQ_LOCK(kq);
1152 if (kq->kq_knhashmask == 0) {
1153 kq->kq_knhash = tmp_knhash;
1154 kq->kq_knhashmask = tmp_knhashmask;
1155 } else {
1156 free(tmp_knhash, M_KQUEUE);
1157 }
1158 KQ_UNLOCK(kq);
1159 }
1160 }
1161
1162 KQ_NOTOWNED(kq);
1163 return 0;
1164 }
1165
1166 static void
1167 kqueue_task(void *arg, int pending)
1168 {
1169 struct kqueue *kq;
1170 int haskqglobal;
1171
1172 haskqglobal = 0;
1173 kq = arg;
1174
1175 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1176 KQ_LOCK(kq);
1177
1178 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1179
1180 kq->kq_state &= ~KQ_TASKSCHED;
1181 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1182 wakeup(&kq->kq_state);
1183 }
1184 KQ_UNLOCK(kq);
1185 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1186 }
1187
1188 /*
1189 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1190 * We treat KN_MARKER knotes as if they are INFLUX.
1191 */
1192 static int
1193 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1194 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1195 {
1196 struct kevent *kevp;
1197 struct timeval atv, rtv, ttv;
1198 struct knote *kn, *marker;
1199 int count, timeout, nkev, error, influx;
1200 int haskqglobal;
1201
1202 count = maxevents;
1203 nkev = 0;
1204 error = 0;
1205 haskqglobal = 0;
1206
1207 if (maxevents == 0)
1208 goto done_nl;
1209
1210 if (tsp != NULL) {
1211 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1212 if (itimerfix(&atv)) {
1213 error = EINVAL;
1214 goto done_nl;
1215 }
1216 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1217 timeout = -1;
1218 else
1219 timeout = atv.tv_sec > 24 * 60 * 60 ?
1220 24 * 60 * 60 * hz : tvtohz(&atv);
1221 getmicrouptime(&rtv);
1222 timevaladd(&atv, &rtv);
1223 } else {
1224 atv.tv_sec = 0;
1225 atv.tv_usec = 0;
1226 timeout = 0;
1227 }
1228 marker = knote_alloc(1);
1229 if (marker == NULL) {
1230 error = ENOMEM;
1231 goto done_nl;
1232 }
1233 marker->kn_status = KN_MARKER;
1234 KQ_LOCK(kq);
1235 goto start;
1236
1237 retry:
1238 if (atv.tv_sec || atv.tv_usec) {
1239 getmicrouptime(&rtv);
1240 if (timevalcmp(&rtv, &atv, >=))
1241 goto done;
1242 ttv = atv;
1243 timevalsub(&ttv, &rtv);
1244 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1245 24 * 60 * 60 * hz : tvtohz(&ttv);
1246 }
1247
1248 start:
1249 kevp = keva;
1250 if (kq->kq_count == 0) {
1251 if (timeout < 0) {
1252 error = EWOULDBLOCK;
1253 } else {
1254 kq->kq_state |= KQ_SLEEP;
1255 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1256 "kqread", timeout);
1257 }
1258 if (error == 0)
1259 goto retry;
1260 /* don't restart after signals... */
1261 if (error == ERESTART)
1262 error = EINTR;
1263 else if (error == EWOULDBLOCK)
1264 error = 0;
1265 goto done;
1266 }
1267
1268 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1269 influx = 0;
1270 while (count) {
1271 KQ_OWNED(kq);
1272 kn = TAILQ_FIRST(&kq->kq_head);
1273
1274 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1275 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1276 if (influx) {
1277 influx = 0;
1278 KQ_FLUX_WAKEUP(kq);
1279 }
1280 kq->kq_state |= KQ_FLUXWAIT;
1281 error = msleep(kq, &kq->kq_lock, PSOCK,
1282 "kqflxwt", 0);
1283 continue;
1284 }
1285
1286 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1287 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1288 kn->kn_status &= ~KN_QUEUED;
1289 kq->kq_count--;
1290 continue;
1291 }
1292 if (kn == marker) {
1293 KQ_FLUX_WAKEUP(kq);
1294 if (count == maxevents)
1295 goto retry;
1296 goto done;
1297 }
1298 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1299 ("KN_INFLUX set when not suppose to be"));
1300
1301 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1302 kn->kn_status &= ~KN_QUEUED;
1303 kn->kn_status |= KN_INFLUX;
1304 kq->kq_count--;
1305 KQ_UNLOCK(kq);
1306 /*
1307 * We don't need to lock the list since we've marked
1308 * it _INFLUX.
1309 */
1310 *kevp = kn->kn_kevent;
1311 if (!(kn->kn_status & KN_DETACHED))
1312 kn->kn_fop->f_detach(kn);
1313 knote_drop(kn, td);
1314 KQ_LOCK(kq);
1315 kn = NULL;
1316 } else {
1317 kn->kn_status |= KN_INFLUX;
1318 KQ_UNLOCK(kq);
1319 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1320 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1321 KN_LIST_LOCK(kn);
1322 if (kn->kn_fop->f_event(kn, 0) == 0) {
1323 KQ_LOCK(kq);
1324 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1325 kn->kn_status &=
1326 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1327 kq->kq_count--;
1328 KN_LIST_UNLOCK(kn);
1329 influx = 1;
1330 continue;
1331 }
1332 *kevp = kn->kn_kevent;
1333 KQ_LOCK(kq);
1334 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1335 if (kn->kn_flags & EV_CLEAR) {
1336 kn->kn_data = 0;
1337 kn->kn_fflags = 0;
1338 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1339 kq->kq_count--;
1340 } else
1341 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1342
1343 kn->kn_status &= ~(KN_INFLUX);
1344 KN_LIST_UNLOCK(kn);
1345 influx = 1;
1346 }
1347
1348 /* we are returning a copy to the user */
1349 kevp++;
1350 nkev++;
1351 count--;
1352
1353 if (nkev == KQ_NEVENTS) {
1354 influx = 0;
1355 KQ_UNLOCK_FLUX(kq);
1356 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1357 nkev = 0;
1358 kevp = keva;
1359 KQ_LOCK(kq);
1360 if (error)
1361 break;
1362 }
1363 }
1364 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1365 done:
1366 KQ_OWNED(kq);
1367 KQ_UNLOCK_FLUX(kq);
1368 knote_free(marker);
1369 done_nl:
1370 KQ_NOTOWNED(kq);
1371 if (nkev != 0)
1372 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1373 td->td_retval[0] = maxevents - count;
1374 return (error);
1375 }
1376
1377 /*
1378 * XXX
1379 * This could be expanded to call kqueue_scan, if desired.
1380 */
1381 /*ARGSUSED*/
1382 static int
1383 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1384 int flags, struct thread *td)
1385 {
1386 return (ENXIO);
1387 }
1388
1389 /*ARGSUSED*/
1390 static int
1391 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1392 int flags, struct thread *td)
1393 {
1394 return (ENXIO);
1395 }
1396
1397 /*ARGSUSED*/
1398 static int
1399 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1400 struct ucred *active_cred, struct thread *td)
1401 {
1402 /*
1403 * Enabling sigio causes two major problems:
1404 * 1) infinite recursion:
1405 * Synopsys: kevent is being used to track signals and have FIOASYNC
1406 * set. On receipt of a signal this will cause a kqueue to recurse
1407 * into itself over and over. Sending the sigio causes the kqueue
1408 * to become ready, which in turn posts sigio again, forever.
1409 * Solution: this can be solved by setting a flag in the kqueue that
1410 * we have a SIGIO in progress.
1411 * 2) locking problems:
1412 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1413 * us above the proc and pgrp locks.
1414 * Solution: Post a signal using an async mechanism, being sure to
1415 * record a generation count in the delivery so that we do not deliver
1416 * a signal to the wrong process.
1417 *
1418 * Note, these two mechanisms are somewhat mutually exclusive!
1419 */
1420 #if 0
1421 struct kqueue *kq;
1422
1423 kq = fp->f_data;
1424 switch (cmd) {
1425 case FIOASYNC:
1426 if (*(int *)data) {
1427 kq->kq_state |= KQ_ASYNC;
1428 } else {
1429 kq->kq_state &= ~KQ_ASYNC;
1430 }
1431 return (0);
1432
1433 case FIOSETOWN:
1434 return (fsetown(*(int *)data, &kq->kq_sigio));
1435
1436 case FIOGETOWN:
1437 *(int *)data = fgetown(&kq->kq_sigio);
1438 return (0);
1439 }
1440 #endif
1441
1442 return (ENOTTY);
1443 }
1444
1445 /*ARGSUSED*/
1446 static int
1447 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1448 struct thread *td)
1449 {
1450 struct kqueue *kq;
1451 int revents = 0;
1452 int error;
1453
1454 if ((error = kqueue_acquire(fp, &kq)))
1455 return POLLERR;
1456
1457 KQ_LOCK(kq);
1458 if (events & (POLLIN | POLLRDNORM)) {
1459 if (kq->kq_count) {
1460 revents |= events & (POLLIN | POLLRDNORM);
1461 } else {
1462 selrecord(td, &kq->kq_sel);
1463 kq->kq_state |= KQ_SEL;
1464 }
1465 }
1466 kqueue_release(kq, 1);
1467 KQ_UNLOCK(kq);
1468 return (revents);
1469 }
1470
1471 /*ARGSUSED*/
1472 static int
1473 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1474 struct thread *td)
1475 {
1476
1477 bzero((void *)st, sizeof *st);
1478 /*
1479 * We no longer return kq_count because the unlocked value is useless.
1480 * If you spent all this time getting the count, why not spend your
1481 * syscall better by calling kevent?
1482 *
1483 * XXX - This is needed for libc_r.
1484 */
1485 st->st_mode = S_IFIFO;
1486 return (0);
1487 }
1488
1489 /*ARGSUSED*/
1490 static int
1491 kqueue_close(struct file *fp, struct thread *td)
1492 {
1493 struct kqueue *kq = fp->f_data;
1494 struct filedesc *fdp;
1495 struct knote *kn;
1496 int i;
1497 int error;
1498
1499 if ((error = kqueue_acquire(fp, &kq)))
1500 return error;
1501
1502 KQ_LOCK(kq);
1503
1504 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1505 ("kqueue already closing"));
1506 kq->kq_state |= KQ_CLOSING;
1507 if (kq->kq_refcnt > 1)
1508 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1509
1510 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1511 fdp = kq->kq_fdp;
1512
1513 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1514 ("kqueue's knlist not empty"));
1515
1516 for (i = 0; i < kq->kq_knlistsize; i++) {
1517 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1518 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1519 kq->kq_state |= KQ_FLUXWAIT;
1520 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1521 continue;
1522 }
1523 kn->kn_status |= KN_INFLUX;
1524 KQ_UNLOCK(kq);
1525 if (!(kn->kn_status & KN_DETACHED))
1526 kn->kn_fop->f_detach(kn);
1527 knote_drop(kn, td);
1528 KQ_LOCK(kq);
1529 }
1530 }
1531 if (kq->kq_knhashmask != 0) {
1532 for (i = 0; i <= kq->kq_knhashmask; i++) {
1533 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1534 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1535 kq->kq_state |= KQ_FLUXWAIT;
1536 msleep(kq, &kq->kq_lock, PSOCK,
1537 "kqclo2", 0);
1538 continue;
1539 }
1540 kn->kn_status |= KN_INFLUX;
1541 KQ_UNLOCK(kq);
1542 if (!(kn->kn_status & KN_DETACHED))
1543 kn->kn_fop->f_detach(kn);
1544 knote_drop(kn, td);
1545 KQ_LOCK(kq);
1546 }
1547 }
1548 }
1549
1550 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1551 kq->kq_state |= KQ_TASKDRAIN;
1552 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1553 }
1554
1555 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1556 kq->kq_state &= ~KQ_SEL;
1557 selwakeuppri(&kq->kq_sel, PSOCK);
1558 }
1559
1560 KQ_UNLOCK(kq);
1561
1562 FILEDESC_XLOCK(fdp);
1563 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1564 FILEDESC_XUNLOCK(fdp);
1565
1566 knlist_destroy(&kq->kq_sel.si_note);
1567 mtx_destroy(&kq->kq_lock);
1568 kq->kq_fdp = NULL;
1569
1570 if (kq->kq_knhash != NULL)
1571 free(kq->kq_knhash, M_KQUEUE);
1572 if (kq->kq_knlist != NULL)
1573 free(kq->kq_knlist, M_KQUEUE);
1574
1575 funsetown(&kq->kq_sigio);
1576 free(kq, M_KQUEUE);
1577 fp->f_data = NULL;
1578
1579 return (0);
1580 }
1581
1582 static void
1583 kqueue_wakeup(struct kqueue *kq)
1584 {
1585 KQ_OWNED(kq);
1586
1587 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1588 kq->kq_state &= ~KQ_SLEEP;
1589 wakeup(kq);
1590 }
1591 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1592 kq->kq_state &= ~KQ_SEL;
1593 selwakeuppri(&kq->kq_sel, PSOCK);
1594 }
1595 if (!knlist_empty(&kq->kq_sel.si_note))
1596 kqueue_schedtask(kq);
1597 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1598 pgsigio(&kq->kq_sigio, SIGIO, 0);
1599 }
1600 }
1601
1602 /*
1603 * Walk down a list of knotes, activating them if their event has triggered.
1604 *
1605 * There is a possibility to optimize in the case of one kq watching another.
1606 * Instead of scheduling a task to wake it up, you could pass enough state
1607 * down the chain to make up the parent kqueue. Make this code functional
1608 * first.
1609 */
1610 void
1611 knote(struct knlist *list, long hint, int islocked)
1612 {
1613 struct kqueue *kq;
1614 struct knote *kn;
1615
1616 if (list == NULL)
1617 return;
1618
1619 KNL_ASSERT_LOCK(list, islocked);
1620
1621 if (!islocked)
1622 list->kl_lock(list->kl_lockarg);
1623
1624 /*
1625 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1626 * the kqueue scheduling, but this will introduce four
1627 * lock/unlock's for each knote to test. If we do, continue to use
1628 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1629 * only safe if you want to remove the current item, which we are
1630 * not doing.
1631 */
1632 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1633 kq = kn->kn_kq;
1634 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1635 KQ_LOCK(kq);
1636 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1637 kn->kn_status |= KN_HASKQLOCK;
1638 if (kn->kn_fop->f_event(kn, hint))
1639 KNOTE_ACTIVATE(kn, 1);
1640 kn->kn_status &= ~KN_HASKQLOCK;
1641 }
1642 KQ_UNLOCK(kq);
1643 }
1644 kq = NULL;
1645 }
1646 if (!islocked)
1647 list->kl_unlock(list->kl_lockarg);
1648 }
1649
1650 /*
1651 * add a knote to a knlist
1652 */
1653 void
1654 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1655 {
1656 KNL_ASSERT_LOCK(knl, islocked);
1657 KQ_NOTOWNED(kn->kn_kq);
1658 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1659 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1660 if (!islocked)
1661 knl->kl_lock(knl->kl_lockarg);
1662 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1663 if (!islocked)
1664 knl->kl_unlock(knl->kl_lockarg);
1665 KQ_LOCK(kn->kn_kq);
1666 kn->kn_knlist = knl;
1667 kn->kn_status &= ~KN_DETACHED;
1668 KQ_UNLOCK(kn->kn_kq);
1669 }
1670
1671 static void
1672 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1673 {
1674 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1675 KNL_ASSERT_LOCK(knl, knlislocked);
1676 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1677 if (!kqislocked)
1678 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1679 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1680 if (!knlislocked)
1681 knl->kl_lock(knl->kl_lockarg);
1682 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1683 kn->kn_knlist = NULL;
1684 if (!knlislocked)
1685 knl->kl_unlock(knl->kl_lockarg);
1686 if (!kqislocked)
1687 KQ_LOCK(kn->kn_kq);
1688 kn->kn_status |= KN_DETACHED;
1689 if (!kqislocked)
1690 KQ_UNLOCK(kn->kn_kq);
1691 }
1692
1693 /*
1694 * remove all knotes from a specified klist
1695 */
1696 void
1697 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1698 {
1699
1700 knlist_remove_kq(knl, kn, islocked, 0);
1701 }
1702
1703 /*
1704 * remove knote from a specified klist while in f_event handler.
1705 */
1706 void
1707 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1708 {
1709
1710 knlist_remove_kq(knl, kn, 1,
1711 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1712 }
1713
1714 int
1715 knlist_empty(struct knlist *knl)
1716 {
1717 KNL_ASSERT_LOCKED(knl);
1718 return SLIST_EMPTY(&knl->kl_list);
1719 }
1720
1721 static struct mtx knlist_lock;
1722 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1723 MTX_DEF);
1724 static void knlist_mtx_lock(void *arg);
1725 static void knlist_mtx_unlock(void *arg);
1726 static int knlist_mtx_locked(void *arg);
1727
1728 static void
1729 knlist_mtx_lock(void *arg)
1730 {
1731 mtx_lock((struct mtx *)arg);
1732 }
1733
1734 static void
1735 knlist_mtx_unlock(void *arg)
1736 {
1737 mtx_unlock((struct mtx *)arg);
1738 }
1739
1740 static int
1741 knlist_mtx_locked(void *arg)
1742 {
1743 return (mtx_owned((struct mtx *)arg));
1744 }
1745
1746 void
1747 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1748 void (*kl_unlock)(void *), int (*kl_locked)(void *))
1749 {
1750
1751 if (lock == NULL)
1752 knl->kl_lockarg = &knlist_lock;
1753 else
1754 knl->kl_lockarg = lock;
1755
1756 if (kl_lock == NULL)
1757 knl->kl_lock = knlist_mtx_lock;
1758 else
1759 knl->kl_lock = kl_lock;
1760 if (kl_unlock == NULL)
1761 knl->kl_unlock = knlist_mtx_unlock;
1762 else
1763 knl->kl_unlock = kl_unlock;
1764 if (kl_locked == NULL)
1765 knl->kl_locked = knlist_mtx_locked;
1766 else
1767 knl->kl_locked = kl_locked;
1768
1769 SLIST_INIT(&knl->kl_list);
1770 }
1771
1772 void
1773 knlist_destroy(struct knlist *knl)
1774 {
1775
1776 #ifdef INVARIANTS
1777 /*
1778 * if we run across this error, we need to find the offending
1779 * driver and have it call knlist_clear.
1780 */
1781 if (!SLIST_EMPTY(&knl->kl_list))
1782 printf("WARNING: destroying knlist w/ knotes on it!\n");
1783 #endif
1784
1785 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1786 SLIST_INIT(&knl->kl_list);
1787 }
1788
1789 /*
1790 * Even if we are locked, we may need to drop the lock to allow any influx
1791 * knotes time to "settle".
1792 */
1793 void
1794 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1795 {
1796 struct knote *kn, *kn2;
1797 struct kqueue *kq;
1798
1799 if (islocked)
1800 KNL_ASSERT_LOCKED(knl);
1801 else {
1802 KNL_ASSERT_UNLOCKED(knl);
1803 again: /* need to reacquire lock since we have dropped it */
1804 knl->kl_lock(knl->kl_lockarg);
1805 }
1806
1807 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
1808 kq = kn->kn_kq;
1809 KQ_LOCK(kq);
1810 if ((kn->kn_status & KN_INFLUX)) {
1811 KQ_UNLOCK(kq);
1812 continue;
1813 }
1814 knlist_remove_kq(knl, kn, 1, 1);
1815 if (killkn) {
1816 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1817 KQ_UNLOCK(kq);
1818 knote_drop(kn, td);
1819 } else {
1820 /* Make sure cleared knotes disappear soon */
1821 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1822 KQ_UNLOCK(kq);
1823 }
1824 kq = NULL;
1825 }
1826
1827 if (!SLIST_EMPTY(&knl->kl_list)) {
1828 /* there are still KN_INFLUX remaining */
1829 kn = SLIST_FIRST(&knl->kl_list);
1830 kq = kn->kn_kq;
1831 KQ_LOCK(kq);
1832 KASSERT(kn->kn_status & KN_INFLUX,
1833 ("knote removed w/o list lock"));
1834 knl->kl_unlock(knl->kl_lockarg);
1835 kq->kq_state |= KQ_FLUXWAIT;
1836 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1837 kq = NULL;
1838 goto again;
1839 }
1840
1841 if (islocked)
1842 KNL_ASSERT_LOCKED(knl);
1843 else {
1844 knl->kl_unlock(knl->kl_lockarg);
1845 KNL_ASSERT_UNLOCKED(knl);
1846 }
1847 }
1848
1849 /*
1850 * Remove all knotes referencing a specified fd must be called with FILEDESC
1851 * lock. This prevents a race where a new fd comes along and occupies the
1852 * entry and we attach a knote to the fd.
1853 */
1854 void
1855 knote_fdclose(struct thread *td, int fd)
1856 {
1857 struct filedesc *fdp = td->td_proc->p_fd;
1858 struct kqueue *kq;
1859 struct knote *kn;
1860 int influx;
1861
1862 FILEDESC_XLOCK_ASSERT(fdp);
1863
1864 /*
1865 * We shouldn't have to worry about new kevents appearing on fd
1866 * since filedesc is locked.
1867 */
1868 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1869 KQ_LOCK(kq);
1870
1871 again:
1872 influx = 0;
1873 while (kq->kq_knlistsize > fd &&
1874 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1875 if (kn->kn_status & KN_INFLUX) {
1876 /* someone else might be waiting on our knote */
1877 if (influx)
1878 wakeup(kq);
1879 kq->kq_state |= KQ_FLUXWAIT;
1880 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1881 goto again;
1882 }
1883 kn->kn_status |= KN_INFLUX;
1884 KQ_UNLOCK(kq);
1885 if (!(kn->kn_status & KN_DETACHED))
1886 kn->kn_fop->f_detach(kn);
1887 knote_drop(kn, td);
1888 influx = 1;
1889 KQ_LOCK(kq);
1890 }
1891 KQ_UNLOCK_FLUX(kq);
1892 }
1893 }
1894
1895 static int
1896 knote_attach(struct knote *kn, struct kqueue *kq)
1897 {
1898 struct klist *list;
1899
1900 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1901 KQ_OWNED(kq);
1902
1903 if (kn->kn_fop->f_isfd) {
1904 if (kn->kn_id >= kq->kq_knlistsize)
1905 return ENOMEM;
1906 list = &kq->kq_knlist[kn->kn_id];
1907 } else {
1908 if (kq->kq_knhash == NULL)
1909 return ENOMEM;
1910 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1911 }
1912
1913 SLIST_INSERT_HEAD(list, kn, kn_link);
1914
1915 return 0;
1916 }
1917
1918 /*
1919 * knote must already have been detached using the f_detach method.
1920 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1921 * to prevent other removal.
1922 */
1923 static void
1924 knote_drop(struct knote *kn, struct thread *td)
1925 {
1926 struct kqueue *kq;
1927 struct klist *list;
1928
1929 kq = kn->kn_kq;
1930
1931 KQ_NOTOWNED(kq);
1932 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1933 ("knote_drop called without KN_INFLUX set in kn_status"));
1934
1935 KQ_LOCK(kq);
1936 if (kn->kn_fop->f_isfd)
1937 list = &kq->kq_knlist[kn->kn_id];
1938 else
1939 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1940
1941 if (!SLIST_EMPTY(list))
1942 SLIST_REMOVE(list, kn, knote, kn_link);
1943 if (kn->kn_status & KN_QUEUED)
1944 knote_dequeue(kn);
1945 KQ_UNLOCK_FLUX(kq);
1946
1947 if (kn->kn_fop->f_isfd) {
1948 fdrop(kn->kn_fp, td);
1949 kn->kn_fp = NULL;
1950 }
1951 kqueue_fo_release(kn->kn_kevent.filter);
1952 kn->kn_fop = NULL;
1953 knote_free(kn);
1954 }
1955
1956 static void
1957 knote_enqueue(struct knote *kn)
1958 {
1959 struct kqueue *kq = kn->kn_kq;
1960
1961 KQ_OWNED(kn->kn_kq);
1962 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1963
1964 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1965 kn->kn_status |= KN_QUEUED;
1966 kq->kq_count++;
1967 kqueue_wakeup(kq);
1968 }
1969
1970 static void
1971 knote_dequeue(struct knote *kn)
1972 {
1973 struct kqueue *kq = kn->kn_kq;
1974
1975 KQ_OWNED(kn->kn_kq);
1976 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1977
1978 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1979 kn->kn_status &= ~KN_QUEUED;
1980 kq->kq_count--;
1981 }
1982
1983 static void
1984 knote_init(void)
1985 {
1986
1987 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
1988 NULL, NULL, UMA_ALIGN_PTR, 0);
1989 }
1990 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
1991
1992 static struct knote *
1993 knote_alloc(int waitok)
1994 {
1995 return ((struct knote *)uma_zalloc(knote_zone,
1996 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
1997 }
1998
1999 static void
2000 knote_free(struct knote *kn)
2001 {
2002 if (kn != NULL)
2003 uma_zfree(knote_zone, kn);
2004 }
2005
2006 /*
2007 * Register the kev w/ the kq specified by fd.
2008 */
2009 int
2010 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2011 {
2012 struct kqueue *kq;
2013 struct file *fp;
2014 int error;
2015
2016 if ((error = fget(td, fd, &fp)) != 0)
2017 return (error);
2018 if ((error = kqueue_acquire(fp, &kq)) != 0)
2019 goto noacquire;
2020
2021 error = kqueue_register(kq, kev, td, waitok);
2022
2023 kqueue_release(kq, 0);
2024
2025 noacquire:
2026 fdrop(fp, td);
2027
2028 return error;
2029 }
Cache object: eac1429f19f190cced6692248d9102d7
|