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;
55 /* every key in the rules file becomes a token */
57 unsigned int token_cur;
58 unsigned int token_max;
60 /* all key strings are copied and de-duplicated in a single continuous string buffer */
61 struct strbuf *strbuf;
63 /* during rule parsing, uid/gid lookup results are cached */
65 unsigned int uids_cur;
66 unsigned int uids_max;
68 unsigned int gids_cur;
69 unsigned int gids_max;
72 static char *rules_str(struct udev_rules *rules, unsigned int off) {
73 return rules->strbuf->buf + off;
76 static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
77 return strbuf_add_string(rules->strbuf, s, strlen(s));
80 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
93 enum string_glob_type {
95 GL_PLAIN, /* no special chars */
96 GL_GLOB, /* shell globs ?,*,[] */
97 GL_SPLIT, /* multi-value A|B */
98 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
99 GL_SOMETHING, /* commonly used "?*" */
102 enum string_subst_type {
109 /* tokens of a rule are sorted/handled in this order */
114 TK_M_ACTION, /* val */
115 TK_M_DEVPATH, /* val */
116 TK_M_KERNEL, /* val */
117 TK_M_DEVLINK, /* val */
119 TK_M_ENV, /* val, attr */
121 TK_M_SUBSYSTEM, /* val */
122 TK_M_DRIVER, /* val */
123 TK_M_WAITFOR, /* val */
124 TK_M_ATTR, /* val, attr */
127 TK_M_KERNELS, /* val */
128 TK_M_SUBSYSTEMS, /* val */
129 TK_M_DRIVERS, /* val */
130 TK_M_ATTRS, /* val, attr */
134 TK_M_TEST, /* val, mode_t */
135 TK_M_EVENT_TIMEOUT, /* int */
136 TK_M_PROGRAM, /* val */
137 TK_M_IMPORT_FILE, /* val */
138 TK_M_IMPORT_PROG, /* val */
139 TK_M_IMPORT_BUILTIN, /* val */
140 TK_M_IMPORT_DB, /* val */
141 TK_M_IMPORT_CMDLINE, /* val */
142 TK_M_IMPORT_PARENT, /* val */
143 TK_M_RESULT, /* val */
146 TK_A_STRING_ESCAPE_NONE,
147 TK_A_STRING_ESCAPE_REPLACE,
149 TK_A_INOTIFY_WATCH, /* int */
150 TK_A_DEVLINK_PRIO, /* int */
151 TK_A_OWNER, /* val */
152 TK_A_GROUP, /* val */
154 TK_A_OWNER_ID, /* uid_t */
155 TK_A_GROUP_ID, /* gid_t */
156 TK_A_MODE_ID, /* mode_t */
158 TK_A_STATIC_NODE, /* val */
159 TK_A_SECLABEL, /* val, attr */
160 TK_A_ENV, /* val, attr */
162 TK_A_DEVLINK, /* val */
163 TK_A_ATTR, /* val, attr */
164 TK_A_RUN_BUILTIN, /* val, bool */
165 TK_A_RUN_PROGRAM, /* val, bool */
166 TK_A_GOTO, /* size_t */
171 /* we try to pack stuff in a way that we take only 12 bytes per token */
174 unsigned char type; /* same in rule and key */
176 enum token_type type:8;
178 bool has_static_node:1;
179 unsigned int unused:6;
180 unsigned short token_count;
181 unsigned int label_off;
182 unsigned short filename_off;
183 unsigned short filename_line;
186 enum token_type type:8;
187 enum operation_type op:8;
188 enum string_glob_type glob:8;
189 enum string_subst_type subst:4;
190 enum string_subst_type attrsubst:4;
191 unsigned int value_off;
193 unsigned int attr_off;
194 unsigned int rule_goto;
201 enum udev_builtin_cmd builtin_cmd;
209 struct udev_rules *rules;
211 struct token token[MAX_TK];
212 unsigned int token_cur;
216 static const char *operation_str(enum operation_type type)
218 static const char *operation_strs[] = {
219 [OP_UNSET] = "UNSET",
220 [OP_MATCH] = "match",
221 [OP_NOMATCH] = "nomatch",
222 [OP_MATCH_MAX] = "MATCH_MAX",
225 [OP_ASSIGN] = "assign",
226 [OP_ASSIGN_FINAL] = "assign-final",
229 return operation_strs[type];
232 static const char *string_glob_str(enum string_glob_type type)
234 static const char *string_glob_strs[] = {
235 [GL_UNSET] = "UNSET",
236 [GL_PLAIN] = "plain",
238 [GL_SPLIT] = "split",
239 [GL_SPLIT_GLOB] = "split-glob",
240 [GL_SOMETHING] = "split-glob",
243 return string_glob_strs[type];
246 static const char *token_str(enum token_type type)
248 static const char *token_strs[] = {
249 [TK_UNSET] = "UNSET",
252 [TK_M_ACTION] = "M ACTION",
253 [TK_M_DEVPATH] = "M DEVPATH",
254 [TK_M_KERNEL] = "M KERNEL",
255 [TK_M_DEVLINK] = "M DEVLINK",
256 [TK_M_NAME] = "M NAME",
257 [TK_M_ENV] = "M ENV",
258 [TK_M_TAG] = "M TAG",
259 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
260 [TK_M_DRIVER] = "M DRIVER",
261 [TK_M_WAITFOR] = "M WAITFOR",
262 [TK_M_ATTR] = "M ATTR",
264 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
265 [TK_M_KERNELS] = "M KERNELS",
266 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
267 [TK_M_DRIVERS] = "M DRIVERS",
268 [TK_M_ATTRS] = "M ATTRS",
269 [TK_M_TAGS] = "M TAGS",
270 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
272 [TK_M_TEST] = "M TEST",
273 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
274 [TK_M_PROGRAM] = "M PROGRAM",
275 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
276 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
277 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
278 [TK_M_IMPORT_DB] = "M IMPORT_DB",
279 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
280 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
281 [TK_M_RESULT] = "M RESULT",
282 [TK_M_MAX] = "M MAX",
284 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
285 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
286 [TK_A_DB_PERSIST] = "A DB_PERSIST",
287 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
288 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
289 [TK_A_OWNER] = "A OWNER",
290 [TK_A_GROUP] = "A GROUP",
291 [TK_A_MODE] = "A MODE",
292 [TK_A_OWNER_ID] = "A OWNER_ID",
293 [TK_A_GROUP_ID] = "A GROUP_ID",
294 [TK_A_STATIC_NODE] = "A STATIC_NODE",
295 [TK_A_SECLABEL] = "A SECLABEL",
296 [TK_A_MODE_ID] = "A MODE_ID",
297 [TK_A_ENV] = "A ENV",
298 [TK_A_TAG] = "A ENV",
299 [TK_A_NAME] = "A NAME",
300 [TK_A_DEVLINK] = "A DEVLINK",
301 [TK_A_ATTR] = "A ATTR",
302 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
303 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
304 [TK_A_GOTO] = "A GOTO",
309 return token_strs[type];
312 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'\n",
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)\n",
360 token_str(type), operation_str(op), value, string_glob_str(glob));
362 case TK_M_IMPORT_BUILTIN:
363 log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
370 log_debug("%s %s '%s' '%s'(%s)\n",
371 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
375 log_debug("%s %s '%s'\n", 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\n", token_str(type));
383 log_debug("%s %s '%s'(%s) %#o\n",
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\n", token_str(type), token->key.watch);
389 case TK_A_DEVLINK_PRIO:
390 log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
393 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
396 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
399 log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
401 case TK_A_STATIC_NODE:
402 log_debug("%s '%s'\n", token_str(type), value);
405 log_debug("%s %s '%s' '%s'\n", token_str(type), operation_str(op), attr, value);
407 case TK_M_EVENT_TIMEOUT:
408 log_debug("%s %u\n", token_str(type), token->key.event_timeout);
411 log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
414 log_debug("* %s\n", token_str(type));
416 case TK_M_PARENTS_MIN:
417 case TK_M_PARENTS_MAX:
420 log_debug("unknown type %u\n", type);
425 static void dump_rules(struct udev_rules *rules)
429 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
431 rules->token_cur * sizeof(struct token),
434 for(i = 0; i < rules->token_cur; i++)
435 dump_token(rules, &rules->tokens[i]);
438 static inline const char *operation_str(enum operation_type type) { return NULL; }
439 static inline const char *token_str(enum token_type type) { return NULL; }
440 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
441 static inline void dump_rules(struct udev_rules *rules) {}
444 static int add_token(struct udev_rules *rules, struct token *token)
446 /* grow buffer if needed */
447 if (rules->token_cur+1 >= rules->token_max) {
448 struct token *tokens;
451 /* double the buffer size */
452 add = rules->token_max;
456 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
459 rules->tokens = tokens;
460 rules->token_max += add;
462 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
467 static uid_t add_uid(struct udev_rules *rules, const char *owner)
473 /* lookup, if we know it already */
474 for (i = 0; i < rules->uids_cur; i++) {
475 off = rules->uids[i].name_off;
476 if (streq(rules_str(rules, off), owner)) {
477 uid = rules->uids[i].uid;
481 uid = util_lookup_user(rules->udev, owner);
483 /* grow buffer if needed */
484 if (rules->uids_cur+1 >= rules->uids_max) {
485 struct uid_gid *uids;
488 /* double the buffer size */
489 add = rules->uids_max;
493 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
497 rules->uids_max += add;
499 rules->uids[rules->uids_cur].uid = uid;
500 off = rules_add_string(rules, owner);
503 rules->uids[rules->uids_cur].name_off = off;
508 static gid_t add_gid(struct udev_rules *rules, const char *group)
514 /* lookup, if we know it already */
515 for (i = 0; i < rules->gids_cur; i++) {
516 off = rules->gids[i].name_off;
517 if (streq(rules_str(rules, off), group)) {
518 gid = rules->gids[i].gid;
522 gid = util_lookup_group(rules->udev, group);
524 /* grow buffer if needed */
525 if (rules->gids_cur+1 >= rules->gids_max) {
526 struct uid_gid *gids;
529 /* double the buffer size */
530 add = rules->gids_max;
534 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
538 rules->gids_max += add;
540 rules->gids[rules->gids_cur].gid = gid;
541 off = rules_add_string(rules, group);
544 rules->gids[rules->gids_cur].name_off = off;
549 static int import_property_from_string(struct udev_device *dev, char *line)
554 struct udev_list_entry *entry;
558 while (isspace(key[0]))
561 /* comment or empty line */
562 if (key[0] == '#' || key[0] == '\0')
565 /* split key/value */
566 val = strchr(key, '=');
573 while (isspace(val[0]))
580 while (isspace(key[len-1]))
584 /* terminate value */
588 while (isspace(val[len-1]))
596 if (val[0] == '"' || val[0] == '\'') {
597 if (val[len-1] != val[0]) {
598 log_debug("inconsistent quoting: '%s', skip\n", line);
605 entry = udev_device_add_property(dev, key, val);
606 /* store in db, skip private keys */
608 udev_list_entry_set_num(entry, true);
613 static int import_file_into_properties(struct udev_device *dev, const char *filename)
616 char line[UTIL_LINE_SIZE];
618 f = fopen(filename, "re");
621 while (fgets(line, sizeof(line), f) != NULL)
622 import_property_from_string(dev, line);
627 static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
629 struct udev_device *dev = event->dev;
631 char result[UTIL_LINE_SIZE];
635 envp = udev_device_get_properties_envp(dev);
636 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
641 while (line != NULL) {
644 pos = strchr(line, '\n');
649 import_property_from_string(dev, line);
655 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 struct udev_list_entry *entry;
671 entry = udev_device_add_property(dev, key, val);
672 /* store in db, skip private keys */
674 udev_list_entry_set_num(entry, true);
680 #define WAIT_LOOP_PER_SECOND 50
681 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
683 char filepath[UTIL_PATH_SIZE];
684 char devicepath[UTIL_PATH_SIZE];
686 int loop = timeout * WAIT_LOOP_PER_SECOND;
688 /* a relative path is a device attribute */
689 devicepath[0] = '\0';
690 if (file[0] != '/') {
691 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
692 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
697 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
700 if (stat(file, &stats) == 0) {
701 log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
704 /* make sure, the device did not disappear in the meantime */
705 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
706 log_debug("device disappeared while waiting for '%s'\n", file);
709 log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
710 nanosleep(&duration, NULL);
712 log_debug("waiting for '%s' failed\n", file);
716 static int attr_subst_subdir(char *attr, size_t len)
720 if (strstr(attr, "/*/")) {
722 char dirname[UTIL_PATH_SIZE];
726 strscpy(dirname, sizeof(dirname), attr);
727 pos = strstr(dirname, "/*/");
732 dir = opendir(dirname);
736 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
739 if (dent->d_name[0] == '.')
741 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
742 if (stat(attr, &stats) == 0) {
754 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
760 if (linepos == NULL || linepos[0] == '\0')
763 /* skip whitespace */
764 while (isspace(linepos[0]) || linepos[0] == ',')
768 if (linepos[0] == '\0')
774 if (linepos[0] == '\0')
776 if (isspace(linepos[0]))
778 if (linepos[0] == '=')
780 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
781 if (linepos[1] == '=')
785 /* remember end of key */
788 /* skip whitespace after key */
789 while (isspace(linepos[0]))
791 if (linepos[0] == '\0')
794 /* get operation type */
795 if (linepos[0] == '=' && linepos[1] == '=') {
798 } else if (linepos[0] == '!' && linepos[1] == '=') {
801 } else if (linepos[0] == '+' && linepos[1] == '=') {
804 } else if (linepos[0] == '=') {
807 } else if (linepos[0] == ':' && linepos[1] == '=') {
808 *op = OP_ASSIGN_FINAL;
816 /* skip whitespace after operator */
817 while (isspace(linepos[0]))
819 if (linepos[0] == '\0')
823 if (linepos[0] == '"')
830 temp = strchr(linepos, '"');
836 /* move line to next key */
841 /* extract possible KEY{attr} */
842 static const char *get_key_attribute(struct udev *udev, char *str)
847 attr = strchr(str, '{');
850 pos = strchr(attr, '}');
852 log_error("missing closing brace for format\n");
861 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
862 enum operation_type op,
863 const char *value, const void *data)
865 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
866 const char *attr = NULL;
868 memset(token, 0x00, sizeof(struct token));
880 case TK_M_SUBSYSTEMS:
884 case TK_M_IMPORT_FILE:
885 case TK_M_IMPORT_PROG:
887 case TK_M_IMPORT_CMDLINE:
888 case TK_M_IMPORT_PARENT:
898 token->key.value_off = rules_add_string(rule_tmp->rules, value);
900 case TK_M_IMPORT_BUILTIN:
901 token->key.value_off = rules_add_string(rule_tmp->rules, value);
902 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
911 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
915 token->key.value_off = rules_add_string(rule_tmp->rules, value);
917 token->key.mode = *(mode_t *)data;
919 case TK_A_STRING_ESCAPE_NONE:
920 case TK_A_STRING_ESCAPE_REPLACE:
921 case TK_A_DB_PERSIST:
923 case TK_A_RUN_BUILTIN:
924 case TK_A_RUN_PROGRAM:
925 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
926 token->key.value_off = rules_add_string(rule_tmp->rules, value);
928 case TK_A_INOTIFY_WATCH:
929 case TK_A_DEVLINK_PRIO:
930 token->key.devlink_prio = *(int *)data;
933 token->key.uid = *(uid_t *)data;
936 token->key.gid = *(gid_t *)data;
939 token->key.mode = *(mode_t *)data;
941 case TK_A_STATIC_NODE:
942 token->key.value_off = rules_add_string(rule_tmp->rules, value);
944 case TK_M_EVENT_TIMEOUT:
945 token->key.event_timeout = *(int *)data;
948 case TK_M_PARENTS_MIN:
949 case TK_M_PARENTS_MAX:
953 log_error("wrong type %u\n", type);
957 if (value != NULL && type < TK_M_MAX) {
958 /* check if we need to split or call fnmatch() while matching rules */
959 enum string_glob_type glob;
963 has_split = (strchr(value, '|') != NULL);
964 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
965 if (has_split && has_glob) {
966 glob = GL_SPLIT_GLOB;
967 } else if (has_split) {
969 } else if (has_glob) {
970 if (streq(value, "?*"))
977 token->key.glob = glob;
980 if (value != NULL && type > TK_M_MAX) {
981 /* check if assigned value has substitution chars */
983 token->key.subst = SB_SUBSYS;
984 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
985 token->key.subst = SB_FORMAT;
987 token->key.subst = SB_NONE;
991 /* check if property/attribut name has substitution chars */
993 token->key.attrsubst = SB_SUBSYS;
994 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
995 token->key.attrsubst = SB_FORMAT;
997 token->key.attrsubst = SB_NONE;
1000 token->key.type = type;
1002 rule_tmp->token_cur++;
1003 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
1004 log_error("temporary rule array too small\n");
1010 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1013 unsigned int start = 0;
1014 unsigned int end = rule_tmp->token_cur;
1016 for (i = 0; i < rule_tmp->token_cur; i++) {
1017 enum token_type next_val = TK_UNSET;
1018 unsigned int next_idx = 0;
1021 /* find smallest value */
1022 for (j = start; j < end; j++) {
1023 if (rule_tmp->token[j].type == TK_UNSET)
1025 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1026 next_val = rule_tmp->token[j].type;
1031 /* add token and mark done */
1032 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1034 rule_tmp->token[next_idx].type = TK_UNSET;
1037 if (next_idx == start)
1039 if (next_idx+1 == end)
1045 static int add_rule(struct udev_rules *rules, char *line,
1046 const char *filename, unsigned int filename_off, unsigned int lineno)
1050 struct rule_tmp rule_tmp;
1052 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1053 rule_tmp.rules = rules;
1054 rule_tmp.rule.type = TK_RULE;
1055 /* the offset in the rule is limited to unsigned short */
1056 if (filename_off < USHRT_MAX)
1057 rule_tmp.rule.rule.filename_off = filename_off;
1058 rule_tmp.rule.rule.filename_line = lineno;
1064 enum operation_type op;
1066 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1067 /* Avoid erroring on trailing whitespace. This is probably rare
1068 * so save the work for the error case instead of always trying
1069 * to strip the trailing whitespace with strstrip(). */
1070 while (isblank(*linepos))
1073 /* If we aren't at the end of the line, this is a parsing error.
1074 * Make a best effort to describe where the problem is. */
1075 if (*linepos != '\n') {
1076 char buf[2] = {linepos[1]};
1077 _cleanup_free_ char *tmp;
1080 log_error("invalid key/value pair in file %s on line %u,"
1081 "starting at character %tu ('%s')\n",
1082 filename, lineno, linepos - line + 1, tmp);
1083 if (linepos[1] == '#')
1084 log_error("hint: comments can only start at beginning of line");
1089 if (streq(key, "ACTION")) {
1090 if (op > OP_MATCH_MAX) {
1091 log_error("invalid ACTION operation\n");
1094 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1098 if (streq(key, "DEVPATH")) {
1099 if (op > OP_MATCH_MAX) {
1100 log_error("invalid DEVPATH operation\n");
1103 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1107 if (streq(key, "KERNEL")) {
1108 if (op > OP_MATCH_MAX) {
1109 log_error("invalid KERNEL operation\n");
1112 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1116 if (streq(key, "SUBSYSTEM")) {
1117 if (op > OP_MATCH_MAX) {
1118 log_error("invalid SUBSYSTEM operation\n");
1121 /* bus, class, subsystem events should all be the same */
1122 if (streq(value, "subsystem") ||
1123 streq(value, "bus") ||
1124 streq(value, "class")) {
1125 if (streq(value, "bus") || streq(value, "class"))
1126 log_error("'%s' must be specified as 'subsystem' \n"
1127 "please fix it in %s:%u", value, filename, lineno);
1128 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1130 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1134 if (streq(key, "DRIVER")) {
1135 if (op > OP_MATCH_MAX) {
1136 log_error("invalid DRIVER operation\n");
1139 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1143 if (startswith(key, "ATTR{")) {
1144 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1146 log_error("error parsing ATTR attribute\n");
1149 if (op < OP_MATCH_MAX) {
1150 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1152 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1157 if (startswith(key, "SECLABEL{")) {
1158 attr = get_key_attribute(rules->udev, key + sizeof("SECLABEL")-1);
1160 log_error("error parsing SECLABEL attribute\n");
1164 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1168 if (streq(key, "KERNELS")) {
1169 if (op > OP_MATCH_MAX) {
1170 log_error("invalid KERNELS operation\n");
1173 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1177 if (streq(key, "SUBSYSTEMS")) {
1178 if (op > OP_MATCH_MAX) {
1179 log_error("invalid SUBSYSTEMS operation\n");
1182 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1186 if (streq(key, "DRIVERS")) {
1187 if (op > OP_MATCH_MAX) {
1188 log_error("invalid DRIVERS operation\n");
1191 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1195 if (startswith(key, "ATTRS{")) {
1196 if (op > OP_MATCH_MAX) {
1197 log_error("invalid ATTRS operation\n");
1200 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1202 log_error("error parsing ATTRS attribute\n");
1205 if (startswith(attr, "device/"))
1206 log_error("the 'device' link may not be available in a future kernel, "
1207 "please fix it in %s:%u", filename, lineno);
1208 else if (strstr(attr, "../") != NULL)
1209 log_error("do not reference parent sysfs directories directly, "
1210 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1211 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1215 if (streq(key, "TAGS")) {
1216 if (op > OP_MATCH_MAX) {
1217 log_error("invalid TAGS operation\n");
1220 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1224 if (startswith(key, "ENV{")) {
1225 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1227 log_error("error parsing ENV attribute\n");
1230 if (op < OP_MATCH_MAX) {
1231 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1234 static const char *blacklist[] = {
1249 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1250 if (!streq(attr, blacklist[i]))
1252 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
1255 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1261 if (streq(key, "TAG")) {
1262 if (op < OP_MATCH_MAX)
1263 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1265 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1269 if (streq(key, "PROGRAM")) {
1270 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1274 if (streq(key, "RESULT")) {
1275 if (op > OP_MATCH_MAX) {
1276 log_error("invalid RESULT operation\n");
1279 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1283 if (startswith(key, "IMPORT")) {
1284 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1286 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1289 if (streq(attr, "program")) {
1290 /* find known built-in command */
1291 if (value[0] != '/') {
1292 enum udev_builtin_cmd cmd;
1294 cmd = udev_builtin_lookup(value);
1295 if (cmd < UDEV_BUILTIN_MAX) {
1296 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1297 value, filename, lineno);
1298 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1302 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1303 } else if (streq(attr, "builtin")) {
1304 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1306 if (cmd < UDEV_BUILTIN_MAX)
1307 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1309 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1310 } else if (streq(attr, "file")) {
1311 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1312 } else if (streq(attr, "db")) {
1313 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1314 } else if (streq(attr, "cmdline")) {
1315 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1316 } else if (streq(attr, "parent")) {
1317 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1319 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno);
1323 if (startswith(key, "TEST")) {
1326 if (op > OP_MATCH_MAX) {
1327 log_error("invalid TEST operation\n");
1330 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1332 mode = strtol(attr, NULL, 8);
1333 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1335 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1340 if (startswith(key, "RUN")) {
1341 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1345 if (streq(attr, "builtin")) {
1346 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1348 if (cmd < UDEV_BUILTIN_MAX)
1349 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1351 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1352 } else if (streq(attr, "program")) {
1353 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1355 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1357 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno);
1363 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1364 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1368 if (streq(key, "LABEL")) {
1369 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1373 if (streq(key, "GOTO")) {
1374 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1378 if (startswith(key, "NAME")) {
1379 if (op < OP_MATCH_MAX) {
1380 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1382 if (streq(value, "%k")) {
1383 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1384 "please remove it from %s:%u\n", filename, lineno);
1387 if (value[0] == '\0') {
1388 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1389 "please remove it from %s:%u\n", filename, lineno);
1392 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1394 rule_tmp.rule.rule.can_set_name = true;
1398 if (streq(key, "SYMLINK")) {
1399 if (op < OP_MATCH_MAX)
1400 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1402 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1403 rule_tmp.rule.rule.can_set_name = true;
1407 if (streq(key, "OWNER")) {
1411 uid = strtoul(value, &endptr, 10);
1412 if (endptr[0] == '\0') {
1413 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1414 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1415 uid = add_uid(rules, value);
1416 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1417 } else if (rules->resolve_names >= 0) {
1418 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1420 rule_tmp.rule.rule.can_set_name = true;
1424 if (streq(key, "GROUP")) {
1428 gid = strtoul(value, &endptr, 10);
1429 if (endptr[0] == '\0') {
1430 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1431 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1432 gid = add_gid(rules, value);
1433 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1434 } else if (rules->resolve_names >= 0) {
1435 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1437 rule_tmp.rule.rule.can_set_name = true;
1441 if (streq(key, "MODE")) {
1445 mode = strtol(value, &endptr, 8);
1446 if (endptr[0] == '\0')
1447 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1449 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1450 rule_tmp.rule.rule.can_set_name = true;
1454 if (streq(key, "OPTIONS")) {
1457 pos = strstr(value, "link_priority=");
1459 int prio = atoi(&pos[strlen("link_priority=")]);
1461 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1464 pos = strstr(value, "event_timeout=");
1466 int tout = atoi(&pos[strlen("event_timeout=")]);
1468 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
1471 pos = strstr(value, "string_escape=");
1473 pos = &pos[strlen("string_escape=")];
1474 if (startswith(pos, "none"))
1475 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1476 else if (startswith(pos, "replace"))
1477 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1480 pos = strstr(value, "db_persist");
1482 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1484 pos = strstr(value, "nowatch");
1488 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1490 pos = strstr(value, "watch");
1494 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1498 pos = strstr(value, "static_node=");
1500 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1501 rule_tmp.rule.rule.has_static_node = true;
1507 log_error("unknown key '%s' in %s:%u\n", key, filename, lineno);
1511 /* add rule token */
1512 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1513 if (add_token(rules, &rule_tmp.rule) != 0)
1516 /* add tokens to list, sorted by type */
1517 if (sort_token(rules, &rule_tmp) != 0)
1522 log_error("invalid rule '%s:%u'\n", filename, lineno);
1526 static int parse_file(struct udev_rules *rules, const char *filename)
1529 unsigned int first_token;
1530 unsigned int filename_off;
1531 char line[UTIL_LINE_SIZE];
1535 if (null_or_empty_path(filename)) {
1536 log_debug("skip empty file: %s\n", filename);
1539 log_debug("read rules file: %s\n", filename);
1541 f = fopen(filename, "re");
1545 first_token = rules->token_cur;
1546 filename_off = rules_add_string(rules, filename);
1548 while (fgets(line, sizeof(line), f) != NULL) {
1552 /* skip whitespace */
1555 while (isspace(key[0]))
1566 /* continue reading if backslash+newline is found */
1567 while (line[len-2] == '\\') {
1568 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1570 if (strlen(&line[len-2]) < 2)
1576 if (len+1 >= sizeof(line)) {
1577 log_error("line too long '%s':%u, ignored\n", filename, line_nr);
1580 add_rule(rules, key, filename, filename_off, line_nr);
1584 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1585 for (i = first_token+1; i < rules->token_cur; i++) {
1586 if (rules->tokens[i].type == TK_A_GOTO) {
1587 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1590 for (j = i+1; j < rules->token_cur; j++) {
1591 if (rules->tokens[j].type != TK_RULE)
1593 if (rules->tokens[j].rule.label_off == 0)
1595 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1597 rules->tokens[i].key.rule_goto = j;
1600 if (rules->tokens[i].key.rule_goto == 0)
1601 log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename);
1607 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1609 struct udev_rules *rules;
1610 struct udev_list file_list;
1611 struct token end_token;
1615 rules = calloc(1, sizeof(struct udev_rules));
1619 rules->resolve_names = resolve_names;
1620 udev_list_init(udev, &file_list, true);
1622 /* init token array and string buffer */
1623 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1624 if (rules->tokens == NULL)
1625 return udev_rules_unref(rules);
1626 rules->token_max = PREALLOC_TOKEN;
1628 rules->strbuf = strbuf_new();
1630 return udev_rules_unref(rules);
1632 rules->dirs = strv_new("/etc/udev/rules.d",
1633 "/run/udev/rules.d",
1634 UDEVLIBEXECDIR "/rules.d",
1637 log_error("failed to build config directory array");
1638 return udev_rules_unref(rules);
1640 if (!path_strv_canonicalize(rules->dirs)) {
1641 log_error("failed to canonicalize config directories\n");
1642 return udev_rules_unref(rules);
1644 strv_uniq(rules->dirs);
1646 udev_rules_check_timestamp(rules);
1648 r = conf_files_list_strv(&files, ".rules", NULL, (const char **)rules->dirs);
1650 log_error("failed to enumerate rules files: %s\n", strerror(-r));
1651 return udev_rules_unref(rules);
1655 * The offset value in the rules strct is limited; add all
1656 * rules file names to the beginning of the string buffer.
1658 STRV_FOREACH(f, files)
1659 rules_add_string(rules, *f);
1661 STRV_FOREACH(f, files)
1662 parse_file(rules, *f);
1666 memset(&end_token, 0x00, sizeof(struct token));
1667 end_token.type = TK_END;
1668 add_token(rules, &end_token);
1669 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings\n",
1670 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1672 /* cleanup temporary strbuf data */
1673 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used\n",
1674 rules->strbuf->in_count, rules->strbuf->in_len,
1675 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1676 strbuf_complete(rules->strbuf);
1678 /* cleanup uid/gid cache */
1681 rules->uids_cur = 0;
1682 rules->uids_max = 0;
1685 rules->gids_cur = 0;
1686 rules->gids_max = 0;
1692 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1696 free(rules->tokens);
1697 strbuf_cleanup(rules->strbuf);
1700 strv_free(rules->dirs);
1705 bool udev_rules_check_timestamp(struct udev_rules *rules)
1710 return paths_check_timestamp(rules->dirs, &rules->dirs_ts_usec, true);
1713 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1715 char *key_value = rules_str(rules, token->key.value_off);
1722 switch (token->key.glob) {
1724 match = (streq(key_value, val));
1727 match = (fnmatch(key_value, val, 0) == 0);
1734 s = rules_str(rules, token->key.value_off);
1739 next = strchr(s, '|');
1741 size_t matchlen = (size_t)(next - s);
1743 match = (matchlen == len && strneq(s, val, matchlen));
1747 match = (streq(s, val));
1756 char value[UTIL_PATH_SIZE];
1758 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1760 while (key_value != NULL) {
1761 pos = strchr(key_value, '|');
1766 match = (fnmatch(key_value, val, 0) == 0);
1774 match = (val[0] != '\0');
1780 if (match && (token->key.op == OP_MATCH))
1782 if (!match && (token->key.op == OP_NOMATCH))
1787 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1790 char nbuf[UTIL_NAME_SIZE];
1792 char vbuf[UTIL_NAME_SIZE];
1795 name = rules_str(rules, cur->key.attr_off);
1796 switch (cur->key.attrsubst) {
1798 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1802 value = udev_device_get_sysattr_value(dev, name);
1807 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1815 /* remove trailing whitespace, if not asked to match for it */
1816 len = strlen(value);
1817 if (len > 0 && isspace(value[len-1])) {
1818 const char *key_value;
1821 key_value = rules_str(rules, cur->key.value_off);
1822 klen = strlen(key_value);
1823 if (klen > 0 && !isspace(key_value[klen-1])) {
1824 if (value != vbuf) {
1825 strscpy(vbuf, sizeof(vbuf), value);
1828 while (len > 0 && isspace(vbuf[--len]))
1833 return match_key(rules, cur, value);
1842 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
1846 enum escape_type esc = ESCAPE_UNSET;
1849 if (rules->tokens == NULL)
1852 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1853 (major(udev_device_get_devnum(event->dev)) > 0 ||
1854 udev_device_get_ifindex(event->dev) > 0));
1856 /* loop through token list, match, run actions or forward to next rule */
1857 cur = &rules->tokens[0];
1860 dump_token(rules, cur);
1861 switch (cur->type) {
1865 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1866 if (!can_set_name && rule->rule.can_set_name)
1871 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1875 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1879 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1882 case TK_M_DEVLINK: {
1883 struct udev_list_entry *list_entry;
1886 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1887 const char *devlink;
1889 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1890 if (match_key(rules, cur, devlink) == 0) {
1900 if (match_key(rules, cur, event->name) != 0)
1904 const char *key_name = rules_str(rules, cur->key.attr_off);
1907 value = udev_device_get_property_value(event->dev, key_name);
1910 if (match_key(rules, cur, value))
1915 struct udev_list_entry *list_entry;
1918 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1919 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1924 if (!match && (cur->key.op != OP_NOMATCH))
1928 case TK_M_SUBSYSTEM:
1929 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1933 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1936 case TK_M_WAITFOR: {
1937 char filename[UTIL_PATH_SIZE];
1940 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1941 found = (wait_for_file(event->dev, filename, 10) == 0);
1942 if (!found && (cur->key.op != OP_NOMATCH))
1947 if (match_attr(rules, event->dev, event, cur) != 0)
1951 case TK_M_SUBSYSTEMS:
1957 /* get whole sequence of parent matches */
1959 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1962 /* loop over parents */
1963 event->dev_parent = event->dev;
1967 /* loop over sequence of parent match keys */
1968 for (key = cur; key < next; key++ ) {
1969 dump_token(rules, key);
1972 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1975 case TK_M_SUBSYSTEMS:
1976 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1980 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1984 if (match_attr(rules, event->dev_parent, event, key) != 0)
1988 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
1990 if (match && key->key.op == OP_NOMATCH)
1992 if (!match && key->key.op == OP_MATCH)
2003 event->dev_parent = udev_device_get_parent(event->dev_parent);
2004 if (event->dev_parent == NULL)
2007 /* move behind our sequence of parent match keys */
2012 char filename[UTIL_PATH_SIZE];
2013 struct stat statbuf;
2016 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
2017 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2018 if (filename[0] != '/') {
2019 char tmp[UTIL_PATH_SIZE];
2021 strscpy(tmp, sizeof(tmp), filename);
2022 strscpyl(filename, sizeof(filename),
2023 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2026 attr_subst_subdir(filename, sizeof(filename));
2028 match = (stat(filename, &statbuf) == 0);
2029 if (match && cur->key.mode > 0)
2030 match = ((statbuf.st_mode & cur->key.mode) > 0);
2031 if (match && cur->key.op == OP_NOMATCH)
2033 if (!match && cur->key.op == OP_MATCH)
2037 case TK_M_EVENT_TIMEOUT:
2038 log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout);
2039 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2041 case TK_M_PROGRAM: {
2042 char program[UTIL_PATH_SIZE];
2044 char result[UTIL_PATH_SIZE];
2046 free(event->program_result);
2047 event->program_result = NULL;
2048 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2049 envp = udev_device_get_properties_envp(event->dev);
2050 log_debug("PROGRAM '%s' %s:%u\n",
2052 rules_str(rules, rule->rule.filename_off),
2053 rule->rule.filename_line);
2055 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2056 if (cur->key.op != OP_NOMATCH)
2061 util_remove_trailing_chars(result, '\n');
2062 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2063 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2065 log_debug("%i character(s) replaced\n" , count);
2067 event->program_result = strdup(result);
2068 if (cur->key.op == OP_NOMATCH)
2073 case TK_M_IMPORT_FILE: {
2074 char import[UTIL_PATH_SIZE];
2076 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2077 if (import_file_into_properties(event->dev, import) != 0)
2078 if (cur->key.op != OP_NOMATCH)
2082 case TK_M_IMPORT_PROG: {
2083 char import[UTIL_PATH_SIZE];
2085 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2086 log_debug("IMPORT '%s' %s:%u\n",
2088 rules_str(rules, rule->rule.filename_off),
2089 rule->rule.filename_line);
2091 if (import_program_into_properties(event, import, sigmask) != 0)
2092 if (cur->key.op != OP_NOMATCH)
2096 case TK_M_IMPORT_BUILTIN: {
2097 char command[UTIL_PATH_SIZE];
2099 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2100 /* check if we ran already */
2101 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2102 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2103 udev_builtin_name(cur->key.builtin_cmd),
2104 rules_str(rules, rule->rule.filename_off),
2105 rule->rule.filename_line);
2106 /* return the result from earlier run */
2107 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2108 if (cur->key.op != OP_NOMATCH)
2113 event->builtin_run |= (1 << cur->key.builtin_cmd);
2116 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2117 log_debug("IMPORT builtin '%s' %s:%u\n",
2118 udev_builtin_name(cur->key.builtin_cmd),
2119 rules_str(rules, rule->rule.filename_off),
2120 rule->rule.filename_line);
2122 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2123 /* remember failure */
2124 log_debug("IMPORT builtin '%s' returned non-zero\n",
2125 udev_builtin_name(cur->key.builtin_cmd));
2126 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2127 if (cur->key.op != OP_NOMATCH)
2132 case TK_M_IMPORT_DB: {
2133 const char *key = rules_str(rules, cur->key.value_off);
2136 value = udev_device_get_property_value(event->dev_db, key);
2137 if (value != NULL) {
2138 struct udev_list_entry *entry;
2140 entry = udev_device_add_property(event->dev, key, value);
2141 udev_list_entry_set_num(entry, true);
2143 if (cur->key.op != OP_NOMATCH)
2148 case TK_M_IMPORT_CMDLINE: {
2150 bool imported = false;
2152 f = fopen("/proc/cmdline", "re");
2156 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2157 const char *key = rules_str(rules, cur->key.value_off);
2160 pos = strstr(cmdline, key);
2162 struct udev_list_entry *entry;
2165 if (pos[0] == '\0' || isspace(pos[0])) {
2166 /* we import simple flags as 'FLAG=1' */
2167 entry = udev_device_add_property(event->dev, key, "1");
2168 udev_list_entry_set_num(entry, true);
2170 } else if (pos[0] == '=') {
2175 while (pos[0] != '\0' && !isspace(pos[0]))
2178 entry = udev_device_add_property(event->dev, key, value);
2179 udev_list_entry_set_num(entry, true);
2186 if (!imported && cur->key.op != OP_NOMATCH)
2190 case TK_M_IMPORT_PARENT: {
2191 char import[UTIL_PATH_SIZE];
2193 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2194 if (import_parent_into_properties(event->dev, import) != 0)
2195 if (cur->key.op != OP_NOMATCH)
2200 if (match_key(rules, cur, event->program_result) != 0)
2203 case TK_A_STRING_ESCAPE_NONE:
2206 case TK_A_STRING_ESCAPE_REPLACE:
2207 esc = ESCAPE_REPLACE;
2209 case TK_A_DB_PERSIST:
2210 udev_device_set_db_persist(event->dev);
2212 case TK_A_INOTIFY_WATCH:
2213 if (event->inotify_watch_final)
2215 if (cur->key.op == OP_ASSIGN_FINAL)
2216 event->inotify_watch_final = true;
2217 event->inotify_watch = cur->key.watch;
2219 case TK_A_DEVLINK_PRIO:
2220 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2223 char owner[UTIL_NAME_SIZE];
2225 if (event->owner_final)
2227 if (cur->key.op == OP_ASSIGN_FINAL)
2228 event->owner_final = true;
2229 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2230 event->owner_set = true;
2231 event->uid = util_lookup_user(event->udev, owner);
2232 log_debug("OWNER %u %s:%u\n",
2234 rules_str(rules, rule->rule.filename_off),
2235 rule->rule.filename_line);
2239 char group[UTIL_NAME_SIZE];
2241 if (event->group_final)
2243 if (cur->key.op == OP_ASSIGN_FINAL)
2244 event->group_final = true;
2245 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2246 event->group_set = true;
2247 event->gid = util_lookup_group(event->udev, group);
2248 log_debug("GROUP %u %s:%u\n",
2250 rules_str(rules, rule->rule.filename_off),
2251 rule->rule.filename_line);
2255 char mode_str[UTIL_NAME_SIZE];
2259 if (event->mode_final)
2261 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2262 mode = strtol(mode_str, &endptr, 8);
2263 if (endptr[0] != '\0') {
2264 log_error("ignoring invalid mode '%s'\n", mode_str);
2267 if (cur->key.op == OP_ASSIGN_FINAL)
2268 event->mode_final = true;
2269 event->mode_set = true;
2271 log_debug("MODE %#o %s:%u\n",
2273 rules_str(rules, rule->rule.filename_off),
2274 rule->rule.filename_line);
2278 if (event->owner_final)
2280 if (cur->key.op == OP_ASSIGN_FINAL)
2281 event->owner_final = true;
2282 event->owner_set = true;
2283 event->uid = cur->key.uid;
2284 log_debug("OWNER %u %s:%u\n",
2286 rules_str(rules, rule->rule.filename_off),
2287 rule->rule.filename_line);
2290 if (event->group_final)
2292 if (cur->key.op == OP_ASSIGN_FINAL)
2293 event->group_final = true;
2294 event->group_set = true;
2295 event->gid = cur->key.gid;
2296 log_debug("GROUP %u %s:%u\n",
2298 rules_str(rules, rule->rule.filename_off),
2299 rule->rule.filename_line);
2302 if (event->mode_final)
2304 if (cur->key.op == OP_ASSIGN_FINAL)
2305 event->mode_final = true;
2306 event->mode_set = true;
2307 event->mode = cur->key.mode;
2308 log_debug("MODE %#o %s:%u\n",
2310 rules_str(rules, rule->rule.filename_off),
2311 rule->rule.filename_line);
2313 case TK_A_SECLABEL: {
2314 const char *name, *label;
2316 name = rules_str(rules, cur->key.attr_off);
2317 label = rules_str(rules, cur->key.value_off);
2318 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2319 udev_list_cleanup(&event->seclabel_list);
2320 udev_list_entry_add(&event->seclabel_list, name, label);
2321 log_debug("SECLABEL{%s}='%s' %s:%u\n",
2323 rules_str(rules, rule->rule.filename_off),
2324 rule->rule.filename_line);
2328 const char *name = rules_str(rules, cur->key.attr_off);
2329 char *value = rules_str(rules, cur->key.value_off);
2330 char value_new[UTIL_NAME_SIZE];
2331 const char *value_old = NULL;
2332 struct udev_list_entry *entry;
2334 if (value[0] == '\0') {
2335 if (cur->key.op == OP_ADD)
2337 udev_device_add_property(event->dev, name, NULL);
2341 if (cur->key.op == OP_ADD)
2342 value_old = udev_device_get_property_value(event->dev, name);
2344 char temp[UTIL_NAME_SIZE];
2346 /* append value separated by space */
2347 udev_event_apply_format(event, value, temp, sizeof(temp));
2348 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2350 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2352 entry = udev_device_add_property(event->dev, name, value_new);
2353 /* store in db, skip private keys */
2355 udev_list_entry_set_num(entry, true);
2359 char tag[UTIL_PATH_SIZE];
2362 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2363 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2364 udev_device_cleanup_tags_list(event->dev);
2365 for (p = tag; *p != '\0'; p++) {
2366 if ((*p >= 'a' && *p <= 'z') ||
2367 (*p >= 'A' && *p <= 'Z') ||
2368 (*p >= '0' && *p <= '9') ||
2369 *p == '-' || *p == '_')
2371 log_error("ignoring invalid tag name '%s'\n", tag);
2374 udev_device_add_tag(event->dev, tag);
2378 const char *name = rules_str(rules, cur->key.value_off);
2380 char name_str[UTIL_PATH_SIZE];
2383 if (event->name_final)
2385 if (cur->key.op == OP_ASSIGN_FINAL)
2386 event->name_final = true;
2387 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2388 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2389 count = util_replace_chars(name_str, "/");
2391 log_debug("%i character(s) replaced\n", count);
2393 if (major(udev_device_get_devnum(event->dev)) &&
2394 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2395 log_error("NAME=\"%s\" ignored, kernel device nodes "
2396 "can not be renamed; please fix it in %s:%u\n", name,
2397 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2401 event->name = strdup(name_str);
2402 log_debug("NAME '%s' %s:%u\n",
2404 rules_str(rules, rule->rule.filename_off),
2405 rule->rule.filename_line);
2408 case TK_A_DEVLINK: {
2409 char temp[UTIL_PATH_SIZE];
2410 char filename[UTIL_PATH_SIZE];
2414 if (event->devlink_final)
2416 if (major(udev_device_get_devnum(event->dev)) == 0)
2418 if (cur->key.op == OP_ASSIGN_FINAL)
2419 event->devlink_final = true;
2420 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2421 udev_device_cleanup_devlinks_list(event->dev);
2423 /* allow multiple symlinks separated by spaces */
2424 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2425 if (esc == ESCAPE_UNSET)
2426 count = util_replace_chars(temp, "/ ");
2427 else if (esc == ESCAPE_REPLACE)
2428 count = util_replace_chars(temp, "/");
2430 log_debug("%i character(s) replaced\n" , count);
2432 while (isspace(pos[0]))
2434 next = strchr(pos, ' ');
2435 while (next != NULL) {
2437 log_debug("LINK '%s' %s:%u\n", pos,
2438 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2439 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2440 udev_device_add_devlink(event->dev, filename);
2441 while (isspace(next[1]))
2444 next = strchr(pos, ' ');
2446 if (pos[0] != '\0') {
2447 log_debug("LINK '%s' %s:%u\n", pos,
2448 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2449 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2450 udev_device_add_devlink(event->dev, filename);
2455 const char *key_name = rules_str(rules, cur->key.attr_off);
2456 char attr[UTIL_PATH_SIZE];
2457 char value[UTIL_NAME_SIZE];
2460 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2461 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2462 attr_subst_subdir(attr, sizeof(attr));
2464 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2465 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
2466 rules_str(rules, rule->rule.filename_off),
2467 rule->rule.filename_line);
2468 f = fopen(attr, "we");
2470 if (fprintf(f, "%s", value) <= 0)
2471 log_error("error writing ATTR{%s}: %m\n", attr);
2474 log_error("error opening ATTR{%s} for writing: %m\n", attr);
2478 case TK_A_RUN_BUILTIN:
2479 case TK_A_RUN_PROGRAM: {
2480 struct udev_list_entry *entry;
2482 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2483 udev_list_cleanup(&event->run_list);
2484 log_debug("RUN '%s' %s:%u\n",
2485 rules_str(rules, cur->key.value_off),
2486 rules_str(rules, rule->rule.filename_off),
2487 rule->rule.filename_line);
2488 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2489 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2493 if (cur->key.rule_goto == 0)
2495 cur = &rules->tokens[cur->key.rule_goto];
2500 case TK_M_PARENTS_MIN:
2501 case TK_M_PARENTS_MAX:
2504 log_error("wrong type %u\n", cur->type);
2511 /* fast-forward to next rule */
2512 cur = rule + rule->rule.token_count;
2516 int udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2523 _cleanup_strv_free_ char **tags = NULL;
2526 _cleanup_free_ char *path = NULL;
2529 if (rules->tokens == NULL)
2532 cur = &rules->tokens[0];
2535 switch (cur->type) {
2540 /* skip rules without a static_node tag */
2541 if (!rule->rule.has_static_node)
2557 mode = cur->key.mode;
2560 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2565 case TK_A_STATIC_NODE: {
2566 char device_node[UTIL_PATH_SIZE];
2567 char tags_dir[UTIL_PATH_SIZE];
2568 char tag_symlink[UTIL_PATH_SIZE];
2571 /* we assure, that the permissions tokens are sorted before the static token */
2572 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2574 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2575 if (stat(device_node, &stats) != 0)
2577 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2581 /* Export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2583 STRV_FOREACH(t, tags) {
2584 _cleanup_free_ char *unescaped_filename = NULL;
2586 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2587 r = mkdir_p(tags_dir, 0755);
2589 log_error("failed to create %s: %s\n", tags_dir, strerror(-r));
2593 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2595 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2596 r = symlink(device_node, tag_symlink);
2597 if (r < 0 && errno != EEXIST) {
2598 log_error("failed to create symlink %s -> %s: %s\n", tag_symlink, device_node, strerror(errno));
2605 /* don't touch the permissions if only the tags were set */
2606 if (mode == 0 && uid == 0 && gid == 0)
2615 if (mode != (stats.st_mode & 01777)) {
2616 r = chmod(device_node, mode);
2618 log_error("failed to chmod '%s' %#o\n", device_node, mode);
2621 log_debug("chmod '%s' %#o\n", device_node, mode);
2624 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2625 r = chown(device_node, uid, gid);
2627 log_error("failed to chown '%s' %u %u \n", device_node, uid, gid);
2630 log_debug("chown '%s' %u %u\n", device_node, uid, gid);
2633 utimensat(AT_FDCWD, device_node, NULL, 0);
2643 /* fast-forward to next rule */
2644 cur = rule + rule->rule.token_count;
2651 fchmod(fileno(f), 0644);
2652 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2654 unlink("/run/udev/static_node-tags");