19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro 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" 43db6a20e2SGarrett Wollman #include "opt_ktrace.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/systm.h> 47d2d3e875SBruce Evans #include <sys/sysproto.h> 484a144410SRobert Watson #include <sys/capsicum.h> 49df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 5020982410SBruce Evans #include <sys/filio.h> 513ac4d1efSBruce Evans #include <sys/fcntl.h> 52df8bae1dSRodney W. Grimes #include <sys/file.h> 532609222aSPawel Jakub Dawidek #include <sys/lock.h> 54df8bae1dSRodney W. Grimes #include <sys/proc.h> 55797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 56df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 57df8bae1dSRodney W. Grimes #include <sys/uio.h> 58*7a202823SKonstantin Belousov #include <sys/eventfd.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> 67*7a202823SKonstantin Belousov #include <sys/specialfd.h> 688f19eb88SIan Dowse #include <sys/syscallsubr.h> 698cb96f20SPeter Wemm #include <sys/sysctl.h> 7042d11757SPeter Wemm #include <sys/sysent.h> 719bbee259SAndrey A. Chernov #include <sys/vnode.h> 72279d7226SMatthew Dillon #include <sys/bio.h> 73279d7226SMatthew Dillon #include <sys/buf.h> 74265fc98fSSeigo Tanimura #include <sys/condvar.h> 75df8bae1dSRodney W. Grimes #ifdef KTRACE 76df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 77df8bae1dSRodney W. Grimes #endif 78df8bae1dSRodney W. Grimes 79e4650294SJohn Baldwin #include <security/audit/audit.h> 80ace8398dSJeff Roberson 8150ae6690SHans Petter Selasky /* 8250ae6690SHans Petter Selasky * The following macro defines how many bytes will be allocated from 8350ae6690SHans Petter Selasky * the stack instead of memory allocated when passing the IOCTL data 8450ae6690SHans Petter Selasky * structures from userspace and to the kernel. Some IOCTLs having 8550ae6690SHans Petter Selasky * small data structures are used very frequently and this small 8650ae6690SHans Petter Selasky * buffer on the stack gives a significant speedup improvement for 8750ae6690SHans Petter Selasky * those requests. The value of this define should be greater or equal 8850ae6690SHans Petter Selasky * to 64 bytes and should also be power of two. The data structure is 8950ae6690SHans Petter Selasky * currently hard-aligned to a 8-byte boundary on the stack. This 9050ae6690SHans Petter Selasky * should currently be sufficient for all supported platforms. 9150ae6690SHans Petter Selasky */ 9250ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_SIZE 128 /* bytes */ 9350ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_ALIGN 8 /* bytes */ 9450ae6690SHans Petter Selasky 95611fcff9SJohn Baldwin #ifdef __LP64__ 96611fcff9SJohn Baldwin static int iosize_max_clamp = 0; 978bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW, 988bb9a904SKonstantin Belousov &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX"); 99611fcff9SJohn Baldwin static int devfs_iosize_max_clamp = 1; 100bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW, 101bf3e483bSKonstantin Belousov &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices"); 102611fcff9SJohn Baldwin #endif 103bf3e483bSKonstantin Belousov 1048bb9a904SKonstantin Belousov /* 1058bb9a904SKonstantin Belousov * Assert that the return value of read(2) and write(2) syscalls fits 1068bb9a904SKonstantin Belousov * into a register. If not, an architecture will need to provide the 1078bb9a904SKonstantin Belousov * usermode wrappers to reconstruct the result. 1088bb9a904SKonstantin Belousov */ 1098bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t)); 110526d0bd5SKonstantin Belousov 111a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 112a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 113a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 11455166637SPoul-Henning Kamp 1156d8feddaSKonstantin Belousov static int pollout(struct thread *, struct pollfd *, struct pollfd *, 1166d8feddaSKonstantin Belousov u_int); 117bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 118ace8398dSJeff Roberson static int pollrescan(struct thread *); 119bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 120ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 121ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 122ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 123bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 124bcd9e0ddSJohn Baldwin off_t, int); 125bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 126bcd9e0ddSJohn Baldwin off_t, int); 127512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 128ace8398dSJeff Roberson static void seltdinit(struct thread *); 129cf5e4fe6SDavide Italiano static int seltdwait(struct thread *, sbintime_t, sbintime_t); 130ace8398dSJeff Roberson static void seltdclear(struct thread *); 131ace8398dSJeff Roberson 132ace8398dSJeff Roberson /* 133ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 134ace8398dSJeff Roberson * 135ace8398dSJeff Roberson * t - protected by st_mtx 136ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 137ace8398dSJeff Roberson */ 138ace8398dSJeff Roberson struct seltd { 139ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 140ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 141ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 142ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 143ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 144ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 145ace8398dSJeff Roberson }; 146ace8398dSJeff Roberson 147ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 148ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 149ace8398dSJeff Roberson 150ace8398dSJeff Roberson /* 151ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 152ace8398dSJeff Roberson * f - protected by sf_mtx 153ace8398dSJeff Roberson */ 154ace8398dSJeff Roberson struct selfd { 155ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 156ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 157ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 158ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 159ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 160ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 161ace8398dSJeff Roberson }; 162ace8398dSJeff Roberson 163ea33cca9SMateusz Guzik MALLOC_DEFINE(M_SELFD, "selfd", "selfd"); 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 192cc3c9df8SEd Maste sys_read(struct thread *td, struct read_args *uap) 193df8bae1dSRodney W. Grimes { 194bcd9e0ddSJohn Baldwin struct uio auio; 195bcd9e0ddSJohn Baldwin struct iovec aiov; 196279d7226SMatthew Dillon int error; 197df8bae1dSRodney W. Grimes 198526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 199bcd9e0ddSJohn Baldwin return (EINVAL); 200bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 201bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 202bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 203bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 204bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 205bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 206bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 207279d7226SMatthew Dillon return (error); 208df8bae1dSRodney W. Grimes } 209df8bae1dSRodney W. Grimes 210df8bae1dSRodney W. Grimes /* 211bcd9e0ddSJohn Baldwin * Positioned read system call 2124160ccd9SAlan Cox */ 2134160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 2144160ccd9SAlan Cox struct pread_args { 2154160ccd9SAlan Cox int fd; 2164160ccd9SAlan Cox void *buf; 2174160ccd9SAlan Cox size_t nbyte; 2188fe387abSDmitrij Tejblum int pad; 2194160ccd9SAlan Cox off_t offset; 2204160ccd9SAlan Cox }; 2214160ccd9SAlan Cox #endif 2224160ccd9SAlan Cox int 223b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap) 224b38b22b0SEdward Tomasz Napierala { 225b38b22b0SEdward Tomasz Napierala 226b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 227b38b22b0SEdward Tomasz Napierala } 228b38b22b0SEdward Tomasz Napierala 229b38b22b0SEdward Tomasz Napierala int 230b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset) 2314160ccd9SAlan Cox { 2324160ccd9SAlan Cox struct uio auio; 2334160ccd9SAlan Cox struct iovec aiov; 234bcd9e0ddSJohn Baldwin int error; 2354160ccd9SAlan Cox 236b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 237bcd9e0ddSJohn Baldwin return (EINVAL); 238b38b22b0SEdward Tomasz Napierala aiov.iov_base = buf; 239b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 2404160ccd9SAlan Cox auio.uio_iov = &aiov; 2414160ccd9SAlan Cox auio.uio_iovcnt = 1; 242b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 2434160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 244b38b22b0SEdward Tomasz Napierala error = kern_preadv(td, fd, &auio, offset); 2454160ccd9SAlan Cox return (error); 2464160ccd9SAlan Cox } 2474160ccd9SAlan Cox 2480538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 249c2815ad5SPeter Wemm int 250b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap) 251c2815ad5SPeter Wemm { 252c2815ad5SPeter Wemm 253b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 254c2815ad5SPeter Wemm } 2550538aafcSKonstantin Belousov #endif 256c2815ad5SPeter Wemm 2574160ccd9SAlan Cox /* 258df8bae1dSRodney W. Grimes * Scatter read system call. 259df8bae1dSRodney W. Grimes */ 260d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 261df8bae1dSRodney W. Grimes struct readv_args { 2627147b19dSBruce Evans int fd; 263df8bae1dSRodney W. Grimes struct iovec *iovp; 264df8bae1dSRodney W. Grimes u_int iovcnt; 265df8bae1dSRodney W. Grimes }; 266d2d3e875SBruce Evans #endif 26726f9a767SRodney W. Grimes int 2688451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap) 269df8bae1dSRodney W. Grimes { 270b88ec951SJohn Baldwin struct uio *auio; 271b88ec951SJohn Baldwin int error; 272b88ec951SJohn Baldwin 273b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 274b88ec951SJohn Baldwin if (error) 275b88ec951SJohn Baldwin return (error); 276b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 277b88ec951SJohn Baldwin free(auio, M_IOV); 278b88ec951SJohn Baldwin return (error); 279b88ec951SJohn Baldwin } 280b88ec951SJohn Baldwin 281b88ec951SJohn Baldwin int 282b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 283b88ec951SJohn Baldwin { 284b064d43dSMatthew Dillon struct file *fp; 285bcd9e0ddSJohn Baldwin int error; 286bcd9e0ddSJohn Baldwin 287cbd92ce6SMatt Macy error = fget_read(td, fd, &cap_read_rights, &fp); 288bcd9e0ddSJohn Baldwin if (error) 289bcd9e0ddSJohn Baldwin return (error); 290bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 291bcd9e0ddSJohn Baldwin fdrop(fp, td); 292bcd9e0ddSJohn Baldwin return (error); 293bcd9e0ddSJohn Baldwin } 294bcd9e0ddSJohn Baldwin 295bcd9e0ddSJohn Baldwin /* 296bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 297bcd9e0ddSJohn Baldwin */ 298bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 299bcd9e0ddSJohn Baldwin struct preadv_args { 300bcd9e0ddSJohn Baldwin int fd; 301bcd9e0ddSJohn Baldwin struct iovec *iovp; 302bcd9e0ddSJohn Baldwin u_int iovcnt; 303bcd9e0ddSJohn Baldwin off_t offset; 304bcd9e0ddSJohn Baldwin }; 305bcd9e0ddSJohn Baldwin #endif 306bcd9e0ddSJohn Baldwin int 3078451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap) 308bcd9e0ddSJohn Baldwin { 309bcd9e0ddSJohn Baldwin struct uio *auio; 310bcd9e0ddSJohn Baldwin int error; 311bcd9e0ddSJohn Baldwin 312bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 313bcd9e0ddSJohn Baldwin if (error) 314bcd9e0ddSJohn Baldwin return (error); 315bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 316bcd9e0ddSJohn Baldwin free(auio, M_IOV); 317bcd9e0ddSJohn Baldwin return (error); 318bcd9e0ddSJohn Baldwin } 319bcd9e0ddSJohn Baldwin 320bcd9e0ddSJohn Baldwin int 321cc3c9df8SEd Maste kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset) 322bcd9e0ddSJohn Baldwin { 323bcd9e0ddSJohn Baldwin struct file *fp; 324bcd9e0ddSJohn Baldwin int error; 325bcd9e0ddSJohn Baldwin 326cbd92ce6SMatt Macy error = fget_read(td, fd, &cap_pread_rights, &fp); 327bcd9e0ddSJohn Baldwin if (error) 328bcd9e0ddSJohn Baldwin return (error); 329bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 330bcd9e0ddSJohn Baldwin error = ESPIPE; 33132a1fb0dSMahdi Mokhtari else if (offset < 0 && 33232a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 333bcd9e0ddSJohn Baldwin error = EINVAL; 334bcd9e0ddSJohn Baldwin else 335bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 336bcd9e0ddSJohn Baldwin fdrop(fp, td); 337bcd9e0ddSJohn Baldwin return (error); 338bcd9e0ddSJohn Baldwin } 339bcd9e0ddSJohn Baldwin 340bcd9e0ddSJohn Baldwin /* 341bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 342bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 343bcd9e0ddSJohn Baldwin */ 344bcd9e0ddSJohn Baldwin static int 345cc3c9df8SEd Maste dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio, 346cc3c9df8SEd Maste off_t offset, int flags) 347bcd9e0ddSJohn Baldwin { 348bcd9e0ddSJohn Baldwin ssize_t cnt; 34982641acdSAlan Cox int error; 350df8bae1dSRodney W. Grimes #ifdef KTRACE 351552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 352df8bae1dSRodney W. Grimes #endif 353df8bae1dSRodney W. Grimes 35451d1f690SRobert Watson AUDIT_ARG_FD(fd); 35551d1f690SRobert Watson 3564f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3574f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3584f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3594f8d23d6SPoul-Henning Kamp return (0); 3604f8d23d6SPoul-Henning Kamp } 361552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 362bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 363552afd9cSPoul-Henning Kamp auio->uio_td = td; 364df8bae1dSRodney W. Grimes #ifdef KTRACE 365552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 366552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 367df8bae1dSRodney W. Grimes #endif 368552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 369bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 370552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 371df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 372df8bae1dSRodney W. Grimes error = 0; 373279d7226SMatthew Dillon } 374552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 375df8bae1dSRodney W. Grimes #ifdef KTRACE 376552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 377552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 378b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 379df8bae1dSRodney W. Grimes } 380df8bae1dSRodney W. Grimes #endif 381b40ce416SJulian Elischer td->td_retval[0] = cnt; 382df8bae1dSRodney W. Grimes return (error); 383df8bae1dSRodney W. Grimes } 384df8bae1dSRodney W. Grimes 385d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 386df8bae1dSRodney W. Grimes struct write_args { 387df8bae1dSRodney W. Grimes int fd; 388134e06feSBruce Evans const void *buf; 389134e06feSBruce Evans size_t nbyte; 390df8bae1dSRodney W. Grimes }; 391d2d3e875SBruce Evans #endif 39226f9a767SRodney W. Grimes int 393cc3c9df8SEd Maste sys_write(struct thread *td, struct write_args *uap) 394df8bae1dSRodney W. Grimes { 395bcd9e0ddSJohn Baldwin struct uio auio; 396bcd9e0ddSJohn Baldwin struct iovec aiov; 397279d7226SMatthew Dillon int error; 398df8bae1dSRodney W. Grimes 399526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 400bcd9e0ddSJohn Baldwin return (EINVAL); 401bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 402bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 403bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 404bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 405bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 406bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 407bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 408279d7226SMatthew Dillon return (error); 409df8bae1dSRodney W. Grimes } 410df8bae1dSRodney W. Grimes 411df8bae1dSRodney W. Grimes /* 4120c14ff0eSRobert Watson * Positioned write system call. 4134160ccd9SAlan Cox */ 4144160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 4154160ccd9SAlan Cox struct pwrite_args { 4164160ccd9SAlan Cox int fd; 4174160ccd9SAlan Cox const void *buf; 4184160ccd9SAlan Cox size_t nbyte; 4198fe387abSDmitrij Tejblum int pad; 4204160ccd9SAlan Cox off_t offset; 4214160ccd9SAlan Cox }; 4224160ccd9SAlan Cox #endif 4234160ccd9SAlan Cox int 424b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap) 425b38b22b0SEdward Tomasz Napierala { 426b38b22b0SEdward Tomasz Napierala 427b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 428b38b22b0SEdward Tomasz Napierala } 429b38b22b0SEdward Tomasz Napierala 430b38b22b0SEdward Tomasz Napierala int 431b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte, 432b38b22b0SEdward Tomasz Napierala off_t offset) 4334160ccd9SAlan Cox { 4344160ccd9SAlan Cox struct uio auio; 4354160ccd9SAlan Cox struct iovec aiov; 436bcd9e0ddSJohn Baldwin int error; 4374160ccd9SAlan Cox 438b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 439bcd9e0ddSJohn Baldwin return (EINVAL); 440b38b22b0SEdward Tomasz Napierala aiov.iov_base = (void *)(uintptr_t)buf; 441b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 4424160ccd9SAlan Cox auio.uio_iov = &aiov; 4434160ccd9SAlan Cox auio.uio_iovcnt = 1; 444b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 4454160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 446b38b22b0SEdward Tomasz Napierala error = kern_pwritev(td, fd, &auio, offset); 4474160ccd9SAlan Cox return (error); 4484160ccd9SAlan Cox } 4494160ccd9SAlan Cox 4500538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 451c2815ad5SPeter Wemm int 452b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap) 453c2815ad5SPeter Wemm { 454c2815ad5SPeter Wemm 455b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 456c2815ad5SPeter Wemm } 4570538aafcSKonstantin Belousov #endif 458c2815ad5SPeter Wemm 4594160ccd9SAlan Cox /* 4600c14ff0eSRobert Watson * Gather write system call. 461df8bae1dSRodney W. Grimes */ 462d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 463df8bae1dSRodney W. Grimes struct writev_args { 464df8bae1dSRodney W. Grimes int fd; 465df8bae1dSRodney W. Grimes struct iovec *iovp; 466df8bae1dSRodney W. Grimes u_int iovcnt; 467df8bae1dSRodney W. Grimes }; 468d2d3e875SBruce Evans #endif 46926f9a767SRodney W. Grimes int 4708451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap) 471df8bae1dSRodney W. Grimes { 472b88ec951SJohn Baldwin struct uio *auio; 473b88ec951SJohn Baldwin int error; 474b88ec951SJohn Baldwin 475b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 476b88ec951SJohn Baldwin if (error) 477b88ec951SJohn Baldwin return (error); 478b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 479b88ec951SJohn Baldwin free(auio, M_IOV); 480b88ec951SJohn Baldwin return (error); 481b88ec951SJohn Baldwin } 482b88ec951SJohn Baldwin 483b88ec951SJohn Baldwin int 484b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 485b88ec951SJohn Baldwin { 486b064d43dSMatthew Dillon struct file *fp; 487bcd9e0ddSJohn Baldwin int error; 488bcd9e0ddSJohn Baldwin 489cbd92ce6SMatt Macy error = fget_write(td, fd, &cap_write_rights, &fp); 490bcd9e0ddSJohn Baldwin if (error) 491af56abaaSJohn Baldwin return (error); 492bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 493bcd9e0ddSJohn Baldwin fdrop(fp, td); 494bcd9e0ddSJohn Baldwin return (error); 495bcd9e0ddSJohn Baldwin } 496bcd9e0ddSJohn Baldwin 497bcd9e0ddSJohn Baldwin /* 4980c14ff0eSRobert Watson * Gather positioned write system call. 499bcd9e0ddSJohn Baldwin */ 500bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 501bcd9e0ddSJohn Baldwin struct pwritev_args { 502bcd9e0ddSJohn Baldwin int fd; 503bcd9e0ddSJohn Baldwin struct iovec *iovp; 504bcd9e0ddSJohn Baldwin u_int iovcnt; 505bcd9e0ddSJohn Baldwin off_t offset; 506bcd9e0ddSJohn Baldwin }; 507bcd9e0ddSJohn Baldwin #endif 508bcd9e0ddSJohn Baldwin int 5098451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap) 510bcd9e0ddSJohn Baldwin { 511bcd9e0ddSJohn Baldwin struct uio *auio; 512bcd9e0ddSJohn Baldwin int error; 513bcd9e0ddSJohn Baldwin 514bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 515bcd9e0ddSJohn Baldwin if (error) 516bcd9e0ddSJohn Baldwin return (error); 517bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 518bcd9e0ddSJohn Baldwin free(auio, M_IOV); 519bcd9e0ddSJohn Baldwin return (error); 520bcd9e0ddSJohn Baldwin } 521bcd9e0ddSJohn Baldwin 522bcd9e0ddSJohn Baldwin int 523d5cdcc3aSAndrew Gallatin kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset) 524bcd9e0ddSJohn Baldwin { 525bcd9e0ddSJohn Baldwin struct file *fp; 526bcd9e0ddSJohn Baldwin int error; 527bcd9e0ddSJohn Baldwin 528cbd92ce6SMatt Macy error = fget_write(td, fd, &cap_pwrite_rights, &fp); 529bcd9e0ddSJohn Baldwin if (error) 530af56abaaSJohn Baldwin return (error); 531bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 532bcd9e0ddSJohn Baldwin error = ESPIPE; 53332a1fb0dSMahdi Mokhtari else if (offset < 0 && 53432a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 535bcd9e0ddSJohn Baldwin error = EINVAL; 536bcd9e0ddSJohn Baldwin else 537bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 538bcd9e0ddSJohn Baldwin fdrop(fp, td); 539bcd9e0ddSJohn Baldwin return (error); 540bcd9e0ddSJohn Baldwin } 541bcd9e0ddSJohn Baldwin 542bcd9e0ddSJohn Baldwin /* 543bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 544bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 545bcd9e0ddSJohn Baldwin */ 546bcd9e0ddSJohn Baldwin static int 547cc3c9df8SEd Maste dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio, 548cc3c9df8SEd Maste off_t offset, int flags) 549bcd9e0ddSJohn Baldwin { 550bcd9e0ddSJohn Baldwin ssize_t cnt; 551552afd9cSPoul-Henning Kamp int error; 552df8bae1dSRodney W. Grimes #ifdef KTRACE 553552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 554df8bae1dSRodney W. Grimes #endif 555df8bae1dSRodney W. Grimes 55651d1f690SRobert Watson AUDIT_ARG_FD(fd); 557552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 558552afd9cSPoul-Henning Kamp auio->uio_td = td; 559bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 560df8bae1dSRodney W. Grimes #ifdef KTRACE 561552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 562552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 563df8bae1dSRodney W. Grimes #endif 564552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 565bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 566552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 567df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 568df8bae1dSRodney W. Grimes error = 0; 569bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5706f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 571b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 5727a6f3d78SJohn Baldwin tdsignal(td, SIGPIPE); 573b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 57419eb87d2SJohn Baldwin } 575df8bae1dSRodney W. Grimes } 576552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 577df8bae1dSRodney W. Grimes #ifdef KTRACE 578552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 579552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 580b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 581df8bae1dSRodney W. Grimes } 582df8bae1dSRodney W. Grimes #endif 583b40ce416SJulian Elischer td->td_retval[0] = cnt; 584df8bae1dSRodney W. Grimes return (error); 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes 587e4650294SJohn Baldwin /* 588e4650294SJohn Baldwin * Truncate a file given a file descriptor. 589e4650294SJohn Baldwin * 590e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 591e4650294SJohn Baldwin * descriptor isn't writable. 592e4650294SJohn Baldwin */ 593e4650294SJohn Baldwin int 594cc3c9df8SEd Maste kern_ftruncate(struct thread *td, int fd, off_t length) 595e4650294SJohn Baldwin { 596e4650294SJohn Baldwin struct file *fp; 597e4650294SJohn Baldwin int error; 598e4650294SJohn Baldwin 59914961ba7SRobert Watson AUDIT_ARG_FD(fd); 600e4650294SJohn Baldwin if (length < 0) 601e4650294SJohn Baldwin return (EINVAL); 602cbd92ce6SMatt Macy error = fget(td, fd, &cap_ftruncate_rights, &fp); 603e4650294SJohn Baldwin if (error) 604e4650294SJohn Baldwin return (error); 60514961ba7SRobert Watson AUDIT_ARG_FILE(td->td_proc, fp); 606e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 607e4650294SJohn Baldwin fdrop(fp, td); 608e4650294SJohn Baldwin return (EINVAL); 609e4650294SJohn Baldwin } 610e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 611e4650294SJohn Baldwin fdrop(fp, td); 612e4650294SJohn Baldwin return (error); 613e4650294SJohn Baldwin } 614e4650294SJohn Baldwin 615e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 616e4650294SJohn Baldwin struct ftruncate_args { 617e4650294SJohn Baldwin int fd; 618e4650294SJohn Baldwin int pad; 619e4650294SJohn Baldwin off_t length; 620e4650294SJohn Baldwin }; 621e4650294SJohn Baldwin #endif 622e4650294SJohn Baldwin int 623cc3c9df8SEd Maste sys_ftruncate(struct thread *td, struct ftruncate_args *uap) 624e4650294SJohn Baldwin { 625e4650294SJohn Baldwin 626e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 627e4650294SJohn Baldwin } 628e4650294SJohn Baldwin 629e4650294SJohn Baldwin #if defined(COMPAT_43) 630e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 631e4650294SJohn Baldwin struct oftruncate_args { 632e4650294SJohn Baldwin int fd; 633e4650294SJohn Baldwin long length; 634e4650294SJohn Baldwin }; 635e4650294SJohn Baldwin #endif 636e4650294SJohn Baldwin int 637cc3c9df8SEd Maste oftruncate(struct thread *td, struct oftruncate_args *uap) 638e4650294SJohn Baldwin { 639e4650294SJohn Baldwin 640e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 641e4650294SJohn Baldwin } 642e4650294SJohn Baldwin #endif /* COMPAT_43 */ 643e4650294SJohn Baldwin 644d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 645df8bae1dSRodney W. Grimes struct ioctl_args { 646df8bae1dSRodney W. Grimes int fd; 647069e9bc1SDoug Rabson u_long com; 648df8bae1dSRodney W. Grimes caddr_t data; 649df8bae1dSRodney W. Grimes }; 650d2d3e875SBruce Evans #endif 651df8bae1dSRodney W. Grimes /* ARGSUSED */ 65226f9a767SRodney W. Grimes int 6538451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap) 654df8bae1dSRodney W. Grimes { 65550ae6690SHans Petter Selasky u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN); 656a90fb6cfSHans Petter Selasky uint32_t com; 6579fddcc66SRuslan Ermilov int arg, error; 6583e15c66fSPoul-Henning Kamp u_int size; 6599fddcc66SRuslan Ermilov caddr_t data; 660df8bae1dSRodney W. Grimes 661a90fb6cfSHans Petter Selasky #ifdef INVARIANTS 6629fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6639fc6aa06SPoul-Henning Kamp printf( 6649fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 665431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6669fc6aa06SPoul-Henning Kamp } 667a90fb6cfSHans Petter Selasky #endif 668a90fb6cfSHans Petter Selasky com = (uint32_t)uap->com; 669df8bae1dSRodney W. Grimes 670df8bae1dSRodney W. Grimes /* 671df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 672df8bae1dSRodney W. Grimes * copied to/from the user's address space. 673df8bae1dSRodney W. Grimes */ 674df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 675ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 676ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6772de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6782de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6792de92a38SPeter Wemm #else 6802de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6812de92a38SPeter Wemm #endif 6829fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 683426da3bcSAlfred Perlstein return (ENOTTY); 684279d7226SMatthew Dillon 685ca51b19bSPoul-Henning Kamp if (size > 0) { 686715457f6SDavid E. O'Brien if (com & IOC_VOID) { 6879fddcc66SRuslan Ermilov /* Integer argument. */ 6889fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6899fddcc66SRuslan Ermilov data = (void *)&arg; 6909fddcc66SRuslan Ermilov size = 0; 6915cbf44bfSMateusz Guzik } else { 6920ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 693715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 6940ecd606bSHans Petter Selasky else 6950ecd606bSHans Petter Selasky data = smalldata; 6965cbf44bfSMateusz Guzik } 6979fddcc66SRuslan Ermilov } else 6989fddcc66SRuslan Ermilov data = (void *)&uap->data; 699df8bae1dSRodney W. Grimes if (com & IOC_IN) { 700df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 7015cbf44bfSMateusz Guzik if (error != 0) 7025cbf44bfSMateusz Guzik goto out; 703ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 704df8bae1dSRodney W. Grimes /* 705df8bae1dSRodney W. Grimes * Zero the buffer so the user always 706df8bae1dSRodney W. Grimes * gets back something deterministic. 707df8bae1dSRodney W. Grimes */ 708df8bae1dSRodney W. Grimes bzero(data, size); 709279d7226SMatthew Dillon } 710df8bae1dSRodney W. Grimes 711d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 712d9f46233SJohn Baldwin 713d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 714d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 715d9f46233SJohn Baldwin 7165cbf44bfSMateusz Guzik out: 7170ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 7189fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 719d9f46233SJohn Baldwin return (error); 720d9f46233SJohn Baldwin } 721d9f46233SJohn Baldwin 722d9f46233SJohn Baldwin int 723d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 724d9f46233SJohn Baldwin { 725d9f46233SJohn Baldwin struct file *fp; 726d9f46233SJohn Baldwin struct filedesc *fdp; 7272609222aSPawel Jakub Dawidek int error, tmp, locked; 728d9f46233SJohn Baldwin 72964c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 73064c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7312609222aSPawel Jakub Dawidek 732d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7332609222aSPawel Jakub Dawidek 734d9f46233SJohn Baldwin switch (com) { 735d9f46233SJohn Baldwin case FIONCLEX: 736d9f46233SJohn Baldwin case FIOCLEX: 7375e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7382609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7392609222aSPawel Jakub Dawidek break; 7402609222aSPawel Jakub Dawidek default: 7412609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7422609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7432609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7442609222aSPawel Jakub Dawidek #else 7452609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7462609222aSPawel Jakub Dawidek #endif 7472609222aSPawel Jakub Dawidek break; 7482609222aSPawel Jakub Dawidek } 7492609222aSPawel Jakub Dawidek 7502609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7512609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7522609222aSPawel Jakub Dawidek error = EBADF; 7532609222aSPawel Jakub Dawidek goto out; 7542609222aSPawel Jakub Dawidek } 7552609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7562609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7572609222aSPawel Jakub Dawidek goto out; 7582609222aSPawel Jakub Dawidek } 759f1cf2b9dSKonstantin Belousov if (!fhold(fp)) { 760f1cf2b9dSKonstantin Belousov error = EBADF; 761f1cf2b9dSKonstantin Belousov fp = NULL; 762f1cf2b9dSKonstantin Belousov goto out; 763f1cf2b9dSKonstantin Belousov } 7642609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 7652609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 7662609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7672609222aSPawel Jakub Dawidek } 7682609222aSPawel Jakub Dawidek #else 769cbd92ce6SMatt Macy error = fget(td, fd, &cap_ioctl_rights, &fp); 7707008be5bSPawel Jakub Dawidek if (error != 0) { 7712609222aSPawel Jakub Dawidek fp = NULL; 7722609222aSPawel Jakub Dawidek goto out; 7732609222aSPawel Jakub Dawidek } 7742609222aSPawel Jakub Dawidek #endif 7752609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 7762609222aSPawel Jakub Dawidek error = EBADF; 7772609222aSPawel Jakub Dawidek goto out; 7782609222aSPawel Jakub Dawidek } 7792609222aSPawel Jakub Dawidek 7802609222aSPawel Jakub Dawidek switch (com) { 7812609222aSPawel Jakub Dawidek case FIONCLEX: 7822609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 7832609222aSPawel Jakub Dawidek goto out; 7842609222aSPawel Jakub Dawidek case FIOCLEX: 7852609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 786d9f46233SJohn Baldwin goto out; 787d9f46233SJohn Baldwin case FIONBIO: 788bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 789397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 790df8bae1dSRodney W. Grimes else 791397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7928ccf264fSPoul-Henning Kamp data = (void *)&tmp; 793d9f46233SJohn Baldwin break; 794d9f46233SJohn Baldwin case FIOASYNC: 795bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 796397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 797df8bae1dSRodney W. Grimes else 798397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7998ccf264fSPoul-Henning Kamp data = (void *)&tmp; 800d9f46233SJohn Baldwin break; 801df8bae1dSRodney W. Grimes } 8028ccf264fSPoul-Henning Kamp 8038ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 804d9f46233SJohn Baldwin out: 8052609222aSPawel Jakub Dawidek switch (locked) { 8062609222aSPawel Jakub Dawidek case LA_XLOCKED: 8072609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 8082609222aSPawel Jakub Dawidek break; 8092609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 8102609222aSPawel Jakub Dawidek case LA_SLOCKED: 8112609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8122609222aSPawel Jakub Dawidek break; 8132609222aSPawel Jakub Dawidek #endif 8142609222aSPawel Jakub Dawidek default: 8152609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8162609222aSPawel Jakub Dawidek break; 8172609222aSPawel Jakub Dawidek } 8182609222aSPawel Jakub Dawidek if (fp != NULL) 819b40ce416SJulian Elischer fdrop(fp, td); 820df8bae1dSRodney W. Grimes return (error); 821df8bae1dSRodney W. Grimes } 822df8bae1dSRodney W. Grimes 823125dcf8cSKonstantin Belousov int 8242856d85eSKyle Evans sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap) 8252856d85eSKyle Evans { 8262856d85eSKyle Evans int error; 8272856d85eSKyle Evans 8282856d85eSKyle Evans error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len); 8292856d85eSKyle Evans return (kern_posix_error(td, error)); 8302856d85eSKyle Evans } 8312856d85eSKyle Evans 8322856d85eSKyle Evans int 8332856d85eSKyle Evans kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len) 8342856d85eSKyle Evans { 8352856d85eSKyle Evans struct file *fp; 8362856d85eSKyle Evans int error; 8372856d85eSKyle Evans 8382856d85eSKyle Evans AUDIT_ARG_FD(fd); 8392856d85eSKyle Evans if (offset < 0 || len <= 0) 8402856d85eSKyle Evans return (EINVAL); 8412856d85eSKyle Evans /* Check for wrap. */ 8422856d85eSKyle Evans if (offset > OFF_MAX - len) 8432856d85eSKyle Evans return (EFBIG); 8442856d85eSKyle Evans AUDIT_ARG_FD(fd); 8452856d85eSKyle Evans error = fget(td, fd, &cap_pwrite_rights, &fp); 8462856d85eSKyle Evans if (error != 0) 8472856d85eSKyle Evans return (error); 8482856d85eSKyle Evans AUDIT_ARG_FILE(td->td_proc, fp); 8492856d85eSKyle Evans if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) { 8502856d85eSKyle Evans error = ESPIPE; 8512856d85eSKyle Evans goto out; 8522856d85eSKyle Evans } 8532856d85eSKyle Evans if ((fp->f_flag & FWRITE) == 0) { 8542856d85eSKyle Evans error = EBADF; 8552856d85eSKyle Evans goto out; 8562856d85eSKyle Evans } 8572856d85eSKyle Evans 8582856d85eSKyle Evans error = fo_fallocate(fp, offset, len, td); 8592856d85eSKyle Evans out: 8602856d85eSKyle Evans fdrop(fp, td); 8612856d85eSKyle Evans return (error); 8622856d85eSKyle Evans } 8632856d85eSKyle Evans 8642856d85eSKyle Evans int 865*7a202823SKonstantin Belousov kern_specialfd(struct thread *td, int type, void *arg) 866*7a202823SKonstantin Belousov { 867*7a202823SKonstantin Belousov struct file *fp; 868*7a202823SKonstantin Belousov struct specialfd_eventfd *ae; 869*7a202823SKonstantin Belousov int error, fd, fflags; 870*7a202823SKonstantin Belousov 871*7a202823SKonstantin Belousov fflags = 0; 872*7a202823SKonstantin Belousov error = falloc_noinstall(td, &fp); 873*7a202823SKonstantin Belousov if (error != 0) 874*7a202823SKonstantin Belousov return (error); 875*7a202823SKonstantin Belousov 876*7a202823SKonstantin Belousov switch (type) { 877*7a202823SKonstantin Belousov case SPECIALFD_EVENTFD: 878*7a202823SKonstantin Belousov ae = arg; 879*7a202823SKonstantin Belousov if ((ae->flags & EFD_CLOEXEC) != 0) 880*7a202823SKonstantin Belousov fflags |= O_CLOEXEC; 881*7a202823SKonstantin Belousov error = eventfd_create_file(td, fp, ae->initval, ae->flags); 882*7a202823SKonstantin Belousov break; 883*7a202823SKonstantin Belousov default: 884*7a202823SKonstantin Belousov error = EINVAL; 885*7a202823SKonstantin Belousov break; 886*7a202823SKonstantin Belousov } 887*7a202823SKonstantin Belousov 888*7a202823SKonstantin Belousov if (error == 0) 889*7a202823SKonstantin Belousov error = finstall(td, fp, &fd, fflags, NULL); 890*7a202823SKonstantin Belousov fdrop(fp, td); 891*7a202823SKonstantin Belousov if (error == 0) 892*7a202823SKonstantin Belousov td->td_retval[0] = fd; 893*7a202823SKonstantin Belousov return (error); 894*7a202823SKonstantin Belousov } 895*7a202823SKonstantin Belousov 896*7a202823SKonstantin Belousov int 897*7a202823SKonstantin Belousov sys___specialfd(struct thread *td, struct __specialfd_args *args) 898*7a202823SKonstantin Belousov { 899*7a202823SKonstantin Belousov struct specialfd_eventfd ae; 900*7a202823SKonstantin Belousov int error; 901*7a202823SKonstantin Belousov 902*7a202823SKonstantin Belousov switch (args->type) { 903*7a202823SKonstantin Belousov case SPECIALFD_EVENTFD: 904*7a202823SKonstantin Belousov if (args->len != sizeof(struct specialfd_eventfd)) { 905*7a202823SKonstantin Belousov error = EINVAL; 906*7a202823SKonstantin Belousov break; 907*7a202823SKonstantin Belousov } 908*7a202823SKonstantin Belousov error = copyin(args->req, &ae, sizeof(ae)); 909*7a202823SKonstantin Belousov if (error != 0) 910*7a202823SKonstantin Belousov break; 911*7a202823SKonstantin Belousov if ((ae.flags & ~(EFD_CLOEXEC | EFD_NONBLOCK | 912*7a202823SKonstantin Belousov EFD_SEMAPHORE)) != 0) { 913*7a202823SKonstantin Belousov error = EINVAL; 914*7a202823SKonstantin Belousov break; 915*7a202823SKonstantin Belousov } 916*7a202823SKonstantin Belousov error = kern_specialfd(td, args->type, &ae); 917*7a202823SKonstantin Belousov break; 918*7a202823SKonstantin Belousov default: 919*7a202823SKonstantin Belousov error = EINVAL; 920*7a202823SKonstantin Belousov break; 921*7a202823SKonstantin Belousov } 922*7a202823SKonstantin Belousov return (error); 923*7a202823SKonstantin Belousov } 924*7a202823SKonstantin Belousov 925*7a202823SKonstantin Belousov int 926125dcf8cSKonstantin Belousov poll_no_poll(int events) 927125dcf8cSKonstantin Belousov { 928125dcf8cSKonstantin Belousov /* 929125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 930125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 931125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 932125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 933125dcf8cSKonstantin Belousov * of specific filesystem implementations. 934125dcf8cSKonstantin Belousov */ 935125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 936125dcf8cSKonstantin Belousov return (POLLNVAL); 937125dcf8cSKonstantin Belousov 938125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 939125dcf8cSKonstantin Belousov } 940125dcf8cSKonstantin Belousov 941066d836bSKonstantin Belousov int 9428451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 943066d836bSKonstantin Belousov { 944066d836bSKonstantin Belousov struct timespec ts; 945066d836bSKonstantin Belousov struct timeval tv, *tvp; 946066d836bSKonstantin Belousov sigset_t set, *uset; 947066d836bSKonstantin Belousov int error; 948066d836bSKonstantin Belousov 949066d836bSKonstantin Belousov if (uap->ts != NULL) { 950066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 951066d836bSKonstantin Belousov if (error != 0) 952066d836bSKonstantin Belousov return (error); 953066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 954066d836bSKonstantin Belousov tvp = &tv; 955066d836bSKonstantin Belousov } else 956066d836bSKonstantin Belousov tvp = NULL; 957066d836bSKonstantin Belousov if (uap->sm != NULL) { 958066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 959066d836bSKonstantin Belousov if (error != 0) 960066d836bSKonstantin Belousov return (error); 961066d836bSKonstantin Belousov uset = &set; 962066d836bSKonstantin Belousov } else 963066d836bSKonstantin Belousov uset = NULL; 964066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 965066d836bSKonstantin Belousov uset, NFDBITS)); 966066d836bSKonstantin Belousov } 967066d836bSKonstantin Belousov 968066d836bSKonstantin Belousov int 969066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 970066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 971066d836bSKonstantin Belousov { 972066d836bSKonstantin Belousov int error; 973066d836bSKonstantin Belousov 974066d836bSKonstantin Belousov if (uset != NULL) { 975066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 976066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 977066d836bSKonstantin Belousov if (error != 0) 978066d836bSKonstantin Belousov return (error); 979066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 980066d836bSKonstantin Belousov /* 981066d836bSKonstantin Belousov * Make sure that ast() is called on return to 982066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 983066d836bSKonstantin Belousov * sigmask. 984066d836bSKonstantin Belousov */ 985066d836bSKonstantin Belousov thread_lock(td); 986066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 987066d836bSKonstantin Belousov thread_unlock(td); 988066d836bSKonstantin Belousov } 989066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 990066d836bSKonstantin Belousov return (error); 991066d836bSKonstantin Belousov } 992066d836bSKonstantin Belousov 993d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 994df8bae1dSRodney W. Grimes struct select_args { 995b08f7993SSujal Patel int nd; 996df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 997df8bae1dSRodney W. Grimes struct timeval *tv; 998df8bae1dSRodney W. Grimes }; 999d2d3e875SBruce Evans #endif 100026f9a767SRodney W. Grimes int 10018451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 1002df8bae1dSRodney W. Grimes { 10038f19eb88SIan Dowse struct timeval tv, *tvp; 10048f19eb88SIan Dowse int error; 10058f19eb88SIan Dowse 10068f19eb88SIan Dowse if (uap->tv != NULL) { 10078f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 10088f19eb88SIan Dowse if (error) 10098f19eb88SIan Dowse return (error); 10108f19eb88SIan Dowse tvp = &tv; 10118f19eb88SIan Dowse } else 10128f19eb88SIan Dowse tvp = NULL; 10138f19eb88SIan Dowse 1014b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 1015b55ef216SKonstantin Belousov NFDBITS)); 10168f19eb88SIan Dowse } 10178f19eb88SIan Dowse 101856be1b9aSKonstantin Belousov /* 101956be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 102056be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 102156be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 102256be1b9aSKonstantin Belousov * 102356be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 102456be1b9aSKonstantin Belousov * 1025d8bc2a17SMateusz Guzik * nd is fd_nfiles. 102656be1b9aSKonstantin Belousov */ 102756be1b9aSKonstantin Belousov static int 102856be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 102956be1b9aSKonstantin Belousov { 103056be1b9aSKonstantin Belousov char *addr, *oaddr; 103156be1b9aSKonstantin Belousov int b, i, res; 103256be1b9aSKonstantin Belousov uint8_t bits; 103356be1b9aSKonstantin Belousov 103456be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 103556be1b9aSKonstantin Belousov return (0); 103656be1b9aSKonstantin Belousov 103756be1b9aSKonstantin Belousov oaddr = NULL; 103856be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 103956be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 104056be1b9aSKonstantin Belousov b = i / NBBY; 104156be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 104256be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 104356be1b9aSKonstantin Belousov #else 104456be1b9aSKonstantin Belousov addr = (char *)fd_in; 104556be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 104656be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 104756be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 104856be1b9aSKonstantin Belousov } else { 104956be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 105056be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 105156be1b9aSKonstantin Belousov } 105256be1b9aSKonstantin Belousov #endif 105356be1b9aSKonstantin Belousov if (addr != oaddr) { 105456be1b9aSKonstantin Belousov res = fubyte(addr); 105556be1b9aSKonstantin Belousov if (res == -1) 105656be1b9aSKonstantin Belousov return (EFAULT); 105756be1b9aSKonstantin Belousov oaddr = addr; 105856be1b9aSKonstantin Belousov bits = res; 105956be1b9aSKonstantin Belousov } 106056be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 106156be1b9aSKonstantin Belousov return (EBADF); 106256be1b9aSKonstantin Belousov } 106356be1b9aSKonstantin Belousov return (0); 106456be1b9aSKonstantin Belousov } 106556be1b9aSKonstantin Belousov 10668f19eb88SIan Dowse int 10678f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 1068b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 10698f19eb88SIan Dowse { 1070426da3bcSAlfred Perlstein struct filedesc *fdp; 1071d5e4d7e1SBruce Evans /* 1072d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 1073d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 1074d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 1075d5e4d7e1SBruce Evans * of 256. 1076d5e4d7e1SBruce Evans */ 1077d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 1078eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 1079cf5e4fe6SDavide Italiano struct timeval rtv; 1080cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 1081b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 1082cf5e4fe6SDavide Italiano int error, lf, ndu; 1083df8bae1dSRodney W. Grimes 10848f19eb88SIan Dowse if (nd < 0) 1085acbfbfeaSSujal Patel return (EINVAL); 1086426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 108756be1b9aSKonstantin Belousov ndu = nd; 1088d8bc2a17SMateusz Guzik lf = fdp->fd_nfiles; 1089d8bc2a17SMateusz Guzik if (nd > lf) 1090d8bc2a17SMateusz Guzik nd = lf; 109156be1b9aSKonstantin Belousov 109256be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 109356be1b9aSKonstantin Belousov if (error != 0) 109456be1b9aSKonstantin Belousov return (error); 109556be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 109656be1b9aSKonstantin Belousov if (error != 0) 109756be1b9aSKonstantin Belousov return (error); 109856be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 109956be1b9aSKonstantin Belousov if (error != 0) 110056be1b9aSKonstantin Belousov return (error); 1101b08f7993SSujal Patel 1102d5e4d7e1SBruce Evans /* 1103d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 1104d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 1105d5e4d7e1SBruce Evans */ 11068f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 1107d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 1108b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 1109d5e4d7e1SBruce Evans nbufbytes = 0; 11108f19eb88SIan Dowse if (fd_in != NULL) 1111d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 11128f19eb88SIan Dowse if (fd_ou != NULL) 1113d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 11148f19eb88SIan Dowse if (fd_ex != NULL) 1115d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1116d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1117d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1118d5e4d7e1SBruce Evans else 1119a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1120b08f7993SSujal Patel 1121b08f7993SSujal Patel /* 1122d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1123d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1124d5e4d7e1SBruce Evans * together. 1125b08f7993SSujal Patel */ 1126d5e4d7e1SBruce Evans sbp = selbits; 1127df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1128d5e4d7e1SBruce Evans do { \ 1129841c0c7eSNathan Whitehorn if (name == NULL) { \ 1130d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1131841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1132841c0c7eSNathan Whitehorn } else { \ 1133d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1134d5e4d7e1SBruce Evans obits[x] = sbp; \ 1135d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1136b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1137265fc98fSSeigo Tanimura if (error != 0) \ 1138ace8398dSJeff Roberson goto done; \ 1139d6fda03aSMateusz Guzik if (ncpbytes != ncpubytes) \ 1140b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1141b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1142e04ac2feSJohn Baldwin } \ 1143d5e4d7e1SBruce Evans } while (0) 11448f19eb88SIan Dowse getbits(fd_in, 0); 11458f19eb88SIan Dowse getbits(fd_ou, 1); 11468f19eb88SIan Dowse getbits(fd_ex, 2); 1147df8bae1dSRodney W. Grimes #undef getbits 1148841c0c7eSNathan Whitehorn 1149841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1150841c0c7eSNathan Whitehorn /* 1151841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1152841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1153841c0c7eSNathan Whitehorn * generic. 1154841c0c7eSNathan Whitehorn */ 1155841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1156841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1157841c0c7eSNathan Whitehorn int i; \ 1158841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1159841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1160841c0c7eSNathan Whitehorn } 1161841c0c7eSNathan Whitehorn #else 1162841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1163841c0c7eSNathan Whitehorn #endif 1164841c0c7eSNathan Whitehorn 1165841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1166841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1167841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1168841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1169841c0c7eSNathan Whitehorn 1170d5e4d7e1SBruce Evans if (nbufbytes != 0) 1171d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1172df8bae1dSRodney W. Grimes 1173cf5e4fe6SDavide Italiano precision = 0; 11748f19eb88SIan Dowse if (tvp != NULL) { 1175cf5e4fe6SDavide Italiano rtv = *tvp; 1176cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1177cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1178df8bae1dSRodney W. Grimes error = EINVAL; 1179ace8398dSJeff Roberson goto done; 1180df8bae1dSRodney W. Grimes } 118121a37a71SAlexander Motin if (!timevalisset(&rtv)) 1182980c545dSAlexander Motin asbt = 0; 118321a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1184cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1185cf5e4fe6SDavide Italiano precision = rsbt; 1186cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1187cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1188cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 11894bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1190cf5e4fe6SDavide Italiano asbt += rsbt; 119121a37a71SAlexander Motin else 1192980c545dSAlexander Motin asbt = -1; 1193980c545dSAlexander Motin } else 1194980c545dSAlexander Motin asbt = -1; 1195cf5e4fe6SDavide Italiano } else 1196cf5e4fe6SDavide Italiano asbt = -1; 1197ace8398dSJeff Roberson seltdinit(td); 1198ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1199ace8398dSJeff Roberson for (;;) { 12008f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1201ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1202ace8398dSJeff Roberson break; 1203cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1204ace8398dSJeff Roberson if (error) 1205ace8398dSJeff Roberson break; 1206ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1207ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1208ace8398dSJeff Roberson break; 120985f190e4SAlfred Perlstein } 1210ace8398dSJeff Roberson seltdclear(td); 1211265fc98fSSeigo Tanimura 1212df8bae1dSRodney W. Grimes done: 1213df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1214df8bae1dSRodney W. Grimes if (error == ERESTART) 1215df8bae1dSRodney W. Grimes error = EINTR; 1216df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1217df8bae1dSRodney W. Grimes error = 0; 1218841c0c7eSNathan Whitehorn 1219841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1220841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1221841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1222841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1223841c0c7eSNathan Whitehorn #undef swizzle_fdset 1224841c0c7eSNathan Whitehorn 1225df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1226b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1227df8bae1dSRodney W. Grimes error = error2; 1228df8bae1dSRodney W. Grimes if (error == 0) { 1229df8bae1dSRodney W. Grimes int error2; 1230df8bae1dSRodney W. Grimes 12318f19eb88SIan Dowse putbits(fd_in, 0); 12328f19eb88SIan Dowse putbits(fd_ou, 1); 12338f19eb88SIan Dowse putbits(fd_ex, 2); 1234df8bae1dSRodney W. Grimes #undef putbits 1235df8bae1dSRodney W. Grimes } 1236d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1237d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1238ad2edad9SMatthew Dillon 1239df8bae1dSRodney W. Grimes return (error); 1240df8bae1dSRodney W. Grimes } 124111b763dfSJeff Roberson /* 124211b763dfSJeff Roberson * Convert a select bit set to poll flags. 1243748b9df6SJeff Roberson * 124411b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 124511b763dfSJeff Roberson * return this as a set bit in any set. 124611b763dfSJeff Roberson */ 124711b763dfSJeff Roberson static int select_flags[3] = { 124811b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 124911b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 125061e53a38SKonstantin Belousov POLLRDBAND | POLLERR 125111b763dfSJeff Roberson }; 125211b763dfSJeff Roberson 125311b763dfSJeff Roberson /* 125411b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 125511b763dfSJeff Roberson * bit position in the fd_mask array. 125611b763dfSJeff Roberson */ 125711b763dfSJeff Roberson static __inline int 125860b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 125911b763dfSJeff Roberson { 126011b763dfSJeff Roberson int flags; 126111b763dfSJeff Roberson int msk; 126211b763dfSJeff Roberson 126311b763dfSJeff Roberson flags = 0; 126411b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 126511b763dfSJeff Roberson if (ibits[msk] == NULL) 126611b763dfSJeff Roberson continue; 126760b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 126811b763dfSJeff Roberson continue; 126911b763dfSJeff Roberson flags |= select_flags[msk]; 127011b763dfSJeff Roberson } 127111b763dfSJeff Roberson return (flags); 127211b763dfSJeff Roberson } 127311b763dfSJeff Roberson 127411b763dfSJeff Roberson /* 127511b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 127611b763dfSJeff Roberson * input bits originally requested. 127711b763dfSJeff Roberson */ 127811b763dfSJeff Roberson static __inline int 127911b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 128011b763dfSJeff Roberson { 128111b763dfSJeff Roberson int msk; 128211b763dfSJeff Roberson int n; 128311b763dfSJeff Roberson 128411b763dfSJeff Roberson n = 0; 128511b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 128611b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 128711b763dfSJeff Roberson continue; 128811b763dfSJeff Roberson if (ibits[msk] == NULL) 128911b763dfSJeff Roberson continue; 129011b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 129111b763dfSJeff Roberson continue; 129211b763dfSJeff Roberson /* 129311b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 129411b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 129511b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 129611b763dfSJeff Roberson * call selsetbits twice as a result. 129711b763dfSJeff Roberson */ 129811b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 129911b763dfSJeff Roberson continue; 130011b763dfSJeff Roberson obits[msk][idx] |= bit; 130111b763dfSJeff Roberson n++; 130211b763dfSJeff Roberson } 130311b763dfSJeff Roberson 130411b763dfSJeff Roberson return (n); 130511b763dfSJeff Roberson } 1306df8bae1dSRodney W. Grimes 1307a9d2f8d8SRobert Watson static __inline int 1308a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1309a9d2f8d8SRobert Watson { 1310a9d2f8d8SRobert Watson 131152604ed7SMateusz Guzik return (fget_unlocked(fdp, fd, &cap_event_rights, fpp)); 1312a9d2f8d8SRobert Watson } 1313a9d2f8d8SRobert Watson 1314ace8398dSJeff Roberson /* 1315ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1316ace8398dSJeff Roberson * completion. 1317ace8398dSJeff Roberson */ 1318ace8398dSJeff Roberson static int 1319ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1320ace8398dSJeff Roberson { 132111b763dfSJeff Roberson struct filedesc *fdp; 132211b763dfSJeff Roberson struct selinfo *si; 1323ace8398dSJeff Roberson struct seltd *stp; 1324ace8398dSJeff Roberson struct selfd *sfp; 1325ace8398dSJeff Roberson struct selfd *sfn; 1326ace8398dSJeff Roberson struct file *fp; 13279cdacff1SJeff Roberson fd_mask bit; 13289cdacff1SJeff Roberson int fd, ev, n, idx; 1329a9d2f8d8SRobert Watson int error; 1330ace8398dSJeff Roberson 133111b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1332ace8398dSJeff Roberson stp = td->td_sel; 133311b763dfSJeff Roberson n = 0; 1334ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1335ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1336ace8398dSJeff Roberson si = sfp->sf_si; 1337ace8398dSJeff Roberson selfdfree(stp, sfp); 1338ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1339ace8398dSJeff Roberson if (si != NULL) 1340ace8398dSJeff Roberson continue; 1341a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1342a9d2f8d8SRobert Watson if (error) 1343a9d2f8d8SRobert Watson return (error); 134411b763dfSJeff Roberson idx = fd / NFDBITS; 13459cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 134611b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1347bf422e5fSJeff Roberson fdrop(fp, td); 134811b763dfSJeff Roberson if (ev != 0) 134911b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1350ace8398dSJeff Roberson } 1351ace8398dSJeff Roberson stp->st_flags = 0; 1352ace8398dSJeff Roberson td->td_retval[0] = n; 1353ace8398dSJeff Roberson return (0); 1354ace8398dSJeff Roberson } 1355ace8398dSJeff Roberson 1356ace8398dSJeff Roberson /* 1357ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1358ace8398dSJeff Roberson * each selinfo. 1359ace8398dSJeff Roberson */ 1360265fc98fSSeigo Tanimura static int 1361cc3c9df8SEd Maste selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd) 1362df8bae1dSRodney W. Grimes { 136311b763dfSJeff Roberson struct filedesc *fdp; 1364df8bae1dSRodney W. Grimes struct file *fp; 13659cdacff1SJeff Roberson fd_mask bit; 136611b763dfSJeff Roberson int ev, flags, end, fd; 13679cdacff1SJeff Roberson int n, idx; 1368a9d2f8d8SRobert Watson int error; 1369df8bae1dSRodney W. Grimes 137011b763dfSJeff Roberson fdp = td->td_proc->p_fd; 137111b763dfSJeff Roberson n = 0; 13729cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 137311b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 137411b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 137511b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 137611b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 137711b763dfSJeff Roberson if (flags == 0) 1378f082218cSPeter Wemm continue; 1379a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1380a9d2f8d8SRobert Watson if (error) 1381a9d2f8d8SRobert Watson return (error); 1382ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 138311b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1384bf422e5fSJeff Roberson fdrop(fp, td); 138511b763dfSJeff Roberson if (ev != 0) 138611b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1387df8bae1dSRodney W. Grimes } 1388df8bae1dSRodney W. Grimes } 138911b763dfSJeff Roberson 1390b40ce416SJulian Elischer td->td_retval[0] = n; 1391df8bae1dSRodney W. Grimes return (0); 1392df8bae1dSRodney W. Grimes } 1393df8bae1dSRodney W. Grimes 139442d11757SPeter Wemm int 1395186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap) 1396186d9c34SDmitry Chagin { 1397186d9c34SDmitry Chagin struct timespec ts, *tsp; 1398186d9c34SDmitry Chagin 1399186d9c34SDmitry Chagin if (uap->timeout != INFTIM) { 1400186d9c34SDmitry Chagin if (uap->timeout < 0) 1401186d9c34SDmitry Chagin return (EINVAL); 1402186d9c34SDmitry Chagin ts.tv_sec = uap->timeout / 1000; 1403186d9c34SDmitry Chagin ts.tv_nsec = (uap->timeout % 1000) * 1000000; 1404186d9c34SDmitry Chagin tsp = &ts; 1405186d9c34SDmitry Chagin } else 1406186d9c34SDmitry Chagin tsp = NULL; 1407186d9c34SDmitry Chagin 1408186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); 1409186d9c34SDmitry Chagin } 1410186d9c34SDmitry Chagin 1411186d9c34SDmitry Chagin int 14122384981bSConrad Meyer kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, 1413186d9c34SDmitry Chagin struct timespec *tsp, sigset_t *uset) 141442d11757SPeter Wemm { 14152384981bSConrad Meyer struct pollfd *kfds; 14162384981bSConrad Meyer struct pollfd stackfds[32]; 1417186d9c34SDmitry Chagin sbintime_t sbt, precision, tmp; 1418186d9c34SDmitry Chagin time_t over; 1419186d9c34SDmitry Chagin struct timespec ts; 1420cf5e4fe6SDavide Italiano int error; 142142d11757SPeter Wemm 1422186d9c34SDmitry Chagin precision = 0; 1423186d9c34SDmitry Chagin if (tsp != NULL) { 1424186d9c34SDmitry Chagin if (tsp->tv_sec < 0) 1425186d9c34SDmitry Chagin return (EINVAL); 1426186d9c34SDmitry Chagin if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000) 1427186d9c34SDmitry Chagin return (EINVAL); 1428186d9c34SDmitry Chagin if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1429186d9c34SDmitry Chagin sbt = 0; 1430186d9c34SDmitry Chagin else { 1431186d9c34SDmitry Chagin ts = *tsp; 1432186d9c34SDmitry Chagin if (ts.tv_sec > INT32_MAX / 2) { 1433186d9c34SDmitry Chagin over = ts.tv_sec - INT32_MAX / 2; 1434186d9c34SDmitry Chagin ts.tv_sec -= over; 1435186d9c34SDmitry Chagin } else 1436186d9c34SDmitry Chagin over = 0; 1437186d9c34SDmitry Chagin tmp = tstosbt(ts); 1438186d9c34SDmitry Chagin precision = tmp; 1439186d9c34SDmitry Chagin precision >>= tc_precexp; 1440186d9c34SDmitry Chagin if (TIMESEL(&sbt, tmp)) 1441186d9c34SDmitry Chagin sbt += tc_tick_sbt; 1442186d9c34SDmitry Chagin sbt += tmp; 1443186d9c34SDmitry Chagin } 1444186d9c34SDmitry Chagin } else 1445186d9c34SDmitry Chagin sbt = -1; 1446186d9c34SDmitry Chagin 144778c2a980SConrad Meyer /* 144878c2a980SConrad Meyer * This is kinda bogus. We have fd limits, but that is not 144978c2a980SConrad Meyer * really related to the size of the pollfd array. Make sure 145078c2a980SConrad Meyer * we let the process use at least FD_SETSIZE entries and at 145178c2a980SConrad Meyer * least enough for the system-wide limits. We want to be reasonably 145278c2a980SConrad Meyer * safe, but not overly restrictive. 145378c2a980SConrad Meyer */ 1454374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1455ace8398dSJeff Roberson return (EINVAL); 14562384981bSConrad Meyer if (nfds > nitems(stackfds)) 14572384981bSConrad Meyer kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); 145842d11757SPeter Wemm else 14592384981bSConrad Meyer kfds = stackfds; 14602384981bSConrad Meyer error = copyin(ufds, kfds, nfds * sizeof(*kfds)); 146142d11757SPeter Wemm if (error) 1462ace8398dSJeff Roberson goto done; 1463186d9c34SDmitry Chagin 1464186d9c34SDmitry Chagin if (uset != NULL) { 1465186d9c34SDmitry Chagin error = kern_sigprocmask(td, SIG_SETMASK, uset, 1466186d9c34SDmitry Chagin &td->td_oldsigmask, 0); 1467186d9c34SDmitry Chagin if (error) 1468ace8398dSJeff Roberson goto done; 1469186d9c34SDmitry Chagin td->td_pflags |= TDP_OLDMASK; 1470186d9c34SDmitry Chagin /* 1471186d9c34SDmitry Chagin * Make sure that ast() is called on return to 1472186d9c34SDmitry Chagin * usermode and TDP_OLDMASK is cleared, restoring old 1473186d9c34SDmitry Chagin * sigmask. 1474186d9c34SDmitry Chagin */ 1475186d9c34SDmitry Chagin thread_lock(td); 1476186d9c34SDmitry Chagin td->td_flags |= TDF_ASTPENDING; 1477186d9c34SDmitry Chagin thread_unlock(td); 147842d11757SPeter Wemm } 1479186d9c34SDmitry Chagin 1480ace8398dSJeff Roberson seltdinit(td); 1481ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1482ace8398dSJeff Roberson for (;;) { 14832384981bSConrad Meyer error = pollscan(td, kfds, nfds); 1484ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1485ace8398dSJeff Roberson break; 1486186d9c34SDmitry Chagin error = seltdwait(td, sbt, precision); 1487ace8398dSJeff Roberson if (error) 1488ace8398dSJeff Roberson break; 1489ace8398dSJeff Roberson error = pollrescan(td); 1490ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1491ace8398dSJeff Roberson break; 149285f190e4SAlfred Perlstein } 1493ace8398dSJeff Roberson seltdclear(td); 1494265fc98fSSeigo Tanimura 149542d11757SPeter Wemm done: 149642d11757SPeter Wemm /* poll is not restarted after signals... */ 149742d11757SPeter Wemm if (error == ERESTART) 149842d11757SPeter Wemm error = EINTR; 149942d11757SPeter Wemm if (error == EWOULDBLOCK) 150042d11757SPeter Wemm error = 0; 150142d11757SPeter Wemm if (error == 0) { 15022384981bSConrad Meyer error = pollout(td, kfds, ufds, nfds); 150342d11757SPeter Wemm if (error) 150442d11757SPeter Wemm goto out; 150542d11757SPeter Wemm } 150642d11757SPeter Wemm out: 15072384981bSConrad Meyer if (nfds > nitems(stackfds)) 15082384981bSConrad Meyer free(kfds, M_TEMP); 150942d11757SPeter Wemm return (error); 151042d11757SPeter Wemm } 151142d11757SPeter Wemm 1512186d9c34SDmitry Chagin int 1513186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap) 1514186d9c34SDmitry Chagin { 1515186d9c34SDmitry Chagin struct timespec ts, *tsp; 1516186d9c34SDmitry Chagin sigset_t set, *ssp; 1517186d9c34SDmitry Chagin int error; 1518186d9c34SDmitry Chagin 1519186d9c34SDmitry Chagin if (uap->ts != NULL) { 1520186d9c34SDmitry Chagin error = copyin(uap->ts, &ts, sizeof(ts)); 1521186d9c34SDmitry Chagin if (error) 1522186d9c34SDmitry Chagin return (error); 1523186d9c34SDmitry Chagin tsp = &ts; 1524186d9c34SDmitry Chagin } else 1525186d9c34SDmitry Chagin tsp = NULL; 1526186d9c34SDmitry Chagin if (uap->set != NULL) { 1527186d9c34SDmitry Chagin error = copyin(uap->set, &set, sizeof(set)); 1528186d9c34SDmitry Chagin if (error) 1529186d9c34SDmitry Chagin return (error); 1530186d9c34SDmitry Chagin ssp = &set; 1531186d9c34SDmitry Chagin } else 1532186d9c34SDmitry Chagin ssp = NULL; 1533186d9c34SDmitry Chagin /* 1534186d9c34SDmitry Chagin * fds is still a pointer to user space. kern_poll() will 1535186d9c34SDmitry Chagin * take care of copyin that array to the kernel space. 1536186d9c34SDmitry Chagin */ 1537186d9c34SDmitry Chagin 1538186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 1539186d9c34SDmitry Chagin } 1540186d9c34SDmitry Chagin 1541b1607c87SMateusz Guzik #ifdef CAPABILITIES 1542b1607c87SMateusz Guzik static int 1543b1607c87SMateusz Guzik poll_fget(struct filedesc *fdp, int fd, struct file **fpp) 1544b1607c87SMateusz Guzik { 1545b1607c87SMateusz Guzik const struct filedescent *fde; 1546b1607c87SMateusz Guzik const struct fdescenttbl *fdt; 1547b1607c87SMateusz Guzik const cap_rights_t *haverights; 1548b1607c87SMateusz Guzik struct file *fp; 1549b1607c87SMateusz Guzik int error; 1550b1607c87SMateusz Guzik 1551b1607c87SMateusz Guzik if (__predict_false(fd >= fdp->fd_nfiles)) 1552b1607c87SMateusz Guzik return (EBADF); 1553b1607c87SMateusz Guzik 1554b1607c87SMateusz Guzik fdt = fdp->fd_files; 1555b1607c87SMateusz Guzik fde = &fdt->fdt_ofiles[fd]; 1556b1607c87SMateusz Guzik fp = fde->fde_file; 1557b1607c87SMateusz Guzik if (__predict_false(fp == NULL)) 1558b1607c87SMateusz Guzik return (EBADF); 1559b1607c87SMateusz Guzik haverights = cap_rights_fde_inline(fde); 1560b1607c87SMateusz Guzik error = cap_check_inline(haverights, &cap_event_rights); 1561b1607c87SMateusz Guzik if (__predict_false(error != 0)) 1562b1607c87SMateusz Guzik return (EBADF); 1563b1607c87SMateusz Guzik *fpp = fp; 1564b1607c87SMateusz Guzik return (0); 1565b1607c87SMateusz Guzik } 1566b1607c87SMateusz Guzik #else 1567b1607c87SMateusz Guzik static int 1568b1607c87SMateusz Guzik poll_fget(struct filedesc *fdp, int fd, struct file **fpp) 1569b1607c87SMateusz Guzik { 1570b1607c87SMateusz Guzik struct file *fp; 1571b1607c87SMateusz Guzik 1572b1607c87SMateusz Guzik if (__predict_false(fd >= fdp->fd_nfiles)) 1573b1607c87SMateusz Guzik return (EBADF); 1574b1607c87SMateusz Guzik 1575b1607c87SMateusz Guzik fp = fdp->fd_ofiles[fd].fde_file; 1576b1607c87SMateusz Guzik if (__predict_false(fp == NULL)) 1577b1607c87SMateusz Guzik return (EBADF); 1578b1607c87SMateusz Guzik 1579b1607c87SMateusz Guzik *fpp = fp; 1580b1607c87SMateusz Guzik return (0); 1581b1607c87SMateusz Guzik } 1582b1607c87SMateusz Guzik #endif 1583b1607c87SMateusz Guzik 158442d11757SPeter Wemm static int 1585ace8398dSJeff Roberson pollrescan(struct thread *td) 1586ace8398dSJeff Roberson { 1587ace8398dSJeff Roberson struct seltd *stp; 1588ace8398dSJeff Roberson struct selfd *sfp; 1589ace8398dSJeff Roberson struct selfd *sfn; 1590ace8398dSJeff Roberson struct selinfo *si; 1591ace8398dSJeff Roberson struct filedesc *fdp; 1592ace8398dSJeff Roberson struct file *fp; 1593ace8398dSJeff Roberson struct pollfd *fd; 1594ace8398dSJeff Roberson int n; 1595ace8398dSJeff Roberson 1596ace8398dSJeff Roberson n = 0; 1597ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1598ace8398dSJeff Roberson stp = td->td_sel; 1599ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1600ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1601ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1602ace8398dSJeff Roberson si = sfp->sf_si; 1603ace8398dSJeff Roberson selfdfree(stp, sfp); 1604ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1605ace8398dSJeff Roberson if (si != NULL) 1606ace8398dSJeff Roberson continue; 1607b1607c87SMateusz Guzik if (poll_fget(fdp, fd->fd, &fp) != 0) { 1608ace8398dSJeff Roberson fd->revents = POLLNVAL; 1609ace8398dSJeff Roberson n++; 1610ace8398dSJeff Roberson continue; 1611ace8398dSJeff Roberson } 1612ace8398dSJeff Roberson /* 1613ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1614ace8398dSJeff Roberson * POLLERR if appropriate. 1615ace8398dSJeff Roberson */ 1616ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1617ace8398dSJeff Roberson if (fd->revents != 0) 1618ace8398dSJeff Roberson n++; 1619ace8398dSJeff Roberson } 1620ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1621ace8398dSJeff Roberson stp->st_flags = 0; 1622ace8398dSJeff Roberson td->td_retval[0] = n; 1623ace8398dSJeff Roberson return (0); 1624ace8398dSJeff Roberson } 1625ace8398dSJeff Roberson 1626ace8398dSJeff Roberson static int 1627cc3c9df8SEd Maste pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd) 1628ae81968fSRobert Watson { 1629ae81968fSRobert Watson int error = 0; 1630ae81968fSRobert Watson u_int i = 0; 16316d8feddaSKonstantin Belousov u_int n = 0; 1632ae81968fSRobert Watson 1633ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1634ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1635ae81968fSRobert Watson sizeof(ufds->revents)); 1636ae81968fSRobert Watson if (error) 1637ae81968fSRobert Watson return (error); 16386d8feddaSKonstantin Belousov if (fds->revents != 0) 16396d8feddaSKonstantin Belousov n++; 1640ae81968fSRobert Watson fds++; 1641ae81968fSRobert Watson ufds++; 1642ae81968fSRobert Watson } 16436d8feddaSKonstantin Belousov td->td_retval[0] = n; 1644ae81968fSRobert Watson return (0); 1645ae81968fSRobert Watson } 1646ae81968fSRobert Watson 1647ae81968fSRobert Watson static int 1648cc3c9df8SEd Maste pollscan(struct thread *td, struct pollfd *fds, u_int nfd) 164942d11757SPeter Wemm { 1650b1607c87SMateusz Guzik struct filedesc *fdp; 165142d11757SPeter Wemm struct file *fp; 1652b1607c87SMateusz Guzik int i, n; 165342d11757SPeter Wemm 1654b1607c87SMateusz Guzik n = 0; 1655b1607c87SMateusz Guzik fdp = td->td_proc->p_fd; 16565e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1657eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1658b1607c87SMateusz Guzik if (fds->fd < 0) { 1659337c9691SJordan K. Hubbard fds->revents = 0; 1660b1607c87SMateusz Guzik continue; 1661b1607c87SMateusz Guzik } 1662b1607c87SMateusz Guzik if (poll_fget(fdp, fds->fd, &fp) != 0) { 166342d11757SPeter Wemm fds->revents = POLLNVAL; 166442d11757SPeter Wemm n++; 1665b1607c87SMateusz Guzik continue; 1666b1607c87SMateusz Guzik } 16672087c896SBruce Evans /* 16682087c896SBruce Evans * Note: backend also returns POLLHUP and 16692087c896SBruce Evans * POLLERR if appropriate. 16702087c896SBruce Evans */ 1671ace8398dSJeff Roberson selfdalloc(td, fds); 167213ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1673ea6027a8SRobert Watson td->td_ucred, td); 1674f2159cc7SKonstantin Belousov /* 1675f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1676f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1677f2159cc7SKonstantin Belousov */ 1678f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1679f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1680f2159cc7SKonstantin Belousov 168142d11757SPeter Wemm if (fds->revents != 0) 168242d11757SPeter Wemm n++; 168342d11757SPeter Wemm } 16845e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1685b40ce416SJulian Elischer td->td_retval[0] = n; 168642d11757SPeter Wemm return (0); 168742d11757SPeter Wemm } 168842d11757SPeter Wemm 168942d11757SPeter Wemm /* 1690237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1691237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1692237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1693237abf0cSDavide Italiano * determine which events fired. 1694237abf0cSDavide Italiano */ 1695237abf0cSDavide Italiano int 1696237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1697237abf0cSDavide Italiano { 1698237abf0cSDavide Italiano struct timeval rtv; 1699237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1700237abf0cSDavide Italiano int error; 1701237abf0cSDavide Italiano 170276665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1703237abf0cSDavide Italiano if (tvp != NULL) { 1704237abf0cSDavide Italiano rtv = *tvp; 1705237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1706237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1707237abf0cSDavide Italiano return (EINVAL); 1708237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1709237abf0cSDavide Italiano asbt = 0; 1710237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1711237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1712237abf0cSDavide Italiano precision = rsbt; 1713237abf0cSDavide Italiano precision >>= tc_precexp; 1714237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1715237abf0cSDavide Italiano asbt += tc_tick_sbt; 17164bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1717237abf0cSDavide Italiano asbt += rsbt; 1718237abf0cSDavide Italiano else 1719237abf0cSDavide Italiano asbt = -1; 1720237abf0cSDavide Italiano } else 1721237abf0cSDavide Italiano asbt = -1; 1722237abf0cSDavide Italiano } else 1723237abf0cSDavide Italiano asbt = -1; 1724237abf0cSDavide Italiano seltdinit(td); 1725237abf0cSDavide Italiano /* 1726237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1727237abf0cSDavide Italiano */ 1728237abf0cSDavide Italiano for (;;) { 1729237abf0cSDavide Italiano selfdalloc(td, NULL); 1730237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1731237abf0cSDavide Italiano /* error here is actually the ready events. */ 1732237abf0cSDavide Italiano if (error) 1733237abf0cSDavide Italiano return (0); 1734237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1735237abf0cSDavide Italiano if (error) 1736237abf0cSDavide Italiano break; 1737237abf0cSDavide Italiano } 1738237abf0cSDavide Italiano seltdclear(td); 1739237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1740237abf0cSDavide Italiano if (error == ERESTART) 1741237abf0cSDavide Italiano error = 0; 1742237abf0cSDavide Italiano return (error); 1743237abf0cSDavide Italiano } 1744237abf0cSDavide Italiano 1745237abf0cSDavide Italiano /* 1746ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1747ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1748ace8398dSJeff Roberson */ 1749ace8398dSJeff Roberson static void 1750ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1751ace8398dSJeff Roberson { 1752ace8398dSJeff Roberson struct seltd *stp; 1753ace8398dSJeff Roberson 1754ace8398dSJeff Roberson stp = td->td_sel; 1755ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1756ea33cca9SMateusz Guzik stp->st_free1 = malloc(sizeof(*stp->st_free1), M_SELFD, M_WAITOK|M_ZERO); 1757ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1758ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1759ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1760ea33cca9SMateusz Guzik stp->st_free2 = malloc(sizeof(*stp->st_free2), M_SELFD, M_WAITOK|M_ZERO); 1761ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1762ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1763ace8398dSJeff Roberson } 1764ace8398dSJeff Roberson 1765ace8398dSJeff Roberson static void 1766ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1767ace8398dSJeff Roberson { 1768ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 176931b2ac4bSMateusz Guzik /* 177031b2ac4bSMateusz Guzik * Paired with doselwakeup. 177131b2ac4bSMateusz Guzik */ 177231b2ac4bSMateusz Guzik if (atomic_load_acq_ptr((uintptr_t *)&sfp->sf_si) != (uintptr_t)NULL) { 1773ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1774fcb5b3a4SKonstantin Belousov if (sfp->sf_si != NULL) { 1775ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1776fcb5b3a4SKonstantin Belousov } 1777ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 177873f2e5f7SMateusz Guzik } 1779ea33cca9SMateusz Guzik free(sfp, M_SELFD); 178085f190e4SAlfred Perlstein } 178185f190e4SAlfred Perlstein 17826aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ 17836aba400aSAttilio Rao void 1784cc3c9df8SEd Maste seldrain(struct selinfo *sip) 17856aba400aSAttilio Rao { 17866aba400aSAttilio Rao 17876aba400aSAttilio Rao /* 17886aba400aSAttilio Rao * This feature is already provided by doselwakeup(), thus it is 17896aba400aSAttilio Rao * enough to go for it. 17906aba400aSAttilio Rao * Eventually, the context, should take care to avoid races 17916aba400aSAttilio Rao * between thread calling select()/poll() and file descriptor 17926aba400aSAttilio Rao * detaching, but, again, the races are just the same as 17936aba400aSAttilio Rao * selwakeup(). 17946aba400aSAttilio Rao */ 17956aba400aSAttilio Rao doselwakeup(sip, -1); 17966aba400aSAttilio Rao } 17976aba400aSAttilio Rao 1798df8bae1dSRodney W. Grimes /* 1799df8bae1dSRodney W. Grimes * Record a select request. 1800df8bae1dSRodney W. Grimes */ 1801df8bae1dSRodney W. Grimes void 1802cc3c9df8SEd Maste selrecord(struct thread *selector, struct selinfo *sip) 1803df8bae1dSRodney W. Grimes { 1804ace8398dSJeff Roberson struct selfd *sfp; 1805ace8398dSJeff Roberson struct seltd *stp; 1806ace8398dSJeff Roberson struct mtx *mtxp; 1807df8bae1dSRodney W. Grimes 1808ace8398dSJeff Roberson stp = selector->td_sel; 180985f190e4SAlfred Perlstein /* 1810ace8398dSJeff Roberson * Don't record when doing a rescan. 181185f190e4SAlfred Perlstein */ 1812ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1813ace8398dSJeff Roberson return; 1814ace8398dSJeff Roberson /* 1815ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1816ace8398dSJeff Roberson */ 1817ace8398dSJeff Roberson sfp = NULL; 1818ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1819ace8398dSJeff Roberson stp->st_free1 = NULL; 1820ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1821ace8398dSJeff Roberson stp->st_free2 = NULL; 1822ace8398dSJeff Roberson else 1823ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 18242141453eSJeff Roberson mtxp = sip->si_mtx; 18252141453eSJeff Roberson if (mtxp == NULL) 18262141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1827ace8398dSJeff Roberson /* 1828ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1829ace8398dSJeff Roberson */ 1830ace8398dSJeff Roberson sfp->sf_si = sip; 1831ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1832ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1833ace8398dSJeff Roberson /* 1834ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1835ace8398dSJeff Roberson */ 1836ace8398dSJeff Roberson mtx_lock(mtxp); 1837ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1838ace8398dSJeff Roberson sip->si_mtx = mtxp; 1839ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 184085f190e4SAlfred Perlstein } 1841ace8398dSJeff Roberson /* 1842ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1843ace8398dSJeff Roberson */ 1844ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1845ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1846df8bae1dSRodney W. Grimes } 1847df8bae1dSRodney W. Grimes 1848512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1849df8bae1dSRodney W. Grimes void 1850cc3c9df8SEd Maste selwakeup(struct selinfo *sip) 1851df8bae1dSRodney W. Grimes { 1852512824f8SSeigo Tanimura doselwakeup(sip, -1); 1853512824f8SSeigo Tanimura } 1854512824f8SSeigo Tanimura 1855512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1856512824f8SSeigo Tanimura void 1857cc3c9df8SEd Maste selwakeuppri(struct selinfo *sip, int pri) 1858512824f8SSeigo Tanimura { 1859512824f8SSeigo Tanimura doselwakeup(sip, pri); 1860512824f8SSeigo Tanimura } 1861512824f8SSeigo Tanimura 1862512824f8SSeigo Tanimura /* 1863512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1864512824f8SSeigo Tanimura */ 1865512824f8SSeigo Tanimura static void 1866cc3c9df8SEd Maste doselwakeup(struct selinfo *sip, int pri) 1867512824f8SSeigo Tanimura { 1868ace8398dSJeff Roberson struct selfd *sfp; 1869ace8398dSJeff Roberson struct selfd *sfn; 1870ace8398dSJeff Roberson struct seltd *stp; 1871df8bae1dSRodney W. Grimes 1872ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1873ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1874b40ce416SJulian Elischer return; 1875ace8398dSJeff Roberson /* 1876ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1877ace8398dSJeff Roberson */ 1878ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1879ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1880ace8398dSJeff Roberson /* 1881ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1882ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1883ace8398dSJeff Roberson */ 1884ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1885ace8398dSJeff Roberson stp = sfp->sf_td; 1886ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1887ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1888ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1889ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 189010e64782SMateusz Guzik /* 189110e64782SMateusz Guzik * Paired with selfdfree. 189210e64782SMateusz Guzik * 189310e64782SMateusz Guzik * Storing this only after the wakeup provides an invariant that 189410e64782SMateusz Guzik * stp is not used after selfdfree returns. 189510e64782SMateusz Guzik */ 189610e64782SMateusz Guzik atomic_store_rel_ptr((uintptr_t *)&sfp->sf_si, (uintptr_t)NULL); 1897b40ce416SJulian Elischer } 1898ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1899ace8398dSJeff Roberson } 1900ace8398dSJeff Roberson 1901ace8398dSJeff Roberson static void 1902ace8398dSJeff Roberson seltdinit(struct thread *td) 1903ace8398dSJeff Roberson { 1904ace8398dSJeff Roberson struct seltd *stp; 1905ace8398dSJeff Roberson 190610e64782SMateusz Guzik stp = td->td_sel; 190710e64782SMateusz Guzik if (stp != NULL) { 190810e64782SMateusz Guzik MPASS(stp->st_flags == 0); 190910e64782SMateusz Guzik MPASS(STAILQ_EMPTY(&stp->st_selq)); 191010e64782SMateusz Guzik return; 191110e64782SMateusz Guzik } 191210e64782SMateusz Guzik stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1913ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1914ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1915ace8398dSJeff Roberson stp->st_flags = 0; 1916ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 191710e64782SMateusz Guzik td->td_sel = stp; 1918ace8398dSJeff Roberson } 1919ace8398dSJeff Roberson 1920ace8398dSJeff Roberson static int 1921cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision) 1922ace8398dSJeff Roberson { 1923ace8398dSJeff Roberson struct seltd *stp; 1924ace8398dSJeff Roberson int error; 1925ace8398dSJeff Roberson 1926ace8398dSJeff Roberson stp = td->td_sel; 1927ace8398dSJeff Roberson /* 1928ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1929ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1930ace8398dSJeff Roberson */ 1931ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1932ace8398dSJeff Roberson /* 1933ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1934ace8398dSJeff Roberson */ 1935ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1936ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1937ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1938ace8398dSJeff Roberson return (0); 1939ace8398dSJeff Roberson } 1940cf5e4fe6SDavide Italiano if (sbt == 0) 1941cf5e4fe6SDavide Italiano error = EWOULDBLOCK; 1942cf5e4fe6SDavide Italiano else if (sbt != -1) 1943cf5e4fe6SDavide Italiano error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx, 1944cf5e4fe6SDavide Italiano sbt, precision, C_ABSOLUTE); 1945ace8398dSJeff Roberson else 1946ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1947ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1948ace8398dSJeff Roberson 1949ace8398dSJeff Roberson return (error); 1950ace8398dSJeff Roberson } 1951ace8398dSJeff Roberson 1952ace8398dSJeff Roberson void 1953ace8398dSJeff Roberson seltdfini(struct thread *td) 1954ace8398dSJeff Roberson { 1955ace8398dSJeff Roberson struct seltd *stp; 1956ace8398dSJeff Roberson 1957ace8398dSJeff Roberson stp = td->td_sel; 1958ace8398dSJeff Roberson if (stp == NULL) 1959ace8398dSJeff Roberson return; 196010e64782SMateusz Guzik MPASS(stp->st_flags == 0); 196110e64782SMateusz Guzik MPASS(STAILQ_EMPTY(&stp->st_selq)); 1962ace8398dSJeff Roberson if (stp->st_free1) 1963ea33cca9SMateusz Guzik free(stp->st_free1, M_SELFD); 1964ace8398dSJeff Roberson if (stp->st_free2) 1965ea33cca9SMateusz Guzik free(stp->st_free2, M_SELFD); 1966ace8398dSJeff Roberson td->td_sel = NULL; 196736bce27bSKonstantin Belousov cv_destroy(&stp->st_wait); 196836bce27bSKonstantin Belousov mtx_destroy(&stp->st_mtx); 1969ace8398dSJeff Roberson free(stp, M_SELECT); 1970ace8398dSJeff Roberson } 1971ace8398dSJeff Roberson 1972ace8398dSJeff Roberson /* 1973ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1974ace8398dSJeff Roberson * polling. 1975ace8398dSJeff Roberson */ 1976ace8398dSJeff Roberson static void 1977ace8398dSJeff Roberson seltdclear(struct thread *td) 1978ace8398dSJeff Roberson { 1979ace8398dSJeff Roberson struct seltd *stp; 1980ace8398dSJeff Roberson struct selfd *sfp; 1981ace8398dSJeff Roberson struct selfd *sfn; 1982ace8398dSJeff Roberson 1983ace8398dSJeff Roberson stp = td->td_sel; 1984ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1985ace8398dSJeff Roberson selfdfree(stp, sfp); 1986ace8398dSJeff Roberson stp->st_flags = 0; 1987df8bae1dSRodney W. Grimes } 1988265fc98fSSeigo Tanimura 19894d77a549SAlfred Perlstein static void selectinit(void *); 1990ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1991265fc98fSSeigo Tanimura static void 1992ace8398dSJeff Roberson selectinit(void *dummy __unused) 1993265fc98fSSeigo Tanimura { 19942141453eSJeff Roberson 19952141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1996265fc98fSSeigo Tanimura } 19970acf5d0bSMark Johnston 19980acf5d0bSMark Johnston /* 19990acf5d0bSMark Johnston * Set up a syscall return value that follows the convention specified for 20000acf5d0bSMark Johnston * posix_* functions. 20010acf5d0bSMark Johnston */ 20020acf5d0bSMark Johnston int 20030acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error) 20040acf5d0bSMark Johnston { 20050acf5d0bSMark Johnston 20060acf5d0bSMark Johnston if (error <= 0) 20070acf5d0bSMark Johnston return (error); 20080acf5d0bSMark Johnston td->td_errno = error; 20090acf5d0bSMark Johnston td->td_pflags |= TDP_NERRNO; 20100acf5d0bSMark Johnston td->td_retval[0] = error; 20110acf5d0bSMark Johnston return (0); 20120acf5d0bSMark Johnston } 2013