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 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 43df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 44df8bae1dSRodney W. Grimes #include <sys/ioctl.h> 45df8bae1dSRodney W. Grimes #include <sys/file.h> 46df8bae1dSRodney W. Grimes #include <sys/proc.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48df8bae1dSRodney W. Grimes #include <sys/uio.h> 49df8bae1dSRodney W. Grimes #include <sys/kernel.h> 50df8bae1dSRodney W. Grimes #include <sys/stat.h> 51df8bae1dSRodney W. Grimes #include <sys/malloc.h> 52df8bae1dSRodney W. Grimes #ifdef KTRACE 53df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 54df8bae1dSRodney W. Grimes #endif 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes /* 57df8bae1dSRodney W. Grimes * Read system call. 58df8bae1dSRodney W. Grimes */ 59df8bae1dSRodney W. Grimes struct read_args { 60df8bae1dSRodney W. Grimes int fd; 61df8bae1dSRodney W. Grimes char *buf; 62df8bae1dSRodney W. Grimes u_int nbyte; 63df8bae1dSRodney W. Grimes }; 64df8bae1dSRodney W. Grimes /* ARGSUSED */ 65df8bae1dSRodney W. Grimes read(p, uap, retval) 66df8bae1dSRodney W. Grimes struct proc *p; 67df8bae1dSRodney W. Grimes register struct read_args *uap; 68df8bae1dSRodney W. Grimes int *retval; 69df8bae1dSRodney W. Grimes { 70df8bae1dSRodney W. Grimes register struct file *fp; 71df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 72df8bae1dSRodney W. Grimes struct uio auio; 73df8bae1dSRodney W. Grimes struct iovec aiov; 74df8bae1dSRodney W. Grimes long cnt, error = 0; 75df8bae1dSRodney W. Grimes #ifdef KTRACE 76df8bae1dSRodney W. Grimes struct iovec ktriov; 77df8bae1dSRodney W. Grimes #endif 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 80df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 81df8bae1dSRodney W. Grimes (fp->f_flag & FREAD) == 0) 82df8bae1dSRodney W. Grimes return (EBADF); 83df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t)uap->buf; 84df8bae1dSRodney W. Grimes aiov.iov_len = uap->nbyte; 85df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 86df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 87df8bae1dSRodney W. Grimes auio.uio_resid = uap->nbyte; 88df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 89df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 90df8bae1dSRodney W. Grimes auio.uio_procp = p; 91df8bae1dSRodney W. Grimes #ifdef KTRACE 92df8bae1dSRodney W. Grimes /* 93df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 94df8bae1dSRodney W. Grimes */ 95df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) 96df8bae1dSRodney W. Grimes ktriov = aiov; 97df8bae1dSRodney W. Grimes #endif 98df8bae1dSRodney W. Grimes cnt = uap->nbyte; 99df8bae1dSRodney W. Grimes if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred)) 100df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 101df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 102df8bae1dSRodney W. Grimes error = 0; 103df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 104df8bae1dSRodney W. Grimes #ifdef KTRACE 105df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO) && error == 0) 106df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktriov, cnt, error); 107df8bae1dSRodney W. Grimes #endif 108df8bae1dSRodney W. Grimes *retval = cnt; 109df8bae1dSRodney W. Grimes return (error); 110df8bae1dSRodney W. Grimes } 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes /* 113df8bae1dSRodney W. Grimes * Scatter read system call. 114df8bae1dSRodney W. Grimes */ 115df8bae1dSRodney W. Grimes struct readv_args { 116df8bae1dSRodney W. Grimes int fdes; 117df8bae1dSRodney W. Grimes struct iovec *iovp; 118df8bae1dSRodney W. Grimes u_int iovcnt; 119df8bae1dSRodney W. Grimes }; 120df8bae1dSRodney W. Grimes readv(p, uap, retval) 121df8bae1dSRodney W. Grimes struct proc *p; 122df8bae1dSRodney W. Grimes register struct readv_args *uap; 123df8bae1dSRodney W. Grimes int *retval; 124df8bae1dSRodney W. Grimes { 125df8bae1dSRodney W. Grimes register struct file *fp; 126df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 127df8bae1dSRodney W. Grimes struct uio auio; 128df8bae1dSRodney W. Grimes register struct iovec *iov; 129df8bae1dSRodney W. Grimes struct iovec *needfree; 130df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 131df8bae1dSRodney W. Grimes long i, cnt, error = 0; 132df8bae1dSRodney W. Grimes u_int iovlen; 133df8bae1dSRodney W. Grimes #ifdef KTRACE 134df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 135df8bae1dSRodney W. Grimes #endif 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes if (((u_int)uap->fdes) >= fdp->fd_nfiles || 138df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fdes]) == NULL || 139df8bae1dSRodney W. Grimes (fp->f_flag & FREAD) == 0) 140df8bae1dSRodney W. Grimes return (EBADF); 141df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 142df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 143df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 144df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_MAXIOV) 145df8bae1dSRodney W. Grimes return (EINVAL); 146df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 147df8bae1dSRodney W. Grimes needfree = iov; 148df8bae1dSRodney W. Grimes } else { 149df8bae1dSRodney W. Grimes iov = aiov; 150df8bae1dSRodney W. Grimes needfree = NULL; 151df8bae1dSRodney W. Grimes } 152df8bae1dSRodney W. Grimes auio.uio_iov = iov; 153df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 154df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 155df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 156df8bae1dSRodney W. Grimes auio.uio_procp = p; 157df8bae1dSRodney W. Grimes if (error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)) 158df8bae1dSRodney W. Grimes goto done; 159df8bae1dSRodney W. Grimes auio.uio_resid = 0; 160df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 161df8bae1dSRodney W. Grimes if (iov->iov_len < 0) { 162df8bae1dSRodney W. Grimes error = EINVAL; 163df8bae1dSRodney W. Grimes goto done; 164df8bae1dSRodney W. Grimes } 165df8bae1dSRodney W. Grimes auio.uio_resid += iov->iov_len; 166df8bae1dSRodney W. Grimes if (auio.uio_resid < 0) { 167df8bae1dSRodney W. Grimes error = EINVAL; 168df8bae1dSRodney W. Grimes goto done; 169df8bae1dSRodney W. Grimes } 170df8bae1dSRodney W. Grimes iov++; 171df8bae1dSRodney W. Grimes } 172df8bae1dSRodney W. Grimes #ifdef KTRACE 173df8bae1dSRodney W. Grimes /* 174df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 175df8bae1dSRodney W. Grimes */ 176df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) { 177df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 178df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 179df8bae1dSRodney W. Grimes } 180df8bae1dSRodney W. Grimes #endif 181df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 182df8bae1dSRodney W. Grimes if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred)) 183df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 184df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 185df8bae1dSRodney W. Grimes error = 0; 186df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 187df8bae1dSRodney W. Grimes #ifdef KTRACE 188df8bae1dSRodney W. Grimes if (ktriov != NULL) { 189df8bae1dSRodney W. Grimes if (error == 0) 190df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fdes, UIO_READ, ktriov, 191df8bae1dSRodney W. Grimes cnt, error); 192df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 193df8bae1dSRodney W. Grimes } 194df8bae1dSRodney W. Grimes #endif 195df8bae1dSRodney W. Grimes *retval = cnt; 196df8bae1dSRodney W. Grimes done: 197df8bae1dSRodney W. Grimes if (needfree) 198df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 199df8bae1dSRodney W. Grimes return (error); 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes 202df8bae1dSRodney W. Grimes /* 203df8bae1dSRodney W. Grimes * Write system call 204df8bae1dSRodney W. Grimes */ 205df8bae1dSRodney W. Grimes struct write_args { 206df8bae1dSRodney W. Grimes int fd; 207df8bae1dSRodney W. Grimes char *buf; 208df8bae1dSRodney W. Grimes u_int nbyte; 209df8bae1dSRodney W. Grimes }; 210df8bae1dSRodney W. Grimes write(p, uap, retval) 211df8bae1dSRodney W. Grimes struct proc *p; 212df8bae1dSRodney W. Grimes register struct write_args *uap; 213df8bae1dSRodney W. Grimes int *retval; 214df8bae1dSRodney W. Grimes { 215df8bae1dSRodney W. Grimes register struct file *fp; 216df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 217df8bae1dSRodney W. Grimes struct uio auio; 218df8bae1dSRodney W. Grimes struct iovec aiov; 219df8bae1dSRodney W. Grimes long cnt, error = 0; 220df8bae1dSRodney W. Grimes #ifdef KTRACE 221df8bae1dSRodney W. Grimes struct iovec ktriov; 222df8bae1dSRodney W. Grimes #endif 223df8bae1dSRodney W. Grimes 224df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 225df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 226df8bae1dSRodney W. Grimes (fp->f_flag & FWRITE) == 0) 227df8bae1dSRodney W. Grimes return (EBADF); 228df8bae1dSRodney W. Grimes aiov.iov_base = (caddr_t)uap->buf; 229df8bae1dSRodney W. Grimes aiov.iov_len = uap->nbyte; 230df8bae1dSRodney W. Grimes auio.uio_iov = &aiov; 231df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 232df8bae1dSRodney W. Grimes auio.uio_resid = uap->nbyte; 233df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 234df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 235df8bae1dSRodney W. Grimes auio.uio_procp = p; 236df8bae1dSRodney W. Grimes #ifdef KTRACE 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 239df8bae1dSRodney W. Grimes */ 240df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) 241df8bae1dSRodney W. Grimes ktriov = aiov; 242df8bae1dSRodney W. Grimes #endif 243df8bae1dSRodney W. Grimes cnt = uap->nbyte; 244df8bae1dSRodney W. Grimes if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) { 245df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 246df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 247df8bae1dSRodney W. Grimes error = 0; 248df8bae1dSRodney W. Grimes if (error == EPIPE) 249df8bae1dSRodney W. Grimes psignal(p, SIGPIPE); 250df8bae1dSRodney W. Grimes } 251df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 252df8bae1dSRodney W. Grimes #ifdef KTRACE 253df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO) && error == 0) 254df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, 255df8bae1dSRodney W. Grimes &ktriov, cnt, error); 256df8bae1dSRodney W. Grimes #endif 257df8bae1dSRodney W. Grimes *retval = cnt; 258df8bae1dSRodney W. Grimes return (error); 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * Gather write system call 263df8bae1dSRodney W. Grimes */ 264df8bae1dSRodney W. Grimes struct writev_args { 265df8bae1dSRodney W. Grimes int fd; 266df8bae1dSRodney W. Grimes struct iovec *iovp; 267df8bae1dSRodney W. Grimes u_int iovcnt; 268df8bae1dSRodney W. Grimes }; 269df8bae1dSRodney W. Grimes writev(p, uap, retval) 270df8bae1dSRodney W. Grimes struct proc *p; 271df8bae1dSRodney W. Grimes register struct writev_args *uap; 272df8bae1dSRodney W. Grimes int *retval; 273df8bae1dSRodney W. Grimes { 274df8bae1dSRodney W. Grimes register struct file *fp; 275df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 276df8bae1dSRodney W. Grimes struct uio auio; 277df8bae1dSRodney W. Grimes register struct iovec *iov; 278df8bae1dSRodney W. Grimes struct iovec *needfree; 279df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 280df8bae1dSRodney W. Grimes long i, cnt, error = 0; 281df8bae1dSRodney W. Grimes u_int iovlen; 282df8bae1dSRodney W. Grimes #ifdef KTRACE 283df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 284df8bae1dSRodney W. Grimes #endif 285df8bae1dSRodney W. Grimes 286df8bae1dSRodney W. Grimes if (((u_int)uap->fd) >= fdp->fd_nfiles || 287df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL || 288df8bae1dSRodney W. Grimes (fp->f_flag & FWRITE) == 0) 289df8bae1dSRodney W. Grimes return (EBADF); 290df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 291df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 292df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 293df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_MAXIOV) 294df8bae1dSRodney W. Grimes return (EINVAL); 295df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 296df8bae1dSRodney W. Grimes needfree = iov; 297df8bae1dSRodney W. Grimes } else { 298df8bae1dSRodney W. Grimes iov = aiov; 299df8bae1dSRodney W. Grimes needfree = NULL; 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes auio.uio_iov = iov; 302df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 303df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 304df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 305df8bae1dSRodney W. Grimes auio.uio_procp = p; 306df8bae1dSRodney W. Grimes if (error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)) 307df8bae1dSRodney W. Grimes goto done; 308df8bae1dSRodney W. Grimes auio.uio_resid = 0; 309df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 310df8bae1dSRodney W. Grimes if (iov->iov_len < 0) { 311df8bae1dSRodney W. Grimes error = EINVAL; 312df8bae1dSRodney W. Grimes goto done; 313df8bae1dSRodney W. Grimes } 314df8bae1dSRodney W. Grimes auio.uio_resid += iov->iov_len; 315df8bae1dSRodney W. Grimes if (auio.uio_resid < 0) { 316df8bae1dSRodney W. Grimes error = EINVAL; 317df8bae1dSRodney W. Grimes goto done; 318df8bae1dSRodney W. Grimes } 319df8bae1dSRodney W. Grimes iov++; 320df8bae1dSRodney W. Grimes } 321df8bae1dSRodney W. Grimes #ifdef KTRACE 322df8bae1dSRodney W. Grimes /* 323df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 324df8bae1dSRodney W. Grimes */ 325df8bae1dSRodney W. Grimes if (KTRPOINT(p, KTR_GENIO)) { 326df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 327df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes #endif 330df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 331df8bae1dSRodney W. Grimes if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) { 332df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 333df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 334df8bae1dSRodney W. Grimes error = 0; 335df8bae1dSRodney W. Grimes if (error == EPIPE) 336df8bae1dSRodney W. Grimes psignal(p, SIGPIPE); 337df8bae1dSRodney W. Grimes } 338df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 339df8bae1dSRodney W. Grimes #ifdef KTRACE 340df8bae1dSRodney W. Grimes if (ktriov != NULL) { 341df8bae1dSRodney W. Grimes if (error == 0) 342df8bae1dSRodney W. Grimes ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, 343df8bae1dSRodney W. Grimes ktriov, cnt, error); 344df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes #endif 347df8bae1dSRodney W. Grimes *retval = cnt; 348df8bae1dSRodney W. Grimes done: 349df8bae1dSRodney W. Grimes if (needfree) 350df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 351df8bae1dSRodney W. Grimes return (error); 352df8bae1dSRodney W. Grimes } 353df8bae1dSRodney W. Grimes 354df8bae1dSRodney W. Grimes /* 355df8bae1dSRodney W. Grimes * Ioctl system call 356df8bae1dSRodney W. Grimes */ 357df8bae1dSRodney W. Grimes struct ioctl_args { 358df8bae1dSRodney W. Grimes int fd; 359df8bae1dSRodney W. Grimes int com; 360df8bae1dSRodney W. Grimes caddr_t data; 361df8bae1dSRodney W. Grimes }; 362df8bae1dSRodney W. Grimes /* ARGSUSED */ 363df8bae1dSRodney W. Grimes ioctl(p, uap, retval) 364df8bae1dSRodney W. Grimes struct proc *p; 365df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 366df8bae1dSRodney W. Grimes int *retval; 367df8bae1dSRodney W. Grimes { 368df8bae1dSRodney W. Grimes register struct file *fp; 369df8bae1dSRodney W. Grimes register struct filedesc *fdp; 370df8bae1dSRodney W. Grimes register int com, error; 371df8bae1dSRodney W. Grimes register u_int size; 372df8bae1dSRodney W. Grimes caddr_t data, memp; 373df8bae1dSRodney W. Grimes int tmp; 374df8bae1dSRodney W. Grimes #define STK_PARAMS 128 375df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 376df8bae1dSRodney W. Grimes 377df8bae1dSRodney W. Grimes fdp = p->p_fd; 378df8bae1dSRodney W. Grimes if ((u_int)uap->fd >= fdp->fd_nfiles || 379df8bae1dSRodney W. Grimes (fp = fdp->fd_ofiles[uap->fd]) == NULL) 380df8bae1dSRodney W. Grimes return (EBADF); 381df8bae1dSRodney W. Grimes 382df8bae1dSRodney W. Grimes if ((fp->f_flag & (FREAD | FWRITE)) == 0) 383df8bae1dSRodney W. Grimes return (EBADF); 384df8bae1dSRodney W. Grimes 385df8bae1dSRodney W. Grimes switch (com = uap->com) { 386df8bae1dSRodney W. Grimes case FIONCLEX: 387df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 388df8bae1dSRodney W. Grimes return (0); 389df8bae1dSRodney W. Grimes case FIOCLEX: 390df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 391df8bae1dSRodney W. Grimes return (0); 392df8bae1dSRodney W. Grimes } 393df8bae1dSRodney W. Grimes 394df8bae1dSRodney W. Grimes /* 395df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 396df8bae1dSRodney W. Grimes * copied to/from the user's address space. 397df8bae1dSRodney W. Grimes */ 398df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 399df8bae1dSRodney W. Grimes if (size > IOCPARM_MAX) 400df8bae1dSRodney W. Grimes return (ENOTTY); 401df8bae1dSRodney W. Grimes memp = NULL; 402df8bae1dSRodney W. Grimes if (size > sizeof (stkbuf)) { 403df8bae1dSRodney W. Grimes memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 404df8bae1dSRodney W. Grimes data = memp; 405df8bae1dSRodney W. Grimes } else 406df8bae1dSRodney W. Grimes data = stkbuf; 407df8bae1dSRodney W. Grimes if (com&IOC_IN) { 408df8bae1dSRodney W. Grimes if (size) { 409df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 410df8bae1dSRodney W. Grimes if (error) { 411df8bae1dSRodney W. Grimes if (memp) 412df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 413df8bae1dSRodney W. Grimes return (error); 414df8bae1dSRodney W. Grimes } 415df8bae1dSRodney W. Grimes } else 416df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 417df8bae1dSRodney W. Grimes } else if ((com&IOC_OUT) && size) 418df8bae1dSRodney W. Grimes /* 419df8bae1dSRodney W. Grimes * Zero the buffer so the user always 420df8bae1dSRodney W. Grimes * gets back something deterministic. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes bzero(data, size); 423df8bae1dSRodney W. Grimes else if (com&IOC_VOID) 424df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes switch (com) { 427df8bae1dSRodney W. Grimes 428df8bae1dSRodney W. Grimes case FIONBIO: 429df8bae1dSRodney W. Grimes if (tmp = *(int *)data) 430df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 431df8bae1dSRodney W. Grimes else 432df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 433df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p); 434df8bae1dSRodney W. Grimes break; 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes case FIOASYNC: 437df8bae1dSRodney W. Grimes if (tmp = *(int *)data) 438df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 439df8bae1dSRodney W. Grimes else 440df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 441df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p); 442df8bae1dSRodney W. Grimes break; 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes case FIOSETOWN: 445df8bae1dSRodney W. Grimes tmp = *(int *)data; 446df8bae1dSRodney W. Grimes if (fp->f_type == DTYPE_SOCKET) { 447df8bae1dSRodney W. Grimes ((struct socket *)fp->f_data)->so_pgid = tmp; 448df8bae1dSRodney W. Grimes error = 0; 449df8bae1dSRodney W. Grimes break; 450df8bae1dSRodney W. Grimes } 451df8bae1dSRodney W. Grimes if (tmp <= 0) { 452df8bae1dSRodney W. Grimes tmp = -tmp; 453df8bae1dSRodney W. Grimes } else { 454df8bae1dSRodney W. Grimes struct proc *p1 = pfind(tmp); 455df8bae1dSRodney W. Grimes if (p1 == 0) { 456df8bae1dSRodney W. Grimes error = ESRCH; 457df8bae1dSRodney W. Grimes break; 458df8bae1dSRodney W. Grimes } 459df8bae1dSRodney W. Grimes tmp = p1->p_pgrp->pg_id; 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl) 462df8bae1dSRodney W. Grimes (fp, (int)TIOCSPGRP, (caddr_t)&tmp, p); 463df8bae1dSRodney W. Grimes break; 464df8bae1dSRodney W. Grimes 465df8bae1dSRodney W. Grimes case FIOGETOWN: 466df8bae1dSRodney W. Grimes if (fp->f_type == DTYPE_SOCKET) { 467df8bae1dSRodney W. Grimes error = 0; 468df8bae1dSRodney W. Grimes *(int *)data = ((struct socket *)fp->f_data)->so_pgid; 469df8bae1dSRodney W. Grimes break; 470df8bae1dSRodney W. Grimes } 471df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, (int)TIOCGPGRP, data, p); 472df8bae1dSRodney W. Grimes *(int *)data = -*(int *)data; 473df8bae1dSRodney W. Grimes break; 474df8bae1dSRodney W. Grimes 475df8bae1dSRodney W. Grimes default: 476df8bae1dSRodney W. Grimes error = (*fp->f_ops->fo_ioctl)(fp, com, data, p); 477df8bae1dSRodney W. Grimes /* 478df8bae1dSRodney W. Grimes * Copy any data to user, size was 479df8bae1dSRodney W. Grimes * already set and checked above. 480df8bae1dSRodney W. Grimes */ 481df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 482df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 483df8bae1dSRodney W. Grimes break; 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes if (memp) 486df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 487df8bae1dSRodney W. Grimes return (error); 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes int selwait, nselcoll; 491df8bae1dSRodney W. Grimes 492df8bae1dSRodney W. Grimes /* 493df8bae1dSRodney W. Grimes * Select system call. 494df8bae1dSRodney W. Grimes */ 495df8bae1dSRodney W. Grimes struct select_args { 496df8bae1dSRodney W. Grimes u_int nd; 497df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 498df8bae1dSRodney W. Grimes struct timeval *tv; 499df8bae1dSRodney W. Grimes }; 500df8bae1dSRodney W. Grimes select(p, uap, retval) 501df8bae1dSRodney W. Grimes register struct proc *p; 502df8bae1dSRodney W. Grimes register struct select_args *uap; 503df8bae1dSRodney W. Grimes int *retval; 504df8bae1dSRodney W. Grimes { 505df8bae1dSRodney W. Grimes fd_set ibits[3], obits[3]; 506df8bae1dSRodney W. Grimes struct timeval atv; 507df8bae1dSRodney W. Grimes int s, ncoll, error = 0, timo; 508df8bae1dSRodney W. Grimes u_int ni; 509df8bae1dSRodney W. Grimes 510df8bae1dSRodney W. Grimes bzero((caddr_t)ibits, sizeof(ibits)); 511df8bae1dSRodney W. Grimes bzero((caddr_t)obits, sizeof(obits)); 512df8bae1dSRodney W. Grimes if (uap->nd > FD_SETSIZE) 513df8bae1dSRodney W. Grimes return (EINVAL); 514df8bae1dSRodney W. Grimes if (uap->nd > p->p_fd->fd_nfiles) 515df8bae1dSRodney W. Grimes uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 516df8bae1dSRodney W. Grimes ni = howmany(uap->nd, NFDBITS) * sizeof(fd_mask); 517df8bae1dSRodney W. Grimes 518df8bae1dSRodney W. Grimes #define getbits(name, x) \ 519df8bae1dSRodney W. Grimes if (uap->name && \ 520df8bae1dSRodney W. Grimes (error = copyin((caddr_t)uap->name, (caddr_t)&ibits[x], ni))) \ 521df8bae1dSRodney W. Grimes goto done; 522df8bae1dSRodney W. Grimes getbits(in, 0); 523df8bae1dSRodney W. Grimes getbits(ou, 1); 524df8bae1dSRodney W. Grimes getbits(ex, 2); 525df8bae1dSRodney W. Grimes #undef getbits 526df8bae1dSRodney W. Grimes 527df8bae1dSRodney W. Grimes if (uap->tv) { 528df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 529df8bae1dSRodney W. Grimes sizeof (atv)); 530df8bae1dSRodney W. Grimes if (error) 531df8bae1dSRodney W. Grimes goto done; 532df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 533df8bae1dSRodney W. Grimes error = EINVAL; 534df8bae1dSRodney W. Grimes goto done; 535df8bae1dSRodney W. Grimes } 536df8bae1dSRodney W. Grimes s = splclock(); 537df8bae1dSRodney W. Grimes timevaladd(&atv, (struct timeval *)&time); 538df8bae1dSRodney W. Grimes timo = hzto(&atv); 539df8bae1dSRodney W. Grimes /* 540df8bae1dSRodney W. Grimes * Avoid inadvertently sleeping forever. 541df8bae1dSRodney W. Grimes */ 542df8bae1dSRodney W. Grimes if (timo == 0) 543df8bae1dSRodney W. Grimes timo = 1; 544df8bae1dSRodney W. Grimes splx(s); 545df8bae1dSRodney W. Grimes } else 546df8bae1dSRodney W. Grimes timo = 0; 547df8bae1dSRodney W. Grimes retry: 548df8bae1dSRodney W. Grimes ncoll = nselcoll; 549df8bae1dSRodney W. Grimes p->p_flag |= P_SELECT; 550df8bae1dSRodney W. Grimes error = selscan(p, ibits, obits, uap->nd, retval); 551df8bae1dSRodney W. Grimes if (error || *retval) 552df8bae1dSRodney W. Grimes goto done; 553df8bae1dSRodney W. Grimes s = splhigh(); 554df8bae1dSRodney W. Grimes /* this should be timercmp(&time, &atv, >=) */ 555df8bae1dSRodney W. Grimes if (uap->tv && (time.tv_sec > atv.tv_sec || 556df8bae1dSRodney W. Grimes time.tv_sec == atv.tv_sec && time.tv_usec >= atv.tv_usec)) { 557df8bae1dSRodney W. Grimes splx(s); 558df8bae1dSRodney W. Grimes goto done; 559df8bae1dSRodney W. Grimes } 560df8bae1dSRodney W. Grimes if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 561df8bae1dSRodney W. Grimes splx(s); 562df8bae1dSRodney W. Grimes goto retry; 563df8bae1dSRodney W. Grimes } 564df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 565df8bae1dSRodney W. Grimes error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo); 566df8bae1dSRodney W. Grimes splx(s); 567df8bae1dSRodney W. Grimes if (error == 0) 568df8bae1dSRodney W. Grimes goto retry; 569df8bae1dSRodney W. Grimes done: 570df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 571df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 572df8bae1dSRodney W. Grimes if (error == ERESTART) 573df8bae1dSRodney W. Grimes error = EINTR; 574df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 575df8bae1dSRodney W. Grimes error = 0; 576df8bae1dSRodney W. Grimes #define putbits(name, x) \ 577df8bae1dSRodney W. Grimes if (uap->name && \ 578df8bae1dSRodney W. Grimes (error2 = copyout((caddr_t)&obits[x], (caddr_t)uap->name, ni))) \ 579df8bae1dSRodney W. Grimes error = error2; 580df8bae1dSRodney W. Grimes if (error == 0) { 581df8bae1dSRodney W. Grimes int error2; 582df8bae1dSRodney W. Grimes 583df8bae1dSRodney W. Grimes putbits(in, 0); 584df8bae1dSRodney W. Grimes putbits(ou, 1); 585df8bae1dSRodney W. Grimes putbits(ex, 2); 586df8bae1dSRodney W. Grimes #undef putbits 587df8bae1dSRodney W. Grimes } 588df8bae1dSRodney W. Grimes return (error); 589df8bae1dSRodney W. Grimes } 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes selscan(p, ibits, obits, nfd, retval) 592df8bae1dSRodney W. Grimes struct proc *p; 593df8bae1dSRodney W. Grimes fd_set *ibits, *obits; 594df8bae1dSRodney W. Grimes int nfd, *retval; 595df8bae1dSRodney W. Grimes { 596df8bae1dSRodney W. Grimes register struct filedesc *fdp = p->p_fd; 597df8bae1dSRodney W. Grimes register int msk, i, j, fd; 598df8bae1dSRodney W. Grimes register fd_mask bits; 599df8bae1dSRodney W. Grimes struct file *fp; 600df8bae1dSRodney W. Grimes int n = 0; 601df8bae1dSRodney W. Grimes static int flag[3] = { FREAD, FWRITE, 0 }; 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 604df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 605df8bae1dSRodney W. Grimes bits = ibits[msk].fds_bits[i/NFDBITS]; 606df8bae1dSRodney W. Grimes while ((j = ffs(bits)) && (fd = i + --j) < nfd) { 607df8bae1dSRodney W. Grimes bits &= ~(1 << j); 608df8bae1dSRodney W. Grimes fp = fdp->fd_ofiles[fd]; 609df8bae1dSRodney W. Grimes if (fp == NULL) 610df8bae1dSRodney W. Grimes return (EBADF); 611df8bae1dSRodney W. Grimes if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) { 612df8bae1dSRodney W. Grimes FD_SET(fd, &obits[msk]); 613df8bae1dSRodney W. Grimes n++; 614df8bae1dSRodney W. Grimes } 615df8bae1dSRodney W. Grimes } 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes *retval = n; 619df8bae1dSRodney W. Grimes return (0); 620df8bae1dSRodney W. Grimes } 621df8bae1dSRodney W. Grimes 622df8bae1dSRodney W. Grimes /*ARGSUSED*/ 623df8bae1dSRodney W. Grimes seltrue(dev, flag, p) 624df8bae1dSRodney W. Grimes dev_t dev; 625df8bae1dSRodney W. Grimes int flag; 626df8bae1dSRodney W. Grimes struct proc *p; 627df8bae1dSRodney W. Grimes { 628df8bae1dSRodney W. Grimes 629df8bae1dSRodney W. Grimes return (1); 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes 632df8bae1dSRodney W. Grimes /* 633df8bae1dSRodney W. Grimes * Record a select request. 634df8bae1dSRodney W. Grimes */ 635df8bae1dSRodney W. Grimes void 636df8bae1dSRodney W. Grimes selrecord(selector, sip) 637df8bae1dSRodney W. Grimes struct proc *selector; 638df8bae1dSRodney W. Grimes struct selinfo *sip; 639df8bae1dSRodney W. Grimes { 640df8bae1dSRodney W. Grimes struct proc *p; 641df8bae1dSRodney W. Grimes pid_t mypid; 642df8bae1dSRodney W. Grimes 643df8bae1dSRodney W. Grimes mypid = selector->p_pid; 644df8bae1dSRodney W. Grimes if (sip->si_pid == mypid) 645df8bae1dSRodney W. Grimes return; 646df8bae1dSRodney W. Grimes if (sip->si_pid && (p = pfind(sip->si_pid)) && 647df8bae1dSRodney W. Grimes p->p_wchan == (caddr_t)&selwait) 648df8bae1dSRodney W. Grimes sip->si_flags |= SI_COLL; 649df8bae1dSRodney W. Grimes else 650df8bae1dSRodney W. Grimes sip->si_pid = mypid; 651df8bae1dSRodney W. Grimes } 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes /* 654df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 655df8bae1dSRodney W. Grimes */ 656df8bae1dSRodney W. Grimes void 657df8bae1dSRodney W. Grimes selwakeup(sip) 658df8bae1dSRodney W. Grimes register struct selinfo *sip; 659df8bae1dSRodney W. Grimes { 660df8bae1dSRodney W. Grimes register struct proc *p; 661df8bae1dSRodney W. Grimes int s; 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes if (sip->si_pid == 0) 664df8bae1dSRodney W. Grimes return; 665df8bae1dSRodney W. Grimes if (sip->si_flags & SI_COLL) { 666df8bae1dSRodney W. Grimes nselcoll++; 667df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 668df8bae1dSRodney W. Grimes wakeup((caddr_t)&selwait); 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes p = pfind(sip->si_pid); 671df8bae1dSRodney W. Grimes sip->si_pid = 0; 672df8bae1dSRodney W. Grimes if (p != NULL) { 673df8bae1dSRodney W. Grimes s = splhigh(); 674df8bae1dSRodney W. Grimes if (p->p_wchan == (caddr_t)&selwait) { 675df8bae1dSRodney W. Grimes if (p->p_stat == SSLEEP) 676df8bae1dSRodney W. Grimes setrunnable(p); 677df8bae1dSRodney W. Grimes else 678df8bae1dSRodney W. Grimes unsleep(p); 679df8bae1dSRodney W. Grimes } else if (p->p_flag & P_SELECT) 680df8bae1dSRodney W. Grimes p->p_flag &= ~P_SELECT; 681df8bae1dSRodney W. Grimes splx(s); 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes } 684