2 * Copyright (C) 2008 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/>.
33 #define PREALLOC_TOKEN 2048
34 #define PREALLOC_STRBUF 32 * 1024
35 #define PREALLOC_TRIE 256
38 unsigned int name_off;
46 /* this node's first child */
47 unsigned int child_idx;
48 /* the next child of our parent node's child list */
49 unsigned int next_child_idx;
50 /* this node's last child (shortcut for append) */
51 unsigned int last_child_idx;
52 unsigned int value_off;
53 unsigned short value_len;
61 /* every key in the rules file becomes a token */
63 unsigned int token_cur;
64 unsigned int token_max;
66 /* all key strings are copied to a single string buffer */
70 unsigned int buf_count;
72 /* during rule parsing, strings are indexed to find duplicates */
73 struct trie_node *trie_nodes;
74 unsigned int trie_nodes_cur;
75 unsigned int trie_nodes_max;
77 /* during rule parsing, uid/gid lookup results are cached */
79 unsigned int uids_cur;
80 unsigned int uids_max;
82 unsigned int gids_cur;
83 unsigned int gids_max;
86 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
99 enum string_glob_type {
101 GL_PLAIN, /* no special chars */
102 GL_GLOB, /* shell globs ?,*,[] */
103 GL_SPLIT, /* multi-value A|B */
104 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
105 GL_SOMETHING, /* commonly used "?*" */
109 /* tokens of a rule are sorted/handled in this order */
114 TK_M_ACTION, /* val */
115 TK_M_DEVPATH, /* val */
116 TK_M_KERNEL, /* val */
117 TK_M_DEVLINK, /* val */
119 TK_M_ENV, /* val, attr */
120 TK_M_SUBSYSTEM, /* val */
121 TK_M_DRIVER, /* val */
122 TK_M_WAITFOR, /* val */
123 TK_M_ATTR, /* val, attr */
126 TK_M_KERNELS, /* val */
127 TK_M_SUBSYSTEMS, /* val */
128 TK_M_DRIVERS, /* val */
129 TK_M_ATTRS, /* val, attr */
132 TK_M_TEST, /* val, mode_t */
133 TK_M_PROGRAM, /* val */
134 TK_M_IMPORT_FILE, /* val */
135 TK_M_IMPORT_PROG, /* val */
136 TK_M_IMPORT_PARENT, /* val */
137 TK_M_RESULT, /* val */
141 TK_A_STRING_ESCAPE_NONE,
142 TK_A_STRING_ESCAPE_REPLACE,
143 TK_A_NUM_FAKE_PART, /* int */
144 TK_A_DEVLINK_PRIO, /* int */
145 TK_A_OWNER, /* val */
146 TK_A_GROUP, /* val */
148 TK_A_OWNER_ID, /* uid_t */
149 TK_A_GROUP_ID, /* gid_t */
150 TK_A_MODE_ID, /* mode_t */
151 TK_A_ENV, /* val, attr */
153 TK_A_DEVLINK, /* val */
154 TK_A_EVENT_TIMEOUT, /* int */
156 TK_A_ATTR, /* val, attr */
157 TK_A_RUN, /* val, bool */
158 TK_A_GOTO, /* size_t */
164 /* we try to pack stuff in a way that we take only 12 bytes per token */
167 unsigned char type; /* same as in rule and key */
171 unsigned short token_count;
172 unsigned int label_off;
173 unsigned short filename_off;
174 unsigned short filename_line;
181 unsigned int value_off;
183 unsigned int attr_off;
186 unsigned int rule_goto;
200 struct udev_rules *rules;
202 struct token token[MAX_TK];
203 unsigned int token_cur;
207 static const char *operation_str(enum operation_type type)
209 static const char *operation_strs[] = {
210 [OP_UNSET] = "UNSET",
211 [OP_MATCH] = "match",
212 [OP_NOMATCH] = "nomatch",
213 [OP_MATCH_MAX] = "MATCH_MAX",
216 [OP_ASSIGN] = "assign",
217 [OP_ASSIGN_FINAL] = "assign-final",
220 return operation_strs[type];
223 static const char *string_glob_str(enum string_glob_type type)
225 static const char *string_glob_strs[] = {
226 [GL_UNSET] = "UNSET",
227 [GL_PLAIN] = "plain",
229 [GL_SPLIT] = "split",
230 [GL_SPLIT_GLOB] = "split-glob",
231 [GL_SOMETHING] = "split-glob",
232 [GL_FORMAT] = "format",
235 return string_glob_strs[type];
238 static const char *token_str(enum token_type type)
240 static const char *token_strs[] = {
241 [TK_UNSET] = "UNSET",
244 [TK_M_ACTION] = "M ACTION",
245 [TK_M_DEVPATH] = "M DEVPATH",
246 [TK_M_KERNEL] = "M KERNEL",
247 [TK_M_DEVLINK] = "M DEVLINK",
248 [TK_M_NAME] = "M NAME",
249 [TK_M_ENV] = "M ENV",
250 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
251 [TK_M_DRIVER] = "M DRIVER",
252 [TK_M_WAITFOR] = "M WAITFOR",
253 [TK_M_ATTR] = "M ATTR",
255 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
256 [TK_M_KERNELS] = "M KERNELS",
257 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
258 [TK_M_DRIVERS] = "M DRIVERS",
259 [TK_M_ATTRS] = "M ATTRS",
260 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
262 [TK_M_TEST] = "M TEST",
263 [TK_M_PROGRAM] = "M PROGRAM",
264 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
265 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
266 [TK_M_IMPORT_PARENT] = "M MPORT_PARENT",
267 [TK_M_RESULT] = "M RESULT",
268 [TK_M_MAX] = "M MAX",
270 [TK_A_IGNORE_DEVICE] = "A IGNORE_DEVICE",
271 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
272 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
273 [TK_A_NUM_FAKE_PART] = "A NUM_FAKE_PART",
274 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
275 [TK_A_OWNER] = "A OWNER",
276 [TK_A_GROUP] = "A GROUP",
277 [TK_A_MODE] = "A MODE",
278 [TK_A_OWNER_ID] = "A OWNER_ID",
279 [TK_A_GROUP_ID] = "A GROUP_ID",
280 [TK_A_MODE_ID] = "A MODE_ID",
281 [TK_A_ENV] = "A ENV",
282 [TK_A_NAME] = "A NAME",
283 [TK_A_DEVLINK] = "A DEVLINK",
284 [TK_A_EVENT_TIMEOUT] = "A EVENT_TIMEOUT",
285 [TK_A_IGNORE_REMOVE] = "A IGNORE_REMOVE",
286 [TK_A_ATTR] = "A ATTR",
287 [TK_A_RUN] = "A RUN",
288 [TK_A_GOTO] = "A GOTO",
289 [TK_A_LAST_RULE] = "A LAST_RULE",
294 return token_strs[type];
297 static void dump_token(struct udev_rules *rules, struct token *token)
299 enum token_type type = token->type;
300 enum operation_type op = token->key.op;
301 enum string_glob_type glob = token->key.glob;
302 const char *value = &rules->buf[token->key.value_off];
303 const char *attr = &rules->buf[token->key.attr_off];
308 const char *tks_ptr = (char *)rules->tokens;
309 const char *tk_ptr = (char *)token;
310 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
312 dbg(rules->udev, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n",
313 &rules->buf[token->rule.filename_off], token->rule.filename_line,
314 idx, token->rule.token_count,
315 &rules->buf[token->rule.label_off],
328 case TK_M_SUBSYSTEMS:
331 case TK_M_IMPORT_FILE:
332 case TK_M_IMPORT_PROG:
333 case TK_M_IMPORT_PARENT:
341 dbg(rules->udev, "%s %s '%s'(%s)\n",
342 token_str(type), operation_str(op), value, string_glob_str(glob));
349 dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
350 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
352 case TK_A_IGNORE_DEVICE:
353 case TK_A_STRING_ESCAPE_NONE:
354 case TK_A_STRING_ESCAPE_REPLACE:
356 case TK_A_IGNORE_REMOVE:
357 dbg(rules->udev, "%s\n", token_str(type));
360 dbg(rules->udev, "%s %s '%s'(%s) %#o\n",
361 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
363 case TK_A_NUM_FAKE_PART:
364 dbg(rules->udev, "%s %u\n", token_str(type), token->key.num_fake_part);
366 case TK_A_DEVLINK_PRIO:
367 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.devlink_prio);
370 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
373 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
376 dbg(rules->udev, "%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
378 case TK_A_EVENT_TIMEOUT:
379 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.event_timeout);
382 dbg(rules->udev, "%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
385 dbg(rules->udev, "* %s\n", token_str(type));
387 case TK_M_PARENTS_MIN:
388 case TK_M_PARENTS_MAX:
391 dbg(rules->udev, "unknown type %u\n", type);
396 static void dump_rules(struct udev_rules *rules)
400 dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
402 rules->token_cur * sizeof(struct token),
405 for(i = 0; i < rules->token_cur; i++)
406 dump_token(rules, &rules->tokens[i]);
409 static inline const char *operation_str(enum operation_type type) { return NULL; }
410 static inline const char *token_str(enum token_type type) { return NULL; }
411 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
412 static inline void dump_rules(struct udev_rules *rules) {}
415 static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
419 /* grow buffer if needed */
420 if (rules->buf_cur + bytes+1 >= rules->buf_max) {
424 /* double the buffer size */
425 add = rules->buf_max;
429 buf = realloc(rules->buf, rules->buf_max + add);
432 dbg(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add);
434 rules->buf_max += add;
436 off = rules->buf_cur;
437 memcpy(&rules->buf[rules->buf_cur], str, bytes);
438 rules->buf_cur += bytes;
443 static int add_string(struct udev_rules *rules, const char *str)
445 unsigned int node_idx;
446 struct trie_node *new_node;
447 unsigned int new_node_idx;
452 struct trie_node *parent;
454 /* walk trie, start from last character of str to find matching tails */
458 for (depth = 0; depth <= len; depth++) {
459 struct trie_node *node;
460 unsigned int child_idx;
462 node = &rules->trie_nodes[node_idx];
463 off = node->value_off + node->value_len - len;
465 /* match against current node */
466 if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0))
469 /* lookup child node */
470 key = str[len - 1 - depth];
471 child_idx = node->child_idx;
472 while (child_idx > 0) {
473 struct trie_node *child;
475 child = &rules->trie_nodes[child_idx];
476 if (child->key == key)
478 child_idx = child->next_child_idx;
482 node_idx = child_idx;
485 /* string not found, add it */
486 off = add_new_string(rules, str, len + 1);
488 /* grow trie nodes if needed */
489 if (rules->trie_nodes_cur >= rules->trie_nodes_max) {
490 struct trie_node *nodes;
493 /* double the buffer size */
494 add = rules->trie_nodes_max;
498 nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node));
501 dbg(rules->udev, "extend trie nodes from %u to %u\n",
502 rules->trie_nodes_max, rules->trie_nodes_max + add);
503 rules->trie_nodes = nodes;
504 rules->trie_nodes_max += add;
508 new_node_idx = rules->trie_nodes_cur;
509 rules->trie_nodes_cur++;
510 new_node = &rules->trie_nodes[new_node_idx];
511 memset(new_node, 0x00, sizeof(struct trie_node));
512 new_node->value_off = off;
513 new_node->value_len = len;
516 /* join the parent's child list */
517 parent = &rules->trie_nodes[node_idx];
518 if (parent->child_idx == 0) {
519 parent->child_idx = new_node_idx;
521 struct trie_node *last_child;
523 last_child = &rules->trie_nodes[parent->last_child_idx];
524 last_child->next_child_idx = new_node_idx;
526 parent->last_child_idx = new_node_idx;
530 static int add_token(struct udev_rules *rules, struct token *token)
532 /* grow buffer if needed */
533 if (rules->token_cur+1 >= rules->token_max) {
534 struct token *tokens;
537 /* double the buffer size */
538 add = rules->token_max;
542 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
545 dbg(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add);
546 rules->tokens = tokens;
547 rules->token_max += add;
549 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
554 static uid_t add_uid(struct udev_rules *rules, const char *owner)
560 /* lookup, if we know it already */
561 for (i = 0; i < rules->uids_cur; i++) {
562 off = rules->uids[i].name_off;
563 if (strcmp(&rules->buf[off], owner) == 0) {
564 uid = rules->uids[i].uid;
565 dbg(rules->udev, "return existing %u for '%s'\n", uid, owner);
569 uid = util_lookup_user(rules->udev, owner);
571 /* grow buffer if needed */
572 if (rules->uids_cur+1 >= rules->uids_max) {
573 struct uid_gid *uids;
576 /* double the buffer size */
577 add = rules->uids_max;
581 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
584 dbg(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add);
586 rules->uids_max += add;
588 rules->uids[rules->uids_cur].uid = uid;
589 off = add_string(rules, owner);
592 rules->uids[rules->uids_cur].name_off = off;
597 static gid_t add_gid(struct udev_rules *rules, const char *group)
603 /* lookup, if we know it already */
604 for (i = 0; i < rules->gids_cur; i++) {
605 off = rules->gids[i].name_off;
606 if (strcmp(&rules->buf[off], group) == 0) {
607 gid = rules->gids[i].gid;
608 dbg(rules->udev, "return existing %u for '%s'\n", gid, group);
612 gid = util_lookup_group(rules->udev, group);
614 /* grow buffer if needed */
615 if (rules->gids_cur+1 >= rules->gids_max) {
616 struct uid_gid *gids;
619 /* double the buffer size */
620 add = rules->gids_max;
624 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
627 dbg(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add);
629 rules->gids_max += add;
631 rules->gids[rules->gids_cur].gid = gid;
632 off = add_string(rules, group);
635 rules->gids[rules->gids_cur].name_off = off;
640 static int import_property_from_string(struct udev_device *dev, char *line)
642 struct udev *udev = udev_device_get_udev(dev);
649 while (isspace(key[0]))
652 /* comment or empty line */
653 if (key[0] == '#' || key[0] == '\0')
656 /* split key/value */
657 val = strchr(key, '=');
664 while (isspace(val[0]))
671 while (isspace(key[len-1]))
675 /* terminate value */
679 while (isspace(val[len-1]))
687 if (val[0] == '"' || val[0] == '\'') {
688 if (val[len-1] != val[0]) {
689 info(udev, "inconsistent quoting: '%s', skip\n", line);
696 dbg(udev, "adding '%s'='%s'\n", key, val);
698 /* handle device, renamed by external tool, returning new path */
699 if (strcmp(key, "DEVPATH") == 0) {
700 char syspath[UTIL_PATH_SIZE];
702 info(udev, "updating devpath from '%s' to '%s'\n",
703 udev_device_get_devpath(dev), val);
704 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
705 util_strlcat(syspath, val, sizeof(syspath));
706 udev_device_set_syspath(dev, syspath);
708 struct udev_list_entry *entry;
710 entry = udev_device_add_property(dev, key, val);
712 udev_list_entry_set_flag(entry, 1);
717 static int import_file_into_properties(struct udev_device *dev, const char *filename)
720 char line[UTIL_LINE_SIZE];
722 f = fopen(filename, "r");
725 while (fgets(line, sizeof(line), f) != NULL)
726 import_property_from_string(dev, line);
731 static int import_program_into_properties(struct udev_device *dev, const char *program)
733 struct udev *udev = udev_device_get_udev(dev);
739 envp = udev_device_get_properties_envp(dev);
740 if (util_run_program(udev, program, envp, result, sizeof(result), &reslen) != 0)
744 while (line != NULL) {
747 pos = strchr(line, '\n');
752 import_property_from_string(dev, line);
758 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
760 struct udev *udev = udev_device_get_udev(dev);
761 struct udev_device *dev_parent;
762 struct udev_list_entry *list_entry;
764 dev_parent = udev_device_get_parent(dev);
765 if (dev_parent == NULL)
768 dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent));
769 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
770 const char *key = udev_list_entry_get_name(list_entry);
771 const char *val = udev_list_entry_get_value(list_entry);
773 if (fnmatch(filter, key, 0) == 0) {
774 struct udev_list_entry *entry;
776 dbg(udev, "import key '%s=%s'\n", key, val);
777 entry = udev_device_add_property(dev, key, val);
779 udev_list_entry_set_flag(entry, 1);
785 #define WAIT_LOOP_PER_SECOND 50
786 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
788 struct udev *udev = udev_device_get_udev(dev);
789 char filepath[UTIL_PATH_SIZE];
790 char devicepath[UTIL_PATH_SIZE];
792 int loop = timeout * WAIT_LOOP_PER_SECOND;
794 /* a relative path is a device attribute */
795 devicepath[0] = '\0';
796 if (file[0] != '/') {
797 util_strlcpy(devicepath, udev_get_sys_path(udev), sizeof(devicepath));
798 util_strlcat(devicepath, udev_device_get_devpath(dev), sizeof(devicepath));
800 util_strlcpy(filepath, devicepath, sizeof(filepath));
801 util_strlcat(filepath, "/", sizeof(filepath));
802 util_strlcat(filepath, file, sizeof(filepath));
806 dbg(udev, "will wait %i sec for '%s'\n", timeout, file);
809 if (stat(file, &stats) == 0) {
810 info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
813 /* make sure, the device did not disappear in the meantime */
814 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
815 info(udev, "device disappeared while waiting for '%s'\n", file);
818 info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
819 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
821 info(udev, "waiting for '%s' failed\n", file);
825 static int attr_subst_subdir(char *attr, size_t len)
830 pos = strstr(attr, "/*/");
832 char str[UTIL_PATH_SIZE];
836 util_strlcpy(str, &pos[2], sizeof(str));
841 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
844 if (dent->d_name[0] == '.')
846 util_strlcat(attr, dent->d_name, len);
847 util_strlcat(attr, str, len);
848 if (stat(attr, &stats) == 0) {
857 util_strlcat(attr, str, len);
863 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
869 if (linepos == NULL && linepos[0] == '\0')
872 /* skip whitespace */
873 while (isspace(linepos[0]) || linepos[0] == ',')
877 if (linepos[0] == '\0')
883 if (linepos[0] == '\0')
885 if (isspace(linepos[0]))
887 if (linepos[0] == '=')
889 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
890 if (linepos[1] == '=')
894 /* remember end of key */
897 /* skip whitespace after key */
898 while (isspace(linepos[0]))
900 if (linepos[0] == '\0')
903 /* get operation type */
904 if (linepos[0] == '=' && linepos[1] == '=') {
907 } else if (linepos[0] == '!' && linepos[1] == '=') {
910 } else if (linepos[0] == '+' && linepos[1] == '=') {
913 } else if (linepos[0] == '=') {
916 } else if (linepos[0] == ':' && linepos[1] == '=') {
917 *op = OP_ASSIGN_FINAL;
925 /* skip whitespace after operator */
926 while (isspace(linepos[0]))
928 if (linepos[0] == '\0')
932 if (linepos[0] == '"')
939 temp = strchr(linepos, '"');
944 dbg(udev, "%s '%s'-'%s'\n", operation_str(*op), *key, *value);
946 /* move line to next key */
951 /* extract possible KEY{attr} */
952 static char *get_key_attribute(struct udev *udev, char *str)
957 attr = strchr(str, '{');
960 pos = strchr(attr, '}');
962 err(udev, "missing closing brace for format\n");
966 dbg(udev, "attribute='%s'\n", attr);
972 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
973 enum operation_type op,
974 const char *value, const void *data)
976 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
977 const char *attr = data;
978 enum string_glob_type glob;
980 memset(token, 0x00, sizeof(struct token));
992 case TK_M_SUBSYSTEMS:
995 case TK_M_IMPORT_FILE:
996 case TK_M_IMPORT_PROG:
997 case TK_M_IMPORT_PARENT:
1005 token->key.value_off = add_string(rule_tmp->rules, value);
1012 token->key.value_off = add_string(rule_tmp->rules, value);
1013 token->key.attr_off = add_string(rule_tmp->rules, attr);
1016 token->key.value_off = add_string(rule_tmp->rules, value);
1018 token->key.mode = *(mode_t *)data;
1020 case TK_A_IGNORE_DEVICE:
1021 case TK_A_STRING_ESCAPE_NONE:
1022 case TK_A_STRING_ESCAPE_REPLACE:
1023 case TK_A_IGNORE_REMOVE:
1024 case TK_A_LAST_RULE:
1027 token->key.value_off = add_string(rule_tmp->rules, value);
1028 token->key.ignore_error = *(int *)data;
1030 case TK_A_NUM_FAKE_PART:
1031 token->key.num_fake_part = *(int *)data;
1033 case TK_A_DEVLINK_PRIO:
1034 token->key.devlink_prio = *(int *)data;
1037 token->key.uid = *(uid_t *)data;
1040 token->key.gid = *(gid_t *)data;
1043 token->key.mode = *(mode_t *)data;
1045 case TK_A_EVENT_TIMEOUT:
1046 token->key.event_timeout = *(int *)data;
1049 case TK_M_PARENTS_MIN:
1050 case TK_M_PARENTS_MAX:
1054 err(rule_tmp->rules->udev, "wrong type %u\n", type);
1059 if (value != NULL) {
1060 if (type < TK_M_MAX) {
1061 /* check if we need to split or call fnmatch() while matching rules */
1065 has_split = (strchr(value, '|') != NULL);
1066 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL ||
1067 strchr(value, '[') != NULL || strchr(value, ']') != NULL);
1068 if (has_split && has_glob) {
1069 glob = GL_SPLIT_GLOB;
1070 } else if (has_split) {
1072 } else if (has_glob) {
1073 if (strcmp(value, "?*") == 0)
1074 glob = GL_SOMETHING;
1079 /* check if we need to substitute format strings for matching rules */
1080 if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
1085 token->key.type = type;
1087 token->key.glob = glob;
1088 rule_tmp->token_cur++;
1089 if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) {
1090 err(rule_tmp->rules->udev, "temporary rule array too small\n");
1096 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1099 unsigned int start = 0;
1100 unsigned int end = rule_tmp->token_cur;
1102 for (i = 0; i < rule_tmp->token_cur; i++) {
1103 enum token_type next_val = TK_UNSET;
1104 unsigned int next_idx;
1107 /* find smallest value */
1108 for (j = start; j < end; j++) {
1109 if (rule_tmp->token[j].type == TK_UNSET)
1111 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1112 next_val = rule_tmp->token[j].type;
1117 /* add token and mark done */
1118 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1120 rule_tmp->token[next_idx].type = TK_UNSET;
1123 if (next_idx == start)
1125 if (next_idx+1 == end)
1131 static int add_rule(struct udev_rules *rules, char *line,
1132 const char *filename, unsigned int filename_off, unsigned int lineno)
1139 struct rule_tmp rule_tmp;
1141 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1142 rule_tmp.rules = rules;
1143 rule_tmp.rule.type = TK_RULE;
1144 rule_tmp.rule.rule.filename_off = filename_off;
1145 rule_tmp.rule.rule.filename_line = lineno;
1151 enum operation_type op;
1153 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1156 if (strcasecmp(key, "ACTION") == 0) {
1157 if (op > OP_MATCH_MAX) {
1158 err(rules->udev, "invalid ACTION operation\n");
1161 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1166 if (strcasecmp(key, "DEVPATH") == 0) {
1167 if (op > OP_MATCH_MAX) {
1168 err(rules->udev, "invalid DEVPATH operation\n");
1171 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1176 if (strcasecmp(key, "KERNEL") == 0) {
1177 if (op > OP_MATCH_MAX) {
1178 err(rules->udev, "invalid KERNEL operation\n");
1181 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1186 if (strcasecmp(key, "SUBSYSTEM") == 0) {
1187 if (op > OP_MATCH_MAX) {
1188 err(rules->udev, "invalid SUBSYSTEM operation\n");
1191 /* bus, class, subsystem events should all be the same */
1192 if (strcmp(value, "subsystem") == 0 ||
1193 strcmp(value, "bus") == 0 ||
1194 strcmp(value, "class") == 0) {
1195 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
1196 err(rules->udev, "'%s' must be specified as 'subsystem' \n"
1197 "please fix it in %s:%u", value, filename, lineno);
1198 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1200 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1205 if (strcasecmp(key, "DRIVER") == 0) {
1206 if (op > OP_MATCH_MAX) {
1207 err(rules->udev, "invalid DRIVER operation\n");
1210 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1215 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
1216 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1218 err(rules->udev, "error parsing ATTR attribute\n");
1221 if (op < OP_MATCH_MAX) {
1222 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1224 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1230 if (strcasecmp(key, "KERNELS") == 0 ||
1231 strcasecmp(key, "ID") == 0) {
1232 if (op > OP_MATCH_MAX) {
1233 err(rules->udev, "invalid KERNELS operation\n");
1236 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1241 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
1242 strcasecmp(key, "BUS") == 0) {
1243 if (op > OP_MATCH_MAX) {
1244 err(rules->udev, "invalid SUBSYSTEMS operation\n");
1247 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1252 if (strcasecmp(key, "DRIVERS") == 0) {
1253 if (op > OP_MATCH_MAX) {
1254 err(rules->udev, "invalid DRIVERS operation\n");
1257 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1262 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1263 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1264 if (op > OP_MATCH_MAX) {
1265 err(rules->udev, "invalid ATTRS operation\n");
1268 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1270 err(rules->udev, "error parsing ATTRS attribute\n");
1273 if (strncmp(attr, "device/", 7) == 0)
1274 err(rules->udev, "the 'device' link may not be available in a future kernel, "
1275 "please fix it in %s:%u", filename, lineno);
1276 else if (strstr(attr, "../") != NULL)
1277 err(rules->udev, "do not reference parent sysfs directories directly, "
1278 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1279 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1284 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
1285 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1287 err(rules->udev, "error parsing ENV attribute\n");
1290 if (strncmp(attr, "PHYSDEV", 7) == 0)
1292 if (op < OP_MATCH_MAX) {
1293 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1296 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1303 if (strcasecmp(key, "PROGRAM") == 0) {
1304 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1309 if (strcasecmp(key, "RESULT") == 0) {
1310 if (op > OP_MATCH_MAX) {
1311 err(rules->udev, "invalid RESULT operation\n");
1314 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1319 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
1320 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1321 if (attr != NULL && strstr(attr, "program")) {
1322 dbg(rules->udev, "IMPORT will be executed\n");
1323 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1325 } else if (attr != NULL && strstr(attr, "file")) {
1326 dbg(rules->udev, "IMPORT will be included as file\n");
1327 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1329 } else if (attr != NULL && strstr(attr, "parent")) {
1330 dbg(rules->udev, "IMPORT will include the parent values\n");
1331 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1334 /* figure it out if it is executable */
1335 char file[UTIL_PATH_SIZE];
1337 struct stat statbuf;
1339 util_strlcpy(file, value, sizeof(file));
1340 pos = strchr(file, ' ');
1344 /* allow programs in /lib/udev called without the path */
1345 if (strchr(file, '/') == NULL) {
1346 util_strlcpy(file, UDEV_PREFIX "/lib/udev/", sizeof(file));
1347 util_strlcat(file, value, sizeof(file));
1348 pos = strchr(file, ' ');
1353 dbg(rules->udev, "IMPORT auto mode for '%s'\n", file);
1354 if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
1355 dbg(rules->udev, "IMPORT will be executed (autotype)\n");
1356 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1359 dbg(rules->udev, "IMPORT will be included as file (autotype)\n");
1360 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1367 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
1370 if (op > OP_MATCH_MAX) {
1371 err(rules->udev, "invalid TEST operation\n");
1374 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1376 mode = strtol(attr, NULL, 8);
1377 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1379 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1385 if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
1388 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1389 if (attr != NULL && strstr(attr, "ignore_error"))
1391 rule_add_key(&rule_tmp, TK_A_RUN, op, value, &flag);
1396 if (strcasecmp(key, "WAIT_FOR") == 0 || strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
1397 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1403 if (strcasecmp(key, "LABEL") == 0) {
1404 rule_tmp.rule.rule.label_off = add_string(rules, value);
1409 if (strcasecmp(key, "GOTO") == 0) {
1410 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1415 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
1416 if (op < OP_MATCH_MAX) {
1417 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1419 if (value[0] == '\0')
1420 dbg(rules->udev, "name empty, node creation suppressed\n");
1421 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1422 attr = get_key_attribute(rules->udev, key + sizeof("NAME")-1);
1424 if (strstr(attr, "all_partitions") != NULL) {
1425 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1427 dbg(rules->udev, "creation of partition nodes requested\n");
1428 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1430 if (strstr(attr, "ignore_remove") != NULL) {
1431 dbg(rules->udev, "remove event should be ignored\n");
1432 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1436 rule_tmp.rule.rule.flags = 1;
1440 if (strcasecmp(key, "SYMLINK") == 0) {
1441 if (op < OP_MATCH_MAX)
1442 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1444 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1445 rule_tmp.rule.rule.flags = 1;
1450 if (strcasecmp(key, "OWNER") == 0) {
1454 uid = strtoul(value, &endptr, 10);
1455 if (endptr[0] == '\0') {
1456 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1457 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1458 uid = add_uid(rules, value);
1459 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1460 } else if (rules->resolve_names == 0) {
1461 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1463 rule_tmp.rule.rule.flags = 1;
1468 if (strcasecmp(key, "GROUP") == 0) {
1472 gid = strtoul(value, &endptr, 10);
1473 if (endptr[0] == '\0') {
1474 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1475 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1476 gid = add_gid(rules, value);
1477 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1478 } else if (rules->resolve_names == 0) {
1479 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1481 rule_tmp.rule.rule.flags = 1;
1486 if (strcasecmp(key, "MODE") == 0) {
1490 mode = strtol(value, &endptr, 8);
1491 if (endptr[0] == '\0')
1492 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1494 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1495 rule_tmp.rule.rule.flags = 1;
1500 if (strcasecmp(key, "OPTIONS") == 0) {
1503 if (strstr(value, "last_rule") != NULL) {
1504 dbg(rules->udev, "last rule to be applied\n");
1505 rule_add_key(&rule_tmp, TK_A_LAST_RULE, 0, NULL, NULL);
1507 if (strstr(value, "ignore_device") != NULL) {
1508 dbg(rules->udev, "device should be ignored\n");
1509 rule_add_key(&rule_tmp, TK_A_IGNORE_DEVICE, 0, NULL, NULL);
1511 if (strstr(value, "ignore_remove") != NULL) {
1512 dbg(rules->udev, "remove event should be ignored\n");
1513 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1515 pos = strstr(value, "link_priority=");
1517 int prio = atoi(&pos[strlen("link_priority=")]);
1519 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, 0, NULL, &prio);
1520 dbg(rules->udev, "link priority=%i\n", prio);
1522 pos = strstr(value, "event_timeout=");
1524 int tout = atoi(&pos[strlen("event_timeout=")]);
1526 rule_add_key(&rule_tmp, TK_A_EVENT_TIMEOUT, 0, NULL, &tout);
1527 dbg(rules->udev, "event timout=%i\n", tout);
1529 pos = strstr(value, "string_escape=");
1531 pos = &pos[strlen("string_escape=")];
1532 if (strncmp(pos, "none", strlen("none")) == 0)
1533 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, 0, NULL, NULL);
1534 else if (strncmp(pos, "replace", strlen("replace")) == 0)
1535 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, 0, NULL, NULL);
1537 if (strstr(value, "all_partitions") != NULL) {
1538 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1540 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1541 dbg(rules->udev, "creation of partition nodes requested\n");
1546 err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno);
1549 if (physdev && !waitfor)
1550 err(rules->udev, "PHYSDEV* values are deprecated and not available on recent kernels, "
1551 "please fix it in %s:%u\n", filename, lineno);
1553 /* skip line if no valid key was found */
1557 /* add rule token */
1558 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1559 if (add_token(rules, &rule_tmp.rule) != 0)
1562 /* add tokens to list, sorted by type */
1563 if (sort_token(rules, &rule_tmp) != 0)
1568 err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
1572 static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off)
1575 unsigned int first_token;
1576 char line[UTIL_LINE_SIZE];
1580 info(rules->udev, "reading '%s' as rules file\n", filename);
1582 f = fopen(filename, "r");
1586 first_token = rules->token_cur;
1588 while(fgets(line, sizeof(line), f) != NULL) {
1592 /* skip whitespace */
1595 while (isspace(key[0]))
1606 /* continue reading if backslash+newline is found */
1607 while (line[len-2] == '\\') {
1608 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1614 if (len+1 >= sizeof(line)) {
1615 err(rules->udev, "line too long '%s':%u, ignored\n", filename, line_nr);
1618 add_rule(rules, key, filename, filename_off, line_nr);
1622 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1623 for (i = first_token+1; i < rules->token_cur; i++) {
1624 if (rules->tokens[i].type == TK_A_GOTO) {
1625 char *label = &rules->buf[rules->tokens[i].key.value_off];
1628 for (j = i+1; j < rules->token_cur; j++) {
1629 if (rules->tokens[j].type != TK_RULE)
1631 if (rules->tokens[j].rule.label_off == 0)
1633 if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0)
1635 rules->tokens[i].key.rule_goto = j;
1637 if (rules->tokens[i].key.rule_goto == 0)
1638 err(rules->udev, "GOTO '%s' has no matching label in: '%s'\n", label, filename);
1644 static int add_matching_files(struct udev *udev, struct udev_list_node *file_list, const char *dirname, const char *suffix)
1647 char filename[UTIL_PATH_SIZE];
1649 dbg(udev, "open directory '%s'\n", dirname);
1650 dir = opendir(dirname);
1652 info(udev, "unable to open '%s': %m\n", dirname);
1657 struct dirent *dent;
1659 dent = readdir(dir);
1660 if (dent == NULL || dent->d_name[0] == '\0')
1663 if (dent->d_name[0] == '.')
1666 /* look for file matching with specified suffix */
1667 if (suffix != NULL) {
1670 ext = strrchr(dent->d_name, '.');
1673 if (strcmp(ext, suffix) != 0)
1676 dbg(udev, "put file '%s/%s' into list\n", dirname, dent->d_name);
1678 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
1679 filename[sizeof(filename)-1] = '\0';
1680 udev_list_entry_add(udev, file_list, filename, NULL, 1, 1);
1687 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1689 struct udev_rules *rules;
1690 struct stat statbuf;
1691 struct udev_list_node file_list;
1692 struct udev_list_entry *file_loop, *file_tmp;
1693 struct token end_token;
1695 rules = malloc(sizeof(struct udev_rules));
1698 memset(rules, 0x00, sizeof(struct udev_rules));
1700 rules->resolve_names = resolve_names;
1701 udev_list_init(&file_list);
1703 /* init token array and string buffer */
1704 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1705 if (rules->tokens == NULL)
1707 rules->token_max = PREALLOC_TOKEN;
1709 rules->buf = malloc(PREALLOC_STRBUF);
1710 if (rules->buf == NULL)
1712 rules->buf_max = PREALLOC_STRBUF;
1713 /* offset 0 is always '\0' */
1714 rules->buf[0] = '\0';
1716 dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1717 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1719 rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
1720 if (rules->trie_nodes == NULL)
1722 rules->trie_nodes_max = PREALLOC_TRIE;
1723 /* offset 0 is the trie root, with an empty string */
1724 memset(rules->trie_nodes, 0x00, sizeof(struct trie_node));
1725 rules->trie_nodes_cur = 1;
1727 if (udev_get_rules_path(udev) != NULL) {
1728 /* custom rules location for testing */
1729 add_matching_files(udev, &file_list, udev_get_rules_path(udev), ".rules");
1731 char filename[PATH_MAX];
1732 struct udev_list_node sort_list;
1733 struct udev_list_entry *sort_loop, *sort_tmp;
1735 /* read user/custom rules */
1736 add_matching_files(udev, &file_list, SYSCONFDIR "/udev/rules.d", ".rules");
1738 /* read dynamic/temporary rules */
1739 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
1740 util_strlcat(filename, "/.udev/rules.d", sizeof(filename));
1741 if (stat(filename, &statbuf) != 0) {
1742 util_create_path(udev, filename);
1743 udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
1744 mkdir(filename, 0755);
1745 udev_selinux_resetfscreatecon(udev);
1747 udev_list_init(&sort_list);
1748 add_matching_files(udev, &sort_list, filename, ".rules");
1750 /* read default rules */
1751 add_matching_files(udev, &sort_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules");
1753 /* sort all rules files by basename into list of files */
1754 udev_list_entry_foreach_safe(sort_loop, sort_tmp, udev_list_get_entry(&sort_list)) {
1755 const char *sort_name = udev_list_entry_get_name(sort_loop);
1756 const char *sort_base = strrchr(sort_name, '/');
1758 if (sort_base == NULL)
1760 /* sort entry into existing list */
1761 udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1762 const char *file_name = udev_list_entry_get_name(file_loop);
1763 const char *file_base = strrchr(file_name, '/');
1765 if (file_base == NULL)
1767 if (strcmp(file_base, sort_base) == 0) {
1768 info(udev, "rule file basename '%s' already added, ignoring '%s'\n",
1769 file_name, sort_name);
1770 udev_list_entry_delete(sort_loop);
1774 if (strcmp(file_base, sort_base) > 0) {
1775 /* found later file, insert before */
1776 udev_list_entry_remove(sort_loop);
1777 udev_list_entry_insert_before(sort_loop, file_loop);
1782 /* current file already handled */
1783 if (sort_loop == NULL)
1785 /* no later file, append to end of list */
1786 udev_list_entry_remove(sort_loop);
1787 udev_list_entry_append(sort_loop, &file_list);
1791 /* add all filenames to the string buffer */
1792 udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1793 const char *filename = udev_list_entry_get_name(file_loop);
1794 unsigned int filename_off;
1796 filename_off = add_string(rules, filename);
1797 /* the offset in the rule is limited to unsigned short */
1798 if (filename_off < USHRT_MAX)
1799 udev_list_entry_set_flag(file_loop, filename_off);
1802 /* parse list of files */
1803 udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1804 const char *filename = udev_list_entry_get_name(file_loop);
1805 unsigned int filename_off = udev_list_entry_get_flag(file_loop);
1807 if (stat(filename, &statbuf) == 0 && statbuf.st_size > 0)
1808 parse_file(rules, filename, filename_off);
1810 info(udev, "can not read '%s'\n", filename);
1811 udev_list_entry_delete(file_loop);
1814 memset(&end_token, 0x00, sizeof(struct token));
1815 end_token.type = TK_END;
1816 add_token(rules, &end_token);
1818 /* shrink allocated token and string buffer */
1819 if (rules->token_cur < rules->token_max) {
1820 struct token *tokens;
1822 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1823 if (tokens != NULL || rules->token_cur == 0) {
1824 rules->tokens = tokens;
1825 rules->token_max = rules->token_cur;
1828 if (rules->buf_cur < rules->buf_max) {
1831 buf = realloc(rules->buf, rules->buf_cur);
1832 if (buf != NULL || rules->buf_cur == 0) {
1834 rules->buf_max = rules->buf_cur;
1837 info(udev, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1838 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1839 info(udev, "temporary index used %zu bytes (%u * %zu bytes)\n",
1840 rules->trie_nodes_cur * sizeof(struct trie_node),
1841 rules->trie_nodes_cur, sizeof(struct trie_node));
1844 free(rules->trie_nodes);
1845 rules->trie_nodes = NULL;
1846 rules->trie_nodes_cur = 0;
1847 rules->trie_nodes_max = 0;
1849 /* cleanup uid/gid cache */
1852 rules->uids_cur = 0;
1853 rules->uids_max = 0;
1856 rules->gids_cur = 0;
1857 rules->gids_max = 0;
1863 void udev_rules_unref(struct udev_rules *rules)
1867 free(rules->tokens);
1869 free(rules->trie_nodes);
1875 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1877 char *key_value = &rules->buf[token->key.value_off];
1884 switch (token->key.glob) {
1886 match = (strcmp(key_value, val) == 0);
1889 match = (fnmatch(key_value, val, 0) == 0);
1896 split = &rules->buf[token->key.value_off];
1901 next = strchr(split, '|');
1903 size_t matchlen = (size_t)(next - split);
1905 match = (matchlen == len && strncmp(split, val, matchlen) == 0);
1909 match = (strcmp(split, val) == 0);
1918 char value[UTIL_PATH_SIZE];
1920 util_strlcpy(value, &rules->buf[token->key.value_off], sizeof(value));
1922 while (key_value != NULL) {
1923 pos = strchr(key_value, '|');
1928 dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str(token->type), key_value, val);
1929 match = (fnmatch(key_value, val, 0) == 0);
1937 match = (val[0] != '\0');
1944 if (match && (token->key.op == OP_MATCH)) {
1945 dbg(rules->udev, "%s is true (matching value)\n", token_str(token->type));
1948 if (!match && (token->key.op == OP_NOMATCH)) {
1949 dbg(rules->udev, "%s is true (non-matching value)\n", token_str(token->type));
1952 dbg(rules->udev, "%s is not true\n", token_str(token->type));
1956 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1958 const char *key_name = &rules->buf[cur->key.attr_off];
1959 const char *key_value = &rules->buf[cur->key.value_off];
1960 char value[UTIL_NAME_SIZE];
1964 if (key_name[0] == '[') {
1965 char attr[UTIL_PATH_SIZE];
1967 util_strlcpy(attr, key_name, sizeof(attr));
1968 util_resolve_subsys_kernel(event->udev, attr, value, sizeof(value), 1);
1970 if (value[0] == '\0') {
1973 val = udev_device_get_sysattr_value(dev, key_name);
1976 util_strlcpy(value, val, sizeof(value));
1979 /* strip trailing whitespace of value, if not asked to match for it */
1980 len = strlen(key_value);
1981 if (len > 0 && !isspace(key_value[len-1])) {
1982 len = strlen(value);
1983 while (len > 0 && isspace(value[--len]))
1985 dbg(rules->udev, "removed trailing whitespace from '%s'\n", value);
1987 return match_key(rules, cur, value);
1996 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event)
2000 enum escape_type esc = ESCAPE_UNSET;
2003 if (rules->tokens == NULL)
2006 can_set_name = ((strcmp(udev_device_get_action(event->dev), "add") == 0 ||
2007 strcmp(udev_device_get_action(event->dev), "change") == 0) &&
2008 (major(udev_device_get_devnum(event->dev)) > 0 ||
2009 strcmp(udev_device_get_subsystem(event->dev), "net") == 0));
2011 /* loop through token list, match, run actions or forward to next rule */
2012 cur = &rules->tokens[0];
2015 dump_token(rules, cur);
2016 switch (cur->type) {
2020 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2021 if (!can_set_name && rule->rule.flags)
2026 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
2030 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
2034 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
2039 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2040 struct udev_list_entry *list_entry;
2043 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
2044 const char *devlink;
2046 devlink = &udev_list_entry_get_name(list_entry)[devlen];
2047 if (match_key(rules, cur, devlink) == 0) {
2057 if (match_key(rules, cur, event->name) != 0)
2062 const char *key_name = &rules->buf[cur->key.attr_off];
2065 value = udev_device_get_property_value(event->dev, key_name);
2066 if (value == NULL) {
2067 dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name);
2070 if (match_key(rules, cur, value))
2074 case TK_M_SUBSYSTEM:
2075 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
2079 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2084 char filename[UTIL_PATH_SIZE];
2087 util_strlcpy(filename, &rules->buf[cur->key.value_off], sizeof(filename));
2088 udev_event_apply_format(event, filename, sizeof(filename));
2089 found = (wait_for_file(event->dev, filename, 10) == 0);
2090 if (!found && (cur->key.op != OP_NOMATCH))
2095 if (match_attr(rules, event->dev, event, cur) != 0)
2099 case TK_M_SUBSYSTEMS:
2105 /* get whole sequence of parent matches */
2107 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2110 /* loop over parents */
2111 event->dev_parent = event->dev;
2115 dbg(event->udev, "parent: '%s'\n", udev_device_get_syspath(event->dev_parent));
2116 /* loop over sequence of parent match keys */
2117 for (key = cur; key < next; key++ ) {
2118 dump_token(rules, key);
2121 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2124 case TK_M_SUBSYSTEMS:
2125 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2129 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2133 if (match_attr(rules, event->dev_parent, event, key) != 0)
2139 dbg(event->udev, "parent key matched\n");
2141 dbg(event->udev, "all parent keys matched\n");
2142 /* all keys matched */
2146 event->dev_parent = udev_device_get_parent(event->dev_parent);
2147 if (event->dev_parent == NULL)
2150 /* move behind our sequence of parent match keys */
2156 char filename[UTIL_PATH_SIZE];
2157 struct stat statbuf;
2160 util_strlcpy(filename, &rules->buf[cur->key.value_off], sizeof(filename));
2161 udev_event_apply_format(event, filename, sizeof(filename));
2162 if (util_resolve_subsys_kernel(event->udev, NULL, filename, sizeof(filename), 0) != 0)
2163 if (filename[0] != '/') {
2164 char tmp[UTIL_PATH_SIZE];
2166 util_strlcpy(tmp, udev_device_get_syspath(event->dev), sizeof(tmp));
2167 util_strlcat(tmp, "/", sizeof(tmp));
2168 util_strlcat(tmp, filename, sizeof(tmp));
2169 util_strlcpy(filename, tmp, sizeof(filename));
2172 attr_subst_subdir(filename, sizeof(filename));
2174 match = (stat(filename, &statbuf) == 0);
2175 dbg(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
2176 if (match && cur->key.mode > 0) {
2177 match = ((statbuf.st_mode & cur->key.mode) > 0);
2178 dbg(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
2179 match ? "matches" : "does not match", cur->key.mode);
2181 if (match && cur->key.op == OP_NOMATCH)
2183 if (!match && cur->key.op == OP_MATCH)
2189 char program[UTIL_PATH_SIZE];
2191 char result[UTIL_PATH_SIZE];
2193 free(event->program_result);
2194 event->program_result = NULL;
2195 util_strlcpy(program, &rules->buf[cur->key.value_off], sizeof(program));
2196 udev_event_apply_format(event, program, sizeof(program));
2197 envp = udev_device_get_properties_envp(event->dev);
2198 info(event->udev, "PROGRAM '%s' %s:%u\n",
2200 &rules->buf[rule->rule.filename_off],
2201 rule->rule.filename_line);
2202 if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
2203 if (cur->key.op != OP_NOMATCH)
2208 util_remove_trailing_chars(result, '\n');
2209 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2210 count = udev_util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2212 info(event->udev, "%i character(s) replaced\n" , count);
2214 event->program_result = strdup(result);
2215 dbg(event->udev, "storing result '%s'\n", event->program_result);
2216 if (cur->key.op == OP_NOMATCH)
2221 case TK_M_IMPORT_FILE:
2223 char import[UTIL_PATH_SIZE];
2225 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2226 udev_event_apply_format(event, import, sizeof(import));
2227 if (import_file_into_properties(event->dev, import) != 0)
2228 if (cur->key.op != OP_NOMATCH)
2232 case TK_M_IMPORT_PROG:
2234 char import[UTIL_PATH_SIZE];
2236 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2237 udev_event_apply_format(event, import, sizeof(import));
2238 info(event->udev, "IMPORT '%s' %s:%u\n",
2240 &rules->buf[rule->rule.filename_off],
2241 rule->rule.filename_line);
2242 if (import_program_into_properties(event->dev, import) != 0)
2243 if (cur->key.op != OP_NOMATCH)
2247 case TK_M_IMPORT_PARENT:
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_parent_into_properties(event->dev, import) != 0)
2254 if (cur->key.op != OP_NOMATCH)
2259 if (match_key(rules, cur, event->program_result) != 0)
2263 case TK_A_IGNORE_DEVICE:
2264 event->ignore_device = 1;
2267 case TK_A_STRING_ESCAPE_NONE:
2270 case TK_A_STRING_ESCAPE_REPLACE:
2271 esc = ESCAPE_REPLACE;
2273 case TK_A_NUM_FAKE_PART:
2274 if (strcmp(udev_device_get_subsystem(event->dev), "block") != 0)
2276 if (udev_device_get_sysnum(event->dev) != NULL)
2278 udev_device_set_num_fake_partitions(event->dev, cur->key.num_fake_part);
2280 case TK_A_DEVLINK_PRIO:
2281 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2285 char owner[UTIL_NAME_SIZE];
2287 if (event->owner_final)
2289 if (cur->key.op == OP_ASSIGN_FINAL)
2290 event->owner_final = 1;
2291 util_strlcpy(owner, &rules->buf[cur->key.value_off], sizeof(owner));
2292 udev_event_apply_format(event, owner, sizeof(owner));
2293 event->uid = util_lookup_user(event->udev, owner);
2294 info(event->udev, "OWNER %u %s:%u\n",
2296 &rules->buf[rule->rule.filename_off],
2297 rule->rule.filename_line);
2302 char group[UTIL_NAME_SIZE];
2304 if (event->group_final)
2306 if (cur->key.op == OP_ASSIGN_FINAL)
2307 event->group_final = 1;
2308 util_strlcpy(group, &rules->buf[cur->key.value_off], sizeof(group));
2309 udev_event_apply_format(event, group, sizeof(group));
2310 event->gid = util_lookup_group(event->udev, group);
2311 info(event->udev, "GROUP %u %s:%u\n",
2313 &rules->buf[rule->rule.filename_off],
2314 rule->rule.filename_line);
2319 char mode[UTIL_NAME_SIZE];
2322 if (event->mode_final)
2324 if (cur->key.op == OP_ASSIGN_FINAL)
2325 event->mode_final = 1;
2326 util_strlcpy(mode, &rules->buf[cur->key.value_off], sizeof(mode));
2327 udev_event_apply_format(event, mode, sizeof(mode));
2328 event->mode = strtol(mode, &endptr, 8);
2329 if (endptr[0] != '\0') {
2330 err(event->udev, "invalide mode '%s' set default mode 0660\n", mode);
2333 info(event->udev, "MODE %#o %s:%u\n",
2335 &rules->buf[rule->rule.filename_off],
2336 rule->rule.filename_line);
2340 if (event->owner_final)
2342 if (cur->key.op == OP_ASSIGN_FINAL)
2343 event->owner_final = 1;
2344 event->uid = cur->key.uid;
2345 info(event->udev, "OWNER %u %s:%u\n",
2347 &rules->buf[rule->rule.filename_off],
2348 rule->rule.filename_line);
2351 if (event->group_final)
2353 if (cur->key.op == OP_ASSIGN_FINAL)
2354 event->group_final = 1;
2355 event->gid = cur->key.gid;
2356 info(event->udev, "GROUP %u %s:%u\n",
2358 &rules->buf[rule->rule.filename_off],
2359 rule->rule.filename_line);
2362 if (event->mode_final)
2364 if (cur->key.op == OP_ASSIGN_FINAL)
2365 event->mode_final = 1;
2366 event->mode = cur->key.mode;
2367 info(event->udev, "MODE %#o %s:%u\n",
2369 &rules->buf[rule->rule.filename_off],
2370 rule->rule.filename_line);
2374 const char *name = &rules->buf[cur->key.attr_off];
2375 char *value = &rules->buf[cur->key.value_off];
2377 if (value[0] != '\0') {
2378 char temp_value[UTIL_NAME_SIZE];
2379 struct udev_list_entry *entry;
2381 util_strlcpy(temp_value, value, sizeof(temp_value));
2382 udev_event_apply_format(event, temp_value, sizeof(temp_value));
2383 entry = udev_device_add_property(event->dev, name, temp_value);
2385 udev_list_entry_set_flag(entry, 1);
2387 udev_device_add_property(event->dev, name, NULL);
2393 const char *name = &rules->buf[cur->key.value_off];
2394 char name_str[UTIL_PATH_SIZE];
2397 if (event->name_final)
2399 if (cur->key.op == OP_ASSIGN_FINAL)
2400 event->name_final = 1;
2401 if (name[0] == '\0') {
2406 util_strlcpy(name_str, name, sizeof(name_str));
2407 udev_event_apply_format(event, name_str, sizeof(name_str));
2408 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2409 count = udev_util_replace_chars(name_str, "/");
2411 info(event->udev, "%i character(s) replaced\n", count);
2413 event->name = strdup(name_str);
2414 info(event->udev, "NAME '%s' %s:%u\n",
2416 &rules->buf[rule->rule.filename_off],
2417 rule->rule.filename_line);
2423 char temp[UTIL_PATH_SIZE];
2424 char filename[UTIL_PATH_SIZE];
2428 if (event->devlink_final)
2430 if (major(udev_device_get_devnum(event->dev)) == 0)
2432 if (cur->key.op == OP_ASSIGN_FINAL)
2433 event->devlink_final = 1;
2434 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2435 udev_device_cleanup_devlinks_list(event->dev);
2437 /* allow multiple symlinks separated by spaces */
2438 util_strlcpy(temp, &rules->buf[cur->key.value_off], sizeof(temp));
2439 udev_event_apply_format(event, temp, sizeof(temp));
2440 if (esc == ESCAPE_UNSET)
2441 count = udev_util_replace_chars(temp, "/ ");
2442 else if (esc == ESCAPE_REPLACE)
2443 count = udev_util_replace_chars(temp, "/");
2445 info(event->udev, "%i character(s) replaced\n" , count);
2446 dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
2448 while (isspace(pos[0]))
2450 next = strchr(pos, ' ');
2453 info(event->udev, "LINK '%s' %s:%u\n",
2455 &rules->buf[rule->rule.filename_off],
2456 rule->rule.filename_line);
2457 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
2458 util_strlcat(filename, "/", sizeof(filename));
2459 util_strlcat(filename, pos, sizeof(filename));
2460 udev_device_add_devlink(event->dev, filename);
2461 while (isspace(next[1]))
2464 next = strchr(pos, ' ');
2466 if (pos[0] != '\0') {
2467 info(event->udev, "LINK '%s' %s:%u\n",
2469 &rules->buf[rule->rule.filename_off],
2470 rule->rule.filename_line);
2471 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
2472 util_strlcat(filename, "/", sizeof(filename));
2473 util_strlcat(filename, pos, sizeof(filename));
2474 udev_device_add_devlink(event->dev, filename);
2478 case TK_A_EVENT_TIMEOUT:
2479 udev_device_set_event_timeout(event->dev, cur->key.event_timeout);
2481 case TK_A_IGNORE_REMOVE:
2482 udev_device_set_ignore_remove(event->dev, 1);
2486 const char *key_name = &rules->buf[cur->key.attr_off];
2487 char attr[UTIL_PATH_SIZE];
2488 char value[UTIL_NAME_SIZE];
2491 util_strlcpy(attr, key_name, sizeof(attr));
2492 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0) {
2493 util_strlcpy(attr, udev_device_get_syspath(event->dev), sizeof(attr));
2494 util_strlcat(attr, "/", sizeof(attr));
2495 util_strlcat(attr, key_name, sizeof(attr));
2498 attr_subst_subdir(attr, sizeof(attr));
2500 util_strlcpy(value, &rules->buf[cur->key.value_off], sizeof(value));
2501 udev_event_apply_format(event, value, sizeof(value));
2502 info(event->udev, "ATTR '%s' writing '%s' %s:%u\n", attr, value,
2503 &rules->buf[rule->rule.filename_off],
2504 rule->rule.filename_line);
2505 f = fopen(attr, "w");
2507 if (fprintf(f, "%s", value) <= 0)
2508 err(event->udev, "error writing ATTR{%s}: %m\n", attr);
2511 err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);
2517 struct udev_list_entry *list_entry;
2519 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2520 udev_list_cleanup_entries(event->udev, &event->run_list);
2521 info(event->udev, "RUN '%s' %s:%u\n",
2522 &rules->buf[cur->key.value_off],
2523 &rules->buf[rule->rule.filename_off],
2524 rule->rule.filename_line);
2525 list_entry = udev_list_entry_add(event->udev, &event->run_list,
2526 &rules->buf[cur->key.value_off], NULL, 1, 0);
2527 if (cur->key.ignore_error)
2528 udev_list_entry_set_flag(list_entry, 1);
2532 cur = &rules->tokens[cur->key.rule_goto];
2534 case TK_A_LAST_RULE:
2538 case TK_M_PARENTS_MIN:
2539 case TK_M_PARENTS_MAX:
2542 err(rules->udev, "wrong type %u\n", cur->type);
2549 /* fast-forward to next rule */
2550 cur = rule + rule->rule.token_count;
2551 dbg(rules->udev, "forward to rule: %u\n",
2552 (unsigned int) (cur - rules->tokens));