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; 6529fddcc66SRuslan Ermilov caddr_t data; 653df8bae1dSRodney W. Grimes 6549fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6559fc6aa06SPoul-Henning Kamp printf( 6569fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 657431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6589fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 6599fc6aa06SPoul-Henning Kamp } 660d9f46233SJohn Baldwin com = uap->com; 661df8bae1dSRodney W. Grimes 662df8bae1dSRodney W. Grimes /* 663df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 664df8bae1dSRodney W. Grimes * copied to/from the user's address space. 665df8bae1dSRodney W. Grimes */ 666df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 667ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 668ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6692de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6702de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6712de92a38SPeter Wemm #else 6722de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6732de92a38SPeter Wemm #endif 6749fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 675426da3bcSAlfred Perlstein return (ENOTTY); 676279d7226SMatthew Dillon 677ca51b19bSPoul-Henning Kamp if (size > 0) { 678715457f6SDavid E. O'Brien if (com & IOC_VOID) { 6799fddcc66SRuslan Ermilov /* Integer argument. */ 6809fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6819fddcc66SRuslan Ermilov data = (void *)&arg; 6829fddcc66SRuslan Ermilov size = 0; 683715457f6SDavid E. O'Brien } else 684715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 6859fddcc66SRuslan Ermilov } else 6869fddcc66SRuslan Ermilov data = (void *)&uap->data; 687df8bae1dSRodney W. Grimes if (com & IOC_IN) { 688df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 689df8bae1dSRodney W. Grimes if (error) { 690a1b0a180SRuslan Ermilov if (size > 0) 6919fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 6923e15c66fSPoul-Henning Kamp return (error); 693df8bae1dSRodney W. Grimes } 694ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 695df8bae1dSRodney W. Grimes /* 696df8bae1dSRodney W. Grimes * Zero the buffer so the user always 697df8bae1dSRodney W. Grimes * gets back something deterministic. 698df8bae1dSRodney W. Grimes */ 699df8bae1dSRodney W. Grimes bzero(data, size); 700279d7226SMatthew Dillon } 701df8bae1dSRodney W. Grimes 702d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 703d9f46233SJohn Baldwin 704d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 705d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 706d9f46233SJohn Baldwin 7079fddcc66SRuslan Ermilov if (size > 0) 7089fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 709d9f46233SJohn Baldwin return (error); 710d9f46233SJohn Baldwin } 711d9f46233SJohn Baldwin 712d9f46233SJohn Baldwin int 713d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 714d9f46233SJohn Baldwin { 715d9f46233SJohn Baldwin struct file *fp; 716d9f46233SJohn Baldwin struct filedesc *fdp; 7177008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES 7187008be5bSPawel Jakub Dawidek cap_rights_t rights; 7197008be5bSPawel Jakub Dawidek #endif 7202609222aSPawel Jakub Dawidek int error, tmp, locked; 721d9f46233SJohn Baldwin 72264c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 72364c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7242609222aSPawel Jakub Dawidek 725d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7262609222aSPawel Jakub Dawidek 727d9f46233SJohn Baldwin switch (com) { 728d9f46233SJohn Baldwin case FIONCLEX: 729d9f46233SJohn Baldwin case FIOCLEX: 7305e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7312609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7322609222aSPawel Jakub Dawidek break; 7332609222aSPawel Jakub Dawidek default: 7342609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7352609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7362609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7372609222aSPawel Jakub Dawidek #else 7382609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7392609222aSPawel Jakub Dawidek #endif 7402609222aSPawel Jakub Dawidek break; 7412609222aSPawel Jakub Dawidek } 7422609222aSPawel Jakub Dawidek 7432609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7442609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7452609222aSPawel Jakub Dawidek error = EBADF; 7462609222aSPawel Jakub Dawidek goto out; 7472609222aSPawel Jakub Dawidek } 7482609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7492609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7502609222aSPawel Jakub Dawidek goto out; 7512609222aSPawel Jakub Dawidek } 7522609222aSPawel Jakub Dawidek fhold(fp); 7532609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 7542609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 7552609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7562609222aSPawel Jakub Dawidek } 7572609222aSPawel Jakub Dawidek #else 7587008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp); 7597008be5bSPawel Jakub Dawidek if (error != 0) { 7602609222aSPawel Jakub Dawidek fp = NULL; 7612609222aSPawel Jakub Dawidek goto out; 7622609222aSPawel Jakub Dawidek } 7632609222aSPawel Jakub Dawidek #endif 7642609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 7652609222aSPawel Jakub Dawidek error = EBADF; 7662609222aSPawel Jakub Dawidek goto out; 7672609222aSPawel Jakub Dawidek } 7682609222aSPawel Jakub Dawidek 7692609222aSPawel Jakub Dawidek switch (com) { 7702609222aSPawel Jakub Dawidek case FIONCLEX: 7712609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 7722609222aSPawel Jakub Dawidek goto out; 7732609222aSPawel Jakub Dawidek case FIOCLEX: 7742609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 775d9f46233SJohn Baldwin goto out; 776d9f46233SJohn Baldwin case FIONBIO: 777bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 778397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 779df8bae1dSRodney W. Grimes else 780397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7818ccf264fSPoul-Henning Kamp data = (void *)&tmp; 782d9f46233SJohn Baldwin break; 783d9f46233SJohn Baldwin case FIOASYNC: 784bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 785397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 786df8bae1dSRodney W. Grimes else 787397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7888ccf264fSPoul-Henning Kamp data = (void *)&tmp; 789d9f46233SJohn Baldwin break; 790df8bae1dSRodney W. Grimes } 7918ccf264fSPoul-Henning Kamp 7928ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 793d9f46233SJohn Baldwin out: 7942609222aSPawel Jakub Dawidek switch (locked) { 7952609222aSPawel Jakub Dawidek case LA_XLOCKED: 7962609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 7972609222aSPawel Jakub Dawidek break; 7982609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7992609222aSPawel Jakub Dawidek case LA_SLOCKED: 8002609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8012609222aSPawel Jakub Dawidek break; 8022609222aSPawel Jakub Dawidek #endif 8032609222aSPawel Jakub Dawidek default: 8042609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8052609222aSPawel Jakub Dawidek break; 8062609222aSPawel Jakub Dawidek } 8072609222aSPawel Jakub Dawidek if (fp != NULL) 808b40ce416SJulian Elischer fdrop(fp, td); 809df8bae1dSRodney W. Grimes return (error); 810df8bae1dSRodney W. Grimes } 811df8bae1dSRodney W. Grimes 812125dcf8cSKonstantin Belousov int 813125dcf8cSKonstantin Belousov poll_no_poll(int events) 814125dcf8cSKonstantin Belousov { 815125dcf8cSKonstantin Belousov /* 816125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 817125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 818125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 819125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 820125dcf8cSKonstantin Belousov * of specific filesystem implementations. 821125dcf8cSKonstantin Belousov */ 822125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 823125dcf8cSKonstantin Belousov return (POLLNVAL); 824125dcf8cSKonstantin Belousov 825125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 826125dcf8cSKonstantin Belousov } 827125dcf8cSKonstantin Belousov 828066d836bSKonstantin Belousov int 8298451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 830066d836bSKonstantin Belousov { 831066d836bSKonstantin Belousov struct timespec ts; 832066d836bSKonstantin Belousov struct timeval tv, *tvp; 833066d836bSKonstantin Belousov sigset_t set, *uset; 834066d836bSKonstantin Belousov int error; 835066d836bSKonstantin Belousov 836066d836bSKonstantin Belousov if (uap->ts != NULL) { 837066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 838066d836bSKonstantin Belousov if (error != 0) 839066d836bSKonstantin Belousov return (error); 840066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 841066d836bSKonstantin Belousov tvp = &tv; 842066d836bSKonstantin Belousov } else 843066d836bSKonstantin Belousov tvp = NULL; 844066d836bSKonstantin Belousov if (uap->sm != NULL) { 845066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 846066d836bSKonstantin Belousov if (error != 0) 847066d836bSKonstantin Belousov return (error); 848066d836bSKonstantin Belousov uset = &set; 849066d836bSKonstantin Belousov } else 850066d836bSKonstantin Belousov uset = NULL; 851066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 852066d836bSKonstantin Belousov uset, NFDBITS)); 853066d836bSKonstantin Belousov } 854066d836bSKonstantin Belousov 855066d836bSKonstantin Belousov int 856066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 857066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 858066d836bSKonstantin Belousov { 859066d836bSKonstantin Belousov int error; 860066d836bSKonstantin Belousov 861066d836bSKonstantin Belousov if (uset != NULL) { 862066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 863066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 864066d836bSKonstantin Belousov if (error != 0) 865066d836bSKonstantin Belousov return (error); 866066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 867066d836bSKonstantin Belousov /* 868066d836bSKonstantin Belousov * Make sure that ast() is called on return to 869066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 870066d836bSKonstantin Belousov * sigmask. 871066d836bSKonstantin Belousov */ 872066d836bSKonstantin Belousov thread_lock(td); 873066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 874066d836bSKonstantin Belousov thread_unlock(td); 875066d836bSKonstantin Belousov } 876066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 877066d836bSKonstantin Belousov return (error); 878066d836bSKonstantin Belousov } 879066d836bSKonstantin Belousov 880d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 881df8bae1dSRodney W. Grimes struct select_args { 882b08f7993SSujal Patel int nd; 883df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 884df8bae1dSRodney W. Grimes struct timeval *tv; 885df8bae1dSRodney W. Grimes }; 886d2d3e875SBruce Evans #endif 88726f9a767SRodney W. Grimes int 8888451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 889df8bae1dSRodney W. Grimes { 8908f19eb88SIan Dowse struct timeval tv, *tvp; 8918f19eb88SIan Dowse int error; 8928f19eb88SIan Dowse 8938f19eb88SIan Dowse if (uap->tv != NULL) { 8948f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 8958f19eb88SIan Dowse if (error) 8968f19eb88SIan Dowse return (error); 8978f19eb88SIan Dowse tvp = &tv; 8988f19eb88SIan Dowse } else 8998f19eb88SIan Dowse tvp = NULL; 9008f19eb88SIan Dowse 901b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 902b55ef216SKonstantin Belousov NFDBITS)); 9038f19eb88SIan Dowse } 9048f19eb88SIan Dowse 90556be1b9aSKonstantin Belousov /* 90656be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 90756be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 90856be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 90956be1b9aSKonstantin Belousov * 91056be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 91156be1b9aSKonstantin Belousov * 91256be1b9aSKonstantin Belousov * nd is fd_lastfile + 1. 91356be1b9aSKonstantin Belousov */ 91456be1b9aSKonstantin Belousov static int 91556be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 91656be1b9aSKonstantin Belousov { 91756be1b9aSKonstantin Belousov char *addr, *oaddr; 91856be1b9aSKonstantin Belousov int b, i, res; 91956be1b9aSKonstantin Belousov uint8_t bits; 92056be1b9aSKonstantin Belousov 92156be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 92256be1b9aSKonstantin Belousov return (0); 92356be1b9aSKonstantin Belousov 92456be1b9aSKonstantin Belousov oaddr = NULL; 92556be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 92656be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 92756be1b9aSKonstantin Belousov b = i / NBBY; 92856be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 92956be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 93056be1b9aSKonstantin Belousov #else 93156be1b9aSKonstantin Belousov addr = (char *)fd_in; 93256be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 93356be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 93456be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 93556be1b9aSKonstantin Belousov } else { 93656be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 93756be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 93856be1b9aSKonstantin Belousov } 93956be1b9aSKonstantin Belousov #endif 94056be1b9aSKonstantin Belousov if (addr != oaddr) { 94156be1b9aSKonstantin Belousov res = fubyte(addr); 94256be1b9aSKonstantin Belousov if (res == -1) 94356be1b9aSKonstantin Belousov return (EFAULT); 94456be1b9aSKonstantin Belousov oaddr = addr; 94556be1b9aSKonstantin Belousov bits = res; 94656be1b9aSKonstantin Belousov } 94756be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 94856be1b9aSKonstantin Belousov return (EBADF); 94956be1b9aSKonstantin Belousov } 95056be1b9aSKonstantin Belousov return (0); 95156be1b9aSKonstantin Belousov } 95256be1b9aSKonstantin Belousov 9538f19eb88SIan Dowse int 9548f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 955b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 9568f19eb88SIan Dowse { 957426da3bcSAlfred Perlstein struct filedesc *fdp; 958d5e4d7e1SBruce Evans /* 959d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 960d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 961d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 962d5e4d7e1SBruce Evans * of 256. 963d5e4d7e1SBruce Evans */ 964d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 965eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 966cf5e4fe6SDavide Italiano struct timeval rtv; 967cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 968b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 969cf5e4fe6SDavide Italiano int error, lf, ndu; 970df8bae1dSRodney W. Grimes 9718f19eb88SIan Dowse if (nd < 0) 972acbfbfeaSSujal Patel return (EINVAL); 973426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 97456be1b9aSKonstantin Belousov ndu = nd; 97556be1b9aSKonstantin Belousov lf = fdp->fd_lastfile; 97656be1b9aSKonstantin Belousov if (nd > lf + 1) 97756be1b9aSKonstantin Belousov nd = lf + 1; 97856be1b9aSKonstantin Belousov 97956be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 98056be1b9aSKonstantin Belousov if (error != 0) 98156be1b9aSKonstantin Belousov return (error); 98256be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 98356be1b9aSKonstantin Belousov if (error != 0) 98456be1b9aSKonstantin Belousov return (error); 98556be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 98656be1b9aSKonstantin Belousov if (error != 0) 98756be1b9aSKonstantin Belousov return (error); 988b08f7993SSujal Patel 989d5e4d7e1SBruce Evans /* 990d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 991d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 992d5e4d7e1SBruce Evans */ 9938f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 994d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 995b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 996d5e4d7e1SBruce Evans nbufbytes = 0; 9978f19eb88SIan Dowse if (fd_in != NULL) 998d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 9998f19eb88SIan Dowse if (fd_ou != NULL) 1000d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10018f19eb88SIan Dowse if (fd_ex != NULL) 1002d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1003d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1004d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1005d5e4d7e1SBruce Evans else 1006a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1007b08f7993SSujal Patel 1008b08f7993SSujal Patel /* 1009d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1010d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1011d5e4d7e1SBruce Evans * together. 1012b08f7993SSujal Patel */ 1013d5e4d7e1SBruce Evans sbp = selbits; 1014df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1015d5e4d7e1SBruce Evans do { \ 1016841c0c7eSNathan Whitehorn if (name == NULL) { \ 1017d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1018841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1019841c0c7eSNathan Whitehorn } else { \ 1020d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1021d5e4d7e1SBruce Evans obits[x] = sbp; \ 1022d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1023b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1024265fc98fSSeigo Tanimura if (error != 0) \ 1025ace8398dSJeff Roberson goto done; \ 1026b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1027b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1028e04ac2feSJohn Baldwin } \ 1029d5e4d7e1SBruce Evans } while (0) 10308f19eb88SIan Dowse getbits(fd_in, 0); 10318f19eb88SIan Dowse getbits(fd_ou, 1); 10328f19eb88SIan Dowse getbits(fd_ex, 2); 1033df8bae1dSRodney W. Grimes #undef getbits 1034841c0c7eSNathan Whitehorn 1035841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1036841c0c7eSNathan Whitehorn /* 1037841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1038841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1039841c0c7eSNathan Whitehorn * generic. 1040841c0c7eSNathan Whitehorn */ 1041841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1042841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1043841c0c7eSNathan Whitehorn int i; \ 1044841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1045841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1046841c0c7eSNathan Whitehorn } 1047841c0c7eSNathan Whitehorn #else 1048841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1049841c0c7eSNathan Whitehorn #endif 1050841c0c7eSNathan Whitehorn 1051841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1052841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1053841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1054841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1055841c0c7eSNathan Whitehorn 1056d5e4d7e1SBruce Evans if (nbufbytes != 0) 1057d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1058df8bae1dSRodney W. Grimes 1059cf5e4fe6SDavide Italiano precision = 0; 10608f19eb88SIan Dowse if (tvp != NULL) { 1061cf5e4fe6SDavide Italiano rtv = *tvp; 1062cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1063cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1064df8bae1dSRodney W. Grimes error = EINVAL; 1065ace8398dSJeff Roberson goto done; 1066df8bae1dSRodney W. Grimes } 106721a37a71SAlexander Motin if (!timevalisset(&rtv)) 1068980c545dSAlexander Motin asbt = 0; 106921a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1070cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1071cf5e4fe6SDavide Italiano precision = rsbt; 1072cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1073cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1074cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 10754bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1076cf5e4fe6SDavide Italiano asbt += rsbt; 107721a37a71SAlexander Motin else 1078980c545dSAlexander Motin asbt = -1; 1079980c545dSAlexander Motin } else 1080980c545dSAlexander Motin asbt = -1; 1081cf5e4fe6SDavide Italiano } else 1082cf5e4fe6SDavide Italiano asbt = -1; 1083ace8398dSJeff Roberson seltdinit(td); 1084ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1085ace8398dSJeff Roberson for (;;) { 10868f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1087ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1088ace8398dSJeff Roberson break; 1089cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1090ace8398dSJeff Roberson if (error) 1091ace8398dSJeff Roberson break; 1092ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1093ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1094ace8398dSJeff Roberson break; 109585f190e4SAlfred Perlstein } 1096ace8398dSJeff Roberson seltdclear(td); 1097265fc98fSSeigo Tanimura 1098df8bae1dSRodney W. Grimes done: 1099df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1100df8bae1dSRodney W. Grimes if (error == ERESTART) 1101df8bae1dSRodney W. Grimes error = EINTR; 1102df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1103df8bae1dSRodney W. Grimes error = 0; 1104841c0c7eSNathan Whitehorn 1105841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1106841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1107841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1108841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1109841c0c7eSNathan Whitehorn #undef swizzle_fdset 1110841c0c7eSNathan Whitehorn 1111df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1112b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1113df8bae1dSRodney W. Grimes error = error2; 1114df8bae1dSRodney W. Grimes if (error == 0) { 1115df8bae1dSRodney W. Grimes int error2; 1116df8bae1dSRodney W. Grimes 11178f19eb88SIan Dowse putbits(fd_in, 0); 11188f19eb88SIan Dowse putbits(fd_ou, 1); 11198f19eb88SIan Dowse putbits(fd_ex, 2); 1120df8bae1dSRodney W. Grimes #undef putbits 1121df8bae1dSRodney W. Grimes } 1122d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1123d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1124ad2edad9SMatthew Dillon 1125df8bae1dSRodney W. Grimes return (error); 1126df8bae1dSRodney W. Grimes } 112711b763dfSJeff Roberson /* 112811b763dfSJeff Roberson * Convert a select bit set to poll flags. 1129748b9df6SJeff Roberson * 113011b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 113111b763dfSJeff Roberson * return this as a set bit in any set. 113211b763dfSJeff Roberson */ 113311b763dfSJeff Roberson static int select_flags[3] = { 113411b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 113511b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 113661e53a38SKonstantin Belousov POLLRDBAND | POLLERR 113711b763dfSJeff Roberson }; 113811b763dfSJeff Roberson 113911b763dfSJeff Roberson /* 114011b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 114111b763dfSJeff Roberson * bit position in the fd_mask array. 114211b763dfSJeff Roberson */ 114311b763dfSJeff Roberson static __inline int 114460b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 114511b763dfSJeff Roberson { 114611b763dfSJeff Roberson int flags; 114711b763dfSJeff Roberson int msk; 114811b763dfSJeff Roberson 114911b763dfSJeff Roberson flags = 0; 115011b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 115111b763dfSJeff Roberson if (ibits[msk] == NULL) 115211b763dfSJeff Roberson continue; 115360b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 115411b763dfSJeff Roberson continue; 115511b763dfSJeff Roberson flags |= select_flags[msk]; 115611b763dfSJeff Roberson } 115711b763dfSJeff Roberson return (flags); 115811b763dfSJeff Roberson } 115911b763dfSJeff Roberson 116011b763dfSJeff Roberson /* 116111b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 116211b763dfSJeff Roberson * input bits originally requested. 116311b763dfSJeff Roberson */ 116411b763dfSJeff Roberson static __inline int 116511b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 116611b763dfSJeff Roberson { 116711b763dfSJeff Roberson int msk; 116811b763dfSJeff Roberson int n; 116911b763dfSJeff Roberson 117011b763dfSJeff Roberson n = 0; 117111b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 117211b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 117311b763dfSJeff Roberson continue; 117411b763dfSJeff Roberson if (ibits[msk] == NULL) 117511b763dfSJeff Roberson continue; 117611b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 117711b763dfSJeff Roberson continue; 117811b763dfSJeff Roberson /* 117911b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 118011b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 118111b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 118211b763dfSJeff Roberson * call selsetbits twice as a result. 118311b763dfSJeff Roberson */ 118411b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 118511b763dfSJeff Roberson continue; 118611b763dfSJeff Roberson obits[msk][idx] |= bit; 118711b763dfSJeff Roberson n++; 118811b763dfSJeff Roberson } 118911b763dfSJeff Roberson 119011b763dfSJeff Roberson return (n); 119111b763dfSJeff Roberson } 1192df8bae1dSRodney W. Grimes 1193a9d2f8d8SRobert Watson static __inline int 1194a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1195a9d2f8d8SRobert Watson { 11967008be5bSPawel Jakub Dawidek cap_rights_t rights; 1197a9d2f8d8SRobert Watson 1198ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT); 1199ed5848c8SPawel Jakub Dawidek 1200ed5848c8SPawel Jakub Dawidek return (fget_unlocked(fdp, fd, &rights, 0, fpp, NULL)); 1201a9d2f8d8SRobert Watson } 1202a9d2f8d8SRobert Watson 1203ace8398dSJeff Roberson /* 1204ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1205ace8398dSJeff Roberson * completion. 1206ace8398dSJeff Roberson */ 1207ace8398dSJeff Roberson static int 1208ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1209ace8398dSJeff Roberson { 121011b763dfSJeff Roberson struct filedesc *fdp; 121111b763dfSJeff Roberson struct selinfo *si; 1212ace8398dSJeff Roberson struct seltd *stp; 1213ace8398dSJeff Roberson struct selfd *sfp; 1214ace8398dSJeff Roberson struct selfd *sfn; 1215ace8398dSJeff Roberson struct file *fp; 12169cdacff1SJeff Roberson fd_mask bit; 12179cdacff1SJeff Roberson int fd, ev, n, idx; 1218a9d2f8d8SRobert Watson int error; 1219ace8398dSJeff Roberson 122011b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1221ace8398dSJeff Roberson stp = td->td_sel; 122211b763dfSJeff Roberson n = 0; 1223ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1224ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1225ace8398dSJeff Roberson si = sfp->sf_si; 1226ace8398dSJeff Roberson selfdfree(stp, sfp); 1227ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1228ace8398dSJeff Roberson if (si != NULL) 1229ace8398dSJeff Roberson continue; 1230a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1231a9d2f8d8SRobert Watson if (error) 1232a9d2f8d8SRobert Watson return (error); 123311b763dfSJeff Roberson idx = fd / NFDBITS; 12349cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 123511b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1236bf422e5fSJeff Roberson fdrop(fp, td); 123711b763dfSJeff Roberson if (ev != 0) 123811b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1239ace8398dSJeff Roberson } 1240ace8398dSJeff Roberson stp->st_flags = 0; 1241ace8398dSJeff Roberson td->td_retval[0] = n; 1242ace8398dSJeff Roberson return (0); 1243ace8398dSJeff Roberson } 1244ace8398dSJeff Roberson 1245ace8398dSJeff Roberson /* 1246ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1247ace8398dSJeff Roberson * each selinfo. 1248ace8398dSJeff Roberson */ 1249265fc98fSSeigo Tanimura static int 1250b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 1251b40ce416SJulian Elischer struct thread *td; 1252b08f7993SSujal Patel fd_mask **ibits, **obits; 1253cb226aaaSPoul-Henning Kamp int nfd; 1254df8bae1dSRodney W. Grimes { 125511b763dfSJeff Roberson struct filedesc *fdp; 1256df8bae1dSRodney W. Grimes struct file *fp; 12579cdacff1SJeff Roberson fd_mask bit; 125811b763dfSJeff Roberson int ev, flags, end, fd; 12599cdacff1SJeff Roberson int n, idx; 1260a9d2f8d8SRobert Watson int error; 1261df8bae1dSRodney W. Grimes 126211b763dfSJeff Roberson fdp = td->td_proc->p_fd; 126311b763dfSJeff Roberson n = 0; 12649cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 126511b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 126611b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 126711b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 126811b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 126911b763dfSJeff Roberson if (flags == 0) 1270f082218cSPeter Wemm continue; 1271a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1272a9d2f8d8SRobert Watson if (error) 1273a9d2f8d8SRobert Watson return (error); 1274ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 127511b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1276bf422e5fSJeff Roberson fdrop(fp, td); 127711b763dfSJeff Roberson if (ev != 0) 127811b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1279df8bae1dSRodney W. Grimes } 1280df8bae1dSRodney W. Grimes } 128111b763dfSJeff Roberson 1282b40ce416SJulian Elischer td->td_retval[0] = n; 1283df8bae1dSRodney W. Grimes return (0); 1284df8bae1dSRodney W. Grimes } 1285df8bae1dSRodney W. Grimes 128642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 128742d11757SPeter Wemm struct poll_args { 128842d11757SPeter Wemm struct pollfd *fds; 128942d11757SPeter Wemm u_int nfds; 129042d11757SPeter Wemm int timeout; 129142d11757SPeter Wemm }; 129242d11757SPeter Wemm #endif 129342d11757SPeter Wemm int 12948451d0ddSKip Macy sys_poll(td, uap) 1295b40ce416SJulian Elischer struct thread *td; 1296ea0237edSJonathan Lemon struct poll_args *uap; 129742d11757SPeter Wemm { 12982580f4e5SAndre Oppermann struct pollfd *bits; 12992580f4e5SAndre Oppermann struct pollfd smallbits[32]; 1300cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 1301ace8398dSJeff Roberson u_int nfds; 1302cf5e4fe6SDavide Italiano int error; 130342d11757SPeter Wemm size_t ni; 130442d11757SPeter Wemm 1305d1e405c5SAlfred Perlstein nfds = uap->nfds; 1306374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1307ace8398dSJeff Roberson return (EINVAL); 130889b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 130942d11757SPeter Wemm if (ni > sizeof(smallbits)) 1310a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 131142d11757SPeter Wemm else 131242d11757SPeter Wemm bits = smallbits; 1313d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 131442d11757SPeter Wemm if (error) 1315ace8398dSJeff Roberson goto done; 1316cf5e4fe6SDavide Italiano precision = 0; 1317d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 1318cf5e4fe6SDavide Italiano if (uap->timeout < 0) { 131942d11757SPeter Wemm error = EINVAL; 1320ace8398dSJeff Roberson goto done; 132142d11757SPeter Wemm } 1322980c545dSAlexander Motin if (uap->timeout == 0) 1323980c545dSAlexander Motin asbt = 0; 1324980c545dSAlexander Motin else { 1325cf5e4fe6SDavide Italiano rsbt = SBT_1MS * uap->timeout; 1326cf5e4fe6SDavide Italiano precision = rsbt; 1327cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1328cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1329cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 1330cf5e4fe6SDavide Italiano asbt += rsbt; 1331980c545dSAlexander Motin } 1332cf5e4fe6SDavide Italiano } else 1333cf5e4fe6SDavide Italiano asbt = -1; 1334ace8398dSJeff Roberson seltdinit(td); 1335ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1336ace8398dSJeff Roberson for (;;) { 13372580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 1338ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1339ace8398dSJeff Roberson break; 1340cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1341ace8398dSJeff Roberson if (error) 1342ace8398dSJeff Roberson break; 1343ace8398dSJeff Roberson error = pollrescan(td); 1344ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1345ace8398dSJeff Roberson break; 134685f190e4SAlfred Perlstein } 1347ace8398dSJeff Roberson seltdclear(td); 1348265fc98fSSeigo Tanimura 134942d11757SPeter Wemm done: 135042d11757SPeter Wemm /* poll is not restarted after signals... */ 135142d11757SPeter Wemm if (error == ERESTART) 135242d11757SPeter Wemm error = EINTR; 135342d11757SPeter Wemm if (error == EWOULDBLOCK) 135442d11757SPeter Wemm error = 0; 135542d11757SPeter Wemm if (error == 0) { 13566d8feddaSKonstantin Belousov error = pollout(td, bits, uap->fds, nfds); 135742d11757SPeter Wemm if (error) 135842d11757SPeter Wemm goto out; 135942d11757SPeter Wemm } 136042d11757SPeter Wemm out: 136142d11757SPeter Wemm if (ni > sizeof(smallbits)) 136242d11757SPeter Wemm free(bits, M_TEMP); 136342d11757SPeter Wemm return (error); 136442d11757SPeter Wemm } 136542d11757SPeter Wemm 136642d11757SPeter Wemm static int 1367ace8398dSJeff Roberson pollrescan(struct thread *td) 1368ace8398dSJeff Roberson { 1369ace8398dSJeff Roberson struct seltd *stp; 1370ace8398dSJeff Roberson struct selfd *sfp; 1371ace8398dSJeff Roberson struct selfd *sfn; 1372ace8398dSJeff Roberson struct selinfo *si; 1373ace8398dSJeff Roberson struct filedesc *fdp; 1374ace8398dSJeff Roberson struct file *fp; 1375ace8398dSJeff Roberson struct pollfd *fd; 13768e076effSSean Bruno #ifdef CAPABILITIES 13777008be5bSPawel Jakub Dawidek cap_rights_t rights; 13788e076effSSean Bruno #endif 1379ace8398dSJeff Roberson int n; 1380ace8398dSJeff Roberson 1381ace8398dSJeff Roberson n = 0; 1382ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1383ace8398dSJeff Roberson stp = td->td_sel; 1384ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1385ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1386ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1387ace8398dSJeff Roberson si = sfp->sf_si; 1388ace8398dSJeff Roberson selfdfree(stp, sfp); 1389ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1390ace8398dSJeff Roberson if (si != NULL) 1391ace8398dSJeff Roberson continue; 13922609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fd->fd].fde_file; 1393d6f72489SJonathan Anderson #ifdef CAPABILITIES 13942609222aSPawel Jakub Dawidek if (fp == NULL || 13957008be5bSPawel Jakub Dawidek cap_check(cap_rights(fdp, fd->fd), 1396ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1397d6f72489SJonathan Anderson #else 13982609222aSPawel Jakub Dawidek if (fp == NULL) 1399d6f72489SJonathan Anderson #endif 14002609222aSPawel Jakub Dawidek { 1401ace8398dSJeff Roberson fd->revents = POLLNVAL; 1402ace8398dSJeff Roberson n++; 1403ace8398dSJeff Roberson continue; 1404ace8398dSJeff Roberson } 1405d6f72489SJonathan Anderson 1406ace8398dSJeff Roberson /* 1407ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1408ace8398dSJeff Roberson * POLLERR if appropriate. 1409ace8398dSJeff Roberson */ 1410ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1411ace8398dSJeff Roberson if (fd->revents != 0) 1412ace8398dSJeff Roberson n++; 1413ace8398dSJeff Roberson } 1414ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1415ace8398dSJeff Roberson stp->st_flags = 0; 1416ace8398dSJeff Roberson td->td_retval[0] = n; 1417ace8398dSJeff Roberson return (0); 1418ace8398dSJeff Roberson } 1419ace8398dSJeff Roberson 1420ace8398dSJeff Roberson 1421ace8398dSJeff Roberson static int 14226d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd) 14236d8feddaSKonstantin Belousov struct thread *td; 1424ae81968fSRobert Watson struct pollfd *fds; 1425ae81968fSRobert Watson struct pollfd *ufds; 1426ae81968fSRobert Watson u_int nfd; 1427ae81968fSRobert Watson { 1428ae81968fSRobert Watson int error = 0; 1429ae81968fSRobert Watson u_int i = 0; 14306d8feddaSKonstantin Belousov u_int n = 0; 1431ae81968fSRobert Watson 1432ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1433ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1434ae81968fSRobert Watson sizeof(ufds->revents)); 1435ae81968fSRobert Watson if (error) 1436ae81968fSRobert Watson return (error); 14376d8feddaSKonstantin Belousov if (fds->revents != 0) 14386d8feddaSKonstantin Belousov n++; 1439ae81968fSRobert Watson fds++; 1440ae81968fSRobert Watson ufds++; 1441ae81968fSRobert Watson } 14426d8feddaSKonstantin Belousov td->td_retval[0] = n; 1443ae81968fSRobert Watson return (0); 1444ae81968fSRobert Watson } 1445ae81968fSRobert Watson 1446ae81968fSRobert Watson static int 1447b40ce416SJulian Elischer pollscan(td, fds, nfd) 1448b40ce416SJulian Elischer struct thread *td; 144942d11757SPeter Wemm struct pollfd *fds; 1450ea0237edSJonathan Lemon u_int nfd; 145142d11757SPeter Wemm { 1452ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 145342d11757SPeter Wemm struct file *fp; 14548e076effSSean Bruno #ifdef CAPABILITIES 14557008be5bSPawel Jakub Dawidek cap_rights_t rights; 14568e076effSSean Bruno #endif 14572609222aSPawel Jakub Dawidek int i, n = 0; 145842d11757SPeter Wemm 14595e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1460eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1461adf87ab0SMateusz Guzik if (fds->fd > fdp->fd_lastfile) { 146242d11757SPeter Wemm fds->revents = POLLNVAL; 146342d11757SPeter Wemm n++; 1464337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1465337c9691SJordan K. Hubbard fds->revents = 0; 146642d11757SPeter Wemm } else { 14672609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fds->fd].fde_file; 1468d6f72489SJonathan Anderson #ifdef CAPABILITIES 14692609222aSPawel Jakub Dawidek if (fp == NULL || 14702609222aSPawel Jakub Dawidek cap_check(cap_rights(fdp, fds->fd), 1471ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1472d6f72489SJonathan Anderson #else 14732609222aSPawel Jakub Dawidek if (fp == NULL) 1474d6f72489SJonathan Anderson #endif 14752609222aSPawel Jakub Dawidek { 147642d11757SPeter Wemm fds->revents = POLLNVAL; 147742d11757SPeter Wemm n++; 147842d11757SPeter Wemm } else { 14792087c896SBruce Evans /* 14802087c896SBruce Evans * Note: backend also returns POLLHUP and 14812087c896SBruce Evans * POLLERR if appropriate. 14822087c896SBruce Evans */ 1483ace8398dSJeff Roberson selfdalloc(td, fds); 148413ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1485ea6027a8SRobert Watson td->td_ucred, td); 1486f2159cc7SKonstantin Belousov /* 1487f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1488f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1489f2159cc7SKonstantin Belousov */ 1490f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1491f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1492f2159cc7SKonstantin Belousov 149342d11757SPeter Wemm if (fds->revents != 0) 149442d11757SPeter Wemm n++; 149542d11757SPeter Wemm } 149642d11757SPeter Wemm } 149742d11757SPeter Wemm } 14985e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1499b40ce416SJulian Elischer td->td_retval[0] = n; 150042d11757SPeter Wemm return (0); 150142d11757SPeter Wemm } 150242d11757SPeter Wemm 150342d11757SPeter Wemm /* 150442d11757SPeter Wemm * OpenBSD poll system call. 15050c14ff0eSRobert Watson * 150642d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 150742d11757SPeter Wemm */ 150842d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 150942d11757SPeter Wemm struct openbsd_poll_args { 151042d11757SPeter Wemm struct pollfd *fds; 151142d11757SPeter Wemm u_int nfds; 151242d11757SPeter Wemm int timeout; 151342d11757SPeter Wemm }; 151442d11757SPeter Wemm #endif 151542d11757SPeter Wemm int 15168451d0ddSKip Macy sys_openbsd_poll(td, uap) 1517b40ce416SJulian Elischer register struct thread *td; 151842d11757SPeter Wemm register struct openbsd_poll_args *uap; 151942d11757SPeter Wemm { 15208451d0ddSKip Macy return (sys_poll(td, (struct poll_args *)uap)); 152142d11757SPeter Wemm } 152242d11757SPeter Wemm 152385f190e4SAlfred Perlstein /* 1524237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1525237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1526237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1527237abf0cSDavide Italiano * determine which events fired. 1528237abf0cSDavide Italiano */ 1529237abf0cSDavide Italiano int 1530237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1531237abf0cSDavide Italiano { 1532237abf0cSDavide Italiano struct timeval rtv; 1533237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1534237abf0cSDavide Italiano int error; 1535237abf0cSDavide Italiano 153676665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1537237abf0cSDavide Italiano if (tvp != NULL) { 1538237abf0cSDavide Italiano rtv = *tvp; 1539237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1540237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1541237abf0cSDavide Italiano return (EINVAL); 1542237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1543237abf0cSDavide Italiano asbt = 0; 1544237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1545237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1546237abf0cSDavide Italiano precision = rsbt; 1547237abf0cSDavide Italiano precision >>= tc_precexp; 1548237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1549237abf0cSDavide Italiano asbt += tc_tick_sbt; 15504bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1551237abf0cSDavide Italiano asbt += rsbt; 1552237abf0cSDavide Italiano else 1553237abf0cSDavide Italiano asbt = -1; 1554237abf0cSDavide Italiano } else 1555237abf0cSDavide Italiano asbt = -1; 1556237abf0cSDavide Italiano } else 1557237abf0cSDavide Italiano asbt = -1; 1558237abf0cSDavide Italiano seltdinit(td); 1559237abf0cSDavide Italiano /* 1560237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1561237abf0cSDavide Italiano */ 1562237abf0cSDavide Italiano for (;;) { 1563237abf0cSDavide Italiano selfdalloc(td, NULL); 1564237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1565237abf0cSDavide Italiano /* error here is actually the ready events. */ 1566237abf0cSDavide Italiano if (error) 1567237abf0cSDavide Italiano return (0); 1568237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1569237abf0cSDavide Italiano if (error) 1570237abf0cSDavide Italiano break; 1571237abf0cSDavide Italiano } 1572237abf0cSDavide Italiano seltdclear(td); 1573237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1574237abf0cSDavide Italiano if (error == ERESTART) 1575237abf0cSDavide Italiano error = 0; 1576237abf0cSDavide Italiano return (error); 1577237abf0cSDavide Italiano } 1578237abf0cSDavide Italiano 1579237abf0cSDavide Italiano /* 1580ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1581ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1582ace8398dSJeff Roberson */ 1583ace8398dSJeff Roberson static void 1584ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1585ace8398dSJeff Roberson { 1586ace8398dSJeff Roberson struct seltd *stp; 1587ace8398dSJeff Roberson 1588ace8398dSJeff Roberson stp = td->td_sel; 1589ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1590ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1591ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1592ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1593ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1594ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1595ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1596ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1597ace8398dSJeff Roberson } 1598ace8398dSJeff Roberson 1599ace8398dSJeff Roberson static void 1600ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1601ace8398dSJeff Roberson { 1602ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 1603*73f2e5f7SMateusz Guzik if (sfp->sf_si != NULL) { 1604ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1605ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1606ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 1607*73f2e5f7SMateusz Guzik } 1608ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 160985f190e4SAlfred Perlstein } 161085f190e4SAlfred Perlstein 16116aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ 16126aba400aSAttilio Rao void 16136aba400aSAttilio Rao seldrain(sip) 16146aba400aSAttilio Rao struct selinfo *sip; 16156aba400aSAttilio Rao { 16166aba400aSAttilio Rao 16176aba400aSAttilio Rao /* 16186aba400aSAttilio Rao * This feature is already provided by doselwakeup(), thus it is 16196aba400aSAttilio Rao * enough to go for it. 16206aba400aSAttilio Rao * Eventually, the context, should take care to avoid races 16216aba400aSAttilio Rao * between thread calling select()/poll() and file descriptor 16226aba400aSAttilio Rao * detaching, but, again, the races are just the same as 16236aba400aSAttilio Rao * selwakeup(). 16246aba400aSAttilio Rao */ 16256aba400aSAttilio Rao doselwakeup(sip, -1); 16266aba400aSAttilio Rao } 16276aba400aSAttilio Rao 1628df8bae1dSRodney W. Grimes /* 1629df8bae1dSRodney W. Grimes * Record a select request. 1630df8bae1dSRodney W. Grimes */ 1631df8bae1dSRodney W. Grimes void 1632df8bae1dSRodney W. Grimes selrecord(selector, sip) 1633b40ce416SJulian Elischer struct thread *selector; 1634df8bae1dSRodney W. Grimes struct selinfo *sip; 1635df8bae1dSRodney W. Grimes { 1636ace8398dSJeff Roberson struct selfd *sfp; 1637ace8398dSJeff Roberson struct seltd *stp; 1638ace8398dSJeff Roberson struct mtx *mtxp; 1639df8bae1dSRodney W. Grimes 1640ace8398dSJeff Roberson stp = selector->td_sel; 164185f190e4SAlfred Perlstein /* 1642ace8398dSJeff Roberson * Don't record when doing a rescan. 164385f190e4SAlfred Perlstein */ 1644ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1645ace8398dSJeff Roberson return; 1646ace8398dSJeff Roberson /* 1647ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1648ace8398dSJeff Roberson */ 1649ace8398dSJeff Roberson sfp = NULL; 1650ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1651ace8398dSJeff Roberson stp->st_free1 = NULL; 1652ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1653ace8398dSJeff Roberson stp->st_free2 = NULL; 1654ace8398dSJeff Roberson else 1655ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 16562141453eSJeff Roberson mtxp = sip->si_mtx; 16572141453eSJeff Roberson if (mtxp == NULL) 16582141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1659ace8398dSJeff Roberson /* 1660ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1661ace8398dSJeff Roberson */ 1662ace8398dSJeff Roberson sfp->sf_si = sip; 1663ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1664ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1665ace8398dSJeff Roberson /* 1666ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1667ace8398dSJeff Roberson */ 1668ace8398dSJeff Roberson mtx_lock(mtxp); 1669ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1670ace8398dSJeff Roberson sip->si_mtx = mtxp; 1671ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 167285f190e4SAlfred Perlstein } 1673ace8398dSJeff Roberson /* 1674ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1675ace8398dSJeff Roberson */ 1676ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1677ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1678df8bae1dSRodney W. Grimes } 1679df8bae1dSRodney W. Grimes 1680512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1681df8bae1dSRodney W. Grimes void 1682df8bae1dSRodney W. Grimes selwakeup(sip) 168385f190e4SAlfred Perlstein struct selinfo *sip; 1684df8bae1dSRodney W. Grimes { 1685512824f8SSeigo Tanimura doselwakeup(sip, -1); 1686512824f8SSeigo Tanimura } 1687512824f8SSeigo Tanimura 1688512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1689512824f8SSeigo Tanimura void 1690512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1691512824f8SSeigo Tanimura struct selinfo *sip; 1692512824f8SSeigo Tanimura int pri; 1693512824f8SSeigo Tanimura { 1694512824f8SSeigo Tanimura doselwakeup(sip, pri); 1695512824f8SSeigo Tanimura } 1696512824f8SSeigo Tanimura 1697512824f8SSeigo Tanimura /* 1698512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1699512824f8SSeigo Tanimura */ 1700512824f8SSeigo Tanimura static void 1701512824f8SSeigo Tanimura doselwakeup(sip, pri) 1702512824f8SSeigo Tanimura struct selinfo *sip; 1703512824f8SSeigo Tanimura int pri; 1704512824f8SSeigo Tanimura { 1705ace8398dSJeff Roberson struct selfd *sfp; 1706ace8398dSJeff Roberson struct selfd *sfn; 1707ace8398dSJeff Roberson struct seltd *stp; 1708df8bae1dSRodney W. Grimes 1709ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1710ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1711b40ce416SJulian Elischer return; 1712ace8398dSJeff Roberson /* 1713ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1714ace8398dSJeff Roberson */ 1715ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1716ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1717ace8398dSJeff Roberson /* 1718ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1719ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1720ace8398dSJeff Roberson */ 1721ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1722ace8398dSJeff Roberson sfp->sf_si = NULL; 1723ace8398dSJeff Roberson stp = sfp->sf_td; 1724ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1725ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1726ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1727ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1728b40ce416SJulian Elischer } 1729ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1730ace8398dSJeff Roberson } 1731ace8398dSJeff Roberson 1732ace8398dSJeff Roberson static void 1733ace8398dSJeff Roberson seltdinit(struct thread *td) 1734ace8398dSJeff Roberson { 1735ace8398dSJeff Roberson struct seltd *stp; 1736ace8398dSJeff Roberson 1737ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1738ace8398dSJeff Roberson goto out; 1739ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1740ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1741ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1742ace8398dSJeff Roberson out: 1743ace8398dSJeff Roberson stp->st_flags = 0; 1744ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1745ace8398dSJeff Roberson } 1746ace8398dSJeff Roberson 1747ace8398dSJeff Roberson static int 1748cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision) 1749ace8398dSJeff Roberson { 1750ace8398dSJeff Roberson struct seltd *stp; 1751ace8398dSJeff Roberson int error; 1752ace8398dSJeff Roberson 1753ace8398dSJeff Roberson stp = td->td_sel; 1754ace8398dSJeff Roberson /* 1755ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1756ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1757ace8398dSJeff Roberson */ 1758ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1759ace8398dSJeff Roberson /* 1760ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1761ace8398dSJeff Roberson */ 1762ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1763ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1764ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1765ace8398dSJeff Roberson return (0); 1766ace8398dSJeff Roberson } 1767cf5e4fe6SDavide Italiano if (sbt == 0) 1768cf5e4fe6SDavide Italiano error = EWOULDBLOCK; 1769cf5e4fe6SDavide Italiano else if (sbt != -1) 1770cf5e4fe6SDavide Italiano error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx, 1771cf5e4fe6SDavide Italiano sbt, precision, C_ABSOLUTE); 1772ace8398dSJeff Roberson else 1773ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1774ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1775ace8398dSJeff Roberson 1776ace8398dSJeff Roberson return (error); 1777ace8398dSJeff Roberson } 1778ace8398dSJeff Roberson 1779ace8398dSJeff Roberson void 1780ace8398dSJeff Roberson seltdfini(struct thread *td) 1781ace8398dSJeff Roberson { 1782ace8398dSJeff Roberson struct seltd *stp; 1783ace8398dSJeff Roberson 1784ace8398dSJeff Roberson stp = td->td_sel; 1785ace8398dSJeff Roberson if (stp == NULL) 1786ace8398dSJeff Roberson return; 1787ace8398dSJeff Roberson if (stp->st_free1) 1788ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1789ace8398dSJeff Roberson if (stp->st_free2) 1790ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1791ace8398dSJeff Roberson td->td_sel = NULL; 1792ace8398dSJeff Roberson free(stp, M_SELECT); 1793ace8398dSJeff Roberson } 1794ace8398dSJeff Roberson 1795ace8398dSJeff Roberson /* 1796ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1797ace8398dSJeff Roberson * polling. 1798ace8398dSJeff Roberson */ 1799ace8398dSJeff Roberson static void 1800ace8398dSJeff Roberson seltdclear(struct thread *td) 1801ace8398dSJeff Roberson { 1802ace8398dSJeff Roberson struct seltd *stp; 1803ace8398dSJeff Roberson struct selfd *sfp; 1804ace8398dSJeff Roberson struct selfd *sfn; 1805ace8398dSJeff Roberson 1806ace8398dSJeff Roberson stp = td->td_sel; 1807ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1808ace8398dSJeff Roberson selfdfree(stp, sfp); 1809ace8398dSJeff Roberson stp->st_flags = 0; 1810df8bae1dSRodney W. Grimes } 1811265fc98fSSeigo Tanimura 18124d77a549SAlfred Perlstein static void selectinit(void *); 1813ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1814265fc98fSSeigo Tanimura static void 1815ace8398dSJeff Roberson selectinit(void *dummy __unused) 1816265fc98fSSeigo Tanimura { 18172141453eSJeff Roberson 1818ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1819ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 18202141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1821265fc98fSSeigo Tanimura } 1822