19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 402de92a38SPeter Wemm #include "opt_compat.h" 41db6a20e2SGarrett Wollman #include "opt_ktrace.h" 42db6a20e2SGarrett Wollman 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 45d2d3e875SBruce Evans #include <sys/sysproto.h> 46df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 4720982410SBruce Evans #include <sys/filio.h> 483ac4d1efSBruce Evans #include <sys/fcntl.h> 49df8bae1dSRodney W. Grimes #include <sys/file.h> 50df8bae1dSRodney W. Grimes #include <sys/proc.h> 51797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53df8bae1dSRodney W. Grimes #include <sys/uio.h> 54df8bae1dSRodney W. Grimes #include <sys/kernel.h> 55e4650294SJohn Baldwin #include <sys/ktr.h> 56104a9b7eSAlexander Kabaev #include <sys/limits.h> 57df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5842d11757SPeter Wemm #include <sys/poll.h> 5989b71647SPeter Wemm #include <sys/resourcevar.h> 600a2c3d48SGarrett Wollman #include <sys/selinfo.h> 6144f3b092SJohn Baldwin #include <sys/sleepqueue.h> 628f19eb88SIan Dowse #include <sys/syscallsubr.h> 638cb96f20SPeter Wemm #include <sys/sysctl.h> 6442d11757SPeter Wemm #include <sys/sysent.h> 659bbee259SAndrey A. Chernov #include <sys/vnode.h> 66279d7226SMatthew Dillon #include <sys/bio.h> 67279d7226SMatthew Dillon #include <sys/buf.h> 68265fc98fSSeigo Tanimura #include <sys/condvar.h> 69df8bae1dSRodney W. Grimes #ifdef KTRACE 70df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 71df8bae1dSRodney W. Grimes #endif 72df8bae1dSRodney W. Grimes 73e4650294SJohn Baldwin #include <security/audit/audit.h> 74ace8398dSJeff Roberson 75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 76a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 77a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7855166637SPoul-Henning Kamp 79ae81968fSRobert Watson static int pollout(struct pollfd *, struct pollfd *, u_int); 80bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 81ace8398dSJeff Roberson static int pollrescan(struct thread *); 82bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 83ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 84ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 85ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 86bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 87bcd9e0ddSJohn Baldwin off_t, int); 88bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 89bcd9e0ddSJohn Baldwin off_t, int); 90512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 91ace8398dSJeff Roberson static void seltdinit(struct thread *); 92ace8398dSJeff Roberson static int seltdwait(struct thread *, int); 93ace8398dSJeff Roberson static void seltdclear(struct thread *); 94ace8398dSJeff Roberson 95ace8398dSJeff Roberson /* 96ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 97ace8398dSJeff Roberson * 98ace8398dSJeff Roberson * t - protected by st_mtx 99ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 100ace8398dSJeff Roberson */ 101ace8398dSJeff Roberson struct seltd { 102ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 103ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 104ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 105ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 106ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 107ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 108ace8398dSJeff Roberson }; 109ace8398dSJeff Roberson 110ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 111ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 112ace8398dSJeff Roberson 113ace8398dSJeff Roberson /* 114ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 115ace8398dSJeff Roberson * f - protected by sf_mtx 116ace8398dSJeff Roberson */ 117ace8398dSJeff Roberson struct selfd { 118ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 119ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 120ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 121ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 122ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 123ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 124ace8398dSJeff Roberson }; 125ace8398dSJeff Roberson 126ace8398dSJeff Roberson static uma_zone_t selfd_zone; 1272141453eSJeff Roberson static struct mtx_pool *mtxpool_select; 1288fe387abSDmitrij Tejblum 129d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 130df8bae1dSRodney W. Grimes struct read_args { 131df8bae1dSRodney W. Grimes int fd; 132134e06feSBruce Evans void *buf; 133134e06feSBruce Evans size_t nbyte; 134df8bae1dSRodney W. Grimes }; 135d2d3e875SBruce Evans #endif 13626f9a767SRodney W. Grimes int 137b40ce416SJulian Elischer read(td, uap) 138b40ce416SJulian Elischer struct thread *td; 139b064d43dSMatthew Dillon struct read_args *uap; 140df8bae1dSRodney W. Grimes { 141bcd9e0ddSJohn Baldwin struct uio auio; 142bcd9e0ddSJohn Baldwin struct iovec aiov; 143279d7226SMatthew Dillon int error; 144df8bae1dSRodney W. Grimes 145bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 146bcd9e0ddSJohn Baldwin return (EINVAL); 147bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 148bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 149bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 150bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 151bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 152bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 153bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 154279d7226SMatthew Dillon return(error); 155df8bae1dSRodney W. Grimes } 156df8bae1dSRodney W. Grimes 157df8bae1dSRodney W. Grimes /* 158bcd9e0ddSJohn Baldwin * Positioned read system call 1594160ccd9SAlan Cox */ 1604160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1614160ccd9SAlan Cox struct pread_args { 1624160ccd9SAlan Cox int fd; 1634160ccd9SAlan Cox void *buf; 1644160ccd9SAlan Cox size_t nbyte; 1658fe387abSDmitrij Tejblum int pad; 1664160ccd9SAlan Cox off_t offset; 1674160ccd9SAlan Cox }; 1684160ccd9SAlan Cox #endif 1694160ccd9SAlan Cox int 170b40ce416SJulian Elischer pread(td, uap) 171b40ce416SJulian Elischer struct thread *td; 172b064d43dSMatthew Dillon struct pread_args *uap; 1734160ccd9SAlan Cox { 1744160ccd9SAlan Cox struct uio auio; 1754160ccd9SAlan Cox struct iovec aiov; 176bcd9e0ddSJohn Baldwin int error; 1774160ccd9SAlan Cox 178bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 179bcd9e0ddSJohn Baldwin return (EINVAL); 180bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 181bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 1824160ccd9SAlan Cox auio.uio_iov = &aiov; 1834160ccd9SAlan Cox auio.uio_iovcnt = 1; 184bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 1854160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 186bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, &auio, uap->offset); 1874160ccd9SAlan Cox return(error); 1884160ccd9SAlan Cox } 1894160ccd9SAlan Cox 190c2815ad5SPeter Wemm int 191c2815ad5SPeter Wemm freebsd6_pread(td, uap) 192c2815ad5SPeter Wemm struct thread *td; 193c2815ad5SPeter Wemm struct freebsd6_pread_args *uap; 194c2815ad5SPeter Wemm { 195c2815ad5SPeter Wemm struct pread_args oargs; 196c2815ad5SPeter Wemm 197c2815ad5SPeter Wemm oargs.fd = uap->fd; 198c2815ad5SPeter Wemm oargs.buf = uap->buf; 199c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 200c2815ad5SPeter Wemm oargs.offset = uap->offset; 201c2815ad5SPeter Wemm return (pread(td, &oargs)); 202c2815ad5SPeter Wemm } 203c2815ad5SPeter Wemm 2044160ccd9SAlan Cox /* 205df8bae1dSRodney W. Grimes * Scatter read system call. 206df8bae1dSRodney W. Grimes */ 207d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 208df8bae1dSRodney W. Grimes struct readv_args { 2097147b19dSBruce Evans int fd; 210df8bae1dSRodney W. Grimes struct iovec *iovp; 211df8bae1dSRodney W. Grimes u_int iovcnt; 212df8bae1dSRodney W. Grimes }; 213d2d3e875SBruce Evans #endif 21426f9a767SRodney W. Grimes int 215552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap) 216df8bae1dSRodney W. Grimes { 217b88ec951SJohn Baldwin struct uio *auio; 218b88ec951SJohn Baldwin int error; 219b88ec951SJohn Baldwin 220b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 221b88ec951SJohn Baldwin if (error) 222b88ec951SJohn Baldwin return (error); 223b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 224b88ec951SJohn Baldwin free(auio, M_IOV); 225b88ec951SJohn Baldwin return (error); 226b88ec951SJohn Baldwin } 227b88ec951SJohn Baldwin 228b88ec951SJohn Baldwin int 229b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 230b88ec951SJohn Baldwin { 231b064d43dSMatthew Dillon struct file *fp; 232bcd9e0ddSJohn Baldwin int error; 233bcd9e0ddSJohn Baldwin 234bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 235bcd9e0ddSJohn Baldwin if (error) 236bcd9e0ddSJohn Baldwin return (error); 237bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 238bcd9e0ddSJohn Baldwin fdrop(fp, td); 239bcd9e0ddSJohn Baldwin return (error); 240bcd9e0ddSJohn Baldwin } 241bcd9e0ddSJohn Baldwin 242bcd9e0ddSJohn Baldwin /* 243bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 244bcd9e0ddSJohn Baldwin */ 245bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 246bcd9e0ddSJohn Baldwin struct preadv_args { 247bcd9e0ddSJohn Baldwin int fd; 248bcd9e0ddSJohn Baldwin struct iovec *iovp; 249bcd9e0ddSJohn Baldwin u_int iovcnt; 250bcd9e0ddSJohn Baldwin off_t offset; 251bcd9e0ddSJohn Baldwin }; 252bcd9e0ddSJohn Baldwin #endif 253bcd9e0ddSJohn Baldwin int 254bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap) 255bcd9e0ddSJohn Baldwin { 256bcd9e0ddSJohn Baldwin struct uio *auio; 257bcd9e0ddSJohn Baldwin int error; 258bcd9e0ddSJohn Baldwin 259bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 260bcd9e0ddSJohn Baldwin if (error) 261bcd9e0ddSJohn Baldwin return (error); 262bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 263bcd9e0ddSJohn Baldwin free(auio, M_IOV); 264bcd9e0ddSJohn Baldwin return (error); 265bcd9e0ddSJohn Baldwin } 266bcd9e0ddSJohn Baldwin 267bcd9e0ddSJohn Baldwin int 268bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset) 269bcd9e0ddSJohn Baldwin struct thread *td; 270bcd9e0ddSJohn Baldwin int fd; 271bcd9e0ddSJohn Baldwin struct uio *auio; 272bcd9e0ddSJohn Baldwin off_t offset; 273bcd9e0ddSJohn Baldwin { 274bcd9e0ddSJohn Baldwin struct file *fp; 275bcd9e0ddSJohn Baldwin int error; 276bcd9e0ddSJohn Baldwin 277bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 278bcd9e0ddSJohn Baldwin if (error) 279bcd9e0ddSJohn Baldwin return (error); 280bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 281bcd9e0ddSJohn Baldwin error = ESPIPE; 282bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 283bcd9e0ddSJohn Baldwin error = EINVAL; 284bcd9e0ddSJohn Baldwin else 285bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 286bcd9e0ddSJohn Baldwin fdrop(fp, td); 287bcd9e0ddSJohn Baldwin return (error); 288bcd9e0ddSJohn Baldwin } 289bcd9e0ddSJohn Baldwin 290bcd9e0ddSJohn Baldwin /* 291bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 292bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 293bcd9e0ddSJohn Baldwin */ 294bcd9e0ddSJohn Baldwin static int 295bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 296bcd9e0ddSJohn Baldwin struct thread *td; 297bcd9e0ddSJohn Baldwin int fd; 298bcd9e0ddSJohn Baldwin struct file *fp; 299bcd9e0ddSJohn Baldwin struct uio *auio; 300bcd9e0ddSJohn Baldwin off_t offset; 301bcd9e0ddSJohn Baldwin int flags; 302bcd9e0ddSJohn Baldwin { 303bcd9e0ddSJohn Baldwin ssize_t cnt; 30482641acdSAlan Cox int error; 305df8bae1dSRodney W. Grimes #ifdef KTRACE 306552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 307df8bae1dSRodney W. Grimes #endif 308df8bae1dSRodney W. Grimes 3094f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3104f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3114f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3124f8d23d6SPoul-Henning Kamp return(0); 3134f8d23d6SPoul-Henning Kamp } 314552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 315bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 316552afd9cSPoul-Henning Kamp auio->uio_td = td; 317df8bae1dSRodney W. Grimes #ifdef KTRACE 318552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 319552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 320df8bae1dSRodney W. Grimes #endif 321552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 322bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 323552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 324df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 325df8bae1dSRodney W. Grimes error = 0; 326279d7226SMatthew Dillon } 327552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 328df8bae1dSRodney W. Grimes #ifdef KTRACE 329552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 330552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 331b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes #endif 334b40ce416SJulian Elischer td->td_retval[0] = cnt; 335df8bae1dSRodney W. Grimes return (error); 336df8bae1dSRodney W. Grimes } 337df8bae1dSRodney W. Grimes 338d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 339df8bae1dSRodney W. Grimes struct write_args { 340df8bae1dSRodney W. Grimes int fd; 341134e06feSBruce Evans const void *buf; 342134e06feSBruce Evans size_t nbyte; 343df8bae1dSRodney W. Grimes }; 344d2d3e875SBruce Evans #endif 34526f9a767SRodney W. Grimes int 346b40ce416SJulian Elischer write(td, uap) 347b40ce416SJulian Elischer struct thread *td; 348b064d43dSMatthew Dillon struct write_args *uap; 349df8bae1dSRodney W. Grimes { 350bcd9e0ddSJohn Baldwin struct uio auio; 351bcd9e0ddSJohn Baldwin struct iovec aiov; 352279d7226SMatthew Dillon int error; 353df8bae1dSRodney W. Grimes 354bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 355bcd9e0ddSJohn Baldwin return (EINVAL); 356bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 357bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 358bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 359bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 360bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 361bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 362bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 363279d7226SMatthew Dillon return(error); 364df8bae1dSRodney W. Grimes } 365df8bae1dSRodney W. Grimes 366df8bae1dSRodney W. Grimes /* 3670c14ff0eSRobert Watson * Positioned write system call. 3684160ccd9SAlan Cox */ 3694160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3704160ccd9SAlan Cox struct pwrite_args { 3714160ccd9SAlan Cox int fd; 3724160ccd9SAlan Cox const void *buf; 3734160ccd9SAlan Cox size_t nbyte; 3748fe387abSDmitrij Tejblum int pad; 3754160ccd9SAlan Cox off_t offset; 3764160ccd9SAlan Cox }; 3774160ccd9SAlan Cox #endif 3784160ccd9SAlan Cox int 379b40ce416SJulian Elischer pwrite(td, uap) 380b40ce416SJulian Elischer struct thread *td; 381b064d43dSMatthew Dillon struct pwrite_args *uap; 3824160ccd9SAlan Cox { 3834160ccd9SAlan Cox struct uio auio; 3844160ccd9SAlan Cox struct iovec aiov; 385bcd9e0ddSJohn Baldwin int error; 3864160ccd9SAlan Cox 387bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 388bcd9e0ddSJohn Baldwin return (EINVAL); 389bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 390bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 3914160ccd9SAlan Cox auio.uio_iov = &aiov; 3924160ccd9SAlan Cox auio.uio_iovcnt = 1; 393bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 3944160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 395bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, &auio, uap->offset); 3964160ccd9SAlan Cox return(error); 3974160ccd9SAlan Cox } 3984160ccd9SAlan Cox 399c2815ad5SPeter Wemm int 400c2815ad5SPeter Wemm freebsd6_pwrite(td, uap) 401c2815ad5SPeter Wemm struct thread *td; 402c2815ad5SPeter Wemm struct freebsd6_pwrite_args *uap; 403c2815ad5SPeter Wemm { 404c2815ad5SPeter Wemm struct pwrite_args oargs; 405c2815ad5SPeter Wemm 406c2815ad5SPeter Wemm oargs.fd = uap->fd; 407c2815ad5SPeter Wemm oargs.buf = uap->buf; 408c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 409c2815ad5SPeter Wemm oargs.offset = uap->offset; 410c2815ad5SPeter Wemm return (pwrite(td, &oargs)); 411c2815ad5SPeter Wemm } 412c2815ad5SPeter Wemm 4134160ccd9SAlan Cox /* 4140c14ff0eSRobert Watson * Gather write system call. 415df8bae1dSRodney W. Grimes */ 416d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 417df8bae1dSRodney W. Grimes struct writev_args { 418df8bae1dSRodney W. Grimes int fd; 419df8bae1dSRodney W. Grimes struct iovec *iovp; 420df8bae1dSRodney W. Grimes u_int iovcnt; 421df8bae1dSRodney W. Grimes }; 422d2d3e875SBruce Evans #endif 42326f9a767SRodney W. Grimes int 424552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap) 425df8bae1dSRodney W. Grimes { 426b88ec951SJohn Baldwin struct uio *auio; 427b88ec951SJohn Baldwin int error; 428b88ec951SJohn Baldwin 429b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 430b88ec951SJohn Baldwin if (error) 431b88ec951SJohn Baldwin return (error); 432b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 433b88ec951SJohn Baldwin free(auio, M_IOV); 434b88ec951SJohn Baldwin return (error); 435b88ec951SJohn Baldwin } 436b88ec951SJohn Baldwin 437b88ec951SJohn Baldwin int 438b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 439b88ec951SJohn Baldwin { 440b064d43dSMatthew Dillon struct file *fp; 441bcd9e0ddSJohn Baldwin int error; 442bcd9e0ddSJohn Baldwin 443bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 444bcd9e0ddSJohn Baldwin if (error) 445af56abaaSJohn Baldwin return (error); 446bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 447bcd9e0ddSJohn Baldwin fdrop(fp, td); 448bcd9e0ddSJohn Baldwin return (error); 449bcd9e0ddSJohn Baldwin } 450bcd9e0ddSJohn Baldwin 451bcd9e0ddSJohn Baldwin /* 4520c14ff0eSRobert Watson * Gather positioned write system call. 453bcd9e0ddSJohn Baldwin */ 454bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 455bcd9e0ddSJohn Baldwin struct pwritev_args { 456bcd9e0ddSJohn Baldwin int fd; 457bcd9e0ddSJohn Baldwin struct iovec *iovp; 458bcd9e0ddSJohn Baldwin u_int iovcnt; 459bcd9e0ddSJohn Baldwin off_t offset; 460bcd9e0ddSJohn Baldwin }; 461bcd9e0ddSJohn Baldwin #endif 462bcd9e0ddSJohn Baldwin int 463bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap) 464bcd9e0ddSJohn Baldwin { 465bcd9e0ddSJohn Baldwin struct uio *auio; 466bcd9e0ddSJohn Baldwin int error; 467bcd9e0ddSJohn Baldwin 468bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 469bcd9e0ddSJohn Baldwin if (error) 470bcd9e0ddSJohn Baldwin return (error); 471bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 472bcd9e0ddSJohn Baldwin free(auio, M_IOV); 473bcd9e0ddSJohn Baldwin return (error); 474bcd9e0ddSJohn Baldwin } 475bcd9e0ddSJohn Baldwin 476bcd9e0ddSJohn Baldwin int 477bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset) 478bcd9e0ddSJohn Baldwin struct thread *td; 479bcd9e0ddSJohn Baldwin struct uio *auio; 480bcd9e0ddSJohn Baldwin int fd; 481bcd9e0ddSJohn Baldwin off_t offset; 482bcd9e0ddSJohn Baldwin { 483bcd9e0ddSJohn Baldwin struct file *fp; 484bcd9e0ddSJohn Baldwin int error; 485bcd9e0ddSJohn Baldwin 486bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 487bcd9e0ddSJohn Baldwin if (error) 488af56abaaSJohn Baldwin return (error); 489bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 490bcd9e0ddSJohn Baldwin error = ESPIPE; 491bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 492bcd9e0ddSJohn Baldwin error = EINVAL; 493bcd9e0ddSJohn Baldwin else 494bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 495bcd9e0ddSJohn Baldwin fdrop(fp, td); 496bcd9e0ddSJohn Baldwin return (error); 497bcd9e0ddSJohn Baldwin } 498bcd9e0ddSJohn Baldwin 499bcd9e0ddSJohn Baldwin /* 500bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 501bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 502bcd9e0ddSJohn Baldwin */ 503bcd9e0ddSJohn Baldwin static int 504bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 505bcd9e0ddSJohn Baldwin struct thread *td; 506bcd9e0ddSJohn Baldwin int fd; 507bcd9e0ddSJohn Baldwin struct file *fp; 508bcd9e0ddSJohn Baldwin struct uio *auio; 509bcd9e0ddSJohn Baldwin off_t offset; 510bcd9e0ddSJohn Baldwin int flags; 511bcd9e0ddSJohn Baldwin { 512bcd9e0ddSJohn Baldwin ssize_t cnt; 513552afd9cSPoul-Henning Kamp int error; 514df8bae1dSRodney W. Grimes #ifdef KTRACE 515552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 516df8bae1dSRodney W. Grimes #endif 517df8bae1dSRodney W. Grimes 518552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 519552afd9cSPoul-Henning Kamp auio->uio_td = td; 520bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 521df8bae1dSRodney W. Grimes #ifdef KTRACE 522552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 523552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 524df8bae1dSRodney W. Grimes #endif 525552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 526a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5279440653dSMatthew Dillon bwillwrite(); 528bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 529552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 530df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 531df8bae1dSRodney W. Grimes error = 0; 532bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5336f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 534b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 535b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 536b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 53719eb87d2SJohn Baldwin } 538df8bae1dSRodney W. Grimes } 539552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 540df8bae1dSRodney W. Grimes #ifdef KTRACE 541552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 542552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 543b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes #endif 546b40ce416SJulian Elischer td->td_retval[0] = cnt; 547df8bae1dSRodney W. Grimes return (error); 548df8bae1dSRodney W. Grimes } 549df8bae1dSRodney W. Grimes 550e4650294SJohn Baldwin /* 551e4650294SJohn Baldwin * Truncate a file given a file descriptor. 552e4650294SJohn Baldwin * 553e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 554e4650294SJohn Baldwin * descriptor isn't writable. 555e4650294SJohn Baldwin */ 556e4650294SJohn Baldwin int 557e4650294SJohn Baldwin kern_ftruncate(td, fd, length) 558e4650294SJohn Baldwin struct thread *td; 559e4650294SJohn Baldwin int fd; 560e4650294SJohn Baldwin off_t length; 561e4650294SJohn Baldwin { 562e4650294SJohn Baldwin struct file *fp; 563e4650294SJohn Baldwin int error; 564e4650294SJohn Baldwin 56514961ba7SRobert Watson AUDIT_ARG_FD(fd); 566e4650294SJohn Baldwin if (length < 0) 567e4650294SJohn Baldwin return (EINVAL); 568e4650294SJohn Baldwin error = fget(td, fd, &fp); 569e4650294SJohn Baldwin if (error) 570e4650294SJohn Baldwin return (error); 57114961ba7SRobert Watson AUDIT_ARG_FILE(td->td_proc, fp); 572e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 573e4650294SJohn Baldwin fdrop(fp, td); 574e4650294SJohn Baldwin return (EINVAL); 575e4650294SJohn Baldwin } 576e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 577e4650294SJohn Baldwin fdrop(fp, td); 578e4650294SJohn Baldwin return (error); 579e4650294SJohn Baldwin } 580e4650294SJohn Baldwin 581e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 582e4650294SJohn Baldwin struct ftruncate_args { 583e4650294SJohn Baldwin int fd; 584e4650294SJohn Baldwin int pad; 585e4650294SJohn Baldwin off_t length; 586e4650294SJohn Baldwin }; 587e4650294SJohn Baldwin #endif 588e4650294SJohn Baldwin int 589e4650294SJohn Baldwin ftruncate(td, uap) 590e4650294SJohn Baldwin struct thread *td; 591e4650294SJohn Baldwin struct ftruncate_args *uap; 592e4650294SJohn Baldwin { 593e4650294SJohn Baldwin 594e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 595e4650294SJohn Baldwin } 596e4650294SJohn Baldwin 597e4650294SJohn Baldwin #if defined(COMPAT_43) 598e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 599e4650294SJohn Baldwin struct oftruncate_args { 600e4650294SJohn Baldwin int fd; 601e4650294SJohn Baldwin long length; 602e4650294SJohn Baldwin }; 603e4650294SJohn Baldwin #endif 604e4650294SJohn Baldwin int 605e4650294SJohn Baldwin oftruncate(td, uap) 606e4650294SJohn Baldwin struct thread *td; 607e4650294SJohn Baldwin struct oftruncate_args *uap; 608e4650294SJohn Baldwin { 609e4650294SJohn Baldwin 610e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 611e4650294SJohn Baldwin } 612e4650294SJohn Baldwin #endif /* COMPAT_43 */ 613e4650294SJohn Baldwin 614d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 615df8bae1dSRodney W. Grimes struct ioctl_args { 616df8bae1dSRodney W. Grimes int fd; 617069e9bc1SDoug Rabson u_long com; 618df8bae1dSRodney W. Grimes caddr_t data; 619df8bae1dSRodney W. Grimes }; 620d2d3e875SBruce Evans #endif 621df8bae1dSRodney W. Grimes /* ARGSUSED */ 62226f9a767SRodney W. Grimes int 6233e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap) 624df8bae1dSRodney W. Grimes { 6253e15c66fSPoul-Henning Kamp u_long com; 6269fddcc66SRuslan Ermilov int arg, error; 6273e15c66fSPoul-Henning Kamp u_int size; 6289fddcc66SRuslan Ermilov caddr_t data; 629df8bae1dSRodney W. Grimes 6309fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6319fc6aa06SPoul-Henning Kamp printf( 6329fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 633431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6349fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 6359fc6aa06SPoul-Henning Kamp } 636d9f46233SJohn Baldwin com = uap->com; 637df8bae1dSRodney W. Grimes 638df8bae1dSRodney W. Grimes /* 639df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 640df8bae1dSRodney W. Grimes * copied to/from the user's address space. 641df8bae1dSRodney W. Grimes */ 642df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 643ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 644ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6452de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6462de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6472de92a38SPeter Wemm #else 6482de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6492de92a38SPeter Wemm #endif 6509fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 651426da3bcSAlfred Perlstein return (ENOTTY); 652279d7226SMatthew Dillon 653ca51b19bSPoul-Henning Kamp if (size > 0) { 654715457f6SDavid E. O'Brien if (com & IOC_VOID) { 6559fddcc66SRuslan Ermilov /* Integer argument. */ 6569fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6579fddcc66SRuslan Ermilov data = (void *)&arg; 6589fddcc66SRuslan Ermilov size = 0; 659715457f6SDavid E. O'Brien } else 660715457f6SDavid E. O'Brien data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 6619fddcc66SRuslan Ermilov } else 6629fddcc66SRuslan Ermilov data = (void *)&uap->data; 663df8bae1dSRodney W. Grimes if (com & IOC_IN) { 664df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 665df8bae1dSRodney W. Grimes if (error) { 666a1b0a180SRuslan Ermilov if (size > 0) 6679fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 6683e15c66fSPoul-Henning Kamp return (error); 669df8bae1dSRodney W. Grimes } 670ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 671df8bae1dSRodney W. Grimes /* 672df8bae1dSRodney W. Grimes * Zero the buffer so the user always 673df8bae1dSRodney W. Grimes * gets back something deterministic. 674df8bae1dSRodney W. Grimes */ 675df8bae1dSRodney W. Grimes bzero(data, size); 676279d7226SMatthew Dillon } 677df8bae1dSRodney W. Grimes 678d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 679d9f46233SJohn Baldwin 680d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 681d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 682d9f46233SJohn Baldwin 6839fddcc66SRuslan Ermilov if (size > 0) 6849fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 685d9f46233SJohn Baldwin return (error); 686d9f46233SJohn Baldwin } 687d9f46233SJohn Baldwin 688d9f46233SJohn Baldwin int 689d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 690d9f46233SJohn Baldwin { 691d9f46233SJohn Baldwin struct file *fp; 692d9f46233SJohn Baldwin struct filedesc *fdp; 693d9f46233SJohn Baldwin int error; 694d9f46233SJohn Baldwin int tmp; 695d9f46233SJohn Baldwin 69664c9a4d9SRobert Watson AUDIT_ARG_FD(fd); 69764c9a4d9SRobert Watson AUDIT_ARG_CMD(com); 698d9f46233SJohn Baldwin if ((error = fget(td, fd, &fp)) != 0) 699d9f46233SJohn Baldwin return (error); 700d9f46233SJohn Baldwin if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 701d9f46233SJohn Baldwin fdrop(fp, td); 702d9f46233SJohn Baldwin return (EBADF); 703d9f46233SJohn Baldwin } 704d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 705d9f46233SJohn Baldwin switch (com) { 706d9f46233SJohn Baldwin case FIONCLEX: 7075e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 708d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE; 7095e3f7694SRobert Watson FILEDESC_XUNLOCK(fdp); 710d9f46233SJohn Baldwin goto out; 711d9f46233SJohn Baldwin case FIOCLEX: 7125e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 713d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 7145e3f7694SRobert Watson FILEDESC_XUNLOCK(fdp); 715d9f46233SJohn Baldwin goto out; 716d9f46233SJohn Baldwin case FIONBIO: 717bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 718397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 719df8bae1dSRodney W. Grimes else 720397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7218ccf264fSPoul-Henning Kamp data = (void *)&tmp; 722d9f46233SJohn Baldwin break; 723d9f46233SJohn Baldwin case FIOASYNC: 724bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 725397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 726df8bae1dSRodney W. Grimes else 727397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7288ccf264fSPoul-Henning Kamp data = (void *)&tmp; 729d9f46233SJohn Baldwin break; 730df8bae1dSRodney W. Grimes } 7318ccf264fSPoul-Henning Kamp 7328ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 733d9f46233SJohn Baldwin out: 734b40ce416SJulian Elischer fdrop(fp, td); 735df8bae1dSRodney W. Grimes return (error); 736df8bae1dSRodney W. Grimes } 737df8bae1dSRodney W. Grimes 738125dcf8cSKonstantin Belousov int 739125dcf8cSKonstantin Belousov poll_no_poll(int events) 740125dcf8cSKonstantin Belousov { 741125dcf8cSKonstantin Belousov /* 742125dcf8cSKonstantin Belousov * Return true for read/write. If the user asked for something 743125dcf8cSKonstantin Belousov * special, return POLLNVAL, so that clients have a way of 744125dcf8cSKonstantin Belousov * determining reliably whether or not the extended 745125dcf8cSKonstantin Belousov * functionality is present without hard-coding knowledge 746125dcf8cSKonstantin Belousov * of specific filesystem implementations. 747125dcf8cSKonstantin Belousov */ 748125dcf8cSKonstantin Belousov if (events & ~POLLSTANDARD) 749125dcf8cSKonstantin Belousov return (POLLNVAL); 750125dcf8cSKonstantin Belousov 751125dcf8cSKonstantin Belousov return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 752125dcf8cSKonstantin Belousov } 753125dcf8cSKonstantin Belousov 754066d836bSKonstantin Belousov int 755066d836bSKonstantin Belousov pselect(struct thread *td, struct pselect_args *uap) 756066d836bSKonstantin Belousov { 757066d836bSKonstantin Belousov struct timespec ts; 758066d836bSKonstantin Belousov struct timeval tv, *tvp; 759066d836bSKonstantin Belousov sigset_t set, *uset; 760066d836bSKonstantin Belousov int error; 761066d836bSKonstantin Belousov 762066d836bSKonstantin Belousov if (uap->ts != NULL) { 763066d836bSKonstantin Belousov error = copyin(uap->ts, &ts, sizeof(ts)); 764066d836bSKonstantin Belousov if (error != 0) 765066d836bSKonstantin Belousov return (error); 766066d836bSKonstantin Belousov TIMESPEC_TO_TIMEVAL(&tv, &ts); 767066d836bSKonstantin Belousov tvp = &tv; 768066d836bSKonstantin Belousov } else 769066d836bSKonstantin Belousov tvp = NULL; 770066d836bSKonstantin Belousov if (uap->sm != NULL) { 771066d836bSKonstantin Belousov error = copyin(uap->sm, &set, sizeof(set)); 772066d836bSKonstantin Belousov if (error != 0) 773066d836bSKonstantin Belousov return (error); 774066d836bSKonstantin Belousov uset = &set; 775066d836bSKonstantin Belousov } else 776066d836bSKonstantin Belousov uset = NULL; 777066d836bSKonstantin Belousov return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 778066d836bSKonstantin Belousov uset, NFDBITS)); 779066d836bSKonstantin Belousov } 780066d836bSKonstantin Belousov 781066d836bSKonstantin Belousov int 782066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, 783066d836bSKonstantin Belousov struct timeval *tvp, sigset_t *uset, int abi_nfdbits) 784066d836bSKonstantin Belousov { 785066d836bSKonstantin Belousov int error; 786066d836bSKonstantin Belousov 787066d836bSKonstantin Belousov if (uset != NULL) { 788066d836bSKonstantin Belousov error = kern_sigprocmask(td, SIG_SETMASK, uset, 789066d836bSKonstantin Belousov &td->td_oldsigmask, 0); 790066d836bSKonstantin Belousov if (error != 0) 791066d836bSKonstantin Belousov return (error); 792066d836bSKonstantin Belousov td->td_pflags |= TDP_OLDMASK; 793066d836bSKonstantin Belousov /* 794066d836bSKonstantin Belousov * Make sure that ast() is called on return to 795066d836bSKonstantin Belousov * usermode and TDP_OLDMASK is cleared, restoring old 796066d836bSKonstantin Belousov * sigmask. 797066d836bSKonstantin Belousov */ 798066d836bSKonstantin Belousov thread_lock(td); 799066d836bSKonstantin Belousov td->td_flags |= TDF_ASTPENDING; 800066d836bSKonstantin Belousov thread_unlock(td); 801066d836bSKonstantin Belousov } 802066d836bSKonstantin Belousov error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); 803066d836bSKonstantin Belousov return (error); 804066d836bSKonstantin Belousov } 805066d836bSKonstantin Belousov 806d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 807df8bae1dSRodney W. Grimes struct select_args { 808b08f7993SSujal Patel int nd; 809df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 810df8bae1dSRodney W. Grimes struct timeval *tv; 811df8bae1dSRodney W. Grimes }; 812d2d3e875SBruce Evans #endif 81326f9a767SRodney W. Grimes int 814066d836bSKonstantin Belousov select(struct thread *td, struct select_args *uap) 815df8bae1dSRodney W. Grimes { 8168f19eb88SIan Dowse struct timeval tv, *tvp; 8178f19eb88SIan Dowse int error; 8188f19eb88SIan Dowse 8198f19eb88SIan Dowse if (uap->tv != NULL) { 8208f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 8218f19eb88SIan Dowse if (error) 8228f19eb88SIan Dowse return (error); 8238f19eb88SIan Dowse tvp = &tv; 8248f19eb88SIan Dowse } else 8258f19eb88SIan Dowse tvp = NULL; 8268f19eb88SIan Dowse 827b55ef216SKonstantin Belousov return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp, 828b55ef216SKonstantin Belousov NFDBITS)); 8298f19eb88SIan Dowse } 8308f19eb88SIan Dowse 8318f19eb88SIan Dowse int 8328f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 833b55ef216SKonstantin Belousov fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) 8348f19eb88SIan Dowse { 835426da3bcSAlfred Perlstein struct filedesc *fdp; 836d5e4d7e1SBruce Evans /* 837d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 838d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 839d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 840d5e4d7e1SBruce Evans * of 256. 841d5e4d7e1SBruce Evans */ 842d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 843eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 84400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 8459ae6d334SKelly Yancey int error, timo; 846b55ef216SKonstantin Belousov u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; 847df8bae1dSRodney W. Grimes 8488f19eb88SIan Dowse if (nd < 0) 849acbfbfeaSSujal Patel return (EINVAL); 850426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 8512141453eSJeff Roberson if (nd > fdp->fd_lastfile + 1) 8522141453eSJeff Roberson nd = fdp->fd_lastfile + 1; 853b08f7993SSujal Patel 854d5e4d7e1SBruce Evans /* 855d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 856d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 857d5e4d7e1SBruce Evans */ 8588f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 859d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 860b55ef216SKonstantin Belousov ncpubytes = roundup(nd, abi_nfdbits) / NBBY; 861d5e4d7e1SBruce Evans nbufbytes = 0; 8628f19eb88SIan Dowse if (fd_in != NULL) 863d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 8648f19eb88SIan Dowse if (fd_ou != NULL) 865d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 8668f19eb88SIan Dowse if (fd_ex != NULL) 867d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 868d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 869d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 870d5e4d7e1SBruce Evans else 871a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 872b08f7993SSujal Patel 873b08f7993SSujal Patel /* 874d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 875d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 876d5e4d7e1SBruce Evans * together. 877b08f7993SSujal Patel */ 878d5e4d7e1SBruce Evans sbp = selbits; 879df8bae1dSRodney W. Grimes #define getbits(name, x) \ 880d5e4d7e1SBruce Evans do { \ 8818f19eb88SIan Dowse if (name == NULL) \ 882d5e4d7e1SBruce Evans ibits[x] = NULL; \ 883d5e4d7e1SBruce Evans else { \ 884d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 885d5e4d7e1SBruce Evans obits[x] = sbp; \ 886d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 887b55ef216SKonstantin Belousov error = copyin(name, ibits[x], ncpubytes); \ 888265fc98fSSeigo Tanimura if (error != 0) \ 889ace8398dSJeff Roberson goto done; \ 890b55ef216SKonstantin Belousov bzero((char *)ibits[x] + ncpubytes, \ 891b55ef216SKonstantin Belousov ncpbytes - ncpubytes); \ 892e04ac2feSJohn Baldwin } \ 893d5e4d7e1SBruce Evans } while (0) 8948f19eb88SIan Dowse getbits(fd_in, 0); 8958f19eb88SIan Dowse getbits(fd_ou, 1); 8968f19eb88SIan Dowse getbits(fd_ex, 2); 897df8bae1dSRodney W. Grimes #undef getbits 898d5e4d7e1SBruce Evans if (nbufbytes != 0) 899d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 900df8bae1dSRodney W. Grimes 9018f19eb88SIan Dowse if (tvp != NULL) { 9028f19eb88SIan Dowse atv = *tvp; 903df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 904df8bae1dSRodney W. Grimes error = EINVAL; 905ace8398dSJeff Roberson goto done; 906df8bae1dSRodney W. Grimes } 907c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 90800af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9099c386f6bSJohn Baldwin } else { 91000af9731SPoul-Henning Kamp atv.tv_sec = 0; 9119c386f6bSJohn Baldwin atv.tv_usec = 0; 9129c386f6bSJohn Baldwin } 91300af9731SPoul-Henning Kamp timo = 0; 914ace8398dSJeff Roberson seltdinit(td); 915ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 916ace8398dSJeff Roberson for (;;) { 9178f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 918ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 919ace8398dSJeff Roberson break; 9204da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 921c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 92285f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 923ace8398dSJeff Roberson break; 92400af9731SPoul-Henning Kamp ttv = atv; 92500af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 92600af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 92700af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 928df8bae1dSRodney W. Grimes } 929ace8398dSJeff Roberson error = seltdwait(td, timo); 930ace8398dSJeff Roberson if (error) 931ace8398dSJeff Roberson break; 932ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 933ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 934ace8398dSJeff Roberson break; 93585f190e4SAlfred Perlstein } 936ace8398dSJeff Roberson seltdclear(td); 937265fc98fSSeigo Tanimura 938df8bae1dSRodney W. Grimes done: 939df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 940df8bae1dSRodney W. Grimes if (error == ERESTART) 941df8bae1dSRodney W. Grimes error = EINTR; 942df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 943df8bae1dSRodney W. Grimes error = 0; 944df8bae1dSRodney W. Grimes #define putbits(name, x) \ 945b55ef216SKonstantin Belousov if (name && (error2 = copyout(obits[x], name, ncpubytes))) \ 946df8bae1dSRodney W. Grimes error = error2; 947df8bae1dSRodney W. Grimes if (error == 0) { 948df8bae1dSRodney W. Grimes int error2; 949df8bae1dSRodney W. Grimes 9508f19eb88SIan Dowse putbits(fd_in, 0); 9518f19eb88SIan Dowse putbits(fd_ou, 1); 9528f19eb88SIan Dowse putbits(fd_ex, 2); 953df8bae1dSRodney W. Grimes #undef putbits 954df8bae1dSRodney W. Grimes } 955d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 956d5e4d7e1SBruce Evans free(selbits, M_SELECT); 957ad2edad9SMatthew Dillon 958df8bae1dSRodney W. Grimes return (error); 959df8bae1dSRodney W. Grimes } 96011b763dfSJeff Roberson /* 96111b763dfSJeff Roberson * Convert a select bit set to poll flags. 962748b9df6SJeff Roberson * 96311b763dfSJeff Roberson * The backend always returns POLLHUP/POLLERR if appropriate and we 96411b763dfSJeff Roberson * return this as a set bit in any set. 96511b763dfSJeff Roberson */ 96611b763dfSJeff Roberson static int select_flags[3] = { 96711b763dfSJeff Roberson POLLRDNORM | POLLHUP | POLLERR, 96811b763dfSJeff Roberson POLLWRNORM | POLLHUP | POLLERR, 96911b763dfSJeff Roberson POLLRDBAND | POLLHUP | POLLERR 97011b763dfSJeff Roberson }; 97111b763dfSJeff Roberson 97211b763dfSJeff Roberson /* 97311b763dfSJeff Roberson * Compute the fo_poll flags required for a fd given by the index and 97411b763dfSJeff Roberson * bit position in the fd_mask array. 97511b763dfSJeff Roberson */ 97611b763dfSJeff Roberson static __inline int 97760b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit) 97811b763dfSJeff Roberson { 97911b763dfSJeff Roberson int flags; 98011b763dfSJeff Roberson int msk; 98111b763dfSJeff Roberson 98211b763dfSJeff Roberson flags = 0; 98311b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 98411b763dfSJeff Roberson if (ibits[msk] == NULL) 98511b763dfSJeff Roberson continue; 98660b7f468SStephane E. Potvin if ((ibits[msk][idx] & bit) == 0) 98711b763dfSJeff Roberson continue; 98811b763dfSJeff Roberson flags |= select_flags[msk]; 98911b763dfSJeff Roberson } 99011b763dfSJeff Roberson return (flags); 99111b763dfSJeff Roberson } 99211b763dfSJeff Roberson 99311b763dfSJeff Roberson /* 99411b763dfSJeff Roberson * Set the appropriate output bits given a mask of fired events and the 99511b763dfSJeff Roberson * input bits originally requested. 99611b763dfSJeff Roberson */ 99711b763dfSJeff Roberson static __inline int 99811b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events) 99911b763dfSJeff Roberson { 100011b763dfSJeff Roberson int msk; 100111b763dfSJeff Roberson int n; 100211b763dfSJeff Roberson 100311b763dfSJeff Roberson n = 0; 100411b763dfSJeff Roberson for (msk = 0; msk < 3; msk++) { 100511b763dfSJeff Roberson if ((events & select_flags[msk]) == 0) 100611b763dfSJeff Roberson continue; 100711b763dfSJeff Roberson if (ibits[msk] == NULL) 100811b763dfSJeff Roberson continue; 100911b763dfSJeff Roberson if ((ibits[msk][idx] & bit) == 0) 101011b763dfSJeff Roberson continue; 101111b763dfSJeff Roberson /* 101211b763dfSJeff Roberson * XXX Check for a duplicate set. This can occur because a 101311b763dfSJeff Roberson * socket calls selrecord() twice for each poll() call 101411b763dfSJeff Roberson * resulting in two selfds per real fd. selrescan() will 101511b763dfSJeff Roberson * call selsetbits twice as a result. 101611b763dfSJeff Roberson */ 101711b763dfSJeff Roberson if ((obits[msk][idx] & bit) != 0) 101811b763dfSJeff Roberson continue; 101911b763dfSJeff Roberson obits[msk][idx] |= bit; 102011b763dfSJeff Roberson n++; 102111b763dfSJeff Roberson } 102211b763dfSJeff Roberson 102311b763dfSJeff Roberson return (n); 102411b763dfSJeff Roberson } 1025df8bae1dSRodney W. Grimes 1026ace8398dSJeff Roberson /* 1027ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 1028ace8398dSJeff Roberson * completion. 1029ace8398dSJeff Roberson */ 1030ace8398dSJeff Roberson static int 1031ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 1032ace8398dSJeff Roberson { 103311b763dfSJeff Roberson struct filedesc *fdp; 103411b763dfSJeff Roberson struct selinfo *si; 1035ace8398dSJeff Roberson struct seltd *stp; 1036ace8398dSJeff Roberson struct selfd *sfp; 1037ace8398dSJeff Roberson struct selfd *sfn; 1038ace8398dSJeff Roberson struct file *fp; 10399cdacff1SJeff Roberson fd_mask bit; 10409cdacff1SJeff Roberson int fd, ev, n, idx; 1041ace8398dSJeff Roberson 104211b763dfSJeff Roberson fdp = td->td_proc->p_fd; 1043ace8398dSJeff Roberson stp = td->td_sel; 104411b763dfSJeff Roberson n = 0; 1045ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1046ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 1047ace8398dSJeff Roberson si = sfp->sf_si; 1048ace8398dSJeff Roberson selfdfree(stp, sfp); 1049ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1050ace8398dSJeff Roberson if (si != NULL) 1051ace8398dSJeff Roberson continue; 1052bf422e5fSJeff Roberson if ((fp = fget_unlocked(fdp, fd)) == NULL) 1053ace8398dSJeff Roberson return (EBADF); 105411b763dfSJeff Roberson idx = fd / NFDBITS; 10559cdacff1SJeff Roberson bit = (fd_mask)1 << (fd % NFDBITS); 105611b763dfSJeff Roberson ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td); 1057bf422e5fSJeff Roberson fdrop(fp, td); 105811b763dfSJeff Roberson if (ev != 0) 105911b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1060ace8398dSJeff Roberson } 1061ace8398dSJeff Roberson stp->st_flags = 0; 1062ace8398dSJeff Roberson td->td_retval[0] = n; 1063ace8398dSJeff Roberson return (0); 1064ace8398dSJeff Roberson } 1065ace8398dSJeff Roberson 1066ace8398dSJeff Roberson /* 1067ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 1068ace8398dSJeff Roberson * each selinfo. 1069ace8398dSJeff Roberson */ 1070265fc98fSSeigo Tanimura static int 1071b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 1072b40ce416SJulian Elischer struct thread *td; 1073b08f7993SSujal Patel fd_mask **ibits, **obits; 1074cb226aaaSPoul-Henning Kamp int nfd; 1075df8bae1dSRodney W. Grimes { 107611b763dfSJeff Roberson struct filedesc *fdp; 1077df8bae1dSRodney W. Grimes struct file *fp; 10789cdacff1SJeff Roberson fd_mask bit; 107911b763dfSJeff Roberson int ev, flags, end, fd; 10809cdacff1SJeff Roberson int n, idx; 1081df8bae1dSRodney W. Grimes 108211b763dfSJeff Roberson fdp = td->td_proc->p_fd; 108311b763dfSJeff Roberson n = 0; 10849cdacff1SJeff Roberson for (idx = 0, fd = 0; fd < nfd; idx++) { 108511b763dfSJeff Roberson end = imin(fd + NFDBITS, nfd); 108611b763dfSJeff Roberson for (bit = 1; fd < end; bit <<= 1, fd++) { 108711b763dfSJeff Roberson /* Compute the list of events we're interested in. */ 108811b763dfSJeff Roberson flags = selflags(ibits, idx, bit); 108911b763dfSJeff Roberson if (flags == 0) 1090f082218cSPeter Wemm continue; 1091bf422e5fSJeff Roberson if ((fp = fget_unlocked(fdp, fd)) == NULL) 1092df8bae1dSRodney W. Grimes return (EBADF); 1093ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 109411b763dfSJeff Roberson ev = fo_poll(fp, flags, td->td_ucred, td); 1095bf422e5fSJeff Roberson fdrop(fp, td); 109611b763dfSJeff Roberson if (ev != 0) 109711b763dfSJeff Roberson n += selsetbits(ibits, obits, idx, bit, ev); 1098df8bae1dSRodney W. Grimes } 1099df8bae1dSRodney W. Grimes } 110011b763dfSJeff Roberson 1101b40ce416SJulian Elischer td->td_retval[0] = n; 1102df8bae1dSRodney W. Grimes return (0); 1103df8bae1dSRodney W. Grimes } 1104df8bae1dSRodney W. Grimes 110542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 110642d11757SPeter Wemm struct poll_args { 110742d11757SPeter Wemm struct pollfd *fds; 110842d11757SPeter Wemm u_int nfds; 110942d11757SPeter Wemm int timeout; 111042d11757SPeter Wemm }; 111142d11757SPeter Wemm #endif 111242d11757SPeter Wemm int 1113b40ce416SJulian Elischer poll(td, uap) 1114b40ce416SJulian Elischer struct thread *td; 1115ea0237edSJonathan Lemon struct poll_args *uap; 111642d11757SPeter Wemm { 11172580f4e5SAndre Oppermann struct pollfd *bits; 11182580f4e5SAndre Oppermann struct pollfd smallbits[32]; 111900af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 11209ae6d334SKelly Yancey int error = 0, timo; 1121ace8398dSJeff Roberson u_int nfds; 112242d11757SPeter Wemm size_t ni; 112342d11757SPeter Wemm 1124d1e405c5SAlfred Perlstein nfds = uap->nfds; 1125374ae2a3SJeff Roberson if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 1126ace8398dSJeff Roberson return (EINVAL); 112789b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 112842d11757SPeter Wemm if (ni > sizeof(smallbits)) 1129a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 113042d11757SPeter Wemm else 113142d11757SPeter Wemm bits = smallbits; 1132d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 113342d11757SPeter Wemm if (error) 1134ace8398dSJeff Roberson goto done; 1135d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 1136d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 1137d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 113842d11757SPeter Wemm if (itimerfix(&atv)) { 113942d11757SPeter Wemm error = EINVAL; 1140ace8398dSJeff Roberson goto done; 114142d11757SPeter Wemm } 1142c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 114300af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 11449c386f6bSJohn Baldwin } else { 114500af9731SPoul-Henning Kamp atv.tv_sec = 0; 11469c386f6bSJohn Baldwin atv.tv_usec = 0; 11479c386f6bSJohn Baldwin } 114800af9731SPoul-Henning Kamp timo = 0; 1149ace8398dSJeff Roberson seltdinit(td); 1150ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1151ace8398dSJeff Roberson for (;;) { 11522580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 1153ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1154ace8398dSJeff Roberson break; 11554da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1156c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 115785f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 1158ace8398dSJeff Roberson break; 115900af9731SPoul-Henning Kamp ttv = atv; 116000af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 116100af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 116200af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 116342d11757SPeter Wemm } 1164ace8398dSJeff Roberson error = seltdwait(td, timo); 1165ace8398dSJeff Roberson if (error) 1166ace8398dSJeff Roberson break; 1167ace8398dSJeff Roberson error = pollrescan(td); 1168ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1169ace8398dSJeff Roberson break; 117085f190e4SAlfred Perlstein } 1171ace8398dSJeff Roberson seltdclear(td); 1172265fc98fSSeigo Tanimura 117342d11757SPeter Wemm done: 117442d11757SPeter Wemm /* poll is not restarted after signals... */ 117542d11757SPeter Wemm if (error == ERESTART) 117642d11757SPeter Wemm error = EINTR; 117742d11757SPeter Wemm if (error == EWOULDBLOCK) 117842d11757SPeter Wemm error = 0; 117942d11757SPeter Wemm if (error == 0) { 1180ae81968fSRobert Watson error = pollout(bits, uap->fds, nfds); 118142d11757SPeter Wemm if (error) 118242d11757SPeter Wemm goto out; 118342d11757SPeter Wemm } 118442d11757SPeter Wemm out: 118542d11757SPeter Wemm if (ni > sizeof(smallbits)) 118642d11757SPeter Wemm free(bits, M_TEMP); 118742d11757SPeter Wemm return (error); 118842d11757SPeter Wemm } 118942d11757SPeter Wemm 119042d11757SPeter Wemm static int 1191ace8398dSJeff Roberson pollrescan(struct thread *td) 1192ace8398dSJeff Roberson { 1193ace8398dSJeff Roberson struct seltd *stp; 1194ace8398dSJeff Roberson struct selfd *sfp; 1195ace8398dSJeff Roberson struct selfd *sfn; 1196ace8398dSJeff Roberson struct selinfo *si; 1197ace8398dSJeff Roberson struct filedesc *fdp; 1198ace8398dSJeff Roberson struct file *fp; 1199ace8398dSJeff Roberson struct pollfd *fd; 1200ace8398dSJeff Roberson int n; 1201ace8398dSJeff Roberson 1202ace8398dSJeff Roberson n = 0; 1203ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1204ace8398dSJeff Roberson stp = td->td_sel; 1205ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1206ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1207ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1208ace8398dSJeff Roberson si = sfp->sf_si; 1209ace8398dSJeff Roberson selfdfree(stp, sfp); 1210ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1211ace8398dSJeff Roberson if (si != NULL) 1212ace8398dSJeff Roberson continue; 1213ace8398dSJeff Roberson fp = fdp->fd_ofiles[fd->fd]; 1214ace8398dSJeff Roberson if (fp == NULL) { 1215ace8398dSJeff Roberson fd->revents = POLLNVAL; 1216ace8398dSJeff Roberson n++; 1217ace8398dSJeff Roberson continue; 1218ace8398dSJeff Roberson } 1219ace8398dSJeff Roberson /* 1220ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1221ace8398dSJeff Roberson * POLLERR if appropriate. 1222ace8398dSJeff Roberson */ 1223ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1224ace8398dSJeff Roberson if (fd->revents != 0) 1225ace8398dSJeff Roberson n++; 1226ace8398dSJeff Roberson } 1227ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1228ace8398dSJeff Roberson stp->st_flags = 0; 1229ace8398dSJeff Roberson td->td_retval[0] = n; 1230ace8398dSJeff Roberson return (0); 1231ace8398dSJeff Roberson } 1232ace8398dSJeff Roberson 1233ace8398dSJeff Roberson 1234ace8398dSJeff Roberson static int 1235ae81968fSRobert Watson pollout(fds, ufds, nfd) 1236ae81968fSRobert Watson struct pollfd *fds; 1237ae81968fSRobert Watson struct pollfd *ufds; 1238ae81968fSRobert Watson u_int nfd; 1239ae81968fSRobert Watson { 1240ae81968fSRobert Watson int error = 0; 1241ae81968fSRobert Watson u_int i = 0; 1242ae81968fSRobert Watson 1243ae81968fSRobert Watson for (i = 0; i < nfd; i++) { 1244ae81968fSRobert Watson error = copyout(&fds->revents, &ufds->revents, 1245ae81968fSRobert Watson sizeof(ufds->revents)); 1246ae81968fSRobert Watson if (error) 1247ae81968fSRobert Watson return (error); 1248ae81968fSRobert Watson fds++; 1249ae81968fSRobert Watson ufds++; 1250ae81968fSRobert Watson } 1251ae81968fSRobert Watson return (0); 1252ae81968fSRobert Watson } 1253ae81968fSRobert Watson 1254ae81968fSRobert Watson static int 1255b40ce416SJulian Elischer pollscan(td, fds, nfd) 1256b40ce416SJulian Elischer struct thread *td; 125742d11757SPeter Wemm struct pollfd *fds; 1258ea0237edSJonathan Lemon u_int nfd; 125942d11757SPeter Wemm { 1260ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 126142d11757SPeter Wemm int i; 126242d11757SPeter Wemm struct file *fp; 126342d11757SPeter Wemm int n = 0; 126442d11757SPeter Wemm 12655e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1266eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1267337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 126842d11757SPeter Wemm fds->revents = POLLNVAL; 126942d11757SPeter Wemm n++; 1270337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1271337c9691SJordan K. Hubbard fds->revents = 0; 127242d11757SPeter Wemm } else { 127342d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1274279d7226SMatthew Dillon if (fp == NULL) { 127542d11757SPeter Wemm fds->revents = POLLNVAL; 127642d11757SPeter Wemm n++; 127742d11757SPeter Wemm } else { 12782087c896SBruce Evans /* 12792087c896SBruce Evans * Note: backend also returns POLLHUP and 12802087c896SBruce Evans * POLLERR if appropriate. 12812087c896SBruce Evans */ 1282ace8398dSJeff Roberson selfdalloc(td, fds); 128313ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1284ea6027a8SRobert Watson td->td_ucred, td); 1285f2159cc7SKonstantin Belousov /* 1286f2159cc7SKonstantin Belousov * POSIX requires POLLOUT to be never 1287f2159cc7SKonstantin Belousov * set simultaneously with POLLHUP. 1288f2159cc7SKonstantin Belousov */ 1289f2159cc7SKonstantin Belousov if ((fds->revents & POLLHUP) != 0) 1290f2159cc7SKonstantin Belousov fds->revents &= ~POLLOUT; 1291f2159cc7SKonstantin Belousov 129242d11757SPeter Wemm if (fds->revents != 0) 129342d11757SPeter Wemm n++; 129442d11757SPeter Wemm } 129542d11757SPeter Wemm } 129642d11757SPeter Wemm } 12975e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1298b40ce416SJulian Elischer td->td_retval[0] = n; 129942d11757SPeter Wemm return (0); 130042d11757SPeter Wemm } 130142d11757SPeter Wemm 130242d11757SPeter Wemm /* 130342d11757SPeter Wemm * OpenBSD poll system call. 13040c14ff0eSRobert Watson * 130542d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 130642d11757SPeter Wemm */ 130742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 130842d11757SPeter Wemm struct openbsd_poll_args { 130942d11757SPeter Wemm struct pollfd *fds; 131042d11757SPeter Wemm u_int nfds; 131142d11757SPeter Wemm int timeout; 131242d11757SPeter Wemm }; 131342d11757SPeter Wemm #endif 131442d11757SPeter Wemm int 1315b40ce416SJulian Elischer openbsd_poll(td, uap) 1316b40ce416SJulian Elischer register struct thread *td; 131742d11757SPeter Wemm register struct openbsd_poll_args *uap; 131842d11757SPeter Wemm { 1319b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 132042d11757SPeter Wemm } 132142d11757SPeter Wemm 132285f190e4SAlfred Perlstein /* 1323ace8398dSJeff Roberson * XXX This was created specifically to support netncp and netsmb. This 1324ace8398dSJeff Roberson * allows the caller to specify a socket to wait for events on. It returns 1325ace8398dSJeff Roberson * 0 if any events matched and an error otherwise. There is no way to 1326ace8398dSJeff Roberson * determine which events fired. 132785f190e4SAlfred Perlstein */ 1328ace8398dSJeff Roberson int 1329ace8398dSJeff Roberson selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 133085f190e4SAlfred Perlstein { 1331ace8398dSJeff Roberson struct timeval atv, rtv, ttv; 1332ace8398dSJeff Roberson int error, timo; 133385f190e4SAlfred Perlstein 1334ace8398dSJeff Roberson if (tvp != NULL) { 1335ace8398dSJeff Roberson atv = *tvp; 1336ace8398dSJeff Roberson if (itimerfix(&atv)) 1337ace8398dSJeff Roberson return (EINVAL); 1338ace8398dSJeff Roberson getmicrouptime(&rtv); 1339ace8398dSJeff Roberson timevaladd(&atv, &rtv); 1340ace8398dSJeff Roberson } else { 1341ace8398dSJeff Roberson atv.tv_sec = 0; 1342ace8398dSJeff Roberson atv.tv_usec = 0; 1343ace8398dSJeff Roberson } 1344ace8398dSJeff Roberson 1345ace8398dSJeff Roberson timo = 0; 1346ace8398dSJeff Roberson seltdinit(td); 1347ace8398dSJeff Roberson /* 1348ace8398dSJeff Roberson * Iterate until the timeout expires or the socket becomes ready. 1349ace8398dSJeff Roberson */ 1350ace8398dSJeff Roberson for (;;) { 1351ace8398dSJeff Roberson selfdalloc(td, NULL); 1352ace8398dSJeff Roberson error = sopoll(so, events, NULL, td); 1353ace8398dSJeff Roberson /* error here is actually the ready events. */ 1354ace8398dSJeff Roberson if (error) 1355ace8398dSJeff Roberson return (0); 1356ace8398dSJeff Roberson if (atv.tv_sec || atv.tv_usec) { 1357ace8398dSJeff Roberson getmicrouptime(&rtv); 1358ace8398dSJeff Roberson if (timevalcmp(&rtv, &atv, >=)) { 1359ace8398dSJeff Roberson seltdclear(td); 1360ace8398dSJeff Roberson return (EWOULDBLOCK); 1361ace8398dSJeff Roberson } 1362ace8398dSJeff Roberson ttv = atv; 1363ace8398dSJeff Roberson timevalsub(&ttv, &rtv); 1364ace8398dSJeff Roberson timo = ttv.tv_sec > 24 * 60 * 60 ? 1365ace8398dSJeff Roberson 24 * 60 * 60 * hz : tvtohz(&ttv); 1366ace8398dSJeff Roberson } 1367ace8398dSJeff Roberson error = seltdwait(td, timo); 1368ace8398dSJeff Roberson seltdclear(td); 1369ace8398dSJeff Roberson if (error) 1370ace8398dSJeff Roberson break; 1371ace8398dSJeff Roberson } 1372ace8398dSJeff Roberson /* XXX Duplicates ncp/smb behavior. */ 1373ace8398dSJeff Roberson if (error == ERESTART) 1374ace8398dSJeff Roberson error = 0; 1375ace8398dSJeff Roberson return (error); 1376ace8398dSJeff Roberson } 1377ace8398dSJeff Roberson 1378ace8398dSJeff Roberson /* 1379ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1380ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1381ace8398dSJeff Roberson */ 1382ace8398dSJeff Roberson static void 1383ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1384ace8398dSJeff Roberson { 1385ace8398dSJeff Roberson struct seltd *stp; 1386ace8398dSJeff Roberson 1387ace8398dSJeff Roberson stp = td->td_sel; 1388ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1389ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1390ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1391ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1392ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1393ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1394ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1395ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1396ace8398dSJeff Roberson } 1397ace8398dSJeff Roberson 1398ace8398dSJeff Roberson static void 1399ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1400ace8398dSJeff Roberson { 1401ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 1402ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1403ace8398dSJeff Roberson if (sfp->sf_si) 1404ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1405ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 1406ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 140785f190e4SAlfred Perlstein } 140885f190e4SAlfred Perlstein 1409df8bae1dSRodney W. Grimes /* 1410df8bae1dSRodney W. Grimes * Record a select request. 1411df8bae1dSRodney W. Grimes */ 1412df8bae1dSRodney W. Grimes void 1413df8bae1dSRodney W. Grimes selrecord(selector, sip) 1414b40ce416SJulian Elischer struct thread *selector; 1415df8bae1dSRodney W. Grimes struct selinfo *sip; 1416df8bae1dSRodney W. Grimes { 1417ace8398dSJeff Roberson struct selfd *sfp; 1418ace8398dSJeff Roberson struct seltd *stp; 1419ace8398dSJeff Roberson struct mtx *mtxp; 1420df8bae1dSRodney W. Grimes 1421ace8398dSJeff Roberson stp = selector->td_sel; 142285f190e4SAlfred Perlstein /* 1423ace8398dSJeff Roberson * Don't record when doing a rescan. 142485f190e4SAlfred Perlstein */ 1425ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1426ace8398dSJeff Roberson return; 1427ace8398dSJeff Roberson /* 1428ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1429ace8398dSJeff Roberson */ 1430ace8398dSJeff Roberson sfp = NULL; 1431ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1432ace8398dSJeff Roberson stp->st_free1 = NULL; 1433ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1434ace8398dSJeff Roberson stp->st_free2 = NULL; 1435ace8398dSJeff Roberson else 1436ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 14372141453eSJeff Roberson mtxp = sip->si_mtx; 14382141453eSJeff Roberson if (mtxp == NULL) 14392141453eSJeff Roberson mtxp = mtx_pool_find(mtxpool_select, sip); 1440ace8398dSJeff Roberson /* 1441ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1442ace8398dSJeff Roberson */ 1443ace8398dSJeff Roberson sfp->sf_si = sip; 1444ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1445ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1446ace8398dSJeff Roberson /* 1447ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1448ace8398dSJeff Roberson */ 1449ace8398dSJeff Roberson mtx_lock(mtxp); 1450ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1451ace8398dSJeff Roberson sip->si_mtx = mtxp; 1452ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 145385f190e4SAlfred Perlstein } 1454ace8398dSJeff Roberson /* 1455ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1456ace8398dSJeff Roberson */ 1457ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1458ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1459df8bae1dSRodney W. Grimes } 1460df8bae1dSRodney W. Grimes 1461512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1462df8bae1dSRodney W. Grimes void 1463df8bae1dSRodney W. Grimes selwakeup(sip) 146485f190e4SAlfred Perlstein struct selinfo *sip; 1465df8bae1dSRodney W. Grimes { 1466512824f8SSeigo Tanimura doselwakeup(sip, -1); 1467512824f8SSeigo Tanimura } 1468512824f8SSeigo Tanimura 1469512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1470512824f8SSeigo Tanimura void 1471512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1472512824f8SSeigo Tanimura struct selinfo *sip; 1473512824f8SSeigo Tanimura int pri; 1474512824f8SSeigo Tanimura { 1475512824f8SSeigo Tanimura doselwakeup(sip, pri); 1476512824f8SSeigo Tanimura } 1477512824f8SSeigo Tanimura 1478512824f8SSeigo Tanimura /* 1479512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1480512824f8SSeigo Tanimura */ 1481512824f8SSeigo Tanimura static void 1482512824f8SSeigo Tanimura doselwakeup(sip, pri) 1483512824f8SSeigo Tanimura struct selinfo *sip; 1484512824f8SSeigo Tanimura int pri; 1485512824f8SSeigo Tanimura { 1486ace8398dSJeff Roberson struct selfd *sfp; 1487ace8398dSJeff Roberson struct selfd *sfn; 1488ace8398dSJeff Roberson struct seltd *stp; 1489df8bae1dSRodney W. Grimes 1490ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1491ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1492b40ce416SJulian Elischer return; 1493ace8398dSJeff Roberson /* 1494ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1495ace8398dSJeff Roberson */ 1496ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1497ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1498ace8398dSJeff Roberson /* 1499ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1500ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1501ace8398dSJeff Roberson */ 1502ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1503ace8398dSJeff Roberson sfp->sf_si = NULL; 1504ace8398dSJeff Roberson stp = sfp->sf_td; 1505ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1506ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1507ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1508ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1509b40ce416SJulian Elischer } 1510ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1511ace8398dSJeff Roberson } 1512ace8398dSJeff Roberson 1513ace8398dSJeff Roberson static void 1514ace8398dSJeff Roberson seltdinit(struct thread *td) 1515ace8398dSJeff Roberson { 1516ace8398dSJeff Roberson struct seltd *stp; 1517ace8398dSJeff Roberson 1518ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1519ace8398dSJeff Roberson goto out; 1520ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1521ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1522ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1523ace8398dSJeff Roberson out: 1524ace8398dSJeff Roberson stp->st_flags = 0; 1525ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1526ace8398dSJeff Roberson } 1527ace8398dSJeff Roberson 1528ace8398dSJeff Roberson static int 1529ace8398dSJeff Roberson seltdwait(struct thread *td, int timo) 1530ace8398dSJeff Roberson { 1531ace8398dSJeff Roberson struct seltd *stp; 1532ace8398dSJeff Roberson int error; 1533ace8398dSJeff Roberson 1534ace8398dSJeff Roberson stp = td->td_sel; 1535ace8398dSJeff Roberson /* 1536ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1537ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1538ace8398dSJeff Roberson */ 1539ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1540ace8398dSJeff Roberson /* 1541ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1542ace8398dSJeff Roberson */ 1543ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1544ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1545ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1546ace8398dSJeff Roberson return (0); 1547ace8398dSJeff Roberson } 1548ace8398dSJeff Roberson if (timo > 0) 1549ace8398dSJeff Roberson error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo); 1550ace8398dSJeff Roberson else 1551ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1552ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1553ace8398dSJeff Roberson 1554ace8398dSJeff Roberson return (error); 1555ace8398dSJeff Roberson } 1556ace8398dSJeff Roberson 1557ace8398dSJeff Roberson void 1558ace8398dSJeff Roberson seltdfini(struct thread *td) 1559ace8398dSJeff Roberson { 1560ace8398dSJeff Roberson struct seltd *stp; 1561ace8398dSJeff Roberson 1562ace8398dSJeff Roberson stp = td->td_sel; 1563ace8398dSJeff Roberson if (stp == NULL) 1564ace8398dSJeff Roberson return; 1565ace8398dSJeff Roberson if (stp->st_free1) 1566ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1567ace8398dSJeff Roberson if (stp->st_free2) 1568ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1569ace8398dSJeff Roberson td->td_sel = NULL; 1570ace8398dSJeff Roberson free(stp, M_SELECT); 1571ace8398dSJeff Roberson } 1572ace8398dSJeff Roberson 1573ace8398dSJeff Roberson /* 1574ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1575ace8398dSJeff Roberson * polling. 1576ace8398dSJeff Roberson */ 1577ace8398dSJeff Roberson static void 1578ace8398dSJeff Roberson seltdclear(struct thread *td) 1579ace8398dSJeff Roberson { 1580ace8398dSJeff Roberson struct seltd *stp; 1581ace8398dSJeff Roberson struct selfd *sfp; 1582ace8398dSJeff Roberson struct selfd *sfn; 1583ace8398dSJeff Roberson 1584ace8398dSJeff Roberson stp = td->td_sel; 1585ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1586ace8398dSJeff Roberson selfdfree(stp, sfp); 1587ace8398dSJeff Roberson stp->st_flags = 0; 1588df8bae1dSRodney W. Grimes } 1589265fc98fSSeigo Tanimura 15904d77a549SAlfred Perlstein static void selectinit(void *); 1591ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1592265fc98fSSeigo Tanimura static void 1593ace8398dSJeff Roberson selectinit(void *dummy __unused) 1594265fc98fSSeigo Tanimura { 15952141453eSJeff Roberson 1596ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1597ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 15982141453eSJeff Roberson mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF); 1599265fc98fSSeigo Tanimura } 1600