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 9626f9a767SRodney W. Grimes int 97b40ce416SJulian Elischer read(td, uap) 98b40ce416SJulian Elischer struct thread *td; 99b064d43dSMatthew Dillon struct read_args *uap; 100df8bae1dSRodney W. Grimes { 101bcd9e0ddSJohn Baldwin struct uio auio; 102bcd9e0ddSJohn Baldwin struct iovec aiov; 103279d7226SMatthew Dillon int error; 104df8bae1dSRodney W. Grimes 105bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 106bcd9e0ddSJohn Baldwin return (EINVAL); 107bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 108bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 109bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 110bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 111bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 112bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 113bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 114279d7226SMatthew Dillon return(error); 115df8bae1dSRodney W. Grimes } 116df8bae1dSRodney W. Grimes 117df8bae1dSRodney W. Grimes /* 118bcd9e0ddSJohn Baldwin * Positioned read system call 1194160ccd9SAlan Cox */ 1204160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1214160ccd9SAlan Cox struct pread_args { 1224160ccd9SAlan Cox int fd; 1234160ccd9SAlan Cox void *buf; 1244160ccd9SAlan Cox size_t nbyte; 1258fe387abSDmitrij Tejblum int pad; 1264160ccd9SAlan Cox off_t offset; 1274160ccd9SAlan Cox }; 1284160ccd9SAlan Cox #endif 1294160ccd9SAlan Cox int 130b40ce416SJulian Elischer pread(td, uap) 131b40ce416SJulian Elischer struct thread *td; 132b064d43dSMatthew Dillon struct pread_args *uap; 1334160ccd9SAlan Cox { 1344160ccd9SAlan Cox struct uio auio; 1354160ccd9SAlan Cox struct iovec aiov; 136bcd9e0ddSJohn Baldwin int error; 1374160ccd9SAlan Cox 138bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 139bcd9e0ddSJohn Baldwin return (EINVAL); 140bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 141bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 1424160ccd9SAlan Cox auio.uio_iov = &aiov; 1434160ccd9SAlan Cox auio.uio_iovcnt = 1; 144bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 1454160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 146bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, &auio, uap->offset); 1474160ccd9SAlan Cox return(error); 1484160ccd9SAlan Cox } 1494160ccd9SAlan Cox 1504160ccd9SAlan Cox /* 151df8bae1dSRodney W. Grimes * Scatter read system call. 152df8bae1dSRodney W. Grimes */ 153d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 154df8bae1dSRodney W. Grimes struct readv_args { 1557147b19dSBruce Evans int fd; 156df8bae1dSRodney W. Grimes struct iovec *iovp; 157df8bae1dSRodney W. Grimes u_int iovcnt; 158df8bae1dSRodney W. Grimes }; 159d2d3e875SBruce Evans #endif 16026f9a767SRodney W. Grimes int 161552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap) 162df8bae1dSRodney W. Grimes { 163b88ec951SJohn Baldwin struct uio *auio; 164b88ec951SJohn Baldwin int error; 165b88ec951SJohn Baldwin 166b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 167b88ec951SJohn Baldwin if (error) 168b88ec951SJohn Baldwin return (error); 169b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 170b88ec951SJohn Baldwin free(auio, M_IOV); 171b88ec951SJohn Baldwin return (error); 172b88ec951SJohn Baldwin } 173b88ec951SJohn Baldwin 174b88ec951SJohn Baldwin int 175b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 176b88ec951SJohn Baldwin { 177b064d43dSMatthew Dillon struct file *fp; 178bcd9e0ddSJohn Baldwin int error; 179bcd9e0ddSJohn Baldwin 180bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 181bcd9e0ddSJohn Baldwin if (error) 182bcd9e0ddSJohn Baldwin return (error); 183bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 184bcd9e0ddSJohn Baldwin fdrop(fp, td); 185bcd9e0ddSJohn Baldwin return (error); 186bcd9e0ddSJohn Baldwin } 187bcd9e0ddSJohn Baldwin 188bcd9e0ddSJohn Baldwin /* 189bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 190bcd9e0ddSJohn Baldwin */ 191bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 192bcd9e0ddSJohn Baldwin struct preadv_args { 193bcd9e0ddSJohn Baldwin int fd; 194bcd9e0ddSJohn Baldwin struct iovec *iovp; 195bcd9e0ddSJohn Baldwin u_int iovcnt; 196bcd9e0ddSJohn Baldwin off_t offset; 197bcd9e0ddSJohn Baldwin }; 198bcd9e0ddSJohn Baldwin #endif 199bcd9e0ddSJohn Baldwin int 200bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap) 201bcd9e0ddSJohn Baldwin { 202bcd9e0ddSJohn Baldwin struct uio *auio; 203bcd9e0ddSJohn Baldwin int error; 204bcd9e0ddSJohn Baldwin 205bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 206bcd9e0ddSJohn Baldwin if (error) 207bcd9e0ddSJohn Baldwin return (error); 208bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 209bcd9e0ddSJohn Baldwin free(auio, M_IOV); 210bcd9e0ddSJohn Baldwin return (error); 211bcd9e0ddSJohn Baldwin } 212bcd9e0ddSJohn Baldwin 213bcd9e0ddSJohn Baldwin int 214bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset) 215bcd9e0ddSJohn Baldwin struct thread *td; 216bcd9e0ddSJohn Baldwin int fd; 217bcd9e0ddSJohn Baldwin struct uio *auio; 218bcd9e0ddSJohn Baldwin off_t offset; 219bcd9e0ddSJohn Baldwin { 220bcd9e0ddSJohn Baldwin struct file *fp; 221bcd9e0ddSJohn Baldwin int error; 222bcd9e0ddSJohn Baldwin 223bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 224bcd9e0ddSJohn Baldwin if (error) 225bcd9e0ddSJohn Baldwin return (error); 226bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 227bcd9e0ddSJohn Baldwin error = ESPIPE; 228bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 229bcd9e0ddSJohn Baldwin error = EINVAL; 230bcd9e0ddSJohn Baldwin else 231bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 232bcd9e0ddSJohn Baldwin fdrop(fp, td); 233bcd9e0ddSJohn Baldwin return (error); 234bcd9e0ddSJohn Baldwin } 235bcd9e0ddSJohn Baldwin 236bcd9e0ddSJohn Baldwin /* 237bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 238bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 239bcd9e0ddSJohn Baldwin */ 240bcd9e0ddSJohn Baldwin static int 241bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 242bcd9e0ddSJohn Baldwin struct thread *td; 243bcd9e0ddSJohn Baldwin int fd; 244bcd9e0ddSJohn Baldwin struct file *fp; 245bcd9e0ddSJohn Baldwin struct uio *auio; 246bcd9e0ddSJohn Baldwin off_t offset; 247bcd9e0ddSJohn Baldwin int flags; 248bcd9e0ddSJohn Baldwin { 249bcd9e0ddSJohn Baldwin ssize_t cnt; 25082641acdSAlan Cox int error; 251df8bae1dSRodney W. Grimes #ifdef KTRACE 252552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 253df8bae1dSRodney W. Grimes #endif 254df8bae1dSRodney W. Grimes 2554f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 2564f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 2574f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 2584f8d23d6SPoul-Henning Kamp return(0); 2594f8d23d6SPoul-Henning Kamp } 260552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 261bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 262552afd9cSPoul-Henning Kamp auio->uio_td = td; 263df8bae1dSRodney W. Grimes #ifdef KTRACE 264552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 265552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 266df8bae1dSRodney W. Grimes #endif 267552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 268bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 269552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 270df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 271df8bae1dSRodney W. Grimes error = 0; 272279d7226SMatthew Dillon } 273552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 274df8bae1dSRodney W. Grimes #ifdef KTRACE 275552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 276552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 277b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 278df8bae1dSRodney W. Grimes } 279df8bae1dSRodney W. Grimes #endif 280b40ce416SJulian Elischer td->td_retval[0] = cnt; 281df8bae1dSRodney W. Grimes return (error); 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes 284df8bae1dSRodney W. Grimes /* 285df8bae1dSRodney W. Grimes * Write system call 286df8bae1dSRodney W. Grimes */ 287d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 288df8bae1dSRodney W. Grimes struct write_args { 289df8bae1dSRodney W. Grimes int fd; 290134e06feSBruce Evans const void *buf; 291134e06feSBruce Evans size_t nbyte; 292df8bae1dSRodney W. Grimes }; 293d2d3e875SBruce Evans #endif 29426f9a767SRodney W. Grimes int 295b40ce416SJulian Elischer write(td, uap) 296b40ce416SJulian Elischer struct thread *td; 297b064d43dSMatthew Dillon struct write_args *uap; 298df8bae1dSRodney W. Grimes { 299bcd9e0ddSJohn Baldwin struct uio auio; 300bcd9e0ddSJohn Baldwin struct iovec aiov; 301279d7226SMatthew Dillon int error; 302df8bae1dSRodney W. Grimes 303bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 304bcd9e0ddSJohn Baldwin return (EINVAL); 305bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 306bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 307bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 308bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 309bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 310bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 311bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 312279d7226SMatthew Dillon return(error); 313df8bae1dSRodney W. Grimes } 314df8bae1dSRodney W. Grimes 315df8bae1dSRodney W. Grimes /* 3160c14ff0eSRobert Watson * Positioned write system call. 3174160ccd9SAlan Cox */ 3184160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3194160ccd9SAlan Cox struct pwrite_args { 3204160ccd9SAlan Cox int fd; 3214160ccd9SAlan Cox const void *buf; 3224160ccd9SAlan Cox size_t nbyte; 3238fe387abSDmitrij Tejblum int pad; 3244160ccd9SAlan Cox off_t offset; 3254160ccd9SAlan Cox }; 3264160ccd9SAlan Cox #endif 3274160ccd9SAlan Cox int 328b40ce416SJulian Elischer pwrite(td, uap) 329b40ce416SJulian Elischer struct thread *td; 330b064d43dSMatthew Dillon struct pwrite_args *uap; 3314160ccd9SAlan Cox { 3324160ccd9SAlan Cox struct uio auio; 3334160ccd9SAlan Cox struct iovec aiov; 334bcd9e0ddSJohn Baldwin int error; 3354160ccd9SAlan Cox 336bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 337bcd9e0ddSJohn Baldwin return (EINVAL); 338bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 339bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 3404160ccd9SAlan Cox auio.uio_iov = &aiov; 3414160ccd9SAlan Cox auio.uio_iovcnt = 1; 342bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 3434160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 344bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, &auio, uap->offset); 3454160ccd9SAlan Cox return(error); 3464160ccd9SAlan Cox } 3474160ccd9SAlan Cox 3484160ccd9SAlan Cox /* 3490c14ff0eSRobert Watson * Gather write system call. 350df8bae1dSRodney W. Grimes */ 351d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 352df8bae1dSRodney W. Grimes struct writev_args { 353df8bae1dSRodney W. Grimes int fd; 354df8bae1dSRodney W. Grimes struct iovec *iovp; 355df8bae1dSRodney W. Grimes u_int iovcnt; 356df8bae1dSRodney W. Grimes }; 357d2d3e875SBruce Evans #endif 35826f9a767SRodney W. Grimes int 359552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap) 360df8bae1dSRodney W. Grimes { 361b88ec951SJohn Baldwin struct uio *auio; 362b88ec951SJohn Baldwin int error; 363b88ec951SJohn Baldwin 364b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 365b88ec951SJohn Baldwin if (error) 366b88ec951SJohn Baldwin return (error); 367b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 368b88ec951SJohn Baldwin free(auio, M_IOV); 369b88ec951SJohn Baldwin return (error); 370b88ec951SJohn Baldwin } 371b88ec951SJohn Baldwin 372b88ec951SJohn Baldwin int 373b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 374b88ec951SJohn Baldwin { 375b064d43dSMatthew Dillon struct file *fp; 376bcd9e0ddSJohn Baldwin int error; 377bcd9e0ddSJohn Baldwin 378bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 379bcd9e0ddSJohn Baldwin if (error) 380af56abaaSJohn Baldwin return (error); 381bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 382bcd9e0ddSJohn Baldwin fdrop(fp, td); 383bcd9e0ddSJohn Baldwin return (error); 384bcd9e0ddSJohn Baldwin } 385bcd9e0ddSJohn Baldwin 386bcd9e0ddSJohn Baldwin /* 3870c14ff0eSRobert Watson * Gather positioned write system call. 388bcd9e0ddSJohn Baldwin */ 389bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 390bcd9e0ddSJohn Baldwin struct pwritev_args { 391bcd9e0ddSJohn Baldwin int fd; 392bcd9e0ddSJohn Baldwin struct iovec *iovp; 393bcd9e0ddSJohn Baldwin u_int iovcnt; 394bcd9e0ddSJohn Baldwin off_t offset; 395bcd9e0ddSJohn Baldwin }; 396bcd9e0ddSJohn Baldwin #endif 397bcd9e0ddSJohn Baldwin int 398bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap) 399bcd9e0ddSJohn Baldwin { 400bcd9e0ddSJohn Baldwin struct uio *auio; 401bcd9e0ddSJohn Baldwin int error; 402bcd9e0ddSJohn Baldwin 403bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 404bcd9e0ddSJohn Baldwin if (error) 405bcd9e0ddSJohn Baldwin return (error); 406bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 407bcd9e0ddSJohn Baldwin free(auio, M_IOV); 408bcd9e0ddSJohn Baldwin return (error); 409bcd9e0ddSJohn Baldwin } 410bcd9e0ddSJohn Baldwin 411bcd9e0ddSJohn Baldwin int 412bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset) 413bcd9e0ddSJohn Baldwin struct thread *td; 414bcd9e0ddSJohn Baldwin struct uio *auio; 415bcd9e0ddSJohn Baldwin int fd; 416bcd9e0ddSJohn Baldwin off_t offset; 417bcd9e0ddSJohn Baldwin { 418bcd9e0ddSJohn Baldwin struct file *fp; 419bcd9e0ddSJohn Baldwin int error; 420bcd9e0ddSJohn Baldwin 421bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 422bcd9e0ddSJohn Baldwin if (error) 423af56abaaSJohn Baldwin return (error); 424bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 425bcd9e0ddSJohn Baldwin error = ESPIPE; 426bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 427bcd9e0ddSJohn Baldwin error = EINVAL; 428bcd9e0ddSJohn Baldwin else 429bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 430bcd9e0ddSJohn Baldwin fdrop(fp, td); 431bcd9e0ddSJohn Baldwin return (error); 432bcd9e0ddSJohn Baldwin } 433bcd9e0ddSJohn Baldwin 434bcd9e0ddSJohn Baldwin /* 435bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 436bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 437bcd9e0ddSJohn Baldwin */ 438bcd9e0ddSJohn Baldwin static int 439bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 440bcd9e0ddSJohn Baldwin struct thread *td; 441bcd9e0ddSJohn Baldwin int fd; 442bcd9e0ddSJohn Baldwin struct file *fp; 443bcd9e0ddSJohn Baldwin struct uio *auio; 444bcd9e0ddSJohn Baldwin off_t offset; 445bcd9e0ddSJohn Baldwin int flags; 446bcd9e0ddSJohn Baldwin { 447bcd9e0ddSJohn Baldwin ssize_t cnt; 448552afd9cSPoul-Henning Kamp int error; 449df8bae1dSRodney W. Grimes #ifdef KTRACE 450552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 451df8bae1dSRodney W. Grimes #endif 452df8bae1dSRodney W. Grimes 453552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 454552afd9cSPoul-Henning Kamp auio->uio_td = td; 455bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 456df8bae1dSRodney W. Grimes #ifdef KTRACE 457552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 458552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 459df8bae1dSRodney W. Grimes #endif 460552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 461a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 4629440653dSMatthew Dillon bwillwrite(); 463bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 464552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 465df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 466df8bae1dSRodney W. Grimes error = 0; 467bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 4686f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 469b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 470b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 471b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 47219eb87d2SJohn Baldwin } 473df8bae1dSRodney W. Grimes } 474552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 475df8bae1dSRodney W. Grimes #ifdef KTRACE 476552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 477552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 478b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 479df8bae1dSRodney W. Grimes } 480df8bae1dSRodney W. Grimes #endif 481b40ce416SJulian Elischer td->td_retval[0] = cnt; 482df8bae1dSRodney W. Grimes return (error); 483df8bae1dSRodney W. Grimes } 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes /* 4860c14ff0eSRobert Watson * Ioctl system call. 487df8bae1dSRodney W. Grimes */ 488d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 489df8bae1dSRodney W. Grimes struct ioctl_args { 490df8bae1dSRodney W. Grimes int fd; 491069e9bc1SDoug Rabson u_long com; 492df8bae1dSRodney W. Grimes caddr_t data; 493df8bae1dSRodney W. Grimes }; 494d2d3e875SBruce Evans #endif 495df8bae1dSRodney W. Grimes /* ARGSUSED */ 49626f9a767SRodney W. Grimes int 4973e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap) 498df8bae1dSRodney W. Grimes { 4993e15c66fSPoul-Henning Kamp u_long com; 5009fddcc66SRuslan Ermilov int arg, error; 5013e15c66fSPoul-Henning Kamp u_int size; 5029fddcc66SRuslan Ermilov caddr_t data; 503df8bae1dSRodney W. Grimes 5049fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 5059fc6aa06SPoul-Henning Kamp printf( 5069fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 5079fc6aa06SPoul-Henning Kamp td->td_proc->p_pid, td->td_proc->p_comm, uap->com); 5089fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 5099fc6aa06SPoul-Henning Kamp } 510d9f46233SJohn Baldwin com = uap->com; 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes /* 513df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 514df8bae1dSRodney W. Grimes * copied to/from the user's address space. 515df8bae1dSRodney W. Grimes */ 516df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 517ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 518ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 5192de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 5202de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 5212de92a38SPeter Wemm #else 5222de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 5232de92a38SPeter Wemm #endif 5249fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 525426da3bcSAlfred Perlstein return (ENOTTY); 526279d7226SMatthew Dillon 527ca51b19bSPoul-Henning Kamp if (size > 0) { 5289fddcc66SRuslan Ermilov if (!(com & IOC_VOID)) 5299fddcc66SRuslan Ermilov data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 5309fddcc66SRuslan Ermilov else { 5319fddcc66SRuslan Ermilov /* Integer argument. */ 5329fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 5339fddcc66SRuslan Ermilov data = (void *)&arg; 5349fddcc66SRuslan Ermilov size = 0; 535279d7226SMatthew Dillon } 5369fddcc66SRuslan Ermilov } else 5379fddcc66SRuslan Ermilov data = (void *)&uap->data; 538df8bae1dSRodney W. Grimes if (com & IOC_IN) { 539df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 540df8bae1dSRodney W. Grimes if (error) { 541a1b0a180SRuslan Ermilov if (size > 0) 5429fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 5433e15c66fSPoul-Henning Kamp return (error); 544df8bae1dSRodney W. Grimes } 545ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 546df8bae1dSRodney W. Grimes /* 547df8bae1dSRodney W. Grimes * Zero the buffer so the user always 548df8bae1dSRodney W. Grimes * gets back something deterministic. 549df8bae1dSRodney W. Grimes */ 550df8bae1dSRodney W. Grimes bzero(data, size); 551279d7226SMatthew Dillon } 552df8bae1dSRodney W. Grimes 553d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 554d9f46233SJohn Baldwin 555d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 556d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 557d9f46233SJohn Baldwin 5589fddcc66SRuslan Ermilov if (size > 0) 5599fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 560d9f46233SJohn Baldwin return (error); 561d9f46233SJohn Baldwin } 562d9f46233SJohn Baldwin 563d9f46233SJohn Baldwin int 564d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 565d9f46233SJohn Baldwin { 566d9f46233SJohn Baldwin struct file *fp; 567d9f46233SJohn Baldwin struct filedesc *fdp; 568d9f46233SJohn Baldwin int error; 569d9f46233SJohn Baldwin int tmp; 570d9f46233SJohn Baldwin 571d9f46233SJohn Baldwin if ((error = fget(td, fd, &fp)) != 0) 572d9f46233SJohn Baldwin return (error); 573d9f46233SJohn Baldwin if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 574d9f46233SJohn Baldwin fdrop(fp, td); 575d9f46233SJohn Baldwin return (EBADF); 576d9f46233SJohn Baldwin } 577d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 578d9f46233SJohn Baldwin switch (com) { 579d9f46233SJohn Baldwin case FIONCLEX: 580d9f46233SJohn Baldwin FILEDESC_LOCK_FAST(fdp); 581d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE; 582d9f46233SJohn Baldwin FILEDESC_UNLOCK_FAST(fdp); 583d9f46233SJohn Baldwin goto out; 584d9f46233SJohn Baldwin case FIOCLEX: 585d9f46233SJohn Baldwin FILEDESC_LOCK_FAST(fdp); 586d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 587d9f46233SJohn Baldwin FILEDESC_UNLOCK_FAST(fdp); 588d9f46233SJohn Baldwin goto out; 589d9f46233SJohn Baldwin case FIONBIO: 590426da3bcSAlfred Perlstein FILE_LOCK(fp); 591bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 592df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 593df8bae1dSRodney W. Grimes else 594df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 595426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 5968ccf264fSPoul-Henning Kamp data = (void *)&tmp; 597d9f46233SJohn Baldwin break; 598d9f46233SJohn Baldwin case FIOASYNC: 599426da3bcSAlfred Perlstein FILE_LOCK(fp); 600bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 601df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 602df8bae1dSRodney W. Grimes else 603df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 604426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 6058ccf264fSPoul-Henning Kamp data = (void *)&tmp; 606d9f46233SJohn Baldwin break; 607df8bae1dSRodney W. Grimes } 6088ccf264fSPoul-Henning Kamp 6098ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 610d9f46233SJohn Baldwin out: 611b40ce416SJulian Elischer fdrop(fp, td); 612df8bae1dSRodney W. Grimes return (error); 613df8bae1dSRodney W. Grimes } 614df8bae1dSRodney W. Grimes 61585f190e4SAlfred Perlstein /* 61685f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 61785f190e4SAlfred Perlstein */ 61885f190e4SAlfred Perlstein struct mtx sellock; 619265fc98fSSeigo Tanimura struct cv selwait; 6209ae6d334SKelly Yancey u_int nselcoll; /* Select collisions since boot */ 6219ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes /* 624df8bae1dSRodney W. Grimes * Select system call. 625df8bae1dSRodney W. Grimes */ 626d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 627df8bae1dSRodney W. Grimes struct select_args { 628b08f7993SSujal Patel int nd; 629df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 630df8bae1dSRodney W. Grimes struct timeval *tv; 631df8bae1dSRodney W. Grimes }; 632d2d3e875SBruce Evans #endif 63326f9a767SRodney W. Grimes int 634b40ce416SJulian Elischer select(td, uap) 635b40ce416SJulian Elischer register struct thread *td; 636df8bae1dSRodney W. Grimes register struct select_args *uap; 637df8bae1dSRodney W. Grimes { 6388f19eb88SIan Dowse struct timeval tv, *tvp; 6398f19eb88SIan Dowse int error; 6408f19eb88SIan Dowse 6418f19eb88SIan Dowse if (uap->tv != NULL) { 6428f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 6438f19eb88SIan Dowse if (error) 6448f19eb88SIan Dowse return (error); 6458f19eb88SIan Dowse tvp = &tv; 6468f19eb88SIan Dowse } else 6478f19eb88SIan Dowse tvp = NULL; 6488f19eb88SIan Dowse 6498f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 6508f19eb88SIan Dowse } 6518f19eb88SIan Dowse 6528f19eb88SIan Dowse int 6538f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 6548f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 6558f19eb88SIan Dowse { 656426da3bcSAlfred Perlstein struct filedesc *fdp; 657d5e4d7e1SBruce Evans /* 658d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 659d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 660d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 661d5e4d7e1SBruce Evans * of 256. 662d5e4d7e1SBruce Evans */ 663d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 664eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 66500af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 6669ae6d334SKelly Yancey int error, timo; 6679ae6d334SKelly Yancey u_int ncoll, nbufbytes, ncpbytes, nfdbits; 668df8bae1dSRodney W. Grimes 6698f19eb88SIan Dowse if (nd < 0) 670acbfbfeaSSujal Patel return (EINVAL); 671426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 672db446e30SPoul-Henning Kamp 673124e4c3bSPoul-Henning Kamp FILEDESC_LOCK_FAST(fdp); 674ad2edad9SMatthew Dillon 6758f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 6768f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 677124e4c3bSPoul-Henning Kamp FILEDESC_UNLOCK_FAST(fdp); 678b08f7993SSujal Patel 679d5e4d7e1SBruce Evans /* 680d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 681d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 682d5e4d7e1SBruce Evans */ 6838f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 684d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 685d5e4d7e1SBruce Evans nbufbytes = 0; 6868f19eb88SIan Dowse if (fd_in != NULL) 687d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 6888f19eb88SIan Dowse if (fd_ou != NULL) 689d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 6908f19eb88SIan Dowse if (fd_ex != NULL) 691d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 692d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 693d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 694d5e4d7e1SBruce Evans else 695a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 696b08f7993SSujal Patel 697b08f7993SSujal Patel /* 698d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 699d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 700d5e4d7e1SBruce Evans * together. 701b08f7993SSujal Patel */ 702d5e4d7e1SBruce Evans sbp = selbits; 703df8bae1dSRodney W. Grimes #define getbits(name, x) \ 704d5e4d7e1SBruce Evans do { \ 7058f19eb88SIan Dowse if (name == NULL) \ 706d5e4d7e1SBruce Evans ibits[x] = NULL; \ 707d5e4d7e1SBruce Evans else { \ 708d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 709d5e4d7e1SBruce Evans obits[x] = sbp; \ 710d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 7118f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 712265fc98fSSeigo Tanimura if (error != 0) \ 71385f190e4SAlfred Perlstein goto done_nosellock; \ 714e04ac2feSJohn Baldwin } \ 715d5e4d7e1SBruce Evans } while (0) 7168f19eb88SIan Dowse getbits(fd_in, 0); 7178f19eb88SIan Dowse getbits(fd_ou, 1); 7188f19eb88SIan Dowse getbits(fd_ex, 2); 719df8bae1dSRodney W. Grimes #undef getbits 720d5e4d7e1SBruce Evans if (nbufbytes != 0) 721d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 722df8bae1dSRodney W. Grimes 7238f19eb88SIan Dowse if (tvp != NULL) { 7248f19eb88SIan Dowse atv = *tvp; 725df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 726df8bae1dSRodney W. Grimes error = EINVAL; 72785f190e4SAlfred Perlstein goto done_nosellock; 728df8bae1dSRodney W. Grimes } 729c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 73000af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 7319c386f6bSJohn Baldwin } else { 73200af9731SPoul-Henning Kamp atv.tv_sec = 0; 7339c386f6bSJohn Baldwin atv.tv_usec = 0; 7349c386f6bSJohn Baldwin } 73500af9731SPoul-Henning Kamp timo = 0; 7362149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 73785f190e4SAlfred Perlstein mtx_lock(&sellock); 738df8bae1dSRodney W. Grimes retry: 739df8bae1dSRodney W. Grimes ncoll = nselcoll; 740fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 741b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 742fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 74385f190e4SAlfred Perlstein mtx_unlock(&sellock); 74485f190e4SAlfred Perlstein 7458f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 74685f190e4SAlfred Perlstein mtx_lock(&sellock); 747b40ce416SJulian Elischer if (error || td->td_retval[0]) 748df8bae1dSRodney W. Grimes goto done; 7494da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 750c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 75185f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 752df8bae1dSRodney W. Grimes goto done; 75300af9731SPoul-Henning Kamp ttv = atv; 75400af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 75500af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 75600af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 757df8bae1dSRodney W. Grimes } 75885f190e4SAlfred Perlstein 75985f190e4SAlfred Perlstein /* 76085f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 76185f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 76285f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 76385f190e4SAlfred Perlstein * necessary. 76485f190e4SAlfred Perlstein */ 765fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 76685f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 76785f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 76885f190e4SAlfred Perlstein goto retry; 76985f190e4SAlfred Perlstein } 770fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 771bfbbc4aaSJason Evans 772265fc98fSSeigo Tanimura if (timo > 0) 77385f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 774265fc98fSSeigo Tanimura else 77585f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 776bfbbc4aaSJason Evans 777df8bae1dSRodney W. Grimes if (error == 0) 778df8bae1dSRodney W. Grimes goto retry; 779265fc98fSSeigo Tanimura 780df8bae1dSRodney W. Grimes done: 78185f190e4SAlfred Perlstein clear_selinfo_list(td); 782fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 783b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 784fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 78585f190e4SAlfred Perlstein mtx_unlock(&sellock); 78685f190e4SAlfred Perlstein 78785f190e4SAlfred Perlstein done_nosellock: 788df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 789df8bae1dSRodney W. Grimes if (error == ERESTART) 790df8bae1dSRodney W. Grimes error = EINTR; 791df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 792df8bae1dSRodney W. Grimes error = 0; 793df8bae1dSRodney W. Grimes #define putbits(name, x) \ 7948f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 795df8bae1dSRodney W. Grimes error = error2; 796df8bae1dSRodney W. Grimes if (error == 0) { 797df8bae1dSRodney W. Grimes int error2; 798df8bae1dSRodney W. Grimes 7998f19eb88SIan Dowse putbits(fd_in, 0); 8008f19eb88SIan Dowse putbits(fd_ou, 1); 8018f19eb88SIan Dowse putbits(fd_ex, 2); 802df8bae1dSRodney W. Grimes #undef putbits 803df8bae1dSRodney W. Grimes } 804d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 805d5e4d7e1SBruce Evans free(selbits, M_SELECT); 806ad2edad9SMatthew Dillon 807df8bae1dSRodney W. Grimes return (error); 808df8bae1dSRodney W. Grimes } 809df8bae1dSRodney W. Grimes 810265fc98fSSeigo Tanimura static int 811b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 812b40ce416SJulian Elischer struct thread *td; 813b08f7993SSujal Patel fd_mask **ibits, **obits; 814cb226aaaSPoul-Henning Kamp int nfd; 815df8bae1dSRodney W. Grimes { 816f082218cSPeter Wemm int msk, i, fd; 817f082218cSPeter Wemm fd_mask bits; 818df8bae1dSRodney W. Grimes struct file *fp; 819df8bae1dSRodney W. Grimes int n = 0; 8202087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 82142d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 822eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 823df8bae1dSRodney W. Grimes 824eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 825df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 826d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 827d5e4d7e1SBruce Evans continue; 828df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 829b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 830f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 831f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 832f082218cSPeter Wemm if (!(bits & 1)) 833f082218cSPeter Wemm continue; 834eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 835eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 836df8bae1dSRodney W. Grimes return (EBADF); 837eb209311SAlfred Perlstein } 838ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 839ea6027a8SRobert Watson td)) { 840b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 841f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 842df8bae1dSRodney W. Grimes n++; 843df8bae1dSRodney W. Grimes } 844df8bae1dSRodney W. Grimes } 845df8bae1dSRodney W. Grimes } 846df8bae1dSRodney W. Grimes } 847eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 848b40ce416SJulian Elischer td->td_retval[0] = n; 849df8bae1dSRodney W. Grimes return (0); 850df8bae1dSRodney W. Grimes } 851df8bae1dSRodney W. Grimes 85242d11757SPeter Wemm /* 85342d11757SPeter Wemm * Poll system call. 85442d11757SPeter Wemm */ 85542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 85642d11757SPeter Wemm struct poll_args { 85742d11757SPeter Wemm struct pollfd *fds; 85842d11757SPeter Wemm u_int nfds; 85942d11757SPeter Wemm int timeout; 86042d11757SPeter Wemm }; 86142d11757SPeter Wemm #endif 86242d11757SPeter Wemm int 863b40ce416SJulian Elischer poll(td, uap) 864b40ce416SJulian Elischer struct thread *td; 865ea0237edSJonathan Lemon struct poll_args *uap; 86642d11757SPeter Wemm { 8672580f4e5SAndre Oppermann struct pollfd *bits; 8682580f4e5SAndre Oppermann struct pollfd smallbits[32]; 86900af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 8709ae6d334SKelly Yancey int error = 0, timo; 8719ae6d334SKelly Yancey u_int ncoll, nfds; 87242d11757SPeter Wemm size_t ni; 87342d11757SPeter Wemm 874d1e405c5SAlfred Perlstein nfds = uap->nfds; 875ad2edad9SMatthew Dillon 8765d8dd01dSRobert Watson /* 8772bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 8782bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 8792bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 8802bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 8812bd5ac33SPeter Wemm * safe, but not overly restrictive. 88289b71647SPeter Wemm */ 88391d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 88491d5354aSJohn Baldwin if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) && 885b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 88691d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 887ad2edad9SMatthew Dillon error = EINVAL; 888ad2edad9SMatthew Dillon goto done2; 889ad2edad9SMatthew Dillon } 89091d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 89189b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 89242d11757SPeter Wemm if (ni > sizeof(smallbits)) 893a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 89442d11757SPeter Wemm else 89542d11757SPeter Wemm bits = smallbits; 896d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 89742d11757SPeter Wemm if (error) 89885f190e4SAlfred Perlstein goto done_nosellock; 899d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 900d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 901d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 90242d11757SPeter Wemm if (itimerfix(&atv)) { 90342d11757SPeter Wemm error = EINVAL; 90485f190e4SAlfred Perlstein goto done_nosellock; 90542d11757SPeter Wemm } 906c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 90700af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9089c386f6bSJohn Baldwin } else { 90900af9731SPoul-Henning Kamp atv.tv_sec = 0; 9109c386f6bSJohn Baldwin atv.tv_usec = 0; 9119c386f6bSJohn Baldwin } 91200af9731SPoul-Henning Kamp timo = 0; 9132149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 91485f190e4SAlfred Perlstein mtx_lock(&sellock); 91542d11757SPeter Wemm retry: 91642d11757SPeter Wemm ncoll = nselcoll; 917fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 918b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 919fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 92085f190e4SAlfred Perlstein mtx_unlock(&sellock); 92185f190e4SAlfred Perlstein 9222580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 92385f190e4SAlfred Perlstein mtx_lock(&sellock); 924b40ce416SJulian Elischer if (error || td->td_retval[0]) 92542d11757SPeter Wemm goto done; 9264da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 927c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 92885f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 92942d11757SPeter Wemm goto done; 93000af9731SPoul-Henning Kamp ttv = atv; 93100af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 93200af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 93300af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 93442d11757SPeter Wemm } 93585f190e4SAlfred Perlstein /* 93685f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 93785f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 93885f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 93985f190e4SAlfred Perlstein */ 940fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 94185f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 942fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 94385f190e4SAlfred Perlstein goto retry; 94485f190e4SAlfred Perlstein } 94585f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 94685f190e4SAlfred Perlstein 947265fc98fSSeigo Tanimura if (timo > 0) 94885f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 949265fc98fSSeigo Tanimura else 95085f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 95185f190e4SAlfred Perlstein 95242d11757SPeter Wemm if (error == 0) 95342d11757SPeter Wemm goto retry; 954265fc98fSSeigo Tanimura 95542d11757SPeter Wemm done: 95685f190e4SAlfred Perlstein clear_selinfo_list(td); 957fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 958b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 959fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 96085f190e4SAlfred Perlstein mtx_unlock(&sellock); 96185f190e4SAlfred Perlstein 96285f190e4SAlfred Perlstein done_nosellock: 96342d11757SPeter Wemm /* poll is not restarted after signals... */ 96442d11757SPeter Wemm if (error == ERESTART) 96542d11757SPeter Wemm error = EINTR; 96642d11757SPeter Wemm if (error == EWOULDBLOCK) 96742d11757SPeter Wemm error = 0; 96842d11757SPeter Wemm if (error == 0) { 969d1e405c5SAlfred Perlstein error = copyout(bits, uap->fds, ni); 97042d11757SPeter Wemm if (error) 97142d11757SPeter Wemm goto out; 97242d11757SPeter Wemm } 97342d11757SPeter Wemm out: 97442d11757SPeter Wemm if (ni > sizeof(smallbits)) 97542d11757SPeter Wemm free(bits, M_TEMP); 976ad2edad9SMatthew Dillon done2: 97742d11757SPeter Wemm return (error); 97842d11757SPeter Wemm } 97942d11757SPeter Wemm 98042d11757SPeter Wemm static int 981b40ce416SJulian Elischer pollscan(td, fds, nfd) 982b40ce416SJulian Elischer struct thread *td; 98342d11757SPeter Wemm struct pollfd *fds; 984ea0237edSJonathan Lemon u_int nfd; 98542d11757SPeter Wemm { 986b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 98742d11757SPeter Wemm int i; 98842d11757SPeter Wemm struct file *fp; 98942d11757SPeter Wemm int n = 0; 99042d11757SPeter Wemm 991426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 992eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 993337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 99442d11757SPeter Wemm fds->revents = POLLNVAL; 99542d11757SPeter Wemm n++; 996337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 997337c9691SJordan K. Hubbard fds->revents = 0; 99842d11757SPeter Wemm } else { 99942d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1000279d7226SMatthew Dillon if (fp == NULL) { 100142d11757SPeter Wemm fds->revents = POLLNVAL; 100242d11757SPeter Wemm n++; 100342d11757SPeter Wemm } else { 10042087c896SBruce Evans /* 10052087c896SBruce Evans * Note: backend also returns POLLHUP and 10062087c896SBruce Evans * POLLERR if appropriate. 10072087c896SBruce Evans */ 100813ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1009ea6027a8SRobert Watson td->td_ucred, td); 101042d11757SPeter Wemm if (fds->revents != 0) 101142d11757SPeter Wemm n++; 101242d11757SPeter Wemm } 101342d11757SPeter Wemm } 101442d11757SPeter Wemm } 1015eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1016b40ce416SJulian Elischer td->td_retval[0] = n; 101742d11757SPeter Wemm return (0); 101842d11757SPeter Wemm } 101942d11757SPeter Wemm 102042d11757SPeter Wemm /* 102142d11757SPeter Wemm * OpenBSD poll system call. 10220c14ff0eSRobert Watson * 102342d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 102442d11757SPeter Wemm */ 102542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 102642d11757SPeter Wemm struct openbsd_poll_args { 102742d11757SPeter Wemm struct pollfd *fds; 102842d11757SPeter Wemm u_int nfds; 102942d11757SPeter Wemm int timeout; 103042d11757SPeter Wemm }; 103142d11757SPeter Wemm #endif 103242d11757SPeter Wemm int 1033b40ce416SJulian Elischer openbsd_poll(td, uap) 1034b40ce416SJulian Elischer register struct thread *td; 103542d11757SPeter Wemm register struct openbsd_poll_args *uap; 103642d11757SPeter Wemm { 1037b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 103842d11757SPeter Wemm } 103942d11757SPeter Wemm 104085f190e4SAlfred Perlstein /* 10410c14ff0eSRobert Watson * Remove the references to the thread from all of the objects we were 10420c14ff0eSRobert Watson * polling. 104385f190e4SAlfred Perlstein * 10440c14ff0eSRobert Watson * This code assumes that the underlying owner of the selinfo structure will 10450c14ff0eSRobert Watson * hold sellock before it changes it, and that it will unlink itself from our 10460c14ff0eSRobert Watson * list if it goes away. 104785f190e4SAlfred Perlstein */ 104885f190e4SAlfred Perlstein void 104985f190e4SAlfred Perlstein clear_selinfo_list(td) 105085f190e4SAlfred Perlstein struct thread *td; 105185f190e4SAlfred Perlstein { 105285f190e4SAlfred Perlstein struct selinfo *si; 105385f190e4SAlfred Perlstein 105485f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 105585f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 105685f190e4SAlfred Perlstein si->si_thread = NULL; 105785f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 105885f190e4SAlfred Perlstein } 105985f190e4SAlfred Perlstein 1060df8bae1dSRodney W. Grimes /* 1061df8bae1dSRodney W. Grimes * Record a select request. 1062df8bae1dSRodney W. Grimes */ 1063df8bae1dSRodney W. Grimes void 1064df8bae1dSRodney W. Grimes selrecord(selector, sip) 1065b40ce416SJulian Elischer struct thread *selector; 1066df8bae1dSRodney W. Grimes struct selinfo *sip; 1067df8bae1dSRodney W. Grimes { 1068df8bae1dSRodney W. Grimes 106985f190e4SAlfred Perlstein mtx_lock(&sellock); 107085f190e4SAlfred Perlstein /* 1071b605b54cSAlfred Perlstein * If the selinfo's thread pointer is NULL then take ownership of it. 1072b605b54cSAlfred Perlstein * 1073b605b54cSAlfred Perlstein * If the thread pointer is not NULL and it points to another 1074b605b54cSAlfred Perlstein * thread, then we have a collision. 1075b605b54cSAlfred Perlstein * 1076b605b54cSAlfred Perlstein * If the thread pointer is not NULL and points back to us then leave 1077b605b54cSAlfred Perlstein * it alone as we've already added pointed it at us and added it to 1078b605b54cSAlfred Perlstein * our list. 107985f190e4SAlfred Perlstein */ 108085f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1081b40ce416SJulian Elischer sip->si_thread = selector; 108285f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 108385f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 108485f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 108585f190e4SAlfred Perlstein } 108685f190e4SAlfred Perlstein 108785f190e4SAlfred Perlstein mtx_unlock(&sellock); 1088df8bae1dSRodney W. Grimes } 1089df8bae1dSRodney W. Grimes 1090512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1091df8bae1dSRodney W. Grimes void 1092df8bae1dSRodney W. Grimes selwakeup(sip) 109385f190e4SAlfred Perlstein struct selinfo *sip; 1094df8bae1dSRodney W. Grimes { 1095512824f8SSeigo Tanimura doselwakeup(sip, -1); 1096512824f8SSeigo Tanimura } 1097512824f8SSeigo Tanimura 1098512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1099512824f8SSeigo Tanimura void 1100512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1101512824f8SSeigo Tanimura struct selinfo *sip; 1102512824f8SSeigo Tanimura int pri; 1103512824f8SSeigo Tanimura { 1104512824f8SSeigo Tanimura doselwakeup(sip, pri); 1105512824f8SSeigo Tanimura } 1106512824f8SSeigo Tanimura 1107512824f8SSeigo Tanimura /* 1108512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1109512824f8SSeigo Tanimura */ 1110512824f8SSeigo Tanimura static void 1111512824f8SSeigo Tanimura doselwakeup(sip, pri) 1112512824f8SSeigo Tanimura struct selinfo *sip; 1113512824f8SSeigo Tanimura int pri; 1114512824f8SSeigo Tanimura { 1115b40ce416SJulian Elischer struct thread *td; 1116df8bae1dSRodney W. Grimes 111785f190e4SAlfred Perlstein mtx_lock(&sellock); 111885f190e4SAlfred Perlstein td = sip->si_thread; 111985f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1120df8bae1dSRodney W. Grimes nselcoll++; 1121df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1122512824f8SSeigo Tanimura cv_broadcastpri(&selwait, pri); 1123df8bae1dSRodney W. Grimes } 112485f190e4SAlfred Perlstein if (td == NULL) { 112585f190e4SAlfred Perlstein mtx_unlock(&sellock); 1126b40ce416SJulian Elischer return; 1127b40ce416SJulian Elischer } 112885f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 112985f190e4SAlfred Perlstein sip->si_thread = NULL; 11309ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1131b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 113233a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 113344f3b092SJohn Baldwin sleepq_remove(td, &selwait); 113485f190e4SAlfred Perlstein mtx_unlock(&sellock); 1135df8bae1dSRodney W. Grimes } 1136265fc98fSSeigo Tanimura 11374d77a549SAlfred Perlstein static void selectinit(void *); 1138265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1139265fc98fSSeigo Tanimura 1140265fc98fSSeigo Tanimura /* ARGSUSED*/ 1141265fc98fSSeigo Tanimura static void 1142265fc98fSSeigo Tanimura selectinit(dummy) 1143265fc98fSSeigo Tanimura void *dummy; 1144265fc98fSSeigo Tanimura { 1145265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 11466008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1147265fc98fSSeigo Tanimura } 1148