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. */ 159fcb5b3a4SKonstantin Belousov u_int sf_refs; 160ace8398dSJeff Roberson }; 161ace8398dSJeff Roberson 162*ea33cca9SMateusz Guzik MALLOC_DEFINE(M_SELFD, "selfd", "selfd"); 1632141453eSJeff Roberson static struct mtx_pool *mtxpool_select; 1648fe387abSDmitrij Tejblum 165611fcff9SJohn Baldwin #ifdef __LP64__ 166611fcff9SJohn Baldwin size_t 167611fcff9SJohn Baldwin devfs_iosize_max(void) 168611fcff9SJohn Baldwin { 169611fcff9SJohn Baldwin 170611fcff9SJohn Baldwin return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 171611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 172611fcff9SJohn Baldwin } 173611fcff9SJohn Baldwin 174611fcff9SJohn Baldwin size_t 175611fcff9SJohn Baldwin iosize_max(void) 176611fcff9SJohn Baldwin { 177611fcff9SJohn Baldwin 178611fcff9SJohn Baldwin return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ? 179611fcff9SJohn Baldwin INT_MAX : SSIZE_MAX); 180611fcff9SJohn Baldwin } 181611fcff9SJohn Baldwin #endif 182611fcff9SJohn Baldwin 183d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 184df8bae1dSRodney W. Grimes struct read_args { 185df8bae1dSRodney W. Grimes int fd; 186134e06feSBruce Evans void *buf; 187134e06feSBruce Evans size_t nbyte; 188df8bae1dSRodney W. Grimes }; 189d2d3e875SBruce Evans #endif 19026f9a767SRodney W. Grimes int 191cc3c9df8SEd Maste sys_read(struct thread *td, struct read_args *uap) 192df8bae1dSRodney W. Grimes { 193bcd9e0ddSJohn Baldwin struct uio auio; 194bcd9e0ddSJohn Baldwin struct iovec aiov; 195279d7226SMatthew Dillon int error; 196df8bae1dSRodney W. Grimes 197526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 198bcd9e0ddSJohn Baldwin return (EINVAL); 199bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 200bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 201bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 202bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 203bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 204bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 205bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 206279d7226SMatthew Dillon return (error); 207df8bae1dSRodney W. Grimes } 208df8bae1dSRodney W. Grimes 209df8bae1dSRodney W. Grimes /* 210bcd9e0ddSJohn Baldwin * Positioned read system call 2114160ccd9SAlan Cox */ 2124160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 2134160ccd9SAlan Cox struct pread_args { 2144160ccd9SAlan Cox int fd; 2154160ccd9SAlan Cox void *buf; 2164160ccd9SAlan Cox size_t nbyte; 2178fe387abSDmitrij Tejblum int pad; 2184160ccd9SAlan Cox off_t offset; 2194160ccd9SAlan Cox }; 2204160ccd9SAlan Cox #endif 2214160ccd9SAlan Cox int 222b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap) 223b38b22b0SEdward Tomasz Napierala { 224b38b22b0SEdward Tomasz Napierala 225b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 226b38b22b0SEdward Tomasz Napierala } 227b38b22b0SEdward Tomasz Napierala 228b38b22b0SEdward Tomasz Napierala int 229b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset) 2304160ccd9SAlan Cox { 2314160ccd9SAlan Cox struct uio auio; 2324160ccd9SAlan Cox struct iovec aiov; 233bcd9e0ddSJohn Baldwin int error; 2344160ccd9SAlan Cox 235b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 236bcd9e0ddSJohn Baldwin return (EINVAL); 237b38b22b0SEdward Tomasz Napierala aiov.iov_base = buf; 238b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 2394160ccd9SAlan Cox auio.uio_iov = &aiov; 2404160ccd9SAlan Cox auio.uio_iovcnt = 1; 241b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 2424160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 243b38b22b0SEdward Tomasz Napierala error = kern_preadv(td, fd, &auio, offset); 2444160ccd9SAlan Cox return (error); 2454160ccd9SAlan Cox } 2464160ccd9SAlan Cox 2470538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 248c2815ad5SPeter Wemm int 249b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap) 250c2815ad5SPeter Wemm { 251c2815ad5SPeter Wemm 252b38b22b0SEdward Tomasz Napierala return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 253c2815ad5SPeter Wemm } 2540538aafcSKonstantin Belousov #endif 255c2815ad5SPeter Wemm 2564160ccd9SAlan Cox /* 257df8bae1dSRodney W. Grimes * Scatter read system call. 258df8bae1dSRodney W. Grimes */ 259d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 260df8bae1dSRodney W. Grimes struct readv_args { 2617147b19dSBruce Evans int fd; 262df8bae1dSRodney W. Grimes struct iovec *iovp; 263df8bae1dSRodney W. Grimes u_int iovcnt; 264df8bae1dSRodney W. Grimes }; 265d2d3e875SBruce Evans #endif 26626f9a767SRodney W. Grimes int 2678451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap) 268df8bae1dSRodney W. Grimes { 269b88ec951SJohn Baldwin struct uio *auio; 270b88ec951SJohn Baldwin int error; 271b88ec951SJohn Baldwin 272b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 273b88ec951SJohn Baldwin if (error) 274b88ec951SJohn Baldwin return (error); 275b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 276b88ec951SJohn Baldwin free(auio, M_IOV); 277b88ec951SJohn Baldwin return (error); 278b88ec951SJohn Baldwin } 279b88ec951SJohn Baldwin 280b88ec951SJohn Baldwin int 281b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 282b88ec951SJohn Baldwin { 283b064d43dSMatthew Dillon struct file *fp; 284bcd9e0ddSJohn Baldwin int error; 285bcd9e0ddSJohn Baldwin 286cbd92ce6SMatt Macy error = fget_read(td, fd, &cap_read_rights, &fp); 287bcd9e0ddSJohn Baldwin if (error) 288bcd9e0ddSJohn Baldwin return (error); 289bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 290bcd9e0ddSJohn Baldwin fdrop(fp, td); 291bcd9e0ddSJohn Baldwin return (error); 292bcd9e0ddSJohn Baldwin } 293bcd9e0ddSJohn Baldwin 294bcd9e0ddSJohn Baldwin /* 295bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 296bcd9e0ddSJohn Baldwin */ 297bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 298bcd9e0ddSJohn Baldwin struct preadv_args { 299bcd9e0ddSJohn Baldwin int fd; 300bcd9e0ddSJohn Baldwin struct iovec *iovp; 301bcd9e0ddSJohn Baldwin u_int iovcnt; 302bcd9e0ddSJohn Baldwin off_t offset; 303bcd9e0ddSJohn Baldwin }; 304bcd9e0ddSJohn Baldwin #endif 305bcd9e0ddSJohn Baldwin int 3068451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap) 307bcd9e0ddSJohn Baldwin { 308bcd9e0ddSJohn Baldwin struct uio *auio; 309bcd9e0ddSJohn Baldwin int error; 310bcd9e0ddSJohn Baldwin 311bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 312bcd9e0ddSJohn Baldwin if (error) 313bcd9e0ddSJohn Baldwin return (error); 314bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 315bcd9e0ddSJohn Baldwin free(auio, M_IOV); 316bcd9e0ddSJohn Baldwin return (error); 317bcd9e0ddSJohn Baldwin } 318bcd9e0ddSJohn Baldwin 319bcd9e0ddSJohn Baldwin int 320cc3c9df8SEd Maste kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset) 321bcd9e0ddSJohn Baldwin { 322bcd9e0ddSJohn Baldwin struct file *fp; 323bcd9e0ddSJohn Baldwin int error; 324bcd9e0ddSJohn Baldwin 325cbd92ce6SMatt Macy error = fget_read(td, fd, &cap_pread_rights, &fp); 326bcd9e0ddSJohn Baldwin if (error) 327bcd9e0ddSJohn Baldwin return (error); 328bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 329bcd9e0ddSJohn Baldwin error = ESPIPE; 33032a1fb0dSMahdi Mokhtari else if (offset < 0 && 33132a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 332bcd9e0ddSJohn Baldwin error = EINVAL; 333bcd9e0ddSJohn Baldwin else 334bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 335bcd9e0ddSJohn Baldwin fdrop(fp, td); 336bcd9e0ddSJohn Baldwin return (error); 337bcd9e0ddSJohn Baldwin } 338bcd9e0ddSJohn Baldwin 339bcd9e0ddSJohn Baldwin /* 340bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 341bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 342bcd9e0ddSJohn Baldwin */ 343bcd9e0ddSJohn Baldwin static int 344cc3c9df8SEd Maste dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio, 345cc3c9df8SEd Maste off_t offset, int flags) 346bcd9e0ddSJohn Baldwin { 347bcd9e0ddSJohn Baldwin ssize_t cnt; 34882641acdSAlan Cox int error; 349df8bae1dSRodney W. Grimes #ifdef KTRACE 350552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 351df8bae1dSRodney W. Grimes #endif 352df8bae1dSRodney W. Grimes 35351d1f690SRobert Watson AUDIT_ARG_FD(fd); 35451d1f690SRobert Watson 3554f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3564f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3574f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3584f8d23d6SPoul-Henning Kamp return (0); 3594f8d23d6SPoul-Henning Kamp } 360552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 361bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 362552afd9cSPoul-Henning Kamp auio->uio_td = td; 363df8bae1dSRodney W. Grimes #ifdef KTRACE 364552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 365552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 366df8bae1dSRodney W. Grimes #endif 367552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 368bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 369552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 370df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 371df8bae1dSRodney W. Grimes error = 0; 372279d7226SMatthew Dillon } 373552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 374df8bae1dSRodney W. Grimes #ifdef KTRACE 375552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 376552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 377b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes #endif 380b40ce416SJulian Elischer td->td_retval[0] = cnt; 381df8bae1dSRodney W. Grimes return (error); 382df8bae1dSRodney W. Grimes } 383df8bae1dSRodney W. Grimes 384d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 385df8bae1dSRodney W. Grimes struct write_args { 386df8bae1dSRodney W. Grimes int fd; 387134e06feSBruce Evans const void *buf; 388134e06feSBruce Evans size_t nbyte; 389df8bae1dSRodney W. Grimes }; 390d2d3e875SBruce Evans #endif 39126f9a767SRodney W. Grimes int 392cc3c9df8SEd Maste sys_write(struct thread *td, struct write_args *uap) 393df8bae1dSRodney W. Grimes { 394bcd9e0ddSJohn Baldwin struct uio auio; 395bcd9e0ddSJohn Baldwin struct iovec aiov; 396279d7226SMatthew Dillon int error; 397df8bae1dSRodney W. Grimes 398526d0bd5SKonstantin Belousov if (uap->nbyte > IOSIZE_MAX) 399bcd9e0ddSJohn Baldwin return (EINVAL); 400bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 401bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 402bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 403bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 404bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 405bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 406bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 407279d7226SMatthew Dillon return (error); 408df8bae1dSRodney W. Grimes } 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes /* 4110c14ff0eSRobert Watson * Positioned write system call. 4124160ccd9SAlan Cox */ 4134160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 4144160ccd9SAlan Cox struct pwrite_args { 4154160ccd9SAlan Cox int fd; 4164160ccd9SAlan Cox const void *buf; 4174160ccd9SAlan Cox size_t nbyte; 4188fe387abSDmitrij Tejblum int pad; 4194160ccd9SAlan Cox off_t offset; 4204160ccd9SAlan Cox }; 4214160ccd9SAlan Cox #endif 4224160ccd9SAlan Cox int 423b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap) 424b38b22b0SEdward Tomasz Napierala { 425b38b22b0SEdward Tomasz Napierala 426b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 427b38b22b0SEdward Tomasz Napierala } 428b38b22b0SEdward Tomasz Napierala 429b38b22b0SEdward Tomasz Napierala int 430b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte, 431b38b22b0SEdward Tomasz Napierala off_t offset) 4324160ccd9SAlan Cox { 4334160ccd9SAlan Cox struct uio auio; 4344160ccd9SAlan Cox struct iovec aiov; 435bcd9e0ddSJohn Baldwin int error; 4364160ccd9SAlan Cox 437b38b22b0SEdward Tomasz Napierala if (nbyte > IOSIZE_MAX) 438bcd9e0ddSJohn Baldwin return (EINVAL); 439b38b22b0SEdward Tomasz Napierala aiov.iov_base = (void *)(uintptr_t)buf; 440b38b22b0SEdward Tomasz Napierala aiov.iov_len = nbyte; 4414160ccd9SAlan Cox auio.uio_iov = &aiov; 4424160ccd9SAlan Cox auio.uio_iovcnt = 1; 443b38b22b0SEdward Tomasz Napierala auio.uio_resid = nbyte; 4444160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 445b38b22b0SEdward Tomasz Napierala error = kern_pwritev(td, fd, &auio, offset); 4464160ccd9SAlan Cox return (error); 4474160ccd9SAlan Cox } 4484160ccd9SAlan Cox 4490538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6) 450c2815ad5SPeter Wemm int 451b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap) 452c2815ad5SPeter Wemm { 453c2815ad5SPeter Wemm 454b38b22b0SEdward Tomasz Napierala return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset)); 455c2815ad5SPeter Wemm } 4560538aafcSKonstantin Belousov #endif 457c2815ad5SPeter Wemm 4584160ccd9SAlan Cox /* 4590c14ff0eSRobert Watson * Gather write system call. 460df8bae1dSRodney W. Grimes */ 461d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 462df8bae1dSRodney W. Grimes struct writev_args { 463df8bae1dSRodney W. Grimes int fd; 464df8bae1dSRodney W. Grimes struct iovec *iovp; 465df8bae1dSRodney W. Grimes u_int iovcnt; 466df8bae1dSRodney W. Grimes }; 467d2d3e875SBruce Evans #endif 46826f9a767SRodney W. Grimes int 4698451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap) 470df8bae1dSRodney W. Grimes { 471b88ec951SJohn Baldwin struct uio *auio; 472b88ec951SJohn Baldwin int error; 473b88ec951SJohn Baldwin 474b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 475b88ec951SJohn Baldwin if (error) 476b88ec951SJohn Baldwin return (error); 477b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 478b88ec951SJohn Baldwin free(auio, M_IOV); 479b88ec951SJohn Baldwin return (error); 480b88ec951SJohn Baldwin } 481b88ec951SJohn Baldwin 482b88ec951SJohn Baldwin int 483b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 484b88ec951SJohn Baldwin { 485b064d43dSMatthew Dillon struct file *fp; 486bcd9e0ddSJohn Baldwin int error; 487bcd9e0ddSJohn Baldwin 488cbd92ce6SMatt Macy error = fget_write(td, fd, &cap_write_rights, &fp); 489bcd9e0ddSJohn Baldwin if (error) 490af56abaaSJohn Baldwin return (error); 491bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 492bcd9e0ddSJohn Baldwin fdrop(fp, td); 493bcd9e0ddSJohn Baldwin return (error); 494bcd9e0ddSJohn Baldwin } 495bcd9e0ddSJohn Baldwin 496bcd9e0ddSJohn Baldwin /* 4970c14ff0eSRobert Watson * Gather positioned write system call. 498bcd9e0ddSJohn Baldwin */ 499bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 500bcd9e0ddSJohn Baldwin struct pwritev_args { 501bcd9e0ddSJohn Baldwin int fd; 502bcd9e0ddSJohn Baldwin struct iovec *iovp; 503bcd9e0ddSJohn Baldwin u_int iovcnt; 504bcd9e0ddSJohn Baldwin off_t offset; 505bcd9e0ddSJohn Baldwin }; 506bcd9e0ddSJohn Baldwin #endif 507bcd9e0ddSJohn Baldwin int 5088451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap) 509bcd9e0ddSJohn Baldwin { 510bcd9e0ddSJohn Baldwin struct uio *auio; 511bcd9e0ddSJohn Baldwin int error; 512bcd9e0ddSJohn Baldwin 513bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 514bcd9e0ddSJohn Baldwin if (error) 515bcd9e0ddSJohn Baldwin return (error); 516bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 517bcd9e0ddSJohn Baldwin free(auio, M_IOV); 518bcd9e0ddSJohn Baldwin return (error); 519bcd9e0ddSJohn Baldwin } 520bcd9e0ddSJohn Baldwin 521bcd9e0ddSJohn Baldwin int 522d5cdcc3aSAndrew Gallatin kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset) 523bcd9e0ddSJohn Baldwin { 524bcd9e0ddSJohn Baldwin struct file *fp; 525bcd9e0ddSJohn Baldwin int error; 526bcd9e0ddSJohn Baldwin 527cbd92ce6SMatt Macy error = fget_write(td, fd, &cap_pwrite_rights, &fp); 528bcd9e0ddSJohn Baldwin if (error) 529af56abaaSJohn Baldwin return (error); 530bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 531bcd9e0ddSJohn Baldwin error = ESPIPE; 53232a1fb0dSMahdi Mokhtari else if (offset < 0 && 53332a1fb0dSMahdi Mokhtari (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) 534bcd9e0ddSJohn Baldwin error = EINVAL; 535bcd9e0ddSJohn Baldwin else 536bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 537bcd9e0ddSJohn Baldwin fdrop(fp, td); 538bcd9e0ddSJohn Baldwin return (error); 539bcd9e0ddSJohn Baldwin } 540bcd9e0ddSJohn Baldwin 541bcd9e0ddSJohn Baldwin /* 542bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 543bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 544bcd9e0ddSJohn Baldwin */ 545bcd9e0ddSJohn Baldwin static int 546cc3c9df8SEd Maste dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio, 547cc3c9df8SEd Maste off_t offset, int flags) 548bcd9e0ddSJohn Baldwin { 549bcd9e0ddSJohn Baldwin ssize_t cnt; 550552afd9cSPoul-Henning Kamp int error; 551df8bae1dSRodney W. Grimes #ifdef KTRACE 552552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 553df8bae1dSRodney W. Grimes #endif 554df8bae1dSRodney W. Grimes 55551d1f690SRobert Watson AUDIT_ARG_FD(fd); 556552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 557552afd9cSPoul-Henning Kamp auio->uio_td = td; 558bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 559df8bae1dSRodney W. Grimes #ifdef KTRACE 560552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 561552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 562df8bae1dSRodney W. Grimes #endif 563552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 564bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 565552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 566df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 567df8bae1dSRodney W. Grimes error = 0; 568bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5696f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 570b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 5717a6f3d78SJohn Baldwin tdsignal(td, SIGPIPE); 572b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 57319eb87d2SJohn Baldwin } 574df8bae1dSRodney W. Grimes } 575552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 576df8bae1dSRodney W. Grimes #ifdef KTRACE 577552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 578552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 579b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 580df8bae1dSRodney W. Grimes } 581df8bae1dSRodney W. Grimes #endif 582b40ce416SJulian Elischer td->td_retval[0] = cnt; 583df8bae1dSRodney W. Grimes return (error); 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes 586e4650294SJohn Baldwin /* 587e4650294SJohn Baldwin * Truncate a file given a file descriptor. 588e4650294SJohn Baldwin * 589e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 590e4650294SJohn Baldwin * descriptor isn't writable. 591e4650294SJohn Baldwin */ 592e4650294SJohn Baldwin int 593cc3c9df8SEd Maste kern_ftruncate(struct thread *td, int fd, off_t length) 594e4650294SJohn Baldwin { 595e4650294SJohn Baldwin struct file *fp; 596e4650294SJohn Baldwin int error; 597e4650294SJohn Baldwin 59814961ba7SRobert Watson AUDIT_ARG_FD(fd); 599e4650294SJohn Baldwin if (length < 0) 600e4650294SJohn Baldwin return (EINVAL); 601cbd92ce6SMatt Macy error = fget(td, fd, &cap_ftruncate_rights, &fp); 602e4650294SJohn Baldwin if (error) 603e4650294SJohn Baldwin return (error); 60414961ba7SRobert Watson AUDIT_ARG_FILE(td->td_proc, fp); 605e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 606e4650294SJohn Baldwin fdrop(fp, td); 607e4650294SJohn Baldwin return (EINVAL); 608e4650294SJohn Baldwin } 609e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 610e4650294SJohn Baldwin fdrop(fp, td); 611e4650294SJohn Baldwin return (error); 612e4650294SJohn Baldwin } 613e4650294SJohn Baldwin 614e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 615e4650294SJohn Baldwin struct ftruncate_args { 616e4650294SJohn Baldwin int fd; 617e4650294SJohn Baldwin int pad; 618e4650294SJohn Baldwin off_t length; 619e4650294SJohn Baldwin }; 620e4650294SJohn Baldwin #endif 621e4650294SJohn Baldwin int 622cc3c9df8SEd Maste sys_ftruncate(struct thread *td, struct ftruncate_args *uap) 623e4650294SJohn Baldwin { 624e4650294SJohn Baldwin 625e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 626e4650294SJohn Baldwin } 627e4650294SJohn Baldwin 628e4650294SJohn Baldwin #if defined(COMPAT_43) 629e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 630e4650294SJohn Baldwin struct oftruncate_args { 631e4650294SJohn Baldwin int fd; 632e4650294SJohn Baldwin long length; 633e4650294SJohn Baldwin }; 634e4650294SJohn Baldwin #endif 635e4650294SJohn Baldwin int 636cc3c9df8SEd Maste oftruncate(struct thread *td, struct oftruncate_args *uap) 637e4650294SJohn Baldwin { 638e4650294SJohn Baldwin 639e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 640e4650294SJohn Baldwin } 641e4650294SJohn Baldwin #endif /* COMPAT_43 */ 642e4650294SJohn Baldwin 643d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 644df8bae1dSRodney W. Grimes struct ioctl_args { 645df8bae1dSRodney W. Grimes int fd; 646069e9bc1SDoug Rabson u_long com; 647df8bae1dSRodney W. Grimes caddr_t data; 648df8bae1dSRodney W. Grimes }; 649d2d3e875SBruce Evans #endif 650df8bae1dSRodney W. Grimes /* ARGSUSED */ 65126f9a767SRodney W. Grimes int 6528451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap) 653df8bae1dSRodney W. Grimes { 65450ae6690SHans Petter Selasky u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN); 655a90fb6cfSHans Petter Selasky uint32_t com; 6569fddcc66SRuslan Ermilov int arg, error; 6573e15c66fSPoul-Henning Kamp u_int size; 6589fddcc66SRuslan Ermilov caddr_t data; 659df8bae1dSRodney W. Grimes 660a90fb6cfSHans Petter Selasky #ifdef INVARIANTS 6619fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6629fc6aa06SPoul-Henning Kamp printf( 6639fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 664431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6659fc6aa06SPoul-Henning Kamp } 666a90fb6cfSHans Petter Selasky #endif 667a90fb6cfSHans Petter Selasky com = (uint32_t)uap->com; 668df8bae1dSRodney W. Grimes 669df8bae1dSRodney W. Grimes /* 670df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 671df8bae1dSRodney W. Grimes * copied to/from the user's address space. 672df8bae1dSRodney W. Grimes */ 673df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 674ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 675ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6762de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6772de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6782de92a38SPeter Wemm #else 6792de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6802de92a38SPeter Wemm #endif 6819fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 682426da3bcSAlfred Perlstein return (ENOTTY); 683279d7226SMatthew Dillon 684ca51b19bSPoul-Henning Kamp if (size > 0) { 685715457f6SDavid E. O'Brien if (com & IOC_VOID) { 6869fddcc66SRuslan Ermilov /* Integer argument. */ 6879fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6889fddcc66SRuslan Ermilov data = (void *)&arg; 6899fddcc66SRuslan Ermilov size = 0; 6905cbf44bfSMateusz Guzik } else { 6910ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 692715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 6930ecd606bSHans Petter Selasky else 6940ecd606bSHans Petter Selasky data = smalldata; 6955cbf44bfSMateusz Guzik } 6969fddcc66SRuslan Ermilov } else 6979fddcc66SRuslan Ermilov data = (void *)&uap->data; 698df8bae1dSRodney W. Grimes if (com & IOC_IN) { 699df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 7005cbf44bfSMateusz Guzik if (error != 0) 7015cbf44bfSMateusz Guzik goto out; 702ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 703df8bae1dSRodney W. Grimes /* 704df8bae1dSRodney W. Grimes * Zero the buffer so the user always 705df8bae1dSRodney W. Grimes * gets back something deterministic. 706df8bae1dSRodney W. Grimes */ 707df8bae1dSRodney W. Grimes bzero(data, size); 708279d7226SMatthew Dillon } 709df8bae1dSRodney W. Grimes 710d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 711d9f46233SJohn Baldwin 712d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 713d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 714d9f46233SJohn Baldwin 7155cbf44bfSMateusz Guzik out: 7160ecd606bSHans Petter Selasky if (size > SYS_IOCTL_SMALL_SIZE) 7179fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 718d9f46233SJohn Baldwin return (error); 719d9f46233SJohn Baldwin } 720d9f46233SJohn Baldwin 721d9f46233SJohn Baldwin int 722d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 723d9f46233SJohn Baldwin { 724d9f46233SJohn Baldwin struct file *fp; 725d9f46233SJohn Baldwin struct filedesc *fdp; 7262609222aSPawel Jakub Dawidek int error, tmp, locked; 727d9f46233SJohn Baldwin 72864c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 72964c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 7302609222aSPawel Jakub Dawidek 731d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 7322609222aSPawel Jakub Dawidek 733d9f46233SJohn Baldwin switch (com) { 734d9f46233SJohn Baldwin case FIONCLEX: 735d9f46233SJohn Baldwin case FIOCLEX: 7365e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 7372609222aSPawel Jakub Dawidek locked = LA_XLOCKED; 7382609222aSPawel Jakub Dawidek break; 7392609222aSPawel Jakub Dawidek default: 7402609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7412609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp); 7422609222aSPawel Jakub Dawidek locked = LA_SLOCKED; 7432609222aSPawel Jakub Dawidek #else 7442609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7452609222aSPawel Jakub Dawidek #endif 7462609222aSPawel Jakub Dawidek break; 7472609222aSPawel Jakub Dawidek } 7482609222aSPawel Jakub Dawidek 7492609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 7502609222aSPawel Jakub Dawidek if ((fp = fget_locked(fdp, fd)) == NULL) { 7512609222aSPawel Jakub Dawidek error = EBADF; 7522609222aSPawel Jakub Dawidek goto out; 7532609222aSPawel Jakub Dawidek } 7542609222aSPawel Jakub Dawidek if ((error = cap_ioctl_check(fdp, fd, com)) != 0) { 7552609222aSPawel Jakub Dawidek fp = NULL; /* fhold() was not called yet */ 7562609222aSPawel Jakub Dawidek goto out; 7572609222aSPawel Jakub Dawidek } 758f1cf2b9dSKonstantin Belousov if (!fhold(fp)) { 759f1cf2b9dSKonstantin Belousov error = EBADF; 760f1cf2b9dSKonstantin Belousov fp = NULL; 761f1cf2b9dSKonstantin Belousov goto out; 762f1cf2b9dSKonstantin Belousov } 7632609222aSPawel Jakub Dawidek if (locked == LA_SLOCKED) { 7642609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 7652609222aSPawel Jakub Dawidek locked = LA_UNLOCKED; 7662609222aSPawel Jakub Dawidek } 7672609222aSPawel Jakub Dawidek #else 768cbd92ce6SMatt Macy error = fget(td, fd, &cap_ioctl_rights, &fp); 7697008be5bSPawel Jakub Dawidek if (error != 0) { 7702609222aSPawel Jakub Dawidek fp = NULL; 7712609222aSPawel Jakub Dawidek goto out; 7722609222aSPawel Jakub Dawidek } 7732609222aSPawel Jakub Dawidek #endif 7742609222aSPawel Jakub Dawidek if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 7752609222aSPawel Jakub Dawidek error = EBADF; 7762609222aSPawel Jakub Dawidek goto out; 7772609222aSPawel Jakub Dawidek } 7782609222aSPawel Jakub Dawidek 7792609222aSPawel Jakub Dawidek switch (com) { 7802609222aSPawel Jakub Dawidek case FIONCLEX: 7812609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE; 7822609222aSPawel Jakub Dawidek goto out; 7832609222aSPawel Jakub Dawidek case FIOCLEX: 7842609222aSPawel Jakub Dawidek fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE; 785d9f46233SJohn Baldwin goto out; 786d9f46233SJohn Baldwin case FIONBIO: 787bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 788397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 789df8bae1dSRodney W. Grimes else 790397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7918ccf264fSPoul-Henning Kamp data = (void *)&tmp; 792d9f46233SJohn Baldwin break; 793d9f46233SJohn Baldwin case FIOASYNC: 794bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 795397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 796df8bae1dSRodney W. Grimes else 797397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7988ccf264fSPoul-Henning Kamp data = (void *)&tmp; 799d9f46233SJohn Baldwin break; 800df8bae1dSRodney W. Grimes } 8018ccf264fSPoul-Henning Kamp 8028ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 803d9f46233SJohn Baldwin out: 8042609222aSPawel Jakub Dawidek switch (locked) { 8052609222aSPawel Jakub Dawidek case LA_XLOCKED: 8062609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp); 8072609222aSPawel Jakub Dawidek break; 8082609222aSPawel Jakub Dawidek #ifdef CAPABILITIES 8092609222aSPawel Jakub Dawidek case LA_SLOCKED: 8102609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp); 8112609222aSPawel Jakub Dawidek break; 8122609222aSPawel Jakub Dawidek #endif 8132609222aSPawel Jakub Dawidek default: 8142609222aSPawel Jakub Dawidek FILEDESC_UNLOCK_ASSERT(fdp); 8152609222aSPawel Jakub Dawidek break; 8162609222aSPawel Jakub Dawidek } 8172609222aSPawel Jakub Dawidek if (fp != NULL) 818b40ce416SJulian Elischer fdrop(fp, td); 819df8bae1dSRodney W. Grimes return (error); 820df8bae1dSRodney W. Grimes } 821df8bae1dSRodney W. Grimes 822125dcf8cSKonstantin Belousov int 8232856d85eSKyle Evans sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap) 8242856d85eSKyle Evans { 8252856d85eSKyle Evans int error; 8262856d85eSKyle Evans 8272856d85eSKyle Evans error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len); 8282856d85eSKyle Evans return (kern_posix_error(td, error)); 8292856d85eSKyle Evans } 8302856d85eSKyle Evans 8312856d85eSKyle Evans int 8322856d85eSKyle Evans kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len) 8332856d85eSKyle Evans { 8342856d85eSKyle Evans struct file *fp; 8352856d85eSKyle Evans int error; 8362856d85eSKyle Evans 8372856d85eSKyle Evans AUDIT_ARG_FD(fd); 8382856d85eSKyle Evans if (offset < 0 || len <= 0) 8392856d85eSKyle Evans return (EINVAL); 8402856d85eSKyle Evans /* Check for wrap. */ 8412856d85eSKyle Evans if (offset > OFF_MAX - len) 8422856d85eSKyle Evans return (EFBIG); 8432856d85eSKyle Evans AUDIT_ARG_FD(fd); 8442856d85eSKyle Evans error = fget(td, fd, &cap_pwrite_rights, &fp); 8452856d85eSKyle Evans if (error != 0) 8462856d85eSKyle Evans return (error); 8472856d85eSKyle Evans AUDIT_ARG_FILE(td->td_proc, fp); 8482856d85eSKyle Evans if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) { 8492856d85eSKyle Evans error = ESPIPE; 8502856d85eSKyle Evans goto out; 8512856d85eSKyle Evans } 8522856d85eSKyle Evans if ((fp->f_flag & FWRITE) == 0) { 8532856d85eSKyle Evans error = EBADF; 8542856d85eSKyle Evans goto out; 8552856d85eSKyle Evans } 8562856d85eSKyle Evans 8572856d85eSKyle Evans error = fo_fallocate(fp, offset, len, td); 8582856d85eSKyle Evans out: 8592856d85eSKyle Evans fdrop(fp, td); 8602856d85eSKyle Evans return (error); 8612856d85eSKyle Evans } 8622856d85eSKyle Evans 8632856d85eSKyle Evans int 864125dcf8cSKonstantin Belousov poll_no_poll(int events) 865125dcf8cSKonstantin Belousov { 866125dcf8cSKonstantin Belousov /* 867125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 868125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 869125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 870125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 871125dcf8cSKonstantin Belousov * of specific filesystem implementations. 872125dcf8cSKonstantin Belousov */ 873125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 874125dcf8cSKonstantin Belousov return (POLLNVAL); 875125dcf8cSKonstantin Belousov 876125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 877125dcf8cSKonstantin Belousov } 878125dcf8cSKonstantin Belousov 879066d836bSKonstantin Belousov int 8808451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap) 881066d836bSKonstantin Belousov { 882066d836bSKonstantin Belousov struct timespec ts; 883066d836bSKonstantin Belousov struct timeval tv, *tvp; 884066d836bSKonstantin Belousov sigset_t set, *uset; 885066d836bSKonstantin Belousov int error; 886066d836bSKonstantin Belousov 887066d836bSKonstantin Belousov if (uap->ts != NULL) { 888066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 889066d836bSKonstantin Belousov if (error != 0) 890066d836bSKonstantin Belousov return (error); 891066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 892066d836bSKonstantin Belousov tvp = &tv; 893066d836bSKonstantin Belousov } else 894066d836bSKonstantin Belousov tvp = NULL; 895066d836bSKonstantin Belousov if (uap->sm != NULL) { 896066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 897066d836bSKonstantin Belousov if (error != 0) 898066d836bSKonstantin Belousov return (error); 899066d836bSKonstantin Belousov uset = &set; 900066d836bSKonstantin Belousov } else 901066d836bSKonstantin Belousov uset = NULL; 902066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 903066d836bSKonstantin Belousov uset, NFDBITS)); 904066d836bSKonstantin Belousov } 905066d836bSKonstantin Belousov 906066d836bSKonstantin Belousov int 907066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 908066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 909066d836bSKonstantin Belousov { 910066d836bSKonstantin Belousov int error; 911066d836bSKonstantin Belousov 912066d836bSKonstantin Belousov if (uset != NULL) { 913066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 914066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 915066d836bSKonstantin Belousov if (error != 0) 916066d836bSKonstantin Belousov return (error); 917066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 918066d836bSKonstantin Belousov /* 919066d836bSKonstantin Belousov * Make sure that ast() is called on return to 920066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 921066d836bSKonstantin Belousov * sigmask. 922066d836bSKonstantin Belousov */ 923066d836bSKonstantin Belousov thread_lock(td); 924066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 925066d836bSKonstantin Belousov thread_unlock(td); 926066d836bSKonstantin Belousov } 927066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 928066d836bSKonstantin Belousov return (error); 929066d836bSKonstantin Belousov } 930066d836bSKonstantin Belousov 931d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 932df8bae1dSRodney W. Grimes struct select_args { 933b08f7993SSujal Patel int nd; 934df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 935df8bae1dSRodney W. Grimes struct timeval *tv; 936df8bae1dSRodney W. Grimes }; 937d2d3e875SBruce Evans #endif 93826f9a767SRodney W. Grimes int 9398451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap) 940df8bae1dSRodney W. Grimes { 9418f19eb88SIan Dowse struct timeval tv, *tvp; 9428f19eb88SIan Dowse int error; 9438f19eb88SIan Dowse 9448f19eb88SIan Dowse if (uap->tv != NULL) { 9458f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 9468f19eb88SIan Dowse if (error) 9478f19eb88SIan Dowse return (error); 9488f19eb88SIan Dowse tvp = &tv; 9498f19eb88SIan Dowse } else 9508f19eb88SIan Dowse tvp = NULL; 9518f19eb88SIan Dowse 952b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 953b55ef216SKonstantin Belousov NFDBITS)); 9548f19eb88SIan Dowse } 9558f19eb88SIan Dowse 95656be1b9aSKonstantin Belousov /* 95756be1b9aSKonstantin Belousov * In the unlikely case when user specified n greater then the last 95856be1b9aSKonstantin Belousov * open file descriptor, check that no bits are set after the last 95956be1b9aSKonstantin Belousov * valid fd. We must return EBADF if any is set. 96056be1b9aSKonstantin Belousov * 96156be1b9aSKonstantin Belousov * There are applications that rely on the behaviour. 96256be1b9aSKonstantin Belousov * 963d8bc2a17SMateusz Guzik * nd is fd_nfiles. 96456be1b9aSKonstantin Belousov */ 96556be1b9aSKonstantin Belousov static int 96656be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits) 96756be1b9aSKonstantin Belousov { 96856be1b9aSKonstantin Belousov char *addr, *oaddr; 96956be1b9aSKonstantin Belousov int b, i, res; 97056be1b9aSKonstantin Belousov uint8_t bits; 97156be1b9aSKonstantin Belousov 97256be1b9aSKonstantin Belousov if (nd >= ndu || fd_in == NULL) 97356be1b9aSKonstantin Belousov return (0); 97456be1b9aSKonstantin Belousov 97556be1b9aSKonstantin Belousov oaddr = NULL; 97656be1b9aSKonstantin Belousov bits = 0; /* silence gcc */ 97756be1b9aSKonstantin Belousov for (i = nd; i < ndu; i++) { 97856be1b9aSKonstantin Belousov b = i / NBBY; 97956be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 98056be1b9aSKonstantin Belousov addr = (char *)fd_in + b; 98156be1b9aSKonstantin Belousov #else 98256be1b9aSKonstantin Belousov addr = (char *)fd_in; 98356be1b9aSKonstantin Belousov if (abi_nfdbits == NFDBITS) { 98456be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(fd_mask)) + 98556be1b9aSKonstantin Belousov sizeof(fd_mask) - 1 - b % sizeof(fd_mask); 98656be1b9aSKonstantin Belousov } else { 98756be1b9aSKonstantin Belousov addr += rounddown(b, sizeof(uint32_t)) + 98856be1b9aSKonstantin Belousov sizeof(uint32_t) - 1 - b % sizeof(uint32_t); 98956be1b9aSKonstantin Belousov } 99056be1b9aSKonstantin Belousov #endif 99156be1b9aSKonstantin Belousov if (addr != oaddr) { 99256be1b9aSKonstantin Belousov res = fubyte(addr); 99356be1b9aSKonstantin Belousov if (res == -1) 99456be1b9aSKonstantin Belousov return (EFAULT); 99556be1b9aSKonstantin Belousov oaddr = addr; 99656be1b9aSKonstantin Belousov bits = res; 99756be1b9aSKonstantin Belousov } 99856be1b9aSKonstantin Belousov if ((bits & (1 << (i % NBBY))) != 0) 99956be1b9aSKonstantin Belousov return (EBADF); 100056be1b9aSKonstantin Belousov } 100156be1b9aSKonstantin Belousov return (0); 100256be1b9aSKonstantin Belousov } 100356be1b9aSKonstantin Belousov 10048f19eb88SIan Dowse int 10058f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 1006b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 10078f19eb88SIan Dowse { 1008426da3bcSAlfred Perlstein struct filedesc *fdp; 1009d5e4d7e1SBruce Evans /* 1010d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 1011d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 1012d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 1013d5e4d7e1SBruce Evans * of 256. 1014d5e4d7e1SBruce Evans */ 1015d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 1016eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 1017cf5e4fe6SDavide Italiano struct timeval rtv; 1018cf5e4fe6SDavide Italiano sbintime_t asbt, precision, rsbt; 1019b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 1020cf5e4fe6SDavide Italiano int error, lf, ndu; 1021df8bae1dSRodney W. Grimes 10228f19eb88SIan Dowse if (nd < 0) 1023acbfbfeaSSujal Patel return (EINVAL); 1024426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 102556be1b9aSKonstantin Belousov ndu = nd; 1026d8bc2a17SMateusz Guzik lf = fdp->fd_nfiles; 1027d8bc2a17SMateusz Guzik if (nd > lf) 1028d8bc2a17SMateusz Guzik nd = lf; 102956be1b9aSKonstantin Belousov 103056be1b9aSKonstantin Belousov error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); 103156be1b9aSKonstantin Belousov if (error != 0) 103256be1b9aSKonstantin Belousov return (error); 103356be1b9aSKonstantin Belousov error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); 103456be1b9aSKonstantin Belousov if (error != 0) 103556be1b9aSKonstantin Belousov return (error); 103656be1b9aSKonstantin Belousov error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); 103756be1b9aSKonstantin Belousov if (error != 0) 103856be1b9aSKonstantin Belousov return (error); 1039b08f7993SSujal Patel 1040d5e4d7e1SBruce Evans /* 1041d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 1042d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 1043d5e4d7e1SBruce Evans */ 10448f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 1045d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 1046b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 1047d5e4d7e1SBruce Evans nbufbytes = 0; 10488f19eb88SIan Dowse if (fd_in != NULL) 1049d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10508f19eb88SIan Dowse if (fd_ou != NULL) 1051d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 10528f19eb88SIan Dowse if (fd_ex != NULL) 1053d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 1054d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 1055d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 1056d5e4d7e1SBruce Evans else 1057a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 1058b08f7993SSujal Patel 1059b08f7993SSujal Patel /* 1060d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 1061d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 1062d5e4d7e1SBruce Evans * together. 1063b08f7993SSujal Patel */ 1064d5e4d7e1SBruce Evans sbp = selbits; 1065df8bae1dSRodney W. Grimes #define getbits(name, x) \ 1066d5e4d7e1SBruce Evans do { \ 1067841c0c7eSNathan Whitehorn if (name == NULL) { \ 1068d5e4d7e1SBruce Evans ibits[x] = NULL; \ 1069841c0c7eSNathan Whitehorn obits[x] = NULL; \ 1070841c0c7eSNathan Whitehorn } else { \ 1071d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 1072d5e4d7e1SBruce Evans obits[x] = sbp; \ 1073d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 1074b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 1075265fc98fSSeigo Tanimura if (error != 0) \ 1076ace8398dSJeff Roberson goto done; \ 1077d6fda03aSMateusz Guzik if (ncpbytes != ncpubytes) \ 1078b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 1079b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 1080e04ac2feSJohn Baldwin } \ 1081d5e4d7e1SBruce Evans } while (0) 10828f19eb88SIan Dowse getbits(fd_in, 0); 10838f19eb88SIan Dowse getbits(fd_ou, 1); 10848f19eb88SIan Dowse getbits(fd_ex, 2); 1085df8bae1dSRodney W. Grimes #undef getbits 1086841c0c7eSNathan Whitehorn 1087841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__) 1088841c0c7eSNathan Whitehorn /* 1089841c0c7eSNathan Whitehorn * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS, 1090841c0c7eSNathan Whitehorn * we are running under 32-bit emulation. This should be more 1091841c0c7eSNathan Whitehorn * generic. 1092841c0c7eSNathan Whitehorn */ 1093841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) \ 1094841c0c7eSNathan Whitehorn if (abi_nfdbits != NFDBITS && bits != NULL) { \ 1095841c0c7eSNathan Whitehorn int i; \ 1096841c0c7eSNathan Whitehorn for (i = 0; i < ncpbytes / sizeof *sbp; i++) \ 1097841c0c7eSNathan Whitehorn bits[i] = (bits[i] >> 32) | (bits[i] << 32); \ 1098841c0c7eSNathan Whitehorn } 1099841c0c7eSNathan Whitehorn #else 1100841c0c7eSNathan Whitehorn #define swizzle_fdset(bits) 1101841c0c7eSNathan Whitehorn #endif 1102841c0c7eSNathan Whitehorn 1103841c0c7eSNathan Whitehorn /* Make sure the bit order makes it through an ABI transition */ 1104841c0c7eSNathan Whitehorn swizzle_fdset(ibits[0]); 1105841c0c7eSNathan Whitehorn swizzle_fdset(ibits[1]); 1106841c0c7eSNathan Whitehorn swizzle_fdset(ibits[2]); 1107841c0c7eSNathan Whitehorn 1108d5e4d7e1SBruce Evans if (nbufbytes != 0) 1109d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 1110df8bae1dSRodney W. Grimes 1111cf5e4fe6SDavide Italiano precision = 0; 11128f19eb88SIan Dowse if (tvp != NULL) { 1113cf5e4fe6SDavide Italiano rtv = *tvp; 1114cf5e4fe6SDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1115cf5e4fe6SDavide Italiano rtv.tv_usec >= 1000000) { 1116df8bae1dSRodney W. Grimes error = EINVAL; 1117ace8398dSJeff Roberson goto done; 1118df8bae1dSRodney W. Grimes } 111921a37a71SAlexander Motin if (!timevalisset(&rtv)) 1120980c545dSAlexander Motin asbt = 0; 112121a37a71SAlexander Motin else if (rtv.tv_sec <= INT32_MAX) { 1122cf5e4fe6SDavide Italiano rsbt = tvtosbt(rtv); 1123cf5e4fe6SDavide Italiano precision = rsbt; 1124cf5e4fe6SDavide Italiano precision >>= tc_precexp; 1125cf5e4fe6SDavide Italiano if (TIMESEL(&asbt, rsbt)) 1126cf5e4fe6SDavide Italiano asbt += tc_tick_sbt; 11274bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1128cf5e4fe6SDavide Italiano asbt += rsbt; 112921a37a71SAlexander Motin else 1130980c545dSAlexander Motin asbt = -1; 1131980c545dSAlexander Motin } else 1132980c545dSAlexander Motin asbt = -1; 1133cf5e4fe6SDavide Italiano } else 1134cf5e4fe6SDavide Italiano asbt = -1; 1135ace8398dSJeff Roberson seltdinit(td); 1136ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1137ace8398dSJeff Roberson for (;;) { 11388f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 1139ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1140ace8398dSJeff Roberson break; 1141cf5e4fe6SDavide Italiano error = seltdwait(td, asbt, precision); 1142ace8398dSJeff Roberson if (error) 1143ace8398dSJeff Roberson break; 1144ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 1145ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1146ace8398dSJeff Roberson break; 114785f190e4SAlfred Perlstein } 1148ace8398dSJeff Roberson seltdclear(td); 1149265fc98fSSeigo Tanimura 1150df8bae1dSRodney W. Grimes done: 1151df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 1152df8bae1dSRodney W. Grimes if (error == ERESTART) 1153df8bae1dSRodney W. Grimes error = EINTR; 1154df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 1155df8bae1dSRodney W. Grimes error = 0; 1156841c0c7eSNathan Whitehorn 1157841c0c7eSNathan Whitehorn /* swizzle bit order back, if necessary */ 1158841c0c7eSNathan Whitehorn swizzle_fdset(obits[0]); 1159841c0c7eSNathan Whitehorn swizzle_fdset(obits[1]); 1160841c0c7eSNathan Whitehorn swizzle_fdset(obits[2]); 1161841c0c7eSNathan Whitehorn #undef swizzle_fdset 1162841c0c7eSNathan Whitehorn 1163df8bae1dSRodney W. Grimes #define putbits(name, x) \ 1164b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 1165df8bae1dSRodney W. Grimes error = error2; 1166df8bae1dSRodney W. Grimes if (error == 0) { 1167df8bae1dSRodney W. Grimes int error2; 1168df8bae1dSRodney W. Grimes 11698f19eb88SIan Dowse putbits(fd_in, 0); 11708f19eb88SIan Dowse putbits(fd_ou, 1); 11718f19eb88SIan Dowse putbits(fd_ex, 2); 1172df8bae1dSRodney W. Grimes #undef putbits 1173df8bae1dSRodney W. Grimes } 1174d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 1175d5e4d7e1SBruce Evans free(selbits, M_SELECT); 1176ad2edad9SMatthew Dillon 1177df8bae1dSRodney W. Grimes return (error); 1178df8bae1dSRodney W. Grimes } 117911b763dfSJeff Roberson /* 118011b763dfSJeff Roberson * Convert a select bit set to poll flags. 1181748b9df6SJeff Roberson * 118211b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 118311b763dfSJeff Roberson * return this as a set bit in any set. 118411b763dfSJeff Roberson */ 118511b763dfSJeff Roberson static int select_flags[3] = { 118611b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 118711b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 118861e53a38SKonstantin Belousov POLLRDBAND | POLLERR 118911b763dfSJeff Roberson }; 119011b763dfSJeff Roberson 119111b763dfSJeff Roberson /* 119211b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 119311b763dfSJeff Roberson * bit position in the fd_mask array. 119411b763dfSJeff Roberson */ 119511b763dfSJeff Roberson static __inline int 119660b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 119711b763dfSJeff Roberson { 119811b763dfSJeff Roberson int flags; 119911b763dfSJeff Roberson int msk; 120011b763dfSJeff Roberson 120111b763dfSJeff Roberson flags = 0; 120211b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 120311b763dfSJeff Roberson if (ibits[msk] == NULL) 120411b763dfSJeff Roberson continue; 120560b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 120611b763dfSJeff Roberson continue; 120711b763dfSJeff Roberson flags |= select_flags[msk]; 120811b763dfSJeff Roberson } 120911b763dfSJeff Roberson return (flags); 121011b763dfSJeff Roberson } 121111b763dfSJeff Roberson 121211b763dfSJeff Roberson /* 121311b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 121411b763dfSJeff Roberson * input bits originally requested. 121511b763dfSJeff Roberson */ 121611b763dfSJeff Roberson static __inline int 121711b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 121811b763dfSJeff Roberson { 121911b763dfSJeff Roberson int msk; 122011b763dfSJeff Roberson int n; 122111b763dfSJeff Roberson 122211b763dfSJeff Roberson n = 0; 122311b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 122411b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 122511b763dfSJeff Roberson continue; 122611b763dfSJeff Roberson if (ibits[msk] == NULL) 122711b763dfSJeff Roberson continue; 122811b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 122911b763dfSJeff Roberson continue; 123011b763dfSJeff Roberson /* 123111b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 123211b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 123311b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 123411b763dfSJeff Roberson * call selsetbits twice as a result. 123511b763dfSJeff Roberson */ 123611b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 123711b763dfSJeff Roberson continue; 123811b763dfSJeff Roberson obits[msk][idx] |= bit; 123911b763dfSJeff Roberson n++; 124011b763dfSJeff Roberson } 124111b763dfSJeff Roberson 124211b763dfSJeff Roberson return (n); 124311b763dfSJeff Roberson } 1244df8bae1dSRodney W. Grimes 1245a9d2f8d8SRobert Watson static __inline int 1246a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp) 1247a9d2f8d8SRobert Watson { 1248a9d2f8d8SRobert Watson 124952604ed7SMateusz Guzik return (fget_unlocked(fdp, fd, &cap_event_rights, fpp)); 1250a9d2f8d8SRobert Watson } 1251a9d2f8d8SRobert Watson 1252ace8398dSJeff Roberson /* 1253ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1254ace8398dSJeff Roberson * completion. 1255ace8398dSJeff Roberson */ 1256ace8398dSJeff Roberson static int 1257ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1258ace8398dSJeff Roberson { 125911b763dfSJeff Roberson struct filedesc *fdp; 126011b763dfSJeff Roberson struct selinfo *si; 1261ace8398dSJeff Roberson struct seltd *stp; 1262ace8398dSJeff Roberson struct selfd *sfp; 1263ace8398dSJeff Roberson struct selfd *sfn; 1264ace8398dSJeff Roberson struct file *fp; 12659cdacff1SJeff Roberson fd_mask bit; 12669cdacff1SJeff Roberson int fd, ev, n, idx; 1267a9d2f8d8SRobert Watson int error; 1268ace8398dSJeff Roberson 126911b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1270ace8398dSJeff Roberson stp = td->td_sel; 127111b763dfSJeff Roberson n = 0; 1272ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1273ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1274ace8398dSJeff Roberson si = sfp->sf_si; 1275ace8398dSJeff Roberson selfdfree(stp, sfp); 1276ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1277ace8398dSJeff Roberson if (si != NULL) 1278ace8398dSJeff Roberson continue; 1279a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1280a9d2f8d8SRobert Watson if (error) 1281a9d2f8d8SRobert Watson return (error); 128211b763dfSJeff Roberson idx = fd / NFDBITS; 12839cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 128411b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1285bf422e5fSJeff Roberson fdrop(fp, td); 128611b763dfSJeff Roberson if (ev != 0) 128711b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1288ace8398dSJeff Roberson } 1289ace8398dSJeff Roberson stp->st_flags = 0; 1290ace8398dSJeff Roberson td->td_retval[0] = n; 1291ace8398dSJeff Roberson return (0); 1292ace8398dSJeff Roberson } 1293ace8398dSJeff Roberson 1294ace8398dSJeff Roberson /* 1295ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1296ace8398dSJeff Roberson * each selinfo. 1297ace8398dSJeff Roberson */ 1298265fc98fSSeigo Tanimura static int 1299cc3c9df8SEd Maste selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd) 1300df8bae1dSRodney W. Grimes { 130111b763dfSJeff Roberson struct filedesc *fdp; 1302df8bae1dSRodney W. Grimes struct file *fp; 13039cdacff1SJeff Roberson fd_mask bit; 130411b763dfSJeff Roberson int ev, flags, end, fd; 13059cdacff1SJeff Roberson int n, idx; 1306a9d2f8d8SRobert Watson int error; 1307df8bae1dSRodney W. Grimes 130811b763dfSJeff Roberson fdp = td->td_proc->p_fd; 130911b763dfSJeff Roberson n = 0; 13109cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 131111b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 131211b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 131311b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 131411b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 131511b763dfSJeff Roberson if (flags == 0) 1316f082218cSPeter Wemm continue; 1317a9d2f8d8SRobert Watson error = getselfd_cap(fdp, fd, &fp); 1318a9d2f8d8SRobert Watson if (error) 1319a9d2f8d8SRobert Watson return (error); 1320ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 132111b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1322bf422e5fSJeff Roberson fdrop(fp, td); 132311b763dfSJeff Roberson if (ev != 0) 132411b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1325df8bae1dSRodney W. Grimes } 1326df8bae1dSRodney W. Grimes } 132711b763dfSJeff Roberson 1328b40ce416SJulian Elischer td->td_retval[0] = n; 1329df8bae1dSRodney W. Grimes return (0); 1330df8bae1dSRodney W. Grimes } 1331df8bae1dSRodney W. Grimes 133242d11757SPeter Wemm int 1333186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap) 1334186d9c34SDmitry Chagin { 1335186d9c34SDmitry Chagin struct timespec ts, *tsp; 1336186d9c34SDmitry Chagin 1337186d9c34SDmitry Chagin if (uap->timeout != INFTIM) { 1338186d9c34SDmitry Chagin if (uap->timeout < 0) 1339186d9c34SDmitry Chagin return (EINVAL); 1340186d9c34SDmitry Chagin ts.tv_sec = uap->timeout / 1000; 1341186d9c34SDmitry Chagin ts.tv_nsec = (uap->timeout % 1000) * 1000000; 1342186d9c34SDmitry Chagin tsp = &ts; 1343186d9c34SDmitry Chagin } else 1344186d9c34SDmitry Chagin tsp = NULL; 1345186d9c34SDmitry Chagin 1346186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); 1347186d9c34SDmitry Chagin } 1348186d9c34SDmitry Chagin 1349186d9c34SDmitry Chagin int 13502384981bSConrad Meyer kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, 1351186d9c34SDmitry Chagin struct timespec *tsp, sigset_t *uset) 135242d11757SPeter Wemm { 13532384981bSConrad Meyer struct pollfd *kfds; 13542384981bSConrad Meyer struct pollfd stackfds[32]; 1355186d9c34SDmitry Chagin sbintime_t sbt, precision, tmp; 1356186d9c34SDmitry Chagin time_t over; 1357186d9c34SDmitry Chagin struct timespec ts; 1358cf5e4fe6SDavide Italiano int error; 135942d11757SPeter Wemm 1360186d9c34SDmitry Chagin precision = 0; 1361186d9c34SDmitry Chagin if (tsp != NULL) { 1362186d9c34SDmitry Chagin if (tsp->tv_sec < 0) 1363186d9c34SDmitry Chagin return (EINVAL); 1364186d9c34SDmitry Chagin if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000) 1365186d9c34SDmitry Chagin return (EINVAL); 1366186d9c34SDmitry Chagin if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1367186d9c34SDmitry Chagin sbt = 0; 1368186d9c34SDmitry Chagin else { 1369186d9c34SDmitry Chagin ts = *tsp; 1370186d9c34SDmitry Chagin if (ts.tv_sec > INT32_MAX / 2) { 1371186d9c34SDmitry Chagin over = ts.tv_sec - INT32_MAX / 2; 1372186d9c34SDmitry Chagin ts.tv_sec -= over; 1373186d9c34SDmitry Chagin } else 1374186d9c34SDmitry Chagin over = 0; 1375186d9c34SDmitry Chagin tmp = tstosbt(ts); 1376186d9c34SDmitry Chagin precision = tmp; 1377186d9c34SDmitry Chagin precision >>= tc_precexp; 1378186d9c34SDmitry Chagin if (TIMESEL(&sbt, tmp)) 1379186d9c34SDmitry Chagin sbt += tc_tick_sbt; 1380186d9c34SDmitry Chagin sbt += tmp; 1381186d9c34SDmitry Chagin } 1382186d9c34SDmitry Chagin } else 1383186d9c34SDmitry Chagin sbt = -1; 1384186d9c34SDmitry Chagin 138578c2a980SConrad Meyer /* 138678c2a980SConrad Meyer * This is kinda bogus. We have fd limits, but that is not 138778c2a980SConrad Meyer * really related to the size of the pollfd array. Make sure 138878c2a980SConrad Meyer * we let the process use at least FD_SETSIZE entries and at 138978c2a980SConrad Meyer * least enough for the system-wide limits. We want to be reasonably 139078c2a980SConrad Meyer * safe, but not overly restrictive. 139178c2a980SConrad Meyer */ 1392374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1393ace8398dSJeff Roberson return (EINVAL); 13942384981bSConrad Meyer if (nfds > nitems(stackfds)) 13952384981bSConrad Meyer kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); 139642d11757SPeter Wemm else 13972384981bSConrad Meyer kfds = stackfds; 13982384981bSConrad Meyer error = copyin(ufds, kfds, nfds * sizeof(*kfds)); 139942d11757SPeter Wemm if (error) 1400ace8398dSJeff Roberson goto done; 1401186d9c34SDmitry Chagin 1402186d9c34SDmitry Chagin if (uset != NULL) { 1403186d9c34SDmitry Chagin error = kern_sigprocmask(td, SIG_SETMASK, uset, 1404186d9c34SDmitry Chagin &td->td_oldsigmask, 0); 1405186d9c34SDmitry Chagin if (error) 1406ace8398dSJeff Roberson goto done; 1407186d9c34SDmitry Chagin td->td_pflags |= TDP_OLDMASK; 1408186d9c34SDmitry Chagin /* 1409186d9c34SDmitry Chagin * Make sure that ast() is called on return to 1410186d9c34SDmitry Chagin * usermode and TDP_OLDMASK is cleared, restoring old 1411186d9c34SDmitry Chagin * sigmask. 1412186d9c34SDmitry Chagin */ 1413186d9c34SDmitry Chagin thread_lock(td); 1414186d9c34SDmitry Chagin td->td_flags |= TDF_ASTPENDING; 1415186d9c34SDmitry Chagin thread_unlock(td); 141642d11757SPeter Wemm } 1417186d9c34SDmitry Chagin 1418ace8398dSJeff Roberson seltdinit(td); 1419ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1420ace8398dSJeff Roberson for (;;) { 14212384981bSConrad Meyer error = pollscan(td, kfds, nfds); 1422ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1423ace8398dSJeff Roberson break; 1424186d9c34SDmitry Chagin error = seltdwait(td, sbt, precision); 1425ace8398dSJeff Roberson if (error) 1426ace8398dSJeff Roberson break; 1427ace8398dSJeff Roberson error = pollrescan(td); 1428ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1429ace8398dSJeff Roberson break; 143085f190e4SAlfred Perlstein } 1431ace8398dSJeff Roberson seltdclear(td); 1432265fc98fSSeigo Tanimura 143342d11757SPeter Wemm done: 143442d11757SPeter Wemm /* poll is not restarted after signals... */ 143542d11757SPeter Wemm if (error == ERESTART) 143642d11757SPeter Wemm error = EINTR; 143742d11757SPeter Wemm if (error == EWOULDBLOCK) 143842d11757SPeter Wemm error = 0; 143942d11757SPeter Wemm if (error == 0) { 14402384981bSConrad Meyer error = pollout(td, kfds, ufds, nfds); 144142d11757SPeter Wemm if (error) 144242d11757SPeter Wemm goto out; 144342d11757SPeter Wemm } 144442d11757SPeter Wemm out: 14452384981bSConrad Meyer if (nfds > nitems(stackfds)) 14462384981bSConrad Meyer free(kfds, M_TEMP); 144742d11757SPeter Wemm return (error); 144842d11757SPeter Wemm } 144942d11757SPeter Wemm 1450186d9c34SDmitry Chagin int 1451186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap) 1452186d9c34SDmitry Chagin { 1453186d9c34SDmitry Chagin struct timespec ts, *tsp; 1454186d9c34SDmitry Chagin sigset_t set, *ssp; 1455186d9c34SDmitry Chagin int error; 1456186d9c34SDmitry Chagin 1457186d9c34SDmitry Chagin if (uap->ts != NULL) { 1458186d9c34SDmitry Chagin error = copyin(uap->ts, &ts, sizeof(ts)); 1459186d9c34SDmitry Chagin if (error) 1460186d9c34SDmitry Chagin return (error); 1461186d9c34SDmitry Chagin tsp = &ts; 1462186d9c34SDmitry Chagin } else 1463186d9c34SDmitry Chagin tsp = NULL; 1464186d9c34SDmitry Chagin if (uap->set != NULL) { 1465186d9c34SDmitry Chagin error = copyin(uap->set, &set, sizeof(set)); 1466186d9c34SDmitry Chagin if (error) 1467186d9c34SDmitry Chagin return (error); 1468186d9c34SDmitry Chagin ssp = &set; 1469186d9c34SDmitry Chagin } else 1470186d9c34SDmitry Chagin ssp = NULL; 1471186d9c34SDmitry Chagin /* 1472186d9c34SDmitry Chagin * fds is still a pointer to user space. kern_poll() will 1473186d9c34SDmitry Chagin * take care of copyin that array to the kernel space. 1474186d9c34SDmitry Chagin */ 1475186d9c34SDmitry Chagin 1476186d9c34SDmitry Chagin return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); 1477186d9c34SDmitry Chagin } 1478186d9c34SDmitry Chagin 1479b1607c87SMateusz Guzik #ifdef CAPABILITIES 1480b1607c87SMateusz Guzik static int 1481b1607c87SMateusz Guzik poll_fget(struct filedesc *fdp, int fd, struct file **fpp) 1482b1607c87SMateusz Guzik { 1483b1607c87SMateusz Guzik const struct filedescent *fde; 1484b1607c87SMateusz Guzik const struct fdescenttbl *fdt; 1485b1607c87SMateusz Guzik const cap_rights_t *haverights; 1486b1607c87SMateusz Guzik struct file *fp; 1487b1607c87SMateusz Guzik int error; 1488b1607c87SMateusz Guzik 1489b1607c87SMateusz Guzik if (__predict_false(fd >= fdp->fd_nfiles)) 1490b1607c87SMateusz Guzik return (EBADF); 1491b1607c87SMateusz Guzik 1492b1607c87SMateusz Guzik fdt = fdp->fd_files; 1493b1607c87SMateusz Guzik fde = &fdt->fdt_ofiles[fd]; 1494b1607c87SMateusz Guzik fp = fde->fde_file; 1495b1607c87SMateusz Guzik if (__predict_false(fp == NULL)) 1496b1607c87SMateusz Guzik return (EBADF); 1497b1607c87SMateusz Guzik haverights = cap_rights_fde_inline(fde); 1498b1607c87SMateusz Guzik error = cap_check_inline(haverights, &cap_event_rights); 1499b1607c87SMateusz Guzik if (__predict_false(error != 0)) 1500b1607c87SMateusz Guzik return (EBADF); 1501b1607c87SMateusz Guzik *fpp = fp; 1502b1607c87SMateusz Guzik return (0); 1503b1607c87SMateusz Guzik } 1504b1607c87SMateusz Guzik #else 1505b1607c87SMateusz Guzik static int 1506b1607c87SMateusz Guzik poll_fget(struct filedesc *fdp, int fd, struct file **fpp) 1507b1607c87SMateusz Guzik { 1508b1607c87SMateusz Guzik struct file *fp; 1509b1607c87SMateusz Guzik 1510b1607c87SMateusz Guzik if (__predict_false(fd >= fdp->fd_nfiles)) 1511b1607c87SMateusz Guzik return (EBADF); 1512b1607c87SMateusz Guzik 1513b1607c87SMateusz Guzik fp = fdp->fd_ofiles[fd].fde_file; 1514b1607c87SMateusz Guzik if (__predict_false(fp == NULL)) 1515b1607c87SMateusz Guzik return (EBADF); 1516b1607c87SMateusz Guzik 1517b1607c87SMateusz Guzik *fpp = fp; 1518b1607c87SMateusz Guzik return (0); 1519b1607c87SMateusz Guzik } 1520b1607c87SMateusz Guzik #endif 1521b1607c87SMateusz Guzik 152242d11757SPeter Wemm static int 1523ace8398dSJeff Roberson pollrescan(struct thread *td) 1524ace8398dSJeff Roberson { 1525ace8398dSJeff Roberson struct seltd *stp; 1526ace8398dSJeff Roberson struct selfd *sfp; 1527ace8398dSJeff Roberson struct selfd *sfn; 1528ace8398dSJeff Roberson struct selinfo *si; 1529ace8398dSJeff Roberson struct filedesc *fdp; 1530ace8398dSJeff Roberson struct file *fp; 1531ace8398dSJeff Roberson struct pollfd *fd; 1532ace8398dSJeff Roberson int n; 1533ace8398dSJeff Roberson 1534ace8398dSJeff Roberson n = 0; 1535ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1536ace8398dSJeff Roberson stp = td->td_sel; 1537ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1538ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1539ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1540ace8398dSJeff Roberson si = sfp->sf_si; 1541ace8398dSJeff Roberson selfdfree(stp, sfp); 1542ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1543ace8398dSJeff Roberson if (si != NULL) 1544ace8398dSJeff Roberson continue; 1545b1607c87SMateusz Guzik if (poll_fget(fdp, fd->fd, &fp) != 0) { 1546ace8398dSJeff Roberson fd->revents = POLLNVAL; 1547ace8398dSJeff Roberson n++; 1548ace8398dSJeff Roberson continue; 1549ace8398dSJeff Roberson } 1550ace8398dSJeff Roberson /* 1551ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1552ace8398dSJeff Roberson * POLLERR if appropriate. 1553ace8398dSJeff Roberson */ 1554ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1555ace8398dSJeff Roberson if (fd->revents != 0) 1556ace8398dSJeff Roberson n++; 1557ace8398dSJeff Roberson } 1558ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1559ace8398dSJeff Roberson stp->st_flags = 0; 1560ace8398dSJeff Roberson td->td_retval[0] = n; 1561ace8398dSJeff Roberson return (0); 1562ace8398dSJeff Roberson } 1563ace8398dSJeff Roberson 1564ace8398dSJeff Roberson static int 1565cc3c9df8SEd Maste pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd) 1566ae81968fSRobert Watson { 1567ae81968fSRobert Watson int error = 0; 1568ae81968fSRobert Watson u_int i = 0; 15696d8feddaSKonstantin Belousov u_int n = 0; 1570ae81968fSRobert Watson 1571ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1572ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1573ae81968fSRobert Watson sizeof(ufds->revents)); 1574ae81968fSRobert Watson if (error) 1575ae81968fSRobert Watson return (error); 15766d8feddaSKonstantin Belousov if (fds->revents != 0) 15776d8feddaSKonstantin Belousov n++; 1578ae81968fSRobert Watson fds++; 1579ae81968fSRobert Watson ufds++; 1580ae81968fSRobert Watson } 15816d8feddaSKonstantin Belousov td->td_retval[0] = n; 1582ae81968fSRobert Watson return (0); 1583ae81968fSRobert Watson } 1584ae81968fSRobert Watson 1585ae81968fSRobert Watson static int 1586cc3c9df8SEd Maste pollscan(struct thread *td, struct pollfd *fds, u_int nfd) 158742d11757SPeter Wemm { 1588b1607c87SMateusz Guzik struct filedesc *fdp; 158942d11757SPeter Wemm struct file *fp; 1590b1607c87SMateusz Guzik int i, n; 159142d11757SPeter Wemm 1592b1607c87SMateusz Guzik n = 0; 1593b1607c87SMateusz Guzik fdp = td->td_proc->p_fd; 15945e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1595eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1596b1607c87SMateusz Guzik if (fds->fd < 0) { 1597337c9691SJordan K. Hubbard fds->revents = 0; 1598b1607c87SMateusz Guzik continue; 1599b1607c87SMateusz Guzik } 1600b1607c87SMateusz Guzik if (poll_fget(fdp, fds->fd, &fp) != 0) { 160142d11757SPeter Wemm fds->revents = POLLNVAL; 160242d11757SPeter Wemm n++; 1603b1607c87SMateusz Guzik continue; 1604b1607c87SMateusz Guzik } 16052087c896SBruce Evans /* 16062087c896SBruce Evans * Note: backend also returns POLLHUP and 16072087c896SBruce Evans * POLLERR if appropriate. 16082087c896SBruce Evans */ 1609ace8398dSJeff Roberson selfdalloc(td, fds); 161013ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1611ea6027a8SRobert Watson td->td_ucred, td); 1612f2159cc7SKonstantin Belousov /* 1613f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1614f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1615f2159cc7SKonstantin Belousov */ 1616f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1617f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1618f2159cc7SKonstantin Belousov 161942d11757SPeter Wemm if (fds->revents != 0) 162042d11757SPeter Wemm n++; 162142d11757SPeter Wemm } 16225e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1623b40ce416SJulian Elischer td->td_retval[0] = n; 162442d11757SPeter Wemm return (0); 162542d11757SPeter Wemm } 162642d11757SPeter Wemm 162742d11757SPeter Wemm /* 1628237abf0cSDavide Italiano * XXX This was created specifically to support netncp and netsmb. This 1629237abf0cSDavide Italiano * allows the caller to specify a socket to wait for events on. It returns 1630237abf0cSDavide Italiano * 0 if any events matched and an error otherwise. There is no way to 1631237abf0cSDavide Italiano * determine which events fired. 1632237abf0cSDavide Italiano */ 1633237abf0cSDavide Italiano int 1634237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 1635237abf0cSDavide Italiano { 1636237abf0cSDavide Italiano struct timeval rtv; 1637237abf0cSDavide Italiano sbintime_t asbt, precision, rsbt; 1638237abf0cSDavide Italiano int error; 1639237abf0cSDavide Italiano 164076665df9SPeter Wemm precision = 0; /* stupid gcc! */ 1641237abf0cSDavide Italiano if (tvp != NULL) { 1642237abf0cSDavide Italiano rtv = *tvp; 1643237abf0cSDavide Italiano if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 1644237abf0cSDavide Italiano rtv.tv_usec >= 1000000) 1645237abf0cSDavide Italiano return (EINVAL); 1646237abf0cSDavide Italiano if (!timevalisset(&rtv)) 1647237abf0cSDavide Italiano asbt = 0; 1648237abf0cSDavide Italiano else if (rtv.tv_sec <= INT32_MAX) { 1649237abf0cSDavide Italiano rsbt = tvtosbt(rtv); 1650237abf0cSDavide Italiano precision = rsbt; 1651237abf0cSDavide Italiano precision >>= tc_precexp; 1652237abf0cSDavide Italiano if (TIMESEL(&asbt, rsbt)) 1653237abf0cSDavide Italiano asbt += tc_tick_sbt; 16544bc38a5aSDavide Italiano if (asbt <= SBT_MAX - rsbt) 1655237abf0cSDavide Italiano asbt += rsbt; 1656237abf0cSDavide Italiano else 1657237abf0cSDavide Italiano asbt = -1; 1658237abf0cSDavide Italiano } else 1659237abf0cSDavide Italiano asbt = -1; 1660237abf0cSDavide Italiano } else 1661237abf0cSDavide Italiano asbt = -1; 1662237abf0cSDavide Italiano seltdinit(td); 1663237abf0cSDavide Italiano /* 1664237abf0cSDavide Italiano * Iterate until the timeout expires or the socket becomes ready. 1665237abf0cSDavide Italiano */ 1666237abf0cSDavide Italiano for (;;) { 1667237abf0cSDavide Italiano selfdalloc(td, NULL); 1668237abf0cSDavide Italiano error = sopoll(so, events, NULL, td); 1669237abf0cSDavide Italiano /* error here is actually the ready events. */ 1670237abf0cSDavide Italiano if (error) 1671237abf0cSDavide Italiano return (0); 1672237abf0cSDavide Italiano error = seltdwait(td, asbt, precision); 1673237abf0cSDavide Italiano if (error) 1674237abf0cSDavide Italiano break; 1675237abf0cSDavide Italiano } 1676237abf0cSDavide Italiano seltdclear(td); 1677237abf0cSDavide Italiano /* XXX Duplicates ncp/smb behavior. */ 1678237abf0cSDavide Italiano if (error == ERESTART) 1679237abf0cSDavide Italiano error = 0; 1680237abf0cSDavide Italiano return (error); 1681237abf0cSDavide Italiano } 1682237abf0cSDavide Italiano 1683237abf0cSDavide Italiano /* 1684ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1685ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1686ace8398dSJeff Roberson */ 1687ace8398dSJeff Roberson static void 1688ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1689ace8398dSJeff Roberson { 1690ace8398dSJeff Roberson struct seltd *stp; 1691ace8398dSJeff Roberson 1692ace8398dSJeff Roberson stp = td->td_sel; 1693ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1694*ea33cca9SMateusz Guzik stp->st_free1 = malloc(sizeof(*stp->st_free1), M_SELFD, M_WAITOK|M_ZERO); 1695ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1696ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1697ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1698*ea33cca9SMateusz Guzik stp->st_free2 = malloc(sizeof(*stp->st_free2), M_SELFD, M_WAITOK|M_ZERO); 1699ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1700ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1701ace8398dSJeff Roberson } 1702ace8398dSJeff Roberson 1703ace8398dSJeff Roberson static void 1704ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1705ace8398dSJeff Roberson { 1706ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 170773f2e5f7SMateusz Guzik if (sfp->sf_si != NULL) { 1708ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1709fcb5b3a4SKonstantin Belousov if (sfp->sf_si != NULL) { 1710ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1711fcb5b3a4SKonstantin Belousov refcount_release(&sfp->sf_refs); 1712fcb5b3a4SKonstantin Belousov } 1713ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 171473f2e5f7SMateusz Guzik } 1715fcb5b3a4SKonstantin Belousov if (refcount_release(&sfp->sf_refs)) 1716*ea33cca9SMateusz 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; 1769fcb5b3a4SKonstantin Belousov refcount_init(&sfp->sf_refs, 2); 1770ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1771ace8398dSJeff Roberson /* 1772ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1773ace8398dSJeff Roberson */ 1774ace8398dSJeff Roberson mtx_lock(mtxp); 1775ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1776ace8398dSJeff Roberson sip->si_mtx = mtxp; 1777ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 177885f190e4SAlfred Perlstein } 1779ace8398dSJeff Roberson /* 1780ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1781ace8398dSJeff Roberson */ 1782ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1783ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1784df8bae1dSRodney W. Grimes } 1785df8bae1dSRodney W. Grimes 1786512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1787df8bae1dSRodney W. Grimes void 1788cc3c9df8SEd Maste selwakeup(struct selinfo *sip) 1789df8bae1dSRodney W. Grimes { 1790512824f8SSeigo Tanimura doselwakeup(sip, -1); 1791512824f8SSeigo Tanimura } 1792512824f8SSeigo Tanimura 1793512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1794512824f8SSeigo Tanimura void 1795cc3c9df8SEd Maste selwakeuppri(struct selinfo *sip, int pri) 1796512824f8SSeigo Tanimura { 1797512824f8SSeigo Tanimura doselwakeup(sip, pri); 1798512824f8SSeigo Tanimura } 1799512824f8SSeigo Tanimura 1800512824f8SSeigo Tanimura /* 1801512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1802512824f8SSeigo Tanimura */ 1803512824f8SSeigo Tanimura static void 1804cc3c9df8SEd Maste doselwakeup(struct selinfo *sip, int pri) 1805512824f8SSeigo Tanimura { 1806ace8398dSJeff Roberson struct selfd *sfp; 1807ace8398dSJeff Roberson struct selfd *sfn; 1808ace8398dSJeff Roberson struct seltd *stp; 1809df8bae1dSRodney W. Grimes 1810ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1811ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1812b40ce416SJulian Elischer return; 1813ace8398dSJeff Roberson /* 1814ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1815ace8398dSJeff Roberson */ 1816ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1817ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1818ace8398dSJeff Roberson /* 1819ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1820ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1821ace8398dSJeff Roberson */ 1822ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1823ace8398dSJeff Roberson sfp->sf_si = NULL; 1824ace8398dSJeff Roberson stp = sfp->sf_td; 1825ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1826ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1827ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1828ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1829fcb5b3a4SKonstantin Belousov if (refcount_release(&sfp->sf_refs)) 1830*ea33cca9SMateusz Guzik free(sfp, M_SELFD); 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) 1891*ea33cca9SMateusz Guzik free(stp->st_free1, M_SELFD); 1892ace8398dSJeff Roberson if (stp->st_free2) 1893*ea33cca9SMateusz 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