2 * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #define PREALLOC_TOKEN 2048
36 #define PREALLOC_STRBUF 32 * 1024
37 #define PREALLOC_TRIE 256
40 unsigned int name_off;
48 /* this node's first child */
49 unsigned int child_idx;
50 /* the next child of our parent node's child list */
51 unsigned int next_child_idx;
52 /* this node's last child (shortcut for append) */
53 unsigned int last_child_idx;
54 unsigned int value_off;
55 unsigned short value_len;
63 /* every key in the rules file becomes a token */
65 unsigned int token_cur;
66 unsigned int token_max;
68 /* all key strings are copied to a single string buffer */
72 unsigned int buf_count;
74 /* during rule parsing, strings are indexed to find duplicates */
75 struct trie_node *trie_nodes;
76 unsigned int trie_nodes_cur;
77 unsigned int trie_nodes_max;
79 /* during rule parsing, uid/gid lookup results are cached */
81 unsigned int uids_cur;
82 unsigned int uids_max;
84 unsigned int gids_cur;
85 unsigned int gids_max;
88 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
101 enum string_glob_type {
103 GL_PLAIN, /* no special chars */
104 GL_GLOB, /* shell globs ?,*,[] */
105 GL_SPLIT, /* multi-value A|B */
106 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
107 GL_SOMETHING, /* commonly used "?*" */
110 enum string_subst_type {
117 /* tokens of a rule are sorted/handled in this order */
122 TK_M_ACTION, /* val */
123 TK_M_DEVPATH, /* val */
124 TK_M_KERNEL, /* val */
125 TK_M_DEVLINK, /* val */
127 TK_M_ENV, /* val, attr */
129 TK_M_SUBSYSTEM, /* val */
130 TK_M_DRIVER, /* val */
131 TK_M_WAITFOR, /* val */
132 TK_M_ATTR, /* val, attr */
135 TK_M_KERNELS, /* val */
136 TK_M_SUBSYSTEMS, /* val */
137 TK_M_DRIVERS, /* val */
138 TK_M_ATTRS, /* val, attr */
142 TK_M_TEST, /* val, mode_t */
143 TK_M_EVENT_TIMEOUT, /* int */
144 TK_M_PROGRAM, /* val */
145 TK_M_IMPORT_FILE, /* val */
146 TK_M_IMPORT_PROG, /* val */
147 TK_M_IMPORT_BUILTIN, /* val */
148 TK_M_IMPORT_DB, /* val */
149 TK_M_IMPORT_CMDLINE, /* val */
150 TK_M_IMPORT_PARENT, /* val */
151 TK_M_RESULT, /* val */
154 TK_A_STRING_ESCAPE_NONE,
155 TK_A_STRING_ESCAPE_REPLACE,
157 TK_A_INOTIFY_WATCH, /* int */
158 TK_A_DEVLINK_PRIO, /* int */
159 TK_A_OWNER, /* val */
160 TK_A_GROUP, /* val */
162 TK_A_OWNER_ID, /* uid_t */
163 TK_A_GROUP_ID, /* gid_t */
164 TK_A_MODE_ID, /* mode_t */
165 TK_A_STATIC_NODE, /* val */
166 TK_A_ENV, /* val, attr */
169 TK_A_DEVLINK, /* val */
170 TK_A_ATTR, /* val, attr */
171 TK_A_RUN, /* val, bool */
172 TK_A_GOTO, /* size_t */
177 /* we try to pack stuff in a way that we take only 12 bytes per token */
180 unsigned char type; /* same in rule and key */
182 enum token_type type:8;
184 bool has_static_node:1;
185 unsigned int unused:6;
186 unsigned short token_count;
187 unsigned int label_off;
188 unsigned short filename_off;
189 unsigned short filename_line;
192 enum token_type type:8;
193 enum operation_type op:8;
194 enum string_glob_type glob:8;
195 enum string_subst_type subst:4;
196 enum string_subst_type attrsubst:4;
197 unsigned int value_off;
199 unsigned int attr_off;
201 unsigned int rule_goto;
208 enum udev_builtin_cmd builtin_cmd;
216 struct udev_rules *rules;
218 struct token token[MAX_TK];
219 unsigned int token_cur;
223 static const char *operation_str(enum operation_type type)
225 static const char *operation_strs[] = {
226 [OP_UNSET] = "UNSET",
227 [OP_MATCH] = "match",
228 [OP_NOMATCH] = "nomatch",
229 [OP_MATCH_MAX] = "MATCH_MAX",
232 [OP_ASSIGN] = "assign",
233 [OP_ASSIGN_FINAL] = "assign-final",
236 return operation_strs[type];
239 static const char *string_glob_str(enum string_glob_type type)
241 static const char *string_glob_strs[] = {
242 [GL_UNSET] = "UNSET",
243 [GL_PLAIN] = "plain",
245 [GL_SPLIT] = "split",
246 [GL_SPLIT_GLOB] = "split-glob",
247 [GL_SOMETHING] = "split-glob",
250 return string_glob_strs[type];
253 static const char *token_str(enum token_type type)
255 static const char *token_strs[] = {
256 [TK_UNSET] = "UNSET",
259 [TK_M_ACTION] = "M ACTION",
260 [TK_M_DEVPATH] = "M DEVPATH",
261 [TK_M_KERNEL] = "M KERNEL",
262 [TK_M_DEVLINK] = "M DEVLINK",
263 [TK_M_NAME] = "M NAME",
264 [TK_M_ENV] = "M ENV",
265 [TK_M_TAG] = "M TAG",
266 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
267 [TK_M_DRIVER] = "M DRIVER",
268 [TK_M_WAITFOR] = "M WAITFOR",
269 [TK_M_ATTR] = "M ATTR",
271 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
272 [TK_M_KERNELS] = "M KERNELS",
273 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
274 [TK_M_DRIVERS] = "M DRIVERS",
275 [TK_M_ATTRS] = "M ATTRS",
276 [TK_M_TAGS] = "M TAGS",
277 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
279 [TK_M_TEST] = "M TEST",
280 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
281 [TK_M_PROGRAM] = "M PROGRAM",
282 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
283 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
284 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
285 [TK_M_IMPORT_DB] = "M IMPORT_DB",
286 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
287 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
288 [TK_M_RESULT] = "M RESULT",
289 [TK_M_MAX] = "M MAX",
291 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
292 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
293 [TK_A_DB_PERSIST] = "A DB_PERSIST",
294 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
295 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
296 [TK_A_OWNER] = "A OWNER",
297 [TK_A_GROUP] = "A GROUP",
298 [TK_A_MODE] = "A MODE",
299 [TK_A_OWNER_ID] = "A OWNER_ID",
300 [TK_A_GROUP_ID] = "A GROUP_ID",
301 [TK_A_STATIC_NODE] = "A STATIC_NODE",
302 [TK_A_MODE_ID] = "A MODE_ID",
303 [TK_A_ENV] = "A ENV",
304 [TK_A_TAG] = "A ENV",
305 [TK_A_NAME] = "A NAME",
306 [TK_A_DEVLINK] = "A DEVLINK",
307 [TK_A_ATTR] = "A ATTR",
308 [TK_A_RUN] = "A RUN",
309 [TK_A_GOTO] = "A GOTO",
314 return token_strs[type];
317 static void dump_token(struct udev_rules *rules, struct token *token)
319 enum token_type type = token->type;
320 enum operation_type op = token->key.op;
321 enum string_glob_type glob = token->key.glob;
322 const char *value = &rules->buf[token->key.value_off];
323 const char *attr = &rules->buf[token->key.attr_off];
328 const char *tks_ptr = (char *)rules->tokens;
329 const char *tk_ptr = (char *)token;
330 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
332 dbg(rules->udev, "* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
333 &rules->buf[token->rule.filename_off], token->rule.filename_line,
334 idx, token->rule.token_count,
335 &rules->buf[token->rule.label_off]);
347 case TK_M_SUBSYSTEMS:
351 case TK_M_IMPORT_FILE:
352 case TK_M_IMPORT_PROG:
354 case TK_M_IMPORT_CMDLINE:
355 case TK_M_IMPORT_PARENT:
363 dbg(rules->udev, "%s %s '%s'(%s)\n",
364 token_str(type), operation_str(op), value, string_glob_str(glob));
366 case TK_M_IMPORT_BUILTIN:
367 dbg(rules->udev, "%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
374 dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
375 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
379 dbg(rules->udev, "%s %s '%s'\n", token_str(type), operation_str(op), value);
381 case TK_A_STRING_ESCAPE_NONE:
382 case TK_A_STRING_ESCAPE_REPLACE:
383 case TK_A_DB_PERSIST:
384 dbg(rules->udev, "%s\n", token_str(type));
387 dbg(rules->udev, "%s %s '%s'(%s) %#o\n",
388 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
390 case TK_A_INOTIFY_WATCH:
391 dbg(rules->udev, "%s %u\n", token_str(type), token->key.watch);
393 case TK_A_DEVLINK_PRIO:
394 dbg(rules->udev, "%s %u\n", token_str(type), token->key.devlink_prio);
397 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
400 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
403 dbg(rules->udev, "%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
405 case TK_A_STATIC_NODE:
406 dbg(rules->udev, "%s '%s'\n", token_str(type), value);
408 case TK_M_EVENT_TIMEOUT:
409 dbg(rules->udev, "%s %u\n", token_str(type), token->key.event_timeout);
412 dbg(rules->udev, "%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
415 dbg(rules->udev, "* %s\n", token_str(type));
417 case TK_M_PARENTS_MIN:
418 case TK_M_PARENTS_MAX:
421 dbg(rules->udev, "unknown type %u\n", type);
426 static void dump_rules(struct udev_rules *rules)
430 dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
432 rules->token_cur * sizeof(struct token),
435 for(i = 0; i < rules->token_cur; i++)
436 dump_token(rules, &rules->tokens[i]);
439 static inline const char *operation_str(enum operation_type type) { return NULL; }
440 static inline const char *token_str(enum token_type type) { return NULL; }
441 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
442 static inline void dump_rules(struct udev_rules *rules) {}
443 #endif /* ENABLE_DEBUG */
445 static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
449 /* grow buffer if needed */
450 if (rules->buf_cur + bytes+1 >= rules->buf_max) {
454 /* double the buffer size */
455 add = rules->buf_max;
459 buf = realloc(rules->buf, rules->buf_max + add);
462 dbg(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add);
464 rules->buf_max += add;
466 off = rules->buf_cur;
467 memcpy(&rules->buf[rules->buf_cur], str, bytes);
468 rules->buf_cur += bytes;
473 static int add_string(struct udev_rules *rules, const char *str)
475 unsigned int node_idx;
476 struct trie_node *new_node;
477 unsigned int new_node_idx;
482 struct trie_node *parent;
484 /* walk trie, start from last character of str to find matching tails */
488 for (depth = 0; depth <= len; depth++) {
489 struct trie_node *node;
490 unsigned int child_idx;
492 node = &rules->trie_nodes[node_idx];
493 off = node->value_off + node->value_len - len;
495 /* match against current node */
496 if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0))
499 /* lookup child node */
500 key = str[len - 1 - depth];
501 child_idx = node->child_idx;
502 while (child_idx > 0) {
503 struct trie_node *child;
505 child = &rules->trie_nodes[child_idx];
506 if (child->key == key)
508 child_idx = child->next_child_idx;
512 node_idx = child_idx;
515 /* string not found, add it */
516 off = add_new_string(rules, str, len + 1);
518 /* grow trie nodes if needed */
519 if (rules->trie_nodes_cur >= rules->trie_nodes_max) {
520 struct trie_node *nodes;
523 /* double the buffer size */
524 add = rules->trie_nodes_max;
528 nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node));
531 dbg(rules->udev, "extend trie nodes from %u to %u\n",
532 rules->trie_nodes_max, rules->trie_nodes_max + add);
533 rules->trie_nodes = nodes;
534 rules->trie_nodes_max += add;
538 new_node_idx = rules->trie_nodes_cur;
539 rules->trie_nodes_cur++;
540 new_node = &rules->trie_nodes[new_node_idx];
541 memset(new_node, 0x00, sizeof(struct trie_node));
542 new_node->value_off = off;
543 new_node->value_len = len;
546 /* join the parent's child list */
547 parent = &rules->trie_nodes[node_idx];
548 if (parent->child_idx == 0) {
549 parent->child_idx = new_node_idx;
551 struct trie_node *last_child;
553 last_child = &rules->trie_nodes[parent->last_child_idx];
554 last_child->next_child_idx = new_node_idx;
556 parent->last_child_idx = new_node_idx;
560 static int add_token(struct udev_rules *rules, struct token *token)
562 /* grow buffer if needed */
563 if (rules->token_cur+1 >= rules->token_max) {
564 struct token *tokens;
567 /* double the buffer size */
568 add = rules->token_max;
572 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
575 dbg(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add);
576 rules->tokens = tokens;
577 rules->token_max += add;
579 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
584 static uid_t add_uid(struct udev_rules *rules, const char *owner)
590 /* lookup, if we know it already */
591 for (i = 0; i < rules->uids_cur; i++) {
592 off = rules->uids[i].name_off;
593 if (strcmp(&rules->buf[off], owner) == 0) {
594 uid = rules->uids[i].uid;
595 dbg(rules->udev, "return existing %u for '%s'\n", uid, owner);
599 uid = util_lookup_user(rules->udev, owner);
601 /* grow buffer if needed */
602 if (rules->uids_cur+1 >= rules->uids_max) {
603 struct uid_gid *uids;
606 /* double the buffer size */
607 add = rules->uids_max;
611 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
614 dbg(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add);
616 rules->uids_max += add;
618 rules->uids[rules->uids_cur].uid = uid;
619 off = add_string(rules, owner);
622 rules->uids[rules->uids_cur].name_off = off;
627 static gid_t add_gid(struct udev_rules *rules, const char *group)
633 /* lookup, if we know it already */
634 for (i = 0; i < rules->gids_cur; i++) {
635 off = rules->gids[i].name_off;
636 if (strcmp(&rules->buf[off], group) == 0) {
637 gid = rules->gids[i].gid;
638 dbg(rules->udev, "return existing %u for '%s'\n", gid, group);
642 gid = util_lookup_group(rules->udev, group);
644 /* grow buffer if needed */
645 if (rules->gids_cur+1 >= rules->gids_max) {
646 struct uid_gid *gids;
649 /* double the buffer size */
650 add = rules->gids_max;
654 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
657 dbg(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add);
659 rules->gids_max += add;
661 rules->gids[rules->gids_cur].gid = gid;
662 off = add_string(rules, group);
665 rules->gids[rules->gids_cur].name_off = off;
670 static int import_property_from_string(struct udev_device *dev, char *line)
672 struct udev *udev = udev_device_get_udev(dev);
679 while (isspace(key[0]))
682 /* comment or empty line */
683 if (key[0] == '#' || key[0] == '\0')
686 /* split key/value */
687 val = strchr(key, '=');
694 while (isspace(val[0]))
701 while (isspace(key[len-1]))
705 /* terminate value */
709 while (isspace(val[len-1]))
717 if (val[0] == '"' || val[0] == '\'') {
718 if (val[len-1] != val[0]) {
719 info(udev, "inconsistent quoting: '%s', skip\n", line);
726 dbg(udev, "adding '%s'='%s'\n", key, val);
728 /* handle device, renamed by external tool, returning new path */
729 if (strcmp(key, "DEVPATH") == 0) {
730 char syspath[UTIL_PATH_SIZE];
732 info(udev, "updating devpath from '%s' to '%s'\n",
733 udev_device_get_devpath(dev), val);
734 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), val, NULL);
735 udev_device_set_syspath(dev, syspath);
737 struct udev_list_entry *entry;
739 entry = udev_device_add_property(dev, key, val);
740 /* store in db, skip private keys */
742 udev_list_entry_set_num(entry, true);
747 static int import_file_into_properties(struct udev_device *dev, const char *filename)
750 char line[UTIL_LINE_SIZE];
752 f = fopen(filename, "r");
755 while (fgets(line, sizeof(line), f) != NULL)
756 import_property_from_string(dev, line);
761 static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
763 struct udev_device *dev = event->dev;
765 char result[UTIL_LINE_SIZE];
769 envp = udev_device_get_properties_envp(dev);
770 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
775 while (line != NULL) {
778 pos = strchr(line, '\n');
783 import_property_from_string(dev, line);
789 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
791 struct udev *udev = udev_device_get_udev(dev);
792 struct udev_device *dev_parent;
793 struct udev_list_entry *list_entry;
795 dev_parent = udev_device_get_parent(dev);
796 if (dev_parent == NULL)
799 dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent));
800 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
801 const char *key = udev_list_entry_get_name(list_entry);
802 const char *val = udev_list_entry_get_value(list_entry);
804 if (fnmatch(filter, key, 0) == 0) {
805 struct udev_list_entry *entry;
807 dbg(udev, "import key '%s=%s'\n", key, val);
808 entry = udev_device_add_property(dev, key, val);
809 /* store in db, skip private keys */
811 udev_list_entry_set_num(entry, true);
817 #define WAIT_LOOP_PER_SECOND 50
818 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
820 struct udev *udev = udev_device_get_udev(dev);
821 char filepath[UTIL_PATH_SIZE];
822 char devicepath[UTIL_PATH_SIZE];
824 int loop = timeout * WAIT_LOOP_PER_SECOND;
826 /* a relative path is a device attribute */
827 devicepath[0] = '\0';
828 if (file[0] != '/') {
829 util_strscpyl(devicepath, sizeof(devicepath),
830 udev_get_sys_path(udev), udev_device_get_devpath(dev), NULL);
831 util_strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
835 dbg(udev, "will wait %i sec for '%s'\n", timeout, file);
837 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
840 if (stat(file, &stats) == 0) {
841 info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
844 /* make sure, the device did not disappear in the meantime */
845 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
846 info(udev, "device disappeared while waiting for '%s'\n", file);
849 info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
850 nanosleep(&duration, NULL);
852 info(udev, "waiting for '%s' failed\n", file);
856 static int attr_subst_subdir(char *attr, size_t len)
860 if (strstr(attr, "/*/")) {
862 char dirname[UTIL_PATH_SIZE];
866 util_strscpy(dirname, sizeof(dirname), attr);
867 pos = strstr(dirname, "/*/");
872 dir = opendir(dirname);
876 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
879 if (dent->d_name[0] == '.')
881 util_strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
882 if (stat(attr, &stats) == 0) {
894 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
900 if (linepos == NULL || linepos[0] == '\0')
903 /* skip whitespace */
904 while (isspace(linepos[0]) || linepos[0] == ',')
908 if (linepos[0] == '\0')
914 if (linepos[0] == '\0')
916 if (isspace(linepos[0]))
918 if (linepos[0] == '=')
920 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
921 if (linepos[1] == '=')
925 /* remember end of key */
928 /* skip whitespace after key */
929 while (isspace(linepos[0]))
931 if (linepos[0] == '\0')
934 /* get operation type */
935 if (linepos[0] == '=' && linepos[1] == '=') {
938 } else if (linepos[0] == '!' && linepos[1] == '=') {
941 } else if (linepos[0] == '+' && linepos[1] == '=') {
944 } else if (linepos[0] == '=') {
947 } else if (linepos[0] == ':' && linepos[1] == '=') {
948 *op = OP_ASSIGN_FINAL;
956 /* skip whitespace after operator */
957 while (isspace(linepos[0]))
959 if (linepos[0] == '\0')
963 if (linepos[0] == '"')
970 temp = strchr(linepos, '"');
975 dbg(udev, "%s '%s'-'%s'\n", operation_str(*op), *key, *value);
977 /* move line to next key */
982 /* extract possible KEY{attr} */
983 static char *get_key_attribute(struct udev *udev, char *str)
988 attr = strchr(str, '{');
991 pos = strchr(attr, '}');
993 err(udev, "missing closing brace for format\n");
997 dbg(udev, "attribute='%s'\n", attr);
1003 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
1004 enum operation_type op,
1005 const char *value, const void *data)
1007 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
1008 const char *attr = NULL;
1010 memset(token, 0x00, sizeof(struct token));
1016 case TK_M_SUBSYSTEM:
1022 case TK_M_SUBSYSTEMS:
1026 case TK_M_IMPORT_FILE:
1027 case TK_M_IMPORT_PROG:
1028 case TK_M_IMPORT_DB:
1029 case TK_M_IMPORT_CMDLINE:
1030 case TK_M_IMPORT_PARENT:
1039 token->key.value_off = add_string(rule_tmp->rules, value);
1041 case TK_M_IMPORT_BUILTIN:
1042 token->key.value_off = add_string(rule_tmp->rules, value);
1043 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
1051 token->key.value_off = add_string(rule_tmp->rules, value);
1052 token->key.attr_off = add_string(rule_tmp->rules, attr);
1055 token->key.value_off = add_string(rule_tmp->rules, value);
1056 token->key.devlink_unique = *(int *)data;
1059 token->key.value_off = add_string(rule_tmp->rules, value);
1061 token->key.mode = *(mode_t *)data;
1063 case TK_A_STRING_ESCAPE_NONE:
1064 case TK_A_STRING_ESCAPE_REPLACE:
1065 case TK_A_DB_PERSIST:
1068 token->key.value_off = add_string(rule_tmp->rules, value);
1070 case TK_A_INOTIFY_WATCH:
1071 case TK_A_DEVLINK_PRIO:
1072 token->key.devlink_prio = *(int *)data;
1075 token->key.uid = *(uid_t *)data;
1078 token->key.gid = *(gid_t *)data;
1081 token->key.mode = *(mode_t *)data;
1083 case TK_A_STATIC_NODE:
1084 token->key.value_off = add_string(rule_tmp->rules, value);
1086 case TK_M_EVENT_TIMEOUT:
1087 token->key.event_timeout = *(int *)data;
1090 case TK_M_PARENTS_MIN:
1091 case TK_M_PARENTS_MAX:
1095 err(rule_tmp->rules->udev, "wrong type %u\n", type);
1099 if (value != NULL && type < TK_M_MAX) {
1100 /* check if we need to split or call fnmatch() while matching rules */
1101 enum string_glob_type glob;
1105 has_split = (strchr(value, '|') != NULL);
1106 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
1107 if (has_split && has_glob) {
1108 glob = GL_SPLIT_GLOB;
1109 } else if (has_split) {
1111 } else if (has_glob) {
1112 if (strcmp(value, "?*") == 0)
1113 glob = GL_SOMETHING;
1119 token->key.glob = glob;
1122 if (value != NULL && type > TK_M_MAX) {
1123 /* check if assigned value has substitution chars */
1124 if (value[0] == '[')
1125 token->key.subst = SB_SUBSYS;
1126 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
1127 token->key.subst = SB_FORMAT;
1129 token->key.subst = SB_NONE;
1133 /* check if property/attribut name has substitution chars */
1135 token->key.attrsubst = SB_SUBSYS;
1136 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
1137 token->key.attrsubst = SB_FORMAT;
1139 token->key.attrsubst = SB_NONE;
1142 token->key.type = type;
1144 rule_tmp->token_cur++;
1145 if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) {
1146 err(rule_tmp->rules->udev, "temporary rule array too small\n");
1152 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1155 unsigned int start = 0;
1156 unsigned int end = rule_tmp->token_cur;
1158 for (i = 0; i < rule_tmp->token_cur; i++) {
1159 enum token_type next_val = TK_UNSET;
1160 unsigned int next_idx = 0;
1163 /* find smallest value */
1164 for (j = start; j < end; j++) {
1165 if (rule_tmp->token[j].type == TK_UNSET)
1167 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1168 next_val = rule_tmp->token[j].type;
1173 /* add token and mark done */
1174 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1176 rule_tmp->token[next_idx].type = TK_UNSET;
1179 if (next_idx == start)
1181 if (next_idx+1 == end)
1187 static int add_rule(struct udev_rules *rules, char *line,
1188 const char *filename, unsigned int filename_off, unsigned int lineno)
1192 struct rule_tmp rule_tmp;
1194 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1195 rule_tmp.rules = rules;
1196 rule_tmp.rule.type = TK_RULE;
1197 rule_tmp.rule.rule.filename_off = filename_off;
1198 rule_tmp.rule.rule.filename_line = lineno;
1204 enum operation_type op;
1206 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1209 if (strcmp(key, "ACTION") == 0) {
1210 if (op > OP_MATCH_MAX) {
1211 err(rules->udev, "invalid ACTION operation\n");
1214 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1218 if (strcmp(key, "DEVPATH") == 0) {
1219 if (op > OP_MATCH_MAX) {
1220 err(rules->udev, "invalid DEVPATH operation\n");
1223 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1227 if (strcmp(key, "KERNEL") == 0) {
1228 if (op > OP_MATCH_MAX) {
1229 err(rules->udev, "invalid KERNEL operation\n");
1232 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1236 if (strcmp(key, "SUBSYSTEM") == 0) {
1237 if (op > OP_MATCH_MAX) {
1238 err(rules->udev, "invalid SUBSYSTEM operation\n");
1241 /* bus, class, subsystem events should all be the same */
1242 if (strcmp(value, "subsystem") == 0 ||
1243 strcmp(value, "bus") == 0 ||
1244 strcmp(value, "class") == 0) {
1245 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
1246 err(rules->udev, "'%s' must be specified as 'subsystem' \n"
1247 "please fix it in %s:%u", value, filename, lineno);
1248 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1250 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1254 if (strcmp(key, "DRIVER") == 0) {
1255 if (op > OP_MATCH_MAX) {
1256 err(rules->udev, "invalid DRIVER operation\n");
1259 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1263 if (strncmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
1264 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1266 err(rules->udev, "error parsing ATTR attribute\n");
1269 if (op < OP_MATCH_MAX) {
1270 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1272 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1277 if (strcmp(key, "KERNELS") == 0) {
1278 if (op > OP_MATCH_MAX) {
1279 err(rules->udev, "invalid KERNELS operation\n");
1282 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1286 if (strcmp(key, "SUBSYSTEMS") == 0) {
1287 if (op > OP_MATCH_MAX) {
1288 err(rules->udev, "invalid SUBSYSTEMS operation\n");
1291 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1295 if (strcmp(key, "DRIVERS") == 0) {
1296 if (op > OP_MATCH_MAX) {
1297 err(rules->udev, "invalid DRIVERS operation\n");
1300 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1304 if (strncmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0) {
1305 if (op > OP_MATCH_MAX) {
1306 err(rules->udev, "invalid ATTRS operation\n");
1309 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1311 err(rules->udev, "error parsing ATTRS attribute\n");
1314 if (strncmp(attr, "device/", 7) == 0)
1315 err(rules->udev, "the 'device' link may not be available in a future kernel, "
1316 "please fix it in %s:%u", filename, lineno);
1317 else if (strstr(attr, "../") != NULL)
1318 err(rules->udev, "do not reference parent sysfs directories directly, "
1319 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1320 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1324 if (strcmp(key, "TAGS") == 0) {
1325 if (op > OP_MATCH_MAX) {
1326 err(rules->udev, "invalid TAGS operation\n");
1329 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1333 if (strncmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
1334 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1336 err(rules->udev, "error parsing ENV attribute\n");
1339 if (op < OP_MATCH_MAX) {
1340 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1343 static const char *blacklist[] = {
1358 for (i = 0; i < ARRAY_SIZE(blacklist); i++)
1359 if (strcmp(attr, blacklist[i]) == 0) {
1360 err(rules->udev, "invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
1363 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1369 if (strcmp(key, "TAG") == 0) {
1370 if (op < OP_MATCH_MAX)
1371 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1373 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1377 if (strcmp(key, "PROGRAM") == 0) {
1378 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1382 if (strcmp(key, "RESULT") == 0) {
1383 if (op > OP_MATCH_MAX) {
1384 err(rules->udev, "invalid RESULT operation\n");
1387 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1391 if (strncmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
1392 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1394 err(rules->udev, "IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1397 if (strstr(attr, "program")) {
1398 /* find known built-in command */
1399 if (value[0] != '/') {
1400 enum udev_builtin_cmd cmd;
1402 cmd = udev_builtin_lookup(value);
1403 if (cmd < UDEV_BUILTIN_MAX) {
1404 info(rules->udev, "IMPORT found builtin '%s', replacing %s:%u\n",
1405 value, filename, lineno);
1406 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1410 dbg(rules->udev, "IMPORT will be executed\n");
1411 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1412 } else if (strstr(attr, "builtin")) {
1413 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1415 dbg(rules->udev, "IMPORT execute builtin\n");
1416 if (cmd < UDEV_BUILTIN_MAX)
1417 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1419 err(rules->udev, "IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1420 } else if (strstr(attr, "file")) {
1421 dbg(rules->udev, "IMPORT will be included as file\n");
1422 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1423 } else if (strstr(attr, "db")) {
1424 dbg(rules->udev, "IMPORT will include db values\n");
1425 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1426 } else if (strstr(attr, "cmdline")) {
1427 dbg(rules->udev, "IMPORT will include db values\n");
1428 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1429 } else if (strstr(attr, "parent")) {
1430 dbg(rules->udev, "IMPORT will include the parent values\n");
1431 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1436 if (strncmp(key, "TEST", sizeof("TEST")-1) == 0) {
1439 if (op > OP_MATCH_MAX) {
1440 err(rules->udev, "invalid TEST operation\n");
1443 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1445 mode = strtol(attr, NULL, 8);
1446 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1448 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1453 if (strcmp(key, "RUN") == 0) {
1454 if (strncmp(value, "socket:", 7) == 0)
1455 err(rules->udev, "RUN+=\"socket:...\" support will be removed from a future udev release. "
1456 "Please remove it from: %s:%u and use libudev to subscribe to events.\n", filename, lineno);
1457 rule_add_key(&rule_tmp, TK_A_RUN, op, value, NULL);
1461 if (strcmp(key, "WAIT_FOR") == 0 || strcmp(key, "WAIT_FOR_SYSFS") == 0) {
1462 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1466 if (strcmp(key, "LABEL") == 0) {
1467 rule_tmp.rule.rule.label_off = add_string(rules, value);
1471 if (strcmp(key, "GOTO") == 0) {
1472 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1476 if (strncmp(key, "NAME", sizeof("NAME")-1) == 0) {
1477 if (op < OP_MATCH_MAX) {
1478 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1480 if (strcmp(value, "%k") == 0) {
1481 err(rules->udev, "NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1482 "please remove it from %s:%u\n", filename, lineno);
1485 if (value[0] == '\0') {
1486 info(rules->udev, "NAME=\"\" is ignored, because udev will not delete any device nodes, "
1487 "please remove it from %s:%u\n", filename, lineno);
1490 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1492 rule_tmp.rule.rule.can_set_name = true;
1496 if (strncmp(key, "SYMLINK", sizeof("SYMLINK")-1) == 0) {
1497 if (op < OP_MATCH_MAX) {
1498 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1502 attr = get_key_attribute(rules->udev, key + sizeof("SYMLINK")-1);
1503 if (attr != NULL && strstr(attr, "unique") != NULL)
1505 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, &flag);
1507 rule_tmp.rule.rule.can_set_name = true;
1511 if (strcmp(key, "OWNER") == 0) {
1515 uid = strtoul(value, &endptr, 10);
1516 if (endptr[0] == '\0') {
1517 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1518 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1519 uid = add_uid(rules, value);
1520 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1521 } else if (rules->resolve_names >= 0) {
1522 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1524 rule_tmp.rule.rule.can_set_name = true;
1528 if (strcmp(key, "GROUP") == 0) {
1532 gid = strtoul(value, &endptr, 10);
1533 if (endptr[0] == '\0') {
1534 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1535 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1536 gid = add_gid(rules, value);
1537 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1538 } else if (rules->resolve_names >= 0) {
1539 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1541 rule_tmp.rule.rule.can_set_name = true;
1545 if (strcmp(key, "MODE") == 0) {
1549 mode = strtol(value, &endptr, 8);
1550 if (endptr[0] == '\0')
1551 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1553 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1554 rule_tmp.rule.rule.can_set_name = true;
1558 if (strcmp(key, "OPTIONS") == 0) {
1561 pos = strstr(value, "link_priority=");
1563 int prio = atoi(&pos[strlen("link_priority=")]);
1565 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1566 dbg(rules->udev, "link priority=%i\n", prio);
1569 pos = strstr(value, "event_timeout=");
1571 int tout = atoi(&pos[strlen("event_timeout=")]);
1573 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
1574 dbg(rules->udev, "event timeout=%i\n", tout);
1577 pos = strstr(value, "string_escape=");
1579 pos = &pos[strlen("string_escape=")];
1580 if (strncmp(pos, "none", strlen("none")) == 0)
1581 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1582 else if (strncmp(pos, "replace", strlen("replace")) == 0)
1583 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1586 pos = strstr(value, "db_persist");
1588 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1590 pos = strstr(value, "nowatch");
1594 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1595 dbg(rules->udev, "inotify watch of device disabled\n");
1597 pos = strstr(value, "watch");
1601 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1602 dbg(rules->udev, "inotify watch of device requested\n");
1606 pos = strstr(value, "static_node=");
1608 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1609 rule_tmp.rule.rule.has_static_node = true;
1615 err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno);
1619 /* add rule token */
1620 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1621 if (add_token(rules, &rule_tmp.rule) != 0)
1624 /* add tokens to list, sorted by type */
1625 if (sort_token(rules, &rule_tmp) != 0)
1630 err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
1634 static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off)
1637 unsigned int first_token;
1638 char line[UTIL_LINE_SIZE];
1642 info(rules->udev, "reading '%s' as rules file\n", filename);
1644 f = fopen(filename, "r");
1648 first_token = rules->token_cur;
1650 while (fgets(line, sizeof(line), f) != NULL) {
1654 /* skip whitespace */
1657 while (isspace(key[0]))
1668 /* continue reading if backslash+newline is found */
1669 while (line[len-2] == '\\') {
1670 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1672 if (strlen(&line[len-2]) < 2)
1678 if (len+1 >= sizeof(line)) {
1679 err(rules->udev, "line too long '%s':%u, ignored\n", filename, line_nr);
1682 add_rule(rules, key, filename, filename_off, line_nr);
1686 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1687 for (i = first_token+1; i < rules->token_cur; i++) {
1688 if (rules->tokens[i].type == TK_A_GOTO) {
1689 char *label = &rules->buf[rules->tokens[i].key.value_off];
1692 for (j = i+1; j < rules->token_cur; j++) {
1693 if (rules->tokens[j].type != TK_RULE)
1695 if (rules->tokens[j].rule.label_off == 0)
1697 if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0)
1699 rules->tokens[i].key.rule_goto = j;
1702 if (rules->tokens[i].key.rule_goto == 0)
1703 err(rules->udev, "GOTO '%s' has no matching label in: '%s'\n", label, filename);
1709 static int add_matching_files(struct udev *udev, struct udev_list *file_list, const char *dirname, const char *suffix)
1712 struct dirent *dent;
1713 char filename[UTIL_PATH_SIZE];
1715 dbg(udev, "open directory '%s'\n", dirname);
1716 dir = opendir(dirname);
1718 info(udev, "unable to open '%s': %m\n", dirname);
1722 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1723 if (dent->d_name[0] == '.')
1726 /* look for file matching with specified suffix */
1727 if (suffix != NULL) {
1730 ext = strrchr(dent->d_name, '.');
1733 if (strcmp(ext, suffix) != 0)
1736 util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL);
1737 dbg(udev, "put file '%s' into list\n", filename);
1739 * the basename is the key, the filename the value
1740 * identical basenames from different directories override each other
1741 * entries are sorted after basename
1743 udev_list_entry_add(file_list, dent->d_name, filename);
1750 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1752 struct udev_rules *rules;
1753 struct udev_list file_list;
1754 struct udev_list_entry *file_loop;
1755 struct token end_token;
1758 rules = calloc(1, sizeof(struct udev_rules));
1762 rules->resolve_names = resolve_names;
1763 udev_list_init(udev, &file_list, true);
1765 /* init token array and string buffer */
1766 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1767 if (rules->tokens == NULL) {
1771 rules->token_max = PREALLOC_TOKEN;
1773 rules->buf = malloc(PREALLOC_STRBUF);
1774 if (rules->buf == NULL) {
1775 free(rules->tokens);
1779 rules->buf_max = PREALLOC_STRBUF;
1780 /* offset 0 is always '\0' */
1781 rules->buf[0] = '\0';
1783 dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1784 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1786 rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
1787 if (rules->trie_nodes == NULL) {
1789 free(rules->tokens);
1793 rules->trie_nodes_max = PREALLOC_TRIE;
1794 /* offset 0 is the trie root, with an empty string */
1795 memset(rules->trie_nodes, 0x00, sizeof(struct trie_node));
1796 rules->trie_nodes_cur = 1;
1798 for (udev_get_rules_path(udev, &s, NULL); *s != NULL; s++)
1799 add_matching_files(udev, &file_list, *s, ".rules");
1801 /* add all filenames to the string buffer */
1802 udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1803 const char *filename = udev_list_entry_get_value(file_loop);
1804 unsigned int filename_off;
1806 filename_off = add_string(rules, filename);
1807 /* the offset in the rule is limited to unsigned short */
1808 if (filename_off < USHRT_MAX)
1809 udev_list_entry_set_num(file_loop, filename_off);
1812 /* parse all rules files */
1813 udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1814 const char *filename = udev_list_entry_get_value(file_loop);
1815 unsigned int filename_off = udev_list_entry_get_num(file_loop);
1818 if (stat(filename, &st) != 0) {
1819 err(udev, "can not find '%s': %m\n", filename);
1822 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
1823 info(udev, "ignore empty '%s'\n", filename);
1826 if (S_ISCHR(st.st_mode)) {
1827 info(udev, "ignore masked '%s'\n", filename);
1830 parse_file(rules, filename, filename_off);
1832 udev_list_cleanup(&file_list);
1834 memset(&end_token, 0x00, sizeof(struct token));
1835 end_token.type = TK_END;
1836 add_token(rules, &end_token);
1838 /* shrink allocated token and string buffer */
1839 if (rules->token_cur < rules->token_max) {
1840 struct token *tokens;
1842 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1843 if (tokens != NULL || rules->token_cur == 0) {
1844 rules->tokens = tokens;
1845 rules->token_max = rules->token_cur;
1848 if (rules->buf_cur < rules->buf_max) {
1851 buf = realloc(rules->buf, rules->buf_cur);
1852 if (buf != NULL || rules->buf_cur == 0) {
1854 rules->buf_max = rules->buf_cur;
1857 info(udev, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1858 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1859 info(udev, "temporary index used %zu bytes (%u * %zu bytes)\n",
1860 rules->trie_nodes_cur * sizeof(struct trie_node),
1861 rules->trie_nodes_cur, sizeof(struct trie_node));
1864 free(rules->trie_nodes);
1865 rules->trie_nodes = NULL;
1866 rules->trie_nodes_cur = 0;
1867 rules->trie_nodes_max = 0;
1869 /* cleanup uid/gid cache */
1872 rules->uids_cur = 0;
1873 rules->uids_max = 0;
1876 rules->gids_cur = 0;
1877 rules->gids_max = 0;
1883 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1887 free(rules->tokens);
1889 free(rules->trie_nodes);
1896 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1898 char *key_value = &rules->buf[token->key.value_off];
1905 switch (token->key.glob) {
1907 match = (strcmp(key_value, val) == 0);
1910 match = (fnmatch(key_value, val, 0) == 0);
1917 split = &rules->buf[token->key.value_off];
1922 next = strchr(split, '|');
1924 size_t matchlen = (size_t)(next - split);
1926 match = (matchlen == len && strncmp(split, val, matchlen) == 0);
1930 match = (strcmp(split, val) == 0);
1939 char value[UTIL_PATH_SIZE];
1941 util_strscpy(value, sizeof(value), &rules->buf[token->key.value_off]);
1943 while (key_value != NULL) {
1944 pos = strchr(key_value, '|');
1949 dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str(token->type), key_value, val);
1950 match = (fnmatch(key_value, val, 0) == 0);
1958 match = (val[0] != '\0');
1964 if (match && (token->key.op == OP_MATCH)) {
1965 dbg(rules->udev, "%s is true (matching value)\n", token_str(token->type));
1968 if (!match && (token->key.op == OP_NOMATCH)) {
1969 dbg(rules->udev, "%s is true (non-matching value)\n", token_str(token->type));
1972 dbg(rules->udev, "%s is not true\n", token_str(token->type));
1976 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1979 char nbuf[UTIL_NAME_SIZE];
1981 char vbuf[UTIL_NAME_SIZE];
1984 name = &rules->buf[cur->key.attr_off];
1985 switch (cur->key.attrsubst) {
1987 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1991 value = udev_device_get_sysattr_value(dev, name);
1996 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
2004 /* remove trailing whitespace, if not asked to match for it */
2005 len = strlen(value);
2006 if (len > 0 && isspace(value[len-1])) {
2007 const char *key_value;
2010 key_value = &rules->buf[cur->key.value_off];
2011 klen = strlen(key_value);
2012 if (klen > 0 && !isspace(key_value[klen-1])) {
2013 if (value != vbuf) {
2014 util_strscpy(vbuf, sizeof(vbuf), value);
2017 while (len > 0 && isspace(vbuf[--len]))
2019 dbg(rules->udev, "removed trailing whitespace from '%s'\n", value);
2023 return match_key(rules, cur, value);
2032 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
2036 enum escape_type esc = ESCAPE_UNSET;
2039 if (rules->tokens == NULL)
2042 can_set_name = ((strcmp(udev_device_get_action(event->dev), "remove") != 0) &&
2043 (major(udev_device_get_devnum(event->dev)) > 0 ||
2044 udev_device_get_ifindex(event->dev) > 0));
2046 /* loop through token list, match, run actions or forward to next rule */
2047 cur = &rules->tokens[0];
2050 dump_token(rules, cur);
2051 switch (cur->type) {
2055 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2056 if (!can_set_name && rule->rule.can_set_name)
2061 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
2065 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
2069 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
2072 case TK_M_DEVLINK: {
2073 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2074 struct udev_list_entry *list_entry;
2077 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
2078 const char *devlink;
2080 devlink = &udev_list_entry_get_name(list_entry)[devlen];
2081 if (match_key(rules, cur, devlink) == 0) {
2091 if (match_key(rules, cur, event->name) != 0)
2095 const char *key_name = &rules->buf[cur->key.attr_off];
2098 value = udev_device_get_property_value(event->dev, key_name);
2099 if (value == NULL) {
2100 dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name);
2103 if (match_key(rules, cur, value))
2108 struct udev_list_entry *list_entry;
2111 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
2112 if (strcmp(&rules->buf[cur->key.value_off], udev_list_entry_get_name(list_entry)) == 0) {
2117 if (!match && (cur->key.op != OP_NOMATCH))
2121 case TK_M_SUBSYSTEM:
2122 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
2126 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2129 case TK_M_WAITFOR: {
2130 char filename[UTIL_PATH_SIZE];
2133 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2134 found = (wait_for_file(event->dev, filename, 10) == 0);
2135 if (!found && (cur->key.op != OP_NOMATCH))
2140 if (match_attr(rules, event->dev, event, cur) != 0)
2144 case TK_M_SUBSYSTEMS:
2150 /* get whole sequence of parent matches */
2152 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2155 /* loop over parents */
2156 event->dev_parent = event->dev;
2160 dbg(event->udev, "parent: '%s'\n", udev_device_get_syspath(event->dev_parent));
2161 /* loop over sequence of parent match keys */
2162 for (key = cur; key < next; key++ ) {
2163 dump_token(rules, key);
2166 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2169 case TK_M_SUBSYSTEMS:
2170 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2174 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2178 if (match_attr(rules, event->dev_parent, event, key) != 0)
2182 bool match = udev_device_has_tag(event->dev_parent, &rules->buf[cur->key.value_off]);
2184 if (match && key->key.op == OP_NOMATCH)
2186 if (!match && key->key.op == OP_MATCH)
2193 dbg(event->udev, "parent key matched\n");
2195 dbg(event->udev, "all parent keys matched\n");
2199 event->dev_parent = udev_device_get_parent(event->dev_parent);
2200 if (event->dev_parent == NULL)
2203 /* move behind our sequence of parent match keys */
2208 char filename[UTIL_PATH_SIZE];
2209 struct stat statbuf;
2212 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2213 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2214 if (filename[0] != '/') {
2215 char tmp[UTIL_PATH_SIZE];
2217 util_strscpy(tmp, sizeof(tmp), filename);
2218 util_strscpyl(filename, sizeof(filename),
2219 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2222 attr_subst_subdir(filename, sizeof(filename));
2224 match = (stat(filename, &statbuf) == 0);
2225 dbg(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
2226 if (match && cur->key.mode > 0) {
2227 match = ((statbuf.st_mode & cur->key.mode) > 0);
2228 dbg(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
2229 match ? "matches" : "does not match", cur->key.mode);
2231 if (match && cur->key.op == OP_NOMATCH)
2233 if (!match && cur->key.op == OP_MATCH)
2237 case TK_M_EVENT_TIMEOUT:
2238 info(event->udev, "OPTIONS event_timeout=%u\n", cur->key.event_timeout);
2239 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2241 case TK_M_PROGRAM: {
2242 char program[UTIL_PATH_SIZE];
2244 char result[UTIL_PATH_SIZE];
2246 free(event->program_result);
2247 event->program_result = NULL;
2248 udev_event_apply_format(event, &rules->buf[cur->key.value_off], program, sizeof(program));
2249 envp = udev_device_get_properties_envp(event->dev);
2250 info(event->udev, "PROGRAM '%s' %s:%u\n",
2252 &rules->buf[rule->rule.filename_off],
2253 rule->rule.filename_line);
2255 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2256 if (cur->key.op != OP_NOMATCH)
2261 util_remove_trailing_chars(result, '\n');
2262 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2263 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2265 info(event->udev, "%i character(s) replaced\n" , count);
2267 event->program_result = strdup(result);
2268 dbg(event->udev, "storing result '%s'\n", event->program_result);
2269 if (cur->key.op == OP_NOMATCH)
2274 case TK_M_IMPORT_FILE: {
2275 char import[UTIL_PATH_SIZE];
2277 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2278 if (import_file_into_properties(event->dev, import) != 0)
2279 if (cur->key.op != OP_NOMATCH)
2283 case TK_M_IMPORT_PROG: {
2284 char import[UTIL_PATH_SIZE];
2286 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2287 info(event->udev, "IMPORT '%s' %s:%u\n",
2289 &rules->buf[rule->rule.filename_off],
2290 rule->rule.filename_line);
2292 if (import_program_into_properties(event, import, sigmask) != 0)
2293 if (cur->key.op != OP_NOMATCH)
2297 case TK_M_IMPORT_BUILTIN: {
2298 char command[UTIL_PATH_SIZE];
2300 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2301 /* check if we ran already */
2302 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2303 info(event->udev, "IMPORT builtin skip '%s' %s:%u\n",
2304 udev_builtin_name(cur->key.builtin_cmd),
2305 &rules->buf[rule->rule.filename_off],
2306 rule->rule.filename_line);
2307 /* return the result from earlier run */
2308 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2309 if (cur->key.op != OP_NOMATCH)
2314 event->builtin_run |= (1 << cur->key.builtin_cmd);
2317 udev_event_apply_format(event, &rules->buf[cur->key.value_off], command, sizeof(command));
2318 info(event->udev, "IMPORT builtin '%s' %s:%u\n",
2319 udev_builtin_name(cur->key.builtin_cmd),
2320 &rules->buf[rule->rule.filename_off],
2321 rule->rule.filename_line);
2323 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2324 /* remember failure */
2325 info(rules->udev, "IMPORT builtin '%s' returned non-zero\n",
2326 udev_builtin_name(cur->key.builtin_cmd));
2327 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2328 if (cur->key.op != OP_NOMATCH)
2333 case TK_M_IMPORT_DB: {
2334 const char *key = &rules->buf[cur->key.value_off];
2337 value = udev_device_get_property_value(event->dev_db, key);
2338 if (value != NULL) {
2339 struct udev_list_entry *entry;
2341 entry = udev_device_add_property(event->dev, key, value);
2342 udev_list_entry_set_num(entry, true);
2344 if (cur->key.op != OP_NOMATCH)
2349 case TK_M_IMPORT_CMDLINE: {
2351 bool imported = false;
2353 f = fopen("/proc/cmdline", "r");
2357 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2358 const char *key = &rules->buf[cur->key.value_off];
2361 pos = strstr(cmdline, key);
2363 struct udev_list_entry *entry;
2366 if (pos[0] == '\0' || isspace(pos[0])) {
2367 /* we import simple flags as 'FLAG=1' */
2368 entry = udev_device_add_property(event->dev, key, "1");
2369 udev_list_entry_set_num(entry, true);
2371 } else if (pos[0] == '=') {
2376 while (pos[0] != '\0' && !isspace(pos[0]))
2379 entry = udev_device_add_property(event->dev, key, value);
2380 udev_list_entry_set_num(entry, true);
2387 if (!imported && cur->key.op != OP_NOMATCH)
2391 case TK_M_IMPORT_PARENT: {
2392 char import[UTIL_PATH_SIZE];
2394 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2395 if (import_parent_into_properties(event->dev, import) != 0)
2396 if (cur->key.op != OP_NOMATCH)
2401 if (match_key(rules, cur, event->program_result) != 0)
2404 case TK_A_STRING_ESCAPE_NONE:
2407 case TK_A_STRING_ESCAPE_REPLACE:
2408 esc = ESCAPE_REPLACE;
2410 case TK_A_DB_PERSIST:
2411 udev_device_set_db_persist(event->dev);
2413 case TK_A_INOTIFY_WATCH:
2414 if (event->inotify_watch_final)
2416 if (cur->key.op == OP_ASSIGN_FINAL)
2417 event->inotify_watch_final = true;
2418 event->inotify_watch = cur->key.watch;
2420 case TK_A_DEVLINK_PRIO:
2421 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2424 char owner[UTIL_NAME_SIZE];
2426 if (event->owner_final)
2428 if (cur->key.op == OP_ASSIGN_FINAL)
2429 event->owner_final = true;
2430 udev_event_apply_format(event, &rules->buf[cur->key.value_off], owner, sizeof(owner));
2431 event->uid = util_lookup_user(event->udev, owner);
2432 info(event->udev, "OWNER %u %s:%u\n",
2434 &rules->buf[rule->rule.filename_off],
2435 rule->rule.filename_line);
2439 char group[UTIL_NAME_SIZE];
2441 if (event->group_final)
2443 if (cur->key.op == OP_ASSIGN_FINAL)
2444 event->group_final = true;
2445 udev_event_apply_format(event, &rules->buf[cur->key.value_off], group, sizeof(group));
2446 event->gid = util_lookup_group(event->udev, group);
2447 info(event->udev, "GROUP %u %s:%u\n",
2449 &rules->buf[rule->rule.filename_off],
2450 rule->rule.filename_line);
2454 char mode_str[UTIL_NAME_SIZE];
2458 if (event->mode_final)
2460 udev_event_apply_format(event, &rules->buf[cur->key.value_off], mode_str, sizeof(mode_str));
2461 mode = strtol(mode_str, &endptr, 8);
2462 if (endptr[0] != '\0') {
2463 err(event->udev, "ignoring invalid mode '%s'\n", mode_str);
2466 if (cur->key.op == OP_ASSIGN_FINAL)
2467 event->mode_final = true;
2468 event->mode_set = true;
2470 info(event->udev, "MODE %#o %s:%u\n",
2472 &rules->buf[rule->rule.filename_off],
2473 rule->rule.filename_line);
2477 if (event->owner_final)
2479 if (cur->key.op == OP_ASSIGN_FINAL)
2480 event->owner_final = true;
2481 event->uid = cur->key.uid;
2482 info(event->udev, "OWNER %u %s:%u\n",
2484 &rules->buf[rule->rule.filename_off],
2485 rule->rule.filename_line);
2488 if (event->group_final)
2490 if (cur->key.op == OP_ASSIGN_FINAL)
2491 event->group_final = true;
2492 event->gid = cur->key.gid;
2493 info(event->udev, "GROUP %u %s:%u\n",
2495 &rules->buf[rule->rule.filename_off],
2496 rule->rule.filename_line);
2499 if (event->mode_final)
2501 if (cur->key.op == OP_ASSIGN_FINAL)
2502 event->mode_final = true;
2503 event->mode_set = true;
2504 event->mode = cur->key.mode;
2505 info(event->udev, "MODE %#o %s:%u\n",
2507 &rules->buf[rule->rule.filename_off],
2508 rule->rule.filename_line);
2511 const char *name = &rules->buf[cur->key.attr_off];
2512 char *value = &rules->buf[cur->key.value_off];
2514 if (value[0] != '\0') {
2515 char temp_value[UTIL_NAME_SIZE];
2516 struct udev_list_entry *entry;
2518 udev_event_apply_format(event, value, temp_value, sizeof(temp_value));
2519 entry = udev_device_add_property(event->dev, name, temp_value);
2520 /* store in db, skip private keys */
2522 udev_list_entry_set_num(entry, true);
2524 udev_device_add_property(event->dev, name, NULL);
2529 char tag[UTIL_PATH_SIZE];
2532 udev_event_apply_format(event, &rules->buf[cur->key.value_off], tag, sizeof(tag));
2533 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2534 udev_device_cleanup_tags_list(event->dev);
2535 for (p = tag; *p != '\0'; p++) {
2536 if ((*p >= 'a' && *p <= 'z') ||
2537 (*p >= 'A' && *p <= 'Z') ||
2538 (*p >= '0' && *p <= '9') ||
2539 *p == '-' || *p == '_')
2541 err(event->udev, "ignoring invalid tag name '%s'\n", tag);
2544 udev_device_add_tag(event->dev, tag);
2548 const char *name = &rules->buf[cur->key.value_off];
2550 char name_str[UTIL_PATH_SIZE];
2553 if (event->name_final)
2555 if (cur->key.op == OP_ASSIGN_FINAL)
2556 event->name_final = true;
2557 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2558 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2559 count = util_replace_chars(name_str, "/");
2561 info(event->udev, "%i character(s) replaced\n", count);
2563 if (major(udev_device_get_devnum(event->dev))) {
2564 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2566 if (strcmp(name_str, &udev_device_get_devnode(event->dev)[devlen]) != 0) {
2567 err(event->udev, "NAME=\"%s\" ignored, kernel device nodes "
2568 "can not be renamed; please fix it in %s:%u\n", name,
2569 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2574 event->name = strdup(name_str);
2575 info(event->udev, "NAME '%s' %s:%u\n",
2577 &rules->buf[rule->rule.filename_off],
2578 rule->rule.filename_line);
2581 case TK_A_DEVLINK: {
2582 char temp[UTIL_PATH_SIZE];
2583 char filename[UTIL_PATH_SIZE];
2587 if (event->devlink_final)
2589 if (major(udev_device_get_devnum(event->dev)) == 0)
2591 if (cur->key.op == OP_ASSIGN_FINAL)
2592 event->devlink_final = true;
2593 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2594 udev_device_cleanup_devlinks_list(event->dev);
2596 /* allow multiple symlinks separated by spaces */
2597 udev_event_apply_format(event, &rules->buf[cur->key.value_off], temp, sizeof(temp));
2598 if (esc == ESCAPE_UNSET)
2599 count = util_replace_chars(temp, "/ ");
2600 else if (esc == ESCAPE_REPLACE)
2601 count = util_replace_chars(temp, "/");
2603 info(event->udev, "%i character(s) replaced\n" , count);
2604 dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
2606 while (isspace(pos[0]))
2608 next = strchr(pos, ' ');
2609 while (next != NULL) {
2611 info(event->udev, "LINK '%s' %s:%u\n", pos,
2612 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2613 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2614 udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique);
2615 while (isspace(next[1]))
2618 next = strchr(pos, ' ');
2620 if (pos[0] != '\0') {
2621 info(event->udev, "LINK '%s' %s:%u\n", pos,
2622 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2623 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2624 udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique);
2629 const char *key_name = &rules->buf[cur->key.attr_off];
2630 char attr[UTIL_PATH_SIZE];
2631 char value[UTIL_NAME_SIZE];
2634 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2635 util_strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2636 attr_subst_subdir(attr, sizeof(attr));
2638 udev_event_apply_format(event, &rules->buf[cur->key.value_off], value, sizeof(value));
2639 info(event->udev, "ATTR '%s' writing '%s' %s:%u\n", attr, value,
2640 &rules->buf[rule->rule.filename_off],
2641 rule->rule.filename_line);
2642 f = fopen(attr, "w");
2644 if (fprintf(f, "%s", value) <= 0)
2645 err(event->udev, "error writing ATTR{%s}: %m\n", attr);
2648 err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);
2653 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2654 udev_list_cleanup(&event->run_list);
2655 info(event->udev, "RUN '%s' %s:%u\n",
2656 &rules->buf[cur->key.value_off],
2657 &rules->buf[rule->rule.filename_off],
2658 rule->rule.filename_line);
2659 udev_list_entry_add(&event->run_list, &rules->buf[cur->key.value_off], NULL);
2663 if (cur->key.rule_goto == 0)
2665 cur = &rules->tokens[cur->key.rule_goto];
2670 case TK_M_PARENTS_MIN:
2671 case TK_M_PARENTS_MAX:
2674 err(rules->udev, "wrong type %u\n", cur->type);
2681 /* fast-forward to next rule */
2682 cur = rule + rule->rule.token_count;
2683 dbg(rules->udev, "forward to rule: %u\n",
2684 (unsigned int) (cur - rules->tokens));
2688 void udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2696 if (rules->tokens == NULL)
2699 cur = &rules->tokens[0];
2702 switch (cur->type) {
2707 /* skip rules without a static_node tag */
2708 if (!rule->rule.has_static_node)
2722 mode = cur->key.mode;
2724 case TK_A_STATIC_NODE: {
2725 char filename[UTIL_PATH_SIZE];
2728 /* we assure, that the permissions tokens are sorted before the static token */
2729 if (mode == 0 && uid == 0 && gid == 0)
2731 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(rules->udev), "/",
2732 &rules->buf[cur->key.value_off], NULL);
2733 if (stat(filename, &stats) != 0)
2735 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2743 if (mode != (stats.st_mode & 01777)) {
2744 chmod(filename, mode);
2745 info(rules->udev, "chmod '%s' %#o\n", filename, mode);
2748 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2749 chown(filename, uid, gid);
2750 info(rules->udev, "chown '%s' %u %u\n", filename, uid, gid);
2753 utimensat(AT_FDCWD, filename, NULL, 0);
2763 /* fast-forward to next rule */
2764 cur = rule + rule->rule.token_count;