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