summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2019-03-09 02:08:03 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2019-03-11 19:49:53 +0100
commitd4d690facd063321fc22dc25f2e57e08f10b2ad5 (patch)
tree85c82c905a5169252d35b06599b8411454a2b746 /src
parentutil: introduce device_for_action() (diff)
downloadsystemd-d4d690facd063321fc22dc25f2e57e08f10b2ad5.tar.xz
systemd-d4d690facd063321fc22dc25f2e57e08f10b2ad5.zip
udevd: use device_get_action() and device_get_seqnum()
Diffstat (limited to 'src')
-rw-r--r--src/udev/udev-event.c27
-rw-r--r--src/udev/udev-rules.c9
-rw-r--r--src/udev/udevd.c35
3 files changed, 29 insertions, 42 deletions
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..b27b212a99 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -1767,18 +1767,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 +1798,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/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);
- if (r < 0)
- return r;
-
- r = safe_atou64(val, &seqnum);
+ r = device_get_seqnum(dev, &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;
}