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 77b40ce416SJulian Elischer static int pollscan __P((struct thread *, struct pollfd *, u_int)); 78b40ce416SJulian Elischer static int selscan __P((struct thread *, fd_mask **, fd_mask **, int)); 79b40ce416SJulian Elischer static int dofileread __P((struct thread *, struct file *, int, void *, 808fe387abSDmitrij Tejblum size_t, off_t, int)); 81b40ce416SJulian Elischer static int dofilewrite __P((struct thread *, struct file *, int, 828fe387abSDmitrij Tejblum 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 6998cb96f20SPeter Wemm static int nselcoll; /* Select collisions since boot */ 700265fc98fSSeigo Tanimura struct cv selwait; 7018cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 702df8bae1dSRodney W. Grimes 703df8bae1dSRodney W. Grimes /* 704df8bae1dSRodney W. Grimes * Select system call. 705df8bae1dSRodney W. Grimes */ 706d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 707df8bae1dSRodney W. Grimes struct select_args { 708b08f7993SSujal Patel int nd; 709df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 710df8bae1dSRodney W. Grimes struct timeval *tv; 711df8bae1dSRodney W. Grimes }; 712d2d3e875SBruce Evans #endif 713ad2edad9SMatthew Dillon /* 714ad2edad9SMatthew Dillon * MPSAFE 715ad2edad9SMatthew Dillon */ 71626f9a767SRodney W. Grimes int 717b40ce416SJulian Elischer select(td, uap) 718b40ce416SJulian Elischer register struct thread *td; 719df8bae1dSRodney W. Grimes register struct select_args *uap; 720df8bae1dSRodney W. Grimes { 721426da3bcSAlfred Perlstein struct filedesc *fdp; 722d5e4d7e1SBruce Evans /* 723d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 724d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 725d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 726d5e4d7e1SBruce Evans * of 256. 727d5e4d7e1SBruce Evans */ 728d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 729265fc98fSSeigo Tanimura fd_mask s_heldbits[howmany(2048, NFDBITS)]; 730eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 73100af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 732265fc98fSSeigo Tanimura int ncoll, error, timo, i; 733d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 734df8bae1dSRodney W. Grimes 735b08f7993SSujal Patel if (uap->nd < 0) 736acbfbfeaSSujal Patel return (EINVAL); 737426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 738ad2edad9SMatthew Dillon mtx_lock(&Giant); 739426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 740ad2edad9SMatthew Dillon 741b40ce416SJulian Elischer if (uap->nd > td->td_proc->p_fd->fd_nfiles) 742b40ce416SJulian Elischer uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 743426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 744b08f7993SSujal Patel 745d5e4d7e1SBruce Evans /* 746d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 747d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 748d5e4d7e1SBruce Evans */ 749d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 750d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 751d5e4d7e1SBruce Evans nbufbytes = 0; 752d5e4d7e1SBruce Evans if (uap->in != NULL) 753d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 754d5e4d7e1SBruce Evans if (uap->ou != NULL) 755d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 756d5e4d7e1SBruce Evans if (uap->ex != NULL) 757d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 758d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 759d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 760d5e4d7e1SBruce Evans else 761d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 762b08f7993SSujal Patel 763b08f7993SSujal Patel /* 764d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 765d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 766d5e4d7e1SBruce Evans * together. 767b08f7993SSujal Patel */ 768d5e4d7e1SBruce Evans sbp = selbits; 769df8bae1dSRodney W. Grimes #define getbits(name, x) \ 770d5e4d7e1SBruce Evans do { \ 771d5e4d7e1SBruce Evans if (uap->name == NULL) \ 772d5e4d7e1SBruce Evans ibits[x] = NULL; \ 773d5e4d7e1SBruce Evans else { \ 774d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 775d5e4d7e1SBruce Evans obits[x] = sbp; \ 776d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 777d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 778265fc98fSSeigo Tanimura if (error != 0) \ 779265fc98fSSeigo Tanimura goto done_noproclock; \ 780e04ac2feSJohn Baldwin } \ 781d5e4d7e1SBruce Evans } while (0) 782df8bae1dSRodney W. Grimes getbits(in, 0); 783df8bae1dSRodney W. Grimes getbits(ou, 1); 784df8bae1dSRodney W. Grimes getbits(ex, 2); 785df8bae1dSRodney W. Grimes #undef getbits 786d5e4d7e1SBruce Evans if (nbufbytes != 0) 787d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 788df8bae1dSRodney W. Grimes 789df8bae1dSRodney W. Grimes if (uap->tv) { 790df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 791df8bae1dSRodney W. Grimes sizeof (atv)); 792265fc98fSSeigo Tanimura if (error) 793265fc98fSSeigo Tanimura goto done_noproclock; 794df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 795df8bae1dSRodney W. Grimes error = EINVAL; 796265fc98fSSeigo Tanimura goto done_noproclock; 797df8bae1dSRodney W. Grimes } 798c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 79900af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8009c386f6bSJohn Baldwin } else { 80100af9731SPoul-Henning Kamp atv.tv_sec = 0; 8029c386f6bSJohn Baldwin atv.tv_usec = 0; 8039c386f6bSJohn Baldwin } 80400af9731SPoul-Henning Kamp timo = 0; 805b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 806df8bae1dSRodney W. Grimes retry: 807df8bae1dSRodney W. Grimes ncoll = nselcoll; 808fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 809b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 810fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 811b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 812b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 813b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 814b40ce416SJulian Elischer if (error || td->td_retval[0]) 815df8bae1dSRodney W. Grimes goto done; 8164da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 817c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 8181b369704SSeigo Tanimura if (timevalcmp(&rtv, &atv, >=)) { 8191b369704SSeigo Tanimura /* 8201b369704SSeigo Tanimura * An event of our interest may occur during locking a process. 8211b369704SSeigo Tanimura * In order to avoid missing the event that occured during locking 822b40ce416SJulian Elischer * the process, test TDF_SELECT and rescan file descriptors if 8231b369704SSeigo Tanimura * necessary. 8241b369704SSeigo Tanimura */ 825fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 826b40ce416SJulian Elischer if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 8271b369704SSeigo Tanimura ncoll = nselcoll; 828b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 829fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 830b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 831b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 832b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 833fea2ab83SJohn Baldwin } else 834fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 835df8bae1dSRodney W. Grimes goto done; 8361b369704SSeigo Tanimura } 83700af9731SPoul-Henning Kamp ttv = atv; 83800af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 83900af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 84000af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 841df8bae1dSRodney W. Grimes } 842fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 843b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 844fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 845bfbbc4aaSJason Evans 846265fc98fSSeigo Tanimura if (timo > 0) 847b40ce416SJulian Elischer error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo); 848265fc98fSSeigo Tanimura else 849b40ce416SJulian Elischer error = cv_wait_sig(&selwait, &td->td_proc->p_mtx); 850bfbbc4aaSJason Evans 851df8bae1dSRodney W. Grimes if (error == 0) 852df8bae1dSRodney W. Grimes goto retry; 853265fc98fSSeigo Tanimura 854df8bae1dSRodney W. Grimes done: 855fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 856b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 857fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 858b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 859265fc98fSSeigo Tanimura done_noproclock: 860df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 861df8bae1dSRodney W. Grimes if (error == ERESTART) 862df8bae1dSRodney W. Grimes error = EINTR; 863df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 864df8bae1dSRodney W. Grimes error = 0; 865df8bae1dSRodney W. Grimes #define putbits(name, x) \ 866d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 867df8bae1dSRodney W. Grimes error = error2; 868df8bae1dSRodney W. Grimes if (error == 0) { 869df8bae1dSRodney W. Grimes int error2; 870df8bae1dSRodney W. Grimes 871df8bae1dSRodney W. Grimes putbits(in, 0); 872df8bae1dSRodney W. Grimes putbits(ou, 1); 873df8bae1dSRodney W. Grimes putbits(ex, 2); 874df8bae1dSRodney W. Grimes #undef putbits 875df8bae1dSRodney W. Grimes } 876d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 877d5e4d7e1SBruce Evans free(selbits, M_SELECT); 878ad2edad9SMatthew Dillon 879ad2edad9SMatthew Dillon mtx_unlock(&Giant); 880df8bae1dSRodney W. Grimes return (error); 881df8bae1dSRodney W. Grimes } 882df8bae1dSRodney W. Grimes 883265fc98fSSeigo Tanimura static int 884b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 885b40ce416SJulian Elischer struct thread *td; 886b08f7993SSujal Patel fd_mask **ibits, **obits; 887cb226aaaSPoul-Henning Kamp int nfd; 888df8bae1dSRodney W. Grimes { 889f082218cSPeter Wemm int msk, i, fd; 890f082218cSPeter Wemm fd_mask bits; 891df8bae1dSRodney W. Grimes struct file *fp; 892df8bae1dSRodney W. Grimes int n = 0; 8932087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 89442d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 895eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 896df8bae1dSRodney W. Grimes 897eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 898df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 899d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 900d5e4d7e1SBruce Evans continue; 901df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 902b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 903f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 904f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 905f082218cSPeter Wemm if (!(bits & 1)) 906f082218cSPeter Wemm continue; 907eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 908eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 909df8bae1dSRodney W. Grimes return (EBADF); 910eb209311SAlfred Perlstein } 911b40ce416SJulian Elischer if (fo_poll(fp, flag[msk], fp->f_cred, td)) { 912b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 913f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 914df8bae1dSRodney W. Grimes n++; 915df8bae1dSRodney W. Grimes } 916df8bae1dSRodney W. Grimes } 917df8bae1dSRodney W. Grimes } 918df8bae1dSRodney W. Grimes } 919eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 920b40ce416SJulian Elischer td->td_retval[0] = n; 921df8bae1dSRodney W. Grimes return (0); 922df8bae1dSRodney W. Grimes } 923df8bae1dSRodney W. Grimes 92442d11757SPeter Wemm /* 92542d11757SPeter Wemm * Poll system call. 92642d11757SPeter Wemm */ 92742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 92842d11757SPeter Wemm struct poll_args { 92942d11757SPeter Wemm struct pollfd *fds; 93042d11757SPeter Wemm u_int nfds; 93142d11757SPeter Wemm int timeout; 93242d11757SPeter Wemm }; 93342d11757SPeter Wemm #endif 934ad2edad9SMatthew Dillon /* 935ad2edad9SMatthew Dillon * MPSAFE 936ad2edad9SMatthew Dillon */ 93742d11757SPeter Wemm int 938b40ce416SJulian Elischer poll(td, uap) 939b40ce416SJulian Elischer struct thread *td; 940ea0237edSJonathan Lemon struct poll_args *uap; 94142d11757SPeter Wemm { 94242d11757SPeter Wemm caddr_t bits; 94342d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 94400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 945265fc98fSSeigo Tanimura int ncoll, error = 0, timo; 946ea0237edSJonathan Lemon u_int nfds; 94742d11757SPeter Wemm size_t ni; 94842d11757SPeter Wemm 94989b71647SPeter Wemm nfds = SCARG(uap, nfds); 950ad2edad9SMatthew Dillon 951ad2edad9SMatthew Dillon mtx_lock(&Giant); 95289b71647SPeter Wemm /* 9532bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9542bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9552bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9562bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9572bd5ac33SPeter Wemm * safe, but not overly restrictive. 95889b71647SPeter Wemm */ 959b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 960b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 961ad2edad9SMatthew Dillon error = EINVAL; 962ad2edad9SMatthew Dillon goto done2; 963ad2edad9SMatthew Dillon } 96489b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 96542d11757SPeter Wemm if (ni > sizeof(smallbits)) 96642d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 96742d11757SPeter Wemm else 96842d11757SPeter Wemm bits = smallbits; 96942d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 97042d11757SPeter Wemm if (error) 971265fc98fSSeigo Tanimura goto done_noproclock; 97242d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 97342d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 97442d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 97542d11757SPeter Wemm if (itimerfix(&atv)) { 97642d11757SPeter Wemm error = EINVAL; 977265fc98fSSeigo Tanimura goto done_noproclock; 97842d11757SPeter Wemm } 979c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 98000af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9819c386f6bSJohn Baldwin } else { 98200af9731SPoul-Henning Kamp atv.tv_sec = 0; 9839c386f6bSJohn Baldwin atv.tv_usec = 0; 9849c386f6bSJohn Baldwin } 98500af9731SPoul-Henning Kamp timo = 0; 986b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 98742d11757SPeter Wemm retry: 98842d11757SPeter Wemm ncoll = nselcoll; 989fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 990b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 991fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 992b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 993b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 994b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 995b40ce416SJulian Elischer if (error || td->td_retval[0]) 99642d11757SPeter Wemm goto done; 9974da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 998c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 9991b369704SSeigo Tanimura if (timevalcmp(&rtv, &atv, >=)) { 10001b369704SSeigo Tanimura /* 10011b369704SSeigo Tanimura * An event of our interest may occur during locking a process. 10021b369704SSeigo Tanimura * In order to avoid missing the event that occured during locking 1003b40ce416SJulian Elischer * the process, test TDF_SELECT and rescan file descriptors if 10041b369704SSeigo Tanimura * necessary. 10051b369704SSeigo Tanimura */ 1006fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1007b40ce416SJulian Elischer if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 10081b369704SSeigo Tanimura ncoll = nselcoll; 1009b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 1010fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 1011b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1012b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 1013b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 1014fea2ab83SJohn Baldwin } else 1015fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 101642d11757SPeter Wemm goto done; 10171b369704SSeigo Tanimura } 101800af9731SPoul-Henning Kamp ttv = atv; 101900af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 102000af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 102100af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 102242d11757SPeter Wemm } 1023fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1024b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1025fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 1026265fc98fSSeigo Tanimura if (timo > 0) 1027b40ce416SJulian Elischer error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo); 1028265fc98fSSeigo Tanimura else 1029b40ce416SJulian Elischer error = cv_wait_sig(&selwait, &td->td_proc->p_mtx); 103042d11757SPeter Wemm if (error == 0) 103142d11757SPeter Wemm goto retry; 1032265fc98fSSeigo Tanimura 103342d11757SPeter Wemm done: 1034fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1035b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1036fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 1037b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1038265fc98fSSeigo Tanimura done_noproclock: 103942d11757SPeter Wemm /* poll is not restarted after signals... */ 104042d11757SPeter Wemm if (error == ERESTART) 104142d11757SPeter Wemm error = EINTR; 104242d11757SPeter Wemm if (error == EWOULDBLOCK) 104342d11757SPeter Wemm error = 0; 104442d11757SPeter Wemm if (error == 0) { 104542d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 104642d11757SPeter Wemm if (error) 104742d11757SPeter Wemm goto out; 104842d11757SPeter Wemm } 104942d11757SPeter Wemm out: 105042d11757SPeter Wemm if (ni > sizeof(smallbits)) 105142d11757SPeter Wemm free(bits, M_TEMP); 1052ad2edad9SMatthew Dillon done2: 1053ad2edad9SMatthew Dillon mtx_unlock(&Giant); 105442d11757SPeter Wemm return (error); 105542d11757SPeter Wemm } 105642d11757SPeter Wemm 105742d11757SPeter Wemm static int 1058b40ce416SJulian Elischer pollscan(td, fds, nfd) 1059b40ce416SJulian Elischer struct thread *td; 106042d11757SPeter Wemm struct pollfd *fds; 1061ea0237edSJonathan Lemon u_int nfd; 106242d11757SPeter Wemm { 1063b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 106442d11757SPeter Wemm int i; 106542d11757SPeter Wemm struct file *fp; 106642d11757SPeter Wemm int n = 0; 106742d11757SPeter Wemm 1068426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1069eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1070337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 107142d11757SPeter Wemm fds->revents = POLLNVAL; 107242d11757SPeter Wemm n++; 1073337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1074337c9691SJordan K. Hubbard fds->revents = 0; 107542d11757SPeter Wemm } else { 107642d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1077279d7226SMatthew Dillon if (fp == NULL) { 107842d11757SPeter Wemm fds->revents = POLLNVAL; 107942d11757SPeter Wemm n++; 108042d11757SPeter Wemm } else { 10812087c896SBruce Evans /* 10822087c896SBruce Evans * Note: backend also returns POLLHUP and 10832087c896SBruce Evans * POLLERR if appropriate. 10842087c896SBruce Evans */ 108513ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1086b40ce416SJulian Elischer fp->f_cred, td); 108742d11757SPeter Wemm if (fds->revents != 0) 108842d11757SPeter Wemm n++; 108942d11757SPeter Wemm } 109042d11757SPeter Wemm } 109142d11757SPeter Wemm } 1092eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1093b40ce416SJulian Elischer td->td_retval[0] = n; 109442d11757SPeter Wemm return (0); 109542d11757SPeter Wemm } 109642d11757SPeter Wemm 109742d11757SPeter Wemm /* 109842d11757SPeter Wemm * OpenBSD poll system call. 109942d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 110042d11757SPeter Wemm */ 110142d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 110242d11757SPeter Wemm struct openbsd_poll_args { 110342d11757SPeter Wemm struct pollfd *fds; 110442d11757SPeter Wemm u_int nfds; 110542d11757SPeter Wemm int timeout; 110642d11757SPeter Wemm }; 110742d11757SPeter Wemm #endif 1108ad2edad9SMatthew Dillon /* 1109ad2edad9SMatthew Dillon * MPSAFE 1110ad2edad9SMatthew Dillon */ 111142d11757SPeter Wemm int 1112b40ce416SJulian Elischer openbsd_poll(td, uap) 1113b40ce416SJulian Elischer register struct thread *td; 111442d11757SPeter Wemm register struct openbsd_poll_args *uap; 111542d11757SPeter Wemm { 1116b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 111742d11757SPeter Wemm } 111842d11757SPeter Wemm 1119df8bae1dSRodney W. Grimes /*ARGSUSED*/ 112026f9a767SRodney W. Grimes int 1121b40ce416SJulian Elischer seltrue(dev, events, td) 1122df8bae1dSRodney W. Grimes dev_t dev; 112342d11757SPeter Wemm int events; 1124b40ce416SJulian Elischer struct thread *td; 1125df8bae1dSRodney W. Grimes { 1126df8bae1dSRodney W. Grimes 112742d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1128df8bae1dSRodney W. Grimes } 1129df8bae1dSRodney W. Grimes 1130b40ce416SJulian Elischer static int 1131b40ce416SJulian Elischer find_thread_in_proc(struct proc *p, struct thread *td) 1132b40ce416SJulian Elischer { 1133b40ce416SJulian Elischer struct thread *td2; 1134b40ce416SJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 1135b40ce416SJulian Elischer if (td2 == td) { 1136b40ce416SJulian Elischer return (1); 1137b40ce416SJulian Elischer } 1138b40ce416SJulian Elischer } 1139b40ce416SJulian Elischer return (0); 1140b40ce416SJulian Elischer } 1141b40ce416SJulian Elischer 1142df8bae1dSRodney W. Grimes /* 1143df8bae1dSRodney W. Grimes * Record a select request. 1144df8bae1dSRodney W. Grimes */ 1145df8bae1dSRodney W. Grimes void 1146df8bae1dSRodney W. Grimes selrecord(selector, sip) 1147b40ce416SJulian Elischer struct thread *selector; 1148df8bae1dSRodney W. Grimes struct selinfo *sip; 1149df8bae1dSRodney W. Grimes { 1150df8bae1dSRodney W. Grimes struct proc *p; 1151df8bae1dSRodney W. Grimes pid_t mypid; 1152df8bae1dSRodney W. Grimes 1153b40ce416SJulian Elischer mypid = selector->td_proc->p_pid; 1154b40ce416SJulian Elischer if ((sip->si_pid == mypid) && 1155b40ce416SJulian Elischer (sip->si_thread == selector)) { /* XXXKSE should be an ID? */ 1156df8bae1dSRodney W. Grimes return; 1157b40ce416SJulian Elischer } 1158b40ce416SJulian Elischer if (sip->si_pid && 1159b40ce416SJulian Elischer (p = pfind(sip->si_pid)) && 1160b40ce416SJulian Elischer (find_thread_in_proc(p, sip->si_thread))) { 11619ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1162b40ce416SJulian Elischer if (sip->si_thread->td_wchan == (caddr_t)&selwait) { 11639ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 116433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1165df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 1166e04ac2feSJohn Baldwin return; 1167e04ac2feSJohn Baldwin } 11689ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 116933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1170e04ac2feSJohn Baldwin } 1171df8bae1dSRodney W. Grimes sip->si_pid = mypid; 1172b40ce416SJulian Elischer sip->si_thread = selector; 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) 1180df8bae1dSRodney W. Grimes register struct selinfo *sip; 1181df8bae1dSRodney W. Grimes { 1182b40ce416SJulian Elischer struct thread *td; 1183df8bae1dSRodney W. Grimes register struct proc *p; 1184df8bae1dSRodney W. Grimes 1185df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 1186df8bae1dSRodney W. Grimes return; 1187df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 1188df8bae1dSRodney W. Grimes nselcoll++; 1189df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1190265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1191df8bae1dSRodney W. Grimes } 1192df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 1193df8bae1dSRodney W. Grimes sip->si_pid = 0; 1194b40ce416SJulian Elischer td = sip->si_thread; 1195df8bae1dSRodney W. Grimes if (p != NULL) { 1196b40ce416SJulian Elischer if (!find_thread_in_proc(p, td)) { 1197b40ce416SJulian Elischer PROC_UNLOCK(p); /* lock is in pfind() */; 1198b40ce416SJulian Elischer return; 1199b40ce416SJulian Elischer } 12009ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1201b40ce416SJulian Elischer if (td->td_wchan == (caddr_t)&selwait) { 1202b40ce416SJulian Elischer if (td->td_proc->p_stat == SSLEEP) 1203b40ce416SJulian Elischer setrunnable(td); 1204df8bae1dSRodney W. Grimes else 1205b40ce416SJulian Elischer cv_waitq_remove(td); 120633a9ed9dSJohn Baldwin } else 1207b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 120833a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 1209b40ce416SJulian Elischer PROC_UNLOCK(p); /* Lock is in pfind() */ 1210e04ac2feSJohn Baldwin } 1211df8bae1dSRodney W. Grimes } 1212265fc98fSSeigo Tanimura 1213265fc98fSSeigo Tanimura static void selectinit __P((void *)); 1214265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1215265fc98fSSeigo Tanimura 1216265fc98fSSeigo Tanimura /* ARGSUSED*/ 1217265fc98fSSeigo Tanimura static void 1218265fc98fSSeigo Tanimura selectinit(dummy) 1219265fc98fSSeigo Tanimura void *dummy; 1220265fc98fSSeigo Tanimura { 1221265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 1222265fc98fSSeigo Tanimura } 1223