On Monday 26 March 2012 20:47:57 souf wrote:
> 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))) {
                if (getpid() == proc->pid))
                        continue;
>               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);
                                        //saves a few bytes.
                                        tmp2 = concat_path_file(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;
> }
> 
> 

Hi,
Looks good, but the output is not same as before because it is listing also the 
files
that lsof itself opens. I don't know if this behaviour is acceptable.
With this little fix it is as before and bloatometer is still good:

scripts/bloat-o-meter busybox_old busybox_unstripped
function                                             old     new   delta
.rodata                                           139563  139550     -13
lsof_main                                            385     174    -211
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-224)           Total: -224 bytes

Ciao,
Tito
/* 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))) {
		if (getpid() == proc->pid)
			continue;

		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 = concat_path_file(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

Reply via email to