Blame 00QUICKSTART

Packit Service 603f59
Packit Service 603f59
			A Quick Start for Lsof
Packit Service 603f59
Packit Service 603f59
1.  Introduction
Packit Service 603f59
================
Packit Service 603f59
Packit Service 603f59
  Agreed, the lsof man page is dense and lsof has a plethora of
Packit Service 603f59
  options.  There are examples, but the manual page format buries
Packit Service 603f59
  them at the end.  How does one get started with lsof?
Packit Service 603f59
Packit Service 603f59
  This file is an attempt to answer that question.  It plunges
Packit Service 603f59
  immediately into examples of lsof use to solve problems that
Packit Service 603f59
  involve looking at the open files of Unix processes.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
			    Contents
Packit Service 603f59
Packit Service 603f59
	    1.  Introduction
Packit Service 603f59
	    2.  Finding Uses of a Specific Open File
Packit Service 603f59
	    3.  Finding Open Files Filling a File System
Packit Service 603f59
		a.  Finding an Unlinked Open File
Packit Service 603f59
	    4.  Finding Processes Blocking Umount
Packit Service 603f59
	    5.  Finding Listening Sockets
Packit Service 603f59
	    6.  Finding a Particular Network Connection
Packit Service 603f59
	    7.  Identifying a Netstat Connection
Packit Service 603f59
	    8.  Finding Files Open to a Named Command
Packit Service 603f59
	    9.  Deciphering the Remote Login Trail
Packit Service 603f59
		a.  The Fundamentals
Packit Service 603f59
		b.  The idrlogin.perl[5] Scripts
Packit Service 603f59
	    10. Watching an Ftp or Rcp Transfer
Packit Service 603f59
	    11. Listing Open NFS Files
Packit Service 603f59
	    12. Listing Files Open by a Specific Login
Packit Service 603f59
		a.  Ignoring a Specific Login
Packit Service 603f59
	    13. Listing Files Open to a Specific Process Group
Packit Service 603f59
	    14. When Lsof Seems to Hang
Packit Service 603f59
		a.  Kernel lstat(), readlink(), and stat() Blockages
Packit Service 603f59
		b.  Problems with /dev or /devices
Packit Service 603f59
		c.  Host and Service Name Lookup Hangs
Packit Service 603f59
		d.  UID to Login Name Conversion Delays
Packit Service 603f59
	    15. Output for Other Programs
Packit Service 603f59
	    16. The Lsof Exit Code and Shell Scripts
Packit Service 603f59
	    17. Strange messages in the NAME column
Packit Service 603f59
Packit Service 603f59
			Options
Packit Service 603f59
Packit Service 603f59
	    A.  Selection Options
Packit Service 603f59
	    B.  Output Options
Packit Service 603f59
	    C.  Precautionary Options
Packit Service 603f59
	    D.  Miscellaneous Lsof Options
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
2.  Finding Uses of a Specific Open File
Packit Service 603f59
========================================
Packit Service 603f59
Packit Service 603f59
  Often you're interested in knowing who is using a specific file.
Packit Service 603f59
  You know the path to it and you want lsof to tell you the processes
Packit Service 603f59
  that have open references to it.
Packit Service 603f59
Packit Service 603f59
  Simple -- execute lsof and give it the path name of the file of
Packit Service 603f59
  interest -- e.g.,
Packit Service 603f59
Packit Service 603f59
  $ lsof /etc/passwd
Packit Service 603f59
Packit Service 603f59
  Caveat: this only works if lsof has permission to get the status
Packit Service 603f59
  (via stat(2)) of the file at the named path.  Unless the lsof
Packit Service 603f59
  process has enough authority  -- e.g., it is being run with a
Packit Service 603f59
  real User ID (UID) of root -- this AIX example won't work:
Packit Service 603f59
Packit Service 603f59
  Further caveat: this use of lsof will fail if the stat(2) kernel
Packit Service 603f59
  syscall returns different file parameters -- particularly device
Packit Service 603f59
  and inode numbers -- than lsof finds in kernel node structures.
Packit Service 603f59
  This condition is rare and is usually documented in the 00FAQ
Packit Service 603f59
  file of the lsof distribution.
Packit Service 603f59
Packit Service 603f59
  $ lsof /etc/security/passwd
Packit Service 603f59
  lsof: status error on /etc/security/passwd: Permission denied
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
3.  Finding Open Files Filling a File System
Packit Service 603f59
============================================
Packit Service 603f59
Packit Service 603f59
  Oh! Oh!  /tmp is filling and ls doesn't show that any large files
Packit Service 603f59
  are being created.  Can lsof help?
Packit Service 603f59
Packit Service 603f59
  Maybe.  If there's a process that is writing to a file that has
Packit Service 603f59
  been unlinked, lsof may be able to discover the process for you.
Packit Service 603f59
  You ask it to list all open files on the file system where /tmp
Packit Service 603f59
  is located.
Packit Service 603f59
Packit Service 603f59
  Sometimes /tmp is a file system by itself.  In that case,
Packit Service 603f59
Packit Service 603f59
  $ lsof /tmp
Packit Service 603f59
Packit Service 603f59
  is the appropriate command.  If, however, /tmp is part of another
Packit Service 603f59
  file system, typically /, then you may have to ask lsof to list
Packit Service 603f59
  all files open on the containing file system and locate the
Packit Service 603f59
  offending file and its process by inspection -- e.g.,
Packit Service 603f59
Packit Service 603f59
    $ lsof / | more
Packit Service 603f59
  or
Packit Service 603f59
    $ lsof / | grep ...
Packit Service 603f59
Packit Service 603f59
  Caveat: there must be a file open to a for the lsof search to
Packit Service 603f59
  succeed.  Sometimes the kernel may cause a file reference to
Packit Service 603f59
  persist, even where there's no file open to a process.  (Can you
Packit Service 603f59
  say kernel bug?  Maybe.)  In any event, lsof won't be able to
Packit Service 603f59
  help in this case.
Packit Service 603f59
Packit Service 603f59
  a.  Finding an Unlinked Open File
Packit Service 603f59
  =================================
Packit Service 603f59
Packit Service 603f59
  A pesky variant of a file that is filling a file system is an
Packit Service 603f59
  unlinked file to which some process is still writing.  When a
Packit Service 603f59
  process opens a file and then unlinks it, the file's resources
Packit Service 603f59
  remain in use by the process, but the file's directory entries
Packit Service 603f59
  are removed.  Hence, even when you know the directory where the
Packit Service 603f59
  file once resided, you can't detect it with ls.
Packit Service 603f59
Packit Service 603f59
  This can be an administrative problem when the unlinked file is
Packit Service 603f59
  large, and the process that holds it open continues to write to
Packit Service 603f59
  it.  Only when the process closes the file will its resources,
Packit Service 603f59
  particularly disk space, be released.
Packit Service 603f59
Packit Service 603f59
  Lsof can help you find unlinked files on local disks.  It has an
Packit Service 603f59
  option, +L, that will list the link counts of open files.  That
Packit Service 603f59
  helps because an unlinked file on a local disk has a zero link
Packit Service 603f59
  count.  Note: this is NOT true for NFS files, accessed from a
Packit Service 603f59
  remote server.
Packit Service 603f59
Packit Service 603f59
  You could use the option to list all files and look for a zero
Packit Service 603f59
  link count in the NLINK column -- e.g.,
Packit Service 603f59
Packit Service 603f59
    $lsof +L
Packit Service 603f59
    COMMAND   PID USER   FD  TYPE DEVICE SIZE/OFF NLINK  NODE NAME
Packit Service 603f59
    ...
Packit Service 603f59
    less    25366  abe  txt  VREG    6,0    40960     1 76319 /usr/...
Packit Service 603f59
    ...
Packit Service 603f59
  > less    25366  abe    3r VREG    6,0    17360     0 98768 / (/dev/sd0a)
Packit Service 603f59
Packit Service 603f59
  Better yet, you can specify an upper bound to the +L option, and
Packit Service 603f59
  lsof will select only files that have a link count less than the
Packit Service 603f59
  upper bound.  For example:
Packit Service 603f59
Packit Service 603f59
    $ lsof +L1
Packit Service 603f59
    COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NLINK  NODE NAME
Packit Service 603f59
    less    25366  abe    3r  VREG    6,0    17360     0 98768 / (/dev/sd0a)
Packit Service 603f59
Packit Service 603f59
  You can use lsof's -a (AND) option to narrow the link count search
Packit Service 603f59
  to a particular file system.  For example, to look for zero link
Packit Service 603f59
  counts on the /home file system, use:
Packit Service 603f59
Packit Service 603f59
    $ lsof -a +L1 /home
Packit Service 603f59
Packit Service 603f59
  CAUTION: lsof can't always report link counts for all file types
Packit Service 603f59
  -- e.g., it may not report them for FIFOs, pipes, or sockets.
Packit Service 603f59
  Remember also that link counts for NFS files on an NFS client
Packit Service 603f59
  host don't behave as do link counts for files on local disks.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
4.  Finding Processes Blocking Umount
Packit Service 603f59
=====================================
Packit Service 603f59
Packit Service 603f59
  When you need to unmount a file system with the umount command,
Packit Service 603f59
  you may find the operation blocked by a process that has a file
Packit Service 603f59
  open on the file systems.  Lsof may be able to help you find the
Packit Service 603f59
  process.  In response to:
Packit Service 603f59
Packit Service 603f59
  $ lsof <file_system_name>
Packit Service 603f59
Packit Service 603f59
  Lsof will display all open files on the named file system.  It
Packit Service 603f59
  will also set its exit code zero when it finds some open files
Packit Service 603f59
  and non-zero when it doesn't, making this type of lsof call
Packit Service 603f59
  useful in shell scripts.  (See section 16.)
Packit Service 603f59
Packit Service 603f59
  Consult the output of the df command for file system names.
Packit Service 603f59
Packit Service 603f59
  See the caveat in the preceding section about file references
Packit Service 603f59
  that persist in the kernel without open file traces.  That
Packit Service 603f59
  situation may hamper lsof's ability to help with umount, too.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
5.  Finding Listening Sockets
Packit Service 603f59
=============================
Packit Service 603f59
Packit Service 603f59
  Sooner or later you may wonder if someone has installed a network
Packit Service 603f59
  server that you don't know about.  Lsof can list for you all the
Packit Service 603f59
  network socket files open on your machine with:
Packit Service 603f59
Packit Service 603f59
    $ lsof -i
Packit Service 603f59
Packit Service 603f59
  The -i option without further qualification lists all open Internet
Packit Service 603f59
  socket files.  You can add network names or addresses, protocol
Packit Service 603f59
  names, and service names or port numbers to the -i option to
Packit Service 603f59
  refine the search.  (See the next section.)
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
6.  Finding a Particular Network Connection
Packit Service 603f59
===========================================
Packit Service 603f59
Packit Service 603f59
  When you know the source or destination of a network connection
Packit Service 603f59
  whose open files and process you'd like to identify, the -i option
Packit Service 603f59
  may help.
Packit Service 603f59
Packit Service 603f59
  If, for example, you want to know what process has a connection
Packit Service 603f59
  open to or from the Internet host named aaa.bbb.ccc, you can ask
Packit Service 603f59
  lsof to search for it with:
Packit Service 603f59
Packit Service 603f59
  $ lsof -i@aaa.bbb.ccc
Packit Service 603f59
Packit Service 603f59
  If you're interested in a particular protocol -- TCP or UDP --
Packit Service 603f59
  and a specific port number or service name, you can add those
Packit Service 603f59
  discriminators to the -i information:
Packit Service 603f59
Packit Service 603f59
  $ lsof -iTCP@aaa.bbb.ccc:ftp-data
Packit Service 603f59
Packit Service 603f59
  If you're interested in a particular IP version -- IPv4 or IPv6
Packit Service 603f59
  -- and your UNIX dialect supports both (It does if "IPv[46]"
Packit Service 603f59
  appears in the lsof -h output.), you can add the '4' or '6'
Packit Service 603f59
  selector immediately after -i:
Packit Service 603f59
Packit Service 603f59
  $ lsof -i4
Packit Service 603f59
  $ lsof -i6
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
7.  Identifying a Netstat Connection
Packit Service 603f59
====================================
Packit Service 603f59
Packit Service 603f59
  How do I identify the process that has a network connection
Packit Service 603f59
  described in netstat output?  For example, if netstat says:
Packit Service 603f59
Packit Service 603f59
  Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
Packit Service 603f59
  tcp        0      0  vic.1023               ipscgate.login         ESTABLISHED
Packit Service 603f59
Packit Service 603f59
  What process is connected to service name ``login'' on ipscgate?
Packit Service 603f59
Packit Service 603f59
  Use lsof's -i option:
Packit Service 603f59
Packit Service 603f59
  $lsof -iTCP@ipscgate:login
Packit Service 603f59
  COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
Packit Service 603f59
  rlogin    25023      abe    3u  inet 0x10144168      0t184    TCP lsof.itap.purdue.edu:1023->ipscgate.cc.purdue.edu:login
Packit Service 603f59
  ...
Packit Service 603f59
Packit Service 603f59
  There's another way.  Notice the 0x10144168 in the DEVICE column
Packit Service 603f59
  of the lsof output?  That's the protocol control block (PCB)
Packit Service 603f59
  address.  Many netstat applications will display it when given
Packit Service 603f59
  the -A option:
Packit Service 603f59
Packit Service 603f59
  $ netstat -A
Packit Service 603f59
  PCB      Proto Recv-Q Send-Q  Local Address      Foreign Address    (state)
Packit Service 603f59
  10144168 tcp        0      0  vic.1023           ipscgate.login     ESTABLISHED
Packit Service 603f59
  ...
Packit Service 603f59
Packit Service 603f59
  Using the PCB address, lsof, and grep, you can find the process this
Packit Service 603f59
  way, too:
Packit Service 603f59
Packit Service 603f59
  $ lsof -i | grep 10144168
Packit Service 603f59
  rlogin    25023      abe    3u  inet 0x10144168      0t184    TCP lsof.itap.purdue.edu:1023->ipscgate.cc.purdue.edu:login
Packit Service 603f59
  ...
Packit Service 603f59
Packit Service 603f59
  If the file is a UNIX socket and netstat reveals and adress for it,
Packit Service 603f59
  like this Solaris 11 example:
Packit Service 603f59
Packit Service 603f59
  $ netstat -a -f unix
Packit Service 603f59
  Active UNIX domain sockets
Packit Service 603f59
  Address  Type          Vnode     Conn  Local Addr      Remote Addr
Packit Service 603f59
  ffffff0084253b68 stream-ord 0000000 0000000
Packit Service 603f59
Packit Service 603f59
  Using lsof's -U option and its output piped to a grep on the address
Packit Service 603f59
  yields:
Packit Service 603f59
Packit Service 603f59
  $ lsof -U | grep ffffff0084253b68
Packit Service 603f59
  squid 1638 nobody 12u unix 18,98 0t10 9437188 /devices/pseudo/tl@0:ticots->0xffffff0084253b68 stream-ord
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
8.  Finding Files Open to a Named Command
Packit Service 603f59
=========================================
Packit Service 603f59
Packit Service 603f59
  When you want to look at the files open to a particular command,
Packit Service 603f59
  you can look up the PID of the process running the command and
Packit Service 603f59
  use lsof's -p option to specify it.
Packit Service 603f59
Packit Service 603f59
  $ lsof -p <PID>
Packit Service 603f59
Packit Service 603f59
  However, there's a quicker way, using lsof's -c option, provided
Packit Service 603f59
  you don't mind seeing output for every process running the named
Packit Service 603f59
  command.
Packit Service 603f59
Packit Service 603f59
  $ lsof -c <first_characters_of_command_name_that_interest_you>
Packit Service 603f59
Packit Service 603f59
  The lsof -c option is useful when you want to see how many instances
Packit Service 603f59
  of a given command are executing and what their open files are.
Packit Service 603f59
  One useful example is for the sendmail command.
Packit Service 603f59
Packit Service 603f59
  $ lsof -c sendmail
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
9.  Deciphering the Remote Login Trail
Packit Service 603f59
======================================
Packit Service 603f59
Packit Service 603f59
  If the network connection you're interested in tracing has been
Packit Service 603f59
  initiated externally and is connected to an rlogind, sshd, or
Packit Service 603f59
  telnetd process, asking lsof to identify that process might not
Packit Service 603f59
  give a wholly satisfying answer.  The report may be that the
Packit Service 603f59
  connection exists, but to a process owned by root.
Packit Service 603f59
Packit Service 603f59
  a.  The Fundamentals
Packit Service 603f59
  ====================
Packit Service 603f59
Packit Service 603f59
    How do you get from there to the login name really using the
Packit Service 603f59
    connection?  You have to know a little about how real and pseudo
Packit Service 603f59
    ttys are paired in your system, and then use several lsof probes
Packit Service 603f59
    to identify the login.
Packit Service 603f59
Packit Service 603f59
    This example comes from a Solaris 2.4 system, named klaatu.cc.
Packit Service 603f59
    I've logged on to it via rlogin from lsof.itap.  The first lsof
Packit Service 603f59
    probe,
Packit Service 603f59
Packit Service 603f59
    $ lsof -i@lsof.itap
Packit Service 603f59
Packit Service 603f59
    yields (among other things):
Packit Service 603f59
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
Packit Service 603f59
    in.rlogin  7362     root    0u  inet 0xfc0193b0      0t242    TCP klaatu.cc.purdue.edu:login->lsof.itap.purdue.edu:1023
Packit Service 603f59
    ...
Packit Service 603f59
Packit Service 603f59
    This confirms that a connection exists.  A second lsof probe
Packit Service 603f59
    shows:
Packit Service 603f59
Packit Service 603f59
    $ lsof -p7362
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
Packit Service 603f59
    ...
Packit Service 603f59
    in.rlogin  7362     root    0u  inet 0xfc0193b0      0t242    TCP klaatu.cc.purdue.edu:login->lsof.itap.purdue.edu:1023
Packit Service 603f59
    ...
Packit Service 603f59
    in.rlogin  7362     root    3u  VCHR    23,   0       0t66  52928 /devices/pseudo/clone@0:ptmx->pckt->ptm
Packit Service 603f59
Packit Service 603f59
    7362 is the Process ID (PID) of the in.rlogin process, discovered
Packit Service 603f59
    in the first lsof probe.  (I've abbreviated the output to simplify
Packit Service 603f59
    the example.)  Now comes a need to understand Solaris pseudo-ttys.
Packit Service 603f59
    The key indicator is in the DEVICE column for FD 3, the major/minor
Packit Service 603f59
    device number of 23,0.  This translates to /dev/pts/0, so a third
Packit Service 603f59
    lsof probe,
Packit Service 603f59
Packit Service 603f59
    $ lsof /dev/pts/0
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
Packit Service 603f59
    ksh        7364      abe    0u  VCHR    24,   0     0t2410  53410 /dev/pts/../../devices/pseudo/pts@0:0
Packit Service 603f59
Packit Service 603f59
    shows in part that login abe has a ksh process on /dev/pts/0.
Packit Service 603f59
    (The NAME that lsof shows is not /dev/pts/0 but the full expansion
Packit Service 603f59
    of the symbolic link that lsof finds at /dev/pts/0.)
Packit Service 603f59
Packit Service 603f59
    Here's a second example, done on an HP-UX 9.01 host named ghg.ecn.
Packit Service 603f59
    Again, I've logged on to it from lsof.itap, so I start with:
Packit Service 603f59
Packit Service 603f59
    $ lsof -i@lsof.itap
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE       DEVICE   SIZE/OFF  INODE NAME
Packit Service 603f59
    rlogind   10214     root    0u  inet   0x041d5f00     0t1536    TCP ghg.ecn.purdue.edu:login->lsof.itap.purdue.edu:1023
Packit Service 603f59
    ...
Packit Service 603f59
Packit Service 603f59
    Then,
Packit Service 603f59
Packit Service 603f59
    $ lsof -p10214
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE       DEVICE   SIZE/OFF  INODE NAME
Packit Service 603f59
    ...
Packit Service 603f59
    rlogind   10214     root    0u  inet   0x041d5f00     0t2005    TCP ghg.ecn.purdue.edu:login->lsof.itap.purdue.edu:1023
Packit Service 603f59
    ...
Packit Service 603f59
    rlogind   10214     root    3u  VCHR  16,0x000030     0t2037  24642 /dev/ptym/ptys0
Packit Service 603f59
Packit Service 603f59
    Here the key is the NAME /dev/ptym/ptys0.  In HP-UX 9.01 tty and
Packit Service 603f59
    pseudo tty devices are paired with the names like /dev/ptym/ptys0
Packit Service 603f59
    and /dev/pty/ttys0, so the following lsof probe is the final step.
Packit Service 603f59
Packit Service 603f59
    $ lsof /dev/pty/ttys0
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE       DEVICE   SIZE/OFF  INODE NAME
Packit Service 603f59
    ksh       10215      abe    0u  VCHR  17,0x000030     0t3399  22607 /dev/pty/ttys0
Packit Service 603f59
    ...
Packit Service 603f59
Packit Service 603f59
    Here's a third example for an AIX 4.1.4 system.  I've used telnet
Packit Service 603f59
    to connect to it from lsof.itap.purdue.edu.  I start with:
Packit Service 603f59
Packit Service 603f59
    $ lsof -i@lsof.itap.purdue.edu
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF      INODE NAME
Packit Service 603f59
    ...
Packit Service 603f59
    telnetd   15616     root    0u  inet 0x05a93400     0t5156        TCP cloud.cc.purdue.edu:telnet->lsof.itap.purdue.edu:3369
Packit Service 603f59
Packit Service 603f59
    Then I look at the telnetd process:
Packit Service 603f59
Packit Service 603f59
    $ lsof -p15616
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF      INODE NAME
Packit Service 603f59
    ...
Packit Service 603f59
    telnetd   15616     root    0u  inet 0x05a93400     0t5641        TCP cloud.cc.purdue.edu:telnet->lsof.itap.purdue.edu:3369
Packit Service 603f59
    ...
Packit Service 603f59
    telnetd   15616     root    3u  VCHR    25,   0     0t5493        103 /dev/ptc/0
Packit Service 603f59
Packit Service 603f59
    Here the key is /dev/ptc/0.  In AIX it's paired with /dev/pts/0.
Packit Service 603f59
    The last probe for that shows:
Packit Service 603f59
Packit Service 603f59
    $ lsof /dev/pts/0
Packit Service 603f59
    COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF      INODE NAME
Packit Service 603f59
    ...
Packit Service 603f59
    ksh       16642      abe    0u  VCHR    26,   0     0t6461        360 /dev/pts/0
Packit Service 603f59
Packit Service 603f59
  b.  The idrlogin.perl[5] Scripts
Packit Service 603f59
  ================================
Packit Service 603f59
Packit Service 603f59
    There's another, perhaps easier way, to go about the job of
Packit Service 603f59
    tracing a network connection.  The lsof distribution contains
Packit Service 603f59
    two Perl scripts, idrlogin.perl (Perl 4) and idrlogin.perl5
Packit Service 603f59
    (Perl 5), that use lsof field output to display values for
Packit Service 603f59
    shells that are parented by rlogind, sshd, or telnetd, or
Packit Service 603f59
    connected directly to TCP sockets.  The lsof test suite contains
Packit Service 603f59
    a C library that can be adapted for use with C programs that
Packit Service 603f59
    need to call lsof and process its field output.
Packit Service 603f59
Packit Service 603f59
    The two Perl scripts use the lsof -R option; it causes the
Packit Service 603f59
    paRent process ID (PPID) to be listed in the lsof output.  The
Packit Service 603f59
    scripts identify all shell processes -- e.g., ones whose command
Packit Service 603f59
    names end in ``sh'' -- and determine if: 1) the ultimate ancestor
Packit Service 603f59
    process before a PID greater than 2 (e.g., init's PID is 1) is
Packit Service 603f59
    rlogind, sshd, or telnetd; or 2) the shell process has open
Packit Service 603f59
    TCP socket files.
Packit Service 603f59
Packit Service 603f59
    Here's an example of output from idlogin.perl on a Solaris 2.4
Packit Service 603f59
    system:
Packit Service 603f59
Packit Service 603f59
    centurion: 1 = cd src/lsof4/scripts
Packit Service 603f59
    centurion: 2 = ./idrlogin.perl
Packit Service 603f59
    Login    Shell       PID Via           PID TTY        From
Packit Service 603f59
    oboyle   ksh       12640 in.telnetd  12638 pts/5      opal.cc.purdue.edu
Packit Service 603f59
    icdtest  ksh       15158 in.rlogind  15155 pts/6      localhost
Packit Service 603f59
    sh       csh       18207 in.rlogind  18205 pts/1      babylon5.cc.purdue.edu
Packit Service 603f59
    root     csh       18242 in.rlogind  18205 pts/1      babylon5.cc.purdue.edu
Packit Service 603f59
    trouble  ksh       19208 in.rlogind  18205 pts/1      babylon5.cc.purdue.edu
Packit Service 603f59
    abe      ksh       21334 in.rlogind  21332 pts/2      lsof.itap.purdue.edu
Packit Service 603f59
Packit Service 603f59
    The scripts assume that its parent directory contains an
Packit Service 603f59
    executable lsof.  If you decide to use one of the scripts, you
Packit Service 603f59
    may want to customize it for your local lsof and perl paths.
Packit Service 603f59
Packit Service 603f59
    Note that processes executing as remote shells are also
Packit Service 603f59
    identified.
Packit Service 603f59
Packit Service 603f59
    Here's another example from a UnixWare 7.1.0 system.
Packit Service 603f59
Packit Service 603f59
    tweeker: 1 = cd src/lsof4/scripts
Packit Service 603f59
    tweeker: 9 = ./idrlogin.perl
Packit Service 603f59
    Login    Shell       PID Via           PID TTY        From
Packit Service 603f59
    abe      ksh        9438 in.telnetd   9436 pts/3      lsof.itap.purdue.edu
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
10. Watching an Ftp or Rcp Transfer
Packit Service 603f59
===================================
Packit Service 603f59
Packit Service 603f59
  The nature of the Internet being one of unpredictable performance
Packit Service 603f59
  at times, occasionally you want to know if a file transfer, being
Packit Service 603f59
  done by ftp or rcp, is making any progress.
Packit Service 603f59
Packit Service 603f59
  To use lsof for watching a file transfer, you need to know the
Packit Service 603f59
  PID of the file transfer process.  You can use ps to find that.
Packit Service 603f59
  Then use lsof,
Packit Service 603f59
Packit Service 603f59
  $ lsof -p<PID>
Packit Service 603f59
Packit Service 603f59
  to examine the files open to the transfer process.  Usually the
Packit Service 603f59
  ftp files or interest are at file descriptors 9 and 10 or 10 and
Packit Service 603f59
  11; for rcp, 3 and 4.  They describe the network socket file and
Packit Service 603f59
  the local data file.
Packit Service 603f59
Packit Service 603f59
  If you want to watch only those file descriptors as the file
Packit Service 603f59
  transfer progresses, try these lsof forms (for ftp in the example):
Packit Service 603f59
Packit Service 603f59
    $ lsof -p<PID> -ad9,10 -r
Packit Service 603f59
  or
Packit Service 603f59
    $ lsof -p<PID> -ad10,11 -r
Packit Service 603f59
Packit Service 603f59
  Some options need explaining:
Packit Service 603f59
Packit Service 603f59
    -p<PID>	specifies that lsof is to restrict its attention
Packit Service 603f59
		to the process whose ID is <PID>.  You can specify
Packit Service 603f59
		a set of PIDs by separating them with commas.
Packit Service 603f59
Packit Service 603f59
		    $ lsof -p 1234,5678,9012
Packit Service 603f59
Packit Service 603f59
    -a		specifies that lsof is to AND its tests together.
Packit Service 603f59
		The two tests that are specified are tests on the
Packit Service 603f59
		PID and tests on file descriptions (``d9,10'').
Packit Service 603f59
Packit Service 603f59
    d9,10	specifies that lsof is to test only file descriptors
Packit Service 603f59
		9 and 10.  Note that the `-' is absent, since ``-a''
Packit Service 603f59
		is a unary option and can be followed immediately
Packit Service 603f59
		by another lsof option.
Packit Service 603f59
Packit Service 603f59
    -r          tells lsof to list the requested open file information,
Packit Service 603f59
		sleep for a default 15 seconds, then list the open
Packit Service 603f59
		file information again.  You can specify a different
Packit Service 603f59
		time (in seconds) after -r and override the default.
Packit Service 603f59
		Lsof issues a short line of equal signs between
Packit Service 603f59
		each set of output to distinguish it.
Packit Service 603f59
Packit Service 603f59
  For an rcp transfer, the above example becomes:
Packit Service 603f59
Packit Service 603f59
  $ lsof -p<PID> -ad3,4 -r
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
11. Listing Open NFS Files
Packit Service 603f59
==========================
Packit Service 603f59
Packit Service 603f59
  Lsof will list all files open on remote file systems, supported
Packit Service 603f59
  by an NFS server.  Just use:
Packit Service 603f59
Packit Service 603f59
  $ lsof -N
Packit Service 603f59
Packit Service 603f59
  Note, however, that when run on an NFS server, lsof will not list
Packit Service 603f59
  files open to the server from one of its clients.  That's because
Packit Service 603f59
  lsof can only examine the processes running on the machine where
Packit Service 603f59
  it is called -- i.e., on the NFS server.
Packit Service 603f59
Packit Service 603f59
  If you run lsof on the NFS client, using the -N option, it will
Packit Service 603f59
  list files open by processes on the client that are on remote
Packit Service 603f59
  NFS file systems.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
12. Listing Files Open by a Specific Login
Packit Service 603f59
==========================================
Packit Service 603f59
Packit Service 603f59
  If you're interested in knowing what files the processes owned
Packit Service 603f59
  by a particular login name have open, lsof can help.
Packit Service 603f59
Packit Service 603f59
    $ lsof -u<login>
Packit Service 603f59
  or
Packit Service 603f59
    $ lsof -u<User ID number>
Packit Service 603f59
Packit Service 603f59
  You can specify either the login name or the UID associated with
Packit Service 603f59
  it.  You can specify multiple login names and UID numbers, mixed
Packit Service 603f59
  together, by separating them with commas.
Packit Service 603f59
Packit Service 603f59
  $ lsof -u548,abe
Packit Service 603f59
Packit Service 603f59
  On the subject of login names and UIDs, it's worth noting that
Packit Service 603f59
  lsof can be told to report either.  By default it reports login
Packit Service 603f59
  names; the -l option switches reporting to UIDs.  You might want
Packit Service 603f59
  to use -l if login name lookup is slow for some reason.
Packit Service 603f59
Packit Service 603f59
  a.  Ignoring a Specific Login
Packit Service 603f59
  =============================
Packit Service 603f59
Packit Service 603f59
    The -u option can also be used to direct lsof to ignore a
Packit Service 603f59
    specific login name or UID, or a list of them.  Simply prefix
Packit Service 603f59
    the login names or UIDs with a `^' character, as you might do
Packit Service 603f59
    in a regular expression.  The `^' prefix is useful, for example,
Packit Service 603f59
    when you want to have lsof ignore the files open to system
Packit Service 603f59
    processes, owned by the root (UID 0) login.  Try:
Packit Service 603f59
Packit Service 603f59
      $ lsof -u ^root
Packit Service 603f59
    or
Packit Service 603f59
      $ lsof -u ^0
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
13. Listing Files Open to a Specific Process Group
Packit Service 603f59
==================================================
Packit Service 603f59
Packit Service 603f59
  There's a Unix collection of processes called a process group.
Packit Service 603f59
  The name indicates that the processes of the group have a common
Packit Service 603f59
  association and are grouped so that a signal sent to one (e.g.,
Packit Service 603f59
  a keyboard kill stroke) is delivered to all.
Packit Service 603f59
Packit Service 603f59
  This causes Unix to create a two element process group:
Packit Service 603f59
Packit Service 603f59
  $ lsof | less
Packit Service 603f59
Packit Service 603f59
  You can use lsof to look at the open files of all members of a
Packit Service 603f59
  process group, if you know the process group ID number.  Assuming
Packit Service 603f59
  that it is 12717 for the above example, this lsof command:
Packit Service 603f59
Packit Service 603f59
  $ lsof -g12717 -adcwd
Packit Service 603f59
Packit Service 603f59
  would produce on a Solaris 8 system:
Packit Service 603f59
Packit Service 603f59
  $ lsof -g12717 -adcwd
Packit Service 603f59
  COMMAND   PID  PGID USER  FD TYPE DEVICE SIZE/OFF    NODE NAME
Packit Service 603f59
  sshd    11369 12717 root cwd VDIR    0,2      189 1449175 /tmp (swap)
Packit Service 603f59
  sshd    12717 12717 root cwd VDIR  136,0     1024       2 /
Packit Service 603f59
Packit Service 603f59
  The ``-g12717'' option specifies the process group ID of interest;
Packit Service 603f59
  the ``-adcwd'' option specifies that options are to be ANDed and
Packit Service 603f59
  that lsof should limit file output to information about current
Packit Service 603f59
  working directory (``cwd'') files.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
14. When Lsof Seems to Hang
Packit Service 603f59
===========================
Packit Service 603f59
Packit Service 603f59
  On occasion when you run lsof it seems to hang and produce no
Packit Service 603f59
  output.  This may result from system conditions beyond the control
Packit Service 603f59
  of lsof.  Lsof has a number of options that may allow you to
Packit Service 603f59
  bypass the blockage.
Packit Service 603f59
Packit Service 603f59
  a.  Kernel lstat(), readlink(), and stat() Blockages
Packit Service 603f59
  ====================================================
Packit Service 603f59
Packit Service 603f59
    Lsof uses the kernel (system) calls lstat(), readlink(), and
Packit Service 603f59
    stat() to locate mounted file system information.  When a file
Packit Service 603f59
    system has been mounted from an NFS server and that server is
Packit Service 603f59
    temporarily unavailable, the calls lsof uses may block in the
Packit Service 603f59
    kernel.
Packit Service 603f59
Packit Service 603f59
    Lsof will announce that it is being blocked with warning messages
Packit Service 603f59
    (unless they have been suppressed by the lsof builder), but
Packit Service 603f59
    only after a default waiting period of fifteen seconds has
Packit Service 603f59
    expired for each file system whose server is unavailable.  If
Packit Service 603f59
    you have a number of such file systems, the total wait may be
Packit Service 603f59
    unacceptably long.
Packit Service 603f59
Packit Service 603f59
    You can do two things to shorten your suffering: 1) reduce the
Packit Service 603f59
    wait time with the -S option; or 2) tell lsof to avoid the
Packit Service 603f59
    kernel calls that might block by specifying the -b option.
Packit Service 603f59
Packit Service 603f59
      $ lsof -S 5
Packit Service 603f59
    or
Packit Service 603f59
      $ lsof -b
Packit Service 603f59
Packit Service 603f59
    Avoiding the kernel calls that might block may result in the
Packit Service 603f59
    lack of some information that lsof needs to know about mounted
Packit Service 603f59
    file systems.  Thus, when you use -b, lsof warns that it might
Packit Service 603f59
    lack important information.
Packit Service 603f59
Packit Service 603f59
    The warnings that result from using -b (unless suppressed by
Packit Service 603f59
    the lsof builder) can themselves be annoying.  You can suppress
Packit Service 603f59
    them by adding the -w option.  (Of course, if you do, you won't
Packit Service 603f59
    know what warning messages lsof might have issued.)
Packit Service 603f59
Packit Service 603f59
    $ lsof -bw
Packit Service 603f59
Packit Service 603f59
    Note: if the lsof builder suppressed warning message issuance,
Packit Service 603f59
    you don't need to use -w to suppress them.  You can tell what
Packit Service 603f59
    the default state of message warning issuance is by looking at
Packit Service 603f59
    the -h (help) output.  If it says ``-w enable warnings'' then
Packit Service 603f59
    warnings are disabled by default; ``-w disable warnings'', they
Packit Service 603f59
    are enabled by default.
Packit Service 603f59
Packit Service 603f59
  b.  Problems with /dev or /devices
Packit Service 603f59
  ==================================
Packit Service 603f59
Packit Service 603f59
    Lsof scans the /dev or /devices branch of your file system to
Packit Service 603f59
    obtain information about your system's devices.  (The scan isn't
Packit Service 603f59
    necessary when a device cache file exists.)
Packit Service 603f59
Packit Service 603f59
    Sometimes that scan can take a very long time, especially if
Packit Service 603f59
    you have a large number of devices, and if your kernel is
Packit Service 603f59
    relatively slow to process the stat() system call on device
Packit Service 603f59
    nodes.  You can't do anything about the stat() system call
Packit Service 603f59
    speed.
Packit Service 603f59
Packit Service 603f59
    However, you can make sure that lsof is allowed to use its
Packit Service 603f59
    device cache file feature.  When lsof can use a device cache
Packit Service 603f59
    file, it retains information it gleans via the stat() calls
Packit Service 603f59
    on /dev or /devices in a separate file for later, faster
Packit Service 603f59
    access.
Packit Service 603f59
Packit Service 603f59
    The device cache file feature is described in the lsof man
Packit Service 603f59
    page.  See the DEVICE CACHE FILE, LSOF PERMISSIONS THAT AFFECT
Packit Service 603f59
    DEVICE CACHE FILE ACCESS, DEVICE CACHE FILE PATH FROM THE -D
Packit Service 603f59
    OPTION, DEVICE CACHE PATH FROM AN ENVIRONMENT VARIABLE,
Packit Service 603f59
    SYSTEM-WIDE DEVICE CACHE PATH, PERSONAL DEVICE CACHE PATH
Packit Service 603f59
    (DEFAULT), and MODIFIED PERSONAL DEVICE CACHE PATH sections.
Packit Service 603f59
Packit Service 603f59
    There is also a separate file in the lsof distribution, named
Packit Service 603f59
    00DCACHE, that describes the device cache file in detail,
Packit Service 603f59
    including information about possible security problems.
Packit Service 603f59
Packit Service 603f59
    One final observation: don't overlook the possibility that your
Packit Service 603f59
    /dev or /devices tree might be damaged.  See if
Packit Service 603f59
Packit Service 603f59
      $ ls -R /dev
Packit Service 603f59
    or
Packit Service 603f59
      $ ls -R /devices
Packit Service 603f59
Packit Service 603f59
    completes or hangs.  If it hangs, then lsof will probably hang,
Packit Service 603f59
    too, and you should try to discover why ls hangs.
Packit Service 603f59
Packit Service 603f59
    c.  Host and Service Name Lookup Hangs
Packit Service 603f59
    ======================================
Packit Service 603f59
Packit Service 603f59
    Lsof can hang up when it tries to convert an Internet dot-form
Packit Service 603f59
    address to a host name, or a port number to a service name.  Both
Packit Service 603f59
    hangs are caused by the lookup functions of your system.
Packit Service 603f59
Packit Service 603f59
    An independent check for both types of hangs can be made with
Packit Service 603f59
    the netstat program.  Run it without arguments.  If it hangs,
Packit Service 603f59
    then it is probably having lookup difficulties.  When you run
Packit Service 603f59
    it with -n it shouldn't hang and should report network and port
Packit Service 603f59
    numbers instead of names.
Packit Service 603f59
Packit Service 603f59
    Lsof has two options that serve the same purpose as netstat's
Packit Service 603f59
    -n option.  The lsof -n option tells it to avoid host name
Packit Service 603f59
    lookups; and -P, service name lookups.  Try those options when
Packit Service 603f59
    you suspect lsof may be hanging because of lookup problems.
Packit Service 603f59
Packit Service 603f59
      $ lsof -n
Packit Service 603f59
    or
Packit Service 603f59
      $ lsof -P
Packit Service 603f59
    or
Packit Service 603f59
      $ lsof -nP
Packit Service 603f59
Packit Service 603f59
    d.  UID to Login Name Conversion Delays
Packit Service 603f59
    =======================================
Packit Service 603f59
Packit Service 603f59
    By default lsof converts User IDentification (UID) numbers to
Packit Service 603f59
    login names when it produces output.  That conversion process
Packit Service 603f59
    may sometimes hang because of system problems or interlocks.
Packit Service 603f59
Packit Service 603f59
    You can tell lsof to skip the lookup with the -l option; it
Packit Service 603f59
    will then report UIDs in the USER column.
Packit Service 603f59
Packit Service 603f59
    $ lsof -l
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
15. Output for Other Programs
Packit Service 603f59
=============================
Packit Service 603f59
Packit Service 603f59
  The -F option allows you to specify that lsof should describe
Packit Service 603f59
  open files with a special form of output, called field output,
Packit Service 603f59
  that can be parsed easily by a subsequent program.  The lsof
Packit Service 603f59
  distribution comes with sample AWK, Perl 4, and Perl 5 scripts
Packit Service 603f59
  that post-process field output.  The lsof test suite has a C
Packit Service 603f59
  library that could be adapted for use by C programs that want to
Packit Service 603f59
  process lsof field output from an in-bound pipe.
Packit Service 603f59
Packit Service 603f59
  The lsof manual page describes field output in detail in its
Packit Service 603f59
  OUTPUT FOR OTHER PROGRAMS section.  A quick look at a sample
Packit Service 603f59
  script in the scripts/ subdirectory of the lsof distribution will
Packit Service 603f59
  also give you an idea how field output works.
Packit Service 603f59
Packit Service 603f59
  The most important thing about field output is that it is relatively
Packit Service 603f59
  homogeneous across Unix dialects.  Thus, if you write a script
Packit Service 603f59
  to post-process field output for AIX, it probably will work for
Packit Service 603f59
  HP-UX, Solaris, and Ultrix as well.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
16. The Lsof Exit Code and Shell Scripts
Packit Service 603f59
========================================
Packit Service 603f59
Packit Service 603f59
  When lsof exits successfully it returns an exit code based on
Packit Service 603f59
  the result of its search for specified files.  (If no files were
Packit Service 603f59
  specified, then the successful exit code is 0 (zero).)
Packit Service 603f59
Packit Service 603f59
  If lsof was asked to search for specific files, including any
Packit Service 603f59
  files on specified file systems, it returns an exit code of 0
Packit Service 603f59
  (zero) if it found all the specified files and at least one file
Packit Service 603f59
  on each specified file system.  Otherwise it returns a 1 (one).
Packit Service 603f59
Packit Service 603f59
  If lsof detects an error and makes an unsuccessful exit, it
Packit Service 603f59
  returns an exit code of 1 (one).
Packit Service 603f59
Packit Service 603f59
  You can use the exit code in a shell script to search for files
Packit Service 603f59
  on a file system and take action based on the result -- e.g.,
Packit Service 603f59
Packit Service 603f59
    #!/bin/sh
Packit Service 603f59
    lsof <file_system_name> > /dev/null 2>&1
Packit Service 603f59
    if test $? -eq 0
Packit Service 603f59
    then
Packit Service 603f59
      echo "<file_system_name> has some users."
Packit Service 603f59
    else
Packit Service 603f59
      echo "<file_system_name> may have no users."
Packit Service 603f59
    fi
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
17. Strange messages in the NAME column
Packit Service 603f59
=======================================
Packit Service 603f59
Packit Service 603f59
  When lsof encounters problems analyzing a particular file, it may
Packit Service 603f59
  put a message in the file's NAME column.  Many of those messages
Packit Service 603f59
  are explained in the 00FAQ file of the lsof distribution.
Packit Service 603f59
Packit Service 603f59
  So consult 00FAQ first if you encounter a NAME column message you
Packit Service 603f59
  don't understand.  (00FAQ is a possible source of information
Packit Service 603f59
  about other unfamiliar things in lsof output, too.)
Packit Service 603f59
  
Packit Service 603f59
  If you can't find help in 00FAQ, you can use grep to look in the
Packit Service 603f59
  lsof source files for the message -- e.g.,
Packit Service 603f59
Packit Service 603f59
    $ cd .../lsof_4.76_src
Packit Service 603f59
    $ grep "can't identify protocol" *.[ch]
Packit Service 603f59
Packit Service 603f59
  The code associated with the message will usually make clear the
Packit Service 603f59
  reason for the message.
Packit Service 603f59
Packit Service 603f59
  If you have an lsof source tree that has been processed by the
Packit Service 603f59
  lsof Configure script, you need grep only there.  If, however,
Packit Service 603f59
  your source tree hasn't been processed by Configure, you may
Packit Service 603f59
  have to look in the top-level lsof source directory and in the
Packit Service 603f59
  dialects sub-directory for the UNIX dialect you are using - e.g.,
Packit Service 603f59
Packit Service 603f59
    $ cd .../lsof_4.76_src
Packit Service 603f59
    $ grep "can't identify protocol" *.[ch]
Packit Service 603f59
    $ cd dialects/Linux
Packit Service 603f59
    $ grep "can't identify protocol" *.[ch]
Packit Service 603f59
Packit Service 603f59
  In rare cases you may have to look in the lsof library, too --
Packit Service 603f59
  e.g.,
Packit Service 603f59
Packit Service 603f59
    $ cd .../lsof_4.76_src
Packit Service 603f59
    $ grep "can't identify protocol" *.[ch]
Packit Service 603f59
    $ cd dialects/Linux
Packit Service 603f59
    $ grep "can't identify protocol" *.[ch]
Packit Service 603f59
    $ cd ../../lib
Packit Service 603f59
    $ grep "can't identify protocol" *.[ch]
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
Options
Packit Service 603f59
=======
Packit Service 603f59
Packit Service 603f59
  The following appendices describe the lsof options in detail.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
A.  Selection Options
Packit Service 603f59
====================
Packit Service 603f59
Packit Service 603f59
  Lsof has a rich set of options for selecting the files to be
Packit Service 603f59
  displayed.  These include:
Packit Service 603f59
Packit Service 603f59
	-a	tells lsof to AND the set of selection options that
Packit Service 603f59
		are specified.  Normally lsof ORs them.
Packit Service 603f59
		
Packit Service 603f59
		For example, if you specify the -p<PID> and -u<UID>
Packit Service 603f59
		options, lsof will display all files for the
Packit Service 603f59
		specified PID or for the specified UID.
Packit Service 603f59
Packit Service 603f59
		By adding -a, you specify that the listed files
Packit Service 603f59
		should be limited to PIDs owned by the specified
Packit Service 603f59
		UIDs -- i.e., they match the PIDs *and* the UIDs.
Packit Service 603f59
Packit Service 603f59
		    $ lsof -p1234 -au 5678
Packit Service 603f59
Packit Service 603f59
	-c	specifies that lsof should list files belonging
Packit Service 603f59
		to processes having the associated command name.
Packit Service 603f59
Packit Service 603f59
		Hint: if you want to select files based on more than
Packit Service 603f59
		one command name, use multiple -c<name> specifications.
Packit Service 603f59
Packit Service 603f59
		    $ lsof -clsof -cksh
Packit Service 603f59
Packit Service 603f59
	-d      tells lsof to select by the associated file descriptor
Packit Service 603f59
		(FD) set.  An FD set is a comma-separated list of
Packit Service 603f59
		numbers and the names lsof normally displays in
Packit Service 603f59
		its FD column:  cwd, Lnn, ltx, <number>, etc.  See
Packit Service 603f59
		the OUTPUT section of the lsof man page for the
Packit Service 603f59
		complete list of possible file descriptors.  Example:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -dcwd,0,1,2
Packit Service 603f59
Packit Service 603f59
	-g      tells lsof to select by the associated process
Packit Service 603f59
		group ID (PGID) set.  The PGID set is a comma-separated
Packit Service 603f59
		list of PGID numbers.  When -g is specified, it also
Packit Service 603f59
		enables the display of PGID numbers.
Packit Service 603f59
Packit Service 603f59
		Note: when -g isn't followed by a PGID set, it
Packit Service 603f59
		simply selects the listing of PGID for all processes.
Packit Service 603f59
		Examples:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -g
Packit Service 603f59
		    $ lsof -g1234,5678
Packit Service 603f59
Packit Service 603f59
	-i	tells lsof to display Internet socket files.  If no
Packit Service 603f59
		protocol/address/port specification follows -i,
Packit Service 603f59
		lsof lists all Internet socket files.
Packit Service 603f59
Packit Service 603f59
		If a specification follows -i, lsof lists only the
Packit Service 603f59
		socket files whose Internet addresses match the
Packit Service 603f59
		specification.
Packit Service 603f59
Packit Service 603f59
		Hint: multiple addresses may be specified with
Packit Service 603f59
		multiple -i options.  Examples:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -iTCP
Packit Service 603f59
		    $ lsof -i@lsof.itap.purdue.edu:sendmail
Packit Service 603f59
Packit Service 603f59
	-N	selects the listing of files mounted on NFS devices.
Packit Service 603f59
Packit Service 603f59
	-U	selects the listing of socket files in the Unix
Packit Service 603f59
		domain.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
B.  Output Options
Packit Service 603f59
==================
Packit Service 603f59
Packit Service 603f59
  Lsof has these options to control its output format:
Packit Service 603f59
Packit Service 603f59
	-F	produce output that can be parsed by a subsequent
Packit Service 603f59
		program.
Packit Service 603f59
Packit Service 603f59
	-g	print process group (PGID) IDs.
Packit Service 603f59
Packit Service 603f59
	-l	list UID numbers instead of login names.
Packit Service 603f59
Packit Service 603f59
	-n	list network numbers instead of host names.
Packit Service 603f59
Packit Service 603f59
	-o	always list file offset.
Packit Service 603f59
Packit Service 603f59
	-P	list port numbers instead of port service names.
Packit Service 603f59
Packit Service 603f59
	-s	always list file size.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
C.  Precautionary Options
Packit Service 603f59
=========================
Packit Service 603f59
Packit Service 603f59
  Lsof uses system functions that can block or take a long time,
Packit Service 603f59
  depending on the health of the Unix dialect supporting it.  These
Packit Service 603f59
  include:
Packit Service 603f59
Packit Service 603f59
	-b	directs lsof to avoid system functions -- e.g.,
Packit Service 603f59
		lstat(2), readlink(2), stat(2) -- that might block
Packit Service 603f59
		in the kernel.  See the BLOCKS AND TIMEOUTS
Packit Service 603f59
		section of the lsof man page.
Packit Service 603f59
Packit Service 603f59
		You might want to use this option when you have
Packit Service 603f59
		a mount from an NFS server that is not responding.
Packit Service 603f59
Packit Service 603f59
	-C	tells lsof to ignore the kernel's name cache.  As
Packit Service 603f59
		a precaution this option will have little effect on
Packit Service 603f59
		lsof performance, but might be useful if the kernel's
Packit Service 603f59
		name cache is scrambled.  (I've never seen that
Packit Service 603f59
		happen.)
Packit Service 603f59
Packit Service 603f59
	-D	might be used to direct lsof to ignore an existing
Packit Service 603f59
		device cache file and generate a new one from /dev
Packit Service 603f59
		(and /devices).  This might be useful if you have
Packit Service 603f59
		doubts about the integrity of an existing device
Packit Service 603f59
		cache file.
Packit Service 603f59
Packit Service 603f59
	-l      tells lsof to list UID numbers instead of login
Packit Service 603f59
		names -- this is useful when UID to login name
Packit Service 603f59
		conversion is slow or inoperative.
Packit Service 603f59
Packit Service 603f59
	-n	tells lsof to avoid converting Internet addresses
Packit Service 603f59
		to host numbers.  This might be useful when your
Packit Service 603f59
		host name lookup (e.g., DNS) is inoperative.
Packit Service 603f59
Packit Service 603f59
	-O      tells lsof to avoid its strategy of forking to
Packit Service 603f59
		perform potentially blocking kernel operations.
Packit Service 603f59
		While the forking allows lsof to detect that a
Packit Service 603f59
		block has occurred (and possibly break it), the
Packit Service 603f59
		fork operation is a costly one.  Use the -O option
Packit Service 603f59
		with care, lest your lsof be blocked.
Packit Service 603f59
Packit Service 603f59
	-P      directs lsof to list port numbers instead of trying
Packit Service 603f59
		to convert them to port service names.  This might
Packit Service 603f59
		be useful if port to service name lookups (e.g.,
Packit Service 603f59
		via NIS) are slow or failing.
Packit Service 603f59
Packit Service 603f59
	-S      can be used to change the lstat/readlink/stat
Packit Service 603f59
		timeout interval that governs how long lsof waits
Packit Service 603f59
		for response from the kernel.  This might be useful
Packit Service 603f59
		when an NFS server is slow or unresponsive.  When
Packit Service 603f59
		lsof times out of a kernel function, it may have
Packit Service 603f59
		less information to display.  Example:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -S2
Packit Service 603f59
Packit Service 603f59
	-w	tells lsof to avoid issuing warning messages, if
Packit Service 603f59
		they are enabled by default, or enable them if they
Packit Service 603f59
		are disabled by default.  Check the -h (help) output
Packit Service 603f59
		to determine their status.  If it says ``-w enable
Packit Service 603f59
		warnings'', then warning messages are disabled by
Packit Service 603f59
		default; ``-w disable warnings'', they are enabled
Packit Service 603f59
		by default.
Packit Service 603f59
Packit Service 603f59
		This may be a useful option, for example, when you
Packit Service 603f59
		specify -b, if warning messages are enabled, because
Packit Service 603f59
		it will suppress the warning messages lsof issues
Packit Service 603f59
		about avoiding functions that might block in the
Packit Service 603f59
		kernel.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
D.  Miscellaneous Lsof Options
Packit Service 603f59
==============================
Packit Service 603f59
Packit Service 603f59
  There are some lsof options that are hard to classify, including:
Packit Service 603f59
Packit Service 603f59
	-?	these options select help output.
Packit Service 603f59
	-h
Packit Service 603f59
Packit Service 603f59
	-F      selects field output.  Field output is a mode where
Packit Service 603f59
		lsof produces output that can be parsed easily by
Packit Service 603f59
		subsequent programs -- e.g., AWK or Perl scripts.
Packit Service 603f59
		See ``15. Output for Other Programs'' for more
Packit Service 603f59
		information.
Packit Service 603f59
Packit Service 603f59
	-k	specifies an alternate kernel symbol file -- i.e.,
Packit Service 603f59
		where nlist() will get its information.  Example:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -k/usr/crash/vmunix.1
Packit Service 603f59
Packit Service 603f59
	-m	specifies an alternate kernel memory file from
Packit Service 603f59
		which lsof will read kernel structures in place
Packit Service 603f59
		of /dev/kmem or kvm_read().  Example:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -m/usr/crash/vmcore.n
Packit Service 603f59
Packit Service 603f59
	-r	tells lsof to repeat its scan every 15 seconds (the
Packit Service 603f59
		default when no associated value is specified).  A
Packit Service 603f59
		repeat time, different from the default, can follow
Packit Service 603f59
		-r.  Example:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -r30
Packit Service 603f59
Packit Service 603f59
	-v	displays information about the building of the
Packit Service 603f59
		lsof executable.
Packit Service 603f59
Packit Service 603f59
	--      The double minus sign option may be used to
Packit Service 603f59
		signal the end of options.  It's particularly useful
Packit Service 603f59
		when arguments to the last option are optional and
Packit Service 603f59
		you want to supply a file path that could be confused
Packit Service 603f59
		for arguments to the last option.  Example:
Packit Service 603f59
Packit Service 603f59
		    $ lsof -g -- 1
Packit Service 603f59
		
Packit Service 603f59
		Where `1' is a file path, not PGID ID 1.
Packit Service 603f59
Packit Service 603f59
Packit Service 603f59
Vic Abell <abe@purdue.edu>
Packit Service 603f59
October 13, 2014