2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "path-util.h"
34 #include "conf-files.h"
39 #define PREALLOC_TOKEN 2048
42 unsigned int name_off;
55 /* every key in the rules file becomes a token */
57 unsigned int token_cur;
58 unsigned int token_max;
60 /* all key strings are copied and de-duplicated in a single continuous string buffer */
61 struct strbuf *strbuf;
63 /* during rule parsing, uid/gid lookup results are cached */
65 unsigned int uids_cur;
66 unsigned int uids_max;
68 unsigned int gids_cur;
69 unsigned int gids_max;
72 static char *rules_str(struct udev_rules *rules, unsigned int off) {
73 return rules->strbuf->buf + off;
76 static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
77 return strbuf_add_string(rules->strbuf, s, strlen(s));
80 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
93 enum string_glob_type {
95 GL_PLAIN, /* no special chars */
96 GL_GLOB, /* shell globs ?,*,[] */
97 GL_SPLIT, /* multi-value A|B */
98 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
99 GL_SOMETHING, /* commonly used "?*" */
102 enum string_subst_type {
109 /* tokens of a rule are sorted/handled in this order */
114 TK_M_ACTION, /* val */
115 TK_M_DEVPATH, /* val */
116 TK_M_KERNEL, /* val */
117 TK_M_DEVLINK, /* val */
119 TK_M_ENV, /* val, attr */
121 TK_M_SUBSYSTEM, /* val */
122 TK_M_DRIVER, /* val */
123 TK_M_WAITFOR, /* val */
124 TK_M_ATTR, /* val, attr */
127 TK_M_KERNELS, /* val */
128 TK_M_SUBSYSTEMS, /* val */
129 TK_M_DRIVERS, /* val */
130 TK_M_ATTRS, /* val, attr */
134 TK_M_TEST, /* val, mode_t */
135 TK_M_EVENT_TIMEOUT, /* int */
136 TK_M_PROGRAM, /* val */
137 TK_M_IMPORT_FILE, /* val */
138 TK_M_IMPORT_PROG, /* val */
139 TK_M_IMPORT_BUILTIN, /* val */
140 TK_M_IMPORT_DB, /* val */
141 TK_M_IMPORT_CMDLINE, /* val */
142 TK_M_IMPORT_PARENT, /* val */
143 TK_M_RESULT, /* val */
146 TK_A_STRING_ESCAPE_NONE,
147 TK_A_STRING_ESCAPE_REPLACE,
149 TK_A_INOTIFY_WATCH, /* int */
150 TK_A_DEVLINK_PRIO, /* int */
151 TK_A_OWNER, /* val */
152 TK_A_GROUP, /* val */
154 TK_A_OWNER_ID, /* uid_t */
155 TK_A_GROUP_ID, /* gid_t */
156 TK_A_MODE_ID, /* mode_t */
158 TK_A_STATIC_NODE, /* val */
159 TK_A_SECLABEL, /* val, attr */
160 TK_A_ENV, /* val, attr */
162 TK_A_DEVLINK, /* val */
163 TK_A_ATTR, /* val, attr */
164 TK_A_RUN_BUILTIN, /* val, bool */
165 TK_A_RUN_PROGRAM, /* val, bool */
166 TK_A_GOTO, /* size_t */
171 /* we try to pack stuff in a way that we take only 12 bytes per token */
174 unsigned char type; /* same in rule and key */
176 enum token_type type:8;
178 bool has_static_node:1;
179 unsigned int unused:6;
180 unsigned short token_count;
181 unsigned int label_off;
182 unsigned short filename_off;
183 unsigned short filename_line;
186 enum token_type type:8;
187 enum operation_type op:8;
188 enum string_glob_type glob:8;
189 enum string_subst_type subst:4;
190 enum string_subst_type attrsubst:4;
191 unsigned int value_off;
193 unsigned int attr_off;
194 unsigned int rule_goto;
201 enum udev_builtin_cmd builtin_cmd;
209 struct udev_rules *rules;
211 struct token token[MAX_TK];
212 unsigned int token_cur;
216 static const char *operation_str(enum operation_type type)
218 static const char *operation_strs[] = {
219 [OP_UNSET] = "UNSET",
220 [OP_MATCH] = "match",
221 [OP_NOMATCH] = "nomatch",
222 [OP_MATCH_MAX] = "MATCH_MAX",
225 [OP_ASSIGN] = "assign",
226 [OP_ASSIGN_FINAL] = "assign-final",
229 return operation_strs[type];
232 static const char *string_glob_str(enum string_glob_type type)
234 static const char *string_glob_strs[] = {
235 [GL_UNSET] = "UNSET",
236 [GL_PLAIN] = "plain",
238 [GL_SPLIT] = "split",
239 [GL_SPLIT_GLOB] = "split-glob",
240 [GL_SOMETHING] = "split-glob",
243 return string_glob_strs[type];
246 static const char *token_str(enum token_type type)
248 static const char *token_strs[] = {
249 [TK_UNSET] = "UNSET",
252 [TK_M_ACTION] = "M ACTION",
253 [TK_M_DEVPATH] = "M DEVPATH",
254 [TK_M_KERNEL] = "M KERNEL",
255 [TK_M_DEVLINK] = "M DEVLINK",
256 [TK_M_NAME] = "M NAME",
257 [TK_M_ENV] = "M ENV",
258 [TK_M_TAG] = "M TAG",
259 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
260 [TK_M_DRIVER] = "M DRIVER",
261 [TK_M_WAITFOR] = "M WAITFOR",
262 [TK_M_ATTR] = "M ATTR",
264 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
265 [TK_M_KERNELS] = "M KERNELS",
266 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
267 [TK_M_DRIVERS] = "M DRIVERS",
268 [TK_M_ATTRS] = "M ATTRS",
269 [TK_M_TAGS] = "M TAGS",
270 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
272 [TK_M_TEST] = "M TEST",
273 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
274 [TK_M_PROGRAM] = "M PROGRAM",
275 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
276 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
277 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
278 [TK_M_IMPORT_DB] = "M IMPORT_DB",
279 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
280 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
281 [TK_M_RESULT] = "M RESULT",
282 [TK_M_MAX] = "M MAX",
284 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
285 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
286 [TK_A_DB_PERSIST] = "A DB_PERSIST",
287 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
288 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
289 [TK_A_OWNER] = "A OWNER",
290 [TK_A_GROUP] = "A GROUP",
291 [TK_A_MODE] = "A MODE",
292 [TK_A_OWNER_ID] = "A OWNER_ID",
293 [TK_A_GROUP_ID] = "A GROUP_ID",
294 [TK_A_STATIC_NODE] = "A STATIC_NODE",
295 [TK_A_SECLABEL] = "A SECLABEL",
296 [TK_A_MODE_ID] = "A MODE_ID",
297 [TK_A_ENV] = "A ENV",
298 [TK_A_TAG] = "A ENV",
299 [TK_A_NAME] = "A NAME",
300 [TK_A_DEVLINK] = "A DEVLINK",
301 [TK_A_ATTR] = "A ATTR",
302 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
303 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
304 [TK_A_GOTO] = "A GOTO",
309 return token_strs[type];
312 static void dump_token(struct udev_rules *rules, struct token *token)
314 enum token_type type = token->type;
315 enum operation_type op = token->key.op;
316 enum string_glob_type glob = token->key.glob;
317 const char *value = str(rules, token->key.value_off);
318 const char *attr = &rules->buf[token->key.attr_off];
323 const char *tks_ptr = (char *)rules->tokens;
324 const char *tk_ptr = (char *)token;
325 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
327 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
328 &rules->buf[token->rule.filename_off], token->rule.filename_line,
329 idx, token->rule.token_count,
330 &rules->buf[token->rule.label_off]);
342 case TK_M_SUBSYSTEMS:
346 case TK_M_IMPORT_FILE:
347 case TK_M_IMPORT_PROG:
349 case TK_M_IMPORT_CMDLINE:
350 case TK_M_IMPORT_PARENT:
357 case TK_A_RUN_BUILTIN:
358 case TK_A_RUN_PROGRAM:
359 log_debug("%s %s '%s'(%s)\n",
360 token_str(type), operation_str(op), value, string_glob_str(glob));
362 case TK_M_IMPORT_BUILTIN:
363 log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
370 log_debug("%s %s '%s' '%s'(%s)\n",
371 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
375 log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value);
377 case TK_A_STRING_ESCAPE_NONE:
378 case TK_A_STRING_ESCAPE_REPLACE:
379 case TK_A_DB_PERSIST:
380 log_debug("%s\n", token_str(type));
383 log_debug("%s %s '%s'(%s) %#o\n",
384 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
386 case TK_A_INOTIFY_WATCH:
387 log_debug("%s %u\n", token_str(type), token->key.watch);
389 case TK_A_DEVLINK_PRIO:
390 log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
393 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
396 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
399 log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
401 case TK_A_STATIC_NODE:
402 log_debug("%s '%s'\n", token_str(type), value);
405 log_debug("%s %s '%s' '%s'\n", token_str(type), operation_str(op), attr, value);
407 case TK_M_EVENT_TIMEOUT:
408 log_debug("%s %u\n", token_str(type), token->key.event_timeout);
411 log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
414 log_debug("* %s\n", token_str(type));
416 case TK_M_PARENTS_MIN:
417 case TK_M_PARENTS_MAX:
420 log_debug("unknown type %u\n", type);
425 static void dump_rules(struct udev_rules *rules)
429 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
431 rules->token_cur * sizeof(struct token),
434 for(i = 0; i < rules->token_cur; i++)
435 dump_token(rules, &rules->tokens[i]);
438 static inline const char *operation_str(enum operation_type type) { return NULL; }
439 static inline const char *token_str(enum token_type type) { return NULL; }
440 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
441 static inline void dump_rules(struct udev_rules *rules) {}
444 static int add_token(struct udev_rules *rules, struct token *token)
446 /* grow buffer if needed */
447 if (rules->token_cur+1 >= rules->token_max) {
448 struct token *tokens;
451 /* double the buffer size */
452 add = rules->token_max;
456 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
459 rules->tokens = tokens;
460 rules->token_max += add;
462 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
467 static uid_t add_uid(struct udev_rules *rules, const char *owner)
473 /* lookup, if we know it already */
474 for (i = 0; i < rules->uids_cur; i++) {
475 off = rules->uids[i].name_off;
476 if (streq(rules_str(rules, off), owner)) {
477 uid = rules->uids[i].uid;
481 uid = util_lookup_user(rules->udev, owner);
483 /* grow buffer if needed */
484 if (rules->uids_cur+1 >= rules->uids_max) {
485 struct uid_gid *uids;
488 /* double the buffer size */
489 add = rules->uids_max;
493 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
497 rules->uids_max += add;
499 rules->uids[rules->uids_cur].uid = uid;
500 off = rules_add_string(rules, owner);
503 rules->uids[rules->uids_cur].name_off = off;
508 static gid_t add_gid(struct udev_rules *rules, const char *group)
514 /* lookup, if we know it already */
515 for (i = 0; i < rules->gids_cur; i++) {
516 off = rules->gids[i].name_off;
517 if (streq(rules_str(rules, off), group)) {
518 gid = rules->gids[i].gid;
522 gid = util_lookup_group(rules->udev, group);
524 /* grow buffer if needed */
525 if (rules->gids_cur+1 >= rules->gids_max) {
526 struct uid_gid *gids;
529 /* double the buffer size */
530 add = rules->gids_max;
534 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
538 rules->gids_max += add;
540 rules->gids[rules->gids_cur].gid = gid;
541 off = rules_add_string(rules, group);
544 rules->gids[rules->gids_cur].name_off = off;
549 static int import_property_from_string(struct udev_device *dev, char *line)
557 while (isspace(key[0]))
560 /* comment or empty line */
561 if (key[0] == '#' || key[0] == '\0')
564 /* split key/value */
565 val = strchr(key, '=');
572 while (isspace(val[0]))
579 while (isspace(key[len-1]))
583 /* terminate value */
587 while (isspace(val[len-1]))
595 if (val[0] == '"' || val[0] == '\'') {
596 if (val[len-1] != val[0]) {
597 log_debug("inconsistent quoting: '%s', skip\n", line);
604 /* handle device, renamed by external tool, returning new path */
605 if (streq(key, "DEVPATH")) {
606 char syspath[UTIL_PATH_SIZE];
608 log_debug("updating devpath from '%s' to '%s'\n",
609 udev_device_get_devpath(dev), val);
610 strscpyl(syspath, sizeof(syspath), "/sys", val, NULL);
611 udev_device_set_syspath(dev, syspath);
613 struct udev_list_entry *entry;
615 entry = udev_device_add_property(dev, key, val);
616 /* store in db, skip private keys */
618 udev_list_entry_set_num(entry, true);
623 static int import_file_into_properties(struct udev_device *dev, const char *filename)
626 char line[UTIL_LINE_SIZE];
628 f = fopen(filename, "re");
631 while (fgets(line, sizeof(line), f) != NULL)
632 import_property_from_string(dev, line);
637 static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
639 struct udev_device *dev = event->dev;
641 char result[UTIL_LINE_SIZE];
645 envp = udev_device_get_properties_envp(dev);
646 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
651 while (line != NULL) {
654 pos = strchr(line, '\n');
659 import_property_from_string(dev, line);
665 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
667 struct udev_device *dev_parent;
668 struct udev_list_entry *list_entry;
670 dev_parent = udev_device_get_parent(dev);
671 if (dev_parent == NULL)
674 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
675 const char *key = udev_list_entry_get_name(list_entry);
676 const char *val = udev_list_entry_get_value(list_entry);
678 if (fnmatch(filter, key, 0) == 0) {
679 struct udev_list_entry *entry;
681 entry = udev_device_add_property(dev, key, val);
682 /* store in db, skip private keys */
684 udev_list_entry_set_num(entry, true);
690 #define WAIT_LOOP_PER_SECOND 50
691 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
693 char filepath[UTIL_PATH_SIZE];
694 char devicepath[UTIL_PATH_SIZE];
696 int loop = timeout * WAIT_LOOP_PER_SECOND;
698 /* a relative path is a device attribute */
699 devicepath[0] = '\0';
700 if (file[0] != '/') {
701 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
702 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
707 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
710 if (stat(file, &stats) == 0) {
711 log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
714 /* make sure, the device did not disappear in the meantime */
715 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
716 log_debug("device disappeared while waiting for '%s'\n", file);
719 log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
720 nanosleep(&duration, NULL);
722 log_debug("waiting for '%s' failed\n", file);
726 static int attr_subst_subdir(char *attr, size_t len)
730 if (strstr(attr, "/*/")) {
732 char dirname[UTIL_PATH_SIZE];
736 strscpy(dirname, sizeof(dirname), attr);
737 pos = strstr(dirname, "/*/");
742 dir = opendir(dirname);
746 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
749 if (dent->d_name[0] == '.')
751 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
752 if (stat(attr, &stats) == 0) {
764 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
770 if (linepos == NULL || linepos[0] == '\0')
773 /* skip whitespace */
774 while (isspace(linepos[0]) || linepos[0] == ',')
778 if (linepos[0] == '\0')
784 if (linepos[0] == '\0')
786 if (isspace(linepos[0]))
788 if (linepos[0] == '=')
790 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
791 if (linepos[1] == '=')
795 /* remember end of key */
798 /* skip whitespace after key */
799 while (isspace(linepos[0]))
801 if (linepos[0] == '\0')
804 /* get operation type */
805 if (linepos[0] == '=' && linepos[1] == '=') {
808 } else if (linepos[0] == '!' && linepos[1] == '=') {
811 } else if (linepos[0] == '+' && linepos[1] == '=') {
814 } else if (linepos[0] == '=') {
817 } else if (linepos[0] == ':' && linepos[1] == '=') {
818 *op = OP_ASSIGN_FINAL;
826 /* skip whitespace after operator */
827 while (isspace(linepos[0]))
829 if (linepos[0] == '\0')
833 if (linepos[0] == '"')
840 temp = strchr(linepos, '"');
846 /* move line to next key */
851 /* extract possible KEY{attr} */
852 static const char *get_key_attribute(struct udev *udev, char *str)
857 attr = strchr(str, '{');
860 pos = strchr(attr, '}');
862 log_error("missing closing brace for format\n");
871 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
872 enum operation_type op,
873 const char *value, const void *data)
875 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
876 const char *attr = NULL;
878 memset(token, 0x00, sizeof(struct token));
890 case TK_M_SUBSYSTEMS:
894 case TK_M_IMPORT_FILE:
895 case TK_M_IMPORT_PROG:
897 case TK_M_IMPORT_CMDLINE:
898 case TK_M_IMPORT_PARENT:
908 token->key.value_off = rules_add_string(rule_tmp->rules, value);
910 case TK_M_IMPORT_BUILTIN:
911 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
921 token->key.value_off = rules_add_string(rule_tmp->rules, value);
922 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
925 token->key.value_off = rules_add_string(rule_tmp->rules, value);
927 token->key.mode = *(mode_t *)data;
929 case TK_A_STRING_ESCAPE_NONE:
930 case TK_A_STRING_ESCAPE_REPLACE:
931 case TK_A_DB_PERSIST:
933 case TK_A_RUN_BUILTIN:
934 case TK_A_RUN_PROGRAM:
935 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
936 token->key.value_off = rules_add_string(rule_tmp->rules, value);
938 case TK_A_INOTIFY_WATCH:
939 case TK_A_DEVLINK_PRIO:
940 token->key.devlink_prio = *(int *)data;
943 token->key.uid = *(uid_t *)data;
946 token->key.gid = *(gid_t *)data;
949 token->key.mode = *(mode_t *)data;
951 case TK_A_STATIC_NODE:
952 token->key.value_off = rules_add_string(rule_tmp->rules, value);
954 case TK_M_EVENT_TIMEOUT:
955 token->key.event_timeout = *(int *)data;
958 case TK_M_PARENTS_MIN:
959 case TK_M_PARENTS_MAX:
963 log_error("wrong type %u\n", type);
967 if (value != NULL && type < TK_M_MAX) {
968 /* check if we need to split or call fnmatch() while matching rules */
969 enum string_glob_type glob;
973 has_split = (strchr(value, '|') != NULL);
974 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
975 if (has_split && has_glob) {
976 glob = GL_SPLIT_GLOB;
977 } else if (has_split) {
979 } else if (has_glob) {
980 if (streq(value, "?*"))
987 token->key.glob = glob;
990 if (value != NULL && type > TK_M_MAX) {
991 /* check if assigned value has substitution chars */
993 token->key.subst = SB_SUBSYS;
994 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
995 token->key.subst = SB_FORMAT;
997 token->key.subst = SB_NONE;
1001 /* check if property/attribut name has substitution chars */
1003 token->key.attrsubst = SB_SUBSYS;
1004 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
1005 token->key.attrsubst = SB_FORMAT;
1007 token->key.attrsubst = SB_NONE;
1010 token->key.type = type;
1012 rule_tmp->token_cur++;
1013 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
1014 log_error("temporary rule array too small\n");
1020 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1023 unsigned int start = 0;
1024 unsigned int end = rule_tmp->token_cur;
1026 for (i = 0; i < rule_tmp->token_cur; i++) {
1027 enum token_type next_val = TK_UNSET;
1028 unsigned int next_idx = 0;
1031 /* find smallest value */
1032 for (j = start; j < end; j++) {
1033 if (rule_tmp->token[j].type == TK_UNSET)
1035 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1036 next_val = rule_tmp->token[j].type;
1041 /* add token and mark done */
1042 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1044 rule_tmp->token[next_idx].type = TK_UNSET;
1047 if (next_idx == start)
1049 if (next_idx+1 == end)
1055 static int add_rule(struct udev_rules *rules, char *line,
1056 const char *filename, unsigned int filename_off, unsigned int lineno)
1060 struct rule_tmp rule_tmp;
1062 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1063 rule_tmp.rules = rules;
1064 rule_tmp.rule.type = TK_RULE;
1065 /* the offset in the rule is limited to unsigned short */
1066 if (filename_off < USHRT_MAX)
1067 rule_tmp.rule.rule.filename_off = filename_off;
1068 rule_tmp.rule.rule.filename_line = lineno;
1074 enum operation_type op;
1076 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1077 /* Avoid erroring on trailing whitespace. This is probably rare
1078 * so save the work for the error case instead of always trying
1079 * to strip the trailing whitespace with strstrip(). */
1080 while (isblank(*linepos))
1083 /* If we aren't at the end of the line, this is a parsing error.
1084 * Make a best effort to describe where the problem is. */
1085 if (*linepos != '\n') {
1086 char buf[2] = {linepos[1]};
1087 _cleanup_free_ char *tmp;
1090 log_error("invalid key/value pair in file %s on line %u,"
1091 "starting at character %tu ('%s')\n",
1092 filename, lineno, linepos - line + 1, tmp);
1093 if (linepos[1] == '#')
1094 log_info("hint: comments can only start at beginning of line");
1099 if (streq(key, "ACTION")) {
1100 if (op > OP_MATCH_MAX) {
1101 log_error("invalid ACTION operation\n");
1104 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1108 if (streq(key, "DEVPATH")) {
1109 if (op > OP_MATCH_MAX) {
1110 log_error("invalid DEVPATH operation\n");
1113 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1117 if (streq(key, "KERNEL")) {
1118 if (op > OP_MATCH_MAX) {
1119 log_error("invalid KERNEL operation\n");
1122 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1126 if (streq(key, "SUBSYSTEM")) {
1127 if (op > OP_MATCH_MAX) {
1128 log_error("invalid SUBSYSTEM operation\n");
1131 /* bus, class, subsystem events should all be the same */
1132 if (streq(value, "subsystem") ||
1133 streq(value, "bus") ||
1134 streq(value, "class")) {
1135 if (streq(value, "bus") || streq(value, "class"))
1136 log_error("'%s' must be specified as 'subsystem' \n"
1137 "please fix it in %s:%u", value, filename, lineno);
1138 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1140 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1144 if (streq(key, "DRIVER")) {
1145 if (op > OP_MATCH_MAX) {
1146 log_error("invalid DRIVER operation\n");
1149 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1153 if (startswith(key, "ATTR{")) {
1154 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1156 log_error("error parsing ATTR attribute\n");
1159 if (op < OP_MATCH_MAX) {
1160 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1162 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1167 if (startswith(key, "SECLABEL{")) {
1168 attr = get_key_attribute(rules->udev, key + sizeof("SECLABEL")-1);
1170 log_error("error parsing SECLABEL attribute\n");
1174 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1178 if (streq(key, "KERNELS")) {
1179 if (op > OP_MATCH_MAX) {
1180 log_error("invalid KERNELS operation\n");
1183 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1187 if (streq(key, "SUBSYSTEMS")) {
1188 if (op > OP_MATCH_MAX) {
1189 log_error("invalid SUBSYSTEMS operation\n");
1192 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1196 if (streq(key, "DRIVERS")) {
1197 if (op > OP_MATCH_MAX) {
1198 log_error("invalid DRIVERS operation\n");
1201 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1205 if (startswith(key, "ATTRS{")) {
1206 if (op > OP_MATCH_MAX) {
1207 log_error("invalid ATTRS operation\n");
1210 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1212 log_error("error parsing ATTRS attribute\n");
1215 if (startswith(attr, "device/"))
1216 log_error("the 'device' link may not be available in a future kernel, "
1217 "please fix it in %s:%u", filename, lineno);
1218 else if (strstr(attr, "../") != NULL)
1219 log_error("do not reference parent sysfs directories directly, "
1220 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1221 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1225 if (streq(key, "TAGS")) {
1226 if (op > OP_MATCH_MAX) {
1227 log_error("invalid TAGS operation\n");
1230 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1234 if (startswith(key, "ENV{")) {
1235 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1237 log_error("error parsing ENV attribute\n");
1240 if (op < OP_MATCH_MAX) {
1241 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1244 static const char *blacklist[] = {
1259 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1260 if (!streq(attr, blacklist[i]))
1262 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
1265 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1271 if (streq(key, "TAG")) {
1272 if (op < OP_MATCH_MAX)
1273 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1275 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1279 if (streq(key, "PROGRAM")) {
1280 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1284 if (streq(key, "RESULT")) {
1285 if (op > OP_MATCH_MAX) {
1286 log_error("invalid RESULT operation\n");
1289 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1293 if (startswith(key, "IMPORT")) {
1294 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1296 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1299 if (streq(attr, "program")) {
1300 /* find known built-in command */
1301 if (value[0] != '/') {
1302 enum udev_builtin_cmd cmd;
1304 cmd = udev_builtin_lookup(value);
1305 if (cmd < UDEV_BUILTIN_MAX) {
1306 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1307 value, filename, lineno);
1308 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1312 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1313 } else if (streq(attr, "builtin")) {
1314 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1316 if (cmd < UDEV_BUILTIN_MAX)
1317 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1319 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1320 } else if (streq(attr, "file")) {
1321 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1322 } else if (streq(attr, "db")) {
1323 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1324 } else if (streq(attr, "cmdline")) {
1325 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1326 } else if (streq(attr, "parent")) {
1327 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1329 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno);
1333 if (startswith(key, "TEST")) {
1336 if (op > OP_MATCH_MAX) {
1337 log_error("invalid TEST operation\n");
1340 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1342 mode = strtol(attr, NULL, 8);
1343 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1345 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1350 if (startswith(key, "RUN")) {
1351 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1355 if (streq(attr, "builtin")) {
1356 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1358 if (cmd < UDEV_BUILTIN_MAX)
1359 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1361 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1362 } else if (streq(attr, "program")) {
1363 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1365 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1367 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno);
1373 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1374 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1378 if (streq(key, "LABEL")) {
1379 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1383 if (streq(key, "GOTO")) {
1384 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1388 if (startswith(key, "NAME")) {
1389 if (op < OP_MATCH_MAX) {
1390 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1392 if (streq(value, "%k")) {
1393 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1394 "please remove it from %s:%u\n", filename, lineno);
1397 if (value[0] == '\0') {
1398 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1399 "please remove it from %s:%u\n", filename, lineno);
1402 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1404 rule_tmp.rule.rule.can_set_name = true;
1408 if (streq(key, "SYMLINK")) {
1409 if (op < OP_MATCH_MAX)
1410 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1412 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1413 rule_tmp.rule.rule.can_set_name = true;
1417 if (streq(key, "OWNER")) {
1421 uid = strtoul(value, &endptr, 10);
1422 if (endptr[0] == '\0') {
1423 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1424 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1425 uid = add_uid(rules, value);
1426 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1427 } else if (rules->resolve_names >= 0) {
1428 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1430 rule_tmp.rule.rule.can_set_name = true;
1434 if (streq(key, "GROUP")) {
1438 gid = strtoul(value, &endptr, 10);
1439 if (endptr[0] == '\0') {
1440 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1441 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1442 gid = add_gid(rules, value);
1443 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1444 } else if (rules->resolve_names >= 0) {
1445 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1447 rule_tmp.rule.rule.can_set_name = true;
1451 if (streq(key, "MODE")) {
1455 mode = strtol(value, &endptr, 8);
1456 if (endptr[0] == '\0')
1457 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1459 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1460 rule_tmp.rule.rule.can_set_name = true;
1464 if (streq(key, "OPTIONS")) {
1467 pos = strstr(value, "link_priority=");
1469 int prio = atoi(&pos[strlen("link_priority=")]);
1471 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1474 pos = strstr(value, "event_timeout=");
1476 int tout = atoi(&pos[strlen("event_timeout=")]);
1478 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
1481 pos = strstr(value, "string_escape=");
1483 pos = &pos[strlen("string_escape=")];
1484 if (startswith(pos, "none"))
1485 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1486 else if (startswith(pos, "replace"))
1487 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1490 pos = strstr(value, "db_persist");
1492 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1494 pos = strstr(value, "nowatch");
1498 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1500 pos = strstr(value, "watch");
1504 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1508 pos = strstr(value, "static_node=");
1510 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1511 rule_tmp.rule.rule.has_static_node = true;
1517 log_error("unknown key '%s' in %s:%u\n", key, filename, lineno);
1521 /* add rule token */
1522 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1523 if (add_token(rules, &rule_tmp.rule) != 0)
1526 /* add tokens to list, sorted by type */
1527 if (sort_token(rules, &rule_tmp) != 0)
1532 log_error("invalid rule '%s:%u'\n", filename, lineno);
1536 static int parse_file(struct udev_rules *rules, const char *filename)
1539 unsigned int first_token;
1540 unsigned int filename_off;
1541 char line[UTIL_LINE_SIZE];
1545 if (null_or_empty_path(filename)) {
1546 log_debug("skip empty file: %s\n", filename);
1549 log_debug("read rules file: %s\n", filename);
1551 f = fopen(filename, "re");
1555 first_token = rules->token_cur;
1556 filename_off = rules_add_string(rules, filename);
1558 while (fgets(line, sizeof(line), f) != NULL) {
1562 /* skip whitespace */
1565 while (isspace(key[0]))
1576 /* continue reading if backslash+newline is found */
1577 while (line[len-2] == '\\') {
1578 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1580 if (strlen(&line[len-2]) < 2)
1586 if (len+1 >= sizeof(line)) {
1587 log_error("line too long '%s':%u, ignored\n", filename, line_nr);
1590 add_rule(rules, key, filename, filename_off, line_nr);
1594 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1595 for (i = first_token+1; i < rules->token_cur; i++) {
1596 if (rules->tokens[i].type == TK_A_GOTO) {
1597 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1600 for (j = i+1; j < rules->token_cur; j++) {
1601 if (rules->tokens[j].type != TK_RULE)
1603 if (rules->tokens[j].rule.label_off == 0)
1605 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1607 rules->tokens[i].key.rule_goto = j;
1610 if (rules->tokens[i].key.rule_goto == 0)
1611 log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename);
1617 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1619 struct udev_rules *rules;
1620 struct udev_list file_list;
1621 struct token end_token;
1625 rules = calloc(1, sizeof(struct udev_rules));
1629 rules->resolve_names = resolve_names;
1630 udev_list_init(udev, &file_list, true);
1632 /* init token array and string buffer */
1633 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1634 if (rules->tokens == NULL)
1635 return udev_rules_unref(rules);
1636 rules->token_max = PREALLOC_TOKEN;
1638 rules->strbuf = strbuf_new();
1640 return udev_rules_unref(rules);
1642 rules->dirs = strv_new("/etc/udev/rules.d",
1643 "/run/udev/rules.d",
1644 UDEVLIBEXECDIR "/rules.d",
1647 log_error("failed to build config directory array");
1648 return udev_rules_unref(rules);
1650 if (!path_strv_canonicalize(rules->dirs)) {
1651 log_error("failed to canonicalize config directories\n");
1652 return udev_rules_unref(rules);
1654 strv_uniq(rules->dirs);
1656 rules->dirs_ts_usec = calloc(strv_length(rules->dirs), sizeof(usec_t));
1657 if(!rules->dirs_ts_usec)
1658 return udev_rules_unref(rules);
1659 udev_rules_check_timestamp(rules);
1661 r = conf_files_list_strv(&files, ".rules", NULL, (const char **)rules->dirs);
1663 log_error("failed to enumerate rules files: %s\n", strerror(-r));
1664 return udev_rules_unref(rules);
1668 * The offset value in the rules strct is limited; add all
1669 * rules file names to the beginning of the string buffer.
1671 STRV_FOREACH(f, files)
1672 rules_add_string(rules, *f);
1674 STRV_FOREACH(f, files)
1675 parse_file(rules, *f);
1679 memset(&end_token, 0x00, sizeof(struct token));
1680 end_token.type = TK_END;
1681 add_token(rules, &end_token);
1682 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings\n",
1683 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1685 /* cleanup temporary strbuf data */
1686 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used\n",
1687 rules->strbuf->in_count, rules->strbuf->in_len,
1688 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1689 strbuf_complete(rules->strbuf);
1691 /* cleanup uid/gid cache */
1694 rules->uids_cur = 0;
1695 rules->uids_max = 0;
1698 rules->gids_cur = 0;
1699 rules->gids_max = 0;
1705 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1709 free(rules->tokens);
1710 strbuf_cleanup(rules->strbuf);
1713 strv_free(rules->dirs);
1714 free(rules->dirs_ts_usec);
1719 bool udev_rules_check_timestamp(struct udev_rules *rules)
1722 bool changed = false;
1727 for (i = 0; rules->dirs[i]; i++) {
1730 if (stat(rules->dirs[i], &stats) < 0)
1733 if (rules->dirs_ts_usec[i] == timespec_load(&stats.st_mtim))
1737 if (rules->dirs_ts_usec[i] != 0) {
1738 log_debug("reload - timestamp of '%s' changed\n", rules->dirs[i]);
1742 /* update timestamp */
1743 rules->dirs_ts_usec[i] = timespec_load(&stats.st_mtim);
1749 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1751 char *key_value = rules_str(rules, token->key.value_off);
1758 switch (token->key.glob) {
1760 match = (streq(key_value, val));
1763 match = (fnmatch(key_value, val, 0) == 0);
1770 s = rules_str(rules, token->key.value_off);
1775 next = strchr(s, '|');
1777 size_t matchlen = (size_t)(next - s);
1779 match = (matchlen == len && strneq(s, val, matchlen));
1783 match = (streq(s, val));
1792 char value[UTIL_PATH_SIZE];
1794 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1796 while (key_value != NULL) {
1797 pos = strchr(key_value, '|');
1802 match = (fnmatch(key_value, val, 0) == 0);
1810 match = (val[0] != '\0');
1816 if (match && (token->key.op == OP_MATCH))
1818 if (!match && (token->key.op == OP_NOMATCH))
1823 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1826 char nbuf[UTIL_NAME_SIZE];
1828 char vbuf[UTIL_NAME_SIZE];
1831 name = rules_str(rules, cur->key.attr_off);
1832 switch (cur->key.attrsubst) {
1834 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1838 value = udev_device_get_sysattr_value(dev, name);
1843 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1851 /* remove trailing whitespace, if not asked to match for it */
1852 len = strlen(value);
1853 if (len > 0 && isspace(value[len-1])) {
1854 const char *key_value;
1857 key_value = rules_str(rules, cur->key.value_off);
1858 klen = strlen(key_value);
1859 if (klen > 0 && !isspace(key_value[klen-1])) {
1860 if (value != vbuf) {
1861 strscpy(vbuf, sizeof(vbuf), value);
1864 while (len > 0 && isspace(vbuf[--len]))
1869 return match_key(rules, cur, value);
1878 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
1882 enum escape_type esc = ESCAPE_UNSET;
1885 if (rules->tokens == NULL)
1888 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1889 (major(udev_device_get_devnum(event->dev)) > 0 ||
1890 udev_device_get_ifindex(event->dev) > 0));
1892 /* loop through token list, match, run actions or forward to next rule */
1893 cur = &rules->tokens[0];
1896 dump_token(rules, cur);
1897 switch (cur->type) {
1901 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1902 if (!can_set_name && rule->rule.can_set_name)
1907 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1911 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1915 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1918 case TK_M_DEVLINK: {
1919 struct udev_list_entry *list_entry;
1922 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1923 const char *devlink;
1925 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1926 if (match_key(rules, cur, devlink) == 0) {
1936 if (match_key(rules, cur, event->name) != 0)
1940 const char *key_name = rules_str(rules, cur->key.attr_off);
1943 value = udev_device_get_property_value(event->dev, key_name);
1946 if (match_key(rules, cur, value))
1951 struct udev_list_entry *list_entry;
1954 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1955 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1960 if (!match && (cur->key.op != OP_NOMATCH))
1964 case TK_M_SUBSYSTEM:
1965 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1969 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1972 case TK_M_WAITFOR: {
1973 char filename[UTIL_PATH_SIZE];
1976 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1977 found = (wait_for_file(event->dev, filename, 10) == 0);
1978 if (!found && (cur->key.op != OP_NOMATCH))
1983 if (match_attr(rules, event->dev, event, cur) != 0)
1987 case TK_M_SUBSYSTEMS:
1993 /* get whole sequence of parent matches */
1995 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1998 /* loop over parents */
1999 event->dev_parent = event->dev;
2003 /* loop over sequence of parent match keys */
2004 for (key = cur; key < next; key++ ) {
2005 dump_token(rules, key);
2008 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2011 case TK_M_SUBSYSTEMS:
2012 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2016 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2020 if (match_attr(rules, event->dev_parent, event, key) != 0)
2024 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
2026 if (match && key->key.op == OP_NOMATCH)
2028 if (!match && key->key.op == OP_MATCH)
2039 event->dev_parent = udev_device_get_parent(event->dev_parent);
2040 if (event->dev_parent == NULL)
2043 /* move behind our sequence of parent match keys */
2048 char filename[UTIL_PATH_SIZE];
2049 struct stat statbuf;
2052 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
2053 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2054 if (filename[0] != '/') {
2055 char tmp[UTIL_PATH_SIZE];
2057 strscpy(tmp, sizeof(tmp), filename);
2058 strscpyl(filename, sizeof(filename),
2059 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2062 attr_subst_subdir(filename, sizeof(filename));
2064 match = (stat(filename, &statbuf) == 0);
2065 if (match && cur->key.mode > 0)
2066 match = ((statbuf.st_mode & cur->key.mode) > 0);
2067 if (match && cur->key.op == OP_NOMATCH)
2069 if (!match && cur->key.op == OP_MATCH)
2073 case TK_M_EVENT_TIMEOUT:
2074 log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout);
2075 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2077 case TK_M_PROGRAM: {
2078 char program[UTIL_PATH_SIZE];
2080 char result[UTIL_PATH_SIZE];
2082 free(event->program_result);
2083 event->program_result = NULL;
2084 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2085 envp = udev_device_get_properties_envp(event->dev);
2086 log_debug("PROGRAM '%s' %s:%u\n",
2088 rules_str(rules, rule->rule.filename_off),
2089 rule->rule.filename_line);
2091 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2092 if (cur->key.op != OP_NOMATCH)
2097 util_remove_trailing_chars(result, '\n');
2098 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2099 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2101 log_debug("%i character(s) replaced\n" , count);
2103 event->program_result = strdup(result);
2104 if (cur->key.op == OP_NOMATCH)
2109 case TK_M_IMPORT_FILE: {
2110 char import[UTIL_PATH_SIZE];
2112 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2113 if (import_file_into_properties(event->dev, import) != 0)
2114 if (cur->key.op != OP_NOMATCH)
2118 case TK_M_IMPORT_PROG: {
2119 char import[UTIL_PATH_SIZE];
2121 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2122 log_debug("IMPORT '%s' %s:%u\n",
2124 rules_str(rules, rule->rule.filename_off),
2125 rule->rule.filename_line);
2127 if (import_program_into_properties(event, import, sigmask) != 0)
2128 if (cur->key.op != OP_NOMATCH)
2132 case TK_M_IMPORT_BUILTIN: {
2133 char command[UTIL_PATH_SIZE];
2135 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2136 /* check if we ran already */
2137 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2138 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2139 udev_builtin_name(cur->key.builtin_cmd),
2140 rules_str(rules, rule->rule.filename_off),
2141 rule->rule.filename_line);
2142 /* return the result from earlier run */
2143 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2144 if (cur->key.op != OP_NOMATCH)
2149 event->builtin_run |= (1 << cur->key.builtin_cmd);
2152 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2153 log_debug("IMPORT builtin '%s' %s:%u\n",
2154 udev_builtin_name(cur->key.builtin_cmd),
2155 rules_str(rules, rule->rule.filename_off),
2156 rule->rule.filename_line);
2158 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2159 /* remember failure */
2160 log_debug("IMPORT builtin '%s' returned non-zero\n",
2161 udev_builtin_name(cur->key.builtin_cmd));
2162 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2163 if (cur->key.op != OP_NOMATCH)
2168 case TK_M_IMPORT_DB: {
2169 const char *key = rules_str(rules, cur->key.value_off);
2172 value = udev_device_get_property_value(event->dev_db, key);
2173 if (value != NULL) {
2174 struct udev_list_entry *entry;
2176 entry = udev_device_add_property(event->dev, key, value);
2177 udev_list_entry_set_num(entry, true);
2179 if (cur->key.op != OP_NOMATCH)
2184 case TK_M_IMPORT_CMDLINE: {
2186 bool imported = false;
2188 f = fopen("/proc/cmdline", "re");
2192 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2193 const char *key = rules_str(rules, cur->key.value_off);
2196 pos = strstr(cmdline, key);
2198 struct udev_list_entry *entry;
2201 if (pos[0] == '\0' || isspace(pos[0])) {
2202 /* we import simple flags as 'FLAG=1' */
2203 entry = udev_device_add_property(event->dev, key, "1");
2204 udev_list_entry_set_num(entry, true);
2206 } else if (pos[0] == '=') {
2211 while (pos[0] != '\0' && !isspace(pos[0]))
2214 entry = udev_device_add_property(event->dev, key, value);
2215 udev_list_entry_set_num(entry, true);
2222 if (!imported && cur->key.op != OP_NOMATCH)
2226 case TK_M_IMPORT_PARENT: {
2227 char import[UTIL_PATH_SIZE];
2229 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2230 if (import_parent_into_properties(event->dev, import) != 0)
2231 if (cur->key.op != OP_NOMATCH)
2236 if (match_key(rules, cur, event->program_result) != 0)
2239 case TK_A_STRING_ESCAPE_NONE:
2242 case TK_A_STRING_ESCAPE_REPLACE:
2243 esc = ESCAPE_REPLACE;
2245 case TK_A_DB_PERSIST:
2246 udev_device_set_db_persist(event->dev);
2248 case TK_A_INOTIFY_WATCH:
2249 if (event->inotify_watch_final)
2251 if (cur->key.op == OP_ASSIGN_FINAL)
2252 event->inotify_watch_final = true;
2253 event->inotify_watch = cur->key.watch;
2255 case TK_A_DEVLINK_PRIO:
2256 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2259 char owner[UTIL_NAME_SIZE];
2261 if (event->owner_final)
2263 if (cur->key.op == OP_ASSIGN_FINAL)
2264 event->owner_final = true;
2265 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2266 event->owner_set = true;
2267 event->uid = util_lookup_user(event->udev, owner);
2268 log_debug("OWNER %u %s:%u\n",
2270 rules_str(rules, rule->rule.filename_off),
2271 rule->rule.filename_line);
2275 char group[UTIL_NAME_SIZE];
2277 if (event->group_final)
2279 if (cur->key.op == OP_ASSIGN_FINAL)
2280 event->group_final = true;
2281 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2282 event->group_set = true;
2283 event->gid = util_lookup_group(event->udev, group);
2284 log_debug("GROUP %u %s:%u\n",
2286 rules_str(rules, rule->rule.filename_off),
2287 rule->rule.filename_line);
2291 char mode_str[UTIL_NAME_SIZE];
2295 if (event->mode_final)
2297 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2298 mode = strtol(mode_str, &endptr, 8);
2299 if (endptr[0] != '\0') {
2300 log_error("ignoring invalid mode '%s'\n", mode_str);
2303 if (cur->key.op == OP_ASSIGN_FINAL)
2304 event->mode_final = true;
2305 event->mode_set = true;
2307 log_debug("MODE %#o %s:%u\n",
2309 rules_str(rules, rule->rule.filename_off),
2310 rule->rule.filename_line);
2314 if (event->owner_final)
2316 if (cur->key.op == OP_ASSIGN_FINAL)
2317 event->owner_final = true;
2318 event->owner_set = true;
2319 event->uid = cur->key.uid;
2320 log_debug("OWNER %u %s:%u\n",
2322 rules_str(rules, rule->rule.filename_off),
2323 rule->rule.filename_line);
2326 if (event->group_final)
2328 if (cur->key.op == OP_ASSIGN_FINAL)
2329 event->group_final = true;
2330 event->group_set = true;
2331 event->gid = cur->key.gid;
2332 log_debug("GROUP %u %s:%u\n",
2334 rules_str(rules, rule->rule.filename_off),
2335 rule->rule.filename_line);
2338 if (event->mode_final)
2340 if (cur->key.op == OP_ASSIGN_FINAL)
2341 event->mode_final = true;
2342 event->mode_set = true;
2343 event->mode = cur->key.mode;
2344 log_debug("MODE %#o %s:%u\n",
2346 rules_str(rules, rule->rule.filename_off),
2347 rule->rule.filename_line);
2349 case TK_A_SECLABEL: {
2350 const char *name, *label;
2352 name = rules_str(rules, cur->key.attr_off);
2353 label = rules_str(rules, cur->key.value_off);
2354 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2355 udev_list_cleanup(&event->seclabel_list);
2356 udev_list_entry_add(&event->seclabel_list, name, label);
2357 log_debug("SECLABEL{%s}='%s' %s:%u\n",
2359 rules_str(rules, rule->rule.filename_off),
2360 rule->rule.filename_line);
2364 const char *name = rules_str(rules, cur->key.attr_off);
2365 char *value = rules_str(rules, cur->key.value_off);
2366 char value_new[UTIL_NAME_SIZE];
2367 const char *value_old = NULL;
2368 struct udev_list_entry *entry;
2370 if (value[0] == '\0') {
2371 if (cur->key.op == OP_ADD)
2373 udev_device_add_property(event->dev, name, NULL);
2377 if (cur->key.op == OP_ADD)
2378 value_old = udev_device_get_property_value(event->dev, name);
2380 char temp[UTIL_NAME_SIZE];
2382 /* append value separated by space */
2383 udev_event_apply_format(event, value, temp, sizeof(temp));
2384 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2386 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2388 entry = udev_device_add_property(event->dev, name, value_new);
2389 /* store in db, skip private keys */
2391 udev_list_entry_set_num(entry, true);
2395 char tag[UTIL_PATH_SIZE];
2398 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2399 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2400 udev_device_cleanup_tags_list(event->dev);
2401 for (p = tag; *p != '\0'; p++) {
2402 if ((*p >= 'a' && *p <= 'z') ||
2403 (*p >= 'A' && *p <= 'Z') ||
2404 (*p >= '0' && *p <= '9') ||
2405 *p == '-' || *p == '_')
2407 log_error("ignoring invalid tag name '%s'\n", tag);
2410 udev_device_add_tag(event->dev, tag);
2414 const char *name = rules_str(rules, cur->key.value_off);
2416 char name_str[UTIL_PATH_SIZE];
2419 if (event->name_final)
2421 if (cur->key.op == OP_ASSIGN_FINAL)
2422 event->name_final = true;
2423 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2424 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2425 count = util_replace_chars(name_str, "/");
2427 log_debug("%i character(s) replaced\n", count);
2429 if (major(udev_device_get_devnum(event->dev)) &&
2430 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2431 log_error("NAME=\"%s\" ignored, kernel device nodes "
2432 "can not be renamed; please fix it in %s:%u\n", name,
2433 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2437 event->name = strdup(name_str);
2438 log_debug("NAME '%s' %s:%u\n",
2440 rules_str(rules, rule->rule.filename_off),
2441 rule->rule.filename_line);
2444 case TK_A_DEVLINK: {
2445 char temp[UTIL_PATH_SIZE];
2446 char filename[UTIL_PATH_SIZE];
2450 if (event->devlink_final)
2452 if (major(udev_device_get_devnum(event->dev)) == 0)
2454 if (cur->key.op == OP_ASSIGN_FINAL)
2455 event->devlink_final = true;
2456 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2457 udev_device_cleanup_devlinks_list(event->dev);
2459 /* allow multiple symlinks separated by spaces */
2460 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2461 if (esc == ESCAPE_UNSET)
2462 count = util_replace_chars(temp, "/ ");
2463 else if (esc == ESCAPE_REPLACE)
2464 count = util_replace_chars(temp, "/");
2466 log_debug("%i character(s) replaced\n" , count);
2468 while (isspace(pos[0]))
2470 next = strchr(pos, ' ');
2471 while (next != NULL) {
2473 log_debug("LINK '%s' %s:%u\n", pos,
2474 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2475 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2476 udev_device_add_devlink(event->dev, filename);
2477 while (isspace(next[1]))
2480 next = strchr(pos, ' ');
2482 if (pos[0] != '\0') {
2483 log_debug("LINK '%s' %s:%u\n", pos,
2484 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2485 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2486 udev_device_add_devlink(event->dev, filename);
2491 const char *key_name = rules_str(rules, cur->key.attr_off);
2492 char attr[UTIL_PATH_SIZE];
2493 char value[UTIL_NAME_SIZE];
2496 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2497 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2498 attr_subst_subdir(attr, sizeof(attr));
2500 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2501 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
2502 rules_str(rules, rule->rule.filename_off),
2503 rule->rule.filename_line);
2504 f = fopen(attr, "we");
2506 if (fprintf(f, "%s", value) <= 0)
2507 log_error("error writing ATTR{%s}: %m\n", attr);
2510 log_error("error opening ATTR{%s} for writing: %m\n", attr);
2514 case TK_A_RUN_BUILTIN:
2515 case TK_A_RUN_PROGRAM: {
2516 struct udev_list_entry *entry;
2518 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2519 udev_list_cleanup(&event->run_list);
2520 log_debug("RUN '%s' %s:%u\n",
2521 rules_str(rules, cur->key.value_off),
2522 rules_str(rules, rule->rule.filename_off),
2523 rule->rule.filename_line);
2524 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2525 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2529 if (cur->key.rule_goto == 0)
2531 cur = &rules->tokens[cur->key.rule_goto];
2536 case TK_M_PARENTS_MIN:
2537 case TK_M_PARENTS_MAX:
2540 log_error("wrong type %u\n", cur->type);
2547 /* fast-forward to next rule */
2548 cur = rule + rule->rule.token_count;
2552 int udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2559 _cleanup_strv_free_ char **tags = NULL;
2562 _cleanup_free_ char *path = NULL;
2565 if (rules->tokens == NULL)
2568 cur = &rules->tokens[0];
2571 switch (cur->type) {
2576 /* skip rules without a static_node tag */
2577 if (!rule->rule.has_static_node)
2593 mode = cur->key.mode;
2596 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2601 case TK_A_STATIC_NODE: {
2602 char device_node[UTIL_PATH_SIZE];
2603 char tags_dir[UTIL_PATH_SIZE];
2604 char tag_symlink[UTIL_PATH_SIZE];
2607 /* we assure, that the permissions tokens are sorted before the static token */
2608 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2610 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2611 if (stat(device_node, &stats) != 0)
2613 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2617 /* Export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2619 STRV_FOREACH(t, tags) {
2620 _cleanup_free_ char *unescaped_filename = NULL;
2622 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2623 r = mkdir_p(tags_dir, 0755);
2625 log_error("failed to create %s: %s\n", tags_dir, strerror(-r));
2629 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2631 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2632 r = symlink(device_node, tag_symlink);
2633 if (r < 0 && errno != EEXIST) {
2634 log_error("failed to create symlink %s -> %s: %s\n", tag_symlink, device_node, strerror(errno));
2641 /* don't touch the permissions if only the tags were set */
2642 if (mode == 0 && uid == 0 && gid == 0)
2651 if (mode != (stats.st_mode & 01777)) {
2652 r = chmod(device_node, mode);
2654 log_error("failed to chmod '%s' %#o\n", device_node, mode);
2657 log_debug("chmod '%s' %#o\n", device_node, mode);
2660 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2661 r = chown(device_node, uid, gid);
2663 log_error("failed to chown '%s' %u %u \n", device_node, uid, gid);
2666 log_debug("chown '%s' %u %u\n", device_node, uid, gid);
2669 utimensat(AT_FDCWD, device_node, NULL, 0);
2679 /* fast-forward to next rule */
2680 cur = rule + rule->rule.token_count;
2687 fchmod(fileno(f), 0644);
2688 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2690 unlink("/run/udev/static_node-tags");