From 56615f203bf78684d05828c2718325016f0d025f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 5 Dec 2021 01:58:01 +0900 Subject: [PATCH 1/2] udev/path_id: fix skip_subsystem() This partially reverts d340bdd1bd435e9f5524f4246feaf38511b2ff45. The function previously drops multiple except for the last parents which matches the specified subsystem. But the commit d340bdd1bd435e9f5524f4246feaf38511b2ff45 drops all parents. E.g. input is pci-pci-pci-usb-usb-usb, then Before d340bdd: pci-pci-pci-usb After d340bdd: pci-pci-pci --- src/udev/udev-builtin-path_id.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index b46a1e5af6..9f71fadeea 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -80,19 +80,29 @@ static int format_lun_number(sd_device *dev, char **path) { } static sd_device *skip_subsystem(sd_device *dev, const char *subsys) { + sd_device *parent; + assert(dev); assert(subsys); - for (;;) { + /* Unlike the function name, this drops multiple parent devices EXCEPT FOR THE LAST ONE. + * The last one will be dropped at the end of the loop in builtin_path_id(). + * E.g. + * Input: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 + * Output: /sys/devices/pci0000:00/0000:00:14.0/usb1 + */ + + for (parent = dev; ; ) { const char *subsystem; - if (sd_device_get_subsystem(dev, &subsystem) < 0) + if (sd_device_get_subsystem(parent, &subsystem) < 0) break; if (!streq(subsystem, subsys)) break; - if (sd_device_get_parent(dev, &dev) < 0) + dev = parent; + if (sd_device_get_parent(dev, &parent) < 0) break; } From 6885a857e4071536d3e378f0bf41c02e959a7681 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 6 Dec 2021 16:05:35 +0900 Subject: [PATCH 2/2] udev/path-id: comment why USB host number is dropped from the PATH_ID Closes #21617. The comment is borrowed from https://github.com/systemd/systemd/pull/21620#discussion_r762521508. --- src/udev/udev-builtin-path_id.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index 9f71fadeea..6fdd3eceea 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -503,6 +503,10 @@ static sd_device *handle_usb(sd_device *parent, char **path) { return parent; port++; + /* USB host number may change across reboots (and probably even without reboot). The part after + * USB host number is determined by device topology and so does not change. Hence, drop the + * host number and always use '0' instead. */ + path_prepend(path, "usb-0:%s", port); return skip_subsystem(parent, "usb"); }