HOME

TheInfoList




In
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser Multi-user software is computer software Software is a collection of Instruction (computer science), instructions that tell a computer how to work. This is in contrast t ...

Unix
and
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system An operating system (OS) is system software System software is software designed to provide a platform for other software. Examples of system software include operating s ...
computer operating systems, a file descriptor (FD, less frequently fildes) is a unique identifier (
handle A handle is a part of, or attachment to, an object that allows it to be grasp A grasp is an act of taking, holding or seizing firmly with (or as if with) the hand A hand is a prehensile, multi- fingered appendage located at the end of t ...
) for a
file File or filing may refer to: Mechanical tools and processes * File (tool) A file is a tool used to remove fine amounts of material from a workpiece. It is common in woodworking, metalworking, and other similar trade and hobby tasks. Most are ...
or other
input/output In computing, input/output (I/O, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, possibly a human or another information processing system. Information, Inputs are ...
resource Resource refers to all the materials available in our environment which help us to satisfy our needs and wants. Resources can broadly be classified upon their availability — they are classified into renewable A renewable resource, also know ...
, such as a
pipe Pipe(s) or PIPE(S) may refer to: Common uses * Pipe (fluid conveyance) A pipe is a tubular section or hollow Cylinder (geometry), cylinder, usually but not necessarily of circle, circular cross section (geometry), cross-section, used m ...
or
network socket A network socket is a software structure within a network nodeIn telecommunications network A telecommunications network is a group of nodes interconnected by links that are used to exchange messages between the nodes. The links may use a var ...
. File descriptors typically have non-negative integer values, with negative values being reserved to indicate "no value" or error conditions. File descriptors are a part of the
POSIX The Portable Operating System Interface (POSIX) is a family of standard Standard may refer to: Flags * Colours, standards and guidons * Standard (flag), a type of flag used for personal identification Norm, convention or requirement * St ...
API In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and so ...
. Each Unix
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management *Business process A business process, business method ...
(except perhaps daemons) should have three standard POSIX file descriptors, corresponding to the three
standard streams In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, genera ...
:


Overview

In the traditional implementation of Unix, file descriptors index into a per-process maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the . 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 In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and so ...
, 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. On
Linux Linux ( or ) is a family of open-source Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product ...

Linux
, the set of file descriptors open in a process can be accessed under the path /proc/PID/fd/, where PID is the
process identifier In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithm of an algorithm (Euclid's algorithm) for calculating the greatest common ...
. In
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system An operating system (OS) is system software System software is software designed to provide a platform for other software. Examples of system software include operating s ...
systems, file descriptors can refer to any Unix file type named in a file system. As well as regular files, this includes
directories Directory may refer to: * Directory (computing) In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and developmen ...
,
block Block or blocked may refer to: Arts, entertainment and media Broadcasting * Block programming, the result of a programming strategy in broadcasting * W242BX, a radio station licensed to Greenville, South Carolina, United States known as ''96.3 ...
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 socket A network socket is a software structure within a network nodeIn telecommunications network A telecommunications network is a group of nodes interconnected by links that are used to exchange messages between the nodes. The links may use a var ...
s. The FILE data structure in the stdio, 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 A handle is a part of, or attachment to, an object that allows it to be grasp A grasp is an act of taking, holding or seizing firmly with (or as if with) the hand A hand is a prehensile, multi- fingered appendage located at the end of t ...
.''


Operations on file descriptors

The following lists typical operations on file descriptors on modern
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system An operating system (OS) is system software System software is software designed to provide a platform for other software. Examples of system software include operating s ...
systems. Most of these functions are declared in the header, but some are in the header instead.


Creating file descriptors

* * * * * * * (Linux) * (Linux) * (Linux) * (Linux) * (Linux) * (Linux) * (Linux) * (Linux) * (with flag CLONE_PIDFD, Linux) * (Linux) * (Linux)


Deriving file descriptors

* *


Operations on a single file descriptor

* *, *, *, *, *, (also used for sending FDs to other processes over a Unix domain socket) *, *, * * * * * * * * *, (Linux) *, (Linux) * (Linux) * (Linux) * (Linux) * (Linux) * (with P_PIDFD ID type, Linux) * (stdio function:converts file descriptor to FILE*) * (stdio function: prints to file descriptor)


Operations on multiple file descriptors

*select (Unix), , *poll (Unix), , *, , (Linux, takes a single epoll filedescriptor to wait on many other file descriptors) *Epoll, (for Linux) *Kqueue, (for BSD-based systems). * *splice (system call), , (for Linux) * (for Linux) * (for Linux)


Operations on the file descriptor table

The 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 and . * * (BSD and Solaris only; deletes all file descriptors greater than or equal to specified number) * (duplicates an existing file descriptor guaranteeing to be the lowest number available file descriptor) *, (Close fd1 if necessary, and make file descriptor fd1 point to the open file of fd2) *


Operations that modify process state

* (sets the process's current working directory based on a directory file descriptor) * (maps ranges of a file into the process's address space)


File locking

* * and *


Sockets

* * * * (creates a new file descriptor for an incoming connection) * * * * * (shuts down one or both halves of a full duplex connection)


Miscellaneous

*ioctl, (a large collection of miscellaneous operations on a single file descriptor, often associated with a device)


Upcoming operations

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 Portable Operating System Interface (POSIX) is a family of standard Standard may refer to: Flags * Colours, standards and guidons * Standard (flag), a type of flag used for personal identification Norm, convention or requirement * St ...
. 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 Time-of-check-to-time-of-use, TOCTOU attacks. * * * * * * * * * * * * * * *


File descriptors as capabilities

Unix file descriptors behave in many ways as Capability-based security, 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 (computer security), C-list.


See also

* fuser (Unix) * lsof * File Control Block (FCB) - an alternative scheme in C/PM and early versions of DOS


References

{{Object-capability security POSIX Unix file system technology de:Handle#Datei-Handle