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