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/kern_descrip.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  * Copyright (c) 1982, 1986, 1989, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)kern_descrip.c      8.6 (Berkeley) 4/19/94
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD: releng/9.1/sys/kern/kern_descrip.c 238560 2012-07-18 04:52:37Z mjg $");
   39 
   40 #include "opt_capsicum.h"
   41 #include "opt_compat.h"
   42 #include "opt_ddb.h"
   43 #include "opt_ktrace.h"
   44 #include "opt_procdesc.h"
   45 
   46 #include <sys/param.h>
   47 #include <sys/systm.h>
   48 
   49 #include <sys/capability.h>
   50 #include <sys/conf.h>
   51 #include <sys/domain.h>
   52 #include <sys/fcntl.h>
   53 #include <sys/file.h>
   54 #include <sys/filedesc.h>
   55 #include <sys/filio.h>
   56 #include <sys/jail.h>
   57 #include <sys/kernel.h>
   58 #include <sys/limits.h>
   59 #include <sys/lock.h>
   60 #include <sys/malloc.h>
   61 #include <sys/mman.h>
   62 #include <sys/mount.h>
   63 #include <sys/mqueue.h>
   64 #include <sys/mutex.h>
   65 #include <sys/namei.h>
   66 #include <sys/selinfo.h>
   67 #include <sys/pipe.h>
   68 #include <sys/priv.h>
   69 #include <sys/proc.h>
   70 #include <sys/procdesc.h>
   71 #include <sys/protosw.h>
   72 #include <sys/racct.h>
   73 #include <sys/resourcevar.h>
   74 #include <sys/signalvar.h>
   75 #include <sys/socketvar.h>
   76 #include <sys/stat.h>
   77 #include <sys/sx.h>
   78 #include <sys/syscallsubr.h>
   79 #include <sys/sysctl.h>
   80 #include <sys/sysproto.h>
   81 #include <sys/tty.h>
   82 #include <sys/unistd.h>
   83 #include <sys/un.h>
   84 #include <sys/unpcb.h>
   85 #include <sys/user.h>
   86 #include <sys/vnode.h>
   87 #ifdef KTRACE
   88 #include <sys/ktrace.h>
   89 #endif
   90 
   91 #include <net/vnet.h>
   92 
   93 #include <netinet/in.h>
   94 #include <netinet/in_pcb.h>
   95 
   96 #include <security/audit/audit.h>
   97 
   98 #include <vm/uma.h>
   99 #include <vm/vm.h>
  100 
  101 #include <ddb/ddb.h>
  102 
  103 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
  104 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
  105                      "file desc to leader structures");
  106 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
  107 
  108 MALLOC_DECLARE(M_FADVISE);
  109 
  110 static uma_zone_t file_zone;
  111 
  112 
  113 /* Flags for do_dup() */
  114 #define DUP_FIXED       0x1     /* Force fixed allocation */
  115 #define DUP_FCNTL       0x2     /* fcntl()-style errors */
  116 
  117 static int do_dup(struct thread *td, int flags, int old, int new,
  118     register_t *retval);
  119 static int      fd_first_free(struct filedesc *, int, int);
  120 static int      fd_last_used(struct filedesc *, int, int);
  121 static void     fdgrowtable(struct filedesc *, int);
  122 static void     fdunused(struct filedesc *fdp, int fd);
  123 static void     fdused(struct filedesc *fdp, int fd);
  124 static int      fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
  125 static int      fill_socket_info(struct socket *so, struct kinfo_file *kif);
  126 static int      fill_pts_info(struct tty *tp, struct kinfo_file *kif);
  127 static int      fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
  128 static int      fill_procdesc_info(struct procdesc *pdp,
  129     struct kinfo_file *kif);
  130 static int      fill_shm_info(struct file *fp, struct kinfo_file *kif);
  131 
  132 /*
  133  * A process is initially started out with NDFILE descriptors stored within
  134  * this structure, selected to be enough for typical applications based on
  135  * the historical limit of 20 open files (and the usage of descriptors by
  136  * shells).  If these descriptors are exhausted, a larger descriptor table
  137  * may be allocated, up to a process' resource limit; the internal arrays
  138  * are then unused.
  139  */
  140 #define NDFILE          20
  141 #define NDSLOTSIZE      sizeof(NDSLOTTYPE)
  142 #define NDENTRIES       (NDSLOTSIZE * __CHAR_BIT)
  143 #define NDSLOT(x)       ((x) / NDENTRIES)
  144 #define NDBIT(x)        ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
  145 #define NDSLOTS(x)      (((x) + NDENTRIES - 1) / NDENTRIES)
  146 
  147 /*
  148  * Storage required per open file descriptor.
  149  */
  150 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
  151 
  152 /*
  153  * Storage to hold unused ofiles that need to be reclaimed.
  154  */
  155 struct freetable {
  156         struct file     **ft_table;
  157         SLIST_ENTRY(freetable) ft_next;
  158 };
  159 
  160 /*
  161  * Basic allocation of descriptors:
  162  * one of the above, plus arrays for NDFILE descriptors.
  163  */
  164 struct filedesc0 {
  165         struct  filedesc fd_fd;
  166         /*
  167          * ofiles which need to be reclaimed on free.
  168          */
  169         SLIST_HEAD(,freetable) fd_free;
  170         /*
  171          * These arrays are used when the number of open files is
  172          * <= NDFILE, and are then pointed to by the pointers above.
  173          */
  174         struct  file *fd_dfiles[NDFILE];
  175         char    fd_dfileflags[NDFILE];
  176         NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
  177 };
  178 
  179 /*
  180  * Descriptor management.
  181  */
  182 volatile int openfiles;                 /* actual number of open files */
  183 struct mtx sigio_lock;          /* mtx to protect pointers to sigio */
  184 void    (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
  185 
  186 /* A mutex to protect the association between a proc and filedesc. */
  187 static struct mtx       fdesc_mtx;
  188 
  189 /*
  190  * Find the first zero bit in the given bitmap, starting at low and not
  191  * exceeding size - 1.
  192  */
  193 static int
  194 fd_first_free(struct filedesc *fdp, int low, int size)
  195 {
  196         NDSLOTTYPE *map = fdp->fd_map;
  197         NDSLOTTYPE mask;
  198         int off, maxoff;
  199 
  200         if (low >= size)
  201                 return (low);
  202 
  203         off = NDSLOT(low);
  204         if (low % NDENTRIES) {
  205                 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
  206                 if ((mask &= ~map[off]) != 0UL)
  207                         return (off * NDENTRIES + ffsl(mask) - 1);
  208                 ++off;
  209         }
  210         for (maxoff = NDSLOTS(size); off < maxoff; ++off)
  211                 if (map[off] != ~0UL)
  212                         return (off * NDENTRIES + ffsl(~map[off]) - 1);
  213         return (size);
  214 }
  215 
  216 /*
  217  * Find the highest non-zero bit in the given bitmap, starting at low and
  218  * not exceeding size - 1.
  219  */
  220 static int
  221 fd_last_used(struct filedesc *fdp, int low, int size)
  222 {
  223         NDSLOTTYPE *map = fdp->fd_map;
  224         NDSLOTTYPE mask;
  225         int off, minoff;
  226 
  227         if (low >= size)
  228                 return (-1);
  229 
  230         off = NDSLOT(size);
  231         if (size % NDENTRIES) {
  232                 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
  233                 if ((mask &= map[off]) != 0)
  234                         return (off * NDENTRIES + flsl(mask) - 1);
  235                 --off;
  236         }
  237         for (minoff = NDSLOT(low); off >= minoff; --off)
  238                 if (map[off] != 0)
  239                         return (off * NDENTRIES + flsl(map[off]) - 1);
  240         return (low - 1);
  241 }
  242 
  243 static int
  244 fdisused(struct filedesc *fdp, int fd)
  245 {
  246         KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
  247             ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
  248         return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
  249 }
  250 
  251 /*
  252  * Mark a file descriptor as used.
  253  */
  254 static void
  255 fdused(struct filedesc *fdp, int fd)
  256 {
  257 
  258         FILEDESC_XLOCK_ASSERT(fdp);
  259         KASSERT(!fdisused(fdp, fd),
  260             ("fd already used"));
  261 
  262         fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
  263         if (fd > fdp->fd_lastfile)
  264                 fdp->fd_lastfile = fd;
  265         if (fd == fdp->fd_freefile)
  266                 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
  267 }
  268 
  269 /*
  270  * Mark a file descriptor as unused.
  271  */
  272 static void
  273 fdunused(struct filedesc *fdp, int fd)
  274 {
  275 
  276         FILEDESC_XLOCK_ASSERT(fdp);
  277         KASSERT(fdisused(fdp, fd),
  278             ("fd is already unused"));
  279         KASSERT(fdp->fd_ofiles[fd] == NULL,
  280             ("fd is still in use"));
  281 
  282         fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
  283         if (fd < fdp->fd_freefile)
  284                 fdp->fd_freefile = fd;
  285         if (fd == fdp->fd_lastfile)
  286                 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
  287 }
  288 
  289 /*
  290  * System calls on descriptors.
  291  */
  292 #ifndef _SYS_SYSPROTO_H_
  293 struct getdtablesize_args {
  294         int     dummy;
  295 };
  296 #endif
  297 /* ARGSUSED */
  298 int
  299 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
  300 {
  301         struct proc *p = td->td_proc;
  302         uint64_t lim;
  303 
  304         PROC_LOCK(p);
  305         td->td_retval[0] =
  306             min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
  307         lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
  308         PROC_UNLOCK(p);
  309         if (lim < td->td_retval[0])
  310                 td->td_retval[0] = lim;
  311         return (0);
  312 }
  313 
  314 /*
  315  * Duplicate a file descriptor to a particular value.
  316  *
  317  * Note: keep in mind that a potential race condition exists when closing
  318  * descriptors from a shared descriptor table (via rfork).
  319  */
  320 #ifndef _SYS_SYSPROTO_H_
  321 struct dup2_args {
  322         u_int   from;
  323         u_int   to;
  324 };
  325 #endif
  326 /* ARGSUSED */
  327 int
  328 sys_dup2(struct thread *td, struct dup2_args *uap)
  329 {
  330 
  331         return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
  332                     td->td_retval));
  333 }
  334 
  335 /*
  336  * Duplicate a file descriptor.
  337  */
  338 #ifndef _SYS_SYSPROTO_H_
  339 struct dup_args {
  340         u_int   fd;
  341 };
  342 #endif
  343 /* ARGSUSED */
  344 int
  345 sys_dup(struct thread *td, struct dup_args *uap)
  346 {
  347 
  348         return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
  349 }
  350 
  351 /*
  352  * The file control system call.
  353  */
  354 #ifndef _SYS_SYSPROTO_H_
  355 struct fcntl_args {
  356         int     fd;
  357         int     cmd;
  358         long    arg;
  359 };
  360 #endif
  361 /* ARGSUSED */
  362 int
  363 sys_fcntl(struct thread *td, struct fcntl_args *uap)
  364 {
  365         struct flock fl;
  366         struct oflock ofl;
  367         intptr_t arg;
  368         int error;
  369         int cmd;
  370 
  371         error = 0;
  372         cmd = uap->cmd;
  373         switch (uap->cmd) {
  374         case F_OGETLK:
  375         case F_OSETLK:
  376         case F_OSETLKW:
  377                 /*
  378                  * Convert old flock structure to new.
  379                  */
  380                 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
  381                 fl.l_start = ofl.l_start;
  382                 fl.l_len = ofl.l_len;
  383                 fl.l_pid = ofl.l_pid;
  384                 fl.l_type = ofl.l_type;
  385                 fl.l_whence = ofl.l_whence;
  386                 fl.l_sysid = 0;
  387 
  388                 switch (uap->cmd) {
  389                 case F_OGETLK:
  390                     cmd = F_GETLK;
  391                     break;
  392                 case F_OSETLK:
  393                     cmd = F_SETLK;
  394                     break;
  395                 case F_OSETLKW:
  396                     cmd = F_SETLKW;
  397                     break;
  398                 }
  399                 arg = (intptr_t)&fl;
  400                 break;
  401         case F_GETLK:
  402         case F_SETLK:
  403         case F_SETLKW:
  404         case F_SETLK_REMOTE:
  405                 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
  406                 arg = (intptr_t)&fl;
  407                 break;
  408         default:
  409                 arg = uap->arg;
  410                 break;
  411         }
  412         if (error)
  413                 return (error);
  414         error = kern_fcntl(td, uap->fd, cmd, arg);
  415         if (error)
  416                 return (error);
  417         if (uap->cmd == F_OGETLK) {
  418                 ofl.l_start = fl.l_start;
  419                 ofl.l_len = fl.l_len;
  420                 ofl.l_pid = fl.l_pid;
  421                 ofl.l_type = fl.l_type;
  422                 ofl.l_whence = fl.l_whence;
  423                 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
  424         } else if (uap->cmd == F_GETLK) {
  425                 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
  426         }
  427         return (error);
  428 }
  429 
  430 static inline struct file *
  431 fdtofp(int fd, struct filedesc *fdp)
  432 {
  433         struct file *fp;
  434 
  435         FILEDESC_LOCK_ASSERT(fdp);
  436         if ((unsigned)fd >= fdp->fd_nfiles ||
  437             (fp = fdp->fd_ofiles[fd]) == NULL)
  438                 return (NULL);
  439         return (fp);
  440 }
  441 
  442 static inline int
  443 fdunwrap(int fd, cap_rights_t rights, struct filedesc *fdp, struct file **fpp)
  444 {
  445 
  446         *fpp = fdtofp(fd, fdp);
  447         if (*fpp == NULL)
  448                 return (EBADF);
  449 
  450 #ifdef CAPABILITIES
  451         if ((*fpp)->f_type == DTYPE_CAPABILITY) {
  452                 int err = cap_funwrap(*fpp, rights, fpp);
  453                 if (err != 0) {
  454                         *fpp = NULL;
  455                         return (err);
  456                 }
  457         }
  458 #endif /* CAPABILITIES */
  459         return (0);
  460 }
  461 
  462 int
  463 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
  464 {
  465         struct filedesc *fdp;
  466         struct flock *flp;
  467         struct file *fp;
  468         struct proc *p;
  469         char *pop;
  470         struct vnode *vp;
  471         int error, flg, tmp;
  472         int vfslocked;
  473         u_int old, new;
  474         uint64_t bsize;
  475 
  476         vfslocked = 0;
  477         error = 0;
  478         flg = F_POSIX;
  479         p = td->td_proc;
  480         fdp = p->p_fd;
  481 
  482         switch (cmd) {
  483         case F_DUPFD:
  484                 tmp = arg;
  485                 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
  486                 break;
  487 
  488         case F_DUP2FD:
  489                 tmp = arg;
  490                 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
  491                 break;
  492 
  493         case F_GETFD:
  494                 FILEDESC_SLOCK(fdp);
  495                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  496                         FILEDESC_SUNLOCK(fdp);
  497                         error = EBADF;
  498                         break;
  499                 }
  500                 pop = &fdp->fd_ofileflags[fd];
  501                 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
  502                 FILEDESC_SUNLOCK(fdp);
  503                 break;
  504 
  505         case F_SETFD:
  506                 FILEDESC_XLOCK(fdp);
  507                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  508                         FILEDESC_XUNLOCK(fdp);
  509                         error = EBADF;
  510                         break;
  511                 }
  512                 pop = &fdp->fd_ofileflags[fd];
  513                 *pop = (*pop &~ UF_EXCLOSE) |
  514                     (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
  515                 FILEDESC_XUNLOCK(fdp);
  516                 break;
  517 
  518         case F_GETFL:
  519                 FILEDESC_SLOCK(fdp);
  520                 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
  521                 if (error != 0) {
  522                         FILEDESC_SUNLOCK(fdp);
  523                         break;
  524                 }
  525                 td->td_retval[0] = OFLAGS(fp->f_flag);
  526                 FILEDESC_SUNLOCK(fdp);
  527                 break;
  528 
  529         case F_SETFL:
  530                 FILEDESC_SLOCK(fdp);
  531                 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
  532                 if (error != 0) {
  533                         FILEDESC_SUNLOCK(fdp);
  534                         break;
  535                 }
  536                 fhold(fp);
  537                 FILEDESC_SUNLOCK(fdp);
  538                 do {
  539                         tmp = flg = fp->f_flag;
  540                         tmp &= ~FCNTLFLAGS;
  541                         tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
  542                 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
  543                 tmp = fp->f_flag & FNONBLOCK;
  544                 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  545                 if (error) {
  546                         fdrop(fp, td);
  547                         break;
  548                 }
  549                 tmp = fp->f_flag & FASYNC;
  550                 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
  551                 if (error == 0) {
  552                         fdrop(fp, td);
  553                         break;
  554                 }
  555                 atomic_clear_int(&fp->f_flag, FNONBLOCK);
  556                 tmp = 0;
  557                 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  558                 fdrop(fp, td);
  559                 break;
  560 
  561         case F_GETOWN:
  562                 FILEDESC_SLOCK(fdp);
  563                 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
  564                 if (error != 0) {
  565                         FILEDESC_SUNLOCK(fdp);
  566                         break;
  567                 }
  568                 fhold(fp);
  569                 FILEDESC_SUNLOCK(fdp);
  570                 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
  571                 if (error == 0)
  572                         td->td_retval[0] = tmp;
  573                 fdrop(fp, td);
  574                 break;
  575 
  576         case F_SETOWN:
  577                 FILEDESC_SLOCK(fdp);
  578                 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
  579                 if (error != 0) {
  580                         FILEDESC_SUNLOCK(fdp);
  581                         break;
  582                 }
  583                 fhold(fp);
  584                 FILEDESC_SUNLOCK(fdp);
  585                 tmp = arg;
  586                 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
  587                 fdrop(fp, td);
  588                 break;
  589 
  590         case F_SETLK_REMOTE:
  591                 error = priv_check(td, PRIV_NFS_LOCKD);
  592                 if (error)
  593                         return (error);
  594                 flg = F_REMOTE;
  595                 goto do_setlk;
  596 
  597         case F_SETLKW:
  598                 flg |= F_WAIT;
  599                 /* FALLTHROUGH F_SETLK */
  600 
  601         case F_SETLK:
  602         do_setlk:
  603                 FILEDESC_SLOCK(fdp);
  604                 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
  605                 if (error != 0) {
  606                         FILEDESC_SUNLOCK(fdp);
  607                         break;
  608                 }
  609                 if (fp->f_type != DTYPE_VNODE) {
  610                         FILEDESC_SUNLOCK(fdp);
  611                         error = EBADF;
  612                         break;
  613                 }
  614                 flp = (struct flock *)arg;
  615                 if (flp->l_whence == SEEK_CUR) {
  616                         if (fp->f_offset < 0 ||
  617                             (flp->l_start > 0 &&
  618                              fp->f_offset > OFF_MAX - flp->l_start)) {
  619                                 FILEDESC_SUNLOCK(fdp);
  620                                 error = EOVERFLOW;
  621                                 break;
  622                         }
  623                         flp->l_start += fp->f_offset;
  624                 }
  625 
  626                 /*
  627                  * VOP_ADVLOCK() may block.
  628                  */
  629                 fhold(fp);
  630                 FILEDESC_SUNLOCK(fdp);
  631                 vp = fp->f_vnode;
  632                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  633                 switch (flp->l_type) {
  634                 case F_RDLCK:
  635                         if ((fp->f_flag & FREAD) == 0) {
  636                                 error = EBADF;
  637                                 break;
  638                         }
  639                         PROC_LOCK(p->p_leader);
  640                         p->p_leader->p_flag |= P_ADVLOCK;
  641                         PROC_UNLOCK(p->p_leader);
  642                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  643                             flp, flg);
  644                         break;
  645                 case F_WRLCK:
  646                         if ((fp->f_flag & FWRITE) == 0) {
  647                                 error = EBADF;
  648                                 break;
  649                         }
  650                         PROC_LOCK(p->p_leader);
  651                         p->p_leader->p_flag |= P_ADVLOCK;
  652                         PROC_UNLOCK(p->p_leader);
  653                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  654                             flp, flg);
  655                         break;
  656                 case F_UNLCK:
  657                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
  658                             flp, flg);
  659                         break;
  660                 case F_UNLCKSYS:
  661                         /*
  662                          * Temporary api for testing remote lock
  663                          * infrastructure.
  664                          */
  665                         if (flg != F_REMOTE) {
  666                                 error = EINVAL;
  667                                 break;
  668                         }
  669                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
  670                             F_UNLCKSYS, flp, flg);
  671                         break;
  672                 default:
  673                         error = EINVAL;
  674                         break;
  675                 }
  676                 VFS_UNLOCK_GIANT(vfslocked);
  677                 vfslocked = 0;
  678                 /* Check for race with close */
  679                 FILEDESC_SLOCK(fdp);
  680                 if ((unsigned) fd >= fdp->fd_nfiles ||
  681                     fp != fdp->fd_ofiles[fd]) {
  682                         FILEDESC_SUNLOCK(fdp);
  683                         flp->l_whence = SEEK_SET;
  684                         flp->l_start = 0;
  685                         flp->l_len = 0;
  686                         flp->l_type = F_UNLCK;
  687                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  688                         (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
  689                                            F_UNLCK, flp, F_POSIX);
  690                         VFS_UNLOCK_GIANT(vfslocked);
  691                         vfslocked = 0;
  692                 } else
  693                         FILEDESC_SUNLOCK(fdp);
  694                 fdrop(fp, td);
  695                 break;
  696 
  697         case F_GETLK:
  698                 FILEDESC_SLOCK(fdp);
  699                 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
  700                 if (error != 0) {
  701                         FILEDESC_SUNLOCK(fdp);
  702                         break;
  703                 }
  704                 if (fp->f_type != DTYPE_VNODE) {
  705                         FILEDESC_SUNLOCK(fdp);
  706                         error = EBADF;
  707                         break;
  708                 }
  709                 flp = (struct flock *)arg;
  710                 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
  711                     flp->l_type != F_UNLCK) {
  712                         FILEDESC_SUNLOCK(fdp);
  713                         error = EINVAL;
  714                         break;
  715                 }
  716                 if (flp->l_whence == SEEK_CUR) {
  717                         if ((flp->l_start > 0 &&
  718                             fp->f_offset > OFF_MAX - flp->l_start) ||
  719                             (flp->l_start < 0 &&
  720                              fp->f_offset < OFF_MIN - flp->l_start)) {
  721                                 FILEDESC_SUNLOCK(fdp);
  722                                 error = EOVERFLOW;
  723                                 break;
  724                         }
  725                         flp->l_start += fp->f_offset;
  726                 }
  727                 /*
  728                  * VOP_ADVLOCK() may block.
  729                  */
  730                 fhold(fp);
  731                 FILEDESC_SUNLOCK(fdp);
  732                 vp = fp->f_vnode;
  733                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  734                 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
  735                     F_POSIX);
  736                 VFS_UNLOCK_GIANT(vfslocked);
  737                 vfslocked = 0;
  738                 fdrop(fp, td);
  739                 break;
  740 
  741         case F_RDAHEAD:
  742                 arg = arg ? 128 * 1024: 0;
  743                 /* FALLTHROUGH */
  744         case F_READAHEAD:
  745                 FILEDESC_SLOCK(fdp);
  746                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  747                         FILEDESC_SUNLOCK(fdp);
  748                         error = EBADF;
  749                         break;
  750                 }
  751                 if (fp->f_type != DTYPE_VNODE) {
  752                         FILEDESC_SUNLOCK(fdp);
  753                         error = EBADF;
  754                         break;
  755                 }
  756                 fhold(fp);
  757                 FILEDESC_SUNLOCK(fdp);
  758                 if (arg != 0) {
  759                         vp = fp->f_vnode;
  760                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  761                         error = vn_lock(vp, LK_SHARED);
  762                         if (error != 0)
  763                                 goto readahead_vnlock_fail;
  764                         bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
  765                         VOP_UNLOCK(vp, 0);
  766                         fp->f_seqcount = (arg + bsize - 1) / bsize;
  767                         do {
  768                                 new = old = fp->f_flag;
  769                                 new |= FRDAHEAD;
  770                         } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
  771 readahead_vnlock_fail:
  772                         VFS_UNLOCK_GIANT(vfslocked);
  773                         vfslocked = 0;
  774                 } else {
  775                         do {
  776                                 new = old = fp->f_flag;
  777                                 new &= ~FRDAHEAD;
  778                         } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
  779                 }
  780                 fdrop(fp, td);
  781                 break;
  782 
  783         default:
  784                 error = EINVAL;
  785                 break;
  786         }
  787         VFS_UNLOCK_GIANT(vfslocked);
  788         return (error);
  789 }
  790 
  791 /*
  792  * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
  793  */
  794 static int
  795 do_dup(struct thread *td, int flags, int old, int new,
  796     register_t *retval)
  797 {
  798         struct filedesc *fdp;
  799         struct proc *p;
  800         struct file *fp;
  801         struct file *delfp;
  802         int error, holdleaders, maxfd;
  803 
  804         p = td->td_proc;
  805         fdp = p->p_fd;
  806 
  807         /*
  808          * Verify we have a valid descriptor to dup from and possibly to
  809          * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
  810          * return EINVAL when the new descriptor is out of bounds.
  811          */
  812         if (old < 0)
  813                 return (EBADF);
  814         if (new < 0)
  815                 return (flags & DUP_FCNTL ? EINVAL : EBADF);
  816         PROC_LOCK(p);
  817         maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
  818         PROC_UNLOCK(p);
  819         if (new >= maxfd)
  820                 return (flags & DUP_FCNTL ? EINVAL : EBADF);
  821 
  822         FILEDESC_XLOCK(fdp);
  823         if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
  824                 FILEDESC_XUNLOCK(fdp);
  825                 return (EBADF);
  826         }
  827         if (flags & DUP_FIXED && old == new) {
  828                 *retval = new;
  829                 FILEDESC_XUNLOCK(fdp);
  830                 return (0);
  831         }
  832         fp = fdp->fd_ofiles[old];
  833         fhold(fp);
  834 
  835         /*
  836          * If the caller specified a file descriptor, make sure the file
  837          * table is large enough to hold it, and grab it.  Otherwise, just
  838          * allocate a new descriptor the usual way.  Since the filedesc
  839          * lock may be temporarily dropped in the process, we have to look
  840          * out for a race.
  841          */
  842         if (flags & DUP_FIXED) {
  843                 if (new >= fdp->fd_nfiles) {
  844                         /*
  845                          * The resource limits are here instead of e.g.
  846                          * fdalloc(), because the file descriptor table may be
  847                          * shared between processes, so we can't really use
  848                          * racct_add()/racct_sub().  Instead of counting the
  849                          * number of actually allocated descriptors, just put
  850                          * the limit on the size of the file descriptor table.
  851                          */
  852 #ifdef RACCT
  853                         PROC_LOCK(p);
  854                         error = racct_set(p, RACCT_NOFILE, new + 1);
  855                         PROC_UNLOCK(p);
  856                         if (error != 0) {
  857                                 FILEDESC_XUNLOCK(fdp);
  858                                 fdrop(fp, td);
  859                                 return (EMFILE);
  860                         }
  861 #endif
  862                         fdgrowtable(fdp, new + 1);
  863                 }
  864                 if (fdp->fd_ofiles[new] == NULL)
  865                         fdused(fdp, new);
  866         } else {
  867                 if ((error = fdalloc(td, new, &new)) != 0) {
  868                         FILEDESC_XUNLOCK(fdp);
  869                         fdrop(fp, td);
  870                         return (error);
  871                 }
  872         }
  873 
  874         /*
  875          * If the old file changed out from under us then treat it as a
  876          * bad file descriptor.  Userland should do its own locking to
  877          * avoid this case.
  878          */
  879         if (fdp->fd_ofiles[old] != fp) {
  880                 /* we've allocated a descriptor which we won't use */
  881                 if (fdp->fd_ofiles[new] == NULL)
  882                         fdunused(fdp, new);
  883                 FILEDESC_XUNLOCK(fdp);
  884                 fdrop(fp, td);
  885                 return (EBADF);
  886         }
  887         KASSERT(old != new,
  888             ("new fd is same as old"));
  889 
  890         /*
  891          * Save info on the descriptor being overwritten.  We cannot close
  892          * it without introducing an ownership race for the slot, since we
  893          * need to drop the filedesc lock to call closef().
  894          *
  895          * XXX this duplicates parts of close().
  896          */
  897         delfp = fdp->fd_ofiles[new];
  898         holdleaders = 0;
  899         if (delfp != NULL) {
  900                 if (td->td_proc->p_fdtol != NULL) {
  901                         /*
  902                          * Ask fdfree() to sleep to ensure that all relevant
  903                          * process leaders can be traversed in closef().
  904                          */
  905                         fdp->fd_holdleaderscount++;
  906                         holdleaders = 1;
  907                 }
  908         }
  909 
  910         /*
  911          * Duplicate the source descriptor
  912          */
  913         fdp->fd_ofiles[new] = fp;
  914         fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
  915         if (new > fdp->fd_lastfile)
  916                 fdp->fd_lastfile = new;
  917         *retval = new;
  918 
  919         /*
  920          * If we dup'd over a valid file, we now own the reference to it
  921          * and must dispose of it using closef() semantics (as if a
  922          * close() were performed on it).
  923          *
  924          * XXX this duplicates parts of close().
  925          */
  926         if (delfp != NULL) {
  927                 knote_fdclose(td, new);
  928                 if (delfp->f_type == DTYPE_MQUEUE)
  929                         mq_fdclose(td, new, delfp);
  930                 FILEDESC_XUNLOCK(fdp);
  931                 (void) closef(delfp, td);
  932                 if (holdleaders) {
  933                         FILEDESC_XLOCK(fdp);
  934                         fdp->fd_holdleaderscount--;
  935                         if (fdp->fd_holdleaderscount == 0 &&
  936                             fdp->fd_holdleaderswakeup != 0) {
  937                                 fdp->fd_holdleaderswakeup = 0;
  938                                 wakeup(&fdp->fd_holdleaderscount);
  939                         }
  940                         FILEDESC_XUNLOCK(fdp);
  941                 }
  942         } else {
  943                 FILEDESC_XUNLOCK(fdp);
  944         }
  945         return (0);
  946 }
  947 
  948 /*
  949  * If sigio is on the list associated with a process or process group,
  950  * disable signalling from the device, remove sigio from the list and
  951  * free sigio.
  952  */
  953 void
  954 funsetown(struct sigio **sigiop)
  955 {
  956         struct sigio *sigio;
  957 
  958         SIGIO_LOCK();
  959         sigio = *sigiop;
  960         if (sigio == NULL) {
  961                 SIGIO_UNLOCK();
  962                 return;
  963         }
  964         *(sigio->sio_myref) = NULL;
  965         if ((sigio)->sio_pgid < 0) {
  966                 struct pgrp *pg = (sigio)->sio_pgrp;
  967                 PGRP_LOCK(pg);
  968                 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
  969                              sigio, sio_pgsigio);
  970                 PGRP_UNLOCK(pg);
  971         } else {
  972                 struct proc *p = (sigio)->sio_proc;
  973                 PROC_LOCK(p);
  974                 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
  975                              sigio, sio_pgsigio);
  976                 PROC_UNLOCK(p);
  977         }
  978         SIGIO_UNLOCK();
  979         crfree(sigio->sio_ucred);
  980         free(sigio, M_SIGIO);
  981 }
  982 
  983 /*
  984  * Free a list of sigio structures.
  985  * We only need to lock the SIGIO_LOCK because we have made ourselves
  986  * inaccessible to callers of fsetown and therefore do not need to lock
  987  * the proc or pgrp struct for the list manipulation.
  988  */
  989 void
  990 funsetownlst(struct sigiolst *sigiolst)
  991 {
  992         struct proc *p;
  993         struct pgrp *pg;
  994         struct sigio *sigio;
  995 
  996         sigio = SLIST_FIRST(sigiolst);
  997         if (sigio == NULL)
  998                 return;
  999         p = NULL;
 1000         pg = NULL;
 1001 
 1002         /*
 1003          * Every entry of the list should belong
 1004          * to a single proc or pgrp.
 1005          */
 1006         if (sigio->sio_pgid < 0) {
 1007                 pg = sigio->sio_pgrp;
 1008                 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
 1009         } else /* if (sigio->sio_pgid > 0) */ {
 1010                 p = sigio->sio_proc;
 1011                 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
 1012         }
 1013 
 1014         SIGIO_LOCK();
 1015         while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
 1016                 *(sigio->sio_myref) = NULL;
 1017                 if (pg != NULL) {
 1018                         KASSERT(sigio->sio_pgid < 0,
 1019                             ("Proc sigio in pgrp sigio list"));
 1020                         KASSERT(sigio->sio_pgrp == pg,
 1021                             ("Bogus pgrp in sigio list"));
 1022                         PGRP_LOCK(pg);
 1023                         SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
 1024                             sio_pgsigio);
 1025                         PGRP_UNLOCK(pg);
 1026                 } else /* if (p != NULL) */ {
 1027                         KASSERT(sigio->sio_pgid > 0,
 1028                             ("Pgrp sigio in proc sigio list"));
 1029                         KASSERT(sigio->sio_proc == p,
 1030                             ("Bogus proc in sigio list"));
 1031                         PROC_LOCK(p);
 1032                         SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
 1033                             sio_pgsigio);
 1034                         PROC_UNLOCK(p);
 1035                 }
 1036                 SIGIO_UNLOCK();
 1037                 crfree(sigio->sio_ucred);
 1038                 free(sigio, M_SIGIO);
 1039                 SIGIO_LOCK();
 1040         }
 1041         SIGIO_UNLOCK();
 1042 }
 1043 
 1044 /*
 1045  * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
 1046  *
 1047  * After permission checking, add a sigio structure to the sigio list for
 1048  * the process or process group.
 1049  */
 1050 int
 1051 fsetown(pid_t pgid, struct sigio **sigiop)
 1052 {
 1053         struct proc *proc;
 1054         struct pgrp *pgrp;
 1055         struct sigio *sigio;
 1056         int ret;
 1057 
 1058         if (pgid == 0) {
 1059                 funsetown(sigiop);
 1060                 return (0);
 1061         }
 1062 
 1063         ret = 0;
 1064 
 1065         /* Allocate and fill in the new sigio out of locks. */
 1066         sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
 1067         sigio->sio_pgid = pgid;
 1068         sigio->sio_ucred = crhold(curthread->td_ucred);
 1069         sigio->sio_myref = sigiop;
 1070 
 1071         sx_slock(&proctree_lock);
 1072         if (pgid > 0) {
 1073                 proc = pfind(pgid);
 1074                 if (proc == NULL) {
 1075                         ret = ESRCH;
 1076                         goto fail;
 1077                 }
 1078 
 1079                 /*
 1080                  * Policy - Don't allow a process to FSETOWN a process
 1081                  * in another session.
 1082                  *
 1083                  * Remove this test to allow maximum flexibility or
 1084                  * restrict FSETOWN to the current process or process
 1085                  * group for maximum safety.
 1086                  */
 1087                 PROC_UNLOCK(proc);
 1088                 if (proc->p_session != curthread->td_proc->p_session) {
 1089                         ret = EPERM;
 1090                         goto fail;
 1091                 }
 1092 
 1093                 pgrp = NULL;
 1094         } else /* if (pgid < 0) */ {
 1095                 pgrp = pgfind(-pgid);
 1096                 if (pgrp == NULL) {
 1097                         ret = ESRCH;
 1098                         goto fail;
 1099                 }
 1100                 PGRP_UNLOCK(pgrp);
 1101 
 1102                 /*
 1103                  * Policy - Don't allow a process to FSETOWN a process
 1104                  * in another session.
 1105                  *
 1106                  * Remove this test to allow maximum flexibility or
 1107                  * restrict FSETOWN to the current process or process
 1108                  * group for maximum safety.
 1109                  */
 1110                 if (pgrp->pg_session != curthread->td_proc->p_session) {
 1111                         ret = EPERM;
 1112                         goto fail;
 1113                 }
 1114 
 1115                 proc = NULL;
 1116         }
 1117         funsetown(sigiop);
 1118         if (pgid > 0) {
 1119                 PROC_LOCK(proc);
 1120                 /*
 1121                  * Since funsetownlst() is called without the proctree
 1122                  * locked, we need to check for P_WEXIT.
 1123                  * XXX: is ESRCH correct?
 1124                  */
 1125                 if ((proc->p_flag & P_WEXIT) != 0) {
 1126                         PROC_UNLOCK(proc);
 1127                         ret = ESRCH;
 1128                         goto fail;
 1129                 }
 1130                 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
 1131                 sigio->sio_proc = proc;
 1132                 PROC_UNLOCK(proc);
 1133         } else {
 1134                 PGRP_LOCK(pgrp);
 1135                 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
 1136                 sigio->sio_pgrp = pgrp;
 1137                 PGRP_UNLOCK(pgrp);
 1138         }
 1139         sx_sunlock(&proctree_lock);
 1140         SIGIO_LOCK();
 1141         *sigiop = sigio;
 1142         SIGIO_UNLOCK();
 1143         return (0);
 1144 
 1145 fail:
 1146         sx_sunlock(&proctree_lock);
 1147         crfree(sigio->sio_ucred);
 1148         free(sigio, M_SIGIO);
 1149         return (ret);
 1150 }
 1151 
 1152 /*
 1153  * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
 1154  */
 1155 pid_t
 1156 fgetown(sigiop)
 1157         struct sigio **sigiop;
 1158 {
 1159         pid_t pgid;
 1160 
 1161         SIGIO_LOCK();
 1162         pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
 1163         SIGIO_UNLOCK();
 1164         return (pgid);
 1165 }
 1166 
 1167 /*
 1168  * Close a file descriptor.
 1169  */
 1170 #ifndef _SYS_SYSPROTO_H_
 1171 struct close_args {
 1172         int     fd;
 1173 };
 1174 #endif
 1175 /* ARGSUSED */
 1176 int
 1177 sys_close(td, uap)
 1178         struct thread *td;
 1179         struct close_args *uap;
 1180 {
 1181 
 1182         return (kern_close(td, uap->fd));
 1183 }
 1184 
 1185 int
 1186 kern_close(td, fd)
 1187         struct thread *td;
 1188         int fd;
 1189 {
 1190         struct filedesc *fdp;
 1191         struct file *fp, *fp_object;
 1192         int error;
 1193         int holdleaders;
 1194 
 1195         error = 0;
 1196         holdleaders = 0;
 1197         fdp = td->td_proc->p_fd;
 1198 
 1199         AUDIT_SYSCLOSE(td, fd);
 1200 
 1201         FILEDESC_XLOCK(fdp);
 1202         if ((unsigned)fd >= fdp->fd_nfiles ||
 1203             (fp = fdp->fd_ofiles[fd]) == NULL) {
 1204                 FILEDESC_XUNLOCK(fdp);
 1205                 return (EBADF);
 1206         }
 1207         fdp->fd_ofiles[fd] = NULL;
 1208         fdp->fd_ofileflags[fd] = 0;
 1209         fdunused(fdp, fd);
 1210         if (td->td_proc->p_fdtol != NULL) {
 1211                 /*
 1212                  * Ask fdfree() to sleep to ensure that all relevant
 1213                  * process leaders can be traversed in closef().
 1214                  */
 1215                 fdp->fd_holdleaderscount++;
 1216                 holdleaders = 1;
 1217         }
 1218 
 1219         /*
 1220          * We now hold the fp reference that used to be owned by the
 1221          * descriptor array.  We have to unlock the FILEDESC *AFTER*
 1222          * knote_fdclose to prevent a race of the fd getting opened, a knote
 1223          * added, and deleteing a knote for the new fd.
 1224          */
 1225         knote_fdclose(td, fd);
 1226 
 1227         /*
 1228          * When we're closing an fd with a capability, we need to notify
 1229          * mqueue if the underlying object is of type mqueue.
 1230          */
 1231         (void)cap_funwrap(fp, 0, &fp_object);
 1232         if (fp_object->f_type == DTYPE_MQUEUE)
 1233                 mq_fdclose(td, fd, fp_object);
 1234         FILEDESC_XUNLOCK(fdp);
 1235 
 1236         error = closef(fp, td);
 1237         if (holdleaders) {
 1238                 FILEDESC_XLOCK(fdp);
 1239                 fdp->fd_holdleaderscount--;
 1240                 if (fdp->fd_holdleaderscount == 0 &&
 1241                     fdp->fd_holdleaderswakeup != 0) {
 1242                         fdp->fd_holdleaderswakeup = 0;
 1243                         wakeup(&fdp->fd_holdleaderscount);
 1244                 }
 1245                 FILEDESC_XUNLOCK(fdp);
 1246         }
 1247         return (error);
 1248 }
 1249 
 1250 /*
 1251  * Close open file descriptors.
 1252  */
 1253 #ifndef _SYS_SYSPROTO_H_
 1254 struct closefrom_args {
 1255         int     lowfd;
 1256 };
 1257 #endif
 1258 /* ARGSUSED */
 1259 int
 1260 sys_closefrom(struct thread *td, struct closefrom_args *uap)
 1261 {
 1262         struct filedesc *fdp;
 1263         int fd;
 1264 
 1265         fdp = td->td_proc->p_fd;
 1266         AUDIT_ARG_FD(uap->lowfd);
 1267 
 1268         /*
 1269          * Treat negative starting file descriptor values identical to
 1270          * closefrom(0) which closes all files.
 1271          */
 1272         if (uap->lowfd < 0)
 1273                 uap->lowfd = 0;
 1274         FILEDESC_SLOCK(fdp);
 1275         for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
 1276                 if (fdp->fd_ofiles[fd] != NULL) {
 1277                         FILEDESC_SUNLOCK(fdp);
 1278                         (void)kern_close(td, fd);
 1279                         FILEDESC_SLOCK(fdp);
 1280                 }
 1281         }
 1282         FILEDESC_SUNLOCK(fdp);
 1283         return (0);
 1284 }
 1285 
 1286 #if defined(COMPAT_43)
 1287 /*
 1288  * Return status information about a file descriptor.
 1289  */
 1290 #ifndef _SYS_SYSPROTO_H_
 1291 struct ofstat_args {
 1292         int     fd;
 1293         struct  ostat *sb;
 1294 };
 1295 #endif
 1296 /* ARGSUSED */
 1297 int
 1298 ofstat(struct thread *td, struct ofstat_args *uap)
 1299 {
 1300         struct ostat oub;
 1301         struct stat ub;
 1302         int error;
 1303 
 1304         error = kern_fstat(td, uap->fd, &ub);
 1305         if (error == 0) {
 1306                 cvtstat(&ub, &oub);
 1307                 error = copyout(&oub, uap->sb, sizeof(oub));
 1308         }
 1309         return (error);
 1310 }
 1311 #endif /* COMPAT_43 */
 1312 
 1313 /*
 1314  * Return status information about a file descriptor.
 1315  */
 1316 #ifndef _SYS_SYSPROTO_H_
 1317 struct fstat_args {
 1318         int     fd;
 1319         struct  stat *sb;
 1320 };
 1321 #endif
 1322 /* ARGSUSED */
 1323 int
 1324 sys_fstat(struct thread *td, struct fstat_args *uap)
 1325 {
 1326         struct stat ub;
 1327         int error;
 1328 
 1329         error = kern_fstat(td, uap->fd, &ub);
 1330         if (error == 0)
 1331                 error = copyout(&ub, uap->sb, sizeof(ub));
 1332         return (error);
 1333 }
 1334 
 1335 int
 1336 kern_fstat(struct thread *td, int fd, struct stat *sbp)
 1337 {
 1338         struct file *fp;
 1339         int error;
 1340 
 1341         AUDIT_ARG_FD(fd);
 1342 
 1343         if ((error = fget(td, fd, CAP_FSTAT, &fp)) != 0)
 1344                 return (error);
 1345 
 1346         AUDIT_ARG_FILE(td->td_proc, fp);
 1347 
 1348         error = fo_stat(fp, sbp, td->td_ucred, td);
 1349         fdrop(fp, td);
 1350 #ifdef KTRACE
 1351         if (error == 0 && KTRPOINT(td, KTR_STRUCT))
 1352                 ktrstat(sbp);
 1353 #endif
 1354         return (error);
 1355 }
 1356 
 1357 /*
 1358  * Return status information about a file descriptor.
 1359  */
 1360 #ifndef _SYS_SYSPROTO_H_
 1361 struct nfstat_args {
 1362         int     fd;
 1363         struct  nstat *sb;
 1364 };
 1365 #endif
 1366 /* ARGSUSED */
 1367 int
 1368 sys_nfstat(struct thread *td, struct nfstat_args *uap)
 1369 {
 1370         struct nstat nub;
 1371         struct stat ub;
 1372         int error;
 1373 
 1374         error = kern_fstat(td, uap->fd, &ub);
 1375         if (error == 0) {
 1376                 cvtnstat(&ub, &nub);
 1377                 error = copyout(&nub, uap->sb, sizeof(nub));
 1378         }
 1379         return (error);
 1380 }
 1381 
 1382 /*
 1383  * Return pathconf information about a file descriptor.
 1384  */
 1385 #ifndef _SYS_SYSPROTO_H_
 1386 struct fpathconf_args {
 1387         int     fd;
 1388         int     name;
 1389 };
 1390 #endif
 1391 /* ARGSUSED */
 1392 int
 1393 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
 1394 {
 1395         struct file *fp;
 1396         struct vnode *vp;
 1397         int error;
 1398 
 1399         if ((error = fget(td, uap->fd, CAP_FPATHCONF, &fp)) != 0)
 1400                 return (error);
 1401 
 1402         /* If asynchronous I/O is available, it works for all descriptors. */
 1403         if (uap->name == _PC_ASYNC_IO) {
 1404                 td->td_retval[0] = async_io_version;
 1405                 goto out;
 1406         }
 1407         vp = fp->f_vnode;
 1408         if (vp != NULL) {
 1409                 int vfslocked;
 1410                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1411                 vn_lock(vp, LK_SHARED | LK_RETRY);
 1412                 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
 1413                 VOP_UNLOCK(vp, 0);
 1414                 VFS_UNLOCK_GIANT(vfslocked);
 1415         } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
 1416                 if (uap->name != _PC_PIPE_BUF) {
 1417                         error = EINVAL;
 1418                 } else {
 1419                         td->td_retval[0] = PIPE_BUF;
 1420                 error = 0;
 1421                 }
 1422         } else {
 1423                 error = EOPNOTSUPP;
 1424         }
 1425 out:
 1426         fdrop(fp, td);
 1427         return (error);
 1428 }
 1429 
 1430 /*
 1431  * Grow the file table to accomodate (at least) nfd descriptors.  This may
 1432  * block and drop the filedesc lock, but it will reacquire it before
 1433  * returning.
 1434  */
 1435 static void
 1436 fdgrowtable(struct filedesc *fdp, int nfd)
 1437 {
 1438         struct filedesc0 *fdp0;
 1439         struct freetable *fo;
 1440         struct file **ntable;
 1441         struct file **otable;
 1442         char *nfileflags;
 1443         int nnfiles, onfiles;
 1444         NDSLOTTYPE *nmap;
 1445 
 1446         FILEDESC_XLOCK_ASSERT(fdp);
 1447 
 1448         KASSERT(fdp->fd_nfiles > 0,
 1449             ("zero-length file table"));
 1450 
 1451         /* compute the size of the new table */
 1452         onfiles = fdp->fd_nfiles;
 1453         nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
 1454         if (nnfiles <= onfiles)
 1455                 /* the table is already large enough */
 1456                 return;
 1457 
 1458         /* allocate a new table and (if required) new bitmaps */
 1459         FILEDESC_XUNLOCK(fdp);
 1460         ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable),
 1461             M_FILEDESC, M_ZERO | M_WAITOK);
 1462         nfileflags = (char *)&ntable[nnfiles];
 1463         if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
 1464                 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE,
 1465                     M_FILEDESC, M_ZERO | M_WAITOK);
 1466         else
 1467                 nmap = NULL;
 1468         FILEDESC_XLOCK(fdp);
 1469 
 1470         /*
 1471          * We now have new tables ready to go.  Since we dropped the
 1472          * filedesc lock to call malloc(), watch out for a race.
 1473          */
 1474         onfiles = fdp->fd_nfiles;
 1475         if (onfiles >= nnfiles) {
 1476                 /* we lost the race, but that's OK */
 1477                 free(ntable, M_FILEDESC);
 1478                 if (nmap != NULL)
 1479                         free(nmap, M_FILEDESC);
 1480                 return;
 1481         }
 1482         bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
 1483         bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
 1484         otable = fdp->fd_ofiles;
 1485         fdp->fd_ofileflags = nfileflags;
 1486         fdp->fd_ofiles = ntable;
 1487         /*
 1488          * We must preserve ofiles until the process exits because we can't
 1489          * be certain that no threads have references to the old table via
 1490          * _fget().
 1491          */
 1492         if (onfiles > NDFILE) {
 1493                 fo = (struct freetable *)&otable[onfiles];
 1494                 fdp0 = (struct filedesc0 *)fdp;
 1495                 fo->ft_table = otable;
 1496                 SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next);
 1497         }
 1498         if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
 1499                 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
 1500                 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
 1501                         free(fdp->fd_map, M_FILEDESC);
 1502                 fdp->fd_map = nmap;
 1503         }
 1504         fdp->fd_nfiles = nnfiles;
 1505 }
 1506 
 1507 /*
 1508  * Allocate a file descriptor for the process.
 1509  */
 1510 int
 1511 fdalloc(struct thread *td, int minfd, int *result)
 1512 {
 1513         struct proc *p = td->td_proc;
 1514         struct filedesc *fdp = p->p_fd;
 1515         int fd = -1, maxfd;
 1516 #ifdef RACCT
 1517         int error;
 1518 #endif
 1519 
 1520         FILEDESC_XLOCK_ASSERT(fdp);
 1521 
 1522         if (fdp->fd_freefile > minfd)
 1523                 minfd = fdp->fd_freefile;
 1524 
 1525         PROC_LOCK(p);
 1526         maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
 1527         PROC_UNLOCK(p);
 1528 
 1529         /*
 1530          * Search the bitmap for a free descriptor.  If none is found, try
 1531          * to grow the file table.  Keep at it until we either get a file
 1532          * descriptor or run into process or system limits; fdgrowtable()
 1533          * may drop the filedesc lock, so we're in a race.
 1534          */
 1535         for (;;) {
 1536                 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
 1537                 if (fd >= maxfd)
 1538                         return (EMFILE);
 1539                 if (fd < fdp->fd_nfiles)
 1540                         break;
 1541 #ifdef RACCT
 1542                 PROC_LOCK(p);
 1543                 error = racct_set(p, RACCT_NOFILE, min(fdp->fd_nfiles * 2, maxfd));
 1544                 PROC_UNLOCK(p);
 1545                 if (error != 0)
 1546                         return (EMFILE);
 1547 #endif
 1548                 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
 1549         }
 1550 
 1551         /*
 1552          * Perform some sanity checks, then mark the file descriptor as
 1553          * used and return it to the caller.
 1554          */
 1555         KASSERT(!fdisused(fdp, fd),
 1556             ("fd_first_free() returned non-free descriptor"));
 1557         KASSERT(fdp->fd_ofiles[fd] == NULL,
 1558             ("free descriptor isn't"));
 1559         fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
 1560         fdused(fdp, fd);
 1561         *result = fd;
 1562         return (0);
 1563 }
 1564 
 1565 /*
 1566  * Check to see whether n user file descriptors are available to the process
 1567  * p.
 1568  */
 1569 int
 1570 fdavail(struct thread *td, int n)
 1571 {
 1572         struct proc *p = td->td_proc;
 1573         struct filedesc *fdp = td->td_proc->p_fd;
 1574         struct file **fpp;
 1575         int i, lim, last;
 1576 
 1577         FILEDESC_LOCK_ASSERT(fdp);
 1578 
 1579         /*
 1580          * XXX: This is only called from uipc_usrreq.c:unp_externalize();
 1581          *      call racct_add() from there instead of dealing with containers
 1582          *      here.
 1583          */
 1584         PROC_LOCK(p);
 1585         lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
 1586         PROC_UNLOCK(p);
 1587         if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
 1588                 return (1);
 1589         last = min(fdp->fd_nfiles, lim);
 1590         fpp = &fdp->fd_ofiles[fdp->fd_freefile];
 1591         for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
 1592                 if (*fpp == NULL && --n <= 0)
 1593                         return (1);
 1594         }
 1595         return (0);
 1596 }
 1597 
 1598 /*
 1599  * Create a new open file structure and allocate a file decriptor for the
 1600  * process that refers to it.  We add one reference to the file for the
 1601  * descriptor table and one reference for resultfp. This is to prevent us
 1602  * being preempted and the entry in the descriptor table closed after we
 1603  * release the FILEDESC lock.
 1604  */
 1605 int
 1606 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
 1607 {
 1608         struct file *fp;
 1609         int error, fd;
 1610 
 1611         error = falloc_noinstall(td, &fp);
 1612         if (error)
 1613                 return (error);         /* no reference held on error */
 1614 
 1615         error = finstall(td, fp, &fd, flags);
 1616         if (error) {
 1617                 fdrop(fp, td);          /* one reference (fp only) */
 1618                 return (error);
 1619         }
 1620 
 1621         if (resultfp != NULL)
 1622                 *resultfp = fp;         /* copy out result */
 1623         else
 1624                 fdrop(fp, td);          /* release local reference */
 1625 
 1626         if (resultfd != NULL)
 1627                 *resultfd = fd;
 1628 
 1629         return (0);
 1630 }
 1631 
 1632 /*
 1633  * Create a new open file structure without allocating a file descriptor.
 1634  */
 1635 int
 1636 falloc_noinstall(struct thread *td, struct file **resultfp)
 1637 {
 1638         struct file *fp;
 1639         int maxuserfiles = maxfiles - (maxfiles / 20);
 1640         static struct timeval lastfail;
 1641         static int curfail;
 1642 
 1643         KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
 1644 
 1645         if ((openfiles >= maxuserfiles &&
 1646             priv_check(td, PRIV_MAXFILES) != 0) ||
 1647             openfiles >= maxfiles) {
 1648                 if (ppsratecheck(&lastfail, &curfail, 1)) {
 1649                         printf("kern.maxfiles limit exceeded by uid %i, "
 1650                             "please see tuning(7).\n", td->td_ucred->cr_ruid);
 1651                 }
 1652                 return (ENFILE);
 1653         }
 1654         atomic_add_int(&openfiles, 1);
 1655         fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
 1656         refcount_init(&fp->f_count, 1);
 1657         fp->f_cred = crhold(td->td_ucred);
 1658         fp->f_ops = &badfileops;
 1659         fp->f_data = NULL;
 1660         fp->f_vnode = NULL;
 1661         *resultfp = fp;
 1662         return (0);
 1663 }
 1664 
 1665 /*
 1666  * Install a file in a file descriptor table.
 1667  */
 1668 int
 1669 finstall(struct thread *td, struct file *fp, int *fd, int flags)
 1670 {
 1671         struct filedesc *fdp = td->td_proc->p_fd;
 1672         int error;
 1673 
 1674         KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
 1675         KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
 1676 
 1677         FILEDESC_XLOCK(fdp);
 1678         if ((error = fdalloc(td, 0, fd))) {
 1679                 FILEDESC_XUNLOCK(fdp);
 1680                 return (error);
 1681         }
 1682         fhold(fp);
 1683         fdp->fd_ofiles[*fd] = fp;
 1684         if ((flags & O_CLOEXEC) != 0)
 1685                 fdp->fd_ofileflags[*fd] |= UF_EXCLOSE;
 1686         FILEDESC_XUNLOCK(fdp);
 1687         return (0);
 1688 }
 1689 
 1690 /*
 1691  * Build a new filedesc structure from another.
 1692  * Copy the current, root, and jail root vnode references.
 1693  */
 1694 struct filedesc *
 1695 fdinit(struct filedesc *fdp)
 1696 {
 1697         struct filedesc0 *newfdp;
 1698 
 1699         newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
 1700         FILEDESC_LOCK_INIT(&newfdp->fd_fd);
 1701         if (fdp != NULL) {
 1702                 FILEDESC_XLOCK(fdp);
 1703                 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
 1704                 if (newfdp->fd_fd.fd_cdir)
 1705                         VREF(newfdp->fd_fd.fd_cdir);
 1706                 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
 1707                 if (newfdp->fd_fd.fd_rdir)
 1708                         VREF(newfdp->fd_fd.fd_rdir);
 1709                 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
 1710                 if (newfdp->fd_fd.fd_jdir)
 1711                         VREF(newfdp->fd_fd.fd_jdir);
 1712                 FILEDESC_XUNLOCK(fdp);
 1713         }
 1714 
 1715         /* Create the file descriptor table. */
 1716         newfdp->fd_fd.fd_refcnt = 1;
 1717         newfdp->fd_fd.fd_holdcnt = 1;
 1718         newfdp->fd_fd.fd_cmask = CMASK;
 1719         newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
 1720         newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
 1721         newfdp->fd_fd.fd_nfiles = NDFILE;
 1722         newfdp->fd_fd.fd_map = newfdp->fd_dmap;
 1723         newfdp->fd_fd.fd_lastfile = -1;
 1724         return (&newfdp->fd_fd);
 1725 }
 1726 
 1727 static struct filedesc *
 1728 fdhold(struct proc *p)
 1729 {
 1730         struct filedesc *fdp;
 1731 
 1732         mtx_lock(&fdesc_mtx);
 1733         fdp = p->p_fd;
 1734         if (fdp != NULL)
 1735                 fdp->fd_holdcnt++;
 1736         mtx_unlock(&fdesc_mtx);
 1737         return (fdp);
 1738 }
 1739 
 1740 static void
 1741 fddrop(struct filedesc *fdp)
 1742 {
 1743         struct filedesc0 *fdp0;
 1744         struct freetable *ft;
 1745         int i;
 1746 
 1747         mtx_lock(&fdesc_mtx);
 1748         i = --fdp->fd_holdcnt;
 1749         mtx_unlock(&fdesc_mtx);
 1750         if (i > 0)
 1751                 return;
 1752 
 1753         FILEDESC_LOCK_DESTROY(fdp);
 1754         fdp0 = (struct filedesc0 *)fdp;
 1755         while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
 1756                 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
 1757                 free(ft->ft_table, M_FILEDESC);
 1758         }
 1759         free(fdp, M_FILEDESC);
 1760 }
 1761 
 1762 /*
 1763  * Share a filedesc structure.
 1764  */
 1765 struct filedesc *
 1766 fdshare(struct filedesc *fdp)
 1767 {
 1768 
 1769         FILEDESC_XLOCK(fdp);
 1770         fdp->fd_refcnt++;
 1771         FILEDESC_XUNLOCK(fdp);
 1772         return (fdp);
 1773 }
 1774 
 1775 /*
 1776  * Unshare a filedesc structure, if necessary by making a copy
 1777  */
 1778 void
 1779 fdunshare(struct proc *p, struct thread *td)
 1780 {
 1781 
 1782         FILEDESC_XLOCK(p->p_fd);
 1783         if (p->p_fd->fd_refcnt > 1) {
 1784                 struct filedesc *tmp;
 1785 
 1786                 FILEDESC_XUNLOCK(p->p_fd);
 1787                 tmp = fdcopy(p->p_fd);
 1788                 fdfree(td);
 1789                 p->p_fd = tmp;
 1790         } else
 1791                 FILEDESC_XUNLOCK(p->p_fd);
 1792 }
 1793 
 1794 /*
 1795  * Copy a filedesc structure.  A NULL pointer in returns a NULL reference,
 1796  * this is to ease callers, not catch errors.
 1797  */
 1798 struct filedesc *
 1799 fdcopy(struct filedesc *fdp)
 1800 {
 1801         struct filedesc *newfdp;
 1802         int i;
 1803 
 1804         /* Certain daemons might not have file descriptors. */
 1805         if (fdp == NULL)
 1806                 return (NULL);
 1807 
 1808         newfdp = fdinit(fdp);
 1809         FILEDESC_SLOCK(fdp);
 1810         while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
 1811                 FILEDESC_SUNLOCK(fdp);
 1812                 FILEDESC_XLOCK(newfdp);
 1813                 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
 1814                 FILEDESC_XUNLOCK(newfdp);
 1815                 FILEDESC_SLOCK(fdp);
 1816         }
 1817         /* copy all passable descriptors (i.e. not kqueue) */
 1818         newfdp->fd_freefile = -1;
 1819         for (i = 0; i <= fdp->fd_lastfile; ++i) {
 1820                 if (fdisused(fdp, i) &&
 1821                     (fdp->fd_ofiles[i]->f_ops->fo_flags & DFLAG_PASSABLE) &&
 1822                     fdp->fd_ofiles[i]->f_ops != &badfileops) {
 1823                         newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
 1824                         newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
 1825                         fhold(newfdp->fd_ofiles[i]);
 1826                         newfdp->fd_lastfile = i;
 1827                 } else {
 1828                         if (newfdp->fd_freefile == -1)
 1829                                 newfdp->fd_freefile = i;
 1830                 }
 1831         }
 1832         newfdp->fd_cmask = fdp->fd_cmask;
 1833         FILEDESC_SUNLOCK(fdp);
 1834         FILEDESC_XLOCK(newfdp);
 1835         for (i = 0; i <= newfdp->fd_lastfile; ++i)
 1836                 if (newfdp->fd_ofiles[i] != NULL)
 1837                         fdused(newfdp, i);
 1838         if (newfdp->fd_freefile == -1)
 1839                 newfdp->fd_freefile = i;
 1840         FILEDESC_XUNLOCK(newfdp);
 1841         return (newfdp);
 1842 }
 1843 
 1844 /*
 1845  * Release a filedesc structure.
 1846  */
 1847 void
 1848 fdfree(struct thread *td)
 1849 {
 1850         struct filedesc *fdp;
 1851         struct file **fpp;
 1852         int i, locked;
 1853         struct filedesc_to_leader *fdtol;
 1854         struct file *fp;
 1855         struct vnode *cdir, *jdir, *rdir, *vp;
 1856         struct flock lf;
 1857 
 1858         /* Certain daemons might not have file descriptors. */
 1859         fdp = td->td_proc->p_fd;
 1860         if (fdp == NULL)
 1861                 return;
 1862 
 1863 #ifdef RACCT
 1864         PROC_LOCK(td->td_proc);
 1865         racct_set(td->td_proc, RACCT_NOFILE, 0);
 1866         PROC_UNLOCK(td->td_proc);
 1867 #endif
 1868 
 1869         /* Check for special need to clear POSIX style locks */
 1870         fdtol = td->td_proc->p_fdtol;
 1871         if (fdtol != NULL) {
 1872                 FILEDESC_XLOCK(fdp);
 1873                 KASSERT(fdtol->fdl_refcount > 0,
 1874                         ("filedesc_to_refcount botch: fdl_refcount=%d",
 1875                          fdtol->fdl_refcount));
 1876                 if (fdtol->fdl_refcount == 1 &&
 1877                     (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 1878                         for (i = 0, fpp = fdp->fd_ofiles;
 1879                              i <= fdp->fd_lastfile;
 1880                              i++, fpp++) {
 1881                                 if (*fpp == NULL ||
 1882                                     (*fpp)->f_type != DTYPE_VNODE)
 1883                                         continue;
 1884                                 fp = *fpp;
 1885                                 fhold(fp);
 1886                                 FILEDESC_XUNLOCK(fdp);
 1887                                 lf.l_whence = SEEK_SET;
 1888                                 lf.l_start = 0;
 1889                                 lf.l_len = 0;
 1890                                 lf.l_type = F_UNLCK;
 1891                                 vp = fp->f_vnode;
 1892                                 locked = VFS_LOCK_GIANT(vp->v_mount);
 1893                                 (void) VOP_ADVLOCK(vp,
 1894                                                    (caddr_t)td->td_proc->
 1895                                                    p_leader,
 1896                                                    F_UNLCK,
 1897                                                    &lf,
 1898                                                    F_POSIX);
 1899                                 VFS_UNLOCK_GIANT(locked);
 1900                                 FILEDESC_XLOCK(fdp);
 1901                                 fdrop(fp, td);
 1902                                 fpp = fdp->fd_ofiles + i;
 1903                         }
 1904                 }
 1905         retry:
 1906                 if (fdtol->fdl_refcount == 1) {
 1907                         if (fdp->fd_holdleaderscount > 0 &&
 1908                             (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 1909                                 /*
 1910                                  * close() or do_dup() has cleared a reference
 1911                                  * in a shared file descriptor table.
 1912                                  */
 1913                                 fdp->fd_holdleaderswakeup = 1;
 1914                                 sx_sleep(&fdp->fd_holdleaderscount,
 1915                                     FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
 1916                                 goto retry;
 1917                         }
 1918                         if (fdtol->fdl_holdcount > 0) {
 1919                                 /*
 1920                                  * Ensure that fdtol->fdl_leader remains
 1921                                  * valid in closef().
 1922                                  */
 1923                                 fdtol->fdl_wakeup = 1;
 1924                                 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
 1925                                     "fdlhold", 0);
 1926                                 goto retry;
 1927                         }
 1928                 }
 1929                 fdtol->fdl_refcount--;
 1930                 if (fdtol->fdl_refcount == 0 &&
 1931                     fdtol->fdl_holdcount == 0) {
 1932                         fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
 1933                         fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
 1934                 } else
 1935                         fdtol = NULL;
 1936                 td->td_proc->p_fdtol = NULL;
 1937                 FILEDESC_XUNLOCK(fdp);
 1938                 if (fdtol != NULL)
 1939                         free(fdtol, M_FILEDESC_TO_LEADER);
 1940         }
 1941         FILEDESC_XLOCK(fdp);
 1942         i = --fdp->fd_refcnt;
 1943         FILEDESC_XUNLOCK(fdp);
 1944         if (i > 0)
 1945                 return;
 1946 
 1947         fpp = fdp->fd_ofiles;
 1948         for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
 1949                 if (*fpp) {
 1950                         FILEDESC_XLOCK(fdp);
 1951                         fp = *fpp;
 1952                         *fpp = NULL;
 1953                         FILEDESC_XUNLOCK(fdp);
 1954                         (void) closef(fp, td);
 1955                 }
 1956         }
 1957         FILEDESC_XLOCK(fdp);
 1958 
 1959         /* XXX This should happen earlier. */
 1960         mtx_lock(&fdesc_mtx);
 1961         td->td_proc->p_fd = NULL;
 1962         mtx_unlock(&fdesc_mtx);
 1963 
 1964         if (fdp->fd_nfiles > NDFILE)
 1965                 free(fdp->fd_ofiles, M_FILEDESC);
 1966         if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
 1967                 free(fdp->fd_map, M_FILEDESC);
 1968 
 1969         fdp->fd_nfiles = 0;
 1970 
 1971         cdir = fdp->fd_cdir;
 1972         fdp->fd_cdir = NULL;
 1973         rdir = fdp->fd_rdir;
 1974         fdp->fd_rdir = NULL;
 1975         jdir = fdp->fd_jdir;
 1976         fdp->fd_jdir = NULL;
 1977         FILEDESC_XUNLOCK(fdp);
 1978 
 1979         if (cdir) {
 1980                 locked = VFS_LOCK_GIANT(cdir->v_mount);
 1981                 vrele(cdir);
 1982                 VFS_UNLOCK_GIANT(locked);
 1983         }
 1984         if (rdir) {
 1985                 locked = VFS_LOCK_GIANT(rdir->v_mount);
 1986                 vrele(rdir);
 1987                 VFS_UNLOCK_GIANT(locked);
 1988         }
 1989         if (jdir) {
 1990                 locked = VFS_LOCK_GIANT(jdir->v_mount);
 1991                 vrele(jdir);
 1992                 VFS_UNLOCK_GIANT(locked);
 1993         }
 1994 
 1995         fddrop(fdp);
 1996 }
 1997 
 1998 /*
 1999  * For setugid programs, we don't want to people to use that setugidness
 2000  * to generate error messages which write to a file which otherwise would
 2001  * otherwise be off-limits to the process.  We check for filesystems where
 2002  * the vnode can change out from under us after execve (like [lin]procfs).
 2003  *
 2004  * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
 2005  * sufficient.  We also don't check for setugidness since we know we are.
 2006  */
 2007 static int
 2008 is_unsafe(struct file *fp)
 2009 {
 2010         if (fp->f_type == DTYPE_VNODE) {
 2011                 struct vnode *vp = fp->f_vnode;
 2012 
 2013                 if ((vp->v_vflag & VV_PROCDEP) != 0)
 2014                         return (1);
 2015         }
 2016         return (0);
 2017 }
 2018 
 2019 /*
 2020  * Make this setguid thing safe, if at all possible.
 2021  */
 2022 void
 2023 setugidsafety(struct thread *td)
 2024 {
 2025         struct filedesc *fdp;
 2026         int i;
 2027 
 2028         /* Certain daemons might not have file descriptors. */
 2029         fdp = td->td_proc->p_fd;
 2030         if (fdp == NULL)
 2031                 return;
 2032 
 2033         /*
 2034          * Note: fdp->fd_ofiles may be reallocated out from under us while
 2035          * we are blocked in a close.  Be careful!
 2036          */
 2037         FILEDESC_XLOCK(fdp);
 2038         for (i = 0; i <= fdp->fd_lastfile; i++) {
 2039                 if (i > 2)
 2040                         break;
 2041                 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
 2042                         struct file *fp;
 2043 
 2044                         knote_fdclose(td, i);
 2045                         /*
 2046                          * NULL-out descriptor prior to close to avoid
 2047                          * a race while close blocks.
 2048                          */
 2049                         fp = fdp->fd_ofiles[i];
 2050                         fdp->fd_ofiles[i] = NULL;
 2051                         fdp->fd_ofileflags[i] = 0;
 2052                         fdunused(fdp, i);
 2053                         FILEDESC_XUNLOCK(fdp);
 2054                         (void) closef(fp, td);
 2055                         FILEDESC_XLOCK(fdp);
 2056                 }
 2057         }
 2058         FILEDESC_XUNLOCK(fdp);
 2059 }
 2060 
 2061 /*
 2062  * If a specific file object occupies a specific file descriptor, close the
 2063  * file descriptor entry and drop a reference on the file object.  This is a
 2064  * convenience function to handle a subsequent error in a function that calls
 2065  * falloc() that handles the race that another thread might have closed the
 2066  * file descriptor out from under the thread creating the file object.
 2067  */
 2068 void
 2069 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
 2070 {
 2071 
 2072         FILEDESC_XLOCK(fdp);
 2073         if (fdp->fd_ofiles[idx] == fp) {
 2074                 fdp->fd_ofiles[idx] = NULL;
 2075                 fdunused(fdp, idx);
 2076                 FILEDESC_XUNLOCK(fdp);
 2077                 fdrop(fp, td);
 2078         } else
 2079                 FILEDESC_XUNLOCK(fdp);
 2080 }
 2081 
 2082 /*
 2083  * Close any files on exec?
 2084  */
 2085 void
 2086 fdcloseexec(struct thread *td)
 2087 {
 2088         struct filedesc *fdp;
 2089         int i;
 2090 
 2091         /* Certain daemons might not have file descriptors. */
 2092         fdp = td->td_proc->p_fd;
 2093         if (fdp == NULL)
 2094                 return;
 2095 
 2096         FILEDESC_XLOCK(fdp);
 2097 
 2098         /*
 2099          * We cannot cache fd_ofiles or fd_ofileflags since operations
 2100          * may block and rip them out from under us.
 2101          */
 2102         for (i = 0; i <= fdp->fd_lastfile; i++) {
 2103                 if (fdp->fd_ofiles[i] != NULL &&
 2104                     (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
 2105                     (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
 2106                         struct file *fp;
 2107 
 2108                         knote_fdclose(td, i);
 2109                         /*
 2110                          * NULL-out descriptor prior to close to avoid
 2111                          * a race while close blocks.
 2112                          */
 2113                         fp = fdp->fd_ofiles[i];
 2114                         fdp->fd_ofiles[i] = NULL;
 2115                         fdp->fd_ofileflags[i] = 0;
 2116                         fdunused(fdp, i);
 2117                         if (fp->f_type == DTYPE_MQUEUE)
 2118                                 mq_fdclose(td, i, fp);
 2119                         FILEDESC_XUNLOCK(fdp);
 2120                         (void) closef(fp, td);
 2121                         FILEDESC_XLOCK(fdp);
 2122                 }
 2123         }
 2124         FILEDESC_XUNLOCK(fdp);
 2125 }
 2126 
 2127 /*
 2128  * It is unsafe for set[ug]id processes to be started with file
 2129  * descriptors 0..2 closed, as these descriptors are given implicit
 2130  * significance in the Standard C library.  fdcheckstd() will create a
 2131  * descriptor referencing /dev/null for each of stdin, stdout, and
 2132  * stderr that is not already open.
 2133  */
 2134 int
 2135 fdcheckstd(struct thread *td)
 2136 {
 2137         struct filedesc *fdp;
 2138         register_t retval, save;
 2139         int i, error, devnull;
 2140 
 2141         fdp = td->td_proc->p_fd;
 2142         if (fdp == NULL)
 2143                 return (0);
 2144         KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
 2145         devnull = -1;
 2146         error = 0;
 2147         for (i = 0; i < 3; i++) {
 2148                 if (fdp->fd_ofiles[i] != NULL)
 2149                         continue;
 2150                 if (devnull < 0) {
 2151                         save = td->td_retval[0];
 2152                         error = kern_open(td, "/dev/null", UIO_SYSSPACE,
 2153                             O_RDWR, 0);
 2154                         devnull = td->td_retval[0];
 2155                         td->td_retval[0] = save;
 2156                         if (error)
 2157                                 break;
 2158                         KASSERT(devnull == i, ("oof, we didn't get our fd"));
 2159                 } else {
 2160                         error = do_dup(td, DUP_FIXED, devnull, i, &retval);
 2161                         if (error != 0)
 2162                                 break;
 2163                 }
 2164         }
 2165         return (error);
 2166 }
 2167 
 2168 /*
 2169  * Internal form of close.  Decrement reference count on file structure.
 2170  * Note: td may be NULL when closing a file that was being passed in a
 2171  * message.
 2172  *
 2173  * XXXRW: Giant is not required for the caller, but often will be held; this
 2174  * makes it moderately likely the Giant will be recursed in the VFS case.
 2175  */
 2176 int
 2177 closef(struct file *fp, struct thread *td)
 2178 {
 2179         struct vnode *vp;
 2180         struct flock lf;
 2181         struct filedesc_to_leader *fdtol;
 2182         struct filedesc *fdp;
 2183         struct file *fp_object;
 2184 
 2185         /*
 2186          * POSIX record locking dictates that any close releases ALL
 2187          * locks owned by this process.  This is handled by setting
 2188          * a flag in the unlock to free ONLY locks obeying POSIX
 2189          * semantics, and not to free BSD-style file locks.
 2190          * If the descriptor was in a message, POSIX-style locks
 2191          * aren't passed with the descriptor, and the thread pointer
 2192          * will be NULL.  Callers should be careful only to pass a
 2193          * NULL thread pointer when there really is no owning
 2194          * context that might have locks, or the locks will be
 2195          * leaked.
 2196          *
 2197          * If this is a capability, we do lock processing under the underlying
 2198          * node, not the capability itself.
 2199          */
 2200         (void)cap_funwrap(fp, 0, &fp_object);
 2201         if ((fp_object->f_type == DTYPE_VNODE) && (td != NULL)) {
 2202                 int vfslocked;
 2203 
 2204                 vp = fp_object->f_vnode;
 2205                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2206                 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 2207                         lf.l_whence = SEEK_SET;
 2208                         lf.l_start = 0;
 2209                         lf.l_len = 0;
 2210                         lf.l_type = F_UNLCK;
 2211                         (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
 2212                                            F_UNLCK, &lf, F_POSIX);
 2213                 }
 2214                 fdtol = td->td_proc->p_fdtol;
 2215                 if (fdtol != NULL) {
 2216                         /*
 2217                          * Handle special case where file descriptor table is
 2218                          * shared between multiple process leaders.
 2219                          */
 2220                         fdp = td->td_proc->p_fd;
 2221                         FILEDESC_XLOCK(fdp);
 2222                         for (fdtol = fdtol->fdl_next;
 2223                              fdtol != td->td_proc->p_fdtol;
 2224                              fdtol = fdtol->fdl_next) {
 2225                                 if ((fdtol->fdl_leader->p_flag &
 2226                                      P_ADVLOCK) == 0)
 2227                                         continue;
 2228                                 fdtol->fdl_holdcount++;
 2229                                 FILEDESC_XUNLOCK(fdp);
 2230                                 lf.l_whence = SEEK_SET;
 2231                                 lf.l_start = 0;
 2232                                 lf.l_len = 0;
 2233                                 lf.l_type = F_UNLCK;
 2234                                 vp = fp_object->f_vnode;
 2235                                 (void) VOP_ADVLOCK(vp,
 2236                                                    (caddr_t)fdtol->fdl_leader,
 2237                                                    F_UNLCK, &lf, F_POSIX);
 2238                                 FILEDESC_XLOCK(fdp);
 2239                                 fdtol->fdl_holdcount--;
 2240                                 if (fdtol->fdl_holdcount == 0 &&
 2241                                     fdtol->fdl_wakeup != 0) {
 2242                                         fdtol->fdl_wakeup = 0;
 2243                                         wakeup(fdtol);
 2244                                 }
 2245                         }
 2246                         FILEDESC_XUNLOCK(fdp);
 2247                 }
 2248                 VFS_UNLOCK_GIANT(vfslocked);
 2249         }
 2250         return (fdrop(fp, td));
 2251 }
 2252 
 2253 /*
 2254  * Initialize the file pointer with the specified properties.
 2255  *
 2256  * The ops are set with release semantics to be certain that the flags, type,
 2257  * and data are visible when ops is.  This is to prevent ops methods from being
 2258  * called with bad data.
 2259  */
 2260 void
 2261 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
 2262 {
 2263         fp->f_data = data;
 2264         fp->f_flag = flag;
 2265         fp->f_type = type;
 2266         atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
 2267 }
 2268 
 2269 struct file *
 2270 fget_unlocked(struct filedesc *fdp, int fd)
 2271 {
 2272         struct file *fp;
 2273         u_int count;
 2274 
 2275         if (fd < 0 || fd >= fdp->fd_nfiles)
 2276                 return (NULL);
 2277         /*
 2278          * Fetch the descriptor locklessly.  We avoid fdrop() races by
 2279          * never raising a refcount above 0.  To accomplish this we have
 2280          * to use a cmpset loop rather than an atomic_add.  The descriptor
 2281          * must be re-verified once we acquire a reference to be certain
 2282          * that the identity is still correct and we did not lose a race
 2283          * due to preemption.
 2284          */
 2285         for (;;) {
 2286                 fp = fdp->fd_ofiles[fd];
 2287                 if (fp == NULL)
 2288                         break;
 2289                 count = fp->f_count;
 2290                 if (count == 0)
 2291                         continue;
 2292                 /*
 2293                  * Use an acquire barrier to prevent caching of fd_ofiles
 2294                  * so it is refreshed for verification.
 2295                  */
 2296                 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
 2297                         continue;
 2298                 if (fp == fdp->fd_ofiles[fd])
 2299                         break;
 2300                 fdrop(fp, curthread);
 2301         }
 2302 
 2303         return (fp);
 2304 }
 2305 
 2306 /*
 2307  * Extract the file pointer associated with the specified descriptor for the
 2308  * current user process.
 2309  *
 2310  * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
 2311  * returned.
 2312  *
 2313  * If the FGET_GETCAP flag is set, the capability itself will be returned.
 2314  * Calling _fget() with FGET_GETCAP on a non-capability will return EINVAL.
 2315  * Otherwise, if the file is a capability, its rights will be checked against
 2316  * the capability rights mask, and if successful, the object will be unwrapped.
 2317  *
 2318  * If an error occured the non-zero error is returned and *fpp is set to
 2319  * NULL.  Otherwise *fpp is held and set and zero is returned.  Caller is
 2320  * responsible for fdrop().
 2321  */
 2322 #define FGET_GETCAP     0x00000001
 2323 static __inline int
 2324 _fget(struct thread *td, int fd, struct file **fpp, int flags,
 2325     cap_rights_t needrights, cap_rights_t *haverightsp, u_char *maxprotp,
 2326     int fget_flags)
 2327 {
 2328         struct filedesc *fdp;
 2329         struct file *fp;
 2330 #ifdef CAPABILITIES
 2331         struct file *fp_fromcap;
 2332 #endif
 2333         int error;
 2334 
 2335         *fpp = NULL;
 2336         if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
 2337                 return (EBADF);
 2338         if ((fp = fget_unlocked(fdp, fd)) == NULL)
 2339                 return (EBADF);
 2340         if (fp->f_ops == &badfileops) {
 2341                 fdrop(fp, td);
 2342                 return (EBADF);
 2343         }
 2344 
 2345 #ifdef CAPABILITIES
 2346         /*
 2347          * If this is a capability, what rights does it have?
 2348          */
 2349         if (haverightsp != NULL) {
 2350                 if (fp->f_type == DTYPE_CAPABILITY)
 2351                         *haverightsp = cap_rights(fp);
 2352                 else
 2353                         *haverightsp = CAP_MASK_VALID;
 2354         }
 2355 
 2356         /*
 2357          * If a capability has been requested, return the capability directly.
 2358          * Otherwise, check capability rights, extract the underlying object,
 2359          * and check its access flags.
 2360          */
 2361         if (fget_flags & FGET_GETCAP) {
 2362                 if (fp->f_type != DTYPE_CAPABILITY) {
 2363                         fdrop(fp, td);
 2364                         return (EINVAL);
 2365                 }
 2366         } else {
 2367                 if (maxprotp == NULL)
 2368                         error = cap_funwrap(fp, needrights, &fp_fromcap);
 2369                 else
 2370                         error = cap_funwrap_mmap(fp, needrights, maxprotp,
 2371                             &fp_fromcap);
 2372                 if (error != 0) {
 2373                         fdrop(fp, td);
 2374                         return (error);
 2375                 }
 2376 
 2377                 /*
 2378                  * If we've unwrapped a file, drop the original capability
 2379                  * and hold the new descriptor.  fp after this point refers to
 2380                  * the actual (unwrapped) object, not the capability.
 2381                  */
 2382                 if (fp != fp_fromcap) {
 2383                         fhold(fp_fromcap);
 2384                         fdrop(fp, td);
 2385                         fp = fp_fromcap;
 2386                 }
 2387         }
 2388 #else /* !CAPABILITIES */
 2389         KASSERT(fp->f_type != DTYPE_CAPABILITY,
 2390             ("%s: saw capability", __func__));
 2391         if (maxprotp != NULL)
 2392                 *maxprotp = VM_PROT_ALL;
 2393 #endif /* CAPABILITIES */
 2394 
 2395         /*
 2396          * FREAD and FWRITE failure return EBADF as per POSIX.
 2397          */
 2398         error = 0;
 2399         switch (flags) {
 2400         case FREAD:
 2401         case FWRITE:
 2402                 if ((fp->f_flag & flags) == 0)
 2403                         error = EBADF;
 2404                 break;
 2405         case FEXEC:
 2406                 if ((fp->f_flag & (FREAD | FEXEC)) == 0 ||
 2407                     ((fp->f_flag & FWRITE) != 0))
 2408                         error = EBADF;
 2409                 break;
 2410         case 0:
 2411                 break;
 2412         default:
 2413                 KASSERT(0, ("wrong flags"));
 2414         }
 2415 
 2416         if (error != 0) {
 2417                 fdrop(fp, td);
 2418                 return (error);
 2419         }
 2420 
 2421         *fpp = fp;
 2422         return (0);
 2423 }
 2424 
 2425 int
 2426 fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
 2427 {
 2428 
 2429         return(_fget(td, fd, fpp, 0, rights, NULL, NULL, 0));
 2430 }
 2431 
 2432 int
 2433 fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp,
 2434     struct file **fpp)
 2435 {
 2436 
 2437         return (_fget(td, fd, fpp, 0, rights, NULL, maxprotp, 0));
 2438 }
 2439 
 2440 int
 2441 fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
 2442 {
 2443 
 2444         return(_fget(td, fd, fpp, FREAD, rights, NULL, NULL, 0));
 2445 }
 2446 
 2447 int
 2448 fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
 2449 {
 2450 
 2451         return (_fget(td, fd, fpp, FWRITE, rights, NULL, NULL, 0));
 2452 }
 2453 
 2454 /*
 2455  * Unlike the other fget() calls, which accept and check capability rights
 2456  * but never return capabilities, fgetcap() returns the capability but doesn't
 2457  * check capability rights.
 2458  */
 2459 int
 2460 fgetcap(struct thread *td, int fd, struct file **fpp)
 2461 {
 2462 
 2463         return (_fget(td, fd, fpp, 0, 0, NULL, NULL, FGET_GETCAP));
 2464 }
 2465 
 2466 
 2467 /*
 2468  * Like fget() but loads the underlying vnode, or returns an error if the
 2469  * descriptor does not represent a vnode.  Note that pipes use vnodes but
 2470  * never have VM objects.  The returned vnode will be vref()'d.
 2471  *
 2472  * XXX: what about the unused flags ?
 2473  */
 2474 static __inline int
 2475 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights,
 2476     cap_rights_t *haverightsp, struct vnode **vpp)
 2477 {
 2478         struct file *fp;
 2479         int error;
 2480 
 2481         *vpp = NULL;
 2482         if ((error = _fget(td, fd, &fp, flags, needrights, haverightsp,
 2483             NULL, 0)) != 0)
 2484                 return (error);
 2485         if (fp->f_vnode == NULL) {
 2486                 error = EINVAL;
 2487         } else {
 2488                 *vpp = fp->f_vnode;
 2489                 vref(*vpp);
 2490         }
 2491         fdrop(fp, td);
 2492 
 2493         return (error);
 2494 }
 2495 
 2496 int
 2497 fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
 2498 {
 2499 
 2500         return (_fgetvp(td, fd, 0, rights, NULL, vpp));
 2501 }
 2502 
 2503 int
 2504 fgetvp_rights(struct thread *td, int fd, cap_rights_t need, cap_rights_t *have,
 2505     struct vnode **vpp)
 2506 {
 2507         return (_fgetvp(td, fd, 0, need, have, vpp));
 2508 }
 2509 
 2510 int
 2511 fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
 2512 {
 2513 
 2514         return (_fgetvp(td, fd, FREAD, rights, NULL, vpp));
 2515 }
 2516 
 2517 int
 2518 fgetvp_exec(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
 2519 {
 2520 
 2521         return (_fgetvp(td, fd, FEXEC, rights, NULL, vpp));
 2522 }
 2523 
 2524 #ifdef notyet
 2525 int
 2526 fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
 2527     struct vnode **vpp)
 2528 {
 2529 
 2530         return (_fgetvp(td, fd, FWRITE, rights, NULL, vpp));
 2531 }
 2532 #endif
 2533 
 2534 /*
 2535  * Like fget() but loads the underlying socket, or returns an error if the
 2536  * descriptor does not represent a socket.
 2537  *
 2538  * We bump the ref count on the returned socket.  XXX Also obtain the SX lock
 2539  * in the future.
 2540  *
 2541  * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
 2542  * on their file descriptor reference to prevent the socket from being free'd
 2543  * during use.
 2544  */
 2545 int
 2546 fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp,
 2547     u_int *fflagp)
 2548 {
 2549         struct file *fp;
 2550         int error;
 2551 
 2552         *spp = NULL;
 2553         if (fflagp != NULL)
 2554                 *fflagp = 0;
 2555         if ((error = _fget(td, fd, &fp, 0, rights, NULL, NULL, 0)) != 0)
 2556                 return (error);
 2557         if (fp->f_type != DTYPE_SOCKET) {
 2558                 error = ENOTSOCK;
 2559         } else {
 2560                 *spp = fp->f_data;
 2561                 if (fflagp)
 2562                         *fflagp = fp->f_flag;
 2563                 SOCK_LOCK(*spp);
 2564                 soref(*spp);
 2565                 SOCK_UNLOCK(*spp);
 2566         }
 2567         fdrop(fp, td);
 2568 
 2569         return (error);
 2570 }
 2571 
 2572 /*
 2573  * Drop the reference count on the socket and XXX release the SX lock in the
 2574  * future.  The last reference closes the socket.
 2575  *
 2576  * Note: fputsock() is deprecated, see comment for fgetsock().
 2577  */
 2578 void
 2579 fputsock(struct socket *so)
 2580 {
 2581 
 2582         ACCEPT_LOCK();
 2583         SOCK_LOCK(so);
 2584         CURVNET_SET(so->so_vnet);
 2585         sorele(so);
 2586         CURVNET_RESTORE();
 2587 }
 2588 
 2589 /*
 2590  * Handle the last reference to a file being closed.
 2591  *
 2592  * No special capability handling here, as the capability's fo_close will run
 2593  * instead of the object here, and perform any necessary drop on the object.
 2594  */
 2595 int
 2596 _fdrop(struct file *fp, struct thread *td)
 2597 {
 2598         int error;
 2599 
 2600         error = 0;
 2601         if (fp->f_count != 0)
 2602                 panic("fdrop: count %d", fp->f_count);
 2603         if (fp->f_ops != &badfileops)
 2604                 error = fo_close(fp, td);
 2605         atomic_subtract_int(&openfiles, 1);
 2606         crfree(fp->f_cred);
 2607         free(fp->f_advice, M_FADVISE);
 2608         uma_zfree(file_zone, fp);
 2609 
 2610         return (error);
 2611 }
 2612 
 2613 /*
 2614  * Apply an advisory lock on a file descriptor.
 2615  *
 2616  * Just attempt to get a record lock of the requested type on the entire file
 2617  * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
 2618  */
 2619 #ifndef _SYS_SYSPROTO_H_
 2620 struct flock_args {
 2621         int     fd;
 2622         int     how;
 2623 };
 2624 #endif
 2625 /* ARGSUSED */
 2626 int
 2627 sys_flock(struct thread *td, struct flock_args *uap)
 2628 {
 2629         struct file *fp;
 2630         struct vnode *vp;
 2631         struct flock lf;
 2632         int vfslocked;
 2633         int error;
 2634 
 2635         if ((error = fget(td, uap->fd, CAP_FLOCK, &fp)) != 0)
 2636                 return (error);
 2637         if (fp->f_type != DTYPE_VNODE) {
 2638                 fdrop(fp, td);
 2639                 return (EOPNOTSUPP);
 2640         }
 2641 
 2642         vp = fp->f_vnode;
 2643         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2644         lf.l_whence = SEEK_SET;
 2645         lf.l_start = 0;
 2646         lf.l_len = 0;
 2647         if (uap->how & LOCK_UN) {
 2648                 lf.l_type = F_UNLCK;
 2649                 atomic_clear_int(&fp->f_flag, FHASLOCK);
 2650                 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
 2651                 goto done2;
 2652         }
 2653         if (uap->how & LOCK_EX)
 2654                 lf.l_type = F_WRLCK;
 2655         else if (uap->how & LOCK_SH)
 2656                 lf.l_type = F_RDLCK;
 2657         else {
 2658                 error = EBADF;
 2659                 goto done2;
 2660         }
 2661         atomic_set_int(&fp->f_flag, FHASLOCK);
 2662         error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
 2663             (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
 2664 done2:
 2665         fdrop(fp, td);
 2666         VFS_UNLOCK_GIANT(vfslocked);
 2667         return (error);
 2668 }
 2669 /*
 2670  * Duplicate the specified descriptor to a free descriptor.
 2671  */
 2672 int
 2673 dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
 2674 {
 2675         struct file *wfp;
 2676         struct file *fp;
 2677 
 2678         /*
 2679          * If the to-be-dup'd fd number is greater than the allowed number
 2680          * of file descriptors, or the fd to be dup'd has already been
 2681          * closed, then reject.
 2682          */
 2683         FILEDESC_XLOCK(fdp);
 2684         if (dfd < 0 || dfd >= fdp->fd_nfiles ||
 2685             (wfp = fdp->fd_ofiles[dfd]) == NULL) {
 2686                 FILEDESC_XUNLOCK(fdp);
 2687                 return (EBADF);
 2688         }
 2689 
 2690         /*
 2691          * There are two cases of interest here.
 2692          *
 2693          * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
 2694          *
 2695          * For ENXIO steal away the file structure from (dfd) and store it in
 2696          * (indx).  (dfd) is effectively closed by this operation.
 2697          *
 2698          * Any other error code is just returned.
 2699          */
 2700         switch (error) {
 2701         case ENODEV:
 2702                 /*
 2703                  * Check that the mode the file is being opened for is a
 2704                  * subset of the mode of the existing descriptor.
 2705                  */
 2706                 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
 2707                         FILEDESC_XUNLOCK(fdp);
 2708                         return (EACCES);
 2709                 }
 2710                 fp = fdp->fd_ofiles[indx];
 2711                 fdp->fd_ofiles[indx] = wfp;
 2712                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2713                 if (fp == NULL)
 2714                         fdused(fdp, indx);
 2715                 fhold(wfp);
 2716                 FILEDESC_XUNLOCK(fdp);
 2717                 if (fp != NULL)
 2718                         /*
 2719                          * We now own the reference to fp that the ofiles[]
 2720                          * array used to own.  Release it.
 2721                          */
 2722                         fdrop(fp, td);
 2723                 return (0);
 2724 
 2725         case ENXIO:
 2726                 /*
 2727                  * Steal away the file pointer from dfd and stuff it into indx.
 2728                  */
 2729                 fp = fdp->fd_ofiles[indx];
 2730                 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
 2731                 fdp->fd_ofiles[dfd] = NULL;
 2732                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2733                 fdp->fd_ofileflags[dfd] = 0;
 2734                 fdunused(fdp, dfd);
 2735                 if (fp == NULL)
 2736                         fdused(fdp, indx);
 2737                 FILEDESC_XUNLOCK(fdp);
 2738 
 2739                 /*
 2740                  * We now own the reference to fp that the ofiles[] array
 2741                  * used to own.  Release it.
 2742                  */
 2743                 if (fp != NULL)
 2744                         fdrop(fp, td);
 2745                 return (0);
 2746 
 2747         default:
 2748                 FILEDESC_XUNLOCK(fdp);
 2749                 return (error);
 2750         }
 2751         /* NOTREACHED */
 2752 }
 2753 
 2754 /*
 2755  * Scan all active processes and prisons to see if any of them have a current
 2756  * or root directory of `olddp'. If so, replace them with the new mount point.
 2757  */
 2758 void
 2759 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
 2760 {
 2761         struct filedesc *fdp;
 2762         struct prison *pr;
 2763         struct proc *p;
 2764         int nrele;
 2765 
 2766         if (vrefcnt(olddp) == 1)
 2767                 return;
 2768         nrele = 0;
 2769         sx_slock(&allproc_lock);
 2770         FOREACH_PROC_IN_SYSTEM(p) {
 2771                 fdp = fdhold(p);
 2772                 if (fdp == NULL)
 2773                         continue;
 2774                 FILEDESC_XLOCK(fdp);
 2775                 if (fdp->fd_cdir == olddp) {
 2776                         vref(newdp);
 2777                         fdp->fd_cdir = newdp;
 2778                         nrele++;
 2779                 }
 2780                 if (fdp->fd_rdir == olddp) {
 2781                         vref(newdp);
 2782                         fdp->fd_rdir = newdp;
 2783                         nrele++;
 2784                 }
 2785                 if (fdp->fd_jdir == olddp) {
 2786                         vref(newdp);
 2787                         fdp->fd_jdir = newdp;
 2788                         nrele++;
 2789                 }
 2790                 FILEDESC_XUNLOCK(fdp);
 2791                 fddrop(fdp);
 2792         }
 2793         sx_sunlock(&allproc_lock);
 2794         if (rootvnode == olddp) {
 2795                 vref(newdp);
 2796                 rootvnode = newdp;
 2797                 nrele++;
 2798         }
 2799         mtx_lock(&prison0.pr_mtx);
 2800         if (prison0.pr_root == olddp) {
 2801                 vref(newdp);
 2802                 prison0.pr_root = newdp;
 2803                 nrele++;
 2804         }
 2805         mtx_unlock(&prison0.pr_mtx);
 2806         sx_slock(&allprison_lock);
 2807         TAILQ_FOREACH(pr, &allprison, pr_list) {
 2808                 mtx_lock(&pr->pr_mtx);
 2809                 if (pr->pr_root == olddp) {
 2810                         vref(newdp);
 2811                         pr->pr_root = newdp;
 2812                         nrele++;
 2813                 }
 2814                 mtx_unlock(&pr->pr_mtx);
 2815         }
 2816         sx_sunlock(&allprison_lock);
 2817         while (nrele--)
 2818                 vrele(olddp);
 2819 }
 2820 
 2821 struct filedesc_to_leader *
 2822 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
 2823 {
 2824         struct filedesc_to_leader *fdtol;
 2825 
 2826         fdtol = malloc(sizeof(struct filedesc_to_leader),
 2827                M_FILEDESC_TO_LEADER,
 2828                M_WAITOK);
 2829         fdtol->fdl_refcount = 1;
 2830         fdtol->fdl_holdcount = 0;
 2831         fdtol->fdl_wakeup = 0;
 2832         fdtol->fdl_leader = leader;
 2833         if (old != NULL) {
 2834                 FILEDESC_XLOCK(fdp);
 2835                 fdtol->fdl_next = old->fdl_next;
 2836                 fdtol->fdl_prev = old;
 2837                 old->fdl_next = fdtol;
 2838                 fdtol->fdl_next->fdl_prev = fdtol;
 2839                 FILEDESC_XUNLOCK(fdp);
 2840         } else {
 2841                 fdtol->fdl_next = fdtol;
 2842                 fdtol->fdl_prev = fdtol;
 2843         }
 2844         return (fdtol);
 2845 }
 2846 
 2847 /*
 2848  * Get file structures globally.
 2849  */
 2850 static int
 2851 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
 2852 {
 2853         struct xfile xf;
 2854         struct filedesc *fdp;
 2855         struct file *fp;
 2856         struct proc *p;
 2857         int error, n;
 2858 
 2859         error = sysctl_wire_old_buffer(req, 0);
 2860         if (error != 0)
 2861                 return (error);
 2862         if (req->oldptr == NULL) {
 2863                 n = 0;
 2864                 sx_slock(&allproc_lock);
 2865                 FOREACH_PROC_IN_SYSTEM(p) {
 2866                         if (p->p_state == PRS_NEW)
 2867                                 continue;
 2868                         fdp = fdhold(p);
 2869                         if (fdp == NULL)
 2870                                 continue;
 2871                         /* overestimates sparse tables. */
 2872                         if (fdp->fd_lastfile > 0)
 2873                                 n += fdp->fd_lastfile;
 2874                         fddrop(fdp);
 2875                 }
 2876                 sx_sunlock(&allproc_lock);
 2877                 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
 2878         }
 2879         error = 0;
 2880         bzero(&xf, sizeof(xf));
 2881         xf.xf_size = sizeof(xf);
 2882         sx_slock(&allproc_lock);
 2883         FOREACH_PROC_IN_SYSTEM(p) {
 2884                 PROC_LOCK(p);
 2885                 if (p->p_state == PRS_NEW) {
 2886                         PROC_UNLOCK(p);
 2887                         continue;
 2888                 }
 2889                 if (p_cansee(req->td, p) != 0) {
 2890                         PROC_UNLOCK(p);
 2891                         continue;
 2892                 }
 2893                 xf.xf_pid = p->p_pid;
 2894                 xf.xf_uid = p->p_ucred->cr_uid;
 2895                 PROC_UNLOCK(p);
 2896                 fdp = fdhold(p);
 2897                 if (fdp == NULL)
 2898                         continue;
 2899                 FILEDESC_SLOCK(fdp);
 2900                 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
 2901                         if ((fp = fdp->fd_ofiles[n]) == NULL)
 2902                                 continue;
 2903                         xf.xf_fd = n;
 2904                         xf.xf_file = fp;
 2905                         xf.xf_data = fp->f_data;
 2906                         xf.xf_vnode = fp->f_vnode;
 2907                         xf.xf_type = fp->f_type;
 2908                         xf.xf_count = fp->f_count;
 2909                         xf.xf_msgcount = 0;
 2910                         xf.xf_offset = fp->f_offset;
 2911                         xf.xf_flag = fp->f_flag;
 2912                         error = SYSCTL_OUT(req, &xf, sizeof(xf));
 2913                         if (error)
 2914                                 break;
 2915                 }
 2916                 FILEDESC_SUNLOCK(fdp);
 2917                 fddrop(fdp);
 2918                 if (error)
 2919                         break;
 2920         }
 2921         sx_sunlock(&allproc_lock);
 2922         return (error);
 2923 }
 2924 
 2925 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
 2926     0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
 2927 
 2928 #ifdef KINFO_OFILE_SIZE
 2929 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
 2930 #endif
 2931 
 2932 #ifdef COMPAT_FREEBSD7
 2933 static int
 2934 export_vnode_for_osysctl(struct vnode *vp, int type,
 2935     struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
 2936 {
 2937         int error;
 2938         char *fullpath, *freepath;
 2939         int vfslocked;
 2940 
 2941         bzero(kif, sizeof(*kif));
 2942         kif->kf_structsize = sizeof(*kif);
 2943 
 2944         vref(vp);
 2945         kif->kf_fd = type;
 2946         kif->kf_type = KF_TYPE_VNODE;
 2947         /* This function only handles directories. */
 2948         if (vp->v_type != VDIR) {
 2949                 vrele(vp);
 2950                 return (ENOTDIR);
 2951         }
 2952         kif->kf_vnode_type = KF_VTYPE_VDIR;
 2953 
 2954         /*
 2955          * This is not a true file descriptor, so we set a bogus refcount
 2956          * and offset to indicate these fields should be ignored.
 2957          */
 2958         kif->kf_ref_count = -1;
 2959         kif->kf_offset = -1;
 2960 
 2961         freepath = NULL;
 2962         fullpath = "-";
 2963         FILEDESC_SUNLOCK(fdp);
 2964         vn_fullpath(curthread, vp, &fullpath, &freepath);
 2965         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2966         vrele(vp);
 2967         VFS_UNLOCK_GIANT(vfslocked);
 2968         strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
 2969         if (freepath != NULL)
 2970                 free(freepath, M_TEMP);
 2971         error = SYSCTL_OUT(req, kif, sizeof(*kif));
 2972         FILEDESC_SLOCK(fdp);
 2973         return (error);
 2974 }
 2975 
 2976 /*
 2977  * Get per-process file descriptors for use by procstat(1), et al.
 2978  */
 2979 static int
 2980 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
 2981 {
 2982         char *fullpath, *freepath;
 2983         struct kinfo_ofile *kif;
 2984         struct filedesc *fdp;
 2985         int error, i, *name;
 2986         struct shmfd *shmfd;
 2987         struct socket *so;
 2988         struct vnode *vp;
 2989         struct file *fp;
 2990         struct proc *p;
 2991         struct tty *tp;
 2992         int vfslocked;
 2993 
 2994         name = (int *)arg1;
 2995         if ((p = pfind((pid_t)name[0])) == NULL)
 2996                 return (ESRCH);
 2997         if ((error = p_candebug(curthread, p))) {
 2998                 PROC_UNLOCK(p);
 2999                 return (error);
 3000         }
 3001         fdp = fdhold(p);
 3002         PROC_UNLOCK(p);
 3003         if (fdp == NULL)
 3004                 return (ENOENT);
 3005         kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
 3006         FILEDESC_SLOCK(fdp);
 3007         if (fdp->fd_cdir != NULL)
 3008                 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
 3009                                 fdp, req);
 3010         if (fdp->fd_rdir != NULL)
 3011                 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
 3012                                 fdp, req);
 3013         if (fdp->fd_jdir != NULL)
 3014                 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
 3015                                 fdp, req);
 3016         for (i = 0; i < fdp->fd_nfiles; i++) {
 3017                 if ((fp = fdp->fd_ofiles[i]) == NULL)
 3018                         continue;
 3019                 bzero(kif, sizeof(*kif));
 3020                 kif->kf_structsize = sizeof(*kif);
 3021                 vp = NULL;
 3022                 so = NULL;
 3023                 tp = NULL;
 3024                 shmfd = NULL;
 3025                 kif->kf_fd = i;
 3026 
 3027 #ifdef CAPABILITIES
 3028                 /*
 3029                  * When reporting a capability, most fields will be from the
 3030                  * underlying object, but do mark as a capability. With
 3031                  * ofiledesc, we don't have a field to export the cap_rights_t,
 3032                  * but we do with the new filedesc.
 3033                  */
 3034                 if (fp->f_type == DTYPE_CAPABILITY) {
 3035                         kif->kf_flags |= KF_FLAG_CAPABILITY;
 3036                         (void)cap_funwrap(fp, 0, &fp);
 3037                 }
 3038 #else
 3039                 KASSERT(fp->f_type != DTYPE_CAPABILITY,
 3040                     ("sysctl_kern_proc_ofiledesc: saw capability"));
 3041 #endif
 3042                 switch (fp->f_type) {
 3043                 case DTYPE_VNODE:
 3044                         kif->kf_type = KF_TYPE_VNODE;
 3045                         vp = fp->f_vnode;
 3046                         break;
 3047 
 3048                 case DTYPE_SOCKET:
 3049                         kif->kf_type = KF_TYPE_SOCKET;
 3050                         so = fp->f_data;
 3051                         break;
 3052 
 3053                 case DTYPE_PIPE:
 3054                         kif->kf_type = KF_TYPE_PIPE;
 3055                         break;
 3056 
 3057                 case DTYPE_FIFO:
 3058                         kif->kf_type = KF_TYPE_FIFO;
 3059                         vp = fp->f_vnode;
 3060                         break;
 3061 
 3062                 case DTYPE_KQUEUE:
 3063                         kif->kf_type = KF_TYPE_KQUEUE;
 3064                         break;
 3065 
 3066                 case DTYPE_CRYPTO:
 3067                         kif->kf_type = KF_TYPE_CRYPTO;
 3068                         break;
 3069 
 3070                 case DTYPE_MQUEUE:
 3071                         kif->kf_type = KF_TYPE_MQUEUE;
 3072                         break;
 3073 
 3074                 case DTYPE_SHM:
 3075                         kif->kf_type = KF_TYPE_SHM;
 3076                         shmfd = fp->f_data;
 3077                         break;
 3078 
 3079                 case DTYPE_SEM:
 3080                         kif->kf_type = KF_TYPE_SEM;
 3081                         break;
 3082 
 3083                 case DTYPE_PTS:
 3084                         kif->kf_type = KF_TYPE_PTS;
 3085                         tp = fp->f_data;
 3086                         break;
 3087 
 3088 #ifdef PROCDESC
 3089                 case DTYPE_PROCDESC:
 3090                         kif->kf_type = KF_TYPE_PROCDESC;
 3091                         break;
 3092 #endif
 3093 
 3094                 default:
 3095                         kif->kf_type = KF_TYPE_UNKNOWN;
 3096                         break;
 3097                 }
 3098                 kif->kf_ref_count = fp->f_count;
 3099                 if (fp->f_flag & FREAD)
 3100                         kif->kf_flags |= KF_FLAG_READ;
 3101                 if (fp->f_flag & FWRITE)
 3102                         kif->kf_flags |= KF_FLAG_WRITE;
 3103                 if (fp->f_flag & FAPPEND)
 3104                         kif->kf_flags |= KF_FLAG_APPEND;
 3105                 if (fp->f_flag & FASYNC)
 3106                         kif->kf_flags |= KF_FLAG_ASYNC;
 3107                 if (fp->f_flag & FFSYNC)
 3108                         kif->kf_flags |= KF_FLAG_FSYNC;
 3109                 if (fp->f_flag & FNONBLOCK)
 3110                         kif->kf_flags |= KF_FLAG_NONBLOCK;
 3111                 if (fp->f_flag & O_DIRECT)
 3112                         kif->kf_flags |= KF_FLAG_DIRECT;
 3113                 if (fp->f_flag & FHASLOCK)
 3114                         kif->kf_flags |= KF_FLAG_HASLOCK;
 3115                 kif->kf_offset = fp->f_offset;
 3116                 if (vp != NULL) {
 3117                         vref(vp);
 3118                         switch (vp->v_type) {
 3119                         case VNON:
 3120                                 kif->kf_vnode_type = KF_VTYPE_VNON;
 3121                                 break;
 3122                         case VREG:
 3123                                 kif->kf_vnode_type = KF_VTYPE_VREG;
 3124                                 break;
 3125                         case VDIR:
 3126                                 kif->kf_vnode_type = KF_VTYPE_VDIR;
 3127                                 break;
 3128                         case VBLK:
 3129                                 kif->kf_vnode_type = KF_VTYPE_VBLK;
 3130                                 break;
 3131                         case VCHR:
 3132                                 kif->kf_vnode_type = KF_VTYPE_VCHR;
 3133                                 break;
 3134                         case VLNK:
 3135                                 kif->kf_vnode_type = KF_VTYPE_VLNK;
 3136                                 break;
 3137                         case VSOCK:
 3138                                 kif->kf_vnode_type = KF_VTYPE_VSOCK;
 3139                                 break;
 3140                         case VFIFO:
 3141                                 kif->kf_vnode_type = KF_VTYPE_VFIFO;
 3142                                 break;
 3143                         case VBAD:
 3144                                 kif->kf_vnode_type = KF_VTYPE_VBAD;
 3145                                 break;
 3146                         default:
 3147                                 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
 3148                                 break;
 3149                         }
 3150                         /*
 3151                          * It is OK to drop the filedesc lock here as we will
 3152                          * re-validate and re-evaluate its properties when
 3153                          * the loop continues.
 3154                          */
 3155                         freepath = NULL;
 3156                         fullpath = "-";
 3157                         FILEDESC_SUNLOCK(fdp);
 3158                         vn_fullpath(curthread, vp, &fullpath, &freepath);
 3159                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 3160                         vrele(vp);
 3161                         VFS_UNLOCK_GIANT(vfslocked);
 3162                         strlcpy(kif->kf_path, fullpath,
 3163                             sizeof(kif->kf_path));
 3164                         if (freepath != NULL)
 3165                                 free(freepath, M_TEMP);
 3166                         FILEDESC_SLOCK(fdp);
 3167                 }
 3168                 if (so != NULL) {
 3169                         struct sockaddr *sa;
 3170 
 3171                         if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
 3172                             == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
 3173                                 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
 3174                                 free(sa, M_SONAME);
 3175                         }
 3176                         if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
 3177                             == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
 3178                                 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
 3179                                 free(sa, M_SONAME);
 3180                         }
 3181                         kif->kf_sock_domain =
 3182                             so->so_proto->pr_domain->dom_family;
 3183                         kif->kf_sock_type = so->so_type;
 3184                         kif->kf_sock_protocol = so->so_proto->pr_protocol;
 3185                 }
 3186                 if (tp != NULL) {
 3187                         strlcpy(kif->kf_path, tty_devname(tp),
 3188                             sizeof(kif->kf_path));
 3189                 }
 3190                 if (shmfd != NULL)
 3191                         shm_path(shmfd, kif->kf_path, sizeof(kif->kf_path));
 3192                 error = SYSCTL_OUT(req, kif, sizeof(*kif));
 3193                 if (error)
 3194                         break;
 3195         }
 3196         FILEDESC_SUNLOCK(fdp);
 3197         fddrop(fdp);
 3198         free(kif, M_TEMP);
 3199         return (0);
 3200 }
 3201 
 3202 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
 3203     sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
 3204 #endif  /* COMPAT_FREEBSD7 */
 3205 
 3206 #ifdef KINFO_FILE_SIZE
 3207 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
 3208 #endif
 3209 
 3210 static int
 3211 export_fd_for_sysctl(void *data, int type, int fd, int fflags, int refcnt,
 3212     int64_t offset, int fd_is_cap, cap_rights_t fd_cap_rights,
 3213     struct kinfo_file *kif, struct sysctl_req *req)
 3214 {
 3215         struct {
 3216                 int     fflag;
 3217                 int     kf_fflag;
 3218         } fflags_table[] = {
 3219                 { FAPPEND, KF_FLAG_APPEND },
 3220                 { FASYNC, KF_FLAG_ASYNC },
 3221                 { FFSYNC, KF_FLAG_FSYNC },
 3222                 { FHASLOCK, KF_FLAG_HASLOCK },
 3223                 { FNONBLOCK, KF_FLAG_NONBLOCK },
 3224                 { FREAD, KF_FLAG_READ },
 3225                 { FWRITE, KF_FLAG_WRITE },
 3226                 { O_CREAT, KF_FLAG_CREAT },
 3227                 { O_DIRECT, KF_FLAG_DIRECT },
 3228                 { O_EXCL, KF_FLAG_EXCL },
 3229                 { O_EXEC, KF_FLAG_EXEC },
 3230                 { O_EXLOCK, KF_FLAG_EXLOCK },
 3231                 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
 3232                 { O_SHLOCK, KF_FLAG_SHLOCK },
 3233                 { O_TRUNC, KF_FLAG_TRUNC }
 3234         };
 3235 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
 3236         struct vnode *vp;
 3237         int error, vfslocked;
 3238         unsigned int i;
 3239 
 3240         bzero(kif, sizeof(*kif));
 3241         switch (type) {
 3242         case KF_TYPE_FIFO:
 3243         case KF_TYPE_VNODE:
 3244                 vp = (struct vnode *)data;
 3245                 error = fill_vnode_info(vp, kif);
 3246                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 3247                 vrele(vp);
 3248                 VFS_UNLOCK_GIANT(vfslocked);
 3249                 break;
 3250         case KF_TYPE_SOCKET:
 3251                 error = fill_socket_info((struct socket *)data, kif);
 3252                 break;
 3253         case KF_TYPE_PIPE:
 3254                 error = fill_pipe_info((struct pipe *)data, kif);
 3255                 break;
 3256         case KF_TYPE_PTS:
 3257                 error = fill_pts_info((struct tty *)data, kif);
 3258                 break;
 3259         case KF_TYPE_PROCDESC:
 3260                 error = fill_procdesc_info((struct procdesc *)data, kif);
 3261                 break;
 3262         case KF_TYPE_SHM:
 3263                 error = fill_shm_info((struct file *)data, kif);
 3264                 break;
 3265         default:
 3266                 error = 0;
 3267         }
 3268         if (error == 0)
 3269                 kif->kf_status |= KF_ATTR_VALID;
 3270 
 3271         /*
 3272          * Translate file access flags.
 3273          */
 3274         for (i = 0; i < NFFLAGS; i++)
 3275                 if (fflags & fflags_table[i].fflag)
 3276                         kif->kf_flags |=  fflags_table[i].kf_fflag;
 3277         if (fd_is_cap)
 3278                 kif->kf_flags |= KF_FLAG_CAPABILITY;
 3279         if (fd_is_cap)
 3280                 kif->kf_cap_rights = fd_cap_rights;
 3281         kif->kf_fd = fd;
 3282         kif->kf_type = type;
 3283         kif->kf_ref_count = refcnt;
 3284         kif->kf_offset = offset;
 3285         /* Pack record size down */
 3286         kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
 3287             strlen(kif->kf_path) + 1;
 3288         kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
 3289         error = SYSCTL_OUT(req, kif, kif->kf_structsize);
 3290         return (error);
 3291 }
 3292 
 3293 /*
 3294  * Get per-process file descriptors for use by procstat(1), et al.
 3295  */
 3296 static int
 3297 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
 3298 {
 3299         struct file *fp;
 3300         struct filedesc *fdp;
 3301         struct kinfo_file *kif;
 3302         struct proc *p;
 3303         struct vnode *cttyvp, *textvp, *tracevp;
 3304         size_t oldidx;
 3305         int64_t offset;
 3306         void *data;
 3307         int error, i, *name;
 3308         int fd_is_cap, type, refcnt, fflags;
 3309         cap_rights_t fd_cap_rights;
 3310 
 3311         name = (int *)arg1;
 3312         if ((p = pfind((pid_t)name[0])) == NULL)
 3313                 return (ESRCH);
 3314         if ((error = p_candebug(curthread, p))) {
 3315                 PROC_UNLOCK(p);
 3316                 return (error);
 3317         }
 3318         /* ktrace vnode */
 3319         tracevp = p->p_tracevp;
 3320         if (tracevp != NULL)
 3321                 vref(tracevp);
 3322         /* text vnode */
 3323         textvp = p->p_textvp;
 3324         if (textvp != NULL)
 3325                 vref(textvp);
 3326         /* Controlling tty. */
 3327         cttyvp = NULL;
 3328         if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
 3329                 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
 3330                 if (cttyvp != NULL)
 3331                         vref(cttyvp);
 3332         }
 3333         fdp = fdhold(p);
 3334         PROC_UNLOCK(p);
 3335         kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
 3336         if (tracevp != NULL)
 3337                 export_fd_for_sysctl(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
 3338                     FREAD | FWRITE, -1, -1, 0, 0, kif, req);
 3339         if (textvp != NULL)
 3340                 export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
 3341                     FREAD, -1, -1, 0, 0, kif, req);
 3342         if (cttyvp != NULL)
 3343                 export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
 3344                     FREAD | FWRITE, -1, -1, 0, 0, kif, req);
 3345         if (fdp == NULL)
 3346                 goto fail;
 3347         FILEDESC_SLOCK(fdp);
 3348         /* working directory */
 3349         if (fdp->fd_cdir != NULL) {
 3350                 vref(fdp->fd_cdir);
 3351                 data = fdp->fd_cdir;
 3352                 FILEDESC_SUNLOCK(fdp);
 3353                 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
 3354                     FREAD, -1, -1, 0, 0, kif, req);
 3355                 FILEDESC_SLOCK(fdp);
 3356         }
 3357         /* root directory */
 3358         if (fdp->fd_rdir != NULL) {
 3359                 vref(fdp->fd_rdir);
 3360                 data = fdp->fd_rdir;
 3361                 FILEDESC_SUNLOCK(fdp);
 3362                 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
 3363                     FREAD, -1, -1, 0, 0, kif, req);
 3364                 FILEDESC_SLOCK(fdp);
 3365         }
 3366         /* jail directory */
 3367         if (fdp->fd_jdir != NULL) {
 3368                 vref(fdp->fd_jdir);
 3369                 data = fdp->fd_jdir;
 3370                 FILEDESC_SUNLOCK(fdp);
 3371                 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
 3372                     FREAD, -1, -1, 0, 0, kif, req);
 3373                 FILEDESC_SLOCK(fdp);
 3374         }
 3375         for (i = 0; i < fdp->fd_nfiles; i++) {
 3376                 if ((fp = fdp->fd_ofiles[i]) == NULL)
 3377                         continue;
 3378                 data = NULL;
 3379                 fd_is_cap = 0;
 3380                 fd_cap_rights = 0;
 3381 
 3382 #ifdef CAPABILITIES
 3383                 /*
 3384                  * When reporting a capability, most fields will be from the
 3385                  * underlying object, but do mark as a capability and export
 3386                  * the capability rights mask.
 3387                  */
 3388                 if (fp->f_type == DTYPE_CAPABILITY) {
 3389                         fd_is_cap = 1;
 3390                         fd_cap_rights = cap_rights(fp);
 3391                         (void)cap_funwrap(fp, 0, &fp);
 3392                 }
 3393 #else /* !CAPABILITIES */
 3394                 KASSERT(fp->f_type != DTYPE_CAPABILITY,
 3395                     ("sysctl_kern_proc_filedesc: saw capability"));
 3396 #endif
 3397                 switch (fp->f_type) {
 3398                 case DTYPE_VNODE:
 3399                         type = KF_TYPE_VNODE;
 3400                         vref(fp->f_vnode);
 3401                         data = fp->f_vnode;
 3402                         break;
 3403 
 3404                 case DTYPE_SOCKET:
 3405                         type = KF_TYPE_SOCKET;
 3406                         data = fp->f_data;
 3407                         break;
 3408 
 3409                 case DTYPE_PIPE:
 3410                         type = KF_TYPE_PIPE;
 3411                         data = fp->f_data;
 3412                         break;
 3413 
 3414                 case DTYPE_FIFO:
 3415                         type = KF_TYPE_FIFO;
 3416                         vref(fp->f_vnode);
 3417                         data = fp->f_vnode;
 3418                         break;
 3419 
 3420                 case DTYPE_KQUEUE:
 3421                         type = KF_TYPE_KQUEUE;
 3422                         break;
 3423 
 3424                 case DTYPE_CRYPTO:
 3425                         type = KF_TYPE_CRYPTO;
 3426                         break;
 3427 
 3428                 case DTYPE_MQUEUE:
 3429                         type = KF_TYPE_MQUEUE;
 3430                         break;
 3431 
 3432                 case DTYPE_SHM:
 3433                         type = KF_TYPE_SHM;
 3434                         data = fp;
 3435                         break;
 3436 
 3437                 case DTYPE_SEM:
 3438                         type = KF_TYPE_SEM;
 3439                         break;
 3440 
 3441                 case DTYPE_PTS:
 3442                         type = KF_TYPE_PTS;
 3443                         data = fp->f_data;
 3444                         break;
 3445 
 3446 #ifdef PROCDESC
 3447                 case DTYPE_PROCDESC:
 3448                         type = KF_TYPE_PROCDESC;
 3449                         data = fp->f_data;
 3450                         break;
 3451 #endif
 3452 
 3453                 default:
 3454                         type = KF_TYPE_UNKNOWN;
 3455                         break;
 3456                 }
 3457                 refcnt = fp->f_count;
 3458                 fflags = fp->f_flag;
 3459                 offset = fp->f_offset;
 3460 
 3461                 /*
 3462                  * Create sysctl entry.
 3463                  * It is OK to drop the filedesc lock here as we will
 3464                  * re-validate and re-evaluate its properties when
 3465                  * the loop continues.
 3466                  */
 3467                 oldidx = req->oldidx;
 3468                 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
 3469                         FILEDESC_SUNLOCK(fdp);
 3470                 error = export_fd_for_sysctl(data, type, i, fflags, refcnt,
 3471                     offset, fd_is_cap, fd_cap_rights, kif, req);
 3472                 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
 3473                         FILEDESC_SLOCK(fdp);
 3474                 if (error) {
 3475                         if (error == ENOMEM) {
 3476                                 /*
 3477                                  * The hack to keep the ABI of sysctl
 3478                                  * kern.proc.filedesc intact, but not
 3479                                  * to account a partially copied
 3480                                  * kinfo_file into the oldidx.
 3481                                  */
 3482                                 req->oldidx = oldidx;
 3483                                 error = 0;
 3484                         }
 3485                         break;
 3486                 }
 3487         }
 3488         FILEDESC_SUNLOCK(fdp);
 3489 fail:
 3490         if (fdp != NULL)
 3491                 fddrop(fdp);
 3492         free(kif, M_TEMP);
 3493         return (error);
 3494 }
 3495 
 3496 int
 3497 vntype_to_kinfo(int vtype)
 3498 {
 3499         struct {
 3500                 int     vtype;
 3501                 int     kf_vtype;
 3502         } vtypes_table[] = {
 3503                 { VBAD, KF_VTYPE_VBAD },
 3504                 { VBLK, KF_VTYPE_VBLK },
 3505                 { VCHR, KF_VTYPE_VCHR },
 3506                 { VDIR, KF_VTYPE_VDIR },
 3507                 { VFIFO, KF_VTYPE_VFIFO },
 3508                 { VLNK, KF_VTYPE_VLNK },
 3509                 { VNON, KF_VTYPE_VNON },
 3510                 { VREG, KF_VTYPE_VREG },
 3511                 { VSOCK, KF_VTYPE_VSOCK }
 3512         };
 3513 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
 3514         unsigned int i;
 3515 
 3516         /*
 3517          * Perform vtype translation.
 3518          */
 3519         for (i = 0; i < NVTYPES; i++)
 3520                 if (vtypes_table[i].vtype == vtype)
 3521                         break;
 3522         if (i < NVTYPES)
 3523                 return (vtypes_table[i].kf_vtype);
 3524 
 3525         return (KF_VTYPE_UNKNOWN);
 3526 }
 3527 
 3528 static int
 3529 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
 3530 {
 3531         struct vattr va;
 3532         char *fullpath, *freepath;
 3533         int error, vfslocked;
 3534 
 3535         if (vp == NULL)
 3536                 return (1);
 3537         kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
 3538         freepath = NULL;
 3539         fullpath = "-";
 3540         error = vn_fullpath(curthread, vp, &fullpath, &freepath);
 3541         if (error == 0) {
 3542                 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
 3543         }
 3544         if (freepath != NULL)
 3545                 free(freepath, M_TEMP);
 3546 
 3547         /*
 3548          * Retrieve vnode attributes.
 3549          */
 3550         va.va_fsid = VNOVAL;
 3551         va.va_rdev = NODEV;
 3552         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 3553         vn_lock(vp, LK_SHARED | LK_RETRY);
 3554         error = VOP_GETATTR(vp, &va, curthread->td_ucred);
 3555         VOP_UNLOCK(vp, 0);
 3556         VFS_UNLOCK_GIANT(vfslocked);
 3557         if (error != 0)
 3558                 return (error);
 3559         if (va.va_fsid != VNOVAL)
 3560                 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
 3561         else
 3562                 kif->kf_un.kf_file.kf_file_fsid =
 3563                     vp->v_mount->mnt_stat.f_fsid.val[0];
 3564         kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
 3565         kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
 3566         kif->kf_un.kf_file.kf_file_size = va.va_size;
 3567         kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
 3568         return (0);
 3569 }
 3570 
 3571 static int
 3572 fill_socket_info(struct socket *so, struct kinfo_file *kif)
 3573 {
 3574         struct sockaddr *sa;
 3575         struct inpcb *inpcb;
 3576         struct unpcb *unpcb;
 3577         int error;
 3578 
 3579         if (so == NULL)
 3580                 return (1);
 3581         kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
 3582         kif->kf_sock_type = so->so_type;
 3583         kif->kf_sock_protocol = so->so_proto->pr_protocol;
 3584         kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
 3585         switch(kif->kf_sock_domain) {
 3586         case AF_INET:
 3587         case AF_INET6:
 3588                 if (kif->kf_sock_protocol == IPPROTO_TCP) {
 3589                         if (so->so_pcb != NULL) {
 3590                                 inpcb = (struct inpcb *)(so->so_pcb);
 3591                                 kif->kf_un.kf_sock.kf_sock_inpcb =
 3592                                     (uintptr_t)inpcb->inp_ppcb;
 3593                         }
 3594                 }
 3595                 break;
 3596         case AF_UNIX:
 3597                 if (so->so_pcb != NULL) {
 3598                         unpcb = (struct unpcb *)(so->so_pcb);
 3599                         if (unpcb->unp_conn) {
 3600                                 kif->kf_un.kf_sock.kf_sock_unpconn =
 3601                                     (uintptr_t)unpcb->unp_conn;
 3602                                 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
 3603                                     so->so_rcv.sb_state;
 3604                                 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
 3605                                     so->so_snd.sb_state;
 3606                         }
 3607                 }
 3608                 break;
 3609         }
 3610         error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
 3611         if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
 3612                 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
 3613                 free(sa, M_SONAME);
 3614         }
 3615         error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
 3616         if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
 3617                 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
 3618                 free(sa, M_SONAME);
 3619         }
 3620         strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
 3621             sizeof(kif->kf_path));
 3622         return (0);
 3623 }
 3624 
 3625 static int
 3626 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
 3627 {
 3628 
 3629         if (tp == NULL)
 3630                 return (1);
 3631         kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
 3632         strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
 3633         return (0);
 3634 }
 3635 
 3636 static int
 3637 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
 3638 {
 3639 
 3640         if (pi == NULL)
 3641                 return (1);
 3642         kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
 3643         kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
 3644         kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
 3645         return (0);
 3646 }
 3647 
 3648 static int
 3649 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
 3650 {
 3651 
 3652         if (pdp == NULL)
 3653                 return (1);
 3654         kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
 3655         return (0);
 3656 }
 3657 
 3658 static int
 3659 fill_shm_info(struct file *fp, struct kinfo_file *kif)
 3660 {
 3661         struct thread *td;
 3662         struct stat sb;
 3663 
 3664         td = curthread;
 3665         if (fp->f_data == NULL)
 3666                 return (1);
 3667         if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
 3668                 return (1);
 3669         shm_path(fp->f_data, kif->kf_path, sizeof(kif->kf_path));
 3670         kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
 3671         kif->kf_un.kf_file.kf_file_size = sb.st_size;
 3672         return (0);
 3673 }
 3674 
 3675 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
 3676     sysctl_kern_proc_filedesc, "Process filedesc entries");
 3677 
 3678 #ifdef DDB
 3679 /*
 3680  * For the purposes of debugging, generate a human-readable string for the
 3681  * file type.
 3682  */
 3683 static const char *
 3684 file_type_to_name(short type)
 3685 {
 3686 
 3687         switch (type) {
 3688         case 0:
 3689                 return ("zero");
 3690         case DTYPE_VNODE:
 3691                 return ("vnod");
 3692         case DTYPE_SOCKET:
 3693                 return ("sock");
 3694         case DTYPE_PIPE:
 3695                 return ("pipe");
 3696         case DTYPE_FIFO:
 3697                 return ("fifo");
 3698         case DTYPE_KQUEUE:
 3699                 return ("kque");
 3700         case DTYPE_CRYPTO:
 3701                 return ("crpt");
 3702         case DTYPE_MQUEUE:
 3703                 return ("mque");
 3704         case DTYPE_SHM:
 3705                 return ("shm");
 3706         case DTYPE_SEM:
 3707                 return ("ksem");
 3708         default:
 3709                 return ("unkn");
 3710         }
 3711 }
 3712 
 3713 /*
 3714  * For the purposes of debugging, identify a process (if any, perhaps one of
 3715  * many) that references the passed file in its file descriptor array. Return
 3716  * NULL if none.
 3717  */
 3718 static struct proc *
 3719 file_to_first_proc(struct file *fp)
 3720 {
 3721         struct filedesc *fdp;
 3722         struct proc *p;
 3723         int n;
 3724 
 3725         FOREACH_PROC_IN_SYSTEM(p) {
 3726                 if (p->p_state == PRS_NEW)
 3727                         continue;
 3728                 fdp = p->p_fd;
 3729                 if (fdp == NULL)
 3730                         continue;
 3731                 for (n = 0; n < fdp->fd_nfiles; n++) {
 3732                         if (fp == fdp->fd_ofiles[n])
 3733                                 return (p);
 3734                 }
 3735         }
 3736         return (NULL);
 3737 }
 3738 
 3739 static void
 3740 db_print_file(struct file *fp, int header)
 3741 {
 3742         struct proc *p;
 3743 
 3744         if (header)
 3745                 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
 3746                     "File", "Type", "Data", "Flag", "GCFl", "Count",
 3747                     "MCount", "Vnode", "FPID", "FCmd");
 3748         p = file_to_first_proc(fp);
 3749         db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
 3750             file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
 3751             0, fp->f_count, 0, fp->f_vnode,
 3752             p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
 3753 }
 3754 
 3755 DB_SHOW_COMMAND(file, db_show_file)
 3756 {
 3757         struct file *fp;
 3758 
 3759         if (!have_addr) {
 3760                 db_printf("usage: show file <addr>\n");
 3761                 return;
 3762         }
 3763         fp = (struct file *)addr;
 3764         db_print_file(fp, 1);
 3765 }
 3766 
 3767 DB_SHOW_COMMAND(files, db_show_files)
 3768 {
 3769         struct filedesc *fdp;
 3770         struct file *fp;
 3771         struct proc *p;
 3772         int header;
 3773         int n;
 3774 
 3775         header = 1;
 3776         FOREACH_PROC_IN_SYSTEM(p) {
 3777                 if (p->p_state == PRS_NEW)
 3778                         continue;
 3779                 if ((fdp = p->p_fd) == NULL)
 3780                         continue;
 3781                 for (n = 0; n < fdp->fd_nfiles; ++n) {
 3782                         if ((fp = fdp->fd_ofiles[n]) == NULL)
 3783                                 continue;
 3784                         db_print_file(fp, header);
 3785                         header = 0;
 3786                 }
 3787         }
 3788 }
 3789 #endif
 3790 
 3791 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
 3792     &maxfilesperproc, 0, "Maximum files allowed open per process");
 3793 
 3794 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
 3795     &maxfiles, 0, "Maximum number of files");
 3796 
 3797 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
 3798     __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
 3799 
 3800 /* ARGSUSED*/
 3801 static void
 3802 filelistinit(void *dummy)
 3803 {
 3804 
 3805         file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
 3806             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
 3807         mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
 3808         mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
 3809 }
 3810 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
 3811 
 3812 /*-------------------------------------------------------------------*/
 3813 
 3814 static int
 3815 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred,
 3816     int flags, struct thread *td)
 3817 {
 3818 
 3819         return (EBADF);
 3820 }
 3821 
 3822 static int
 3823 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
 3824     struct thread *td)
 3825 {
 3826 
 3827         return (EINVAL);
 3828 }
 3829 
 3830 static int
 3831 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
 3832     struct thread *td)
 3833 {
 3834 
 3835         return (EBADF);
 3836 }
 3837 
 3838 static int
 3839 badfo_poll(struct file *fp, int events, struct ucred *active_cred,
 3840     struct thread *td)
 3841 {
 3842 
 3843         return (0);
 3844 }
 3845 
 3846 static int
 3847 badfo_kqfilter(struct file *fp, struct knote *kn)
 3848 {
 3849 
 3850         return (EBADF);
 3851 }
 3852 
 3853 static int
 3854 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
 3855     struct thread *td)
 3856 {
 3857 
 3858         return (EBADF);
 3859 }
 3860 
 3861 static int
 3862 badfo_close(struct file *fp, struct thread *td)
 3863 {
 3864 
 3865         return (EBADF);
 3866 }
 3867 
 3868 static int
 3869 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
 3870     struct thread *td)
 3871 {
 3872 
 3873         return (EBADF);
 3874 }
 3875 
 3876 static int
 3877 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
 3878     struct thread *td)
 3879 {
 3880 
 3881         return (EBADF);
 3882 }
 3883 
 3884 struct fileops badfileops = {
 3885         .fo_read = badfo_readwrite,
 3886         .fo_write = badfo_readwrite,
 3887         .fo_truncate = badfo_truncate,
 3888         .fo_ioctl = badfo_ioctl,
 3889         .fo_poll = badfo_poll,
 3890         .fo_kqfilter = badfo_kqfilter,
 3891         .fo_stat = badfo_stat,
 3892         .fo_close = badfo_close,
 3893         .fo_chmod = badfo_chmod,
 3894         .fo_chown = badfo_chown,
 3895 };
 3896 
 3897 int
 3898 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
 3899     struct thread *td)
 3900 {
 3901 
 3902         return (EINVAL);
 3903 }
 3904 
 3905 int
 3906 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
 3907     struct thread *td)
 3908 {
 3909 
 3910         return (EINVAL);
 3911 }
 3912 
 3913 /*-------------------------------------------------------------------*/
 3914 
 3915 /*
 3916  * File Descriptor pseudo-device driver (/dev/fd/).
 3917  *
 3918  * Opening minor device N dup()s the file (if any) connected to file
 3919  * descriptor N belonging to the calling process.  Note that this driver
 3920  * consists of only the ``open()'' routine, because all subsequent
 3921  * references to this file will be direct to the other driver.
 3922  *
 3923  * XXX: we could give this one a cloning event handler if necessary.
 3924  */
 3925 
 3926 /* ARGSUSED */
 3927 static int
 3928 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
 3929 {
 3930 
 3931         /*
 3932          * XXX Kludge: set curthread->td_dupfd to contain the value of the
 3933          * the file descriptor being sought for duplication. The error
 3934          * return ensures that the vnode for this device will be released
 3935          * by vn_open. Open will detect this special error and take the
 3936          * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
 3937          * will simply report the error.
 3938          */
 3939         td->td_dupfd = dev2unit(dev);
 3940         return (ENODEV);
 3941 }
 3942 
 3943 static struct cdevsw fildesc_cdevsw = {
 3944         .d_version =    D_VERSION,
 3945         .d_open =       fdopen,
 3946         .d_name =       "FD",
 3947 };
 3948 
 3949 static void
 3950 fildesc_drvinit(void *unused)
 3951 {
 3952         struct cdev *dev;
 3953 
 3954         dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
 3955             UID_ROOT, GID_WHEEL, 0666, "fd/0");
 3956         make_dev_alias(dev, "stdin");
 3957         dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
 3958             UID_ROOT, GID_WHEEL, 0666, "fd/1");
 3959         make_dev_alias(dev, "stdout");
 3960         dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
 3961             UID_ROOT, GID_WHEEL, 0666, "fd/2");
 3962         make_dev_alias(dev, "stderr");
 3963 }
 3964 
 3965 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);

Cache object: bc47a59ef599e1b9f1e1b9fd6904950b


[ 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.