19454b2d8SWarner Losh /*- 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 402de92a38SPeter Wemm #include "opt_compat.h" 41db6a20e2SGarrett Wollman #include "opt_ktrace.h" 42db6a20e2SGarrett Wollman 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 45d2d3e875SBruce Evans #include <sys/sysproto.h> 46df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 4720982410SBruce Evans #include <sys/filio.h> 483ac4d1efSBruce Evans #include <sys/fcntl.h> 49df8bae1dSRodney W. Grimes #include <sys/file.h> 50df8bae1dSRodney W. Grimes #include <sys/proc.h> 51797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53df8bae1dSRodney W. Grimes #include <sys/uio.h> 54df8bae1dSRodney W. Grimes #include <sys/kernel.h> 55104a9b7eSAlexander Kabaev #include <sys/limits.h> 56df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5742d11757SPeter Wemm #include <sys/poll.h> 5889b71647SPeter Wemm #include <sys/resourcevar.h> 590a2c3d48SGarrett Wollman #include <sys/selinfo.h> 6044f3b092SJohn Baldwin #include <sys/sleepqueue.h> 618f19eb88SIan Dowse #include <sys/syscallsubr.h> 628cb96f20SPeter Wemm #include <sys/sysctl.h> 6342d11757SPeter Wemm #include <sys/sysent.h> 649bbee259SAndrey A. Chernov #include <sys/vnode.h> 65279d7226SMatthew Dillon #include <sys/bio.h> 66279d7226SMatthew Dillon #include <sys/buf.h> 67265fc98fSSeigo Tanimura #include <sys/condvar.h> 68df8bae1dSRodney W. Grimes #ifdef KTRACE 69df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 70df8bae1dSRodney W. Grimes #endif 71279d7226SMatthew Dillon #include <vm/vm.h> 72279d7226SMatthew Dillon #include <vm/vm_page.h> 73df8bae1dSRodney W. Grimes 74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 76a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7755166637SPoul-Henning Kamp 78bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 79bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 80bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 81bcd9e0ddSJohn Baldwin off_t, int); 82bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 83bcd9e0ddSJohn Baldwin off_t, int); 84512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 858fe387abSDmitrij Tejblum 86df8bae1dSRodney W. Grimes /* 87df8bae1dSRodney W. Grimes * Read system call. 88df8bae1dSRodney W. Grimes */ 89d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 90df8bae1dSRodney W. Grimes struct read_args { 91df8bae1dSRodney W. Grimes int fd; 92134e06feSBruce Evans void *buf; 93134e06feSBruce Evans size_t nbyte; 94df8bae1dSRodney W. Grimes }; 95d2d3e875SBruce Evans #endif 96ad2edad9SMatthew Dillon /* 97ad2edad9SMatthew Dillon * MPSAFE 98ad2edad9SMatthew Dillon */ 9926f9a767SRodney W. Grimes int 100b40ce416SJulian Elischer read(td, uap) 101b40ce416SJulian Elischer struct thread *td; 102b064d43dSMatthew Dillon struct read_args *uap; 103df8bae1dSRodney W. Grimes { 104bcd9e0ddSJohn Baldwin struct uio auio; 105bcd9e0ddSJohn Baldwin struct iovec aiov; 106279d7226SMatthew Dillon int error; 107df8bae1dSRodney W. Grimes 108bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 109bcd9e0ddSJohn Baldwin return (EINVAL); 110bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 111bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 112bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 113bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 114bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 115bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 116bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 117279d7226SMatthew Dillon return(error); 118df8bae1dSRodney W. Grimes } 119df8bae1dSRodney W. Grimes 120df8bae1dSRodney W. Grimes /* 121bcd9e0ddSJohn Baldwin * Positioned read system call 1224160ccd9SAlan Cox */ 1234160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1244160ccd9SAlan Cox struct pread_args { 1254160ccd9SAlan Cox int fd; 1264160ccd9SAlan Cox void *buf; 1274160ccd9SAlan Cox size_t nbyte; 1288fe387abSDmitrij Tejblum int pad; 1294160ccd9SAlan Cox off_t offset; 1304160ccd9SAlan Cox }; 1314160ccd9SAlan Cox #endif 132ad2edad9SMatthew Dillon /* 133ad2edad9SMatthew Dillon * MPSAFE 134ad2edad9SMatthew Dillon */ 1354160ccd9SAlan Cox int 136b40ce416SJulian Elischer pread(td, uap) 137b40ce416SJulian Elischer struct thread *td; 138b064d43dSMatthew Dillon struct pread_args *uap; 1394160ccd9SAlan Cox { 1404160ccd9SAlan Cox struct uio auio; 1414160ccd9SAlan Cox struct iovec aiov; 142bcd9e0ddSJohn Baldwin int error; 1434160ccd9SAlan Cox 144bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 145bcd9e0ddSJohn Baldwin return (EINVAL); 146bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 147bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 1484160ccd9SAlan Cox auio.uio_iov = &aiov; 1494160ccd9SAlan Cox auio.uio_iovcnt = 1; 150bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 1514160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 152bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, &auio, uap->offset); 1534160ccd9SAlan Cox return(error); 1544160ccd9SAlan Cox } 1554160ccd9SAlan Cox 1564160ccd9SAlan Cox /* 157df8bae1dSRodney W. Grimes * Scatter read system call. 158df8bae1dSRodney W. Grimes */ 159d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 160df8bae1dSRodney W. Grimes struct readv_args { 1617147b19dSBruce Evans int fd; 162df8bae1dSRodney W. Grimes struct iovec *iovp; 163df8bae1dSRodney W. Grimes u_int iovcnt; 164df8bae1dSRodney W. Grimes }; 165d2d3e875SBruce Evans #endif 166ad2edad9SMatthew Dillon /* 167ad2edad9SMatthew Dillon * MPSAFE 168ad2edad9SMatthew Dillon */ 16926f9a767SRodney W. Grimes int 170552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap) 171df8bae1dSRodney W. Grimes { 172b88ec951SJohn Baldwin struct uio *auio; 173b88ec951SJohn Baldwin int error; 174b88ec951SJohn Baldwin 175b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 176b88ec951SJohn Baldwin if (error) 177b88ec951SJohn Baldwin return (error); 178b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 179b88ec951SJohn Baldwin free(auio, M_IOV); 180b88ec951SJohn Baldwin return (error); 181b88ec951SJohn Baldwin } 182b88ec951SJohn Baldwin 183b88ec951SJohn Baldwin int 184b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 185b88ec951SJohn Baldwin { 186b064d43dSMatthew Dillon struct file *fp; 187bcd9e0ddSJohn Baldwin int error; 188bcd9e0ddSJohn Baldwin 189bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 190bcd9e0ddSJohn Baldwin if (error) 191bcd9e0ddSJohn Baldwin return (error); 192bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 193bcd9e0ddSJohn Baldwin fdrop(fp, td); 194bcd9e0ddSJohn Baldwin return (error); 195bcd9e0ddSJohn Baldwin } 196bcd9e0ddSJohn Baldwin 197bcd9e0ddSJohn Baldwin /* 198bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 199bcd9e0ddSJohn Baldwin */ 200bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 201bcd9e0ddSJohn Baldwin struct preadv_args { 202bcd9e0ddSJohn Baldwin int fd; 203bcd9e0ddSJohn Baldwin struct iovec *iovp; 204bcd9e0ddSJohn Baldwin u_int iovcnt; 205bcd9e0ddSJohn Baldwin off_t offset; 206bcd9e0ddSJohn Baldwin }; 207bcd9e0ddSJohn Baldwin #endif 208bcd9e0ddSJohn Baldwin /* 209bcd9e0ddSJohn Baldwin * MPSAFE 210bcd9e0ddSJohn Baldwin */ 211bcd9e0ddSJohn Baldwin int 212bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap) 213bcd9e0ddSJohn Baldwin { 214bcd9e0ddSJohn Baldwin struct uio *auio; 215bcd9e0ddSJohn Baldwin int error; 216bcd9e0ddSJohn Baldwin 217bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 218bcd9e0ddSJohn Baldwin if (error) 219bcd9e0ddSJohn Baldwin return (error); 220bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 221bcd9e0ddSJohn Baldwin free(auio, M_IOV); 222bcd9e0ddSJohn Baldwin return (error); 223bcd9e0ddSJohn Baldwin } 224bcd9e0ddSJohn Baldwin 225bcd9e0ddSJohn Baldwin int 226bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset) 227bcd9e0ddSJohn Baldwin struct thread *td; 228bcd9e0ddSJohn Baldwin int fd; 229bcd9e0ddSJohn Baldwin struct uio *auio; 230bcd9e0ddSJohn Baldwin off_t offset; 231bcd9e0ddSJohn Baldwin { 232bcd9e0ddSJohn Baldwin struct file *fp; 233bcd9e0ddSJohn Baldwin int error; 234bcd9e0ddSJohn Baldwin 235bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 236bcd9e0ddSJohn Baldwin if (error) 237bcd9e0ddSJohn Baldwin return (error); 238bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 239bcd9e0ddSJohn Baldwin error = ESPIPE; 240bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 241bcd9e0ddSJohn Baldwin error = EINVAL; 242bcd9e0ddSJohn Baldwin else 243bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 244bcd9e0ddSJohn Baldwin fdrop(fp, td); 245bcd9e0ddSJohn Baldwin return (error); 246bcd9e0ddSJohn Baldwin } 247bcd9e0ddSJohn Baldwin 248bcd9e0ddSJohn Baldwin /* 249bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 250bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 251bcd9e0ddSJohn Baldwin */ 252bcd9e0ddSJohn Baldwin static int 253bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 254bcd9e0ddSJohn Baldwin struct thread *td; 255bcd9e0ddSJohn Baldwin int fd; 256bcd9e0ddSJohn Baldwin struct file *fp; 257bcd9e0ddSJohn Baldwin struct uio *auio; 258bcd9e0ddSJohn Baldwin off_t offset; 259bcd9e0ddSJohn Baldwin int flags; 260bcd9e0ddSJohn Baldwin { 261bcd9e0ddSJohn Baldwin ssize_t cnt; 26282641acdSAlan Cox int error; 263df8bae1dSRodney W. Grimes #ifdef KTRACE 264552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 265df8bae1dSRodney W. Grimes #endif 266df8bae1dSRodney W. Grimes 2674f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 2684f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 2694f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 2704f8d23d6SPoul-Henning Kamp return(0); 2714f8d23d6SPoul-Henning Kamp } 272552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 273bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 274552afd9cSPoul-Henning Kamp auio->uio_td = td; 275df8bae1dSRodney W. Grimes #ifdef KTRACE 276552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 277552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 278df8bae1dSRodney W. Grimes #endif 279552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 280bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 281552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 282df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 283df8bae1dSRodney W. Grimes error = 0; 284279d7226SMatthew Dillon } 285552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 286df8bae1dSRodney W. Grimes #ifdef KTRACE 287552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 288552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 289b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 290df8bae1dSRodney W. Grimes } 291df8bae1dSRodney W. Grimes #endif 292b40ce416SJulian Elischer td->td_retval[0] = cnt; 293df8bae1dSRodney W. Grimes return (error); 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes 296df8bae1dSRodney W. Grimes /* 297df8bae1dSRodney W. Grimes * Write system call 298df8bae1dSRodney W. Grimes */ 299d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 300df8bae1dSRodney W. Grimes struct write_args { 301df8bae1dSRodney W. Grimes int fd; 302134e06feSBruce Evans const void *buf; 303134e06feSBruce Evans size_t nbyte; 304df8bae1dSRodney W. Grimes }; 305d2d3e875SBruce Evans #endif 306ad2edad9SMatthew Dillon /* 307ad2edad9SMatthew Dillon * MPSAFE 308ad2edad9SMatthew Dillon */ 30926f9a767SRodney W. Grimes int 310b40ce416SJulian Elischer write(td, uap) 311b40ce416SJulian Elischer struct thread *td; 312b064d43dSMatthew Dillon struct write_args *uap; 313df8bae1dSRodney W. Grimes { 314bcd9e0ddSJohn Baldwin struct uio auio; 315bcd9e0ddSJohn Baldwin struct iovec aiov; 316279d7226SMatthew Dillon int error; 317df8bae1dSRodney W. Grimes 318bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 319bcd9e0ddSJohn Baldwin return (EINVAL); 320bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 321bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 322bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 323bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 324bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 325bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 326bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 327279d7226SMatthew Dillon return(error); 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes 330df8bae1dSRodney W. Grimes /* 331bcd9e0ddSJohn Baldwin * Positioned write system call 3324160ccd9SAlan Cox */ 3334160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3344160ccd9SAlan Cox struct pwrite_args { 3354160ccd9SAlan Cox int fd; 3364160ccd9SAlan Cox const void *buf; 3374160ccd9SAlan Cox size_t nbyte; 3388fe387abSDmitrij Tejblum int pad; 3394160ccd9SAlan Cox off_t offset; 3404160ccd9SAlan Cox }; 3414160ccd9SAlan Cox #endif 342ad2edad9SMatthew Dillon /* 343ad2edad9SMatthew Dillon * MPSAFE 344ad2edad9SMatthew Dillon */ 3454160ccd9SAlan Cox int 346b40ce416SJulian Elischer pwrite(td, uap) 347b40ce416SJulian Elischer struct thread *td; 348b064d43dSMatthew Dillon struct pwrite_args *uap; 3494160ccd9SAlan Cox { 3504160ccd9SAlan Cox struct uio auio; 3514160ccd9SAlan Cox struct iovec aiov; 352bcd9e0ddSJohn Baldwin int error; 3534160ccd9SAlan Cox 354bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 355bcd9e0ddSJohn Baldwin return (EINVAL); 356bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 357bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 3584160ccd9SAlan Cox auio.uio_iov = &aiov; 3594160ccd9SAlan Cox auio.uio_iovcnt = 1; 360bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 3614160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 362bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, &auio, uap->offset); 3634160ccd9SAlan Cox return(error); 3644160ccd9SAlan Cox } 3654160ccd9SAlan Cox 3664160ccd9SAlan Cox /* 367df8bae1dSRodney W. Grimes * Gather write system call 368df8bae1dSRodney W. Grimes */ 369d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 370df8bae1dSRodney W. Grimes struct writev_args { 371df8bae1dSRodney W. Grimes int fd; 372df8bae1dSRodney W. Grimes struct iovec *iovp; 373df8bae1dSRodney W. Grimes u_int iovcnt; 374df8bae1dSRodney W. Grimes }; 375d2d3e875SBruce Evans #endif 376ad2edad9SMatthew Dillon /* 377ad2edad9SMatthew Dillon * MPSAFE 378ad2edad9SMatthew Dillon */ 37926f9a767SRodney W. Grimes int 380552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap) 381df8bae1dSRodney W. Grimes { 382b88ec951SJohn Baldwin struct uio *auio; 383b88ec951SJohn Baldwin int error; 384b88ec951SJohn Baldwin 385b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 386b88ec951SJohn Baldwin if (error) 387b88ec951SJohn Baldwin return (error); 388b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 389b88ec951SJohn Baldwin free(auio, M_IOV); 390b88ec951SJohn Baldwin return (error); 391b88ec951SJohn Baldwin } 392b88ec951SJohn Baldwin 393b88ec951SJohn Baldwin int 394b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 395b88ec951SJohn Baldwin { 396b064d43dSMatthew Dillon struct file *fp; 397bcd9e0ddSJohn Baldwin int error; 398bcd9e0ddSJohn Baldwin 399bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 400bcd9e0ddSJohn Baldwin if (error) 401af56abaaSJohn Baldwin return (error); 402bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 403bcd9e0ddSJohn Baldwin fdrop(fp, td); 404bcd9e0ddSJohn Baldwin return (error); 405bcd9e0ddSJohn Baldwin } 406bcd9e0ddSJohn Baldwin 407bcd9e0ddSJohn Baldwin /* 408bcd9e0ddSJohn Baldwin * Gather positioned write system call 409bcd9e0ddSJohn Baldwin */ 410bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 411bcd9e0ddSJohn Baldwin struct pwritev_args { 412bcd9e0ddSJohn Baldwin int fd; 413bcd9e0ddSJohn Baldwin struct iovec *iovp; 414bcd9e0ddSJohn Baldwin u_int iovcnt; 415bcd9e0ddSJohn Baldwin off_t offset; 416bcd9e0ddSJohn Baldwin }; 417bcd9e0ddSJohn Baldwin #endif 418bcd9e0ddSJohn Baldwin /* 419bcd9e0ddSJohn Baldwin * MPSAFE 420bcd9e0ddSJohn Baldwin */ 421bcd9e0ddSJohn Baldwin int 422bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap) 423bcd9e0ddSJohn Baldwin { 424bcd9e0ddSJohn Baldwin struct uio *auio; 425bcd9e0ddSJohn Baldwin int error; 426bcd9e0ddSJohn Baldwin 427bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 428bcd9e0ddSJohn Baldwin if (error) 429bcd9e0ddSJohn Baldwin return (error); 430bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 431bcd9e0ddSJohn Baldwin free(auio, M_IOV); 432bcd9e0ddSJohn Baldwin return (error); 433bcd9e0ddSJohn Baldwin } 434bcd9e0ddSJohn Baldwin 435bcd9e0ddSJohn Baldwin int 436bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset) 437bcd9e0ddSJohn Baldwin struct thread *td; 438bcd9e0ddSJohn Baldwin struct uio *auio; 439bcd9e0ddSJohn Baldwin int fd; 440bcd9e0ddSJohn Baldwin off_t offset; 441bcd9e0ddSJohn Baldwin { 442bcd9e0ddSJohn Baldwin struct file *fp; 443bcd9e0ddSJohn Baldwin int error; 444bcd9e0ddSJohn Baldwin 445bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 446bcd9e0ddSJohn Baldwin if (error) 447af56abaaSJohn Baldwin return (error); 448bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 449bcd9e0ddSJohn Baldwin error = ESPIPE; 450bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 451bcd9e0ddSJohn Baldwin error = EINVAL; 452bcd9e0ddSJohn Baldwin else 453bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 454bcd9e0ddSJohn Baldwin fdrop(fp, td); 455bcd9e0ddSJohn Baldwin return (error); 456bcd9e0ddSJohn Baldwin } 457bcd9e0ddSJohn Baldwin 458bcd9e0ddSJohn Baldwin /* 459bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 460bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 461bcd9e0ddSJohn Baldwin */ 462bcd9e0ddSJohn Baldwin static int 463bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 464bcd9e0ddSJohn Baldwin struct thread *td; 465bcd9e0ddSJohn Baldwin int fd; 466bcd9e0ddSJohn Baldwin struct file *fp; 467bcd9e0ddSJohn Baldwin struct uio *auio; 468bcd9e0ddSJohn Baldwin off_t offset; 469bcd9e0ddSJohn Baldwin int flags; 470bcd9e0ddSJohn Baldwin { 471bcd9e0ddSJohn Baldwin ssize_t cnt; 472552afd9cSPoul-Henning Kamp int error; 473df8bae1dSRodney W. Grimes #ifdef KTRACE 474552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 475df8bae1dSRodney W. Grimes #endif 476df8bae1dSRodney W. Grimes 477552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 478552afd9cSPoul-Henning Kamp auio->uio_td = td; 479bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 480df8bae1dSRodney W. Grimes #ifdef KTRACE 481552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 482552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 483df8bae1dSRodney W. Grimes #endif 484552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 485a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 4869440653dSMatthew Dillon bwillwrite(); 487bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 488552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 489df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 490df8bae1dSRodney W. Grimes error = 0; 491bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 49219eb87d2SJohn Baldwin if (error == EPIPE) { 493b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 494b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 495b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 49619eb87d2SJohn Baldwin } 497df8bae1dSRodney W. Grimes } 498552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 499df8bae1dSRodney W. Grimes #ifdef KTRACE 500552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 501552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 502b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes #endif 505b40ce416SJulian Elischer td->td_retval[0] = cnt; 506df8bae1dSRodney W. Grimes return (error); 507df8bae1dSRodney W. Grimes } 508df8bae1dSRodney W. Grimes 509df8bae1dSRodney W. Grimes /* 510df8bae1dSRodney W. Grimes * Ioctl system call 511df8bae1dSRodney W. Grimes */ 512d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 513df8bae1dSRodney W. Grimes struct ioctl_args { 514df8bae1dSRodney W. Grimes int fd; 515069e9bc1SDoug Rabson u_long com; 516df8bae1dSRodney W. Grimes caddr_t data; 517df8bae1dSRodney W. Grimes }; 518d2d3e875SBruce Evans #endif 519ad2edad9SMatthew Dillon /* 520ad2edad9SMatthew Dillon * MPSAFE 521ad2edad9SMatthew Dillon */ 522df8bae1dSRodney W. Grimes /* ARGSUSED */ 52326f9a767SRodney W. Grimes int 5243e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap) 525df8bae1dSRodney W. Grimes { 5263e15c66fSPoul-Henning Kamp u_long com; 527d9f46233SJohn Baldwin int error; 5283e15c66fSPoul-Henning Kamp u_int size; 529df8bae1dSRodney W. Grimes caddr_t data, memp; 530df8bae1dSRodney W. Grimes 5319fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 5329fc6aa06SPoul-Henning Kamp printf( 5339fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 5349fc6aa06SPoul-Henning Kamp td->td_proc->p_pid, td->td_proc->p_comm, uap->com); 5359fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 5369fc6aa06SPoul-Henning Kamp } 537d9f46233SJohn Baldwin com = uap->com; 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes /* 540df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 541df8bae1dSRodney W. Grimes * copied to/from the user's address space. 542df8bae1dSRodney W. Grimes */ 543df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 544ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 545ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 5462de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 5472de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 5482de92a38SPeter Wemm #else 5492de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 5502de92a38SPeter Wemm #endif 551d9f46233SJohn Baldwin ((com & IOC_VOID) && size > 0)) 552426da3bcSAlfred Perlstein return (ENOTTY); 553279d7226SMatthew Dillon 554ca51b19bSPoul-Henning Kamp if (size > 0) { 555a163d034SWarner Losh memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 556df8bae1dSRodney W. Grimes data = memp; 557279d7226SMatthew Dillon } else { 558ca51b19bSPoul-Henning Kamp memp = NULL; 559ca51b19bSPoul-Henning Kamp data = (void *)&uap->data; 560279d7226SMatthew Dillon } 561df8bae1dSRodney W. Grimes if (com & IOC_IN) { 562df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 563df8bae1dSRodney W. Grimes if (error) { 564df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 5653e15c66fSPoul-Henning Kamp return (error); 566df8bae1dSRodney W. Grimes } 567ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 568df8bae1dSRodney W. Grimes /* 569df8bae1dSRodney W. Grimes * Zero the buffer so the user always 570df8bae1dSRodney W. Grimes * gets back something deterministic. 571df8bae1dSRodney W. Grimes */ 572df8bae1dSRodney W. Grimes bzero(data, size); 573279d7226SMatthew Dillon } 574df8bae1dSRodney W. Grimes 575d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 576d9f46233SJohn Baldwin 577d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 578d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 579d9f46233SJohn Baldwin 580d9f46233SJohn Baldwin if (memp != NULL) 581d9f46233SJohn Baldwin free(memp, M_IOCTLOPS); 582d9f46233SJohn Baldwin return (error); 583d9f46233SJohn Baldwin } 584d9f46233SJohn Baldwin 585d9f46233SJohn Baldwin int 586d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 587d9f46233SJohn Baldwin { 588d9f46233SJohn Baldwin struct file *fp; 589d9f46233SJohn Baldwin struct filedesc *fdp; 590d9f46233SJohn Baldwin int error; 591d9f46233SJohn Baldwin int tmp; 592d9f46233SJohn Baldwin 593d9f46233SJohn Baldwin if ((error = fget(td, fd, &fp)) != 0) 594d9f46233SJohn Baldwin return (error); 595d9f46233SJohn Baldwin if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 596d9f46233SJohn Baldwin fdrop(fp, td); 597d9f46233SJohn Baldwin return (EBADF); 598d9f46233SJohn Baldwin } 599d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 600d9f46233SJohn Baldwin switch (com) { 601d9f46233SJohn Baldwin case FIONCLEX: 602d9f46233SJohn Baldwin FILEDESC_LOCK_FAST(fdp); 603d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE; 604d9f46233SJohn Baldwin FILEDESC_UNLOCK_FAST(fdp); 605d9f46233SJohn Baldwin goto out; 606d9f46233SJohn Baldwin case FIOCLEX: 607d9f46233SJohn Baldwin FILEDESC_LOCK_FAST(fdp); 608d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 609d9f46233SJohn Baldwin FILEDESC_UNLOCK_FAST(fdp); 610d9f46233SJohn Baldwin goto out; 611d9f46233SJohn Baldwin case FIONBIO: 612426da3bcSAlfred Perlstein FILE_LOCK(fp); 613bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 614df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 615df8bae1dSRodney W. Grimes else 616df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 617426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 6188ccf264fSPoul-Henning Kamp data = (void *)&tmp; 619d9f46233SJohn Baldwin break; 620d9f46233SJohn Baldwin case FIOASYNC: 621426da3bcSAlfred Perlstein FILE_LOCK(fp); 622bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 623df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 624df8bae1dSRodney W. Grimes else 625df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 626426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 6278ccf264fSPoul-Henning Kamp data = (void *)&tmp; 628d9f46233SJohn Baldwin break; 629df8bae1dSRodney W. Grimes } 6308ccf264fSPoul-Henning Kamp 6318ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 632d9f46233SJohn Baldwin out: 633b40ce416SJulian Elischer fdrop(fp, td); 634df8bae1dSRodney W. Grimes return (error); 635df8bae1dSRodney W. Grimes } 636df8bae1dSRodney W. Grimes 63785f190e4SAlfred Perlstein /* 63885f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 63985f190e4SAlfred Perlstein */ 64085f190e4SAlfred Perlstein struct mtx sellock; 641265fc98fSSeigo Tanimura struct cv selwait; 6429ae6d334SKelly Yancey u_int nselcoll; /* Select collisions since boot */ 6439ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 644df8bae1dSRodney W. Grimes 645df8bae1dSRodney W. Grimes /* 646df8bae1dSRodney W. Grimes * Select system call. 647df8bae1dSRodney W. Grimes */ 648d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 649df8bae1dSRodney W. Grimes struct select_args { 650b08f7993SSujal Patel int nd; 651df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 652df8bae1dSRodney W. Grimes struct timeval *tv; 653df8bae1dSRodney W. Grimes }; 654d2d3e875SBruce Evans #endif 655ad2edad9SMatthew Dillon /* 656ad2edad9SMatthew Dillon * MPSAFE 657ad2edad9SMatthew Dillon */ 65826f9a767SRodney W. Grimes int 659b40ce416SJulian Elischer select(td, uap) 660b40ce416SJulian Elischer register struct thread *td; 661df8bae1dSRodney W. Grimes register struct select_args *uap; 662df8bae1dSRodney W. Grimes { 6638f19eb88SIan Dowse struct timeval tv, *tvp; 6648f19eb88SIan Dowse int error; 6658f19eb88SIan Dowse 6668f19eb88SIan Dowse if (uap->tv != NULL) { 6678f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 6688f19eb88SIan Dowse if (error) 6698f19eb88SIan Dowse return (error); 6708f19eb88SIan Dowse tvp = &tv; 6718f19eb88SIan Dowse } else 6728f19eb88SIan Dowse tvp = NULL; 6738f19eb88SIan Dowse 6748f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 6758f19eb88SIan Dowse } 6768f19eb88SIan Dowse 6778f19eb88SIan Dowse int 6788f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 6798f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 6808f19eb88SIan Dowse { 681426da3bcSAlfred Perlstein struct filedesc *fdp; 682d5e4d7e1SBruce Evans /* 683d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 684d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 685d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 686d5e4d7e1SBruce Evans * of 256. 687d5e4d7e1SBruce Evans */ 688d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 689eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 69000af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 6919ae6d334SKelly Yancey int error, timo; 6929ae6d334SKelly Yancey u_int ncoll, nbufbytes, ncpbytes, nfdbits; 693df8bae1dSRodney W. Grimes 6948f19eb88SIan Dowse if (nd < 0) 695acbfbfeaSSujal Patel return (EINVAL); 696426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 697db446e30SPoul-Henning Kamp 698124e4c3bSPoul-Henning Kamp FILEDESC_LOCK_FAST(fdp); 699ad2edad9SMatthew Dillon 7008f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 7018f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 702124e4c3bSPoul-Henning Kamp FILEDESC_UNLOCK_FAST(fdp); 703b08f7993SSujal Patel 704d5e4d7e1SBruce Evans /* 705d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 706d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 707d5e4d7e1SBruce Evans */ 7088f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 709d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 710d5e4d7e1SBruce Evans nbufbytes = 0; 7118f19eb88SIan Dowse if (fd_in != NULL) 712d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7138f19eb88SIan Dowse if (fd_ou != NULL) 714d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7158f19eb88SIan Dowse if (fd_ex != NULL) 716d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 717d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 718d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 719d5e4d7e1SBruce Evans else 720a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 721b08f7993SSujal Patel 722b08f7993SSujal Patel /* 723d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 724d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 725d5e4d7e1SBruce Evans * together. 726b08f7993SSujal Patel */ 727d5e4d7e1SBruce Evans sbp = selbits; 728df8bae1dSRodney W. Grimes #define getbits(name, x) \ 729d5e4d7e1SBruce Evans do { \ 7308f19eb88SIan Dowse if (name == NULL) \ 731d5e4d7e1SBruce Evans ibits[x] = NULL; \ 732d5e4d7e1SBruce Evans else { \ 733d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 734d5e4d7e1SBruce Evans obits[x] = sbp; \ 735d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 7368f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 737265fc98fSSeigo Tanimura if (error != 0) \ 73885f190e4SAlfred Perlstein goto done_nosellock; \ 739e04ac2feSJohn Baldwin } \ 740d5e4d7e1SBruce Evans } while (0) 7418f19eb88SIan Dowse getbits(fd_in, 0); 7428f19eb88SIan Dowse getbits(fd_ou, 1); 7438f19eb88SIan Dowse getbits(fd_ex, 2); 744df8bae1dSRodney W. Grimes #undef getbits 745d5e4d7e1SBruce Evans if (nbufbytes != 0) 746d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 747df8bae1dSRodney W. Grimes 7488f19eb88SIan Dowse if (tvp != NULL) { 7498f19eb88SIan Dowse atv = *tvp; 750df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 751df8bae1dSRodney W. Grimes error = EINVAL; 75285f190e4SAlfred Perlstein goto done_nosellock; 753df8bae1dSRodney W. Grimes } 754c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 75500af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 7569c386f6bSJohn Baldwin } else { 75700af9731SPoul-Henning Kamp atv.tv_sec = 0; 7589c386f6bSJohn Baldwin atv.tv_usec = 0; 7599c386f6bSJohn Baldwin } 76000af9731SPoul-Henning Kamp timo = 0; 7612149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 76285f190e4SAlfred Perlstein mtx_lock(&sellock); 763df8bae1dSRodney W. Grimes retry: 764df8bae1dSRodney W. Grimes ncoll = nselcoll; 765fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 766b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 767fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 76885f190e4SAlfred Perlstein mtx_unlock(&sellock); 76985f190e4SAlfred Perlstein 7708f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 77185f190e4SAlfred Perlstein mtx_lock(&sellock); 772b40ce416SJulian Elischer if (error || td->td_retval[0]) 773df8bae1dSRodney W. Grimes goto done; 7744da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 775c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 77685f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 777df8bae1dSRodney W. Grimes goto done; 77800af9731SPoul-Henning Kamp ttv = atv; 77900af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 78000af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 78100af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 782df8bae1dSRodney W. Grimes } 78385f190e4SAlfred Perlstein 78485f190e4SAlfred Perlstein /* 78585f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 78685f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 78785f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 78885f190e4SAlfred Perlstein * necessary. 78985f190e4SAlfred Perlstein */ 790fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 79185f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 79285f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 79385f190e4SAlfred Perlstein goto retry; 79485f190e4SAlfred Perlstein } 795fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 796bfbbc4aaSJason Evans 797265fc98fSSeigo Tanimura if (timo > 0) 79885f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 799265fc98fSSeigo Tanimura else 80085f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 801bfbbc4aaSJason Evans 802df8bae1dSRodney W. Grimes if (error == 0) 803df8bae1dSRodney W. Grimes goto retry; 804265fc98fSSeigo Tanimura 805df8bae1dSRodney W. Grimes done: 80685f190e4SAlfred Perlstein clear_selinfo_list(td); 807fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 808b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 809fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 81085f190e4SAlfred Perlstein mtx_unlock(&sellock); 81185f190e4SAlfred Perlstein 81285f190e4SAlfred Perlstein done_nosellock: 813df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 814df8bae1dSRodney W. Grimes if (error == ERESTART) 815df8bae1dSRodney W. Grimes error = EINTR; 816df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 817df8bae1dSRodney W. Grimes error = 0; 818df8bae1dSRodney W. Grimes #define putbits(name, x) \ 8198f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 820df8bae1dSRodney W. Grimes error = error2; 821df8bae1dSRodney W. Grimes if (error == 0) { 822df8bae1dSRodney W. Grimes int error2; 823df8bae1dSRodney W. Grimes 8248f19eb88SIan Dowse putbits(fd_in, 0); 8258f19eb88SIan Dowse putbits(fd_ou, 1); 8268f19eb88SIan Dowse putbits(fd_ex, 2); 827df8bae1dSRodney W. Grimes #undef putbits 828df8bae1dSRodney W. Grimes } 829d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 830d5e4d7e1SBruce Evans free(selbits, M_SELECT); 831ad2edad9SMatthew Dillon 832df8bae1dSRodney W. Grimes return (error); 833df8bae1dSRodney W. Grimes } 834df8bae1dSRodney W. Grimes 835265fc98fSSeigo Tanimura static int 836b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 837b40ce416SJulian Elischer struct thread *td; 838b08f7993SSujal Patel fd_mask **ibits, **obits; 839cb226aaaSPoul-Henning Kamp int nfd; 840df8bae1dSRodney W. Grimes { 841f082218cSPeter Wemm int msk, i, fd; 842f082218cSPeter Wemm fd_mask bits; 843df8bae1dSRodney W. Grimes struct file *fp; 844df8bae1dSRodney W. Grimes int n = 0; 8452087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 84642d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 847eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 848df8bae1dSRodney W. Grimes 849eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 850df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 851d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 852d5e4d7e1SBruce Evans continue; 853df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 854b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 855f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 856f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 857f082218cSPeter Wemm if (!(bits & 1)) 858f082218cSPeter Wemm continue; 859eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 860eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 861df8bae1dSRodney W. Grimes return (EBADF); 862eb209311SAlfred Perlstein } 863ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 864ea6027a8SRobert Watson td)) { 865b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 866f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 867df8bae1dSRodney W. Grimes n++; 868df8bae1dSRodney W. Grimes } 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes } 871df8bae1dSRodney W. Grimes } 872eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 873b40ce416SJulian Elischer td->td_retval[0] = n; 874df8bae1dSRodney W. Grimes return (0); 875df8bae1dSRodney W. Grimes } 876df8bae1dSRodney W. Grimes 87742d11757SPeter Wemm /* 87842d11757SPeter Wemm * Poll system call. 87942d11757SPeter Wemm */ 88042d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 88142d11757SPeter Wemm struct poll_args { 88242d11757SPeter Wemm struct pollfd *fds; 88342d11757SPeter Wemm u_int nfds; 88442d11757SPeter Wemm int timeout; 88542d11757SPeter Wemm }; 88642d11757SPeter Wemm #endif 887ad2edad9SMatthew Dillon /* 888ad2edad9SMatthew Dillon * MPSAFE 889ad2edad9SMatthew Dillon */ 89042d11757SPeter Wemm int 891b40ce416SJulian Elischer poll(td, uap) 892b40ce416SJulian Elischer struct thread *td; 893ea0237edSJonathan Lemon struct poll_args *uap; 89442d11757SPeter Wemm { 8952580f4e5SAndre Oppermann struct pollfd *bits; 8962580f4e5SAndre Oppermann struct pollfd smallbits[32]; 89700af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 8989ae6d334SKelly Yancey int error = 0, timo; 8999ae6d334SKelly Yancey u_int ncoll, nfds; 90042d11757SPeter Wemm size_t ni; 90142d11757SPeter Wemm 902d1e405c5SAlfred Perlstein nfds = uap->nfds; 903ad2edad9SMatthew Dillon 9045d8dd01dSRobert Watson /* 9052bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9062bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9072bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9082bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9092bd5ac33SPeter Wemm * safe, but not overly restrictive. 91089b71647SPeter Wemm */ 91191d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 91291d5354aSJohn Baldwin if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) && 913b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 91491d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 915ad2edad9SMatthew Dillon error = EINVAL; 916ad2edad9SMatthew Dillon goto done2; 917ad2edad9SMatthew Dillon } 91891d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 91989b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 92042d11757SPeter Wemm if (ni > sizeof(smallbits)) 921a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 92242d11757SPeter Wemm else 92342d11757SPeter Wemm bits = smallbits; 924d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 92542d11757SPeter Wemm if (error) 92685f190e4SAlfred Perlstein goto done_nosellock; 927d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 928d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 929d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 93042d11757SPeter Wemm if (itimerfix(&atv)) { 93142d11757SPeter Wemm error = EINVAL; 93285f190e4SAlfred Perlstein goto done_nosellock; 93342d11757SPeter Wemm } 934c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 93500af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9369c386f6bSJohn Baldwin } else { 93700af9731SPoul-Henning Kamp atv.tv_sec = 0; 9389c386f6bSJohn Baldwin atv.tv_usec = 0; 9399c386f6bSJohn Baldwin } 94000af9731SPoul-Henning Kamp timo = 0; 9412149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 94285f190e4SAlfred Perlstein mtx_lock(&sellock); 94342d11757SPeter Wemm retry: 94442d11757SPeter Wemm ncoll = nselcoll; 945fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 946b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 947fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 94885f190e4SAlfred Perlstein mtx_unlock(&sellock); 94985f190e4SAlfred Perlstein 9502580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 95185f190e4SAlfred Perlstein mtx_lock(&sellock); 952b40ce416SJulian Elischer if (error || td->td_retval[0]) 95342d11757SPeter Wemm goto done; 9544da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 955c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 95685f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 95742d11757SPeter Wemm goto done; 95800af9731SPoul-Henning Kamp ttv = atv; 95900af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 96000af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 96100af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 96242d11757SPeter Wemm } 96385f190e4SAlfred Perlstein /* 96485f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 96585f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 96685f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 96785f190e4SAlfred Perlstein */ 968fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 96985f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 970fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 97185f190e4SAlfred Perlstein goto retry; 97285f190e4SAlfred Perlstein } 97385f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 97485f190e4SAlfred Perlstein 975265fc98fSSeigo Tanimura if (timo > 0) 97685f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 977265fc98fSSeigo Tanimura else 97885f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 97985f190e4SAlfred Perlstein 98042d11757SPeter Wemm if (error == 0) 98142d11757SPeter Wemm goto retry; 982265fc98fSSeigo Tanimura 98342d11757SPeter Wemm done: 98485f190e4SAlfred Perlstein clear_selinfo_list(td); 985fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 986b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 987fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 98885f190e4SAlfred Perlstein mtx_unlock(&sellock); 98985f190e4SAlfred Perlstein 99085f190e4SAlfred Perlstein done_nosellock: 99142d11757SPeter Wemm /* poll is not restarted after signals... */ 99242d11757SPeter Wemm if (error == ERESTART) 99342d11757SPeter Wemm error = EINTR; 99442d11757SPeter Wemm if (error == EWOULDBLOCK) 99542d11757SPeter Wemm error = 0; 99642d11757SPeter Wemm if (error == 0) { 997d1e405c5SAlfred Perlstein error = copyout(bits, uap->fds, ni); 99842d11757SPeter Wemm if (error) 99942d11757SPeter Wemm goto out; 100042d11757SPeter Wemm } 100142d11757SPeter Wemm out: 100242d11757SPeter Wemm if (ni > sizeof(smallbits)) 100342d11757SPeter Wemm free(bits, M_TEMP); 1004ad2edad9SMatthew Dillon done2: 100542d11757SPeter Wemm return (error); 100642d11757SPeter Wemm } 100742d11757SPeter Wemm 100842d11757SPeter Wemm static int 1009b40ce416SJulian Elischer pollscan(td, fds, nfd) 1010b40ce416SJulian Elischer struct thread *td; 101142d11757SPeter Wemm struct pollfd *fds; 1012ea0237edSJonathan Lemon u_int nfd; 101342d11757SPeter Wemm { 1014b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 101542d11757SPeter Wemm int i; 101642d11757SPeter Wemm struct file *fp; 101742d11757SPeter Wemm int n = 0; 101842d11757SPeter Wemm 1019426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1020eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1021337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 102242d11757SPeter Wemm fds->revents = POLLNVAL; 102342d11757SPeter Wemm n++; 1024337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1025337c9691SJordan K. Hubbard fds->revents = 0; 102642d11757SPeter Wemm } else { 102742d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1028279d7226SMatthew Dillon if (fp == NULL) { 102942d11757SPeter Wemm fds->revents = POLLNVAL; 103042d11757SPeter Wemm n++; 103142d11757SPeter Wemm } else { 10322087c896SBruce Evans /* 10332087c896SBruce Evans * Note: backend also returns POLLHUP and 10342087c896SBruce Evans * POLLERR if appropriate. 10352087c896SBruce Evans */ 103613ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1037ea6027a8SRobert Watson td->td_ucred, td); 103842d11757SPeter Wemm if (fds->revents != 0) 103942d11757SPeter Wemm n++; 104042d11757SPeter Wemm } 104142d11757SPeter Wemm } 104242d11757SPeter Wemm } 1043eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1044b40ce416SJulian Elischer td->td_retval[0] = n; 104542d11757SPeter Wemm return (0); 104642d11757SPeter Wemm } 104742d11757SPeter Wemm 104842d11757SPeter Wemm /* 104942d11757SPeter Wemm * OpenBSD poll system call. 105042d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 105142d11757SPeter Wemm */ 105242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 105342d11757SPeter Wemm struct openbsd_poll_args { 105442d11757SPeter Wemm struct pollfd *fds; 105542d11757SPeter Wemm u_int nfds; 105642d11757SPeter Wemm int timeout; 105742d11757SPeter Wemm }; 105842d11757SPeter Wemm #endif 1059ad2edad9SMatthew Dillon /* 1060ad2edad9SMatthew Dillon * MPSAFE 1061ad2edad9SMatthew Dillon */ 106242d11757SPeter Wemm int 1063b40ce416SJulian Elischer openbsd_poll(td, uap) 1064b40ce416SJulian Elischer register struct thread *td; 106542d11757SPeter Wemm register struct openbsd_poll_args *uap; 106642d11757SPeter Wemm { 1067b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 106842d11757SPeter Wemm } 106942d11757SPeter Wemm 107085f190e4SAlfred Perlstein /* 107185f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 107285f190e4SAlfred Perlstein * we were polling. 107385f190e4SAlfred Perlstein * 107485f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 107585f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 107685f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 107785f190e4SAlfred Perlstein */ 107885f190e4SAlfred Perlstein void 107985f190e4SAlfred Perlstein clear_selinfo_list(td) 108085f190e4SAlfred Perlstein struct thread *td; 108185f190e4SAlfred Perlstein { 108285f190e4SAlfred Perlstein struct selinfo *si; 108385f190e4SAlfred Perlstein 108485f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 108585f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 108685f190e4SAlfred Perlstein si->si_thread = NULL; 108785f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 108885f190e4SAlfred Perlstein } 108985f190e4SAlfred Perlstein 1090df8bae1dSRodney W. Grimes /* 1091df8bae1dSRodney W. Grimes * Record a select request. 1092df8bae1dSRodney W. Grimes */ 1093df8bae1dSRodney W. Grimes void 1094df8bae1dSRodney W. Grimes selrecord(selector, sip) 1095b40ce416SJulian Elischer struct thread *selector; 1096df8bae1dSRodney W. Grimes struct selinfo *sip; 1097df8bae1dSRodney W. Grimes { 1098df8bae1dSRodney W. Grimes 109985f190e4SAlfred Perlstein mtx_lock(&sellock); 110085f190e4SAlfred Perlstein /* 1101b605b54cSAlfred Perlstein * If the selinfo's thread pointer is NULL then take ownership of it. 1102b605b54cSAlfred Perlstein * 1103b605b54cSAlfred Perlstein * If the thread pointer is not NULL and it points to another 1104b605b54cSAlfred Perlstein * thread, then we have a collision. 1105b605b54cSAlfred Perlstein * 1106b605b54cSAlfred Perlstein * If the thread pointer is not NULL and points back to us then leave 1107b605b54cSAlfred Perlstein * it alone as we've already added pointed it at us and added it to 1108b605b54cSAlfred Perlstein * our list. 110985f190e4SAlfred Perlstein */ 111085f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1111b40ce416SJulian Elischer sip->si_thread = selector; 111285f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 111385f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 111485f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 111585f190e4SAlfred Perlstein } 111685f190e4SAlfred Perlstein 111785f190e4SAlfred Perlstein mtx_unlock(&sellock); 1118df8bae1dSRodney W. Grimes } 1119df8bae1dSRodney W. Grimes 1120512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1121df8bae1dSRodney W. Grimes void 1122df8bae1dSRodney W. Grimes selwakeup(sip) 112385f190e4SAlfred Perlstein struct selinfo *sip; 1124df8bae1dSRodney W. Grimes { 1125512824f8SSeigo Tanimura doselwakeup(sip, -1); 1126512824f8SSeigo Tanimura } 1127512824f8SSeigo Tanimura 1128512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1129512824f8SSeigo Tanimura void 1130512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1131512824f8SSeigo Tanimura struct selinfo *sip; 1132512824f8SSeigo Tanimura int pri; 1133512824f8SSeigo Tanimura { 1134512824f8SSeigo Tanimura doselwakeup(sip, pri); 1135512824f8SSeigo Tanimura } 1136512824f8SSeigo Tanimura 1137512824f8SSeigo Tanimura /* 1138512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1139512824f8SSeigo Tanimura */ 1140512824f8SSeigo Tanimura static void 1141512824f8SSeigo Tanimura doselwakeup(sip, pri) 1142512824f8SSeigo Tanimura struct selinfo *sip; 1143512824f8SSeigo Tanimura int pri; 1144512824f8SSeigo Tanimura { 1145b40ce416SJulian Elischer struct thread *td; 1146df8bae1dSRodney W. Grimes 114785f190e4SAlfred Perlstein mtx_lock(&sellock); 114885f190e4SAlfred Perlstein td = sip->si_thread; 114985f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1150df8bae1dSRodney W. Grimes nselcoll++; 1151df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1152512824f8SSeigo Tanimura cv_broadcastpri(&selwait, pri); 1153df8bae1dSRodney W. Grimes } 115485f190e4SAlfred Perlstein if (td == NULL) { 115585f190e4SAlfred Perlstein mtx_unlock(&sellock); 1156b40ce416SJulian Elischer return; 1157b40ce416SJulian Elischer } 115885f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 115985f190e4SAlfred Perlstein sip->si_thread = NULL; 11609ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1161b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 116233a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 116344f3b092SJohn Baldwin sleepq_remove(td, &selwait); 116485f190e4SAlfred Perlstein mtx_unlock(&sellock); 1165df8bae1dSRodney W. Grimes } 1166265fc98fSSeigo Tanimura 11674d77a549SAlfred Perlstein static void selectinit(void *); 1168265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1169265fc98fSSeigo Tanimura 1170265fc98fSSeigo Tanimura /* ARGSUSED*/ 1171265fc98fSSeigo Tanimura static void 1172265fc98fSSeigo Tanimura selectinit(dummy) 1173265fc98fSSeigo Tanimura void *dummy; 1174265fc98fSSeigo Tanimura { 1175265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 11766008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1177265fc98fSSeigo Tanimura } 1178