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 79bbbb04ceSAlfred Perlstein static int pollscan(struct thread *, struct pollfd *, u_int); 80ace8398dSJeff Roberson static int pollrescan(struct thread *); 81bbbb04ceSAlfred Perlstein static int selscan(struct thread *, fd_mask **, fd_mask **, int); 82ace8398dSJeff Roberson static int selrescan(struct thread *, fd_mask **, fd_mask **); 83ace8398dSJeff Roberson static void selfdalloc(struct thread *, void *); 84ace8398dSJeff Roberson static void selfdfree(struct seltd *, struct selfd *); 85bcd9e0ddSJohn Baldwin static int dofileread(struct thread *, int, struct file *, struct uio *, 86bcd9e0ddSJohn Baldwin off_t, int); 87bcd9e0ddSJohn Baldwin static int dofilewrite(struct thread *, int, struct file *, struct uio *, 88bcd9e0ddSJohn Baldwin off_t, int); 89512824f8SSeigo Tanimura static void doselwakeup(struct selinfo *, int); 90ace8398dSJeff Roberson static void seltdinit(struct thread *); 91ace8398dSJeff Roberson static int seltdwait(struct thread *, int); 92ace8398dSJeff Roberson static void seltdclear(struct thread *); 93ace8398dSJeff Roberson 94ace8398dSJeff Roberson /* 95ace8398dSJeff Roberson * One seltd per-thread allocated on demand as needed. 96ace8398dSJeff Roberson * 97ace8398dSJeff Roberson * t - protected by st_mtx 98ace8398dSJeff Roberson * k - Only accessed by curthread or read-only 99ace8398dSJeff Roberson */ 100ace8398dSJeff Roberson struct seltd { 101ace8398dSJeff Roberson STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */ 102ace8398dSJeff Roberson struct selfd *st_free1; /* (k) free fd for read set. */ 103ace8398dSJeff Roberson struct selfd *st_free2; /* (k) free fd for write set. */ 104ace8398dSJeff Roberson struct mtx st_mtx; /* Protects struct seltd */ 105ace8398dSJeff Roberson struct cv st_wait; /* (t) Wait channel. */ 106ace8398dSJeff Roberson int st_flags; /* (t) SELTD_ flags. */ 107ace8398dSJeff Roberson }; 108ace8398dSJeff Roberson 109ace8398dSJeff Roberson #define SELTD_PENDING 0x0001 /* We have pending events. */ 110ace8398dSJeff Roberson #define SELTD_RESCAN 0x0002 /* Doing a rescan. */ 111ace8398dSJeff Roberson 112ace8398dSJeff Roberson /* 113ace8398dSJeff Roberson * One selfd allocated per-thread per-file-descriptor. 114ace8398dSJeff Roberson * f - protected by sf_mtx 115ace8398dSJeff Roberson */ 116ace8398dSJeff Roberson struct selfd { 117ace8398dSJeff Roberson STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */ 118ace8398dSJeff Roberson TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */ 119ace8398dSJeff Roberson struct selinfo *sf_si; /* (f) selinfo when linked. */ 120ace8398dSJeff Roberson struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ 121ace8398dSJeff Roberson struct seltd *sf_td; /* (k) owning seltd. */ 122ace8398dSJeff Roberson void *sf_cookie; /* (k) fd or pollfd. */ 123ace8398dSJeff Roberson }; 124ace8398dSJeff Roberson 125ace8398dSJeff Roberson static uma_zone_t selfd_zone; 1268fe387abSDmitrij Tejblum 127d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 128df8bae1dSRodney W. Grimes struct read_args { 129df8bae1dSRodney W. Grimes int fd; 130134e06feSBruce Evans void *buf; 131134e06feSBruce Evans size_t nbyte; 132df8bae1dSRodney W. Grimes }; 133d2d3e875SBruce Evans #endif 13426f9a767SRodney W. Grimes int 135b40ce416SJulian Elischer read(td, uap) 136b40ce416SJulian Elischer struct thread *td; 137b064d43dSMatthew Dillon struct read_args *uap; 138df8bae1dSRodney W. Grimes { 139bcd9e0ddSJohn Baldwin struct uio auio; 140bcd9e0ddSJohn Baldwin struct iovec aiov; 141279d7226SMatthew Dillon int error; 142df8bae1dSRodney W. Grimes 143bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 144bcd9e0ddSJohn Baldwin return (EINVAL); 145bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 146bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 147bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 148bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 149bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 150bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 151bcd9e0ddSJohn Baldwin error = kern_readv(td, uap->fd, &auio); 152279d7226SMatthew Dillon return(error); 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes 155df8bae1dSRodney W. Grimes /* 156bcd9e0ddSJohn Baldwin * Positioned read system call 1574160ccd9SAlan Cox */ 1584160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 1594160ccd9SAlan Cox struct pread_args { 1604160ccd9SAlan Cox int fd; 1614160ccd9SAlan Cox void *buf; 1624160ccd9SAlan Cox size_t nbyte; 1638fe387abSDmitrij Tejblum int pad; 1644160ccd9SAlan Cox off_t offset; 1654160ccd9SAlan Cox }; 1664160ccd9SAlan Cox #endif 1674160ccd9SAlan Cox int 168b40ce416SJulian Elischer pread(td, uap) 169b40ce416SJulian Elischer struct thread *td; 170b064d43dSMatthew Dillon struct pread_args *uap; 1714160ccd9SAlan Cox { 1724160ccd9SAlan Cox struct uio auio; 1734160ccd9SAlan Cox struct iovec aiov; 174bcd9e0ddSJohn Baldwin int error; 1754160ccd9SAlan Cox 176bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 177bcd9e0ddSJohn Baldwin return (EINVAL); 178bcd9e0ddSJohn Baldwin aiov.iov_base = uap->buf; 179bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 1804160ccd9SAlan Cox auio.uio_iov = &aiov; 1814160ccd9SAlan Cox auio.uio_iovcnt = 1; 182bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 1834160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 184bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, &auio, uap->offset); 1854160ccd9SAlan Cox return(error); 1864160ccd9SAlan Cox } 1874160ccd9SAlan Cox 188c2815ad5SPeter Wemm int 189c2815ad5SPeter Wemm freebsd6_pread(td, uap) 190c2815ad5SPeter Wemm struct thread *td; 191c2815ad5SPeter Wemm struct freebsd6_pread_args *uap; 192c2815ad5SPeter Wemm { 193c2815ad5SPeter Wemm struct pread_args oargs; 194c2815ad5SPeter Wemm 195c2815ad5SPeter Wemm oargs.fd = uap->fd; 196c2815ad5SPeter Wemm oargs.buf = uap->buf; 197c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 198c2815ad5SPeter Wemm oargs.offset = uap->offset; 199c2815ad5SPeter Wemm return (pread(td, &oargs)); 200c2815ad5SPeter Wemm } 201c2815ad5SPeter Wemm 2024160ccd9SAlan Cox /* 203df8bae1dSRodney W. Grimes * Scatter read system call. 204df8bae1dSRodney W. Grimes */ 205d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 206df8bae1dSRodney W. Grimes struct readv_args { 2077147b19dSBruce Evans int fd; 208df8bae1dSRodney W. Grimes struct iovec *iovp; 209df8bae1dSRodney W. Grimes u_int iovcnt; 210df8bae1dSRodney W. Grimes }; 211d2d3e875SBruce Evans #endif 21226f9a767SRodney W. Grimes int 213552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap) 214df8bae1dSRodney W. Grimes { 215b88ec951SJohn Baldwin struct uio *auio; 216b88ec951SJohn Baldwin int error; 217b88ec951SJohn Baldwin 218b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 219b88ec951SJohn Baldwin if (error) 220b88ec951SJohn Baldwin return (error); 221b88ec951SJohn Baldwin error = kern_readv(td, uap->fd, auio); 222b88ec951SJohn Baldwin free(auio, M_IOV); 223b88ec951SJohn Baldwin return (error); 224b88ec951SJohn Baldwin } 225b88ec951SJohn Baldwin 226b88ec951SJohn Baldwin int 227b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio) 228b88ec951SJohn Baldwin { 229b064d43dSMatthew Dillon struct file *fp; 230bcd9e0ddSJohn Baldwin int error; 231bcd9e0ddSJohn Baldwin 232bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 233bcd9e0ddSJohn Baldwin if (error) 234bcd9e0ddSJohn Baldwin return (error); 235bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, (off_t)-1, 0); 236bcd9e0ddSJohn Baldwin fdrop(fp, td); 237bcd9e0ddSJohn Baldwin return (error); 238bcd9e0ddSJohn Baldwin } 239bcd9e0ddSJohn Baldwin 240bcd9e0ddSJohn Baldwin /* 241bcd9e0ddSJohn Baldwin * Scatter positioned read system call. 242bcd9e0ddSJohn Baldwin */ 243bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 244bcd9e0ddSJohn Baldwin struct preadv_args { 245bcd9e0ddSJohn Baldwin int fd; 246bcd9e0ddSJohn Baldwin struct iovec *iovp; 247bcd9e0ddSJohn Baldwin u_int iovcnt; 248bcd9e0ddSJohn Baldwin off_t offset; 249bcd9e0ddSJohn Baldwin }; 250bcd9e0ddSJohn Baldwin #endif 251bcd9e0ddSJohn Baldwin int 252bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap) 253bcd9e0ddSJohn Baldwin { 254bcd9e0ddSJohn Baldwin struct uio *auio; 255bcd9e0ddSJohn Baldwin int error; 256bcd9e0ddSJohn Baldwin 257bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 258bcd9e0ddSJohn Baldwin if (error) 259bcd9e0ddSJohn Baldwin return (error); 260bcd9e0ddSJohn Baldwin error = kern_preadv(td, uap->fd, auio, uap->offset); 261bcd9e0ddSJohn Baldwin free(auio, M_IOV); 262bcd9e0ddSJohn Baldwin return (error); 263bcd9e0ddSJohn Baldwin } 264bcd9e0ddSJohn Baldwin 265bcd9e0ddSJohn Baldwin int 266bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset) 267bcd9e0ddSJohn Baldwin struct thread *td; 268bcd9e0ddSJohn Baldwin int fd; 269bcd9e0ddSJohn Baldwin struct uio *auio; 270bcd9e0ddSJohn Baldwin off_t offset; 271bcd9e0ddSJohn Baldwin { 272bcd9e0ddSJohn Baldwin struct file *fp; 273bcd9e0ddSJohn Baldwin int error; 274bcd9e0ddSJohn Baldwin 275bcd9e0ddSJohn Baldwin error = fget_read(td, fd, &fp); 276bcd9e0ddSJohn Baldwin if (error) 277bcd9e0ddSJohn Baldwin return (error); 278bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 279bcd9e0ddSJohn Baldwin error = ESPIPE; 280bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 281bcd9e0ddSJohn Baldwin error = EINVAL; 282bcd9e0ddSJohn Baldwin else 283bcd9e0ddSJohn Baldwin error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET); 284bcd9e0ddSJohn Baldwin fdrop(fp, td); 285bcd9e0ddSJohn Baldwin return (error); 286bcd9e0ddSJohn Baldwin } 287bcd9e0ddSJohn Baldwin 288bcd9e0ddSJohn Baldwin /* 289bcd9e0ddSJohn Baldwin * Common code for readv and preadv that reads data in 290bcd9e0ddSJohn Baldwin * from a file using the passed in uio, offset, and flags. 291bcd9e0ddSJohn Baldwin */ 292bcd9e0ddSJohn Baldwin static int 293bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags) 294bcd9e0ddSJohn Baldwin struct thread *td; 295bcd9e0ddSJohn Baldwin int fd; 296bcd9e0ddSJohn Baldwin struct file *fp; 297bcd9e0ddSJohn Baldwin struct uio *auio; 298bcd9e0ddSJohn Baldwin off_t offset; 299bcd9e0ddSJohn Baldwin int flags; 300bcd9e0ddSJohn Baldwin { 301bcd9e0ddSJohn Baldwin ssize_t cnt; 30282641acdSAlan Cox int error; 303df8bae1dSRodney W. Grimes #ifdef KTRACE 304552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 305df8bae1dSRodney W. Grimes #endif 306df8bae1dSRodney W. Grimes 3074f8d23d6SPoul-Henning Kamp /* Finish zero length reads right here */ 3084f8d23d6SPoul-Henning Kamp if (auio->uio_resid == 0) { 3094f8d23d6SPoul-Henning Kamp td->td_retval[0] = 0; 3104f8d23d6SPoul-Henning Kamp return(0); 3114f8d23d6SPoul-Henning Kamp } 312552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_READ; 313bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 314552afd9cSPoul-Henning Kamp auio->uio_td = td; 315df8bae1dSRodney W. Grimes #ifdef KTRACE 316552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 317552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 318df8bae1dSRodney W. Grimes #endif 319552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 320bcd9e0ddSJohn Baldwin if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) { 321552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 322df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 323df8bae1dSRodney W. Grimes error = 0; 324279d7226SMatthew Dillon } 325552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 326df8bae1dSRodney W. Grimes #ifdef KTRACE 327552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 328552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 329b88ec951SJohn Baldwin ktrgenio(fd, UIO_READ, ktruio, error); 330df8bae1dSRodney W. Grimes } 331df8bae1dSRodney W. Grimes #endif 332b40ce416SJulian Elischer td->td_retval[0] = cnt; 333df8bae1dSRodney W. Grimes return (error); 334df8bae1dSRodney W. Grimes } 335df8bae1dSRodney W. Grimes 336d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 337df8bae1dSRodney W. Grimes struct write_args { 338df8bae1dSRodney W. Grimes int fd; 339134e06feSBruce Evans const void *buf; 340134e06feSBruce Evans size_t nbyte; 341df8bae1dSRodney W. Grimes }; 342d2d3e875SBruce Evans #endif 34326f9a767SRodney W. Grimes int 344b40ce416SJulian Elischer write(td, uap) 345b40ce416SJulian Elischer struct thread *td; 346b064d43dSMatthew Dillon struct write_args *uap; 347df8bae1dSRodney W. Grimes { 348bcd9e0ddSJohn Baldwin struct uio auio; 349bcd9e0ddSJohn Baldwin struct iovec aiov; 350279d7226SMatthew Dillon int error; 351df8bae1dSRodney W. Grimes 352bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 353bcd9e0ddSJohn Baldwin return (EINVAL); 354bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 355bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 356bcd9e0ddSJohn Baldwin auio.uio_iov = &aiov; 357bcd9e0ddSJohn Baldwin auio.uio_iovcnt = 1; 358bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 359bcd9e0ddSJohn Baldwin auio.uio_segflg = UIO_USERSPACE; 360bcd9e0ddSJohn Baldwin error = kern_writev(td, uap->fd, &auio); 361279d7226SMatthew Dillon return(error); 362df8bae1dSRodney W. Grimes } 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes /* 3650c14ff0eSRobert Watson * Positioned write system call. 3664160ccd9SAlan Cox */ 3674160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_ 3684160ccd9SAlan Cox struct pwrite_args { 3694160ccd9SAlan Cox int fd; 3704160ccd9SAlan Cox const void *buf; 3714160ccd9SAlan Cox size_t nbyte; 3728fe387abSDmitrij Tejblum int pad; 3734160ccd9SAlan Cox off_t offset; 3744160ccd9SAlan Cox }; 3754160ccd9SAlan Cox #endif 3764160ccd9SAlan Cox int 377b40ce416SJulian Elischer pwrite(td, uap) 378b40ce416SJulian Elischer struct thread *td; 379b064d43dSMatthew Dillon struct pwrite_args *uap; 3804160ccd9SAlan Cox { 3814160ccd9SAlan Cox struct uio auio; 3824160ccd9SAlan Cox struct iovec aiov; 383bcd9e0ddSJohn Baldwin int error; 3844160ccd9SAlan Cox 385bcd9e0ddSJohn Baldwin if (uap->nbyte > INT_MAX) 386bcd9e0ddSJohn Baldwin return (EINVAL); 387bcd9e0ddSJohn Baldwin aiov.iov_base = (void *)(uintptr_t)uap->buf; 388bcd9e0ddSJohn Baldwin aiov.iov_len = uap->nbyte; 3894160ccd9SAlan Cox auio.uio_iov = &aiov; 3904160ccd9SAlan Cox auio.uio_iovcnt = 1; 391bcd9e0ddSJohn Baldwin auio.uio_resid = uap->nbyte; 3924160ccd9SAlan Cox auio.uio_segflg = UIO_USERSPACE; 393bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, &auio, uap->offset); 3944160ccd9SAlan Cox return(error); 3954160ccd9SAlan Cox } 3964160ccd9SAlan Cox 397c2815ad5SPeter Wemm int 398c2815ad5SPeter Wemm freebsd6_pwrite(td, uap) 399c2815ad5SPeter Wemm struct thread *td; 400c2815ad5SPeter Wemm struct freebsd6_pwrite_args *uap; 401c2815ad5SPeter Wemm { 402c2815ad5SPeter Wemm struct pwrite_args oargs; 403c2815ad5SPeter Wemm 404c2815ad5SPeter Wemm oargs.fd = uap->fd; 405c2815ad5SPeter Wemm oargs.buf = uap->buf; 406c2815ad5SPeter Wemm oargs.nbyte = uap->nbyte; 407c2815ad5SPeter Wemm oargs.offset = uap->offset; 408c2815ad5SPeter Wemm return (pwrite(td, &oargs)); 409c2815ad5SPeter Wemm } 410c2815ad5SPeter Wemm 4114160ccd9SAlan Cox /* 4120c14ff0eSRobert Watson * Gather write system call. 413df8bae1dSRodney W. Grimes */ 414d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 415df8bae1dSRodney W. Grimes struct writev_args { 416df8bae1dSRodney W. Grimes int fd; 417df8bae1dSRodney W. Grimes struct iovec *iovp; 418df8bae1dSRodney W. Grimes u_int iovcnt; 419df8bae1dSRodney W. Grimes }; 420d2d3e875SBruce Evans #endif 42126f9a767SRodney W. Grimes int 422552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap) 423df8bae1dSRodney W. Grimes { 424b88ec951SJohn Baldwin struct uio *auio; 425b88ec951SJohn Baldwin int error; 426b88ec951SJohn Baldwin 427b88ec951SJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 428b88ec951SJohn Baldwin if (error) 429b88ec951SJohn Baldwin return (error); 430b88ec951SJohn Baldwin error = kern_writev(td, uap->fd, auio); 431b88ec951SJohn Baldwin free(auio, M_IOV); 432b88ec951SJohn Baldwin return (error); 433b88ec951SJohn Baldwin } 434b88ec951SJohn Baldwin 435b88ec951SJohn Baldwin int 436b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio) 437b88ec951SJohn Baldwin { 438b064d43dSMatthew Dillon struct file *fp; 439bcd9e0ddSJohn Baldwin int error; 440bcd9e0ddSJohn Baldwin 441bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 442bcd9e0ddSJohn Baldwin if (error) 443af56abaaSJohn Baldwin return (error); 444bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0); 445bcd9e0ddSJohn Baldwin fdrop(fp, td); 446bcd9e0ddSJohn Baldwin return (error); 447bcd9e0ddSJohn Baldwin } 448bcd9e0ddSJohn Baldwin 449bcd9e0ddSJohn Baldwin /* 4500c14ff0eSRobert Watson * Gather positioned write system call. 451bcd9e0ddSJohn Baldwin */ 452bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 453bcd9e0ddSJohn Baldwin struct pwritev_args { 454bcd9e0ddSJohn Baldwin int fd; 455bcd9e0ddSJohn Baldwin struct iovec *iovp; 456bcd9e0ddSJohn Baldwin u_int iovcnt; 457bcd9e0ddSJohn Baldwin off_t offset; 458bcd9e0ddSJohn Baldwin }; 459bcd9e0ddSJohn Baldwin #endif 460bcd9e0ddSJohn Baldwin int 461bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap) 462bcd9e0ddSJohn Baldwin { 463bcd9e0ddSJohn Baldwin struct uio *auio; 464bcd9e0ddSJohn Baldwin int error; 465bcd9e0ddSJohn Baldwin 466bcd9e0ddSJohn Baldwin error = copyinuio(uap->iovp, uap->iovcnt, &auio); 467bcd9e0ddSJohn Baldwin if (error) 468bcd9e0ddSJohn Baldwin return (error); 469bcd9e0ddSJohn Baldwin error = kern_pwritev(td, uap->fd, auio, uap->offset); 470bcd9e0ddSJohn Baldwin free(auio, M_IOV); 471bcd9e0ddSJohn Baldwin return (error); 472bcd9e0ddSJohn Baldwin } 473bcd9e0ddSJohn Baldwin 474bcd9e0ddSJohn Baldwin int 475bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset) 476bcd9e0ddSJohn Baldwin struct thread *td; 477bcd9e0ddSJohn Baldwin struct uio *auio; 478bcd9e0ddSJohn Baldwin int fd; 479bcd9e0ddSJohn Baldwin off_t offset; 480bcd9e0ddSJohn Baldwin { 481bcd9e0ddSJohn Baldwin struct file *fp; 482bcd9e0ddSJohn Baldwin int error; 483bcd9e0ddSJohn Baldwin 484bcd9e0ddSJohn Baldwin error = fget_write(td, fd, &fp); 485bcd9e0ddSJohn Baldwin if (error) 486af56abaaSJohn Baldwin return (error); 487bcd9e0ddSJohn Baldwin if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) 488bcd9e0ddSJohn Baldwin error = ESPIPE; 489bcd9e0ddSJohn Baldwin else if (offset < 0 && fp->f_vnode->v_type != VCHR) 490bcd9e0ddSJohn Baldwin error = EINVAL; 491bcd9e0ddSJohn Baldwin else 492bcd9e0ddSJohn Baldwin error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET); 493bcd9e0ddSJohn Baldwin fdrop(fp, td); 494bcd9e0ddSJohn Baldwin return (error); 495bcd9e0ddSJohn Baldwin } 496bcd9e0ddSJohn Baldwin 497bcd9e0ddSJohn Baldwin /* 498bcd9e0ddSJohn Baldwin * Common code for writev and pwritev that writes data to 499bcd9e0ddSJohn Baldwin * a file using the passed in uio, offset, and flags. 500bcd9e0ddSJohn Baldwin */ 501bcd9e0ddSJohn Baldwin static int 502bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags) 503bcd9e0ddSJohn Baldwin struct thread *td; 504bcd9e0ddSJohn Baldwin int fd; 505bcd9e0ddSJohn Baldwin struct file *fp; 506bcd9e0ddSJohn Baldwin struct uio *auio; 507bcd9e0ddSJohn Baldwin off_t offset; 508bcd9e0ddSJohn Baldwin int flags; 509bcd9e0ddSJohn Baldwin { 510bcd9e0ddSJohn Baldwin ssize_t cnt; 511552afd9cSPoul-Henning Kamp int error; 512df8bae1dSRodney W. Grimes #ifdef KTRACE 513552afd9cSPoul-Henning Kamp struct uio *ktruio = NULL; 514df8bae1dSRodney W. Grimes #endif 515df8bae1dSRodney W. Grimes 516552afd9cSPoul-Henning Kamp auio->uio_rw = UIO_WRITE; 517552afd9cSPoul-Henning Kamp auio->uio_td = td; 518bcd9e0ddSJohn Baldwin auio->uio_offset = offset; 519df8bae1dSRodney W. Grimes #ifdef KTRACE 520552afd9cSPoul-Henning Kamp if (KTRPOINT(td, KTR_GENIO)) 521552afd9cSPoul-Henning Kamp ktruio = cloneuio(auio); 522df8bae1dSRodney W. Grimes #endif 523552afd9cSPoul-Henning Kamp cnt = auio->uio_resid; 524a41ce5d3SMatthew Dillon if (fp->f_type == DTYPE_VNODE) 5259440653dSMatthew Dillon bwillwrite(); 526bcd9e0ddSJohn Baldwin if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) { 527552afd9cSPoul-Henning Kamp if (auio->uio_resid != cnt && (error == ERESTART || 528df8bae1dSRodney W. Grimes error == EINTR || error == EWOULDBLOCK)) 529df8bae1dSRodney W. Grimes error = 0; 530bcd9e0ddSJohn Baldwin /* Socket layer is responsible for issuing SIGPIPE. */ 5316f7ca813SBruce M Simpson if (fp->f_type != DTYPE_SOCKET && error == EPIPE) { 532b40ce416SJulian Elischer PROC_LOCK(td->td_proc); 533b40ce416SJulian Elischer psignal(td->td_proc, SIGPIPE); 534b40ce416SJulian Elischer PROC_UNLOCK(td->td_proc); 53519eb87d2SJohn Baldwin } 536df8bae1dSRodney W. Grimes } 537552afd9cSPoul-Henning Kamp cnt -= auio->uio_resid; 538df8bae1dSRodney W. Grimes #ifdef KTRACE 539552afd9cSPoul-Henning Kamp if (ktruio != NULL) { 540552afd9cSPoul-Henning Kamp ktruio->uio_resid = cnt; 541b88ec951SJohn Baldwin ktrgenio(fd, UIO_WRITE, ktruio, error); 542df8bae1dSRodney W. Grimes } 543df8bae1dSRodney W. Grimes #endif 544b40ce416SJulian Elischer td->td_retval[0] = cnt; 545df8bae1dSRodney W. Grimes return (error); 546df8bae1dSRodney W. Grimes } 547df8bae1dSRodney W. Grimes 548e4650294SJohn Baldwin /* 549e4650294SJohn Baldwin * Truncate a file given a file descriptor. 550e4650294SJohn Baldwin * 551e4650294SJohn Baldwin * Can't use fget_write() here, since must return EINVAL and not EBADF if the 552e4650294SJohn Baldwin * descriptor isn't writable. 553e4650294SJohn Baldwin */ 554e4650294SJohn Baldwin int 555e4650294SJohn Baldwin kern_ftruncate(td, fd, length) 556e4650294SJohn Baldwin struct thread *td; 557e4650294SJohn Baldwin int fd; 558e4650294SJohn Baldwin off_t length; 559e4650294SJohn Baldwin { 560e4650294SJohn Baldwin struct file *fp; 561e4650294SJohn Baldwin int error; 562e4650294SJohn Baldwin 563e4650294SJohn Baldwin AUDIT_ARG(fd, fd); 564e4650294SJohn Baldwin if (length < 0) 565e4650294SJohn Baldwin return (EINVAL); 566e4650294SJohn Baldwin error = fget(td, fd, &fp); 567e4650294SJohn Baldwin if (error) 568e4650294SJohn Baldwin return (error); 569e4650294SJohn Baldwin AUDIT_ARG(file, td->td_proc, fp); 570e4650294SJohn Baldwin if (!(fp->f_flag & FWRITE)) { 571e4650294SJohn Baldwin fdrop(fp, td); 572e4650294SJohn Baldwin return (EINVAL); 573e4650294SJohn Baldwin } 574e4650294SJohn Baldwin error = fo_truncate(fp, length, td->td_ucred, td); 575e4650294SJohn Baldwin fdrop(fp, td); 576e4650294SJohn Baldwin return (error); 577e4650294SJohn Baldwin } 578e4650294SJohn Baldwin 579e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 580e4650294SJohn Baldwin struct ftruncate_args { 581e4650294SJohn Baldwin int fd; 582e4650294SJohn Baldwin int pad; 583e4650294SJohn Baldwin off_t length; 584e4650294SJohn Baldwin }; 585e4650294SJohn Baldwin #endif 586e4650294SJohn Baldwin int 587e4650294SJohn Baldwin ftruncate(td, uap) 588e4650294SJohn Baldwin struct thread *td; 589e4650294SJohn Baldwin struct ftruncate_args *uap; 590e4650294SJohn Baldwin { 591e4650294SJohn Baldwin 592e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 593e4650294SJohn Baldwin } 594e4650294SJohn Baldwin 595e4650294SJohn Baldwin #if defined(COMPAT_43) 596e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_ 597e4650294SJohn Baldwin struct oftruncate_args { 598e4650294SJohn Baldwin int fd; 599e4650294SJohn Baldwin long length; 600e4650294SJohn Baldwin }; 601e4650294SJohn Baldwin #endif 602e4650294SJohn Baldwin int 603e4650294SJohn Baldwin oftruncate(td, uap) 604e4650294SJohn Baldwin struct thread *td; 605e4650294SJohn Baldwin struct oftruncate_args *uap; 606e4650294SJohn Baldwin { 607e4650294SJohn Baldwin 608e4650294SJohn Baldwin return (kern_ftruncate(td, uap->fd, uap->length)); 609e4650294SJohn Baldwin } 610e4650294SJohn Baldwin #endif /* COMPAT_43 */ 611e4650294SJohn Baldwin 612d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 613df8bae1dSRodney W. Grimes struct ioctl_args { 614df8bae1dSRodney W. Grimes int fd; 615069e9bc1SDoug Rabson u_long com; 616df8bae1dSRodney W. Grimes caddr_t data; 617df8bae1dSRodney W. Grimes }; 618d2d3e875SBruce Evans #endif 619df8bae1dSRodney W. Grimes /* ARGSUSED */ 62026f9a767SRodney W. Grimes int 6213e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap) 622df8bae1dSRodney W. Grimes { 6233e15c66fSPoul-Henning Kamp u_long com; 6249fddcc66SRuslan Ermilov int arg, error; 6253e15c66fSPoul-Henning Kamp u_int size; 6269fddcc66SRuslan Ermilov caddr_t data; 627df8bae1dSRodney W. Grimes 6289fc6aa06SPoul-Henning Kamp if (uap->com > 0xffffffff) { 6299fc6aa06SPoul-Henning Kamp printf( 6309fc6aa06SPoul-Henning Kamp "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n", 631431f8906SJulian Elischer td->td_proc->p_pid, td->td_name, uap->com); 6329fc6aa06SPoul-Henning Kamp uap->com &= 0xffffffff; 6339fc6aa06SPoul-Henning Kamp } 634d9f46233SJohn Baldwin com = uap->com; 635df8bae1dSRodney W. Grimes 636df8bae1dSRodney W. Grimes /* 637df8bae1dSRodney W. Grimes * Interpret high order word to find amount of data to be 638df8bae1dSRodney W. Grimes * copied to/from the user's address space. 639df8bae1dSRodney W. Grimes */ 640df8bae1dSRodney W. Grimes size = IOCPARM_LEN(com); 641ca51b19bSPoul-Henning Kamp if ((size > IOCPARM_MAX) || 642ca51b19bSPoul-Henning Kamp ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) || 6432de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 6442de92a38SPeter Wemm ((com & IOC_OUT) && size == 0) || 6452de92a38SPeter Wemm #else 6462de92a38SPeter Wemm ((com & (IOC_IN | IOC_OUT)) && size == 0) || 6472de92a38SPeter Wemm #endif 6489fddcc66SRuslan Ermilov ((com & IOC_VOID) && size > 0 && size != sizeof(int))) 649426da3bcSAlfred Perlstein return (ENOTTY); 650279d7226SMatthew Dillon 651ca51b19bSPoul-Henning Kamp if (size > 0) { 6529fddcc66SRuslan Ermilov if (!(com & IOC_VOID)) 6539fddcc66SRuslan Ermilov data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK); 6549fddcc66SRuslan Ermilov else { 6559fddcc66SRuslan Ermilov /* Integer argument. */ 6569fddcc66SRuslan Ermilov arg = (intptr_t)uap->data; 6579fddcc66SRuslan Ermilov data = (void *)&arg; 6589fddcc66SRuslan Ermilov size = 0; 659279d7226SMatthew Dillon } 6609fddcc66SRuslan Ermilov } else 6619fddcc66SRuslan Ermilov data = (void *)&uap->data; 662df8bae1dSRodney W. Grimes if (com & IOC_IN) { 663df8bae1dSRodney W. Grimes error = copyin(uap->data, data, (u_int)size); 664df8bae1dSRodney W. Grimes if (error) { 665a1b0a180SRuslan Ermilov if (size > 0) 6669fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 6673e15c66fSPoul-Henning Kamp return (error); 668df8bae1dSRodney W. Grimes } 669ca51b19bSPoul-Henning Kamp } else if (com & IOC_OUT) { 670df8bae1dSRodney W. Grimes /* 671df8bae1dSRodney W. Grimes * Zero the buffer so the user always 672df8bae1dSRodney W. Grimes * gets back something deterministic. 673df8bae1dSRodney W. Grimes */ 674df8bae1dSRodney W. Grimes bzero(data, size); 675279d7226SMatthew Dillon } 676df8bae1dSRodney W. Grimes 677d9f46233SJohn Baldwin error = kern_ioctl(td, uap->fd, com, data); 678d9f46233SJohn Baldwin 679d9f46233SJohn Baldwin if (error == 0 && (com & IOC_OUT)) 680d9f46233SJohn Baldwin error = copyout(data, uap->data, (u_int)size); 681d9f46233SJohn Baldwin 6829fddcc66SRuslan Ermilov if (size > 0) 6839fddcc66SRuslan Ermilov free(data, M_IOCTLOPS); 684d9f46233SJohn Baldwin return (error); 685d9f46233SJohn Baldwin } 686d9f46233SJohn Baldwin 687d9f46233SJohn Baldwin int 688d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data) 689d9f46233SJohn Baldwin { 690d9f46233SJohn Baldwin struct file *fp; 691d9f46233SJohn Baldwin struct filedesc *fdp; 692d9f46233SJohn Baldwin int error; 693d9f46233SJohn Baldwin int tmp; 694d9f46233SJohn Baldwin 695d9f46233SJohn Baldwin if ((error = fget(td, fd, &fp)) != 0) 696d9f46233SJohn Baldwin return (error); 697d9f46233SJohn Baldwin if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 698d9f46233SJohn Baldwin fdrop(fp, td); 699d9f46233SJohn Baldwin return (EBADF); 700d9f46233SJohn Baldwin } 701d9f46233SJohn Baldwin fdp = td->td_proc->p_fd; 702d9f46233SJohn Baldwin switch (com) { 703d9f46233SJohn Baldwin case FIONCLEX: 7045e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 705d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE; 7065e3f7694SRobert Watson FILEDESC_XUNLOCK(fdp); 707d9f46233SJohn Baldwin goto out; 708d9f46233SJohn Baldwin case FIOCLEX: 7095e3f7694SRobert Watson FILEDESC_XLOCK(fdp); 710d9f46233SJohn Baldwin fdp->fd_ofileflags[fd] |= UF_EXCLOSE; 7115e3f7694SRobert Watson FILEDESC_XUNLOCK(fdp); 712d9f46233SJohn Baldwin goto out; 713d9f46233SJohn Baldwin case FIONBIO: 714bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 715397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FNONBLOCK); 716df8bae1dSRodney W. Grimes else 717397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FNONBLOCK); 7188ccf264fSPoul-Henning Kamp data = (void *)&tmp; 719d9f46233SJohn Baldwin break; 720d9f46233SJohn Baldwin case FIOASYNC: 721bb56ec4aSPoul-Henning Kamp if ((tmp = *(int *)data)) 722397c19d1SJeff Roberson atomic_set_int(&fp->f_flag, FASYNC); 723df8bae1dSRodney W. Grimes else 724397c19d1SJeff Roberson atomic_clear_int(&fp->f_flag, FASYNC); 7258ccf264fSPoul-Henning Kamp data = (void *)&tmp; 726d9f46233SJohn Baldwin break; 727df8bae1dSRodney W. Grimes } 7288ccf264fSPoul-Henning Kamp 7298ccf264fSPoul-Henning Kamp error = fo_ioctl(fp, com, data, td->td_ucred, td); 730d9f46233SJohn Baldwin out: 731b40ce416SJulian Elischer fdrop(fp, td); 732df8bae1dSRodney W. Grimes return (error); 733df8bae1dSRodney W. Grimes } 734df8bae1dSRodney W. Grimes 735d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 736df8bae1dSRodney W. Grimes struct select_args { 737b08f7993SSujal Patel int nd; 738df8bae1dSRodney W. Grimes fd_set *in, *ou, *ex; 739df8bae1dSRodney W. Grimes struct timeval *tv; 740df8bae1dSRodney W. Grimes }; 741d2d3e875SBruce Evans #endif 74226f9a767SRodney W. Grimes int 743b40ce416SJulian Elischer select(td, uap) 744b40ce416SJulian Elischer register struct thread *td; 745df8bae1dSRodney W. Grimes register struct select_args *uap; 746df8bae1dSRodney W. Grimes { 7478f19eb88SIan Dowse struct timeval tv, *tvp; 7488f19eb88SIan Dowse int error; 7498f19eb88SIan Dowse 7508f19eb88SIan Dowse if (uap->tv != NULL) { 7518f19eb88SIan Dowse error = copyin(uap->tv, &tv, sizeof(tv)); 7528f19eb88SIan Dowse if (error) 7538f19eb88SIan Dowse return (error); 7548f19eb88SIan Dowse tvp = &tv; 7558f19eb88SIan Dowse } else 7568f19eb88SIan Dowse tvp = NULL; 7578f19eb88SIan Dowse 7588f19eb88SIan Dowse return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp)); 7598f19eb88SIan Dowse } 7608f19eb88SIan Dowse 7618f19eb88SIan Dowse int 7628f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, 7638f19eb88SIan Dowse fd_set *fd_ex, struct timeval *tvp) 7648f19eb88SIan Dowse { 765426da3bcSAlfred Perlstein struct filedesc *fdp; 766d5e4d7e1SBruce Evans /* 767d5e4d7e1SBruce Evans * The magic 2048 here is chosen to be just enough for FD_SETSIZE 768d5e4d7e1SBruce Evans * infds with the new FD_SETSIZE of 1024, and more than enough for 769d5e4d7e1SBruce Evans * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE 770d5e4d7e1SBruce Evans * of 256. 771d5e4d7e1SBruce Evans */ 772d5e4d7e1SBruce Evans fd_mask s_selbits[howmany(2048, NFDBITS)]; 773eb209311SAlfred Perlstein fd_mask *ibits[3], *obits[3], *selbits, *sbp; 77400af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 7759ae6d334SKelly Yancey int error, timo; 776ace8398dSJeff Roberson u_int nbufbytes, ncpbytes, nfdbits; 777df8bae1dSRodney W. Grimes 7788f19eb88SIan Dowse if (nd < 0) 779acbfbfeaSSujal Patel return (EINVAL); 780426da3bcSAlfred Perlstein fdp = td->td_proc->p_fd; 781db446e30SPoul-Henning Kamp 7825e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 7838f19eb88SIan Dowse if (nd > td->td_proc->p_fd->fd_nfiles) 7848f19eb88SIan Dowse nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */ 7855e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 786b08f7993SSujal Patel 787d5e4d7e1SBruce Evans /* 788d5e4d7e1SBruce Evans * Allocate just enough bits for the non-null fd_sets. Use the 789d5e4d7e1SBruce Evans * preallocated auto buffer if possible. 790d5e4d7e1SBruce Evans */ 7918f19eb88SIan Dowse nfdbits = roundup(nd, NFDBITS); 792d5e4d7e1SBruce Evans ncpbytes = nfdbits / NBBY; 793d5e4d7e1SBruce Evans nbufbytes = 0; 7948f19eb88SIan Dowse if (fd_in != NULL) 795d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7968f19eb88SIan Dowse if (fd_ou != NULL) 797d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 7988f19eb88SIan Dowse if (fd_ex != NULL) 799d5e4d7e1SBruce Evans nbufbytes += 2 * ncpbytes; 800d5e4d7e1SBruce Evans if (nbufbytes <= sizeof s_selbits) 801d5e4d7e1SBruce Evans selbits = &s_selbits[0]; 802d5e4d7e1SBruce Evans else 803a163d034SWarner Losh selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); 804b08f7993SSujal Patel 805b08f7993SSujal Patel /* 806d5e4d7e1SBruce Evans * Assign pointers into the bit buffers and fetch the input bits. 807d5e4d7e1SBruce Evans * Put the output buffers together so that they can be bzeroed 808d5e4d7e1SBruce Evans * together. 809b08f7993SSujal Patel */ 810d5e4d7e1SBruce Evans sbp = selbits; 811df8bae1dSRodney W. Grimes #define getbits(name, x) \ 812d5e4d7e1SBruce Evans do { \ 8138f19eb88SIan Dowse if (name == NULL) \ 814d5e4d7e1SBruce Evans ibits[x] = NULL; \ 815d5e4d7e1SBruce Evans else { \ 816d5e4d7e1SBruce Evans ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \ 817d5e4d7e1SBruce Evans obits[x] = sbp; \ 818d5e4d7e1SBruce Evans sbp += ncpbytes / sizeof *sbp; \ 8198f19eb88SIan Dowse error = copyin(name, ibits[x], ncpbytes); \ 820265fc98fSSeigo Tanimura if (error != 0) \ 821ace8398dSJeff Roberson goto done; \ 822e04ac2feSJohn Baldwin } \ 823d5e4d7e1SBruce Evans } while (0) 8248f19eb88SIan Dowse getbits(fd_in, 0); 8258f19eb88SIan Dowse getbits(fd_ou, 1); 8268f19eb88SIan Dowse getbits(fd_ex, 2); 827df8bae1dSRodney W. Grimes #undef getbits 828d5e4d7e1SBruce Evans if (nbufbytes != 0) 829d5e4d7e1SBruce Evans bzero(selbits, nbufbytes / 2); 830df8bae1dSRodney W. Grimes 8318f19eb88SIan Dowse if (tvp != NULL) { 8328f19eb88SIan Dowse atv = *tvp; 833df8bae1dSRodney W. Grimes if (itimerfix(&atv)) { 834df8bae1dSRodney W. Grimes error = EINVAL; 835ace8398dSJeff Roberson goto done; 836df8bae1dSRodney W. Grimes } 837c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 83800af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 8399c386f6bSJohn Baldwin } else { 84000af9731SPoul-Henning Kamp atv.tv_sec = 0; 8419c386f6bSJohn Baldwin atv.tv_usec = 0; 8429c386f6bSJohn Baldwin } 84300af9731SPoul-Henning Kamp timo = 0; 844ace8398dSJeff Roberson seltdinit(td); 845ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 846ace8398dSJeff Roberson for (;;) { 8478f19eb88SIan Dowse error = selscan(td, ibits, obits, nd); 848ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 849ace8398dSJeff Roberson break; 8504da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 851c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 85285f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 853ace8398dSJeff Roberson break; 85400af9731SPoul-Henning Kamp ttv = atv; 85500af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 85600af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 85700af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 858df8bae1dSRodney W. Grimes } 859ace8398dSJeff Roberson error = seltdwait(td, timo); 860ace8398dSJeff Roberson if (error) 861ace8398dSJeff Roberson break; 862ace8398dSJeff Roberson error = selrescan(td, ibits, obits); 863ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 864ace8398dSJeff Roberson break; 86585f190e4SAlfred Perlstein } 866ace8398dSJeff Roberson seltdclear(td); 867265fc98fSSeigo Tanimura 868df8bae1dSRodney W. Grimes done: 869df8bae1dSRodney W. Grimes /* select is not restarted after signals... */ 870df8bae1dSRodney W. Grimes if (error == ERESTART) 871df8bae1dSRodney W. Grimes error = EINTR; 872df8bae1dSRodney W. Grimes if (error == EWOULDBLOCK) 873df8bae1dSRodney W. Grimes error = 0; 874df8bae1dSRodney W. Grimes #define putbits(name, x) \ 8758f19eb88SIan Dowse if (name && (error2 = copyout(obits[x], name, ncpbytes))) \ 876df8bae1dSRodney W. Grimes error = error2; 877df8bae1dSRodney W. Grimes if (error == 0) { 878df8bae1dSRodney W. Grimes int error2; 879df8bae1dSRodney W. Grimes 8808f19eb88SIan Dowse putbits(fd_in, 0); 8818f19eb88SIan Dowse putbits(fd_ou, 1); 8828f19eb88SIan Dowse putbits(fd_ex, 2); 883df8bae1dSRodney W. Grimes #undef putbits 884df8bae1dSRodney W. Grimes } 885d5e4d7e1SBruce Evans if (selbits != &s_selbits[0]) 886d5e4d7e1SBruce Evans free(selbits, M_SELECT); 887ad2edad9SMatthew Dillon 888df8bae1dSRodney W. Grimes return (error); 889df8bae1dSRodney W. Grimes } 890df8bae1dSRodney W. Grimes 891ace8398dSJeff Roberson /* 892ace8398dSJeff Roberson * Traverse the list of fds attached to this thread's seltd and check for 893ace8398dSJeff Roberson * completion. 894ace8398dSJeff Roberson */ 895ace8398dSJeff Roberson static int 896ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits) 897ace8398dSJeff Roberson { 898ace8398dSJeff Roberson struct seltd *stp; 899ace8398dSJeff Roberson struct selfd *sfp; 900ace8398dSJeff Roberson struct selfd *sfn; 901ace8398dSJeff Roberson struct selinfo *si; 902ace8398dSJeff Roberson struct file *fp; 903ace8398dSJeff Roberson int msk, fd; 904ace8398dSJeff Roberson int n = 0; 905ace8398dSJeff Roberson /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 906ace8398dSJeff Roberson static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 907ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 908ace8398dSJeff Roberson 909ace8398dSJeff Roberson stp = td->td_sel; 910ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 911ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 912ace8398dSJeff Roberson fd = (int)(uintptr_t)sfp->sf_cookie; 913ace8398dSJeff Roberson si = sfp->sf_si; 914ace8398dSJeff Roberson selfdfree(stp, sfp); 915ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 916ace8398dSJeff Roberson if (si != NULL) 917ace8398dSJeff Roberson continue; 918ace8398dSJeff Roberson if ((fp = fget_locked(fdp, fd)) == NULL) { 919ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 920ace8398dSJeff Roberson return (EBADF); 921ace8398dSJeff Roberson } 922ace8398dSJeff Roberson for (msk = 0; msk < 3; msk++) { 923ace8398dSJeff Roberson if (ibits[msk] == NULL) 924ace8398dSJeff Roberson continue; 925ace8398dSJeff Roberson if ((ibits[msk][fd/NFDBITS] & 926ace8398dSJeff Roberson ((fd_mask) 1 << (fd % NFDBITS))) == 0) 927ace8398dSJeff Roberson continue; 928ace8398dSJeff Roberson if (fo_poll(fp, flag[msk], td->td_ucred, td)) { 929ace8398dSJeff Roberson obits[msk][(fd)/NFDBITS] |= 930ace8398dSJeff Roberson ((fd_mask)1 << ((fd) % NFDBITS)); 931ace8398dSJeff Roberson n++; 932ace8398dSJeff Roberson } 933ace8398dSJeff Roberson } 934ace8398dSJeff Roberson } 935ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 936ace8398dSJeff Roberson stp->st_flags = 0; 937ace8398dSJeff Roberson td->td_retval[0] = n; 938ace8398dSJeff Roberson return (0); 939ace8398dSJeff Roberson } 940ace8398dSJeff Roberson 941ace8398dSJeff Roberson /* 942ace8398dSJeff Roberson * Perform the initial filedescriptor scan and register ourselves with 943ace8398dSJeff Roberson * each selinfo. 944ace8398dSJeff Roberson */ 945265fc98fSSeigo Tanimura static int 946b40ce416SJulian Elischer selscan(td, ibits, obits, nfd) 947b40ce416SJulian Elischer struct thread *td; 948b08f7993SSujal Patel fd_mask **ibits, **obits; 949cb226aaaSPoul-Henning Kamp int nfd; 950df8bae1dSRodney W. Grimes { 951f082218cSPeter Wemm int msk, i, fd; 952f082218cSPeter Wemm fd_mask bits; 953df8bae1dSRodney W. Grimes struct file *fp; 954df8bae1dSRodney W. Grimes int n = 0; 9552087c896SBruce Evans /* Note: backend also returns POLLHUP/POLLERR if appropriate. */ 95642d11757SPeter Wemm static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND }; 957eb209311SAlfred Perlstein struct filedesc *fdp = td->td_proc->p_fd; 958df8bae1dSRodney W. Grimes 9595e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 960df8bae1dSRodney W. Grimes for (msk = 0; msk < 3; msk++) { 961d5e4d7e1SBruce Evans if (ibits[msk] == NULL) 962d5e4d7e1SBruce Evans continue; 963df8bae1dSRodney W. Grimes for (i = 0; i < nfd; i += NFDBITS) { 964b08f7993SSujal Patel bits = ibits[msk][i/NFDBITS]; 965f082218cSPeter Wemm /* ffs(int mask) not portable, fd_mask is long */ 966f082218cSPeter Wemm for (fd = i; bits && fd < nfd; fd++, bits >>= 1) { 967f082218cSPeter Wemm if (!(bits & 1)) 968f082218cSPeter Wemm continue; 969eb209311SAlfred Perlstein if ((fp = fget_locked(fdp, fd)) == NULL) { 9705e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 971df8bae1dSRodney W. Grimes return (EBADF); 972eb209311SAlfred Perlstein } 973ace8398dSJeff Roberson selfdalloc(td, (void *)(uintptr_t)fd); 974ea6027a8SRobert Watson if (fo_poll(fp, flag[msk], td->td_ucred, 975ea6027a8SRobert Watson td)) { 976b08f7993SSujal Patel obits[msk][(fd)/NFDBITS] |= 977f082218cSPeter Wemm ((fd_mask)1 << ((fd) % NFDBITS)); 978df8bae1dSRodney W. Grimes n++; 979df8bae1dSRodney W. Grimes } 980df8bae1dSRodney W. Grimes } 981df8bae1dSRodney W. Grimes } 982df8bae1dSRodney W. Grimes } 9835e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 984b40ce416SJulian Elischer td->td_retval[0] = n; 985df8bae1dSRodney W. Grimes return (0); 986df8bae1dSRodney W. Grimes } 987df8bae1dSRodney W. Grimes 98842d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 98942d11757SPeter Wemm struct poll_args { 99042d11757SPeter Wemm struct pollfd *fds; 99142d11757SPeter Wemm u_int nfds; 99242d11757SPeter Wemm int timeout; 99342d11757SPeter Wemm }; 99442d11757SPeter Wemm #endif 99542d11757SPeter Wemm int 996b40ce416SJulian Elischer poll(td, uap) 997b40ce416SJulian Elischer struct thread *td; 998ea0237edSJonathan Lemon struct poll_args *uap; 99942d11757SPeter Wemm { 10002580f4e5SAndre Oppermann struct pollfd *bits; 10012580f4e5SAndre Oppermann struct pollfd smallbits[32]; 100200af9731SPoul-Henning Kamp struct timeval atv, rtv, ttv; 10039ae6d334SKelly Yancey int error = 0, timo; 1004ace8398dSJeff Roberson u_int nfds; 100542d11757SPeter Wemm size_t ni; 100642d11757SPeter Wemm 1007d1e405c5SAlfred Perlstein nfds = uap->nfds; 1008ad2edad9SMatthew Dillon 10095d8dd01dSRobert Watson /* 10102bd5ac33SPeter Wemm * This is kinda bogus. We have fd limits, but that is not 10112bd5ac33SPeter Wemm * really related to the size of the pollfd array. Make sure 10122bd5ac33SPeter Wemm * we let the process use at least FD_SETSIZE entries and at 10132bd5ac33SPeter Wemm * least enough for the current limits. We want to be reasonably 10142bd5ac33SPeter Wemm * safe, but not overly restrictive. 101589b71647SPeter Wemm */ 101691d5354aSJohn Baldwin PROC_LOCK(td->td_proc); 101791d5354aSJohn Baldwin if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) && 1018b40ce416SJulian Elischer (nfds > FD_SETSIZE)) { 101991d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 1020ace8398dSJeff Roberson return (EINVAL); 1021ad2edad9SMatthew Dillon } 102291d5354aSJohn Baldwin PROC_UNLOCK(td->td_proc); 102389b71647SPeter Wemm ni = nfds * sizeof(struct pollfd); 102442d11757SPeter Wemm if (ni > sizeof(smallbits)) 1025a163d034SWarner Losh bits = malloc(ni, M_TEMP, M_WAITOK); 102642d11757SPeter Wemm else 102742d11757SPeter Wemm bits = smallbits; 1028d1e405c5SAlfred Perlstein error = copyin(uap->fds, bits, ni); 102942d11757SPeter Wemm if (error) 1030ace8398dSJeff Roberson goto done; 1031d1e405c5SAlfred Perlstein if (uap->timeout != INFTIM) { 1032d1e405c5SAlfred Perlstein atv.tv_sec = uap->timeout / 1000; 1033d1e405c5SAlfred Perlstein atv.tv_usec = (uap->timeout % 1000) * 1000; 103442d11757SPeter Wemm if (itimerfix(&atv)) { 103542d11757SPeter Wemm error = EINVAL; 1036ace8398dSJeff Roberson goto done; 103742d11757SPeter Wemm } 1038c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 103900af9731SPoul-Henning Kamp timevaladd(&atv, &rtv); 10409c386f6bSJohn Baldwin } else { 104100af9731SPoul-Henning Kamp atv.tv_sec = 0; 10429c386f6bSJohn Baldwin atv.tv_usec = 0; 10439c386f6bSJohn Baldwin } 104400af9731SPoul-Henning Kamp timo = 0; 1045ace8398dSJeff Roberson seltdinit(td); 1046ace8398dSJeff Roberson /* Iterate until the timeout expires or descriptors become ready. */ 1047ace8398dSJeff Roberson for (;;) { 10482580f4e5SAndre Oppermann error = pollscan(td, bits, nfds); 1049ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1050ace8398dSJeff Roberson break; 10514da144c0SJohn Baldwin if (atv.tv_sec || atv.tv_usec) { 1052c21410e1SPoul-Henning Kamp getmicrouptime(&rtv); 105385f190e4SAlfred Perlstein if (timevalcmp(&rtv, &atv, >=)) 1054ace8398dSJeff Roberson break; 105500af9731SPoul-Henning Kamp ttv = atv; 105600af9731SPoul-Henning Kamp timevalsub(&ttv, &rtv); 105700af9731SPoul-Henning Kamp timo = ttv.tv_sec > 24 * 60 * 60 ? 105800af9731SPoul-Henning Kamp 24 * 60 * 60 * hz : tvtohz(&ttv); 105942d11757SPeter Wemm } 1060ace8398dSJeff Roberson error = seltdwait(td, timo); 1061ace8398dSJeff Roberson if (error) 1062ace8398dSJeff Roberson break; 1063ace8398dSJeff Roberson error = pollrescan(td); 1064ace8398dSJeff Roberson if (error || td->td_retval[0] != 0) 1065ace8398dSJeff Roberson break; 106685f190e4SAlfred Perlstein } 1067ace8398dSJeff Roberson seltdclear(td); 1068265fc98fSSeigo Tanimura 106942d11757SPeter Wemm done: 107042d11757SPeter Wemm /* poll is not restarted after signals... */ 107142d11757SPeter Wemm if (error == ERESTART) 107242d11757SPeter Wemm error = EINTR; 107342d11757SPeter Wemm if (error == EWOULDBLOCK) 107442d11757SPeter Wemm error = 0; 107542d11757SPeter Wemm if (error == 0) { 1076d1e405c5SAlfred Perlstein error = copyout(bits, uap->fds, ni); 107742d11757SPeter Wemm if (error) 107842d11757SPeter Wemm goto out; 107942d11757SPeter Wemm } 108042d11757SPeter Wemm out: 108142d11757SPeter Wemm if (ni > sizeof(smallbits)) 108242d11757SPeter Wemm free(bits, M_TEMP); 108342d11757SPeter Wemm return (error); 108442d11757SPeter Wemm } 108542d11757SPeter Wemm 108642d11757SPeter Wemm static int 1087ace8398dSJeff Roberson pollrescan(struct thread *td) 1088ace8398dSJeff Roberson { 1089ace8398dSJeff Roberson struct seltd *stp; 1090ace8398dSJeff Roberson struct selfd *sfp; 1091ace8398dSJeff Roberson struct selfd *sfn; 1092ace8398dSJeff Roberson struct selinfo *si; 1093ace8398dSJeff Roberson struct filedesc *fdp; 1094ace8398dSJeff Roberson struct file *fp; 1095ace8398dSJeff Roberson struct pollfd *fd; 1096ace8398dSJeff Roberson int n; 1097ace8398dSJeff Roberson 1098ace8398dSJeff Roberson n = 0; 1099ace8398dSJeff Roberson fdp = td->td_proc->p_fd; 1100ace8398dSJeff Roberson stp = td->td_sel; 1101ace8398dSJeff Roberson FILEDESC_SLOCK(fdp); 1102ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) { 1103ace8398dSJeff Roberson fd = (struct pollfd *)sfp->sf_cookie; 1104ace8398dSJeff Roberson si = sfp->sf_si; 1105ace8398dSJeff Roberson selfdfree(stp, sfp); 1106ace8398dSJeff Roberson /* If the selinfo wasn't cleared the event didn't fire. */ 1107ace8398dSJeff Roberson if (si != NULL) 1108ace8398dSJeff Roberson continue; 1109ace8398dSJeff Roberson fp = fdp->fd_ofiles[fd->fd]; 1110ace8398dSJeff Roberson if (fp == NULL) { 1111ace8398dSJeff Roberson fd->revents = POLLNVAL; 1112ace8398dSJeff Roberson n++; 1113ace8398dSJeff Roberson continue; 1114ace8398dSJeff Roberson } 1115ace8398dSJeff Roberson /* 1116ace8398dSJeff Roberson * Note: backend also returns POLLHUP and 1117ace8398dSJeff Roberson * POLLERR if appropriate. 1118ace8398dSJeff Roberson */ 1119ace8398dSJeff Roberson fd->revents = fo_poll(fp, fd->events, td->td_ucred, td); 1120ace8398dSJeff Roberson if (fd->revents != 0) 1121ace8398dSJeff Roberson n++; 1122ace8398dSJeff Roberson } 1123ace8398dSJeff Roberson FILEDESC_SUNLOCK(fdp); 1124ace8398dSJeff Roberson stp->st_flags = 0; 1125ace8398dSJeff Roberson td->td_retval[0] = n; 1126ace8398dSJeff Roberson return (0); 1127ace8398dSJeff Roberson } 1128ace8398dSJeff Roberson 1129ace8398dSJeff Roberson 1130ace8398dSJeff Roberson static int 1131b40ce416SJulian Elischer pollscan(td, fds, nfd) 1132b40ce416SJulian Elischer struct thread *td; 113342d11757SPeter Wemm struct pollfd *fds; 1134ea0237edSJonathan Lemon u_int nfd; 113542d11757SPeter Wemm { 1136ace8398dSJeff Roberson struct filedesc *fdp = td->td_proc->p_fd; 113742d11757SPeter Wemm int i; 113842d11757SPeter Wemm struct file *fp; 113942d11757SPeter Wemm int n = 0; 114042d11757SPeter Wemm 11415e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 1142eb209311SAlfred Perlstein for (i = 0; i < nfd; i++, fds++) { 1143337c9691SJordan K. Hubbard if (fds->fd >= fdp->fd_nfiles) { 114442d11757SPeter Wemm fds->revents = POLLNVAL; 114542d11757SPeter Wemm n++; 1146337c9691SJordan K. Hubbard } else if (fds->fd < 0) { 1147337c9691SJordan K. Hubbard fds->revents = 0; 114842d11757SPeter Wemm } else { 114942d11757SPeter Wemm fp = fdp->fd_ofiles[fds->fd]; 1150279d7226SMatthew Dillon if (fp == NULL) { 115142d11757SPeter Wemm fds->revents = POLLNVAL; 115242d11757SPeter Wemm n++; 115342d11757SPeter Wemm } else { 11542087c896SBruce Evans /* 11552087c896SBruce Evans * Note: backend also returns POLLHUP and 11562087c896SBruce Evans * POLLERR if appropriate. 11572087c896SBruce Evans */ 1158ace8398dSJeff Roberson selfdalloc(td, fds); 115913ccadd4SBrian Feldman fds->revents = fo_poll(fp, fds->events, 1160ea6027a8SRobert Watson td->td_ucred, td); 116142d11757SPeter Wemm if (fds->revents != 0) 116242d11757SPeter Wemm n++; 116342d11757SPeter Wemm } 116442d11757SPeter Wemm } 116542d11757SPeter Wemm } 11665e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 1167b40ce416SJulian Elischer td->td_retval[0] = n; 116842d11757SPeter Wemm return (0); 116942d11757SPeter Wemm } 117042d11757SPeter Wemm 117142d11757SPeter Wemm /* 117242d11757SPeter Wemm * OpenBSD poll system call. 11730c14ff0eSRobert Watson * 117442d11757SPeter Wemm * XXX this isn't quite a true representation.. OpenBSD uses select ops. 117542d11757SPeter Wemm */ 117642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_ 117742d11757SPeter Wemm struct openbsd_poll_args { 117842d11757SPeter Wemm struct pollfd *fds; 117942d11757SPeter Wemm u_int nfds; 118042d11757SPeter Wemm int timeout; 118142d11757SPeter Wemm }; 118242d11757SPeter Wemm #endif 118342d11757SPeter Wemm int 1184b40ce416SJulian Elischer openbsd_poll(td, uap) 1185b40ce416SJulian Elischer register struct thread *td; 118642d11757SPeter Wemm register struct openbsd_poll_args *uap; 118742d11757SPeter Wemm { 1188b40ce416SJulian Elischer return (poll(td, (struct poll_args *)uap)); 118942d11757SPeter Wemm } 119042d11757SPeter Wemm 119185f190e4SAlfred Perlstein /* 1192ace8398dSJeff Roberson * XXX This was created specifically to support netncp and netsmb. This 1193ace8398dSJeff Roberson * allows the caller to specify a socket to wait for events on. It returns 1194ace8398dSJeff Roberson * 0 if any events matched and an error otherwise. There is no way to 1195ace8398dSJeff Roberson * determine which events fired. 119685f190e4SAlfred Perlstein */ 1197ace8398dSJeff Roberson int 1198ace8398dSJeff Roberson selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td) 119985f190e4SAlfred Perlstein { 1200ace8398dSJeff Roberson struct timeval atv, rtv, ttv; 1201ace8398dSJeff Roberson int error, timo; 120285f190e4SAlfred Perlstein 1203ace8398dSJeff Roberson if (tvp != NULL) { 1204ace8398dSJeff Roberson atv = *tvp; 1205ace8398dSJeff Roberson if (itimerfix(&atv)) 1206ace8398dSJeff Roberson return (EINVAL); 1207ace8398dSJeff Roberson getmicrouptime(&rtv); 1208ace8398dSJeff Roberson timevaladd(&atv, &rtv); 1209ace8398dSJeff Roberson } else { 1210ace8398dSJeff Roberson atv.tv_sec = 0; 1211ace8398dSJeff Roberson atv.tv_usec = 0; 1212ace8398dSJeff Roberson } 1213ace8398dSJeff Roberson 1214ace8398dSJeff Roberson timo = 0; 1215ace8398dSJeff Roberson seltdinit(td); 1216ace8398dSJeff Roberson /* 1217ace8398dSJeff Roberson * Iterate until the timeout expires or the socket becomes ready. 1218ace8398dSJeff Roberson */ 1219ace8398dSJeff Roberson for (;;) { 1220ace8398dSJeff Roberson selfdalloc(td, NULL); 1221ace8398dSJeff Roberson error = sopoll(so, events, NULL, td); 1222ace8398dSJeff Roberson /* error here is actually the ready events. */ 1223ace8398dSJeff Roberson if (error) 1224ace8398dSJeff Roberson return (0); 1225ace8398dSJeff Roberson if (atv.tv_sec || atv.tv_usec) { 1226ace8398dSJeff Roberson getmicrouptime(&rtv); 1227ace8398dSJeff Roberson if (timevalcmp(&rtv, &atv, >=)) { 1228ace8398dSJeff Roberson seltdclear(td); 1229ace8398dSJeff Roberson return (EWOULDBLOCK); 1230ace8398dSJeff Roberson } 1231ace8398dSJeff Roberson ttv = atv; 1232ace8398dSJeff Roberson timevalsub(&ttv, &rtv); 1233ace8398dSJeff Roberson timo = ttv.tv_sec > 24 * 60 * 60 ? 1234ace8398dSJeff Roberson 24 * 60 * 60 * hz : tvtohz(&ttv); 1235ace8398dSJeff Roberson } 1236ace8398dSJeff Roberson error = seltdwait(td, timo); 1237ace8398dSJeff Roberson seltdclear(td); 1238ace8398dSJeff Roberson if (error) 1239ace8398dSJeff Roberson break; 1240ace8398dSJeff Roberson } 1241ace8398dSJeff Roberson /* XXX Duplicates ncp/smb behavior. */ 1242ace8398dSJeff Roberson if (error == ERESTART) 1243ace8398dSJeff Roberson error = 0; 1244ace8398dSJeff Roberson return (error); 1245ace8398dSJeff Roberson } 1246ace8398dSJeff Roberson 1247ace8398dSJeff Roberson /* 1248ace8398dSJeff Roberson * Preallocate two selfds associated with 'cookie'. Some fo_poll routines 1249ace8398dSJeff Roberson * have two select sets, one for read and another for write. 1250ace8398dSJeff Roberson */ 1251ace8398dSJeff Roberson static void 1252ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie) 1253ace8398dSJeff Roberson { 1254ace8398dSJeff Roberson struct seltd *stp; 1255ace8398dSJeff Roberson 1256ace8398dSJeff Roberson stp = td->td_sel; 1257ace8398dSJeff Roberson if (stp->st_free1 == NULL) 1258ace8398dSJeff Roberson stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1259ace8398dSJeff Roberson stp->st_free1->sf_td = stp; 1260ace8398dSJeff Roberson stp->st_free1->sf_cookie = cookie; 1261ace8398dSJeff Roberson if (stp->st_free2 == NULL) 1262ace8398dSJeff Roberson stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO); 1263ace8398dSJeff Roberson stp->st_free2->sf_td = stp; 1264ace8398dSJeff Roberson stp->st_free2->sf_cookie = cookie; 1265ace8398dSJeff Roberson } 1266ace8398dSJeff Roberson 1267ace8398dSJeff Roberson static void 1268ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp) 1269ace8398dSJeff Roberson { 1270ace8398dSJeff Roberson STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); 1271ace8398dSJeff Roberson mtx_lock(sfp->sf_mtx); 1272ace8398dSJeff Roberson if (sfp->sf_si) 1273ace8398dSJeff Roberson TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); 1274ace8398dSJeff Roberson mtx_unlock(sfp->sf_mtx); 1275ace8398dSJeff Roberson uma_zfree(selfd_zone, sfp); 127685f190e4SAlfred Perlstein } 127785f190e4SAlfred Perlstein 1278df8bae1dSRodney W. Grimes /* 1279df8bae1dSRodney W. Grimes * Record a select request. 1280df8bae1dSRodney W. Grimes */ 1281df8bae1dSRodney W. Grimes void 1282df8bae1dSRodney W. Grimes selrecord(selector, sip) 1283b40ce416SJulian Elischer struct thread *selector; 1284df8bae1dSRodney W. Grimes struct selinfo *sip; 1285df8bae1dSRodney W. Grimes { 1286ace8398dSJeff Roberson struct selfd *sfp; 1287ace8398dSJeff Roberson struct seltd *stp; 1288ace8398dSJeff Roberson struct mtx *mtxp; 1289df8bae1dSRodney W. Grimes 1290ace8398dSJeff Roberson stp = selector->td_sel; 129185f190e4SAlfred Perlstein /* 1292ace8398dSJeff Roberson * Don't record when doing a rescan. 129385f190e4SAlfred Perlstein */ 1294ace8398dSJeff Roberson if (stp->st_flags & SELTD_RESCAN) 1295ace8398dSJeff Roberson return; 1296ace8398dSJeff Roberson /* 1297ace8398dSJeff Roberson * Grab one of the preallocated descriptors. 1298ace8398dSJeff Roberson */ 1299ace8398dSJeff Roberson sfp = NULL; 1300ace8398dSJeff Roberson if ((sfp = stp->st_free1) != NULL) 1301ace8398dSJeff Roberson stp->st_free1 = NULL; 1302ace8398dSJeff Roberson else if ((sfp = stp->st_free2) != NULL) 1303ace8398dSJeff Roberson stp->st_free2 = NULL; 1304ace8398dSJeff Roberson else 1305ace8398dSJeff Roberson panic("selrecord: No free selfd on selq"); 1306ace8398dSJeff Roberson mtxp = mtx_pool_find(mtxpool_sleep, sip); 1307ace8398dSJeff Roberson /* 1308ace8398dSJeff Roberson * Initialize the sfp and queue it in the thread. 1309ace8398dSJeff Roberson */ 1310ace8398dSJeff Roberson sfp->sf_si = sip; 1311ace8398dSJeff Roberson sfp->sf_mtx = mtxp; 1312ace8398dSJeff Roberson STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); 1313ace8398dSJeff Roberson /* 1314ace8398dSJeff Roberson * Now that we've locked the sip, check for initialization. 1315ace8398dSJeff Roberson */ 1316ace8398dSJeff Roberson mtx_lock(mtxp); 1317ace8398dSJeff Roberson if (sip->si_mtx == NULL) { 1318ace8398dSJeff Roberson sip->si_mtx = mtxp; 1319ace8398dSJeff Roberson TAILQ_INIT(&sip->si_tdlist); 132085f190e4SAlfred Perlstein } 1321ace8398dSJeff Roberson /* 1322ace8398dSJeff Roberson * Add this thread to the list of selfds listening on this selinfo. 1323ace8398dSJeff Roberson */ 1324ace8398dSJeff Roberson TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads); 1325ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1326df8bae1dSRodney W. Grimes } 1327df8bae1dSRodney W. Grimes 1328512824f8SSeigo Tanimura /* Wake up a selecting thread. */ 1329df8bae1dSRodney W. Grimes void 1330df8bae1dSRodney W. Grimes selwakeup(sip) 133185f190e4SAlfred Perlstein struct selinfo *sip; 1332df8bae1dSRodney W. Grimes { 1333512824f8SSeigo Tanimura doselwakeup(sip, -1); 1334512824f8SSeigo Tanimura } 1335512824f8SSeigo Tanimura 1336512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */ 1337512824f8SSeigo Tanimura void 1338512824f8SSeigo Tanimura selwakeuppri(sip, pri) 1339512824f8SSeigo Tanimura struct selinfo *sip; 1340512824f8SSeigo Tanimura int pri; 1341512824f8SSeigo Tanimura { 1342512824f8SSeigo Tanimura doselwakeup(sip, pri); 1343512824f8SSeigo Tanimura } 1344512824f8SSeigo Tanimura 1345512824f8SSeigo Tanimura /* 1346512824f8SSeigo Tanimura * Do a wakeup when a selectable event occurs. 1347512824f8SSeigo Tanimura */ 1348512824f8SSeigo Tanimura static void 1349512824f8SSeigo Tanimura doselwakeup(sip, pri) 1350512824f8SSeigo Tanimura struct selinfo *sip; 1351512824f8SSeigo Tanimura int pri; 1352512824f8SSeigo Tanimura { 1353ace8398dSJeff Roberson struct selfd *sfp; 1354ace8398dSJeff Roberson struct selfd *sfn; 1355ace8398dSJeff Roberson struct seltd *stp; 1356df8bae1dSRodney W. Grimes 1357ace8398dSJeff Roberson /* If it's not initialized there can't be any waiters. */ 1358ace8398dSJeff Roberson if (sip->si_mtx == NULL) 1359b40ce416SJulian Elischer return; 1360ace8398dSJeff Roberson /* 1361ace8398dSJeff Roberson * Locking the selinfo locks all selfds associated with it. 1362ace8398dSJeff Roberson */ 1363ace8398dSJeff Roberson mtx_lock(sip->si_mtx); 1364ace8398dSJeff Roberson TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) { 1365ace8398dSJeff Roberson /* 1366ace8398dSJeff Roberson * Once we remove this sfp from the list and clear the 1367ace8398dSJeff Roberson * sf_si seltdclear will know to ignore this si. 1368ace8398dSJeff Roberson */ 1369ace8398dSJeff Roberson TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); 1370ace8398dSJeff Roberson sfp->sf_si = NULL; 1371ace8398dSJeff Roberson stp = sfp->sf_td; 1372ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1373ace8398dSJeff Roberson stp->st_flags |= SELTD_PENDING; 1374ace8398dSJeff Roberson cv_broadcastpri(&stp->st_wait, pri); 1375ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1376b40ce416SJulian Elischer } 1377ace8398dSJeff Roberson mtx_unlock(sip->si_mtx); 1378ace8398dSJeff Roberson } 1379ace8398dSJeff Roberson 1380ace8398dSJeff Roberson static void 1381ace8398dSJeff Roberson seltdinit(struct thread *td) 1382ace8398dSJeff Roberson { 1383ace8398dSJeff Roberson struct seltd *stp; 1384ace8398dSJeff Roberson 1385ace8398dSJeff Roberson if ((stp = td->td_sel) != NULL) 1386ace8398dSJeff Roberson goto out; 1387ace8398dSJeff Roberson td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO); 1388ace8398dSJeff Roberson mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF); 1389ace8398dSJeff Roberson cv_init(&stp->st_wait, "select"); 1390ace8398dSJeff Roberson out: 1391ace8398dSJeff Roberson stp->st_flags = 0; 1392ace8398dSJeff Roberson STAILQ_INIT(&stp->st_selq); 1393ace8398dSJeff Roberson } 1394ace8398dSJeff Roberson 1395ace8398dSJeff Roberson static int 1396ace8398dSJeff Roberson seltdwait(struct thread *td, int timo) 1397ace8398dSJeff Roberson { 1398ace8398dSJeff Roberson struct seltd *stp; 1399ace8398dSJeff Roberson int error; 1400ace8398dSJeff Roberson 1401ace8398dSJeff Roberson stp = td->td_sel; 1402ace8398dSJeff Roberson /* 1403ace8398dSJeff Roberson * An event of interest may occur while we do not hold the seltd 1404ace8398dSJeff Roberson * locked so check the pending flag before we sleep. 1405ace8398dSJeff Roberson */ 1406ace8398dSJeff Roberson mtx_lock(&stp->st_mtx); 1407ace8398dSJeff Roberson /* 1408ace8398dSJeff Roberson * Any further calls to selrecord will be a rescan. 1409ace8398dSJeff Roberson */ 1410ace8398dSJeff Roberson stp->st_flags |= SELTD_RESCAN; 1411ace8398dSJeff Roberson if (stp->st_flags & SELTD_PENDING) { 1412ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1413ace8398dSJeff Roberson return (0); 1414ace8398dSJeff Roberson } 1415ace8398dSJeff Roberson if (timo > 0) 1416ace8398dSJeff Roberson error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo); 1417ace8398dSJeff Roberson else 1418ace8398dSJeff Roberson error = cv_wait_sig(&stp->st_wait, &stp->st_mtx); 1419ace8398dSJeff Roberson mtx_unlock(&stp->st_mtx); 1420ace8398dSJeff Roberson 1421ace8398dSJeff Roberson return (error); 1422ace8398dSJeff Roberson } 1423ace8398dSJeff Roberson 1424ace8398dSJeff Roberson void 1425ace8398dSJeff Roberson seltdfini(struct thread *td) 1426ace8398dSJeff Roberson { 1427ace8398dSJeff Roberson struct seltd *stp; 1428ace8398dSJeff Roberson 1429ace8398dSJeff Roberson stp = td->td_sel; 1430ace8398dSJeff Roberson if (stp == NULL) 1431ace8398dSJeff Roberson return; 1432ace8398dSJeff Roberson if (stp->st_free1) 1433ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free1); 1434ace8398dSJeff Roberson if (stp->st_free2) 1435ace8398dSJeff Roberson uma_zfree(selfd_zone, stp->st_free2); 1436ace8398dSJeff Roberson td->td_sel = NULL; 1437ace8398dSJeff Roberson free(stp, M_SELECT); 1438ace8398dSJeff Roberson } 1439ace8398dSJeff Roberson 1440ace8398dSJeff Roberson /* 1441ace8398dSJeff Roberson * Remove the references to the thread from all of the objects we were 1442ace8398dSJeff Roberson * polling. 1443ace8398dSJeff Roberson */ 1444ace8398dSJeff Roberson static void 1445ace8398dSJeff Roberson seltdclear(struct thread *td) 1446ace8398dSJeff Roberson { 1447ace8398dSJeff Roberson struct seltd *stp; 1448ace8398dSJeff Roberson struct selfd *sfp; 1449ace8398dSJeff Roberson struct selfd *sfn; 1450ace8398dSJeff Roberson 1451ace8398dSJeff Roberson stp = td->td_sel; 1452ace8398dSJeff Roberson STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) 1453ace8398dSJeff Roberson selfdfree(stp, sfp); 1454ace8398dSJeff Roberson stp->st_flags = 0; 1455df8bae1dSRodney W. Grimes } 1456265fc98fSSeigo Tanimura 14574d77a549SAlfred Perlstein static void selectinit(void *); 1458ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL); 1459265fc98fSSeigo Tanimura static void 1460ace8398dSJeff Roberson selectinit(void *dummy __unused) 1461265fc98fSSeigo Tanimura { 1462ace8398dSJeff Roberson selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL, 1463ace8398dSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 1464265fc98fSSeigo Tanimura } 1465