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 394160ccd9SAlan Cox * $Id: sys_generic.c,v 1.45 1999/01/29 08:10:35 bde Exp $ 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> 4920982410SBruce Evans #include <sys/ttycom.h> 503ac4d1efSBruce Evans #include <sys/fcntl.h> 51df8bae1dSRodney W. Grimes #include <sys/file.h> 52df8bae1dSRodney W. Grimes #include <sys/proc.h> 53797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 54df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 55df8bae1dSRodney W. Grimes #include <sys/uio.h> 56df8bae1dSRodney W. Grimes #include <sys/kernel.h> 57df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5842d11757SPeter Wemm #include <sys/poll.h> 5942d11757SPeter Wemm #include <sys/sysent.h> 60df8bae1dSRodney W. Grimes #ifdef KTRACE 61df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 62df8bae1dSRodney W. Grimes #endif 63df8bae1dSRodney W. Grimes 64069e9bc1SDoug Rabson #include <machine/limits.h> 65069e9bc1SDoug Rabson 66a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 67a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 68a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 6955166637SPoul-Henning Kamp 70cb226aaaSPoul-Henning Kamp static int pollscan __P((struct proc *, struct pollfd *, int)); 712087c896SBruce Evans static int selscan __P((struct proc *, fd_mask **, fd_mask **, int)); 72d93f860cSPoul-Henning Kamp 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * Read system call. 75df8bae1dSRodney W. Grimes */ 76d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 77df8bae1dSRodney W. Grimes struct read_args { 78df8bae1dSRodney W. Grimes int fd; 79134e06feSBruce Evans void *buf; 80134e06feSBruce Evans size_t nbyte; 81df8bae1dSRodney W. Grimes }; 82d2d3e875SBruce Evans #endif 83df8bae1dSRodney W. Grimes /* ARGSUSED */ 8426f9a767SRodney W. Grimes int 85cb226aaaSPoul-Henning Kamp read(p, uap) 86df8bae1dSRodney W. Grimes struct proc *p; 87df8bae1dSRodney W. Grimes register struct read_args *uap; 88df8bae1dSRodney W. Grimes { 89df8bae1dSRodney W. Grimes register struct file *fp; 90df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 91df8bae1dSRodney W. Grimes struct uio auio; 92df8bae1dSRodney W. Grimes struct iovec aiov; 93df8bae1dSRodney W. Grimes long cnt, error = 0; 94df8bae1dSRodney W. Grimes #ifdef KTRACE 95df8bae1dSRodney W. Grimes struct iovec ktriov; 96df8bae1dSRodney W. Grimes #endif 97df8bae1dSRodney W. Grimes 98df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 99df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 100df8bae1dSRodney W. Grimes (fp->f_flag & FREAD) == 0) 101df8bae1dSRodney W. Grimes return (EBADF); 102df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t)uap->buf; 103df8bae1dSRodney W. Grimes aiov.iov_len = uap->nbyte; 104df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 105df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 1062c1011f7SJohn Dyson auio.uio_offset = -1; 107069e9bc1SDoug Rabson if (uap->nbyte > INT_MAX) 108dd968eaeSDavid Greenman return (EINVAL); 109069e9bc1SDoug Rabson auio.uio_resid = uap->nbyte; 110df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 111df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 112df8bae1dSRodney W. Grimes auio.uio_procp = p; 113df8bae1dSRodney W. Grimes #ifdef KTRACE 114df8bae1dSRodney W. Grimes /* 115df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 116df8bae1dSRodney W. Grimes */ 117df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) 118df8bae1dSRodney W. Grimes ktriov = aiov; 119df8bae1dSRodney W. Grimes #endif 120df8bae1dSRodney W. Grimes cnt = uap->nbyte; 121bb56ec4aSPoul-Henning Kamp if ((error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))) 122df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 123df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 124df8bae1dSRodney W. Grimes error = 0; 125df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 126df8bae1dSRodney W. Grimes #ifdef KTRACE 127df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO) && error == 0) 128df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktriov, cnt, error); 129df8bae1dSRodney W. Grimes #endif 130cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 131df8bae1dSRodney W. Grimes return (error); 132df8bae1dSRodney W. Grimes } 133df8bae1dSRodney W. Grimes 134df8bae1dSRodney W. Grimes /* 1354160ccd9SAlan Cox * pread system call. 1364160ccd9SAlan Cox */ 1374160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1384160ccd9SAlan Cox struct pread_args { 1394160ccd9SAlan Cox int fd; 1404160ccd9SAlan Cox void *buf; 1414160ccd9SAlan Cox size_t nbyte; 1424160ccd9SAlan Cox off_t offset; 1434160ccd9SAlan Cox }; 1444160ccd9SAlan Cox #endif 1454160ccd9SAlan Cox /* ARGSUSED */ 1464160ccd9SAlan Cox int 1474160ccd9SAlan Cox pread(p, uap) 1484160ccd9SAlan Cox struct proc *p; 1494160ccd9SAlan Cox register struct pread_args *uap; 1504160ccd9SAlan Cox { 1514160ccd9SAlan Cox register struct file *fp; 1524160ccd9SAlan Cox register struct filedesc *fdp = p->p_fd; 1534160ccd9SAlan Cox struct uio auio; 1544160ccd9SAlan Cox struct iovec aiov; 1554160ccd9SAlan Cox long cnt, error = 0; 1564160ccd9SAlan Cox #ifdef KTRACE 1574160ccd9SAlan Cox struct iovec ktriov; 1584160ccd9SAlan Cox #endif 1594160ccd9SAlan Cox 1604160ccd9SAlan Cox if (((u_int)uap->fd) >= fdp->fd_nfiles || 1614160ccd9SAlan Cox (fp = fdp->fd_ofiles[uap->fd]) == NULL || 1624160ccd9SAlan Cox (fp->f_flag & FREAD) == 0) 1634160ccd9SAlan Cox return (EBADF); 1644160ccd9SAlan Cox if (fp->f_type != DTYPE_VNODE) 1654160ccd9SAlan Cox return (ESPIPE); 1664160ccd9SAlan Cox aiov.iov_base = (caddr_t)uap->buf; 1674160ccd9SAlan Cox aiov.iov_len = uap->nbyte; 1684160ccd9SAlan Cox auio.uio_iov = &aiov; 1694160ccd9SAlan Cox auio.uio_iovcnt = 1; 1704160ccd9SAlan Cox auio.uio_offset = uap->offset; 1714160ccd9SAlan Cox if (uap->nbyte > INT_MAX) 1724160ccd9SAlan Cox return (EINVAL); 1734160ccd9SAlan Cox auio.uio_resid = uap->nbyte; 1744160ccd9SAlan Cox auio.uio_rw = UIO_READ; 1754160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 1764160ccd9SAlan Cox auio.uio_procp = p; 1774160ccd9SAlan Cox #ifdef KTRACE 1784160ccd9SAlan Cox /* 1794160ccd9SAlan Cox * if tracing, save a copy of iovec 1804160ccd9SAlan Cox */ 1814160ccd9SAlan Cox if (KTRPOINT(p, KTR_GENIO)) 1824160ccd9SAlan Cox ktriov = aiov; 1834160ccd9SAlan Cox #endif 1844160ccd9SAlan Cox cnt = uap->nbyte; 1854160ccd9SAlan Cox if ((error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))) 1864160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 1874160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 1884160ccd9SAlan Cox error = 0; 1894160ccd9SAlan Cox cnt -= auio.uio_resid; 1904160ccd9SAlan Cox #ifdef KTRACE 1914160ccd9SAlan Cox if (KTRPOINT(p, KTR_GENIO) && error == 0) 1924160ccd9SAlan Cox ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktriov, cnt, error); 1934160ccd9SAlan Cox #endif 1944160ccd9SAlan Cox p->p_retval[0] = cnt; 1954160ccd9SAlan Cox return (error); 1964160ccd9SAlan Cox } 1974160ccd9SAlan Cox 1984160ccd9SAlan Cox /* 199df8bae1dSRodney W. Grimes * Scatter read system call. 200df8bae1dSRodney W. Grimes */ 201d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 202df8bae1dSRodney W. Grimes struct readv_args { 2037147b19dSBruce Evans int fd; 204df8bae1dSRodney W. Grimes struct iovec *iovp; 205df8bae1dSRodney W. Grimes u_int iovcnt; 206df8bae1dSRodney W. Grimes }; 207d2d3e875SBruce Evans #endif 20826f9a767SRodney W. Grimes int 209cb226aaaSPoul-Henning Kamp readv(p, uap) 210df8bae1dSRodney W. Grimes struct proc *p; 211df8bae1dSRodney W. Grimes register struct readv_args *uap; 212df8bae1dSRodney W. Grimes { 213df8bae1dSRodney W. Grimes register struct file *fp; 214df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 215df8bae1dSRodney W. Grimes struct uio auio; 216df8bae1dSRodney W. Grimes register struct iovec *iov; 217df8bae1dSRodney W. Grimes struct iovec *needfree; 218df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 219df8bae1dSRodney W. Grimes long i, cnt, error = 0; 220df8bae1dSRodney W. Grimes u_int iovlen; 221df8bae1dSRodney W. Grimes #ifdef KTRACE 222df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 223df8bae1dSRodney W. Grimes #endif 224df8bae1dSRodney W. Grimes 2257147b19dSBruce Evans if (((u_int)uap->fd) >= fdp->fd_nfiles || 2267147b19dSBruce Evans (fp = fdp->fd_ofiles[uap->fd]) == NULL || 227df8bae1dSRodney W. Grimes (fp->f_flag & FREAD) == 0) 228df8bae1dSRodney W. Grimes return (EBADF); 229df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 230df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 231df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 232df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_MAXIOV) 233df8bae1dSRodney W. Grimes return (EINVAL); 234df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 235df8bae1dSRodney W. Grimes needfree = iov; 236df8bae1dSRodney W. Grimes } else { 237df8bae1dSRodney W. Grimes iov = aiov; 238df8bae1dSRodney W. Grimes needfree = NULL; 239df8bae1dSRodney W. Grimes } 240df8bae1dSRodney W. Grimes auio.uio_iov = iov; 241df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 242df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 243df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 244df8bae1dSRodney W. Grimes auio.uio_procp = p; 2452c1011f7SJohn Dyson auio.uio_offset = -1; 246bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 247df8bae1dSRodney W. Grimes goto done; 248df8bae1dSRodney W. Grimes auio.uio_resid = 0; 249df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 250069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 251df8bae1dSRodney W. Grimes error = EINVAL; 252df8bae1dSRodney W. Grimes goto done; 253df8bae1dSRodney W. Grimes } 254069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 255df8bae1dSRodney W. Grimes iov++; 256df8bae1dSRodney W. Grimes } 257df8bae1dSRodney W. Grimes #ifdef KTRACE 258df8bae1dSRodney W. Grimes /* 259df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 260df8bae1dSRodney W. Grimes */ 261df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) { 262df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 263df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 264df8bae1dSRodney W. Grimes } 265df8bae1dSRodney W. Grimes #endif 266df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 267bb56ec4aSPoul-Henning Kamp if ((error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))) 268df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 269df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 270df8bae1dSRodney W. Grimes error = 0; 271df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 272df8bae1dSRodney W. Grimes #ifdef KTRACE 273df8bae1dSRodney W. Grimes if (ktriov != NULL) { 274df8bae1dSRodney W. Grimes if (error == 0) 2757147b19dSBruce Evans ktrgenio(p->p_tracep, uap->fd, UIO_READ, ktriov, 276df8bae1dSRodney W. Grimes cnt, error); 277df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 278df8bae1dSRodney W. Grimes } 279df8bae1dSRodney W. Grimes #endif 280cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 281df8bae1dSRodney W. Grimes done: 282df8bae1dSRodney W. Grimes if (needfree) 283df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 284df8bae1dSRodney W. Grimes return (error); 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes 287df8bae1dSRodney W. Grimes /* 288df8bae1dSRodney W. Grimes * Write system call 289df8bae1dSRodney W. Grimes */ 290d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 291df8bae1dSRodney W. Grimes struct write_args { 292df8bae1dSRodney W. Grimes int fd; 293134e06feSBruce Evans const void *buf; 294134e06feSBruce Evans size_t nbyte; 295df8bae1dSRodney W. Grimes }; 296d2d3e875SBruce Evans #endif 29726f9a767SRodney W. Grimes int 298cb226aaaSPoul-Henning Kamp write(p, uap) 299df8bae1dSRodney W. Grimes struct proc *p; 300df8bae1dSRodney W. Grimes register struct write_args *uap; 301df8bae1dSRodney W. Grimes { 302df8bae1dSRodney W. Grimes register struct file *fp; 303df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 304df8bae1dSRodney W. Grimes struct uio auio; 305df8bae1dSRodney W. Grimes struct iovec aiov; 306df8bae1dSRodney W. Grimes long cnt, error = 0; 307df8bae1dSRodney W. Grimes #ifdef KTRACE 308df8bae1dSRodney W. Grimes struct iovec ktriov; 309df8bae1dSRodney W. Grimes #endif 310df8bae1dSRodney W. Grimes 311df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 312df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 313df8bae1dSRodney W. Grimes (fp->f_flag & FWRITE) == 0) 314df8bae1dSRodney W. Grimes return (EBADF); 315425c50cfSBruce Evans aiov.iov_base = uap->buf; 316df8bae1dSRodney W. Grimes aiov.iov_len = uap->nbyte; 317df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 318df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 3192c1011f7SJohn Dyson auio.uio_offset = -1; 320069e9bc1SDoug Rabson if (uap->nbyte > INT_MAX) 321069e9bc1SDoug Rabson return (EINVAL); 322df8bae1dSRodney W. Grimes auio.uio_resid = uap->nbyte; 323df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 324df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 325df8bae1dSRodney W. Grimes auio.uio_procp = p; 326df8bae1dSRodney W. Grimes #ifdef KTRACE 327df8bae1dSRodney W. Grimes /* 328df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 329df8bae1dSRodney W. Grimes */ 330df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) 331df8bae1dSRodney W. Grimes ktriov = aiov; 332df8bae1dSRodney W. Grimes #endif 333df8bae1dSRodney W. Grimes cnt = uap->nbyte; 334bb56ec4aSPoul-Henning Kamp if ((error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred))) { 335df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 336df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 337df8bae1dSRodney W. Grimes error = 0; 338df8bae1dSRodney W. Grimes if (error == EPIPE) 339df8bae1dSRodney W. Grimes psignal(p, SIGPIPE); 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 342df8bae1dSRodney W. Grimes #ifdef KTRACE 343df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO) && error == 0) 344df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, 345df8bae1dSRodney W. Grimes &ktriov, cnt, error); 346df8bae1dSRodney W. Grimes #endif 347cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 348df8bae1dSRodney W. Grimes return (error); 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes /* 3524160ccd9SAlan Cox * pwrite system call 3534160ccd9SAlan Cox */ 3544160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3554160ccd9SAlan Cox struct pwrite_args { 3564160ccd9SAlan Cox int fd; 3574160ccd9SAlan Cox const void *buf; 3584160ccd9SAlan Cox size_t nbyte; 3594160ccd9SAlan Cox off_t offset; 3604160ccd9SAlan Cox }; 3614160ccd9SAlan Cox #endif 3624160ccd9SAlan Cox int 3634160ccd9SAlan Cox pwrite(p, uap) 3644160ccd9SAlan Cox struct proc *p; 3654160ccd9SAlan Cox register struct pwrite_args *uap; 3664160ccd9SAlan Cox { 3674160ccd9SAlan Cox register struct file *fp; 3684160ccd9SAlan Cox register struct filedesc *fdp = p->p_fd; 3694160ccd9SAlan Cox struct uio auio; 3704160ccd9SAlan Cox struct iovec aiov; 3714160ccd9SAlan Cox long cnt, error = 0; 3724160ccd9SAlan Cox #ifdef KTRACE 3734160ccd9SAlan Cox struct iovec ktriov; 3744160ccd9SAlan Cox #endif 3754160ccd9SAlan Cox 3764160ccd9SAlan Cox if (((u_int)uap->fd) >= fdp->fd_nfiles || 3774160ccd9SAlan Cox (fp = fdp->fd_ofiles[uap->fd]) == NULL || 3784160ccd9SAlan Cox (fp->f_flag & FWRITE) == 0) 3794160ccd9SAlan Cox return (EBADF); 3804160ccd9SAlan Cox if (fp->f_type != DTYPE_VNODE) 3814160ccd9SAlan Cox return (ESPIPE); 3824160ccd9SAlan Cox aiov.iov_base = (caddr_t)uap->buf; 3834160ccd9SAlan Cox aiov.iov_len = uap->nbyte; 3844160ccd9SAlan Cox auio.uio_iov = &aiov; 3854160ccd9SAlan Cox auio.uio_iovcnt = 1; 3864160ccd9SAlan Cox auio.uio_offset = uap->offset; 3874160ccd9SAlan Cox if (uap->nbyte > INT_MAX) 3884160ccd9SAlan Cox return (EINVAL); 3894160ccd9SAlan Cox auio.uio_resid = uap->nbyte; 3904160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 3914160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 3924160ccd9SAlan Cox auio.uio_procp = p; 3934160ccd9SAlan Cox #ifdef KTRACE 3944160ccd9SAlan Cox /* 3954160ccd9SAlan Cox * if tracing, save a copy of iovec 3964160ccd9SAlan Cox */ 3974160ccd9SAlan Cox if (KTRPOINT(p, KTR_GENIO)) 3984160ccd9SAlan Cox ktriov = aiov; 3994160ccd9SAlan Cox #endif 4004160ccd9SAlan Cox cnt = uap->nbyte; 4014160ccd9SAlan Cox if ((error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred))) { 4024160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4034160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4044160ccd9SAlan Cox error = 0; 4054160ccd9SAlan Cox if (error == EPIPE) 4064160ccd9SAlan Cox psignal(p, SIGPIPE); 4074160ccd9SAlan Cox } 4084160ccd9SAlan Cox cnt -= auio.uio_resid; 4094160ccd9SAlan Cox #ifdef KTRACE 4104160ccd9SAlan Cox if (KTRPOINT(p, KTR_GENIO) && error == 0) 4114160ccd9SAlan Cox ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, 4124160ccd9SAlan Cox &ktriov, cnt, error); 4134160ccd9SAlan Cox #endif 4144160ccd9SAlan Cox p->p_retval[0] = cnt; 4154160ccd9SAlan Cox return (error); 4164160ccd9SAlan Cox } 4174160ccd9SAlan Cox 4184160ccd9SAlan Cox /* 419df8bae1dSRodney W. Grimes * Gather write system call 420df8bae1dSRodney W. Grimes */ 421d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 422df8bae1dSRodney W. Grimes struct writev_args { 423df8bae1dSRodney W. Grimes int fd; 424df8bae1dSRodney W. Grimes struct iovec *iovp; 425df8bae1dSRodney W. Grimes u_int iovcnt; 426df8bae1dSRodney W. Grimes }; 427d2d3e875SBruce Evans #endif 42826f9a767SRodney W. Grimes int 429cb226aaaSPoul-Henning Kamp writev(p, uap) 430df8bae1dSRodney W. Grimes struct proc *p; 431df8bae1dSRodney W. Grimes register struct writev_args *uap; 432df8bae1dSRodney W. Grimes { 433df8bae1dSRodney W. Grimes register struct file *fp; 434df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 435df8bae1dSRodney W. Grimes struct uio auio; 436df8bae1dSRodney W. Grimes register struct iovec *iov; 437df8bae1dSRodney W. Grimes struct iovec *needfree; 438df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 439df8bae1dSRodney W. Grimes long i, cnt, error = 0; 440df8bae1dSRodney W. Grimes u_int iovlen; 441df8bae1dSRodney W. Grimes #ifdef KTRACE 442df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 443df8bae1dSRodney W. Grimes #endif 444df8bae1dSRodney W. Grimes 445df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 446df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 447df8bae1dSRodney W. Grimes (fp->f_flag & FWRITE) == 0) 448df8bae1dSRodney W. Grimes return (EBADF); 449df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 450df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 451df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 452df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_MAXIOV) 453df8bae1dSRodney W. Grimes return (EINVAL); 454df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 455df8bae1dSRodney W. Grimes needfree = iov; 456df8bae1dSRodney W. Grimes } else { 457df8bae1dSRodney W. Grimes iov = aiov; 458df8bae1dSRodney W. Grimes needfree = NULL; 459df8bae1dSRodney W. Grimes } 460df8bae1dSRodney W. Grimes auio.uio_iov = iov; 461df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 462df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 463df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 464df8bae1dSRodney W. Grimes auio.uio_procp = p; 4652c1011f7SJohn Dyson auio.uio_offset = -1; 466bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 467df8bae1dSRodney W. Grimes goto done; 468df8bae1dSRodney W. Grimes auio.uio_resid = 0; 469df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 470069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 471df8bae1dSRodney W. Grimes error = EINVAL; 472df8bae1dSRodney W. Grimes goto done; 473df8bae1dSRodney W. Grimes } 474069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 475df8bae1dSRodney W. Grimes iov++; 476df8bae1dSRodney W. Grimes } 477df8bae1dSRodney W. Grimes #ifdef KTRACE 478df8bae1dSRodney W. Grimes /* 479df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 480df8bae1dSRodney W. Grimes */ 481df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) { 482df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 483df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes #endif 486df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 487bb56ec4aSPoul-Henning Kamp if ((error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred))) { 488df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 489df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 490df8bae1dSRodney W. Grimes error = 0; 491df8bae1dSRodney W. Grimes if (error == EPIPE) 492df8bae1dSRodney W. Grimes psignal(p, SIGPIPE); 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 495df8bae1dSRodney W. Grimes #ifdef KTRACE 496df8bae1dSRodney W. Grimes if (ktriov != NULL) { 497df8bae1dSRodney W. Grimes if (error == 0) 498df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, 499df8bae1dSRodney W. Grimes ktriov, cnt, error); 500df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 501df8bae1dSRodney W. Grimes } 502df8bae1dSRodney W. Grimes #endif 503cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 504df8bae1dSRodney W. Grimes done: 505df8bae1dSRodney W. Grimes if (needfree) 506df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 507df8bae1dSRodney W. Grimes return (error); 508df8bae1dSRodney W. Grimes } 509df8bae1dSRodney W. Grimes 510df8bae1dSRodney W. Grimes /* 511df8bae1dSRodney W. Grimes * Ioctl system call 512df8bae1dSRodney W. Grimes */ 513d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 514df8bae1dSRodney W. Grimes struct ioctl_args { 515df8bae1dSRodney W. Grimes int fd; 516069e9bc1SDoug Rabson u_long com; 517df8bae1dSRodney W. Grimes caddr_t data; 518df8bae1dSRodney W. Grimes }; 519d2d3e875SBruce Evans #endif 520df8bae1dSRodney W. Grimes /* ARGSUSED */ 52126f9a767SRodney W. Grimes int 522cb226aaaSPoul-Henning Kamp ioctl(p, uap) 523df8bae1dSRodney W. Grimes struct proc *p; 524df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 525df8bae1dSRodney W. Grimes { 526df8bae1dSRodney W. Grimes register struct file *fp; 527df8bae1dSRodney W. Grimes register struct filedesc *fdp; 528831b9ef2SDoug Rabson register u_long com; 529831b9ef2SDoug Rabson int error; 530df8bae1dSRodney W. Grimes register u_int size; 531df8bae1dSRodney W. Grimes caddr_t data, memp; 532df8bae1dSRodney W. Grimes int tmp; 533df8bae1dSRodney W. Grimes #define STK_PARAMS 128 534df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 535df8bae1dSRodney W. Grimes 536df8bae1dSRodney W. Grimes fdp = p->p_fd; 537df8bae1dSRodney W. Grimes if ((u_int)uap->fd >= fdp->fd_nfiles || 538df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 539df8bae1dSRodney W. Grimes return (EBADF); 540df8bae1dSRodney W. Grimes 541df8bae1dSRodney W. Grimes if ((fp->f_flag & (FREAD | FWRITE)) == 0) 542df8bae1dSRodney W. Grimes return (EBADF); 543df8bae1dSRodney W. Grimes 544df8bae1dSRodney W. Grimes switch (com = uap->com) { 545df8bae1dSRodney W. Grimes case FIONCLEX: 546df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 547df8bae1dSRodney W. Grimes return (0); 548df8bae1dSRodney W. Grimes case FIOCLEX: 549df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 550df8bae1dSRodney W. Grimes return (0); 551df8bae1dSRodney W. Grimes } 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes /* 554df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 555df8bae1dSRodney W. Grimes * copied to/from the user's address space. 556df8bae1dSRodney W. Grimes */ 557df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 558df8bae1dSRodney W. Grimes if (size > IOCPARM_MAX) 559df8bae1dSRodney W. Grimes return (ENOTTY); 560df8bae1dSRodney W. Grimes memp = NULL; 561df8bae1dSRodney W. Grimes if (size > sizeof (stkbuf)) { 562df8bae1dSRodney W. Grimes memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 563df8bae1dSRodney W. Grimes data = memp; 564df8bae1dSRodney W. Grimes } else 565df8bae1dSRodney W. Grimes data = stkbuf; 566df8bae1dSRodney W. Grimes if (com&IOC_IN) { 567df8bae1dSRodney W. Grimes if (size) { 568df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 569df8bae1dSRodney W. Grimes if (error) { 570df8bae1dSRodney W. Grimes if (memp) 571df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 572df8bae1dSRodney W. Grimes return (error); 573df8bae1dSRodney W. Grimes } 574df8bae1dSRodney W. Grimes } else 575df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 576df8bae1dSRodney W. Grimes } else if ((com&IOC_OUT) && size) 577df8bae1dSRodney W. Grimes /* 578df8bae1dSRodney W. Grimes * Zero the buffer so the user always 579df8bae1dSRodney W. Grimes * gets back something deterministic. 580df8bae1dSRodney W. Grimes */ 581df8bae1dSRodney W. Grimes bzero(data, size); 582df8bae1dSRodney W. Grimes else if (com&IOC_VOID) 583df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 584df8bae1dSRodney W. Grimes 585df8bae1dSRodney W. Grimes switch (com) { 586df8bae1dSRodney W. Grimes 587df8bae1dSRodney W. Grimes case FIONBIO: 588bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 589df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 590df8bae1dSRodney W. Grimes else 591df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 592df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p); 593df8bae1dSRodney W. Grimes break; 594df8bae1dSRodney W. Grimes 595df8bae1dSRodney W. Grimes case FIOASYNC: 596bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 597df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 598df8bae1dSRodney W. Grimes else 599df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 600df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p); 601df8bae1dSRodney W. Grimes break; 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes default: 604df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, com, data, p); 605df8bae1dSRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * Copy any data to user, size was 607df8bae1dSRodney W. Grimes * already set and checked above. 608df8bae1dSRodney W. Grimes */ 609df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 610df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 611df8bae1dSRodney W. Grimes break; 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes if (memp) 614df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 615df8bae1dSRodney W. Grimes return (error); 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes 61887b6de2bSPoul-Henning Kamp static int nselcoll; 61987b6de2bSPoul-Henning Kamp int selwait; 620df8bae1dSRodney W. Grimes 621df8bae1dSRodney W. Grimes /* 622df8bae1dSRodney W. Grimes * Select system call. 623df8bae1dSRodney W. Grimes */ 624d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 625df8bae1dSRodney W. Grimes struct select_args { 626b08f7993SSujal Patel int nd; 627df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 628df8bae1dSRodney W. Grimes struct timeval *tv; 629df8bae1dSRodney W. Grimes }; 630d2d3e875SBruce Evans #endif 63126f9a767SRodney W. Grimes int 632cb226aaaSPoul-Henning Kamp select(p, uap) 633df8bae1dSRodney W. Grimes register struct proc *p; 634df8bae1dSRodney W. Grimes register struct select_args *uap; 635df8bae1dSRodney W. Grimes { 636d5e4d7e1SBruce Evans /* 637d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 638d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 639d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 640d5e4d7e1SBruce Evans * of 256. 641d5e4d7e1SBruce Evans */ 642d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 643d5e4d7e1SBruce Evans fd_mask *ibits[3], *obits[3], *selbits, *sbp; 64400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 64500af9731SPoul-Henning Kamp int s, ncoll, error, timo; 646d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 647df8bae1dSRodney W. Grimes 648b08f7993SSujal Patel if (uap->nd < 0) 649acbfbfeaSSujal Patel return (EINVAL); 650df8bae1dSRodney W. Grimes if (uap->nd > p->p_fd->fd_nfiles) 651df8bae1dSRodney W. Grimes uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 652b08f7993SSujal Patel 653d5e4d7e1SBruce Evans /* 654d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 655d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 656d5e4d7e1SBruce Evans */ 657d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 658d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 659d5e4d7e1SBruce Evans nbufbytes = 0; 660d5e4d7e1SBruce Evans if (uap->in != NULL) 661d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 662d5e4d7e1SBruce Evans if (uap->ou != NULL) 663d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 664d5e4d7e1SBruce Evans if (uap->ex != NULL) 665d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 666d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 667d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 668d5e4d7e1SBruce Evans else 669d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 670b08f7993SSujal Patel 671b08f7993SSujal Patel /* 672d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 673d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 674d5e4d7e1SBruce Evans * together. 675b08f7993SSujal Patel */ 676d5e4d7e1SBruce Evans sbp = selbits; 677df8bae1dSRodney W. Grimes #define getbits(name, x) \ 678d5e4d7e1SBruce Evans do { \ 679d5e4d7e1SBruce Evans if (uap->name == NULL) \ 680d5e4d7e1SBruce Evans ibits[x] = NULL; \ 681d5e4d7e1SBruce Evans else { \ 682d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 683d5e4d7e1SBruce Evans obits[x] = sbp; \ 684d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 685d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 686d5e4d7e1SBruce Evans if (error != 0) \ 687d5e4d7e1SBruce Evans goto done; \ 688d5e4d7e1SBruce Evans } \ 689d5e4d7e1SBruce Evans } while (0) 690df8bae1dSRodney W. Grimes getbits(in, 0); 691df8bae1dSRodney W. Grimes getbits(ou, 1); 692df8bae1dSRodney W. Grimes getbits(ex, 2); 693df8bae1dSRodney W. Grimes #undef getbits 694d5e4d7e1SBruce Evans if (nbufbytes != 0) 695d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 696df8bae1dSRodney W. Grimes 697df8bae1dSRodney W. Grimes if (uap->tv) { 698df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 699df8bae1dSRodney W. Grimes sizeof (atv)); 700df8bae1dSRodney W. Grimes if (error) 701df8bae1dSRodney W. Grimes goto done; 702df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 703df8bae1dSRodney W. Grimes error = EINVAL; 704df8bae1dSRodney W. Grimes goto done; 705df8bae1dSRodney W. Grimes } 706c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 70700af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 70880a39463SAndrey A. Chernov } else 70900af9731SPoul-Henning Kamp atv.tv_sec = 0; 71000af9731SPoul-Henning Kamp timo = 0; 711df8bae1dSRodney W. Grimes retry: 712df8bae1dSRodney W. Grimes ncoll = nselcoll; 713df8bae1dSRodney W. Grimes p->p_flag |= P_SELECT; 714cb226aaaSPoul-Henning Kamp error = selscan(p, ibits, obits, uap->nd); 715cb226aaaSPoul-Henning Kamp if (error || p->p_retval[0]) 716df8bae1dSRodney W. Grimes goto done; 71700af9731SPoul-Henning Kamp if (atv.tv_sec) { 718c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 71900af9731SPoul-Henning Kamp if (timevalcmp(&rtv, &atv, >=)) 720df8bae1dSRodney W. Grimes goto done; 72100af9731SPoul-Henning Kamp ttv = atv; 72200af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 72300af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 72400af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 725df8bae1dSRodney W. Grimes } 72600af9731SPoul-Henning Kamp s = splhigh(); 727df8bae1dSRodney W. Grimes if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 728df8bae1dSRodney W. Grimes splx(s); 729df8bae1dSRodney W. Grimes goto retry; 730df8bae1dSRodney W. Grimes } 731df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 732df8bae1dSRodney W. Grimes error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo); 733df8bae1dSRodney W. Grimes splx(s); 734df8bae1dSRodney W. Grimes if (error == 0) 735df8bae1dSRodney W. Grimes goto retry; 736df8bae1dSRodney W. Grimes done: 737df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 738df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 739df8bae1dSRodney W. Grimes if (error == ERESTART) 740df8bae1dSRodney W. Grimes error = EINTR; 741df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 742df8bae1dSRodney W. Grimes error = 0; 743df8bae1dSRodney W. Grimes #define putbits(name, x) \ 744d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 745df8bae1dSRodney W. Grimes error = error2; 746df8bae1dSRodney W. Grimes if (error == 0) { 747df8bae1dSRodney W. Grimes int error2; 748df8bae1dSRodney W. Grimes 749df8bae1dSRodney W. Grimes putbits(in, 0); 750df8bae1dSRodney W. Grimes putbits(ou, 1); 751df8bae1dSRodney W. Grimes putbits(ex, 2); 752df8bae1dSRodney W. Grimes #undef putbits 753df8bae1dSRodney W. Grimes } 754d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 755d5e4d7e1SBruce Evans free(selbits, M_SELECT); 756df8bae1dSRodney W. Grimes return (error); 757df8bae1dSRodney W. Grimes } 758df8bae1dSRodney W. Grimes 75987b6de2bSPoul-Henning Kamp static int 760cb226aaaSPoul-Henning Kamp selscan(p, ibits, obits, nfd) 761df8bae1dSRodney W. Grimes struct proc *p; 762b08f7993SSujal Patel fd_mask **ibits, **obits; 763cb226aaaSPoul-Henning Kamp int nfd; 764df8bae1dSRodney W. Grimes { 765df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 766df8bae1dSRodney W. Grimes register int msk, i, j, fd; 767df8bae1dSRodney W. Grimes register fd_mask bits; 768df8bae1dSRodney W. Grimes struct file *fp; 769df8bae1dSRodney W. Grimes int n = 0; 7702087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 77142d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 772df8bae1dSRodney W. Grimes 773df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 774d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 775d5e4d7e1SBruce Evans continue; 776df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 777b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 778df8bae1dSRodney W. Grimes while ((j = ffs(bits)) && (fd = i + --j) < nfd) { 779df8bae1dSRodney W. Grimes bits &= ~(1 << j); 780df8bae1dSRodney W. Grimes fp = fdp->fd_ofiles[fd]; 781df8bae1dSRodney W. Grimes if (fp == NULL) 782df8bae1dSRodney W. Grimes return (EBADF); 78342d11757SPeter Wemm if ((*fp->f_ops->fo_poll)(fp, flag[msk], 78442d11757SPeter Wemm fp->f_cred, p)) { 785b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 786b08f7993SSujal Patel (1 << ((fd) % NFDBITS)); 787df8bae1dSRodney W. Grimes n++; 788df8bae1dSRodney W. Grimes } 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes } 792cb226aaaSPoul-Henning Kamp p->p_retval[0] = n; 793df8bae1dSRodney W. Grimes return (0); 794df8bae1dSRodney W. Grimes } 795df8bae1dSRodney W. Grimes 79642d11757SPeter Wemm /* 79742d11757SPeter Wemm * Poll system call. 79842d11757SPeter Wemm */ 79942d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 80042d11757SPeter Wemm struct poll_args { 80142d11757SPeter Wemm struct pollfd *fds; 80242d11757SPeter Wemm u_int nfds; 80342d11757SPeter Wemm int timeout; 80442d11757SPeter Wemm }; 80542d11757SPeter Wemm #endif 80642d11757SPeter Wemm int 807cb226aaaSPoul-Henning Kamp poll(p, uap) 80842d11757SPeter Wemm register struct proc *p; 80942d11757SPeter Wemm register struct poll_args *uap; 81042d11757SPeter Wemm { 81142d11757SPeter Wemm caddr_t bits; 81242d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 81300af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 81400af9731SPoul-Henning Kamp int s, ncoll, error = 0, timo; 81542d11757SPeter Wemm size_t ni; 81642d11757SPeter Wemm 81742d11757SPeter Wemm if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) { 81842d11757SPeter Wemm /* forgiving; slightly wrong */ 81942d11757SPeter Wemm SCARG(uap, nfds) = p->p_fd->fd_nfiles; 82042d11757SPeter Wemm } 82142d11757SPeter Wemm ni = SCARG(uap, nfds) * sizeof(struct pollfd); 82242d11757SPeter Wemm if (ni > sizeof(smallbits)) 82342d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 82442d11757SPeter Wemm else 82542d11757SPeter Wemm bits = smallbits; 82642d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 82742d11757SPeter Wemm if (error) 82842d11757SPeter Wemm goto done; 82942d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 83042d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 83142d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 83242d11757SPeter Wemm if (itimerfix(&atv)) { 83342d11757SPeter Wemm error = EINVAL; 83442d11757SPeter Wemm goto done; 83542d11757SPeter Wemm } 836c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 83700af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 83880a39463SAndrey A. Chernov } else 83900af9731SPoul-Henning Kamp atv.tv_sec = 0; 84000af9731SPoul-Henning Kamp timo = 0; 84142d11757SPeter Wemm retry: 84242d11757SPeter Wemm ncoll = nselcoll; 84342d11757SPeter Wemm p->p_flag |= P_SELECT; 844cb226aaaSPoul-Henning Kamp error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds)); 845cb226aaaSPoul-Henning Kamp if (error || p->p_retval[0]) 84642d11757SPeter Wemm goto done; 84700af9731SPoul-Henning Kamp if (atv.tv_sec) { 848c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 84900af9731SPoul-Henning Kamp if (timevalcmp(&rtv, &atv, >=)) 85042d11757SPeter Wemm goto done; 85100af9731SPoul-Henning Kamp ttv = atv; 85200af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 85300af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 85400af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 85542d11757SPeter Wemm } 85600af9731SPoul-Henning Kamp s = splhigh(); 85742d11757SPeter Wemm if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 85842d11757SPeter Wemm splx(s); 85942d11757SPeter Wemm goto retry; 86042d11757SPeter Wemm } 86142d11757SPeter Wemm p->p_flag &= ~P_SELECT; 86242d11757SPeter Wemm error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo); 86342d11757SPeter Wemm splx(s); 86442d11757SPeter Wemm if (error == 0) 86542d11757SPeter Wemm goto retry; 86642d11757SPeter Wemm done: 86742d11757SPeter Wemm p->p_flag &= ~P_SELECT; 86842d11757SPeter Wemm /* poll is not restarted after signals... */ 86942d11757SPeter Wemm if (error == ERESTART) 87042d11757SPeter Wemm error = EINTR; 87142d11757SPeter Wemm if (error == EWOULDBLOCK) 87242d11757SPeter Wemm error = 0; 87342d11757SPeter Wemm if (error == 0) { 87442d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 87542d11757SPeter Wemm if (error) 87642d11757SPeter Wemm goto out; 87742d11757SPeter Wemm } 87842d11757SPeter Wemm out: 87942d11757SPeter Wemm if (ni > sizeof(smallbits)) 88042d11757SPeter Wemm free(bits, M_TEMP); 88142d11757SPeter Wemm return (error); 88242d11757SPeter Wemm } 88342d11757SPeter Wemm 88442d11757SPeter Wemm static int 885cb226aaaSPoul-Henning Kamp pollscan(p, fds, nfd) 88642d11757SPeter Wemm struct proc *p; 88742d11757SPeter Wemm struct pollfd *fds; 88842d11757SPeter Wemm int nfd; 88942d11757SPeter Wemm { 89042d11757SPeter Wemm register struct filedesc *fdp = p->p_fd; 89142d11757SPeter Wemm int i; 89242d11757SPeter Wemm struct file *fp; 89342d11757SPeter Wemm int n = 0; 89442d11757SPeter Wemm 89542d11757SPeter Wemm for (i = 0; i < nfd; i++, fds++) { 896337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 89742d11757SPeter Wemm fds->revents = POLLNVAL; 89842d11757SPeter Wemm n++; 899337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 900337c9691SJordan K. Hubbard fds->revents = 0; 90142d11757SPeter Wemm } else { 90242d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 90342d11757SPeter Wemm if (fp == 0) { 90442d11757SPeter Wemm fds->revents = POLLNVAL; 90542d11757SPeter Wemm n++; 90642d11757SPeter Wemm } else { 9072087c896SBruce Evans /* 9082087c896SBruce Evans * Note: backend also returns POLLHUP and 9092087c896SBruce Evans * POLLERR if appropriate. 9102087c896SBruce Evans */ 91142d11757SPeter Wemm fds->revents = (*fp->f_ops->fo_poll)(fp, 91242d11757SPeter Wemm fds->events, fp->f_cred, p); 91342d11757SPeter Wemm if (fds->revents != 0) 91442d11757SPeter Wemm n++; 91542d11757SPeter Wemm } 91642d11757SPeter Wemm } 91742d11757SPeter Wemm } 918cb226aaaSPoul-Henning Kamp p->p_retval[0] = n; 91942d11757SPeter Wemm return (0); 92042d11757SPeter Wemm } 92142d11757SPeter Wemm 92242d11757SPeter Wemm /* 92342d11757SPeter Wemm * OpenBSD poll system call. 92442d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 92542d11757SPeter Wemm */ 92642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 92742d11757SPeter Wemm struct openbsd_poll_args { 92842d11757SPeter Wemm struct pollfd *fds; 92942d11757SPeter Wemm u_int nfds; 93042d11757SPeter Wemm int timeout; 93142d11757SPeter Wemm }; 93242d11757SPeter Wemm #endif 93342d11757SPeter Wemm int 934cb226aaaSPoul-Henning Kamp openbsd_poll(p, uap) 93542d11757SPeter Wemm register struct proc *p; 93642d11757SPeter Wemm register struct openbsd_poll_args *uap; 93742d11757SPeter Wemm { 938cb226aaaSPoul-Henning Kamp return (poll(p, (struct poll_args *)uap)); 93942d11757SPeter Wemm } 94042d11757SPeter Wemm 941df8bae1dSRodney W. Grimes /*ARGSUSED*/ 94226f9a767SRodney W. Grimes int 94342d11757SPeter Wemm seltrue(dev, events, p) 944df8bae1dSRodney W. Grimes dev_t dev; 94542d11757SPeter Wemm int events; 946df8bae1dSRodney W. Grimes struct proc *p; 947df8bae1dSRodney W. Grimes { 948df8bae1dSRodney W. Grimes 94942d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 950df8bae1dSRodney W. Grimes } 951df8bae1dSRodney W. Grimes 952df8bae1dSRodney W. Grimes /* 953df8bae1dSRodney W. Grimes * Record a select request. 954df8bae1dSRodney W. Grimes */ 955df8bae1dSRodney W. Grimes void 956df8bae1dSRodney W. Grimes selrecord(selector, sip) 957df8bae1dSRodney W. Grimes struct proc *selector; 958df8bae1dSRodney W. Grimes struct selinfo *sip; 959df8bae1dSRodney W. Grimes { 960df8bae1dSRodney W. Grimes struct proc *p; 961df8bae1dSRodney W. Grimes pid_t mypid; 962df8bae1dSRodney W. Grimes 963df8bae1dSRodney W. Grimes mypid = selector->p_pid; 964df8bae1dSRodney W. Grimes if (sip->si_pid == mypid) 965df8bae1dSRodney W. Grimes return; 966df8bae1dSRodney W. Grimes if (sip->si_pid && (p = pfind(sip->si_pid)) && 967df8bae1dSRodney W. Grimes p->p_wchan == (caddr_t)&selwait) 968df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 969df8bae1dSRodney W. Grimes else 970df8bae1dSRodney W. Grimes sip->si_pid = mypid; 971df8bae1dSRodney W. Grimes } 972df8bae1dSRodney W. Grimes 973df8bae1dSRodney W. Grimes /* 974df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 975df8bae1dSRodney W. Grimes */ 976df8bae1dSRodney W. Grimes void 977df8bae1dSRodney W. Grimes selwakeup(sip) 978df8bae1dSRodney W. Grimes register struct selinfo *sip; 979df8bae1dSRodney W. Grimes { 980df8bae1dSRodney W. Grimes register struct proc *p; 981df8bae1dSRodney W. Grimes int s; 982df8bae1dSRodney W. Grimes 983df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 984df8bae1dSRodney W. Grimes return; 985df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 986df8bae1dSRodney W. Grimes nselcoll++; 987df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 988df8bae1dSRodney W. Grimes wakeup((caddr_t)&selwait); 989df8bae1dSRodney W. Grimes } 990df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 991df8bae1dSRodney W. Grimes sip->si_pid = 0; 992df8bae1dSRodney W. Grimes if (p != NULL) { 993df8bae1dSRodney W. Grimes s = splhigh(); 994df8bae1dSRodney W. Grimes if (p->p_wchan == (caddr_t)&selwait) { 995df8bae1dSRodney W. Grimes if (p->p_stat == SSLEEP) 996df8bae1dSRodney W. Grimes setrunnable(p); 997df8bae1dSRodney W. Grimes else 998df8bae1dSRodney W. Grimes unsleep(p); 999df8bae1dSRodney W. Grimes } else if (p->p_flag & P_SELECT) 1000df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 1001df8bae1dSRodney W. Grimes splx(s); 1002df8bae1dSRodney W. Grimes } 1003df8bae1dSRodney W. Grimes } 1004