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 39337c9691SJordan K. Hubbard * $Id: sys_generic.c,v 1.42 1998/11/11 10:03:55 truckman 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 /* 135df8bae1dSRodney W. Grimes * Scatter read system call. 136df8bae1dSRodney W. Grimes */ 137d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 138df8bae1dSRodney W. Grimes struct readv_args { 1397147b19dSBruce Evans int fd; 140df8bae1dSRodney W. Grimes struct iovec *iovp; 141df8bae1dSRodney W. Grimes u_int iovcnt; 142df8bae1dSRodney W. Grimes }; 143d2d3e875SBruce Evans #endif 14426f9a767SRodney W. Grimes int 145cb226aaaSPoul-Henning Kamp readv(p, uap) 146df8bae1dSRodney W. Grimes struct proc *p; 147df8bae1dSRodney W. Grimes register struct readv_args *uap; 148df8bae1dSRodney W. Grimes { 149df8bae1dSRodney W. Grimes register struct file *fp; 150df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 151df8bae1dSRodney W. Grimes struct uio auio; 152df8bae1dSRodney W. Grimes register struct iovec *iov; 153df8bae1dSRodney W. Grimes struct iovec *needfree; 154df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 155df8bae1dSRodney W. Grimes long i, cnt, error = 0; 156df8bae1dSRodney W. Grimes u_int iovlen; 157df8bae1dSRodney W. Grimes #ifdef KTRACE 158df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 159df8bae1dSRodney W. Grimes #endif 160df8bae1dSRodney W. Grimes 1617147b19dSBruce Evans if (((u_int)uap->fd) >= fdp->fd_nfiles || 1627147b19dSBruce Evans (fp = fdp->fd_ofiles[uap->fd]) == NULL || 163df8bae1dSRodney W. Grimes (fp->f_flag & FREAD) == 0) 164df8bae1dSRodney W. Grimes return (EBADF); 165df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 166df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 167df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 168df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_MAXIOV) 169df8bae1dSRodney W. Grimes return (EINVAL); 170df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 171df8bae1dSRodney W. Grimes needfree = iov; 172df8bae1dSRodney W. Grimes } else { 173df8bae1dSRodney W. Grimes iov = aiov; 174df8bae1dSRodney W. Grimes needfree = NULL; 175df8bae1dSRodney W. Grimes } 176df8bae1dSRodney W. Grimes auio.uio_iov = iov; 177df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 178df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 179df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 180df8bae1dSRodney W. Grimes auio.uio_procp = p; 1812c1011f7SJohn Dyson auio.uio_offset = -1; 182bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 183df8bae1dSRodney W. Grimes goto done; 184df8bae1dSRodney W. Grimes auio.uio_resid = 0; 185df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 186069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 187df8bae1dSRodney W. Grimes error = EINVAL; 188df8bae1dSRodney W. Grimes goto done; 189df8bae1dSRodney W. Grimes } 190069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 191df8bae1dSRodney W. Grimes iov++; 192df8bae1dSRodney W. Grimes } 193df8bae1dSRodney W. Grimes #ifdef KTRACE 194df8bae1dSRodney W. Grimes /* 195df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 196df8bae1dSRodney W. Grimes */ 197df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) { 198df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 199df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes #endif 202df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 203bb56ec4aSPoul-Henning Kamp if ((error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))) 204df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 205df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 206df8bae1dSRodney W. Grimes error = 0; 207df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 208df8bae1dSRodney W. Grimes #ifdef KTRACE 209df8bae1dSRodney W. Grimes if (ktriov != NULL) { 210df8bae1dSRodney W. Grimes if (error == 0) 2117147b19dSBruce Evans ktrgenio(p->p_tracep, uap->fd, UIO_READ, ktriov, 212df8bae1dSRodney W. Grimes cnt, error); 213df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 214df8bae1dSRodney W. Grimes } 215df8bae1dSRodney W. Grimes #endif 216cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 217df8bae1dSRodney W. Grimes done: 218df8bae1dSRodney W. Grimes if (needfree) 219df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 220df8bae1dSRodney W. Grimes return (error); 221df8bae1dSRodney W. Grimes } 222df8bae1dSRodney W. Grimes 223df8bae1dSRodney W. Grimes /* 224df8bae1dSRodney W. Grimes * Write system call 225df8bae1dSRodney W. Grimes */ 226d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 227df8bae1dSRodney W. Grimes struct write_args { 228df8bae1dSRodney W. Grimes int fd; 229134e06feSBruce Evans const void *buf; 230134e06feSBruce Evans size_t nbyte; 231df8bae1dSRodney W. Grimes }; 232d2d3e875SBruce Evans #endif 23326f9a767SRodney W. Grimes int 234cb226aaaSPoul-Henning Kamp write(p, uap) 235df8bae1dSRodney W. Grimes struct proc *p; 236df8bae1dSRodney W. Grimes register struct write_args *uap; 237df8bae1dSRodney W. Grimes { 238df8bae1dSRodney W. Grimes register struct file *fp; 239df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 240df8bae1dSRodney W. Grimes struct uio auio; 241df8bae1dSRodney W. Grimes struct iovec aiov; 242df8bae1dSRodney W. Grimes long cnt, error = 0; 243df8bae1dSRodney W. Grimes #ifdef KTRACE 244df8bae1dSRodney W. Grimes struct iovec ktriov; 245df8bae1dSRodney W. Grimes #endif 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 248df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 249df8bae1dSRodney W. Grimes (fp->f_flag & FWRITE) == 0) 250df8bae1dSRodney W. Grimes return (EBADF); 251df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t)uap->buf; 252df8bae1dSRodney W. Grimes aiov.iov_len = uap->nbyte; 253df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 254df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 2552c1011f7SJohn Dyson auio.uio_offset = -1; 256069e9bc1SDoug Rabson if (uap->nbyte > INT_MAX) 257069e9bc1SDoug Rabson return (EINVAL); 258df8bae1dSRodney W. Grimes auio.uio_resid = uap->nbyte; 259df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 260df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 261df8bae1dSRodney W. Grimes auio.uio_procp = p; 262df8bae1dSRodney W. Grimes #ifdef KTRACE 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 265df8bae1dSRodney W. Grimes */ 266df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) 267df8bae1dSRodney W. Grimes ktriov = aiov; 268df8bae1dSRodney W. Grimes #endif 269df8bae1dSRodney W. Grimes cnt = uap->nbyte; 270bb56ec4aSPoul-Henning Kamp if ((error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred))) { 271df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 272df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 273df8bae1dSRodney W. Grimes error = 0; 274df8bae1dSRodney W. Grimes if (error == EPIPE) 275df8bae1dSRodney W. Grimes psignal(p, SIGPIPE); 276df8bae1dSRodney W. Grimes } 277df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 278df8bae1dSRodney W. Grimes #ifdef KTRACE 279df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO) && error == 0) 280df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, 281df8bae1dSRodney W. Grimes &ktriov, cnt, error); 282df8bae1dSRodney W. Grimes #endif 283cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 284df8bae1dSRodney W. Grimes return (error); 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes 287df8bae1dSRodney W. Grimes /* 288df8bae1dSRodney W. Grimes * Gather write system call 289df8bae1dSRodney W. Grimes */ 290d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 291df8bae1dSRodney W. Grimes struct writev_args { 292df8bae1dSRodney W. Grimes int fd; 293df8bae1dSRodney W. Grimes struct iovec *iovp; 294df8bae1dSRodney W. Grimes u_int iovcnt; 295df8bae1dSRodney W. Grimes }; 296d2d3e875SBruce Evans #endif 29726f9a767SRodney W. Grimes int 298cb226aaaSPoul-Henning Kamp writev(p, uap) 299df8bae1dSRodney W. Grimes struct proc *p; 300df8bae1dSRodney W. Grimes register struct writev_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 register struct iovec *iov; 306df8bae1dSRodney W. Grimes struct iovec *needfree; 307df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 308df8bae1dSRodney W. Grimes long i, cnt, error = 0; 309df8bae1dSRodney W. Grimes u_int iovlen; 310df8bae1dSRodney W. Grimes #ifdef KTRACE 311df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 312df8bae1dSRodney W. Grimes #endif 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 315df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 316df8bae1dSRodney W. Grimes (fp->f_flag & FWRITE) == 0) 317df8bae1dSRodney W. Grimes return (EBADF); 318df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 319df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 320df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 321df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_MAXIOV) 322df8bae1dSRodney W. Grimes return (EINVAL); 323df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 324df8bae1dSRodney W. Grimes needfree = iov; 325df8bae1dSRodney W. Grimes } else { 326df8bae1dSRodney W. Grimes iov = aiov; 327df8bae1dSRodney W. Grimes needfree = NULL; 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes auio.uio_iov = iov; 330df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 331df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 332df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 333df8bae1dSRodney W. Grimes auio.uio_procp = p; 3342c1011f7SJohn Dyson auio.uio_offset = -1; 335bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 336df8bae1dSRodney W. Grimes goto done; 337df8bae1dSRodney W. Grimes auio.uio_resid = 0; 338df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 339069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 340df8bae1dSRodney W. Grimes error = EINVAL; 341df8bae1dSRodney W. Grimes goto done; 342df8bae1dSRodney W. Grimes } 343069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 344df8bae1dSRodney W. Grimes iov++; 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes #ifdef KTRACE 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 349df8bae1dSRodney W. Grimes */ 350df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) { 351df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 352df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 353df8bae1dSRodney W. Grimes } 354df8bae1dSRodney W. Grimes #endif 355df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 356bb56ec4aSPoul-Henning Kamp if ((error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred))) { 357df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 358df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 359df8bae1dSRodney W. Grimes error = 0; 360df8bae1dSRodney W. Grimes if (error == EPIPE) 361df8bae1dSRodney W. Grimes psignal(p, SIGPIPE); 362df8bae1dSRodney W. Grimes } 363df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 364df8bae1dSRodney W. Grimes #ifdef KTRACE 365df8bae1dSRodney W. Grimes if (ktriov != NULL) { 366df8bae1dSRodney W. Grimes if (error == 0) 367df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, 368df8bae1dSRodney W. Grimes ktriov, cnt, error); 369df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 370df8bae1dSRodney W. Grimes } 371df8bae1dSRodney W. Grimes #endif 372cb226aaaSPoul-Henning Kamp p->p_retval[0] = cnt; 373df8bae1dSRodney W. Grimes done: 374df8bae1dSRodney W. Grimes if (needfree) 375df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 376df8bae1dSRodney W. Grimes return (error); 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes 379df8bae1dSRodney W. Grimes /* 380df8bae1dSRodney W. Grimes * Ioctl system call 381df8bae1dSRodney W. Grimes */ 382d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 383df8bae1dSRodney W. Grimes struct ioctl_args { 384df8bae1dSRodney W. Grimes int fd; 385069e9bc1SDoug Rabson u_long com; 386df8bae1dSRodney W. Grimes caddr_t data; 387df8bae1dSRodney W. Grimes }; 388d2d3e875SBruce Evans #endif 389df8bae1dSRodney W. Grimes /* ARGSUSED */ 39026f9a767SRodney W. Grimes int 391cb226aaaSPoul-Henning Kamp ioctl(p, uap) 392df8bae1dSRodney W. Grimes struct proc *p; 393df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 394df8bae1dSRodney W. Grimes { 395df8bae1dSRodney W. Grimes register struct file *fp; 396df8bae1dSRodney W. Grimes register struct filedesc *fdp; 397831b9ef2SDoug Rabson register u_long com; 398831b9ef2SDoug Rabson int error; 399df8bae1dSRodney W. Grimes register u_int size; 400df8bae1dSRodney W. Grimes caddr_t data, memp; 401df8bae1dSRodney W. Grimes int tmp; 402df8bae1dSRodney W. Grimes #define STK_PARAMS 128 403df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 404df8bae1dSRodney W. Grimes 405df8bae1dSRodney W. Grimes fdp = p->p_fd; 406df8bae1dSRodney W. Grimes if ((u_int)uap->fd >= fdp->fd_nfiles || 407df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 408df8bae1dSRodney W. Grimes return (EBADF); 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes if ((fp->f_flag & (FREAD | FWRITE)) == 0) 411df8bae1dSRodney W. Grimes return (EBADF); 412df8bae1dSRodney W. Grimes 413df8bae1dSRodney W. Grimes switch (com = uap->com) { 414df8bae1dSRodney W. Grimes case FIONCLEX: 415df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 416df8bae1dSRodney W. Grimes return (0); 417df8bae1dSRodney W. Grimes case FIOCLEX: 418df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 419df8bae1dSRodney W. Grimes return (0); 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes 422df8bae1dSRodney W. Grimes /* 423df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 424df8bae1dSRodney W. Grimes * copied to/from the user's address space. 425df8bae1dSRodney W. Grimes */ 426df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 427df8bae1dSRodney W. Grimes if (size > IOCPARM_MAX) 428df8bae1dSRodney W. Grimes return (ENOTTY); 429df8bae1dSRodney W. Grimes memp = NULL; 430df8bae1dSRodney W. Grimes if (size > sizeof (stkbuf)) { 431df8bae1dSRodney W. Grimes memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 432df8bae1dSRodney W. Grimes data = memp; 433df8bae1dSRodney W. Grimes } else 434df8bae1dSRodney W. Grimes data = stkbuf; 435df8bae1dSRodney W. Grimes if (com&IOC_IN) { 436df8bae1dSRodney W. Grimes if (size) { 437df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 438df8bae1dSRodney W. Grimes if (error) { 439df8bae1dSRodney W. Grimes if (memp) 440df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 441df8bae1dSRodney W. Grimes return (error); 442df8bae1dSRodney W. Grimes } 443df8bae1dSRodney W. Grimes } else 444df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 445df8bae1dSRodney W. Grimes } else if ((com&IOC_OUT) && size) 446df8bae1dSRodney W. Grimes /* 447df8bae1dSRodney W. Grimes * Zero the buffer so the user always 448df8bae1dSRodney W. Grimes * gets back something deterministic. 449df8bae1dSRodney W. Grimes */ 450df8bae1dSRodney W. Grimes bzero(data, size); 451df8bae1dSRodney W. Grimes else if (com&IOC_VOID) 452df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 453df8bae1dSRodney W. Grimes 454df8bae1dSRodney W. Grimes switch (com) { 455df8bae1dSRodney W. Grimes 456df8bae1dSRodney W. Grimes case FIONBIO: 457bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 458df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 459df8bae1dSRodney W. Grimes else 460df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 461df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p); 462df8bae1dSRodney W. Grimes break; 463df8bae1dSRodney W. Grimes 464df8bae1dSRodney W. Grimes case FIOASYNC: 465bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 466df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 467df8bae1dSRodney W. Grimes else 468df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 469df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p); 470df8bae1dSRodney W. Grimes break; 471df8bae1dSRodney W. Grimes 472df8bae1dSRodney W. Grimes default: 473df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, com, data, p); 474df8bae1dSRodney W. Grimes /* 475df8bae1dSRodney W. Grimes * Copy any data to user, size was 476df8bae1dSRodney W. Grimes * already set and checked above. 477df8bae1dSRodney W. Grimes */ 478df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 479df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 480df8bae1dSRodney W. Grimes break; 481df8bae1dSRodney W. Grimes } 482df8bae1dSRodney W. Grimes if (memp) 483df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 484df8bae1dSRodney W. Grimes return (error); 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes 48787b6de2bSPoul-Henning Kamp static int nselcoll; 48887b6de2bSPoul-Henning Kamp int selwait; 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes /* 491df8bae1dSRodney W. Grimes * Select system call. 492df8bae1dSRodney W. Grimes */ 493d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 494df8bae1dSRodney W. Grimes struct select_args { 495b08f7993SSujal Patel int nd; 496df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 497df8bae1dSRodney W. Grimes struct timeval *tv; 498df8bae1dSRodney W. Grimes }; 499d2d3e875SBruce Evans #endif 50026f9a767SRodney W. Grimes int 501cb226aaaSPoul-Henning Kamp select(p, uap) 502df8bae1dSRodney W. Grimes register struct proc *p; 503df8bae1dSRodney W. Grimes register struct select_args *uap; 504df8bae1dSRodney W. Grimes { 505d5e4d7e1SBruce Evans /* 506d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 507d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 508d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 509d5e4d7e1SBruce Evans * of 256. 510d5e4d7e1SBruce Evans */ 511d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 512d5e4d7e1SBruce Evans fd_mask *ibits[3], *obits[3], *selbits, *sbp; 51300af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 51400af9731SPoul-Henning Kamp int s, ncoll, error, timo; 515d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 516df8bae1dSRodney W. Grimes 517b08f7993SSujal Patel if (uap->nd < 0) 518acbfbfeaSSujal Patel return (EINVAL); 519df8bae1dSRodney W. Grimes if (uap->nd > p->p_fd->fd_nfiles) 520df8bae1dSRodney W. Grimes uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 521b08f7993SSujal Patel 522d5e4d7e1SBruce Evans /* 523d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 524d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 525d5e4d7e1SBruce Evans */ 526d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 527d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 528d5e4d7e1SBruce Evans nbufbytes = 0; 529d5e4d7e1SBruce Evans if (uap->in != NULL) 530d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 531d5e4d7e1SBruce Evans if (uap->ou != NULL) 532d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 533d5e4d7e1SBruce Evans if (uap->ex != NULL) 534d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 535d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 536d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 537d5e4d7e1SBruce Evans else 538d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 539b08f7993SSujal Patel 540b08f7993SSujal Patel /* 541d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 542d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 543d5e4d7e1SBruce Evans * together. 544b08f7993SSujal Patel */ 545d5e4d7e1SBruce Evans sbp = selbits; 546df8bae1dSRodney W. Grimes #define getbits(name, x) \ 547d5e4d7e1SBruce Evans do { \ 548d5e4d7e1SBruce Evans if (uap->name == NULL) \ 549d5e4d7e1SBruce Evans ibits[x] = NULL; \ 550d5e4d7e1SBruce Evans else { \ 551d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 552d5e4d7e1SBruce Evans obits[x] = sbp; \ 553d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 554d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 555d5e4d7e1SBruce Evans if (error != 0) \ 556d5e4d7e1SBruce Evans goto done; \ 557d5e4d7e1SBruce Evans } \ 558d5e4d7e1SBruce Evans } while (0) 559df8bae1dSRodney W. Grimes getbits(in, 0); 560df8bae1dSRodney W. Grimes getbits(ou, 1); 561df8bae1dSRodney W. Grimes getbits(ex, 2); 562df8bae1dSRodney W. Grimes #undef getbits 563d5e4d7e1SBruce Evans if (nbufbytes != 0) 564d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 565df8bae1dSRodney W. Grimes 566df8bae1dSRodney W. Grimes if (uap->tv) { 567df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 568df8bae1dSRodney W. Grimes sizeof (atv)); 569df8bae1dSRodney W. Grimes if (error) 570df8bae1dSRodney W. Grimes goto done; 571df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 572df8bae1dSRodney W. Grimes error = EINVAL; 573df8bae1dSRodney W. Grimes goto done; 574df8bae1dSRodney W. Grimes } 575c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 57600af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 57780a39463SAndrey A. Chernov } else 57800af9731SPoul-Henning Kamp atv.tv_sec = 0; 57900af9731SPoul-Henning Kamp timo = 0; 580df8bae1dSRodney W. Grimes retry: 581df8bae1dSRodney W. Grimes ncoll = nselcoll; 582df8bae1dSRodney W. Grimes p->p_flag |= P_SELECT; 583cb226aaaSPoul-Henning Kamp error = selscan(p, ibits, obits, uap->nd); 584cb226aaaSPoul-Henning Kamp if (error || p->p_retval[0]) 585df8bae1dSRodney W. Grimes goto done; 58600af9731SPoul-Henning Kamp if (atv.tv_sec) { 587c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 58800af9731SPoul-Henning Kamp if (timevalcmp(&rtv, &atv, >=)) 589df8bae1dSRodney W. Grimes goto done; 59000af9731SPoul-Henning Kamp ttv = atv; 59100af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 59200af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 59300af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 594df8bae1dSRodney W. Grimes } 59500af9731SPoul-Henning Kamp s = splhigh(); 596df8bae1dSRodney W. Grimes if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 597df8bae1dSRodney W. Grimes splx(s); 598df8bae1dSRodney W. Grimes goto retry; 599df8bae1dSRodney W. Grimes } 600df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 601df8bae1dSRodney W. Grimes error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo); 602df8bae1dSRodney W. Grimes splx(s); 603df8bae1dSRodney W. Grimes if (error == 0) 604df8bae1dSRodney W. Grimes goto retry; 605df8bae1dSRodney W. Grimes done: 606df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 607df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 608df8bae1dSRodney W. Grimes if (error == ERESTART) 609df8bae1dSRodney W. Grimes error = EINTR; 610df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 611df8bae1dSRodney W. Grimes error = 0; 612df8bae1dSRodney W. Grimes #define putbits(name, x) \ 613d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 614df8bae1dSRodney W. Grimes error = error2; 615df8bae1dSRodney W. Grimes if (error == 0) { 616df8bae1dSRodney W. Grimes int error2; 617df8bae1dSRodney W. Grimes 618df8bae1dSRodney W. Grimes putbits(in, 0); 619df8bae1dSRodney W. Grimes putbits(ou, 1); 620df8bae1dSRodney W. Grimes putbits(ex, 2); 621df8bae1dSRodney W. Grimes #undef putbits 622df8bae1dSRodney W. Grimes } 623d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 624d5e4d7e1SBruce Evans free(selbits, M_SELECT); 625df8bae1dSRodney W. Grimes return (error); 626df8bae1dSRodney W. Grimes } 627df8bae1dSRodney W. Grimes 62887b6de2bSPoul-Henning Kamp static int 629cb226aaaSPoul-Henning Kamp selscan(p, ibits, obits, nfd) 630df8bae1dSRodney W. Grimes struct proc *p; 631b08f7993SSujal Patel fd_mask **ibits, **obits; 632cb226aaaSPoul-Henning Kamp int nfd; 633df8bae1dSRodney W. Grimes { 634df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 635df8bae1dSRodney W. Grimes register int msk, i, j, fd; 636df8bae1dSRodney W. Grimes register fd_mask bits; 637df8bae1dSRodney W. Grimes struct file *fp; 638df8bae1dSRodney W. Grimes int n = 0; 6392087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 64042d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 641df8bae1dSRodney W. Grimes 642df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 643d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 644d5e4d7e1SBruce Evans continue; 645df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 646b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 647df8bae1dSRodney W. Grimes while ((j = ffs(bits)) && (fd = i + --j) < nfd) { 648df8bae1dSRodney W. Grimes bits &= ~(1 << j); 649df8bae1dSRodney W. Grimes fp = fdp->fd_ofiles[fd]; 650df8bae1dSRodney W. Grimes if (fp == NULL) 651df8bae1dSRodney W. Grimes return (EBADF); 65242d11757SPeter Wemm if ((*fp->f_ops->fo_poll)(fp, flag[msk], 65342d11757SPeter Wemm fp->f_cred, p)) { 654b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 655b08f7993SSujal Patel (1 << ((fd) % NFDBITS)); 656df8bae1dSRodney W. Grimes n++; 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes } 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes } 661cb226aaaSPoul-Henning Kamp p->p_retval[0] = n; 662df8bae1dSRodney W. Grimes return (0); 663df8bae1dSRodney W. Grimes } 664df8bae1dSRodney W. Grimes 66542d11757SPeter Wemm /* 66642d11757SPeter Wemm * Poll system call. 66742d11757SPeter Wemm */ 66842d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 66942d11757SPeter Wemm struct poll_args { 67042d11757SPeter Wemm struct pollfd *fds; 67142d11757SPeter Wemm u_int nfds; 67242d11757SPeter Wemm int timeout; 67342d11757SPeter Wemm }; 67442d11757SPeter Wemm #endif 67542d11757SPeter Wemm int 676cb226aaaSPoul-Henning Kamp poll(p, uap) 67742d11757SPeter Wemm register struct proc *p; 67842d11757SPeter Wemm register struct poll_args *uap; 67942d11757SPeter Wemm { 68042d11757SPeter Wemm caddr_t bits; 68142d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 68200af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 68300af9731SPoul-Henning Kamp int s, ncoll, error = 0, timo; 68442d11757SPeter Wemm size_t ni; 68542d11757SPeter Wemm 68642d11757SPeter Wemm if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) { 68742d11757SPeter Wemm /* forgiving; slightly wrong */ 68842d11757SPeter Wemm SCARG(uap, nfds) = p->p_fd->fd_nfiles; 68942d11757SPeter Wemm } 69042d11757SPeter Wemm ni = SCARG(uap, nfds) * sizeof(struct pollfd); 69142d11757SPeter Wemm if (ni > sizeof(smallbits)) 69242d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 69342d11757SPeter Wemm else 69442d11757SPeter Wemm bits = smallbits; 69542d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 69642d11757SPeter Wemm if (error) 69742d11757SPeter Wemm goto done; 69842d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 69942d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 70042d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 70142d11757SPeter Wemm if (itimerfix(&atv)) { 70242d11757SPeter Wemm error = EINVAL; 70342d11757SPeter Wemm goto done; 70442d11757SPeter Wemm } 705c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 70600af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 70780a39463SAndrey A. Chernov } else 70800af9731SPoul-Henning Kamp atv.tv_sec = 0; 70900af9731SPoul-Henning Kamp timo = 0; 71042d11757SPeter Wemm retry: 71142d11757SPeter Wemm ncoll = nselcoll; 71242d11757SPeter Wemm p->p_flag |= P_SELECT; 713cb226aaaSPoul-Henning Kamp error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds)); 714cb226aaaSPoul-Henning Kamp if (error || p->p_retval[0]) 71542d11757SPeter Wemm goto done; 71600af9731SPoul-Henning Kamp if (atv.tv_sec) { 717c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 71800af9731SPoul-Henning Kamp if (timevalcmp(&rtv, &atv, >=)) 71942d11757SPeter Wemm goto done; 72000af9731SPoul-Henning Kamp ttv = atv; 72100af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 72200af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 72300af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 72442d11757SPeter Wemm } 72500af9731SPoul-Henning Kamp s = splhigh(); 72642d11757SPeter Wemm if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 72742d11757SPeter Wemm splx(s); 72842d11757SPeter Wemm goto retry; 72942d11757SPeter Wemm } 73042d11757SPeter Wemm p->p_flag &= ~P_SELECT; 73142d11757SPeter Wemm error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo); 73242d11757SPeter Wemm splx(s); 73342d11757SPeter Wemm if (error == 0) 73442d11757SPeter Wemm goto retry; 73542d11757SPeter Wemm done: 73642d11757SPeter Wemm p->p_flag &= ~P_SELECT; 73742d11757SPeter Wemm /* poll is not restarted after signals... */ 73842d11757SPeter Wemm if (error == ERESTART) 73942d11757SPeter Wemm error = EINTR; 74042d11757SPeter Wemm if (error == EWOULDBLOCK) 74142d11757SPeter Wemm error = 0; 74242d11757SPeter Wemm if (error == 0) { 74342d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 74442d11757SPeter Wemm if (error) 74542d11757SPeter Wemm goto out; 74642d11757SPeter Wemm } 74742d11757SPeter Wemm out: 74842d11757SPeter Wemm if (ni > sizeof(smallbits)) 74942d11757SPeter Wemm free(bits, M_TEMP); 75042d11757SPeter Wemm return (error); 75142d11757SPeter Wemm } 75242d11757SPeter Wemm 75342d11757SPeter Wemm static int 754cb226aaaSPoul-Henning Kamp pollscan(p, fds, nfd) 75542d11757SPeter Wemm struct proc *p; 75642d11757SPeter Wemm struct pollfd *fds; 75742d11757SPeter Wemm int nfd; 75842d11757SPeter Wemm { 75942d11757SPeter Wemm register struct filedesc *fdp = p->p_fd; 76042d11757SPeter Wemm int i; 76142d11757SPeter Wemm struct file *fp; 76242d11757SPeter Wemm int n = 0; 76342d11757SPeter Wemm 76442d11757SPeter Wemm for (i = 0; i < nfd; i++, fds++) { 765337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 76642d11757SPeter Wemm fds->revents = POLLNVAL; 76742d11757SPeter Wemm n++; 768337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 769337c9691SJordan K. Hubbard fds->revents = 0; 77042d11757SPeter Wemm } else { 77142d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 77242d11757SPeter Wemm if (fp == 0) { 77342d11757SPeter Wemm fds->revents = POLLNVAL; 77442d11757SPeter Wemm n++; 77542d11757SPeter Wemm } else { 7762087c896SBruce Evans /* 7772087c896SBruce Evans * Note: backend also returns POLLHUP and 7782087c896SBruce Evans * POLLERR if appropriate. 7792087c896SBruce Evans */ 78042d11757SPeter Wemm fds->revents = (*fp->f_ops->fo_poll)(fp, 78142d11757SPeter Wemm fds->events, fp->f_cred, p); 78242d11757SPeter Wemm if (fds->revents != 0) 78342d11757SPeter Wemm n++; 78442d11757SPeter Wemm } 78542d11757SPeter Wemm } 78642d11757SPeter Wemm } 787cb226aaaSPoul-Henning Kamp p->p_retval[0] = n; 78842d11757SPeter Wemm return (0); 78942d11757SPeter Wemm } 79042d11757SPeter Wemm 79142d11757SPeter Wemm /* 79242d11757SPeter Wemm * OpenBSD poll system call. 79342d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 79442d11757SPeter Wemm */ 79542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 79642d11757SPeter Wemm struct openbsd_poll_args { 79742d11757SPeter Wemm struct pollfd *fds; 79842d11757SPeter Wemm u_int nfds; 79942d11757SPeter Wemm int timeout; 80042d11757SPeter Wemm }; 80142d11757SPeter Wemm #endif 80242d11757SPeter Wemm int 803cb226aaaSPoul-Henning Kamp openbsd_poll(p, uap) 80442d11757SPeter Wemm register struct proc *p; 80542d11757SPeter Wemm register struct openbsd_poll_args *uap; 80642d11757SPeter Wemm { 807cb226aaaSPoul-Henning Kamp return (poll(p, (struct poll_args *)uap)); 80842d11757SPeter Wemm } 80942d11757SPeter Wemm 810df8bae1dSRodney W. Grimes /*ARGSUSED*/ 81126f9a767SRodney W. Grimes int 81242d11757SPeter Wemm seltrue(dev, events, p) 813df8bae1dSRodney W. Grimes dev_t dev; 81442d11757SPeter Wemm int events; 815df8bae1dSRodney W. Grimes struct proc *p; 816df8bae1dSRodney W. Grimes { 817df8bae1dSRodney W. Grimes 81842d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 819df8bae1dSRodney W. Grimes } 820df8bae1dSRodney W. Grimes 821df8bae1dSRodney W. Grimes /* 822df8bae1dSRodney W. Grimes * Record a select request. 823df8bae1dSRodney W. Grimes */ 824df8bae1dSRodney W. Grimes void 825df8bae1dSRodney W. Grimes selrecord(selector, sip) 826df8bae1dSRodney W. Grimes struct proc *selector; 827df8bae1dSRodney W. Grimes struct selinfo *sip; 828df8bae1dSRodney W. Grimes { 829df8bae1dSRodney W. Grimes struct proc *p; 830df8bae1dSRodney W. Grimes pid_t mypid; 831df8bae1dSRodney W. Grimes 832df8bae1dSRodney W. Grimes mypid = selector->p_pid; 833df8bae1dSRodney W. Grimes if (sip->si_pid == mypid) 834df8bae1dSRodney W. Grimes return; 835df8bae1dSRodney W. Grimes if (sip->si_pid && (p = pfind(sip->si_pid)) && 836df8bae1dSRodney W. Grimes p->p_wchan == (caddr_t)&selwait) 837df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 838df8bae1dSRodney W. Grimes else 839df8bae1dSRodney W. Grimes sip->si_pid = mypid; 840df8bae1dSRodney W. Grimes } 841df8bae1dSRodney W. Grimes 842df8bae1dSRodney W. Grimes /* 843df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 844df8bae1dSRodney W. Grimes */ 845df8bae1dSRodney W. Grimes void 846df8bae1dSRodney W. Grimes selwakeup(sip) 847df8bae1dSRodney W. Grimes register struct selinfo *sip; 848df8bae1dSRodney W. Grimes { 849df8bae1dSRodney W. Grimes register struct proc *p; 850df8bae1dSRodney W. Grimes int s; 851df8bae1dSRodney W. Grimes 852df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 853df8bae1dSRodney W. Grimes return; 854df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 855df8bae1dSRodney W. Grimes nselcoll++; 856df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 857df8bae1dSRodney W. Grimes wakeup((caddr_t)&selwait); 858df8bae1dSRodney W. Grimes } 859df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 860df8bae1dSRodney W. Grimes sip->si_pid = 0; 861df8bae1dSRodney W. Grimes if (p != NULL) { 862df8bae1dSRodney W. Grimes s = splhigh(); 863df8bae1dSRodney W. Grimes if (p->p_wchan == (caddr_t)&selwait) { 864df8bae1dSRodney W. Grimes if (p->p_stat == SSLEEP) 865df8bae1dSRodney W. Grimes setrunnable(p); 866df8bae1dSRodney W. Grimes else 867df8bae1dSRodney W. Grimes unsleep(p); 868df8bae1dSRodney W. Grimes } else if (p->p_flag & P_SELECT) 869df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 870df8bae1dSRodney W. Grimes splx(s); 871df8bae1dSRodney W. Grimes } 872df8bae1dSRodney W. Grimes } 873