From owner-svn-src-head@freebsd.org Thu Nov 19 19:05:17 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 3DCA04725C0; Thu, 19 Nov 2020 19:05:17 +0000 (UTC) (envelope-from fernape@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CcTdn0tR7z3HqH; Thu, 19 Nov 2020 19:05:17 +0000 (UTC) (envelope-from fernape@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 06A6FBE9; Thu, 19 Nov 2020 19:05:17 +0000 (UTC) (envelope-from fernape@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0AJJ5Gs5077226; Thu, 19 Nov 2020 19:05:16 GMT (envelope-from fernape@FreeBSD.org) Received: (from fernape@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AJJ5GDZ077225; Thu, 19 Nov 2020 19:05:16 GMT (envelope-from fernape@FreeBSD.org) Message-Id: <202011191905.0AJJ5GDZ077225@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: fernape set sender to fernape@FreeBSD.org using -f From: =?UTF-8?Q?Fernando_Apestegu=c3=ada?= Date: Thu, 19 Nov 2020 19:05:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367851 - head/usr.bin/fstat X-SVN-Group: head X-SVN-Commit-Author: fernape X-SVN-Commit-Paths: head/usr.bin/fstat X-SVN-Commit-Revision: 367851 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Nov 2020 19:05:17 -0000 Author: fernape (ports committer) Date: Thu Nov 19 19:05:16 2020 New Revision: 367851 URL: https://svnweb.freebsd.org/changeset/base/367851 Log: fstat(1): Add EXAMPLES section * Add examples covering -f, -m and -p flags. While here, extend the initial description paragraph to note that fstat(1) will report on all opened files, belonging to processes the user has access to. The current paragraph may lead to understand that you can get information on opened files from processes belonging to other users. Reviewed by: bjk@, danfe@, gbe@ Approved by: manpages (gbe@) Differential Revision: https://reviews.freebsd.org/D26949 Modified: head/usr.bin/fstat/fstat.1 Modified: head/usr.bin/fstat/fstat.1 ============================================================================== --- head/usr.bin/fstat/fstat.1 Thu Nov 19 18:58:15 2020 (r367850) +++ head/usr.bin/fstat/fstat.1 Thu Nov 19 19:05:16 2020 (r367851) @@ -28,7 +28,7 @@ .\" @(#)fstat.1 8.3 (Berkeley) 2/25/94 .\" $FreeBSD$ .\" -.Dd June 17, 2020 +.Dd November 19, 2020 .Dt FSTAT 1 .Os .Sh NAME @@ -51,7 +51,7 @@ is the working directory, root directory, jail root di active executable text, or kernel trace file for that process. If no options are specified, .Nm -reports on all open files in the system. +reports on all open files in the system for processes the user has access to. .Pp The following options are available: .Bl -tag -width "-N system" @@ -118,7 +118,7 @@ The process id. The file number in the per-process open file table or one of the following special names: .Pp -.Bl -tag -offset indent -compact +.Bl -tag -width jail -offset indent -compact .It Sy jail jail root directory .It Sy mmap @@ -235,6 +235,93 @@ a double arrow .Pq Ql <-> . For UNIX/local sockets either the local or remote address is shown, depending on which one is available. +.Sh EXIT STATUS +.Ex -std +.Sh EXAMPLES +Show all open files except those opened by +.Nm +itself: +.Bd -literal -offset indent +$ fstat | awk '$2 != "fstat"' +USER CMD PID FD MOUNT INUM MODE SZ|DV R/W +alice bash 469 text /usr/local 143355 -rwxr-xr-x 1166448 r +alice bash 469 ctty /dev 346 crw--w---- pts/81 rw +\&... +.Ed +.Pp +Report all files opened by the current shell in the same file system as +.Pa /usr/local +including memory-mapped files: +.Bd -literal -offset indent +$ fstat -m -p $$ -f /usr/local +USER CMD PID FD MOUNT INUM MODE SZ|DV R/W +bob bash 469 text /usr/local 143355 -rwxr-xr-x 1166448 r +bob bash 469 mmap /usr/local 143355 -rwxr-xr-x 1166448 r +\&... +.Ed +.Pp +Requesting information about a file that is not opened results in just a +header line instead of an error: +.Bd -literal -offset indent +$ fstat /etc/rc.conf +USER CMD PID FD MOUNT INUM MODE SZ|DV R/W NAME +.Ed +.Pp +All parameters after +.Fl f +will be interpreted as files, so the following will not work as expected: +.Bd -literal -offset indent +$ fstat -f /usr/local -m -p $$ +fstat: -m: No such file or directory +fstat: -p: No such file or directory +fstat: 469: No such file or directory +\&... +.Ed +.Pp +Show number of pipes opened by firefox processes: +.Bd -literal -offset indent +$ fstat | awk '$2=="firefox" && $5=="pipe"' | wc -l +.Ed +.Pp +Show processes belonging to user +.Dq bob +whose standard error descriptor is opened in ttyv0: +.Bd -literal -offset indent +$ fstat -u bob | awk '$4 == 2 && $8 == "ttyv0"' +bob firefox 77842 2 /dev 103 crw------- ttyv0 rw +bob xinit 1194 2 /dev 103 crw------- ttyv0 rw +\&... +.Ed +.Pp +Show opened TCP sockets. +This output resembles the one produced by +.Ql netstat -A -p tcp +: +.Bd -literal -offset indent +$ fstat | awk '$7 == "tcp"' +alice firefox 77991 32* internet stream tcp fffff800b7f147a0 +alice firefox 77991 137* internet stream tcp fffff800b7f12b70 +\&... +.Ed +.Pp +Show a list of processes with files opened in the current directory +mimicking the output of +.Xr fuser 1 +: +.Bd -literal -offset indent +$ fstat . | awk 'NR > 1 {printf "%d%s(%s) ", $3, $4, $1;}' +2133wd(alice) 2132wd(alice) 1991wd(alice) +.Ed +.Pp +Create a list of processes sorted by number of opened files in desdencing order: +.Bd -literal -offset indent +$ fstat | awk 'NR > 1 {print $2;}' | sort | uniq -c | sort -r + 728 firefox + 23 bash + 14 sort + 8 fstat + 7 awk +.Ed .Sh SEE ALSO .Xr fuser 1 , .Xr netstat 1 ,