X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fudev%2Fudev-rules.c;h=447a86864ea39688a75f9037041fd40a70334915;hp=16348126ee6e88140cec2b48709c8614954cbfa6;hb=9f5ecdb0b11557be41c065f460bb22ab52bb0034;hpb=1291bc895b3dc52fbf7f4c3a04507c14428c4152 diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 16348126e..447a86864 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -46,10 +46,15 @@ struct uid_gid { }; }; +static const char* const rules_dirs[] = { + "/etc/udev/rules.d", + "/run/udev/rules.d", + UDEVLIBEXECDIR "/rules.d", + NULL}; + struct udev_rules { struct udev *udev; - char **dirs; - usec_t *dirs_ts_usec; + usec_t dirs_ts_usec; int resolve_names; /* every key in the rules file becomes a token */ @@ -77,7 +82,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, @@ -86,6 +91,7 @@ enum operation_type { OP_MATCH_MAX, OP_ADD, + OP_REMOVE, OP_ASSIGN, OP_ASSIGN_FINAL, }; @@ -132,7 +138,6 @@ enum token_type { TK_M_PARENTS_MAX, TK_M_TEST, /* val, mode_t */ - TK_M_EVENT_TIMEOUT, /* int */ TK_M_PROGRAM, /* val */ TK_M_IMPORT_FILE, /* val */ TK_M_IMPORT_PROG, /* val */ @@ -156,6 +161,7 @@ enum token_type { TK_A_MODE_ID, /* mode_t */ TK_A_TAG, /* val */ TK_A_STATIC_NODE, /* val */ + TK_A_SECLABEL, /* val, attr */ TK_A_ENV, /* val, attr */ TK_A_NAME, /* val */ TK_A_DEVLINK, /* val */ @@ -195,7 +201,6 @@ struct token { uid_t uid; gid_t gid; int devlink_prio; - int event_timeout; int watch; enum udev_builtin_cmd builtin_cmd; }; @@ -212,8 +217,7 @@ struct rule_tmp { }; #ifdef DEBUG -static const char *operation_str(enum operation_type type) -{ +static const char *operation_str(enum operation_type type) { static const char *operation_strs[] = { [OP_UNSET] = "UNSET", [OP_MATCH] = "match", @@ -221,6 +225,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", } ; @@ -228,8 +233,7 @@ static const char *operation_str(enum operation_type type) return operation_strs[type]; } -static const char *string_glob_str(enum string_glob_type type) -{ +static const char *string_glob_str(enum string_glob_type type) { static const char *string_glob_strs[] = { [GL_UNSET] = "UNSET", [GL_PLAIN] = "plain", @@ -242,8 +246,7 @@ static const char *string_glob_str(enum string_glob_type type) return string_glob_strs[type]; } -static const char *token_str(enum token_type type) -{ +static const char *token_str(enum token_type type) { static const char *token_strs[] = { [TK_UNSET] = "UNSET", [TK_RULE] = "RULE", @@ -269,7 +272,6 @@ static const char *token_str(enum token_type type) [TK_M_PARENTS_MAX] = "M PARENTS_MAX", [TK_M_TEST] = "M TEST", - [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT", [TK_M_PROGRAM] = "M PROGRAM", [TK_M_IMPORT_FILE] = "M IMPORT_FILE", [TK_M_IMPORT_PROG] = "M IMPORT_PROG", @@ -291,6 +293,7 @@ static const char *token_str(enum token_type type) [TK_A_OWNER_ID] = "A OWNER_ID", [TK_A_GROUP_ID] = "A GROUP_ID", [TK_A_STATIC_NODE] = "A STATIC_NODE", + [TK_A_SECLABEL] = "A SECLABEL", [TK_A_MODE_ID] = "A MODE_ID", [TK_A_ENV] = "A ENV", [TK_A_TAG] = "A ENV", @@ -307,8 +310,7 @@ static const char *token_str(enum token_type type) return token_strs[type]; } -static void dump_token(struct udev_rules *rules, struct token *token) -{ +static void dump_token(struct udev_rules *rules, struct token *token) { enum token_type type = token->type; enum operation_type op = token->key.op; enum string_glob_type glob = token->key.glob; @@ -322,7 +324,7 @@ static void dump_token(struct udev_rules *rules, struct token *token) const char *tk_ptr = (char *)token; unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token); - log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n", + log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'", &rules->buf[token->rule.filename_off], token->rule.filename_line, idx, token->rule.token_count, &rules->buf[token->rule.label_off]); @@ -354,90 +356,86 @@ static void dump_token(struct udev_rules *rules, struct token *token) case TK_A_MODE: case TK_A_RUN_BUILTIN: case TK_A_RUN_PROGRAM: - log_debug("%s %s '%s'(%s)\n", + log_debug("%s %s '%s'(%s)", token_str(type), operation_str(op), value, string_glob_str(glob)); break; case TK_M_IMPORT_BUILTIN: - log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value); + log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value); break; case TK_M_ATTR: case TK_M_ATTRS: case TK_M_ENV: case TK_A_ATTR: case TK_A_ENV: - log_debug("%s %s '%s' '%s'(%s)\n", + log_debug("%s %s '%s' '%s'(%s)", token_str(type), operation_str(op), attr, value, string_glob_str(glob)); break; case TK_M_TAG: case TK_A_TAG: - log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value); + log_debug("%s %s '%s'", token_str(type), operation_str(op), value); break; case TK_A_STRING_ESCAPE_NONE: case TK_A_STRING_ESCAPE_REPLACE: case TK_A_DB_PERSIST: - log_debug("%s\n", token_str(type)); + log_debug("%s", token_str(type)); break; case TK_M_TEST: - log_debug("%s %s '%s'(%s) %#o\n", + log_debug("%s %s '%s'(%s) %#o", token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode); break; case TK_A_INOTIFY_WATCH: - log_debug("%s %u\n", token_str(type), token->key.watch); + log_debug("%s %u", token_str(type), token->key.watch); break; case TK_A_DEVLINK_PRIO: - log_debug("%s %u\n", token_str(type), token->key.devlink_prio); + log_debug("%s %u", token_str(type), token->key.devlink_prio); break; case TK_A_OWNER_ID: - log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid); + log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid); break; case TK_A_GROUP_ID: - log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid); + log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid); break; case TK_A_MODE_ID: - log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode); + log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode); break; case TK_A_STATIC_NODE: - log_debug("%s '%s'\n", token_str(type), value); + log_debug("%s '%s'", token_str(type), value); break; - case TK_M_EVENT_TIMEOUT: - log_debug("%s %u\n", token_str(type), token->key.event_timeout); + case TK_A_SECLABEL: + log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value); break; case TK_A_GOTO: - log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto); + log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto); break; case TK_END: - log_debug("* %s\n", token_str(type)); + log_debug("* %s", token_str(type)); break; case TK_M_PARENTS_MIN: case TK_M_PARENTS_MAX: case TK_M_MAX: case TK_UNSET: - log_debug("unknown type %u\n", type); + log_debug("unknown type %u", type); break; } } -static void dump_rules(struct udev_rules *rules) -{ +static void dump_rules(struct udev_rules *rules) { unsigned int i; - log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n", + log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings", rules->token_cur, rules->token_cur * sizeof(struct token), rules->buf_count, rules->buf_cur); - for(i = 0; i < rules->token_cur; i++) + for (i = 0; i < rules->token_cur; i++) dump_token(rules, &rules->tokens[i]); } #else -static inline const char *operation_str(enum operation_type type) { return NULL; } -static inline const char *token_str(enum token_type type) { return NULL; } static inline void dump_token(struct udev_rules *rules, struct token *token) {} static inline void dump_rules(struct udev_rules *rules) {} #endif /* DEBUG */ -static int add_token(struct udev_rules *rules, struct token *token) -{ +static int add_token(struct udev_rules *rules, struct token *token) { /* grow buffer if needed */ if (rules->token_cur+1 >= rules->token_max) { struct token *tokens; @@ -459,11 +457,11 @@ static int add_token(struct udev_rules *rules, struct token *token) return 0; } -static uid_t add_uid(struct udev_rules *rules, const char *owner) -{ +static uid_t add_uid(struct udev_rules *rules, const char *owner) { unsigned int i; - uid_t uid; + uid_t uid = 0; unsigned int off; + int r; /* lookup, if we know it already */ for (i = 0; i < rules->uids_cur; i++) { @@ -473,7 +471,13 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner) return uid; } } - uid = util_lookup_user(rules->udev, owner); + r = get_user_creds(&owner, &uid, NULL, NULL, NULL); + if (r < 0) { + if (r == -ENOENT || r == -ESRCH) + log_error("specified user '%s' unknown", owner); + else + log_error_errno(r, "error resolving user '%s': %m", owner); + } /* grow buffer if needed */ if (rules->uids_cur+1 >= rules->uids_max) { @@ -500,11 +504,11 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner) return uid; } -static gid_t add_gid(struct udev_rules *rules, const char *group) -{ +static gid_t add_gid(struct udev_rules *rules, const char *group) { unsigned int i; - gid_t gid; + gid_t gid = 0; unsigned int off; + int r; /* lookup, if we know it already */ for (i = 0; i < rules->gids_cur; i++) { @@ -514,7 +518,13 @@ static gid_t add_gid(struct udev_rules *rules, const char *group) return gid; } } - gid = util_lookup_group(rules->udev, group); + r = get_group_creds(&group, &gid); + if (r < 0) { + if (r == -ENOENT || r == -ESRCH) + log_error("specified group '%s' unknown", group); + else + log_error_errno(r, "error resolving group '%s': %m", group); + } /* grow buffer if needed */ if (rules->gids_cur+1 >= rules->gids_max) { @@ -541,11 +551,11 @@ static gid_t add_gid(struct udev_rules *rules, const char *group) return gid; } -static int import_property_from_string(struct udev_device *dev, char *line) -{ +static int import_property_from_string(struct udev_device *dev, char *line) { char *key; char *val; size_t len; + struct udev_list_entry *entry; /* find key */ key = line; @@ -589,34 +599,22 @@ static int import_property_from_string(struct udev_device *dev, char *line) /* unquote */ if (val[0] == '"' || val[0] == '\'') { if (val[len-1] != val[0]) { - log_debug("inconsistent quoting: '%s', skip\n", line); + log_debug("inconsistent quoting: '%s', skip", line); return -1; } val[len-1] = '\0'; val++; } - /* handle device, renamed by external tool, returning new path */ - if (streq(key, "DEVPATH")) { - char syspath[UTIL_PATH_SIZE]; - - log_debug("updating devpath from '%s' to '%s'\n", - udev_device_get_devpath(dev), val); - strscpyl(syspath, sizeof(syspath), "/sys", val, NULL); - udev_device_set_syspath(dev, syspath); - } else { - struct udev_list_entry *entry; - - entry = udev_device_add_property(dev, key, val); - /* store in db, skip private keys */ - if (key[0] != '.') - udev_list_entry_set_num(entry, true); - } + entry = udev_device_add_property(dev, key, val); + /* store in db, skip private keys */ + if (key[0] != '.') + udev_list_entry_set_num(entry, true); + return 0; } -static int import_file_into_properties(struct udev_device *dev, const char *filename) -{ +static int import_file_into_properties(struct udev_device *dev, const char *filename) { FILE *f; char line[UTIL_LINE_SIZE]; @@ -629,8 +627,10 @@ static int import_file_into_properties(struct udev_device *dev, const char *file return 0; } -static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask) -{ +static int import_program_into_properties(struct udev_event *event, + usec_t timeout_usec, + usec_t timeout_warn_usec, + const char *program, const sigset_t *sigmask) { struct udev_device *dev = event->dev; char **envp; char result[UTIL_LINE_SIZE]; @@ -638,7 +638,7 @@ static int import_program_into_properties(struct udev_event *event, const char * int err; envp = udev_device_get_properties_envp(dev); - err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)); + err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, program, envp, sigmask, result, sizeof(result)); if (err < 0) return err; @@ -657,8 +657,7 @@ static int import_program_into_properties(struct udev_event *event, const char * return 0; } -static int import_parent_into_properties(struct udev_device *dev, const char *filter) -{ +static int import_parent_into_properties(struct udev_device *dev, const char *filter) { struct udev_device *dev_parent; struct udev_list_entry *list_entry; @@ -683,8 +682,7 @@ static int import_parent_into_properties(struct udev_device *dev, const char *fi } #define WAIT_LOOP_PER_SECOND 50 -static int wait_for_file(struct udev_device *dev, const char *file, int timeout) -{ +static int wait_for_file(struct udev_device *dev, const char *file, int timeout) { char filepath[UTIL_PATH_SIZE]; char devicepath[UTIL_PATH_SIZE]; struct stat stats; @@ -703,23 +701,22 @@ static int wait_for_file(struct udev_device *dev, const char *file, int timeout) /* lookup file */ if (stat(file, &stats) == 0) { - log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1); + log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1); return 0; } /* make sure, the device did not disappear in the meantime */ if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) { - log_debug("device disappeared while waiting for '%s'\n", file); + log_debug("device disappeared while waiting for '%s'", file); return -2; } - log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND); + log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND); nanosleep(&duration, NULL); } - log_debug("waiting for '%s' failed\n", file); + log_debug("waiting for '%s' failed", file); return -1; } -static int attr_subst_subdir(char *attr, size_t len) -{ +static int attr_subst_subdir(char *attr, size_t len) { bool found = false; if (strstr(attr, "/*/")) { @@ -756,8 +753,7 @@ static int attr_subst_subdir(char *attr, size_t len) return found; } -static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) -{ +static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) { char *linepos; char *temp; @@ -782,7 +778,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; } @@ -806,6 +802,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++; @@ -844,8 +843,7 @@ static int get_key(struct udev *udev, char **line, char **key, enum operation_ty } /* extract possible KEY{attr} */ -static const char *get_key_attribute(struct udev *udev, char *str) -{ +static const char *get_key_attribute(struct udev *udev, char *str) { char *pos; char *attr; @@ -854,7 +852,7 @@ static const char *get_key_attribute(struct udev *udev, char *str) attr++; pos = strchr(attr, '}'); if (pos == NULL) { - log_error("missing closing brace for format\n"); + log_error("missing closing brace for format"); return NULL; } pos[0] = '\0'; @@ -865,12 +863,11 @@ static const char *get_key_attribute(struct udev *udev, char *str) static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, enum operation_type op, - const char *value, const void *data) -{ + const char *value, const void *data) { struct token *token = &rule_tmp->token[rule_tmp->token_cur]; const char *attr = NULL; - memset(token, 0x00, sizeof(struct token)); + memzero(token, sizeof(struct token)); switch (type) { case TK_M_ACTION: @@ -900,6 +897,7 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, case TK_A_GOTO: case TK_M_TAG: case TK_A_TAG: + case TK_A_STATIC_NODE: token->key.value_off = rules_add_string(rule_tmp->rules, value); break; case TK_M_IMPORT_BUILTIN: @@ -911,6 +909,7 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, case TK_M_ATTRS: case TK_A_ATTR: case TK_A_ENV: + case TK_A_SECLABEL: attr = data; token->key.value_off = rules_add_string(rule_tmp->rules, value); token->key.attr_off = rules_add_string(rule_tmp->rules, attr); @@ -942,19 +941,13 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, case TK_A_MODE_ID: token->key.mode = *(mode_t *)data; break; - case TK_A_STATIC_NODE: - token->key.value_off = rules_add_string(rule_tmp->rules, value); - break; - case TK_M_EVENT_TIMEOUT: - token->key.event_timeout = *(int *)data; - break; case TK_RULE: case TK_M_PARENTS_MIN: case TK_M_PARENTS_MAX: case TK_M_MAX: case TK_END: case TK_UNSET: - log_error("wrong type %u\n", type); + log_error("wrong type %u", type); return -1; } @@ -965,7 +958,7 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, int has_glob; has_split = (strchr(value, '|') != NULL); - has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL); + has_glob = string_is_glob(value); if (has_split && has_glob) { glob = GL_SPLIT_GLOB; } else if (has_split) { @@ -992,7 +985,7 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, } if (attr != NULL) { - /* check if property/attribut name has substitution chars */ + /* check if property/attribute name has substitution chars */ if (attr[0] == '[') token->key.attrsubst = SB_SUBSYS; else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL) @@ -1005,14 +998,13 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, token->key.op = op; rule_tmp->token_cur++; if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) { - log_error("temporary rule array too small\n"); + log_error("temporary rule array too small"); return -1; } return 0; } -static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) -{ +static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) { unsigned int i; unsigned int start = 0; unsigned int end = rule_tmp->token_cur; @@ -1047,15 +1039,14 @@ static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) } static int add_rule(struct udev_rules *rules, char *line, - const char *filename, unsigned int filename_off, unsigned int lineno) -{ + const char *filename, unsigned int filename_off, unsigned int lineno) { char *linepos; const char *attr; - struct rule_tmp rule_tmp; + struct rule_tmp rule_tmp = { + .rules = rules, + .rule.type = TK_RULE, + }; - memset(&rule_tmp, 0x00, sizeof(struct rule_tmp)); - rule_tmp.rules = rules; - rule_tmp.rule.type = TK_RULE; /* the offset in the rule is limited to unsigned short */ if (filename_off < USHRT_MAX) rule_tmp.rule.rule.filename_off = filename_off; @@ -1068,25 +1059,30 @@ static int add_rule(struct udev_rules *rules, char *line, enum operation_type op; if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) { + /* Avoid erroring on trailing whitespace. This is probably rare + * so save the work for the error case instead of always trying + * to strip the trailing whitespace with strstrip(). */ + while (isblank(*linepos)) + linepos++; + /* If we aren't at the end of the line, this is a parsing error. * Make a best effort to describe where the problem is. */ - if (*linepos != '\n') { - char buf[2] = {linepos[1]}; + if (!strchr(NEWLINE "\0", *linepos)) { + char buf[2] = {*linepos}; _cleanup_free_ char *tmp; tmp = cescape(buf); - log_error("invalid key/value pair in file %s on line %u," - "starting at character %lu ('%s')\n", + log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')", filename, lineno, linepos - line + 1, tmp); - if (linepos[1] == '#') - log_info("hint: comments can only start at beginning of line"); + if (*linepos == '#') + log_error("hint: comments can only start at beginning of line"); } break; } if (streq(key, "ACTION")) { if (op > OP_MATCH_MAX) { - log_error("invalid ACTION operation\n"); + log_error("invalid ACTION operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL); @@ -1095,7 +1091,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "DEVPATH")) { if (op > OP_MATCH_MAX) { - log_error("invalid DEVPATH operation\n"); + log_error("invalid DEVPATH operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL); @@ -1104,7 +1100,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "KERNEL")) { if (op > OP_MATCH_MAX) { - log_error("invalid KERNEL operation\n"); + log_error("invalid KERNEL operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL); @@ -1113,7 +1109,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "SUBSYSTEM")) { if (op > OP_MATCH_MAX) { - log_error("invalid SUBSYSTEM operation\n"); + log_error("invalid SUBSYSTEM operation"); goto invalid; } /* bus, class, subsystem events should all be the same */ @@ -1121,7 +1117,7 @@ static int add_rule(struct udev_rules *rules, char *line, streq(value, "bus") || streq(value, "class")) { if (streq(value, "bus") || streq(value, "class")) - log_error("'%s' must be specified as 'subsystem' \n" + log_error("'%s' must be specified as 'subsystem' " "please fix it in %s:%u", value, filename, lineno); rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL); } else @@ -1131,7 +1127,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "DRIVER")) { if (op > OP_MATCH_MAX) { - log_error("invalid DRIVER operation\n"); + log_error("invalid DRIVER operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL); @@ -1139,9 +1135,13 @@ static int add_rule(struct udev_rules *rules, char *line, } if (startswith(key, "ATTR{")) { - attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1); + attr = get_key_attribute(rules->udev, key + strlen("ATTR")); if (attr == NULL) { - log_error("error parsing ATTR attribute\n"); + log_error("error parsing ATTR attribute"); + goto invalid; + } + if (op == OP_REMOVE) { + log_error("invalid ATTR operation"); goto invalid; } if (op < OP_MATCH_MAX) { @@ -1152,9 +1152,24 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } + if (startswith(key, "SECLABEL{")) { + attr = get_key_attribute(rules->udev, key + strlen("SECLABEL")); + if (!attr) { + 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; + } + if (streq(key, "KERNELS")) { if (op > OP_MATCH_MAX) { - log_error("invalid KERNELS operation\n"); + log_error("invalid KERNELS operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL); @@ -1163,7 +1178,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "SUBSYSTEMS")) { if (op > OP_MATCH_MAX) { - log_error("invalid SUBSYSTEMS operation\n"); + log_error("invalid SUBSYSTEMS operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL); @@ -1172,7 +1187,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "DRIVERS")) { if (op > OP_MATCH_MAX) { - log_error("invalid DRIVERS operation\n"); + log_error("invalid DRIVERS operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL); @@ -1181,12 +1196,12 @@ static int add_rule(struct udev_rules *rules, char *line, if (startswith(key, "ATTRS{")) { if (op > OP_MATCH_MAX) { - log_error("invalid ATTRS operation\n"); + log_error("invalid ATTRS operation"); goto invalid; } - attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1); + attr = get_key_attribute(rules->udev, key + strlen("ATTRS")); if (attr == NULL) { - log_error("error parsing ATTRS attribute\n"); + log_error("error parsing ATTRS attribute"); goto invalid; } if (startswith(attr, "device/")) @@ -1201,7 +1216,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (streq(key, "TAGS")) { if (op > OP_MATCH_MAX) { - log_error("invalid TAGS operation\n"); + log_error("invalid TAGS operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL); @@ -1209,9 +1224,13 @@ static int add_rule(struct udev_rules *rules, char *line, } if (startswith(key, "ENV{")) { - attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1); + attr = get_key_attribute(rules->udev, key + strlen("ENV")); if (attr == NULL) { - log_error("error parsing ENV attribute\n"); + log_error("error parsing ENV attribute"); + goto invalid; + } + if (op == OP_REMOVE) { + log_error("invalid ENV operation"); goto invalid; } if (op < OP_MATCH_MAX) { @@ -1236,7 +1255,7 @@ static int add_rule(struct udev_rules *rules, char *line, for (i = 0; i < ELEMENTSOF(blacklist); i++) { if (!streq(attr, blacklist[i])) continue; - log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno); + log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno); goto invalid; } if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0) @@ -1254,13 +1273,17 @@ 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; } if (streq(key, "RESULT")) { if (op > OP_MATCH_MAX) { - log_error("invalid RESULT operation\n"); + log_error("invalid RESULT operation"); goto invalid; } rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL); @@ -1268,11 +1291,15 @@ static int add_rule(struct udev_rules *rules, char *line, } if (startswith(key, "IMPORT")) { - attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1); + attr = get_key_attribute(rules->udev, key + strlen("IMPORT")); if (attr == NULL) { - log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno); + 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] != '/') { @@ -1280,7 +1307,7 @@ static int add_rule(struct udev_rules *rules, char *line, cmd = udev_builtin_lookup(value); if (cmd < UDEV_BUILTIN_MAX) { - log_debug("IMPORT found builtin '%s', replacing %s:%u\n", + log_debug("IMPORT found builtin '%s', replacing %s:%u", value, filename, lineno); rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd); continue; @@ -1293,7 +1320,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (cmd < UDEV_BUILTIN_MAX) rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd); else - log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno); + log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno); } else if (streq(attr, "file")) { rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL); } else if (streq(attr, "db")) { @@ -1303,7 +1330,7 @@ static int add_rule(struct udev_rules *rules, char *line, } else if (streq(attr, "parent")) { rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL); } else - log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno); + log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno); continue; } @@ -1311,10 +1338,10 @@ static int add_rule(struct udev_rules *rules, char *line, mode_t mode = 0; if (op > OP_MATCH_MAX) { - log_error("invalid TEST operation\n"); + log_error("invalid TEST operation"); goto invalid; } - attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1); + attr = get_key_attribute(rules->udev, key + strlen("TEST")); if (attr != NULL) { mode = strtol(attr, NULL, 8); rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode); @@ -1325,9 +1352,13 @@ static int add_rule(struct udev_rules *rules, char *line, } if (startswith(key, "RUN")) { - attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1); + 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); @@ -1335,34 +1366,50 @@ static int add_rule(struct udev_rules *rules, char *line, if (cmd < UDEV_BUILTIN_MAX) rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd); else - log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno); + log_error("RUN{builtin}: '%s' unknown %s:%u", value, filename, lineno); } else if (streq(attr, "program")) { enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX; rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd); } else { - log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno); + log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno); } continue; } 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 { @@ -1383,6 +1430,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 @@ -1395,6 +1446,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); @@ -1412,6 +1468,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); @@ -1429,6 +1490,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); @@ -1441,6 +1507,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=")]); @@ -1448,13 +1519,6 @@ static int add_rule(struct udev_rules *rules, char *line, rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio); } - pos = strstr(value, "event_timeout="); - if (pos != NULL) { - int tout = atoi(&pos[strlen("event_timeout=")]); - - rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout); - } - pos = strstr(value, "string_escape="); if (pos != NULL) { pos = &pos[strlen("string_escape=")]; @@ -1491,7 +1555,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - log_error("unknown key '%s' in %s:%u\n", key, filename, lineno); + log_error("unknown key '%s' in %s:%u", key, filename, lineno); goto invalid; } @@ -1506,28 +1570,31 @@ static int add_rule(struct udev_rules *rules, char *line, return 0; invalid: - log_error("invalid rule '%s:%u'\n", filename, lineno); + log_error("invalid rule '%s:%u'", filename, lineno); return -1; } -static int parse_file(struct udev_rules *rules, const char *filename) -{ - FILE *f; +static int parse_file(struct udev_rules *rules, const char *filename) { + _cleanup_fclose_ FILE *f = NULL; unsigned int first_token; unsigned int filename_off; char line[UTIL_LINE_SIZE]; int line_nr = 0; unsigned int i; - if (null_or_empty_path(filename)) { - log_debug("skip empty file: %s\n", filename); - return 0; + f = fopen(filename, "re"); + if (!f) { + if (errno == ENOENT) + return 0; + else + return -errno; } - log_debug("read rules file: %s\n", filename); - f = fopen(filename, "re"); - if (f == NULL) - return -1; + if (null_or_empty_fd(fileno(f))) { + log_debug("Skipping empty file: %s", filename); + return 0; + } else + log_debug("Reading rules file: %s", filename); first_token = rules->token_cur; filename_off = rules_add_string(rules, filename); @@ -1561,12 +1628,11 @@ static int parse_file(struct udev_rules *rules, const char *filename) } if (len+1 >= sizeof(line)) { - log_error("line too long '%s':%u, ignored\n", filename, line_nr); + log_error("line too long '%s':%u, ignored", filename, line_nr); continue; } add_rule(rules, key, filename, filename_off, line_nr); } - fclose(f); /* link GOTOs to LABEL rules in this file to be able to fast-forward */ for (i = first_token+1; i < rules->token_cur; i++) { @@ -1585,21 +1651,20 @@ static int parse_file(struct udev_rules *rules, const char *filename) break; } if (rules->tokens[i].key.rule_goto == 0) - log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename); + log_error("GOTO '%s' has no matching label in: '%s'", label, filename); } } return 0; } -struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) -{ +struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) { struct udev_rules *rules; struct udev_list file_list; struct token end_token; char **files, **f; int r; - rules = calloc(1, sizeof(struct udev_rules)); + rules = new0(struct udev_rules, 1); if (rules == NULL) return NULL; rules->udev = udev; @@ -1616,28 +1681,11 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) if (!rules->strbuf) return udev_rules_unref(rules); - rules->dirs = strv_new("/etc/udev/rules.d", - "/run/udev/rules.d", - UDEVLIBEXECDIR "/rules.d", - NULL); - if (!rules->dirs) { - log_error("failed to build config directory array"); - return udev_rules_unref(rules); - } - if (!path_strv_canonicalize(rules->dirs)) { - log_error("failed to canonicalize config directories\n"); - return udev_rules_unref(rules); - } - strv_uniq(rules->dirs); - - rules->dirs_ts_usec = calloc(strv_length(rules->dirs), sizeof(usec_t)); - if(!rules->dirs_ts_usec) - return udev_rules_unref(rules); udev_rules_check_timestamp(rules); - r = conf_files_list_strv(&files, ".rules", NULL, (const char **)rules->dirs); + r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs); if (r < 0) { - log_error("failed to enumerate rules files: %s\n", strerror(-r)); + log_error_errno(r, "failed to enumerate rules files: %m"); return udev_rules_unref(rules); } @@ -1653,14 +1701,14 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) strv_free(files); - memset(&end_token, 0x00, sizeof(struct token)); + memzero(&end_token, sizeof(struct token)); end_token.type = TK_END; add_token(rules, &end_token); - log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings\n", + log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings", rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len); /* cleanup temporary strbuf data */ - log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used\n", + log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used", rules->strbuf->in_count, rules->strbuf->in_len, rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count); strbuf_complete(rules->strbuf); @@ -1679,52 +1727,25 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) return rules; } -struct udev_rules *udev_rules_unref(struct udev_rules *rules) -{ +struct udev_rules *udev_rules_unref(struct udev_rules *rules) { if (rules == NULL) return NULL; free(rules->tokens); strbuf_cleanup(rules->strbuf); free(rules->uids); free(rules->gids); - strv_free(rules->dirs); - free(rules->dirs_ts_usec); free(rules); return NULL; } -bool udev_rules_check_timestamp(struct udev_rules *rules) -{ - unsigned int i; - bool changed = false; - - if (rules == NULL) - goto out; - - for (i = 0; rules->dirs[i]; i++) { - struct stat stats; - - if (stat(rules->dirs[i], &stats) < 0) - continue; - - if (rules->dirs_ts_usec[i] == timespec_load(&stats.st_mtim)) - continue; - - /* first check */ - if (rules->dirs_ts_usec[i] != 0) { - log_debug("reload - timestamp of '%s' changed\n", rules->dirs[i]); - changed = true; - } +bool udev_rules_check_timestamp(struct udev_rules *rules) { + if (!rules) + return false; - /* update timestamp */ - rules->dirs_ts_usec[i] = timespec_load(&stats.st_mtim); - } -out: - return changed; + return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true); } -static int match_key(struct udev_rules *rules, struct token *token, const char *val) -{ +static int match_key(struct udev_rules *rules, struct token *token, const char *val) { char *key_value = rules_str(rules, token->key.value_off); char *pos; bool match = false; @@ -1797,8 +1818,7 @@ static int match_key(struct udev_rules *rules, struct token *token, const char * return -1; } -static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) -{ +static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) { const char *name; char nbuf[UTIL_NAME_SIZE]; const char *value; @@ -1852,8 +1872,12 @@ enum escape_type { ESCAPE_REPLACE, }; -int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask) -{ +int udev_rules_apply_to_event(struct udev_rules *rules, + struct udev_event *event, + usec_t timeout_usec, + usec_t timeout_warn_usec, + struct udev_list *properties_list, + const sigset_t *sigmask) { struct token *cur; struct token *rule; enum escape_type esc = ESCAPE_UNSET; @@ -1918,7 +1942,18 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event const char *value; value = udev_device_get_property_value(event->dev, key_name); - if (value == NULL) + + /* check global properties */ + if (!value && properties_list) { + struct udev_list_entry *list_entry; + + list_entry = udev_list_get_entry(properties_list); + list_entry = udev_list_entry_get_by_name(list_entry, key_name); + if (list_entry != NULL) + value = udev_list_entry_get_value(list_entry); + } + + if (!value) value = ""; if (match_key(rules, cur, value)) goto nomatch; @@ -2047,25 +2082,21 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event goto nomatch; break; } - case TK_M_EVENT_TIMEOUT: - log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout); - event->timeout_usec = cur->key.event_timeout * 1000 * 1000; - break; case TK_M_PROGRAM: { char program[UTIL_PATH_SIZE]; char **envp; - char result[UTIL_PATH_SIZE]; + char result[UTIL_LINE_SIZE]; free(event->program_result); event->program_result = NULL; udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program)); envp = udev_device_get_properties_envp(event->dev); - log_debug("PROGRAM '%s' %s:%u\n", + log_debug("PROGRAM '%s' %s:%u", program, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); - if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) { + if (udev_event_spawn(event, timeout_usec, timeout_warn_usec, program, envp, sigmask, result, sizeof(result)) < 0) { if (cur->key.op != OP_NOMATCH) goto nomatch; } else { @@ -2075,7 +2106,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) { count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT); if (count > 0) - log_debug("%i character(s) replaced\n" , count); + log_debug("%i character(s) replaced" , count); } event->program_result = strdup(result); if (cur->key.op == OP_NOMATCH) @@ -2096,12 +2127,12 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event char import[UTIL_PATH_SIZE]; udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import)); - log_debug("IMPORT '%s' %s:%u\n", + log_debug("IMPORT '%s' %s:%u", import, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); - if (import_program_into_properties(event, import, sigmask) != 0) + if (import_program_into_properties(event, timeout_usec, timeout_warn_usec, import, sigmask) != 0) if (cur->key.op != OP_NOMATCH) goto nomatch; break; @@ -2112,7 +2143,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event if (udev_builtin_run_once(cur->key.builtin_cmd)) { /* check if we ran already */ if (event->builtin_run & (1 << cur->key.builtin_cmd)) { - log_debug("IMPORT builtin skip '%s' %s:%u\n", + log_debug("IMPORT builtin skip '%s' %s:%u", udev_builtin_name(cur->key.builtin_cmd), rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2127,14 +2158,14 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event } udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command)); - log_debug("IMPORT builtin '%s' %s:%u\n", + log_debug("IMPORT builtin '%s' %s:%u", udev_builtin_name(cur->key.builtin_cmd), rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) { /* remember failure */ - log_debug("IMPORT builtin '%s' returned non-zero\n", + log_debug("IMPORT builtin '%s' returned non-zero", udev_builtin_name(cur->key.builtin_cmd)); event->builtin_ret |= (1 << cur->key.builtin_cmd); if (cur->key.op != OP_NOMATCH) @@ -2234,6 +2265,8 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event break; case TK_A_OWNER: { char owner[UTIL_NAME_SIZE]; + const char *ow = owner; + int r; if (event->owner_final) break; @@ -2241,8 +2274,16 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event event->owner_final = true; udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner)); event->owner_set = true; - event->uid = util_lookup_user(event->udev, owner); - log_debug("OWNER %u %s:%u\n", + r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL); + if (r < 0) { + if (r == -ENOENT || r == -ESRCH) + log_error("specified user '%s' unknown", owner); + else + log_error_errno(r, "error resolving user '%s': %m", owner); + + event->uid = 0; + } + log_debug("OWNER %u %s:%u", event->uid, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2250,6 +2291,8 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event } case TK_A_GROUP: { char group[UTIL_NAME_SIZE]; + const char *gr = group; + int r; if (event->group_final) break; @@ -2257,8 +2300,16 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event event->group_final = true; udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group)); event->group_set = true; - event->gid = util_lookup_group(event->udev, group); - log_debug("GROUP %u %s:%u\n", + r = get_group_creds(&gr, &event->gid); + if (r < 0) { + if (r == -ENOENT || r == -ESRCH) + log_error("specified group '%s' unknown", group); + else + log_error_errno(r, "error resolving group '%s': %m", group); + + event->gid = 0; + } + log_debug("GROUP %u %s:%u", event->gid, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2274,14 +2325,14 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str)); mode = strtol(mode_str, &endptr, 8); if (endptr[0] != '\0') { - log_error("ignoring invalid mode '%s'\n", mode_str); + log_error("ignoring invalid mode '%s'", mode_str); break; } if (cur->key.op == OP_ASSIGN_FINAL) event->mode_final = true; event->mode_set = true; event->mode = mode; - log_debug("MODE %#o %s:%u\n", + log_debug("MODE %#o %s:%u", event->mode, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2294,7 +2345,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event event->owner_final = true; event->owner_set = true; event->uid = cur->key.uid; - log_debug("OWNER %u %s:%u\n", + log_debug("OWNER %u %s:%u", event->uid, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2306,7 +2357,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event event->group_final = true; event->group_set = true; event->gid = cur->key.gid; - log_debug("GROUP %u %s:%u\n", + log_debug("GROUP %u %s:%u", event->gid, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2318,11 +2369,25 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event event->mode_final = true; event->mode_set = true; event->mode = cur->key.mode; - log_debug("MODE %#o %s:%u\n", + log_debug("MODE %#o %s:%u", event->mode, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); break; + case TK_A_SECLABEL: { + const char *name, *label; + + name = rules_str(rules, cur->key.attr_off); + label = rules_str(rules, cur->key.value_off); + if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL) + udev_list_cleanup(&event->seclabel_list); + udev_list_entry_add(&event->seclabel_list, name, label); + log_debug("SECLABEL{%s}='%s' %s:%u", + name, label, + rules_str(rules, rule->rule.filename_off), + rule->rule.filename_line); + break; + } case TK_A_ENV: { const char *name = rules_str(rules, cur->key.attr_off); char *value = rules_str(rules, cur->key.value_off); @@ -2367,10 +2432,13 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event (*p >= '0' && *p <= '9') || *p == '-' || *p == '_') continue; - log_error("ignoring invalid tag name '%s'\n", tag); + 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: { @@ -2387,7 +2455,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) { count = util_replace_chars(name_str, "/"); if (count > 0) - log_debug("%i character(s) replaced\n", count); + log_debug("%i character(s) replaced", count); } if (major(udev_device_get_devnum(event->dev)) && (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) { @@ -2398,7 +2466,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event } free(event->name); event->name = strdup(name_str); - log_debug("NAME '%s' %s:%u\n", + log_debug("NAME '%s' %s:%u", event->name, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2426,14 +2494,14 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event else if (esc == ESCAPE_REPLACE) count = util_replace_chars(temp, "/"); if (count > 0) - log_debug("%i character(s) replaced\n" , count); + log_debug("%i character(s) replaced" , count); pos = temp; while (isspace(pos[0])) pos++; next = strchr(pos, ' '); while (next != NULL) { next[0] = '\0'; - log_debug("LINK '%s' %s:%u\n", pos, + log_debug("LINK '%s' %s:%u", pos, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); strscpyl(filename, sizeof(filename), "/dev/", pos, NULL); udev_device_add_devlink(event->dev, filename); @@ -2443,7 +2511,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event next = strchr(pos, ' '); } if (pos[0] != '\0') { - log_debug("LINK '%s' %s:%u\n", pos, + log_debug("LINK '%s' %s:%u", pos, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); strscpyl(filename, sizeof(filename), "/dev/", pos, NULL); udev_device_add_devlink(event->dev, filename); @@ -2461,16 +2529,16 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event attr_subst_subdir(attr, sizeof(attr)); udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value)); - log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value, + log_debug("ATTR '%s' writing '%s' %s:%u", attr, value, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); f = fopen(attr, "we"); if (f != NULL) { if (fprintf(f, "%s", value) <= 0) - log_error("error writing ATTR{%s}: %m\n", attr); + log_error_errno(errno, "error writing ATTR{%s}: %m", attr); fclose(f); } else { - log_error("error opening ATTR{%s} for writing: %m\n", attr); + log_error_errno(errno, "error opening ATTR{%s} for writing: %m", attr); } break; } @@ -2480,7 +2548,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL) udev_list_cleanup(&event->run_list); - log_debug("RUN '%s' %s:%u\n", + log_debug("RUN '%s' %s:%u", rules_str(rules, cur->key.value_off), rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); @@ -2500,7 +2568,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event case TK_M_PARENTS_MAX: case TK_M_MAX: case TK_UNSET: - log_error("wrong type %u\n", cur->type); + log_error("wrong type %u", cur->type); goto nomatch; } @@ -2512,8 +2580,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event } } -int udev_rules_apply_static_dev_perms(struct udev_rules *rules) -{ +int udev_rules_apply_static_dev_perms(struct udev_rules *rules) { struct token *cur; struct token *rule; uid_t uid = 0; @@ -2568,42 +2635,41 @@ int udev_rules_apply_static_dev_perms(struct udev_rules *rules) struct stat stats; /* we assure, that the permissions tokens are sorted before the static token */ + if (mode == 0 && uid == 0 && gid == 0 && tags == NULL) goto next; + strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL); if (stat(device_node, &stats) != 0) - goto next; + break; if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode)) - goto next; + break; + /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */ if (tags) { - /* Export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */ - STRV_FOREACH(t, tags) { _cleanup_free_ char *unescaped_filename = NULL; strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL); r = mkdir_p(tags_dir, 0755); - if (r < 0) { - log_error("failed to create %s: %s\n", tags_dir, strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "failed to create %s: %m", tags_dir); unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/."); strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL); r = symlink(device_node, tag_symlink); - if (r < 0 && errno != EEXIST) { - log_error("failed to create symlink %s -> %s: %s\n", tag_symlink, device_node, strerror(errno)); - return -errno; - } else + if (r < 0 && errno != EEXIST) + return log_error_errno(errno, "failed to create symlink %s -> %s: %m", + tag_symlink, device_node); + else r = 0; } } /* don't touch the permissions if only the tags were set */ if (mode == 0 && uid == 0 && gid == 0) - goto next; + break; if (mode == 0) { if (gid > 0) @@ -2614,19 +2680,19 @@ int udev_rules_apply_static_dev_perms(struct udev_rules *rules) if (mode != (stats.st_mode & 01777)) { r = chmod(device_node, mode); if (r < 0) { - log_error("failed to chmod '%s' %#o\n", device_node, mode); + log_error("failed to chmod '%s' %#o", device_node, mode); return -errno; } else - log_debug("chmod '%s' %#o\n", device_node, mode); + log_debug("chmod '%s' %#o", device_node, mode); } if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) { r = chown(device_node, uid, gid); if (r < 0) { - log_error("failed to chown '%s' %u %u \n", device_node, uid, gid); + log_error("failed to chown '%s' %u %u ", device_node, uid, gid); return -errno; } else - log_debug("chown '%s' %u %u\n", device_node, uid, gid); + log_debug("chown '%s' %u %u", device_node, uid, gid); } utimensat(AT_FDCWD, device_node, NULL, 0);