diff --git a/src/core/device.c b/src/core/device.c index a979caf21c..506bf74478 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -893,7 +893,8 @@ static void device_propagate_reload_by_sysfs(Manager *m, const char *sysfs) { static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata) { Manager *m = userdata; - const char *action, *sysfs; + DeviceAction action; + const char *sysfs; int r; assert(m); @@ -905,19 +906,19 @@ static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void * return 0; } - r = sd_device_get_property_value(dev, "ACTION", &action); + r = device_get_action(dev, &action); if (r < 0) { - log_device_error_errno(dev, r, "Failed to get udev action string: %m"); + log_device_error_errno(dev, r, "Failed to get udev action: %m"); return 0; } - if (streq(action, "change")) + if (action == DEVICE_ACTION_CHANGE) device_propagate_reload_by_sysfs(m, sysfs); /* A change event can signal that a device is becoming ready, in particular if * the device is using the SYSTEMD_READY logic in udev * so we need to reach the else block of the follwing if, even for change events */ - if (streq(action, "remove")) { + if (action == DEVICE_ACTION_REMOVE) { r = swap_process_device_remove(m, dev); if (r < 0) log_device_warning_errno(dev, r, "Failed to process swap device remove event, ignoring: %m"); diff --git a/src/libsystemd/sd-device/device-internal.h b/src/libsystemd/sd-device/device-internal.h index 4d03d09cd6..023fe0fcd7 100644 --- a/src/libsystemd/sd-device/device-internal.h +++ b/src/libsystemd/sd-device/device-internal.h @@ -3,6 +3,7 @@ #include "sd-device.h" +#include "device-private.h" #include "hashmap.h" #include "set.h" #include "time-util.h" @@ -64,6 +65,10 @@ struct sd_device { uid_t devuid; gid_t devgid; + /* only set when device is passed through netlink */ + DeviceAction action; + uint64_t seqnum; + bool parent_set:1; /* no need to try to reload parent */ bool sysattrs_read:1; /* don't try to re-read sysattrs once read */ bool property_tags_outdated:1; /* need to update TAGS= property */ diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index 0d0bdca800..6f17eeeee9 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -183,6 +183,72 @@ static int device_set_devgid(sd_device *device, const char *gid) { return 0; } +int device_get_action(sd_device *device, DeviceAction *action) { + assert(device); + + if (device->action < 0) + return -ENOENT; + + if (action) + *action = device->action; + + return 0; +} + +static int device_set_action(sd_device *device, const char *action) { + DeviceAction a; + int r; + + assert(device); + assert(action); + + a = device_action_from_string(action); + if (a < 0) + return -EINVAL; + + r = device_add_property_internal(device, "ACTION", action); + if (r < 0) + return r; + + device->action = a; + + return 0; +} + +int device_get_seqnum(sd_device *device, uint64_t *seqnum) { + assert(device); + + if (device->seqnum == 0) + return -ENOENT; + + if (seqnum) + *seqnum = device->seqnum; + + return 0; +} + +static int device_set_seqnum(sd_device *device, const char *str) { + uint64_t seqnum; + int r; + + assert(device); + assert(str); + + r = safe_atou64(str, &seqnum); + if (r < 0) + return r; + if (seqnum == 0) + return -EINVAL; + + r = device_add_property_internal(device, "SEQNUM", str); + if (r < 0) + return r; + + device->seqnum = seqnum; + + return 0; +} + static int device_amend(sd_device *device, const char *key, const char *value) { int r; @@ -241,6 +307,14 @@ static int device_amend(sd_device *device, const char *key, const char *value) { r = device_set_devgid(device, value); if (r < 0) return log_device_debug_errno(device, r, "sd-device: Failed to set devgid to '%s': %m", value); + } else if (streq(key, "ACTION")) { + r = device_set_action(device, value); + if (r < 0) + return log_device_debug_errno(device, r, "sd-device: Failed to set action to '%s': %m", value); + } else if (streq(key, "SEQNUM")) { + r = device_set_seqnum(device, value); + if (r < 0) + return log_device_debug_errno(device, r, "sd-device: Failed to set SEQNUM to '%s': %m", value); } else if (streq(key, "DEVLINKS")) { const char *word, *state; size_t l; @@ -278,23 +352,7 @@ static int device_amend(sd_device *device, const char *key, const char *value) { return 0; } -static const char* const device_action_table[_DEVICE_ACTION_MAX] = { - [DEVICE_ACTION_ADD] = "add", - [DEVICE_ACTION_REMOVE] = "remove", - [DEVICE_ACTION_CHANGE] = "change", - [DEVICE_ACTION_MOVE] = "move", - [DEVICE_ACTION_ONLINE] = "online", - [DEVICE_ACTION_OFFLINE] = "offline", - [DEVICE_ACTION_BIND] = "bind", - [DEVICE_ACTION_UNBIND] = "unbind", -}; - -DEFINE_STRING_TABLE_LOOKUP(device_action, DeviceAction); - -static int device_append(sd_device *device, char *key, const char **_major, const char **_minor, uint64_t *_seqnum, - DeviceAction *_action) { - DeviceAction action = _DEVICE_ACTION_INVALID; - uint64_t seqnum = 0; +static int device_append(sd_device *device, char *key, const char **_major, const char **_minor) { const char *major = NULL, *minor = NULL; char *value; int r; @@ -303,8 +361,6 @@ static int device_append(sd_device *device, char *key, const char **_major, cons assert(key); assert(_major); assert(_minor); - assert(_seqnum); - assert(_action); value = strchr(key, '='); if (!value) { @@ -321,19 +377,6 @@ static int device_append(sd_device *device, char *key, const char **_major, cons else if (streq(key, "MINOR")) minor = value; else { - if (streq(key, "ACTION")) { - action = device_action_from_string(value); - if (action == _DEVICE_ACTION_INVALID) - return -EINVAL; - } else if (streq(key, "SEQNUM")) { - r = safe_atou64(value, &seqnum); - if (r < 0) - return r; - else if (seqnum == 0) - /* kernel only sends seqnum > 0 */ - return -EINVAL; - } - r = device_amend(device, key, value); if (r < 0) return r; @@ -345,12 +388,6 @@ static int device_append(sd_device *device, char *key, const char **_major, cons if (minor != 0) *_minor = minor; - if (action != _DEVICE_ACTION_INVALID) - *_action = action; - - if (seqnum > 0) - *_seqnum = seqnum; - return 0; } @@ -360,10 +397,10 @@ void device_seal(sd_device *device) { device->sealed = true; } -static int device_verify(sd_device *device, DeviceAction action, uint64_t seqnum) { +static int device_verify(sd_device *device) { assert(device); - if (!device->devpath || !device->subsystem || action == _DEVICE_ACTION_INVALID || seqnum == 0) { + if (!device->devpath || !device->subsystem || device->action < 0 || device->seqnum == 0) { log_device_debug(device, "sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum."); return -EINVAL; } @@ -377,8 +414,6 @@ int device_new_from_strv(sd_device **ret, char **strv) { _cleanup_(sd_device_unrefp) sd_device *device = NULL; char **key; const char *major = NULL, *minor = NULL; - DeviceAction action = _DEVICE_ACTION_INVALID; - uint64_t seqnum = 0; int r; assert(ret); @@ -389,7 +424,7 @@ int device_new_from_strv(sd_device **ret, char **strv) { return r; STRV_FOREACH(key, strv) { - r = device_append(device, *key, &major, &minor, &seqnum, &action); + r = device_append(device, *key, &major, &minor); if (r < 0) return r; } @@ -400,7 +435,7 @@ int device_new_from_strv(sd_device **ret, char **strv) { return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor); } - r = device_verify(device, action, seqnum); + r = device_verify(device); if (r < 0) return r; @@ -412,8 +447,6 @@ int device_new_from_strv(sd_device **ret, char **strv) { int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) { _cleanup_(sd_device_unrefp) sd_device *device = NULL; const char *major = NULL, *minor = NULL; - DeviceAction action = _DEVICE_ACTION_INVALID; - uint64_t seqnum = 0; unsigned i = 0; int r; @@ -437,7 +470,7 @@ int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) { } i += end - key + 1; - r = device_append(device, key, &major, &minor, &seqnum, &action); + r = device_append(device, key, &major, &minor); if (r < 0) return r; } @@ -448,7 +481,7 @@ int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) { return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor); } - r = device_verify(device, action, seqnum); + r = device_verify(device); if (r < 0) return r; @@ -674,7 +707,7 @@ int device_new_from_synthetic_event(sd_device **new_device, const char *syspath, if (r < 0) return r; - r = device_add_property_internal(ret, "ACTION", action); + r = device_set_action(ret, action); if (r < 0) return r; @@ -953,3 +986,16 @@ int device_delete_db(sd_device *device) { return 0; } + +static const char* const device_action_table[_DEVICE_ACTION_MAX] = { + [DEVICE_ACTION_ADD] = "add", + [DEVICE_ACTION_REMOVE] = "remove", + [DEVICE_ACTION_CHANGE] = "change", + [DEVICE_ACTION_MOVE] = "move", + [DEVICE_ACTION_ONLINE] = "online", + [DEVICE_ACTION_OFFLINE] = "offline", + [DEVICE_ACTION_BIND] = "bind", + [DEVICE_ACTION_UNBIND] = "unbind", +}; + +DEFINE_STRING_TABLE_LOOKUP(device_action, DeviceAction); diff --git a/src/libsystemd/sd-device/device-private.h b/src/libsystemd/sd-device/device-private.h index 46e10105c9..9696f9c02d 100644 --- a/src/libsystemd/sd-device/device-private.h +++ b/src/libsystemd/sd-device/device-private.h @@ -10,6 +10,19 @@ #include "macro.h" +typedef enum DeviceAction { + DEVICE_ACTION_ADD, + DEVICE_ACTION_REMOVE, + DEVICE_ACTION_CHANGE, + DEVICE_ACTION_MOVE, + DEVICE_ACTION_ONLINE, + DEVICE_ACTION_OFFLINE, + DEVICE_ACTION_BIND, + DEVICE_ACTION_UNBIND, + _DEVICE_ACTION_MAX, + _DEVICE_ACTION_INVALID = -1, +} DeviceAction; + int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len); int device_new_from_strv(sd_device **ret, char **strv); int device_new_from_stat_rdev(sd_device **ret, const struct stat *st); @@ -21,6 +34,8 @@ int device_get_watch_handle(sd_device *device, int *handle); int device_get_devnode_mode(sd_device *device, mode_t *mode); int device_get_devnode_uid(sd_device *device, uid_t *uid); int device_get_devnode_gid(sd_device *device, gid_t *gid); +int device_get_action(sd_device *device, DeviceAction *action); +int device_get_seqnum(sd_device *device, uint64_t *seqnum); void device_seal(sd_device *device); void device_set_is_initialized(sd_device *device); @@ -58,18 +73,5 @@ static inline int device_read_db(sd_device *device) { return device_read_db_internal(device, false); } -typedef enum DeviceAction { - DEVICE_ACTION_ADD, - DEVICE_ACTION_REMOVE, - DEVICE_ACTION_CHANGE, - DEVICE_ACTION_MOVE, - DEVICE_ACTION_ONLINE, - DEVICE_ACTION_OFFLINE, - DEVICE_ACTION_BIND, - DEVICE_ACTION_UNBIND, - _DEVICE_ACTION_MAX, - _DEVICE_ACTION_INVALID = -1, -} DeviceAction; - DeviceAction device_action_from_string(const char *s) _pure_; const char *device_action_to_string(DeviceAction a) _const_; diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 584e7def8a..f455a4eb27 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -43,6 +43,7 @@ int device_new_aux(sd_device **ret) { .devmode = (mode_t) -1, .devuid = (uid_t) -1, .devgid = (gid_t) -1, + .action = _DEVICE_ACTION_INVALID, }; *ret = device; diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c index 1e7d774ba9..357adf6964 100644 --- a/src/libudev/libudev-device.c +++ b/src/libudev/libudev-device.c @@ -45,24 +45,15 @@ * * Returns: the kernel event sequence number, or 0 if there is no sequence number available. **/ -_public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device) { - const char *seqnum; - unsigned long long ret; - int r; +_public_ unsigned long long udev_device_get_seqnum(struct udev_device *udev_device) { + uint64_t seqnum; assert_return_errno(udev_device, 0, EINVAL); - r = sd_device_get_property_value(udev_device->device, "SEQNUM", &seqnum); - if (r == -ENOENT) + if (device_get_seqnum(udev_device->device, &seqnum) < 0) return 0; - else if (r < 0) - return_with_errno(0, r); - r = safe_atollu(seqnum, &ret); - if (r < 0) - return_with_errno(0, r); - - return ret; + return seqnum; } /** @@ -652,18 +643,14 @@ _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct ud * Returns: the kernel action value, or #NULL if there is no action value available. **/ _public_ const char *udev_device_get_action(struct udev_device *udev_device) { - const char *action; - int r; + DeviceAction action; assert_return_errno(udev_device, NULL, EINVAL); - r = sd_device_get_property_value(udev_device->device, "ACTION", &action); - if (r == -ENOENT) + if (device_get_action(udev_device->device, &action) < 0) return NULL; - if (r < 0) - return_with_errno(NULL, r); - return action; + return device_action_to_string(action); } /** diff --git a/src/login/logind-core.c b/src/login/logind-core.c index 60831bb1c3..f3d8de4f14 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -24,6 +24,7 @@ #include "process-util.h" #include "strv.h" #include "terminal-util.h" +#include "udev-util.h" #include "user-util.h" void manager_reset_config(Manager *m) { @@ -238,14 +239,12 @@ int manager_add_button(Manager *m, const char *name, Button **_button) { } int manager_process_seat_device(Manager *m, sd_device *d) { - const char *action; Device *device; int r; assert(m); - if (sd_device_get_property_value(d, "ACTION", &action) >= 0 && - streq(action, "remove")) { + if (device_for_action(d, DEVICE_ACTION_REMOVE)) { const char *syspath; r = sd_device_get_syspath(d, &syspath); @@ -305,7 +304,7 @@ int manager_process_seat_device(Manager *m, sd_device *d) { } int manager_process_button_device(Manager *m, sd_device *d) { - const char *action, *sysname; + const char *sysname; Button *b; int r; @@ -315,8 +314,7 @@ int manager_process_button_device(Manager *m, sd_device *d) { if (r < 0) return r; - if (sd_device_get_property_value(d, "ACTION", &action) >= 0 && - streq(action, "remove")) { + if (device_for_action(d, DEVICE_ACTION_REMOVE)) { b = hashmap_get(m->buttons, sysname); if (!b) diff --git a/src/login/logind.c b/src/login/logind.c index 95ec0a57c6..c78994aae6 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -26,6 +26,7 @@ #include "signal-util.h" #include "strv.h" #include "terminal-util.h" +#include "udev-util.h" static Manager* manager_unref(Manager *m); DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref); @@ -559,7 +560,7 @@ static int manager_dispatch_device_udev(sd_device_monitor *monitor, sd_device *d static int manager_dispatch_vcsa_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { Manager *m = userdata; - const char *name, *action; + const char *name; assert(m); assert(device); @@ -569,8 +570,7 @@ static int manager_dispatch_vcsa_udev(sd_device_monitor *monitor, sd_device *dev if (sd_device_get_sysname(device, &name) >= 0 && startswith(name, "vcsa") && - sd_device_get_property_value(device, "ACTION", &action) >= 0 && - streq(action, "remove")) + device_for_action(device, DEVICE_ACTION_REMOVE)) seat_preallocate_vts(m->seat0); return 0; diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index c87f907957..53d1cfb446 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -12,6 +12,7 @@ #include "bus-util.h" #include "conf-parser.h" #include "def.h" +#include "device-private.h" #include "device-util.h" #include "dns-domain.h" #include "fd-util.h" @@ -182,21 +183,21 @@ int manager_connect_bus(Manager *m) { static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) { Manager *m = userdata; - const char *action; + DeviceAction action; Link *link = NULL; int r, ifindex; assert(m); assert(device); - r = sd_device_get_property_value(device, "ACTION", &action); + r = device_get_action(device, &action); if (r < 0) { - log_device_debug_errno(device, r, "Failed to get 'ACTION' property, ignoring device: %m"); + log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m"); return 0; } - if (!STR_IN_SET(action, "add", "change", "move")) { - log_device_debug(device, "Ignoring udev %s event for device.", action); + if (!IN_SET(action, DEVICE_ACTION_ADD, DEVICE_ACTION_CHANGE, DEVICE_ACTION_MOVE)) { + log_device_debug(device, "Ignoring udev %s event for device.", device_action_to_string(action)); return 0; } @@ -208,7 +209,8 @@ static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *devi r = device_is_renaming(device); if (r < 0) { - log_device_error_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m", action); + log_device_error_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m", + device_action_to_string(action)); return 0; } if (r > 0) { diff --git a/src/shared/udev-util.c b/src/shared/udev-util.c index 6847d715f6..19d823c11d 100644 --- a/src/shared/udev-util.c +++ b/src/shared/udev-util.c @@ -181,3 +181,14 @@ int device_is_renaming(sd_device *dev) { return r >= 0; } + +bool device_for_action(sd_device *dev, DeviceAction action) { + DeviceAction a; + + assert(dev); + + if (device_get_action(dev, &a) < 0) + return false; + + return a == action; +} diff --git a/src/shared/udev-util.h b/src/shared/udev-util.h index c45d6a11fd..3c45447515 100644 --- a/src/shared/udev-util.h +++ b/src/shared/udev-util.h @@ -3,6 +3,7 @@ #include "sd-device.h" +#include "device-private.h" #include "time-util.h" typedef enum ResolveNameTiming { @@ -28,3 +29,4 @@ static inline int udev_parse_config(void) { int device_wait_for_initialization(sd_device *device, const char *subsystem, sd_device **ret); int device_is_renaming(sd_device *dev); +bool device_for_action(sd_device *dev, DeviceAction action); diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c index cefc2f4445..cab1b5ac0c 100644 --- a/src/udev/udev-event.c +++ b/src/udev/udev-event.c @@ -28,6 +28,7 @@ #include "strxcpyx.h" #include "udev-builtin.h" #include "udev-node.h" +#include "udev-util.h" #include "udev-watch.h" #include "udev.h" @@ -695,7 +696,7 @@ int udev_event_spawn(UdevEvent *event, static int rename_netif(UdevEvent *event) { sd_device *dev = event->dev; - const char *action, *oldname; + const char *oldname; int ifindex, r; if (!event->name) @@ -708,11 +709,7 @@ static int rename_netif(UdevEvent *event) { if (streq(event->name, oldname)) return 0; /* The interface name is already requested name. */ - 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"); - - if (!streq(action, "add")) + if (!device_for_action(dev, DEVICE_ACTION_ADD)) return 0; /* Rename the interface only when it is added. */ r = sd_device_get_ifindex(dev, &ifindex); @@ -742,7 +739,6 @@ static int rename_netif(UdevEvent *event) { static int update_devnode(UdevEvent *event) { sd_device *dev = event->dev; - const char *action; bool apply; int r; @@ -782,11 +778,7 @@ static int update_devnode(UdevEvent *event) { } } - 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"); - - apply = streq(action, "add") || event->owner_set || event->group_set || event->mode_set; + apply = device_for_action(dev, DEVICE_ACTION_ADD) || event->owner_set || event->group_set || event->mode_set; return udev_node_add(dev, apply, event->mode, event->uid, event->gid, event->seclabel_list); } @@ -843,7 +835,8 @@ int udev_event_execute_rules(UdevEvent *event, usec_t timeout_usec, Hashmap *properties_list, UdevRules *rules) { - const char *subsystem, *action; + const char *subsystem; + DeviceAction action; sd_device *dev; int r; @@ -856,11 +849,11 @@ int udev_event_execute_rules(UdevEvent *event, if (r < 0) return log_device_error_errno(dev, r, "Failed to get subsystem: %m"); - r = sd_device_get_property_value(dev, "ACTION", &action); + r = device_get_action(dev, &action); if (r < 0) - return log_device_error_errno(dev, r, "Failed to get property 'ACTION': %m"); + return log_device_error_errno(dev, r, "Failed to get ACTION: %m"); - if (streq(action, "remove")) { + if (action == DEVICE_ACTION_REMOVE) { event_execute_rules_on_remove(event, timeout_usec, properties_list, rules); return 0; } @@ -873,7 +866,7 @@ int udev_event_execute_rules(UdevEvent *event, /* Disable watch during event processing. */ (void) udev_watch_end(event->dev_db_clone); - if (streq(action, "move")) + if (action == DEVICE_ACTION_MOVE) (void) udev_event_on_move(event); (void) udev_rules_apply_to_event(rules, event, timeout_usec, properties_list); diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index d4aeca2fd3..835ce17165 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -1156,6 +1156,7 @@ static void add_rule(UdevRules *rules, char *line, else { if (STR_IN_SET(attr, "ACTION", + "SEQNUM", "SUBSYSTEM", "DEVTYPE", "MAJOR", @@ -1767,18 +1768,19 @@ int udev_rules_apply_to_event( sd_device *dev = event->dev; enum escape_type esc = ESCAPE_UNSET; struct token *cur, *rule; - const char *action, *val; + DeviceAction action; + const char *val; bool can_set_name; int r; if (!rules->tokens) return 0; - r = sd_device_get_property_value(dev, "ACTION", &action); + r = device_get_action(dev, &action); if (r < 0) return r; - can_set_name = (!streq(action, "remove") && + can_set_name = (action != DEVICE_ACTION_REMOVE && (sd_device_get_devnum(dev, NULL) >= 0 || sd_device_get_ifindex(dev, NULL) >= 0)); @@ -1797,7 +1799,7 @@ int udev_rules_apply_to_event( esc = ESCAPE_UNSET; break; case TK_M_ACTION: - if (!match_key(rules, cur, action)) + if (!match_key(rules, cur, device_action_to_string(action))) goto nomatch; break; case TK_M_DEVPATH: diff --git a/src/udev/udevadm-monitor.c b/src/udev/udevadm-monitor.c index 3dde3f338a..07caaa3794 100644 --- a/src/udev/udevadm-monitor.c +++ b/src/udev/udevadm-monitor.c @@ -9,6 +9,7 @@ #include "alloc-util.h" #include "device-monitor-private.h" +#include "device-private.h" #include "device-util.h" #include "fd-util.h" #include "format-util.h" @@ -26,14 +27,15 @@ static Set *arg_tag_filter = NULL; static Hashmap *arg_subsystem_filter = NULL; static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) { - const char *action = NULL, *devpath = NULL, *subsystem = NULL; + DeviceAction action = _DEVICE_ACTION_INVALID; + const char *devpath = NULL, *subsystem = NULL; MonitorNetlinkGroup group = PTR_TO_INT(userdata); struct timespec ts; assert(device); assert(IN_SET(group, MONITOR_GROUP_UDEV, MONITOR_GROUP_KERNEL)); - (void) sd_device_get_property_value(device, "ACTION", &action); + (void) device_get_action(device, &action); (void) sd_device_get_devpath(device, &devpath); (void) sd_device_get_subsystem(device, &subsystem); @@ -42,7 +44,8 @@ static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, printf("%-6s[%"PRI_TIME".%06"PRI_NSEC"] %-8s %s (%s)\n", group == MONITOR_GROUP_UDEV ? "UDEV" : "KERNEL", ts.tv_sec, (nsec_t)ts.tv_nsec/1000, - action, devpath, subsystem); + strna(device_action_to_string(action)), + devpath, subsystem); if (arg_show_property) { const char *key, *value; diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index 9c1784489a..da4c4cb87c 100644 --- a/src/udev/udevadm-test.c +++ b/src/udev/udevadm-test.c @@ -53,9 +53,17 @@ static int parse_argv(int argc, char *argv[]) { while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0) switch (c) { - case 'a': + case 'a': { + DeviceAction a; + + a = device_action_from_string(optarg); + if (a < 0) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Invalid action '%s'", optarg); + arg_action = optarg; break; + } case 'N': arg_resolve_name_timing = resolve_name_timing_from_string(optarg); if (arg_resolve_name_timing < 0) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 2e31994b58..0da82dca55 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -333,11 +333,7 @@ static int worker_lock_block_device(sd_device *dev, int *ret_fd) { * udev has finished its event handling. */ - r = sd_device_get_property_value(dev, "ACTION", &val); - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get the value of property 'ACTION': %m"); - - if (streq(val, "remove")) + if (device_for_action(dev, DEVICE_ACTION_REMOVE)) return 0; r = sd_device_get_subsystem(dev, &val); @@ -385,21 +381,23 @@ static int worker_lock_block_device(sd_device *dev, int *ret_fd) { static int worker_process_device(Manager *manager, sd_device *dev) { _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL; _cleanup_close_ int fd_lock = -1; - const char *seqnum, *action; + DeviceAction action; + uint64_t seqnum; int r; assert(manager); assert(dev); - r = sd_device_get_property_value(dev, "SEQNUM", &seqnum); + r = device_get_seqnum(dev, &seqnum); if (r < 0) return log_device_debug_errno(dev, r, "Failed to get SEQNUM: %m"); - r = sd_device_get_property_value(dev, "ACTION", &action); + r = device_get_action(dev, &action); if (r < 0) return log_device_debug_errno(dev, r, "Failed to get ACTION: %m"); - log_device_debug(dev, "Processing device (SEQNUM=%s, ACTION=%s)", seqnum, action); + log_device_debug(dev, "Processing device (SEQNUM=%"PRIu64", ACTION=%s)", + seqnum, device_action_to_string(action)); udev_event = udev_event_new(dev, arg_exec_delay_usec, manager->rtnl); if (!udev_event) @@ -425,7 +423,8 @@ static int worker_process_device(Manager *manager, sd_device *dev) { return log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/: %m"); } - log_device_debug(dev, "Device (SEQNUM=%s, ACTION=%s) processed", seqnum, action); + log_device_debug(dev, "Device (SEQNUM=%"PRIu64", ACTION=%s) processed", + seqnum, device_action_to_string(action)); return 0; } @@ -581,8 +580,8 @@ static void event_run(Manager *manager, struct event *event) { static int event_queue_insert(Manager *manager, sd_device *dev) { _cleanup_(sd_device_unrefp) sd_device *clone = NULL; - const char *val, *action; struct event *event; + DeviceAction action; uint64_t seqnum; int r; @@ -596,19 +595,12 @@ static int event_queue_insert(Manager *manager, sd_device *dev) { assert(manager->pid == getpid_cached()); /* We only accepts devices received by device monitor. */ - r = sd_device_get_property_value(dev, "SEQNUM", &val); + r = device_get_seqnum(dev, &seqnum); if (r < 0) return r; - r = safe_atou64(val, &seqnum); - if (r < 0) - return r; - - if (seqnum == 0) - return -EINVAL; - /* Refuse devices do not have ACTION property. */ - r = sd_device_get_property_value(dev, "ACTION", &action); + r = device_get_action(dev, &action); if (r < 0) return r; @@ -641,7 +633,8 @@ static int event_queue_insert(Manager *manager, sd_device *dev) { LIST_APPEND(event, manager->events, event); - log_device_debug(dev, "Device (SEQNUM=%"PRIu64", ACTION=%s) is queued", seqnum, action); + log_device_debug(dev, "Device (SEQNUM=%"PRIu64", ACTION=%s) is queued", + seqnum, device_action_to_string(action)); return 0; } diff --git a/test/TEST-29-UDEV-ID_RENAMING/testsuite.sh b/test/TEST-29-UDEV-ID_RENAMING/testsuite.sh index e5c2b5c7ab..3d6f8a0bbe 100755 --- a/test/TEST-29-UDEV-ID_RENAMING/testsuite.sh +++ b/test/TEST-29-UDEV-ID_RENAMING/testsuite.sh @@ -21,19 +21,19 @@ EOF udevadm control --log-priority=debug --reload udevadm trigger --action=add --settle /sys/devices/virtual/net/lo udevadm info /sys/devices/virtual/net/lo - +sleep 1 STATE=$(systemctl show --property=ActiveState --value sys-devices-virtual-net-lo.device) [[ $STATE == "active" ]] || exit 1 udevadm trigger --action=change --settle /sys/devices/virtual/net/lo udevadm info /sys/devices/virtual/net/lo - +sleep 1 STATE=$(systemctl show --property=ActiveState --value sys-devices-virtual-net-lo.device) [[ $STATE == "inactive" ]] || exit 1 udevadm trigger --action=move --settle /sys/devices/virtual/net/lo udevadm info /sys/devices/virtual/net/lo - +sleep 1 STATE=$(systemctl show --property=ActiveState --value sys-devices-virtual-net-lo.device) [[ $STATE == "active" ]] || exit 1