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/servers/fs/filedes.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 /* This file contains the procedures that manipulate file descriptors.
    2  *
    3  * The entry points into this file are
    4  *   get_fd:    look for free file descriptor and free filp slots
    5  *   get_filp:  look up the filp entry for a given file descriptor
    6  *   find_filp: find a filp slot that points to a given inode
    7  */
    8 
    9 #include "fs.h"
   10 #include "file.h"
   11 #include "fproc.h"
   12 #include "inode.h"
   13 
   14 /*===========================================================================*
   15  *                              get_fd                                       *
   16  *===========================================================================*/
   17 PUBLIC int get_fd(int start, mode_t bits, int *k, struct filp **fpt)
   18 {
   19 /* Look for a free file descriptor and a free filp slot.  Fill in the mode word
   20  * in the latter, but don't claim either one yet, since the open() or creat()
   21  * may yet fail.
   22  */
   23 
   24   register struct filp *f;
   25   register int i;
   26 
   27   *k = -1;                      /* we need a way to tell if file desc found */
   28 
   29   /* Search the fproc fp_filp table for a free file descriptor. */
   30   for (i = start; i < OPEN_MAX; i++) {
   31         if (fp->fp_filp[i] == NIL_FILP) {
   32                 /* A file descriptor has been located. */
   33                 *k = i;
   34                 break;
   35         }
   36   }
   37 
   38   /* Check to see if a file descriptor has been found. */
   39   if (*k < 0) return(EMFILE);   /* this is why we initialized k to -1 */
   40 
   41   /* Now that a file descriptor has been found, look for a free filp slot. */
   42   for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
   43         if (f->filp_count == 0) {
   44                 f->filp_mode = bits;
   45                 f->filp_pos = 0L;
   46                 f->filp_selectors = 0;
   47                 f->filp_select_ops = 0;
   48                 f->filp_pipe_select_ops = 0;
   49                 f->filp_flags = 0;
   50                 *fpt = f;
   51                 return(OK);
   52         }
   53   }
   54 
   55   /* If control passes here, the filp table must be full.  Report that back. */
   56   return(ENFILE);
   57 }
   58 
   59 /*===========================================================================*
   60  *                              get_filp                                     *
   61  *===========================================================================*/
   62 PUBLIC struct filp *get_filp(fild)
   63 int fild;                       /* file descriptor */
   64 {
   65 /* See if 'fild' refers to a valid file descr.  If so, return its filp ptr. */
   66 
   67   err_code = EBADF;
   68   if (fild < 0 || fild >= OPEN_MAX ) return(NIL_FILP);
   69   return(fp->fp_filp[fild]);    /* may also be NIL_FILP */
   70 }
   71 
   72 /*===========================================================================*
   73  *                              find_filp                                    *
   74  *===========================================================================*/
   75 PUBLIC struct filp *find_filp(register struct inode *rip, mode_t bits)
   76 {
   77 /* Find a filp slot that refers to the inode 'rip' in a way as described
   78  * by the mode bit 'bits'. Used for determining whether somebody is still
   79  * interested in either end of a pipe.  Also used when opening a FIFO to
   80  * find partners to share a filp field with (to shared the file position).
   81  * Like 'get_fd' it performs its job by linear search through the filp table.
   82  */
   83 
   84   register struct filp *f;
   85 
   86   for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
   87         if (f->filp_count != 0 && f->filp_ino == rip && (f->filp_mode & bits)){
   88                 return(f);
   89         }
   90   }
   91 
   92   /* If control passes here, the filp wasn't there.  Report that back. */
   93   return(NIL_FILP);
   94 }

Cache object: 3c1c2fb4603720799c83bb0d0c372405


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