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