1 /* $OpenBSD: kern_event.c,v 1.10 2001/10/26 12:03:27 art 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 FILE_SET_MATURE(fp); 298 return (0); 299 } 300 301 int 302 sys_kevent(struct proc *p, void *v, register_t *retval) 303 { 304 struct filedesc* fdp = p->p_fd; 305 struct sys_kevent_args /* { 306 syscallarg(int) fd; 307 syscallarg(const struct kevent *) changelist; 308 syscallarg(int) nchanges; 309 syscallarg(struct kevent *) eventlist; 310 syscallarg(int) nevents; 311 syscallarg(const struct timespec *) timeout; 312 } */ *uap = v; 313 struct kevent *kevp; 314 struct kqueue *kq; 315 struct file *fp = NULL; 316 struct timespec ts; 317 int i, n, nerrors, error; 318 319 if ((fp = fd_getfile(fdp, 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 ((fp = fd_getfile(fdp, kev->ident)) == NULL) 407 return (EBADF); 408 fp->f_count++; 409 410 if (kev->ident < fdp->fd_knlistsize) { 411 SLIST_FOREACH(kn, &fdp->fd_knlist[kev->ident], kn_link) 412 if (kq == kn->kn_kq && 413 kev->filter == kn->kn_filter) 414 break; 415 } 416 } else { 417 if (fdp->fd_knhashmask != 0) { 418 struct klist *list; 419 420 list = &fdp->fd_knhash[ 421 KN_HASH((u_long)kev->ident, fdp->fd_knhashmask)]; 422 SLIST_FOREACH(kn, list, kn_link) 423 if (kev->ident == kn->kn_id && 424 kq == kn->kn_kq && 425 kev->filter == kn->kn_filter) 426 break; 427 } 428 } 429 430 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) { 431 error = ENOENT; 432 goto done; 433 } 434 435 /* 436 * kn now contains the matching knote, or NULL if no match 437 */ 438 if (kev->flags & EV_ADD) { 439 440 if (kn == NULL) { 441 kn = knote_alloc(); 442 if (kn == NULL) { 443 error = ENOMEM; 444 goto done; 445 } 446 kn->kn_fp = fp; 447 kn->kn_kq = kq; 448 kn->kn_fop = fops; 449 450 /* 451 * apply reference count to knote structure, and 452 * do not release it at the end of this routine. 453 */ 454 fp = NULL; 455 456 kn->kn_sfflags = kev->fflags; 457 kn->kn_sdata = kev->data; 458 kev->fflags = 0; 459 kev->data = 0; 460 kn->kn_kevent = *kev; 461 462 knote_attach(kn, fdp); 463 if ((error = fops->f_attach(kn)) != 0) { 464 knote_drop(kn, p); 465 goto done; 466 } 467 } else { 468 /* 469 * The user may change some filter values after the 470 * initial EV_ADD, but doing so will not reset any 471 * filter which have already been triggered. 472 */ 473 kn->kn_sfflags = kev->fflags; 474 kn->kn_sdata = kev->data; 475 kn->kn_kevent.udata = kev->udata; 476 } 477 478 s = splhigh(); 479 if (kn->kn_fop->f_event(kn, 0)) 480 KNOTE_ACTIVATE(kn); 481 splx(s); 482 483 } else if (kev->flags & EV_DELETE) { 484 kn->kn_fop->f_detach(kn); 485 knote_drop(kn, p); 486 goto done; 487 } 488 489 if ((kev->flags & EV_DISABLE) && 490 ((kn->kn_status & KN_DISABLED) == 0)) { 491 s = splhigh(); 492 kn->kn_status |= KN_DISABLED; 493 splx(s); 494 } 495 496 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) { 497 s = splhigh(); 498 kn->kn_status &= ~KN_DISABLED; 499 if ((kn->kn_status & KN_ACTIVE) && 500 ((kn->kn_status & KN_QUEUED) == 0)) 501 knote_enqueue(kn); 502 splx(s); 503 } 504 505 done: 506 if (fp != NULL) 507 closef(fp, p); 508 return (error); 509 } 510 511 int 512 kqueue_scan(struct file *fp, int maxevents, struct kevent *ulistp, 513 const struct timespec *tsp, struct proc *p, int *retval) 514 { 515 struct kqueue *kq = (struct kqueue *)fp->f_data; 516 struct kevent *kevp; 517 struct timeval atv; 518 struct knote *kn, marker; 519 int s, count, timeout, nkev = 0, error = 0; 520 521 count = maxevents; 522 if (count == 0) 523 goto done; 524 525 if (tsp != NULL) { 526 TIMESPEC_TO_TIMEVAL(&atv, tsp); 527 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) { 528 /* No timeout, just poll */ 529 timeout = -1; 530 goto start; 531 } 532 if (itimerfix(&atv)) { 533 error = EINVAL; 534 goto done; 535 } 536 537 s = splclock(); 538 timeradd(&atv, &time, &atv); 539 timeout = hzto(&atv); 540 splx(s); 541 } else { 542 atv.tv_sec = 0; 543 atv.tv_usec = 0; 544 timeout = 0; 545 } 546 goto start; 547 548 retry: 549 if (atv.tv_sec || atv.tv_usec) { 550 timeout = hzto(&atv); 551 if (timeout <= 0) 552 goto done; 553 } 554 555 start: 556 kevp = kq->kq_kev; 557 s = splhigh(); 558 if (kq->kq_count == 0) { 559 if (timeout < 0) { 560 error = EWOULDBLOCK; 561 } else { 562 kq->kq_state |= KQ_SLEEP; 563 error = tsleep(kq, PSOCK | PCATCH, "kqread", timeout); 564 } 565 splx(s); 566 if (error == 0) 567 goto retry; 568 /* don't restart after signals... */ 569 if (error == ERESTART) 570 error = EINTR; 571 else if (error == EWOULDBLOCK) 572 error = 0; 573 goto done; 574 } 575 576 TAILQ_INSERT_TAIL(&kq->kq_head, &marker, kn_tqe); 577 while (count) { 578 kn = TAILQ_FIRST(&kq->kq_head); 579 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 580 if (kn == &marker) { 581 splx(s); 582 if (count == maxevents) 583 goto retry; 584 goto done; 585 } 586 if (kn->kn_status & KN_DISABLED) { 587 kn->kn_status &= ~KN_QUEUED; 588 kq->kq_count--; 589 continue; 590 } 591 if ((kn->kn_flags & EV_ONESHOT) == 0 && 592 kn->kn_fop->f_event(kn, 0) == 0) { 593 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE); 594 kq->kq_count--; 595 continue; 596 } 597 *kevp = kn->kn_kevent; 598 kevp++; 599 nkev++; 600 if (kn->kn_flags & EV_ONESHOT) { 601 kn->kn_status &= ~KN_QUEUED; 602 kq->kq_count--; 603 splx(s); 604 kn->kn_fop->f_detach(kn); 605 knote_drop(kn, p); 606 s = splhigh(); 607 } else if (kn->kn_flags & EV_CLEAR) { 608 kn->kn_data = 0; 609 kn->kn_fflags = 0; 610 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE); 611 kq->kq_count--; 612 } else { 613 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 614 } 615 count--; 616 if (nkev == KQ_NEVENTS) { 617 splx(s); 618 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp, 619 sizeof(struct kevent) * nkev); 620 ulistp += nkev; 621 nkev = 0; 622 kevp = kq->kq_kev; 623 s = splhigh(); 624 if (error) 625 break; 626 } 627 } 628 TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe); 629 splx(s); 630 done: 631 if (nkev != 0) 632 error = copyout((caddr_t)&kq->kq_kev, (caddr_t)ulistp, 633 sizeof(struct kevent) * nkev); 634 *retval = maxevents - count; 635 return (error); 636 } 637 638 /* 639 * XXX 640 * This could be expanded to call kqueue_scan, if desired. 641 */ 642 /*ARGSUSED*/ 643 int 644 kqueue_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred) 645 { 646 return (ENXIO); 647 } 648 649 /*ARGSUSED*/ 650 int 651 kqueue_write(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred) 652 653 { 654 return (ENXIO); 655 } 656 657 /*ARGSUSED*/ 658 int 659 kqueue_ioctl(struct file *fp, u_long com, caddr_t data, struct proc *p) 660 { 661 return (ENOTTY); 662 } 663 664 /*ARGSUSED*/ 665 int 666 kqueue_select(struct file *fp, int which, struct proc *p) 667 { 668 struct kqueue *kq = (struct kqueue *)fp->f_data; 669 int res = 0; 670 int s = splnet(); 671 672 if (which == FREAD) { 673 if (kq->kq_count) { 674 res = 1; 675 } else { 676 selrecord(p, &kq->kq_sel); 677 kq->kq_state |= KQ_SEL; 678 } 679 } 680 splx(s); 681 return (res); 682 } 683 684 /*ARGSUSED*/ 685 int 686 kqueue_stat(struct file *fp, struct stat *st, struct proc *p) 687 { 688 struct kqueue *kq = (struct kqueue *)fp->f_data; 689 690 bzero((void *)st, sizeof(*st)); 691 st->st_size = kq->kq_count; 692 st->st_blksize = sizeof(struct kevent); 693 st->st_mode = S_IFIFO; 694 return (0); 695 } 696 697 /*ARGSUSED*/ 698 int 699 kqueue_close(struct file *fp, struct proc *p) 700 { 701 struct kqueue *kq = (struct kqueue *)fp->f_data; 702 struct filedesc *fdp = p->p_fd; 703 struct knote **knp, *kn, *kn0; 704 int i; 705 706 for (i = 0; i < fdp->fd_knlistsize; i++) { 707 knp = &SLIST_FIRST(&fdp->fd_knlist[i]); 708 kn = *knp; 709 while (kn != NULL) { 710 kn0 = SLIST_NEXT(kn, kn_link); 711 if (kq == kn->kn_kq) { 712 kn->kn_fop->f_detach(kn); 713 closef(kn->kn_fp, p); 714 knote_free(kn); 715 *knp = kn0; 716 } else { 717 knp = &SLIST_NEXT(kn, kn_link); 718 } 719 kn = kn0; 720 } 721 } 722 if (fdp->fd_knhashmask != 0) { 723 for (i = 0; i < fdp->fd_knhashmask + 1; i++) { 724 knp = &SLIST_FIRST(&fdp->fd_knhash[i]); 725 kn = *knp; 726 while (kn != NULL) { 727 kn0 = SLIST_NEXT(kn, kn_link); 728 if (kq == kn->kn_kq) { 729 kn->kn_fop->f_detach(kn); 730 /* XXX non-fd release of kn->kn_ptr */ 731 knote_free(kn); 732 *knp = kn0; 733 } else { 734 knp = &SLIST_NEXT(kn, kn_link); 735 } 736 kn = kn0; 737 } 738 } 739 } 740 free(kq, M_TEMP); 741 fp->f_data = NULL; 742 743 return (0); 744 } 745 746 void 747 kqueue_wakeup(struct kqueue *kq) 748 { 749 750 if (kq->kq_state & KQ_SLEEP) { 751 kq->kq_state &= ~KQ_SLEEP; 752 wakeup(kq); 753 } 754 if (kq->kq_state & KQ_SEL) { 755 kq->kq_state &= ~KQ_SEL; 756 selwakeup(&kq->kq_sel); 757 } 758 KNOTE(&kq->kq_sel.si_note, 0); 759 } 760 761 /* 762 * walk down a list of knotes, activating them if their event has triggered. 763 */ 764 void 765 knote(struct klist *list, long hint) 766 { 767 struct knote *kn; 768 769 SLIST_FOREACH(kn, list, kn_selnext) 770 if (kn->kn_fop->f_event(kn, hint)) 771 KNOTE_ACTIVATE(kn); 772 } 773 774 /* 775 * remove all knotes from a specified klist 776 */ 777 void 778 knote_remove(struct proc *p, struct klist *list) 779 { 780 struct knote *kn; 781 782 while ((kn = SLIST_FIRST(list)) != NULL) { 783 kn->kn_fop->f_detach(kn); 784 knote_drop(kn, p); 785 } 786 } 787 788 /* 789 * remove all knotes referencing a specified fd 790 */ 791 void 792 knote_fdclose(struct proc *p, int fd) 793 { 794 struct filedesc *fdp = p->p_fd; 795 struct klist *list = &fdp->fd_knlist[fd]; 796 797 knote_remove(p, list); 798 } 799 800 void 801 knote_attach(struct knote *kn, struct filedesc *fdp) 802 { 803 struct klist *list; 804 int size; 805 806 if (! kn->kn_fop->f_isfd) { 807 if (fdp->fd_knhashmask == 0) 808 fdp->fd_knhash = hashinit(KN_HASHSIZE, M_TEMP, 809 M_WAITOK, &fdp->fd_knhashmask); 810 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)]; 811 goto done; 812 } 813 814 if (fdp->fd_knlistsize <= kn->kn_id) { 815 size = fdp->fd_knlistsize; 816 while (size <= kn->kn_id) 817 size += KQEXTENT; 818 MALLOC(list, struct klist *, 819 size * sizeof(struct klist *), M_TEMP, M_WAITOK); 820 bcopy((caddr_t)fdp->fd_knlist, (caddr_t)list, 821 fdp->fd_knlistsize * sizeof(struct klist *)); 822 bzero((caddr_t)list + 823 fdp->fd_knlistsize * sizeof(struct klist *), 824 (size - fdp->fd_knlistsize) * sizeof(struct klist *)); 825 if (fdp->fd_knlist != NULL) 826 FREE(fdp->fd_knlist, M_TEMP); 827 fdp->fd_knlistsize = size; 828 fdp->fd_knlist = list; 829 } 830 list = &fdp->fd_knlist[kn->kn_id]; 831 done: 832 SLIST_INSERT_HEAD(list, kn, kn_link); 833 kn->kn_status = 0; 834 } 835 836 /* 837 * should be called at spl == 0, since we don't want to hold spl 838 * while calling closef and free. 839 */ 840 void 841 knote_drop(struct knote *kn, struct proc *p) 842 { 843 struct filedesc *fdp = p->p_fd; 844 struct klist *list; 845 846 if (kn->kn_fop->f_isfd) 847 list = &fdp->fd_knlist[kn->kn_id]; 848 else 849 list = &fdp->fd_knhash[KN_HASH(kn->kn_id, fdp->fd_knhashmask)]; 850 851 SLIST_REMOVE(list, kn, knote, kn_link); 852 if (kn->kn_status & KN_QUEUED) 853 knote_dequeue(kn); 854 if (kn->kn_fop->f_isfd) 855 closef(kn->kn_fp, p); 856 knote_free(kn); 857 } 858 859 860 void 861 knote_enqueue(struct knote *kn) 862 { 863 struct kqueue *kq = kn->kn_kq; 864 int s = splhigh(); 865 866 KASSERT((kn->kn_status & KN_QUEUED) == 0); 867 868 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 869 kn->kn_status |= KN_QUEUED; 870 kq->kq_count++; 871 splx(s); 872 kqueue_wakeup(kq); 873 } 874 875 void 876 knote_dequeue(struct knote *kn) 877 { 878 struct kqueue *kq = kn->kn_kq; 879 int s = splhigh(); 880 881 KASSERT(kn->kn_status & KN_QUEUED); 882 883 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 884 kn->kn_status &= ~KN_QUEUED; 885 kq->kq_count--; 886 splx(s); 887 } 888 889 void 890 knote_init(void) 891 { 892 pool_init(&knote_pool, sizeof(struct knote), 0, 0, 0, "knotepl", 893 0, pool_page_alloc_nointr, pool_page_free_nointr, M_KNOTE); 894 } 895 896 struct knote * 897 knote_alloc(void) 898 { 899 static int knote_pool_initialised; 900 901 if (!knote_pool_initialised) { 902 knote_init(); 903 knote_pool_initialised++; 904 } 905 906 return (pool_get(&knote_pool, PR_WAITOK)); 907 } 908 909 void 910 knote_free(struct knote *kn) 911 { 912 pool_put(&knote_pool, kn); 913 } 914