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> 65df8bae1dSRodney W. Grimes #ifdef KTRACE 66df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 67df8bae1dSRodney W. Grimes #endif 68279d7226SMatthew Dillon #include <vm/vm.h> 69279d7226SMatthew Dillon #include <vm/vm_page.h> 70df8bae1dSRodney W. Grimes 71069e9bc1SDoug Rabson #include <machine/limits.h> 72069e9bc1SDoug Rabson 73a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 75a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7655166637SPoul-Henning Kamp 77bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 78bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 79bbbb04ceSAlfred Perlstein static int dofileread(struct thread *, struct file *, int, void *, 80bbbb04ceSAlfred Perlstein size_t, off_t, int); 81bbbb04ceSAlfred Perlstein static int dofilewrite(struct thread *, struct file *, int, 82bbbb04ceSAlfred Perlstein const void *, size_t, off_t, int); 838fe387abSDmitrij Tejblum 84df8bae1dSRodney W. Grimes /* 85df8bae1dSRodney W. Grimes * Read system call. 86df8bae1dSRodney W. Grimes */ 87d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 88df8bae1dSRodney W. Grimes struct read_args { 89df8bae1dSRodney W. Grimes int fd; 90134e06feSBruce Evans void *buf; 91134e06feSBruce Evans size_t nbyte; 92df8bae1dSRodney W. Grimes }; 93d2d3e875SBruce Evans #endif 94ad2edad9SMatthew Dillon /* 95ad2edad9SMatthew Dillon * MPSAFE 96ad2edad9SMatthew Dillon */ 9726f9a767SRodney W. Grimes int 98b40ce416SJulian Elischer read(td, uap) 99b40ce416SJulian Elischer struct thread *td; 100b064d43dSMatthew Dillon struct read_args *uap; 101df8bae1dSRodney W. Grimes { 102b064d43dSMatthew Dillon struct file *fp; 103279d7226SMatthew Dillon int error; 104df8bae1dSRodney W. Grimes 105ad2edad9SMatthew Dillon mtx_lock(&Giant); 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 } 111ad2edad9SMatthew Dillon mtx_unlock(&Giant); 112279d7226SMatthew Dillon return(error); 113df8bae1dSRodney W. Grimes } 114df8bae1dSRodney W. Grimes 115df8bae1dSRodney W. Grimes /* 1168fe387abSDmitrij Tejblum * Pread system call 1174160ccd9SAlan Cox */ 1184160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1194160ccd9SAlan Cox struct pread_args { 1204160ccd9SAlan Cox int fd; 1214160ccd9SAlan Cox void *buf; 1224160ccd9SAlan Cox size_t nbyte; 1238fe387abSDmitrij Tejblum int pad; 1244160ccd9SAlan Cox off_t offset; 1254160ccd9SAlan Cox }; 1264160ccd9SAlan Cox #endif 127ad2edad9SMatthew Dillon /* 128ad2edad9SMatthew Dillon * MPSAFE 129ad2edad9SMatthew Dillon */ 1304160ccd9SAlan Cox int 131b40ce416SJulian Elischer pread(td, uap) 132b40ce416SJulian Elischer struct thread *td; 133b064d43dSMatthew Dillon struct pread_args *uap; 1344160ccd9SAlan Cox { 135b064d43dSMatthew Dillon struct file *fp; 136279d7226SMatthew Dillon int error; 1378fe387abSDmitrij Tejblum 13897fa4397SAlfred Perlstein if ((error = fget_read(td, uap->fd, &fp)) != 0) 13997fa4397SAlfred Perlstein return (error); 140aa11a498SAlfred Perlstein mtx_lock(&Giant); 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); 148aa11a498SAlfred Perlstein mtx_unlock(&Giant); 149279d7226SMatthew Dillon return(error); 1508fe387abSDmitrij Tejblum } 1518fe387abSDmitrij Tejblum 1528fe387abSDmitrij Tejblum /* 1538fe387abSDmitrij Tejblum * Code common for read and pread 1548fe387abSDmitrij Tejblum */ 1558fe387abSDmitrij Tejblum int 156b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 157b40ce416SJulian Elischer struct thread *td; 1588fe387abSDmitrij Tejblum struct file *fp; 1598fe387abSDmitrij Tejblum int fd, flags; 1608fe387abSDmitrij Tejblum void *buf; 1618fe387abSDmitrij Tejblum size_t nbyte; 1628fe387abSDmitrij Tejblum off_t offset; 1638fe387abSDmitrij Tejblum { 1644160ccd9SAlan Cox struct uio auio; 1654160ccd9SAlan Cox struct iovec aiov; 1664160ccd9SAlan Cox long cnt, error = 0; 1674160ccd9SAlan Cox #ifdef KTRACE 1684160ccd9SAlan Cox struct iovec ktriov; 16942ebfbf2SBrian Feldman struct uio ktruio; 1703c89e357SBrian Feldman int didktr = 0; 1714160ccd9SAlan Cox #endif 1724160ccd9SAlan Cox 1738fe387abSDmitrij Tejblum aiov.iov_base = (caddr_t)buf; 1748fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 1754160ccd9SAlan Cox auio.uio_iov = &aiov; 1764160ccd9SAlan Cox auio.uio_iovcnt = 1; 1778fe387abSDmitrij Tejblum auio.uio_offset = offset; 1788fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 1794160ccd9SAlan Cox return (EINVAL); 1808fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 1814160ccd9SAlan Cox auio.uio_rw = UIO_READ; 1824160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 183b40ce416SJulian Elischer auio.uio_td = td; 1844160ccd9SAlan Cox #ifdef KTRACE 1854160ccd9SAlan Cox /* 1864160ccd9SAlan Cox * if tracing, save a copy of iovec 1874160ccd9SAlan Cox */ 188b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 1894160ccd9SAlan Cox ktriov = aiov; 19042ebfbf2SBrian Feldman ktruio = auio; 1913c89e357SBrian Feldman didktr = 1; 19242ebfbf2SBrian Feldman } 1934160ccd9SAlan Cox #endif 1948fe387abSDmitrij Tejblum cnt = nbyte; 195279d7226SMatthew Dillon 196b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) { 1974160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 1984160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 1994160ccd9SAlan Cox error = 0; 200279d7226SMatthew Dillon } 2014160ccd9SAlan Cox cnt -= auio.uio_resid; 2024160ccd9SAlan Cox #ifdef KTRACE 2033c89e357SBrian Feldman if (didktr && error == 0) { 20442ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 20542ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 206b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_READ, &ktruio, error); 20742ebfbf2SBrian Feldman } 2084160ccd9SAlan Cox #endif 209b40ce416SJulian Elischer td->td_retval[0] = cnt; 2104160ccd9SAlan Cox return (error); 2114160ccd9SAlan Cox } 2124160ccd9SAlan Cox 2134160ccd9SAlan Cox /* 214df8bae1dSRodney W. Grimes * Scatter read system call. 215df8bae1dSRodney W. Grimes */ 216d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 217df8bae1dSRodney W. Grimes struct readv_args { 2187147b19dSBruce Evans int fd; 219df8bae1dSRodney W. Grimes struct iovec *iovp; 220df8bae1dSRodney W. Grimes u_int iovcnt; 221df8bae1dSRodney W. Grimes }; 222d2d3e875SBruce Evans #endif 223ad2edad9SMatthew Dillon /* 224ad2edad9SMatthew Dillon * MPSAFE 225ad2edad9SMatthew Dillon */ 22626f9a767SRodney W. Grimes int 227b40ce416SJulian Elischer readv(td, uap) 228b40ce416SJulian Elischer struct thread *td; 229b064d43dSMatthew Dillon struct readv_args *uap; 230df8bae1dSRodney W. Grimes { 231b064d43dSMatthew Dillon struct file *fp; 232df8bae1dSRodney W. Grimes struct uio auio; 233b064d43dSMatthew Dillon struct iovec *iov; 234df8bae1dSRodney W. Grimes struct iovec *needfree; 235df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 236df8bae1dSRodney W. Grimes long i, cnt, error = 0; 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 242ad2edad9SMatthew Dillon mtx_lock(&Giant); 243df8bae1dSRodney W. Grimes 244b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) != 0) 245ad2edad9SMatthew Dillon goto done2; 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; 251ad2edad9SMatthew Dillon goto done2; 252ad2edad9SMatthew Dillon } 253df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 254df8bae1dSRodney W. Grimes needfree = iov; 255df8bae1dSRodney W. Grimes } else { 256df8bae1dSRodney W. Grimes iov = aiov; 257df8bae1dSRodney W. Grimes needfree = NULL; 258df8bae1dSRodney W. Grimes } 259df8bae1dSRodney W. Grimes auio.uio_iov = iov; 260df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 261df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 262df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 263b40ce416SJulian Elischer auio.uio_td = td; 2642c1011f7SJohn Dyson auio.uio_offset = -1; 265bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 266df8bae1dSRodney W. Grimes goto done; 267df8bae1dSRodney W. Grimes auio.uio_resid = 0; 268df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 269069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 270df8bae1dSRodney W. Grimes error = EINVAL; 271df8bae1dSRodney W. Grimes goto done; 272df8bae1dSRodney W. Grimes } 273069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 274df8bae1dSRodney W. Grimes iov++; 275df8bae1dSRodney W. Grimes } 276df8bae1dSRodney W. Grimes #ifdef KTRACE 277df8bae1dSRodney W. Grimes /* 278df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 279df8bae1dSRodney W. Grimes */ 280b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 281df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 282df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 28342ebfbf2SBrian Feldman ktruio = auio; 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes #endif 286df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 287b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) { 288df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 289df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 290df8bae1dSRodney W. Grimes error = 0; 291279d7226SMatthew Dillon } 292df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 293df8bae1dSRodney W. Grimes #ifdef KTRACE 294df8bae1dSRodney W. Grimes if (ktriov != NULL) { 29542ebfbf2SBrian Feldman if (error == 0) { 29642ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 29742ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 298b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio, 29942ebfbf2SBrian Feldman error); 30042ebfbf2SBrian Feldman } 301df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 302df8bae1dSRodney W. Grimes } 303df8bae1dSRodney W. Grimes #endif 304b40ce416SJulian Elischer td->td_retval[0] = cnt; 305df8bae1dSRodney W. Grimes done: 306b40ce416SJulian Elischer fdrop(fp, td); 307df8bae1dSRodney W. Grimes if (needfree) 308df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 309ad2edad9SMatthew Dillon done2: 310ad2edad9SMatthew Dillon mtx_unlock(&Giant); 311df8bae1dSRodney W. Grimes return (error); 312df8bae1dSRodney W. Grimes } 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes /* 315df8bae1dSRodney W. Grimes * Write system call 316df8bae1dSRodney W. Grimes */ 317d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 318df8bae1dSRodney W. Grimes struct write_args { 319df8bae1dSRodney W. Grimes int fd; 320134e06feSBruce Evans const void *buf; 321134e06feSBruce Evans size_t nbyte; 322df8bae1dSRodney W. Grimes }; 323d2d3e875SBruce Evans #endif 324ad2edad9SMatthew Dillon /* 325ad2edad9SMatthew Dillon * MPSAFE 326ad2edad9SMatthew Dillon */ 32726f9a767SRodney W. Grimes int 328b40ce416SJulian Elischer write(td, uap) 329b40ce416SJulian Elischer struct thread *td; 330b064d43dSMatthew Dillon struct write_args *uap; 331df8bae1dSRodney W. Grimes { 332b064d43dSMatthew Dillon struct file *fp; 333279d7226SMatthew Dillon int error; 334df8bae1dSRodney W. Grimes 335ad2edad9SMatthew Dillon mtx_lock(&Giant); 336b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 337b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 338ad2edad9SMatthew Dillon (off_t)-1, 0); 339b40ce416SJulian Elischer fdrop(fp, td); 340ad2edad9SMatthew Dillon } else { 341b064d43dSMatthew Dillon error = EBADF; /* XXX this can't be right */ 342ad2edad9SMatthew Dillon } 343ad2edad9SMatthew Dillon mtx_unlock(&Giant); 344279d7226SMatthew Dillon return(error); 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes 347df8bae1dSRodney W. Grimes /* 3488fe387abSDmitrij Tejblum * Pwrite system call 3494160ccd9SAlan Cox */ 3504160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3514160ccd9SAlan Cox struct pwrite_args { 3524160ccd9SAlan Cox int fd; 3534160ccd9SAlan Cox const void *buf; 3544160ccd9SAlan Cox size_t nbyte; 3558fe387abSDmitrij Tejblum int pad; 3564160ccd9SAlan Cox off_t offset; 3574160ccd9SAlan Cox }; 3584160ccd9SAlan Cox #endif 359ad2edad9SMatthew Dillon /* 360ad2edad9SMatthew Dillon * MPSAFE 361ad2edad9SMatthew Dillon */ 3624160ccd9SAlan Cox int 363b40ce416SJulian Elischer pwrite(td, uap) 364b40ce416SJulian Elischer struct thread *td; 365b064d43dSMatthew Dillon struct pwrite_args *uap; 3664160ccd9SAlan Cox { 367b064d43dSMatthew Dillon struct file *fp; 368279d7226SMatthew Dillon int error; 3698fe387abSDmitrij Tejblum 370b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 371b5c93a56SAlfred Perlstein mtx_lock(&Giant); 372aa11a498SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) { 373b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 374b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 375b064d43dSMatthew Dillon } else { 376279d7226SMatthew Dillon error = ESPIPE; 377b064d43dSMatthew Dillon } 378b40ce416SJulian Elischer fdrop(fp, td); 379aa11a498SAlfred Perlstein mtx_unlock(&Giant); 380279d7226SMatthew Dillon } else { 381b064d43dSMatthew Dillon error = EBADF; /* this can't be right */ 382ad2edad9SMatthew Dillon } 383279d7226SMatthew Dillon return(error); 3848fe387abSDmitrij Tejblum } 3858fe387abSDmitrij Tejblum 3868fe387abSDmitrij Tejblum static int 387b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 388b40ce416SJulian Elischer struct thread *td; 3898fe387abSDmitrij Tejblum struct file *fp; 3908fe387abSDmitrij Tejblum int fd, flags; 3918fe387abSDmitrij Tejblum const void *buf; 3928fe387abSDmitrij Tejblum size_t nbyte; 3938fe387abSDmitrij Tejblum off_t offset; 3948fe387abSDmitrij Tejblum { 3954160ccd9SAlan Cox struct uio auio; 3964160ccd9SAlan Cox struct iovec aiov; 3974160ccd9SAlan Cox long cnt, error = 0; 3984160ccd9SAlan Cox #ifdef KTRACE 3994160ccd9SAlan Cox struct iovec ktriov; 40042ebfbf2SBrian Feldman struct uio ktruio; 4013c89e357SBrian Feldman int didktr = 0; 4024160ccd9SAlan Cox #endif 4034160ccd9SAlan Cox 404b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 4058fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 4064160ccd9SAlan Cox auio.uio_iov = &aiov; 4074160ccd9SAlan Cox auio.uio_iovcnt = 1; 4088fe387abSDmitrij Tejblum auio.uio_offset = offset; 4098fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 4104160ccd9SAlan Cox return (EINVAL); 4118fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 4124160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 4134160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 414b40ce416SJulian Elischer auio.uio_td = td; 4154160ccd9SAlan Cox #ifdef KTRACE 4164160ccd9SAlan Cox /* 41742ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4184160ccd9SAlan Cox */ 419b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 4204160ccd9SAlan Cox ktriov = aiov; 42142ebfbf2SBrian Feldman ktruio = auio; 4223c89e357SBrian Feldman didktr = 1; 42342ebfbf2SBrian Feldman } 4244160ccd9SAlan Cox #endif 4258fe387abSDmitrij Tejblum cnt = nbyte; 426c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 427279d7226SMatthew Dillon bwillwrite(); 428b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) { 4294160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4304160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4314160ccd9SAlan Cox error = 0; 43219eb87d2SJohn Baldwin if (error == EPIPE) { 433b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 434b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 435b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 43619eb87d2SJohn Baldwin } 4374160ccd9SAlan Cox } 4384160ccd9SAlan Cox cnt -= auio.uio_resid; 4394160ccd9SAlan Cox #ifdef KTRACE 4403c89e357SBrian Feldman if (didktr && error == 0) { 44142ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 44242ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 443b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_WRITE, &ktruio, error); 44442ebfbf2SBrian Feldman } 4454160ccd9SAlan Cox #endif 446b40ce416SJulian Elischer td->td_retval[0] = cnt; 4474160ccd9SAlan Cox return (error); 4484160ccd9SAlan Cox } 4494160ccd9SAlan Cox 4504160ccd9SAlan Cox /* 451df8bae1dSRodney W. Grimes * Gather write system call 452df8bae1dSRodney W. Grimes */ 453d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 454df8bae1dSRodney W. Grimes struct writev_args { 455df8bae1dSRodney W. Grimes int fd; 456df8bae1dSRodney W. Grimes struct iovec *iovp; 457df8bae1dSRodney W. Grimes u_int iovcnt; 458df8bae1dSRodney W. Grimes }; 459d2d3e875SBruce Evans #endif 460ad2edad9SMatthew Dillon /* 461ad2edad9SMatthew Dillon * MPSAFE 462ad2edad9SMatthew Dillon */ 46326f9a767SRodney W. Grimes int 464b40ce416SJulian Elischer writev(td, uap) 465b40ce416SJulian Elischer struct thread *td; 466df8bae1dSRodney W. Grimes register struct writev_args *uap; 467df8bae1dSRodney W. Grimes { 468b064d43dSMatthew Dillon struct file *fp; 469df8bae1dSRodney W. Grimes struct uio auio; 470df8bae1dSRodney W. Grimes register struct iovec *iov; 471df8bae1dSRodney W. Grimes struct iovec *needfree; 472df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 473df8bae1dSRodney W. Grimes long i, cnt, error = 0; 474df8bae1dSRodney W. Grimes u_int iovlen; 475df8bae1dSRodney W. Grimes #ifdef KTRACE 476df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 47742ebfbf2SBrian Feldman struct uio ktruio; 478df8bae1dSRodney W. Grimes #endif 479df8bae1dSRodney W. Grimes 480ad2edad9SMatthew Dillon mtx_lock(&Giant); 481b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) != 0) { 482ad2edad9SMatthew Dillon error = EBADF; 483ad2edad9SMatthew Dillon goto done2; 484ad2edad9SMatthew Dillon } 485df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 486df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 487df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 4881aa3e7ddSBrian Feldman if (uap->iovcnt > UIO_MAXIOV) { 4891aa3e7ddSBrian Feldman needfree = NULL; 4901aa3e7ddSBrian Feldman error = EINVAL; 4911aa3e7ddSBrian Feldman goto done; 4921aa3e7ddSBrian Feldman } 493df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 494df8bae1dSRodney W. Grimes needfree = iov; 495df8bae1dSRodney W. Grimes } else { 496df8bae1dSRodney W. Grimes iov = aiov; 497df8bae1dSRodney W. Grimes needfree = NULL; 498df8bae1dSRodney W. Grimes } 499df8bae1dSRodney W. Grimes auio.uio_iov = iov; 500df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 501df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 502df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 503b40ce416SJulian Elischer auio.uio_td = td; 5042c1011f7SJohn Dyson auio.uio_offset = -1; 505bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 506df8bae1dSRodney W. Grimes goto done; 507df8bae1dSRodney W. Grimes auio.uio_resid = 0; 508df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 509069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 510df8bae1dSRodney W. Grimes error = EINVAL; 511df8bae1dSRodney W. Grimes goto done; 512df8bae1dSRodney W. Grimes } 513069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 514df8bae1dSRodney W. Grimes iov++; 515df8bae1dSRodney W. Grimes } 516df8bae1dSRodney W. Grimes #ifdef KTRACE 517df8bae1dSRodney W. Grimes /* 51842ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 519df8bae1dSRodney W. Grimes */ 520b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 521df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 522df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 52342ebfbf2SBrian Feldman ktruio = auio; 524df8bae1dSRodney W. Grimes } 525df8bae1dSRodney W. Grimes #endif 526df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 527a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5289440653dSMatthew Dillon bwillwrite(); 529b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) { 530df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 531df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 532df8bae1dSRodney W. Grimes error = 0; 53319eb87d2SJohn Baldwin if (error == EPIPE) { 534b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 535b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 536b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 53719eb87d2SJohn Baldwin } 538df8bae1dSRodney W. Grimes } 539df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 540df8bae1dSRodney W. Grimes #ifdef KTRACE 541df8bae1dSRodney W. Grimes if (ktriov != NULL) { 54242ebfbf2SBrian Feldman if (error == 0) { 54342ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 54442ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 545b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_WRITE, &ktruio, 54642ebfbf2SBrian Feldman error); 54742ebfbf2SBrian Feldman } 548df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 549df8bae1dSRodney W. Grimes } 550df8bae1dSRodney W. Grimes #endif 551b40ce416SJulian Elischer td->td_retval[0] = cnt; 552df8bae1dSRodney W. Grimes done: 553b40ce416SJulian Elischer fdrop(fp, td); 554df8bae1dSRodney W. Grimes if (needfree) 555df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 556ad2edad9SMatthew Dillon done2: 557ad2edad9SMatthew Dillon mtx_unlock(&Giant); 558df8bae1dSRodney W. Grimes return (error); 559df8bae1dSRodney W. Grimes } 560df8bae1dSRodney W. Grimes 561df8bae1dSRodney W. Grimes /* 562df8bae1dSRodney W. Grimes * Ioctl system call 563df8bae1dSRodney W. Grimes */ 564d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 565df8bae1dSRodney W. Grimes struct ioctl_args { 566df8bae1dSRodney W. Grimes int fd; 567069e9bc1SDoug Rabson u_long com; 568df8bae1dSRodney W. Grimes caddr_t data; 569df8bae1dSRodney W. Grimes }; 570d2d3e875SBruce Evans #endif 571ad2edad9SMatthew Dillon /* 572ad2edad9SMatthew Dillon * MPSAFE 573ad2edad9SMatthew Dillon */ 574df8bae1dSRodney W. Grimes /* ARGSUSED */ 57526f9a767SRodney W. Grimes int 576b40ce416SJulian Elischer ioctl(td, uap) 577b40ce416SJulian Elischer struct thread *td; 578df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 579df8bae1dSRodney W. Grimes { 580a4db4953SAlfred Perlstein struct file *fp; 581df8bae1dSRodney W. Grimes register struct filedesc *fdp; 582831b9ef2SDoug Rabson register u_long com; 583ad2edad9SMatthew Dillon int error = 0; 584df8bae1dSRodney W. Grimes register u_int size; 585df8bae1dSRodney W. Grimes caddr_t data, memp; 586df8bae1dSRodney W. Grimes int tmp; 587df8bae1dSRodney W. Grimes #define STK_PARAMS 128 588d2ba455cSMatthew Dillon union { 589df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 590d2ba455cSMatthew Dillon long align; 591d2ba455cSMatthew Dillon } ubuf; 592df8bae1dSRodney W. Grimes 593a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 594a4db4953SAlfred Perlstein return (error); 595aa11a498SAlfred Perlstein mtx_lock(&Giant); 596ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 597426da3bcSAlfred Perlstein fdrop(fp, td); 598aa11a498SAlfred Perlstein mtx_unlock(&Giant); 599426da3bcSAlfred Perlstein return (EBADF); 600ad2edad9SMatthew Dillon } 601426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 602df8bae1dSRodney W. Grimes switch (com = uap->com) { 603df8bae1dSRodney W. Grimes case FIONCLEX: 604426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 605df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 606426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 607426da3bcSAlfred Perlstein fdrop(fp, td); 608aa11a498SAlfred Perlstein mtx_unlock(&Giant); 609426da3bcSAlfred Perlstein return (0); 610df8bae1dSRodney W. Grimes case FIOCLEX: 611426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 612df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 613426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 614426da3bcSAlfred Perlstein fdrop(fp, td); 615aa11a498SAlfred Perlstein mtx_unlock(&Giant); 616426da3bcSAlfred Perlstein return (0); 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes /* 620df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 621df8bae1dSRodney W. Grimes * copied to/from the user's address space. 622df8bae1dSRodney W. Grimes */ 623df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 624ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 625426da3bcSAlfred Perlstein fdrop(fp, td); 626aa11a498SAlfred Perlstein mtx_unlock(&Giant); 627426da3bcSAlfred Perlstein return (ENOTTY); 628ad2edad9SMatthew Dillon } 629279d7226SMatthew Dillon 630df8bae1dSRodney W. Grimes memp = NULL; 631d2ba455cSMatthew Dillon if (size > sizeof (ubuf.stkbuf)) { 632df8bae1dSRodney W. Grimes memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 633df8bae1dSRodney W. Grimes data = memp; 634279d7226SMatthew Dillon } else { 635d2ba455cSMatthew Dillon data = ubuf.stkbuf; 636279d7226SMatthew Dillon } 637df8bae1dSRodney W. Grimes if (com&IOC_IN) { 638df8bae1dSRodney W. Grimes if (size) { 639df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 640df8bae1dSRodney W. Grimes if (error) { 641df8bae1dSRodney W. Grimes if (memp) 642df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 643b40ce416SJulian Elischer fdrop(fp, td); 644426da3bcSAlfred Perlstein goto done; 645df8bae1dSRodney W. Grimes } 646279d7226SMatthew Dillon } else { 647df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 648279d7226SMatthew Dillon } 649279d7226SMatthew Dillon } else if ((com&IOC_OUT) && size) { 650df8bae1dSRodney W. Grimes /* 651df8bae1dSRodney W. Grimes * Zero the buffer so the user always 652df8bae1dSRodney W. Grimes * gets back something deterministic. 653df8bae1dSRodney W. Grimes */ 654df8bae1dSRodney W. Grimes bzero(data, size); 655279d7226SMatthew Dillon } else if (com&IOC_VOID) { 656df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 657279d7226SMatthew Dillon } 658df8bae1dSRodney W. Grimes 659df8bae1dSRodney W. Grimes switch (com) { 660df8bae1dSRodney W. Grimes 661df8bae1dSRodney W. Grimes case FIONBIO: 662426da3bcSAlfred Perlstein FILE_LOCK(fp); 663bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 664df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 665df8bae1dSRodney W. Grimes else 666df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 667426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 668b40ce416SJulian Elischer error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td); 669df8bae1dSRodney W. Grimes break; 670df8bae1dSRodney W. Grimes 671df8bae1dSRodney W. Grimes case FIOASYNC: 672426da3bcSAlfred Perlstein FILE_LOCK(fp); 673bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 674df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 675df8bae1dSRodney W. Grimes else 676df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 677426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 678b40ce416SJulian Elischer error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td); 679df8bae1dSRodney W. Grimes break; 680df8bae1dSRodney W. Grimes 681df8bae1dSRodney W. Grimes default: 682b40ce416SJulian Elischer error = fo_ioctl(fp, com, data, td); 683df8bae1dSRodney W. Grimes /* 684df8bae1dSRodney W. Grimes * Copy any data to user, size was 685df8bae1dSRodney W. Grimes * already set and checked above. 686df8bae1dSRodney W. Grimes */ 687df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 688df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 689df8bae1dSRodney W. Grimes break; 690df8bae1dSRodney W. Grimes } 691df8bae1dSRodney W. Grimes if (memp) 692df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 693b40ce416SJulian Elischer fdrop(fp, td); 694426da3bcSAlfred Perlstein done: 695ad2edad9SMatthew Dillon mtx_unlock(&Giant); 696df8bae1dSRodney W. Grimes return (error); 697df8bae1dSRodney W. Grimes } 698df8bae1dSRodney W. Grimes 69985f190e4SAlfred Perlstein /* 70085f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 70185f190e4SAlfred Perlstein */ 70285f190e4SAlfred Perlstein struct mtx sellock; 703265fc98fSSeigo Tanimura struct cv selwait; 70485f190e4SAlfred Perlstein int nselcoll; /* Select collisions since boot */ 7058cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 706df8bae1dSRodney W. Grimes 707df8bae1dSRodney W. Grimes /* 708df8bae1dSRodney W. Grimes * Select system call. 709df8bae1dSRodney W. Grimes */ 710d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 711df8bae1dSRodney W. Grimes struct select_args { 712b08f7993SSujal Patel int nd; 713df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 714df8bae1dSRodney W. Grimes struct timeval *tv; 715df8bae1dSRodney W. Grimes }; 716d2d3e875SBruce Evans #endif 717ad2edad9SMatthew Dillon /* 718ad2edad9SMatthew Dillon * MPSAFE 719ad2edad9SMatthew Dillon */ 72026f9a767SRodney W. Grimes int 721b40ce416SJulian Elischer select(td, uap) 722b40ce416SJulian Elischer register struct thread *td; 723df8bae1dSRodney W. Grimes register struct select_args *uap; 724df8bae1dSRodney W. Grimes { 725426da3bcSAlfred Perlstein struct filedesc *fdp; 726d5e4d7e1SBruce Evans /* 727d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 728d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 729d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 730d5e4d7e1SBruce Evans * of 256. 731d5e4d7e1SBruce Evans */ 732d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 733eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 73400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7354658f926SAlfred Perlstein int ncoll, error, timo; 736d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 737df8bae1dSRodney W. Grimes 738b08f7993SSujal Patel if (uap->nd < 0) 739acbfbfeaSSujal Patel return (EINVAL); 740426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 741ad2edad9SMatthew Dillon mtx_lock(&Giant); 742426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 743ad2edad9SMatthew Dillon 744b40ce416SJulian Elischer if (uap->nd > td->td_proc->p_fd->fd_nfiles) 745b40ce416SJulian Elischer uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 746426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 747b08f7993SSujal Patel 748d5e4d7e1SBruce Evans /* 749d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 750d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 751d5e4d7e1SBruce Evans */ 752d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 753d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 754d5e4d7e1SBruce Evans nbufbytes = 0; 755d5e4d7e1SBruce Evans if (uap->in != NULL) 756d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 757d5e4d7e1SBruce Evans if (uap->ou != NULL) 758d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 759d5e4d7e1SBruce Evans if (uap->ex != NULL) 760d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 761d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 762d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 763d5e4d7e1SBruce Evans else 764d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 765b08f7993SSujal Patel 766b08f7993SSujal Patel /* 767d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 768d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 769d5e4d7e1SBruce Evans * together. 770b08f7993SSujal Patel */ 771d5e4d7e1SBruce Evans sbp = selbits; 772df8bae1dSRodney W. Grimes #define getbits(name, x) \ 773d5e4d7e1SBruce Evans do { \ 774d5e4d7e1SBruce Evans if (uap->name == NULL) \ 775d5e4d7e1SBruce Evans ibits[x] = NULL; \ 776d5e4d7e1SBruce Evans else { \ 777d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 778d5e4d7e1SBruce Evans obits[x] = sbp; \ 779d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 780d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 781265fc98fSSeigo Tanimura if (error != 0) \ 78285f190e4SAlfred Perlstein goto done_nosellock; \ 783e04ac2feSJohn Baldwin } \ 784d5e4d7e1SBruce Evans } while (0) 785df8bae1dSRodney W. Grimes getbits(in, 0); 786df8bae1dSRodney W. Grimes getbits(ou, 1); 787df8bae1dSRodney W. Grimes getbits(ex, 2); 788df8bae1dSRodney W. Grimes #undef getbits 789d5e4d7e1SBruce Evans if (nbufbytes != 0) 790d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 791df8bae1dSRodney W. Grimes 792df8bae1dSRodney W. Grimes if (uap->tv) { 793df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 794df8bae1dSRodney W. Grimes sizeof (atv)); 795265fc98fSSeigo Tanimura if (error) 79685f190e4SAlfred Perlstein goto done_nosellock; 797df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 798df8bae1dSRodney W. Grimes error = EINVAL; 79985f190e4SAlfred Perlstein goto done_nosellock; 800df8bae1dSRodney W. Grimes } 801c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 80200af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8039c386f6bSJohn Baldwin } else { 80400af9731SPoul-Henning Kamp atv.tv_sec = 0; 8059c386f6bSJohn Baldwin atv.tv_usec = 0; 8069c386f6bSJohn Baldwin } 80700af9731SPoul-Henning Kamp timo = 0; 80885f190e4SAlfred Perlstein mtx_lock(&sellock); 809df8bae1dSRodney W. Grimes retry: 810df8bae1dSRodney W. Grimes ncoll = nselcoll; 811fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 812b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 813fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 81485f190e4SAlfred Perlstein mtx_unlock(&sellock); 81585f190e4SAlfred Perlstein 81685f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 81785f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 818b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 81985f190e4SAlfred Perlstein mtx_lock(&sellock); 820b40ce416SJulian Elischer if (error || td->td_retval[0]) 821df8bae1dSRodney W. Grimes goto done; 8224da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 823c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 82485f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 825df8bae1dSRodney W. Grimes goto done; 82600af9731SPoul-Henning Kamp ttv = atv; 82700af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 82800af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 82900af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 830df8bae1dSRodney W. Grimes } 83185f190e4SAlfred Perlstein 83285f190e4SAlfred Perlstein /* 83385f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 83485f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 83585f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 83685f190e4SAlfred Perlstein * necessary. 83785f190e4SAlfred Perlstein */ 838fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 83985f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 84085f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 84185f190e4SAlfred Perlstein goto retry; 84285f190e4SAlfred Perlstein } 843fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 844bfbbc4aaSJason Evans 845265fc98fSSeigo Tanimura if (timo > 0) 84685f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 847265fc98fSSeigo Tanimura else 84885f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 849bfbbc4aaSJason Evans 850df8bae1dSRodney W. Grimes if (error == 0) 851df8bae1dSRodney W. Grimes goto retry; 852265fc98fSSeigo Tanimura 853df8bae1dSRodney W. Grimes done: 85485f190e4SAlfred Perlstein clear_selinfo_list(td); 855fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 856b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 857fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 85885f190e4SAlfred Perlstein mtx_unlock(&sellock); 85985f190e4SAlfred Perlstein 86085f190e4SAlfred Perlstein done_nosellock: 861df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 862df8bae1dSRodney W. Grimes if (error == ERESTART) 863df8bae1dSRodney W. Grimes error = EINTR; 864df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 865df8bae1dSRodney W. Grimes error = 0; 866df8bae1dSRodney W. Grimes #define putbits(name, x) \ 867d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 868df8bae1dSRodney W. Grimes error = error2; 869df8bae1dSRodney W. Grimes if (error == 0) { 870df8bae1dSRodney W. Grimes int error2; 871df8bae1dSRodney W. Grimes 872df8bae1dSRodney W. Grimes putbits(in, 0); 873df8bae1dSRodney W. Grimes putbits(ou, 1); 874df8bae1dSRodney W. Grimes putbits(ex, 2); 875df8bae1dSRodney W. Grimes #undef putbits 876df8bae1dSRodney W. Grimes } 877d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 878d5e4d7e1SBruce Evans free(selbits, M_SELECT); 879ad2edad9SMatthew Dillon 880ad2edad9SMatthew Dillon mtx_unlock(&Giant); 881df8bae1dSRodney W. Grimes return (error); 882df8bae1dSRodney W. Grimes } 883df8bae1dSRodney W. Grimes 884265fc98fSSeigo Tanimura static int 885b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 886b40ce416SJulian Elischer struct thread *td; 887b08f7993SSujal Patel fd_mask **ibits, **obits; 888cb226aaaSPoul-Henning Kamp int nfd; 889df8bae1dSRodney W. Grimes { 890f082218cSPeter Wemm int msk, i, fd; 891f082218cSPeter Wemm fd_mask bits; 892df8bae1dSRodney W. Grimes struct file *fp; 893df8bae1dSRodney W. Grimes int n = 0; 8942087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 89542d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 896eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 897df8bae1dSRodney W. Grimes 898eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 899df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 900d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 901d5e4d7e1SBruce Evans continue; 902df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 903b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 904f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 905f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 906f082218cSPeter Wemm if (!(bits & 1)) 907f082218cSPeter Wemm continue; 908eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 909eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 910df8bae1dSRodney W. Grimes return (EBADF); 911eb209311SAlfred Perlstein } 912b40ce416SJulian Elischer if (fo_poll(fp, flag[msk], fp->f_cred, td)) { 913b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 914f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 915df8bae1dSRodney W. Grimes n++; 916df8bae1dSRodney W. Grimes } 917df8bae1dSRodney W. Grimes } 918df8bae1dSRodney W. Grimes } 919df8bae1dSRodney W. Grimes } 920eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 921b40ce416SJulian Elischer td->td_retval[0] = n; 922df8bae1dSRodney W. Grimes return (0); 923df8bae1dSRodney W. Grimes } 924df8bae1dSRodney W. Grimes 92542d11757SPeter Wemm /* 92642d11757SPeter Wemm * Poll system call. 92742d11757SPeter Wemm */ 92842d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 92942d11757SPeter Wemm struct poll_args { 93042d11757SPeter Wemm struct pollfd *fds; 93142d11757SPeter Wemm u_int nfds; 93242d11757SPeter Wemm int timeout; 93342d11757SPeter Wemm }; 93442d11757SPeter Wemm #endif 935ad2edad9SMatthew Dillon /* 936ad2edad9SMatthew Dillon * MPSAFE 937ad2edad9SMatthew Dillon */ 93842d11757SPeter Wemm int 939b40ce416SJulian Elischer poll(td, uap) 940b40ce416SJulian Elischer struct thread *td; 941ea0237edSJonathan Lemon struct poll_args *uap; 94242d11757SPeter Wemm { 94342d11757SPeter Wemm caddr_t bits; 94442d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 94500af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 946265fc98fSSeigo Tanimura int ncoll, error = 0, timo; 947ea0237edSJonathan Lemon u_int nfds; 94842d11757SPeter Wemm size_t ni; 94942d11757SPeter Wemm 95089b71647SPeter Wemm nfds = SCARG(uap, nfds); 951ad2edad9SMatthew Dillon 952ad2edad9SMatthew Dillon mtx_lock(&Giant); 95389b71647SPeter Wemm /* 9542bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9552bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9562bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9572bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9582bd5ac33SPeter Wemm * safe, but not overly restrictive. 95989b71647SPeter Wemm */ 960b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 961b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 962ad2edad9SMatthew Dillon error = EINVAL; 963ad2edad9SMatthew Dillon goto done2; 964ad2edad9SMatthew Dillon } 96589b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 96642d11757SPeter Wemm if (ni > sizeof(smallbits)) 96742d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 96842d11757SPeter Wemm else 96942d11757SPeter Wemm bits = smallbits; 97042d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 97142d11757SPeter Wemm if (error) 97285f190e4SAlfred Perlstein goto done_nosellock; 97342d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 97442d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 97542d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 97642d11757SPeter Wemm if (itimerfix(&atv)) { 97742d11757SPeter Wemm error = EINVAL; 97885f190e4SAlfred Perlstein goto done_nosellock; 97942d11757SPeter Wemm } 980c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 98100af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9829c386f6bSJohn Baldwin } else { 98300af9731SPoul-Henning Kamp atv.tv_sec = 0; 9849c386f6bSJohn Baldwin atv.tv_usec = 0; 9859c386f6bSJohn Baldwin } 98600af9731SPoul-Henning Kamp timo = 0; 98785f190e4SAlfred Perlstein mtx_lock(&sellock); 98842d11757SPeter Wemm retry: 98942d11757SPeter Wemm ncoll = nselcoll; 990fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 991b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 992fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 99385f190e4SAlfred Perlstein mtx_unlock(&sellock); 99485f190e4SAlfred Perlstein 99585f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 99685f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 997b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 99885f190e4SAlfred Perlstein mtx_lock(&sellock); 999b40ce416SJulian Elischer if (error || td->td_retval[0]) 100042d11757SPeter Wemm goto done; 10014da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1002c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 100385f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 100442d11757SPeter Wemm goto done; 100500af9731SPoul-Henning Kamp ttv = atv; 100600af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 100700af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 100800af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 100942d11757SPeter Wemm } 101085f190e4SAlfred Perlstein /* 101185f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 101285f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 101385f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 101485f190e4SAlfred Perlstein */ 1015fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 101685f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 1017fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 101885f190e4SAlfred Perlstein goto retry; 101985f190e4SAlfred Perlstein } 102085f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 102185f190e4SAlfred Perlstein 1022265fc98fSSeigo Tanimura if (timo > 0) 102385f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 1024265fc98fSSeigo Tanimura else 102585f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 102685f190e4SAlfred Perlstein 102742d11757SPeter Wemm if (error == 0) 102842d11757SPeter Wemm goto retry; 1029265fc98fSSeigo Tanimura 103042d11757SPeter Wemm done: 103185f190e4SAlfred Perlstein clear_selinfo_list(td); 1032fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1033b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1034fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 103585f190e4SAlfred Perlstein mtx_unlock(&sellock); 103685f190e4SAlfred Perlstein 103785f190e4SAlfred Perlstein done_nosellock: 103842d11757SPeter Wemm /* poll is not restarted after signals... */ 103942d11757SPeter Wemm if (error == ERESTART) 104042d11757SPeter Wemm error = EINTR; 104142d11757SPeter Wemm if (error == EWOULDBLOCK) 104242d11757SPeter Wemm error = 0; 104342d11757SPeter Wemm if (error == 0) { 104442d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 104542d11757SPeter Wemm if (error) 104642d11757SPeter Wemm goto out; 104742d11757SPeter Wemm } 104842d11757SPeter Wemm out: 104942d11757SPeter Wemm if (ni > sizeof(smallbits)) 105042d11757SPeter Wemm free(bits, M_TEMP); 1051ad2edad9SMatthew Dillon done2: 1052ad2edad9SMatthew Dillon mtx_unlock(&Giant); 105342d11757SPeter Wemm return (error); 105442d11757SPeter Wemm } 105542d11757SPeter Wemm 105642d11757SPeter Wemm static int 1057b40ce416SJulian Elischer pollscan(td, fds, nfd) 1058b40ce416SJulian Elischer struct thread *td; 105942d11757SPeter Wemm struct pollfd *fds; 1060ea0237edSJonathan Lemon u_int nfd; 106142d11757SPeter Wemm { 1062b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 106342d11757SPeter Wemm int i; 106442d11757SPeter Wemm struct file *fp; 106542d11757SPeter Wemm int n = 0; 106642d11757SPeter Wemm 1067426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1068eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1069337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 107042d11757SPeter Wemm fds->revents = POLLNVAL; 107142d11757SPeter Wemm n++; 1072337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1073337c9691SJordan K. Hubbard fds->revents = 0; 107442d11757SPeter Wemm } else { 107542d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1076279d7226SMatthew Dillon if (fp == NULL) { 107742d11757SPeter Wemm fds->revents = POLLNVAL; 107842d11757SPeter Wemm n++; 107942d11757SPeter Wemm } else { 10802087c896SBruce Evans /* 10812087c896SBruce Evans * Note: backend also returns POLLHUP and 10822087c896SBruce Evans * POLLERR if appropriate. 10832087c896SBruce Evans */ 108413ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1085b40ce416SJulian Elischer fp->f_cred, td); 108642d11757SPeter Wemm if (fds->revents != 0) 108742d11757SPeter Wemm n++; 108842d11757SPeter Wemm } 108942d11757SPeter Wemm } 109042d11757SPeter Wemm } 1091eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1092b40ce416SJulian Elischer td->td_retval[0] = n; 109342d11757SPeter Wemm return (0); 109442d11757SPeter Wemm } 109542d11757SPeter Wemm 109642d11757SPeter Wemm /* 109742d11757SPeter Wemm * OpenBSD poll system call. 109842d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 109942d11757SPeter Wemm */ 110042d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 110142d11757SPeter Wemm struct openbsd_poll_args { 110242d11757SPeter Wemm struct pollfd *fds; 110342d11757SPeter Wemm u_int nfds; 110442d11757SPeter Wemm int timeout; 110542d11757SPeter Wemm }; 110642d11757SPeter Wemm #endif 1107ad2edad9SMatthew Dillon /* 1108ad2edad9SMatthew Dillon * MPSAFE 1109ad2edad9SMatthew Dillon */ 111042d11757SPeter Wemm int 1111b40ce416SJulian Elischer openbsd_poll(td, uap) 1112b40ce416SJulian Elischer register struct thread *td; 111342d11757SPeter Wemm register struct openbsd_poll_args *uap; 111442d11757SPeter Wemm { 1115b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 111642d11757SPeter Wemm } 111742d11757SPeter Wemm 111885f190e4SAlfred Perlstein /* 111985f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 112085f190e4SAlfred Perlstein * we were polling. 112185f190e4SAlfred Perlstein * 112285f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 112385f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 112485f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 112585f190e4SAlfred Perlstein */ 112685f190e4SAlfred Perlstein void 112785f190e4SAlfred Perlstein clear_selinfo_list(td) 112885f190e4SAlfred Perlstein struct thread *td; 112985f190e4SAlfred Perlstein { 113085f190e4SAlfred Perlstein struct selinfo *si; 113185f190e4SAlfred Perlstein 113285f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 113385f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 113485f190e4SAlfred Perlstein si->si_thread = NULL; 113585f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 113685f190e4SAlfred Perlstein } 113785f190e4SAlfred Perlstein 1138df8bae1dSRodney W. Grimes /*ARGSUSED*/ 113926f9a767SRodney W. Grimes int 1140b40ce416SJulian Elischer seltrue(dev, events, td) 1141df8bae1dSRodney W. Grimes dev_t dev; 114242d11757SPeter Wemm int events; 1143b40ce416SJulian Elischer struct thread *td; 1144df8bae1dSRodney W. Grimes { 1145df8bae1dSRodney W. Grimes 114642d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1147df8bae1dSRodney W. Grimes } 1148df8bae1dSRodney W. Grimes 1149df8bae1dSRodney W. Grimes /* 1150df8bae1dSRodney W. Grimes * Record a select request. 1151df8bae1dSRodney W. Grimes */ 1152df8bae1dSRodney W. Grimes void 1153df8bae1dSRodney W. Grimes selrecord(selector, sip) 1154b40ce416SJulian Elischer struct thread *selector; 1155df8bae1dSRodney W. Grimes struct selinfo *sip; 1156df8bae1dSRodney W. Grimes { 1157df8bae1dSRodney W. Grimes 115885f190e4SAlfred Perlstein mtx_lock(&sellock); 115985f190e4SAlfred Perlstein /* 116085f190e4SAlfred Perlstein * If the thread is NULL then take ownership of selinfo 116185f190e4SAlfred Perlstein * however if the thread is not NULL and the thread points to 116285f190e4SAlfred Perlstein * someone else, then we have a collision, otherwise leave it alone 116385f190e4SAlfred Perlstein * as we've owned it in a previous selrecord on this selinfo. 116485f190e4SAlfred Perlstein */ 116585f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1166b40ce416SJulian Elischer sip->si_thread = selector; 116785f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 116885f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 116985f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 117085f190e4SAlfred Perlstein } 117185f190e4SAlfred Perlstein 117285f190e4SAlfred Perlstein mtx_unlock(&sellock); 1173df8bae1dSRodney W. Grimes } 1174df8bae1dSRodney W. Grimes 1175df8bae1dSRodney W. Grimes /* 1176df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1177df8bae1dSRodney W. Grimes */ 1178df8bae1dSRodney W. Grimes void 1179df8bae1dSRodney W. Grimes selwakeup(sip) 118085f190e4SAlfred Perlstein struct selinfo *sip; 1181df8bae1dSRodney W. Grimes { 1182b40ce416SJulian Elischer struct thread *td; 1183df8bae1dSRodney W. Grimes 118485f190e4SAlfred Perlstein mtx_lock(&sellock); 118585f190e4SAlfred Perlstein td = sip->si_thread; 118685f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1187df8bae1dSRodney W. Grimes nselcoll++; 1188df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1189265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1190df8bae1dSRodney W. Grimes } 119185f190e4SAlfred Perlstein if (td == NULL) { 119285f190e4SAlfred Perlstein mtx_unlock(&sellock); 1193b40ce416SJulian Elischer return; 1194b40ce416SJulian Elischer } 119585f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 119685f190e4SAlfred Perlstein sip->si_thread = NULL; 11979ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1198b40ce416SJulian Elischer if (td->td_wchan == (caddr_t)&selwait) { 1199b40ce416SJulian Elischer if (td->td_proc->p_stat == SSLEEP) 1200b40ce416SJulian Elischer setrunnable(td); 1201df8bae1dSRodney W. Grimes else 1202b40ce416SJulian Elischer cv_waitq_remove(td); 120333a9ed9dSJohn Baldwin } else 1204b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 120533a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 120685f190e4SAlfred Perlstein mtx_unlock(&sellock); 1207df8bae1dSRodney W. Grimes } 1208265fc98fSSeigo Tanimura 1209265fc98fSSeigo Tanimura static void selectinit __P((void *)); 1210265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1211265fc98fSSeigo Tanimura 1212265fc98fSSeigo Tanimura /* ARGSUSED*/ 1213265fc98fSSeigo Tanimura static void 1214265fc98fSSeigo Tanimura selectinit(dummy) 1215265fc98fSSeigo Tanimura void *dummy; 1216265fc98fSSeigo Tanimura { 1217265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 121885f190e4SAlfred Perlstein mtx_init(&sellock, "sellck", MTX_DEF); 1219265fc98fSSeigo Tanimura } 1220