[PATCH v2] lsof: correct check for symbolic link

Mike Frysinger vapier at gentoo.org
Fri Jun 21 16:05:04 UTC 2013


On Friday 21 June 2013 01:52:20 Thomas De Schampheleire wrote:
> --- 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);
> +				safe_strncpy(name + baseofs, entry->d_name, 10);
> +
> +				if (entry->d_type == DT_UNKNOWN) {

it's too bad busybox doesn't have unlikely() as this would be a good place to 
slap that

i would rewrite the code a little to avoid the constant strncpy (assuming it 
doesn't severely impact code size):
	while (...) {
		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) {
			... new lstat logic ...
		}

		... readlink + printf ...
	}
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20130621/77060600/attachment.asc>


More information about the busybox mailing list