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