1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-12 13:18:14 +03:00

udev-rules: replace udev_device by sd_device in udev_rules_apply_to_event()

This commit is contained in:
Yu Watanabe 2018-10-29 16:15:07 +09:00
parent dbea7f24d2
commit cf697ec00e

View File

@ -1727,18 +1727,23 @@ int udev_rules_apply_to_event(
usec_t timeout_usec, usec_t timeout_usec,
usec_t timeout_warn_usec, usec_t timeout_warn_usec,
Hashmap *properties_list) { Hashmap *properties_list) {
struct token *cur; sd_device *dev = event->dev->device;
struct token *rule;
enum escape_type esc = ESCAPE_UNSET; enum escape_type esc = ESCAPE_UNSET;
struct token *cur, *rule;
const char *action, *val;
bool can_set_name; bool can_set_name;
int r; int r;
if (!rules->tokens) if (!rules->tokens)
return 0; return 0;
can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) && r = sd_device_get_property_value(dev, "ACTION", &action);
(major(udev_device_get_devnum(event->dev)) > 0 || if (r < 0)
udev_device_get_ifindex(event->dev) > 0)); return r;
can_set_name = (!streq(action, "remove") &&
(sd_device_get_devnum(dev, NULL) >= 0 ||
sd_device_get_ifindex(dev, NULL) >= 0));
/* loop through token list, match, run actions or forward to next rule */ /* loop through token list, match, run actions or forward to next rule */
cur = &rules->tokens[0]; cur = &rules->tokens[0];
@ -1755,22 +1760,26 @@ int udev_rules_apply_to_event(
esc = ESCAPE_UNSET; esc = ESCAPE_UNSET;
break; break;
case TK_M_ACTION: case TK_M_ACTION:
if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0) if (match_key(rules, cur, action) != 0)
goto nomatch; goto nomatch;
break; break;
case TK_M_DEVPATH: case TK_M_DEVPATH:
if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0) if (sd_device_get_devpath(dev, &val) < 0)
goto nomatch;
if (match_key(rules, cur, val) != 0)
goto nomatch; goto nomatch;
break; break;
case TK_M_KERNEL: case TK_M_KERNEL:
if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0) if (sd_device_get_sysname(dev, &val) < 0)
goto nomatch;
if (match_key(rules, cur, val) != 0)
goto nomatch; goto nomatch;
break; break;
case TK_M_DEVLINK: { case TK_M_DEVLINK: {
const char *devlink; const char *devlink;
bool match = false; bool match = false;
FOREACH_DEVICE_DEVLINK(event->dev->device, devlink) FOREACH_DEVICE_DEVLINK(dev, devlink)
if (match_key(rules, cur, devlink + STRLEN("/dev/")) == 0) { if (match_key(rules, cur, devlink + STRLEN("/dev/")) == 0) {
match = true; match = true;
break; break;
@ -1786,15 +1795,16 @@ int udev_rules_apply_to_event(
break; break;
case TK_M_ENV: { case TK_M_ENV: {
const char *key_name = rules_str(rules, cur->key.attr_off); const char *key_name = rules_str(rules, cur->key.attr_off);
const char *value;
value = udev_device_get_property_value(event->dev, key_name); if (sd_device_get_property_value(dev, key_name, &val) < 0) {
/* check global properties */
if (properties_list)
val = hashmap_get(properties_list, key_name);
else
val = NULL;
}
/* check global properties */ if (match_key(rules, cur, strempty(val)))
if (!value && properties_list)
value = hashmap_get(properties_list, key_name);
if (match_key(rules, cur, strempty(value)))
goto nomatch; goto nomatch;
break; break;
} }
@ -1802,7 +1812,7 @@ int udev_rules_apply_to_event(
bool match = false; bool match = false;
const char *tag; const char *tag;
FOREACH_DEVICE_TAG(event->dev->device, tag) FOREACH_DEVICE_TAG(dev, tag)
if (streq(rules_str(rules, cur->key.value_off), tag)) { if (streq(rules_str(rules, cur->key.value_off), tag)) {
match = true; match = true;
break; break;
@ -1814,15 +1824,19 @@ int udev_rules_apply_to_event(
break; break;
} }
case TK_M_SUBSYSTEM: case TK_M_SUBSYSTEM:
if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0) if (sd_device_get_subsystem(dev, &val) < 0)
goto nomatch;
if (match_key(rules, cur, val) != 0)
goto nomatch; goto nomatch;
break; break;
case TK_M_DRIVER: case TK_M_DRIVER:
if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0) if (sd_device_get_driver(dev, &val) < 0)
goto nomatch;
if (match_key(rules, cur, val) != 0)
goto nomatch; goto nomatch;
break; break;
case TK_M_ATTR: case TK_M_ATTR:
if (match_attr(rules, event->dev->device, event, cur) != 0) if (match_attr(rules, dev, event, cur) != 0)
goto nomatch; goto nomatch;
break; break;
case TK_M_SYSCTL: { case TK_M_SYSCTL: {
@ -1855,10 +1869,9 @@ int udev_rules_apply_to_event(
next++; next++;
/* loop over parents */ /* loop over parents */
event->dev_parent = event->dev->device; event->dev_parent = dev;
for (;;) { for (;;) {
struct token *key; struct token *key;
const char *val;
/* loop over sequence of parent match keys */ /* loop over sequence of parent match keys */
for (key = cur; key < next; key++ ) { for (key = cur; key < next; key++ ) {
@ -1921,9 +1934,11 @@ int udev_rules_apply_to_event(
if (filename[0] != '/') { if (filename[0] != '/') {
char tmp[UTIL_PATH_SIZE]; char tmp[UTIL_PATH_SIZE];
if (sd_device_get_syspath(dev, &val) < 0)
goto nomatch;
strscpy(tmp, sizeof(tmp), filename); strscpy(tmp, sizeof(tmp), filename);
strscpyl(filename, sizeof(filename), strscpyl(filename, sizeof(filename), val, "/", tmp, NULL);
udev_device_get_syspath(event->dev), "/", tmp, NULL);
} }
} }
attr_subst_subdir(filename, sizeof(filename)); attr_subst_subdir(filename, sizeof(filename));
@ -1938,8 +1953,7 @@ int udev_rules_apply_to_event(
break; break;
} }
case TK_M_PROGRAM: { case TK_M_PROGRAM: {
char program[UTIL_PATH_SIZE]; char program[UTIL_PATH_SIZE], result[UTIL_LINE_SIZE];
char result[UTIL_LINE_SIZE];
event->program_result = mfree(event->program_result); event->program_result = mfree(event->program_result);
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program), false); udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program), false);
@ -1970,7 +1984,7 @@ int udev_rules_apply_to_event(
char import[UTIL_PATH_SIZE]; char import[UTIL_PATH_SIZE];
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import), false); udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import), false);
if (import_file_into_properties(event->dev->device, import) != 0) if (import_file_into_properties(dev, import) != 0)
if (cur->key.op != OP_NOMATCH) if (cur->key.op != OP_NOMATCH)
goto nomatch; goto nomatch;
break; break;
@ -2015,7 +2029,7 @@ int udev_rules_apply_to_event(
rules_str(rules, rule->rule.filename_off), rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line); rule->rule.filename_line);
r = udev_builtin_run(event->dev->device, cur->key.builtin_cmd, command, false); r = udev_builtin_run(dev, cur->key.builtin_cmd, command, false);
if (r < 0) { if (r < 0) {
/* remember failure */ /* remember failure */
log_debug_errno(r, "IMPORT builtin '%s' fails: %m", log_debug_errno(r, "IMPORT builtin '%s' fails: %m",
@ -2027,11 +2041,11 @@ int udev_rules_apply_to_event(
break; break;
} }
case TK_M_IMPORT_DB: { case TK_M_IMPORT_DB: {
const char *key = rules_str(rules, cur->key.value_off); const char *key;
const char *value;
if (sd_device_get_property_value(event->dev_db_clone, key, &value) >= 0) key = rules_str(rules, cur->key.value_off);
udev_device_add_property(event->dev, key, value); if (sd_device_get_property_value(event->dev_db_clone, key, &val) >= 0)
device_add_property(dev, key, val);
else if (cur->key.op != OP_NOMATCH) else if (cur->key.op != OP_NOMATCH)
goto nomatch; goto nomatch;
break; break;
@ -2042,7 +2056,6 @@ int udev_rules_apply_to_event(
const char *key; const char *key;
key = rules_str(rules, cur->key.value_off); key = rules_str(rules, cur->key.value_off);
r = proc_cmdline_get_key(key, PROC_CMDLINE_VALUE_OPTIONAL, &value); r = proc_cmdline_get_key(key, PROC_CMDLINE_VALUE_OPTIONAL, &value);
if (r < 0) if (r < 0)
log_debug_errno(r, "Failed to read %s from /proc/cmdline, ignoring: %m", key); log_debug_errno(r, "Failed to read %s from /proc/cmdline, ignoring: %m", key);
@ -2050,10 +2063,10 @@ int udev_rules_apply_to_event(
imported = true; imported = true;
if (value) if (value)
udev_device_add_property(event->dev, key, value); device_add_property(dev, key, value);
else else
/* we import simple flags as 'FLAG=1' */ /* we import simple flags as 'FLAG=1' */
udev_device_add_property(event->dev, key, "1"); device_add_property(dev, key, "1");
} }
if (!imported && cur->key.op != OP_NOMATCH) if (!imported && cur->key.op != OP_NOMATCH)
@ -2064,7 +2077,7 @@ int udev_rules_apply_to_event(
char import[UTIL_PATH_SIZE]; char import[UTIL_PATH_SIZE];
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import), false); udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import), false);
if (import_parent_into_properties(event->dev->device, import) != 0) if (import_parent_into_properties(dev, import) != 0)
if (cur->key.op != OP_NOMATCH) if (cur->key.op != OP_NOMATCH)
goto nomatch; goto nomatch;
break; break;
@ -2080,7 +2093,7 @@ int udev_rules_apply_to_event(
esc = ESCAPE_REPLACE; esc = ESCAPE_REPLACE;
break; break;
case TK_A_DB_PERSIST: case TK_A_DB_PERSIST:
udev_device_set_db_persist(event->dev); device_set_db_persist(dev);
break; break;
case TK_A_INOTIFY_WATCH: case TK_A_INOTIFY_WATCH:
if (event->inotify_watch_final) if (event->inotify_watch_final)
@ -2090,7 +2103,7 @@ int udev_rules_apply_to_event(
event->inotify_watch = cur->key.watch; event->inotify_watch = cur->key.watch;
break; break;
case TK_A_DEVLINK_PRIO: case TK_A_DEVLINK_PRIO:
udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio); device_set_devlink_priority(dev, cur->key.devlink_prio);
break; break;
case TK_A_OWNER: { case TK_A_OWNER: {
char owner[UTIL_NAME_SIZE]; char owner[UTIL_NAME_SIZE];
@ -2135,9 +2148,8 @@ int udev_rules_apply_to_event(
break; break;
} }
case TK_A_MODE: { case TK_A_MODE: {
char mode_str[UTIL_NAME_SIZE]; char mode_str[UTIL_NAME_SIZE], *endptr;
mode_t mode; mode_t mode;
char *endptr;
if (event->mode_final) if (event->mode_final)
break; break;
@ -2229,30 +2241,29 @@ int udev_rules_apply_to_event(
break; break;
} }
case TK_A_ENV: { case TK_A_ENV: {
const char *name = rules_str(rules, cur->key.attr_off);
char *value = rules_str(rules, cur->key.value_off);
char value_new[UTIL_NAME_SIZE]; char value_new[UTIL_NAME_SIZE];
const char *value_old = NULL; const char *name, *value_old;
if (value[0] == '\0') { name = rules_str(rules, cur->key.attr_off);
val = rules_str(rules, cur->key.value_off);
if (val[0] == '\0') {
if (cur->key.op == OP_ADD) if (cur->key.op == OP_ADD)
break; break;
udev_device_add_property(event->dev, name, NULL); device_add_property(dev, name, NULL);
break; break;
} }
if (cur->key.op == OP_ADD) if (cur->key.op == OP_ADD &&
value_old = udev_device_get_property_value(event->dev, name); sd_device_get_property_value(dev, name, &value_old) >= 0) {
if (value_old) {
char temp[UTIL_NAME_SIZE]; char temp[UTIL_NAME_SIZE];
/* append value separated by space */ /* append value separated by space */
udev_event_apply_format(event, value, temp, sizeof(temp), false); udev_event_apply_format(event, val, temp, sizeof(temp), false);
strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL); strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
} else } else
udev_event_apply_format(event, value, value_new, sizeof(value_new), false); udev_event_apply_format(event, val, value_new, sizeof(value_new), false);
udev_device_add_property(event->dev, name, value_new); device_add_property(dev, name, value_new);
break; break;
} }
case TK_A_TAG: { case TK_A_TAG: {
@ -2261,7 +2272,7 @@ int udev_rules_apply_to_event(
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag), false); udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag), false);
if (IN_SET(cur->key.op, OP_ASSIGN, OP_ASSIGN_FINAL)) if (IN_SET(cur->key.op, OP_ASSIGN, OP_ASSIGN_FINAL))
udev_device_cleanup_tags_list(event->dev); device_cleanup_tags(dev);
for (p = tag; *p != '\0'; p++) { for (p = tag; *p != '\0'; p++) {
if ((*p >= 'a' && *p <= 'z') || if ((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') || (*p >= 'A' && *p <= 'Z') ||
@ -2272,17 +2283,17 @@ int udev_rules_apply_to_event(
break; break;
} }
if (cur->key.op == OP_REMOVE) if (cur->key.op == OP_REMOVE)
udev_device_remove_tag(event->dev, tag); device_remove_tag(dev, tag);
else else
udev_device_add_tag(event->dev, tag); device_add_tag(dev, tag);
break; break;
} }
case TK_A_NAME: { case TK_A_NAME: {
const char *name = rules_str(rules, cur->key.value_off);
char name_str[UTIL_PATH_SIZE]; char name_str[UTIL_PATH_SIZE];
const char *name;
int count; int count;
name = rules_str(rules, cur->key.value_off);
if (event->name_final) if (event->name_final)
break; break;
if (cur->key.op == OP_ASSIGN_FINAL) if (cur->key.op == OP_ASSIGN_FINAL)
@ -2293,8 +2304,9 @@ int udev_rules_apply_to_event(
if (count > 0) if (count > 0)
log_debug("%i character(s) replaced", count); log_debug("%i character(s) replaced", count);
} }
if (major(udev_device_get_devnum(event->dev)) && if (sd_device_get_devnum(dev, NULL) >= 0 &&
!streq(name_str, udev_device_get_devnode(event->dev) + STRLEN("/dev/"))) { (sd_device_get_devname(dev, &val) < 0 ||
!streq(name_str, val + STRLEN("/dev/")))) {
log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n", log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
name, name,
rules_str(rules, rule->rule.filename_off), rules_str(rules, rule->rule.filename_off),
@ -2311,19 +2323,17 @@ int udev_rules_apply_to_event(
break; break;
} }
case TK_A_DEVLINK: { case TK_A_DEVLINK: {
char temp[UTIL_PATH_SIZE]; char temp[UTIL_PATH_SIZE], filename[UTIL_PATH_SIZE], *pos, *next;
char filename[UTIL_PATH_SIZE];
char *pos, *next;
int count = 0; int count = 0;
if (event->devlink_final) if (event->devlink_final)
break; break;
if (major(udev_device_get_devnum(event->dev)) == 0) if (sd_device_get_devnum(dev, NULL) < 0)
break; break;
if (cur->key.op == OP_ASSIGN_FINAL) if (cur->key.op == OP_ASSIGN_FINAL)
event->devlink_final = true; event->devlink_final = true;
if (IN_SET(cur->key.op, OP_ASSIGN, OP_ASSIGN_FINAL)) if (IN_SET(cur->key.op, OP_ASSIGN, OP_ASSIGN_FINAL))
udev_device_cleanup_devlinks_list(event->dev); device_cleanup_devlinks(dev);
/* allow multiple symlinks separated by spaces */ /* allow multiple symlinks separated by spaces */
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp), esc != ESCAPE_NONE); udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp), esc != ESCAPE_NONE);
@ -2337,12 +2347,12 @@ int udev_rules_apply_to_event(
while (isspace(pos[0])) while (isspace(pos[0]))
pos++; pos++;
next = strchr(pos, ' '); next = strchr(pos, ' ');
while (next != NULL) { while (next) {
next[0] = '\0'; next[0] = '\0';
log_debug("LINK '%s' %s:%u", pos, log_debug("LINK '%s' %s:%u", pos,
rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
strscpyl(filename, sizeof(filename), "/dev/", pos, NULL); strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
udev_device_add_devlink(event->dev, filename); device_add_devlink(dev, filename);
while (isspace(next[1])) while (isspace(next[1]))
next++; next++;
pos = &next[1]; pos = &next[1];
@ -2352,18 +2362,19 @@ int udev_rules_apply_to_event(
log_debug("LINK '%s' %s:%u", pos, log_debug("LINK '%s' %s:%u", pos,
rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
strscpyl(filename, sizeof(filename), "/dev/", pos, NULL); strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
udev_device_add_devlink(event->dev, filename); device_add_devlink(dev, filename);
} }
break; break;
} }
case TK_A_ATTR: { case TK_A_ATTR: {
const char *key_name = rules_str(rules, cur->key.attr_off); char attr[UTIL_PATH_SIZE], value[UTIL_NAME_SIZE];
char attr[UTIL_PATH_SIZE];
char value[UTIL_NAME_SIZE];
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
const char *key_name;
if (util_resolve_subsys_kernel(key_name, attr, sizeof(attr), 0) != 0) key_name = rules_str(rules, cur->key.attr_off);
strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL); if (util_resolve_subsys_kernel(key_name, attr, sizeof(attr), 0) != 0 &&
sd_device_get_syspath(dev, &val) >= 0)
strscpyl(attr, sizeof(attr), val, "/", key_name, NULL);
attr_subst_subdir(attr, sizeof(attr)); attr_subst_subdir(attr, sizeof(attr));
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value), false); udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value), false);
@ -2378,8 +2389,7 @@ int udev_rules_apply_to_event(
break; break;
} }
case TK_A_SYSCTL: { case TK_A_SYSCTL: {
char filename[UTIL_PATH_SIZE]; char filename[UTIL_PATH_SIZE], value[UTIL_NAME_SIZE];
char value[UTIL_NAME_SIZE];
udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename), false); udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename), false);
sysctl_normalize(filename); sysctl_normalize(filename);