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