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> 55104a9b7eSAlexander Kabaev #include <sys/limits.h> 56df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5742d11757SPeter Wemm #include <sys/poll.h> 5889b71647SPeter Wemm #include <sys/resourcevar.h> 590a2c3d48SGarrett Wollman #include <sys/selinfo.h> 6044f3b092SJohn Baldwin #include <sys/sleepqueue.h> 618f19eb88SIan Dowse #include <sys/syscallsubr.h> 628cb96f20SPeter Wemm #include <sys/sysctl.h> 6342d11757SPeter Wemm #include <sys/sysent.h> 649bbee259SAndrey A. Chernov #include <sys/vnode.h> 65279d7226SMatthew Dillon #include <sys/bio.h> 66279d7226SMatthew Dillon #include <sys/buf.h> 67265fc98fSSeigo Tanimura #include <sys/condvar.h> 68df8bae1dSRodney W. Grimes #ifdef KTRACE 69df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 70df8bae1dSRodney W. Grimes #endif 71df8bae1dSRodney W. Grimes 72ace8398dSJeff Roberson #include <sys/ktr.h> 73ace8398dSJeff Roberson 74a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer"); 75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer"); 76a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's"); 7755166637SPoul-Henning Kamp 78bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 79ace8398dSJeff Roberson static int pollrescan(struct thread *); 80bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 81ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 82ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 83ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 84bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 85bcd9e0ddSJohn Baldwin off_t, int); 86bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 87bcd9e0ddSJohn Baldwin off_t, int); 88512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 89ace8398dSJeff Roberson static void seltdinit(struct thread *); 90ace8398dSJeff Roberson static int seltdwait(struct thread *, int); 91ace8398dSJeff Roberson static void seltdclear(struct thread *); 92ace8398dSJeff Roberson 93ace8398dSJeff Roberson /* 94ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 95ace8398dSJeff Roberson * 96ace8398dSJeff Roberson * t - protected by st_mtx 97ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 98ace8398dSJeff Roberson */ 99ace8398dSJeff Roberson struct seltd { 100ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 101ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 102ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 103ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 104ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 105ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 106ace8398dSJeff Roberson }; 107ace8398dSJeff Roberson 108ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 109ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 110ace8398dSJeff Roberson 111ace8398dSJeff Roberson /* 112ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 113ace8398dSJeff Roberson * f - protected by sf_mtx 114ace8398dSJeff Roberson */ 115ace8398dSJeff Roberson struct selfd { 116ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 117ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 118ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 119ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 120ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 121ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 122ace8398dSJeff Roberson }; 123ace8398dSJeff Roberson 124ace8398dSJeff Roberson static uma_zone_t selfd_zone; 1258fe387abSDmitrij Tejblum 126d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 127df8bae1dSRodney W. Grimes struct read_args { 128df8bae1dSRodney W. Grimes int fd; 129134e06feSBruce Evans void *buf; 130134e06feSBruce Evans size_t nbyte; 131df8bae1dSRodney W. Grimes }; 132d2d3e875SBruce Evans #endif 13326f9a767SRodney W. Grimes int 134b40ce416SJulian Elischer read(td, uap) 135b40ce416SJulian Elischer struct thread *td; 136b064d43dSMatthew Dillon struct read_args *uap; 137df8bae1dSRodney W. Grimes { 138bcd9e0ddSJohn Baldwin struct uio auio; 139bcd9e0ddSJohn Baldwin struct iovec aiov; 140279d7226SMatthew Dillon int error; 141df8bae1dSRodney W. Grimes 142bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 143bcd9e0ddSJohn Baldwin return (EINVAL); 144bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 145bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 146bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 147bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 148bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 149bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 150bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 151279d7226SMatthew Dillon return(error); 152df8bae1dSRodney W. Grimes } 153df8bae1dSRodney W. Grimes 154df8bae1dSRodney W. Grimes /* 155bcd9e0ddSJohn Baldwin * Positioned read system call 1564160ccd9SAlan Cox */ 1574160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1584160ccd9SAlan Cox struct pread_args { 1594160ccd9SAlan Cox int fd; 1604160ccd9SAlan Cox void *buf; 1614160ccd9SAlan Cox size_t nbyte; 1628fe387abSDmitrij Tejblum int pad; 1634160ccd9SAlan Cox off_t offset; 1644160ccd9SAlan Cox }; 1654160ccd9SAlan Cox #endif 1664160ccd9SAlan Cox int 167b40ce416SJulian Elischer pread(td, uap) 168b40ce416SJulian Elischer struct thread *td; 169b064d43dSMatthew Dillon struct pread_args *uap; 1704160ccd9SAlan Cox { 1714160ccd9SAlan Cox struct uio auio; 1724160ccd9SAlan Cox struct iovec aiov; 173bcd9e0ddSJohn Baldwin int error; 1744160ccd9SAlan Cox 175bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 176bcd9e0ddSJohn Baldwin return (EINVAL); 177bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 178bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 1794160ccd9SAlan Cox auio.uio_iov = &aiov; 1804160ccd9SAlan Cox auio.uio_iovcnt = 1; 181bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 1824160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 183bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, &auio, uap->offset); 1844160ccd9SAlan Cox return(error); 1854160ccd9SAlan Cox } 1864160ccd9SAlan Cox 187c2815ad5SPeter Wemm int 188c2815ad5SPeter Wemm freebsd6_pread(td, uap) 189c2815ad5SPeter Wemm struct thread *td; 190c2815ad5SPeter Wemm struct freebsd6_pread_args *uap; 191c2815ad5SPeter Wemm { 192c2815ad5SPeter Wemm struct pread_args oargs; 193c2815ad5SPeter Wemm 194c2815ad5SPeter Wemm oargs.fd = uap->fd; 195c2815ad5SPeter Wemm oargs.buf = uap->buf; 196c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 197c2815ad5SPeter Wemm oargs.offset = uap->offset; 198c2815ad5SPeter Wemm return (pread(td, &oargs)); 199c2815ad5SPeter Wemm } 200c2815ad5SPeter Wemm 2014160ccd9SAlan Cox /* 202df8bae1dSRodney W. Grimes * Scatter read system call. 203df8bae1dSRodney W. Grimes */ 204d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 205df8bae1dSRodney W. Grimes struct readv_args { 2067147b19dSBruce Evans int fd; 207df8bae1dSRodney W. Grimes struct iovec *iovp; 208df8bae1dSRodney W. Grimes u_int iovcnt; 209df8bae1dSRodney W. Grimes }; 210d2d3e875SBruce Evans #endif 21126f9a767SRodney W. Grimes int 212552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap) 213df8bae1dSRodney W. Grimes { 214b88ec951SJohn Baldwin struct uio *auio; 215b88ec951SJohn Baldwin int error; 216b88ec951SJohn Baldwin 217b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 218b88ec951SJohn Baldwin if (error) 219b88ec951SJohn Baldwin return (error); 220b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 221b88ec951SJohn Baldwin free(auio, M_IOV); 222b88ec951SJohn Baldwin return (error); 223b88ec951SJohn Baldwin } 224b88ec951SJohn Baldwin 225b88ec951SJohn Baldwin int 226b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 227b88ec951SJohn Baldwin { 228b064d43dSMatthew Dillon struct file *fp; 229bcd9e0ddSJohn Baldwin int error; 230bcd9e0ddSJohn Baldwin 231bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 232bcd9e0ddSJohn Baldwin if (error) 233bcd9e0ddSJohn Baldwin return (error); 234bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 235bcd9e0ddSJohn Baldwin fdrop(fp, td); 236bcd9e0ddSJohn Baldwin return (error); 237bcd9e0ddSJohn Baldwin } 238bcd9e0ddSJohn Baldwin 239bcd9e0ddSJohn Baldwin /* 240bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 241bcd9e0ddSJohn Baldwin */ 242bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 243bcd9e0ddSJohn Baldwin struct preadv_args { 244bcd9e0ddSJohn Baldwin int fd; 245bcd9e0ddSJohn Baldwin struct iovec *iovp; 246bcd9e0ddSJohn Baldwin u_int iovcnt; 247bcd9e0ddSJohn Baldwin off_t offset; 248bcd9e0ddSJohn Baldwin }; 249bcd9e0ddSJohn Baldwin #endif 250bcd9e0ddSJohn Baldwin int 251bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap) 252bcd9e0ddSJohn Baldwin { 253bcd9e0ddSJohn Baldwin struct uio *auio; 254bcd9e0ddSJohn Baldwin int error; 255bcd9e0ddSJohn Baldwin 256bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 257bcd9e0ddSJohn Baldwin if (error) 258bcd9e0ddSJohn Baldwin return (error); 259bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 260bcd9e0ddSJohn Baldwin free(auio, M_IOV); 261bcd9e0ddSJohn Baldwin return (error); 262bcd9e0ddSJohn Baldwin } 263bcd9e0ddSJohn Baldwin 264bcd9e0ddSJohn Baldwin int 265bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset) 266bcd9e0ddSJohn Baldwin struct thread *td; 267bcd9e0ddSJohn Baldwin int fd; 268bcd9e0ddSJohn Baldwin struct uio *auio; 269bcd9e0ddSJohn Baldwin off_t offset; 270bcd9e0ddSJohn Baldwin { 271bcd9e0ddSJohn Baldwin struct file *fp; 272bcd9e0ddSJohn Baldwin int error; 273bcd9e0ddSJohn Baldwin 274bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 275bcd9e0ddSJohn Baldwin if (error) 276bcd9e0ddSJohn Baldwin return (error); 277bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 278bcd9e0ddSJohn Baldwin error = ESPIPE; 279bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 280bcd9e0ddSJohn Baldwin error = EINVAL; 281bcd9e0ddSJohn Baldwin else 282bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 283bcd9e0ddSJohn Baldwin fdrop(fp, td); 284bcd9e0ddSJohn Baldwin return (error); 285bcd9e0ddSJohn Baldwin } 286bcd9e0ddSJohn Baldwin 287bcd9e0ddSJohn Baldwin /* 288bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 289bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 290bcd9e0ddSJohn Baldwin */ 291bcd9e0ddSJohn Baldwin static int 292bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 293bcd9e0ddSJohn Baldwin struct thread *td; 294bcd9e0ddSJohn Baldwin int fd; 295bcd9e0ddSJohn Baldwin struct file *fp; 296bcd9e0ddSJohn Baldwin struct uio *auio; 297bcd9e0ddSJohn Baldwin off_t offset; 298bcd9e0ddSJohn Baldwin int flags; 299bcd9e0ddSJohn Baldwin { 300bcd9e0ddSJohn Baldwin ssize_t cnt; 30182641acdSAlan Cox int error; 302df8bae1dSRodney W. Grimes #ifdef KTRACE 303552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 304df8bae1dSRodney W. Grimes #endif 305df8bae1dSRodney W. Grimes 3064f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3074f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3084f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3094f8d23d6SPoul-Henning Kamp return(0); 3104f8d23d6SPoul-Henning Kamp } 311552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 312bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 313552afd9cSPoul-Henning Kamp auio->uio_td = td; 314df8bae1dSRodney W. Grimes #ifdef KTRACE 315552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 316552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 317df8bae1dSRodney W. Grimes #endif 318552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 319bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 320552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 321df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 322df8bae1dSRodney W. Grimes error = 0; 323279d7226SMatthew Dillon } 324552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 325df8bae1dSRodney W. Grimes #ifdef KTRACE 326552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 327552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 328b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes #endif 331b40ce416SJulian Elischer td->td_retval[0] = cnt; 332df8bae1dSRodney W. Grimes return (error); 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes 335d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 336df8bae1dSRodney W. Grimes struct write_args { 337df8bae1dSRodney W. Grimes int fd; 338134e06feSBruce Evans const void *buf; 339134e06feSBruce Evans size_t nbyte; 340df8bae1dSRodney W. Grimes }; 341d2d3e875SBruce Evans #endif 34226f9a767SRodney W. Grimes int 343b40ce416SJulian Elischer write(td, uap) 344b40ce416SJulian Elischer struct thread *td; 345b064d43dSMatthew Dillon struct write_args *uap; 346df8bae1dSRodney W. Grimes { 347bcd9e0ddSJohn Baldwin struct uio auio; 348bcd9e0ddSJohn Baldwin struct iovec aiov; 349279d7226SMatthew Dillon int error; 350df8bae1dSRodney W. Grimes 351bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 352bcd9e0ddSJohn Baldwin return (EINVAL); 353bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 354bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 355bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 356bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 357bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 358bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 359bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 360279d7226SMatthew Dillon return(error); 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes 363df8bae1dSRodney W. Grimes /* 3640c14ff0eSRobert Watson * Positioned write system call. 3654160ccd9SAlan Cox */ 3664160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3674160ccd9SAlan Cox struct pwrite_args { 3684160ccd9SAlan Cox int fd; 3694160ccd9SAlan Cox const void *buf; 3704160ccd9SAlan Cox size_t nbyte; 3718fe387abSDmitrij Tejblum int pad; 3724160ccd9SAlan Cox off_t offset; 3734160ccd9SAlan Cox }; 3744160ccd9SAlan Cox #endif 3754160ccd9SAlan Cox int 376b40ce416SJulian Elischer pwrite(td, uap) 377b40ce416SJulian Elischer struct thread *td; 378b064d43dSMatthew Dillon struct pwrite_args *uap; 3794160ccd9SAlan Cox { 3804160ccd9SAlan Cox struct uio auio; 3814160ccd9SAlan Cox struct iovec aiov; 382bcd9e0ddSJohn Baldwin int error; 3834160ccd9SAlan Cox 384bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 385bcd9e0ddSJohn Baldwin return (EINVAL); 386bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 387bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 3884160ccd9SAlan Cox auio.uio_iov = &aiov; 3894160ccd9SAlan Cox auio.uio_iovcnt = 1; 390bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 3914160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 392bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, &auio, uap->offset); 3934160ccd9SAlan Cox return(error); 3944160ccd9SAlan Cox } 3954160ccd9SAlan Cox 396c2815ad5SPeter Wemm int 397c2815ad5SPeter Wemm freebsd6_pwrite(td, uap) 398c2815ad5SPeter Wemm struct thread *td; 399c2815ad5SPeter Wemm struct freebsd6_pwrite_args *uap; 400c2815ad5SPeter Wemm { 401c2815ad5SPeter Wemm struct pwrite_args oargs; 402c2815ad5SPeter Wemm 403c2815ad5SPeter Wemm oargs.fd = uap->fd; 404c2815ad5SPeter Wemm oargs.buf = uap->buf; 405c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 406c2815ad5SPeter Wemm oargs.offset = uap->offset; 407c2815ad5SPeter Wemm return (pwrite(td, &oargs)); 408c2815ad5SPeter Wemm } 409c2815ad5SPeter Wemm 4104160ccd9SAlan Cox /* 4110c14ff0eSRobert Watson * Gather write system call. 412df8bae1dSRodney W. Grimes */ 413d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 414df8bae1dSRodney W. Grimes struct writev_args { 415df8bae1dSRodney W. Grimes int fd; 416df8bae1dSRodney W. Grimes struct iovec *iovp; 417df8bae1dSRodney W. Grimes u_int iovcnt; 418df8bae1dSRodney W. Grimes }; 419d2d3e875SBruce Evans #endif 42026f9a767SRodney W. Grimes int 421552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap) 422df8bae1dSRodney W. Grimes { 423b88ec951SJohn Baldwin struct uio *auio; 424b88ec951SJohn Baldwin int error; 425b88ec951SJohn Baldwin 426b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 427b88ec951SJohn Baldwin if (error) 428b88ec951SJohn Baldwin return (error); 429b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 430b88ec951SJohn Baldwin free(auio, M_IOV); 431b88ec951SJohn Baldwin return (error); 432b88ec951SJohn Baldwin } 433b88ec951SJohn Baldwin 434b88ec951SJohn Baldwin int 435b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 436b88ec951SJohn Baldwin { 437b064d43dSMatthew Dillon struct file *fp; 438bcd9e0ddSJohn Baldwin int error; 439bcd9e0ddSJohn Baldwin 440bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 441bcd9e0ddSJohn Baldwin if (error) 442af56abaaSJohn Baldwin return (error); 443bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 444bcd9e0ddSJohn Baldwin fdrop(fp, td); 445bcd9e0ddSJohn Baldwin return (error); 446bcd9e0ddSJohn Baldwin } 447bcd9e0ddSJohn Baldwin 448bcd9e0ddSJohn Baldwin /* 4490c14ff0eSRobert Watson * Gather positioned write system call. 450bcd9e0ddSJohn Baldwin */ 451bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 452bcd9e0ddSJohn Baldwin struct pwritev_args { 453bcd9e0ddSJohn Baldwin int fd; 454bcd9e0ddSJohn Baldwin struct iovec *iovp; 455bcd9e0ddSJohn Baldwin u_int iovcnt; 456bcd9e0ddSJohn Baldwin off_t offset; 457bcd9e0ddSJohn Baldwin }; 458bcd9e0ddSJohn Baldwin #endif 459bcd9e0ddSJohn Baldwin int 460bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap) 461bcd9e0ddSJohn Baldwin { 462bcd9e0ddSJohn Baldwin struct uio *auio; 463bcd9e0ddSJohn Baldwin int error; 464bcd9e0ddSJohn Baldwin 465bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 466bcd9e0ddSJohn Baldwin if (error) 467bcd9e0ddSJohn Baldwin return (error); 468bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 469bcd9e0ddSJohn Baldwin free(auio, M_IOV); 470bcd9e0ddSJohn Baldwin return (error); 471bcd9e0ddSJohn Baldwin } 472bcd9e0ddSJohn Baldwin 473bcd9e0ddSJohn Baldwin int 474bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset) 475bcd9e0ddSJohn Baldwin struct thread *td; 476bcd9e0ddSJohn Baldwin struct uio *auio; 477bcd9e0ddSJohn Baldwin int fd; 478bcd9e0ddSJohn Baldwin off_t offset; 479bcd9e0ddSJohn Baldwin { 480bcd9e0ddSJohn Baldwin struct file *fp; 481bcd9e0ddSJohn Baldwin int error; 482bcd9e0ddSJohn Baldwin 483bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 484bcd9e0ddSJohn Baldwin if (error) 485af56abaaSJohn Baldwin return (error); 486bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 487bcd9e0ddSJohn Baldwin error = ESPIPE; 488bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 489bcd9e0ddSJohn Baldwin error = EINVAL; 490bcd9e0ddSJohn Baldwin else 491bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 492bcd9e0ddSJohn Baldwin fdrop(fp, td); 493bcd9e0ddSJohn Baldwin return (error); 494bcd9e0ddSJohn Baldwin } 495bcd9e0ddSJohn Baldwin 496bcd9e0ddSJohn Baldwin /* 497bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 498bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 499bcd9e0ddSJohn Baldwin */ 500bcd9e0ddSJohn Baldwin static int 501bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 502bcd9e0ddSJohn Baldwin struct thread *td; 503bcd9e0ddSJohn Baldwin int fd; 504bcd9e0ddSJohn Baldwin struct file *fp; 505bcd9e0ddSJohn Baldwin struct uio *auio; 506bcd9e0ddSJohn Baldwin off_t offset; 507bcd9e0ddSJohn Baldwin int flags; 508bcd9e0ddSJohn Baldwin { 509bcd9e0ddSJohn Baldwin ssize_t cnt; 510552afd9cSPoul-Henning Kamp int error; 511df8bae1dSRodney W. Grimes #ifdef KTRACE 512552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 513df8bae1dSRodney W. Grimes #endif 514df8bae1dSRodney W. Grimes 515552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 516552afd9cSPoul-Henning Kamp auio->uio_td = td; 517bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 518df8bae1dSRodney W. Grimes #ifdef KTRACE 519552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 520552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 521df8bae1dSRodney W. Grimes #endif 522552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 523a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5249440653dSMatthew Dillon bwillwrite(); 525bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 526552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 527df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 528df8bae1dSRodney W. Grimes error = 0; 529bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5306f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 531b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 532b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 533b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 53419eb87d2SJohn Baldwin } 535df8bae1dSRodney W. Grimes } 536552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 537df8bae1dSRodney W. Grimes #ifdef KTRACE 538552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 539552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 540b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes #endif 543b40ce416SJulian Elischer td->td_retval[0] = cnt; 544df8bae1dSRodney W. Grimes return (error); 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes 547d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 548df8bae1dSRodney W. Grimes struct ioctl_args { 549df8bae1dSRodney W. Grimes int fd; 550069e9bc1SDoug Rabson u_long com; 551df8bae1dSRodney W. Grimes caddr_t data; 552df8bae1dSRodney W. Grimes }; 553d2d3e875SBruce Evans #endif 554df8bae1dSRodney W. Grimes /* ARGSUSED */ 55526f9a767SRodney W. Grimes int 5563e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap) 557df8bae1dSRodney W. Grimes { 5583e15c66fSPoul-Henning Kamp u_long com; 5599fddcc66SRuslan Ermilov int arg, error; 5603e15c66fSPoul-Henning Kamp u_int size; 5619fddcc66SRuslan Ermilov caddr_t data; 562df8bae1dSRodney W. Grimes 5639fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 5649fc6aa06SPoul-Henning Kamp printf( 5659fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 566431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 5679fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 5689fc6aa06SPoul-Henning Kamp } 569d9f46233SJohn Baldwin com = uap->com; 570df8bae1dSRodney W. Grimes 571df8bae1dSRodney W. Grimes /* 572df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 573df8bae1dSRodney W. Grimes * copied to/from the user's address space. 574df8bae1dSRodney W. Grimes */ 575df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 576ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 577ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 5782de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 5792de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 5802de92a38SPeter Wemm #else 5812de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 5822de92a38SPeter Wemm #endif 5839fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 584426da3bcSAlfred Perlstein return (ENOTTY); 585279d7226SMatthew Dillon 586ca51b19bSPoul-Henning Kamp if (size > 0) { 5879fddcc66SRuslan Ermilov if (!(com & IOC_VOID)) 5889fddcc66SRuslan Ermilov data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 5899fddcc66SRuslan Ermilov else { 5909fddcc66SRuslan Ermilov /* Integer argument. */ 5919fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 5929fddcc66SRuslan Ermilov data = (void *)&arg; 5939fddcc66SRuslan Ermilov size = 0; 594279d7226SMatthew Dillon } 5959fddcc66SRuslan Ermilov } else 5969fddcc66SRuslan Ermilov data = (void *)&uap->data; 597df8bae1dSRodney W. Grimes if (com & IOC_IN) { 598df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 599df8bae1dSRodney W. Grimes if (error) { 600a1b0a180SRuslan Ermilov if (size > 0) 6019fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 6023e15c66fSPoul-Henning Kamp return (error); 603df8bae1dSRodney W. Grimes } 604ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 605df8bae1dSRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * Zero the buffer so the user always 607df8bae1dSRodney W. Grimes * gets back something deterministic. 608df8bae1dSRodney W. Grimes */ 609df8bae1dSRodney W. Grimes bzero(data, size); 610279d7226SMatthew Dillon } 611df8bae1dSRodney W. Grimes 612d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 613d9f46233SJohn Baldwin 614d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 615d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 616d9f46233SJohn Baldwin 6179fddcc66SRuslan Ermilov if (size > 0) 6189fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 619d9f46233SJohn Baldwin return (error); 620d9f46233SJohn Baldwin } 621d9f46233SJohn Baldwin 622d9f46233SJohn Baldwin int 623d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 624d9f46233SJohn Baldwin { 625d9f46233SJohn Baldwin struct file *fp; 626d9f46233SJohn Baldwin struct filedesc *fdp; 627d9f46233SJohn Baldwin int error; 628d9f46233SJohn Baldwin int tmp; 629d9f46233SJohn Baldwin 630d9f46233SJohn Baldwin if ((error = fget(td, fd, &fp)) != 0) 631d9f46233SJohn Baldwin return (error); 632d9f46233SJohn Baldwin if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 633d9f46233SJohn Baldwin fdrop(fp, td); 634d9f46233SJohn Baldwin return (EBADF); 635d9f46233SJohn Baldwin } 636d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 637d9f46233SJohn Baldwin switch (com) { 638d9f46233SJohn Baldwin case FIONCLEX: 6395e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 640d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE; 6415e3f7694SRobert Watson FILEDESC_XUNLOCK(fdp); 642d9f46233SJohn Baldwin goto out; 643d9f46233SJohn Baldwin case FIOCLEX: 6445e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 645d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 6465e3f7694SRobert Watson FILEDESC_XUNLOCK(fdp); 647d9f46233SJohn Baldwin goto out; 648d9f46233SJohn Baldwin case FIONBIO: 649bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 650397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 651df8bae1dSRodney W. Grimes else 652397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 6538ccf264fSPoul-Henning Kamp data = (void *)&tmp; 654d9f46233SJohn Baldwin break; 655d9f46233SJohn Baldwin case FIOASYNC: 656bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 657397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 658df8bae1dSRodney W. Grimes else 659397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 6608ccf264fSPoul-Henning Kamp data = (void *)&tmp; 661d9f46233SJohn Baldwin break; 662df8bae1dSRodney W. Grimes } 6638ccf264fSPoul-Henning Kamp 6648ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 665d9f46233SJohn Baldwin out: 666b40ce416SJulian Elischer fdrop(fp, td); 667df8bae1dSRodney W. Grimes return (error); 668df8bae1dSRodney W. Grimes } 669df8bae1dSRodney W. Grimes 670d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 671df8bae1dSRodney W. Grimes struct select_args { 672b08f7993SSujal Patel int nd; 673df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 674df8bae1dSRodney W. Grimes struct timeval *tv; 675df8bae1dSRodney W. Grimes }; 676d2d3e875SBruce Evans #endif 67726f9a767SRodney W. Grimes int 678b40ce416SJulian Elischer select(td, uap) 679b40ce416SJulian Elischer register struct thread *td; 680df8bae1dSRodney W. Grimes register struct select_args *uap; 681df8bae1dSRodney W. Grimes { 6828f19eb88SIan Dowse struct timeval tv, *tvp; 6838f19eb88SIan Dowse int error; 6848f19eb88SIan Dowse 6858f19eb88SIan Dowse if (uap->tv != NULL) { 6868f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 6878f19eb88SIan Dowse if (error) 6888f19eb88SIan Dowse return (error); 6898f19eb88SIan Dowse tvp = &tv; 6908f19eb88SIan Dowse } else 6918f19eb88SIan Dowse tvp = NULL; 6928f19eb88SIan Dowse 6938f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 6948f19eb88SIan Dowse } 6958f19eb88SIan Dowse 6968f19eb88SIan Dowse int 6978f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 6988f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 6998f19eb88SIan Dowse { 700426da3bcSAlfred Perlstein struct filedesc *fdp; 701d5e4d7e1SBruce Evans /* 702d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 703d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 704d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 705d5e4d7e1SBruce Evans * of 256. 706d5e4d7e1SBruce Evans */ 707d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 708eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 70900af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7109ae6d334SKelly Yancey int error, timo; 711ace8398dSJeff Roberson u_int nbufbytes, ncpbytes, nfdbits; 712df8bae1dSRodney W. Grimes 7138f19eb88SIan Dowse if (nd < 0) 714acbfbfeaSSujal Patel return (EINVAL); 715426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 716db446e30SPoul-Henning Kamp 7175e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 7188f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 7198f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 7205e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 721b08f7993SSujal Patel 722d5e4d7e1SBruce Evans /* 723d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 724d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 725d5e4d7e1SBruce Evans */ 7268f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 727d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 728d5e4d7e1SBruce Evans nbufbytes = 0; 7298f19eb88SIan Dowse if (fd_in != NULL) 730d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7318f19eb88SIan Dowse if (fd_ou != NULL) 732d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7338f19eb88SIan Dowse if (fd_ex != NULL) 734d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 735d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 736d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 737d5e4d7e1SBruce Evans else 738a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 739b08f7993SSujal Patel 740b08f7993SSujal Patel /* 741d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 742d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 743d5e4d7e1SBruce Evans * together. 744b08f7993SSujal Patel */ 745d5e4d7e1SBruce Evans sbp = selbits; 746df8bae1dSRodney W. Grimes #define getbits(name, x) \ 747d5e4d7e1SBruce Evans do { \ 7488f19eb88SIan Dowse if (name == NULL) \ 749d5e4d7e1SBruce Evans ibits[x] = NULL; \ 750d5e4d7e1SBruce Evans else { \ 751d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 752d5e4d7e1SBruce Evans obits[x] = sbp; \ 753d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 7548f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 755265fc98fSSeigo Tanimura if (error != 0) \ 756ace8398dSJeff Roberson goto done; \ 757e04ac2feSJohn Baldwin } \ 758d5e4d7e1SBruce Evans } while (0) 7598f19eb88SIan Dowse getbits(fd_in, 0); 7608f19eb88SIan Dowse getbits(fd_ou, 1); 7618f19eb88SIan Dowse getbits(fd_ex, 2); 762df8bae1dSRodney W. Grimes #undef getbits 763d5e4d7e1SBruce Evans if (nbufbytes != 0) 764d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 765df8bae1dSRodney W. Grimes 7668f19eb88SIan Dowse if (tvp != NULL) { 7678f19eb88SIan Dowse atv = *tvp; 768df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 769df8bae1dSRodney W. Grimes error = EINVAL; 770ace8398dSJeff Roberson goto done; 771df8bae1dSRodney W. Grimes } 772c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 77300af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 7749c386f6bSJohn Baldwin } else { 77500af9731SPoul-Henning Kamp atv.tv_sec = 0; 7769c386f6bSJohn Baldwin atv.tv_usec = 0; 7779c386f6bSJohn Baldwin } 77800af9731SPoul-Henning Kamp timo = 0; 779ace8398dSJeff Roberson seltdinit(td); 780ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 781ace8398dSJeff Roberson for (;;) { 7828f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 783ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 784ace8398dSJeff Roberson break; 7854da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 786c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 78785f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 788ace8398dSJeff Roberson break; 78900af9731SPoul-Henning Kamp ttv = atv; 79000af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 79100af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 79200af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 793df8bae1dSRodney W. Grimes } 794ace8398dSJeff Roberson error = seltdwait(td, timo); 795ace8398dSJeff Roberson if (error) 796ace8398dSJeff Roberson break; 797ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 798ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 799ace8398dSJeff Roberson break; 80085f190e4SAlfred Perlstein } 801ace8398dSJeff Roberson seltdclear(td); 802265fc98fSSeigo Tanimura 803df8bae1dSRodney W. Grimes done: 804df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 805df8bae1dSRodney W. Grimes if (error == ERESTART) 806df8bae1dSRodney W. Grimes error = EINTR; 807df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 808df8bae1dSRodney W. Grimes error = 0; 809df8bae1dSRodney W. Grimes #define putbits(name, x) \ 8108f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 811df8bae1dSRodney W. Grimes error = error2; 812df8bae1dSRodney W. Grimes if (error == 0) { 813df8bae1dSRodney W. Grimes int error2; 814df8bae1dSRodney W. Grimes 8158f19eb88SIan Dowse putbits(fd_in, 0); 8168f19eb88SIan Dowse putbits(fd_ou, 1); 8178f19eb88SIan Dowse putbits(fd_ex, 2); 818df8bae1dSRodney W. Grimes #undef putbits 819df8bae1dSRodney W. Grimes } 820d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 821d5e4d7e1SBruce Evans free(selbits, M_SELECT); 822ad2edad9SMatthew Dillon 823df8bae1dSRodney W. Grimes return (error); 824df8bae1dSRodney W. Grimes } 825df8bae1dSRodney W. Grimes 826ace8398dSJeff Roberson /* 827ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 828ace8398dSJeff Roberson * completion. 829ace8398dSJeff Roberson */ 830ace8398dSJeff Roberson static int 831ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 832ace8398dSJeff Roberson { 833ace8398dSJeff Roberson struct seltd *stp; 834ace8398dSJeff Roberson struct selfd *sfp; 835ace8398dSJeff Roberson struct selfd *sfn; 836ace8398dSJeff Roberson struct selinfo *si; 837ace8398dSJeff Roberson struct file *fp; 838ace8398dSJeff Roberson int msk, fd; 839ace8398dSJeff Roberson int n = 0; 840ace8398dSJeff Roberson /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 841ace8398dSJeff Roberson static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 842ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 843ace8398dSJeff Roberson 844ace8398dSJeff Roberson stp = td->td_sel; 845ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 846ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 847ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 848ace8398dSJeff Roberson si = sfp->sf_si; 849ace8398dSJeff Roberson selfdfree(stp, sfp); 850ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 851ace8398dSJeff Roberson if (si != NULL) 852ace8398dSJeff Roberson continue; 853ace8398dSJeff Roberson if ((fp = fget_locked(fdp, fd)) == NULL) { 854ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 855ace8398dSJeff Roberson return (EBADF); 856ace8398dSJeff Roberson } 857ace8398dSJeff Roberson for (msk = 0; msk < 3; msk++) { 858ace8398dSJeff Roberson if (ibits[msk] == NULL) 859ace8398dSJeff Roberson continue; 860ace8398dSJeff Roberson if ((ibits[msk][fd/NFDBITS] & 861ace8398dSJeff Roberson ((fd_mask) 1 << (fd % NFDBITS))) == 0) 862ace8398dSJeff Roberson continue; 863ace8398dSJeff Roberson if (fo_poll(fp, flag[msk], td->td_ucred, td)) { 864ace8398dSJeff Roberson obits[msk][(fd)/NFDBITS] |= 865ace8398dSJeff Roberson ((fd_mask)1 << ((fd) % NFDBITS)); 866ace8398dSJeff Roberson n++; 867ace8398dSJeff Roberson } 868ace8398dSJeff Roberson } 869ace8398dSJeff Roberson } 870ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 871ace8398dSJeff Roberson stp->st_flags = 0; 872ace8398dSJeff Roberson td->td_retval[0] = n; 873ace8398dSJeff Roberson return (0); 874ace8398dSJeff Roberson } 875ace8398dSJeff Roberson 876ace8398dSJeff Roberson /* 877ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 878ace8398dSJeff Roberson * each selinfo. 879ace8398dSJeff Roberson */ 880265fc98fSSeigo Tanimura static int 881b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 882b40ce416SJulian Elischer struct thread *td; 883b08f7993SSujal Patel fd_mask **ibits, **obits; 884cb226aaaSPoul-Henning Kamp int nfd; 885df8bae1dSRodney W. Grimes { 886f082218cSPeter Wemm int msk, i, fd; 887f082218cSPeter Wemm fd_mask bits; 888df8bae1dSRodney W. Grimes struct file *fp; 889df8bae1dSRodney W. Grimes int n = 0; 8902087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 89142d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 892eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 893df8bae1dSRodney W. Grimes 8945e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 895df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 896d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 897d5e4d7e1SBruce Evans continue; 898df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 899b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 900f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 901f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 902f082218cSPeter Wemm if (!(bits & 1)) 903f082218cSPeter Wemm continue; 904eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 9055e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 906df8bae1dSRodney W. Grimes return (EBADF); 907eb209311SAlfred Perlstein } 908ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 909ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 910ea6027a8SRobert Watson td)) { 911b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 912f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 913df8bae1dSRodney W. Grimes n++; 914df8bae1dSRodney W. Grimes } 915df8bae1dSRodney W. Grimes } 916df8bae1dSRodney W. Grimes } 917df8bae1dSRodney W. Grimes } 9185e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 919b40ce416SJulian Elischer td->td_retval[0] = n; 920df8bae1dSRodney W. Grimes return (0); 921df8bae1dSRodney W. Grimes } 922df8bae1dSRodney W. Grimes 92342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 92442d11757SPeter Wemm struct poll_args { 92542d11757SPeter Wemm struct pollfd *fds; 92642d11757SPeter Wemm u_int nfds; 92742d11757SPeter Wemm int timeout; 92842d11757SPeter Wemm }; 92942d11757SPeter Wemm #endif 93042d11757SPeter Wemm int 931b40ce416SJulian Elischer poll(td, uap) 932b40ce416SJulian Elischer struct thread *td; 933ea0237edSJonathan Lemon struct poll_args *uap; 93442d11757SPeter Wemm { 9352580f4e5SAndre Oppermann struct pollfd *bits; 9362580f4e5SAndre Oppermann struct pollfd smallbits[32]; 93700af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 9389ae6d334SKelly Yancey int error = 0, timo; 939ace8398dSJeff Roberson u_int nfds; 94042d11757SPeter Wemm size_t ni; 94142d11757SPeter Wemm 942d1e405c5SAlfred Perlstein nfds = uap->nfds; 943ad2edad9SMatthew Dillon 9445d8dd01dSRobert Watson /* 9452bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 9462bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 9472bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 9482bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 9492bd5ac33SPeter Wemm * safe, but not overly restrictive. 95089b71647SPeter Wemm */ 95191d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 95291d5354aSJohn Baldwin if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) && 953b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 95491d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 955ace8398dSJeff Roberson return (EINVAL); 956ad2edad9SMatthew Dillon } 95791d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 95889b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 95942d11757SPeter Wemm if (ni > sizeof(smallbits)) 960a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 96142d11757SPeter Wemm else 96242d11757SPeter Wemm bits = smallbits; 963d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 96442d11757SPeter Wemm if (error) 965ace8398dSJeff Roberson goto done; 966d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 967d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 968d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 96942d11757SPeter Wemm if (itimerfix(&atv)) { 97042d11757SPeter Wemm error = EINVAL; 971ace8398dSJeff Roberson goto done; 97242d11757SPeter Wemm } 973c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 97400af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 9759c386f6bSJohn Baldwin } else { 97600af9731SPoul-Henning Kamp atv.tv_sec = 0; 9779c386f6bSJohn Baldwin atv.tv_usec = 0; 9789c386f6bSJohn Baldwin } 97900af9731SPoul-Henning Kamp timo = 0; 980ace8398dSJeff Roberson seltdinit(td); 981ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 982ace8398dSJeff Roberson for (;;) { 9832580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 984ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 985ace8398dSJeff Roberson break; 9864da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 987c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 98885f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 989ace8398dSJeff Roberson break; 99000af9731SPoul-Henning Kamp ttv = atv; 99100af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 99200af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 99300af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 99442d11757SPeter Wemm } 995ace8398dSJeff Roberson error = seltdwait(td, timo); 996ace8398dSJeff Roberson if (error) 997ace8398dSJeff Roberson break; 998ace8398dSJeff Roberson error = pollrescan(td); 999ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1000ace8398dSJeff Roberson break; 100185f190e4SAlfred Perlstein } 1002ace8398dSJeff Roberson seltdclear(td); 1003265fc98fSSeigo Tanimura 100442d11757SPeter Wemm done: 100542d11757SPeter Wemm /* poll is not restarted after signals... */ 100642d11757SPeter Wemm if (error == ERESTART) 100742d11757SPeter Wemm error = EINTR; 100842d11757SPeter Wemm if (error == EWOULDBLOCK) 100942d11757SPeter Wemm error = 0; 101042d11757SPeter Wemm if (error == 0) { 1011d1e405c5SAlfred Perlstein error = copyout(bits, uap->fds, ni); 101242d11757SPeter Wemm if (error) 101342d11757SPeter Wemm goto out; 101442d11757SPeter Wemm } 101542d11757SPeter Wemm out: 101642d11757SPeter Wemm if (ni > sizeof(smallbits)) 101742d11757SPeter Wemm free(bits, M_TEMP); 101842d11757SPeter Wemm return (error); 101942d11757SPeter Wemm } 102042d11757SPeter Wemm 102142d11757SPeter Wemm static int 1022ace8398dSJeff Roberson pollrescan(struct thread *td) 1023ace8398dSJeff Roberson { 1024ace8398dSJeff Roberson struct seltd *stp; 1025ace8398dSJeff Roberson struct selfd *sfp; 1026ace8398dSJeff Roberson struct selfd *sfn; 1027ace8398dSJeff Roberson struct selinfo *si; 1028ace8398dSJeff Roberson struct filedesc *fdp; 1029ace8398dSJeff Roberson struct file *fp; 1030ace8398dSJeff Roberson struct pollfd *fd; 1031ace8398dSJeff Roberson int n; 1032ace8398dSJeff Roberson 1033ace8398dSJeff Roberson n = 0; 1034ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1035ace8398dSJeff Roberson stp = td->td_sel; 1036ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1037ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1038ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1039ace8398dSJeff Roberson si = sfp->sf_si; 1040ace8398dSJeff Roberson selfdfree(stp, sfp); 1041ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1042ace8398dSJeff Roberson if (si != NULL) 1043ace8398dSJeff Roberson continue; 1044ace8398dSJeff Roberson fp = fdp->fd_ofiles[fd->fd]; 1045ace8398dSJeff Roberson if (fp == NULL) { 1046ace8398dSJeff Roberson fd->revents = POLLNVAL; 1047ace8398dSJeff Roberson n++; 1048ace8398dSJeff Roberson continue; 1049ace8398dSJeff Roberson } 1050ace8398dSJeff Roberson /* 1051ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1052ace8398dSJeff Roberson * POLLERR if appropriate. 1053ace8398dSJeff Roberson */ 1054ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1055ace8398dSJeff Roberson if (fd->revents != 0) 1056ace8398dSJeff Roberson n++; 1057ace8398dSJeff Roberson } 1058ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1059ace8398dSJeff Roberson stp->st_flags = 0; 1060ace8398dSJeff Roberson td->td_retval[0] = n; 1061ace8398dSJeff Roberson return (0); 1062ace8398dSJeff Roberson } 1063ace8398dSJeff Roberson 1064ace8398dSJeff Roberson 1065ace8398dSJeff Roberson static int 1066b40ce416SJulian Elischer pollscan(td, fds, nfd) 1067b40ce416SJulian Elischer struct thread *td; 106842d11757SPeter Wemm struct pollfd *fds; 1069ea0237edSJonathan Lemon u_int nfd; 107042d11757SPeter Wemm { 1071ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 107242d11757SPeter Wemm int i; 107342d11757SPeter Wemm struct file *fp; 107442d11757SPeter Wemm int n = 0; 107542d11757SPeter Wemm 10765e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1077eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1078337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 107942d11757SPeter Wemm fds->revents = POLLNVAL; 108042d11757SPeter Wemm n++; 1081337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1082337c9691SJordan K. Hubbard fds->revents = 0; 108342d11757SPeter Wemm } else { 108442d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1085279d7226SMatthew Dillon if (fp == NULL) { 108642d11757SPeter Wemm fds->revents = POLLNVAL; 108742d11757SPeter Wemm n++; 108842d11757SPeter Wemm } else { 10892087c896SBruce Evans /* 10902087c896SBruce Evans * Note: backend also returns POLLHUP and 10912087c896SBruce Evans * POLLERR if appropriate. 10922087c896SBruce Evans */ 1093ace8398dSJeff Roberson selfdalloc(td, fds); 109413ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1095ea6027a8SRobert Watson td->td_ucred, td); 109642d11757SPeter Wemm if (fds->revents != 0) 109742d11757SPeter Wemm n++; 109842d11757SPeter Wemm } 109942d11757SPeter Wemm } 110042d11757SPeter Wemm } 11015e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1102b40ce416SJulian Elischer td->td_retval[0] = n; 110342d11757SPeter Wemm return (0); 110442d11757SPeter Wemm } 110542d11757SPeter Wemm 110642d11757SPeter Wemm /* 110742d11757SPeter Wemm * OpenBSD poll system call. 11080c14ff0eSRobert Watson * 110942d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 111042d11757SPeter Wemm */ 111142d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 111242d11757SPeter Wemm struct openbsd_poll_args { 111342d11757SPeter Wemm struct pollfd *fds; 111442d11757SPeter Wemm u_int nfds; 111542d11757SPeter Wemm int timeout; 111642d11757SPeter Wemm }; 111742d11757SPeter Wemm #endif 111842d11757SPeter Wemm int 1119b40ce416SJulian Elischer openbsd_poll(td, uap) 1120b40ce416SJulian Elischer register struct thread *td; 112142d11757SPeter Wemm register struct openbsd_poll_args *uap; 112242d11757SPeter Wemm { 1123b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 112442d11757SPeter Wemm } 112542d11757SPeter Wemm 112685f190e4SAlfred Perlstein /* 1127ace8398dSJeff Roberson * XXX This was created specifically to support netncp and netsmb. This 1128ace8398dSJeff Roberson * allows the caller to specify a socket to wait for events on. It returns 1129ace8398dSJeff Roberson * 0 if any events matched and an error otherwise. There is no way to 1130ace8398dSJeff Roberson * determine which events fired. 113185f190e4SAlfred Perlstein */ 1132ace8398dSJeff Roberson int 1133ace8398dSJeff Roberson selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 113485f190e4SAlfred Perlstein { 1135ace8398dSJeff Roberson struct timeval atv, rtv, ttv; 1136ace8398dSJeff Roberson int error, timo; 113785f190e4SAlfred Perlstein 1138ace8398dSJeff Roberson if (tvp != NULL) { 1139ace8398dSJeff Roberson atv = *tvp; 1140ace8398dSJeff Roberson if (itimerfix(&atv)) 1141ace8398dSJeff Roberson return (EINVAL); 1142ace8398dSJeff Roberson getmicrouptime(&rtv); 1143ace8398dSJeff Roberson timevaladd(&atv, &rtv); 1144ace8398dSJeff Roberson } else { 1145ace8398dSJeff Roberson atv.tv_sec = 0; 1146ace8398dSJeff Roberson atv.tv_usec = 0; 1147ace8398dSJeff Roberson } 1148ace8398dSJeff Roberson 1149ace8398dSJeff Roberson timo = 0; 1150ace8398dSJeff Roberson seltdinit(td); 1151ace8398dSJeff Roberson /* 1152ace8398dSJeff Roberson * Iterate until the timeout expires or the socket becomes ready. 1153ace8398dSJeff Roberson */ 1154ace8398dSJeff Roberson for (;;) { 1155ace8398dSJeff Roberson selfdalloc(td, NULL); 1156ace8398dSJeff Roberson error = sopoll(so, events, NULL, td); 1157ace8398dSJeff Roberson /* error here is actually the ready events. */ 1158ace8398dSJeff Roberson if (error) 1159ace8398dSJeff Roberson return (0); 1160ace8398dSJeff Roberson if (atv.tv_sec || atv.tv_usec) { 1161ace8398dSJeff Roberson getmicrouptime(&rtv); 1162ace8398dSJeff Roberson if (timevalcmp(&rtv, &atv, >=)) { 1163ace8398dSJeff Roberson seltdclear(td); 1164ace8398dSJeff Roberson return (EWOULDBLOCK); 1165ace8398dSJeff Roberson } 1166ace8398dSJeff Roberson ttv = atv; 1167ace8398dSJeff Roberson timevalsub(&ttv, &rtv); 1168ace8398dSJeff Roberson timo = ttv.tv_sec > 24 * 60 * 60 ? 1169ace8398dSJeff Roberson 24 * 60 * 60 * hz : tvtohz(&ttv); 1170ace8398dSJeff Roberson } 1171ace8398dSJeff Roberson error = seltdwait(td, timo); 1172ace8398dSJeff Roberson seltdclear(td); 1173ace8398dSJeff Roberson if (error) 1174ace8398dSJeff Roberson break; 1175ace8398dSJeff Roberson } 1176ace8398dSJeff Roberson /* XXX Duplicates ncp/smb behavior. */ 1177ace8398dSJeff Roberson if (error == ERESTART) 1178ace8398dSJeff Roberson error = 0; 1179ace8398dSJeff Roberson return (error); 1180ace8398dSJeff Roberson } 1181ace8398dSJeff Roberson 1182ace8398dSJeff Roberson /* 1183ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1184ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1185ace8398dSJeff Roberson */ 1186ace8398dSJeff Roberson static void 1187ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1188ace8398dSJeff Roberson { 1189ace8398dSJeff Roberson struct seltd *stp; 1190ace8398dSJeff Roberson 1191ace8398dSJeff Roberson stp = td->td_sel; 1192ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1193ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1194ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1195ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1196ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1197ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1198ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1199ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1200ace8398dSJeff Roberson } 1201ace8398dSJeff Roberson 1202ace8398dSJeff Roberson static void 1203ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1204ace8398dSJeff Roberson { 1205ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 1206ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1207ace8398dSJeff Roberson if (sfp->sf_si) 1208ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1209ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 1210ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 121185f190e4SAlfred Perlstein } 121285f190e4SAlfred Perlstein 1213df8bae1dSRodney W. Grimes /* 1214df8bae1dSRodney W. Grimes * Record a select request. 1215df8bae1dSRodney W. Grimes */ 1216df8bae1dSRodney W. Grimes void 1217df8bae1dSRodney W. Grimes selrecord(selector, sip) 1218b40ce416SJulian Elischer struct thread *selector; 1219df8bae1dSRodney W. Grimes struct selinfo *sip; 1220df8bae1dSRodney W. Grimes { 1221ace8398dSJeff Roberson struct selfd *sfp; 1222ace8398dSJeff Roberson struct seltd *stp; 1223ace8398dSJeff Roberson struct mtx *mtxp; 1224df8bae1dSRodney W. Grimes 1225ace8398dSJeff Roberson stp = selector->td_sel; 122685f190e4SAlfred Perlstein /* 1227ace8398dSJeff Roberson * Don't record when doing a rescan. 122885f190e4SAlfred Perlstein */ 1229ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1230ace8398dSJeff Roberson return; 1231ace8398dSJeff Roberson /* 1232ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1233ace8398dSJeff Roberson */ 1234ace8398dSJeff Roberson sfp = NULL; 1235ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1236ace8398dSJeff Roberson stp->st_free1 = NULL; 1237ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1238ace8398dSJeff Roberson stp->st_free2 = NULL; 1239ace8398dSJeff Roberson else 1240ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 1241ace8398dSJeff Roberson mtxp = mtx_pool_find(mtxpool_sleep, sip); 1242ace8398dSJeff Roberson /* 1243ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1244ace8398dSJeff Roberson */ 1245ace8398dSJeff Roberson sfp->sf_si = sip; 1246ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1247ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1248ace8398dSJeff Roberson /* 1249ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1250ace8398dSJeff Roberson */ 1251ace8398dSJeff Roberson mtx_lock(mtxp); 1252ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1253ace8398dSJeff Roberson sip->si_mtx = mtxp; 1254ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 125585f190e4SAlfred Perlstein } 1256ace8398dSJeff Roberson /* 1257ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1258ace8398dSJeff Roberson */ 1259ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1260ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes 1263512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1264df8bae1dSRodney W. Grimes void 1265df8bae1dSRodney W. Grimes selwakeup(sip) 126685f190e4SAlfred Perlstein struct selinfo *sip; 1267df8bae1dSRodney W. Grimes { 1268512824f8SSeigo Tanimura doselwakeup(sip, -1); 1269512824f8SSeigo Tanimura } 1270512824f8SSeigo Tanimura 1271512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1272512824f8SSeigo Tanimura void 1273512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1274512824f8SSeigo Tanimura struct selinfo *sip; 1275512824f8SSeigo Tanimura int pri; 1276512824f8SSeigo Tanimura { 1277512824f8SSeigo Tanimura doselwakeup(sip, pri); 1278512824f8SSeigo Tanimura } 1279512824f8SSeigo Tanimura 1280512824f8SSeigo Tanimura /* 1281512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1282512824f8SSeigo Tanimura */ 1283512824f8SSeigo Tanimura static void 1284512824f8SSeigo Tanimura doselwakeup(sip, pri) 1285512824f8SSeigo Tanimura struct selinfo *sip; 1286512824f8SSeigo Tanimura int pri; 1287512824f8SSeigo Tanimura { 1288ace8398dSJeff Roberson struct selfd *sfp; 1289ace8398dSJeff Roberson struct selfd *sfn; 1290ace8398dSJeff Roberson struct seltd *stp; 1291df8bae1dSRodney W. Grimes 1292ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1293ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1294b40ce416SJulian Elischer return; 1295ace8398dSJeff Roberson /* 1296ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1297ace8398dSJeff Roberson */ 1298ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1299ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1300ace8398dSJeff Roberson /* 1301ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1302ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1303ace8398dSJeff Roberson */ 1304ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1305ace8398dSJeff Roberson sfp->sf_si = NULL; 1306ace8398dSJeff Roberson stp = sfp->sf_td; 1307ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1308ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1309ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1310ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1311b40ce416SJulian Elischer } 1312ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1313ace8398dSJeff Roberson } 1314ace8398dSJeff Roberson 1315ace8398dSJeff Roberson static void 1316ace8398dSJeff Roberson seltdinit(struct thread *td) 1317ace8398dSJeff Roberson { 1318ace8398dSJeff Roberson struct seltd *stp; 1319ace8398dSJeff Roberson 1320ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1321ace8398dSJeff Roberson goto out; 1322ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1323ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1324ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1325ace8398dSJeff Roberson out: 1326ace8398dSJeff Roberson stp->st_flags = 0; 1327ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1328ace8398dSJeff Roberson } 1329ace8398dSJeff Roberson 1330ace8398dSJeff Roberson static int 1331ace8398dSJeff Roberson seltdwait(struct thread *td, int timo) 1332ace8398dSJeff Roberson { 1333ace8398dSJeff Roberson struct seltd *stp; 1334ace8398dSJeff Roberson int error; 1335ace8398dSJeff Roberson 1336ace8398dSJeff Roberson stp = td->td_sel; 1337ace8398dSJeff Roberson /* 1338ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1339ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1340ace8398dSJeff Roberson */ 1341ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1342ace8398dSJeff Roberson /* 1343ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1344ace8398dSJeff Roberson */ 1345ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1346ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1347ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1348ace8398dSJeff Roberson return (0); 1349ace8398dSJeff Roberson } 1350ace8398dSJeff Roberson if (timo > 0) 1351ace8398dSJeff Roberson error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo); 1352ace8398dSJeff Roberson else 1353ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1354ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1355ace8398dSJeff Roberson 1356ace8398dSJeff Roberson return (error); 1357ace8398dSJeff Roberson } 1358ace8398dSJeff Roberson 1359ace8398dSJeff Roberson void 1360ace8398dSJeff Roberson seltdfini(struct thread *td) 1361ace8398dSJeff Roberson { 1362ace8398dSJeff Roberson struct seltd *stp; 1363ace8398dSJeff Roberson 1364ace8398dSJeff Roberson stp = td->td_sel; 1365ace8398dSJeff Roberson if (stp == NULL) 1366ace8398dSJeff Roberson return; 1367ace8398dSJeff Roberson if (stp->st_free1) 1368ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1369ace8398dSJeff Roberson if (stp->st_free2) 1370ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1371ace8398dSJeff Roberson td->td_sel = NULL; 1372ace8398dSJeff Roberson free(stp, M_SELECT); 1373ace8398dSJeff Roberson } 1374ace8398dSJeff Roberson 1375ace8398dSJeff Roberson /* 1376ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1377ace8398dSJeff Roberson * polling. 1378ace8398dSJeff Roberson */ 1379ace8398dSJeff Roberson static void 1380ace8398dSJeff Roberson seltdclear(struct thread *td) 1381ace8398dSJeff Roberson { 1382ace8398dSJeff Roberson struct seltd *stp; 1383ace8398dSJeff Roberson struct selfd *sfp; 1384ace8398dSJeff Roberson struct selfd *sfn; 1385ace8398dSJeff Roberson 1386ace8398dSJeff Roberson stp = td->td_sel; 1387ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1388ace8398dSJeff Roberson selfdfree(stp, sfp); 1389ace8398dSJeff Roberson stp->st_flags = 0; 1390df8bae1dSRodney W. Grimes } 1391265fc98fSSeigo Tanimura 13924d77a549SAlfred Perlstein static void selectinit(void *); 1393ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1394265fc98fSSeigo Tanimura static void 1395ace8398dSJeff Roberson selectinit(void *dummy __unused) 1396265fc98fSSeigo Tanimura { 1397ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1398ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 1399265fc98fSSeigo Tanimura } 1400