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 868757e5bbSAlfred Perlstein struct file* 87279d7226SMatthew Dillon holdfp(fdp, fd, flag) 888fe387abSDmitrij Tejblum struct filedesc* fdp; 898fe387abSDmitrij Tejblum int fd, flag; 908fe387abSDmitrij Tejblum { 918fe387abSDmitrij Tejblum struct file* fp; 928fe387abSDmitrij Tejblum 938fe387abSDmitrij Tejblum if (((u_int)fd) >= fdp->fd_nfiles || 948fe387abSDmitrij Tejblum (fp = fdp->fd_ofiles[fd]) == NULL || 95279d7226SMatthew Dillon (fp->f_flag & flag) == 0) { 968fe387abSDmitrij Tejblum return (NULL); 97279d7226SMatthew Dillon } 98279d7226SMatthew Dillon fhold(fp); 998fe387abSDmitrij Tejblum return (fp); 1008fe387abSDmitrij Tejblum } 101d93f860cSPoul-Henning Kamp 102df8bae1dSRodney W. Grimes /* 103df8bae1dSRodney W. Grimes * Read system call. 104df8bae1dSRodney W. Grimes */ 105d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 106df8bae1dSRodney W. Grimes struct read_args { 107df8bae1dSRodney W. Grimes int fd; 108134e06feSBruce Evans void *buf; 109134e06feSBruce Evans size_t nbyte; 110df8bae1dSRodney W. Grimes }; 111d2d3e875SBruce Evans #endif 112ad2edad9SMatthew Dillon /* 113ad2edad9SMatthew Dillon * MPSAFE 114ad2edad9SMatthew Dillon */ 11526f9a767SRodney W. Grimes int 116b40ce416SJulian Elischer read(td, uap) 117b40ce416SJulian Elischer struct thread *td; 118df8bae1dSRodney W. Grimes register struct read_args *uap; 119df8bae1dSRodney W. Grimes { 120df8bae1dSRodney W. Grimes register struct file *fp; 121279d7226SMatthew Dillon int error; 122df8bae1dSRodney W. Grimes 123ad2edad9SMatthew Dillon mtx_lock(&Giant); 124b40ce416SJulian Elischer if ((fp = holdfp(td->td_proc->p_fd, uap->fd, FREAD)) != NULL) { 125b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, 126ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 127b40ce416SJulian Elischer fdrop(fp, td); 128ad2edad9SMatthew Dillon } else { 129ad2edad9SMatthew Dillon error = EBADF; 130ad2edad9SMatthew Dillon } 131ad2edad9SMatthew Dillon mtx_unlock(&Giant); 132279d7226SMatthew Dillon return(error); 133df8bae1dSRodney W. Grimes } 134df8bae1dSRodney W. Grimes 135df8bae1dSRodney W. Grimes /* 1368fe387abSDmitrij Tejblum * Pread system call 1374160ccd9SAlan Cox */ 1384160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1394160ccd9SAlan Cox struct pread_args { 1404160ccd9SAlan Cox int fd; 1414160ccd9SAlan Cox void *buf; 1424160ccd9SAlan Cox size_t nbyte; 1438fe387abSDmitrij Tejblum int pad; 1444160ccd9SAlan Cox off_t offset; 1454160ccd9SAlan Cox }; 1464160ccd9SAlan Cox #endif 147ad2edad9SMatthew Dillon /* 148ad2edad9SMatthew Dillon * MPSAFE 149ad2edad9SMatthew Dillon */ 1504160ccd9SAlan Cox int 151b40ce416SJulian Elischer pread(td, uap) 152b40ce416SJulian Elischer struct thread *td; 1534160ccd9SAlan Cox register struct pread_args *uap; 1544160ccd9SAlan Cox { 1554160ccd9SAlan Cox register struct file *fp; 156279d7226SMatthew Dillon int error; 1578fe387abSDmitrij Tejblum 158ad2edad9SMatthew Dillon mtx_lock(&Giant); 159b40ce416SJulian Elischer if ((fp = holdfp(td->td_proc->p_fd, uap->fd, FREAD)) == NULL) { 160ad2edad9SMatthew Dillon error = EBADF; 161ad2edad9SMatthew Dillon } else if (fp->f_type != DTYPE_VNODE) { 162279d7226SMatthew Dillon error = ESPIPE; 163b40ce416SJulian Elischer fdrop(fp, td); 164279d7226SMatthew Dillon } else { 165b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte, 166279d7226SMatthew Dillon uap->offset, FOF_OFFSET); 167b40ce416SJulian Elischer fdrop(fp, td); 168ad2edad9SMatthew Dillon } 169ad2edad9SMatthew Dillon mtx_unlock(&Giant); 170279d7226SMatthew Dillon return(error); 1718fe387abSDmitrij Tejblum } 1728fe387abSDmitrij Tejblum 1738fe387abSDmitrij Tejblum /* 1748fe387abSDmitrij Tejblum * Code common for read and pread 1758fe387abSDmitrij Tejblum */ 1768fe387abSDmitrij Tejblum int 177b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 178b40ce416SJulian Elischer struct thread *td; 1798fe387abSDmitrij Tejblum struct file *fp; 1808fe387abSDmitrij Tejblum int fd, flags; 1818fe387abSDmitrij Tejblum void *buf; 1828fe387abSDmitrij Tejblum size_t nbyte; 1838fe387abSDmitrij Tejblum off_t offset; 1848fe387abSDmitrij Tejblum { 1854160ccd9SAlan Cox struct uio auio; 1864160ccd9SAlan Cox struct iovec aiov; 1874160ccd9SAlan Cox long cnt, error = 0; 1884160ccd9SAlan Cox #ifdef KTRACE 1894160ccd9SAlan Cox struct iovec ktriov; 19042ebfbf2SBrian Feldman struct uio ktruio; 1913c89e357SBrian Feldman int didktr = 0; 1924160ccd9SAlan Cox #endif 1934160ccd9SAlan Cox 1948fe387abSDmitrij Tejblum aiov.iov_base = (caddr_t)buf; 1958fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 1964160ccd9SAlan Cox auio.uio_iov = &aiov; 1974160ccd9SAlan Cox auio.uio_iovcnt = 1; 1988fe387abSDmitrij Tejblum auio.uio_offset = offset; 1998fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 2004160ccd9SAlan Cox return (EINVAL); 2018fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 2024160ccd9SAlan Cox auio.uio_rw = UIO_READ; 2034160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 204b40ce416SJulian Elischer auio.uio_td = td; 2054160ccd9SAlan Cox #ifdef KTRACE 2064160ccd9SAlan Cox /* 2074160ccd9SAlan Cox * if tracing, save a copy of iovec 2084160ccd9SAlan Cox */ 209b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 2104160ccd9SAlan Cox ktriov = aiov; 21142ebfbf2SBrian Feldman ktruio = auio; 2123c89e357SBrian Feldman didktr = 1; 21342ebfbf2SBrian Feldman } 2144160ccd9SAlan Cox #endif 2158fe387abSDmitrij Tejblum cnt = nbyte; 216279d7226SMatthew Dillon 217b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) { 2184160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 2194160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 2204160ccd9SAlan Cox error = 0; 221279d7226SMatthew Dillon } 2224160ccd9SAlan Cox cnt -= auio.uio_resid; 2234160ccd9SAlan Cox #ifdef KTRACE 2243c89e357SBrian Feldman if (didktr && error == 0) { 22542ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 22642ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 227b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_READ, &ktruio, error); 22842ebfbf2SBrian Feldman } 2294160ccd9SAlan Cox #endif 230b40ce416SJulian Elischer td->td_retval[0] = cnt; 2314160ccd9SAlan Cox return (error); 2324160ccd9SAlan Cox } 2334160ccd9SAlan Cox 2344160ccd9SAlan Cox /* 235df8bae1dSRodney W. Grimes * Scatter read system call. 236df8bae1dSRodney W. Grimes */ 237d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 238df8bae1dSRodney W. Grimes struct readv_args { 2397147b19dSBruce Evans int fd; 240df8bae1dSRodney W. Grimes struct iovec *iovp; 241df8bae1dSRodney W. Grimes u_int iovcnt; 242df8bae1dSRodney W. Grimes }; 243d2d3e875SBruce Evans #endif 244ad2edad9SMatthew Dillon /* 245ad2edad9SMatthew Dillon * MPSAFE 246ad2edad9SMatthew Dillon */ 24726f9a767SRodney W. Grimes int 248b40ce416SJulian Elischer readv(td, uap) 249b40ce416SJulian Elischer struct thread *td; 250df8bae1dSRodney W. Grimes register struct readv_args *uap; 251df8bae1dSRodney W. Grimes { 252df8bae1dSRodney W. Grimes register struct file *fp; 253ad2edad9SMatthew Dillon register struct filedesc *fdp; 254df8bae1dSRodney W. Grimes struct uio auio; 255df8bae1dSRodney W. Grimes register struct iovec *iov; 256df8bae1dSRodney W. Grimes struct iovec *needfree; 257df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 258df8bae1dSRodney W. Grimes long i, cnt, error = 0; 259df8bae1dSRodney W. Grimes u_int iovlen; 260df8bae1dSRodney W. Grimes #ifdef KTRACE 261df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 26242ebfbf2SBrian Feldman struct uio ktruio; 263df8bae1dSRodney W. Grimes #endif 264ad2edad9SMatthew Dillon mtx_lock(&Giant); 265b40ce416SJulian Elischer fdp = td->td_proc->p_fd; 266df8bae1dSRodney W. Grimes 267ad2edad9SMatthew Dillon if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL) { 268ad2edad9SMatthew Dillon error = EBADF; 269ad2edad9SMatthew Dillon goto done2; 270ad2edad9SMatthew Dillon } 271df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 272df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 273df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 274ad2edad9SMatthew Dillon if (uap->iovcnt > UIO_MAXIOV) { 275ad2edad9SMatthew Dillon error = EINVAL; 276ad2edad9SMatthew Dillon goto done2; 277ad2edad9SMatthew Dillon } 278df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 279df8bae1dSRodney W. Grimes needfree = iov; 280df8bae1dSRodney W. Grimes } else { 281df8bae1dSRodney W. Grimes iov = aiov; 282df8bae1dSRodney W. Grimes needfree = NULL; 283df8bae1dSRodney W. Grimes } 284df8bae1dSRodney W. Grimes auio.uio_iov = iov; 285df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 286df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 287df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 288b40ce416SJulian Elischer auio.uio_td = td; 2892c1011f7SJohn Dyson auio.uio_offset = -1; 290bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 291df8bae1dSRodney W. Grimes goto done; 292df8bae1dSRodney W. Grimes auio.uio_resid = 0; 293df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 294069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 295df8bae1dSRodney W. Grimes error = EINVAL; 296df8bae1dSRodney W. Grimes goto done; 297df8bae1dSRodney W. Grimes } 298069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 299df8bae1dSRodney W. Grimes iov++; 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes #ifdef KTRACE 302df8bae1dSRodney W. Grimes /* 303df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 304df8bae1dSRodney W. Grimes */ 305b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 306df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 307df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 30842ebfbf2SBrian Feldman ktruio = auio; 309df8bae1dSRodney W. Grimes } 310df8bae1dSRodney W. Grimes #endif 311df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 312b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) { 313df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 314df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 315df8bae1dSRodney W. Grimes error = 0; 316279d7226SMatthew Dillon } 317df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 318df8bae1dSRodney W. Grimes #ifdef KTRACE 319df8bae1dSRodney W. Grimes if (ktriov != NULL) { 32042ebfbf2SBrian Feldman if (error == 0) { 32142ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 32242ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 323b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio, 32442ebfbf2SBrian Feldman error); 32542ebfbf2SBrian Feldman } 326df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 327df8bae1dSRodney W. Grimes } 328df8bae1dSRodney W. Grimes #endif 329b40ce416SJulian Elischer td->td_retval[0] = cnt; 330df8bae1dSRodney W. Grimes done: 331b40ce416SJulian Elischer fdrop(fp, td); 332df8bae1dSRodney W. Grimes if (needfree) 333df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 334ad2edad9SMatthew Dillon done2: 335ad2edad9SMatthew Dillon mtx_unlock(&Giant); 336df8bae1dSRodney W. Grimes return (error); 337df8bae1dSRodney W. Grimes } 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes /* 340df8bae1dSRodney W. Grimes * Write system call 341df8bae1dSRodney W. Grimes */ 342d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 343df8bae1dSRodney W. Grimes struct write_args { 344df8bae1dSRodney W. Grimes int fd; 345134e06feSBruce Evans const void *buf; 346134e06feSBruce Evans size_t nbyte; 347df8bae1dSRodney W. Grimes }; 348d2d3e875SBruce Evans #endif 349ad2edad9SMatthew Dillon /* 350ad2edad9SMatthew Dillon * MPSAFE 351ad2edad9SMatthew Dillon */ 35226f9a767SRodney W. Grimes int 353b40ce416SJulian Elischer write(td, uap) 354b40ce416SJulian Elischer struct thread *td; 355df8bae1dSRodney W. Grimes register struct write_args *uap; 356df8bae1dSRodney W. Grimes { 357df8bae1dSRodney W. Grimes register struct file *fp; 358279d7226SMatthew Dillon int error; 359df8bae1dSRodney W. Grimes 360ad2edad9SMatthew Dillon mtx_lock(&Giant); 361b40ce416SJulian Elischer if ((fp = holdfp(td->td_proc->p_fd, uap->fd, FWRITE)) != NULL) { 362b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 363ad2edad9SMatthew Dillon (off_t)-1, 0); 364b40ce416SJulian Elischer fdrop(fp, td); 365ad2edad9SMatthew Dillon } else { 366ad2edad9SMatthew Dillon error = EBADF; 367ad2edad9SMatthew Dillon } 368ad2edad9SMatthew Dillon mtx_unlock(&Giant); 369279d7226SMatthew Dillon return(error); 370df8bae1dSRodney W. Grimes } 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes /* 3738fe387abSDmitrij Tejblum * Pwrite system call 3744160ccd9SAlan Cox */ 3754160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3764160ccd9SAlan Cox struct pwrite_args { 3774160ccd9SAlan Cox int fd; 3784160ccd9SAlan Cox const void *buf; 3794160ccd9SAlan Cox size_t nbyte; 3808fe387abSDmitrij Tejblum int pad; 3814160ccd9SAlan Cox off_t offset; 3824160ccd9SAlan Cox }; 3834160ccd9SAlan Cox #endif 384ad2edad9SMatthew Dillon /* 385ad2edad9SMatthew Dillon * MPSAFE 386ad2edad9SMatthew Dillon */ 3874160ccd9SAlan Cox int 388b40ce416SJulian Elischer pwrite(td, uap) 389b40ce416SJulian Elischer struct thread *td; 3904160ccd9SAlan Cox register struct pwrite_args *uap; 3914160ccd9SAlan Cox { 3924160ccd9SAlan Cox register struct file *fp; 393279d7226SMatthew Dillon int error; 3948fe387abSDmitrij Tejblum 395ad2edad9SMatthew Dillon mtx_lock(&Giant); 396b40ce416SJulian Elischer if ((fp = holdfp(td->td_proc->p_fd, uap->fd, FWRITE)) == NULL) { 397ad2edad9SMatthew Dillon error = EBADF; 398ad2edad9SMatthew Dillon } else if (fp->f_type != DTYPE_VNODE) { 399279d7226SMatthew Dillon error = ESPIPE; 400b40ce416SJulian Elischer fdrop(fp, td); 401279d7226SMatthew Dillon } else { 402b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 403279d7226SMatthew Dillon uap->offset, FOF_OFFSET); 404b40ce416SJulian Elischer fdrop(fp, td); 405ad2edad9SMatthew Dillon } 406ad2edad9SMatthew Dillon mtx_unlock(&Giant); 407279d7226SMatthew Dillon return(error); 4088fe387abSDmitrij Tejblum } 4098fe387abSDmitrij Tejblum 4108fe387abSDmitrij Tejblum static int 411b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 412b40ce416SJulian Elischer struct thread *td; 4138fe387abSDmitrij Tejblum struct file *fp; 4148fe387abSDmitrij Tejblum int fd, flags; 4158fe387abSDmitrij Tejblum const void *buf; 4168fe387abSDmitrij Tejblum size_t nbyte; 4178fe387abSDmitrij Tejblum off_t offset; 4188fe387abSDmitrij Tejblum { 4194160ccd9SAlan Cox struct uio auio; 4204160ccd9SAlan Cox struct iovec aiov; 4214160ccd9SAlan Cox long cnt, error = 0; 4224160ccd9SAlan Cox #ifdef KTRACE 4234160ccd9SAlan Cox struct iovec ktriov; 42442ebfbf2SBrian Feldman struct uio ktruio; 4253c89e357SBrian Feldman int didktr = 0; 4264160ccd9SAlan Cox #endif 4274160ccd9SAlan Cox 428b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 4298fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 4304160ccd9SAlan Cox auio.uio_iov = &aiov; 4314160ccd9SAlan Cox auio.uio_iovcnt = 1; 4328fe387abSDmitrij Tejblum auio.uio_offset = offset; 4338fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 4344160ccd9SAlan Cox return (EINVAL); 4358fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 4364160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 4374160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 438b40ce416SJulian Elischer auio.uio_td = td; 4394160ccd9SAlan Cox #ifdef KTRACE 4404160ccd9SAlan Cox /* 44142ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4424160ccd9SAlan Cox */ 443b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 4444160ccd9SAlan Cox ktriov = aiov; 44542ebfbf2SBrian Feldman ktruio = auio; 4463c89e357SBrian Feldman didktr = 1; 44742ebfbf2SBrian Feldman } 4484160ccd9SAlan Cox #endif 4498fe387abSDmitrij Tejblum cnt = nbyte; 450c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 451279d7226SMatthew Dillon bwillwrite(); 452b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) { 4534160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4544160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4554160ccd9SAlan Cox error = 0; 45619eb87d2SJohn Baldwin if (error == EPIPE) { 457b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 458b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 459b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 46019eb87d2SJohn Baldwin } 4614160ccd9SAlan Cox } 4624160ccd9SAlan Cox cnt -= auio.uio_resid; 4634160ccd9SAlan Cox #ifdef KTRACE 4643c89e357SBrian Feldman if (didktr && error == 0) { 46542ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 46642ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 467b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_WRITE, &ktruio, error); 46842ebfbf2SBrian Feldman } 4694160ccd9SAlan Cox #endif 470b40ce416SJulian Elischer td->td_retval[0] = cnt; 4714160ccd9SAlan Cox return (error); 4724160ccd9SAlan Cox } 4734160ccd9SAlan Cox 4744160ccd9SAlan Cox /* 475df8bae1dSRodney W. Grimes * Gather write system call 476df8bae1dSRodney W. Grimes */ 477d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 478df8bae1dSRodney W. Grimes struct writev_args { 479df8bae1dSRodney W. Grimes int fd; 480df8bae1dSRodney W. Grimes struct iovec *iovp; 481df8bae1dSRodney W. Grimes u_int iovcnt; 482df8bae1dSRodney W. Grimes }; 483d2d3e875SBruce Evans #endif 484ad2edad9SMatthew Dillon /* 485ad2edad9SMatthew Dillon * MPSAFE 486ad2edad9SMatthew Dillon */ 48726f9a767SRodney W. Grimes int 488b40ce416SJulian Elischer writev(td, uap) 489b40ce416SJulian Elischer struct thread *td; 490df8bae1dSRodney W. Grimes register struct writev_args *uap; 491df8bae1dSRodney W. Grimes { 492df8bae1dSRodney W. Grimes register struct file *fp; 493ad2edad9SMatthew Dillon register struct filedesc *fdp; 494df8bae1dSRodney W. Grimes struct uio auio; 495df8bae1dSRodney W. Grimes register struct iovec *iov; 496df8bae1dSRodney W. Grimes struct iovec *needfree; 497df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 498df8bae1dSRodney W. Grimes long i, cnt, error = 0; 499df8bae1dSRodney W. Grimes u_int iovlen; 500df8bae1dSRodney W. Grimes #ifdef KTRACE 501df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 50242ebfbf2SBrian Feldman struct uio ktruio; 503df8bae1dSRodney W. Grimes #endif 504df8bae1dSRodney W. Grimes 505ad2edad9SMatthew Dillon mtx_lock(&Giant); 506b40ce416SJulian Elischer fdp = td->td_proc->p_fd; 507ad2edad9SMatthew Dillon if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL) { 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 619ad2edad9SMatthew Dillon mtx_lock(&Giant); 620b40ce416SJulian Elischer fdp = td->td_proc->p_fd; 621df8bae1dSRodney W. Grimes if ((u_int)uap->fd >= fdp->fd_nfiles || 622ad2edad9SMatthew Dillon (fp = fdp->fd_ofiles[uap->fd]) == NULL) { 623ad2edad9SMatthew Dillon error = EBADF; 624ad2edad9SMatthew Dillon goto done2; 625ad2edad9SMatthew Dillon } 626df8bae1dSRodney W. Grimes 627ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 628ad2edad9SMatthew Dillon error = EBADF; 629ad2edad9SMatthew Dillon goto done2; 630ad2edad9SMatthew Dillon } 631df8bae1dSRodney W. Grimes 632df8bae1dSRodney W. Grimes switch (com = uap->com) { 633df8bae1dSRodney W. Grimes case FIONCLEX: 634df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 635ad2edad9SMatthew Dillon goto done2; 636df8bae1dSRodney W. Grimes case FIOCLEX: 637df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 638ad2edad9SMatthew Dillon goto done2; 639df8bae1dSRodney W. Grimes } 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes /* 642df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 643df8bae1dSRodney W. Grimes * copied to/from the user's address space. 644df8bae1dSRodney W. Grimes */ 645df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 646ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 647ad2edad9SMatthew Dillon error = ENOTTY; 648ad2edad9SMatthew Dillon goto done2; 649ad2edad9SMatthew Dillon } 650279d7226SMatthew Dillon 651279d7226SMatthew Dillon fhold(fp); 652279d7226SMatthew Dillon 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); 667ad2edad9SMatthew Dillon goto done2; 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: 685bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 686df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 687df8bae1dSRodney W. Grimes else 688df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 689b40ce416SJulian Elischer error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td); 690df8bae1dSRodney W. Grimes break; 691df8bae1dSRodney W. Grimes 692df8bae1dSRodney W. Grimes case FIOASYNC: 693bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 694df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 695df8bae1dSRodney W. Grimes else 696df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 697b40ce416SJulian Elischer error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td); 698df8bae1dSRodney W. Grimes break; 699df8bae1dSRodney W. Grimes 700df8bae1dSRodney W. Grimes default: 701b40ce416SJulian Elischer error = fo_ioctl(fp, com, data, td); 702df8bae1dSRodney W. Grimes /* 703df8bae1dSRodney W. Grimes * Copy any data to user, size was 704df8bae1dSRodney W. Grimes * already set and checked above. 705df8bae1dSRodney W. Grimes */ 706df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 707df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 708df8bae1dSRodney W. Grimes break; 709df8bae1dSRodney W. Grimes } 710df8bae1dSRodney W. Grimes if (memp) 711df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 712b40ce416SJulian Elischer fdrop(fp, td); 713ad2edad9SMatthew Dillon done2: 714ad2edad9SMatthew Dillon mtx_unlock(&Giant); 715df8bae1dSRodney W. Grimes return (error); 716df8bae1dSRodney W. Grimes } 717df8bae1dSRodney W. Grimes 7188cb96f20SPeter Wemm static int nselcoll; /* Select collisions since boot */ 719265fc98fSSeigo Tanimura struct cv selwait; 7208cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 721df8bae1dSRodney W. Grimes 722df8bae1dSRodney W. Grimes /* 723df8bae1dSRodney W. Grimes * Select system call. 724df8bae1dSRodney W. Grimes */ 725d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 726df8bae1dSRodney W. Grimes struct select_args { 727b08f7993SSujal Patel int nd; 728df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 729df8bae1dSRodney W. Grimes struct timeval *tv; 730df8bae1dSRodney W. Grimes }; 731d2d3e875SBruce Evans #endif 732ad2edad9SMatthew Dillon /* 733ad2edad9SMatthew Dillon * MPSAFE 734ad2edad9SMatthew Dillon */ 73526f9a767SRodney W. Grimes int 736b40ce416SJulian Elischer select(td, uap) 737b40ce416SJulian Elischer register struct thread *td; 738df8bae1dSRodney W. Grimes register struct select_args *uap; 739df8bae1dSRodney W. Grimes { 740d5e4d7e1SBruce Evans /* 741d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 742d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 743d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 744d5e4d7e1SBruce Evans * of 256. 745d5e4d7e1SBruce Evans */ 746d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 747265fc98fSSeigo Tanimura fd_mask s_heldbits[howmany(2048, NFDBITS)]; 748265fc98fSSeigo Tanimura fd_mask *ibits[3], *obits[3], *selbits, *sbp, *heldbits, *hibits, *hobits; 74900af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 750265fc98fSSeigo Tanimura int ncoll, error, timo, i; 751d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 752df8bae1dSRodney W. Grimes 753b08f7993SSujal Patel if (uap->nd < 0) 754acbfbfeaSSujal Patel return (EINVAL); 755ad2edad9SMatthew Dillon 756ad2edad9SMatthew Dillon mtx_lock(&Giant); 757ad2edad9SMatthew Dillon 758b40ce416SJulian Elischer if (uap->nd > td->td_proc->p_fd->fd_nfiles) 759b40ce416SJulian Elischer uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 760b08f7993SSujal Patel 761d5e4d7e1SBruce Evans /* 762d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 763d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 764d5e4d7e1SBruce Evans */ 765d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 766d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 767d5e4d7e1SBruce Evans nbufbytes = 0; 768d5e4d7e1SBruce Evans if (uap->in != NULL) 769d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 770d5e4d7e1SBruce Evans if (uap->ou != NULL) 771d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 772d5e4d7e1SBruce Evans if (uap->ex != NULL) 773d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 774d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 775d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 776d5e4d7e1SBruce Evans else 777d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 778265fc98fSSeigo Tanimura if (2 * ncpbytes <= sizeof s_heldbits) { 779265fc98fSSeigo Tanimura bzero(s_heldbits, sizeof(s_heldbits)); 780265fc98fSSeigo Tanimura heldbits = &s_heldbits[0]; 781265fc98fSSeigo Tanimura } else 782265fc98fSSeigo Tanimura heldbits = malloc(2 * ncpbytes, M_SELECT, M_WAITOK | M_ZERO); 783b08f7993SSujal Patel 784b08f7993SSujal Patel /* 785d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 786d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 787d5e4d7e1SBruce Evans * together. 788b08f7993SSujal Patel */ 789d5e4d7e1SBruce Evans sbp = selbits; 790265fc98fSSeigo Tanimura hibits = heldbits + ncpbytes / sizeof *heldbits; 791265fc98fSSeigo Tanimura hobits = heldbits; 792df8bae1dSRodney W. Grimes #define getbits(name, x) \ 793d5e4d7e1SBruce Evans do { \ 794d5e4d7e1SBruce Evans if (uap->name == NULL) \ 795d5e4d7e1SBruce Evans ibits[x] = NULL; \ 796d5e4d7e1SBruce Evans else { \ 797d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 798d5e4d7e1SBruce Evans obits[x] = sbp; \ 799d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 800d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 801265fc98fSSeigo Tanimura if (error != 0) \ 802265fc98fSSeigo Tanimura goto done_noproclock; \ 803265fc98fSSeigo Tanimura for (i = 0; \ 804265fc98fSSeigo Tanimura i < ncpbytes / sizeof ibits[i][0]; \ 805265fc98fSSeigo Tanimura i++) \ 806265fc98fSSeigo Tanimura hibits[i] |= ibits[x][i]; \ 807e04ac2feSJohn Baldwin } \ 808d5e4d7e1SBruce Evans } while (0) 809df8bae1dSRodney W. Grimes getbits(in, 0); 810df8bae1dSRodney W. Grimes getbits(ou, 1); 811df8bae1dSRodney W. Grimes getbits(ex, 2); 812df8bae1dSRodney W. Grimes #undef getbits 813d5e4d7e1SBruce Evans if (nbufbytes != 0) 814d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 815df8bae1dSRodney W. Grimes 816df8bae1dSRodney W. Grimes if (uap->tv) { 817df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 818df8bae1dSRodney W. Grimes sizeof (atv)); 819265fc98fSSeigo Tanimura if (error) 820265fc98fSSeigo Tanimura goto done_noproclock; 821df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 822df8bae1dSRodney W. Grimes error = EINVAL; 823265fc98fSSeigo Tanimura goto done_noproclock; 824df8bae1dSRodney W. Grimes } 825c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 82600af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8279c386f6bSJohn Baldwin } else { 82800af9731SPoul-Henning Kamp atv.tv_sec = 0; 8299c386f6bSJohn Baldwin atv.tv_usec = 0; 8309c386f6bSJohn Baldwin } 831b40ce416SJulian Elischer selholddrop(td, hibits, hobits, uap->nd, 1); 83200af9731SPoul-Henning Kamp timo = 0; 833b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 834df8bae1dSRodney W. Grimes retry: 835df8bae1dSRodney W. Grimes ncoll = nselcoll; 836b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 837b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 838b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 839b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 840b40ce416SJulian Elischer if (error || td->td_retval[0]) 841df8bae1dSRodney W. Grimes goto done; 8424da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 843c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 8441b369704SSeigo Tanimura if (timevalcmp(&rtv, &atv, >=)) { 8451b369704SSeigo Tanimura /* 8461b369704SSeigo Tanimura * An event of our interest may occur during locking a process. 8471b369704SSeigo Tanimura * In order to avoid missing the event that occured during locking 848b40ce416SJulian Elischer * the process, test TDF_SELECT and rescan file descriptors if 8491b369704SSeigo Tanimura * necessary. 8501b369704SSeigo Tanimura */ 851b40ce416SJulian Elischer if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 8521b369704SSeigo Tanimura ncoll = nselcoll; 853b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 854b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 855b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 856b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 8571b369704SSeigo Tanimura } 858df8bae1dSRodney W. Grimes goto done; 8591b369704SSeigo Tanimura } 86000af9731SPoul-Henning Kamp ttv = atv; 86100af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 86200af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 86300af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 864df8bae1dSRodney W. Grimes } 865b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 866bfbbc4aaSJason Evans 867265fc98fSSeigo Tanimura if (timo > 0) 868b40ce416SJulian Elischer error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo); 869265fc98fSSeigo Tanimura else 870b40ce416SJulian Elischer error = cv_wait_sig(&selwait, &td->td_proc->p_mtx); 871bfbbc4aaSJason Evans 872df8bae1dSRodney W. Grimes if (error == 0) 873df8bae1dSRodney W. Grimes goto retry; 874265fc98fSSeigo Tanimura 875df8bae1dSRodney W. Grimes done: 876b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 877b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 878b40ce416SJulian Elischer selholddrop(td, hibits, hobits, uap->nd, 0); 879265fc98fSSeigo Tanimura done_noproclock: 880df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 881df8bae1dSRodney W. Grimes if (error == ERESTART) 882df8bae1dSRodney W. Grimes error = EINTR; 883df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 884df8bae1dSRodney W. Grimes error = 0; 885df8bae1dSRodney W. Grimes #define putbits(name, x) \ 886d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 887df8bae1dSRodney W. Grimes error = error2; 888df8bae1dSRodney W. Grimes if (error == 0) { 889df8bae1dSRodney W. Grimes int error2; 890df8bae1dSRodney W. Grimes 891df8bae1dSRodney W. Grimes putbits(in, 0); 892df8bae1dSRodney W. Grimes putbits(ou, 1); 893df8bae1dSRodney W. Grimes putbits(ex, 2); 894df8bae1dSRodney W. Grimes #undef putbits 895df8bae1dSRodney W. Grimes } 896d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 897d5e4d7e1SBruce Evans free(selbits, M_SELECT); 898265fc98fSSeigo Tanimura if (heldbits != &s_heldbits[0]) 899265fc98fSSeigo Tanimura free(heldbits, M_SELECT); 900ad2edad9SMatthew Dillon 901ad2edad9SMatthew Dillon mtx_unlock(&Giant); 902df8bae1dSRodney W. Grimes return (error); 903df8bae1dSRodney W. Grimes } 904df8bae1dSRodney W. Grimes 90587b6de2bSPoul-Henning Kamp static int 906b40ce416SJulian Elischer selholddrop(td, ibits, obits, nfd, hold) 907b40ce416SJulian Elischer struct thread *td; 908265fc98fSSeigo Tanimura fd_mask *ibits, *obits; 909265fc98fSSeigo Tanimura int nfd, hold; 910265fc98fSSeigo Tanimura { 911b40ce416SJulian Elischer struct filedesc *fdp = td->td_proc->p_fd; 912265fc98fSSeigo Tanimura int i, fd; 913265fc98fSSeigo Tanimura fd_mask bits; 914265fc98fSSeigo Tanimura struct file *fp; 915265fc98fSSeigo Tanimura 916265fc98fSSeigo Tanimura for (i = 0; i < nfd; i += NFDBITS) { 917265fc98fSSeigo Tanimura if (hold) 918265fc98fSSeigo Tanimura bits = ibits[i/NFDBITS]; 919265fc98fSSeigo Tanimura else 920265fc98fSSeigo Tanimura bits = obits[i/NFDBITS]; 921265fc98fSSeigo Tanimura /* ffs(int mask) not portable, fd_mask is long */ 922265fc98fSSeigo Tanimura for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 923265fc98fSSeigo Tanimura if (!(bits & 1)) 924265fc98fSSeigo Tanimura continue; 925265fc98fSSeigo Tanimura fp = fdp->fd_ofiles[fd]; 926265fc98fSSeigo Tanimura if (fp == NULL) 927265fc98fSSeigo Tanimura return (EBADF); 928265fc98fSSeigo Tanimura if (hold) { 929265fc98fSSeigo Tanimura fhold(fp); 930265fc98fSSeigo Tanimura obits[(fd)/NFDBITS] |= 931265fc98fSSeigo Tanimura ((fd_mask)1 << ((fd) % NFDBITS)); 932265fc98fSSeigo Tanimura } else 933b40ce416SJulian Elischer fdrop(fp, td); 934265fc98fSSeigo Tanimura } 935265fc98fSSeigo Tanimura } 936265fc98fSSeigo Tanimura return (0); 937265fc98fSSeigo Tanimura } 938265fc98fSSeigo Tanimura 939265fc98fSSeigo Tanimura static int 940b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 941b40ce416SJulian Elischer struct thread *td; 942b08f7993SSujal Patel fd_mask **ibits, **obits; 943cb226aaaSPoul-Henning Kamp int nfd; 944df8bae1dSRodney W. Grimes { 945b40ce416SJulian Elischer struct filedesc *fdp = td->td_proc->p_fd; 946f082218cSPeter Wemm int msk, i, fd; 947f082218cSPeter Wemm fd_mask bits; 948df8bae1dSRodney W. Grimes struct file *fp; 949df8bae1dSRodney W. Grimes int n = 0; 9502087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 95142d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 952df8bae1dSRodney W. Grimes 953df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 954d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 955d5e4d7e1SBruce Evans continue; 956df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 957b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 958f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 959f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 960f082218cSPeter Wemm if (!(bits & 1)) 961f082218cSPeter Wemm continue; 962df8bae1dSRodney W. Grimes fp = fdp->fd_ofiles[fd]; 963df8bae1dSRodney W. Grimes if (fp == NULL) 964df8bae1dSRodney W. Grimes return (EBADF); 965b40ce416SJulian Elischer if (fo_poll(fp, flag[msk], fp->f_cred, td)) { 966b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 967f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 968df8bae1dSRodney W. Grimes n++; 969df8bae1dSRodney W. Grimes } 970df8bae1dSRodney W. Grimes } 971df8bae1dSRodney W. Grimes } 972df8bae1dSRodney W. Grimes } 973b40ce416SJulian Elischer td->td_retval[0] = n; 974df8bae1dSRodney W. Grimes return (0); 975df8bae1dSRodney W. Grimes } 976df8bae1dSRodney W. Grimes 97742d11757SPeter Wemm /* 97842d11757SPeter Wemm * Poll system call. 97942d11757SPeter Wemm */ 98042d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 98142d11757SPeter Wemm struct poll_args { 98242d11757SPeter Wemm struct pollfd *fds; 98342d11757SPeter Wemm u_int nfds; 98442d11757SPeter Wemm int timeout; 98542d11757SPeter Wemm }; 98642d11757SPeter Wemm #endif 987ad2edad9SMatthew Dillon /* 988ad2edad9SMatthew Dillon * MPSAFE 989ad2edad9SMatthew Dillon */ 99042d11757SPeter Wemm int 991b40ce416SJulian Elischer poll(td, uap) 992b40ce416SJulian Elischer struct thread *td; 993ea0237edSJonathan Lemon struct poll_args *uap; 99442d11757SPeter Wemm { 99542d11757SPeter Wemm caddr_t bits; 99642d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 99700af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 998265fc98fSSeigo Tanimura int ncoll, error = 0, timo; 999ea0237edSJonathan Lemon u_int nfds; 100042d11757SPeter Wemm size_t ni; 1001265fc98fSSeigo Tanimura struct pollfd p_heldbits[32]; 1002265fc98fSSeigo Tanimura struct pollfd *heldbits; 100342d11757SPeter Wemm 100489b71647SPeter Wemm nfds = SCARG(uap, nfds); 1005ad2edad9SMatthew Dillon 1006ad2edad9SMatthew Dillon mtx_lock(&Giant); 100789b71647SPeter Wemm /* 10082bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 10092bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 10102bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 10112bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 10122bd5ac33SPeter Wemm * safe, but not overly restrictive. 101389b71647SPeter Wemm */ 1014b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 1015b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 1016ad2edad9SMatthew Dillon error = EINVAL; 1017ad2edad9SMatthew Dillon goto done2; 1018ad2edad9SMatthew Dillon } 101989b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 102042d11757SPeter Wemm if (ni > sizeof(smallbits)) 102142d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 102242d11757SPeter Wemm else 102342d11757SPeter Wemm bits = smallbits; 1024265fc98fSSeigo Tanimura if (ni > sizeof(p_heldbits)) 1025265fc98fSSeigo Tanimura heldbits = malloc(ni, M_TEMP, M_WAITOK); 1026265fc98fSSeigo Tanimura else { 1027265fc98fSSeigo Tanimura bzero(p_heldbits, sizeof(p_heldbits)); 1028265fc98fSSeigo Tanimura heldbits = p_heldbits; 1029265fc98fSSeigo Tanimura } 103042d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 103142d11757SPeter Wemm if (error) 1032265fc98fSSeigo Tanimura goto done_noproclock; 1033265fc98fSSeigo Tanimura bcopy(bits, heldbits, ni); 103442d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 103542d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 103642d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 103742d11757SPeter Wemm if (itimerfix(&atv)) { 103842d11757SPeter Wemm error = EINVAL; 1039265fc98fSSeigo Tanimura goto done_noproclock; 104042d11757SPeter Wemm } 1041c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 104200af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 10439c386f6bSJohn Baldwin } else { 104400af9731SPoul-Henning Kamp atv.tv_sec = 0; 10459c386f6bSJohn Baldwin atv.tv_usec = 0; 10469c386f6bSJohn Baldwin } 1047b40ce416SJulian Elischer pollholddrop(td, heldbits, nfds, 1); 104800af9731SPoul-Henning Kamp timo = 0; 1049b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 105042d11757SPeter Wemm retry: 105142d11757SPeter Wemm ncoll = nselcoll; 1052b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 1053b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1054b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 1055b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 1056b40ce416SJulian Elischer if (error || td->td_retval[0]) 105742d11757SPeter Wemm goto done; 10584da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1059c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 10601b369704SSeigo Tanimura if (timevalcmp(&rtv, &atv, >=)) { 10611b369704SSeigo Tanimura /* 10621b369704SSeigo Tanimura * An event of our interest may occur during locking a process. 10631b369704SSeigo Tanimura * In order to avoid missing the event that occured during locking 1064b40ce416SJulian Elischer * the process, test TDF_SELECT and rescan file descriptors if 10651b369704SSeigo Tanimura * necessary. 10661b369704SSeigo Tanimura */ 1067b40ce416SJulian Elischer if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 10681b369704SSeigo Tanimura ncoll = nselcoll; 1069b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 1070b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1071b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 1072b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 10731b369704SSeigo Tanimura } 107442d11757SPeter Wemm goto done; 10751b369704SSeigo Tanimura } 107600af9731SPoul-Henning Kamp ttv = atv; 107700af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 107800af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 107900af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 108042d11757SPeter Wemm } 1081b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1082265fc98fSSeigo Tanimura if (timo > 0) 1083b40ce416SJulian Elischer error = cv_timedwait_sig(&selwait, &td->td_proc->p_mtx, timo); 1084265fc98fSSeigo Tanimura else 1085b40ce416SJulian Elischer error = cv_wait_sig(&selwait, &td->td_proc->p_mtx); 108642d11757SPeter Wemm if (error == 0) 108742d11757SPeter Wemm goto retry; 1088265fc98fSSeigo Tanimura 108942d11757SPeter Wemm done: 1090b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1091b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 1092b40ce416SJulian Elischer pollholddrop(td, heldbits, nfds, 0); 1093265fc98fSSeigo Tanimura done_noproclock: 109442d11757SPeter Wemm /* poll is not restarted after signals... */ 109542d11757SPeter Wemm if (error == ERESTART) 109642d11757SPeter Wemm error = EINTR; 109742d11757SPeter Wemm if (error == EWOULDBLOCK) 109842d11757SPeter Wemm error = 0; 109942d11757SPeter Wemm if (error == 0) { 110042d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 110142d11757SPeter Wemm if (error) 110242d11757SPeter Wemm goto out; 110342d11757SPeter Wemm } 110442d11757SPeter Wemm out: 110542d11757SPeter Wemm if (ni > sizeof(smallbits)) 110642d11757SPeter Wemm free(bits, M_TEMP); 1107265fc98fSSeigo Tanimura if (ni > sizeof(p_heldbits)) 1108265fc98fSSeigo Tanimura free(heldbits, M_TEMP); 1109ad2edad9SMatthew Dillon done2: 1110ad2edad9SMatthew Dillon mtx_unlock(&Giant); 111142d11757SPeter Wemm return (error); 111242d11757SPeter Wemm } 111342d11757SPeter Wemm 111442d11757SPeter Wemm static int 1115b40ce416SJulian Elischer pollholddrop(td, fds, nfd, hold) 1116b40ce416SJulian Elischer struct thread *td; 1117265fc98fSSeigo Tanimura struct pollfd *fds; 1118265fc98fSSeigo Tanimura u_int nfd; 1119265fc98fSSeigo Tanimura int hold; 1120265fc98fSSeigo Tanimura { 1121b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 1122265fc98fSSeigo Tanimura int i; 1123265fc98fSSeigo Tanimura struct file *fp; 1124265fc98fSSeigo Tanimura 1125265fc98fSSeigo Tanimura for (i = 0; i < nfd; i++, fds++) { 1126265fc98fSSeigo Tanimura if (0 <= fds->fd && fds->fd < fdp->fd_nfiles) { 1127265fc98fSSeigo Tanimura fp = fdp->fd_ofiles[fds->fd]; 1128265fc98fSSeigo Tanimura if (hold) { 1129265fc98fSSeigo Tanimura if (fp != NULL) { 1130265fc98fSSeigo Tanimura fhold(fp); 1131265fc98fSSeigo Tanimura fds->revents = 1; 1132265fc98fSSeigo Tanimura } else 1133265fc98fSSeigo Tanimura fds->revents = 0; 1134265fc98fSSeigo Tanimura } else if(fp != NULL && fds->revents) 1135b40ce416SJulian Elischer fdrop(fp, td); 1136265fc98fSSeigo Tanimura } 1137265fc98fSSeigo Tanimura } 1138265fc98fSSeigo Tanimura return (0); 1139265fc98fSSeigo Tanimura } 1140265fc98fSSeigo Tanimura 1141265fc98fSSeigo Tanimura static int 1142b40ce416SJulian Elischer pollscan(td, fds, nfd) 1143b40ce416SJulian Elischer struct thread *td; 114442d11757SPeter Wemm struct pollfd *fds; 1145ea0237edSJonathan Lemon u_int nfd; 114642d11757SPeter Wemm { 1147b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 114842d11757SPeter Wemm int i; 114942d11757SPeter Wemm struct file *fp; 115042d11757SPeter Wemm int n = 0; 115142d11757SPeter Wemm 115242d11757SPeter Wemm for (i = 0; i < nfd; i++, fds++) { 1153337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 115442d11757SPeter Wemm fds->revents = POLLNVAL; 115542d11757SPeter Wemm n++; 1156337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1157337c9691SJordan K. Hubbard fds->revents = 0; 115842d11757SPeter Wemm } else { 115942d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1160279d7226SMatthew Dillon if (fp == NULL) { 116142d11757SPeter Wemm fds->revents = POLLNVAL; 116242d11757SPeter Wemm n++; 116342d11757SPeter Wemm } else { 11642087c896SBruce Evans /* 11652087c896SBruce Evans * Note: backend also returns POLLHUP and 11662087c896SBruce Evans * POLLERR if appropriate. 11672087c896SBruce Evans */ 116813ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1169b40ce416SJulian Elischer fp->f_cred, td); 117042d11757SPeter Wemm if (fds->revents != 0) 117142d11757SPeter Wemm n++; 117242d11757SPeter Wemm } 117342d11757SPeter Wemm } 117442d11757SPeter Wemm } 1175b40ce416SJulian Elischer td->td_retval[0] = n; 117642d11757SPeter Wemm return (0); 117742d11757SPeter Wemm } 117842d11757SPeter Wemm 117942d11757SPeter Wemm /* 118042d11757SPeter Wemm * OpenBSD poll system call. 118142d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 118242d11757SPeter Wemm */ 118342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 118442d11757SPeter Wemm struct openbsd_poll_args { 118542d11757SPeter Wemm struct pollfd *fds; 118642d11757SPeter Wemm u_int nfds; 118742d11757SPeter Wemm int timeout; 118842d11757SPeter Wemm }; 118942d11757SPeter Wemm #endif 1190ad2edad9SMatthew Dillon /* 1191ad2edad9SMatthew Dillon * MPSAFE 1192ad2edad9SMatthew Dillon */ 119342d11757SPeter Wemm int 1194b40ce416SJulian Elischer openbsd_poll(td, uap) 1195b40ce416SJulian Elischer register struct thread *td; 119642d11757SPeter Wemm register struct openbsd_poll_args *uap; 119742d11757SPeter Wemm { 1198b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 119942d11757SPeter Wemm } 120042d11757SPeter Wemm 1201df8bae1dSRodney W. Grimes /*ARGSUSED*/ 120226f9a767SRodney W. Grimes int 1203b40ce416SJulian Elischer seltrue(dev, events, td) 1204df8bae1dSRodney W. Grimes dev_t dev; 120542d11757SPeter Wemm int events; 1206b40ce416SJulian Elischer struct thread *td; 1207df8bae1dSRodney W. Grimes { 1208df8bae1dSRodney W. Grimes 120942d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1210df8bae1dSRodney W. Grimes } 1211df8bae1dSRodney W. Grimes 1212b40ce416SJulian Elischer static int 1213b40ce416SJulian Elischer find_thread_in_proc(struct proc *p, struct thread *td) 1214b40ce416SJulian Elischer { 1215b40ce416SJulian Elischer struct thread *td2; 1216b40ce416SJulian Elischer FOREACH_THREAD_IN_PROC(p, td2) { 1217b40ce416SJulian Elischer if (td2 == td) { 1218b40ce416SJulian Elischer return (1); 1219b40ce416SJulian Elischer } 1220b40ce416SJulian Elischer } 1221b40ce416SJulian Elischer return (0); 1222b40ce416SJulian Elischer } 1223b40ce416SJulian Elischer 1224df8bae1dSRodney W. Grimes /* 1225df8bae1dSRodney W. Grimes * Record a select request. 1226df8bae1dSRodney W. Grimes */ 1227df8bae1dSRodney W. Grimes void 1228df8bae1dSRodney W. Grimes selrecord(selector, sip) 1229b40ce416SJulian Elischer struct thread *selector; 1230df8bae1dSRodney W. Grimes struct selinfo *sip; 1231df8bae1dSRodney W. Grimes { 1232df8bae1dSRodney W. Grimes struct proc *p; 1233df8bae1dSRodney W. Grimes pid_t mypid; 1234df8bae1dSRodney W. Grimes 1235b40ce416SJulian Elischer mypid = selector->td_proc->p_pid; 1236b40ce416SJulian Elischer if ((sip->si_pid == mypid) && 1237b40ce416SJulian Elischer (sip->si_thread == selector)) { /* XXXKSE should be an ID? */ 1238df8bae1dSRodney W. Grimes return; 1239b40ce416SJulian Elischer } 1240b40ce416SJulian Elischer if (sip->si_pid && 1241b40ce416SJulian Elischer (p = pfind(sip->si_pid)) && 1242b40ce416SJulian Elischer (find_thread_in_proc(p, sip->si_thread))) { 12439ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1244b40ce416SJulian Elischer if (sip->si_thread->td_wchan == (caddr_t)&selwait) { 12459ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 124633a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1247df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 1248e04ac2feSJohn Baldwin return; 1249e04ac2feSJohn Baldwin } 12509ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 125133a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1252e04ac2feSJohn Baldwin } 1253df8bae1dSRodney W. Grimes sip->si_pid = mypid; 1254b40ce416SJulian Elischer sip->si_thread = selector; 1255df8bae1dSRodney W. Grimes } 1256df8bae1dSRodney W. Grimes 1257df8bae1dSRodney W. Grimes /* 1258df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1259df8bae1dSRodney W. Grimes */ 1260df8bae1dSRodney W. Grimes void 1261df8bae1dSRodney W. Grimes selwakeup(sip) 1262df8bae1dSRodney W. Grimes register struct selinfo *sip; 1263df8bae1dSRodney W. Grimes { 1264b40ce416SJulian Elischer struct thread *td; 1265df8bae1dSRodney W. Grimes register struct proc *p; 1266df8bae1dSRodney W. Grimes 1267df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 1268df8bae1dSRodney W. Grimes return; 1269df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 1270df8bae1dSRodney W. Grimes nselcoll++; 1271df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1272265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1273df8bae1dSRodney W. Grimes } 1274df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 1275df8bae1dSRodney W. Grimes sip->si_pid = 0; 1276b40ce416SJulian Elischer td = sip->si_thread; 1277df8bae1dSRodney W. Grimes if (p != NULL) { 1278b40ce416SJulian Elischer if (!find_thread_in_proc(p, td)) { 1279b40ce416SJulian Elischer PROC_UNLOCK(p); /* lock is in pfind() */; 1280b40ce416SJulian Elischer return; 1281b40ce416SJulian Elischer } 12829ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1283b40ce416SJulian Elischer if (td->td_wchan == (caddr_t)&selwait) { 1284b40ce416SJulian Elischer if (td->td_proc->p_stat == SSLEEP) 1285b40ce416SJulian Elischer setrunnable(td); 1286df8bae1dSRodney W. Grimes else 1287b40ce416SJulian Elischer cv_waitq_remove(td); 128833a9ed9dSJohn Baldwin } else 1289b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 129033a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 1291b40ce416SJulian Elischer PROC_UNLOCK(p); /* Lock is in pfind() */ 1292e04ac2feSJohn Baldwin } 1293df8bae1dSRodney W. Grimes } 1294265fc98fSSeigo Tanimura 1295265fc98fSSeigo Tanimura static void selectinit __P((void *)); 1296265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1297265fc98fSSeigo Tanimura 1298265fc98fSSeigo Tanimura /* ARGSUSED*/ 1299265fc98fSSeigo Tanimura static void 1300265fc98fSSeigo Tanimura selectinit(dummy) 1301265fc98fSSeigo Tanimura void *dummy; 1302265fc98fSSeigo Tanimura { 1303265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 1304265fc98fSSeigo Tanimura } 1305