From 034f35d7e634d3900c32e58e791bf631f30a1e57 Mon Sep 17 00:00:00 2001 From: "kay.sievers@vrfy.org" Date: Fri, 23 Jan 2004 03:01:02 -0800 Subject: [PATCH] [PATCH] udev - udevinfo with device chain walk udevinfo is now capable to print "all" attributes along the device chain of a sysfs device. Just like udev itself it walks the chain upwards and prints all usable attributes in the udev key format. So it should be easy to find unique attributes to compose a rule. All multiline attribute values and values containing non printable characters are skipped now. I hope nothing useful gets lost with this :) NOTE: The BUS value corresponding with the attributes is printed for every device. Don't specify BUS= in a rule and mix SYSFS_attributes from different busses, the rule can't match. ./udevinfo /sys/block/sda/sda1 device '/sys/block/sda/sda1' has major:minor 8:1 looking at class device '/sys/block/sda/sda1': SYSFS_dev="8:1" SYSFS_start="32" SYSFS_size="160" SYSFS_stat=" 0 0 0 0" follow the class device's "device" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host32/32:0:0:0': BUS="scsi" ID="32:0:0:0" SYSFS_detach_state="0" SYSFS_type="0" SYSFS_model="USB 2 HS-CF" SYSFS_vendor="SMSC " SYSFS_max_sectors="240" SYSFS_device_blocked="0" SYSFS_queue_depth="1" SYSFS_scsi_level="3" SYSFS_rev="1.25" SYSFS_online="1" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0/host32': BUS="" ID="host32" SYSFS_detach_state="0" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3/1-1.3:1.0': BUS="usb" ID="1-1.3:1.0" SYSFS_detach_state="0" SYSFS_bInterfaceNumber="00" SYSFS_bAlternateSetting=" 0" SYSFS_bNumEndpoints="02" SYSFS_bInterfaceClass="08" SYSFS_bInterfaceSubClass="06" SYSFS_bInterfaceProtocol="50" SYSFS_iInterface="00" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1.3': BUS="usb" ID="1-1.3" SYSFS_detach_state="0" SYSFS_bNumConfigurations="1" SYSFS_bNumInterfaces=" 1" SYSFS_bConfigurationValue="1" SYSFS_bmAttributes="80" SYSFS_bMaxPower=" 96mA" SYSFS_idVendor="0424" SYSFS_idProduct="20fc" SYSFS_bcdDevice="0125" SYSFS_bDeviceClass="00" SYSFS_bDeviceSubClass="00" SYSFS_bDeviceProtocol="00" SYSFS_speed="12" SYSFS_manufacturer="SMSC" SYSFS_product="USB 2 Flash Media Device" SYSFS_serial="0305037000C2" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1': BUS="usb" ID="1-1" SYSFS_detach_state="0" SYSFS_bNumConfigurations="1" SYSFS_bNumInterfaces=" 1" SYSFS_bConfigurationValue="1" SYSFS_bmAttributes="e0" SYSFS_bMaxPower=" 64mA" SYSFS_idVendor="03eb" SYSFS_idProduct="3301" SYSFS_bcdDevice="0300" SYSFS_bDeviceClass="09" SYSFS_bDeviceSubClass="00" SYSFS_bDeviceProtocol="00" SYSFS_speed="12" SYSFS_product="Standard USB Hub" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1': BUS="usb" ID="usb1" SYSFS_detach_state="0" SYSFS_bNumConfigurations="1" SYSFS_bNumInterfaces=" 1" SYSFS_bConfigurationValue="1" SYSFS_bmAttributes="40" SYSFS_bMaxPower=" 0mA" SYSFS_idVendor="0000" SYSFS_idProduct="0000" SYSFS_bcdDevice="0206" SYSFS_bDeviceClass="09" SYSFS_bDeviceSubClass="00" SYSFS_bDeviceProtocol="00" SYSFS_speed="12" SYSFS_manufacturer="Linux 2.6.2-rc1-p4 uhci_hcd" SYSFS_product="UHCI Host Controller" SYSFS_serial="0000:00:1d.0" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0': BUS="pci" ID="0000:00:1d.0" SYSFS_detach_state="0" SYSFS_vendor="0x8086" SYSFS_device="0x2482" SYSFS_subsystem_vendor="0x1014" SYSFS_subsystem_device="0x0220" SYSFS_class="0x0c0300" SYSFS_irq="9" looking at the device chain at '/sys/devices/pci0000:00': BUS="" ID="pci0000:00" SYSFS_detach_state="0" --- extras/udevinfo/udevinfo.c | 53 ++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/extras/udevinfo/udevinfo.c b/extras/udevinfo/udevinfo.c index 4f229cd3..c5934be2 100644 --- a/extras/udevinfo/udevinfo.c +++ b/extras/udevinfo/udevinfo.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "libsysfs.h" @@ -54,9 +55,23 @@ static int print_all_attributes(char *path) if (attr->value != NULL) { strncpy(value, attr->value, VALUE_SIZE); len = strlen(value); - if (value[len-1] == '\n') + if (len == 0) + continue; + + /* remove trailing newline */ + if (value[len-1] == '\n') { value[len-1] = '\0'; - printf(" SYSFS_%s=\"%s\"\n", attr->name, value); + len--; + } + + /* skip nonprintable values */ + while (len) { + if (isprint(value[len-1]) == 0) + break; + len--; + } + if (len == 0) + printf(" SYSFS_%s=\"%s\"\n", attr->name, value); } } printf("\n"); @@ -74,7 +89,8 @@ int main(int argc, char **argv, char **envp) struct sysfs_class_device *class_dev; struct sysfs_class_device *class_dev_parent; struct sysfs_attribute *attr; - struct sysfs_device *sysfs_device; + struct sysfs_device *sysfs_dev; + struct sysfs_device *sysfs_dev_parent; char *path; int retval = 0; @@ -98,33 +114,44 @@ int main(int argc, char **argv, char **envp) retval = -1; goto exit; } - printf("\ndevice '%s' has major:minor %s\n", class_dev->path, attr->value); + printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value); sysfs_close_attribute(attr); /* open sysfs class device directory and print all attributes */ - printf("looking at class device '%s':\n", class_dev->path); + printf(" looking at class device '%s':\n", class_dev->path); if (print_all_attributes(class_dev->path) != 0) { printf("couldn't open class device directory\n"); retval = -1; goto exit; } - /* get the device (if parent exists use it instead) */ + /* get the device link (if parent exists look here) */ class_dev_parent = sysfs_get_classdev_parent(class_dev); if (class_dev_parent != NULL) { //sysfs_close_class_device(class_dev); class_dev = class_dev_parent; } - sysfs_device = sysfs_get_classdev_device(class_dev); - if (sysfs_device != NULL) { - printf("follow class device's \"device\" link '%s':\n", class_dev->path); - printf(" BUS=\"%s\"\n", sysfs_device->bus); - printf(" ID=\"%s\"\n", sysfs_device->bus_id); + sysfs_dev = sysfs_get_classdev_device(class_dev); + if (sysfs_dev != NULL) + printf("follow the class device's \"device\"\n"); + + /* look the device chain upwards */ + while (sysfs_dev != NULL) { + printf(" looking at the device chain at '%s':\n", sysfs_dev->path); + printf(" BUS=\"%s\"\n", sysfs_dev->bus); + printf(" ID=\"%s\"\n", sysfs_dev->bus_id); /* open sysfs device directory and print all attributes */ - print_all_attributes(sysfs_device->path); - sysfs_close_device(sysfs_device); + print_all_attributes(sysfs_dev->path); + + sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev); + if (sysfs_dev_parent == NULL) + break; + + //sysfs_close_device(sysfs_dev); + sysfs_dev = sysfs_dev_parent; } + sysfs_close_device(sysfs_dev); exit: //sysfs_close_class_device(class_dev); -- 2.39.5