static int test_matches(
sd_device_enumerator *enumerator,
- sd_device *device) {
+ sd_device *device,
+ bool ignore_parent_match) {
int r;
if (r <= 0)
return r;
- if (!device_match_parent(device, enumerator->match_parent, NULL))
+ if (!ignore_parent_match &&
+ !device_match_parent(device, enumerator->match_parent, NULL))
return false;
if (!match_tag(enumerator, device))
return false;
}
+static int enumerator_add_parent_devices(
+ sd_device_enumerator *enumerator,
+ sd_device *device,
+ bool ignore_parent_match) {
+
+ int k, r = 0;
+
+ assert(enumerator);
+ assert(device);
+
+ for (;;) {
+ const char *ss, *usn;
+
+ k = sd_device_get_parent(device, &device);
+ if (k == -ENOENT) /* Reached the top? */
+ break;
+ if (k < 0) {
+ r = k;
+ break;
+ }
+
+ k = sd_device_get_subsystem(device, &ss);
+ if (k == -ENOENT) /* Has no subsystem? */
+ continue;
+ if (k < 0) {
+ r = k;
+ break;
+ }
+
+ if (!match_subsystem(enumerator, ss))
+ continue;
+
+ k = sd_device_get_sysname(device, &usn);
+ if (k < 0) {
+ r = k;
+ break;
+ }
+
+ if (!match_sysname(enumerator, usn))
+ continue;
+
+ k = test_matches(enumerator, device, ignore_parent_match);
+ if (k < 0) {
+ r = k;
+ break;
+ }
+ if (k == 0)
+ continue;
+
+ k = device_enumerator_add_device(enumerator, device);
+ if (k < 0) {
+ r = k;
+ break;
+ }
+ if (k == 0) /* Exists already? Then no need to go further up. */
+ break;
+ }
+
+ return r;
+}
+
+int device_enumerator_add_parent_devices(sd_device_enumerator *enumerator, sd_device *device) {
+ return enumerator_add_parent_devices(enumerator, device, /* ignore_parent_match = */ true);
+}
+
static bool relevant_sysfs_subdir(const struct dirent *de) {
assert(de);
FOREACH_DIRENT_ALL(de, dir, return -errno) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
char syspath[strlen(path) + 1 + strlen(de->d_name) + 1];
- sd_device *upwards;
if (!relevant_sysfs_subdir(de))
continue;
continue;
}
- k = test_matches(enumerator, device);
+ k = test_matches(enumerator, device, /* ignore_parent_match = */ false);
if (k <= 0) {
if (k < 0)
r = k;
/* Also include all potentially matching parent devices in the enumeration. These are things
* like root busses — e.g. /sys/devices/pci0000:00/ or /sys/devices/pnp0/, which ar not
* linked from /sys/class/ or /sys/bus/, hence pick them up explicitly here. */
- upwards = device;
- for (;;) {
- const char *ss, *usn;
-
- k = sd_device_get_parent(upwards, &upwards);
- if (k == -ENOENT) /* Reached the top? */
- break;
- if (k < 0) {
- r = k;
- break;
- }
-
- k = sd_device_get_subsystem(upwards, &ss);
- if (k == -ENOENT) /* Has no subsystem? */
- continue;
- if (k < 0) {
- r = k;
- break;
- }
-
- if (!match_subsystem(enumerator, ss))
- continue;
-
- k = sd_device_get_sysname(upwards, &usn);
- if (k < 0) {
- r = k;
- break;
- }
-
- if (!match_sysname(enumerator, usn))
- continue;
-
- k = test_matches(enumerator, upwards);
- if (k < 0)
- break;
- if (k == 0)
- continue;
-
- k = device_enumerator_add_device(enumerator, upwards);
- if (k < 0)
- r = k;
- else if (k == 0) /* Exists already? Then no need to go further up. */
- break;
- }
+ k = enumerator_add_parent_devices(enumerator, device, /* ignore_parent_match = */ false);
+ if (k < 0)
+ r = k;
}
return r;