diff options
author | David Herrmann <dh.herrmann@gmail.com> | 2014-09-11 13:25:21 +0200 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2014-09-13 08:46:04 -0400 |
commit | ffdc02f45bcc973249406b2c9bd996f8b27fa2a5 (patch) | |
tree | 5a61a2008512269266b5837b1ea228608a020aa4 /src/udev | |
parent | 0dc651610248c873e584cbe8afb9495241cdb622 (diff) |
udev: allow removing tags via TAG-="foobar"
This extends the udev parser to support OP_REMOVE (-=) and adds support
for TAG-= to remove previously set tags. We don't fail if the tag didn't
exist.
This is pretty handy if we ship default rules for seat-assignments and
users want to exclude specific devices from that. They can easily add
rules that drop any automatically added "seat" tags again.
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src/udev')
-rw-r--r-- | src/udev/udev-rules.c | 78 |
1 files changed, 75 insertions, 3 deletions
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index c8cb72d076..a5348b84c7 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -87,7 +87,7 @@ static unsigned int rules_add_string(struct udev_rules *rules, const char *s) { return strbuf_add_string(rules->strbuf, s, strlen(s)); } -/* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */ +/* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */ enum operation_type { OP_UNSET, @@ -96,6 +96,7 @@ enum operation_type { OP_MATCH_MAX, OP_ADD, + OP_REMOVE, OP_ASSIGN, OP_ASSIGN_FINAL, }; @@ -229,6 +230,7 @@ static const char *operation_str(enum operation_type type) { [OP_MATCH_MAX] = "MATCH_MAX", [OP_ADD] = "add", + [OP_REMOVE] = "remove", [OP_ASSIGN] = "assign", [OP_ASSIGN_FINAL] = "assign-final", } ; @@ -766,7 +768,7 @@ static int get_key(struct udev *udev, char **line, char **key, enum operation_ty break; if (linepos[0] == '=') break; - if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':')) + if ((linepos[0] == '+') || (linepos[0] == '-') || (linepos[0] == '!') || (linepos[0] == ':')) if (linepos[1] == '=') break; } @@ -790,6 +792,9 @@ static int get_key(struct udev *udev, char **line, char **key, enum operation_ty } else if (linepos[0] == '+' && linepos[1] == '=') { *op = OP_ADD; linepos += 2; + } else if (linepos[0] == '-' && linepos[1] == '=') { + *op = OP_REMOVE; + linepos += 2; } else if (linepos[0] == '=') { *op = OP_ASSIGN; linepos++; @@ -1126,6 +1131,10 @@ static int add_rule(struct udev_rules *rules, char *line, log_error("error parsing ATTR attribute"); goto invalid; } + if (op == OP_REMOVE) { + log_error("invalid ATTR operation"); + goto invalid; + } if (op < OP_MATCH_MAX) { rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr); } else { @@ -1140,6 +1149,10 @@ static int add_rule(struct udev_rules *rules, char *line, log_error("error parsing SECLABEL attribute"); goto invalid; } + if (op == OP_REMOVE) { + log_error("invalid SECLABEL operation"); + goto invalid; + } rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr); continue; @@ -1207,6 +1220,10 @@ static int add_rule(struct udev_rules *rules, char *line, log_error("error parsing ENV attribute"); goto invalid; } + if (op == OP_REMOVE) { + log_error("invalid ENV operation"); + goto invalid; + } if (op < OP_MATCH_MAX) { if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0) goto invalid; @@ -1247,6 +1264,10 @@ static int add_rule(struct udev_rules *rules, char *line, } if (streq(key, "PROGRAM")) { + if (op == OP_REMOVE) { + log_error("invalid PROGRAM operation"); + goto invalid; + } rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL); continue; } @@ -1266,6 +1287,10 @@ static int add_rule(struct udev_rules *rules, char *line, log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno); continue; } + if (op == OP_REMOVE) { + log_error("invalid IMPORT operation"); + goto invalid; + } if (streq(attr, "program")) { /* find known built-in command */ if (value[0] != '/') { @@ -1321,6 +1346,10 @@ static int add_rule(struct udev_rules *rules, char *line, attr = get_key_attribute(rules->udev, key + strlen("RUN")); if (attr == NULL) attr = "program"; + if (op == OP_REMOVE) { + log_error("invalid RUN operation"); + goto invalid; + } if (streq(attr, "builtin")) { enum udev_builtin_cmd cmd = udev_builtin_lookup(value); @@ -1341,21 +1370,37 @@ static int add_rule(struct udev_rules *rules, char *line, } if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) { + if (op == OP_REMOVE) { + log_error("invalid WAIT_FOR/WAIT_FOR_SYSFS operation"); + goto invalid; + } rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL); continue; } if (streq(key, "LABEL")) { + if (op == OP_REMOVE) { + log_error("invalid LABEL operation"); + goto invalid; + } rule_tmp.rule.rule.label_off = rules_add_string(rules, value); continue; } if (streq(key, "GOTO")) { + if (op == OP_REMOVE) { + log_error("invalid GOTO operation"); + goto invalid; + } rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL); continue; } if (startswith(key, "NAME")) { + if (op == OP_REMOVE) { + log_error("invalid NAME operation"); + goto invalid; + } if (op < OP_MATCH_MAX) { rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL); } else { @@ -1376,6 +1421,10 @@ static int add_rule(struct udev_rules *rules, char *line, } if (streq(key, "SYMLINK")) { + if (op == OP_REMOVE) { + log_error("invalid SYMLINK operation"); + goto invalid; + } if (op < OP_MATCH_MAX) rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL); else @@ -1388,6 +1437,11 @@ static int add_rule(struct udev_rules *rules, char *line, uid_t uid; char *endptr; + if (op == OP_REMOVE) { + log_error("invalid OWNER operation"); + goto invalid; + } + uid = strtoul(value, &endptr, 10); if (endptr[0] == '\0') { rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid); @@ -1405,6 +1459,11 @@ static int add_rule(struct udev_rules *rules, char *line, gid_t gid; char *endptr; + if (op == OP_REMOVE) { + log_error("invalid GROUP operation"); + goto invalid; + } + gid = strtoul(value, &endptr, 10); if (endptr[0] == '\0') { rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid); @@ -1422,6 +1481,11 @@ static int add_rule(struct udev_rules *rules, char *line, mode_t mode; char *endptr; + if (op == OP_REMOVE) { + log_error("invalid MODE operation"); + goto invalid; + } + mode = strtol(value, &endptr, 8); if (endptr[0] == '\0') rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode); @@ -1434,6 +1498,11 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "OPTIONS")) { const char *pos; + if (op == OP_REMOVE) { + log_error("invalid OPTIONS operation"); + goto invalid; + } + pos = strstr(value, "link_priority="); if (pos != NULL) { int prio = atoi(&pos[strlen("link_priority=")]); @@ -2389,7 +2458,10 @@ int udev_rules_apply_to_event(struct udev_rules *rules, log_error("ignoring invalid tag name '%s'", tag); break; } - udev_device_add_tag(event->dev, tag); + if (cur->key.op == OP_REMOVE) + udev_device_remove_tag(event->dev, tag); + else + udev_device_add_tag(event->dev, tag); break; } case TK_A_NAME: { |