[PATCH] lsof reworked

souf souf_oued at yahoo.fr
Mon Mar 26 18:47:57 UTC 2012


Le 25/03/2012 18:47, Sven Oliver Moll a écrit :
> 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
>
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox
Hi,
You can try this :

int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
{
	DIR *d_fd;
	char *tmp, *tmp2;
	char *fdlink;
	struct dirent *entry;
	int scan_mask;
	procps_status_t *proc = NULL;

	scan_mask = PSSCAN_PID|PSSCAN_EXE;

	while ((proc = procps_scan(proc, scan_mask))) {
		tmp = xasprintf("/proc/%d/fd/", proc->pid);
		d_fd = opendir(tmp);

		if (d_fd) {
			while ((entry = readdir(d_fd))) {
				if (entry->d_type == DT_LNK) {
					tmp2 = xasprintf("%s%s", tmp, entry->d_name);
					fdlink = xmalloc_readlink(tmp2);
					free(tmp2);
					printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink);
					free(fdlink);
				}
			}
			closedir(d_fd);
		}
		free(tmp);
	}

	return EXIT_SUCCESS;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: lsof.c
Type: text/x-csrc
Size: 1132 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20120326/d241d042/attachment.c>


More information about the busybox mailing list