File descriptor

From Wikipedia, the free encyclopedia

The file descriptors for input, output, and error
The file descriptors for input, output, and error

In computer programming, a file descriptor is an abstract key for accessing a file. The term is generally used in POSIX operating systems. In Microsoft Windows terminology and in the context of the C standard I/O library, "file handle" is preferred, though the latter case is technically a different object (see below).

In POSIX, a file descriptor is an integer, specifically of the C type int. There are 3 standard POSIX file descriptors which presumably every process (save perhaps a daemon) should expect to have:

Integer value Name
0 Standard Input (stdin)
1 Standard Output (stdout)
2 Standard Error (stderr)

Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table. The user application passes the abstract key to the kernel through a system call, and the kernel will access the file on behalf of the application, based on the key. The application itself cannot read or write the file descriptor table directly.

In Unix-like systems, file descriptors can refer to files, directories, block or character devices (also called "special files"), sockets, FIFOs (also called named pipes), or unnamed pipes.

The FILE * file handle in the C standard I/O library routines is technically a pointer to a data structure managed by those library routines; one of those structures usually includes an actual low level file descriptor for the object in question on Unix-like systems. Since file handle refers to this additional layer, it is not interchangeable with file descriptor.

To further complicate terminology, Microsoft Windows also uses the term file handle to refer to the more low-level construct, akin to POSIX's file descriptors. Microsoft's C libraries also provide compatibility functions which "wrap" these native handles to support the POSIX-like convention of integer file descriptors as detailed above.

Contents

[edit] Operations on file descriptors

A modern Unix typically provides the following operations on file descriptors.

[edit] Creating file descriptors

  • open(), open64(), creat(), creat64()
  • socket()
  • socketpair()
  • pipe()

[edit] Deriving file descriptors

  • fileno()
  • dirfd()

[edit] Operations on a single file descriptor

  • read(), write()
  • recv(), send()
  • recvmsg(), sendmsg() (inc. allowing sending FDs)
  • sendfile()
  • lseek(), lseek64()
  • fstat(), fstat64()
  • fchmod()
  • fchown()
  • fdopen()
  • gzdopen()
  • ftruncate()

[edit] Operations on multiple file descriptors

  • select(), pselect()
  • poll()

[edit] Operations on the file descriptor table

  • close()
  • dup()
  • dup2()
  • fcntl (F_DUPFD)
  • fcntl (F_GETFD and F_SETFD)

[edit] 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

[edit] File locking

  • flock()
  • fcntl (F_GETLK, F_SETLK and F_SETLKW)
  • lockf()

[edit] Sockets

  • connect()
  • bind()
  • listen()
  • accept(): creates a new file descriptor for an incoming connection
  • getsockname()
  • getpeername()
  • getsockopt(), setsockopt()
  • shutdown(): shuts down one or both halves of a full duplex connection

[edit] Miscellaneous

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

[edit] Upcoming Operations

A series of new operations on file descriptors has been added to Solaris and Linux, as well as numerous C libraries, to be standardized in a future version of POSIX,

  • openat()
  • faccessat()
  • fchmodat()
  • fchownat()
  • fstatat()
  • futimesat()
  • linkat()
  • mkdirat()
  • mknodat()
  • readlinkat()
  • renameat()
  • symlinkat()
  • unlinkat()
  • mkfifoat()

[edit] File descriptors as capabilities

Unix file descriptors are capabilities. They can be passed between processes across Unix domain sockets using the sendmsg() system call.

A Unix process' file descriptor table is an example of a C-list.

[edit] Capabilities

Using file descriptors properly allows a programmer to avoid race conditions in applications that need secure file management. Although it has been suggested that the current set, and even the proposed set of features is not enough to secure a program properly.

[edit] References