19454b2d8SWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 40a9d2f8d8SRobert Watson #include "opt_capsicum.h" 412de92a38SPeter Wemm #include "opt_compat.h" 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> 474a144410SRobert Watson #include <sys/capsicum.h> 48df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 4920982410SBruce Evans #include <sys/filio.h> 503ac4d1efSBruce Evans #include <sys/fcntl.h> 51df8bae1dSRodney W. Grimes #include <sys/file.h> 522609222aSPawel Jakub Dawidek #include <sys/lock.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 54797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 55df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 56df8bae1dSRodney W. Grimes #include <sys/uio.h> 57df8bae1dSRodney W. Grimes #include <sys/kernel.h> 58e4650294SJohn Baldwin #include <sys/ktr.h> 59104a9b7eSAlexander Kabaev #include <sys/limits.h> 60df8bae1dSRodney W. Grimes #include <sys/malloc.h> 6142d11757SPeter Wemm #include <sys/poll.h> 6289b71647SPeter Wemm #include <sys/resourcevar.h> 630a2c3d48SGarrett Wollman #include <sys/selinfo.h> 6444f3b092SJohn Baldwin #include <sys/sleepqueue.h> 658f19eb88SIan Dowse #include <sys/syscallsubr.h> 668cb96f20SPeter Wemm #include <sys/sysctl.h> 6742d11757SPeter Wemm #include <sys/sysent.h> 689bbee259SAndrey A. Chernov #include <sys/vnode.h> 69279d7226SMatthew Dillon #include <sys/bio.h> 70279d7226SMatthew Dillon #include <sys/buf.h> 71265fc98fSSeigo Tanimura #include <sys/condvar.h> 72df8bae1dSRodney W. Grimes #ifdef KTRACE 73df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 74df8bae1dSRodney W. Grimes #endif 75df8bae1dSRodney W. Grimes 76e4650294SJohn Baldwin #include <security/audit/audit.h> 77ace8398dSJeff Roberson 78cd4dd444SKonstantin Belousov int iosize_max_clamp = 0; 798bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW, 808bb9a904SKonstantin Belousov &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX"); 81bf3e483bSKonstantin Belousov int devfs_iosize_max_clamp = 1; 82bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW, 83bf3e483bSKonstantin Belousov &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices"); 84bf3e483bSKonstantin Belousov 858bb9a904SKonstantin Belousov /* 868bb9a904SKonstantin Belousov * Assert that the return value of read(2) and write(2) syscalls fits 878bb9a904SKonstantin Belousov * into a register. If not, an architecture will need to provide the 888bb9a904SKonstantin Belousov * usermode wrappers to reconstruct the result. 898bb9a904SKonstantin Belousov */ 908bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t)); 91526d0bd5SKonstantin Belousov 92a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 93a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 94a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 9555166637SPoul-Henning Kamp 966d8feddaSKonstantin Belousov static int pollout(struct thread *, struct pollfd *, struct pollfd *, 976d8feddaSKonstantin Belousov u_int); 98bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 99ace8398dSJeff Roberson static int pollrescan(struct thread *); 100bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 101ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 102ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 103ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 104bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 105bcd9e0ddSJohn Baldwin off_t, int); 106bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 107bcd9e0ddSJohn Baldwin off_t, int); 108512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 109ace8398dSJeff Roberson static void seltdinit(struct thread *); 110cf5e4fe6SDavide Italiano static int seltdwait(struct thread *, sbintime_t, sbintime_t); 111ace8398dSJeff Roberson static void seltdclear(struct thread *); 112ace8398dSJeff Roberson 113ace8398dSJeff Roberson /* 114ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 115ace8398dSJeff Roberson * 116ace8398dSJeff Roberson * t - protected by st_mtx 117ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 118ace8398dSJeff Roberson */ 119ace8398dSJeff Roberson struct seltd { 120ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 121ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 122ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 123ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 124ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 125ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 126ace8398dSJeff Roberson }; 127ace8398dSJeff Roberson 128ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 129ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 130ace8398dSJeff Roberson 131ace8398dSJeff Roberson /* 132ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 133ace8398dSJeff Roberson * f - protected by sf_mtx 134ace8398dSJeff Roberson */ 135ace8398dSJeff Roberson struct selfd { 136ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 137ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 138ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 139ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 140ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 141ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 142ace8398dSJeff Roberson }; 143ace8398dSJeff Roberson 144ace8398dSJeff Roberson static uma_zone_t selfd_zone; 1452141453eSJeff Roberson static struct mtx_pool *mtxpool_select; 1468fe387abSDmitrij Tejblum 147d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 148df8bae1dSRodney W. Grimes struct read_args { 149df8bae1dSRodney W. Grimes int fd; 150134e06feSBruce Evans void *buf; 151134e06feSBruce Evans size_t nbyte; 152df8bae1dSRodney W. Grimes }; 153d2d3e875SBruce Evans #endif 15426f9a767SRodney W. Grimes int 1558451d0ddSKip Macy sys_read(td, uap) 156b40ce416SJulian Elischer struct thread *td; 157b064d43dSMatthew Dillon struct read_args *uap; 158df8bae1dSRodney W. Grimes { 159bcd9e0ddSJohn Baldwin struct uio auio; 160bcd9e0ddSJohn Baldwin struct iovec aiov; 161279d7226SMatthew Dillon int error; 162df8bae1dSRodney W. Grimes 163526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 164bcd9e0ddSJohn Baldwin return (EINVAL); 165bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 166bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 167bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 168bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 169bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 170bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 171bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 172279d7226SMatthew Dillon return(error); 173df8bae1dSRodney W. Grimes } 174df8bae1dSRodney W. Grimes 175df8bae1dSRodney W. Grimes /* 176bcd9e0ddSJohn Baldwin * Positioned read system call 1774160ccd9SAlan Cox */ 1784160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1794160ccd9SAlan Cox struct pread_args { 1804160ccd9SAlan Cox int fd; 1814160ccd9SAlan Cox void *buf; 1824160ccd9SAlan Cox size_t nbyte; 1838fe387abSDmitrij Tejblum int pad; 1844160ccd9SAlan Cox off_t offset; 1854160ccd9SAlan Cox }; 1864160ccd9SAlan Cox #endif 1874160ccd9SAlan Cox int 1888451d0ddSKip Macy sys_pread(td, uap) 189b40ce416SJulian Elischer struct thread *td; 190b064d43dSMatthew Dillon struct pread_args *uap; 1914160ccd9SAlan Cox { 1924160ccd9SAlan Cox struct uio auio; 1934160ccd9SAlan Cox struct iovec aiov; 194bcd9e0ddSJohn Baldwin int error; 1954160ccd9SAlan Cox 196526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 197bcd9e0ddSJohn Baldwin return (EINVAL); 198bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 199bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 2004160ccd9SAlan Cox auio.uio_iov = &aiov; 2014160ccd9SAlan Cox auio.uio_iovcnt = 1; 202bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 2034160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 204bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, &auio, uap->offset); 2054160ccd9SAlan Cox return(error); 2064160ccd9SAlan Cox } 2074160ccd9SAlan Cox 208c2815ad5SPeter Wemm int 209c2815ad5SPeter Wemm freebsd6_pread(td, uap) 210c2815ad5SPeter Wemm struct thread *td; 211c2815ad5SPeter Wemm struct freebsd6_pread_args *uap; 212c2815ad5SPeter Wemm { 213c2815ad5SPeter Wemm struct pread_args oargs; 214c2815ad5SPeter Wemm 215c2815ad5SPeter Wemm oargs.fd = uap->fd; 216c2815ad5SPeter Wemm oargs.buf = uap->buf; 217c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 218c2815ad5SPeter Wemm oargs.offset = uap->offset; 2198451d0ddSKip Macy return (sys_pread(td, &oargs)); 220c2815ad5SPeter Wemm } 221c2815ad5SPeter Wemm 2224160ccd9SAlan Cox /* 223df8bae1dSRodney W. Grimes * Scatter read system call. 224df8bae1dSRodney W. Grimes */ 225d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 226df8bae1dSRodney W. Grimes struct readv_args { 2277147b19dSBruce Evans int fd; 228df8bae1dSRodney W. Grimes struct iovec *iovp; 229df8bae1dSRodney W. Grimes u_int iovcnt; 230df8bae1dSRodney W. Grimes }; 231d2d3e875SBruce Evans #endif 23226f9a767SRodney W. Grimes int 2338451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap) 234df8bae1dSRodney W. Grimes { 235b88ec951SJohn Baldwin struct uio *auio; 236b88ec951SJohn Baldwin int error; 237b88ec951SJohn Baldwin 238b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 239b88ec951SJohn Baldwin if (error) 240b88ec951SJohn Baldwin return (error); 241b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 242b88ec951SJohn Baldwin free(auio, M_IOV); 243b88ec951SJohn Baldwin return (error); 244b88ec951SJohn Baldwin } 245b88ec951SJohn Baldwin 246b88ec951SJohn Baldwin int 247b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 248b88ec951SJohn Baldwin { 249b064d43dSMatthew Dillon struct file *fp; 2507008be5bSPawel Jakub Dawidek cap_rights_t rights; 251bcd9e0ddSJohn Baldwin int error; 252bcd9e0ddSJohn Baldwin 2537008be5bSPawel Jakub Dawidek error = fget_read(td, fd, cap_rights_init(&rights, CAP_READ), &fp); 254bcd9e0ddSJohn Baldwin if (error) 255bcd9e0ddSJohn Baldwin return (error); 256bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 257bcd9e0ddSJohn Baldwin fdrop(fp, td); 258bcd9e0ddSJohn Baldwin return (error); 259bcd9e0ddSJohn Baldwin } 260bcd9e0ddSJohn Baldwin 261bcd9e0ddSJohn Baldwin /* 262bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 263bcd9e0ddSJohn Baldwin */ 264bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 265bcd9e0ddSJohn Baldwin struct preadv_args { 266bcd9e0ddSJohn Baldwin int fd; 267bcd9e0ddSJohn Baldwin struct iovec *iovp; 268bcd9e0ddSJohn Baldwin u_int iovcnt; 269bcd9e0ddSJohn Baldwin off_t offset; 270bcd9e0ddSJohn Baldwin }; 271bcd9e0ddSJohn Baldwin #endif 272bcd9e0ddSJohn Baldwin int 2738451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap) 274bcd9e0ddSJohn Baldwin { 275bcd9e0ddSJohn Baldwin struct uio *auio; 276bcd9e0ddSJohn Baldwin int error; 277bcd9e0ddSJohn Baldwin 278bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 279bcd9e0ddSJohn Baldwin if (error) 280bcd9e0ddSJohn Baldwin return (error); 281bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 282bcd9e0ddSJohn Baldwin free(auio, M_IOV); 283bcd9e0ddSJohn Baldwin return (error); 284bcd9e0ddSJohn Baldwin } 285bcd9e0ddSJohn Baldwin 286bcd9e0ddSJohn Baldwin int 287bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset) 288bcd9e0ddSJohn Baldwin struct thread *td; 289bcd9e0ddSJohn Baldwin int fd; 290bcd9e0ddSJohn Baldwin struct uio *auio; 291bcd9e0ddSJohn Baldwin off_t offset; 292bcd9e0ddSJohn Baldwin { 293bcd9e0ddSJohn Baldwin struct file *fp; 2947008be5bSPawel Jakub Dawidek cap_rights_t rights; 295bcd9e0ddSJohn Baldwin int error; 296bcd9e0ddSJohn Baldwin 2977008be5bSPawel Jakub Dawidek error = fget_read(td, fd, cap_rights_init(&rights, CAP_PREAD), &fp); 298bcd9e0ddSJohn Baldwin if (error) 299bcd9e0ddSJohn Baldwin return (error); 300bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 301bcd9e0ddSJohn Baldwin error = ESPIPE; 302bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 303bcd9e0ddSJohn Baldwin error = EINVAL; 304bcd9e0ddSJohn Baldwin else 305bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 306bcd9e0ddSJohn Baldwin fdrop(fp, td); 307bcd9e0ddSJohn Baldwin return (error); 308bcd9e0ddSJohn Baldwin } 309bcd9e0ddSJohn Baldwin 310bcd9e0ddSJohn Baldwin /* 311bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 312bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 313bcd9e0ddSJohn Baldwin */ 314bcd9e0ddSJohn Baldwin static int 315bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 316bcd9e0ddSJohn Baldwin struct thread *td; 317bcd9e0ddSJohn Baldwin int fd; 318bcd9e0ddSJohn Baldwin struct file *fp; 319bcd9e0ddSJohn Baldwin struct uio *auio; 320bcd9e0ddSJohn Baldwin off_t offset; 321bcd9e0ddSJohn Baldwin int flags; 322bcd9e0ddSJohn Baldwin { 323bcd9e0ddSJohn Baldwin ssize_t cnt; 32482641acdSAlan Cox int error; 325df8bae1dSRodney W. Grimes #ifdef KTRACE 326552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 327df8bae1dSRodney W. Grimes #endif 328df8bae1dSRodney W. Grimes 3294f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3304f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3314f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3324f8d23d6SPoul-Henning Kamp return(0); 3334f8d23d6SPoul-Henning Kamp } 334552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 335bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 336552afd9cSPoul-Henning Kamp auio->uio_td = td; 337df8bae1dSRodney W. Grimes #ifdef KTRACE 338552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 339552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 340df8bae1dSRodney W. Grimes #endif 341552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 342bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 343552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 344df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 345df8bae1dSRodney W. Grimes error = 0; 346279d7226SMatthew Dillon } 347552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 348df8bae1dSRodney W. Grimes #ifdef KTRACE 349552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 350552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 351b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 352df8bae1dSRodney W. Grimes } 353df8bae1dSRodney W. Grimes #endif 354b40ce416SJulian Elischer td->td_retval[0] = cnt; 355df8bae1dSRodney W. Grimes return (error); 356df8bae1dSRodney W. Grimes } 357df8bae1dSRodney W. Grimes 358d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 359df8bae1dSRodney W. Grimes struct write_args { 360df8bae1dSRodney W. Grimes int fd; 361134e06feSBruce Evans const void *buf; 362134e06feSBruce Evans size_t nbyte; 363df8bae1dSRodney W. Grimes }; 364d2d3e875SBruce Evans #endif 36526f9a767SRodney W. Grimes int 3668451d0ddSKip Macy sys_write(td, uap) 367b40ce416SJulian Elischer struct thread *td; 368b064d43dSMatthew Dillon struct write_args *uap; 369df8bae1dSRodney W. Grimes { 370bcd9e0ddSJohn Baldwin struct uio auio; 371bcd9e0ddSJohn Baldwin struct iovec aiov; 372279d7226SMatthew Dillon int error; 373df8bae1dSRodney W. Grimes 374526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 375bcd9e0ddSJohn Baldwin return (EINVAL); 376bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 377bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 378bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 379bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 380bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 381bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 382bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 383279d7226SMatthew Dillon return(error); 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes 386df8bae1dSRodney W. Grimes /* 3870c14ff0eSRobert Watson * Positioned write system call. 3884160ccd9SAlan Cox */ 3894160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3904160ccd9SAlan Cox struct pwrite_args { 3914160ccd9SAlan Cox int fd; 3924160ccd9SAlan Cox const void *buf; 3934160ccd9SAlan Cox size_t nbyte; 3948fe387abSDmitrij Tejblum int pad; 3954160ccd9SAlan Cox off_t offset; 3964160ccd9SAlan Cox }; 3974160ccd9SAlan Cox #endif 3984160ccd9SAlan Cox int 3998451d0ddSKip Macy sys_pwrite(td, uap) 400b40ce416SJulian Elischer struct thread *td; 401b064d43dSMatthew Dillon struct pwrite_args *uap; 4024160ccd9SAlan Cox { 4034160ccd9SAlan Cox struct uio auio; 4044160ccd9SAlan Cox struct iovec aiov; 405bcd9e0ddSJohn Baldwin int error; 4064160ccd9SAlan Cox 407526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 408bcd9e0ddSJohn Baldwin return (EINVAL); 409bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 410bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 4114160ccd9SAlan Cox auio.uio_iov = &aiov; 4124160ccd9SAlan Cox auio.uio_iovcnt = 1; 413bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 4144160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 415bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, &auio, uap->offset); 4164160ccd9SAlan Cox return(error); 4174160ccd9SAlan Cox } 4184160ccd9SAlan Cox 419c2815ad5SPeter Wemm int 420c2815ad5SPeter Wemm freebsd6_pwrite(td, uap) 421c2815ad5SPeter Wemm struct thread *td; 422c2815ad5SPeter Wemm struct freebsd6_pwrite_args *uap; 423c2815ad5SPeter Wemm { 424c2815ad5SPeter Wemm struct pwrite_args oargs; 425c2815ad5SPeter Wemm 426c2815ad5SPeter Wemm oargs.fd = uap->fd; 427c2815ad5SPeter Wemm oargs.buf = uap->buf; 428c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 429c2815ad5SPeter Wemm oargs.offset = uap->offset; 4308451d0ddSKip Macy return (sys_pwrite(td, &oargs)); 431c2815ad5SPeter Wemm } 432c2815ad5SPeter Wemm 4334160ccd9SAlan Cox /* 4340c14ff0eSRobert Watson * Gather write system call. 435df8bae1dSRodney W. Grimes */ 436d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 437df8bae1dSRodney W. Grimes struct writev_args { 438df8bae1dSRodney W. Grimes int fd; 439df8bae1dSRodney W. Grimes struct iovec *iovp; 440df8bae1dSRodney W. Grimes u_int iovcnt; 441df8bae1dSRodney W. Grimes }; 442d2d3e875SBruce Evans #endif 44326f9a767SRodney W. Grimes int 4448451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap) 445df8bae1dSRodney W. Grimes { 446b88ec951SJohn Baldwin struct uio *auio; 447b88ec951SJohn Baldwin int error; 448b88ec951SJohn Baldwin 449b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 450b88ec951SJohn Baldwin if (error) 451b88ec951SJohn Baldwin return (error); 452b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 453b88ec951SJohn Baldwin free(auio, M_IOV); 454b88ec951SJohn Baldwin return (error); 455b88ec951SJohn Baldwin } 456b88ec951SJohn Baldwin 457b88ec951SJohn Baldwin int 458b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 459b88ec951SJohn Baldwin { 460b064d43dSMatthew Dillon struct file *fp; 4617008be5bSPawel Jakub Dawidek cap_rights_t rights; 462bcd9e0ddSJohn Baldwin int error; 463bcd9e0ddSJohn Baldwin 4647008be5bSPawel Jakub Dawidek error = fget_write(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp); 465bcd9e0ddSJohn Baldwin if (error) 466af56abaaSJohn Baldwin return (error); 467bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 468bcd9e0ddSJohn Baldwin fdrop(fp, td); 469bcd9e0ddSJohn Baldwin return (error); 470bcd9e0ddSJohn Baldwin } 471bcd9e0ddSJohn Baldwin 472bcd9e0ddSJohn Baldwin /* 4730c14ff0eSRobert Watson * Gather positioned write system call. 474bcd9e0ddSJohn Baldwin */ 475bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 476bcd9e0ddSJohn Baldwin struct pwritev_args { 477bcd9e0ddSJohn Baldwin int fd; 478bcd9e0ddSJohn Baldwin struct iovec *iovp; 479bcd9e0ddSJohn Baldwin u_int iovcnt; 480bcd9e0ddSJohn Baldwin off_t offset; 481bcd9e0ddSJohn Baldwin }; 482bcd9e0ddSJohn Baldwin #endif 483bcd9e0ddSJohn Baldwin int 4848451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap) 485bcd9e0ddSJohn Baldwin { 486bcd9e0ddSJohn Baldwin struct uio *auio; 487bcd9e0ddSJohn Baldwin int error; 488bcd9e0ddSJohn Baldwin 489bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 490bcd9e0ddSJohn Baldwin if (error) 491bcd9e0ddSJohn Baldwin return (error); 492bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 493bcd9e0ddSJohn Baldwin free(auio, M_IOV); 494bcd9e0ddSJohn Baldwin return (error); 495bcd9e0ddSJohn Baldwin } 496bcd9e0ddSJohn Baldwin 497bcd9e0ddSJohn Baldwin int 498bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset) 499bcd9e0ddSJohn Baldwin struct thread *td; 500bcd9e0ddSJohn Baldwin struct uio *auio; 501bcd9e0ddSJohn Baldwin int fd; 502bcd9e0ddSJohn Baldwin off_t offset; 503bcd9e0ddSJohn Baldwin { 504bcd9e0ddSJohn Baldwin struct file *fp; 5057008be5bSPawel Jakub Dawidek cap_rights_t rights; 506bcd9e0ddSJohn Baldwin int error; 507bcd9e0ddSJohn Baldwin 5087008be5bSPawel Jakub Dawidek error = fget_write(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp); 509bcd9e0ddSJohn Baldwin if (error) 510af56abaaSJohn Baldwin return (error); 511bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 512bcd9e0ddSJohn Baldwin error = ESPIPE; 513bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 514bcd9e0ddSJohn Baldwin error = EINVAL; 515bcd9e0ddSJohn Baldwin else 516bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 517bcd9e0ddSJohn Baldwin fdrop(fp, td); 518bcd9e0ddSJohn Baldwin return (error); 519bcd9e0ddSJohn Baldwin } 520bcd9e0ddSJohn Baldwin 521bcd9e0ddSJohn Baldwin /* 522bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 523bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 524bcd9e0ddSJohn Baldwin */ 525bcd9e0ddSJohn Baldwin static int 526bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 527bcd9e0ddSJohn Baldwin struct thread *td; 528bcd9e0ddSJohn Baldwin int fd; 529bcd9e0ddSJohn Baldwin struct file *fp; 530bcd9e0ddSJohn Baldwin struct uio *auio; 531bcd9e0ddSJohn Baldwin off_t offset; 532bcd9e0ddSJohn Baldwin int flags; 533bcd9e0ddSJohn Baldwin { 534bcd9e0ddSJohn Baldwin ssize_t cnt; 535552afd9cSPoul-Henning Kamp int error; 536df8bae1dSRodney W. Grimes #ifdef KTRACE 537552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 538df8bae1dSRodney W. Grimes #endif 539df8bae1dSRodney W. Grimes 540552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 541552afd9cSPoul-Henning Kamp auio->uio_td = td; 542bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 543df8bae1dSRodney W. Grimes #ifdef KTRACE 544552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 545552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 546df8bae1dSRodney W. Grimes #endif 547552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 548ad9789f6SKonstantin Belousov if (fp->f_type == DTYPE_VNODE && 549ad9789f6SKonstantin Belousov (fp->f_vnread_flags & FDEVFS_VNODE) == 0) 5509440653dSMatthew Dillon bwillwrite(); 551bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 552552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 553df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 554df8bae1dSRodney W. Grimes error = 0; 555bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5566f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 557b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 5587a6f3d78SJohn Baldwin tdsignal(td, SIGPIPE); 559b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 56019eb87d2SJohn Baldwin } 561df8bae1dSRodney W. Grimes } 562552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 563df8bae1dSRodney W. Grimes #ifdef KTRACE 564552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 565552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 566b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes #endif 569b40ce416SJulian Elischer td->td_retval[0] = cnt; 570df8bae1dSRodney W. Grimes return (error); 571df8bae1dSRodney W. Grimes } 572df8bae1dSRodney W. Grimes 573e4650294SJohn Baldwin /* 574e4650294SJohn Baldwin * Truncate a file given a file descriptor. 575e4650294SJohn Baldwin * 576e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 577e4650294SJohn Baldwin * descriptor isn't writable. 578e4650294SJohn Baldwin */ 579e4650294SJohn Baldwin int 580e4650294SJohn Baldwin kern_ftruncate(td, fd, length) 581e4650294SJohn Baldwin struct thread *td; 582e4650294SJohn Baldwin int fd; 583e4650294SJohn Baldwin off_t length; 584e4650294SJohn Baldwin { 585e4650294SJohn Baldwin struct file *fp; 5867008be5bSPawel Jakub Dawidek cap_rights_t rights; 587e4650294SJohn Baldwin int error; 588e4650294SJohn Baldwin 58914961ba7SRobert Watson AUDIT_ARG_FD(fd); 590e4650294SJohn Baldwin if (length < 0) 591e4650294SJohn Baldwin return (EINVAL); 5927008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp); 593e4650294SJohn Baldwin if (error) 594e4650294SJohn Baldwin return (error); 59514961ba7SRobert Watson AUDIT_ARG_FILE(td->td_proc, fp); 596e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 597e4650294SJohn Baldwin fdrop(fp, td); 598e4650294SJohn Baldwin return (EINVAL); 599e4650294SJohn Baldwin } 600e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 601e4650294SJohn Baldwin fdrop(fp, td); 602e4650294SJohn Baldwin return (error); 603e4650294SJohn Baldwin } 604e4650294SJohn Baldwin 605e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 606e4650294SJohn Baldwin struct ftruncate_args { 607e4650294SJohn Baldwin int fd; 608e4650294SJohn Baldwin int pad; 609e4650294SJohn Baldwin off_t length; 610e4650294SJohn Baldwin }; 611e4650294SJohn Baldwin #endif 612e4650294SJohn Baldwin int 6138451d0ddSKip Macy sys_ftruncate(td, uap) 614e4650294SJohn Baldwin struct thread *td; 615e4650294SJohn Baldwin struct ftruncate_args *uap; 616e4650294SJohn Baldwin { 617e4650294SJohn Baldwin 618e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 619e4650294SJohn Baldwin } 620e4650294SJohn Baldwin 621e4650294SJohn Baldwin #if defined(COMPAT_43) 622e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 623e4650294SJohn Baldwin struct oftruncate_args { 624e4650294SJohn Baldwin int fd; 625e4650294SJohn Baldwin long length; 626e4650294SJohn Baldwin }; 627e4650294SJohn Baldwin #endif 628e4650294SJohn Baldwin int 629e4650294SJohn Baldwin oftruncate(td, uap) 630e4650294SJohn Baldwin struct thread *td; 631e4650294SJohn Baldwin struct oftruncate_args *uap; 632e4650294SJohn Baldwin { 633e4650294SJohn Baldwin 634e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 635e4650294SJohn Baldwin } 636e4650294SJohn Baldwin #endif /* COMPAT_43 */ 637e4650294SJohn Baldwin 638d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 639df8bae1dSRodney W. Grimes struct ioctl_args { 640df8bae1dSRodney W. Grimes int fd; 641069e9bc1SDoug Rabson u_long com; 642df8bae1dSRodney W. Grimes caddr_t data; 643df8bae1dSRodney W. Grimes }; 644d2d3e875SBruce Evans #endif 645df8bae1dSRodney W. Grimes /* ARGSUSED */ 64626f9a767SRodney W. Grimes int 6478451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap) 648df8bae1dSRodney W. Grimes { 6490ecd606bSHans Petter Selasky #ifndef SYS_IOCTL_SMALL_SIZE 6500ecd606bSHans Petter Selasky #define SYS_IOCTL_SMALL_SIZE 128 6510ecd606bSHans Petter Selasky #endif 6520ecd606bSHans Petter Selasky u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(8); 6533e15c66fSPoul-Henning Kamp u_long com; 6549fddcc66SRuslan Ermilov int arg, error; 6553e15c66fSPoul-Henning Kamp u_int size; 6569fddcc66SRuslan Ermilov caddr_t data; 657df8bae1dSRodney W. Grimes 6589fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6599fc6aa06SPoul-Henning Kamp printf( 6609fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 661431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6629fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 6639fc6aa06SPoul-Henning Kamp } 664d9f46233SJohn Baldwin com = uap->com; 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes /* 667df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 668df8bae1dSRodney W. Grimes * copied to/from the user's address space. 669df8bae1dSRodney W. Grimes */ 670df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 671ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 672ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6732de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6742de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6752de92a38SPeter Wemm #else 6762de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6772de92a38SPeter Wemm #endif 6789fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 679426da3bcSAlfred Perlstein return (ENOTTY); 680279d7226SMatthew Dillon 681ca51b19bSPoul-Henning Kamp if (size > 0) { 682715457f6SDavid E. O'Brien if (com & IOC_VOID) { 6839fddcc66SRuslan Ermilov /* Integer argument. */ 6849fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6859fddcc66SRuslan Ermilov data = (void *)&arg; 6869fddcc66SRuslan Ermilov size = 0; 6875cbf44bfSMateusz Guzik } else { 6880ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 689715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 6900ecd606bSHans Petter Selasky else 6910ecd606bSHans Petter Selasky data = smalldata; 6925cbf44bfSMateusz Guzik } 6939fddcc66SRuslan Ermilov } else 6949fddcc66SRuslan Ermilov data = (void *)&uap->data; 695df8bae1dSRodney W. Grimes if (com & IOC_IN) { 696df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 6975cbf44bfSMateusz Guzik if (error != 0) 6985cbf44bfSMateusz Guzik goto out; 699ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 700df8bae1dSRodney W. Grimes /* 701df8bae1dSRodney W. Grimes * Zero the buffer so the user always 702df8bae1dSRodney W. Grimes * gets back something deterministic. 703df8bae1dSRodney W. Grimes */ 704df8bae1dSRodney W. Grimes bzero(data, size); 705279d7226SMatthew Dillon } 706df8bae1dSRodney W. Grimes 707d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 708d9f46233SJohn Baldwin 709d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 710d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 711d9f46233SJohn Baldwin 7125cbf44bfSMateusz Guzik out: 7130ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 7149fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 715d9f46233SJohn Baldwin return (error); 716d9f46233SJohn Baldwin } 717d9f46233SJohn Baldwin 718d9f46233SJohn Baldwin int 719d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 720d9f46233SJohn Baldwin { 721d9f46233SJohn Baldwin struct file *fp; 722d9f46233SJohn Baldwin struct filedesc *fdp; 7237008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES 7247008be5bSPawel Jakub Dawidek cap_rights_t rights; 7257008be5bSPawel Jakub Dawidek #endif 7262609222aSPawel Jakub Dawidek int error, tmp, locked; 727d9f46233SJohn Baldwin 72864c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 72964c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7302609222aSPawel Jakub Dawidek 731d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7322609222aSPawel Jakub Dawidek 733d9f46233SJohn Baldwin switch (com) { 734d9f46233SJohn Baldwin case FIONCLEX: 735d9f46233SJohn Baldwin case FIOCLEX: 7365e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7372609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7382609222aSPawel Jakub Dawidek break; 7392609222aSPawel Jakub Dawidek default: 7402609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7412609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7422609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7432609222aSPawel Jakub Dawidek #else 7442609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7452609222aSPawel Jakub Dawidek #endif 7462609222aSPawel Jakub Dawidek break; 7472609222aSPawel Jakub Dawidek } 7482609222aSPawel Jakub Dawidek 7492609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7502609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7512609222aSPawel Jakub Dawidek error = EBADF; 7522609222aSPawel Jakub Dawidek goto out; 7532609222aSPawel Jakub Dawidek } 7542609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7552609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7562609222aSPawel Jakub Dawidek goto out; 7572609222aSPawel Jakub Dawidek } 7582609222aSPawel Jakub Dawidek fhold(fp); 7592609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 7602609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 7612609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7622609222aSPawel Jakub Dawidek } 7632609222aSPawel Jakub Dawidek #else 7647008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp); 7657008be5bSPawel Jakub Dawidek if (error != 0) { 7662609222aSPawel Jakub Dawidek fp = NULL; 7672609222aSPawel Jakub Dawidek goto out; 7682609222aSPawel Jakub Dawidek } 7692609222aSPawel Jakub Dawidek #endif 7702609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 7712609222aSPawel Jakub Dawidek error = EBADF; 7722609222aSPawel Jakub Dawidek goto out; 7732609222aSPawel Jakub Dawidek } 7742609222aSPawel Jakub Dawidek 7752609222aSPawel Jakub Dawidek switch (com) { 7762609222aSPawel Jakub Dawidek case FIONCLEX: 7772609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 7782609222aSPawel Jakub Dawidek goto out; 7792609222aSPawel Jakub Dawidek case FIOCLEX: 7802609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 781d9f46233SJohn Baldwin goto out; 782d9f46233SJohn Baldwin case FIONBIO: 783bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 784397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 785df8bae1dSRodney W. Grimes else 786397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7878ccf264fSPoul-Henning Kamp data = (void *)&tmp; 788d9f46233SJohn Baldwin break; 789d9f46233SJohn Baldwin case FIOASYNC: 790bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 791397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 792df8bae1dSRodney W. Grimes else 793397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7948ccf264fSPoul-Henning Kamp data = (void *)&tmp; 795d9f46233SJohn Baldwin break; 796df8bae1dSRodney W. Grimes } 7978ccf264fSPoul-Henning Kamp 7988ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 799d9f46233SJohn Baldwin out: 8002609222aSPawel Jakub Dawidek switch (locked) { 8012609222aSPawel Jakub Dawidek case LA_XLOCKED: 8022609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 8032609222aSPawel Jakub Dawidek break; 8042609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 8052609222aSPawel Jakub Dawidek case LA_SLOCKED: 8062609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8072609222aSPawel Jakub Dawidek break; 8082609222aSPawel Jakub Dawidek #endif 8092609222aSPawel Jakub Dawidek default: 8102609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8112609222aSPawel Jakub Dawidek break; 8122609222aSPawel Jakub Dawidek } 8132609222aSPawel Jakub Dawidek if (fp != NULL) 814b40ce416SJulian Elischer fdrop(fp, td); 815df8bae1dSRodney W. Grimes return (error); 816df8bae1dSRodney W. Grimes } 817df8bae1dSRodney W. Grimes 818125dcf8cSKonstantin Belousov int 819125dcf8cSKonstantin Belousov poll_no_poll(int events) 820125dcf8cSKonstantin Belousov { 821125dcf8cSKonstantin Belousov /* 822125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 823125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 824125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 825125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 826125dcf8cSKonstantin Belousov * of specific filesystem implementations. 827125dcf8cSKonstantin Belousov */ 828125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 829125dcf8cSKonstantin Belousov return (POLLNVAL); 830125dcf8cSKonstantin Belousov 831125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 832125dcf8cSKonstantin Belousov } 833125dcf8cSKonstantin Belousov 834066d836bSKonstantin Belousov int 8358451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 836066d836bSKonstantin Belousov { 837066d836bSKonstantin Belousov struct timespec ts; 838066d836bSKonstantin Belousov struct timeval tv, *tvp; 839066d836bSKonstantin Belousov sigset_t set, *uset; 840066d836bSKonstantin Belousov int error; 841066d836bSKonstantin Belousov 842066d836bSKonstantin Belousov if (uap->ts != NULL) { 843066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 844066d836bSKonstantin Belousov if (error != 0) 845066d836bSKonstantin Belousov return (error); 846066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 847066d836bSKonstantin Belousov tvp = &tv; 848066d836bSKonstantin Belousov } else 849066d836bSKonstantin Belousov tvp = NULL; 850066d836bSKonstantin Belousov if (uap->sm != NULL) { 851066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 852066d836bSKonstantin Belousov if (error != 0) 853066d836bSKonstantin Belousov return (error); 854066d836bSKonstantin Belousov uset = &set; 855066d836bSKonstantin Belousov } else 856066d836bSKonstantin Belousov uset = NULL; 857066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 858066d836bSKonstantin Belousov uset, NFDBITS)); 859066d836bSKonstantin Belousov } 860066d836bSKonstantin Belousov 861066d836bSKonstantin Belousov int 862066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 863066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 864066d836bSKonstantin Belousov { 865066d836bSKonstantin Belousov int error; 866066d836bSKonstantin Belousov 867066d836bSKonstantin Belousov if (uset != NULL) { 868066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 869066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 870066d836bSKonstantin Belousov if (error != 0) 871066d836bSKonstantin Belousov return (error); 872066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 873066d836bSKonstantin Belousov /* 874066d836bSKonstantin Belousov * Make sure that ast() is called on return to 875066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 876066d836bSKonstantin Belousov * sigmask. 877066d836bSKonstantin Belousov */ 878066d836bSKonstantin Belousov thread_lock(td); 879066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 880066d836bSKonstantin Belousov thread_unlock(td); 881066d836bSKonstantin Belousov } 882066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 883066d836bSKonstantin Belousov return (error); 884066d836bSKonstantin Belousov } 885066d836bSKonstantin Belousov 886d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 887df8bae1dSRodney W. Grimes struct select_args { 888b08f7993SSujal Patel int nd; 889df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 890df8bae1dSRodney W. Grimes struct timeval *tv; 891df8bae1dSRodney W. Grimes }; 892d2d3e875SBruce Evans #endif 89326f9a767SRodney W. Grimes int 8948451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 895df8bae1dSRodney W. Grimes { 8968f19eb88SIan Dowse struct timeval tv, *tvp; 8978f19eb88SIan Dowse int error; 8988f19eb88SIan Dowse 8998f19eb88SIan Dowse if (uap->tv != NULL) { 9008f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 9018f19eb88SIan Dowse if (error) 9028f19eb88SIan Dowse return (error); 9038f19eb88SIan Dowse tvp = &tv; 9048f19eb88SIan Dowse } else 9058f19eb88SIan Dowse tvp = NULL; 9068f19eb88SIan Dowse 907b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 908b55ef216SKonstantin Belousov NFDBITS)); 9098f19eb88SIan Dowse } 9108f19eb88SIan Dowse 91156be1b9aSKonstantin Belousov /* 91256be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 91356be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 91456be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 91556be1b9aSKonstantin Belousov * 91656be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 91756be1b9aSKonstantin Belousov * 91856be1b9aSKonstantin Belousov * nd is fd_lastfile + 1. 91956be1b9aSKonstantin Belousov */ 92056be1b9aSKonstantin Belousov static int 92156be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 92256be1b9aSKonstantin Belousov { 92356be1b9aSKonstantin Belousov char *addr, *oaddr; 92456be1b9aSKonstantin Belousov int b, i, res; 92556be1b9aSKonstantin Belousov uint8_t bits; 92656be1b9aSKonstantin Belousov 92756be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 92856be1b9aSKonstantin Belousov return (0); 92956be1b9aSKonstantin Belousov 93056be1b9aSKonstantin Belousov oaddr = NULL; 93156be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 93256be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 93356be1b9aSKonstantin Belousov b = i / NBBY; 93456be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 93556be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 93656be1b9aSKonstantin Belousov #else 93756be1b9aSKonstantin Belousov addr = (char *)fd_in; 93856be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 93956be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 94056be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 94156be1b9aSKonstantin Belousov } else { 94256be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 94356be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 94456be1b9aSKonstantin Belousov } 94556be1b9aSKonstantin Belousov #endif 94656be1b9aSKonstantin Belousov if (addr != oaddr) { 94756be1b9aSKonstantin Belousov res = fubyte(addr); 94856be1b9aSKonstantin Belousov if (res == -1) 94956be1b9aSKonstantin Belousov return (EFAULT); 95056be1b9aSKonstantin Belousov oaddr = addr; 95156be1b9aSKonstantin Belousov bits = res; 95256be1b9aSKonstantin Belousov } 95356be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 95456be1b9aSKonstantin Belousov return (EBADF); 95556be1b9aSKonstantin Belousov } 95656be1b9aSKonstantin Belousov return (0); 95756be1b9aSKonstantin Belousov } 95856be1b9aSKonstantin Belousov 9598f19eb88SIan Dowse int 9608f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 961b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 9628f19eb88SIan Dowse { 963426da3bcSAlfred Perlstein struct filedesc *fdp; 964d5e4d7e1SBruce Evans /* 965d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 966d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 967d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 968d5e4d7e1SBruce Evans * of 256. 969d5e4d7e1SBruce Evans */ 970d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 971eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 972cf5e4fe6SDavide Italiano struct timeval rtv; 973cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 974b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 975cf5e4fe6SDavide Italiano int error, lf, ndu; 976df8bae1dSRodney W. Grimes 9778f19eb88SIan Dowse if (nd < 0) 978acbfbfeaSSujal Patel return (EINVAL); 979426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 98056be1b9aSKonstantin Belousov ndu = nd; 98156be1b9aSKonstantin Belousov lf = fdp->fd_lastfile; 98256be1b9aSKonstantin Belousov if (nd > lf + 1) 98356be1b9aSKonstantin Belousov nd = lf + 1; 98456be1b9aSKonstantin Belousov 98556be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 98656be1b9aSKonstantin Belousov if (error != 0) 98756be1b9aSKonstantin Belousov return (error); 98856be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 98956be1b9aSKonstantin Belousov if (error != 0) 99056be1b9aSKonstantin Belousov return (error); 99156be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 99256be1b9aSKonstantin Belousov if (error != 0) 99356be1b9aSKonstantin Belousov return (error); 994b08f7993SSujal Patel 995d5e4d7e1SBruce Evans /* 996d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 997d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 998d5e4d7e1SBruce Evans */ 9998f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 1000d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 1001b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 1002d5e4d7e1SBruce Evans nbufbytes = 0; 10038f19eb88SIan Dowse if (fd_in != NULL) 1004d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10058f19eb88SIan Dowse if (fd_ou != NULL) 1006d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10078f19eb88SIan Dowse if (fd_ex != NULL) 1008d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1009d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1010d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1011d5e4d7e1SBruce Evans else 1012a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1013b08f7993SSujal Patel 1014b08f7993SSujal Patel /* 1015d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1016d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1017d5e4d7e1SBruce Evans * together. 1018b08f7993SSujal Patel */ 1019d5e4d7e1SBruce Evans sbp = selbits; 1020df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1021d5e4d7e1SBruce Evans do { \ 1022841c0c7eSNathan Whitehorn if (name == NULL) { \ 1023d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1024841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1025841c0c7eSNathan Whitehorn } else { \ 1026d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1027d5e4d7e1SBruce Evans obits[x] = sbp; \ 1028d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1029b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1030265fc98fSSeigo Tanimura if (error != 0) \ 1031ace8398dSJeff Roberson goto done; \ 1032b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1033b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1034e04ac2feSJohn Baldwin } \ 1035d5e4d7e1SBruce Evans } while (0) 10368f19eb88SIan Dowse getbits(fd_in, 0); 10378f19eb88SIan Dowse getbits(fd_ou, 1); 10388f19eb88SIan Dowse getbits(fd_ex, 2); 1039df8bae1dSRodney W. Grimes #undef getbits 1040841c0c7eSNathan Whitehorn 1041841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1042841c0c7eSNathan Whitehorn /* 1043841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1044841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1045841c0c7eSNathan Whitehorn * generic. 1046841c0c7eSNathan Whitehorn */ 1047841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1048841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1049841c0c7eSNathan Whitehorn int i; \ 1050841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1051841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1052841c0c7eSNathan Whitehorn } 1053841c0c7eSNathan Whitehorn #else 1054841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1055841c0c7eSNathan Whitehorn #endif 1056841c0c7eSNathan Whitehorn 1057841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1058841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1059841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1060841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1061841c0c7eSNathan Whitehorn 1062d5e4d7e1SBruce Evans if (nbufbytes != 0) 1063d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1064df8bae1dSRodney W. Grimes 1065cf5e4fe6SDavide Italiano precision = 0; 10668f19eb88SIan Dowse if (tvp != NULL) { 1067cf5e4fe6SDavide Italiano rtv = *tvp; 1068cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1069cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1070df8bae1dSRodney W. Grimes error = EINVAL; 1071ace8398dSJeff Roberson goto done; 1072df8bae1dSRodney W. Grimes } 107321a37a71SAlexander Motin if (!timevalisset(&rtv)) 1074980c545dSAlexander Motin asbt = 0; 107521a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1076cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1077cf5e4fe6SDavide Italiano precision = rsbt; 1078cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1079cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1080cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 10814bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1082cf5e4fe6SDavide Italiano asbt += rsbt; 108321a37a71SAlexander Motin else 1084980c545dSAlexander Motin asbt = -1; 1085980c545dSAlexander Motin } else 1086980c545dSAlexander Motin asbt = -1; 1087cf5e4fe6SDavide Italiano } else 1088cf5e4fe6SDavide Italiano asbt = -1; 1089ace8398dSJeff Roberson seltdinit(td); 1090ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1091ace8398dSJeff Roberson for (;;) { 10928f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1093ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1094ace8398dSJeff Roberson break; 1095cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1096ace8398dSJeff Roberson if (error) 1097ace8398dSJeff Roberson break; 1098ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1099ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1100ace8398dSJeff Roberson break; 110185f190e4SAlfred Perlstein } 1102ace8398dSJeff Roberson seltdclear(td); 1103265fc98fSSeigo Tanimura 1104df8bae1dSRodney W. Grimes done: 1105df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1106df8bae1dSRodney W. Grimes if (error == ERESTART) 1107df8bae1dSRodney W. Grimes error = EINTR; 1108df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1109df8bae1dSRodney W. Grimes error = 0; 1110841c0c7eSNathan Whitehorn 1111841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1112841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1113841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1114841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1115841c0c7eSNathan Whitehorn #undef swizzle_fdset 1116841c0c7eSNathan Whitehorn 1117df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1118b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1119df8bae1dSRodney W. Grimes error = error2; 1120df8bae1dSRodney W. Grimes if (error == 0) { 1121df8bae1dSRodney W. Grimes int error2; 1122df8bae1dSRodney W. Grimes 11238f19eb88SIan Dowse putbits(fd_in, 0); 11248f19eb88SIan Dowse putbits(fd_ou, 1); 11258f19eb88SIan Dowse putbits(fd_ex, 2); 1126df8bae1dSRodney W. Grimes #undef putbits 1127df8bae1dSRodney W. Grimes } 1128d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1129d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1130ad2edad9SMatthew Dillon 1131df8bae1dSRodney W. Grimes return (error); 1132df8bae1dSRodney W. Grimes } 113311b763dfSJeff Roberson /* 113411b763dfSJeff Roberson * Convert a select bit set to poll flags. 1135748b9df6SJeff Roberson * 113611b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 113711b763dfSJeff Roberson * return this as a set bit in any set. 113811b763dfSJeff Roberson */ 113911b763dfSJeff Roberson static int select_flags[3] = { 114011b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 114111b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 114261e53a38SKonstantin Belousov POLLRDBAND | POLLERR 114311b763dfSJeff Roberson }; 114411b763dfSJeff Roberson 114511b763dfSJeff Roberson /* 114611b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 114711b763dfSJeff Roberson * bit position in the fd_mask array. 114811b763dfSJeff Roberson */ 114911b763dfSJeff Roberson static __inline int 115060b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 115111b763dfSJeff Roberson { 115211b763dfSJeff Roberson int flags; 115311b763dfSJeff Roberson int msk; 115411b763dfSJeff Roberson 115511b763dfSJeff Roberson flags = 0; 115611b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 115711b763dfSJeff Roberson if (ibits[msk] == NULL) 115811b763dfSJeff Roberson continue; 115960b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 116011b763dfSJeff Roberson continue; 116111b763dfSJeff Roberson flags |= select_flags[msk]; 116211b763dfSJeff Roberson } 116311b763dfSJeff Roberson return (flags); 116411b763dfSJeff Roberson } 116511b763dfSJeff Roberson 116611b763dfSJeff Roberson /* 116711b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 116811b763dfSJeff Roberson * input bits originally requested. 116911b763dfSJeff Roberson */ 117011b763dfSJeff Roberson static __inline int 117111b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 117211b763dfSJeff Roberson { 117311b763dfSJeff Roberson int msk; 117411b763dfSJeff Roberson int n; 117511b763dfSJeff Roberson 117611b763dfSJeff Roberson n = 0; 117711b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 117811b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 117911b763dfSJeff Roberson continue; 118011b763dfSJeff Roberson if (ibits[msk] == NULL) 118111b763dfSJeff Roberson continue; 118211b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 118311b763dfSJeff Roberson continue; 118411b763dfSJeff Roberson /* 118511b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 118611b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 118711b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 118811b763dfSJeff Roberson * call selsetbits twice as a result. 118911b763dfSJeff Roberson */ 119011b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 119111b763dfSJeff Roberson continue; 119211b763dfSJeff Roberson obits[msk][idx] |= bit; 119311b763dfSJeff Roberson n++; 119411b763dfSJeff Roberson } 119511b763dfSJeff Roberson 119611b763dfSJeff Roberson return (n); 119711b763dfSJeff Roberson } 1198df8bae1dSRodney W. Grimes 1199a9d2f8d8SRobert Watson static __inline int 1200a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1201a9d2f8d8SRobert Watson { 12027008be5bSPawel Jakub Dawidek cap_rights_t rights; 1203a9d2f8d8SRobert Watson 1204ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT); 1205ed5848c8SPawel Jakub Dawidek 1206ed5848c8SPawel Jakub Dawidek return (fget_unlocked(fdp, fd, &rights, 0, fpp, NULL)); 1207a9d2f8d8SRobert Watson } 1208a9d2f8d8SRobert Watson 1209ace8398dSJeff Roberson /* 1210ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1211ace8398dSJeff Roberson * completion. 1212ace8398dSJeff Roberson */ 1213ace8398dSJeff Roberson static int 1214ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1215ace8398dSJeff Roberson { 121611b763dfSJeff Roberson struct filedesc *fdp; 121711b763dfSJeff Roberson struct selinfo *si; 1218ace8398dSJeff Roberson struct seltd *stp; 1219ace8398dSJeff Roberson struct selfd *sfp; 1220ace8398dSJeff Roberson struct selfd *sfn; 1221ace8398dSJeff Roberson struct file *fp; 12229cdacff1SJeff Roberson fd_mask bit; 12239cdacff1SJeff Roberson int fd, ev, n, idx; 1224a9d2f8d8SRobert Watson int error; 1225ace8398dSJeff Roberson 122611b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1227ace8398dSJeff Roberson stp = td->td_sel; 122811b763dfSJeff Roberson n = 0; 1229ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1230ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1231ace8398dSJeff Roberson si = sfp->sf_si; 1232ace8398dSJeff Roberson selfdfree(stp, sfp); 1233ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1234ace8398dSJeff Roberson if (si != NULL) 1235ace8398dSJeff Roberson continue; 1236a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1237a9d2f8d8SRobert Watson if (error) 1238a9d2f8d8SRobert Watson return (error); 123911b763dfSJeff Roberson idx = fd / NFDBITS; 12409cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 124111b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1242bf422e5fSJeff Roberson fdrop(fp, td); 124311b763dfSJeff Roberson if (ev != 0) 124411b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1245ace8398dSJeff Roberson } 1246ace8398dSJeff Roberson stp->st_flags = 0; 1247ace8398dSJeff Roberson td->td_retval[0] = n; 1248ace8398dSJeff Roberson return (0); 1249ace8398dSJeff Roberson } 1250ace8398dSJeff Roberson 1251ace8398dSJeff Roberson /* 1252ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1253ace8398dSJeff Roberson * each selinfo. 1254ace8398dSJeff Roberson */ 1255265fc98fSSeigo Tanimura static int 1256b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 1257b40ce416SJulian Elischer struct thread *td; 1258b08f7993SSujal Patel fd_mask **ibits, **obits; 1259cb226aaaSPoul-Henning Kamp int nfd; 1260df8bae1dSRodney W. Grimes { 126111b763dfSJeff Roberson struct filedesc *fdp; 1262df8bae1dSRodney W. Grimes struct file *fp; 12639cdacff1SJeff Roberson fd_mask bit; 126411b763dfSJeff Roberson int ev, flags, end, fd; 12659cdacff1SJeff Roberson int n, idx; 1266a9d2f8d8SRobert Watson int error; 1267df8bae1dSRodney W. Grimes 126811b763dfSJeff Roberson fdp = td->td_proc->p_fd; 126911b763dfSJeff Roberson n = 0; 12709cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 127111b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 127211b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 127311b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 127411b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 127511b763dfSJeff Roberson if (flags == 0) 1276f082218cSPeter Wemm continue; 1277a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1278a9d2f8d8SRobert Watson if (error) 1279a9d2f8d8SRobert Watson return (error); 1280ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 128111b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1282bf422e5fSJeff Roberson fdrop(fp, td); 128311b763dfSJeff Roberson if (ev != 0) 128411b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1285df8bae1dSRodney W. Grimes } 1286df8bae1dSRodney W. Grimes } 128711b763dfSJeff Roberson 1288b40ce416SJulian Elischer td->td_retval[0] = n; 1289df8bae1dSRodney W. Grimes return (0); 1290df8bae1dSRodney W. Grimes } 1291df8bae1dSRodney W. Grimes 129242d11757SPeter Wemm int 1293*186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap) 1294*186d9c34SDmitry Chagin { 1295*186d9c34SDmitry Chagin struct timespec ts, *tsp; 1296*186d9c34SDmitry Chagin 1297*186d9c34SDmitry Chagin if (uap->timeout != INFTIM) { 1298*186d9c34SDmitry Chagin if (uap->timeout < 0) 1299*186d9c34SDmitry Chagin return (EINVAL); 1300*186d9c34SDmitry Chagin ts.tv_sec = uap->timeout / 1000; 1301*186d9c34SDmitry Chagin ts.tv_nsec = (uap->timeout % 1000) * 1000000; 1302*186d9c34SDmitry Chagin tsp = &ts; 1303*186d9c34SDmitry Chagin } else 1304*186d9c34SDmitry Chagin tsp = NULL; 1305*186d9c34SDmitry Chagin 1306*186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); 1307*186d9c34SDmitry Chagin } 1308*186d9c34SDmitry Chagin 1309*186d9c34SDmitry Chagin int 1310*186d9c34SDmitry Chagin kern_poll(struct thread *td, struct pollfd *fds, u_int nfds, 1311*186d9c34SDmitry Chagin struct timespec *tsp, sigset_t *uset) 131242d11757SPeter Wemm { 13132580f4e5SAndre Oppermann struct pollfd *bits; 13142580f4e5SAndre Oppermann struct pollfd smallbits[32]; 1315*186d9c34SDmitry Chagin sbintime_t sbt, precision, tmp; 1316*186d9c34SDmitry Chagin time_t over; 1317*186d9c34SDmitry Chagin struct timespec ts; 1318cf5e4fe6SDavide Italiano int error; 131942d11757SPeter Wemm size_t ni; 132042d11757SPeter Wemm 1321*186d9c34SDmitry Chagin precision = 0; 1322*186d9c34SDmitry Chagin if (tsp != NULL) { 1323*186d9c34SDmitry Chagin if (tsp->tv_sec < 0) 1324*186d9c34SDmitry Chagin return (EINVAL); 1325*186d9c34SDmitry Chagin if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000) 1326*186d9c34SDmitry Chagin return (EINVAL); 1327*186d9c34SDmitry Chagin if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1328*186d9c34SDmitry Chagin sbt = 0; 1329*186d9c34SDmitry Chagin else { 1330*186d9c34SDmitry Chagin ts = *tsp; 1331*186d9c34SDmitry Chagin if (ts.tv_sec > INT32_MAX / 2) { 1332*186d9c34SDmitry Chagin over = ts.tv_sec - INT32_MAX / 2; 1333*186d9c34SDmitry Chagin ts.tv_sec -= over; 1334*186d9c34SDmitry Chagin } else 1335*186d9c34SDmitry Chagin over = 0; 1336*186d9c34SDmitry Chagin tmp = tstosbt(ts); 1337*186d9c34SDmitry Chagin precision = tmp; 1338*186d9c34SDmitry Chagin precision >>= tc_precexp; 1339*186d9c34SDmitry Chagin if (TIMESEL(&sbt, tmp)) 1340*186d9c34SDmitry Chagin sbt += tc_tick_sbt; 1341*186d9c34SDmitry Chagin sbt += tmp; 1342*186d9c34SDmitry Chagin } 1343*186d9c34SDmitry Chagin } else 1344*186d9c34SDmitry Chagin sbt = -1; 1345*186d9c34SDmitry Chagin 1346374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1347ace8398dSJeff Roberson return (EINVAL); 134889b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 134942d11757SPeter Wemm if (ni > sizeof(smallbits)) 1350a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 135142d11757SPeter Wemm else 135242d11757SPeter Wemm bits = smallbits; 1353*186d9c34SDmitry Chagin error = copyin(fds, bits, ni); 135442d11757SPeter Wemm if (error) 1355ace8398dSJeff Roberson goto done; 1356*186d9c34SDmitry Chagin 1357*186d9c34SDmitry Chagin if (uset != NULL) { 1358*186d9c34SDmitry Chagin error = kern_sigprocmask(td, SIG_SETMASK, uset, 1359*186d9c34SDmitry Chagin &td->td_oldsigmask, 0); 1360*186d9c34SDmitry Chagin if (error) 1361ace8398dSJeff Roberson goto done; 1362*186d9c34SDmitry Chagin td->td_pflags |= TDP_OLDMASK; 1363*186d9c34SDmitry Chagin /* 1364*186d9c34SDmitry Chagin * Make sure that ast() is called on return to 1365*186d9c34SDmitry Chagin * usermode and TDP_OLDMASK is cleared, restoring old 1366*186d9c34SDmitry Chagin * sigmask. 1367*186d9c34SDmitry Chagin */ 1368*186d9c34SDmitry Chagin thread_lock(td); 1369*186d9c34SDmitry Chagin td->td_flags |= TDF_ASTPENDING; 1370*186d9c34SDmitry Chagin thread_unlock(td); 137142d11757SPeter Wemm } 1372*186d9c34SDmitry Chagin 1373ace8398dSJeff Roberson seltdinit(td); 1374ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1375ace8398dSJeff Roberson for (;;) { 13762580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 1377ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1378ace8398dSJeff Roberson break; 1379*186d9c34SDmitry Chagin error = seltdwait(td, sbt, precision); 1380ace8398dSJeff Roberson if (error) 1381ace8398dSJeff Roberson break; 1382ace8398dSJeff Roberson error = pollrescan(td); 1383ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1384ace8398dSJeff Roberson break; 138585f190e4SAlfred Perlstein } 1386ace8398dSJeff Roberson seltdclear(td); 1387265fc98fSSeigo Tanimura 138842d11757SPeter Wemm done: 138942d11757SPeter Wemm /* poll is not restarted after signals... */ 139042d11757SPeter Wemm if (error == ERESTART) 139142d11757SPeter Wemm error = EINTR; 139242d11757SPeter Wemm if (error == EWOULDBLOCK) 139342d11757SPeter Wemm error = 0; 139442d11757SPeter Wemm if (error == 0) { 1395*186d9c34SDmitry Chagin error = pollout(td, bits, fds, nfds); 139642d11757SPeter Wemm if (error) 139742d11757SPeter Wemm goto out; 139842d11757SPeter Wemm } 139942d11757SPeter Wemm out: 140042d11757SPeter Wemm if (ni > sizeof(smallbits)) 140142d11757SPeter Wemm free(bits, M_TEMP); 140242d11757SPeter Wemm return (error); 140342d11757SPeter Wemm } 140442d11757SPeter Wemm 1405*186d9c34SDmitry Chagin int 1406*186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap) 1407*186d9c34SDmitry Chagin { 1408*186d9c34SDmitry Chagin struct timespec ts, *tsp; 1409*186d9c34SDmitry Chagin sigset_t set, *ssp; 1410*186d9c34SDmitry Chagin int error; 1411*186d9c34SDmitry Chagin 1412*186d9c34SDmitry Chagin if (uap->ts != NULL) { 1413*186d9c34SDmitry Chagin error = copyin(uap->ts, &ts, sizeof(ts)); 1414*186d9c34SDmitry Chagin if (error) 1415*186d9c34SDmitry Chagin return (error); 1416*186d9c34SDmitry Chagin tsp = &ts; 1417*186d9c34SDmitry Chagin } else 1418*186d9c34SDmitry Chagin tsp = NULL; 1419*186d9c34SDmitry Chagin if (uap->set != NULL) { 1420*186d9c34SDmitry Chagin error = copyin(uap->set, &set, sizeof(set)); 1421*186d9c34SDmitry Chagin if (error) 1422*186d9c34SDmitry Chagin return (error); 1423*186d9c34SDmitry Chagin ssp = &set; 1424*186d9c34SDmitry Chagin } else 1425*186d9c34SDmitry Chagin ssp = NULL; 1426*186d9c34SDmitry Chagin /* 1427*186d9c34SDmitry Chagin * fds is still a pointer to user space. kern_poll() will 1428*186d9c34SDmitry Chagin * take care of copyin that array to the kernel space. 1429*186d9c34SDmitry Chagin */ 1430*186d9c34SDmitry Chagin 1431*186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 1432*186d9c34SDmitry Chagin } 1433*186d9c34SDmitry Chagin 143442d11757SPeter Wemm static int 1435ace8398dSJeff Roberson pollrescan(struct thread *td) 1436ace8398dSJeff Roberson { 1437ace8398dSJeff Roberson struct seltd *stp; 1438ace8398dSJeff Roberson struct selfd *sfp; 1439ace8398dSJeff Roberson struct selfd *sfn; 1440ace8398dSJeff Roberson struct selinfo *si; 1441ace8398dSJeff Roberson struct filedesc *fdp; 1442ace8398dSJeff Roberson struct file *fp; 1443ace8398dSJeff Roberson struct pollfd *fd; 14448e076effSSean Bruno #ifdef CAPABILITIES 14457008be5bSPawel Jakub Dawidek cap_rights_t rights; 14468e076effSSean Bruno #endif 1447ace8398dSJeff Roberson int n; 1448ace8398dSJeff Roberson 1449ace8398dSJeff Roberson n = 0; 1450ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1451ace8398dSJeff Roberson stp = td->td_sel; 1452ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1453ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1454ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1455ace8398dSJeff Roberson si = sfp->sf_si; 1456ace8398dSJeff Roberson selfdfree(stp, sfp); 1457ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1458ace8398dSJeff Roberson if (si != NULL) 1459ace8398dSJeff Roberson continue; 14602609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fd->fd].fde_file; 1461d6f72489SJonathan Anderson #ifdef CAPABILITIES 14622609222aSPawel Jakub Dawidek if (fp == NULL || 14637008be5bSPawel Jakub Dawidek cap_check(cap_rights(fdp, fd->fd), 1464ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1465d6f72489SJonathan Anderson #else 14662609222aSPawel Jakub Dawidek if (fp == NULL) 1467d6f72489SJonathan Anderson #endif 14682609222aSPawel Jakub Dawidek { 1469ace8398dSJeff Roberson fd->revents = POLLNVAL; 1470ace8398dSJeff Roberson n++; 1471ace8398dSJeff Roberson continue; 1472ace8398dSJeff Roberson } 1473d6f72489SJonathan Anderson 1474ace8398dSJeff Roberson /* 1475ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1476ace8398dSJeff Roberson * POLLERR if appropriate. 1477ace8398dSJeff Roberson */ 1478ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1479ace8398dSJeff Roberson if (fd->revents != 0) 1480ace8398dSJeff Roberson n++; 1481ace8398dSJeff Roberson } 1482ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1483ace8398dSJeff Roberson stp->st_flags = 0; 1484ace8398dSJeff Roberson td->td_retval[0] = n; 1485ace8398dSJeff Roberson return (0); 1486ace8398dSJeff Roberson } 1487ace8398dSJeff Roberson 1488ace8398dSJeff Roberson 1489ace8398dSJeff Roberson static int 14906d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd) 14916d8feddaSKonstantin Belousov struct thread *td; 1492ae81968fSRobert Watson struct pollfd *fds; 1493ae81968fSRobert Watson struct pollfd *ufds; 1494ae81968fSRobert Watson u_int nfd; 1495ae81968fSRobert Watson { 1496ae81968fSRobert Watson int error = 0; 1497ae81968fSRobert Watson u_int i = 0; 14986d8feddaSKonstantin Belousov u_int n = 0; 1499ae81968fSRobert Watson 1500ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1501ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1502ae81968fSRobert Watson sizeof(ufds->revents)); 1503ae81968fSRobert Watson if (error) 1504ae81968fSRobert Watson return (error); 15056d8feddaSKonstantin Belousov if (fds->revents != 0) 15066d8feddaSKonstantin Belousov n++; 1507ae81968fSRobert Watson fds++; 1508ae81968fSRobert Watson ufds++; 1509ae81968fSRobert Watson } 15106d8feddaSKonstantin Belousov td->td_retval[0] = n; 1511ae81968fSRobert Watson return (0); 1512ae81968fSRobert Watson } 1513ae81968fSRobert Watson 1514ae81968fSRobert Watson static int 1515b40ce416SJulian Elischer pollscan(td, fds, nfd) 1516b40ce416SJulian Elischer struct thread *td; 151742d11757SPeter Wemm struct pollfd *fds; 1518ea0237edSJonathan Lemon u_int nfd; 151942d11757SPeter Wemm { 1520ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 152142d11757SPeter Wemm struct file *fp; 15228e076effSSean Bruno #ifdef CAPABILITIES 15237008be5bSPawel Jakub Dawidek cap_rights_t rights; 15248e076effSSean Bruno #endif 15252609222aSPawel Jakub Dawidek int i, n = 0; 152642d11757SPeter Wemm 15275e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1528eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1529adf87ab0SMateusz Guzik if (fds->fd > fdp->fd_lastfile) { 153042d11757SPeter Wemm fds->revents = POLLNVAL; 153142d11757SPeter Wemm n++; 1532337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1533337c9691SJordan K. Hubbard fds->revents = 0; 153442d11757SPeter Wemm } else { 15352609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fds->fd].fde_file; 1536d6f72489SJonathan Anderson #ifdef CAPABILITIES 15372609222aSPawel Jakub Dawidek if (fp == NULL || 15382609222aSPawel Jakub Dawidek cap_check(cap_rights(fdp, fds->fd), 1539ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1540d6f72489SJonathan Anderson #else 15412609222aSPawel Jakub Dawidek if (fp == NULL) 1542d6f72489SJonathan Anderson #endif 15432609222aSPawel Jakub Dawidek { 154442d11757SPeter Wemm fds->revents = POLLNVAL; 154542d11757SPeter Wemm n++; 154642d11757SPeter Wemm } else { 15472087c896SBruce Evans /* 15482087c896SBruce Evans * Note: backend also returns POLLHUP and 15492087c896SBruce Evans * POLLERR if appropriate. 15502087c896SBruce Evans */ 1551ace8398dSJeff Roberson selfdalloc(td, fds); 155213ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1553ea6027a8SRobert Watson td->td_ucred, td); 1554f2159cc7SKonstantin Belousov /* 1555f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1556f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1557f2159cc7SKonstantin Belousov */ 1558f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1559f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1560f2159cc7SKonstantin Belousov 156142d11757SPeter Wemm if (fds->revents != 0) 156242d11757SPeter Wemm n++; 156342d11757SPeter Wemm } 156442d11757SPeter Wemm } 156542d11757SPeter Wemm } 15665e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1567b40ce416SJulian Elischer td->td_retval[0] = n; 156842d11757SPeter Wemm return (0); 156942d11757SPeter Wemm } 157042d11757SPeter Wemm 157142d11757SPeter Wemm /* 157242d11757SPeter Wemm * OpenBSD poll system call. 15730c14ff0eSRobert Watson * 157442d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 157542d11757SPeter Wemm */ 157642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 157742d11757SPeter Wemm struct openbsd_poll_args { 157842d11757SPeter Wemm struct pollfd *fds; 157942d11757SPeter Wemm u_int nfds; 158042d11757SPeter Wemm int timeout; 158142d11757SPeter Wemm }; 158242d11757SPeter Wemm #endif 158342d11757SPeter Wemm int 15848451d0ddSKip Macy sys_openbsd_poll(td, uap) 1585b40ce416SJulian Elischer register struct thread *td; 158642d11757SPeter Wemm register struct openbsd_poll_args *uap; 158742d11757SPeter Wemm { 15888451d0ddSKip Macy return (sys_poll(td, (struct poll_args *)uap)); 158942d11757SPeter Wemm } 159042d11757SPeter Wemm 159185f190e4SAlfred Perlstein /* 1592237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1593237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1594237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1595237abf0cSDavide Italiano * determine which events fired. 1596237abf0cSDavide Italiano */ 1597237abf0cSDavide Italiano int 1598237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1599237abf0cSDavide Italiano { 1600237abf0cSDavide Italiano struct timeval rtv; 1601237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1602237abf0cSDavide Italiano int error; 1603237abf0cSDavide Italiano 160476665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1605237abf0cSDavide Italiano if (tvp != NULL) { 1606237abf0cSDavide Italiano rtv = *tvp; 1607237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1608237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1609237abf0cSDavide Italiano return (EINVAL); 1610237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1611237abf0cSDavide Italiano asbt = 0; 1612237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1613237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1614237abf0cSDavide Italiano precision = rsbt; 1615237abf0cSDavide Italiano precision >>= tc_precexp; 1616237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1617237abf0cSDavide Italiano asbt += tc_tick_sbt; 16184bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1619237abf0cSDavide Italiano asbt += rsbt; 1620237abf0cSDavide Italiano else 1621237abf0cSDavide Italiano asbt = -1; 1622237abf0cSDavide Italiano } else 1623237abf0cSDavide Italiano asbt = -1; 1624237abf0cSDavide Italiano } else 1625237abf0cSDavide Italiano asbt = -1; 1626237abf0cSDavide Italiano seltdinit(td); 1627237abf0cSDavide Italiano /* 1628237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1629237abf0cSDavide Italiano */ 1630237abf0cSDavide Italiano for (;;) { 1631237abf0cSDavide Italiano selfdalloc(td, NULL); 1632237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1633237abf0cSDavide Italiano /* error here is actually the ready events. */ 1634237abf0cSDavide Italiano if (error) 1635237abf0cSDavide Italiano return (0); 1636237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1637237abf0cSDavide Italiano if (error) 1638237abf0cSDavide Italiano break; 1639237abf0cSDavide Italiano } 1640237abf0cSDavide Italiano seltdclear(td); 1641237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1642237abf0cSDavide Italiano if (error == ERESTART) 1643237abf0cSDavide Italiano error = 0; 1644237abf0cSDavide Italiano return (error); 1645237abf0cSDavide Italiano } 1646237abf0cSDavide Italiano 1647237abf0cSDavide Italiano /* 1648ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1649ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1650ace8398dSJeff Roberson */ 1651ace8398dSJeff Roberson static void 1652ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1653ace8398dSJeff Roberson { 1654ace8398dSJeff Roberson struct seltd *stp; 1655ace8398dSJeff Roberson 1656ace8398dSJeff Roberson stp = td->td_sel; 1657ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1658ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1659ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1660ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1661ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1662ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1663ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1664ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1665ace8398dSJeff Roberson } 1666ace8398dSJeff Roberson 1667ace8398dSJeff Roberson static void 1668ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1669ace8398dSJeff Roberson { 1670ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 167173f2e5f7SMateusz Guzik if (sfp->sf_si != NULL) { 1672ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1673ffc5ce7bSMateusz Guzik if (sfp->sf_si != NULL) 1674ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1675ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 167673f2e5f7SMateusz Guzik } 1677ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 167885f190e4SAlfred Perlstein } 167985f190e4SAlfred Perlstein 16806aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ 16816aba400aSAttilio Rao void 16826aba400aSAttilio Rao seldrain(sip) 16836aba400aSAttilio Rao struct selinfo *sip; 16846aba400aSAttilio Rao { 16856aba400aSAttilio Rao 16866aba400aSAttilio Rao /* 16876aba400aSAttilio Rao * This feature is already provided by doselwakeup(), thus it is 16886aba400aSAttilio Rao * enough to go for it. 16896aba400aSAttilio Rao * Eventually, the context, should take care to avoid races 16906aba400aSAttilio Rao * between thread calling select()/poll() and file descriptor 16916aba400aSAttilio Rao * detaching, but, again, the races are just the same as 16926aba400aSAttilio Rao * selwakeup(). 16936aba400aSAttilio Rao */ 16946aba400aSAttilio Rao doselwakeup(sip, -1); 16956aba400aSAttilio Rao } 16966aba400aSAttilio Rao 1697df8bae1dSRodney W. Grimes /* 1698df8bae1dSRodney W. Grimes * Record a select request. 1699df8bae1dSRodney W. Grimes */ 1700df8bae1dSRodney W. Grimes void 1701df8bae1dSRodney W. Grimes selrecord(selector, sip) 1702b40ce416SJulian Elischer struct thread *selector; 1703df8bae1dSRodney W. Grimes struct selinfo *sip; 1704df8bae1dSRodney W. Grimes { 1705ace8398dSJeff Roberson struct selfd *sfp; 1706ace8398dSJeff Roberson struct seltd *stp; 1707ace8398dSJeff Roberson struct mtx *mtxp; 1708df8bae1dSRodney W. Grimes 1709ace8398dSJeff Roberson stp = selector->td_sel; 171085f190e4SAlfred Perlstein /* 1711ace8398dSJeff Roberson * Don't record when doing a rescan. 171285f190e4SAlfred Perlstein */ 1713ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1714ace8398dSJeff Roberson return; 1715ace8398dSJeff Roberson /* 1716ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1717ace8398dSJeff Roberson */ 1718ace8398dSJeff Roberson sfp = NULL; 1719ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1720ace8398dSJeff Roberson stp->st_free1 = NULL; 1721ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1722ace8398dSJeff Roberson stp->st_free2 = NULL; 1723ace8398dSJeff Roberson else 1724ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 17252141453eSJeff Roberson mtxp = sip->si_mtx; 17262141453eSJeff Roberson if (mtxp == NULL) 17272141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1728ace8398dSJeff Roberson /* 1729ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1730ace8398dSJeff Roberson */ 1731ace8398dSJeff Roberson sfp->sf_si = sip; 1732ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1733ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1734ace8398dSJeff Roberson /* 1735ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1736ace8398dSJeff Roberson */ 1737ace8398dSJeff Roberson mtx_lock(mtxp); 1738ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1739ace8398dSJeff Roberson sip->si_mtx = mtxp; 1740ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 174185f190e4SAlfred Perlstein } 1742ace8398dSJeff Roberson /* 1743ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1744ace8398dSJeff Roberson */ 1745ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1746ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1747df8bae1dSRodney W. Grimes } 1748df8bae1dSRodney W. Grimes 1749512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1750df8bae1dSRodney W. Grimes void 1751df8bae1dSRodney W. Grimes selwakeup(sip) 175285f190e4SAlfred Perlstein struct selinfo *sip; 1753df8bae1dSRodney W. Grimes { 1754512824f8SSeigo Tanimura doselwakeup(sip, -1); 1755512824f8SSeigo Tanimura } 1756512824f8SSeigo Tanimura 1757512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1758512824f8SSeigo Tanimura void 1759512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1760512824f8SSeigo Tanimura struct selinfo *sip; 1761512824f8SSeigo Tanimura int pri; 1762512824f8SSeigo Tanimura { 1763512824f8SSeigo Tanimura doselwakeup(sip, pri); 1764512824f8SSeigo Tanimura } 1765512824f8SSeigo Tanimura 1766512824f8SSeigo Tanimura /* 1767512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1768512824f8SSeigo Tanimura */ 1769512824f8SSeigo Tanimura static void 1770512824f8SSeigo Tanimura doselwakeup(sip, pri) 1771512824f8SSeigo Tanimura struct selinfo *sip; 1772512824f8SSeigo Tanimura int pri; 1773512824f8SSeigo Tanimura { 1774ace8398dSJeff Roberson struct selfd *sfp; 1775ace8398dSJeff Roberson struct selfd *sfn; 1776ace8398dSJeff Roberson struct seltd *stp; 1777df8bae1dSRodney W. Grimes 1778ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1779ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1780b40ce416SJulian Elischer return; 1781ace8398dSJeff Roberson /* 1782ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1783ace8398dSJeff Roberson */ 1784ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1785ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1786ace8398dSJeff Roberson /* 1787ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1788ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1789ace8398dSJeff Roberson */ 1790ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1791ace8398dSJeff Roberson sfp->sf_si = NULL; 1792ace8398dSJeff Roberson stp = sfp->sf_td; 1793ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1794ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1795ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1796ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1797b40ce416SJulian Elischer } 1798ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1799ace8398dSJeff Roberson } 1800ace8398dSJeff Roberson 1801ace8398dSJeff Roberson static void 1802ace8398dSJeff Roberson seltdinit(struct thread *td) 1803ace8398dSJeff Roberson { 1804ace8398dSJeff Roberson struct seltd *stp; 1805ace8398dSJeff Roberson 1806ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1807ace8398dSJeff Roberson goto out; 1808ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1809ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1810ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1811ace8398dSJeff Roberson out: 1812ace8398dSJeff Roberson stp->st_flags = 0; 1813ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1814ace8398dSJeff Roberson } 1815ace8398dSJeff Roberson 1816ace8398dSJeff Roberson static int 1817cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision) 1818ace8398dSJeff Roberson { 1819ace8398dSJeff Roberson struct seltd *stp; 1820ace8398dSJeff Roberson int error; 1821ace8398dSJeff Roberson 1822ace8398dSJeff Roberson stp = td->td_sel; 1823ace8398dSJeff Roberson /* 1824ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1825ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1826ace8398dSJeff Roberson */ 1827ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1828ace8398dSJeff Roberson /* 1829ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1830ace8398dSJeff Roberson */ 1831ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1832ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1833ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1834ace8398dSJeff Roberson return (0); 1835ace8398dSJeff Roberson } 1836cf5e4fe6SDavide Italiano if (sbt == 0) 1837cf5e4fe6SDavide Italiano error = EWOULDBLOCK; 1838cf5e4fe6SDavide Italiano else if (sbt != -1) 1839cf5e4fe6SDavide Italiano error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx, 1840cf5e4fe6SDavide Italiano sbt, precision, C_ABSOLUTE); 1841ace8398dSJeff Roberson else 1842ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1843ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1844ace8398dSJeff Roberson 1845ace8398dSJeff Roberson return (error); 1846ace8398dSJeff Roberson } 1847ace8398dSJeff Roberson 1848ace8398dSJeff Roberson void 1849ace8398dSJeff Roberson seltdfini(struct thread *td) 1850ace8398dSJeff Roberson { 1851ace8398dSJeff Roberson struct seltd *stp; 1852ace8398dSJeff Roberson 1853ace8398dSJeff Roberson stp = td->td_sel; 1854ace8398dSJeff Roberson if (stp == NULL) 1855ace8398dSJeff Roberson return; 1856ace8398dSJeff Roberson if (stp->st_free1) 1857ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1858ace8398dSJeff Roberson if (stp->st_free2) 1859ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1860ace8398dSJeff Roberson td->td_sel = NULL; 1861ace8398dSJeff Roberson free(stp, M_SELECT); 1862ace8398dSJeff Roberson } 1863ace8398dSJeff Roberson 1864ace8398dSJeff Roberson /* 1865ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1866ace8398dSJeff Roberson * polling. 1867ace8398dSJeff Roberson */ 1868ace8398dSJeff Roberson static void 1869ace8398dSJeff Roberson seltdclear(struct thread *td) 1870ace8398dSJeff Roberson { 1871ace8398dSJeff Roberson struct seltd *stp; 1872ace8398dSJeff Roberson struct selfd *sfp; 1873ace8398dSJeff Roberson struct selfd *sfn; 1874ace8398dSJeff Roberson 1875ace8398dSJeff Roberson stp = td->td_sel; 1876ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1877ace8398dSJeff Roberson selfdfree(stp, sfp); 1878ace8398dSJeff Roberson stp->st_flags = 0; 1879df8bae1dSRodney W. Grimes } 1880265fc98fSSeigo Tanimura 18814d77a549SAlfred Perlstein static void selectinit(void *); 1882ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1883265fc98fSSeigo Tanimura static void 1884ace8398dSJeff Roberson selectinit(void *dummy __unused) 1885265fc98fSSeigo Tanimura { 18862141453eSJeff Roberson 1887ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1888ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 18892141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1890265fc98fSSeigo Tanimura } 1891