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) {
464 /* lookup, if we know it already */
465 for (i = 0; i < rules->uids_cur; i++) {
466 off = rules->uids[i].name_off;
467 if (streq(rules_str(rules, off), owner)) {
468 uid = rules->uids[i].uid;
472 r = get_user_creds(&owner, &uid, NULL, NULL, NULL);
476 /* grow buffer if needed */
477 if (rules->uids_cur+1 >= rules->uids_max) {
478 struct uid_gid *uids;
481 /* double the buffer size */
482 add = rules->uids_max;
486 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
490 rules->uids_max += add;
492 rules->uids[rules->uids_cur].uid = uid;
493 off = rules_add_string(rules, owner);
496 rules->uids[rules->uids_cur].name_off = off;
501 static gid_t add_gid(struct udev_rules *rules, const char *group) {
507 /* lookup, if we know it already */
508 for (i = 0; i < rules->gids_cur; i++) {
509 off = rules->gids[i].name_off;
510 if (streq(rules_str(rules, off), group)) {
511 gid = rules->gids[i].gid;
515 r = get_group_creds(&group, &gid);
519 /* grow buffer if needed */
520 if (rules->gids_cur+1 >= rules->gids_max) {
521 struct uid_gid *gids;
524 /* double the buffer size */
525 add = rules->gids_max;
529 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
533 rules->gids_max += add;
535 rules->gids[rules->gids_cur].gid = gid;
536 off = rules_add_string(rules, group);
539 rules->gids[rules->gids_cur].name_off = off;
544 static int import_property_from_string(struct udev_device *dev, char *line) {
548 struct udev_list_entry *entry;
552 while (isspace(key[0]))
555 /* comment or empty line */
556 if (key[0] == '#' || key[0] == '\0')
559 /* split key/value */
560 val = strchr(key, '=');
567 while (isspace(val[0]))
574 while (isspace(key[len-1]))
578 /* terminate value */
582 while (isspace(val[len-1]))
590 if (val[0] == '"' || val[0] == '\'') {
591 if (val[len-1] != val[0]) {
592 log_debug("inconsistent quoting: '%s', skip", line);
599 entry = udev_device_add_property(dev, key, val);
600 /* store in db, skip private keys */
602 udev_list_entry_set_num(entry, true);
607 static int import_file_into_properties(struct udev_device *dev, const char *filename) {
609 char line[UTIL_LINE_SIZE];
611 f = fopen(filename, "re");
614 while (fgets(line, sizeof(line), f) != NULL)
615 import_property_from_string(dev, line);
620 static int import_program_into_properties(struct udev_event *event,
622 const char *program, const sigset_t *sigmask) {
623 struct udev_device *dev = event->dev;
625 char result[UTIL_LINE_SIZE];
629 envp = udev_device_get_properties_envp(dev);
630 err = udev_event_spawn(event, timeout_usec, program, envp, sigmask, result, sizeof(result));
635 while (line != NULL) {
638 pos = strchr(line, '\n');
643 import_property_from_string(dev, line);
649 static int import_parent_into_properties(struct udev_device *dev, const char *filter) {
650 struct udev_device *dev_parent;
651 struct udev_list_entry *list_entry;
653 dev_parent = udev_device_get_parent(dev);
654 if (dev_parent == NULL)
657 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
658 const char *key = udev_list_entry_get_name(list_entry);
659 const char *val = udev_list_entry_get_value(list_entry);
661 if (fnmatch(filter, key, 0) == 0) {
662 struct udev_list_entry *entry;
664 entry = udev_device_add_property(dev, key, val);
665 /* store in db, skip private keys */
667 udev_list_entry_set_num(entry, true);
673 #define WAIT_LOOP_PER_SECOND 50
674 static int wait_for_file(struct udev_device *dev, const char *file, int timeout) {
675 char filepath[UTIL_PATH_SIZE];
676 char devicepath[UTIL_PATH_SIZE];
678 int loop = timeout * WAIT_LOOP_PER_SECOND;
680 /* a relative path is a device attribute */
681 devicepath[0] = '\0';
682 if (file[0] != '/') {
683 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
684 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
689 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
692 if (stat(file, &stats) == 0) {
693 log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
696 /* make sure, the device did not disappear in the meantime */
697 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
698 log_debug("device disappeared while waiting for '%s'", file);
701 log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
702 nanosleep(&duration, NULL);
704 log_debug("waiting for '%s' failed", file);
708 static int attr_subst_subdir(char *attr, size_t len) {
711 if (strstr(attr, "/*/")) {
713 char dirname[UTIL_PATH_SIZE];
717 strscpy(dirname, sizeof(dirname), attr);
718 pos = strstr(dirname, "/*/");
723 dir = opendir(dirname);
727 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
730 if (dent->d_name[0] == '.')
732 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
733 if (stat(attr, &stats) == 0) {
745 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) {
750 if (linepos == NULL || linepos[0] == '\0')
753 /* skip whitespace */
754 while (isspace(linepos[0]) || linepos[0] == ',')
758 if (linepos[0] == '\0')
764 if (linepos[0] == '\0')
766 if (isspace(linepos[0]))
768 if (linepos[0] == '=')
770 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
771 if (linepos[1] == '=')
775 /* remember end of key */
778 /* skip whitespace after key */
779 while (isspace(linepos[0]))
781 if (linepos[0] == '\0')
784 /* get operation type */
785 if (linepos[0] == '=' && linepos[1] == '=') {
788 } else if (linepos[0] == '!' && linepos[1] == '=') {
791 } else if (linepos[0] == '+' && linepos[1] == '=') {
794 } else if (linepos[0] == '=') {
797 } else if (linepos[0] == ':' && linepos[1] == '=') {
798 *op = OP_ASSIGN_FINAL;
806 /* skip whitespace after operator */
807 while (isspace(linepos[0]))
809 if (linepos[0] == '\0')
813 if (linepos[0] == '"')
820 temp = strchr(linepos, '"');
826 /* move line to next key */
831 /* extract possible KEY{attr} */
832 static const char *get_key_attribute(struct udev *udev, char *str) {
836 attr = strchr(str, '{');
839 pos = strchr(attr, '}');
841 log_error("missing closing brace for format");
850 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
851 enum operation_type op,
852 const char *value, const void *data) {
853 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
854 const char *attr = NULL;
856 memzero(token, sizeof(struct token));
868 case TK_M_SUBSYSTEMS:
872 case TK_M_IMPORT_FILE:
873 case TK_M_IMPORT_PROG:
875 case TK_M_IMPORT_CMDLINE:
876 case TK_M_IMPORT_PARENT:
886 case TK_A_STATIC_NODE:
887 token->key.value_off = rules_add_string(rule_tmp->rules, value);
889 case TK_M_IMPORT_BUILTIN:
890 token->key.value_off = rules_add_string(rule_tmp->rules, value);
891 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
900 token->key.value_off = rules_add_string(rule_tmp->rules, value);
901 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
904 token->key.value_off = rules_add_string(rule_tmp->rules, value);
906 token->key.mode = *(mode_t *)data;
908 case TK_A_STRING_ESCAPE_NONE:
909 case TK_A_STRING_ESCAPE_REPLACE:
910 case TK_A_DB_PERSIST:
912 case TK_A_RUN_BUILTIN:
913 case TK_A_RUN_PROGRAM:
914 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
915 token->key.value_off = rules_add_string(rule_tmp->rules, value);
917 case TK_A_INOTIFY_WATCH:
918 case TK_A_DEVLINK_PRIO:
919 token->key.devlink_prio = *(int *)data;
922 token->key.uid = *(uid_t *)data;
925 token->key.gid = *(gid_t *)data;
928 token->key.mode = *(mode_t *)data;
931 case TK_M_PARENTS_MIN:
932 case TK_M_PARENTS_MAX:
936 log_error("wrong type %u", type);
940 if (value != NULL && type < TK_M_MAX) {
941 /* check if we need to split or call fnmatch() while matching rules */
942 enum string_glob_type glob;
946 has_split = (strchr(value, '|') != NULL);
947 has_glob = string_is_glob(value);
948 if (has_split && has_glob) {
949 glob = GL_SPLIT_GLOB;
950 } else if (has_split) {
952 } else if (has_glob) {
953 if (streq(value, "?*"))
960 token->key.glob = glob;
963 if (value != NULL && type > TK_M_MAX) {
964 /* check if assigned value has substitution chars */
966 token->key.subst = SB_SUBSYS;
967 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
968 token->key.subst = SB_FORMAT;
970 token->key.subst = SB_NONE;
974 /* check if property/attribute name has substitution chars */
976 token->key.attrsubst = SB_SUBSYS;
977 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
978 token->key.attrsubst = SB_FORMAT;
980 token->key.attrsubst = SB_NONE;
983 token->key.type = type;
985 rule_tmp->token_cur++;
986 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
987 log_error("temporary rule array too small");
993 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
995 unsigned int start = 0;
996 unsigned int end = rule_tmp->token_cur;
998 for (i = 0; i < rule_tmp->token_cur; i++) {
999 enum token_type next_val = TK_UNSET;
1000 unsigned int next_idx = 0;
1003 /* find smallest value */
1004 for (j = start; j < end; j++) {
1005 if (rule_tmp->token[j].type == TK_UNSET)
1007 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1008 next_val = rule_tmp->token[j].type;
1013 /* add token and mark done */
1014 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1016 rule_tmp->token[next_idx].type = TK_UNSET;
1019 if (next_idx == start)
1021 if (next_idx+1 == end)
1027 static int add_rule(struct udev_rules *rules, char *line,
1028 const char *filename, unsigned int filename_off, unsigned int lineno) {
1031 struct rule_tmp rule_tmp;
1033 memzero(&rule_tmp, sizeof(struct rule_tmp));
1034 rule_tmp.rules = rules;
1035 rule_tmp.rule.type = TK_RULE;
1036 /* the offset in the rule is limited to unsigned short */
1037 if (filename_off < USHRT_MAX)
1038 rule_tmp.rule.rule.filename_off = filename_off;
1039 rule_tmp.rule.rule.filename_line = lineno;
1045 enum operation_type op;
1047 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1048 /* Avoid erroring on trailing whitespace. This is probably rare
1049 * so save the work for the error case instead of always trying
1050 * to strip the trailing whitespace with strstrip(). */
1051 while (isblank(*linepos))
1054 /* If we aren't at the end of the line, this is a parsing error.
1055 * Make a best effort to describe where the problem is. */
1056 if (*linepos != '\n') {
1057 char buf[2] = {linepos[1]};
1058 _cleanup_free_ char *tmp;
1061 log_error("invalid key/value pair in file %s on line %u,"
1062 "starting at character %tu ('%s')\n",
1063 filename, lineno, linepos - line + 1, tmp);
1064 if (linepos[1] == '#')
1065 log_error("hint: comments can only start at beginning of line");
1070 if (streq(key, "ACTION")) {
1071 if (op > OP_MATCH_MAX) {
1072 log_error("invalid ACTION operation");
1075 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1079 if (streq(key, "DEVPATH")) {
1080 if (op > OP_MATCH_MAX) {
1081 log_error("invalid DEVPATH operation");
1084 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1088 if (streq(key, "KERNEL")) {
1089 if (op > OP_MATCH_MAX) {
1090 log_error("invalid KERNEL operation");
1093 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1097 if (streq(key, "SUBSYSTEM")) {
1098 if (op > OP_MATCH_MAX) {
1099 log_error("invalid SUBSYSTEM operation");
1102 /* bus, class, subsystem events should all be the same */
1103 if (streq(value, "subsystem") ||
1104 streq(value, "bus") ||
1105 streq(value, "class")) {
1106 if (streq(value, "bus") || streq(value, "class"))
1107 log_error("'%s' must be specified as 'subsystem' "
1108 "please fix it in %s:%u", value, filename, lineno);
1109 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1111 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1115 if (streq(key, "DRIVER")) {
1116 if (op > OP_MATCH_MAX) {
1117 log_error("invalid DRIVER operation");
1120 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1124 if (startswith(key, "ATTR{")) {
1125 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
1127 log_error("error parsing ATTR attribute");
1130 if (op < OP_MATCH_MAX) {
1131 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1133 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1138 if (startswith(key, "SECLABEL{")) {
1139 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
1141 log_error("error parsing SECLABEL attribute");
1145 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1149 if (streq(key, "KERNELS")) {
1150 if (op > OP_MATCH_MAX) {
1151 log_error("invalid KERNELS operation");
1154 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1158 if (streq(key, "SUBSYSTEMS")) {
1159 if (op > OP_MATCH_MAX) {
1160 log_error("invalid SUBSYSTEMS operation");
1163 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1167 if (streq(key, "DRIVERS")) {
1168 if (op > OP_MATCH_MAX) {
1169 log_error("invalid DRIVERS operation");
1172 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1176 if (startswith(key, "ATTRS{")) {
1177 if (op > OP_MATCH_MAX) {
1178 log_error("invalid ATTRS operation");
1181 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
1183 log_error("error parsing ATTRS attribute");
1186 if (startswith(attr, "device/"))
1187 log_error("the 'device' link may not be available in a future kernel, "
1188 "please fix it in %s:%u", filename, lineno);
1189 else if (strstr(attr, "../") != NULL)
1190 log_error("do not reference parent sysfs directories directly, "
1191 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1192 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1196 if (streq(key, "TAGS")) {
1197 if (op > OP_MATCH_MAX) {
1198 log_error("invalid TAGS operation");
1201 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1205 if (startswith(key, "ENV{")) {
1206 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
1208 log_error("error parsing ENV attribute");
1211 if (op < OP_MATCH_MAX) {
1212 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1215 static const char *blacklist[] = {
1230 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1231 if (!streq(attr, blacklist[i]))
1233 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
1236 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1242 if (streq(key, "TAG")) {
1243 if (op < OP_MATCH_MAX)
1244 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1246 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1250 if (streq(key, "PROGRAM")) {
1251 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1255 if (streq(key, "RESULT")) {
1256 if (op > OP_MATCH_MAX) {
1257 log_error("invalid RESULT operation");
1260 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1264 if (startswith(key, "IMPORT")) {
1265 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
1267 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
1270 if (streq(attr, "program")) {
1271 /* find known built-in command */
1272 if (value[0] != '/') {
1273 enum udev_builtin_cmd cmd;
1275 cmd = udev_builtin_lookup(value);
1276 if (cmd < UDEV_BUILTIN_MAX) {
1277 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1278 value, filename, lineno);
1279 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1283 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1284 } else if (streq(attr, "builtin")) {
1285 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1287 if (cmd < UDEV_BUILTIN_MAX)
1288 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1290 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1291 } else if (streq(attr, "file")) {
1292 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1293 } else if (streq(attr, "db")) {
1294 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1295 } else if (streq(attr, "cmdline")) {
1296 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1297 } else if (streq(attr, "parent")) {
1298 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1300 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
1304 if (startswith(key, "TEST")) {
1307 if (op > OP_MATCH_MAX) {
1308 log_error("invalid TEST operation");
1311 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
1313 mode = strtol(attr, NULL, 8);
1314 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1316 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1321 if (startswith(key, "RUN")) {
1322 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
1326 if (streq(attr, "builtin")) {
1327 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1329 if (cmd < UDEV_BUILTIN_MAX)
1330 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1332 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
1333 } else if (streq(attr, "program")) {
1334 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1336 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1338 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
1344 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1345 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1349 if (streq(key, "LABEL")) {
1350 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1354 if (streq(key, "GOTO")) {
1355 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1359 if (startswith(key, "NAME")) {
1360 if (op < OP_MATCH_MAX) {
1361 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1363 if (streq(value, "%k")) {
1364 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1365 "please remove it from %s:%u\n", filename, lineno);
1368 if (value[0] == '\0') {
1369 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1370 "please remove it from %s:%u\n", filename, lineno);
1373 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1375 rule_tmp.rule.rule.can_set_name = true;
1379 if (streq(key, "SYMLINK")) {
1380 if (op < OP_MATCH_MAX)
1381 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1383 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1384 rule_tmp.rule.rule.can_set_name = true;
1388 if (streq(key, "OWNER")) {
1392 uid = strtoul(value, &endptr, 10);
1393 if (endptr[0] == '\0') {
1394 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1395 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1396 uid = add_uid(rules, value);
1397 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1398 } else if (rules->resolve_names >= 0) {
1399 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1401 rule_tmp.rule.rule.can_set_name = true;
1405 if (streq(key, "GROUP")) {
1409 gid = strtoul(value, &endptr, 10);
1410 if (endptr[0] == '\0') {
1411 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1412 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1413 gid = add_gid(rules, value);
1414 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1415 } else if (rules->resolve_names >= 0) {
1416 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1418 rule_tmp.rule.rule.can_set_name = true;
1422 if (streq(key, "MODE")) {
1426 mode = strtol(value, &endptr, 8);
1427 if (endptr[0] == '\0')
1428 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1430 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1431 rule_tmp.rule.rule.can_set_name = true;
1435 if (streq(key, "OPTIONS")) {
1438 pos = strstr(value, "link_priority=");
1440 int prio = atoi(&pos[strlen("link_priority=")]);
1442 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1445 pos = strstr(value, "string_escape=");
1447 pos = &pos[strlen("string_escape=")];
1448 if (startswith(pos, "none"))
1449 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1450 else if (startswith(pos, "replace"))
1451 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1454 pos = strstr(value, "db_persist");
1456 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1458 pos = strstr(value, "nowatch");
1462 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1464 pos = strstr(value, "watch");
1468 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1472 pos = strstr(value, "static_node=");
1474 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1475 rule_tmp.rule.rule.has_static_node = true;
1481 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
1485 /* add rule token */
1486 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1487 if (add_token(rules, &rule_tmp.rule) != 0)
1490 /* add tokens to list, sorted by type */
1491 if (sort_token(rules, &rule_tmp) != 0)
1496 log_error("invalid rule '%s:%u'", filename, lineno);
1500 static int parse_file(struct udev_rules *rules, const char *filename) {
1502 unsigned int first_token;
1503 unsigned int filename_off;
1504 char line[UTIL_LINE_SIZE];
1508 f = fopen(filename, "re");
1510 if (errno == ENOENT)
1516 if (null_or_empty_fd(fileno(f))) {
1517 log_debug("Skipping empty file: %s", filename);
1520 log_debug("Reading rules file: %s", filename);
1522 first_token = rules->token_cur;
1523 filename_off = rules_add_string(rules, filename);
1525 while (fgets(line, sizeof(line), f) != NULL) {
1529 /* skip whitespace */
1532 while (isspace(key[0]))
1543 /* continue reading if backslash+newline is found */
1544 while (line[len-2] == '\\') {
1545 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1547 if (strlen(&line[len-2]) < 2)
1553 if (len+1 >= sizeof(line)) {
1554 log_error("line too long '%s':%u, ignored", filename, line_nr);
1557 add_rule(rules, key, filename, filename_off, line_nr);
1561 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1562 for (i = first_token+1; i < rules->token_cur; i++) {
1563 if (rules->tokens[i].type == TK_A_GOTO) {
1564 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1567 for (j = i+1; j < rules->token_cur; j++) {
1568 if (rules->tokens[j].type != TK_RULE)
1570 if (rules->tokens[j].rule.label_off == 0)
1572 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1574 rules->tokens[i].key.rule_goto = j;
1577 if (rules->tokens[i].key.rule_goto == 0)
1578 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
1584 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) {
1585 struct udev_rules *rules;
1586 struct udev_list file_list;
1587 struct token end_token;
1591 rules = new0(struct udev_rules, 1);
1595 rules->resolve_names = resolve_names;
1596 udev_list_init(udev, &file_list, true);
1598 /* init token array and string buffer */
1599 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1600 if (rules->tokens == NULL)
1601 return udev_rules_unref(rules);
1602 rules->token_max = PREALLOC_TOKEN;
1604 rules->strbuf = strbuf_new();
1606 return udev_rules_unref(rules);
1608 udev_rules_check_timestamp(rules);
1610 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
1612 log_error("failed to enumerate rules files: %s", strerror(-r));
1613 return udev_rules_unref(rules);
1617 * The offset value in the rules strct is limited; add all
1618 * rules file names to the beginning of the string buffer.
1620 STRV_FOREACH(f, files)
1621 rules_add_string(rules, *f);
1623 STRV_FOREACH(f, files)
1624 parse_file(rules, *f);
1628 memzero(&end_token, sizeof(struct token));
1629 end_token.type = TK_END;
1630 add_token(rules, &end_token);
1631 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1632 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1634 /* cleanup temporary strbuf data */
1635 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1636 rules->strbuf->in_count, rules->strbuf->in_len,
1637 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1638 strbuf_complete(rules->strbuf);
1640 /* cleanup uid/gid cache */
1643 rules->uids_cur = 0;
1644 rules->uids_max = 0;
1647 rules->gids_cur = 0;
1648 rules->gids_max = 0;
1654 struct udev_rules *udev_rules_unref(struct udev_rules *rules) {
1657 free(rules->tokens);
1658 strbuf_cleanup(rules->strbuf);
1665 bool udev_rules_check_timestamp(struct udev_rules *rules) {
1669 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
1672 static int match_key(struct udev_rules *rules, struct token *token, const char *val) {
1673 char *key_value = rules_str(rules, token->key.value_off);
1680 switch (token->key.glob) {
1682 match = (streq(key_value, val));
1685 match = (fnmatch(key_value, val, 0) == 0);
1692 s = rules_str(rules, token->key.value_off);
1697 next = strchr(s, '|');
1699 size_t matchlen = (size_t)(next - s);
1701 match = (matchlen == len && strneq(s, val, matchlen));
1705 match = (streq(s, val));
1714 char value[UTIL_PATH_SIZE];
1716 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1718 while (key_value != NULL) {
1719 pos = strchr(key_value, '|');
1724 match = (fnmatch(key_value, val, 0) == 0);
1732 match = (val[0] != '\0');
1738 if (match && (token->key.op == OP_MATCH))
1740 if (!match && (token->key.op == OP_NOMATCH))
1745 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) {
1747 char nbuf[UTIL_NAME_SIZE];
1749 char vbuf[UTIL_NAME_SIZE];
1752 name = rules_str(rules, cur->key.attr_off);
1753 switch (cur->key.attrsubst) {
1755 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1759 value = udev_device_get_sysattr_value(dev, name);
1764 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1772 /* remove trailing whitespace, if not asked to match for it */
1773 len = strlen(value);
1774 if (len > 0 && isspace(value[len-1])) {
1775 const char *key_value;
1778 key_value = rules_str(rules, cur->key.value_off);
1779 klen = strlen(key_value);
1780 if (klen > 0 && !isspace(key_value[klen-1])) {
1781 if (value != vbuf) {
1782 strscpy(vbuf, sizeof(vbuf), value);
1785 while (len > 0 && isspace(vbuf[--len]))
1790 return match_key(rules, cur, value);
1799 int udev_rules_apply_to_event(struct udev_rules *rules,
1800 struct udev_event *event,
1801 usec_t timeout_usec,
1802 const sigset_t *sigmask) {
1805 enum escape_type esc = ESCAPE_UNSET;
1808 if (rules->tokens == NULL)
1811 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1812 (major(udev_device_get_devnum(event->dev)) > 0 ||
1813 udev_device_get_ifindex(event->dev) > 0));
1815 /* loop through token list, match, run actions or forward to next rule */
1816 cur = &rules->tokens[0];
1819 dump_token(rules, cur);
1820 switch (cur->type) {
1824 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1825 if (!can_set_name && rule->rule.can_set_name)
1830 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1834 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1838 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1841 case TK_M_DEVLINK: {
1842 struct udev_list_entry *list_entry;
1845 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1846 const char *devlink;
1848 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1849 if (match_key(rules, cur, devlink) == 0) {
1859 if (match_key(rules, cur, event->name) != 0)
1863 const char *key_name = rules_str(rules, cur->key.attr_off);
1866 value = udev_device_get_property_value(event->dev, key_name);
1869 if (match_key(rules, cur, value))
1874 struct udev_list_entry *list_entry;
1877 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1878 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1883 if (!match && (cur->key.op != OP_NOMATCH))
1887 case TK_M_SUBSYSTEM:
1888 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1892 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1895 case TK_M_WAITFOR: {
1896 char filename[UTIL_PATH_SIZE];
1899 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1900 found = (wait_for_file(event->dev, filename, 10) == 0);
1901 if (!found && (cur->key.op != OP_NOMATCH))
1906 if (match_attr(rules, event->dev, event, cur) != 0)
1910 case TK_M_SUBSYSTEMS:
1916 /* get whole sequence of parent matches */
1918 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1921 /* loop over parents */
1922 event->dev_parent = event->dev;
1926 /* loop over sequence of parent match keys */
1927 for (key = cur; key < next; key++ ) {
1928 dump_token(rules, key);
1931 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1934 case TK_M_SUBSYSTEMS:
1935 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1939 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1943 if (match_attr(rules, event->dev_parent, event, key) != 0)
1947 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
1949 if (match && key->key.op == OP_NOMATCH)
1951 if (!match && key->key.op == OP_MATCH)
1962 event->dev_parent = udev_device_get_parent(event->dev_parent);
1963 if (event->dev_parent == NULL)
1966 /* move behind our sequence of parent match keys */
1971 char filename[UTIL_PATH_SIZE];
1972 struct stat statbuf;
1975 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
1976 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
1977 if (filename[0] != '/') {
1978 char tmp[UTIL_PATH_SIZE];
1980 strscpy(tmp, sizeof(tmp), filename);
1981 strscpyl(filename, sizeof(filename),
1982 udev_device_get_syspath(event->dev), "/", tmp, NULL);
1985 attr_subst_subdir(filename, sizeof(filename));
1987 match = (stat(filename, &statbuf) == 0);
1988 if (match && cur->key.mode > 0)
1989 match = ((statbuf.st_mode & cur->key.mode) > 0);
1990 if (match && cur->key.op == OP_NOMATCH)
1992 if (!match && cur->key.op == OP_MATCH)
1996 case TK_M_PROGRAM: {
1997 char program[UTIL_PATH_SIZE];
1999 char result[UTIL_LINE_SIZE];
2001 free(event->program_result);
2002 event->program_result = NULL;
2003 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2004 envp = udev_device_get_properties_envp(event->dev);
2005 log_debug("PROGRAM '%s' %s:%u",
2007 rules_str(rules, rule->rule.filename_off),
2008 rule->rule.filename_line);
2010 if (udev_event_spawn(event, timeout_usec, program, envp, sigmask, result, sizeof(result)) < 0) {
2011 if (cur->key.op != OP_NOMATCH)
2016 util_remove_trailing_chars(result, '\n');
2017 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2018 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2020 log_debug("%i character(s) replaced" , count);
2022 event->program_result = strdup(result);
2023 if (cur->key.op == OP_NOMATCH)
2028 case TK_M_IMPORT_FILE: {
2029 char import[UTIL_PATH_SIZE];
2031 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2032 if (import_file_into_properties(event->dev, import) != 0)
2033 if (cur->key.op != OP_NOMATCH)
2037 case TK_M_IMPORT_PROG: {
2038 char import[UTIL_PATH_SIZE];
2040 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2041 log_debug("IMPORT '%s' %s:%u",
2043 rules_str(rules, rule->rule.filename_off),
2044 rule->rule.filename_line);
2046 if (import_program_into_properties(event, timeout_usec, import, sigmask) != 0)
2047 if (cur->key.op != OP_NOMATCH)
2051 case TK_M_IMPORT_BUILTIN: {
2052 char command[UTIL_PATH_SIZE];
2054 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2055 /* check if we ran already */
2056 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2057 log_debug("IMPORT builtin skip '%s' %s:%u",
2058 udev_builtin_name(cur->key.builtin_cmd),
2059 rules_str(rules, rule->rule.filename_off),
2060 rule->rule.filename_line);
2061 /* return the result from earlier run */
2062 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2063 if (cur->key.op != OP_NOMATCH)
2068 event->builtin_run |= (1 << cur->key.builtin_cmd);
2071 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2072 log_debug("IMPORT builtin '%s' %s:%u",
2073 udev_builtin_name(cur->key.builtin_cmd),
2074 rules_str(rules, rule->rule.filename_off),
2075 rule->rule.filename_line);
2077 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2078 /* remember failure */
2079 log_debug("IMPORT builtin '%s' returned non-zero",
2080 udev_builtin_name(cur->key.builtin_cmd));
2081 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2082 if (cur->key.op != OP_NOMATCH)
2087 case TK_M_IMPORT_DB: {
2088 const char *key = rules_str(rules, cur->key.value_off);
2091 value = udev_device_get_property_value(event->dev_db, key);
2092 if (value != NULL) {
2093 struct udev_list_entry *entry;
2095 entry = udev_device_add_property(event->dev, key, value);
2096 udev_list_entry_set_num(entry, true);
2098 if (cur->key.op != OP_NOMATCH)
2103 case TK_M_IMPORT_CMDLINE: {
2105 bool imported = false;
2107 f = fopen("/proc/cmdline", "re");
2111 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2112 const char *key = rules_str(rules, cur->key.value_off);
2115 pos = strstr(cmdline, key);
2117 struct udev_list_entry *entry;
2120 if (pos[0] == '\0' || isspace(pos[0])) {
2121 /* we import simple flags as 'FLAG=1' */
2122 entry = udev_device_add_property(event->dev, key, "1");
2123 udev_list_entry_set_num(entry, true);
2125 } else if (pos[0] == '=') {
2130 while (pos[0] != '\0' && !isspace(pos[0]))
2133 entry = udev_device_add_property(event->dev, key, value);
2134 udev_list_entry_set_num(entry, true);
2141 if (!imported && cur->key.op != OP_NOMATCH)
2145 case TK_M_IMPORT_PARENT: {
2146 char import[UTIL_PATH_SIZE];
2148 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2149 if (import_parent_into_properties(event->dev, import) != 0)
2150 if (cur->key.op != OP_NOMATCH)
2155 if (match_key(rules, cur, event->program_result) != 0)
2158 case TK_A_STRING_ESCAPE_NONE:
2161 case TK_A_STRING_ESCAPE_REPLACE:
2162 esc = ESCAPE_REPLACE;
2164 case TK_A_DB_PERSIST:
2165 udev_device_set_db_persist(event->dev);
2167 case TK_A_INOTIFY_WATCH:
2168 if (event->inotify_watch_final)
2170 if (cur->key.op == OP_ASSIGN_FINAL)
2171 event->inotify_watch_final = true;
2172 event->inotify_watch = cur->key.watch;
2174 case TK_A_DEVLINK_PRIO:
2175 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2178 char owner[UTIL_NAME_SIZE];
2181 if (event->owner_final)
2183 if (cur->key.op == OP_ASSIGN_FINAL)
2184 event->owner_final = true;
2185 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2186 event->owner_set = true;
2187 r = get_user_creds(&owner, &event->uid, NULL, NULL, NULL);
2190 log_debug("OWNER %u %s:%u",
2192 rules_str(rules, rule->rule.filename_off),
2193 rule->rule.filename_line);
2197 char group[UTIL_NAME_SIZE];
2200 if (event->group_final)
2202 if (cur->key.op == OP_ASSIGN_FINAL)
2203 event->group_final = true;
2204 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2205 event->group_set = true;
2206 r = get_group_creds(&group, &event->gid);
2209 log_debug("GROUP %u %s:%u",
2211 rules_str(rules, rule->rule.filename_off),
2212 rule->rule.filename_line);
2216 char mode_str[UTIL_NAME_SIZE];
2220 if (event->mode_final)
2222 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2223 mode = strtol(mode_str, &endptr, 8);
2224 if (endptr[0] != '\0') {
2225 log_error("ignoring invalid mode '%s'", mode_str);
2228 if (cur->key.op == OP_ASSIGN_FINAL)
2229 event->mode_final = true;
2230 event->mode_set = true;
2232 log_debug("MODE %#o %s:%u",
2234 rules_str(rules, rule->rule.filename_off),
2235 rule->rule.filename_line);
2239 if (event->owner_final)
2241 if (cur->key.op == OP_ASSIGN_FINAL)
2242 event->owner_final = true;
2243 event->owner_set = true;
2244 event->uid = cur->key.uid;
2245 log_debug("OWNER %u %s:%u",
2247 rules_str(rules, rule->rule.filename_off),
2248 rule->rule.filename_line);
2251 if (event->group_final)
2253 if (cur->key.op == OP_ASSIGN_FINAL)
2254 event->group_final = true;
2255 event->group_set = true;
2256 event->gid = cur->key.gid;
2257 log_debug("GROUP %u %s:%u",
2259 rules_str(rules, rule->rule.filename_off),
2260 rule->rule.filename_line);
2263 if (event->mode_final)
2265 if (cur->key.op == OP_ASSIGN_FINAL)
2266 event->mode_final = true;
2267 event->mode_set = true;
2268 event->mode = cur->key.mode;
2269 log_debug("MODE %#o %s:%u",
2271 rules_str(rules, rule->rule.filename_off),
2272 rule->rule.filename_line);
2274 case TK_A_SECLABEL: {
2275 const char *name, *label;
2277 name = rules_str(rules, cur->key.attr_off);
2278 label = rules_str(rules, cur->key.value_off);
2279 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2280 udev_list_cleanup(&event->seclabel_list);
2281 udev_list_entry_add(&event->seclabel_list, name, label);
2282 log_debug("SECLABEL{%s}='%s' %s:%u",
2284 rules_str(rules, rule->rule.filename_off),
2285 rule->rule.filename_line);
2289 const char *name = rules_str(rules, cur->key.attr_off);
2290 char *value = rules_str(rules, cur->key.value_off);
2291 char value_new[UTIL_NAME_SIZE];
2292 const char *value_old = NULL;
2293 struct udev_list_entry *entry;
2295 if (value[0] == '\0') {
2296 if (cur->key.op == OP_ADD)
2298 udev_device_add_property(event->dev, name, NULL);
2302 if (cur->key.op == OP_ADD)
2303 value_old = udev_device_get_property_value(event->dev, name);
2305 char temp[UTIL_NAME_SIZE];
2307 /* append value separated by space */
2308 udev_event_apply_format(event, value, temp, sizeof(temp));
2309 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2311 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2313 entry = udev_device_add_property(event->dev, name, value_new);
2314 /* store in db, skip private keys */
2316 udev_list_entry_set_num(entry, true);
2320 char tag[UTIL_PATH_SIZE];
2323 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2324 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2325 udev_device_cleanup_tags_list(event->dev);
2326 for (p = tag; *p != '\0'; p++) {
2327 if ((*p >= 'a' && *p <= 'z') ||
2328 (*p >= 'A' && *p <= 'Z') ||
2329 (*p >= '0' && *p <= '9') ||
2330 *p == '-' || *p == '_')
2332 log_error("ignoring invalid tag name '%s'", tag);
2335 udev_device_add_tag(event->dev, tag);
2339 const char *name = rules_str(rules, cur->key.value_off);
2341 char name_str[UTIL_PATH_SIZE];
2344 if (event->name_final)
2346 if (cur->key.op == OP_ASSIGN_FINAL)
2347 event->name_final = true;
2348 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2349 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2350 count = util_replace_chars(name_str, "/");
2352 log_debug("%i character(s) replaced", count);
2354 if (major(udev_device_get_devnum(event->dev)) &&
2355 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
2356 log_error("NAME=\"%s\" ignored, kernel device nodes "
2357 "can not be renamed; please fix it in %s:%u\n", name,
2358 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2362 event->name = strdup(name_str);
2363 log_debug("NAME '%s' %s:%u",
2365 rules_str(rules, rule->rule.filename_off),
2366 rule->rule.filename_line);
2369 case TK_A_DEVLINK: {
2370 char temp[UTIL_PATH_SIZE];
2371 char filename[UTIL_PATH_SIZE];
2375 if (event->devlink_final)
2377 if (major(udev_device_get_devnum(event->dev)) == 0)
2379 if (cur->key.op == OP_ASSIGN_FINAL)
2380 event->devlink_final = true;
2381 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2382 udev_device_cleanup_devlinks_list(event->dev);
2384 /* allow multiple symlinks separated by spaces */
2385 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2386 if (esc == ESCAPE_UNSET)
2387 count = util_replace_chars(temp, "/ ");
2388 else if (esc == ESCAPE_REPLACE)
2389 count = util_replace_chars(temp, "/");
2391 log_debug("%i character(s) replaced" , count);
2393 while (isspace(pos[0]))
2395 next = strchr(pos, ' ');
2396 while (next != NULL) {
2398 log_debug("LINK '%s' %s:%u", pos,
2399 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2400 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2401 udev_device_add_devlink(event->dev, filename);
2402 while (isspace(next[1]))
2405 next = strchr(pos, ' ');
2407 if (pos[0] != '\0') {
2408 log_debug("LINK '%s' %s:%u", pos,
2409 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2410 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2411 udev_device_add_devlink(event->dev, filename);
2416 const char *key_name = rules_str(rules, cur->key.attr_off);
2417 char attr[UTIL_PATH_SIZE];
2418 char value[UTIL_NAME_SIZE];
2421 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2422 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2423 attr_subst_subdir(attr, sizeof(attr));
2425 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2426 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
2427 rules_str(rules, rule->rule.filename_off),
2428 rule->rule.filename_line);
2429 f = fopen(attr, "we");
2431 if (fprintf(f, "%s", value) <= 0)
2432 log_error("error writing ATTR{%s}: %m", attr);
2435 log_error("error opening ATTR{%s} for writing: %m", attr);
2439 case TK_A_RUN_BUILTIN:
2440 case TK_A_RUN_PROGRAM: {
2441 struct udev_list_entry *entry;
2443 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2444 udev_list_cleanup(&event->run_list);
2445 log_debug("RUN '%s' %s:%u",
2446 rules_str(rules, cur->key.value_off),
2447 rules_str(rules, rule->rule.filename_off),
2448 rule->rule.filename_line);
2449 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2450 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2454 if (cur->key.rule_goto == 0)
2456 cur = &rules->tokens[cur->key.rule_goto];
2461 case TK_M_PARENTS_MIN:
2462 case TK_M_PARENTS_MAX:
2465 log_error("wrong type %u", cur->type);
2472 /* fast-forward to next rule */
2473 cur = rule + rule->rule.token_count;
2477 int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
2483 _cleanup_strv_free_ char **tags = NULL;
2486 _cleanup_free_ char *path = NULL;
2489 if (rules->tokens == NULL)
2492 cur = &rules->tokens[0];
2495 switch (cur->type) {
2500 /* skip rules without a static_node tag */
2501 if (!rule->rule.has_static_node)
2517 mode = cur->key.mode;
2520 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2525 case TK_A_STATIC_NODE: {
2526 char device_node[UTIL_PATH_SIZE];
2527 char tags_dir[UTIL_PATH_SIZE];
2528 char tag_symlink[UTIL_PATH_SIZE];
2531 /* we assure, that the permissions tokens are sorted before the static token */
2533 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2536 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2537 if (stat(device_node, &stats) != 0)
2539 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2542 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2544 STRV_FOREACH(t, tags) {
2545 _cleanup_free_ char *unescaped_filename = NULL;
2547 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2548 r = mkdir_p(tags_dir, 0755);
2550 log_error("failed to create %s: %s", tags_dir, strerror(-r));
2554 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2556 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2557 r = symlink(device_node, tag_symlink);
2558 if (r < 0 && errno != EEXIST) {
2559 log_error("failed to create symlink %s -> %s: %m", tag_symlink, device_node);
2566 /* don't touch the permissions if only the tags were set */
2567 if (mode == 0 && uid == 0 && gid == 0)
2576 if (mode != (stats.st_mode & 01777)) {
2577 r = chmod(device_node, mode);
2579 log_error("failed to chmod '%s' %#o", device_node, mode);
2582 log_debug("chmod '%s' %#o", device_node, mode);
2585 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2586 r = chown(device_node, uid, gid);
2588 log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
2591 log_debug("chown '%s' %u %u", device_node, uid, gid);
2594 utimensat(AT_FDCWD, device_node, NULL, 0);
2604 /* fast-forward to next rule */
2605 cur = rule + rule->rule.token_count;
2612 fchmod(fileno(f), 0644);
2613 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2615 unlink("/run/udev/static_node-tags");