diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-10-25 04:14:11 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-10-27 00:25:27 +0200 |
commit | eb1f9e30ec603ab34631cc3adba1def49485b505 (patch) | |
tree | e766f2b09ad783c963d61f56fb325cd50d30f789 /src | |
parent | udev-event: move devnode handling logic to a new function (diff) | |
download | systemd-eb1f9e30ec603ab34631cc3adba1def49485b505.tar.xz systemd-eb1f9e30ec603ab34631cc3adba1def49485b505.zip |
udev-event: replace udev_device in udev_event_execute_rules() by sd_device
Also, this adds many logs.
Diffstat (limited to 'src')
-rw-r--r-- | src/udev/udev-event.c | 143 | ||||
-rw-r--r-- | src/udev/udev.h | 8 |
2 files changed, 106 insertions, 45 deletions
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c index 14d97f05e7..db2e3b733c 100644 --- a/src/udev/udev-event.c +++ b/src/udev/udev-event.c @@ -771,58 +771,119 @@ static int update_devnode(struct udev_event *event) { return udev_node_add(dev, apply, event->mode, event->uid, event->gid, event->seclabel_list); } -void udev_event_execute_rules(struct udev_event *event, - usec_t timeout_usec, usec_t timeout_warn_usec, - Hashmap *properties_list, - struct udev_rules *rules) { - struct udev_device *dev = event->dev; +static void event_execute_rules_on_remove( + struct udev_event *event, + usec_t timeout_usec, usec_t timeout_warn_usec, + Hashmap *properties_list, + struct udev_rules *rules) { - if (udev_device_get_subsystem(dev) == NULL) - return; + sd_device *dev = event->dev->device; + dev_t devnum; + int r; - if (streq(udev_device_get_action(dev), "remove")) { - udev_device_read_db(dev); - udev_device_tag_index(dev, NULL, false); - udev_device_delete_db(dev); + r = device_read_db_force(dev); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to read database under /run/udev/data/: %m"); - if (major(udev_device_get_devnum(dev)) != 0) - udev_watch_end(dev->device); + r = device_tag_index(dev, NULL, false); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m"); - udev_rules_apply_to_event(rules, event, - timeout_usec, timeout_warn_usec, - properties_list); + r = device_delete_db(dev); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to delete database under /run/udev/data/, ignoring: %m"); - if (major(udev_device_get_devnum(dev)) != 0) - udev_node_remove(dev->device); - } else { - event->dev_db = udev_device_clone_with_db(dev); - if (event->dev_db != NULL) { - /* disable watch during event processing */ - if (major(udev_device_get_devnum(dev)) != 0) - udev_watch_end(event->dev_db->device); - - if (major(udev_device_get_devnum(dev)) == 0 && - streq(udev_device_get_action(dev), "move")) - udev_device_copy_properties(dev, event->dev_db); - } + r = sd_device_get_devnum(dev, &devnum); + if (r < 0) { + if (r != -ENOENT) + log_device_debug_errno(dev, r, "Failed to get devnum, ignoring: %m"); + } else + (void) udev_watch_end(dev); + + (void) udev_rules_apply_to_event(rules, event, + timeout_usec, timeout_warn_usec, + properties_list); + + if (major(devnum) > 0) + (void) udev_node_remove(dev); +} + +int udev_event_execute_rules(struct udev_event *event, + usec_t timeout_usec, usec_t timeout_warn_usec, + Hashmap *properties_list, + struct udev_rules *rules) { + _cleanup_(sd_device_unrefp) sd_device *clone = NULL; + sd_device *dev = event->dev->device; + const char *subsystem, *action; + dev_t devnum; + int r; + + assert(event); + assert(rules); + + r = sd_device_get_subsystem(dev, &subsystem); + if (r < 0) + return log_device_error_errno(dev, r, "Failed to get subsystem: %m"); + + r = sd_device_get_property_value(dev, "ACTION", &action); + if (r < 0) + return log_device_error_errno(dev, r, "Failed to get property 'ACTION': %m"); - udev_rules_apply_to_event(rules, event, - timeout_usec, timeout_warn_usec, - properties_list); + if (streq(action, "remove")) { + event_execute_rules_on_remove(event, timeout_usec, timeout_warn_usec, properties_list, rules); + return 0; + } - (void) rename_netif(event); - (void) update_devnode(event); + r = device_clone_with_db(dev, &clone); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to clone sd_device object, ignoring: %m"); - /* preserve old, or get new initialization timestamp */ - udev_device_ensure_usec_initialized(event->dev, event->dev_db); + if (clone) { + event->dev_db = udev_device_new(NULL, clone); + if (!event->dev_db) + return -ENOMEM; - /* (re)write database file */ - udev_device_tag_index(dev, event->dev_db, true); - udev_device_update_db(dev); - udev_device_set_is_initialized(dev); + r = sd_device_get_devnum(dev, &devnum); + if (r < 0) { + if (r != -ENOENT) + log_device_debug_errno(dev, r, "Failed to get devnum, ignoring: %m"); - event->dev_db = udev_device_unref(event->dev_db); + if (streq(action, "move")) { + r = device_copy_properties(dev, clone); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to copy properties from cloned device, ignoring: %m"); + } + } else + /* Disable watch during event processing. */ + (void) udev_watch_end(clone); } + + (void) udev_rules_apply_to_event(rules, event, + timeout_usec, timeout_warn_usec, + properties_list); + + (void) rename_netif(event); + (void) update_devnode(event); + + /* preserve old, or get new initialization timestamp */ + r = device_ensure_usec_initialized(dev, clone); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to set initialization timestamp, ignoring: %m"); + + /* (re)write database file */ + r = device_tag_index(dev, clone, true); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to update tags under /run/udev/tag/, ignoring: %m"); + + r = device_update_db(dev); + if (r < 0) + log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/, ignoring: %m"); + + device_set_is_initialized(dev); + + event->dev_db = udev_device_unref(event->dev_db); + + return 0; } void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec) { diff --git a/src/udev/udev.h b/src/udev/udev.h index ec1662d700..add8b569b8 100644 --- a/src/udev/udev.h +++ b/src/udev/udev.h @@ -72,10 +72,10 @@ int udev_event_spawn(struct udev_event *event, usec_t timeout_warn_usec, bool accept_failure, const char *cmd, char *result, size_t ressize); -void udev_event_execute_rules(struct udev_event *event, - usec_t timeout_usec, usec_t timeout_warn_usec, - Hashmap *properties_list, - struct udev_rules *rules); +int udev_event_execute_rules(struct udev_event *event, + usec_t timeout_usec, usec_t timeout_warn_usec, + Hashmap *properties_list, + struct udev_rules *rules); void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec); /* Cleanup functions */ |