1 /*- 2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org> 3 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/proc.h> 37 #include <sys/malloc.h> 38 #include <sys/unistd.h> 39 #include <sys/file.h> 40 #include <sys/filedesc.h> 41 #include <sys/filio.h> 42 #include <sys/fcntl.h> 43 #include <sys/kthread.h> 44 #include <sys/selinfo.h> 45 #include <sys/queue.h> 46 #include <sys/event.h> 47 #include <sys/eventvar.h> 48 #include <sys/poll.h> 49 #include <sys/protosw.h> 50 #include <sys/sigio.h> 51 #include <sys/signalvar.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/stat.h> 55 #include <sys/sysctl.h> 56 #include <sys/sysproto.h> 57 #include <sys/syscallsubr.h> 58 #include <sys/taskqueue.h> 59 #include <sys/uio.h> 60 61 #include <vm/uma.h> 62 63 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system"); 64 65 /* 66 * This lock is used if multiple kq locks are required. This possibly 67 * should be made into a per proc lock. 68 */ 69 static struct mtx kq_global; 70 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF); 71 #define KQ_GLOBAL_LOCK(lck, haslck) do { \ 72 if (!haslck) \ 73 mtx_lock(lck); \ 74 haslck = 1; \ 75 } while (0) 76 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \ 77 if (haslck) \ 78 mtx_unlock(lck); \ 79 haslck = 0; \ 80 } while (0) 81 82 TASKQUEUE_DEFINE_THREAD(kqueue); 83 84 static int kevent_copyout(void *arg, struct kevent *kevp, int count); 85 static int kevent_copyin(void *arg, struct kevent *kevp, int count); 86 static int kqueue_aquire(struct file *fp, struct kqueue **kqp); 87 static void kqueue_release(struct kqueue *kq, int locked); 88 static int kqueue_expand(struct kqueue *kq, struct filterops *fops, 89 uintptr_t ident, int waitok); 90 static void kqueue_task(void *arg, int pending); 91 static int kqueue_scan(struct kqueue *kq, int maxevents, 92 struct kevent_copyops *k_ops, 93 const struct timespec *timeout, 94 struct kevent *keva, struct thread *td); 95 static void kqueue_wakeup(struct kqueue *kq); 96 static struct filterops *kqueue_fo_find(int filt); 97 static void kqueue_fo_release(int filt); 98 99 static fo_rdwr_t kqueue_read; 100 static fo_rdwr_t kqueue_write; 101 static fo_ioctl_t kqueue_ioctl; 102 static fo_poll_t kqueue_poll; 103 static fo_kqfilter_t kqueue_kqfilter; 104 static fo_stat_t kqueue_stat; 105 static fo_close_t kqueue_close; 106 107 static struct fileops kqueueops = { 108 .fo_read = kqueue_read, 109 .fo_write = kqueue_write, 110 .fo_ioctl = kqueue_ioctl, 111 .fo_poll = kqueue_poll, 112 .fo_kqfilter = kqueue_kqfilter, 113 .fo_stat = kqueue_stat, 114 .fo_close = kqueue_close, 115 }; 116 117 static int knote_attach(struct knote *kn, struct kqueue *kq); 118 static void knote_drop(struct knote *kn, struct thread *td); 119 static void knote_enqueue(struct knote *kn); 120 static void knote_dequeue(struct knote *kn); 121 static void knote_init(void); 122 static struct knote *knote_alloc(int waitok); 123 static void knote_free(struct knote *kn); 124 125 static void filt_kqdetach(struct knote *kn); 126 static int filt_kqueue(struct knote *kn, long hint); 127 static int filt_procattach(struct knote *kn); 128 static void filt_procdetach(struct knote *kn); 129 static int filt_proc(struct knote *kn, long hint); 130 static int filt_fileattach(struct knote *kn); 131 static void filt_timerexpire(void *knx); 132 static int filt_timerattach(struct knote *kn); 133 static void filt_timerdetach(struct knote *kn); 134 static int filt_timer(struct knote *kn, long hint); 135 136 static struct filterops file_filtops = 137 { 1, filt_fileattach, NULL, NULL }; 138 static struct filterops kqread_filtops = 139 { 1, NULL, filt_kqdetach, filt_kqueue }; 140 /* XXX - move to kern_proc.c? */ 141 static struct filterops proc_filtops = 142 { 0, filt_procattach, filt_procdetach, filt_proc }; 143 static struct filterops timer_filtops = 144 { 0, filt_timerattach, filt_timerdetach, filt_timer }; 145 146 static uma_zone_t knote_zone; 147 static int kq_ncallouts = 0; 148 static int kq_calloutmax = (4 * 1024); 149 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW, 150 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue"); 151 152 /* XXX - ensure not KN_INFLUX?? */ 153 #define KNOTE_ACTIVATE(kn, islock) do { \ 154 if ((islock)) \ 155 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \ 156 else \ 157 KQ_LOCK((kn)->kn_kq); \ 158 (kn)->kn_status |= KN_ACTIVE; \ 159 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \ 160 knote_enqueue((kn)); \ 161 if (!(islock)) \ 162 KQ_UNLOCK((kn)->kn_kq); \ 163 } while(0) 164 #define KQ_LOCK(kq) do { \ 165 mtx_lock(&(kq)->kq_lock); \ 166 } while (0) 167 #define KQ_FLUX_WAKEUP(kq) do { \ 168 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \ 169 (kq)->kq_state &= ~KQ_FLUXWAIT; \ 170 wakeup((kq)); \ 171 } \ 172 } while (0) 173 #define KQ_UNLOCK_FLUX(kq) do { \ 174 KQ_FLUX_WAKEUP(kq); \ 175 mtx_unlock(&(kq)->kq_lock); \ 176 } while (0) 177 #define KQ_UNLOCK(kq) do { \ 178 mtx_unlock(&(kq)->kq_lock); \ 179 } while (0) 180 #define KQ_OWNED(kq) do { \ 181 mtx_assert(&(kq)->kq_lock, MA_OWNED); \ 182 } while (0) 183 #define KQ_NOTOWNED(kq) do { \ 184 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \ 185 } while (0) 186 #define KN_LIST_LOCK(kn) do { \ 187 if (kn->kn_knlist != NULL) \ 188 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \ 189 } while (0) 190 #define KN_LIST_UNLOCK(kn) do { \ 191 if (kn->kn_knlist != NULL) \ 192 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \ 193 } while (0) 194 #define KNL_ASSERT_LOCK(knl, islocked) do { \ 195 if (islocked) \ 196 KNL_ASSERT_LOCKED(knl); \ 197 else \ 198 KNL_ASSERT_UNLOCKED(knl); \ 199 } while (0) 200 #ifdef INVARIANTS 201 #define KNL_ASSERT_LOCKED(knl) do { \ 202 if (!knl->kl_locked((knl)->kl_lockarg)) \ 203 panic("knlist not locked, but should be"); \ 204 } while (0) 205 #define KNL_ASSERT_UNLOCKED(knl) do { \ 206 if (knl->kl_locked((knl)->kl_lockarg)) \ 207 panic("knlist locked, but should not be"); \ 208 } while (0) 209 #else /* !INVARIANTS */ 210 #define KNL_ASSERT_LOCKED(knl) do {} while(0) 211 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0) 212 #endif /* INVARIANTS */ 213 214 #define KN_HASHSIZE 64 /* XXX should be tunable */ 215 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask)) 216 217 static int 218 filt_nullattach(struct knote *kn) 219 { 220 221 return (ENXIO); 222 }; 223 224 struct filterops null_filtops = 225 { 0, filt_nullattach, NULL, NULL }; 226 227 /* XXX - make SYSINIT to add these, and move into respective modules. */ 228 extern struct filterops sig_filtops; 229 extern struct filterops fs_filtops; 230 231 /* 232 * Table for for all system-defined filters. 233 */ 234 static struct mtx filterops_lock; 235 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops", 236 MTX_DEF); 237 static struct { 238 struct filterops *for_fop; 239 int for_refcnt; 240 } sysfilt_ops[EVFILT_SYSCOUNT] = { 241 { &file_filtops }, /* EVFILT_READ */ 242 { &file_filtops }, /* EVFILT_WRITE */ 243 { &null_filtops }, /* EVFILT_AIO */ 244 { &file_filtops }, /* EVFILT_VNODE */ 245 { &proc_filtops }, /* EVFILT_PROC */ 246 { &sig_filtops }, /* EVFILT_SIGNAL */ 247 { &timer_filtops }, /* EVFILT_TIMER */ 248 { &file_filtops }, /* EVFILT_NETDEV */ 249 { &fs_filtops }, /* EVFILT_FS */ 250 { &null_filtops }, /* EVFILT_LIO */ 251 }; 252 253 /* 254 * Simple redirection for all cdevsw style objects to call their fo_kqfilter 255 * method. 256 */ 257 static int 258 filt_fileattach(struct knote *kn) 259 { 260 261 return (fo_kqfilter(kn->kn_fp, kn)); 262 } 263 264 /*ARGSUSED*/ 265 static int 266 kqueue_kqfilter(struct file *fp, struct knote *kn) 267 { 268 struct kqueue *kq = kn->kn_fp->f_data; 269 270 if (kn->kn_filter != EVFILT_READ) 271 return (EINVAL); 272 273 kn->kn_status |= KN_KQUEUE; 274 kn->kn_fop = &kqread_filtops; 275 knlist_add(&kq->kq_sel.si_note, kn, 0); 276 277 return (0); 278 } 279 280 static void 281 filt_kqdetach(struct knote *kn) 282 { 283 struct kqueue *kq = kn->kn_fp->f_data; 284 285 knlist_remove(&kq->kq_sel.si_note, kn, 0); 286 } 287 288 /*ARGSUSED*/ 289 static int 290 filt_kqueue(struct knote *kn, long hint) 291 { 292 struct kqueue *kq = kn->kn_fp->f_data; 293 294 kn->kn_data = kq->kq_count; 295 return (kn->kn_data > 0); 296 } 297 298 /* XXX - move to kern_proc.c? */ 299 static int 300 filt_procattach(struct knote *kn) 301 { 302 struct proc *p; 303 int immediate; 304 int error; 305 306 immediate = 0; 307 p = pfind(kn->kn_id); 308 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) { 309 p = zpfind(kn->kn_id); 310 immediate = 1; 311 } else if (p != NULL && (p->p_flag & P_WEXIT)) { 312 immediate = 1; 313 } 314 315 if (p == NULL) 316 return (ESRCH); 317 if ((error = p_cansee(curthread, p))) 318 return (error); 319 320 kn->kn_ptr.p_proc = p; 321 kn->kn_flags |= EV_CLEAR; /* automatically set */ 322 323 /* 324 * internal flag indicating registration done by kernel 325 */ 326 if (kn->kn_flags & EV_FLAG1) { 327 kn->kn_data = kn->kn_sdata; /* ppid */ 328 kn->kn_fflags = NOTE_CHILD; 329 kn->kn_flags &= ~EV_FLAG1; 330 } 331 332 if (immediate == 0) 333 knlist_add(&p->p_klist, kn, 1); 334 335 /* 336 * Immediately activate any exit notes if the target process is a 337 * zombie. This is necessary to handle the case where the target 338 * process, e.g. a child, dies before the kevent is registered. 339 */ 340 if (immediate && filt_proc(kn, NOTE_EXIT)) 341 KNOTE_ACTIVATE(kn, 0); 342 343 PROC_UNLOCK(p); 344 345 return (0); 346 } 347 348 /* 349 * The knote may be attached to a different process, which may exit, 350 * leaving nothing for the knote to be attached to. So when the process 351 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so 352 * it will be deleted when read out. However, as part of the knote deletion, 353 * this routine is called, so a check is needed to avoid actually performing 354 * a detach, because the original process does not exist any more. 355 */ 356 /* XXX - move to kern_proc.c? */ 357 static void 358 filt_procdetach(struct knote *kn) 359 { 360 struct proc *p; 361 362 p = kn->kn_ptr.p_proc; 363 knlist_remove(&p->p_klist, kn, 0); 364 kn->kn_ptr.p_proc = NULL; 365 } 366 367 /* XXX - move to kern_proc.c? */ 368 static int 369 filt_proc(struct knote *kn, long hint) 370 { 371 struct proc *p = kn->kn_ptr.p_proc; 372 u_int event; 373 374 /* 375 * mask off extra data 376 */ 377 event = (u_int)hint & NOTE_PCTRLMASK; 378 379 /* 380 * if the user is interested in this event, record it. 381 */ 382 if (kn->kn_sfflags & event) 383 kn->kn_fflags |= event; 384 385 /* 386 * process is gone, so flag the event as finished. 387 */ 388 if (event == NOTE_EXIT) { 389 if (!(kn->kn_status & KN_DETACHED)) 390 knlist_remove_inevent(&p->p_klist, kn); 391 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 392 kn->kn_ptr.p_proc = NULL; 393 return (1); 394 } 395 396 /* 397 * process forked, and user wants to track the new process, 398 * so attach a new knote to it, and immediately report an 399 * event with the parent's pid. 400 */ 401 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) { 402 struct kevent kev; 403 int error; 404 405 /* 406 * register knote with new process. 407 */ 408 kev.ident = hint & NOTE_PDATAMASK; /* pid */ 409 kev.filter = kn->kn_filter; 410 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1; 411 kev.fflags = kn->kn_sfflags; 412 kev.data = kn->kn_id; /* parent */ 413 kev.udata = kn->kn_kevent.udata; /* preserve udata */ 414 error = kqueue_register(kn->kn_kq, &kev, NULL, 0); 415 if (error) 416 kn->kn_fflags |= NOTE_TRACKERR; 417 } 418 419 return (kn->kn_fflags != 0); 420 } 421 422 static int 423 timertoticks(intptr_t data) 424 { 425 struct timeval tv; 426 int tticks; 427 428 tv.tv_sec = data / 1000; 429 tv.tv_usec = (data % 1000) * 1000; 430 tticks = tvtohz(&tv); 431 432 return tticks; 433 } 434 435 /* XXX - move to kern_timeout.c? */ 436 static void 437 filt_timerexpire(void *knx) 438 { 439 struct knote *kn = knx; 440 struct callout *calloutp; 441 442 kn->kn_data++; 443 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */ 444 445 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) { 446 calloutp = (struct callout *)kn->kn_hook; 447 callout_reset(calloutp, timertoticks(kn->kn_sdata), 448 filt_timerexpire, kn); 449 } 450 } 451 452 /* 453 * data contains amount of time to sleep, in milliseconds 454 */ 455 /* XXX - move to kern_timeout.c? */ 456 static int 457 filt_timerattach(struct knote *kn) 458 { 459 struct callout *calloutp; 460 461 atomic_add_int(&kq_ncallouts, 1); 462 463 if (kq_ncallouts >= kq_calloutmax) { 464 atomic_add_int(&kq_ncallouts, -1); 465 return (ENOMEM); 466 } 467 468 kn->kn_flags |= EV_CLEAR; /* automatically set */ 469 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */ 470 MALLOC(calloutp, struct callout *, sizeof(*calloutp), 471 M_KQUEUE, M_WAITOK); 472 callout_init(calloutp, CALLOUT_MPSAFE); 473 kn->kn_hook = calloutp; 474 callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire, 475 kn); 476 477 return (0); 478 } 479 480 /* XXX - move to kern_timeout.c? */ 481 static void 482 filt_timerdetach(struct knote *kn) 483 { 484 struct callout *calloutp; 485 486 calloutp = (struct callout *)kn->kn_hook; 487 callout_drain(calloutp); 488 FREE(calloutp, M_KQUEUE); 489 atomic_add_int(&kq_ncallouts, -1); 490 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */ 491 } 492 493 /* XXX - move to kern_timeout.c? */ 494 static int 495 filt_timer(struct knote *kn, long hint) 496 { 497 498 return (kn->kn_data != 0); 499 } 500 501 /* 502 * MPSAFE 503 */ 504 int 505 kqueue(struct thread *td, struct kqueue_args *uap) 506 { 507 struct filedesc *fdp; 508 struct kqueue *kq; 509 struct file *fp; 510 int fd, error; 511 512 fdp = td->td_proc->p_fd; 513 error = falloc(td, &fp, &fd); 514 if (error) 515 goto done2; 516 517 /* An extra reference on `nfp' has been held for us by falloc(). */ 518 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO); 519 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK); 520 TAILQ_INIT(&kq->kq_head); 521 kq->kq_fdp = fdp; 522 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL); 523 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq); 524 525 FILEDESC_LOCK_FAST(fdp); 526 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list); 527 FILEDESC_UNLOCK_FAST(fdp); 528 529 FILE_LOCK(fp); 530 fp->f_flag = FREAD | FWRITE; 531 fp->f_type = DTYPE_KQUEUE; 532 fp->f_ops = &kqueueops; 533 fp->f_data = kq; 534 FILE_UNLOCK(fp); 535 fdrop(fp, td); 536 537 td->td_retval[0] = fd; 538 done2: 539 return (error); 540 } 541 542 #ifndef _SYS_SYSPROTO_H_ 543 struct kevent_args { 544 int fd; 545 const struct kevent *changelist; 546 int nchanges; 547 struct kevent *eventlist; 548 int nevents; 549 const struct timespec *timeout; 550 }; 551 #endif 552 /* 553 * MPSAFE 554 */ 555 int 556 kevent(struct thread *td, struct kevent_args *uap) 557 { 558 struct timespec ts, *tsp; 559 struct kevent_copyops k_ops = { uap, 560 kevent_copyout, 561 kevent_copyin}; 562 int error; 563 564 if (uap->timeout != NULL) { 565 error = copyin(uap->timeout, &ts, sizeof(ts)); 566 if (error) 567 return (error); 568 tsp = &ts; 569 } else 570 tsp = NULL; 571 572 return (kern_kevent(td, uap->fd, uap->nchanges, uap->nevents, 573 &k_ops, tsp)); 574 } 575 576 /* 577 * Copy 'count' items into the destination list pointed to by uap->eventlist. 578 */ 579 static int 580 kevent_copyout(void *arg, struct kevent *kevp, int count) 581 { 582 struct kevent_args *uap; 583 int error; 584 585 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count)); 586 uap = (struct kevent_args *)arg; 587 588 error = copyout(kevp, uap->eventlist, count * sizeof *kevp); 589 if (error == 0) 590 uap->eventlist += count; 591 return (error); 592 } 593 594 /* 595 * Copy 'count' items from the list pointed to by uap->changelist. 596 */ 597 static int 598 kevent_copyin(void *arg, struct kevent *kevp, int count) 599 { 600 struct kevent_args *uap; 601 int error; 602 603 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count)); 604 uap = (struct kevent_args *)arg; 605 606 error = copyin(uap->changelist, kevp, count * sizeof *kevp); 607 if (error == 0) 608 uap->changelist += count; 609 return (error); 610 } 611 612 int 613 kern_kevent(struct thread *td, int fd, int nchanges, int nevents, 614 struct kevent_copyops *k_ops, const struct timespec *timeout) 615 { 616 struct kevent keva[KQ_NEVENTS]; 617 struct kevent *kevp, *changes; 618 struct kqueue *kq; 619 struct file *fp; 620 int i, n, nerrors, error; 621 622 if ((error = fget(td, fd, &fp)) != 0) 623 return (error); 624 if ((error = kqueue_aquire(fp, &kq)) != 0) 625 goto done_norel; 626 627 nerrors = 0; 628 629 while (nchanges > 0) { 630 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges; 631 error = k_ops->k_copyin(k_ops->arg, keva, n); 632 if (error) 633 goto done; 634 changes = keva; 635 for (i = 0; i < n; i++) { 636 kevp = &changes[i]; 637 if (!kevp->filter) 638 continue; 639 kevp->flags &= ~EV_SYSFLAGS; 640 error = kqueue_register(kq, kevp, td, 1); 641 if (error) { 642 if (nevents != 0) { 643 kevp->flags = EV_ERROR; 644 kevp->data = error; 645 (void) k_ops->k_copyout(k_ops->arg, 646 kevp, 1); 647 nevents--; 648 nerrors++; 649 } else { 650 goto done; 651 } 652 } 653 } 654 nchanges -= n; 655 } 656 if (nerrors) { 657 td->td_retval[0] = nerrors; 658 error = 0; 659 goto done; 660 } 661 662 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td); 663 done: 664 kqueue_release(kq, 0); 665 done_norel: 666 if (fp != NULL) 667 fdrop(fp, td); 668 return (error); 669 } 670 671 int 672 kqueue_add_filteropts(int filt, struct filterops *filtops) 673 { 674 int error; 675 676 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) { 677 printf( 678 "trying to add a filterop that is out of range: %d is beyond %d\n", 679 ~filt, EVFILT_SYSCOUNT); 680 return EINVAL; 681 } 682 mtx_lock(&filterops_lock); 683 if (sysfilt_ops[~filt].for_fop != &null_filtops && 684 sysfilt_ops[~filt].for_fop != NULL) 685 error = EEXIST; 686 else { 687 sysfilt_ops[~filt].for_fop = filtops; 688 sysfilt_ops[~filt].for_refcnt = 0; 689 } 690 mtx_unlock(&filterops_lock); 691 692 return (0); 693 } 694 695 int 696 kqueue_del_filteropts(int filt) 697 { 698 int error; 699 700 error = 0; 701 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) 702 return EINVAL; 703 704 mtx_lock(&filterops_lock); 705 if (sysfilt_ops[~filt].for_fop == &null_filtops || 706 sysfilt_ops[~filt].for_fop == NULL) 707 error = EINVAL; 708 else if (sysfilt_ops[~filt].for_refcnt != 0) 709 error = EBUSY; 710 else { 711 sysfilt_ops[~filt].for_fop = &null_filtops; 712 sysfilt_ops[~filt].for_refcnt = 0; 713 } 714 mtx_unlock(&filterops_lock); 715 716 return error; 717 } 718 719 static struct filterops * 720 kqueue_fo_find(int filt) 721 { 722 723 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) 724 return NULL; 725 726 mtx_lock(&filterops_lock); 727 sysfilt_ops[~filt].for_refcnt++; 728 if (sysfilt_ops[~filt].for_fop == NULL) 729 sysfilt_ops[~filt].for_fop = &null_filtops; 730 mtx_unlock(&filterops_lock); 731 732 return sysfilt_ops[~filt].for_fop; 733 } 734 735 static void 736 kqueue_fo_release(int filt) 737 { 738 739 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) 740 return; 741 742 mtx_lock(&filterops_lock); 743 KASSERT(sysfilt_ops[~filt].for_refcnt > 0, 744 ("filter object refcount not valid on release")); 745 sysfilt_ops[~filt].for_refcnt--; 746 mtx_unlock(&filterops_lock); 747 } 748 749 /* 750 * A ref to kq (obtained via kqueue_aquire) should be held. waitok will 751 * influence if memory allocation should wait. Make sure it is 0 if you 752 * hold any mutexes. 753 */ 754 int 755 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok) 756 { 757 struct filedesc *fdp; 758 struct filterops *fops; 759 struct file *fp; 760 struct knote *kn, *tkn; 761 int error, filt, event; 762 int haskqglobal; 763 int fd; 764 765 fdp = NULL; 766 fp = NULL; 767 kn = NULL; 768 error = 0; 769 haskqglobal = 0; 770 771 filt = kev->filter; 772 fops = kqueue_fo_find(filt); 773 if (fops == NULL) 774 return EINVAL; 775 776 tkn = knote_alloc(waitok); /* prevent waiting with locks */ 777 778 findkn: 779 if (fops->f_isfd) { 780 KASSERT(td != NULL, ("td is NULL")); 781 fdp = td->td_proc->p_fd; 782 FILEDESC_LOCK(fdp); 783 /* validate descriptor */ 784 fd = kev->ident; 785 if (fd < 0 || fd >= fdp->fd_nfiles || 786 (fp = fdp->fd_ofiles[fd]) == NULL) { 787 FILEDESC_UNLOCK(fdp); 788 error = EBADF; 789 goto done; 790 } 791 fhold(fp); 792 793 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops, 794 kev->ident, 0) != 0) { 795 /* unlock and try again */ 796 FILEDESC_UNLOCK(fdp); 797 fdrop(fp, td); 798 fp = NULL; 799 error = kqueue_expand(kq, fops, kev->ident, waitok); 800 if (error) 801 goto done; 802 goto findkn; 803 } 804 805 if (fp->f_type == DTYPE_KQUEUE) { 806 /* 807 * if we add some inteligence about what we are doing, 808 * we should be able to support events on ourselves. 809 * We need to know when we are doing this to prevent 810 * getting both the knlist lock and the kq lock since 811 * they are the same thing. 812 */ 813 if (fp->f_data == kq) { 814 FILEDESC_UNLOCK(fdp); 815 error = EINVAL; 816 goto done_noglobal; 817 } 818 819 KQ_GLOBAL_LOCK(&kq_global, haskqglobal); 820 } 821 822 FILEDESC_UNLOCK(fdp); 823 KQ_LOCK(kq); 824 if (kev->ident < kq->kq_knlistsize) { 825 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link) 826 if (kev->filter == kn->kn_filter) 827 break; 828 } 829 } else { 830 if ((kev->flags & EV_ADD) == EV_ADD) 831 kqueue_expand(kq, fops, kev->ident, waitok); 832 833 KQ_LOCK(kq); 834 if (kq->kq_knhashmask != 0) { 835 struct klist *list; 836 837 list = &kq->kq_knhash[ 838 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)]; 839 SLIST_FOREACH(kn, list, kn_link) 840 if (kev->ident == kn->kn_id && 841 kev->filter == kn->kn_filter) 842 break; 843 } 844 } 845 846 /* knote is in the process of changing, wait for it to stablize. */ 847 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) { 848 if (fp != NULL) { 849 fdrop(fp, td); 850 fp = NULL; 851 } 852 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 853 kq->kq_state |= KQ_FLUXWAIT; 854 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0); 855 goto findkn; 856 } 857 858 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) { 859 KQ_UNLOCK(kq); 860 error = ENOENT; 861 goto done; 862 } 863 864 /* 865 * kn now contains the matching knote, or NULL if no match 866 */ 867 if (kev->flags & EV_ADD) { 868 if (kn == NULL) { 869 kn = tkn; 870 tkn = NULL; 871 if (kn == NULL) { 872 error = ENOMEM; 873 goto done; 874 } 875 kn->kn_fp = fp; 876 kn->kn_kq = kq; 877 kn->kn_fop = fops; 878 /* 879 * apply reference counts to knote structure, and 880 * do not release it at the end of this routine. 881 */ 882 fops = NULL; 883 fp = NULL; 884 885 kn->kn_sfflags = kev->fflags; 886 kn->kn_sdata = kev->data; 887 kev->fflags = 0; 888 kev->data = 0; 889 kn->kn_kevent = *kev; 890 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE | 891 EV_ENABLE | EV_DISABLE); 892 kn->kn_status = KN_INFLUX|KN_DETACHED; 893 894 error = knote_attach(kn, kq); 895 KQ_UNLOCK(kq); 896 if (error != 0) { 897 tkn = kn; 898 goto done; 899 } 900 901 if ((error = kn->kn_fop->f_attach(kn)) != 0) { 902 knote_drop(kn, td); 903 goto done; 904 } 905 KN_LIST_LOCK(kn); 906 } else { 907 /* 908 * The user may change some filter values after the 909 * initial EV_ADD, but doing so will not reset any 910 * filter which has already been triggered. 911 */ 912 kn->kn_status |= KN_INFLUX; 913 KQ_UNLOCK(kq); 914 KN_LIST_LOCK(kn); 915 kn->kn_sfflags = kev->fflags; 916 kn->kn_sdata = kev->data; 917 kn->kn_kevent.udata = kev->udata; 918 } 919 920 /* 921 * We can get here with kn->kn_knlist == NULL. 922 * This can happen when the initial attach event decides that 923 * the event is "completed" already. i.e. filt_procattach 924 * is called on a zombie process. It will call filt_proc 925 * which will remove it from the list, and NULL kn_knlist. 926 */ 927 event = kn->kn_fop->f_event(kn, 0); 928 KQ_LOCK(kq); 929 if (event) 930 KNOTE_ACTIVATE(kn, 1); 931 kn->kn_status &= ~KN_INFLUX; 932 KN_LIST_UNLOCK(kn); 933 } else if (kev->flags & EV_DELETE) { 934 kn->kn_status |= KN_INFLUX; 935 KQ_UNLOCK(kq); 936 if (!(kn->kn_status & KN_DETACHED)) 937 kn->kn_fop->f_detach(kn); 938 knote_drop(kn, td); 939 goto done; 940 } 941 942 if ((kev->flags & EV_DISABLE) && 943 ((kn->kn_status & KN_DISABLED) == 0)) { 944 kn->kn_status |= KN_DISABLED; 945 } 946 947 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) { 948 kn->kn_status &= ~KN_DISABLED; 949 if ((kn->kn_status & KN_ACTIVE) && 950 ((kn->kn_status & KN_QUEUED) == 0)) 951 knote_enqueue(kn); 952 } 953 KQ_UNLOCK_FLUX(kq); 954 955 done: 956 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 957 done_noglobal: 958 if (fp != NULL) 959 fdrop(fp, td); 960 if (tkn != NULL) 961 knote_free(tkn); 962 if (fops != NULL) 963 kqueue_fo_release(filt); 964 return (error); 965 } 966 967 static int 968 kqueue_aquire(struct file *fp, struct kqueue **kqp) 969 { 970 int error; 971 struct kqueue *kq; 972 973 error = 0; 974 975 FILE_LOCK(fp); 976 do { 977 kq = fp->f_data; 978 if (fp->f_type != DTYPE_KQUEUE || kq == NULL) { 979 error = EBADF; 980 break; 981 } 982 *kqp = kq; 983 KQ_LOCK(kq); 984 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) { 985 KQ_UNLOCK(kq); 986 error = EBADF; 987 break; 988 } 989 kq->kq_refcnt++; 990 KQ_UNLOCK(kq); 991 } while (0); 992 FILE_UNLOCK(fp); 993 994 return error; 995 } 996 997 static void 998 kqueue_release(struct kqueue *kq, int locked) 999 { 1000 if (locked) 1001 KQ_OWNED(kq); 1002 else 1003 KQ_LOCK(kq); 1004 kq->kq_refcnt--; 1005 if (kq->kq_refcnt == 1) 1006 wakeup(&kq->kq_refcnt); 1007 if (!locked) 1008 KQ_UNLOCK(kq); 1009 } 1010 1011 static void 1012 kqueue_schedtask(struct kqueue *kq) 1013 { 1014 1015 KQ_OWNED(kq); 1016 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN), 1017 ("scheduling kqueue task while draining")); 1018 1019 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) { 1020 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task); 1021 kq->kq_state |= KQ_TASKSCHED; 1022 } 1023 } 1024 1025 /* 1026 * Expand the kq to make sure we have storage for fops/ident pair. 1027 * 1028 * Return 0 on success (or no work necessary), return errno on failure. 1029 * 1030 * Not calling hashinit w/ waitok (proper malloc flag) should be safe. 1031 * If kqueue_register is called from a non-fd context, there usually/should 1032 * be no locks held. 1033 */ 1034 static int 1035 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident, 1036 int waitok) 1037 { 1038 struct klist *list, *tmp_knhash; 1039 u_long tmp_knhashmask; 1040 int size; 1041 int fd; 1042 int mflag = waitok ? M_WAITOK : M_NOWAIT; 1043 1044 KQ_NOTOWNED(kq); 1045 1046 if (fops->f_isfd) { 1047 fd = ident; 1048 if (kq->kq_knlistsize <= fd) { 1049 size = kq->kq_knlistsize; 1050 while (size <= fd) 1051 size += KQEXTENT; 1052 MALLOC(list, struct klist *, 1053 size * sizeof list, M_KQUEUE, mflag); 1054 if (list == NULL) 1055 return ENOMEM; 1056 KQ_LOCK(kq); 1057 if (kq->kq_knlistsize > fd) { 1058 FREE(list, M_KQUEUE); 1059 list = NULL; 1060 } else { 1061 if (kq->kq_knlist != NULL) { 1062 bcopy(kq->kq_knlist, list, 1063 kq->kq_knlistsize * sizeof list); 1064 FREE(kq->kq_knlist, M_KQUEUE); 1065 kq->kq_knlist = NULL; 1066 } 1067 bzero((caddr_t)list + 1068 kq->kq_knlistsize * sizeof list, 1069 (size - kq->kq_knlistsize) * sizeof list); 1070 kq->kq_knlistsize = size; 1071 kq->kq_knlist = list; 1072 } 1073 KQ_UNLOCK(kq); 1074 } 1075 } else { 1076 if (kq->kq_knhashmask == 0) { 1077 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE, 1078 &tmp_knhashmask); 1079 if (tmp_knhash == NULL) 1080 return ENOMEM; 1081 KQ_LOCK(kq); 1082 if (kq->kq_knhashmask == 0) { 1083 kq->kq_knhash = tmp_knhash; 1084 kq->kq_knhashmask = tmp_knhashmask; 1085 } else { 1086 free(tmp_knhash, M_KQUEUE); 1087 } 1088 KQ_UNLOCK(kq); 1089 } 1090 } 1091 1092 KQ_NOTOWNED(kq); 1093 return 0; 1094 } 1095 1096 static void 1097 kqueue_task(void *arg, int pending) 1098 { 1099 struct kqueue *kq; 1100 int haskqglobal; 1101 1102 haskqglobal = 0; 1103 kq = arg; 1104 1105 KQ_GLOBAL_LOCK(&kq_global, haskqglobal); 1106 KQ_LOCK(kq); 1107 1108 KNOTE_LOCKED(&kq->kq_sel.si_note, 0); 1109 1110 kq->kq_state &= ~KQ_TASKSCHED; 1111 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) { 1112 wakeup(&kq->kq_state); 1113 } 1114 KQ_UNLOCK(kq); 1115 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 1116 } 1117 1118 /* 1119 * Scan, update kn_data (if not ONESHOT), and copyout triggered events. 1120 * We treat KN_MARKER knotes as if they are INFLUX. 1121 */ 1122 static int 1123 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops, 1124 const struct timespec *tsp, struct kevent *keva, struct thread *td) 1125 { 1126 struct kevent *kevp; 1127 struct timeval atv, rtv, ttv; 1128 struct knote *kn, *marker; 1129 int count, timeout, nkev, error; 1130 int haskqglobal; 1131 1132 count = maxevents; 1133 nkev = 0; 1134 error = 0; 1135 haskqglobal = 0; 1136 1137 if (maxevents == 0) 1138 goto done_nl; 1139 1140 if (tsp != NULL) { 1141 TIMESPEC_TO_TIMEVAL(&atv, tsp); 1142 if (itimerfix(&atv)) { 1143 error = EINVAL; 1144 goto done_nl; 1145 } 1146 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1147 timeout = -1; 1148 else 1149 timeout = atv.tv_sec > 24 * 60 * 60 ? 1150 24 * 60 * 60 * hz : tvtohz(&atv); 1151 getmicrouptime(&rtv); 1152 timevaladd(&atv, &rtv); 1153 } else { 1154 atv.tv_sec = 0; 1155 atv.tv_usec = 0; 1156 timeout = 0; 1157 } 1158 marker = knote_alloc(1); 1159 if (marker == NULL) { 1160 error = ENOMEM; 1161 goto done_nl; 1162 } 1163 marker->kn_status = KN_MARKER; 1164 KQ_LOCK(kq); 1165 goto start; 1166 1167 retry: 1168 if (atv.tv_sec || atv.tv_usec) { 1169 getmicrouptime(&rtv); 1170 if (timevalcmp(&rtv, &atv, >=)) 1171 goto done; 1172 ttv = atv; 1173 timevalsub(&ttv, &rtv); 1174 timeout = ttv.tv_sec > 24 * 60 * 60 ? 1175 24 * 60 * 60 * hz : tvtohz(&ttv); 1176 } 1177 1178 start: 1179 kevp = keva; 1180 if (kq->kq_count == 0) { 1181 if (timeout < 0) { 1182 error = EWOULDBLOCK; 1183 } else { 1184 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 1185 kq->kq_state |= KQ_SLEEP; 1186 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH, 1187 "kqread", timeout); 1188 } 1189 if (error == 0) 1190 goto retry; 1191 /* don't restart after signals... */ 1192 if (error == ERESTART) 1193 error = EINTR; 1194 else if (error == EWOULDBLOCK) 1195 error = 0; 1196 goto done; 1197 } 1198 1199 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe); 1200 while (count) { 1201 KQ_OWNED(kq); 1202 kn = TAILQ_FIRST(&kq->kq_head); 1203 1204 if ((kn->kn_status == KN_MARKER && kn != marker) || 1205 (kn->kn_status & KN_INFLUX) == KN_INFLUX) { 1206 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 1207 kq->kq_state |= KQ_FLUXWAIT; 1208 error = msleep(kq, &kq->kq_lock, PSOCK, 1209 "kqflxwt", 0); 1210 continue; 1211 } 1212 1213 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 1214 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) { 1215 kn->kn_status &= ~KN_QUEUED; 1216 kq->kq_count--; 1217 continue; 1218 } 1219 if (kn == marker) { 1220 KQ_FLUX_WAKEUP(kq); 1221 if (count == maxevents) 1222 goto retry; 1223 goto done; 1224 } 1225 KASSERT((kn->kn_status & KN_INFLUX) == 0, 1226 ("KN_INFLUX set when not suppose to be")); 1227 1228 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) { 1229 kn->kn_status &= ~KN_QUEUED; 1230 kn->kn_status |= KN_INFLUX; 1231 kq->kq_count--; 1232 KQ_UNLOCK(kq); 1233 /* 1234 * We don't need to lock the list since we've marked 1235 * it _INFLUX. 1236 */ 1237 *kevp = kn->kn_kevent; 1238 if (!(kn->kn_status & KN_DETACHED)) 1239 kn->kn_fop->f_detach(kn); 1240 knote_drop(kn, td); 1241 KQ_LOCK(kq); 1242 kn = NULL; 1243 } else { 1244 kn->kn_status |= KN_INFLUX; 1245 KQ_UNLOCK(kq); 1246 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE) 1247 KQ_GLOBAL_LOCK(&kq_global, haskqglobal); 1248 KN_LIST_LOCK(kn); 1249 if (kn->kn_fop->f_event(kn, 0) == 0) { 1250 KQ_LOCK(kq); 1251 kn->kn_status &= 1252 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX); 1253 kq->kq_count--; 1254 KN_LIST_UNLOCK(kn); 1255 continue; 1256 } 1257 *kevp = kn->kn_kevent; 1258 KQ_LOCK(kq); 1259 if (kn->kn_flags & EV_CLEAR) { 1260 kn->kn_data = 0; 1261 kn->kn_fflags = 0; 1262 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE); 1263 kq->kq_count--; 1264 } else 1265 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 1266 1267 kn->kn_status &= ~(KN_INFLUX); 1268 KN_LIST_UNLOCK(kn); 1269 1270 } 1271 1272 /* we are returning a copy to the user */ 1273 kevp++; 1274 nkev++; 1275 count--; 1276 1277 if (nkev == KQ_NEVENTS) { 1278 KQ_UNLOCK_FLUX(kq); 1279 error = k_ops->k_copyout(k_ops->arg, keva, nkev); 1280 nkev = 0; 1281 kevp = keva; 1282 KQ_LOCK(kq); 1283 if (error) 1284 break; 1285 } 1286 } 1287 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe); 1288 done: 1289 KQ_OWNED(kq); 1290 KQ_UNLOCK_FLUX(kq); 1291 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 1292 knote_free(marker); 1293 done_nl: 1294 KQ_NOTOWNED(kq); 1295 if (nkev != 0) 1296 error = k_ops->k_copyout(k_ops->arg, keva, nkev); 1297 td->td_retval[0] = maxevents - count; 1298 return (error); 1299 } 1300 1301 /* 1302 * XXX 1303 * This could be expanded to call kqueue_scan, if desired. 1304 */ 1305 /*ARGSUSED*/ 1306 static int 1307 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 1308 int flags, struct thread *td) 1309 { 1310 return (ENXIO); 1311 } 1312 1313 /*ARGSUSED*/ 1314 static int 1315 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 1316 int flags, struct thread *td) 1317 { 1318 return (ENXIO); 1319 } 1320 1321 /*ARGSUSED*/ 1322 static int 1323 kqueue_ioctl(struct file *fp, u_long cmd, void *data, 1324 struct ucred *active_cred, struct thread *td) 1325 { 1326 /* 1327 * Enabling sigio causes two major problems: 1328 * 1) infinite recursion: 1329 * Synopsys: kevent is being used to track signals and have FIOASYNC 1330 * set. On receipt of a signal this will cause a kqueue to recurse 1331 * into itself over and over. Sending the sigio causes the kqueue 1332 * to become ready, which in turn posts sigio again, forever. 1333 * Solution: this can be solved by setting a flag in the kqueue that 1334 * we have a SIGIO in progress. 1335 * 2) locking problems: 1336 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts 1337 * us above the proc and pgrp locks. 1338 * Solution: Post a signal using an async mechanism, being sure to 1339 * record a generation count in the delivery so that we do not deliver 1340 * a signal to the wrong process. 1341 * 1342 * Note, these two mechanisms are somewhat mutually exclusive! 1343 */ 1344 #if 0 1345 struct kqueue *kq; 1346 1347 kq = fp->f_data; 1348 switch (cmd) { 1349 case FIOASYNC: 1350 if (*(int *)data) { 1351 kq->kq_state |= KQ_ASYNC; 1352 } else { 1353 kq->kq_state &= ~KQ_ASYNC; 1354 } 1355 return (0); 1356 1357 case FIOSETOWN: 1358 return (fsetown(*(int *)data, &kq->kq_sigio)); 1359 1360 case FIOGETOWN: 1361 *(int *)data = fgetown(&kq->kq_sigio); 1362 return (0); 1363 } 1364 #endif 1365 1366 return (ENOTTY); 1367 } 1368 1369 /*ARGSUSED*/ 1370 static int 1371 kqueue_poll(struct file *fp, int events, struct ucred *active_cred, 1372 struct thread *td) 1373 { 1374 struct kqueue *kq; 1375 int revents = 0; 1376 int error; 1377 1378 if ((error = kqueue_aquire(fp, &kq))) 1379 return POLLERR; 1380 1381 KQ_LOCK(kq); 1382 if (events & (POLLIN | POLLRDNORM)) { 1383 if (kq->kq_count) { 1384 revents |= events & (POLLIN | POLLRDNORM); 1385 } else { 1386 selrecord(td, &kq->kq_sel); 1387 kq->kq_state |= KQ_SEL; 1388 } 1389 } 1390 kqueue_release(kq, 1); 1391 KQ_UNLOCK(kq); 1392 return (revents); 1393 } 1394 1395 /*ARGSUSED*/ 1396 static int 1397 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred, 1398 struct thread *td) 1399 { 1400 1401 bzero((void *)st, sizeof *st); 1402 /* 1403 * We no longer return kq_count because the unlocked value is useless. 1404 * If you spent all this time getting the count, why not spend your 1405 * syscall better by calling kevent? 1406 * 1407 * XXX - This is needed for libc_r. 1408 */ 1409 st->st_mode = S_IFIFO; 1410 return (0); 1411 } 1412 1413 /*ARGSUSED*/ 1414 static int 1415 kqueue_close(struct file *fp, struct thread *td) 1416 { 1417 struct kqueue *kq = fp->f_data; 1418 struct filedesc *fdp; 1419 struct knote *kn; 1420 int i; 1421 int error; 1422 1423 if ((error = kqueue_aquire(fp, &kq))) 1424 return error; 1425 1426 KQ_LOCK(kq); 1427 1428 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING, 1429 ("kqueue already closing")); 1430 kq->kq_state |= KQ_CLOSING; 1431 if (kq->kq_refcnt > 1) 1432 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0); 1433 1434 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!")); 1435 fdp = kq->kq_fdp; 1436 1437 KASSERT(knlist_empty(&kq->kq_sel.si_note), 1438 ("kqueue's knlist not empty")); 1439 1440 for (i = 0; i < kq->kq_knlistsize; i++) { 1441 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) { 1442 KASSERT((kn->kn_status & KN_INFLUX) == 0, 1443 ("KN_INFLUX set when not suppose to be")); 1444 kn->kn_status |= KN_INFLUX; 1445 KQ_UNLOCK(kq); 1446 if (!(kn->kn_status & KN_DETACHED)) 1447 kn->kn_fop->f_detach(kn); 1448 knote_drop(kn, td); 1449 KQ_LOCK(kq); 1450 } 1451 } 1452 if (kq->kq_knhashmask != 0) { 1453 for (i = 0; i <= kq->kq_knhashmask; i++) { 1454 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) { 1455 KASSERT((kn->kn_status & KN_INFLUX) == 0, 1456 ("KN_INFLUX set when not suppose to be")); 1457 kn->kn_status |= KN_INFLUX; 1458 KQ_UNLOCK(kq); 1459 if (!(kn->kn_status & KN_DETACHED)) 1460 kn->kn_fop->f_detach(kn); 1461 knote_drop(kn, td); 1462 KQ_LOCK(kq); 1463 } 1464 } 1465 } 1466 1467 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) { 1468 kq->kq_state |= KQ_TASKDRAIN; 1469 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0); 1470 } 1471 1472 if ((kq->kq_state & KQ_SEL) == KQ_SEL) { 1473 kq->kq_state &= ~KQ_SEL; 1474 selwakeuppri(&kq->kq_sel, PSOCK); 1475 } 1476 1477 KQ_UNLOCK(kq); 1478 1479 FILEDESC_LOCK_FAST(fdp); 1480 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list); 1481 FILEDESC_UNLOCK_FAST(fdp); 1482 1483 knlist_destroy(&kq->kq_sel.si_note); 1484 mtx_destroy(&kq->kq_lock); 1485 kq->kq_fdp = NULL; 1486 1487 if (kq->kq_knhash != NULL) 1488 free(kq->kq_knhash, M_KQUEUE); 1489 if (kq->kq_knlist != NULL) 1490 free(kq->kq_knlist, M_KQUEUE); 1491 1492 funsetown(&kq->kq_sigio); 1493 free(kq, M_KQUEUE); 1494 fp->f_data = NULL; 1495 1496 return (0); 1497 } 1498 1499 static void 1500 kqueue_wakeup(struct kqueue *kq) 1501 { 1502 KQ_OWNED(kq); 1503 1504 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) { 1505 kq->kq_state &= ~KQ_SLEEP; 1506 wakeup(kq); 1507 } 1508 if ((kq->kq_state & KQ_SEL) == KQ_SEL) { 1509 kq->kq_state &= ~KQ_SEL; 1510 selwakeuppri(&kq->kq_sel, PSOCK); 1511 } 1512 if (!knlist_empty(&kq->kq_sel.si_note)) 1513 kqueue_schedtask(kq); 1514 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) { 1515 pgsigio(&kq->kq_sigio, SIGIO, 0); 1516 } 1517 } 1518 1519 /* 1520 * Walk down a list of knotes, activating them if their event has triggered. 1521 * 1522 * There is a possibility to optimize in the case of one kq watching another. 1523 * Instead of scheduling a task to wake it up, you could pass enough state 1524 * down the chain to make up the parent kqueue. Make this code functional 1525 * first. 1526 */ 1527 void 1528 knote(struct knlist *list, long hint, int islocked) 1529 { 1530 struct kqueue *kq; 1531 struct knote *kn; 1532 1533 if (list == NULL) 1534 return; 1535 1536 KNL_ASSERT_LOCK(list, islocked); 1537 1538 if (!islocked) 1539 list->kl_lock(list->kl_lockarg); 1540 1541 /* 1542 * If we unlock the list lock (and set KN_INFLUX), we can eliminate 1543 * the kqueue scheduling, but this will introduce four 1544 * lock/unlock's for each knote to test. If we do, continue to use 1545 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is 1546 * only safe if you want to remove the current item, which we are 1547 * not doing. 1548 */ 1549 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) { 1550 kq = kn->kn_kq; 1551 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) { 1552 KQ_LOCK(kq); 1553 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) { 1554 kn->kn_status |= KN_HASKQLOCK; 1555 if (kn->kn_fop->f_event(kn, hint)) 1556 KNOTE_ACTIVATE(kn, 1); 1557 kn->kn_status &= ~KN_HASKQLOCK; 1558 } 1559 KQ_UNLOCK(kq); 1560 } 1561 kq = NULL; 1562 } 1563 if (!islocked) 1564 list->kl_unlock(list->kl_lockarg); 1565 } 1566 1567 /* 1568 * add a knote to a knlist 1569 */ 1570 void 1571 knlist_add(struct knlist *knl, struct knote *kn, int islocked) 1572 { 1573 KNL_ASSERT_LOCK(knl, islocked); 1574 KQ_NOTOWNED(kn->kn_kq); 1575 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == 1576 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED")); 1577 if (!islocked) 1578 knl->kl_lock(knl->kl_lockarg); 1579 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext); 1580 if (!islocked) 1581 knl->kl_unlock(knl->kl_lockarg); 1582 KQ_LOCK(kn->kn_kq); 1583 kn->kn_knlist = knl; 1584 kn->kn_status &= ~KN_DETACHED; 1585 KQ_UNLOCK(kn->kn_kq); 1586 } 1587 1588 static void 1589 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked) 1590 { 1591 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked")); 1592 KNL_ASSERT_LOCK(knl, knlislocked); 1593 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED); 1594 if (!kqislocked) 1595 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX, 1596 ("knlist_remove called w/o knote being KN_INFLUX or already removed")); 1597 if (!knlislocked) 1598 knl->kl_lock(knl->kl_lockarg); 1599 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext); 1600 kn->kn_knlist = NULL; 1601 if (!knlislocked) 1602 knl->kl_unlock(knl->kl_lockarg); 1603 if (!kqislocked) 1604 KQ_LOCK(kn->kn_kq); 1605 kn->kn_status |= KN_DETACHED; 1606 if (!kqislocked) 1607 KQ_UNLOCK(kn->kn_kq); 1608 } 1609 1610 /* 1611 * remove all knotes from a specified klist 1612 */ 1613 void 1614 knlist_remove(struct knlist *knl, struct knote *kn, int islocked) 1615 { 1616 1617 knlist_remove_kq(knl, kn, islocked, 0); 1618 } 1619 1620 /* 1621 * remove knote from a specified klist while in f_event handler. 1622 */ 1623 void 1624 knlist_remove_inevent(struct knlist *knl, struct knote *kn) 1625 { 1626 1627 knlist_remove_kq(knl, kn, 1, 1628 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK); 1629 } 1630 1631 int 1632 knlist_empty(struct knlist *knl) 1633 { 1634 KNL_ASSERT_LOCKED(knl); 1635 return SLIST_EMPTY(&knl->kl_list); 1636 } 1637 1638 static struct mtx knlist_lock; 1639 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects", 1640 MTX_DEF); 1641 static void knlist_mtx_lock(void *arg); 1642 static void knlist_mtx_unlock(void *arg); 1643 static int knlist_mtx_locked(void *arg); 1644 1645 static void 1646 knlist_mtx_lock(void *arg) 1647 { 1648 mtx_lock((struct mtx *)arg); 1649 } 1650 1651 static void 1652 knlist_mtx_unlock(void *arg) 1653 { 1654 mtx_unlock((struct mtx *)arg); 1655 } 1656 1657 static int 1658 knlist_mtx_locked(void *arg) 1659 { 1660 return (mtx_owned((struct mtx *)arg)); 1661 } 1662 1663 void 1664 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *), 1665 void (*kl_unlock)(void *), int (*kl_locked)(void *)) 1666 { 1667 1668 if (lock == NULL) 1669 knl->kl_lockarg = &knlist_lock; 1670 else 1671 knl->kl_lockarg = lock; 1672 1673 if (kl_lock == NULL) 1674 knl->kl_lock = knlist_mtx_lock; 1675 else 1676 knl->kl_lock = kl_lock; 1677 if (kl_unlock == NULL) 1678 knl->kl_unlock = knlist_mtx_unlock; 1679 else 1680 knl->kl_unlock = kl_unlock; 1681 if (kl_locked == NULL) 1682 knl->kl_locked = knlist_mtx_locked; 1683 else 1684 knl->kl_locked = kl_locked; 1685 1686 SLIST_INIT(&knl->kl_list); 1687 } 1688 1689 void 1690 knlist_destroy(struct knlist *knl) 1691 { 1692 1693 #ifdef INVARIANTS 1694 /* 1695 * if we run across this error, we need to find the offending 1696 * driver and have it call knlist_clear. 1697 */ 1698 if (!SLIST_EMPTY(&knl->kl_list)) 1699 printf("WARNING: destroying knlist w/ knotes on it!\n"); 1700 #endif 1701 1702 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL; 1703 SLIST_INIT(&knl->kl_list); 1704 } 1705 1706 /* 1707 * Even if we are locked, we may need to drop the lock to allow any influx 1708 * knotes time to "settle". 1709 */ 1710 void 1711 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn) 1712 { 1713 struct knote *kn; 1714 struct kqueue *kq; 1715 1716 if (islocked) 1717 KNL_ASSERT_LOCKED(knl); 1718 else { 1719 KNL_ASSERT_UNLOCKED(knl); 1720 again: /* need to reaquire lock since we have dropped it */ 1721 knl->kl_lock(knl->kl_lockarg); 1722 } 1723 1724 SLIST_FOREACH(kn, &knl->kl_list, kn_selnext) { 1725 kq = kn->kn_kq; 1726 KQ_LOCK(kq); 1727 if ((kn->kn_status & KN_INFLUX)) { 1728 KQ_UNLOCK(kq); 1729 continue; 1730 } 1731 knlist_remove_kq(knl, kn, 1, 1); 1732 if (killkn) { 1733 kn->kn_status |= KN_INFLUX | KN_DETACHED; 1734 KQ_UNLOCK(kq); 1735 knote_drop(kn, td); 1736 } else { 1737 /* Make sure cleared knotes disappear soon */ 1738 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 1739 KQ_UNLOCK(kq); 1740 } 1741 kq = NULL; 1742 } 1743 1744 if (!SLIST_EMPTY(&knl->kl_list)) { 1745 /* there are still KN_INFLUX remaining */ 1746 kn = SLIST_FIRST(&knl->kl_list); 1747 kq = kn->kn_kq; 1748 KQ_LOCK(kq); 1749 KASSERT(kn->kn_status & KN_INFLUX, 1750 ("knote removed w/o list lock")); 1751 knl->kl_unlock(knl->kl_lockarg); 1752 kq->kq_state |= KQ_FLUXWAIT; 1753 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0); 1754 kq = NULL; 1755 goto again; 1756 } 1757 1758 if (islocked) 1759 KNL_ASSERT_LOCKED(knl); 1760 else { 1761 knl->kl_unlock(knl->kl_lockarg); 1762 KNL_ASSERT_UNLOCKED(knl); 1763 } 1764 } 1765 1766 /* 1767 * remove all knotes referencing a specified fd 1768 * must be called with FILEDESC lock. This prevents a race where a new fd 1769 * comes along and occupies the entry and we attach a knote to the fd. 1770 */ 1771 void 1772 knote_fdclose(struct thread *td, int fd) 1773 { 1774 struct filedesc *fdp = td->td_proc->p_fd; 1775 struct kqueue *kq; 1776 struct knote *kn; 1777 int influx; 1778 1779 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED); 1780 1781 /* 1782 * We shouldn't have to worry about new kevents appearing on fd 1783 * since filedesc is locked. 1784 */ 1785 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) { 1786 KQ_LOCK(kq); 1787 1788 again: 1789 influx = 0; 1790 while (kq->kq_knlistsize > fd && 1791 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) { 1792 if (kn->kn_status & KN_INFLUX) { 1793 /* someone else might be waiting on our knote */ 1794 if (influx) 1795 wakeup(kq); 1796 kq->kq_state |= KQ_FLUXWAIT; 1797 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0); 1798 goto again; 1799 } 1800 kn->kn_status |= KN_INFLUX; 1801 KQ_UNLOCK(kq); 1802 if (!(kn->kn_status & KN_DETACHED)) 1803 kn->kn_fop->f_detach(kn); 1804 knote_drop(kn, td); 1805 influx = 1; 1806 KQ_LOCK(kq); 1807 } 1808 KQ_UNLOCK_FLUX(kq); 1809 } 1810 } 1811 1812 static int 1813 knote_attach(struct knote *kn, struct kqueue *kq) 1814 { 1815 struct klist *list; 1816 1817 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX")); 1818 KQ_OWNED(kq); 1819 1820 if (kn->kn_fop->f_isfd) { 1821 if (kn->kn_id >= kq->kq_knlistsize) 1822 return ENOMEM; 1823 list = &kq->kq_knlist[kn->kn_id]; 1824 } else { 1825 if (kq->kq_knhash == NULL) 1826 return ENOMEM; 1827 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)]; 1828 } 1829 1830 SLIST_INSERT_HEAD(list, kn, kn_link); 1831 1832 return 0; 1833 } 1834 1835 /* 1836 * knote must already have been detached using the f_detach method. 1837 * no lock need to be held, it is assumed that the KN_INFLUX flag is set 1838 * to prevent other removal. 1839 */ 1840 static void 1841 knote_drop(struct knote *kn, struct thread *td) 1842 { 1843 struct kqueue *kq; 1844 struct klist *list; 1845 1846 kq = kn->kn_kq; 1847 1848 KQ_NOTOWNED(kq); 1849 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX, 1850 ("knote_drop called without KN_INFLUX set in kn_status")); 1851 1852 KQ_LOCK(kq); 1853 if (kn->kn_fop->f_isfd) 1854 list = &kq->kq_knlist[kn->kn_id]; 1855 else 1856 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)]; 1857 1858 if (!SLIST_EMPTY(list)) 1859 SLIST_REMOVE(list, kn, knote, kn_link); 1860 if (kn->kn_status & KN_QUEUED) 1861 knote_dequeue(kn); 1862 KQ_UNLOCK_FLUX(kq); 1863 1864 if (kn->kn_fop->f_isfd) { 1865 fdrop(kn->kn_fp, td); 1866 kn->kn_fp = NULL; 1867 } 1868 kqueue_fo_release(kn->kn_kevent.filter); 1869 kn->kn_fop = NULL; 1870 knote_free(kn); 1871 } 1872 1873 static void 1874 knote_enqueue(struct knote *kn) 1875 { 1876 struct kqueue *kq = kn->kn_kq; 1877 1878 KQ_OWNED(kn->kn_kq); 1879 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued")); 1880 1881 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 1882 kn->kn_status |= KN_QUEUED; 1883 kq->kq_count++; 1884 kqueue_wakeup(kq); 1885 } 1886 1887 static void 1888 knote_dequeue(struct knote *kn) 1889 { 1890 struct kqueue *kq = kn->kn_kq; 1891 1892 KQ_OWNED(kn->kn_kq); 1893 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued")); 1894 1895 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 1896 kn->kn_status &= ~KN_QUEUED; 1897 kq->kq_count--; 1898 } 1899 1900 static void 1901 knote_init(void) 1902 { 1903 1904 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL, 1905 NULL, NULL, UMA_ALIGN_PTR, 0); 1906 } 1907 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL) 1908 1909 static struct knote * 1910 knote_alloc(int waitok) 1911 { 1912 return ((struct knote *)uma_zalloc(knote_zone, 1913 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO)); 1914 } 1915 1916 static void 1917 knote_free(struct knote *kn) 1918 { 1919 if (kn != NULL) 1920 uma_zfree(knote_zone, kn); 1921 } 1922