FILEDESC(9)
π FILEDESC(9) π
okay bestie, welcome to FILEDESC(9) from the Kernel Developer's Manual β this is the whole file descriptor table saga, and it's lowkey iconic π
πΈ NAME πΈ
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 β file descriptor tables and operations π«Ά
β¨ SYNOPSIS β¨
#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);
π¦ DESCRIPTION π¦
For user processes, all I/O is done through file descriptors. These file descriptors represent underlying objects supported by the kernel and are created by system calls specific to the type of object. In NetBSD, six types of objects can be represented by file descriptors: data files, pipes, sockets, event queues, crypto, and miscellaneous β¨
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:
- the number of descriptors allocated in the file descriptor table;
- approximate next free descriptor;
- a reference count on the file descriptor table; and
- an array of open file entries.
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.
π FUNCTIONS π
The following functions are high-level interface routines to access the file descriptor table for a process and its file entries.
fd_alloc(p, want, *result)β Create a new open file entry in the file descriptor table and allocate a file descriptor for the processp. The credential on the file entry are inherited from processp. Calling thefd_alloc()function expands the file descriptor table when necessary. The index of the file entry is returned in*result. Thefd_alloc()function returns zero on success, or an appropriate error value otherwise.fd_getfile(fd)β Get the file entry for file descriptorfd. The file entry is returned if it is valid and usable, otherwiseNULLis returned.fd_dup(fp, minfd, *newp, exclose)β Duplicate file descriptorfpfor the current process. The fd picked will be at leastminfd. The resulting descriptor is given innewp.fd_dup2(fp, newfd, flags)β Duplicate file descriptorfpin fd numbernewfd. Ifnewfdis already in use by an open file, close it (Seedup2(2)).fd_dupopen(old, *newp, error)β Duplicate file descriptor specified inoldfor the current lwp.
The following functions operate on the file descriptor table for a process.
fd_alloc(p, want, *result)β Allocate a file descriptorwantfor processp. The resultant file descriptor is returned in*result. Thefd_alloc()function returns zero on success, otherwise an appropriate error is returned.fd_clone(fp, fd, flag, fops, data)β This function is meant to be used by devices which allocate a file entry upon open.fd_clone()fillsfpwith the given parameters. It always returns the in-kernel errnoEMOVEFD. This special return value is interpreted by the caller of the device open routine.fd_closeexec(void)β Close any files for the current process that are marked βclose on execβ. This operation is performed by invokingfd_close()on the appropriate file descriptor.fd_copy(void)β Copy the file descriptor table from the current process and return a pointer to the copy. The returned file descriptor is guaranteed to have a reference count of one. All file descriptor state is maintained. The reference counts on each file entry referenced by the file descriptor table is incremented accordingly.fd_tryexpand(p)β Expand the file descriptor table for processpby allocating memory for additional file descriptors.fd_free(void)β Decrement the reference count on the file descriptor table for the current lwp and release the file descriptor table if the reference count drops to zero.fd_share(p)β Make processpshare the current process's filedesc structure.fd_checkstd(l)β Check the standard file descriptors0,1, and2and ensure they are referencing valid file descriptors. If they are not, create references to/dev/null. This is done to setuid/setgid executables, as file descriptors0,1,2are implicitly used by the Standard C Library.fd_init(fdp)β Create a file descriptor table using the same current and root directories of the current process. The returned file descriptor table is guaranteed to have a reference count of one.
π RETURN VALUES π
Successful operations return zero. A failed operation will return a non-zero value. Possible values include:
[EBADF]β Bad file descriptor specified.[EMFILE]β Cannot exceed file descriptor limit.[ENOSPC]β No space left in file descriptor table.
π§ CODE REFERENCES π§
The framework for file descriptor handling is implemented within the file sys/kern/kern_descrip.c π₯
π₯ SEE ALSO π₯
mwah π β this page's source/version line says Debian, and the date on it is April 8, 2019 π
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2002, 2005, 2006 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms