Bump to lsof 4.91
[platform/upstream/lsof.git] / 00QUICKSTART
1
2                         A Quick Start for Lsof
3
4 1.  Introduction
5 ================
6
7   Agreed, the lsof man page is dense and lsof has a plethora of
8   options.  There are examples, but the manual page format buries
9   them at the end.  How does one get started with lsof?
10
11   This file is an attempt to answer that question.  It plunges
12   immediately into examples of lsof use to solve problems that
13   involve looking at the open files of Unix processes.
14
15
16                             Contents
17
18             1.  Introduction
19             2.  Finding Uses of a Specific Open File
20             3.  Finding Open Files Filling a File System
21                 a.  Finding an Unlinked Open File
22             4.  Finding Processes Blocking Umount
23             5.  Finding Listening Sockets
24             6.  Finding a Particular Network Connection
25             7.  Identifying a Netstat Connection
26             8.  Finding Files Open to a Named Command
27             9.  Deciphering the Remote Login Trail
28                 a.  The Fundamentals
29                 b.  The idrlogin.perl[5] Scripts
30             10. Watching an Ftp or Rcp Transfer
31             11. Listing Open NFS Files
32             12. Listing Files Open by a Specific Login
33                 a.  Ignoring a Specific Login
34             13. Listing Files Open to a Specific Process Group
35             14. When Lsof Seems to Hang
36                 a.  Kernel lstat(), readlink(), and stat() Blockages
37                 b.  Problems with /dev or /devices
38                 c.  Host and Service Name Lookup Hangs
39                 d.  UID to Login Name Conversion Delays
40             15. Output for Other Programs
41             16. The Lsof Exit Code and Shell Scripts
42             17. Strange messages in the NAME column
43
44                         Options
45
46             A.  Selection Options
47             B.  Output Options
48             C.  Precautionary Options
49             D.  Miscellaneous Lsof Options
50
51
52 2.  Finding Uses of a Specific Open File
53 ========================================
54
55   Often you're interested in knowing who is using a specific file.
56   You know the path to it and you want lsof to tell you the processes
57   that have open references to it.
58
59   Simple -- execute lsof and give it the path name of the file of
60   interest -- e.g.,
61
62   $ lsof /etc/passwd
63
64   Caveat: this only works if lsof has permission to get the status
65   (via stat(2)) of the file at the named path.  Unless the lsof
66   process has enough authority  -- e.g., it is being run with a
67   real User ID (UID) of root -- this AIX example won't work:
68
69   Further caveat: this use of lsof will fail if the stat(2) kernel
70   syscall returns different file parameters -- particularly device
71   and inode numbers -- than lsof finds in kernel node structures.
72   This condition is rare and is usually documented in the 00FAQ
73   file of the lsof distribution.
74
75   $ lsof /etc/security/passwd
76   lsof: status error on /etc/security/passwd: Permission denied
77
78
79 3.  Finding Open Files Filling a File System
80 ============================================
81
82   Oh! Oh!  /tmp is filling and ls doesn't show that any large files
83   are being created.  Can lsof help?
84
85   Maybe.  If there's a process that is writing to a file that has
86   been unlinked, lsof may be able to discover the process for you.
87   You ask it to list all open files on the file system where /tmp
88   is located.
89
90   Sometimes /tmp is a file system by itself.  In that case,
91
92   $ lsof /tmp
93
94   is the appropriate command.  If, however, /tmp is part of another
95   file system, typically /, then you may have to ask lsof to list
96   all files open on the containing file system and locate the
97   offending file and its process by inspection -- e.g.,
98
99     $ lsof / | more
100   or
101     $ lsof / | grep ...
102
103   Caveat: there must be a file open to a for the lsof search to
104   succeed.  Sometimes the kernel may cause a file reference to
105   persist, even where there's no file open to a process.  (Can you
106   say kernel bug?  Maybe.)  In any event, lsof won't be able to
107   help in this case.
108
109   a.  Finding an Unlinked Open File
110   =================================
111
112   A pesky variant of a file that is filling a file system is an
113   unlinked file to which some process is still writing.  When a
114   process opens a file and then unlinks it, the file's resources
115   remain in use by the process, but the file's directory entries
116   are removed.  Hence, even when you know the directory where the
117   file once resided, you can't detect it with ls.
118
119   This can be an administrative problem when the unlinked file is
120   large, and the process that holds it open continues to write to
121   it.  Only when the process closes the file will its resources,
122   particularly disk space, be released.
123
124   Lsof can help you find unlinked files on local disks.  It has an
125   option, +L, that will list the link counts of open files.  That
126   helps because an unlinked file on a local disk has a zero link
127   count.  Note: this is NOT true for NFS files, accessed from a
128   remote server.
129
130   You could use the option to list all files and look for a zero
131   link count in the NLINK column -- e.g.,
132
133     $lsof +L
134     COMMAND   PID USER   FD  TYPE DEVICE SIZE/OFF NLINK  NODE NAME
135     ...
136     less    25366  abe  txt  VREG    6,0    40960     1 76319 /usr/...
137     ...
138   > less    25366  abe    3r VREG    6,0    17360     0 98768 / (/dev/sd0a)
139
140   Better yet, you can specify an upper bound to the +L option, and
141   lsof will select only files that have a link count less than the
142   upper bound.  For example:
143
144     $ lsof +L1
145     COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NLINK  NODE NAME
146     less    25366  abe    3r  VREG    6,0    17360     0 98768 / (/dev/sd0a)
147
148   You can use lsof's -a (AND) option to narrow the link count search
149   to a particular file system.  For example, to look for zero link
150   counts on the /home file system, use:
151
152     $ lsof -a +L1 /home
153
154   CAUTION: lsof can't always report link counts for all file types
155   -- e.g., it may not report them for FIFOs, pipes, or sockets.
156   Remember also that link counts for NFS files on an NFS client
157   host don't behave as do link counts for files on local disks.
158
159
160 4.  Finding Processes Blocking Umount
161 =====================================
162
163   When you need to unmount a file system with the umount command,
164   you may find the operation blocked by a process that has a file
165   open on the file systems.  Lsof may be able to help you find the
166   process.  In response to:
167
168   $ lsof <file_system_name>
169
170   Lsof will display all open files on the named file system.  It
171   will also set its exit code zero when it finds some open files
172   and non-zero when it doesn't, making this type of lsof call
173   useful in shell scripts.  (See section 16.)
174
175   Consult the output of the df command for file system names.
176
177   See the caveat in the preceding section about file references
178   that persist in the kernel without open file traces.  That
179   situation may hamper lsof's ability to help with umount, too.
180
181
182 5.  Finding Listening Sockets
183 =============================
184
185   Sooner or later you may wonder if someone has installed a network
186   server that you don't know about.  Lsof can list for you all the
187   network socket files open on your machine with:
188
189     $ lsof -i
190
191   The -i option without further qualification lists all open Internet
192   socket files.  You can add network names or addresses, protocol
193   names, and service names or port numbers to the -i option to
194   refine the search.  (See the next section.)
195
196
197 6.  Finding a Particular Network Connection
198 ===========================================
199
200   When you know the source or destination of a network connection
201   whose open files and process you'd like to identify, the -i option
202   may help.
203
204   If, for example, you want to know what process has a connection
205   open to or from the Internet host named aaa.bbb.ccc, you can ask
206   lsof to search for it with:
207
208   $ lsof -i@aaa.bbb.ccc
209
210   If you're interested in a particular protocol -- TCP or UDP --
211   and a specific port number or service name, you can add those
212   discriminators to the -i information:
213
214   $ lsof -iTCP@aaa.bbb.ccc:ftp-data
215
216   If you're interested in a particular IP version -- IPv4 or IPv6
217   -- and your UNIX dialect supports both (It does if "IPv[46]"
218   appears in the lsof -h output.), you can add the '4' or '6'
219   selector immediately after -i:
220
221   $ lsof -i4
222   $ lsof -i6
223
224
225 7.  Identifying a Netstat Connection
226 ====================================
227
228   How do I identify the process that has a network connection
229   described in netstat output?  For example, if netstat says:
230
231   Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
232   tcp        0      0  vic.1023               ipscgate.login         ESTABLISHED
233
234   What process is connected to service name ``login'' on ipscgate?
235
236   Use lsof's -i option:
237
238   $lsof -iTCP@ipscgate:login
239   COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
240   rlogin    25023      abe    3u  inet 0x10144168      0t184    TCP lsof.itap.purdue.edu:1023->ipscgate.cc.purdue.edu:login
241   ...
242
243   There's another way.  Notice the 0x10144168 in the DEVICE column
244   of the lsof output?  That's the protocol control block (PCB)
245   address.  Many netstat applications will display it when given
246   the -A option:
247
248   $ netstat -A
249   PCB      Proto Recv-Q Send-Q  Local Address      Foreign Address    (state)
250   10144168 tcp        0      0  vic.1023           ipscgate.login     ESTABLISHED
251   ...
252
253   Using the PCB address, lsof, and grep, you can find the process this
254   way, too:
255
256   $ lsof -i | grep 10144168
257   rlogin    25023      abe    3u  inet 0x10144168      0t184    TCP lsof.itap.purdue.edu:1023->ipscgate.cc.purdue.edu:login
258   ...
259
260   If the file is a UNIX socket and netstat reveals and adress for it,
261   like this Solaris 11 example:
262
263   $ netstat -a -f unix
264   Active UNIX domain sockets
265   Address  Type          Vnode     Conn  Local Addr      Remote Addr
266   ffffff0084253b68 stream-ord 0000000 0000000
267
268   Using lsof's -U option and its output piped to a grep on the address
269   yields:
270
271   $ lsof -U | grep ffffff0084253b68
272   squid 1638 nobody 12u unix 18,98 0t10 9437188 /devices/pseudo/tl@0:ticots->0xffffff0084253b68 stream-ord
273
274
275 8.  Finding Files Open to a Named Command
276 =========================================
277
278   When you want to look at the files open to a particular command,
279   you can look up the PID of the process running the command and
280   use lsof's -p option to specify it.
281
282   $ lsof -p <PID>
283
284   However, there's a quicker way, using lsof's -c option, provided
285   you don't mind seeing output for every process running the named
286   command.
287
288   $ lsof -c <first_characters_of_command_name_that_interest_you>
289
290   The lsof -c option is useful when you want to see how many instances
291   of a given command are executing and what their open files are.
292   One useful example is for the sendmail command.
293
294   $ lsof -c sendmail
295
296
297 9.  Deciphering the Remote Login Trail
298 ======================================
299
300   If the network connection you're interested in tracing has been
301   initiated externally and is connected to an rlogind, sshd, or
302   telnetd process, asking lsof to identify that process might not
303   give a wholly satisfying answer.  The report may be that the
304   connection exists, but to a process owned by root.
305
306   a.  The Fundamentals
307   ====================
308
309     How do you get from there to the login name really using the
310     connection?  You have to know a little about how real and pseudo
311     ttys are paired in your system, and then use several lsof probes
312     to identify the login.
313
314     This example comes from a Solaris 2.4 system, named klaatu.cc.
315     I've logged on to it via rlogin from lsof.itap.  The first lsof
316     probe,
317
318     $ lsof -i@lsof.itap
319
320     yields (among other things):
321
322     COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
323     in.rlogin  7362     root    0u  inet 0xfc0193b0      0t242    TCP klaatu.cc.purdue.edu:login->lsof.itap.purdue.edu:1023
324     ...
325
326     This confirms that a connection exists.  A second lsof probe
327     shows:
328
329     $ lsof -p7362
330     COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
331     ...
332     in.rlogin  7362     root    0u  inet 0xfc0193b0      0t242    TCP klaatu.cc.purdue.edu:login->lsof.itap.purdue.edu:1023
333     ...
334     in.rlogin  7362     root    3u  VCHR    23,   0       0t66  52928 /devices/pseudo/clone@0:ptmx->pckt->ptm
335
336     7362 is the Process ID (PID) of the in.rlogin process, discovered
337     in the first lsof probe.  (I've abbreviated the output to simplify
338     the example.)  Now comes a need to understand Solaris pseudo-ttys.
339     The key indicator is in the DEVICE column for FD 3, the major/minor
340     device number of 23,0.  This translates to /dev/pts/0, so a third
341     lsof probe,
342
343     $ lsof /dev/pts/0
344     COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF  INODE NAME
345     ksh        7364      abe    0u  VCHR    24,   0     0t2410  53410 /dev/pts/../../devices/pseudo/pts@0:0
346
347     shows in part that login abe has a ksh process on /dev/pts/0.
348     (The NAME that lsof shows is not /dev/pts/0 but the full expansion
349     of the symbolic link that lsof finds at /dev/pts/0.)
350
351     Here's a second example, done on an HP-UX 9.01 host named ghg.ecn.
352     Again, I've logged on to it from lsof.itap, so I start with:
353
354     $ lsof -i@lsof.itap
355     COMMAND     PID     USER   FD   TYPE       DEVICE   SIZE/OFF  INODE NAME
356     rlogind   10214     root    0u  inet   0x041d5f00     0t1536    TCP ghg.ecn.purdue.edu:login->lsof.itap.purdue.edu:1023
357     ...
358
359     Then,
360
361     $ lsof -p10214
362     COMMAND     PID     USER   FD   TYPE       DEVICE   SIZE/OFF  INODE NAME
363     ...
364     rlogind   10214     root    0u  inet   0x041d5f00     0t2005    TCP ghg.ecn.purdue.edu:login->lsof.itap.purdue.edu:1023
365     ...
366     rlogind   10214     root    3u  VCHR  16,0x000030     0t2037  24642 /dev/ptym/ptys0
367
368     Here the key is the NAME /dev/ptym/ptys0.  In HP-UX 9.01 tty and
369     pseudo tty devices are paired with the names like /dev/ptym/ptys0
370     and /dev/pty/ttys0, so the following lsof probe is the final step.
371
372     $ lsof /dev/pty/ttys0
373     COMMAND     PID     USER   FD   TYPE       DEVICE   SIZE/OFF  INODE NAME
374     ksh       10215      abe    0u  VCHR  17,0x000030     0t3399  22607 /dev/pty/ttys0
375     ...
376
377     Here's a third example for an AIX 4.1.4 system.  I've used telnet
378     to connect to it from lsof.itap.purdue.edu.  I start with:
379
380     $ lsof -i@lsof.itap.purdue.edu
381     COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF      INODE NAME
382     ...
383     telnetd   15616     root    0u  inet 0x05a93400     0t5156        TCP cloud.cc.purdue.edu:telnet->lsof.itap.purdue.edu:3369
384
385     Then I look at the telnetd process:
386
387     $ lsof -p15616
388     COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF      INODE NAME
389     ...
390     telnetd   15616     root    0u  inet 0x05a93400     0t5641        TCP cloud.cc.purdue.edu:telnet->lsof.itap.purdue.edu:3369
391     ...
392     telnetd   15616     root    3u  VCHR    25,   0     0t5493        103 /dev/ptc/0
393
394     Here the key is /dev/ptc/0.  In AIX it's paired with /dev/pts/0.
395     The last probe for that shows:
396
397     $ lsof /dev/pts/0
398     COMMAND     PID     USER   FD   TYPE     DEVICE   SIZE/OFF      INODE NAME
399     ...
400     ksh       16642      abe    0u  VCHR    26,   0     0t6461        360 /dev/pts/0
401
402   b.  The idrlogin.perl[5] Scripts
403   ================================
404
405     There's another, perhaps easier way, to go about the job of
406     tracing a network connection.  The lsof distribution contains
407     two Perl scripts, idrlogin.perl (Perl 4) and idrlogin.perl5
408     (Perl 5), that use lsof field output to display values for
409     shells that are parented by rlogind, sshd, or telnetd, or
410     connected directly to TCP sockets.  The lsof test suite contains
411     a C library that can be adapted for use with C programs that
412     need to call lsof and process its field output.
413
414     The two Perl scripts use the lsof -R option; it causes the
415     paRent process ID (PPID) to be listed in the lsof output.  The
416     scripts identify all shell processes -- e.g., ones whose command
417     names end in ``sh'' -- and determine if: 1) the ultimate ancestor
418     process before a PID greater than 2 (e.g., init's PID is 1) is
419     rlogind, sshd, or telnetd; or 2) the shell process has open
420     TCP socket files.
421
422     Here's an example of output from idlogin.perl on a Solaris 2.4
423     system:
424
425     centurion: 1 = cd src/lsof4/scripts
426     centurion: 2 = ./idrlogin.perl
427     Login    Shell       PID Via           PID TTY        From
428     oboyle   ksh       12640 in.telnetd  12638 pts/5      opal.cc.purdue.edu
429     icdtest  ksh       15158 in.rlogind  15155 pts/6      localhost
430     sh       csh       18207 in.rlogind  18205 pts/1      babylon5.cc.purdue.edu
431     root     csh       18242 in.rlogind  18205 pts/1      babylon5.cc.purdue.edu
432     trouble  ksh       19208 in.rlogind  18205 pts/1      babylon5.cc.purdue.edu
433     abe      ksh       21334 in.rlogind  21332 pts/2      lsof.itap.purdue.edu
434
435     The scripts assume that its parent directory contains an
436     executable lsof.  If you decide to use one of the scripts, you
437     may want to customize it for your local lsof and perl paths.
438
439     Note that processes executing as remote shells are also
440     identified.
441
442     Here's another example from a UnixWare 7.1.0 system.
443
444     tweeker: 1 = cd src/lsof4/scripts
445     tweeker: 9 = ./idrlogin.perl
446     Login    Shell       PID Via           PID TTY        From
447     abe      ksh        9438 in.telnetd   9436 pts/3      lsof.itap.purdue.edu
448
449
450 10. Watching an Ftp or Rcp Transfer
451 ===================================
452
453   The nature of the Internet being one of unpredictable performance
454   at times, occasionally you want to know if a file transfer, being
455   done by ftp or rcp, is making any progress.
456
457   To use lsof for watching a file transfer, you need to know the
458   PID of the file transfer process.  You can use ps to find that.
459   Then use lsof,
460
461   $ lsof -p<PID>
462
463   to examine the files open to the transfer process.  Usually the
464   ftp files or interest are at file descriptors 9 and 10 or 10 and
465   11; for rcp, 3 and 4.  They describe the network socket file and
466   the local data file.
467
468   If you want to watch only those file descriptors as the file
469   transfer progresses, try these lsof forms (for ftp in the example):
470
471     $ lsof -p<PID> -ad9,10 -r
472   or
473     $ lsof -p<PID> -ad10,11 -r
474
475   Some options need explaining:
476
477     -p<PID>     specifies that lsof is to restrict its attention
478                 to the process whose ID is <PID>.  You can specify
479                 a set of PIDs by separating them with commas.
480
481                     $ lsof -p 1234,5678,9012
482
483     -a          specifies that lsof is to AND its tests together.
484                 The two tests that are specified are tests on the
485                 PID and tests on file descriptions (``d9,10'').
486
487     d9,10       specifies that lsof is to test only file descriptors
488                 9 and 10.  Note that the `-' is absent, since ``-a''
489                 is a unary option and can be followed immediately
490                 by another lsof option.
491
492     -r          tells lsof to list the requested open file information,
493                 sleep for a default 15 seconds, then list the open
494                 file information again.  You can specify a different
495                 time (in seconds) after -r and override the default.
496                 Lsof issues a short line of equal signs between
497                 each set of output to distinguish it.
498
499   For an rcp transfer, the above example becomes:
500
501   $ lsof -p<PID> -ad3,4 -r
502
503
504 11. Listing Open NFS Files
505 ==========================
506
507   Lsof will list all files open on remote file systems, supported
508   by an NFS server.  Just use:
509
510   $ lsof -N
511
512   Note, however, that when run on an NFS server, lsof will not list
513   files open to the server from one of its clients.  That's because
514   lsof can only examine the processes running on the machine where
515   it is called -- i.e., on the NFS server.
516
517   If you run lsof on the NFS client, using the -N option, it will
518   list files open by processes on the client that are on remote
519   NFS file systems.
520
521
522 12. Listing Files Open by a Specific Login
523 ==========================================
524
525   If you're interested in knowing what files the processes owned
526   by a particular login name have open, lsof can help.
527
528     $ lsof -u<login>
529   or
530     $ lsof -u<User ID number>
531
532   You can specify either the login name or the UID associated with
533   it.  You can specify multiple login names and UID numbers, mixed
534   together, by separating them with commas.
535
536   $ lsof -u548,abe
537
538   On the subject of login names and UIDs, it's worth noting that
539   lsof can be told to report either.  By default it reports login
540   names; the -l option switches reporting to UIDs.  You might want
541   to use -l if login name lookup is slow for some reason.
542
543   a.  Ignoring a Specific Login
544   =============================
545
546     The -u option can also be used to direct lsof to ignore a
547     specific login name or UID, or a list of them.  Simply prefix
548     the login names or UIDs with a `^' character, as you might do
549     in a regular expression.  The `^' prefix is useful, for example,
550     when you want to have lsof ignore the files open to system
551     processes, owned by the root (UID 0) login.  Try:
552
553       $ lsof -u ^root
554     or
555       $ lsof -u ^0
556
557
558 13. Listing Files Open to a Specific Process Group
559 ==================================================
560
561   There's a Unix collection of processes called a process group.
562   The name indicates that the processes of the group have a common
563   association and are grouped so that a signal sent to one (e.g.,
564   a keyboard kill stroke) is delivered to all.
565
566   This causes Unix to create a two element process group:
567
568   $ lsof | less
569
570   You can use lsof to look at the open files of all members of a
571   process group, if you know the process group ID number.  Assuming
572   that it is 12717 for the above example, this lsof command:
573
574   $ lsof -g12717 -adcwd
575
576   would produce on a Solaris 8 system:
577
578   $ lsof -g12717 -adcwd
579   COMMAND   PID  PGID USER  FD TYPE DEVICE SIZE/OFF    NODE NAME
580   sshd    11369 12717 root cwd VDIR    0,2      189 1449175 /tmp (swap)
581   sshd    12717 12717 root cwd VDIR  136,0     1024       2 /
582
583   The ``-g12717'' option specifies the process group ID of interest;
584   the ``-adcwd'' option specifies that options are to be ANDed and
585   that lsof should limit file output to information about current
586   working directory (``cwd'') files.
587
588
589 14. When Lsof Seems to Hang
590 ===========================
591
592   On occasion when you run lsof it seems to hang and produce no
593   output.  This may result from system conditions beyond the control
594   of lsof.  Lsof has a number of options that may allow you to
595   bypass the blockage.
596
597   a.  Kernel lstat(), readlink(), and stat() Blockages
598   ====================================================
599
600     Lsof uses the kernel (system) calls lstat(), readlink(), and
601     stat() to locate mounted file system information.  When a file
602     system has been mounted from an NFS server and that server is
603     temporarily unavailable, the calls lsof uses may block in the
604     kernel.
605
606     Lsof will announce that it is being blocked with warning messages
607     (unless they have been suppressed by the lsof builder), but
608     only after a default waiting period of fifteen seconds has
609     expired for each file system whose server is unavailable.  If
610     you have a number of such file systems, the total wait may be
611     unacceptably long.
612
613     You can do two things to shorten your suffering: 1) reduce the
614     wait time with the -S option; or 2) tell lsof to avoid the
615     kernel calls that might block by specifying the -b option.
616
617       $ lsof -S 5
618     or
619       $ lsof -b
620
621     Avoiding the kernel calls that might block may result in the
622     lack of some information that lsof needs to know about mounted
623     file systems.  Thus, when you use -b, lsof warns that it might
624     lack important information.
625
626     The warnings that result from using -b (unless suppressed by
627     the lsof builder) can themselves be annoying.  You can suppress
628     them by adding the -w option.  (Of course, if you do, you won't
629     know what warning messages lsof might have issued.)
630
631     $ lsof -bw
632
633     Note: if the lsof builder suppressed warning message issuance,
634     you don't need to use -w to suppress them.  You can tell what
635     the default state of message warning issuance is by looking at
636     the -h (help) output.  If it says ``-w enable warnings'' then
637     warnings are disabled by default; ``-w disable warnings'', they
638     are enabled by default.
639
640   b.  Problems with /dev or /devices
641   ==================================
642
643     Lsof scans the /dev or /devices branch of your file system to
644     obtain information about your system's devices.  (The scan isn't
645     necessary when a device cache file exists.)
646
647     Sometimes that scan can take a very long time, especially if
648     you have a large number of devices, and if your kernel is
649     relatively slow to process the stat() system call on device
650     nodes.  You can't do anything about the stat() system call
651     speed.
652
653     However, you can make sure that lsof is allowed to use its
654     device cache file feature.  When lsof can use a device cache
655     file, it retains information it gleans via the stat() calls
656     on /dev or /devices in a separate file for later, faster
657     access.
658
659     The device cache file feature is described in the lsof man
660     page.  See the DEVICE CACHE FILE, LSOF PERMISSIONS THAT AFFECT
661     DEVICE CACHE FILE ACCESS, DEVICE CACHE FILE PATH FROM THE -D
662     OPTION, DEVICE CACHE PATH FROM AN ENVIRONMENT VARIABLE,
663     SYSTEM-WIDE DEVICE CACHE PATH, PERSONAL DEVICE CACHE PATH
664     (DEFAULT), and MODIFIED PERSONAL DEVICE CACHE PATH sections.
665
666     There is also a separate file in the lsof distribution, named
667     00DCACHE, that describes the device cache file in detail,
668     including information about possible security problems.
669
670     One final observation: don't overlook the possibility that your
671     /dev or /devices tree might be damaged.  See if
672
673       $ ls -R /dev
674     or
675       $ ls -R /devices
676
677     completes or hangs.  If it hangs, then lsof will probably hang,
678     too, and you should try to discover why ls hangs.
679
680     c.  Host and Service Name Lookup Hangs
681     ======================================
682
683     Lsof can hang up when it tries to convert an Internet dot-form
684     address to a host name, or a port number to a service name.  Both
685     hangs are caused by the lookup functions of your system.
686
687     An independent check for both types of hangs can be made with
688     the netstat program.  Run it without arguments.  If it hangs,
689     then it is probably having lookup difficulties.  When you run
690     it with -n it shouldn't hang and should report network and port
691     numbers instead of names.
692
693     Lsof has two options that serve the same purpose as netstat's
694     -n option.  The lsof -n option tells it to avoid host name
695     lookups; and -P, service name lookups.  Try those options when
696     you suspect lsof may be hanging because of lookup problems.
697
698       $ lsof -n
699     or
700       $ lsof -P
701     or
702       $ lsof -nP
703
704     d.  UID to Login Name Conversion Delays
705     =======================================
706
707     By default lsof converts User IDentification (UID) numbers to
708     login names when it produces output.  That conversion process
709     may sometimes hang because of system problems or interlocks.
710
711     You can tell lsof to skip the lookup with the -l option; it
712     will then report UIDs in the USER column.
713
714     $ lsof -l
715
716
717 15. Output for Other Programs
718 =============================
719
720   The -F option allows you to specify that lsof should describe
721   open files with a special form of output, called field output,
722   that can be parsed easily by a subsequent program.  The lsof
723   distribution comes with sample AWK, Perl 4, and Perl 5 scripts
724   that post-process field output.  The lsof test suite has a C
725   library that could be adapted for use by C programs that want to
726   process lsof field output from an in-bound pipe.
727
728   The lsof manual page describes field output in detail in its
729   OUTPUT FOR OTHER PROGRAMS section.  A quick look at a sample
730   script in the scripts/ subdirectory of the lsof distribution will
731   also give you an idea how field output works.
732
733   The most important thing about field output is that it is relatively
734   homogeneous across Unix dialects.  Thus, if you write a script
735   to post-process field output for AIX, it probably will work for
736   HP-UX, Solaris, and Ultrix as well.
737
738
739 16. The Lsof Exit Code and Shell Scripts
740 ========================================
741
742   When lsof exits successfully it returns an exit code based on
743   the result of its search for specified files.  (If no files were
744   specified, then the successful exit code is 0 (zero).)
745
746   If lsof was asked to search for specific files, including any
747   files on specified file systems, it returns an exit code of 0
748   (zero) if it found all the specified files and at least one file
749   on each specified file system.  Otherwise it returns a 1 (one).
750
751   If lsof detects an error and makes an unsuccessful exit, it
752   returns an exit code of 1 (one).
753
754   You can use the exit code in a shell script to search for files
755   on a file system and take action based on the result -- e.g.,
756
757     #!/bin/sh
758     lsof <file_system_name> > /dev/null 2>&1
759     if test $? -eq 0
760     then
761       echo "<file_system_name> has some users."
762     else
763       echo "<file_system_name> may have no users."
764     fi
765
766
767 17. Strange messages in the NAME column
768 =======================================
769
770   When lsof encounters problems analyzing a particular file, it may
771   put a message in the file's NAME column.  Many of those messages
772   are explained in the 00FAQ file of the lsof distribution.
773
774   So consult 00FAQ first if you encounter a NAME column message you
775   don't understand.  (00FAQ is a possible source of information
776   about other unfamiliar things in lsof output, too.)
777   
778   If you can't find help in 00FAQ, you can use grep to look in the
779   lsof source files for the message -- e.g.,
780
781     $ cd .../lsof_4.76_src
782     $ grep "can't identify protocol" *.[ch]
783
784   The code associated with the message will usually make clear the
785   reason for the message.
786
787   If you have an lsof source tree that has been processed by the
788   lsof Configure script, you need grep only there.  If, however,
789   your source tree hasn't been processed by Configure, you may
790   have to look in the top-level lsof source directory and in the
791   dialects sub-directory for the UNIX dialect you are using - e.g.,
792
793     $ cd .../lsof_4.76_src
794     $ grep "can't identify protocol" *.[ch]
795     $ cd dialects/Linux
796     $ grep "can't identify protocol" *.[ch]
797
798   In rare cases you may have to look in the lsof library, too --
799   e.g.,
800
801     $ cd .../lsof_4.76_src
802     $ grep "can't identify protocol" *.[ch]
803     $ cd dialects/Linux
804     $ grep "can't identify protocol" *.[ch]
805     $ cd ../../lib
806     $ grep "can't identify protocol" *.[ch]
807
808
809 Options
810 =======
811
812   The following appendices describe the lsof options in detail.
813
814
815 A.  Selection Options
816 ====================
817
818   Lsof has a rich set of options for selecting the files to be
819   displayed.  These include:
820
821         -a      tells lsof to AND the set of selection options that
822                 are specified.  Normally lsof ORs them.
823                 
824                 For example, if you specify the -p<PID> and -u<UID>
825                 options, lsof will display all files for the
826                 specified PID or for the specified UID.
827
828                 By adding -a, you specify that the listed files
829                 should be limited to PIDs owned by the specified
830                 UIDs -- i.e., they match the PIDs *and* the UIDs.
831
832                     $ lsof -p1234 -au 5678
833
834         -c      specifies that lsof should list files belonging
835                 to processes having the associated command name.
836
837                 Hint: if you want to select files based on more than
838                 one command name, use multiple -c<name> specifications.
839
840                     $ lsof -clsof -cksh
841
842         -d      tells lsof to select by the associated file descriptor
843                 (FD) set.  An FD set is a comma-separated list of
844                 numbers and the names lsof normally displays in
845                 its FD column:  cwd, Lnn, ltx, <number>, etc.  See
846                 the OUTPUT section of the lsof man page for the
847                 complete list of possible file descriptors.  Example:
848
849                     $ lsof -dcwd,0,1,2
850
851         -g      tells lsof to select by the associated process
852                 group ID (PGID) set.  The PGID set is a comma-separated
853                 list of PGID numbers.  When -g is specified, it also
854                 enables the display of PGID numbers.
855
856                 Note: when -g isn't followed by a PGID set, it
857                 simply selects the listing of PGID for all processes.
858                 Examples:
859
860                     $ lsof -g
861                     $ lsof -g1234,5678
862
863         -i      tells lsof to display Internet socket files.  If no
864                 protocol/address/port specification follows -i,
865                 lsof lists all Internet socket files.
866
867                 If a specification follows -i, lsof lists only the
868                 socket files whose Internet addresses match the
869                 specification.
870
871                 Hint: multiple addresses may be specified with
872                 multiple -i options.  Examples:
873
874                     $ lsof -iTCP
875                     $ lsof -i@lsof.itap.purdue.edu:sendmail
876
877         -N      selects the listing of files mounted on NFS devices.
878
879         -U      selects the listing of socket files in the Unix
880                 domain.
881
882
883 B.  Output Options
884 ==================
885
886   Lsof has these options to control its output format:
887
888         -F      produce output that can be parsed by a subsequent
889                 program.
890
891         -g      print process group (PGID) IDs.
892
893         -l      list UID numbers instead of login names.
894
895         -n      list network numbers instead of host names.
896
897         -o      always list file offset.
898
899         -P      list port numbers instead of port service names.
900
901         -s      always list file size.
902
903
904 C.  Precautionary Options
905 =========================
906
907   Lsof uses system functions that can block or take a long time,
908   depending on the health of the Unix dialect supporting it.  These
909   include:
910
911         -b      directs lsof to avoid system functions -- e.g.,
912                 lstat(2), readlink(2), stat(2) -- that might block
913                 in the kernel.  See the BLOCKS AND TIMEOUTS
914                 section of the lsof man page.
915
916                 You might want to use this option when you have
917                 a mount from an NFS server that is not responding.
918
919         -C      tells lsof to ignore the kernel's name cache.  As
920                 a precaution this option will have little effect on
921                 lsof performance, but might be useful if the kernel's
922                 name cache is scrambled.  (I've never seen that
923                 happen.)
924
925         -D      might be used to direct lsof to ignore an existing
926                 device cache file and generate a new one from /dev
927                 (and /devices).  This might be useful if you have
928                 doubts about the integrity of an existing device
929                 cache file.
930
931         -l      tells lsof to list UID numbers instead of login
932                 names -- this is useful when UID to login name
933                 conversion is slow or inoperative.
934
935         -n      tells lsof to avoid converting Internet addresses
936                 to host numbers.  This might be useful when your
937                 host name lookup (e.g., DNS) is inoperative.
938
939         -O      tells lsof to avoid its strategy of forking to
940                 perform potentially blocking kernel operations.
941                 While the forking allows lsof to detect that a
942                 block has occurred (and possibly break it), the
943                 fork operation is a costly one.  Use the -O option
944                 with care, lest your lsof be blocked.
945
946         -P      directs lsof to list port numbers instead of trying
947                 to convert them to port service names.  This might
948                 be useful if port to service name lookups (e.g.,
949                 via NIS) are slow or failing.
950
951         -S      can be used to change the lstat/readlink/stat
952                 timeout interval that governs how long lsof waits
953                 for response from the kernel.  This might be useful
954                 when an NFS server is slow or unresponsive.  When
955                 lsof times out of a kernel function, it may have
956                 less information to display.  Example:
957
958                     $ lsof -S2
959
960         -w      tells lsof to avoid issuing warning messages, if
961                 they are enabled by default, or enable them if they
962                 are disabled by default.  Check the -h (help) output
963                 to determine their status.  If it says ``-w enable
964                 warnings'', then warning messages are disabled by
965                 default; ``-w disable warnings'', they are enabled
966                 by default.
967
968                 This may be a useful option, for example, when you
969                 specify -b, if warning messages are enabled, because
970                 it will suppress the warning messages lsof issues
971                 about avoiding functions that might block in the
972                 kernel.
973
974
975 D.  Miscellaneous Lsof Options
976 ==============================
977
978   There are some lsof options that are hard to classify, including:
979
980         -?      these options select help output.
981         -h
982
983         -F      selects field output.  Field output is a mode where
984                 lsof produces output that can be parsed easily by
985                 subsequent programs -- e.g., AWK or Perl scripts.
986                 See ``15. Output for Other Programs'' for more
987                 information.
988
989         -k      specifies an alternate kernel symbol file -- i.e.,
990                 where nlist() will get its information.  Example:
991
992                     $ lsof -k/usr/crash/vmunix.1
993
994         -m      specifies an alternate kernel memory file from
995                 which lsof will read kernel structures in place
996                 of /dev/kmem or kvm_read().  Example:
997
998                     $ lsof -m/usr/crash/vmcore.n
999
1000         -r      tells lsof to repeat its scan every 15 seconds (the
1001                 default when no associated value is specified).  A
1002                 repeat time, different from the default, can follow
1003                 -r.  Example:
1004
1005                     $ lsof -r30
1006
1007         -v      displays information about the building of the
1008                 lsof executable.
1009
1010         --      The double minus sign option may be used to
1011                 signal the end of options.  It's particularly useful
1012                 when arguments to the last option are optional and
1013                 you want to supply a file path that could be confused
1014                 for arguments to the last option.  Example:
1015
1016                     $ lsof -g -- 1
1017                 
1018                 Where `1' is a file path, not PGID ID 1.
1019
1020
1021 Vic Abell <abe@purdue.edu>
1022 October 13, 2014