3
0

lsof.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.4 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. //applet:IF_LSOF(APPLET(lsof, BB_DIR_USR_BIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_LSOF) += lsof.o
  17. //usage:#define lsof_trivial_usage
  18. //usage: ""
  19. //usage:#define lsof_full_usage "\n\n"
  20. //usage: "Show all open files"
  21. #include "libbb.h"
  22. /*
  23. * Examples of "standard" lsof output:
  24. *
  25. * COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  26. * init 1 root cwd DIR 8,5 4096 2 /
  27. * init 1 root rtd DIR 8,5 4096 2 /
  28. * init 1 root txt REG 8,5 872400 63408 /app/busybox-1.19.2/busybox
  29. * rpc.portm 1064 root mem REG 8,5 43494 47451 /app/glibc-2.11/lib/libnss_files-2.11.so
  30. * rpc.portm 1064 root 3u IPv4 2178 UDP *:111
  31. * rpc.portm 1064 root 4u IPv4 1244 TCP *:111 (LISTEN)
  32. * runsvdir 1116 root 0r CHR 1,3 1214 /dev/null
  33. * runsvdir 1116 root 1w CHR 1,3 1214 /dev/null
  34. * runsvdir 1116 root 2w CHR 1,3 1214 /dev/null
  35. * runsvdir 1116 root 3r DIR 8,6 1560 58359 /.local/var/service
  36. * gpm 1128 root 4u unix 0xffff88007c09ccc0 1302 /dev/gpmctl
  37. */
  38. int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  40. {
  41. procps_status_t *proc = NULL;
  42. while ((proc = procps_scan(proc, PSSCAN_PID|PSSCAN_EXE)) != NULL) {
  43. char name[sizeof("/proc/%u/fd/0123456789") + sizeof(int)*3];
  44. unsigned baseofs;
  45. DIR *d_fd;
  46. char *fdlink;
  47. struct dirent *entry;
  48. if (getpid() == proc->pid)
  49. continue;
  50. baseofs = sprintf(name, "/proc/%u/fd/", proc->pid);
  51. d_fd = opendir(name);
  52. if (d_fd) {
  53. while ((entry = readdir(d_fd)) != NULL) {
  54. /* Skip entries '.' and '..' (and any hidden file) */
  55. if (entry->d_name[0] == '.')
  56. continue;
  57. safe_strncpy(name + baseofs, entry->d_name, 10);
  58. if ((fdlink = xmalloc_readlink(name)) != NULL) {
  59. printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink);
  60. free(fdlink);
  61. }
  62. }
  63. closedir(d_fd);
  64. }
  65. }
  66. return EXIT_SUCCESS;
  67. }