On Sunday 25 March 2012 18:47:14 Sven Oliver Moll wrote:
> 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
> 

Hi,
improved version with malloced/freed memory and better use
of libbb functions for further size reduction.

scripts/bloat-o-meter busybox_old busybox_unstripped
function                                             old     new   delta
lsof_main                                            385     290     -95
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-95)             Total: -95 bytes

Ciao,
Tito
 
int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
{
        char *fdpath;
        char *exelink;
        char *fdlink;
        char *tmp;
        char *rest = NULL;
        pid_t readpid = 0;
        struct dirent *entry_pid = (struct dirent *)0;
        struct dirent *entry_fd  = (struct dirent *)0;
        DIR *d_pid = (DIR*)0;
        DIR *d_fd  = (DIR*)0;
        
        d_pid = xopendir("/proc");
        for(;;) {
                entry_pid = readdir(d_pid);
                if(!entry_pid) {
                        closedir(d_pid);
                        return EXIT_SUCCESS;
                }
                errno = 0;
                readpid = bb_strtoul(entry_pid->d_name, &rest, 10);
                if( (getpid() != readpid) && (!errno) && (!*rest)) {
                        tmp = xasprintf("/proc/%s/exe", entry_pid->d_name);
                        exelink = xmalloc_readlink(tmp);
                        free(tmp);
                        fdpath = xasprintf("/proc/%s/fd/", entry_pid->d_name);
                        d_fd = opendir(fdpath);
                        if(d_fd) {
                                for(;;) {
                                        entry_fd = readdir(d_fd);
                                        if(!entry_fd) {
                                                break;
                                        }
                                        if(entry_fd->d_type == DT_LNK) {
                                                tmp = xasprintf("%s/%s", 
fdpath, entry_fd->d_name);
                                                fdlink =  xmalloc_readlink(tmp);
                                                free(tmp);
                                                printf("%d\t%s\t%s\n", readpid, 
exelink, fdlink);
                                                free(fdlink);
                                        }
                                }
                        }
                        closedir(d_fd);
                        free(fdpath);
                        free(exelink);
                }
        }
}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to