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
[email protected]
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;
}
/* 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"
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;
}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox