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 39c3aac50fSPeter Wemm * $FreeBSD$ 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> 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/signalvar.h> 53df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 54df8bae1dSRodney W. Grimes #include <sys/uio.h> 55df8bae1dSRodney W. Grimes #include <sys/kernel.h> 56df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5742d11757SPeter Wemm #include <sys/poll.h> 5889b71647SPeter Wemm #include <sys/resourcevar.h> 590a2c3d48SGarrett Wollman #include <sys/selinfo.h> 608cb96f20SPeter Wemm #include <sys/sysctl.h> 6142d11757SPeter Wemm #include <sys/sysent.h> 62279d7226SMatthew Dillon #include <sys/bio.h> 63279d7226SMatthew Dillon #include <sys/buf.h> 64265fc98fSSeigo Tanimura #include <sys/condvar.h> 65df8bae1dSRodney W. Grimes #ifdef KTRACE 66df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 67df8bae1dSRodney W. Grimes #endif 68279d7226SMatthew Dillon #include <vm/vm.h> 69279d7226SMatthew Dillon #include <vm/vm_page.h> 70df8bae1dSRodney W. Grimes 71069e9bc1SDoug Rabson #include <machine/limits.h> 72069e9bc1SDoug Rabson 73a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 75a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7655166637SPoul-Henning Kamp 77bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 78bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 79bbbb04ceSAlfred Perlstein static int dofileread(struct thread *, struct file *, int, void *, 80bbbb04ceSAlfred Perlstein size_t, off_t, int); 81bbbb04ceSAlfred Perlstein static int dofilewrite(struct thread *, struct file *, int, 82bbbb04ceSAlfred Perlstein const void *, size_t, off_t, int); 838fe387abSDmitrij Tejblum 84df8bae1dSRodney W. Grimes /* 85df8bae1dSRodney W. Grimes * Read system call. 86df8bae1dSRodney W. Grimes */ 87d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 88df8bae1dSRodney W. Grimes struct read_args { 89df8bae1dSRodney W. Grimes int fd; 90134e06feSBruce Evans void *buf; 91134e06feSBruce Evans size_t nbyte; 92df8bae1dSRodney W. Grimes }; 93d2d3e875SBruce Evans #endif 94ad2edad9SMatthew Dillon /* 95ad2edad9SMatthew Dillon * MPSAFE 96ad2edad9SMatthew Dillon */ 9726f9a767SRodney W. Grimes int 98b40ce416SJulian Elischer read(td, uap) 99b40ce416SJulian Elischer struct thread *td; 100b064d43dSMatthew Dillon struct read_args *uap; 101df8bae1dSRodney W. Grimes { 102b064d43dSMatthew Dillon struct file *fp; 103279d7226SMatthew Dillon int error; 104df8bae1dSRodney W. Grimes 105b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) == 0) { 106b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, 107ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 108b40ce416SJulian Elischer fdrop(fp, td); 109ad2edad9SMatthew Dillon } 110279d7226SMatthew Dillon return(error); 111df8bae1dSRodney W. Grimes } 112df8bae1dSRodney W. Grimes 113df8bae1dSRodney W. Grimes /* 1148fe387abSDmitrij Tejblum * Pread system call 1154160ccd9SAlan Cox */ 1164160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1174160ccd9SAlan Cox struct pread_args { 1184160ccd9SAlan Cox int fd; 1194160ccd9SAlan Cox void *buf; 1204160ccd9SAlan Cox size_t nbyte; 1218fe387abSDmitrij Tejblum int pad; 1224160ccd9SAlan Cox off_t offset; 1234160ccd9SAlan Cox }; 1244160ccd9SAlan Cox #endif 125ad2edad9SMatthew Dillon /* 126ad2edad9SMatthew Dillon * MPSAFE 127ad2edad9SMatthew Dillon */ 1284160ccd9SAlan Cox int 129b40ce416SJulian Elischer pread(td, uap) 130b40ce416SJulian Elischer struct thread *td; 131b064d43dSMatthew Dillon struct pread_args *uap; 1324160ccd9SAlan Cox { 133b064d43dSMatthew Dillon struct file *fp; 134279d7226SMatthew Dillon int error; 1358fe387abSDmitrij Tejblum 13697fa4397SAlfred Perlstein if ((error = fget_read(td, uap->fd, &fp)) != 0) 13797fa4397SAlfred Perlstein return (error); 138426da3bcSAlfred Perlstein if (fp->f_type != DTYPE_VNODE) { 139b064d43dSMatthew Dillon error = ESPIPE; 140426da3bcSAlfred Perlstein } else { 141426da3bcSAlfred Perlstein error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte, 142426da3bcSAlfred Perlstein uap->offset, FOF_OFFSET); 143b064d43dSMatthew Dillon } 144b40ce416SJulian Elischer fdrop(fp, td); 145279d7226SMatthew Dillon return(error); 1468fe387abSDmitrij Tejblum } 1478fe387abSDmitrij Tejblum 1488fe387abSDmitrij Tejblum /* 1498fe387abSDmitrij Tejblum * Code common for read and pread 1508fe387abSDmitrij Tejblum */ 1518fe387abSDmitrij Tejblum int 152b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 153b40ce416SJulian Elischer struct thread *td; 1548fe387abSDmitrij Tejblum struct file *fp; 1558fe387abSDmitrij Tejblum int fd, flags; 1568fe387abSDmitrij Tejblum void *buf; 1578fe387abSDmitrij Tejblum size_t nbyte; 1588fe387abSDmitrij Tejblum off_t offset; 1598fe387abSDmitrij Tejblum { 1604160ccd9SAlan Cox struct uio auio; 1614160ccd9SAlan Cox struct iovec aiov; 1624160ccd9SAlan Cox long cnt, error = 0; 1634160ccd9SAlan Cox #ifdef KTRACE 1644160ccd9SAlan Cox struct iovec ktriov; 16542ebfbf2SBrian Feldman struct uio ktruio; 1663c89e357SBrian Feldman int didktr = 0; 1674160ccd9SAlan Cox #endif 1684160ccd9SAlan Cox 1698fe387abSDmitrij Tejblum aiov.iov_base = (caddr_t)buf; 1708fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 1714160ccd9SAlan Cox auio.uio_iov = &aiov; 1724160ccd9SAlan Cox auio.uio_iovcnt = 1; 1738fe387abSDmitrij Tejblum auio.uio_offset = offset; 1748fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 1754160ccd9SAlan Cox return (EINVAL); 1768fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 1774160ccd9SAlan Cox auio.uio_rw = UIO_READ; 1784160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 179b40ce416SJulian Elischer auio.uio_td = td; 1804160ccd9SAlan Cox #ifdef KTRACE 1814160ccd9SAlan Cox /* 1824160ccd9SAlan Cox * if tracing, save a copy of iovec 1834160ccd9SAlan Cox */ 184b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 1854160ccd9SAlan Cox ktriov = aiov; 18642ebfbf2SBrian Feldman ktruio = auio; 1873c89e357SBrian Feldman didktr = 1; 18842ebfbf2SBrian Feldman } 1894160ccd9SAlan Cox #endif 1908fe387abSDmitrij Tejblum cnt = nbyte; 191279d7226SMatthew Dillon 192b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) { 1934160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 1944160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 1954160ccd9SAlan Cox error = 0; 196279d7226SMatthew Dillon } 1974160ccd9SAlan Cox cnt -= auio.uio_resid; 1984160ccd9SAlan Cox #ifdef KTRACE 1993c89e357SBrian Feldman if (didktr && error == 0) { 20042ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 20142ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 202b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_READ, &ktruio, error); 20342ebfbf2SBrian Feldman } 2044160ccd9SAlan Cox #endif 205b40ce416SJulian Elischer td->td_retval[0] = cnt; 2064160ccd9SAlan Cox return (error); 2074160ccd9SAlan Cox } 2084160ccd9SAlan Cox 2094160ccd9SAlan Cox /* 210df8bae1dSRodney W. Grimes * Scatter read system call. 211df8bae1dSRodney W. Grimes */ 212d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 213df8bae1dSRodney W. Grimes struct readv_args { 2147147b19dSBruce Evans int fd; 215df8bae1dSRodney W. Grimes struct iovec *iovp; 216df8bae1dSRodney W. Grimes u_int iovcnt; 217df8bae1dSRodney W. Grimes }; 218d2d3e875SBruce Evans #endif 219ad2edad9SMatthew Dillon /* 220ad2edad9SMatthew Dillon * MPSAFE 221ad2edad9SMatthew Dillon */ 22226f9a767SRodney W. Grimes int 223b40ce416SJulian Elischer readv(td, uap) 224b40ce416SJulian Elischer struct thread *td; 225b064d43dSMatthew Dillon struct readv_args *uap; 226df8bae1dSRodney W. Grimes { 227b064d43dSMatthew Dillon struct file *fp; 228df8bae1dSRodney W. Grimes struct uio auio; 229b064d43dSMatthew Dillon struct iovec *iov; 230df8bae1dSRodney W. Grimes struct iovec *needfree; 231df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 232df8bae1dSRodney W. Grimes long i, cnt, error = 0; 233df8bae1dSRodney W. Grimes u_int iovlen; 234df8bae1dSRodney W. Grimes #ifdef KTRACE 235df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 23642ebfbf2SBrian Feldman struct uio ktruio; 237df8bae1dSRodney W. Grimes #endif 238ad2edad9SMatthew Dillon mtx_lock(&Giant); 239df8bae1dSRodney W. Grimes 240b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) != 0) 241ad2edad9SMatthew Dillon goto done2; 242df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 243df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 244df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 245ad2edad9SMatthew Dillon if (uap->iovcnt > UIO_MAXIOV) { 246ad2edad9SMatthew Dillon error = EINVAL; 247ad2edad9SMatthew Dillon goto done2; 248ad2edad9SMatthew Dillon } 249df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 250df8bae1dSRodney W. Grimes needfree = iov; 251df8bae1dSRodney W. Grimes } else { 252df8bae1dSRodney W. Grimes iov = aiov; 253df8bae1dSRodney W. Grimes needfree = NULL; 254df8bae1dSRodney W. Grimes } 255df8bae1dSRodney W. Grimes auio.uio_iov = iov; 256df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 257df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 258df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 259b40ce416SJulian Elischer auio.uio_td = td; 2602c1011f7SJohn Dyson auio.uio_offset = -1; 261bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 262df8bae1dSRodney W. Grimes goto done; 263df8bae1dSRodney W. Grimes auio.uio_resid = 0; 264df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 265069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 266df8bae1dSRodney W. Grimes error = EINVAL; 267df8bae1dSRodney W. Grimes goto done; 268df8bae1dSRodney W. Grimes } 269069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 270df8bae1dSRodney W. Grimes iov++; 271df8bae1dSRodney W. Grimes } 272df8bae1dSRodney W. Grimes #ifdef KTRACE 273df8bae1dSRodney W. Grimes /* 274df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 275df8bae1dSRodney W. Grimes */ 276b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 277df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 278df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 27942ebfbf2SBrian Feldman ktruio = auio; 280df8bae1dSRodney W. Grimes } 281df8bae1dSRodney W. Grimes #endif 282df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 283b40ce416SJulian Elischer if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) { 284df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 285df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 286df8bae1dSRodney W. Grimes error = 0; 287279d7226SMatthew Dillon } 288df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 289df8bae1dSRodney W. Grimes #ifdef KTRACE 290df8bae1dSRodney W. Grimes if (ktriov != NULL) { 29142ebfbf2SBrian Feldman if (error == 0) { 29242ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 29342ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 294b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_READ, &ktruio, 29542ebfbf2SBrian Feldman error); 29642ebfbf2SBrian Feldman } 297df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 298df8bae1dSRodney W. Grimes } 299df8bae1dSRodney W. Grimes #endif 300b40ce416SJulian Elischer td->td_retval[0] = cnt; 301df8bae1dSRodney W. Grimes done: 302b40ce416SJulian Elischer fdrop(fp, td); 303df8bae1dSRodney W. Grimes if (needfree) 304df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 305ad2edad9SMatthew Dillon done2: 306ad2edad9SMatthew Dillon mtx_unlock(&Giant); 307df8bae1dSRodney W. Grimes return (error); 308df8bae1dSRodney W. Grimes } 309df8bae1dSRodney W. Grimes 310df8bae1dSRodney W. Grimes /* 311df8bae1dSRodney W. Grimes * Write system call 312df8bae1dSRodney W. Grimes */ 313d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 314df8bae1dSRodney W. Grimes struct write_args { 315df8bae1dSRodney W. Grimes int fd; 316134e06feSBruce Evans const void *buf; 317134e06feSBruce Evans size_t nbyte; 318df8bae1dSRodney W. Grimes }; 319d2d3e875SBruce Evans #endif 320ad2edad9SMatthew Dillon /* 321ad2edad9SMatthew Dillon * MPSAFE 322ad2edad9SMatthew Dillon */ 32326f9a767SRodney W. Grimes int 324b40ce416SJulian Elischer write(td, uap) 325b40ce416SJulian Elischer struct thread *td; 326b064d43dSMatthew Dillon struct write_args *uap; 327df8bae1dSRodney W. Grimes { 328b064d43dSMatthew Dillon struct file *fp; 329279d7226SMatthew Dillon int error; 330df8bae1dSRodney W. Grimes 331b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 332b40ce416SJulian Elischer error = dofilewrite(td, fp, uap->fd, uap->buf, uap->nbyte, 333ad2edad9SMatthew Dillon (off_t)-1, 0); 334b40ce416SJulian Elischer fdrop(fp, td); 335ad2edad9SMatthew Dillon } else { 336b064d43dSMatthew Dillon error = EBADF; /* XXX this can't be right */ 337ad2edad9SMatthew Dillon } 338279d7226SMatthew Dillon return(error); 339df8bae1dSRodney W. Grimes } 340df8bae1dSRodney W. Grimes 341df8bae1dSRodney W. Grimes /* 3428fe387abSDmitrij Tejblum * Pwrite system call 3434160ccd9SAlan Cox */ 3444160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3454160ccd9SAlan Cox struct pwrite_args { 3464160ccd9SAlan Cox int fd; 3474160ccd9SAlan Cox const void *buf; 3484160ccd9SAlan Cox size_t nbyte; 3498fe387abSDmitrij Tejblum int pad; 3504160ccd9SAlan Cox off_t offset; 3514160ccd9SAlan Cox }; 3524160ccd9SAlan Cox #endif 353ad2edad9SMatthew Dillon /* 354ad2edad9SMatthew Dillon * MPSAFE 355ad2edad9SMatthew Dillon */ 3564160ccd9SAlan Cox int 357b40ce416SJulian Elischer pwrite(td, uap) 358b40ce416SJulian Elischer struct thread *td; 359b064d43dSMatthew Dillon struct pwrite_args *uap; 3604160ccd9SAlan Cox { 361b064d43dSMatthew Dillon struct file *fp; 362279d7226SMatthew Dillon int error; 3638fe387abSDmitrij Tejblum 364b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) == 0) { 365aa11a498SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) { 366b064d43dSMatthew Dillon error = dofilewrite(td, fp, uap->fd, uap->buf, 367b064d43dSMatthew Dillon uap->nbyte, uap->offset, FOF_OFFSET); 368b064d43dSMatthew Dillon } else { 369279d7226SMatthew Dillon error = ESPIPE; 370b064d43dSMatthew Dillon } 371b40ce416SJulian Elischer fdrop(fp, td); 372279d7226SMatthew Dillon } else { 373b064d43dSMatthew Dillon error = EBADF; /* this can't be right */ 374ad2edad9SMatthew Dillon } 375279d7226SMatthew Dillon return(error); 3768fe387abSDmitrij Tejblum } 3778fe387abSDmitrij Tejblum 3788fe387abSDmitrij Tejblum static int 379b40ce416SJulian Elischer dofilewrite(td, fp, fd, buf, nbyte, offset, flags) 380b40ce416SJulian Elischer struct thread *td; 3818fe387abSDmitrij Tejblum struct file *fp; 3828fe387abSDmitrij Tejblum int fd, flags; 3838fe387abSDmitrij Tejblum const void *buf; 3848fe387abSDmitrij Tejblum size_t nbyte; 3858fe387abSDmitrij Tejblum off_t offset; 3868fe387abSDmitrij Tejblum { 3874160ccd9SAlan Cox struct uio auio; 3884160ccd9SAlan Cox struct iovec aiov; 3894160ccd9SAlan Cox long cnt, error = 0; 3904160ccd9SAlan Cox #ifdef KTRACE 3914160ccd9SAlan Cox struct iovec ktriov; 39242ebfbf2SBrian Feldman struct uio ktruio; 3933c89e357SBrian Feldman int didktr = 0; 3944160ccd9SAlan Cox #endif 3954160ccd9SAlan Cox 396b31ae1adSPeter Wemm aiov.iov_base = (void *)(uintptr_t)buf; 3978fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 3984160ccd9SAlan Cox auio.uio_iov = &aiov; 3994160ccd9SAlan Cox auio.uio_iovcnt = 1; 4008fe387abSDmitrij Tejblum auio.uio_offset = offset; 4018fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 4024160ccd9SAlan Cox return (EINVAL); 4038fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 4044160ccd9SAlan Cox auio.uio_rw = UIO_WRITE; 4054160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 406b40ce416SJulian Elischer auio.uio_td = td; 4074160ccd9SAlan Cox #ifdef KTRACE 4084160ccd9SAlan Cox /* 40942ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 4104160ccd9SAlan Cox */ 411b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 4124160ccd9SAlan Cox ktriov = aiov; 41342ebfbf2SBrian Feldman ktruio = auio; 4143c89e357SBrian Feldman didktr = 1; 41542ebfbf2SBrian Feldman } 4164160ccd9SAlan Cox #endif 4178fe387abSDmitrij Tejblum cnt = nbyte; 418c6ab5768SAlfred Perlstein if (fp->f_type == DTYPE_VNODE) 419279d7226SMatthew Dillon bwillwrite(); 420b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) { 4214160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4224160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4234160ccd9SAlan Cox error = 0; 42419eb87d2SJohn Baldwin if (error == EPIPE) { 425b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 426b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 427b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 42819eb87d2SJohn Baldwin } 4294160ccd9SAlan Cox } 4304160ccd9SAlan Cox cnt -= auio.uio_resid; 4314160ccd9SAlan Cox #ifdef KTRACE 4323c89e357SBrian Feldman if (didktr && error == 0) { 43342ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 43442ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 435b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, fd, UIO_WRITE, &ktruio, error); 43642ebfbf2SBrian Feldman } 4374160ccd9SAlan Cox #endif 438b40ce416SJulian Elischer td->td_retval[0] = cnt; 4394160ccd9SAlan Cox return (error); 4404160ccd9SAlan Cox } 4414160ccd9SAlan Cox 4424160ccd9SAlan Cox /* 443df8bae1dSRodney W. Grimes * Gather write system call 444df8bae1dSRodney W. Grimes */ 445d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 446df8bae1dSRodney W. Grimes struct writev_args { 447df8bae1dSRodney W. Grimes int fd; 448df8bae1dSRodney W. Grimes struct iovec *iovp; 449df8bae1dSRodney W. Grimes u_int iovcnt; 450df8bae1dSRodney W. Grimes }; 451d2d3e875SBruce Evans #endif 452ad2edad9SMatthew Dillon /* 453ad2edad9SMatthew Dillon * MPSAFE 454ad2edad9SMatthew Dillon */ 45526f9a767SRodney W. Grimes int 456b40ce416SJulian Elischer writev(td, uap) 457b40ce416SJulian Elischer struct thread *td; 458df8bae1dSRodney W. Grimes register struct writev_args *uap; 459df8bae1dSRodney W. Grimes { 460b064d43dSMatthew Dillon struct file *fp; 461df8bae1dSRodney W. Grimes struct uio auio; 462df8bae1dSRodney W. Grimes register struct iovec *iov; 463df8bae1dSRodney W. Grimes struct iovec *needfree; 464df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 465df8bae1dSRodney W. Grimes long i, cnt, error = 0; 466df8bae1dSRodney W. Grimes u_int iovlen; 467df8bae1dSRodney W. Grimes #ifdef KTRACE 468df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 46942ebfbf2SBrian Feldman struct uio ktruio; 470df8bae1dSRodney W. Grimes #endif 471df8bae1dSRodney W. Grimes 472ad2edad9SMatthew Dillon mtx_lock(&Giant); 473b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) != 0) { 474ad2edad9SMatthew Dillon error = EBADF; 475ad2edad9SMatthew Dillon goto done2; 476ad2edad9SMatthew Dillon } 477df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 478df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 479df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 4801aa3e7ddSBrian Feldman if (uap->iovcnt > UIO_MAXIOV) { 4811aa3e7ddSBrian Feldman needfree = NULL; 4821aa3e7ddSBrian Feldman error = EINVAL; 4831aa3e7ddSBrian Feldman goto done; 4841aa3e7ddSBrian Feldman } 485df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 486df8bae1dSRodney W. Grimes needfree = iov; 487df8bae1dSRodney W. Grimes } else { 488df8bae1dSRodney W. Grimes iov = aiov; 489df8bae1dSRodney W. Grimes needfree = NULL; 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes auio.uio_iov = iov; 492df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 493df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 494df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 495b40ce416SJulian Elischer auio.uio_td = td; 4962c1011f7SJohn Dyson auio.uio_offset = -1; 497bb56ec4aSPoul-Henning Kamp if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))) 498df8bae1dSRodney W. Grimes goto done; 499df8bae1dSRodney W. Grimes auio.uio_resid = 0; 500df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 501069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 502df8bae1dSRodney W. Grimes error = EINVAL; 503df8bae1dSRodney W. Grimes goto done; 504df8bae1dSRodney W. Grimes } 505069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 506df8bae1dSRodney W. Grimes iov++; 507df8bae1dSRodney W. Grimes } 508df8bae1dSRodney W. Grimes #ifdef KTRACE 509df8bae1dSRodney W. Grimes /* 51042ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 511df8bae1dSRodney W. Grimes */ 512b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_GENIO)) { 513df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 514df8bae1dSRodney W. Grimes bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen); 51542ebfbf2SBrian Feldman ktruio = auio; 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes #endif 518df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 519a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5209440653dSMatthew Dillon bwillwrite(); 521b40ce416SJulian Elischer if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) { 522df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 523df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 524df8bae1dSRodney W. Grimes error = 0; 52519eb87d2SJohn Baldwin if (error == EPIPE) { 526b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 527b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 528b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 52919eb87d2SJohn Baldwin } 530df8bae1dSRodney W. Grimes } 531df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 532df8bae1dSRodney W. Grimes #ifdef KTRACE 533df8bae1dSRodney W. Grimes if (ktriov != NULL) { 53442ebfbf2SBrian Feldman if (error == 0) { 53542ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 53642ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 537b40ce416SJulian Elischer ktrgenio(td->td_proc->p_tracep, uap->fd, UIO_WRITE, &ktruio, 53842ebfbf2SBrian Feldman error); 53942ebfbf2SBrian Feldman } 540df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes #endif 543b40ce416SJulian Elischer td->td_retval[0] = cnt; 544df8bae1dSRodney W. Grimes done: 545b40ce416SJulian Elischer fdrop(fp, td); 546df8bae1dSRodney W. Grimes if (needfree) 547df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 548ad2edad9SMatthew Dillon done2: 549ad2edad9SMatthew Dillon mtx_unlock(&Giant); 550df8bae1dSRodney W. Grimes return (error); 551df8bae1dSRodney W. Grimes } 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes /* 554df8bae1dSRodney W. Grimes * Ioctl system call 555df8bae1dSRodney W. Grimes */ 556d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 557df8bae1dSRodney W. Grimes struct ioctl_args { 558df8bae1dSRodney W. Grimes int fd; 559069e9bc1SDoug Rabson u_long com; 560df8bae1dSRodney W. Grimes caddr_t data; 561df8bae1dSRodney W. Grimes }; 562d2d3e875SBruce Evans #endif 563ad2edad9SMatthew Dillon /* 564ad2edad9SMatthew Dillon * MPSAFE 565ad2edad9SMatthew Dillon */ 566df8bae1dSRodney W. Grimes /* ARGSUSED */ 56726f9a767SRodney W. Grimes int 568b40ce416SJulian Elischer ioctl(td, uap) 569b40ce416SJulian Elischer struct thread *td; 570df8bae1dSRodney W. Grimes register struct ioctl_args *uap; 571df8bae1dSRodney W. Grimes { 572a4db4953SAlfred Perlstein struct file *fp; 573df8bae1dSRodney W. Grimes register struct filedesc *fdp; 574831b9ef2SDoug Rabson register u_long com; 575ad2edad9SMatthew Dillon int error = 0; 576df8bae1dSRodney W. Grimes register u_int size; 577df8bae1dSRodney W. Grimes caddr_t data, memp; 578df8bae1dSRodney W. Grimes int tmp; 579df8bae1dSRodney W. Grimes #define STK_PARAMS 128 580d2ba455cSMatthew Dillon union { 581df8bae1dSRodney W. Grimes char stkbuf[STK_PARAMS]; 582d2ba455cSMatthew Dillon long align; 583d2ba455cSMatthew Dillon } ubuf; 584df8bae1dSRodney W. Grimes 585a4db4953SAlfred Perlstein if ((error = fget(td, uap->fd, &fp)) != 0) 586a4db4953SAlfred Perlstein return (error); 587aa11a498SAlfred Perlstein mtx_lock(&Giant); 588ad2edad9SMatthew Dillon if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 589426da3bcSAlfred Perlstein fdrop(fp, td); 590aa11a498SAlfred Perlstein mtx_unlock(&Giant); 591426da3bcSAlfred Perlstein return (EBADF); 592ad2edad9SMatthew Dillon } 593426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 594df8bae1dSRodney W. Grimes switch (com = uap->com) { 595df8bae1dSRodney W. Grimes case FIONCLEX: 596426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 597df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE; 598426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 599426da3bcSAlfred Perlstein fdrop(fp, td); 600aa11a498SAlfred Perlstein mtx_unlock(&Giant); 601426da3bcSAlfred Perlstein return (0); 602df8bae1dSRodney W. Grimes case FIOCLEX: 603426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 604df8bae1dSRodney W. Grimes fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE; 605426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 606426da3bcSAlfred Perlstein fdrop(fp, td); 607aa11a498SAlfred Perlstein mtx_unlock(&Giant); 608426da3bcSAlfred Perlstein return (0); 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes 611df8bae1dSRodney W. Grimes /* 612df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 613df8bae1dSRodney W. Grimes * copied to/from the user's address space. 614df8bae1dSRodney W. Grimes */ 615df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 616ad2edad9SMatthew Dillon if (size > IOCPARM_MAX) { 617426da3bcSAlfred Perlstein fdrop(fp, td); 618aa11a498SAlfred Perlstein mtx_unlock(&Giant); 619426da3bcSAlfred Perlstein return (ENOTTY); 620ad2edad9SMatthew Dillon } 621279d7226SMatthew Dillon 622df8bae1dSRodney W. Grimes memp = NULL; 623d2ba455cSMatthew Dillon if (size > sizeof (ubuf.stkbuf)) { 624df8bae1dSRodney W. Grimes memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 625df8bae1dSRodney W. Grimes data = memp; 626279d7226SMatthew Dillon } else { 627d2ba455cSMatthew Dillon data = ubuf.stkbuf; 628279d7226SMatthew Dillon } 629df8bae1dSRodney W. Grimes if (com&IOC_IN) { 630df8bae1dSRodney W. Grimes if (size) { 631df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 632df8bae1dSRodney W. Grimes if (error) { 633df8bae1dSRodney W. Grimes if (memp) 634df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 635b40ce416SJulian Elischer fdrop(fp, td); 636426da3bcSAlfred Perlstein goto done; 637df8bae1dSRodney W. Grimes } 638279d7226SMatthew Dillon } else { 639df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 640279d7226SMatthew Dillon } 641279d7226SMatthew Dillon } else if ((com&IOC_OUT) && size) { 642df8bae1dSRodney W. Grimes /* 643df8bae1dSRodney W. Grimes * Zero the buffer so the user always 644df8bae1dSRodney W. Grimes * gets back something deterministic. 645df8bae1dSRodney W. Grimes */ 646df8bae1dSRodney W. Grimes bzero(data, size); 647279d7226SMatthew Dillon } else if (com&IOC_VOID) { 648df8bae1dSRodney W. Grimes *(caddr_t *)data = uap->data; 649279d7226SMatthew Dillon } 650df8bae1dSRodney W. Grimes 651df8bae1dSRodney W. Grimes switch (com) { 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes case FIONBIO: 654426da3bcSAlfred Perlstein FILE_LOCK(fp); 655bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 656df8bae1dSRodney W. Grimes fp->f_flag |= FNONBLOCK; 657df8bae1dSRodney W. Grimes else 658df8bae1dSRodney W. Grimes fp->f_flag &= ~FNONBLOCK; 659426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 660b40ce416SJulian Elischer error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td); 661df8bae1dSRodney W. Grimes break; 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes case FIOASYNC: 664426da3bcSAlfred Perlstein FILE_LOCK(fp); 665bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 666df8bae1dSRodney W. Grimes fp->f_flag |= FASYNC; 667df8bae1dSRodney W. Grimes else 668df8bae1dSRodney W. Grimes fp->f_flag &= ~FASYNC; 669426da3bcSAlfred Perlstein FILE_UNLOCK(fp); 670b40ce416SJulian Elischer error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td); 671df8bae1dSRodney W. Grimes break; 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes default: 674b40ce416SJulian Elischer error = fo_ioctl(fp, com, data, td); 675df8bae1dSRodney W. Grimes /* 676df8bae1dSRodney W. Grimes * Copy any data to user, size was 677df8bae1dSRodney W. Grimes * already set and checked above. 678df8bae1dSRodney W. Grimes */ 679df8bae1dSRodney W. Grimes if (error == 0 && (com&IOC_OUT) && size) 680df8bae1dSRodney W. Grimes error = copyout(data, uap->data, (u_int)size); 681df8bae1dSRodney W. Grimes break; 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes if (memp) 684df8bae1dSRodney W. Grimes free(memp, M_IOCTLOPS); 685b40ce416SJulian Elischer fdrop(fp, td); 686426da3bcSAlfred Perlstein done: 687ad2edad9SMatthew Dillon mtx_unlock(&Giant); 688df8bae1dSRodney W. Grimes return (error); 689df8bae1dSRodney W. Grimes } 690df8bae1dSRodney W. Grimes 69185f190e4SAlfred Perlstein /* 69285f190e4SAlfred Perlstein * sellock and selwait are initialized in selectinit() via SYSINIT. 69385f190e4SAlfred Perlstein */ 69485f190e4SAlfred Perlstein struct mtx sellock; 695265fc98fSSeigo Tanimura struct cv selwait; 69685f190e4SAlfred Perlstein int nselcoll; /* Select collisions since boot */ 6978cb96f20SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, ""); 698df8bae1dSRodney W. Grimes 699df8bae1dSRodney W. Grimes /* 700df8bae1dSRodney W. Grimes * Select system call. 701df8bae1dSRodney W. Grimes */ 702d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 703df8bae1dSRodney W. Grimes struct select_args { 704b08f7993SSujal Patel int nd; 705df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 706df8bae1dSRodney W. Grimes struct timeval *tv; 707df8bae1dSRodney W. Grimes }; 708d2d3e875SBruce Evans #endif 709ad2edad9SMatthew Dillon /* 710ad2edad9SMatthew Dillon * MPSAFE 711ad2edad9SMatthew Dillon */ 71226f9a767SRodney W. Grimes int 713b40ce416SJulian Elischer select(td, uap) 714b40ce416SJulian Elischer register struct thread *td; 715df8bae1dSRodney W. Grimes register struct select_args *uap; 716df8bae1dSRodney W. Grimes { 717426da3bcSAlfred Perlstein struct filedesc *fdp; 718d5e4d7e1SBruce Evans /* 719d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 720d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 721d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 722d5e4d7e1SBruce Evans * of 256. 723d5e4d7e1SBruce Evans */ 724d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 725eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 72600af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7274658f926SAlfred Perlstein int ncoll, error, timo; 728d5e4d7e1SBruce Evans u_int nbufbytes, ncpbytes, nfdbits; 729df8bae1dSRodney W. Grimes 730b08f7993SSujal Patel if (uap->nd < 0) 731acbfbfeaSSujal Patel return (EINVAL); 732426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 733ad2edad9SMatthew Dillon mtx_lock(&Giant); 734426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 735ad2edad9SMatthew Dillon 736b40ce416SJulian Elischer if (uap->nd > td->td_proc->p_fd->fd_nfiles) 737b40ce416SJulian Elischer uap->nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 738426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 739b08f7993SSujal Patel 740d5e4d7e1SBruce Evans /* 741d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 742d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 743d5e4d7e1SBruce Evans */ 744d5e4d7e1SBruce Evans nfdbits = roundup(uap->nd, NFDBITS); 745d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 746d5e4d7e1SBruce Evans nbufbytes = 0; 747d5e4d7e1SBruce Evans if (uap->in != NULL) 748d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 749d5e4d7e1SBruce Evans if (uap->ou != NULL) 750d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 751d5e4d7e1SBruce Evans if (uap->ex != NULL) 752d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 753d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 754d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 755d5e4d7e1SBruce Evans else 756d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 757b08f7993SSujal Patel 758b08f7993SSujal Patel /* 759d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 760d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 761d5e4d7e1SBruce Evans * together. 762b08f7993SSujal Patel */ 763d5e4d7e1SBruce Evans sbp = selbits; 764df8bae1dSRodney W. Grimes #define getbits(name, x) \ 765d5e4d7e1SBruce Evans do { \ 766d5e4d7e1SBruce Evans if (uap->name == NULL) \ 767d5e4d7e1SBruce Evans ibits[x] = NULL; \ 768d5e4d7e1SBruce Evans else { \ 769d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 770d5e4d7e1SBruce Evans obits[x] = sbp; \ 771d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 772d5e4d7e1SBruce Evans error = copyin(uap->name, ibits[x], ncpbytes); \ 773265fc98fSSeigo Tanimura if (error != 0) \ 77485f190e4SAlfred Perlstein goto done_nosellock; \ 775e04ac2feSJohn Baldwin } \ 776d5e4d7e1SBruce Evans } while (0) 777df8bae1dSRodney W. Grimes getbits(in, 0); 778df8bae1dSRodney W. Grimes getbits(ou, 1); 779df8bae1dSRodney W. Grimes getbits(ex, 2); 780df8bae1dSRodney W. Grimes #undef getbits 781d5e4d7e1SBruce Evans if (nbufbytes != 0) 782d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes if (uap->tv) { 785df8bae1dSRodney W. Grimes error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 786df8bae1dSRodney W. Grimes sizeof (atv)); 787265fc98fSSeigo Tanimura if (error) 78885f190e4SAlfred Perlstein goto done_nosellock; 789df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 790df8bae1dSRodney W. Grimes error = EINVAL; 79185f190e4SAlfred Perlstein goto done_nosellock; 792df8bae1dSRodney W. Grimes } 793c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 79400af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 7959c386f6bSJohn Baldwin } else { 79600af9731SPoul-Henning Kamp atv.tv_sec = 0; 7979c386f6bSJohn Baldwin atv.tv_usec = 0; 7989c386f6bSJohn Baldwin } 79900af9731SPoul-Henning Kamp timo = 0; 80085f190e4SAlfred Perlstein mtx_lock(&sellock); 801df8bae1dSRodney W. Grimes retry: 802df8bae1dSRodney W. Grimes ncoll = nselcoll; 803fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 804b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 805fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 80685f190e4SAlfred Perlstein mtx_unlock(&sellock); 80785f190e4SAlfred Perlstein 80885f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 80985f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 810b40ce416SJulian Elischer error = selscan(td, ibits, obits, uap->nd); 81185f190e4SAlfred Perlstein mtx_lock(&sellock); 812b40ce416SJulian Elischer if (error || td->td_retval[0]) 813df8bae1dSRodney W. Grimes goto done; 8144da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 815c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 81685f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 817df8bae1dSRodney W. Grimes goto done; 81800af9731SPoul-Henning Kamp ttv = atv; 81900af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 82000af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 82100af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 822df8bae1dSRodney W. Grimes } 82385f190e4SAlfred Perlstein 82485f190e4SAlfred Perlstein /* 82585f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 82685f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 82785f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 82885f190e4SAlfred Perlstein * necessary. 82985f190e4SAlfred Perlstein */ 830fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 83185f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 83285f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 83385f190e4SAlfred Perlstein goto retry; 83485f190e4SAlfred Perlstein } 835fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 836bfbbc4aaSJason Evans 837265fc98fSSeigo Tanimura if (timo > 0) 83885f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 839265fc98fSSeigo Tanimura else 84085f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 841bfbbc4aaSJason Evans 842df8bae1dSRodney W. Grimes if (error == 0) 843df8bae1dSRodney W. Grimes goto retry; 844265fc98fSSeigo Tanimura 845df8bae1dSRodney W. Grimes done: 84685f190e4SAlfred Perlstein clear_selinfo_list(td); 847fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 848b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 849fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 85085f190e4SAlfred Perlstein mtx_unlock(&sellock); 85185f190e4SAlfred Perlstein 85285f190e4SAlfred Perlstein done_nosellock: 853df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 854df8bae1dSRodney W. Grimes if (error == ERESTART) 855df8bae1dSRodney W. Grimes error = EINTR; 856df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 857df8bae1dSRodney W. Grimes error = 0; 858df8bae1dSRodney W. Grimes #define putbits(name, x) \ 859d5e4d7e1SBruce Evans if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \ 860df8bae1dSRodney W. Grimes error = error2; 861df8bae1dSRodney W. Grimes if (error == 0) { 862df8bae1dSRodney W. Grimes int error2; 863df8bae1dSRodney W. Grimes 864df8bae1dSRodney W. Grimes putbits(in, 0); 865df8bae1dSRodney W. Grimes putbits(ou, 1); 866df8bae1dSRodney W. Grimes putbits(ex, 2); 867df8bae1dSRodney W. Grimes #undef putbits 868df8bae1dSRodney W. Grimes } 869d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 870d5e4d7e1SBruce Evans free(selbits, M_SELECT); 871ad2edad9SMatthew Dillon 872ad2edad9SMatthew Dillon mtx_unlock(&Giant); 873df8bae1dSRodney W. Grimes return (error); 874df8bae1dSRodney W. Grimes } 875df8bae1dSRodney W. Grimes 876265fc98fSSeigo Tanimura static int 877b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 878b40ce416SJulian Elischer struct thread *td; 879b08f7993SSujal Patel fd_mask **ibits, **obits; 880cb226aaaSPoul-Henning Kamp int nfd; 881df8bae1dSRodney W. Grimes { 882f082218cSPeter Wemm int msk, i, fd; 883f082218cSPeter Wemm fd_mask bits; 884df8bae1dSRodney W. Grimes struct file *fp; 885df8bae1dSRodney W. Grimes int n = 0; 8862087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 88742d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 888eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 889df8bae1dSRodney W. Grimes 890eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 891df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 892d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 893d5e4d7e1SBruce Evans continue; 894df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 895b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 896f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 897f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 898f082218cSPeter Wemm if (!(bits & 1)) 899f082218cSPeter Wemm continue; 900eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 901eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 902df8bae1dSRodney W. Grimes return (EBADF); 903eb209311SAlfred Perlstein } 904b40ce416SJulian Elischer if (fo_poll(fp, flag[msk], fp->f_cred, td)) { 905b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 906f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 907df8bae1dSRodney W. Grimes n++; 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes } 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes } 912eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 913b40ce416SJulian Elischer td->td_retval[0] = n; 914df8bae1dSRodney W. Grimes return (0); 915df8bae1dSRodney W. Grimes } 916df8bae1dSRodney W. Grimes 91742d11757SPeter Wemm /* 91842d11757SPeter Wemm * Poll system call. 91942d11757SPeter Wemm */ 92042d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 92142d11757SPeter Wemm struct poll_args { 92242d11757SPeter Wemm struct pollfd *fds; 92342d11757SPeter Wemm u_int nfds; 92442d11757SPeter Wemm int timeout; 92542d11757SPeter Wemm }; 92642d11757SPeter Wemm #endif 927ad2edad9SMatthew Dillon /* 928ad2edad9SMatthew Dillon * MPSAFE 929ad2edad9SMatthew Dillon */ 93042d11757SPeter Wemm int 931b40ce416SJulian Elischer poll(td, uap) 932b40ce416SJulian Elischer struct thread *td; 933ea0237edSJonathan Lemon struct poll_args *uap; 93442d11757SPeter Wemm { 93542d11757SPeter Wemm caddr_t bits; 93642d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 93700af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 938265fc98fSSeigo Tanimura int ncoll, error = 0, timo; 939ea0237edSJonathan Lemon u_int nfds; 94042d11757SPeter Wemm size_t ni; 94142d11757SPeter Wemm 94289b71647SPeter Wemm nfds = SCARG(uap, nfds); 943ad2edad9SMatthew Dillon 944ad2edad9SMatthew Dillon mtx_lock(&Giant); 94589b71647SPeter Wemm /* 9462bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9472bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9482bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9492bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9502bd5ac33SPeter Wemm * safe, but not overly restrictive. 95189b71647SPeter Wemm */ 952b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 953b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 954ad2edad9SMatthew Dillon error = EINVAL; 955ad2edad9SMatthew Dillon goto done2; 956ad2edad9SMatthew Dillon } 95789b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 95842d11757SPeter Wemm if (ni > sizeof(smallbits)) 95942d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 96042d11757SPeter Wemm else 96142d11757SPeter Wemm bits = smallbits; 96242d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 96342d11757SPeter Wemm if (error) 96485f190e4SAlfred Perlstein goto done_nosellock; 96542d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 96642d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 96742d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 96842d11757SPeter Wemm if (itimerfix(&atv)) { 96942d11757SPeter Wemm error = EINVAL; 97085f190e4SAlfred Perlstein goto done_nosellock; 97142d11757SPeter Wemm } 972c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 97300af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9749c386f6bSJohn Baldwin } else { 97500af9731SPoul-Henning Kamp atv.tv_sec = 0; 9769c386f6bSJohn Baldwin atv.tv_usec = 0; 9779c386f6bSJohn Baldwin } 97800af9731SPoul-Henning Kamp timo = 0; 97985f190e4SAlfred Perlstein mtx_lock(&sellock); 98042d11757SPeter Wemm retry: 98142d11757SPeter Wemm ncoll = nselcoll; 982fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 983b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 984fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 98585f190e4SAlfred Perlstein mtx_unlock(&sellock); 98685f190e4SAlfred Perlstein 98785f190e4SAlfred Perlstein /* XXX Is there a better place for this? */ 98885f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 989b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 99085f190e4SAlfred Perlstein mtx_lock(&sellock); 991b40ce416SJulian Elischer if (error || td->td_retval[0]) 99242d11757SPeter Wemm goto done; 9934da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 994c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 99585f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 99642d11757SPeter Wemm goto done; 99700af9731SPoul-Henning Kamp ttv = atv; 99800af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 99900af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 100000af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 100142d11757SPeter Wemm } 100285f190e4SAlfred Perlstein /* 100385f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 100485f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 100585f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 100685f190e4SAlfred Perlstein */ 1007fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 100885f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 1009fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 101085f190e4SAlfred Perlstein goto retry; 101185f190e4SAlfred Perlstein } 101285f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 101385f190e4SAlfred Perlstein 1014265fc98fSSeigo Tanimura if (timo > 0) 101585f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 1016265fc98fSSeigo Tanimura else 101785f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 101885f190e4SAlfred Perlstein 101942d11757SPeter Wemm if (error == 0) 102042d11757SPeter Wemm goto retry; 1021265fc98fSSeigo Tanimura 102242d11757SPeter Wemm done: 102385f190e4SAlfred Perlstein clear_selinfo_list(td); 1024fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1025b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1026fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 102785f190e4SAlfred Perlstein mtx_unlock(&sellock); 102885f190e4SAlfred Perlstein 102985f190e4SAlfred Perlstein done_nosellock: 103042d11757SPeter Wemm /* poll is not restarted after signals... */ 103142d11757SPeter Wemm if (error == ERESTART) 103242d11757SPeter Wemm error = EINTR; 103342d11757SPeter Wemm if (error == EWOULDBLOCK) 103442d11757SPeter Wemm error = 0; 103542d11757SPeter Wemm if (error == 0) { 103642d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 103742d11757SPeter Wemm if (error) 103842d11757SPeter Wemm goto out; 103942d11757SPeter Wemm } 104042d11757SPeter Wemm out: 104142d11757SPeter Wemm if (ni > sizeof(smallbits)) 104242d11757SPeter Wemm free(bits, M_TEMP); 1043ad2edad9SMatthew Dillon done2: 1044ad2edad9SMatthew Dillon mtx_unlock(&Giant); 104542d11757SPeter Wemm return (error); 104642d11757SPeter Wemm } 104742d11757SPeter Wemm 104842d11757SPeter Wemm static int 1049b40ce416SJulian Elischer pollscan(td, fds, nfd) 1050b40ce416SJulian Elischer struct thread *td; 105142d11757SPeter Wemm struct pollfd *fds; 1052ea0237edSJonathan Lemon u_int nfd; 105342d11757SPeter Wemm { 1054b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 105542d11757SPeter Wemm int i; 105642d11757SPeter Wemm struct file *fp; 105742d11757SPeter Wemm int n = 0; 105842d11757SPeter Wemm 1059426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1060eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1061337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 106242d11757SPeter Wemm fds->revents = POLLNVAL; 106342d11757SPeter Wemm n++; 1064337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1065337c9691SJordan K. Hubbard fds->revents = 0; 106642d11757SPeter Wemm } else { 106742d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1068279d7226SMatthew Dillon if (fp == NULL) { 106942d11757SPeter Wemm fds->revents = POLLNVAL; 107042d11757SPeter Wemm n++; 107142d11757SPeter Wemm } else { 10722087c896SBruce Evans /* 10732087c896SBruce Evans * Note: backend also returns POLLHUP and 10742087c896SBruce Evans * POLLERR if appropriate. 10752087c896SBruce Evans */ 107613ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1077b40ce416SJulian Elischer fp->f_cred, td); 107842d11757SPeter Wemm if (fds->revents != 0) 107942d11757SPeter Wemm n++; 108042d11757SPeter Wemm } 108142d11757SPeter Wemm } 108242d11757SPeter Wemm } 1083eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1084b40ce416SJulian Elischer td->td_retval[0] = n; 108542d11757SPeter Wemm return (0); 108642d11757SPeter Wemm } 108742d11757SPeter Wemm 108842d11757SPeter Wemm /* 108942d11757SPeter Wemm * OpenBSD poll system call. 109042d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 109142d11757SPeter Wemm */ 109242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 109342d11757SPeter Wemm struct openbsd_poll_args { 109442d11757SPeter Wemm struct pollfd *fds; 109542d11757SPeter Wemm u_int nfds; 109642d11757SPeter Wemm int timeout; 109742d11757SPeter Wemm }; 109842d11757SPeter Wemm #endif 1099ad2edad9SMatthew Dillon /* 1100ad2edad9SMatthew Dillon * MPSAFE 1101ad2edad9SMatthew Dillon */ 110242d11757SPeter Wemm int 1103b40ce416SJulian Elischer openbsd_poll(td, uap) 1104b40ce416SJulian Elischer register struct thread *td; 110542d11757SPeter Wemm register struct openbsd_poll_args *uap; 110642d11757SPeter Wemm { 1107b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 110842d11757SPeter Wemm } 110942d11757SPeter Wemm 111085f190e4SAlfred Perlstein /* 111185f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 111285f190e4SAlfred Perlstein * we were polling. 111385f190e4SAlfred Perlstein * 111485f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 111585f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 111685f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 111785f190e4SAlfred Perlstein */ 111885f190e4SAlfred Perlstein void 111985f190e4SAlfred Perlstein clear_selinfo_list(td) 112085f190e4SAlfred Perlstein struct thread *td; 112185f190e4SAlfred Perlstein { 112285f190e4SAlfred Perlstein struct selinfo *si; 112385f190e4SAlfred Perlstein 112485f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 112585f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 112685f190e4SAlfred Perlstein si->si_thread = NULL; 112785f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 112885f190e4SAlfred Perlstein } 112985f190e4SAlfred Perlstein 1130df8bae1dSRodney W. Grimes /*ARGSUSED*/ 113126f9a767SRodney W. Grimes int 1132b40ce416SJulian Elischer seltrue(dev, events, td) 1133df8bae1dSRodney W. Grimes dev_t dev; 113442d11757SPeter Wemm int events; 1135b40ce416SJulian Elischer struct thread *td; 1136df8bae1dSRodney W. Grimes { 1137df8bae1dSRodney W. Grimes 113842d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1139df8bae1dSRodney W. Grimes } 1140df8bae1dSRodney W. Grimes 1141df8bae1dSRodney W. Grimes /* 1142df8bae1dSRodney W. Grimes * Record a select request. 1143df8bae1dSRodney W. Grimes */ 1144df8bae1dSRodney W. Grimes void 1145df8bae1dSRodney W. Grimes selrecord(selector, sip) 1146b40ce416SJulian Elischer struct thread *selector; 1147df8bae1dSRodney W. Grimes struct selinfo *sip; 1148df8bae1dSRodney W. Grimes { 1149df8bae1dSRodney W. Grimes 115085f190e4SAlfred Perlstein mtx_lock(&sellock); 115185f190e4SAlfred Perlstein /* 115285f190e4SAlfred Perlstein * If the thread is NULL then take ownership of selinfo 115385f190e4SAlfred Perlstein * however if the thread is not NULL and the thread points to 115485f190e4SAlfred Perlstein * someone else, then we have a collision, otherwise leave it alone 115585f190e4SAlfred Perlstein * as we've owned it in a previous selrecord on this selinfo. 115685f190e4SAlfred Perlstein */ 115785f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1158b40ce416SJulian Elischer sip->si_thread = selector; 115985f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 116085f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 116185f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 116285f190e4SAlfred Perlstein } 116385f190e4SAlfred Perlstein 116485f190e4SAlfred Perlstein mtx_unlock(&sellock); 1165df8bae1dSRodney W. Grimes } 1166df8bae1dSRodney W. Grimes 1167df8bae1dSRodney W. Grimes /* 1168df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1169df8bae1dSRodney W. Grimes */ 1170df8bae1dSRodney W. Grimes void 1171df8bae1dSRodney W. Grimes selwakeup(sip) 117285f190e4SAlfred Perlstein struct selinfo *sip; 1173df8bae1dSRodney W. Grimes { 1174b40ce416SJulian Elischer struct thread *td; 1175df8bae1dSRodney W. Grimes 117685f190e4SAlfred Perlstein mtx_lock(&sellock); 117785f190e4SAlfred Perlstein td = sip->si_thread; 117885f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1179df8bae1dSRodney W. Grimes nselcoll++; 1180df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1181265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1182df8bae1dSRodney W. Grimes } 118385f190e4SAlfred Perlstein if (td == NULL) { 118485f190e4SAlfred Perlstein mtx_unlock(&sellock); 1185b40ce416SJulian Elischer return; 1186b40ce416SJulian Elischer } 118785f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 118885f190e4SAlfred Perlstein sip->si_thread = NULL; 11899ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1190b40ce416SJulian Elischer if (td->td_wchan == (caddr_t)&selwait) { 1191b40ce416SJulian Elischer if (td->td_proc->p_stat == SSLEEP) 1192b40ce416SJulian Elischer setrunnable(td); 1193df8bae1dSRodney W. Grimes else 1194b40ce416SJulian Elischer cv_waitq_remove(td); 119533a9ed9dSJohn Baldwin } else 1196b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 119733a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 119885f190e4SAlfred Perlstein mtx_unlock(&sellock); 1199df8bae1dSRodney W. Grimes } 1200265fc98fSSeigo Tanimura 12014d77a549SAlfred Perlstein static void selectinit(void *); 1202265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1203265fc98fSSeigo Tanimura 1204265fc98fSSeigo Tanimura /* ARGSUSED*/ 1205265fc98fSSeigo Tanimura static void 1206265fc98fSSeigo Tanimura selectinit(dummy) 1207265fc98fSSeigo Tanimura void *dummy; 1208265fc98fSSeigo Tanimura { 1209265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 121085f190e4SAlfred Perlstein mtx_init(&sellock, "sellck", MTX_DEF); 1211265fc98fSSeigo Tanimura } 1212