[PATCH v3] lsof: correct check for symbolic link
Thomas De Schampheleire
patrickdepinguin+busybox at gmail.com
Fri Jun 21 17:21:44 UTC 2013
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.
With this patch, the symbolic link check is done with lstat and a subsequent
S_ISLNK check, if the d_type field does not contain useful information.
If neither works, ignore the entry.
[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>
---
v2: avoid lstat overhead when possible
v3: avoid strncpy overhead when possible
procps/lsof.c | 22 +++++++++++++++++-----
1 files changed, 17 insertions(+), 5 deletions(-)
Size impact (x86, based on allnoconfig):
original lsof:
text data bss dec hex filename
5633 414 20 6067 17b3 busybox
234 0 0 234 ea procps/lsof.o
patch-v2:
text data bss dec hex filename
5828 418 20 6266 187a busybox
290 0 0 290 122 procps/lsof.o
patch-v3:
text data bss dec hex filename
5837 418 20 6275 1883 busybox
299 0 0 299 12b procps/lsof.o
diff --git a/procps/lsof.c b/procps/lsof.c
--- a/procps/lsof.c
+++ b/procps/lsof.c
@@ -61,12 +61,24 @@ 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);
- fdlink = xmalloc_readlink(name);
- printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink);
- free(fdlink);
+ if (entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK)
+ continue;
+
+ safe_strncpy(name + baseofs, entry->d_name, 10);
+
+ if (entry->d_type == DT_UNKNOWN) {
+ struct stat st;
+
+ if (lstat(name, &st) != 0)
+ continue;
+
+ if (!S_ISLNK(st.st_mode))
+ continue;
}
+
+ fdlink = xmalloc_readlink(name);
+ printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink);
+ free(fdlink);
}
closedir(d_fd);
}
More information about the busybox
mailing list