lspci

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Tue Nov 17 22:52:08 UTC 2009


On Tue, Nov 17, 2009 at 11:03:56PM +0100, Souf Oued wrote:
>Hi all,
>
>I Thank you for your comments, I send you a new version with lspci
>and lsusb.

please provide size(1) and bloat-o-meter output

I'm not convinced putting both lspci and lsusb into one single file is a
good idea (they don't really share anything, it could well be that it's
smaller to have them separate).

>diff -U 3 -H -d -p -r -N -- busybox.orig/include/usage.h busybox/include/usage.h
>--- busybox.orig/include/usage.h	2009-11-16 05:51:18.000000000 +0100
>+++ busybox/include/usage.h	2009-11-17 22:37:54.726828604 +0100
>@@ -2493,6 +2493,19 @@
> #define lsmod_full_usage "\n\n" \
>        "List the currently loaded kernel modules"
> 
>+#define lspci_trivial_usage \
>+       "[-mk]"
>+#define lspci_full_usage "\n\n" \
>+       "List all PCI devices" \
>+     "\n" \
>+     "\n	m	Produce easily parsed data by scripts." \

just "Parseable output"

>+     "\n	k	Dump kernel driver." \
s/\.//

>+
>+#define lsusb_trivial_usage \
>+       ""

#define lsusb_trivial_usage NOUSAGE_STR

>+#define lsusb_full_usage "\n\n" \
>+       "List all USB devices"
>+
> #if ENABLE_FEATURE_MAKEDEVS_LEAF
> #define makedevs_trivial_usage \
>        "NAME TYPE MAJOR MINOR FIRST LAST [s]"
>diff -U 3 -H -d -p -r -N -- busybox.orig/util-linux/Config.in busybox/util-linux/Config.in
>--- busybox.orig/util-linux/Config.in	2009-11-16 05:51:18.000000000 +0100
>+++ busybox/util-linux/Config.in	2009-11-17 22:33:43.008016386 +0100
>@@ -348,6 +348,26 @@ config LOSETUP
> 	  file or block device, and to query the status of a loop device. This
> 	  version does not currently support enabling data encryption.
> 
>+config LSPCI
>+	bool "lspci"
>+	default n
>+	help
>+	  lspci is a utility for displaying information about PCI buses in the
>+          system and devices connected to them.
>+
>+	  This vesrsion uses only sysfs (/sys/bus/pci/devices) to collect
>+	  informations.

typo
	  This version uses sysfs (/sys/bus/pci/devices) only.

>+
>+config LSUSB
>+	bool "lsusb"
>+	default n
>+	help
>+	  lsusb is a utility for displaying information about USB buses in the
>+          system and devices connected to them.
>+
>+	  This vesrsion uses only sysfs (/sys/bus/usb/devices) to collect
>+	  informations.

see above

>+
> config MDEV
> 	bool "mdev"
> 	default n
>diff -U 3 -H -d -p -r -N -- busybox.orig/util-linux/Kbuild busybox/util-linux/Kbuild
>--- busybox.orig/util-linux/Kbuild	2009-11-16 05:51:18.000000000 +0100
>+++ busybox/util-linux/Kbuild	2009-11-17 22:34:32.104830061 +0100
>@@ -21,6 +21,8 @@ lib-$(CONFIG_HWCLOCK)           += hwclo
> lib-$(CONFIG_IPCRM)             += ipcrm.o
> lib-$(CONFIG_IPCS)              += ipcs.o
> lib-$(CONFIG_LOSETUP)           += losetup.o
>+lib-$(CONFIG_LSPCI)             += lspci.o
>+lib-$(CONFIG_LSUSB)             += lspci.o
> lib-$(CONFIG_MDEV)              += mdev.o
> lib-$(CONFIG_MKFS_EXT2)         += mkfs_ext2.o
> lib-$(CONFIG_MKFS_MINIX)        += mkfs_minix.o
>diff -U 3 -H -d -p -r -N -- busybox.orig/util-linux/lspci.c busybox/util-linux/lspci.c
>--- busybox.orig/util-linux/lspci.c	1970-01-01 01:00:00.000000000 +0100
>+++ busybox/util-linux/lspci.c	2009-11-17 22:25:46.000000000 +0100
>@@ -0,0 +1,175 @@

missing copyright information
See e.g.
$ head miscutils/eject.c
and copy that

>+#include <libbb.h>
>+
>+#define SCRATCH_SIZE 80
>+
>+/*
>+ * read_uevent: reads uevent file and write the values in the same order as given keys.
>+ */
>+static void read_uevent(const char *fileName, const char **keys, const char *values[])
>+{
>+	FILE *uevent;
>+	char *uevent_line;
>+
>+	uevent = xfopen_for_read(fileName);
>+	while ((uevent_line = xmalloc_fgetline(uevent)) != NULL) {
>+		int i;
>+
>+		for (i=0; keys[i]; i++) {
>+			char *s = strstr(uevent_line, keys[i]);
>+			if (s) {
>+				values[i] = xstrdup(s+strlen(keys[i])+1);
>+			}
>+		}
>+	}
>+	free(uevent_line);
>+	fclose(uevent);
>+}
>+
>+#ifdef CONFIG_LSPCI
>+enum {
>+	OPT_m = (1<<0) * ENABLE_LSPCI,
>+	OPT_k = (1<<1) * ENABLE_LSPCI,
>+};
>+
>+static void printf_pci(const char *fileName)
>+{
>+	const char *values[64];
>+	const char *pci_keys[] = {"PCI_SLOT_NAME", "PCI_CLASS", "PCI_ID", "PCI_SUBSYS_ID", "DRIVER", NULL};
>+	char did[5];
>+
>+	memset(did, 0, 5);
>+
>+	read_uevent(fileName, pci_keys, values);
>+
>+	// PCI_SLOT_NAME
>+	printf("%s ", values[0]+5);
>+
>+	// PCI_CLASS
>+	if (option_mask32 & OPT_m)
>+		printf("\"%04x\"", xstrtou(values[1], 16)>>8);
>+	else
>+		printf("%04x:", xstrtou(values[1], 16)>>8);
>+
>+	// PCI_ID  VID
>+	memcpy(did, values[2], 4);
>+
>+	if (option_mask32 & OPT_m)
>+		printf(" \"%04x\" ", xstrtou(did, 16));
>+	else
>+		printf(" %04x:", xstrtou(did, 16));
>+	// PCI_ID  DID
>+	memcpy(did, values[2]+5, 4);
>+	if (option_mask32 & OPT_m)
>+		printf("\"%04x\"", xstrtou(did, 16));
>+	else
>+		printf("%04x", xstrtou(did, 16));
>+
>+	if (option_mask32 & OPT_m) {
>+		// PCI_SUBSYS_ID VID
>+		memcpy(did, values[3], 4);
>+		if (option_mask32 & OPT_m)
>+			printf(" \"%04x\" ", xstrtou(did, 16));
>+		else
>+			printf(" %04x:", xstrtou(did, 16));
>+
>+		// PCI_SUBSYS_ID DID
>+		memcpy(did, values[3]+5, 4);
>+		if (option_mask32 & OPT_m)
>+			printf("\"%04x\"", xstrtou(did, 16));
>+		else
>+			printf("%04x", xstrtou(did, 16));
>+	}
>+
>+	if ((option_mask32 & OPT_k) && values[4]) {
>+		if (option_mask32 & OPT_m) {
>+			printf(" \"%s\"", values[4]);
>+		} else {
>+			printf(" %s", values[4]);
>+		}
>+	}
>+	bb_putchar('\n');
>+}

This functions sounds rather big.
>+#endif
>+
>+#ifdef CONFIG_LSUSB
>+static void printf_usb(const char *fileName)
>+{
>+	const char *values[3];
>+	const char *usb_keys[] = {"DEVTYPE", "DEVICE", "PRODUCT", NULL};
>+	char did[5];
>+	const char *s1, *s2;
>+
>+	memset(did, 0, 5);
>+
>+	read_uevent(fileName, usb_keys, values);
>+
>+	if (strcmp(values[0], "usb_interface")) return;
>+
>+	memcpy(did, values[1]+strlen("/proc/bus/usb/"), 3);
>+	printf("Bus %s Device %s: ", did, values[1]+strlen("/proc/bus/usb/000/"));
>+
>+	s1 = values[2];
>+	if (s1[3] == '/') {
>+		did[0] = '0';
>+		memcpy(did+1, s1, 3);
>+		s1 += 4;
>+	} else {
>+		memcpy(did, s1, 4);
>+		s1 += 5;
>+	}
>+	printf("ID %s:", did);
>+
>+	s2 = strchr(s1, '/');
>+
>+	memset(did, 0, 5);
>+	memcpy(did, s1, strlen(s1) - strlen(s2));
>+
>+	printf("%04x", xstrtou(did, 16));
>+	bb_putchar('\n');
>+}
>+#endif
>+
>+static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf UNUSED_PARAM, void *userData, int depth UNUSED_PARAM)
>+{
>+	strcpy(userData, (char *)fileName);
>+	strcat(userData, "/uevent");

concat_path_file()
>+
>+	switch (applet_name[2]) {
>+#ifdef CONFIG_LSPCI
>+	case 'p':
>+		printf_pci(userData);
>+		break;
>+#endif
>+#ifdef CONFIG_LSUSB
>+	case 'u':
>+		printf_usb(userData);
>+		break;
>+#endif
>+	default:
>+		break;
>+	}

I wouldn't be surprised if a plain if-else chain would be smaller (not
sure if that was fixed in gcc already; google flatten-switch-stmt

>+
>+	return TRUE;
>+}
>+
>+int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
>+int lspci_main(int argc UNUSED_PARAM, char **argv)
>+{
>+	RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
>+	const char *bus;
>+
>+	if (applet_name[2] == 'p') { /* lspci */
>+		bus = "/sys/bus/pci/devices";
>+		getopt32(argv, "mk");
>+	} else { /* lsusb */
>+		if (argc != 1)
>+			bb_show_usage();
>+		bus = "/sys/bus/usb/devices";
>+	}
>+
>+	recursive_action(bus,
>+			 ACTION_RECURSE,
>+			 fileAction, NULL, temp, 0);
>+
>+	return EXIT_SUCCESS;
>+}


More information about the busybox mailing list