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> 58df8bae1dSRodney W. Grimes #include <sys/kernel.h> 59e4650294SJohn Baldwin #include <sys/ktr.h> 60104a9b7eSAlexander Kabaev #include <sys/limits.h> 61df8bae1dSRodney W. Grimes #include <sys/malloc.h> 6242d11757SPeter Wemm #include <sys/poll.h> 6389b71647SPeter Wemm #include <sys/resourcevar.h> 640a2c3d48SGarrett Wollman #include <sys/selinfo.h> 6544f3b092SJohn Baldwin #include <sys/sleepqueue.h> 668f19eb88SIan Dowse #include <sys/syscallsubr.h> 678cb96f20SPeter Wemm #include <sys/sysctl.h> 6842d11757SPeter Wemm #include <sys/sysent.h> 699bbee259SAndrey A. Chernov #include <sys/vnode.h> 70279d7226SMatthew Dillon #include <sys/bio.h> 71279d7226SMatthew Dillon #include <sys/buf.h> 72265fc98fSSeigo Tanimura #include <sys/condvar.h> 73df8bae1dSRodney W. Grimes #ifdef KTRACE 74df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 75df8bae1dSRodney W. Grimes #endif 76df8bae1dSRodney W. Grimes 77e4650294SJohn Baldwin #include <security/audit/audit.h> 78ace8398dSJeff Roberson 7950ae6690SHans Petter Selasky /* 8050ae6690SHans Petter Selasky * The following macro defines how many bytes will be allocated from 8150ae6690SHans Petter Selasky * the stack instead of memory allocated when passing the IOCTL data 8250ae6690SHans Petter Selasky * structures from userspace and to the kernel. Some IOCTLs having 8350ae6690SHans Petter Selasky * small data structures are used very frequently and this small 8450ae6690SHans Petter Selasky * buffer on the stack gives a significant speedup improvement for 8550ae6690SHans Petter Selasky * those requests. The value of this define should be greater or equal 8650ae6690SHans Petter Selasky * to 64 bytes and should also be power of two. The data structure is 8750ae6690SHans Petter Selasky * currently hard-aligned to a 8-byte boundary on the stack. This 8850ae6690SHans Petter Selasky * should currently be sufficient for all supported platforms. 8950ae6690SHans Petter Selasky */ 9050ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_SIZE 128 /* bytes */ 9150ae6690SHans Petter Selasky #define SYS_IOCTL_SMALL_ALIGN 8 /* bytes */ 9250ae6690SHans Petter Selasky 93611fcff9SJohn Baldwin #ifdef __LP64__ 94611fcff9SJohn Baldwin static int iosize_max_clamp = 0; 958bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW, 968bb9a904SKonstantin Belousov &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX"); 97611fcff9SJohn Baldwin static int devfs_iosize_max_clamp = 1; 98bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW, 99bf3e483bSKonstantin Belousov &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices"); 100611fcff9SJohn Baldwin #endif 101bf3e483bSKonstantin Belousov 1028bb9a904SKonstantin Belousov /* 1038bb9a904SKonstantin Belousov * Assert that the return value of read(2) and write(2) syscalls fits 1048bb9a904SKonstantin Belousov * into a register. If not, an architecture will need to provide the 1058bb9a904SKonstantin Belousov * usermode wrappers to reconstruct the result. 1068bb9a904SKonstantin Belousov */ 1078bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t)); 108526d0bd5SKonstantin Belousov 109a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 110a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 111a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 11255166637SPoul-Henning Kamp 1136d8feddaSKonstantin Belousov static int pollout(struct thread *, struct pollfd *, struct pollfd *, 1146d8feddaSKonstantin Belousov u_int); 115bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 116ace8398dSJeff Roberson static int pollrescan(struct thread *); 117bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 118ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 119ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 120ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 121bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 122bcd9e0ddSJohn Baldwin off_t, int); 123bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 124bcd9e0ddSJohn Baldwin off_t, int); 125512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 126ace8398dSJeff Roberson static void seltdinit(struct thread *); 127cf5e4fe6SDavide Italiano static int seltdwait(struct thread *, sbintime_t, sbintime_t); 128ace8398dSJeff Roberson static void seltdclear(struct thread *); 129ace8398dSJeff Roberson 130ace8398dSJeff Roberson /* 131ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 132ace8398dSJeff Roberson * 133ace8398dSJeff Roberson * t - protected by st_mtx 134ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 135ace8398dSJeff Roberson */ 136ace8398dSJeff Roberson struct seltd { 137ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 138ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 139ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 140ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 141ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 142ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 143ace8398dSJeff Roberson }; 144ace8398dSJeff Roberson 145ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 146ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 147ace8398dSJeff Roberson 148ace8398dSJeff Roberson /* 149ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 150ace8398dSJeff Roberson * f - protected by sf_mtx 151ace8398dSJeff Roberson */ 152ace8398dSJeff Roberson struct selfd { 153ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 154ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 155ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 156ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 157ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 158ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 159ace8398dSJeff Roberson }; 160ace8398dSJeff Roberson 161ea33cca9SMateusz Guzik MALLOC_DEFINE(M_SELFD, "selfd", "selfd"); 1622141453eSJeff Roberson static struct mtx_pool *mtxpool_select; 1638fe387abSDmitrij Tejblum 164611fcff9SJohn Baldwin #ifdef __LP64__ 165611fcff9SJohn Baldwin size_t 166611fcff9SJohn Baldwin devfs_iosize_max(void) 167611fcff9SJohn Baldwin { 168611fcff9SJohn Baldwin 169611fcff9SJohn Baldwin return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 170611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 171611fcff9SJohn Baldwin } 172611fcff9SJohn Baldwin 173611fcff9SJohn Baldwin size_t 174611fcff9SJohn Baldwin iosize_max(void) 175611fcff9SJohn Baldwin { 176611fcff9SJohn Baldwin 177611fcff9SJohn Baldwin return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 178611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 179611fcff9SJohn Baldwin } 180611fcff9SJohn Baldwin #endif 181611fcff9SJohn Baldwin 182d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 183df8bae1dSRodney W. Grimes struct read_args { 184df8bae1dSRodney W. Grimes int fd; 185134e06feSBruce Evans void *buf; 186134e06feSBruce Evans size_t nbyte; 187df8bae1dSRodney W. Grimes }; 188d2d3e875SBruce Evans #endif 18926f9a767SRodney W. Grimes int 190cc3c9df8SEd Maste sys_read(struct thread *td, struct read_args *uap) 191df8bae1dSRodney W. Grimes { 192bcd9e0ddSJohn Baldwin struct uio auio; 193bcd9e0ddSJohn Baldwin struct iovec aiov; 194279d7226SMatthew Dillon int error; 195df8bae1dSRodney W. Grimes 196526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 197bcd9e0ddSJohn Baldwin return (EINVAL); 198bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 199bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 200bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 201bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 202bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 203bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 204bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 205279d7226SMatthew Dillon return (error); 206df8bae1dSRodney W. Grimes } 207df8bae1dSRodney W. Grimes 208df8bae1dSRodney W. Grimes /* 209bcd9e0ddSJohn Baldwin * Positioned read system call 2104160ccd9SAlan Cox */ 2114160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 2124160ccd9SAlan Cox struct pread_args { 2134160ccd9SAlan Cox int fd; 2144160ccd9SAlan Cox void *buf; 2154160ccd9SAlan Cox size_t nbyte; 2168fe387abSDmitrij Tejblum int pad; 2174160ccd9SAlan Cox off_t offset; 2184160ccd9SAlan Cox }; 2194160ccd9SAlan Cox #endif 2204160ccd9SAlan Cox int 221b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap) 222b38b22b0SEdward Tomasz Napierala { 223b38b22b0SEdward Tomasz Napierala 224b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 225b38b22b0SEdward Tomasz Napierala } 226b38b22b0SEdward Tomasz Napierala 227b38b22b0SEdward Tomasz Napierala int 228b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset) 2294160ccd9SAlan Cox { 2304160ccd9SAlan Cox struct uio auio; 2314160ccd9SAlan Cox struct iovec aiov; 232bcd9e0ddSJohn Baldwin int error; 2334160ccd9SAlan Cox 234b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 235bcd9e0ddSJohn Baldwin return (EINVAL); 236b38b22b0SEdward Tomasz Napierala aiov.iov_base = buf; 237b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 2384160ccd9SAlan Cox auio.uio_iov = &aiov; 2394160ccd9SAlan Cox auio.uio_iovcnt = 1; 240b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 2414160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 242b38b22b0SEdward Tomasz Napierala error = kern_preadv(td, fd, &auio, offset); 2434160ccd9SAlan Cox return (error); 2444160ccd9SAlan Cox } 2454160ccd9SAlan Cox 2460538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 247c2815ad5SPeter Wemm int 248b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap) 249c2815ad5SPeter Wemm { 250c2815ad5SPeter Wemm 251b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 252c2815ad5SPeter Wemm } 2530538aafcSKonstantin Belousov #endif 254c2815ad5SPeter Wemm 2554160ccd9SAlan Cox /* 256df8bae1dSRodney W. Grimes * Scatter read system call. 257df8bae1dSRodney W. Grimes */ 258d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 259df8bae1dSRodney W. Grimes struct readv_args { 2607147b19dSBruce Evans int fd; 261df8bae1dSRodney W. Grimes struct iovec *iovp; 262df8bae1dSRodney W. Grimes u_int iovcnt; 263df8bae1dSRodney W. Grimes }; 264d2d3e875SBruce Evans #endif 26526f9a767SRodney W. Grimes int 2668451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap) 267df8bae1dSRodney W. Grimes { 268b88ec951SJohn Baldwin struct uio *auio; 269b88ec951SJohn Baldwin int error; 270b88ec951SJohn Baldwin 271b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 272b88ec951SJohn Baldwin if (error) 273b88ec951SJohn Baldwin return (error); 274b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 275b88ec951SJohn Baldwin free(auio, M_IOV); 276b88ec951SJohn Baldwin return (error); 277b88ec951SJohn Baldwin } 278b88ec951SJohn Baldwin 279b88ec951SJohn Baldwin int 280b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 281b88ec951SJohn Baldwin { 282b064d43dSMatthew Dillon struct file *fp; 283bcd9e0ddSJohn Baldwin int error; 284bcd9e0ddSJohn Baldwin 285cbd92ce6SMatt Macy error = fget_read(td, fd, &cap_read_rights, &fp); 286bcd9e0ddSJohn Baldwin if (error) 287bcd9e0ddSJohn Baldwin return (error); 288bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 289bcd9e0ddSJohn Baldwin fdrop(fp, td); 290bcd9e0ddSJohn Baldwin return (error); 291bcd9e0ddSJohn Baldwin } 292bcd9e0ddSJohn Baldwin 293bcd9e0ddSJohn Baldwin /* 294bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 295bcd9e0ddSJohn Baldwin */ 296bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 297bcd9e0ddSJohn Baldwin struct preadv_args { 298bcd9e0ddSJohn Baldwin int fd; 299bcd9e0ddSJohn Baldwin struct iovec *iovp; 300bcd9e0ddSJohn Baldwin u_int iovcnt; 301bcd9e0ddSJohn Baldwin off_t offset; 302bcd9e0ddSJohn Baldwin }; 303bcd9e0ddSJohn Baldwin #endif 304bcd9e0ddSJohn Baldwin int 3058451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap) 306bcd9e0ddSJohn Baldwin { 307bcd9e0ddSJohn Baldwin struct uio *auio; 308bcd9e0ddSJohn Baldwin int error; 309bcd9e0ddSJohn Baldwin 310bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 311bcd9e0ddSJohn Baldwin if (error) 312bcd9e0ddSJohn Baldwin return (error); 313bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 314bcd9e0ddSJohn Baldwin free(auio, M_IOV); 315bcd9e0ddSJohn Baldwin return (error); 316bcd9e0ddSJohn Baldwin } 317bcd9e0ddSJohn Baldwin 318bcd9e0ddSJohn Baldwin int 319cc3c9df8SEd Maste kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset) 320bcd9e0ddSJohn Baldwin { 321bcd9e0ddSJohn Baldwin struct file *fp; 322bcd9e0ddSJohn Baldwin int error; 323bcd9e0ddSJohn Baldwin 324cbd92ce6SMatt Macy error = fget_read(td, fd, &cap_pread_rights, &fp); 325bcd9e0ddSJohn Baldwin if (error) 326bcd9e0ddSJohn Baldwin return (error); 327bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 328bcd9e0ddSJohn Baldwin error = ESPIPE; 32932a1fb0dSMahdi Mokhtari else if (offset < 0 && 33032a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 331bcd9e0ddSJohn Baldwin error = EINVAL; 332bcd9e0ddSJohn Baldwin else 333bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 334bcd9e0ddSJohn Baldwin fdrop(fp, td); 335bcd9e0ddSJohn Baldwin return (error); 336bcd9e0ddSJohn Baldwin } 337bcd9e0ddSJohn Baldwin 338bcd9e0ddSJohn Baldwin /* 339bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 340bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 341bcd9e0ddSJohn Baldwin */ 342bcd9e0ddSJohn Baldwin static int 343cc3c9df8SEd Maste dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio, 344cc3c9df8SEd Maste off_t offset, int flags) 345bcd9e0ddSJohn Baldwin { 346bcd9e0ddSJohn Baldwin ssize_t cnt; 34782641acdSAlan Cox int error; 348df8bae1dSRodney W. Grimes #ifdef KTRACE 349552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 350df8bae1dSRodney W. Grimes #endif 351df8bae1dSRodney W. Grimes 35251d1f690SRobert Watson AUDIT_ARG_FD(fd); 35351d1f690SRobert Watson 3544f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3554f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3564f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3574f8d23d6SPoul-Henning Kamp return (0); 3584f8d23d6SPoul-Henning Kamp } 359552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 360bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 361552afd9cSPoul-Henning Kamp auio->uio_td = td; 362df8bae1dSRodney W. Grimes #ifdef KTRACE 363552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 364552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 365df8bae1dSRodney W. Grimes #endif 366552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 367bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 368552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 369df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 370df8bae1dSRodney W. Grimes error = 0; 371279d7226SMatthew Dillon } 372552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 373df8bae1dSRodney W. Grimes #ifdef KTRACE 374552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 375552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 376b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes #endif 379b40ce416SJulian Elischer td->td_retval[0] = cnt; 380df8bae1dSRodney W. Grimes return (error); 381df8bae1dSRodney W. Grimes } 382df8bae1dSRodney W. Grimes 383d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 384df8bae1dSRodney W. Grimes struct write_args { 385df8bae1dSRodney W. Grimes int fd; 386134e06feSBruce Evans const void *buf; 387134e06feSBruce Evans size_t nbyte; 388df8bae1dSRodney W. Grimes }; 389d2d3e875SBruce Evans #endif 39026f9a767SRodney W. Grimes int 391cc3c9df8SEd Maste sys_write(struct thread *td, struct write_args *uap) 392df8bae1dSRodney W. Grimes { 393bcd9e0ddSJohn Baldwin struct uio auio; 394bcd9e0ddSJohn Baldwin struct iovec aiov; 395279d7226SMatthew Dillon int error; 396df8bae1dSRodney W. Grimes 397526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 398bcd9e0ddSJohn Baldwin return (EINVAL); 399bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 400bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 401bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 402bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 403bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 404bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 405bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 406279d7226SMatthew Dillon return (error); 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes /* 4100c14ff0eSRobert Watson * Positioned write system call. 4114160ccd9SAlan Cox */ 4124160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 4134160ccd9SAlan Cox struct pwrite_args { 4144160ccd9SAlan Cox int fd; 4154160ccd9SAlan Cox const void *buf; 4164160ccd9SAlan Cox size_t nbyte; 4178fe387abSDmitrij Tejblum int pad; 4184160ccd9SAlan Cox off_t offset; 4194160ccd9SAlan Cox }; 4204160ccd9SAlan Cox #endif 4214160ccd9SAlan Cox int 422b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap) 423b38b22b0SEdward Tomasz Napierala { 424b38b22b0SEdward Tomasz Napierala 425b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 426b38b22b0SEdward Tomasz Napierala } 427b38b22b0SEdward Tomasz Napierala 428b38b22b0SEdward Tomasz Napierala int 429b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte, 430b38b22b0SEdward Tomasz Napierala off_t offset) 4314160ccd9SAlan Cox { 4324160ccd9SAlan Cox struct uio auio; 4334160ccd9SAlan Cox struct iovec aiov; 434bcd9e0ddSJohn Baldwin int error; 4354160ccd9SAlan Cox 436b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 437bcd9e0ddSJohn Baldwin return (EINVAL); 438b38b22b0SEdward Tomasz Napierala aiov.iov_base = (void *)(uintptr_t)buf; 439b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 4404160ccd9SAlan Cox auio.uio_iov = &aiov; 4414160ccd9SAlan Cox auio.uio_iovcnt = 1; 442b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 4434160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 444b38b22b0SEdward Tomasz Napierala error = kern_pwritev(td, fd, &auio, offset); 4454160ccd9SAlan Cox return (error); 4464160ccd9SAlan Cox } 4474160ccd9SAlan Cox 4480538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 449c2815ad5SPeter Wemm int 450b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap) 451c2815ad5SPeter Wemm { 452c2815ad5SPeter Wemm 453b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 454c2815ad5SPeter Wemm } 4550538aafcSKonstantin Belousov #endif 456c2815ad5SPeter Wemm 4574160ccd9SAlan Cox /* 4580c14ff0eSRobert Watson * Gather write system call. 459df8bae1dSRodney W. Grimes */ 460d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 461df8bae1dSRodney W. Grimes struct writev_args { 462df8bae1dSRodney W. Grimes int fd; 463df8bae1dSRodney W. Grimes struct iovec *iovp; 464df8bae1dSRodney W. Grimes u_int iovcnt; 465df8bae1dSRodney W. Grimes }; 466d2d3e875SBruce Evans #endif 46726f9a767SRodney W. Grimes int 4688451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap) 469df8bae1dSRodney W. Grimes { 470b88ec951SJohn Baldwin struct uio *auio; 471b88ec951SJohn Baldwin int error; 472b88ec951SJohn Baldwin 473b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 474b88ec951SJohn Baldwin if (error) 475b88ec951SJohn Baldwin return (error); 476b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 477b88ec951SJohn Baldwin free(auio, M_IOV); 478b88ec951SJohn Baldwin return (error); 479b88ec951SJohn Baldwin } 480b88ec951SJohn Baldwin 481b88ec951SJohn Baldwin int 482b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 483b88ec951SJohn Baldwin { 484b064d43dSMatthew Dillon struct file *fp; 485bcd9e0ddSJohn Baldwin int error; 486bcd9e0ddSJohn Baldwin 487cbd92ce6SMatt Macy error = fget_write(td, fd, &cap_write_rights, &fp); 488bcd9e0ddSJohn Baldwin if (error) 489af56abaaSJohn Baldwin return (error); 490bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 491bcd9e0ddSJohn Baldwin fdrop(fp, td); 492bcd9e0ddSJohn Baldwin return (error); 493bcd9e0ddSJohn Baldwin } 494bcd9e0ddSJohn Baldwin 495bcd9e0ddSJohn Baldwin /* 4960c14ff0eSRobert Watson * Gather positioned write system call. 497bcd9e0ddSJohn Baldwin */ 498bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 499bcd9e0ddSJohn Baldwin struct pwritev_args { 500bcd9e0ddSJohn Baldwin int fd; 501bcd9e0ddSJohn Baldwin struct iovec *iovp; 502bcd9e0ddSJohn Baldwin u_int iovcnt; 503bcd9e0ddSJohn Baldwin off_t offset; 504bcd9e0ddSJohn Baldwin }; 505bcd9e0ddSJohn Baldwin #endif 506bcd9e0ddSJohn Baldwin int 5078451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap) 508bcd9e0ddSJohn Baldwin { 509bcd9e0ddSJohn Baldwin struct uio *auio; 510bcd9e0ddSJohn Baldwin int error; 511bcd9e0ddSJohn Baldwin 512bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 513bcd9e0ddSJohn Baldwin if (error) 514bcd9e0ddSJohn Baldwin return (error); 515bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 516bcd9e0ddSJohn Baldwin free(auio, M_IOV); 517bcd9e0ddSJohn Baldwin return (error); 518bcd9e0ddSJohn Baldwin } 519bcd9e0ddSJohn Baldwin 520bcd9e0ddSJohn Baldwin int 521d5cdcc3aSAndrew Gallatin kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset) 522bcd9e0ddSJohn Baldwin { 523bcd9e0ddSJohn Baldwin struct file *fp; 524bcd9e0ddSJohn Baldwin int error; 525bcd9e0ddSJohn Baldwin 526cbd92ce6SMatt Macy error = fget_write(td, fd, &cap_pwrite_rights, &fp); 527bcd9e0ddSJohn Baldwin if (error) 528af56abaaSJohn Baldwin return (error); 529bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 530bcd9e0ddSJohn Baldwin error = ESPIPE; 53132a1fb0dSMahdi Mokhtari else if (offset < 0 && 53232a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 533bcd9e0ddSJohn Baldwin error = EINVAL; 534bcd9e0ddSJohn Baldwin else 535bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 536bcd9e0ddSJohn Baldwin fdrop(fp, td); 537bcd9e0ddSJohn Baldwin return (error); 538bcd9e0ddSJohn Baldwin } 539bcd9e0ddSJohn Baldwin 540bcd9e0ddSJohn Baldwin /* 541bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 542bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 543bcd9e0ddSJohn Baldwin */ 544bcd9e0ddSJohn Baldwin static int 545cc3c9df8SEd Maste dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio, 546cc3c9df8SEd Maste off_t offset, int flags) 547bcd9e0ddSJohn Baldwin { 548bcd9e0ddSJohn Baldwin ssize_t cnt; 549552afd9cSPoul-Henning Kamp int error; 550df8bae1dSRodney W. Grimes #ifdef KTRACE 551552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 552df8bae1dSRodney W. Grimes #endif 553df8bae1dSRodney W. Grimes 55451d1f690SRobert Watson AUDIT_ARG_FD(fd); 555552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 556552afd9cSPoul-Henning Kamp auio->uio_td = td; 557bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 558df8bae1dSRodney W. Grimes #ifdef KTRACE 559552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 560552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 561df8bae1dSRodney W. Grimes #endif 562552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 563bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 564552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 565df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 566df8bae1dSRodney W. Grimes error = 0; 567bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5686f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 569b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 5707a6f3d78SJohn Baldwin tdsignal(td, SIGPIPE); 571b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 57219eb87d2SJohn Baldwin } 573df8bae1dSRodney W. Grimes } 574552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 575df8bae1dSRodney W. Grimes #ifdef KTRACE 576552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 577552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 578b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 579df8bae1dSRodney W. Grimes } 580df8bae1dSRodney W. Grimes #endif 581b40ce416SJulian Elischer td->td_retval[0] = cnt; 582df8bae1dSRodney W. Grimes return (error); 583df8bae1dSRodney W. Grimes } 584df8bae1dSRodney W. Grimes 585e4650294SJohn Baldwin /* 586e4650294SJohn Baldwin * Truncate a file given a file descriptor. 587e4650294SJohn Baldwin * 588e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 589e4650294SJohn Baldwin * descriptor isn't writable. 590e4650294SJohn Baldwin */ 591e4650294SJohn Baldwin int 592cc3c9df8SEd Maste kern_ftruncate(struct thread *td, int fd, off_t length) 593e4650294SJohn Baldwin { 594e4650294SJohn Baldwin struct file *fp; 595e4650294SJohn Baldwin int error; 596e4650294SJohn Baldwin 59714961ba7SRobert Watson AUDIT_ARG_FD(fd); 598e4650294SJohn Baldwin if (length < 0) 599e4650294SJohn Baldwin return (EINVAL); 600cbd92ce6SMatt Macy error = fget(td, fd, &cap_ftruncate_rights, &fp); 601e4650294SJohn Baldwin if (error) 602e4650294SJohn Baldwin return (error); 60314961ba7SRobert Watson AUDIT_ARG_FILE(td->td_proc, fp); 604e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 605e4650294SJohn Baldwin fdrop(fp, td); 606e4650294SJohn Baldwin return (EINVAL); 607e4650294SJohn Baldwin } 608e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 609e4650294SJohn Baldwin fdrop(fp, td); 610e4650294SJohn Baldwin return (error); 611e4650294SJohn Baldwin } 612e4650294SJohn Baldwin 613e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 614e4650294SJohn Baldwin struct ftruncate_args { 615e4650294SJohn Baldwin int fd; 616e4650294SJohn Baldwin int pad; 617e4650294SJohn Baldwin off_t length; 618e4650294SJohn Baldwin }; 619e4650294SJohn Baldwin #endif 620e4650294SJohn Baldwin int 621cc3c9df8SEd Maste sys_ftruncate(struct thread *td, struct ftruncate_args *uap) 622e4650294SJohn Baldwin { 623e4650294SJohn Baldwin 624e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 625e4650294SJohn Baldwin } 626e4650294SJohn Baldwin 627e4650294SJohn Baldwin #if defined(COMPAT_43) 628e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 629e4650294SJohn Baldwin struct oftruncate_args { 630e4650294SJohn Baldwin int fd; 631e4650294SJohn Baldwin long length; 632e4650294SJohn Baldwin }; 633e4650294SJohn Baldwin #endif 634e4650294SJohn Baldwin int 635cc3c9df8SEd Maste oftruncate(struct thread *td, struct oftruncate_args *uap) 636e4650294SJohn Baldwin { 637e4650294SJohn Baldwin 638e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 639e4650294SJohn Baldwin } 640e4650294SJohn Baldwin #endif /* COMPAT_43 */ 641e4650294SJohn Baldwin 642d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 643df8bae1dSRodney W. Grimes struct ioctl_args { 644df8bae1dSRodney W. Grimes int fd; 645069e9bc1SDoug Rabson u_long com; 646df8bae1dSRodney W. Grimes caddr_t data; 647df8bae1dSRodney W. Grimes }; 648d2d3e875SBruce Evans #endif 649df8bae1dSRodney W. Grimes /* ARGSUSED */ 65026f9a767SRodney W. Grimes int 6518451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap) 652df8bae1dSRodney W. Grimes { 65350ae6690SHans Petter Selasky u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN); 654a90fb6cfSHans Petter Selasky uint32_t com; 6559fddcc66SRuslan Ermilov int arg, error; 6563e15c66fSPoul-Henning Kamp u_int size; 6579fddcc66SRuslan Ermilov caddr_t data; 658df8bae1dSRodney W. Grimes 659a90fb6cfSHans Petter Selasky #ifdef INVARIANTS 6609fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6619fc6aa06SPoul-Henning Kamp printf( 6629fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 663431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6649fc6aa06SPoul-Henning Kamp } 665a90fb6cfSHans Petter Selasky #endif 666a90fb6cfSHans Petter Selasky com = (uint32_t)uap->com; 667df8bae1dSRodney W. Grimes 668df8bae1dSRodney W. Grimes /* 669df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 670df8bae1dSRodney W. Grimes * copied to/from the user's address space. 671df8bae1dSRodney W. Grimes */ 672df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 673ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 674ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6752de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6762de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6772de92a38SPeter Wemm #else 6782de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6792de92a38SPeter Wemm #endif 6809fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 681426da3bcSAlfred Perlstein return (ENOTTY); 682279d7226SMatthew Dillon 683ca51b19bSPoul-Henning Kamp if (size > 0) { 684715457f6SDavid E. O'Brien if (com & IOC_VOID) { 6859fddcc66SRuslan Ermilov /* Integer argument. */ 6869fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6879fddcc66SRuslan Ermilov data = (void *)&arg; 6889fddcc66SRuslan Ermilov size = 0; 6895cbf44bfSMateusz Guzik } else { 6900ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 691715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 6920ecd606bSHans Petter Selasky else 6930ecd606bSHans Petter Selasky data = smalldata; 6945cbf44bfSMateusz Guzik } 6959fddcc66SRuslan Ermilov } else 6969fddcc66SRuslan Ermilov data = (void *)&uap->data; 697df8bae1dSRodney W. Grimes if (com & IOC_IN) { 698df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 6995cbf44bfSMateusz Guzik if (error != 0) 7005cbf44bfSMateusz Guzik goto out; 701ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 702df8bae1dSRodney W. Grimes /* 703df8bae1dSRodney W. Grimes * Zero the buffer so the user always 704df8bae1dSRodney W. Grimes * gets back something deterministic. 705df8bae1dSRodney W. Grimes */ 706df8bae1dSRodney W. Grimes bzero(data, size); 707279d7226SMatthew Dillon } 708df8bae1dSRodney W. Grimes 709d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 710d9f46233SJohn Baldwin 711d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 712d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 713d9f46233SJohn Baldwin 7145cbf44bfSMateusz Guzik out: 7150ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 7169fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 717d9f46233SJohn Baldwin return (error); 718d9f46233SJohn Baldwin } 719d9f46233SJohn Baldwin 720d9f46233SJohn Baldwin int 721d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 722d9f46233SJohn Baldwin { 723d9f46233SJohn Baldwin struct file *fp; 724d9f46233SJohn Baldwin struct filedesc *fdp; 7252609222aSPawel Jakub Dawidek int error, tmp, locked; 726d9f46233SJohn Baldwin 72764c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 72864c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7292609222aSPawel Jakub Dawidek 730d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7312609222aSPawel Jakub Dawidek 732d9f46233SJohn Baldwin switch (com) { 733d9f46233SJohn Baldwin case FIONCLEX: 734d9f46233SJohn Baldwin case FIOCLEX: 7355e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7362609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7372609222aSPawel Jakub Dawidek break; 7382609222aSPawel Jakub Dawidek default: 7392609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7402609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7412609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7422609222aSPawel Jakub Dawidek #else 7432609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7442609222aSPawel Jakub Dawidek #endif 7452609222aSPawel Jakub Dawidek break; 7462609222aSPawel Jakub Dawidek } 7472609222aSPawel Jakub Dawidek 7482609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7492609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7502609222aSPawel Jakub Dawidek error = EBADF; 7512609222aSPawel Jakub Dawidek goto out; 7522609222aSPawel Jakub Dawidek } 7532609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7542609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7552609222aSPawel Jakub Dawidek goto out; 7562609222aSPawel Jakub Dawidek } 757f1cf2b9dSKonstantin Belousov if (!fhold(fp)) { 758f1cf2b9dSKonstantin Belousov error = EBADF; 759f1cf2b9dSKonstantin Belousov fp = NULL; 760f1cf2b9dSKonstantin Belousov goto out; 761f1cf2b9dSKonstantin Belousov } 7622609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 7632609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 7642609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7652609222aSPawel Jakub Dawidek } 7662609222aSPawel Jakub Dawidek #else 767cbd92ce6SMatt Macy error = fget(td, fd, &cap_ioctl_rights, &fp); 7687008be5bSPawel Jakub Dawidek if (error != 0) { 7692609222aSPawel Jakub Dawidek fp = NULL; 7702609222aSPawel Jakub Dawidek goto out; 7712609222aSPawel Jakub Dawidek } 7722609222aSPawel Jakub Dawidek #endif 7732609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 7742609222aSPawel Jakub Dawidek error = EBADF; 7752609222aSPawel Jakub Dawidek goto out; 7762609222aSPawel Jakub Dawidek } 7772609222aSPawel Jakub Dawidek 7782609222aSPawel Jakub Dawidek switch (com) { 7792609222aSPawel Jakub Dawidek case FIONCLEX: 7802609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 7812609222aSPawel Jakub Dawidek goto out; 7822609222aSPawel Jakub Dawidek case FIOCLEX: 7832609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 784d9f46233SJohn Baldwin goto out; 785d9f46233SJohn Baldwin case FIONBIO: 786bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 787397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 788df8bae1dSRodney W. Grimes else 789397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7908ccf264fSPoul-Henning Kamp data = (void *)&tmp; 791d9f46233SJohn Baldwin break; 792d9f46233SJohn Baldwin case FIOASYNC: 793bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 794397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 795df8bae1dSRodney W. Grimes else 796397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7978ccf264fSPoul-Henning Kamp data = (void *)&tmp; 798d9f46233SJohn Baldwin break; 799df8bae1dSRodney W. Grimes } 8008ccf264fSPoul-Henning Kamp 8018ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 802d9f46233SJohn Baldwin out: 8032609222aSPawel Jakub Dawidek switch (locked) { 8042609222aSPawel Jakub Dawidek case LA_XLOCKED: 8052609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 8062609222aSPawel Jakub Dawidek break; 8072609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 8082609222aSPawel Jakub Dawidek case LA_SLOCKED: 8092609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8102609222aSPawel Jakub Dawidek break; 8112609222aSPawel Jakub Dawidek #endif 8122609222aSPawel Jakub Dawidek default: 8132609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8142609222aSPawel Jakub Dawidek break; 8152609222aSPawel Jakub Dawidek } 8162609222aSPawel Jakub Dawidek if (fp != NULL) 817b40ce416SJulian Elischer fdrop(fp, td); 818df8bae1dSRodney W. Grimes return (error); 819df8bae1dSRodney W. Grimes } 820df8bae1dSRodney W. Grimes 821125dcf8cSKonstantin Belousov int 8222856d85eSKyle Evans sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap) 8232856d85eSKyle Evans { 8242856d85eSKyle Evans int error; 8252856d85eSKyle Evans 8262856d85eSKyle Evans error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len); 8272856d85eSKyle Evans return (kern_posix_error(td, error)); 8282856d85eSKyle Evans } 8292856d85eSKyle Evans 8302856d85eSKyle Evans int 8312856d85eSKyle Evans kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len) 8322856d85eSKyle Evans { 8332856d85eSKyle Evans struct file *fp; 8342856d85eSKyle Evans int error; 8352856d85eSKyle Evans 8362856d85eSKyle Evans AUDIT_ARG_FD(fd); 8372856d85eSKyle Evans if (offset < 0 || len <= 0) 8382856d85eSKyle Evans return (EINVAL); 8392856d85eSKyle Evans /* Check for wrap. */ 8402856d85eSKyle Evans if (offset > OFF_MAX - len) 8412856d85eSKyle Evans return (EFBIG); 8422856d85eSKyle Evans AUDIT_ARG_FD(fd); 8432856d85eSKyle Evans error = fget(td, fd, &cap_pwrite_rights, &fp); 8442856d85eSKyle Evans if (error != 0) 8452856d85eSKyle Evans return (error); 8462856d85eSKyle Evans AUDIT_ARG_FILE(td->td_proc, fp); 8472856d85eSKyle Evans if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) { 8482856d85eSKyle Evans error = ESPIPE; 8492856d85eSKyle Evans goto out; 8502856d85eSKyle Evans } 8512856d85eSKyle Evans if ((fp->f_flag & FWRITE) == 0) { 8522856d85eSKyle Evans error = EBADF; 8532856d85eSKyle Evans goto out; 8542856d85eSKyle Evans } 8552856d85eSKyle Evans 8562856d85eSKyle Evans error = fo_fallocate(fp, offset, len, td); 8572856d85eSKyle Evans out: 8582856d85eSKyle Evans fdrop(fp, td); 8592856d85eSKyle Evans return (error); 8602856d85eSKyle Evans } 8612856d85eSKyle Evans 8622856d85eSKyle Evans int 863125dcf8cSKonstantin Belousov poll_no_poll(int events) 864125dcf8cSKonstantin Belousov { 865125dcf8cSKonstantin Belousov /* 866125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 867125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 868125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 869125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 870125dcf8cSKonstantin Belousov * of specific filesystem implementations. 871125dcf8cSKonstantin Belousov */ 872125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 873125dcf8cSKonstantin Belousov return (POLLNVAL); 874125dcf8cSKonstantin Belousov 875125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 876125dcf8cSKonstantin Belousov } 877125dcf8cSKonstantin Belousov 878066d836bSKonstantin Belousov int 8798451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 880066d836bSKonstantin Belousov { 881066d836bSKonstantin Belousov struct timespec ts; 882066d836bSKonstantin Belousov struct timeval tv, *tvp; 883066d836bSKonstantin Belousov sigset_t set, *uset; 884066d836bSKonstantin Belousov int error; 885066d836bSKonstantin Belousov 886066d836bSKonstantin Belousov if (uap->ts != NULL) { 887066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 888066d836bSKonstantin Belousov if (error != 0) 889066d836bSKonstantin Belousov return (error); 890066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 891066d836bSKonstantin Belousov tvp = &tv; 892066d836bSKonstantin Belousov } else 893066d836bSKonstantin Belousov tvp = NULL; 894066d836bSKonstantin Belousov if (uap->sm != NULL) { 895066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 896066d836bSKonstantin Belousov if (error != 0) 897066d836bSKonstantin Belousov return (error); 898066d836bSKonstantin Belousov uset = &set; 899066d836bSKonstantin Belousov } else 900066d836bSKonstantin Belousov uset = NULL; 901066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 902066d836bSKonstantin Belousov uset, NFDBITS)); 903066d836bSKonstantin Belousov } 904066d836bSKonstantin Belousov 905066d836bSKonstantin Belousov int 906066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 907066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 908066d836bSKonstantin Belousov { 909066d836bSKonstantin Belousov int error; 910066d836bSKonstantin Belousov 911066d836bSKonstantin Belousov if (uset != NULL) { 912066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 913066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 914066d836bSKonstantin Belousov if (error != 0) 915066d836bSKonstantin Belousov return (error); 916066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 917066d836bSKonstantin Belousov /* 918066d836bSKonstantin Belousov * Make sure that ast() is called on return to 919066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 920066d836bSKonstantin Belousov * sigmask. 921066d836bSKonstantin Belousov */ 922066d836bSKonstantin Belousov thread_lock(td); 923066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 924066d836bSKonstantin Belousov thread_unlock(td); 925066d836bSKonstantin Belousov } 926066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 927066d836bSKonstantin Belousov return (error); 928066d836bSKonstantin Belousov } 929066d836bSKonstantin Belousov 930d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 931df8bae1dSRodney W. Grimes struct select_args { 932b08f7993SSujal Patel int nd; 933df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 934df8bae1dSRodney W. Grimes struct timeval *tv; 935df8bae1dSRodney W. Grimes }; 936d2d3e875SBruce Evans #endif 93726f9a767SRodney W. Grimes int 9388451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 939df8bae1dSRodney W. Grimes { 9408f19eb88SIan Dowse struct timeval tv, *tvp; 9418f19eb88SIan Dowse int error; 9428f19eb88SIan Dowse 9438f19eb88SIan Dowse if (uap->tv != NULL) { 9448f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 9458f19eb88SIan Dowse if (error) 9468f19eb88SIan Dowse return (error); 9478f19eb88SIan Dowse tvp = &tv; 9488f19eb88SIan Dowse } else 9498f19eb88SIan Dowse tvp = NULL; 9508f19eb88SIan Dowse 951b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 952b55ef216SKonstantin Belousov NFDBITS)); 9538f19eb88SIan Dowse } 9548f19eb88SIan Dowse 95556be1b9aSKonstantin Belousov /* 95656be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 95756be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 95856be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 95956be1b9aSKonstantin Belousov * 96056be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 96156be1b9aSKonstantin Belousov * 962d8bc2a17SMateusz Guzik * nd is fd_nfiles. 96356be1b9aSKonstantin Belousov */ 96456be1b9aSKonstantin Belousov static int 96556be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 96656be1b9aSKonstantin Belousov { 96756be1b9aSKonstantin Belousov char *addr, *oaddr; 96856be1b9aSKonstantin Belousov int b, i, res; 96956be1b9aSKonstantin Belousov uint8_t bits; 97056be1b9aSKonstantin Belousov 97156be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 97256be1b9aSKonstantin Belousov return (0); 97356be1b9aSKonstantin Belousov 97456be1b9aSKonstantin Belousov oaddr = NULL; 97556be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 97656be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 97756be1b9aSKonstantin Belousov b = i / NBBY; 97856be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 97956be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 98056be1b9aSKonstantin Belousov #else 98156be1b9aSKonstantin Belousov addr = (char *)fd_in; 98256be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 98356be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 98456be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 98556be1b9aSKonstantin Belousov } else { 98656be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 98756be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 98856be1b9aSKonstantin Belousov } 98956be1b9aSKonstantin Belousov #endif 99056be1b9aSKonstantin Belousov if (addr != oaddr) { 99156be1b9aSKonstantin Belousov res = fubyte(addr); 99256be1b9aSKonstantin Belousov if (res == -1) 99356be1b9aSKonstantin Belousov return (EFAULT); 99456be1b9aSKonstantin Belousov oaddr = addr; 99556be1b9aSKonstantin Belousov bits = res; 99656be1b9aSKonstantin Belousov } 99756be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 99856be1b9aSKonstantin Belousov return (EBADF); 99956be1b9aSKonstantin Belousov } 100056be1b9aSKonstantin Belousov return (0); 100156be1b9aSKonstantin Belousov } 100256be1b9aSKonstantin Belousov 10038f19eb88SIan Dowse int 10048f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 1005b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 10068f19eb88SIan Dowse { 1007426da3bcSAlfred Perlstein struct filedesc *fdp; 1008d5e4d7e1SBruce Evans /* 1009d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 1010d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 1011d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 1012d5e4d7e1SBruce Evans * of 256. 1013d5e4d7e1SBruce Evans */ 1014d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 1015eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 1016cf5e4fe6SDavide Italiano struct timeval rtv; 1017cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 1018b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 1019cf5e4fe6SDavide Italiano int error, lf, ndu; 1020df8bae1dSRodney W. Grimes 10218f19eb88SIan Dowse if (nd < 0) 1022acbfbfeaSSujal Patel return (EINVAL); 1023426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 102456be1b9aSKonstantin Belousov ndu = nd; 1025d8bc2a17SMateusz Guzik lf = fdp->fd_nfiles; 1026d8bc2a17SMateusz Guzik if (nd > lf) 1027d8bc2a17SMateusz Guzik nd = lf; 102856be1b9aSKonstantin Belousov 102956be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 103056be1b9aSKonstantin Belousov if (error != 0) 103156be1b9aSKonstantin Belousov return (error); 103256be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 103356be1b9aSKonstantin Belousov if (error != 0) 103456be1b9aSKonstantin Belousov return (error); 103556be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 103656be1b9aSKonstantin Belousov if (error != 0) 103756be1b9aSKonstantin Belousov return (error); 1038b08f7993SSujal Patel 1039d5e4d7e1SBruce Evans /* 1040d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 1041d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 1042d5e4d7e1SBruce Evans */ 10438f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 1044d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 1045b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 1046d5e4d7e1SBruce Evans nbufbytes = 0; 10478f19eb88SIan Dowse if (fd_in != NULL) 1048d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10498f19eb88SIan Dowse if (fd_ou != NULL) 1050d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10518f19eb88SIan Dowse if (fd_ex != NULL) 1052d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1053d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1054d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1055d5e4d7e1SBruce Evans else 1056a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1057b08f7993SSujal Patel 1058b08f7993SSujal Patel /* 1059d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1060d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1061d5e4d7e1SBruce Evans * together. 1062b08f7993SSujal Patel */ 1063d5e4d7e1SBruce Evans sbp = selbits; 1064df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1065d5e4d7e1SBruce Evans do { \ 1066841c0c7eSNathan Whitehorn if (name == NULL) { \ 1067d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1068841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1069841c0c7eSNathan Whitehorn } else { \ 1070d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1071d5e4d7e1SBruce Evans obits[x] = sbp; \ 1072d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1073b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1074265fc98fSSeigo Tanimura if (error != 0) \ 1075ace8398dSJeff Roberson goto done; \ 1076d6fda03aSMateusz Guzik if (ncpbytes != ncpubytes) \ 1077b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1078b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1079e04ac2feSJohn Baldwin } \ 1080d5e4d7e1SBruce Evans } while (0) 10818f19eb88SIan Dowse getbits(fd_in, 0); 10828f19eb88SIan Dowse getbits(fd_ou, 1); 10838f19eb88SIan Dowse getbits(fd_ex, 2); 1084df8bae1dSRodney W. Grimes #undef getbits 1085841c0c7eSNathan Whitehorn 1086841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1087841c0c7eSNathan Whitehorn /* 1088841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1089841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1090841c0c7eSNathan Whitehorn * generic. 1091841c0c7eSNathan Whitehorn */ 1092841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1093841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1094841c0c7eSNathan Whitehorn int i; \ 1095841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1096841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1097841c0c7eSNathan Whitehorn } 1098841c0c7eSNathan Whitehorn #else 1099841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1100841c0c7eSNathan Whitehorn #endif 1101841c0c7eSNathan Whitehorn 1102841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1103841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1104841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1105841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1106841c0c7eSNathan Whitehorn 1107d5e4d7e1SBruce Evans if (nbufbytes != 0) 1108d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1109df8bae1dSRodney W. Grimes 1110cf5e4fe6SDavide Italiano precision = 0; 11118f19eb88SIan Dowse if (tvp != NULL) { 1112cf5e4fe6SDavide Italiano rtv = *tvp; 1113cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1114cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1115df8bae1dSRodney W. Grimes error = EINVAL; 1116ace8398dSJeff Roberson goto done; 1117df8bae1dSRodney W. Grimes } 111821a37a71SAlexander Motin if (!timevalisset(&rtv)) 1119980c545dSAlexander Motin asbt = 0; 112021a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1121cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1122cf5e4fe6SDavide Italiano precision = rsbt; 1123cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1124cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1125cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 11264bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1127cf5e4fe6SDavide Italiano asbt += rsbt; 112821a37a71SAlexander Motin else 1129980c545dSAlexander Motin asbt = -1; 1130980c545dSAlexander Motin } else 1131980c545dSAlexander Motin asbt = -1; 1132cf5e4fe6SDavide Italiano } else 1133cf5e4fe6SDavide Italiano asbt = -1; 1134ace8398dSJeff Roberson seltdinit(td); 1135ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1136ace8398dSJeff Roberson for (;;) { 11378f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1138ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1139ace8398dSJeff Roberson break; 1140cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1141ace8398dSJeff Roberson if (error) 1142ace8398dSJeff Roberson break; 1143ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1144ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1145ace8398dSJeff Roberson break; 114685f190e4SAlfred Perlstein } 1147ace8398dSJeff Roberson seltdclear(td); 1148265fc98fSSeigo Tanimura 1149df8bae1dSRodney W. Grimes done: 1150df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1151df8bae1dSRodney W. Grimes if (error == ERESTART) 1152df8bae1dSRodney W. Grimes error = EINTR; 1153df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1154df8bae1dSRodney W. Grimes error = 0; 1155841c0c7eSNathan Whitehorn 1156841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1157841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1158841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1159841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1160841c0c7eSNathan Whitehorn #undef swizzle_fdset 1161841c0c7eSNathan Whitehorn 1162df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1163b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1164df8bae1dSRodney W. Grimes error = error2; 1165df8bae1dSRodney W. Grimes if (error == 0) { 1166df8bae1dSRodney W. Grimes int error2; 1167df8bae1dSRodney W. Grimes 11688f19eb88SIan Dowse putbits(fd_in, 0); 11698f19eb88SIan Dowse putbits(fd_ou, 1); 11708f19eb88SIan Dowse putbits(fd_ex, 2); 1171df8bae1dSRodney W. Grimes #undef putbits 1172df8bae1dSRodney W. Grimes } 1173d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1174d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1175ad2edad9SMatthew Dillon 1176df8bae1dSRodney W. Grimes return (error); 1177df8bae1dSRodney W. Grimes } 117811b763dfSJeff Roberson /* 117911b763dfSJeff Roberson * Convert a select bit set to poll flags. 1180748b9df6SJeff Roberson * 118111b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 118211b763dfSJeff Roberson * return this as a set bit in any set. 118311b763dfSJeff Roberson */ 118411b763dfSJeff Roberson static int select_flags[3] = { 118511b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 118611b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 118761e53a38SKonstantin Belousov POLLRDBAND | POLLERR 118811b763dfSJeff Roberson }; 118911b763dfSJeff Roberson 119011b763dfSJeff Roberson /* 119111b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 119211b763dfSJeff Roberson * bit position in the fd_mask array. 119311b763dfSJeff Roberson */ 119411b763dfSJeff Roberson static __inline int 119560b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 119611b763dfSJeff Roberson { 119711b763dfSJeff Roberson int flags; 119811b763dfSJeff Roberson int msk; 119911b763dfSJeff Roberson 120011b763dfSJeff Roberson flags = 0; 120111b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 120211b763dfSJeff Roberson if (ibits[msk] == NULL) 120311b763dfSJeff Roberson continue; 120460b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 120511b763dfSJeff Roberson continue; 120611b763dfSJeff Roberson flags |= select_flags[msk]; 120711b763dfSJeff Roberson } 120811b763dfSJeff Roberson return (flags); 120911b763dfSJeff Roberson } 121011b763dfSJeff Roberson 121111b763dfSJeff Roberson /* 121211b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 121311b763dfSJeff Roberson * input bits originally requested. 121411b763dfSJeff Roberson */ 121511b763dfSJeff Roberson static __inline int 121611b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 121711b763dfSJeff Roberson { 121811b763dfSJeff Roberson int msk; 121911b763dfSJeff Roberson int n; 122011b763dfSJeff Roberson 122111b763dfSJeff Roberson n = 0; 122211b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 122311b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 122411b763dfSJeff Roberson continue; 122511b763dfSJeff Roberson if (ibits[msk] == NULL) 122611b763dfSJeff Roberson continue; 122711b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 122811b763dfSJeff Roberson continue; 122911b763dfSJeff Roberson /* 123011b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 123111b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 123211b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 123311b763dfSJeff Roberson * call selsetbits twice as a result. 123411b763dfSJeff Roberson */ 123511b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 123611b763dfSJeff Roberson continue; 123711b763dfSJeff Roberson obits[msk][idx] |= bit; 123811b763dfSJeff Roberson n++; 123911b763dfSJeff Roberson } 124011b763dfSJeff Roberson 124111b763dfSJeff Roberson return (n); 124211b763dfSJeff Roberson } 1243df8bae1dSRodney W. Grimes 1244a9d2f8d8SRobert Watson static __inline int 1245a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1246a9d2f8d8SRobert Watson { 1247a9d2f8d8SRobert Watson 124852604ed7SMateusz Guzik return (fget_unlocked(fdp, fd, &cap_event_rights, fpp)); 1249a9d2f8d8SRobert Watson } 1250a9d2f8d8SRobert Watson 1251ace8398dSJeff Roberson /* 1252ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1253ace8398dSJeff Roberson * completion. 1254ace8398dSJeff Roberson */ 1255ace8398dSJeff Roberson static int 1256ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1257ace8398dSJeff Roberson { 125811b763dfSJeff Roberson struct filedesc *fdp; 125911b763dfSJeff Roberson struct selinfo *si; 1260ace8398dSJeff Roberson struct seltd *stp; 1261ace8398dSJeff Roberson struct selfd *sfp; 1262ace8398dSJeff Roberson struct selfd *sfn; 1263ace8398dSJeff Roberson struct file *fp; 12649cdacff1SJeff Roberson fd_mask bit; 12659cdacff1SJeff Roberson int fd, ev, n, idx; 1266a9d2f8d8SRobert Watson int error; 1267ace8398dSJeff Roberson 126811b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1269ace8398dSJeff Roberson stp = td->td_sel; 127011b763dfSJeff Roberson n = 0; 1271ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1272ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1273ace8398dSJeff Roberson si = sfp->sf_si; 1274ace8398dSJeff Roberson selfdfree(stp, sfp); 1275ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1276ace8398dSJeff Roberson if (si != NULL) 1277ace8398dSJeff Roberson continue; 1278a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1279a9d2f8d8SRobert Watson if (error) 1280a9d2f8d8SRobert Watson return (error); 128111b763dfSJeff Roberson idx = fd / NFDBITS; 12829cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 128311b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1284bf422e5fSJeff Roberson fdrop(fp, td); 128511b763dfSJeff Roberson if (ev != 0) 128611b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1287ace8398dSJeff Roberson } 1288ace8398dSJeff Roberson stp->st_flags = 0; 1289ace8398dSJeff Roberson td->td_retval[0] = n; 1290ace8398dSJeff Roberson return (0); 1291ace8398dSJeff Roberson } 1292ace8398dSJeff Roberson 1293ace8398dSJeff Roberson /* 1294ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1295ace8398dSJeff Roberson * each selinfo. 1296ace8398dSJeff Roberson */ 1297265fc98fSSeigo Tanimura static int 1298cc3c9df8SEd Maste selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd) 1299df8bae1dSRodney W. Grimes { 130011b763dfSJeff Roberson struct filedesc *fdp; 1301df8bae1dSRodney W. Grimes struct file *fp; 13029cdacff1SJeff Roberson fd_mask bit; 130311b763dfSJeff Roberson int ev, flags, end, fd; 13049cdacff1SJeff Roberson int n, idx; 1305a9d2f8d8SRobert Watson int error; 1306df8bae1dSRodney W. Grimes 130711b763dfSJeff Roberson fdp = td->td_proc->p_fd; 130811b763dfSJeff Roberson n = 0; 13099cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 131011b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 131111b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 131211b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 131311b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 131411b763dfSJeff Roberson if (flags == 0) 1315f082218cSPeter Wemm continue; 1316a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1317a9d2f8d8SRobert Watson if (error) 1318a9d2f8d8SRobert Watson return (error); 1319ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 132011b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1321bf422e5fSJeff Roberson fdrop(fp, td); 132211b763dfSJeff Roberson if (ev != 0) 132311b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1324df8bae1dSRodney W. Grimes } 1325df8bae1dSRodney W. Grimes } 132611b763dfSJeff Roberson 1327b40ce416SJulian Elischer td->td_retval[0] = n; 1328df8bae1dSRodney W. Grimes return (0); 1329df8bae1dSRodney W. Grimes } 1330df8bae1dSRodney W. Grimes 133142d11757SPeter Wemm int 1332186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap) 1333186d9c34SDmitry Chagin { 1334186d9c34SDmitry Chagin struct timespec ts, *tsp; 1335186d9c34SDmitry Chagin 1336186d9c34SDmitry Chagin if (uap->timeout != INFTIM) { 1337186d9c34SDmitry Chagin if (uap->timeout < 0) 1338186d9c34SDmitry Chagin return (EINVAL); 1339186d9c34SDmitry Chagin ts.tv_sec = uap->timeout / 1000; 1340186d9c34SDmitry Chagin ts.tv_nsec = (uap->timeout % 1000) * 1000000; 1341186d9c34SDmitry Chagin tsp = &ts; 1342186d9c34SDmitry Chagin } else 1343186d9c34SDmitry Chagin tsp = NULL; 1344186d9c34SDmitry Chagin 1345186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); 1346186d9c34SDmitry Chagin } 1347186d9c34SDmitry Chagin 1348186d9c34SDmitry Chagin int 13492384981bSConrad Meyer kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, 1350186d9c34SDmitry Chagin struct timespec *tsp, sigset_t *uset) 135142d11757SPeter Wemm { 13522384981bSConrad Meyer struct pollfd *kfds; 13532384981bSConrad Meyer struct pollfd stackfds[32]; 1354186d9c34SDmitry Chagin sbintime_t sbt, precision, tmp; 1355186d9c34SDmitry Chagin time_t over; 1356186d9c34SDmitry Chagin struct timespec ts; 1357cf5e4fe6SDavide Italiano int error; 135842d11757SPeter Wemm 1359186d9c34SDmitry Chagin precision = 0; 1360186d9c34SDmitry Chagin if (tsp != NULL) { 1361186d9c34SDmitry Chagin if (tsp->tv_sec < 0) 1362186d9c34SDmitry Chagin return (EINVAL); 1363186d9c34SDmitry Chagin if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000) 1364186d9c34SDmitry Chagin return (EINVAL); 1365186d9c34SDmitry Chagin if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1366186d9c34SDmitry Chagin sbt = 0; 1367186d9c34SDmitry Chagin else { 1368186d9c34SDmitry Chagin ts = *tsp; 1369186d9c34SDmitry Chagin if (ts.tv_sec > INT32_MAX / 2) { 1370186d9c34SDmitry Chagin over = ts.tv_sec - INT32_MAX / 2; 1371186d9c34SDmitry Chagin ts.tv_sec -= over; 1372186d9c34SDmitry Chagin } else 1373186d9c34SDmitry Chagin over = 0; 1374186d9c34SDmitry Chagin tmp = tstosbt(ts); 1375186d9c34SDmitry Chagin precision = tmp; 1376186d9c34SDmitry Chagin precision >>= tc_precexp; 1377186d9c34SDmitry Chagin if (TIMESEL(&sbt, tmp)) 1378186d9c34SDmitry Chagin sbt += tc_tick_sbt; 1379186d9c34SDmitry Chagin sbt += tmp; 1380186d9c34SDmitry Chagin } 1381186d9c34SDmitry Chagin } else 1382186d9c34SDmitry Chagin sbt = -1; 1383186d9c34SDmitry Chagin 138478c2a980SConrad Meyer /* 138578c2a980SConrad Meyer * This is kinda bogus. We have fd limits, but that is not 138678c2a980SConrad Meyer * really related to the size of the pollfd array. Make sure 138778c2a980SConrad Meyer * we let the process use at least FD_SETSIZE entries and at 138878c2a980SConrad Meyer * least enough for the system-wide limits. We want to be reasonably 138978c2a980SConrad Meyer * safe, but not overly restrictive. 139078c2a980SConrad Meyer */ 1391374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1392ace8398dSJeff Roberson return (EINVAL); 13932384981bSConrad Meyer if (nfds > nitems(stackfds)) 13942384981bSConrad Meyer kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); 139542d11757SPeter Wemm else 13962384981bSConrad Meyer kfds = stackfds; 13972384981bSConrad Meyer error = copyin(ufds, kfds, nfds * sizeof(*kfds)); 139842d11757SPeter Wemm if (error) 1399ace8398dSJeff Roberson goto done; 1400186d9c34SDmitry Chagin 1401186d9c34SDmitry Chagin if (uset != NULL) { 1402186d9c34SDmitry Chagin error = kern_sigprocmask(td, SIG_SETMASK, uset, 1403186d9c34SDmitry Chagin &td->td_oldsigmask, 0); 1404186d9c34SDmitry Chagin if (error) 1405ace8398dSJeff Roberson goto done; 1406186d9c34SDmitry Chagin td->td_pflags |= TDP_OLDMASK; 1407186d9c34SDmitry Chagin /* 1408186d9c34SDmitry Chagin * Make sure that ast() is called on return to 1409186d9c34SDmitry Chagin * usermode and TDP_OLDMASK is cleared, restoring old 1410186d9c34SDmitry Chagin * sigmask. 1411186d9c34SDmitry Chagin */ 1412186d9c34SDmitry Chagin thread_lock(td); 1413186d9c34SDmitry Chagin td->td_flags |= TDF_ASTPENDING; 1414186d9c34SDmitry Chagin thread_unlock(td); 141542d11757SPeter Wemm } 1416186d9c34SDmitry Chagin 1417ace8398dSJeff Roberson seltdinit(td); 1418ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1419ace8398dSJeff Roberson for (;;) { 14202384981bSConrad Meyer error = pollscan(td, kfds, nfds); 1421ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1422ace8398dSJeff Roberson break; 1423186d9c34SDmitry Chagin error = seltdwait(td, sbt, precision); 1424ace8398dSJeff Roberson if (error) 1425ace8398dSJeff Roberson break; 1426ace8398dSJeff Roberson error = pollrescan(td); 1427ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1428ace8398dSJeff Roberson break; 142985f190e4SAlfred Perlstein } 1430ace8398dSJeff Roberson seltdclear(td); 1431265fc98fSSeigo Tanimura 143242d11757SPeter Wemm done: 143342d11757SPeter Wemm /* poll is not restarted after signals... */ 143442d11757SPeter Wemm if (error == ERESTART) 143542d11757SPeter Wemm error = EINTR; 143642d11757SPeter Wemm if (error == EWOULDBLOCK) 143742d11757SPeter Wemm error = 0; 143842d11757SPeter Wemm if (error == 0) { 14392384981bSConrad Meyer error = pollout(td, kfds, ufds, nfds); 144042d11757SPeter Wemm if (error) 144142d11757SPeter Wemm goto out; 144242d11757SPeter Wemm } 144342d11757SPeter Wemm out: 14442384981bSConrad Meyer if (nfds > nitems(stackfds)) 14452384981bSConrad Meyer free(kfds, M_TEMP); 144642d11757SPeter Wemm return (error); 144742d11757SPeter Wemm } 144842d11757SPeter Wemm 1449186d9c34SDmitry Chagin int 1450186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap) 1451186d9c34SDmitry Chagin { 1452186d9c34SDmitry Chagin struct timespec ts, *tsp; 1453186d9c34SDmitry Chagin sigset_t set, *ssp; 1454186d9c34SDmitry Chagin int error; 1455186d9c34SDmitry Chagin 1456186d9c34SDmitry Chagin if (uap->ts != NULL) { 1457186d9c34SDmitry Chagin error = copyin(uap->ts, &ts, sizeof(ts)); 1458186d9c34SDmitry Chagin if (error) 1459186d9c34SDmitry Chagin return (error); 1460186d9c34SDmitry Chagin tsp = &ts; 1461186d9c34SDmitry Chagin } else 1462186d9c34SDmitry Chagin tsp = NULL; 1463186d9c34SDmitry Chagin if (uap->set != NULL) { 1464186d9c34SDmitry Chagin error = copyin(uap->set, &set, sizeof(set)); 1465186d9c34SDmitry Chagin if (error) 1466186d9c34SDmitry Chagin return (error); 1467186d9c34SDmitry Chagin ssp = &set; 1468186d9c34SDmitry Chagin } else 1469186d9c34SDmitry Chagin ssp = NULL; 1470186d9c34SDmitry Chagin /* 1471186d9c34SDmitry Chagin * fds is still a pointer to user space. kern_poll() will 1472186d9c34SDmitry Chagin * take care of copyin that array to the kernel space. 1473186d9c34SDmitry Chagin */ 1474186d9c34SDmitry Chagin 1475186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 1476186d9c34SDmitry Chagin } 1477186d9c34SDmitry Chagin 1478b1607c87SMateusz Guzik #ifdef CAPABILITIES 1479b1607c87SMateusz Guzik static int 1480b1607c87SMateusz Guzik poll_fget(struct filedesc *fdp, int fd, struct file **fpp) 1481b1607c87SMateusz Guzik { 1482b1607c87SMateusz Guzik const struct filedescent *fde; 1483b1607c87SMateusz Guzik const struct fdescenttbl *fdt; 1484b1607c87SMateusz Guzik const cap_rights_t *haverights; 1485b1607c87SMateusz Guzik struct file *fp; 1486b1607c87SMateusz Guzik int error; 1487b1607c87SMateusz Guzik 1488b1607c87SMateusz Guzik if (__predict_false(fd >= fdp->fd_nfiles)) 1489b1607c87SMateusz Guzik return (EBADF); 1490b1607c87SMateusz Guzik 1491b1607c87SMateusz Guzik fdt = fdp->fd_files; 1492b1607c87SMateusz Guzik fde = &fdt->fdt_ofiles[fd]; 1493b1607c87SMateusz Guzik fp = fde->fde_file; 1494b1607c87SMateusz Guzik if (__predict_false(fp == NULL)) 1495b1607c87SMateusz Guzik return (EBADF); 1496b1607c87SMateusz Guzik haverights = cap_rights_fde_inline(fde); 1497b1607c87SMateusz Guzik error = cap_check_inline(haverights, &cap_event_rights); 1498b1607c87SMateusz Guzik if (__predict_false(error != 0)) 1499b1607c87SMateusz Guzik return (EBADF); 1500b1607c87SMateusz Guzik *fpp = fp; 1501b1607c87SMateusz Guzik return (0); 1502b1607c87SMateusz Guzik } 1503b1607c87SMateusz Guzik #else 1504b1607c87SMateusz Guzik static int 1505b1607c87SMateusz Guzik poll_fget(struct filedesc *fdp, int fd, struct file **fpp) 1506b1607c87SMateusz Guzik { 1507b1607c87SMateusz Guzik struct file *fp; 1508b1607c87SMateusz Guzik 1509b1607c87SMateusz Guzik if (__predict_false(fd >= fdp->fd_nfiles)) 1510b1607c87SMateusz Guzik return (EBADF); 1511b1607c87SMateusz Guzik 1512b1607c87SMateusz Guzik fp = fdp->fd_ofiles[fd].fde_file; 1513b1607c87SMateusz Guzik if (__predict_false(fp == NULL)) 1514b1607c87SMateusz Guzik return (EBADF); 1515b1607c87SMateusz Guzik 1516b1607c87SMateusz Guzik *fpp = fp; 1517b1607c87SMateusz Guzik return (0); 1518b1607c87SMateusz Guzik } 1519b1607c87SMateusz Guzik #endif 1520b1607c87SMateusz Guzik 152142d11757SPeter Wemm static int 1522ace8398dSJeff Roberson pollrescan(struct thread *td) 1523ace8398dSJeff Roberson { 1524ace8398dSJeff Roberson struct seltd *stp; 1525ace8398dSJeff Roberson struct selfd *sfp; 1526ace8398dSJeff Roberson struct selfd *sfn; 1527ace8398dSJeff Roberson struct selinfo *si; 1528ace8398dSJeff Roberson struct filedesc *fdp; 1529ace8398dSJeff Roberson struct file *fp; 1530ace8398dSJeff Roberson struct pollfd *fd; 1531ace8398dSJeff Roberson int n; 1532ace8398dSJeff Roberson 1533ace8398dSJeff Roberson n = 0; 1534ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1535ace8398dSJeff Roberson stp = td->td_sel; 1536ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1537ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1538ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1539ace8398dSJeff Roberson si = sfp->sf_si; 1540ace8398dSJeff Roberson selfdfree(stp, sfp); 1541ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1542ace8398dSJeff Roberson if (si != NULL) 1543ace8398dSJeff Roberson continue; 1544b1607c87SMateusz Guzik if (poll_fget(fdp, fd->fd, &fp) != 0) { 1545ace8398dSJeff Roberson fd->revents = POLLNVAL; 1546ace8398dSJeff Roberson n++; 1547ace8398dSJeff Roberson continue; 1548ace8398dSJeff Roberson } 1549ace8398dSJeff Roberson /* 1550ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1551ace8398dSJeff Roberson * POLLERR if appropriate. 1552ace8398dSJeff Roberson */ 1553ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1554ace8398dSJeff Roberson if (fd->revents != 0) 1555ace8398dSJeff Roberson n++; 1556ace8398dSJeff Roberson } 1557ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1558ace8398dSJeff Roberson stp->st_flags = 0; 1559ace8398dSJeff Roberson td->td_retval[0] = n; 1560ace8398dSJeff Roberson return (0); 1561ace8398dSJeff Roberson } 1562ace8398dSJeff Roberson 1563ace8398dSJeff Roberson static int 1564cc3c9df8SEd Maste pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd) 1565ae81968fSRobert Watson { 1566ae81968fSRobert Watson int error = 0; 1567ae81968fSRobert Watson u_int i = 0; 15686d8feddaSKonstantin Belousov u_int n = 0; 1569ae81968fSRobert Watson 1570ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1571ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1572ae81968fSRobert Watson sizeof(ufds->revents)); 1573ae81968fSRobert Watson if (error) 1574ae81968fSRobert Watson return (error); 15756d8feddaSKonstantin Belousov if (fds->revents != 0) 15766d8feddaSKonstantin Belousov n++; 1577ae81968fSRobert Watson fds++; 1578ae81968fSRobert Watson ufds++; 1579ae81968fSRobert Watson } 15806d8feddaSKonstantin Belousov td->td_retval[0] = n; 1581ae81968fSRobert Watson return (0); 1582ae81968fSRobert Watson } 1583ae81968fSRobert Watson 1584ae81968fSRobert Watson static int 1585cc3c9df8SEd Maste pollscan(struct thread *td, struct pollfd *fds, u_int nfd) 158642d11757SPeter Wemm { 1587b1607c87SMateusz Guzik struct filedesc *fdp; 158842d11757SPeter Wemm struct file *fp; 1589b1607c87SMateusz Guzik int i, n; 159042d11757SPeter Wemm 1591b1607c87SMateusz Guzik n = 0; 1592b1607c87SMateusz Guzik fdp = td->td_proc->p_fd; 15935e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1594eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1595b1607c87SMateusz Guzik if (fds->fd < 0) { 1596337c9691SJordan K. Hubbard fds->revents = 0; 1597b1607c87SMateusz Guzik continue; 1598b1607c87SMateusz Guzik } 1599b1607c87SMateusz Guzik if (poll_fget(fdp, fds->fd, &fp) != 0) { 160042d11757SPeter Wemm fds->revents = POLLNVAL; 160142d11757SPeter Wemm n++; 1602b1607c87SMateusz Guzik continue; 1603b1607c87SMateusz Guzik } 16042087c896SBruce Evans /* 16052087c896SBruce Evans * Note: backend also returns POLLHUP and 16062087c896SBruce Evans * POLLERR if appropriate. 16072087c896SBruce Evans */ 1608ace8398dSJeff Roberson selfdalloc(td, fds); 160913ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1610ea6027a8SRobert Watson td->td_ucred, td); 1611f2159cc7SKonstantin Belousov /* 1612f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1613f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1614f2159cc7SKonstantin Belousov */ 1615f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1616f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1617f2159cc7SKonstantin Belousov 161842d11757SPeter Wemm if (fds->revents != 0) 161942d11757SPeter Wemm n++; 162042d11757SPeter Wemm } 16215e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1622b40ce416SJulian Elischer td->td_retval[0] = n; 162342d11757SPeter Wemm return (0); 162442d11757SPeter Wemm } 162542d11757SPeter Wemm 162642d11757SPeter Wemm /* 1627237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1628237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1629237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1630237abf0cSDavide Italiano * determine which events fired. 1631237abf0cSDavide Italiano */ 1632237abf0cSDavide Italiano int 1633237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1634237abf0cSDavide Italiano { 1635237abf0cSDavide Italiano struct timeval rtv; 1636237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1637237abf0cSDavide Italiano int error; 1638237abf0cSDavide Italiano 163976665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1640237abf0cSDavide Italiano if (tvp != NULL) { 1641237abf0cSDavide Italiano rtv = *tvp; 1642237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1643237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1644237abf0cSDavide Italiano return (EINVAL); 1645237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1646237abf0cSDavide Italiano asbt = 0; 1647237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1648237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1649237abf0cSDavide Italiano precision = rsbt; 1650237abf0cSDavide Italiano precision >>= tc_precexp; 1651237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1652237abf0cSDavide Italiano asbt += tc_tick_sbt; 16534bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1654237abf0cSDavide Italiano asbt += rsbt; 1655237abf0cSDavide Italiano else 1656237abf0cSDavide Italiano asbt = -1; 1657237abf0cSDavide Italiano } else 1658237abf0cSDavide Italiano asbt = -1; 1659237abf0cSDavide Italiano } else 1660237abf0cSDavide Italiano asbt = -1; 1661237abf0cSDavide Italiano seltdinit(td); 1662237abf0cSDavide Italiano /* 1663237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1664237abf0cSDavide Italiano */ 1665237abf0cSDavide Italiano for (;;) { 1666237abf0cSDavide Italiano selfdalloc(td, NULL); 1667237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1668237abf0cSDavide Italiano /* error here is actually the ready events. */ 1669237abf0cSDavide Italiano if (error) 1670237abf0cSDavide Italiano return (0); 1671237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1672237abf0cSDavide Italiano if (error) 1673237abf0cSDavide Italiano break; 1674237abf0cSDavide Italiano } 1675237abf0cSDavide Italiano seltdclear(td); 1676237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1677237abf0cSDavide Italiano if (error == ERESTART) 1678237abf0cSDavide Italiano error = 0; 1679237abf0cSDavide Italiano return (error); 1680237abf0cSDavide Italiano } 1681237abf0cSDavide Italiano 1682237abf0cSDavide Italiano /* 1683ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1684ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1685ace8398dSJeff Roberson */ 1686ace8398dSJeff Roberson static void 1687ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1688ace8398dSJeff Roberson { 1689ace8398dSJeff Roberson struct seltd *stp; 1690ace8398dSJeff Roberson 1691ace8398dSJeff Roberson stp = td->td_sel; 1692ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1693ea33cca9SMateusz Guzik stp->st_free1 = malloc(sizeof(*stp->st_free1), M_SELFD, M_WAITOK|M_ZERO); 1694ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1695ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1696ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1697ea33cca9SMateusz Guzik stp->st_free2 = malloc(sizeof(*stp->st_free2), M_SELFD, M_WAITOK|M_ZERO); 1698ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1699ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1700ace8398dSJeff Roberson } 1701ace8398dSJeff Roberson 1702ace8398dSJeff Roberson static void 1703ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1704ace8398dSJeff Roberson { 1705ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 1706*31b2ac4bSMateusz Guzik /* 1707*31b2ac4bSMateusz Guzik * Paired with doselwakeup. 1708*31b2ac4bSMateusz Guzik */ 1709*31b2ac4bSMateusz Guzik if (atomic_load_acq_ptr((uintptr_t *)&sfp->sf_si) != (uintptr_t)NULL) { 1710ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1711fcb5b3a4SKonstantin Belousov if (sfp->sf_si != NULL) { 1712ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1713fcb5b3a4SKonstantin Belousov } 1714ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 171573f2e5f7SMateusz Guzik } 1716ea33cca9SMateusz Guzik free(sfp, M_SELFD); 171785f190e4SAlfred Perlstein } 171885f190e4SAlfred Perlstein 17196aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ 17206aba400aSAttilio Rao void 1721cc3c9df8SEd Maste seldrain(struct selinfo *sip) 17226aba400aSAttilio Rao { 17236aba400aSAttilio Rao 17246aba400aSAttilio Rao /* 17256aba400aSAttilio Rao * This feature is already provided by doselwakeup(), thus it is 17266aba400aSAttilio Rao * enough to go for it. 17276aba400aSAttilio Rao * Eventually, the context, should take care to avoid races 17286aba400aSAttilio Rao * between thread calling select()/poll() and file descriptor 17296aba400aSAttilio Rao * detaching, but, again, the races are just the same as 17306aba400aSAttilio Rao * selwakeup(). 17316aba400aSAttilio Rao */ 17326aba400aSAttilio Rao doselwakeup(sip, -1); 17336aba400aSAttilio Rao } 17346aba400aSAttilio Rao 1735df8bae1dSRodney W. Grimes /* 1736df8bae1dSRodney W. Grimes * Record a select request. 1737df8bae1dSRodney W. Grimes */ 1738df8bae1dSRodney W. Grimes void 1739cc3c9df8SEd Maste selrecord(struct thread *selector, struct selinfo *sip) 1740df8bae1dSRodney W. Grimes { 1741ace8398dSJeff Roberson struct selfd *sfp; 1742ace8398dSJeff Roberson struct seltd *stp; 1743ace8398dSJeff Roberson struct mtx *mtxp; 1744df8bae1dSRodney W. Grimes 1745ace8398dSJeff Roberson stp = selector->td_sel; 174685f190e4SAlfred Perlstein /* 1747ace8398dSJeff Roberson * Don't record when doing a rescan. 174885f190e4SAlfred Perlstein */ 1749ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1750ace8398dSJeff Roberson return; 1751ace8398dSJeff Roberson /* 1752ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1753ace8398dSJeff Roberson */ 1754ace8398dSJeff Roberson sfp = NULL; 1755ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1756ace8398dSJeff Roberson stp->st_free1 = NULL; 1757ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1758ace8398dSJeff Roberson stp->st_free2 = NULL; 1759ace8398dSJeff Roberson else 1760ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 17612141453eSJeff Roberson mtxp = sip->si_mtx; 17622141453eSJeff Roberson if (mtxp == NULL) 17632141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1764ace8398dSJeff Roberson /* 1765ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1766ace8398dSJeff Roberson */ 1767ace8398dSJeff Roberson sfp->sf_si = sip; 1768ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1769ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1770ace8398dSJeff Roberson /* 1771ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1772ace8398dSJeff Roberson */ 1773ace8398dSJeff Roberson mtx_lock(mtxp); 1774ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1775ace8398dSJeff Roberson sip->si_mtx = mtxp; 1776ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 177785f190e4SAlfred Perlstein } 1778ace8398dSJeff Roberson /* 1779ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1780ace8398dSJeff Roberson */ 1781ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1782ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1783df8bae1dSRodney W. Grimes } 1784df8bae1dSRodney W. Grimes 1785512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1786df8bae1dSRodney W. Grimes void 1787cc3c9df8SEd Maste selwakeup(struct selinfo *sip) 1788df8bae1dSRodney W. Grimes { 1789512824f8SSeigo Tanimura doselwakeup(sip, -1); 1790512824f8SSeigo Tanimura } 1791512824f8SSeigo Tanimura 1792512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1793512824f8SSeigo Tanimura void 1794cc3c9df8SEd Maste selwakeuppri(struct selinfo *sip, int pri) 1795512824f8SSeigo Tanimura { 1796512824f8SSeigo Tanimura doselwakeup(sip, pri); 1797512824f8SSeigo Tanimura } 1798512824f8SSeigo Tanimura 1799512824f8SSeigo Tanimura /* 1800512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1801512824f8SSeigo Tanimura */ 1802512824f8SSeigo Tanimura static void 1803cc3c9df8SEd Maste doselwakeup(struct selinfo *sip, int pri) 1804512824f8SSeigo Tanimura { 1805ace8398dSJeff Roberson struct selfd *sfp; 1806ace8398dSJeff Roberson struct selfd *sfn; 1807ace8398dSJeff Roberson struct seltd *stp; 1808df8bae1dSRodney W. Grimes 1809ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1810ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1811b40ce416SJulian Elischer return; 1812ace8398dSJeff Roberson /* 1813ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1814ace8398dSJeff Roberson */ 1815ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1816ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1817ace8398dSJeff Roberson /* 1818ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1819ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1820ace8398dSJeff Roberson */ 1821ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1822ace8398dSJeff Roberson stp = sfp->sf_td; 1823*31b2ac4bSMateusz Guzik /* 1824*31b2ac4bSMateusz Guzik * Paired with selfdfree. 1825*31b2ac4bSMateusz Guzik */ 1826*31b2ac4bSMateusz Guzik atomic_store_rel_ptr((uintptr_t *)&sfp->sf_si, (uintptr_t)NULL); 1827ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1828ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1829ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1830ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1831b40ce416SJulian Elischer } 1832ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1833ace8398dSJeff Roberson } 1834ace8398dSJeff Roberson 1835ace8398dSJeff Roberson static void 1836ace8398dSJeff Roberson seltdinit(struct thread *td) 1837ace8398dSJeff Roberson { 1838ace8398dSJeff Roberson struct seltd *stp; 1839ace8398dSJeff Roberson 1840ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1841ace8398dSJeff Roberson goto out; 1842ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1843ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1844ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1845ace8398dSJeff Roberson out: 1846ace8398dSJeff Roberson stp->st_flags = 0; 1847ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1848ace8398dSJeff Roberson } 1849ace8398dSJeff Roberson 1850ace8398dSJeff Roberson static int 1851cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision) 1852ace8398dSJeff Roberson { 1853ace8398dSJeff Roberson struct seltd *stp; 1854ace8398dSJeff Roberson int error; 1855ace8398dSJeff Roberson 1856ace8398dSJeff Roberson stp = td->td_sel; 1857ace8398dSJeff Roberson /* 1858ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1859ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1860ace8398dSJeff Roberson */ 1861ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1862ace8398dSJeff Roberson /* 1863ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1864ace8398dSJeff Roberson */ 1865ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1866ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1867ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1868ace8398dSJeff Roberson return (0); 1869ace8398dSJeff Roberson } 1870cf5e4fe6SDavide Italiano if (sbt == 0) 1871cf5e4fe6SDavide Italiano error = EWOULDBLOCK; 1872cf5e4fe6SDavide Italiano else if (sbt != -1) 1873cf5e4fe6SDavide Italiano error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx, 1874cf5e4fe6SDavide Italiano sbt, precision, C_ABSOLUTE); 1875ace8398dSJeff Roberson else 1876ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1877ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1878ace8398dSJeff Roberson 1879ace8398dSJeff Roberson return (error); 1880ace8398dSJeff Roberson } 1881ace8398dSJeff Roberson 1882ace8398dSJeff Roberson void 1883ace8398dSJeff Roberson seltdfini(struct thread *td) 1884ace8398dSJeff Roberson { 1885ace8398dSJeff Roberson struct seltd *stp; 1886ace8398dSJeff Roberson 1887ace8398dSJeff Roberson stp = td->td_sel; 1888ace8398dSJeff Roberson if (stp == NULL) 1889ace8398dSJeff Roberson return; 1890ace8398dSJeff Roberson if (stp->st_free1) 1891ea33cca9SMateusz Guzik free(stp->st_free1, M_SELFD); 1892ace8398dSJeff Roberson if (stp->st_free2) 1893ea33cca9SMateusz Guzik free(stp->st_free2, M_SELFD); 1894ace8398dSJeff Roberson td->td_sel = NULL; 189536bce27bSKonstantin Belousov cv_destroy(&stp->st_wait); 189636bce27bSKonstantin Belousov mtx_destroy(&stp->st_mtx); 1897ace8398dSJeff Roberson free(stp, M_SELECT); 1898ace8398dSJeff Roberson } 1899ace8398dSJeff Roberson 1900ace8398dSJeff Roberson /* 1901ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1902ace8398dSJeff Roberson * polling. 1903ace8398dSJeff Roberson */ 1904ace8398dSJeff Roberson static void 1905ace8398dSJeff Roberson seltdclear(struct thread *td) 1906ace8398dSJeff Roberson { 1907ace8398dSJeff Roberson struct seltd *stp; 1908ace8398dSJeff Roberson struct selfd *sfp; 1909ace8398dSJeff Roberson struct selfd *sfn; 1910ace8398dSJeff Roberson 1911ace8398dSJeff Roberson stp = td->td_sel; 1912ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1913ace8398dSJeff Roberson selfdfree(stp, sfp); 1914ace8398dSJeff Roberson stp->st_flags = 0; 1915df8bae1dSRodney W. Grimes } 1916265fc98fSSeigo Tanimura 19174d77a549SAlfred Perlstein static void selectinit(void *); 1918ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1919265fc98fSSeigo Tanimura static void 1920ace8398dSJeff Roberson selectinit(void *dummy __unused) 1921265fc98fSSeigo Tanimura { 19222141453eSJeff Roberson 19232141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1924265fc98fSSeigo Tanimura } 19250acf5d0bSMark Johnston 19260acf5d0bSMark Johnston /* 19270acf5d0bSMark Johnston * Set up a syscall return value that follows the convention specified for 19280acf5d0bSMark Johnston * posix_* functions. 19290acf5d0bSMark Johnston */ 19300acf5d0bSMark Johnston int 19310acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error) 19320acf5d0bSMark Johnston { 19330acf5d0bSMark Johnston 19340acf5d0bSMark Johnston if (error <= 0) 19350acf5d0bSMark Johnston return (error); 19360acf5d0bSMark Johnston td->td_errno = error; 19370acf5d0bSMark Johnston td->td_pflags |= TDP_NERRNO; 19380acf5d0bSMark Johnston td->td_retval[0] = error; 19390acf5d0bSMark Johnston return (0); 19400acf5d0bSMark Johnston } 1941