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)
221 static const char *operation_strs[] = {
222 [OP_UNSET] = "UNSET",
223 [OP_MATCH] = "match",
224 [OP_NOMATCH] = "nomatch",
225 [OP_MATCH_MAX] = "MATCH_MAX",
228 [OP_ASSIGN] = "assign",
229 [OP_ASSIGN_FINAL] = "assign-final",
232 return operation_strs[type];
235 static const char *string_glob_str(enum string_glob_type type)
237 static const char *string_glob_strs[] = {
238 [GL_UNSET] = "UNSET",
239 [GL_PLAIN] = "plain",
241 [GL_SPLIT] = "split",
242 [GL_SPLIT_GLOB] = "split-glob",
243 [GL_SOMETHING] = "split-glob",
246 return string_glob_strs[type];
249 static const char *token_str(enum token_type type)
251 static const char *token_strs[] = {
252 [TK_UNSET] = "UNSET",
255 [TK_M_ACTION] = "M ACTION",
256 [TK_M_DEVPATH] = "M DEVPATH",
257 [TK_M_KERNEL] = "M KERNEL",
258 [TK_M_DEVLINK] = "M DEVLINK",
259 [TK_M_NAME] = "M NAME",
260 [TK_M_ENV] = "M ENV",
261 [TK_M_TAG] = "M TAG",
262 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
263 [TK_M_DRIVER] = "M DRIVER",
264 [TK_M_WAITFOR] = "M WAITFOR",
265 [TK_M_ATTR] = "M ATTR",
267 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
268 [TK_M_KERNELS] = "M KERNELS",
269 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
270 [TK_M_DRIVERS] = "M DRIVERS",
271 [TK_M_ATTRS] = "M ATTRS",
272 [TK_M_TAGS] = "M TAGS",
273 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
275 [TK_M_TEST] = "M TEST",
276 [TK_M_PROGRAM] = "M PROGRAM",
277 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
278 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
279 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
280 [TK_M_IMPORT_DB] = "M IMPORT_DB",
281 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
282 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
283 [TK_M_RESULT] = "M RESULT",
284 [TK_M_MAX] = "M MAX",
286 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
287 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
288 [TK_A_DB_PERSIST] = "A DB_PERSIST",
289 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
290 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
291 [TK_A_OWNER] = "A OWNER",
292 [TK_A_GROUP] = "A GROUP",
293 [TK_A_MODE] = "A MODE",
294 [TK_A_OWNER_ID] = "A OWNER_ID",
295 [TK_A_GROUP_ID] = "A GROUP_ID",
296 [TK_A_STATIC_NODE] = "A STATIC_NODE",
297 [TK_A_SECLABEL] = "A SECLABEL",
298 [TK_A_MODE_ID] = "A MODE_ID",
299 [TK_A_ENV] = "A ENV",
300 [TK_A_TAG] = "A ENV",
301 [TK_A_NAME] = "A NAME",
302 [TK_A_DEVLINK] = "A DEVLINK",
303 [TK_A_ATTR] = "A ATTR",
304 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
305 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
306 [TK_A_GOTO] = "A GOTO",
311 return token_strs[type];
314 static void dump_token(struct udev_rules *rules, struct token *token)
316 enum token_type type = token->type;
317 enum operation_type op = token->key.op;
318 enum string_glob_type glob = token->key.glob;
319 const char *value = str(rules, token->key.value_off);
320 const char *attr = &rules->buf[token->key.attr_off];
325 const char *tks_ptr = (char *)rules->tokens;
326 const char *tk_ptr = (char *)token;
327 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
329 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
330 &rules->buf[token->rule.filename_off], token->rule.filename_line,
331 idx, token->rule.token_count,
332 &rules->buf[token->rule.label_off]);
344 case TK_M_SUBSYSTEMS:
348 case TK_M_IMPORT_FILE:
349 case TK_M_IMPORT_PROG:
351 case TK_M_IMPORT_CMDLINE:
352 case TK_M_IMPORT_PARENT:
359 case TK_A_RUN_BUILTIN:
360 case TK_A_RUN_PROGRAM:
361 log_debug("%s %s '%s'(%s)",
362 token_str(type), operation_str(op), value, string_glob_str(glob));
364 case TK_M_IMPORT_BUILTIN:
365 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
372 log_debug("%s %s '%s' '%s'(%s)",
373 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
377 log_debug("%s %s '%s'", token_str(type), operation_str(op), value);
379 case TK_A_STRING_ESCAPE_NONE:
380 case TK_A_STRING_ESCAPE_REPLACE:
381 case TK_A_DB_PERSIST:
382 log_debug("%s", token_str(type));
385 log_debug("%s %s '%s'(%s) %#o",
386 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
388 case TK_A_INOTIFY_WATCH:
389 log_debug("%s %u", token_str(type), token->key.watch);
391 case TK_A_DEVLINK_PRIO:
392 log_debug("%s %u", token_str(type), token->key.devlink_prio);
395 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid);
398 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid);
401 log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode);
403 case TK_A_STATIC_NODE:
404 log_debug("%s '%s'", token_str(type), value);
407 log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value);
410 log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto);
413 log_debug("* %s", token_str(type));
415 case TK_M_PARENTS_MIN:
416 case TK_M_PARENTS_MAX:
419 log_debug("unknown type %u", type);
424 static void dump_rules(struct udev_rules *rules)
428 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
430 rules->token_cur * sizeof(struct token),
433 for (i = 0; i < rules->token_cur; i++)
434 dump_token(rules, &rules->tokens[i]);
437 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
438 static inline void dump_rules(struct udev_rules *rules) {}
441 static int add_token(struct udev_rules *rules, struct token *token)
443 /* grow buffer if needed */
444 if (rules->token_cur+1 >= rules->token_max) {
445 struct token *tokens;
448 /* double the buffer size */
449 add = rules->token_max;
453 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
456 rules->tokens = tokens;
457 rules->token_max += add;
459 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
464 static uid_t add_uid(struct udev_rules *rules, const char *owner)
470 /* lookup, if we know it already */
471 for (i = 0; i < rules->uids_cur; i++) {
472 off = rules->uids[i].name_off;
473 if (streq(rules_str(rules, off), owner)) {
474 uid = rules->uids[i].uid;
478 uid = util_lookup_user(rules->udev, owner);
480 /* grow buffer if needed */
481 if (rules->uids_cur+1 >= rules->uids_max) {
482 struct uid_gid *uids;
485 /* double the buffer size */
486 add = rules->uids_max;
490 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
494 rules->uids_max += add;
496 rules->uids[rules->uids_cur].uid = uid;
497 off = rules_add_string(rules, owner);
500 rules->uids[rules->uids_cur].name_off = off;
505 static gid_t add_gid(struct udev_rules *rules, const char *group)
511 /* lookup, if we know it already */
512 for (i = 0; i < rules->gids_cur; i++) {
513 off = rules->gids[i].name_off;
514 if (streq(rules_str(rules, off), group)) {
515 gid = rules->gids[i].gid;
519 gid = util_lookup_group(rules->udev, group);
521 /* grow buffer if needed */
522 if (rules->gids_cur+1 >= rules->gids_max) {
523 struct uid_gid *gids;
526 /* double the buffer size */
527 add = rules->gids_max;
531 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
535 rules->gids_max += add;
537 rules->gids[rules->gids_cur].gid = gid;
538 off = rules_add_string(rules, group);
541 rules->gids[rules->gids_cur].name_off = off;
546 static int import_property_from_string(struct udev_device *dev, char *line)
551 struct udev_list_entry *entry;
555 while (isspace(key[0]))
558 /* comment or empty line */
559 if (key[0] == '#' || key[0] == '\0')
562 /* split key/value */
563 val = strchr(key, '=');
570 while (isspace(val[0]))
577 while (isspace(key[len-1]))
581 /* terminate value */
585 while (isspace(val[len-1]))
593 if (val[0] == '"' || val[0] == '\'') {
594 if (val[len-1] != val[0]) {
595 log_debug("inconsistent quoting: '%s', skip", line);
602 entry = udev_device_add_property(dev, key, val);
603 /* store in db, skip private keys */
605 udev_list_entry_set_num(entry, true);
610 static int import_file_into_properties(struct udev_device *dev, const char *filename)
613 char line[UTIL_LINE_SIZE];
615 f = fopen(filename, "re");
618 while (fgets(line, sizeof(line), f) != NULL)
619 import_property_from_string(dev, line);
624 static int import_program_into_properties(struct udev_event *event,
626 const char *program, const sigset_t *sigmask) {
627 struct udev_device *dev = event->dev;
629 char result[UTIL_LINE_SIZE];
633 envp = udev_device_get_properties_envp(dev);
634 err = udev_event_spawn(event, timeout_usec, program, envp, sigmask, result, sizeof(result));
639 while (line != NULL) {
642 pos = strchr(line, '\n');
647 import_property_from_string(dev, line);
653 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
655 struct udev_device *dev_parent;
656 struct udev_list_entry *list_entry;
658 dev_parent = udev_device_get_parent(dev);
659 if (dev_parent == NULL)
662 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
663 const char *key = udev_list_entry_get_name(list_entry);
664 const char *val = udev_list_entry_get_value(list_entry);
666 if (fnmatch(filter, key, 0) == 0) {
667 struct udev_list_entry *entry;
669 entry = udev_device_add_property(dev, key, val);
670 /* store in db, skip private keys */
672 udev_list_entry_set_num(entry, true);
678 #define WAIT_LOOP_PER_SECOND 50
679 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
681 char filepath[UTIL_PATH_SIZE];
682 char devicepath[UTIL_PATH_SIZE];
684 int loop = timeout * WAIT_LOOP_PER_SECOND;
686 /* a relative path is a device attribute */
687 devicepath[0] = '\0';
688 if (file[0] != '/') {
689 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
690 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
695 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
698 if (stat(file, &stats) == 0) {
699 log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
702 /* make sure, the device did not disappear in the meantime */
703 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
704 log_debug("device disappeared while waiting for '%s'", file);
707 log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
708 nanosleep(&duration, NULL);
710 log_debug("waiting for '%s' failed", file);
714 static int attr_subst_subdir(char *attr, size_t len)
718 if (strstr(attr, "/*/")) {
720 char dirname[UTIL_PATH_SIZE];
724 strscpy(dirname, sizeof(dirname), attr);
725 pos = strstr(dirname, "/*/");
730 dir = opendir(dirname);
734 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
737 if (dent->d_name[0] == '.')
739 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
740 if (stat(attr, &stats) == 0) {
752 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
758 if (linepos == NULL || linepos[0] == '\0')
761 /* skip whitespace */
762 while (isspace(linepos[0]) || linepos[0] == ',')
766 if (linepos[0] == '\0')
772 if (linepos[0] == '\0')
774 if (isspace(linepos[0]))
776 if (linepos[0] == '=')
778 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
779 if (linepos[1] == '=')
783 /* remember end of key */
786 /* skip whitespace after key */
787 while (isspace(linepos[0]))
789 if (linepos[0] == '\0')
792 /* get operation type */
793 if (linepos[0] == '=' && linepos[1] == '=') {
796 } else if (linepos[0] == '!' && linepos[1] == '=') {
799 } else if (linepos[0] == '+' && linepos[1] == '=') {
802 } else if (linepos[0] == '=') {
805 } else if (linepos[0] == ':' && linepos[1] == '=') {
806 *op = OP_ASSIGN_FINAL;
814 /* skip whitespace after operator */
815 while (isspace(linepos[0]))
817 if (linepos[0] == '\0')
821 if (linepos[0] == '"')
828 temp = strchr(linepos, '"');
834 /* move line to next key */
839 /* extract possible KEY{attr} */
840 static const char *get_key_attribute(struct udev *udev, char *str)
845 attr = strchr(str, '{');
848 pos = strchr(attr, '}');
850 log_error("missing closing brace for format");
859 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
860 enum operation_type op,
861 const char *value, const void *data)
863 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
864 const char *attr = NULL;
866 memzero(token, sizeof(struct token));
878 case TK_M_SUBSYSTEMS:
882 case TK_M_IMPORT_FILE:
883 case TK_M_IMPORT_PROG:
885 case TK_M_IMPORT_CMDLINE:
886 case TK_M_IMPORT_PARENT:
896 case TK_A_STATIC_NODE:
897 token->key.value_off = rules_add_string(rule_tmp->rules, value);
899 case TK_M_IMPORT_BUILTIN:
900 token->key.value_off = rules_add_string(rule_tmp->rules, value);
901 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
910 token->key.value_off = rules_add_string(rule_tmp->rules, value);
911 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
914 token->key.value_off = rules_add_string(rule_tmp->rules, value);
916 token->key.mode = *(mode_t *)data;
918 case TK_A_STRING_ESCAPE_NONE:
919 case TK_A_STRING_ESCAPE_REPLACE:
920 case TK_A_DB_PERSIST:
922 case TK_A_RUN_BUILTIN:
923 case TK_A_RUN_PROGRAM:
924 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
925 token->key.value_off = rules_add_string(rule_tmp->rules, value);
927 case TK_A_INOTIFY_WATCH:
928 case TK_A_DEVLINK_PRIO:
929 token->key.devlink_prio = *(int *)data;
932 token->key.uid = *(uid_t *)data;
935 token->key.gid = *(gid_t *)data;
938 token->key.mode = *(mode_t *)data;
941 case TK_M_PARENTS_MIN:
942 case TK_M_PARENTS_MAX:
946 log_error("wrong type %u", type);
950 if (value != NULL && type < TK_M_MAX) {
951 /* check if we need to split or call fnmatch() while matching rules */
952 enum string_glob_type glob;
956 has_split = (strchr(value, '|') != NULL);
957 has_glob = string_is_glob(value);
958 if (has_split && has_glob) {
959 glob = GL_SPLIT_GLOB;
960 } else if (has_split) {
962 } else if (has_glob) {
963 if (streq(value, "?*"))
970 token->key.glob = glob;
973 if (value != NULL && type > TK_M_MAX) {
974 /* check if assigned value has substitution chars */
976 token->key.subst = SB_SUBSYS;
977 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
978 token->key.subst = SB_FORMAT;
980 token->key.subst = SB_NONE;
984 /* check if property/attribute name has substitution chars */
986 token->key.attrsubst = SB_SUBSYS;
987 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
988 token->key.attrsubst = SB_FORMAT;
990 token->key.attrsubst = SB_NONE;
993 token->key.type = type;
995 rule_tmp->token_cur++;
996 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
997 log_error("temporary rule array too small");
1003 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1006 unsigned int start = 0;
1007 unsigned int end = rule_tmp->token_cur;
1009 for (i = 0; i < rule_tmp->token_cur; i++) {
1010 enum token_type next_val = TK_UNSET;
1011 unsigned int next_idx = 0;
1014 /* find smallest value */
1015 for (j = start; j < end; j++) {
1016 if (rule_tmp->token[j].type == TK_UNSET)
1018 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1019 next_val = rule_tmp->token[j].type;
1024 /* add token and mark done */
1025 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1027 rule_tmp->token[next_idx].type = TK_UNSET;
1030 if (next_idx == start)
1032 if (next_idx+1 == end)
1038 static int add_rule(struct udev_rules *rules, char *line,
1039 const char *filename, unsigned int filename_off, unsigned int lineno)
1043 struct rule_tmp rule_tmp;
1045 memzero(&rule_tmp, sizeof(struct rule_tmp));
1046 rule_tmp.rules = rules;
1047 rule_tmp.rule.type = TK_RULE;
1048 /* the offset in the rule is limited to unsigned short */
1049 if (filename_off < USHRT_MAX)
1050 rule_tmp.rule.rule.filename_off = filename_off;
1051 rule_tmp.rule.rule.filename_line = lineno;
1057 enum operation_type op;
1059 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1060 /* Avoid erroring on trailing whitespace. This is probably rare
1061 * so save the work for the error case instead of always trying
1062 * to strip the trailing whitespace with strstrip(). */
1063 while (isblank(*linepos))
1066 /* If we aren't at the end of the line, this is a parsing error.
1067 * Make a best effort to describe where the problem is. */
1068 if (*linepos != '\n') {
1069 char buf[2] = {linepos[1]};
1070 _cleanup_free_ char *tmp;
1073 log_error("invalid key/value pair in file %s on line %u,"
1074 "starting at character %tu ('%s')\n",
1075 filename, lineno, linepos - line + 1, tmp);
1076 if (linepos[1] == '#')
1077 log_error("hint: comments can only start at beginning of line");
1082 if (streq(key, "ACTION")) {
1083 if (op > OP_MATCH_MAX) {
1084 log_error("invalid ACTION operation");
1087 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1091 if (streq(key, "DEVPATH")) {
1092 if (op > OP_MATCH_MAX) {
1093 log_error("invalid DEVPATH operation");
1096 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1100 if (streq(key, "KERNEL")) {
1101 if (op > OP_MATCH_MAX) {
1102 log_error("invalid KERNEL operation");
1105 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1109 if (streq(key, "SUBSYSTEM")) {
1110 if (op > OP_MATCH_MAX) {
1111 log_error("invalid SUBSYSTEM operation");
1114 /* bus, class, subsystem events should all be the same */
1115 if (streq(value, "subsystem") ||
1116 streq(value, "bus") ||
1117 streq(value, "class")) {
1118 if (streq(value, "bus") || streq(value, "class"))
1119 log_error("'%s' must be specified as 'subsystem' "
1120 "please fix it in %s:%u", value, filename, lineno);
1121 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1123 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1127 if (streq(key, "DRIVER")) {
1128 if (op > OP_MATCH_MAX) {
1129 log_error("invalid DRIVER operation");
1132 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1136 if (startswith(key, "ATTR{")) {
1137 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
1139 log_error("error parsing ATTR attribute");
1142 if (op < OP_MATCH_MAX) {
1143 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1145 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1150 if (startswith(key, "SECLABEL{")) {
1151 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
1153 log_error("error parsing SECLABEL attribute");
1157 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1161 if (streq(key, "KERNELS")) {
1162 if (op > OP_MATCH_MAX) {
1163 log_error("invalid KERNELS operation");
1166 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1170 if (streq(key, "SUBSYSTEMS")) {
1171 if (op > OP_MATCH_MAX) {
1172 log_error("invalid SUBSYSTEMS operation");
1175 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1179 if (streq(key, "DRIVERS")) {
1180 if (op > OP_MATCH_MAX) {
1181 log_error("invalid DRIVERS operation");
1184 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1188 if (startswith(key, "ATTRS{")) {
1189 if (op > OP_MATCH_MAX) {
1190 log_error("invalid ATTRS operation");
1193 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
1195 log_error("error parsing ATTRS attribute");
1198 if (startswith(attr, "device/"))
1199 log_error("the 'device' link may not be available in a future kernel, "
1200 "please fix it in %s:%u", filename, lineno);
1201 else if (strstr(attr, "../") != NULL)
1202 log_error("do not reference parent sysfs directories directly, "
1203 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1204 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1208 if (streq(key, "TAGS")) {
1209 if (op > OP_MATCH_MAX) {
1210 log_error("invalid TAGS operation");
1213 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1217 if (startswith(key, "ENV{")) {
1218 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
1220 log_error("error parsing ENV attribute");
1223 if (op < OP_MATCH_MAX) {
1224 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1227 static const char *blacklist[] = {
1242 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1243 if (!streq(attr, blacklist[i]))
1245 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
1248 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1254 if (streq(key, "TAG")) {
1255 if (op < OP_MATCH_MAX)
1256 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1258 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1262 if (streq(key, "PROGRAM")) {
1263 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1267 if (streq(key, "RESULT")) {
1268 if (op > OP_MATCH_MAX) {
1269 log_error("invalid RESULT operation");
1272 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1276 if (startswith(key, "IMPORT")) {
1277 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
1279 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
1282 if (streq(attr, "program")) {
1283 /* find known built-in command */
1284 if (value[0] != '/') {
1285 enum udev_builtin_cmd cmd;
1287 cmd = udev_builtin_lookup(value);
1288 if (cmd < UDEV_BUILTIN_MAX) {
1289 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1290 value, filename, lineno);
1291 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1295 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1296 } else if (streq(attr, "builtin")) {
1297 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1299 if (cmd < UDEV_BUILTIN_MAX)
1300 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1302 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1303 } else if (streq(attr, "file")) {
1304 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1305 } else if (streq(attr, "db")) {
1306 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1307 } else if (streq(attr, "cmdline")) {
1308 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1309 } else if (streq(attr, "parent")) {
1310 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1312 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
1316 if (startswith(key, "TEST")) {
1319 if (op > OP_MATCH_MAX) {
1320 log_error("invalid TEST operation");
1323 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
1325 mode = strtol(attr, NULL, 8);
1326 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1328 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1333 if (startswith(key, "RUN")) {
1334 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
1338 if (streq(attr, "builtin")) {
1339 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1341 if (cmd < UDEV_BUILTIN_MAX)
1342 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1344 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1345 } else if (streq(attr, "program")) {
1346 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1348 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1350 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
1356 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1357 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1361 if (streq(key, "LABEL")) {
1362 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1366 if (streq(key, "GOTO")) {
1367 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1371 if (startswith(key, "NAME")) {
1372 if (op < OP_MATCH_MAX) {
1373 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1375 if (streq(value, "%k")) {
1376 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1377 "please remove it from %s:%u\n", filename, lineno);
1380 if (value[0] == '\0') {
1381 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1382 "please remove it from %s:%u\n", filename, lineno);
1385 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1387 rule_tmp.rule.rule.can_set_name = true;
1391 if (streq(key, "SYMLINK")) {
1392 if (op < OP_MATCH_MAX)
1393 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1395 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1396 rule_tmp.rule.rule.can_set_name = true;
1400 if (streq(key, "OWNER")) {
1404 uid = strtoul(value, &endptr, 10);
1405 if (endptr[0] == '\0') {
1406 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1407 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1408 uid = add_uid(rules, value);
1409 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1410 } else if (rules->resolve_names >= 0) {
1411 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1413 rule_tmp.rule.rule.can_set_name = true;
1417 if (streq(key, "GROUP")) {
1421 gid = strtoul(value, &endptr, 10);
1422 if (endptr[0] == '\0') {
1423 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1424 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1425 gid = add_gid(rules, value);
1426 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1427 } else if (rules->resolve_names >= 0) {
1428 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1430 rule_tmp.rule.rule.can_set_name = true;
1434 if (streq(key, "MODE")) {
1438 mode = strtol(value, &endptr, 8);
1439 if (endptr[0] == '\0')
1440 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1442 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1443 rule_tmp.rule.rule.can_set_name = true;
1447 if (streq(key, "OPTIONS")) {
1450 pos = strstr(value, "link_priority=");
1452 int prio = atoi(&pos[strlen("link_priority=")]);
1454 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1458 pos = &pos[strlen("string_escape=")];
1459 if (startswith(pos, "none"))
1460 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1461 else if (startswith(pos, "replace"))
1462 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1465 pos = strstr(value, "db_persist");
1467 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1469 pos = strstr(value, "nowatch");
1473 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1475 pos = strstr(value, "watch");
1479 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1483 pos = strstr(value, "static_node=");
1485 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1486 rule_tmp.rule.rule.has_static_node = true;
1492 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
1496 /* add rule token */
1497 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1498 if (add_token(rules, &rule_tmp.rule) != 0)
1501 /* add tokens to list, sorted by type */
1502 if (sort_token(rules, &rule_tmp) != 0)
1507 log_error("invalid rule '%s:%u'", filename, lineno);
1511 static int parse_file(struct udev_rules *rules, const char *filename)
1514 unsigned int first_token;
1515 unsigned int filename_off;
1516 char line[UTIL_LINE_SIZE];
1520 f = fopen(filename, "re");
1522 if (errno == ENOENT)
1528 if (null_or_empty_fd(fileno(f))) {
1529 log_debug("Skipping empty file: %s", filename);
1532 log_debug("Reading rules file: %s", filename);
1534 first_token = rules->token_cur;
1535 filename_off = rules_add_string(rules, filename);
1537 while (fgets(line, sizeof(line), f) != NULL) {
1541 /* skip whitespace */
1544 while (isspace(key[0]))
1555 /* continue reading if backslash+newline is found */
1556 while (line[len-2] == '\\') {
1557 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1559 if (strlen(&line[len-2]) < 2)
1565 if (len+1 >= sizeof(line)) {
1566 log_error("line too long '%s':%u, ignored", filename, line_nr);
1569 add_rule(rules, key, filename, filename_off, line_nr);
1573 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1574 for (i = first_token+1; i < rules->token_cur; i++) {
1575 if (rules->tokens[i].type == TK_A_GOTO) {
1576 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1579 for (j = i+1; j < rules->token_cur; j++) {
1580 if (rules->tokens[j].type != TK_RULE)
1582 if (rules->tokens[j].rule.label_off == 0)
1584 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1586 rules->tokens[i].key.rule_goto = j;
1589 if (rules->tokens[i].key.rule_goto == 0)
1590 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
1596 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1598 struct udev_rules *rules;
1599 struct udev_list file_list;
1600 struct token end_token;
1604 rules = new0(struct udev_rules, 1);
1608 rules->resolve_names = resolve_names;
1609 udev_list_init(udev, &file_list, true);
1611 /* init token array and string buffer */
1612 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1613 if (rules->tokens == NULL)
1614 return udev_rules_unref(rules);
1615 rules->token_max = PREALLOC_TOKEN;
1617 rules->strbuf = strbuf_new();
1619 return udev_rules_unref(rules);
1621 udev_rules_check_timestamp(rules);
1623 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
1625 log_error("failed to enumerate rules files: %s", strerror(-r));
1626 return udev_rules_unref(rules);
1630 * The offset value in the rules strct is limited; add all
1631 * rules file names to the beginning of the string buffer.
1633 STRV_FOREACH(f, files)
1634 rules_add_string(rules, *f);
1636 STRV_FOREACH(f, files)
1637 parse_file(rules, *f);
1641 memzero(&end_token, sizeof(struct token));
1642 end_token.type = TK_END;
1643 add_token(rules, &end_token);
1644 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1645 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1647 /* cleanup temporary strbuf data */
1648 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1649 rules->strbuf->in_count, rules->strbuf->in_len,
1650 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1651 strbuf_complete(rules->strbuf);
1653 /* cleanup uid/gid cache */
1656 rules->uids_cur = 0;
1657 rules->uids_max = 0;
1660 rules->gids_cur = 0;
1661 rules->gids_max = 0;
1667 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1671 free(rules->tokens);
1672 strbuf_cleanup(rules->strbuf);
1679 bool udev_rules_check_timestamp(struct udev_rules *rules)
1684 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
1687 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1689 char *key_value = rules_str(rules, token->key.value_off);
1696 switch (token->key.glob) {
1698 match = (streq(key_value, val));
1701 match = (fnmatch(key_value, val, 0) == 0);
1708 s = rules_str(rules, token->key.value_off);
1713 next = strchr(s, '|');
1715 size_t matchlen = (size_t)(next - s);
1717 match = (matchlen == len && strneq(s, val, matchlen));
1721 match = (streq(s, val));
1730 char value[UTIL_PATH_SIZE];
1732 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1734 while (key_value != NULL) {
1735 pos = strchr(key_value, '|');
1740 match = (fnmatch(key_value, val, 0) == 0);
1748 match = (val[0] != '\0');
1754 if (match && (token->key.op == OP_MATCH))
1756 if (!match && (token->key.op == OP_NOMATCH))
1761 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1764 char nbuf[UTIL_NAME_SIZE];
1766 char vbuf[UTIL_NAME_SIZE];
1769 name = rules_str(rules, cur->key.attr_off);
1770 switch (cur->key.attrsubst) {
1772 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1776 value = udev_device_get_sysattr_value(dev, name);
1781 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1789 /* remove trailing whitespace, if not asked to match for it */
1790 len = strlen(value);
1791 if (len > 0 && isspace(value[len-1])) {
1792 const char *key_value;
1795 key_value = rules_str(rules, cur->key.value_off);
1796 klen = strlen(key_value);
1797 if (klen > 0 && !isspace(key_value[klen-1])) {
1798 if (value != vbuf) {
1799 strscpy(vbuf, sizeof(vbuf), value);
1802 while (len > 0 && isspace(vbuf[--len]))
1807 return match_key(rules, cur, value);
1816 int udev_rules_apply_to_event(struct udev_rules *rules,
1817 struct udev_event *event,
1818 usec_t timeout_usec,
1819 const sigset_t *sigmask) {
1822 enum escape_type esc = ESCAPE_UNSET;
1825 if (rules->tokens == NULL)
1828 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1829 (major(udev_device_get_devnum(event->dev)) > 0 ||
1830 udev_device_get_ifindex(event->dev) > 0));
1832 /* loop through token list, match, run actions or forward to next rule */
1833 cur = &rules->tokens[0];
1836 dump_token(rules, cur);
1837 switch (cur->type) {
1841 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1842 if (!can_set_name && rule->rule.can_set_name)
1847 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1851 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1855 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1858 case TK_M_DEVLINK: {
1859 struct udev_list_entry *list_entry;
1862 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1863 const char *devlink;
1865 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1866 if (match_key(rules, cur, devlink) == 0) {
1876 if (match_key(rules, cur, event->name) != 0)
1880 const char *key_name = rules_str(rules, cur->key.attr_off);
1883 value = udev_device_get_property_value(event->dev, key_name);
1886 if (match_key(rules, cur, value))
1891 struct udev_list_entry *list_entry;
1894 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1895 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1900 if (!match && (cur->key.op != OP_NOMATCH))
1904 case TK_M_SUBSYSTEM:
1905 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1909 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1912 case TK_M_WAITFOR: {
1913 char filename[UTIL_PATH_SIZE];
1916 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1917 found = (wait_for_file(event->dev, filename, 10) == 0);
1918 if (!found && (cur->key.op != OP_NOMATCH))
1923 if (match_attr(rules, event->dev, event, cur) != 0)
1927 case TK_M_SUBSYSTEMS:
1933 /* get whole sequence of parent matches */
1935 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1938 /* loop over parents */
1939 event->dev_parent = event->dev;
1943 /* loop over sequence of parent match keys */
1944 for (key = cur; key < next; key++ ) {
1945 dump_token(rules, key);
1948 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1951 case TK_M_SUBSYSTEMS:
1952 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1956 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1960 if (match_attr(rules, event->dev_parent, event, key) != 0)
1964 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
1966 if (match && key->key.op == OP_NOMATCH)
1968 if (!match && key->key.op == OP_MATCH)
1979 event->dev_parent = udev_device_get_parent(event->dev_parent);
1980 if (event->dev_parent == NULL)
1983 /* move behind our sequence of parent match keys */
1988 char filename[UTIL_PATH_SIZE];
1989 struct stat statbuf;
1992 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1993 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
1994 if (filename[0] != '/') {
1995 char tmp[UTIL_PATH_SIZE];
1997 strscpy(tmp, sizeof(tmp), filename);
1998 strscpyl(filename, sizeof(filename),
1999 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2002 attr_subst_subdir(filename, sizeof(filename));
2004 match = (stat(filename, &statbuf) == 0);
2005 if (match && cur->key.mode > 0)
2006 match = ((statbuf.st_mode & cur->key.mode) > 0);
2007 if (match && cur->key.op == OP_NOMATCH)
2009 if (!match && cur->key.op == OP_MATCH)
2013 case TK_M_PROGRAM: {
2014 char program[UTIL_PATH_SIZE];
2016 char result[UTIL_LINE_SIZE];
2018 free(event->program_result);
2019 event->program_result = NULL;
2020 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2021 envp = udev_device_get_properties_envp(event->dev);
2022 log_debug("PROGRAM '%s' %s:%u",
2024 rules_str(rules, rule->rule.filename_off),
2025 rule->rule.filename_line);
2027 if (udev_event_spawn(event, timeout_usec, program, envp, sigmask, result, sizeof(result)) < 0) {
2028 if (cur->key.op != OP_NOMATCH)
2033 util_remove_trailing_chars(result, '\n');
2034 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2035 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2037 log_debug("%i character(s) replaced" , count);
2039 event->program_result = strdup(result);
2040 if (cur->key.op == OP_NOMATCH)
2045 case TK_M_IMPORT_FILE: {
2046 char import[UTIL_PATH_SIZE];
2048 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2049 if (import_file_into_properties(event->dev, import) != 0)
2050 if (cur->key.op != OP_NOMATCH)
2054 case TK_M_IMPORT_PROG: {
2055 char import[UTIL_PATH_SIZE];
2057 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2058 log_debug("IMPORT '%s' %s:%u",
2060 rules_str(rules, rule->rule.filename_off),
2061 rule->rule.filename_line);
2063 if (import_program_into_properties(event, timeout_usec, import, sigmask) != 0)
2064 if (cur->key.op != OP_NOMATCH)
2068 case TK_M_IMPORT_BUILTIN: {
2069 char command[UTIL_PATH_SIZE];
2071 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2072 /* check if we ran already */
2073 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2074 log_debug("IMPORT builtin skip '%s' %s:%u",
2075 udev_builtin_name(cur->key.builtin_cmd),
2076 rules_str(rules, rule->rule.filename_off),
2077 rule->rule.filename_line);
2078 /* return the result from earlier run */
2079 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2080 if (cur->key.op != OP_NOMATCH)
2085 event->builtin_run |= (1 << cur->key.builtin_cmd);
2088 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2089 log_debug("IMPORT builtin '%s' %s:%u",
2090 udev_builtin_name(cur->key.builtin_cmd),
2091 rules_str(rules, rule->rule.filename_off),
2092 rule->rule.filename_line);
2094 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2095 /* remember failure */
2096 log_debug("IMPORT builtin '%s' returned non-zero",
2097 udev_builtin_name(cur->key.builtin_cmd));
2098 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2099 if (cur->key.op != OP_NOMATCH)
2104 case TK_M_IMPORT_DB: {
2105 const char *key = rules_str(rules, cur->key.value_off);
2108 value = udev_device_get_property_value(event->dev_db, key);
2109 if (value != NULL) {
2110 struct udev_list_entry *entry;
2112 entry = udev_device_add_property(event->dev, key, value);
2113 udev_list_entry_set_num(entry, true);
2115 if (cur->key.op != OP_NOMATCH)
2120 case TK_M_IMPORT_CMDLINE: {
2122 bool imported = false;
2124 f = fopen("/proc/cmdline", "re");
2128 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2129 const char *key = rules_str(rules, cur->key.value_off);
2132 pos = strstr(cmdline, key);
2134 struct udev_list_entry *entry;
2137 if (pos[0] == '\0' || isspace(pos[0])) {
2138 /* we import simple flags as 'FLAG=1' */
2139 entry = udev_device_add_property(event->dev, key, "1");
2140 udev_list_entry_set_num(entry, true);
2142 } else if (pos[0] == '=') {
2147 while (pos[0] != '\0' && !isspace(pos[0]))
2150 entry = udev_device_add_property(event->dev, key, value);
2151 udev_list_entry_set_num(entry, true);
2158 if (!imported && cur->key.op != OP_NOMATCH)
2162 case TK_M_IMPORT_PARENT: {
2163 char import[UTIL_PATH_SIZE];
2165 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2166 if (import_parent_into_properties(event->dev, import) != 0)
2167 if (cur->key.op != OP_NOMATCH)
2172 if (match_key(rules, cur, event->program_result) != 0)
2175 case TK_A_STRING_ESCAPE_NONE:
2178 case TK_A_STRING_ESCAPE_REPLACE:
2179 esc = ESCAPE_REPLACE;
2181 case TK_A_DB_PERSIST:
2182 udev_device_set_db_persist(event->dev);
2184 case TK_A_INOTIFY_WATCH:
2185 if (event->inotify_watch_final)
2187 if (cur->key.op == OP_ASSIGN_FINAL)
2188 event->inotify_watch_final = true;
2189 event->inotify_watch = cur->key.watch;
2191 case TK_A_DEVLINK_PRIO:
2192 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2195 char owner[UTIL_NAME_SIZE];
2197 if (event->owner_final)
2199 if (cur->key.op == OP_ASSIGN_FINAL)
2200 event->owner_final = true;
2201 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2202 event->owner_set = true;
2203 event->uid = util_lookup_user(event->udev, owner);
2204 log_debug("OWNER %u %s:%u",
2206 rules_str(rules, rule->rule.filename_off),
2207 rule->rule.filename_line);
2211 char group[UTIL_NAME_SIZE];
2213 if (event->group_final)
2215 if (cur->key.op == OP_ASSIGN_FINAL)
2216 event->group_final = true;
2217 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2218 event->group_set = true;
2219 event->gid = util_lookup_group(event->udev, group);
2220 log_debug("GROUP %u %s:%u",
2222 rules_str(rules, rule->rule.filename_off),
2223 rule->rule.filename_line);
2227 char mode_str[UTIL_NAME_SIZE];
2231 if (event->mode_final)
2233 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2234 mode = strtol(mode_str, &endptr, 8);
2235 if (endptr[0] != '\0') {
2236 log_error("ignoring invalid mode '%s'", mode_str);
2239 if (cur->key.op == OP_ASSIGN_FINAL)
2240 event->mode_final = true;
2241 event->mode_set = true;
2243 log_debug("MODE %#o %s:%u",
2245 rules_str(rules, rule->rule.filename_off),
2246 rule->rule.filename_line);
2250 if (event->owner_final)
2252 if (cur->key.op == OP_ASSIGN_FINAL)
2253 event->owner_final = true;
2254 event->owner_set = true;
2255 event->uid = cur->key.uid;
2256 log_debug("OWNER %u %s:%u",
2258 rules_str(rules, rule->rule.filename_off),
2259 rule->rule.filename_line);
2262 if (event->group_final)
2264 if (cur->key.op == OP_ASSIGN_FINAL)
2265 event->group_final = true;
2266 event->group_set = true;
2267 event->gid = cur->key.gid;
2268 log_debug("GROUP %u %s:%u",
2270 rules_str(rules, rule->rule.filename_off),
2271 rule->rule.filename_line);
2274 if (event->mode_final)
2276 if (cur->key.op == OP_ASSIGN_FINAL)
2277 event->mode_final = true;
2278 event->mode_set = true;
2279 event->mode = cur->key.mode;
2280 log_debug("MODE %#o %s:%u",
2282 rules_str(rules, rule->rule.filename_off),
2283 rule->rule.filename_line);
2285 case TK_A_SECLABEL: {
2286 const char *name, *label;
2288 name = rules_str(rules, cur->key.attr_off);
2289 label = rules_str(rules, cur->key.value_off);
2290 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2291 udev_list_cleanup(&event->seclabel_list);
2292 udev_list_entry_add(&event->seclabel_list, name, label);
2293 log_debug("SECLABEL{%s}='%s' %s:%u",
2295 rules_str(rules, rule->rule.filename_off),
2296 rule->rule.filename_line);
2300 const char *name = rules_str(rules, cur->key.attr_off);
2301 char *value = rules_str(rules, cur->key.value_off);
2302 char value_new[UTIL_NAME_SIZE];
2303 const char *value_old = NULL;
2304 struct udev_list_entry *entry;
2306 if (value[0] == '\0') {
2307 if (cur->key.op == OP_ADD)
2309 udev_device_add_property(event->dev, name, NULL);
2313 if (cur->key.op == OP_ADD)
2314 value_old = udev_device_get_property_value(event->dev, name);
2316 char temp[UTIL_NAME_SIZE];
2318 /* append value separated by space */
2319 udev_event_apply_format(event, value, temp, sizeof(temp));
2320 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2322 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2324 entry = udev_device_add_property(event->dev, name, value_new);
2325 /* store in db, skip private keys */
2327 udev_list_entry_set_num(entry, true);
2331 char tag[UTIL_PATH_SIZE];
2334 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2335 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2336 udev_device_cleanup_tags_list(event->dev);
2337 for (p = tag; *p != '\0'; p++) {
2338 if ((*p >= 'a' && *p <= 'z') ||
2339 (*p >= 'A' && *p <= 'Z') ||
2340 (*p >= '0' && *p <= '9') ||
2341 *p == '-' || *p == '_')
2343 log_error("ignoring invalid tag name '%s'", tag);
2346 udev_device_add_tag(event->dev, tag);
2350 const char *name = rules_str(rules, cur->key.value_off);
2352 char name_str[UTIL_PATH_SIZE];
2355 if (event->name_final)
2357 if (cur->key.op == OP_ASSIGN_FINAL)
2358 event->name_final = true;
2359 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2360 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2361 count = util_replace_chars(name_str, "/");
2363 log_debug("%i character(s) replaced", count);
2365 if (major(udev_device_get_devnum(event->dev)) &&
2366 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2367 log_error("NAME=\"%s\" ignored, kernel device nodes "
2368 "can not be renamed; please fix it in %s:%u\n", name,
2369 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2373 event->name = strdup(name_str);
2374 log_debug("NAME '%s' %s:%u",
2376 rules_str(rules, rule->rule.filename_off),
2377 rule->rule.filename_line);
2380 case TK_A_DEVLINK: {
2381 char temp[UTIL_PATH_SIZE];
2382 char filename[UTIL_PATH_SIZE];
2386 if (event->devlink_final)
2388 if (major(udev_device_get_devnum(event->dev)) == 0)
2390 if (cur->key.op == OP_ASSIGN_FINAL)
2391 event->devlink_final = true;
2392 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2393 udev_device_cleanup_devlinks_list(event->dev);
2395 /* allow multiple symlinks separated by spaces */
2396 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2397 if (esc == ESCAPE_UNSET)
2398 count = util_replace_chars(temp, "/ ");
2399 else if (esc == ESCAPE_REPLACE)
2400 count = util_replace_chars(temp, "/");
2402 log_debug("%i character(s) replaced" , count);
2404 while (isspace(pos[0]))
2406 next = strchr(pos, ' ');
2407 while (next != NULL) {
2409 log_debug("LINK '%s' %s:%u", pos,
2410 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2411 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2412 udev_device_add_devlink(event->dev, filename);
2413 while (isspace(next[1]))
2416 next = strchr(pos, ' ');
2418 if (pos[0] != '\0') {
2419 log_debug("LINK '%s' %s:%u", pos,
2420 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2421 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2422 udev_device_add_devlink(event->dev, filename);
2427 const char *key_name = rules_str(rules, cur->key.attr_off);
2428 char attr[UTIL_PATH_SIZE];
2429 char value[UTIL_NAME_SIZE];
2432 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2433 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2434 attr_subst_subdir(attr, sizeof(attr));
2436 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2437 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
2438 rules_str(rules, rule->rule.filename_off),
2439 rule->rule.filename_line);
2440 f = fopen(attr, "we");
2442 if (fprintf(f, "%s", value) <= 0)
2443 log_error("error writing ATTR{%s}: %m", attr);
2446 log_error("error opening ATTR{%s} for writing: %m", attr);
2450 case TK_A_RUN_BUILTIN:
2451 case TK_A_RUN_PROGRAM: {
2452 struct udev_list_entry *entry;
2454 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2455 udev_list_cleanup(&event->run_list);
2456 log_debug("RUN '%s' %s:%u",
2457 rules_str(rules, cur->key.value_off),
2458 rules_str(rules, rule->rule.filename_off),
2459 rule->rule.filename_line);
2460 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2461 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2465 if (cur->key.rule_goto == 0)
2467 cur = &rules->tokens[cur->key.rule_goto];
2472 case TK_M_PARENTS_MIN:
2473 case TK_M_PARENTS_MAX:
2476 log_error("wrong type %u", cur->type);
2483 /* fast-forward to next rule */
2484 cur = rule + rule->rule.token_count;
2488 int udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2495 _cleanup_strv_free_ char **tags = NULL;
2498 _cleanup_free_ char *path = NULL;
2501 if (rules->tokens == NULL)
2504 cur = &rules->tokens[0];
2507 switch (cur->type) {
2512 /* skip rules without a static_node tag */
2513 if (!rule->rule.has_static_node)
2529 mode = cur->key.mode;
2532 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2537 case TK_A_STATIC_NODE: {
2538 char device_node[UTIL_PATH_SIZE];
2539 char tags_dir[UTIL_PATH_SIZE];
2540 char tag_symlink[UTIL_PATH_SIZE];
2543 /* we assure, that the permissions tokens are sorted before the static token */
2545 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2548 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2549 if (stat(device_node, &stats) != 0)
2551 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2554 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2556 STRV_FOREACH(t, tags) {
2557 _cleanup_free_ char *unescaped_filename = NULL;
2559 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2560 r = mkdir_p(tags_dir, 0755);
2562 log_error("failed to create %s: %s", tags_dir, strerror(-r));
2566 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2568 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2569 r = symlink(device_node, tag_symlink);
2570 if (r < 0 && errno != EEXIST) {
2571 log_error("failed to create symlink %s -> %s: %m", tag_symlink, device_node);
2578 /* don't touch the permissions if only the tags were set */
2579 if (mode == 0 && uid == 0 && gid == 0)
2588 if (mode != (stats.st_mode & 01777)) {
2589 r = chmod(device_node, mode);
2591 log_error("failed to chmod '%s' %#o", device_node, mode);
2594 log_debug("chmod '%s' %#o", device_node, mode);
2597 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2598 r = chown(device_node, uid, gid);
2600 log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
2603 log_debug("chown '%s' %u %u", device_node, uid, gid);
2606 utimensat(AT_FDCWD, device_node, NULL, 0);
2616 /* fast-forward to next rule */
2617 cur = rule + rule->rule.token_count;
2624 fchmod(fileno(f), 0644);
2625 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2627 unlink("/run/udev/static_node-tags");