2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "path-util.h"
34 #include "conf-files.h"
39 #define PREALLOC_TOKEN 2048
42 unsigned int name_off;
49 static const char* const rules_dirs[] = {
52 UDEVLIBEXECDIR "/rules.d",
60 /* every key in the rules file becomes a token */
62 unsigned int token_cur;
63 unsigned int token_max;
65 /* all key strings are copied and de-duplicated in a single continuous string buffer */
66 struct strbuf *strbuf;
68 /* during rule parsing, uid/gid lookup results are cached */
70 unsigned int uids_cur;
71 unsigned int uids_max;
73 unsigned int gids_cur;
74 unsigned int gids_max;
77 static char *rules_str(struct udev_rules *rules, unsigned int off) {
78 return rules->strbuf->buf + off;
81 static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
82 return strbuf_add_string(rules->strbuf, s, strlen(s));
85 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
98 enum string_glob_type {
100 GL_PLAIN, /* no special chars */
101 GL_GLOB, /* shell globs ?,*,[] */
102 GL_SPLIT, /* multi-value A|B */
103 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
104 GL_SOMETHING, /* commonly used "?*" */
107 enum string_subst_type {
114 /* tokens of a rule are sorted/handled in this order */
119 TK_M_ACTION, /* val */
120 TK_M_DEVPATH, /* val */
121 TK_M_KERNEL, /* val */
122 TK_M_DEVLINK, /* val */
124 TK_M_ENV, /* val, attr */
126 TK_M_SUBSYSTEM, /* val */
127 TK_M_DRIVER, /* val */
128 TK_M_WAITFOR, /* val */
129 TK_M_ATTR, /* val, attr */
132 TK_M_KERNELS, /* val */
133 TK_M_SUBSYSTEMS, /* val */
134 TK_M_DRIVERS, /* val */
135 TK_M_ATTRS, /* val, attr */
139 TK_M_TEST, /* val, mode_t */
140 TK_M_PROGRAM, /* val */
141 TK_M_IMPORT_FILE, /* val */
142 TK_M_IMPORT_PROG, /* val */
143 TK_M_IMPORT_BUILTIN, /* val */
144 TK_M_IMPORT_DB, /* val */
145 TK_M_IMPORT_CMDLINE, /* val */
146 TK_M_IMPORT_PARENT, /* val */
147 TK_M_RESULT, /* val */
150 TK_A_STRING_ESCAPE_NONE,
151 TK_A_STRING_ESCAPE_REPLACE,
153 TK_A_INOTIFY_WATCH, /* int */
154 TK_A_DEVLINK_PRIO, /* int */
155 TK_A_OWNER, /* val */
156 TK_A_GROUP, /* val */
158 TK_A_OWNER_ID, /* uid_t */
159 TK_A_GROUP_ID, /* gid_t */
160 TK_A_MODE_ID, /* mode_t */
162 TK_A_STATIC_NODE, /* val */
163 TK_A_SECLABEL, /* val, attr */
164 TK_A_ENV, /* val, attr */
166 TK_A_DEVLINK, /* val */
167 TK_A_ATTR, /* val, attr */
168 TK_A_RUN_BUILTIN, /* val, bool */
169 TK_A_RUN_PROGRAM, /* val, bool */
170 TK_A_GOTO, /* size_t */
175 /* we try to pack stuff in a way that we take only 12 bytes per token */
178 unsigned char type; /* same in rule and key */
180 enum token_type type:8;
182 bool has_static_node:1;
183 unsigned int unused:6;
184 unsigned short token_count;
185 unsigned int label_off;
186 unsigned short filename_off;
187 unsigned short filename_line;
190 enum token_type type:8;
191 enum operation_type op:8;
192 enum string_glob_type glob:8;
193 enum string_subst_type subst:4;
194 enum string_subst_type attrsubst:4;
195 unsigned int value_off;
197 unsigned int attr_off;
198 unsigned int rule_goto;
204 enum udev_builtin_cmd builtin_cmd;
212 struct udev_rules *rules;
214 struct token token[MAX_TK];
215 unsigned int token_cur;
219 static const char *operation_str(enum operation_type type) {
220 static const char *operation_strs[] = {
221 [OP_UNSET] = "UNSET",
222 [OP_MATCH] = "match",
223 [OP_NOMATCH] = "nomatch",
224 [OP_MATCH_MAX] = "MATCH_MAX",
227 [OP_ASSIGN] = "assign",
228 [OP_ASSIGN_FINAL] = "assign-final",
231 return operation_strs[type];
234 static const char *string_glob_str(enum string_glob_type type) {
235 static const char *string_glob_strs[] = {
236 [GL_UNSET] = "UNSET",
237 [GL_PLAIN] = "plain",
239 [GL_SPLIT] = "split",
240 [GL_SPLIT_GLOB] = "split-glob",
241 [GL_SOMETHING] = "split-glob",
244 return string_glob_strs[type];
247 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_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_SECLABEL] = "A SECLABEL",
295 [TK_A_MODE_ID] = "A MODE_ID",
296 [TK_A_ENV] = "A ENV",
297 [TK_A_TAG] = "A ENV",
298 [TK_A_NAME] = "A NAME",
299 [TK_A_DEVLINK] = "A DEVLINK",
300 [TK_A_ATTR] = "A ATTR",
301 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
302 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
303 [TK_A_GOTO] = "A GOTO",
308 return token_strs[type];
311 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'",
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)",
358 token_str(type), operation_str(op), value, string_glob_str(glob));
360 case TK_M_IMPORT_BUILTIN:
361 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
368 log_debug("%s %s '%s' '%s'(%s)",
369 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
373 log_debug("%s %s '%s'", 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", token_str(type));
381 log_debug("%s %s '%s'(%s) %#o",
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", token_str(type), token->key.watch);
387 case TK_A_DEVLINK_PRIO:
388 log_debug("%s %u", token_str(type), token->key.devlink_prio);
391 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid);
394 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid);
397 log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode);
399 case TK_A_STATIC_NODE:
400 log_debug("%s '%s'", token_str(type), value);
403 log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value);
406 log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto);
409 log_debug("* %s", token_str(type));
411 case TK_M_PARENTS_MIN:
412 case TK_M_PARENTS_MAX:
415 log_debug("unknown type %u", type);
420 static void dump_rules(struct udev_rules *rules) {
423 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
425 rules->token_cur * sizeof(struct token),
428 for (i = 0; i < rules->token_cur; i++)
429 dump_token(rules, &rules->tokens[i]);
432 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
433 static inline void dump_rules(struct udev_rules *rules) {}
436 static int add_token(struct udev_rules *rules, struct token *token) {
437 /* grow buffer if needed */
438 if (rules->token_cur+1 >= rules->token_max) {
439 struct token *tokens;
442 /* double the buffer size */
443 add = rules->token_max;
447 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
450 rules->tokens = tokens;
451 rules->token_max += add;
453 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
458 static uid_t add_uid(struct udev_rules *rules, const char *owner) {
463 /* lookup, if we know it already */
464 for (i = 0; i < rules->uids_cur; i++) {
465 off = rules->uids[i].name_off;
466 if (streq(rules_str(rules, off), owner)) {
467 uid = rules->uids[i].uid;
471 uid = util_lookup_user(rules->udev, owner);
473 /* grow buffer if needed */
474 if (rules->uids_cur+1 >= rules->uids_max) {
475 struct uid_gid *uids;
478 /* double the buffer size */
479 add = rules->uids_max;
483 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
487 rules->uids_max += add;
489 rules->uids[rules->uids_cur].uid = uid;
490 off = rules_add_string(rules, owner);
493 rules->uids[rules->uids_cur].name_off = off;
498 static gid_t add_gid(struct udev_rules *rules, const char *group) {
503 /* lookup, if we know it already */
504 for (i = 0; i < rules->gids_cur; i++) {
505 off = rules->gids[i].name_off;
506 if (streq(rules_str(rules, off), group)) {
507 gid = rules->gids[i].gid;
511 gid = util_lookup_group(rules->udev, group);
513 /* grow buffer if needed */
514 if (rules->gids_cur+1 >= rules->gids_max) {
515 struct uid_gid *gids;
518 /* double the buffer size */
519 add = rules->gids_max;
523 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
527 rules->gids_max += add;
529 rules->gids[rules->gids_cur].gid = gid;
530 off = rules_add_string(rules, group);
533 rules->gids[rules->gids_cur].name_off = off;
538 static int import_property_from_string(struct udev_device *dev, char *line) {
542 struct udev_list_entry *entry;
546 while (isspace(key[0]))
549 /* comment or empty line */
550 if (key[0] == '#' || key[0] == '\0')
553 /* split key/value */
554 val = strchr(key, '=');
561 while (isspace(val[0]))
568 while (isspace(key[len-1]))
572 /* terminate value */
576 while (isspace(val[len-1]))
584 if (val[0] == '"' || val[0] == '\'') {
585 if (val[len-1] != val[0]) {
586 log_debug("inconsistent quoting: '%s', skip", line);
593 entry = udev_device_add_property(dev, key, val);
594 /* store in db, skip private keys */
596 udev_list_entry_set_num(entry, true);
601 static int import_file_into_properties(struct udev_device *dev, const char *filename) {
603 char line[UTIL_LINE_SIZE];
605 f = fopen(filename, "re");
608 while (fgets(line, sizeof(line), f) != NULL)
609 import_property_from_string(dev, line);
614 static int import_program_into_properties(struct udev_event *event,
616 const char *program, const sigset_t *sigmask) {
617 struct udev_device *dev = event->dev;
619 char result[UTIL_LINE_SIZE];
623 envp = udev_device_get_properties_envp(dev);
624 err = udev_event_spawn(event, timeout_usec, program, envp, sigmask, result, sizeof(result));
629 while (line != NULL) {
632 pos = strchr(line, '\n');
637 import_property_from_string(dev, line);
643 static int import_parent_into_properties(struct udev_device *dev, const char *filter) {
644 struct udev_device *dev_parent;
645 struct udev_list_entry *list_entry;
647 dev_parent = udev_device_get_parent(dev);
648 if (dev_parent == NULL)
651 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
652 const char *key = udev_list_entry_get_name(list_entry);
653 const char *val = udev_list_entry_get_value(list_entry);
655 if (fnmatch(filter, key, 0) == 0) {
656 struct udev_list_entry *entry;
658 entry = udev_device_add_property(dev, key, val);
659 /* store in db, skip private keys */
661 udev_list_entry_set_num(entry, true);
667 #define WAIT_LOOP_PER_SECOND 50
668 static int wait_for_file(struct udev_device *dev, const char *file, int timeout) {
669 char filepath[UTIL_PATH_SIZE];
670 char devicepath[UTIL_PATH_SIZE];
672 int loop = timeout * WAIT_LOOP_PER_SECOND;
674 /* a relative path is a device attribute */
675 devicepath[0] = '\0';
676 if (file[0] != '/') {
677 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
678 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
683 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
686 if (stat(file, &stats) == 0) {
687 log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
690 /* make sure, the device did not disappear in the meantime */
691 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
692 log_debug("device disappeared while waiting for '%s'", file);
695 log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
696 nanosleep(&duration, NULL);
698 log_debug("waiting for '%s' failed", file);
702 static int attr_subst_subdir(char *attr, size_t len) {
705 if (strstr(attr, "/*/")) {
707 char dirname[UTIL_PATH_SIZE];
711 strscpy(dirname, sizeof(dirname), attr);
712 pos = strstr(dirname, "/*/");
717 dir = opendir(dirname);
721 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
724 if (dent->d_name[0] == '.')
726 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
727 if (stat(attr, &stats) == 0) {
739 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) {
744 if (linepos == NULL || linepos[0] == '\0')
747 /* skip whitespace */
748 while (isspace(linepos[0]) || linepos[0] == ',')
752 if (linepos[0] == '\0')
758 if (linepos[0] == '\0')
760 if (isspace(linepos[0]))
762 if (linepos[0] == '=')
764 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
765 if (linepos[1] == '=')
769 /* remember end of key */
772 /* skip whitespace after key */
773 while (isspace(linepos[0]))
775 if (linepos[0] == '\0')
778 /* get operation type */
779 if (linepos[0] == '=' && linepos[1] == '=') {
782 } else if (linepos[0] == '!' && linepos[1] == '=') {
785 } else if (linepos[0] == '+' && linepos[1] == '=') {
788 } else if (linepos[0] == '=') {
791 } else if (linepos[0] == ':' && linepos[1] == '=') {
792 *op = OP_ASSIGN_FINAL;
800 /* skip whitespace after operator */
801 while (isspace(linepos[0]))
803 if (linepos[0] == '\0')
807 if (linepos[0] == '"')
814 temp = strchr(linepos, '"');
820 /* move line to next key */
825 /* extract possible KEY{attr} */
826 static const char *get_key_attribute(struct udev *udev, char *str) {
830 attr = strchr(str, '{');
833 pos = strchr(attr, '}');
835 log_error("missing closing brace for format");
844 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
845 enum operation_type op,
846 const char *value, const void *data) {
847 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
848 const char *attr = NULL;
850 memzero(token, sizeof(struct token));
862 case TK_M_SUBSYSTEMS:
866 case TK_M_IMPORT_FILE:
867 case TK_M_IMPORT_PROG:
869 case TK_M_IMPORT_CMDLINE:
870 case TK_M_IMPORT_PARENT:
880 case TK_A_STATIC_NODE:
881 token->key.value_off = rules_add_string(rule_tmp->rules, value);
883 case TK_M_IMPORT_BUILTIN:
884 token->key.value_off = rules_add_string(rule_tmp->rules, value);
885 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
894 token->key.value_off = rules_add_string(rule_tmp->rules, value);
895 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
898 token->key.value_off = rules_add_string(rule_tmp->rules, value);
900 token->key.mode = *(mode_t *)data;
902 case TK_A_STRING_ESCAPE_NONE:
903 case TK_A_STRING_ESCAPE_REPLACE:
904 case TK_A_DB_PERSIST:
906 case TK_A_RUN_BUILTIN:
907 case TK_A_RUN_PROGRAM:
908 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
909 token->key.value_off = rules_add_string(rule_tmp->rules, value);
911 case TK_A_INOTIFY_WATCH:
912 case TK_A_DEVLINK_PRIO:
913 token->key.devlink_prio = *(int *)data;
916 token->key.uid = *(uid_t *)data;
919 token->key.gid = *(gid_t *)data;
922 token->key.mode = *(mode_t *)data;
925 case TK_M_PARENTS_MIN:
926 case TK_M_PARENTS_MAX:
930 log_error("wrong type %u", type);
934 if (value != NULL && type < TK_M_MAX) {
935 /* check if we need to split or call fnmatch() while matching rules */
936 enum string_glob_type glob;
940 has_split = (strchr(value, '|') != NULL);
941 has_glob = string_is_glob(value);
942 if (has_split && has_glob) {
943 glob = GL_SPLIT_GLOB;
944 } else if (has_split) {
946 } else if (has_glob) {
947 if (streq(value, "?*"))
954 token->key.glob = glob;
957 if (value != NULL && type > TK_M_MAX) {
958 /* check if assigned value has substitution chars */
960 token->key.subst = SB_SUBSYS;
961 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
962 token->key.subst = SB_FORMAT;
964 token->key.subst = SB_NONE;
968 /* check if property/attribute name has substitution chars */
970 token->key.attrsubst = SB_SUBSYS;
971 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
972 token->key.attrsubst = SB_FORMAT;
974 token->key.attrsubst = SB_NONE;
977 token->key.type = type;
979 rule_tmp->token_cur++;
980 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
981 log_error("temporary rule array too small");
987 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
989 unsigned int start = 0;
990 unsigned int end = rule_tmp->token_cur;
992 for (i = 0; i < rule_tmp->token_cur; i++) {
993 enum token_type next_val = TK_UNSET;
994 unsigned int next_idx = 0;
997 /* find smallest value */
998 for (j = start; j < end; j++) {
999 if (rule_tmp->token[j].type == TK_UNSET)
1001 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1002 next_val = rule_tmp->token[j].type;
1007 /* add token and mark done */
1008 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1010 rule_tmp->token[next_idx].type = TK_UNSET;
1013 if (next_idx == start)
1015 if (next_idx+1 == end)
1021 static int add_rule(struct udev_rules *rules, char *line,
1022 const char *filename, unsigned int filename_off, unsigned int lineno) {
1025 struct rule_tmp rule_tmp;
1027 memzero(&rule_tmp, sizeof(struct rule_tmp));
1028 rule_tmp.rules = rules;
1029 rule_tmp.rule.type = TK_RULE;
1030 /* the offset in the rule is limited to unsigned short */
1031 if (filename_off < USHRT_MAX)
1032 rule_tmp.rule.rule.filename_off = filename_off;
1033 rule_tmp.rule.rule.filename_line = lineno;
1039 enum operation_type op;
1041 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1042 /* Avoid erroring on trailing whitespace. This is probably rare
1043 * so save the work for the error case instead of always trying
1044 * to strip the trailing whitespace with strstrip(). */
1045 while (isblank(*linepos))
1048 /* If we aren't at the end of the line, this is a parsing error.
1049 * Make a best effort to describe where the problem is. */
1050 if (*linepos != '\n') {
1051 char buf[2] = {linepos[1]};
1052 _cleanup_free_ char *tmp;
1055 log_error("invalid key/value pair in file %s on line %u,"
1056 "starting at character %tu ('%s')\n",
1057 filename, lineno, linepos - line + 1, tmp);
1058 if (linepos[1] == '#')
1059 log_error("hint: comments can only start at beginning of line");
1064 if (streq(key, "ACTION")) {
1065 if (op > OP_MATCH_MAX) {
1066 log_error("invalid ACTION operation");
1069 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1073 if (streq(key, "DEVPATH")) {
1074 if (op > OP_MATCH_MAX) {
1075 log_error("invalid DEVPATH operation");
1078 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1082 if (streq(key, "KERNEL")) {
1083 if (op > OP_MATCH_MAX) {
1084 log_error("invalid KERNEL operation");
1087 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1091 if (streq(key, "SUBSYSTEM")) {
1092 if (op > OP_MATCH_MAX) {
1093 log_error("invalid SUBSYSTEM operation");
1096 /* bus, class, subsystem events should all be the same */
1097 if (streq(value, "subsystem") ||
1098 streq(value, "bus") ||
1099 streq(value, "class")) {
1100 if (streq(value, "bus") || streq(value, "class"))
1101 log_error("'%s' must be specified as 'subsystem' "
1102 "please fix it in %s:%u", value, filename, lineno);
1103 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1105 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1109 if (streq(key, "DRIVER")) {
1110 if (op > OP_MATCH_MAX) {
1111 log_error("invalid DRIVER operation");
1114 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1118 if (startswith(key, "ATTR{")) {
1119 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
1121 log_error("error parsing ATTR attribute");
1124 if (op < OP_MATCH_MAX) {
1125 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1127 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1132 if (startswith(key, "SECLABEL{")) {
1133 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
1135 log_error("error parsing SECLABEL attribute");
1139 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1143 if (streq(key, "KERNELS")) {
1144 if (op > OP_MATCH_MAX) {
1145 log_error("invalid KERNELS operation");
1148 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1152 if (streq(key, "SUBSYSTEMS")) {
1153 if (op > OP_MATCH_MAX) {
1154 log_error("invalid SUBSYSTEMS operation");
1157 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1161 if (streq(key, "DRIVERS")) {
1162 if (op > OP_MATCH_MAX) {
1163 log_error("invalid DRIVERS operation");
1166 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1170 if (startswith(key, "ATTRS{")) {
1171 if (op > OP_MATCH_MAX) {
1172 log_error("invalid ATTRS operation");
1175 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
1177 log_error("error parsing ATTRS attribute");
1180 if (startswith(attr, "device/"))
1181 log_error("the 'device' link may not be available in a future kernel, "
1182 "please fix it in %s:%u", filename, lineno);
1183 else if (strstr(attr, "../") != NULL)
1184 log_error("do not reference parent sysfs directories directly, "
1185 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1186 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1190 if (streq(key, "TAGS")) {
1191 if (op > OP_MATCH_MAX) {
1192 log_error("invalid TAGS operation");
1195 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1199 if (startswith(key, "ENV{")) {
1200 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
1202 log_error("error parsing ENV attribute");
1205 if (op < OP_MATCH_MAX) {
1206 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1209 static const char *blacklist[] = {
1224 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1225 if (!streq(attr, blacklist[i]))
1227 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
1230 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1236 if (streq(key, "TAG")) {
1237 if (op < OP_MATCH_MAX)
1238 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1240 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1244 if (streq(key, "PROGRAM")) {
1245 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1249 if (streq(key, "RESULT")) {
1250 if (op > OP_MATCH_MAX) {
1251 log_error("invalid RESULT operation");
1254 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1258 if (startswith(key, "IMPORT")) {
1259 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
1261 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
1264 if (streq(attr, "program")) {
1265 /* find known built-in command */
1266 if (value[0] != '/') {
1267 enum udev_builtin_cmd cmd;
1269 cmd = udev_builtin_lookup(value);
1270 if (cmd < UDEV_BUILTIN_MAX) {
1271 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1272 value, filename, lineno);
1273 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1277 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1278 } else if (streq(attr, "builtin")) {
1279 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1281 if (cmd < UDEV_BUILTIN_MAX)
1282 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1284 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1285 } else if (streq(attr, "file")) {
1286 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1287 } else if (streq(attr, "db")) {
1288 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1289 } else if (streq(attr, "cmdline")) {
1290 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1291 } else if (streq(attr, "parent")) {
1292 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1294 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
1298 if (startswith(key, "TEST")) {
1301 if (op > OP_MATCH_MAX) {
1302 log_error("invalid TEST operation");
1305 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
1307 mode = strtol(attr, NULL, 8);
1308 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1310 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1315 if (startswith(key, "RUN")) {
1316 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
1320 if (streq(attr, "builtin")) {
1321 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1323 if (cmd < UDEV_BUILTIN_MAX)
1324 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1326 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1327 } else if (streq(attr, "program")) {
1328 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1330 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1332 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
1338 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1339 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1343 if (streq(key, "LABEL")) {
1344 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1348 if (streq(key, "GOTO")) {
1349 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1353 if (startswith(key, "NAME")) {
1354 if (op < OP_MATCH_MAX) {
1355 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1357 if (streq(value, "%k")) {
1358 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1359 "please remove it from %s:%u\n", filename, lineno);
1362 if (value[0] == '\0') {
1363 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1364 "please remove it from %s:%u\n", filename, lineno);
1367 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1369 rule_tmp.rule.rule.can_set_name = true;
1373 if (streq(key, "SYMLINK")) {
1374 if (op < OP_MATCH_MAX)
1375 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1377 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1378 rule_tmp.rule.rule.can_set_name = true;
1382 if (streq(key, "OWNER")) {
1386 uid = strtoul(value, &endptr, 10);
1387 if (endptr[0] == '\0') {
1388 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1389 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1390 uid = add_uid(rules, value);
1391 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1392 } else if (rules->resolve_names >= 0) {
1393 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1395 rule_tmp.rule.rule.can_set_name = true;
1399 if (streq(key, "GROUP")) {
1403 gid = strtoul(value, &endptr, 10);
1404 if (endptr[0] == '\0') {
1405 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1406 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1407 gid = add_gid(rules, value);
1408 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1409 } else if (rules->resolve_names >= 0) {
1410 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1412 rule_tmp.rule.rule.can_set_name = true;
1416 if (streq(key, "MODE")) {
1420 mode = strtol(value, &endptr, 8);
1421 if (endptr[0] == '\0')
1422 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1424 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1425 rule_tmp.rule.rule.can_set_name = true;
1429 if (streq(key, "OPTIONS")) {
1432 pos = strstr(value, "link_priority=");
1434 int prio = atoi(&pos[strlen("link_priority=")]);
1436 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1439 pos = strstr(value, "string_escape=");
1441 pos = &pos[strlen("string_escape=")];
1442 if (startswith(pos, "none"))
1443 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1444 else if (startswith(pos, "replace"))
1445 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1448 pos = strstr(value, "db_persist");
1450 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1452 pos = strstr(value, "nowatch");
1456 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1458 pos = strstr(value, "watch");
1462 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1466 pos = strstr(value, "static_node=");
1468 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1469 rule_tmp.rule.rule.has_static_node = true;
1475 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
1479 /* add rule token */
1480 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1481 if (add_token(rules, &rule_tmp.rule) != 0)
1484 /* add tokens to list, sorted by type */
1485 if (sort_token(rules, &rule_tmp) != 0)
1490 log_error("invalid rule '%s:%u'", filename, lineno);
1494 static int parse_file(struct udev_rules *rules, const char *filename) {
1496 unsigned int first_token;
1497 unsigned int filename_off;
1498 char line[UTIL_LINE_SIZE];
1502 f = fopen(filename, "re");
1504 if (errno == ENOENT)
1510 if (null_or_empty_fd(fileno(f))) {
1511 log_debug("Skipping empty file: %s", filename);
1514 log_debug("Reading rules file: %s", filename);
1516 first_token = rules->token_cur;
1517 filename_off = rules_add_string(rules, filename);
1519 while (fgets(line, sizeof(line), f) != NULL) {
1523 /* skip whitespace */
1526 while (isspace(key[0]))
1537 /* continue reading if backslash+newline is found */
1538 while (line[len-2] == '\\') {
1539 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1541 if (strlen(&line[len-2]) < 2)
1547 if (len+1 >= sizeof(line)) {
1548 log_error("line too long '%s':%u, ignored", filename, line_nr);
1551 add_rule(rules, key, filename, filename_off, line_nr);
1555 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1556 for (i = first_token+1; i < rules->token_cur; i++) {
1557 if (rules->tokens[i].type == TK_A_GOTO) {
1558 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1561 for (j = i+1; j < rules->token_cur; j++) {
1562 if (rules->tokens[j].type != TK_RULE)
1564 if (rules->tokens[j].rule.label_off == 0)
1566 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1568 rules->tokens[i].key.rule_goto = j;
1571 if (rules->tokens[i].key.rule_goto == 0)
1572 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
1578 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) {
1579 struct udev_rules *rules;
1580 struct udev_list file_list;
1581 struct token end_token;
1585 rules = new0(struct udev_rules, 1);
1589 rules->resolve_names = resolve_names;
1590 udev_list_init(udev, &file_list, true);
1592 /* init token array and string buffer */
1593 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1594 if (rules->tokens == NULL)
1595 return udev_rules_unref(rules);
1596 rules->token_max = PREALLOC_TOKEN;
1598 rules->strbuf = strbuf_new();
1600 return udev_rules_unref(rules);
1602 udev_rules_check_timestamp(rules);
1604 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
1606 log_error("failed to enumerate rules files: %s", strerror(-r));
1607 return udev_rules_unref(rules);
1611 * The offset value in the rules strct is limited; add all
1612 * rules file names to the beginning of the string buffer.
1614 STRV_FOREACH(f, files)
1615 rules_add_string(rules, *f);
1617 STRV_FOREACH(f, files)
1618 parse_file(rules, *f);
1622 memzero(&end_token, sizeof(struct token));
1623 end_token.type = TK_END;
1624 add_token(rules, &end_token);
1625 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1626 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1628 /* cleanup temporary strbuf data */
1629 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1630 rules->strbuf->in_count, rules->strbuf->in_len,
1631 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1632 strbuf_complete(rules->strbuf);
1634 /* cleanup uid/gid cache */
1637 rules->uids_cur = 0;
1638 rules->uids_max = 0;
1641 rules->gids_cur = 0;
1642 rules->gids_max = 0;
1648 struct udev_rules *udev_rules_unref(struct udev_rules *rules) {
1651 free(rules->tokens);
1652 strbuf_cleanup(rules->strbuf);
1659 bool udev_rules_check_timestamp(struct udev_rules *rules) {
1663 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
1666 static int match_key(struct udev_rules *rules, struct token *token, const char *val) {
1667 char *key_value = rules_str(rules, token->key.value_off);
1674 switch (token->key.glob) {
1676 match = (streq(key_value, val));
1679 match = (fnmatch(key_value, val, 0) == 0);
1686 s = rules_str(rules, token->key.value_off);
1691 next = strchr(s, '|');
1693 size_t matchlen = (size_t)(next - s);
1695 match = (matchlen == len && strneq(s, val, matchlen));
1699 match = (streq(s, val));
1708 char value[UTIL_PATH_SIZE];
1710 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1712 while (key_value != NULL) {
1713 pos = strchr(key_value, '|');
1718 match = (fnmatch(key_value, val, 0) == 0);
1726 match = (val[0] != '\0');
1732 if (match && (token->key.op == OP_MATCH))
1734 if (!match && (token->key.op == OP_NOMATCH))
1739 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) {
1741 char nbuf[UTIL_NAME_SIZE];
1743 char vbuf[UTIL_NAME_SIZE];
1746 name = rules_str(rules, cur->key.attr_off);
1747 switch (cur->key.attrsubst) {
1749 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1753 value = udev_device_get_sysattr_value(dev, name);
1758 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1766 /* remove trailing whitespace, if not asked to match for it */
1767 len = strlen(value);
1768 if (len > 0 && isspace(value[len-1])) {
1769 const char *key_value;
1772 key_value = rules_str(rules, cur->key.value_off);
1773 klen = strlen(key_value);
1774 if (klen > 0 && !isspace(key_value[klen-1])) {
1775 if (value != vbuf) {
1776 strscpy(vbuf, sizeof(vbuf), value);
1779 while (len > 0 && isspace(vbuf[--len]))
1784 return match_key(rules, cur, value);
1793 int udev_rules_apply_to_event(struct udev_rules *rules,
1794 struct udev_event *event,
1795 usec_t timeout_usec,
1796 const sigset_t *sigmask) {
1799 enum escape_type esc = ESCAPE_UNSET;
1802 if (rules->tokens == NULL)
1805 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1806 (major(udev_device_get_devnum(event->dev)) > 0 ||
1807 udev_device_get_ifindex(event->dev) > 0));
1809 /* loop through token list, match, run actions or forward to next rule */
1810 cur = &rules->tokens[0];
1813 dump_token(rules, cur);
1814 switch (cur->type) {
1818 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1819 if (!can_set_name && rule->rule.can_set_name)
1824 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1828 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1832 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1835 case TK_M_DEVLINK: {
1836 struct udev_list_entry *list_entry;
1839 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1840 const char *devlink;
1842 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1843 if (match_key(rules, cur, devlink) == 0) {
1853 if (match_key(rules, cur, event->name) != 0)
1857 const char *key_name = rules_str(rules, cur->key.attr_off);
1860 value = udev_device_get_property_value(event->dev, key_name);
1863 if (match_key(rules, cur, value))
1868 struct udev_list_entry *list_entry;
1871 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1872 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1877 if (!match && (cur->key.op != OP_NOMATCH))
1881 case TK_M_SUBSYSTEM:
1882 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1886 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1889 case TK_M_WAITFOR: {
1890 char filename[UTIL_PATH_SIZE];
1893 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1894 found = (wait_for_file(event->dev, filename, 10) == 0);
1895 if (!found && (cur->key.op != OP_NOMATCH))
1900 if (match_attr(rules, event->dev, event, cur) != 0)
1904 case TK_M_SUBSYSTEMS:
1910 /* get whole sequence of parent matches */
1912 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1915 /* loop over parents */
1916 event->dev_parent = event->dev;
1920 /* loop over sequence of parent match keys */
1921 for (key = cur; key < next; key++ ) {
1922 dump_token(rules, key);
1925 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1928 case TK_M_SUBSYSTEMS:
1929 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1933 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1937 if (match_attr(rules, event->dev_parent, event, key) != 0)
1941 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
1943 if (match && key->key.op == OP_NOMATCH)
1945 if (!match && key->key.op == OP_MATCH)
1956 event->dev_parent = udev_device_get_parent(event->dev_parent);
1957 if (event->dev_parent == NULL)
1960 /* move behind our sequence of parent match keys */
1965 char filename[UTIL_PATH_SIZE];
1966 struct stat statbuf;
1969 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1970 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
1971 if (filename[0] != '/') {
1972 char tmp[UTIL_PATH_SIZE];
1974 strscpy(tmp, sizeof(tmp), filename);
1975 strscpyl(filename, sizeof(filename),
1976 udev_device_get_syspath(event->dev), "/", tmp, NULL);
1979 attr_subst_subdir(filename, sizeof(filename));
1981 match = (stat(filename, &statbuf) == 0);
1982 if (match && cur->key.mode > 0)
1983 match = ((statbuf.st_mode & cur->key.mode) > 0);
1984 if (match && cur->key.op == OP_NOMATCH)
1986 if (!match && cur->key.op == OP_MATCH)
1990 case TK_M_PROGRAM: {
1991 char program[UTIL_PATH_SIZE];
1993 char result[UTIL_LINE_SIZE];
1995 free(event->program_result);
1996 event->program_result = NULL;
1997 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
1998 envp = udev_device_get_properties_envp(event->dev);
1999 log_debug("PROGRAM '%s' %s:%u",
2001 rules_str(rules, rule->rule.filename_off),
2002 rule->rule.filename_line);
2004 if (udev_event_spawn(event, timeout_usec, program, envp, sigmask, result, sizeof(result)) < 0) {
2005 if (cur->key.op != OP_NOMATCH)
2010 util_remove_trailing_chars(result, '\n');
2011 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2012 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2014 log_debug("%i character(s) replaced" , count);
2016 event->program_result = strdup(result);
2017 if (cur->key.op == OP_NOMATCH)
2022 case TK_M_IMPORT_FILE: {
2023 char import[UTIL_PATH_SIZE];
2025 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2026 if (import_file_into_properties(event->dev, import) != 0)
2027 if (cur->key.op != OP_NOMATCH)
2031 case TK_M_IMPORT_PROG: {
2032 char import[UTIL_PATH_SIZE];
2034 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2035 log_debug("IMPORT '%s' %s:%u",
2037 rules_str(rules, rule->rule.filename_off),
2038 rule->rule.filename_line);
2040 if (import_program_into_properties(event, timeout_usec, import, sigmask) != 0)
2041 if (cur->key.op != OP_NOMATCH)
2045 case TK_M_IMPORT_BUILTIN: {
2046 char command[UTIL_PATH_SIZE];
2048 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2049 /* check if we ran already */
2050 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2051 log_debug("IMPORT builtin skip '%s' %s:%u",
2052 udev_builtin_name(cur->key.builtin_cmd),
2053 rules_str(rules, rule->rule.filename_off),
2054 rule->rule.filename_line);
2055 /* return the result from earlier run */
2056 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2057 if (cur->key.op != OP_NOMATCH)
2062 event->builtin_run |= (1 << cur->key.builtin_cmd);
2065 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2066 log_debug("IMPORT builtin '%s' %s:%u",
2067 udev_builtin_name(cur->key.builtin_cmd),
2068 rules_str(rules, rule->rule.filename_off),
2069 rule->rule.filename_line);
2071 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2072 /* remember failure */
2073 log_debug("IMPORT builtin '%s' returned non-zero",
2074 udev_builtin_name(cur->key.builtin_cmd));
2075 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2076 if (cur->key.op != OP_NOMATCH)
2081 case TK_M_IMPORT_DB: {
2082 const char *key = rules_str(rules, cur->key.value_off);
2085 value = udev_device_get_property_value(event->dev_db, key);
2086 if (value != NULL) {
2087 struct udev_list_entry *entry;
2089 entry = udev_device_add_property(event->dev, key, value);
2090 udev_list_entry_set_num(entry, true);
2092 if (cur->key.op != OP_NOMATCH)
2097 case TK_M_IMPORT_CMDLINE: {
2099 bool imported = false;
2101 f = fopen("/proc/cmdline", "re");
2105 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2106 const char *key = rules_str(rules, cur->key.value_off);
2109 pos = strstr(cmdline, key);
2111 struct udev_list_entry *entry;
2114 if (pos[0] == '\0' || isspace(pos[0])) {
2115 /* we import simple flags as 'FLAG=1' */
2116 entry = udev_device_add_property(event->dev, key, "1");
2117 udev_list_entry_set_num(entry, true);
2119 } else if (pos[0] == '=') {
2124 while (pos[0] != '\0' && !isspace(pos[0]))
2127 entry = udev_device_add_property(event->dev, key, value);
2128 udev_list_entry_set_num(entry, true);
2135 if (!imported && cur->key.op != OP_NOMATCH)
2139 case TK_M_IMPORT_PARENT: {
2140 char import[UTIL_PATH_SIZE];
2142 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2143 if (import_parent_into_properties(event->dev, import) != 0)
2144 if (cur->key.op != OP_NOMATCH)
2149 if (match_key(rules, cur, event->program_result) != 0)
2152 case TK_A_STRING_ESCAPE_NONE:
2155 case TK_A_STRING_ESCAPE_REPLACE:
2156 esc = ESCAPE_REPLACE;
2158 case TK_A_DB_PERSIST:
2159 udev_device_set_db_persist(event->dev);
2161 case TK_A_INOTIFY_WATCH:
2162 if (event->inotify_watch_final)
2164 if (cur->key.op == OP_ASSIGN_FINAL)
2165 event->inotify_watch_final = true;
2166 event->inotify_watch = cur->key.watch;
2168 case TK_A_DEVLINK_PRIO:
2169 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2172 char owner[UTIL_NAME_SIZE];
2174 if (event->owner_final)
2176 if (cur->key.op == OP_ASSIGN_FINAL)
2177 event->owner_final = true;
2178 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2179 event->owner_set = true;
2180 event->uid = util_lookup_user(event->udev, owner);
2181 log_debug("OWNER %u %s:%u",
2183 rules_str(rules, rule->rule.filename_off),
2184 rule->rule.filename_line);
2188 char group[UTIL_NAME_SIZE];
2190 if (event->group_final)
2192 if (cur->key.op == OP_ASSIGN_FINAL)
2193 event->group_final = true;
2194 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2195 event->group_set = true;
2196 event->gid = util_lookup_group(event->udev, group);
2197 log_debug("GROUP %u %s:%u",
2199 rules_str(rules, rule->rule.filename_off),
2200 rule->rule.filename_line);
2204 char mode_str[UTIL_NAME_SIZE];
2208 if (event->mode_final)
2210 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2211 mode = strtol(mode_str, &endptr, 8);
2212 if (endptr[0] != '\0') {
2213 log_error("ignoring invalid mode '%s'", mode_str);
2216 if (cur->key.op == OP_ASSIGN_FINAL)
2217 event->mode_final = true;
2218 event->mode_set = true;
2220 log_debug("MODE %#o %s:%u",
2222 rules_str(rules, rule->rule.filename_off),
2223 rule->rule.filename_line);
2227 if (event->owner_final)
2229 if (cur->key.op == OP_ASSIGN_FINAL)
2230 event->owner_final = true;
2231 event->owner_set = true;
2232 event->uid = cur->key.uid;
2233 log_debug("OWNER %u %s:%u",
2235 rules_str(rules, rule->rule.filename_off),
2236 rule->rule.filename_line);
2239 if (event->group_final)
2241 if (cur->key.op == OP_ASSIGN_FINAL)
2242 event->group_final = true;
2243 event->group_set = true;
2244 event->gid = cur->key.gid;
2245 log_debug("GROUP %u %s:%u",
2247 rules_str(rules, rule->rule.filename_off),
2248 rule->rule.filename_line);
2251 if (event->mode_final)
2253 if (cur->key.op == OP_ASSIGN_FINAL)
2254 event->mode_final = true;
2255 event->mode_set = true;
2256 event->mode = cur->key.mode;
2257 log_debug("MODE %#o %s:%u",
2259 rules_str(rules, rule->rule.filename_off),
2260 rule->rule.filename_line);
2262 case TK_A_SECLABEL: {
2263 const char *name, *label;
2265 name = rules_str(rules, cur->key.attr_off);
2266 label = rules_str(rules, cur->key.value_off);
2267 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2268 udev_list_cleanup(&event->seclabel_list);
2269 udev_list_entry_add(&event->seclabel_list, name, label);
2270 log_debug("SECLABEL{%s}='%s' %s:%u",
2272 rules_str(rules, rule->rule.filename_off),
2273 rule->rule.filename_line);
2277 const char *name = rules_str(rules, cur->key.attr_off);
2278 char *value = rules_str(rules, cur->key.value_off);
2279 char value_new[UTIL_NAME_SIZE];
2280 const char *value_old = NULL;
2281 struct udev_list_entry *entry;
2283 if (value[0] == '\0') {
2284 if (cur->key.op == OP_ADD)
2286 udev_device_add_property(event->dev, name, NULL);
2290 if (cur->key.op == OP_ADD)
2291 value_old = udev_device_get_property_value(event->dev, name);
2293 char temp[UTIL_NAME_SIZE];
2295 /* append value separated by space */
2296 udev_event_apply_format(event, value, temp, sizeof(temp));
2297 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2299 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2301 entry = udev_device_add_property(event->dev, name, value_new);
2302 /* store in db, skip private keys */
2304 udev_list_entry_set_num(entry, true);
2308 char tag[UTIL_PATH_SIZE];
2311 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2312 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2313 udev_device_cleanup_tags_list(event->dev);
2314 for (p = tag; *p != '\0'; p++) {
2315 if ((*p >= 'a' && *p <= 'z') ||
2316 (*p >= 'A' && *p <= 'Z') ||
2317 (*p >= '0' && *p <= '9') ||
2318 *p == '-' || *p == '_')
2320 log_error("ignoring invalid tag name '%s'", tag);
2323 udev_device_add_tag(event->dev, tag);
2327 const char *name = rules_str(rules, cur->key.value_off);
2329 char name_str[UTIL_PATH_SIZE];
2332 if (event->name_final)
2334 if (cur->key.op == OP_ASSIGN_FINAL)
2335 event->name_final = true;
2336 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2337 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2338 count = util_replace_chars(name_str, "/");
2340 log_debug("%i character(s) replaced", count);
2342 if (major(udev_device_get_devnum(event->dev)) &&
2343 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2344 log_error("NAME=\"%s\" ignored, kernel device nodes "
2345 "can not be renamed; please fix it in %s:%u\n", name,
2346 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2350 event->name = strdup(name_str);
2351 log_debug("NAME '%s' %s:%u",
2353 rules_str(rules, rule->rule.filename_off),
2354 rule->rule.filename_line);
2357 case TK_A_DEVLINK: {
2358 char temp[UTIL_PATH_SIZE];
2359 char filename[UTIL_PATH_SIZE];
2363 if (event->devlink_final)
2365 if (major(udev_device_get_devnum(event->dev)) == 0)
2367 if (cur->key.op == OP_ASSIGN_FINAL)
2368 event->devlink_final = true;
2369 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2370 udev_device_cleanup_devlinks_list(event->dev);
2372 /* allow multiple symlinks separated by spaces */
2373 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2374 if (esc == ESCAPE_UNSET)
2375 count = util_replace_chars(temp, "/ ");
2376 else if (esc == ESCAPE_REPLACE)
2377 count = util_replace_chars(temp, "/");
2379 log_debug("%i character(s) replaced" , count);
2381 while (isspace(pos[0]))
2383 next = strchr(pos, ' ');
2384 while (next != NULL) {
2386 log_debug("LINK '%s' %s:%u", pos,
2387 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2388 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2389 udev_device_add_devlink(event->dev, filename);
2390 while (isspace(next[1]))
2393 next = strchr(pos, ' ');
2395 if (pos[0] != '\0') {
2396 log_debug("LINK '%s' %s:%u", pos,
2397 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2398 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2399 udev_device_add_devlink(event->dev, filename);
2404 const char *key_name = rules_str(rules, cur->key.attr_off);
2405 char attr[UTIL_PATH_SIZE];
2406 char value[UTIL_NAME_SIZE];
2409 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2410 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2411 attr_subst_subdir(attr, sizeof(attr));
2413 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2414 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
2415 rules_str(rules, rule->rule.filename_off),
2416 rule->rule.filename_line);
2417 f = fopen(attr, "we");
2419 if (fprintf(f, "%s", value) <= 0)
2420 log_error("error writing ATTR{%s}: %m", attr);
2423 log_error("error opening ATTR{%s} for writing: %m", attr);
2427 case TK_A_RUN_BUILTIN:
2428 case TK_A_RUN_PROGRAM: {
2429 struct udev_list_entry *entry;
2431 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2432 udev_list_cleanup(&event->run_list);
2433 log_debug("RUN '%s' %s:%u",
2434 rules_str(rules, cur->key.value_off),
2435 rules_str(rules, rule->rule.filename_off),
2436 rule->rule.filename_line);
2437 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2438 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2442 if (cur->key.rule_goto == 0)
2444 cur = &rules->tokens[cur->key.rule_goto];
2449 case TK_M_PARENTS_MIN:
2450 case TK_M_PARENTS_MAX:
2453 log_error("wrong type %u", cur->type);
2460 /* fast-forward to next rule */
2461 cur = rule + rule->rule.token_count;
2465 int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
2471 _cleanup_strv_free_ char **tags = NULL;
2474 _cleanup_free_ char *path = NULL;
2477 if (rules->tokens == NULL)
2480 cur = &rules->tokens[0];
2483 switch (cur->type) {
2488 /* skip rules without a static_node tag */
2489 if (!rule->rule.has_static_node)
2505 mode = cur->key.mode;
2508 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2513 case TK_A_STATIC_NODE: {
2514 char device_node[UTIL_PATH_SIZE];
2515 char tags_dir[UTIL_PATH_SIZE];
2516 char tag_symlink[UTIL_PATH_SIZE];
2519 /* we assure, that the permissions tokens are sorted before the static token */
2521 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2524 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2525 if (stat(device_node, &stats) != 0)
2527 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2530 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2532 STRV_FOREACH(t, tags) {
2533 _cleanup_free_ char *unescaped_filename = NULL;
2535 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2536 r = mkdir_p(tags_dir, 0755);
2538 log_error("failed to create %s: %s", tags_dir, strerror(-r));
2542 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2544 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2545 r = symlink(device_node, tag_symlink);
2546 if (r < 0 && errno != EEXIST) {
2547 log_error("failed to create symlink %s -> %s: %m", tag_symlink, device_node);
2554 /* don't touch the permissions if only the tags were set */
2555 if (mode == 0 && uid == 0 && gid == 0)
2564 if (mode != (stats.st_mode & 01777)) {
2565 r = chmod(device_node, mode);
2567 log_error("failed to chmod '%s' %#o", device_node, mode);
2570 log_debug("chmod '%s' %#o", device_node, mode);
2573 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2574 r = chown(device_node, uid, gid);
2576 log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
2579 log_debug("chown '%s' %u %u", device_node, uid, gid);
2582 utimensat(AT_FDCWD, device_node, NULL, 0);
2592 /* fast-forward to next rule */
2593 cur = rule + rule->rule.token_count;
2600 fchmod(fileno(f), 0644);
2601 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2603 unlink("/run/udev/static_node-tags");