kernel - Make adjustments to the per-lwp ucred
[dragonfly.git] / sys / kern / kern_event.c
blob74d4efda28eda527696a2655e2841718194d2591
1 /*-
2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/kern/kern_event.c,v 1.2.2.10 2004/04/04 07:03:14 cperciva Exp $
27 * $DragonFly: src/sys/kern/kern_event.c,v 1.33 2007/02/03 17:05:57 corecode Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/proc.h>
34 #include <sys/malloc.h>
35 #include <sys/unistd.h>
36 #include <sys/file.h>
37 #include <sys/lock.h>
38 #include <sys/fcntl.h>
39 #include <sys/select.h>
40 #include <sys/queue.h>
41 #include <sys/event.h>
42 #include <sys/eventvar.h>
43 #include <sys/poll.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysproto.h>
50 #include <sys/uio.h>
51 #include <sys/thread2.h>
52 #include <sys/signalvar.h>
53 #include <sys/filio.h>
54 #include <sys/file2.h>
56 #include <vm/vm_zone.h>
58 MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
60 static int kqueue_scan(struct file *fp, int maxevents,
61 struct kevent *ulistp, const struct timespec *timeout,
62 struct thread *td, int *res);
63 static int kqueue_read(struct file *fp, struct uio *uio,
64 struct ucred *cred, int flags);
65 static int kqueue_write(struct file *fp, struct uio *uio,
66 struct ucred *cred, int flags);
67 static int kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
68 struct ucred *cred, struct sysmsg *msg);
69 static int kqueue_poll(struct file *fp, int events, struct ucred *cred);
70 static int kqueue_kqfilter(struct file *fp, struct knote *kn);
71 static int kqueue_stat(struct file *fp, struct stat *st,
72 struct ucred *cred);
73 static int kqueue_close(struct file *fp);
74 static void kqueue_wakeup(struct kqueue *kq);
77 * MPSAFE
79 static struct fileops kqueueops = {
80 .fo_read = kqueue_read,
81 .fo_write = kqueue_write,
82 .fo_ioctl = kqueue_ioctl,
83 .fo_poll = kqueue_poll,
84 .fo_kqfilter = kqueue_kqfilter,
85 .fo_stat = kqueue_stat,
86 .fo_close = kqueue_close,
87 .fo_shutdown = nofo_shutdown
90 static void knote_attach(struct knote *kn, struct filedesc *fdp);
91 static void knote_drop(struct knote *kn, struct thread *td);
92 static void knote_enqueue(struct knote *kn);
93 static void knote_dequeue(struct knote *kn);
94 static void knote_init(void);
95 static struct knote *knote_alloc(void);
96 static void knote_free(struct knote *kn);
98 static void filt_kqdetach(struct knote *kn);
99 static int filt_kqueue(struct knote *kn, long hint);
100 static int filt_procattach(struct knote *kn);
101 static void filt_procdetach(struct knote *kn);
102 static int filt_proc(struct knote *kn, long hint);
103 static int filt_fileattach(struct knote *kn);
104 static void filt_timerexpire(void *knx);
105 static int filt_timerattach(struct knote *kn);
106 static void filt_timerdetach(struct knote *kn);
107 static int filt_timer(struct knote *kn, long hint);
109 static struct filterops file_filtops =
110 { 1, filt_fileattach, NULL, NULL };
111 static struct filterops kqread_filtops =
112 { 1, NULL, filt_kqdetach, filt_kqueue };
113 static struct filterops proc_filtops =
114 { 0, filt_procattach, filt_procdetach, filt_proc };
115 static struct filterops timer_filtops =
116 { 0, filt_timerattach, filt_timerdetach, filt_timer };
118 static vm_zone_t knote_zone;
119 static int kq_ncallouts = 0;
120 static int kq_calloutmax = (4 * 1024);
121 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
122 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
124 #define KNOTE_ACTIVATE(kn) do { \
125 kn->kn_status |= KN_ACTIVE; \
126 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
127 knote_enqueue(kn); \
128 } while(0)
130 #define KN_HASHSIZE 64 /* XXX should be tunable */
131 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
133 extern struct filterops aio_filtops;
134 extern struct filterops sig_filtops;
137 * Table for for all system-defined filters.
139 static struct filterops *sysfilt_ops[] = {
140 &file_filtops, /* EVFILT_READ */
141 &file_filtops, /* EVFILT_WRITE */
142 &aio_filtops, /* EVFILT_AIO */
143 &file_filtops, /* EVFILT_VNODE */
144 &proc_filtops, /* EVFILT_PROC */
145 &sig_filtops, /* EVFILT_SIGNAL */
146 &timer_filtops, /* EVFILT_TIMER */
149 static int
150 filt_fileattach(struct knote *kn)
152 return (fo_kqfilter(kn->kn_fp, kn));
156 * MPALMOSTSAFE - acquires mplock
158 static int
159 kqueue_kqfilter(struct file *fp, struct knote *kn)
161 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
163 get_mplock();
164 if (kn->kn_filter != EVFILT_READ) {
165 rel_mplock();
166 return (1);
169 kn->kn_fop = &kqread_filtops;
170 SLIST_INSERT_HEAD(&kq->kq_sel.si_note, kn, kn_selnext);
171 rel_mplock();
172 return (0);
175 static void
176 filt_kqdetach(struct knote *kn)
178 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
180 SLIST_REMOVE(&kq->kq_sel.si_note, kn, knote, kn_selnext);
183 /*ARGSUSED*/
184 static int
185 filt_kqueue(struct knote *kn, long hint)
187 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
189 kn->kn_data = kq->kq_count;
190 return (kn->kn_data > 0);
193 static int
194 filt_procattach(struct knote *kn)
196 struct proc *p;
197 int immediate;
199 immediate = 0;
200 p = pfind(kn->kn_id);
201 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
202 p = zpfind(kn->kn_id);
203 immediate = 1;
205 if (p == NULL)
206 return (ESRCH);
207 if (! PRISON_CHECK(curproc->p_ucred, p->p_ucred))
208 return (EACCES);
210 kn->kn_ptr.p_proc = p;
211 kn->kn_flags |= EV_CLEAR; /* automatically set */
214 * internal flag indicating registration done by kernel
216 if (kn->kn_flags & EV_FLAG1) {
217 kn->kn_data = kn->kn_sdata; /* ppid */
218 kn->kn_fflags = NOTE_CHILD;
219 kn->kn_flags &= ~EV_FLAG1;
222 /* XXX lock the proc here while adding to the list? */
223 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
226 * Immediately activate any exit notes if the target process is a
227 * zombie. This is necessary to handle the case where the target
228 * process, e.g. a child, dies before the kevent is registered.
230 if (immediate && filt_proc(kn, NOTE_EXIT))
231 KNOTE_ACTIVATE(kn);
233 return (0);
237 * The knote may be attached to a different process, which may exit,
238 * leaving nothing for the knote to be attached to. So when the process
239 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
240 * it will be deleted when read out. However, as part of the knote deletion,
241 * this routine is called, so a check is needed to avoid actually performing
242 * a detach, because the original process does not exist any more.
244 static void
245 filt_procdetach(struct knote *kn)
247 struct proc *p;
249 if (kn->kn_status & KN_DETACHED)
250 return;
251 /* XXX locking? this might modify another process. */
252 p = kn->kn_ptr.p_proc;
253 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
256 static int
257 filt_proc(struct knote *kn, long hint)
259 u_int event;
262 * mask off extra data
264 event = (u_int)hint & NOTE_PCTRLMASK;
267 * if the user is interested in this event, record it.
269 if (kn->kn_sfflags & event)
270 kn->kn_fflags |= event;
273 * Process is gone, so flag the event as finished. Detach the
274 * knote from the process now because the process will be poof,
275 * gone later on.
277 if (event == NOTE_EXIT) {
278 struct proc *p = kn->kn_ptr.p_proc;
279 if ((kn->kn_status & KN_DETACHED) == 0) {
280 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
281 kn->kn_status |= KN_DETACHED;
282 kn->kn_data = p->p_xstat;
283 kn->kn_ptr.p_proc = NULL;
285 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
286 return (1);
290 * process forked, and user wants to track the new process,
291 * so attach a new knote to it, and immediately report an
292 * event with the parent's pid.
294 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
295 struct kevent kev;
296 int error;
299 * register knote with new process.
301 kev.ident = hint & NOTE_PDATAMASK; /* pid */
302 kev.filter = kn->kn_filter;
303 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
304 kev.fflags = kn->kn_sfflags;
305 kev.data = kn->kn_id; /* parent */
306 kev.udata = kn->kn_kevent.udata; /* preserve udata */
307 error = kqueue_register(kn->kn_kq, &kev, NULL);
308 if (error)
309 kn->kn_fflags |= NOTE_TRACKERR;
312 return (kn->kn_fflags != 0);
315 static void
316 filt_timerexpire(void *knx)
318 struct knote *kn = knx;
319 struct callout *calloutp;
320 struct timeval tv;
321 int tticks;
323 kn->kn_data++;
324 KNOTE_ACTIVATE(kn);
326 if ((kn->kn_flags & EV_ONESHOT) == 0) {
327 tv.tv_sec = kn->kn_sdata / 1000;
328 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
329 tticks = tvtohz_high(&tv);
330 calloutp = (struct callout *)kn->kn_hook;
331 callout_reset(calloutp, tticks, filt_timerexpire, kn);
336 * data contains amount of time to sleep, in milliseconds
338 static int
339 filt_timerattach(struct knote *kn)
341 struct callout *calloutp;
342 struct timeval tv;
343 int tticks;
345 if (kq_ncallouts >= kq_calloutmax)
346 return (ENOMEM);
347 kq_ncallouts++;
349 tv.tv_sec = kn->kn_sdata / 1000;
350 tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
351 tticks = tvtohz_high(&tv);
353 kn->kn_flags |= EV_CLEAR; /* automatically set */
354 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
355 M_KQUEUE, M_WAITOK);
356 callout_init(calloutp);
357 kn->kn_hook = (caddr_t)calloutp;
358 callout_reset(calloutp, tticks, filt_timerexpire, kn);
360 return (0);
363 static void
364 filt_timerdetach(struct knote *kn)
366 struct callout *calloutp;
368 calloutp = (struct callout *)kn->kn_hook;
369 callout_stop(calloutp);
370 FREE(calloutp, M_KQUEUE);
371 kq_ncallouts--;
374 static int
375 filt_timer(struct knote *kn, long hint)
378 return (kn->kn_data != 0);
382 * MPSAFE
385 sys_kqueue(struct kqueue_args *uap)
387 struct proc *p = curproc;
388 struct filedesc *fdp = p->p_fd;
389 struct kqueue *kq;
390 struct file *fp;
391 int fd, error;
393 error = falloc(p, &fp, &fd);
394 if (error)
395 return (error);
396 fp->f_flag = FREAD | FWRITE;
397 fp->f_type = DTYPE_KQUEUE;
398 fp->f_ops = &kqueueops;
400 kq = kmalloc(sizeof(struct kqueue), M_KQUEUE, M_WAITOK | M_ZERO);
401 TAILQ_INIT(&kq->kq_head);
402 kq->kq_fdp = fdp;
403 fp->f_data = kq;
405 fsetfd(p, fp, fd);
406 uap->sysmsg_result = fd;
407 fdrop(fp);
408 return (error);
412 * MPALMOSTSAFE
415 sys_kevent(struct kevent_args *uap)
417 struct thread *td = curthread;
418 struct proc *p = td->td_proc;
419 struct kevent *kevp;
420 struct kqueue *kq;
421 struct file *fp = NULL;
422 struct timespec ts;
423 int i, n, nerrors, error;
425 fp = holdfp(p->p_fd, uap->fd, -1);
426 if (fp == NULL)
427 return (EBADF);
428 if (fp->f_type != DTYPE_KQUEUE) {
429 fdrop(fp);
430 return (EBADF);
433 if (uap->timeout != NULL) {
434 error = copyin(uap->timeout, &ts, sizeof(ts));
435 if (error)
436 goto done;
437 uap->timeout = &ts;
440 kq = (struct kqueue *)fp->f_data;
441 nerrors = 0;
443 get_mplock();
444 while (uap->nchanges > 0) {
445 n = uap->nchanges > KQ_NEVENTS ? KQ_NEVENTS : uap->nchanges;
446 error = copyin(uap->changelist, kq->kq_kev,
447 n * sizeof(struct kevent));
448 if (error)
449 goto done;
450 for (i = 0; i < n; i++) {
451 kevp = &kq->kq_kev[i];
452 kevp->flags &= ~EV_SYSFLAGS;
453 error = kqueue_register(kq, kevp, td);
454 if (error) {
455 if (uap->nevents != 0) {
456 kevp->flags = EV_ERROR;
457 kevp->data = error;
458 (void) copyout((caddr_t)kevp,
459 (caddr_t)uap->eventlist,
460 sizeof(*kevp));
461 uap->eventlist++;
462 uap->nevents--;
463 nerrors++;
464 } else {
465 goto done;
469 uap->nchanges -= n;
470 uap->changelist += n;
472 if (nerrors) {
473 uap->sysmsg_result = nerrors;
474 error = 0;
475 goto done;
478 error = kqueue_scan(fp, uap->nevents, uap->eventlist,
479 uap->timeout, td, &uap->sysmsg_result);
480 done:
481 rel_mplock();
482 if (fp != NULL)
483 fdrop(fp);
484 return (error);
488 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td)
490 struct filedesc *fdp = kq->kq_fdp;
491 struct filterops *fops;
492 struct file *fp = NULL;
493 struct knote *kn = NULL;
494 int error = 0;
496 if (kev->filter < 0) {
497 if (kev->filter + EVFILT_SYSCOUNT < 0)
498 return (EINVAL);
499 fops = sysfilt_ops[~kev->filter]; /* to 0-base index */
500 } else {
502 * XXX
503 * filter attach routine is responsible for insuring that
504 * the identifier can be attached to it.
506 kprintf("unknown filter: %d\n", kev->filter);
507 return (EINVAL);
510 if (fops->f_isfd) {
511 /* validate descriptor */
512 fp = holdfp(fdp, kev->ident, -1);
513 if (fp == NULL)
514 return (EBADF);
516 if (kev->ident < fdp->fd_knlistsize) {
517 SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link)
518 if (kq == kn->kn_kq &&
519 kev->filter == kn->kn_filter)
520 break;
522 } else {
523 if (fdp->fd_knhashmask != 0) {
524 struct klist *list;
526 list = &fdp->fd_knhash[
527 KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
528 SLIST_FOREACH(kn, list, kn_link)
529 if (kev->ident == kn->kn_id &&
530 kq == kn->kn_kq &&
531 kev->filter == kn->kn_filter)
532 break;
536 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
537 error = ENOENT;
538 goto done;
542 * kn now contains the matching knote, or NULL if no match
544 if (kev->flags & EV_ADD) {
546 if (kn == NULL) {
547 kn = knote_alloc();
548 if (kn == NULL) {
549 error = ENOMEM;
550 goto done;
552 kn->kn_fp = fp;
553 kn->kn_kq = kq;
554 kn->kn_fop = fops;
557 * apply reference count to knote structure, and
558 * do not release it at the end of this routine.
560 fp = NULL;
562 kn->kn_sfflags = kev->fflags;
563 kn->kn_sdata = kev->data;
564 kev->fflags = 0;
565 kev->data = 0;
566 kn->kn_kevent = *kev;
568 knote_attach(kn, fdp);
569 if ((error = fops->f_attach(kn)) != 0) {
570 knote_drop(kn, td);
571 goto done;
573 } else {
575 * The user may change some filter values after the
576 * initial EV_ADD, but doing so will not reset any
577 * filter which have already been triggered.
579 kn->kn_sfflags = kev->fflags;
580 kn->kn_sdata = kev->data;
581 kn->kn_kevent.udata = kev->udata;
584 crit_enter();
585 if (kn->kn_fop->f_event(kn, 0))
586 KNOTE_ACTIVATE(kn);
587 crit_exit();
588 } else if (kev->flags & EV_DELETE) {
589 kn->kn_fop->f_detach(kn);
590 knote_drop(kn, td);
591 goto done;
594 if ((kev->flags & EV_DISABLE) &&
595 ((kn->kn_status & KN_DISABLED) == 0)) {
596 crit_enter();
597 kn->kn_status |= KN_DISABLED;
598 crit_exit();
601 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
602 crit_enter();
603 kn->kn_status &= ~KN_DISABLED;
604 if ((kn->kn_status & KN_ACTIVE) &&
605 ((kn->kn_status & KN_QUEUED) == 0))
606 knote_enqueue(kn);
607 crit_exit();
610 done:
611 if (fp != NULL)
612 fdrop(fp);
613 return (error);
616 static int
617 kqueue_scan(struct file *fp, int maxevents, struct kevent *ulistp,
618 const struct timespec *tsp, struct thread *td, int *res)
620 struct kqueue *kq = (struct kqueue *)fp->f_data;
621 struct kevent *kevp;
622 struct timeval atv, rtv, ttv;
623 struct knote *kn, marker;
624 int count, timeout, nkev = 0, error = 0;
626 count = maxevents;
627 if (count == 0)
628 goto done;
630 if (tsp != NULL) {
631 TIMESPEC_TO_TIMEVAL(&atv, tsp);
632 if (itimerfix(&atv)) {
633 error = EINVAL;
634 goto done;
636 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
637 timeout = -1;
638 else
639 timeout = atv.tv_sec > 24 * 60 * 60 ?
640 24 * 60 * 60 * hz : tvtohz_high(&atv);
641 getmicrouptime(&rtv);
642 timevaladd(&atv, &rtv);
643 } else {
644 atv.tv_sec = 0;
645 atv.tv_usec = 0;
646 timeout = 0;
648 goto start;
650 retry:
651 if (atv.tv_sec || atv.tv_usec) {
652 getmicrouptime(&rtv);
653 if (timevalcmp(&rtv, &atv, >=))
654 goto done;
655 ttv = atv;
656 timevalsub(&ttv, &rtv);
657 timeout = ttv.tv_sec > 24 * 60 * 60 ?
658 24 * 60 * 60 * hz : tvtohz_high(&ttv);
661 start:
662 kevp = kq->kq_kev;
663 crit_enter();
664 if (kq->kq_count == 0) {
665 if (timeout < 0) {
666 error = EWOULDBLOCK;
667 } else {
668 kq->kq_state |= KQ_SLEEP;
669 error = tsleep(kq, PCATCH, "kqread", timeout);
671 crit_exit();
672 if (error == 0)
673 goto retry;
674 /* don't restart after signals... */
675 if (error == ERESTART)
676 error = EINTR;
677 else if (error == EWOULDBLOCK)
678 error = 0;
679 goto done;
682 TAILQ_INSERT_TAIL(&kq->kq_head, &marker, kn_tqe);
683 while (count) {
684 kn = TAILQ_FIRST(&kq->kq_head);
685 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
686 if (kn == &marker) {
687 crit_exit();
688 if (count == maxevents)
689 goto retry;
690 goto done;
692 if (kn->kn_status & KN_DISABLED) {
693 kn->kn_status &= ~KN_QUEUED;
694 kq->kq_count--;
695 continue;
697 if ((kn->kn_flags & EV_ONESHOT) == 0 &&
698 kn->kn_fop->f_event(kn, 0) == 0) {
699 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
700 kq->kq_count--;
701 continue;
703 *kevp = kn->kn_kevent;
704 kevp++;
705 nkev++;
706 if (kn->kn_flags & EV_ONESHOT) {
707 kn->kn_status &= ~KN_QUEUED;
708 kq->kq_count--;
709 crit_exit();
710 kn->kn_fop->f_detach(kn);
711 knote_drop(kn, td);
712 crit_enter();
713 } else if (kn->kn_flags & EV_CLEAR) {
714 kn->kn_data = 0;
715 kn->kn_fflags = 0;
716 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
717 kq->kq_count--;
718 } else {
719 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
721 count--;
722 if (nkev == KQ_NEVENTS) {
723 crit_exit();
724 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
725 sizeof(struct kevent) * nkev);
726 ulistp += nkev;
727 nkev = 0;
728 kevp = kq->kq_kev;
729 crit_enter();
730 if (error)
731 break;
734 TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe);
735 crit_exit();
736 done:
737 if (nkev != 0)
738 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
739 sizeof(struct kevent) * nkev);
740 *res = maxevents - count;
741 return (error);
745 * XXX
746 * This could be expanded to call kqueue_scan, if desired.
748 * MPSAFE
750 static int
751 kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
753 return (ENXIO);
757 * MPSAFE
759 static int
760 kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
762 return (ENXIO);
766 * MPSAFE
768 static int
769 kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
770 struct ucred *cred, struct sysmsg *msg)
772 struct kqueue *kq;
773 int error;
775 get_mplock();
776 kq = (struct kqueue *)fp->f_data;
778 switch(com) {
779 case FIOASYNC:
780 if (*(int *)data)
781 kq->kq_state |= KQ_ASYNC;
782 else
783 kq->kq_state &= ~KQ_ASYNC;
784 error = 0;
785 break;
786 case FIOSETOWN:
787 error = fsetown(*(int *)data, &kq->kq_sigio);
788 break;
789 default:
790 error = ENOTTY;
791 break;
793 rel_mplock();
794 return (error);
798 * MPALMOSTSAFE - acquires mplock
800 static int
801 kqueue_poll(struct file *fp, int events, struct ucred *cred)
803 struct kqueue *kq = (struct kqueue *)fp->f_data;
804 int revents = 0;
806 get_mplock();
807 crit_enter();
808 if (events & (POLLIN | POLLRDNORM)) {
809 if (kq->kq_count) {
810 revents |= events & (POLLIN | POLLRDNORM);
811 } else {
812 selrecord(curthread, &kq->kq_sel);
813 kq->kq_state |= KQ_SEL;
816 crit_exit();
817 rel_mplock();
818 return (revents);
822 * MPSAFE
824 static int
825 kqueue_stat(struct file *fp, struct stat *st, struct ucred *cred)
827 struct kqueue *kq = (struct kqueue *)fp->f_data;
829 bzero((void *)st, sizeof(*st));
830 st->st_size = kq->kq_count;
831 st->st_blksize = sizeof(struct kevent);
832 st->st_mode = S_IFIFO;
833 return (0);
837 * MPALMOSTSAFE - acquires mplock
839 static int
840 kqueue_close(struct file *fp)
842 struct thread *td = curthread;
843 struct proc *p = td->td_proc;
844 struct kqueue *kq = (struct kqueue *)fp->f_data;
845 struct filedesc *fdp;
846 struct knote **knp, *kn, *kn0;
847 int i;
849 KKASSERT(p);
850 get_mplock();
851 fdp = p->p_fd;
852 for (i = 0; i < fdp->fd_knlistsize; i++) {
853 knp = &SLIST_FIRST(&fdp->fd_knlist[i]);
854 kn = *knp;
855 while (kn != NULL) {
856 kn0 = SLIST_NEXT(kn, kn_link);
857 if (kq == kn->kn_kq) {
858 kn->kn_fop->f_detach(kn);
859 fdrop(kn->kn_fp);
860 knote_free(kn);
861 *knp = kn0;
862 } else {
863 knp = &SLIST_NEXT(kn, kn_link);
865 kn = kn0;
868 if (fdp->fd_knhashmask != 0) {
869 for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
870 knp = &SLIST_FIRST(&fdp->fd_knhash[i]);
871 kn = *knp;
872 while (kn != NULL) {
873 kn0 = SLIST_NEXT(kn, kn_link);
874 if (kq == kn->kn_kq) {
875 kn->kn_fop->f_detach(kn);
876 /* XXX non-fd release of kn->kn_ptr */
877 knote_free(kn);
878 *knp = kn0;
879 } else {
880 knp = &SLIST_NEXT(kn, kn_link);
882 kn = kn0;
886 fp->f_data = NULL;
887 funsetown(kq->kq_sigio);
888 rel_mplock();
890 kfree(kq, M_KQUEUE);
891 return (0);
894 static void
895 kqueue_wakeup(struct kqueue *kq)
897 if (kq->kq_state & KQ_SLEEP) {
898 kq->kq_state &= ~KQ_SLEEP;
899 wakeup(kq);
901 if (kq->kq_state & KQ_SEL) {
902 kq->kq_state &= ~KQ_SEL;
903 selwakeup(&kq->kq_sel);
905 KNOTE(&kq->kq_sel.si_note, 0);
909 * walk down a list of knotes, activating them if their event has triggered.
911 void
912 knote(struct klist *list, long hint)
914 struct knote *kn;
916 SLIST_FOREACH(kn, list, kn_selnext)
917 if (kn->kn_fop->f_event(kn, hint))
918 KNOTE_ACTIVATE(kn);
922 * remove all knotes from a specified klist
924 void
925 knote_remove(struct thread *td, struct klist *list)
927 struct knote *kn;
929 while ((kn = SLIST_FIRST(list)) != NULL) {
930 kn->kn_fop->f_detach(kn);
931 knote_drop(kn, td);
936 * remove all knotes referencing a specified fd
938 void
939 knote_fdclose(struct proc *p, int fd)
941 struct filedesc *fdp = p->p_fd;
942 struct klist *list = &fdp->fd_knlist[fd];
943 /* Take any thread of p */
944 struct thread *td = FIRST_LWP_IN_PROC(p)->lwp_thread;
946 knote_remove(td, list);
949 static void
950 knote_attach(struct knote *kn, struct filedesc *fdp)
952 struct klist *list;
953 int size;
955 if (! kn->kn_fop->f_isfd) {
956 if (fdp->fd_knhashmask == 0)
957 fdp->fd_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
958 &fdp->fd_knhashmask);
959 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
960 goto done;
963 if (fdp->fd_knlistsize <= kn->kn_id) {
964 size = fdp->fd_knlistsize;
965 while (size <= kn->kn_id)
966 size += KQEXTENT;
967 MALLOC(list, struct klist *,
968 size * sizeof(struct klist *), M_KQUEUE, M_WAITOK);
969 bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list,
970 fdp->fd_knlistsize * sizeof(struct klist *));
971 bzero((caddr_t)list +
972 fdp->fd_knlistsize * sizeof(struct klist *),
973 (size - fdp->fd_knlistsize) * sizeof(struct klist *));
974 if (fdp->fd_knlist != NULL)
975 FREE(fdp->fd_knlist, M_KQUEUE);
976 fdp->fd_knlistsize = size;
977 fdp->fd_knlist = list;
979 list = &fdp->fd_knlist[kn->kn_id];
980 done:
981 SLIST_INSERT_HEAD(list, kn, kn_link);
982 kn->kn_status = 0;
986 * should be called outside of a critical section, since we don't want to
987 * hold a critical section while calling fdrop and free.
989 static void
990 knote_drop(struct knote *kn, struct thread *td)
992 struct filedesc *fdp;
993 struct klist *list;
995 KKASSERT(td->td_proc);
996 fdp = td->td_proc->p_fd;
997 if (kn->kn_fop->f_isfd)
998 list = &fdp->fd_knlist[kn->kn_id];
999 else
1000 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
1002 SLIST_REMOVE(list, kn, knote, kn_link);
1003 if (kn->kn_status & KN_QUEUED)
1004 knote_dequeue(kn);
1005 if (kn->kn_fop->f_isfd)
1006 fdrop(kn->kn_fp);
1007 knote_free(kn);
1011 static void
1012 knote_enqueue(struct knote *kn)
1014 struct kqueue *kq = kn->kn_kq;
1016 crit_enter();
1017 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1019 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1020 kn->kn_status |= KN_QUEUED;
1021 ++kq->kq_count;
1024 * Send SIGIO on request (typically set up as a mailbox signal)
1026 if (kq->kq_sigio && (kq->kq_state & KQ_ASYNC) && kq->kq_count == 1)
1027 pgsigio(kq->kq_sigio, SIGIO, 0);
1028 crit_exit();
1029 kqueue_wakeup(kq);
1032 static void
1033 knote_dequeue(struct knote *kn)
1035 struct kqueue *kq = kn->kn_kq;
1037 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1038 crit_enter();
1040 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1041 kn->kn_status &= ~KN_QUEUED;
1042 kq->kq_count--;
1043 crit_exit();
1046 static void
1047 knote_init(void)
1049 knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
1051 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
1053 static struct knote *
1054 knote_alloc(void)
1056 return ((struct knote *)zalloc(knote_zone));
1059 static void
1060 knote_free(struct knote *kn)
1062 zfree(knote_zone, kn);