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_ENV, /* val, attr */
161 TK_A_DEVLINK, /* val */
162 TK_A_ATTR, /* val, attr */
163 TK_A_RUN_BUILTIN, /* val, bool */
164 TK_A_RUN_PROGRAM, /* val, bool */
165 TK_A_GOTO, /* size_t */
170 /* we try to pack stuff in a way that we take only 12 bytes per token */
173 unsigned char type; /* same in rule and key */
175 enum token_type type:8;
177 bool has_static_node:1;
178 unsigned int unused:6;
179 unsigned short token_count;
180 unsigned int label_off;
181 unsigned short filename_off;
182 unsigned short filename_line;
185 enum token_type type:8;
186 enum operation_type op:8;
187 enum string_glob_type glob:8;
188 enum string_subst_type subst:4;
189 enum string_subst_type attrsubst:4;
190 unsigned int value_off;
192 unsigned int attr_off;
193 unsigned int rule_goto;
200 enum udev_builtin_cmd builtin_cmd;
208 struct udev_rules *rules;
210 struct token token[MAX_TK];
211 unsigned int token_cur;
215 static const char *operation_str(enum operation_type type)
217 static const char *operation_strs[] = {
218 [OP_UNSET] = "UNSET",
219 [OP_MATCH] = "match",
220 [OP_NOMATCH] = "nomatch",
221 [OP_MATCH_MAX] = "MATCH_MAX",
224 [OP_ASSIGN] = "assign",
225 [OP_ASSIGN_FINAL] = "assign-final",
228 return operation_strs[type];
231 static const char *string_glob_str(enum string_glob_type type)
233 static const char *string_glob_strs[] = {
234 [GL_UNSET] = "UNSET",
235 [GL_PLAIN] = "plain",
237 [GL_SPLIT] = "split",
238 [GL_SPLIT_GLOB] = "split-glob",
239 [GL_SOMETHING] = "split-glob",
242 return string_glob_strs[type];
245 static const char *token_str(enum token_type type)
247 static const char *token_strs[] = {
248 [TK_UNSET] = "UNSET",
251 [TK_M_ACTION] = "M ACTION",
252 [TK_M_DEVPATH] = "M DEVPATH",
253 [TK_M_KERNEL] = "M KERNEL",
254 [TK_M_DEVLINK] = "M DEVLINK",
255 [TK_M_NAME] = "M NAME",
256 [TK_M_ENV] = "M ENV",
257 [TK_M_TAG] = "M TAG",
258 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
259 [TK_M_DRIVER] = "M DRIVER",
260 [TK_M_WAITFOR] = "M WAITFOR",
261 [TK_M_ATTR] = "M ATTR",
263 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
264 [TK_M_KERNELS] = "M KERNELS",
265 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
266 [TK_M_DRIVERS] = "M DRIVERS",
267 [TK_M_ATTRS] = "M ATTRS",
268 [TK_M_TAGS] = "M TAGS",
269 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
271 [TK_M_TEST] = "M TEST",
272 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
273 [TK_M_PROGRAM] = "M PROGRAM",
274 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
275 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
276 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
277 [TK_M_IMPORT_DB] = "M IMPORT_DB",
278 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
279 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
280 [TK_M_RESULT] = "M RESULT",
281 [TK_M_MAX] = "M MAX",
283 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
284 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
285 [TK_A_DB_PERSIST] = "A DB_PERSIST",
286 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
287 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
288 [TK_A_OWNER] = "A OWNER",
289 [TK_A_GROUP] = "A GROUP",
290 [TK_A_MODE] = "A MODE",
291 [TK_A_OWNER_ID] = "A OWNER_ID",
292 [TK_A_GROUP_ID] = "A GROUP_ID",
293 [TK_A_STATIC_NODE] = "A STATIC_NODE",
294 [TK_A_MODE_ID] = "A MODE_ID",
295 [TK_A_ENV] = "A ENV",
296 [TK_A_TAG] = "A ENV",
297 [TK_A_NAME] = "A NAME",
298 [TK_A_DEVLINK] = "A DEVLINK",
299 [TK_A_ATTR] = "A ATTR",
300 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
301 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
302 [TK_A_GOTO] = "A GOTO",
307 return token_strs[type];
310 static void dump_token(struct udev_rules *rules, struct token *token)
312 enum token_type type = token->type;
313 enum operation_type op = token->key.op;
314 enum string_glob_type glob = token->key.glob;
315 const char *value = str(rules, token->key.value_off);
316 const char *attr = &rules->buf[token->key.attr_off];
321 const char *tks_ptr = (char *)rules->tokens;
322 const char *tk_ptr = (char *)token;
323 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
325 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
326 &rules->buf[token->rule.filename_off], token->rule.filename_line,
327 idx, token->rule.token_count,
328 &rules->buf[token->rule.label_off]);
340 case TK_M_SUBSYSTEMS:
344 case TK_M_IMPORT_FILE:
345 case TK_M_IMPORT_PROG:
347 case TK_M_IMPORT_CMDLINE:
348 case TK_M_IMPORT_PARENT:
355 case TK_A_RUN_BUILTIN:
356 case TK_A_RUN_PROGRAM:
357 log_debug("%s %s '%s'(%s)\n",
358 token_str(type), operation_str(op), value, string_glob_str(glob));
360 case TK_M_IMPORT_BUILTIN:
361 log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
368 log_debug("%s %s '%s' '%s'(%s)\n",
369 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
373 log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value);
375 case TK_A_STRING_ESCAPE_NONE:
376 case TK_A_STRING_ESCAPE_REPLACE:
377 case TK_A_DB_PERSIST:
378 log_debug("%s\n", token_str(type));
381 log_debug("%s %s '%s'(%s) %#o\n",
382 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
384 case TK_A_INOTIFY_WATCH:
385 log_debug("%s %u\n", token_str(type), token->key.watch);
387 case TK_A_DEVLINK_PRIO:
388 log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
391 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
394 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
397 log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
399 case TK_A_STATIC_NODE:
400 log_debug("%s '%s'\n", token_str(type), value);
402 case TK_M_EVENT_TIMEOUT:
403 log_debug("%s %u\n", token_str(type), token->key.event_timeout);
406 log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
409 log_debug("* %s\n", token_str(type));
411 case TK_M_PARENTS_MIN:
412 case TK_M_PARENTS_MAX:
415 log_debug("unknown type %u\n", type);
420 static void dump_rules(struct udev_rules *rules)
424 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
426 rules->token_cur * sizeof(struct token),
429 for(i = 0; i < rules->token_cur; i++)
430 dump_token(rules, &rules->tokens[i]);
433 static inline const char *operation_str(enum operation_type type) { return NULL; }
434 static inline const char *token_str(enum token_type type) { return NULL; }
435 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
436 static inline void dump_rules(struct udev_rules *rules) {}
439 static int add_token(struct udev_rules *rules, struct token *token)
441 /* grow buffer if needed */
442 if (rules->token_cur+1 >= rules->token_max) {
443 struct token *tokens;
446 /* double the buffer size */
447 add = rules->token_max;
451 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
454 rules->tokens = tokens;
455 rules->token_max += add;
457 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
462 static uid_t add_uid(struct udev_rules *rules, const char *owner)
468 /* lookup, if we know it already */
469 for (i = 0; i < rules->uids_cur; i++) {
470 off = rules->uids[i].name_off;
471 if (streq(rules_str(rules, off), owner)) {
472 uid = rules->uids[i].uid;
476 uid = util_lookup_user(rules->udev, owner);
478 /* grow buffer if needed */
479 if (rules->uids_cur+1 >= rules->uids_max) {
480 struct uid_gid *uids;
483 /* double the buffer size */
484 add = rules->uids_max;
488 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
492 rules->uids_max += add;
494 rules->uids[rules->uids_cur].uid = uid;
495 off = rules_add_string(rules, owner);
498 rules->uids[rules->uids_cur].name_off = off;
503 static gid_t add_gid(struct udev_rules *rules, const char *group)
509 /* lookup, if we know it already */
510 for (i = 0; i < rules->gids_cur; i++) {
511 off = rules->gids[i].name_off;
512 if (streq(rules_str(rules, off), group)) {
513 gid = rules->gids[i].gid;
517 gid = util_lookup_group(rules->udev, group);
519 /* grow buffer if needed */
520 if (rules->gids_cur+1 >= rules->gids_max) {
521 struct uid_gid *gids;
524 /* double the buffer size */
525 add = rules->gids_max;
529 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
533 rules->gids_max += add;
535 rules->gids[rules->gids_cur].gid = gid;
536 off = rules_add_string(rules, group);
539 rules->gids[rules->gids_cur].name_off = off;
544 static int import_property_from_string(struct udev_device *dev, char *line)
552 while (isspace(key[0]))
555 /* comment or empty line */
556 if (key[0] == '#' || key[0] == '\0')
559 /* split key/value */
560 val = strchr(key, '=');
567 while (isspace(val[0]))
574 while (isspace(key[len-1]))
578 /* terminate value */
582 while (isspace(val[len-1]))
590 if (val[0] == '"' || val[0] == '\'') {
591 if (val[len-1] != val[0]) {
592 log_debug("inconsistent quoting: '%s', skip\n", line);
599 /* handle device, renamed by external tool, returning new path */
600 if (streq(key, "DEVPATH")) {
601 char syspath[UTIL_PATH_SIZE];
603 log_debug("updating devpath from '%s' to '%s'\n",
604 udev_device_get_devpath(dev), val);
605 strscpyl(syspath, sizeof(syspath), "/sys", val, NULL);
606 udev_device_set_syspath(dev, syspath);
608 struct udev_list_entry *entry;
610 entry = udev_device_add_property(dev, key, val);
611 /* store in db, skip private keys */
613 udev_list_entry_set_num(entry, true);
618 static int import_file_into_properties(struct udev_device *dev, const char *filename)
621 char line[UTIL_LINE_SIZE];
623 f = fopen(filename, "re");
626 while (fgets(line, sizeof(line), f) != NULL)
627 import_property_from_string(dev, line);
632 static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
634 struct udev_device *dev = event->dev;
636 char result[UTIL_LINE_SIZE];
640 envp = udev_device_get_properties_envp(dev);
641 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
646 while (line != NULL) {
649 pos = strchr(line, '\n');
654 import_property_from_string(dev, line);
660 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
662 struct udev_device *dev_parent;
663 struct udev_list_entry *list_entry;
665 dev_parent = udev_device_get_parent(dev);
666 if (dev_parent == NULL)
669 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
670 const char *key = udev_list_entry_get_name(list_entry);
671 const char *val = udev_list_entry_get_value(list_entry);
673 if (fnmatch(filter, key, 0) == 0) {
674 struct udev_list_entry *entry;
676 entry = udev_device_add_property(dev, key, val);
677 /* store in db, skip private keys */
679 udev_list_entry_set_num(entry, true);
685 #define WAIT_LOOP_PER_SECOND 50
686 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
688 char filepath[UTIL_PATH_SIZE];
689 char devicepath[UTIL_PATH_SIZE];
691 int loop = timeout * WAIT_LOOP_PER_SECOND;
693 /* a relative path is a device attribute */
694 devicepath[0] = '\0';
695 if (file[0] != '/') {
696 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
697 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
702 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
705 if (stat(file, &stats) == 0) {
706 log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
709 /* make sure, the device did not disappear in the meantime */
710 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
711 log_debug("device disappeared while waiting for '%s'\n", file);
714 log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
715 nanosleep(&duration, NULL);
717 log_debug("waiting for '%s' failed\n", file);
721 static int attr_subst_subdir(char *attr, size_t len)
725 if (strstr(attr, "/*/")) {
727 char dirname[UTIL_PATH_SIZE];
731 strscpy(dirname, sizeof(dirname), attr);
732 pos = strstr(dirname, "/*/");
737 dir = opendir(dirname);
741 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
744 if (dent->d_name[0] == '.')
746 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
747 if (stat(attr, &stats) == 0) {
759 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
765 if (linepos == NULL || linepos[0] == '\0')
768 /* skip whitespace */
769 while (isspace(linepos[0]) || linepos[0] == ',')
773 if (linepos[0] == '\0')
779 if (linepos[0] == '\0')
781 if (isspace(linepos[0]))
783 if (linepos[0] == '=')
785 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
786 if (linepos[1] == '=')
790 /* remember end of key */
793 /* skip whitespace after key */
794 while (isspace(linepos[0]))
796 if (linepos[0] == '\0')
799 /* get operation type */
800 if (linepos[0] == '=' && linepos[1] == '=') {
803 } else if (linepos[0] == '!' && linepos[1] == '=') {
806 } else if (linepos[0] == '+' && linepos[1] == '=') {
809 } else if (linepos[0] == '=') {
812 } else if (linepos[0] == ':' && linepos[1] == '=') {
813 *op = OP_ASSIGN_FINAL;
821 /* skip whitespace after operator */
822 while (isspace(linepos[0]))
824 if (linepos[0] == '\0')
828 if (linepos[0] == '"')
835 temp = strchr(linepos, '"');
841 /* move line to next key */
846 /* extract possible KEY{attr} */
847 static const char *get_key_attribute(struct udev *udev, char *str)
852 attr = strchr(str, '{');
855 pos = strchr(attr, '}');
857 log_error("missing closing brace for format\n");
866 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
867 enum operation_type op,
868 const char *value, const void *data)
870 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
871 const char *attr = NULL;
873 memset(token, 0x00, sizeof(struct token));
885 case TK_M_SUBSYSTEMS:
889 case TK_M_IMPORT_FILE:
890 case TK_M_IMPORT_PROG:
892 case TK_M_IMPORT_CMDLINE:
893 case TK_M_IMPORT_PARENT:
903 token->key.value_off = rules_add_string(rule_tmp->rules, value);
905 case TK_M_IMPORT_BUILTIN:
906 token->key.value_off = rules_add_string(rule_tmp->rules, value);
907 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
915 token->key.value_off = rules_add_string(rule_tmp->rules, value);
916 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
919 token->key.value_off = rules_add_string(rule_tmp->rules, value);
921 token->key.mode = *(mode_t *)data;
923 case TK_A_STRING_ESCAPE_NONE:
924 case TK_A_STRING_ESCAPE_REPLACE:
925 case TK_A_DB_PERSIST:
927 case TK_A_RUN_BUILTIN:
928 case TK_A_RUN_PROGRAM:
929 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
930 token->key.value_off = rules_add_string(rule_tmp->rules, value);
932 case TK_A_INOTIFY_WATCH:
933 case TK_A_DEVLINK_PRIO:
934 token->key.devlink_prio = *(int *)data;
937 token->key.uid = *(uid_t *)data;
940 token->key.gid = *(gid_t *)data;
943 token->key.mode = *(mode_t *)data;
945 case TK_A_STATIC_NODE:
946 token->key.value_off = rules_add_string(rule_tmp->rules, value);
948 case TK_M_EVENT_TIMEOUT:
949 token->key.event_timeout = *(int *)data;
952 case TK_M_PARENTS_MIN:
953 case TK_M_PARENTS_MAX:
957 log_error("wrong type %u\n", type);
961 if (value != NULL && type < TK_M_MAX) {
962 /* check if we need to split or call fnmatch() while matching rules */
963 enum string_glob_type glob;
967 has_split = (strchr(value, '|') != NULL);
968 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
969 if (has_split && has_glob) {
970 glob = GL_SPLIT_GLOB;
971 } else if (has_split) {
973 } else if (has_glob) {
974 if (streq(value, "?*"))
981 token->key.glob = glob;
984 if (value != NULL && type > TK_M_MAX) {
985 /* check if assigned value has substitution chars */
987 token->key.subst = SB_SUBSYS;
988 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
989 token->key.subst = SB_FORMAT;
991 token->key.subst = SB_NONE;
995 /* check if property/attribut name has substitution chars */
997 token->key.attrsubst = SB_SUBSYS;
998 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
999 token->key.attrsubst = SB_FORMAT;
1001 token->key.attrsubst = SB_NONE;
1004 token->key.type = type;
1006 rule_tmp->token_cur++;
1007 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
1008 log_error("temporary rule array too small\n");
1014 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1017 unsigned int start = 0;
1018 unsigned int end = rule_tmp->token_cur;
1020 for (i = 0; i < rule_tmp->token_cur; i++) {
1021 enum token_type next_val = TK_UNSET;
1022 unsigned int next_idx = 0;
1025 /* find smallest value */
1026 for (j = start; j < end; j++) {
1027 if (rule_tmp->token[j].type == TK_UNSET)
1029 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1030 next_val = rule_tmp->token[j].type;
1035 /* add token and mark done */
1036 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1038 rule_tmp->token[next_idx].type = TK_UNSET;
1041 if (next_idx == start)
1043 if (next_idx+1 == end)
1049 static int add_rule(struct udev_rules *rules, char *line,
1050 const char *filename, unsigned int filename_off, unsigned int lineno)
1054 struct rule_tmp rule_tmp;
1056 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1057 rule_tmp.rules = rules;
1058 rule_tmp.rule.type = TK_RULE;
1059 /* the offset in the rule is limited to unsigned short */
1060 if (filename_off < USHRT_MAX)
1061 rule_tmp.rule.rule.filename_off = filename_off;
1062 rule_tmp.rule.rule.filename_line = lineno;
1068 enum operation_type op;
1070 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1071 /* Avoid erroring on trailing whitespace. This is probably rare
1072 * so save the work for the error case instead of always trying
1073 * to strip the trailing whitespace with strstrip(). */
1074 while (isblank(*linepos))
1077 /* If we aren't at the end of the line, this is a parsing error.
1078 * Make a best effort to describe where the problem is. */
1079 if (*linepos != '\n') {
1080 char buf[2] = {linepos[1]};
1081 _cleanup_free_ char *tmp;
1084 log_error("invalid key/value pair in file %s on line %u,"
1085 "starting at character %tu ('%s')\n",
1086 filename, lineno, linepos - line + 1, tmp);
1087 if (linepos[1] == '#')
1088 log_info("hint: comments can only start at beginning of line");
1093 if (streq(key, "ACTION")) {
1094 if (op > OP_MATCH_MAX) {
1095 log_error("invalid ACTION operation\n");
1098 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1102 if (streq(key, "DEVPATH")) {
1103 if (op > OP_MATCH_MAX) {
1104 log_error("invalid DEVPATH operation\n");
1107 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1111 if (streq(key, "KERNEL")) {
1112 if (op > OP_MATCH_MAX) {
1113 log_error("invalid KERNEL operation\n");
1116 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1120 if (streq(key, "SUBSYSTEM")) {
1121 if (op > OP_MATCH_MAX) {
1122 log_error("invalid SUBSYSTEM operation\n");
1125 /* bus, class, subsystem events should all be the same */
1126 if (streq(value, "subsystem") ||
1127 streq(value, "bus") ||
1128 streq(value, "class")) {
1129 if (streq(value, "bus") || streq(value, "class"))
1130 log_error("'%s' must be specified as 'subsystem' \n"
1131 "please fix it in %s:%u", value, filename, lineno);
1132 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1134 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1138 if (streq(key, "DRIVER")) {
1139 if (op > OP_MATCH_MAX) {
1140 log_error("invalid DRIVER operation\n");
1143 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1147 if (startswith(key, "ATTR{")) {
1148 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1150 log_error("error parsing ATTR attribute\n");
1153 if (op < OP_MATCH_MAX) {
1154 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1156 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1161 if (streq(key, "KERNELS")) {
1162 if (op > OP_MATCH_MAX) {
1163 log_error("invalid KERNELS operation\n");
1166 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1170 if (streq(key, "SUBSYSTEMS")) {
1171 if (op > OP_MATCH_MAX) {
1172 log_error("invalid SUBSYSTEMS operation\n");
1175 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1179 if (streq(key, "DRIVERS")) {
1180 if (op > OP_MATCH_MAX) {
1181 log_error("invalid DRIVERS operation\n");
1184 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1188 if (startswith(key, "ATTRS{")) {
1189 if (op > OP_MATCH_MAX) {
1190 log_error("invalid ATTRS operation\n");
1193 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1195 log_error("error parsing ATTRS attribute\n");
1198 if (startswith(attr, "device/"))
1199 log_error("the 'device' link may not be available in a future kernel, "
1200 "please fix it in %s:%u", filename, lineno);
1201 else if (strstr(attr, "../") != NULL)
1202 log_error("do not reference parent sysfs directories directly, "
1203 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1204 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1208 if (streq(key, "TAGS")) {
1209 if (op > OP_MATCH_MAX) {
1210 log_error("invalid TAGS operation\n");
1213 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1217 if (startswith(key, "ENV{")) {
1218 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1220 log_error("error parsing ENV attribute\n");
1223 if (op < OP_MATCH_MAX) {
1224 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1227 static const char *blacklist[] = {
1242 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1243 if (!streq(attr, blacklist[i]))
1245 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
1248 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1254 if (streq(key, "TAG")) {
1255 if (op < OP_MATCH_MAX)
1256 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1258 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1262 if (streq(key, "PROGRAM")) {
1263 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1267 if (streq(key, "RESULT")) {
1268 if (op > OP_MATCH_MAX) {
1269 log_error("invalid RESULT operation\n");
1272 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1276 if (startswith(key, "IMPORT")) {
1277 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1279 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1282 if (streq(attr, "program")) {
1283 /* find known built-in command */
1284 if (value[0] != '/') {
1285 enum udev_builtin_cmd cmd;
1287 cmd = udev_builtin_lookup(value);
1288 if (cmd < UDEV_BUILTIN_MAX) {
1289 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1290 value, filename, lineno);
1291 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1295 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1296 } else if (streq(attr, "builtin")) {
1297 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1299 if (cmd < UDEV_BUILTIN_MAX)
1300 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1302 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1303 } else if (streq(attr, "file")) {
1304 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1305 } else if (streq(attr, "db")) {
1306 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1307 } else if (streq(attr, "cmdline")) {
1308 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1309 } else if (streq(attr, "parent")) {
1310 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1312 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno);
1316 if (startswith(key, "TEST")) {
1319 if (op > OP_MATCH_MAX) {
1320 log_error("invalid TEST operation\n");
1323 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1325 mode = strtol(attr, NULL, 8);
1326 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1328 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1333 if (startswith(key, "RUN")) {
1334 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1338 if (streq(attr, "builtin")) {
1339 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1341 if (cmd < UDEV_BUILTIN_MAX)
1342 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1344 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1345 } else if (streq(attr, "program")) {
1346 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1348 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1350 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno);
1356 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1357 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1361 if (streq(key, "LABEL")) {
1362 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1366 if (streq(key, "GOTO")) {
1367 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1371 if (startswith(key, "NAME")) {
1372 if (op < OP_MATCH_MAX) {
1373 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1375 if (streq(value, "%k")) {
1376 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1377 "please remove it from %s:%u\n", filename, lineno);
1380 if (value[0] == '\0') {
1381 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1382 "please remove it from %s:%u\n", filename, lineno);
1385 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1387 rule_tmp.rule.rule.can_set_name = true;
1391 if (streq(key, "SYMLINK")) {
1392 if (op < OP_MATCH_MAX)
1393 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1395 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1396 rule_tmp.rule.rule.can_set_name = true;
1400 if (streq(key, "OWNER")) {
1404 uid = strtoul(value, &endptr, 10);
1405 if (endptr[0] == '\0') {
1406 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1407 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1408 uid = add_uid(rules, value);
1409 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1410 } else if (rules->resolve_names >= 0) {
1411 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1413 rule_tmp.rule.rule.can_set_name = true;
1417 if (streq(key, "GROUP")) {
1421 gid = strtoul(value, &endptr, 10);
1422 if (endptr[0] == '\0') {
1423 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1424 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1425 gid = add_gid(rules, value);
1426 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1427 } else if (rules->resolve_names >= 0) {
1428 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1430 rule_tmp.rule.rule.can_set_name = true;
1434 if (streq(key, "MODE")) {
1438 mode = strtol(value, &endptr, 8);
1439 if (endptr[0] == '\0')
1440 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1442 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1443 rule_tmp.rule.rule.can_set_name = true;
1447 if (streq(key, "OPTIONS")) {
1450 pos = strstr(value, "link_priority=");
1452 int prio = atoi(&pos[strlen("link_priority=")]);
1454 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1457 pos = strstr(value, "event_timeout=");
1459 int tout = atoi(&pos[strlen("event_timeout=")]);
1461 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
1464 pos = strstr(value, "string_escape=");
1466 pos = &pos[strlen("string_escape=")];
1467 if (startswith(pos, "none"))
1468 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1469 else if (startswith(pos, "replace"))
1470 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1473 pos = strstr(value, "db_persist");
1475 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1477 pos = strstr(value, "nowatch");
1481 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1483 pos = strstr(value, "watch");
1487 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1491 pos = strstr(value, "static_node=");
1493 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1494 rule_tmp.rule.rule.has_static_node = true;
1500 log_error("unknown key '%s' in %s:%u\n", key, filename, lineno);
1504 /* add rule token */
1505 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1506 if (add_token(rules, &rule_tmp.rule) != 0)
1509 /* add tokens to list, sorted by type */
1510 if (sort_token(rules, &rule_tmp) != 0)
1515 log_error("invalid rule '%s:%u'\n", filename, lineno);
1519 static int parse_file(struct udev_rules *rules, const char *filename)
1522 unsigned int first_token;
1523 unsigned int filename_off;
1524 char line[UTIL_LINE_SIZE];
1528 if (null_or_empty_path(filename)) {
1529 log_debug("skip empty file: %s\n", filename);
1532 log_debug("read rules file: %s\n", filename);
1534 f = fopen(filename, "re");
1538 first_token = rules->token_cur;
1539 filename_off = rules_add_string(rules, filename);
1541 while (fgets(line, sizeof(line), f) != NULL) {
1545 /* skip whitespace */
1548 while (isspace(key[0]))
1559 /* continue reading if backslash+newline is found */
1560 while (line[len-2] == '\\') {
1561 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1563 if (strlen(&line[len-2]) < 2)
1569 if (len+1 >= sizeof(line)) {
1570 log_error("line too long '%s':%u, ignored\n", filename, line_nr);
1573 add_rule(rules, key, filename, filename_off, line_nr);
1577 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1578 for (i = first_token+1; i < rules->token_cur; i++) {
1579 if (rules->tokens[i].type == TK_A_GOTO) {
1580 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1583 for (j = i+1; j < rules->token_cur; j++) {
1584 if (rules->tokens[j].type != TK_RULE)
1586 if (rules->tokens[j].rule.label_off == 0)
1588 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1590 rules->tokens[i].key.rule_goto = j;
1593 if (rules->tokens[i].key.rule_goto == 0)
1594 log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename);
1600 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1602 struct udev_rules *rules;
1603 struct udev_list file_list;
1604 struct token end_token;
1608 rules = calloc(1, sizeof(struct udev_rules));
1612 rules->resolve_names = resolve_names;
1613 udev_list_init(udev, &file_list, true);
1615 /* init token array and string buffer */
1616 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1617 if (rules->tokens == NULL)
1618 return udev_rules_unref(rules);
1619 rules->token_max = PREALLOC_TOKEN;
1621 rules->strbuf = strbuf_new();
1623 return udev_rules_unref(rules);
1625 rules->dirs = strv_new("/etc/udev/rules.d",
1626 "/run/udev/rules.d",
1627 UDEVLIBEXECDIR "/rules.d",
1630 log_error("failed to build config directory array");
1631 return udev_rules_unref(rules);
1633 if (!path_strv_canonicalize(rules->dirs)) {
1634 log_error("failed to canonicalize config directories\n");
1635 return udev_rules_unref(rules);
1637 strv_uniq(rules->dirs);
1639 rules->dirs_ts_usec = calloc(strv_length(rules->dirs), sizeof(usec_t));
1640 if(!rules->dirs_ts_usec)
1641 return udev_rules_unref(rules);
1642 udev_rules_check_timestamp(rules);
1644 r = conf_files_list_strv(&files, ".rules", NULL, (const char **)rules->dirs);
1646 log_error("failed to enumerate rules files: %s\n", strerror(-r));
1647 return udev_rules_unref(rules);
1651 * The offset value in the rules strct is limited; add all
1652 * rules file names to the beginning of the string buffer.
1654 STRV_FOREACH(f, files)
1655 rules_add_string(rules, *f);
1657 STRV_FOREACH(f, files)
1658 parse_file(rules, *f);
1662 memset(&end_token, 0x00, sizeof(struct token));
1663 end_token.type = TK_END;
1664 add_token(rules, &end_token);
1665 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings\n",
1666 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1668 /* cleanup temporary strbuf data */
1669 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used\n",
1670 rules->strbuf->in_count, rules->strbuf->in_len,
1671 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1672 strbuf_complete(rules->strbuf);
1674 /* cleanup uid/gid cache */
1677 rules->uids_cur = 0;
1678 rules->uids_max = 0;
1681 rules->gids_cur = 0;
1682 rules->gids_max = 0;
1688 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1692 free(rules->tokens);
1693 strbuf_cleanup(rules->strbuf);
1696 strv_free(rules->dirs);
1697 free(rules->dirs_ts_usec);
1702 bool udev_rules_check_timestamp(struct udev_rules *rules)
1705 bool changed = false;
1710 for (i = 0; rules->dirs[i]; i++) {
1713 if (stat(rules->dirs[i], &stats) < 0)
1716 if (rules->dirs_ts_usec[i] == timespec_load(&stats.st_mtim))
1720 if (rules->dirs_ts_usec[i] != 0) {
1721 log_debug("reload - timestamp of '%s' changed\n", rules->dirs[i]);
1725 /* update timestamp */
1726 rules->dirs_ts_usec[i] = timespec_load(&stats.st_mtim);
1732 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1734 char *key_value = rules_str(rules, token->key.value_off);
1741 switch (token->key.glob) {
1743 match = (streq(key_value, val));
1746 match = (fnmatch(key_value, val, 0) == 0);
1753 s = rules_str(rules, token->key.value_off);
1758 next = strchr(s, '|');
1760 size_t matchlen = (size_t)(next - s);
1762 match = (matchlen == len && strneq(s, val, matchlen));
1766 match = (streq(s, val));
1775 char value[UTIL_PATH_SIZE];
1777 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1779 while (key_value != NULL) {
1780 pos = strchr(key_value, '|');
1785 match = (fnmatch(key_value, val, 0) == 0);
1793 match = (val[0] != '\0');
1799 if (match && (token->key.op == OP_MATCH))
1801 if (!match && (token->key.op == OP_NOMATCH))
1806 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1809 char nbuf[UTIL_NAME_SIZE];
1811 char vbuf[UTIL_NAME_SIZE];
1814 name = rules_str(rules, cur->key.attr_off);
1815 switch (cur->key.attrsubst) {
1817 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1821 value = udev_device_get_sysattr_value(dev, name);
1826 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1834 /* remove trailing whitespace, if not asked to match for it */
1835 len = strlen(value);
1836 if (len > 0 && isspace(value[len-1])) {
1837 const char *key_value;
1840 key_value = rules_str(rules, cur->key.value_off);
1841 klen = strlen(key_value);
1842 if (klen > 0 && !isspace(key_value[klen-1])) {
1843 if (value != vbuf) {
1844 strscpy(vbuf, sizeof(vbuf), value);
1847 while (len > 0 && isspace(vbuf[--len]))
1852 return match_key(rules, cur, value);
1861 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
1865 enum escape_type esc = ESCAPE_UNSET;
1868 if (rules->tokens == NULL)
1871 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1872 (major(udev_device_get_devnum(event->dev)) > 0 ||
1873 udev_device_get_ifindex(event->dev) > 0));
1875 /* loop through token list, match, run actions or forward to next rule */
1876 cur = &rules->tokens[0];
1879 dump_token(rules, cur);
1880 switch (cur->type) {
1884 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1885 if (!can_set_name && rule->rule.can_set_name)
1890 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1894 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1898 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1901 case TK_M_DEVLINK: {
1902 struct udev_list_entry *list_entry;
1905 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1906 const char *devlink;
1908 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1909 if (match_key(rules, cur, devlink) == 0) {
1919 if (match_key(rules, cur, event->name) != 0)
1923 const char *key_name = rules_str(rules, cur->key.attr_off);
1926 value = udev_device_get_property_value(event->dev, key_name);
1929 if (match_key(rules, cur, value))
1934 struct udev_list_entry *list_entry;
1937 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1938 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1943 if (!match && (cur->key.op != OP_NOMATCH))
1947 case TK_M_SUBSYSTEM:
1948 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1952 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1955 case TK_M_WAITFOR: {
1956 char filename[UTIL_PATH_SIZE];
1959 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1960 found = (wait_for_file(event->dev, filename, 10) == 0);
1961 if (!found && (cur->key.op != OP_NOMATCH))
1966 if (match_attr(rules, event->dev, event, cur) != 0)
1970 case TK_M_SUBSYSTEMS:
1976 /* get whole sequence of parent matches */
1978 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1981 /* loop over parents */
1982 event->dev_parent = event->dev;
1986 /* loop over sequence of parent match keys */
1987 for (key = cur; key < next; key++ ) {
1988 dump_token(rules, key);
1991 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1994 case TK_M_SUBSYSTEMS:
1995 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1999 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2003 if (match_attr(rules, event->dev_parent, event, key) != 0)
2007 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
2009 if (match && key->key.op == OP_NOMATCH)
2011 if (!match && key->key.op == OP_MATCH)
2022 event->dev_parent = udev_device_get_parent(event->dev_parent);
2023 if (event->dev_parent == NULL)
2026 /* move behind our sequence of parent match keys */
2031 char filename[UTIL_PATH_SIZE];
2032 struct stat statbuf;
2035 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
2036 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2037 if (filename[0] != '/') {
2038 char tmp[UTIL_PATH_SIZE];
2040 strscpy(tmp, sizeof(tmp), filename);
2041 strscpyl(filename, sizeof(filename),
2042 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2045 attr_subst_subdir(filename, sizeof(filename));
2047 match = (stat(filename, &statbuf) == 0);
2048 if (match && cur->key.mode > 0)
2049 match = ((statbuf.st_mode & cur->key.mode) > 0);
2050 if (match && cur->key.op == OP_NOMATCH)
2052 if (!match && cur->key.op == OP_MATCH)
2056 case TK_M_EVENT_TIMEOUT:
2057 log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout);
2058 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2060 case TK_M_PROGRAM: {
2061 char program[UTIL_PATH_SIZE];
2063 char result[UTIL_PATH_SIZE];
2065 free(event->program_result);
2066 event->program_result = NULL;
2067 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2068 envp = udev_device_get_properties_envp(event->dev);
2069 log_debug("PROGRAM '%s' %s:%u\n",
2071 rules_str(rules, rule->rule.filename_off),
2072 rule->rule.filename_line);
2074 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2075 if (cur->key.op != OP_NOMATCH)
2080 util_remove_trailing_chars(result, '\n');
2081 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2082 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2084 log_debug("%i character(s) replaced\n" , count);
2086 event->program_result = strdup(result);
2087 if (cur->key.op == OP_NOMATCH)
2092 case TK_M_IMPORT_FILE: {
2093 char import[UTIL_PATH_SIZE];
2095 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2096 if (import_file_into_properties(event->dev, import) != 0)
2097 if (cur->key.op != OP_NOMATCH)
2101 case TK_M_IMPORT_PROG: {
2102 char import[UTIL_PATH_SIZE];
2104 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2105 log_debug("IMPORT '%s' %s:%u\n",
2107 rules_str(rules, rule->rule.filename_off),
2108 rule->rule.filename_line);
2110 if (import_program_into_properties(event, import, sigmask) != 0)
2111 if (cur->key.op != OP_NOMATCH)
2115 case TK_M_IMPORT_BUILTIN: {
2116 char command[UTIL_PATH_SIZE];
2118 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2119 /* check if we ran already */
2120 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2121 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2122 udev_builtin_name(cur->key.builtin_cmd),
2123 rules_str(rules, rule->rule.filename_off),
2124 rule->rule.filename_line);
2125 /* return the result from earlier run */
2126 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2127 if (cur->key.op != OP_NOMATCH)
2132 event->builtin_run |= (1 << cur->key.builtin_cmd);
2135 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2136 log_debug("IMPORT builtin '%s' %s:%u\n",
2137 udev_builtin_name(cur->key.builtin_cmd),
2138 rules_str(rules, rule->rule.filename_off),
2139 rule->rule.filename_line);
2141 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2142 /* remember failure */
2143 log_debug("IMPORT builtin '%s' returned non-zero\n",
2144 udev_builtin_name(cur->key.builtin_cmd));
2145 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2146 if (cur->key.op != OP_NOMATCH)
2151 case TK_M_IMPORT_DB: {
2152 const char *key = rules_str(rules, cur->key.value_off);
2155 value = udev_device_get_property_value(event->dev_db, key);
2156 if (value != NULL) {
2157 struct udev_list_entry *entry;
2159 entry = udev_device_add_property(event->dev, key, value);
2160 udev_list_entry_set_num(entry, true);
2162 if (cur->key.op != OP_NOMATCH)
2167 case TK_M_IMPORT_CMDLINE: {
2169 bool imported = false;
2171 f = fopen("/proc/cmdline", "re");
2175 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2176 const char *key = rules_str(rules, cur->key.value_off);
2179 pos = strstr(cmdline, key);
2181 struct udev_list_entry *entry;
2184 if (pos[0] == '\0' || isspace(pos[0])) {
2185 /* we import simple flags as 'FLAG=1' */
2186 entry = udev_device_add_property(event->dev, key, "1");
2187 udev_list_entry_set_num(entry, true);
2189 } else if (pos[0] == '=') {
2194 while (pos[0] != '\0' && !isspace(pos[0]))
2197 entry = udev_device_add_property(event->dev, key, value);
2198 udev_list_entry_set_num(entry, true);
2205 if (!imported && cur->key.op != OP_NOMATCH)
2209 case TK_M_IMPORT_PARENT: {
2210 char import[UTIL_PATH_SIZE];
2212 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2213 if (import_parent_into_properties(event->dev, import) != 0)
2214 if (cur->key.op != OP_NOMATCH)
2219 if (match_key(rules, cur, event->program_result) != 0)
2222 case TK_A_STRING_ESCAPE_NONE:
2225 case TK_A_STRING_ESCAPE_REPLACE:
2226 esc = ESCAPE_REPLACE;
2228 case TK_A_DB_PERSIST:
2229 udev_device_set_db_persist(event->dev);
2231 case TK_A_INOTIFY_WATCH:
2232 if (event->inotify_watch_final)
2234 if (cur->key.op == OP_ASSIGN_FINAL)
2235 event->inotify_watch_final = true;
2236 event->inotify_watch = cur->key.watch;
2238 case TK_A_DEVLINK_PRIO:
2239 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2242 char owner[UTIL_NAME_SIZE];
2244 if (event->owner_final)
2246 if (cur->key.op == OP_ASSIGN_FINAL)
2247 event->owner_final = true;
2248 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2249 event->owner_set = true;
2250 event->uid = util_lookup_user(event->udev, owner);
2251 log_debug("OWNER %u %s:%u\n",
2253 rules_str(rules, rule->rule.filename_off),
2254 rule->rule.filename_line);
2258 char group[UTIL_NAME_SIZE];
2260 if (event->group_final)
2262 if (cur->key.op == OP_ASSIGN_FINAL)
2263 event->group_final = true;
2264 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2265 event->group_set = true;
2266 event->gid = util_lookup_group(event->udev, group);
2267 log_debug("GROUP %u %s:%u\n",
2269 rules_str(rules, rule->rule.filename_off),
2270 rule->rule.filename_line);
2274 char mode_str[UTIL_NAME_SIZE];
2278 if (event->mode_final)
2280 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2281 mode = strtol(mode_str, &endptr, 8);
2282 if (endptr[0] != '\0') {
2283 log_error("ignoring invalid mode '%s'\n", mode_str);
2286 if (cur->key.op == OP_ASSIGN_FINAL)
2287 event->mode_final = true;
2288 event->mode_set = true;
2290 log_debug("MODE %#o %s:%u\n",
2292 rules_str(rules, rule->rule.filename_off),
2293 rule->rule.filename_line);
2297 if (event->owner_final)
2299 if (cur->key.op == OP_ASSIGN_FINAL)
2300 event->owner_final = true;
2301 event->owner_set = true;
2302 event->uid = cur->key.uid;
2303 log_debug("OWNER %u %s:%u\n",
2305 rules_str(rules, rule->rule.filename_off),
2306 rule->rule.filename_line);
2309 if (event->group_final)
2311 if (cur->key.op == OP_ASSIGN_FINAL)
2312 event->group_final = true;
2313 event->group_set = true;
2314 event->gid = cur->key.gid;
2315 log_debug("GROUP %u %s:%u\n",
2317 rules_str(rules, rule->rule.filename_off),
2318 rule->rule.filename_line);
2321 if (event->mode_final)
2323 if (cur->key.op == OP_ASSIGN_FINAL)
2324 event->mode_final = true;
2325 event->mode_set = true;
2326 event->mode = cur->key.mode;
2327 log_debug("MODE %#o %s:%u\n",
2329 rules_str(rules, rule->rule.filename_off),
2330 rule->rule.filename_line);
2333 const char *name = rules_str(rules, cur->key.attr_off);
2334 char *value = rules_str(rules, cur->key.value_off);
2335 char value_new[UTIL_NAME_SIZE];
2336 const char *value_old = NULL;
2337 struct udev_list_entry *entry;
2339 if (value[0] == '\0') {
2340 if (cur->key.op == OP_ADD)
2342 udev_device_add_property(event->dev, name, NULL);
2346 if (cur->key.op == OP_ADD)
2347 value_old = udev_device_get_property_value(event->dev, name);
2349 char temp[UTIL_NAME_SIZE];
2351 /* append value separated by space */
2352 udev_event_apply_format(event, value, temp, sizeof(temp));
2353 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2355 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2357 entry = udev_device_add_property(event->dev, name, value_new);
2358 /* store in db, skip private keys */
2360 udev_list_entry_set_num(entry, true);
2364 char tag[UTIL_PATH_SIZE];
2367 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2368 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2369 udev_device_cleanup_tags_list(event->dev);
2370 for (p = tag; *p != '\0'; p++) {
2371 if ((*p >= 'a' && *p <= 'z') ||
2372 (*p >= 'A' && *p <= 'Z') ||
2373 (*p >= '0' && *p <= '9') ||
2374 *p == '-' || *p == '_')
2376 log_error("ignoring invalid tag name '%s'\n", tag);
2379 udev_device_add_tag(event->dev, tag);
2383 const char *name = rules_str(rules, cur->key.value_off);
2385 char name_str[UTIL_PATH_SIZE];
2388 if (event->name_final)
2390 if (cur->key.op == OP_ASSIGN_FINAL)
2391 event->name_final = true;
2392 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2393 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2394 count = util_replace_chars(name_str, "/");
2396 log_debug("%i character(s) replaced\n", count);
2398 if (major(udev_device_get_devnum(event->dev)) &&
2399 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2400 log_error("NAME=\"%s\" ignored, kernel device nodes "
2401 "can not be renamed; please fix it in %s:%u\n", name,
2402 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2406 event->name = strdup(name_str);
2407 log_debug("NAME '%s' %s:%u\n",
2409 rules_str(rules, rule->rule.filename_off),
2410 rule->rule.filename_line);
2413 case TK_A_DEVLINK: {
2414 char temp[UTIL_PATH_SIZE];
2415 char filename[UTIL_PATH_SIZE];
2419 if (event->devlink_final)
2421 if (major(udev_device_get_devnum(event->dev)) == 0)
2423 if (cur->key.op == OP_ASSIGN_FINAL)
2424 event->devlink_final = true;
2425 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2426 udev_device_cleanup_devlinks_list(event->dev);
2428 /* allow multiple symlinks separated by spaces */
2429 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2430 if (esc == ESCAPE_UNSET)
2431 count = util_replace_chars(temp, "/ ");
2432 else if (esc == ESCAPE_REPLACE)
2433 count = util_replace_chars(temp, "/");
2435 log_debug("%i character(s) replaced\n" , count);
2437 while (isspace(pos[0]))
2439 next = strchr(pos, ' ');
2440 while (next != NULL) {
2442 log_debug("LINK '%s' %s:%u\n", pos,
2443 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2444 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2445 udev_device_add_devlink(event->dev, filename);
2446 while (isspace(next[1]))
2449 next = strchr(pos, ' ');
2451 if (pos[0] != '\0') {
2452 log_debug("LINK '%s' %s:%u\n", pos,
2453 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2454 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2455 udev_device_add_devlink(event->dev, filename);
2460 const char *key_name = rules_str(rules, cur->key.attr_off);
2461 char attr[UTIL_PATH_SIZE];
2462 char value[UTIL_NAME_SIZE];
2465 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2466 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2467 attr_subst_subdir(attr, sizeof(attr));
2469 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2470 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
2471 rules_str(rules, rule->rule.filename_off),
2472 rule->rule.filename_line);
2473 f = fopen(attr, "we");
2475 if (fprintf(f, "%s", value) <= 0)
2476 log_error("error writing ATTR{%s}: %m\n", attr);
2479 log_error("error opening ATTR{%s} for writing: %m\n", attr);
2483 case TK_A_RUN_BUILTIN:
2484 case TK_A_RUN_PROGRAM: {
2485 struct udev_list_entry *entry;
2487 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2488 udev_list_cleanup(&event->run_list);
2489 log_debug("RUN '%s' %s:%u\n",
2490 rules_str(rules, cur->key.value_off),
2491 rules_str(rules, rule->rule.filename_off),
2492 rule->rule.filename_line);
2493 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2494 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2498 if (cur->key.rule_goto == 0)
2500 cur = &rules->tokens[cur->key.rule_goto];
2505 case TK_M_PARENTS_MIN:
2506 case TK_M_PARENTS_MAX:
2509 log_error("wrong type %u\n", cur->type);
2516 /* fast-forward to next rule */
2517 cur = rule + rule->rule.token_count;
2521 int udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2528 _cleanup_strv_free_ char **tags = NULL;
2531 _cleanup_free_ char *path = NULL;
2534 if (rules->tokens == NULL)
2537 cur = &rules->tokens[0];
2540 switch (cur->type) {
2545 /* skip rules without a static_node tag */
2546 if (!rule->rule.has_static_node)
2562 mode = cur->key.mode;
2565 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2570 case TK_A_STATIC_NODE: {
2571 char device_node[UTIL_PATH_SIZE];
2572 char tags_dir[UTIL_PATH_SIZE];
2573 char tag_symlink[UTIL_PATH_SIZE];
2576 /* we assure, that the permissions tokens are sorted before the static token */
2577 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2579 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2580 if (stat(device_node, &stats) != 0)
2582 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2586 /* Export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2588 STRV_FOREACH(t, tags) {
2589 _cleanup_free_ char *unescaped_filename = NULL;
2591 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2592 r = mkdir_p(tags_dir, 0755);
2594 log_error("failed to create %s: %s\n", tags_dir, strerror(-r));
2598 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2600 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2601 r = symlink(device_node, tag_symlink);
2602 if (r < 0 && errno != EEXIST) {
2603 log_error("failed to create symlink %s -> %s: %s\n", tag_symlink, device_node, strerror(errno));
2610 /* don't touch the permissions if only the tags were set */
2611 if (mode == 0 && uid == 0 && gid == 0)
2620 if (mode != (stats.st_mode & 01777)) {
2621 r = chmod(device_node, mode);
2623 log_error("failed to chmod '%s' %#o\n", device_node, mode);
2626 log_debug("chmod '%s' %#o\n", device_node, mode);
2629 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2630 r = chown(device_node, uid, gid);
2632 log_error("failed to chown '%s' %u %u \n", device_node, uid, gid);
2635 log_debug("chown '%s' %u %u\n", device_node, uid, gid);
2638 utimensat(AT_FDCWD, device_node, NULL, 0);
2648 /* fast-forward to next rule */
2649 cur = rule + rule->rule.token_count;
2656 fchmod(fileno(f), 0644);
2657 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2659 unlink("/run/udev/static_node-tags");