1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_compat.h" 41 #include "opt_ktrace.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/sysproto.h> 46 #include <sys/filedesc.h> 47 #include <sys/filio.h> 48 #include <sys/fcntl.h> 49 #include <sys/file.h> 50 #include <sys/proc.h> 51 #include <sys/signalvar.h> 52 #include <sys/socketvar.h> 53 #include <sys/uio.h> 54 #include <sys/kernel.h> 55 #include <sys/ktr.h> 56 #include <sys/limits.h> 57 #include <sys/malloc.h> 58 #include <sys/poll.h> 59 #include <sys/resourcevar.h> 60 #include <sys/selinfo.h> 61 #include <sys/sleepqueue.h> 62 #include <sys/syscallsubr.h> 63 #include <sys/sysctl.h> 64 #include <sys/sysent.h> 65 #include <sys/vnode.h> 66 #include <sys/bio.h> 67 #include <sys/buf.h> 68 #include <sys/condvar.h> 69 #ifdef KTRACE 70 #include <sys/ktrace.h> 71 #endif 72 73 #include <security/audit/audit.h> 74 75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 76 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 77 MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 78 79 static int pollscan(struct thread *, struct pollfd *, u_int); 80 static int pollrescan(struct thread *); 81 static int selscan(struct thread *, fd_mask **, fd_mask **, int); 82 static int selrescan(struct thread *, fd_mask **, fd_mask **); 83 static void selfdalloc(struct thread *, void *); 84 static void selfdfree(struct seltd *, struct selfd *); 85 static int dofileread(struct thread *, int, struct file *, struct uio *, 86 off_t, int); 87 static int dofilewrite(struct thread *, int, struct file *, struct uio *, 88 off_t, int); 89 static void doselwakeup(struct selinfo *, int); 90 static void seltdinit(struct thread *); 91 static int seltdwait(struct thread *, int); 92 static void seltdclear(struct thread *); 93 94 /* 95 * One seltd per-thread allocated on demand as needed. 96 * 97 * t - protected by st_mtx 98 * k - Only accessed by curthread or read-only 99 */ 100 struct seltd { 101 STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 102 struct selfd *st_free1; /* (k) free fd for read set. */ 103 struct selfd *st_free2; /* (k) free fd for write set. */ 104 struct mtx st_mtx; /* Protects struct seltd */ 105 struct cv st_wait; /* (t) Wait channel. */ 106 int st_flags; /* (t) SELTD_ flags. */ 107 }; 108 109 #define SELTD_PENDING 0x0001 /* We have pending events. */ 110 #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 111 112 /* 113 * One selfd allocated per-thread per-file-descriptor. 114 * f - protected by sf_mtx 115 */ 116 struct selfd { 117 STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 118 TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 119 struct selinfo *sf_si; /* (f) selinfo when linked. */ 120 struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 121 struct seltd *sf_td; /* (k) owning seltd. */ 122 void *sf_cookie; /* (k) fd or pollfd. */ 123 }; 124 125 static uma_zone_t selfd_zone; 126 127 #ifndef _SYS_SYSPROTO_H_ 128 struct read_args { 129 int fd; 130 void *buf; 131 size_t nbyte; 132 }; 133 #endif 134 int 135 read(td, uap) 136 struct thread *td; 137 struct read_args *uap; 138 { 139 struct uio auio; 140 struct iovec aiov; 141 int error; 142 143 if (uap->nbyte > INT_MAX) 144 return (EINVAL); 145 aiov.iov_base = uap->buf; 146 aiov.iov_len = uap->nbyte; 147 auio.uio_iov = &aiov; 148 auio.uio_iovcnt = 1; 149 auio.uio_resid = uap->nbyte; 150 auio.uio_segflg = UIO_USERSPACE; 151 error = kern_readv(td, uap->fd, &auio); 152 return(error); 153 } 154 155 /* 156 * Positioned read system call 157 */ 158 #ifndef _SYS_SYSPROTO_H_ 159 struct pread_args { 160 int fd; 161 void *buf; 162 size_t nbyte; 163 int pad; 164 off_t offset; 165 }; 166 #endif 167 int 168 pread(td, uap) 169 struct thread *td; 170 struct pread_args *uap; 171 { 172 struct uio auio; 173 struct iovec aiov; 174 int error; 175 176 if (uap->nbyte > INT_MAX) 177 return (EINVAL); 178 aiov.iov_base = uap->buf; 179 aiov.iov_len = uap->nbyte; 180 auio.uio_iov = &aiov; 181 auio.uio_iovcnt = 1; 182 auio.uio_resid = uap->nbyte; 183 auio.uio_segflg = UIO_USERSPACE; 184 error = kern_preadv(td, uap->fd, &auio, uap->offset); 185 return(error); 186 } 187 188 int 189 freebsd6_pread(td, uap) 190 struct thread *td; 191 struct freebsd6_pread_args *uap; 192 { 193 struct pread_args oargs; 194 195 oargs.fd = uap->fd; 196 oargs.buf = uap->buf; 197 oargs.nbyte = uap->nbyte; 198 oargs.offset = uap->offset; 199 return (pread(td, &oargs)); 200 } 201 202 /* 203 * Scatter read system call. 204 */ 205 #ifndef _SYS_SYSPROTO_H_ 206 struct readv_args { 207 int fd; 208 struct iovec *iovp; 209 u_int iovcnt; 210 }; 211 #endif 212 int 213 readv(struct thread *td, struct readv_args *uap) 214 { 215 struct uio *auio; 216 int error; 217 218 error = copyinuio(uap->iovp, uap->iovcnt, &auio); 219 if (error) 220 return (error); 221 error = kern_readv(td, uap->fd, auio); 222 free(auio, M_IOV); 223 return (error); 224 } 225 226 int 227 kern_readv(struct thread *td, int fd, struct uio *auio) 228 { 229 struct file *fp; 230 int error; 231 232 error = fget_read(td, fd, &fp); 233 if (error) 234 return (error); 235 error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 236 fdrop(fp, td); 237 return (error); 238 } 239 240 /* 241 * Scatter positioned read system call. 242 */ 243 #ifndef _SYS_SYSPROTO_H_ 244 struct preadv_args { 245 int fd; 246 struct iovec *iovp; 247 u_int iovcnt; 248 off_t offset; 249 }; 250 #endif 251 int 252 preadv(struct thread *td, struct preadv_args *uap) 253 { 254 struct uio *auio; 255 int error; 256 257 error = copyinuio(uap->iovp, uap->iovcnt, &auio); 258 if (error) 259 return (error); 260 error = kern_preadv(td, uap->fd, auio, uap->offset); 261 free(auio, M_IOV); 262 return (error); 263 } 264 265 int 266 kern_preadv(td, fd, auio, offset) 267 struct thread *td; 268 int fd; 269 struct uio *auio; 270 off_t offset; 271 { 272 struct file *fp; 273 int error; 274 275 error = fget_read(td, fd, &fp); 276 if (error) 277 return (error); 278 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 279 error = ESPIPE; 280 else if (offset < 0 && fp->f_vnode->v_type != VCHR) 281 error = EINVAL; 282 else 283 error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 284 fdrop(fp, td); 285 return (error); 286 } 287 288 /* 289 * Common code for readv and preadv that reads data in 290 * from a file using the passed in uio, offset, and flags. 291 */ 292 static int 293 dofileread(td, fd, fp, auio, offset, flags) 294 struct thread *td; 295 int fd; 296 struct file *fp; 297 struct uio *auio; 298 off_t offset; 299 int flags; 300 { 301 ssize_t cnt; 302 int error; 303 #ifdef KTRACE 304 struct uio *ktruio = NULL; 305 #endif 306 307 /* Finish zero length reads right here */ 308 if (auio->uio_resid == 0) { 309 td->td_retval[0] = 0; 310 return(0); 311 } 312 auio->uio_rw = UIO_READ; 313 auio->uio_offset = offset; 314 auio->uio_td = td; 315 #ifdef KTRACE 316 if (KTRPOINT(td, KTR_GENIO)) 317 ktruio = cloneuio(auio); 318 #endif 319 cnt = auio->uio_resid; 320 if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 321 if (auio->uio_resid != cnt && (error == ERESTART || 322 error == EINTR || error == EWOULDBLOCK)) 323 error = 0; 324 } 325 cnt -= auio->uio_resid; 326 #ifdef KTRACE 327 if (ktruio != NULL) { 328 ktruio->uio_resid = cnt; 329 ktrgenio(fd, UIO_READ, ktruio, error); 330 } 331 #endif 332 td->td_retval[0] = cnt; 333 return (error); 334 } 335 336 #ifndef _SYS_SYSPROTO_H_ 337 struct write_args { 338 int fd; 339 const void *buf; 340 size_t nbyte; 341 }; 342 #endif 343 int 344 write(td, uap) 345 struct thread *td; 346 struct write_args *uap; 347 { 348 struct uio auio; 349 struct iovec aiov; 350 int error; 351 352 if (uap->nbyte > INT_MAX) 353 return (EINVAL); 354 aiov.iov_base = (void *)(uintptr_t)uap->buf; 355 aiov.iov_len = uap->nbyte; 356 auio.uio_iov = &aiov; 357 auio.uio_iovcnt = 1; 358 auio.uio_resid = uap->nbyte; 359 auio.uio_segflg = UIO_USERSPACE; 360 error = kern_writev(td, uap->fd, &auio); 361 return(error); 362 } 363 364 /* 365 * Positioned write system call. 366 */ 367 #ifndef _SYS_SYSPROTO_H_ 368 struct pwrite_args { 369 int fd; 370 const void *buf; 371 size_t nbyte; 372 int pad; 373 off_t offset; 374 }; 375 #endif 376 int 377 pwrite(td, uap) 378 struct thread *td; 379 struct pwrite_args *uap; 380 { 381 struct uio auio; 382 struct iovec aiov; 383 int error; 384 385 if (uap->nbyte > INT_MAX) 386 return (EINVAL); 387 aiov.iov_base = (void *)(uintptr_t)uap->buf; 388 aiov.iov_len = uap->nbyte; 389 auio.uio_iov = &aiov; 390 auio.uio_iovcnt = 1; 391 auio.uio_resid = uap->nbyte; 392 auio.uio_segflg = UIO_USERSPACE; 393 error = kern_pwritev(td, uap->fd, &auio, uap->offset); 394 return(error); 395 } 396 397 int 398 freebsd6_pwrite(td, uap) 399 struct thread *td; 400 struct freebsd6_pwrite_args *uap; 401 { 402 struct pwrite_args oargs; 403 404 oargs.fd = uap->fd; 405 oargs.buf = uap->buf; 406 oargs.nbyte = uap->nbyte; 407 oargs.offset = uap->offset; 408 return (pwrite(td, &oargs)); 409 } 410 411 /* 412 * Gather write system call. 413 */ 414 #ifndef _SYS_SYSPROTO_H_ 415 struct writev_args { 416 int fd; 417 struct iovec *iovp; 418 u_int iovcnt; 419 }; 420 #endif 421 int 422 writev(struct thread *td, struct writev_args *uap) 423 { 424 struct uio *auio; 425 int error; 426 427 error = copyinuio(uap->iovp, uap->iovcnt, &auio); 428 if (error) 429 return (error); 430 error = kern_writev(td, uap->fd, auio); 431 free(auio, M_IOV); 432 return (error); 433 } 434 435 int 436 kern_writev(struct thread *td, int fd, struct uio *auio) 437 { 438 struct file *fp; 439 int error; 440 441 error = fget_write(td, fd, &fp); 442 if (error) 443 return (error); 444 error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 445 fdrop(fp, td); 446 return (error); 447 } 448 449 /* 450 * Gather positioned write system call. 451 */ 452 #ifndef _SYS_SYSPROTO_H_ 453 struct pwritev_args { 454 int fd; 455 struct iovec *iovp; 456 u_int iovcnt; 457 off_t offset; 458 }; 459 #endif 460 int 461 pwritev(struct thread *td, struct pwritev_args *uap) 462 { 463 struct uio *auio; 464 int error; 465 466 error = copyinuio(uap->iovp, uap->iovcnt, &auio); 467 if (error) 468 return (error); 469 error = kern_pwritev(td, uap->fd, auio, uap->offset); 470 free(auio, M_IOV); 471 return (error); 472 } 473 474 int 475 kern_pwritev(td, fd, auio, offset) 476 struct thread *td; 477 struct uio *auio; 478 int fd; 479 off_t offset; 480 { 481 struct file *fp; 482 int error; 483 484 error = fget_write(td, fd, &fp); 485 if (error) 486 return (error); 487 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 488 error = ESPIPE; 489 else if (offset < 0 && fp->f_vnode->v_type != VCHR) 490 error = EINVAL; 491 else 492 error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 493 fdrop(fp, td); 494 return (error); 495 } 496 497 /* 498 * Common code for writev and pwritev that writes data to 499 * a file using the passed in uio, offset, and flags. 500 */ 501 static int 502 dofilewrite(td, fd, fp, auio, offset, flags) 503 struct thread *td; 504 int fd; 505 struct file *fp; 506 struct uio *auio; 507 off_t offset; 508 int flags; 509 { 510 ssize_t cnt; 511 int error; 512 #ifdef KTRACE 513 struct uio *ktruio = NULL; 514 #endif 515 516 auio->uio_rw = UIO_WRITE; 517 auio->uio_td = td; 518 auio->uio_offset = offset; 519 #ifdef KTRACE 520 if (KTRPOINT(td, KTR_GENIO)) 521 ktruio = cloneuio(auio); 522 #endif 523 cnt = auio->uio_resid; 524 if (fp->f_type == DTYPE_VNODE) 525 bwillwrite(); 526 if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 527 if (auio->uio_resid != cnt && (error == ERESTART || 528 error == EINTR || error == EWOULDBLOCK)) 529 error = 0; 530 /* Socket layer is responsible for issuing SIGPIPE. */ 531 if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 532 PROC_LOCK(td->td_proc); 533 psignal(td->td_proc, SIGPIPE); 534 PROC_UNLOCK(td->td_proc); 535 } 536 } 537 cnt -= auio->uio_resid; 538 #ifdef KTRACE 539 if (ktruio != NULL) { 540 ktruio->uio_resid = cnt; 541 ktrgenio(fd, UIO_WRITE, ktruio, error); 542 } 543 #endif 544 td->td_retval[0] = cnt; 545 return (error); 546 } 547 548 /* 549 * Truncate a file given a file descriptor. 550 * 551 * Can't use fget_write() here, since must return EINVAL and not EBADF if the 552 * descriptor isn't writable. 553 */ 554 int 555 kern_ftruncate(td, fd, length) 556 struct thread *td; 557 int fd; 558 off_t length; 559 { 560 struct file *fp; 561 int error; 562 563 AUDIT_ARG(fd, fd); 564 if (length < 0) 565 return (EINVAL); 566 error = fget(td, fd, &fp); 567 if (error) 568 return (error); 569 AUDIT_ARG(file, td->td_proc, fp); 570 if (!(fp->f_flag & FWRITE)) { 571 fdrop(fp, td); 572 return (EINVAL); 573 } 574 error = fo_truncate(fp, length, td->td_ucred, td); 575 fdrop(fp, td); 576 return (error); 577 } 578 579 #ifndef _SYS_SYSPROTO_H_ 580 struct ftruncate_args { 581 int fd; 582 int pad; 583 off_t length; 584 }; 585 #endif 586 int 587 ftruncate(td, uap) 588 struct thread *td; 589 struct ftruncate_args *uap; 590 { 591 592 return (kern_ftruncate(td, uap->fd, uap->length)); 593 } 594 595 #if defined(COMPAT_43) 596 #ifndef _SYS_SYSPROTO_H_ 597 struct oftruncate_args { 598 int fd; 599 long length; 600 }; 601 #endif 602 int 603 oftruncate(td, uap) 604 struct thread *td; 605 struct oftruncate_args *uap; 606 { 607 608 return (kern_ftruncate(td, uap->fd, uap->length)); 609 } 610 #endif /* COMPAT_43 */ 611 612 #ifndef _SYS_SYSPROTO_H_ 613 struct ioctl_args { 614 int fd; 615 u_long com; 616 caddr_t data; 617 }; 618 #endif 619 /* ARGSUSED */ 620 int 621 ioctl(struct thread *td, struct ioctl_args *uap) 622 { 623 u_long com; 624 int arg, error; 625 u_int size; 626 caddr_t data; 627 628 if (uap->com > 0xffffffff) { 629 printf( 630 "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 631 td->td_proc->p_pid, td->td_name, uap->com); 632 uap->com &= 0xffffffff; 633 } 634 com = uap->com; 635 636 /* 637 * Interpret high order word to find amount of data to be 638 * copied to/from the user's address space. 639 */ 640 size = IOCPARM_LEN(com); 641 if ((size > IOCPARM_MAX) || 642 ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 643 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 644 ((com & IOC_OUT) && size == 0) || 645 #else 646 ((com & (IOC_IN | IOC_OUT)) && size == 0) || 647 #endif 648 ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 649 return (ENOTTY); 650 651 if (size > 0) { 652 if (com & IOC_VOID) { 653 /* Integer argument. */ 654 arg = (intptr_t)uap->data; 655 data = (void *)&arg; 656 size = 0; 657 } else 658 data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 659 } else 660 data = (void *)&uap->data; 661 if (com & IOC_IN) { 662 error = copyin(uap->data, data, (u_int)size); 663 if (error) { 664 if (size > 0) 665 free(data, M_IOCTLOPS); 666 return (error); 667 } 668 } else if (com & IOC_OUT) { 669 /* 670 * Zero the buffer so the user always 671 * gets back something deterministic. 672 */ 673 bzero(data, size); 674 } 675 676 error = kern_ioctl(td, uap->fd, com, data); 677 678 if (error == 0 && (com & IOC_OUT)) 679 error = copyout(data, uap->data, (u_int)size); 680 681 if (size > 0) 682 free(data, M_IOCTLOPS); 683 return (error); 684 } 685 686 int 687 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 688 { 689 struct file *fp; 690 struct filedesc *fdp; 691 int error; 692 int tmp; 693 694 if ((error = fget(td, fd, &fp)) != 0) 695 return (error); 696 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 697 fdrop(fp, td); 698 return (EBADF); 699 } 700 fdp = td->td_proc->p_fd; 701 switch (com) { 702 case FIONCLEX: 703 FILEDESC_XLOCK(fdp); 704 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE; 705 FILEDESC_XUNLOCK(fdp); 706 goto out; 707 case FIOCLEX: 708 FILEDESC_XLOCK(fdp); 709 fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 710 FILEDESC_XUNLOCK(fdp); 711 goto out; 712 case FIONBIO: 713 if ((tmp = *(int *)data)) 714 atomic_set_int(&fp->f_flag, FNONBLOCK); 715 else 716 atomic_clear_int(&fp->f_flag, FNONBLOCK); 717 data = (void *)&tmp; 718 break; 719 case FIOASYNC: 720 if ((tmp = *(int *)data)) 721 atomic_set_int(&fp->f_flag, FASYNC); 722 else 723 atomic_clear_int(&fp->f_flag, FASYNC); 724 data = (void *)&tmp; 725 break; 726 } 727 728 error = fo_ioctl(fp, com, data, td->td_ucred, td); 729 out: 730 fdrop(fp, td); 731 return (error); 732 } 733 734 #ifndef _SYS_SYSPROTO_H_ 735 struct select_args { 736 int nd; 737 fd_set *in, *ou, *ex; 738 struct timeval *tv; 739 }; 740 #endif 741 int 742 select(td, uap) 743 register struct thread *td; 744 register struct select_args *uap; 745 { 746 struct timeval tv, *tvp; 747 int error; 748 749 if (uap->tv != NULL) { 750 error = copyin(uap->tv, &tv, sizeof(tv)); 751 if (error) 752 return (error); 753 tvp = &tv; 754 } else 755 tvp = NULL; 756 757 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 758 } 759 760 int 761 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 762 fd_set *fd_ex, struct timeval *tvp) 763 { 764 struct filedesc *fdp; 765 /* 766 * The magic 2048 here is chosen to be just enough for FD_SETSIZE 767 * infds with the new FD_SETSIZE of 1024, and more than enough for 768 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 769 * of 256. 770 */ 771 fd_mask s_selbits[howmany(2048, NFDBITS)]; 772 fd_mask *ibits[3], *obits[3], *selbits, *sbp; 773 struct timeval atv, rtv, ttv; 774 int error, timo; 775 u_int nbufbytes, ncpbytes, nfdbits; 776 777 if (nd < 0) 778 return (EINVAL); 779 fdp = td->td_proc->p_fd; 780 781 FILEDESC_SLOCK(fdp); 782 if (nd > td->td_proc->p_fd->fd_nfiles) 783 nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 784 FILEDESC_SUNLOCK(fdp); 785 786 /* 787 * Allocate just enough bits for the non-null fd_sets. Use the 788 * preallocated auto buffer if possible. 789 */ 790 nfdbits = roundup(nd, NFDBITS); 791 ncpbytes = nfdbits / NBBY; 792 nbufbytes = 0; 793 if (fd_in != NULL) 794 nbufbytes += 2 * ncpbytes; 795 if (fd_ou != NULL) 796 nbufbytes += 2 * ncpbytes; 797 if (fd_ex != NULL) 798 nbufbytes += 2 * ncpbytes; 799 if (nbufbytes <= sizeof s_selbits) 800 selbits = &s_selbits[0]; 801 else 802 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 803 804 /* 805 * Assign pointers into the bit buffers and fetch the input bits. 806 * Put the output buffers together so that they can be bzeroed 807 * together. 808 */ 809 sbp = selbits; 810 #define getbits(name, x) \ 811 do { \ 812 if (name == NULL) \ 813 ibits[x] = NULL; \ 814 else { \ 815 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 816 obits[x] = sbp; \ 817 sbp += ncpbytes / sizeof *sbp; \ 818 error = copyin(name, ibits[x], ncpbytes); \ 819 if (error != 0) \ 820 goto done; \ 821 } \ 822 } while (0) 823 getbits(fd_in, 0); 824 getbits(fd_ou, 1); 825 getbits(fd_ex, 2); 826 #undef getbits 827 if (nbufbytes != 0) 828 bzero(selbits, nbufbytes / 2); 829 830 if (tvp != NULL) { 831 atv = *tvp; 832 if (itimerfix(&atv)) { 833 error = EINVAL; 834 goto done; 835 } 836 getmicrouptime(&rtv); 837 timevaladd(&atv, &rtv); 838 } else { 839 atv.tv_sec = 0; 840 atv.tv_usec = 0; 841 } 842 timo = 0; 843 seltdinit(td); 844 /* Iterate until the timeout expires or descriptors become ready. */ 845 for (;;) { 846 error = selscan(td, ibits, obits, nd); 847 if (error || td->td_retval[0] != 0) 848 break; 849 if (atv.tv_sec || atv.tv_usec) { 850 getmicrouptime(&rtv); 851 if (timevalcmp(&rtv, &atv, >=)) 852 break; 853 ttv = atv; 854 timevalsub(&ttv, &rtv); 855 timo = ttv.tv_sec > 24 * 60 * 60 ? 856 24 * 60 * 60 * hz : tvtohz(&ttv); 857 } 858 error = seltdwait(td, timo); 859 if (error) 860 break; 861 error = selrescan(td, ibits, obits); 862 if (error || td->td_retval[0] != 0) 863 break; 864 } 865 seltdclear(td); 866 867 done: 868 /* select is not restarted after signals... */ 869 if (error == ERESTART) 870 error = EINTR; 871 if (error == EWOULDBLOCK) 872 error = 0; 873 #define putbits(name, x) \ 874 if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 875 error = error2; 876 if (error == 0) { 877 int error2; 878 879 putbits(fd_in, 0); 880 putbits(fd_ou, 1); 881 putbits(fd_ex, 2); 882 #undef putbits 883 } 884 if (selbits != &s_selbits[0]) 885 free(selbits, M_SELECT); 886 887 return (error); 888 } 889 890 /* 891 * Traverse the list of fds attached to this thread's seltd and check for 892 * completion. 893 */ 894 static int 895 selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 896 { 897 struct seltd *stp; 898 struct selfd *sfp; 899 struct selfd *sfn; 900 struct selinfo *si; 901 struct file *fp; 902 int msk, fd; 903 int n = 0; 904 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 905 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 906 struct filedesc *fdp = td->td_proc->p_fd; 907 908 stp = td->td_sel; 909 FILEDESC_SLOCK(fdp); 910 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 911 fd = (int)(uintptr_t)sfp->sf_cookie; 912 si = sfp->sf_si; 913 selfdfree(stp, sfp); 914 /* If the selinfo wasn't cleared the event didn't fire. */ 915 if (si != NULL) 916 continue; 917 if ((fp = fget_locked(fdp, fd)) == NULL) { 918 FILEDESC_SUNLOCK(fdp); 919 return (EBADF); 920 } 921 for (msk = 0; msk < 3; msk++) { 922 if (ibits[msk] == NULL) 923 continue; 924 if ((ibits[msk][fd/NFDBITS] & 925 ((fd_mask) 1 << (fd % NFDBITS))) == 0) 926 continue; 927 if (fo_poll(fp, flag[msk], td->td_ucred, td)) { 928 obits[msk][(fd)/NFDBITS] |= 929 ((fd_mask)1 << ((fd) % NFDBITS)); 930 n++; 931 } 932 } 933 } 934 FILEDESC_SUNLOCK(fdp); 935 stp->st_flags = 0; 936 td->td_retval[0] = n; 937 return (0); 938 } 939 940 /* 941 * Perform the initial filedescriptor scan and register ourselves with 942 * each selinfo. 943 */ 944 static int 945 selscan(td, ibits, obits, nfd) 946 struct thread *td; 947 fd_mask **ibits, **obits; 948 int nfd; 949 { 950 int msk, i, fd; 951 fd_mask bits; 952 struct file *fp; 953 int n = 0; 954 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 955 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 956 struct filedesc *fdp = td->td_proc->p_fd; 957 958 FILEDESC_SLOCK(fdp); 959 for (msk = 0; msk < 3; msk++) { 960 if (ibits[msk] == NULL) 961 continue; 962 for (i = 0; i < nfd; i += NFDBITS) { 963 bits = ibits[msk][i/NFDBITS]; 964 /* ffs(int mask) not portable, fd_mask is long */ 965 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 966 if (!(bits & 1)) 967 continue; 968 if ((fp = fget_locked(fdp, fd)) == NULL) { 969 FILEDESC_SUNLOCK(fdp); 970 return (EBADF); 971 } 972 selfdalloc(td, (void *)(uintptr_t)fd); 973 if (fo_poll(fp, flag[msk], td->td_ucred, 974 td)) { 975 obits[msk][(fd)/NFDBITS] |= 976 ((fd_mask)1 << ((fd) % NFDBITS)); 977 n++; 978 } 979 } 980 } 981 } 982 FILEDESC_SUNLOCK(fdp); 983 td->td_retval[0] = n; 984 return (0); 985 } 986 987 #ifndef _SYS_SYSPROTO_H_ 988 struct poll_args { 989 struct pollfd *fds; 990 u_int nfds; 991 int timeout; 992 }; 993 #endif 994 int 995 poll(td, uap) 996 struct thread *td; 997 struct poll_args *uap; 998 { 999 struct pollfd *bits; 1000 struct pollfd smallbits[32]; 1001 struct timeval atv, rtv, ttv; 1002 int error = 0, timo; 1003 u_int nfds; 1004 size_t ni; 1005 1006 nfds = uap->nfds; 1007 if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1008 return (EINVAL); 1009 ni = nfds * sizeof(struct pollfd); 1010 if (ni > sizeof(smallbits)) 1011 bits = malloc(ni, M_TEMP, M_WAITOK); 1012 else 1013 bits = smallbits; 1014 error = copyin(uap->fds, bits, ni); 1015 if (error) 1016 goto done; 1017 if (uap->timeout != INFTIM) { 1018 atv.tv_sec = uap->timeout / 1000; 1019 atv.tv_usec = (uap->timeout % 1000) * 1000; 1020 if (itimerfix(&atv)) { 1021 error = EINVAL; 1022 goto done; 1023 } 1024 getmicrouptime(&rtv); 1025 timevaladd(&atv, &rtv); 1026 } else { 1027 atv.tv_sec = 0; 1028 atv.tv_usec = 0; 1029 } 1030 timo = 0; 1031 seltdinit(td); 1032 /* Iterate until the timeout expires or descriptors become ready. */ 1033 for (;;) { 1034 error = pollscan(td, bits, nfds); 1035 if (error || td->td_retval[0] != 0) 1036 break; 1037 if (atv.tv_sec || atv.tv_usec) { 1038 getmicrouptime(&rtv); 1039 if (timevalcmp(&rtv, &atv, >=)) 1040 break; 1041 ttv = atv; 1042 timevalsub(&ttv, &rtv); 1043 timo = ttv.tv_sec > 24 * 60 * 60 ? 1044 24 * 60 * 60 * hz : tvtohz(&ttv); 1045 } 1046 error = seltdwait(td, timo); 1047 if (error) 1048 break; 1049 error = pollrescan(td); 1050 if (error || td->td_retval[0] != 0) 1051 break; 1052 } 1053 seltdclear(td); 1054 1055 done: 1056 /* poll is not restarted after signals... */ 1057 if (error == ERESTART) 1058 error = EINTR; 1059 if (error == EWOULDBLOCK) 1060 error = 0; 1061 if (error == 0) { 1062 error = copyout(bits, uap->fds, ni); 1063 if (error) 1064 goto out; 1065 } 1066 out: 1067 if (ni > sizeof(smallbits)) 1068 free(bits, M_TEMP); 1069 return (error); 1070 } 1071 1072 static int 1073 pollrescan(struct thread *td) 1074 { 1075 struct seltd *stp; 1076 struct selfd *sfp; 1077 struct selfd *sfn; 1078 struct selinfo *si; 1079 struct filedesc *fdp; 1080 struct file *fp; 1081 struct pollfd *fd; 1082 int n; 1083 1084 n = 0; 1085 fdp = td->td_proc->p_fd; 1086 stp = td->td_sel; 1087 FILEDESC_SLOCK(fdp); 1088 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1089 fd = (struct pollfd *)sfp->sf_cookie; 1090 si = sfp->sf_si; 1091 selfdfree(stp, sfp); 1092 /* If the selinfo wasn't cleared the event didn't fire. */ 1093 if (si != NULL) 1094 continue; 1095 fp = fdp->fd_ofiles[fd->fd]; 1096 if (fp == NULL) { 1097 fd->revents = POLLNVAL; 1098 n++; 1099 continue; 1100 } 1101 /* 1102 * Note: backend also returns POLLHUP and 1103 * POLLERR if appropriate. 1104 */ 1105 fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1106 if (fd->revents != 0) 1107 n++; 1108 } 1109 FILEDESC_SUNLOCK(fdp); 1110 stp->st_flags = 0; 1111 td->td_retval[0] = n; 1112 return (0); 1113 } 1114 1115 1116 static int 1117 pollscan(td, fds, nfd) 1118 struct thread *td; 1119 struct pollfd *fds; 1120 u_int nfd; 1121 { 1122 struct filedesc *fdp = td->td_proc->p_fd; 1123 int i; 1124 struct file *fp; 1125 int n = 0; 1126 1127 FILEDESC_SLOCK(fdp); 1128 for (i = 0; i < nfd; i++, fds++) { 1129 if (fds->fd >= fdp->fd_nfiles) { 1130 fds->revents = POLLNVAL; 1131 n++; 1132 } else if (fds->fd < 0) { 1133 fds->revents = 0; 1134 } else { 1135 fp = fdp->fd_ofiles[fds->fd]; 1136 if (fp == NULL) { 1137 fds->revents = POLLNVAL; 1138 n++; 1139 } else { 1140 /* 1141 * Note: backend also returns POLLHUP and 1142 * POLLERR if appropriate. 1143 */ 1144 selfdalloc(td, fds); 1145 fds->revents = fo_poll(fp, fds->events, 1146 td->td_ucred, td); 1147 if (fds->revents != 0) 1148 n++; 1149 } 1150 } 1151 } 1152 FILEDESC_SUNLOCK(fdp); 1153 td->td_retval[0] = n; 1154 return (0); 1155 } 1156 1157 /* 1158 * OpenBSD poll system call. 1159 * 1160 * XXX this isn't quite a true representation.. OpenBSD uses select ops. 1161 */ 1162 #ifndef _SYS_SYSPROTO_H_ 1163 struct openbsd_poll_args { 1164 struct pollfd *fds; 1165 u_int nfds; 1166 int timeout; 1167 }; 1168 #endif 1169 int 1170 openbsd_poll(td, uap) 1171 register struct thread *td; 1172 register struct openbsd_poll_args *uap; 1173 { 1174 return (poll(td, (struct poll_args *)uap)); 1175 } 1176 1177 /* 1178 * XXX This was created specifically to support netncp and netsmb. This 1179 * allows the caller to specify a socket to wait for events on. It returns 1180 * 0 if any events matched and an error otherwise. There is no way to 1181 * determine which events fired. 1182 */ 1183 int 1184 selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1185 { 1186 struct timeval atv, rtv, ttv; 1187 int error, timo; 1188 1189 if (tvp != NULL) { 1190 atv = *tvp; 1191 if (itimerfix(&atv)) 1192 return (EINVAL); 1193 getmicrouptime(&rtv); 1194 timevaladd(&atv, &rtv); 1195 } else { 1196 atv.tv_sec = 0; 1197 atv.tv_usec = 0; 1198 } 1199 1200 timo = 0; 1201 seltdinit(td); 1202 /* 1203 * Iterate until the timeout expires or the socket becomes ready. 1204 */ 1205 for (;;) { 1206 selfdalloc(td, NULL); 1207 error = sopoll(so, events, NULL, td); 1208 /* error here is actually the ready events. */ 1209 if (error) 1210 return (0); 1211 if (atv.tv_sec || atv.tv_usec) { 1212 getmicrouptime(&rtv); 1213 if (timevalcmp(&rtv, &atv, >=)) { 1214 seltdclear(td); 1215 return (EWOULDBLOCK); 1216 } 1217 ttv = atv; 1218 timevalsub(&ttv, &rtv); 1219 timo = ttv.tv_sec > 24 * 60 * 60 ? 1220 24 * 60 * 60 * hz : tvtohz(&ttv); 1221 } 1222 error = seltdwait(td, timo); 1223 seltdclear(td); 1224 if (error) 1225 break; 1226 } 1227 /* XXX Duplicates ncp/smb behavior. */ 1228 if (error == ERESTART) 1229 error = 0; 1230 return (error); 1231 } 1232 1233 /* 1234 * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1235 * have two select sets, one for read and another for write. 1236 */ 1237 static void 1238 selfdalloc(struct thread *td, void *cookie) 1239 { 1240 struct seltd *stp; 1241 1242 stp = td->td_sel; 1243 if (stp->st_free1 == NULL) 1244 stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1245 stp->st_free1->sf_td = stp; 1246 stp->st_free1->sf_cookie = cookie; 1247 if (stp->st_free2 == NULL) 1248 stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1249 stp->st_free2->sf_td = stp; 1250 stp->st_free2->sf_cookie = cookie; 1251 } 1252 1253 static void 1254 selfdfree(struct seltd *stp, struct selfd *sfp) 1255 { 1256 STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 1257 mtx_lock(sfp->sf_mtx); 1258 if (sfp->sf_si) 1259 TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1260 mtx_unlock(sfp->sf_mtx); 1261 uma_zfree(selfd_zone, sfp); 1262 } 1263 1264 /* 1265 * Record a select request. 1266 */ 1267 void 1268 selrecord(selector, sip) 1269 struct thread *selector; 1270 struct selinfo *sip; 1271 { 1272 struct selfd *sfp; 1273 struct seltd *stp; 1274 struct mtx *mtxp; 1275 1276 stp = selector->td_sel; 1277 /* 1278 * Don't record when doing a rescan. 1279 */ 1280 if (stp->st_flags & SELTD_RESCAN) 1281 return; 1282 /* 1283 * Grab one of the preallocated descriptors. 1284 */ 1285 sfp = NULL; 1286 if ((sfp = stp->st_free1) != NULL) 1287 stp->st_free1 = NULL; 1288 else if ((sfp = stp->st_free2) != NULL) 1289 stp->st_free2 = NULL; 1290 else 1291 panic("selrecord: No free selfd on selq"); 1292 mtxp = mtx_pool_find(mtxpool_sleep, sip); 1293 /* 1294 * Initialize the sfp and queue it in the thread. 1295 */ 1296 sfp->sf_si = sip; 1297 sfp->sf_mtx = mtxp; 1298 STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1299 /* 1300 * Now that we've locked the sip, check for initialization. 1301 */ 1302 mtx_lock(mtxp); 1303 if (sip->si_mtx == NULL) { 1304 sip->si_mtx = mtxp; 1305 TAILQ_INIT(&sip->si_tdlist); 1306 } 1307 /* 1308 * Add this thread to the list of selfds listening on this selinfo. 1309 */ 1310 TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1311 mtx_unlock(sip->si_mtx); 1312 } 1313 1314 /* Wake up a selecting thread. */ 1315 void 1316 selwakeup(sip) 1317 struct selinfo *sip; 1318 { 1319 doselwakeup(sip, -1); 1320 } 1321 1322 /* Wake up a selecting thread, and set its priority. */ 1323 void 1324 selwakeuppri(sip, pri) 1325 struct selinfo *sip; 1326 int pri; 1327 { 1328 doselwakeup(sip, pri); 1329 } 1330 1331 /* 1332 * Do a wakeup when a selectable event occurs. 1333 */ 1334 static void 1335 doselwakeup(sip, pri) 1336 struct selinfo *sip; 1337 int pri; 1338 { 1339 struct selfd *sfp; 1340 struct selfd *sfn; 1341 struct seltd *stp; 1342 1343 /* If it's not initialized there can't be any waiters. */ 1344 if (sip->si_mtx == NULL) 1345 return; 1346 /* 1347 * Locking the selinfo locks all selfds associated with it. 1348 */ 1349 mtx_lock(sip->si_mtx); 1350 TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1351 /* 1352 * Once we remove this sfp from the list and clear the 1353 * sf_si seltdclear will know to ignore this si. 1354 */ 1355 TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1356 sfp->sf_si = NULL; 1357 stp = sfp->sf_td; 1358 mtx_lock(&stp->st_mtx); 1359 stp->st_flags |= SELTD_PENDING; 1360 cv_broadcastpri(&stp->st_wait, pri); 1361 mtx_unlock(&stp->st_mtx); 1362 } 1363 mtx_unlock(sip->si_mtx); 1364 } 1365 1366 static void 1367 seltdinit(struct thread *td) 1368 { 1369 struct seltd *stp; 1370 1371 if ((stp = td->td_sel) != NULL) 1372 goto out; 1373 td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1374 mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1375 cv_init(&stp->st_wait, "select"); 1376 out: 1377 stp->st_flags = 0; 1378 STAILQ_INIT(&stp->st_selq); 1379 } 1380 1381 static int 1382 seltdwait(struct thread *td, int timo) 1383 { 1384 struct seltd *stp; 1385 int error; 1386 1387 stp = td->td_sel; 1388 /* 1389 * An event of interest may occur while we do not hold the seltd 1390 * locked so check the pending flag before we sleep. 1391 */ 1392 mtx_lock(&stp->st_mtx); 1393 /* 1394 * Any further calls to selrecord will be a rescan. 1395 */ 1396 stp->st_flags |= SELTD_RESCAN; 1397 if (stp->st_flags & SELTD_PENDING) { 1398 mtx_unlock(&stp->st_mtx); 1399 return (0); 1400 } 1401 if (timo > 0) 1402 error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo); 1403 else 1404 error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1405 mtx_unlock(&stp->st_mtx); 1406 1407 return (error); 1408 } 1409 1410 void 1411 seltdfini(struct thread *td) 1412 { 1413 struct seltd *stp; 1414 1415 stp = td->td_sel; 1416 if (stp == NULL) 1417 return; 1418 if (stp->st_free1) 1419 uma_zfree(selfd_zone, stp->st_free1); 1420 if (stp->st_free2) 1421 uma_zfree(selfd_zone, stp->st_free2); 1422 td->td_sel = NULL; 1423 free(stp, M_SELECT); 1424 } 1425 1426 /* 1427 * Remove the references to the thread from all of the objects we were 1428 * polling. 1429 */ 1430 static void 1431 seltdclear(struct thread *td) 1432 { 1433 struct seltd *stp; 1434 struct selfd *sfp; 1435 struct selfd *sfn; 1436 1437 stp = td->td_sel; 1438 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1439 selfdfree(stp, sfp); 1440 stp->st_flags = 0; 1441 } 1442 1443 static void selectinit(void *); 1444 SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1445 static void 1446 selectinit(void *dummy __unused) 1447 { 1448 selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1449 NULL, NULL, UMA_ALIGN_PTR, 0); 1450 } 1451