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 { 6493e15c66fSPoul-Henning Kamp u_long com; 6509fddcc66SRuslan Ermilov int arg, error; 6513e15c66fSPoul-Henning Kamp u_int size; 652*5cbf44bfSMateusz Guzik u_char smalldata[128]; 6539fddcc66SRuslan Ermilov caddr_t data; 654df8bae1dSRodney W. Grimes 6559fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6569fc6aa06SPoul-Henning Kamp printf( 6579fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 658431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6599fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 6609fc6aa06SPoul-Henning Kamp } 661d9f46233SJohn Baldwin com = uap->com; 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes /* 664df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 665df8bae1dSRodney W. Grimes * copied to/from the user's address space. 666df8bae1dSRodney W. Grimes */ 667df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 668ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 669ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6702de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6712de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6722de92a38SPeter Wemm #else 6732de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6742de92a38SPeter Wemm #endif 6759fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 676426da3bcSAlfred Perlstein return (ENOTTY); 677279d7226SMatthew Dillon 678ca51b19bSPoul-Henning Kamp if (size > 0) { 679715457f6SDavid E. O'Brien if (com & IOC_VOID) { 6809fddcc66SRuslan Ermilov /* Integer argument. */ 6819fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6829fddcc66SRuslan Ermilov data = (void *)&arg; 6839fddcc66SRuslan Ermilov size = 0; 684*5cbf44bfSMateusz Guzik } else { 685*5cbf44bfSMateusz Guzik if (size <= sizeof(smalldata)) 686*5cbf44bfSMateusz Guzik data = smalldata; 687*5cbf44bfSMateusz Guzik else 688715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 689*5cbf44bfSMateusz Guzik } 6909fddcc66SRuslan Ermilov } else 6919fddcc66SRuslan Ermilov data = (void *)&uap->data; 692df8bae1dSRodney W. Grimes if (com & IOC_IN) { 693df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 694*5cbf44bfSMateusz Guzik if (error != 0) 695*5cbf44bfSMateusz Guzik goto out; 696ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 697df8bae1dSRodney W. Grimes /* 698df8bae1dSRodney W. Grimes * Zero the buffer so the user always 699df8bae1dSRodney W. Grimes * gets back something deterministic. 700df8bae1dSRodney W. Grimes */ 701df8bae1dSRodney W. Grimes bzero(data, size); 702279d7226SMatthew Dillon } 703df8bae1dSRodney W. Grimes 704d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 705d9f46233SJohn Baldwin 706d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 707d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 708d9f46233SJohn Baldwin 709*5cbf44bfSMateusz Guzik out: 710*5cbf44bfSMateusz Guzik if (size > 0 && data != (caddr_t)&smalldata) 7119fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 712d9f46233SJohn Baldwin return (error); 713d9f46233SJohn Baldwin } 714d9f46233SJohn Baldwin 715d9f46233SJohn Baldwin int 716d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 717d9f46233SJohn Baldwin { 718d9f46233SJohn Baldwin struct file *fp; 719d9f46233SJohn Baldwin struct filedesc *fdp; 7207008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES 7217008be5bSPawel Jakub Dawidek cap_rights_t rights; 7227008be5bSPawel Jakub Dawidek #endif 7232609222aSPawel Jakub Dawidek int error, tmp, locked; 724d9f46233SJohn Baldwin 72564c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 72664c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7272609222aSPawel Jakub Dawidek 728d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7292609222aSPawel Jakub Dawidek 730d9f46233SJohn Baldwin switch (com) { 731d9f46233SJohn Baldwin case FIONCLEX: 732d9f46233SJohn Baldwin case FIOCLEX: 7335e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7342609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7352609222aSPawel Jakub Dawidek break; 7362609222aSPawel Jakub Dawidek default: 7372609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7382609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7392609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7402609222aSPawel Jakub Dawidek #else 7412609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7422609222aSPawel Jakub Dawidek #endif 7432609222aSPawel Jakub Dawidek break; 7442609222aSPawel Jakub Dawidek } 7452609222aSPawel Jakub Dawidek 7462609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7472609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7482609222aSPawel Jakub Dawidek error = EBADF; 7492609222aSPawel Jakub Dawidek goto out; 7502609222aSPawel Jakub Dawidek } 7512609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7522609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7532609222aSPawel Jakub Dawidek goto out; 7542609222aSPawel Jakub Dawidek } 7552609222aSPawel Jakub Dawidek fhold(fp); 7562609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 7572609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 7582609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7592609222aSPawel Jakub Dawidek } 7602609222aSPawel Jakub Dawidek #else 7617008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp); 7627008be5bSPawel Jakub Dawidek if (error != 0) { 7632609222aSPawel Jakub Dawidek fp = NULL; 7642609222aSPawel Jakub Dawidek goto out; 7652609222aSPawel Jakub Dawidek } 7662609222aSPawel Jakub Dawidek #endif 7672609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 7682609222aSPawel Jakub Dawidek error = EBADF; 7692609222aSPawel Jakub Dawidek goto out; 7702609222aSPawel Jakub Dawidek } 7712609222aSPawel Jakub Dawidek 7722609222aSPawel Jakub Dawidek switch (com) { 7732609222aSPawel Jakub Dawidek case FIONCLEX: 7742609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 7752609222aSPawel Jakub Dawidek goto out; 7762609222aSPawel Jakub Dawidek case FIOCLEX: 7772609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 778d9f46233SJohn Baldwin goto out; 779d9f46233SJohn Baldwin case FIONBIO: 780bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 781397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 782df8bae1dSRodney W. Grimes else 783397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7848ccf264fSPoul-Henning Kamp data = (void *)&tmp; 785d9f46233SJohn Baldwin break; 786d9f46233SJohn Baldwin case FIOASYNC: 787bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 788397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 789df8bae1dSRodney W. Grimes else 790397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7918ccf264fSPoul-Henning Kamp data = (void *)&tmp; 792d9f46233SJohn Baldwin break; 793df8bae1dSRodney W. Grimes } 7948ccf264fSPoul-Henning Kamp 7958ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 796d9f46233SJohn Baldwin out: 7972609222aSPawel Jakub Dawidek switch (locked) { 7982609222aSPawel Jakub Dawidek case LA_XLOCKED: 7992609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 8002609222aSPawel Jakub Dawidek break; 8012609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 8022609222aSPawel Jakub Dawidek case LA_SLOCKED: 8032609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8042609222aSPawel Jakub Dawidek break; 8052609222aSPawel Jakub Dawidek #endif 8062609222aSPawel Jakub Dawidek default: 8072609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8082609222aSPawel Jakub Dawidek break; 8092609222aSPawel Jakub Dawidek } 8102609222aSPawel Jakub Dawidek if (fp != NULL) 811b40ce416SJulian Elischer fdrop(fp, td); 812df8bae1dSRodney W. Grimes return (error); 813df8bae1dSRodney W. Grimes } 814df8bae1dSRodney W. Grimes 815125dcf8cSKonstantin Belousov int 816125dcf8cSKonstantin Belousov poll_no_poll(int events) 817125dcf8cSKonstantin Belousov { 818125dcf8cSKonstantin Belousov /* 819125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 820125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 821125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 822125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 823125dcf8cSKonstantin Belousov * of specific filesystem implementations. 824125dcf8cSKonstantin Belousov */ 825125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 826125dcf8cSKonstantin Belousov return (POLLNVAL); 827125dcf8cSKonstantin Belousov 828125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 829125dcf8cSKonstantin Belousov } 830125dcf8cSKonstantin Belousov 831066d836bSKonstantin Belousov int 8328451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 833066d836bSKonstantin Belousov { 834066d836bSKonstantin Belousov struct timespec ts; 835066d836bSKonstantin Belousov struct timeval tv, *tvp; 836066d836bSKonstantin Belousov sigset_t set, *uset; 837066d836bSKonstantin Belousov int error; 838066d836bSKonstantin Belousov 839066d836bSKonstantin Belousov if (uap->ts != NULL) { 840066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 841066d836bSKonstantin Belousov if (error != 0) 842066d836bSKonstantin Belousov return (error); 843066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 844066d836bSKonstantin Belousov tvp = &tv; 845066d836bSKonstantin Belousov } else 846066d836bSKonstantin Belousov tvp = NULL; 847066d836bSKonstantin Belousov if (uap->sm != NULL) { 848066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 849066d836bSKonstantin Belousov if (error != 0) 850066d836bSKonstantin Belousov return (error); 851066d836bSKonstantin Belousov uset = &set; 852066d836bSKonstantin Belousov } else 853066d836bSKonstantin Belousov uset = NULL; 854066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 855066d836bSKonstantin Belousov uset, NFDBITS)); 856066d836bSKonstantin Belousov } 857066d836bSKonstantin Belousov 858066d836bSKonstantin Belousov int 859066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 860066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 861066d836bSKonstantin Belousov { 862066d836bSKonstantin Belousov int error; 863066d836bSKonstantin Belousov 864066d836bSKonstantin Belousov if (uset != NULL) { 865066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 866066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 867066d836bSKonstantin Belousov if (error != 0) 868066d836bSKonstantin Belousov return (error); 869066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 870066d836bSKonstantin Belousov /* 871066d836bSKonstantin Belousov * Make sure that ast() is called on return to 872066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 873066d836bSKonstantin Belousov * sigmask. 874066d836bSKonstantin Belousov */ 875066d836bSKonstantin Belousov thread_lock(td); 876066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 877066d836bSKonstantin Belousov thread_unlock(td); 878066d836bSKonstantin Belousov } 879066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 880066d836bSKonstantin Belousov return (error); 881066d836bSKonstantin Belousov } 882066d836bSKonstantin Belousov 883d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 884df8bae1dSRodney W. Grimes struct select_args { 885b08f7993SSujal Patel int nd; 886df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 887df8bae1dSRodney W. Grimes struct timeval *tv; 888df8bae1dSRodney W. Grimes }; 889d2d3e875SBruce Evans #endif 89026f9a767SRodney W. Grimes int 8918451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 892df8bae1dSRodney W. Grimes { 8938f19eb88SIan Dowse struct timeval tv, *tvp; 8948f19eb88SIan Dowse int error; 8958f19eb88SIan Dowse 8968f19eb88SIan Dowse if (uap->tv != NULL) { 8978f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 8988f19eb88SIan Dowse if (error) 8998f19eb88SIan Dowse return (error); 9008f19eb88SIan Dowse tvp = &tv; 9018f19eb88SIan Dowse } else 9028f19eb88SIan Dowse tvp = NULL; 9038f19eb88SIan Dowse 904b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 905b55ef216SKonstantin Belousov NFDBITS)); 9068f19eb88SIan Dowse } 9078f19eb88SIan Dowse 90856be1b9aSKonstantin Belousov /* 90956be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 91056be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 91156be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 91256be1b9aSKonstantin Belousov * 91356be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 91456be1b9aSKonstantin Belousov * 91556be1b9aSKonstantin Belousov * nd is fd_lastfile + 1. 91656be1b9aSKonstantin Belousov */ 91756be1b9aSKonstantin Belousov static int 91856be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 91956be1b9aSKonstantin Belousov { 92056be1b9aSKonstantin Belousov char *addr, *oaddr; 92156be1b9aSKonstantin Belousov int b, i, res; 92256be1b9aSKonstantin Belousov uint8_t bits; 92356be1b9aSKonstantin Belousov 92456be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 92556be1b9aSKonstantin Belousov return (0); 92656be1b9aSKonstantin Belousov 92756be1b9aSKonstantin Belousov oaddr = NULL; 92856be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 92956be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 93056be1b9aSKonstantin Belousov b = i / NBBY; 93156be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 93256be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 93356be1b9aSKonstantin Belousov #else 93456be1b9aSKonstantin Belousov addr = (char *)fd_in; 93556be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 93656be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 93756be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 93856be1b9aSKonstantin Belousov } else { 93956be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 94056be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 94156be1b9aSKonstantin Belousov } 94256be1b9aSKonstantin Belousov #endif 94356be1b9aSKonstantin Belousov if (addr != oaddr) { 94456be1b9aSKonstantin Belousov res = fubyte(addr); 94556be1b9aSKonstantin Belousov if (res == -1) 94656be1b9aSKonstantin Belousov return (EFAULT); 94756be1b9aSKonstantin Belousov oaddr = addr; 94856be1b9aSKonstantin Belousov bits = res; 94956be1b9aSKonstantin Belousov } 95056be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 95156be1b9aSKonstantin Belousov return (EBADF); 95256be1b9aSKonstantin Belousov } 95356be1b9aSKonstantin Belousov return (0); 95456be1b9aSKonstantin Belousov } 95556be1b9aSKonstantin Belousov 9568f19eb88SIan Dowse int 9578f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 958b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 9598f19eb88SIan Dowse { 960426da3bcSAlfred Perlstein struct filedesc *fdp; 961d5e4d7e1SBruce Evans /* 962d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 963d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 964d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 965d5e4d7e1SBruce Evans * of 256. 966d5e4d7e1SBruce Evans */ 967d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 968eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 969cf5e4fe6SDavide Italiano struct timeval rtv; 970cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 971b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 972cf5e4fe6SDavide Italiano int error, lf, ndu; 973df8bae1dSRodney W. Grimes 9748f19eb88SIan Dowse if (nd < 0) 975acbfbfeaSSujal Patel return (EINVAL); 976426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 97756be1b9aSKonstantin Belousov ndu = nd; 97856be1b9aSKonstantin Belousov lf = fdp->fd_lastfile; 97956be1b9aSKonstantin Belousov if (nd > lf + 1) 98056be1b9aSKonstantin Belousov nd = lf + 1; 98156be1b9aSKonstantin Belousov 98256be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 98356be1b9aSKonstantin Belousov if (error != 0) 98456be1b9aSKonstantin Belousov return (error); 98556be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 98656be1b9aSKonstantin Belousov if (error != 0) 98756be1b9aSKonstantin Belousov return (error); 98856be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 98956be1b9aSKonstantin Belousov if (error != 0) 99056be1b9aSKonstantin Belousov return (error); 991b08f7993SSujal Patel 992d5e4d7e1SBruce Evans /* 993d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 994d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 995d5e4d7e1SBruce Evans */ 9968f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 997d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 998b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 999d5e4d7e1SBruce Evans nbufbytes = 0; 10008f19eb88SIan Dowse if (fd_in != NULL) 1001d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10028f19eb88SIan Dowse if (fd_ou != NULL) 1003d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10048f19eb88SIan Dowse if (fd_ex != NULL) 1005d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1006d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1007d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1008d5e4d7e1SBruce Evans else 1009a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1010b08f7993SSujal Patel 1011b08f7993SSujal Patel /* 1012d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1013d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1014d5e4d7e1SBruce Evans * together. 1015b08f7993SSujal Patel */ 1016d5e4d7e1SBruce Evans sbp = selbits; 1017df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1018d5e4d7e1SBruce Evans do { \ 1019841c0c7eSNathan Whitehorn if (name == NULL) { \ 1020d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1021841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1022841c0c7eSNathan Whitehorn } else { \ 1023d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1024d5e4d7e1SBruce Evans obits[x] = sbp; \ 1025d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1026b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1027265fc98fSSeigo Tanimura if (error != 0) \ 1028ace8398dSJeff Roberson goto done; \ 1029b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1030b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1031e04ac2feSJohn Baldwin } \ 1032d5e4d7e1SBruce Evans } while (0) 10338f19eb88SIan Dowse getbits(fd_in, 0); 10348f19eb88SIan Dowse getbits(fd_ou, 1); 10358f19eb88SIan Dowse getbits(fd_ex, 2); 1036df8bae1dSRodney W. Grimes #undef getbits 1037841c0c7eSNathan Whitehorn 1038841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1039841c0c7eSNathan Whitehorn /* 1040841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1041841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1042841c0c7eSNathan Whitehorn * generic. 1043841c0c7eSNathan Whitehorn */ 1044841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1045841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1046841c0c7eSNathan Whitehorn int i; \ 1047841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1048841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1049841c0c7eSNathan Whitehorn } 1050841c0c7eSNathan Whitehorn #else 1051841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1052841c0c7eSNathan Whitehorn #endif 1053841c0c7eSNathan Whitehorn 1054841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1055841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1056841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1057841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1058841c0c7eSNathan Whitehorn 1059d5e4d7e1SBruce Evans if (nbufbytes != 0) 1060d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1061df8bae1dSRodney W. Grimes 1062cf5e4fe6SDavide Italiano precision = 0; 10638f19eb88SIan Dowse if (tvp != NULL) { 1064cf5e4fe6SDavide Italiano rtv = *tvp; 1065cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1066cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1067df8bae1dSRodney W. Grimes error = EINVAL; 1068ace8398dSJeff Roberson goto done; 1069df8bae1dSRodney W. Grimes } 107021a37a71SAlexander Motin if (!timevalisset(&rtv)) 1071980c545dSAlexander Motin asbt = 0; 107221a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1073cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1074cf5e4fe6SDavide Italiano precision = rsbt; 1075cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1076cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1077cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 10784bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1079cf5e4fe6SDavide Italiano asbt += rsbt; 108021a37a71SAlexander Motin else 1081980c545dSAlexander Motin asbt = -1; 1082980c545dSAlexander Motin } else 1083980c545dSAlexander Motin asbt = -1; 1084cf5e4fe6SDavide Italiano } else 1085cf5e4fe6SDavide Italiano asbt = -1; 1086ace8398dSJeff Roberson seltdinit(td); 1087ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1088ace8398dSJeff Roberson for (;;) { 10898f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1090ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1091ace8398dSJeff Roberson break; 1092cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1093ace8398dSJeff Roberson if (error) 1094ace8398dSJeff Roberson break; 1095ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1096ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1097ace8398dSJeff Roberson break; 109885f190e4SAlfred Perlstein } 1099ace8398dSJeff Roberson seltdclear(td); 1100265fc98fSSeigo Tanimura 1101df8bae1dSRodney W. Grimes done: 1102df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1103df8bae1dSRodney W. Grimes if (error == ERESTART) 1104df8bae1dSRodney W. Grimes error = EINTR; 1105df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1106df8bae1dSRodney W. Grimes error = 0; 1107841c0c7eSNathan Whitehorn 1108841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1109841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1110841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1111841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1112841c0c7eSNathan Whitehorn #undef swizzle_fdset 1113841c0c7eSNathan Whitehorn 1114df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1115b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1116df8bae1dSRodney W. Grimes error = error2; 1117df8bae1dSRodney W. Grimes if (error == 0) { 1118df8bae1dSRodney W. Grimes int error2; 1119df8bae1dSRodney W. Grimes 11208f19eb88SIan Dowse putbits(fd_in, 0); 11218f19eb88SIan Dowse putbits(fd_ou, 1); 11228f19eb88SIan Dowse putbits(fd_ex, 2); 1123df8bae1dSRodney W. Grimes #undef putbits 1124df8bae1dSRodney W. Grimes } 1125d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1126d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1127ad2edad9SMatthew Dillon 1128df8bae1dSRodney W. Grimes return (error); 1129df8bae1dSRodney W. Grimes } 113011b763dfSJeff Roberson /* 113111b763dfSJeff Roberson * Convert a select bit set to poll flags. 1132748b9df6SJeff Roberson * 113311b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 113411b763dfSJeff Roberson * return this as a set bit in any set. 113511b763dfSJeff Roberson */ 113611b763dfSJeff Roberson static int select_flags[3] = { 113711b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 113811b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 113961e53a38SKonstantin Belousov POLLRDBAND | POLLERR 114011b763dfSJeff Roberson }; 114111b763dfSJeff Roberson 114211b763dfSJeff Roberson /* 114311b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 114411b763dfSJeff Roberson * bit position in the fd_mask array. 114511b763dfSJeff Roberson */ 114611b763dfSJeff Roberson static __inline int 114760b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 114811b763dfSJeff Roberson { 114911b763dfSJeff Roberson int flags; 115011b763dfSJeff Roberson int msk; 115111b763dfSJeff Roberson 115211b763dfSJeff Roberson flags = 0; 115311b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 115411b763dfSJeff Roberson if (ibits[msk] == NULL) 115511b763dfSJeff Roberson continue; 115660b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 115711b763dfSJeff Roberson continue; 115811b763dfSJeff Roberson flags |= select_flags[msk]; 115911b763dfSJeff Roberson } 116011b763dfSJeff Roberson return (flags); 116111b763dfSJeff Roberson } 116211b763dfSJeff Roberson 116311b763dfSJeff Roberson /* 116411b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 116511b763dfSJeff Roberson * input bits originally requested. 116611b763dfSJeff Roberson */ 116711b763dfSJeff Roberson static __inline int 116811b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 116911b763dfSJeff Roberson { 117011b763dfSJeff Roberson int msk; 117111b763dfSJeff Roberson int n; 117211b763dfSJeff Roberson 117311b763dfSJeff Roberson n = 0; 117411b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 117511b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 117611b763dfSJeff Roberson continue; 117711b763dfSJeff Roberson if (ibits[msk] == NULL) 117811b763dfSJeff Roberson continue; 117911b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 118011b763dfSJeff Roberson continue; 118111b763dfSJeff Roberson /* 118211b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 118311b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 118411b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 118511b763dfSJeff Roberson * call selsetbits twice as a result. 118611b763dfSJeff Roberson */ 118711b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 118811b763dfSJeff Roberson continue; 118911b763dfSJeff Roberson obits[msk][idx] |= bit; 119011b763dfSJeff Roberson n++; 119111b763dfSJeff Roberson } 119211b763dfSJeff Roberson 119311b763dfSJeff Roberson return (n); 119411b763dfSJeff Roberson } 1195df8bae1dSRodney W. Grimes 1196a9d2f8d8SRobert Watson static __inline int 1197a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1198a9d2f8d8SRobert Watson { 11997008be5bSPawel Jakub Dawidek cap_rights_t rights; 1200a9d2f8d8SRobert Watson 1201ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT); 1202ed5848c8SPawel Jakub Dawidek 1203ed5848c8SPawel Jakub Dawidek return (fget_unlocked(fdp, fd, &rights, 0, fpp, NULL)); 1204a9d2f8d8SRobert Watson } 1205a9d2f8d8SRobert Watson 1206ace8398dSJeff Roberson /* 1207ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1208ace8398dSJeff Roberson * completion. 1209ace8398dSJeff Roberson */ 1210ace8398dSJeff Roberson static int 1211ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1212ace8398dSJeff Roberson { 121311b763dfSJeff Roberson struct filedesc *fdp; 121411b763dfSJeff Roberson struct selinfo *si; 1215ace8398dSJeff Roberson struct seltd *stp; 1216ace8398dSJeff Roberson struct selfd *sfp; 1217ace8398dSJeff Roberson struct selfd *sfn; 1218ace8398dSJeff Roberson struct file *fp; 12199cdacff1SJeff Roberson fd_mask bit; 12209cdacff1SJeff Roberson int fd, ev, n, idx; 1221a9d2f8d8SRobert Watson int error; 1222ace8398dSJeff Roberson 122311b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1224ace8398dSJeff Roberson stp = td->td_sel; 122511b763dfSJeff Roberson n = 0; 1226ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1227ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1228ace8398dSJeff Roberson si = sfp->sf_si; 1229ace8398dSJeff Roberson selfdfree(stp, sfp); 1230ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1231ace8398dSJeff Roberson if (si != NULL) 1232ace8398dSJeff Roberson continue; 1233a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1234a9d2f8d8SRobert Watson if (error) 1235a9d2f8d8SRobert Watson return (error); 123611b763dfSJeff Roberson idx = fd / NFDBITS; 12379cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 123811b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1239bf422e5fSJeff Roberson fdrop(fp, td); 124011b763dfSJeff Roberson if (ev != 0) 124111b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1242ace8398dSJeff Roberson } 1243ace8398dSJeff Roberson stp->st_flags = 0; 1244ace8398dSJeff Roberson td->td_retval[0] = n; 1245ace8398dSJeff Roberson return (0); 1246ace8398dSJeff Roberson } 1247ace8398dSJeff Roberson 1248ace8398dSJeff Roberson /* 1249ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1250ace8398dSJeff Roberson * each selinfo. 1251ace8398dSJeff Roberson */ 1252265fc98fSSeigo Tanimura static int 1253b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 1254b40ce416SJulian Elischer struct thread *td; 1255b08f7993SSujal Patel fd_mask **ibits, **obits; 1256cb226aaaSPoul-Henning Kamp int nfd; 1257df8bae1dSRodney W. Grimes { 125811b763dfSJeff Roberson struct filedesc *fdp; 1259df8bae1dSRodney W. Grimes struct file *fp; 12609cdacff1SJeff Roberson fd_mask bit; 126111b763dfSJeff Roberson int ev, flags, end, fd; 12629cdacff1SJeff Roberson int n, idx; 1263a9d2f8d8SRobert Watson int error; 1264df8bae1dSRodney W. Grimes 126511b763dfSJeff Roberson fdp = td->td_proc->p_fd; 126611b763dfSJeff Roberson n = 0; 12679cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 126811b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 126911b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 127011b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 127111b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 127211b763dfSJeff Roberson if (flags == 0) 1273f082218cSPeter Wemm continue; 1274a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1275a9d2f8d8SRobert Watson if (error) 1276a9d2f8d8SRobert Watson return (error); 1277ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 127811b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1279bf422e5fSJeff Roberson fdrop(fp, td); 128011b763dfSJeff Roberson if (ev != 0) 128111b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1282df8bae1dSRodney W. Grimes } 1283df8bae1dSRodney W. Grimes } 128411b763dfSJeff Roberson 1285b40ce416SJulian Elischer td->td_retval[0] = n; 1286df8bae1dSRodney W. Grimes return (0); 1287df8bae1dSRodney W. Grimes } 1288df8bae1dSRodney W. Grimes 128942d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 129042d11757SPeter Wemm struct poll_args { 129142d11757SPeter Wemm struct pollfd *fds; 129242d11757SPeter Wemm u_int nfds; 129342d11757SPeter Wemm int timeout; 129442d11757SPeter Wemm }; 129542d11757SPeter Wemm #endif 129642d11757SPeter Wemm int 12978451d0ddSKip Macy sys_poll(td, uap) 1298b40ce416SJulian Elischer struct thread *td; 1299ea0237edSJonathan Lemon struct poll_args *uap; 130042d11757SPeter Wemm { 13012580f4e5SAndre Oppermann struct pollfd *bits; 13022580f4e5SAndre Oppermann struct pollfd smallbits[32]; 1303cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 1304ace8398dSJeff Roberson u_int nfds; 1305cf5e4fe6SDavide Italiano int error; 130642d11757SPeter Wemm size_t ni; 130742d11757SPeter Wemm 1308d1e405c5SAlfred Perlstein nfds = uap->nfds; 1309374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1310ace8398dSJeff Roberson return (EINVAL); 131189b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 131242d11757SPeter Wemm if (ni > sizeof(smallbits)) 1313a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 131442d11757SPeter Wemm else 131542d11757SPeter Wemm bits = smallbits; 1316d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 131742d11757SPeter Wemm if (error) 1318ace8398dSJeff Roberson goto done; 1319cf5e4fe6SDavide Italiano precision = 0; 1320d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 1321cf5e4fe6SDavide Italiano if (uap->timeout < 0) { 132242d11757SPeter Wemm error = EINVAL; 1323ace8398dSJeff Roberson goto done; 132442d11757SPeter Wemm } 1325980c545dSAlexander Motin if (uap->timeout == 0) 1326980c545dSAlexander Motin asbt = 0; 1327980c545dSAlexander Motin else { 1328cf5e4fe6SDavide Italiano rsbt = SBT_1MS * uap->timeout; 1329cf5e4fe6SDavide Italiano precision = rsbt; 1330cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1331cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1332cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 1333cf5e4fe6SDavide Italiano asbt += rsbt; 1334980c545dSAlexander Motin } 1335cf5e4fe6SDavide Italiano } else 1336cf5e4fe6SDavide Italiano asbt = -1; 1337ace8398dSJeff Roberson seltdinit(td); 1338ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1339ace8398dSJeff Roberson for (;;) { 13402580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 1341ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1342ace8398dSJeff Roberson break; 1343cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1344ace8398dSJeff Roberson if (error) 1345ace8398dSJeff Roberson break; 1346ace8398dSJeff Roberson error = pollrescan(td); 1347ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1348ace8398dSJeff Roberson break; 134985f190e4SAlfred Perlstein } 1350ace8398dSJeff Roberson seltdclear(td); 1351265fc98fSSeigo Tanimura 135242d11757SPeter Wemm done: 135342d11757SPeter Wemm /* poll is not restarted after signals... */ 135442d11757SPeter Wemm if (error == ERESTART) 135542d11757SPeter Wemm error = EINTR; 135642d11757SPeter Wemm if (error == EWOULDBLOCK) 135742d11757SPeter Wemm error = 0; 135842d11757SPeter Wemm if (error == 0) { 13596d8feddaSKonstantin Belousov error = pollout(td, bits, uap->fds, nfds); 136042d11757SPeter Wemm if (error) 136142d11757SPeter Wemm goto out; 136242d11757SPeter Wemm } 136342d11757SPeter Wemm out: 136442d11757SPeter Wemm if (ni > sizeof(smallbits)) 136542d11757SPeter Wemm free(bits, M_TEMP); 136642d11757SPeter Wemm return (error); 136742d11757SPeter Wemm } 136842d11757SPeter Wemm 136942d11757SPeter Wemm static int 1370ace8398dSJeff Roberson pollrescan(struct thread *td) 1371ace8398dSJeff Roberson { 1372ace8398dSJeff Roberson struct seltd *stp; 1373ace8398dSJeff Roberson struct selfd *sfp; 1374ace8398dSJeff Roberson struct selfd *sfn; 1375ace8398dSJeff Roberson struct selinfo *si; 1376ace8398dSJeff Roberson struct filedesc *fdp; 1377ace8398dSJeff Roberson struct file *fp; 1378ace8398dSJeff Roberson struct pollfd *fd; 13798e076effSSean Bruno #ifdef CAPABILITIES 13807008be5bSPawel Jakub Dawidek cap_rights_t rights; 13818e076effSSean Bruno #endif 1382ace8398dSJeff Roberson int n; 1383ace8398dSJeff Roberson 1384ace8398dSJeff Roberson n = 0; 1385ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1386ace8398dSJeff Roberson stp = td->td_sel; 1387ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1388ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1389ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1390ace8398dSJeff Roberson si = sfp->sf_si; 1391ace8398dSJeff Roberson selfdfree(stp, sfp); 1392ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1393ace8398dSJeff Roberson if (si != NULL) 1394ace8398dSJeff Roberson continue; 13952609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fd->fd].fde_file; 1396d6f72489SJonathan Anderson #ifdef CAPABILITIES 13972609222aSPawel Jakub Dawidek if (fp == NULL || 13987008be5bSPawel Jakub Dawidek cap_check(cap_rights(fdp, fd->fd), 1399ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1400d6f72489SJonathan Anderson #else 14012609222aSPawel Jakub Dawidek if (fp == NULL) 1402d6f72489SJonathan Anderson #endif 14032609222aSPawel Jakub Dawidek { 1404ace8398dSJeff Roberson fd->revents = POLLNVAL; 1405ace8398dSJeff Roberson n++; 1406ace8398dSJeff Roberson continue; 1407ace8398dSJeff Roberson } 1408d6f72489SJonathan Anderson 1409ace8398dSJeff Roberson /* 1410ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1411ace8398dSJeff Roberson * POLLERR if appropriate. 1412ace8398dSJeff Roberson */ 1413ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1414ace8398dSJeff Roberson if (fd->revents != 0) 1415ace8398dSJeff Roberson n++; 1416ace8398dSJeff Roberson } 1417ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1418ace8398dSJeff Roberson stp->st_flags = 0; 1419ace8398dSJeff Roberson td->td_retval[0] = n; 1420ace8398dSJeff Roberson return (0); 1421ace8398dSJeff Roberson } 1422ace8398dSJeff Roberson 1423ace8398dSJeff Roberson 1424ace8398dSJeff Roberson static int 14256d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd) 14266d8feddaSKonstantin Belousov struct thread *td; 1427ae81968fSRobert Watson struct pollfd *fds; 1428ae81968fSRobert Watson struct pollfd *ufds; 1429ae81968fSRobert Watson u_int nfd; 1430ae81968fSRobert Watson { 1431ae81968fSRobert Watson int error = 0; 1432ae81968fSRobert Watson u_int i = 0; 14336d8feddaSKonstantin Belousov u_int n = 0; 1434ae81968fSRobert Watson 1435ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1436ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1437ae81968fSRobert Watson sizeof(ufds->revents)); 1438ae81968fSRobert Watson if (error) 1439ae81968fSRobert Watson return (error); 14406d8feddaSKonstantin Belousov if (fds->revents != 0) 14416d8feddaSKonstantin Belousov n++; 1442ae81968fSRobert Watson fds++; 1443ae81968fSRobert Watson ufds++; 1444ae81968fSRobert Watson } 14456d8feddaSKonstantin Belousov td->td_retval[0] = n; 1446ae81968fSRobert Watson return (0); 1447ae81968fSRobert Watson } 1448ae81968fSRobert Watson 1449ae81968fSRobert Watson static int 1450b40ce416SJulian Elischer pollscan(td, fds, nfd) 1451b40ce416SJulian Elischer struct thread *td; 145242d11757SPeter Wemm struct pollfd *fds; 1453ea0237edSJonathan Lemon u_int nfd; 145442d11757SPeter Wemm { 1455ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 145642d11757SPeter Wemm struct file *fp; 14578e076effSSean Bruno #ifdef CAPABILITIES 14587008be5bSPawel Jakub Dawidek cap_rights_t rights; 14598e076effSSean Bruno #endif 14602609222aSPawel Jakub Dawidek int i, n = 0; 146142d11757SPeter Wemm 14625e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1463eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1464adf87ab0SMateusz Guzik if (fds->fd > fdp->fd_lastfile) { 146542d11757SPeter Wemm fds->revents = POLLNVAL; 146642d11757SPeter Wemm n++; 1467337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1468337c9691SJordan K. Hubbard fds->revents = 0; 146942d11757SPeter Wemm } else { 14702609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fds->fd].fde_file; 1471d6f72489SJonathan Anderson #ifdef CAPABILITIES 14722609222aSPawel Jakub Dawidek if (fp == NULL || 14732609222aSPawel Jakub Dawidek cap_check(cap_rights(fdp, fds->fd), 1474ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1475d6f72489SJonathan Anderson #else 14762609222aSPawel Jakub Dawidek if (fp == NULL) 1477d6f72489SJonathan Anderson #endif 14782609222aSPawel Jakub Dawidek { 147942d11757SPeter Wemm fds->revents = POLLNVAL; 148042d11757SPeter Wemm n++; 148142d11757SPeter Wemm } else { 14822087c896SBruce Evans /* 14832087c896SBruce Evans * Note: backend also returns POLLHUP and 14842087c896SBruce Evans * POLLERR if appropriate. 14852087c896SBruce Evans */ 1486ace8398dSJeff Roberson selfdalloc(td, fds); 148713ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1488ea6027a8SRobert Watson td->td_ucred, td); 1489f2159cc7SKonstantin Belousov /* 1490f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1491f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1492f2159cc7SKonstantin Belousov */ 1493f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1494f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1495f2159cc7SKonstantin Belousov 149642d11757SPeter Wemm if (fds->revents != 0) 149742d11757SPeter Wemm n++; 149842d11757SPeter Wemm } 149942d11757SPeter Wemm } 150042d11757SPeter Wemm } 15015e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1502b40ce416SJulian Elischer td->td_retval[0] = n; 150342d11757SPeter Wemm return (0); 150442d11757SPeter Wemm } 150542d11757SPeter Wemm 150642d11757SPeter Wemm /* 150742d11757SPeter Wemm * OpenBSD poll system call. 15080c14ff0eSRobert Watson * 150942d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 151042d11757SPeter Wemm */ 151142d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 151242d11757SPeter Wemm struct openbsd_poll_args { 151342d11757SPeter Wemm struct pollfd *fds; 151442d11757SPeter Wemm u_int nfds; 151542d11757SPeter Wemm int timeout; 151642d11757SPeter Wemm }; 151742d11757SPeter Wemm #endif 151842d11757SPeter Wemm int 15198451d0ddSKip Macy sys_openbsd_poll(td, uap) 1520b40ce416SJulian Elischer register struct thread *td; 152142d11757SPeter Wemm register struct openbsd_poll_args *uap; 152242d11757SPeter Wemm { 15238451d0ddSKip Macy return (sys_poll(td, (struct poll_args *)uap)); 152442d11757SPeter Wemm } 152542d11757SPeter Wemm 152685f190e4SAlfred Perlstein /* 1527237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1528237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1529237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1530237abf0cSDavide Italiano * determine which events fired. 1531237abf0cSDavide Italiano */ 1532237abf0cSDavide Italiano int 1533237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1534237abf0cSDavide Italiano { 1535237abf0cSDavide Italiano struct timeval rtv; 1536237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1537237abf0cSDavide Italiano int error; 1538237abf0cSDavide Italiano 153976665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1540237abf0cSDavide Italiano if (tvp != NULL) { 1541237abf0cSDavide Italiano rtv = *tvp; 1542237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1543237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1544237abf0cSDavide Italiano return (EINVAL); 1545237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1546237abf0cSDavide Italiano asbt = 0; 1547237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1548237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1549237abf0cSDavide Italiano precision = rsbt; 1550237abf0cSDavide Italiano precision >>= tc_precexp; 1551237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1552237abf0cSDavide Italiano asbt += tc_tick_sbt; 15534bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1554237abf0cSDavide Italiano asbt += rsbt; 1555237abf0cSDavide Italiano else 1556237abf0cSDavide Italiano asbt = -1; 1557237abf0cSDavide Italiano } else 1558237abf0cSDavide Italiano asbt = -1; 1559237abf0cSDavide Italiano } else 1560237abf0cSDavide Italiano asbt = -1; 1561237abf0cSDavide Italiano seltdinit(td); 1562237abf0cSDavide Italiano /* 1563237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1564237abf0cSDavide Italiano */ 1565237abf0cSDavide Italiano for (;;) { 1566237abf0cSDavide Italiano selfdalloc(td, NULL); 1567237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1568237abf0cSDavide Italiano /* error here is actually the ready events. */ 1569237abf0cSDavide Italiano if (error) 1570237abf0cSDavide Italiano return (0); 1571237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1572237abf0cSDavide Italiano if (error) 1573237abf0cSDavide Italiano break; 1574237abf0cSDavide Italiano } 1575237abf0cSDavide Italiano seltdclear(td); 1576237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1577237abf0cSDavide Italiano if (error == ERESTART) 1578237abf0cSDavide Italiano error = 0; 1579237abf0cSDavide Italiano return (error); 1580237abf0cSDavide Italiano } 1581237abf0cSDavide Italiano 1582237abf0cSDavide Italiano /* 1583ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1584ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1585ace8398dSJeff Roberson */ 1586ace8398dSJeff Roberson static void 1587ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1588ace8398dSJeff Roberson { 1589ace8398dSJeff Roberson struct seltd *stp; 1590ace8398dSJeff Roberson 1591ace8398dSJeff Roberson stp = td->td_sel; 1592ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1593ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1594ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1595ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1596ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1597ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1598ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1599ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1600ace8398dSJeff Roberson } 1601ace8398dSJeff Roberson 1602ace8398dSJeff Roberson static void 1603ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1604ace8398dSJeff Roberson { 1605ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 160673f2e5f7SMateusz Guzik if (sfp->sf_si != NULL) { 1607ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1608ffc5ce7bSMateusz Guzik if (sfp->sf_si != NULL) 1609ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1610ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 161173f2e5f7SMateusz Guzik } 1612ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 161385f190e4SAlfred Perlstein } 161485f190e4SAlfred Perlstein 16156aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ 16166aba400aSAttilio Rao void 16176aba400aSAttilio Rao seldrain(sip) 16186aba400aSAttilio Rao struct selinfo *sip; 16196aba400aSAttilio Rao { 16206aba400aSAttilio Rao 16216aba400aSAttilio Rao /* 16226aba400aSAttilio Rao * This feature is already provided by doselwakeup(), thus it is 16236aba400aSAttilio Rao * enough to go for it. 16246aba400aSAttilio Rao * Eventually, the context, should take care to avoid races 16256aba400aSAttilio Rao * between thread calling select()/poll() and file descriptor 16266aba400aSAttilio Rao * detaching, but, again, the races are just the same as 16276aba400aSAttilio Rao * selwakeup(). 16286aba400aSAttilio Rao */ 16296aba400aSAttilio Rao doselwakeup(sip, -1); 16306aba400aSAttilio Rao } 16316aba400aSAttilio Rao 1632df8bae1dSRodney W. Grimes /* 1633df8bae1dSRodney W. Grimes * Record a select request. 1634df8bae1dSRodney W. Grimes */ 1635df8bae1dSRodney W. Grimes void 1636df8bae1dSRodney W. Grimes selrecord(selector, sip) 1637b40ce416SJulian Elischer struct thread *selector; 1638df8bae1dSRodney W. Grimes struct selinfo *sip; 1639df8bae1dSRodney W. Grimes { 1640ace8398dSJeff Roberson struct selfd *sfp; 1641ace8398dSJeff Roberson struct seltd *stp; 1642ace8398dSJeff Roberson struct mtx *mtxp; 1643df8bae1dSRodney W. Grimes 1644ace8398dSJeff Roberson stp = selector->td_sel; 164585f190e4SAlfred Perlstein /* 1646ace8398dSJeff Roberson * Don't record when doing a rescan. 164785f190e4SAlfred Perlstein */ 1648ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1649ace8398dSJeff Roberson return; 1650ace8398dSJeff Roberson /* 1651ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1652ace8398dSJeff Roberson */ 1653ace8398dSJeff Roberson sfp = NULL; 1654ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1655ace8398dSJeff Roberson stp->st_free1 = NULL; 1656ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1657ace8398dSJeff Roberson stp->st_free2 = NULL; 1658ace8398dSJeff Roberson else 1659ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 16602141453eSJeff Roberson mtxp = sip->si_mtx; 16612141453eSJeff Roberson if (mtxp == NULL) 16622141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1663ace8398dSJeff Roberson /* 1664ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1665ace8398dSJeff Roberson */ 1666ace8398dSJeff Roberson sfp->sf_si = sip; 1667ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1668ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1669ace8398dSJeff Roberson /* 1670ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1671ace8398dSJeff Roberson */ 1672ace8398dSJeff Roberson mtx_lock(mtxp); 1673ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1674ace8398dSJeff Roberson sip->si_mtx = mtxp; 1675ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 167685f190e4SAlfred Perlstein } 1677ace8398dSJeff Roberson /* 1678ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1679ace8398dSJeff Roberson */ 1680ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1681ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1682df8bae1dSRodney W. Grimes } 1683df8bae1dSRodney W. Grimes 1684512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1685df8bae1dSRodney W. Grimes void 1686df8bae1dSRodney W. Grimes selwakeup(sip) 168785f190e4SAlfred Perlstein struct selinfo *sip; 1688df8bae1dSRodney W. Grimes { 1689512824f8SSeigo Tanimura doselwakeup(sip, -1); 1690512824f8SSeigo Tanimura } 1691512824f8SSeigo Tanimura 1692512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1693512824f8SSeigo Tanimura void 1694512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1695512824f8SSeigo Tanimura struct selinfo *sip; 1696512824f8SSeigo Tanimura int pri; 1697512824f8SSeigo Tanimura { 1698512824f8SSeigo Tanimura doselwakeup(sip, pri); 1699512824f8SSeigo Tanimura } 1700512824f8SSeigo Tanimura 1701512824f8SSeigo Tanimura /* 1702512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1703512824f8SSeigo Tanimura */ 1704512824f8SSeigo Tanimura static void 1705512824f8SSeigo Tanimura doselwakeup(sip, pri) 1706512824f8SSeigo Tanimura struct selinfo *sip; 1707512824f8SSeigo Tanimura int pri; 1708512824f8SSeigo Tanimura { 1709ace8398dSJeff Roberson struct selfd *sfp; 1710ace8398dSJeff Roberson struct selfd *sfn; 1711ace8398dSJeff Roberson struct seltd *stp; 1712df8bae1dSRodney W. Grimes 1713ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1714ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1715b40ce416SJulian Elischer return; 1716ace8398dSJeff Roberson /* 1717ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1718ace8398dSJeff Roberson */ 1719ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1720ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1721ace8398dSJeff Roberson /* 1722ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1723ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1724ace8398dSJeff Roberson */ 1725ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1726ace8398dSJeff Roberson sfp->sf_si = NULL; 1727ace8398dSJeff Roberson stp = sfp->sf_td; 1728ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1729ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1730ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1731ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1732b40ce416SJulian Elischer } 1733ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1734ace8398dSJeff Roberson } 1735ace8398dSJeff Roberson 1736ace8398dSJeff Roberson static void 1737ace8398dSJeff Roberson seltdinit(struct thread *td) 1738ace8398dSJeff Roberson { 1739ace8398dSJeff Roberson struct seltd *stp; 1740ace8398dSJeff Roberson 1741ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1742ace8398dSJeff Roberson goto out; 1743ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1744ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1745ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1746ace8398dSJeff Roberson out: 1747ace8398dSJeff Roberson stp->st_flags = 0; 1748ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1749ace8398dSJeff Roberson } 1750ace8398dSJeff Roberson 1751ace8398dSJeff Roberson static int 1752cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision) 1753ace8398dSJeff Roberson { 1754ace8398dSJeff Roberson struct seltd *stp; 1755ace8398dSJeff Roberson int error; 1756ace8398dSJeff Roberson 1757ace8398dSJeff Roberson stp = td->td_sel; 1758ace8398dSJeff Roberson /* 1759ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1760ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1761ace8398dSJeff Roberson */ 1762ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1763ace8398dSJeff Roberson /* 1764ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1765ace8398dSJeff Roberson */ 1766ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1767ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1768ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1769ace8398dSJeff Roberson return (0); 1770ace8398dSJeff Roberson } 1771cf5e4fe6SDavide Italiano if (sbt == 0) 1772cf5e4fe6SDavide Italiano error = EWOULDBLOCK; 1773cf5e4fe6SDavide Italiano else if (sbt != -1) 1774cf5e4fe6SDavide Italiano error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx, 1775cf5e4fe6SDavide Italiano sbt, precision, C_ABSOLUTE); 1776ace8398dSJeff Roberson else 1777ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1778ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1779ace8398dSJeff Roberson 1780ace8398dSJeff Roberson return (error); 1781ace8398dSJeff Roberson } 1782ace8398dSJeff Roberson 1783ace8398dSJeff Roberson void 1784ace8398dSJeff Roberson seltdfini(struct thread *td) 1785ace8398dSJeff Roberson { 1786ace8398dSJeff Roberson struct seltd *stp; 1787ace8398dSJeff Roberson 1788ace8398dSJeff Roberson stp = td->td_sel; 1789ace8398dSJeff Roberson if (stp == NULL) 1790ace8398dSJeff Roberson return; 1791ace8398dSJeff Roberson if (stp->st_free1) 1792ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1793ace8398dSJeff Roberson if (stp->st_free2) 1794ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1795ace8398dSJeff Roberson td->td_sel = NULL; 1796ace8398dSJeff Roberson free(stp, M_SELECT); 1797ace8398dSJeff Roberson } 1798ace8398dSJeff Roberson 1799ace8398dSJeff Roberson /* 1800ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1801ace8398dSJeff Roberson * polling. 1802ace8398dSJeff Roberson */ 1803ace8398dSJeff Roberson static void 1804ace8398dSJeff Roberson seltdclear(struct thread *td) 1805ace8398dSJeff Roberson { 1806ace8398dSJeff Roberson struct seltd *stp; 1807ace8398dSJeff Roberson struct selfd *sfp; 1808ace8398dSJeff Roberson struct selfd *sfn; 1809ace8398dSJeff Roberson 1810ace8398dSJeff Roberson stp = td->td_sel; 1811ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1812ace8398dSJeff Roberson selfdfree(stp, sfp); 1813ace8398dSJeff Roberson stp->st_flags = 0; 1814df8bae1dSRodney W. Grimes } 1815265fc98fSSeigo Tanimura 18164d77a549SAlfred Perlstein static void selectinit(void *); 1817ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1818265fc98fSSeigo Tanimura static void 1819ace8398dSJeff Roberson selectinit(void *dummy __unused) 1820265fc98fSSeigo Tanimura { 18212141453eSJeff Roberson 1822ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1823ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 18242141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1825265fc98fSSeigo Tanimura } 1826