diff --git a/man/udevadm.xml b/man/udevadm.xml index b7a2494f83a..0107c575762 100644 --- a/man/udevadm.xml +++ b/man/udevadm.xml @@ -189,7 +189,7 @@ Request device events from the kernel. Primarily used to replay events at system coldplug time. - Takes a device specification as a positional argument. See the description of info + Takes device specifications as positional arguments. See the description of info above. @@ -298,7 +298,7 @@ Trigger events for devices with a matching device path. When this option is specified more than once, - the last NAME is used. + then each matching result is ORed, that is, all specified devices are triggered. @@ -306,7 +306,7 @@ Trigger events for all children of a given device. When this option is specified more than once, - the last NAME is used. + then each matching result is ORed, that is, all children of each specified device are triggered. @@ -332,8 +332,8 @@ - In addition, an optional positional argument can be used - to specify device name or sys path. It must start with + In addition, optional positional arguments can be used + to specify device names or sys paths. They must start with /dev or /sys respectively. diff --git a/src/libsystemd/sd-device/device-enumerator-private.h b/src/libsystemd/sd-device/device-enumerator-private.h index 87411bfdd80..cf2b2614824 100644 --- a/src/libsystemd/sd-device/device-enumerator-private.h +++ b/src/libsystemd/sd-device/device-enumerator-private.h @@ -7,6 +7,7 @@ int device_enumerator_scan_devices(sd_device_enumerator *enumeartor); int device_enumerator_scan_subsystems(sd_device_enumerator *enumeartor); int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *device); int device_enumerator_add_match_is_initialized(sd_device_enumerator *enumerator); +int device_enumerator_add_match_parent_incremental(sd_device_enumerator *enumerator, sd_device *parent); sd_device *device_enumerator_get_first(sd_device_enumerator *enumerator); sd_device *device_enumerator_get_next(sd_device_enumerator *enumerator); sd_device **device_enumerator_get_devices(sd_device_enumerator *enumerator, size_t *ret_n_devices); diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c index 20529aafd36..f643c6ea376 100644 --- a/src/libsystemd/sd-device/device-enumerator.c +++ b/src/libsystemd/sd-device/device-enumerator.c @@ -36,7 +36,7 @@ struct sd_device_enumerator { Hashmap *match_property; Set *match_sysname; Set *match_tag; - sd_device *match_parent; + Set *match_parent; bool match_allow_uninitialized; }; @@ -75,7 +75,7 @@ static sd_device_enumerator *device_enumerator_free(sd_device_enumerator *enumer hashmap_free_free_free(enumerator->match_property); set_free_free(enumerator->match_sysname); set_free_free(enumerator->match_tag); - sd_device_unref(enumerator->match_parent); + set_free_free(enumerator->match_parent); return mfree(enumerator); } @@ -217,18 +217,42 @@ _public_ int sd_device_enumerator_add_match_tag(sd_device_enumerator *enumerator return 0; } -_public_ int sd_device_enumerator_add_match_parent(sd_device_enumerator *enumerator, sd_device *parent) { +static void device_enumerator_clear_match_parent(sd_device_enumerator *enumerator) { + if (!enumerator) + return; + + set_clear_free(enumerator->match_parent); +} + +int device_enumerator_add_match_parent_incremental(sd_device_enumerator *enumerator, sd_device *parent) { + const char *path; + int r; + assert_return(enumerator, -EINVAL); assert_return(parent, -EINVAL); - sd_device_unref(enumerator->match_parent); - enumerator->match_parent = sd_device_ref(parent); + r = sd_device_get_syspath(parent, &path); + if (r < 0) + return r; + + r = set_ensure_allocated(&enumerator->match_parent, NULL); + if (r < 0) + return r; + + r = set_put_strdup(enumerator->match_parent, path); + if (r < 0) + return r; enumerator->scan_uptodate = false; return 0; } +_public_ int sd_device_enumerator_add_match_parent(sd_device_enumerator *enumerator, sd_device *parent) { + device_enumerator_clear_match_parent(enumerator); + return device_enumerator_add_match_parent_incremental(enumerator, parent); +} + _public_ int sd_device_enumerator_allow_uninitialized(sd_device_enumerator *enumerator) { assert_return(enumerator, -EINVAL); @@ -399,22 +423,24 @@ static bool match_tag(sd_device_enumerator *enumerator, sd_device *device) { } static bool match_parent(sd_device_enumerator *enumerator, sd_device *device) { - const char *devpath, *devpath_dev; + const char *syspath_parent, *syspath; + Iterator i; int r; assert(enumerator); assert(device); - if (!enumerator->match_parent) + if (set_isempty(enumerator->match_parent)) return true; - r = sd_device_get_devpath(enumerator->match_parent, &devpath); + r = sd_device_get_syspath(device, &syspath); assert(r >= 0); - r = sd_device_get_devpath(device, &devpath_dev); - assert(r >= 0); + SET_FOREACH(syspath_parent, enumerator->match_parent, i) + if (path_startswith(syspath, syspath_parent)) + return true; - return startswith(devpath_dev, devpath); + return false; } static bool match_sysname(sd_device_enumerator *enumerator, const char *sysname) { @@ -745,18 +771,17 @@ static int parent_crawl_children(sd_device_enumerator *enumerator, const char *p static int enumerator_scan_devices_children(sd_device_enumerator *enumerator) { const char *path; int r = 0, k; + Iterator i; - r = sd_device_get_syspath(enumerator->match_parent, &path); - if (r < 0) - return r; + SET_FOREACH(path, enumerator->match_parent, i) { + k = parent_add_child(enumerator, path); + if (k < 0) + r = k; - k = parent_add_child(enumerator, path); - if (k < 0) - r = k; - - k = parent_crawl_children(enumerator, path, DEVICE_ENUMERATE_MAX_DEPTH); - if (k < 0) - r = k; + k = parent_crawl_children(enumerator, path, DEVICE_ENUMERATE_MAX_DEPTH); + if (k < 0) + r = k; + } return r; } diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c index e54ee572c58..80d5bafdf7d 100644 --- a/src/libudev/libudev-enumerate.c +++ b/src/libudev/libudev-enumerate.c @@ -277,9 +277,6 @@ _public_ int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, * Return the devices on the subtree of one given device. The parent * itself is included in the list. * - * A reference for the device is held until the udev_enumerate context - * is cleaned up. - * * Returns: 0 on success, otherwise a negative error value. */ _public_ int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent) { diff --git a/src/udev/udevadm-trigger.c b/src/udev/udevadm-trigger.c index 95329469e38..2a658158e61 100644 --- a/src/udev/udevadm-trigger.c +++ b/src/udev/udevadm-trigger.c @@ -256,7 +256,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to open the device '%s': %m", optarg); - r = sd_device_enumerator_add_match_parent(e, dev); + r = device_enumerator_add_match_parent_incremental(e, dev); if (r < 0) return log_error_errno(r, "Failed to add parent match '%s': %m", optarg); break; @@ -272,7 +272,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to open the device '%s': %m", optarg); - r = sd_device_enumerator_add_match_parent(e, dev); + r = device_enumerator_add_match_parent_incremental(e, dev); if (r < 0) return log_error_errno(r, "Failed to add parent match '%s': %m", optarg); break; @@ -324,7 +324,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to open the device '%s': %m", argv[optind]); - r = sd_device_enumerator_add_match_parent(e, dev); + r = device_enumerator_add_match_parent_incremental(e, dev); if (r < 0) return log_error_errno(r, "Failed to add parent match '%s': %m", argv[optind]); }