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