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_BUILTIN, /* val, bool */
172 TK_A_RUN_PROGRAM, /* val, bool */
173 TK_A_GOTO, /* size_t */
178 /* we try to pack stuff in a way that we take only 12 bytes per token */
181 unsigned char type; /* same in rule and key */
183 enum token_type type:8;
185 bool has_static_node:1;
186 unsigned int unused:6;
187 unsigned short token_count;
188 unsigned int label_off;
189 unsigned short filename_off;
190 unsigned short filename_line;
193 enum token_type type:8;
194 enum operation_type op:8;
195 enum string_glob_type glob:8;
196 enum string_subst_type subst:4;
197 enum string_subst_type attrsubst:4;
198 unsigned int value_off;
200 unsigned int attr_off;
202 unsigned int rule_goto;
209 enum udev_builtin_cmd builtin_cmd;
217 struct udev_rules *rules;
219 struct token token[MAX_TK];
220 unsigned int token_cur;
224 static const char *operation_str(enum operation_type type)
226 static const char *operation_strs[] = {
227 [OP_UNSET] = "UNSET",
228 [OP_MATCH] = "match",
229 [OP_NOMATCH] = "nomatch",
230 [OP_MATCH_MAX] = "MATCH_MAX",
233 [OP_ASSIGN] = "assign",
234 [OP_ASSIGN_FINAL] = "assign-final",
237 return operation_strs[type];
240 static const char *string_glob_str(enum string_glob_type type)
242 static const char *string_glob_strs[] = {
243 [GL_UNSET] = "UNSET",
244 [GL_PLAIN] = "plain",
246 [GL_SPLIT] = "split",
247 [GL_SPLIT_GLOB] = "split-glob",
248 [GL_SOMETHING] = "split-glob",
251 return string_glob_strs[type];
254 static const char *token_str(enum token_type type)
256 static const char *token_strs[] = {
257 [TK_UNSET] = "UNSET",
260 [TK_M_ACTION] = "M ACTION",
261 [TK_M_DEVPATH] = "M DEVPATH",
262 [TK_M_KERNEL] = "M KERNEL",
263 [TK_M_DEVLINK] = "M DEVLINK",
264 [TK_M_NAME] = "M NAME",
265 [TK_M_ENV] = "M ENV",
266 [TK_M_TAG] = "M TAG",
267 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
268 [TK_M_DRIVER] = "M DRIVER",
269 [TK_M_WAITFOR] = "M WAITFOR",
270 [TK_M_ATTR] = "M ATTR",
272 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
273 [TK_M_KERNELS] = "M KERNELS",
274 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
275 [TK_M_DRIVERS] = "M DRIVERS",
276 [TK_M_ATTRS] = "M ATTRS",
277 [TK_M_TAGS] = "M TAGS",
278 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
280 [TK_M_TEST] = "M TEST",
281 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
282 [TK_M_PROGRAM] = "M PROGRAM",
283 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
284 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
285 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
286 [TK_M_IMPORT_DB] = "M IMPORT_DB",
287 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
288 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
289 [TK_M_RESULT] = "M RESULT",
290 [TK_M_MAX] = "M MAX",
292 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
293 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
294 [TK_A_DB_PERSIST] = "A DB_PERSIST",
295 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
296 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
297 [TK_A_OWNER] = "A OWNER",
298 [TK_A_GROUP] = "A GROUP",
299 [TK_A_MODE] = "A MODE",
300 [TK_A_OWNER_ID] = "A OWNER_ID",
301 [TK_A_GROUP_ID] = "A GROUP_ID",
302 [TK_A_STATIC_NODE] = "A STATIC_NODE",
303 [TK_A_MODE_ID] = "A MODE_ID",
304 [TK_A_ENV] = "A ENV",
305 [TK_A_TAG] = "A ENV",
306 [TK_A_NAME] = "A NAME",
307 [TK_A_DEVLINK] = "A DEVLINK",
308 [TK_A_ATTR] = "A ATTR",
309 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
310 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
311 [TK_A_GOTO] = "A GOTO",
316 return token_strs[type];
319 static void dump_token(struct udev_rules *rules, struct token *token)
321 enum token_type type = token->type;
322 enum operation_type op = token->key.op;
323 enum string_glob_type glob = token->key.glob;
324 const char *value = &rules->buf[token->key.value_off];
325 const char *attr = &rules->buf[token->key.attr_off];
330 const char *tks_ptr = (char *)rules->tokens;
331 const char *tk_ptr = (char *)token;
332 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
334 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
335 &rules->buf[token->rule.filename_off], token->rule.filename_line,
336 idx, token->rule.token_count,
337 &rules->buf[token->rule.label_off]);
349 case TK_M_SUBSYSTEMS:
353 case TK_M_IMPORT_FILE:
354 case TK_M_IMPORT_PROG:
356 case TK_M_IMPORT_CMDLINE:
357 case TK_M_IMPORT_PARENT:
364 case TK_A_RUN_BUILTIN:
365 case TK_A_RUN_PROGRAM:
366 log_debug("%s %s '%s'(%s)\n",
367 token_str(type), operation_str(op), value, string_glob_str(glob));
369 case TK_M_IMPORT_BUILTIN:
370 log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
377 log_debug("%s %s '%s' '%s'(%s)\n",
378 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
382 log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value);
384 case TK_A_STRING_ESCAPE_NONE:
385 case TK_A_STRING_ESCAPE_REPLACE:
386 case TK_A_DB_PERSIST:
387 log_debug("%s\n", token_str(type));
390 log_debug("%s %s '%s'(%s) %#o\n",
391 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
393 case TK_A_INOTIFY_WATCH:
394 log_debug("%s %u\n", token_str(type), token->key.watch);
396 case TK_A_DEVLINK_PRIO:
397 log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
400 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
403 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
406 log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
408 case TK_A_STATIC_NODE:
409 log_debug("%s '%s'\n", token_str(type), value);
411 case TK_M_EVENT_TIMEOUT:
412 log_debug("%s %u\n", token_str(type), token->key.event_timeout);
415 log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
418 log_debug("* %s\n", token_str(type));
420 case TK_M_PARENTS_MIN:
421 case TK_M_PARENTS_MAX:
424 log_debug("unknown type %u\n", type);
429 static void dump_rules(struct udev_rules *rules)
433 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
435 rules->token_cur * sizeof(struct token),
438 for(i = 0; i < rules->token_cur; i++)
439 dump_token(rules, &rules->tokens[i]);
442 static inline const char *operation_str(enum operation_type type) { return NULL; }
443 static inline const char *token_str(enum token_type type) { return NULL; }
444 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
445 static inline void dump_rules(struct udev_rules *rules) {}
448 static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
452 /* grow buffer if needed */
453 if (rules->buf_cur + bytes+1 >= rules->buf_max) {
457 /* double the buffer size */
458 add = rules->buf_max;
462 buf = realloc(rules->buf, rules->buf_max + add);
466 rules->buf_max += add;
468 off = rules->buf_cur;
469 memcpy(&rules->buf[rules->buf_cur], str, bytes);
470 rules->buf_cur += bytes;
475 static int add_string(struct udev_rules *rules, const char *str)
477 unsigned int node_idx;
478 struct trie_node *new_node;
479 unsigned int new_node_idx;
484 struct trie_node *parent;
486 /* walk trie, start from last character of str to find matching tails */
490 for (depth = 0; depth <= len; depth++) {
491 struct trie_node *node;
492 unsigned int child_idx;
494 node = &rules->trie_nodes[node_idx];
495 off = node->value_off + node->value_len - len;
497 /* match against current node */
498 if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0))
501 /* lookup child node */
502 key = str[len - 1 - depth];
503 child_idx = node->child_idx;
504 while (child_idx > 0) {
505 struct trie_node *child;
507 child = &rules->trie_nodes[child_idx];
508 if (child->key == key)
510 child_idx = child->next_child_idx;
514 node_idx = child_idx;
517 /* string not found, add it */
518 off = add_new_string(rules, str, len + 1);
520 /* grow trie nodes if needed */
521 if (rules->trie_nodes_cur >= rules->trie_nodes_max) {
522 struct trie_node *nodes;
525 /* double the buffer size */
526 add = rules->trie_nodes_max;
530 nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node));
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 rules->tokens = tokens;
576 rules->token_max += add;
578 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
583 static uid_t add_uid(struct udev_rules *rules, const char *owner)
589 /* lookup, if we know it already */
590 for (i = 0; i < rules->uids_cur; i++) {
591 off = rules->uids[i].name_off;
592 if (strcmp(&rules->buf[off], owner) == 0) {
593 uid = rules->uids[i].uid;
597 uid = util_lookup_user(rules->udev, owner);
599 /* grow buffer if needed */
600 if (rules->uids_cur+1 >= rules->uids_max) {
601 struct uid_gid *uids;
604 /* double the buffer size */
605 add = rules->uids_max;
609 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
613 rules->uids_max += add;
615 rules->uids[rules->uids_cur].uid = uid;
616 off = add_string(rules, owner);
619 rules->uids[rules->uids_cur].name_off = off;
624 static gid_t add_gid(struct udev_rules *rules, const char *group)
630 /* lookup, if we know it already */
631 for (i = 0; i < rules->gids_cur; i++) {
632 off = rules->gids[i].name_off;
633 if (strcmp(&rules->buf[off], group) == 0) {
634 gid = rules->gids[i].gid;
638 gid = util_lookup_group(rules->udev, group);
640 /* grow buffer if needed */
641 if (rules->gids_cur+1 >= rules->gids_max) {
642 struct uid_gid *gids;
645 /* double the buffer size */
646 add = rules->gids_max;
650 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
654 rules->gids_max += add;
656 rules->gids[rules->gids_cur].gid = gid;
657 off = add_string(rules, group);
660 rules->gids[rules->gids_cur].name_off = off;
665 static int import_property_from_string(struct udev_device *dev, char *line)
667 struct udev *udev = udev_device_get_udev(dev);
674 while (isspace(key[0]))
677 /* comment or empty line */
678 if (key[0] == '#' || key[0] == '\0')
681 /* split key/value */
682 val = strchr(key, '=');
689 while (isspace(val[0]))
696 while (isspace(key[len-1]))
700 /* terminate value */
704 while (isspace(val[len-1]))
712 if (val[0] == '"' || val[0] == '\'') {
713 if (val[len-1] != val[0]) {
714 log_debug("inconsistent quoting: '%s', skip\n", line);
721 /* handle device, renamed by external tool, returning new path */
722 if (strcmp(key, "DEVPATH") == 0) {
723 char syspath[UTIL_PATH_SIZE];
725 log_debug("updating devpath from '%s' to '%s'\n",
726 udev_device_get_devpath(dev), val);
727 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), val, NULL);
728 udev_device_set_syspath(dev, syspath);
730 struct udev_list_entry *entry;
732 entry = udev_device_add_property(dev, key, val);
733 /* store in db, skip private keys */
735 udev_list_entry_set_num(entry, true);
740 static int import_file_into_properties(struct udev_device *dev, const char *filename)
743 char line[UTIL_LINE_SIZE];
745 f = fopen(filename, "r");
748 while (fgets(line, sizeof(line), f) != NULL)
749 import_property_from_string(dev, line);
754 static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
756 struct udev_device *dev = event->dev;
758 char result[UTIL_LINE_SIZE];
762 envp = udev_device_get_properties_envp(dev);
763 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
768 while (line != NULL) {
771 pos = strchr(line, '\n');
776 import_property_from_string(dev, line);
782 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
784 struct udev *udev = udev_device_get_udev(dev);
785 struct udev_device *dev_parent;
786 struct udev_list_entry *list_entry;
788 dev_parent = udev_device_get_parent(dev);
789 if (dev_parent == NULL)
792 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
793 const char *key = udev_list_entry_get_name(list_entry);
794 const char *val = udev_list_entry_get_value(list_entry);
796 if (fnmatch(filter, key, 0) == 0) {
797 struct udev_list_entry *entry;
799 entry = udev_device_add_property(dev, key, val);
800 /* store in db, skip private keys */
802 udev_list_entry_set_num(entry, true);
808 #define WAIT_LOOP_PER_SECOND 50
809 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
811 struct udev *udev = udev_device_get_udev(dev);
812 char filepath[UTIL_PATH_SIZE];
813 char devicepath[UTIL_PATH_SIZE];
815 int loop = timeout * WAIT_LOOP_PER_SECOND;
817 /* a relative path is a device attribute */
818 devicepath[0] = '\0';
819 if (file[0] != '/') {
820 util_strscpyl(devicepath, sizeof(devicepath),
821 udev_get_sys_path(udev), udev_device_get_devpath(dev), NULL);
822 util_strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
827 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
830 if (stat(file, &stats) == 0) {
831 log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
834 /* make sure, the device did not disappear in the meantime */
835 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
836 log_debug("device disappeared while waiting for '%s'\n", file);
839 log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
840 nanosleep(&duration, NULL);
842 log_debug("waiting for '%s' failed\n", file);
846 static int attr_subst_subdir(char *attr, size_t len)
850 if (strstr(attr, "/*/")) {
852 char dirname[UTIL_PATH_SIZE];
856 util_strscpy(dirname, sizeof(dirname), attr);
857 pos = strstr(dirname, "/*/");
862 dir = opendir(dirname);
866 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
869 if (dent->d_name[0] == '.')
871 util_strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
872 if (stat(attr, &stats) == 0) {
884 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
890 if (linepos == NULL || linepos[0] == '\0')
893 /* skip whitespace */
894 while (isspace(linepos[0]) || linepos[0] == ',')
898 if (linepos[0] == '\0')
904 if (linepos[0] == '\0')
906 if (isspace(linepos[0]))
908 if (linepos[0] == '=')
910 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
911 if (linepos[1] == '=')
915 /* remember end of key */
918 /* skip whitespace after key */
919 while (isspace(linepos[0]))
921 if (linepos[0] == '\0')
924 /* get operation type */
925 if (linepos[0] == '=' && linepos[1] == '=') {
928 } else if (linepos[0] == '!' && linepos[1] == '=') {
931 } else if (linepos[0] == '+' && linepos[1] == '=') {
934 } else if (linepos[0] == '=') {
937 } else if (linepos[0] == ':' && linepos[1] == '=') {
938 *op = OP_ASSIGN_FINAL;
946 /* skip whitespace after operator */
947 while (isspace(linepos[0]))
949 if (linepos[0] == '\0')
953 if (linepos[0] == '"')
960 temp = strchr(linepos, '"');
966 /* move line to next key */
971 /* extract possible KEY{attr} */
972 static char *get_key_attribute(struct udev *udev, char *str)
977 attr = strchr(str, '{');
980 pos = strchr(attr, '}');
982 log_error("missing closing brace for format\n");
991 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
992 enum operation_type op,
993 const char *value, const void *data)
995 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
996 const char *attr = NULL;
998 memset(token, 0x00, sizeof(struct token));
1004 case TK_M_SUBSYSTEM:
1010 case TK_M_SUBSYSTEMS:
1014 case TK_M_IMPORT_FILE:
1015 case TK_M_IMPORT_PROG:
1016 case TK_M_IMPORT_DB:
1017 case TK_M_IMPORT_CMDLINE:
1018 case TK_M_IMPORT_PARENT:
1027 token->key.value_off = add_string(rule_tmp->rules, value);
1029 case TK_M_IMPORT_BUILTIN:
1030 token->key.value_off = add_string(rule_tmp->rules, value);
1031 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
1039 token->key.value_off = add_string(rule_tmp->rules, value);
1040 token->key.attr_off = add_string(rule_tmp->rules, attr);
1043 token->key.value_off = add_string(rule_tmp->rules, value);
1044 token->key.devlink_unique = *(int *)data;
1047 token->key.value_off = add_string(rule_tmp->rules, value);
1049 token->key.mode = *(mode_t *)data;
1051 case TK_A_STRING_ESCAPE_NONE:
1052 case TK_A_STRING_ESCAPE_REPLACE:
1053 case TK_A_DB_PERSIST:
1055 case TK_A_RUN_BUILTIN:
1056 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
1057 case TK_A_RUN_PROGRAM:
1058 token->key.value_off = add_string(rule_tmp->rules, value);
1060 case TK_A_INOTIFY_WATCH:
1061 case TK_A_DEVLINK_PRIO:
1062 token->key.devlink_prio = *(int *)data;
1065 token->key.uid = *(uid_t *)data;
1068 token->key.gid = *(gid_t *)data;
1071 token->key.mode = *(mode_t *)data;
1073 case TK_A_STATIC_NODE:
1074 token->key.value_off = add_string(rule_tmp->rules, value);
1076 case TK_M_EVENT_TIMEOUT:
1077 token->key.event_timeout = *(int *)data;
1080 case TK_M_PARENTS_MIN:
1081 case TK_M_PARENTS_MAX:
1085 log_error("wrong type %u\n", type);
1089 if (value != NULL && type < TK_M_MAX) {
1090 /* check if we need to split or call fnmatch() while matching rules */
1091 enum string_glob_type glob;
1095 has_split = (strchr(value, '|') != NULL);
1096 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
1097 if (has_split && has_glob) {
1098 glob = GL_SPLIT_GLOB;
1099 } else if (has_split) {
1101 } else if (has_glob) {
1102 if (strcmp(value, "?*") == 0)
1103 glob = GL_SOMETHING;
1109 token->key.glob = glob;
1112 if (value != NULL && type > TK_M_MAX) {
1113 /* check if assigned value has substitution chars */
1114 if (value[0] == '[')
1115 token->key.subst = SB_SUBSYS;
1116 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
1117 token->key.subst = SB_FORMAT;
1119 token->key.subst = SB_NONE;
1123 /* check if property/attribut name has substitution chars */
1125 token->key.attrsubst = SB_SUBSYS;
1126 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
1127 token->key.attrsubst = SB_FORMAT;
1129 token->key.attrsubst = SB_NONE;
1132 token->key.type = type;
1134 rule_tmp->token_cur++;
1135 if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) {
1136 log_error("temporary rule array too small\n");
1142 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1145 unsigned int start = 0;
1146 unsigned int end = rule_tmp->token_cur;
1148 for (i = 0; i < rule_tmp->token_cur; i++) {
1149 enum token_type next_val = TK_UNSET;
1150 unsigned int next_idx = 0;
1153 /* find smallest value */
1154 for (j = start; j < end; j++) {
1155 if (rule_tmp->token[j].type == TK_UNSET)
1157 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1158 next_val = rule_tmp->token[j].type;
1163 /* add token and mark done */
1164 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1166 rule_tmp->token[next_idx].type = TK_UNSET;
1169 if (next_idx == start)
1171 if (next_idx+1 == end)
1177 static int add_rule(struct udev_rules *rules, char *line,
1178 const char *filename, unsigned int filename_off, unsigned int lineno)
1182 struct rule_tmp rule_tmp;
1184 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1185 rule_tmp.rules = rules;
1186 rule_tmp.rule.type = TK_RULE;
1187 rule_tmp.rule.rule.filename_off = filename_off;
1188 rule_tmp.rule.rule.filename_line = lineno;
1194 enum operation_type op;
1196 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1199 if (strcmp(key, "ACTION") == 0) {
1200 if (op > OP_MATCH_MAX) {
1201 log_error("invalid ACTION operation\n");
1204 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1208 if (strcmp(key, "DEVPATH") == 0) {
1209 if (op > OP_MATCH_MAX) {
1210 log_error("invalid DEVPATH operation\n");
1213 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1217 if (strcmp(key, "KERNEL") == 0) {
1218 if (op > OP_MATCH_MAX) {
1219 log_error("invalid KERNEL operation\n");
1222 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1226 if (strcmp(key, "SUBSYSTEM") == 0) {
1227 if (op > OP_MATCH_MAX) {
1228 log_error("invalid SUBSYSTEM operation\n");
1231 /* bus, class, subsystem events should all be the same */
1232 if (strcmp(value, "subsystem") == 0 ||
1233 strcmp(value, "bus") == 0 ||
1234 strcmp(value, "class") == 0) {
1235 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
1236 log_error("'%s' must be specified as 'subsystem' \n"
1237 "please fix it in %s:%u", value, filename, lineno);
1238 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1240 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1244 if (strcmp(key, "DRIVER") == 0) {
1245 if (op > OP_MATCH_MAX) {
1246 log_error("invalid DRIVER operation\n");
1249 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1253 if (strncmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
1254 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1256 log_error("error parsing ATTR attribute\n");
1259 if (op < OP_MATCH_MAX) {
1260 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1262 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1267 if (strcmp(key, "KERNELS") == 0) {
1268 if (op > OP_MATCH_MAX) {
1269 log_error("invalid KERNELS operation\n");
1272 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1276 if (strcmp(key, "SUBSYSTEMS") == 0) {
1277 if (op > OP_MATCH_MAX) {
1278 log_error("invalid SUBSYSTEMS operation\n");
1281 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1285 if (strcmp(key, "DRIVERS") == 0) {
1286 if (op > OP_MATCH_MAX) {
1287 log_error("invalid DRIVERS operation\n");
1290 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1294 if (strncmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0) {
1295 if (op > OP_MATCH_MAX) {
1296 log_error("invalid ATTRS operation\n");
1299 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1301 log_error("error parsing ATTRS attribute\n");
1304 if (strncmp(attr, "device/", 7) == 0)
1305 log_error("the 'device' link may not be available in a future kernel, "
1306 "please fix it in %s:%u", filename, lineno);
1307 else if (strstr(attr, "../") != NULL)
1308 log_error("do not reference parent sysfs directories directly, "
1309 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1310 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1314 if (strcmp(key, "TAGS") == 0) {
1315 if (op > OP_MATCH_MAX) {
1316 log_error("invalid TAGS operation\n");
1319 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1323 if (strncmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
1324 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1326 log_error("error parsing ENV attribute\n");
1329 if (op < OP_MATCH_MAX) {
1330 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1333 static const char *blacklist[] = {
1348 for (i = 0; i < ARRAY_SIZE(blacklist); i++)
1349 if (strcmp(attr, blacklist[i]) == 0) {
1350 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
1353 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1359 if (strcmp(key, "TAG") == 0) {
1360 if (op < OP_MATCH_MAX)
1361 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1363 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1367 if (strcmp(key, "PROGRAM") == 0) {
1368 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1372 if (strcmp(key, "RESULT") == 0) {
1373 if (op > OP_MATCH_MAX) {
1374 log_error("invalid RESULT operation\n");
1377 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1381 if (strncmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
1382 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1384 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1387 if (strcmp(attr, "program") == 0) {
1388 /* find known built-in command */
1389 if (value[0] != '/') {
1390 enum udev_builtin_cmd cmd;
1392 cmd = udev_builtin_lookup(value);
1393 if (cmd < UDEV_BUILTIN_MAX) {
1394 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1395 value, filename, lineno);
1396 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1400 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1401 } else if (strcmp(attr, "builtin") == 0) {
1402 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1404 if (cmd < UDEV_BUILTIN_MAX)
1405 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1407 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1408 } else if (strcmp(attr, "file") == 0) {
1409 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1410 } else if (strcmp(attr, "db") == 0) {
1411 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1412 } else if (strcmp(attr, "cmdline") == 0) {
1413 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1414 } else if (strcmp(attr, "parent") == 0) {
1415 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1417 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno);
1421 if (strncmp(key, "TEST", sizeof("TEST")-1) == 0) {
1424 if (op > OP_MATCH_MAX) {
1425 log_error("invalid TEST operation\n");
1428 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1430 mode = strtol(attr, NULL, 8);
1431 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1433 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1438 if (strncmp(key, "RUN", sizeof("RUN")-1) == 0) {
1439 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1443 if (strcmp(attr, "builtin") == 0) {
1444 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1446 if (cmd < UDEV_BUILTIN_MAX)
1447 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1449 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1450 } else if (strcmp(attr, "program") == 0) {
1451 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1453 if (strncmp(value, "socket:", 7) == 0)
1454 log_error("RUN+=\"socket:...\" support will be removed from a future udev release. "
1455 "Please remove it from: %s:%u and use libudev to subscribe to events.\n", filename, lineno);
1456 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1458 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno);
1464 if (strcmp(key, "WAIT_FOR") == 0 || strcmp(key, "WAIT_FOR_SYSFS") == 0) {
1465 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1469 if (strcmp(key, "LABEL") == 0) {
1470 rule_tmp.rule.rule.label_off = add_string(rules, value);
1474 if (strcmp(key, "GOTO") == 0) {
1475 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1479 if (strncmp(key, "NAME", sizeof("NAME")-1) == 0) {
1480 if (op < OP_MATCH_MAX) {
1481 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1483 if (strcmp(value, "%k") == 0) {
1484 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1485 "please remove it from %s:%u\n", filename, lineno);
1488 if (value[0] == '\0') {
1489 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1490 "please remove it from %s:%u\n", filename, lineno);
1493 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1495 rule_tmp.rule.rule.can_set_name = true;
1499 if (strncmp(key, "SYMLINK", sizeof("SYMLINK")-1) == 0) {
1500 if (op < OP_MATCH_MAX) {
1501 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1505 attr = get_key_attribute(rules->udev, key + sizeof("SYMLINK")-1);
1506 if (attr != NULL && strstr(attr, "unique") != NULL)
1508 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, &flag);
1510 rule_tmp.rule.rule.can_set_name = true;
1514 if (strcmp(key, "OWNER") == 0) {
1518 uid = strtoul(value, &endptr, 10);
1519 if (endptr[0] == '\0') {
1520 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1521 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1522 uid = add_uid(rules, value);
1523 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1524 } else if (rules->resolve_names >= 0) {
1525 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1527 rule_tmp.rule.rule.can_set_name = true;
1531 if (strcmp(key, "GROUP") == 0) {
1535 gid = strtoul(value, &endptr, 10);
1536 if (endptr[0] == '\0') {
1537 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1538 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1539 gid = add_gid(rules, value);
1540 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1541 } else if (rules->resolve_names >= 0) {
1542 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1544 rule_tmp.rule.rule.can_set_name = true;
1548 if (strcmp(key, "MODE") == 0) {
1552 mode = strtol(value, &endptr, 8);
1553 if (endptr[0] == '\0')
1554 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1556 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1557 rule_tmp.rule.rule.can_set_name = true;
1561 if (strcmp(key, "OPTIONS") == 0) {
1564 pos = strstr(value, "link_priority=");
1566 int prio = atoi(&pos[strlen("link_priority=")]);
1568 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1571 pos = strstr(value, "event_timeout=");
1573 int tout = atoi(&pos[strlen("event_timeout=")]);
1575 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
1578 pos = strstr(value, "string_escape=");
1580 pos = &pos[strlen("string_escape=")];
1581 if (strncmp(pos, "none", strlen("none")) == 0)
1582 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1583 else if (strncmp(pos, "replace", strlen("replace")) == 0)
1584 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1587 pos = strstr(value, "db_persist");
1589 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1591 pos = strstr(value, "nowatch");
1595 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1597 pos = strstr(value, "watch");
1601 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1605 pos = strstr(value, "static_node=");
1607 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1608 rule_tmp.rule.rule.has_static_node = true;
1614 log_error("unknown key '%s' in %s:%u\n", key, filename, lineno);
1618 /* add rule token */
1619 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1620 if (add_token(rules, &rule_tmp.rule) != 0)
1623 /* add tokens to list, sorted by type */
1624 if (sort_token(rules, &rule_tmp) != 0)
1629 log_error("invalid rule '%s:%u'\n", filename, lineno);
1633 static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off)
1636 unsigned int first_token;
1637 char line[UTIL_LINE_SIZE];
1641 log_debug("reading '%s' as rules file\n", filename);
1643 f = fopen(filename, "r");
1647 first_token = rules->token_cur;
1649 while (fgets(line, sizeof(line), f) != NULL) {
1653 /* skip whitespace */
1656 while (isspace(key[0]))
1667 /* continue reading if backslash+newline is found */
1668 while (line[len-2] == '\\') {
1669 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1671 if (strlen(&line[len-2]) < 2)
1677 if (len+1 >= sizeof(line)) {
1678 log_error("line too long '%s':%u, ignored\n", filename, line_nr);
1681 add_rule(rules, key, filename, filename_off, line_nr);
1685 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1686 for (i = first_token+1; i < rules->token_cur; i++) {
1687 if (rules->tokens[i].type == TK_A_GOTO) {
1688 char *label = &rules->buf[rules->tokens[i].key.value_off];
1691 for (j = i+1; j < rules->token_cur; j++) {
1692 if (rules->tokens[j].type != TK_RULE)
1694 if (rules->tokens[j].rule.label_off == 0)
1696 if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0)
1698 rules->tokens[i].key.rule_goto = j;
1701 if (rules->tokens[i].key.rule_goto == 0)
1702 log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename);
1708 static int add_matching_files(struct udev *udev, struct udev_list *file_list, const char *dirname, const char *suffix)
1711 struct dirent *dent;
1712 char filename[UTIL_PATH_SIZE];
1714 dir = opendir(dirname);
1716 log_debug("unable to open '%s': %m\n", dirname);
1720 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1721 if (dent->d_name[0] == '.')
1724 /* look for file matching with specified suffix */
1725 if (suffix != NULL) {
1728 ext = strrchr(dent->d_name, '.');
1731 if (strcmp(ext, suffix) != 0)
1734 util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL);
1736 * the basename is the key, the filename the value
1737 * identical basenames from different directories override each other
1738 * entries are sorted after basename
1740 udev_list_entry_add(file_list, dent->d_name, filename);
1747 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1749 struct udev_rules *rules;
1750 struct udev_list file_list;
1751 struct udev_list_entry *file_loop;
1752 struct token end_token;
1755 rules = calloc(1, sizeof(struct udev_rules));
1759 rules->resolve_names = resolve_names;
1760 udev_list_init(udev, &file_list, true);
1762 /* init token array and string buffer */
1763 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1764 if (rules->tokens == NULL) {
1768 rules->token_max = PREALLOC_TOKEN;
1770 rules->buf = malloc(PREALLOC_STRBUF);
1771 if (rules->buf == NULL) {
1772 free(rules->tokens);
1776 rules->buf_max = PREALLOC_STRBUF;
1777 /* offset 0 is always '\0' */
1778 rules->buf[0] = '\0';
1781 rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
1782 if (rules->trie_nodes == NULL) {
1784 free(rules->tokens);
1788 rules->trie_nodes_max = PREALLOC_TRIE;
1789 /* offset 0 is the trie root, with an empty string */
1790 memset(rules->trie_nodes, 0x00, sizeof(struct trie_node));
1791 rules->trie_nodes_cur = 1;
1793 for (udev_get_rules_path(udev, &s, NULL); *s != NULL; s++)
1794 add_matching_files(udev, &file_list, *s, ".rules");
1796 /* add all filenames to the string buffer */
1797 udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1798 const char *filename = udev_list_entry_get_value(file_loop);
1799 unsigned int filename_off;
1801 filename_off = add_string(rules, filename);
1802 /* the offset in the rule is limited to unsigned short */
1803 if (filename_off < USHRT_MAX)
1804 udev_list_entry_set_num(file_loop, filename_off);
1807 /* parse all rules files */
1808 udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1809 const char *filename = udev_list_entry_get_value(file_loop);
1810 unsigned int filename_off = udev_list_entry_get_num(file_loop);
1813 if (stat(filename, &st) != 0) {
1814 log_error("can not find '%s': %m\n", filename);
1817 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
1818 log_debug("ignore empty '%s'\n", filename);
1821 if (S_ISCHR(st.st_mode)) {
1822 log_debug("ignore masked '%s'\n", filename);
1825 parse_file(rules, filename, filename_off);
1827 udev_list_cleanup(&file_list);
1829 memset(&end_token, 0x00, sizeof(struct token));
1830 end_token.type = TK_END;
1831 add_token(rules, &end_token);
1833 /* shrink allocated token and string buffer */
1834 if (rules->token_cur < rules->token_max) {
1835 struct token *tokens;
1837 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1838 if (tokens != NULL || rules->token_cur == 0) {
1839 rules->tokens = tokens;
1840 rules->token_max = rules->token_cur;
1843 if (rules->buf_cur < rules->buf_max) {
1846 buf = realloc(rules->buf, rules->buf_cur);
1847 if (buf != NULL || rules->buf_cur == 0) {
1849 rules->buf_max = rules->buf_cur;
1852 log_debug("rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1853 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1854 log_debug("temporary index used %zu bytes (%u * %zu bytes)\n",
1855 rules->trie_nodes_cur * sizeof(struct trie_node),
1856 rules->trie_nodes_cur, sizeof(struct trie_node));
1859 free(rules->trie_nodes);
1860 rules->trie_nodes = NULL;
1861 rules->trie_nodes_cur = 0;
1862 rules->trie_nodes_max = 0;
1864 /* cleanup uid/gid cache */
1867 rules->uids_cur = 0;
1868 rules->uids_max = 0;
1871 rules->gids_cur = 0;
1872 rules->gids_max = 0;
1878 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1882 free(rules->tokens);
1884 free(rules->trie_nodes);
1891 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1893 char *key_value = &rules->buf[token->key.value_off];
1900 switch (token->key.glob) {
1902 match = (strcmp(key_value, val) == 0);
1905 match = (fnmatch(key_value, val, 0) == 0);
1912 split = &rules->buf[token->key.value_off];
1917 next = strchr(split, '|');
1919 size_t matchlen = (size_t)(next - split);
1921 match = (matchlen == len && strncmp(split, val, matchlen) == 0);
1925 match = (strcmp(split, val) == 0);
1934 char value[UTIL_PATH_SIZE];
1936 util_strscpy(value, sizeof(value), &rules->buf[token->key.value_off]);
1938 while (key_value != NULL) {
1939 pos = strchr(key_value, '|');
1944 match = (fnmatch(key_value, val, 0) == 0);
1952 match = (val[0] != '\0');
1958 if (match && (token->key.op == OP_MATCH))
1960 if (!match && (token->key.op == OP_NOMATCH))
1965 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1968 char nbuf[UTIL_NAME_SIZE];
1970 char vbuf[UTIL_NAME_SIZE];
1973 name = &rules->buf[cur->key.attr_off];
1974 switch (cur->key.attrsubst) {
1976 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1980 value = udev_device_get_sysattr_value(dev, name);
1985 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1993 /* remove trailing whitespace, if not asked to match for it */
1994 len = strlen(value);
1995 if (len > 0 && isspace(value[len-1])) {
1996 const char *key_value;
1999 key_value = &rules->buf[cur->key.value_off];
2000 klen = strlen(key_value);
2001 if (klen > 0 && !isspace(key_value[klen-1])) {
2002 if (value != vbuf) {
2003 util_strscpy(vbuf, sizeof(vbuf), value);
2006 while (len > 0 && isspace(vbuf[--len]))
2011 return match_key(rules, cur, value);
2020 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
2024 enum escape_type esc = ESCAPE_UNSET;
2027 if (rules->tokens == NULL)
2030 can_set_name = ((strcmp(udev_device_get_action(event->dev), "remove") != 0) &&
2031 (major(udev_device_get_devnum(event->dev)) > 0 ||
2032 udev_device_get_ifindex(event->dev) > 0));
2034 /* loop through token list, match, run actions or forward to next rule */
2035 cur = &rules->tokens[0];
2038 dump_token(rules, cur);
2039 switch (cur->type) {
2043 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2044 if (!can_set_name && rule->rule.can_set_name)
2049 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
2053 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
2057 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
2060 case TK_M_DEVLINK: {
2061 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2062 struct udev_list_entry *list_entry;
2065 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
2066 const char *devlink;
2068 devlink = &udev_list_entry_get_name(list_entry)[devlen];
2069 if (match_key(rules, cur, devlink) == 0) {
2079 if (match_key(rules, cur, event->name) != 0)
2083 const char *key_name = &rules->buf[cur->key.attr_off];
2086 value = udev_device_get_property_value(event->dev, key_name);
2089 if (match_key(rules, cur, value))
2094 struct udev_list_entry *list_entry;
2097 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
2098 if (strcmp(&rules->buf[cur->key.value_off], udev_list_entry_get_name(list_entry)) == 0) {
2103 if (!match && (cur->key.op != OP_NOMATCH))
2107 case TK_M_SUBSYSTEM:
2108 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
2112 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2115 case TK_M_WAITFOR: {
2116 char filename[UTIL_PATH_SIZE];
2119 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2120 found = (wait_for_file(event->dev, filename, 10) == 0);
2121 if (!found && (cur->key.op != OP_NOMATCH))
2126 if (match_attr(rules, event->dev, event, cur) != 0)
2130 case TK_M_SUBSYSTEMS:
2136 /* get whole sequence of parent matches */
2138 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2141 /* loop over parents */
2142 event->dev_parent = event->dev;
2146 /* loop over sequence of parent match keys */
2147 for (key = cur; key < next; key++ ) {
2148 dump_token(rules, key);
2151 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2154 case TK_M_SUBSYSTEMS:
2155 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2159 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2163 if (match_attr(rules, event->dev_parent, event, key) != 0)
2167 bool match = udev_device_has_tag(event->dev_parent, &rules->buf[cur->key.value_off]);
2169 if (match && key->key.op == OP_NOMATCH)
2171 if (!match && key->key.op == OP_MATCH)
2182 event->dev_parent = udev_device_get_parent(event->dev_parent);
2183 if (event->dev_parent == NULL)
2186 /* move behind our sequence of parent match keys */
2191 char filename[UTIL_PATH_SIZE];
2192 struct stat statbuf;
2195 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2196 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2197 if (filename[0] != '/') {
2198 char tmp[UTIL_PATH_SIZE];
2200 util_strscpy(tmp, sizeof(tmp), filename);
2201 util_strscpyl(filename, sizeof(filename),
2202 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2205 attr_subst_subdir(filename, sizeof(filename));
2207 match = (stat(filename, &statbuf) == 0);
2208 if (match && cur->key.mode > 0)
2209 match = ((statbuf.st_mode & cur->key.mode) > 0);
2210 if (match && cur->key.op == OP_NOMATCH)
2212 if (!match && cur->key.op == OP_MATCH)
2216 case TK_M_EVENT_TIMEOUT:
2217 log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout);
2218 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2220 case TK_M_PROGRAM: {
2221 char program[UTIL_PATH_SIZE];
2223 char result[UTIL_PATH_SIZE];
2225 free(event->program_result);
2226 event->program_result = NULL;
2227 udev_event_apply_format(event, &rules->buf[cur->key.value_off], program, sizeof(program));
2228 envp = udev_device_get_properties_envp(event->dev);
2229 log_debug("PROGRAM '%s' %s:%u\n",
2231 &rules->buf[rule->rule.filename_off],
2232 rule->rule.filename_line);
2234 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2235 if (cur->key.op != OP_NOMATCH)
2240 util_remove_trailing_chars(result, '\n');
2241 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2242 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2244 log_debug("%i character(s) replaced\n" , count);
2246 event->program_result = strdup(result);
2247 if (cur->key.op == OP_NOMATCH)
2252 case TK_M_IMPORT_FILE: {
2253 char import[UTIL_PATH_SIZE];
2255 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2256 if (import_file_into_properties(event->dev, import) != 0)
2257 if (cur->key.op != OP_NOMATCH)
2261 case TK_M_IMPORT_PROG: {
2262 char import[UTIL_PATH_SIZE];
2264 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2265 log_debug("IMPORT '%s' %s:%u\n",
2267 &rules->buf[rule->rule.filename_off],
2268 rule->rule.filename_line);
2270 if (import_program_into_properties(event, import, sigmask) != 0)
2271 if (cur->key.op != OP_NOMATCH)
2275 case TK_M_IMPORT_BUILTIN: {
2276 char command[UTIL_PATH_SIZE];
2278 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2279 /* check if we ran already */
2280 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2281 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2282 udev_builtin_name(cur->key.builtin_cmd),
2283 &rules->buf[rule->rule.filename_off],
2284 rule->rule.filename_line);
2285 /* return the result from earlier run */
2286 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2287 if (cur->key.op != OP_NOMATCH)
2292 event->builtin_run |= (1 << cur->key.builtin_cmd);
2295 udev_event_apply_format(event, &rules->buf[cur->key.value_off], command, sizeof(command));
2296 log_debug("IMPORT builtin '%s' %s:%u\n",
2297 udev_builtin_name(cur->key.builtin_cmd),
2298 &rules->buf[rule->rule.filename_off],
2299 rule->rule.filename_line);
2301 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2302 /* remember failure */
2303 log_debug("IMPORT builtin '%s' returned non-zero\n",
2304 udev_builtin_name(cur->key.builtin_cmd));
2305 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2306 if (cur->key.op != OP_NOMATCH)
2311 case TK_M_IMPORT_DB: {
2312 const char *key = &rules->buf[cur->key.value_off];
2315 value = udev_device_get_property_value(event->dev_db, key);
2316 if (value != NULL) {
2317 struct udev_list_entry *entry;
2319 entry = udev_device_add_property(event->dev, key, value);
2320 udev_list_entry_set_num(entry, true);
2322 if (cur->key.op != OP_NOMATCH)
2327 case TK_M_IMPORT_CMDLINE: {
2329 bool imported = false;
2331 f = fopen("/proc/cmdline", "r");
2335 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2336 const char *key = &rules->buf[cur->key.value_off];
2339 pos = strstr(cmdline, key);
2341 struct udev_list_entry *entry;
2344 if (pos[0] == '\0' || isspace(pos[0])) {
2345 /* we import simple flags as 'FLAG=1' */
2346 entry = udev_device_add_property(event->dev, key, "1");
2347 udev_list_entry_set_num(entry, true);
2349 } else if (pos[0] == '=') {
2354 while (pos[0] != '\0' && !isspace(pos[0]))
2357 entry = udev_device_add_property(event->dev, key, value);
2358 udev_list_entry_set_num(entry, true);
2365 if (!imported && cur->key.op != OP_NOMATCH)
2369 case TK_M_IMPORT_PARENT: {
2370 char import[UTIL_PATH_SIZE];
2372 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2373 if (import_parent_into_properties(event->dev, import) != 0)
2374 if (cur->key.op != OP_NOMATCH)
2379 if (match_key(rules, cur, event->program_result) != 0)
2382 case TK_A_STRING_ESCAPE_NONE:
2385 case TK_A_STRING_ESCAPE_REPLACE:
2386 esc = ESCAPE_REPLACE;
2388 case TK_A_DB_PERSIST:
2389 udev_device_set_db_persist(event->dev);
2391 case TK_A_INOTIFY_WATCH:
2392 if (event->inotify_watch_final)
2394 if (cur->key.op == OP_ASSIGN_FINAL)
2395 event->inotify_watch_final = true;
2396 event->inotify_watch = cur->key.watch;
2398 case TK_A_DEVLINK_PRIO:
2399 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2402 char owner[UTIL_NAME_SIZE];
2404 if (event->owner_final)
2406 if (cur->key.op == OP_ASSIGN_FINAL)
2407 event->owner_final = true;
2408 udev_event_apply_format(event, &rules->buf[cur->key.value_off], owner, sizeof(owner));
2409 event->uid = util_lookup_user(event->udev, owner);
2410 log_debug("OWNER %u %s:%u\n",
2412 &rules->buf[rule->rule.filename_off],
2413 rule->rule.filename_line);
2417 char group[UTIL_NAME_SIZE];
2419 if (event->group_final)
2421 if (cur->key.op == OP_ASSIGN_FINAL)
2422 event->group_final = true;
2423 udev_event_apply_format(event, &rules->buf[cur->key.value_off], group, sizeof(group));
2424 event->gid = util_lookup_group(event->udev, group);
2425 log_debug("GROUP %u %s:%u\n",
2427 &rules->buf[rule->rule.filename_off],
2428 rule->rule.filename_line);
2432 char mode_str[UTIL_NAME_SIZE];
2436 if (event->mode_final)
2438 udev_event_apply_format(event, &rules->buf[cur->key.value_off], mode_str, sizeof(mode_str));
2439 mode = strtol(mode_str, &endptr, 8);
2440 if (endptr[0] != '\0') {
2441 log_error("ignoring invalid mode '%s'\n", mode_str);
2444 if (cur->key.op == OP_ASSIGN_FINAL)
2445 event->mode_final = true;
2446 event->mode_set = true;
2448 log_debug("MODE %#o %s:%u\n",
2450 &rules->buf[rule->rule.filename_off],
2451 rule->rule.filename_line);
2455 if (event->owner_final)
2457 if (cur->key.op == OP_ASSIGN_FINAL)
2458 event->owner_final = true;
2459 event->uid = cur->key.uid;
2460 log_debug("OWNER %u %s:%u\n",
2462 &rules->buf[rule->rule.filename_off],
2463 rule->rule.filename_line);
2466 if (event->group_final)
2468 if (cur->key.op == OP_ASSIGN_FINAL)
2469 event->group_final = true;
2470 event->gid = cur->key.gid;
2471 log_debug("GROUP %u %s:%u\n",
2473 &rules->buf[rule->rule.filename_off],
2474 rule->rule.filename_line);
2477 if (event->mode_final)
2479 if (cur->key.op == OP_ASSIGN_FINAL)
2480 event->mode_final = true;
2481 event->mode_set = true;
2482 event->mode = cur->key.mode;
2483 log_debug("MODE %#o %s:%u\n",
2485 &rules->buf[rule->rule.filename_off],
2486 rule->rule.filename_line);
2489 const char *name = &rules->buf[cur->key.attr_off];
2490 char *value = &rules->buf[cur->key.value_off];
2492 if (value[0] != '\0') {
2493 char temp_value[UTIL_NAME_SIZE];
2494 struct udev_list_entry *entry;
2496 udev_event_apply_format(event, value, temp_value, sizeof(temp_value));
2497 entry = udev_device_add_property(event->dev, name, temp_value);
2498 /* store in db, skip private keys */
2500 udev_list_entry_set_num(entry, true);
2502 udev_device_add_property(event->dev, name, NULL);
2507 char tag[UTIL_PATH_SIZE];
2510 udev_event_apply_format(event, &rules->buf[cur->key.value_off], tag, sizeof(tag));
2511 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2512 udev_device_cleanup_tags_list(event->dev);
2513 for (p = tag; *p != '\0'; p++) {
2514 if ((*p >= 'a' && *p <= 'z') ||
2515 (*p >= 'A' && *p <= 'Z') ||
2516 (*p >= '0' && *p <= '9') ||
2517 *p == '-' || *p == '_')
2519 log_error("ignoring invalid tag name '%s'\n", tag);
2522 udev_device_add_tag(event->dev, tag);
2526 const char *name = &rules->buf[cur->key.value_off];
2528 char name_str[UTIL_PATH_SIZE];
2531 if (event->name_final)
2533 if (cur->key.op == OP_ASSIGN_FINAL)
2534 event->name_final = true;
2535 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2536 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2537 count = util_replace_chars(name_str, "/");
2539 log_debug("%i character(s) replaced\n", count);
2541 if (major(udev_device_get_devnum(event->dev))) {
2542 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2544 if (strcmp(name_str, &udev_device_get_devnode(event->dev)[devlen]) != 0) {
2545 log_error("NAME=\"%s\" ignored, kernel device nodes "
2546 "can not be renamed; please fix it in %s:%u\n", name,
2547 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2552 event->name = strdup(name_str);
2553 log_debug("NAME '%s' %s:%u\n",
2555 &rules->buf[rule->rule.filename_off],
2556 rule->rule.filename_line);
2559 case TK_A_DEVLINK: {
2560 char temp[UTIL_PATH_SIZE];
2561 char filename[UTIL_PATH_SIZE];
2565 if (event->devlink_final)
2567 if (major(udev_device_get_devnum(event->dev)) == 0)
2569 if (cur->key.op == OP_ASSIGN_FINAL)
2570 event->devlink_final = true;
2571 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2572 udev_device_cleanup_devlinks_list(event->dev);
2574 /* allow multiple symlinks separated by spaces */
2575 udev_event_apply_format(event, &rules->buf[cur->key.value_off], temp, sizeof(temp));
2576 if (esc == ESCAPE_UNSET)
2577 count = util_replace_chars(temp, "/ ");
2578 else if (esc == ESCAPE_REPLACE)
2579 count = util_replace_chars(temp, "/");
2581 log_debug("%i character(s) replaced\n" , count);
2583 while (isspace(pos[0]))
2585 next = strchr(pos, ' ');
2586 while (next != NULL) {
2588 log_debug("LINK '%s' %s:%u\n", pos,
2589 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2590 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2591 udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique);
2592 while (isspace(next[1]))
2595 next = strchr(pos, ' ');
2597 if (pos[0] != '\0') {
2598 log_debug("LINK '%s' %s:%u\n", pos,
2599 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2600 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2601 udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique);
2606 const char *key_name = &rules->buf[cur->key.attr_off];
2607 char attr[UTIL_PATH_SIZE];
2608 char value[UTIL_NAME_SIZE];
2611 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2612 util_strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2613 attr_subst_subdir(attr, sizeof(attr));
2615 udev_event_apply_format(event, &rules->buf[cur->key.value_off], value, sizeof(value));
2616 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
2617 &rules->buf[rule->rule.filename_off],
2618 rule->rule.filename_line);
2619 f = fopen(attr, "w");
2621 if (fprintf(f, "%s", value) <= 0)
2622 log_error("error writing ATTR{%s}: %m\n", attr);
2625 log_error("error opening ATTR{%s} for writing: %m\n", attr);
2629 case TK_A_RUN_BUILTIN:
2630 case TK_A_RUN_PROGRAM: {
2631 struct udev_list_entry *entry;
2633 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2634 udev_list_cleanup(&event->run_list);
2635 log_debug("RUN '%s' %s:%u\n",
2636 &rules->buf[cur->key.value_off],
2637 &rules->buf[rule->rule.filename_off],
2638 rule->rule.filename_line);
2639 entry = udev_list_entry_add(&event->run_list, &rules->buf[cur->key.value_off], NULL);
2640 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2644 if (cur->key.rule_goto == 0)
2646 cur = &rules->tokens[cur->key.rule_goto];
2651 case TK_M_PARENTS_MIN:
2652 case TK_M_PARENTS_MAX:
2655 log_error("wrong type %u\n", cur->type);
2662 /* fast-forward to next rule */
2663 cur = rule + rule->rule.token_count;
2667 void udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2675 if (rules->tokens == NULL)
2678 cur = &rules->tokens[0];
2681 switch (cur->type) {
2686 /* skip rules without a static_node tag */
2687 if (!rule->rule.has_static_node)
2701 mode = cur->key.mode;
2703 case TK_A_STATIC_NODE: {
2704 char filename[UTIL_PATH_SIZE];
2707 /* we assure, that the permissions tokens are sorted before the static token */
2708 if (mode == 0 && uid == 0 && gid == 0)
2710 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(rules->udev), "/",
2711 &rules->buf[cur->key.value_off], NULL);
2712 if (stat(filename, &stats) != 0)
2714 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2722 if (mode != (stats.st_mode & 01777)) {
2723 chmod(filename, mode);
2724 log_debug("chmod '%s' %#o\n", filename, mode);
2727 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2728 chown(filename, uid, gid);
2729 log_debug("chown '%s' %u %u\n", filename, uid, gid);
2732 utimensat(AT_FDCWD, filename, NULL, 0);
2742 /* fast-forward to next rule */
2743 cur = rule + rule->rule.token_count;