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. 18*69a28758SEd Maste * 3. 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 7850ae6690SHans Petter Selasky /* 7950ae6690SHans Petter Selasky * The following macro defines how many bytes will be allocated from 8050ae6690SHans Petter Selasky * the stack instead of memory allocated when passing the IOCTL data 8150ae6690SHans Petter Selasky * structures from userspace and to the kernel. Some IOCTLs having 8250ae6690SHans Petter Selasky * small data structures are used very frequently and this small 8350ae6690SHans Petter Selasky * buffer on the stack gives a significant speedup improvement for 8450ae6690SHans Petter Selasky * those requests. The value of this define should be greater or equal 8550ae6690SHans Petter Selasky * to 64 bytes and should also be power of two. The data structure is 8650ae6690SHans Petter Selasky * currently hard-aligned to a 8-byte boundary on the stack. This 8750ae6690SHans Petter Selasky * should currently be sufficient for all supported platforms. 8850ae6690SHans Petter Selasky */ 8950ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_SIZE 128 /* bytes */ 9050ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_ALIGN 8 /* bytes */ 9150ae6690SHans Petter Selasky 92611fcff9SJohn Baldwin #ifdef __LP64__ 93611fcff9SJohn Baldwin static int iosize_max_clamp = 0; 948bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW, 958bb9a904SKonstantin Belousov &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX"); 96611fcff9SJohn Baldwin static int devfs_iosize_max_clamp = 1; 97bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW, 98bf3e483bSKonstantin Belousov &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices"); 99611fcff9SJohn Baldwin #endif 100bf3e483bSKonstantin Belousov 1018bb9a904SKonstantin Belousov /* 1028bb9a904SKonstantin Belousov * Assert that the return value of read(2) and write(2) syscalls fits 1038bb9a904SKonstantin Belousov * into a register. If not, an architecture will need to provide the 1048bb9a904SKonstantin Belousov * usermode wrappers to reconstruct the result. 1058bb9a904SKonstantin Belousov */ 1068bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t)); 107526d0bd5SKonstantin Belousov 108a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 109a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 110a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 11155166637SPoul-Henning Kamp 1126d8feddaSKonstantin Belousov static int pollout(struct thread *, struct pollfd *, struct pollfd *, 1136d8feddaSKonstantin Belousov u_int); 114bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 115ace8398dSJeff Roberson static int pollrescan(struct thread *); 116bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 117ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 118ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 119ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 120bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 121bcd9e0ddSJohn Baldwin off_t, int); 122bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 123bcd9e0ddSJohn Baldwin off_t, int); 124512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 125ace8398dSJeff Roberson static void seltdinit(struct thread *); 126cf5e4fe6SDavide Italiano static int seltdwait(struct thread *, sbintime_t, sbintime_t); 127ace8398dSJeff Roberson static void seltdclear(struct thread *); 128ace8398dSJeff Roberson 129ace8398dSJeff Roberson /* 130ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 131ace8398dSJeff Roberson * 132ace8398dSJeff Roberson * t - protected by st_mtx 133ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 134ace8398dSJeff Roberson */ 135ace8398dSJeff Roberson struct seltd { 136ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 137ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 138ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 139ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 140ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 141ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 142ace8398dSJeff Roberson }; 143ace8398dSJeff Roberson 144ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 145ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 146ace8398dSJeff Roberson 147ace8398dSJeff Roberson /* 148ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 149ace8398dSJeff Roberson * f - protected by sf_mtx 150ace8398dSJeff Roberson */ 151ace8398dSJeff Roberson struct selfd { 152ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 153ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 154ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 155ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 156ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 157ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 158fcb5b3a4SKonstantin Belousov u_int sf_refs; 159ace8398dSJeff Roberson }; 160ace8398dSJeff Roberson 161ace8398dSJeff Roberson static uma_zone_t selfd_zone; 1622141453eSJeff Roberson static struct mtx_pool *mtxpool_select; 1638fe387abSDmitrij Tejblum 164611fcff9SJohn Baldwin #ifdef __LP64__ 165611fcff9SJohn Baldwin size_t 166611fcff9SJohn Baldwin devfs_iosize_max(void) 167611fcff9SJohn Baldwin { 168611fcff9SJohn Baldwin 169611fcff9SJohn Baldwin return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 170611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 171611fcff9SJohn Baldwin } 172611fcff9SJohn Baldwin 173611fcff9SJohn Baldwin size_t 174611fcff9SJohn Baldwin iosize_max(void) 175611fcff9SJohn Baldwin { 176611fcff9SJohn Baldwin 177611fcff9SJohn Baldwin return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 178611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 179611fcff9SJohn Baldwin } 180611fcff9SJohn Baldwin #endif 181611fcff9SJohn Baldwin 182d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 183df8bae1dSRodney W. Grimes struct read_args { 184df8bae1dSRodney W. Grimes int fd; 185134e06feSBruce Evans void *buf; 186134e06feSBruce Evans size_t nbyte; 187df8bae1dSRodney W. Grimes }; 188d2d3e875SBruce Evans #endif 18926f9a767SRodney W. Grimes int 1908451d0ddSKip Macy sys_read(td, uap) 191b40ce416SJulian Elischer struct thread *td; 192b064d43dSMatthew Dillon struct read_args *uap; 193df8bae1dSRodney W. Grimes { 194bcd9e0ddSJohn Baldwin struct uio auio; 195bcd9e0ddSJohn Baldwin struct iovec aiov; 196279d7226SMatthew Dillon int error; 197df8bae1dSRodney W. Grimes 198526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 199bcd9e0ddSJohn Baldwin return (EINVAL); 200bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 201bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 202bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 203bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 204bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 205bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 206bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 207279d7226SMatthew Dillon return(error); 208df8bae1dSRodney W. Grimes } 209df8bae1dSRodney W. Grimes 210df8bae1dSRodney W. Grimes /* 211bcd9e0ddSJohn Baldwin * Positioned read system call 2124160ccd9SAlan Cox */ 2134160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 2144160ccd9SAlan Cox struct pread_args { 2154160ccd9SAlan Cox int fd; 2164160ccd9SAlan Cox void *buf; 2174160ccd9SAlan Cox size_t nbyte; 2188fe387abSDmitrij Tejblum int pad; 2194160ccd9SAlan Cox off_t offset; 2204160ccd9SAlan Cox }; 2214160ccd9SAlan Cox #endif 2224160ccd9SAlan Cox int 2238451d0ddSKip Macy sys_pread(td, uap) 224b40ce416SJulian Elischer struct thread *td; 225b064d43dSMatthew Dillon struct pread_args *uap; 2264160ccd9SAlan Cox { 2274160ccd9SAlan Cox struct uio auio; 2284160ccd9SAlan Cox struct iovec aiov; 229bcd9e0ddSJohn Baldwin int error; 2304160ccd9SAlan Cox 231526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 232bcd9e0ddSJohn Baldwin return (EINVAL); 233bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 234bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 2354160ccd9SAlan Cox auio.uio_iov = &aiov; 2364160ccd9SAlan Cox auio.uio_iovcnt = 1; 237bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 2384160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 239bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, &auio, uap->offset); 2404160ccd9SAlan Cox return(error); 2414160ccd9SAlan Cox } 2424160ccd9SAlan Cox 2430538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 244c2815ad5SPeter Wemm int 245c2815ad5SPeter Wemm freebsd6_pread(td, uap) 246c2815ad5SPeter Wemm struct thread *td; 247c2815ad5SPeter Wemm struct freebsd6_pread_args *uap; 248c2815ad5SPeter Wemm { 249c2815ad5SPeter Wemm struct pread_args oargs; 250c2815ad5SPeter Wemm 251c2815ad5SPeter Wemm oargs.fd = uap->fd; 252c2815ad5SPeter Wemm oargs.buf = uap->buf; 253c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 254c2815ad5SPeter Wemm oargs.offset = uap->offset; 2558451d0ddSKip Macy return (sys_pread(td, &oargs)); 256c2815ad5SPeter Wemm } 2570538aafcSKonstantin Belousov #endif 258c2815ad5SPeter Wemm 2594160ccd9SAlan Cox /* 260df8bae1dSRodney W. Grimes * Scatter read system call. 261df8bae1dSRodney W. Grimes */ 262d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 263df8bae1dSRodney W. Grimes struct readv_args { 2647147b19dSBruce Evans int fd; 265df8bae1dSRodney W. Grimes struct iovec *iovp; 266df8bae1dSRodney W. Grimes u_int iovcnt; 267df8bae1dSRodney W. Grimes }; 268d2d3e875SBruce Evans #endif 26926f9a767SRodney W. Grimes int 2708451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap) 271df8bae1dSRodney W. Grimes { 272b88ec951SJohn Baldwin struct uio *auio; 273b88ec951SJohn Baldwin int error; 274b88ec951SJohn Baldwin 275b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 276b88ec951SJohn Baldwin if (error) 277b88ec951SJohn Baldwin return (error); 278b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 279b88ec951SJohn Baldwin free(auio, M_IOV); 280b88ec951SJohn Baldwin return (error); 281b88ec951SJohn Baldwin } 282b88ec951SJohn Baldwin 283b88ec951SJohn Baldwin int 284b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 285b88ec951SJohn Baldwin { 286b064d43dSMatthew Dillon struct file *fp; 2877008be5bSPawel Jakub Dawidek cap_rights_t rights; 288bcd9e0ddSJohn Baldwin int error; 289bcd9e0ddSJohn Baldwin 2907008be5bSPawel Jakub Dawidek error = fget_read(td, fd, cap_rights_init(&rights, CAP_READ), &fp); 291bcd9e0ddSJohn Baldwin if (error) 292bcd9e0ddSJohn Baldwin return (error); 293bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 294bcd9e0ddSJohn Baldwin fdrop(fp, td); 295bcd9e0ddSJohn Baldwin return (error); 296bcd9e0ddSJohn Baldwin } 297bcd9e0ddSJohn Baldwin 298bcd9e0ddSJohn Baldwin /* 299bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 300bcd9e0ddSJohn Baldwin */ 301bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 302bcd9e0ddSJohn Baldwin struct preadv_args { 303bcd9e0ddSJohn Baldwin int fd; 304bcd9e0ddSJohn Baldwin struct iovec *iovp; 305bcd9e0ddSJohn Baldwin u_int iovcnt; 306bcd9e0ddSJohn Baldwin off_t offset; 307bcd9e0ddSJohn Baldwin }; 308bcd9e0ddSJohn Baldwin #endif 309bcd9e0ddSJohn Baldwin int 3108451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap) 311bcd9e0ddSJohn Baldwin { 312bcd9e0ddSJohn Baldwin struct uio *auio; 313bcd9e0ddSJohn Baldwin int error; 314bcd9e0ddSJohn Baldwin 315bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 316bcd9e0ddSJohn Baldwin if (error) 317bcd9e0ddSJohn Baldwin return (error); 318bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 319bcd9e0ddSJohn Baldwin free(auio, M_IOV); 320bcd9e0ddSJohn Baldwin return (error); 321bcd9e0ddSJohn Baldwin } 322bcd9e0ddSJohn Baldwin 323bcd9e0ddSJohn Baldwin int 324bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset) 325bcd9e0ddSJohn Baldwin struct thread *td; 326bcd9e0ddSJohn Baldwin int fd; 327bcd9e0ddSJohn Baldwin struct uio *auio; 328bcd9e0ddSJohn Baldwin off_t offset; 329bcd9e0ddSJohn Baldwin { 330bcd9e0ddSJohn Baldwin struct file *fp; 3317008be5bSPawel Jakub Dawidek cap_rights_t rights; 332bcd9e0ddSJohn Baldwin int error; 333bcd9e0ddSJohn Baldwin 3347008be5bSPawel Jakub Dawidek error = fget_read(td, fd, cap_rights_init(&rights, CAP_PREAD), &fp); 335bcd9e0ddSJohn Baldwin if (error) 336bcd9e0ddSJohn Baldwin return (error); 337bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 338bcd9e0ddSJohn Baldwin error = ESPIPE; 339bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 340bcd9e0ddSJohn Baldwin error = EINVAL; 341bcd9e0ddSJohn Baldwin else 342bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 343bcd9e0ddSJohn Baldwin fdrop(fp, td); 344bcd9e0ddSJohn Baldwin return (error); 345bcd9e0ddSJohn Baldwin } 346bcd9e0ddSJohn Baldwin 347bcd9e0ddSJohn Baldwin /* 348bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 349bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 350bcd9e0ddSJohn Baldwin */ 351bcd9e0ddSJohn Baldwin static int 352bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 353bcd9e0ddSJohn Baldwin struct thread *td; 354bcd9e0ddSJohn Baldwin int fd; 355bcd9e0ddSJohn Baldwin struct file *fp; 356bcd9e0ddSJohn Baldwin struct uio *auio; 357bcd9e0ddSJohn Baldwin off_t offset; 358bcd9e0ddSJohn Baldwin int flags; 359bcd9e0ddSJohn Baldwin { 360bcd9e0ddSJohn Baldwin ssize_t cnt; 36182641acdSAlan Cox int error; 362df8bae1dSRodney W. Grimes #ifdef KTRACE 363552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 364df8bae1dSRodney W. Grimes #endif 365df8bae1dSRodney W. Grimes 36651d1f690SRobert Watson AUDIT_ARG_FD(fd); 36751d1f690SRobert Watson 3684f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3694f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3704f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3714f8d23d6SPoul-Henning Kamp return(0); 3724f8d23d6SPoul-Henning Kamp } 373552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 374bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 375552afd9cSPoul-Henning Kamp auio->uio_td = td; 376df8bae1dSRodney W. Grimes #ifdef KTRACE 377552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 378552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 379df8bae1dSRodney W. Grimes #endif 380552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 381bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 382552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 383df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 384df8bae1dSRodney W. Grimes error = 0; 385279d7226SMatthew Dillon } 386552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 387df8bae1dSRodney W. Grimes #ifdef KTRACE 388552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 389552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 390b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 391df8bae1dSRodney W. Grimes } 392df8bae1dSRodney W. Grimes #endif 393b40ce416SJulian Elischer td->td_retval[0] = cnt; 394df8bae1dSRodney W. Grimes return (error); 395df8bae1dSRodney W. Grimes } 396df8bae1dSRodney W. Grimes 397d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 398df8bae1dSRodney W. Grimes struct write_args { 399df8bae1dSRodney W. Grimes int fd; 400134e06feSBruce Evans const void *buf; 401134e06feSBruce Evans size_t nbyte; 402df8bae1dSRodney W. Grimes }; 403d2d3e875SBruce Evans #endif 40426f9a767SRodney W. Grimes int 4058451d0ddSKip Macy sys_write(td, uap) 406b40ce416SJulian Elischer struct thread *td; 407b064d43dSMatthew Dillon struct write_args *uap; 408df8bae1dSRodney W. Grimes { 409bcd9e0ddSJohn Baldwin struct uio auio; 410bcd9e0ddSJohn Baldwin struct iovec aiov; 411279d7226SMatthew Dillon int error; 412df8bae1dSRodney W. Grimes 413526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 414bcd9e0ddSJohn Baldwin return (EINVAL); 415bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 416bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 417bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 418bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 419bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 420bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 421bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 422279d7226SMatthew Dillon return(error); 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes 425df8bae1dSRodney W. Grimes /* 4260c14ff0eSRobert Watson * Positioned write system call. 4274160ccd9SAlan Cox */ 4284160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 4294160ccd9SAlan Cox struct pwrite_args { 4304160ccd9SAlan Cox int fd; 4314160ccd9SAlan Cox const void *buf; 4324160ccd9SAlan Cox size_t nbyte; 4338fe387abSDmitrij Tejblum int pad; 4344160ccd9SAlan Cox off_t offset; 4354160ccd9SAlan Cox }; 4364160ccd9SAlan Cox #endif 4374160ccd9SAlan Cox int 4388451d0ddSKip Macy sys_pwrite(td, uap) 439b40ce416SJulian Elischer struct thread *td; 440b064d43dSMatthew Dillon struct pwrite_args *uap; 4414160ccd9SAlan Cox { 4424160ccd9SAlan Cox struct uio auio; 4434160ccd9SAlan Cox struct iovec aiov; 444bcd9e0ddSJohn Baldwin int error; 4454160ccd9SAlan Cox 446526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 447bcd9e0ddSJohn Baldwin return (EINVAL); 448bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 449bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 4504160ccd9SAlan Cox auio.uio_iov = &aiov; 4514160ccd9SAlan Cox auio.uio_iovcnt = 1; 452bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 4534160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 454bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, &auio, uap->offset); 4554160ccd9SAlan Cox return(error); 4564160ccd9SAlan Cox } 4574160ccd9SAlan Cox 4580538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 459c2815ad5SPeter Wemm int 460c2815ad5SPeter Wemm freebsd6_pwrite(td, uap) 461c2815ad5SPeter Wemm struct thread *td; 462c2815ad5SPeter Wemm struct freebsd6_pwrite_args *uap; 463c2815ad5SPeter Wemm { 464c2815ad5SPeter Wemm struct pwrite_args oargs; 465c2815ad5SPeter Wemm 466c2815ad5SPeter Wemm oargs.fd = uap->fd; 467c2815ad5SPeter Wemm oargs.buf = uap->buf; 468c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 469c2815ad5SPeter Wemm oargs.offset = uap->offset; 4708451d0ddSKip Macy return (sys_pwrite(td, &oargs)); 471c2815ad5SPeter Wemm } 4720538aafcSKonstantin Belousov #endif 473c2815ad5SPeter Wemm 4744160ccd9SAlan Cox /* 4750c14ff0eSRobert Watson * Gather write system call. 476df8bae1dSRodney W. Grimes */ 477d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 478df8bae1dSRodney W. Grimes struct writev_args { 479df8bae1dSRodney W. Grimes int fd; 480df8bae1dSRodney W. Grimes struct iovec *iovp; 481df8bae1dSRodney W. Grimes u_int iovcnt; 482df8bae1dSRodney W. Grimes }; 483d2d3e875SBruce Evans #endif 48426f9a767SRodney W. Grimes int 4858451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap) 486df8bae1dSRodney W. Grimes { 487b88ec951SJohn Baldwin struct uio *auio; 488b88ec951SJohn Baldwin int error; 489b88ec951SJohn Baldwin 490b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 491b88ec951SJohn Baldwin if (error) 492b88ec951SJohn Baldwin return (error); 493b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 494b88ec951SJohn Baldwin free(auio, M_IOV); 495b88ec951SJohn Baldwin return (error); 496b88ec951SJohn Baldwin } 497b88ec951SJohn Baldwin 498b88ec951SJohn Baldwin int 499b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 500b88ec951SJohn Baldwin { 501b064d43dSMatthew Dillon struct file *fp; 5027008be5bSPawel Jakub Dawidek cap_rights_t rights; 503bcd9e0ddSJohn Baldwin int error; 504bcd9e0ddSJohn Baldwin 5057008be5bSPawel Jakub Dawidek error = fget_write(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp); 506bcd9e0ddSJohn Baldwin if (error) 507af56abaaSJohn Baldwin return (error); 508bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 509bcd9e0ddSJohn Baldwin fdrop(fp, td); 510bcd9e0ddSJohn Baldwin return (error); 511bcd9e0ddSJohn Baldwin } 512bcd9e0ddSJohn Baldwin 513bcd9e0ddSJohn Baldwin /* 5140c14ff0eSRobert Watson * Gather positioned write system call. 515bcd9e0ddSJohn Baldwin */ 516bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 517bcd9e0ddSJohn Baldwin struct pwritev_args { 518bcd9e0ddSJohn Baldwin int fd; 519bcd9e0ddSJohn Baldwin struct iovec *iovp; 520bcd9e0ddSJohn Baldwin u_int iovcnt; 521bcd9e0ddSJohn Baldwin off_t offset; 522bcd9e0ddSJohn Baldwin }; 523bcd9e0ddSJohn Baldwin #endif 524bcd9e0ddSJohn Baldwin int 5258451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap) 526bcd9e0ddSJohn Baldwin { 527bcd9e0ddSJohn Baldwin struct uio *auio; 528bcd9e0ddSJohn Baldwin int error; 529bcd9e0ddSJohn Baldwin 530bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 531bcd9e0ddSJohn Baldwin if (error) 532bcd9e0ddSJohn Baldwin return (error); 533bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 534bcd9e0ddSJohn Baldwin free(auio, M_IOV); 535bcd9e0ddSJohn Baldwin return (error); 536bcd9e0ddSJohn Baldwin } 537bcd9e0ddSJohn Baldwin 538bcd9e0ddSJohn Baldwin int 539bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset) 540bcd9e0ddSJohn Baldwin struct thread *td; 541bcd9e0ddSJohn Baldwin struct uio *auio; 542bcd9e0ddSJohn Baldwin int fd; 543bcd9e0ddSJohn Baldwin off_t offset; 544bcd9e0ddSJohn Baldwin { 545bcd9e0ddSJohn Baldwin struct file *fp; 5467008be5bSPawel Jakub Dawidek cap_rights_t rights; 547bcd9e0ddSJohn Baldwin int error; 548bcd9e0ddSJohn Baldwin 5497008be5bSPawel Jakub Dawidek error = fget_write(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp); 550bcd9e0ddSJohn Baldwin if (error) 551af56abaaSJohn Baldwin return (error); 552bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 553bcd9e0ddSJohn Baldwin error = ESPIPE; 554bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 555bcd9e0ddSJohn Baldwin error = EINVAL; 556bcd9e0ddSJohn Baldwin else 557bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 558bcd9e0ddSJohn Baldwin fdrop(fp, td); 559bcd9e0ddSJohn Baldwin return (error); 560bcd9e0ddSJohn Baldwin } 561bcd9e0ddSJohn Baldwin 562bcd9e0ddSJohn Baldwin /* 563bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 564bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 565bcd9e0ddSJohn Baldwin */ 566bcd9e0ddSJohn Baldwin static int 567bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 568bcd9e0ddSJohn Baldwin struct thread *td; 569bcd9e0ddSJohn Baldwin int fd; 570bcd9e0ddSJohn Baldwin struct file *fp; 571bcd9e0ddSJohn Baldwin struct uio *auio; 572bcd9e0ddSJohn Baldwin off_t offset; 573bcd9e0ddSJohn Baldwin int flags; 574bcd9e0ddSJohn Baldwin { 575bcd9e0ddSJohn Baldwin ssize_t cnt; 576552afd9cSPoul-Henning Kamp int error; 577df8bae1dSRodney W. Grimes #ifdef KTRACE 578552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 579df8bae1dSRodney W. Grimes #endif 580df8bae1dSRodney W. Grimes 58151d1f690SRobert Watson AUDIT_ARG_FD(fd); 582552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 583552afd9cSPoul-Henning Kamp auio->uio_td = td; 584bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 585df8bae1dSRodney W. Grimes #ifdef KTRACE 586552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 587552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 588df8bae1dSRodney W. Grimes #endif 589552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 590ad9789f6SKonstantin Belousov if (fp->f_type == DTYPE_VNODE && 591ad9789f6SKonstantin Belousov (fp->f_vnread_flags & FDEVFS_VNODE) == 0) 5929440653dSMatthew Dillon bwillwrite(); 593bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 594552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 595df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 596df8bae1dSRodney W. Grimes error = 0; 597bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5986f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 599b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 6007a6f3d78SJohn Baldwin tdsignal(td, SIGPIPE); 601b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 60219eb87d2SJohn Baldwin } 603df8bae1dSRodney W. Grimes } 604552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 605df8bae1dSRodney W. Grimes #ifdef KTRACE 606552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 607552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 608b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes #endif 611b40ce416SJulian Elischer td->td_retval[0] = cnt; 612df8bae1dSRodney W. Grimes return (error); 613df8bae1dSRodney W. Grimes } 614df8bae1dSRodney W. Grimes 615e4650294SJohn Baldwin /* 616e4650294SJohn Baldwin * Truncate a file given a file descriptor. 617e4650294SJohn Baldwin * 618e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 619e4650294SJohn Baldwin * descriptor isn't writable. 620e4650294SJohn Baldwin */ 621e4650294SJohn Baldwin int 622e4650294SJohn Baldwin kern_ftruncate(td, fd, length) 623e4650294SJohn Baldwin struct thread *td; 624e4650294SJohn Baldwin int fd; 625e4650294SJohn Baldwin off_t length; 626e4650294SJohn Baldwin { 627e4650294SJohn Baldwin struct file *fp; 6287008be5bSPawel Jakub Dawidek cap_rights_t rights; 629e4650294SJohn Baldwin int error; 630e4650294SJohn Baldwin 63114961ba7SRobert Watson AUDIT_ARG_FD(fd); 632e4650294SJohn Baldwin if (length < 0) 633e4650294SJohn Baldwin return (EINVAL); 6347008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp); 635e4650294SJohn Baldwin if (error) 636e4650294SJohn Baldwin return (error); 63714961ba7SRobert Watson AUDIT_ARG_FILE(td->td_proc, fp); 638e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 639e4650294SJohn Baldwin fdrop(fp, td); 640e4650294SJohn Baldwin return (EINVAL); 641e4650294SJohn Baldwin } 642e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 643e4650294SJohn Baldwin fdrop(fp, td); 644e4650294SJohn Baldwin return (error); 645e4650294SJohn Baldwin } 646e4650294SJohn Baldwin 647e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 648e4650294SJohn Baldwin struct ftruncate_args { 649e4650294SJohn Baldwin int fd; 650e4650294SJohn Baldwin int pad; 651e4650294SJohn Baldwin off_t length; 652e4650294SJohn Baldwin }; 653e4650294SJohn Baldwin #endif 654e4650294SJohn Baldwin int 6558451d0ddSKip Macy sys_ftruncate(td, uap) 656e4650294SJohn Baldwin struct thread *td; 657e4650294SJohn Baldwin struct ftruncate_args *uap; 658e4650294SJohn Baldwin { 659e4650294SJohn Baldwin 660e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 661e4650294SJohn Baldwin } 662e4650294SJohn Baldwin 663e4650294SJohn Baldwin #if defined(COMPAT_43) 664e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 665e4650294SJohn Baldwin struct oftruncate_args { 666e4650294SJohn Baldwin int fd; 667e4650294SJohn Baldwin long length; 668e4650294SJohn Baldwin }; 669e4650294SJohn Baldwin #endif 670e4650294SJohn Baldwin int 671e4650294SJohn Baldwin oftruncate(td, uap) 672e4650294SJohn Baldwin struct thread *td; 673e4650294SJohn Baldwin struct oftruncate_args *uap; 674e4650294SJohn Baldwin { 675e4650294SJohn Baldwin 676e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 677e4650294SJohn Baldwin } 678e4650294SJohn Baldwin #endif /* COMPAT_43 */ 679e4650294SJohn Baldwin 680d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 681df8bae1dSRodney W. Grimes struct ioctl_args { 682df8bae1dSRodney W. Grimes int fd; 683069e9bc1SDoug Rabson u_long com; 684df8bae1dSRodney W. Grimes caddr_t data; 685df8bae1dSRodney W. Grimes }; 686d2d3e875SBruce Evans #endif 687df8bae1dSRodney W. Grimes /* ARGSUSED */ 68826f9a767SRodney W. Grimes int 6898451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap) 690df8bae1dSRodney W. Grimes { 69150ae6690SHans Petter Selasky u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN); 6923e15c66fSPoul-Henning Kamp u_long com; 6939fddcc66SRuslan Ermilov int arg, error; 6943e15c66fSPoul-Henning Kamp u_int size; 6959fddcc66SRuslan Ermilov caddr_t data; 696df8bae1dSRodney W. Grimes 6979fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6989fc6aa06SPoul-Henning Kamp printf( 6999fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 700431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 7019fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 7029fc6aa06SPoul-Henning Kamp } 703d9f46233SJohn Baldwin com = uap->com; 704df8bae1dSRodney W. Grimes 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 707df8bae1dSRodney W. Grimes * copied to/from the user's address space. 708df8bae1dSRodney W. Grimes */ 709df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 710ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 711ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 7122de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 7132de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 7142de92a38SPeter Wemm #else 7152de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 7162de92a38SPeter Wemm #endif 7179fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 718426da3bcSAlfred Perlstein return (ENOTTY); 719279d7226SMatthew Dillon 720ca51b19bSPoul-Henning Kamp if (size > 0) { 721715457f6SDavid E. O'Brien if (com & IOC_VOID) { 7229fddcc66SRuslan Ermilov /* Integer argument. */ 7239fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 7249fddcc66SRuslan Ermilov data = (void *)&arg; 7259fddcc66SRuslan Ermilov size = 0; 7265cbf44bfSMateusz Guzik } else { 7270ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 728715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 7290ecd606bSHans Petter Selasky else 7300ecd606bSHans Petter Selasky data = smalldata; 7315cbf44bfSMateusz Guzik } 7329fddcc66SRuslan Ermilov } else 7339fddcc66SRuslan Ermilov data = (void *)&uap->data; 734df8bae1dSRodney W. Grimes if (com & IOC_IN) { 735df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 7365cbf44bfSMateusz Guzik if (error != 0) 7375cbf44bfSMateusz Guzik goto out; 738ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 739df8bae1dSRodney W. Grimes /* 740df8bae1dSRodney W. Grimes * Zero the buffer so the user always 741df8bae1dSRodney W. Grimes * gets back something deterministic. 742df8bae1dSRodney W. Grimes */ 743df8bae1dSRodney W. Grimes bzero(data, size); 744279d7226SMatthew Dillon } 745df8bae1dSRodney W. Grimes 746d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 747d9f46233SJohn Baldwin 748d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 749d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 750d9f46233SJohn Baldwin 7515cbf44bfSMateusz Guzik out: 7520ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 7539fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 754d9f46233SJohn Baldwin return (error); 755d9f46233SJohn Baldwin } 756d9f46233SJohn Baldwin 757d9f46233SJohn Baldwin int 758d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 759d9f46233SJohn Baldwin { 760d9f46233SJohn Baldwin struct file *fp; 761d9f46233SJohn Baldwin struct filedesc *fdp; 7627008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES 7637008be5bSPawel Jakub Dawidek cap_rights_t rights; 7647008be5bSPawel Jakub Dawidek #endif 7652609222aSPawel Jakub Dawidek int error, tmp, locked; 766d9f46233SJohn Baldwin 76764c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 76864c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7692609222aSPawel Jakub Dawidek 770d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7712609222aSPawel Jakub Dawidek 772d9f46233SJohn Baldwin switch (com) { 773d9f46233SJohn Baldwin case FIONCLEX: 774d9f46233SJohn Baldwin case FIOCLEX: 7755e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7762609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7772609222aSPawel Jakub Dawidek break; 7782609222aSPawel Jakub Dawidek default: 7792609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7802609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7812609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7822609222aSPawel Jakub Dawidek #else 7832609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7842609222aSPawel Jakub Dawidek #endif 7852609222aSPawel Jakub Dawidek break; 7862609222aSPawel Jakub Dawidek } 7872609222aSPawel Jakub Dawidek 7882609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7892609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7902609222aSPawel Jakub Dawidek error = EBADF; 7912609222aSPawel Jakub Dawidek goto out; 7922609222aSPawel Jakub Dawidek } 7932609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7942609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7952609222aSPawel Jakub Dawidek goto out; 7962609222aSPawel Jakub Dawidek } 7972609222aSPawel Jakub Dawidek fhold(fp); 7982609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 7992609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8002609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 8012609222aSPawel Jakub Dawidek } 8022609222aSPawel Jakub Dawidek #else 8037008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp); 8047008be5bSPawel Jakub Dawidek if (error != 0) { 8052609222aSPawel Jakub Dawidek fp = NULL; 8062609222aSPawel Jakub Dawidek goto out; 8072609222aSPawel Jakub Dawidek } 8082609222aSPawel Jakub Dawidek #endif 8092609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 8102609222aSPawel Jakub Dawidek error = EBADF; 8112609222aSPawel Jakub Dawidek goto out; 8122609222aSPawel Jakub Dawidek } 8132609222aSPawel Jakub Dawidek 8142609222aSPawel Jakub Dawidek switch (com) { 8152609222aSPawel Jakub Dawidek case FIONCLEX: 8162609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 8172609222aSPawel Jakub Dawidek goto out; 8182609222aSPawel Jakub Dawidek case FIOCLEX: 8192609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 820d9f46233SJohn Baldwin goto out; 821d9f46233SJohn Baldwin case FIONBIO: 822bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 823397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 824df8bae1dSRodney W. Grimes else 825397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 8268ccf264fSPoul-Henning Kamp data = (void *)&tmp; 827d9f46233SJohn Baldwin break; 828d9f46233SJohn Baldwin case FIOASYNC: 829bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 830397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 831df8bae1dSRodney W. Grimes else 832397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 8338ccf264fSPoul-Henning Kamp data = (void *)&tmp; 834d9f46233SJohn Baldwin break; 835df8bae1dSRodney W. Grimes } 8368ccf264fSPoul-Henning Kamp 8378ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 838d9f46233SJohn Baldwin out: 8392609222aSPawel Jakub Dawidek switch (locked) { 8402609222aSPawel Jakub Dawidek case LA_XLOCKED: 8412609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 8422609222aSPawel Jakub Dawidek break; 8432609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 8442609222aSPawel Jakub Dawidek case LA_SLOCKED: 8452609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8462609222aSPawel Jakub Dawidek break; 8472609222aSPawel Jakub Dawidek #endif 8482609222aSPawel Jakub Dawidek default: 8492609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8502609222aSPawel Jakub Dawidek break; 8512609222aSPawel Jakub Dawidek } 8522609222aSPawel Jakub Dawidek if (fp != NULL) 853b40ce416SJulian Elischer fdrop(fp, td); 854df8bae1dSRodney W. Grimes return (error); 855df8bae1dSRodney W. Grimes } 856df8bae1dSRodney W. Grimes 857125dcf8cSKonstantin Belousov int 858125dcf8cSKonstantin Belousov poll_no_poll(int events) 859125dcf8cSKonstantin Belousov { 860125dcf8cSKonstantin Belousov /* 861125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 862125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 863125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 864125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 865125dcf8cSKonstantin Belousov * of specific filesystem implementations. 866125dcf8cSKonstantin Belousov */ 867125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 868125dcf8cSKonstantin Belousov return (POLLNVAL); 869125dcf8cSKonstantin Belousov 870125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 871125dcf8cSKonstantin Belousov } 872125dcf8cSKonstantin Belousov 873066d836bSKonstantin Belousov int 8748451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 875066d836bSKonstantin Belousov { 876066d836bSKonstantin Belousov struct timespec ts; 877066d836bSKonstantin Belousov struct timeval tv, *tvp; 878066d836bSKonstantin Belousov sigset_t set, *uset; 879066d836bSKonstantin Belousov int error; 880066d836bSKonstantin Belousov 881066d836bSKonstantin Belousov if (uap->ts != NULL) { 882066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 883066d836bSKonstantin Belousov if (error != 0) 884066d836bSKonstantin Belousov return (error); 885066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 886066d836bSKonstantin Belousov tvp = &tv; 887066d836bSKonstantin Belousov } else 888066d836bSKonstantin Belousov tvp = NULL; 889066d836bSKonstantin Belousov if (uap->sm != NULL) { 890066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 891066d836bSKonstantin Belousov if (error != 0) 892066d836bSKonstantin Belousov return (error); 893066d836bSKonstantin Belousov uset = &set; 894066d836bSKonstantin Belousov } else 895066d836bSKonstantin Belousov uset = NULL; 896066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 897066d836bSKonstantin Belousov uset, NFDBITS)); 898066d836bSKonstantin Belousov } 899066d836bSKonstantin Belousov 900066d836bSKonstantin Belousov int 901066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 902066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 903066d836bSKonstantin Belousov { 904066d836bSKonstantin Belousov int error; 905066d836bSKonstantin Belousov 906066d836bSKonstantin Belousov if (uset != NULL) { 907066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 908066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 909066d836bSKonstantin Belousov if (error != 0) 910066d836bSKonstantin Belousov return (error); 911066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 912066d836bSKonstantin Belousov /* 913066d836bSKonstantin Belousov * Make sure that ast() is called on return to 914066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 915066d836bSKonstantin Belousov * sigmask. 916066d836bSKonstantin Belousov */ 917066d836bSKonstantin Belousov thread_lock(td); 918066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 919066d836bSKonstantin Belousov thread_unlock(td); 920066d836bSKonstantin Belousov } 921066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 922066d836bSKonstantin Belousov return (error); 923066d836bSKonstantin Belousov } 924066d836bSKonstantin Belousov 925d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 926df8bae1dSRodney W. Grimes struct select_args { 927b08f7993SSujal Patel int nd; 928df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 929df8bae1dSRodney W. Grimes struct timeval *tv; 930df8bae1dSRodney W. Grimes }; 931d2d3e875SBruce Evans #endif 93226f9a767SRodney W. Grimes int 9338451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 934df8bae1dSRodney W. Grimes { 9358f19eb88SIan Dowse struct timeval tv, *tvp; 9368f19eb88SIan Dowse int error; 9378f19eb88SIan Dowse 9388f19eb88SIan Dowse if (uap->tv != NULL) { 9398f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 9408f19eb88SIan Dowse if (error) 9418f19eb88SIan Dowse return (error); 9428f19eb88SIan Dowse tvp = &tv; 9438f19eb88SIan Dowse } else 9448f19eb88SIan Dowse tvp = NULL; 9458f19eb88SIan Dowse 946b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 947b55ef216SKonstantin Belousov NFDBITS)); 9488f19eb88SIan Dowse } 9498f19eb88SIan Dowse 95056be1b9aSKonstantin Belousov /* 95156be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 95256be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 95356be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 95456be1b9aSKonstantin Belousov * 95556be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 95656be1b9aSKonstantin Belousov * 95756be1b9aSKonstantin Belousov * nd is fd_lastfile + 1. 95856be1b9aSKonstantin Belousov */ 95956be1b9aSKonstantin Belousov static int 96056be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 96156be1b9aSKonstantin Belousov { 96256be1b9aSKonstantin Belousov char *addr, *oaddr; 96356be1b9aSKonstantin Belousov int b, i, res; 96456be1b9aSKonstantin Belousov uint8_t bits; 96556be1b9aSKonstantin Belousov 96656be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 96756be1b9aSKonstantin Belousov return (0); 96856be1b9aSKonstantin Belousov 96956be1b9aSKonstantin Belousov oaddr = NULL; 97056be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 97156be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 97256be1b9aSKonstantin Belousov b = i / NBBY; 97356be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 97456be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 97556be1b9aSKonstantin Belousov #else 97656be1b9aSKonstantin Belousov addr = (char *)fd_in; 97756be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 97856be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 97956be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 98056be1b9aSKonstantin Belousov } else { 98156be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 98256be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 98356be1b9aSKonstantin Belousov } 98456be1b9aSKonstantin Belousov #endif 98556be1b9aSKonstantin Belousov if (addr != oaddr) { 98656be1b9aSKonstantin Belousov res = fubyte(addr); 98756be1b9aSKonstantin Belousov if (res == -1) 98856be1b9aSKonstantin Belousov return (EFAULT); 98956be1b9aSKonstantin Belousov oaddr = addr; 99056be1b9aSKonstantin Belousov bits = res; 99156be1b9aSKonstantin Belousov } 99256be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 99356be1b9aSKonstantin Belousov return (EBADF); 99456be1b9aSKonstantin Belousov } 99556be1b9aSKonstantin Belousov return (0); 99656be1b9aSKonstantin Belousov } 99756be1b9aSKonstantin Belousov 9988f19eb88SIan Dowse int 9998f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 1000b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 10018f19eb88SIan Dowse { 1002426da3bcSAlfred Perlstein struct filedesc *fdp; 1003d5e4d7e1SBruce Evans /* 1004d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 1005d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 1006d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 1007d5e4d7e1SBruce Evans * of 256. 1008d5e4d7e1SBruce Evans */ 1009d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 1010eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 1011cf5e4fe6SDavide Italiano struct timeval rtv; 1012cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 1013b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 1014cf5e4fe6SDavide Italiano int error, lf, ndu; 1015df8bae1dSRodney W. Grimes 10168f19eb88SIan Dowse if (nd < 0) 1017acbfbfeaSSujal Patel return (EINVAL); 1018426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 101956be1b9aSKonstantin Belousov ndu = nd; 102056be1b9aSKonstantin Belousov lf = fdp->fd_lastfile; 102156be1b9aSKonstantin Belousov if (nd > lf + 1) 102256be1b9aSKonstantin Belousov nd = lf + 1; 102356be1b9aSKonstantin Belousov 102456be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 102556be1b9aSKonstantin Belousov if (error != 0) 102656be1b9aSKonstantin Belousov return (error); 102756be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 102856be1b9aSKonstantin Belousov if (error != 0) 102956be1b9aSKonstantin Belousov return (error); 103056be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 103156be1b9aSKonstantin Belousov if (error != 0) 103256be1b9aSKonstantin Belousov return (error); 1033b08f7993SSujal Patel 1034d5e4d7e1SBruce Evans /* 1035d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 1036d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 1037d5e4d7e1SBruce Evans */ 10388f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 1039d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 1040b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 1041d5e4d7e1SBruce Evans nbufbytes = 0; 10428f19eb88SIan Dowse if (fd_in != NULL) 1043d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10448f19eb88SIan Dowse if (fd_ou != NULL) 1045d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10468f19eb88SIan Dowse if (fd_ex != NULL) 1047d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1048d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1049d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1050d5e4d7e1SBruce Evans else 1051a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1052b08f7993SSujal Patel 1053b08f7993SSujal Patel /* 1054d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1055d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1056d5e4d7e1SBruce Evans * together. 1057b08f7993SSujal Patel */ 1058d5e4d7e1SBruce Evans sbp = selbits; 1059df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1060d5e4d7e1SBruce Evans do { \ 1061841c0c7eSNathan Whitehorn if (name == NULL) { \ 1062d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1063841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1064841c0c7eSNathan Whitehorn } else { \ 1065d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1066d5e4d7e1SBruce Evans obits[x] = sbp; \ 1067d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1068b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1069265fc98fSSeigo Tanimura if (error != 0) \ 1070ace8398dSJeff Roberson goto done; \ 1071b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1072b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1073e04ac2feSJohn Baldwin } \ 1074d5e4d7e1SBruce Evans } while (0) 10758f19eb88SIan Dowse getbits(fd_in, 0); 10768f19eb88SIan Dowse getbits(fd_ou, 1); 10778f19eb88SIan Dowse getbits(fd_ex, 2); 1078df8bae1dSRodney W. Grimes #undef getbits 1079841c0c7eSNathan Whitehorn 1080841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1081841c0c7eSNathan Whitehorn /* 1082841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1083841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1084841c0c7eSNathan Whitehorn * generic. 1085841c0c7eSNathan Whitehorn */ 1086841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1087841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1088841c0c7eSNathan Whitehorn int i; \ 1089841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1090841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1091841c0c7eSNathan Whitehorn } 1092841c0c7eSNathan Whitehorn #else 1093841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1094841c0c7eSNathan Whitehorn #endif 1095841c0c7eSNathan Whitehorn 1096841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1097841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1098841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1099841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1100841c0c7eSNathan Whitehorn 1101d5e4d7e1SBruce Evans if (nbufbytes != 0) 1102d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1103df8bae1dSRodney W. Grimes 1104cf5e4fe6SDavide Italiano precision = 0; 11058f19eb88SIan Dowse if (tvp != NULL) { 1106cf5e4fe6SDavide Italiano rtv = *tvp; 1107cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1108cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1109df8bae1dSRodney W. Grimes error = EINVAL; 1110ace8398dSJeff Roberson goto done; 1111df8bae1dSRodney W. Grimes } 111221a37a71SAlexander Motin if (!timevalisset(&rtv)) 1113980c545dSAlexander Motin asbt = 0; 111421a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1115cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1116cf5e4fe6SDavide Italiano precision = rsbt; 1117cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1118cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1119cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 11204bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1121cf5e4fe6SDavide Italiano asbt += rsbt; 112221a37a71SAlexander Motin else 1123980c545dSAlexander Motin asbt = -1; 1124980c545dSAlexander Motin } else 1125980c545dSAlexander Motin asbt = -1; 1126cf5e4fe6SDavide Italiano } else 1127cf5e4fe6SDavide Italiano asbt = -1; 1128ace8398dSJeff Roberson seltdinit(td); 1129ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1130ace8398dSJeff Roberson for (;;) { 11318f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1132ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1133ace8398dSJeff Roberson break; 1134cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1135ace8398dSJeff Roberson if (error) 1136ace8398dSJeff Roberson break; 1137ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1138ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1139ace8398dSJeff Roberson break; 114085f190e4SAlfred Perlstein } 1141ace8398dSJeff Roberson seltdclear(td); 1142265fc98fSSeigo Tanimura 1143df8bae1dSRodney W. Grimes done: 1144df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1145df8bae1dSRodney W. Grimes if (error == ERESTART) 1146df8bae1dSRodney W. Grimes error = EINTR; 1147df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1148df8bae1dSRodney W. Grimes error = 0; 1149841c0c7eSNathan Whitehorn 1150841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1151841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1152841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1153841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1154841c0c7eSNathan Whitehorn #undef swizzle_fdset 1155841c0c7eSNathan Whitehorn 1156df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1157b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1158df8bae1dSRodney W. Grimes error = error2; 1159df8bae1dSRodney W. Grimes if (error == 0) { 1160df8bae1dSRodney W. Grimes int error2; 1161df8bae1dSRodney W. Grimes 11628f19eb88SIan Dowse putbits(fd_in, 0); 11638f19eb88SIan Dowse putbits(fd_ou, 1); 11648f19eb88SIan Dowse putbits(fd_ex, 2); 1165df8bae1dSRodney W. Grimes #undef putbits 1166df8bae1dSRodney W. Grimes } 1167d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1168d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1169ad2edad9SMatthew Dillon 1170df8bae1dSRodney W. Grimes return (error); 1171df8bae1dSRodney W. Grimes } 117211b763dfSJeff Roberson /* 117311b763dfSJeff Roberson * Convert a select bit set to poll flags. 1174748b9df6SJeff Roberson * 117511b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 117611b763dfSJeff Roberson * return this as a set bit in any set. 117711b763dfSJeff Roberson */ 117811b763dfSJeff Roberson static int select_flags[3] = { 117911b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 118011b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 118161e53a38SKonstantin Belousov POLLRDBAND | POLLERR 118211b763dfSJeff Roberson }; 118311b763dfSJeff Roberson 118411b763dfSJeff Roberson /* 118511b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 118611b763dfSJeff Roberson * bit position in the fd_mask array. 118711b763dfSJeff Roberson */ 118811b763dfSJeff Roberson static __inline int 118960b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 119011b763dfSJeff Roberson { 119111b763dfSJeff Roberson int flags; 119211b763dfSJeff Roberson int msk; 119311b763dfSJeff Roberson 119411b763dfSJeff Roberson flags = 0; 119511b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 119611b763dfSJeff Roberson if (ibits[msk] == NULL) 119711b763dfSJeff Roberson continue; 119860b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 119911b763dfSJeff Roberson continue; 120011b763dfSJeff Roberson flags |= select_flags[msk]; 120111b763dfSJeff Roberson } 120211b763dfSJeff Roberson return (flags); 120311b763dfSJeff Roberson } 120411b763dfSJeff Roberson 120511b763dfSJeff Roberson /* 120611b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 120711b763dfSJeff Roberson * input bits originally requested. 120811b763dfSJeff Roberson */ 120911b763dfSJeff Roberson static __inline int 121011b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 121111b763dfSJeff Roberson { 121211b763dfSJeff Roberson int msk; 121311b763dfSJeff Roberson int n; 121411b763dfSJeff Roberson 121511b763dfSJeff Roberson n = 0; 121611b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 121711b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 121811b763dfSJeff Roberson continue; 121911b763dfSJeff Roberson if (ibits[msk] == NULL) 122011b763dfSJeff Roberson continue; 122111b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 122211b763dfSJeff Roberson continue; 122311b763dfSJeff Roberson /* 122411b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 122511b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 122611b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 122711b763dfSJeff Roberson * call selsetbits twice as a result. 122811b763dfSJeff Roberson */ 122911b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 123011b763dfSJeff Roberson continue; 123111b763dfSJeff Roberson obits[msk][idx] |= bit; 123211b763dfSJeff Roberson n++; 123311b763dfSJeff Roberson } 123411b763dfSJeff Roberson 123511b763dfSJeff Roberson return (n); 123611b763dfSJeff Roberson } 1237df8bae1dSRodney W. Grimes 1238a9d2f8d8SRobert Watson static __inline int 1239a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1240a9d2f8d8SRobert Watson { 12417008be5bSPawel Jakub Dawidek cap_rights_t rights; 1242a9d2f8d8SRobert Watson 1243ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT); 1244ed5848c8SPawel Jakub Dawidek 1245b7a39e9eSMateusz Guzik return (fget_unlocked(fdp, fd, &rights, fpp, NULL)); 1246a9d2f8d8SRobert Watson } 1247a9d2f8d8SRobert Watson 1248ace8398dSJeff Roberson /* 1249ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1250ace8398dSJeff Roberson * completion. 1251ace8398dSJeff Roberson */ 1252ace8398dSJeff Roberson static int 1253ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1254ace8398dSJeff Roberson { 125511b763dfSJeff Roberson struct filedesc *fdp; 125611b763dfSJeff Roberson struct selinfo *si; 1257ace8398dSJeff Roberson struct seltd *stp; 1258ace8398dSJeff Roberson struct selfd *sfp; 1259ace8398dSJeff Roberson struct selfd *sfn; 1260ace8398dSJeff Roberson struct file *fp; 12619cdacff1SJeff Roberson fd_mask bit; 12629cdacff1SJeff Roberson int fd, ev, n, idx; 1263a9d2f8d8SRobert Watson int error; 1264ace8398dSJeff Roberson 126511b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1266ace8398dSJeff Roberson stp = td->td_sel; 126711b763dfSJeff Roberson n = 0; 1268ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1269ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1270ace8398dSJeff Roberson si = sfp->sf_si; 1271ace8398dSJeff Roberson selfdfree(stp, sfp); 1272ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1273ace8398dSJeff Roberson if (si != NULL) 1274ace8398dSJeff Roberson continue; 1275a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1276a9d2f8d8SRobert Watson if (error) 1277a9d2f8d8SRobert Watson return (error); 127811b763dfSJeff Roberson idx = fd / NFDBITS; 12799cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 128011b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1281bf422e5fSJeff Roberson fdrop(fp, td); 128211b763dfSJeff Roberson if (ev != 0) 128311b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1284ace8398dSJeff Roberson } 1285ace8398dSJeff Roberson stp->st_flags = 0; 1286ace8398dSJeff Roberson td->td_retval[0] = n; 1287ace8398dSJeff Roberson return (0); 1288ace8398dSJeff Roberson } 1289ace8398dSJeff Roberson 1290ace8398dSJeff Roberson /* 1291ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1292ace8398dSJeff Roberson * each selinfo. 1293ace8398dSJeff Roberson */ 1294265fc98fSSeigo Tanimura static int 1295b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 1296b40ce416SJulian Elischer struct thread *td; 1297b08f7993SSujal Patel fd_mask **ibits, **obits; 1298cb226aaaSPoul-Henning Kamp int nfd; 1299df8bae1dSRodney W. Grimes { 130011b763dfSJeff Roberson struct filedesc *fdp; 1301df8bae1dSRodney W. Grimes struct file *fp; 13029cdacff1SJeff Roberson fd_mask bit; 130311b763dfSJeff Roberson int ev, flags, end, fd; 13049cdacff1SJeff Roberson int n, idx; 1305a9d2f8d8SRobert Watson int error; 1306df8bae1dSRodney W. Grimes 130711b763dfSJeff Roberson fdp = td->td_proc->p_fd; 130811b763dfSJeff Roberson n = 0; 13099cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 131011b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 131111b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 131211b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 131311b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 131411b763dfSJeff Roberson if (flags == 0) 1315f082218cSPeter Wemm continue; 1316a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1317a9d2f8d8SRobert Watson if (error) 1318a9d2f8d8SRobert Watson return (error); 1319ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 132011b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1321bf422e5fSJeff Roberson fdrop(fp, td); 132211b763dfSJeff Roberson if (ev != 0) 132311b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1324df8bae1dSRodney W. Grimes } 1325df8bae1dSRodney W. Grimes } 132611b763dfSJeff Roberson 1327b40ce416SJulian Elischer td->td_retval[0] = n; 1328df8bae1dSRodney W. Grimes return (0); 1329df8bae1dSRodney W. Grimes } 1330df8bae1dSRodney W. Grimes 133142d11757SPeter Wemm int 1332186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap) 1333186d9c34SDmitry Chagin { 1334186d9c34SDmitry Chagin struct timespec ts, *tsp; 1335186d9c34SDmitry Chagin 1336186d9c34SDmitry Chagin if (uap->timeout != INFTIM) { 1337186d9c34SDmitry Chagin if (uap->timeout < 0) 1338186d9c34SDmitry Chagin return (EINVAL); 1339186d9c34SDmitry Chagin ts.tv_sec = uap->timeout / 1000; 1340186d9c34SDmitry Chagin ts.tv_nsec = (uap->timeout % 1000) * 1000000; 1341186d9c34SDmitry Chagin tsp = &ts; 1342186d9c34SDmitry Chagin } else 1343186d9c34SDmitry Chagin tsp = NULL; 1344186d9c34SDmitry Chagin 1345186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); 1346186d9c34SDmitry Chagin } 1347186d9c34SDmitry Chagin 1348186d9c34SDmitry Chagin int 1349186d9c34SDmitry Chagin kern_poll(struct thread *td, struct pollfd *fds, u_int nfds, 1350186d9c34SDmitry Chagin struct timespec *tsp, sigset_t *uset) 135142d11757SPeter Wemm { 13522580f4e5SAndre Oppermann struct pollfd *bits; 13532580f4e5SAndre Oppermann struct pollfd smallbits[32]; 1354186d9c34SDmitry Chagin sbintime_t sbt, precision, tmp; 1355186d9c34SDmitry Chagin time_t over; 1356186d9c34SDmitry Chagin struct timespec ts; 1357cf5e4fe6SDavide Italiano int error; 135842d11757SPeter Wemm size_t ni; 135942d11757SPeter Wemm 1360186d9c34SDmitry Chagin precision = 0; 1361186d9c34SDmitry Chagin if (tsp != NULL) { 1362186d9c34SDmitry Chagin if (tsp->tv_sec < 0) 1363186d9c34SDmitry Chagin return (EINVAL); 1364186d9c34SDmitry Chagin if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000) 1365186d9c34SDmitry Chagin return (EINVAL); 1366186d9c34SDmitry Chagin if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1367186d9c34SDmitry Chagin sbt = 0; 1368186d9c34SDmitry Chagin else { 1369186d9c34SDmitry Chagin ts = *tsp; 1370186d9c34SDmitry Chagin if (ts.tv_sec > INT32_MAX / 2) { 1371186d9c34SDmitry Chagin over = ts.tv_sec - INT32_MAX / 2; 1372186d9c34SDmitry Chagin ts.tv_sec -= over; 1373186d9c34SDmitry Chagin } else 1374186d9c34SDmitry Chagin over = 0; 1375186d9c34SDmitry Chagin tmp = tstosbt(ts); 1376186d9c34SDmitry Chagin precision = tmp; 1377186d9c34SDmitry Chagin precision >>= tc_precexp; 1378186d9c34SDmitry Chagin if (TIMESEL(&sbt, tmp)) 1379186d9c34SDmitry Chagin sbt += tc_tick_sbt; 1380186d9c34SDmitry Chagin sbt += tmp; 1381186d9c34SDmitry Chagin } 1382186d9c34SDmitry Chagin } else 1383186d9c34SDmitry Chagin sbt = -1; 1384186d9c34SDmitry Chagin 1385374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1386ace8398dSJeff Roberson return (EINVAL); 138789b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 138842d11757SPeter Wemm if (ni > sizeof(smallbits)) 1389a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 139042d11757SPeter Wemm else 139142d11757SPeter Wemm bits = smallbits; 1392186d9c34SDmitry Chagin error = copyin(fds, bits, ni); 139342d11757SPeter Wemm if (error) 1394ace8398dSJeff Roberson goto done; 1395186d9c34SDmitry Chagin 1396186d9c34SDmitry Chagin if (uset != NULL) { 1397186d9c34SDmitry Chagin error = kern_sigprocmask(td, SIG_SETMASK, uset, 1398186d9c34SDmitry Chagin &td->td_oldsigmask, 0); 1399186d9c34SDmitry Chagin if (error) 1400ace8398dSJeff Roberson goto done; 1401186d9c34SDmitry Chagin td->td_pflags |= TDP_OLDMASK; 1402186d9c34SDmitry Chagin /* 1403186d9c34SDmitry Chagin * Make sure that ast() is called on return to 1404186d9c34SDmitry Chagin * usermode and TDP_OLDMASK is cleared, restoring old 1405186d9c34SDmitry Chagin * sigmask. 1406186d9c34SDmitry Chagin */ 1407186d9c34SDmitry Chagin thread_lock(td); 1408186d9c34SDmitry Chagin td->td_flags |= TDF_ASTPENDING; 1409186d9c34SDmitry Chagin thread_unlock(td); 141042d11757SPeter Wemm } 1411186d9c34SDmitry Chagin 1412ace8398dSJeff Roberson seltdinit(td); 1413ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1414ace8398dSJeff Roberson for (;;) { 14152580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 1416ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1417ace8398dSJeff Roberson break; 1418186d9c34SDmitry Chagin error = seltdwait(td, sbt, precision); 1419ace8398dSJeff Roberson if (error) 1420ace8398dSJeff Roberson break; 1421ace8398dSJeff Roberson error = pollrescan(td); 1422ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1423ace8398dSJeff Roberson break; 142485f190e4SAlfred Perlstein } 1425ace8398dSJeff Roberson seltdclear(td); 1426265fc98fSSeigo Tanimura 142742d11757SPeter Wemm done: 142842d11757SPeter Wemm /* poll is not restarted after signals... */ 142942d11757SPeter Wemm if (error == ERESTART) 143042d11757SPeter Wemm error = EINTR; 143142d11757SPeter Wemm if (error == EWOULDBLOCK) 143242d11757SPeter Wemm error = 0; 143342d11757SPeter Wemm if (error == 0) { 1434186d9c34SDmitry Chagin error = pollout(td, bits, fds, nfds); 143542d11757SPeter Wemm if (error) 143642d11757SPeter Wemm goto out; 143742d11757SPeter Wemm } 143842d11757SPeter Wemm out: 143942d11757SPeter Wemm if (ni > sizeof(smallbits)) 144042d11757SPeter Wemm free(bits, M_TEMP); 144142d11757SPeter Wemm return (error); 144242d11757SPeter Wemm } 144342d11757SPeter Wemm 1444186d9c34SDmitry Chagin int 1445186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap) 1446186d9c34SDmitry Chagin { 1447186d9c34SDmitry Chagin struct timespec ts, *tsp; 1448186d9c34SDmitry Chagin sigset_t set, *ssp; 1449186d9c34SDmitry Chagin int error; 1450186d9c34SDmitry Chagin 1451186d9c34SDmitry Chagin if (uap->ts != NULL) { 1452186d9c34SDmitry Chagin error = copyin(uap->ts, &ts, sizeof(ts)); 1453186d9c34SDmitry Chagin if (error) 1454186d9c34SDmitry Chagin return (error); 1455186d9c34SDmitry Chagin tsp = &ts; 1456186d9c34SDmitry Chagin } else 1457186d9c34SDmitry Chagin tsp = NULL; 1458186d9c34SDmitry Chagin if (uap->set != NULL) { 1459186d9c34SDmitry Chagin error = copyin(uap->set, &set, sizeof(set)); 1460186d9c34SDmitry Chagin if (error) 1461186d9c34SDmitry Chagin return (error); 1462186d9c34SDmitry Chagin ssp = &set; 1463186d9c34SDmitry Chagin } else 1464186d9c34SDmitry Chagin ssp = NULL; 1465186d9c34SDmitry Chagin /* 1466186d9c34SDmitry Chagin * fds is still a pointer to user space. kern_poll() will 1467186d9c34SDmitry Chagin * take care of copyin that array to the kernel space. 1468186d9c34SDmitry Chagin */ 1469186d9c34SDmitry Chagin 1470186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 1471186d9c34SDmitry Chagin } 1472186d9c34SDmitry Chagin 147342d11757SPeter Wemm static int 1474ace8398dSJeff Roberson pollrescan(struct thread *td) 1475ace8398dSJeff Roberson { 1476ace8398dSJeff Roberson struct seltd *stp; 1477ace8398dSJeff Roberson struct selfd *sfp; 1478ace8398dSJeff Roberson struct selfd *sfn; 1479ace8398dSJeff Roberson struct selinfo *si; 1480ace8398dSJeff Roberson struct filedesc *fdp; 1481ace8398dSJeff Roberson struct file *fp; 1482ace8398dSJeff Roberson struct pollfd *fd; 14838e076effSSean Bruno #ifdef CAPABILITIES 14847008be5bSPawel Jakub Dawidek cap_rights_t rights; 14858e076effSSean Bruno #endif 1486ace8398dSJeff Roberson int n; 1487ace8398dSJeff Roberson 1488ace8398dSJeff Roberson n = 0; 1489ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1490ace8398dSJeff Roberson stp = td->td_sel; 1491ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1492ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1493ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1494ace8398dSJeff Roberson si = sfp->sf_si; 1495ace8398dSJeff Roberson selfdfree(stp, sfp); 1496ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1497ace8398dSJeff Roberson if (si != NULL) 1498ace8398dSJeff Roberson continue; 14992609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fd->fd].fde_file; 1500d6f72489SJonathan Anderson #ifdef CAPABILITIES 15012609222aSPawel Jakub Dawidek if (fp == NULL || 15027008be5bSPawel Jakub Dawidek cap_check(cap_rights(fdp, fd->fd), 1503ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1504d6f72489SJonathan Anderson #else 15052609222aSPawel Jakub Dawidek if (fp == NULL) 1506d6f72489SJonathan Anderson #endif 15072609222aSPawel Jakub Dawidek { 1508ace8398dSJeff Roberson fd->revents = POLLNVAL; 1509ace8398dSJeff Roberson n++; 1510ace8398dSJeff Roberson continue; 1511ace8398dSJeff Roberson } 1512d6f72489SJonathan Anderson 1513ace8398dSJeff Roberson /* 1514ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1515ace8398dSJeff Roberson * POLLERR if appropriate. 1516ace8398dSJeff Roberson */ 1517ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1518ace8398dSJeff Roberson if (fd->revents != 0) 1519ace8398dSJeff Roberson n++; 1520ace8398dSJeff Roberson } 1521ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1522ace8398dSJeff Roberson stp->st_flags = 0; 1523ace8398dSJeff Roberson td->td_retval[0] = n; 1524ace8398dSJeff Roberson return (0); 1525ace8398dSJeff Roberson } 1526ace8398dSJeff Roberson 1527ace8398dSJeff Roberson 1528ace8398dSJeff Roberson static int 15296d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd) 15306d8feddaSKonstantin Belousov struct thread *td; 1531ae81968fSRobert Watson struct pollfd *fds; 1532ae81968fSRobert Watson struct pollfd *ufds; 1533ae81968fSRobert Watson u_int nfd; 1534ae81968fSRobert Watson { 1535ae81968fSRobert Watson int error = 0; 1536ae81968fSRobert Watson u_int i = 0; 15376d8feddaSKonstantin Belousov u_int n = 0; 1538ae81968fSRobert Watson 1539ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1540ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1541ae81968fSRobert Watson sizeof(ufds->revents)); 1542ae81968fSRobert Watson if (error) 1543ae81968fSRobert Watson return (error); 15446d8feddaSKonstantin Belousov if (fds->revents != 0) 15456d8feddaSKonstantin Belousov n++; 1546ae81968fSRobert Watson fds++; 1547ae81968fSRobert Watson ufds++; 1548ae81968fSRobert Watson } 15496d8feddaSKonstantin Belousov td->td_retval[0] = n; 1550ae81968fSRobert Watson return (0); 1551ae81968fSRobert Watson } 1552ae81968fSRobert Watson 1553ae81968fSRobert Watson static int 1554b40ce416SJulian Elischer pollscan(td, fds, nfd) 1555b40ce416SJulian Elischer struct thread *td; 155642d11757SPeter Wemm struct pollfd *fds; 1557ea0237edSJonathan Lemon u_int nfd; 155842d11757SPeter Wemm { 1559ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 156042d11757SPeter Wemm struct file *fp; 15618e076effSSean Bruno #ifdef CAPABILITIES 15627008be5bSPawel Jakub Dawidek cap_rights_t rights; 15638e076effSSean Bruno #endif 15642609222aSPawel Jakub Dawidek int i, n = 0; 156542d11757SPeter Wemm 15665e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1567eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1568adf87ab0SMateusz Guzik if (fds->fd > fdp->fd_lastfile) { 156942d11757SPeter Wemm fds->revents = POLLNVAL; 157042d11757SPeter Wemm n++; 1571337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1572337c9691SJordan K. Hubbard fds->revents = 0; 157342d11757SPeter Wemm } else { 15742609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fds->fd].fde_file; 1575d6f72489SJonathan Anderson #ifdef CAPABILITIES 15762609222aSPawel Jakub Dawidek if (fp == NULL || 15772609222aSPawel Jakub Dawidek cap_check(cap_rights(fdp, fds->fd), 1578ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1579d6f72489SJonathan Anderson #else 15802609222aSPawel Jakub Dawidek if (fp == NULL) 1581d6f72489SJonathan Anderson #endif 15822609222aSPawel Jakub Dawidek { 158342d11757SPeter Wemm fds->revents = POLLNVAL; 158442d11757SPeter Wemm n++; 158542d11757SPeter Wemm } else { 15862087c896SBruce Evans /* 15872087c896SBruce Evans * Note: backend also returns POLLHUP and 15882087c896SBruce Evans * POLLERR if appropriate. 15892087c896SBruce Evans */ 1590ace8398dSJeff Roberson selfdalloc(td, fds); 159113ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1592ea6027a8SRobert Watson td->td_ucred, td); 1593f2159cc7SKonstantin Belousov /* 1594f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1595f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1596f2159cc7SKonstantin Belousov */ 1597f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1598f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1599f2159cc7SKonstantin Belousov 160042d11757SPeter Wemm if (fds->revents != 0) 160142d11757SPeter Wemm n++; 160242d11757SPeter Wemm } 160342d11757SPeter Wemm } 160442d11757SPeter Wemm } 16055e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1606b40ce416SJulian Elischer td->td_retval[0] = n; 160742d11757SPeter Wemm return (0); 160842d11757SPeter Wemm } 160942d11757SPeter Wemm 161042d11757SPeter Wemm /* 1611237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1612237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1613237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1614237abf0cSDavide Italiano * determine which events fired. 1615237abf0cSDavide Italiano */ 1616237abf0cSDavide Italiano int 1617237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1618237abf0cSDavide Italiano { 1619237abf0cSDavide Italiano struct timeval rtv; 1620237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1621237abf0cSDavide Italiano int error; 1622237abf0cSDavide Italiano 162376665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1624237abf0cSDavide Italiano if (tvp != NULL) { 1625237abf0cSDavide Italiano rtv = *tvp; 1626237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1627237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1628237abf0cSDavide Italiano return (EINVAL); 1629237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1630237abf0cSDavide Italiano asbt = 0; 1631237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1632237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1633237abf0cSDavide Italiano precision = rsbt; 1634237abf0cSDavide Italiano precision >>= tc_precexp; 1635237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1636237abf0cSDavide Italiano asbt += tc_tick_sbt; 16374bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1638237abf0cSDavide Italiano asbt += rsbt; 1639237abf0cSDavide Italiano else 1640237abf0cSDavide Italiano asbt = -1; 1641237abf0cSDavide Italiano } else 1642237abf0cSDavide Italiano asbt = -1; 1643237abf0cSDavide Italiano } else 1644237abf0cSDavide Italiano asbt = -1; 1645237abf0cSDavide Italiano seltdinit(td); 1646237abf0cSDavide Italiano /* 1647237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1648237abf0cSDavide Italiano */ 1649237abf0cSDavide Italiano for (;;) { 1650237abf0cSDavide Italiano selfdalloc(td, NULL); 1651237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1652237abf0cSDavide Italiano /* error here is actually the ready events. */ 1653237abf0cSDavide Italiano if (error) 1654237abf0cSDavide Italiano return (0); 1655237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1656237abf0cSDavide Italiano if (error) 1657237abf0cSDavide Italiano break; 1658237abf0cSDavide Italiano } 1659237abf0cSDavide Italiano seltdclear(td); 1660237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1661237abf0cSDavide Italiano if (error == ERESTART) 1662237abf0cSDavide Italiano error = 0; 1663237abf0cSDavide Italiano return (error); 1664237abf0cSDavide Italiano } 1665237abf0cSDavide Italiano 1666237abf0cSDavide Italiano /* 1667ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1668ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1669ace8398dSJeff Roberson */ 1670ace8398dSJeff Roberson static void 1671ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1672ace8398dSJeff Roberson { 1673ace8398dSJeff Roberson struct seltd *stp; 1674ace8398dSJeff Roberson 1675ace8398dSJeff Roberson stp = td->td_sel; 1676ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1677ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1678ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1679ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1680ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1681ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1682ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1683ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1684ace8398dSJeff Roberson } 1685ace8398dSJeff Roberson 1686ace8398dSJeff Roberson static void 1687ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1688ace8398dSJeff Roberson { 1689ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 169073f2e5f7SMateusz Guzik if (sfp->sf_si != NULL) { 1691ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1692fcb5b3a4SKonstantin Belousov if (sfp->sf_si != NULL) { 1693ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1694fcb5b3a4SKonstantin Belousov refcount_release(&sfp->sf_refs); 1695fcb5b3a4SKonstantin Belousov } 1696ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 169773f2e5f7SMateusz Guzik } 1698fcb5b3a4SKonstantin Belousov if (refcount_release(&sfp->sf_refs)) 1699ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 170085f190e4SAlfred Perlstein } 170185f190e4SAlfred Perlstein 17026aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ 17036aba400aSAttilio Rao void 17046aba400aSAttilio Rao seldrain(sip) 17056aba400aSAttilio Rao struct selinfo *sip; 17066aba400aSAttilio Rao { 17076aba400aSAttilio Rao 17086aba400aSAttilio Rao /* 17096aba400aSAttilio Rao * This feature is already provided by doselwakeup(), thus it is 17106aba400aSAttilio Rao * enough to go for it. 17116aba400aSAttilio Rao * Eventually, the context, should take care to avoid races 17126aba400aSAttilio Rao * between thread calling select()/poll() and file descriptor 17136aba400aSAttilio Rao * detaching, but, again, the races are just the same as 17146aba400aSAttilio Rao * selwakeup(). 17156aba400aSAttilio Rao */ 17166aba400aSAttilio Rao doselwakeup(sip, -1); 17176aba400aSAttilio Rao } 17186aba400aSAttilio Rao 1719df8bae1dSRodney W. Grimes /* 1720df8bae1dSRodney W. Grimes * Record a select request. 1721df8bae1dSRodney W. Grimes */ 1722df8bae1dSRodney W. Grimes void 1723df8bae1dSRodney W. Grimes selrecord(selector, sip) 1724b40ce416SJulian Elischer struct thread *selector; 1725df8bae1dSRodney W. Grimes struct selinfo *sip; 1726df8bae1dSRodney W. Grimes { 1727ace8398dSJeff Roberson struct selfd *sfp; 1728ace8398dSJeff Roberson struct seltd *stp; 1729ace8398dSJeff Roberson struct mtx *mtxp; 1730df8bae1dSRodney W. Grimes 1731ace8398dSJeff Roberson stp = selector->td_sel; 173285f190e4SAlfred Perlstein /* 1733ace8398dSJeff Roberson * Don't record when doing a rescan. 173485f190e4SAlfred Perlstein */ 1735ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1736ace8398dSJeff Roberson return; 1737ace8398dSJeff Roberson /* 1738ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1739ace8398dSJeff Roberson */ 1740ace8398dSJeff Roberson sfp = NULL; 1741ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1742ace8398dSJeff Roberson stp->st_free1 = NULL; 1743ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1744ace8398dSJeff Roberson stp->st_free2 = NULL; 1745ace8398dSJeff Roberson else 1746ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 17472141453eSJeff Roberson mtxp = sip->si_mtx; 17482141453eSJeff Roberson if (mtxp == NULL) 17492141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1750ace8398dSJeff Roberson /* 1751ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1752ace8398dSJeff Roberson */ 1753ace8398dSJeff Roberson sfp->sf_si = sip; 1754ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1755fcb5b3a4SKonstantin Belousov refcount_init(&sfp->sf_refs, 2); 1756ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1757ace8398dSJeff Roberson /* 1758ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1759ace8398dSJeff Roberson */ 1760ace8398dSJeff Roberson mtx_lock(mtxp); 1761ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1762ace8398dSJeff Roberson sip->si_mtx = mtxp; 1763ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 176485f190e4SAlfred Perlstein } 1765ace8398dSJeff Roberson /* 1766ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1767ace8398dSJeff Roberson */ 1768ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1769ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1770df8bae1dSRodney W. Grimes } 1771df8bae1dSRodney W. Grimes 1772512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1773df8bae1dSRodney W. Grimes void 1774df8bae1dSRodney W. Grimes selwakeup(sip) 177585f190e4SAlfred Perlstein struct selinfo *sip; 1776df8bae1dSRodney W. Grimes { 1777512824f8SSeigo Tanimura doselwakeup(sip, -1); 1778512824f8SSeigo Tanimura } 1779512824f8SSeigo Tanimura 1780512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1781512824f8SSeigo Tanimura void 1782512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1783512824f8SSeigo Tanimura struct selinfo *sip; 1784512824f8SSeigo Tanimura int pri; 1785512824f8SSeigo Tanimura { 1786512824f8SSeigo Tanimura doselwakeup(sip, pri); 1787512824f8SSeigo Tanimura } 1788512824f8SSeigo Tanimura 1789512824f8SSeigo Tanimura /* 1790512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1791512824f8SSeigo Tanimura */ 1792512824f8SSeigo Tanimura static void 1793512824f8SSeigo Tanimura doselwakeup(sip, pri) 1794512824f8SSeigo Tanimura struct selinfo *sip; 1795512824f8SSeigo Tanimura int pri; 1796512824f8SSeigo Tanimura { 1797ace8398dSJeff Roberson struct selfd *sfp; 1798ace8398dSJeff Roberson struct selfd *sfn; 1799ace8398dSJeff Roberson struct seltd *stp; 1800df8bae1dSRodney W. Grimes 1801ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1802ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1803b40ce416SJulian Elischer return; 1804ace8398dSJeff Roberson /* 1805ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1806ace8398dSJeff Roberson */ 1807ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1808ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1809ace8398dSJeff Roberson /* 1810ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1811ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1812ace8398dSJeff Roberson */ 1813ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1814ace8398dSJeff Roberson sfp->sf_si = NULL; 1815ace8398dSJeff Roberson stp = sfp->sf_td; 1816ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1817ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1818ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1819ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1820fcb5b3a4SKonstantin Belousov if (refcount_release(&sfp->sf_refs)) 1821fcb5b3a4SKonstantin Belousov uma_zfree(selfd_zone, sfp); 1822b40ce416SJulian Elischer } 1823ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1824ace8398dSJeff Roberson } 1825ace8398dSJeff Roberson 1826ace8398dSJeff Roberson static void 1827ace8398dSJeff Roberson seltdinit(struct thread *td) 1828ace8398dSJeff Roberson { 1829ace8398dSJeff Roberson struct seltd *stp; 1830ace8398dSJeff Roberson 1831ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1832ace8398dSJeff Roberson goto out; 1833ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1834ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1835ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1836ace8398dSJeff Roberson out: 1837ace8398dSJeff Roberson stp->st_flags = 0; 1838ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1839ace8398dSJeff Roberson } 1840ace8398dSJeff Roberson 1841ace8398dSJeff Roberson static int 1842cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision) 1843ace8398dSJeff Roberson { 1844ace8398dSJeff Roberson struct seltd *stp; 1845ace8398dSJeff Roberson int error; 1846ace8398dSJeff Roberson 1847ace8398dSJeff Roberson stp = td->td_sel; 1848ace8398dSJeff Roberson /* 1849ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1850ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1851ace8398dSJeff Roberson */ 1852ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1853ace8398dSJeff Roberson /* 1854ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1855ace8398dSJeff Roberson */ 1856ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1857ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1858ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1859ace8398dSJeff Roberson return (0); 1860ace8398dSJeff Roberson } 1861cf5e4fe6SDavide Italiano if (sbt == 0) 1862cf5e4fe6SDavide Italiano error = EWOULDBLOCK; 1863cf5e4fe6SDavide Italiano else if (sbt != -1) 1864cf5e4fe6SDavide Italiano error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx, 1865cf5e4fe6SDavide Italiano sbt, precision, C_ABSOLUTE); 1866ace8398dSJeff Roberson else 1867ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1868ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1869ace8398dSJeff Roberson 1870ace8398dSJeff Roberson return (error); 1871ace8398dSJeff Roberson } 1872ace8398dSJeff Roberson 1873ace8398dSJeff Roberson void 1874ace8398dSJeff Roberson seltdfini(struct thread *td) 1875ace8398dSJeff Roberson { 1876ace8398dSJeff Roberson struct seltd *stp; 1877ace8398dSJeff Roberson 1878ace8398dSJeff Roberson stp = td->td_sel; 1879ace8398dSJeff Roberson if (stp == NULL) 1880ace8398dSJeff Roberson return; 1881ace8398dSJeff Roberson if (stp->st_free1) 1882ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1883ace8398dSJeff Roberson if (stp->st_free2) 1884ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1885ace8398dSJeff Roberson td->td_sel = NULL; 1886ace8398dSJeff Roberson free(stp, M_SELECT); 1887ace8398dSJeff Roberson } 1888ace8398dSJeff Roberson 1889ace8398dSJeff Roberson /* 1890ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1891ace8398dSJeff Roberson * polling. 1892ace8398dSJeff Roberson */ 1893ace8398dSJeff Roberson static void 1894ace8398dSJeff Roberson seltdclear(struct thread *td) 1895ace8398dSJeff Roberson { 1896ace8398dSJeff Roberson struct seltd *stp; 1897ace8398dSJeff Roberson struct selfd *sfp; 1898ace8398dSJeff Roberson struct selfd *sfn; 1899ace8398dSJeff Roberson 1900ace8398dSJeff Roberson stp = td->td_sel; 1901ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1902ace8398dSJeff Roberson selfdfree(stp, sfp); 1903ace8398dSJeff Roberson stp->st_flags = 0; 1904df8bae1dSRodney W. Grimes } 1905265fc98fSSeigo Tanimura 19064d77a549SAlfred Perlstein static void selectinit(void *); 1907ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1908265fc98fSSeigo Tanimura static void 1909ace8398dSJeff Roberson selectinit(void *dummy __unused) 1910265fc98fSSeigo Tanimura { 19112141453eSJeff Roberson 1912ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1913ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 19142141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1915265fc98fSSeigo Tanimura } 19160acf5d0bSMark Johnston 19170acf5d0bSMark Johnston /* 19180acf5d0bSMark Johnston * Set up a syscall return value that follows the convention specified for 19190acf5d0bSMark Johnston * posix_* functions. 19200acf5d0bSMark Johnston */ 19210acf5d0bSMark Johnston int 19220acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error) 19230acf5d0bSMark Johnston { 19240acf5d0bSMark Johnston 19250acf5d0bSMark Johnston if (error <= 0) 19260acf5d0bSMark Johnston return (error); 19270acf5d0bSMark Johnston td->td_errno = error; 19280acf5d0bSMark Johnston td->td_pflags |= TDP_NERRNO; 19290acf5d0bSMark Johnston td->td_retval[0] = error; 19300acf5d0bSMark Johnston return (0); 19310acf5d0bSMark Johnston } 1932