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> 608f19eb88SIan Dowse #include <sys/syscallsubr.h> 618cb96f20SPeter Wemm #include <sys/sysctl.h> 6242d11757SPeter Wemm #include <sys/sysent.h> 63279d7226SMatthew Dillon #include <sys/bio.h> 64279d7226SMatthew Dillon #include <sys/buf.h> 65265fc98fSSeigo Tanimura #include <sys/condvar.h> 66f67ad03aSPoul-Henning Kamp #ifdef __alpha__ 67f67ad03aSPoul-Henning Kamp #include <sys/disklabel.h> 68f67ad03aSPoul-Henning Kamp #endif 69df8bae1dSRodney W. Grimes #ifdef KTRACE 70df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 71df8bae1dSRodney W. Grimes #endif 72279d7226SMatthew Dillon #include <vm/vm.h> 73279d7226SMatthew Dillon #include <vm/vm_page.h> 74df8bae1dSRodney W. Grimes 75069e9bc1SDoug Rabson #include <machine/limits.h> 76069e9bc1SDoug Rabson 77a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 78a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 79a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 8055166637SPoul-Henning Kamp 81bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 82bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 83bbbb04ceSAlfred Perlstein static int dofileread(struct thread *, struct file *, int, void *, 84bbbb04ceSAlfred Perlstein size_t, off_t, int); 85bbbb04ceSAlfred Perlstein static int dofilewrite(struct thread *, struct file *, int, 86bbbb04ceSAlfred Perlstein const void *, size_t, off_t, int); 878fe387abSDmitrij Tejblum 88df8bae1dSRodney W. Grimes /* 89df8bae1dSRodney W. Grimes * Read system call. 90df8bae1dSRodney W. Grimes */ 91d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 92df8bae1dSRodney W. Grimes struct read_args { 93df8bae1dSRodney W. Grimes int fd; 94134e06feSBruce Evans void *buf; 95134e06feSBruce Evans size_t nbyte; 96df8bae1dSRodney W. Grimes }; 97d2d3e875SBruce Evans #endif 98ad2edad9SMatthew Dillon /* 99ad2edad9SMatthew Dillon * MPSAFE 100ad2edad9SMatthew Dillon */ 10126f9a767SRodney W. Grimes int 102b40ce416SJulian Elischer read(td, uap) 103b40ce416SJulian Elischer struct thread *td; 104b064d43dSMatthew Dillon struct read_args *uap; 105df8bae1dSRodney W. Grimes { 106b064d43dSMatthew Dillon struct file *fp; 107279d7226SMatthew Dillon int error; 108df8bae1dSRodney W. Grimes 109b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) == 0) { 110b40ce416SJulian Elischer error = dofileread(td, fp, uap->fd, uap->buf, 111ad2edad9SMatthew Dillon uap->nbyte, (off_t)-1, 0); 112b40ce416SJulian Elischer fdrop(fp, td); 113ad2edad9SMatthew Dillon } 114279d7226SMatthew Dillon return(error); 115df8bae1dSRodney W. Grimes } 116df8bae1dSRodney W. Grimes 117df8bae1dSRodney W. Grimes /* 1188fe387abSDmitrij Tejblum * Pread system call 1194160ccd9SAlan Cox */ 1204160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1214160ccd9SAlan Cox struct pread_args { 1224160ccd9SAlan Cox int fd; 1234160ccd9SAlan Cox void *buf; 1244160ccd9SAlan Cox size_t nbyte; 1258fe387abSDmitrij Tejblum int pad; 1264160ccd9SAlan Cox off_t offset; 1274160ccd9SAlan Cox }; 1284160ccd9SAlan Cox #endif 129ad2edad9SMatthew Dillon /* 130ad2edad9SMatthew Dillon * MPSAFE 131ad2edad9SMatthew Dillon */ 1324160ccd9SAlan Cox int 133b40ce416SJulian Elischer pread(td, uap) 134b40ce416SJulian Elischer struct thread *td; 135b064d43dSMatthew Dillon struct pread_args *uap; 1364160ccd9SAlan Cox { 137b064d43dSMatthew Dillon struct file *fp; 138279d7226SMatthew Dillon int error; 1398fe387abSDmitrij Tejblum 14097fa4397SAlfred Perlstein if ((error = fget_read(td, uap->fd, &fp)) != 0) 14197fa4397SAlfred Perlstein return (error); 142426da3bcSAlfred Perlstein if (fp->f_type != DTYPE_VNODE) { 143b064d43dSMatthew Dillon error = ESPIPE; 144426da3bcSAlfred Perlstein } else { 145426da3bcSAlfred Perlstein error = dofileread(td, fp, uap->fd, uap->buf, uap->nbyte, 146426da3bcSAlfred Perlstein uap->offset, FOF_OFFSET); 147b064d43dSMatthew Dillon } 148b40ce416SJulian Elischer fdrop(fp, td); 149279d7226SMatthew Dillon return(error); 1508fe387abSDmitrij Tejblum } 1518fe387abSDmitrij Tejblum 1528fe387abSDmitrij Tejblum /* 1538fe387abSDmitrij Tejblum * Code common for read and pread 1548fe387abSDmitrij Tejblum */ 1558fe387abSDmitrij Tejblum int 156b40ce416SJulian Elischer dofileread(td, fp, fd, buf, nbyte, offset, flags) 157b40ce416SJulian Elischer struct thread *td; 1588fe387abSDmitrij Tejblum struct file *fp; 1598fe387abSDmitrij Tejblum int fd, flags; 1608fe387abSDmitrij Tejblum void *buf; 1618fe387abSDmitrij Tejblum size_t nbyte; 1628fe387abSDmitrij Tejblum off_t offset; 1638fe387abSDmitrij Tejblum { 1644160ccd9SAlan Cox struct uio auio; 1654160ccd9SAlan Cox struct iovec aiov; 1664160ccd9SAlan Cox long cnt, error = 0; 1674160ccd9SAlan Cox #ifdef KTRACE 1684160ccd9SAlan Cox struct iovec ktriov; 16942ebfbf2SBrian Feldman struct uio ktruio; 1703c89e357SBrian Feldman int didktr = 0; 1714160ccd9SAlan Cox #endif 1724160ccd9SAlan Cox 1730a3e28cfSAlfred Perlstein aiov.iov_base = buf; 1748fe387abSDmitrij Tejblum aiov.iov_len = nbyte; 1754160ccd9SAlan Cox auio.uio_iov = &aiov; 1764160ccd9SAlan Cox auio.uio_iovcnt = 1; 1778fe387abSDmitrij Tejblum auio.uio_offset = offset; 1788fe387abSDmitrij Tejblum if (nbyte > INT_MAX) 1794160ccd9SAlan Cox return (EINVAL); 1808fe387abSDmitrij Tejblum auio.uio_resid = nbyte; 1814160ccd9SAlan Cox auio.uio_rw = UIO_READ; 1824160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 183b40ce416SJulian Elischer auio.uio_td = td; 1844160ccd9SAlan Cox #ifdef KTRACE 1854160ccd9SAlan Cox /* 1864160ccd9SAlan Cox * if tracing, save a copy of iovec 1874160ccd9SAlan Cox */ 18860a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 1894160ccd9SAlan Cox ktriov = aiov; 19042ebfbf2SBrian Feldman ktruio = auio; 1913c89e357SBrian Feldman didktr = 1; 19242ebfbf2SBrian Feldman } 1934160ccd9SAlan Cox #endif 1948fe387abSDmitrij Tejblum cnt = nbyte; 195279d7226SMatthew Dillon 1969ca43589SRobert Watson if ((error = fo_read(fp, &auio, td->td_ucred, flags, td))) { 1974160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 1984160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 1994160ccd9SAlan Cox error = 0; 200279d7226SMatthew Dillon } 2014160ccd9SAlan Cox cnt -= auio.uio_resid; 2024160ccd9SAlan Cox #ifdef KTRACE 2033c89e357SBrian Feldman if (didktr && error == 0) { 20442ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 20542ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 20660a9bb19SJohn Baldwin ktrgenio(fd, UIO_READ, &ktruio, error); 20742ebfbf2SBrian Feldman } 2084160ccd9SAlan Cox #endif 209b40ce416SJulian Elischer td->td_retval[0] = cnt; 2104160ccd9SAlan Cox return (error); 2114160ccd9SAlan Cox } 2124160ccd9SAlan Cox 2134160ccd9SAlan Cox /* 214df8bae1dSRodney W. Grimes * Scatter read system call. 215df8bae1dSRodney W. Grimes */ 216d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 217df8bae1dSRodney W. Grimes struct readv_args { 2187147b19dSBruce Evans int fd; 219df8bae1dSRodney W. Grimes struct iovec *iovp; 220df8bae1dSRodney W. Grimes u_int iovcnt; 221df8bae1dSRodney W. Grimes }; 222d2d3e875SBruce Evans #endif 223ad2edad9SMatthew Dillon /* 224ad2edad9SMatthew Dillon * MPSAFE 225ad2edad9SMatthew Dillon */ 22626f9a767SRodney W. Grimes int 227b40ce416SJulian Elischer readv(td, uap) 228b40ce416SJulian Elischer struct thread *td; 229b064d43dSMatthew Dillon struct readv_args *uap; 230df8bae1dSRodney W. Grimes { 231b064d43dSMatthew Dillon struct file *fp; 232df8bae1dSRodney W. Grimes struct uio auio; 233b064d43dSMatthew Dillon struct iovec *iov; 234df8bae1dSRodney W. Grimes struct iovec *needfree; 235df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 23682641acdSAlan Cox long i, cnt; 23782641acdSAlan Cox int error; 238df8bae1dSRodney W. Grimes u_int iovlen; 239df8bae1dSRodney W. Grimes #ifdef KTRACE 240df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 24142ebfbf2SBrian Feldman struct uio ktruio; 242df8bae1dSRodney W. Grimes #endif 243df8bae1dSRodney W. Grimes 244b064d43dSMatthew Dillon if ((error = fget_read(td, uap->fd, &fp)) != 0) 24582641acdSAlan Cox return (error); 24682641acdSAlan Cox needfree = NULL; 247df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 248df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 249df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 250ad2edad9SMatthew Dillon if (uap->iovcnt > UIO_MAXIOV) { 251ad2edad9SMatthew Dillon error = EINVAL; 25282641acdSAlan Cox goto done; 253ad2edad9SMatthew Dillon } 254df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 255df8bae1dSRodney W. Grimes needfree = iov; 25682641acdSAlan Cox } else 257df8bae1dSRodney W. Grimes iov = aiov; 258df8bae1dSRodney W. Grimes auio.uio_iov = iov; 259df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 260df8bae1dSRodney W. Grimes auio.uio_rw = UIO_READ; 261df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 262b40ce416SJulian Elischer auio.uio_td = td; 2632c1011f7SJohn Dyson auio.uio_offset = -1; 2640a3e28cfSAlfred Perlstein if ((error = copyin(uap->iovp, iov, iovlen))) 265df8bae1dSRodney W. Grimes goto done; 266df8bae1dSRodney W. Grimes auio.uio_resid = 0; 267df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 268069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 269df8bae1dSRodney W. Grimes error = EINVAL; 270df8bae1dSRodney W. Grimes goto done; 271df8bae1dSRodney W. Grimes } 272069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 273df8bae1dSRodney W. Grimes iov++; 274df8bae1dSRodney W. Grimes } 275df8bae1dSRodney W. Grimes #ifdef KTRACE 276df8bae1dSRodney W. Grimes /* 277df8bae1dSRodney W. Grimes * if tracing, save a copy of iovec 278df8bae1dSRodney W. Grimes */ 27960a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 280df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 2810a3e28cfSAlfred Perlstein bcopy(auio.uio_iov, ktriov, iovlen); 28242ebfbf2SBrian Feldman ktruio = auio; 283df8bae1dSRodney W. Grimes } 284df8bae1dSRodney W. Grimes #endif 285df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 2869ca43589SRobert Watson if ((error = fo_read(fp, &auio, td->td_ucred, 0, td))) { 287df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 288df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 289df8bae1dSRodney W. Grimes error = 0; 290279d7226SMatthew Dillon } 291df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 292df8bae1dSRodney W. Grimes #ifdef KTRACE 293df8bae1dSRodney W. Grimes if (ktriov != NULL) { 29442ebfbf2SBrian Feldman if (error == 0) { 29542ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 29642ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 29760a9bb19SJohn Baldwin ktrgenio(uap->fd, UIO_READ, &ktruio, error); 29842ebfbf2SBrian Feldman } 299df8bae1dSRodney W. Grimes FREE(ktriov, M_TEMP); 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes #endif 302b40ce416SJulian Elischer td->td_retval[0] = cnt; 303df8bae1dSRodney W. Grimes done: 304b40ce416SJulian Elischer fdrop(fp, td); 305df8bae1dSRodney W. Grimes if (needfree) 306df8bae1dSRodney W. Grimes FREE(needfree, M_IOV); 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 */ 41160a9bb19SJohn Baldwin if (KTRPOINT(td, 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(); 4209ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, flags, td))) { 4214160ccd9SAlan Cox if (auio.uio_resid != cnt && (error == ERESTART || 4224160ccd9SAlan Cox error == EINTR || error == EWOULDBLOCK)) 4234160ccd9SAlan Cox error = 0; 424c33c8251SAlfred Perlstein /* Socket layer is responsible for issuing SIGPIPE. */ 425c33c8251SAlfred Perlstein if (error == EPIPE && fp->f_type != DTYPE_SOCKET) { 426b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 427b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 428b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 42919eb87d2SJohn Baldwin } 4304160ccd9SAlan Cox } 4314160ccd9SAlan Cox cnt -= auio.uio_resid; 4324160ccd9SAlan Cox #ifdef KTRACE 4333c89e357SBrian Feldman if (didktr && error == 0) { 43442ebfbf2SBrian Feldman ktruio.uio_iov = &ktriov; 43542ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 43660a9bb19SJohn Baldwin ktrgenio(fd, UIO_WRITE, &ktruio, error); 43742ebfbf2SBrian Feldman } 4384160ccd9SAlan Cox #endif 439b40ce416SJulian Elischer td->td_retval[0] = cnt; 4404160ccd9SAlan Cox return (error); 4414160ccd9SAlan Cox } 4424160ccd9SAlan Cox 4434160ccd9SAlan Cox /* 444df8bae1dSRodney W. Grimes * Gather write system call 445df8bae1dSRodney W. Grimes */ 446d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 447df8bae1dSRodney W. Grimes struct writev_args { 448df8bae1dSRodney W. Grimes int fd; 449df8bae1dSRodney W. Grimes struct iovec *iovp; 450df8bae1dSRodney W. Grimes u_int iovcnt; 451df8bae1dSRodney W. Grimes }; 452d2d3e875SBruce Evans #endif 453ad2edad9SMatthew Dillon /* 454ad2edad9SMatthew Dillon * MPSAFE 455ad2edad9SMatthew Dillon */ 45626f9a767SRodney W. Grimes int 457b40ce416SJulian Elischer writev(td, uap) 458b40ce416SJulian Elischer struct thread *td; 459df8bae1dSRodney W. Grimes register struct writev_args *uap; 460df8bae1dSRodney W. Grimes { 461b064d43dSMatthew Dillon struct file *fp; 462df8bae1dSRodney W. Grimes struct uio auio; 463df8bae1dSRodney W. Grimes register struct iovec *iov; 464df8bae1dSRodney W. Grimes struct iovec *needfree; 465df8bae1dSRodney W. Grimes struct iovec aiov[UIO_SMALLIOV]; 466df8bae1dSRodney W. Grimes long i, cnt, error = 0; 467df8bae1dSRodney W. Grimes u_int iovlen; 468df8bae1dSRodney W. Grimes #ifdef KTRACE 469df8bae1dSRodney W. Grimes struct iovec *ktriov = NULL; 47042ebfbf2SBrian Feldman struct uio ktruio; 471df8bae1dSRodney W. Grimes #endif 472df8bae1dSRodney W. Grimes 473ad2edad9SMatthew Dillon mtx_lock(&Giant); 474b064d43dSMatthew Dillon if ((error = fget_write(td, uap->fd, &fp)) != 0) { 475ad2edad9SMatthew Dillon error = EBADF; 476ad2edad9SMatthew Dillon goto done2; 477ad2edad9SMatthew Dillon } 478df8bae1dSRodney W. Grimes /* note: can't use iovlen until iovcnt is validated */ 479df8bae1dSRodney W. Grimes iovlen = uap->iovcnt * sizeof (struct iovec); 480df8bae1dSRodney W. Grimes if (uap->iovcnt > UIO_SMALLIOV) { 4811aa3e7ddSBrian Feldman if (uap->iovcnt > UIO_MAXIOV) { 4821aa3e7ddSBrian Feldman needfree = NULL; 4831aa3e7ddSBrian Feldman error = EINVAL; 4841aa3e7ddSBrian Feldman goto done; 4851aa3e7ddSBrian Feldman } 486df8bae1dSRodney W. Grimes MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 487df8bae1dSRodney W. Grimes needfree = iov; 488df8bae1dSRodney W. Grimes } else { 489df8bae1dSRodney W. Grimes iov = aiov; 490df8bae1dSRodney W. Grimes needfree = NULL; 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes auio.uio_iov = iov; 493df8bae1dSRodney W. Grimes auio.uio_iovcnt = uap->iovcnt; 494df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 495df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_USERSPACE; 496b40ce416SJulian Elischer auio.uio_td = td; 4972c1011f7SJohn Dyson auio.uio_offset = -1; 4980a3e28cfSAlfred Perlstein if ((error = copyin(uap->iovp, iov, iovlen))) 499df8bae1dSRodney W. Grimes goto done; 500df8bae1dSRodney W. Grimes auio.uio_resid = 0; 501df8bae1dSRodney W. Grimes for (i = 0; i < uap->iovcnt; i++) { 502069e9bc1SDoug Rabson if (iov->iov_len > INT_MAX - auio.uio_resid) { 503df8bae1dSRodney W. Grimes error = EINVAL; 504df8bae1dSRodney W. Grimes goto done; 505df8bae1dSRodney W. Grimes } 506069e9bc1SDoug Rabson auio.uio_resid += iov->iov_len; 507df8bae1dSRodney W. Grimes iov++; 508df8bae1dSRodney W. Grimes } 509df8bae1dSRodney W. Grimes #ifdef KTRACE 510df8bae1dSRodney W. Grimes /* 51142ebfbf2SBrian Feldman * if tracing, save a copy of iovec and uio 512df8bae1dSRodney W. Grimes */ 51360a9bb19SJohn Baldwin if (KTRPOINT(td, KTR_GENIO)) { 514df8bae1dSRodney W. Grimes MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 5150a3e28cfSAlfred Perlstein bcopy(auio.uio_iov, ktriov, iovlen); 51642ebfbf2SBrian Feldman ktruio = auio; 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes #endif 519df8bae1dSRodney W. Grimes cnt = auio.uio_resid; 520a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5219440653dSMatthew Dillon bwillwrite(); 5229ca43589SRobert Watson if ((error = fo_write(fp, &auio, td->td_ucred, 0, td))) { 523df8bae1dSRodney W. Grimes if (auio.uio_resid != cnt && (error == ERESTART || 524df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 525df8bae1dSRodney W. Grimes error = 0; 52619eb87d2SJohn Baldwin if (error == EPIPE) { 527b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 528b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 529b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 53019eb87d2SJohn Baldwin } 531df8bae1dSRodney W. Grimes } 532df8bae1dSRodney W. Grimes cnt -= auio.uio_resid; 533df8bae1dSRodney W. Grimes #ifdef KTRACE 534df8bae1dSRodney W. Grimes if (ktriov != NULL) { 53542ebfbf2SBrian Feldman if (error == 0) { 53642ebfbf2SBrian Feldman ktruio.uio_iov = ktriov; 53742ebfbf2SBrian Feldman ktruio.uio_resid = cnt; 53860a9bb19SJohn Baldwin ktrgenio(uap->fd, UIO_WRITE, &ktruio, 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)) { 6240a3e28cfSAlfred Perlstein memp = 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); 660d49fa1caSRobert Watson error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, 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); 670d49fa1caSRobert Watson error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); 671df8bae1dSRodney W. Grimes break; 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes default: 674d49fa1caSRobert Watson error = fo_ioctl(fp, com, data, td->td_ucred, 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; 6969ae6d334SKelly Yancey u_int nselcoll; /* Select collisions since boot */ 6979ae6d334SKelly Yancey SYSCTL_UINT(_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 { 7178f19eb88SIan Dowse struct timeval tv, *tvp; 7188f19eb88SIan Dowse int error; 7198f19eb88SIan Dowse 7208f19eb88SIan Dowse if (uap->tv != NULL) { 7218f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 7228f19eb88SIan Dowse if (error) 7238f19eb88SIan Dowse return (error); 7248f19eb88SIan Dowse tvp = &tv; 7258f19eb88SIan Dowse } else 7268f19eb88SIan Dowse tvp = NULL; 7278f19eb88SIan Dowse 7288f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 7298f19eb88SIan Dowse } 7308f19eb88SIan Dowse 7318f19eb88SIan Dowse int 7328f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 7338f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 7348f19eb88SIan Dowse { 735426da3bcSAlfred Perlstein struct filedesc *fdp; 736d5e4d7e1SBruce Evans /* 737d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 738d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 739d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 740d5e4d7e1SBruce Evans * of 256. 741d5e4d7e1SBruce Evans */ 742d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 743eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 74400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7459ae6d334SKelly Yancey int error, timo; 7469ae6d334SKelly Yancey u_int ncoll, nbufbytes, ncpbytes, nfdbits; 747df8bae1dSRodney W. Grimes 7488f19eb88SIan Dowse if (nd < 0) 749acbfbfeaSSujal Patel return (EINVAL); 750426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 751ad2edad9SMatthew Dillon mtx_lock(&Giant); 752426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 753ad2edad9SMatthew Dillon 7548f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 7558f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 756426da3bcSAlfred Perlstein FILEDESC_UNLOCK(fdp); 757b08f7993SSujal Patel 758d5e4d7e1SBruce Evans /* 759d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 760d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 761d5e4d7e1SBruce Evans */ 7628f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 763d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 764d5e4d7e1SBruce Evans nbufbytes = 0; 7658f19eb88SIan Dowse if (fd_in != NULL) 766d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7678f19eb88SIan Dowse if (fd_ou != NULL) 768d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7698f19eb88SIan Dowse if (fd_ex != NULL) 770d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 771d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 772d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 773d5e4d7e1SBruce Evans else 774d5e4d7e1SBruce Evans selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 775b08f7993SSujal Patel 776b08f7993SSujal Patel /* 777d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 778d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 779d5e4d7e1SBruce Evans * together. 780b08f7993SSujal Patel */ 781d5e4d7e1SBruce Evans sbp = selbits; 782df8bae1dSRodney W. Grimes #define getbits(name, x) \ 783d5e4d7e1SBruce Evans do { \ 7848f19eb88SIan Dowse if (name == NULL) \ 785d5e4d7e1SBruce Evans ibits[x] = NULL; \ 786d5e4d7e1SBruce Evans else { \ 787d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 788d5e4d7e1SBruce Evans obits[x] = sbp; \ 789d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 7908f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 791265fc98fSSeigo Tanimura if (error != 0) \ 79285f190e4SAlfred Perlstein goto done_nosellock; \ 793e04ac2feSJohn Baldwin } \ 794d5e4d7e1SBruce Evans } while (0) 7958f19eb88SIan Dowse getbits(fd_in, 0); 7968f19eb88SIan Dowse getbits(fd_ou, 1); 7978f19eb88SIan Dowse getbits(fd_ex, 2); 798df8bae1dSRodney W. Grimes #undef getbits 799d5e4d7e1SBruce Evans if (nbufbytes != 0) 800d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 801df8bae1dSRodney W. Grimes 8028f19eb88SIan Dowse if (tvp != NULL) { 8038f19eb88SIan Dowse atv = *tvp; 804df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 805df8bae1dSRodney W. Grimes error = EINVAL; 80685f190e4SAlfred Perlstein goto done_nosellock; 807df8bae1dSRodney W. Grimes } 808c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 80900af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8109c386f6bSJohn Baldwin } else { 81100af9731SPoul-Henning Kamp atv.tv_sec = 0; 8129c386f6bSJohn Baldwin atv.tv_usec = 0; 8139c386f6bSJohn Baldwin } 81400af9731SPoul-Henning Kamp timo = 0; 8152149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 81685f190e4SAlfred Perlstein mtx_lock(&sellock); 817df8bae1dSRodney W. Grimes retry: 818df8bae1dSRodney W. Grimes ncoll = nselcoll; 819fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 820b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 821fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 82285f190e4SAlfred Perlstein mtx_unlock(&sellock); 82385f190e4SAlfred Perlstein 8248f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 82585f190e4SAlfred Perlstein mtx_lock(&sellock); 826b40ce416SJulian Elischer if (error || td->td_retval[0]) 827df8bae1dSRodney W. Grimes goto done; 8284da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 829c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 83085f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 831df8bae1dSRodney W. Grimes goto done; 83200af9731SPoul-Henning Kamp ttv = atv; 83300af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 83400af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 83500af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 836df8bae1dSRodney W. Grimes } 83785f190e4SAlfred Perlstein 83885f190e4SAlfred Perlstein /* 83985f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 84085f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of 84185f190e4SAlfred Perlstein * collisions and rescan the file descriptors if 84285f190e4SAlfred Perlstein * necessary. 84385f190e4SAlfred Perlstein */ 844fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 84585f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 84685f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 84785f190e4SAlfred Perlstein goto retry; 84885f190e4SAlfred Perlstein } 849fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 850bfbbc4aaSJason Evans 851265fc98fSSeigo Tanimura if (timo > 0) 85285f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 853265fc98fSSeigo Tanimura else 85485f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 855bfbbc4aaSJason Evans 856df8bae1dSRodney W. Grimes if (error == 0) 857df8bae1dSRodney W. Grimes goto retry; 858265fc98fSSeigo Tanimura 859df8bae1dSRodney W. Grimes done: 86085f190e4SAlfred Perlstein clear_selinfo_list(td); 861fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 862b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 863fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 86485f190e4SAlfred Perlstein mtx_unlock(&sellock); 86585f190e4SAlfred Perlstein 86685f190e4SAlfred Perlstein done_nosellock: 867df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 868df8bae1dSRodney W. Grimes if (error == ERESTART) 869df8bae1dSRodney W. Grimes error = EINTR; 870df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 871df8bae1dSRodney W. Grimes error = 0; 872df8bae1dSRodney W. Grimes #define putbits(name, x) \ 8738f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 874df8bae1dSRodney W. Grimes error = error2; 875df8bae1dSRodney W. Grimes if (error == 0) { 876df8bae1dSRodney W. Grimes int error2; 877df8bae1dSRodney W. Grimes 8788f19eb88SIan Dowse putbits(fd_in, 0); 8798f19eb88SIan Dowse putbits(fd_ou, 1); 8808f19eb88SIan Dowse putbits(fd_ex, 2); 881df8bae1dSRodney W. Grimes #undef putbits 882df8bae1dSRodney W. Grimes } 883d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 884d5e4d7e1SBruce Evans free(selbits, M_SELECT); 885ad2edad9SMatthew Dillon 886ad2edad9SMatthew Dillon mtx_unlock(&Giant); 887df8bae1dSRodney W. Grimes return (error); 888df8bae1dSRodney W. Grimes } 889df8bae1dSRodney W. Grimes 890265fc98fSSeigo Tanimura static int 891b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 892b40ce416SJulian Elischer struct thread *td; 893b08f7993SSujal Patel fd_mask **ibits, **obits; 894cb226aaaSPoul-Henning Kamp int nfd; 895df8bae1dSRodney W. Grimes { 896f082218cSPeter Wemm int msk, i, fd; 897f082218cSPeter Wemm fd_mask bits; 898df8bae1dSRodney W. Grimes struct file *fp; 899df8bae1dSRodney W. Grimes int n = 0; 9002087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 90142d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 902eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 903df8bae1dSRodney W. Grimes 904eb209311SAlfred Perlstein FILEDESC_LOCK(fdp); 905df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 906d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 907d5e4d7e1SBruce Evans continue; 908df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 909b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 910f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 911f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 912f082218cSPeter Wemm if (!(bits & 1)) 913f082218cSPeter Wemm continue; 914eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 915eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 916df8bae1dSRodney W. Grimes return (EBADF); 917eb209311SAlfred Perlstein } 918ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 919ea6027a8SRobert Watson td)) { 920b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 921f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 922df8bae1dSRodney W. Grimes n++; 923df8bae1dSRodney W. Grimes } 924df8bae1dSRodney W. Grimes } 925df8bae1dSRodney W. Grimes } 926df8bae1dSRodney W. Grimes } 927eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 928b40ce416SJulian Elischer td->td_retval[0] = n; 929df8bae1dSRodney W. Grimes return (0); 930df8bae1dSRodney W. Grimes } 931df8bae1dSRodney W. Grimes 93242d11757SPeter Wemm /* 93342d11757SPeter Wemm * Poll system call. 93442d11757SPeter Wemm */ 93542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 93642d11757SPeter Wemm struct poll_args { 93742d11757SPeter Wemm struct pollfd *fds; 93842d11757SPeter Wemm u_int nfds; 93942d11757SPeter Wemm int timeout; 94042d11757SPeter Wemm }; 94142d11757SPeter Wemm #endif 942ad2edad9SMatthew Dillon /* 943ad2edad9SMatthew Dillon * MPSAFE 944ad2edad9SMatthew Dillon */ 94542d11757SPeter Wemm int 946b40ce416SJulian Elischer poll(td, uap) 947b40ce416SJulian Elischer struct thread *td; 948ea0237edSJonathan Lemon struct poll_args *uap; 94942d11757SPeter Wemm { 95042d11757SPeter Wemm caddr_t bits; 95142d11757SPeter Wemm char smallbits[32 * sizeof(struct pollfd)]; 95200af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 9539ae6d334SKelly Yancey int error = 0, timo; 9549ae6d334SKelly Yancey u_int ncoll, nfds; 95542d11757SPeter Wemm size_t ni; 95642d11757SPeter Wemm 95789b71647SPeter Wemm nfds = SCARG(uap, nfds); 958ad2edad9SMatthew Dillon 959ad2edad9SMatthew Dillon mtx_lock(&Giant); 96089b71647SPeter Wemm /* 9612bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9622bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9632bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9642bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9652bd5ac33SPeter Wemm * safe, but not overly restrictive. 96689b71647SPeter Wemm */ 967b40ce416SJulian Elischer if ((nfds > td->td_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur) && 968b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 969ad2edad9SMatthew Dillon error = EINVAL; 970ad2edad9SMatthew Dillon goto done2; 971ad2edad9SMatthew Dillon } 97289b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 97342d11757SPeter Wemm if (ni > sizeof(smallbits)) 97442d11757SPeter Wemm bits = malloc(ni, M_TEMP, M_WAITOK); 97542d11757SPeter Wemm else 97642d11757SPeter Wemm bits = smallbits; 97742d11757SPeter Wemm error = copyin(SCARG(uap, fds), bits, ni); 97842d11757SPeter Wemm if (error) 97985f190e4SAlfred Perlstein goto done_nosellock; 98042d11757SPeter Wemm if (SCARG(uap, timeout) != INFTIM) { 98142d11757SPeter Wemm atv.tv_sec = SCARG(uap, timeout) / 1000; 98242d11757SPeter Wemm atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000; 98342d11757SPeter Wemm if (itimerfix(&atv)) { 98442d11757SPeter Wemm error = EINVAL; 98585f190e4SAlfred Perlstein goto done_nosellock; 98642d11757SPeter Wemm } 987c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 98800af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9899c386f6bSJohn Baldwin } else { 99000af9731SPoul-Henning Kamp atv.tv_sec = 0; 9919c386f6bSJohn Baldwin atv.tv_usec = 0; 9929c386f6bSJohn Baldwin } 99300af9731SPoul-Henning Kamp timo = 0; 9942149c527SPeter Wemm TAILQ_INIT(&td->td_selq); 99585f190e4SAlfred Perlstein mtx_lock(&sellock); 99642d11757SPeter Wemm retry: 99742d11757SPeter Wemm ncoll = nselcoll; 998fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 999b40ce416SJulian Elischer td->td_flags |= TDF_SELECT; 1000fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 100185f190e4SAlfred Perlstein mtx_unlock(&sellock); 100285f190e4SAlfred Perlstein 1003b40ce416SJulian Elischer error = pollscan(td, (struct pollfd *)bits, nfds); 100485f190e4SAlfred Perlstein mtx_lock(&sellock); 1005b40ce416SJulian Elischer if (error || td->td_retval[0]) 100642d11757SPeter Wemm goto done; 10074da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1008c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 100985f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 101042d11757SPeter Wemm goto done; 101100af9731SPoul-Henning Kamp ttv = atv; 101200af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 101300af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 101400af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 101542d11757SPeter Wemm } 101685f190e4SAlfred Perlstein /* 101785f190e4SAlfred Perlstein * An event of interest may occur while we do not hold 101885f190e4SAlfred Perlstein * sellock, so check TDF_SELECT and the number of collisions 101985f190e4SAlfred Perlstein * and rescan the file descriptors if necessary. 102085f190e4SAlfred Perlstein */ 1021fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 102285f190e4SAlfred Perlstein if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) { 1023fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 102485f190e4SAlfred Perlstein goto retry; 102585f190e4SAlfred Perlstein } 102685f190e4SAlfred Perlstein mtx_unlock_spin(&sched_lock); 102785f190e4SAlfred Perlstein 1028265fc98fSSeigo Tanimura if (timo > 0) 102985f190e4SAlfred Perlstein error = cv_timedwait_sig(&selwait, &sellock, timo); 1030265fc98fSSeigo Tanimura else 103185f190e4SAlfred Perlstein error = cv_wait_sig(&selwait, &sellock); 103285f190e4SAlfred Perlstein 103342d11757SPeter Wemm if (error == 0) 103442d11757SPeter Wemm goto retry; 1035265fc98fSSeigo Tanimura 103642d11757SPeter Wemm done: 103785f190e4SAlfred Perlstein clear_selinfo_list(td); 1038fea2ab83SJohn Baldwin mtx_lock_spin(&sched_lock); 1039b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 1040fea2ab83SJohn Baldwin mtx_unlock_spin(&sched_lock); 104185f190e4SAlfred Perlstein mtx_unlock(&sellock); 104285f190e4SAlfred Perlstein 104385f190e4SAlfred Perlstein done_nosellock: 104442d11757SPeter Wemm /* poll is not restarted after signals... */ 104542d11757SPeter Wemm if (error == ERESTART) 104642d11757SPeter Wemm error = EINTR; 104742d11757SPeter Wemm if (error == EWOULDBLOCK) 104842d11757SPeter Wemm error = 0; 104942d11757SPeter Wemm if (error == 0) { 105042d11757SPeter Wemm error = copyout(bits, SCARG(uap, fds), ni); 105142d11757SPeter Wemm if (error) 105242d11757SPeter Wemm goto out; 105342d11757SPeter Wemm } 105442d11757SPeter Wemm out: 105542d11757SPeter Wemm if (ni > sizeof(smallbits)) 105642d11757SPeter Wemm free(bits, M_TEMP); 1057ad2edad9SMatthew Dillon done2: 1058ad2edad9SMatthew Dillon mtx_unlock(&Giant); 105942d11757SPeter Wemm return (error); 106042d11757SPeter Wemm } 106142d11757SPeter Wemm 106242d11757SPeter Wemm static int 1063b40ce416SJulian Elischer pollscan(td, fds, nfd) 1064b40ce416SJulian Elischer struct thread *td; 106542d11757SPeter Wemm struct pollfd *fds; 1066ea0237edSJonathan Lemon u_int nfd; 106742d11757SPeter Wemm { 1068b40ce416SJulian Elischer register struct filedesc *fdp = td->td_proc->p_fd; 106942d11757SPeter Wemm int i; 107042d11757SPeter Wemm struct file *fp; 107142d11757SPeter Wemm int n = 0; 107242d11757SPeter Wemm 1073426da3bcSAlfred Perlstein FILEDESC_LOCK(fdp); 1074eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1075337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 107642d11757SPeter Wemm fds->revents = POLLNVAL; 107742d11757SPeter Wemm n++; 1078337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1079337c9691SJordan K. Hubbard fds->revents = 0; 108042d11757SPeter Wemm } else { 108142d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1082279d7226SMatthew Dillon if (fp == NULL) { 108342d11757SPeter Wemm fds->revents = POLLNVAL; 108442d11757SPeter Wemm n++; 108542d11757SPeter Wemm } else { 10862087c896SBruce Evans /* 10872087c896SBruce Evans * Note: backend also returns POLLHUP and 10882087c896SBruce Evans * POLLERR if appropriate. 10892087c896SBruce Evans */ 109013ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1091ea6027a8SRobert Watson td->td_ucred, td); 109242d11757SPeter Wemm if (fds->revents != 0) 109342d11757SPeter Wemm n++; 109442d11757SPeter Wemm } 109542d11757SPeter Wemm } 109642d11757SPeter Wemm } 1097eb209311SAlfred Perlstein FILEDESC_UNLOCK(fdp); 1098b40ce416SJulian Elischer td->td_retval[0] = n; 109942d11757SPeter Wemm return (0); 110042d11757SPeter Wemm } 110142d11757SPeter Wemm 110242d11757SPeter Wemm /* 110342d11757SPeter Wemm * OpenBSD poll system call. 110442d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 110542d11757SPeter Wemm */ 110642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 110742d11757SPeter Wemm struct openbsd_poll_args { 110842d11757SPeter Wemm struct pollfd *fds; 110942d11757SPeter Wemm u_int nfds; 111042d11757SPeter Wemm int timeout; 111142d11757SPeter Wemm }; 111242d11757SPeter Wemm #endif 1113ad2edad9SMatthew Dillon /* 1114ad2edad9SMatthew Dillon * MPSAFE 1115ad2edad9SMatthew Dillon */ 111642d11757SPeter Wemm int 1117b40ce416SJulian Elischer openbsd_poll(td, uap) 1118b40ce416SJulian Elischer register struct thread *td; 111942d11757SPeter Wemm register struct openbsd_poll_args *uap; 112042d11757SPeter Wemm { 1121b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 112242d11757SPeter Wemm } 112342d11757SPeter Wemm 112485f190e4SAlfred Perlstein /* 112585f190e4SAlfred Perlstein * Remove the references to the thread from all of the objects 112685f190e4SAlfred Perlstein * we were polling. 112785f190e4SAlfred Perlstein * 112885f190e4SAlfred Perlstein * This code assumes that the underlying owner of the selinfo 112985f190e4SAlfred Perlstein * structure will hold sellock before it changes it, and that 113085f190e4SAlfred Perlstein * it will unlink itself from our list if it goes away. 113185f190e4SAlfred Perlstein */ 113285f190e4SAlfred Perlstein void 113385f190e4SAlfred Perlstein clear_selinfo_list(td) 113485f190e4SAlfred Perlstein struct thread *td; 113585f190e4SAlfred Perlstein { 113685f190e4SAlfred Perlstein struct selinfo *si; 113785f190e4SAlfred Perlstein 113885f190e4SAlfred Perlstein mtx_assert(&sellock, MA_OWNED); 113985f190e4SAlfred Perlstein TAILQ_FOREACH(si, &td->td_selq, si_thrlist) 114085f190e4SAlfred Perlstein si->si_thread = NULL; 114185f190e4SAlfred Perlstein TAILQ_INIT(&td->td_selq); 114285f190e4SAlfred Perlstein } 114385f190e4SAlfred Perlstein 1144df8bae1dSRodney W. Grimes /*ARGSUSED*/ 114526f9a767SRodney W. Grimes int 1146b40ce416SJulian Elischer seltrue(dev, events, td) 1147df8bae1dSRodney W. Grimes dev_t dev; 114842d11757SPeter Wemm int events; 1149b40ce416SJulian Elischer struct thread *td; 1150df8bae1dSRodney W. Grimes { 1151df8bae1dSRodney W. Grimes 115242d11757SPeter Wemm return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 1153df8bae1dSRodney W. Grimes } 1154df8bae1dSRodney W. Grimes 1155df8bae1dSRodney W. Grimes /* 1156df8bae1dSRodney W. Grimes * Record a select request. 1157df8bae1dSRodney W. Grimes */ 1158df8bae1dSRodney W. Grimes void 1159df8bae1dSRodney W. Grimes selrecord(selector, sip) 1160b40ce416SJulian Elischer struct thread *selector; 1161df8bae1dSRodney W. Grimes struct selinfo *sip; 1162df8bae1dSRodney W. Grimes { 1163df8bae1dSRodney W. Grimes 116485f190e4SAlfred Perlstein mtx_lock(&sellock); 116585f190e4SAlfred Perlstein /* 1166b605b54cSAlfred Perlstein * If the selinfo's thread pointer is NULL then take ownership of it. 1167b605b54cSAlfred Perlstein * 1168b605b54cSAlfred Perlstein * If the thread pointer is not NULL and it points to another 1169b605b54cSAlfred Perlstein * thread, then we have a collision. 1170b605b54cSAlfred Perlstein * 1171b605b54cSAlfred Perlstein * If the thread pointer is not NULL and points back to us then leave 1172b605b54cSAlfred Perlstein * it alone as we've already added pointed it at us and added it to 1173b605b54cSAlfred Perlstein * our list. 117485f190e4SAlfred Perlstein */ 117585f190e4SAlfred Perlstein if (sip->si_thread == NULL) { 1176b40ce416SJulian Elischer sip->si_thread = selector; 117785f190e4SAlfred Perlstein TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist); 117885f190e4SAlfred Perlstein } else if (sip->si_thread != selector) { 117985f190e4SAlfred Perlstein sip->si_flags |= SI_COLL; 118085f190e4SAlfred Perlstein } 118185f190e4SAlfred Perlstein 118285f190e4SAlfred Perlstein mtx_unlock(&sellock); 1183df8bae1dSRodney W. Grimes } 1184df8bae1dSRodney W. Grimes 1185df8bae1dSRodney W. Grimes /* 1186df8bae1dSRodney W. Grimes * Do a wakeup when a selectable event occurs. 1187df8bae1dSRodney W. Grimes */ 1188df8bae1dSRodney W. Grimes void 1189df8bae1dSRodney W. Grimes selwakeup(sip) 119085f190e4SAlfred Perlstein struct selinfo *sip; 1191df8bae1dSRodney W. Grimes { 1192b40ce416SJulian Elischer struct thread *td; 1193df8bae1dSRodney W. Grimes 119485f190e4SAlfred Perlstein mtx_lock(&sellock); 119585f190e4SAlfred Perlstein td = sip->si_thread; 119685f190e4SAlfred Perlstein if ((sip->si_flags & SI_COLL) != 0) { 1197df8bae1dSRodney W. Grimes nselcoll++; 1198df8bae1dSRodney W. Grimes sip->si_flags &= ~SI_COLL; 1199265fc98fSSeigo Tanimura cv_broadcast(&selwait); 1200df8bae1dSRodney W. Grimes } 120185f190e4SAlfred Perlstein if (td == NULL) { 120285f190e4SAlfred Perlstein mtx_unlock(&sellock); 1203b40ce416SJulian Elischer return; 1204b40ce416SJulian Elischer } 120585f190e4SAlfred Perlstein TAILQ_REMOVE(&td->td_selq, sip, si_thrlist); 120685f190e4SAlfred Perlstein sip->si_thread = NULL; 12079ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 12080a3e28cfSAlfred Perlstein if (td->td_wchan == &selwait) { 1209e602ba25SJulian Elischer if (td->td_state == TDS_SLP) 1210b40ce416SJulian Elischer setrunnable(td); 1211df8bae1dSRodney W. Grimes else 1212b40ce416SJulian Elischer cv_waitq_remove(td); 121333a9ed9dSJohn Baldwin } else 1214b40ce416SJulian Elischer td->td_flags &= ~TDF_SELECT; 121533a9ed9dSJohn Baldwin mtx_unlock_spin(&sched_lock); 121685f190e4SAlfred Perlstein mtx_unlock(&sellock); 1217df8bae1dSRodney W. Grimes } 1218265fc98fSSeigo Tanimura 12194d77a549SAlfred Perlstein static void selectinit(void *); 1220265fc98fSSeigo Tanimura SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL) 1221265fc98fSSeigo Tanimura 1222265fc98fSSeigo Tanimura /* ARGSUSED*/ 1223265fc98fSSeigo Tanimura static void 1224265fc98fSSeigo Tanimura selectinit(dummy) 1225265fc98fSSeigo Tanimura void *dummy; 1226265fc98fSSeigo Tanimura { 1227265fc98fSSeigo Tanimura cv_init(&selwait, "select"); 12286008862bSJohn Baldwin mtx_init(&sellock, "sellck", NULL, MTX_DEF); 1229265fc98fSSeigo Tanimura } 1230