Debian Codegraph @

upstream/debian-unstable/man5/proc_pid_fd.5

raw
1 .\" Copyright 1994-1995, Daniel Quinlan <[email protected]>
2 .\" Copyright 2002-2017, Michael Kerrisk <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: GPL-3.0-or-later
6 .\"
7 .TH proc_pid_fd 5 2026-02-08 "Linux man-pages 6.18"
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.
21 A
22 .BR readlink (2)
23 call on this file returns a string in the format:
24 .IP
25 .in +4n
26 .EX
27 .IR 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
103 to
104 .IR /proc/self/fd ,
105 in fact.
106 .IP
107 Most systems provide symbolic links
108 .IR /dev/stdin ,
109 .IR /dev/stdout ,
110 and
111 .IR /dev/stderr ,
112 which respectively link to the files
113 .IR 0 ,
114 .IR 1 ,
115 and
116 .I 2
117 in
118 .IR /proc/self/fd .
119 Thus the example command above could be written as:
120 .IP
121 .in +4n
122 .EX
123 .RB "$" " foobar \-i /dev/stdin \-o /dev/stdout ..."
124 .EE
125 .in
126 .IP
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;
132 see
133 .BR ptrace (2).
134 .IP
135 Note that for file descriptors referring to inodes
136 (pipes and sockets, see above),
137 those inodes still have permission bits and ownership information
138 distinct from those of the
139 .IR /proc/ pid /fd
140 entry,
141 and that the owner may differ from the user and group IDs of the process.
142 An unprivileged process may lack permissions to open them, as in this example:
143 .IP
144 .in +4n
145 .EX
146 .RB "$" " echo test | sudo \-u nobody cat"
147 test
148 .RB "$" " echo test | sudo \-u nobody cat /proc/self/fd/0"
149 cat: /proc/self/fd/0: Permission denied
150 .EE
151 .in
152 .IP
153 File descriptor 0 refers to the pipe created by the shell
154 and owned by that shell's user, which is not
155 .IR nobody ,
156 so
157 .B cat
158 does not have permission
159 to create a new file descriptor to read from that inode,
160 even though it can still read from its existing file descriptor 0.
161 .SH SEE ALSO
162 .BR proc (5)