hush: if we did match "LINENO" or "OPTIND", stop further comparisons
[oweals/busybox.git] / procps / lsof.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini lsof implementation for busybox
4  *
5  * Copyright (C) 2012 by Sven Oliver 'SvOlli' Moll <svolli@svolli.de>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 //config:config LSOF
10 //config:       bool "lsof (3.6 kb)"
11 //config:       default y
12 //config:       help
13 //config:       Show open files in the format of:
14 //config:       PID <TAB> /path/to/executable <TAB> /path/to/opened/file
15
16 //applet:IF_LSOF(APPLET(lsof, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18 //kbuild:lib-$(CONFIG_LSOF) += lsof.o
19
20 //usage:#define lsof_trivial_usage
21 //usage:       ""
22 //usage:#define lsof_full_usage "\n\n"
23 //usage:       "Show all open files"
24
25 #include "libbb.h"
26
27 /*
28  * Examples of "standard" lsof output:
29  *
30  * COMMAND    PID USER   FD   TYPE             DEVICE     SIZE       NODE NAME
31  * init         1 root  cwd    DIR                8,5     4096          2 /
32  * init         1 root  rtd    DIR                8,5     4096          2 /
33  * init         1 root  txt    REG                8,5   872400      63408 /app/busybox-1.19.2/busybox
34  * rpc.portm 1064 root  mem    REG                8,5    43494      47451 /app/glibc-2.11/lib/libnss_files-2.11.so
35  * rpc.portm 1064 root    3u  IPv4               2178                 UDP *:111
36  * rpc.portm 1064 root    4u  IPv4               1244                 TCP *:111 (LISTEN)
37  * runsvdir  1116 root    0r   CHR                1,3                1214 /dev/null
38  * runsvdir  1116 root    1w   CHR                1,3                1214 /dev/null
39  * runsvdir  1116 root    2w   CHR                1,3                1214 /dev/null
40  * runsvdir  1116 root    3r   DIR                8,6     1560      58359 /.local/var/service
41  * gpm       1128 root    4u  unix 0xffff88007c09ccc0                1302 /dev/gpmctl
42  */
43
44 int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45 int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
46 {
47         procps_status_t *proc = NULL;
48
49         while ((proc = procps_scan(proc, PSSCAN_PID|PSSCAN_EXE)) != NULL) {
50                 char name[sizeof("/proc/%u/fd/0123456789") + sizeof(int)*3];
51                 unsigned baseofs;
52                 DIR *d_fd;
53                 char *fdlink;
54                 struct dirent *entry;
55
56                 if (getpid() == proc->pid)
57                         continue;
58
59                 baseofs = sprintf(name, "/proc/%u/fd/", proc->pid);
60                 d_fd = opendir(name);
61                 if (d_fd) {
62                         while ((entry = readdir(d_fd)) != NULL) {
63                                 /* Skip entries '.' and '..' (and any hidden file) */
64                                 if (entry->d_name[0] == '.')
65                                         continue;
66
67                                 safe_strncpy(name + baseofs, entry->d_name, 10);
68                                 if ((fdlink = xmalloc_readlink(name)) != NULL) {
69                                         printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink);
70                                         free(fdlink);
71                                 }
72                         }
73                         closedir(d_fd);
74                 }
75         }
76
77         return EXIT_SUCCESS;
78 }