| FILEDESC(9) | Kernel Developer's Manual | FILEDESC(9) | 
filedesc, fd_alloc,
  fd_checkstd, fd_clone,
  fd_closeexec, fd_copy,
  fd_dup, fd_dup2,
  fd_dupopen, fd_free,
  fd_init, fd_getfile,
  fd_share, fd_tryexpand
  —
#include <sys/file.h>
#include <sys/filedesc.h>
int
  
  fd_alloc(proc_t
    *p, int want,
    int *result);
int
  
  fd_checkstd(void);
int
  
  fd_clone(file_t
    *fp, int fd,
    int flag,
    const struct fileops
    *fops, void
  *data);
filedesc_t *
  
  fd_copy(void);
void
  
  fd_closeexec(void);
int
  
  fd_dup(file_t
    *fp, int minfd,
    int *newp,
    bool exclose);
int
  
  fd_dup2(file_t
    *fp, unsigned
    newfd, int
  flags);
int
  
  fd_dupopen(int
    old, int *newp,
    int error);
void
  
  fd_free(void);
filedesc_t *
  
  fd_init(filedesc_t
    *fdp);
file_t *
  
  fd_getfile(unsigned
    fd);
void
  
  fd_share(proc_t
    *p);
void
  
  fd_tryexpand(proc_t
    *p);
The kernel maintains a descriptor table for each process which is used to translate the external representation of a file descriptor into an internal representation. The file descriptor is merely an index into this table. The table maintains the following information:
On creation of the file descriptor table, a fixed number of file entries are created. It is the responsibility of the file descriptor operations to expand the available number of entries if more are required. Each file entry in the descriptor table contains the information needed to access the underlying object and to maintain common information. See file(9) for details of operations on the file entries.
New file descriptors are generally allocated by
    fd_alloc() and freed by
    fd_free(). File entries are extracted from the file
    descriptor table by fd_getfile(). Most of the
    remaining functions in the interface are purpose-specific and perform
    lower-level file descriptor operations.
fd_alloc(p,
    want, *result)fd_alloc() function expands the file
      descriptor table when necessary.
    The index of the file entry is returned in
        *result. The fd_alloc()
        function returns zero on success, or an appropriate error value
        otherwise.
fd_getfile(fd)NULL is returned.fd_dup(fp,
    minfd, *newp,
    exclose)fd_dup2(fp,
    newfd, flags)fd_dupopen(old,
    *newp, error)The following functions operate on the file descriptor table for a process.
fd_alloc(p,
    want, *result)fd_alloc()
      function returns zero on success, otherwise an appropriate error is
      returned.fd_clone(fp,
    fd, flag,
    fops, data)fd_clone() fills
      fp with the given parameters. It always returns the
      in-kernel errno EMOVEFD. This special return value
      is interpreted by the caller of the device open routine.fd_closeexec(void)fd_close() on the appropriate file
    descriptor.fd_copy(void)fd_tryexpand(p)fd_free(void)fd_share(p)fd_checkstd(l)fd_init(fdp)EBADF]EMFILE]ENOSPC]| April 8, 2019 | NetBSD 9.3 |