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/7.4/sys/kern/kern_descrip.c 195694 2009-07-14 19:48:31Z jhb $");
   39 
   40 #include "opt_compat.h"
   41 #include "opt_ddb.h"
   42 #include "opt_ktrace.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 
   47 #include <sys/conf.h>
   48 #include <sys/domain.h>
   49 #include <sys/fcntl.h>
   50 #include <sys/file.h>
   51 #include <sys/filedesc.h>
   52 #include <sys/filio.h>
   53 #include <sys/jail.h>
   54 #include <sys/kernel.h>
   55 #include <sys/limits.h>
   56 #include <sys/lock.h>
   57 #include <sys/malloc.h>
   58 #include <sys/mount.h>
   59 #include <sys/mqueue.h>
   60 #include <sys/mutex.h>
   61 #include <sys/namei.h>
   62 #include <sys/priv.h>
   63 #include <sys/proc.h>
   64 #include <sys/protosw.h>
   65 #include <sys/resourcevar.h>
   66 #include <sys/signalvar.h>
   67 #include <sys/socketvar.h>
   68 #include <sys/stat.h>
   69 #include <sys/sx.h>
   70 #include <sys/syscallsubr.h>
   71 #include <sys/sysctl.h>
   72 #include <sys/sysproto.h>
   73 #include <sys/unistd.h>
   74 #include <sys/user.h>
   75 #include <sys/vnode.h>
   76 #ifdef KTRACE
   77 #include <sys/ktrace.h>
   78 #endif
   79 
   80 #include <security/audit/audit.h>
   81 
   82 #include <vm/uma.h>
   83 
   84 #include <ddb/ddb.h>
   85 
   86 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
   87 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
   88                      "file desc to leader structures");
   89 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
   90 
   91 static uma_zone_t file_zone;
   92 
   93 
   94 /* How to treat 'new' parameter when allocating a fd for do_dup(). */
   95 enum dup_type { DUP_VARIABLE, DUP_FIXED };
   96 
   97 static int do_dup(struct thread *td, enum dup_type type, int old, int new,
   98     register_t *retval);
   99 static int      fd_first_free(struct filedesc *, int, int);
  100 static int      fd_last_used(struct filedesc *, int, int);
  101 static void     fdgrowtable(struct filedesc *, int);
  102 static int      fdrop_locked(struct file *fp, struct thread *td);
  103 static void     fdunused(struct filedesc *fdp, int fd);
  104 static void     fdused(struct filedesc *fdp, int fd);
  105 
  106 /*
  107  * A process is initially started out with NDFILE descriptors stored within
  108  * this structure, selected to be enough for typical applications based on
  109  * the historical limit of 20 open files (and the usage of descriptors by
  110  * shells).  If these descriptors are exhausted, a larger descriptor table
  111  * may be allocated, up to a process' resource limit; the internal arrays
  112  * are then unused.
  113  */
  114 #define NDFILE          20
  115 #define NDSLOTSIZE      sizeof(NDSLOTTYPE)
  116 #define NDENTRIES       (NDSLOTSIZE * __CHAR_BIT)
  117 #define NDSLOT(x)       ((x) / NDENTRIES)
  118 #define NDBIT(x)        ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
  119 #define NDSLOTS(x)      (((x) + NDENTRIES - 1) / NDENTRIES)
  120 
  121 /*
  122  * Storage required per open file descriptor.
  123  */
  124 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
  125 
  126 /*
  127  * Basic allocation of descriptors:
  128  * one of the above, plus arrays for NDFILE descriptors.
  129  */
  130 struct filedesc0 {
  131         struct  filedesc fd_fd;
  132         /*
  133          * These arrays are used when the number of open files is
  134          * <= NDFILE, and are then pointed to by the pointers above.
  135          */
  136         struct  file *fd_dfiles[NDFILE];
  137         char    fd_dfileflags[NDFILE];
  138         NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
  139 };
  140 
  141 /*
  142  * Descriptor management.
  143  */
  144 struct filelist filehead;       /* head of list of open files */
  145 int openfiles;                  /* actual number of open files */
  146 struct sx filelist_lock;        /* sx to protect filelist */
  147 struct mtx sigio_lock;          /* mtx to protect pointers to sigio */
  148 void    (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
  149 
  150 /* A mutex to protect the association between a proc and filedesc. */
  151 static struct mtx       fdesc_mtx;
  152 
  153 /*
  154  * Find the first zero bit in the given bitmap, starting at low and not
  155  * exceeding size - 1.
  156  */
  157 static int
  158 fd_first_free(struct filedesc *fdp, int low, int size)
  159 {
  160         NDSLOTTYPE *map = fdp->fd_map;
  161         NDSLOTTYPE mask;
  162         int off, maxoff;
  163 
  164         if (low >= size)
  165                 return (low);
  166 
  167         off = NDSLOT(low);
  168         if (low % NDENTRIES) {
  169                 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
  170                 if ((mask &= ~map[off]) != 0UL)
  171                         return (off * NDENTRIES + ffsl(mask) - 1);
  172                 ++off;
  173         }
  174         for (maxoff = NDSLOTS(size); off < maxoff; ++off)
  175                 if (map[off] != ~0UL)
  176                         return (off * NDENTRIES + ffsl(~map[off]) - 1);
  177         return (size);
  178 }
  179 
  180 /*
  181  * Find the highest non-zero bit in the given bitmap, starting at low and
  182  * not exceeding size - 1.
  183  */
  184 static int
  185 fd_last_used(struct filedesc *fdp, int low, int size)
  186 {
  187         NDSLOTTYPE *map = fdp->fd_map;
  188         NDSLOTTYPE mask;
  189         int off, minoff;
  190 
  191         if (low >= size)
  192                 return (-1);
  193 
  194         off = NDSLOT(size);
  195         if (size % NDENTRIES) {
  196                 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
  197                 if ((mask &= map[off]) != 0)
  198                         return (off * NDENTRIES + flsl(mask) - 1);
  199                 --off;
  200         }
  201         for (minoff = NDSLOT(low); off >= minoff; --off)
  202                 if (map[off] != 0)
  203                         return (off * NDENTRIES + flsl(map[off]) - 1);
  204         return (low - 1);
  205 }
  206 
  207 static int
  208 fdisused(struct filedesc *fdp, int fd)
  209 {
  210         KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
  211             ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
  212         return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
  213 }
  214 
  215 /*
  216  * Mark a file descriptor as used.
  217  */
  218 static void
  219 fdused(struct filedesc *fdp, int fd)
  220 {
  221 
  222         FILEDESC_XLOCK_ASSERT(fdp);
  223         KASSERT(!fdisused(fdp, fd),
  224             ("fd already used"));
  225 
  226         fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
  227         if (fd > fdp->fd_lastfile)
  228                 fdp->fd_lastfile = fd;
  229         if (fd == fdp->fd_freefile)
  230                 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
  231 }
  232 
  233 /*
  234  * Mark a file descriptor as unused.
  235  */
  236 static void
  237 fdunused(struct filedesc *fdp, int fd)
  238 {
  239 
  240         FILEDESC_XLOCK_ASSERT(fdp);
  241         KASSERT(fdisused(fdp, fd),
  242             ("fd is already unused"));
  243         KASSERT(fdp->fd_ofiles[fd] == NULL,
  244             ("fd is still in use"));
  245 
  246         fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
  247         if (fd < fdp->fd_freefile)
  248                 fdp->fd_freefile = fd;
  249         if (fd == fdp->fd_lastfile)
  250                 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
  251 }
  252 
  253 /*
  254  * System calls on descriptors.
  255  */
  256 #ifndef _SYS_SYSPROTO_H_
  257 struct getdtablesize_args {
  258         int     dummy;
  259 };
  260 #endif
  261 /* ARGSUSED */
  262 int
  263 getdtablesize(struct thread *td, struct getdtablesize_args *uap)
  264 {
  265         struct proc *p = td->td_proc;
  266 
  267         PROC_LOCK(p);
  268         td->td_retval[0] =
  269             min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
  270         PROC_UNLOCK(p);
  271         return (0);
  272 }
  273 
  274 /*
  275  * Duplicate a file descriptor to a particular value.
  276  *
  277  * Note: keep in mind that a potential race condition exists when closing
  278  * descriptors from a shared descriptor table (via rfork).
  279  */
  280 #ifndef _SYS_SYSPROTO_H_
  281 struct dup2_args {
  282         u_int   from;
  283         u_int   to;
  284 };
  285 #endif
  286 /* ARGSUSED */
  287 int
  288 dup2(struct thread *td, struct dup2_args *uap)
  289 {
  290 
  291         return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
  292                     td->td_retval));
  293 }
  294 
  295 /*
  296  * Duplicate a file descriptor.
  297  */
  298 #ifndef _SYS_SYSPROTO_H_
  299 struct dup_args {
  300         u_int   fd;
  301 };
  302 #endif
  303 /* ARGSUSED */
  304 int
  305 dup(struct thread *td, struct dup_args *uap)
  306 {
  307 
  308         return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
  309 }
  310 
  311 /*
  312  * The file control system call.
  313  */
  314 #ifndef _SYS_SYSPROTO_H_
  315 struct fcntl_args {
  316         int     fd;
  317         int     cmd;
  318         long    arg;
  319 };
  320 #endif
  321 /* ARGSUSED */
  322 int
  323 fcntl(struct thread *td, struct fcntl_args *uap)
  324 {
  325         struct flock fl;
  326         struct oflock ofl;
  327         intptr_t arg;
  328         int error;
  329         int cmd;
  330 
  331         error = 0;
  332         cmd = uap->cmd;
  333         switch (uap->cmd) {
  334         case F_OGETLK:
  335         case F_OSETLK:
  336         case F_OSETLKW:
  337                 /*
  338                  * Convert old flock structure to new.
  339                  */
  340                 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
  341                 fl.l_start = ofl.l_start;
  342                 fl.l_len = ofl.l_len;
  343                 fl.l_pid = ofl.l_pid;
  344                 fl.l_type = ofl.l_type;
  345                 fl.l_whence = ofl.l_whence;
  346                 fl.l_sysid = 0;
  347 
  348                 switch (uap->cmd) {
  349                 case F_OGETLK:
  350                     cmd = F_GETLK;
  351                     break;
  352                 case F_OSETLK:
  353                     cmd = F_SETLK;
  354                     break;
  355                 case F_OSETLKW:
  356                     cmd = F_SETLKW;
  357                     break;
  358                 }
  359                 arg = (intptr_t)&fl;
  360                 break;
  361         case F_GETLK:
  362         case F_SETLK:
  363         case F_SETLKW:
  364         case F_SETLK_REMOTE:
  365                 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
  366                 arg = (intptr_t)&fl;
  367                 break;
  368         default:
  369                 arg = uap->arg;
  370                 break;
  371         }
  372         if (error)
  373                 return (error);
  374         error = kern_fcntl(td, uap->fd, cmd, arg);
  375         if (error)
  376                 return (error);
  377         if (uap->cmd == F_OGETLK) {
  378                 ofl.l_start = fl.l_start;
  379                 ofl.l_len = fl.l_len;
  380                 ofl.l_pid = fl.l_pid;
  381                 ofl.l_type = fl.l_type;
  382                 ofl.l_whence = fl.l_whence;
  383                 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
  384         } else if (uap->cmd == F_GETLK) {
  385                 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
  386         }
  387         return (error);
  388 }
  389 
  390 static inline struct file *
  391 fdtofp(int fd, struct filedesc *fdp)
  392 {
  393         struct file *fp;
  394 
  395         FILEDESC_LOCK_ASSERT(fdp);
  396         if ((unsigned)fd >= fdp->fd_nfiles ||
  397             (fp = fdp->fd_ofiles[fd]) == NULL)
  398                 return (NULL);
  399         return (fp);
  400 }
  401 
  402 int
  403 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
  404 {
  405         struct filedesc *fdp;
  406         struct flock *flp;
  407         struct file *fp;
  408         struct proc *p;
  409         char *pop;
  410         struct vnode *vp;
  411         u_int newmin;
  412         int error, flg, tmp;
  413         int vfslocked;
  414 
  415         vfslocked = 0;
  416         error = 0;
  417         flg = F_POSIX;
  418         p = td->td_proc;
  419         fdp = p->p_fd;
  420 
  421         switch (cmd) {
  422         case F_DUPFD:
  423                 FILEDESC_SLOCK(fdp);
  424                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  425                         FILEDESC_SUNLOCK(fdp);
  426                         error = EBADF;
  427                         break;
  428                 }
  429                 FILEDESC_SUNLOCK(fdp);
  430                 newmin = arg;
  431                 PROC_LOCK(p);
  432                 if (newmin >= lim_cur(p, RLIMIT_NOFILE) ||
  433                     newmin >= maxfilesperproc) {
  434                         PROC_UNLOCK(p);
  435                         error = EINVAL;
  436                         break;
  437                 }
  438                 PROC_UNLOCK(p);
  439                 error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
  440                 break;
  441 
  442         case F_DUP2FD:
  443                 tmp = arg;
  444                 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
  445                 break;
  446 
  447         case F_GETFD:
  448                 FILEDESC_SLOCK(fdp);
  449                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  450                         FILEDESC_SUNLOCK(fdp);
  451                         error = EBADF;
  452                         break;
  453                 }
  454                 pop = &fdp->fd_ofileflags[fd];
  455                 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
  456                 FILEDESC_SUNLOCK(fdp);
  457                 break;
  458 
  459         case F_SETFD:
  460                 FILEDESC_XLOCK(fdp);
  461                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  462                         FILEDESC_XUNLOCK(fdp);
  463                         error = EBADF;
  464                         break;
  465                 }
  466                 pop = &fdp->fd_ofileflags[fd];
  467                 *pop = (*pop &~ UF_EXCLOSE) |
  468                     (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
  469                 FILEDESC_XUNLOCK(fdp);
  470                 break;
  471 
  472         case F_GETFL:
  473                 FILEDESC_SLOCK(fdp);
  474                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  475                         FILEDESC_SUNLOCK(fdp);
  476                         error = EBADF;
  477                         break;
  478                 }
  479                 FILE_LOCK(fp);
  480                 td->td_retval[0] = OFLAGS(fp->f_flag);
  481                 FILE_UNLOCK(fp);
  482                 FILEDESC_SUNLOCK(fdp);
  483                 break;
  484 
  485         case F_SETFL:
  486                 FILEDESC_SLOCK(fdp);
  487                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  488                         FILEDESC_SUNLOCK(fdp);
  489                         error = EBADF;
  490                         break;
  491                 }
  492                 FILE_LOCK(fp);
  493                 fhold_locked(fp);
  494                 fp->f_flag &= ~FCNTLFLAGS;
  495                 fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
  496                 FILE_UNLOCK(fp);
  497                 FILEDESC_SUNLOCK(fdp);
  498                 tmp = fp->f_flag & FNONBLOCK;
  499                 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  500                 if (error) {
  501                         fdrop(fp, td);
  502                         break;
  503                 }
  504                 tmp = fp->f_flag & FASYNC;
  505                 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
  506                 if (error == 0) {
  507                         fdrop(fp, td);
  508                         break;
  509                 }
  510                 FILE_LOCK(fp);
  511                 fp->f_flag &= ~FNONBLOCK;
  512                 FILE_UNLOCK(fp);
  513                 tmp = 0;
  514                 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  515                 fdrop(fp, td);
  516                 break;
  517 
  518         case F_GETOWN:
  519                 FILEDESC_SLOCK(fdp);
  520                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  521                         FILEDESC_SUNLOCK(fdp);
  522                         error = EBADF;
  523                         break;
  524                 }
  525                 fhold(fp);
  526                 FILEDESC_SUNLOCK(fdp);
  527                 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
  528                 if (error == 0)
  529                         td->td_retval[0] = tmp;
  530                 fdrop(fp, td);
  531                 break;
  532 
  533         case F_SETOWN:
  534                 FILEDESC_SLOCK(fdp);
  535                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  536                         FILEDESC_SUNLOCK(fdp);
  537                         error = EBADF;
  538                         break;
  539                 }
  540                 fhold(fp);
  541                 FILEDESC_SUNLOCK(fdp);
  542                 tmp = arg;
  543                 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
  544                 fdrop(fp, td);
  545                 break;
  546 
  547         case F_SETLK_REMOTE:
  548                 error = priv_check(td, PRIV_NFS_LOCKD);
  549                 if (error)
  550                         return (error);
  551                 flg = F_REMOTE;
  552                 goto do_setlk;
  553 
  554         case F_SETLKW:
  555                 flg |= F_WAIT;
  556                 /* FALLTHROUGH F_SETLK */
  557 
  558         case F_SETLK:
  559         do_setlk:
  560                 FILEDESC_SLOCK(fdp);
  561                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  562                         FILEDESC_SUNLOCK(fdp);
  563                         error = EBADF;
  564                         break;
  565                 }
  566                 if (fp->f_type != DTYPE_VNODE) {
  567                         FILEDESC_SUNLOCK(fdp);
  568                         error = EBADF;
  569                         break;
  570                 }
  571                 flp = (struct flock *)arg;
  572                 if (flp->l_whence == SEEK_CUR) {
  573                         if (fp->f_offset < 0 ||
  574                             (flp->l_start > 0 &&
  575                              fp->f_offset > OFF_MAX - flp->l_start)) {
  576                                 FILEDESC_SUNLOCK(fdp);
  577                                 error = EOVERFLOW;
  578                                 break;
  579                         }
  580                         flp->l_start += fp->f_offset;
  581                 }
  582 
  583                 /*
  584                  * VOP_ADVLOCK() may block.
  585                  */
  586                 fhold(fp);
  587                 FILEDESC_SUNLOCK(fdp);
  588                 vp = fp->f_vnode;
  589                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  590                 switch (flp->l_type) {
  591                 case F_RDLCK:
  592                         if ((fp->f_flag & FREAD) == 0) {
  593                                 error = EBADF;
  594                                 break;
  595                         }
  596                         PROC_LOCK(p->p_leader);
  597                         p->p_leader->p_flag |= P_ADVLOCK;
  598                         PROC_UNLOCK(p->p_leader);
  599                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  600                             flp, flg);
  601                         break;
  602                 case F_WRLCK:
  603                         if ((fp->f_flag & FWRITE) == 0) {
  604                                 error = EBADF;
  605                                 break;
  606                         }
  607                         PROC_LOCK(p->p_leader);
  608                         p->p_leader->p_flag |= P_ADVLOCK;
  609                         PROC_UNLOCK(p->p_leader);
  610                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  611                             flp, flg);
  612                         break;
  613                 case F_UNLCK:
  614                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
  615                             flp, flg);
  616                         break;
  617                 case F_UNLCKSYS:
  618                         /*
  619                          * Temporary api for testing remote lock
  620                          * infrastructure.
  621                          */
  622                         if (flg != F_REMOTE) {
  623                                 error = EINVAL;
  624                                 break;
  625                         }
  626                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
  627                             F_UNLCKSYS, flp, flg);
  628                         break;
  629                 default:
  630                         error = EINVAL;
  631                         break;
  632                 }
  633                 VFS_UNLOCK_GIANT(vfslocked);
  634                 vfslocked = 0;
  635                 /* Check for race with close */
  636                 FILEDESC_SLOCK(fdp);
  637                 if ((unsigned) fd >= fdp->fd_nfiles ||
  638                     fp != fdp->fd_ofiles[fd]) {
  639                         FILEDESC_SUNLOCK(fdp);
  640                         flp->l_whence = SEEK_SET;
  641                         flp->l_start = 0;
  642                         flp->l_len = 0;
  643                         flp->l_type = F_UNLCK;
  644                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  645                         (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
  646                                            F_UNLCK, flp, F_POSIX);
  647                         VFS_UNLOCK_GIANT(vfslocked);
  648                         vfslocked = 0;
  649                 } else
  650                         FILEDESC_SUNLOCK(fdp);
  651                 fdrop(fp, td);
  652                 break;
  653 
  654         case F_GETLK:
  655                 FILEDESC_SLOCK(fdp);
  656                 if ((fp = fdtofp(fd, fdp)) == NULL) {
  657                         FILEDESC_SUNLOCK(fdp);
  658                         error = EBADF;
  659                         break;
  660                 }
  661                 if (fp->f_type != DTYPE_VNODE) {
  662                         FILEDESC_SUNLOCK(fdp);
  663                         error = EBADF;
  664                         break;
  665                 }
  666                 flp = (struct flock *)arg;
  667                 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
  668                     flp->l_type != F_UNLCK) {
  669                         FILEDESC_SUNLOCK(fdp);
  670                         error = EINVAL;
  671                         break;
  672                 }
  673                 if (flp->l_whence == SEEK_CUR) {
  674                         if ((flp->l_start > 0 &&
  675                             fp->f_offset > OFF_MAX - flp->l_start) ||
  676                             (flp->l_start < 0 &&
  677                              fp->f_offset < OFF_MIN - flp->l_start)) {
  678                                 FILEDESC_SUNLOCK(fdp);
  679                                 error = EOVERFLOW;
  680                                 break;
  681                         }
  682                         flp->l_start += fp->f_offset;
  683                 }
  684                 /*
  685                  * VOP_ADVLOCK() may block.
  686                  */
  687                 fhold(fp);
  688                 FILEDESC_SUNLOCK(fdp);
  689                 vp = fp->f_vnode;
  690                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  691                 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
  692                     F_POSIX);
  693                 VFS_UNLOCK_GIANT(vfslocked);
  694                 vfslocked = 0;
  695                 fdrop(fp, td);
  696                 break;
  697         default:
  698                 error = EINVAL;
  699                 break;
  700         }
  701         VFS_UNLOCK_GIANT(vfslocked);
  702         return (error);
  703 }
  704 
  705 /*
  706  * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
  707  */
  708 static int
  709 do_dup(struct thread *td, enum dup_type type, int old, int new,
  710     register_t *retval)
  711 {
  712         struct filedesc *fdp;
  713         struct proc *p;
  714         struct file *fp;
  715         struct file *delfp;
  716         int error, holdleaders, maxfd;
  717 
  718         KASSERT((type == DUP_VARIABLE || type == DUP_FIXED),
  719             ("invalid dup type %d", type));
  720 
  721         p = td->td_proc;
  722         fdp = p->p_fd;
  723 
  724         /*
  725          * Verify we have a valid descriptor to dup from and possibly to
  726          * dup to.
  727          */
  728         if (old < 0 || new < 0)
  729                 return (EBADF);
  730         PROC_LOCK(p);
  731         maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
  732         PROC_UNLOCK(p);
  733         if (new >= maxfd)
  734                 return (EMFILE);
  735 
  736         FILEDESC_XLOCK(fdp);
  737         if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
  738                 FILEDESC_XUNLOCK(fdp);
  739                 return (EBADF);
  740         }
  741         if (type == DUP_FIXED && old == new) {
  742                 *retval = new;
  743                 FILEDESC_XUNLOCK(fdp);
  744                 return (0);
  745         }
  746         fp = fdp->fd_ofiles[old];
  747         fhold(fp);
  748 
  749         /*
  750          * If the caller specified a file descriptor, make sure the file
  751          * table is large enough to hold it, and grab it.  Otherwise, just
  752          * allocate a new descriptor the usual way.  Since the filedesc
  753          * lock may be temporarily dropped in the process, we have to look
  754          * out for a race.
  755          */
  756         if (type == DUP_FIXED) {
  757                 if (new >= fdp->fd_nfiles)
  758                         fdgrowtable(fdp, new + 1);
  759                 if (fdp->fd_ofiles[new] == NULL)
  760                         fdused(fdp, new);
  761         } else {
  762                 if ((error = fdalloc(td, new, &new)) != 0) {
  763                         FILEDESC_XUNLOCK(fdp);
  764                         fdrop(fp, td);
  765                         return (error);
  766                 }
  767         }
  768 
  769         /*
  770          * If the old file changed out from under us then treat it as a
  771          * bad file descriptor.  Userland should do its own locking to
  772          * avoid this case.
  773          */
  774         if (fdp->fd_ofiles[old] != fp) {
  775                 /* we've allocated a descriptor which we won't use */
  776                 if (fdp->fd_ofiles[new] == NULL)
  777                         fdunused(fdp, new);
  778                 FILEDESC_XUNLOCK(fdp);
  779                 fdrop(fp, td);
  780                 return (EBADF);
  781         }
  782         KASSERT(old != new,
  783             ("new fd is same as old"));
  784 
  785         /*
  786          * Save info on the descriptor being overwritten.  We cannot close
  787          * it without introducing an ownership race for the slot, since we
  788          * need to drop the filedesc lock to call closef().
  789          *
  790          * XXX this duplicates parts of close().
  791          */
  792         delfp = fdp->fd_ofiles[new];
  793         holdleaders = 0;
  794         if (delfp != NULL) {
  795                 if (td->td_proc->p_fdtol != NULL) {
  796                         /*
  797                          * Ask fdfree() to sleep to ensure that all relevant
  798                          * process leaders can be traversed in closef().
  799                          */
  800                         fdp->fd_holdleaderscount++;
  801                         holdleaders = 1;
  802                 }
  803         }
  804 
  805         /*
  806          * Duplicate the source descriptor
  807          */
  808         fdp->fd_ofiles[new] = fp;
  809         fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
  810         if (new > fdp->fd_lastfile)
  811                 fdp->fd_lastfile = new;
  812         *retval = new;
  813 
  814         /*
  815          * If we dup'd over a valid file, we now own the reference to it
  816          * and must dispose of it using closef() semantics (as if a
  817          * close() were performed on it).
  818          *
  819          * XXX this duplicates parts of close().
  820          */
  821         if (delfp != NULL) {
  822                 knote_fdclose(td, new);
  823                 if (delfp->f_type == DTYPE_MQUEUE)
  824                         mq_fdclose(td, new, delfp);
  825                 FILEDESC_XUNLOCK(fdp);
  826                 (void) closef(delfp, td);
  827                 if (holdleaders) {
  828                         FILEDESC_XLOCK(fdp);
  829                         fdp->fd_holdleaderscount--;
  830                         if (fdp->fd_holdleaderscount == 0 &&
  831                             fdp->fd_holdleaderswakeup != 0) {
  832                                 fdp->fd_holdleaderswakeup = 0;
  833                                 wakeup(&fdp->fd_holdleaderscount);
  834                         }
  835                         FILEDESC_XUNLOCK(fdp);
  836                 }
  837         } else {
  838                 FILEDESC_XUNLOCK(fdp);
  839         }
  840         return (0);
  841 }
  842 
  843 /*
  844  * If sigio is on the list associated with a process or process group,
  845  * disable signalling from the device, remove sigio from the list and
  846  * free sigio.
  847  */
  848 void
  849 funsetown(struct sigio **sigiop)
  850 {
  851         struct sigio *sigio;
  852 
  853         SIGIO_LOCK();
  854         sigio = *sigiop;
  855         if (sigio == NULL) {
  856                 SIGIO_UNLOCK();
  857                 return;
  858         }
  859         *(sigio->sio_myref) = NULL;
  860         if ((sigio)->sio_pgid < 0) {
  861                 struct pgrp *pg = (sigio)->sio_pgrp;
  862                 PGRP_LOCK(pg);
  863                 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
  864                              sigio, sio_pgsigio);
  865                 PGRP_UNLOCK(pg);
  866         } else {
  867                 struct proc *p = (sigio)->sio_proc;
  868                 PROC_LOCK(p);
  869                 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
  870                              sigio, sio_pgsigio);
  871                 PROC_UNLOCK(p);
  872         }
  873         SIGIO_UNLOCK();
  874         crfree(sigio->sio_ucred);
  875         FREE(sigio, M_SIGIO);
  876 }
  877 
  878 /*
  879  * Free a list of sigio structures.
  880  * We only need to lock the SIGIO_LOCK because we have made ourselves
  881  * inaccessible to callers of fsetown and therefore do not need to lock
  882  * the proc or pgrp struct for the list manipulation.
  883  */
  884 void
  885 funsetownlst(struct sigiolst *sigiolst)
  886 {
  887         struct proc *p;
  888         struct pgrp *pg;
  889         struct sigio *sigio;
  890 
  891         sigio = SLIST_FIRST(sigiolst);
  892         if (sigio == NULL)
  893                 return;
  894         p = NULL;
  895         pg = NULL;
  896 
  897         /*
  898          * Every entry of the list should belong
  899          * to a single proc or pgrp.
  900          */
  901         if (sigio->sio_pgid < 0) {
  902                 pg = sigio->sio_pgrp;
  903                 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
  904         } else /* if (sigio->sio_pgid > 0) */ {
  905                 p = sigio->sio_proc;
  906                 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  907         }
  908 
  909         SIGIO_LOCK();
  910         while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
  911                 *(sigio->sio_myref) = NULL;
  912                 if (pg != NULL) {
  913                         KASSERT(sigio->sio_pgid < 0,
  914                             ("Proc sigio in pgrp sigio list"));
  915                         KASSERT(sigio->sio_pgrp == pg,
  916                             ("Bogus pgrp in sigio list"));
  917                         PGRP_LOCK(pg);
  918                         SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
  919                             sio_pgsigio);
  920                         PGRP_UNLOCK(pg);
  921                 } else /* if (p != NULL) */ {
  922                         KASSERT(sigio->sio_pgid > 0,
  923                             ("Pgrp sigio in proc sigio list"));
  924                         KASSERT(sigio->sio_proc == p,
  925                             ("Bogus proc in sigio list"));
  926                         PROC_LOCK(p);
  927                         SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
  928                             sio_pgsigio);
  929                         PROC_UNLOCK(p);
  930                 }
  931                 SIGIO_UNLOCK();
  932                 crfree(sigio->sio_ucred);
  933                 FREE(sigio, M_SIGIO);
  934                 SIGIO_LOCK();
  935         }
  936         SIGIO_UNLOCK();
  937 }
  938 
  939 /*
  940  * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
  941  *
  942  * After permission checking, add a sigio structure to the sigio list for
  943  * the process or process group.
  944  */
  945 int
  946 fsetown(pid_t pgid, struct sigio **sigiop)
  947 {
  948         struct proc *proc;
  949         struct pgrp *pgrp;
  950         struct sigio *sigio;
  951         int ret;
  952 
  953         if (pgid == 0) {
  954                 funsetown(sigiop);
  955                 return (0);
  956         }
  957 
  958         ret = 0;
  959 
  960         /* Allocate and fill in the new sigio out of locks. */
  961         MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
  962         sigio->sio_pgid = pgid;
  963         sigio->sio_ucred = crhold(curthread->td_ucred);
  964         sigio->sio_myref = sigiop;
  965 
  966         sx_slock(&proctree_lock);
  967         if (pgid > 0) {
  968                 proc = pfind(pgid);
  969                 if (proc == NULL) {
  970                         ret = ESRCH;
  971                         goto fail;
  972                 }
  973 
  974                 /*
  975                  * Policy - Don't allow a process to FSETOWN a process
  976                  * in another session.
  977                  *
  978                  * Remove this test to allow maximum flexibility or
  979                  * restrict FSETOWN to the current process or process
  980                  * group for maximum safety.
  981                  */
  982                 PROC_UNLOCK(proc);
  983                 if (proc->p_session != curthread->td_proc->p_session) {
  984                         ret = EPERM;
  985                         goto fail;
  986                 }
  987 
  988                 pgrp = NULL;
  989         } else /* if (pgid < 0) */ {
  990                 pgrp = pgfind(-pgid);
  991                 if (pgrp == NULL) {
  992                         ret = ESRCH;
  993                         goto fail;
  994                 }
  995                 PGRP_UNLOCK(pgrp);
  996 
  997                 /*
  998                  * Policy - Don't allow a process to FSETOWN a process
  999                  * in another session.
 1000                  *
 1001                  * Remove this test to allow maximum flexibility or
 1002                  * restrict FSETOWN to the current process or process
 1003                  * group for maximum safety.
 1004                  */
 1005                 if (pgrp->pg_session != curthread->td_proc->p_session) {
 1006                         ret = EPERM;
 1007                         goto fail;
 1008                 }
 1009 
 1010                 proc = NULL;
 1011         }
 1012         funsetown(sigiop);
 1013         if (pgid > 0) {
 1014                 PROC_LOCK(proc);
 1015                 /*
 1016                  * Since funsetownlst() is called without the proctree
 1017                  * locked, we need to check for P_WEXIT.
 1018                  * XXX: is ESRCH correct?
 1019                  */
 1020                 if ((proc->p_flag & P_WEXIT) != 0) {
 1021                         PROC_UNLOCK(proc);
 1022                         ret = ESRCH;
 1023                         goto fail;
 1024                 }
 1025                 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
 1026                 sigio->sio_proc = proc;
 1027                 PROC_UNLOCK(proc);
 1028         } else {
 1029                 PGRP_LOCK(pgrp);
 1030                 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
 1031                 sigio->sio_pgrp = pgrp;
 1032                 PGRP_UNLOCK(pgrp);
 1033         }
 1034         sx_sunlock(&proctree_lock);
 1035         SIGIO_LOCK();
 1036         *sigiop = sigio;
 1037         SIGIO_UNLOCK();
 1038         return (0);
 1039 
 1040 fail:
 1041         sx_sunlock(&proctree_lock);
 1042         crfree(sigio->sio_ucred);
 1043         FREE(sigio, M_SIGIO);
 1044         return (ret);
 1045 }
 1046 
 1047 /*
 1048  * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
 1049  */
 1050 pid_t
 1051 fgetown(sigiop)
 1052         struct sigio **sigiop;
 1053 {
 1054         pid_t pgid;
 1055 
 1056         SIGIO_LOCK();
 1057         pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
 1058         SIGIO_UNLOCK();
 1059         return (pgid);
 1060 }
 1061 
 1062 /*
 1063  * Close a file descriptor.
 1064  */
 1065 #ifndef _SYS_SYSPROTO_H_
 1066 struct close_args {
 1067         int     fd;
 1068 };
 1069 #endif
 1070 /* ARGSUSED */
 1071 int
 1072 close(td, uap)
 1073         struct thread *td;
 1074         struct close_args *uap;
 1075 {
 1076 
 1077         return (kern_close(td, uap->fd));
 1078 }
 1079 
 1080 int
 1081 kern_close(td, fd)
 1082         struct thread *td;
 1083         int fd;
 1084 {
 1085         struct filedesc *fdp;
 1086         struct file *fp;
 1087         int error;
 1088         int holdleaders;
 1089 
 1090         error = 0;
 1091         holdleaders = 0;
 1092         fdp = td->td_proc->p_fd;
 1093 
 1094         AUDIT_SYSCLOSE(td, fd);
 1095 
 1096         FILEDESC_XLOCK(fdp);
 1097         if ((unsigned)fd >= fdp->fd_nfiles ||
 1098             (fp = fdp->fd_ofiles[fd]) == NULL) {
 1099                 FILEDESC_XUNLOCK(fdp);
 1100                 return (EBADF);
 1101         }
 1102         fdp->fd_ofiles[fd] = NULL;
 1103         fdp->fd_ofileflags[fd] = 0;
 1104         fdunused(fdp, fd);
 1105         if (td->td_proc->p_fdtol != NULL) {
 1106                 /*
 1107                  * Ask fdfree() to sleep to ensure that all relevant
 1108                  * process leaders can be traversed in closef().
 1109                  */
 1110                 fdp->fd_holdleaderscount++;
 1111                 holdleaders = 1;
 1112         }
 1113 
 1114         /*
 1115          * We now hold the fp reference that used to be owned by the
 1116          * descriptor array.  We have to unlock the FILEDESC *AFTER*
 1117          * knote_fdclose to prevent a race of the fd getting opened, a knote
 1118          * added, and deleteing a knote for the new fd.
 1119          */
 1120         knote_fdclose(td, fd);
 1121         if (fp->f_type == DTYPE_MQUEUE)
 1122                 mq_fdclose(td, fd, fp);
 1123         FILEDESC_XUNLOCK(fdp);
 1124 
 1125         error = closef(fp, td);
 1126         if (holdleaders) {
 1127                 FILEDESC_XLOCK(fdp);
 1128                 fdp->fd_holdleaderscount--;
 1129                 if (fdp->fd_holdleaderscount == 0 &&
 1130                     fdp->fd_holdleaderswakeup != 0) {
 1131                         fdp->fd_holdleaderswakeup = 0;
 1132                         wakeup(&fdp->fd_holdleaderscount);
 1133                 }
 1134                 FILEDESC_XUNLOCK(fdp);
 1135         }
 1136         return (error);
 1137 }
 1138 
 1139 /*
 1140  * Close open file descriptors.
 1141  */
 1142 #ifndef _SYS_SYSPROTO_H_
 1143 struct closefrom_args {
 1144         int     lowfd;
 1145 };
 1146 #endif
 1147 /* ARGSUSED */
 1148 int
 1149 closefrom(struct thread *td, struct closefrom_args *uap)
 1150 {
 1151         struct filedesc *fdp;
 1152         int fd;
 1153 
 1154         fdp = td->td_proc->p_fd;
 1155         AUDIT_ARG(fd, uap->lowfd);
 1156 
 1157         /*
 1158          * Treat negative starting file descriptor values identical to
 1159          * closefrom(0) which closes all files.
 1160          */
 1161         if (uap->lowfd < 0)
 1162                 uap->lowfd = 0;
 1163         FILEDESC_SLOCK(fdp);
 1164         for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
 1165                 if (fdp->fd_ofiles[fd] != NULL) {
 1166                         FILEDESC_SUNLOCK(fdp);
 1167                         (void)kern_close(td, fd);
 1168                         FILEDESC_SLOCK(fdp);
 1169                 }
 1170         }
 1171         FILEDESC_SUNLOCK(fdp);
 1172         return (0);
 1173 }
 1174 
 1175 #if defined(COMPAT_43)
 1176 /*
 1177  * Return status information about a file descriptor.
 1178  */
 1179 #ifndef _SYS_SYSPROTO_H_
 1180 struct ofstat_args {
 1181         int     fd;
 1182         struct  ostat *sb;
 1183 };
 1184 #endif
 1185 /* ARGSUSED */
 1186 int
 1187 ofstat(struct thread *td, struct ofstat_args *uap)
 1188 {
 1189         struct ostat oub;
 1190         struct stat ub;
 1191         int error;
 1192 
 1193         error = kern_fstat(td, uap->fd, &ub);
 1194         if (error == 0) {
 1195                 cvtstat(&ub, &oub);
 1196                 error = copyout(&oub, uap->sb, sizeof(oub));
 1197         }
 1198         return (error);
 1199 }
 1200 #endif /* COMPAT_43 */
 1201 
 1202 /*
 1203  * Return status information about a file descriptor.
 1204  */
 1205 #ifndef _SYS_SYSPROTO_H_
 1206 struct fstat_args {
 1207         int     fd;
 1208         struct  stat *sb;
 1209 };
 1210 #endif
 1211 /* ARGSUSED */
 1212 int
 1213 fstat(struct thread *td, struct fstat_args *uap)
 1214 {
 1215         struct stat ub;
 1216         int error;
 1217 
 1218         error = kern_fstat(td, uap->fd, &ub);
 1219         if (error == 0)
 1220                 error = copyout(&ub, uap->sb, sizeof(ub));
 1221         return (error);
 1222 }
 1223 
 1224 int
 1225 kern_fstat(struct thread *td, int fd, struct stat *sbp)
 1226 {
 1227         struct file *fp;
 1228         int error;
 1229 
 1230         AUDIT_ARG(fd, fd);
 1231 
 1232         if ((error = fget(td, fd, &fp)) != 0)
 1233                 return (error);
 1234 
 1235         AUDIT_ARG(file, td->td_proc, fp);
 1236 
 1237         error = fo_stat(fp, sbp, td->td_ucred, td);
 1238         fdrop(fp, td);
 1239 #ifdef KTRACE
 1240         if (error == 0 && KTRPOINT(td, KTR_STRUCT))
 1241                 ktrstat(sbp);
 1242 #endif
 1243         return (error);
 1244 }
 1245 
 1246 /*
 1247  * Return status information about a file descriptor.
 1248  */
 1249 #ifndef _SYS_SYSPROTO_H_
 1250 struct nfstat_args {
 1251         int     fd;
 1252         struct  nstat *sb;
 1253 };
 1254 #endif
 1255 /* ARGSUSED */
 1256 int
 1257 nfstat(struct thread *td, struct nfstat_args *uap)
 1258 {
 1259         struct nstat nub;
 1260         struct stat ub;
 1261         int error;
 1262 
 1263         error = kern_fstat(td, uap->fd, &ub);
 1264         if (error == 0) {
 1265                 cvtnstat(&ub, &nub);
 1266                 error = copyout(&nub, uap->sb, sizeof(nub));
 1267         }
 1268         return (error);
 1269 }
 1270 
 1271 /*
 1272  * Return pathconf information about a file descriptor.
 1273  */
 1274 #ifndef _SYS_SYSPROTO_H_
 1275 struct fpathconf_args {
 1276         int     fd;
 1277         int     name;
 1278 };
 1279 #endif
 1280 /* ARGSUSED */
 1281 int
 1282 fpathconf(struct thread *td, struct fpathconf_args *uap)
 1283 {
 1284         struct file *fp;
 1285         struct vnode *vp;
 1286         int error;
 1287 
 1288         if ((error = fget(td, uap->fd, &fp)) != 0)
 1289                 return (error);
 1290 
 1291         /* If asynchronous I/O is available, it works for all descriptors. */
 1292         if (uap->name == _PC_ASYNC_IO) {
 1293                 td->td_retval[0] = async_io_version;
 1294                 goto out;
 1295         }
 1296         vp = fp->f_vnode;
 1297         if (vp != NULL) {
 1298                 int vfslocked;
 1299                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1300                 vn_lock(vp, LK_SHARED | LK_RETRY, td);
 1301                 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
 1302                 VOP_UNLOCK(vp, 0, td);
 1303                 VFS_UNLOCK_GIANT(vfslocked);
 1304         } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
 1305                 if (uap->name != _PC_PIPE_BUF) {
 1306                         error = EINVAL;
 1307                 } else {
 1308                         td->td_retval[0] = PIPE_BUF;
 1309                 error = 0;
 1310                 }
 1311         } else {
 1312                 error = EOPNOTSUPP;
 1313         }
 1314 out:
 1315         fdrop(fp, td);
 1316         return (error);
 1317 }
 1318 
 1319 /*
 1320  * Grow the file table to accomodate (at least) nfd descriptors.  This may
 1321  * block and drop the filedesc lock, but it will reacquire it before
 1322  * returning.
 1323  */
 1324 static void
 1325 fdgrowtable(struct filedesc *fdp, int nfd)
 1326 {
 1327         struct file **ntable;
 1328         char *nfileflags;
 1329         int nnfiles, onfiles;
 1330         NDSLOTTYPE *nmap;
 1331 
 1332         FILEDESC_XLOCK_ASSERT(fdp);
 1333 
 1334         KASSERT(fdp->fd_nfiles > 0,
 1335             ("zero-length file table"));
 1336 
 1337         /* compute the size of the new table */
 1338         onfiles = fdp->fd_nfiles;
 1339         nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
 1340         if (nnfiles <= onfiles)
 1341                 /* the table is already large enough */
 1342                 return;
 1343 
 1344         /* allocate a new table and (if required) new bitmaps */
 1345         FILEDESC_XUNLOCK(fdp);
 1346         MALLOC(ntable, struct file **, nnfiles * OFILESIZE,
 1347             M_FILEDESC, M_ZERO | M_WAITOK);
 1348         nfileflags = (char *)&ntable[nnfiles];
 1349         if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
 1350                 MALLOC(nmap, NDSLOTTYPE *, NDSLOTS(nnfiles) * NDSLOTSIZE,
 1351                     M_FILEDESC, M_ZERO | M_WAITOK);
 1352         else
 1353                 nmap = NULL;
 1354         FILEDESC_XLOCK(fdp);
 1355 
 1356         /*
 1357          * We now have new tables ready to go.  Since we dropped the
 1358          * filedesc lock to call malloc(), watch out for a race.
 1359          */
 1360         onfiles = fdp->fd_nfiles;
 1361         if (onfiles >= nnfiles) {
 1362                 /* we lost the race, but that's OK */
 1363                 free(ntable, M_FILEDESC);
 1364                 if (nmap != NULL)
 1365                         free(nmap, M_FILEDESC);
 1366                 return;
 1367         }
 1368         bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
 1369         bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
 1370         if (onfiles > NDFILE)
 1371                 free(fdp->fd_ofiles, M_FILEDESC);
 1372         fdp->fd_ofiles = ntable;
 1373         fdp->fd_ofileflags = nfileflags;
 1374         if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
 1375                 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
 1376                 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
 1377                         free(fdp->fd_map, M_FILEDESC);
 1378                 fdp->fd_map = nmap;
 1379         }
 1380         fdp->fd_nfiles = nnfiles;
 1381 }
 1382 
 1383 /*
 1384  * Allocate a file descriptor for the process.
 1385  */
 1386 int
 1387 fdalloc(struct thread *td, int minfd, int *result)
 1388 {
 1389         struct proc *p = td->td_proc;
 1390         struct filedesc *fdp = p->p_fd;
 1391         int fd = -1, maxfd;
 1392 
 1393         FILEDESC_XLOCK_ASSERT(fdp);
 1394 
 1395         if (fdp->fd_freefile > minfd)
 1396                 minfd = fdp->fd_freefile;          
 1397 
 1398         PROC_LOCK(p);
 1399         maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
 1400         PROC_UNLOCK(p);
 1401 
 1402         /*
 1403          * Search the bitmap for a free descriptor.  If none is found, try
 1404          * to grow the file table.  Keep at it until we either get a file
 1405          * descriptor or run into process or system limits; fdgrowtable()
 1406          * may drop the filedesc lock, so we're in a race.
 1407          */
 1408         for (;;) {
 1409                 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
 1410                 if (fd >= maxfd)
 1411                         return (EMFILE);
 1412                 if (fd < fdp->fd_nfiles)
 1413                         break;
 1414                 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
 1415         }
 1416 
 1417         /*
 1418          * Perform some sanity checks, then mark the file descriptor as
 1419          * used and return it to the caller.
 1420          */
 1421         KASSERT(!fdisused(fdp, fd),
 1422             ("fd_first_free() returned non-free descriptor"));
 1423         KASSERT(fdp->fd_ofiles[fd] == NULL,
 1424             ("free descriptor isn't"));
 1425         fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
 1426         fdused(fdp, fd);
 1427         *result = fd;
 1428         return (0);
 1429 }
 1430 
 1431 /*
 1432  * Check to see whether n user file descriptors are available to the process
 1433  * p.
 1434  */
 1435 int
 1436 fdavail(struct thread *td, int n)
 1437 {
 1438         struct proc *p = td->td_proc;
 1439         struct filedesc *fdp = td->td_proc->p_fd;
 1440         struct file **fpp;
 1441         int i, lim, last;
 1442 
 1443         FILEDESC_LOCK_ASSERT(fdp);
 1444 
 1445         PROC_LOCK(p);
 1446         lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
 1447         PROC_UNLOCK(p);
 1448         if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
 1449                 return (1);
 1450         last = min(fdp->fd_nfiles, lim);
 1451         fpp = &fdp->fd_ofiles[fdp->fd_freefile];
 1452         for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
 1453                 if (*fpp == NULL && --n <= 0)
 1454                         return (1);
 1455         }
 1456         return (0);
 1457 }
 1458 
 1459 /*
 1460  * Create a new open file structure and allocate a file decriptor for the
 1461  * process that refers to it.  We add one reference to the file for the
 1462  * descriptor table and one reference for resultfp. This is to prevent us
 1463  * being preempted and the entry in the descriptor table closed after we
 1464  * release the FILEDESC lock.
 1465  */
 1466 int
 1467 falloc(struct thread *td, struct file **resultfp, int *resultfd)
 1468 {
 1469         struct proc *p = td->td_proc;
 1470         struct file *fp, *fq;
 1471         int error, i;
 1472         int maxuserfiles = maxfiles - (maxfiles / 20);
 1473         static struct timeval lastfail;
 1474         static int curfail;
 1475 
 1476         fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
 1477         sx_xlock(&filelist_lock);
 1478 
 1479         if ((openfiles >= maxuserfiles &&
 1480             priv_check(td, PRIV_MAXFILES) != 0) ||
 1481             openfiles >= maxfiles) {
 1482                 if (ppsratecheck(&lastfail, &curfail, 1)) {
 1483                         printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
 1484                                 td->td_ucred->cr_ruid);
 1485                 }
 1486                 sx_xunlock(&filelist_lock);
 1487                 uma_zfree(file_zone, fp);
 1488                 return (ENFILE);
 1489         }
 1490         openfiles++;
 1491 
 1492         /*
 1493          * If the process has file descriptor zero open, add the new file
 1494          * descriptor to the list of open files at that point, otherwise
 1495          * put it at the front of the list of open files.
 1496          */
 1497         fp->f_mtxp = mtx_pool_alloc(mtxpool_sleep);
 1498         fp->f_count = 1;
 1499         if (resultfp)
 1500                 fp->f_count++;
 1501         fp->f_cred = crhold(td->td_ucred);
 1502         fp->f_ops = &badfileops;
 1503         fp->f_data = NULL;
 1504         fp->f_vnode = NULL;
 1505         FILEDESC_XLOCK(p->p_fd);
 1506         if ((fq = p->p_fd->fd_ofiles[0])) {
 1507                 LIST_INSERT_AFTER(fq, fp, f_list);
 1508         } else {
 1509                 LIST_INSERT_HEAD(&filehead, fp, f_list);
 1510         }
 1511         sx_xunlock(&filelist_lock);
 1512         if ((error = fdalloc(td, 0, &i))) {
 1513                 FILEDESC_XUNLOCK(p->p_fd);
 1514                 fdrop(fp, td);
 1515                 if (resultfp)
 1516                         fdrop(fp, td);
 1517                 return (error);
 1518         }
 1519         p->p_fd->fd_ofiles[i] = fp;
 1520         FILEDESC_XUNLOCK(p->p_fd);
 1521         if (resultfp)
 1522                 *resultfp = fp;
 1523         if (resultfd)
 1524                 *resultfd = i;
 1525         return (0);
 1526 }
 1527 
 1528 /*
 1529  * Build a new filedesc structure from another.
 1530  * Copy the current, root, and jail root vnode references.
 1531  */
 1532 struct filedesc *
 1533 fdinit(struct filedesc *fdp)
 1534 {
 1535         struct filedesc0 *newfdp;
 1536 
 1537         newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
 1538         FILEDESC_LOCK_INIT(&newfdp->fd_fd);
 1539         if (fdp != NULL) {
 1540                 FILEDESC_XLOCK(fdp);
 1541                 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
 1542                 if (newfdp->fd_fd.fd_cdir)
 1543                         VREF(newfdp->fd_fd.fd_cdir);
 1544                 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
 1545                 if (newfdp->fd_fd.fd_rdir)
 1546                         VREF(newfdp->fd_fd.fd_rdir);
 1547                 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
 1548                 if (newfdp->fd_fd.fd_jdir)
 1549                         VREF(newfdp->fd_fd.fd_jdir);
 1550                 FILEDESC_XUNLOCK(fdp);
 1551         }
 1552 
 1553         /* Create the file descriptor table. */
 1554         newfdp->fd_fd.fd_refcnt = 1;
 1555         newfdp->fd_fd.fd_holdcnt = 1;
 1556         newfdp->fd_fd.fd_cmask = CMASK;
 1557         newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
 1558         newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
 1559         newfdp->fd_fd.fd_nfiles = NDFILE;
 1560         newfdp->fd_fd.fd_map = newfdp->fd_dmap;
 1561         newfdp->fd_fd.fd_lastfile = -1;
 1562         return (&newfdp->fd_fd);
 1563 }
 1564 
 1565 static struct filedesc *
 1566 fdhold(struct proc *p)
 1567 {
 1568         struct filedesc *fdp;
 1569 
 1570         mtx_lock(&fdesc_mtx);
 1571         fdp = p->p_fd;
 1572         if (fdp != NULL)
 1573                 fdp->fd_holdcnt++;
 1574         mtx_unlock(&fdesc_mtx);
 1575         return (fdp);
 1576 }
 1577 
 1578 static void
 1579 fddrop(struct filedesc *fdp)
 1580 {
 1581         int i;
 1582 
 1583         mtx_lock(&fdesc_mtx);
 1584         i = --fdp->fd_holdcnt;
 1585         mtx_unlock(&fdesc_mtx);
 1586         if (i > 0)
 1587                 return;
 1588 
 1589         FILEDESC_LOCK_DESTROY(fdp);
 1590         FREE(fdp, M_FILEDESC);
 1591 }
 1592 
 1593 /*
 1594  * Share a filedesc structure.
 1595  */
 1596 struct filedesc *
 1597 fdshare(struct filedesc *fdp)
 1598 {
 1599 
 1600         FILEDESC_XLOCK(fdp);
 1601         fdp->fd_refcnt++;
 1602         FILEDESC_XUNLOCK(fdp);
 1603         return (fdp);
 1604 }
 1605 
 1606 /*
 1607  * Unshare a filedesc structure, if necessary by making a copy
 1608  */
 1609 void
 1610 fdunshare(struct proc *p, struct thread *td)
 1611 {
 1612 
 1613         FILEDESC_XLOCK(p->p_fd);
 1614         if (p->p_fd->fd_refcnt > 1) {
 1615                 struct filedesc *tmp;
 1616 
 1617                 FILEDESC_XUNLOCK(p->p_fd);
 1618                 tmp = fdcopy(p->p_fd);
 1619                 fdfree(td);
 1620                 p->p_fd = tmp;
 1621         } else
 1622                 FILEDESC_XUNLOCK(p->p_fd);
 1623 }
 1624 
 1625 /*
 1626  * Copy a filedesc structure.  A NULL pointer in returns a NULL reference,
 1627  * this is to ease callers, not catch errors.
 1628  */
 1629 struct filedesc *
 1630 fdcopy(struct filedesc *fdp)
 1631 {
 1632         struct filedesc *newfdp;
 1633         int i;
 1634 
 1635         /* Certain daemons might not have file descriptors. */
 1636         if (fdp == NULL)
 1637                 return (NULL);
 1638 
 1639         newfdp = fdinit(fdp);
 1640         FILEDESC_SLOCK(fdp);
 1641         while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
 1642                 FILEDESC_SUNLOCK(fdp);
 1643                 FILEDESC_XLOCK(newfdp);
 1644                 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
 1645                 FILEDESC_XUNLOCK(newfdp);
 1646                 FILEDESC_SLOCK(fdp);
 1647         }
 1648         /* copy everything except kqueue descriptors */
 1649         newfdp->fd_freefile = -1;
 1650         for (i = 0; i <= fdp->fd_lastfile; ++i) {
 1651                 if (fdisused(fdp, i) &&
 1652                     fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE &&
 1653                     fdp->fd_ofiles[i]->f_ops != &badfileops) {
 1654                         newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
 1655                         newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
 1656                         fhold(newfdp->fd_ofiles[i]);
 1657                         newfdp->fd_lastfile = i;
 1658                 } else {
 1659                         if (newfdp->fd_freefile == -1)
 1660                                 newfdp->fd_freefile = i;
 1661                 }
 1662         }
 1663         FILEDESC_SUNLOCK(fdp);
 1664         FILEDESC_XLOCK(newfdp);
 1665         for (i = 0; i <= newfdp->fd_lastfile; ++i)
 1666                 if (newfdp->fd_ofiles[i] != NULL)
 1667                         fdused(newfdp, i);
 1668         FILEDESC_XUNLOCK(newfdp);
 1669         FILEDESC_SLOCK(fdp);
 1670         if (newfdp->fd_freefile == -1)
 1671                 newfdp->fd_freefile = i;
 1672         newfdp->fd_cmask = fdp->fd_cmask;
 1673         FILEDESC_SUNLOCK(fdp);
 1674         return (newfdp);
 1675 }
 1676 
 1677 /*
 1678  * Release a filedesc structure.
 1679  */
 1680 void
 1681 fdfree(struct thread *td)
 1682 {
 1683         struct filedesc *fdp;
 1684         struct file **fpp;
 1685         int i, locked;
 1686         struct filedesc_to_leader *fdtol;
 1687         struct file *fp;
 1688         struct vnode *cdir, *jdir, *rdir, *vp;
 1689         struct flock lf;
 1690 
 1691         /* Certain daemons might not have file descriptors. */
 1692         fdp = td->td_proc->p_fd;
 1693         if (fdp == NULL)
 1694                 return;
 1695 
 1696         /* Check for special need to clear POSIX style locks */
 1697         fdtol = td->td_proc->p_fdtol;
 1698         if (fdtol != NULL) {
 1699                 FILEDESC_XLOCK(fdp);
 1700                 KASSERT(fdtol->fdl_refcount > 0,
 1701                         ("filedesc_to_refcount botch: fdl_refcount=%d",
 1702                          fdtol->fdl_refcount));
 1703                 if (fdtol->fdl_refcount == 1 &&
 1704                     (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 1705                         for (i = 0, fpp = fdp->fd_ofiles;
 1706                              i <= fdp->fd_lastfile;
 1707                              i++, fpp++) {
 1708                                 if (*fpp == NULL ||
 1709                                     (*fpp)->f_type != DTYPE_VNODE)
 1710                                         continue;
 1711                                 fp = *fpp;
 1712                                 fhold(fp);
 1713                                 FILEDESC_XUNLOCK(fdp);
 1714                                 lf.l_whence = SEEK_SET;
 1715                                 lf.l_start = 0;
 1716                                 lf.l_len = 0;
 1717                                 lf.l_type = F_UNLCK;
 1718                                 vp = fp->f_vnode;
 1719                                 locked = VFS_LOCK_GIANT(vp->v_mount);
 1720                                 (void) VOP_ADVLOCK(vp,
 1721                                                    (caddr_t)td->td_proc->
 1722                                                    p_leader,
 1723                                                    F_UNLCK,
 1724                                                    &lf,
 1725                                                    F_POSIX);
 1726                                 VFS_UNLOCK_GIANT(locked);
 1727                                 FILEDESC_XLOCK(fdp);
 1728                                 fdrop(fp, td);
 1729                                 fpp = fdp->fd_ofiles + i;
 1730                         }
 1731                 }
 1732         retry:
 1733                 if (fdtol->fdl_refcount == 1) {
 1734                         if (fdp->fd_holdleaderscount > 0 &&
 1735                             (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 1736                                 /*
 1737                                  * close() or do_dup() has cleared a reference
 1738                                  * in a shared file descriptor table.
 1739                                  */
 1740                                 fdp->fd_holdleaderswakeup = 1;
 1741                                 sx_sleep(&fdp->fd_holdleaderscount,
 1742                                     FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
 1743                                 goto retry;
 1744                         }
 1745                         if (fdtol->fdl_holdcount > 0) {
 1746                                 /*
 1747                                  * Ensure that fdtol->fdl_leader remains
 1748                                  * valid in closef().
 1749                                  */
 1750                                 fdtol->fdl_wakeup = 1;
 1751                                 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
 1752                                     "fdlhold", 0);
 1753                                 goto retry;
 1754                         }
 1755                 }
 1756                 fdtol->fdl_refcount--;
 1757                 if (fdtol->fdl_refcount == 0 &&
 1758                     fdtol->fdl_holdcount == 0) {
 1759                         fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
 1760                         fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
 1761                 } else
 1762                         fdtol = NULL;
 1763                 td->td_proc->p_fdtol = NULL;
 1764                 FILEDESC_XUNLOCK(fdp);
 1765                 if (fdtol != NULL)
 1766                         FREE(fdtol, M_FILEDESC_TO_LEADER);
 1767         }
 1768         FILEDESC_XLOCK(fdp);
 1769         i = --fdp->fd_refcnt;
 1770         FILEDESC_XUNLOCK(fdp);
 1771         if (i > 0)
 1772                 return;
 1773 
 1774         fpp = fdp->fd_ofiles;
 1775         for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
 1776                 if (*fpp) {
 1777                         FILEDESC_XLOCK(fdp);
 1778                         fp = *fpp;
 1779                         *fpp = NULL;
 1780                         FILEDESC_XUNLOCK(fdp);
 1781                         (void) closef(fp, td);
 1782                 }
 1783         }
 1784         FILEDESC_XLOCK(fdp);
 1785 
 1786         /* XXX This should happen earlier. */
 1787         mtx_lock(&fdesc_mtx);
 1788         td->td_proc->p_fd = NULL;
 1789         mtx_unlock(&fdesc_mtx);
 1790 
 1791         if (fdp->fd_nfiles > NDFILE)
 1792                 FREE(fdp->fd_ofiles, M_FILEDESC);
 1793         if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
 1794                 FREE(fdp->fd_map, M_FILEDESC);
 1795 
 1796         fdp->fd_nfiles = 0;
 1797 
 1798         cdir = fdp->fd_cdir;
 1799         fdp->fd_cdir = NULL;
 1800         rdir = fdp->fd_rdir;
 1801         fdp->fd_rdir = NULL;
 1802         jdir = fdp->fd_jdir;
 1803         fdp->fd_jdir = NULL;
 1804         FILEDESC_XUNLOCK(fdp);
 1805 
 1806         if (cdir) {
 1807                 locked = VFS_LOCK_GIANT(cdir->v_mount);
 1808                 vrele(cdir);
 1809                 VFS_UNLOCK_GIANT(locked);
 1810         }
 1811         if (rdir) {
 1812                 locked = VFS_LOCK_GIANT(rdir->v_mount);
 1813                 vrele(rdir);
 1814                 VFS_UNLOCK_GIANT(locked);
 1815         }
 1816         if (jdir) {
 1817                 locked = VFS_LOCK_GIANT(jdir->v_mount);
 1818                 vrele(jdir);
 1819                 VFS_UNLOCK_GIANT(locked);
 1820         }
 1821 
 1822         fddrop(fdp);
 1823 }
 1824 
 1825 /*
 1826  * For setugid programs, we don't want to people to use that setugidness
 1827  * to generate error messages which write to a file which otherwise would
 1828  * otherwise be off-limits to the process.  We check for filesystems where
 1829  * the vnode can change out from under us after execve (like [lin]procfs).
 1830  *
 1831  * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
 1832  * sufficient.  We also don't check for setugidness since we know we are.
 1833  */
 1834 static int
 1835 is_unsafe(struct file *fp)
 1836 {
 1837         if (fp->f_type == DTYPE_VNODE) {
 1838                 struct vnode *vp = fp->f_vnode;
 1839 
 1840                 if ((vp->v_vflag & VV_PROCDEP) != 0)
 1841                         return (1);
 1842         }
 1843         return (0);
 1844 }
 1845 
 1846 /*
 1847  * Make this setguid thing safe, if at all possible.
 1848  */
 1849 void
 1850 setugidsafety(struct thread *td)
 1851 {
 1852         struct filedesc *fdp;
 1853         int i;
 1854 
 1855         /* Certain daemons might not have file descriptors. */
 1856         fdp = td->td_proc->p_fd;
 1857         if (fdp == NULL)
 1858                 return;
 1859 
 1860         /*
 1861          * Note: fdp->fd_ofiles may be reallocated out from under us while
 1862          * we are blocked in a close.  Be careful!
 1863          */
 1864         FILEDESC_XLOCK(fdp);
 1865         for (i = 0; i <= fdp->fd_lastfile; i++) {
 1866                 if (i > 2)
 1867                         break;
 1868                 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
 1869                         struct file *fp;
 1870 
 1871                         knote_fdclose(td, i);
 1872                         /*
 1873                          * NULL-out descriptor prior to close to avoid
 1874                          * a race while close blocks.
 1875                          */
 1876                         fp = fdp->fd_ofiles[i];
 1877                         fdp->fd_ofiles[i] = NULL;
 1878                         fdp->fd_ofileflags[i] = 0;
 1879                         fdunused(fdp, i);
 1880                         FILEDESC_XUNLOCK(fdp);
 1881                         (void) closef(fp, td);
 1882                         FILEDESC_XLOCK(fdp);
 1883                 }
 1884         }
 1885         FILEDESC_XUNLOCK(fdp);
 1886 }
 1887 
 1888 /*
 1889  * If a specific file object occupies a specific file descriptor, close the
 1890  * file descriptor entry and drop a reference on the file object.  This is a
 1891  * convenience function to handle a subsequent error in a function that calls
 1892  * falloc() that handles the race that another thread might have closed the
 1893  * file descriptor out from under the thread creating the file object.
 1894  */
 1895 void
 1896 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
 1897 {
 1898 
 1899         FILEDESC_XLOCK(fdp);
 1900         if (fdp->fd_ofiles[idx] == fp) {
 1901                 fdp->fd_ofiles[idx] = NULL;
 1902                 fdunused(fdp, idx);
 1903                 FILEDESC_XUNLOCK(fdp);
 1904                 fdrop(fp, td);
 1905         } else
 1906                 FILEDESC_XUNLOCK(fdp);
 1907 }
 1908 
 1909 /*
 1910  * Close any files on exec?
 1911  */
 1912 void
 1913 fdcloseexec(struct thread *td)
 1914 {
 1915         struct filedesc *fdp;
 1916         int i;
 1917 
 1918         /* Certain daemons might not have file descriptors. */
 1919         fdp = td->td_proc->p_fd;
 1920         if (fdp == NULL)
 1921                 return;
 1922 
 1923         FILEDESC_XLOCK(fdp);
 1924 
 1925         /*
 1926          * We cannot cache fd_ofiles or fd_ofileflags since operations
 1927          * may block and rip them out from under us.
 1928          */
 1929         for (i = 0; i <= fdp->fd_lastfile; i++) {
 1930                 if (fdp->fd_ofiles[i] != NULL &&
 1931                     (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
 1932                     (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
 1933                         struct file *fp;
 1934 
 1935                         knote_fdclose(td, i);
 1936                         /*
 1937                          * NULL-out descriptor prior to close to avoid
 1938                          * a race while close blocks.
 1939                          */
 1940                         fp = fdp->fd_ofiles[i];
 1941                         fdp->fd_ofiles[i] = NULL;
 1942                         fdp->fd_ofileflags[i] = 0;
 1943                         fdunused(fdp, i);
 1944                         if (fp->f_type == DTYPE_MQUEUE)
 1945                                 mq_fdclose(td, i, fp);
 1946                         FILEDESC_XUNLOCK(fdp);
 1947                         (void) closef(fp, td);
 1948                         FILEDESC_XLOCK(fdp);
 1949                 }
 1950         }
 1951         FILEDESC_XUNLOCK(fdp);
 1952 }
 1953 
 1954 /*
 1955  * It is unsafe for set[ug]id processes to be started with file
 1956  * descriptors 0..2 closed, as these descriptors are given implicit
 1957  * significance in the Standard C library.  fdcheckstd() will create a
 1958  * descriptor referencing /dev/null for each of stdin, stdout, and
 1959  * stderr that is not already open.
 1960  */
 1961 int
 1962 fdcheckstd(struct thread *td)
 1963 {
 1964         struct filedesc *fdp;
 1965         register_t retval, save;
 1966         int i, error, devnull;
 1967 
 1968         fdp = td->td_proc->p_fd;
 1969         if (fdp == NULL)
 1970                 return (0);
 1971         KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
 1972         devnull = -1;
 1973         error = 0;
 1974         for (i = 0; i < 3; i++) {
 1975                 if (fdp->fd_ofiles[i] != NULL)
 1976                         continue;
 1977                 if (devnull < 0) {
 1978                         save = td->td_retval[0];
 1979                         error = kern_open(td, "/dev/null", UIO_SYSSPACE,
 1980                             O_RDWR, 0);
 1981                         devnull = td->td_retval[0];
 1982                         KASSERT(devnull == i, ("oof, we didn't get our fd"));
 1983                         td->td_retval[0] = save;
 1984                         if (error)
 1985                                 break;
 1986                 } else {
 1987                         error = do_dup(td, DUP_FIXED, devnull, i, &retval);
 1988                         if (error != 0)
 1989                                 break;
 1990                 }
 1991         }
 1992         return (error);
 1993 }
 1994 
 1995 /*
 1996  * Internal form of close.  Decrement reference count on file structure.
 1997  * Note: td may be NULL when closing a file that was being passed in a
 1998  * message.
 1999  *
 2000  * XXXRW: Giant is not required for the caller, but often will be held; this
 2001  * makes it moderately likely the Giant will be recursed in the VFS case.
 2002  */
 2003 int
 2004 closef(struct file *fp, struct thread *td)
 2005 {
 2006         struct vnode *vp;
 2007         struct flock lf;
 2008         struct filedesc_to_leader *fdtol;
 2009         struct filedesc *fdp;
 2010 
 2011         /*
 2012          * POSIX record locking dictates that any close releases ALL
 2013          * locks owned by this process.  This is handled by setting
 2014          * a flag in the unlock to free ONLY locks obeying POSIX
 2015          * semantics, and not to free BSD-style file locks.
 2016          * If the descriptor was in a message, POSIX-style locks
 2017          * aren't passed with the descriptor, and the thread pointer
 2018          * will be NULL.  Callers should be careful only to pass a
 2019          * NULL thread pointer when there really is no owning
 2020          * context that might have locks, or the locks will be
 2021          * leaked.
 2022          */
 2023         if (fp->f_type == DTYPE_VNODE && td != NULL) {
 2024                 int vfslocked;
 2025 
 2026                 vp = fp->f_vnode;
 2027                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2028                 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 2029                         lf.l_whence = SEEK_SET;
 2030                         lf.l_start = 0;
 2031                         lf.l_len = 0;
 2032                         lf.l_type = F_UNLCK;
 2033                         (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
 2034                                            F_UNLCK, &lf, F_POSIX);
 2035                 }
 2036                 fdtol = td->td_proc->p_fdtol;
 2037                 if (fdtol != NULL) {
 2038                         /*
 2039                          * Handle special case where file descriptor table is
 2040                          * shared between multiple process leaders.
 2041                          */
 2042                         fdp = td->td_proc->p_fd;
 2043                         FILEDESC_XLOCK(fdp);
 2044                         for (fdtol = fdtol->fdl_next;
 2045                              fdtol != td->td_proc->p_fdtol;
 2046                              fdtol = fdtol->fdl_next) {
 2047                                 if ((fdtol->fdl_leader->p_flag &
 2048                                      P_ADVLOCK) == 0)
 2049                                         continue;
 2050                                 fdtol->fdl_holdcount++;
 2051                                 FILEDESC_XUNLOCK(fdp);
 2052                                 lf.l_whence = SEEK_SET;
 2053                                 lf.l_start = 0;
 2054                                 lf.l_len = 0;
 2055                                 lf.l_type = F_UNLCK;
 2056                                 vp = fp->f_vnode;
 2057                                 (void) VOP_ADVLOCK(vp,
 2058                                                    (caddr_t)fdtol->fdl_leader,
 2059                                                    F_UNLCK, &lf, F_POSIX);
 2060                                 FILEDESC_XLOCK(fdp);
 2061                                 fdtol->fdl_holdcount--;
 2062                                 if (fdtol->fdl_holdcount == 0 &&
 2063                                     fdtol->fdl_wakeup != 0) {
 2064                                         fdtol->fdl_wakeup = 0;
 2065                                         wakeup(fdtol);
 2066                                 }
 2067                         }
 2068                         FILEDESC_XUNLOCK(fdp);
 2069                 }
 2070                 VFS_UNLOCK_GIANT(vfslocked);
 2071         }
 2072         return (fdrop(fp, td));
 2073 }
 2074 
 2075 /*
 2076  * Extract the file pointer associated with the specified descriptor for the
 2077  * current user process.
 2078  *
 2079  * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
 2080  * returned.
 2081  *
 2082  * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
 2083  * It should be dropped with fdrop().  If it is not set, then the refcount
 2084  * will not be bumped however the thread's filedesc struct will be returned
 2085  * locked (for fgetsock).
 2086  *
 2087  * If an error occured the non-zero error is returned and *fpp is set to
 2088  * NULL.  Otherwise *fpp is set and zero is returned.
 2089  */
 2090 static __inline int
 2091 _fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
 2092 {
 2093         struct filedesc *fdp;
 2094         struct file *fp;
 2095 
 2096         *fpp = NULL;
 2097         if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
 2098                 return (EBADF);
 2099         FILEDESC_SLOCK(fdp);
 2100         if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
 2101                 FILEDESC_SUNLOCK(fdp);
 2102                 return (EBADF);
 2103         }
 2104 
 2105         /*
 2106          * FREAD and FWRITE failure return EBADF as per POSIX.
 2107          *
 2108          * Only one flag, or 0, may be specified.
 2109          */
 2110         if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
 2111                 FILEDESC_SUNLOCK(fdp);
 2112                 return (EBADF);
 2113         }
 2114         if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
 2115                 FILEDESC_SUNLOCK(fdp);
 2116                 return (EBADF);
 2117         }
 2118         if (hold) {
 2119                 fhold(fp);
 2120                 FILEDESC_SUNLOCK(fdp);
 2121         }
 2122         *fpp = fp;
 2123         return (0);
 2124 }
 2125 
 2126 int
 2127 fget(struct thread *td, int fd, struct file **fpp)
 2128 {
 2129 
 2130         return(_fget(td, fd, fpp, 0, 1));
 2131 }
 2132 
 2133 int
 2134 fget_read(struct thread *td, int fd, struct file **fpp)
 2135 {
 2136 
 2137         return(_fget(td, fd, fpp, FREAD, 1));
 2138 }
 2139 
 2140 int
 2141 fget_write(struct thread *td, int fd, struct file **fpp)
 2142 {
 2143 
 2144         return(_fget(td, fd, fpp, FWRITE, 1));
 2145 }
 2146 
 2147 /*
 2148  * Like fget() but loads the underlying vnode, or returns an error if the
 2149  * descriptor does not represent a vnode.  Note that pipes use vnodes but
 2150  * never have VM objects.  The returned vnode will be vref()'d.
 2151  *
 2152  * XXX: what about the unused flags ?
 2153  */
 2154 static __inline int
 2155 _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
 2156 {
 2157         struct file *fp;
 2158         int error;
 2159 
 2160         *vpp = NULL;
 2161         if ((error = _fget(td, fd, &fp, flags, 0)) != 0)
 2162                 return (error);
 2163         if (fp->f_vnode == NULL) {
 2164                 error = EINVAL;
 2165         } else {
 2166                 *vpp = fp->f_vnode;
 2167                 vref(*vpp);
 2168         }
 2169         FILEDESC_SUNLOCK(td->td_proc->p_fd);
 2170         return (error);
 2171 }
 2172 
 2173 int
 2174 fgetvp(struct thread *td, int fd, struct vnode **vpp)
 2175 {
 2176 
 2177         return (_fgetvp(td, fd, vpp, 0));
 2178 }
 2179 
 2180 int
 2181 fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
 2182 {
 2183 
 2184         return (_fgetvp(td, fd, vpp, FREAD));
 2185 }
 2186 
 2187 #ifdef notyet
 2188 int
 2189 fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
 2190 {
 2191 
 2192         return (_fgetvp(td, fd, vpp, FWRITE));
 2193 }
 2194 #endif
 2195 
 2196 /*
 2197  * Like fget() but loads the underlying socket, or returns an error if the
 2198  * descriptor does not represent a socket.
 2199  *
 2200  * We bump the ref count on the returned socket.  XXX Also obtain the SX lock
 2201  * in the future.
 2202  *
 2203  * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
 2204  * on their file descriptor reference to prevent the socket from being free'd
 2205  * during use.
 2206  */
 2207 int
 2208 fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
 2209 {
 2210         struct file *fp;
 2211         int error;
 2212 
 2213         *spp = NULL;
 2214         if (fflagp != NULL)
 2215                 *fflagp = 0;
 2216         if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
 2217                 return (error);
 2218         if (fp->f_type != DTYPE_SOCKET) {
 2219                 error = ENOTSOCK;
 2220         } else {
 2221                 *spp = fp->f_data;
 2222                 if (fflagp)
 2223                         *fflagp = fp->f_flag;
 2224                 SOCK_LOCK(*spp);
 2225                 soref(*spp);
 2226                 SOCK_UNLOCK(*spp);
 2227         }
 2228         FILEDESC_SUNLOCK(td->td_proc->p_fd);
 2229         return (error);
 2230 }
 2231 
 2232 /*
 2233  * Drop the reference count on the socket and XXX release the SX lock in the
 2234  * future.  The last reference closes the socket.
 2235  *
 2236  * Note: fputsock() is deprecated, see comment for fgetsock().
 2237  */
 2238 void
 2239 fputsock(struct socket *so)
 2240 {
 2241 
 2242         ACCEPT_LOCK();
 2243         SOCK_LOCK(so);
 2244         sorele(so);
 2245 }
 2246 
 2247 int
 2248 fdrop(struct file *fp, struct thread *td)
 2249 {
 2250 
 2251         FILE_LOCK(fp);
 2252         return (fdrop_locked(fp, td));
 2253 }
 2254 
 2255 /*
 2256  * Drop reference on struct file passed in, may call closef if the
 2257  * reference hits zero.
 2258  * Expects struct file locked, and will unlock it.
 2259  */
 2260 static int
 2261 fdrop_locked(struct file *fp, struct thread *td)
 2262 {
 2263         int error;
 2264 
 2265         FILE_LOCK_ASSERT(fp, MA_OWNED);
 2266 
 2267         if (--fp->f_count > 0) {
 2268                 FILE_UNLOCK(fp);
 2269                 return (0);
 2270         }
 2271 
 2272         /*
 2273          * We might have just dropped the last reference to a file
 2274          * object that is for a UNIX domain socket whose message
 2275          * buffers are being examined in unp_gc().  If that is the
 2276          * case, FWAIT will be set in f_gcflag and we need to wait for
 2277          * unp_gc() to finish its scan.
 2278          */
 2279         while (fp->f_gcflag & FWAIT)
 2280                 msleep(&fp->f_gcflag, fp->f_mtxp, 0, "fpdrop", 0);
 2281 
 2282         /* We have the last ref so we can proceed without the file lock. */
 2283         FILE_UNLOCK(fp);
 2284         if (fp->f_count < 0)
 2285                 panic("fdrop: count < 0");
 2286         if (fp->f_ops != &badfileops)
 2287                 error = fo_close(fp, td);
 2288         else
 2289                 error = 0;
 2290 
 2291         sx_xlock(&filelist_lock);
 2292         LIST_REMOVE(fp, f_list);
 2293         openfiles--;
 2294         sx_xunlock(&filelist_lock);
 2295 
 2296         /*
 2297          * The f_cdevpriv cannot be assigned non-NULL value while we
 2298          * are destroying the file.
 2299          */
 2300         if (fp->f_cdevpriv != NULL)
 2301                 devfs_fpdrop(fp);
 2302         crfree(fp->f_cred);
 2303         uma_zfree(file_zone, fp);
 2304 
 2305         return (error);
 2306 }
 2307 
 2308 /*
 2309  * Apply an advisory lock on a file descriptor.
 2310  *
 2311  * Just attempt to get a record lock of the requested type on the entire file
 2312  * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
 2313  */
 2314 #ifndef _SYS_SYSPROTO_H_
 2315 struct flock_args {
 2316         int     fd;
 2317         int     how;
 2318 };
 2319 #endif
 2320 /* ARGSUSED */
 2321 int
 2322 flock(struct thread *td, struct flock_args *uap)
 2323 {
 2324         struct file *fp;
 2325         struct vnode *vp;
 2326         struct flock lf;
 2327         int vfslocked;
 2328         int error;
 2329 
 2330         if ((error = fget(td, uap->fd, &fp)) != 0)
 2331                 return (error);
 2332         if (fp->f_type != DTYPE_VNODE) {
 2333                 fdrop(fp, td);
 2334                 return (EOPNOTSUPP);
 2335         }
 2336 
 2337         vp = fp->f_vnode;
 2338         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2339         lf.l_whence = SEEK_SET;
 2340         lf.l_start = 0;
 2341         lf.l_len = 0;
 2342         if (uap->how & LOCK_UN) {
 2343                 lf.l_type = F_UNLCK;
 2344                 FILE_LOCK(fp);
 2345                 fp->f_flag &= ~FHASLOCK;
 2346                 FILE_UNLOCK(fp);
 2347                 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
 2348                 goto done2;
 2349         }
 2350         if (uap->how & LOCK_EX)
 2351                 lf.l_type = F_WRLCK;
 2352         else if (uap->how & LOCK_SH)
 2353                 lf.l_type = F_RDLCK;
 2354         else {
 2355                 error = EBADF;
 2356                 goto done2;
 2357         }
 2358         FILE_LOCK(fp);
 2359         fp->f_flag |= FHASLOCK;
 2360         FILE_UNLOCK(fp);
 2361         error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
 2362             (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
 2363 done2:
 2364         fdrop(fp, td);
 2365         VFS_UNLOCK_GIANT(vfslocked);
 2366         return (error);
 2367 }
 2368 /*
 2369  * Duplicate the specified descriptor to a free descriptor.
 2370  */
 2371 int
 2372 dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
 2373 {
 2374         struct file *wfp;
 2375         struct file *fp;
 2376 
 2377         /*
 2378          * If the to-be-dup'd fd number is greater than the allowed number
 2379          * of file descriptors, or the fd to be dup'd has already been
 2380          * closed, then reject.
 2381          */
 2382         FILEDESC_XLOCK(fdp);
 2383         if (dfd < 0 || dfd >= fdp->fd_nfiles ||
 2384             (wfp = fdp->fd_ofiles[dfd]) == NULL) {
 2385                 FILEDESC_XUNLOCK(fdp);
 2386                 return (EBADF);
 2387         }
 2388 
 2389         /*
 2390          * There are two cases of interest here.
 2391          *
 2392          * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
 2393          *
 2394          * For ENXIO steal away the file structure from (dfd) and store it in
 2395          * (indx).  (dfd) is effectively closed by this operation.
 2396          *
 2397          * Any other error code is just returned.
 2398          */
 2399         switch (error) {
 2400         case ENODEV:
 2401                 /*
 2402                  * Check that the mode the file is being opened for is a
 2403                  * subset of the mode of the existing descriptor.
 2404                  */
 2405                 FILE_LOCK(wfp);
 2406                 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
 2407                         FILE_UNLOCK(wfp);
 2408                         FILEDESC_XUNLOCK(fdp);
 2409                         return (EACCES);
 2410                 }
 2411                 fp = fdp->fd_ofiles[indx];
 2412                 fdp->fd_ofiles[indx] = wfp;
 2413                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2414                 if (fp == NULL)
 2415                         fdused(fdp, indx);
 2416                 fhold_locked(wfp);
 2417                 FILE_UNLOCK(wfp);
 2418                 FILEDESC_XUNLOCK(fdp);
 2419                 if (fp != NULL)
 2420                         /*
 2421                          * We now own the reference to fp that the ofiles[]
 2422                          * array used to own.  Release it.
 2423                          */
 2424                         fdrop(fp, td);
 2425                 return (0);
 2426 
 2427         case ENXIO:
 2428                 /*
 2429                  * Steal away the file pointer from dfd and stuff it into indx.
 2430                  */
 2431                 fp = fdp->fd_ofiles[indx];
 2432                 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
 2433                 fdp->fd_ofiles[dfd] = NULL;
 2434                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2435                 fdp->fd_ofileflags[dfd] = 0;
 2436                 fdunused(fdp, dfd);
 2437                 if (fp == NULL)
 2438                         fdused(fdp, indx);
 2439                 FILEDESC_XUNLOCK(fdp);
 2440 
 2441                 /*
 2442                  * We now own the reference to fp that the ofiles[] array
 2443                  * used to own.  Release it.
 2444                  */
 2445                 if (fp != NULL)
 2446                         fdrop(fp, td);
 2447                 return (0);
 2448 
 2449         default:
 2450                 FILEDESC_XUNLOCK(fdp);
 2451                 return (error);
 2452         }
 2453         /* NOTREACHED */
 2454 }
 2455 
 2456 /*
 2457  * Scan all active processes to see if any of them have a current or root
 2458  * directory of `olddp'. If so, replace them with the new mount point.
 2459  */
 2460 void
 2461 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
 2462 {
 2463         struct filedesc *fdp;
 2464         struct proc *p;
 2465         int nrele;
 2466 
 2467         if (vrefcnt(olddp) == 1)
 2468                 return;
 2469         sx_slock(&allproc_lock);
 2470         FOREACH_PROC_IN_SYSTEM(p) {
 2471                 fdp = fdhold(p);
 2472                 if (fdp == NULL)
 2473                         continue;
 2474                 nrele = 0;
 2475                 FILEDESC_XLOCK(fdp);
 2476                 if (fdp->fd_cdir == olddp) {
 2477                         vref(newdp);
 2478                         fdp->fd_cdir = newdp;
 2479                         nrele++;
 2480                 }
 2481                 if (fdp->fd_rdir == olddp) {
 2482                         vref(newdp);
 2483                         fdp->fd_rdir = newdp;
 2484                         nrele++;
 2485                 }
 2486                 FILEDESC_XUNLOCK(fdp);
 2487                 fddrop(fdp);
 2488                 while (nrele--)
 2489                         vrele(olddp);
 2490         }
 2491         sx_sunlock(&allproc_lock);
 2492         if (rootvnode == olddp) {
 2493                 vrele(rootvnode);
 2494                 vref(newdp);
 2495                 rootvnode = newdp;
 2496         }
 2497 }
 2498 
 2499 struct filedesc_to_leader *
 2500 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
 2501 {
 2502         struct filedesc_to_leader *fdtol;
 2503 
 2504         MALLOC(fdtol, struct filedesc_to_leader *,
 2505                sizeof(struct filedesc_to_leader),
 2506                M_FILEDESC_TO_LEADER,
 2507                M_WAITOK);
 2508         fdtol->fdl_refcount = 1;
 2509         fdtol->fdl_holdcount = 0;
 2510         fdtol->fdl_wakeup = 0;
 2511         fdtol->fdl_leader = leader;
 2512         if (old != NULL) {
 2513                 FILEDESC_XLOCK(fdp);
 2514                 fdtol->fdl_next = old->fdl_next;
 2515                 fdtol->fdl_prev = old;
 2516                 old->fdl_next = fdtol;
 2517                 fdtol->fdl_next->fdl_prev = fdtol;
 2518                 FILEDESC_XUNLOCK(fdp);
 2519         } else {
 2520                 fdtol->fdl_next = fdtol;
 2521                 fdtol->fdl_prev = fdtol;
 2522         }
 2523         return (fdtol);
 2524 }
 2525 
 2526 /*
 2527  * Get file structures globally.
 2528  */
 2529 static int
 2530 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
 2531 {
 2532         struct xfile xf;
 2533         struct filedesc *fdp;
 2534         struct file *fp;
 2535         struct proc *p;
 2536         int error, n;
 2537 
 2538         /*
 2539          * Note: because the number of file descriptors is calculated
 2540          * in different ways for sizing vs returning the data,
 2541          * there is information leakage from the first loop.  However,
 2542          * it is of a similar order of magnitude to the leakage from
 2543          * global system statistics such as kern.openfiles.
 2544          */
 2545         error = sysctl_wire_old_buffer(req, 0);
 2546         if (error != 0)
 2547                 return (error);
 2548         if (req->oldptr == NULL) {
 2549                 n = 16;         /* A slight overestimate. */
 2550                 sx_slock(&filelist_lock);
 2551                 LIST_FOREACH(fp, &filehead, f_list) {
 2552                         /*
 2553                          * We should grab the lock, but this is an
 2554                          * estimate, so does it really matter?
 2555                          */
 2556                         /* mtx_lock(fp->f_mtxp); */
 2557                         n += fp->f_count;
 2558                         /* mtx_unlock(f->f_mtxp); */
 2559                 }
 2560                 sx_sunlock(&filelist_lock);
 2561                 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
 2562         }
 2563         error = 0;
 2564         bzero(&xf, sizeof(xf));
 2565         xf.xf_size = sizeof(xf);
 2566         sx_slock(&allproc_lock);
 2567         FOREACH_PROC_IN_SYSTEM(p) {
 2568                 if (p->p_state == PRS_NEW)
 2569                         continue;
 2570                 PROC_LOCK(p);
 2571                 if (p_cansee(req->td, p) != 0) {
 2572                         PROC_UNLOCK(p);
 2573                         continue;
 2574                 }
 2575                 xf.xf_pid = p->p_pid;
 2576                 xf.xf_uid = p->p_ucred->cr_uid;
 2577                 PROC_UNLOCK(p);
 2578                 fdp = fdhold(p);
 2579                 if (fdp == NULL)
 2580                         continue;
 2581                 FILEDESC_SLOCK(fdp);
 2582                 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
 2583                         if ((fp = fdp->fd_ofiles[n]) == NULL)
 2584                                 continue;
 2585                         xf.xf_fd = n;
 2586                         xf.xf_file = fp;
 2587                         xf.xf_data = fp->f_data;
 2588                         xf.xf_vnode = fp->f_vnode;
 2589                         xf.xf_type = fp->f_type;
 2590                         xf.xf_count = fp->f_count;
 2591                         xf.xf_msgcount = fp->f_msgcount;
 2592                         xf.xf_offset = fp->f_offset;
 2593                         xf.xf_flag = fp->f_flag;
 2594                         error = SYSCTL_OUT(req, &xf, sizeof(xf));
 2595                         if (error)
 2596                                 break;
 2597                 }
 2598                 FILEDESC_SUNLOCK(fdp);
 2599                 fddrop(fdp);
 2600                 if (error)
 2601                         break;
 2602         }
 2603         sx_sunlock(&allproc_lock);
 2604         return (error);
 2605 }
 2606 
 2607 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
 2608     0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
 2609 
 2610 #ifdef KINFO_OFILE_SIZE
 2611 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
 2612 #endif
 2613 
 2614 /* Compatability with early 7-stable */
 2615 static int
 2616 export_vnode_for_osysctl(struct vnode *vp, int type,
 2617     struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
 2618 {
 2619         int error;
 2620         char *fullpath, *freepath;
 2621         int vfslocked;
 2622 
 2623         bzero(kif, sizeof(*kif));
 2624         kif->kf_structsize = sizeof(*kif);
 2625 
 2626         vref(vp);
 2627         kif->kf_fd = type;
 2628         kif->kf_type = KF_TYPE_VNODE;
 2629         /* This function only handles directories. */
 2630         KASSERT(vp->v_type == VDIR, ("export_vnode_for_osysctl: vnode not directory"));
 2631         kif->kf_vnode_type = KF_VTYPE_VDIR;
 2632 
 2633         /*
 2634          * This is not a true file descriptor, so we set a bogus refcount
 2635          * and offset to indicate these fields should be ignored.
 2636          */
 2637         kif->kf_ref_count = -1;
 2638         kif->kf_offset = -1;
 2639 
 2640         freepath = NULL;
 2641         fullpath = "-";
 2642         FILEDESC_SUNLOCK(fdp);
 2643         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2644         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
 2645         vn_fullpath(curthread, vp, &fullpath, &freepath);
 2646         vput(vp);
 2647         VFS_UNLOCK_GIANT(vfslocked);
 2648         strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
 2649         if (freepath != NULL)
 2650                 free(freepath, M_TEMP);
 2651         error = SYSCTL_OUT(req, kif, sizeof(*kif));
 2652         FILEDESC_SLOCK(fdp);
 2653         return (error);
 2654 }
 2655 
 2656 /*
 2657  * Get per-process file descriptors for use by procstat(1), et al.
 2658  */
 2659 static int
 2660 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
 2661 {
 2662         char *fullpath, *freepath;
 2663         struct kinfo_ofile *kif;
 2664         struct filedesc *fdp;
 2665         int error, i, *name;
 2666         struct socket *so;
 2667         struct vnode *vp;
 2668         struct file *fp;
 2669         struct proc *p;
 2670         int vfslocked;
 2671 
 2672         name = (int *)arg1;
 2673         if ((p = pfind((pid_t)name[0])) == NULL)
 2674                 return (ESRCH);
 2675         if ((error = p_candebug(curthread, p))) {
 2676                 PROC_UNLOCK(p);
 2677                 return (error);
 2678         }
 2679         fdp = fdhold(p);
 2680         PROC_UNLOCK(p);
 2681         if (fdp == NULL)
 2682                 return (ENOENT);
 2683         kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
 2684         FILEDESC_SLOCK(fdp);
 2685         if (fdp->fd_cdir != NULL)
 2686                 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
 2687                                 fdp, req);
 2688         if (fdp->fd_rdir != NULL)
 2689                 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
 2690                                 fdp, req);
 2691         if (fdp->fd_jdir != NULL)
 2692                 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
 2693                                 fdp, req);
 2694         for (i = 0; i < fdp->fd_nfiles; i++) {
 2695                 if ((fp = fdp->fd_ofiles[i]) == NULL)
 2696                         continue;
 2697                 bzero(kif, sizeof(*kif));
 2698                 kif->kf_structsize = sizeof(*kif);
 2699                 FILE_LOCK(fp);
 2700                 vp = NULL;
 2701                 so = NULL;
 2702                 kif->kf_fd = i;
 2703                 switch (fp->f_type) {
 2704                 case DTYPE_VNODE:
 2705                         kif->kf_type = KF_TYPE_VNODE;
 2706                         vp = fp->f_vnode;
 2707                         vref(vp);
 2708                         break;
 2709 
 2710                 case DTYPE_SOCKET:
 2711                         kif->kf_type = KF_TYPE_SOCKET;
 2712                         so = fp->f_data;
 2713                         break;
 2714 
 2715                 case DTYPE_PIPE:
 2716                         kif->kf_type = KF_TYPE_PIPE;
 2717                         break;
 2718 
 2719                 case DTYPE_FIFO:
 2720                         kif->kf_type = KF_TYPE_FIFO;
 2721                         vp = fp->f_vnode;
 2722                         vref(vp);
 2723                         break;
 2724 
 2725                 case DTYPE_KQUEUE:
 2726                         kif->kf_type = KF_TYPE_KQUEUE;
 2727                         break;
 2728 
 2729                 case DTYPE_CRYPTO:
 2730                         kif->kf_type = KF_TYPE_CRYPTO;
 2731                         break;
 2732 
 2733                 case DTYPE_MQUEUE:
 2734                         kif->kf_type = KF_TYPE_MQUEUE;
 2735                         break;
 2736 
 2737                 default:
 2738                         kif->kf_type = KF_TYPE_UNKNOWN;
 2739                         break;
 2740                 }
 2741                 kif->kf_ref_count = fp->f_count;
 2742                 if (fp->f_flag & FREAD)
 2743                         kif->kf_flags |= KF_FLAG_READ;
 2744                 if (fp->f_flag & FWRITE)
 2745                         kif->kf_flags |= KF_FLAG_WRITE;
 2746                 if (fp->f_flag & FAPPEND)
 2747                         kif->kf_flags |= KF_FLAG_APPEND;
 2748                 if (fp->f_flag & FASYNC)
 2749                         kif->kf_flags |= KF_FLAG_ASYNC;
 2750                 if (fp->f_flag & FFSYNC)
 2751                         kif->kf_flags |= KF_FLAG_FSYNC;
 2752                 if (fp->f_flag & FNONBLOCK)
 2753                         kif->kf_flags |= KF_FLAG_NONBLOCK;
 2754                 if (fp->f_flag & O_DIRECT)
 2755                         kif->kf_flags |= KF_FLAG_DIRECT;
 2756                 if (fp->f_flag & FHASLOCK)
 2757                         kif->kf_flags |= KF_FLAG_HASLOCK;
 2758                 kif->kf_offset = fp->f_offset;
 2759                 FILE_UNLOCK(fp);
 2760                 if (vp != NULL) {
 2761                         switch (vp->v_type) {
 2762                         case VNON:
 2763                                 kif->kf_vnode_type = KF_VTYPE_VNON;
 2764                                 break;
 2765                         case VREG:
 2766                                 kif->kf_vnode_type = KF_VTYPE_VREG;
 2767                                 break;
 2768                         case VDIR:
 2769                                 kif->kf_vnode_type = KF_VTYPE_VDIR;
 2770                                 break;
 2771                         case VBLK:
 2772                                 kif->kf_vnode_type = KF_VTYPE_VBLK;
 2773                                 break;
 2774                         case VCHR:
 2775                                 kif->kf_vnode_type = KF_VTYPE_VCHR;
 2776                                 break;
 2777                         case VLNK:
 2778                                 kif->kf_vnode_type = KF_VTYPE_VLNK;
 2779                                 break;
 2780                         case VSOCK:
 2781                                 kif->kf_vnode_type = KF_VTYPE_VSOCK;
 2782                                 break;
 2783                         case VFIFO:
 2784                                 kif->kf_vnode_type = KF_VTYPE_VFIFO;
 2785                                 break;
 2786                         case VBAD:
 2787                                 kif->kf_vnode_type = KF_VTYPE_VBAD;
 2788                                 break;
 2789                         default:
 2790                                 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
 2791                                 break;
 2792                         }
 2793                         /*
 2794                          * It is OK to drop the filedesc lock here as we will
 2795                          * re-validate and re-evaluate its properties when
 2796                          * the loop continues.
 2797                          */
 2798                         freepath = NULL;
 2799                         fullpath = "-";
 2800                         FILEDESC_SUNLOCK(fdp);
 2801                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2802                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
 2803                         vn_fullpath(curthread, vp, &fullpath, &freepath);
 2804                         vput(vp);
 2805                         VFS_UNLOCK_GIANT(vfslocked);
 2806                         strlcpy(kif->kf_path, fullpath,
 2807                             sizeof(kif->kf_path));
 2808                         if (freepath != NULL)
 2809                                 free(freepath, M_TEMP);
 2810                         FILEDESC_SLOCK(fdp);
 2811                 }
 2812                 if (so != NULL) {
 2813                         struct sockaddr *sa;
 2814 
 2815                         if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
 2816                             == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
 2817                                 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
 2818                                 free(sa, M_SONAME);
 2819                         }
 2820                         if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
 2821                             == 00 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
 2822                                 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
 2823                                 free(sa, M_SONAME);
 2824                         }
 2825                         kif->kf_sock_domain =
 2826                             so->so_proto->pr_domain->dom_family;
 2827                         kif->kf_sock_type = so->so_type;
 2828                         kif->kf_sock_protocol = so->so_proto->pr_protocol;
 2829                 }
 2830                 error = SYSCTL_OUT(req, kif, sizeof(*kif));
 2831                 if (error)
 2832                         break;
 2833         }
 2834         FILEDESC_SUNLOCK(fdp);
 2835         fddrop(fdp);
 2836         free(kif, M_TEMP);
 2837         return (0);
 2838 }
 2839 
 2840 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
 2841     sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
 2842 
 2843 #ifdef KINFO_FILE_SIZE
 2844 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
 2845 #endif
 2846 
 2847 static int
 2848 export_vnode_for_sysctl(struct vnode *vp, int type,
 2849     struct kinfo_file *kif, struct filedesc *fdp, struct sysctl_req *req)
 2850 {
 2851         int error;
 2852         char *fullpath, *freepath;
 2853         int vfslocked;
 2854 
 2855         bzero(kif, sizeof(*kif));
 2856 
 2857         vref(vp);
 2858         kif->kf_fd = type;
 2859         kif->kf_type = KF_TYPE_VNODE;
 2860         /* This function only handles directories. */
 2861         KASSERT(vp->v_type == VDIR, ("export_vnode_for_sysctl: vnode not directory"));
 2862         kif->kf_vnode_type = KF_VTYPE_VDIR;
 2863 
 2864         /*
 2865          * This is not a true file descriptor, so we set a bogus refcount
 2866          * and offset to indicate these fields should be ignored.
 2867          */
 2868         kif->kf_ref_count = -1;
 2869         kif->kf_offset = -1;
 2870 
 2871         freepath = NULL;
 2872         fullpath = "-";
 2873         FILEDESC_SUNLOCK(fdp);
 2874         vn_fullpath(curthread, vp, &fullpath, &freepath);
 2875         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 2876         vrele(vp);
 2877         VFS_UNLOCK_GIANT(vfslocked);
 2878         strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
 2879         if (freepath != NULL)
 2880                 free(freepath, M_TEMP);
 2881         /* Pack record size down */
 2882         kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
 2883             strlen(kif->kf_path) + 1;
 2884         kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
 2885         error = SYSCTL_OUT(req, kif, kif->kf_structsize);
 2886         FILEDESC_SLOCK(fdp);
 2887         return (error);
 2888 }
 2889 
 2890 /*
 2891  * Get per-process file descriptors for use by procstat(1), et al.
 2892  */
 2893 static int
 2894 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
 2895 {
 2896         char *fullpath, *freepath;
 2897         struct kinfo_file *kif;
 2898         struct filedesc *fdp;
 2899         int error, i, *name;
 2900         struct socket *so;
 2901         struct vnode *vp;
 2902         struct file *fp;
 2903         struct proc *p;
 2904         int vfslocked;
 2905         size_t oldidx;
 2906 
 2907         name = (int *)arg1;
 2908         if ((p = pfind((pid_t)name[0])) == NULL)
 2909                 return (ESRCH);
 2910         if ((error = p_candebug(curthread, p))) {
 2911                 PROC_UNLOCK(p);
 2912                 return (error);
 2913         }
 2914         fdp = fdhold(p);
 2915         PROC_UNLOCK(p);
 2916         if (fdp == NULL)
 2917                 return (ENOENT);
 2918         kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
 2919         FILEDESC_SLOCK(fdp);
 2920         if (fdp->fd_cdir != NULL)
 2921                 export_vnode_for_sysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
 2922                                 fdp, req);
 2923         if (fdp->fd_rdir != NULL)
 2924                 export_vnode_for_sysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
 2925                                 fdp, req);
 2926         if (fdp->fd_jdir != NULL)
 2927                 export_vnode_for_sysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
 2928                                 fdp, req);
 2929         for (i = 0; i < fdp->fd_nfiles; i++) {
 2930                 if ((fp = fdp->fd_ofiles[i]) == NULL)
 2931                         continue;
 2932                 bzero(kif, sizeof(*kif));
 2933                 FILE_LOCK(fp);
 2934                 vp = NULL;
 2935                 so = NULL;
 2936                 kif->kf_fd = i;
 2937                 switch (fp->f_type) {
 2938                 case DTYPE_VNODE:
 2939                         kif->kf_type = KF_TYPE_VNODE;
 2940                         vp = fp->f_vnode;
 2941                         vref(vp);
 2942                         break;
 2943 
 2944                 case DTYPE_SOCKET:
 2945                         kif->kf_type = KF_TYPE_SOCKET;
 2946                         so = fp->f_data;
 2947                         break;
 2948 
 2949                 case DTYPE_PIPE:
 2950                         kif->kf_type = KF_TYPE_PIPE;
 2951                         break;
 2952 
 2953                 case DTYPE_FIFO:
 2954                         kif->kf_type = KF_TYPE_FIFO;
 2955                         vp = fp->f_vnode;
 2956                         vref(vp);
 2957                         break;
 2958 
 2959                 case DTYPE_KQUEUE:
 2960                         kif->kf_type = KF_TYPE_KQUEUE;
 2961                         break;
 2962 
 2963                 case DTYPE_CRYPTO:
 2964                         kif->kf_type = KF_TYPE_CRYPTO;
 2965                         break;
 2966 
 2967                 case DTYPE_MQUEUE:
 2968                         kif->kf_type = KF_TYPE_MQUEUE;
 2969                         break;
 2970 
 2971                 case DTYPE_SEM:
 2972                         kif->kf_type = KF_TYPE_SEM;
 2973                         break;
 2974 
 2975                 default:
 2976                         kif->kf_type = KF_TYPE_UNKNOWN;
 2977                         break;
 2978                 }
 2979                 kif->kf_ref_count = fp->f_count;
 2980                 if (fp->f_flag & FREAD)
 2981                         kif->kf_flags |= KF_FLAG_READ;
 2982                 if (fp->f_flag & FWRITE)
 2983                         kif->kf_flags |= KF_FLAG_WRITE;
 2984                 if (fp->f_flag & FAPPEND)
 2985                         kif->kf_flags |= KF_FLAG_APPEND;
 2986                 if (fp->f_flag & FASYNC)
 2987                         kif->kf_flags |= KF_FLAG_ASYNC;
 2988                 if (fp->f_flag & FFSYNC)
 2989                         kif->kf_flags |= KF_FLAG_FSYNC;
 2990                 if (fp->f_flag & FNONBLOCK)
 2991                         kif->kf_flags |= KF_FLAG_NONBLOCK;
 2992                 if (fp->f_flag & O_DIRECT)
 2993                         kif->kf_flags |= KF_FLAG_DIRECT;
 2994                 if (fp->f_flag & FHASLOCK)
 2995                         kif->kf_flags |= KF_FLAG_HASLOCK;
 2996                 kif->kf_offset = fp->f_offset;
 2997                 FILE_UNLOCK(fp);
 2998                 if (vp != NULL) {
 2999                         switch (vp->v_type) {
 3000                         case VNON:
 3001                                 kif->kf_vnode_type = KF_VTYPE_VNON;
 3002                                 break;
 3003                         case VREG:
 3004                                 kif->kf_vnode_type = KF_VTYPE_VREG;
 3005                                 break;
 3006                         case VDIR:
 3007                                 kif->kf_vnode_type = KF_VTYPE_VDIR;
 3008                                 break;
 3009                         case VBLK:
 3010                                 kif->kf_vnode_type = KF_VTYPE_VBLK;
 3011                                 break;
 3012                         case VCHR:
 3013                                 kif->kf_vnode_type = KF_VTYPE_VCHR;
 3014                                 break;
 3015                         case VLNK:
 3016                                 kif->kf_vnode_type = KF_VTYPE_VLNK;
 3017                                 break;
 3018                         case VSOCK:
 3019                                 kif->kf_vnode_type = KF_VTYPE_VSOCK;
 3020                                 break;
 3021                         case VFIFO:
 3022                                 kif->kf_vnode_type = KF_VTYPE_VFIFO;
 3023                                 break;
 3024                         case VBAD:
 3025                                 kif->kf_vnode_type = KF_VTYPE_VBAD;
 3026                                 break;
 3027                         default:
 3028                                 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
 3029                                 break;
 3030                         }
 3031                         /*
 3032                          * It is OK to drop the filedesc lock here as we will
 3033                          * re-validate and re-evaluate its properties when
 3034                          * the loop continues.
 3035                          */
 3036                         freepath = NULL;
 3037                         fullpath = "-";
 3038                         FILEDESC_SUNLOCK(fdp);
 3039                         vn_fullpath(curthread, vp, &fullpath, &freepath);
 3040                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 3041                         vrele(vp);
 3042                         VFS_UNLOCK_GIANT(vfslocked);
 3043                         strlcpy(kif->kf_path, fullpath,
 3044                             sizeof(kif->kf_path));
 3045                         if (freepath != NULL)
 3046                                 free(freepath, M_TEMP);
 3047                         FILEDESC_SLOCK(fdp);
 3048                 }
 3049                 if (so != NULL) {
 3050                         struct sockaddr *sa;
 3051 
 3052                         if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
 3053                             == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
 3054                                 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
 3055                                 free(sa, M_SONAME);
 3056                         }
 3057                         if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
 3058                             == 00 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
 3059                                 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
 3060                                 free(sa, M_SONAME);
 3061                         }
 3062                         kif->kf_sock_domain =
 3063                             so->so_proto->pr_domain->dom_family;
 3064                         kif->kf_sock_type = so->so_type;
 3065                         kif->kf_sock_protocol = so->so_proto->pr_protocol;
 3066                 }
 3067                 /* Pack record size down */
 3068                 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
 3069                     strlen(kif->kf_path) + 1;
 3070                 kif->kf_structsize = roundup(kif->kf_structsize,
 3071                     sizeof(uint64_t));
 3072                 oldidx = req->oldidx;
 3073                 error = SYSCTL_OUT(req, kif, kif->kf_structsize);
 3074                 if (error) {
 3075                         if (error == ENOMEM) {
 3076                                 /*
 3077                                  * The hack to keep the ABI of sysctl
 3078                                  * kern.proc.filedesc intact, but not
 3079                                  * to account a partially copied
 3080                                  * kinfo_file into the oldidx.
 3081                                  */
 3082                                 req->oldidx = oldidx;
 3083                                 error = 0;
 3084                         }
 3085                         break;
 3086                 }
 3087         }
 3088         FILEDESC_SUNLOCK(fdp);
 3089         fddrop(fdp);
 3090         free(kif, M_TEMP);
 3091         return (error);
 3092 }
 3093 
 3094 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
 3095     sysctl_kern_proc_filedesc, "Process filedesc entries");
 3096 
 3097 #ifdef DDB
 3098 /*
 3099  * For the purposes of debugging, generate a human-readable string for the
 3100  * file type.
 3101  */
 3102 static const char *
 3103 file_type_to_name(short type)
 3104 {
 3105 
 3106         switch (type) {
 3107         case 0:
 3108                 return ("zero");
 3109         case DTYPE_VNODE:
 3110                 return ("vnod");
 3111         case DTYPE_SOCKET:
 3112                 return ("sock");
 3113         case DTYPE_PIPE:
 3114                 return ("pipe");
 3115         case DTYPE_FIFO:
 3116                 return ("fifo");
 3117         case DTYPE_KQUEUE:
 3118                 return ("kque");
 3119         case DTYPE_CRYPTO:
 3120                 return ("crpt");
 3121         case DTYPE_MQUEUE:
 3122                 return ("mque");
 3123         case DTYPE_SEM:
 3124                 return ("ksem");
 3125         default:
 3126                 return ("unkn");
 3127         }
 3128 }
 3129 
 3130 /*
 3131  * For the purposes of debugging, identify a process (if any, perhaps one of
 3132  * many) that references the passed file in its file descriptor array. Return
 3133  * NULL if none.
 3134  */
 3135 static struct proc *
 3136 file_to_first_proc(struct file *fp)
 3137 {
 3138         struct filedesc *fdp;
 3139         struct proc *p;
 3140         int n;
 3141 
 3142         FOREACH_PROC_IN_SYSTEM(p) {
 3143                 if (p->p_state == PRS_NEW)
 3144                         continue;
 3145                 fdp = p->p_fd;
 3146                 if (fdp == NULL)
 3147                         continue;
 3148                 for (n = 0; n < fdp->fd_nfiles; n++) {
 3149                         if (fp == fdp->fd_ofiles[n])
 3150                                 return (p);
 3151                 }
 3152         }
 3153         return (NULL);
 3154 }
 3155 
 3156 static void
 3157 db_print_file(struct file *fp, int header)
 3158 {
 3159         struct proc *p;
 3160 
 3161         if (header)
 3162                 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
 3163                     "File", "Type", "Data", "Flag", "GCFl", "Count",
 3164                     "MCount", "Vnode", "FPID", "FCmd");
 3165         p = file_to_first_proc(fp);
 3166         db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
 3167             file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
 3168             fp->f_gcflag, fp->f_count, fp->f_msgcount, fp->f_vnode,
 3169             p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
 3170 }
 3171 
 3172 DB_SHOW_COMMAND(file, db_show_file)
 3173 {
 3174         struct file *fp;
 3175 
 3176         if (!have_addr) {
 3177                 db_printf("usage: show file <addr>\n");
 3178                 return;
 3179         }
 3180         fp = (struct file *)addr;
 3181         db_print_file(fp, 1);
 3182 }
 3183 
 3184 DB_SHOW_COMMAND(files, db_show_files)
 3185 {
 3186         struct file *fp;
 3187         int header;
 3188 
 3189         header = 1;
 3190         LIST_FOREACH(fp, &filehead, f_list) {
 3191                 db_print_file(fp, header);
 3192                 header = 0;
 3193         }
 3194 }
 3195 #endif
 3196 
 3197 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
 3198     &maxfilesperproc, 0, "Maximum files allowed open per process");
 3199 
 3200 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
 3201     &maxfiles, 0, "Maximum number of files");
 3202 
 3203 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
 3204     &openfiles, 0, "System-wide number of open files");
 3205 
 3206 /* ARGSUSED*/
 3207 static void
 3208 filelistinit(void *dummy)
 3209 {
 3210 
 3211         file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
 3212             NULL, NULL, UMA_ALIGN_PTR, 0);
 3213         sx_init(&filelist_lock, "filelist lock");
 3214         mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
 3215         mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
 3216 }
 3217 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
 3218 
 3219 /*-------------------------------------------------------------------*/
 3220 
 3221 static int
 3222 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
 3223 {
 3224 
 3225         return (EBADF);
 3226 }
 3227 
 3228 static int
 3229 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
 3230 {
 3231 
 3232         return (EBADF);
 3233 }
 3234 
 3235 static int
 3236 badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
 3237 {
 3238 
 3239         return (0);
 3240 }
 3241 
 3242 static int
 3243 badfo_kqfilter(struct file *fp, struct knote *kn)
 3244 {
 3245 
 3246         return (EBADF);
 3247 }
 3248 
 3249 static int
 3250 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
 3251 {
 3252 
 3253         return (EBADF);
 3254 }
 3255 
 3256 static int
 3257 badfo_close(struct file *fp, struct thread *td)
 3258 {
 3259 
 3260         return (EBADF);
 3261 }
 3262 
 3263 struct fileops badfileops = {
 3264         .fo_read = badfo_readwrite,
 3265         .fo_write = badfo_readwrite,
 3266         .fo_ioctl = badfo_ioctl,
 3267         .fo_poll = badfo_poll,
 3268         .fo_kqfilter = badfo_kqfilter,
 3269         .fo_stat = badfo_stat,
 3270         .fo_close = badfo_close,
 3271 };
 3272 
 3273 
 3274 /*-------------------------------------------------------------------*/
 3275 
 3276 /*
 3277  * File Descriptor pseudo-device driver (/dev/fd/).
 3278  *
 3279  * Opening minor device N dup()s the file (if any) connected to file
 3280  * descriptor N belonging to the calling process.  Note that this driver
 3281  * consists of only the ``open()'' routine, because all subsequent
 3282  * references to this file will be direct to the other driver.
 3283  *
 3284  * XXX: we could give this one a cloning event handler if necessary.
 3285  */
 3286 
 3287 /* ARGSUSED */
 3288 static int
 3289 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
 3290 {
 3291 
 3292         /*
 3293          * XXX Kludge: set curthread->td_dupfd to contain the value of the
 3294          * the file descriptor being sought for duplication. The error
 3295          * return ensures that the vnode for this device will be released
 3296          * by vn_open. Open will detect this special error and take the
 3297          * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
 3298          * will simply report the error.
 3299          */
 3300         td->td_dupfd = dev2unit(dev);
 3301         return (ENODEV);
 3302 }
 3303 
 3304 static struct cdevsw fildesc_cdevsw = {
 3305         .d_version =    D_VERSION,
 3306         .d_flags =      D_NEEDGIANT,
 3307         .d_open =       fdopen,
 3308         .d_name =       "FD",
 3309 };
 3310 
 3311 static void
 3312 fildesc_drvinit(void *unused)
 3313 {
 3314         struct cdev *dev;
 3315 
 3316         dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
 3317         make_dev_alias(dev, "stdin");
 3318         dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
 3319         make_dev_alias(dev, "stdout");
 3320         dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
 3321         make_dev_alias(dev, "stderr");
 3322 }
 3323 
 3324 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);

Cache object: 6cc6e3da95f5db3e7c8ff7ca0cfcdaa2


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