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