CONTRIBUTING.d/patches: Please provide a git-range-diff(1)
[man-pages.git] / man5 / proc_pid_fd.5
blob2e3341ad881e87fdc6e13e6e9942979b5adfd86a
1 .\" Copyright (C) 1994, 1995, Daniel Quinlan <quinlan@yggdrasil.com>
2 .\" Copyright (C) 2002-2008, 2017, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Copyright (C) 2023, Alejandro Colomar <alx@kernel.org>
4 .\"
5 .\" SPDX-License-Identifier: GPL-3.0-or-later
6 .\"
7 .TH proc_pid_fd 5 (date) "Linux man-pages (unreleased)"
8 .SH NAME
9 /proc/pid/fd/ \- file descriptors
10 .SH DESCRIPTION
11 .TP
12 .IR /proc/ pid /fd/
13 This is a subdirectory containing one entry for each file which the
14 process has open, named by its file descriptor, and which is a
15 symbolic link to the actual file.
16 Thus, 0 is standard input, 1 standard output, 2 standard error, and so on.
17 .IP
18 For file descriptors for pipes and sockets,
19 the entries will be symbolic links whose content is the
20 file type with the inode.
22 .BR readlink (2)
23 call on this file returns a string in the format:
24 .IP
25 .in +4n
26 .EX
27 type:[inode]
28 .EE
29 .in
30 .IP
31 For example,
32 .I socket:[2248868]
33 will be a socket and its inode is 2248868.
34 For sockets, that inode can be used to find more information
35 in one of the files under
36 .IR /proc/net/ .
37 .IP
38 For file descriptors that have no corresponding inode
39 (e.g., file descriptors produced by
40 .BR bpf (2),
41 .BR epoll_create (2),
42 .BR eventfd (2),
43 .BR inotify_init (2),
44 .BR perf_event_open (2),
45 .BR signalfd (2),
46 .BR timerfd_create (2),
47 and
48 .BR userfaultfd (2)),
49 the entry will be a symbolic link with contents of the form
50 .IP
51 .in +4n
52 .EX
53 .RI anon_inode: file-type
54 .EE
55 .in
56 .IP
57 In many cases (but not all), the
58 .I file-type
59 is surrounded by square brackets.
60 .IP
61 For example, an epoll file descriptor will have a symbolic link
62 whose content is the string
63 .IR "anon_inode:[eventpoll]" .
64 .IP
65 .\"The following was still true as at kernel 2.6.13
66 In a multithreaded process, the contents of this directory
67 are not available if the main thread has already terminated
68 (typically by calling
69 .BR pthread_exit (3)).
70 .IP
71 Programs that take a filename as a command-line argument,
72 but don't take input from standard input if no argument is supplied,
73 and programs that write to a file named as a command-line argument,
74 but don't send their output to standard output
75 if no argument is supplied, can nevertheless be made to use
76 standard input or standard output by using
77 .IR /proc/ pid /fd
78 files as command-line arguments.
79 For example, assuming that
80 .I \-i
81 is the flag designating an input file and
82 .I \-o
83 is the flag designating an output file:
84 .IP
85 .in +4n
86 .EX
87 .RB "$" " foobar \-i /proc/self/fd/0 \-o /proc/self/fd/1 ..."
88 .EE
89 .in
90 .IP
91 and you have a working filter.
92 .\" The following is not true in my tests (MTK):
93 .\" Note that this will not work for
94 .\" programs that seek on their files, as the files in the fd directory
95 .\" are not seekable.
96 .IP
97 .I /proc/self/fd/N
98 is approximately the same as
99 .I /dev/fd/N
100 in some UNIX and UNIX-like systems.
101 Most Linux MAKEDEV scripts symbolically link
102 .I /dev/fd
104 .IR /proc/self/fd ,
105 in fact.
107 Most systems provide symbolic links
108 .IR /dev/stdin ,
109 .IR /dev/stdout ,
111 .IR /dev/stderr ,
112 which respectively link to the files
113 .IR 0 ,
114 .IR 1 ,
116 .I 2
118 .IR /proc/self/fd .
119 Thus the example command above could be written as:
121 .in +4n
123 .RB "$" " foobar \-i /dev/stdin \-o /dev/stdout ..."
127 Permission to dereference or read
128 .RB ( readlink (2))
129 the symbolic links in this directory is governed by a ptrace access mode
130 .B PTRACE_MODE_READ_FSCREDS
131 check; see
132 .BR ptrace (2).
134 Note that for file descriptors referring to inodes
135 (pipes and sockets, see above),
136 those inodes still have permission bits and ownership information
137 distinct from those of the
138 .IR /proc/ pid /fd
139 entry,
140 and that the owner may differ from the user and group IDs of the process.
141 An unprivileged process may lack permissions to open them, as in this example:
143 .in +4n
145 .RB "$" " echo test | sudo \-u nobody cat"
146 test
147 .RB "$" " echo test | sudo \-u nobody cat /proc/self/fd/0"
148 cat: /proc/self/fd/0: Permission denied
152 File descriptor 0 refers to the pipe created by the shell
153 and owned by that shell's user, which is not
154 .IR nobody ,
156 .B cat
157 does not have permission
158 to create a new file descriptor to read from that inode,
159 even though it can still read from its existing file descriptor 0.
160 .SH SEE ALSO
161 .BR proc (5)