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 39c3aac50fSPeter Wemm * $FreeBSD$ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43db6a20e2SGarrett Wollman 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 46d2d3e875SBruce Evans #include <sys/sysproto.h> 47df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 4820982410SBruce Evans #include <sys/filio.h> 493ac4d1efSBruce Evans #include <sys/fcntl.h> 50df8bae1dSRodney W. Grimes #include <sys/file.h> 51df8bae1dSRodney W. Grimes #include <sys/proc.h> 52797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 53df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 54df8bae1dSRodney W. Grimes #include <sys/uio.h> 55df8bae1dSRodney W. Grimes #include <sys/kernel.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> 608cb96f20SPeter Wemm #include <sys/sysctl.h> 6142d11757SPeter Wemm #include <sys/sysent.h> 62279d7226SMatthew Dillon #include <sys/bio.h> 63279d7226SMatthew Dillon #include <sys/buf.h> 64265fc98fSSeigo Tanimura #include <sys/condvar.h> 65f67ad03aSPoul-Henning Kamp #ifdef __alpha__ 66f67ad03aSPoul-Henning Kamp #include <sys/disklabel.h> 67f67ad03aSPoul-Henning Kamp #endif 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 74069e9bc1SDoug Rabson #include <machine/limits.h> 75069e9bc1SDoug Rabson 76a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 77a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 78a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7955166637SPoul-Henning Kamp 80bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 81bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 82bbbb04ceSAlfred Perlstein static int dofileread(struct thread *, struct file *, int, void *, 83bbbb04ceSAlfred Perlstein size_t, off_t, int); 84bbbb04ceSAlfred Perlstein static int dofilewrite(struct thread *, struct file *, int, 85bbbb04ceSAlfred Perlstein const void *, size_t, off_t, 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); 141426da3bcSAlfred Perlstein if (fp->f_type != DTYPE_VNODE) { 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 */ 1548fe387abSDmitrij Tejblum 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 } 253df8bae1dSRodney W. Grimes 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)) { 279df8bae1dSRodney W. Grimes 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) { 364aa11a498SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) { 365b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 366b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 367b064d43dSMatthew Dillon } else { 368279d7226SMatthew Dillon error = ESPIPE; 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 472ad2edad9SMatthew Dillon mtx_lock(&Giant); 473b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) != 0) { 474ad2edad9SMatthew Dillon error = EBADF; 475ad2edad9SMatthew Dillon goto done2; 476ad2edad9SMatthew Dillon } 477df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 478df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 479df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 4801aa3e7ddSBrian Feldman if (uap->iovcnt > UIO_MAXIOV) { 4811aa3e7ddSBrian Feldman needfree = NULL; 4821aa3e7ddSBrian Feldman error = EINVAL; 4831aa3e7ddSBrian Feldman goto done; 4841aa3e7ddSBrian Feldman } 485df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 486df8bae1dSRodney W. Grimes needfree = iov; 487df8bae1dSRodney W. Grimes } else { 488df8bae1dSRodney W. Grimes iov = aiov; 489df8bae1dSRodney W. Grimes needfree = NULL; 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes auio.uio_iov = iov; 492df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 493df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 494df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 495b40ce416SJulian Elischer auio.uio_td = td; 4962c1011f7SJohn Dyson auio.uio_offset = -1; 4970a3e28cfSAlfred Perlstein if ((error = copyin(uap->iovp, iov, iovlen))) 498df8bae1dSRodney W. Grimes goto done; 499df8bae1dSRodney W. Grimes auio.uio_resid = 0; 500df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 501069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 502df8bae1dSRodney W. Grimes error = EINVAL; 503df8bae1dSRodney W. Grimes goto done; 504df8bae1dSRodney W. Grimes } 505069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 506df8bae1dSRodney W. Grimes iov++; 507df8bae1dSRodney W. Grimes } 508df8bae1dSRodney W. Grimes #ifdef KTRACE 509df8bae1dSRodney W. Grimes /* 51042ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 511df8bae1dSRodney W. Grimes */ 51260a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 513df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 5140a3e28cfSAlfred Perlstein bcopy(auio.uio_iov, ktriov, iovlen); 51542ebfbf2SBrian Feldman ktruio = auio; 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes #endif 518df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 519a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5209440653dSMatthew Dillon bwillwrite(); 5219ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, 0, td))) { 522df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 523df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 524df8bae1dSRodney W. Grimes error = 0; 52519eb87d2SJohn Baldwin if (error == EPIPE) { 526b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 527b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 528b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 52919eb87d2SJohn Baldwin } 530df8bae1dSRodney W. Grimes } 531df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 532df8bae1dSRodney W. Grimes #ifdef KTRACE 533df8bae1dSRodney W. Grimes if (ktriov != NULL) { 53442ebfbf2SBrian Feldman if (error == 0) { 53542ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 53642ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 53760a9bb19SJohn Baldwin ktrgenio(uap->fd, UIO_WRITE, &ktruio, error); 53842ebfbf2SBrian Feldman } 539df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 540df8bae1dSRodney W. Grimes } 541df8bae1dSRodney W. Grimes #endif 542b40ce416SJulian Elischer td->td_retval[0] = cnt; 543df8bae1dSRodney W. Grimes done: 544b40ce416SJulian Elischer fdrop(fp, td); 545df8bae1dSRodney W. Grimes if (needfree) 546df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 547ad2edad9SMatthew Dillon done2: 548ad2edad9SMatthew Dillon mtx_unlock(&Giant); 549df8bae1dSRodney W. Grimes return (error); 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes 552df8bae1dSRodney W. Grimes /* 553df8bae1dSRodney W. Grimes * Ioctl system call 554df8bae1dSRodney W. Grimes */ 555d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 556df8bae1dSRodney W. Grimes struct ioctl_args { 557df8bae1dSRodney W. Grimes int fd; 558069e9bc1SDoug Rabson u_long com; 559df8bae1dSRodney W. Grimes caddr_t data; 560df8bae1dSRodney W. Grimes }; 561d2d3e875SBruce Evans #endif 562ad2edad9SMatthew Dillon /* 563ad2edad9SMatthew Dillon * MPSAFE 564ad2edad9SMatthew Dillon */ 565df8bae1dSRodney W. Grimes /* ARGSUSED */ 56626f9a767SRodney W. Grimes int 567b40ce416SJulian Elischer ioctl(td, uap) 568b40ce416SJulian Elischer struct thread *td; 569df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 570df8bae1dSRodney W. Grimes { 571a4db4953SAlfred Perlstein struct file *fp; 572df8bae1dSRodney W. Grimes register struct filedesc *fdp; 573831b9ef2SDoug Rabson register u_long com; 574ad2edad9SMatthew Dillon int error = 0; 575df8bae1dSRodney W. Grimes register u_int size; 576df8bae1dSRodney W. Grimes caddr_t data, memp; 577df8bae1dSRodney W. Grimes int tmp; 578df8bae1dSRodney W. Grimes #define STK_PARAMS 128 579d2ba455cSMatthew Dillon union { 580df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 581d2ba455cSMatthew Dillon long align; 582d2ba455cSMatthew Dillon } ubuf; 583df8bae1dSRodney W. Grimes 584a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 585a4db4953SAlfred Perlstein return (error); 586aa11a498SAlfred Perlstein mtx_lock(&Giant); 587ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 588426da3bcSAlfred Perlstein fdrop(fp, td); 589aa11a498SAlfred Perlstein mtx_unlock(&Giant); 590426da3bcSAlfred Perlstein return (EBADF); 591ad2edad9SMatthew Dillon } 592426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 593df8bae1dSRodney W. Grimes switch (com = uap->com) { 594df8bae1dSRodney W. Grimes case FIONCLEX: 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 case FIOCLEX: 602426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 603df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 604426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 605426da3bcSAlfred Perlstein fdrop(fp, td); 606aa11a498SAlfred Perlstein mtx_unlock(&Giant); 607426da3bcSAlfred Perlstein return (0); 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes /* 611df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 612df8bae1dSRodney W. Grimes * copied to/from the user's address space. 613df8bae1dSRodney W. Grimes */ 614df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 615ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 616426da3bcSAlfred Perlstein fdrop(fp, td); 617aa11a498SAlfred Perlstein mtx_unlock(&Giant); 618426da3bcSAlfred Perlstein return (ENOTTY); 619ad2edad9SMatthew Dillon } 620279d7226SMatthew Dillon 621df8bae1dSRodney W. Grimes memp = NULL; 622d2ba455cSMatthew Dillon if (size > sizeof (ubuf.stkbuf)) { 6230a3e28cfSAlfred Perlstein memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 624df8bae1dSRodney W. Grimes data = memp; 625279d7226SMatthew Dillon } else { 626d2ba455cSMatthew Dillon data = ubuf.stkbuf; 627279d7226SMatthew Dillon } 628df8bae1dSRodney W. Grimes if (com&IOC_IN) { 629df8bae1dSRodney W. Grimes if (size) { 630df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 631df8bae1dSRodney W. Grimes if (error) { 632df8bae1dSRodney W. Grimes if (memp) 633df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 634b40ce416SJulian Elischer fdrop(fp, td); 635426da3bcSAlfred Perlstein goto done; 636df8bae1dSRodney W. Grimes } 637279d7226SMatthew Dillon } else { 638df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 639279d7226SMatthew Dillon } 640279d7226SMatthew Dillon } else if ((com&IOC_OUT) && size) { 641df8bae1dSRodney W. Grimes /* 642df8bae1dSRodney W. Grimes * Zero the buffer so the user always 643df8bae1dSRodney W. Grimes * gets back something deterministic. 644df8bae1dSRodney W. Grimes */ 645df8bae1dSRodney W. Grimes bzero(data, size); 646279d7226SMatthew Dillon } else if (com&IOC_VOID) { 647df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 648279d7226SMatthew Dillon } 649df8bae1dSRodney W. Grimes 650df8bae1dSRodney W. Grimes switch (com) { 651df8bae1dSRodney W. Grimes 652df8bae1dSRodney W. Grimes case FIONBIO: 653426da3bcSAlfred Perlstein FILE_LOCK(fp); 654bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 655df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 656df8bae1dSRodney W. Grimes else 657df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 658426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 659d452ec95SAlfred Perlstein error = fo_ioctl(fp, FIONBIO, &tmp, td); 660df8bae1dSRodney W. Grimes break; 661df8bae1dSRodney W. Grimes 662df8bae1dSRodney W. Grimes case FIOASYNC: 663426da3bcSAlfred Perlstein FILE_LOCK(fp); 664bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 665df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 666df8bae1dSRodney W. Grimes else 667df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 668426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 669d452ec95SAlfred Perlstein error = fo_ioctl(fp, FIOASYNC, &tmp, td); 670df8bae1dSRodney W. Grimes break; 671df8bae1dSRodney W. Grimes 672df8bae1dSRodney W. Grimes default: 673b40ce416SJulian Elischer error = fo_ioctl(fp, com, data, td); 674df8bae1dSRodney W. Grimes /* 675df8bae1dSRodney W. Grimes * Copy any data to user, size was 676df8bae1dSRodney W. Grimes * already set and checked above. 677df8bae1dSRodney W. Grimes */ 678df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 679df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 680df8bae1dSRodney W. Grimes break; 681df8bae1dSRodney W. Grimes } 682df8bae1dSRodney W. Grimes if (memp) 683df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 684b40ce416SJulian Elischer fdrop(fp, td); 685426da3bcSAlfred Perlstein done: 686ad2edad9SMatthew Dillon mtx_unlock(&Giant); 687df8bae1dSRodney W. Grimes return (error); 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes 69085f190e4SAlfred Perlstein /* 69185f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 69285f190e4SAlfred Perlstein */ 69385f190e4SAlfred Perlstein struct mtx sellock; 694265fc98fSSeigo Tanimura struct cv selwait; 6959ae6d334SKelly Yancey u_int nselcoll; /* Select collisions since boot */ 6969ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 697df8bae1dSRodney W. Grimes 698df8bae1dSRodney W. Grimes /* 699df8bae1dSRodney W. Grimes * Select system call. 700df8bae1dSRodney W. Grimes */ 701d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 702df8bae1dSRodney W. Grimes struct select_args { 703b08f7993SSujal Patel int nd; 704df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 705df8bae1dSRodney W. Grimes struct timeval *tv; 706df8bae1dSRodney W. Grimes }; 707d2d3e875SBruce Evans #endif 708ad2edad9SMatthew Dillon /* 709ad2edad9SMatthew Dillon * MPSAFE 710ad2edad9SMatthew Dillon */ 71126f9a767SRodney W. Grimes int 712b40ce416SJulian Elischer select(td, uap) 713b40ce416SJulian Elischer register struct thread *td; 714df8bae1dSRodney W. Grimes register struct select_args *uap; 715df8bae1dSRodney W. Grimes { 716426da3bcSAlfred Perlstein struct filedesc *fdp; 717d5e4d7e1SBruce Evans /* 718d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 719d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 720d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 721d5e4d7e1SBruce Evans * of 256. 722d5e4d7e1SBruce Evans */ 723d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 724eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 72500af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7269ae6d334SKelly Yancey int error, timo; 7279ae6d334SKelly Yancey u_int ncoll, nbufbytes, ncpbytes, nfdbits; 728df8bae1dSRodney W. Grimes 729b08f7993SSujal Patel if (uap->nd < 0) 730acbfbfeaSSujal Patel return (EINVAL); 731426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 732ad2edad9SMatthew Dillon mtx_lock(&Giant); 733426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 734ad2edad9SMatthew Dillon 735b40ce416SJulian Elischer if (uap->nd > td->td_proc->p_fd->fd_nfiles) 736b40ce416SJulian Elischer uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 737426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 738b08f7993SSujal Patel 739d5e4d7e1SBruce Evans /* 740d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 741d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 742d5e4d7e1SBruce Evans */ 743d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 744d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 745d5e4d7e1SBruce Evans nbufbytes = 0; 746d5e4d7e1SBruce Evans if (uap->in != NULL) 747d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 748d5e4d7e1SBruce Evans if (uap->ou != NULL) 749d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 750d5e4d7e1SBruce Evans if (uap->ex != NULL) 751d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 752d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 753d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 754d5e4d7e1SBruce Evans else 755d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 756b08f7993SSujal Patel 757b08f7993SSujal Patel /* 758d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 759d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 760d5e4d7e1SBruce Evans * together. 761b08f7993SSujal Patel */ 762d5e4d7e1SBruce Evans sbp = selbits; 763df8bae1dSRodney W. Grimes #define getbits(name, x) \ 764d5e4d7e1SBruce Evans do { \ 765d5e4d7e1SBruce Evans if (uap->name == NULL) \ 766d5e4d7e1SBruce Evans ibits[x] = NULL; \ 767d5e4d7e1SBruce Evans else { \ 768d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 769d5e4d7e1SBruce Evans obits[x] = sbp; \ 770d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 771d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 772265fc98fSSeigo Tanimura if (error != 0) \ 77385f190e4SAlfred Perlstein goto done_nosellock; \ 774e04ac2feSJohn Baldwin } \ 775d5e4d7e1SBruce Evans } while (0) 776df8bae1dSRodney W. Grimes getbits(in, 0); 777df8bae1dSRodney W. Grimes getbits(ou, 1); 778df8bae1dSRodney W. Grimes getbits(ex, 2); 779df8bae1dSRodney W. Grimes #undef getbits 780d5e4d7e1SBruce Evans if (nbufbytes != 0) 781d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 782df8bae1dSRodney W. Grimes 783df8bae1dSRodney W. Grimes if (uap->tv) { 7840a3e28cfSAlfred Perlstein error = copyin(uap->tv, &atv, sizeof (atv)); 785265fc98fSSeigo Tanimura if (error) 78685f190e4SAlfred Perlstein goto done_nosellock; 787df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 788df8bae1dSRodney W. Grimes error = EINVAL; 78985f190e4SAlfred Perlstein goto done_nosellock; 790df8bae1dSRodney W. Grimes } 791c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 79200af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 7939c386f6bSJohn Baldwin } else { 79400af9731SPoul-Henning Kamp atv.tv_sec = 0; 7959c386f6bSJohn Baldwin atv.tv_usec = 0; 7969c386f6bSJohn Baldwin } 79700af9731SPoul-Henning Kamp timo = 0; 79885f190e4SAlfred Perlstein mtx_lock(&sellock); 799df8bae1dSRodney W. Grimes retry: 800df8bae1dSRodney W. Grimes ncoll = nselcoll; 801fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 802b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 803fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 80485f190e4SAlfred Perlstein mtx_unlock(&sellock); 80585f190e4SAlfred Perlstein 80685f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 80785f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 808b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 80985f190e4SAlfred Perlstein mtx_lock(&sellock); 810b40ce416SJulian Elischer if (error || td->td_retval[0]) 811df8bae1dSRodney W. Grimes goto done; 8124da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 813c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 81485f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 815df8bae1dSRodney W. Grimes goto done; 81600af9731SPoul-Henning Kamp ttv = atv; 81700af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 81800af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 81900af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 820df8bae1dSRodney W. Grimes } 82185f190e4SAlfred Perlstein 82285f190e4SAlfred Perlstein /* 82385f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 82485f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 82585f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 82685f190e4SAlfred Perlstein * necessary. 82785f190e4SAlfred Perlstein */ 828fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 82985f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 83085f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 83185f190e4SAlfred Perlstein goto retry; 83285f190e4SAlfred Perlstein } 833fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 834bfbbc4aaSJason Evans 835265fc98fSSeigo Tanimura if (timo > 0) 83685f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 837265fc98fSSeigo Tanimura else 83885f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 839bfbbc4aaSJason Evans 840df8bae1dSRodney W. Grimes if (error == 0) 841df8bae1dSRodney W. Grimes goto retry; 842265fc98fSSeigo Tanimura 843df8bae1dSRodney W. Grimes done: 84485f190e4SAlfred Perlstein clear_selinfo_list(td); 845fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 846b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 847fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 84885f190e4SAlfred Perlstein mtx_unlock(&sellock); 84985f190e4SAlfred Perlstein 85085f190e4SAlfred Perlstein done_nosellock: 851df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 852df8bae1dSRodney W. Grimes if (error == ERESTART) 853df8bae1dSRodney W. Grimes error = EINTR; 854df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 855df8bae1dSRodney W. Grimes error = 0; 856df8bae1dSRodney W. Grimes #define putbits(name, x) \ 857d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 858df8bae1dSRodney W. Grimes error = error2; 859df8bae1dSRodney W. Grimes if (error == 0) { 860df8bae1dSRodney W. Grimes int error2; 861df8bae1dSRodney W. Grimes 862df8bae1dSRodney W. Grimes putbits(in, 0); 863df8bae1dSRodney W. Grimes putbits(ou, 1); 864df8bae1dSRodney W. Grimes putbits(ex, 2); 865df8bae1dSRodney W. Grimes #undef putbits 866df8bae1dSRodney W. Grimes } 867d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 868d5e4d7e1SBruce Evans free(selbits, M_SELECT); 869ad2edad9SMatthew Dillon 870ad2edad9SMatthew Dillon mtx_unlock(&Giant); 871df8bae1dSRodney W. Grimes return (error); 872df8bae1dSRodney W. Grimes } 873df8bae1dSRodney W. Grimes 874265fc98fSSeigo Tanimura static int 875b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 876b40ce416SJulian Elischer struct thread *td; 877b08f7993SSujal Patel fd_mask **ibits, **obits; 878cb226aaaSPoul-Henning Kamp int nfd; 879df8bae1dSRodney W. Grimes { 880f082218cSPeter Wemm int msk, i, fd; 881f082218cSPeter Wemm fd_mask bits; 882df8bae1dSRodney W. Grimes struct file *fp; 883df8bae1dSRodney W. Grimes int n = 0; 8842087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 88542d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 886eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 887df8bae1dSRodney W. Grimes 888eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 889df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 890d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 891d5e4d7e1SBruce Evans continue; 892df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 893b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 894f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 895f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 896f082218cSPeter Wemm if (!(bits & 1)) 897f082218cSPeter Wemm continue; 898eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 899eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 900df8bae1dSRodney W. Grimes return (EBADF); 901eb209311SAlfred Perlstein } 902ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 903ea6027a8SRobert Watson td)) { 904b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 905f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 906df8bae1dSRodney W. Grimes n++; 907df8bae1dSRodney W. Grimes } 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes } 910df8bae1dSRodney W. Grimes } 911eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 912b40ce416SJulian Elischer td->td_retval[0] = n; 913df8bae1dSRodney W. Grimes return (0); 914df8bae1dSRodney W. Grimes } 915df8bae1dSRodney W. Grimes 91642d11757SPeter Wemm /* 91742d11757SPeter Wemm * Poll system call. 91842d11757SPeter Wemm */ 91942d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 92042d11757SPeter Wemm struct poll_args { 92142d11757SPeter Wemm struct pollfd *fds; 92242d11757SPeter Wemm u_int nfds; 92342d11757SPeter Wemm int timeout; 92442d11757SPeter Wemm }; 92542d11757SPeter Wemm #endif 926ad2edad9SMatthew Dillon /* 927ad2edad9SMatthew Dillon * MPSAFE 928ad2edad9SMatthew Dillon */ 92942d11757SPeter Wemm int 930b40ce416SJulian Elischer poll(td, uap) 931b40ce416SJulian Elischer struct thread *td; 932ea0237edSJonathan Lemon struct poll_args *uap; 93342d11757SPeter Wemm { 93442d11757SPeter Wemm caddr_t bits; 93542d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 93600af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 9379ae6d334SKelly Yancey int error = 0, timo; 9389ae6d334SKelly Yancey u_int ncoll, nfds; 93942d11757SPeter Wemm size_t ni; 94042d11757SPeter Wemm 94189b71647SPeter Wemm nfds = SCARG(uap, nfds); 942ad2edad9SMatthew Dillon 943ad2edad9SMatthew Dillon mtx_lock(&Giant); 94489b71647SPeter Wemm /* 9452bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9462bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9472bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9482bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9492bd5ac33SPeter Wemm * safe, but not overly restrictive. 95089b71647SPeter Wemm */ 951b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 952b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 953ad2edad9SMatthew Dillon error = EINVAL; 954ad2edad9SMatthew Dillon goto done2; 955ad2edad9SMatthew Dillon } 95689b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 95742d11757SPeter Wemm if (ni > sizeof(smallbits)) 95842d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 95942d11757SPeter Wemm else 96042d11757SPeter Wemm bits = smallbits; 96142d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 96242d11757SPeter Wemm if (error) 96385f190e4SAlfred Perlstein goto done_nosellock; 96442d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 96542d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 96642d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 96742d11757SPeter Wemm if (itimerfix(&atv)) { 96842d11757SPeter Wemm error = EINVAL; 96985f190e4SAlfred Perlstein goto done_nosellock; 97042d11757SPeter Wemm } 971c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 97200af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9739c386f6bSJohn Baldwin } else { 97400af9731SPoul-Henning Kamp atv.tv_sec = 0; 9759c386f6bSJohn Baldwin atv.tv_usec = 0; 9769c386f6bSJohn Baldwin } 97700af9731SPoul-Henning Kamp timo = 0; 97885f190e4SAlfred Perlstein mtx_lock(&sellock); 97942d11757SPeter Wemm retry: 98042d11757SPeter Wemm ncoll = nselcoll; 981fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 982b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 983fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 98485f190e4SAlfred Perlstein mtx_unlock(&sellock); 98585f190e4SAlfred Perlstein 98685f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 98785f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 988b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 98985f190e4SAlfred Perlstein mtx_lock(&sellock); 990b40ce416SJulian Elischer if (error || td->td_retval[0]) 99142d11757SPeter Wemm goto done; 9924da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 993c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 99485f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 99542d11757SPeter Wemm goto done; 99600af9731SPoul-Henning Kamp ttv = atv; 99700af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 99800af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 99900af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 100042d11757SPeter Wemm } 100185f190e4SAlfred Perlstein /* 100285f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 100385f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 100485f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 100585f190e4SAlfred Perlstein */ 1006fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 100785f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 1008fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 100985f190e4SAlfred Perlstein goto retry; 101085f190e4SAlfred Perlstein } 101185f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 101285f190e4SAlfred Perlstein 1013265fc98fSSeigo Tanimura if (timo > 0) 101485f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 1015265fc98fSSeigo Tanimura else 101685f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 101785f190e4SAlfred Perlstein 101842d11757SPeter Wemm if (error == 0) 101942d11757SPeter Wemm goto retry; 1020265fc98fSSeigo Tanimura 102142d11757SPeter Wemm done: 102285f190e4SAlfred Perlstein clear_selinfo_list(td); 1023fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1024b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1025fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 102685f190e4SAlfred Perlstein mtx_unlock(&sellock); 102785f190e4SAlfred Perlstein 102885f190e4SAlfred Perlstein done_nosellock: 102942d11757SPeter Wemm /* poll is not restarted after signals... */ 103042d11757SPeter Wemm if (error == ERESTART) 103142d11757SPeter Wemm error = EINTR; 103242d11757SPeter Wemm if (error == EWOULDBLOCK) 103342d11757SPeter Wemm error = 0; 103442d11757SPeter Wemm if (error == 0) { 103542d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 103642d11757SPeter Wemm if (error) 103742d11757SPeter Wemm goto out; 103842d11757SPeter Wemm } 103942d11757SPeter Wemm out: 104042d11757SPeter Wemm if (ni > sizeof(smallbits)) 104142d11757SPeter Wemm free(bits, M_TEMP); 1042ad2edad9SMatthew Dillon done2: 1043ad2edad9SMatthew Dillon mtx_unlock(&Giant); 104442d11757SPeter Wemm return (error); 104542d11757SPeter Wemm } 104642d11757SPeter Wemm 104742d11757SPeter Wemm static int 1048b40ce416SJulian Elischer pollscan(td, fds, nfd) 1049b40ce416SJulian Elischer struct thread *td; 105042d11757SPeter Wemm struct pollfd *fds; 1051ea0237edSJonathan Lemon u_int nfd; 105242d11757SPeter Wemm { 1053b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 105442d11757SPeter Wemm int i; 105542d11757SPeter Wemm struct file *fp; 105642d11757SPeter Wemm int n = 0; 105742d11757SPeter Wemm 1058426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1059eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1060337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 106142d11757SPeter Wemm fds->revents = POLLNVAL; 106242d11757SPeter Wemm n++; 1063337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1064337c9691SJordan K. Hubbard fds->revents = 0; 106542d11757SPeter Wemm } else { 106642d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1067279d7226SMatthew Dillon if (fp == NULL) { 106842d11757SPeter Wemm fds->revents = POLLNVAL; 106942d11757SPeter Wemm n++; 107042d11757SPeter Wemm } else { 10712087c896SBruce Evans /* 10722087c896SBruce Evans * Note: backend also returns POLLHUP and 10732087c896SBruce Evans * POLLERR if appropriate. 10742087c896SBruce Evans */ 107513ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1076ea6027a8SRobert Watson td->td_ucred, td); 107742d11757SPeter Wemm if (fds->revents != 0) 107842d11757SPeter Wemm n++; 107942d11757SPeter Wemm } 108042d11757SPeter Wemm } 108142d11757SPeter Wemm } 1082eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1083b40ce416SJulian Elischer td->td_retval[0] = n; 108442d11757SPeter Wemm return (0); 108542d11757SPeter Wemm } 108642d11757SPeter Wemm 108742d11757SPeter Wemm /* 108842d11757SPeter Wemm * OpenBSD poll system call. 108942d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 109042d11757SPeter Wemm */ 109142d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 109242d11757SPeter Wemm struct openbsd_poll_args { 109342d11757SPeter Wemm struct pollfd *fds; 109442d11757SPeter Wemm u_int nfds; 109542d11757SPeter Wemm int timeout; 109642d11757SPeter Wemm }; 109742d11757SPeter Wemm #endif 1098ad2edad9SMatthew Dillon /* 1099ad2edad9SMatthew Dillon * MPSAFE 1100ad2edad9SMatthew Dillon */ 110142d11757SPeter Wemm int 1102b40ce416SJulian Elischer openbsd_poll(td, uap) 1103b40ce416SJulian Elischer register struct thread *td; 110442d11757SPeter Wemm register struct openbsd_poll_args *uap; 110542d11757SPeter Wemm { 1106b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 110742d11757SPeter Wemm } 110842d11757SPeter Wemm 110985f190e4SAlfred Perlstein /* 111085f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 111185f190e4SAlfred Perlstein * we were polling. 111285f190e4SAlfred Perlstein * 111385f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 111485f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 111585f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 111685f190e4SAlfred Perlstein */ 111785f190e4SAlfred Perlstein void 111885f190e4SAlfred Perlstein clear_selinfo_list(td) 111985f190e4SAlfred Perlstein struct thread *td; 112085f190e4SAlfred Perlstein { 112185f190e4SAlfred Perlstein struct selinfo *si; 112285f190e4SAlfred Perlstein 112385f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 112485f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 112585f190e4SAlfred Perlstein si->si_thread = NULL; 112685f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 112785f190e4SAlfred Perlstein } 112885f190e4SAlfred Perlstein 1129df8bae1dSRodney W. Grimes /*ARGSUSED*/ 113026f9a767SRodney W. Grimes int 1131b40ce416SJulian Elischer seltrue(dev, events, td) 1132df8bae1dSRodney W. Grimes dev_t dev; 113342d11757SPeter Wemm int events; 1134b40ce416SJulian Elischer struct thread *td; 1135df8bae1dSRodney W. Grimes { 1136df8bae1dSRodney W. Grimes 113742d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1138df8bae1dSRodney W. Grimes } 1139df8bae1dSRodney W. Grimes 1140df8bae1dSRodney W. Grimes /* 1141df8bae1dSRodney W. Grimes * Record a select request. 1142df8bae1dSRodney W. Grimes */ 1143df8bae1dSRodney W. Grimes void 1144df8bae1dSRodney W. Grimes selrecord(selector, sip) 1145b40ce416SJulian Elischer struct thread *selector; 1146df8bae1dSRodney W. Grimes struct selinfo *sip; 1147df8bae1dSRodney W. Grimes { 1148df8bae1dSRodney W. Grimes 114985f190e4SAlfred Perlstein mtx_lock(&sellock); 115085f190e4SAlfred Perlstein /* 1151b605b54cSAlfred Perlstein * If the selinfo's thread pointer is NULL then take ownership of it. 1152b605b54cSAlfred Perlstein * 1153b605b54cSAlfred Perlstein * If the thread pointer is not NULL and it points to another 1154b605b54cSAlfred Perlstein * thread, then we have a collision. 1155b605b54cSAlfred Perlstein * 1156b605b54cSAlfred Perlstein * If the thread pointer is not NULL and points back to us then leave 1157b605b54cSAlfred Perlstein * it alone as we've already added pointed it at us and added it to 1158b605b54cSAlfred Perlstein * our list. 115985f190e4SAlfred Perlstein */ 116085f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1161b40ce416SJulian Elischer sip->si_thread = selector; 116285f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 116385f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 116485f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 116585f190e4SAlfred Perlstein } 116685f190e4SAlfred Perlstein 116785f190e4SAlfred Perlstein mtx_unlock(&sellock); 1168df8bae1dSRodney W. Grimes } 1169df8bae1dSRodney W. Grimes 1170df8bae1dSRodney W. Grimes /* 1171df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1172df8bae1dSRodney W. Grimes */ 1173df8bae1dSRodney W. Grimes void 1174df8bae1dSRodney W. Grimes selwakeup(sip) 117585f190e4SAlfred Perlstein struct selinfo *sip; 1176df8bae1dSRodney W. Grimes { 1177b40ce416SJulian Elischer struct thread *td; 1178df8bae1dSRodney W. Grimes 117985f190e4SAlfred Perlstein mtx_lock(&sellock); 118085f190e4SAlfred Perlstein td = sip->si_thread; 118185f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1182df8bae1dSRodney W. Grimes nselcoll++; 1183df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1184265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1185df8bae1dSRodney W. Grimes } 118685f190e4SAlfred Perlstein if (td == NULL) { 118785f190e4SAlfred Perlstein mtx_unlock(&sellock); 1188b40ce416SJulian Elischer return; 1189b40ce416SJulian Elischer } 119085f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 119185f190e4SAlfred Perlstein sip->si_thread = NULL; 11929ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 11930a3e28cfSAlfred Perlstein if (td->td_wchan == &selwait) { 1194e602ba25SJulian Elischer if (td->td_state == TDS_SLP) 1195b40ce416SJulian Elischer setrunnable(td); 1196df8bae1dSRodney W. Grimes else 1197b40ce416SJulian Elischer cv_waitq_remove(td); 119833a9ed9dSJohn Baldwin } else 1199b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 120033a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 120185f190e4SAlfred Perlstein mtx_unlock(&sellock); 1202df8bae1dSRodney W. Grimes } 1203265fc98fSSeigo Tanimura 12044d77a549SAlfred Perlstein static void selectinit(void *); 1205265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1206265fc98fSSeigo Tanimura 1207265fc98fSSeigo Tanimura /* ARGSUSED*/ 1208265fc98fSSeigo Tanimura static void 1209265fc98fSSeigo Tanimura selectinit(dummy) 1210265fc98fSSeigo Tanimura void *dummy; 1211265fc98fSSeigo Tanimura { 1212265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 12136008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1214265fc98fSSeigo Tanimura } 1215