2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "path-util.h"
34 #include "conf-files.h"
39 #define PREALLOC_TOKEN 2048
42 unsigned int name_off;
49 static const char* const rules_dirs[] = {
52 UDEVLIBEXECDIR "/rules.d",
60 /* every key in the rules file becomes a token */
62 unsigned int token_cur;
63 unsigned int token_max;
65 /* all key strings are copied and de-duplicated in a single continuous string buffer */
66 struct strbuf *strbuf;
68 /* during rule parsing, uid/gid lookup results are cached */
70 unsigned int uids_cur;
71 unsigned int uids_max;
73 unsigned int gids_cur;
74 unsigned int gids_max;
77 static char *rules_str(struct udev_rules *rules, unsigned int off) {
78 return rules->strbuf->buf + off;
81 static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
82 return strbuf_add_string(rules->strbuf, s, strlen(s));
85 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
99 enum string_glob_type {
101 GL_PLAIN, /* no special chars */
102 GL_GLOB, /* shell globs ?,*,[] */
103 GL_SPLIT, /* multi-value A|B */
104 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
105 GL_SOMETHING, /* commonly used "?*" */
108 enum string_subst_type {
115 /* tokens of a rule are sorted/handled in this order */
120 TK_M_ACTION, /* val */
121 TK_M_DEVPATH, /* val */
122 TK_M_KERNEL, /* val */
123 TK_M_DEVLINK, /* val */
125 TK_M_ENV, /* val, attr */
127 TK_M_SUBSYSTEM, /* val */
128 TK_M_DRIVER, /* val */
129 TK_M_WAITFOR, /* val */
130 TK_M_ATTR, /* val, attr */
133 TK_M_KERNELS, /* val */
134 TK_M_SUBSYSTEMS, /* val */
135 TK_M_DRIVERS, /* val */
136 TK_M_ATTRS, /* val, attr */
140 TK_M_TEST, /* val, mode_t */
141 TK_M_PROGRAM, /* val */
142 TK_M_IMPORT_FILE, /* val */
143 TK_M_IMPORT_PROG, /* val */
144 TK_M_IMPORT_BUILTIN, /* val */
145 TK_M_IMPORT_DB, /* val */
146 TK_M_IMPORT_CMDLINE, /* val */
147 TK_M_IMPORT_PARENT, /* val */
148 TK_M_RESULT, /* val */
151 TK_A_STRING_ESCAPE_NONE,
152 TK_A_STRING_ESCAPE_REPLACE,
154 TK_A_INOTIFY_WATCH, /* int */
155 TK_A_DEVLINK_PRIO, /* int */
156 TK_A_OWNER, /* val */
157 TK_A_GROUP, /* val */
159 TK_A_OWNER_ID, /* uid_t */
160 TK_A_GROUP_ID, /* gid_t */
161 TK_A_MODE_ID, /* mode_t */
163 TK_A_STATIC_NODE, /* val */
164 TK_A_SECLABEL, /* val, attr */
165 TK_A_ENV, /* val, attr */
167 TK_A_DEVLINK, /* val */
168 TK_A_ATTR, /* val, attr */
169 TK_A_RUN_BUILTIN, /* val, bool */
170 TK_A_RUN_PROGRAM, /* val, bool */
171 TK_A_GOTO, /* size_t */
176 /* we try to pack stuff in a way that we take only 12 bytes per token */
179 unsigned char type; /* same in rule and key */
181 enum token_type type:8;
183 bool has_static_node:1;
184 unsigned int unused:6;
185 unsigned short token_count;
186 unsigned int label_off;
187 unsigned short filename_off;
188 unsigned short filename_line;
191 enum token_type type:8;
192 enum operation_type op:8;
193 enum string_glob_type glob:8;
194 enum string_subst_type subst:4;
195 enum string_subst_type attrsubst:4;
196 unsigned int value_off;
198 unsigned int attr_off;
199 unsigned int rule_goto;
205 enum udev_builtin_cmd builtin_cmd;
213 struct udev_rules *rules;
215 struct token token[MAX_TK];
216 unsigned int token_cur;
220 static const char *operation_str(enum operation_type type) {
221 static const char *operation_strs[] = {
222 [OP_UNSET] = "UNSET",
223 [OP_MATCH] = "match",
224 [OP_NOMATCH] = "nomatch",
225 [OP_MATCH_MAX] = "MATCH_MAX",
228 [OP_REMOVE] = "remove",
229 [OP_ASSIGN] = "assign",
230 [OP_ASSIGN_FINAL] = "assign-final",
233 return operation_strs[type];
236 static const char *string_glob_str(enum string_glob_type type) {
237 static const char *string_glob_strs[] = {
238 [GL_UNSET] = "UNSET",
239 [GL_PLAIN] = "plain",
241 [GL_SPLIT] = "split",
242 [GL_SPLIT_GLOB] = "split-glob",
243 [GL_SOMETHING] = "split-glob",
246 return string_glob_strs[type];
249 static const char *token_str(enum token_type type) {
250 static const char *token_strs[] = {
251 [TK_UNSET] = "UNSET",
254 [TK_M_ACTION] = "M ACTION",
255 [TK_M_DEVPATH] = "M DEVPATH",
256 [TK_M_KERNEL] = "M KERNEL",
257 [TK_M_DEVLINK] = "M DEVLINK",
258 [TK_M_NAME] = "M NAME",
259 [TK_M_ENV] = "M ENV",
260 [TK_M_TAG] = "M TAG",
261 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
262 [TK_M_DRIVER] = "M DRIVER",
263 [TK_M_WAITFOR] = "M WAITFOR",
264 [TK_M_ATTR] = "M ATTR",
266 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
267 [TK_M_KERNELS] = "M KERNELS",
268 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
269 [TK_M_DRIVERS] = "M DRIVERS",
270 [TK_M_ATTRS] = "M ATTRS",
271 [TK_M_TAGS] = "M TAGS",
272 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
274 [TK_M_TEST] = "M TEST",
275 [TK_M_PROGRAM] = "M PROGRAM",
276 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
277 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
278 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
279 [TK_M_IMPORT_DB] = "M IMPORT_DB",
280 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
281 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
282 [TK_M_RESULT] = "M RESULT",
283 [TK_M_MAX] = "M MAX",
285 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
286 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
287 [TK_A_DB_PERSIST] = "A DB_PERSIST",
288 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
289 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
290 [TK_A_OWNER] = "A OWNER",
291 [TK_A_GROUP] = "A GROUP",
292 [TK_A_MODE] = "A MODE",
293 [TK_A_OWNER_ID] = "A OWNER_ID",
294 [TK_A_GROUP_ID] = "A GROUP_ID",
295 [TK_A_STATIC_NODE] = "A STATIC_NODE",
296 [TK_A_SECLABEL] = "A SECLABEL",
297 [TK_A_MODE_ID] = "A MODE_ID",
298 [TK_A_ENV] = "A ENV",
299 [TK_A_TAG] = "A ENV",
300 [TK_A_NAME] = "A NAME",
301 [TK_A_DEVLINK] = "A DEVLINK",
302 [TK_A_ATTR] = "A ATTR",
303 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
304 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
305 [TK_A_GOTO] = "A GOTO",
310 return token_strs[type];
313 static void dump_token(struct udev_rules *rules, struct token *token) {
314 enum token_type type = token->type;
315 enum operation_type op = token->key.op;
316 enum string_glob_type glob = token->key.glob;
317 const char *value = str(rules, token->key.value_off);
318 const char *attr = &rules->buf[token->key.attr_off];
323 const char *tks_ptr = (char *)rules->tokens;
324 const char *tk_ptr = (char *)token;
325 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
327 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
328 &rules->buf[token->rule.filename_off], token->rule.filename_line,
329 idx, token->rule.token_count,
330 &rules->buf[token->rule.label_off]);
342 case TK_M_SUBSYSTEMS:
346 case TK_M_IMPORT_FILE:
347 case TK_M_IMPORT_PROG:
349 case TK_M_IMPORT_CMDLINE:
350 case TK_M_IMPORT_PARENT:
357 case TK_A_RUN_BUILTIN:
358 case TK_A_RUN_PROGRAM:
359 log_debug("%s %s '%s'(%s)",
360 token_str(type), operation_str(op), value, string_glob_str(glob));
362 case TK_M_IMPORT_BUILTIN:
363 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
370 log_debug("%s %s '%s' '%s'(%s)",
371 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
375 log_debug("%s %s '%s'", token_str(type), operation_str(op), value);
377 case TK_A_STRING_ESCAPE_NONE:
378 case TK_A_STRING_ESCAPE_REPLACE:
379 case TK_A_DB_PERSIST:
380 log_debug("%s", token_str(type));
383 log_debug("%s %s '%s'(%s) %#o",
384 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
386 case TK_A_INOTIFY_WATCH:
387 log_debug("%s %u", token_str(type), token->key.watch);
389 case TK_A_DEVLINK_PRIO:
390 log_debug("%s %u", token_str(type), token->key.devlink_prio);
393 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid);
396 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid);
399 log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode);
401 case TK_A_STATIC_NODE:
402 log_debug("%s '%s'", token_str(type), value);
405 log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value);
408 log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto);
411 log_debug("* %s", token_str(type));
413 case TK_M_PARENTS_MIN:
414 case TK_M_PARENTS_MAX:
417 log_debug("unknown type %u", type);
422 static void dump_rules(struct udev_rules *rules) {
425 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
427 rules->token_cur * sizeof(struct token),
430 for (i = 0; i < rules->token_cur; i++)
431 dump_token(rules, &rules->tokens[i]);
434 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
435 static inline void dump_rules(struct udev_rules *rules) {}
438 static int add_token(struct udev_rules *rules, struct token *token) {
439 /* grow buffer if needed */
440 if (rules->token_cur+1 >= rules->token_max) {
441 struct token *tokens;
444 /* double the buffer size */
445 add = rules->token_max;
449 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
452 rules->tokens = tokens;
453 rules->token_max += add;
455 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
460 static uid_t add_uid(struct udev_rules *rules, const char *owner) {
466 /* lookup, if we know it already */
467 for (i = 0; i < rules->uids_cur; i++) {
468 off = rules->uids[i].name_off;
469 if (streq(rules_str(rules, off), owner)) {
470 uid = rules->uids[i].uid;
474 r = get_user_creds(&owner, &uid, NULL, NULL, NULL);
476 if (r == -ENOENT || r == -ESRCH)
477 log_error("specified user '%s' unknown", owner);
479 log_error_errno(r, "error resolving user '%s': %m", owner);
482 /* grow buffer if needed */
483 if (rules->uids_cur+1 >= rules->uids_max) {
484 struct uid_gid *uids;
487 /* double the buffer size */
488 add = rules->uids_max;
492 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
496 rules->uids_max += add;
498 rules->uids[rules->uids_cur].uid = uid;
499 off = rules_add_string(rules, owner);
502 rules->uids[rules->uids_cur].name_off = off;
507 static gid_t add_gid(struct udev_rules *rules, const char *group) {
513 /* lookup, if we know it already */
514 for (i = 0; i < rules->gids_cur; i++) {
515 off = rules->gids[i].name_off;
516 if (streq(rules_str(rules, off), group)) {
517 gid = rules->gids[i].gid;
521 r = get_group_creds(&group, &gid);
523 if (r == -ENOENT || r == -ESRCH)
524 log_error("specified group '%s' unknown", group);
526 log_error_errno(r, "error resolving group '%s': %m", group);
529 /* grow buffer if needed */
530 if (rules->gids_cur+1 >= rules->gids_max) {
531 struct uid_gid *gids;
534 /* double the buffer size */
535 add = rules->gids_max;
539 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
543 rules->gids_max += add;
545 rules->gids[rules->gids_cur].gid = gid;
546 off = rules_add_string(rules, group);
549 rules->gids[rules->gids_cur].name_off = off;
554 static int import_property_from_string(struct udev_device *dev, char *line) {
561 while (isspace(key[0]))
564 /* comment or empty line */
565 if (key[0] == '#' || key[0] == '\0')
568 /* split key/value */
569 val = strchr(key, '=');
576 while (isspace(val[0]))
583 while (isspace(key[len-1]))
587 /* terminate value */
591 while (isspace(val[len-1]))
599 if (val[0] == '"' || val[0] == '\'') {
600 if (val[len-1] != val[0]) {
601 log_debug("inconsistent quoting: '%s', skip", line);
608 udev_device_add_property(dev, key, val);
613 static int import_file_into_properties(struct udev_device *dev, const char *filename) {
615 char line[UTIL_LINE_SIZE];
617 f = fopen(filename, "re");
620 while (fgets(line, sizeof(line), f) != NULL)
621 import_property_from_string(dev, line);
626 static int import_program_into_properties(struct udev_event *event,
628 usec_t timeout_warn_usec,
629 const char *program, const sigset_t *sigmask) {
630 struct udev_device *dev = event->dev;
632 char result[UTIL_LINE_SIZE];
636 envp = udev_device_get_properties_envp(dev);
637 err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, program, envp, sigmask, result, sizeof(result));
642 while (line != NULL) {
645 pos = strchr(line, '\n');
650 import_property_from_string(dev, line);
656 static int import_parent_into_properties(struct udev_device *dev, const char *filter) {
657 struct udev_device *dev_parent;
658 struct udev_list_entry *list_entry;
660 dev_parent = udev_device_get_parent(dev);
661 if (dev_parent == NULL)
664 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
665 const char *key = udev_list_entry_get_name(list_entry);
666 const char *val = udev_list_entry_get_value(list_entry);
668 if (fnmatch(filter, key, 0) == 0) {
669 udev_device_add_property(dev, key, val);
675 #define WAIT_LOOP_PER_SECOND 50
676 static int wait_for_file(struct udev_device *dev, const char *file, int timeout) {
677 char filepath[UTIL_PATH_SIZE];
678 char devicepath[UTIL_PATH_SIZE];
680 int loop = timeout * WAIT_LOOP_PER_SECOND;
682 /* a relative path is a device attribute */
683 devicepath[0] = '\0';
684 if (file[0] != '/') {
685 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
686 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
691 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
694 if (stat(file, &stats) == 0) {
695 log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
698 /* make sure, the device did not disappear in the meantime */
699 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
700 log_debug("device disappeared while waiting for '%s'", file);
703 log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
704 nanosleep(&duration, NULL);
706 log_debug("waiting for '%s' failed", file);
710 static int attr_subst_subdir(char *attr, size_t len) {
713 if (strstr(attr, "/*/")) {
715 char dirname[UTIL_PATH_SIZE];
719 strscpy(dirname, sizeof(dirname), attr);
720 pos = strstr(dirname, "/*/");
725 dir = opendir(dirname);
729 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
732 if (dent->d_name[0] == '.')
734 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
735 if (stat(attr, &stats) == 0) {
747 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) {
752 if (linepos == NULL || linepos[0] == '\0')
755 /* skip whitespace */
756 while (isspace(linepos[0]) || linepos[0] == ',')
760 if (linepos[0] == '\0')
766 if (linepos[0] == '\0')
768 if (isspace(linepos[0]))
770 if (linepos[0] == '=')
772 if ((linepos[0] == '+') || (linepos[0] == '-') || (linepos[0] == '!') || (linepos[0] == ':'))
773 if (linepos[1] == '=')
777 /* remember end of key */
780 /* skip whitespace after key */
781 while (isspace(linepos[0]))
783 if (linepos[0] == '\0')
786 /* get operation type */
787 if (linepos[0] == '=' && linepos[1] == '=') {
790 } else if (linepos[0] == '!' && linepos[1] == '=') {
793 } else if (linepos[0] == '+' && linepos[1] == '=') {
796 } else if (linepos[0] == '-' && linepos[1] == '=') {
799 } else if (linepos[0] == '=') {
802 } else if (linepos[0] == ':' && linepos[1] == '=') {
803 *op = OP_ASSIGN_FINAL;
811 /* skip whitespace after operator */
812 while (isspace(linepos[0]))
814 if (linepos[0] == '\0')
818 if (linepos[0] == '"')
825 temp = strchr(linepos, '"');
831 /* move line to next key */
836 /* extract possible KEY{attr} */
837 static const char *get_key_attribute(struct udev *udev, char *str) {
841 attr = strchr(str, '{');
844 pos = strchr(attr, '}');
846 log_error("missing closing brace for format");
855 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
856 enum operation_type op,
857 const char *value, const void *data) {
858 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
859 const char *attr = NULL;
861 memzero(token, sizeof(struct token));
873 case TK_M_SUBSYSTEMS:
877 case TK_M_IMPORT_FILE:
878 case TK_M_IMPORT_PROG:
880 case TK_M_IMPORT_CMDLINE:
881 case TK_M_IMPORT_PARENT:
891 case TK_A_STATIC_NODE:
892 token->key.value_off = rules_add_string(rule_tmp->rules, value);
894 case TK_M_IMPORT_BUILTIN:
895 token->key.value_off = rules_add_string(rule_tmp->rules, value);
896 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
905 token->key.value_off = rules_add_string(rule_tmp->rules, value);
906 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
909 token->key.value_off = rules_add_string(rule_tmp->rules, value);
911 token->key.mode = *(mode_t *)data;
913 case TK_A_STRING_ESCAPE_NONE:
914 case TK_A_STRING_ESCAPE_REPLACE:
915 case TK_A_DB_PERSIST:
917 case TK_A_RUN_BUILTIN:
918 case TK_A_RUN_PROGRAM:
919 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
920 token->key.value_off = rules_add_string(rule_tmp->rules, value);
922 case TK_A_INOTIFY_WATCH:
923 case TK_A_DEVLINK_PRIO:
924 token->key.devlink_prio = *(int *)data;
927 token->key.uid = *(uid_t *)data;
930 token->key.gid = *(gid_t *)data;
933 token->key.mode = *(mode_t *)data;
936 case TK_M_PARENTS_MIN:
937 case TK_M_PARENTS_MAX:
941 log_error("wrong type %u", type);
945 if (value != NULL && type < TK_M_MAX) {
946 /* check if we need to split or call fnmatch() while matching rules */
947 enum string_glob_type glob;
951 has_split = (strchr(value, '|') != NULL);
952 has_glob = string_is_glob(value);
953 if (has_split && has_glob) {
954 glob = GL_SPLIT_GLOB;
955 } else if (has_split) {
957 } else if (has_glob) {
958 if (streq(value, "?*"))
965 token->key.glob = glob;
968 if (value != NULL && type > TK_M_MAX) {
969 /* check if assigned value has substitution chars */
971 token->key.subst = SB_SUBSYS;
972 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
973 token->key.subst = SB_FORMAT;
975 token->key.subst = SB_NONE;
979 /* check if property/attribute name has substitution chars */
981 token->key.attrsubst = SB_SUBSYS;
982 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
983 token->key.attrsubst = SB_FORMAT;
985 token->key.attrsubst = SB_NONE;
988 token->key.type = type;
990 rule_tmp->token_cur++;
991 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
992 log_error("temporary rule array too small");
998 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
1000 unsigned int start = 0;
1001 unsigned int end = rule_tmp->token_cur;
1003 for (i = 0; i < rule_tmp->token_cur; i++) {
1004 enum token_type next_val = TK_UNSET;
1005 unsigned int next_idx = 0;
1008 /* find smallest value */
1009 for (j = start; j < end; j++) {
1010 if (rule_tmp->token[j].type == TK_UNSET)
1012 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1013 next_val = rule_tmp->token[j].type;
1018 /* add token and mark done */
1019 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1021 rule_tmp->token[next_idx].type = TK_UNSET;
1024 if (next_idx == start)
1026 if (next_idx+1 == end)
1032 static int add_rule(struct udev_rules *rules, char *line,
1033 const char *filename, unsigned int filename_off, unsigned int lineno) {
1036 struct rule_tmp rule_tmp = {
1038 .rule.type = TK_RULE,
1041 /* the offset in the rule is limited to unsigned short */
1042 if (filename_off < USHRT_MAX)
1043 rule_tmp.rule.rule.filename_off = filename_off;
1044 rule_tmp.rule.rule.filename_line = lineno;
1050 enum operation_type op;
1052 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1053 /* Avoid erroring on trailing whitespace. This is probably rare
1054 * so save the work for the error case instead of always trying
1055 * to strip the trailing whitespace with strstrip(). */
1056 while (isblank(*linepos))
1059 /* If we aren't at the end of the line, this is a parsing error.
1060 * Make a best effort to describe where the problem is. */
1061 if (!strchr(NEWLINE, *linepos)) {
1062 char buf[2] = {*linepos};
1063 _cleanup_free_ char *tmp;
1066 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1067 filename, lineno, linepos - line + 1, tmp);
1068 if (*linepos == '#')
1069 log_error("hint: comments can only start at beginning of line");
1074 if (streq(key, "ACTION")) {
1075 if (op > OP_MATCH_MAX) {
1076 log_error("invalid ACTION operation");
1079 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1083 if (streq(key, "DEVPATH")) {
1084 if (op > OP_MATCH_MAX) {
1085 log_error("invalid DEVPATH operation");
1088 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1092 if (streq(key, "KERNEL")) {
1093 if (op > OP_MATCH_MAX) {
1094 log_error("invalid KERNEL operation");
1097 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1101 if (streq(key, "SUBSYSTEM")) {
1102 if (op > OP_MATCH_MAX) {
1103 log_error("invalid SUBSYSTEM operation");
1106 /* bus, class, subsystem events should all be the same */
1107 if (streq(value, "subsystem") ||
1108 streq(value, "bus") ||
1109 streq(value, "class")) {
1110 if (streq(value, "bus") || streq(value, "class"))
1111 log_error("'%s' must be specified as 'subsystem' "
1112 "please fix it in %s:%u", value, filename, lineno);
1113 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1115 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1119 if (streq(key, "DRIVER")) {
1120 if (op > OP_MATCH_MAX) {
1121 log_error("invalid DRIVER operation");
1124 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1128 if (startswith(key, "ATTR{")) {
1129 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
1131 log_error("error parsing ATTR attribute");
1134 if (op == OP_REMOVE) {
1135 log_error("invalid ATTR operation");
1138 if (op < OP_MATCH_MAX) {
1139 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1141 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1146 if (startswith(key, "SECLABEL{")) {
1147 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
1149 log_error("error parsing SECLABEL attribute");
1152 if (op == OP_REMOVE) {
1153 log_error("invalid SECLABEL operation");
1157 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1161 if (streq(key, "KERNELS")) {
1162 if (op > OP_MATCH_MAX) {
1163 log_error("invalid KERNELS operation");
1166 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1170 if (streq(key, "SUBSYSTEMS")) {
1171 if (op > OP_MATCH_MAX) {
1172 log_error("invalid SUBSYSTEMS operation");
1175 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1179 if (streq(key, "DRIVERS")) {
1180 if (op > OP_MATCH_MAX) {
1181 log_error("invalid DRIVERS operation");
1184 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1188 if (startswith(key, "ATTRS{")) {
1189 if (op > OP_MATCH_MAX) {
1190 log_error("invalid ATTRS operation");
1193 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
1195 log_error("error parsing ATTRS attribute");
1198 if (startswith(attr, "device/"))
1199 log_error("the 'device' link may not be available in a future kernel, "
1200 "please fix it in %s:%u", filename, lineno);
1201 else if (strstr(attr, "../") != NULL)
1202 log_error("do not reference parent sysfs directories directly, "
1203 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1204 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1208 if (streq(key, "TAGS")) {
1209 if (op > OP_MATCH_MAX) {
1210 log_error("invalid TAGS operation");
1213 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1217 if (startswith(key, "ENV{")) {
1218 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
1220 log_error("error parsing ENV attribute");
1223 if (op == OP_REMOVE) {
1224 log_error("invalid ENV operation");
1227 if (op < OP_MATCH_MAX) {
1228 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1231 static const char *blacklist[] = {
1246 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1247 if (!streq(attr, blacklist[i]))
1249 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
1252 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1258 if (streq(key, "TAG")) {
1259 if (op < OP_MATCH_MAX)
1260 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1262 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1266 if (streq(key, "PROGRAM")) {
1267 if (op == OP_REMOVE) {
1268 log_error("invalid PROGRAM operation");
1271 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1275 if (streq(key, "RESULT")) {
1276 if (op > OP_MATCH_MAX) {
1277 log_error("invalid RESULT operation");
1280 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1284 if (startswith(key, "IMPORT")) {
1285 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
1287 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
1290 if (op == OP_REMOVE) {
1291 log_error("invalid IMPORT operation");
1294 if (streq(attr, "program")) {
1295 /* find known built-in command */
1296 if (value[0] != '/') {
1297 enum udev_builtin_cmd cmd;
1299 cmd = udev_builtin_lookup(value);
1300 if (cmd < UDEV_BUILTIN_MAX) {
1301 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1302 value, filename, lineno);
1303 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1307 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1308 } else if (streq(attr, "builtin")) {
1309 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1311 if (cmd < UDEV_BUILTIN_MAX)
1312 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1314 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1315 } else if (streq(attr, "file")) {
1316 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1317 } else if (streq(attr, "db")) {
1318 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1319 } else if (streq(attr, "cmdline")) {
1320 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1321 } else if (streq(attr, "parent")) {
1322 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1324 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
1328 if (startswith(key, "TEST")) {
1331 if (op > OP_MATCH_MAX) {
1332 log_error("invalid TEST operation");
1335 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
1337 mode = strtol(attr, NULL, 8);
1338 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1340 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1345 if (startswith(key, "RUN")) {
1346 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
1349 if (op == OP_REMOVE) {
1350 log_error("invalid RUN operation");
1354 if (streq(attr, "builtin")) {
1355 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1357 if (cmd < UDEV_BUILTIN_MAX)
1358 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1360 log_error("RUN{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1361 } else if (streq(attr, "program")) {
1362 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1364 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1366 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
1372 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1373 if (op == OP_REMOVE) {
1374 log_error("invalid WAIT_FOR/WAIT_FOR_SYSFS operation");
1377 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1381 if (streq(key, "LABEL")) {
1382 if (op == OP_REMOVE) {
1383 log_error("invalid LABEL operation");
1386 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1390 if (streq(key, "GOTO")) {
1391 if (op == OP_REMOVE) {
1392 log_error("invalid GOTO operation");
1395 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1399 if (startswith(key, "NAME")) {
1400 if (op == OP_REMOVE) {
1401 log_error("invalid NAME operation");
1404 if (op < OP_MATCH_MAX) {
1405 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1407 if (streq(value, "%k")) {
1408 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1409 "please remove it from %s:%u\n", filename, lineno);
1412 if (value[0] == '\0') {
1413 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1414 "please remove it from %s:%u\n", filename, lineno);
1417 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1419 rule_tmp.rule.rule.can_set_name = true;
1423 if (streq(key, "SYMLINK")) {
1424 if (op == OP_REMOVE) {
1425 log_error("invalid SYMLINK operation");
1428 if (op < OP_MATCH_MAX)
1429 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1431 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1432 rule_tmp.rule.rule.can_set_name = true;
1436 if (streq(key, "OWNER")) {
1440 if (op == OP_REMOVE) {
1441 log_error("invalid OWNER operation");
1445 uid = strtoul(value, &endptr, 10);
1446 if (endptr[0] == '\0') {
1447 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1448 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1449 uid = add_uid(rules, value);
1450 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1451 } else if (rules->resolve_names >= 0) {
1452 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1454 rule_tmp.rule.rule.can_set_name = true;
1458 if (streq(key, "GROUP")) {
1462 if (op == OP_REMOVE) {
1463 log_error("invalid GROUP operation");
1467 gid = strtoul(value, &endptr, 10);
1468 if (endptr[0] == '\0') {
1469 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1470 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1471 gid = add_gid(rules, value);
1472 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1473 } else if (rules->resolve_names >= 0) {
1474 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1476 rule_tmp.rule.rule.can_set_name = true;
1480 if (streq(key, "MODE")) {
1484 if (op == OP_REMOVE) {
1485 log_error("invalid MODE operation");
1489 mode = strtol(value, &endptr, 8);
1490 if (endptr[0] == '\0')
1491 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1493 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1494 rule_tmp.rule.rule.can_set_name = true;
1498 if (streq(key, "OPTIONS")) {
1501 if (op == OP_REMOVE) {
1502 log_error("invalid OPTIONS operation");
1506 pos = strstr(value, "link_priority=");
1508 int prio = atoi(&pos[strlen("link_priority=")]);
1510 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1513 pos = strstr(value, "string_escape=");
1515 pos = &pos[strlen("string_escape=")];
1516 if (startswith(pos, "none"))
1517 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1518 else if (startswith(pos, "replace"))
1519 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1522 pos = strstr(value, "db_persist");
1524 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1526 pos = strstr(value, "nowatch");
1530 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1532 pos = strstr(value, "watch");
1536 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1540 pos = strstr(value, "static_node=");
1542 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1543 rule_tmp.rule.rule.has_static_node = true;
1549 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
1553 /* add rule token */
1554 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1555 if (add_token(rules, &rule_tmp.rule) != 0)
1558 /* add tokens to list, sorted by type */
1559 if (sort_token(rules, &rule_tmp) != 0)
1564 log_error("invalid rule '%s:%u'", filename, lineno);
1568 static int parse_file(struct udev_rules *rules, const char *filename) {
1569 _cleanup_fclose_ FILE *f = NULL;
1570 unsigned int first_token;
1571 unsigned int filename_off;
1572 char line[UTIL_LINE_SIZE];
1576 f = fopen(filename, "re");
1578 if (errno == ENOENT)
1584 if (null_or_empty_fd(fileno(f))) {
1585 log_debug("Skipping empty file: %s", filename);
1588 log_debug("Reading rules file: %s", filename);
1590 first_token = rules->token_cur;
1591 filename_off = rules_add_string(rules, filename);
1593 while (fgets(line, sizeof(line), f) != NULL) {
1597 /* skip whitespace */
1600 while (isspace(key[0]))
1611 /* continue reading if backslash+newline is found */
1612 while (line[len-2] == '\\') {
1613 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1615 if (strlen(&line[len-2]) < 2)
1621 if (len+1 >= sizeof(line)) {
1622 log_error("line too long '%s':%u, ignored", filename, line_nr);
1625 add_rule(rules, key, filename, filename_off, line_nr);
1628 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1629 for (i = first_token+1; i < rules->token_cur; i++) {
1630 if (rules->tokens[i].type == TK_A_GOTO) {
1631 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1634 for (j = i+1; j < rules->token_cur; j++) {
1635 if (rules->tokens[j].type != TK_RULE)
1637 if (rules->tokens[j].rule.label_off == 0)
1639 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1641 rules->tokens[i].key.rule_goto = j;
1644 if (rules->tokens[i].key.rule_goto == 0)
1645 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
1651 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) {
1652 struct udev_rules *rules;
1653 struct udev_list file_list;
1654 struct token end_token;
1658 rules = new0(struct udev_rules, 1);
1662 rules->resolve_names = resolve_names;
1663 udev_list_init(udev, &file_list, true);
1665 /* init token array and string buffer */
1666 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1667 if (rules->tokens == NULL)
1668 return udev_rules_unref(rules);
1669 rules->token_max = PREALLOC_TOKEN;
1671 rules->strbuf = strbuf_new();
1673 return udev_rules_unref(rules);
1675 udev_rules_check_timestamp(rules);
1677 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
1679 log_error_errno(r, "failed to enumerate rules files: %m");
1680 return udev_rules_unref(rules);
1684 * The offset value in the rules strct is limited; add all
1685 * rules file names to the beginning of the string buffer.
1687 STRV_FOREACH(f, files)
1688 rules_add_string(rules, *f);
1690 STRV_FOREACH(f, files)
1691 parse_file(rules, *f);
1695 memzero(&end_token, sizeof(struct token));
1696 end_token.type = TK_END;
1697 add_token(rules, &end_token);
1698 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1699 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1701 /* cleanup temporary strbuf data */
1702 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1703 rules->strbuf->in_count, rules->strbuf->in_len,
1704 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1705 strbuf_complete(rules->strbuf);
1707 /* cleanup uid/gid cache */
1710 rules->uids_cur = 0;
1711 rules->uids_max = 0;
1714 rules->gids_cur = 0;
1715 rules->gids_max = 0;
1721 struct udev_rules *udev_rules_unref(struct udev_rules *rules) {
1724 free(rules->tokens);
1725 strbuf_cleanup(rules->strbuf);
1732 bool udev_rules_check_timestamp(struct udev_rules *rules) {
1736 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
1739 static int match_key(struct udev_rules *rules, struct token *token, const char *val) {
1740 char *key_value = rules_str(rules, token->key.value_off);
1747 switch (token->key.glob) {
1749 match = (streq(key_value, val));
1752 match = (fnmatch(key_value, val, 0) == 0);
1759 s = rules_str(rules, token->key.value_off);
1764 next = strchr(s, '|');
1766 size_t matchlen = (size_t)(next - s);
1768 match = (matchlen == len && strneq(s, val, matchlen));
1772 match = (streq(s, val));
1781 char value[UTIL_PATH_SIZE];
1783 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1785 while (key_value != NULL) {
1786 pos = strchr(key_value, '|');
1791 match = (fnmatch(key_value, val, 0) == 0);
1799 match = (val[0] != '\0');
1805 if (match && (token->key.op == OP_MATCH))
1807 if (!match && (token->key.op == OP_NOMATCH))
1812 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) {
1814 char nbuf[UTIL_NAME_SIZE];
1816 char vbuf[UTIL_NAME_SIZE];
1819 name = rules_str(rules, cur->key.attr_off);
1820 switch (cur->key.attrsubst) {
1822 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1826 value = udev_device_get_sysattr_value(dev, name);
1831 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1839 /* remove trailing whitespace, if not asked to match for it */
1840 len = strlen(value);
1841 if (len > 0 && isspace(value[len-1])) {
1842 const char *key_value;
1845 key_value = rules_str(rules, cur->key.value_off);
1846 klen = strlen(key_value);
1847 if (klen > 0 && !isspace(key_value[klen-1])) {
1848 if (value != vbuf) {
1849 strscpy(vbuf, sizeof(vbuf), value);
1852 while (len > 0 && isspace(vbuf[--len]))
1857 return match_key(rules, cur, value);
1866 int udev_rules_apply_to_event(struct udev_rules *rules,
1867 struct udev_event *event,
1868 usec_t timeout_usec,
1869 usec_t timeout_warn_usec,
1870 struct udev_list *properties_list,
1871 const sigset_t *sigmask) {
1874 enum escape_type esc = ESCAPE_UNSET;
1877 if (rules->tokens == NULL)
1880 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1881 (major(udev_device_get_devnum(event->dev)) > 0 ||
1882 udev_device_get_ifindex(event->dev) > 0));
1884 /* loop through token list, match, run actions or forward to next rule */
1885 cur = &rules->tokens[0];
1888 dump_token(rules, cur);
1889 switch (cur->type) {
1893 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1894 if (!can_set_name && rule->rule.can_set_name)
1899 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1903 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1907 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1910 case TK_M_DEVLINK: {
1911 struct udev_list_entry *list_entry;
1914 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1915 const char *devlink;
1917 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1918 if (match_key(rules, cur, devlink) == 0) {
1928 if (match_key(rules, cur, event->name) != 0)
1932 const char *key_name = rules_str(rules, cur->key.attr_off);
1935 value = udev_device_get_property_value(event->dev, key_name);
1937 /* check global properties */
1938 if (!value && properties_list) {
1939 struct udev_list_entry *list_entry;
1941 list_entry = udev_list_get_entry(properties_list);
1942 list_entry = udev_list_entry_get_by_name(list_entry, key_name);
1943 if (list_entry != NULL)
1944 value = udev_list_entry_get_value(list_entry);
1949 if (match_key(rules, cur, value))
1954 struct udev_list_entry *list_entry;
1957 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1958 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1963 if (!match && (cur->key.op != OP_NOMATCH))
1967 case TK_M_SUBSYSTEM:
1968 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1972 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1975 case TK_M_WAITFOR: {
1976 char filename[UTIL_PATH_SIZE];
1979 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1980 found = (wait_for_file(event->dev, filename, 10) == 0);
1981 if (!found && (cur->key.op != OP_NOMATCH))
1986 if (match_attr(rules, event->dev, event, cur) != 0)
1990 case TK_M_SUBSYSTEMS:
1996 /* get whole sequence of parent matches */
1998 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2001 /* loop over parents */
2002 event->dev_parent = event->dev;
2006 /* loop over sequence of parent match keys */
2007 for (key = cur; key < next; key++ ) {
2008 dump_token(rules, key);
2011 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2014 case TK_M_SUBSYSTEMS:
2015 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2019 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2023 if (match_attr(rules, event->dev_parent, event, key) != 0)
2027 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
2029 if (match && key->key.op == OP_NOMATCH)
2031 if (!match && key->key.op == OP_MATCH)
2042 event->dev_parent = udev_device_get_parent(event->dev_parent);
2043 if (event->dev_parent == NULL)
2046 /* move behind our sequence of parent match keys */
2051 char filename[UTIL_PATH_SIZE];
2052 struct stat statbuf;
2055 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
2056 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2057 if (filename[0] != '/') {
2058 char tmp[UTIL_PATH_SIZE];
2060 strscpy(tmp, sizeof(tmp), filename);
2061 strscpyl(filename, sizeof(filename),
2062 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2065 attr_subst_subdir(filename, sizeof(filename));
2067 match = (stat(filename, &statbuf) == 0);
2068 if (match && cur->key.mode > 0)
2069 match = ((statbuf.st_mode & cur->key.mode) > 0);
2070 if (match && cur->key.op == OP_NOMATCH)
2072 if (!match && cur->key.op == OP_MATCH)
2076 case TK_M_PROGRAM: {
2077 char program[UTIL_PATH_SIZE];
2079 char result[UTIL_LINE_SIZE];
2081 free(event->program_result);
2082 event->program_result = NULL;
2083 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2084 envp = udev_device_get_properties_envp(event->dev);
2085 log_debug("PROGRAM '%s' %s:%u",
2087 rules_str(rules, rule->rule.filename_off),
2088 rule->rule.filename_line);
2090 if (udev_event_spawn(event, timeout_usec, timeout_warn_usec, program, envp, sigmask, result, sizeof(result)) < 0) {
2091 if (cur->key.op != OP_NOMATCH)
2096 util_remove_trailing_chars(result, '\n');
2097 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2098 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2100 log_debug("%i character(s) replaced" , count);
2102 event->program_result = strdup(result);
2103 if (cur->key.op == OP_NOMATCH)
2108 case TK_M_IMPORT_FILE: {
2109 char import[UTIL_PATH_SIZE];
2111 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2112 if (import_file_into_properties(event->dev, import) != 0)
2113 if (cur->key.op != OP_NOMATCH)
2117 case TK_M_IMPORT_PROG: {
2118 char import[UTIL_PATH_SIZE];
2120 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2121 log_debug("IMPORT '%s' %s:%u",
2123 rules_str(rules, rule->rule.filename_off),
2124 rule->rule.filename_line);
2126 if (import_program_into_properties(event, timeout_usec, timeout_warn_usec, import, sigmask) != 0)
2127 if (cur->key.op != OP_NOMATCH)
2131 case TK_M_IMPORT_BUILTIN: {
2132 char command[UTIL_PATH_SIZE];
2134 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2135 /* check if we ran already */
2136 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2137 log_debug("IMPORT builtin skip '%s' %s:%u",
2138 udev_builtin_name(cur->key.builtin_cmd),
2139 rules_str(rules, rule->rule.filename_off),
2140 rule->rule.filename_line);
2141 /* return the result from earlier run */
2142 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2143 if (cur->key.op != OP_NOMATCH)
2148 event->builtin_run |= (1 << cur->key.builtin_cmd);
2151 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2152 log_debug("IMPORT builtin '%s' %s:%u",
2153 udev_builtin_name(cur->key.builtin_cmd),
2154 rules_str(rules, rule->rule.filename_off),
2155 rule->rule.filename_line);
2157 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2158 /* remember failure */
2159 log_debug("IMPORT builtin '%s' returned non-zero",
2160 udev_builtin_name(cur->key.builtin_cmd));
2161 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2162 if (cur->key.op != OP_NOMATCH)
2167 case TK_M_IMPORT_DB: {
2168 const char *key = rules_str(rules, cur->key.value_off);
2171 value = udev_device_get_property_value(event->dev_db, key);
2173 udev_device_add_property(event->dev, key, value);
2175 if (cur->key.op != OP_NOMATCH)
2180 case TK_M_IMPORT_CMDLINE: {
2182 bool imported = false;
2184 f = fopen("/proc/cmdline", "re");
2188 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2189 const char *key = rules_str(rules, cur->key.value_off);
2192 pos = strstr(cmdline, key);
2195 if (pos[0] == '\0' || isspace(pos[0])) {
2196 /* we import simple flags as 'FLAG=1' */
2197 udev_device_add_property(event->dev, key, "1");
2199 } else if (pos[0] == '=') {
2204 while (pos[0] != '\0' && !isspace(pos[0]))
2207 udev_device_add_property(event->dev, key, value);
2214 if (!imported && cur->key.op != OP_NOMATCH)
2218 case TK_M_IMPORT_PARENT: {
2219 char import[UTIL_PATH_SIZE];
2221 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2222 if (import_parent_into_properties(event->dev, import) != 0)
2223 if (cur->key.op != OP_NOMATCH)
2228 if (match_key(rules, cur, event->program_result) != 0)
2231 case TK_A_STRING_ESCAPE_NONE:
2234 case TK_A_STRING_ESCAPE_REPLACE:
2235 esc = ESCAPE_REPLACE;
2237 case TK_A_DB_PERSIST:
2238 udev_device_set_db_persist(event->dev);
2240 case TK_A_INOTIFY_WATCH:
2241 if (event->inotify_watch_final)
2243 if (cur->key.op == OP_ASSIGN_FINAL)
2244 event->inotify_watch_final = true;
2245 event->inotify_watch = cur->key.watch;
2247 case TK_A_DEVLINK_PRIO:
2248 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2251 char owner[UTIL_NAME_SIZE];
2252 const char *ow = owner;
2255 if (event->owner_final)
2257 if (cur->key.op == OP_ASSIGN_FINAL)
2258 event->owner_final = true;
2259 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2260 event->owner_set = true;
2261 r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL);
2263 if (r == -ENOENT || r == -ESRCH)
2264 log_error("specified user '%s' unknown", owner);
2266 log_error_errno(r, "error resolving user '%s': %m", owner);
2270 log_debug("OWNER %u %s:%u",
2272 rules_str(rules, rule->rule.filename_off),
2273 rule->rule.filename_line);
2277 char group[UTIL_NAME_SIZE];
2278 const char *gr = group;
2281 if (event->group_final)
2283 if (cur->key.op == OP_ASSIGN_FINAL)
2284 event->group_final = true;
2285 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2286 event->group_set = true;
2287 r = get_group_creds(&gr, &event->gid);
2289 if (r == -ENOENT || r == -ESRCH)
2290 log_error("specified group '%s' unknown", group);
2292 log_error_errno(r, "error resolving group '%s': %m", group);
2296 log_debug("GROUP %u %s:%u",
2298 rules_str(rules, rule->rule.filename_off),
2299 rule->rule.filename_line);
2303 char mode_str[UTIL_NAME_SIZE];
2307 if (event->mode_final)
2309 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2310 mode = strtol(mode_str, &endptr, 8);
2311 if (endptr[0] != '\0') {
2312 log_error("ignoring invalid mode '%s'", mode_str);
2315 if (cur->key.op == OP_ASSIGN_FINAL)
2316 event->mode_final = true;
2317 event->mode_set = true;
2319 log_debug("MODE %#o %s:%u",
2321 rules_str(rules, rule->rule.filename_off),
2322 rule->rule.filename_line);
2326 if (event->owner_final)
2328 if (cur->key.op == OP_ASSIGN_FINAL)
2329 event->owner_final = true;
2330 event->owner_set = true;
2331 event->uid = cur->key.uid;
2332 log_debug("OWNER %u %s:%u",
2334 rules_str(rules, rule->rule.filename_off),
2335 rule->rule.filename_line);
2338 if (event->group_final)
2340 if (cur->key.op == OP_ASSIGN_FINAL)
2341 event->group_final = true;
2342 event->group_set = true;
2343 event->gid = cur->key.gid;
2344 log_debug("GROUP %u %s:%u",
2346 rules_str(rules, rule->rule.filename_off),
2347 rule->rule.filename_line);
2350 if (event->mode_final)
2352 if (cur->key.op == OP_ASSIGN_FINAL)
2353 event->mode_final = true;
2354 event->mode_set = true;
2355 event->mode = cur->key.mode;
2356 log_debug("MODE %#o %s:%u",
2358 rules_str(rules, rule->rule.filename_off),
2359 rule->rule.filename_line);
2361 case TK_A_SECLABEL: {
2362 const char *name, *label;
2364 name = rules_str(rules, cur->key.attr_off);
2365 label = rules_str(rules, cur->key.value_off);
2366 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2367 udev_list_cleanup(&event->seclabel_list);
2368 udev_list_entry_add(&event->seclabel_list, name, label);
2369 log_debug("SECLABEL{%s}='%s' %s:%u",
2371 rules_str(rules, rule->rule.filename_off),
2372 rule->rule.filename_line);
2376 const char *name = rules_str(rules, cur->key.attr_off);
2377 char *value = rules_str(rules, cur->key.value_off);
2378 char value_new[UTIL_NAME_SIZE];
2379 const char *value_old = NULL;
2381 if (value[0] == '\0') {
2382 if (cur->key.op == OP_ADD)
2384 udev_device_add_property(event->dev, name, NULL);
2388 if (cur->key.op == OP_ADD)
2389 value_old = udev_device_get_property_value(event->dev, name);
2391 char temp[UTIL_NAME_SIZE];
2393 /* append value separated by space */
2394 udev_event_apply_format(event, value, temp, sizeof(temp));
2395 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2397 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2399 udev_device_add_property(event->dev, name, value_new);
2403 char tag[UTIL_PATH_SIZE];
2406 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2407 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2408 udev_device_cleanup_tags_list(event->dev);
2409 for (p = tag; *p != '\0'; p++) {
2410 if ((*p >= 'a' && *p <= 'z') ||
2411 (*p >= 'A' && *p <= 'Z') ||
2412 (*p >= '0' && *p <= '9') ||
2413 *p == '-' || *p == '_')
2415 log_error("ignoring invalid tag name '%s'", tag);
2418 if (cur->key.op == OP_REMOVE)
2419 udev_device_remove_tag(event->dev, tag);
2421 udev_device_add_tag(event->dev, tag);
2425 const char *name = rules_str(rules, cur->key.value_off);
2427 char name_str[UTIL_PATH_SIZE];
2430 if (event->name_final)
2432 if (cur->key.op == OP_ASSIGN_FINAL)
2433 event->name_final = true;
2434 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2435 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2436 count = util_replace_chars(name_str, "/");
2438 log_debug("%i character(s) replaced", count);
2440 if (major(udev_device_get_devnum(event->dev)) &&
2441 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2442 log_error("NAME=\"%s\" ignored, kernel device nodes "
2443 "can not be renamed; please fix it in %s:%u\n", name,
2444 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2448 event->name = strdup(name_str);
2449 log_debug("NAME '%s' %s:%u",
2451 rules_str(rules, rule->rule.filename_off),
2452 rule->rule.filename_line);
2455 case TK_A_DEVLINK: {
2456 char temp[UTIL_PATH_SIZE];
2457 char filename[UTIL_PATH_SIZE];
2461 if (event->devlink_final)
2463 if (major(udev_device_get_devnum(event->dev)) == 0)
2465 if (cur->key.op == OP_ASSIGN_FINAL)
2466 event->devlink_final = true;
2467 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2468 udev_device_cleanup_devlinks_list(event->dev);
2470 /* allow multiple symlinks separated by spaces */
2471 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2472 if (esc == ESCAPE_UNSET)
2473 count = util_replace_chars(temp, "/ ");
2474 else if (esc == ESCAPE_REPLACE)
2475 count = util_replace_chars(temp, "/");
2477 log_debug("%i character(s) replaced" , count);
2479 while (isspace(pos[0]))
2481 next = strchr(pos, ' ');
2482 while (next != NULL) {
2484 log_debug("LINK '%s' %s:%u", pos,
2485 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2486 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2487 udev_device_add_devlink(event->dev, filename);
2488 while (isspace(next[1]))
2491 next = strchr(pos, ' ');
2493 if (pos[0] != '\0') {
2494 log_debug("LINK '%s' %s:%u", pos,
2495 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2496 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2497 udev_device_add_devlink(event->dev, filename);
2502 const char *key_name = rules_str(rules, cur->key.attr_off);
2503 char attr[UTIL_PATH_SIZE];
2504 char value[UTIL_NAME_SIZE];
2507 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2508 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2509 attr_subst_subdir(attr, sizeof(attr));
2511 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2512 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
2513 rules_str(rules, rule->rule.filename_off),
2514 rule->rule.filename_line);
2515 f = fopen(attr, "we");
2517 if (fprintf(f, "%s", value) <= 0)
2518 log_error_errno(errno, "error writing ATTR{%s}: %m", attr);
2521 log_error_errno(errno, "error opening ATTR{%s} for writing: %m", attr);
2525 case TK_A_RUN_BUILTIN:
2526 case TK_A_RUN_PROGRAM: {
2527 struct udev_list_entry *entry;
2529 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2530 udev_list_cleanup(&event->run_list);
2531 log_debug("RUN '%s' %s:%u",
2532 rules_str(rules, cur->key.value_off),
2533 rules_str(rules, rule->rule.filename_off),
2534 rule->rule.filename_line);
2535 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2536 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2540 if (cur->key.rule_goto == 0)
2542 cur = &rules->tokens[cur->key.rule_goto];
2547 case TK_M_PARENTS_MIN:
2548 case TK_M_PARENTS_MAX:
2551 log_error("wrong type %u", cur->type);
2558 /* fast-forward to next rule */
2559 cur = rule + rule->rule.token_count;
2563 int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
2569 _cleanup_strv_free_ char **tags = NULL;
2572 _cleanup_free_ char *path = NULL;
2575 if (rules->tokens == NULL)
2578 cur = &rules->tokens[0];
2581 switch (cur->type) {
2586 /* skip rules without a static_node tag */
2587 if (!rule->rule.has_static_node)
2603 mode = cur->key.mode;
2606 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2611 case TK_A_STATIC_NODE: {
2612 char device_node[UTIL_PATH_SIZE];
2613 char tags_dir[UTIL_PATH_SIZE];
2614 char tag_symlink[UTIL_PATH_SIZE];
2617 /* we assure, that the permissions tokens are sorted before the static token */
2619 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2622 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2623 if (stat(device_node, &stats) != 0)
2625 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2628 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2630 STRV_FOREACH(t, tags) {
2631 _cleanup_free_ char *unescaped_filename = NULL;
2633 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2634 r = mkdir_p(tags_dir, 0755);
2636 return log_error_errno(r, "failed to create %s: %m", tags_dir);
2638 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2640 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2641 r = symlink(device_node, tag_symlink);
2642 if (r < 0 && errno != EEXIST)
2643 return log_error_errno(errno, "failed to create symlink %s -> %s: %m",
2644 tag_symlink, device_node);
2650 /* don't touch the permissions if only the tags were set */
2651 if (mode == 0 && uid == 0 && gid == 0)
2660 if (mode != (stats.st_mode & 01777)) {
2661 r = chmod(device_node, mode);
2663 log_error("failed to chmod '%s' %#o", device_node, mode);
2666 log_debug("chmod '%s' %#o", device_node, mode);
2669 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2670 r = chown(device_node, uid, gid);
2672 log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
2675 log_debug("chown '%s' %u %u", device_node, uid, gid);
2678 utimensat(AT_FDCWD, device_node, NULL, 0);
2688 /* fast-forward to next rule */
2689 cur = rule + rule->rule.token_count;
2696 fchmod(fileno(f), 0644);
2697 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2699 unlink("/run/udev/static_node-tags");