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> 608f19eb88SIan Dowse #include <sys/syscallsubr.h> 618cb96f20SPeter Wemm #include <sys/sysctl.h> 6242d11757SPeter Wemm #include <sys/sysent.h> 63279d7226SMatthew Dillon #include <sys/bio.h> 64279d7226SMatthew Dillon #include <sys/buf.h> 65265fc98fSSeigo Tanimura #include <sys/condvar.h> 66df8bae1dSRodney W. Grimes #ifdef KTRACE 67df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 68df8bae1dSRodney W. Grimes #endif 69279d7226SMatthew Dillon #include <vm/vm.h> 70279d7226SMatthew Dillon #include <vm/vm_page.h> 71df8bae1dSRodney W. Grimes 72069e9bc1SDoug Rabson #include <machine/limits.h> 73069e9bc1SDoug Rabson 74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 76a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7755166637SPoul-Henning Kamp 78bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 79bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 80bbbb04ceSAlfred Perlstein static int dofileread(struct thread *, struct file *, int, void *, 81bbbb04ceSAlfred Perlstein size_t, off_t, int); 82bbbb04ceSAlfred Perlstein static int dofilewrite(struct thread *, struct file *, int, 83bbbb04ceSAlfred Perlstein const void *, size_t, off_t, int); 848fe387abSDmitrij Tejblum 85df8bae1dSRodney W. Grimes /* 86df8bae1dSRodney W. Grimes * Read system call. 87df8bae1dSRodney W. Grimes */ 88d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 89df8bae1dSRodney W. Grimes struct read_args { 90df8bae1dSRodney W. Grimes int fd; 91134e06feSBruce Evans void *buf; 92134e06feSBruce Evans size_t nbyte; 93df8bae1dSRodney W. Grimes }; 94d2d3e875SBruce Evans #endif 95ad2edad9SMatthew Dillon /* 96ad2edad9SMatthew Dillon * MPSAFE 97ad2edad9SMatthew Dillon */ 9826f9a767SRodney W. Grimes int 99b40ce416SJulian Elischer read(td, uap) 100b40ce416SJulian Elischer struct thread *td; 101b064d43dSMatthew Dillon struct read_args *uap; 102df8bae1dSRodney W. Grimes { 103b064d43dSMatthew Dillon struct file *fp; 104279d7226SMatthew Dillon int error; 105df8bae1dSRodney W. Grimes 106b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) == 0) { 107b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, 108ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 109b40ce416SJulian Elischer fdrop(fp, td); 110ad2edad9SMatthew Dillon } 111279d7226SMatthew Dillon return(error); 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 114df8bae1dSRodney W. Grimes /* 1158fe387abSDmitrij Tejblum * Pread system call 1164160ccd9SAlan Cox */ 1174160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1184160ccd9SAlan Cox struct pread_args { 1194160ccd9SAlan Cox int fd; 1204160ccd9SAlan Cox void *buf; 1214160ccd9SAlan Cox size_t nbyte; 1228fe387abSDmitrij Tejblum int pad; 1234160ccd9SAlan Cox off_t offset; 1244160ccd9SAlan Cox }; 1254160ccd9SAlan Cox #endif 126ad2edad9SMatthew Dillon /* 127ad2edad9SMatthew Dillon * MPSAFE 128ad2edad9SMatthew Dillon */ 1294160ccd9SAlan Cox int 130b40ce416SJulian Elischer pread(td, uap) 131b40ce416SJulian Elischer struct thread *td; 132b064d43dSMatthew Dillon struct pread_args *uap; 1334160ccd9SAlan Cox { 134b064d43dSMatthew Dillon struct file *fp; 135279d7226SMatthew Dillon int error; 1368fe387abSDmitrij Tejblum 13797fa4397SAlfred Perlstein if ((error = fget_read(td, uap->fd, &fp)) != 0) 13897fa4397SAlfred Perlstein return (error); 139426da3bcSAlfred Perlstein if (fp->f_type != DTYPE_VNODE) { 140b064d43dSMatthew Dillon error = ESPIPE; 141426da3bcSAlfred Perlstein } else { 142426da3bcSAlfred Perlstein error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte, 143426da3bcSAlfred Perlstein uap->offset, FOF_OFFSET); 144b064d43dSMatthew Dillon } 145b40ce416SJulian Elischer fdrop(fp, td); 146279d7226SMatthew Dillon return(error); 1478fe387abSDmitrij Tejblum } 1488fe387abSDmitrij Tejblum 1498fe387abSDmitrij Tejblum /* 1508fe387abSDmitrij Tejblum * Code common for read and pread 1518fe387abSDmitrij Tejblum */ 15237c84183SPoul-Henning Kamp static int 153b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 154b40ce416SJulian Elischer struct thread *td; 1558fe387abSDmitrij Tejblum struct file *fp; 1568fe387abSDmitrij Tejblum int fd, flags; 1578fe387abSDmitrij Tejblum void *buf; 1588fe387abSDmitrij Tejblum size_t nbyte; 1598fe387abSDmitrij Tejblum off_t offset; 1608fe387abSDmitrij Tejblum { 1614160ccd9SAlan Cox struct uio auio; 1624160ccd9SAlan Cox struct iovec aiov; 1634160ccd9SAlan Cox long cnt, error = 0; 1644160ccd9SAlan Cox #ifdef KTRACE 1654160ccd9SAlan Cox struct iovec ktriov; 16642ebfbf2SBrian Feldman struct uio ktruio; 1673c89e357SBrian Feldman int didktr = 0; 1684160ccd9SAlan Cox #endif 1694160ccd9SAlan Cox 1700a3e28cfSAlfred Perlstein aiov.iov_base = buf; 1718fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 1724160ccd9SAlan Cox auio.uio_iov = &aiov; 1734160ccd9SAlan Cox auio.uio_iovcnt = 1; 1748fe387abSDmitrij Tejblum auio.uio_offset = offset; 1758fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 1764160ccd9SAlan Cox return (EINVAL); 1778fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 1784160ccd9SAlan Cox auio.uio_rw = UIO_READ; 1794160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 180b40ce416SJulian Elischer auio.uio_td = td; 1814160ccd9SAlan Cox #ifdef KTRACE 1824160ccd9SAlan Cox /* 1834160ccd9SAlan Cox * if tracing, save a copy of iovec 1844160ccd9SAlan Cox */ 18560a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 1864160ccd9SAlan Cox ktriov = aiov; 18742ebfbf2SBrian Feldman ktruio = auio; 1883c89e357SBrian Feldman didktr = 1; 18942ebfbf2SBrian Feldman } 1904160ccd9SAlan Cox #endif 1918fe387abSDmitrij Tejblum cnt = nbyte; 192279d7226SMatthew Dillon 1939ca43589SRobert Watson if ((error = fo_read(fp, &auio, td->td_ucred, flags, td))) { 1944160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 1954160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 1964160ccd9SAlan Cox error = 0; 197279d7226SMatthew Dillon } 1984160ccd9SAlan Cox cnt -= auio.uio_resid; 1994160ccd9SAlan Cox #ifdef KTRACE 2003c89e357SBrian Feldman if (didktr && error == 0) { 20142ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 20242ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 20360a9bb19SJohn Baldwin ktrgenio(fd, UIO_READ, &ktruio, error); 20442ebfbf2SBrian Feldman } 2054160ccd9SAlan Cox #endif 206b40ce416SJulian Elischer td->td_retval[0] = cnt; 2074160ccd9SAlan Cox return (error); 2084160ccd9SAlan Cox } 2094160ccd9SAlan Cox 2104160ccd9SAlan Cox /* 211df8bae1dSRodney W. Grimes * Scatter read system call. 212df8bae1dSRodney W. Grimes */ 213d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 214df8bae1dSRodney W. Grimes struct readv_args { 2157147b19dSBruce Evans int fd; 216df8bae1dSRodney W. Grimes struct iovec *iovp; 217df8bae1dSRodney W. Grimes u_int iovcnt; 218df8bae1dSRodney W. Grimes }; 219d2d3e875SBruce Evans #endif 220ad2edad9SMatthew Dillon /* 221ad2edad9SMatthew Dillon * MPSAFE 222ad2edad9SMatthew Dillon */ 22326f9a767SRodney W. Grimes int 224b40ce416SJulian Elischer readv(td, uap) 225b40ce416SJulian Elischer struct thread *td; 226b064d43dSMatthew Dillon struct readv_args *uap; 227df8bae1dSRodney W. Grimes { 228b064d43dSMatthew Dillon struct file *fp; 229df8bae1dSRodney W. Grimes struct uio auio; 230b064d43dSMatthew Dillon struct iovec *iov; 231df8bae1dSRodney W. Grimes struct iovec *needfree; 232df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 23382641acdSAlan Cox long i, cnt; 23482641acdSAlan Cox int error; 235df8bae1dSRodney W. Grimes u_int iovlen; 236df8bae1dSRodney W. Grimes #ifdef KTRACE 237df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 23842ebfbf2SBrian Feldman struct uio ktruio; 239df8bae1dSRodney W. Grimes #endif 240df8bae1dSRodney W. Grimes 241b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) != 0) 24282641acdSAlan Cox return (error); 24382641acdSAlan Cox needfree = NULL; 244df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 245df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 246df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 247ad2edad9SMatthew Dillon if (uap->iovcnt > UIO_MAXIOV) { 248ad2edad9SMatthew Dillon error = EINVAL; 24982641acdSAlan Cox goto done; 250ad2edad9SMatthew Dillon } 251a163d034SWarner Losh MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 252df8bae1dSRodney W. Grimes needfree = iov; 25382641acdSAlan Cox } else 254df8bae1dSRodney W. Grimes iov = aiov; 255df8bae1dSRodney W. Grimes auio.uio_iov = iov; 256df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 257df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 258df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 259b40ce416SJulian Elischer auio.uio_td = td; 2602c1011f7SJohn Dyson auio.uio_offset = -1; 2610a3e28cfSAlfred Perlstein if ((error = copyin(uap->iovp, iov, iovlen))) 262df8bae1dSRodney W. Grimes goto done; 263df8bae1dSRodney W. Grimes auio.uio_resid = 0; 264df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 265069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 266df8bae1dSRodney W. Grimes error = EINVAL; 267df8bae1dSRodney W. Grimes goto done; 268df8bae1dSRodney W. Grimes } 269069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 270df8bae1dSRodney W. Grimes iov++; 271df8bae1dSRodney W. Grimes } 272df8bae1dSRodney W. Grimes #ifdef KTRACE 273df8bae1dSRodney W. Grimes /* 274df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 275df8bae1dSRodney W. Grimes */ 27660a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 277a163d034SWarner Losh MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 2780a3e28cfSAlfred Perlstein bcopy(auio.uio_iov, ktriov, iovlen); 27942ebfbf2SBrian Feldman ktruio = auio; 280df8bae1dSRodney W. Grimes } 281df8bae1dSRodney W. Grimes #endif 282df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 2839ca43589SRobert Watson if ((error = fo_read(fp, &auio, td->td_ucred, 0, td))) { 284df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 285df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 286df8bae1dSRodney W. Grimes error = 0; 287279d7226SMatthew Dillon } 288df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 289df8bae1dSRodney W. Grimes #ifdef KTRACE 290df8bae1dSRodney W. Grimes if (ktriov != NULL) { 29142ebfbf2SBrian Feldman if (error == 0) { 29242ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 29342ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 29460a9bb19SJohn Baldwin ktrgenio(uap->fd, UIO_READ, &ktruio, error); 29542ebfbf2SBrian Feldman } 296df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes #endif 299b40ce416SJulian Elischer td->td_retval[0] = cnt; 300df8bae1dSRodney W. Grimes done: 301b40ce416SJulian Elischer fdrop(fp, td); 302df8bae1dSRodney W. Grimes if (needfree) 303df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 304df8bae1dSRodney W. Grimes return (error); 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes /* 308df8bae1dSRodney W. Grimes * Write system call 309df8bae1dSRodney W. Grimes */ 310d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 311df8bae1dSRodney W. Grimes struct write_args { 312df8bae1dSRodney W. Grimes int fd; 313134e06feSBruce Evans const void *buf; 314134e06feSBruce Evans size_t nbyte; 315df8bae1dSRodney W. Grimes }; 316d2d3e875SBruce Evans #endif 317ad2edad9SMatthew Dillon /* 318ad2edad9SMatthew Dillon * MPSAFE 319ad2edad9SMatthew Dillon */ 32026f9a767SRodney W. Grimes int 321b40ce416SJulian Elischer write(td, uap) 322b40ce416SJulian Elischer struct thread *td; 323b064d43dSMatthew Dillon struct write_args *uap; 324df8bae1dSRodney W. Grimes { 325b064d43dSMatthew Dillon struct file *fp; 326279d7226SMatthew Dillon int error; 327df8bae1dSRodney W. Grimes 328b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 329b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 330ad2edad9SMatthew Dillon (off_t)-1, 0); 331b40ce416SJulian Elischer fdrop(fp, td); 332ad2edad9SMatthew Dillon } else { 333b064d43dSMatthew Dillon error = EBADF; /* XXX this can't be right */ 334ad2edad9SMatthew Dillon } 335279d7226SMatthew Dillon return(error); 336df8bae1dSRodney W. Grimes } 337df8bae1dSRodney W. Grimes 338df8bae1dSRodney W. Grimes /* 3398fe387abSDmitrij Tejblum * Pwrite system call 3404160ccd9SAlan Cox */ 3414160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3424160ccd9SAlan Cox struct pwrite_args { 3434160ccd9SAlan Cox int fd; 3444160ccd9SAlan Cox const void *buf; 3454160ccd9SAlan Cox size_t nbyte; 3468fe387abSDmitrij Tejblum int pad; 3474160ccd9SAlan Cox off_t offset; 3484160ccd9SAlan Cox }; 3494160ccd9SAlan Cox #endif 350ad2edad9SMatthew Dillon /* 351ad2edad9SMatthew Dillon * MPSAFE 352ad2edad9SMatthew Dillon */ 3534160ccd9SAlan Cox int 354b40ce416SJulian Elischer pwrite(td, uap) 355b40ce416SJulian Elischer struct thread *td; 356b064d43dSMatthew Dillon struct pwrite_args *uap; 3574160ccd9SAlan Cox { 358b064d43dSMatthew Dillon struct file *fp; 359279d7226SMatthew Dillon int error; 3608fe387abSDmitrij Tejblum 361b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 362aa11a498SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) { 363b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 364b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 365b064d43dSMatthew Dillon } else { 366279d7226SMatthew Dillon error = ESPIPE; 367b064d43dSMatthew Dillon } 368b40ce416SJulian Elischer fdrop(fp, td); 369279d7226SMatthew Dillon } else { 370b064d43dSMatthew Dillon error = EBADF; /* this can't be right */ 371ad2edad9SMatthew Dillon } 372279d7226SMatthew Dillon return(error); 3738fe387abSDmitrij Tejblum } 3748fe387abSDmitrij Tejblum 3758fe387abSDmitrij Tejblum static int 376b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 377b40ce416SJulian Elischer struct thread *td; 3788fe387abSDmitrij Tejblum struct file *fp; 3798fe387abSDmitrij Tejblum int fd, flags; 3808fe387abSDmitrij Tejblum const void *buf; 3818fe387abSDmitrij Tejblum size_t nbyte; 3828fe387abSDmitrij Tejblum off_t offset; 3838fe387abSDmitrij Tejblum { 3844160ccd9SAlan Cox struct uio auio; 3854160ccd9SAlan Cox struct iovec aiov; 3864160ccd9SAlan Cox long cnt, error = 0; 3874160ccd9SAlan Cox #ifdef KTRACE 3884160ccd9SAlan Cox struct iovec ktriov; 38942ebfbf2SBrian Feldman struct uio ktruio; 3903c89e357SBrian Feldman int didktr = 0; 3914160ccd9SAlan Cox #endif 3924160ccd9SAlan Cox 393b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 3948fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 3954160ccd9SAlan Cox auio.uio_iov = &aiov; 3964160ccd9SAlan Cox auio.uio_iovcnt = 1; 3978fe387abSDmitrij Tejblum auio.uio_offset = offset; 3988fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 3994160ccd9SAlan Cox return (EINVAL); 4008fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 4014160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 4024160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 403b40ce416SJulian Elischer auio.uio_td = td; 4044160ccd9SAlan Cox #ifdef KTRACE 4054160ccd9SAlan Cox /* 40642ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4074160ccd9SAlan Cox */ 40860a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 4094160ccd9SAlan Cox ktriov = aiov; 41042ebfbf2SBrian Feldman ktruio = auio; 4113c89e357SBrian Feldman didktr = 1; 41242ebfbf2SBrian Feldman } 4134160ccd9SAlan Cox #endif 4148fe387abSDmitrij Tejblum cnt = nbyte; 415c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 416279d7226SMatthew Dillon bwillwrite(); 4179ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, flags, td))) { 4184160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4194160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4204160ccd9SAlan Cox error = 0; 421c33c8251SAlfred Perlstein /* Socket layer is responsible for issuing SIGPIPE. */ 422c33c8251SAlfred Perlstein if (error == EPIPE && fp->f_type != DTYPE_SOCKET) { 423b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 424b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 425b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 42619eb87d2SJohn Baldwin } 4274160ccd9SAlan Cox } 4284160ccd9SAlan Cox cnt -= auio.uio_resid; 4294160ccd9SAlan Cox #ifdef KTRACE 4303c89e357SBrian Feldman if (didktr && error == 0) { 43142ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 43242ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 43360a9bb19SJohn Baldwin ktrgenio(fd, UIO_WRITE, &ktruio, error); 43442ebfbf2SBrian Feldman } 4354160ccd9SAlan Cox #endif 436b40ce416SJulian Elischer td->td_retval[0] = cnt; 4374160ccd9SAlan Cox return (error); 4384160ccd9SAlan Cox } 4394160ccd9SAlan Cox 4404160ccd9SAlan Cox /* 441df8bae1dSRodney W. Grimes * Gather write system call 442df8bae1dSRodney W. Grimes */ 443d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 444df8bae1dSRodney W. Grimes struct writev_args { 445df8bae1dSRodney W. Grimes int fd; 446df8bae1dSRodney W. Grimes struct iovec *iovp; 447df8bae1dSRodney W. Grimes u_int iovcnt; 448df8bae1dSRodney W. Grimes }; 449d2d3e875SBruce Evans #endif 450ad2edad9SMatthew Dillon /* 451ad2edad9SMatthew Dillon * MPSAFE 452ad2edad9SMatthew Dillon */ 45326f9a767SRodney W. Grimes int 454b40ce416SJulian Elischer writev(td, uap) 455b40ce416SJulian Elischer struct thread *td; 456df8bae1dSRodney W. Grimes register struct writev_args *uap; 457df8bae1dSRodney W. Grimes { 458b064d43dSMatthew Dillon struct file *fp; 459df8bae1dSRodney W. Grimes struct uio auio; 460df8bae1dSRodney W. Grimes register struct iovec *iov; 461df8bae1dSRodney W. Grimes struct iovec *needfree; 462df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 463df8bae1dSRodney W. Grimes long i, cnt, error = 0; 464df8bae1dSRodney W. Grimes u_int iovlen; 465df8bae1dSRodney W. Grimes #ifdef KTRACE 466df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 46742ebfbf2SBrian Feldman struct uio ktruio; 468df8bae1dSRodney W. Grimes #endif 469df8bae1dSRodney W. Grimes 470ad2edad9SMatthew Dillon mtx_lock(&Giant); 471b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) != 0) { 472ad2edad9SMatthew Dillon error = EBADF; 473ad2edad9SMatthew Dillon goto done2; 474ad2edad9SMatthew Dillon } 475df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 476df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 477df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 4781aa3e7ddSBrian Feldman if (uap->iovcnt > UIO_MAXIOV) { 4791aa3e7ddSBrian Feldman needfree = NULL; 4801aa3e7ddSBrian Feldman error = EINVAL; 4811aa3e7ddSBrian Feldman goto done; 4821aa3e7ddSBrian Feldman } 483a163d034SWarner Losh MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 484df8bae1dSRodney W. Grimes needfree = iov; 485df8bae1dSRodney W. Grimes } else { 486df8bae1dSRodney W. Grimes iov = aiov; 487df8bae1dSRodney W. Grimes needfree = NULL; 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes auio.uio_iov = iov; 490df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 491df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 492df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 493b40ce416SJulian Elischer auio.uio_td = td; 4942c1011f7SJohn Dyson auio.uio_offset = -1; 4950a3e28cfSAlfred Perlstein if ((error = copyin(uap->iovp, iov, iovlen))) 496df8bae1dSRodney W. Grimes goto done; 497df8bae1dSRodney W. Grimes auio.uio_resid = 0; 498df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 499069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 500df8bae1dSRodney W. Grimes error = EINVAL; 501df8bae1dSRodney W. Grimes goto done; 502df8bae1dSRodney W. Grimes } 503069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 504df8bae1dSRodney W. Grimes iov++; 505df8bae1dSRodney W. Grimes } 506df8bae1dSRodney W. Grimes #ifdef KTRACE 507df8bae1dSRodney W. Grimes /* 50842ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 509df8bae1dSRodney W. Grimes */ 51060a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 511a163d034SWarner Losh MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 5120a3e28cfSAlfred Perlstein bcopy(auio.uio_iov, ktriov, iovlen); 51342ebfbf2SBrian Feldman ktruio = auio; 514df8bae1dSRodney W. Grimes } 515df8bae1dSRodney W. Grimes #endif 516df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 517a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5189440653dSMatthew Dillon bwillwrite(); 5199ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, 0, td))) { 520df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 521df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 522df8bae1dSRodney W. Grimes error = 0; 52319eb87d2SJohn Baldwin if (error == EPIPE) { 524b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 525b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 526b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 52719eb87d2SJohn Baldwin } 528df8bae1dSRodney W. Grimes } 529df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 530df8bae1dSRodney W. Grimes #ifdef KTRACE 531df8bae1dSRodney W. Grimes if (ktriov != NULL) { 53242ebfbf2SBrian Feldman if (error == 0) { 53342ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 53442ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 53560a9bb19SJohn Baldwin ktrgenio(uap->fd, UIO_WRITE, &ktruio, error); 53642ebfbf2SBrian Feldman } 537df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 538df8bae1dSRodney W. Grimes } 539df8bae1dSRodney W. Grimes #endif 540b40ce416SJulian Elischer td->td_retval[0] = cnt; 541df8bae1dSRodney W. Grimes done: 542b40ce416SJulian Elischer fdrop(fp, td); 543df8bae1dSRodney W. Grimes if (needfree) 544df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 545ad2edad9SMatthew Dillon done2: 546ad2edad9SMatthew Dillon mtx_unlock(&Giant); 547df8bae1dSRodney W. Grimes return (error); 548df8bae1dSRodney W. Grimes } 549df8bae1dSRodney W. Grimes 550df8bae1dSRodney W. Grimes /* 551df8bae1dSRodney W. Grimes * Ioctl system call 552df8bae1dSRodney W. Grimes */ 553d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 554df8bae1dSRodney W. Grimes struct ioctl_args { 555df8bae1dSRodney W. Grimes int fd; 556069e9bc1SDoug Rabson u_long com; 557df8bae1dSRodney W. Grimes caddr_t data; 558df8bae1dSRodney W. Grimes }; 559d2d3e875SBruce Evans #endif 560ad2edad9SMatthew Dillon /* 561ad2edad9SMatthew Dillon * MPSAFE 562ad2edad9SMatthew Dillon */ 563df8bae1dSRodney W. Grimes /* ARGSUSED */ 56426f9a767SRodney W. Grimes int 565b40ce416SJulian Elischer ioctl(td, uap) 566b40ce416SJulian Elischer struct thread *td; 567df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 568df8bae1dSRodney W. Grimes { 569a4db4953SAlfred Perlstein struct file *fp; 570df8bae1dSRodney W. Grimes register struct filedesc *fdp; 571831b9ef2SDoug Rabson register u_long com; 572ad2edad9SMatthew Dillon int error = 0; 573df8bae1dSRodney W. Grimes register u_int size; 574df8bae1dSRodney W. Grimes caddr_t data, memp; 575df8bae1dSRodney W. Grimes int tmp; 576df8bae1dSRodney W. Grimes #define STK_PARAMS 128 577d2ba455cSMatthew Dillon union { 578df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 579d2ba455cSMatthew Dillon long align; 580d2ba455cSMatthew Dillon } ubuf; 581df8bae1dSRodney W. Grimes 582a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 583a4db4953SAlfred Perlstein return (error); 584aa11a498SAlfred Perlstein mtx_lock(&Giant); 585ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 586426da3bcSAlfred Perlstein fdrop(fp, td); 587aa11a498SAlfred Perlstein mtx_unlock(&Giant); 588426da3bcSAlfred Perlstein return (EBADF); 589ad2edad9SMatthew Dillon } 590426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 591df8bae1dSRodney W. Grimes switch (com = uap->com) { 592df8bae1dSRodney W. Grimes case FIONCLEX: 593426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 594df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 595426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 596426da3bcSAlfred Perlstein fdrop(fp, td); 597aa11a498SAlfred Perlstein mtx_unlock(&Giant); 598426da3bcSAlfred Perlstein return (0); 599df8bae1dSRodney W. Grimes case FIOCLEX: 600426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 601df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 602426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 603426da3bcSAlfred Perlstein fdrop(fp, td); 604aa11a498SAlfred Perlstein mtx_unlock(&Giant); 605426da3bcSAlfred Perlstein return (0); 606df8bae1dSRodney W. Grimes } 607df8bae1dSRodney W. Grimes 608df8bae1dSRodney W. Grimes /* 609df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 610df8bae1dSRodney W. Grimes * copied to/from the user's address space. 611df8bae1dSRodney W. Grimes */ 612df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 613ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 614426da3bcSAlfred Perlstein fdrop(fp, td); 615aa11a498SAlfred Perlstein mtx_unlock(&Giant); 616426da3bcSAlfred Perlstein return (ENOTTY); 617ad2edad9SMatthew Dillon } 618279d7226SMatthew Dillon 619df8bae1dSRodney W. Grimes memp = NULL; 620d2ba455cSMatthew Dillon if (size > sizeof (ubuf.stkbuf)) { 621a163d034SWarner Losh memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 622df8bae1dSRodney W. Grimes data = memp; 623279d7226SMatthew Dillon } else { 624d2ba455cSMatthew Dillon data = ubuf.stkbuf; 625279d7226SMatthew Dillon } 626df8bae1dSRodney W. Grimes if (com&IOC_IN) { 627df8bae1dSRodney W. Grimes if (size) { 628df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 629df8bae1dSRodney W. Grimes if (error) { 630df8bae1dSRodney W. Grimes if (memp) 631df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 632b40ce416SJulian Elischer fdrop(fp, td); 633426da3bcSAlfred Perlstein goto done; 634df8bae1dSRodney W. Grimes } 635279d7226SMatthew Dillon } else { 636df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 637279d7226SMatthew Dillon } 638279d7226SMatthew Dillon } else if ((com&IOC_OUT) && size) { 639df8bae1dSRodney W. Grimes /* 640df8bae1dSRodney W. Grimes * Zero the buffer so the user always 641df8bae1dSRodney W. Grimes * gets back something deterministic. 642df8bae1dSRodney W. Grimes */ 643df8bae1dSRodney W. Grimes bzero(data, size); 644279d7226SMatthew Dillon } else if (com&IOC_VOID) { 645df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 646279d7226SMatthew Dillon } 647df8bae1dSRodney W. Grimes 648df8bae1dSRodney W. Grimes switch (com) { 649df8bae1dSRodney W. Grimes 650df8bae1dSRodney W. Grimes case FIONBIO: 651426da3bcSAlfred Perlstein FILE_LOCK(fp); 652bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 653df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 654df8bae1dSRodney W. Grimes else 655df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 656426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 657d49fa1caSRobert Watson error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); 658df8bae1dSRodney W. Grimes break; 659df8bae1dSRodney W. Grimes 660df8bae1dSRodney W. Grimes case FIOASYNC: 661426da3bcSAlfred Perlstein FILE_LOCK(fp); 662bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 663df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 664df8bae1dSRodney W. Grimes else 665df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 666426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 667d49fa1caSRobert Watson error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); 668df8bae1dSRodney W. Grimes break; 669df8bae1dSRodney W. Grimes 670df8bae1dSRodney W. Grimes default: 671d49fa1caSRobert Watson error = fo_ioctl(fp, com, data, td->td_ucred, td); 672df8bae1dSRodney W. Grimes /* 673df8bae1dSRodney W. Grimes * Copy any data to user, size was 674df8bae1dSRodney W. Grimes * already set and checked above. 675df8bae1dSRodney W. Grimes */ 676df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 677df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 678df8bae1dSRodney W. Grimes break; 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes if (memp) 681df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 682b40ce416SJulian Elischer fdrop(fp, td); 683426da3bcSAlfred Perlstein done: 684ad2edad9SMatthew Dillon mtx_unlock(&Giant); 685df8bae1dSRodney W. Grimes return (error); 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes 68885f190e4SAlfred Perlstein /* 68985f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 69085f190e4SAlfred Perlstein */ 69185f190e4SAlfred Perlstein struct mtx sellock; 692265fc98fSSeigo Tanimura struct cv selwait; 6939ae6d334SKelly Yancey u_int nselcoll; /* Select collisions since boot */ 6949ae6d334SKelly Yancey SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 695df8bae1dSRodney W. Grimes 696df8bae1dSRodney W. Grimes /* 697df8bae1dSRodney W. Grimes * Select system call. 698df8bae1dSRodney W. Grimes */ 699d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 700df8bae1dSRodney W. Grimes struct select_args { 701b08f7993SSujal Patel int nd; 702df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 703df8bae1dSRodney W. Grimes struct timeval *tv; 704df8bae1dSRodney W. Grimes }; 705d2d3e875SBruce Evans #endif 706ad2edad9SMatthew Dillon /* 707ad2edad9SMatthew Dillon * MPSAFE 708ad2edad9SMatthew Dillon */ 70926f9a767SRodney W. Grimes int 710b40ce416SJulian Elischer select(td, uap) 711b40ce416SJulian Elischer register struct thread *td; 712df8bae1dSRodney W. Grimes register struct select_args *uap; 713df8bae1dSRodney W. Grimes { 7148f19eb88SIan Dowse struct timeval tv, *tvp; 7158f19eb88SIan Dowse int error; 7168f19eb88SIan Dowse 7178f19eb88SIan Dowse if (uap->tv != NULL) { 7188f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 7198f19eb88SIan Dowse if (error) 7208f19eb88SIan Dowse return (error); 7218f19eb88SIan Dowse tvp = &tv; 7228f19eb88SIan Dowse } else 7238f19eb88SIan Dowse tvp = NULL; 7248f19eb88SIan Dowse 7258f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 7268f19eb88SIan Dowse } 7278f19eb88SIan Dowse 7288f19eb88SIan Dowse int 7298f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 7308f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 7318f19eb88SIan Dowse { 732426da3bcSAlfred Perlstein struct filedesc *fdp; 733d5e4d7e1SBruce Evans /* 734d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 735d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 736d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 737d5e4d7e1SBruce Evans * of 256. 738d5e4d7e1SBruce Evans */ 739d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 740eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 74100af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7429ae6d334SKelly Yancey int error, timo; 7439ae6d334SKelly Yancey u_int ncoll, nbufbytes, ncpbytes, nfdbits; 744df8bae1dSRodney W. Grimes 7458f19eb88SIan Dowse if (nd < 0) 746acbfbfeaSSujal Patel return (EINVAL); 747426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 748ad2edad9SMatthew Dillon mtx_lock(&Giant); 749426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 750ad2edad9SMatthew Dillon 7518f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 7528f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 753426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 754b08f7993SSujal Patel 755d5e4d7e1SBruce Evans /* 756d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 757d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 758d5e4d7e1SBruce Evans */ 7598f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 760d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 761d5e4d7e1SBruce Evans nbufbytes = 0; 7628f19eb88SIan Dowse if (fd_in != NULL) 763d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7648f19eb88SIan Dowse if (fd_ou != NULL) 765d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7668f19eb88SIan Dowse if (fd_ex != NULL) 767d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 768d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 769d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 770d5e4d7e1SBruce Evans else 771a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 772b08f7993SSujal Patel 773b08f7993SSujal Patel /* 774d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 775d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 776d5e4d7e1SBruce Evans * together. 777b08f7993SSujal Patel */ 778d5e4d7e1SBruce Evans sbp = selbits; 779df8bae1dSRodney W. Grimes #define getbits(name, x) \ 780d5e4d7e1SBruce Evans do { \ 7818f19eb88SIan Dowse if (name == NULL) \ 782d5e4d7e1SBruce Evans ibits[x] = NULL; \ 783d5e4d7e1SBruce Evans else { \ 784d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 785d5e4d7e1SBruce Evans obits[x] = sbp; \ 786d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 7878f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 788265fc98fSSeigo Tanimura if (error != 0) \ 78985f190e4SAlfred Perlstein goto done_nosellock; \ 790e04ac2feSJohn Baldwin } \ 791d5e4d7e1SBruce Evans } while (0) 7928f19eb88SIan Dowse getbits(fd_in, 0); 7938f19eb88SIan Dowse getbits(fd_ou, 1); 7948f19eb88SIan Dowse getbits(fd_ex, 2); 795df8bae1dSRodney W. Grimes #undef getbits 796d5e4d7e1SBruce Evans if (nbufbytes != 0) 797d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 798df8bae1dSRodney W. Grimes 7998f19eb88SIan Dowse if (tvp != NULL) { 8008f19eb88SIan Dowse atv = *tvp; 801df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 802df8bae1dSRodney W. Grimes error = EINVAL; 80385f190e4SAlfred Perlstein goto done_nosellock; 804df8bae1dSRodney W. Grimes } 805c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 80600af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8079c386f6bSJohn Baldwin } else { 80800af9731SPoul-Henning Kamp atv.tv_sec = 0; 8099c386f6bSJohn Baldwin atv.tv_usec = 0; 8109c386f6bSJohn Baldwin } 81100af9731SPoul-Henning Kamp timo = 0; 8122149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 81385f190e4SAlfred Perlstein mtx_lock(&sellock); 814df8bae1dSRodney W. Grimes retry: 815df8bae1dSRodney W. Grimes ncoll = nselcoll; 816fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 817b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 818fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 81985f190e4SAlfred Perlstein mtx_unlock(&sellock); 82085f190e4SAlfred Perlstein 8218f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 82285f190e4SAlfred Perlstein mtx_lock(&sellock); 823b40ce416SJulian Elischer if (error || td->td_retval[0]) 824df8bae1dSRodney W. Grimes goto done; 8254da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 826c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 82785f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 828df8bae1dSRodney W. Grimes goto done; 82900af9731SPoul-Henning Kamp ttv = atv; 83000af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 83100af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 83200af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 833df8bae1dSRodney W. Grimes } 83485f190e4SAlfred Perlstein 83585f190e4SAlfred Perlstein /* 83685f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 83785f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 83885f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 83985f190e4SAlfred Perlstein * necessary. 84085f190e4SAlfred Perlstein */ 841fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 84285f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 84385f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 84485f190e4SAlfred Perlstein goto retry; 84585f190e4SAlfred Perlstein } 846fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 847bfbbc4aaSJason Evans 848265fc98fSSeigo Tanimura if (timo > 0) 84985f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 850265fc98fSSeigo Tanimura else 85185f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 852bfbbc4aaSJason Evans 853df8bae1dSRodney W. Grimes if (error == 0) 854df8bae1dSRodney W. Grimes goto retry; 855265fc98fSSeigo Tanimura 856df8bae1dSRodney W. Grimes done: 85785f190e4SAlfred Perlstein clear_selinfo_list(td); 858fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 859b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 860fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 86185f190e4SAlfred Perlstein mtx_unlock(&sellock); 86285f190e4SAlfred Perlstein 86385f190e4SAlfred Perlstein done_nosellock: 864df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 865df8bae1dSRodney W. Grimes if (error == ERESTART) 866df8bae1dSRodney W. Grimes error = EINTR; 867df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 868df8bae1dSRodney W. Grimes error = 0; 869df8bae1dSRodney W. Grimes #define putbits(name, x) \ 8708f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 871df8bae1dSRodney W. Grimes error = error2; 872df8bae1dSRodney W. Grimes if (error == 0) { 873df8bae1dSRodney W. Grimes int error2; 874df8bae1dSRodney W. Grimes 8758f19eb88SIan Dowse putbits(fd_in, 0); 8768f19eb88SIan Dowse putbits(fd_ou, 1); 8778f19eb88SIan Dowse putbits(fd_ex, 2); 878df8bae1dSRodney W. Grimes #undef putbits 879df8bae1dSRodney W. Grimes } 880d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 881d5e4d7e1SBruce Evans free(selbits, M_SELECT); 882ad2edad9SMatthew Dillon 883ad2edad9SMatthew Dillon mtx_unlock(&Giant); 884df8bae1dSRodney W. Grimes return (error); 885df8bae1dSRodney W. Grimes } 886df8bae1dSRodney W. Grimes 887265fc98fSSeigo Tanimura static int 888b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 889b40ce416SJulian Elischer struct thread *td; 890b08f7993SSujal Patel fd_mask **ibits, **obits; 891cb226aaaSPoul-Henning Kamp int nfd; 892df8bae1dSRodney W. Grimes { 893f082218cSPeter Wemm int msk, i, fd; 894f082218cSPeter Wemm fd_mask bits; 895df8bae1dSRodney W. Grimes struct file *fp; 896df8bae1dSRodney W. Grimes int n = 0; 8972087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 89842d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 899eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 900df8bae1dSRodney W. Grimes 901eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 902df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 903d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 904d5e4d7e1SBruce Evans continue; 905df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 906b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 907f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 908f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 909f082218cSPeter Wemm if (!(bits & 1)) 910f082218cSPeter Wemm continue; 911eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 912eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 913df8bae1dSRodney W. Grimes return (EBADF); 914eb209311SAlfred Perlstein } 915ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 916ea6027a8SRobert Watson td)) { 917b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 918f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 919df8bae1dSRodney W. Grimes n++; 920df8bae1dSRodney W. Grimes } 921df8bae1dSRodney W. Grimes } 922df8bae1dSRodney W. Grimes } 923df8bae1dSRodney W. Grimes } 924eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 925b40ce416SJulian Elischer td->td_retval[0] = n; 926df8bae1dSRodney W. Grimes return (0); 927df8bae1dSRodney W. Grimes } 928df8bae1dSRodney W. Grimes 92942d11757SPeter Wemm /* 93042d11757SPeter Wemm * Poll system call. 93142d11757SPeter Wemm */ 93242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 93342d11757SPeter Wemm struct poll_args { 93442d11757SPeter Wemm struct pollfd *fds; 93542d11757SPeter Wemm u_int nfds; 93642d11757SPeter Wemm int timeout; 93742d11757SPeter Wemm }; 93842d11757SPeter Wemm #endif 939ad2edad9SMatthew Dillon /* 940ad2edad9SMatthew Dillon * MPSAFE 941ad2edad9SMatthew Dillon */ 94242d11757SPeter Wemm int 943b40ce416SJulian Elischer poll(td, uap) 944b40ce416SJulian Elischer struct thread *td; 945ea0237edSJonathan Lemon struct poll_args *uap; 94642d11757SPeter Wemm { 94742d11757SPeter Wemm caddr_t bits; 94842d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 94900af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 9509ae6d334SKelly Yancey int error = 0, timo; 9519ae6d334SKelly Yancey u_int ncoll, nfds; 95242d11757SPeter Wemm size_t ni; 95342d11757SPeter Wemm 954d1e405c5SAlfred Perlstein nfds = uap->nfds; 955ad2edad9SMatthew Dillon 956ad2edad9SMatthew Dillon mtx_lock(&Giant); 95789b71647SPeter Wemm /* 9582bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9592bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9602bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9612bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9622bd5ac33SPeter Wemm * safe, but not overly restrictive. 96389b71647SPeter Wemm */ 964b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 965b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 966ad2edad9SMatthew Dillon error = EINVAL; 967ad2edad9SMatthew Dillon goto done2; 968ad2edad9SMatthew Dillon } 96989b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 97042d11757SPeter Wemm if (ni > sizeof(smallbits)) 971a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 97242d11757SPeter Wemm else 97342d11757SPeter Wemm bits = smallbits; 974d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 97542d11757SPeter Wemm if (error) 97685f190e4SAlfred Perlstein goto done_nosellock; 977d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 978d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 979d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 98042d11757SPeter Wemm if (itimerfix(&atv)) { 98142d11757SPeter Wemm error = EINVAL; 98285f190e4SAlfred Perlstein goto done_nosellock; 98342d11757SPeter Wemm } 984c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 98500af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9869c386f6bSJohn Baldwin } else { 98700af9731SPoul-Henning Kamp atv.tv_sec = 0; 9889c386f6bSJohn Baldwin atv.tv_usec = 0; 9899c386f6bSJohn Baldwin } 99000af9731SPoul-Henning Kamp timo = 0; 9912149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 99285f190e4SAlfred Perlstein mtx_lock(&sellock); 99342d11757SPeter Wemm retry: 99442d11757SPeter Wemm ncoll = nselcoll; 995fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 996b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 997fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 99885f190e4SAlfred Perlstein mtx_unlock(&sellock); 99985f190e4SAlfred Perlstein 1000b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 100185f190e4SAlfred Perlstein mtx_lock(&sellock); 1002b40ce416SJulian Elischer if (error || td->td_retval[0]) 100342d11757SPeter Wemm goto done; 10044da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1005c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 100685f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 100742d11757SPeter Wemm goto done; 100800af9731SPoul-Henning Kamp ttv = atv; 100900af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 101000af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 101100af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 101242d11757SPeter Wemm } 101385f190e4SAlfred Perlstein /* 101485f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 101585f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 101685f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 101785f190e4SAlfred Perlstein */ 1018fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 101985f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 1020fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 102185f190e4SAlfred Perlstein goto retry; 102285f190e4SAlfred Perlstein } 102385f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 102485f190e4SAlfred Perlstein 1025265fc98fSSeigo Tanimura if (timo > 0) 102685f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 1027265fc98fSSeigo Tanimura else 102885f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 102985f190e4SAlfred Perlstein 103042d11757SPeter Wemm if (error == 0) 103142d11757SPeter Wemm goto retry; 1032265fc98fSSeigo Tanimura 103342d11757SPeter Wemm done: 103485f190e4SAlfred Perlstein clear_selinfo_list(td); 1035fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1036b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1037fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 103885f190e4SAlfred Perlstein mtx_unlock(&sellock); 103985f190e4SAlfred Perlstein 104085f190e4SAlfred Perlstein done_nosellock: 104142d11757SPeter Wemm /* poll is not restarted after signals... */ 104242d11757SPeter Wemm if (error == ERESTART) 104342d11757SPeter Wemm error = EINTR; 104442d11757SPeter Wemm if (error == EWOULDBLOCK) 104542d11757SPeter Wemm error = 0; 104642d11757SPeter Wemm if (error == 0) { 1047d1e405c5SAlfred Perlstein error = copyout(bits, uap->fds, ni); 104842d11757SPeter Wemm if (error) 104942d11757SPeter Wemm goto out; 105042d11757SPeter Wemm } 105142d11757SPeter Wemm out: 105242d11757SPeter Wemm if (ni > sizeof(smallbits)) 105342d11757SPeter Wemm free(bits, M_TEMP); 1054ad2edad9SMatthew Dillon done2: 1055ad2edad9SMatthew Dillon mtx_unlock(&Giant); 105642d11757SPeter Wemm return (error); 105742d11757SPeter Wemm } 105842d11757SPeter Wemm 105942d11757SPeter Wemm static int 1060b40ce416SJulian Elischer pollscan(td, fds, nfd) 1061b40ce416SJulian Elischer struct thread *td; 106242d11757SPeter Wemm struct pollfd *fds; 1063ea0237edSJonathan Lemon u_int nfd; 106442d11757SPeter Wemm { 1065b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 106642d11757SPeter Wemm int i; 106742d11757SPeter Wemm struct file *fp; 106842d11757SPeter Wemm int n = 0; 106942d11757SPeter Wemm 1070426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1071eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1072337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 107342d11757SPeter Wemm fds->revents = POLLNVAL; 107442d11757SPeter Wemm n++; 1075337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1076337c9691SJordan K. Hubbard fds->revents = 0; 107742d11757SPeter Wemm } else { 107842d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1079279d7226SMatthew Dillon if (fp == NULL) { 108042d11757SPeter Wemm fds->revents = POLLNVAL; 108142d11757SPeter Wemm n++; 108242d11757SPeter Wemm } else { 10832087c896SBruce Evans /* 10842087c896SBruce Evans * Note: backend also returns POLLHUP and 10852087c896SBruce Evans * POLLERR if appropriate. 10862087c896SBruce Evans */ 108713ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1088ea6027a8SRobert Watson td->td_ucred, td); 108942d11757SPeter Wemm if (fds->revents != 0) 109042d11757SPeter Wemm n++; 109142d11757SPeter Wemm } 109242d11757SPeter Wemm } 109342d11757SPeter Wemm } 1094eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1095b40ce416SJulian Elischer td->td_retval[0] = n; 109642d11757SPeter Wemm return (0); 109742d11757SPeter Wemm } 109842d11757SPeter Wemm 109942d11757SPeter Wemm /* 110042d11757SPeter Wemm * OpenBSD poll system call. 110142d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 110242d11757SPeter Wemm */ 110342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 110442d11757SPeter Wemm struct openbsd_poll_args { 110542d11757SPeter Wemm struct pollfd *fds; 110642d11757SPeter Wemm u_int nfds; 110742d11757SPeter Wemm int timeout; 110842d11757SPeter Wemm }; 110942d11757SPeter Wemm #endif 1110ad2edad9SMatthew Dillon /* 1111ad2edad9SMatthew Dillon * MPSAFE 1112ad2edad9SMatthew Dillon */ 111342d11757SPeter Wemm int 1114b40ce416SJulian Elischer openbsd_poll(td, uap) 1115b40ce416SJulian Elischer register struct thread *td; 111642d11757SPeter Wemm register struct openbsd_poll_args *uap; 111742d11757SPeter Wemm { 1118b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 111942d11757SPeter Wemm } 112042d11757SPeter Wemm 112185f190e4SAlfred Perlstein /* 112285f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 112385f190e4SAlfred Perlstein * we were polling. 112485f190e4SAlfred Perlstein * 112585f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 112685f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 112785f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 112885f190e4SAlfred Perlstein */ 112985f190e4SAlfred Perlstein void 113085f190e4SAlfred Perlstein clear_selinfo_list(td) 113185f190e4SAlfred Perlstein struct thread *td; 113285f190e4SAlfred Perlstein { 113385f190e4SAlfred Perlstein struct selinfo *si; 113485f190e4SAlfred Perlstein 113585f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 113685f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 113785f190e4SAlfred Perlstein si->si_thread = NULL; 113885f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 113985f190e4SAlfred Perlstein } 114085f190e4SAlfred Perlstein 1141df8bae1dSRodney W. Grimes /*ARGSUSED*/ 114226f9a767SRodney W. Grimes int 1143b40ce416SJulian Elischer seltrue(dev, events, td) 1144df8bae1dSRodney W. Grimes dev_t dev; 114542d11757SPeter Wemm int events; 1146b40ce416SJulian Elischer struct thread *td; 1147df8bae1dSRodney W. Grimes { 1148df8bae1dSRodney W. Grimes 114942d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1150df8bae1dSRodney W. Grimes } 1151df8bae1dSRodney W. Grimes 1152df8bae1dSRodney W. Grimes /* 1153df8bae1dSRodney W. Grimes * Record a select request. 1154df8bae1dSRodney W. Grimes */ 1155df8bae1dSRodney W. Grimes void 1156df8bae1dSRodney W. Grimes selrecord(selector, sip) 1157b40ce416SJulian Elischer struct thread *selector; 1158df8bae1dSRodney W. Grimes struct selinfo *sip; 1159df8bae1dSRodney W. Grimes { 1160df8bae1dSRodney W. Grimes 116185f190e4SAlfred Perlstein mtx_lock(&sellock); 116285f190e4SAlfred Perlstein /* 1163b605b54cSAlfred Perlstein * If the selinfo's thread pointer is NULL then take ownership of it. 1164b605b54cSAlfred Perlstein * 1165b605b54cSAlfred Perlstein * If the thread pointer is not NULL and it points to another 1166b605b54cSAlfred Perlstein * thread, then we have a collision. 1167b605b54cSAlfred Perlstein * 1168b605b54cSAlfred Perlstein * If the thread pointer is not NULL and points back to us then leave 1169b605b54cSAlfred Perlstein * it alone as we've already added pointed it at us and added it to 1170b605b54cSAlfred Perlstein * our list. 117185f190e4SAlfred Perlstein */ 117285f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1173b40ce416SJulian Elischer sip->si_thread = selector; 117485f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 117585f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 117685f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 117785f190e4SAlfred Perlstein } 117885f190e4SAlfred Perlstein 117985f190e4SAlfred Perlstein mtx_unlock(&sellock); 1180df8bae1dSRodney W. Grimes } 1181df8bae1dSRodney W. Grimes 1182df8bae1dSRodney W. Grimes /* 1183df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1184df8bae1dSRodney W. Grimes */ 1185df8bae1dSRodney W. Grimes void 1186df8bae1dSRodney W. Grimes selwakeup(sip) 118785f190e4SAlfred Perlstein struct selinfo *sip; 1188df8bae1dSRodney W. Grimes { 1189b40ce416SJulian Elischer struct thread *td; 1190df8bae1dSRodney W. Grimes 119185f190e4SAlfred Perlstein mtx_lock(&sellock); 119285f190e4SAlfred Perlstein td = sip->si_thread; 119385f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1194df8bae1dSRodney W. Grimes nselcoll++; 1195df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1196265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1197df8bae1dSRodney W. Grimes } 119885f190e4SAlfred Perlstein if (td == NULL) { 119985f190e4SAlfred Perlstein mtx_unlock(&sellock); 1200b40ce416SJulian Elischer return; 1201b40ce416SJulian Elischer } 120285f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 120385f190e4SAlfred Perlstein sip->si_thread = NULL; 12049ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 12050a3e28cfSAlfred Perlstein if (td->td_wchan == &selwait) { 1206b40ce416SJulian Elischer cv_waitq_remove(td); 120771fad9fdSJulian Elischer TD_CLR_SLEEPING(td); 120871fad9fdSJulian Elischer setrunnable(td); 120933a9ed9dSJohn Baldwin } else 1210b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 121133a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 121285f190e4SAlfred Perlstein mtx_unlock(&sellock); 1213df8bae1dSRodney W. Grimes } 1214265fc98fSSeigo Tanimura 12154d77a549SAlfred Perlstein static void selectinit(void *); 1216265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1217265fc98fSSeigo Tanimura 1218265fc98fSSeigo Tanimura /* ARGSUSED*/ 1219265fc98fSSeigo Tanimura static void 1220265fc98fSSeigo Tanimura selectinit(dummy) 1221265fc98fSSeigo Tanimura void *dummy; 1222265fc98fSSeigo Tanimura { 1223265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 12246008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1225265fc98fSSeigo Tanimura } 1226