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  * 3. All advertising materials mentioning features or use of this software
   19  *    must display the following acknowledgement:
   20  *      This product includes software developed by the University of
   21  *      California, Berkeley and its contributors.
   22  * 4. Neither the name of the University nor the names of its contributors
   23  *    may be used to endorse or promote products derived from this software
   24  *    without specific prior written permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   36  * SUCH DAMAGE.
   37  *
   38  *      @(#)kern_descrip.c      8.6 (Berkeley) 4/19/94
   39  * $FreeBSD: releng/5.0/sys/kern/kern_descrip.c 109045 2003-01-10 05:18:09Z rwatson $
   40  */
   41 
   42 #include "opt_compat.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/syscallsubr.h>
   47 #include <sys/sysproto.h>
   48 #include <sys/conf.h>
   49 #include <sys/filedesc.h>
   50 #include <sys/lock.h>
   51 #include <sys/kernel.h>
   52 #include <sys/malloc.h>
   53 #include <sys/mutex.h>
   54 #include <sys/sysctl.h>
   55 #include <sys/vnode.h>
   56 #include <sys/mount.h>
   57 #include <sys/proc.h>
   58 #include <sys/namei.h>
   59 #include <sys/file.h>
   60 #include <sys/stat.h>
   61 #include <sys/filio.h>
   62 #include <sys/fcntl.h>
   63 #include <sys/unistd.h>
   64 #include <sys/resourcevar.h>
   65 #include <sys/event.h>
   66 #include <sys/sx.h>
   67 #include <sys/socketvar.h>
   68 #include <sys/signalvar.h>
   69 
   70 #include <machine/limits.h>
   71 
   72 #include <vm/vm.h>
   73 #include <vm/vm_extern.h>
   74 #include <vm/uma.h>
   75 
   76 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
   77 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
   78 
   79 uma_zone_t file_zone;
   80 
   81 static   d_open_t  fdopen;
   82 #define NUMFDESC 64
   83 
   84 #define CDEV_MAJOR 22
   85 static struct cdevsw fildesc_cdevsw = {
   86         /* open */      fdopen,
   87         /* close */     noclose,
   88         /* read */      noread,
   89         /* write */     nowrite,
   90         /* ioctl */     noioctl,
   91         /* poll */      nopoll,
   92         /* mmap */      nommap,
   93         /* strategy */  nostrategy,
   94         /* name */      "FD",
   95         /* maj */       CDEV_MAJOR,
   96         /* dump */      nodump,
   97         /* psize */     nopsize,
   98         /* flags */     0,
   99 };
  100 
  101 /* How to treat 'new' parameter when allocating a fd for do_dup(). */
  102 enum dup_type { DUP_VARIABLE, DUP_FIXED };
  103 
  104 static int do_dup(struct thread *td, enum dup_type type, int old, int new,
  105     register_t *retval);
  106 static int badfo_readwrite(struct file *fp, struct uio *uio,
  107     struct ucred *active_cred, int flags, struct thread *td);
  108 static int badfo_ioctl(struct file *fp, u_long com, void *data,
  109     struct ucred *active_cred, struct thread *td);
  110 static int badfo_poll(struct file *fp, int events,
  111     struct ucred *active_cred, struct thread *td);
  112 static int badfo_kqfilter(struct file *fp, struct knote *kn);
  113 static int badfo_stat(struct file *fp, struct stat *sb,
  114     struct ucred *active_cred, struct thread *td);
  115 static int badfo_close(struct file *fp, struct thread *td);
  116 
  117 /*
  118  * Descriptor management.
  119  */
  120 struct filelist filehead;       /* head of list of open files */
  121 int nfiles;                     /* actual number of open files */
  122 extern int cmask;       
  123 struct sx filelist_lock;        /* sx to protect filelist */
  124 struct mtx sigio_lock;          /* mtx to protect pointers to sigio */
  125 
  126 /*
  127  * System calls on descriptors.
  128  */
  129 #ifndef _SYS_SYSPROTO_H_
  130 struct getdtablesize_args {
  131         int     dummy;
  132 };
  133 #endif
  134 /*
  135  * MPSAFE
  136  */
  137 /* ARGSUSED */
  138 int
  139 getdtablesize(td, uap)
  140         struct thread *td;
  141         struct getdtablesize_args *uap;
  142 {
  143         struct proc *p = td->td_proc;
  144 
  145         mtx_lock(&Giant);
  146         td->td_retval[0] = 
  147             min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
  148         mtx_unlock(&Giant);
  149         return (0);
  150 }
  151 
  152 /*
  153  * Duplicate a file descriptor to a particular value.
  154  *
  155  * note: keep in mind that a potential race condition exists when closing
  156  * descriptors from a shared descriptor table (via rfork).
  157  */
  158 #ifndef _SYS_SYSPROTO_H_
  159 struct dup2_args {
  160         u_int   from;
  161         u_int   to;
  162 };
  163 #endif
  164 /*
  165  * MPSAFE
  166  */
  167 /* ARGSUSED */
  168 int
  169 dup2(td, uap)
  170         struct thread *td;
  171         struct dup2_args *uap;
  172 {
  173 
  174         return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
  175                     td->td_retval));
  176 }
  177 
  178 /*
  179  * Duplicate a file descriptor.
  180  */
  181 #ifndef _SYS_SYSPROTO_H_
  182 struct dup_args {
  183         u_int   fd;
  184 };
  185 #endif
  186 /*
  187  * MPSAFE
  188  */
  189 /* ARGSUSED */
  190 int
  191 dup(td, uap)
  192         struct thread *td;
  193         struct dup_args *uap;
  194 {
  195 
  196         return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
  197 }
  198 
  199 /*
  200  * The file control system call.
  201  */
  202 #ifndef _SYS_SYSPROTO_H_
  203 struct fcntl_args {
  204         int     fd;
  205         int     cmd;
  206         long    arg;
  207 };
  208 #endif
  209 /*
  210  * MPSAFE
  211  */
  212 /* ARGSUSED */
  213 int
  214 fcntl(td, uap)
  215         struct thread *td;
  216         register struct fcntl_args *uap;
  217 {
  218         struct flock fl;
  219         intptr_t arg;
  220         int error;
  221 
  222         error = 0;
  223         switch (uap->cmd) {
  224         case F_GETLK:
  225         case F_SETLK:
  226         case F_SETLKW:
  227                 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
  228                 arg = (intptr_t)&fl;
  229                 break;
  230         default:
  231                 arg = uap->arg;
  232                 break;
  233         }
  234         if (error)
  235                 return (error);
  236         error = kern_fcntl(td, uap->fd, uap->cmd, arg);
  237         if (error)
  238                 return (error);
  239         if (uap->cmd == F_GETLK)
  240                 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
  241         return (error);
  242 }
  243 
  244 int
  245 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
  246 {
  247         register struct filedesc *fdp;
  248         struct flock *flp;
  249         register struct file *fp;
  250         struct proc *p;
  251         register char *pop;
  252         struct vnode *vp;
  253         u_int newmin;
  254         int error, flg, tmp;
  255 
  256         error = 0;
  257         flg = F_POSIX;
  258         p = td->td_proc;
  259         fdp = p->p_fd;
  260         mtx_lock(&Giant);
  261         FILEDESC_LOCK(fdp);
  262         if ((unsigned)fd >= fdp->fd_nfiles ||
  263             (fp = fdp->fd_ofiles[fd]) == NULL) {
  264                 FILEDESC_UNLOCK(fdp);
  265                 error = EBADF;
  266                 goto done2;
  267         }
  268         pop = &fdp->fd_ofileflags[fd];
  269 
  270         switch (cmd) {
  271         case F_DUPFD:
  272                 FILEDESC_UNLOCK(fdp);
  273                 newmin = arg;
  274                 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
  275                     newmin >= maxfilesperproc) {
  276                         error = EINVAL;
  277                         break;
  278                 }
  279                 error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
  280                 break;
  281 
  282         case F_GETFD:
  283                 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
  284                 FILEDESC_UNLOCK(fdp);
  285                 break;
  286 
  287         case F_SETFD:
  288                 *pop = (*pop &~ UF_EXCLOSE) |
  289                     (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
  290                 FILEDESC_UNLOCK(fdp);
  291                 break;
  292 
  293         case F_GETFL:
  294                 FILE_LOCK(fp);
  295                 FILEDESC_UNLOCK(fdp);
  296                 td->td_retval[0] = OFLAGS(fp->f_flag);
  297                 FILE_UNLOCK(fp);
  298                 break;
  299 
  300         case F_SETFL:
  301                 fhold(fp);
  302                 FILEDESC_UNLOCK(fdp);
  303                 fp->f_flag &= ~FCNTLFLAGS;
  304                 fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
  305                 tmp = fp->f_flag & FNONBLOCK;
  306                 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  307                 if (error) {
  308                         fdrop(fp, td);
  309                         break;
  310                 }
  311                 tmp = fp->f_flag & FASYNC;
  312                 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
  313                 if (error == 0) {
  314                         fdrop(fp, td);
  315                         break;
  316                 }
  317                 fp->f_flag &= ~FNONBLOCK;
  318                 tmp = 0;
  319                 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  320                 fdrop(fp, td);
  321                 break;
  322 
  323         case F_GETOWN:
  324                 fhold(fp);
  325                 FILEDESC_UNLOCK(fdp);
  326                 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
  327                 if (error == 0)
  328                         td->td_retval[0] = tmp;
  329                 fdrop(fp, td);
  330                 break;
  331 
  332         case F_SETOWN:
  333                 fhold(fp);
  334                 FILEDESC_UNLOCK(fdp);
  335                 tmp = arg;
  336                 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
  337                 fdrop(fp, td);
  338                 break;
  339 
  340         case F_SETLKW:
  341                 flg |= F_WAIT;
  342                 /* FALLTHROUGH F_SETLK */
  343 
  344         case F_SETLK:
  345                 if (fp->f_type != DTYPE_VNODE) {
  346                         FILEDESC_UNLOCK(fdp);
  347                         error = EBADF;
  348                         break;
  349                 }
  350 
  351                 flp = (struct flock *)arg;
  352                 if (flp->l_whence == SEEK_CUR) {
  353                         if (fp->f_offset < 0 ||
  354                             (flp->l_start > 0 &&
  355                              fp->f_offset > OFF_MAX - flp->l_start)) {
  356                                 FILEDESC_UNLOCK(fdp);
  357                                 error = EOVERFLOW;
  358                                 break;
  359                         }
  360                         flp->l_start += fp->f_offset;
  361                 }
  362 
  363                 /*
  364                  * VOP_ADVLOCK() may block.
  365                  */
  366                 fhold(fp);
  367                 FILEDESC_UNLOCK(fdp);
  368                 vp = (struct vnode *)fp->f_data;
  369 
  370                 switch (flp->l_type) {
  371                 case F_RDLCK:
  372                         if ((fp->f_flag & FREAD) == 0) {
  373                                 error = EBADF;
  374                                 break;
  375                         }
  376                         PROC_LOCK(p);
  377                         p->p_flag |= P_ADVLOCK;
  378                         PROC_UNLOCK(p);
  379                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  380                             flp, flg);
  381                         break;
  382                 case F_WRLCK:
  383                         if ((fp->f_flag & FWRITE) == 0) {
  384                                 error = EBADF;
  385                                 break;
  386                         }
  387                         PROC_LOCK(p);
  388                         p->p_flag |= P_ADVLOCK;
  389                         PROC_UNLOCK(p);
  390                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  391                             flp, flg);
  392                         break;
  393                 case F_UNLCK:
  394                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
  395                             flp, F_POSIX);
  396                         break;
  397                 default:
  398                         error = EINVAL;
  399                         break;
  400                 }
  401                 fdrop(fp, td);
  402                 break;
  403 
  404         case F_GETLK:
  405                 if (fp->f_type != DTYPE_VNODE) {
  406                         FILEDESC_UNLOCK(fdp);
  407                         error = EBADF;
  408                         break;
  409                 }
  410                 flp = (struct flock *)arg;
  411                 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
  412                     flp->l_type != F_UNLCK) {
  413                         FILEDESC_UNLOCK(fdp);
  414                         error = EINVAL;
  415                         break;
  416                 }
  417                 if (flp->l_whence == SEEK_CUR) {
  418                         if ((flp->l_start > 0 &&
  419                             fp->f_offset > OFF_MAX - flp->l_start) ||
  420                             (flp->l_start < 0 &&
  421                              fp->f_offset < OFF_MIN - flp->l_start)) {
  422                                 FILEDESC_UNLOCK(fdp);
  423                                 error = EOVERFLOW;
  424                                 break;
  425                         }
  426                         flp->l_start += fp->f_offset;
  427                 }
  428                 /*
  429                  * VOP_ADVLOCK() may block.
  430                  */
  431                 fhold(fp);
  432                 FILEDESC_UNLOCK(fdp);
  433                 vp = (struct vnode *)fp->f_data;
  434                 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
  435                     F_POSIX);
  436                 fdrop(fp, td);
  437                 break;
  438         default:
  439                 FILEDESC_UNLOCK(fdp);
  440                 error = EINVAL;
  441                 break;
  442         }
  443 done2:
  444         mtx_unlock(&Giant);
  445         return (error);
  446 }
  447 
  448 /*
  449  * Common code for dup, dup2, and fcntl(F_DUPFD).
  450  * filedesc must be locked, but will be unlocked as a side effect.
  451  */
  452 static int
  453 do_dup(td, type, old, new, retval)
  454         enum dup_type type;
  455         int old, new;
  456         register_t *retval;
  457         struct thread *td;
  458 {
  459         register struct filedesc *fdp;
  460         struct proc *p;
  461         struct file *fp;
  462         struct file *delfp;
  463         int error, newfd;
  464 
  465         p = td->td_proc;
  466         fdp = p->p_fd;
  467 
  468         /*
  469          * Verify we have a valid descriptor to dup from and possibly to
  470          * dup to.
  471          */
  472         if (old < 0 || new < 0 || new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
  473             new >= maxfilesperproc)
  474                 return (EBADF);
  475         FILEDESC_LOCK(fdp);
  476         if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
  477                 FILEDESC_UNLOCK(fdp);
  478                 return (EBADF);
  479         }
  480         if (type == DUP_FIXED && old == new) {
  481                 *retval = new;
  482                 FILEDESC_UNLOCK(fdp);
  483                 return (0);
  484         }
  485         fp = fdp->fd_ofiles[old];
  486         fhold(fp);
  487 
  488         /*
  489          * Expand the table for the new descriptor if needed.  This may
  490          * block and drop and reacquire the filedesc lock.
  491          */
  492         if (type == DUP_VARIABLE || new >= fdp->fd_nfiles) {
  493                 error = fdalloc(td, new, &newfd);
  494                 if (error) {
  495                         FILEDESC_UNLOCK(fdp);
  496                         fdrop(fp, td);
  497                         return (error);
  498                 }
  499         }
  500         if (type == DUP_VARIABLE)
  501                 new = newfd;
  502 
  503         /*
  504          * If the old file changed out from under us then treat it as a
  505          * bad file descriptor.  Userland should do its own locking to
  506          * avoid this case.
  507          */
  508         if (fdp->fd_ofiles[old] != fp) {
  509                 if (fdp->fd_ofiles[new] == NULL) {
  510                         if (new < fdp->fd_freefile)
  511                                 fdp->fd_freefile = new;
  512                         while (fdp->fd_lastfile > 0 &&
  513                             fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
  514                                 fdp->fd_lastfile--;
  515                 }
  516                 FILEDESC_UNLOCK(fdp);
  517                 fdrop(fp, td);
  518                 return (EBADF);
  519         }
  520         KASSERT(old != new, ("new fd is same as old"));
  521 
  522         /*
  523          * Save info on the descriptor being overwritten.  We have
  524          * to do the unmap now, but we cannot close it without
  525          * introducing an ownership race for the slot.
  526          */
  527         delfp = fdp->fd_ofiles[new];
  528         KASSERT(delfp == NULL || type == DUP_FIXED,
  529             ("dup() picked an open file"));
  530 #if 0
  531         if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED))
  532                 (void) munmapfd(td, new);
  533 #endif
  534 
  535         /*
  536          * Duplicate the source descriptor, update lastfile
  537          */
  538         fdp->fd_ofiles[new] = fp;
  539         fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
  540         if (new > fdp->fd_lastfile)
  541                 fdp->fd_lastfile = new;
  542         FILEDESC_UNLOCK(fdp);
  543         *retval = new;
  544 
  545         /*
  546          * If we dup'd over a valid file, we now own the reference to it
  547          * and must dispose of it using closef() semantics (as if a
  548          * close() were performed on it).
  549          */
  550         if (delfp) {
  551                 mtx_lock(&Giant);
  552                 (void) closef(delfp, td);
  553                 mtx_unlock(&Giant);
  554         }
  555         return (0);
  556 }
  557 
  558 /*
  559  * If sigio is on the list associated with a process or process group,
  560  * disable signalling from the device, remove sigio from the list and
  561  * free sigio.
  562  */
  563 void
  564 funsetown(sigiop)
  565         struct sigio **sigiop;
  566 {
  567         struct sigio *sigio;
  568 
  569         SIGIO_LOCK();
  570         sigio = *sigiop;
  571         if (sigio == NULL) {
  572                 SIGIO_UNLOCK();
  573                 return;
  574         }
  575         *(sigio->sio_myref) = NULL;
  576         if ((sigio)->sio_pgid < 0) {
  577                 struct pgrp *pg = (sigio)->sio_pgrp;
  578                 PGRP_LOCK(pg);
  579                 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
  580                              sigio, sio_pgsigio);
  581                 PGRP_UNLOCK(pg);
  582         } else {
  583                 struct proc *p = (sigio)->sio_proc;
  584                 PROC_LOCK(p);
  585                 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
  586                              sigio, sio_pgsigio);
  587                 PROC_UNLOCK(p);
  588         }
  589         SIGIO_UNLOCK();
  590         crfree(sigio->sio_ucred);
  591         FREE(sigio, M_SIGIO);
  592 }
  593 
  594 /*
  595  * Free a list of sigio structures.
  596  * We only need to lock the SIGIO_LOCK because we have made ourselves
  597  * inaccessable to callers of fsetown and therefore do not need to lock
  598  * the proc or pgrp struct for the list manipulation.
  599  */
  600 void
  601 funsetownlst(sigiolst)
  602         struct sigiolst *sigiolst;
  603 {
  604         struct proc *p;
  605         struct pgrp *pg;
  606         struct sigio *sigio;
  607 
  608         sigio = SLIST_FIRST(sigiolst);
  609         if (sigio == NULL)
  610                 return;
  611         p = NULL;
  612         pg = NULL;
  613 
  614         /*
  615          * Every entry of the list should belong
  616          * to a single proc or pgrp.
  617          */
  618         if (sigio->sio_pgid < 0) {
  619                 pg = sigio->sio_pgrp;
  620                 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
  621         } else /* if (sigio->sio_pgid > 0) */ {
  622                 p = sigio->sio_proc;
  623                 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  624         }
  625 
  626         SIGIO_LOCK();
  627         while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
  628                 *(sigio->sio_myref) = NULL;
  629                 if (pg != NULL) {
  630                         KASSERT(sigio->sio_pgid < 0,
  631                             ("Proc sigio in pgrp sigio list"));
  632                         KASSERT(sigio->sio_pgrp == pg,
  633                             ("Bogus pgrp in sigio list"));
  634                         PGRP_LOCK(pg);
  635                         SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
  636                             sio_pgsigio);
  637                         PGRP_UNLOCK(pg);
  638                 } else /* if (p != NULL) */ {
  639                         KASSERT(sigio->sio_pgid > 0,
  640                             ("Pgrp sigio in proc sigio list"));
  641                         KASSERT(sigio->sio_proc == p,
  642                             ("Bogus proc in sigio list"));
  643                         PROC_LOCK(p);
  644                         SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
  645                             sio_pgsigio);
  646                         PROC_UNLOCK(p);
  647                 }
  648                 SIGIO_UNLOCK();
  649                 crfree(sigio->sio_ucred);
  650                 FREE(sigio, M_SIGIO);
  651                 SIGIO_LOCK();
  652         }
  653         SIGIO_UNLOCK();
  654 }
  655 
  656 /*
  657  * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
  658  *
  659  * After permission checking, add a sigio structure to the sigio list for
  660  * the process or process group.
  661  */
  662 int
  663 fsetown(pgid, sigiop)
  664         pid_t pgid;
  665         struct sigio **sigiop;
  666 {
  667         struct proc *proc;
  668         struct pgrp *pgrp;
  669         struct sigio *sigio;
  670         int ret;
  671 
  672         if (pgid == 0) {
  673                 funsetown(sigiop);
  674                 return (0);
  675         }
  676 
  677         ret = 0;
  678 
  679         /* Allocate and fill in the new sigio out of locks. */
  680         MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
  681         sigio->sio_pgid = pgid;
  682         sigio->sio_ucred = crhold(curthread->td_ucred);
  683         sigio->sio_myref = sigiop;
  684 
  685         sx_slock(&proctree_lock);
  686         if (pgid > 0) {
  687                 proc = pfind(pgid);
  688                 if (proc == NULL) {
  689                         ret = ESRCH;
  690                         goto fail;
  691                 }
  692 
  693                 /*
  694                  * Policy - Don't allow a process to FSETOWN a process
  695                  * in another session.
  696                  *
  697                  * Remove this test to allow maximum flexibility or
  698                  * restrict FSETOWN to the current process or process
  699                  * group for maximum safety.
  700                  */
  701                 PROC_UNLOCK(proc);
  702                 if (proc->p_session != curthread->td_proc->p_session) {
  703                         ret = EPERM;
  704                         goto fail;
  705                 }
  706 
  707                 pgrp = NULL;
  708         } else /* if (pgid < 0) */ {
  709                 pgrp = pgfind(-pgid);
  710                 if (pgrp == NULL) {
  711                         ret = ESRCH;
  712                         goto fail;
  713                 }
  714                 PGRP_UNLOCK(pgrp);
  715 
  716                 /*
  717                  * Policy - Don't allow a process to FSETOWN a process
  718                  * in another session.
  719                  *
  720                  * Remove this test to allow maximum flexibility or
  721                  * restrict FSETOWN to the current process or process
  722                  * group for maximum safety.
  723                  */
  724                 if (pgrp->pg_session != curthread->td_proc->p_session) {
  725                         ret = EPERM;
  726                         goto fail;
  727                 }
  728 
  729                 proc = NULL;
  730         }
  731         funsetown(sigiop);
  732         if (pgid > 0) {
  733                 PROC_LOCK(proc);
  734                 /* 
  735                  * Since funsetownlst() is called without the proctree
  736                  * locked, we need to check for P_WEXIT.
  737                  * XXX: is ESRCH correct?
  738                  */
  739                 if ((proc->p_flag & P_WEXIT) != 0) {
  740                         PROC_UNLOCK(proc);
  741                         ret = ESRCH;
  742                         goto fail;
  743                 }
  744                 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
  745                 sigio->sio_proc = proc;
  746                 PROC_UNLOCK(proc);
  747         } else {
  748                 PGRP_LOCK(pgrp);
  749                 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
  750                 sigio->sio_pgrp = pgrp;
  751                 PGRP_UNLOCK(pgrp);
  752         }
  753         sx_sunlock(&proctree_lock);
  754         SIGIO_LOCK();
  755         *sigiop = sigio;
  756         SIGIO_UNLOCK();
  757         return (0);
  758 
  759 fail:
  760         sx_sunlock(&proctree_lock);
  761         crfree(sigio->sio_ucred);
  762         FREE(sigio, M_SIGIO);
  763         return (ret);
  764 }
  765 
  766 /*
  767  * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
  768  */
  769 pid_t
  770 fgetown(sigiop)
  771         struct sigio **sigiop;
  772 {
  773         pid_t pgid;
  774 
  775         SIGIO_LOCK();
  776         pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
  777         SIGIO_UNLOCK();
  778         return (pgid);
  779 }
  780 
  781 /*
  782  * Close a file descriptor.
  783  */
  784 #ifndef _SYS_SYSPROTO_H_
  785 struct close_args {
  786         int     fd;
  787 };
  788 #endif
  789 /*
  790  * MPSAFE
  791  */
  792 /* ARGSUSED */
  793 int
  794 close(td, uap)
  795         struct thread *td;
  796         struct close_args *uap;
  797 {
  798         register struct filedesc *fdp;
  799         register struct file *fp;
  800         int fd, error;
  801 
  802         fd = uap->fd;
  803         error = 0;
  804         fdp = td->td_proc->p_fd;
  805         mtx_lock(&Giant);
  806         FILEDESC_LOCK(fdp);
  807         if ((unsigned)fd >= fdp->fd_nfiles ||
  808             (fp = fdp->fd_ofiles[fd]) == NULL) {
  809                 FILEDESC_UNLOCK(fdp);
  810                 error = EBADF;
  811                 goto done2;
  812         }
  813 #if 0
  814         if (fdp->fd_ofileflags[fd] & UF_MAPPED)
  815                 (void) munmapfd(td, fd);
  816 #endif
  817         fdp->fd_ofiles[fd] = NULL;
  818         fdp->fd_ofileflags[fd] = 0;
  819 
  820         /*
  821          * we now hold the fp reference that used to be owned by the descriptor
  822          * array.
  823          */
  824         while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
  825                 fdp->fd_lastfile--;
  826         if (fd < fdp->fd_freefile)
  827                 fdp->fd_freefile = fd;
  828         if (fd < fdp->fd_knlistsize) {
  829                 FILEDESC_UNLOCK(fdp);
  830                 knote_fdclose(td, fd);
  831         } else
  832                 FILEDESC_UNLOCK(fdp);
  833 
  834         error = closef(fp, td);
  835 done2:
  836         mtx_unlock(&Giant);
  837         return (error);
  838 }
  839 
  840 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
  841 /*
  842  * Return status information about a file descriptor.
  843  */
  844 #ifndef _SYS_SYSPROTO_H_
  845 struct ofstat_args {
  846         int     fd;
  847         struct  ostat *sb;
  848 };
  849 #endif
  850 /*
  851  * MPSAFE
  852  */
  853 /* ARGSUSED */
  854 int
  855 ofstat(td, uap)
  856         struct thread *td;
  857         register struct ofstat_args *uap;
  858 {
  859         struct file *fp;
  860         struct stat ub;
  861         struct ostat oub;
  862         int error;
  863 
  864         mtx_lock(&Giant);
  865         if ((error = fget(td, uap->fd, &fp)) != 0)
  866                 goto done2;
  867         error = fo_stat(fp, &ub, td->td_ucred, td);
  868         if (error == 0) {
  869                 cvtstat(&ub, &oub);
  870                 error = copyout(&oub, uap->sb, sizeof(oub));
  871         }
  872         fdrop(fp, td);
  873 done2:
  874         mtx_unlock(&Giant);
  875         return (error);
  876 }
  877 #endif /* COMPAT_43 || COMPAT_SUNOS */
  878 
  879 /*
  880  * Return status information about a file descriptor.
  881  */
  882 #ifndef _SYS_SYSPROTO_H_
  883 struct fstat_args {
  884         int     fd;
  885         struct  stat *sb;
  886 };
  887 #endif
  888 /*
  889  * MPSAFE
  890  */
  891 /* ARGSUSED */
  892 int
  893 fstat(td, uap)
  894         struct thread *td;
  895         struct fstat_args *uap;
  896 {
  897         struct file *fp;
  898         struct stat ub;
  899         int error;
  900 
  901         mtx_lock(&Giant);
  902         if ((error = fget(td, uap->fd, &fp)) != 0)
  903                 goto done2;
  904         error = fo_stat(fp, &ub, td->td_ucred, td);
  905         if (error == 0)
  906                 error = copyout(&ub, uap->sb, sizeof(ub));
  907         fdrop(fp, td);
  908 done2:
  909         mtx_unlock(&Giant);
  910         return (error);
  911 }
  912 
  913 /*
  914  * Return status information about a file descriptor.
  915  */
  916 #ifndef _SYS_SYSPROTO_H_
  917 struct nfstat_args {
  918         int     fd;
  919         struct  nstat *sb;
  920 };
  921 #endif
  922 /*
  923  * MPSAFE
  924  */
  925 /* ARGSUSED */
  926 int
  927 nfstat(td, uap)
  928         struct thread *td;
  929         register struct nfstat_args *uap;
  930 {
  931         struct file *fp;
  932         struct stat ub;
  933         struct nstat nub;
  934         int error;
  935 
  936         mtx_lock(&Giant);
  937         if ((error = fget(td, uap->fd, &fp)) != 0)
  938                 goto done2;
  939         error = fo_stat(fp, &ub, td->td_ucred, td);
  940         if (error == 0) {
  941                 cvtnstat(&ub, &nub);
  942                 error = copyout(&nub, uap->sb, sizeof(nub));
  943         }
  944         fdrop(fp, td);
  945 done2:
  946         mtx_unlock(&Giant);
  947         return (error);
  948 }
  949 
  950 /*
  951  * Return pathconf information about a file descriptor.
  952  */
  953 #ifndef _SYS_SYSPROTO_H_
  954 struct fpathconf_args {
  955         int     fd;
  956         int     name;
  957 };
  958 #endif
  959 /*
  960  * MPSAFE
  961  */
  962 /* ARGSUSED */
  963 int
  964 fpathconf(td, uap)
  965         struct thread *td;
  966         register struct fpathconf_args *uap;
  967 {
  968         struct file *fp;
  969         struct vnode *vp;
  970         int error;
  971 
  972         if ((error = fget(td, uap->fd, &fp)) != 0)
  973                 return (error);
  974 
  975         /* If asynchronous I/O is available, it works for all descriptors. */
  976         if (uap->name == _PC_ASYNC_IO) {
  977                 td->td_retval[0] = async_io_version;
  978                 goto out;
  979         }
  980         switch (fp->f_type) {
  981         case DTYPE_PIPE:
  982         case DTYPE_SOCKET:
  983                 if (uap->name != _PC_PIPE_BUF) {
  984                         error = EINVAL;
  985                 } else {
  986                         td->td_retval[0] = PIPE_BUF;
  987                         error = 0;
  988                 }
  989                 break;
  990         case DTYPE_FIFO:
  991         case DTYPE_VNODE:
  992                 vp = (struct vnode *)fp->f_data;
  993                 mtx_lock(&Giant);
  994                 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
  995                 mtx_unlock(&Giant);
  996                 break;
  997         default:
  998                 error = EOPNOTSUPP;
  999                 break;
 1000         }
 1001 out:
 1002         fdrop(fp, td);
 1003         return (error);
 1004 }
 1005 
 1006 /*
 1007  * Allocate a file descriptor for the process.
 1008  */
 1009 static int fdexpand;
 1010 SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
 1011 
 1012 int
 1013 fdalloc(td, want, result)
 1014         struct thread *td;
 1015         int want;
 1016         int *result;
 1017 {
 1018         struct proc *p = td->td_proc;
 1019         register struct filedesc *fdp = td->td_proc->p_fd;
 1020         register int i;
 1021         int lim, last, nfiles;
 1022         struct file **newofile, **oldofile;
 1023         char *newofileflags;
 1024 
 1025         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
 1026 
 1027         /*
 1028          * Search for a free descriptor starting at the higher
 1029          * of want or fd_freefile.  If that fails, consider
 1030          * expanding the ofile array.
 1031          */
 1032         lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
 1033         for (;;) {
 1034                 last = min(fdp->fd_nfiles, lim);
 1035                 i = max(want, fdp->fd_freefile);
 1036                 for (; i < last; i++) {
 1037                         if (fdp->fd_ofiles[i] == NULL) {
 1038                                 fdp->fd_ofileflags[i] = 0;
 1039                                 if (i > fdp->fd_lastfile)
 1040                                         fdp->fd_lastfile = i;
 1041                                 if (want <= fdp->fd_freefile)
 1042                                         fdp->fd_freefile = i;
 1043                                 *result = i;
 1044                                 return (0);
 1045                         }
 1046                 }
 1047 
 1048                 /*
 1049                  * No space in current array.  Expand?
 1050                  */
 1051                 if (i >= lim)
 1052                         return (EMFILE);
 1053                 if (fdp->fd_nfiles < NDEXTENT)
 1054                         nfiles = NDEXTENT;
 1055                 else
 1056                         nfiles = 2 * fdp->fd_nfiles;
 1057                 while (nfiles < want)
 1058                         nfiles <<= 1;
 1059                 FILEDESC_UNLOCK(fdp);
 1060                 /*
 1061                  * XXX malloc() calls uma_large_malloc() for sizes larger
 1062                  * than KMEM_ZMAX bytes. uma_large_malloc() requires Giant.
 1063                  */
 1064                 mtx_lock(&Giant);
 1065                 newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
 1066                 mtx_unlock(&Giant);
 1067 
 1068                 /*
 1069                  * Deal with file-table extend race that might have
 1070                  * occurred while filedesc was unlocked.
 1071                  */
 1072                 FILEDESC_LOCK(fdp);
 1073                 if (fdp->fd_nfiles >= nfiles) {
 1074                         /* XXX uma_large_free() needs Giant. */
 1075                         FILEDESC_UNLOCK(fdp);
 1076                         mtx_lock(&Giant);
 1077                         free(newofile, M_FILEDESC);
 1078                         mtx_unlock(&Giant);
 1079                         FILEDESC_LOCK(fdp);
 1080                         continue;
 1081                 }
 1082                 newofileflags = (char *) &newofile[nfiles];
 1083                 /*
 1084                  * Copy the existing ofile and ofileflags arrays
 1085                  * and zero the new portion of each array.
 1086                  */
 1087                 i = fdp->fd_nfiles * sizeof(struct file *);
 1088                 bcopy(fdp->fd_ofiles, newofile, i);
 1089                 bzero((char *)newofile + i,
 1090                     nfiles * sizeof(struct file *) - i);
 1091                 i = fdp->fd_nfiles * sizeof(char);
 1092                 bcopy(fdp->fd_ofileflags, newofileflags, i);
 1093                 bzero(newofileflags + i, nfiles * sizeof(char) - i);
 1094                 if (fdp->fd_nfiles > NDFILE)
 1095                         oldofile = fdp->fd_ofiles;
 1096                 else
 1097                         oldofile = NULL;
 1098                 fdp->fd_ofiles = newofile;
 1099                 fdp->fd_ofileflags = newofileflags;
 1100                 fdp->fd_nfiles = nfiles;
 1101                 fdexpand++;
 1102                 if (oldofile != NULL) {
 1103                         /* XXX uma_large_free() needs Giant. */
 1104                         FILEDESC_UNLOCK(fdp);
 1105                         mtx_lock(&Giant);
 1106                         free(oldofile, M_FILEDESC);
 1107                         mtx_unlock(&Giant);
 1108                         FILEDESC_LOCK(fdp);
 1109                 }
 1110         }
 1111         return (0);
 1112 }
 1113 
 1114 /*
 1115  * Check to see whether n user file descriptors
 1116  * are available to the process p.
 1117  */
 1118 int
 1119 fdavail(td, n)
 1120         struct thread *td;
 1121         register int n;
 1122 {
 1123         struct proc *p = td->td_proc;
 1124         register struct filedesc *fdp = td->td_proc->p_fd;
 1125         register struct file **fpp;
 1126         register int i, lim, last;
 1127 
 1128         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
 1129 
 1130         lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
 1131         if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
 1132                 return (1);
 1133         last = min(fdp->fd_nfiles, lim);
 1134         fpp = &fdp->fd_ofiles[fdp->fd_freefile];
 1135         for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
 1136                 if (*fpp == NULL && --n <= 0)
 1137                         return (1);
 1138         }
 1139         return (0);
 1140 }
 1141 
 1142 /*
 1143  * Create a new open file structure and allocate
 1144  * a file decriptor for the process that refers to it.
 1145  */
 1146 int
 1147 falloc(td, resultfp, resultfd)
 1148         register struct thread *td;
 1149         struct file **resultfp;
 1150         int *resultfd;
 1151 {
 1152         struct proc *p = td->td_proc;
 1153         register struct file *fp, *fq;
 1154         int error, i;
 1155 
 1156         fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
 1157         sx_xlock(&filelist_lock);
 1158         if (nfiles >= maxfiles) {
 1159                 sx_xunlock(&filelist_lock);
 1160                 uma_zfree(file_zone, fp);
 1161                 tablefull("file");
 1162                 return (ENFILE);
 1163         }
 1164         nfiles++;
 1165 
 1166         /*
 1167          * If the process has file descriptor zero open, add the new file
 1168          * descriptor to the list of open files at that point, otherwise
 1169          * put it at the front of the list of open files.
 1170          */
 1171         fp->f_mtxp = mtx_pool_alloc();
 1172         fp->f_gcflag = 0;
 1173         fp->f_count = 1;
 1174         fp->f_cred = crhold(td->td_ucred);
 1175         fp->f_ops = &badfileops;
 1176         fp->f_seqcount = 1;
 1177         FILEDESC_LOCK(p->p_fd);
 1178         if ((fq = p->p_fd->fd_ofiles[0])) {
 1179                 LIST_INSERT_AFTER(fq, fp, f_list);
 1180         } else {
 1181                 LIST_INSERT_HEAD(&filehead, fp, f_list);
 1182         }
 1183         sx_xunlock(&filelist_lock);
 1184         if ((error = fdalloc(td, 0, &i))) {
 1185                 FILEDESC_UNLOCK(p->p_fd);
 1186                 fdrop(fp, td);
 1187                 return (error);
 1188         }
 1189         p->p_fd->fd_ofiles[i] = fp;
 1190         FILEDESC_UNLOCK(p->p_fd);
 1191         if (resultfp)
 1192                 *resultfp = fp;
 1193         if (resultfd)
 1194                 *resultfd = i;
 1195         return (0);
 1196 }
 1197 
 1198 /*
 1199  * Free a file descriptor.
 1200  */
 1201 void
 1202 ffree(fp)
 1203         register struct file *fp;
 1204 {
 1205 
 1206         KASSERT(fp->f_count == 0, ("ffree: fp_fcount not 0!"));
 1207         sx_xlock(&filelist_lock);
 1208         LIST_REMOVE(fp, f_list);
 1209         nfiles--;
 1210         sx_xunlock(&filelist_lock);
 1211         crfree(fp->f_cred);
 1212         uma_zfree(file_zone, fp);
 1213 }
 1214 
 1215 /*
 1216  * Build a new filedesc structure.
 1217  */
 1218 struct filedesc *
 1219 fdinit(td)
 1220         struct thread *td;
 1221 {
 1222         register struct filedesc0 *newfdp;
 1223         register struct filedesc *fdp = td->td_proc->p_fd;
 1224 
 1225         MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
 1226             M_FILEDESC, M_WAITOK | M_ZERO);
 1227         mtx_init(&newfdp->fd_fd.fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
 1228         FILEDESC_LOCK(&newfdp->fd_fd);
 1229         newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
 1230         if (newfdp->fd_fd.fd_cdir)
 1231                 VREF(newfdp->fd_fd.fd_cdir);
 1232         newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
 1233         if (newfdp->fd_fd.fd_rdir)
 1234                 VREF(newfdp->fd_fd.fd_rdir);
 1235         newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
 1236         if (newfdp->fd_fd.fd_jdir)
 1237                 VREF(newfdp->fd_fd.fd_jdir);
 1238 
 1239         /* Create the file descriptor table. */
 1240         newfdp->fd_fd.fd_refcnt = 1;
 1241         newfdp->fd_fd.fd_cmask = cmask;
 1242         newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
 1243         newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
 1244         newfdp->fd_fd.fd_nfiles = NDFILE;
 1245         newfdp->fd_fd.fd_knlistsize = -1;
 1246         FILEDESC_UNLOCK(&newfdp->fd_fd);
 1247 
 1248         return (&newfdp->fd_fd);
 1249 }
 1250 
 1251 /*
 1252  * Share a filedesc structure.
 1253  */
 1254 struct filedesc *
 1255 fdshare(p)
 1256         struct proc *p;
 1257 {
 1258         FILEDESC_LOCK(p->p_fd);
 1259         p->p_fd->fd_refcnt++;
 1260         FILEDESC_UNLOCK(p->p_fd);
 1261         return (p->p_fd);
 1262 }
 1263 
 1264 /*
 1265  * Copy a filedesc structure.
 1266  */
 1267 struct filedesc *
 1268 fdcopy(td)
 1269         struct thread *td;
 1270 {
 1271         register struct filedesc *newfdp, *fdp;
 1272         register struct file **fpp;
 1273         register int i, j;
 1274 
 1275         /* Certain daemons might not have file descriptors. */
 1276         fdp = td->td_proc->p_fd;
 1277         if (fdp == NULL)
 1278                 return (NULL);
 1279 
 1280         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
 1281 
 1282         FILEDESC_UNLOCK(fdp);
 1283         MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
 1284             M_FILEDESC, M_WAITOK);
 1285         FILEDESC_LOCK(fdp);
 1286         bcopy(fdp, newfdp, sizeof(struct filedesc));
 1287         FILEDESC_UNLOCK(fdp);
 1288         bzero(&newfdp->fd_mtx, sizeof(newfdp->fd_mtx));
 1289         mtx_init(&newfdp->fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
 1290         if (newfdp->fd_cdir)
 1291                 VREF(newfdp->fd_cdir);
 1292         if (newfdp->fd_rdir)
 1293                 VREF(newfdp->fd_rdir);
 1294         if (newfdp->fd_jdir)
 1295                 VREF(newfdp->fd_jdir);
 1296         newfdp->fd_refcnt = 1;
 1297 
 1298         /*
 1299          * If the number of open files fits in the internal arrays
 1300          * of the open file structure, use them, otherwise allocate
 1301          * additional memory for the number of descriptors currently
 1302          * in use.
 1303          */
 1304         FILEDESC_LOCK(fdp);
 1305         newfdp->fd_lastfile = fdp->fd_lastfile;
 1306         newfdp->fd_nfiles = fdp->fd_nfiles;
 1307         if (newfdp->fd_lastfile < NDFILE) {
 1308                 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
 1309                 newfdp->fd_ofileflags =
 1310                     ((struct filedesc0 *) newfdp)->fd_dfileflags;
 1311                 i = NDFILE;
 1312         } else {
 1313                 /*
 1314                  * Compute the smallest multiple of NDEXTENT needed
 1315                  * for the file descriptors currently in use,
 1316                  * allowing the table to shrink.
 1317                  */
 1318 retry:
 1319                 i = newfdp->fd_nfiles;
 1320                 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
 1321                         i /= 2;
 1322                 FILEDESC_UNLOCK(fdp);
 1323                 MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
 1324                     M_FILEDESC, M_WAITOK);
 1325                 FILEDESC_LOCK(fdp);
 1326                 newfdp->fd_lastfile = fdp->fd_lastfile;
 1327                 newfdp->fd_nfiles = fdp->fd_nfiles;
 1328                 j = newfdp->fd_nfiles;
 1329                 while (j > 2 * NDEXTENT && j > newfdp->fd_lastfile * 2)
 1330                         j /= 2;
 1331                 if (i != j) {
 1332                         /*
 1333                          * The size of the original table has changed.
 1334                          * Go over once again.
 1335                          */
 1336                         FILEDESC_UNLOCK(fdp);
 1337                         FREE(newfdp->fd_ofiles, M_FILEDESC);
 1338                         FILEDESC_LOCK(fdp);
 1339                         newfdp->fd_lastfile = fdp->fd_lastfile;
 1340                         newfdp->fd_nfiles = fdp->fd_nfiles;
 1341                         goto retry;
 1342                 }
 1343                 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
 1344         }
 1345         newfdp->fd_nfiles = i;
 1346         bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
 1347         bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
 1348 
 1349         /*
 1350          * kq descriptors cannot be copied.
 1351          */
 1352         if (newfdp->fd_knlistsize != -1) {
 1353                 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
 1354                 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
 1355                         if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
 1356                                 *fpp = NULL;
 1357                                 if (i < newfdp->fd_freefile)
 1358                                         newfdp->fd_freefile = i;
 1359                         }
 1360                         if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
 1361                                 newfdp->fd_lastfile--;
 1362                 }
 1363                 newfdp->fd_knlist = NULL;
 1364                 newfdp->fd_knlistsize = -1;
 1365                 newfdp->fd_knhash = NULL;
 1366                 newfdp->fd_knhashmask = 0;
 1367         }
 1368 
 1369         fpp = newfdp->fd_ofiles;
 1370         for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
 1371                 if (*fpp != NULL)
 1372                         fhold(*fpp);
 1373         }
 1374         return (newfdp);
 1375 }
 1376 
 1377 /*
 1378  * Release a filedesc structure.
 1379  */
 1380 void
 1381 fdfree(td)
 1382         struct thread *td;
 1383 {
 1384         register struct filedesc *fdp;
 1385         struct file **fpp;
 1386         register int i;
 1387 
 1388         /* Certain daemons might not have file descriptors. */
 1389         fdp = td->td_proc->p_fd;
 1390         if (fdp == NULL)
 1391                 return;
 1392 
 1393         FILEDESC_LOCK(fdp);
 1394         if (--fdp->fd_refcnt > 0) {
 1395                 FILEDESC_UNLOCK(fdp);
 1396                 return;
 1397         }
 1398 
 1399         /*
 1400          * We are the last reference to the structure, so we can
 1401          * safely assume it will not change out from under us.
 1402          */
 1403         FILEDESC_UNLOCK(fdp);
 1404         fpp = fdp->fd_ofiles;
 1405         for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
 1406                 if (*fpp)
 1407                         (void) closef(*fpp, td);
 1408         }
 1409         td->td_proc->p_fd = NULL;
 1410         if (fdp->fd_nfiles > NDFILE)
 1411                 FREE(fdp->fd_ofiles, M_FILEDESC);
 1412         if (fdp->fd_cdir)
 1413                 vrele(fdp->fd_cdir);
 1414         if (fdp->fd_rdir)
 1415                 vrele(fdp->fd_rdir);
 1416         if (fdp->fd_jdir)
 1417                 vrele(fdp->fd_jdir);
 1418         if (fdp->fd_knlist)
 1419                 FREE(fdp->fd_knlist, M_KQUEUE);
 1420         if (fdp->fd_knhash)
 1421                 FREE(fdp->fd_knhash, M_KQUEUE);
 1422         mtx_destroy(&fdp->fd_mtx);
 1423         FREE(fdp, M_FILEDESC);
 1424 }
 1425 
 1426 /*
 1427  * For setugid programs, we don't want to people to use that setugidness
 1428  * to generate error messages which write to a file which otherwise would
 1429  * otherwise be off-limits to the process.  We check for filesystems where
 1430  * the vnode can change out from under us after execve (like [lin]procfs).
 1431  *
 1432  * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
 1433  * sufficient.  We also don't for check setugidness since we know we are.
 1434  */
 1435 static int
 1436 is_unsafe(struct file *fp)
 1437 {
 1438         if (fp->f_type == DTYPE_VNODE) {
 1439                 struct vnode *vp = (struct vnode *)fp->f_data;
 1440 
 1441                 if ((vp->v_vflag & VV_PROCDEP) != 0)
 1442                         return (1);
 1443         }
 1444         return (0);
 1445 }
 1446 
 1447 /*
 1448  * Make this setguid thing safe, if at all possible.
 1449  */
 1450 void
 1451 setugidsafety(td)
 1452         struct thread *td;
 1453 {
 1454         struct filedesc *fdp;
 1455         register int i;
 1456 
 1457         /* Certain daemons might not have file descriptors. */
 1458         fdp = td->td_proc->p_fd;
 1459         if (fdp == NULL)
 1460                 return;
 1461 
 1462         /*
 1463          * Note: fdp->fd_ofiles may be reallocated out from under us while
 1464          * we are blocked in a close.  Be careful!
 1465          */
 1466         FILEDESC_LOCK(fdp);
 1467         for (i = 0; i <= fdp->fd_lastfile; i++) {
 1468                 if (i > 2)
 1469                         break;
 1470                 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
 1471                         struct file *fp;
 1472 
 1473 #if 0
 1474                         if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
 1475                                 (void) munmapfd(td, i);
 1476 #endif
 1477                         if (i < fdp->fd_knlistsize) {
 1478                                 FILEDESC_UNLOCK(fdp);
 1479                                 knote_fdclose(td, i);
 1480                                 FILEDESC_LOCK(fdp);
 1481                         }
 1482                         /*
 1483                          * NULL-out descriptor prior to close to avoid
 1484                          * a race while close blocks.
 1485                          */
 1486                         fp = fdp->fd_ofiles[i];
 1487                         fdp->fd_ofiles[i] = NULL;
 1488                         fdp->fd_ofileflags[i] = 0;
 1489                         if (i < fdp->fd_freefile)
 1490                                 fdp->fd_freefile = i;
 1491                         FILEDESC_UNLOCK(fdp);
 1492                         (void) closef(fp, td);
 1493                         FILEDESC_LOCK(fdp);
 1494                 }
 1495         }
 1496         while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
 1497                 fdp->fd_lastfile--;
 1498         FILEDESC_UNLOCK(fdp);
 1499 }
 1500 
 1501 /*
 1502  * Close any files on exec?
 1503  */
 1504 void
 1505 fdcloseexec(td)
 1506         struct thread *td;
 1507 {
 1508         struct filedesc *fdp;
 1509         register int i;
 1510 
 1511         /* Certain daemons might not have file descriptors. */
 1512         fdp = td->td_proc->p_fd;
 1513         if (fdp == NULL)
 1514                 return;
 1515 
 1516         FILEDESC_LOCK(fdp);
 1517 
 1518         /*
 1519          * We cannot cache fd_ofiles or fd_ofileflags since operations
 1520          * may block and rip them out from under us.
 1521          */
 1522         for (i = 0; i <= fdp->fd_lastfile; i++) {
 1523                 if (fdp->fd_ofiles[i] != NULL &&
 1524                     (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
 1525                         struct file *fp;
 1526 
 1527 #if 0
 1528                         if (fdp->fd_ofileflags[i] & UF_MAPPED)
 1529                                 (void) munmapfd(td, i);
 1530 #endif
 1531                         if (i < fdp->fd_knlistsize) {
 1532                                 FILEDESC_UNLOCK(fdp);
 1533                                 knote_fdclose(td, i);
 1534                                 FILEDESC_LOCK(fdp);
 1535                         }
 1536                         /*
 1537                          * NULL-out descriptor prior to close to avoid
 1538                          * a race while close blocks.
 1539                          */
 1540                         fp = fdp->fd_ofiles[i];
 1541                         fdp->fd_ofiles[i] = NULL;
 1542                         fdp->fd_ofileflags[i] = 0;
 1543                         if (i < fdp->fd_freefile)
 1544                                 fdp->fd_freefile = i;
 1545                         FILEDESC_UNLOCK(fdp);
 1546                         (void) closef(fp, td);
 1547                         FILEDESC_LOCK(fdp);
 1548                 }
 1549         }
 1550         while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
 1551                 fdp->fd_lastfile--;
 1552         FILEDESC_UNLOCK(fdp);
 1553 }
 1554 
 1555 /*
 1556  * It is unsafe for set[ug]id processes to be started with file
 1557  * descriptors 0..2 closed, as these descriptors are given implicit
 1558  * significance in the Standard C library.  fdcheckstd() will create a
 1559  * descriptor referencing /dev/null for each of stdin, stdout, and
 1560  * stderr that is not already open.
 1561  */
 1562 int
 1563 fdcheckstd(td)
 1564         struct thread *td;
 1565 {
 1566         struct nameidata nd;
 1567         struct filedesc *fdp;
 1568         struct file *fp;
 1569         register_t retval;
 1570         int fd, i, error, flags, devnull;
 1571 
 1572         fdp = td->td_proc->p_fd;
 1573         if (fdp == NULL)
 1574                 return (0);
 1575         devnull = -1;
 1576         error = 0;
 1577         for (i = 0; i < 3; i++) {
 1578                 if (fdp->fd_ofiles[i] != NULL)
 1579                         continue;
 1580                 if (devnull < 0) {
 1581                         error = falloc(td, &fp, &fd);
 1582                         if (error != 0)
 1583                                 break;
 1584                         KASSERT(fd == i, ("oof, we didn't get our fd"));
 1585                         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
 1586                             td);
 1587                         flags = FREAD | FWRITE;
 1588                         error = vn_open(&nd, &flags, 0);
 1589                         if (error != 0) {
 1590                                 FILEDESC_LOCK(fdp);
 1591                                 fdp->fd_ofiles[fd] = NULL;
 1592                                 FILEDESC_UNLOCK(fdp);
 1593                                 fdrop(fp, td);
 1594                                 break;
 1595                         }
 1596                         NDFREE(&nd, NDF_ONLY_PNBUF);
 1597                         fp->f_data = nd.ni_vp;
 1598                         fp->f_flag = flags;
 1599                         fp->f_ops = &vnops;
 1600                         fp->f_type = DTYPE_VNODE;
 1601                         VOP_UNLOCK(nd.ni_vp, 0, td);
 1602                         devnull = fd;
 1603                 } else {
 1604                         error = do_dup(td, DUP_FIXED, devnull, i, &retval);
 1605                         if (error != 0)
 1606                                 break;
 1607                 }
 1608         }
 1609         return (error);
 1610 }
 1611 
 1612 /*
 1613  * Internal form of close.
 1614  * Decrement reference count on file structure.
 1615  * Note: td may be NULL when closing a file
 1616  * that was being passed in a message.
 1617  */
 1618 int
 1619 closef(fp, td)
 1620         register struct file *fp;
 1621         register struct thread *td;
 1622 {
 1623         struct vnode *vp;
 1624         struct flock lf;
 1625 
 1626         if (fp == NULL)
 1627                 return (0);
 1628         /*
 1629          * POSIX record locking dictates that any close releases ALL
 1630          * locks owned by this process.  This is handled by setting
 1631          * a flag in the unlock to free ONLY locks obeying POSIX
 1632          * semantics, and not to free BSD-style file locks.
 1633          * If the descriptor was in a message, POSIX-style locks
 1634          * aren't passed with the descriptor.
 1635          */
 1636         if (td && (td->td_proc->p_flag & P_ADVLOCK) &&
 1637             fp->f_type == DTYPE_VNODE) {
 1638                 lf.l_whence = SEEK_SET;
 1639                 lf.l_start = 0;
 1640                 lf.l_len = 0;
 1641                 lf.l_type = F_UNLCK;
 1642                 vp = (struct vnode *)fp->f_data;
 1643                 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
 1644                     F_UNLCK, &lf, F_POSIX);
 1645         }
 1646         return (fdrop(fp, td));
 1647 }
 1648 
 1649 /*
 1650  * Drop reference on struct file passed in, may call closef if the
 1651  * reference hits zero.
 1652  */
 1653 int
 1654 fdrop(fp, td)
 1655         struct file *fp;
 1656         struct thread *td;
 1657 {
 1658 
 1659         FILE_LOCK(fp);
 1660         return (fdrop_locked(fp, td));
 1661 }
 1662 
 1663 /*
 1664  * Extract the file pointer associated with the specified descriptor for
 1665  * the current user process.
 1666  *
 1667  * If the descriptor doesn't exist, EBADF is returned.
 1668  *
 1669  * If the descriptor exists but doesn't match 'flags' then
 1670  * return EBADF for read attempts and EINVAL for write attempts.
 1671  *
 1672  * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
 1673  * It should be droped with fdrop().
 1674  * If it is not set, then the refcount will not be bumped however the
 1675  * thread's filedesc struct will be returned locked (for fgetsock).
 1676  *
 1677  * If an error occured the non-zero error is returned and *fpp is set to NULL.
 1678  * Otherwise *fpp is set and zero is returned.
 1679  */
 1680 static __inline int
 1681 _fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
 1682 {
 1683         struct filedesc *fdp;
 1684         struct file *fp;
 1685 
 1686         *fpp = NULL;
 1687         if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
 1688                 return (EBADF);
 1689         FILEDESC_LOCK(fdp);
 1690         if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
 1691                 FILEDESC_UNLOCK(fdp);
 1692                 return (EBADF);
 1693         }
 1694 
 1695         /*
 1696          * Note: FREAD failures returns EBADF to maintain backwards
 1697          * compatibility with what routines returned before.
 1698          *
 1699          * Only one flag, or 0, may be specified.
 1700          */
 1701         if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
 1702                 FILEDESC_UNLOCK(fdp);
 1703                 return (EBADF);
 1704         }
 1705         if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
 1706                 FILEDESC_UNLOCK(fdp);
 1707                 return (EINVAL);
 1708         }
 1709         if (hold) {
 1710                 fhold(fp);
 1711                 FILEDESC_UNLOCK(fdp);
 1712         }
 1713         *fpp = fp;
 1714         return (0);
 1715 }
 1716 
 1717 int
 1718 fget(struct thread *td, int fd, struct file **fpp)
 1719 {
 1720 
 1721         return(_fget(td, fd, fpp, 0, 1));
 1722 }
 1723 
 1724 int
 1725 fget_read(struct thread *td, int fd, struct file **fpp)
 1726 {
 1727 
 1728         return(_fget(td, fd, fpp, FREAD, 1));
 1729 }
 1730 
 1731 int
 1732 fget_write(struct thread *td, int fd, struct file **fpp)
 1733 {
 1734 
 1735         return(_fget(td, fd, fpp, FWRITE, 1));
 1736 }
 1737 
 1738 /*
 1739  * Like fget() but loads the underlying vnode, or returns an error if
 1740  * the descriptor does not represent a vnode.  Note that pipes use vnodes
 1741  * but never have VM objects (so VOP_GETVOBJECT() calls will return an
 1742  * error).  The returned vnode will be vref()d.
 1743  */
 1744 static __inline int
 1745 _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
 1746 {
 1747         struct file *fp;
 1748         int error;
 1749 
 1750         *vpp = NULL;
 1751         if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
 1752                 return (error);
 1753         if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) {
 1754                 error = EINVAL;
 1755         } else {
 1756                 *vpp = (struct vnode *)fp->f_data;
 1757                 vref(*vpp);
 1758         }
 1759         FILEDESC_UNLOCK(td->td_proc->p_fd);
 1760         return (error);
 1761 }
 1762 
 1763 int
 1764 fgetvp(struct thread *td, int fd, struct vnode **vpp)
 1765 {
 1766 
 1767         return (_fgetvp(td, fd, vpp, 0));
 1768 }
 1769 
 1770 int
 1771 fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
 1772 {
 1773 
 1774         return (_fgetvp(td, fd, vpp, FREAD));
 1775 }
 1776 
 1777 int
 1778 fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
 1779 {
 1780 
 1781         return (_fgetvp(td, fd, vpp, FWRITE));
 1782 }
 1783 
 1784 /*
 1785  * Like fget() but loads the underlying socket, or returns an error if
 1786  * the descriptor does not represent a socket.
 1787  *
 1788  * We bump the ref count on the returned socket.  XXX Also obtain the SX
 1789  * lock in the future.
 1790  */
 1791 int
 1792 fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
 1793 {
 1794         struct file *fp;
 1795         int error;
 1796 
 1797         *spp = NULL;
 1798         if (fflagp != NULL)
 1799                 *fflagp = 0;
 1800         if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
 1801                 return (error);
 1802         if (fp->f_type != DTYPE_SOCKET) {
 1803                 error = ENOTSOCK;
 1804         } else {
 1805                 *spp = (struct socket *)fp->f_data;
 1806                 if (fflagp)
 1807                         *fflagp = fp->f_flag;
 1808                 soref(*spp);
 1809         }
 1810         FILEDESC_UNLOCK(td->td_proc->p_fd);
 1811         return (error);
 1812 }
 1813 
 1814 /*
 1815  * Drop the reference count on the the socket and XXX release the SX lock in
 1816  * the future.  The last reference closes the socket.
 1817  */
 1818 void
 1819 fputsock(struct socket *so)
 1820 {
 1821 
 1822         sorele(so);
 1823 }
 1824 
 1825 /*
 1826  * Drop reference on struct file passed in, may call closef if the
 1827  * reference hits zero.
 1828  * Expects struct file locked, and will unlock it.
 1829  */
 1830 int
 1831 fdrop_locked(fp, td)
 1832         struct file *fp;
 1833         struct thread *td;
 1834 {
 1835         struct flock lf;
 1836         struct vnode *vp;
 1837         int error;
 1838 
 1839         FILE_LOCK_ASSERT(fp, MA_OWNED);
 1840 
 1841         if (--fp->f_count > 0) {
 1842                 FILE_UNLOCK(fp);
 1843                 return (0);
 1844         }
 1845         mtx_lock(&Giant);
 1846         if (fp->f_count < 0)
 1847                 panic("fdrop: count < 0");
 1848         if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
 1849                 lf.l_whence = SEEK_SET;
 1850                 lf.l_start = 0;
 1851                 lf.l_len = 0;
 1852                 lf.l_type = F_UNLCK;
 1853                 vp = (struct vnode *)fp->f_data;
 1854                 FILE_UNLOCK(fp);
 1855                 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
 1856         } else
 1857                 FILE_UNLOCK(fp);
 1858         if (fp->f_ops != &badfileops)
 1859                 error = fo_close(fp, td);
 1860         else
 1861                 error = 0;
 1862         ffree(fp);
 1863         mtx_unlock(&Giant);
 1864         return (error);
 1865 }
 1866 
 1867 /*
 1868  * Apply an advisory lock on a file descriptor.
 1869  *
 1870  * Just attempt to get a record lock of the requested type on
 1871  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
 1872  */
 1873 #ifndef _SYS_SYSPROTO_H_
 1874 struct flock_args {
 1875         int     fd;
 1876         int     how;
 1877 };
 1878 #endif
 1879 /*
 1880  * MPSAFE
 1881  */
 1882 /* ARGSUSED */
 1883 int
 1884 flock(td, uap)
 1885         struct thread *td;
 1886         register struct flock_args *uap;
 1887 {
 1888         struct file *fp;
 1889         struct vnode *vp;
 1890         struct flock lf;
 1891         int error;
 1892 
 1893         if ((error = fget(td, uap->fd, &fp)) != 0)
 1894                 return (error);
 1895         if (fp->f_type != DTYPE_VNODE) {
 1896                 fdrop(fp, td);
 1897                 return (EOPNOTSUPP);
 1898         }
 1899 
 1900         mtx_lock(&Giant);
 1901         vp = (struct vnode *)fp->f_data;
 1902         lf.l_whence = SEEK_SET;
 1903         lf.l_start = 0;
 1904         lf.l_len = 0;
 1905         if (uap->how & LOCK_UN) {
 1906                 lf.l_type = F_UNLCK;
 1907                 FILE_LOCK(fp);
 1908                 fp->f_flag &= ~FHASLOCK;
 1909                 FILE_UNLOCK(fp);
 1910                 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
 1911                 goto done2;
 1912         }
 1913         if (uap->how & LOCK_EX)
 1914                 lf.l_type = F_WRLCK;
 1915         else if (uap->how & LOCK_SH)
 1916                 lf.l_type = F_RDLCK;
 1917         else {
 1918                 error = EBADF;
 1919                 goto done2;
 1920         }
 1921         FILE_LOCK(fp);
 1922         fp->f_flag |= FHASLOCK;
 1923         FILE_UNLOCK(fp);
 1924         error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
 1925             (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
 1926 done2:
 1927         fdrop(fp, td);
 1928         mtx_unlock(&Giant);
 1929         return (error);
 1930 }
 1931 
 1932 /*
 1933  * File Descriptor pseudo-device driver (/dev/fd/).
 1934  *
 1935  * Opening minor device N dup()s the file (if any) connected to file
 1936  * descriptor N belonging to the calling process.  Note that this driver
 1937  * consists of only the ``open()'' routine, because all subsequent
 1938  * references to this file will be direct to the other driver.
 1939  */
 1940 /* ARGSUSED */
 1941 static int
 1942 fdopen(dev, mode, type, td)
 1943         dev_t dev;
 1944         int mode, type;
 1945         struct thread *td;
 1946 {
 1947 
 1948         /*
 1949          * XXX Kludge: set curthread->td_dupfd to contain the value of the
 1950          * the file descriptor being sought for duplication. The error
 1951          * return ensures that the vnode for this device will be released
 1952          * by vn_open. Open will detect this special error and take the
 1953          * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
 1954          * will simply report the error.
 1955          */
 1956         td->td_dupfd = dev2unit(dev);
 1957         return (ENODEV);
 1958 }
 1959 
 1960 /*
 1961  * Duplicate the specified descriptor to a free descriptor.
 1962  */
 1963 int
 1964 dupfdopen(td, fdp, indx, dfd, mode, error)
 1965         struct thread *td;
 1966         struct filedesc *fdp;
 1967         int indx, dfd;
 1968         int mode;
 1969         int error;
 1970 {
 1971         register struct file *wfp;
 1972         struct file *fp;
 1973 
 1974         /*
 1975          * If the to-be-dup'd fd number is greater than the allowed number
 1976          * of file descriptors, or the fd to be dup'd has already been
 1977          * closed, then reject.
 1978          */
 1979         FILEDESC_LOCK(fdp);
 1980         if (dfd < 0 || dfd >= fdp->fd_nfiles ||
 1981             (wfp = fdp->fd_ofiles[dfd]) == NULL) {
 1982                 FILEDESC_UNLOCK(fdp);
 1983                 return (EBADF);
 1984         }
 1985 
 1986         /*
 1987          * There are two cases of interest here.
 1988          *
 1989          * For ENODEV simply dup (dfd) to file descriptor
 1990          * (indx) and return.
 1991          *
 1992          * For ENXIO steal away the file structure from (dfd) and
 1993          * store it in (indx).  (dfd) is effectively closed by
 1994          * this operation.
 1995          *
 1996          * Any other error code is just returned.
 1997          */
 1998         switch (error) {
 1999         case ENODEV:
 2000                 /*
 2001                  * Check that the mode the file is being opened for is a
 2002                  * subset of the mode of the existing descriptor.
 2003                  */
 2004                 FILE_LOCK(wfp);
 2005                 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
 2006                         FILE_UNLOCK(wfp);
 2007                         FILEDESC_UNLOCK(fdp);
 2008                         return (EACCES);
 2009                 }
 2010                 fp = fdp->fd_ofiles[indx];
 2011 #if 0
 2012                 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
 2013                         (void) munmapfd(td, indx);
 2014 #endif
 2015                 fdp->fd_ofiles[indx] = wfp;
 2016                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2017                 fhold_locked(wfp);
 2018                 FILE_UNLOCK(wfp);
 2019                 if (indx > fdp->fd_lastfile)
 2020                         fdp->fd_lastfile = indx;
 2021                 if (fp != NULL)
 2022                         FILE_LOCK(fp);
 2023                 FILEDESC_UNLOCK(fdp);
 2024                 /*
 2025                  * We now own the reference to fp that the ofiles[] array
 2026                  * used to own.  Release it.
 2027                  */
 2028                 if (fp != NULL)
 2029                         fdrop_locked(fp, td);
 2030                 return (0);
 2031 
 2032         case ENXIO:
 2033                 /*
 2034                  * Steal away the file pointer from dfd and stuff it into indx.
 2035                  */
 2036                 fp = fdp->fd_ofiles[indx];
 2037 #if 0
 2038                 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
 2039                         (void) munmapfd(td, indx);
 2040 #endif
 2041                 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
 2042                 fdp->fd_ofiles[dfd] = NULL;
 2043                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2044                 fdp->fd_ofileflags[dfd] = 0;
 2045 
 2046                 /*
 2047                  * Complete the clean up of the filedesc structure by
 2048                  * recomputing the various hints.
 2049                  */
 2050                 if (indx > fdp->fd_lastfile) {
 2051                         fdp->fd_lastfile = indx;
 2052                 } else {
 2053                         while (fdp->fd_lastfile > 0 &&
 2054                            fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
 2055                                 fdp->fd_lastfile--;
 2056                         }
 2057                         if (dfd < fdp->fd_freefile)
 2058                                 fdp->fd_freefile = dfd;
 2059                 }
 2060                 if (fp != NULL)
 2061                         FILE_LOCK(fp);
 2062                 FILEDESC_UNLOCK(fdp);
 2063 
 2064                 /*
 2065                  * we now own the reference to fp that the ofiles[] array
 2066                  * used to own.  Release it.
 2067                  */
 2068                 if (fp != NULL)
 2069                         fdrop_locked(fp, td);
 2070                 return (0);
 2071 
 2072         default:
 2073                 FILEDESC_UNLOCK(fdp);
 2074                 return (error);
 2075         }
 2076         /* NOTREACHED */
 2077 }
 2078 
 2079 /*
 2080  * Get file structures.
 2081  */
 2082 static int
 2083 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
 2084 {
 2085         struct xfile xf;
 2086         struct filedesc *fdp;
 2087         struct file *fp;
 2088         struct proc *p;
 2089         int error, n;
 2090 
 2091         sysctl_wire_old_buffer(req, 0);
 2092         if (req->oldptr == NULL) {
 2093                 n = 16;         /* A slight overestimate. */
 2094                 sx_slock(&filelist_lock);
 2095                 LIST_FOREACH(fp, &filehead, f_list) {
 2096                         /*
 2097                          * We should grab the lock, but this is an
 2098                          * estimate, so does it really matter?
 2099                          */
 2100                         /* mtx_lock(fp->f_mtxp); */
 2101                         n += fp->f_count;
 2102                         /* mtx_unlock(f->f_mtxp); */
 2103                 }
 2104                 sx_sunlock(&filelist_lock);
 2105                 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
 2106         }
 2107         error = 0;
 2108         bzero(&xf, sizeof(xf));
 2109         xf.xf_size = sizeof(xf);
 2110         sx_slock(&allproc_lock);
 2111         LIST_FOREACH(p, &allproc, p_list) {
 2112                 PROC_LOCK(p);
 2113                 xf.xf_pid = p->p_pid;
 2114                 xf.xf_uid = p->p_ucred->cr_uid;
 2115                 if ((fdp = p->p_fd) == NULL) {
 2116                         PROC_UNLOCK(p);
 2117                         continue;
 2118                 }
 2119                 FILEDESC_LOCK(fdp);
 2120                 for (n = 0; n < fdp->fd_nfiles; ++n) {
 2121                         if ((fp = fdp->fd_ofiles[n]) == NULL)
 2122                                 continue;
 2123                         xf.xf_fd = n;
 2124                         xf.xf_file = fp;
 2125 #define XF_COPY(field) xf.xf_##field = fp->f_##field
 2126                         XF_COPY(type);
 2127                         XF_COPY(count);
 2128                         XF_COPY(msgcount);
 2129                         XF_COPY(offset);
 2130                         XF_COPY(data);
 2131                         XF_COPY(flag);
 2132 #undef XF_COPY
 2133                         error = SYSCTL_OUT(req, &xf, sizeof(xf));
 2134                         if (error)
 2135                                 break;
 2136                 }
 2137                 FILEDESC_UNLOCK(fdp);
 2138                 PROC_UNLOCK(p);
 2139                 if (error)
 2140                         break;
 2141         }
 2142         sx_sunlock(&allproc_lock);
 2143         return (error);
 2144 }
 2145 
 2146 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
 2147     0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
 2148 
 2149 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW, 
 2150     &maxfilesperproc, 0, "Maximum files allowed open per process");
 2151 
 2152 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, 
 2153     &maxfiles, 0, "Maximum number of files");
 2154 
 2155 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD, 
 2156     &nfiles, 0, "System-wide number of open files");
 2157 
 2158 static void
 2159 fildesc_drvinit(void *unused)
 2160 {
 2161         dev_t dev;
 2162 
 2163         dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
 2164         make_dev_alias(dev, "stdin");
 2165         dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
 2166         make_dev_alias(dev, "stdout");
 2167         dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
 2168         make_dev_alias(dev, "stderr");
 2169         if (!devfs_present) {
 2170                 int fd;
 2171 
 2172                 for (fd = 3; fd < NUMFDESC; fd++)
 2173                         make_dev(&fildesc_cdevsw, fd, UID_ROOT, GID_WHEEL,
 2174                             0666, "fd/%d", fd);
 2175         }
 2176 }
 2177 
 2178 struct fileops badfileops = {
 2179         badfo_readwrite,
 2180         badfo_readwrite,
 2181         badfo_ioctl,
 2182         badfo_poll,
 2183         badfo_kqfilter,
 2184         badfo_stat,
 2185         badfo_close
 2186 };
 2187 
 2188 static int
 2189 badfo_readwrite(fp, uio, active_cred, flags, td)
 2190         struct file *fp;
 2191         struct uio *uio;
 2192         struct ucred *active_cred;
 2193         struct thread *td;
 2194         int flags;
 2195 {
 2196 
 2197         return (EBADF);
 2198 }
 2199 
 2200 static int
 2201 badfo_ioctl(fp, com, data, active_cred, td)
 2202         struct file *fp;
 2203         u_long com;
 2204         void *data;
 2205         struct ucred *active_cred;
 2206         struct thread *td;
 2207 {
 2208 
 2209         return (EBADF);
 2210 }
 2211 
 2212 static int
 2213 badfo_poll(fp, events, active_cred, td)
 2214         struct file *fp;
 2215         int events;
 2216         struct ucred *active_cred;
 2217         struct thread *td;
 2218 {
 2219 
 2220         return (0);
 2221 }
 2222 
 2223 static int
 2224 badfo_kqfilter(fp, kn)
 2225         struct file *fp;
 2226         struct knote *kn;
 2227 {
 2228 
 2229         return (0);
 2230 }
 2231 
 2232 static int
 2233 badfo_stat(fp, sb, active_cred, td)
 2234         struct file *fp;
 2235         struct stat *sb;
 2236         struct ucred *active_cred;
 2237         struct thread *td;
 2238 {
 2239 
 2240         return (EBADF);
 2241 }
 2242 
 2243 static int
 2244 badfo_close(fp, td)
 2245         struct file *fp;
 2246         struct thread *td;
 2247 {
 2248 
 2249         return (EBADF);
 2250 }
 2251 
 2252 SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
 2253                                         fildesc_drvinit,NULL)
 2254 
 2255 static void filelistinit(void *);
 2256 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
 2257 
 2258 /* ARGSUSED*/
 2259 static void
 2260 filelistinit(dummy)
 2261         void *dummy;
 2262 {
 2263 
 2264         file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
 2265             NULL, NULL, UMA_ALIGN_PTR, 0);
 2266         sx_init(&filelist_lock, "filelist lock");
 2267         mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
 2268 }

Cache object: ff96d0cf58843e2cb909527fa5a7e503


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