diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-05-26 16:13:39 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-05-26 21:44:36 +0200 |
commit | 730b9c1e1469dfd6d6850e9ea06da3cac469eba0 (patch) | |
tree | 9c518023e82c1352cb51e367f744e61cb66b7a29 /src/udev/udevadm-trigger.c | |
parent | b485fd932ad37a945569857127b7e929e87e17b2 (diff) | |
download | systemd-730b9c1e1469dfd6d6850e9ea06da3cac469eba0.tar.gz |
udevadm: make use of the new uuid-enabled triggering for "udevadm trigger"
This adds two things:
- A new switch --uuid is added to "udevadm trigger". If specified a
random UUID is associated with the synthettic uevent and it is printed
to stdout. It may then be used manually to match up uevents as they
propagate through the system.
- The UUID logic is now implicitly enabled if "udevadm trigger --settle"
is used, in order to wait for precisely the uevents we actually
trigger. Fallback support is kept for pre-4.13 kernels (where the
requests for trigger uevents with uuids results in EINVAL).
Diffstat (limited to 'src/udev/udevadm-trigger.c')
-rw-r--r-- | src/udev/udevadm-trigger.c | 118 |
1 files changed, 95 insertions, 23 deletions
diff --git a/src/udev/udevadm-trigger.c b/src/udev/udevadm-trigger.c index ade92286d4..c75445959e 100644 --- a/src/udev/udevadm-trigger.c +++ b/src/udev/udevadm-trigger.c @@ -24,8 +24,14 @@ static bool arg_verbose = false; static bool arg_dry_run = false; static bool arg_quiet = false; +static bool arg_uuid = false; -static int exec_list(sd_device_enumerator *e, sd_device_action_t action, Set **settle_set) { +static int exec_list( + sd_device_enumerator *e, + sd_device_action_t action, + Hashmap *settle_hashmap) { + + bool skip_uuid_logic = false; const char *action_str; sd_device *d; int r, ret = 0; @@ -33,18 +39,33 @@ static int exec_list(sd_device_enumerator *e, sd_device_action_t action, Set **s action_str = device_action_to_string(action); FOREACH_DEVICE_AND_SUBSYSTEM(e, d) { + sd_id128_t id = SD_ID128_NULL; const char *syspath; - if (sd_device_get_syspath(d, &syspath) < 0) + r = sd_device_get_syspath(d, &syspath); + if (r < 0) { + log_debug_errno(r, "Failed to get syspath of enumerated devices, ignoring: %m"); continue; + } if (arg_verbose) - printf("%s\n", strna(syspath)); + printf("%s\n", syspath); if (arg_dry_run) continue; - r = sd_device_trigger(d, action); + /* Use the UUID mode if the user explicitly asked for it, or if --settle has been specified, + * so that we can recognize our own uevent. */ + r = sd_device_trigger_with_uuid(d, action, (arg_uuid || settle_hashmap) && !skip_uuid_logic ? &id : NULL); + if (r == -EINVAL && !arg_uuid && settle_hashmap && !skip_uuid_logic) { + /* If we specified a UUID because of the settling logic, and we got EINVAL this might + * be caused by an old kernel which doesn't know the UUID logic (pre-4.13). Let's try + * if it works without the UUID logic then. */ + r = sd_device_trigger(d, action); + if (r != -EINVAL) + skip_uuid_logic = true; /* dropping the uuid stuff changed the return code, + * hence don't bother next time */ + } if (r < 0) { /* ENOENT may be returned when a device does not have /uevent or is already * removed. Hence, this is logged at debug level and ignored. @@ -86,10 +107,28 @@ static int exec_list(sd_device_enumerator *e, sd_device_action_t action, Set **s continue; } - if (settle_set) { - r = set_put_strdup(settle_set, syspath); + /* If the user asked for it, write event UUID to stdout */ + if (arg_uuid) + printf(SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id)); + + if (settle_hashmap) { + _cleanup_free_ sd_id128_t *mid = NULL; + _cleanup_free_ char *sp = NULL; + + sp = strdup(syspath); + if (!sp) + return log_oom(); + + mid = newdup(sd_id128_t, &id, 1); + if (!d) + return log_oom(); + + r = hashmap_put(settle_hashmap, sp, mid); if (r < 0) return log_oom(); + + TAKE_PTR(sp); + TAKE_PTR(mid); } } @@ -97,24 +136,51 @@ static int exec_list(sd_device_enumerator *e, sd_device_action_t action, Set **s } static int device_monitor_handler(sd_device_monitor *m, sd_device *dev, void *userdata) { - _cleanup_free_ char *val = NULL; - Set *settle_set = userdata; + Hashmap *settle_hashmap = userdata; + sd_id128_t *settle_id; const char *syspath; + char *k; + int r; assert(dev); - assert(settle_set); + assert(settle_hashmap); - if (sd_device_get_syspath(dev, &syspath) < 0) + r = sd_device_get_syspath(dev, &syspath); + if (r < 0) { + log_debug_errno(r, "Failed to get syspath of device event, ignoring: %m"); return 0; + } + + settle_id = hashmap_get2(settle_hashmap, syspath, (void**) &k); + if (!settle_id) { + log_debug("Got uevent for unexpected device '%s', ignoring.", syspath); + return 0; + } + if (!sd_id128_is_null(*settle_id)) { /* If this is SD_ID128_NULL then we are on pre-4.13 and have no UUID to check, hence don't */ + sd_id128_t event_id; + + r = sd_device_get_trigger_uuid(dev, &event_id); + if (r < 0) { + log_debug_errno(r, "Got uevent without synthetic UUID for device '%s', ignoring: %m", syspath); + return 0; + } + + if (!sd_id128_equal(event_id, *settle_id)) { + log_debug("Got uevent not matching expected UUID for device '%s', ignoring.", syspath); + return 0; + } + } if (arg_verbose) printf("settle %s\n", syspath); - val = set_remove(settle_set, syspath); - if (!val) - log_debug("Got epoll event on syspath %s not present in syspath set", syspath); + if (arg_uuid) + printf("settle " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(*settle_id)); + + free(hashmap_remove(settle_hashmap, syspath)); + free(k); - if (set_isempty(settle_set)) + if (hashmap_isempty(settle_hashmap)) return sd_event_exit(sd_device_monitor_get_event(m), 0); return 0; @@ -162,7 +228,8 @@ static int help(void) { " -b --parent-match=NAME Trigger devices with that parent device\n" " -w --settle Wait for the triggered events to complete\n" " --wait-daemon[=SECONDS] Wait for udevd daemon to be initialized\n" - " before triggering uevents\n", + " before triggering uevents\n" + " --uuid Print synthetic uevent UUID\n", program_invocation_short_name); return 0; @@ -172,6 +239,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { enum { ARG_NAME = 0x100, ARG_PING, + ARG_UUID, }; static const struct option options[] = { @@ -193,6 +261,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { { "wait-daemon", optional_argument, NULL, ARG_PING }, { "version", no_argument, NULL, 'V' }, { "help", no_argument, NULL, 'h' }, + { "uuid", no_argument, NULL, ARG_UUID }, {} }; enum { @@ -203,7 +272,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL; _cleanup_(sd_event_unrefp) sd_event *event = NULL; - _cleanup_set_free_ Set *settle_set = NULL; + _cleanup_hashmap_free_ Hashmap *settle_hashmap = NULL; usec_t ping_timeout_usec = 5 * USEC_PER_SEC; bool settle = false, ping = false; int c, r; @@ -327,7 +396,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { break; } - case ARG_PING: { + case ARG_PING: ping = true; if (optarg) { r = parse_sec(optarg, &ping_timeout_usec); @@ -335,7 +404,10 @@ int trigger_main(int argc, char *argv[], void *userdata) { log_error_errno(r, "Failed to parse timeout value '%s', ignoring: %m", optarg); } break; - } + + case ARG_UUID: + arg_uuid = true; + break; case 'V': return print_version(); @@ -377,8 +449,8 @@ int trigger_main(int argc, char *argv[], void *userdata) { } if (settle) { - settle_set = set_new(&string_hash_ops_free); - if (!settle_set) + settle_hashmap = hashmap_new(&path_hash_ops_free_free); + if (!settle_hashmap) return log_oom(); r = sd_event_default(&event); @@ -393,7 +465,7 @@ int trigger_main(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to attach event to device monitor: %m"); - r = sd_device_monitor_start(m, device_monitor_handler, settle_set); + r = sd_device_monitor_start(m, device_monitor_handler, settle_hashmap); if (r < 0) return log_error_errno(r, "Failed to start device monitor: %m"); } @@ -413,11 +485,11 @@ int trigger_main(int argc, char *argv[], void *userdata) { assert_not_reached("Unknown device type"); } - r = exec_list(e, action, settle ? &settle_set : NULL); + r = exec_list(e, action, settle_hashmap); if (r < 0) return r; - if (event && !set_isempty(settle_set)) { + if (event && !hashmap_isempty(settle_hashmap)) { r = sd_event_loop(event); if (r < 0) return log_error_errno(r, "Event loop failed: %m"); |