Hello!

Again another offer for an lsof applet. To make it as small as possible, now all memory is allocated on the stack. If the complete path to an opened file
is larger that PATH_MAX, it will get truncated. On my x86_64 box, size(1) of
lsof.o reports 573 bytes.

Greetings,
SvOlli
--
|  _______       |
| (  /\          | The night will come and the sun will shine
|__)v\/lli a.k.a.| New love will come when pain's forgotten
|Sven Oliver Moll|   -- The Jinxs, "Pain's Forgotten"
diff --git a/include/applets.src.h b/include/applets.src.h
index 87d9cbb..cd08770 100644
--- a/include/applets.src.h
+++ b/include/applets.src.h
@@ -229,6 +229,7 @@ IF_LPQ(APPLET_ODDNAME(lpq, lpqr, BB_DIR_USR_BIN, BB_SUID_DROP, lpq))
 IF_LPR(APPLET_ODDNAME(lpr, lpqr, BB_DIR_USR_BIN, BB_SUID_DROP, lpr))
 IF_LS(APPLET_NOEXEC(ls, ls, BB_DIR_BIN, BB_SUID_DROP, ls))
 IF_LSATTR(APPLET(lsattr, BB_DIR_BIN, BB_SUID_DROP))
+IF_LSOF(APPLET(lsof, BB_DIR_USR_BIN, BB_SUID_DROP))
 IF_LSPCI(APPLET(lspci, BB_DIR_USR_BIN, BB_SUID_DROP))
 IF_LSUSB(APPLET(lsusb, BB_DIR_USR_BIN, BB_SUID_DROP))
 IF_UNLZMA(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
diff --git a/procps/Config.src b/procps/Config.src
index 570b026..070492e 100644
--- a/procps/Config.src
+++ b/procps/Config.src
@@ -46,6 +46,13 @@ config KILLALL5
 	default y
 	depends on KILL
 
+config LSOF
+	bool "lsof"
+	default y
+	help
+	  Show open files in the format of:
+          processid <TAB> /path/to/executable <TAB> /path/to/opened/file
+
 config PGREP
 	bool "pgrep"
 	default y
diff --git a/procps/Kbuild.src b/procps/Kbuild.src
index 89b1cc0..f8bee87 100644
--- a/procps/Kbuild.src
+++ b/procps/Kbuild.src
@@ -11,6 +11,7 @@ lib-$(CONFIG_FREE)	+= free.o
 lib-$(CONFIG_FUSER)	+= fuser.o
 lib-$(CONFIG_KILL)	+= kill.o
 lib-$(CONFIG_ASH)	+= kill.o  # used for built-in kill by ash
+lib-$(CONFIG_LSOF)	+= lsof.o
 lib-$(CONFIG_PGREP)	+= pgrep.o
 lib-$(CONFIG_PKILL)	+= pgrep.o
 lib-$(CONFIG_PIDOF)	+= pidof.o
diff --git a/procps/lsof.c b/procps/lsof.c
new file mode 100644
index 0000000..a05d06b
--- /dev/null
+++ b/procps/lsof.c
@@ -0,0 +1,90 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini lsof implementation for busybox
+ *
+ * Copyright (C) 2012 by Sven Oliver 'SvOlli' Moll <[email protected]>
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ */
+
+/* getopt not needed */
+
+//usage:#define lsof_trivial_usage
+//usage:       ""
+//usage:#define lsof_full_usage "\n\n"
+//usage:       "Show all open files"
+
+#include "libbb.h"
+#include <sys/types.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <string.h>
+#include <alloca.h>
+#include <stdlib.h>
+#include <libgen.h>
+#include <errno.h>
+
+
+int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
+{
+   char exepath[80];
+   char fdpath[80];
+   char exelink[PATH_MAX];
+   char fdlink[PATH_MAX];
+   char *rest = NULL;
+   pid_t readpid = 0;
+   size_t fdend_pos = 0; 
+   struct dirent *entry_pid = (struct dirent *)0;
+   struct dirent *entry_fd  = (struct dirent *)0;
+   DIR *d_pid = (DIR*)0;
+   DIR *d_fd  = (DIR*)0;
+   
+   d_pid = opendir( "/proc" );
+   if( !d_pid )
+   {
+      return 1;
+   }
+
+   for(;;)
+   {
+      entry_pid = readdir( d_pid );
+      if( !entry_pid )
+      {
+         closedir( d_pid );
+         return EXIT_SUCCESS;
+      }
+
+      errno = 0;
+      readpid = bb_strtoul( entry_pid->d_name, &rest, 10 );
+      if( (getpid() != readpid) && (!errno) && (!*rest) )
+      {
+         snprintf( exepath, sizeof(exepath), "/proc/%s/exe", entry_pid->d_name );
+         exelink[ readlink( exepath, exelink, sizeof(exelink) ) ] = '\0';
+         snprintf( fdpath, sizeof(fdpath), "/proc/%s/fd/", entry_pid->d_name );
+         fdend_pos = strlen( fdpath );
+         d_fd = opendir( fdpath );
+         if( d_fd )
+         {
+            for(;;)
+            {
+               entry_fd = readdir( d_fd );
+               if( !entry_fd )
+               {
+                  break;
+               }
+               if( entry_fd->d_type == DT_LNK )
+               {
+                  strncpy( &fdpath[fdend_pos], entry_fd->d_name, sizeof(fdpath)-fdend_pos-1 );
+                  fdpath[sizeof(fdpath)-1] = '\0';
+                  fdlink[ readlink( fdpath, fdlink, sizeof(fdlink) ) ] = '\0';
+                  printf( "%d\t%s\t%s\n", readpid, exelink, fdlink );
+               }
+            }
+            closedir( d_fd );
+         }
+      }
+   }
+}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to