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 39134e06feSBruce Evans * $Id: sys_generic.c,v 1.40 1998/08/24 08:39:38 dfr 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 case FIOSETOWN: 473df8bae1dSRodney W. Grimes tmp = *(int *)data; 474df8bae1dSRodney W. Grimes if (fp->f_type == DTYPE_SOCKET) { 475df8bae1dSRodney W. Grimes ((struct socket *)fp->f_data)->so_pgid = tmp; 476df8bae1dSRodney W. Grimes error = 0; 477df8bae1dSRodney W. Grimes break; 478df8bae1dSRodney W. Grimes } 479df8bae1dSRodney W. Grimes if (tmp <= 0) { 480df8bae1dSRodney W. Grimes tmp = -tmp; 481df8bae1dSRodney W. Grimes } else { 482df8bae1dSRodney W. Grimes struct proc *p1 = pfind(tmp); 483df8bae1dSRodney W. Grimes if (p1 == 0) { 484df8bae1dSRodney W. Grimes error = ESRCH; 485df8bae1dSRodney W. Grimes break; 486df8bae1dSRodney W. Grimes } 487df8bae1dSRodney W. Grimes tmp = p1->p_pgrp->pg_id; 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl) 490df8bae1dSRodney W. Grimes (fp, (int)TIOCSPGRP, (caddr_t)&tmp, p); 491df8bae1dSRodney W. Grimes break; 492df8bae1dSRodney W. Grimes 493df8bae1dSRodney W. Grimes case FIOGETOWN: 494df8bae1dSRodney W. Grimes if (fp->f_type == DTYPE_SOCKET) { 495df8bae1dSRodney W. Grimes error = 0; 496df8bae1dSRodney W. Grimes *(int *)data = ((struct socket *)fp->f_data)->so_pgid; 497df8bae1dSRodney W. Grimes break; 498df8bae1dSRodney W. Grimes } 499df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, (int)TIOCGPGRP, data, p); 500df8bae1dSRodney W. Grimes *(int *)data = -*(int *)data; 501df8bae1dSRodney W. Grimes break; 502df8bae1dSRodney W. Grimes 503df8bae1dSRodney W. Grimes default: 504df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, com, data, p); 505df8bae1dSRodney W. Grimes /* 506df8bae1dSRodney W. Grimes * Copy any data to user, size was 507df8bae1dSRodney W. Grimes * already set and checked above. 508df8bae1dSRodney W. Grimes */ 509df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 510df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 511df8bae1dSRodney W. Grimes break; 512df8bae1dSRodney W. Grimes } 513df8bae1dSRodney W. Grimes if (memp) 514df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 515df8bae1dSRodney W. Grimes return (error); 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes 51887b6de2bSPoul-Henning Kamp static int nselcoll; 51987b6de2bSPoul-Henning Kamp int selwait; 520df8bae1dSRodney W. Grimes 521df8bae1dSRodney W. Grimes /* 522df8bae1dSRodney W. Grimes * Select system call. 523df8bae1dSRodney W. Grimes */ 524d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 525df8bae1dSRodney W. Grimes struct select_args { 526b08f7993SSujal Patel int nd; 527df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 528df8bae1dSRodney W. Grimes struct timeval *tv; 529df8bae1dSRodney W. Grimes }; 530d2d3e875SBruce Evans #endif 53126f9a767SRodney W. Grimes int 532cb226aaaSPoul-Henning Kamp select(p, uap) 533df8bae1dSRodney W. Grimes register struct proc *p; 534df8bae1dSRodney W. Grimes register struct select_args *uap; 535df8bae1dSRodney W. Grimes { 536d5e4d7e1SBruce Evans /* 537d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 538d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 539d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 540d5e4d7e1SBruce Evans * of 256. 541d5e4d7e1SBruce Evans */ 542d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 543d5e4d7e1SBruce Evans fd_mask *ibits[3], *obits[3], *selbits, *sbp; 54400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 54500af9731SPoul-Henning Kamp int s, ncoll, error, timo; 546d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 547df8bae1dSRodney W. Grimes 548b08f7993SSujal Patel if (uap->nd < 0) 549acbfbfeaSSujal Patel return (EINVAL); 550df8bae1dSRodney W. Grimes if (uap->nd > p->p_fd->fd_nfiles) 551df8bae1dSRodney W. Grimes uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 552b08f7993SSujal Patel 553d5e4d7e1SBruce Evans /* 554d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 555d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 556d5e4d7e1SBruce Evans */ 557d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 558d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 559d5e4d7e1SBruce Evans nbufbytes = 0; 560d5e4d7e1SBruce Evans if (uap->in != NULL) 561d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 562d5e4d7e1SBruce Evans if (uap->ou != NULL) 563d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 564d5e4d7e1SBruce Evans if (uap->ex != NULL) 565d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 566d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 567d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 568d5e4d7e1SBruce Evans else 569d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 570b08f7993SSujal Patel 571b08f7993SSujal Patel /* 572d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 573d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 574d5e4d7e1SBruce Evans * together. 575b08f7993SSujal Patel */ 576d5e4d7e1SBruce Evans sbp = selbits; 577df8bae1dSRodney W. Grimes #define getbits(name, x) \ 578d5e4d7e1SBruce Evans do { \ 579d5e4d7e1SBruce Evans if (uap->name == NULL) \ 580d5e4d7e1SBruce Evans ibits[x] = NULL; \ 581d5e4d7e1SBruce Evans else { \ 582d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 583d5e4d7e1SBruce Evans obits[x] = sbp; \ 584d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 585d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 586d5e4d7e1SBruce Evans if (error != 0) \ 587d5e4d7e1SBruce Evans goto done; \ 588d5e4d7e1SBruce Evans } \ 589d5e4d7e1SBruce Evans } while (0) 590df8bae1dSRodney W. Grimes getbits(in, 0); 591df8bae1dSRodney W. Grimes getbits(ou, 1); 592df8bae1dSRodney W. Grimes getbits(ex, 2); 593df8bae1dSRodney W. Grimes #undef getbits 594d5e4d7e1SBruce Evans if (nbufbytes != 0) 595d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 596df8bae1dSRodney W. Grimes 597df8bae1dSRodney W. Grimes if (uap->tv) { 598df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 599df8bae1dSRodney W. Grimes sizeof (atv)); 600df8bae1dSRodney W. Grimes if (error) 601df8bae1dSRodney W. Grimes goto done; 602df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 603df8bae1dSRodney W. Grimes error = EINVAL; 604df8bae1dSRodney W. Grimes goto done; 605df8bae1dSRodney W. Grimes } 606c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 60700af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 60880a39463SAndrey A. Chernov } else 60900af9731SPoul-Henning Kamp atv.tv_sec = 0; 61000af9731SPoul-Henning Kamp timo = 0; 611df8bae1dSRodney W. Grimes retry: 612df8bae1dSRodney W. Grimes ncoll = nselcoll; 613df8bae1dSRodney W. Grimes p->p_flag |= P_SELECT; 614cb226aaaSPoul-Henning Kamp error = selscan(p, ibits, obits, uap->nd); 615cb226aaaSPoul-Henning Kamp if (error || p->p_retval[0]) 616df8bae1dSRodney W. Grimes goto done; 61700af9731SPoul-Henning Kamp if (atv.tv_sec) { 618c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 61900af9731SPoul-Henning Kamp if (timevalcmp(&rtv, &atv, >=)) 620df8bae1dSRodney W. Grimes goto done; 62100af9731SPoul-Henning Kamp ttv = atv; 62200af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 62300af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 62400af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 625df8bae1dSRodney W. Grimes } 62600af9731SPoul-Henning Kamp s = splhigh(); 627df8bae1dSRodney W. Grimes if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 628df8bae1dSRodney W. Grimes splx(s); 629df8bae1dSRodney W. Grimes goto retry; 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 632df8bae1dSRodney W. Grimes error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo); 633df8bae1dSRodney W. Grimes splx(s); 634df8bae1dSRodney W. Grimes if (error == 0) 635df8bae1dSRodney W. Grimes goto retry; 636df8bae1dSRodney W. Grimes done: 637df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 638df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 639df8bae1dSRodney W. Grimes if (error == ERESTART) 640df8bae1dSRodney W. Grimes error = EINTR; 641df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 642df8bae1dSRodney W. Grimes error = 0; 643df8bae1dSRodney W. Grimes #define putbits(name, x) \ 644d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 645df8bae1dSRodney W. Grimes error = error2; 646df8bae1dSRodney W. Grimes if (error == 0) { 647df8bae1dSRodney W. Grimes int error2; 648df8bae1dSRodney W. Grimes 649df8bae1dSRodney W. Grimes putbits(in, 0); 650df8bae1dSRodney W. Grimes putbits(ou, 1); 651df8bae1dSRodney W. Grimes putbits(ex, 2); 652df8bae1dSRodney W. Grimes #undef putbits 653df8bae1dSRodney W. Grimes } 654d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 655d5e4d7e1SBruce Evans free(selbits, M_SELECT); 656df8bae1dSRodney W. Grimes return (error); 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes 65987b6de2bSPoul-Henning Kamp static int 660cb226aaaSPoul-Henning Kamp selscan(p, ibits, obits, nfd) 661df8bae1dSRodney W. Grimes struct proc *p; 662b08f7993SSujal Patel fd_mask **ibits, **obits; 663cb226aaaSPoul-Henning Kamp int nfd; 664df8bae1dSRodney W. Grimes { 665df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 666df8bae1dSRodney W. Grimes register int msk, i, j, fd; 667df8bae1dSRodney W. Grimes register fd_mask bits; 668df8bae1dSRodney W. Grimes struct file *fp; 669df8bae1dSRodney W. Grimes int n = 0; 6702087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 67142d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 674d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 675d5e4d7e1SBruce Evans continue; 676df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 677b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 678df8bae1dSRodney W. Grimes while ((j = ffs(bits)) && (fd = i + --j) < nfd) { 679df8bae1dSRodney W. Grimes bits &= ~(1 << j); 680df8bae1dSRodney W. Grimes fp = fdp->fd_ofiles[fd]; 681df8bae1dSRodney W. Grimes if (fp == NULL) 682df8bae1dSRodney W. Grimes return (EBADF); 68342d11757SPeter Wemm if ((*fp->f_ops->fo_poll)(fp, flag[msk], 68442d11757SPeter Wemm fp->f_cred, p)) { 685b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 686b08f7993SSujal Patel (1 << ((fd) % NFDBITS)); 687df8bae1dSRodney W. Grimes n++; 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes } 690df8bae1dSRodney W. Grimes } 691df8bae1dSRodney W. Grimes } 692cb226aaaSPoul-Henning Kamp p->p_retval[0] = n; 693df8bae1dSRodney W. Grimes return (0); 694df8bae1dSRodney W. Grimes } 695df8bae1dSRodney W. Grimes 69642d11757SPeter Wemm /* 69742d11757SPeter Wemm * Poll system call. 69842d11757SPeter Wemm */ 69942d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 70042d11757SPeter Wemm struct poll_args { 70142d11757SPeter Wemm struct pollfd *fds; 70242d11757SPeter Wemm u_int nfds; 70342d11757SPeter Wemm int timeout; 70442d11757SPeter Wemm }; 70542d11757SPeter Wemm #endif 70642d11757SPeter Wemm int 707cb226aaaSPoul-Henning Kamp poll(p, uap) 70842d11757SPeter Wemm register struct proc *p; 70942d11757SPeter Wemm register struct poll_args *uap; 71042d11757SPeter Wemm { 71142d11757SPeter Wemm caddr_t bits; 71242d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 71300af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 71400af9731SPoul-Henning Kamp int s, ncoll, error = 0, timo; 71542d11757SPeter Wemm size_t ni; 71642d11757SPeter Wemm 71742d11757SPeter Wemm if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) { 71842d11757SPeter Wemm /* forgiving; slightly wrong */ 71942d11757SPeter Wemm SCARG(uap, nfds) = p->p_fd->fd_nfiles; 72042d11757SPeter Wemm } 72142d11757SPeter Wemm ni = SCARG(uap, nfds) * sizeof(struct pollfd); 72242d11757SPeter Wemm if (ni > sizeof(smallbits)) 72342d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 72442d11757SPeter Wemm else 72542d11757SPeter Wemm bits = smallbits; 72642d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 72742d11757SPeter Wemm if (error) 72842d11757SPeter Wemm goto done; 72942d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 73042d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 73142d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 73242d11757SPeter Wemm if (itimerfix(&atv)) { 73342d11757SPeter Wemm error = EINVAL; 73442d11757SPeter Wemm goto done; 73542d11757SPeter Wemm } 736c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 73700af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 73880a39463SAndrey A. Chernov } else 73900af9731SPoul-Henning Kamp atv.tv_sec = 0; 74000af9731SPoul-Henning Kamp timo = 0; 74142d11757SPeter Wemm retry: 74242d11757SPeter Wemm ncoll = nselcoll; 74342d11757SPeter Wemm p->p_flag |= P_SELECT; 744cb226aaaSPoul-Henning Kamp error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds)); 745cb226aaaSPoul-Henning Kamp if (error || p->p_retval[0]) 74642d11757SPeter Wemm goto done; 74700af9731SPoul-Henning Kamp if (atv.tv_sec) { 748c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 74900af9731SPoul-Henning Kamp if (timevalcmp(&rtv, &atv, >=)) 75042d11757SPeter Wemm goto done; 75100af9731SPoul-Henning Kamp ttv = atv; 75200af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 75300af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 75400af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 75542d11757SPeter Wemm } 75600af9731SPoul-Henning Kamp s = splhigh(); 75742d11757SPeter Wemm if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 75842d11757SPeter Wemm splx(s); 75942d11757SPeter Wemm goto retry; 76042d11757SPeter Wemm } 76142d11757SPeter Wemm p->p_flag &= ~P_SELECT; 76242d11757SPeter Wemm error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo); 76342d11757SPeter Wemm splx(s); 76442d11757SPeter Wemm if (error == 0) 76542d11757SPeter Wemm goto retry; 76642d11757SPeter Wemm done: 76742d11757SPeter Wemm p->p_flag &= ~P_SELECT; 76842d11757SPeter Wemm /* poll is not restarted after signals... */ 76942d11757SPeter Wemm if (error == ERESTART) 77042d11757SPeter Wemm error = EINTR; 77142d11757SPeter Wemm if (error == EWOULDBLOCK) 77242d11757SPeter Wemm error = 0; 77342d11757SPeter Wemm if (error == 0) { 77442d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 77542d11757SPeter Wemm if (error) 77642d11757SPeter Wemm goto out; 77742d11757SPeter Wemm } 77842d11757SPeter Wemm out: 77942d11757SPeter Wemm if (ni > sizeof(smallbits)) 78042d11757SPeter Wemm free(bits, M_TEMP); 78142d11757SPeter Wemm return (error); 78242d11757SPeter Wemm } 78342d11757SPeter Wemm 78442d11757SPeter Wemm static int 785cb226aaaSPoul-Henning Kamp pollscan(p, fds, nfd) 78642d11757SPeter Wemm struct proc *p; 78742d11757SPeter Wemm struct pollfd *fds; 78842d11757SPeter Wemm int nfd; 78942d11757SPeter Wemm { 79042d11757SPeter Wemm register struct filedesc *fdp = p->p_fd; 79142d11757SPeter Wemm int i; 79242d11757SPeter Wemm struct file *fp; 79342d11757SPeter Wemm int n = 0; 79442d11757SPeter Wemm 79542d11757SPeter Wemm for (i = 0; i < nfd; i++, fds++) { 79642d11757SPeter Wemm if ((u_int)fds->fd >= fdp->fd_nfiles) { 79742d11757SPeter Wemm fds->revents = POLLNVAL; 79842d11757SPeter Wemm n++; 79942d11757SPeter Wemm } else { 80042d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 80142d11757SPeter Wemm if (fp == 0) { 80242d11757SPeter Wemm fds->revents = POLLNVAL; 80342d11757SPeter Wemm n++; 80442d11757SPeter Wemm } else { 8052087c896SBruce Evans /* 8062087c896SBruce Evans * Note: backend also returns POLLHUP and 8072087c896SBruce Evans * POLLERR if appropriate. 8082087c896SBruce Evans */ 80942d11757SPeter Wemm fds->revents = (*fp->f_ops->fo_poll)(fp, 81042d11757SPeter Wemm fds->events, fp->f_cred, p); 81142d11757SPeter Wemm if (fds->revents != 0) 81242d11757SPeter Wemm n++; 81342d11757SPeter Wemm } 81442d11757SPeter Wemm } 81542d11757SPeter Wemm } 816cb226aaaSPoul-Henning Kamp p->p_retval[0] = n; 81742d11757SPeter Wemm return (0); 81842d11757SPeter Wemm } 81942d11757SPeter Wemm 82042d11757SPeter Wemm /* 82142d11757SPeter Wemm * OpenBSD poll system call. 82242d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 82342d11757SPeter Wemm */ 82442d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 82542d11757SPeter Wemm struct openbsd_poll_args { 82642d11757SPeter Wemm struct pollfd *fds; 82742d11757SPeter Wemm u_int nfds; 82842d11757SPeter Wemm int timeout; 82942d11757SPeter Wemm }; 83042d11757SPeter Wemm #endif 83142d11757SPeter Wemm int 832cb226aaaSPoul-Henning Kamp openbsd_poll(p, uap) 83342d11757SPeter Wemm register struct proc *p; 83442d11757SPeter Wemm register struct openbsd_poll_args *uap; 83542d11757SPeter Wemm { 836cb226aaaSPoul-Henning Kamp return (poll(p, (struct poll_args *)uap)); 83742d11757SPeter Wemm } 83842d11757SPeter Wemm 839df8bae1dSRodney W. Grimes /*ARGSUSED*/ 84026f9a767SRodney W. Grimes int 84142d11757SPeter Wemm seltrue(dev, events, p) 842df8bae1dSRodney W. Grimes dev_t dev; 84342d11757SPeter Wemm int events; 844df8bae1dSRodney W. Grimes struct proc *p; 845df8bae1dSRodney W. Grimes { 846df8bae1dSRodney W. Grimes 84742d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 848df8bae1dSRodney W. Grimes } 849df8bae1dSRodney W. Grimes 850df8bae1dSRodney W. Grimes /* 851df8bae1dSRodney W. Grimes * Record a select request. 852df8bae1dSRodney W. Grimes */ 853df8bae1dSRodney W. Grimes void 854df8bae1dSRodney W. Grimes selrecord(selector, sip) 855df8bae1dSRodney W. Grimes struct proc *selector; 856df8bae1dSRodney W. Grimes struct selinfo *sip; 857df8bae1dSRodney W. Grimes { 858df8bae1dSRodney W. Grimes struct proc *p; 859df8bae1dSRodney W. Grimes pid_t mypid; 860df8bae1dSRodney W. Grimes 861df8bae1dSRodney W. Grimes mypid = selector->p_pid; 862df8bae1dSRodney W. Grimes if (sip->si_pid == mypid) 863df8bae1dSRodney W. Grimes return; 864df8bae1dSRodney W. Grimes if (sip->si_pid && (p = pfind(sip->si_pid)) && 865df8bae1dSRodney W. Grimes p->p_wchan == (caddr_t)&selwait) 866df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 867df8bae1dSRodney W. Grimes else 868df8bae1dSRodney W. Grimes sip->si_pid = mypid; 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes 871df8bae1dSRodney W. Grimes /* 872df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 873df8bae1dSRodney W. Grimes */ 874df8bae1dSRodney W. Grimes void 875df8bae1dSRodney W. Grimes selwakeup(sip) 876df8bae1dSRodney W. Grimes register struct selinfo *sip; 877df8bae1dSRodney W. Grimes { 878df8bae1dSRodney W. Grimes register struct proc *p; 879df8bae1dSRodney W. Grimes int s; 880df8bae1dSRodney W. Grimes 881df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 882df8bae1dSRodney W. Grimes return; 883df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 884df8bae1dSRodney W. Grimes nselcoll++; 885df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 886df8bae1dSRodney W. Grimes wakeup((caddr_t)&selwait); 887df8bae1dSRodney W. Grimes } 888df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 889df8bae1dSRodney W. Grimes sip->si_pid = 0; 890df8bae1dSRodney W. Grimes if (p != NULL) { 891df8bae1dSRodney W. Grimes s = splhigh(); 892df8bae1dSRodney W. Grimes if (p->p_wchan == (caddr_t)&selwait) { 893df8bae1dSRodney W. Grimes if (p->p_stat == SSLEEP) 894df8bae1dSRodney W. Grimes setrunnable(p); 895df8bae1dSRodney W. Grimes else 896df8bae1dSRodney W. Grimes unsleep(p); 897df8bae1dSRodney W. Grimes } else if (p->p_flag & P_SELECT) 898df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 899df8bae1dSRodney W. Grimes splx(s); 900df8bae1dSRodney W. Grimes } 901df8bae1dSRodney W. Grimes } 902