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>
This commit is contained in:
David Herrmann 2014-09-11 13:25:21 +02:00 committed by Anthony G. Basile
parent 0dc6516102
commit ffdc02f45b
4 changed files with 103 additions and 4 deletions

View File

@ -115,6 +115,13 @@
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><literal>-=</literal></term>
<listitem>
<para>Remove the value from a key that holds a list of entries.</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><literal>:=</literal></term> <term><literal>:=</literal></term>
<listitem> <listitem>

View File

@ -1732,9 +1732,14 @@ void udev_device_set_is_initialized(struct udev_device *udev_device)
udev_device->is_initialized = true; udev_device->is_initialized = true;
} }
static bool is_valid_tag(const char *tag)
{
return !strchr(tag, ':') && !strchr(tag, ' ');
}
int udev_device_add_tag(struct udev_device *udev_device, const char *tag) int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
{ {
if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL) if (!is_valid_tag(tag))
return -EINVAL; return -EINVAL;
udev_device->tags_uptodate = false; udev_device->tags_uptodate = false;
if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL) if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
@ -1742,6 +1747,20 @@ int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
return -ENOMEM; return -ENOMEM;
} }
void udev_device_remove_tag(struct udev_device *udev_device, const char *tag)
{
struct udev_list_entry *e;
if (!is_valid_tag(tag))
return;
e = udev_list_get_entry(&udev_device->tags_list);
e = udev_list_entry_get_by_name(e, tag);
if (e) {
udev_device->tags_uptodate = false;
udev_list_entry_delete(e);
}
}
void udev_device_cleanup_tags_list(struct udev_device *udev_device) void udev_device_cleanup_tags_list(struct udev_device *udev_device)
{ {
udev_device->tags_uptodate = false; udev_device->tags_uptodate = false;

View File

@ -74,6 +74,7 @@ const char *udev_device_get_devpath_old(struct udev_device *udev_device);
const char *udev_device_get_id_filename(struct udev_device *udev_device); const char *udev_device_get_id_filename(struct udev_device *udev_device);
void udev_device_set_is_initialized(struct udev_device *udev_device); void udev_device_set_is_initialized(struct udev_device *udev_device);
int udev_device_add_tag(struct udev_device *udev_device, const char *tag); int udev_device_add_tag(struct udev_device *udev_device, const char *tag);
void udev_device_remove_tag(struct udev_device *udev_device, const char *tag);
void udev_device_cleanup_tags_list(struct udev_device *udev_device); void udev_device_cleanup_tags_list(struct udev_device *udev_device);
usec_t udev_device_get_usec_initialized(struct udev_device *udev_device); usec_t udev_device_get_usec_initialized(struct udev_device *udev_device);
void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t usec_initialized); void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t usec_initialized);

View File

@ -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)); return strbuf_add_string(rules->strbuf, s, strlen(s));
} }
/* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */ /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
enum operation_type { enum operation_type {
OP_UNSET, OP_UNSET,
@ -96,6 +96,7 @@ enum operation_type {
OP_MATCH_MAX, OP_MATCH_MAX,
OP_ADD, OP_ADD,
OP_REMOVE,
OP_ASSIGN, OP_ASSIGN,
OP_ASSIGN_FINAL, OP_ASSIGN_FINAL,
}; };
@ -229,6 +230,7 @@ static const char *operation_str(enum operation_type type) {
[OP_MATCH_MAX] = "MATCH_MAX", [OP_MATCH_MAX] = "MATCH_MAX",
[OP_ADD] = "add", [OP_ADD] = "add",
[OP_REMOVE] = "remove",
[OP_ASSIGN] = "assign", [OP_ASSIGN] = "assign",
[OP_ASSIGN_FINAL] = "assign-final", [OP_ASSIGN_FINAL] = "assign-final",
} ; } ;
@ -766,7 +768,7 @@ static int get_key(struct udev *udev, char **line, char **key, enum operation_ty
break; break;
if (linepos[0] == '=') if (linepos[0] == '=')
break; break;
if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':')) if ((linepos[0] == '+') || (linepos[0] == '-') || (linepos[0] == '!') || (linepos[0] == ':'))
if (linepos[1] == '=') if (linepos[1] == '=')
break; 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] == '=') { } else if (linepos[0] == '+' && linepos[1] == '=') {
*op = OP_ADD; *op = OP_ADD;
linepos += 2; linepos += 2;
} else if (linepos[0] == '-' && linepos[1] == '=') {
*op = OP_REMOVE;
linepos += 2;
} else if (linepos[0] == '=') { } else if (linepos[0] == '=') {
*op = OP_ASSIGN; *op = OP_ASSIGN;
linepos++; linepos++;
@ -1126,6 +1131,10 @@ static int add_rule(struct udev_rules *rules, char *line,
log_error("error parsing ATTR attribute"); log_error("error parsing ATTR attribute");
goto invalid; goto invalid;
} }
if (op == OP_REMOVE) {
log_error("invalid ATTR operation");
goto invalid;
}
if (op < OP_MATCH_MAX) { if (op < OP_MATCH_MAX) {
rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr); rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
} else { } else {
@ -1140,6 +1149,10 @@ static int add_rule(struct udev_rules *rules, char *line,
log_error("error parsing SECLABEL attribute"); log_error("error parsing SECLABEL attribute");
goto invalid; goto invalid;
} }
if (op == OP_REMOVE) {
log_error("invalid SECLABEL operation");
goto invalid;
}
rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr); rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
continue; continue;
@ -1207,6 +1220,10 @@ static int add_rule(struct udev_rules *rules, char *line,
log_error("error parsing ENV attribute"); log_error("error parsing ENV attribute");
goto invalid; goto invalid;
} }
if (op == OP_REMOVE) {
log_error("invalid ENV operation");
goto invalid;
}
if (op < OP_MATCH_MAX) { if (op < OP_MATCH_MAX) {
if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0) if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
goto invalid; goto invalid;
@ -1247,6 +1264,10 @@ static int add_rule(struct udev_rules *rules, char *line,
} }
if (streq(key, "PROGRAM")) { 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); rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
continue; 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); log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
continue; continue;
} }
if (op == OP_REMOVE) {
log_error("invalid IMPORT operation");
goto invalid;
}
if (streq(attr, "program")) { if (streq(attr, "program")) {
/* find known built-in command */ /* find known built-in command */
if (value[0] != '/') { 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")); attr = get_key_attribute(rules->udev, key + strlen("RUN"));
if (attr == NULL) if (attr == NULL)
attr = "program"; attr = "program";
if (op == OP_REMOVE) {
log_error("invalid RUN operation");
goto invalid;
}
if (streq(attr, "builtin")) { if (streq(attr, "builtin")) {
enum udev_builtin_cmd cmd = udev_builtin_lookup(value); 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 (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); rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
continue; continue;
} }
if (streq(key, "LABEL")) { 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); rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
continue; continue;
} }
if (streq(key, "GOTO")) { 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); rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
continue; continue;
} }
if (startswith(key, "NAME")) { if (startswith(key, "NAME")) {
if (op == OP_REMOVE) {
log_error("invalid NAME operation");
goto invalid;
}
if (op < OP_MATCH_MAX) { if (op < OP_MATCH_MAX) {
rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL); rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
} else { } else {
@ -1376,6 +1421,10 @@ static int add_rule(struct udev_rules *rules, char *line,
} }
if (streq(key, "SYMLINK")) { if (streq(key, "SYMLINK")) {
if (op == OP_REMOVE) {
log_error("invalid SYMLINK operation");
goto invalid;
}
if (op < OP_MATCH_MAX) if (op < OP_MATCH_MAX)
rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL); rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
else else
@ -1388,6 +1437,11 @@ static int add_rule(struct udev_rules *rules, char *line,
uid_t uid; uid_t uid;
char *endptr; char *endptr;
if (op == OP_REMOVE) {
log_error("invalid OWNER operation");
goto invalid;
}
uid = strtoul(value, &endptr, 10); uid = strtoul(value, &endptr, 10);
if (endptr[0] == '\0') { if (endptr[0] == '\0') {
rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid); 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; gid_t gid;
char *endptr; char *endptr;
if (op == OP_REMOVE) {
log_error("invalid GROUP operation");
goto invalid;
}
gid = strtoul(value, &endptr, 10); gid = strtoul(value, &endptr, 10);
if (endptr[0] == '\0') { if (endptr[0] == '\0') {
rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid); 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; mode_t mode;
char *endptr; char *endptr;
if (op == OP_REMOVE) {
log_error("invalid MODE operation");
goto invalid;
}
mode = strtol(value, &endptr, 8); mode = strtol(value, &endptr, 8);
if (endptr[0] == '\0') if (endptr[0] == '\0')
rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode); 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")) { if (streq(key, "OPTIONS")) {
const char *pos; const char *pos;
if (op == OP_REMOVE) {
log_error("invalid OPTIONS operation");
goto invalid;
}
pos = strstr(value, "link_priority="); pos = strstr(value, "link_priority=");
if (pos != NULL) { if (pos != NULL) {
int prio = atoi(&pos[strlen("link_priority=")]); int prio = atoi(&pos[strlen("link_priority=")]);
@ -2389,6 +2458,9 @@ int udev_rules_apply_to_event(struct udev_rules *rules,
log_error("ignoring invalid tag name '%s'", tag); log_error("ignoring invalid tag name '%s'", tag);
break; break;
} }
if (cur->key.op == OP_REMOVE)
udev_device_remove_tag(event->dev, tag);
else
udev_device_add_tag(event->dev, tag); udev_device_add_tag(event->dev, tag);
break; break;
} }