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 77ea0237edSJonathan Lemon static int pollscan __P((struct proc *, struct pollfd *, u_int)); 78265fc98fSSeigo Tanimura static int pollholddrop __P((struct proc *, struct pollfd *, u_int, int)); 792087c896SBruce Evans static int selscan __P((struct proc *, fd_mask **, fd_mask **, int)); 80265fc98fSSeigo Tanimura static int selholddrop __P((struct proc *, fd_mask *, fd_mask *, int, int)); 818fe387abSDmitrij Tejblum static int dofileread __P((struct proc *, struct file *, int, void *, 828fe387abSDmitrij Tejblum size_t, off_t, int)); 838fe387abSDmitrij Tejblum static int dofilewrite __P((struct proc *, 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 116cb226aaaSPoul-Henning Kamp read(p, uap) 117df8bae1dSRodney W. Grimes struct proc *p; 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); 124ad2edad9SMatthew Dillon if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) != NULL) { 125ad2edad9SMatthew Dillon error = dofileread(p, fp, uap->fd, uap->buf, 126ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 127279d7226SMatthew Dillon fdrop(fp, p); 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 1514160ccd9SAlan Cox pread(p, uap) 1524160ccd9SAlan Cox struct proc *p; 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); 159ad2edad9SMatthew Dillon if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL) { 160ad2edad9SMatthew Dillon error = EBADF; 161ad2edad9SMatthew Dillon } else if (fp->f_type != DTYPE_VNODE) { 162279d7226SMatthew Dillon error = ESPIPE; 163ad2edad9SMatthew Dillon fdrop(fp, p); 164279d7226SMatthew Dillon } else { 165279d7226SMatthew Dillon error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte, 166279d7226SMatthew Dillon uap->offset, FOF_OFFSET); 167279d7226SMatthew Dillon fdrop(fp, p); 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 1778fe387abSDmitrij Tejblum dofileread(p, fp, fd, buf, nbyte, offset, flags) 1788fe387abSDmitrij Tejblum struct proc *p; 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; 2044160ccd9SAlan Cox auio.uio_procp = p; 2054160ccd9SAlan Cox #ifdef KTRACE 2064160ccd9SAlan Cox /* 2074160ccd9SAlan Cox * if tracing, save a copy of iovec 2084160ccd9SAlan Cox */ 20942ebfbf2SBrian Feldman if (KTRPOINT(p, 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 217279d7226SMatthew Dillon if ((error = fo_read(fp, &auio, fp->f_cred, flags, p))) { 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; 22742ebfbf2SBrian Feldman ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error); 22842ebfbf2SBrian Feldman } 2294160ccd9SAlan Cox #endif 2304160ccd9SAlan Cox p->p_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 248cb226aaaSPoul-Henning Kamp readv(p, uap) 249df8bae1dSRodney W. Grimes struct proc *p; 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); 265ad2edad9SMatthew Dillon fdp = p->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; 288df8bae1dSRodney W. Grimes auio.uio_procp = p; 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 */ 305df8bae1dSRodney W. Grimes if (KTRPOINT(p, 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; 312279d7226SMatthew Dillon if ((error = fo_read(fp, &auio, fp->f_cred, 0, p))) { 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; 32342ebfbf2SBrian Feldman ktrgenio(p->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 329cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 330df8bae1dSRodney W. Grimes done: 331279d7226SMatthew Dillon fdrop(fp, p); 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 353cb226aaaSPoul-Henning Kamp write(p, uap) 354df8bae1dSRodney W. Grimes struct proc *p; 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); 361ad2edad9SMatthew Dillon if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) != NULL) { 362ad2edad9SMatthew Dillon error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, 363ad2edad9SMatthew Dillon (off_t)-1, 0); 364279d7226SMatthew Dillon fdrop(fp, p); 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 3884160ccd9SAlan Cox pwrite(p, uap) 3894160ccd9SAlan Cox struct proc *p; 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); 396ad2edad9SMatthew Dillon if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL) { 397ad2edad9SMatthew Dillon error = EBADF; 398ad2edad9SMatthew Dillon } else if (fp->f_type != DTYPE_VNODE) { 399279d7226SMatthew Dillon error = ESPIPE; 400ad2edad9SMatthew Dillon fdrop(fp, p); 401279d7226SMatthew Dillon } else { 402279d7226SMatthew Dillon error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, 403279d7226SMatthew Dillon uap->offset, FOF_OFFSET); 404279d7226SMatthew Dillon fdrop(fp, p); 405ad2edad9SMatthew Dillon } 406ad2edad9SMatthew Dillon mtx_unlock(&Giant); 407279d7226SMatthew Dillon return(error); 4088fe387abSDmitrij Tejblum } 4098fe387abSDmitrij Tejblum 4108fe387abSDmitrij Tejblum static int 4118fe387abSDmitrij Tejblum dofilewrite(p, fp, fd, buf, nbyte, offset, flags) 4128fe387abSDmitrij Tejblum struct proc *p; 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; 4384160ccd9SAlan Cox auio.uio_procp = p; 4394160ccd9SAlan Cox #ifdef KTRACE 4404160ccd9SAlan Cox /* 44142ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4424160ccd9SAlan Cox */ 44342ebfbf2SBrian Feldman if (KTRPOINT(p, 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(); 45213ccadd4SBrian Feldman if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) { 4534160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4544160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4554160ccd9SAlan Cox error = 0; 45619eb87d2SJohn Baldwin if (error == EPIPE) { 45719eb87d2SJohn Baldwin PROC_LOCK(p); 4584160ccd9SAlan Cox psignal(p, SIGPIPE); 45919eb87d2SJohn Baldwin PROC_UNLOCK(p); 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; 46742ebfbf2SBrian Feldman ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error); 46842ebfbf2SBrian Feldman } 4694160ccd9SAlan Cox #endif 4704160ccd9SAlan Cox p->p_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 488cb226aaaSPoul-Henning Kamp writev(p, uap) 489df8bae1dSRodney W. Grimes struct proc *p; 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); 506ad2edad9SMatthew Dillon fdp = p->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; 529df8bae1dSRodney W. Grimes auio.uio_procp = p; 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 */ 546df8bae1dSRodney W. Grimes if (KTRPOINT(p, 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(); 55513ccadd4SBrian Feldman if ((error = fo_write(fp, &auio, fp->f_cred, 0, p))) { 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) { 56019eb87d2SJohn Baldwin PROC_LOCK(p); 561df8bae1dSRodney W. Grimes psignal(p, SIGPIPE); 56219eb87d2SJohn Baldwin PROC_UNLOCK(p); 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; 57142ebfbf2SBrian Feldman ktrgenio(p->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 577cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 578df8bae1dSRodney W. Grimes done: 579d8177437SBrian Feldman fdrop(fp, p); 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 602cb226aaaSPoul-Henning Kamp ioctl(p, uap) 603df8bae1dSRodney W. Grimes struct proc *p; 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); 620df8bae1dSRodney W. Grimes fdp = p->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); 666279d7226SMatthew Dillon fdrop(fp, p); 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; 68913ccadd4SBrian Feldman error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p); 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; 69713ccadd4SBrian Feldman error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p); 698df8bae1dSRodney W. Grimes break; 699df8bae1dSRodney W. Grimes 700df8bae1dSRodney W. Grimes default: 70113ccadd4SBrian Feldman error = fo_ioctl(fp, com, data, p); 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); 712279d7226SMatthew Dillon fdrop(fp, p); 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 736cb226aaaSPoul-Henning Kamp select(p, uap) 737df8bae1dSRodney W. Grimes register struct proc *p; 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 758df8bae1dSRodney W. Grimes if (uap->nd > p->p_fd->fd_nfiles) 759df8bae1dSRodney W. Grimes uap->nd = p->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 } 831265fc98fSSeigo Tanimura selholddrop(p, hibits, hobits, uap->nd, 1); 83200af9731SPoul-Henning Kamp timo = 0; 833e04ac2feSJohn Baldwin PROC_LOCK(p); 834df8bae1dSRodney W. Grimes retry: 835df8bae1dSRodney W. Grimes ncoll = nselcoll; 836df8bae1dSRodney W. Grimes p->p_flag |= P_SELECT; 8371b369704SSeigo Tanimura PROC_UNLOCK(p); 838cb226aaaSPoul-Henning Kamp error = selscan(p, ibits, obits, uap->nd); 8391b369704SSeigo Tanimura PROC_LOCK(p); 840cb226aaaSPoul-Henning Kamp if (error || p->p_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 8481b369704SSeigo Tanimura * the process, test P_SELECT and rescan file descriptors if 8491b369704SSeigo Tanimura * necessary. 8501b369704SSeigo Tanimura */ 8511b369704SSeigo Tanimura if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 8521b369704SSeigo Tanimura ncoll = nselcoll; 8531b369704SSeigo Tanimura p->p_flag |= P_SELECT; 8541b369704SSeigo Tanimura PROC_UNLOCK(p); 8551b369704SSeigo Tanimura error = selscan(p, ibits, obits, uap->nd); 8561b369704SSeigo Tanimura PROC_LOCK(p); 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 } 865df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 866bfbbc4aaSJason Evans 867265fc98fSSeigo Tanimura if (timo > 0) 868265fc98fSSeigo Tanimura error = cv_timedwait_sig(&selwait, &p->p_mtx, timo); 869265fc98fSSeigo Tanimura else 870265fc98fSSeigo Tanimura error = cv_wait_sig(&selwait, &p->p_mtx); 871bfbbc4aaSJason Evans 872df8bae1dSRodney W. Grimes if (error == 0) 873df8bae1dSRodney W. Grimes goto retry; 874265fc98fSSeigo Tanimura 875df8bae1dSRodney W. Grimes done: 876df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 877e04ac2feSJohn Baldwin PROC_UNLOCK(p); 878265fc98fSSeigo Tanimura selholddrop(p, 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 906265fc98fSSeigo Tanimura selholddrop(p, ibits, obits, nfd, hold) 907265fc98fSSeigo Tanimura struct proc *p; 908265fc98fSSeigo Tanimura fd_mask *ibits, *obits; 909265fc98fSSeigo Tanimura int nfd, hold; 910265fc98fSSeigo Tanimura { 911265fc98fSSeigo Tanimura struct filedesc *fdp = p->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 933265fc98fSSeigo Tanimura fdrop(fp, p); 934265fc98fSSeigo Tanimura } 935265fc98fSSeigo Tanimura } 936265fc98fSSeigo Tanimura return (0); 937265fc98fSSeigo Tanimura } 938265fc98fSSeigo Tanimura 939265fc98fSSeigo Tanimura static int 940cb226aaaSPoul-Henning Kamp selscan(p, ibits, obits, nfd) 941df8bae1dSRodney W. Grimes struct proc *p; 942b08f7993SSujal Patel fd_mask **ibits, **obits; 943cb226aaaSPoul-Henning Kamp int nfd; 944df8bae1dSRodney W. Grimes { 945f082218cSPeter Wemm struct filedesc *fdp = p->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); 96513ccadd4SBrian Feldman if (fo_poll(fp, flag[msk], fp->f_cred, p)) { 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 } 973cb226aaaSPoul-Henning Kamp p->p_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 991cb226aaaSPoul-Henning Kamp poll(p, uap) 992ea0237edSJonathan Lemon struct proc *p; 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 */ 1014ad2edad9SMatthew Dillon if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE) { 1015ad2edad9SMatthew Dillon error = EINVAL; 1016ad2edad9SMatthew Dillon goto done2; 1017ad2edad9SMatthew Dillon } 101889b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 101942d11757SPeter Wemm if (ni > sizeof(smallbits)) 102042d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 102142d11757SPeter Wemm else 102242d11757SPeter Wemm bits = smallbits; 1023265fc98fSSeigo Tanimura if (ni > sizeof(p_heldbits)) 1024265fc98fSSeigo Tanimura heldbits = malloc(ni, M_TEMP, M_WAITOK); 1025265fc98fSSeigo Tanimura else { 1026265fc98fSSeigo Tanimura bzero(p_heldbits, sizeof(p_heldbits)); 1027265fc98fSSeigo Tanimura heldbits = p_heldbits; 1028265fc98fSSeigo Tanimura } 102942d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 103042d11757SPeter Wemm if (error) 1031265fc98fSSeigo Tanimura goto done_noproclock; 1032265fc98fSSeigo Tanimura bcopy(bits, heldbits, ni); 103342d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 103442d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 103542d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 103642d11757SPeter Wemm if (itimerfix(&atv)) { 103742d11757SPeter Wemm error = EINVAL; 1038265fc98fSSeigo Tanimura goto done_noproclock; 103942d11757SPeter Wemm } 1040c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 104100af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 10429c386f6bSJohn Baldwin } else { 104300af9731SPoul-Henning Kamp atv.tv_sec = 0; 10449c386f6bSJohn Baldwin atv.tv_usec = 0; 10459c386f6bSJohn Baldwin } 1046265fc98fSSeigo Tanimura pollholddrop(p, heldbits, nfds, 1); 104700af9731SPoul-Henning Kamp timo = 0; 1048265fc98fSSeigo Tanimura PROC_LOCK(p); 104942d11757SPeter Wemm retry: 105042d11757SPeter Wemm ncoll = nselcoll; 105142d11757SPeter Wemm p->p_flag |= P_SELECT; 10521b369704SSeigo Tanimura PROC_UNLOCK(p); 105389b71647SPeter Wemm error = pollscan(p, (struct pollfd *)bits, nfds); 10541b369704SSeigo Tanimura PROC_LOCK(p); 1055cb226aaaSPoul-Henning Kamp if (error || p->p_retval[0]) 105642d11757SPeter Wemm goto done; 10574da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1058c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 10591b369704SSeigo Tanimura if (timevalcmp(&rtv, &atv, >=)) { 10601b369704SSeigo Tanimura /* 10611b369704SSeigo Tanimura * An event of our interest may occur during locking a process. 10621b369704SSeigo Tanimura * In order to avoid missing the event that occured during locking 10631b369704SSeigo Tanimura * the process, test P_SELECT and rescan file descriptors if 10641b369704SSeigo Tanimura * necessary. 10651b369704SSeigo Tanimura */ 10661b369704SSeigo Tanimura if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 10671b369704SSeigo Tanimura ncoll = nselcoll; 10681b369704SSeigo Tanimura p->p_flag |= P_SELECT; 10691b369704SSeigo Tanimura PROC_UNLOCK(p); 10701b369704SSeigo Tanimura error = pollscan(p, (struct pollfd *)bits, nfds); 10711b369704SSeigo Tanimura PROC_LOCK(p); 10721b369704SSeigo Tanimura } 107342d11757SPeter Wemm goto done; 10741b369704SSeigo Tanimura } 107500af9731SPoul-Henning Kamp ttv = atv; 107600af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 107700af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 107800af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 107942d11757SPeter Wemm } 108042d11757SPeter Wemm p->p_flag &= ~P_SELECT; 1081265fc98fSSeigo Tanimura if (timo > 0) 1082265fc98fSSeigo Tanimura error = cv_timedwait_sig(&selwait, &p->p_mtx, timo); 1083265fc98fSSeigo Tanimura else 1084265fc98fSSeigo Tanimura error = cv_wait_sig(&selwait, &p->p_mtx); 108542d11757SPeter Wemm if (error == 0) 108642d11757SPeter Wemm goto retry; 1087265fc98fSSeigo Tanimura 108842d11757SPeter Wemm done: 108942d11757SPeter Wemm p->p_flag &= ~P_SELECT; 1090e04ac2feSJohn Baldwin PROC_UNLOCK(p); 1091265fc98fSSeigo Tanimura pollholddrop(p, heldbits, nfds, 0); 1092265fc98fSSeigo Tanimura done_noproclock: 109342d11757SPeter Wemm /* poll is not restarted after signals... */ 109442d11757SPeter Wemm if (error == ERESTART) 109542d11757SPeter Wemm error = EINTR; 109642d11757SPeter Wemm if (error == EWOULDBLOCK) 109742d11757SPeter Wemm error = 0; 109842d11757SPeter Wemm if (error == 0) { 109942d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 110042d11757SPeter Wemm if (error) 110142d11757SPeter Wemm goto out; 110242d11757SPeter Wemm } 110342d11757SPeter Wemm out: 110442d11757SPeter Wemm if (ni > sizeof(smallbits)) 110542d11757SPeter Wemm free(bits, M_TEMP); 1106265fc98fSSeigo Tanimura if (ni > sizeof(p_heldbits)) 1107265fc98fSSeigo Tanimura free(heldbits, M_TEMP); 1108ad2edad9SMatthew Dillon done2: 1109ad2edad9SMatthew Dillon mtx_unlock(&Giant); 111042d11757SPeter Wemm return (error); 111142d11757SPeter Wemm } 111242d11757SPeter Wemm 111342d11757SPeter Wemm static int 1114265fc98fSSeigo Tanimura pollholddrop(p, fds, nfd, hold) 1115265fc98fSSeigo Tanimura struct proc *p; 1116265fc98fSSeigo Tanimura struct pollfd *fds; 1117265fc98fSSeigo Tanimura u_int nfd; 1118265fc98fSSeigo Tanimura int hold; 1119265fc98fSSeigo Tanimura { 1120265fc98fSSeigo Tanimura register struct filedesc *fdp = p->p_fd; 1121265fc98fSSeigo Tanimura int i; 1122265fc98fSSeigo Tanimura struct file *fp; 1123265fc98fSSeigo Tanimura 1124265fc98fSSeigo Tanimura for (i = 0; i < nfd; i++, fds++) { 1125265fc98fSSeigo Tanimura if (0 <= fds->fd && fds->fd < fdp->fd_nfiles) { 1126265fc98fSSeigo Tanimura fp = fdp->fd_ofiles[fds->fd]; 1127265fc98fSSeigo Tanimura if (hold) { 1128265fc98fSSeigo Tanimura if (fp != NULL) { 1129265fc98fSSeigo Tanimura fhold(fp); 1130265fc98fSSeigo Tanimura fds->revents = 1; 1131265fc98fSSeigo Tanimura } else 1132265fc98fSSeigo Tanimura fds->revents = 0; 1133265fc98fSSeigo Tanimura } else if(fp != NULL && fds->revents) 1134265fc98fSSeigo Tanimura fdrop(fp, p); 1135265fc98fSSeigo Tanimura } 1136265fc98fSSeigo Tanimura } 1137265fc98fSSeigo Tanimura return (0); 1138265fc98fSSeigo Tanimura } 1139265fc98fSSeigo Tanimura 1140265fc98fSSeigo Tanimura static int 1141cb226aaaSPoul-Henning Kamp pollscan(p, fds, nfd) 114242d11757SPeter Wemm struct proc *p; 114342d11757SPeter Wemm struct pollfd *fds; 1144ea0237edSJonathan Lemon u_int nfd; 114542d11757SPeter Wemm { 114642d11757SPeter Wemm register struct filedesc *fdp = p->p_fd; 114742d11757SPeter Wemm int i; 114842d11757SPeter Wemm struct file *fp; 114942d11757SPeter Wemm int n = 0; 115042d11757SPeter Wemm 115142d11757SPeter Wemm for (i = 0; i < nfd; i++, fds++) { 1152337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 115342d11757SPeter Wemm fds->revents = POLLNVAL; 115442d11757SPeter Wemm n++; 1155337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1156337c9691SJordan K. Hubbard fds->revents = 0; 115742d11757SPeter Wemm } else { 115842d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1159279d7226SMatthew Dillon if (fp == NULL) { 116042d11757SPeter Wemm fds->revents = POLLNVAL; 116142d11757SPeter Wemm n++; 116242d11757SPeter Wemm } else { 11632087c896SBruce Evans /* 11642087c896SBruce Evans * Note: backend also returns POLLHUP and 11652087c896SBruce Evans * POLLERR if appropriate. 11662087c896SBruce Evans */ 116713ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 116813ccadd4SBrian Feldman fp->f_cred, p); 116942d11757SPeter Wemm if (fds->revents != 0) 117042d11757SPeter Wemm n++; 117142d11757SPeter Wemm } 117242d11757SPeter Wemm } 117342d11757SPeter Wemm } 1174cb226aaaSPoul-Henning Kamp p->p_retval[0] = n; 117542d11757SPeter Wemm return (0); 117642d11757SPeter Wemm } 117742d11757SPeter Wemm 117842d11757SPeter Wemm /* 117942d11757SPeter Wemm * OpenBSD poll system call. 118042d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 118142d11757SPeter Wemm */ 118242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 118342d11757SPeter Wemm struct openbsd_poll_args { 118442d11757SPeter Wemm struct pollfd *fds; 118542d11757SPeter Wemm u_int nfds; 118642d11757SPeter Wemm int timeout; 118742d11757SPeter Wemm }; 118842d11757SPeter Wemm #endif 1189ad2edad9SMatthew Dillon /* 1190ad2edad9SMatthew Dillon * MPSAFE 1191ad2edad9SMatthew Dillon */ 119242d11757SPeter Wemm int 1193cb226aaaSPoul-Henning Kamp openbsd_poll(p, uap) 119442d11757SPeter Wemm register struct proc *p; 119542d11757SPeter Wemm register struct openbsd_poll_args *uap; 119642d11757SPeter Wemm { 1197cb226aaaSPoul-Henning Kamp return (poll(p, (struct poll_args *)uap)); 119842d11757SPeter Wemm } 119942d11757SPeter Wemm 1200df8bae1dSRodney W. Grimes /*ARGSUSED*/ 120126f9a767SRodney W. Grimes int 120242d11757SPeter Wemm seltrue(dev, events, p) 1203df8bae1dSRodney W. Grimes dev_t dev; 120442d11757SPeter Wemm int events; 1205df8bae1dSRodney W. Grimes struct proc *p; 1206df8bae1dSRodney W. Grimes { 1207df8bae1dSRodney W. Grimes 120842d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1209df8bae1dSRodney W. Grimes } 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes /* 1212df8bae1dSRodney W. Grimes * Record a select request. 1213df8bae1dSRodney W. Grimes */ 1214df8bae1dSRodney W. Grimes void 1215df8bae1dSRodney W. Grimes selrecord(selector, sip) 1216df8bae1dSRodney W. Grimes struct proc *selector; 1217df8bae1dSRodney W. Grimes struct selinfo *sip; 1218df8bae1dSRodney W. Grimes { 1219df8bae1dSRodney W. Grimes struct proc *p; 1220df8bae1dSRodney W. Grimes pid_t mypid; 1221df8bae1dSRodney W. Grimes 1222df8bae1dSRodney W. Grimes mypid = selector->p_pid; 1223df8bae1dSRodney W. Grimes if (sip->si_pid == mypid) 1224df8bae1dSRodney W. Grimes return; 1225e04ac2feSJohn Baldwin if (sip->si_pid && (p = pfind(sip->si_pid))) { 12269ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1227e04ac2feSJohn Baldwin if (p->p_wchan == (caddr_t)&selwait) { 12289ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 122933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1230df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 1231e04ac2feSJohn Baldwin return; 1232e04ac2feSJohn Baldwin } 12339ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 123433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1235e04ac2feSJohn Baldwin } 1236df8bae1dSRodney W. Grimes sip->si_pid = mypid; 1237df8bae1dSRodney W. Grimes } 1238df8bae1dSRodney W. Grimes 1239df8bae1dSRodney W. Grimes /* 1240df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1241df8bae1dSRodney W. Grimes */ 1242df8bae1dSRodney W. Grimes void 1243df8bae1dSRodney W. Grimes selwakeup(sip) 1244df8bae1dSRodney W. Grimes register struct selinfo *sip; 1245df8bae1dSRodney W. Grimes { 1246df8bae1dSRodney W. Grimes register struct proc *p; 1247df8bae1dSRodney W. Grimes 1248df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 1249df8bae1dSRodney W. Grimes return; 1250df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 1251df8bae1dSRodney W. Grimes nselcoll++; 1252df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1253265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1254df8bae1dSRodney W. Grimes } 1255df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 1256df8bae1dSRodney W. Grimes sip->si_pid = 0; 1257df8bae1dSRodney W. Grimes if (p != NULL) { 12589ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1259df8bae1dSRodney W. Grimes if (p->p_wchan == (caddr_t)&selwait) { 1260df8bae1dSRodney W. Grimes if (p->p_stat == SSLEEP) 1261df8bae1dSRodney W. Grimes setrunnable(p); 1262df8bae1dSRodney W. Grimes else 1263265fc98fSSeigo Tanimura cv_waitq_remove(p); 126433a9ed9dSJohn Baldwin } else 1265e04ac2feSJohn Baldwin p->p_flag &= ~P_SELECT; 126633a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 1267e04ac2feSJohn Baldwin PROC_UNLOCK(p); 1268e04ac2feSJohn Baldwin } 1269df8bae1dSRodney W. Grimes } 1270265fc98fSSeigo Tanimura 1271265fc98fSSeigo Tanimura static void selectinit __P((void *)); 1272265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1273265fc98fSSeigo Tanimura 1274265fc98fSSeigo Tanimura /* ARGSUSED*/ 1275265fc98fSSeigo Tanimura static void 1276265fc98fSSeigo Tanimura selectinit(dummy) 1277265fc98fSSeigo Tanimura void *dummy; 1278265fc98fSSeigo Tanimura { 1279265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 1280265fc98fSSeigo Tanimura } 1281