From 1c12b67705a4a2d5854d5e62fcbd2b241852ffe9 Mon Sep 17 00:00:00 2001 From: Sven Oliver Moll Date: Sun, 1 Apr 2012 16:23:23 +0200 Subject: lsof: new applet text data bss dec hex filename 221 0 0 221 dd lsof.o Signed-off-by: Sven Oliver Moll Signed-off-by: souf Signed-off-by: Tito Ragusa Signed-off-by: Denys Vlasenko --- procps/lsof.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 procps/lsof.c (limited to 'procps/lsof.c') 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 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini lsof implementation for busybox + * + * Copyright (C) 2012 by Sven Oliver 'SvOlli' Moll + * + * Licensed under GPLv2, see file LICENSE in this source tree. + */ + +//config:config LSOF +//config: bool "lsof" +//config: default y +//config: help +//config: Show open files in the format of: +//config: PID /path/to/executable /path/to/opened/file + +//applet:IF_LSOF(APPLET(lsof, BB_DIR_USR_BIN, BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_LSOF) += lsof.o + +//usage:#define lsof_trivial_usage +//usage: "" +//usage:#define lsof_full_usage "\n\n" +//usage: "Show all open files" + +#include "libbb.h" + +/* + * Examples of "standard" lsof output: + * + * COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME + * init 1 root cwd DIR 8,5 4096 2 / + * init 1 root rtd DIR 8,5 4096 2 / + * init 1 root txt REG 8,5 872400 63408 /app/busybox-1.19.2/busybox + * rpc.portm 1064 root mem REG 8,5 43494 47451 /app/glibc-2.11/lib/libnss_files-2.11.so + * rpc.portm 1064 root 3u IPv4 2178 UDP *:111 + * rpc.portm 1064 root 4u IPv4 1244 TCP *:111 (LISTEN) + * runsvdir 1116 root 0r CHR 1,3 1214 /dev/null + * runsvdir 1116 root 1w CHR 1,3 1214 /dev/null + * runsvdir 1116 root 2w CHR 1,3 1214 /dev/null + * runsvdir 1116 root 3r DIR 8,6 1560 58359 /.local/var/service + * gpm 1128 root 4u unix 0xffff88007c09ccc0 1302 /dev/gpmctl + */ + +int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) +{ + procps_status_t *proc = NULL; + + while ((proc = procps_scan(proc, PSSCAN_PID|PSSCAN_EXE)) != NULL) { + char name[sizeof("/proc/%u/fd/0123456789") + sizeof(int)*3]; + unsigned baseofs; + DIR *d_fd; + char *fdlink; + struct dirent *entry; + + if (getpid() == proc->pid) + continue; + + baseofs = sprintf(name, "/proc/%u/fd/", proc->pid); + d_fd = opendir(name); + if (d_fd) { + while ((entry = readdir(d_fd)) != NULL) { + if (entry->d_type == DT_LNK) { + safe_strncpy(name + baseofs, entry->d_name, 10); + fdlink = xmalloc_readlink(name); + printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink); + free(fdlink); + } + } + closedir(d_fd); + } + } + + return EXIT_SUCCESS; +} -- cgit v1.2.3