[PATCH] lsof: correct check for symbolic link

Thomas De Schampheleire patrickdepinguin+busybox at gmail.com
Wed Jun 19 16:52:51 UTC 2013


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at alcatel-lucent.com>
# Date 1371558718 -7200
# Node ID 38fb4edf4bb684a9a14ded6c15181e9d490d83ef
# Parent  77208178d3ef7ac035f10e5b6e0859be170ff788
lsof: correct check for symbolic link

Busybox lsof used the d_type field of a 'struct dirent' to verify whether the
entry is a symbolic link. This field, however, is not portable. On at least
one board [1] I have seen, that field is 0, and the check fails even though
the entry is a link.
This patch replaces the check on d_type with a call to lstat and a subsequent
check S_ISLNK. This should work in all cases, at the cost of the extra lstat
call.

[1] A MIPS-based board with glibc 2.9, Linux 2.6.32.27.

Signed-off-by: Thomas De Schampheleire <thomas.de.schampheleire at gmail.com>

---
 procps/lsof.c |  9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/procps/lsof.c b/procps/lsof.c
--- a/procps/lsof.c
+++ b/procps/lsof.c
@@ -61,8 +61,13 @@ int lsof_main(int argc UNUSED_PARAM, cha
 		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);
+				struct stat st;
+				safe_strncpy(name + baseofs, entry->d_name, 10);
+
+				if (lstat(name, &st) != 0)
+					continue;
+
+				if (S_ISLNK(st.st_mode)) {
 					fdlink = xmalloc_readlink(name);
 					printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink);
 					free(fdlink);


More information about the busybox mailing list