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 1728fe387abSDmitrij Tejblum aiov.iov_base = (caddr_t)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 */ 187b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, 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 195b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, 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; 205b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, 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; 263bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)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 */ 278b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 279df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 280df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 28142ebfbf2SBrian Feldman ktruio = auio; 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes #endif 284df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 285b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, 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; 296b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio, 29742ebfbf2SBrian Feldman error); 29842ebfbf2SBrian Feldman } 299df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes #endif 302b40ce416SJulian Elischer td->td_retval[0] = cnt; 303df8bae1dSRodney W. Grimes done: 304b40ce416SJulian Elischer fdrop(fp, td); 305df8bae1dSRodney W. Grimes if (needfree) 306df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 307df8bae1dSRodney W. Grimes return (error); 308df8bae1dSRodney W. Grimes } 309df8bae1dSRodney W. Grimes 310df8bae1dSRodney W. Grimes /* 311df8bae1dSRodney W. Grimes * Write system call 312df8bae1dSRodney W. Grimes */ 313d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 314df8bae1dSRodney W. Grimes struct write_args { 315df8bae1dSRodney W. Grimes int fd; 316134e06feSBruce Evans const void *buf; 317134e06feSBruce Evans size_t nbyte; 318df8bae1dSRodney W. Grimes }; 319d2d3e875SBruce Evans #endif 320ad2edad9SMatthew Dillon /* 321ad2edad9SMatthew Dillon * MPSAFE 322ad2edad9SMatthew Dillon */ 32326f9a767SRodney W. Grimes int 324b40ce416SJulian Elischer write(td, uap) 325b40ce416SJulian Elischer struct thread *td; 326b064d43dSMatthew Dillon struct write_args *uap; 327df8bae1dSRodney W. Grimes { 328b064d43dSMatthew Dillon struct file *fp; 329279d7226SMatthew Dillon int error; 330df8bae1dSRodney W. Grimes 331b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 332b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 333ad2edad9SMatthew Dillon (off_t)-1, 0); 334b40ce416SJulian Elischer fdrop(fp, td); 335ad2edad9SMatthew Dillon } else { 336b064d43dSMatthew Dillon error = EBADF; /* XXX this can't be right */ 337ad2edad9SMatthew Dillon } 338279d7226SMatthew Dillon return(error); 339df8bae1dSRodney W. Grimes } 340df8bae1dSRodney W. Grimes 341df8bae1dSRodney W. Grimes /* 3428fe387abSDmitrij Tejblum * Pwrite system call 3434160ccd9SAlan Cox */ 3444160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3454160ccd9SAlan Cox struct pwrite_args { 3464160ccd9SAlan Cox int fd; 3474160ccd9SAlan Cox const void *buf; 3484160ccd9SAlan Cox size_t nbyte; 3498fe387abSDmitrij Tejblum int pad; 3504160ccd9SAlan Cox off_t offset; 3514160ccd9SAlan Cox }; 3524160ccd9SAlan Cox #endif 353ad2edad9SMatthew Dillon /* 354ad2edad9SMatthew Dillon * MPSAFE 355ad2edad9SMatthew Dillon */ 3564160ccd9SAlan Cox int 357b40ce416SJulian Elischer pwrite(td, uap) 358b40ce416SJulian Elischer struct thread *td; 359b064d43dSMatthew Dillon struct pwrite_args *uap; 3604160ccd9SAlan Cox { 361b064d43dSMatthew Dillon struct file *fp; 362279d7226SMatthew Dillon int error; 3638fe387abSDmitrij Tejblum 364b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 365aa11a498SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) { 366b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 367b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 368b064d43dSMatthew Dillon } else { 369279d7226SMatthew Dillon error = ESPIPE; 370b064d43dSMatthew Dillon } 371b40ce416SJulian Elischer fdrop(fp, td); 372279d7226SMatthew Dillon } else { 373b064d43dSMatthew Dillon error = EBADF; /* this can't be right */ 374ad2edad9SMatthew Dillon } 375279d7226SMatthew Dillon return(error); 3768fe387abSDmitrij Tejblum } 3778fe387abSDmitrij Tejblum 3788fe387abSDmitrij Tejblum static int 379b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 380b40ce416SJulian Elischer struct thread *td; 3818fe387abSDmitrij Tejblum struct file *fp; 3828fe387abSDmitrij Tejblum int fd, flags; 3838fe387abSDmitrij Tejblum const void *buf; 3848fe387abSDmitrij Tejblum size_t nbyte; 3858fe387abSDmitrij Tejblum off_t offset; 3868fe387abSDmitrij Tejblum { 3874160ccd9SAlan Cox struct uio auio; 3884160ccd9SAlan Cox struct iovec aiov; 3894160ccd9SAlan Cox long cnt, error = 0; 3904160ccd9SAlan Cox #ifdef KTRACE 3914160ccd9SAlan Cox struct iovec ktriov; 39242ebfbf2SBrian Feldman struct uio ktruio; 3933c89e357SBrian Feldman int didktr = 0; 3944160ccd9SAlan Cox #endif 3954160ccd9SAlan Cox 396b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 3978fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 3984160ccd9SAlan Cox auio.uio_iov = &aiov; 3994160ccd9SAlan Cox auio.uio_iovcnt = 1; 4008fe387abSDmitrij Tejblum auio.uio_offset = offset; 4018fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 4024160ccd9SAlan Cox return (EINVAL); 4038fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 4044160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 4054160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 406b40ce416SJulian Elischer auio.uio_td = td; 4074160ccd9SAlan Cox #ifdef KTRACE 4084160ccd9SAlan Cox /* 40942ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4104160ccd9SAlan Cox */ 411b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 4124160ccd9SAlan Cox ktriov = aiov; 41342ebfbf2SBrian Feldman ktruio = auio; 4143c89e357SBrian Feldman didktr = 1; 41542ebfbf2SBrian Feldman } 4164160ccd9SAlan Cox #endif 4178fe387abSDmitrij Tejblum cnt = nbyte; 418c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 419279d7226SMatthew Dillon bwillwrite(); 420b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) { 4214160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4224160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4234160ccd9SAlan Cox error = 0; 42419eb87d2SJohn Baldwin if (error == EPIPE) { 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; 435b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, 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; 497bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)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 */ 512b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 513df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 514df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)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(); 521b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, 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; 537b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_WRITE, &ktruio, 53842ebfbf2SBrian Feldman error); 53942ebfbf2SBrian Feldman } 540df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes #endif 543b40ce416SJulian Elischer td->td_retval[0] = cnt; 544df8bae1dSRodney W. Grimes done: 545b40ce416SJulian Elischer fdrop(fp, td); 546df8bae1dSRodney W. Grimes if (needfree) 547df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 548ad2edad9SMatthew Dillon done2: 549ad2edad9SMatthew Dillon mtx_unlock(&Giant); 550df8bae1dSRodney W. Grimes return (error); 551df8bae1dSRodney W. Grimes } 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes /* 554df8bae1dSRodney W. Grimes * Ioctl system call 555df8bae1dSRodney W. Grimes */ 556d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 557df8bae1dSRodney W. Grimes struct ioctl_args { 558df8bae1dSRodney W. Grimes int fd; 559069e9bc1SDoug Rabson u_long com; 560df8bae1dSRodney W. Grimes caddr_t data; 561df8bae1dSRodney W. Grimes }; 562d2d3e875SBruce Evans #endif 563ad2edad9SMatthew Dillon /* 564ad2edad9SMatthew Dillon * MPSAFE 565ad2edad9SMatthew Dillon */ 566df8bae1dSRodney W. Grimes /* ARGSUSED */ 56726f9a767SRodney W. Grimes int 568b40ce416SJulian Elischer ioctl(td, uap) 569b40ce416SJulian Elischer struct thread *td; 570df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 571df8bae1dSRodney W. Grimes { 572a4db4953SAlfred Perlstein struct file *fp; 573df8bae1dSRodney W. Grimes register struct filedesc *fdp; 574831b9ef2SDoug Rabson register u_long com; 575ad2edad9SMatthew Dillon int error = 0; 576df8bae1dSRodney W. Grimes register u_int size; 577df8bae1dSRodney W. Grimes caddr_t data, memp; 578df8bae1dSRodney W. Grimes int tmp; 579df8bae1dSRodney W. Grimes #define STK_PARAMS 128 580d2ba455cSMatthew Dillon union { 581df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 582d2ba455cSMatthew Dillon long align; 583d2ba455cSMatthew Dillon } ubuf; 584df8bae1dSRodney W. Grimes 585a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 586a4db4953SAlfred Perlstein return (error); 587aa11a498SAlfred Perlstein mtx_lock(&Giant); 588ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 589426da3bcSAlfred Perlstein fdrop(fp, td); 590aa11a498SAlfred Perlstein mtx_unlock(&Giant); 591426da3bcSAlfred Perlstein return (EBADF); 592ad2edad9SMatthew Dillon } 593426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 594df8bae1dSRodney W. Grimes switch (com = uap->com) { 595df8bae1dSRodney W. Grimes case FIONCLEX: 596426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 597df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 598426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 599426da3bcSAlfred Perlstein fdrop(fp, td); 600aa11a498SAlfred Perlstein mtx_unlock(&Giant); 601426da3bcSAlfred Perlstein return (0); 602df8bae1dSRodney W. Grimes case FIOCLEX: 603426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 604df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 605426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 606426da3bcSAlfred Perlstein fdrop(fp, td); 607aa11a498SAlfred Perlstein mtx_unlock(&Giant); 608426da3bcSAlfred Perlstein return (0); 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes 611df8bae1dSRodney W. Grimes /* 612df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 613df8bae1dSRodney W. Grimes * copied to/from the user's address space. 614df8bae1dSRodney W. Grimes */ 615df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 616ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 617426da3bcSAlfred Perlstein fdrop(fp, td); 618aa11a498SAlfred Perlstein mtx_unlock(&Giant); 619426da3bcSAlfred Perlstein return (ENOTTY); 620ad2edad9SMatthew Dillon } 621279d7226SMatthew Dillon 622df8bae1dSRodney W. Grimes memp = NULL; 623d2ba455cSMatthew Dillon if (size > sizeof (ubuf.stkbuf)) { 624df8bae1dSRodney W. Grimes memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 625df8bae1dSRodney W. Grimes data = memp; 626279d7226SMatthew Dillon } else { 627d2ba455cSMatthew Dillon data = ubuf.stkbuf; 628279d7226SMatthew Dillon } 629df8bae1dSRodney W. Grimes if (com&IOC_IN) { 630df8bae1dSRodney W. Grimes if (size) { 631df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 632df8bae1dSRodney W. Grimes if (error) { 633df8bae1dSRodney W. Grimes if (memp) 634df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 635b40ce416SJulian Elischer fdrop(fp, td); 636426da3bcSAlfred Perlstein goto done; 637df8bae1dSRodney W. Grimes } 638279d7226SMatthew Dillon } else { 639df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 640279d7226SMatthew Dillon } 641279d7226SMatthew Dillon } else if ((com&IOC_OUT) && size) { 642df8bae1dSRodney W. Grimes /* 643df8bae1dSRodney W. Grimes * Zero the buffer so the user always 644df8bae1dSRodney W. Grimes * gets back something deterministic. 645df8bae1dSRodney W. Grimes */ 646df8bae1dSRodney W. Grimes bzero(data, size); 647279d7226SMatthew Dillon } else if (com&IOC_VOID) { 648df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 649279d7226SMatthew Dillon } 650df8bae1dSRodney W. Grimes 651f67ad03aSPoul-Henning Kamp #ifdef __alpha__ 6520b5d880dSPoul-Henning Kamp { 6530b5d880dSPoul-Henning Kamp int annoy = 1; 6540b5d880dSPoul-Henning Kamp 655f67ad03aSPoul-Henning Kamp if (com == DIOCGDINFO_ALPHAHACK) 656f67ad03aSPoul-Henning Kamp com = DIOCGDINFO; 6570b5d880dSPoul-Henning Kamp else if (com == DIOCSDINFO_ALPHAHACK) 658f67ad03aSPoul-Henning Kamp com = DIOCSDINFO; 6590b5d880dSPoul-Henning Kamp else if (com == DIOCWDINFO_ALPHAHACK) 660f67ad03aSPoul-Henning Kamp com = DIOCWDINFO; 6610b5d880dSPoul-Henning Kamp else if (com == DIOCGDVIRGIN_ALPHAHACK) 662f67ad03aSPoul-Henning Kamp com = DIOCGDVIRGIN; 6630b5d880dSPoul-Henning Kamp else 6640b5d880dSPoul-Henning Kamp annoy = 0; 6650b5d880dSPoul-Henning Kamp if (annoy) { 6660b5d880dSPoul-Henning Kamp uprintf("Recompile this program, it uses obsolete ioctls.\n"); 6670b5d880dSPoul-Henning Kamp printf("Program using uses obsolete ioctls used, recompile.\n"); 6680b5d880dSPoul-Henning Kamp tsleep(&annoy, PPAUSE, "syncer", 15 * hz); 6690b5d880dSPoul-Henning Kamp } 6700b5d880dSPoul-Henning Kamp } 671f67ad03aSPoul-Henning Kamp #endif 672f67ad03aSPoul-Henning Kamp 673df8bae1dSRodney W. Grimes switch (com) { 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes case FIONBIO: 676426da3bcSAlfred Perlstein FILE_LOCK(fp); 677bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 678df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 679df8bae1dSRodney W. Grimes else 680df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 681426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 682b40ce416SJulian Elischer error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td); 683df8bae1dSRodney W. Grimes break; 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes case FIOASYNC: 686426da3bcSAlfred Perlstein FILE_LOCK(fp); 687bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 688df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 689df8bae1dSRodney W. Grimes else 690df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 691426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 692b40ce416SJulian Elischer error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td); 693df8bae1dSRodney W. Grimes break; 694df8bae1dSRodney W. Grimes 695df8bae1dSRodney W. Grimes default: 696b40ce416SJulian Elischer error = fo_ioctl(fp, com, data, td); 697df8bae1dSRodney W. Grimes /* 698df8bae1dSRodney W. Grimes * Copy any data to user, size was 699df8bae1dSRodney W. Grimes * already set and checked above. 700df8bae1dSRodney W. Grimes */ 701df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 702df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 703df8bae1dSRodney W. Grimes break; 704df8bae1dSRodney W. Grimes } 705df8bae1dSRodney W. Grimes if (memp) 706df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 707b40ce416SJulian Elischer fdrop(fp, td); 708426da3bcSAlfred Perlstein done: 709ad2edad9SMatthew Dillon mtx_unlock(&Giant); 710df8bae1dSRodney W. Grimes return (error); 711df8bae1dSRodney W. Grimes } 712df8bae1dSRodney W. Grimes 71385f190e4SAlfred Perlstein /* 71485f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 71585f190e4SAlfred Perlstein */ 71685f190e4SAlfred Perlstein struct mtx sellock; 717265fc98fSSeigo Tanimura struct cv selwait; 71885f190e4SAlfred Perlstein int nselcoll; /* Select collisions since boot */ 7198cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 720df8bae1dSRodney W. Grimes 721df8bae1dSRodney W. Grimes /* 722df8bae1dSRodney W. Grimes * Select system call. 723df8bae1dSRodney W. Grimes */ 724d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 725df8bae1dSRodney W. Grimes struct select_args { 726b08f7993SSujal Patel int nd; 727df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 728df8bae1dSRodney W. Grimes struct timeval *tv; 729df8bae1dSRodney W. Grimes }; 730d2d3e875SBruce Evans #endif 731ad2edad9SMatthew Dillon /* 732ad2edad9SMatthew Dillon * MPSAFE 733ad2edad9SMatthew Dillon */ 73426f9a767SRodney W. Grimes int 735b40ce416SJulian Elischer select(td, uap) 736b40ce416SJulian Elischer register struct thread *td; 737df8bae1dSRodney W. Grimes register struct select_args *uap; 738df8bae1dSRodney W. Grimes { 739426da3bcSAlfred Perlstein struct filedesc *fdp; 740d5e4d7e1SBruce Evans /* 741d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 742d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 743d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 744d5e4d7e1SBruce Evans * of 256. 745d5e4d7e1SBruce Evans */ 746d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 747eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 74800af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7494658f926SAlfred Perlstein int ncoll, error, timo; 750d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 751df8bae1dSRodney W. Grimes 752b08f7993SSujal Patel if (uap->nd < 0) 753acbfbfeaSSujal Patel return (EINVAL); 754426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 755ad2edad9SMatthew Dillon mtx_lock(&Giant); 756426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 757ad2edad9SMatthew Dillon 758b40ce416SJulian Elischer if (uap->nd > td->td_proc->p_fd->fd_nfiles) 759b40ce416SJulian Elischer uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 760426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 761b08f7993SSujal Patel 762d5e4d7e1SBruce Evans /* 763d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 764d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 765d5e4d7e1SBruce Evans */ 766d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 767d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 768d5e4d7e1SBruce Evans nbufbytes = 0; 769d5e4d7e1SBruce Evans if (uap->in != NULL) 770d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 771d5e4d7e1SBruce Evans if (uap->ou != NULL) 772d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 773d5e4d7e1SBruce Evans if (uap->ex != NULL) 774d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 775d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 776d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 777d5e4d7e1SBruce Evans else 778d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 779b08f7993SSujal Patel 780b08f7993SSujal Patel /* 781d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 782d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 783d5e4d7e1SBruce Evans * together. 784b08f7993SSujal Patel */ 785d5e4d7e1SBruce Evans sbp = selbits; 786df8bae1dSRodney W. Grimes #define getbits(name, x) \ 787d5e4d7e1SBruce Evans do { \ 788d5e4d7e1SBruce Evans if (uap->name == NULL) \ 789d5e4d7e1SBruce Evans ibits[x] = NULL; \ 790d5e4d7e1SBruce Evans else { \ 791d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 792d5e4d7e1SBruce Evans obits[x] = sbp; \ 793d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 794d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 795265fc98fSSeigo Tanimura if (error != 0) \ 79685f190e4SAlfred Perlstein goto done_nosellock; \ 797e04ac2feSJohn Baldwin } \ 798d5e4d7e1SBruce Evans } while (0) 799df8bae1dSRodney W. Grimes getbits(in, 0); 800df8bae1dSRodney W. Grimes getbits(ou, 1); 801df8bae1dSRodney W. Grimes getbits(ex, 2); 802df8bae1dSRodney W. Grimes #undef getbits 803d5e4d7e1SBruce Evans if (nbufbytes != 0) 804d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 805df8bae1dSRodney W. Grimes 806df8bae1dSRodney W. Grimes if (uap->tv) { 807df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 808df8bae1dSRodney W. Grimes sizeof (atv)); 809265fc98fSSeigo Tanimura if (error) 81085f190e4SAlfred Perlstein goto done_nosellock; 811df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 812df8bae1dSRodney W. Grimes error = EINVAL; 81385f190e4SAlfred Perlstein goto done_nosellock; 814df8bae1dSRodney W. Grimes } 815c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 81600af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8179c386f6bSJohn Baldwin } else { 81800af9731SPoul-Henning Kamp atv.tv_sec = 0; 8199c386f6bSJohn Baldwin atv.tv_usec = 0; 8209c386f6bSJohn Baldwin } 82100af9731SPoul-Henning Kamp timo = 0; 82285f190e4SAlfred Perlstein mtx_lock(&sellock); 823df8bae1dSRodney W. Grimes retry: 824df8bae1dSRodney W. Grimes ncoll = nselcoll; 825fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 826b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 827fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 82885f190e4SAlfred Perlstein mtx_unlock(&sellock); 82985f190e4SAlfred Perlstein 83085f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 83185f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 832b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 83385f190e4SAlfred Perlstein mtx_lock(&sellock); 834b40ce416SJulian Elischer if (error || td->td_retval[0]) 835df8bae1dSRodney W. Grimes goto done; 8364da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 837c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 83885f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 839df8bae1dSRodney W. Grimes goto done; 84000af9731SPoul-Henning Kamp ttv = atv; 84100af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 84200af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 84300af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 844df8bae1dSRodney W. Grimes } 84585f190e4SAlfred Perlstein 84685f190e4SAlfred Perlstein /* 84785f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 84885f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 84985f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 85085f190e4SAlfred Perlstein * necessary. 85185f190e4SAlfred Perlstein */ 852fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 85385f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 85485f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 85585f190e4SAlfred Perlstein goto retry; 85685f190e4SAlfred Perlstein } 857fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 858bfbbc4aaSJason Evans 859265fc98fSSeigo Tanimura if (timo > 0) 86085f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 861265fc98fSSeigo Tanimura else 86285f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 863bfbbc4aaSJason Evans 864df8bae1dSRodney W. Grimes if (error == 0) 865df8bae1dSRodney W. Grimes goto retry; 866265fc98fSSeigo Tanimura 867df8bae1dSRodney W. Grimes done: 86885f190e4SAlfred Perlstein clear_selinfo_list(td); 869fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 870b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 871fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 87285f190e4SAlfred Perlstein mtx_unlock(&sellock); 87385f190e4SAlfred Perlstein 87485f190e4SAlfred Perlstein done_nosellock: 875df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 876df8bae1dSRodney W. Grimes if (error == ERESTART) 877df8bae1dSRodney W. Grimes error = EINTR; 878df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 879df8bae1dSRodney W. Grimes error = 0; 880df8bae1dSRodney W. Grimes #define putbits(name, x) \ 881d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 882df8bae1dSRodney W. Grimes error = error2; 883df8bae1dSRodney W. Grimes if (error == 0) { 884df8bae1dSRodney W. Grimes int error2; 885df8bae1dSRodney W. Grimes 886df8bae1dSRodney W. Grimes putbits(in, 0); 887df8bae1dSRodney W. Grimes putbits(ou, 1); 888df8bae1dSRodney W. Grimes putbits(ex, 2); 889df8bae1dSRodney W. Grimes #undef putbits 890df8bae1dSRodney W. Grimes } 891d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 892d5e4d7e1SBruce Evans free(selbits, M_SELECT); 893ad2edad9SMatthew Dillon 894ad2edad9SMatthew Dillon mtx_unlock(&Giant); 895df8bae1dSRodney W. Grimes return (error); 896df8bae1dSRodney W. Grimes } 897df8bae1dSRodney W. Grimes 898265fc98fSSeigo Tanimura static int 899b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 900b40ce416SJulian Elischer struct thread *td; 901b08f7993SSujal Patel fd_mask **ibits, **obits; 902cb226aaaSPoul-Henning Kamp int nfd; 903df8bae1dSRodney W. Grimes { 904f082218cSPeter Wemm int msk, i, fd; 905f082218cSPeter Wemm fd_mask bits; 906df8bae1dSRodney W. Grimes struct file *fp; 907df8bae1dSRodney W. Grimes int n = 0; 9082087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 90942d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 910eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 911df8bae1dSRodney W. Grimes 912eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 913df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 914d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 915d5e4d7e1SBruce Evans continue; 916df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 917b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 918f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 919f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 920f082218cSPeter Wemm if (!(bits & 1)) 921f082218cSPeter Wemm continue; 922eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 923eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 924df8bae1dSRodney W. Grimes return (EBADF); 925eb209311SAlfred Perlstein } 926b40ce416SJulian Elischer if (fo_poll(fp, flag[msk], fp->f_cred, td)) { 927b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 928f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 929df8bae1dSRodney W. Grimes n++; 930df8bae1dSRodney W. Grimes } 931df8bae1dSRodney W. Grimes } 932df8bae1dSRodney W. Grimes } 933df8bae1dSRodney W. Grimes } 934eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 935b40ce416SJulian Elischer td->td_retval[0] = n; 936df8bae1dSRodney W. Grimes return (0); 937df8bae1dSRodney W. Grimes } 938df8bae1dSRodney W. Grimes 93942d11757SPeter Wemm /* 94042d11757SPeter Wemm * Poll system call. 94142d11757SPeter Wemm */ 94242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 94342d11757SPeter Wemm struct poll_args { 94442d11757SPeter Wemm struct pollfd *fds; 94542d11757SPeter Wemm u_int nfds; 94642d11757SPeter Wemm int timeout; 94742d11757SPeter Wemm }; 94842d11757SPeter Wemm #endif 949ad2edad9SMatthew Dillon /* 950ad2edad9SMatthew Dillon * MPSAFE 951ad2edad9SMatthew Dillon */ 95242d11757SPeter Wemm int 953b40ce416SJulian Elischer poll(td, uap) 954b40ce416SJulian Elischer struct thread *td; 955ea0237edSJonathan Lemon struct poll_args *uap; 95642d11757SPeter Wemm { 95742d11757SPeter Wemm caddr_t bits; 95842d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 95900af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 960265fc98fSSeigo Tanimura int ncoll, error = 0, timo; 961ea0237edSJonathan Lemon u_int nfds; 96242d11757SPeter Wemm size_t ni; 96342d11757SPeter Wemm 96489b71647SPeter Wemm nfds = SCARG(uap, nfds); 965ad2edad9SMatthew Dillon 966ad2edad9SMatthew Dillon mtx_lock(&Giant); 96789b71647SPeter Wemm /* 9682bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9692bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9702bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9712bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9722bd5ac33SPeter Wemm * safe, but not overly restrictive. 97389b71647SPeter Wemm */ 974b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 975b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 976ad2edad9SMatthew Dillon error = EINVAL; 977ad2edad9SMatthew Dillon goto done2; 978ad2edad9SMatthew Dillon } 97989b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 98042d11757SPeter Wemm if (ni > sizeof(smallbits)) 98142d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 98242d11757SPeter Wemm else 98342d11757SPeter Wemm bits = smallbits; 98442d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 98542d11757SPeter Wemm if (error) 98685f190e4SAlfred Perlstein goto done_nosellock; 98742d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 98842d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 98942d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 99042d11757SPeter Wemm if (itimerfix(&atv)) { 99142d11757SPeter Wemm error = EINVAL; 99285f190e4SAlfred Perlstein goto done_nosellock; 99342d11757SPeter Wemm } 994c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 99500af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9969c386f6bSJohn Baldwin } else { 99700af9731SPoul-Henning Kamp atv.tv_sec = 0; 9989c386f6bSJohn Baldwin atv.tv_usec = 0; 9999c386f6bSJohn Baldwin } 100000af9731SPoul-Henning Kamp timo = 0; 100185f190e4SAlfred Perlstein mtx_lock(&sellock); 100242d11757SPeter Wemm retry: 100342d11757SPeter Wemm ncoll = nselcoll; 1004fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1005b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 1006fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 100785f190e4SAlfred Perlstein mtx_unlock(&sellock); 100885f190e4SAlfred Perlstein 100985f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 101085f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 1011b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 101285f190e4SAlfred Perlstein mtx_lock(&sellock); 1013b40ce416SJulian Elischer if (error || td->td_retval[0]) 101442d11757SPeter Wemm goto done; 10154da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1016c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 101785f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 101842d11757SPeter Wemm goto done; 101900af9731SPoul-Henning Kamp ttv = atv; 102000af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 102100af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 102200af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 102342d11757SPeter Wemm } 102485f190e4SAlfred Perlstein /* 102585f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 102685f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 102785f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 102885f190e4SAlfred Perlstein */ 1029fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 103085f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 1031fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 103285f190e4SAlfred Perlstein goto retry; 103385f190e4SAlfred Perlstein } 103485f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 103585f190e4SAlfred Perlstein 1036265fc98fSSeigo Tanimura if (timo > 0) 103785f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 1038265fc98fSSeigo Tanimura else 103985f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 104085f190e4SAlfred Perlstein 104142d11757SPeter Wemm if (error == 0) 104242d11757SPeter Wemm goto retry; 1043265fc98fSSeigo Tanimura 104442d11757SPeter Wemm done: 104585f190e4SAlfred Perlstein clear_selinfo_list(td); 1046fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1047b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1048fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 104985f190e4SAlfred Perlstein mtx_unlock(&sellock); 105085f190e4SAlfred Perlstein 105185f190e4SAlfred Perlstein done_nosellock: 105242d11757SPeter Wemm /* poll is not restarted after signals... */ 105342d11757SPeter Wemm if (error == ERESTART) 105442d11757SPeter Wemm error = EINTR; 105542d11757SPeter Wemm if (error == EWOULDBLOCK) 105642d11757SPeter Wemm error = 0; 105742d11757SPeter Wemm if (error == 0) { 105842d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 105942d11757SPeter Wemm if (error) 106042d11757SPeter Wemm goto out; 106142d11757SPeter Wemm } 106242d11757SPeter Wemm out: 106342d11757SPeter Wemm if (ni > sizeof(smallbits)) 106442d11757SPeter Wemm free(bits, M_TEMP); 1065ad2edad9SMatthew Dillon done2: 1066ad2edad9SMatthew Dillon mtx_unlock(&Giant); 106742d11757SPeter Wemm return (error); 106842d11757SPeter Wemm } 106942d11757SPeter Wemm 107042d11757SPeter Wemm static int 1071b40ce416SJulian Elischer pollscan(td, fds, nfd) 1072b40ce416SJulian Elischer struct thread *td; 107342d11757SPeter Wemm struct pollfd *fds; 1074ea0237edSJonathan Lemon u_int nfd; 107542d11757SPeter Wemm { 1076b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 107742d11757SPeter Wemm int i; 107842d11757SPeter Wemm struct file *fp; 107942d11757SPeter Wemm int n = 0; 108042d11757SPeter Wemm 1081426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1082eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1083337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 108442d11757SPeter Wemm fds->revents = POLLNVAL; 108542d11757SPeter Wemm n++; 1086337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1087337c9691SJordan K. Hubbard fds->revents = 0; 108842d11757SPeter Wemm } else { 108942d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1090279d7226SMatthew Dillon if (fp == NULL) { 109142d11757SPeter Wemm fds->revents = POLLNVAL; 109242d11757SPeter Wemm n++; 109342d11757SPeter Wemm } else { 10942087c896SBruce Evans /* 10952087c896SBruce Evans * Note: backend also returns POLLHUP and 10962087c896SBruce Evans * POLLERR if appropriate. 10972087c896SBruce Evans */ 109813ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1099b40ce416SJulian Elischer fp->f_cred, td); 110042d11757SPeter Wemm if (fds->revents != 0) 110142d11757SPeter Wemm n++; 110242d11757SPeter Wemm } 110342d11757SPeter Wemm } 110442d11757SPeter Wemm } 1105eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1106b40ce416SJulian Elischer td->td_retval[0] = n; 110742d11757SPeter Wemm return (0); 110842d11757SPeter Wemm } 110942d11757SPeter Wemm 111042d11757SPeter Wemm /* 111142d11757SPeter Wemm * OpenBSD poll system call. 111242d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 111342d11757SPeter Wemm */ 111442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 111542d11757SPeter Wemm struct openbsd_poll_args { 111642d11757SPeter Wemm struct pollfd *fds; 111742d11757SPeter Wemm u_int nfds; 111842d11757SPeter Wemm int timeout; 111942d11757SPeter Wemm }; 112042d11757SPeter Wemm #endif 1121ad2edad9SMatthew Dillon /* 1122ad2edad9SMatthew Dillon * MPSAFE 1123ad2edad9SMatthew Dillon */ 112442d11757SPeter Wemm int 1125b40ce416SJulian Elischer openbsd_poll(td, uap) 1126b40ce416SJulian Elischer register struct thread *td; 112742d11757SPeter Wemm register struct openbsd_poll_args *uap; 112842d11757SPeter Wemm { 1129b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 113042d11757SPeter Wemm } 113142d11757SPeter Wemm 113285f190e4SAlfred Perlstein /* 113385f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 113485f190e4SAlfred Perlstein * we were polling. 113585f190e4SAlfred Perlstein * 113685f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 113785f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 113885f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 113985f190e4SAlfred Perlstein */ 114085f190e4SAlfred Perlstein void 114185f190e4SAlfred Perlstein clear_selinfo_list(td) 114285f190e4SAlfred Perlstein struct thread *td; 114385f190e4SAlfred Perlstein { 114485f190e4SAlfred Perlstein struct selinfo *si; 114585f190e4SAlfred Perlstein 114685f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 114785f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 114885f190e4SAlfred Perlstein si->si_thread = NULL; 114985f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 115085f190e4SAlfred Perlstein } 115185f190e4SAlfred Perlstein 1152df8bae1dSRodney W. Grimes /*ARGSUSED*/ 115326f9a767SRodney W. Grimes int 1154b40ce416SJulian Elischer seltrue(dev, events, td) 1155df8bae1dSRodney W. Grimes dev_t dev; 115642d11757SPeter Wemm int events; 1157b40ce416SJulian Elischer struct thread *td; 1158df8bae1dSRodney W. Grimes { 1159df8bae1dSRodney W. Grimes 116042d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1161df8bae1dSRodney W. Grimes } 1162df8bae1dSRodney W. Grimes 1163df8bae1dSRodney W. Grimes /* 1164df8bae1dSRodney W. Grimes * Record a select request. 1165df8bae1dSRodney W. Grimes */ 1166df8bae1dSRodney W. Grimes void 1167df8bae1dSRodney W. Grimes selrecord(selector, sip) 1168b40ce416SJulian Elischer struct thread *selector; 1169df8bae1dSRodney W. Grimes struct selinfo *sip; 1170df8bae1dSRodney W. Grimes { 1171df8bae1dSRodney W. Grimes 117285f190e4SAlfred Perlstein mtx_lock(&sellock); 117385f190e4SAlfred Perlstein /* 117485f190e4SAlfred Perlstein * If the thread is NULL then take ownership of selinfo 117585f190e4SAlfred Perlstein * however if the thread is not NULL and the thread points to 117685f190e4SAlfred Perlstein * someone else, then we have a collision, otherwise leave it alone 117785f190e4SAlfred Perlstein * as we've owned it in a previous selrecord on this selinfo. 117885f190e4SAlfred Perlstein */ 117985f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1180b40ce416SJulian Elischer sip->si_thread = selector; 118185f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 118285f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 118385f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 118485f190e4SAlfred Perlstein } 118585f190e4SAlfred Perlstein 118685f190e4SAlfred Perlstein mtx_unlock(&sellock); 1187df8bae1dSRodney W. Grimes } 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes /* 1190df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1191df8bae1dSRodney W. Grimes */ 1192df8bae1dSRodney W. Grimes void 1193df8bae1dSRodney W. Grimes selwakeup(sip) 119485f190e4SAlfred Perlstein struct selinfo *sip; 1195df8bae1dSRodney W. Grimes { 1196b40ce416SJulian Elischer struct thread *td; 1197df8bae1dSRodney W. Grimes 119885f190e4SAlfred Perlstein mtx_lock(&sellock); 119985f190e4SAlfred Perlstein td = sip->si_thread; 120085f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1201df8bae1dSRodney W. Grimes nselcoll++; 1202df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1203265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1204df8bae1dSRodney W. Grimes } 120585f190e4SAlfred Perlstein if (td == NULL) { 120685f190e4SAlfred Perlstein mtx_unlock(&sellock); 1207b40ce416SJulian Elischer return; 1208b40ce416SJulian Elischer } 120985f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 121085f190e4SAlfred Perlstein sip->si_thread = NULL; 12119ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1212b40ce416SJulian Elischer if (td->td_wchan == (caddr_t)&selwait) { 1213b40ce416SJulian Elischer if (td->td_proc->p_stat == SSLEEP) 1214b40ce416SJulian Elischer setrunnable(td); 1215df8bae1dSRodney W. Grimes else 1216b40ce416SJulian Elischer cv_waitq_remove(td); 121733a9ed9dSJohn Baldwin } else 1218b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 121933a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 122085f190e4SAlfred Perlstein mtx_unlock(&sellock); 1221df8bae1dSRodney W. Grimes } 1222265fc98fSSeigo Tanimura 12234d77a549SAlfred Perlstein static void selectinit(void *); 1224265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1225265fc98fSSeigo Tanimura 1226265fc98fSSeigo Tanimura /* ARGSUSED*/ 1227265fc98fSSeigo Tanimura static void 1228265fc98fSSeigo Tanimura selectinit(dummy) 1229265fc98fSSeigo Tanimura void *dummy; 1230265fc98fSSeigo Tanimura { 1231265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 12326008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1233265fc98fSSeigo Tanimura } 1234