xref: /freebsd/usr.bin/fstat/fstat.1 (revision a0b9e2e854027e6ff61fb075a1309dbc71c42b54)
1.\" Copyright (c) 1987, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)fstat.1	8.3 (Berkeley) 2/25/94
29.\" $FreeBSD$
30.\"
31.Dd November 19, 2020
32.Dt FSTAT 1
33.Os
34.Sh NAME
35.Nm fstat
36.Nd identify active files
37.Sh SYNOPSIS
38.Nm
39.Op Fl fmnsv
40.Op Fl M Ar core
41.Op Fl N Ar system
42.Op Fl p Ar pid
43.Op Fl u Ar user
44.Op Ar
45.Sh DESCRIPTION
46The
47.Nm
48utility identifies open files.
49A file is considered open by a process if it was explicitly opened,
50is the working directory, root directory, jail root directory,
51active executable text, or kernel trace file for that process.
52If no options are specified,
53.Nm
54reports on all open files in the system for processes the user has access to.
55.Pp
56The following options are available:
57.Bl -tag -width "-N system"
58.It Fl f
59Restrict examination to files open in the same file systems as
60the named file arguments, or to the file system containing the
61current directory if there are no additional filename arguments.
62For example, to find all files open in the file system where the
63directory
64.Pa /usr/src
65resides, type
66.Ql fstat -f /usr/src .
67.It Fl M Ar core
68Extract values associated with the name list from the specified core
69instead of the default
70.Pa /dev/kmem .
71.It Fl m
72Include memory-mapped files in the listing; normally these are excluded
73due to the extra processing required.
74.It Fl N Ar system
75Extract the name list from the specified system instead of the default,
76which is the kernel image the system has booted from.
77.It Fl n
78Numerical format.
79Print the device number (maj,min) of the file system
80the file resides in rather than the mount point name; for special
81files, print the
82device number that the special device refers to rather than the filename
83in
84.Pa /dev ;
85and print the mode of the file in octal instead of symbolic form.
86.It Fl p Ar pid
87Report all files open by the specified process.
88.It Fl s
89Print socket endpoint information.
90.It Fl u Ar user
91Report all files open by the specified user.
92.It Fl v
93Verbose mode.
94Print error messages upon failures to locate particular
95system data structures rather than silently ignoring them.
96Most of
97these data structures are dynamically created or deleted and it is
98possible for them to disappear while
99.Nm
100is running.
101This
102is normal and unavoidable since the rest of the system is running while
103.Nm
104itself is running.
105.It Ar
106Restrict reports to the specified files.
107.El
108.Pp
109The following fields are printed:
110.Bl -tag -width MOUNT
111.It Sy USER
112The username of the owner of the process (effective uid).
113.It Sy CMD
114The command name of the process.
115.It Sy PID
116The process id.
117.It Sy FD
118The file number in the per-process open file table or one of the following
119special names:
120.Pp
121.Bl -tag -width jail -offset indent -compact
122.It Sy jail
123jail root directory
124.It Sy mmap
125memory-mapped file
126.It Sy root
127root inode
128.It Sy text
129executable text inode
130.It Sy tr
131kernel trace file
132.It Sy wd
133current working directory
134.El
135.Pp
136If the file number is followed by an asterisk
137.Pq Ql * ,
138the file is
139not an inode, but rather a socket, FIFO, or there is an error.
140In this case the remainder of the line does not
141correspond to the remaining headers\(em the format of the line
142is described later under
143.Sx SOCKETS .
144.It Sy MOUNT
145If the
146.Fl n
147flag was not specified, this header is present and is the
148pathname that the file system the file resides in is mounted on.
149.It Sy DEV
150If the
151.Fl n
152flag is specified, this header is present and is the
153number of the device that this file resides in.
154.It Sy INUM
155The inode number of the file.
156.It Sy MODE
157The mode of the file.
158If the
159.Fl n
160flag is not specified, the mode is printed
161using a symbolic format (see
162.Xr strmode 3 ) ;
163otherwise, the mode is printed
164as an octal number.
165.It Sy SZ\&|DV
166If the file is a semaphore,
167prints the current value of the semaphore.
168If the file is not a character or block special, prints the size of
169the file in bytes.
170Otherwise, if the
171.Fl n
172flag is not specified, prints
173the name of the special file as located in
174.Pa /dev .
175If that cannot be
176located, or the
177.Fl n
178flag is specified, prints the major/minor device
179number that the special device refers to.
180.It Sy R/W
181This column describes the access mode that the file allows.
182The letter
183.Ql r
184indicates open for reading;
185the letter
186.Ql w
187indicates open for writing.
188This field is useful when trying to find the processes that are
189preventing a file system from being down graded to read-only.
190.It Sy NAME
191If filename arguments are specified and the
192.Fl f
193flag is not, then
194this field is present and is the name associated with the given file.
195Normally the name cannot be determined since there is no mapping
196from an open file back to the directory entry that was used to open
197that file.
198Also, since different directory entries may reference
199the same file (via
200.Xr ln 1 ) ,
201the name printed may not be the actual
202name that the process originally used to open that file.
203.El
204.Sh SOCKETS
205The formatting of open sockets depends on the protocol domain.
206In all cases the first field is the domain name, the second field
207is the socket type (stream, dgram, etc.), and the third is the socket
208flags field (in hex).
209The remaining fields are protocol dependent.
210For TCP, it is the address of the tcpcb, and for UDP, the inpcb (socket pcb).
211For UNIX-domain sockets, its the address of the socket pcb and the address
212of the connected pcb (if connected).
213Otherwise the protocol number and address of the socket itself are printed.
214.Pp
215For example, the addresses mentioned above are the addresses which the
216.Ql netstat -A
217command would print for TCP, UDP, and UNIX-domain.
218Note that since pipes are implemented using sockets, a pipe appears as a
219connected UNIX-domain stream socket.
220A unidirectional UNIX-domain socket indicates the direction of flow with
221an arrow
222.Po Ql <-
223or
224.Ql ->
225.Pc ,
226and a full duplex socket shows a double arrow
227.Pq Ql <-> .
228.Pp
229When the
230.Fl s
231flag is used, socket endpoint information is shown after the address of the
232socket.
233For internet sockets the local and remote addresses are shown, separated with
234a double arrow
235.Pq Ql <-> .
236For UNIX/local sockets either the local or remote address is shown, depending
237on which one is available.
238.Sh EXIT STATUS
239.Ex -std
240.Sh EXAMPLES
241Show all open files except those opened by
242.Nm
243itself:
244.Bd -literal -offset indent
245$ fstat | awk '$2 != "fstat"'
246USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W
247alice  bash         469 text /usr/local 143355 -rwxr-xr-x  1166448  r
248alice  bash         469 ctty /dev        346 crw--w----  pts/81 rw
249\&...
250.Ed
251.Pp
252Report all files opened by the current shell in the same file system as
253.Pa /usr/local
254including memory-mapped files:
255.Bd -literal -offset indent
256$ fstat -m -p $$ -f /usr/local
257USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W
258bob  bash         469 text /usr/local 143355 -rwxr-xr-x  1166448  r
259bob  bash         469 mmap /usr/local 143355 -rwxr-xr-x  1166448  r
260\&...
261.Ed
262.Pp
263Requesting information about a file that is not opened results in just a
264header line instead of an error:
265.Bd -literal -offset indent
266$ fstat /etc/rc.conf
267USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W NAME
268.Ed
269.Pp
270All parameters after
271.Fl f
272will be interpreted as files, so the following will not work as expected:
273.Bd -literal -offset indent
274$ fstat -f /usr/local -m -p $$
275fstat: -m: No such file or directory
276fstat: -p: No such file or directory
277fstat: 469: No such file or directory
278\&...
279.Ed
280.Pp
281Show number of pipes opened by firefox processes:
282.Bd -literal -offset indent
283$ fstat | awk '$2=="firefox" && $5=="pipe"' | wc -l
284.Ed
285.Pp
286Show processes belonging to user
287.Dq bob
288whose standard error descriptor is opened in ttyv0:
289.Bd -literal -offset indent
290$ fstat -u bob | awk '$4 == 2 && $8 == "ttyv0"'
291bob  firefox    77842    2 /dev        103 crw-------   ttyv0 rw
292bob  xinit       1194    2 /dev        103 crw-------   ttyv0 rw
293\&...
294.Ed
295.Pp
296Show opened TCP sockets.
297This output resembles the one produced by
298.Ql netstat -A -p tcp
299:
300.Bd -literal -offset indent
301$ fstat | awk '$7 == "tcp"'
302alice  firefox    77991   32* internet stream tcp fffff800b7f147a0
303alice  firefox    77991  137* internet stream tcp fffff800b7f12b70
304\&...
305.Ed
306.Pp
307Show a list of processes with files opened in the current directory
308mimicking the output of
309.Xr fuser 1
310:
311.Bd -literal -offset indent
312$ fstat . | awk 'NR > 1 {printf "%d%s(%s) ", $3, $4, $1;}'
3132133wd(alice) 2132wd(alice) 1991wd(alice)
314.Ed
315.Pp
316Create a list of processes sorted by number of opened files in desdencing order:
317.Bd -literal -offset indent
318$ fstat | awk 'NR > 1 {print $2;}' | sort | uniq -c | sort -r
319 728 firefox
320  23 bash
321  14 sort
322   8 fstat
323   7 awk
324.Ed
325.Sh SEE ALSO
326.Xr fuser 1 ,
327.Xr netstat 1 ,
328.Xr nfsstat 1 ,
329.Xr procstat 1 ,
330.Xr ps 1 ,
331.Xr sockstat 1 ,
332.Xr systat 1 ,
333.Xr tcp 4 ,
334.Xr unix 4 ,
335.Xr iostat 8 ,
336.Xr pstat 8 ,
337.Xr vmstat 8
338.Sh HISTORY
339The
340.Nm
341command appeared in
342.Bx 4.3 tahoe .
343.Sh BUGS
344Since
345.Nm
346takes a snapshot of the system, it is only correct for a very short period
347of time.
348