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 file descriptor table maintains the following information:
p l -bullet -compact t the number of descriptors allocated in the file descriptor table; t approximate next free descriptor; t a reference count on the file descriptor table; and t an array of open file entries. .El
p 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 necessary to access the underlying object and to maintain common information. See .Xr file 9 for details of operations on the file entries.
p New file descriptors are generally allocated by .Fn falloc and freed by .Fn fdrelease . File entries are extracted from the file descriptor table by .Fn fd_getfile . Most of the remaining functions in the interface are purpose specific and perform lower-level file descriptor operations. .Sh FUNCTIONS The following functions are high-level interface routines to access the file descriptor table for a process and its file entries.
p l -tag -width compact t Fn falloc "p" "*resultfp" "*resultfd" Create a new open file entry and allocate a file descriptor for process .Fa p . This operation is performed by invoking .Fn fdalloc to allocate the new file descriptor. The credential on the file entry are inherited from process .Fa p . The .Fn falloc function is responsible for expanding the file descriptor table when necessary.
p A pointer to the file entry is returned in .Fa *resultfp and the file descriptor is returned in .Fa *resultfd . The .Fn falloc function returns zero on success, otherwise an appropriate error is returned. t Fn fd_getfile "fdp" "fd" Get the file entry for file descriptor .Fa fd in the file descriptor table .Fa fdp . The file entry is returned if it is valid and useable, otherwise .Dv NULL is returned. t Fn dupfdopen "l" "indx" "dfd" "mode" "error" Duplicate file descriptor .Fa dfd for lwp .Fa l . .El
p The following functions operate on the file descriptor table for a process.
p l -tag -width compact t Fn fdalloc "p" "want" "*result" Allocate a file descriptor .Fa want for process .Fa p . The resultant file descriptor is returned in .Fa *result . The .Fn fdalloc function returns zero on success, otherwise an appropriate error is returned. t Fn fdcheckstd "l" Check the standard file descriptors 0, 1, and 2 and ensure they are referencing valid file descriptors. If they are not, create references to
a /dev/null . This operation is necessary as these file descriptors are given implicit significance in the Standard C Library and it is unsafe for .Xr setuid 2 and .Xr setgid 2 processes to be started with these file descriptors closed. t Fn fdclear "l" Clear the descriptor table for lwp .Fa l . This operation is performed by invoking .Fn fdinit to initialise a new file descriptor table to replace the old file descriptor table and invoking .Fn fdfree to release the old one. t Fn fdclone "l" "fp" "fd" "flag" "fops" "data" This function is meant to be used by devices which allocate a file entry upon open. .Fn fdclone fills .Fa fp with the given parameters. It always returns the in-kernel errno value .Er EMOVEFD , which is meant to be returned from the device open routine. This special return value is interpreted by the caller of the device open routine. t Fn fdcloseexec "l" Close any files for process .Fa p that are marked .Dq close on exec . This operation is performed by invoking .Fn fdunshare for the process and invoking .Fn fdrelease on the appropriate file descriptor. t Fn fdcopy "p" Copy the file descriptor table from process .Fa p 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. t Fn fdexpand "p" Expand the file descriptor table for process .Fa p by allocating memory for additional file descriptors. t Fn fdfree "l" Decrement the reference count on the file descriptor table for lwp .Fa l and release the file descriptor table if the reference count drops to zero. t Fn fdinit "p" Create a file descriptor table using the same current and root directories of process .Fa p . The returned file descriptor table is guaranteed to have a reference count of one. t Fn fdrelease "l" "fd" Remove file descriptor .Fa fd from the file descriptor table of lwp .Fa l . The operation is performed by invoking .Fn closef . t Fn fdremove "fdp" "fd" Unconditionally remove the file descriptor .Fa fd from file descriptor table .Fa fdp . t Fn fdshare "p1" "p2" Share the file descriptor table belonging to process .Fa p1 with process .Fa p2 . Process .Fa p2 is assumed not to have a file descriptor table already allocated. The reference count on the file descriptor table is incremented. This function is used by .Xr fork1 9 . t Fn fdunshare "l" Ensure that lwp .Fa l does not share its file descriptor table. If its file descriptor table has more than one reference, the file descriptor table is copied by invoking .Fn fdcopy . The reference count on the original file descriptor table is decremented. .El .Sh RETURN VALUES Successful operations return zero. A failed operation will return a non-zero return value. Possible values include:
p l -tag -width Er t Bq Er EBADF Bad file descriptor specified. t Bq Er EMFILE Cannot exceed file descriptor limit. t Bq Er ENOSPC No space left in file descriptor table. .El .Sh CODE REFERENCES This section describes places within the .Nx source tree where actual code implementing or using file descriptors can be found. All pathnames are relative to
p The framework for file descriptor handling is implemented within the file
a sys/kern/kern_descrip.c . .Sh SEE ALSO .Xr file 9