Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

File descriptor

From Wikipedia, the free encyclopedia
(Redirected fromFcntl.h)
System resource identifier in operating systems
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "File descriptor" – news ·newspapers ·books ·scholar ·JSTOR
(September 2025) (Learn how and when to remove this message)

InUnix andUnix-likecomputeroperating systems, afile descriptor (FD, less frequentlyfildes) is a process-unique identifier (handle) for afile or otherinput/outputresource, such as apipe ornetwork socket.

File descriptors typically have non-negativeinteger values, with negative values being reserved to indicate "no value" or error conditions.

File descriptors are a part of thePOSIXAPI. Each Unixprocess (except perhapsdaemons) should have three standard POSIX file descriptors, corresponding to the threestandard streams:

Integer valueName<unistd.h> symbolic constant[1]<stdio.h> file stream[2]
0Standard inputSTDIN_FILENOstdin
1Standard outputSTDOUT_FILENOstdout
2Standard errorSTDERR_FILENOstderr

Overview

[edit]
File descriptors for a single process, file table andinode table. Note that multiple file descriptors can refer to the same file table entry (e.g., as a result of thedup system call[3]: 104 ) and that multiple file table entries can in turn refer to the same inode (if it has been opened multiple times; the table is still simplified because it represents inodes by file names, even though an inode can havemultiple names). File descriptor 3 does not refer to anything in the file table, signifying that it has been closed.

In the traditional implementation of Unix, file descriptors index into a per-processfile descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called thefile table. This table records themode 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 theinode table that describes the actual underlying files.[3] To perform input or output, the process passes the file descriptor to the kernel through asystem 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.

OnLinux, the set of file descriptors open in a process can be accessed under the path/proc/PID/fd/, where PID is theprocess identifier. File descriptor/proc/PID/fd/0 isstdin,/proc/PID/fd/1 isstdout, and/proc/PID/fd/2 isstderr. As a shortcut to these, any running process can also accessits own file descriptors through the folders/proc/self/fd and/dev/fd.[4]

InUnix-like systems, file descriptors can refer to anyUnix file type named in a file system. As well as regular files, this includesdirectories,block andcharacter devices (also called "special files"),Unix domain sockets, andnamed pipes. File descriptors can also refer to other objects that do not normally exist in the file system, such asanonymous pipes andnetwork sockets.

TheFILE data structure in theC 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 afilehandle.

Operations on file descriptors

[edit]

The following lists typical operations on file descriptors on modernUnix-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

[edit]
  • open()
  • creat()[5]
  • socket()
  • accept()
  • socketpair()
  • pipe()
  • epoll_create() (Linux)
  • signalfd() (Linux)
  • eventfd() (Linux)
  • timerfd_create() (Linux)
  • memfd_create() (Linux)
  • userfaultfd() (Linux)
  • fanotify_init() (Linux)
  • inotify_init() (Linux)
  • clone() (with flag CLONE_PIDFD, Linux)
  • pidfd_open() (Linux)
  • open_by_handle_at() (Linux)
  • kqueue() (BSD)
  • pdfork() (kFreeBSD)

Deriving file descriptors

[edit]
  • dirfd()
  • fileno()

Operations on a single file descriptor

[edit]
  • read(),write()
  • readv(),writev()
  • pread(),pwrite()
  • recv(),send()
  • recvfrom(),sendto()
  • recvmsg(),sendmsg() (also used for sending FDs to other processes over a Unix domain socket)
  • recvmmsg(),sendmmsg()
  • lseek(),llseek()
  • fstat()
  • fstatvfs()
  • fchmod()
  • fchown()
  • ftruncate()
  • fsync()
  • fdatasync()
  • fdopendir()
  • fgetxattr(),fsetxattr() (Linux)
  • flistxattr(),fremovexattr() (Linux)
  • statx (Linux)
  • setns (Linux)
  • vmsplice() (Linux)
  • pidfd_send_signal() (Linux)
  • pdkill() (kFreeBSD)
  • waitid() (with P_PIDFD ID type, Linux)
  • fdopen() (stdio function:converts file descriptor to FILE*)
  • dprintf() (stdio function: prints to file descriptor)

Operations on multiple file descriptors

[edit]
  • select(),pselect()
  • poll(),ppoll()
  • epoll_wait(),epoll_pwait(),epoll_pwait2() (Linux, takes a single epoll filedescriptor to wait on many other file descriptors)
  • epoll_ctl() (for Linux)
  • kqueue() (for BSD-based systems).
  • sendfile()
  • splice(),tee() (for Linux)
  • copy_file_range() (for Linux)

Operations on the file descriptor table

[edit]

Thefcntl() 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, includingF_GETFD, F_SETFD, F_GETFL andF_SETFL.

  • close()
  • closefrom() (BSD and Solaris only; deletes all file descriptors greater than or equal to specified number)
  • close_range() (for Linux)[6]
  • dup() (duplicates an existing file descriptor guaranteeing to be the lowest number available file descriptor)
  • dup2(),dup3() (Close fd1 if necessary, and make file descriptor fd1 point to the open file of fd2)
  • fcntl (F_DUPFD)

Operations that modify process state

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

File locking

[edit]
  • flock()
  • fcntl() (F_GETLK, F_SETLK andF_SETLKW)
  • lockf()

Sockets

[edit]
See also:Berkeley 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)

Miscellaneous

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

at suffix operations

[edit]

A series of new operations has been added to many modern Unix-like systems, as well as numerous C libraries, to be standardized in a future version ofPOSIX.[7] Theat suffix signifies that the function takes an additional first argument supplying a file descriptor from whichrelative paths are resolved, the forms lacking theat suffix thus becoming equivalent to passing a file descriptor corresponding to the currentworking directory. The purpose of these new operations is to defend against a certain class ofTOCTOU attacks.

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

File descriptors as capabilities

[edit]

Unix file descriptors behave in many ways ascapabilities. They can be passed between processes acrossUnix domain sockets using thesendmsg() 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.[8][9] 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 aC-list.

See also

[edit]

References

[edit]
  1. ^The Open Group."The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008, 2016 Edition". Retrieved2017-09-21.
  2. ^The Open Group."The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008, 2016 Edition". <stdio.h>. Retrieved2017-09-21.
  3. ^abBach, Maurice J. (1986).The Design of the UNIX Operating System (8 ed.).Prentice-Hall. pp. 92–96.ISBN 9780132017992.
  4. ^"Devices - What does the output of 'll /Proc/Self/Fd/' (From 'll /Dev/Fd') mean?".
  5. ^The Open Group."The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008, 2018 Edition – creat". Retrieved2019-04-11.
  6. ^Stephen Kitt, Michael Kerrisk."close_range(2) — Linux manual page". Retrieved2021-03-22.
  7. ^Extended API Set, Part 2. The Open Group. October 2006.ISBN 1931624674.
  8. ^Brinkmann, Marcus (2009-02-04)."Building a bridge: library API's and file descriptors?".cap-talk. Archived fromthe original on 2012-07-30. Retrieved2017-09-21.
  9. ^de Boyne Pollard, Jonathan (2007)."Don't set shared file descriptors to non-blocking I/O mode". Retrieved2017-09-21.
Concepts
Operating systems,
kernels
Programming languages
File systems
Specialised hardware
Types
Properties
Organisation
Operations
Linking
Management
Retrieved from "https://en.wikipedia.org/w/index.php?title=File_descriptor&oldid=1322693006#Operations_on_file_descriptors"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp