The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/sys_generic.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1982, 1986, 1989, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  * (c) UNIX System Laboratories, Inc.
    7  * All or some portions of this file are derived from material licensed
    8  * to the University of California by American Telephone and Telegraph
    9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   10  * the permission of UNIX System Laboratories, Inc.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)sys_generic.c       8.5 (Berkeley) 1/21/94
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __FBSDID("$FreeBSD: releng/12.0/sys/kern/sys_generic.c 338855 2018-09-21 13:20:41Z mjg $");
   41 
   42 #include "opt_capsicum.h"
   43 #include "opt_ktrace.h"
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/sysproto.h>
   48 #include <sys/capsicum.h>
   49 #include <sys/filedesc.h>
   50 #include <sys/filio.h>
   51 #include <sys/fcntl.h>
   52 #include <sys/file.h>
   53 #include <sys/lock.h>
   54 #include <sys/proc.h>
   55 #include <sys/signalvar.h>
   56 #include <sys/socketvar.h>
   57 #include <sys/uio.h>
   58 #include <sys/kernel.h>
   59 #include <sys/ktr.h>
   60 #include <sys/limits.h>
   61 #include <sys/malloc.h>
   62 #include <sys/poll.h>
   63 #include <sys/resourcevar.h>
   64 #include <sys/selinfo.h>
   65 #include <sys/sleepqueue.h>
   66 #include <sys/syscallsubr.h>
   67 #include <sys/sysctl.h>
   68 #include <sys/sysent.h>
   69 #include <sys/vnode.h>
   70 #include <sys/bio.h>
   71 #include <sys/buf.h>
   72 #include <sys/condvar.h>
   73 #ifdef KTRACE
   74 #include <sys/ktrace.h>
   75 #endif
   76 
   77 #include <security/audit/audit.h>
   78 
   79 /*
   80  * The following macro defines how many bytes will be allocated from
   81  * the stack instead of memory allocated when passing the IOCTL data
   82  * structures from userspace and to the kernel. Some IOCTLs having
   83  * small data structures are used very frequently and this small
   84  * buffer on the stack gives a significant speedup improvement for
   85  * those requests. The value of this define should be greater or equal
   86  * to 64 bytes and should also be power of two. The data structure is
   87  * currently hard-aligned to a 8-byte boundary on the stack. This
   88  * should currently be sufficient for all supported platforms.
   89  */
   90 #define SYS_IOCTL_SMALL_SIZE    128     /* bytes */
   91 #define SYS_IOCTL_SMALL_ALIGN   8       /* bytes */
   92 
   93 #ifdef __LP64__
   94 static int iosize_max_clamp = 0;
   95 SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
   96     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
   97 static int devfs_iosize_max_clamp = 1;
   98 SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
   99     &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices");
  100 #endif
  101 
  102 /*
  103  * Assert that the return value of read(2) and write(2) syscalls fits
  104  * into a register.  If not, an architecture will need to provide the
  105  * usermode wrappers to reconstruct the result.
  106  */
  107 CTASSERT(sizeof(register_t) >= sizeof(size_t));
  108 
  109 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
  110 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
  111 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
  112 
  113 static int      pollout(struct thread *, struct pollfd *, struct pollfd *,
  114                     u_int);
  115 static int      pollscan(struct thread *, struct pollfd *, u_int);
  116 static int      pollrescan(struct thread *);
  117 static int      selscan(struct thread *, fd_mask **, fd_mask **, int);
  118 static int      selrescan(struct thread *, fd_mask **, fd_mask **);
  119 static void     selfdalloc(struct thread *, void *);
  120 static void     selfdfree(struct seltd *, struct selfd *);
  121 static int      dofileread(struct thread *, int, struct file *, struct uio *,
  122                     off_t, int);
  123 static int      dofilewrite(struct thread *, int, struct file *, struct uio *,
  124                     off_t, int);
  125 static void     doselwakeup(struct selinfo *, int);
  126 static void     seltdinit(struct thread *);
  127 static int      seltdwait(struct thread *, sbintime_t, sbintime_t);
  128 static void     seltdclear(struct thread *);
  129 
  130 /*
  131  * One seltd per-thread allocated on demand as needed.
  132  *
  133  *      t - protected by st_mtx
  134  *      k - Only accessed by curthread or read-only
  135  */
  136 struct seltd {
  137         STAILQ_HEAD(, selfd)    st_selq;        /* (k) List of selfds. */
  138         struct selfd            *st_free1;      /* (k) free fd for read set. */
  139         struct selfd            *st_free2;      /* (k) free fd for write set. */
  140         struct mtx              st_mtx;         /* Protects struct seltd */
  141         struct cv               st_wait;        /* (t) Wait channel. */
  142         int                     st_flags;       /* (t) SELTD_ flags. */
  143 };
  144 
  145 #define SELTD_PENDING   0x0001                  /* We have pending events. */
  146 #define SELTD_RESCAN    0x0002                  /* Doing a rescan. */
  147 
  148 /*
  149  * One selfd allocated per-thread per-file-descriptor.
  150  *      f - protected by sf_mtx
  151  */
  152 struct selfd {
  153         STAILQ_ENTRY(selfd)     sf_link;        /* (k) fds owned by this td. */
  154         TAILQ_ENTRY(selfd)      sf_threads;     /* (f) fds on this selinfo. */
  155         struct selinfo          *sf_si;         /* (f) selinfo when linked. */
  156         struct mtx              *sf_mtx;        /* Pointer to selinfo mtx. */
  157         struct seltd            *sf_td;         /* (k) owning seltd. */
  158         void                    *sf_cookie;     /* (k) fd or pollfd. */
  159         u_int                   sf_refs;
  160 };
  161 
  162 static uma_zone_t selfd_zone;
  163 static struct mtx_pool *mtxpool_select;
  164 
  165 #ifdef __LP64__
  166 size_t
  167 devfs_iosize_max(void)
  168 {
  169 
  170         return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
  171             INT_MAX : SSIZE_MAX);
  172 }
  173 
  174 size_t
  175 iosize_max(void)
  176 {
  177 
  178         return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
  179             INT_MAX : SSIZE_MAX);
  180 }
  181 #endif
  182 
  183 #ifndef _SYS_SYSPROTO_H_
  184 struct read_args {
  185         int     fd;
  186         void    *buf;
  187         size_t  nbyte;
  188 };
  189 #endif
  190 int
  191 sys_read(struct thread *td, struct read_args *uap)
  192 {
  193         struct uio auio;
  194         struct iovec aiov;
  195         int error;
  196 
  197         if (uap->nbyte > IOSIZE_MAX)
  198                 return (EINVAL);
  199         aiov.iov_base = uap->buf;
  200         aiov.iov_len = uap->nbyte;
  201         auio.uio_iov = &aiov;
  202         auio.uio_iovcnt = 1;
  203         auio.uio_resid = uap->nbyte;
  204         auio.uio_segflg = UIO_USERSPACE;
  205         error = kern_readv(td, uap->fd, &auio);
  206         return (error);
  207 }
  208 
  209 /*
  210  * Positioned read system call
  211  */
  212 #ifndef _SYS_SYSPROTO_H_
  213 struct pread_args {
  214         int     fd;
  215         void    *buf;
  216         size_t  nbyte;
  217         int     pad;
  218         off_t   offset;
  219 };
  220 #endif
  221 int
  222 sys_pread(struct thread *td, struct pread_args *uap)
  223 {
  224 
  225         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
  226 }
  227 
  228 int
  229 kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
  230 {
  231         struct uio auio;
  232         struct iovec aiov;
  233         int error;
  234 
  235         if (nbyte > IOSIZE_MAX)
  236                 return (EINVAL);
  237         aiov.iov_base = buf;
  238         aiov.iov_len = nbyte;
  239         auio.uio_iov = &aiov;
  240         auio.uio_iovcnt = 1;
  241         auio.uio_resid = nbyte;
  242         auio.uio_segflg = UIO_USERSPACE;
  243         error = kern_preadv(td, fd, &auio, offset);
  244         return (error);
  245 }
  246 
  247 #if defined(COMPAT_FREEBSD6)
  248 int
  249 freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap)
  250 {
  251 
  252         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
  253 }
  254 #endif
  255 
  256 /*
  257  * Scatter read system call.
  258  */
  259 #ifndef _SYS_SYSPROTO_H_
  260 struct readv_args {
  261         int     fd;
  262         struct  iovec *iovp;
  263         u_int   iovcnt;
  264 };
  265 #endif
  266 int
  267 sys_readv(struct thread *td, struct readv_args *uap)
  268 {
  269         struct uio *auio;
  270         int error;
  271 
  272         error = copyinuio(uap->iovp, uap->iovcnt, &auio);
  273         if (error)
  274                 return (error);
  275         error = kern_readv(td, uap->fd, auio);
  276         free(auio, M_IOV);
  277         return (error);
  278 }
  279 
  280 int
  281 kern_readv(struct thread *td, int fd, struct uio *auio)
  282 {
  283         struct file *fp;
  284         int error;
  285 
  286         error = fget_read(td, fd, &cap_read_rights, &fp);
  287         if (error)
  288                 return (error);
  289         error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
  290         fdrop(fp, td);
  291         return (error);
  292 }
  293 
  294 /*
  295  * Scatter positioned read system call.
  296  */
  297 #ifndef _SYS_SYSPROTO_H_
  298 struct preadv_args {
  299         int     fd;
  300         struct  iovec *iovp;
  301         u_int   iovcnt;
  302         off_t   offset;
  303 };
  304 #endif
  305 int
  306 sys_preadv(struct thread *td, struct preadv_args *uap)
  307 {
  308         struct uio *auio;
  309         int error;
  310 
  311         error = copyinuio(uap->iovp, uap->iovcnt, &auio);
  312         if (error)
  313                 return (error);
  314         error = kern_preadv(td, uap->fd, auio, uap->offset);
  315         free(auio, M_IOV);
  316         return (error);
  317 }
  318 
  319 int
  320 kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset)
  321 {
  322         struct file *fp;
  323         int error;
  324 
  325         error = fget_read(td, fd, &cap_pread_rights, &fp);
  326         if (error)
  327                 return (error);
  328         if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
  329                 error = ESPIPE;
  330         else if (offset < 0 &&
  331             (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
  332                 error = EINVAL;
  333         else
  334                 error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
  335         fdrop(fp, td);
  336         return (error);
  337 }
  338 
  339 /*
  340  * Common code for readv and preadv that reads data in
  341  * from a file using the passed in uio, offset, and flags.
  342  */
  343 static int
  344 dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio,
  345     off_t offset, int flags)
  346 {
  347         ssize_t cnt;
  348         int error;
  349 #ifdef KTRACE
  350         struct uio *ktruio = NULL;
  351 #endif
  352 
  353         AUDIT_ARG_FD(fd);
  354 
  355         /* Finish zero length reads right here */
  356         if (auio->uio_resid == 0) {
  357                 td->td_retval[0] = 0;
  358                 return (0);
  359         }
  360         auio->uio_rw = UIO_READ;
  361         auio->uio_offset = offset;
  362         auio->uio_td = td;
  363 #ifdef KTRACE
  364         if (KTRPOINT(td, KTR_GENIO)) 
  365                 ktruio = cloneuio(auio);
  366 #endif
  367         cnt = auio->uio_resid;
  368         if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
  369                 if (auio->uio_resid != cnt && (error == ERESTART ||
  370                     error == EINTR || error == EWOULDBLOCK))
  371                         error = 0;
  372         }
  373         cnt -= auio->uio_resid;
  374 #ifdef KTRACE
  375         if (ktruio != NULL) {
  376                 ktruio->uio_resid = cnt;
  377                 ktrgenio(fd, UIO_READ, ktruio, error);
  378         }
  379 #endif
  380         td->td_retval[0] = cnt;
  381         return (error);
  382 }
  383 
  384 #ifndef _SYS_SYSPROTO_H_
  385 struct write_args {
  386         int     fd;
  387         const void *buf;
  388         size_t  nbyte;
  389 };
  390 #endif
  391 int
  392 sys_write(struct thread *td, struct write_args *uap)
  393 {
  394         struct uio auio;
  395         struct iovec aiov;
  396         int error;
  397 
  398         if (uap->nbyte > IOSIZE_MAX)
  399                 return (EINVAL);
  400         aiov.iov_base = (void *)(uintptr_t)uap->buf;
  401         aiov.iov_len = uap->nbyte;
  402         auio.uio_iov = &aiov;
  403         auio.uio_iovcnt = 1;
  404         auio.uio_resid = uap->nbyte;
  405         auio.uio_segflg = UIO_USERSPACE;
  406         error = kern_writev(td, uap->fd, &auio);
  407         return (error);
  408 }
  409 
  410 /*
  411  * Positioned write system call.
  412  */
  413 #ifndef _SYS_SYSPROTO_H_
  414 struct pwrite_args {
  415         int     fd;
  416         const void *buf;
  417         size_t  nbyte;
  418         int     pad;
  419         off_t   offset;
  420 };
  421 #endif
  422 int
  423 sys_pwrite(struct thread *td, struct pwrite_args *uap)
  424 {
  425 
  426         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
  427 }
  428 
  429 int
  430 kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte,
  431     off_t offset)
  432 {
  433         struct uio auio;
  434         struct iovec aiov;
  435         int error;
  436 
  437         if (nbyte > IOSIZE_MAX)
  438                 return (EINVAL);
  439         aiov.iov_base = (void *)(uintptr_t)buf;
  440         aiov.iov_len = nbyte;
  441         auio.uio_iov = &aiov;
  442         auio.uio_iovcnt = 1;
  443         auio.uio_resid = nbyte;
  444         auio.uio_segflg = UIO_USERSPACE;
  445         error = kern_pwritev(td, fd, &auio, offset);
  446         return (error);
  447 }
  448 
  449 #if defined(COMPAT_FREEBSD6)
  450 int
  451 freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap)
  452 {
  453 
  454         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
  455 }
  456 #endif
  457 
  458 /*
  459  * Gather write system call.
  460  */
  461 #ifndef _SYS_SYSPROTO_H_
  462 struct writev_args {
  463         int     fd;
  464         struct  iovec *iovp;
  465         u_int   iovcnt;
  466 };
  467 #endif
  468 int
  469 sys_writev(struct thread *td, struct writev_args *uap)
  470 {
  471         struct uio *auio;
  472         int error;
  473 
  474         error = copyinuio(uap->iovp, uap->iovcnt, &auio);
  475         if (error)
  476                 return (error);
  477         error = kern_writev(td, uap->fd, auio);
  478         free(auio, M_IOV);
  479         return (error);
  480 }
  481 
  482 int
  483 kern_writev(struct thread *td, int fd, struct uio *auio)
  484 {
  485         struct file *fp;
  486         int error;
  487 
  488         error = fget_write(td, fd, &cap_write_rights, &fp);
  489         if (error)
  490                 return (error);
  491         error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
  492         fdrop(fp, td);
  493         return (error);
  494 }
  495 
  496 /*
  497  * Gather positioned write system call.
  498  */
  499 #ifndef _SYS_SYSPROTO_H_
  500 struct pwritev_args {
  501         int     fd;
  502         struct  iovec *iovp;
  503         u_int   iovcnt;
  504         off_t   offset;
  505 };
  506 #endif
  507 int
  508 sys_pwritev(struct thread *td, struct pwritev_args *uap)
  509 {
  510         struct uio *auio;
  511         int error;
  512 
  513         error = copyinuio(uap->iovp, uap->iovcnt, &auio);
  514         if (error)
  515                 return (error);
  516         error = kern_pwritev(td, uap->fd, auio, uap->offset);
  517         free(auio, M_IOV);
  518         return (error);
  519 }
  520 
  521 int
  522 kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset)
  523 {
  524         struct file *fp;
  525         int error;
  526 
  527         error = fget_write(td, fd, &cap_pwrite_rights, &fp);
  528         if (error)
  529                 return (error);
  530         if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
  531                 error = ESPIPE;
  532         else if (offset < 0 &&
  533             (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
  534                 error = EINVAL;
  535         else
  536                 error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
  537         fdrop(fp, td);
  538         return (error);
  539 }
  540 
  541 /*
  542  * Common code for writev and pwritev that writes data to
  543  * a file using the passed in uio, offset, and flags.
  544  */
  545 static int
  546 dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio,
  547     off_t offset, int flags)
  548 {
  549         ssize_t cnt;
  550         int error;
  551 #ifdef KTRACE
  552         struct uio *ktruio = NULL;
  553 #endif
  554 
  555         AUDIT_ARG_FD(fd);
  556         auio->uio_rw = UIO_WRITE;
  557         auio->uio_td = td;
  558         auio->uio_offset = offset;
  559 #ifdef KTRACE
  560         if (KTRPOINT(td, KTR_GENIO))
  561                 ktruio = cloneuio(auio);
  562 #endif
  563         cnt = auio->uio_resid;
  564         if (fp->f_type == DTYPE_VNODE &&
  565             (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
  566                 bwillwrite();
  567         if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
  568                 if (auio->uio_resid != cnt && (error == ERESTART ||
  569                     error == EINTR || error == EWOULDBLOCK))
  570                         error = 0;
  571                 /* Socket layer is responsible for issuing SIGPIPE. */
  572                 if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
  573                         PROC_LOCK(td->td_proc);
  574                         tdsignal(td, SIGPIPE);
  575                         PROC_UNLOCK(td->td_proc);
  576                 }
  577         }
  578         cnt -= auio->uio_resid;
  579 #ifdef KTRACE
  580         if (ktruio != NULL) {
  581                 ktruio->uio_resid = cnt;
  582                 ktrgenio(fd, UIO_WRITE, ktruio, error);
  583         }
  584 #endif
  585         td->td_retval[0] = cnt;
  586         return (error);
  587 }
  588 
  589 /*
  590  * Truncate a file given a file descriptor.
  591  *
  592  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
  593  * descriptor isn't writable.
  594  */
  595 int
  596 kern_ftruncate(struct thread *td, int fd, off_t length)
  597 {
  598         struct file *fp;
  599         int error;
  600 
  601         AUDIT_ARG_FD(fd);
  602         if (length < 0)
  603                 return (EINVAL);
  604         error = fget(td, fd, &cap_ftruncate_rights, &fp);
  605         if (error)
  606                 return (error);
  607         AUDIT_ARG_FILE(td->td_proc, fp);
  608         if (!(fp->f_flag & FWRITE)) {
  609                 fdrop(fp, td);
  610                 return (EINVAL);
  611         }
  612         error = fo_truncate(fp, length, td->td_ucred, td);
  613         fdrop(fp, td);
  614         return (error);
  615 }
  616 
  617 #ifndef _SYS_SYSPROTO_H_
  618 struct ftruncate_args {
  619         int     fd;
  620         int     pad;
  621         off_t   length;
  622 };
  623 #endif
  624 int
  625 sys_ftruncate(struct thread *td, struct ftruncate_args *uap)
  626 {
  627 
  628         return (kern_ftruncate(td, uap->fd, uap->length));
  629 }
  630 
  631 #if defined(COMPAT_43)
  632 #ifndef _SYS_SYSPROTO_H_
  633 struct oftruncate_args {
  634         int     fd;
  635         long    length;
  636 };
  637 #endif
  638 int
  639 oftruncate(struct thread *td, struct oftruncate_args *uap)
  640 {
  641 
  642         return (kern_ftruncate(td, uap->fd, uap->length));
  643 }
  644 #endif /* COMPAT_43 */
  645 
  646 #ifndef _SYS_SYSPROTO_H_
  647 struct ioctl_args {
  648         int     fd;
  649         u_long  com;
  650         caddr_t data;
  651 };
  652 #endif
  653 /* ARGSUSED */
  654 int
  655 sys_ioctl(struct thread *td, struct ioctl_args *uap)
  656 {
  657         u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
  658         u_long com;
  659         int arg, error;
  660         u_int size;
  661         caddr_t data;
  662 
  663         if (uap->com > 0xffffffff) {
  664                 printf(
  665                     "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
  666                     td->td_proc->p_pid, td->td_name, uap->com);
  667                 uap->com &= 0xffffffff;
  668         }
  669         com = uap->com;
  670 
  671         /*
  672          * Interpret high order word to find amount of data to be
  673          * copied to/from the user's address space.
  674          */
  675         size = IOCPARM_LEN(com);
  676         if ((size > IOCPARM_MAX) ||
  677             ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
  678 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
  679             ((com & IOC_OUT) && size == 0) ||
  680 #else
  681             ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
  682 #endif
  683             ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
  684                 return (ENOTTY);
  685 
  686         if (size > 0) {
  687                 if (com & IOC_VOID) {
  688                         /* Integer argument. */
  689                         arg = (intptr_t)uap->data;
  690                         data = (void *)&arg;
  691                         size = 0;
  692                 } else {
  693                         if (size > SYS_IOCTL_SMALL_SIZE)
  694                                 data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
  695                         else
  696                                 data = smalldata;
  697                 }
  698         } else
  699                 data = (void *)&uap->data;
  700         if (com & IOC_IN) {
  701                 error = copyin(uap->data, data, (u_int)size);
  702                 if (error != 0)
  703                         goto out;
  704         } else if (com & IOC_OUT) {
  705                 /*
  706                  * Zero the buffer so the user always
  707                  * gets back something deterministic.
  708                  */
  709                 bzero(data, size);
  710         }
  711 
  712         error = kern_ioctl(td, uap->fd, com, data);
  713 
  714         if (error == 0 && (com & IOC_OUT))
  715                 error = copyout(data, uap->data, (u_int)size);
  716 
  717 out:
  718         if (size > SYS_IOCTL_SMALL_SIZE)
  719                 free(data, M_IOCTLOPS);
  720         return (error);
  721 }
  722 
  723 int
  724 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
  725 {
  726         struct file *fp;
  727         struct filedesc *fdp;
  728         int error, tmp, locked;
  729 
  730         AUDIT_ARG_FD(fd);
  731         AUDIT_ARG_CMD(com);
  732 
  733         fdp = td->td_proc->p_fd;
  734 
  735         switch (com) {
  736         case FIONCLEX:
  737         case FIOCLEX:
  738                 FILEDESC_XLOCK(fdp);
  739                 locked = LA_XLOCKED;
  740                 break;
  741         default:
  742 #ifdef CAPABILITIES
  743                 FILEDESC_SLOCK(fdp);
  744                 locked = LA_SLOCKED;
  745 #else
  746                 locked = LA_UNLOCKED;
  747 #endif
  748                 break;
  749         }
  750 
  751 #ifdef CAPABILITIES
  752         if ((fp = fget_locked(fdp, fd)) == NULL) {
  753                 error = EBADF;
  754                 goto out;
  755         }
  756         if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
  757                 fp = NULL;      /* fhold() was not called yet */
  758                 goto out;
  759         }
  760         fhold(fp);
  761         if (locked == LA_SLOCKED) {
  762                 FILEDESC_SUNLOCK(fdp);
  763                 locked = LA_UNLOCKED;
  764         }
  765 #else
  766         error = fget(td, fd, &cap_ioctl_rights, &fp);
  767         if (error != 0) {
  768                 fp = NULL;
  769                 goto out;
  770         }
  771 #endif
  772         if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
  773                 error = EBADF;
  774                 goto out;
  775         }
  776 
  777         switch (com) {
  778         case FIONCLEX:
  779                 fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
  780                 goto out;
  781         case FIOCLEX:
  782                 fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
  783                 goto out;
  784         case FIONBIO:
  785                 if ((tmp = *(int *)data))
  786                         atomic_set_int(&fp->f_flag, FNONBLOCK);
  787                 else
  788                         atomic_clear_int(&fp->f_flag, FNONBLOCK);
  789                 data = (void *)&tmp;
  790                 break;
  791         case FIOASYNC:
  792                 if ((tmp = *(int *)data))
  793                         atomic_set_int(&fp->f_flag, FASYNC);
  794                 else
  795                         atomic_clear_int(&fp->f_flag, FASYNC);
  796                 data = (void *)&tmp;
  797                 break;
  798         }
  799 
  800         error = fo_ioctl(fp, com, data, td->td_ucred, td);
  801 out:
  802         switch (locked) {
  803         case LA_XLOCKED:
  804                 FILEDESC_XUNLOCK(fdp);
  805                 break;
  806 #ifdef CAPABILITIES
  807         case LA_SLOCKED:
  808                 FILEDESC_SUNLOCK(fdp);
  809                 break;
  810 #endif
  811         default:
  812                 FILEDESC_UNLOCK_ASSERT(fdp);
  813                 break;
  814         }
  815         if (fp != NULL)
  816                 fdrop(fp, td);
  817         return (error);
  818 }
  819 
  820 int
  821 poll_no_poll(int events)
  822 {
  823         /*
  824          * Return true for read/write.  If the user asked for something
  825          * special, return POLLNVAL, so that clients have a way of
  826          * determining reliably whether or not the extended
  827          * functionality is present without hard-coding knowledge
  828          * of specific filesystem implementations.
  829          */
  830         if (events & ~POLLSTANDARD)
  831                 return (POLLNVAL);
  832 
  833         return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
  834 }
  835 
  836 int
  837 sys_pselect(struct thread *td, struct pselect_args *uap)
  838 {
  839         struct timespec ts;
  840         struct timeval tv, *tvp;
  841         sigset_t set, *uset;
  842         int error;
  843 
  844         if (uap->ts != NULL) {
  845                 error = copyin(uap->ts, &ts, sizeof(ts));
  846                 if (error != 0)
  847                     return (error);
  848                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
  849                 tvp = &tv;
  850         } else
  851                 tvp = NULL;
  852         if (uap->sm != NULL) {
  853                 error = copyin(uap->sm, &set, sizeof(set));
  854                 if (error != 0)
  855                         return (error);
  856                 uset = &set;
  857         } else
  858                 uset = NULL;
  859         return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
  860             uset, NFDBITS));
  861 }
  862 
  863 int
  864 kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
  865     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
  866 {
  867         int error;
  868 
  869         if (uset != NULL) {
  870                 error = kern_sigprocmask(td, SIG_SETMASK, uset,
  871                     &td->td_oldsigmask, 0);
  872                 if (error != 0)
  873                         return (error);
  874                 td->td_pflags |= TDP_OLDMASK;
  875                 /*
  876                  * Make sure that ast() is called on return to
  877                  * usermode and TDP_OLDMASK is cleared, restoring old
  878                  * sigmask.
  879                  */
  880                 thread_lock(td);
  881                 td->td_flags |= TDF_ASTPENDING;
  882                 thread_unlock(td);
  883         }
  884         error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
  885         return (error);
  886 }
  887 
  888 #ifndef _SYS_SYSPROTO_H_
  889 struct select_args {
  890         int     nd;
  891         fd_set  *in, *ou, *ex;
  892         struct  timeval *tv;
  893 };
  894 #endif
  895 int
  896 sys_select(struct thread *td, struct select_args *uap)
  897 {
  898         struct timeval tv, *tvp;
  899         int error;
  900 
  901         if (uap->tv != NULL) {
  902                 error = copyin(uap->tv, &tv, sizeof(tv));
  903                 if (error)
  904                         return (error);
  905                 tvp = &tv;
  906         } else
  907                 tvp = NULL;
  908 
  909         return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
  910             NFDBITS));
  911 }
  912 
  913 /*
  914  * In the unlikely case when user specified n greater then the last
  915  * open file descriptor, check that no bits are set after the last
  916  * valid fd.  We must return EBADF if any is set.
  917  *
  918  * There are applications that rely on the behaviour.
  919  *
  920  * nd is fd_lastfile + 1.
  921  */
  922 static int
  923 select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
  924 {
  925         char *addr, *oaddr;
  926         int b, i, res;
  927         uint8_t bits;
  928 
  929         if (nd >= ndu || fd_in == NULL)
  930                 return (0);
  931 
  932         oaddr = NULL;
  933         bits = 0; /* silence gcc */
  934         for (i = nd; i < ndu; i++) {
  935                 b = i / NBBY;
  936 #if BYTE_ORDER == LITTLE_ENDIAN
  937                 addr = (char *)fd_in + b;
  938 #else
  939                 addr = (char *)fd_in;
  940                 if (abi_nfdbits == NFDBITS) {
  941                         addr += rounddown(b, sizeof(fd_mask)) +
  942                             sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
  943                 } else {
  944                         addr += rounddown(b, sizeof(uint32_t)) +
  945                             sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
  946                 }
  947 #endif
  948                 if (addr != oaddr) {
  949                         res = fubyte(addr);
  950                         if (res == -1)
  951                                 return (EFAULT);
  952                         oaddr = addr;
  953                         bits = res;
  954                 }
  955                 if ((bits & (1 << (i % NBBY))) != 0)
  956                         return (EBADF);
  957         }
  958         return (0);
  959 }
  960 
  961 int
  962 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
  963     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
  964 {
  965         struct filedesc *fdp;
  966         /*
  967          * The magic 2048 here is chosen to be just enough for FD_SETSIZE
  968          * infds with the new FD_SETSIZE of 1024, and more than enough for
  969          * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
  970          * of 256.
  971          */
  972         fd_mask s_selbits[howmany(2048, NFDBITS)];
  973         fd_mask *ibits[3], *obits[3], *selbits, *sbp;
  974         struct timeval rtv;
  975         sbintime_t asbt, precision, rsbt;
  976         u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
  977         int error, lf, ndu;
  978 
  979         if (nd < 0)
  980                 return (EINVAL);
  981         fdp = td->td_proc->p_fd;
  982         ndu = nd;
  983         lf = fdp->fd_lastfile;
  984         if (nd > lf + 1)
  985                 nd = lf + 1;
  986 
  987         error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
  988         if (error != 0)
  989                 return (error);
  990         error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
  991         if (error != 0)
  992                 return (error);
  993         error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
  994         if (error != 0)
  995                 return (error);
  996 
  997         /*
  998          * Allocate just enough bits for the non-null fd_sets.  Use the
  999          * preallocated auto buffer if possible.
 1000          */
 1001         nfdbits = roundup(nd, NFDBITS);
 1002         ncpbytes = nfdbits / NBBY;
 1003         ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
 1004         nbufbytes = 0;
 1005         if (fd_in != NULL)
 1006                 nbufbytes += 2 * ncpbytes;
 1007         if (fd_ou != NULL)
 1008                 nbufbytes += 2 * ncpbytes;
 1009         if (fd_ex != NULL)
 1010                 nbufbytes += 2 * ncpbytes;
 1011         if (nbufbytes <= sizeof s_selbits)
 1012                 selbits = &s_selbits[0];
 1013         else
 1014                 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
 1015 
 1016         /*
 1017          * Assign pointers into the bit buffers and fetch the input bits.
 1018          * Put the output buffers together so that they can be bzeroed
 1019          * together.
 1020          */
 1021         sbp = selbits;
 1022 #define getbits(name, x) \
 1023         do {                                                            \
 1024                 if (name == NULL) {                                     \
 1025                         ibits[x] = NULL;                                \
 1026                         obits[x] = NULL;                                \
 1027                 } else {                                                \
 1028                         ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;   \
 1029                         obits[x] = sbp;                                 \
 1030                         sbp += ncpbytes / sizeof *sbp;                  \
 1031                         error = copyin(name, ibits[x], ncpubytes);      \
 1032                         if (error != 0)                                 \
 1033                                 goto done;                              \
 1034                         if (ncpbytes != ncpubytes)                      \
 1035                                 bzero((char *)ibits[x] + ncpubytes,     \
 1036                                     ncpbytes - ncpubytes);              \
 1037                 }                                                       \
 1038         } while (0)
 1039         getbits(fd_in, 0);
 1040         getbits(fd_ou, 1);
 1041         getbits(fd_ex, 2);
 1042 #undef  getbits
 1043 
 1044 #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
 1045         /*
 1046          * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
 1047          * we are running under 32-bit emulation. This should be more
 1048          * generic.
 1049          */
 1050 #define swizzle_fdset(bits)                                             \
 1051         if (abi_nfdbits != NFDBITS && bits != NULL) {                   \
 1052                 int i;                                                  \
 1053                 for (i = 0; i < ncpbytes / sizeof *sbp; i++)            \
 1054                         bits[i] = (bits[i] >> 32) | (bits[i] << 32);    \
 1055         }
 1056 #else
 1057 #define swizzle_fdset(bits)
 1058 #endif
 1059 
 1060         /* Make sure the bit order makes it through an ABI transition */
 1061         swizzle_fdset(ibits[0]);
 1062         swizzle_fdset(ibits[1]);
 1063         swizzle_fdset(ibits[2]);
 1064         
 1065         if (nbufbytes != 0)
 1066                 bzero(selbits, nbufbytes / 2);
 1067 
 1068         precision = 0;
 1069         if (tvp != NULL) {
 1070                 rtv = *tvp;
 1071                 if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
 1072                     rtv.tv_usec >= 1000000) {
 1073                         error = EINVAL;
 1074                         goto done;
 1075                 }
 1076                 if (!timevalisset(&rtv))
 1077                         asbt = 0;
 1078                 else if (rtv.tv_sec <= INT32_MAX) {
 1079                         rsbt = tvtosbt(rtv);
 1080                         precision = rsbt;
 1081                         precision >>= tc_precexp;
 1082                         if (TIMESEL(&asbt, rsbt))
 1083                                 asbt += tc_tick_sbt;
 1084                         if (asbt <= SBT_MAX - rsbt)
 1085                                 asbt += rsbt;
 1086                         else
 1087                                 asbt = -1;
 1088                 } else
 1089                         asbt = -1;
 1090         } else
 1091                 asbt = -1;
 1092         seltdinit(td);
 1093         /* Iterate until the timeout expires or descriptors become ready. */
 1094         for (;;) {
 1095                 error = selscan(td, ibits, obits, nd);
 1096                 if (error || td->td_retval[0] != 0)
 1097                         break;
 1098                 error = seltdwait(td, asbt, precision);
 1099                 if (error)
 1100                         break;
 1101                 error = selrescan(td, ibits, obits);
 1102                 if (error || td->td_retval[0] != 0)
 1103                         break;
 1104         }
 1105         seltdclear(td);
 1106 
 1107 done:
 1108         /* select is not restarted after signals... */
 1109         if (error == ERESTART)
 1110                 error = EINTR;
 1111         if (error == EWOULDBLOCK)
 1112                 error = 0;
 1113 
 1114         /* swizzle bit order back, if necessary */
 1115         swizzle_fdset(obits[0]);
 1116         swizzle_fdset(obits[1]);
 1117         swizzle_fdset(obits[2]);
 1118 #undef swizzle_fdset
 1119 
 1120 #define putbits(name, x) \
 1121         if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
 1122                 error = error2;
 1123         if (error == 0) {
 1124                 int error2;
 1125 
 1126                 putbits(fd_in, 0);
 1127                 putbits(fd_ou, 1);
 1128                 putbits(fd_ex, 2);
 1129 #undef putbits
 1130         }
 1131         if (selbits != &s_selbits[0])
 1132                 free(selbits, M_SELECT);
 1133 
 1134         return (error);
 1135 }
 1136 /* 
 1137  * Convert a select bit set to poll flags.
 1138  *
 1139  * The backend always returns POLLHUP/POLLERR if appropriate and we
 1140  * return this as a set bit in any set.
 1141  */
 1142 static int select_flags[3] = {
 1143     POLLRDNORM | POLLHUP | POLLERR,
 1144     POLLWRNORM | POLLHUP | POLLERR,
 1145     POLLRDBAND | POLLERR
 1146 };
 1147 
 1148 /*
 1149  * Compute the fo_poll flags required for a fd given by the index and
 1150  * bit position in the fd_mask array.
 1151  */
 1152 static __inline int
 1153 selflags(fd_mask **ibits, int idx, fd_mask bit)
 1154 {
 1155         int flags;
 1156         int msk;
 1157 
 1158         flags = 0;
 1159         for (msk = 0; msk < 3; msk++) {
 1160                 if (ibits[msk] == NULL)
 1161                         continue;
 1162                 if ((ibits[msk][idx] & bit) == 0)
 1163                         continue;
 1164                 flags |= select_flags[msk];
 1165         }
 1166         return (flags);
 1167 }
 1168 
 1169 /*
 1170  * Set the appropriate output bits given a mask of fired events and the
 1171  * input bits originally requested.
 1172  */
 1173 static __inline int
 1174 selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
 1175 {
 1176         int msk;
 1177         int n;
 1178 
 1179         n = 0;
 1180         for (msk = 0; msk < 3; msk++) {
 1181                 if ((events & select_flags[msk]) == 0)
 1182                         continue;
 1183                 if (ibits[msk] == NULL)
 1184                         continue;
 1185                 if ((ibits[msk][idx] & bit) == 0)
 1186                         continue;
 1187                 /*
 1188                  * XXX Check for a duplicate set.  This can occur because a
 1189                  * socket calls selrecord() twice for each poll() call
 1190                  * resulting in two selfds per real fd.  selrescan() will
 1191                  * call selsetbits twice as a result.
 1192                  */
 1193                 if ((obits[msk][idx] & bit) != 0)
 1194                         continue;
 1195                 obits[msk][idx] |= bit;
 1196                 n++;
 1197         }
 1198 
 1199         return (n);
 1200 }
 1201 
 1202 static __inline int
 1203 getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
 1204 {
 1205 
 1206         return (fget_unlocked(fdp, fd, &cap_event_rights, fpp, NULL));
 1207 }
 1208 
 1209 /*
 1210  * Traverse the list of fds attached to this thread's seltd and check for
 1211  * completion.
 1212  */
 1213 static int
 1214 selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
 1215 {
 1216         struct filedesc *fdp;
 1217         struct selinfo *si;
 1218         struct seltd *stp;
 1219         struct selfd *sfp;
 1220         struct selfd *sfn;
 1221         struct file *fp;
 1222         fd_mask bit;
 1223         int fd, ev, n, idx;
 1224         int error;
 1225 
 1226         fdp = td->td_proc->p_fd;
 1227         stp = td->td_sel;
 1228         n = 0;
 1229         STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
 1230                 fd = (int)(uintptr_t)sfp->sf_cookie;
 1231                 si = sfp->sf_si;
 1232                 selfdfree(stp, sfp);
 1233                 /* If the selinfo wasn't cleared the event didn't fire. */
 1234                 if (si != NULL)
 1235                         continue;
 1236                 error = getselfd_cap(fdp, fd, &fp);
 1237                 if (error)
 1238                         return (error);
 1239                 idx = fd / NFDBITS;
 1240                 bit = (fd_mask)1 << (fd % NFDBITS);
 1241                 ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
 1242                 fdrop(fp, td);
 1243                 if (ev != 0)
 1244                         n += selsetbits(ibits, obits, idx, bit, ev);
 1245         }
 1246         stp->st_flags = 0;
 1247         td->td_retval[0] = n;
 1248         return (0);
 1249 }
 1250 
 1251 /*
 1252  * Perform the initial filedescriptor scan and register ourselves with
 1253  * each selinfo.
 1254  */
 1255 static int
 1256 selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd)
 1257 {
 1258         struct filedesc *fdp;
 1259         struct file *fp;
 1260         fd_mask bit;
 1261         int ev, flags, end, fd;
 1262         int n, idx;
 1263         int error;
 1264 
 1265         fdp = td->td_proc->p_fd;
 1266         n = 0;
 1267         for (idx = 0, fd = 0; fd < nfd; idx++) {
 1268                 end = imin(fd + NFDBITS, nfd);
 1269                 for (bit = 1; fd < end; bit <<= 1, fd++) {
 1270                         /* Compute the list of events we're interested in. */
 1271                         flags = selflags(ibits, idx, bit);
 1272                         if (flags == 0)
 1273                                 continue;
 1274                         error = getselfd_cap(fdp, fd, &fp);
 1275                         if (error)
 1276                                 return (error);
 1277                         selfdalloc(td, (void *)(uintptr_t)fd);
 1278                         ev = fo_poll(fp, flags, td->td_ucred, td);
 1279                         fdrop(fp, td);
 1280                         if (ev != 0)
 1281                                 n += selsetbits(ibits, obits, idx, bit, ev);
 1282                 }
 1283         }
 1284 
 1285         td->td_retval[0] = n;
 1286         return (0);
 1287 }
 1288 
 1289 int
 1290 sys_poll(struct thread *td, struct poll_args *uap)
 1291 {
 1292         struct timespec ts, *tsp;
 1293 
 1294         if (uap->timeout != INFTIM) {
 1295                 if (uap->timeout < 0)
 1296                         return (EINVAL);
 1297                 ts.tv_sec = uap->timeout / 1000;
 1298                 ts.tv_nsec = (uap->timeout % 1000) * 1000000;
 1299                 tsp = &ts;
 1300         } else
 1301                 tsp = NULL;
 1302 
 1303         return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
 1304 }
 1305 
 1306 int
 1307 kern_poll(struct thread *td, struct pollfd *fds, u_int nfds,
 1308     struct timespec *tsp, sigset_t *uset)
 1309 {
 1310         struct pollfd *bits;
 1311         struct pollfd smallbits[32];
 1312         sbintime_t sbt, precision, tmp;
 1313         time_t over;
 1314         struct timespec ts;
 1315         int error;
 1316         size_t ni;
 1317 
 1318         precision = 0;
 1319         if (tsp != NULL) {
 1320                 if (tsp->tv_sec < 0)
 1321                         return (EINVAL);
 1322                 if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
 1323                         return (EINVAL);
 1324                 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
 1325                         sbt = 0;
 1326                 else {
 1327                         ts = *tsp;
 1328                         if (ts.tv_sec > INT32_MAX / 2) {
 1329                                 over = ts.tv_sec - INT32_MAX / 2;
 1330                                 ts.tv_sec -= over;
 1331                         } else
 1332                                 over = 0;
 1333                         tmp = tstosbt(ts);
 1334                         precision = tmp;
 1335                         precision >>= tc_precexp;
 1336                         if (TIMESEL(&sbt, tmp))
 1337                                 sbt += tc_tick_sbt;
 1338                         sbt += tmp;
 1339                 }
 1340         } else
 1341                 sbt = -1;
 1342 
 1343         if (nfds > maxfilesperproc && nfds > FD_SETSIZE) 
 1344                 return (EINVAL);
 1345         ni = nfds * sizeof(struct pollfd);
 1346         if (ni > sizeof(smallbits))
 1347                 bits = malloc(ni, M_TEMP, M_WAITOK);
 1348         else
 1349                 bits = smallbits;
 1350         error = copyin(fds, bits, ni);
 1351         if (error)
 1352                 goto done;
 1353 
 1354         if (uset != NULL) {
 1355                 error = kern_sigprocmask(td, SIG_SETMASK, uset,
 1356                     &td->td_oldsigmask, 0);
 1357                 if (error)
 1358                         goto done;
 1359                 td->td_pflags |= TDP_OLDMASK;
 1360                 /*
 1361                  * Make sure that ast() is called on return to
 1362                  * usermode and TDP_OLDMASK is cleared, restoring old
 1363                  * sigmask.
 1364                  */
 1365                 thread_lock(td);
 1366                 td->td_flags |= TDF_ASTPENDING;
 1367                 thread_unlock(td);
 1368         }
 1369 
 1370         seltdinit(td);
 1371         /* Iterate until the timeout expires or descriptors become ready. */
 1372         for (;;) {
 1373                 error = pollscan(td, bits, nfds);
 1374                 if (error || td->td_retval[0] != 0)
 1375                         break;
 1376                 error = seltdwait(td, sbt, precision);
 1377                 if (error)
 1378                         break;
 1379                 error = pollrescan(td);
 1380                 if (error || td->td_retval[0] != 0)
 1381                         break;
 1382         }
 1383         seltdclear(td);
 1384 
 1385 done:
 1386         /* poll is not restarted after signals... */
 1387         if (error == ERESTART)
 1388                 error = EINTR;
 1389         if (error == EWOULDBLOCK)
 1390                 error = 0;
 1391         if (error == 0) {
 1392                 error = pollout(td, bits, fds, nfds);
 1393                 if (error)
 1394                         goto out;
 1395         }
 1396 out:
 1397         if (ni > sizeof(smallbits))
 1398                 free(bits, M_TEMP);
 1399         return (error);
 1400 }
 1401 
 1402 int
 1403 sys_ppoll(struct thread *td, struct ppoll_args *uap)
 1404 {
 1405         struct timespec ts, *tsp;
 1406         sigset_t set, *ssp;
 1407         int error;
 1408 
 1409         if (uap->ts != NULL) {
 1410                 error = copyin(uap->ts, &ts, sizeof(ts));
 1411                 if (error)
 1412                         return (error);
 1413                 tsp = &ts;
 1414         } else
 1415                 tsp = NULL;
 1416         if (uap->set != NULL) {
 1417                 error = copyin(uap->set, &set, sizeof(set));
 1418                 if (error)
 1419                         return (error);
 1420                 ssp = &set;
 1421         } else
 1422                 ssp = NULL;
 1423         /*
 1424          * fds is still a pointer to user space. kern_poll() will
 1425          * take care of copyin that array to the kernel space.
 1426          */
 1427 
 1428         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
 1429 }
 1430 
 1431 static int
 1432 pollrescan(struct thread *td)
 1433 {
 1434         struct seltd *stp;
 1435         struct selfd *sfp;
 1436         struct selfd *sfn;
 1437         struct selinfo *si;
 1438         struct filedesc *fdp;
 1439         struct file *fp;
 1440         struct pollfd *fd;
 1441         int n;
 1442 
 1443         n = 0;
 1444         fdp = td->td_proc->p_fd;
 1445         stp = td->td_sel;
 1446         FILEDESC_SLOCK(fdp);
 1447         STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
 1448                 fd = (struct pollfd *)sfp->sf_cookie;
 1449                 si = sfp->sf_si;
 1450                 selfdfree(stp, sfp);
 1451                 /* If the selinfo wasn't cleared the event didn't fire. */
 1452                 if (si != NULL)
 1453                         continue;
 1454                 fp = fdp->fd_ofiles[fd->fd].fde_file;
 1455 #ifdef CAPABILITIES
 1456                 if (fp == NULL ||
 1457                     cap_check(cap_rights(fdp, fd->fd), &cap_event_rights) != 0)
 1458 #else
 1459                 if (fp == NULL)
 1460 #endif
 1461                 {
 1462                         fd->revents = POLLNVAL;
 1463                         n++;
 1464                         continue;
 1465                 }
 1466 
 1467                 /*
 1468                  * Note: backend also returns POLLHUP and
 1469                  * POLLERR if appropriate.
 1470                  */
 1471                 fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
 1472                 if (fd->revents != 0)
 1473                         n++;
 1474         }
 1475         FILEDESC_SUNLOCK(fdp);
 1476         stp->st_flags = 0;
 1477         td->td_retval[0] = n;
 1478         return (0);
 1479 }
 1480 
 1481 
 1482 static int
 1483 pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
 1484 {
 1485         int error = 0;
 1486         u_int i = 0;
 1487         u_int n = 0;
 1488 
 1489         for (i = 0; i < nfd; i++) {
 1490                 error = copyout(&fds->revents, &ufds->revents,
 1491                     sizeof(ufds->revents));
 1492                 if (error)
 1493                         return (error);
 1494                 if (fds->revents != 0)
 1495                         n++;
 1496                 fds++;
 1497                 ufds++;
 1498         }
 1499         td->td_retval[0] = n;
 1500         return (0);
 1501 }
 1502 
 1503 static int
 1504 pollscan(struct thread *td, struct pollfd *fds, u_int nfd)
 1505 {
 1506         struct filedesc *fdp = td->td_proc->p_fd;
 1507         struct file *fp;
 1508         int i, n = 0;
 1509 
 1510         FILEDESC_SLOCK(fdp);
 1511         for (i = 0; i < nfd; i++, fds++) {
 1512                 if (fds->fd > fdp->fd_lastfile) {
 1513                         fds->revents = POLLNVAL;
 1514                         n++;
 1515                 } else if (fds->fd < 0) {
 1516                         fds->revents = 0;
 1517                 } else {
 1518                         fp = fdp->fd_ofiles[fds->fd].fde_file;
 1519 #ifdef CAPABILITIES
 1520                         if (fp == NULL ||
 1521                             cap_check(cap_rights(fdp, fds->fd), &cap_event_rights) != 0)
 1522 #else
 1523                         if (fp == NULL)
 1524 #endif
 1525                         {
 1526                                 fds->revents = POLLNVAL;
 1527                                 n++;
 1528                         } else {
 1529                                 /*
 1530                                  * Note: backend also returns POLLHUP and
 1531                                  * POLLERR if appropriate.
 1532                                  */
 1533                                 selfdalloc(td, fds);
 1534                                 fds->revents = fo_poll(fp, fds->events,
 1535                                     td->td_ucred, td);
 1536                                 /*
 1537                                  * POSIX requires POLLOUT to be never
 1538                                  * set simultaneously with POLLHUP.
 1539                                  */
 1540                                 if ((fds->revents & POLLHUP) != 0)
 1541                                         fds->revents &= ~POLLOUT;
 1542 
 1543                                 if (fds->revents != 0)
 1544                                         n++;
 1545                         }
 1546                 }
 1547         }
 1548         FILEDESC_SUNLOCK(fdp);
 1549         td->td_retval[0] = n;
 1550         return (0);
 1551 }
 1552 
 1553 /*
 1554  * XXX This was created specifically to support netncp and netsmb.  This
 1555  * allows the caller to specify a socket to wait for events on.  It returns
 1556  * 0 if any events matched and an error otherwise.  There is no way to
 1557  * determine which events fired.
 1558  */
 1559 int
 1560 selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
 1561 {
 1562         struct timeval rtv;
 1563         sbintime_t asbt, precision, rsbt;
 1564         int error;
 1565 
 1566         precision = 0;  /* stupid gcc! */
 1567         if (tvp != NULL) {
 1568                 rtv = *tvp;
 1569                 if (rtv.tv_sec < 0 || rtv.tv_usec < 0 || 
 1570                     rtv.tv_usec >= 1000000)
 1571                         return (EINVAL);
 1572                 if (!timevalisset(&rtv))
 1573                         asbt = 0;
 1574                 else if (rtv.tv_sec <= INT32_MAX) {
 1575                         rsbt = tvtosbt(rtv);
 1576                         precision = rsbt;
 1577                         precision >>= tc_precexp;
 1578                         if (TIMESEL(&asbt, rsbt))
 1579                                 asbt += tc_tick_sbt;
 1580                         if (asbt <= SBT_MAX - rsbt)
 1581                                 asbt += rsbt;
 1582                         else
 1583                                 asbt = -1;
 1584                 } else
 1585                         asbt = -1;
 1586         } else
 1587                 asbt = -1;
 1588         seltdinit(td);
 1589         /*
 1590          * Iterate until the timeout expires or the socket becomes ready.
 1591          */
 1592         for (;;) {
 1593                 selfdalloc(td, NULL);
 1594                 error = sopoll(so, events, NULL, td);
 1595                 /* error here is actually the ready events. */
 1596                 if (error)
 1597                         return (0);
 1598                 error = seltdwait(td, asbt, precision);
 1599                 if (error)
 1600                         break;
 1601         }
 1602         seltdclear(td);
 1603         /* XXX Duplicates ncp/smb behavior. */
 1604         if (error == ERESTART)
 1605                 error = 0;
 1606         return (error);
 1607 }
 1608 
 1609 /*
 1610  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
 1611  * have two select sets, one for read and another for write.
 1612  */
 1613 static void
 1614 selfdalloc(struct thread *td, void *cookie)
 1615 {
 1616         struct seltd *stp;
 1617 
 1618         stp = td->td_sel;
 1619         if (stp->st_free1 == NULL)
 1620                 stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
 1621         stp->st_free1->sf_td = stp;
 1622         stp->st_free1->sf_cookie = cookie;
 1623         if (stp->st_free2 == NULL)
 1624                 stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
 1625         stp->st_free2->sf_td = stp;
 1626         stp->st_free2->sf_cookie = cookie;
 1627 }
 1628 
 1629 static void
 1630 selfdfree(struct seltd *stp, struct selfd *sfp)
 1631 {
 1632         STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
 1633         if (sfp->sf_si != NULL) {
 1634                 mtx_lock(sfp->sf_mtx);
 1635                 if (sfp->sf_si != NULL) {
 1636                         TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
 1637                         refcount_release(&sfp->sf_refs);
 1638                 }
 1639                 mtx_unlock(sfp->sf_mtx);
 1640         }
 1641         if (refcount_release(&sfp->sf_refs))
 1642                 uma_zfree(selfd_zone, sfp);
 1643 }
 1644 
 1645 /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
 1646 void
 1647 seldrain(struct selinfo *sip)
 1648 {
 1649 
 1650         /*
 1651          * This feature is already provided by doselwakeup(), thus it is
 1652          * enough to go for it.
 1653          * Eventually, the context, should take care to avoid races
 1654          * between thread calling select()/poll() and file descriptor
 1655          * detaching, but, again, the races are just the same as
 1656          * selwakeup().
 1657          */
 1658         doselwakeup(sip, -1);
 1659 }
 1660 
 1661 /*
 1662  * Record a select request.
 1663  */
 1664 void
 1665 selrecord(struct thread *selector, struct selinfo *sip)
 1666 {
 1667         struct selfd *sfp;
 1668         struct seltd *stp;
 1669         struct mtx *mtxp;
 1670 
 1671         stp = selector->td_sel;
 1672         /*
 1673          * Don't record when doing a rescan.
 1674          */
 1675         if (stp->st_flags & SELTD_RESCAN)
 1676                 return;
 1677         /*
 1678          * Grab one of the preallocated descriptors.
 1679          */
 1680         sfp = NULL;
 1681         if ((sfp = stp->st_free1) != NULL)
 1682                 stp->st_free1 = NULL;
 1683         else if ((sfp = stp->st_free2) != NULL)
 1684                 stp->st_free2 = NULL;
 1685         else
 1686                 panic("selrecord: No free selfd on selq");
 1687         mtxp = sip->si_mtx;
 1688         if (mtxp == NULL)
 1689                 mtxp = mtx_pool_find(mtxpool_select, sip);
 1690         /*
 1691          * Initialize the sfp and queue it in the thread.
 1692          */
 1693         sfp->sf_si = sip;
 1694         sfp->sf_mtx = mtxp;
 1695         refcount_init(&sfp->sf_refs, 2);
 1696         STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
 1697         /*
 1698          * Now that we've locked the sip, check for initialization.
 1699          */
 1700         mtx_lock(mtxp);
 1701         if (sip->si_mtx == NULL) {
 1702                 sip->si_mtx = mtxp;
 1703                 TAILQ_INIT(&sip->si_tdlist);
 1704         }
 1705         /*
 1706          * Add this thread to the list of selfds listening on this selinfo.
 1707          */
 1708         TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
 1709         mtx_unlock(sip->si_mtx);
 1710 }
 1711 
 1712 /* Wake up a selecting thread. */
 1713 void
 1714 selwakeup(struct selinfo *sip)
 1715 {
 1716         doselwakeup(sip, -1);
 1717 }
 1718 
 1719 /* Wake up a selecting thread, and set its priority. */
 1720 void
 1721 selwakeuppri(struct selinfo *sip, int pri)
 1722 {
 1723         doselwakeup(sip, pri);
 1724 }
 1725 
 1726 /*
 1727  * Do a wakeup when a selectable event occurs.
 1728  */
 1729 static void
 1730 doselwakeup(struct selinfo *sip, int pri)
 1731 {
 1732         struct selfd *sfp;
 1733         struct selfd *sfn;
 1734         struct seltd *stp;
 1735 
 1736         /* If it's not initialized there can't be any waiters. */
 1737         if (sip->si_mtx == NULL)
 1738                 return;
 1739         /*
 1740          * Locking the selinfo locks all selfds associated with it.
 1741          */
 1742         mtx_lock(sip->si_mtx);
 1743         TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
 1744                 /*
 1745                  * Once we remove this sfp from the list and clear the
 1746                  * sf_si seltdclear will know to ignore this si.
 1747                  */
 1748                 TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
 1749                 sfp->sf_si = NULL;
 1750                 stp = sfp->sf_td;
 1751                 mtx_lock(&stp->st_mtx);
 1752                 stp->st_flags |= SELTD_PENDING;
 1753                 cv_broadcastpri(&stp->st_wait, pri);
 1754                 mtx_unlock(&stp->st_mtx);
 1755                 if (refcount_release(&sfp->sf_refs))
 1756                         uma_zfree(selfd_zone, sfp);
 1757         }
 1758         mtx_unlock(sip->si_mtx);
 1759 }
 1760 
 1761 static void
 1762 seltdinit(struct thread *td)
 1763 {
 1764         struct seltd *stp;
 1765 
 1766         if ((stp = td->td_sel) != NULL)
 1767                 goto out;
 1768         td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
 1769         mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
 1770         cv_init(&stp->st_wait, "select");
 1771 out:
 1772         stp->st_flags = 0;
 1773         STAILQ_INIT(&stp->st_selq);
 1774 }
 1775 
 1776 static int
 1777 seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
 1778 {
 1779         struct seltd *stp;
 1780         int error;
 1781 
 1782         stp = td->td_sel;
 1783         /*
 1784          * An event of interest may occur while we do not hold the seltd
 1785          * locked so check the pending flag before we sleep.
 1786          */
 1787         mtx_lock(&stp->st_mtx);
 1788         /*
 1789          * Any further calls to selrecord will be a rescan.
 1790          */
 1791         stp->st_flags |= SELTD_RESCAN;
 1792         if (stp->st_flags & SELTD_PENDING) {
 1793                 mtx_unlock(&stp->st_mtx);
 1794                 return (0);
 1795         }
 1796         if (sbt == 0)
 1797                 error = EWOULDBLOCK;
 1798         else if (sbt != -1)
 1799                 error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
 1800                     sbt, precision, C_ABSOLUTE);
 1801         else
 1802                 error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
 1803         mtx_unlock(&stp->st_mtx);
 1804 
 1805         return (error);
 1806 }
 1807 
 1808 void
 1809 seltdfini(struct thread *td)
 1810 {
 1811         struct seltd *stp;
 1812 
 1813         stp = td->td_sel;
 1814         if (stp == NULL)
 1815                 return;
 1816         if (stp->st_free1)
 1817                 uma_zfree(selfd_zone, stp->st_free1);
 1818         if (stp->st_free2)
 1819                 uma_zfree(selfd_zone, stp->st_free2);
 1820         td->td_sel = NULL;
 1821         cv_destroy(&stp->st_wait);
 1822         mtx_destroy(&stp->st_mtx);
 1823         free(stp, M_SELECT);
 1824 }
 1825 
 1826 /*
 1827  * Remove the references to the thread from all of the objects we were
 1828  * polling.
 1829  */
 1830 static void
 1831 seltdclear(struct thread *td)
 1832 {
 1833         struct seltd *stp;
 1834         struct selfd *sfp;
 1835         struct selfd *sfn;
 1836 
 1837         stp = td->td_sel;
 1838         STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
 1839                 selfdfree(stp, sfp);
 1840         stp->st_flags = 0;
 1841 }
 1842 
 1843 static void selectinit(void *);
 1844 SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
 1845 static void
 1846 selectinit(void *dummy __unused)
 1847 {
 1848 
 1849         selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
 1850             NULL, NULL, UMA_ALIGN_PTR, 0);
 1851         mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
 1852 }
 1853 
 1854 /*
 1855  * Set up a syscall return value that follows the convention specified for
 1856  * posix_* functions.
 1857  */
 1858 int
 1859 kern_posix_error(struct thread *td, int error)
 1860 {
 1861 
 1862         if (error <= 0)
 1863                 return (error);
 1864         td->td_errno = error;
 1865         td->td_pflags |= TDP_NERRNO;
 1866         td->td_retval[0] = error;
 1867         return (0);
 1868 }

Cache object: af8e0e15f0a5c7c3a47279597eb955df


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.