diff options
author | Sven Oliver Moll <busybox@svol.li> | 2012-04-01 16:23:23 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2012-04-01 16:23:23 +0200 |
commit | 1c12b67705a4a2d5854d5e62fcbd2b241852ffe9 (patch) | |
tree | bdaab6dbe675f11f6bf3ea2388979dfae6b0a36d /procps | |
parent | 25dbb0743e58fc91ecb7ff5c793c5519eec45494 (diff) | |
download | busybox-w32-1c12b67705a4a2d5854d5e62fcbd2b241852ffe9.tar.gz busybox-w32-1c12b67705a4a2d5854d5e62fcbd2b241852ffe9.tar.bz2 busybox-w32-1c12b67705a4a2d5854d5e62fcbd2b241852ffe9.zip |
lsof: new applet
text data bss dec hex filename
221 0 0 221 dd lsof.o
Signed-off-by: Sven Oliver Moll <busybox@svol.li>
Signed-off-by: souf <souf_oued@yahoo.fr>
Signed-off-by: Tito Ragusa <farmatito@tiscali.it>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'procps')
-rw-r--r-- | procps/lsof.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/procps/lsof.c b/procps/lsof.c new file mode 100644 index 000000000..7e0ffa4e5 --- /dev/null +++ b/procps/lsof.c | |||
@@ -0,0 +1,76 @@ | |||
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 | |||
10 | //config:config LSOF | ||
11 | //config: bool "lsof" | ||
12 | //config: default y | ||
13 | //config: help | ||
14 | //config: Show open files in the format of: | ||
15 | //config: PID <TAB> /path/to/executable <TAB> /path/to/opened/file | ||
16 | |||
17 | //applet:IF_LSOF(APPLET(lsof, BB_DIR_USR_BIN, BB_SUID_DROP)) | ||
18 | |||
19 | //kbuild:lib-$(CONFIG_LSOF) += lsof.o | ||
20 | |||
21 | //usage:#define lsof_trivial_usage | ||
22 | //usage: "" | ||
23 | //usage:#define lsof_full_usage "\n\n" | ||
24 | //usage: "Show all open files" | ||
25 | |||
26 | #include "libbb.h" | ||
27 | |||
28 | /* | ||
29 | * Examples of "standard" lsof output: | ||
30 | * | ||
31 | * COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME | ||
32 | * init 1 root cwd DIR 8,5 4096 2 / | ||
33 | * init 1 root rtd DIR 8,5 4096 2 / | ||
34 | * init 1 root txt REG 8,5 872400 63408 /app/busybox-1.19.2/busybox | ||
35 | * rpc.portm 1064 root mem REG 8,5 43494 47451 /app/glibc-2.11/lib/libnss_files-2.11.so | ||
36 | * rpc.portm 1064 root 3u IPv4 2178 UDP *:111 | ||
37 | * rpc.portm 1064 root 4u IPv4 1244 TCP *:111 (LISTEN) | ||
38 | * runsvdir 1116 root 0r CHR 1,3 1214 /dev/null | ||
39 | * runsvdir 1116 root 1w CHR 1,3 1214 /dev/null | ||
40 | * runsvdir 1116 root 2w CHR 1,3 1214 /dev/null | ||
41 | * runsvdir 1116 root 3r DIR 8,6 1560 58359 /.local/var/service | ||
42 | * gpm 1128 root 4u unix 0xffff88007c09ccc0 1302 /dev/gpmctl | ||
43 | */ | ||
44 | |||
45 | int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
46 | int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | ||
47 | { | ||
48 | procps_status_t *proc = NULL; | ||
49 | |||
50 | while ((proc = procps_scan(proc, PSSCAN_PID|PSSCAN_EXE)) != NULL) { | ||
51 | char name[sizeof("/proc/%u/fd/0123456789") + sizeof(int)*3]; | ||
52 | unsigned baseofs; | ||
53 | DIR *d_fd; | ||
54 | char *fdlink; | ||
55 | struct dirent *entry; | ||
56 | |||
57 | if (getpid() == proc->pid) | ||
58 | continue; | ||
59 | |||
60 | baseofs = sprintf(name, "/proc/%u/fd/", proc->pid); | ||
61 | d_fd = opendir(name); | ||
62 | if (d_fd) { | ||
63 | while ((entry = readdir(d_fd)) != NULL) { | ||
64 | if (entry->d_type == DT_LNK) { | ||
65 | safe_strncpy(name + baseofs, entry->d_name, 10); | ||
66 | fdlink = xmalloc_readlink(name); | ||
67 | printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink); | ||
68 | free(fdlink); | ||
69 | } | ||
70 | } | ||
71 | closedir(d_fd); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | return EXIT_SUCCESS; | ||
76 | } | ||