1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 40db6a20e2SGarrett Wollman #include "opt_ktrace.h" 41db6a20e2SGarrett Wollman 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44d2d3e875SBruce Evans #include <sys/sysproto.h> 45df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 4620982410SBruce Evans #include <sys/filio.h> 473ac4d1efSBruce Evans #include <sys/fcntl.h> 48df8bae1dSRodney W. Grimes #include <sys/file.h> 49df8bae1dSRodney W. Grimes #include <sys/proc.h> 50797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 51df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 52df8bae1dSRodney W. Grimes #include <sys/uio.h> 53df8bae1dSRodney W. Grimes #include <sys/kernel.h> 54104a9b7eSAlexander Kabaev #include <sys/limits.h> 55df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5642d11757SPeter Wemm #include <sys/poll.h> 5789b71647SPeter Wemm #include <sys/resourcevar.h> 580a2c3d48SGarrett Wollman #include <sys/selinfo.h> 5944f3b092SJohn Baldwin #include <sys/sleepqueue.h> 608f19eb88SIan Dowse #include <sys/syscallsubr.h> 618cb96f20SPeter Wemm #include <sys/sysctl.h> 6242d11757SPeter Wemm #include <sys/sysent.h> 639bbee259SAndrey A. Chernov #include <sys/vnode.h> 64279d7226SMatthew Dillon #include <sys/bio.h> 65279d7226SMatthew Dillon #include <sys/buf.h> 66265fc98fSSeigo Tanimura #include <sys/condvar.h> 67df8bae1dSRodney W. Grimes #ifdef KTRACE 68df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 69df8bae1dSRodney W. Grimes #endif 70279d7226SMatthew Dillon #include <vm/vm.h> 71279d7226SMatthew Dillon #include <vm/vm_page.h> 72df8bae1dSRodney W. Grimes 73a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 75a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7655166637SPoul-Henning Kamp 77bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 78bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 79bbbb04ceSAlfred Perlstein static int dofileread(struct thread *, struct file *, int, void *, 80bbbb04ceSAlfred Perlstein size_t, off_t, int); 81bbbb04ceSAlfred Perlstein static int dofilewrite(struct thread *, struct file *, int, 82bbbb04ceSAlfred Perlstein const void *, size_t, off_t, int); 83512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 848fe387abSDmitrij Tejblum 85df8bae1dSRodney W. Grimes /* 86df8bae1dSRodney W. Grimes * Read system call. 87df8bae1dSRodney W. Grimes */ 88d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 89df8bae1dSRodney W. Grimes struct read_args { 90df8bae1dSRodney W. Grimes int fd; 91134e06feSBruce Evans void *buf; 92134e06feSBruce Evans size_t nbyte; 93df8bae1dSRodney W. Grimes }; 94d2d3e875SBruce Evans #endif 95ad2edad9SMatthew Dillon /* 96ad2edad9SMatthew Dillon * MPSAFE 97ad2edad9SMatthew Dillon */ 9826f9a767SRodney W. Grimes int 99b40ce416SJulian Elischer read(td, uap) 100b40ce416SJulian Elischer struct thread *td; 101b064d43dSMatthew Dillon struct read_args *uap; 102df8bae1dSRodney W. Grimes { 103b064d43dSMatthew Dillon struct file *fp; 104279d7226SMatthew Dillon int error; 105df8bae1dSRodney W. Grimes 106b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) == 0) { 107b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, 108ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 109b40ce416SJulian Elischer fdrop(fp, td); 110ad2edad9SMatthew Dillon } 111279d7226SMatthew Dillon return(error); 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 114df8bae1dSRodney W. Grimes /* 1158fe387abSDmitrij Tejblum * Pread system call 1164160ccd9SAlan Cox */ 1174160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1184160ccd9SAlan Cox struct pread_args { 1194160ccd9SAlan Cox int fd; 1204160ccd9SAlan Cox void *buf; 1214160ccd9SAlan Cox size_t nbyte; 1228fe387abSDmitrij Tejblum int pad; 1234160ccd9SAlan Cox off_t offset; 1244160ccd9SAlan Cox }; 1254160ccd9SAlan Cox #endif 126ad2edad9SMatthew Dillon /* 127ad2edad9SMatthew Dillon * MPSAFE 128ad2edad9SMatthew Dillon */ 1294160ccd9SAlan Cox int 130b40ce416SJulian Elischer pread(td, uap) 131b40ce416SJulian Elischer struct thread *td; 132b064d43dSMatthew Dillon struct pread_args *uap; 1334160ccd9SAlan Cox { 134b064d43dSMatthew Dillon struct file *fp; 135279d7226SMatthew Dillon int error; 1368fe387abSDmitrij Tejblum 13797fa4397SAlfred Perlstein if ((error = fget_read(td, uap->fd, &fp)) != 0) 13897fa4397SAlfred Perlstein return (error); 1399bbee259SAndrey A. Chernov if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 140b064d43dSMatthew Dillon error = ESPIPE; 1419bbee259SAndrey A. Chernov else if (uap->offset < 0 && fp->f_vnode->v_type != VCHR) 1429bbee259SAndrey A. Chernov error = EINVAL; 1439bbee259SAndrey A. Chernov else { 144426da3bcSAlfred Perlstein error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte, 145426da3bcSAlfred Perlstein uap->offset, FOF_OFFSET); 146b064d43dSMatthew Dillon } 147b40ce416SJulian Elischer fdrop(fp, td); 148279d7226SMatthew Dillon return(error); 1498fe387abSDmitrij Tejblum } 1508fe387abSDmitrij Tejblum 1518fe387abSDmitrij Tejblum /* 1528fe387abSDmitrij Tejblum * Code common for read and pread 1538fe387abSDmitrij Tejblum */ 15437c84183SPoul-Henning Kamp static int 155b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 156b40ce416SJulian Elischer struct thread *td; 1578fe387abSDmitrij Tejblum struct file *fp; 1588fe387abSDmitrij Tejblum int fd, flags; 1598fe387abSDmitrij Tejblum void *buf; 1608fe387abSDmitrij Tejblum size_t nbyte; 1618fe387abSDmitrij Tejblum off_t offset; 1628fe387abSDmitrij Tejblum { 1634160ccd9SAlan Cox struct uio auio; 1644160ccd9SAlan Cox struct iovec aiov; 1654160ccd9SAlan Cox long cnt, error = 0; 1664160ccd9SAlan Cox #ifdef KTRACE 167552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 1684160ccd9SAlan Cox #endif 1694160ccd9SAlan Cox 1700a3e28cfSAlfred Perlstein aiov.iov_base = buf; 1718fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 1724160ccd9SAlan Cox auio.uio_iov = &aiov; 1734160ccd9SAlan Cox auio.uio_iovcnt = 1; 1748fe387abSDmitrij Tejblum auio.uio_offset = offset; 1758fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 1764160ccd9SAlan Cox return (EINVAL); 1778fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 1784160ccd9SAlan Cox auio.uio_rw = UIO_READ; 1794160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 180b40ce416SJulian Elischer auio.uio_td = td; 1814160ccd9SAlan Cox #ifdef KTRACE 182552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 183552afd9cSPoul-Henning Kamp ktruio = cloneuio(&auio); 1844160ccd9SAlan Cox #endif 1858fe387abSDmitrij Tejblum cnt = nbyte; 186279d7226SMatthew Dillon 1879ca43589SRobert Watson if ((error = fo_read(fp, &auio, td->td_ucred, flags, td))) { 1884160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 1894160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 1904160ccd9SAlan Cox error = 0; 191279d7226SMatthew Dillon } 1924160ccd9SAlan Cox cnt -= auio.uio_resid; 1934160ccd9SAlan Cox #ifdef KTRACE 194552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 195552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 196552afd9cSPoul-Henning Kamp ktrgenio(fd, UIO_READ, ktruio, error); 19742ebfbf2SBrian Feldman } 1984160ccd9SAlan Cox #endif 199b40ce416SJulian Elischer td->td_retval[0] = cnt; 2004160ccd9SAlan Cox return (error); 2014160ccd9SAlan Cox } 2024160ccd9SAlan Cox 2034160ccd9SAlan Cox /* 204df8bae1dSRodney W. Grimes * Scatter read system call. 205df8bae1dSRodney W. Grimes */ 206d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 207df8bae1dSRodney W. Grimes struct readv_args { 2087147b19dSBruce Evans int fd; 209df8bae1dSRodney W. Grimes struct iovec *iovp; 210df8bae1dSRodney W. Grimes u_int iovcnt; 211df8bae1dSRodney W. Grimes }; 212d2d3e875SBruce Evans #endif 213ad2edad9SMatthew Dillon /* 214ad2edad9SMatthew Dillon * MPSAFE 215ad2edad9SMatthew Dillon */ 21626f9a767SRodney W. Grimes int 217552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap) 218df8bae1dSRodney W. Grimes { 219b064d43dSMatthew Dillon struct file *fp; 220552afd9cSPoul-Henning Kamp struct uio *auio = NULL; 221552afd9cSPoul-Henning Kamp long cnt; 22282641acdSAlan Cox int error; 223df8bae1dSRodney W. Grimes #ifdef KTRACE 224552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 225df8bae1dSRodney W. Grimes #endif 226df8bae1dSRodney W. Grimes 227552afd9cSPoul-Henning Kamp error = fget_read(td, uap->fd, &fp); 228552afd9cSPoul-Henning Kamp if (error) 22982641acdSAlan Cox return (error); 230552afd9cSPoul-Henning Kamp error = copyinuio(uap->iovp, uap->iovcnt, &auio); 231552afd9cSPoul-Henning Kamp if (error) { 232552afd9cSPoul-Henning Kamp fdrop(fp, td); 233552afd9cSPoul-Henning Kamp return (error); 234ad2edad9SMatthew Dillon } 235552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 236552afd9cSPoul-Henning Kamp auio->uio_td = td; 237df8bae1dSRodney W. Grimes #ifdef KTRACE 238552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 239552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 240df8bae1dSRodney W. Grimes #endif 241552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 242552afd9cSPoul-Henning Kamp if ((error = fo_read(fp, auio, td->td_ucred, 0, td))) { 243552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 244df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 245df8bae1dSRodney W. Grimes error = 0; 246279d7226SMatthew Dillon } 247552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 248df8bae1dSRodney W. Grimes #ifdef KTRACE 249552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 250552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 251552afd9cSPoul-Henning Kamp ktrgenio(uap->fd, UIO_READ, ktruio, error); 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes #endif 254b40ce416SJulian Elischer td->td_retval[0] = cnt; 255552afd9cSPoul-Henning Kamp free(auio, M_IOV); 256b40ce416SJulian Elischer fdrop(fp, td); 257df8bae1dSRodney W. Grimes return (error); 258df8bae1dSRodney W. Grimes } 259df8bae1dSRodney W. Grimes 260df8bae1dSRodney W. Grimes /* 261df8bae1dSRodney W. Grimes * Write system call 262df8bae1dSRodney W. Grimes */ 263d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 264df8bae1dSRodney W. Grimes struct write_args { 265df8bae1dSRodney W. Grimes int fd; 266134e06feSBruce Evans const void *buf; 267134e06feSBruce Evans size_t nbyte; 268df8bae1dSRodney W. Grimes }; 269d2d3e875SBruce Evans #endif 270ad2edad9SMatthew Dillon /* 271ad2edad9SMatthew Dillon * MPSAFE 272ad2edad9SMatthew Dillon */ 27326f9a767SRodney W. Grimes int 274b40ce416SJulian Elischer write(td, uap) 275b40ce416SJulian Elischer struct thread *td; 276b064d43dSMatthew Dillon struct write_args *uap; 277df8bae1dSRodney W. Grimes { 278b064d43dSMatthew Dillon struct file *fp; 279279d7226SMatthew Dillon int error; 280df8bae1dSRodney W. Grimes 281b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 282b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 283ad2edad9SMatthew Dillon (off_t)-1, 0); 284b40ce416SJulian Elischer fdrop(fp, td); 285ad2edad9SMatthew Dillon } else { 286b064d43dSMatthew Dillon error = EBADF; /* XXX this can't be right */ 287ad2edad9SMatthew Dillon } 288279d7226SMatthew Dillon return(error); 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes /* 2928fe387abSDmitrij Tejblum * Pwrite system call 2934160ccd9SAlan Cox */ 2944160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 2954160ccd9SAlan Cox struct pwrite_args { 2964160ccd9SAlan Cox int fd; 2974160ccd9SAlan Cox const void *buf; 2984160ccd9SAlan Cox size_t nbyte; 2998fe387abSDmitrij Tejblum int pad; 3004160ccd9SAlan Cox off_t offset; 3014160ccd9SAlan Cox }; 3024160ccd9SAlan Cox #endif 303ad2edad9SMatthew Dillon /* 304ad2edad9SMatthew Dillon * MPSAFE 305ad2edad9SMatthew Dillon */ 3064160ccd9SAlan Cox int 307b40ce416SJulian Elischer pwrite(td, uap) 308b40ce416SJulian Elischer struct thread *td; 309b064d43dSMatthew Dillon struct pwrite_args *uap; 3104160ccd9SAlan Cox { 311b064d43dSMatthew Dillon struct file *fp; 312279d7226SMatthew Dillon int error; 3138fe387abSDmitrij Tejblum 314b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 3159bbee259SAndrey A. Chernov if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 3162db4b023SPoul-Henning Kamp error = ESPIPE; 3179bbee259SAndrey A. Chernov else if (uap->offset < 0 && fp->f_vnode->v_type != VCHR) 3189bbee259SAndrey A. Chernov error = EINVAL; 3199bbee259SAndrey A. Chernov else { 320b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 321b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 322b064d43dSMatthew Dillon } 323b40ce416SJulian Elischer fdrop(fp, td); 324279d7226SMatthew Dillon } else { 325b064d43dSMatthew Dillon error = EBADF; /* this can't be right */ 326ad2edad9SMatthew Dillon } 327279d7226SMatthew Dillon return(error); 3288fe387abSDmitrij Tejblum } 3298fe387abSDmitrij Tejblum 3308fe387abSDmitrij Tejblum static int 331b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 332b40ce416SJulian Elischer struct thread *td; 3338fe387abSDmitrij Tejblum struct file *fp; 3348fe387abSDmitrij Tejblum int fd, flags; 3358fe387abSDmitrij Tejblum const void *buf; 3368fe387abSDmitrij Tejblum size_t nbyte; 3378fe387abSDmitrij Tejblum off_t offset; 3388fe387abSDmitrij Tejblum { 3394160ccd9SAlan Cox struct uio auio; 3404160ccd9SAlan Cox struct iovec aiov; 3414160ccd9SAlan Cox long cnt, error = 0; 3424160ccd9SAlan Cox #ifdef KTRACE 343552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 3444160ccd9SAlan Cox #endif 3454160ccd9SAlan Cox 346b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 3478fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 3484160ccd9SAlan Cox auio.uio_iov = &aiov; 3494160ccd9SAlan Cox auio.uio_iovcnt = 1; 3508fe387abSDmitrij Tejblum auio.uio_offset = offset; 3518fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 3524160ccd9SAlan Cox return (EINVAL); 3538fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 3544160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 3554160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 356b40ce416SJulian Elischer auio.uio_td = td; 3574160ccd9SAlan Cox #ifdef KTRACE 358552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 359552afd9cSPoul-Henning Kamp ktruio = cloneuio(&auio); 3604160ccd9SAlan Cox #endif 3618fe387abSDmitrij Tejblum cnt = nbyte; 362c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 363279d7226SMatthew Dillon bwillwrite(); 3649ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, flags, td))) { 3654160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 3664160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 3674160ccd9SAlan Cox error = 0; 368c33c8251SAlfred Perlstein /* Socket layer is responsible for issuing SIGPIPE. */ 369c33c8251SAlfred Perlstein if (error == EPIPE && fp->f_type != DTYPE_SOCKET) { 370b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 371b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 372b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 37319eb87d2SJohn Baldwin } 3744160ccd9SAlan Cox } 3754160ccd9SAlan Cox cnt -= auio.uio_resid; 3764160ccd9SAlan Cox #ifdef KTRACE 377552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 378552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 379552afd9cSPoul-Henning Kamp ktrgenio(fd, UIO_WRITE, ktruio, error); 38042ebfbf2SBrian Feldman } 3814160ccd9SAlan Cox #endif 382b40ce416SJulian Elischer td->td_retval[0] = cnt; 3834160ccd9SAlan Cox return (error); 3844160ccd9SAlan Cox } 3854160ccd9SAlan Cox 3864160ccd9SAlan Cox /* 387df8bae1dSRodney W. Grimes * Gather write system call 388df8bae1dSRodney W. Grimes */ 389d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 390df8bae1dSRodney W. Grimes struct writev_args { 391df8bae1dSRodney W. Grimes int fd; 392df8bae1dSRodney W. Grimes struct iovec *iovp; 393df8bae1dSRodney W. Grimes u_int iovcnt; 394df8bae1dSRodney W. Grimes }; 395d2d3e875SBruce Evans #endif 396ad2edad9SMatthew Dillon /* 397ad2edad9SMatthew Dillon * MPSAFE 398ad2edad9SMatthew Dillon */ 39926f9a767SRodney W. Grimes int 400552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap) 401df8bae1dSRodney W. Grimes { 402b064d43dSMatthew Dillon struct file *fp; 403552afd9cSPoul-Henning Kamp struct uio *auio = NULL; 404552afd9cSPoul-Henning Kamp long cnt; 405552afd9cSPoul-Henning Kamp int error; 406df8bae1dSRodney W. Grimes #ifdef KTRACE 407552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 408df8bae1dSRodney W. Grimes #endif 409df8bae1dSRodney W. Grimes 410552afd9cSPoul-Henning Kamp error = fget_write(td, uap->fd, &fp); 411552afd9cSPoul-Henning Kamp if (error) 412882d8469SAlan Cox return (EBADF); 413552afd9cSPoul-Henning Kamp error = copyinuio(uap->iovp, uap->iovcnt, &auio); 414552afd9cSPoul-Henning Kamp if (error) { 415552afd9cSPoul-Henning Kamp fdrop(fp, td); 416552afd9cSPoul-Henning Kamp return (error); 4171aa3e7ddSBrian Feldman } 418552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 419552afd9cSPoul-Henning Kamp auio->uio_td = td; 420df8bae1dSRodney W. Grimes #ifdef KTRACE 421552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 422552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 423df8bae1dSRodney W. Grimes #endif 424552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 425a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 4269440653dSMatthew Dillon bwillwrite(); 427552afd9cSPoul-Henning Kamp if ((error = fo_write(fp, auio, td->td_ucred, 0, td))) { 428552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 429df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 430df8bae1dSRodney W. Grimes error = 0; 43119eb87d2SJohn Baldwin if (error == EPIPE) { 432b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 433b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 434b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 43519eb87d2SJohn Baldwin } 436df8bae1dSRodney W. Grimes } 437552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 438df8bae1dSRodney W. Grimes #ifdef KTRACE 439552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 440552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 441552afd9cSPoul-Henning Kamp ktrgenio(uap->fd, UIO_WRITE, ktruio, error); 442df8bae1dSRodney W. Grimes } 443df8bae1dSRodney W. Grimes #endif 444b40ce416SJulian Elischer td->td_retval[0] = cnt; 445b40ce416SJulian Elischer fdrop(fp, td); 446552afd9cSPoul-Henning Kamp free(auio, M_IOV); 447df8bae1dSRodney W. Grimes return (error); 448df8bae1dSRodney W. Grimes } 449df8bae1dSRodney W. Grimes 450df8bae1dSRodney W. Grimes /* 451df8bae1dSRodney W. Grimes * Ioctl system call 452df8bae1dSRodney W. Grimes */ 453d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 454df8bae1dSRodney W. Grimes struct ioctl_args { 455df8bae1dSRodney W. Grimes int fd; 456069e9bc1SDoug Rabson u_long com; 457df8bae1dSRodney W. Grimes caddr_t data; 458df8bae1dSRodney W. Grimes }; 459d2d3e875SBruce Evans #endif 460ad2edad9SMatthew Dillon /* 461ad2edad9SMatthew Dillon * MPSAFE 462ad2edad9SMatthew Dillon */ 463df8bae1dSRodney W. Grimes /* ARGSUSED */ 46426f9a767SRodney W. Grimes int 4653e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap) 466df8bae1dSRodney W. Grimes { 467a4db4953SAlfred Perlstein struct file *fp; 4683e15c66fSPoul-Henning Kamp struct filedesc *fdp; 4693e15c66fSPoul-Henning Kamp u_long com; 470ad2edad9SMatthew Dillon int error = 0; 4713e15c66fSPoul-Henning Kamp u_int size; 472df8bae1dSRodney W. Grimes caddr_t data, memp; 473df8bae1dSRodney W. Grimes int tmp; 474df8bae1dSRodney W. Grimes 475a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 476a4db4953SAlfred Perlstein return (error); 477aa11a498SAlfred Perlstein mtx_lock(&Giant); 478ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 479426da3bcSAlfred Perlstein fdrop(fp, td); 480aa11a498SAlfred Perlstein mtx_unlock(&Giant); 481426da3bcSAlfred Perlstein return (EBADF); 482ad2edad9SMatthew Dillon } 483426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 484df8bae1dSRodney W. Grimes switch (com = uap->com) { 485df8bae1dSRodney W. Grimes case FIONCLEX: 486124e4c3bSPoul-Henning Kamp FILEDESC_LOCK_FAST(fdp); 487df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 488124e4c3bSPoul-Henning Kamp FILEDESC_UNLOCK_FAST(fdp); 489426da3bcSAlfred Perlstein fdrop(fp, td); 490aa11a498SAlfred Perlstein mtx_unlock(&Giant); 491426da3bcSAlfred Perlstein return (0); 492df8bae1dSRodney W. Grimes case FIOCLEX: 493124e4c3bSPoul-Henning Kamp FILEDESC_LOCK_FAST(fdp); 494df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 495124e4c3bSPoul-Henning Kamp FILEDESC_UNLOCK_FAST(fdp); 496426da3bcSAlfred Perlstein fdrop(fp, td); 497aa11a498SAlfred Perlstein mtx_unlock(&Giant); 498426da3bcSAlfred Perlstein return (0); 499df8bae1dSRodney W. Grimes } 500df8bae1dSRodney W. Grimes 501df8bae1dSRodney W. Grimes /* 502df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 503df8bae1dSRodney W. Grimes * copied to/from the user's address space. 504df8bae1dSRodney W. Grimes */ 505df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 506ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 507ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 508ca51b19bSPoul-Henning Kamp ((com & IOC_VOID) && size > 0) || 509ca51b19bSPoul-Henning Kamp ((com & (IOC_IN | IOC_OUT)) && size == 0)) { 510426da3bcSAlfred Perlstein fdrop(fp, td); 511aa11a498SAlfred Perlstein mtx_unlock(&Giant); 512426da3bcSAlfred Perlstein return (ENOTTY); 513ad2edad9SMatthew Dillon } 514279d7226SMatthew Dillon 515ca51b19bSPoul-Henning Kamp if (size > 0) { 516a163d034SWarner Losh memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 517df8bae1dSRodney W. Grimes data = memp; 518279d7226SMatthew Dillon } else { 519ca51b19bSPoul-Henning Kamp memp = NULL; 520ca51b19bSPoul-Henning Kamp data = (void *)&uap->data; 521279d7226SMatthew Dillon } 522df8bae1dSRodney W. Grimes if (com & IOC_IN) { 523df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 524df8bae1dSRodney W. Grimes if (error) { 525df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 526b40ce416SJulian Elischer fdrop(fp, td); 5273e15c66fSPoul-Henning Kamp mtx_unlock(&Giant); 5283e15c66fSPoul-Henning Kamp return (error); 529df8bae1dSRodney W. Grimes } 530ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 531df8bae1dSRodney W. Grimes /* 532df8bae1dSRodney W. Grimes * Zero the buffer so the user always 533df8bae1dSRodney W. Grimes * gets back something deterministic. 534df8bae1dSRodney W. Grimes */ 535df8bae1dSRodney W. Grimes bzero(data, size); 536279d7226SMatthew Dillon } 537df8bae1dSRodney W. Grimes 5388ccf264fSPoul-Henning Kamp if (com == FIONBIO) { 539426da3bcSAlfred Perlstein FILE_LOCK(fp); 540bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 541df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 542df8bae1dSRodney W. Grimes else 543df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 544426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 5458ccf264fSPoul-Henning Kamp data = (void *)&tmp; 5468ccf264fSPoul-Henning Kamp } else if (com == FIOASYNC) { 547426da3bcSAlfred Perlstein FILE_LOCK(fp); 548bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 549df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 550df8bae1dSRodney W. Grimes else 551df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 552426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 5538ccf264fSPoul-Henning Kamp data = (void *)&tmp; 554df8bae1dSRodney W. Grimes } 5558ccf264fSPoul-Henning Kamp 5568ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 5578ccf264fSPoul-Henning Kamp 5588ccf264fSPoul-Henning Kamp if (error == 0 && (com & IOC_OUT)) 5598ccf264fSPoul-Henning Kamp error = copyout(data, uap->data, (u_int)size); 5608ccf264fSPoul-Henning Kamp 561ca51b19bSPoul-Henning Kamp if (memp != NULL) 562df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 563b40ce416SJulian Elischer fdrop(fp, td); 564ad2edad9SMatthew Dillon mtx_unlock(&Giant); 565df8bae1dSRodney W. Grimes return (error); 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes 56885f190e4SAlfred Perlstein /* 56985f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 57085f190e4SAlfred Perlstein */ 57185f190e4SAlfred Perlstein struct mtx sellock; 572265fc98fSSeigo Tanimura struct cv selwait; 5739ae6d334SKelly Yancey u_int nselcoll; /* Select collisions since boot */ 5749ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 575df8bae1dSRodney W. Grimes 576df8bae1dSRodney W. Grimes /* 577df8bae1dSRodney W. Grimes * Select system call. 578df8bae1dSRodney W. Grimes */ 579d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 580df8bae1dSRodney W. Grimes struct select_args { 581b08f7993SSujal Patel int nd; 582df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 583df8bae1dSRodney W. Grimes struct timeval *tv; 584df8bae1dSRodney W. Grimes }; 585d2d3e875SBruce Evans #endif 586ad2edad9SMatthew Dillon /* 587ad2edad9SMatthew Dillon * MPSAFE 588ad2edad9SMatthew Dillon */ 58926f9a767SRodney W. Grimes int 590b40ce416SJulian Elischer select(td, uap) 591b40ce416SJulian Elischer register struct thread *td; 592df8bae1dSRodney W. Grimes register struct select_args *uap; 593df8bae1dSRodney W. Grimes { 5948f19eb88SIan Dowse struct timeval tv, *tvp; 5958f19eb88SIan Dowse int error; 5968f19eb88SIan Dowse 5978f19eb88SIan Dowse if (uap->tv != NULL) { 5988f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 5998f19eb88SIan Dowse if (error) 6008f19eb88SIan Dowse return (error); 6018f19eb88SIan Dowse tvp = &tv; 6028f19eb88SIan Dowse } else 6038f19eb88SIan Dowse tvp = NULL; 6048f19eb88SIan Dowse 6058f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 6068f19eb88SIan Dowse } 6078f19eb88SIan Dowse 6088f19eb88SIan Dowse int 6098f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 6108f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 6118f19eb88SIan Dowse { 612426da3bcSAlfred Perlstein struct filedesc *fdp; 613d5e4d7e1SBruce Evans /* 614d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 615d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 616d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 617d5e4d7e1SBruce Evans * of 256. 618d5e4d7e1SBruce Evans */ 619d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 620eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 62100af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 6229ae6d334SKelly Yancey int error, timo; 6239ae6d334SKelly Yancey u_int ncoll, nbufbytes, ncpbytes, nfdbits; 624df8bae1dSRodney W. Grimes 6258f19eb88SIan Dowse if (nd < 0) 626acbfbfeaSSujal Patel return (EINVAL); 627426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 6285d8dd01dSRobert Watson /* 6295d8dd01dSRobert Watson * XXX: kern_select() currently requires that we acquire Giant 6305d8dd01dSRobert Watson * even if none of the file descriptors we poll requires Giant. 6315d8dd01dSRobert Watson */ 632ad2edad9SMatthew Dillon mtx_lock(&Giant); 633124e4c3bSPoul-Henning Kamp FILEDESC_LOCK_FAST(fdp); 634ad2edad9SMatthew Dillon 6358f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 6368f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 637124e4c3bSPoul-Henning Kamp FILEDESC_UNLOCK_FAST(fdp); 638b08f7993SSujal Patel 639d5e4d7e1SBruce Evans /* 640d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 641d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 642d5e4d7e1SBruce Evans */ 6438f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 644d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 645d5e4d7e1SBruce Evans nbufbytes = 0; 6468f19eb88SIan Dowse if (fd_in != NULL) 647d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 6488f19eb88SIan Dowse if (fd_ou != NULL) 649d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 6508f19eb88SIan Dowse if (fd_ex != NULL) 651d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 652d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 653d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 654d5e4d7e1SBruce Evans else 655a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 656b08f7993SSujal Patel 657b08f7993SSujal Patel /* 658d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 659d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 660d5e4d7e1SBruce Evans * together. 661b08f7993SSujal Patel */ 662d5e4d7e1SBruce Evans sbp = selbits; 663df8bae1dSRodney W. Grimes #define getbits(name, x) \ 664d5e4d7e1SBruce Evans do { \ 6658f19eb88SIan Dowse if (name == NULL) \ 666d5e4d7e1SBruce Evans ibits[x] = NULL; \ 667d5e4d7e1SBruce Evans else { \ 668d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 669d5e4d7e1SBruce Evans obits[x] = sbp; \ 670d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 6718f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 672265fc98fSSeigo Tanimura if (error != 0) \ 67385f190e4SAlfred Perlstein goto done_nosellock; \ 674e04ac2feSJohn Baldwin } \ 675d5e4d7e1SBruce Evans } while (0) 6768f19eb88SIan Dowse getbits(fd_in, 0); 6778f19eb88SIan Dowse getbits(fd_ou, 1); 6788f19eb88SIan Dowse getbits(fd_ex, 2); 679df8bae1dSRodney W. Grimes #undef getbits 680d5e4d7e1SBruce Evans if (nbufbytes != 0) 681d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 682df8bae1dSRodney W. Grimes 6838f19eb88SIan Dowse if (tvp != NULL) { 6848f19eb88SIan Dowse atv = *tvp; 685df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 686df8bae1dSRodney W. Grimes error = EINVAL; 68785f190e4SAlfred Perlstein goto done_nosellock; 688df8bae1dSRodney W. Grimes } 689c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 69000af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 6919c386f6bSJohn Baldwin } else { 69200af9731SPoul-Henning Kamp atv.tv_sec = 0; 6939c386f6bSJohn Baldwin atv.tv_usec = 0; 6949c386f6bSJohn Baldwin } 69500af9731SPoul-Henning Kamp timo = 0; 6962149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 69785f190e4SAlfred Perlstein mtx_lock(&sellock); 698df8bae1dSRodney W. Grimes retry: 699df8bae1dSRodney W. Grimes ncoll = nselcoll; 700fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 701b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 702fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 70385f190e4SAlfred Perlstein mtx_unlock(&sellock); 70485f190e4SAlfred Perlstein 7058f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 70685f190e4SAlfred Perlstein mtx_lock(&sellock); 707b40ce416SJulian Elischer if (error || td->td_retval[0]) 708df8bae1dSRodney W. Grimes goto done; 7094da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 710c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 71185f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 712df8bae1dSRodney W. Grimes goto done; 71300af9731SPoul-Henning Kamp ttv = atv; 71400af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 71500af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 71600af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 717df8bae1dSRodney W. Grimes } 71885f190e4SAlfred Perlstein 71985f190e4SAlfred Perlstein /* 72085f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 72185f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 72285f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 72385f190e4SAlfred Perlstein * necessary. 72485f190e4SAlfred Perlstein */ 725fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 72685f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 72785f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 72885f190e4SAlfred Perlstein goto retry; 72985f190e4SAlfred Perlstein } 730fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 731bfbbc4aaSJason Evans 732265fc98fSSeigo Tanimura if (timo > 0) 73385f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 734265fc98fSSeigo Tanimura else 73585f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 736bfbbc4aaSJason Evans 737df8bae1dSRodney W. Grimes if (error == 0) 738df8bae1dSRodney W. Grimes goto retry; 739265fc98fSSeigo Tanimura 740df8bae1dSRodney W. Grimes done: 74185f190e4SAlfred Perlstein clear_selinfo_list(td); 742fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 743b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 744fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 74585f190e4SAlfred Perlstein mtx_unlock(&sellock); 74685f190e4SAlfred Perlstein 74785f190e4SAlfred Perlstein done_nosellock: 748df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 749df8bae1dSRodney W. Grimes if (error == ERESTART) 750df8bae1dSRodney W. Grimes error = EINTR; 751df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 752df8bae1dSRodney W. Grimes error = 0; 753df8bae1dSRodney W. Grimes #define putbits(name, x) \ 7548f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 755df8bae1dSRodney W. Grimes error = error2; 756df8bae1dSRodney W. Grimes if (error == 0) { 757df8bae1dSRodney W. Grimes int error2; 758df8bae1dSRodney W. Grimes 7598f19eb88SIan Dowse putbits(fd_in, 0); 7608f19eb88SIan Dowse putbits(fd_ou, 1); 7618f19eb88SIan Dowse putbits(fd_ex, 2); 762df8bae1dSRodney W. Grimes #undef putbits 763df8bae1dSRodney W. Grimes } 764d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 765d5e4d7e1SBruce Evans free(selbits, M_SELECT); 766ad2edad9SMatthew Dillon 767ad2edad9SMatthew Dillon mtx_unlock(&Giant); 768df8bae1dSRodney W. Grimes return (error); 769df8bae1dSRodney W. Grimes } 770df8bae1dSRodney W. Grimes 771265fc98fSSeigo Tanimura static int 772b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 773b40ce416SJulian Elischer struct thread *td; 774b08f7993SSujal Patel fd_mask **ibits, **obits; 775cb226aaaSPoul-Henning Kamp int nfd; 776df8bae1dSRodney W. Grimes { 777f082218cSPeter Wemm int msk, i, fd; 778f082218cSPeter Wemm fd_mask bits; 779df8bae1dSRodney W. Grimes struct file *fp; 780df8bae1dSRodney W. Grimes int n = 0; 7812087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 78242d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 783eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 784df8bae1dSRodney W. Grimes 785eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 786df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 787d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 788d5e4d7e1SBruce Evans continue; 789df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 790b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 791f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 792f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 793f082218cSPeter Wemm if (!(bits & 1)) 794f082218cSPeter Wemm continue; 795eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 796eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 797df8bae1dSRodney W. Grimes return (EBADF); 798eb209311SAlfred Perlstein } 799ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 800ea6027a8SRobert Watson td)) { 801b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 802f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 803df8bae1dSRodney W. Grimes n++; 804df8bae1dSRodney W. Grimes } 805df8bae1dSRodney W. Grimes } 806df8bae1dSRodney W. Grimes } 807df8bae1dSRodney W. Grimes } 808eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 809b40ce416SJulian Elischer td->td_retval[0] = n; 810df8bae1dSRodney W. Grimes return (0); 811df8bae1dSRodney W. Grimes } 812df8bae1dSRodney W. Grimes 81342d11757SPeter Wemm /* 81442d11757SPeter Wemm * Poll system call. 81542d11757SPeter Wemm */ 81642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 81742d11757SPeter Wemm struct poll_args { 81842d11757SPeter Wemm struct pollfd *fds; 81942d11757SPeter Wemm u_int nfds; 82042d11757SPeter Wemm int timeout; 82142d11757SPeter Wemm }; 82242d11757SPeter Wemm #endif 823ad2edad9SMatthew Dillon /* 824ad2edad9SMatthew Dillon * MPSAFE 825ad2edad9SMatthew Dillon */ 82642d11757SPeter Wemm int 827b40ce416SJulian Elischer poll(td, uap) 828b40ce416SJulian Elischer struct thread *td; 829ea0237edSJonathan Lemon struct poll_args *uap; 83042d11757SPeter Wemm { 8312580f4e5SAndre Oppermann struct pollfd *bits; 8322580f4e5SAndre Oppermann struct pollfd smallbits[32]; 83300af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 8349ae6d334SKelly Yancey int error = 0, timo; 8359ae6d334SKelly Yancey u_int ncoll, nfds; 83642d11757SPeter Wemm size_t ni; 83742d11757SPeter Wemm 838d1e405c5SAlfred Perlstein nfds = uap->nfds; 839ad2edad9SMatthew Dillon 8405d8dd01dSRobert Watson /* 8415d8dd01dSRobert Watson * XXX: poll() currently requires that we acquire Giant even if 8425d8dd01dSRobert Watson * none of the file descriptors we poll requires Giant. 8435d8dd01dSRobert Watson */ 844ad2edad9SMatthew Dillon mtx_lock(&Giant); 84589b71647SPeter Wemm /* 8462bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 8472bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 8482bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 8492bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 8502bd5ac33SPeter Wemm * safe, but not overly restrictive. 85189b71647SPeter Wemm */ 85291d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 85391d5354aSJohn Baldwin if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) && 854b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 85591d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 856ad2edad9SMatthew Dillon error = EINVAL; 857ad2edad9SMatthew Dillon goto done2; 858ad2edad9SMatthew Dillon } 85991d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 86089b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 86142d11757SPeter Wemm if (ni > sizeof(smallbits)) 862a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 86342d11757SPeter Wemm else 86442d11757SPeter Wemm bits = smallbits; 865d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 86642d11757SPeter Wemm if (error) 86785f190e4SAlfred Perlstein goto done_nosellock; 868d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 869d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 870d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 87142d11757SPeter Wemm if (itimerfix(&atv)) { 87242d11757SPeter Wemm error = EINVAL; 87385f190e4SAlfred Perlstein goto done_nosellock; 87442d11757SPeter Wemm } 875c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 87600af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8779c386f6bSJohn Baldwin } else { 87800af9731SPoul-Henning Kamp atv.tv_sec = 0; 8799c386f6bSJohn Baldwin atv.tv_usec = 0; 8809c386f6bSJohn Baldwin } 88100af9731SPoul-Henning Kamp timo = 0; 8822149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 88385f190e4SAlfred Perlstein mtx_lock(&sellock); 88442d11757SPeter Wemm retry: 88542d11757SPeter Wemm ncoll = nselcoll; 886fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 887b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 888fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 88985f190e4SAlfred Perlstein mtx_unlock(&sellock); 89085f190e4SAlfred Perlstein 8912580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 89285f190e4SAlfred Perlstein mtx_lock(&sellock); 893b40ce416SJulian Elischer if (error || td->td_retval[0]) 89442d11757SPeter Wemm goto done; 8954da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 896c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 89785f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 89842d11757SPeter Wemm goto done; 89900af9731SPoul-Henning Kamp ttv = atv; 90000af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 90100af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 90200af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 90342d11757SPeter Wemm } 90485f190e4SAlfred Perlstein /* 90585f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 90685f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 90785f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 90885f190e4SAlfred Perlstein */ 909fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 91085f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 911fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 91285f190e4SAlfred Perlstein goto retry; 91385f190e4SAlfred Perlstein } 91485f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 91585f190e4SAlfred Perlstein 916265fc98fSSeigo Tanimura if (timo > 0) 91785f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 918265fc98fSSeigo Tanimura else 91985f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 92085f190e4SAlfred Perlstein 92142d11757SPeter Wemm if (error == 0) 92242d11757SPeter Wemm goto retry; 923265fc98fSSeigo Tanimura 92442d11757SPeter Wemm done: 92585f190e4SAlfred Perlstein clear_selinfo_list(td); 926fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 927b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 928fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 92985f190e4SAlfred Perlstein mtx_unlock(&sellock); 93085f190e4SAlfred Perlstein 93185f190e4SAlfred Perlstein done_nosellock: 93242d11757SPeter Wemm /* poll is not restarted after signals... */ 93342d11757SPeter Wemm if (error == ERESTART) 93442d11757SPeter Wemm error = EINTR; 93542d11757SPeter Wemm if (error == EWOULDBLOCK) 93642d11757SPeter Wemm error = 0; 93742d11757SPeter Wemm if (error == 0) { 938d1e405c5SAlfred Perlstein error = copyout(bits, uap->fds, ni); 93942d11757SPeter Wemm if (error) 94042d11757SPeter Wemm goto out; 94142d11757SPeter Wemm } 94242d11757SPeter Wemm out: 94342d11757SPeter Wemm if (ni > sizeof(smallbits)) 94442d11757SPeter Wemm free(bits, M_TEMP); 945ad2edad9SMatthew Dillon done2: 946ad2edad9SMatthew Dillon mtx_unlock(&Giant); 94742d11757SPeter Wemm return (error); 94842d11757SPeter Wemm } 94942d11757SPeter Wemm 95042d11757SPeter Wemm static int 951b40ce416SJulian Elischer pollscan(td, fds, nfd) 952b40ce416SJulian Elischer struct thread *td; 95342d11757SPeter Wemm struct pollfd *fds; 954ea0237edSJonathan Lemon u_int nfd; 95542d11757SPeter Wemm { 956b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 95742d11757SPeter Wemm int i; 95842d11757SPeter Wemm struct file *fp; 95942d11757SPeter Wemm int n = 0; 96042d11757SPeter Wemm 961426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 962eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 963337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 96442d11757SPeter Wemm fds->revents = POLLNVAL; 96542d11757SPeter Wemm n++; 966337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 967337c9691SJordan K. Hubbard fds->revents = 0; 96842d11757SPeter Wemm } else { 96942d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 970279d7226SMatthew Dillon if (fp == NULL) { 97142d11757SPeter Wemm fds->revents = POLLNVAL; 97242d11757SPeter Wemm n++; 97342d11757SPeter Wemm } else { 9742087c896SBruce Evans /* 9752087c896SBruce Evans * Note: backend also returns POLLHUP and 9762087c896SBruce Evans * POLLERR if appropriate. 9772087c896SBruce Evans */ 97813ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 979ea6027a8SRobert Watson td->td_ucred, td); 98042d11757SPeter Wemm if (fds->revents != 0) 98142d11757SPeter Wemm n++; 98242d11757SPeter Wemm } 98342d11757SPeter Wemm } 98442d11757SPeter Wemm } 985eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 986b40ce416SJulian Elischer td->td_retval[0] = n; 98742d11757SPeter Wemm return (0); 98842d11757SPeter Wemm } 98942d11757SPeter Wemm 99042d11757SPeter Wemm /* 99142d11757SPeter Wemm * OpenBSD poll system call. 99242d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 99342d11757SPeter Wemm */ 99442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 99542d11757SPeter Wemm struct openbsd_poll_args { 99642d11757SPeter Wemm struct pollfd *fds; 99742d11757SPeter Wemm u_int nfds; 99842d11757SPeter Wemm int timeout; 99942d11757SPeter Wemm }; 100042d11757SPeter Wemm #endif 1001ad2edad9SMatthew Dillon /* 1002ad2edad9SMatthew Dillon * MPSAFE 1003ad2edad9SMatthew Dillon */ 100442d11757SPeter Wemm int 1005b40ce416SJulian Elischer openbsd_poll(td, uap) 1006b40ce416SJulian Elischer register struct thread *td; 100742d11757SPeter Wemm register struct openbsd_poll_args *uap; 100842d11757SPeter Wemm { 1009b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 101042d11757SPeter Wemm } 101142d11757SPeter Wemm 101285f190e4SAlfred Perlstein /* 101385f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 101485f190e4SAlfred Perlstein * we were polling. 101585f190e4SAlfred Perlstein * 101685f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 101785f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 101885f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 101985f190e4SAlfred Perlstein */ 102085f190e4SAlfred Perlstein void 102185f190e4SAlfred Perlstein clear_selinfo_list(td) 102285f190e4SAlfred Perlstein struct thread *td; 102385f190e4SAlfred Perlstein { 102485f190e4SAlfred Perlstein struct selinfo *si; 102585f190e4SAlfred Perlstein 102685f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 102785f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 102885f190e4SAlfred Perlstein si->si_thread = NULL; 102985f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 103085f190e4SAlfred Perlstein } 103185f190e4SAlfred Perlstein 1032df8bae1dSRodney W. Grimes /* 1033df8bae1dSRodney W. Grimes * Record a select request. 1034df8bae1dSRodney W. Grimes */ 1035df8bae1dSRodney W. Grimes void 1036df8bae1dSRodney W. Grimes selrecord(selector, sip) 1037b40ce416SJulian Elischer struct thread *selector; 1038df8bae1dSRodney W. Grimes struct selinfo *sip; 1039df8bae1dSRodney W. Grimes { 1040df8bae1dSRodney W. Grimes 104185f190e4SAlfred Perlstein mtx_lock(&sellock); 104285f190e4SAlfred Perlstein /* 1043b605b54cSAlfred Perlstein * If the selinfo's thread pointer is NULL then take ownership of it. 1044b605b54cSAlfred Perlstein * 1045b605b54cSAlfred Perlstein * If the thread pointer is not NULL and it points to another 1046b605b54cSAlfred Perlstein * thread, then we have a collision. 1047b605b54cSAlfred Perlstein * 1048b605b54cSAlfred Perlstein * If the thread pointer is not NULL and points back to us then leave 1049b605b54cSAlfred Perlstein * it alone as we've already added pointed it at us and added it to 1050b605b54cSAlfred Perlstein * our list. 105185f190e4SAlfred Perlstein */ 105285f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1053b40ce416SJulian Elischer sip->si_thread = selector; 105485f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 105585f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 105685f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 105785f190e4SAlfred Perlstein } 105885f190e4SAlfred Perlstein 105985f190e4SAlfred Perlstein mtx_unlock(&sellock); 1060df8bae1dSRodney W. Grimes } 1061df8bae1dSRodney W. Grimes 1062512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1063df8bae1dSRodney W. Grimes void 1064df8bae1dSRodney W. Grimes selwakeup(sip) 106585f190e4SAlfred Perlstein struct selinfo *sip; 1066df8bae1dSRodney W. Grimes { 1067512824f8SSeigo Tanimura doselwakeup(sip, -1); 1068512824f8SSeigo Tanimura } 1069512824f8SSeigo Tanimura 1070512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1071512824f8SSeigo Tanimura void 1072512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1073512824f8SSeigo Tanimura struct selinfo *sip; 1074512824f8SSeigo Tanimura int pri; 1075512824f8SSeigo Tanimura { 1076512824f8SSeigo Tanimura doselwakeup(sip, pri); 1077512824f8SSeigo Tanimura } 1078512824f8SSeigo Tanimura 1079512824f8SSeigo Tanimura /* 1080512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1081512824f8SSeigo Tanimura */ 1082512824f8SSeigo Tanimura static void 1083512824f8SSeigo Tanimura doselwakeup(sip, pri) 1084512824f8SSeigo Tanimura struct selinfo *sip; 1085512824f8SSeigo Tanimura int pri; 1086512824f8SSeigo Tanimura { 1087b40ce416SJulian Elischer struct thread *td; 1088df8bae1dSRodney W. Grimes 108985f190e4SAlfred Perlstein mtx_lock(&sellock); 109085f190e4SAlfred Perlstein td = sip->si_thread; 109185f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1092df8bae1dSRodney W. Grimes nselcoll++; 1093df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1094512824f8SSeigo Tanimura cv_broadcastpri(&selwait, pri); 1095df8bae1dSRodney W. Grimes } 109685f190e4SAlfred Perlstein if (td == NULL) { 109785f190e4SAlfred Perlstein mtx_unlock(&sellock); 1098b40ce416SJulian Elischer return; 1099b40ce416SJulian Elischer } 110085f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 110185f190e4SAlfred Perlstein sip->si_thread = NULL; 11029ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1103b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 110433a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 110544f3b092SJohn Baldwin sleepq_remove(td, &selwait); 110685f190e4SAlfred Perlstein mtx_unlock(&sellock); 1107df8bae1dSRodney W. Grimes } 1108265fc98fSSeigo Tanimura 11094d77a549SAlfred Perlstein static void selectinit(void *); 1110265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1111265fc98fSSeigo Tanimura 1112265fc98fSSeigo Tanimura /* ARGSUSED*/ 1113265fc98fSSeigo Tanimura static void 1114265fc98fSSeigo Tanimura selectinit(dummy) 1115265fc98fSSeigo Tanimura void *dummy; 1116265fc98fSSeigo Tanimura { 1117265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 11186008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1119265fc98fSSeigo Tanimura } 1120