[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    =?us-ascii?Q?=5BPATCH=5D=20fuser=3A=20convert=20generic=20linked=20list=20implementation=20to=20the=
From:       <xmaks () email ! cz>
Date:       2010-05-20 13:34:48
Message-ID: 5232.1767.2711-11940-1360140834-1274362488 () email ! cz
[Download RAW message or body]

Hi Denis,

the patch converts fuser generic linked list implementation to the 'single instantion'.
It also change llist 'add' operation to 'append', because the previous works as a
stack's push and reversing output e.g.:

$ fuser /bin/bash
/bin/bash:            1978e  3619e  3705e 20476e
$ ./busybox fuser /bin/bash
20476 3705 3619 1978 

function                                             old     new   delta
fuser_main                                           663     947    +284
search_dev_inode                                      63      73     +10
get_pid_list                                           -       6      +6
get_ino_list                                           -       6      +6
static.l                                               -       4      +4
add_pid                                               38      39      +1
add_inode                                             84      85      +1
scan_dir_links                                       102      76     -26
scan_link                                             78      46     -32
file_to_dev_inode                                     64       -     -64
scan_proc_net                                        310       -    -310
------------------------------------------------------------------------------
(add/remove: 3/2 grow/shrink: 4/2 up/down: 312/-432)         Total: -120 bytes
   text	   data	    bss	    dec	    hex	filename
 701175	   2097	   9064	 712336	  ade90	busybox_old
 701054	   2097	   9072	 712223	  ade1f	busybox_unstripped

Max.

["=?us-ascii?Q?busybox=2Efuser=2Epatch?=" (text/x-patch)]

diff --git a/procps/fuser.c b/procps/fuser.c
index dc3d01b..3cfbb2b 100644
--- a/procps/fuser.c
+++ b/procps/fuser.c
@@ -44,16 +44,6 @@ static dev_t find_socket_dev(void)
 	return 0;
 }
 
-static int file_to_dev_inode(const char *filename, dev_t *dev, ino_t *inode)
-{
-	struct stat f_stat;
-	if (stat(filename, &f_stat))
-		return 0;
-	*inode = f_stat.st_ino;
-	*dev = f_stat.st_dev;
-	return 1;
-}
-
 static char *parse_net_arg(const char *arg, unsigned *port)
 {
 	char path[20], tproto[5];
@@ -63,54 +53,66 @@ static char *parse_net_arg(const char *arg, unsigned *port)
 	sprintf(path, "/proc/net/%s", tproto);
 	if (access(path, R_OK) != 0)
 		return NULL;
-	return xstrdup(tproto);
+	return xstrdup(path);
+}
+
+static pid_list **get_pid_list(void)
+{
+	static pid_list *l = NULL;
+	return &l;
 }
 
-static pid_list *add_pid(pid_list *plist, pid_t pid)
+static void add_pid(const pid_t pid)
 {
-	pid_list *curr = plist;
-	while (curr != NULL) {
-		if (curr->pid == pid)
-			return plist;
-		curr = curr->next;
+	pid_list **l = get_pid_list();
+
+	while (*l != NULL) {
+		if ((*l)->pid == pid)
+			return;
+		l = &(*l)->next;
 	}
-	curr = xmalloc(sizeof(pid_list));
-	curr->pid = pid;
-	curr->next = plist;
-	return curr;
+
+	*l = xzalloc(sizeof(pid_list));
+	(*l)->pid = pid;
 }
 
-static inode_list *add_inode(inode_list *ilist, dev_t dev, ino_t inode)
+static inode_list **get_ino_list(void)
 {
-	inode_list *curr = ilist;
-	while (curr != NULL) {
-		if (curr->inode == inode && curr->dev == dev)
-			return ilist;
-		curr = curr->next;
+	static inode_list *l = NULL;
+	return &l;
+}
+
+static void add_inode(const struct stat *st)
+{
+	inode_list **l = get_ino_list();
+
+	while (*l != NULL) {
+		if ((*l)->dev == st->st_dev &&
+		  (*l)->inode == st->st_ino
+		) {
+			return;
+		}
+		l = &(*l)->next;
 	}
-	curr = xmalloc(sizeof(inode_list));
-	curr->dev = dev;
-	curr->inode = inode;
-	curr->next = ilist;
-	return curr;
+
+	*l = xzalloc(sizeof(inode_list));
+	(*l)->dev = st->st_dev;
+	(*l)->inode = st->st_ino;
 }
 
-static inode_list *scan_proc_net(const char *proto,
-				unsigned port, inode_list *ilist)
+static void scan_proc_net(const char *path, unsigned port)
 {
-	char path[20], line[MAX_LINE + 1];
-	ino_t tmp_inode;
-	dev_t tmp_dev;
+	char line[MAX_LINE + 1];
 	long long uint64_inode;
 	unsigned tmp_port;
 	FILE *f;
+	struct stat st;
 
-	tmp_dev = find_socket_dev();
+	st.st_dev = find_socket_dev();
 
-	sprintf(path, "/proc/net/%s", proto);
 	f = fopen_for_read(path);
 	if (!f)
-		return ilist;
+		return;
 
 	while (fgets(line, MAX_LINE, f)) {
 		char addr[68];
@@ -124,71 +126,67 @@ static inode_list *scan_proc_net(const char *proto,
 			if (len > 8 && (option_mask32 & OPT_IP4))
 				continue;
 			if (tmp_port == port) {
-				tmp_inode = uint64_inode;
-				ilist = add_inode(ilist, tmp_dev, tmp_inode);
+				st.st_ino = uint64_inode;
+				add_inode(&st);
 			}
 		}
 	}
 	fclose(f);
-	return ilist;
 }
 
-static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode)
+static int search_dev_inode(const struct stat *st)
 {
-	while (ilist) {
-		if (ilist->dev == dev) {
+	inode_list *l = *get_ino_list();
+
+	while (l != NULL) {
+		if (l->dev == st->st_dev) {
 			if (option_mask32 & OPT_MOUNT)
 				return 1;
-			if (ilist->inode == inode)
+			if (l->inode == st->st_ino)
 				return 1;
 		}
-		ilist = ilist->next;
+		l = l->next;
 	}
+
 	return 0;
 }
 
-static pid_list *scan_pid_maps(const char *fname, pid_t pid,
-				inode_list *ilist, pid_list *plist)
+static void scan_pid_maps(const char *fname, pid_t pid)
 {
 	FILE *file;
 	char line[MAX_LINE + 1];
 	int major, minor;
-	ino_t inode;
 	long long uint64_inode;
-	dev_t dev;
+	struct stat st;
 
 	file = fopen_for_read(fname);
 	if (!file)
-		return plist;
+		return;
+
 	while (fgets(line, MAX_LINE, file)) {
 		if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
 			continue;
-		inode = uint64_inode;
-		if (major == 0 && minor == 0 && inode == 0)
+		st.st_ino = uint64_inode;
+		if (major == 0 && minor == 0 && st.st_ino == 0)
 			continue;
-		dev = makedev(major, minor);
-		if (search_dev_inode(ilist, dev, inode))
-			plist = add_pid(plist, pid);
+		st.st_dev = makedev(major, minor);
+		if (search_dev_inode(&st))
+			add_pid(pid);
 	}
 	fclose(file);
-	return plist;
 }
 
-static pid_list *scan_link(const char *lname, pid_t pid,
-				inode_list *ilist, pid_list *plist)
+static void scan_link(const char *lname, pid_t pid)
 {
-	ino_t inode;
-	dev_t dev;
+	struct stat st;
 
-	if (!file_to_dev_inode(lname, &dev, &inode))
-		return plist;
-	if (search_dev_inode(ilist, dev, inode))
-		plist = add_pid(plist, pid);
-	return plist;
+	if (stat(lname, &st) >= 0) {
+		if (search_dev_inode(&st))
+			add_pid(pid);
+	}
 }
 
-static pid_list *scan_dir_links(const char *dname, pid_t pid,
-				inode_list *ilist, pid_list *plist)
+static void scan_dir_links(const char *dname, pid_t pid)
 {
 	DIR *d;
 	struct dirent *de;
@@ -196,86 +194,89 @@ static pid_list *scan_dir_links(const char *dname, pid_t pid,
 
 	d = opendir(dname);
 	if (!d)
-		return plist;
+		return;
+
 	while ((de = readdir(d)) != NULL) {
 		lname = concat_subpath_file(dname, de->d_name);
 		if (lname == NULL)
 			continue;
-		plist = scan_link(lname, pid, ilist, plist);
+		scan_link(lname, pid);
 		free(lname);
 	}
 	closedir(d);
-	return plist;
 }
 
 /* NB: does chdir internally */
-static pid_list *scan_proc_pids(inode_list *ilist)
+static int scan_proc_pids(void)
 {
 	DIR *d;
 	struct dirent *de;
 	pid_t pid;
-	pid_list *plist;
 
 	xchdir("/proc");
 	d = opendir("/proc");
-	if (!d)
-		return NULL;
+	if (d == NULL)
+		return *get_pid_list() != NULL;
 
-	plist = NULL;
 	while ((de = readdir(d)) != NULL) {
 		pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
 		if (errno)
 			continue;
 		if (chdir(de->d_name) < 0)
 			continue;
-		plist = scan_link("cwd", pid, ilist, plist);
-		plist = scan_link("exe", pid, ilist, plist);
-		plist = scan_link("root", pid, ilist, plist);
-		plist = scan_dir_links("fd", pid, ilist, plist);
-		plist = scan_dir_links("lib", pid, ilist, plist);
-		plist = scan_dir_links("mmap", pid, ilist, plist);
-		plist = scan_pid_maps("maps", pid, ilist, plist);
+		scan_link("cwd", pid);
+		scan_link("exe", pid);
+		scan_link("root", pid);
+
+		scan_dir_links("fd", pid);
+		scan_dir_links("lib", pid);
+		scan_dir_links("mmap", pid);
+
+		scan_pid_maps("maps", pid);
 		xchdir("/proc");
 	}
 	closedir(d);
-	return plist;
+
+	return *get_pid_list() != NULL;
 }
 
-static int print_pid_list(pid_list *plist)
+static int print_pid_list(void)
 {
-	while (plist != NULL) {
-		printf("%u ", (unsigned)plist->pid);
-		plist = plist->next;
+	pid_list *l = *get_pid_list();
+
+	while (l != NULL) {
+		printf("%u ", (unsigned)l->pid);
+		l = l->next;
 	}
 	bb_putchar('\n');
+
 	return 1;
 }
 
-static int kill_pid_list(pid_list *plist, int sig)
+static int kill_pid_list(int sig)
 {
+	pid_list *l = *get_pid_list();
 	pid_t mypid = getpid();
 	int success = 1;
 
-	while (plist != NULL) {
-		if (plist->pid != mypid) {
-			if (kill(plist->pid, sig) != 0) {
-				bb_perror_msg("kill pid %u", (unsigned)plist->pid);
+	while (l != NULL) {
+		if (l->pid != mypid) {
+			if (kill(l->pid, sig) != 0) {
+				bb_perror_msg("kill pid %u", (unsigned)l->pid);
 				success = 0;
 			}
 		}
-		plist = plist->next;
+		l = l->next;
 	}
+
 	return success;
 }
 
 int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int fuser_main(int argc UNUSED_PARAM, char **argv)
 {
-	pid_list *plist;
-	inode_list *ilist;
 	char **pp;
-	dev_t dev;
-	ino_t inode;
+	struct stat st;
 	unsigned port;
 	int opt;
 	int success;
@@ -316,30 +317,28 @@ Find processes which use FILEs or PORTs
 	opt = getopt32(argv, OPTION_STRING);
 	argv += optind;
 
-	ilist = NULL;
 	pp = argv;
 	while (*pp) {
-		char *proto = parse_net_arg(*pp, &port);
-		if (proto) { /* PORT/PROTO */
-			ilist = scan_proc_net(proto, port, ilist);
-			free(proto);
+		char *path = parse_net_arg(*pp, &port);
+		if (path) { /* PORT/PROTO */
+			scan_proc_net(path, port);
+			free(path);
 		} else { /* FILE */
-			if (!file_to_dev_inode(*pp, &dev, &inode))
+			if (stat(*pp, &st) < 0)
 				bb_perror_msg_and_die("can't open '%s'", *pp);
-			ilist = add_inode(ilist, dev, inode);
+			add_inode(&st);
 		}
 		pp++;
 	}
 
-	plist = scan_proc_pids(ilist); /* changes dir to "/proc" */
-
-	if (!plist)
+	if (!scan_proc_pids()) /* changes dir to "/proc" */
 		return EXIT_FAILURE;
+
 	success = 1;
 	if (opt & OPT_KILL) {
-		success = kill_pid_list(plist, killsig);
+		success = kill_pid_list(killsig);
 	} else if (!(opt & OPT_SILENT)) {
-		success = print_pid_list(plist);
+		success = print_pid_list();
 	}
 	return (success != 1); /* 0 == success */
 }


_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic