In Unix and related computer operating systems, a file descriptor (FD, less frequently fildes) is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipe or network socket. File descriptors form part of the POSIX application programming interface. A file descriptor is a non-negative integer, generally represented in the C programming language as the type int (negative values being reserved to indicate "no value" or an error condition).
|Integer value||Name||<unistd.h> symbolic constant||<stdio.h> file stream|
- 1 Overview
- 2 Operations on file descriptors
- 3 Upcoming operations
- 4 File descriptors as capabilities
- 5 See also
- 6 References
In the traditional implementation of Unix, file descriptors index into a per-process file descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the file table. This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes. It also indexes into a third table called the inode table that describes the actual underlying files. To perform input or output, the process passes the file descriptor to the kernel through a system call, and the kernel will access the file on behalf of the process. The process does not have direct access to the file or inode tables.
In Unix-like systems, file descriptors can refer to any Unix file type named in a file system. As well as regular files, this includes directories, block and character devices (also called "special files"), Unix domain sockets, and named pipes. File descriptors can also refer to other objects that do not normally exist in the file system, such as anonymous pipes and network sockets.
The FILE data structure in the C standard I/O library usually includes a low level file descriptor for the object in question on Unix-like systems. The overall data structure provides additional abstraction and is instead known as a file handle.
Operations on file descriptors
The following lists typical operations on file descriptors on modern Unix-like systems. Most of these functions are declared in the
<unistd.h> header, but some are in the
<fcntl.h> header instead.
Creating file descriptors
- open_by_handle_at() (Linux)
- signalfd() (Linux)
- eventfd() (Linux)
- timerfd_create() (Linux)
- memfd_create() (Linux)
- userfaultfd() (Linux)
Deriving file descriptors
Operations on a single file descriptor
- read(), write()
- readv(), writev()
- pread(), pwrite()
- recv(), send()
- recvmsg(), sendmsg() (including allowing sending FDs)
- vmsplice() (Linux)
Operations on multiple file descriptors
- select(), pselect()
- epoll() (for Linux)
- kqueue() (for BSD-based systems).
- splice(), tee() (for Linux)
Operations on the file descriptor table
The fcntl() function is used to perform various operations on a file descriptor, depending on the command argument passed to it. There are commands to get and set attributes associated with a file descriptor, including F_GETFD, F_SETFD, F_GETFL and F_SETFL.
- closefrom() (BSD and Solaris only; deletes all file descriptors greater than or equal to specified number)
- dup() (duplicates an existing file descriptor guaranteeing to be the lowest number available file descriptor)
- dup2() (the new file descriptor will have the value passed as an argument)
- fcntl (F_DUPFD)
Operations that modify process state
- fchdir() (sets the process's current working directory based on a directory file descriptor)
- mmap() (maps ranges of a file into the process's address space)
- fcntl() (F_GETLK, F_SETLK) and F_SETLKW
- accept() (creates a new file descriptor for an incoming connection)
- shutdown() (shuts down one or both halves of a full duplex connection)
- ioctl() (a large collection of miscellaneous operations on a single file descriptor, often associated with a device)
A series of new operations on file descriptors has been added to many modern Unix-like systems, as well as numerous C libraries, to be standardized in a future version of POSIX. The
at suffix signifies that the function takes an additional first argument supplying a file descriptor from which relative paths are resolved, the forms lacking the
at suffix thus becoming equivalent to passing a file descriptor corresponding to the current working directory. The purpose of these new operations is to defend against a certain class of TOCTTOU attacks.
File descriptors as capabilities
Unix file descriptors behave in many ways as capabilities. They can be passed between processes across Unix domain sockets using the
sendmsg() system call. Note, however, that what is actually passed is a reference to an "open file description" that has mutable state (the file offset, and the file status and access flags). This complicates the secure use of file descriptors as capabilities, since when programs share access to the same open file description, they can interfere with each other's use of it by changing its offset or whether it is blocking or non-blocking, for example. In operating systems that are specifically designed as capability systems, there is very rarely any mutable state associated with a capability itself.
A Unix process' file descriptor table is an example of a C-list.
- fuser (Unix)
- File Control Block (FCB) - an alternative scheme in C/PM and early versions of DOS
- The Open Group. "The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008, 2016 Edition". Retrieved 2017-09-21.
- The Open Group. "The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008, 2016 Edition". <stdio.h>. Retrieved 2017-09-21.
- Bach, Maurice J. (1986). The Design of the UNIX Operating System (8 ed.). Prentice-Hall. pp. 92–96. ISBN 9780132017992.
- Extended API Set, Part 2. The Open Group. October 2006. ISBN 1931624674.
- Brinkmann, Marcus (2009-02-04). "Building a bridge: library API's and file descriptors?". cap-talk. Retrieved 2017-09-21.
- de Boyne Pollard, Jonathan (2007). "Don't set shared file descriptors to non-blocking I/O mode". Retrieved 2017-09-21.