Deleted Added
sdiff udiff text old ( 70834 ) new ( 71500 )
full compact
1/*-
2 * Copyright (c) 1999,2000 Jonathan Lemon <jlemon@FreeBSD.org>
3 * All rights reserved.
4 *
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.
13 *
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.
25 *
26 * $FreeBSD: head/sys/kern/kern_event.c 71500 2001-01-24 00:35:12Z jhb $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/proc.h>
33#include <sys/malloc.h>
34#include <sys/unistd.h>
35#include <sys/file.h>
36#include <sys/fcntl.h>
37#include <sys/selinfo.h>
38#include <sys/queue.h>
39#include <sys/event.h>
40#include <sys/eventvar.h>
41#include <sys/poll.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/stat.h>
46#include <sys/sysproto.h>
47#include <sys/uio.h>
48
49#include <vm/vm_zone.h>
50
51static int filt_nullattach(struct knote *kn);
52static int filt_rwtypattach(struct knote *kn);
53static int filt_kqattach(struct knote *kn);
54static void filt_kqdetach(struct knote *kn);
55static int filt_kqueue(struct knote *kn, long hint);
56static int filt_procattach(struct knote *kn);
57static void filt_procdetach(struct knote *kn);
58static int filt_proc(struct knote *kn, long hint);
59
60static int kqueue_scan(struct file *fp, int maxevents,
61 struct kevent *ulistp, const struct timespec *timeout,
62 struct proc *p);
63static int kqueue_read(struct file *fp, struct uio *uio,
64 struct ucred *cred, int flags, struct proc *p);
65static int kqueue_write(struct file *fp, struct uio *uio,
66 struct ucred *cred, int flags, struct proc *p);
67static int kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
68 struct proc *p);
69static int kqueue_poll(struct file *fp, int events, struct ucred *cred,
70 struct proc *p);
71static int kqueue_stat(struct file *fp, struct stat *st, struct proc *p);
72static int kqueue_close(struct file *fp, struct proc *p);
73static void kqueue_wakeup(struct kqueue *kq);
74
75static void knote_attach(struct knote *kn, struct filedesc *fdp);
76static void knote_drop(struct knote *kn, struct proc *p);
77static void knote_enqueue(struct knote *kn);
78static void knote_dequeue(struct knote *kn);
79static void knote_init(void);
80static struct knote *knote_alloc(void);
81static void knote_free(struct knote *kn);
82
83static vm_zone_t knote_zone;
84
85#define KNOTE_ACTIVATE(kn) do { \
86 kn->kn_status |= KN_ACTIVE; \
87 if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
88 knote_enqueue(kn); \
89} while(0)
90
91#define KN_HASHSIZE 64 /* XXX should be tunable */
92#define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
93
94static struct fileops kqueueops = {
95 kqueue_read,
96 kqueue_write,
97 kqueue_ioctl,
98 kqueue_poll,
99 kqueue_stat,
100 kqueue_close
101};
102
103extern struct filterops so_rwfiltops[];
104extern struct filterops fifo_rwfiltops[];
105extern struct filterops pipe_rwfiltops[];
106extern struct filterops vn_rwfiltops[];
107
108static struct filterops kq_rwfiltops[] = {
109 { 1, filt_kqattach, filt_kqdetach, filt_kqueue },
110 { 1, filt_nullattach, NULL, NULL },
111};
112
113extern struct filterops aio_filtops;
114extern struct filterops sig_filtops;
115extern struct filterops vn_filtops;
116
117static struct filterops rwtype_filtops =
118 { 1, filt_rwtypattach, NULL, NULL };
119static struct filterops proc_filtops =
120 { 0, filt_procattach, filt_procdetach, filt_proc };
121
122/*
123 * XXX
124 * These must match the order of defines in <sys/file.h>
125 */
126static struct filterops *rwtypfilt_sw[] = {
127 NULL, /* 0 */
128 vn_rwfiltops, /* DTYPE_VNODE */
129 so_rwfiltops, /* DTYPE_SOCKET */
130 pipe_rwfiltops, /* DTYPE_PIPE */
131 fifo_rwfiltops, /* DTYPE_FIFO */
132 kq_rwfiltops, /* DTYPE_KQUEUE */
133};
134
135/*
136 * table for for all system-defined filters.
137 */
138static struct filterops *sysfilt_ops[] = {
139 &rwtype_filtops, /* EVFILT_READ */
140 &rwtype_filtops, /* EVFILT_WRITE */
141 &aio_filtops, /* EVFILT_AIO */
142 &vn_filtops, /* EVFILT_VNODE */
143 &proc_filtops, /* EVFILT_PROC */
144 &sig_filtops, /* EVFILT_SIGNAL */
145};
146
147static int
148filt_nullattach(struct knote *kn)
149{
150 return (ENXIO);
151}
152
153/*
154 * file-type specific attach routine for read/write filters
155 */
156static int
157filt_rwtypattach(struct knote *kn)
158{
159 struct filterops *fops;
160
161 fops = rwtypfilt_sw[kn->kn_fp->f_type];
162 if (fops == NULL)
163 return (EINVAL);
164 kn->kn_fop = &fops[~kn->kn_filter]; /* convert to 0-base index */
165 return (kn->kn_fop->f_attach(kn));
166}
167
168static int
169filt_kqattach(struct knote *kn)
170{
171 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
172
173 SLIST_INSERT_HEAD(&kq->kq_sel.si_note, kn, kn_selnext);
174 return (0);
175}
176
177static void
178filt_kqdetach(struct knote *kn)
179{
180 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
181
182 SLIST_REMOVE(&kq->kq_sel.si_note, kn, knote, kn_selnext);
183}
184
185/*ARGSUSED*/
186static int
187filt_kqueue(struct knote *kn, long hint)
188{
189 struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
190
191 kn->kn_data = kq->kq_count;
192 return (kn->kn_data > 0);
193}
194
195static int
196filt_procattach(struct knote *kn)
197{
198 struct proc *p;
199
200 p = pfind(kn->kn_id);
201 if (p == NULL)
202 return (ESRCH);
203 if (p_can(curproc, p, P_CAN_SEE, NULL))
204 return (EACCES);
205
206 kn->kn_ptr.p_proc = p;
207 kn->kn_flags |= EV_CLEAR; /* automatically set */
208
209 /*
210 * internal flag indicating registration done by kernel
211 */
212 if (kn->kn_flags & EV_FLAG1) {
213 kn->kn_data = kn->kn_sdata; /* ppid */
214 kn->kn_fflags = NOTE_CHILD;
215 kn->kn_flags &= ~EV_FLAG1;
216 }
217
218 PROC_LOCK(p);
219 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
220 PROC_UNLOCK(p);
221
222 return (0);
223}
224
225/*
226 * The knote may be attached to a different process, which may exit,
227 * leaving nothing for the knote to be attached to. So when the process
228 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
229 * it will be deleted when read out. However, as part of the knote deletion,
230 * this routine is called, so a check is needed to avoid actually performing
231 * a detach, because the original process does not exist any more.
232 */
233static void
234filt_procdetach(struct knote *kn)
235{
236 struct proc *p = kn->kn_ptr.p_proc;
237
238 if (kn->kn_status & KN_DETACHED)
239 return;
240
241 PROC_LOCK(p);
242 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
243 PROC_UNLOCK(p);
244}
245
246static int
247filt_proc(struct knote *kn, long hint)
248{
249 u_int event;
250
251 /*
252 * mask off extra data
253 */
254 event = (u_int)hint & NOTE_PCTRLMASK;
255
256 /*
257 * if the user is interested in this event, record it.
258 */
259 if (kn->kn_sfflags & event)
260 kn->kn_fflags |= event;
261
262 /*
263 * process is gone, so flag the event as finished.
264 */
265 if (event == NOTE_EXIT) {
266 kn->kn_status |= KN_DETACHED;
267 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
268 return (1);
269 }
270
271 /*
272 * process forked, and user wants to track the new process,
273 * so attach a new knote to it, and immediately report an
274 * event with the parent's pid.
275 */
276 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
277 struct kevent kev;
278 int error;
279
280 /*
281 * register knote with new process.
282 */
283 kev.ident = hint & NOTE_PDATAMASK; /* pid */
284 kev.filter = kn->kn_filter;
285 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
286 kev.fflags = kn->kn_sfflags;
287 kev.data = kn->kn_id; /* parent */
288 kev.udata = kn->kn_kevent.udata; /* preserve udata */
289 error = kqueue_register(kn->kn_kq, &kev, NULL);
290 if (error)
291 kn->kn_fflags |= NOTE_TRACKERR;
292 }
293
294 return (kn->kn_fflags != 0);
295}
296
297int
298kqueue(struct proc *p, struct kqueue_args *uap)
299{
300 struct filedesc *fdp = p->p_fd;
301 struct kqueue *kq;
302 struct file *fp;
303 int fd, error;
304
305 error = falloc(p, &fp, &fd);
306 if (error)
307 return (error);
308 fp->f_flag = FREAD | FWRITE;
309 fp->f_type = DTYPE_KQUEUE;
310 fp->f_ops = &kqueueops;
311 kq = malloc(sizeof(struct kqueue), M_TEMP, M_WAITOK | M_ZERO);
312 TAILQ_INIT(&kq->kq_head);
313 fp->f_data = (caddr_t)kq;
314 p->p_retval[0] = fd;
315 if (fdp->fd_knlistsize < 0)
316 fdp->fd_knlistsize = 0; /* this process has a kq */
317 kq->kq_fdp = fdp;
318 return (error);
319}
320
321#ifndef _SYS_SYSPROTO_H_
322struct kevent_args {
323 int fd;
324 const struct kevent *changelist;
325 int nchanges;
326 struct kevent *eventlist;
327 int nevents;
328 const struct timespec *timeout;
329};
330#endif
331int
332kevent(struct proc *p, struct kevent_args *uap)
333{
334 struct filedesc* fdp = p->p_fd;
335 struct kevent *kevp;
336 struct kqueue *kq;
337 struct file *fp = NULL;
338 struct timespec ts;
339 int i, n, nerrors, error;
340
341 if (((u_int)uap->fd) >= fdp->fd_nfiles ||
342 (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
343 (fp->f_type != DTYPE_KQUEUE))
344 return (EBADF);
345
346 fhold(fp);
347
348 if (uap->timeout != NULL) {
349 error = copyin(uap->timeout, &ts, sizeof(ts));
350 if (error)
351 goto done;
352 uap->timeout = &ts;
353 }
354
355 kq = (struct kqueue *)fp->f_data;
356 nerrors = 0;
357
358 while (uap->nchanges > 0) {
359 n = uap->nchanges > KQ_NEVENTS ? KQ_NEVENTS : uap->nchanges;
360 error = copyin(uap->changelist, kq->kq_kev,
361 n * sizeof(struct kevent));
362 if (error)
363 goto done;
364 for (i = 0; i < n; i++) {
365 kevp = &kq->kq_kev[i];
366 kevp->flags &= ~EV_SYSFLAGS;
367 error = kqueue_register(kq, kevp, p);
368 if (error) {
369 if (uap->nevents != 0) {
370 kevp->flags = EV_ERROR;
371 kevp->data = error;
372 (void) copyout((caddr_t)kevp,
373 (caddr_t)uap->eventlist,
374 sizeof(*kevp));
375 uap->eventlist++;
376 uap->nevents--;
377 nerrors++;
378 } else {
379 goto done;
380 }
381 }
382 }
383 uap->nchanges -= n;
384 uap->changelist += n;
385 }
386 if (nerrors) {
387 p->p_retval[0] = nerrors;
388 error = 0;
389 goto done;
390 }
391
392 error = kqueue_scan(fp, uap->nevents, uap->eventlist, uap->timeout, p);
393done:
394 if (fp != NULL)
395 fdrop(fp, p);
396 return (error);
397}
398
399int
400kqueue_register(struct kqueue *kq, struct kevent *kev, struct proc *p)
401{
402 struct filedesc *fdp = kq->kq_fdp;
403 struct filterops *fops;
404 struct file *fp = NULL;
405 struct knote *kn = NULL;
406 int s, error = 0;
407
408 if (kev->filter < 0) {
409 if (kev->filter + EVFILT_SYSCOUNT < 0)
410 return (EINVAL);
411 fops = sysfilt_ops[~kev->filter]; /* to 0-base index */
412 } else {
413 /*
414 * XXX
415 * filter attach routine is responsible for insuring that
416 * the identifier can be attached to it.
417 */
418 printf("unknown filter: %d\n", kev->filter);
419 return (EINVAL);
420 }
421
422 if (fops->f_isfd) {
423 /* validate descriptor */
424 if ((u_int)kev->ident >= fdp->fd_nfiles ||
425 (fp = fdp->fd_ofiles[kev->ident]) == NULL)
426 return (EBADF);
427 fhold(fp);
428
429 if (kev->ident < fdp->fd_knlistsize) {
430 SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link)
431 if (kq == kn->kn_kq &&
432 kev->filter == kn->kn_filter)
433 break;
434 }
435 } else {
436 if (fdp->fd_knhashmask != 0) {
437 struct klist *list;
438
439 list = &fdp->fd_knhash[
440 KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)];
441 SLIST_FOREACH(kn, list, kn_link)
442 if (kev->ident == kn->kn_id &&
443 kq == kn->kn_kq &&
444 kev->filter == kn->kn_filter)
445 break;
446 }
447 }
448
449 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
450 error = ENOENT;
451 goto done;
452 }
453
454 /*
455 * kn now contains the matching knote, or NULL if no match
456 */
457 if (kev->flags & EV_ADD) {
458
459 if (kn == NULL) {
460 kn = knote_alloc();
461 if (kn == NULL) {
462 error = ENOMEM;
463 goto done;
464 }
465 kn->kn_fp = fp;
466 kn->kn_kq = kq;
467 kn->kn_fop = fops;
468
469 /*
470 * apply reference count to knode structure, so
471 * do not release it at the end of this routine.
472 */
473 fp = NULL;
474
475 kn->kn_sfflags = kev->fflags;
476 kn->kn_sdata = kev->data;
477 kev->fflags = 0;
478 kev->data = 0;
479 kn->kn_kevent = *kev;
480
481 knote_attach(kn, fdp);
482 if ((error = fops->f_attach(kn)) != 0) {
483 knote_drop(kn, p);
484 goto done;
485 }
486 } else {
487 /*
488 * The user may change some filter values after the
489 * initial EV_ADD, but doing so will not reset any
490 * filter which have already been triggered.
491 */
492 kn->kn_sfflags = kev->fflags;
493 kn->kn_sdata = kev->data;
494 kn->kn_kevent.udata = kev->udata;
495 }
496
497 s = splhigh();
498 if (kn->kn_fop->f_event(kn, 0))
499 KNOTE_ACTIVATE(kn);
500 splx(s);
501
502 } else if (kev->flags & EV_DELETE) {
503 kn->kn_fop->f_detach(kn);
504 knote_drop(kn, p);
505 goto done;
506 }
507
508 if ((kev->flags & EV_DISABLE) &&
509 ((kn->kn_status & KN_DISABLED) == 0)) {
510 s = splhigh();
511 kn->kn_status |= KN_DISABLED;
512 splx(s);
513 }
514
515 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
516 s = splhigh();
517 kn->kn_status &= ~KN_DISABLED;
518 if ((kn->kn_status & KN_ACTIVE) &&
519 ((kn->kn_status & KN_QUEUED) == 0))
520 knote_enqueue(kn);
521 splx(s);
522 }
523
524done:
525 if (fp != NULL)
526 fdrop(fp, p);
527 return (error);
528}
529
530static int
531kqueue_scan(struct file *fp, int maxevents, struct kevent *ulistp,
532 const struct timespec *tsp, struct proc *p)
533{
534 struct kqueue *kq = (struct kqueue *)fp->f_data;
535 struct kevent *kevp;
536 struct timeval atv, rtv, ttv;
537 struct knote *kn, marker;
538 int s, count, timeout, nkev = 0, error = 0;
539
540 count = maxevents;
541 if (count == 0)
542 goto done;
543
544 if (tsp != NULL) {
545 TIMESPEC_TO_TIMEVAL(&atv, tsp);
546 if (itimerfix(&atv)) {
547 error = EINVAL;
548 goto done;
549 }
550 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
551 timeout = -1;
552 else
553 timeout = atv.tv_sec > 24 * 60 * 60 ?
554 24 * 60 * 60 * hz : tvtohz(&atv);
555 getmicrouptime(&rtv);
556 timevaladd(&atv, &rtv);
557 } else {
558 atv.tv_sec = 0;
559 atv.tv_usec = 0;
560 timeout = 0;
561 }
562 goto start;
563
564retry:
565 if (atv.tv_sec || atv.tv_usec) {
566 getmicrouptime(&rtv);
567 if (timevalcmp(&rtv, &atv, >=))
568 goto done;
569 ttv = atv;
570 timevalsub(&ttv, &rtv);
571 timeout = ttv.tv_sec > 24 * 60 * 60 ?
572 24 * 60 * 60 * hz : tvtohz(&ttv);
573 }
574
575start:
576 kevp = kq->kq_kev;
577 s = splhigh();
578 if (kq->kq_count == 0) {
579 if (timeout < 0) {
580 error = EWOULDBLOCK;
581 } else {
582 kq->kq_state |= KQ_SLEEP;
583 error = tsleep(kq, PSOCK | PCATCH, "kqread", timeout);
584 }
585 splx(s);
586 if (error == 0)
587 goto retry;
588 /* don't restart after signals... */
589 if (error == ERESTART)
590 error = EINTR;
591 else if (error == EWOULDBLOCK)
592 error = 0;
593 goto done;
594 }
595
596 TAILQ_INSERT_TAIL(&kq->kq_head, &marker, kn_tqe);
597 while (count) {
598 kn = TAILQ_FIRST(&kq->kq_head);
599 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
600 if (kn == &marker) {
601 splx(s);
602 if (count == maxevents)
603 goto retry;
604 goto done;
605 }
606 if (kn->kn_status & KN_DISABLED) {
607 kn->kn_status &= ~KN_QUEUED;
608 kq->kq_count--;
609 continue;
610 }
611 if ((kn->kn_flags & EV_ONESHOT) == 0 &&
612 kn->kn_fop->f_event(kn, 0) == 0) {
613 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
614 kq->kq_count--;
615 continue;
616 }
617 *kevp = kn->kn_kevent;
618 kevp++;
619 nkev++;
620 if (kn->kn_flags & EV_ONESHOT) {
621 kn->kn_status &= ~KN_QUEUED;
622 kq->kq_count--;
623 splx(s);
624 kn->kn_fop->f_detach(kn);
625 knote_drop(kn, p);
626 s = splhigh();
627 } else if (kn->kn_flags & EV_CLEAR) {
628 kn->kn_data = 0;
629 kn->kn_fflags = 0;
630 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
631 kq->kq_count--;
632 } else {
633 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
634 }
635 count--;
636 if (nkev == KQ_NEVENTS) {
637 splx(s);
638 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
639 sizeof(struct kevent) * nkev);
640 ulistp += nkev;
641 nkev = 0;
642 kevp = kq->kq_kev;
643 s = splhigh();
644 if (error)
645 break;
646 }
647 }
648 TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe);
649 splx(s);
650done:
651 if (nkev != 0)
652 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp,
653 sizeof(struct kevent) * nkev);
654 p->p_retval[0] = maxevents - count;
655 return (error);
656}
657
658/*
659 * XXX
660 * This could be expanded to call kqueue_scan, if desired.
661 */
662/*ARGSUSED*/
663static int
664kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred,
665 int flags, struct proc *p)
666{
667 return (ENXIO);
668}
669
670/*ARGSUSED*/
671static int
672kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred,
673 int flags, struct proc *p)
674{
675 return (ENXIO);
676}
677
678/*ARGSUSED*/
679static int
680kqueue_ioctl(struct file *fp, u_long com, caddr_t data, struct proc *p)
681{
682 return (ENOTTY);
683}
684
685/*ARGSUSED*/
686static int
687kqueue_poll(struct file *fp, int events, struct ucred *cred, struct proc *p)
688{
689 struct kqueue *kq = (struct kqueue *)fp->f_data;
690 int revents = 0;
691 int s = splnet();
692
693 if (events & (POLLIN | POLLRDNORM)) {
694 if (kq->kq_count) {
695 revents |= events & (POLLIN | POLLRDNORM);
696 } else {
697 selrecord(p, &kq->kq_sel);
698 kq->kq_state |= KQ_SEL;
699 }
700 }
701 splx(s);
702 return (revents);
703}
704
705/*ARGSUSED*/
706static int
707kqueue_stat(struct file *fp, struct stat *st, struct proc *p)
708{
709 struct kqueue *kq = (struct kqueue *)fp->f_data;
710
711 bzero((void *)st, sizeof(*st));
712 st->st_size = kq->kq_count;
713 st->st_blksize = sizeof(struct kevent);
714 st->st_mode = S_IFIFO;
715 return (0);
716}
717
718/*ARGSUSED*/
719static int
720kqueue_close(struct file *fp, struct proc *p)
721{
722 struct kqueue *kq = (struct kqueue *)fp->f_data;
723 struct filedesc *fdp = p->p_fd;
724 struct knote **knp, *kn, *kn0;
725 int i;
726
727 for (i = 0; i < fdp->fd_knlistsize; i++) {
728 knp = &SLIST_FIRST(&fdp->fd_knlist[i]);
729 kn = *knp;
730 while (kn != NULL) {
731 kn0 = SLIST_NEXT(kn, kn_link);
732 if (kq == kn->kn_kq) {
733 kn->kn_fop->f_detach(kn);
734 fdrop(kn->kn_fp, p);
735 knote_free(kn);
736 *knp = kn0;
737 } else {
738 knp = &SLIST_NEXT(kn, kn_link);
739 }
740 kn = kn0;
741 }
742 }
743 if (fdp->fd_knhashmask != 0) {
744 for (i = 0; i < fdp->fd_knhashmask + 1; i++) {
745 knp = &SLIST_FIRST(&fdp->fd_knhash[i]);
746 kn = *knp;
747 while (kn != NULL) {
748 kn0 = SLIST_NEXT(kn, kn_link);
749 if (kq == kn->kn_kq) {
750 kn->kn_fop->f_detach(kn);
751 /* XXX non-fd release of kn->kn_ptr */
752 knote_free(kn);
753 *knp = kn0;
754 } else {
755 knp = &SLIST_NEXT(kn, kn_link);
756 }
757 kn = kn0;
758 }
759 }
760 }
761 free(kq, M_TEMP);
762 fp->f_data = NULL;
763
764 return (0);
765}
766
767static void
768kqueue_wakeup(struct kqueue *kq)
769{
770
771 if (kq->kq_state & KQ_SLEEP) {
772 kq->kq_state &= ~KQ_SLEEP;
773 wakeup(kq);
774 }
775 if (kq->kq_state & KQ_SEL) {
776 kq->kq_state &= ~KQ_SEL;
777 selwakeup(&kq->kq_sel);
778 }
779 KNOTE(&kq->kq_sel.si_note, 0);
780}
781
782/*
783 * walk down a list of knotes, activating them if their event has triggered.
784 */
785void
786knote(struct klist *list, long hint)
787{
788 struct knote *kn;
789
790 SLIST_FOREACH(kn, list, kn_selnext)
791 if (kn->kn_fop->f_event(kn, hint))
792 KNOTE_ACTIVATE(kn);
793}
794
795/*
796 * remove all knotes from a specified klist
797 */
798void
799knote_remove(struct proc *p, struct klist *list)
800{
801 struct knote *kn;
802
803 while ((kn = SLIST_FIRST(list)) != NULL) {
804 kn->kn_fop->f_detach(kn);
805 knote_drop(kn, p);
806 }
807}
808
809/*
810 * remove all knotes referencing a specified fd
811 */
812void
813knote_fdclose(struct proc *p, int fd)
814{
815 struct filedesc *fdp = p->p_fd;
816 struct klist *list = &fdp->fd_knlist[fd];
817
818 knote_remove(p, list);
819}
820
821static void
822knote_attach(struct knote *kn, struct filedesc *fdp)
823{
824 struct klist *list;
825 int size;
826
827 if (! kn->kn_fop->f_isfd) {
828 if (fdp->fd_knhashmask == 0)
829 fdp->fd_knhash = hashinit(KN_HASHSIZE, M_TEMP,
830 &fdp->fd_knhashmask);
831 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
832 goto done;
833 }
834
835 if (fdp->fd_knlistsize <= kn->kn_id) {
836 size = fdp->fd_knlistsize;
837 while (size <= kn->kn_id)
838 size += KQEXTENT;
839 MALLOC(list, struct klist *,
840 size * sizeof(struct klist *), M_TEMP, M_WAITOK);
841 bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list,
842 fdp->fd_knlistsize * sizeof(struct klist *));
843 bzero((caddr_t)list +
844 fdp->fd_knlistsize * sizeof(struct klist *),
845 (size - fdp->fd_knlistsize) * sizeof(struct klist *));
846 if (fdp->fd_knlist != NULL)
847 FREE(fdp->fd_knlist, M_TEMP);
848 fdp->fd_knlistsize = size;
849 fdp->fd_knlist = list;
850 }
851 list = &fdp->fd_knlist[kn->kn_id];
852done:
853 SLIST_INSERT_HEAD(list, kn, kn_link);
854 kn->kn_status = 0;
855}
856
857/*
858 * should be called at spl == 0, since we don't want to hold spl
859 * while calling fdrop and free.
860 */
861static void
862knote_drop(struct knote *kn, struct proc *p)
863{
864 struct filedesc *fdp = p->p_fd;
865 struct klist *list;
866
867 if (kn->kn_fop->f_isfd)
868 list = &fdp->fd_knlist[kn->kn_id];
869 else
870 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)];
871
872 SLIST_REMOVE(list, kn, knote, kn_link);
873 if (kn->kn_status & KN_QUEUED)
874 knote_dequeue(kn);
875 if (kn->kn_fop->f_isfd)
876 fdrop(kn->kn_fp, p);
877 knote_free(kn);
878}
879
880
881static void
882knote_enqueue(struct knote *kn)
883{
884 struct kqueue *kq = kn->kn_kq;
885 int s = splhigh();
886
887 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
888
889 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
890 kn->kn_status |= KN_QUEUED;
891 kq->kq_count++;
892 splx(s);
893 kqueue_wakeup(kq);
894}
895
896static void
897knote_dequeue(struct knote *kn)
898{
899 struct kqueue *kq = kn->kn_kq;
900 int s = splhigh();
901
902 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
903
904 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
905 kn->kn_status &= ~KN_QUEUED;
906 kq->kq_count--;
907 splx(s);
908}
909
910static void
911knote_init(void)
912{
913 knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
914}
915SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
916
917static struct knote *
918knote_alloc(void)
919{
920 return ((struct knote *)zalloc(knote_zone));
921}
922
923static void
924knote_free(struct knote *kn)
925{
926 zfree(knote_zone, kn);
927}