19454b2d8SWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 7df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 8df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 9df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 2069a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 39677b542eSDavid E. O'Brien #include <sys/cdefs.h> 40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 41677b542eSDavid E. O'Brien 42a9d2f8d8SRobert Watson #include "opt_capsicum.h" 432de92a38SPeter Wemm #include "opt_compat.h" 44db6a20e2SGarrett Wollman #include "opt_ktrace.h" 45db6a20e2SGarrett Wollman 46df8bae1dSRodney W. Grimes #include <sys/param.h> 47df8bae1dSRodney W. Grimes #include <sys/systm.h> 48d2d3e875SBruce Evans #include <sys/sysproto.h> 494a144410SRobert Watson #include <sys/capsicum.h> 50df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 5120982410SBruce Evans #include <sys/filio.h> 523ac4d1efSBruce Evans #include <sys/fcntl.h> 53df8bae1dSRodney W. Grimes #include <sys/file.h> 542609222aSPawel Jakub Dawidek #include <sys/lock.h> 55df8bae1dSRodney W. Grimes #include <sys/proc.h> 56797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 57df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 58df8bae1dSRodney W. Grimes #include <sys/uio.h> 59df8bae1dSRodney W. Grimes #include <sys/kernel.h> 60e4650294SJohn Baldwin #include <sys/ktr.h> 61104a9b7eSAlexander Kabaev #include <sys/limits.h> 62df8bae1dSRodney W. Grimes #include <sys/malloc.h> 6342d11757SPeter Wemm #include <sys/poll.h> 6489b71647SPeter Wemm #include <sys/resourcevar.h> 650a2c3d48SGarrett Wollman #include <sys/selinfo.h> 6644f3b092SJohn Baldwin #include <sys/sleepqueue.h> 678f19eb88SIan Dowse #include <sys/syscallsubr.h> 688cb96f20SPeter Wemm #include <sys/sysctl.h> 6942d11757SPeter Wemm #include <sys/sysent.h> 709bbee259SAndrey A. Chernov #include <sys/vnode.h> 71279d7226SMatthew Dillon #include <sys/bio.h> 72279d7226SMatthew Dillon #include <sys/buf.h> 73265fc98fSSeigo Tanimura #include <sys/condvar.h> 74df8bae1dSRodney W. Grimes #ifdef KTRACE 75df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 76df8bae1dSRodney W. Grimes #endif 77df8bae1dSRodney W. Grimes 78e4650294SJohn Baldwin #include <security/audit/audit.h> 79ace8398dSJeff Roberson 8050ae6690SHans Petter Selasky /* 8150ae6690SHans Petter Selasky * The following macro defines how many bytes will be allocated from 8250ae6690SHans Petter Selasky * the stack instead of memory allocated when passing the IOCTL data 8350ae6690SHans Petter Selasky * structures from userspace and to the kernel. Some IOCTLs having 8450ae6690SHans Petter Selasky * small data structures are used very frequently and this small 8550ae6690SHans Petter Selasky * buffer on the stack gives a significant speedup improvement for 8650ae6690SHans Petter Selasky * those requests. The value of this define should be greater or equal 8750ae6690SHans Petter Selasky * to 64 bytes and should also be power of two. The data structure is 8850ae6690SHans Petter Selasky * currently hard-aligned to a 8-byte boundary on the stack. This 8950ae6690SHans Petter Selasky * should currently be sufficient for all supported platforms. 9050ae6690SHans Petter Selasky */ 9150ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_SIZE 128 /* bytes */ 9250ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_ALIGN 8 /* bytes */ 9350ae6690SHans Petter Selasky 94611fcff9SJohn Baldwin #ifdef __LP64__ 95611fcff9SJohn Baldwin static int iosize_max_clamp = 0; 968bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW, 978bb9a904SKonstantin Belousov &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX"); 98611fcff9SJohn Baldwin static int devfs_iosize_max_clamp = 1; 99bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW, 100bf3e483bSKonstantin Belousov &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices"); 101611fcff9SJohn Baldwin #endif 102bf3e483bSKonstantin Belousov 1038bb9a904SKonstantin Belousov /* 1048bb9a904SKonstantin Belousov * Assert that the return value of read(2) and write(2) syscalls fits 1058bb9a904SKonstantin Belousov * into a register. If not, an architecture will need to provide the 1068bb9a904SKonstantin Belousov * usermode wrappers to reconstruct the result. 1078bb9a904SKonstantin Belousov */ 1088bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t)); 109526d0bd5SKonstantin Belousov 110a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 111a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 112a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 11355166637SPoul-Henning Kamp 1146d8feddaSKonstantin Belousov static int pollout(struct thread *, struct pollfd *, struct pollfd *, 1156d8feddaSKonstantin Belousov u_int); 116bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 117ace8398dSJeff Roberson static int pollrescan(struct thread *); 118bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 119ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 120ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 121ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 122bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 123bcd9e0ddSJohn Baldwin off_t, int); 124bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 125bcd9e0ddSJohn Baldwin off_t, int); 126512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 127ace8398dSJeff Roberson static void seltdinit(struct thread *); 128cf5e4fe6SDavide Italiano static int seltdwait(struct thread *, sbintime_t, sbintime_t); 129ace8398dSJeff Roberson static void seltdclear(struct thread *); 130ace8398dSJeff Roberson 131ace8398dSJeff Roberson /* 132ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 133ace8398dSJeff Roberson * 134ace8398dSJeff Roberson * t - protected by st_mtx 135ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 136ace8398dSJeff Roberson */ 137ace8398dSJeff Roberson struct seltd { 138ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 139ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 140ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 141ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 142ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 143ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 144ace8398dSJeff Roberson }; 145ace8398dSJeff Roberson 146ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 147ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 148ace8398dSJeff Roberson 149ace8398dSJeff Roberson /* 150ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 151ace8398dSJeff Roberson * f - protected by sf_mtx 152ace8398dSJeff Roberson */ 153ace8398dSJeff Roberson struct selfd { 154ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 155ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 156ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 157ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 158ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 159ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 160fcb5b3a4SKonstantin Belousov u_int sf_refs; 161ace8398dSJeff Roberson }; 162ace8398dSJeff Roberson 163ace8398dSJeff Roberson static uma_zone_t selfd_zone; 1642141453eSJeff Roberson static struct mtx_pool *mtxpool_select; 1658fe387abSDmitrij Tejblum 166611fcff9SJohn Baldwin #ifdef __LP64__ 167611fcff9SJohn Baldwin size_t 168611fcff9SJohn Baldwin devfs_iosize_max(void) 169611fcff9SJohn Baldwin { 170611fcff9SJohn Baldwin 171611fcff9SJohn Baldwin return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 172611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 173611fcff9SJohn Baldwin } 174611fcff9SJohn Baldwin 175611fcff9SJohn Baldwin size_t 176611fcff9SJohn Baldwin iosize_max(void) 177611fcff9SJohn Baldwin { 178611fcff9SJohn Baldwin 179611fcff9SJohn Baldwin return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 180611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 181611fcff9SJohn Baldwin } 182611fcff9SJohn Baldwin #endif 183611fcff9SJohn Baldwin 184d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 185df8bae1dSRodney W. Grimes struct read_args { 186df8bae1dSRodney W. Grimes int fd; 187134e06feSBruce Evans void *buf; 188134e06feSBruce Evans size_t nbyte; 189df8bae1dSRodney W. Grimes }; 190d2d3e875SBruce Evans #endif 19126f9a767SRodney W. Grimes int 1928451d0ddSKip Macy sys_read(td, uap) 193b40ce416SJulian Elischer struct thread *td; 194b064d43dSMatthew Dillon struct read_args *uap; 195df8bae1dSRodney W. Grimes { 196bcd9e0ddSJohn Baldwin struct uio auio; 197bcd9e0ddSJohn Baldwin struct iovec aiov; 198279d7226SMatthew Dillon int error; 199df8bae1dSRodney W. Grimes 200526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 201bcd9e0ddSJohn Baldwin return (EINVAL); 202bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 203bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 204bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 205bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 206bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 207bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 208bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 209279d7226SMatthew Dillon return(error); 210df8bae1dSRodney W. Grimes } 211df8bae1dSRodney W. Grimes 212df8bae1dSRodney W. Grimes /* 213bcd9e0ddSJohn Baldwin * Positioned read system call 2144160ccd9SAlan Cox */ 2154160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 2164160ccd9SAlan Cox struct pread_args { 2174160ccd9SAlan Cox int fd; 2184160ccd9SAlan Cox void *buf; 2194160ccd9SAlan Cox size_t nbyte; 2208fe387abSDmitrij Tejblum int pad; 2214160ccd9SAlan Cox off_t offset; 2224160ccd9SAlan Cox }; 2234160ccd9SAlan Cox #endif 2244160ccd9SAlan Cox int 225b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap) 226b38b22b0SEdward Tomasz Napierala { 227b38b22b0SEdward Tomasz Napierala 228b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 229b38b22b0SEdward Tomasz Napierala } 230b38b22b0SEdward Tomasz Napierala 231b38b22b0SEdward Tomasz Napierala int 232b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset) 2334160ccd9SAlan Cox { 2344160ccd9SAlan Cox struct uio auio; 2354160ccd9SAlan Cox struct iovec aiov; 236bcd9e0ddSJohn Baldwin int error; 2374160ccd9SAlan Cox 238b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 239bcd9e0ddSJohn Baldwin return (EINVAL); 240b38b22b0SEdward Tomasz Napierala aiov.iov_base = buf; 241b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 2424160ccd9SAlan Cox auio.uio_iov = &aiov; 2434160ccd9SAlan Cox auio.uio_iovcnt = 1; 244b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 2454160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 246b38b22b0SEdward Tomasz Napierala error = kern_preadv(td, fd, &auio, offset); 2474160ccd9SAlan Cox return (error); 2484160ccd9SAlan Cox } 2494160ccd9SAlan Cox 2500538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 251c2815ad5SPeter Wemm int 252b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap) 253c2815ad5SPeter Wemm { 254c2815ad5SPeter Wemm 255b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 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; 33932a1fb0dSMahdi Mokhtari else if (offset < 0 && 34032a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 341bcd9e0ddSJohn Baldwin error = EINVAL; 342bcd9e0ddSJohn Baldwin else 343bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 344bcd9e0ddSJohn Baldwin fdrop(fp, td); 345bcd9e0ddSJohn Baldwin return (error); 346bcd9e0ddSJohn Baldwin } 347bcd9e0ddSJohn Baldwin 348bcd9e0ddSJohn Baldwin /* 349bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 350bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 351bcd9e0ddSJohn Baldwin */ 352bcd9e0ddSJohn Baldwin static int 353bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 354bcd9e0ddSJohn Baldwin struct thread *td; 355bcd9e0ddSJohn Baldwin int fd; 356bcd9e0ddSJohn Baldwin struct file *fp; 357bcd9e0ddSJohn Baldwin struct uio *auio; 358bcd9e0ddSJohn Baldwin off_t offset; 359bcd9e0ddSJohn Baldwin int flags; 360bcd9e0ddSJohn Baldwin { 361bcd9e0ddSJohn Baldwin ssize_t cnt; 36282641acdSAlan Cox int error; 363df8bae1dSRodney W. Grimes #ifdef KTRACE 364552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 365df8bae1dSRodney W. Grimes #endif 366df8bae1dSRodney W. Grimes 36751d1f690SRobert Watson AUDIT_ARG_FD(fd); 36851d1f690SRobert Watson 3694f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3704f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3714f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3724f8d23d6SPoul-Henning Kamp return(0); 3734f8d23d6SPoul-Henning Kamp } 374552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 375bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 376552afd9cSPoul-Henning Kamp auio->uio_td = td; 377df8bae1dSRodney W. Grimes #ifdef KTRACE 378552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 379552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 380df8bae1dSRodney W. Grimes #endif 381552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 382bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 383552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 384df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 385df8bae1dSRodney W. Grimes error = 0; 386279d7226SMatthew Dillon } 387552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 388df8bae1dSRodney W. Grimes #ifdef KTRACE 389552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 390552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 391b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 392df8bae1dSRodney W. Grimes } 393df8bae1dSRodney W. Grimes #endif 394b40ce416SJulian Elischer td->td_retval[0] = cnt; 395df8bae1dSRodney W. Grimes return (error); 396df8bae1dSRodney W. Grimes } 397df8bae1dSRodney W. Grimes 398d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 399df8bae1dSRodney W. Grimes struct write_args { 400df8bae1dSRodney W. Grimes int fd; 401134e06feSBruce Evans const void *buf; 402134e06feSBruce Evans size_t nbyte; 403df8bae1dSRodney W. Grimes }; 404d2d3e875SBruce Evans #endif 40526f9a767SRodney W. Grimes int 4068451d0ddSKip Macy sys_write(td, uap) 407b40ce416SJulian Elischer struct thread *td; 408b064d43dSMatthew Dillon struct write_args *uap; 409df8bae1dSRodney W. Grimes { 410bcd9e0ddSJohn Baldwin struct uio auio; 411bcd9e0ddSJohn Baldwin struct iovec aiov; 412279d7226SMatthew Dillon int error; 413df8bae1dSRodney W. Grimes 414526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 415bcd9e0ddSJohn Baldwin return (EINVAL); 416bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 417bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 418bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 419bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 420bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 421bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 422bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 423279d7226SMatthew Dillon return(error); 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes /* 4270c14ff0eSRobert Watson * Positioned write system call. 4284160ccd9SAlan Cox */ 4294160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 4304160ccd9SAlan Cox struct pwrite_args { 4314160ccd9SAlan Cox int fd; 4324160ccd9SAlan Cox const void *buf; 4334160ccd9SAlan Cox size_t nbyte; 4348fe387abSDmitrij Tejblum int pad; 4354160ccd9SAlan Cox off_t offset; 4364160ccd9SAlan Cox }; 4374160ccd9SAlan Cox #endif 4384160ccd9SAlan Cox int 439b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap) 440b38b22b0SEdward Tomasz Napierala { 441b38b22b0SEdward Tomasz Napierala 442b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 443b38b22b0SEdward Tomasz Napierala } 444b38b22b0SEdward Tomasz Napierala 445b38b22b0SEdward Tomasz Napierala int 446b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte, 447b38b22b0SEdward Tomasz Napierala off_t offset) 4484160ccd9SAlan Cox { 4494160ccd9SAlan Cox struct uio auio; 4504160ccd9SAlan Cox struct iovec aiov; 451bcd9e0ddSJohn Baldwin int error; 4524160ccd9SAlan Cox 453b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 454bcd9e0ddSJohn Baldwin return (EINVAL); 455b38b22b0SEdward Tomasz Napierala aiov.iov_base = (void *)(uintptr_t)buf; 456b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 4574160ccd9SAlan Cox auio.uio_iov = &aiov; 4584160ccd9SAlan Cox auio.uio_iovcnt = 1; 459b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 4604160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 461b38b22b0SEdward Tomasz Napierala error = kern_pwritev(td, fd, &auio, offset); 4624160ccd9SAlan Cox return(error); 4634160ccd9SAlan Cox } 4644160ccd9SAlan Cox 4650538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 466c2815ad5SPeter Wemm int 467b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap) 468c2815ad5SPeter Wemm { 469c2815ad5SPeter Wemm 470b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 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; 55432a1fb0dSMahdi Mokhtari else if (offset < 0 && 55532a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 556bcd9e0ddSJohn Baldwin error = EINVAL; 557bcd9e0ddSJohn Baldwin else 558bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 559bcd9e0ddSJohn Baldwin fdrop(fp, td); 560bcd9e0ddSJohn Baldwin return (error); 561bcd9e0ddSJohn Baldwin } 562bcd9e0ddSJohn Baldwin 563bcd9e0ddSJohn Baldwin /* 564bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 565bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 566bcd9e0ddSJohn Baldwin */ 567bcd9e0ddSJohn Baldwin static int 568bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 569bcd9e0ddSJohn Baldwin struct thread *td; 570bcd9e0ddSJohn Baldwin int fd; 571bcd9e0ddSJohn Baldwin struct file *fp; 572bcd9e0ddSJohn Baldwin struct uio *auio; 573bcd9e0ddSJohn Baldwin off_t offset; 574bcd9e0ddSJohn Baldwin int flags; 575bcd9e0ddSJohn Baldwin { 576bcd9e0ddSJohn Baldwin ssize_t cnt; 577552afd9cSPoul-Henning Kamp int error; 578df8bae1dSRodney W. Grimes #ifdef KTRACE 579552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 580df8bae1dSRodney W. Grimes #endif 581df8bae1dSRodney W. Grimes 58251d1f690SRobert Watson AUDIT_ARG_FD(fd); 583552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 584552afd9cSPoul-Henning Kamp auio->uio_td = td; 585bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 586df8bae1dSRodney W. Grimes #ifdef KTRACE 587552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 588552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 589df8bae1dSRodney W. Grimes #endif 590552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 591ad9789f6SKonstantin Belousov if (fp->f_type == DTYPE_VNODE && 592ad9789f6SKonstantin Belousov (fp->f_vnread_flags & FDEVFS_VNODE) == 0) 5939440653dSMatthew Dillon bwillwrite(); 594bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 595552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 596df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 597df8bae1dSRodney W. Grimes error = 0; 598bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5996f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 600b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 6017a6f3d78SJohn Baldwin tdsignal(td, SIGPIPE); 602b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 60319eb87d2SJohn Baldwin } 604df8bae1dSRodney W. Grimes } 605552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 606df8bae1dSRodney W. Grimes #ifdef KTRACE 607552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 608552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 609b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes #endif 612b40ce416SJulian Elischer td->td_retval[0] = cnt; 613df8bae1dSRodney W. Grimes return (error); 614df8bae1dSRodney W. Grimes } 615df8bae1dSRodney W. Grimes 616e4650294SJohn Baldwin /* 617e4650294SJohn Baldwin * Truncate a file given a file descriptor. 618e4650294SJohn Baldwin * 619e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 620e4650294SJohn Baldwin * descriptor isn't writable. 621e4650294SJohn Baldwin */ 622e4650294SJohn Baldwin int 623e4650294SJohn Baldwin kern_ftruncate(td, fd, length) 624e4650294SJohn Baldwin struct thread *td; 625e4650294SJohn Baldwin int fd; 626e4650294SJohn Baldwin off_t length; 627e4650294SJohn Baldwin { 628e4650294SJohn Baldwin struct file *fp; 6297008be5bSPawel Jakub Dawidek cap_rights_t rights; 630e4650294SJohn Baldwin int error; 631e4650294SJohn Baldwin 63214961ba7SRobert Watson AUDIT_ARG_FD(fd); 633e4650294SJohn Baldwin if (length < 0) 634e4650294SJohn Baldwin return (EINVAL); 6357008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp); 636e4650294SJohn Baldwin if (error) 637e4650294SJohn Baldwin return (error); 63814961ba7SRobert Watson AUDIT_ARG_FILE(td->td_proc, fp); 639e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 640e4650294SJohn Baldwin fdrop(fp, td); 641e4650294SJohn Baldwin return (EINVAL); 642e4650294SJohn Baldwin } 643e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 644e4650294SJohn Baldwin fdrop(fp, td); 645e4650294SJohn Baldwin return (error); 646e4650294SJohn Baldwin } 647e4650294SJohn Baldwin 648e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 649e4650294SJohn Baldwin struct ftruncate_args { 650e4650294SJohn Baldwin int fd; 651e4650294SJohn Baldwin int pad; 652e4650294SJohn Baldwin off_t length; 653e4650294SJohn Baldwin }; 654e4650294SJohn Baldwin #endif 655e4650294SJohn Baldwin int 6568451d0ddSKip Macy sys_ftruncate(td, uap) 657e4650294SJohn Baldwin struct thread *td; 658e4650294SJohn Baldwin struct ftruncate_args *uap; 659e4650294SJohn Baldwin { 660e4650294SJohn Baldwin 661e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 662e4650294SJohn Baldwin } 663e4650294SJohn Baldwin 664e4650294SJohn Baldwin #if defined(COMPAT_43) 665e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 666e4650294SJohn Baldwin struct oftruncate_args { 667e4650294SJohn Baldwin int fd; 668e4650294SJohn Baldwin long length; 669e4650294SJohn Baldwin }; 670e4650294SJohn Baldwin #endif 671e4650294SJohn Baldwin int 672e4650294SJohn Baldwin oftruncate(td, uap) 673e4650294SJohn Baldwin struct thread *td; 674e4650294SJohn Baldwin struct oftruncate_args *uap; 675e4650294SJohn Baldwin { 676e4650294SJohn Baldwin 677e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 678e4650294SJohn Baldwin } 679e4650294SJohn Baldwin #endif /* COMPAT_43 */ 680e4650294SJohn Baldwin 681d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 682df8bae1dSRodney W. Grimes struct ioctl_args { 683df8bae1dSRodney W. Grimes int fd; 684069e9bc1SDoug Rabson u_long com; 685df8bae1dSRodney W. Grimes caddr_t data; 686df8bae1dSRodney W. Grimes }; 687d2d3e875SBruce Evans #endif 688df8bae1dSRodney W. Grimes /* ARGSUSED */ 68926f9a767SRodney W. Grimes int 6908451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap) 691df8bae1dSRodney W. Grimes { 69250ae6690SHans Petter Selasky u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN); 6933e15c66fSPoul-Henning Kamp u_long com; 6949fddcc66SRuslan Ermilov int arg, error; 6953e15c66fSPoul-Henning Kamp u_int size; 6969fddcc66SRuslan Ermilov caddr_t data; 697df8bae1dSRodney W. Grimes 6989fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6999fc6aa06SPoul-Henning Kamp printf( 7009fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 701431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 7029fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 7039fc6aa06SPoul-Henning Kamp } 704d9f46233SJohn Baldwin com = uap->com; 705df8bae1dSRodney W. Grimes 706df8bae1dSRodney W. Grimes /* 707df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 708df8bae1dSRodney W. Grimes * copied to/from the user's address space. 709df8bae1dSRodney W. Grimes */ 710df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 711ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 712ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 7132de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 7142de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 7152de92a38SPeter Wemm #else 7162de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 7172de92a38SPeter Wemm #endif 7189fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 719426da3bcSAlfred Perlstein return (ENOTTY); 720279d7226SMatthew Dillon 721ca51b19bSPoul-Henning Kamp if (size > 0) { 722715457f6SDavid E. O'Brien if (com & IOC_VOID) { 7239fddcc66SRuslan Ermilov /* Integer argument. */ 7249fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 7259fddcc66SRuslan Ermilov data = (void *)&arg; 7269fddcc66SRuslan Ermilov size = 0; 7275cbf44bfSMateusz Guzik } else { 7280ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 729715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 7300ecd606bSHans Petter Selasky else 7310ecd606bSHans Petter Selasky data = smalldata; 7325cbf44bfSMateusz Guzik } 7339fddcc66SRuslan Ermilov } else 7349fddcc66SRuslan Ermilov data = (void *)&uap->data; 735df8bae1dSRodney W. Grimes if (com & IOC_IN) { 736df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 7375cbf44bfSMateusz Guzik if (error != 0) 7385cbf44bfSMateusz Guzik goto out; 739ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 740df8bae1dSRodney W. Grimes /* 741df8bae1dSRodney W. Grimes * Zero the buffer so the user always 742df8bae1dSRodney W. Grimes * gets back something deterministic. 743df8bae1dSRodney W. Grimes */ 744df8bae1dSRodney W. Grimes bzero(data, size); 745279d7226SMatthew Dillon } 746df8bae1dSRodney W. Grimes 747d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 748d9f46233SJohn Baldwin 749d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 750d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 751d9f46233SJohn Baldwin 7525cbf44bfSMateusz Guzik out: 7530ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 7549fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 755d9f46233SJohn Baldwin return (error); 756d9f46233SJohn Baldwin } 757d9f46233SJohn Baldwin 758d9f46233SJohn Baldwin int 759d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 760d9f46233SJohn Baldwin { 761d9f46233SJohn Baldwin struct file *fp; 762d9f46233SJohn Baldwin struct filedesc *fdp; 7637008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES 7647008be5bSPawel Jakub Dawidek cap_rights_t rights; 7657008be5bSPawel Jakub Dawidek #endif 7662609222aSPawel Jakub Dawidek int error, tmp, locked; 767d9f46233SJohn Baldwin 76864c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 76964c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7702609222aSPawel Jakub Dawidek 771d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7722609222aSPawel Jakub Dawidek 773d9f46233SJohn Baldwin switch (com) { 774d9f46233SJohn Baldwin case FIONCLEX: 775d9f46233SJohn Baldwin case FIOCLEX: 7765e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7772609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7782609222aSPawel Jakub Dawidek break; 7792609222aSPawel Jakub Dawidek default: 7802609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7812609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7822609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7832609222aSPawel Jakub Dawidek #else 7842609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7852609222aSPawel Jakub Dawidek #endif 7862609222aSPawel Jakub Dawidek break; 7872609222aSPawel Jakub Dawidek } 7882609222aSPawel Jakub Dawidek 7892609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7902609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7912609222aSPawel Jakub Dawidek error = EBADF; 7922609222aSPawel Jakub Dawidek goto out; 7932609222aSPawel Jakub Dawidek } 7942609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7952609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7962609222aSPawel Jakub Dawidek goto out; 7972609222aSPawel Jakub Dawidek } 7982609222aSPawel Jakub Dawidek fhold(fp); 7992609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 8002609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8012609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 8022609222aSPawel Jakub Dawidek } 8032609222aSPawel Jakub Dawidek #else 8047008be5bSPawel Jakub Dawidek error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp); 8057008be5bSPawel Jakub Dawidek if (error != 0) { 8062609222aSPawel Jakub Dawidek fp = NULL; 8072609222aSPawel Jakub Dawidek goto out; 8082609222aSPawel Jakub Dawidek } 8092609222aSPawel Jakub Dawidek #endif 8102609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 8112609222aSPawel Jakub Dawidek error = EBADF; 8122609222aSPawel Jakub Dawidek goto out; 8132609222aSPawel Jakub Dawidek } 8142609222aSPawel Jakub Dawidek 8152609222aSPawel Jakub Dawidek switch (com) { 8162609222aSPawel Jakub Dawidek case FIONCLEX: 8172609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 8182609222aSPawel Jakub Dawidek goto out; 8192609222aSPawel Jakub Dawidek case FIOCLEX: 8202609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 821d9f46233SJohn Baldwin goto out; 822d9f46233SJohn Baldwin case FIONBIO: 823bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 824397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 825df8bae1dSRodney W. Grimes else 826397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 8278ccf264fSPoul-Henning Kamp data = (void *)&tmp; 828d9f46233SJohn Baldwin break; 829d9f46233SJohn Baldwin case FIOASYNC: 830bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 831397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 832df8bae1dSRodney W. Grimes else 833397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 8348ccf264fSPoul-Henning Kamp data = (void *)&tmp; 835d9f46233SJohn Baldwin break; 836df8bae1dSRodney W. Grimes } 8378ccf264fSPoul-Henning Kamp 8388ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 839d9f46233SJohn Baldwin out: 8402609222aSPawel Jakub Dawidek switch (locked) { 8412609222aSPawel Jakub Dawidek case LA_XLOCKED: 8422609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 8432609222aSPawel Jakub Dawidek break; 8442609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 8452609222aSPawel Jakub Dawidek case LA_SLOCKED: 8462609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8472609222aSPawel Jakub Dawidek break; 8482609222aSPawel Jakub Dawidek #endif 8492609222aSPawel Jakub Dawidek default: 8502609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8512609222aSPawel Jakub Dawidek break; 8522609222aSPawel Jakub Dawidek } 8532609222aSPawel Jakub Dawidek if (fp != NULL) 854b40ce416SJulian Elischer fdrop(fp, td); 855df8bae1dSRodney W. Grimes return (error); 856df8bae1dSRodney W. Grimes } 857df8bae1dSRodney W. Grimes 858125dcf8cSKonstantin Belousov int 859125dcf8cSKonstantin Belousov poll_no_poll(int events) 860125dcf8cSKonstantin Belousov { 861125dcf8cSKonstantin Belousov /* 862125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 863125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 864125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 865125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 866125dcf8cSKonstantin Belousov * of specific filesystem implementations. 867125dcf8cSKonstantin Belousov */ 868125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 869125dcf8cSKonstantin Belousov return (POLLNVAL); 870125dcf8cSKonstantin Belousov 871125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 872125dcf8cSKonstantin Belousov } 873125dcf8cSKonstantin Belousov 874066d836bSKonstantin Belousov int 8758451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 876066d836bSKonstantin Belousov { 877066d836bSKonstantin Belousov struct timespec ts; 878066d836bSKonstantin Belousov struct timeval tv, *tvp; 879066d836bSKonstantin Belousov sigset_t set, *uset; 880066d836bSKonstantin Belousov int error; 881066d836bSKonstantin Belousov 882066d836bSKonstantin Belousov if (uap->ts != NULL) { 883066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 884066d836bSKonstantin Belousov if (error != 0) 885066d836bSKonstantin Belousov return (error); 886066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 887066d836bSKonstantin Belousov tvp = &tv; 888066d836bSKonstantin Belousov } else 889066d836bSKonstantin Belousov tvp = NULL; 890066d836bSKonstantin Belousov if (uap->sm != NULL) { 891066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 892066d836bSKonstantin Belousov if (error != 0) 893066d836bSKonstantin Belousov return (error); 894066d836bSKonstantin Belousov uset = &set; 895066d836bSKonstantin Belousov } else 896066d836bSKonstantin Belousov uset = NULL; 897066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 898066d836bSKonstantin Belousov uset, NFDBITS)); 899066d836bSKonstantin Belousov } 900066d836bSKonstantin Belousov 901066d836bSKonstantin Belousov int 902066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 903066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 904066d836bSKonstantin Belousov { 905066d836bSKonstantin Belousov int error; 906066d836bSKonstantin Belousov 907066d836bSKonstantin Belousov if (uset != NULL) { 908066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 909066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 910066d836bSKonstantin Belousov if (error != 0) 911066d836bSKonstantin Belousov return (error); 912066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 913066d836bSKonstantin Belousov /* 914066d836bSKonstantin Belousov * Make sure that ast() is called on return to 915066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 916066d836bSKonstantin Belousov * sigmask. 917066d836bSKonstantin Belousov */ 918066d836bSKonstantin Belousov thread_lock(td); 919066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 920066d836bSKonstantin Belousov thread_unlock(td); 921066d836bSKonstantin Belousov } 922066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 923066d836bSKonstantin Belousov return (error); 924066d836bSKonstantin Belousov } 925066d836bSKonstantin Belousov 926d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 927df8bae1dSRodney W. Grimes struct select_args { 928b08f7993SSujal Patel int nd; 929df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 930df8bae1dSRodney W. Grimes struct timeval *tv; 931df8bae1dSRodney W. Grimes }; 932d2d3e875SBruce Evans #endif 93326f9a767SRodney W. Grimes int 9348451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 935df8bae1dSRodney W. Grimes { 9368f19eb88SIan Dowse struct timeval tv, *tvp; 9378f19eb88SIan Dowse int error; 9388f19eb88SIan Dowse 9398f19eb88SIan Dowse if (uap->tv != NULL) { 9408f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 9418f19eb88SIan Dowse if (error) 9428f19eb88SIan Dowse return (error); 9438f19eb88SIan Dowse tvp = &tv; 9448f19eb88SIan Dowse } else 9458f19eb88SIan Dowse tvp = NULL; 9468f19eb88SIan Dowse 947b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 948b55ef216SKonstantin Belousov NFDBITS)); 9498f19eb88SIan Dowse } 9508f19eb88SIan Dowse 95156be1b9aSKonstantin Belousov /* 95256be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 95356be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 95456be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 95556be1b9aSKonstantin Belousov * 95656be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 95756be1b9aSKonstantin Belousov * 95856be1b9aSKonstantin Belousov * nd is fd_lastfile + 1. 95956be1b9aSKonstantin Belousov */ 96056be1b9aSKonstantin Belousov static int 96156be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 96256be1b9aSKonstantin Belousov { 96356be1b9aSKonstantin Belousov char *addr, *oaddr; 96456be1b9aSKonstantin Belousov int b, i, res; 96556be1b9aSKonstantin Belousov uint8_t bits; 96656be1b9aSKonstantin Belousov 96756be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 96856be1b9aSKonstantin Belousov return (0); 96956be1b9aSKonstantin Belousov 97056be1b9aSKonstantin Belousov oaddr = NULL; 97156be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 97256be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 97356be1b9aSKonstantin Belousov b = i / NBBY; 97456be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 97556be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 97656be1b9aSKonstantin Belousov #else 97756be1b9aSKonstantin Belousov addr = (char *)fd_in; 97856be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 97956be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 98056be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 98156be1b9aSKonstantin Belousov } else { 98256be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 98356be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 98456be1b9aSKonstantin Belousov } 98556be1b9aSKonstantin Belousov #endif 98656be1b9aSKonstantin Belousov if (addr != oaddr) { 98756be1b9aSKonstantin Belousov res = fubyte(addr); 98856be1b9aSKonstantin Belousov if (res == -1) 98956be1b9aSKonstantin Belousov return (EFAULT); 99056be1b9aSKonstantin Belousov oaddr = addr; 99156be1b9aSKonstantin Belousov bits = res; 99256be1b9aSKonstantin Belousov } 99356be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 99456be1b9aSKonstantin Belousov return (EBADF); 99556be1b9aSKonstantin Belousov } 99656be1b9aSKonstantin Belousov return (0); 99756be1b9aSKonstantin Belousov } 99856be1b9aSKonstantin Belousov 9998f19eb88SIan Dowse int 10008f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 1001b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 10028f19eb88SIan Dowse { 1003426da3bcSAlfred Perlstein struct filedesc *fdp; 1004d5e4d7e1SBruce Evans /* 1005d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 1006d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 1007d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 1008d5e4d7e1SBruce Evans * of 256. 1009d5e4d7e1SBruce Evans */ 1010d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 1011eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 1012cf5e4fe6SDavide Italiano struct timeval rtv; 1013cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 1014b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 1015cf5e4fe6SDavide Italiano int error, lf, ndu; 1016df8bae1dSRodney W. Grimes 10178f19eb88SIan Dowse if (nd < 0) 1018acbfbfeaSSujal Patel return (EINVAL); 1019426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 102056be1b9aSKonstantin Belousov ndu = nd; 102156be1b9aSKonstantin Belousov lf = fdp->fd_lastfile; 102256be1b9aSKonstantin Belousov if (nd > lf + 1) 102356be1b9aSKonstantin Belousov nd = lf + 1; 102456be1b9aSKonstantin Belousov 102556be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 102656be1b9aSKonstantin Belousov if (error != 0) 102756be1b9aSKonstantin Belousov return (error); 102856be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 102956be1b9aSKonstantin Belousov if (error != 0) 103056be1b9aSKonstantin Belousov return (error); 103156be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 103256be1b9aSKonstantin Belousov if (error != 0) 103356be1b9aSKonstantin Belousov return (error); 1034b08f7993SSujal Patel 1035d5e4d7e1SBruce Evans /* 1036d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 1037d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 1038d5e4d7e1SBruce Evans */ 10398f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 1040d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 1041b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 1042d5e4d7e1SBruce Evans nbufbytes = 0; 10438f19eb88SIan Dowse if (fd_in != NULL) 1044d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10458f19eb88SIan Dowse if (fd_ou != NULL) 1046d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10478f19eb88SIan Dowse if (fd_ex != NULL) 1048d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1049d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1050d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1051d5e4d7e1SBruce Evans else 1052a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1053b08f7993SSujal Patel 1054b08f7993SSujal Patel /* 1055d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1056d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1057d5e4d7e1SBruce Evans * together. 1058b08f7993SSujal Patel */ 1059d5e4d7e1SBruce Evans sbp = selbits; 1060df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1061d5e4d7e1SBruce Evans do { \ 1062841c0c7eSNathan Whitehorn if (name == NULL) { \ 1063d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1064841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1065841c0c7eSNathan Whitehorn } else { \ 1066d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1067d5e4d7e1SBruce Evans obits[x] = sbp; \ 1068d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1069b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1070265fc98fSSeigo Tanimura if (error != 0) \ 1071ace8398dSJeff Roberson goto done; \ 1072b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1073b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1074e04ac2feSJohn Baldwin } \ 1075d5e4d7e1SBruce Evans } while (0) 10768f19eb88SIan Dowse getbits(fd_in, 0); 10778f19eb88SIan Dowse getbits(fd_ou, 1); 10788f19eb88SIan Dowse getbits(fd_ex, 2); 1079df8bae1dSRodney W. Grimes #undef getbits 1080841c0c7eSNathan Whitehorn 1081841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1082841c0c7eSNathan Whitehorn /* 1083841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1084841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1085841c0c7eSNathan Whitehorn * generic. 1086841c0c7eSNathan Whitehorn */ 1087841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1088841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1089841c0c7eSNathan Whitehorn int i; \ 1090841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1091841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1092841c0c7eSNathan Whitehorn } 1093841c0c7eSNathan Whitehorn #else 1094841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1095841c0c7eSNathan Whitehorn #endif 1096841c0c7eSNathan Whitehorn 1097841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1098841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1099841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1100841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1101841c0c7eSNathan Whitehorn 1102d5e4d7e1SBruce Evans if (nbufbytes != 0) 1103d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1104df8bae1dSRodney W. Grimes 1105cf5e4fe6SDavide Italiano precision = 0; 11068f19eb88SIan Dowse if (tvp != NULL) { 1107cf5e4fe6SDavide Italiano rtv = *tvp; 1108cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1109cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1110df8bae1dSRodney W. Grimes error = EINVAL; 1111ace8398dSJeff Roberson goto done; 1112df8bae1dSRodney W. Grimes } 111321a37a71SAlexander Motin if (!timevalisset(&rtv)) 1114980c545dSAlexander Motin asbt = 0; 111521a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1116cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1117cf5e4fe6SDavide Italiano precision = rsbt; 1118cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1119cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1120cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 11214bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1122cf5e4fe6SDavide Italiano asbt += rsbt; 112321a37a71SAlexander Motin else 1124980c545dSAlexander Motin asbt = -1; 1125980c545dSAlexander Motin } else 1126980c545dSAlexander Motin asbt = -1; 1127cf5e4fe6SDavide Italiano } else 1128cf5e4fe6SDavide Italiano asbt = -1; 1129ace8398dSJeff Roberson seltdinit(td); 1130ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1131ace8398dSJeff Roberson for (;;) { 11328f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1133ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1134ace8398dSJeff Roberson break; 1135cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1136ace8398dSJeff Roberson if (error) 1137ace8398dSJeff Roberson break; 1138ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1139ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1140ace8398dSJeff Roberson break; 114185f190e4SAlfred Perlstein } 1142ace8398dSJeff Roberson seltdclear(td); 1143265fc98fSSeigo Tanimura 1144df8bae1dSRodney W. Grimes done: 1145df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1146df8bae1dSRodney W. Grimes if (error == ERESTART) 1147df8bae1dSRodney W. Grimes error = EINTR; 1148df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1149df8bae1dSRodney W. Grimes error = 0; 1150841c0c7eSNathan Whitehorn 1151841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1152841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1153841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1154841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1155841c0c7eSNathan Whitehorn #undef swizzle_fdset 1156841c0c7eSNathan Whitehorn 1157df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1158b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1159df8bae1dSRodney W. Grimes error = error2; 1160df8bae1dSRodney W. Grimes if (error == 0) { 1161df8bae1dSRodney W. Grimes int error2; 1162df8bae1dSRodney W. Grimes 11638f19eb88SIan Dowse putbits(fd_in, 0); 11648f19eb88SIan Dowse putbits(fd_ou, 1); 11658f19eb88SIan Dowse putbits(fd_ex, 2); 1166df8bae1dSRodney W. Grimes #undef putbits 1167df8bae1dSRodney W. Grimes } 1168d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1169d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1170ad2edad9SMatthew Dillon 1171df8bae1dSRodney W. Grimes return (error); 1172df8bae1dSRodney W. Grimes } 117311b763dfSJeff Roberson /* 117411b763dfSJeff Roberson * Convert a select bit set to poll flags. 1175748b9df6SJeff Roberson * 117611b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 117711b763dfSJeff Roberson * return this as a set bit in any set. 117811b763dfSJeff Roberson */ 117911b763dfSJeff Roberson static int select_flags[3] = { 118011b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 118111b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 118261e53a38SKonstantin Belousov POLLRDBAND | POLLERR 118311b763dfSJeff Roberson }; 118411b763dfSJeff Roberson 118511b763dfSJeff Roberson /* 118611b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 118711b763dfSJeff Roberson * bit position in the fd_mask array. 118811b763dfSJeff Roberson */ 118911b763dfSJeff Roberson static __inline int 119060b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 119111b763dfSJeff Roberson { 119211b763dfSJeff Roberson int flags; 119311b763dfSJeff Roberson int msk; 119411b763dfSJeff Roberson 119511b763dfSJeff Roberson flags = 0; 119611b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 119711b763dfSJeff Roberson if (ibits[msk] == NULL) 119811b763dfSJeff Roberson continue; 119960b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 120011b763dfSJeff Roberson continue; 120111b763dfSJeff Roberson flags |= select_flags[msk]; 120211b763dfSJeff Roberson } 120311b763dfSJeff Roberson return (flags); 120411b763dfSJeff Roberson } 120511b763dfSJeff Roberson 120611b763dfSJeff Roberson /* 120711b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 120811b763dfSJeff Roberson * input bits originally requested. 120911b763dfSJeff Roberson */ 121011b763dfSJeff Roberson static __inline int 121111b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 121211b763dfSJeff Roberson { 121311b763dfSJeff Roberson int msk; 121411b763dfSJeff Roberson int n; 121511b763dfSJeff Roberson 121611b763dfSJeff Roberson n = 0; 121711b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 121811b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 121911b763dfSJeff Roberson continue; 122011b763dfSJeff Roberson if (ibits[msk] == NULL) 122111b763dfSJeff Roberson continue; 122211b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 122311b763dfSJeff Roberson continue; 122411b763dfSJeff Roberson /* 122511b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 122611b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 122711b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 122811b763dfSJeff Roberson * call selsetbits twice as a result. 122911b763dfSJeff Roberson */ 123011b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 123111b763dfSJeff Roberson continue; 123211b763dfSJeff Roberson obits[msk][idx] |= bit; 123311b763dfSJeff Roberson n++; 123411b763dfSJeff Roberson } 123511b763dfSJeff Roberson 123611b763dfSJeff Roberson return (n); 123711b763dfSJeff Roberson } 1238df8bae1dSRodney W. Grimes 1239a9d2f8d8SRobert Watson static __inline int 1240a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1241a9d2f8d8SRobert Watson { 12427008be5bSPawel Jakub Dawidek cap_rights_t rights; 1243a9d2f8d8SRobert Watson 1244ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT); 1245ed5848c8SPawel Jakub Dawidek 1246b7a39e9eSMateusz Guzik return (fget_unlocked(fdp, fd, &rights, fpp, NULL)); 1247a9d2f8d8SRobert Watson } 1248a9d2f8d8SRobert Watson 1249ace8398dSJeff Roberson /* 1250ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1251ace8398dSJeff Roberson * completion. 1252ace8398dSJeff Roberson */ 1253ace8398dSJeff Roberson static int 1254ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1255ace8398dSJeff Roberson { 125611b763dfSJeff Roberson struct filedesc *fdp; 125711b763dfSJeff Roberson struct selinfo *si; 1258ace8398dSJeff Roberson struct seltd *stp; 1259ace8398dSJeff Roberson struct selfd *sfp; 1260ace8398dSJeff Roberson struct selfd *sfn; 1261ace8398dSJeff Roberson struct file *fp; 12629cdacff1SJeff Roberson fd_mask bit; 12639cdacff1SJeff Roberson int fd, ev, n, idx; 1264a9d2f8d8SRobert Watson int error; 1265ace8398dSJeff Roberson 126611b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1267ace8398dSJeff Roberson stp = td->td_sel; 126811b763dfSJeff Roberson n = 0; 1269ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1270ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1271ace8398dSJeff Roberson si = sfp->sf_si; 1272ace8398dSJeff Roberson selfdfree(stp, sfp); 1273ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1274ace8398dSJeff Roberson if (si != NULL) 1275ace8398dSJeff Roberson continue; 1276a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1277a9d2f8d8SRobert Watson if (error) 1278a9d2f8d8SRobert Watson return (error); 127911b763dfSJeff Roberson idx = fd / NFDBITS; 12809cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 128111b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1282bf422e5fSJeff Roberson fdrop(fp, td); 128311b763dfSJeff Roberson if (ev != 0) 128411b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1285ace8398dSJeff Roberson } 1286ace8398dSJeff Roberson stp->st_flags = 0; 1287ace8398dSJeff Roberson td->td_retval[0] = n; 1288ace8398dSJeff Roberson return (0); 1289ace8398dSJeff Roberson } 1290ace8398dSJeff Roberson 1291ace8398dSJeff Roberson /* 1292ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1293ace8398dSJeff Roberson * each selinfo. 1294ace8398dSJeff Roberson */ 1295265fc98fSSeigo Tanimura static int 1296b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 1297b40ce416SJulian Elischer struct thread *td; 1298b08f7993SSujal Patel fd_mask **ibits, **obits; 1299cb226aaaSPoul-Henning Kamp int nfd; 1300df8bae1dSRodney W. Grimes { 130111b763dfSJeff Roberson struct filedesc *fdp; 1302df8bae1dSRodney W. Grimes struct file *fp; 13039cdacff1SJeff Roberson fd_mask bit; 130411b763dfSJeff Roberson int ev, flags, end, fd; 13059cdacff1SJeff Roberson int n, idx; 1306a9d2f8d8SRobert Watson int error; 1307df8bae1dSRodney W. Grimes 130811b763dfSJeff Roberson fdp = td->td_proc->p_fd; 130911b763dfSJeff Roberson n = 0; 13109cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 131111b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 131211b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 131311b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 131411b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 131511b763dfSJeff Roberson if (flags == 0) 1316f082218cSPeter Wemm continue; 1317a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1318a9d2f8d8SRobert Watson if (error) 1319a9d2f8d8SRobert Watson return (error); 1320ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 132111b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1322bf422e5fSJeff Roberson fdrop(fp, td); 132311b763dfSJeff Roberson if (ev != 0) 132411b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1325df8bae1dSRodney W. Grimes } 1326df8bae1dSRodney W. Grimes } 132711b763dfSJeff Roberson 1328b40ce416SJulian Elischer td->td_retval[0] = n; 1329df8bae1dSRodney W. Grimes return (0); 1330df8bae1dSRodney W. Grimes } 1331df8bae1dSRodney W. Grimes 133242d11757SPeter Wemm int 1333186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap) 1334186d9c34SDmitry Chagin { 1335186d9c34SDmitry Chagin struct timespec ts, *tsp; 1336186d9c34SDmitry Chagin 1337186d9c34SDmitry Chagin if (uap->timeout != INFTIM) { 1338186d9c34SDmitry Chagin if (uap->timeout < 0) 1339186d9c34SDmitry Chagin return (EINVAL); 1340186d9c34SDmitry Chagin ts.tv_sec = uap->timeout / 1000; 1341186d9c34SDmitry Chagin ts.tv_nsec = (uap->timeout % 1000) * 1000000; 1342186d9c34SDmitry Chagin tsp = &ts; 1343186d9c34SDmitry Chagin } else 1344186d9c34SDmitry Chagin tsp = NULL; 1345186d9c34SDmitry Chagin 1346186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); 1347186d9c34SDmitry Chagin } 1348186d9c34SDmitry Chagin 1349186d9c34SDmitry Chagin int 1350186d9c34SDmitry Chagin kern_poll(struct thread *td, struct pollfd *fds, u_int nfds, 1351186d9c34SDmitry Chagin struct timespec *tsp, sigset_t *uset) 135242d11757SPeter Wemm { 13532580f4e5SAndre Oppermann struct pollfd *bits; 13542580f4e5SAndre Oppermann struct pollfd smallbits[32]; 1355186d9c34SDmitry Chagin sbintime_t sbt, precision, tmp; 1356186d9c34SDmitry Chagin time_t over; 1357186d9c34SDmitry Chagin struct timespec ts; 1358cf5e4fe6SDavide Italiano int error; 135942d11757SPeter Wemm size_t ni; 136042d11757SPeter Wemm 1361186d9c34SDmitry Chagin precision = 0; 1362186d9c34SDmitry Chagin if (tsp != NULL) { 1363186d9c34SDmitry Chagin if (tsp->tv_sec < 0) 1364186d9c34SDmitry Chagin return (EINVAL); 1365186d9c34SDmitry Chagin if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000) 1366186d9c34SDmitry Chagin return (EINVAL); 1367186d9c34SDmitry Chagin if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1368186d9c34SDmitry Chagin sbt = 0; 1369186d9c34SDmitry Chagin else { 1370186d9c34SDmitry Chagin ts = *tsp; 1371186d9c34SDmitry Chagin if (ts.tv_sec > INT32_MAX / 2) { 1372186d9c34SDmitry Chagin over = ts.tv_sec - INT32_MAX / 2; 1373186d9c34SDmitry Chagin ts.tv_sec -= over; 1374186d9c34SDmitry Chagin } else 1375186d9c34SDmitry Chagin over = 0; 1376186d9c34SDmitry Chagin tmp = tstosbt(ts); 1377186d9c34SDmitry Chagin precision = tmp; 1378186d9c34SDmitry Chagin precision >>= tc_precexp; 1379186d9c34SDmitry Chagin if (TIMESEL(&sbt, tmp)) 1380186d9c34SDmitry Chagin sbt += tc_tick_sbt; 1381186d9c34SDmitry Chagin sbt += tmp; 1382186d9c34SDmitry Chagin } 1383186d9c34SDmitry Chagin } else 1384186d9c34SDmitry Chagin sbt = -1; 1385186d9c34SDmitry Chagin 1386374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1387ace8398dSJeff Roberson return (EINVAL); 138889b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 138942d11757SPeter Wemm if (ni > sizeof(smallbits)) 1390a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 139142d11757SPeter Wemm else 139242d11757SPeter Wemm bits = smallbits; 1393186d9c34SDmitry Chagin error = copyin(fds, bits, ni); 139442d11757SPeter Wemm if (error) 1395ace8398dSJeff Roberson goto done; 1396186d9c34SDmitry Chagin 1397186d9c34SDmitry Chagin if (uset != NULL) { 1398186d9c34SDmitry Chagin error = kern_sigprocmask(td, SIG_SETMASK, uset, 1399186d9c34SDmitry Chagin &td->td_oldsigmask, 0); 1400186d9c34SDmitry Chagin if (error) 1401ace8398dSJeff Roberson goto done; 1402186d9c34SDmitry Chagin td->td_pflags |= TDP_OLDMASK; 1403186d9c34SDmitry Chagin /* 1404186d9c34SDmitry Chagin * Make sure that ast() is called on return to 1405186d9c34SDmitry Chagin * usermode and TDP_OLDMASK is cleared, restoring old 1406186d9c34SDmitry Chagin * sigmask. 1407186d9c34SDmitry Chagin */ 1408186d9c34SDmitry Chagin thread_lock(td); 1409186d9c34SDmitry Chagin td->td_flags |= TDF_ASTPENDING; 1410186d9c34SDmitry Chagin thread_unlock(td); 141142d11757SPeter Wemm } 1412186d9c34SDmitry Chagin 1413ace8398dSJeff Roberson seltdinit(td); 1414ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1415ace8398dSJeff Roberson for (;;) { 14162580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 1417ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1418ace8398dSJeff Roberson break; 1419186d9c34SDmitry Chagin error = seltdwait(td, sbt, precision); 1420ace8398dSJeff Roberson if (error) 1421ace8398dSJeff Roberson break; 1422ace8398dSJeff Roberson error = pollrescan(td); 1423ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1424ace8398dSJeff Roberson break; 142585f190e4SAlfred Perlstein } 1426ace8398dSJeff Roberson seltdclear(td); 1427265fc98fSSeigo Tanimura 142842d11757SPeter Wemm done: 142942d11757SPeter Wemm /* poll is not restarted after signals... */ 143042d11757SPeter Wemm if (error == ERESTART) 143142d11757SPeter Wemm error = EINTR; 143242d11757SPeter Wemm if (error == EWOULDBLOCK) 143342d11757SPeter Wemm error = 0; 143442d11757SPeter Wemm if (error == 0) { 1435186d9c34SDmitry Chagin error = pollout(td, bits, fds, nfds); 143642d11757SPeter Wemm if (error) 143742d11757SPeter Wemm goto out; 143842d11757SPeter Wemm } 143942d11757SPeter Wemm out: 144042d11757SPeter Wemm if (ni > sizeof(smallbits)) 144142d11757SPeter Wemm free(bits, M_TEMP); 144242d11757SPeter Wemm return (error); 144342d11757SPeter Wemm } 144442d11757SPeter Wemm 1445186d9c34SDmitry Chagin int 1446186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap) 1447186d9c34SDmitry Chagin { 1448186d9c34SDmitry Chagin struct timespec ts, *tsp; 1449186d9c34SDmitry Chagin sigset_t set, *ssp; 1450186d9c34SDmitry Chagin int error; 1451186d9c34SDmitry Chagin 1452186d9c34SDmitry Chagin if (uap->ts != NULL) { 1453186d9c34SDmitry Chagin error = copyin(uap->ts, &ts, sizeof(ts)); 1454186d9c34SDmitry Chagin if (error) 1455186d9c34SDmitry Chagin return (error); 1456186d9c34SDmitry Chagin tsp = &ts; 1457186d9c34SDmitry Chagin } else 1458186d9c34SDmitry Chagin tsp = NULL; 1459186d9c34SDmitry Chagin if (uap->set != NULL) { 1460186d9c34SDmitry Chagin error = copyin(uap->set, &set, sizeof(set)); 1461186d9c34SDmitry Chagin if (error) 1462186d9c34SDmitry Chagin return (error); 1463186d9c34SDmitry Chagin ssp = &set; 1464186d9c34SDmitry Chagin } else 1465186d9c34SDmitry Chagin ssp = NULL; 1466186d9c34SDmitry Chagin /* 1467186d9c34SDmitry Chagin * fds is still a pointer to user space. kern_poll() will 1468186d9c34SDmitry Chagin * take care of copyin that array to the kernel space. 1469186d9c34SDmitry Chagin */ 1470186d9c34SDmitry Chagin 1471186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 1472186d9c34SDmitry Chagin } 1473186d9c34SDmitry Chagin 147442d11757SPeter Wemm static int 1475ace8398dSJeff Roberson pollrescan(struct thread *td) 1476ace8398dSJeff Roberson { 1477ace8398dSJeff Roberson struct seltd *stp; 1478ace8398dSJeff Roberson struct selfd *sfp; 1479ace8398dSJeff Roberson struct selfd *sfn; 1480ace8398dSJeff Roberson struct selinfo *si; 1481ace8398dSJeff Roberson struct filedesc *fdp; 1482ace8398dSJeff Roberson struct file *fp; 1483ace8398dSJeff Roberson struct pollfd *fd; 14848e076effSSean Bruno #ifdef CAPABILITIES 14857008be5bSPawel Jakub Dawidek cap_rights_t rights; 14868e076effSSean Bruno #endif 1487ace8398dSJeff Roberson int n; 1488ace8398dSJeff Roberson 1489ace8398dSJeff Roberson n = 0; 1490ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1491ace8398dSJeff Roberson stp = td->td_sel; 1492ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1493ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1494ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1495ace8398dSJeff Roberson si = sfp->sf_si; 1496ace8398dSJeff Roberson selfdfree(stp, sfp); 1497ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1498ace8398dSJeff Roberson if (si != NULL) 1499ace8398dSJeff Roberson continue; 15002609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fd->fd].fde_file; 1501d6f72489SJonathan Anderson #ifdef CAPABILITIES 15022609222aSPawel Jakub Dawidek if (fp == NULL || 15037008be5bSPawel Jakub Dawidek cap_check(cap_rights(fdp, fd->fd), 1504ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1505d6f72489SJonathan Anderson #else 15062609222aSPawel Jakub Dawidek if (fp == NULL) 1507d6f72489SJonathan Anderson #endif 15082609222aSPawel Jakub Dawidek { 1509ace8398dSJeff Roberson fd->revents = POLLNVAL; 1510ace8398dSJeff Roberson n++; 1511ace8398dSJeff Roberson continue; 1512ace8398dSJeff Roberson } 1513d6f72489SJonathan Anderson 1514ace8398dSJeff Roberson /* 1515ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1516ace8398dSJeff Roberson * POLLERR if appropriate. 1517ace8398dSJeff Roberson */ 1518ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1519ace8398dSJeff Roberson if (fd->revents != 0) 1520ace8398dSJeff Roberson n++; 1521ace8398dSJeff Roberson } 1522ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1523ace8398dSJeff Roberson stp->st_flags = 0; 1524ace8398dSJeff Roberson td->td_retval[0] = n; 1525ace8398dSJeff Roberson return (0); 1526ace8398dSJeff Roberson } 1527ace8398dSJeff Roberson 1528ace8398dSJeff Roberson 1529ace8398dSJeff Roberson static int 15306d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd) 15316d8feddaSKonstantin Belousov struct thread *td; 1532ae81968fSRobert Watson struct pollfd *fds; 1533ae81968fSRobert Watson struct pollfd *ufds; 1534ae81968fSRobert Watson u_int nfd; 1535ae81968fSRobert Watson { 1536ae81968fSRobert Watson int error = 0; 1537ae81968fSRobert Watson u_int i = 0; 15386d8feddaSKonstantin Belousov u_int n = 0; 1539ae81968fSRobert Watson 1540ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1541ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1542ae81968fSRobert Watson sizeof(ufds->revents)); 1543ae81968fSRobert Watson if (error) 1544ae81968fSRobert Watson return (error); 15456d8feddaSKonstantin Belousov if (fds->revents != 0) 15466d8feddaSKonstantin Belousov n++; 1547ae81968fSRobert Watson fds++; 1548ae81968fSRobert Watson ufds++; 1549ae81968fSRobert Watson } 15506d8feddaSKonstantin Belousov td->td_retval[0] = n; 1551ae81968fSRobert Watson return (0); 1552ae81968fSRobert Watson } 1553ae81968fSRobert Watson 1554ae81968fSRobert Watson static int 1555b40ce416SJulian Elischer pollscan(td, fds, nfd) 1556b40ce416SJulian Elischer struct thread *td; 155742d11757SPeter Wemm struct pollfd *fds; 1558ea0237edSJonathan Lemon u_int nfd; 155942d11757SPeter Wemm { 1560ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 156142d11757SPeter Wemm struct file *fp; 15628e076effSSean Bruno #ifdef CAPABILITIES 15637008be5bSPawel Jakub Dawidek cap_rights_t rights; 15648e076effSSean Bruno #endif 15652609222aSPawel Jakub Dawidek int i, n = 0; 156642d11757SPeter Wemm 15675e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1568eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1569adf87ab0SMateusz Guzik if (fds->fd > fdp->fd_lastfile) { 157042d11757SPeter Wemm fds->revents = POLLNVAL; 157142d11757SPeter Wemm n++; 1572337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1573337c9691SJordan K. Hubbard fds->revents = 0; 157442d11757SPeter Wemm } else { 15752609222aSPawel Jakub Dawidek fp = fdp->fd_ofiles[fds->fd].fde_file; 1576d6f72489SJonathan Anderson #ifdef CAPABILITIES 15772609222aSPawel Jakub Dawidek if (fp == NULL || 15782609222aSPawel Jakub Dawidek cap_check(cap_rights(fdp, fds->fd), 1579ed5848c8SPawel Jakub Dawidek cap_rights_init(&rights, CAP_EVENT)) != 0) 1580d6f72489SJonathan Anderson #else 15812609222aSPawel Jakub Dawidek if (fp == NULL) 1582d6f72489SJonathan Anderson #endif 15832609222aSPawel Jakub Dawidek { 158442d11757SPeter Wemm fds->revents = POLLNVAL; 158542d11757SPeter Wemm n++; 158642d11757SPeter Wemm } else { 15872087c896SBruce Evans /* 15882087c896SBruce Evans * Note: backend also returns POLLHUP and 15892087c896SBruce Evans * POLLERR if appropriate. 15902087c896SBruce Evans */ 1591ace8398dSJeff Roberson selfdalloc(td, fds); 159213ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1593ea6027a8SRobert Watson td->td_ucred, td); 1594f2159cc7SKonstantin Belousov /* 1595f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1596f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1597f2159cc7SKonstantin Belousov */ 1598f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1599f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1600f2159cc7SKonstantin Belousov 160142d11757SPeter Wemm if (fds->revents != 0) 160242d11757SPeter Wemm n++; 160342d11757SPeter Wemm } 160442d11757SPeter Wemm } 160542d11757SPeter Wemm } 16065e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1607b40ce416SJulian Elischer td->td_retval[0] = n; 160842d11757SPeter Wemm return (0); 160942d11757SPeter Wemm } 161042d11757SPeter Wemm 161142d11757SPeter Wemm /* 1612237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1613237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1614237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1615237abf0cSDavide Italiano * determine which events fired. 1616237abf0cSDavide Italiano */ 1617237abf0cSDavide Italiano int 1618237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1619237abf0cSDavide Italiano { 1620237abf0cSDavide Italiano struct timeval rtv; 1621237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1622237abf0cSDavide Italiano int error; 1623237abf0cSDavide Italiano 162476665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1625237abf0cSDavide Italiano if (tvp != NULL) { 1626237abf0cSDavide Italiano rtv = *tvp; 1627237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1628237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1629237abf0cSDavide Italiano return (EINVAL); 1630237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1631237abf0cSDavide Italiano asbt = 0; 1632237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1633237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1634237abf0cSDavide Italiano precision = rsbt; 1635237abf0cSDavide Italiano precision >>= tc_precexp; 1636237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1637237abf0cSDavide Italiano asbt += tc_tick_sbt; 16384bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1639237abf0cSDavide Italiano asbt += rsbt; 1640237abf0cSDavide Italiano else 1641237abf0cSDavide Italiano asbt = -1; 1642237abf0cSDavide Italiano } else 1643237abf0cSDavide Italiano asbt = -1; 1644237abf0cSDavide Italiano } else 1645237abf0cSDavide Italiano asbt = -1; 1646237abf0cSDavide Italiano seltdinit(td); 1647237abf0cSDavide Italiano /* 1648237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1649237abf0cSDavide Italiano */ 1650237abf0cSDavide Italiano for (;;) { 1651237abf0cSDavide Italiano selfdalloc(td, NULL); 1652237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1653237abf0cSDavide Italiano /* error here is actually the ready events. */ 1654237abf0cSDavide Italiano if (error) 1655237abf0cSDavide Italiano return (0); 1656237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1657237abf0cSDavide Italiano if (error) 1658237abf0cSDavide Italiano break; 1659237abf0cSDavide Italiano } 1660237abf0cSDavide Italiano seltdclear(td); 1661237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1662237abf0cSDavide Italiano if (error == ERESTART) 1663237abf0cSDavide Italiano error = 0; 1664237abf0cSDavide Italiano return (error); 1665237abf0cSDavide Italiano } 1666237abf0cSDavide Italiano 1667237abf0cSDavide Italiano /* 1668ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1669ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1670ace8398dSJeff Roberson */ 1671ace8398dSJeff Roberson static void 1672ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1673ace8398dSJeff Roberson { 1674ace8398dSJeff Roberson struct seltd *stp; 1675ace8398dSJeff Roberson 1676ace8398dSJeff Roberson stp = td->td_sel; 1677ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1678ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1679ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1680ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1681ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1682ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1683ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1684ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1685ace8398dSJeff Roberson } 1686ace8398dSJeff Roberson 1687ace8398dSJeff Roberson static void 1688ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1689ace8398dSJeff Roberson { 1690ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 169173f2e5f7SMateusz Guzik if (sfp->sf_si != NULL) { 1692ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1693fcb5b3a4SKonstantin Belousov if (sfp->sf_si != NULL) { 1694ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1695fcb5b3a4SKonstantin Belousov refcount_release(&sfp->sf_refs); 1696fcb5b3a4SKonstantin Belousov } 1697ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 169873f2e5f7SMateusz Guzik } 1699fcb5b3a4SKonstantin Belousov if (refcount_release(&sfp->sf_refs)) 1700ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 170185f190e4SAlfred Perlstein } 170285f190e4SAlfred Perlstein 17036aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ 17046aba400aSAttilio Rao void 17056aba400aSAttilio Rao seldrain(sip) 17066aba400aSAttilio Rao struct selinfo *sip; 17076aba400aSAttilio Rao { 17086aba400aSAttilio Rao 17096aba400aSAttilio Rao /* 17106aba400aSAttilio Rao * This feature is already provided by doselwakeup(), thus it is 17116aba400aSAttilio Rao * enough to go for it. 17126aba400aSAttilio Rao * Eventually, the context, should take care to avoid races 17136aba400aSAttilio Rao * between thread calling select()/poll() and file descriptor 17146aba400aSAttilio Rao * detaching, but, again, the races are just the same as 17156aba400aSAttilio Rao * selwakeup(). 17166aba400aSAttilio Rao */ 17176aba400aSAttilio Rao doselwakeup(sip, -1); 17186aba400aSAttilio Rao } 17196aba400aSAttilio Rao 1720df8bae1dSRodney W. Grimes /* 1721df8bae1dSRodney W. Grimes * Record a select request. 1722df8bae1dSRodney W. Grimes */ 1723df8bae1dSRodney W. Grimes void 1724df8bae1dSRodney W. Grimes selrecord(selector, sip) 1725b40ce416SJulian Elischer struct thread *selector; 1726df8bae1dSRodney W. Grimes struct selinfo *sip; 1727df8bae1dSRodney W. Grimes { 1728ace8398dSJeff Roberson struct selfd *sfp; 1729ace8398dSJeff Roberson struct seltd *stp; 1730ace8398dSJeff Roberson struct mtx *mtxp; 1731df8bae1dSRodney W. Grimes 1732ace8398dSJeff Roberson stp = selector->td_sel; 173385f190e4SAlfred Perlstein /* 1734ace8398dSJeff Roberson * Don't record when doing a rescan. 173585f190e4SAlfred Perlstein */ 1736ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1737ace8398dSJeff Roberson return; 1738ace8398dSJeff Roberson /* 1739ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1740ace8398dSJeff Roberson */ 1741ace8398dSJeff Roberson sfp = NULL; 1742ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1743ace8398dSJeff Roberson stp->st_free1 = NULL; 1744ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1745ace8398dSJeff Roberson stp->st_free2 = NULL; 1746ace8398dSJeff Roberson else 1747ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 17482141453eSJeff Roberson mtxp = sip->si_mtx; 17492141453eSJeff Roberson if (mtxp == NULL) 17502141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1751ace8398dSJeff Roberson /* 1752ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1753ace8398dSJeff Roberson */ 1754ace8398dSJeff Roberson sfp->sf_si = sip; 1755ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1756fcb5b3a4SKonstantin Belousov refcount_init(&sfp->sf_refs, 2); 1757ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1758ace8398dSJeff Roberson /* 1759ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1760ace8398dSJeff Roberson */ 1761ace8398dSJeff Roberson mtx_lock(mtxp); 1762ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1763ace8398dSJeff Roberson sip->si_mtx = mtxp; 1764ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 176585f190e4SAlfred Perlstein } 1766ace8398dSJeff Roberson /* 1767ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1768ace8398dSJeff Roberson */ 1769ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1770ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1771df8bae1dSRodney W. Grimes } 1772df8bae1dSRodney W. Grimes 1773512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1774df8bae1dSRodney W. Grimes void 1775df8bae1dSRodney W. Grimes selwakeup(sip) 177685f190e4SAlfred Perlstein struct selinfo *sip; 1777df8bae1dSRodney W. Grimes { 1778512824f8SSeigo Tanimura doselwakeup(sip, -1); 1779512824f8SSeigo Tanimura } 1780512824f8SSeigo Tanimura 1781512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1782512824f8SSeigo Tanimura void 1783512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1784512824f8SSeigo Tanimura struct selinfo *sip; 1785512824f8SSeigo Tanimura int pri; 1786512824f8SSeigo Tanimura { 1787512824f8SSeigo Tanimura doselwakeup(sip, pri); 1788512824f8SSeigo Tanimura } 1789512824f8SSeigo Tanimura 1790512824f8SSeigo Tanimura /* 1791512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1792512824f8SSeigo Tanimura */ 1793512824f8SSeigo Tanimura static void 1794512824f8SSeigo Tanimura doselwakeup(sip, pri) 1795512824f8SSeigo Tanimura struct selinfo *sip; 1796512824f8SSeigo Tanimura int pri; 1797512824f8SSeigo Tanimura { 1798ace8398dSJeff Roberson struct selfd *sfp; 1799ace8398dSJeff Roberson struct selfd *sfn; 1800ace8398dSJeff Roberson struct seltd *stp; 1801df8bae1dSRodney W. Grimes 1802ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1803ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1804b40ce416SJulian Elischer return; 1805ace8398dSJeff Roberson /* 1806ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1807ace8398dSJeff Roberson */ 1808ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1809ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1810ace8398dSJeff Roberson /* 1811ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1812ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1813ace8398dSJeff Roberson */ 1814ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1815ace8398dSJeff Roberson sfp->sf_si = NULL; 1816ace8398dSJeff Roberson stp = sfp->sf_td; 1817ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1818ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1819ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1820ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1821fcb5b3a4SKonstantin Belousov if (refcount_release(&sfp->sf_refs)) 1822fcb5b3a4SKonstantin Belousov uma_zfree(selfd_zone, sfp); 1823b40ce416SJulian Elischer } 1824ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1825ace8398dSJeff Roberson } 1826ace8398dSJeff Roberson 1827ace8398dSJeff Roberson static void 1828ace8398dSJeff Roberson seltdinit(struct thread *td) 1829ace8398dSJeff Roberson { 1830ace8398dSJeff Roberson struct seltd *stp; 1831ace8398dSJeff Roberson 1832ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1833ace8398dSJeff Roberson goto out; 1834ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1835ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1836ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1837ace8398dSJeff Roberson out: 1838ace8398dSJeff Roberson stp->st_flags = 0; 1839ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1840ace8398dSJeff Roberson } 1841ace8398dSJeff Roberson 1842ace8398dSJeff Roberson static int 1843cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision) 1844ace8398dSJeff Roberson { 1845ace8398dSJeff Roberson struct seltd *stp; 1846ace8398dSJeff Roberson int error; 1847ace8398dSJeff Roberson 1848ace8398dSJeff Roberson stp = td->td_sel; 1849ace8398dSJeff Roberson /* 1850ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1851ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1852ace8398dSJeff Roberson */ 1853ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1854ace8398dSJeff Roberson /* 1855ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1856ace8398dSJeff Roberson */ 1857ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1858ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1859ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1860ace8398dSJeff Roberson return (0); 1861ace8398dSJeff Roberson } 1862cf5e4fe6SDavide Italiano if (sbt == 0) 1863cf5e4fe6SDavide Italiano error = EWOULDBLOCK; 1864cf5e4fe6SDavide Italiano else if (sbt != -1) 1865cf5e4fe6SDavide Italiano error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx, 1866cf5e4fe6SDavide Italiano sbt, precision, C_ABSOLUTE); 1867ace8398dSJeff Roberson else 1868ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1869ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1870ace8398dSJeff Roberson 1871ace8398dSJeff Roberson return (error); 1872ace8398dSJeff Roberson } 1873ace8398dSJeff Roberson 1874ace8398dSJeff Roberson void 1875ace8398dSJeff Roberson seltdfini(struct thread *td) 1876ace8398dSJeff Roberson { 1877ace8398dSJeff Roberson struct seltd *stp; 1878ace8398dSJeff Roberson 1879ace8398dSJeff Roberson stp = td->td_sel; 1880ace8398dSJeff Roberson if (stp == NULL) 1881ace8398dSJeff Roberson return; 1882ace8398dSJeff Roberson if (stp->st_free1) 1883ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1884ace8398dSJeff Roberson if (stp->st_free2) 1885ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1886ace8398dSJeff Roberson td->td_sel = NULL; 1887ace8398dSJeff Roberson free(stp, M_SELECT); 1888ace8398dSJeff Roberson } 1889ace8398dSJeff Roberson 1890ace8398dSJeff Roberson /* 1891ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1892ace8398dSJeff Roberson * polling. 1893ace8398dSJeff Roberson */ 1894ace8398dSJeff Roberson static void 1895ace8398dSJeff Roberson seltdclear(struct thread *td) 1896ace8398dSJeff Roberson { 1897ace8398dSJeff Roberson struct seltd *stp; 1898ace8398dSJeff Roberson struct selfd *sfp; 1899ace8398dSJeff Roberson struct selfd *sfn; 1900ace8398dSJeff Roberson 1901ace8398dSJeff Roberson stp = td->td_sel; 1902ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1903ace8398dSJeff Roberson selfdfree(stp, sfp); 1904ace8398dSJeff Roberson stp->st_flags = 0; 1905df8bae1dSRodney W. Grimes } 1906265fc98fSSeigo Tanimura 19074d77a549SAlfred Perlstein static void selectinit(void *); 1908ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1909265fc98fSSeigo Tanimura static void 1910ace8398dSJeff Roberson selectinit(void *dummy __unused) 1911265fc98fSSeigo Tanimura { 19122141453eSJeff Roberson 1913ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1914ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 19152141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1916265fc98fSSeigo Tanimura } 19170acf5d0bSMark Johnston 19180acf5d0bSMark Johnston /* 19190acf5d0bSMark Johnston * Set up a syscall return value that follows the convention specified for 19200acf5d0bSMark Johnston * posix_* functions. 19210acf5d0bSMark Johnston */ 19220acf5d0bSMark Johnston int 19230acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error) 19240acf5d0bSMark Johnston { 19250acf5d0bSMark Johnston 19260acf5d0bSMark Johnston if (error <= 0) 19270acf5d0bSMark Johnston return (error); 19280acf5d0bSMark Johnston td->td_errno = error; 19290acf5d0bSMark Johnston td->td_pflags |= TDP_NERRNO; 19300acf5d0bSMark Johnston td->td_retval[0] = error; 19310acf5d0bSMark Johnston return (0); 19320acf5d0bSMark Johnston } 1933