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"
38 #define PREALLOC_TOKEN 2048
41 unsigned int name_off;
54 /* every key in the rules file becomes a token */
56 unsigned int token_cur;
57 unsigned int token_max;
59 /* all key strings are copied and de-duplicated in a single continuous string buffer */
60 struct strbuf *strbuf;
62 /* during rule parsing, uid/gid lookup results are cached */
64 unsigned int uids_cur;
65 unsigned int uids_max;
67 unsigned int gids_cur;
68 unsigned int gids_max;
71 static char *rules_str(struct udev_rules *rules, unsigned int off) {
72 return rules->strbuf->buf + off;
75 static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
76 return strbuf_add_string(rules->strbuf, s, strlen(s));
79 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
92 enum string_glob_type {
94 GL_PLAIN, /* no special chars */
95 GL_GLOB, /* shell globs ?,*,[] */
96 GL_SPLIT, /* multi-value A|B */
97 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
98 GL_SOMETHING, /* commonly used "?*" */
101 enum string_subst_type {
108 /* tokens of a rule are sorted/handled in this order */
113 TK_M_ACTION, /* val */
114 TK_M_DEVPATH, /* val */
115 TK_M_KERNEL, /* val */
116 TK_M_DEVLINK, /* val */
118 TK_M_ENV, /* val, attr */
120 TK_M_SUBSYSTEM, /* val */
121 TK_M_DRIVER, /* val */
122 TK_M_WAITFOR, /* val */
123 TK_M_ATTR, /* val, attr */
126 TK_M_KERNELS, /* val */
127 TK_M_SUBSYSTEMS, /* val */
128 TK_M_DRIVERS, /* val */
129 TK_M_ATTRS, /* val, attr */
133 TK_M_TEST, /* val, mode_t */
134 TK_M_EVENT_TIMEOUT, /* int */
135 TK_M_PROGRAM, /* val */
136 TK_M_IMPORT_FILE, /* val */
137 TK_M_IMPORT_PROG, /* val */
138 TK_M_IMPORT_BUILTIN, /* val */
139 TK_M_IMPORT_DB, /* val */
140 TK_M_IMPORT_CMDLINE, /* val */
141 TK_M_IMPORT_PARENT, /* val */
142 TK_M_RESULT, /* val */
145 TK_A_STRING_ESCAPE_NONE,
146 TK_A_STRING_ESCAPE_REPLACE,
148 TK_A_INOTIFY_WATCH, /* int */
149 TK_A_DEVLINK_PRIO, /* int */
150 TK_A_OWNER, /* val */
151 TK_A_GROUP, /* val */
153 TK_A_OWNER_ID, /* uid_t */
154 TK_A_GROUP_ID, /* gid_t */
155 TK_A_MODE_ID, /* mode_t */
157 TK_A_STATIC_NODE, /* val */
158 TK_A_ENV, /* val, attr */
160 TK_A_DEVLINK, /* val */
161 TK_A_ATTR, /* val, attr */
162 TK_A_RUN_BUILTIN, /* val, bool */
163 TK_A_RUN_PROGRAM, /* val, bool */
164 TK_A_GOTO, /* size_t */
169 /* we try to pack stuff in a way that we take only 12 bytes per token */
172 unsigned char type; /* same in rule and key */
174 enum token_type type:8;
176 bool has_static_node:1;
177 unsigned int unused:6;
178 unsigned short token_count;
179 unsigned int label_off;
180 unsigned short filename_off;
181 unsigned short filename_line;
184 enum token_type type:8;
185 enum operation_type op:8;
186 enum string_glob_type glob:8;
187 enum string_subst_type subst:4;
188 enum string_subst_type attrsubst:4;
189 unsigned int value_off;
191 unsigned int attr_off;
192 unsigned int rule_goto;
199 enum udev_builtin_cmd builtin_cmd;
207 struct udev_rules *rules;
209 struct token token[MAX_TK];
210 unsigned int token_cur;
214 static const char *operation_str(enum operation_type type)
216 static const char *operation_strs[] = {
217 [OP_UNSET] = "UNSET",
218 [OP_MATCH] = "match",
219 [OP_NOMATCH] = "nomatch",
220 [OP_MATCH_MAX] = "MATCH_MAX",
223 [OP_ASSIGN] = "assign",
224 [OP_ASSIGN_FINAL] = "assign-final",
227 return operation_strs[type];
230 static const char *string_glob_str(enum string_glob_type type)
232 static const char *string_glob_strs[] = {
233 [GL_UNSET] = "UNSET",
234 [GL_PLAIN] = "plain",
236 [GL_SPLIT] = "split",
237 [GL_SPLIT_GLOB] = "split-glob",
238 [GL_SOMETHING] = "split-glob",
241 return string_glob_strs[type];
244 static const char *token_str(enum token_type type)
246 static const char *token_strs[] = {
247 [TK_UNSET] = "UNSET",
250 [TK_M_ACTION] = "M ACTION",
251 [TK_M_DEVPATH] = "M DEVPATH",
252 [TK_M_KERNEL] = "M KERNEL",
253 [TK_M_DEVLINK] = "M DEVLINK",
254 [TK_M_NAME] = "M NAME",
255 [TK_M_ENV] = "M ENV",
256 [TK_M_TAG] = "M TAG",
257 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
258 [TK_M_DRIVER] = "M DRIVER",
259 [TK_M_WAITFOR] = "M WAITFOR",
260 [TK_M_ATTR] = "M ATTR",
262 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
263 [TK_M_KERNELS] = "M KERNELS",
264 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
265 [TK_M_DRIVERS] = "M DRIVERS",
266 [TK_M_ATTRS] = "M ATTRS",
267 [TK_M_TAGS] = "M TAGS",
268 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
270 [TK_M_TEST] = "M TEST",
271 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
272 [TK_M_PROGRAM] = "M PROGRAM",
273 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
274 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
275 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
276 [TK_M_IMPORT_DB] = "M IMPORT_DB",
277 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
278 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
279 [TK_M_RESULT] = "M RESULT",
280 [TK_M_MAX] = "M MAX",
282 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
283 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
284 [TK_A_DB_PERSIST] = "A DB_PERSIST",
285 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
286 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
287 [TK_A_OWNER] = "A OWNER",
288 [TK_A_GROUP] = "A GROUP",
289 [TK_A_MODE] = "A MODE",
290 [TK_A_OWNER_ID] = "A OWNER_ID",
291 [TK_A_GROUP_ID] = "A GROUP_ID",
292 [TK_A_STATIC_NODE] = "A STATIC_NODE",
293 [TK_A_MODE_ID] = "A MODE_ID",
294 [TK_A_ENV] = "A ENV",
295 [TK_A_TAG] = "A ENV",
296 [TK_A_NAME] = "A NAME",
297 [TK_A_DEVLINK] = "A DEVLINK",
298 [TK_A_ATTR] = "A ATTR",
299 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
300 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
301 [TK_A_GOTO] = "A GOTO",
306 return token_strs[type];
309 static void dump_token(struct udev_rules *rules, struct token *token)
311 enum token_type type = token->type;
312 enum operation_type op = token->key.op;
313 enum string_glob_type glob = token->key.glob;
314 const char *value = str(rules, token->key.value_off);
315 const char *attr = &rules->buf[token->key.attr_off];
320 const char *tks_ptr = (char *)rules->tokens;
321 const char *tk_ptr = (char *)token;
322 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
324 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
325 &rules->buf[token->rule.filename_off], token->rule.filename_line,
326 idx, token->rule.token_count,
327 &rules->buf[token->rule.label_off]);
339 case TK_M_SUBSYSTEMS:
343 case TK_M_IMPORT_FILE:
344 case TK_M_IMPORT_PROG:
346 case TK_M_IMPORT_CMDLINE:
347 case TK_M_IMPORT_PARENT:
354 case TK_A_RUN_BUILTIN:
355 case TK_A_RUN_PROGRAM:
356 log_debug("%s %s '%s'(%s)\n",
357 token_str(type), operation_str(op), value, string_glob_str(glob));
359 case TK_M_IMPORT_BUILTIN:
360 log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
367 log_debug("%s %s '%s' '%s'(%s)\n",
368 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
372 log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value);
374 case TK_A_STRING_ESCAPE_NONE:
375 case TK_A_STRING_ESCAPE_REPLACE:
376 case TK_A_DB_PERSIST:
377 log_debug("%s\n", token_str(type));
380 log_debug("%s %s '%s'(%s) %#o\n",
381 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
383 case TK_A_INOTIFY_WATCH:
384 log_debug("%s %u\n", token_str(type), token->key.watch);
386 case TK_A_DEVLINK_PRIO:
387 log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
390 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
393 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
396 log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
398 case TK_A_STATIC_NODE:
399 log_debug("%s '%s'\n", token_str(type), value);
401 case TK_M_EVENT_TIMEOUT:
402 log_debug("%s %u\n", token_str(type), token->key.event_timeout);
405 log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
408 log_debug("* %s\n", token_str(type));
410 case TK_M_PARENTS_MIN:
411 case TK_M_PARENTS_MAX:
414 log_debug("unknown type %u\n", type);
419 static void dump_rules(struct udev_rules *rules)
423 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
425 rules->token_cur * sizeof(struct token),
428 for(i = 0; i < rules->token_cur; i++)
429 dump_token(rules, &rules->tokens[i]);
432 static inline const char *operation_str(enum operation_type type) { return NULL; }
433 static inline const char *token_str(enum token_type type) { return NULL; }
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)
440 /* grow buffer if needed */
441 if (rules->token_cur+1 >= rules->token_max) {
442 struct token *tokens;
445 /* double the buffer size */
446 add = rules->token_max;
450 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
453 rules->tokens = tokens;
454 rules->token_max += add;
456 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
461 static uid_t add_uid(struct udev_rules *rules, const char *owner)
467 /* lookup, if we know it already */
468 for (i = 0; i < rules->uids_cur; i++) {
469 off = rules->uids[i].name_off;
470 if (streq(rules_str(rules, off), owner)) {
471 uid = rules->uids[i].uid;
475 uid = util_lookup_user(rules->udev, owner);
477 /* grow buffer if needed */
478 if (rules->uids_cur+1 >= rules->uids_max) {
479 struct uid_gid *uids;
482 /* double the buffer size */
483 add = rules->uids_max;
487 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
491 rules->uids_max += add;
493 rules->uids[rules->uids_cur].uid = uid;
494 off = rules_add_string(rules, owner);
497 rules->uids[rules->uids_cur].name_off = off;
502 static gid_t add_gid(struct udev_rules *rules, const char *group)
508 /* lookup, if we know it already */
509 for (i = 0; i < rules->gids_cur; i++) {
510 off = rules->gids[i].name_off;
511 if (streq(rules_str(rules, off), group)) {
512 gid = rules->gids[i].gid;
516 gid = util_lookup_group(rules->udev, group);
518 /* grow buffer if needed */
519 if (rules->gids_cur+1 >= rules->gids_max) {
520 struct uid_gid *gids;
523 /* double the buffer size */
524 add = rules->gids_max;
528 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
532 rules->gids_max += add;
534 rules->gids[rules->gids_cur].gid = gid;
535 off = rules_add_string(rules, group);
538 rules->gids[rules->gids_cur].name_off = off;
543 static int import_property_from_string(struct udev_device *dev, char *line)
551 while (isspace(key[0]))
554 /* comment or empty line */
555 if (key[0] == '#' || key[0] == '\0')
558 /* split key/value */
559 val = strchr(key, '=');
566 while (isspace(val[0]))
573 while (isspace(key[len-1]))
577 /* terminate value */
581 while (isspace(val[len-1]))
589 if (val[0] == '"' || val[0] == '\'') {
590 if (val[len-1] != val[0]) {
591 log_debug("inconsistent quoting: '%s', skip\n", line);
598 /* handle device, renamed by external tool, returning new path */
599 if (streq(key, "DEVPATH")) {
600 char syspath[UTIL_PATH_SIZE];
602 log_debug("updating devpath from '%s' to '%s'\n",
603 udev_device_get_devpath(dev), val);
604 strscpyl(syspath, sizeof(syspath), "/sys", val, NULL);
605 udev_device_set_syspath(dev, syspath);
607 struct udev_list_entry *entry;
609 entry = udev_device_add_property(dev, key, val);
610 /* store in db, skip private keys */
612 udev_list_entry_set_num(entry, true);
617 static int import_file_into_properties(struct udev_device *dev, const char *filename)
620 char line[UTIL_LINE_SIZE];
622 f = fopen(filename, "re");
625 while (fgets(line, sizeof(line), f) != NULL)
626 import_property_from_string(dev, line);
631 static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
633 struct udev_device *dev = event->dev;
635 char result[UTIL_LINE_SIZE];
639 envp = udev_device_get_properties_envp(dev);
640 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
645 while (line != NULL) {
648 pos = strchr(line, '\n');
653 import_property_from_string(dev, line);
659 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
661 struct udev_device *dev_parent;
662 struct udev_list_entry *list_entry;
664 dev_parent = udev_device_get_parent(dev);
665 if (dev_parent == NULL)
668 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
669 const char *key = udev_list_entry_get_name(list_entry);
670 const char *val = udev_list_entry_get_value(list_entry);
672 if (fnmatch(filter, key, 0) == 0) {
673 struct udev_list_entry *entry;
675 entry = udev_device_add_property(dev, key, val);
676 /* store in db, skip private keys */
678 udev_list_entry_set_num(entry, true);
684 #define WAIT_LOOP_PER_SECOND 50
685 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
687 char filepath[UTIL_PATH_SIZE];
688 char devicepath[UTIL_PATH_SIZE];
690 int loop = timeout * WAIT_LOOP_PER_SECOND;
692 /* a relative path is a device attribute */
693 devicepath[0] = '\0';
694 if (file[0] != '/') {
695 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
696 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
701 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
704 if (stat(file, &stats) == 0) {
705 log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
708 /* make sure, the device did not disappear in the meantime */
709 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
710 log_debug("device disappeared while waiting for '%s'\n", file);
713 log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
714 nanosleep(&duration, NULL);
716 log_debug("waiting for '%s' failed\n", file);
720 static int attr_subst_subdir(char *attr, size_t len)
724 if (strstr(attr, "/*/")) {
726 char dirname[UTIL_PATH_SIZE];
730 strscpy(dirname, sizeof(dirname), attr);
731 pos = strstr(dirname, "/*/");
736 dir = opendir(dirname);
740 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
743 if (dent->d_name[0] == '.')
745 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
746 if (stat(attr, &stats) == 0) {
758 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
764 if (linepos == NULL || linepos[0] == '\0')
767 /* skip whitespace */
768 while (isspace(linepos[0]) || linepos[0] == ',')
772 if (linepos[0] == '\0')
778 if (linepos[0] == '\0')
780 if (isspace(linepos[0]))
782 if (linepos[0] == '=')
784 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
785 if (linepos[1] == '=')
789 /* remember end of key */
792 /* skip whitespace after key */
793 while (isspace(linepos[0]))
795 if (linepos[0] == '\0')
798 /* get operation type */
799 if (linepos[0] == '=' && linepos[1] == '=') {
802 } else if (linepos[0] == '!' && linepos[1] == '=') {
805 } else if (linepos[0] == '+' && linepos[1] == '=') {
808 } else if (linepos[0] == '=') {
811 } else if (linepos[0] == ':' && linepos[1] == '=') {
812 *op = OP_ASSIGN_FINAL;
820 /* skip whitespace after operator */
821 while (isspace(linepos[0]))
823 if (linepos[0] == '\0')
827 if (linepos[0] == '"')
834 temp = strchr(linepos, '"');
840 /* move line to next key */
845 /* extract possible KEY{attr} */
846 static const char *get_key_attribute(struct udev *udev, char *str)
851 attr = strchr(str, '{');
854 pos = strchr(attr, '}');
856 log_error("missing closing brace for format\n");
865 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
866 enum operation_type op,
867 const char *value, const void *data)
869 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
870 const char *attr = NULL;
872 memset(token, 0x00, sizeof(struct token));
884 case TK_M_SUBSYSTEMS:
888 case TK_M_IMPORT_FILE:
889 case TK_M_IMPORT_PROG:
891 case TK_M_IMPORT_CMDLINE:
892 case TK_M_IMPORT_PARENT:
902 token->key.value_off = rules_add_string(rule_tmp->rules, value);
904 case TK_M_IMPORT_BUILTIN:
905 token->key.value_off = rules_add_string(rule_tmp->rules, value);
906 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
914 token->key.value_off = rules_add_string(rule_tmp->rules, value);
915 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
918 token->key.value_off = rules_add_string(rule_tmp->rules, value);
920 token->key.mode = *(mode_t *)data;
922 case TK_A_STRING_ESCAPE_NONE:
923 case TK_A_STRING_ESCAPE_REPLACE:
924 case TK_A_DB_PERSIST:
926 case TK_A_RUN_BUILTIN:
927 case TK_A_RUN_PROGRAM:
928 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
929 token->key.value_off = rules_add_string(rule_tmp->rules, value);
931 case TK_A_INOTIFY_WATCH:
932 case TK_A_DEVLINK_PRIO:
933 token->key.devlink_prio = *(int *)data;
936 token->key.uid = *(uid_t *)data;
939 token->key.gid = *(gid_t *)data;
942 token->key.mode = *(mode_t *)data;
944 case TK_A_STATIC_NODE:
945 token->key.value_off = rules_add_string(rule_tmp->rules, value);
947 case TK_M_EVENT_TIMEOUT:
948 token->key.event_timeout = *(int *)data;
951 case TK_M_PARENTS_MIN:
952 case TK_M_PARENTS_MAX:
956 log_error("wrong type %u\n", type);
960 if (value != NULL && type < TK_M_MAX) {
961 /* check if we need to split or call fnmatch() while matching rules */
962 enum string_glob_type glob;
966 has_split = (strchr(value, '|') != NULL);
967 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
968 if (has_split && has_glob) {
969 glob = GL_SPLIT_GLOB;
970 } else if (has_split) {
972 } else if (has_glob) {
973 if (streq(value, "?*"))
980 token->key.glob = glob;
983 if (value != NULL && type > TK_M_MAX) {
984 /* check if assigned value has substitution chars */
986 token->key.subst = SB_SUBSYS;
987 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
988 token->key.subst = SB_FORMAT;
990 token->key.subst = SB_NONE;
994 /* check if property/attribut name has substitution chars */
996 token->key.attrsubst = SB_SUBSYS;
997 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
998 token->key.attrsubst = SB_FORMAT;
1000 token->key.attrsubst = SB_NONE;
1003 token->key.type = type;
1005 rule_tmp->token_cur++;
1006 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
1007 log_error("temporary rule array too small\n");
1013 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1016 unsigned int start = 0;
1017 unsigned int end = rule_tmp->token_cur;
1019 for (i = 0; i < rule_tmp->token_cur; i++) {
1020 enum token_type next_val = TK_UNSET;
1021 unsigned int next_idx = 0;
1024 /* find smallest value */
1025 for (j = start; j < end; j++) {
1026 if (rule_tmp->token[j].type == TK_UNSET)
1028 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1029 next_val = rule_tmp->token[j].type;
1034 /* add token and mark done */
1035 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1037 rule_tmp->token[next_idx].type = TK_UNSET;
1040 if (next_idx == start)
1042 if (next_idx+1 == end)
1048 static int add_rule(struct udev_rules *rules, char *line,
1049 const char *filename, unsigned int filename_off, unsigned int lineno)
1053 struct rule_tmp rule_tmp;
1055 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1056 rule_tmp.rules = rules;
1057 rule_tmp.rule.type = TK_RULE;
1058 /* the offset in the rule is limited to unsigned short */
1059 if (filename_off < USHRT_MAX)
1060 rule_tmp.rule.rule.filename_off = filename_off;
1061 rule_tmp.rule.rule.filename_line = lineno;
1067 enum operation_type op;
1069 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1070 /* If we aren't at the end of the line, this is a parsing error.
1071 * Make a best effort to describe where the problem is. */
1072 if (*linepos != '\n')
1073 log_error("invalid key/value pair in file %s on line %u,"
1074 "starting at character %lu\n",
1075 filename, lineno, linepos - line + 1);
1079 if (streq(key, "ACTION")) {
1080 if (op > OP_MATCH_MAX) {
1081 log_error("invalid ACTION operation\n");
1084 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1088 if (streq(key, "DEVPATH")) {
1089 if (op > OP_MATCH_MAX) {
1090 log_error("invalid DEVPATH operation\n");
1093 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1097 if (streq(key, "KERNEL")) {
1098 if (op > OP_MATCH_MAX) {
1099 log_error("invalid KERNEL operation\n");
1102 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1106 if (streq(key, "SUBSYSTEM")) {
1107 if (op > OP_MATCH_MAX) {
1108 log_error("invalid SUBSYSTEM operation\n");
1111 /* bus, class, subsystem events should all be the same */
1112 if (streq(value, "subsystem") ||
1113 streq(value, "bus") ||
1114 streq(value, "class")) {
1115 if (streq(value, "bus") || streq(value, "class"))
1116 log_error("'%s' must be specified as 'subsystem' \n"
1117 "please fix it in %s:%u", value, filename, lineno);
1118 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1120 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1124 if (streq(key, "DRIVER")) {
1125 if (op > OP_MATCH_MAX) {
1126 log_error("invalid DRIVER operation\n");
1129 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1133 if (startswith(key, "ATTR{")) {
1134 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1136 log_error("error parsing ATTR attribute\n");
1139 if (op < OP_MATCH_MAX) {
1140 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1142 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1147 if (streq(key, "KERNELS")) {
1148 if (op > OP_MATCH_MAX) {
1149 log_error("invalid KERNELS operation\n");
1152 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1156 if (streq(key, "SUBSYSTEMS")) {
1157 if (op > OP_MATCH_MAX) {
1158 log_error("invalid SUBSYSTEMS operation\n");
1161 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1165 if (streq(key, "DRIVERS")) {
1166 if (op > OP_MATCH_MAX) {
1167 log_error("invalid DRIVERS operation\n");
1170 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1174 if (startswith(key, "ATTRS{")) {
1175 if (op > OP_MATCH_MAX) {
1176 log_error("invalid ATTRS operation\n");
1179 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1181 log_error("error parsing ATTRS attribute\n");
1184 if (startswith(attr, "device/"))
1185 log_error("the 'device' link may not be available in a future kernel, "
1186 "please fix it in %s:%u", filename, lineno);
1187 else if (strstr(attr, "../") != NULL)
1188 log_error("do not reference parent sysfs directories directly, "
1189 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1190 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1194 if (streq(key, "TAGS")) {
1195 if (op > OP_MATCH_MAX) {
1196 log_error("invalid TAGS operation\n");
1199 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1203 if (startswith(key, "ENV{")) {
1204 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1206 log_error("error parsing ENV attribute\n");
1209 if (op < OP_MATCH_MAX) {
1210 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1213 static const char *blacklist[] = {
1228 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1229 if (!streq(attr, blacklist[i]))
1231 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
1234 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1240 if (streq(key, "TAG")) {
1241 if (op < OP_MATCH_MAX)
1242 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1244 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1248 if (streq(key, "PROGRAM")) {
1249 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1253 if (streq(key, "RESULT")) {
1254 if (op > OP_MATCH_MAX) {
1255 log_error("invalid RESULT operation\n");
1258 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1262 if (startswith(key, "IMPORT")) {
1263 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1265 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1268 if (streq(attr, "program")) {
1269 /* find known built-in command */
1270 if (value[0] != '/') {
1271 enum udev_builtin_cmd cmd;
1273 cmd = udev_builtin_lookup(value);
1274 if (cmd < UDEV_BUILTIN_MAX) {
1275 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1276 value, filename, lineno);
1277 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1281 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1282 } else if (streq(attr, "builtin")) {
1283 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1285 if (cmd < UDEV_BUILTIN_MAX)
1286 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1288 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1289 } else if (streq(attr, "file")) {
1290 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1291 } else if (streq(attr, "db")) {
1292 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1293 } else if (streq(attr, "cmdline")) {
1294 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1295 } else if (streq(attr, "parent")) {
1296 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1298 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno);
1302 if (startswith(key, "TEST")) {
1305 if (op > OP_MATCH_MAX) {
1306 log_error("invalid TEST operation\n");
1309 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1311 mode = strtol(attr, NULL, 8);
1312 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1314 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1319 if (startswith(key, "RUN")) {
1320 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1324 if (streq(attr, "builtin")) {
1325 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1327 if (cmd < UDEV_BUILTIN_MAX)
1328 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1330 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1331 } else if (streq(attr, "program")) {
1332 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1334 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1336 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno);
1342 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1343 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1347 if (streq(key, "LABEL")) {
1348 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1352 if (streq(key, "GOTO")) {
1353 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1357 if (startswith(key, "NAME")) {
1358 if (op < OP_MATCH_MAX) {
1359 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1361 if (streq(value, "%k")) {
1362 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1363 "please remove it from %s:%u\n", filename, lineno);
1366 if (value[0] == '\0') {
1367 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1368 "please remove it from %s:%u\n", filename, lineno);
1371 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1373 rule_tmp.rule.rule.can_set_name = true;
1377 if (streq(key, "SYMLINK")) {
1378 if (op < OP_MATCH_MAX)
1379 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1381 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1382 rule_tmp.rule.rule.can_set_name = true;
1386 if (streq(key, "OWNER")) {
1390 uid = strtoul(value, &endptr, 10);
1391 if (endptr[0] == '\0') {
1392 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1393 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1394 uid = add_uid(rules, value);
1395 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1396 } else if (rules->resolve_names >= 0) {
1397 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1399 rule_tmp.rule.rule.can_set_name = true;
1403 if (streq(key, "GROUP")) {
1407 gid = strtoul(value, &endptr, 10);
1408 if (endptr[0] == '\0') {
1409 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1410 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1411 gid = add_gid(rules, value);
1412 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1413 } else if (rules->resolve_names >= 0) {
1414 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1416 rule_tmp.rule.rule.can_set_name = true;
1420 if (streq(key, "MODE")) {
1424 mode = strtol(value, &endptr, 8);
1425 if (endptr[0] == '\0')
1426 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1428 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1429 rule_tmp.rule.rule.can_set_name = true;
1433 if (streq(key, "OPTIONS")) {
1436 pos = strstr(value, "link_priority=");
1438 int prio = atoi(&pos[strlen("link_priority=")]);
1440 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1443 pos = strstr(value, "event_timeout=");
1445 int tout = atoi(&pos[strlen("event_timeout=")]);
1447 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
1450 pos = strstr(value, "string_escape=");
1452 pos = &pos[strlen("string_escape=")];
1453 if (startswith(pos, "none"))
1454 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1455 else if (startswith(pos, "replace"))
1456 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1459 pos = strstr(value, "db_persist");
1461 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1463 pos = strstr(value, "nowatch");
1467 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1469 pos = strstr(value, "watch");
1473 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1477 pos = strstr(value, "static_node=");
1479 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1480 rule_tmp.rule.rule.has_static_node = true;
1486 log_error("unknown key '%s' in %s:%u\n", key, filename, lineno);
1490 /* add rule token */
1491 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1492 if (add_token(rules, &rule_tmp.rule) != 0)
1495 /* add tokens to list, sorted by type */
1496 if (sort_token(rules, &rule_tmp) != 0)
1501 log_error("invalid rule '%s:%u'\n", filename, lineno);
1505 static int parse_file(struct udev_rules *rules, const char *filename)
1508 unsigned int first_token;
1509 unsigned int filename_off;
1510 char line[UTIL_LINE_SIZE];
1514 if (null_or_empty_path(filename)) {
1515 log_debug("skip empty file: %s\n", filename);
1518 log_debug("read rules file: %s\n", filename);
1520 f = fopen(filename, "re");
1524 first_token = rules->token_cur;
1525 filename_off = rules_add_string(rules, filename);
1527 while (fgets(line, sizeof(line), f) != NULL) {
1531 /* skip whitespace */
1534 while (isspace(key[0]))
1545 /* continue reading if backslash+newline is found */
1546 while (line[len-2] == '\\') {
1547 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1549 if (strlen(&line[len-2]) < 2)
1555 if (len+1 >= sizeof(line)) {
1556 log_error("line too long '%s':%u, ignored\n", filename, line_nr);
1559 add_rule(rules, key, filename, filename_off, line_nr);
1563 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1564 for (i = first_token+1; i < rules->token_cur; i++) {
1565 if (rules->tokens[i].type == TK_A_GOTO) {
1566 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1569 for (j = i+1; j < rules->token_cur; j++) {
1570 if (rules->tokens[j].type != TK_RULE)
1572 if (rules->tokens[j].rule.label_off == 0)
1574 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1576 rules->tokens[i].key.rule_goto = j;
1579 if (rules->tokens[i].key.rule_goto == 0)
1580 log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename);
1586 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1588 struct udev_rules *rules;
1589 struct udev_list file_list;
1590 struct token end_token;
1594 rules = calloc(1, sizeof(struct udev_rules));
1598 rules->resolve_names = resolve_names;
1599 udev_list_init(udev, &file_list, true);
1601 /* init token array and string buffer */
1602 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1603 if (rules->tokens == NULL)
1604 return udev_rules_unref(rules);
1605 rules->token_max = PREALLOC_TOKEN;
1607 rules->strbuf = strbuf_new();
1609 return udev_rules_unref(rules);
1611 rules->dirs = strv_new("/etc/udev/rules.d",
1612 "/run/udev/rules.d",
1613 UDEVLIBEXECDIR "/rules.d",
1616 log_error("failed to build config directory array");
1617 return udev_rules_unref(rules);
1619 if (!path_strv_canonicalize(rules->dirs)) {
1620 log_error("failed to canonicalize config directories\n");
1621 return udev_rules_unref(rules);
1623 strv_uniq(rules->dirs);
1625 rules->dirs_ts_usec = calloc(strv_length(rules->dirs), sizeof(usec_t));
1626 if(!rules->dirs_ts_usec)
1627 return udev_rules_unref(rules);
1628 udev_rules_check_timestamp(rules);
1630 r = conf_files_list_strv(&files, ".rules", NULL, (const char **)rules->dirs);
1632 log_error("failed to enumerate rules files: %s\n", strerror(-r));
1633 return udev_rules_unref(rules);
1637 * The offset value in the rules strct is limited; add all
1638 * rules file names to the beginning of the string buffer.
1640 STRV_FOREACH(f, files)
1641 rules_add_string(rules, *f);
1643 STRV_FOREACH(f, files)
1644 parse_file(rules, *f);
1648 memset(&end_token, 0x00, sizeof(struct token));
1649 end_token.type = TK_END;
1650 add_token(rules, &end_token);
1651 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings\n",
1652 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1654 /* cleanup temporary strbuf data */
1655 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used\n",
1656 rules->strbuf->in_count, rules->strbuf->in_len,
1657 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1658 strbuf_complete(rules->strbuf);
1660 /* cleanup uid/gid cache */
1663 rules->uids_cur = 0;
1664 rules->uids_max = 0;
1667 rules->gids_cur = 0;
1668 rules->gids_max = 0;
1674 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1678 free(rules->tokens);
1679 strbuf_cleanup(rules->strbuf);
1682 strv_free(rules->dirs);
1683 free(rules->dirs_ts_usec);
1688 bool udev_rules_check_timestamp(struct udev_rules *rules)
1691 bool changed = false;
1696 for (i = 0; rules->dirs[i]; i++) {
1699 if (stat(rules->dirs[i], &stats) < 0)
1702 if (rules->dirs_ts_usec[i] == timespec_load(&stats.st_mtim))
1706 if (rules->dirs_ts_usec[i] != 0) {
1707 log_debug("reload - timestamp of '%s' changed\n", rules->dirs[i]);
1711 /* update timestamp */
1712 rules->dirs_ts_usec[i] = timespec_load(&stats.st_mtim);
1718 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1720 char *key_value = rules_str(rules, token->key.value_off);
1727 switch (token->key.glob) {
1729 match = (streq(key_value, val));
1732 match = (fnmatch(key_value, val, 0) == 0);
1739 s = rules_str(rules, token->key.value_off);
1744 next = strchr(s, '|');
1746 size_t matchlen = (size_t)(next - s);
1748 match = (matchlen == len && strneq(s, val, matchlen));
1752 match = (streq(s, val));
1761 char value[UTIL_PATH_SIZE];
1763 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1765 while (key_value != NULL) {
1766 pos = strchr(key_value, '|');
1771 match = (fnmatch(key_value, val, 0) == 0);
1779 match = (val[0] != '\0');
1785 if (match && (token->key.op == OP_MATCH))
1787 if (!match && (token->key.op == OP_NOMATCH))
1792 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1795 char nbuf[UTIL_NAME_SIZE];
1797 char vbuf[UTIL_NAME_SIZE];
1800 name = rules_str(rules, cur->key.attr_off);
1801 switch (cur->key.attrsubst) {
1803 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1807 value = udev_device_get_sysattr_value(dev, name);
1812 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1820 /* remove trailing whitespace, if not asked to match for it */
1821 len = strlen(value);
1822 if (len > 0 && isspace(value[len-1])) {
1823 const char *key_value;
1826 key_value = rules_str(rules, cur->key.value_off);
1827 klen = strlen(key_value);
1828 if (klen > 0 && !isspace(key_value[klen-1])) {
1829 if (value != vbuf) {
1830 strscpy(vbuf, sizeof(vbuf), value);
1833 while (len > 0 && isspace(vbuf[--len]))
1838 return match_key(rules, cur, value);
1847 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
1851 enum escape_type esc = ESCAPE_UNSET;
1854 if (rules->tokens == NULL)
1857 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1858 (major(udev_device_get_devnum(event->dev)) > 0 ||
1859 udev_device_get_ifindex(event->dev) > 0));
1861 /* loop through token list, match, run actions or forward to next rule */
1862 cur = &rules->tokens[0];
1865 dump_token(rules, cur);
1866 switch (cur->type) {
1870 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1871 if (!can_set_name && rule->rule.can_set_name)
1876 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1880 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1884 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1887 case TK_M_DEVLINK: {
1888 struct udev_list_entry *list_entry;
1891 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1892 const char *devlink;
1894 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1895 if (match_key(rules, cur, devlink) == 0) {
1905 if (match_key(rules, cur, event->name) != 0)
1909 const char *key_name = rules_str(rules, cur->key.attr_off);
1912 value = udev_device_get_property_value(event->dev, key_name);
1915 if (match_key(rules, cur, value))
1920 struct udev_list_entry *list_entry;
1923 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1924 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1929 if (!match && (cur->key.op != OP_NOMATCH))
1933 case TK_M_SUBSYSTEM:
1934 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1938 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1941 case TK_M_WAITFOR: {
1942 char filename[UTIL_PATH_SIZE];
1945 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1946 found = (wait_for_file(event->dev, filename, 10) == 0);
1947 if (!found && (cur->key.op != OP_NOMATCH))
1952 if (match_attr(rules, event->dev, event, cur) != 0)
1956 case TK_M_SUBSYSTEMS:
1962 /* get whole sequence of parent matches */
1964 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1967 /* loop over parents */
1968 event->dev_parent = event->dev;
1972 /* loop over sequence of parent match keys */
1973 for (key = cur; key < next; key++ ) {
1974 dump_token(rules, key);
1977 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1980 case TK_M_SUBSYSTEMS:
1981 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1985 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1989 if (match_attr(rules, event->dev_parent, event, key) != 0)
1993 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
1995 if (match && key->key.op == OP_NOMATCH)
1997 if (!match && key->key.op == OP_MATCH)
2008 event->dev_parent = udev_device_get_parent(event->dev_parent);
2009 if (event->dev_parent == NULL)
2012 /* move behind our sequence of parent match keys */
2017 char filename[UTIL_PATH_SIZE];
2018 struct stat statbuf;
2021 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
2022 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2023 if (filename[0] != '/') {
2024 char tmp[UTIL_PATH_SIZE];
2026 strscpy(tmp, sizeof(tmp), filename);
2027 strscpyl(filename, sizeof(filename),
2028 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2031 attr_subst_subdir(filename, sizeof(filename));
2033 match = (stat(filename, &statbuf) == 0);
2034 if (match && cur->key.mode > 0)
2035 match = ((statbuf.st_mode & cur->key.mode) > 0);
2036 if (match && cur->key.op == OP_NOMATCH)
2038 if (!match && cur->key.op == OP_MATCH)
2042 case TK_M_EVENT_TIMEOUT:
2043 log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout);
2044 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2046 case TK_M_PROGRAM: {
2047 char program[UTIL_PATH_SIZE];
2049 char result[UTIL_PATH_SIZE];
2051 free(event->program_result);
2052 event->program_result = NULL;
2053 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2054 envp = udev_device_get_properties_envp(event->dev);
2055 log_debug("PROGRAM '%s' %s:%u\n",
2057 rules_str(rules, rule->rule.filename_off),
2058 rule->rule.filename_line);
2060 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2061 if (cur->key.op != OP_NOMATCH)
2066 util_remove_trailing_chars(result, '\n');
2067 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2068 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2070 log_debug("%i character(s) replaced\n" , count);
2072 event->program_result = strdup(result);
2073 if (cur->key.op == OP_NOMATCH)
2078 case TK_M_IMPORT_FILE: {
2079 char import[UTIL_PATH_SIZE];
2081 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2082 if (import_file_into_properties(event->dev, import) != 0)
2083 if (cur->key.op != OP_NOMATCH)
2087 case TK_M_IMPORT_PROG: {
2088 char import[UTIL_PATH_SIZE];
2090 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2091 log_debug("IMPORT '%s' %s:%u\n",
2093 rules_str(rules, rule->rule.filename_off),
2094 rule->rule.filename_line);
2096 if (import_program_into_properties(event, import, sigmask) != 0)
2097 if (cur->key.op != OP_NOMATCH)
2101 case TK_M_IMPORT_BUILTIN: {
2102 char command[UTIL_PATH_SIZE];
2104 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2105 /* check if we ran already */
2106 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2107 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2108 udev_builtin_name(cur->key.builtin_cmd),
2109 rules_str(rules, rule->rule.filename_off),
2110 rule->rule.filename_line);
2111 /* return the result from earlier run */
2112 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2113 if (cur->key.op != OP_NOMATCH)
2118 event->builtin_run |= (1 << cur->key.builtin_cmd);
2121 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2122 log_debug("IMPORT builtin '%s' %s:%u\n",
2123 udev_builtin_name(cur->key.builtin_cmd),
2124 rules_str(rules, rule->rule.filename_off),
2125 rule->rule.filename_line);
2127 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2128 /* remember failure */
2129 log_debug("IMPORT builtin '%s' returned non-zero\n",
2130 udev_builtin_name(cur->key.builtin_cmd));
2131 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2132 if (cur->key.op != OP_NOMATCH)
2137 case TK_M_IMPORT_DB: {
2138 const char *key = rules_str(rules, cur->key.value_off);
2141 value = udev_device_get_property_value(event->dev_db, key);
2142 if (value != NULL) {
2143 struct udev_list_entry *entry;
2145 entry = udev_device_add_property(event->dev, key, value);
2146 udev_list_entry_set_num(entry, true);
2148 if (cur->key.op != OP_NOMATCH)
2153 case TK_M_IMPORT_CMDLINE: {
2155 bool imported = false;
2157 f = fopen("/proc/cmdline", "re");
2161 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2162 const char *key = rules_str(rules, cur->key.value_off);
2165 pos = strstr(cmdline, key);
2167 struct udev_list_entry *entry;
2170 if (pos[0] == '\0' || isspace(pos[0])) {
2171 /* we import simple flags as 'FLAG=1' */
2172 entry = udev_device_add_property(event->dev, key, "1");
2173 udev_list_entry_set_num(entry, true);
2175 } else if (pos[0] == '=') {
2180 while (pos[0] != '\0' && !isspace(pos[0]))
2183 entry = udev_device_add_property(event->dev, key, value);
2184 udev_list_entry_set_num(entry, true);
2191 if (!imported && cur->key.op != OP_NOMATCH)
2195 case TK_M_IMPORT_PARENT: {
2196 char import[UTIL_PATH_SIZE];
2198 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2199 if (import_parent_into_properties(event->dev, import) != 0)
2200 if (cur->key.op != OP_NOMATCH)
2205 if (match_key(rules, cur, event->program_result) != 0)
2208 case TK_A_STRING_ESCAPE_NONE:
2211 case TK_A_STRING_ESCAPE_REPLACE:
2212 esc = ESCAPE_REPLACE;
2214 case TK_A_DB_PERSIST:
2215 udev_device_set_db_persist(event->dev);
2217 case TK_A_INOTIFY_WATCH:
2218 if (event->inotify_watch_final)
2220 if (cur->key.op == OP_ASSIGN_FINAL)
2221 event->inotify_watch_final = true;
2222 event->inotify_watch = cur->key.watch;
2224 case TK_A_DEVLINK_PRIO:
2225 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2228 char owner[UTIL_NAME_SIZE];
2230 if (event->owner_final)
2232 if (cur->key.op == OP_ASSIGN_FINAL)
2233 event->owner_final = true;
2234 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2235 event->owner_set = true;
2236 event->uid = util_lookup_user(event->udev, owner);
2237 log_debug("OWNER %u %s:%u\n",
2239 rules_str(rules, rule->rule.filename_off),
2240 rule->rule.filename_line);
2244 char group[UTIL_NAME_SIZE];
2246 if (event->group_final)
2248 if (cur->key.op == OP_ASSIGN_FINAL)
2249 event->group_final = true;
2250 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2251 event->group_set = true;
2252 event->gid = util_lookup_group(event->udev, group);
2253 log_debug("GROUP %u %s:%u\n",
2255 rules_str(rules, rule->rule.filename_off),
2256 rule->rule.filename_line);
2260 char mode_str[UTIL_NAME_SIZE];
2264 if (event->mode_final)
2266 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2267 mode = strtol(mode_str, &endptr, 8);
2268 if (endptr[0] != '\0') {
2269 log_error("ignoring invalid mode '%s'\n", mode_str);
2272 if (cur->key.op == OP_ASSIGN_FINAL)
2273 event->mode_final = true;
2274 event->mode_set = true;
2276 log_debug("MODE %#o %s:%u\n",
2278 rules_str(rules, rule->rule.filename_off),
2279 rule->rule.filename_line);
2283 if (event->owner_final)
2285 if (cur->key.op == OP_ASSIGN_FINAL)
2286 event->owner_final = true;
2287 event->owner_set = true;
2288 event->uid = cur->key.uid;
2289 log_debug("OWNER %u %s:%u\n",
2291 rules_str(rules, rule->rule.filename_off),
2292 rule->rule.filename_line);
2295 if (event->group_final)
2297 if (cur->key.op == OP_ASSIGN_FINAL)
2298 event->group_final = true;
2299 event->group_set = true;
2300 event->gid = cur->key.gid;
2301 log_debug("GROUP %u %s:%u\n",
2303 rules_str(rules, rule->rule.filename_off),
2304 rule->rule.filename_line);
2307 if (event->mode_final)
2309 if (cur->key.op == OP_ASSIGN_FINAL)
2310 event->mode_final = true;
2311 event->mode_set = true;
2312 event->mode = cur->key.mode;
2313 log_debug("MODE %#o %s:%u\n",
2315 rules_str(rules, rule->rule.filename_off),
2316 rule->rule.filename_line);
2319 const char *name = rules_str(rules, cur->key.attr_off);
2320 char *value = rules_str(rules, cur->key.value_off);
2321 char value_new[UTIL_NAME_SIZE];
2322 const char *value_old = NULL;
2323 struct udev_list_entry *entry;
2325 if (value[0] == '\0') {
2326 if (cur->key.op == OP_ADD)
2328 udev_device_add_property(event->dev, name, NULL);
2332 if (cur->key.op == OP_ADD)
2333 value_old = udev_device_get_property_value(event->dev, name);
2335 char temp[UTIL_NAME_SIZE];
2337 /* append value separated by space */
2338 udev_event_apply_format(event, value, temp, sizeof(temp));
2339 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2341 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2343 entry = udev_device_add_property(event->dev, name, value_new);
2344 /* store in db, skip private keys */
2346 udev_list_entry_set_num(entry, true);
2350 char tag[UTIL_PATH_SIZE];
2353 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2354 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2355 udev_device_cleanup_tags_list(event->dev);
2356 for (p = tag; *p != '\0'; p++) {
2357 if ((*p >= 'a' && *p <= 'z') ||
2358 (*p >= 'A' && *p <= 'Z') ||
2359 (*p >= '0' && *p <= '9') ||
2360 *p == '-' || *p == '_')
2362 log_error("ignoring invalid tag name '%s'\n", tag);
2365 udev_device_add_tag(event->dev, tag);
2369 const char *name = rules_str(rules, cur->key.value_off);
2371 char name_str[UTIL_PATH_SIZE];
2374 if (event->name_final)
2376 if (cur->key.op == OP_ASSIGN_FINAL)
2377 event->name_final = true;
2378 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2379 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2380 count = util_replace_chars(name_str, "/");
2382 log_debug("%i character(s) replaced\n", count);
2384 if (major(udev_device_get_devnum(event->dev)) &&
2385 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2386 log_error("NAME=\"%s\" ignored, kernel device nodes "
2387 "can not be renamed; please fix it in %s:%u\n", name,
2388 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2392 event->name = strdup(name_str);
2393 log_debug("NAME '%s' %s:%u\n",
2395 rules_str(rules, rule->rule.filename_off),
2396 rule->rule.filename_line);
2399 case TK_A_DEVLINK: {
2400 char temp[UTIL_PATH_SIZE];
2401 char filename[UTIL_PATH_SIZE];
2405 if (event->devlink_final)
2407 if (major(udev_device_get_devnum(event->dev)) == 0)
2409 if (cur->key.op == OP_ASSIGN_FINAL)
2410 event->devlink_final = true;
2411 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2412 udev_device_cleanup_devlinks_list(event->dev);
2414 /* allow multiple symlinks separated by spaces */
2415 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2416 if (esc == ESCAPE_UNSET)
2417 count = util_replace_chars(temp, "/ ");
2418 else if (esc == ESCAPE_REPLACE)
2419 count = util_replace_chars(temp, "/");
2421 log_debug("%i character(s) replaced\n" , count);
2423 while (isspace(pos[0]))
2425 next = strchr(pos, ' ');
2426 while (next != NULL) {
2428 log_debug("LINK '%s' %s:%u\n", pos,
2429 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2430 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2431 udev_device_add_devlink(event->dev, filename);
2432 while (isspace(next[1]))
2435 next = strchr(pos, ' ');
2437 if (pos[0] != '\0') {
2438 log_debug("LINK '%s' %s:%u\n", pos,
2439 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2440 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2441 udev_device_add_devlink(event->dev, filename);
2446 const char *key_name = rules_str(rules, cur->key.attr_off);
2447 char attr[UTIL_PATH_SIZE];
2448 char value[UTIL_NAME_SIZE];
2451 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2452 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2453 attr_subst_subdir(attr, sizeof(attr));
2455 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2456 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
2457 rules_str(rules, rule->rule.filename_off),
2458 rule->rule.filename_line);
2459 f = fopen(attr, "we");
2461 if (fprintf(f, "%s", value) <= 0)
2462 log_error("error writing ATTR{%s}: %m\n", attr);
2465 log_error("error opening ATTR{%s} for writing: %m\n", attr);
2469 case TK_A_RUN_BUILTIN:
2470 case TK_A_RUN_PROGRAM: {
2471 struct udev_list_entry *entry;
2473 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2474 udev_list_cleanup(&event->run_list);
2475 log_debug("RUN '%s' %s:%u\n",
2476 rules_str(rules, cur->key.value_off),
2477 rules_str(rules, rule->rule.filename_off),
2478 rule->rule.filename_line);
2479 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2480 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2484 if (cur->key.rule_goto == 0)
2486 cur = &rules->tokens[cur->key.rule_goto];
2491 case TK_M_PARENTS_MIN:
2492 case TK_M_PARENTS_MAX:
2495 log_error("wrong type %u\n", cur->type);
2502 /* fast-forward to next rule */
2503 cur = rule + rule->rule.token_count;
2507 int udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2514 _cleanup_strv_free_ char **tags = NULL;
2517 _cleanup_free_ char *path = NULL;
2520 if (rules->tokens == NULL)
2523 cur = &rules->tokens[0];
2526 switch (cur->type) {
2531 /* skip rules without a static_node tag */
2532 if (!rule->rule.has_static_node)
2548 mode = cur->key.mode;
2551 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2556 case TK_A_STATIC_NODE: {
2557 char device_node[UTIL_PATH_SIZE];
2558 char tags_dir[UTIL_PATH_SIZE];
2559 char tag_symlink[UTIL_PATH_SIZE];
2562 /* we assure, that the permissions tokens are sorted before the static token */
2563 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2565 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2566 if (stat(device_node, &stats) != 0)
2568 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2572 /* Export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2574 STRV_FOREACH(t, tags) {
2575 _cleanup_free_ char *unescaped_filename = NULL;
2577 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2578 r = mkdir_p(tags_dir, 0755);
2580 log_error("failed to create %s: %s\n", tags_dir, strerror(-r));
2584 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2586 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2587 r = symlink(device_node, tag_symlink);
2588 if (r < 0 && errno != EEXIST) {
2589 log_error("failed to create symlink %s -> %s: %s\n", tag_symlink, device_node, strerror(errno));
2596 /* don't touch the permissions if only the tags were set */
2597 if (mode == 0 && uid == 0 && gid == 0)
2606 if (mode != (stats.st_mode & 01777)) {
2607 r = chmod(device_node, mode);
2609 log_error("failed to chmod '%s' %#o\n", device_node, mode);
2612 log_debug("chmod '%s' %#o\n", device_node, mode);
2615 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2616 r = chown(device_node, uid, gid);
2618 log_error("failed to chown '%s' %u %u \n", device_node, uid, gid);
2621 log_debug("chown '%s' %u %u\n", device_node, uid, gid);
2624 utimensat(AT_FDCWD, device_node, NULL, 0);
2634 /* fast-forward to next rule */
2635 cur = rule + rule->rule.token_count;
2642 fchmod(fileno(f), 0644);
2643 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2645 unlink("/run/udev/static_node-tags");