1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41677b542eSDavid E. O'Brien #include <sys/cdefs.h> 42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 43677b542eSDavid E. O'Brien 44db6a20e2SGarrett Wollman #include "opt_ktrace.h" 45db6a20e2SGarrett Wollman 46df8bae1dSRodney W. Grimes #include <sys/param.h> 47df8bae1dSRodney W. Grimes #include <sys/systm.h> 48d2d3e875SBruce Evans #include <sys/sysproto.h> 49df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 5020982410SBruce Evans #include <sys/filio.h> 513ac4d1efSBruce Evans #include <sys/fcntl.h> 52df8bae1dSRodney W. Grimes #include <sys/file.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 54797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 55df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 56df8bae1dSRodney W. Grimes #include <sys/uio.h> 57df8bae1dSRodney W. Grimes #include <sys/kernel.h> 58104a9b7eSAlexander Kabaev #include <sys/limits.h> 59df8bae1dSRodney W. Grimes #include <sys/malloc.h> 6042d11757SPeter Wemm #include <sys/poll.h> 6189b71647SPeter Wemm #include <sys/resourcevar.h> 620a2c3d48SGarrett Wollman #include <sys/selinfo.h> 638f19eb88SIan Dowse #include <sys/syscallsubr.h> 648cb96f20SPeter Wemm #include <sys/sysctl.h> 6542d11757SPeter Wemm #include <sys/sysent.h> 66279d7226SMatthew Dillon #include <sys/bio.h> 67279d7226SMatthew Dillon #include <sys/buf.h> 68265fc98fSSeigo Tanimura #include <sys/condvar.h> 69df8bae1dSRodney W. Grimes #ifdef KTRACE 70df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 71df8bae1dSRodney W. Grimes #endif 72279d7226SMatthew Dillon #include <vm/vm.h> 73279d7226SMatthew Dillon #include <vm/vm_page.h> 74df8bae1dSRodney W. Grimes 75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 76a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 77a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7855166637SPoul-Henning Kamp 79bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 80bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 81bbbb04ceSAlfred Perlstein static int dofileread(struct thread *, struct file *, int, void *, 82bbbb04ceSAlfred Perlstein size_t, off_t, int); 83bbbb04ceSAlfred Perlstein static int dofilewrite(struct thread *, struct file *, int, 84bbbb04ceSAlfred Perlstein const void *, size_t, off_t, int); 85512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 868fe387abSDmitrij Tejblum 87df8bae1dSRodney W. Grimes /* 88df8bae1dSRodney W. Grimes * Read system call. 89df8bae1dSRodney W. Grimes */ 90d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 91df8bae1dSRodney W. Grimes struct read_args { 92df8bae1dSRodney W. Grimes int fd; 93134e06feSBruce Evans void *buf; 94134e06feSBruce Evans size_t nbyte; 95df8bae1dSRodney W. Grimes }; 96d2d3e875SBruce Evans #endif 97ad2edad9SMatthew Dillon /* 98ad2edad9SMatthew Dillon * MPSAFE 99ad2edad9SMatthew Dillon */ 10026f9a767SRodney W. Grimes int 101b40ce416SJulian Elischer read(td, uap) 102b40ce416SJulian Elischer struct thread *td; 103b064d43dSMatthew Dillon struct read_args *uap; 104df8bae1dSRodney W. Grimes { 105b064d43dSMatthew Dillon struct file *fp; 106279d7226SMatthew Dillon int error; 107df8bae1dSRodney W. Grimes 108b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) == 0) { 109b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, 110ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 111b40ce416SJulian Elischer fdrop(fp, td); 112ad2edad9SMatthew Dillon } 113279d7226SMatthew Dillon return(error); 114df8bae1dSRodney W. Grimes } 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes /* 1178fe387abSDmitrij Tejblum * Pread system call 1184160ccd9SAlan Cox */ 1194160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1204160ccd9SAlan Cox struct pread_args { 1214160ccd9SAlan Cox int fd; 1224160ccd9SAlan Cox void *buf; 1234160ccd9SAlan Cox size_t nbyte; 1248fe387abSDmitrij Tejblum int pad; 1254160ccd9SAlan Cox off_t offset; 1264160ccd9SAlan Cox }; 1274160ccd9SAlan Cox #endif 128ad2edad9SMatthew Dillon /* 129ad2edad9SMatthew Dillon * MPSAFE 130ad2edad9SMatthew Dillon */ 1314160ccd9SAlan Cox int 132b40ce416SJulian Elischer pread(td, uap) 133b40ce416SJulian Elischer struct thread *td; 134b064d43dSMatthew Dillon struct pread_args *uap; 1354160ccd9SAlan Cox { 136b064d43dSMatthew Dillon struct file *fp; 137279d7226SMatthew Dillon int error; 1388fe387abSDmitrij Tejblum 13997fa4397SAlfred Perlstein if ((error = fget_read(td, uap->fd, &fp)) != 0) 14097fa4397SAlfred Perlstein return (error); 1412db4b023SPoul-Henning Kamp if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) { 142b064d43dSMatthew Dillon error = ESPIPE; 143426da3bcSAlfred Perlstein } else { 144426da3bcSAlfred Perlstein error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte, 145426da3bcSAlfred Perlstein uap->offset, FOF_OFFSET); 146b064d43dSMatthew Dillon } 147b40ce416SJulian Elischer fdrop(fp, td); 148279d7226SMatthew Dillon return(error); 1498fe387abSDmitrij Tejblum } 1508fe387abSDmitrij Tejblum 1518fe387abSDmitrij Tejblum /* 1528fe387abSDmitrij Tejblum * Code common for read and pread 1538fe387abSDmitrij Tejblum */ 15437c84183SPoul-Henning Kamp static int 155b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 156b40ce416SJulian Elischer struct thread *td; 1578fe387abSDmitrij Tejblum struct file *fp; 1588fe387abSDmitrij Tejblum int fd, flags; 1598fe387abSDmitrij Tejblum void *buf; 1608fe387abSDmitrij Tejblum size_t nbyte; 1618fe387abSDmitrij Tejblum off_t offset; 1628fe387abSDmitrij Tejblum { 1634160ccd9SAlan Cox struct uio auio; 1644160ccd9SAlan Cox struct iovec aiov; 1654160ccd9SAlan Cox long cnt, error = 0; 1664160ccd9SAlan Cox #ifdef KTRACE 1674160ccd9SAlan Cox struct iovec ktriov; 16842ebfbf2SBrian Feldman struct uio ktruio; 1693c89e357SBrian Feldman int didktr = 0; 1704160ccd9SAlan Cox #endif 1714160ccd9SAlan Cox 1720a3e28cfSAlfred Perlstein aiov.iov_base = buf; 1738fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 1744160ccd9SAlan Cox auio.uio_iov = &aiov; 1754160ccd9SAlan Cox auio.uio_iovcnt = 1; 1768fe387abSDmitrij Tejblum auio.uio_offset = offset; 1778fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 1784160ccd9SAlan Cox return (EINVAL); 1798fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 1804160ccd9SAlan Cox auio.uio_rw = UIO_READ; 1814160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 182b40ce416SJulian Elischer auio.uio_td = td; 1834160ccd9SAlan Cox #ifdef KTRACE 1844160ccd9SAlan Cox /* 1854160ccd9SAlan Cox * if tracing, save a copy of iovec 1864160ccd9SAlan Cox */ 18760a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 1884160ccd9SAlan Cox ktriov = aiov; 18942ebfbf2SBrian Feldman ktruio = auio; 1903c89e357SBrian Feldman didktr = 1; 19142ebfbf2SBrian Feldman } 1924160ccd9SAlan Cox #endif 1938fe387abSDmitrij Tejblum cnt = nbyte; 194279d7226SMatthew Dillon 1959ca43589SRobert Watson if ((error = fo_read(fp, &auio, td->td_ucred, flags, td))) { 1964160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 1974160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 1984160ccd9SAlan Cox error = 0; 199279d7226SMatthew Dillon } 2004160ccd9SAlan Cox cnt -= auio.uio_resid; 2014160ccd9SAlan Cox #ifdef KTRACE 2023c89e357SBrian Feldman if (didktr && error == 0) { 20342ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 20442ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 20560a9bb19SJohn Baldwin ktrgenio(fd, UIO_READ, &ktruio, error); 20642ebfbf2SBrian Feldman } 2074160ccd9SAlan Cox #endif 208b40ce416SJulian Elischer td->td_retval[0] = cnt; 2094160ccd9SAlan Cox return (error); 2104160ccd9SAlan Cox } 2114160ccd9SAlan Cox 2124160ccd9SAlan Cox /* 213df8bae1dSRodney W. Grimes * Scatter read system call. 214df8bae1dSRodney W. Grimes */ 215d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 216df8bae1dSRodney W. Grimes struct readv_args { 2177147b19dSBruce Evans int fd; 218df8bae1dSRodney W. Grimes struct iovec *iovp; 219df8bae1dSRodney W. Grimes u_int iovcnt; 220df8bae1dSRodney W. Grimes }; 221d2d3e875SBruce Evans #endif 222ad2edad9SMatthew Dillon /* 223ad2edad9SMatthew Dillon * MPSAFE 224ad2edad9SMatthew Dillon */ 22526f9a767SRodney W. Grimes int 226b40ce416SJulian Elischer readv(td, uap) 227b40ce416SJulian Elischer struct thread *td; 228b064d43dSMatthew Dillon struct readv_args *uap; 229df8bae1dSRodney W. Grimes { 230b064d43dSMatthew Dillon struct file *fp; 231df8bae1dSRodney W. Grimes struct uio auio; 232b064d43dSMatthew Dillon struct iovec *iov; 233df8bae1dSRodney W. Grimes struct iovec *needfree; 234df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 23582641acdSAlan Cox long i, cnt; 23682641acdSAlan Cox int error; 237df8bae1dSRodney W. Grimes u_int iovlen; 238df8bae1dSRodney W. Grimes #ifdef KTRACE 239df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 24042ebfbf2SBrian Feldman struct uio ktruio; 241df8bae1dSRodney W. Grimes #endif 242df8bae1dSRodney W. Grimes 243b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) != 0) 24482641acdSAlan Cox return (error); 24582641acdSAlan Cox needfree = NULL; 246df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 247df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 248df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 249ad2edad9SMatthew Dillon if (uap->iovcnt > UIO_MAXIOV) { 250ad2edad9SMatthew Dillon error = EINVAL; 25182641acdSAlan Cox goto done; 252ad2edad9SMatthew Dillon } 253a163d034SWarner Losh MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 254df8bae1dSRodney W. Grimes needfree = iov; 25582641acdSAlan Cox } else 256df8bae1dSRodney W. Grimes iov = aiov; 257df8bae1dSRodney W. Grimes auio.uio_iov = iov; 258df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 259df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 260df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 261b40ce416SJulian Elischer auio.uio_td = td; 2622c1011f7SJohn Dyson auio.uio_offset = -1; 2630a3e28cfSAlfred Perlstein if ((error = copyin(uap->iovp, iov, iovlen))) 264df8bae1dSRodney W. Grimes goto done; 265df8bae1dSRodney W. Grimes auio.uio_resid = 0; 266df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 267069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 268df8bae1dSRodney W. Grimes error = EINVAL; 269df8bae1dSRodney W. Grimes goto done; 270df8bae1dSRodney W. Grimes } 271069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 272df8bae1dSRodney W. Grimes iov++; 273df8bae1dSRodney W. Grimes } 274df8bae1dSRodney W. Grimes #ifdef KTRACE 275df8bae1dSRodney W. Grimes /* 276df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 277df8bae1dSRodney W. Grimes */ 27860a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 279a163d034SWarner Losh MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 2800a3e28cfSAlfred Perlstein bcopy(auio.uio_iov, ktriov, iovlen); 28142ebfbf2SBrian Feldman ktruio = auio; 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes #endif 284df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 2859ca43589SRobert Watson if ((error = fo_read(fp, &auio, td->td_ucred, 0, td))) { 286df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 287df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 288df8bae1dSRodney W. Grimes error = 0; 289279d7226SMatthew Dillon } 290df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 291df8bae1dSRodney W. Grimes #ifdef KTRACE 292df8bae1dSRodney W. Grimes if (ktriov != NULL) { 29342ebfbf2SBrian Feldman if (error == 0) { 29442ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 29542ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 29660a9bb19SJohn Baldwin ktrgenio(uap->fd, UIO_READ, &ktruio, error); 29742ebfbf2SBrian Feldman } 298df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 299df8bae1dSRodney W. Grimes } 300df8bae1dSRodney W. Grimes #endif 301b40ce416SJulian Elischer td->td_retval[0] = cnt; 302df8bae1dSRodney W. Grimes done: 303b40ce416SJulian Elischer fdrop(fp, td); 304df8bae1dSRodney W. Grimes if (needfree) 305df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 306df8bae1dSRodney W. Grimes return (error); 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * Write system call 311df8bae1dSRodney W. Grimes */ 312d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 313df8bae1dSRodney W. Grimes struct write_args { 314df8bae1dSRodney W. Grimes int fd; 315134e06feSBruce Evans const void *buf; 316134e06feSBruce Evans size_t nbyte; 317df8bae1dSRodney W. Grimes }; 318d2d3e875SBruce Evans #endif 319ad2edad9SMatthew Dillon /* 320ad2edad9SMatthew Dillon * MPSAFE 321ad2edad9SMatthew Dillon */ 32226f9a767SRodney W. Grimes int 323b40ce416SJulian Elischer write(td, uap) 324b40ce416SJulian Elischer struct thread *td; 325b064d43dSMatthew Dillon struct write_args *uap; 326df8bae1dSRodney W. Grimes { 327b064d43dSMatthew Dillon struct file *fp; 328279d7226SMatthew Dillon int error; 329df8bae1dSRodney W. Grimes 330b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 331b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 332ad2edad9SMatthew Dillon (off_t)-1, 0); 333b40ce416SJulian Elischer fdrop(fp, td); 334ad2edad9SMatthew Dillon } else { 335b064d43dSMatthew Dillon error = EBADF; /* XXX this can't be right */ 336ad2edad9SMatthew Dillon } 337279d7226SMatthew Dillon return(error); 338df8bae1dSRodney W. Grimes } 339df8bae1dSRodney W. Grimes 340df8bae1dSRodney W. Grimes /* 3418fe387abSDmitrij Tejblum * Pwrite system call 3424160ccd9SAlan Cox */ 3434160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3444160ccd9SAlan Cox struct pwrite_args { 3454160ccd9SAlan Cox int fd; 3464160ccd9SAlan Cox const void *buf; 3474160ccd9SAlan Cox size_t nbyte; 3488fe387abSDmitrij Tejblum int pad; 3494160ccd9SAlan Cox off_t offset; 3504160ccd9SAlan Cox }; 3514160ccd9SAlan Cox #endif 352ad2edad9SMatthew Dillon /* 353ad2edad9SMatthew Dillon * MPSAFE 354ad2edad9SMatthew Dillon */ 3554160ccd9SAlan Cox int 356b40ce416SJulian Elischer pwrite(td, uap) 357b40ce416SJulian Elischer struct thread *td; 358b064d43dSMatthew Dillon struct pwrite_args *uap; 3594160ccd9SAlan Cox { 360b064d43dSMatthew Dillon struct file *fp; 361279d7226SMatthew Dillon int error; 3628fe387abSDmitrij Tejblum 363b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 3642db4b023SPoul-Henning Kamp if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) { 3652db4b023SPoul-Henning Kamp error = ESPIPE; 3662db4b023SPoul-Henning Kamp } else { 367b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 368b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 369b064d43dSMatthew Dillon } 370b40ce416SJulian Elischer fdrop(fp, td); 371279d7226SMatthew Dillon } else { 372b064d43dSMatthew Dillon error = EBADF; /* this can't be right */ 373ad2edad9SMatthew Dillon } 374279d7226SMatthew Dillon return(error); 3758fe387abSDmitrij Tejblum } 3768fe387abSDmitrij Tejblum 3778fe387abSDmitrij Tejblum static int 378b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 379b40ce416SJulian Elischer struct thread *td; 3808fe387abSDmitrij Tejblum struct file *fp; 3818fe387abSDmitrij Tejblum int fd, flags; 3828fe387abSDmitrij Tejblum const void *buf; 3838fe387abSDmitrij Tejblum size_t nbyte; 3848fe387abSDmitrij Tejblum off_t offset; 3858fe387abSDmitrij Tejblum { 3864160ccd9SAlan Cox struct uio auio; 3874160ccd9SAlan Cox struct iovec aiov; 3884160ccd9SAlan Cox long cnt, error = 0; 3894160ccd9SAlan Cox #ifdef KTRACE 3904160ccd9SAlan Cox struct iovec ktriov; 39142ebfbf2SBrian Feldman struct uio ktruio; 3923c89e357SBrian Feldman int didktr = 0; 3934160ccd9SAlan Cox #endif 3944160ccd9SAlan Cox 395b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 3968fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 3974160ccd9SAlan Cox auio.uio_iov = &aiov; 3984160ccd9SAlan Cox auio.uio_iovcnt = 1; 3998fe387abSDmitrij Tejblum auio.uio_offset = offset; 4008fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 4014160ccd9SAlan Cox return (EINVAL); 4028fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 4034160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 4044160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 405b40ce416SJulian Elischer auio.uio_td = td; 4064160ccd9SAlan Cox #ifdef KTRACE 4074160ccd9SAlan Cox /* 40842ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4094160ccd9SAlan Cox */ 41060a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 4114160ccd9SAlan Cox ktriov = aiov; 41242ebfbf2SBrian Feldman ktruio = auio; 4133c89e357SBrian Feldman didktr = 1; 41442ebfbf2SBrian Feldman } 4154160ccd9SAlan Cox #endif 4168fe387abSDmitrij Tejblum cnt = nbyte; 417c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 418279d7226SMatthew Dillon bwillwrite(); 4199ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, flags, td))) { 4204160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4214160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4224160ccd9SAlan Cox error = 0; 423c33c8251SAlfred Perlstein /* Socket layer is responsible for issuing SIGPIPE. */ 424c33c8251SAlfred Perlstein if (error == EPIPE && fp->f_type != DTYPE_SOCKET) { 425b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 426b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 427b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 42819eb87d2SJohn Baldwin } 4294160ccd9SAlan Cox } 4304160ccd9SAlan Cox cnt -= auio.uio_resid; 4314160ccd9SAlan Cox #ifdef KTRACE 4323c89e357SBrian Feldman if (didktr && error == 0) { 43342ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 43442ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 43560a9bb19SJohn Baldwin ktrgenio(fd, UIO_WRITE, &ktruio, error); 43642ebfbf2SBrian Feldman } 4374160ccd9SAlan Cox #endif 438b40ce416SJulian Elischer td->td_retval[0] = cnt; 4394160ccd9SAlan Cox return (error); 4404160ccd9SAlan Cox } 4414160ccd9SAlan Cox 4424160ccd9SAlan Cox /* 443df8bae1dSRodney W. Grimes * Gather write system call 444df8bae1dSRodney W. Grimes */ 445d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 446df8bae1dSRodney W. Grimes struct writev_args { 447df8bae1dSRodney W. Grimes int fd; 448df8bae1dSRodney W. Grimes struct iovec *iovp; 449df8bae1dSRodney W. Grimes u_int iovcnt; 450df8bae1dSRodney W. Grimes }; 451d2d3e875SBruce Evans #endif 452ad2edad9SMatthew Dillon /* 453ad2edad9SMatthew Dillon * MPSAFE 454ad2edad9SMatthew Dillon */ 45526f9a767SRodney W. Grimes int 456b40ce416SJulian Elischer writev(td, uap) 457b40ce416SJulian Elischer struct thread *td; 458df8bae1dSRodney W. Grimes register struct writev_args *uap; 459df8bae1dSRodney W. Grimes { 460b064d43dSMatthew Dillon struct file *fp; 461df8bae1dSRodney W. Grimes struct uio auio; 462df8bae1dSRodney W. Grimes register struct iovec *iov; 463df8bae1dSRodney W. Grimes struct iovec *needfree; 464df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 465df8bae1dSRodney W. Grimes long i, cnt, error = 0; 466df8bae1dSRodney W. Grimes u_int iovlen; 467df8bae1dSRodney W. Grimes #ifdef KTRACE 468df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 46942ebfbf2SBrian Feldman struct uio ktruio; 470df8bae1dSRodney W. Grimes #endif 471df8bae1dSRodney W. Grimes 472882d8469SAlan Cox if ((error = fget_write(td, uap->fd, &fp)) != 0) 473882d8469SAlan Cox return (EBADF); 474882d8469SAlan Cox needfree = NULL; 475df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 476df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 477df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 4781aa3e7ddSBrian Feldman if (uap->iovcnt > UIO_MAXIOV) { 4791aa3e7ddSBrian Feldman error = EINVAL; 4801aa3e7ddSBrian Feldman goto done; 4811aa3e7ddSBrian Feldman } 482a163d034SWarner Losh MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 483df8bae1dSRodney W. Grimes needfree = iov; 484882d8469SAlan Cox } else 485df8bae1dSRodney W. Grimes iov = aiov; 486df8bae1dSRodney W. Grimes auio.uio_iov = iov; 487df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 488df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 489df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 490b40ce416SJulian Elischer auio.uio_td = td; 4912c1011f7SJohn Dyson auio.uio_offset = -1; 4920a3e28cfSAlfred Perlstein if ((error = copyin(uap->iovp, iov, iovlen))) 493df8bae1dSRodney W. Grimes goto done; 494df8bae1dSRodney W. Grimes auio.uio_resid = 0; 495df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 496069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 497df8bae1dSRodney W. Grimes error = EINVAL; 498df8bae1dSRodney W. Grimes goto done; 499df8bae1dSRodney W. Grimes } 500069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 501df8bae1dSRodney W. Grimes iov++; 502df8bae1dSRodney W. Grimes } 503df8bae1dSRodney W. Grimes #ifdef KTRACE 504df8bae1dSRodney W. Grimes /* 50542ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 506df8bae1dSRodney W. Grimes */ 50760a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 508a163d034SWarner Losh MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 5090a3e28cfSAlfred Perlstein bcopy(auio.uio_iov, ktriov, iovlen); 51042ebfbf2SBrian Feldman ktruio = auio; 511df8bae1dSRodney W. Grimes } 512df8bae1dSRodney W. Grimes #endif 513df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 514a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5159440653dSMatthew Dillon bwillwrite(); 5169ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, 0, td))) { 517df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 518df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 519df8bae1dSRodney W. Grimes error = 0; 52019eb87d2SJohn Baldwin if (error == EPIPE) { 521b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 522b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 523b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 52419eb87d2SJohn Baldwin } 525df8bae1dSRodney W. Grimes } 526df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 527df8bae1dSRodney W. Grimes #ifdef KTRACE 528df8bae1dSRodney W. Grimes if (ktriov != NULL) { 52942ebfbf2SBrian Feldman if (error == 0) { 53042ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 53142ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 53260a9bb19SJohn Baldwin ktrgenio(uap->fd, UIO_WRITE, &ktruio, error); 53342ebfbf2SBrian Feldman } 534df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 535df8bae1dSRodney W. Grimes } 536df8bae1dSRodney W. Grimes #endif 537b40ce416SJulian Elischer td->td_retval[0] = cnt; 538df8bae1dSRodney W. Grimes done: 539b40ce416SJulian Elischer fdrop(fp, td); 540df8bae1dSRodney W. Grimes if (needfree) 541df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 542df8bae1dSRodney W. Grimes return (error); 543df8bae1dSRodney W. Grimes } 544df8bae1dSRodney W. Grimes 545df8bae1dSRodney W. Grimes /* 546df8bae1dSRodney W. Grimes * Ioctl system call 547df8bae1dSRodney W. Grimes */ 548d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 549df8bae1dSRodney W. Grimes struct ioctl_args { 550df8bae1dSRodney W. Grimes int fd; 551069e9bc1SDoug Rabson u_long com; 552df8bae1dSRodney W. Grimes caddr_t data; 553df8bae1dSRodney W. Grimes }; 554d2d3e875SBruce Evans #endif 555ad2edad9SMatthew Dillon /* 556ad2edad9SMatthew Dillon * MPSAFE 557ad2edad9SMatthew Dillon */ 558df8bae1dSRodney W. Grimes /* ARGSUSED */ 55926f9a767SRodney W. Grimes int 560b40ce416SJulian Elischer ioctl(td, uap) 561b40ce416SJulian Elischer struct thread *td; 562df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 563df8bae1dSRodney W. Grimes { 564a4db4953SAlfred Perlstein struct file *fp; 565df8bae1dSRodney W. Grimes register struct filedesc *fdp; 566831b9ef2SDoug Rabson register u_long com; 567ad2edad9SMatthew Dillon int error = 0; 568df8bae1dSRodney W. Grimes register u_int size; 569df8bae1dSRodney W. Grimes caddr_t data, memp; 570df8bae1dSRodney W. Grimes int tmp; 571df8bae1dSRodney W. Grimes #define STK_PARAMS 128 572d2ba455cSMatthew Dillon union { 573df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 574d2ba455cSMatthew Dillon long align; 575d2ba455cSMatthew Dillon } ubuf; 576df8bae1dSRodney W. Grimes 577a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 578a4db4953SAlfred Perlstein return (error); 579aa11a498SAlfred Perlstein mtx_lock(&Giant); 580ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 581426da3bcSAlfred Perlstein fdrop(fp, td); 582aa11a498SAlfred Perlstein mtx_unlock(&Giant); 583426da3bcSAlfred Perlstein return (EBADF); 584ad2edad9SMatthew Dillon } 585426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 586df8bae1dSRodney W. Grimes switch (com = uap->com) { 587df8bae1dSRodney W. Grimes case FIONCLEX: 588426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 589df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 590426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 591426da3bcSAlfred Perlstein fdrop(fp, td); 592aa11a498SAlfred Perlstein mtx_unlock(&Giant); 593426da3bcSAlfred Perlstein return (0); 594df8bae1dSRodney W. Grimes case FIOCLEX: 595426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 596df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 597426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 598426da3bcSAlfred Perlstein fdrop(fp, td); 599aa11a498SAlfred Perlstein mtx_unlock(&Giant); 600426da3bcSAlfred Perlstein return (0); 601df8bae1dSRodney W. Grimes } 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes /* 604df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 605df8bae1dSRodney W. Grimes * copied to/from the user's address space. 606df8bae1dSRodney W. Grimes */ 607df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 608ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 609426da3bcSAlfred Perlstein fdrop(fp, td); 610aa11a498SAlfred Perlstein mtx_unlock(&Giant); 611426da3bcSAlfred Perlstein return (ENOTTY); 612ad2edad9SMatthew Dillon } 613279d7226SMatthew Dillon 614df8bae1dSRodney W. Grimes memp = NULL; 615d2ba455cSMatthew Dillon if (size > sizeof (ubuf.stkbuf)) { 616a163d034SWarner Losh memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 617df8bae1dSRodney W. Grimes data = memp; 618279d7226SMatthew Dillon } else { 619d2ba455cSMatthew Dillon data = ubuf.stkbuf; 620279d7226SMatthew Dillon } 621df8bae1dSRodney W. Grimes if (com&IOC_IN) { 622df8bae1dSRodney W. Grimes if (size) { 623df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 624df8bae1dSRodney W. Grimes if (error) { 625df8bae1dSRodney W. Grimes if (memp) 626df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 627b40ce416SJulian Elischer fdrop(fp, td); 628426da3bcSAlfred Perlstein goto done; 629df8bae1dSRodney W. Grimes } 630279d7226SMatthew Dillon } else { 631df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 632279d7226SMatthew Dillon } 633279d7226SMatthew Dillon } else if ((com&IOC_OUT) && size) { 634df8bae1dSRodney W. Grimes /* 635df8bae1dSRodney W. Grimes * Zero the buffer so the user always 636df8bae1dSRodney W. Grimes * gets back something deterministic. 637df8bae1dSRodney W. Grimes */ 638df8bae1dSRodney W. Grimes bzero(data, size); 639279d7226SMatthew Dillon } else if (com&IOC_VOID) { 640df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 641279d7226SMatthew Dillon } 642df8bae1dSRodney W. Grimes 643df8bae1dSRodney W. Grimes switch (com) { 644df8bae1dSRodney W. Grimes 645df8bae1dSRodney W. Grimes case FIONBIO: 646426da3bcSAlfred Perlstein FILE_LOCK(fp); 647bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 648df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 649df8bae1dSRodney W. Grimes else 650df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 651426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 652d49fa1caSRobert Watson error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); 653df8bae1dSRodney W. Grimes break; 654df8bae1dSRodney W. Grimes 655df8bae1dSRodney W. Grimes case FIOASYNC: 656426da3bcSAlfred Perlstein FILE_LOCK(fp); 657bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 658df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 659df8bae1dSRodney W. Grimes else 660df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 661426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 662d49fa1caSRobert Watson error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); 663df8bae1dSRodney W. Grimes break; 664df8bae1dSRodney W. Grimes 665df8bae1dSRodney W. Grimes default: 666d49fa1caSRobert Watson error = fo_ioctl(fp, com, data, td->td_ucred, td); 667df8bae1dSRodney W. Grimes /* 668df8bae1dSRodney W. Grimes * Copy any data to user, size was 669df8bae1dSRodney W. Grimes * already set and checked above. 670df8bae1dSRodney W. Grimes */ 671df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 672df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 673df8bae1dSRodney W. Grimes break; 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes if (memp) 676df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 677b40ce416SJulian Elischer fdrop(fp, td); 678426da3bcSAlfred Perlstein done: 679ad2edad9SMatthew Dillon mtx_unlock(&Giant); 680df8bae1dSRodney W. Grimes return (error); 681df8bae1dSRodney W. Grimes } 682df8bae1dSRodney W. Grimes 68385f190e4SAlfred Perlstein /* 68485f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 68585f190e4SAlfred Perlstein */ 68685f190e4SAlfred Perlstein struct mtx sellock; 687265fc98fSSeigo Tanimura struct cv selwait; 6889ae6d334SKelly Yancey u_int nselcoll; /* Select collisions since boot */ 6899ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 690df8bae1dSRodney W. Grimes 691df8bae1dSRodney W. Grimes /* 692df8bae1dSRodney W. Grimes * Select system call. 693df8bae1dSRodney W. Grimes */ 694d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 695df8bae1dSRodney W. Grimes struct select_args { 696b08f7993SSujal Patel int nd; 697df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 698df8bae1dSRodney W. Grimes struct timeval *tv; 699df8bae1dSRodney W. Grimes }; 700d2d3e875SBruce Evans #endif 701ad2edad9SMatthew Dillon /* 702ad2edad9SMatthew Dillon * MPSAFE 703ad2edad9SMatthew Dillon */ 70426f9a767SRodney W. Grimes int 705b40ce416SJulian Elischer select(td, uap) 706b40ce416SJulian Elischer register struct thread *td; 707df8bae1dSRodney W. Grimes register struct select_args *uap; 708df8bae1dSRodney W. Grimes { 7098f19eb88SIan Dowse struct timeval tv, *tvp; 7108f19eb88SIan Dowse int error; 7118f19eb88SIan Dowse 7128f19eb88SIan Dowse if (uap->tv != NULL) { 7138f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 7148f19eb88SIan Dowse if (error) 7158f19eb88SIan Dowse return (error); 7168f19eb88SIan Dowse tvp = &tv; 7178f19eb88SIan Dowse } else 7188f19eb88SIan Dowse tvp = NULL; 7198f19eb88SIan Dowse 7208f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 7218f19eb88SIan Dowse } 7228f19eb88SIan Dowse 7238f19eb88SIan Dowse int 7248f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 7258f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 7268f19eb88SIan Dowse { 727426da3bcSAlfred Perlstein struct filedesc *fdp; 728d5e4d7e1SBruce Evans /* 729d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 730d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 731d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 732d5e4d7e1SBruce Evans * of 256. 733d5e4d7e1SBruce Evans */ 734d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 735eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 73600af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7379ae6d334SKelly Yancey int error, timo; 7389ae6d334SKelly Yancey u_int ncoll, nbufbytes, ncpbytes, nfdbits; 739df8bae1dSRodney W. Grimes 7408f19eb88SIan Dowse if (nd < 0) 741acbfbfeaSSujal Patel return (EINVAL); 742426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 743ad2edad9SMatthew Dillon mtx_lock(&Giant); 744426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 745ad2edad9SMatthew Dillon 7468f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 7478f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 748426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 749b08f7993SSujal Patel 750d5e4d7e1SBruce Evans /* 751d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 752d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 753d5e4d7e1SBruce Evans */ 7548f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 755d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 756d5e4d7e1SBruce Evans nbufbytes = 0; 7578f19eb88SIan Dowse if (fd_in != NULL) 758d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7598f19eb88SIan Dowse if (fd_ou != NULL) 760d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7618f19eb88SIan Dowse if (fd_ex != NULL) 762d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 763d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 764d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 765d5e4d7e1SBruce Evans else 766a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 767b08f7993SSujal Patel 768b08f7993SSujal Patel /* 769d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 770d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 771d5e4d7e1SBruce Evans * together. 772b08f7993SSujal Patel */ 773d5e4d7e1SBruce Evans sbp = selbits; 774df8bae1dSRodney W. Grimes #define getbits(name, x) \ 775d5e4d7e1SBruce Evans do { \ 7768f19eb88SIan Dowse if (name == NULL) \ 777d5e4d7e1SBruce Evans ibits[x] = NULL; \ 778d5e4d7e1SBruce Evans else { \ 779d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 780d5e4d7e1SBruce Evans obits[x] = sbp; \ 781d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 7828f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 783265fc98fSSeigo Tanimura if (error != 0) \ 78485f190e4SAlfred Perlstein goto done_nosellock; \ 785e04ac2feSJohn Baldwin } \ 786d5e4d7e1SBruce Evans } while (0) 7878f19eb88SIan Dowse getbits(fd_in, 0); 7888f19eb88SIan Dowse getbits(fd_ou, 1); 7898f19eb88SIan Dowse getbits(fd_ex, 2); 790df8bae1dSRodney W. Grimes #undef getbits 791d5e4d7e1SBruce Evans if (nbufbytes != 0) 792d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 793df8bae1dSRodney W. Grimes 7948f19eb88SIan Dowse if (tvp != NULL) { 7958f19eb88SIan Dowse atv = *tvp; 796df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 797df8bae1dSRodney W. Grimes error = EINVAL; 79885f190e4SAlfred Perlstein goto done_nosellock; 799df8bae1dSRodney W. Grimes } 800c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 80100af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8029c386f6bSJohn Baldwin } else { 80300af9731SPoul-Henning Kamp atv.tv_sec = 0; 8049c386f6bSJohn Baldwin atv.tv_usec = 0; 8059c386f6bSJohn Baldwin } 80600af9731SPoul-Henning Kamp timo = 0; 8072149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 80885f190e4SAlfred Perlstein mtx_lock(&sellock); 809df8bae1dSRodney W. Grimes retry: 810df8bae1dSRodney W. Grimes ncoll = nselcoll; 811fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 812b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 813fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 81485f190e4SAlfred Perlstein mtx_unlock(&sellock); 81585f190e4SAlfred Perlstein 8168f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 81785f190e4SAlfred Perlstein mtx_lock(&sellock); 818b40ce416SJulian Elischer if (error || td->td_retval[0]) 819df8bae1dSRodney W. Grimes goto done; 8204da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 821c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 82285f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 823df8bae1dSRodney W. Grimes goto done; 82400af9731SPoul-Henning Kamp ttv = atv; 82500af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 82600af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 82700af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 828df8bae1dSRodney W. Grimes } 82985f190e4SAlfred Perlstein 83085f190e4SAlfred Perlstein /* 83185f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 83285f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 83385f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 83485f190e4SAlfred Perlstein * necessary. 83585f190e4SAlfred Perlstein */ 836fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 83785f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 83885f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 83985f190e4SAlfred Perlstein goto retry; 84085f190e4SAlfred Perlstein } 841fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 842bfbbc4aaSJason Evans 843265fc98fSSeigo Tanimura if (timo > 0) 84485f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 845265fc98fSSeigo Tanimura else 84685f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 847bfbbc4aaSJason Evans 848df8bae1dSRodney W. Grimes if (error == 0) 849df8bae1dSRodney W. Grimes goto retry; 850265fc98fSSeigo Tanimura 851df8bae1dSRodney W. Grimes done: 85285f190e4SAlfred Perlstein clear_selinfo_list(td); 853fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 854b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 855fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 85685f190e4SAlfred Perlstein mtx_unlock(&sellock); 85785f190e4SAlfred Perlstein 85885f190e4SAlfred Perlstein done_nosellock: 859df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 860df8bae1dSRodney W. Grimes if (error == ERESTART) 861df8bae1dSRodney W. Grimes error = EINTR; 862df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 863df8bae1dSRodney W. Grimes error = 0; 864df8bae1dSRodney W. Grimes #define putbits(name, x) \ 8658f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 866df8bae1dSRodney W. Grimes error = error2; 867df8bae1dSRodney W. Grimes if (error == 0) { 868df8bae1dSRodney W. Grimes int error2; 869df8bae1dSRodney W. Grimes 8708f19eb88SIan Dowse putbits(fd_in, 0); 8718f19eb88SIan Dowse putbits(fd_ou, 1); 8728f19eb88SIan Dowse putbits(fd_ex, 2); 873df8bae1dSRodney W. Grimes #undef putbits 874df8bae1dSRodney W. Grimes } 875d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 876d5e4d7e1SBruce Evans free(selbits, M_SELECT); 877ad2edad9SMatthew Dillon 878ad2edad9SMatthew Dillon mtx_unlock(&Giant); 879df8bae1dSRodney W. Grimes return (error); 880df8bae1dSRodney W. Grimes } 881df8bae1dSRodney W. Grimes 882265fc98fSSeigo Tanimura static int 883b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 884b40ce416SJulian Elischer struct thread *td; 885b08f7993SSujal Patel fd_mask **ibits, **obits; 886cb226aaaSPoul-Henning Kamp int nfd; 887df8bae1dSRodney W. Grimes { 888f082218cSPeter Wemm int msk, i, fd; 889f082218cSPeter Wemm fd_mask bits; 890df8bae1dSRodney W. Grimes struct file *fp; 891df8bae1dSRodney W. Grimes int n = 0; 8922087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 89342d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 894eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 895df8bae1dSRodney W. Grimes 896eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 897df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 898d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 899d5e4d7e1SBruce Evans continue; 900df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 901b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 902f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 903f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 904f082218cSPeter Wemm if (!(bits & 1)) 905f082218cSPeter Wemm continue; 906eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 907eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 908df8bae1dSRodney W. Grimes return (EBADF); 909eb209311SAlfred Perlstein } 910ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 911ea6027a8SRobert Watson td)) { 912b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 913f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 914df8bae1dSRodney W. Grimes n++; 915df8bae1dSRodney W. Grimes } 916df8bae1dSRodney W. Grimes } 917df8bae1dSRodney W. Grimes } 918df8bae1dSRodney W. Grimes } 919eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 920b40ce416SJulian Elischer td->td_retval[0] = n; 921df8bae1dSRodney W. Grimes return (0); 922df8bae1dSRodney W. Grimes } 923df8bae1dSRodney W. Grimes 92442d11757SPeter Wemm /* 92542d11757SPeter Wemm * Poll system call. 92642d11757SPeter Wemm */ 92742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 92842d11757SPeter Wemm struct poll_args { 92942d11757SPeter Wemm struct pollfd *fds; 93042d11757SPeter Wemm u_int nfds; 93142d11757SPeter Wemm int timeout; 93242d11757SPeter Wemm }; 93342d11757SPeter Wemm #endif 934ad2edad9SMatthew Dillon /* 935ad2edad9SMatthew Dillon * MPSAFE 936ad2edad9SMatthew Dillon */ 93742d11757SPeter Wemm int 938b40ce416SJulian Elischer poll(td, uap) 939b40ce416SJulian Elischer struct thread *td; 940ea0237edSJonathan Lemon struct poll_args *uap; 94142d11757SPeter Wemm { 94242d11757SPeter Wemm caddr_t bits; 94342d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 94400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 9459ae6d334SKelly Yancey int error = 0, timo; 9469ae6d334SKelly Yancey u_int ncoll, nfds; 94742d11757SPeter Wemm size_t ni; 94842d11757SPeter Wemm 949d1e405c5SAlfred Perlstein nfds = uap->nfds; 950ad2edad9SMatthew Dillon 951ad2edad9SMatthew Dillon mtx_lock(&Giant); 95289b71647SPeter Wemm /* 9532bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9542bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9552bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9562bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9572bd5ac33SPeter Wemm * safe, but not overly restrictive. 95889b71647SPeter Wemm */ 959b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 960b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 961ad2edad9SMatthew Dillon error = EINVAL; 962ad2edad9SMatthew Dillon goto done2; 963ad2edad9SMatthew Dillon } 96489b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 96542d11757SPeter Wemm if (ni > sizeof(smallbits)) 966a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 96742d11757SPeter Wemm else 96842d11757SPeter Wemm bits = smallbits; 969d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 97042d11757SPeter Wemm if (error) 97185f190e4SAlfred Perlstein goto done_nosellock; 972d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 973d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 974d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 97542d11757SPeter Wemm if (itimerfix(&atv)) { 97642d11757SPeter Wemm error = EINVAL; 97785f190e4SAlfred Perlstein goto done_nosellock; 97842d11757SPeter Wemm } 979c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 98000af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9819c386f6bSJohn Baldwin } else { 98200af9731SPoul-Henning Kamp atv.tv_sec = 0; 9839c386f6bSJohn Baldwin atv.tv_usec = 0; 9849c386f6bSJohn Baldwin } 98500af9731SPoul-Henning Kamp timo = 0; 9862149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 98785f190e4SAlfred Perlstein mtx_lock(&sellock); 98842d11757SPeter Wemm retry: 98942d11757SPeter Wemm ncoll = nselcoll; 990fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 991b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 992fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 99385f190e4SAlfred Perlstein mtx_unlock(&sellock); 99485f190e4SAlfred Perlstein 995b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 99685f190e4SAlfred Perlstein mtx_lock(&sellock); 997b40ce416SJulian Elischer if (error || td->td_retval[0]) 99842d11757SPeter Wemm goto done; 9994da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1000c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 100185f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 100242d11757SPeter Wemm goto done; 100300af9731SPoul-Henning Kamp ttv = atv; 100400af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 100500af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 100600af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 100742d11757SPeter Wemm } 100885f190e4SAlfred Perlstein /* 100985f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 101085f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 101185f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 101285f190e4SAlfred Perlstein */ 1013fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 101485f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 1015fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 101685f190e4SAlfred Perlstein goto retry; 101785f190e4SAlfred Perlstein } 101885f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 101985f190e4SAlfred Perlstein 1020265fc98fSSeigo Tanimura if (timo > 0) 102185f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 1022265fc98fSSeigo Tanimura else 102385f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 102485f190e4SAlfred Perlstein 102542d11757SPeter Wemm if (error == 0) 102642d11757SPeter Wemm goto retry; 1027265fc98fSSeigo Tanimura 102842d11757SPeter Wemm done: 102985f190e4SAlfred Perlstein clear_selinfo_list(td); 1030fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1031b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1032fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 103385f190e4SAlfred Perlstein mtx_unlock(&sellock); 103485f190e4SAlfred Perlstein 103585f190e4SAlfred Perlstein done_nosellock: 103642d11757SPeter Wemm /* poll is not restarted after signals... */ 103742d11757SPeter Wemm if (error == ERESTART) 103842d11757SPeter Wemm error = EINTR; 103942d11757SPeter Wemm if (error == EWOULDBLOCK) 104042d11757SPeter Wemm error = 0; 104142d11757SPeter Wemm if (error == 0) { 1042d1e405c5SAlfred Perlstein error = copyout(bits, uap->fds, ni); 104342d11757SPeter Wemm if (error) 104442d11757SPeter Wemm goto out; 104542d11757SPeter Wemm } 104642d11757SPeter Wemm out: 104742d11757SPeter Wemm if (ni > sizeof(smallbits)) 104842d11757SPeter Wemm free(bits, M_TEMP); 1049ad2edad9SMatthew Dillon done2: 1050ad2edad9SMatthew Dillon mtx_unlock(&Giant); 105142d11757SPeter Wemm return (error); 105242d11757SPeter Wemm } 105342d11757SPeter Wemm 105442d11757SPeter Wemm static int 1055b40ce416SJulian Elischer pollscan(td, fds, nfd) 1056b40ce416SJulian Elischer struct thread *td; 105742d11757SPeter Wemm struct pollfd *fds; 1058ea0237edSJonathan Lemon u_int nfd; 105942d11757SPeter Wemm { 1060b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 106142d11757SPeter Wemm int i; 106242d11757SPeter Wemm struct file *fp; 106342d11757SPeter Wemm int n = 0; 106442d11757SPeter Wemm 1065426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1066eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1067337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 106842d11757SPeter Wemm fds->revents = POLLNVAL; 106942d11757SPeter Wemm n++; 1070337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1071337c9691SJordan K. Hubbard fds->revents = 0; 107242d11757SPeter Wemm } else { 107342d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1074279d7226SMatthew Dillon if (fp == NULL) { 107542d11757SPeter Wemm fds->revents = POLLNVAL; 107642d11757SPeter Wemm n++; 107742d11757SPeter Wemm } else { 10782087c896SBruce Evans /* 10792087c896SBruce Evans * Note: backend also returns POLLHUP and 10802087c896SBruce Evans * POLLERR if appropriate. 10812087c896SBruce Evans */ 108213ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1083ea6027a8SRobert Watson td->td_ucred, td); 108442d11757SPeter Wemm if (fds->revents != 0) 108542d11757SPeter Wemm n++; 108642d11757SPeter Wemm } 108742d11757SPeter Wemm } 108842d11757SPeter Wemm } 1089eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1090b40ce416SJulian Elischer td->td_retval[0] = n; 109142d11757SPeter Wemm return (0); 109242d11757SPeter Wemm } 109342d11757SPeter Wemm 109442d11757SPeter Wemm /* 109542d11757SPeter Wemm * OpenBSD poll system call. 109642d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 109742d11757SPeter Wemm */ 109842d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 109942d11757SPeter Wemm struct openbsd_poll_args { 110042d11757SPeter Wemm struct pollfd *fds; 110142d11757SPeter Wemm u_int nfds; 110242d11757SPeter Wemm int timeout; 110342d11757SPeter Wemm }; 110442d11757SPeter Wemm #endif 1105ad2edad9SMatthew Dillon /* 1106ad2edad9SMatthew Dillon * MPSAFE 1107ad2edad9SMatthew Dillon */ 110842d11757SPeter Wemm int 1109b40ce416SJulian Elischer openbsd_poll(td, uap) 1110b40ce416SJulian Elischer register struct thread *td; 111142d11757SPeter Wemm register struct openbsd_poll_args *uap; 111242d11757SPeter Wemm { 1113b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 111442d11757SPeter Wemm } 111542d11757SPeter Wemm 111685f190e4SAlfred Perlstein /* 111785f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 111885f190e4SAlfred Perlstein * we were polling. 111985f190e4SAlfred Perlstein * 112085f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 112185f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 112285f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 112385f190e4SAlfred Perlstein */ 112485f190e4SAlfred Perlstein void 112585f190e4SAlfred Perlstein clear_selinfo_list(td) 112685f190e4SAlfred Perlstein struct thread *td; 112785f190e4SAlfred Perlstein { 112885f190e4SAlfred Perlstein struct selinfo *si; 112985f190e4SAlfred Perlstein 113085f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 113185f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 113285f190e4SAlfred Perlstein si->si_thread = NULL; 113385f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 113485f190e4SAlfred Perlstein } 113585f190e4SAlfred Perlstein 1136df8bae1dSRodney W. Grimes /* 1137df8bae1dSRodney W. Grimes * Record a select request. 1138df8bae1dSRodney W. Grimes */ 1139df8bae1dSRodney W. Grimes void 1140df8bae1dSRodney W. Grimes selrecord(selector, sip) 1141b40ce416SJulian Elischer struct thread *selector; 1142df8bae1dSRodney W. Grimes struct selinfo *sip; 1143df8bae1dSRodney W. Grimes { 1144df8bae1dSRodney W. Grimes 114585f190e4SAlfred Perlstein mtx_lock(&sellock); 114685f190e4SAlfred Perlstein /* 1147b605b54cSAlfred Perlstein * If the selinfo's thread pointer is NULL then take ownership of it. 1148b605b54cSAlfred Perlstein * 1149b605b54cSAlfred Perlstein * If the thread pointer is not NULL and it points to another 1150b605b54cSAlfred Perlstein * thread, then we have a collision. 1151b605b54cSAlfred Perlstein * 1152b605b54cSAlfred Perlstein * If the thread pointer is not NULL and points back to us then leave 1153b605b54cSAlfred Perlstein * it alone as we've already added pointed it at us and added it to 1154b605b54cSAlfred Perlstein * our list. 115585f190e4SAlfred Perlstein */ 115685f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1157b40ce416SJulian Elischer sip->si_thread = selector; 115885f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 115985f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 116085f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 116185f190e4SAlfred Perlstein } 116285f190e4SAlfred Perlstein 116385f190e4SAlfred Perlstein mtx_unlock(&sellock); 1164df8bae1dSRodney W. Grimes } 1165df8bae1dSRodney W. Grimes 1166512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1167df8bae1dSRodney W. Grimes void 1168df8bae1dSRodney W. Grimes selwakeup(sip) 116985f190e4SAlfred Perlstein struct selinfo *sip; 1170df8bae1dSRodney W. Grimes { 1171512824f8SSeigo Tanimura doselwakeup(sip, -1); 1172512824f8SSeigo Tanimura } 1173512824f8SSeigo Tanimura 1174512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1175512824f8SSeigo Tanimura void 1176512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1177512824f8SSeigo Tanimura struct selinfo *sip; 1178512824f8SSeigo Tanimura int pri; 1179512824f8SSeigo Tanimura { 1180512824f8SSeigo Tanimura doselwakeup(sip, pri); 1181512824f8SSeigo Tanimura } 1182512824f8SSeigo Tanimura 1183512824f8SSeigo Tanimura /* 1184512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1185512824f8SSeigo Tanimura */ 1186512824f8SSeigo Tanimura static void 1187512824f8SSeigo Tanimura doselwakeup(sip, pri) 1188512824f8SSeigo Tanimura struct selinfo *sip; 1189512824f8SSeigo Tanimura int pri; 1190512824f8SSeigo Tanimura { 1191b40ce416SJulian Elischer struct thread *td; 1192df8bae1dSRodney W. Grimes 119385f190e4SAlfred Perlstein mtx_lock(&sellock); 119485f190e4SAlfred Perlstein td = sip->si_thread; 119585f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1196df8bae1dSRodney W. Grimes nselcoll++; 1197df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1198512824f8SSeigo Tanimura cv_broadcastpri(&selwait, pri); 1199df8bae1dSRodney W. Grimes } 120085f190e4SAlfred Perlstein if (td == NULL) { 120185f190e4SAlfred Perlstein mtx_unlock(&sellock); 1202b40ce416SJulian Elischer return; 1203b40ce416SJulian Elischer } 120485f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 120585f190e4SAlfred Perlstein sip->si_thread = NULL; 12069ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 12070a3e28cfSAlfred Perlstein if (td->td_wchan == &selwait) { 1208b40ce416SJulian Elischer cv_waitq_remove(td); 120971fad9fdSJulian Elischer TD_CLR_SLEEPING(td); 1210512824f8SSeigo Tanimura if (pri >= PRI_MIN && pri <= PRI_MAX && td->td_priority > pri) 1211512824f8SSeigo Tanimura td->td_priority = pri; 121271fad9fdSJulian Elischer setrunnable(td); 121333a9ed9dSJohn Baldwin } else 1214b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 121533a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 121685f190e4SAlfred Perlstein mtx_unlock(&sellock); 1217df8bae1dSRodney W. Grimes } 1218265fc98fSSeigo Tanimura 12194d77a549SAlfred Perlstein static void selectinit(void *); 1220265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1221265fc98fSSeigo Tanimura 1222265fc98fSSeigo Tanimura /* ARGSUSED*/ 1223265fc98fSSeigo Tanimura static void 1224265fc98fSSeigo Tanimura selectinit(dummy) 1225265fc98fSSeigo Tanimura void *dummy; 1226265fc98fSSeigo Tanimura { 1227265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 12286008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1229265fc98fSSeigo Tanimura } 1230