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