2 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #define PREALLOC_TOKEN 2048
33 #define PREALLOC_STRBUF 32 * 1024
34 #define PREALLOC_TRIE 256
37 unsigned int name_off;
44 #define TRIE_CHILD_MAX 10
47 unsigned int value_off;
49 unsigned char child_cur;
50 unsigned char child_key[TRIE_CHILD_MAX];
51 unsigned short child[TRIE_CHILD_MAX];
58 /* every key in the rules file becomes a token */
60 unsigned int token_cur;
61 unsigned int token_max;
63 /* all key strings are copied to a single string buffer */
67 unsigned int buf_count;
69 /* key strings are indexed to avoid wasting space with duplicates */
70 struct trie_node *trie;
71 unsigned short trie_cur;
72 unsigned short trie_max;
73 unsigned short *trie_root;
75 /* during rule parsing, we cache uid/gid lookup results */
77 unsigned int uids_cur;
78 unsigned int uids_max;
80 unsigned int gids_cur;
81 unsigned int gids_max;
84 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
97 enum string_glob_type {
99 GL_PLAIN, /* no special chars */
100 GL_GLOB, /* shell globs ?,*,[] */
101 GL_SPLIT, /* multi-value A|B */
102 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
103 GL_SOMETHING, /* commonly used "?*" */
107 /* tokens of a rule are sorted/handled in this order */
112 TK_M_ACTION, /* val */
113 TK_M_DEVPATH, /* val */
114 TK_M_KERNEL, /* val */
115 TK_M_DEVLINK, /* val */
117 TK_M_ENV, /* val, attr */
118 TK_M_SUBSYSTEM, /* val */
119 TK_M_DRIVER, /* val */
120 TK_M_WAITFOR, /* val */
121 TK_M_ATTR, /* val, attr */
124 TK_M_KERNELS, /* val */
125 TK_M_SUBSYSTEMS, /* val */
126 TK_M_DRIVERS, /* val */
127 TK_M_ATTRS, /* val, attr */
130 TK_M_TEST, /* val, mode_t */
131 TK_M_PROGRAM, /* val */
132 TK_M_IMPORT_FILE, /* val */
133 TK_M_IMPORT_PROG, /* val */
134 TK_M_IMPORT_PARENT, /* val */
135 TK_M_RESULT, /* val */
139 TK_A_STRING_ESCAPE_NONE,
140 TK_A_STRING_ESCAPE_REPLACE,
141 TK_A_NUM_FAKE_PART, /* int */
142 TK_A_DEVLINK_PRIO, /* int */
143 TK_A_OWNER, /* val */
144 TK_A_GROUP, /* val */
146 TK_A_OWNER_ID, /* uid_t */
147 TK_A_GROUP_ID, /* gid_t */
148 TK_A_MODE_ID, /* mode_t */
149 TK_A_ENV, /* val, attr */
151 TK_A_DEVLINK, /* val */
152 TK_A_EVENT_TIMEOUT, /* int */
154 TK_A_ATTR, /* val, attr */
155 TK_A_RUN, /* val, bool */
156 TK_A_GOTO, /* size_t */
162 /* we try to pack stuff in a way that we take only 12 bytes per token */
165 unsigned char type; /* same as in rule and key */
169 unsigned short token_count;
170 unsigned int label_off;
171 unsigned short filename_off;
172 unsigned short filename_line;
179 unsigned int value_off;
181 unsigned int attr_off;
184 unsigned int rule_goto;
198 struct udev_rules *rules;
200 struct token token[MAX_TK];
201 unsigned int token_cur;
205 static const char *operation_str(enum operation_type type)
207 static const char *operation_strs[] = {
208 [OP_UNSET] = "UNSET",
209 [OP_MATCH] = "match",
210 [OP_NOMATCH] = "nomatch",
211 [OP_MATCH_MAX] = "MATCH_MAX",
214 [OP_ASSIGN] = "assign",
215 [OP_ASSIGN_FINAL] = "assign-final",
218 return operation_strs[type];
221 static const char *string_glob_str(enum string_glob_type type)
223 static const char *string_glob_strs[] = {
224 [GL_UNSET] = "UNSET",
225 [GL_PLAIN] = "plain",
227 [GL_SPLIT] = "split",
228 [GL_SPLIT_GLOB] = "split-glob",
229 [GL_SOMETHING] = "split-glob",
230 [GL_FORMAT] = "format",
233 return string_glob_strs[type];
236 static const char *token_str(enum token_type type)
238 static const char *token_strs[] = {
239 [TK_UNSET] = "UNSET",
242 [TK_M_ACTION] = "M ACTION",
243 [TK_M_DEVPATH] = "M DEVPATH",
244 [TK_M_KERNEL] = "M KERNEL",
245 [TK_M_DEVLINK] = "M DEVLINK",
246 [TK_M_NAME] = "M NAME",
247 [TK_M_ENV] = "M ENV",
248 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
249 [TK_M_DRIVER] = "M DRIVER",
250 [TK_M_WAITFOR] = "M WAITFOR",
251 [TK_M_ATTR] = "M ATTR",
253 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
254 [TK_M_KERNELS] = "M KERNELS",
255 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
256 [TK_M_DRIVERS] = "M DRIVERS",
257 [TK_M_ATTRS] = "M ATTRS",
258 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
260 [TK_M_TEST] = "M TEST",
261 [TK_M_PROGRAM] = "M PROGRAM",
262 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
263 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
264 [TK_M_IMPORT_PARENT] = "M MPORT_PARENT",
265 [TK_M_RESULT] = "M RESULT",
266 [TK_M_MAX] = "M MAX",
268 [TK_A_IGNORE_DEVICE] = "A IGNORE_DEVICE",
269 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
270 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
271 [TK_A_NUM_FAKE_PART] = "A NUM_FAKE_PART",
272 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
273 [TK_A_OWNER] = "A OWNER",
274 [TK_A_GROUP] = "A GROUP",
275 [TK_A_MODE] = "A MODE",
276 [TK_A_OWNER_ID] = "A OWNER_ID",
277 [TK_A_GROUP_ID] = "A GROUP_ID",
278 [TK_A_MODE_ID] = "A MODE_ID",
279 [TK_A_ENV] = "A ENV",
280 [TK_A_NAME] = "A NAME",
281 [TK_A_DEVLINK] = "A DEVLINK",
282 [TK_A_EVENT_TIMEOUT] = "A EVENT_TIMEOUT",
283 [TK_A_IGNORE_REMOVE] = "A IGNORE_REMOVE",
284 [TK_A_ATTR] = "A ATTR",
285 [TK_A_RUN] = "A RUN",
286 [TK_A_GOTO] = "A GOTO",
287 [TK_A_LAST_RULE] = "A LAST_RULE",
292 return token_strs[type];
295 static void dump_token(struct udev_rules *rules, struct token *token)
297 enum token_type type = token->type;
298 enum operation_type op = token->key.op;
299 enum string_glob_type glob = token->key.glob;
300 const char *value = &rules->buf[token->key.value_off];
301 const char *attr = &rules->buf[token->key.attr_off];
306 const char *tks_ptr = (char *)rules->tokens;
307 const char *tk_ptr = (char *)token;
308 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
310 dbg(rules->udev, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n",
311 &rules->buf[token->rule.filename_off], token->rule.filename_line,
312 idx, token->rule.token_count,
313 &rules->buf[token->rule.label_off],
326 case TK_M_SUBSYSTEMS:
329 case TK_M_IMPORT_FILE:
330 case TK_M_IMPORT_PROG:
331 case TK_M_IMPORT_PARENT:
339 dbg(rules->udev, "%s %s '%s'(%s)\n",
340 token_str(type), operation_str(op), value, string_glob_str(glob));
347 dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
348 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
350 case TK_A_IGNORE_DEVICE:
351 case TK_A_STRING_ESCAPE_NONE:
352 case TK_A_STRING_ESCAPE_REPLACE:
354 case TK_A_IGNORE_REMOVE:
355 dbg(rules->udev, "%s\n", token_str(type));
358 dbg(rules->udev, "%s %s '%s'(%s) %#o\n",
359 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
361 case TK_A_NUM_FAKE_PART:
362 dbg(rules->udev, "%s %u\n", token_str(type), token->key.num_fake_part);
364 case TK_A_DEVLINK_PRIO:
365 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.devlink_prio);
368 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
371 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
374 dbg(rules->udev, "%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
376 case TK_A_EVENT_TIMEOUT:
377 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.event_timeout);
380 dbg(rules->udev, "%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
383 dbg(rules->udev, "* %s\n", token_str(type));
385 case TK_M_PARENTS_MIN:
386 case TK_M_PARENTS_MAX:
389 dbg(rules->udev, "unknown type %u\n", type);
394 static void dump_rules(struct udev_rules *rules)
398 dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
400 rules->token_cur * sizeof(struct token),
403 for(i = 0; i < rules->token_cur; i++)
404 dump_token(rules, &rules->tokens[i]);
407 static inline const char *operation_str(enum operation_type type) { return NULL; }
408 static inline const char *token_str(enum token_type type) { return NULL; }
409 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
410 static inline void dump_rules(struct udev_rules *rules) {}
413 static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
417 /* grow buffer if needed */
418 if (rules->buf_cur + bytes+1 >= rules->buf_max) {
422 /* double the buffer size */
423 add = rules->buf_max;
427 buf = realloc(rules->buf, rules->buf_max + add);
430 dbg(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add);
432 rules->buf_max += add;
434 off = rules->buf_cur;
435 memcpy(&rules->buf[rules->buf_cur], str, bytes);
436 rules->buf_cur += bytes;
441 static unsigned char trie_child_slot(struct trie_node *node, char key)
443 unsigned char child_slot;
445 for (child_slot = 0; child_slot < node->child_cur; child_slot++) {
446 if (node->child_key[child_slot] == key)
453 static int add_string(struct udev_rules *rules, const char *str)
455 struct trie_node *child;
456 unsigned short child_off;
457 unsigned short node_off;
465 /* offset 0 is always '\0' */
469 /* strings with spaces are probably commands e.g. modprobe,
470 with unique arguments. */
471 if (strchr(str, ' ') != NULL)
472 return add_new_string(rules, str, len + 1);
474 /* descend root - start from last character of str */
476 node_off = rules->trie_root[key];
479 /* descend suffix trie */
482 struct trie_node *node = &rules->trie[node_off];
483 unsigned char child_slot;
486 off = node->value_off + node->value_len - len;
488 /* match against current node */
490 (node->value_len >= len &&
491 memcmp(&rules->buf[off], str, len) == 0))
496 /* lookup child node */
497 key = str[len - 1 - depth];
498 child_slot = trie_child_slot(node, key);
500 if(child_slot == node->child_cur)
503 node_off = node->child[child_slot];
507 /* string not found, add it */
508 off = add_new_string(rules, str, len + 1);
510 /* grow trie storage if needed */
511 if (rules->trie_cur >= rules->trie_max) {
512 struct trie_node *trie;
515 /* double the buffer size */
516 add = rules->trie_max;
520 trie = realloc(rules->trie, (rules->trie_max + add) * sizeof(struct trie_node));
523 dbg(rules->udev, "extend string index nodes from %u to %u\n", rules->trie_max, rules->trie_max + add);
525 rules->trie_max += add;
528 /* insert new child node */
529 child_off = rules->trie_cur;
531 rules->trie_root[key] = child_off;
533 struct trie_node *parent = &rules->trie[node_off];
534 unsigned char child_slot = parent->child_cur;
536 /* no space in parent, we can't index this string, nevermind */
537 if (child_slot == TRIE_CHILD_MAX)
540 parent->child[child_slot] = child_off;
541 parent->child_key[child_slot] = key;
542 parent->child_cur = child_slot + 1;
545 /* allocate and construct the child node */
547 child = &rules->trie[child_off];
548 memset(child, 0x00, sizeof(struct trie_node));
549 child->value_off = off;
550 child->value_len = len;
555 static int add_token(struct udev_rules *rules, struct token *token)
557 /* grow buffer if needed */
558 if (rules->token_cur+1 >= rules->token_max) {
559 struct token *tokens;
562 /* double the buffer size */
563 add = rules->token_max;
567 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
570 dbg(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add);
571 rules->tokens = tokens;
572 rules->token_max += add;
574 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
579 static uid_t add_uid(struct udev_rules *rules, const char *owner)
585 /* lookup, if we know it already */
586 for (i = 0; i < rules->uids_cur; i++) {
587 off = rules->uids[i].name_off;
588 if (strcmp(&rules->buf[off], owner) == 0) {
589 uid = rules->uids[i].uid;
590 dbg(rules->udev, "return existing %u for '%s'\n", uid, owner);
594 uid = util_lookup_user(rules->udev, owner);
596 /* grow buffer if needed */
597 if (rules->uids_cur+1 >= rules->uids_max) {
598 struct uid_gid *uids;
601 /* double the buffer size */
602 add = rules->uids_max;
606 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
609 dbg(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add);
611 rules->uids_max += add;
613 rules->uids[rules->uids_cur].uid = uid;
614 off = add_string(rules, owner);
617 rules->uids[rules->uids_cur].name_off = off;
622 static gid_t add_gid(struct udev_rules *rules, const char *group)
628 /* lookup, if we know it already */
629 for (i = 0; i < rules->gids_cur; i++) {
630 off = rules->gids[i].name_off;
631 if (strcmp(&rules->buf[off], group) == 0) {
632 gid = rules->gids[i].gid;
633 dbg(rules->udev, "return existing %u for '%s'\n", gid, group);
637 gid = util_lookup_group(rules->udev, group);
639 /* grow buffer if needed */
640 if (rules->gids_cur+1 >= rules->gids_max) {
641 struct uid_gid *gids;
644 /* double the buffer size */
645 add = rules->gids_max;
649 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
652 dbg(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add);
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 info(udev, "inconsistent quoting: '%s', skip\n", line);
721 dbg(udev, "adding '%s'='%s'\n", key, val);
723 /* handle device, renamed by external tool, returning new path */
724 if (strcmp(key, "DEVPATH") == 0) {
725 char syspath[UTIL_PATH_SIZE];
727 info(udev, "updating devpath from '%s' to '%s'\n",
728 udev_device_get_devpath(dev), val);
729 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
730 util_strlcat(syspath, val, sizeof(syspath));
731 udev_device_set_syspath(dev, syspath);
733 struct udev_list_entry *entry;
735 entry = udev_device_add_property(dev, key, val);
737 udev_list_entry_set_flag(entry, 1);
742 static int import_file_into_properties(struct udev_device *dev, const char *filename)
745 char line[UTIL_LINE_SIZE];
747 f = fopen(filename, "r");
750 while (fgets(line, sizeof(line), f) != NULL)
751 import_property_from_string(dev, line);
756 static int import_program_into_properties(struct udev_device *dev, const char *program)
758 struct udev *udev = udev_device_get_udev(dev);
764 envp = udev_device_get_properties_envp(dev);
765 if (util_run_program(udev, program, envp, result, sizeof(result), &reslen) != 0)
769 while (line != NULL) {
772 pos = strchr(line, '\n');
777 import_property_from_string(dev, line);
783 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
785 struct udev *udev = udev_device_get_udev(dev);
786 struct udev_device *dev_parent;
787 struct udev_list_entry *list_entry;
789 dev_parent = udev_device_get_parent(dev);
790 if (dev_parent == NULL)
793 dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent));
794 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
795 const char *key = udev_list_entry_get_name(list_entry);
796 const char *val = udev_list_entry_get_value(list_entry);
798 if (fnmatch(filter, key, 0) == 0) {
799 struct udev_list_entry *entry;
801 dbg(udev, "import key '%s=%s'\n", key, val);
802 entry = udev_device_add_property(dev, key, val);
804 udev_list_entry_set_flag(entry, 1);
810 #define WAIT_LOOP_PER_SECOND 50
811 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
813 struct udev *udev = udev_device_get_udev(dev);
814 char filepath[UTIL_PATH_SIZE];
815 char devicepath[UTIL_PATH_SIZE];
817 int loop = timeout * WAIT_LOOP_PER_SECOND;
819 /* a relative path is a device attribute */
820 devicepath[0] = '\0';
821 if (file[0] != '/') {
822 util_strlcpy(devicepath, udev_get_sys_path(udev), sizeof(devicepath));
823 util_strlcat(devicepath, udev_device_get_devpath(dev), sizeof(devicepath));
825 util_strlcpy(filepath, devicepath, sizeof(filepath));
826 util_strlcat(filepath, "/", sizeof(filepath));
827 util_strlcat(filepath, file, sizeof(filepath));
831 dbg(udev, "will wait %i sec for '%s'\n", timeout, file);
834 if (stat(file, &stats) == 0) {
835 info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
838 /* make sure, the device did not disappear in the meantime */
839 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
840 info(udev, "device disappeared while waiting for '%s'\n", file);
843 info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
844 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
846 info(udev, "waiting for '%s' failed\n", file);
850 static int attr_subst_subdir(char *attr, size_t len)
855 pos = strstr(attr, "/*/");
857 char str[UTIL_PATH_SIZE];
861 util_strlcpy(str, &pos[2], sizeof(str));
866 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
869 if (dent->d_name[0] == '.')
871 util_strlcat(attr, dent->d_name, len);
872 util_strlcat(attr, str, len);
873 if (stat(attr, &stats) == 0) {
882 util_strlcat(attr, str, len);
888 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
894 if (linepos == NULL && linepos[0] == '\0')
897 /* skip whitespace */
898 while (isspace(linepos[0]) || linepos[0] == ',')
902 if (linepos[0] == '\0')
908 if (linepos[0] == '\0')
910 if (isspace(linepos[0]))
912 if (linepos[0] == '=')
914 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
915 if (linepos[1] == '=')
919 /* remember end of key */
922 /* skip whitespace after key */
923 while (isspace(linepos[0]))
925 if (linepos[0] == '\0')
928 /* get operation type */
929 if (linepos[0] == '=' && linepos[1] == '=') {
932 } else if (linepos[0] == '!' && linepos[1] == '=') {
935 } else if (linepos[0] == '+' && linepos[1] == '=') {
938 } else if (linepos[0] == '=') {
941 } else if (linepos[0] == ':' && linepos[1] == '=') {
942 *op = OP_ASSIGN_FINAL;
950 /* skip whitespace after operator */
951 while (isspace(linepos[0]))
953 if (linepos[0] == '\0')
957 if (linepos[0] == '"')
964 temp = strchr(linepos, '"');
969 dbg(udev, "%s '%s'-'%s'\n", operation_str(*op), *key, *value);
971 /* move line to next key */
976 /* extract possible KEY{attr} */
977 static char *get_key_attribute(struct udev *udev, char *str)
982 attr = strchr(str, '{');
985 pos = strchr(attr, '}');
987 err(udev, "missing closing brace for format\n");
991 dbg(udev, "attribute='%s'\n", attr);
997 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
998 enum operation_type op,
999 const char *value, const void *data)
1001 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
1002 const char *attr = data;
1003 enum string_glob_type glob;
1005 memset(token, 0x00, sizeof(struct token));
1011 case TK_M_SUBSYSTEM:
1017 case TK_M_SUBSYSTEMS:
1020 case TK_M_IMPORT_FILE:
1021 case TK_M_IMPORT_PROG:
1022 case TK_M_IMPORT_PARENT:
1030 token->key.value_off = add_string(rule_tmp->rules, value);
1037 token->key.value_off = add_string(rule_tmp->rules, value);
1038 token->key.attr_off = add_string(rule_tmp->rules, attr);
1041 token->key.value_off = add_string(rule_tmp->rules, value);
1043 token->key.mode = *(mode_t *)data;
1045 case TK_A_IGNORE_DEVICE:
1046 case TK_A_STRING_ESCAPE_NONE:
1047 case TK_A_STRING_ESCAPE_REPLACE:
1048 case TK_A_IGNORE_REMOVE:
1049 case TK_A_LAST_RULE:
1052 token->key.value_off = add_string(rule_tmp->rules, value);
1053 token->key.ignore_error = *(int *)data;
1055 case TK_A_NUM_FAKE_PART:
1056 token->key.num_fake_part = *(int *)data;
1058 case TK_A_DEVLINK_PRIO:
1059 token->key.devlink_prio = *(int *)data;
1062 token->key.uid = *(uid_t *)data;
1065 token->key.gid = *(gid_t *)data;
1068 token->key.mode = *(mode_t *)data;
1070 case TK_A_EVENT_TIMEOUT:
1071 token->key.event_timeout = *(int *)data;
1074 case TK_M_PARENTS_MIN:
1075 case TK_M_PARENTS_MAX:
1079 err(rule_tmp->rules->udev, "wrong type %u\n", type);
1084 if (value != NULL) {
1085 if (type < TK_M_MAX) {
1086 /* check if we need to split or call fnmatch() while matching rules */
1090 has_split = (strchr(value, '|') != NULL);
1091 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL ||
1092 strchr(value, '[') != NULL || strchr(value, ']') != NULL);
1093 if (has_split && has_glob) {
1094 glob = GL_SPLIT_GLOB;
1095 } else if (has_split) {
1097 } else if (has_glob) {
1098 if (strcmp(value, "?*") == 0)
1099 glob = GL_SOMETHING;
1104 /* check if we need to substitute format strings for matching rules */
1105 if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
1110 token->key.type = type;
1112 token->key.glob = glob;
1113 rule_tmp->token_cur++;
1114 if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) {
1115 err(rule_tmp->rules->udev, "temporary rule array too small\n");
1121 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1124 unsigned int start = 0;
1125 unsigned int end = rule_tmp->token_cur;
1127 for (i = 0; i < rule_tmp->token_cur; i++) {
1128 enum token_type next_val = TK_UNSET;
1129 unsigned int next_idx;
1132 /* find smallest value */
1133 for (j = start; j < end; j++) {
1134 if (rule_tmp->token[j].type == TK_UNSET)
1136 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1137 next_val = rule_tmp->token[j].type;
1142 /* add token and mark done */
1143 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1145 rule_tmp->token[next_idx].type = TK_UNSET;
1148 if (next_idx == start)
1150 if (next_idx+1 == end)
1156 static int add_rule(struct udev_rules *rules, char *line,
1157 const char *filename, unsigned int filename_off, unsigned int lineno)
1163 struct rule_tmp rule_tmp;
1165 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1166 rule_tmp.rules = rules;
1167 rule_tmp.rule.type = TK_RULE;
1168 rule_tmp.rule.rule.filename_off = filename_off;
1169 rule_tmp.rule.rule.filename_line = lineno;
1175 enum operation_type op;
1177 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1180 if (strcasecmp(key, "ACTION") == 0) {
1181 if (op > OP_MATCH_MAX) {
1182 err(rules->udev, "invalid ACTION operation\n");
1185 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1190 if (strcasecmp(key, "DEVPATH") == 0) {
1191 if (op > OP_MATCH_MAX) {
1192 err(rules->udev, "invalid DEVPATH operation\n");
1195 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1200 if (strcasecmp(key, "KERNEL") == 0) {
1201 if (op > OP_MATCH_MAX) {
1202 err(rules->udev, "invalid KERNEL operation\n");
1205 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1210 if (strcasecmp(key, "SUBSYSTEM") == 0) {
1211 if (op > OP_MATCH_MAX) {
1212 err(rules->udev, "invalid SUBSYSTEM operation\n");
1215 /* bus, class, subsystem events should all be the same */
1216 if (strcmp(value, "subsystem") == 0 ||
1217 strcmp(value, "bus") == 0 ||
1218 strcmp(value, "class") == 0) {
1219 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
1220 err(rules->udev, "'%s' must be specified as 'subsystem' \n"
1221 "please fix it in %s:%u", value, filename, lineno);
1222 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1224 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1229 if (strcasecmp(key, "DRIVER") == 0) {
1230 if (op > OP_MATCH_MAX) {
1231 err(rules->udev, "invalid DRIVER operation\n");
1234 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1239 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
1240 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1242 err(rules->udev, "error parsing ATTR attribute\n");
1245 if (op < OP_MATCH_MAX) {
1246 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1248 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1254 if (strcasecmp(key, "KERNELS") == 0 ||
1255 strcasecmp(key, "ID") == 0) {
1256 if (op > OP_MATCH_MAX) {
1257 err(rules->udev, "invalid KERNELS operation\n");
1260 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1265 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
1266 strcasecmp(key, "BUS") == 0) {
1267 if (op > OP_MATCH_MAX) {
1268 err(rules->udev, "invalid SUBSYSTEMS operation\n");
1271 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1276 if (strcasecmp(key, "DRIVERS") == 0) {
1277 if (op > OP_MATCH_MAX) {
1278 err(rules->udev, "invalid DRIVERS operation\n");
1281 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1286 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1287 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1288 if (op > OP_MATCH_MAX) {
1289 err(rules->udev, "invalid ATTRS operation\n");
1292 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1294 err(rules->udev, "error parsing ATTRS attribute\n");
1297 if (strncmp(attr, "device/", 7) == 0)
1298 err(rules->udev, "the 'device' link may not be available in a future kernel, "
1299 "please fix it in %s:%u", filename, lineno);
1300 else if (strstr(attr, "../") != NULL)
1301 err(rules->udev, "do not reference parent sysfs directories directly, "
1302 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1303 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1308 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
1309 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1311 err(rules->udev, "error parsing ENV attribute\n");
1314 if (strncmp(attr, "PHYSDEV", 7) == 0)
1316 if (op < OP_MATCH_MAX) {
1317 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1320 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1327 if (strcasecmp(key, "PROGRAM") == 0) {
1328 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1333 if (strcasecmp(key, "RESULT") == 0) {
1334 if (op > OP_MATCH_MAX) {
1335 err(rules->udev, "invalid RESULT operation\n");
1338 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1343 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
1344 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1345 if (attr != NULL && strstr(attr, "program")) {
1346 dbg(rules->udev, "IMPORT will be executed\n");
1347 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1349 } else if (attr != NULL && strstr(attr, "file")) {
1350 dbg(rules->udev, "IMPORT will be included as file\n");
1351 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1353 } else if (attr != NULL && strstr(attr, "parent")) {
1354 dbg(rules->udev, "IMPORT will include the parent values\n");
1355 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1358 /* figure it out if it is executable */
1359 char file[UTIL_PATH_SIZE];
1361 struct stat statbuf;
1363 util_strlcpy(file, value, sizeof(file));
1364 pos = strchr(file, ' ');
1368 /* allow programs in /lib/udev called without the path */
1369 if (strchr(file, '/') == NULL) {
1370 util_strlcpy(file, UDEV_PREFIX "/lib/udev/", sizeof(file));
1371 util_strlcat(file, value, sizeof(file));
1372 pos = strchr(file, ' ');
1377 dbg(rules->udev, "IMPORT auto mode for '%s'\n", file);
1378 if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
1379 dbg(rules->udev, "IMPORT will be executed (autotype)\n");
1380 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1383 dbg(rules->udev, "IMPORT will be included as file (autotype)\n");
1384 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1391 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
1394 if (op > OP_MATCH_MAX) {
1395 err(rules->udev, "invalid TEST operation\n");
1398 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1400 mode = strtol(attr, NULL, 8);
1401 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1403 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1409 if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
1412 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1413 if (attr != NULL && strstr(attr, "ignore_error"))
1415 rule_add_key(&rule_tmp, TK_A_RUN, op, value, &flag);
1420 if (strcasecmp(key, "WAIT_FOR") == 0 || strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
1421 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1426 if (strcasecmp(key, "LABEL") == 0) {
1427 rule_tmp.rule.rule.label_off = add_string(rules, value);
1432 if (strcasecmp(key, "GOTO") == 0) {
1433 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1438 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
1439 if (op < OP_MATCH_MAX) {
1440 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1442 if (value[0] == '\0')
1443 dbg(rules->udev, "name empty, node creation suppressed\n");
1444 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1445 attr = get_key_attribute(rules->udev, key + sizeof("NAME")-1);
1447 if (strstr(attr, "all_partitions") != NULL) {
1448 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1450 dbg(rules->udev, "creation of partition nodes requested\n");
1451 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1453 if (strstr(attr, "ignore_remove") != NULL) {
1454 dbg(rules->udev, "remove event should be ignored\n");
1455 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1459 rule_tmp.rule.rule.flags = 1;
1463 if (strcasecmp(key, "SYMLINK") == 0) {
1464 if (op < OP_MATCH_MAX)
1465 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1467 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1468 rule_tmp.rule.rule.flags = 1;
1473 if (strcasecmp(key, "OWNER") == 0) {
1477 uid = strtoul(value, &endptr, 10);
1478 if (endptr[0] == '\0') {
1479 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1480 } else if (rules->resolve_names && strchr("$%", value[0]) == NULL) {
1481 uid = add_uid(rules, value);
1482 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1484 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1486 rule_tmp.rule.rule.flags = 1;
1491 if (strcasecmp(key, "GROUP") == 0) {
1495 gid = strtoul(value, &endptr, 10);
1496 if (endptr[0] == '\0') {
1497 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1498 } else if (rules->resolve_names && strchr("$%", value[0]) == NULL) {
1499 gid = add_gid(rules, value);
1500 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1502 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1504 rule_tmp.rule.rule.flags = 1;
1509 if (strcasecmp(key, "MODE") == 0) {
1513 mode = strtol(value, &endptr, 8);
1514 if (endptr[0] == '\0')
1515 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1517 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1518 rule_tmp.rule.rule.flags = 1;
1523 if (strcasecmp(key, "OPTIONS") == 0) {
1526 if (strstr(value, "last_rule") != NULL) {
1527 dbg(rules->udev, "last rule to be applied\n");
1528 rule_add_key(&rule_tmp, TK_A_LAST_RULE, 0, NULL, NULL);
1530 if (strstr(value, "ignore_device") != NULL) {
1531 dbg(rules->udev, "device should be ignored\n");
1532 rule_add_key(&rule_tmp, TK_A_IGNORE_DEVICE, 0, NULL, NULL);
1534 if (strstr(value, "ignore_remove") != NULL) {
1535 dbg(rules->udev, "remove event should be ignored\n");
1536 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1538 pos = strstr(value, "link_priority=");
1540 int prio = atoi(&pos[strlen("link_priority=")]);
1542 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, 0, NULL, &prio);
1543 dbg(rules->udev, "link priority=%i\n", prio);
1545 pos = strstr(value, "event_timeout=");
1547 int tout = atoi(&pos[strlen("event_timeout=")]);
1549 rule_add_key(&rule_tmp, TK_A_EVENT_TIMEOUT, 0, NULL, &tout);
1550 dbg(rules->udev, "event timout=%i\n", tout);
1552 pos = strstr(value, "string_escape=");
1554 pos = &pos[strlen("string_escape=")];
1555 if (strncmp(pos, "none", strlen("none")) == 0)
1556 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, 0, NULL, NULL);
1557 else if (strncmp(pos, "replace", strlen("replace")) == 0)
1558 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, 0, NULL, NULL);
1560 if (strstr(value, "all_partitions") != NULL) {
1561 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1563 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1564 dbg(rules->udev, "creation of partition nodes requested\n");
1569 err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno);
1573 err(rules->udev, "PHYSDEV* values are deprecated and not available on recent kernels, \n"
1574 "please fix it in %s:%u", filename, lineno);
1576 /* skip line if not any valid key was found */
1580 /* add rule token */
1581 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1582 if (add_token(rules, &rule_tmp.rule) != 0)
1585 /* add tokens to list, sorted by type */
1586 if (sort_token(rules, &rule_tmp) != 0)
1591 err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
1595 static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off)
1598 unsigned int first_token;
1599 char line[UTIL_LINE_SIZE];
1603 info(rules->udev, "reading '%s' as rules file\n", filename);
1605 f = fopen(filename, "r");
1609 first_token = rules->token_cur;
1611 while(fgets(line, sizeof(line), f) != NULL) {
1615 /* skip whitespace */
1618 while (isspace(key[0]))
1629 /* continue reading if backslash+newline is found */
1630 while (line[len-2] == '\\') {
1631 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1637 if (len+1 >= sizeof(line)) {
1638 err(rules->udev, "line too long '%s':%u, ignored\n", filename, line_nr);
1641 add_rule(rules, key, filename, filename_off, line_nr);
1645 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1646 for (i = first_token+1; i < rules->token_cur; i++) {
1647 if (rules->tokens[i].type == TK_A_GOTO) {
1648 char *label = &rules->buf[rules->tokens[i].key.value_off];
1651 for (j = i+1; j < rules->token_cur; j++) {
1652 if (rules->tokens[j].type != TK_RULE)
1654 if (rules->tokens[j].rule.label_off == 0)
1656 if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0)
1658 rules->tokens[i].key.rule_goto = j;
1660 if (rules->tokens[i].key.rule_goto == 0)
1661 err(rules->udev, "GOTO '%s' has no matching label in: '%s'\n", label, filename);
1667 static int add_matching_files(struct udev *udev, struct udev_list_node *file_list, const char *dirname, const char *suffix)
1670 char filename[UTIL_PATH_SIZE];
1672 dbg(udev, "open directory '%s'\n", dirname);
1673 dir = opendir(dirname);
1675 err(udev, "unable to open '%s': %m\n", dirname);
1680 struct dirent *dent;
1682 dent = readdir(dir);
1683 if (dent == NULL || dent->d_name[0] == '\0')
1686 if (dent->d_name[0] == '.')
1689 /* look for file matching with specified suffix */
1690 if (suffix != NULL) {
1693 ext = strrchr(dent->d_name, '.');
1696 if (strcmp(ext, suffix) != 0)
1699 dbg(udev, "put file '%s/%s' into list\n", dirname, dent->d_name);
1701 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
1702 filename[sizeof(filename)-1] = '\0';
1703 udev_list_entry_add(udev, file_list, filename, NULL, 1, 1);
1710 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1712 struct udev_rules *rules;
1713 struct stat statbuf;
1714 struct udev_list_node file_list;
1715 struct udev_list_entry *file_loop, *file_tmp;
1716 struct token end_token;
1718 rules = malloc(sizeof(struct udev_rules));
1721 memset(rules, 0x00, sizeof(struct udev_rules));
1723 rules->resolve_names = resolve_names;
1724 udev_list_init(&file_list);
1726 /* init token array and string buffer */
1727 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1728 if (rules->tokens == NULL)
1730 rules->token_max = PREALLOC_TOKEN;
1731 rules->buf = malloc(PREALLOC_STRBUF);
1732 if (rules->buf == NULL)
1734 rules->buf_max = PREALLOC_STRBUF;
1735 rules->trie = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
1736 if (rules->trie == NULL)
1738 rules->trie_max = PREALLOC_TRIE;
1739 rules->trie_root = calloc(UCHAR_MAX + 1, sizeof(unsigned short));
1740 /* offset 0 is always '\0' */
1741 rules->buf[0] = '\0';
1743 /* offset 0 is reserved for the null trie node */
1744 rules->trie_cur = 1;
1745 dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1746 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1748 if (udev_get_rules_path(udev) != NULL) {
1749 /* custom rules location for testing */
1750 add_matching_files(udev, &file_list, udev_get_rules_path(udev), ".rules");
1752 char filename[PATH_MAX];
1753 struct udev_list_node sort_list;
1754 struct udev_list_entry *sort_loop, *sort_tmp;
1756 /* read user/custom rules */
1757 add_matching_files(udev, &file_list, SYSCONFDIR "/udev/rules.d", ".rules");
1759 /* read dynamic/temporary rules */
1760 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
1761 util_strlcat(filename, "/.udev/rules.d", sizeof(filename));
1762 if (stat(filename, &statbuf) != 0) {
1763 util_create_path(udev, filename);
1764 udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
1765 mkdir(filename, 0755);
1766 udev_selinux_resetfscreatecon(udev);
1768 udev_list_init(&sort_list);
1769 add_matching_files(udev, &sort_list, filename, ".rules");
1771 /* read default rules */
1772 add_matching_files(udev, &sort_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules");
1774 /* sort all rules files by basename into list of files */
1775 udev_list_entry_foreach_safe(sort_loop, sort_tmp, udev_list_get_entry(&sort_list)) {
1776 const char *sort_name = udev_list_entry_get_name(sort_loop);
1777 const char *sort_base = strrchr(sort_name, '/');
1779 if (sort_base == NULL)
1781 /* sort entry into existing list */
1782 udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1783 const char *file_name = udev_list_entry_get_name(file_loop);
1784 const char *file_base = strrchr(file_name, '/');
1786 if (file_base == NULL)
1788 if (strcmp(file_base, sort_base) == 0) {
1789 info(udev, "rule file basename '%s' already added, ignoring '%s'\n",
1790 file_name, sort_name);
1791 udev_list_entry_delete(sort_loop);
1795 if (strcmp(file_base, sort_base) > 0) {
1796 /* found later file, insert before */
1797 udev_list_entry_remove(sort_loop);
1798 udev_list_entry_insert_before(sort_loop, file_loop);
1803 /* current file already handled */
1804 if (sort_loop == NULL)
1806 /* no later file, append to end of list */
1807 udev_list_entry_remove(sort_loop);
1808 udev_list_entry_append(sort_loop, &file_list);
1812 /* add all filenames to the string buffer */
1813 udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1814 const char *filename = udev_list_entry_get_name(file_loop);
1815 unsigned int filename_off;
1817 filename_off = add_string(rules, filename);
1818 /* the offset in the rule is limited to unsigned short */
1819 if (filename_off < USHRT_MAX)
1820 udev_list_entry_set_flag(file_loop, filename_off);
1823 /* parse list of files */
1824 udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1825 const char *filename = udev_list_entry_get_name(file_loop);
1826 unsigned int filename_off = udev_list_entry_get_flag(file_loop);
1828 if (stat(filename, &statbuf) == 0 && statbuf.st_size > 0)
1829 parse_file(rules, filename, filename_off);
1831 info(udev, "can not read '%s'\n", filename);
1832 udev_list_entry_delete(file_loop);
1835 memset(&end_token, 0x00, sizeof(struct token));
1836 end_token.type = TK_END;
1837 add_token(rules, &end_token);
1839 /* shrink allocated token and string buffer */
1840 if (rules->token_cur < rules->token_max) {
1841 struct token *tokens;
1843 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1844 if (tokens != NULL || rules->token_cur == 0) {
1845 rules->tokens = tokens;
1846 rules->token_max = rules->token_cur;
1849 if (rules->buf_cur < rules->buf_max) {
1852 buf = realloc(rules->buf, rules->buf_cur);
1853 if (buf != NULL || rules->buf_cur == 0) {
1855 rules->buf_max = rules->buf_cur;
1858 info(udev, "shrunk to %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1859 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1860 info(udev, "used %zu bytes of string index nodes (%hu * %zu bytes)\n",
1861 rules->trie_cur * sizeof(struct trie_node), rules->trie_cur, sizeof(struct trie_node));
1866 rules->trie_cur = 0;
1867 rules->trie_max = 0;
1868 free(rules->trie_root);
1869 rules->trie_root = NULL;
1871 /* cleanup uid/gid cache */
1874 rules->uids_cur = 0;
1875 rules->uids_max = 0;
1878 rules->gids_cur = 0;
1879 rules->gids_max = 0;
1885 void udev_rules_unref(struct udev_rules *rules)
1889 free(rules->tokens);
1892 free(rules->trie_root);
1898 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1900 char *key_value = &rules->buf[token->key.value_off];
1907 switch (token->key.glob) {
1909 match = (strcmp(key_value, val) == 0);
1912 match = (fnmatch(key_value, val, 0) == 0);
1919 split = &rules->buf[token->key.value_off];
1924 next = strchr(split, '|');
1926 size_t matchlen = (size_t)(next - split);
1928 match = (matchlen == len && strncmp(split, val, matchlen) == 0);
1932 match = (strcmp(split, val) == 0);
1941 char value[UTIL_PATH_SIZE];
1943 util_strlcpy(value, &rules->buf[token->key.value_off], sizeof(value));
1945 while (key_value != NULL) {
1946 pos = strchr(key_value, '|');
1951 dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str(token->type), key_value, val);
1952 match = (fnmatch(key_value, val, 0) == 0);
1960 match = (val[0] != '\0');
1967 if (match && (token->key.op == OP_MATCH)) {
1968 dbg(rules->udev, "%s is true (matching value)\n", token_str(token->type));
1971 if (!match && (token->key.op == OP_NOMATCH)) {
1972 dbg(rules->udev, "%s is true (non-matching value)\n", token_str(token->type));
1975 dbg(rules->udev, "%s is not true\n", token_str(token->type));
1979 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1981 const char *key_name = &rules->buf[cur->key.attr_off];
1982 const char *key_value = &rules->buf[cur->key.value_off];
1983 char value[UTIL_NAME_SIZE];
1987 if (key_name[0] == '[') {
1988 char attr[UTIL_PATH_SIZE];
1990 util_strlcpy(attr, key_name, sizeof(attr));
1991 util_resolve_subsys_kernel(event->udev, attr, value, sizeof(value), 1);
1993 if (value[0] == '\0') {
1996 val = udev_device_get_sysattr_value(dev, key_name);
1999 util_strlcpy(value, val, sizeof(value));
2002 /* strip trailing whitespace of value, if not asked to match for it */
2003 len = strlen(key_value);
2004 if (len > 0 && !isspace(key_value[len-1])) {
2005 len = strlen(value);
2006 while (len > 0 && isspace(value[--len]))
2008 dbg(rules->udev, "removed trailing whitespace from '%s'\n", value);
2010 return match_key(rules, cur, value);
2019 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event)
2023 enum escape_type esc = ESCAPE_UNSET;
2026 if (rules->tokens == NULL)
2029 can_set_name = ((strcmp(udev_device_get_action(event->dev), "add") == 0 ||
2030 strcmp(udev_device_get_action(event->dev), "change") == 0) &&
2031 (major(udev_device_get_devnum(event->dev)) > 0 ||
2032 strcmp(udev_device_get_subsystem(event->dev), "net") == 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.flags)
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)
2062 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2063 struct udev_list_entry *list_entry;
2066 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
2067 const char *devlink;
2069 devlink = &udev_list_entry_get_name(list_entry)[devlen];
2070 if (match_key(rules, cur, devlink) == 0) {
2080 if (match_key(rules, cur, event->name) != 0)
2085 struct udev_list_entry *list_entry;
2086 const char *key_name = &rules->buf[cur->key.attr_off];
2089 list_entry = udev_device_get_properties_list_entry(event->dev);
2090 list_entry = udev_list_entry_get_by_name(list_entry, key_name);
2091 value = udev_list_entry_get_value(list_entry);
2092 if (value == NULL) {
2093 dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name);
2096 if (match_key(rules, cur, value))
2100 case TK_M_SUBSYSTEM:
2101 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
2105 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2110 char filename[UTIL_PATH_SIZE];
2113 util_strlcpy(filename, &rules->buf[cur->key.value_off], sizeof(filename));
2114 udev_event_apply_format(event, filename, sizeof(filename));
2115 found = (wait_for_file(event->dev, filename, 10) == 0);
2116 if (!found && (cur->key.op != OP_NOMATCH))
2121 if (match_attr(rules, event->dev, event, cur) != 0)
2125 case TK_M_SUBSYSTEMS:
2131 /* get whole sequence of parent matches */
2133 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2136 /* loop over parents */
2137 event->dev_parent = event->dev;
2141 dbg(event->udev, "parent: '%s'\n", udev_device_get_syspath(event->dev_parent));
2142 /* loop over sequence of parent match keys */
2143 for (key = cur; key < next; key++ ) {
2144 dump_token(rules, key);
2147 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2150 case TK_M_SUBSYSTEMS:
2151 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2155 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2159 if (match_attr(rules, event->dev_parent, event, key) != 0)
2165 dbg(event->udev, "parent key matched\n");
2167 dbg(event->udev, "all parent keys matched\n");
2168 /* all keys matched */
2172 event->dev_parent = udev_device_get_parent(event->dev_parent);
2173 if (event->dev_parent == NULL)
2176 /* move behind our sequence of parent match keys */
2182 char filename[UTIL_PATH_SIZE];
2183 struct stat statbuf;
2186 util_strlcpy(filename, &rules->buf[cur->key.value_off], sizeof(filename));
2187 udev_event_apply_format(event, filename, sizeof(filename));
2188 if (util_resolve_subsys_kernel(event->udev, NULL, filename, sizeof(filename), 0) != 0)
2189 if (filename[0] != '/') {
2190 char tmp[UTIL_PATH_SIZE];
2192 util_strlcpy(tmp, udev_device_get_syspath(event->dev), sizeof(tmp));
2193 util_strlcat(tmp, "/", sizeof(tmp));
2194 util_strlcat(tmp, filename, sizeof(tmp));
2195 util_strlcpy(filename, tmp, sizeof(filename));
2198 attr_subst_subdir(filename, sizeof(filename));
2200 match = (stat(filename, &statbuf) == 0);
2201 dbg(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
2202 if (match && cur->key.mode > 0) {
2203 match = ((statbuf.st_mode & cur->key.mode) > 0);
2204 dbg(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
2205 match ? "matches" : "does not match", cur->key.mode);
2207 if (match && cur->key.op == OP_NOMATCH)
2209 if (!match && cur->key.op == OP_MATCH)
2215 char program[UTIL_PATH_SIZE];
2217 char result[UTIL_PATH_SIZE];
2219 free(event->program_result);
2220 event->program_result = NULL;
2221 util_strlcpy(program, &rules->buf[cur->key.value_off], sizeof(program));
2222 udev_event_apply_format(event, program, sizeof(program));
2223 envp = udev_device_get_properties_envp(event->dev);
2224 info(event->udev, "PROGRAM '%s' %s:%u\n",
2226 &rules->buf[rule->rule.filename_off],
2227 rule->rule.filename_line);
2228 if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
2229 if (cur->key.op != OP_NOMATCH)
2234 util_remove_trailing_chars(result, '\n');
2235 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2236 count = udev_util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2238 info(event->udev, "%i character(s) replaced\n" , count);
2240 event->program_result = strdup(result);
2241 dbg(event->udev, "storing result '%s'\n", event->program_result);
2242 if (cur->key.op == OP_NOMATCH)
2247 case TK_M_IMPORT_FILE:
2249 char import[UTIL_PATH_SIZE];
2251 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2252 udev_event_apply_format(event, import, sizeof(import));
2253 if (import_file_into_properties(event->dev, import) != 0)
2254 if (cur->key.op != OP_NOMATCH)
2258 case TK_M_IMPORT_PROG:
2260 char import[UTIL_PATH_SIZE];
2262 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2263 udev_event_apply_format(event, import, sizeof(import));
2264 info(event->udev, "IMPORT '%s' %s:%u\n",
2266 &rules->buf[rule->rule.filename_off],
2267 rule->rule.filename_line);
2268 if (import_program_into_properties(event->dev, import) != 0)
2269 if (cur->key.op != OP_NOMATCH)
2273 case TK_M_IMPORT_PARENT:
2275 char import[UTIL_PATH_SIZE];
2277 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2278 udev_event_apply_format(event, import, sizeof(import));
2279 if (import_parent_into_properties(event->dev, import) != 0)
2280 if (cur->key.op != OP_NOMATCH)
2285 if (match_key(rules, cur, event->program_result) != 0)
2289 case TK_A_IGNORE_DEVICE:
2290 event->ignore_device = 1;
2293 case TK_A_STRING_ESCAPE_NONE:
2296 case TK_A_STRING_ESCAPE_REPLACE:
2297 esc = ESCAPE_REPLACE;
2299 case TK_A_NUM_FAKE_PART:
2300 if (strcmp(udev_device_get_subsystem(event->dev), "block") != 0)
2302 if (udev_device_get_sysnum(event->dev) != NULL)
2304 udev_device_set_num_fake_partitions(event->dev, cur->key.num_fake_part);
2306 case TK_A_DEVLINK_PRIO:
2307 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2311 char owner[UTIL_NAME_SIZE];
2313 if (event->owner_final)
2315 if (cur->key.op == OP_ASSIGN_FINAL)
2316 event->owner_final = 1;
2317 util_strlcpy(owner, &rules->buf[cur->key.value_off], sizeof(owner));
2318 udev_event_apply_format(event, owner, sizeof(owner));
2319 event->uid = util_lookup_user(event->udev, owner);
2320 info(event->udev, "OWNER %u %s:%u\n",
2322 &rules->buf[rule->rule.filename_off],
2323 rule->rule.filename_line);
2328 char group[UTIL_NAME_SIZE];
2330 if (event->group_final)
2332 if (cur->key.op == OP_ASSIGN_FINAL)
2333 event->group_final = 1;
2334 util_strlcpy(group, &rules->buf[cur->key.value_off], sizeof(group));
2335 udev_event_apply_format(event, group, sizeof(group));
2336 event->gid = util_lookup_group(event->udev, group);
2337 info(event->udev, "GROUP %u %s:%u\n",
2339 &rules->buf[rule->rule.filename_off],
2340 rule->rule.filename_line);
2345 char mode[UTIL_NAME_SIZE];
2348 if (event->mode_final)
2350 if (cur->key.op == OP_ASSIGN_FINAL)
2351 event->mode_final = 1;
2352 util_strlcpy(mode, &rules->buf[cur->key.value_off], sizeof(mode));
2353 udev_event_apply_format(event, mode, sizeof(mode));
2354 event->mode = strtol(mode, &endptr, 8);
2355 if (endptr[0] != '\0') {
2356 err(event->udev, "invalide mode '%s' set default mode 0660\n", mode);
2359 info(event->udev, "MODE %#o %s:%u\n",
2361 &rules->buf[rule->rule.filename_off],
2362 rule->rule.filename_line);
2366 if (event->owner_final)
2368 if (cur->key.op == OP_ASSIGN_FINAL)
2369 event->owner_final = 1;
2370 event->uid = cur->key.uid;
2371 info(event->udev, "OWNER %u %s:%u\n",
2373 &rules->buf[rule->rule.filename_off],
2374 rule->rule.filename_line);
2377 if (event->group_final)
2379 if (cur->key.op == OP_ASSIGN_FINAL)
2380 event->group_final = 1;
2381 event->gid = cur->key.gid;
2382 info(event->udev, "GROUP %u %s:%u\n",
2384 &rules->buf[rule->rule.filename_off],
2385 rule->rule.filename_line);
2388 if (event->mode_final)
2390 if (cur->key.op == OP_ASSIGN_FINAL)
2391 event->mode_final = 1;
2392 event->mode = cur->key.mode;
2393 info(event->udev, "MODE %#o %s:%u\n",
2395 &rules->buf[rule->rule.filename_off],
2396 rule->rule.filename_line);
2400 const char *name = &rules->buf[cur->key.attr_off];
2401 char *value = &rules->buf[cur->key.value_off];
2403 if (value[0] != '\0') {
2404 char temp_value[UTIL_NAME_SIZE];
2405 struct udev_list_entry *entry;
2407 util_strlcpy(temp_value, value, sizeof(temp_value));
2408 udev_event_apply_format(event, temp_value, sizeof(temp_value));
2409 entry = udev_device_add_property(event->dev, name, temp_value);
2411 udev_list_entry_set_flag(entry, 1);
2413 udev_device_add_property(event->dev, name, NULL);
2419 const char *name = &rules->buf[cur->key.value_off];
2420 char name_str[UTIL_PATH_SIZE];
2423 if (event->name_final)
2425 if (cur->key.op == OP_ASSIGN_FINAL)
2426 event->name_final = 1;
2427 if (name[0] == '\0') {
2432 util_strlcpy(name_str, name, sizeof(name_str));
2433 udev_event_apply_format(event, name_str, sizeof(name_str));
2434 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2435 count = udev_util_replace_chars(name_str, "/");
2437 info(event->udev, "%i character(s) replaced\n", count);
2439 event->name = strdup(name_str);
2440 info(event->udev, "NAME '%s' %s:%u\n",
2442 &rules->buf[rule->rule.filename_off],
2443 rule->rule.filename_line);
2449 char temp[UTIL_PATH_SIZE];
2450 char filename[UTIL_PATH_SIZE];
2454 if (event->devlink_final)
2456 if (major(udev_device_get_devnum(event->dev)) == 0)
2458 if (cur->key.op == OP_ASSIGN_FINAL)
2459 event->devlink_final = 1;
2460 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2461 udev_device_cleanup_devlinks_list(event->dev);
2463 /* allow multiple symlinks separated by spaces */
2464 util_strlcpy(temp, &rules->buf[cur->key.value_off], sizeof(temp));
2465 udev_event_apply_format(event, temp, sizeof(temp));
2466 if (esc == ESCAPE_UNSET)
2467 count = udev_util_replace_chars(temp, "/ ");
2468 else if (esc == ESCAPE_REPLACE)
2469 count = udev_util_replace_chars(temp, "/");
2471 info(event->udev, "%i character(s) replaced\n" , count);
2472 dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
2474 while (isspace(pos[0]))
2476 next = strchr(pos, ' ');
2479 info(event->udev, "LINK '%s' %s:%u\n",
2481 &rules->buf[rule->rule.filename_off],
2482 rule->rule.filename_line);
2483 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
2484 util_strlcat(filename, "/", sizeof(filename));
2485 util_strlcat(filename, pos, sizeof(filename));
2486 udev_device_add_devlink(event->dev, filename);
2487 while (isspace(next[1]))
2490 next = strchr(pos, ' ');
2492 if (pos[0] != '\0') {
2493 info(event->udev, "LINK '%s' %s:%u\n",
2495 &rules->buf[rule->rule.filename_off],
2496 rule->rule.filename_line);
2497 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
2498 util_strlcat(filename, "/", sizeof(filename));
2499 util_strlcat(filename, pos, sizeof(filename));
2500 udev_device_add_devlink(event->dev, filename);
2504 case TK_A_EVENT_TIMEOUT:
2505 udev_device_set_event_timeout(event->dev, cur->key.event_timeout);
2507 case TK_A_IGNORE_REMOVE:
2508 udev_device_set_ignore_remove(event->dev, 1);
2512 const char *key_name = &rules->buf[cur->key.attr_off];
2513 char attr[UTIL_PATH_SIZE];
2514 char value[UTIL_NAME_SIZE];
2517 util_strlcpy(attr, key_name, sizeof(attr));
2518 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0) {
2519 util_strlcpy(attr, udev_device_get_syspath(event->dev), sizeof(attr));
2520 util_strlcat(attr, "/", sizeof(attr));
2521 util_strlcat(attr, key_name, sizeof(attr));
2524 attr_subst_subdir(attr, sizeof(attr));
2526 util_strlcpy(value, &rules->buf[cur->key.value_off], sizeof(value));
2527 udev_event_apply_format(event, value, sizeof(value));
2528 info(event->udev, "ATTR '%s' writing '%s' %s:%u\n", attr, value,
2529 &rules->buf[rule->rule.filename_off],
2530 rule->rule.filename_line);
2531 f = fopen(attr, "w");
2534 if (fprintf(f, "%s", value) <= 0)
2535 err(event->udev, "error writing ATTR{%s}: %m\n", attr);
2538 err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);
2544 struct udev_list_entry *list_entry;
2546 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2547 udev_list_cleanup_entries(event->udev, &event->run_list);
2548 info(event->udev, "RUN '%s' %s:%u\n",
2549 &rules->buf[cur->key.value_off],
2550 &rules->buf[rule->rule.filename_off],
2551 rule->rule.filename_line);
2552 list_entry = udev_list_entry_add(event->udev, &event->run_list,
2553 &rules->buf[cur->key.value_off], NULL, 1, 0);
2554 if (cur->key.ignore_error)
2555 udev_list_entry_set_flag(list_entry, 1);
2559 cur = &rules->tokens[cur->key.rule_goto];
2561 case TK_A_LAST_RULE:
2565 case TK_M_PARENTS_MIN:
2566 case TK_M_PARENTS_MAX:
2569 err(rules->udev, "wrong type %u\n", cur->type);
2576 /* fast-forward to next rule */
2577 cur = rule + rule->rule.token_count;
2578 dbg(rules->udev, "forward to rule: %u\n",
2579 (unsigned int) (cur - rules->tokens));