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 pollholddrop __P((struct thread *, struct pollfd *, u_int, int)); 79b40ce416SJulian Elischer static int selscan __P((struct thread *, fd_mask **, fd_mask **, int)); 80b40ce416SJulian Elischer static int selholddrop __P((struct thread *, fd_mask *, fd_mask *, int, int)); 81b40ce416SJulian Elischer static int dofileread __P((struct thread *, struct file *, int, void *, 828fe387abSDmitrij Tejblum size_t, off_t, int)); 83b40ce416SJulian Elischer static int dofilewrite __P((struct thread *, struct file *, int, 848fe387abSDmitrij Tejblum const void *, size_t, off_t, int)); 858fe387abSDmitrij Tejblum 86426da3bcSAlfred Perlstein struct file* 87426da3bcSAlfred Perlstein holdfp(fdp, fd, flag) 88426da3bcSAlfred Perlstein struct filedesc* fdp; 89426da3bcSAlfred Perlstein int fd, flag; 90426da3bcSAlfred Perlstein { 91426da3bcSAlfred Perlstein struct file* fp; 92426da3bcSAlfred Perlstein 93426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 94426da3bcSAlfred Perlstein if (((u_int)fd) >= fdp->fd_nfiles || 95426da3bcSAlfred Perlstein (fp = fdp->fd_ofiles[fd]) == NULL) { 96426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 97426da3bcSAlfred Perlstein return (NULL); 98426da3bcSAlfred Perlstein } 99426da3bcSAlfred Perlstein FILE_LOCK(fp); 100426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 101426da3bcSAlfred Perlstein if ((fp->f_flag & flag) == 0) { 102426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 103426da3bcSAlfred Perlstein return (NULL); 104426da3bcSAlfred Perlstein } 105426da3bcSAlfred Perlstein fp->f_count++; 106426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 107426da3bcSAlfred Perlstein return (fp); 108426da3bcSAlfred Perlstein } 109426da3bcSAlfred Perlstein 110df8bae1dSRodney W. Grimes /* 111df8bae1dSRodney W. Grimes * Read system call. 112df8bae1dSRodney W. Grimes */ 113d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 114df8bae1dSRodney W. Grimes struct read_args { 115df8bae1dSRodney W. Grimes int fd; 116134e06feSBruce Evans void *buf; 117134e06feSBruce Evans size_t nbyte; 118df8bae1dSRodney W. Grimes }; 119d2d3e875SBruce Evans #endif 120ad2edad9SMatthew Dillon /* 121ad2edad9SMatthew Dillon * MPSAFE 122ad2edad9SMatthew Dillon */ 12326f9a767SRodney W. Grimes int 124b40ce416SJulian Elischer read(td, uap) 125b40ce416SJulian Elischer struct thread *td; 126b064d43dSMatthew Dillon struct read_args *uap; 127df8bae1dSRodney W. Grimes { 128b064d43dSMatthew Dillon struct file *fp; 129279d7226SMatthew Dillon int error; 130df8bae1dSRodney W. Grimes 131ad2edad9SMatthew Dillon mtx_lock(&Giant); 132b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) == 0) { 133b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, 134ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 135b40ce416SJulian Elischer fdrop(fp, td); 136ad2edad9SMatthew Dillon } 137ad2edad9SMatthew Dillon mtx_unlock(&Giant); 138279d7226SMatthew Dillon return(error); 139df8bae1dSRodney W. Grimes } 140df8bae1dSRodney W. Grimes 141df8bae1dSRodney W. Grimes /* 1428fe387abSDmitrij Tejblum * Pread system call 1434160ccd9SAlan Cox */ 1444160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1454160ccd9SAlan Cox struct pread_args { 1464160ccd9SAlan Cox int fd; 1474160ccd9SAlan Cox void *buf; 1484160ccd9SAlan Cox size_t nbyte; 1498fe387abSDmitrij Tejblum int pad; 1504160ccd9SAlan Cox off_t offset; 1514160ccd9SAlan Cox }; 1524160ccd9SAlan Cox #endif 153ad2edad9SMatthew Dillon /* 154ad2edad9SMatthew Dillon * MPSAFE 155ad2edad9SMatthew Dillon */ 1564160ccd9SAlan Cox int 157b40ce416SJulian Elischer pread(td, uap) 158b40ce416SJulian Elischer struct thread *td; 159b064d43dSMatthew Dillon struct pread_args *uap; 1604160ccd9SAlan Cox { 161b064d43dSMatthew Dillon struct file *fp; 162279d7226SMatthew Dillon int error; 1638fe387abSDmitrij Tejblum 164426da3bcSAlfred Perlstein fp = holdfp(td->td_proc->p_fd, uap->fd, FREAD); 165426da3bcSAlfred Perlstein if (fp == NULL) 166426da3bcSAlfred Perlstein return (EBADF); 167426da3bcSAlfred Perlstein if (fp->f_type != DTYPE_VNODE) { 168b064d43dSMatthew Dillon error = ESPIPE; 169426da3bcSAlfred Perlstein } else { 170426da3bcSAlfred Perlstein mtx_lock(&Giant); 171426da3bcSAlfred Perlstein error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte, 172426da3bcSAlfred Perlstein uap->offset, FOF_OFFSET); 173426da3bcSAlfred Perlstein mtx_unlock(&Giant); 174b064d43dSMatthew Dillon } 175b40ce416SJulian Elischer fdrop(fp, td); 176279d7226SMatthew Dillon return(error); 1778fe387abSDmitrij Tejblum } 1788fe387abSDmitrij Tejblum 1798fe387abSDmitrij Tejblum /* 1808fe387abSDmitrij Tejblum * Code common for read and pread 1818fe387abSDmitrij Tejblum */ 1828fe387abSDmitrij Tejblum int 183b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 184b40ce416SJulian Elischer struct thread *td; 1858fe387abSDmitrij Tejblum struct file *fp; 1868fe387abSDmitrij Tejblum int fd, flags; 1878fe387abSDmitrij Tejblum void *buf; 1888fe387abSDmitrij Tejblum size_t nbyte; 1898fe387abSDmitrij Tejblum off_t offset; 1908fe387abSDmitrij Tejblum { 1914160ccd9SAlan Cox struct uio auio; 1924160ccd9SAlan Cox struct iovec aiov; 1934160ccd9SAlan Cox long cnt, error = 0; 1944160ccd9SAlan Cox #ifdef KTRACE 1954160ccd9SAlan Cox struct iovec ktriov; 19642ebfbf2SBrian Feldman struct uio ktruio; 1973c89e357SBrian Feldman int didktr = 0; 1984160ccd9SAlan Cox #endif 1994160ccd9SAlan Cox 2008fe387abSDmitrij Tejblum aiov.iov_base = (caddr_t)buf; 2018fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 2024160ccd9SAlan Cox auio.uio_iov = &aiov; 2034160ccd9SAlan Cox auio.uio_iovcnt = 1; 2048fe387abSDmitrij Tejblum auio.uio_offset = offset; 2058fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 2064160ccd9SAlan Cox return (EINVAL); 2078fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 2084160ccd9SAlan Cox auio.uio_rw = UIO_READ; 2094160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 210b40ce416SJulian Elischer auio.uio_td = td; 2114160ccd9SAlan Cox #ifdef KTRACE 2124160ccd9SAlan Cox /* 2134160ccd9SAlan Cox * if tracing, save a copy of iovec 2144160ccd9SAlan Cox */ 215b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 2164160ccd9SAlan Cox ktriov = aiov; 21742ebfbf2SBrian Feldman ktruio = auio; 2183c89e357SBrian Feldman didktr = 1; 21942ebfbf2SBrian Feldman } 2204160ccd9SAlan Cox #endif 2218fe387abSDmitrij Tejblum cnt = nbyte; 222279d7226SMatthew Dillon 223b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) { 2244160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 2254160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 2264160ccd9SAlan Cox error = 0; 227279d7226SMatthew Dillon } 2284160ccd9SAlan Cox cnt -= auio.uio_resid; 2294160ccd9SAlan Cox #ifdef KTRACE 2303c89e357SBrian Feldman if (didktr && error == 0) { 23142ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 23242ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 233b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_READ, &ktruio, error); 23442ebfbf2SBrian Feldman } 2354160ccd9SAlan Cox #endif 236b40ce416SJulian Elischer td->td_retval[0] = cnt; 2374160ccd9SAlan Cox return (error); 2384160ccd9SAlan Cox } 2394160ccd9SAlan Cox 2404160ccd9SAlan Cox /* 241df8bae1dSRodney W. Grimes * Scatter read system call. 242df8bae1dSRodney W. Grimes */ 243d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 244df8bae1dSRodney W. Grimes struct readv_args { 2457147b19dSBruce Evans int fd; 246df8bae1dSRodney W. Grimes struct iovec *iovp; 247df8bae1dSRodney W. Grimes u_int iovcnt; 248df8bae1dSRodney W. Grimes }; 249d2d3e875SBruce Evans #endif 250ad2edad9SMatthew Dillon /* 251ad2edad9SMatthew Dillon * MPSAFE 252ad2edad9SMatthew Dillon */ 25326f9a767SRodney W. Grimes int 254b40ce416SJulian Elischer readv(td, uap) 255b40ce416SJulian Elischer struct thread *td; 256b064d43dSMatthew Dillon struct readv_args *uap; 257df8bae1dSRodney W. Grimes { 258b064d43dSMatthew Dillon struct file *fp; 259df8bae1dSRodney W. Grimes struct uio auio; 260b064d43dSMatthew Dillon struct iovec *iov; 261df8bae1dSRodney W. Grimes struct iovec *needfree; 262df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 263df8bae1dSRodney W. Grimes long i, cnt, error = 0; 264df8bae1dSRodney W. Grimes u_int iovlen; 265df8bae1dSRodney W. Grimes #ifdef KTRACE 266df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 26742ebfbf2SBrian Feldman struct uio ktruio; 268df8bae1dSRodney W. Grimes #endif 269ad2edad9SMatthew Dillon mtx_lock(&Giant); 270df8bae1dSRodney W. Grimes 271b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) != 0) 272ad2edad9SMatthew Dillon goto done2; 273df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 274df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 275df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 276ad2edad9SMatthew Dillon if (uap->iovcnt > UIO_MAXIOV) { 277ad2edad9SMatthew Dillon error = EINVAL; 278ad2edad9SMatthew Dillon goto done2; 279ad2edad9SMatthew Dillon } 280df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 281df8bae1dSRodney W. Grimes needfree = iov; 282df8bae1dSRodney W. Grimes } else { 283df8bae1dSRodney W. Grimes iov = aiov; 284df8bae1dSRodney W. Grimes needfree = NULL; 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes auio.uio_iov = iov; 287df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 288df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 289df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 290b40ce416SJulian Elischer auio.uio_td = td; 2912c1011f7SJohn Dyson auio.uio_offset = -1; 292bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 293df8bae1dSRodney W. Grimes goto done; 294df8bae1dSRodney W. Grimes auio.uio_resid = 0; 295df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 296069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 297df8bae1dSRodney W. Grimes error = EINVAL; 298df8bae1dSRodney W. Grimes goto done; 299df8bae1dSRodney W. Grimes } 300069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 301df8bae1dSRodney W. Grimes iov++; 302df8bae1dSRodney W. Grimes } 303df8bae1dSRodney W. Grimes #ifdef KTRACE 304df8bae1dSRodney W. Grimes /* 305df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 306df8bae1dSRodney W. Grimes */ 307b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 308df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 309df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 31042ebfbf2SBrian Feldman ktruio = auio; 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes #endif 313df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 314b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) { 315df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 316df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 317df8bae1dSRodney W. Grimes error = 0; 318279d7226SMatthew Dillon } 319df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 320df8bae1dSRodney W. Grimes #ifdef KTRACE 321df8bae1dSRodney W. Grimes if (ktriov != NULL) { 32242ebfbf2SBrian Feldman if (error == 0) { 32342ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 32442ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 325b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio, 32642ebfbf2SBrian Feldman error); 32742ebfbf2SBrian Feldman } 328df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes #endif 331b40ce416SJulian Elischer td->td_retval[0] = cnt; 332df8bae1dSRodney W. Grimes done: 333b40ce416SJulian Elischer fdrop(fp, td); 334df8bae1dSRodney W. Grimes if (needfree) 335df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 336ad2edad9SMatthew Dillon done2: 337ad2edad9SMatthew Dillon mtx_unlock(&Giant); 338df8bae1dSRodney W. Grimes return (error); 339df8bae1dSRodney W. Grimes } 340df8bae1dSRodney W. Grimes 341df8bae1dSRodney W. Grimes /* 342df8bae1dSRodney W. Grimes * Write system call 343df8bae1dSRodney W. Grimes */ 344d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 345df8bae1dSRodney W. Grimes struct write_args { 346df8bae1dSRodney W. Grimes int fd; 347134e06feSBruce Evans const void *buf; 348134e06feSBruce Evans size_t nbyte; 349df8bae1dSRodney W. Grimes }; 350d2d3e875SBruce Evans #endif 351ad2edad9SMatthew Dillon /* 352ad2edad9SMatthew Dillon * MPSAFE 353ad2edad9SMatthew Dillon */ 35426f9a767SRodney W. Grimes int 355b40ce416SJulian Elischer write(td, uap) 356b40ce416SJulian Elischer struct thread *td; 357b064d43dSMatthew Dillon struct write_args *uap; 358df8bae1dSRodney W. Grimes { 359b064d43dSMatthew Dillon struct file *fp; 360279d7226SMatthew Dillon int error; 361df8bae1dSRodney W. Grimes 362ad2edad9SMatthew Dillon mtx_lock(&Giant); 363b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 364b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 365ad2edad9SMatthew Dillon (off_t)-1, 0); 366b40ce416SJulian Elischer fdrop(fp, td); 367ad2edad9SMatthew Dillon } else { 368b064d43dSMatthew Dillon error = EBADF; /* XXX this can't be right */ 369ad2edad9SMatthew Dillon } 370ad2edad9SMatthew Dillon mtx_unlock(&Giant); 371279d7226SMatthew Dillon return(error); 372df8bae1dSRodney W. Grimes } 373df8bae1dSRodney W. Grimes 374df8bae1dSRodney W. Grimes /* 3758fe387abSDmitrij Tejblum * Pwrite system call 3764160ccd9SAlan Cox */ 3774160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3784160ccd9SAlan Cox struct pwrite_args { 3794160ccd9SAlan Cox int fd; 3804160ccd9SAlan Cox const void *buf; 3814160ccd9SAlan Cox size_t nbyte; 3828fe387abSDmitrij Tejblum int pad; 3834160ccd9SAlan Cox off_t offset; 3844160ccd9SAlan Cox }; 3854160ccd9SAlan Cox #endif 386ad2edad9SMatthew Dillon /* 387ad2edad9SMatthew Dillon * MPSAFE 388ad2edad9SMatthew Dillon */ 3894160ccd9SAlan Cox int 390b40ce416SJulian Elischer pwrite(td, uap) 391b40ce416SJulian Elischer struct thread *td; 392b064d43dSMatthew Dillon struct pwrite_args *uap; 3934160ccd9SAlan Cox { 394b064d43dSMatthew Dillon struct file *fp; 395279d7226SMatthew Dillon int error; 3968fe387abSDmitrij Tejblum 397ad2edad9SMatthew Dillon mtx_lock(&Giant); 398b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 399b064d43dSMatthew Dillon if (fp->f_type == DTYPE_VNODE) { 400b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 401b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 402b064d43dSMatthew Dillon } else { 403279d7226SMatthew Dillon error = ESPIPE; 404b064d43dSMatthew Dillon } 405b40ce416SJulian Elischer fdrop(fp, td); 406279d7226SMatthew Dillon } else { 407b064d43dSMatthew Dillon error = EBADF; /* this can't be right */ 408ad2edad9SMatthew Dillon } 409279d7226SMatthew Dillon return(error); 4108fe387abSDmitrij Tejblum } 4118fe387abSDmitrij Tejblum 4128fe387abSDmitrij Tejblum static int 413b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 414b40ce416SJulian Elischer struct thread *td; 4158fe387abSDmitrij Tejblum struct file *fp; 4168fe387abSDmitrij Tejblum int fd, flags; 4178fe387abSDmitrij Tejblum const void *buf; 4188fe387abSDmitrij Tejblum size_t nbyte; 4198fe387abSDmitrij Tejblum off_t offset; 4208fe387abSDmitrij Tejblum { 4214160ccd9SAlan Cox struct uio auio; 4224160ccd9SAlan Cox struct iovec aiov; 4234160ccd9SAlan Cox long cnt, error = 0; 4244160ccd9SAlan Cox #ifdef KTRACE 4254160ccd9SAlan Cox struct iovec ktriov; 42642ebfbf2SBrian Feldman struct uio ktruio; 4273c89e357SBrian Feldman int didktr = 0; 4284160ccd9SAlan Cox #endif 4294160ccd9SAlan Cox 430b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 4318fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 4324160ccd9SAlan Cox auio.uio_iov = &aiov; 4334160ccd9SAlan Cox auio.uio_iovcnt = 1; 4348fe387abSDmitrij Tejblum auio.uio_offset = offset; 4358fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 4364160ccd9SAlan Cox return (EINVAL); 4378fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 4384160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 4394160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 440b40ce416SJulian Elischer auio.uio_td = td; 4414160ccd9SAlan Cox #ifdef KTRACE 4424160ccd9SAlan Cox /* 44342ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4444160ccd9SAlan Cox */ 445b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 4464160ccd9SAlan Cox ktriov = aiov; 44742ebfbf2SBrian Feldman ktruio = auio; 4483c89e357SBrian Feldman didktr = 1; 44942ebfbf2SBrian Feldman } 4504160ccd9SAlan Cox #endif 4518fe387abSDmitrij Tejblum cnt = nbyte; 452c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 453279d7226SMatthew Dillon bwillwrite(); 454b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) { 4554160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4564160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4574160ccd9SAlan Cox error = 0; 45819eb87d2SJohn Baldwin if (error == EPIPE) { 459b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 460b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 461b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 46219eb87d2SJohn Baldwin } 4634160ccd9SAlan Cox } 4644160ccd9SAlan Cox cnt -= auio.uio_resid; 4654160ccd9SAlan Cox #ifdef KTRACE 4663c89e357SBrian Feldman if (didktr && error == 0) { 46742ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 46842ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 469b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_WRITE, &ktruio, error); 47042ebfbf2SBrian Feldman } 4714160ccd9SAlan Cox #endif 472b40ce416SJulian Elischer td->td_retval[0] = cnt; 4734160ccd9SAlan Cox return (error); 4744160ccd9SAlan Cox } 4754160ccd9SAlan Cox 4764160ccd9SAlan Cox /* 477df8bae1dSRodney W. Grimes * Gather write system call 478df8bae1dSRodney W. Grimes */ 479d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 480df8bae1dSRodney W. Grimes struct writev_args { 481df8bae1dSRodney W. Grimes int fd; 482df8bae1dSRodney W. Grimes struct iovec *iovp; 483df8bae1dSRodney W. Grimes u_int iovcnt; 484df8bae1dSRodney W. Grimes }; 485d2d3e875SBruce Evans #endif 486ad2edad9SMatthew Dillon /* 487ad2edad9SMatthew Dillon * MPSAFE 488ad2edad9SMatthew Dillon */ 48926f9a767SRodney W. Grimes int 490b40ce416SJulian Elischer writev(td, uap) 491b40ce416SJulian Elischer struct thread *td; 492df8bae1dSRodney W. Grimes register struct writev_args *uap; 493df8bae1dSRodney W. Grimes { 494b064d43dSMatthew Dillon struct file *fp; 495df8bae1dSRodney W. Grimes struct uio auio; 496df8bae1dSRodney W. Grimes register struct iovec *iov; 497df8bae1dSRodney W. Grimes struct iovec *needfree; 498df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 499df8bae1dSRodney W. Grimes long i, cnt, error = 0; 500df8bae1dSRodney W. Grimes u_int iovlen; 501df8bae1dSRodney W. Grimes #ifdef KTRACE 502df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 50342ebfbf2SBrian Feldman struct uio ktruio; 504df8bae1dSRodney W. Grimes #endif 505df8bae1dSRodney W. Grimes 506ad2edad9SMatthew Dillon mtx_lock(&Giant); 507b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) != 0) { 508ad2edad9SMatthew Dillon error = EBADF; 509ad2edad9SMatthew Dillon goto done2; 510ad2edad9SMatthew Dillon } 511df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 512df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 513df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 5141aa3e7ddSBrian Feldman if (uap->iovcnt > UIO_MAXIOV) { 5151aa3e7ddSBrian Feldman needfree = NULL; 5161aa3e7ddSBrian Feldman error = EINVAL; 5171aa3e7ddSBrian Feldman goto done; 5181aa3e7ddSBrian Feldman } 519df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 520df8bae1dSRodney W. Grimes needfree = iov; 521df8bae1dSRodney W. Grimes } else { 522df8bae1dSRodney W. Grimes iov = aiov; 523df8bae1dSRodney W. Grimes needfree = NULL; 524df8bae1dSRodney W. Grimes } 525df8bae1dSRodney W. Grimes auio.uio_iov = iov; 526df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 527df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 528df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 529b40ce416SJulian Elischer auio.uio_td = td; 5302c1011f7SJohn Dyson auio.uio_offset = -1; 531bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 532df8bae1dSRodney W. Grimes goto done; 533df8bae1dSRodney W. Grimes auio.uio_resid = 0; 534df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 535069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 536df8bae1dSRodney W. Grimes error = EINVAL; 537df8bae1dSRodney W. Grimes goto done; 538df8bae1dSRodney W. Grimes } 539069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 540df8bae1dSRodney W. Grimes iov++; 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes #ifdef KTRACE 543df8bae1dSRodney W. Grimes /* 54442ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 545df8bae1dSRodney W. Grimes */ 546b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 547df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 548df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 54942ebfbf2SBrian Feldman ktruio = auio; 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes #endif 552df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 553a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5549440653dSMatthew Dillon bwillwrite(); 555b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) { 556df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 557df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 558df8bae1dSRodney W. Grimes error = 0; 55919eb87d2SJohn Baldwin if (error == EPIPE) { 560b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 561b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 562b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 56319eb87d2SJohn Baldwin } 564df8bae1dSRodney W. Grimes } 565df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 566df8bae1dSRodney W. Grimes #ifdef KTRACE 567df8bae1dSRodney W. Grimes if (ktriov != NULL) { 56842ebfbf2SBrian Feldman if (error == 0) { 56942ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 57042ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 571b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_WRITE, &ktruio, 57242ebfbf2SBrian Feldman error); 57342ebfbf2SBrian Feldman } 574df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 575df8bae1dSRodney W. Grimes } 576df8bae1dSRodney W. Grimes #endif 577b40ce416SJulian Elischer td->td_retval[0] = cnt; 578df8bae1dSRodney W. Grimes done: 579b40ce416SJulian Elischer fdrop(fp, td); 580df8bae1dSRodney W. Grimes if (needfree) 581df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 582ad2edad9SMatthew Dillon done2: 583ad2edad9SMatthew Dillon mtx_unlock(&Giant); 584df8bae1dSRodney W. Grimes return (error); 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes 587df8bae1dSRodney W. Grimes /* 588df8bae1dSRodney W. Grimes * Ioctl system call 589df8bae1dSRodney W. Grimes */ 590d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 591df8bae1dSRodney W. Grimes struct ioctl_args { 592df8bae1dSRodney W. Grimes int fd; 593069e9bc1SDoug Rabson u_long com; 594df8bae1dSRodney W. Grimes caddr_t data; 595df8bae1dSRodney W. Grimes }; 596d2d3e875SBruce Evans #endif 597ad2edad9SMatthew Dillon /* 598ad2edad9SMatthew Dillon * MPSAFE 599ad2edad9SMatthew Dillon */ 600df8bae1dSRodney W. Grimes /* ARGSUSED */ 60126f9a767SRodney W. Grimes int 602b40ce416SJulian Elischer ioctl(td, uap) 603b40ce416SJulian Elischer struct thread *td; 604df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 605df8bae1dSRodney W. Grimes { 606df8bae1dSRodney W. Grimes register struct file *fp; 607df8bae1dSRodney W. Grimes register struct filedesc *fdp; 608831b9ef2SDoug Rabson register u_long com; 609ad2edad9SMatthew Dillon int error = 0; 610df8bae1dSRodney W. Grimes register u_int size; 611df8bae1dSRodney W. Grimes caddr_t data, memp; 612df8bae1dSRodney W. Grimes int tmp; 613df8bae1dSRodney W. Grimes #define STK_PARAMS 128 614d2ba455cSMatthew Dillon union { 615df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 616d2ba455cSMatthew Dillon long align; 617d2ba455cSMatthew Dillon } ubuf; 618df8bae1dSRodney W. Grimes 619426da3bcSAlfred Perlstein fp = ffind_hold(td, uap->fd); 620426da3bcSAlfred Perlstein if (fp == NULL) 621426da3bcSAlfred Perlstein return (EBADF); 622ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 623426da3bcSAlfred Perlstein fdrop(fp, td); 624426da3bcSAlfred Perlstein return (EBADF); 625ad2edad9SMatthew Dillon } 626426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 627df8bae1dSRodney W. Grimes switch (com = uap->com) { 628df8bae1dSRodney W. Grimes case FIONCLEX: 629426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 630df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 631426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 632426da3bcSAlfred Perlstein fdrop(fp, td); 633426da3bcSAlfred Perlstein return (0); 634df8bae1dSRodney W. Grimes case FIOCLEX: 635426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 636df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 637426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 638426da3bcSAlfred Perlstein fdrop(fp, td); 639426da3bcSAlfred Perlstein return (0); 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes 642df8bae1dSRodney W. Grimes /* 643df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 644df8bae1dSRodney W. Grimes * copied to/from the user's address space. 645df8bae1dSRodney W. Grimes */ 646df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 647ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 648426da3bcSAlfred Perlstein fdrop(fp, td); 649426da3bcSAlfred Perlstein return (ENOTTY); 650ad2edad9SMatthew Dillon } 651279d7226SMatthew Dillon 652426da3bcSAlfred Perlstein mtx_lock(&Giant); 653df8bae1dSRodney W. Grimes memp = NULL; 654d2ba455cSMatthew Dillon if (size > sizeof (ubuf.stkbuf)) { 655df8bae1dSRodney W. Grimes memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 656df8bae1dSRodney W. Grimes data = memp; 657279d7226SMatthew Dillon } else { 658d2ba455cSMatthew Dillon data = ubuf.stkbuf; 659279d7226SMatthew Dillon } 660df8bae1dSRodney W. Grimes if (com&IOC_IN) { 661df8bae1dSRodney W. Grimes if (size) { 662df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 663df8bae1dSRodney W. Grimes if (error) { 664df8bae1dSRodney W. Grimes if (memp) 665df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 666b40ce416SJulian Elischer fdrop(fp, td); 667426da3bcSAlfred Perlstein goto done; 668df8bae1dSRodney W. Grimes } 669279d7226SMatthew Dillon } else { 670df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 671279d7226SMatthew Dillon } 672279d7226SMatthew Dillon } else if ((com&IOC_OUT) && size) { 673df8bae1dSRodney W. Grimes /* 674df8bae1dSRodney W. Grimes * Zero the buffer so the user always 675df8bae1dSRodney W. Grimes * gets back something deterministic. 676df8bae1dSRodney W. Grimes */ 677df8bae1dSRodney W. Grimes bzero(data, size); 678279d7226SMatthew Dillon } else if (com&IOC_VOID) { 679df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 680279d7226SMatthew Dillon } 681df8bae1dSRodney W. Grimes 682df8bae1dSRodney W. Grimes switch (com) { 683df8bae1dSRodney W. Grimes 684df8bae1dSRodney W. Grimes case FIONBIO: 685426da3bcSAlfred Perlstein FILE_LOCK(fp); 686bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 687df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 688df8bae1dSRodney W. Grimes else 689df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 690426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 691b40ce416SJulian Elischer error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td); 692df8bae1dSRodney W. Grimes break; 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes case FIOASYNC: 695426da3bcSAlfred Perlstein FILE_LOCK(fp); 696bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 697df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 698df8bae1dSRodney W. Grimes else 699df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 700426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 701b40ce416SJulian Elischer error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td); 702df8bae1dSRodney W. Grimes break; 703df8bae1dSRodney W. Grimes 704df8bae1dSRodney W. Grimes default: 705b40ce416SJulian Elischer error = fo_ioctl(fp, com, data, td); 706df8bae1dSRodney W. Grimes /* 707df8bae1dSRodney W. Grimes * Copy any data to user, size was 708df8bae1dSRodney W. Grimes * already set and checked above. 709df8bae1dSRodney W. Grimes */ 710df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 711df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 712df8bae1dSRodney W. Grimes break; 713df8bae1dSRodney W. Grimes } 714df8bae1dSRodney W. Grimes if (memp) 715df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 716b40ce416SJulian Elischer fdrop(fp, td); 717426da3bcSAlfred Perlstein done: 718ad2edad9SMatthew Dillon mtx_unlock(&Giant); 719df8bae1dSRodney W. Grimes return (error); 720df8bae1dSRodney W. Grimes } 721df8bae1dSRodney W. Grimes 7228cb96f20SPeter Wemm static int nselcoll; /* Select collisions since boot */ 723265fc98fSSeigo Tanimura struct cv selwait; 7248cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 725df8bae1dSRodney W. Grimes 726df8bae1dSRodney W. Grimes /* 727df8bae1dSRodney W. Grimes * Select system call. 728df8bae1dSRodney W. Grimes */ 729d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 730df8bae1dSRodney W. Grimes struct select_args { 731b08f7993SSujal Patel int nd; 732df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 733df8bae1dSRodney W. Grimes struct timeval *tv; 734df8bae1dSRodney W. Grimes }; 735d2d3e875SBruce Evans #endif 736ad2edad9SMatthew Dillon /* 737ad2edad9SMatthew Dillon * MPSAFE 738ad2edad9SMatthew Dillon */ 73926f9a767SRodney W. Grimes int 740b40ce416SJulian Elischer select(td, uap) 741b40ce416SJulian Elischer register struct thread *td; 742df8bae1dSRodney W. Grimes register struct select_args *uap; 743df8bae1dSRodney W. Grimes { 744426da3bcSAlfred Perlstein struct filedesc *fdp; 745d5e4d7e1SBruce Evans /* 746d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 747d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 748d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 749d5e4d7e1SBruce Evans * of 256. 750d5e4d7e1SBruce Evans */ 751d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 752265fc98fSSeigo Tanimura fd_mask s_heldbits[howmany(2048, NFDBITS)]; 753265fc98fSSeigo Tanimura fd_mask *ibits[3], *obits[3], *selbits, *sbp, *heldbits, *hibits, *hobits; 75400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 755265fc98fSSeigo Tanimura int ncoll, error, timo, i; 756d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 757df8bae1dSRodney W. Grimes 758b08f7993SSujal Patel if (uap->nd < 0) 759acbfbfeaSSujal Patel return (EINVAL); 760426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 761ad2edad9SMatthew Dillon mtx_lock(&Giant); 762426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 763ad2edad9SMatthew Dillon 764b40ce416SJulian Elischer if (uap->nd > td->td_proc->p_fd->fd_nfiles) 765b40ce416SJulian Elischer uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 766426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 767b08f7993SSujal Patel 768d5e4d7e1SBruce Evans /* 769d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 770d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 771d5e4d7e1SBruce Evans */ 772d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 773d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 774d5e4d7e1SBruce Evans nbufbytes = 0; 775d5e4d7e1SBruce Evans if (uap->in != NULL) 776d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 777d5e4d7e1SBruce Evans if (uap->ou != NULL) 778d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 779d5e4d7e1SBruce Evans if (uap->ex != NULL) 780d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 781d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 782d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 783d5e4d7e1SBruce Evans else 784d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 785265fc98fSSeigo Tanimura if (2 * ncpbytes <= sizeof s_heldbits) { 786265fc98fSSeigo Tanimura bzero(s_heldbits, sizeof(s_heldbits)); 787265fc98fSSeigo Tanimura heldbits = &s_heldbits[0]; 788265fc98fSSeigo Tanimura } else 789265fc98fSSeigo Tanimura heldbits = malloc(2 * ncpbytes, M_SELECT, M_WAITOK | M_ZERO); 790b08f7993SSujal Patel 791b08f7993SSujal Patel /* 792d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 793d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 794d5e4d7e1SBruce Evans * together. 795b08f7993SSujal Patel */ 796d5e4d7e1SBruce Evans sbp = selbits; 797265fc98fSSeigo Tanimura hibits = heldbits + ncpbytes / sizeof *heldbits; 798265fc98fSSeigo Tanimura hobits = heldbits; 799df8bae1dSRodney W. Grimes #define getbits(name, x) \ 800d5e4d7e1SBruce Evans do { \ 801d5e4d7e1SBruce Evans if (uap->name == NULL) \ 802d5e4d7e1SBruce Evans ibits[x] = NULL; \ 803d5e4d7e1SBruce Evans else { \ 804d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 805d5e4d7e1SBruce Evans obits[x] = sbp; \ 806d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 807d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 808265fc98fSSeigo Tanimura if (error != 0) \ 809265fc98fSSeigo Tanimura goto done_noproclock; \ 810265fc98fSSeigo Tanimura for (i = 0; \ 811265fc98fSSeigo Tanimura i < ncpbytes / sizeof ibits[i][0]; \ 812265fc98fSSeigo Tanimura i++) \ 813265fc98fSSeigo Tanimura hibits[i] |= ibits[x][i]; \ 814e04ac2feSJohn Baldwin } \ 815d5e4d7e1SBruce Evans } while (0) 816df8bae1dSRodney W. Grimes getbits(in, 0); 817df8bae1dSRodney W. Grimes getbits(ou, 1); 818df8bae1dSRodney W. Grimes getbits(ex, 2); 819df8bae1dSRodney W. Grimes #undef getbits 820d5e4d7e1SBruce Evans if (nbufbytes != 0) 821d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 822df8bae1dSRodney W. Grimes 823df8bae1dSRodney W. Grimes if (uap->tv) { 824df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 825df8bae1dSRodney W. Grimes sizeof (atv)); 826265fc98fSSeigo Tanimura if (error) 827265fc98fSSeigo Tanimura goto done_noproclock; 828df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 829df8bae1dSRodney W. Grimes error = EINVAL; 830265fc98fSSeigo Tanimura goto done_noproclock; 831df8bae1dSRodney W. Grimes } 832c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 83300af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8349c386f6bSJohn Baldwin } else { 83500af9731SPoul-Henning Kamp atv.tv_sec = 0; 8369c386f6bSJohn Baldwin atv.tv_usec = 0; 8379c386f6bSJohn Baldwin } 838b40ce416SJulian Elischer selholddrop(td, hibits, hobits, uap->nd, 1); 83900af9731SPoul-Henning Kamp timo = 0; 840b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 841df8bae1dSRodney W. Grimes retry: 842df8bae1dSRodney W. Grimes ncoll = nselcoll; 843fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 844b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 845fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 846b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 847b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 848b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 849b40ce416SJulian Elischer if (error || td->td_retval[0]) 850df8bae1dSRodney W. Grimes goto done; 8514da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 852c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 8531b369704SSeigo Tanimura if (timevalcmp(&rtv, &atv, >=)) { 8541b369704SSeigo Tanimura /* 8551b369704SSeigo Tanimura * An event of our interest may occur during locking a process. 8561b369704SSeigo Tanimura * In order to avoid missing the event that occured during locking 857b40ce416SJulian Elischer * the process, test TDF_SELECT and rescan file descriptors if 8581b369704SSeigo Tanimura * necessary. 8591b369704SSeigo Tanimura */ 860fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 861b40ce416SJulian Elischer if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 8621b369704SSeigo Tanimura ncoll = nselcoll; 863b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 864fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 865b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 866b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 867b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 868fea2ab83SJohn Baldwin } else 869fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 870df8bae1dSRodney W. Grimes goto done; 8711b369704SSeigo Tanimura } 87200af9731SPoul-Henning Kamp ttv = atv; 87300af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 87400af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 87500af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 876df8bae1dSRodney W. Grimes } 877fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 878b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 879fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 880bfbbc4aaSJason Evans 881265fc98fSSeigo Tanimura if (timo > 0) 882b40ce416SJulian Elischer error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo); 883265fc98fSSeigo Tanimura else 884b40ce416SJulian Elischer error = cv_wait_sig(&selwait, &td->td_proc->p_mtx); 885bfbbc4aaSJason Evans 886df8bae1dSRodney W. Grimes if (error == 0) 887df8bae1dSRodney W. Grimes goto retry; 888265fc98fSSeigo Tanimura 889df8bae1dSRodney W. Grimes done: 890fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 891b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 892fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 893b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 894b40ce416SJulian Elischer selholddrop(td, hibits, hobits, uap->nd, 0); 895265fc98fSSeigo Tanimura done_noproclock: 896df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 897df8bae1dSRodney W. Grimes if (error == ERESTART) 898df8bae1dSRodney W. Grimes error = EINTR; 899df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 900df8bae1dSRodney W. Grimes error = 0; 901df8bae1dSRodney W. Grimes #define putbits(name, x) \ 902d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 903df8bae1dSRodney W. Grimes error = error2; 904df8bae1dSRodney W. Grimes if (error == 0) { 905df8bae1dSRodney W. Grimes int error2; 906df8bae1dSRodney W. Grimes 907df8bae1dSRodney W. Grimes putbits(in, 0); 908df8bae1dSRodney W. Grimes putbits(ou, 1); 909df8bae1dSRodney W. Grimes putbits(ex, 2); 910df8bae1dSRodney W. Grimes #undef putbits 911df8bae1dSRodney W. Grimes } 912d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 913d5e4d7e1SBruce Evans free(selbits, M_SELECT); 914265fc98fSSeigo Tanimura if (heldbits != &s_heldbits[0]) 915265fc98fSSeigo Tanimura free(heldbits, M_SELECT); 916ad2edad9SMatthew Dillon 917ad2edad9SMatthew Dillon mtx_unlock(&Giant); 918df8bae1dSRodney W. Grimes return (error); 919df8bae1dSRodney W. Grimes } 920df8bae1dSRodney W. Grimes 921426da3bcSAlfred Perlstein /* 922426da3bcSAlfred Perlstein * Used to hold then release a group of fds for select(2). 923426da3bcSAlfred Perlstein * Hold (hold == 1) or release (hold == 0) a group of filedescriptors. 924426da3bcSAlfred Perlstein * if holding then use ibits setting the bits in obits, otherwise use obits. 925426da3bcSAlfred Perlstein */ 92687b6de2bSPoul-Henning Kamp static int 927b40ce416SJulian Elischer selholddrop(td, ibits, obits, nfd, hold) 928b40ce416SJulian Elischer struct thread *td; 929265fc98fSSeigo Tanimura fd_mask *ibits, *obits; 930265fc98fSSeigo Tanimura int nfd, hold; 931265fc98fSSeigo Tanimura { 932b40ce416SJulian Elischer struct filedesc *fdp = td->td_proc->p_fd; 933265fc98fSSeigo Tanimura int i, fd; 934265fc98fSSeigo Tanimura fd_mask bits; 935265fc98fSSeigo Tanimura struct file *fp; 936265fc98fSSeigo Tanimura 937426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 938265fc98fSSeigo Tanimura for (i = 0; i < nfd; i += NFDBITS) { 939265fc98fSSeigo Tanimura if (hold) 940265fc98fSSeigo Tanimura bits = ibits[i/NFDBITS]; 941265fc98fSSeigo Tanimura else 942265fc98fSSeigo Tanimura bits = obits[i/NFDBITS]; 943265fc98fSSeigo Tanimura /* ffs(int mask) not portable, fd_mask is long */ 944265fc98fSSeigo Tanimura for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 945265fc98fSSeigo Tanimura if (!(bits & 1)) 946265fc98fSSeigo Tanimura continue; 947265fc98fSSeigo Tanimura fp = fdp->fd_ofiles[fd]; 948426da3bcSAlfred Perlstein if (fp == NULL) { 949426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 950265fc98fSSeigo Tanimura return (EBADF); 951426da3bcSAlfred Perlstein } 952265fc98fSSeigo Tanimura if (hold) { 953265fc98fSSeigo Tanimura fhold(fp); 954265fc98fSSeigo Tanimura obits[(fd)/NFDBITS] |= 955265fc98fSSeigo Tanimura ((fd_mask)1 << ((fd) % NFDBITS)); 956426da3bcSAlfred Perlstein } else { 957426da3bcSAlfred Perlstein /* XXX: optimize by making a special 958426da3bcSAlfred Perlstein * version of fdrop that only unlocks 959426da3bcSAlfred Perlstein * the filedesc if needed? This would 960426da3bcSAlfred Perlstein * redcuce the number of lock/unlock 961426da3bcSAlfred Perlstein * pairs by quite a bit. 962426da3bcSAlfred Perlstein */ 963426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 964b40ce416SJulian Elischer fdrop(fp, td); 965426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 966265fc98fSSeigo Tanimura } 967265fc98fSSeigo Tanimura } 968426da3bcSAlfred Perlstein } 969426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 970265fc98fSSeigo Tanimura return (0); 971265fc98fSSeigo Tanimura } 972265fc98fSSeigo Tanimura 973265fc98fSSeigo Tanimura static int 974b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 975b40ce416SJulian Elischer struct thread *td; 976b08f7993SSujal Patel fd_mask **ibits, **obits; 977cb226aaaSPoul-Henning Kamp int nfd; 978df8bae1dSRodney W. Grimes { 979f082218cSPeter Wemm int msk, i, fd; 980f082218cSPeter Wemm fd_mask bits; 981df8bae1dSRodney W. Grimes struct file *fp; 982df8bae1dSRodney W. Grimes int n = 0; 9832087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 98442d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 985df8bae1dSRodney W. Grimes 986df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 987d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 988d5e4d7e1SBruce Evans continue; 989df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 990b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 991f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 992f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 993f082218cSPeter Wemm if (!(bits & 1)) 994f082218cSPeter Wemm continue; 995426da3bcSAlfred Perlstein fp = ffind_hold(td, fd); 996df8bae1dSRodney W. Grimes if (fp == NULL) 997df8bae1dSRodney W. Grimes return (EBADF); 998b40ce416SJulian Elischer if (fo_poll(fp, flag[msk], fp->f_cred, td)) { 999b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 1000f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 1001df8bae1dSRodney W. Grimes n++; 1002df8bae1dSRodney W. Grimes } 1003426da3bcSAlfred Perlstein fdrop(fp, td); 1004df8bae1dSRodney W. Grimes } 1005df8bae1dSRodney W. Grimes } 1006df8bae1dSRodney W. Grimes } 1007b40ce416SJulian Elischer td->td_retval[0] = n; 1008df8bae1dSRodney W. Grimes return (0); 1009df8bae1dSRodney W. Grimes } 1010df8bae1dSRodney W. Grimes 101142d11757SPeter Wemm /* 101242d11757SPeter Wemm * Poll system call. 101342d11757SPeter Wemm */ 101442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 101542d11757SPeter Wemm struct poll_args { 101642d11757SPeter Wemm struct pollfd *fds; 101742d11757SPeter Wemm u_int nfds; 101842d11757SPeter Wemm int timeout; 101942d11757SPeter Wemm }; 102042d11757SPeter Wemm #endif 1021ad2edad9SMatthew Dillon /* 1022ad2edad9SMatthew Dillon * MPSAFE 1023ad2edad9SMatthew Dillon */ 102442d11757SPeter Wemm int 1025b40ce416SJulian Elischer poll(td, uap) 1026b40ce416SJulian Elischer struct thread *td; 1027ea0237edSJonathan Lemon struct poll_args *uap; 102842d11757SPeter Wemm { 102942d11757SPeter Wemm caddr_t bits; 103042d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 103100af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 1032265fc98fSSeigo Tanimura int ncoll, error = 0, timo; 1033ea0237edSJonathan Lemon u_int nfds; 103442d11757SPeter Wemm size_t ni; 1035265fc98fSSeigo Tanimura struct pollfd p_heldbits[32]; 1036265fc98fSSeigo Tanimura struct pollfd *heldbits; 103742d11757SPeter Wemm 103889b71647SPeter Wemm nfds = SCARG(uap, nfds); 1039ad2edad9SMatthew Dillon 1040ad2edad9SMatthew Dillon mtx_lock(&Giant); 104189b71647SPeter Wemm /* 10422bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 10432bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 10442bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 10452bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 10462bd5ac33SPeter Wemm * safe, but not overly restrictive. 104789b71647SPeter Wemm */ 1048b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 1049b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 1050ad2edad9SMatthew Dillon error = EINVAL; 1051ad2edad9SMatthew Dillon goto done2; 1052ad2edad9SMatthew Dillon } 105389b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 105442d11757SPeter Wemm if (ni > sizeof(smallbits)) 105542d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 105642d11757SPeter Wemm else 105742d11757SPeter Wemm bits = smallbits; 1058265fc98fSSeigo Tanimura if (ni > sizeof(p_heldbits)) 1059265fc98fSSeigo Tanimura heldbits = malloc(ni, M_TEMP, M_WAITOK); 1060265fc98fSSeigo Tanimura else { 1061265fc98fSSeigo Tanimura bzero(p_heldbits, sizeof(p_heldbits)); 1062265fc98fSSeigo Tanimura heldbits = p_heldbits; 1063265fc98fSSeigo Tanimura } 106442d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 106542d11757SPeter Wemm if (error) 1066265fc98fSSeigo Tanimura goto done_noproclock; 1067265fc98fSSeigo Tanimura bcopy(bits, heldbits, ni); 106842d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 106942d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 107042d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 107142d11757SPeter Wemm if (itimerfix(&atv)) { 107242d11757SPeter Wemm error = EINVAL; 1073265fc98fSSeigo Tanimura goto done_noproclock; 107442d11757SPeter Wemm } 1075c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 107600af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 10779c386f6bSJohn Baldwin } else { 107800af9731SPoul-Henning Kamp atv.tv_sec = 0; 10799c386f6bSJohn Baldwin atv.tv_usec = 0; 10809c386f6bSJohn Baldwin } 1081b40ce416SJulian Elischer pollholddrop(td, heldbits, nfds, 1); 108200af9731SPoul-Henning Kamp timo = 0; 1083b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 108442d11757SPeter Wemm retry: 108542d11757SPeter Wemm ncoll = nselcoll; 1086fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1087b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 1088fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 1089b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1090b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 1091b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 1092b40ce416SJulian Elischer if (error || td->td_retval[0]) 109342d11757SPeter Wemm goto done; 10944da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1095c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 10961b369704SSeigo Tanimura if (timevalcmp(&rtv, &atv, >=)) { 10971b369704SSeigo Tanimura /* 10981b369704SSeigo Tanimura * An event of our interest may occur during locking a process. 10991b369704SSeigo Tanimura * In order to avoid missing the event that occured during locking 1100b40ce416SJulian Elischer * the process, test TDF_SELECT and rescan file descriptors if 11011b369704SSeigo Tanimura * necessary. 11021b369704SSeigo Tanimura */ 1103fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1104b40ce416SJulian Elischer if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 11051b369704SSeigo Tanimura ncoll = nselcoll; 1106b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 1107fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 1108b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1109b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 1110b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 1111fea2ab83SJohn Baldwin } else 1112fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 111342d11757SPeter Wemm goto done; 11141b369704SSeigo Tanimura } 111500af9731SPoul-Henning Kamp ttv = atv; 111600af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 111700af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 111800af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 111942d11757SPeter Wemm } 1120fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1121b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1122fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 1123265fc98fSSeigo Tanimura if (timo > 0) 1124b40ce416SJulian Elischer error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo); 1125265fc98fSSeigo Tanimura else 1126b40ce416SJulian Elischer error = cv_wait_sig(&selwait, &td->td_proc->p_mtx); 112742d11757SPeter Wemm if (error == 0) 112842d11757SPeter Wemm goto retry; 1129265fc98fSSeigo Tanimura 113042d11757SPeter Wemm done: 1131fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1132b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1133fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 1134b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1135b40ce416SJulian Elischer pollholddrop(td, heldbits, nfds, 0); 1136265fc98fSSeigo Tanimura done_noproclock: 113742d11757SPeter Wemm /* poll is not restarted after signals... */ 113842d11757SPeter Wemm if (error == ERESTART) 113942d11757SPeter Wemm error = EINTR; 114042d11757SPeter Wemm if (error == EWOULDBLOCK) 114142d11757SPeter Wemm error = 0; 114242d11757SPeter Wemm if (error == 0) { 114342d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 114442d11757SPeter Wemm if (error) 114542d11757SPeter Wemm goto out; 114642d11757SPeter Wemm } 114742d11757SPeter Wemm out: 114842d11757SPeter Wemm if (ni > sizeof(smallbits)) 114942d11757SPeter Wemm free(bits, M_TEMP); 1150265fc98fSSeigo Tanimura if (ni > sizeof(p_heldbits)) 1151265fc98fSSeigo Tanimura free(heldbits, M_TEMP); 1152ad2edad9SMatthew Dillon done2: 1153ad2edad9SMatthew Dillon mtx_unlock(&Giant); 115442d11757SPeter Wemm return (error); 115542d11757SPeter Wemm } 115642d11757SPeter Wemm 115742d11757SPeter Wemm static int 1158b40ce416SJulian Elischer pollholddrop(td, fds, nfd, hold) 1159b40ce416SJulian Elischer struct thread *td; 1160265fc98fSSeigo Tanimura struct pollfd *fds; 1161265fc98fSSeigo Tanimura u_int nfd; 1162265fc98fSSeigo Tanimura int hold; 1163265fc98fSSeigo Tanimura { 1164b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 1165265fc98fSSeigo Tanimura int i; 1166265fc98fSSeigo Tanimura struct file *fp; 1167265fc98fSSeigo Tanimura 1168426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1169265fc98fSSeigo Tanimura for (i = 0; i < nfd; i++, fds++) { 1170265fc98fSSeigo Tanimura if (0 <= fds->fd && fds->fd < fdp->fd_nfiles) { 1171265fc98fSSeigo Tanimura fp = fdp->fd_ofiles[fds->fd]; 1172265fc98fSSeigo Tanimura if (hold) { 1173265fc98fSSeigo Tanimura if (fp != NULL) { 1174265fc98fSSeigo Tanimura fhold(fp); 1175265fc98fSSeigo Tanimura fds->revents = 1; 1176265fc98fSSeigo Tanimura } else 1177265fc98fSSeigo Tanimura fds->revents = 0; 1178426da3bcSAlfred Perlstein } else if(fp != NULL && fds->revents) { 1179426da3bcSAlfred Perlstein FILE_LOCK(fp); 1180426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 1181426da3bcSAlfred Perlstein fdrop_locked(fp, td); 1182426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1183265fc98fSSeigo Tanimura } 1184265fc98fSSeigo Tanimura } 1185426da3bcSAlfred Perlstein } 1186426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 1187265fc98fSSeigo Tanimura return (0); 1188265fc98fSSeigo Tanimura } 1189265fc98fSSeigo Tanimura 1190265fc98fSSeigo Tanimura static int 1191b40ce416SJulian Elischer pollscan(td, fds, nfd) 1192b40ce416SJulian Elischer struct thread *td; 119342d11757SPeter Wemm struct pollfd *fds; 1194ea0237edSJonathan Lemon u_int nfd; 119542d11757SPeter Wemm { 1196b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 119742d11757SPeter Wemm int i; 119842d11757SPeter Wemm struct file *fp; 119942d11757SPeter Wemm int n = 0; 120042d11757SPeter Wemm 120142d11757SPeter Wemm for (i = 0; i < nfd; i++, fds++) { 1202426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1203337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 120442d11757SPeter Wemm fds->revents = POLLNVAL; 120542d11757SPeter Wemm n++; 1206426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 1207337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1208337c9691SJordan K. Hubbard fds->revents = 0; 1209426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 121042d11757SPeter Wemm } else { 121142d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1212426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 1213279d7226SMatthew Dillon if (fp == NULL) { 121442d11757SPeter Wemm fds->revents = POLLNVAL; 121542d11757SPeter Wemm n++; 121642d11757SPeter Wemm } else { 12172087c896SBruce Evans /* 12182087c896SBruce Evans * Note: backend also returns POLLHUP and 12192087c896SBruce Evans * POLLERR if appropriate. 12202087c896SBruce Evans */ 122113ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1222b40ce416SJulian Elischer fp->f_cred, td); 122342d11757SPeter Wemm if (fds->revents != 0) 122442d11757SPeter Wemm n++; 122542d11757SPeter Wemm } 122642d11757SPeter Wemm } 122742d11757SPeter Wemm } 1228b40ce416SJulian Elischer td->td_retval[0] = n; 122942d11757SPeter Wemm return (0); 123042d11757SPeter Wemm } 123142d11757SPeter Wemm 123242d11757SPeter Wemm /* 123342d11757SPeter Wemm * OpenBSD poll system call. 123442d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 123542d11757SPeter Wemm */ 123642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 123742d11757SPeter Wemm struct openbsd_poll_args { 123842d11757SPeter Wemm struct pollfd *fds; 123942d11757SPeter Wemm u_int nfds; 124042d11757SPeter Wemm int timeout; 124142d11757SPeter Wemm }; 124242d11757SPeter Wemm #endif 1243ad2edad9SMatthew Dillon /* 1244ad2edad9SMatthew Dillon * MPSAFE 1245ad2edad9SMatthew Dillon */ 124642d11757SPeter Wemm int 1247b40ce416SJulian Elischer openbsd_poll(td, uap) 1248b40ce416SJulian Elischer register struct thread *td; 124942d11757SPeter Wemm register struct openbsd_poll_args *uap; 125042d11757SPeter Wemm { 1251b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 125242d11757SPeter Wemm } 125342d11757SPeter Wemm 1254df8bae1dSRodney W. Grimes /*ARGSUSED*/ 125526f9a767SRodney W. Grimes int 1256b40ce416SJulian Elischer seltrue(dev, events, td) 1257df8bae1dSRodney W. Grimes dev_t dev; 125842d11757SPeter Wemm int events; 1259b40ce416SJulian Elischer struct thread *td; 1260df8bae1dSRodney W. Grimes { 1261df8bae1dSRodney W. Grimes 126242d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1263df8bae1dSRodney W. Grimes } 1264df8bae1dSRodney W. Grimes 1265b40ce416SJulian Elischer static int 1266b40ce416SJulian Elischer find_thread_in_proc(struct proc *p, struct thread *td) 1267b40ce416SJulian Elischer { 1268b40ce416SJulian Elischer struct thread *td2; 1269b40ce416SJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 1270b40ce416SJulian Elischer if (td2 == td) { 1271b40ce416SJulian Elischer return (1); 1272b40ce416SJulian Elischer } 1273b40ce416SJulian Elischer } 1274b40ce416SJulian Elischer return (0); 1275b40ce416SJulian Elischer } 1276b40ce416SJulian Elischer 1277df8bae1dSRodney W. Grimes /* 1278df8bae1dSRodney W. Grimes * Record a select request. 1279df8bae1dSRodney W. Grimes */ 1280df8bae1dSRodney W. Grimes void 1281df8bae1dSRodney W. Grimes selrecord(selector, sip) 1282b40ce416SJulian Elischer struct thread *selector; 1283df8bae1dSRodney W. Grimes struct selinfo *sip; 1284df8bae1dSRodney W. Grimes { 1285df8bae1dSRodney W. Grimes struct proc *p; 1286df8bae1dSRodney W. Grimes pid_t mypid; 1287df8bae1dSRodney W. Grimes 1288b40ce416SJulian Elischer mypid = selector->td_proc->p_pid; 1289b40ce416SJulian Elischer if ((sip->si_pid == mypid) && 1290b40ce416SJulian Elischer (sip->si_thread == selector)) { /* XXXKSE should be an ID? */ 1291df8bae1dSRodney W. Grimes return; 1292b40ce416SJulian Elischer } 1293b40ce416SJulian Elischer if (sip->si_pid && 1294b40ce416SJulian Elischer (p = pfind(sip->si_pid)) && 1295b40ce416SJulian Elischer (find_thread_in_proc(p, sip->si_thread))) { 12969ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1297b40ce416SJulian Elischer if (sip->si_thread->td_wchan == (caddr_t)&selwait) { 12989ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 129933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1300df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 1301e04ac2feSJohn Baldwin return; 1302e04ac2feSJohn Baldwin } 13039ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 130433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1305e04ac2feSJohn Baldwin } 1306df8bae1dSRodney W. Grimes sip->si_pid = mypid; 1307b40ce416SJulian Elischer sip->si_thread = selector; 1308df8bae1dSRodney W. Grimes } 1309df8bae1dSRodney W. Grimes 1310df8bae1dSRodney W. Grimes /* 1311df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1312df8bae1dSRodney W. Grimes */ 1313df8bae1dSRodney W. Grimes void 1314df8bae1dSRodney W. Grimes selwakeup(sip) 1315df8bae1dSRodney W. Grimes register struct selinfo *sip; 1316df8bae1dSRodney W. Grimes { 1317b40ce416SJulian Elischer struct thread *td; 1318df8bae1dSRodney W. Grimes register struct proc *p; 1319df8bae1dSRodney W. Grimes 1320df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 1321df8bae1dSRodney W. Grimes return; 1322df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 1323df8bae1dSRodney W. Grimes nselcoll++; 1324df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1325265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1326df8bae1dSRodney W. Grimes } 1327df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 1328df8bae1dSRodney W. Grimes sip->si_pid = 0; 1329b40ce416SJulian Elischer td = sip->si_thread; 1330df8bae1dSRodney W. Grimes if (p != NULL) { 1331b40ce416SJulian Elischer if (!find_thread_in_proc(p, td)) { 1332b40ce416SJulian Elischer PROC_UNLOCK(p); /* lock is in pfind() */; 1333b40ce416SJulian Elischer return; 1334b40ce416SJulian Elischer } 13359ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1336b40ce416SJulian Elischer if (td->td_wchan == (caddr_t)&selwait) { 1337b40ce416SJulian Elischer if (td->td_proc->p_stat == SSLEEP) 1338b40ce416SJulian Elischer setrunnable(td); 1339df8bae1dSRodney W. Grimes else 1340b40ce416SJulian Elischer cv_waitq_remove(td); 134133a9ed9dSJohn Baldwin } else 1342b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 134333a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 1344b40ce416SJulian Elischer PROC_UNLOCK(p); /* Lock is in pfind() */ 1345e04ac2feSJohn Baldwin } 1346df8bae1dSRodney W. Grimes } 1347265fc98fSSeigo Tanimura 1348265fc98fSSeigo Tanimura static void selectinit __P((void *)); 1349265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1350265fc98fSSeigo Tanimura 1351265fc98fSSeigo Tanimura /* ARGSUSED*/ 1352265fc98fSSeigo Tanimura static void 1353265fc98fSSeigo Tanimura selectinit(dummy) 1354265fc98fSSeigo Tanimura void *dummy; 1355265fc98fSSeigo Tanimura { 1356265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 1357265fc98fSSeigo Tanimura } 1358