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/>.
31 #define PREALLOC_TOKEN 2048
32 #define PREALLOC_STRBUF 32 * 1024
34 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
47 static const char *operation_str[] = {
50 [OP_NOMATCH] = "nomatch",
51 [OP_MATCH_MAX] = "MATCH_MAX",
54 [OP_ASSIGN] = "assign",
55 [OP_ASSIGN_FINAL] = "assign-final",
58 enum string_glob_type {
60 GL_PLAIN, /* no special chars */
61 GL_GLOB, /* shell globs ?,*,[] */
62 GL_SPLIT, /* multi-value A|B */
63 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
64 GL_SOMETHING, /* commonly used "?*" */
69 static const char *string_glob_str[] = {
74 [GL_SPLIT_GLOB] = "split-glob",
75 [GL_SOMETHING] = "split-glob",
76 [GL_FORMAT] = "format",
80 /* tokens of a rule are sorted/handled in this order */
85 TK_M_ACTION, /* val */
86 TK_M_DEVPATH, /* val */
87 TK_M_KERNEL, /* val */
88 TK_M_DEVLINK, /* val */
90 TK_M_ENV, /* val, attr */
91 TK_M_SUBSYSTEM, /* val */
92 TK_M_DRIVER, /* val */
93 TK_M_WAITFOR, /* val */
94 TK_M_ATTR, /* val, attr */
96 TK_M_KERNELS, /* val */
97 TK_M_SUBSYSTEMS, /* val */
98 TK_M_DRIVERS, /* val */
99 TK_M_ATTRS, /* val, attr */
102 TK_M_TEST, /* val, mode_t */
103 TK_M_PROGRAM, /* val */
104 TK_M_IMPORT_FILE, /* val */
105 TK_M_IMPORT_PROG, /* val */
106 TK_M_IMPORT_PARENT, /* val */
107 TK_M_RESULT, /* val */
111 TK_A_STRING_ESCAPE_NONE,
112 TK_A_STRING_ESCAPE_REPLACE,
113 TK_A_NUM_FAKE_PART, /* int */
114 TK_A_DEVLINK_PRIO, /* int */
115 TK_A_OWNER, /* val */
116 TK_A_GROUP, /* val */
118 TK_A_OWNER_ID, /* uid_t */
119 TK_A_GROUP_ID, /* gid_t */
120 TK_A_MODE_ID, /* mode_t */
121 TK_A_ENV, /* val, attr */
123 TK_A_DEVLINK, /* val */
124 TK_A_EVENT_TIMEOUT, /* int */
126 TK_A_ATTR, /* val, attr */
127 TK_A_RUN, /* val, bool */
128 TK_A_GOTO, /* size_t */
134 static const char *token_str[] = {
135 [TK_UNSET] = "UNSET",
138 [TK_M_ACTION] = "M ACTION",
139 [TK_M_DEVPATH] = "M DEVPATH",
140 [TK_M_KERNEL] = "M KERNEL",
141 [TK_M_DEVLINK] = "M DEVLINK",
142 [TK_M_NAME] = "M NAME",
143 [TK_M_ENV] = "M ENV",
144 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
145 [TK_M_DRIVER] = "M DRIVER",
146 [TK_M_WAITFOR] = "M WAITFOR",
147 [TK_M_ATTR] = "M ATTR",
149 [TK_M_KERNELS] = "M KERNELS",
150 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
151 [TK_M_DRIVERS] = "M DRIVERS",
152 [TK_M_ATTRS] = "M ATTRS",
153 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
155 [TK_M_TEST] = "M TEST",
156 [TK_M_PROGRAM] = "M PROGRAM",
157 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
158 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
159 [TK_M_IMPORT_PARENT] = "M MPORT_PARENT",
160 [TK_M_RESULT] = "M RESULT",
161 [TK_M_MAX] = "M MAX",
163 [TK_A_IGNORE_DEVICE] = "A IGNORE_DEVICE",
164 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
165 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
166 [TK_A_NUM_FAKE_PART] = "A NUM_FAKE_PART",
167 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
168 [TK_A_OWNER] = "A OWNER",
169 [TK_A_GROUP] = "A GROUP",
170 [TK_A_MODE] = "A MODE",
171 [TK_A_OWNER_ID] = "A OWNER_ID",
172 [TK_A_GROUP_ID] = "A GROUP_ID",
173 [TK_A_MODE_ID] = "A MODE_ID",
174 [TK_A_ENV] = "A ENV",
175 [TK_A_NAME] = "A NAME",
176 [TK_A_DEVLINK] = "A DEVLINK",
177 [TK_A_EVENT_TIMEOUT] = "A EVENT_TIMEOUT",
178 [TK_A_IGNORE_REMOVE] = "A IGNORE_REMOVE",
179 [TK_A_ATTR] = "A ATTR",
180 [TK_A_RUN] = "A RUN",
181 [TK_A_GOTO] = "A GOTO",
182 [TK_A_LAST_RULE] = "A LAST_RULE",
188 enum token_type type;
191 unsigned int next_rule;
192 unsigned int label_off;
193 unsigned int filename_off;
198 unsigned int value_off;
200 unsigned int attr_off;
203 unsigned int rule_goto;
217 struct udev_rules *rules;
219 struct token token[MAX_TK];
220 unsigned int token_cur;
224 unsigned int name_off;
235 /* every key in the rules file becomes a token */
236 struct token *tokens;
237 unsigned int token_cur;
238 unsigned int token_max;
240 /* all key strings are copied to a single string buffer */
244 unsigned int buf_count;
246 /* during rule parsing, we cache uid/gid lookup results */
247 struct uid_gid *uids;
248 unsigned int uids_cur;
249 unsigned int uids_max;
250 struct uid_gid *gids;
251 unsigned int gids_cur;
252 unsigned int gids_max;
255 /* NOTE: we could lookup and return existing strings, or tails of strings */
256 static int add_string(struct udev_rules *rules, const char *str)
258 size_t len = strlen(str)+1;
261 /* offset 0 is always '\0' */
265 /* grow buffer if needed */
266 if (rules->buf_cur + len+1 >= rules->buf_max) {
270 /* double the buffer size */
271 add = rules->buf_max;
275 buf = realloc(rules->buf, rules->buf_max + add);
278 info(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add);
280 rules->buf_max += add;
282 off = rules->buf_cur;
283 memcpy(&rules->buf[rules->buf_cur], str, len);
284 rules->buf_cur += len;
289 static int add_token(struct udev_rules *rules, struct token *token)
292 /* grow buffer if needed */
293 if (rules->token_cur+1 >= rules->token_max) {
294 struct token *tokens;
297 /* double the buffer size */
298 add = rules->token_max;
302 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
305 info(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add);
306 rules->tokens = tokens;
307 rules->token_max += add;
309 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
314 static uid_t add_uid(struct udev_rules *rules, const char *owner)
320 /* lookup, if we know it already */
321 for (i = 0; i < rules->uids_cur; i++) {
322 off = rules->uids[i].name_off;
323 if (strcmp(&rules->buf[off], owner) == 0) {
324 uid = rules->uids[i].uid;
325 dbg(rules->udev, "return existing %u for '%s'\n", uid, owner);
329 uid = util_lookup_user(rules->udev, owner);
331 /* grow buffer if needed */
332 if (rules->uids_cur+1 >= rules->uids_max) {
333 struct uid_gid *uids;
336 /* double the buffer size */
337 add = rules->uids_max;
341 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
344 info(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add);
346 rules->uids_max += add;
348 rules->uids[rules->uids_cur].uid = uid;
349 off = add_string(rules, owner);
352 rules->uids[rules->uids_cur].name_off = off;
357 static gid_t add_gid(struct udev_rules *rules, const char *group)
363 /* lookup, if we know it already */
364 for (i = 0; i < rules->gids_cur; i++) {
365 off = rules->gids[i].name_off;
366 if (strcmp(&rules->buf[off], group) == 0) {
367 gid = rules->gids[i].gid;
368 dbg(rules->udev, "return existing %u for '%s'\n", gid, group);
372 gid = util_lookup_group(rules->udev, group);
374 /* grow buffer if needed */
375 if (rules->gids_cur+1 >= rules->gids_max) {
376 struct uid_gid *gids;
379 /* double the buffer size */
380 add = rules->gids_max;
384 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
387 info(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add);
389 rules->gids_max += add;
391 rules->gids[rules->gids_cur].gid = gid;
392 off = add_string(rules, group);
395 rules->gids[rules->gids_cur].name_off = off;
400 static int import_property_from_string(struct udev_device *dev, char *line)
402 struct udev *udev = udev_device_get_udev(dev);
409 while (isspace(key[0]))
412 /* comment or empty line */
413 if (key[0] == '#' || key[0] == '\0')
416 /* split key/value */
417 val = strchr(key, '=');
424 while (isspace(val[0]))
431 while (isspace(key[len-1]))
435 /* terminate value */
439 while (isspace(val[len-1]))
447 if (val[0] == '"' || val[0] == '\'') {
448 if (val[len-1] != val[0]) {
449 info(udev, "inconsistent quoting: '%s', skip\n", line);
456 info(udev, "adding '%s'='%s'\n", key, val);
458 /* handle device, renamed by external tool, returning new path */
459 if (strcmp(key, "DEVPATH") == 0) {
460 char syspath[UTIL_PATH_SIZE];
462 info(udev, "updating devpath from '%s' to '%s'\n",
463 udev_device_get_devpath(dev), val);
464 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
465 util_strlcat(syspath, val, sizeof(syspath));
466 udev_device_set_syspath(dev, syspath);
468 struct udev_list_entry *entry;
470 entry = udev_device_add_property(dev, key, val);
472 udev_list_entry_set_flag(entry, 1);
477 static int import_file_into_properties(struct udev_device *dev, const char *filename)
480 char line[UTIL_LINE_SIZE];
482 f = fopen(filename, "r");
485 while (fgets(line, sizeof(line), f) != NULL)
486 import_property_from_string(dev, line);
491 static int import_program_into_properties(struct udev_device *dev, const char *program)
493 struct udev *udev = udev_device_get_udev(dev);
499 envp = udev_device_get_properties_envp(dev);
500 if (util_run_program(udev, program, envp, result, sizeof(result), &reslen) != 0)
504 while (line != NULL) {
507 pos = strchr(line, '\n');
512 import_property_from_string(dev, line);
518 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
520 struct udev *udev = udev_device_get_udev(dev);
521 struct udev_device *dev_parent;
522 struct udev_list_entry *list_entry;
524 dev_parent = udev_device_get_parent(dev);
525 if (dev_parent == NULL)
528 dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent));
529 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
530 const char *key = udev_list_entry_get_name(list_entry);
531 const char *val = udev_list_entry_get_value(list_entry);
533 if (fnmatch(filter, key, 0) == 0) {
534 struct udev_list_entry *entry;
536 dbg(udev, "import key '%s=%s'\n", key, val);
537 entry = udev_device_add_property(dev, key, val);
539 udev_list_entry_set_flag(entry, 1);
545 #define WAIT_LOOP_PER_SECOND 50
546 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
548 struct udev *udev = udev_device_get_udev(dev);
549 char filepath[UTIL_PATH_SIZE];
550 char devicepath[UTIL_PATH_SIZE] = "";
552 int loop = timeout * WAIT_LOOP_PER_SECOND;
554 /* a relative path is a device attribute */
555 if (file[0] != '/') {
556 util_strlcpy(devicepath, udev_get_sys_path(udev), sizeof(devicepath));
557 util_strlcat(devicepath, udev_device_get_devpath(dev), sizeof(devicepath));
559 util_strlcpy(filepath, devicepath, sizeof(filepath));
560 util_strlcat(filepath, "/", sizeof(filepath));
561 util_strlcat(filepath, file, sizeof(filepath));
565 dbg(udev, "will wait %i sec for '%s'\n", timeout, file);
568 if (stat(file, &stats) == 0) {
569 info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
572 /* make sure, the device did not disappear in the meantime */
573 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
574 info(udev, "device disappeared while waiting for '%s'\n", file);
577 info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
578 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
580 info(udev, "waiting for '%s' failed\n", file);
584 static int attr_subst_subdir(char *attr, size_t len)
589 pos = strstr(attr, "/*/");
591 char str[UTIL_PATH_SIZE];
595 util_strlcpy(str, &pos[2], sizeof(str));
600 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
603 if (dent->d_name[0] == '.')
605 util_strlcat(attr, dent->d_name, len);
606 util_strlcat(attr, str, len);
607 if (stat(attr, &stats) == 0) {
616 util_strlcat(attr, str, len);
622 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
628 if (linepos == NULL && linepos[0] == '\0')
631 /* skip whitespace */
632 while (isspace(linepos[0]) || linepos[0] == ',')
636 if (linepos[0] == '\0')
642 if (linepos[0] == '\0')
644 if (isspace(linepos[0]))
646 if (linepos[0] == '=')
648 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
649 if (linepos[1] == '=')
653 /* remember end of key */
656 /* skip whitespace after key */
657 while (isspace(linepos[0]))
659 if (linepos[0] == '\0')
662 /* get operation type */
663 if (linepos[0] == '=' && linepos[1] == '=') {
666 } else if (linepos[0] == '!' && linepos[1] == '=') {
669 } else if (linepos[0] == '+' && linepos[1] == '=') {
672 } else if (linepos[0] == '=') {
675 } else if (linepos[0] == ':' && linepos[1] == '=') {
676 *op = OP_ASSIGN_FINAL;
684 /* skip whitespace after operator */
685 while (isspace(linepos[0]))
687 if (linepos[0] == '\0')
691 if (linepos[0] == '"')
698 temp = strchr(linepos, '"');
703 dbg(udev, "%s '%s'-'%s'\n", operation_str[*op], *key, *value);
705 /* move line to next key */
710 /* extract possible KEY{attr} */
711 static char *get_key_attribute(struct udev *udev, char *str)
716 attr = strchr(str, '{');
719 pos = strchr(attr, '}');
721 err(udev, "missing closing brace for format\n");
725 dbg(udev, "attribute='%s'\n", attr);
731 static int rule_add_token(struct rule_tmp *rule_tmp, enum token_type type,
732 enum operation_type op,
733 const char *value, const void *data)
735 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
736 const char *attr = data;
737 enum string_glob_type glob;
739 memset(token, 0x00, sizeof(struct token));
751 case TK_M_SUBSYSTEMS:
754 case TK_M_IMPORT_FILE:
755 case TK_M_IMPORT_PROG:
756 case TK_M_IMPORT_PARENT:
764 token->key.value_off = add_string(rule_tmp->rules, value);
771 token->key.value_off = add_string(rule_tmp->rules, value);
772 token->key.attr_off = add_string(rule_tmp->rules, attr);
775 token->key.value_off = add_string(rule_tmp->rules, value);
777 token->key.mode = *(mode_t *)data;
779 case TK_A_IGNORE_DEVICE:
780 case TK_A_STRING_ESCAPE_NONE:
781 case TK_A_STRING_ESCAPE_REPLACE:
782 case TK_A_IGNORE_REMOVE:
786 token->key.value_off = add_string(rule_tmp->rules, value);
787 token->key.ignore_error = *(int *)data;
789 case TK_A_NUM_FAKE_PART:
790 token->key.num_fake_part = *(int *)data;
792 case TK_A_DEVLINK_PRIO:
793 token->key.devlink_prio = *(int *)data;
796 token->key.uid = *(uid_t *)data;
799 token->key.gid = *(gid_t *)data;
802 token->key.mode = *(mode_t *)data;
804 case TK_A_EVENT_TIMEOUT:
805 token->key.event_timeout = *(int *)data;
808 case TK_M_PARENTS_MAX:
812 err(rule_tmp->rules->udev, "wrong type %u\n", type);
818 if (type < TK_M_MAX) {
819 /* check if we need to split or call fnmatch() while matching rules */
823 has_split = (strchr(value, '|') != NULL);
824 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL ||
825 strchr(value, '[') != NULL || strchr(value, ']') != NULL);
826 if (has_split && has_glob) {
827 glob = GL_SPLIT_GLOB;
828 } else if (has_split) {
830 } else if (has_glob) {
831 if (strcmp(value, "?*") == 0)
837 /* check if we need to substitute format strings for matching rules */
838 if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
845 token->key.glob = glob;
846 rule_tmp->token_cur++;
847 if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) {
848 err(rule_tmp->rules->udev, "temporary rule array too small\n");
855 static void dump_token(struct udev_rules *rules, struct token *token)
857 enum token_type type = token->type;
858 enum operation_type op = token->key.op;
859 enum string_glob_type glob = token->key.glob;
860 const char *value = &rules->buf[token->key.value_off];
861 const char *attr = &rules->buf[token->key.attr_off];
866 const char *tks_ptr = (char *)rules->tokens;
867 const char *tk_ptr = (char *)token;
868 unsigned int off = tk_ptr - tks_ptr;
870 dbg(rules->udev, "* RULE '%s', off: %u(%u), next: %u, label: '%s'\n",
871 &rules->buf[token->rule.filename_off],
872 off / (unsigned int) sizeof(struct token), off,
873 token->rule.next_rule,
874 &rules->buf[token->rule.label_off]);
886 case TK_M_SUBSYSTEMS:
889 case TK_M_IMPORT_FILE:
890 case TK_M_IMPORT_PROG:
891 case TK_M_IMPORT_PARENT:
899 dbg(rules->udev, "%s %s '%s'(%s)\n",
900 token_str[type], operation_str[op], value, string_glob_str[glob]);
907 dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
908 token_str[type], operation_str[op], attr, value, string_glob_str[glob]);
910 case TK_A_IGNORE_DEVICE:
911 case TK_A_STRING_ESCAPE_NONE:
912 case TK_A_STRING_ESCAPE_REPLACE:
914 case TK_A_IGNORE_REMOVE:
915 dbg(rules->udev, "%s\n", token_str[type]);
918 dbg(rules->udev, "%s %s '%s'(%s) %#o\n",
919 token_str[type], operation_str[op], value, string_glob_str[glob], token->key.mode);
921 case TK_A_NUM_FAKE_PART:
922 dbg(rules->udev, "%s %u\n", token_str[type], token->key.num_fake_part);
924 case TK_A_DEVLINK_PRIO:
925 dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.devlink_prio);
928 dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.uid);
931 dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.gid);
934 dbg(rules->udev, "%s %s %#o\n", token_str[type], operation_str[op], token->key.mode);
936 case TK_A_EVENT_TIMEOUT:
937 dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.event_timeout);
940 dbg(rules->udev, "%s '%s' %u\n", token_str[type], value, token->key.rule_goto);
943 dbg(rules->udev, "* %s\n", token_str[type]);
945 case TK_M_PARENTS_MAX:
948 dbg(rules->udev, "unknown type %u\n", type);
953 static void dump_rules(struct udev_rules *rules)
957 dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
959 rules->token_cur * sizeof(struct token),
962 for(i = 0; i < rules->token_cur; i++)
963 dump_token(rules, &rules->tokens[i]);
966 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
967 static inline void dump_rules(struct udev_rules *rules) {}
970 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
973 unsigned int start = 0;
974 unsigned int end = rule_tmp->token_cur;
976 for (i = 0; i < rule_tmp->token_cur; i++) {
977 enum token_type next_val = TK_UNSET;
978 unsigned int next_idx;
981 /* find smallest value */
982 for (j = start; j < end; j++) {
983 if (rule_tmp->token[j].type == TK_UNSET)
985 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
986 next_val = rule_tmp->token[j].type;
991 /* add token and mark done */
992 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
994 rule_tmp->token[next_idx].type = TK_UNSET;
997 if (next_idx == start)
999 if (next_idx+1 == end)
1005 static int add_rule(struct udev_rules *rules, char *line,
1006 const char *filename, unsigned int filename_off, unsigned int lineno)
1012 struct rule_tmp rule_tmp;
1014 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1015 rule_tmp.rules = rules;
1016 rule_tmp.rule.type = TK_RULE;
1017 rule_tmp.rule.rule.filename_off = filename_off;
1023 enum operation_type op;
1025 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1028 if (strcasecmp(key, "ACTION") == 0) {
1029 if (op > OP_MATCH_MAX) {
1030 err(rules->udev, "invalid ACTION operation\n");
1033 rule_add_token(&rule_tmp, TK_M_ACTION, op, value, NULL);
1038 if (strcasecmp(key, "DEVPATH") == 0) {
1039 if (op > OP_MATCH_MAX) {
1040 err(rules->udev, "invalid DEVPATH operation\n");
1043 rule_add_token(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1048 if (strcasecmp(key, "KERNEL") == 0) {
1049 if (op > OP_MATCH_MAX) {
1050 err(rules->udev, "invalid KERNEL operation\n");
1053 rule_add_token(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1058 if (strcasecmp(key, "SUBSYSTEM") == 0) {
1059 if (op > OP_MATCH_MAX) {
1060 err(rules->udev, "invalid SUBSYSTEM operation\n");
1063 /* bus, class, subsystem events should all be the same */
1064 if (strcmp(value, "subsystem") == 0 ||
1065 strcmp(value, "bus") == 0 ||
1066 strcmp(value, "class") == 0) {
1067 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
1068 err(rules->udev, "'%s' must be specified as 'subsystem' \n"
1069 "please fix it in %s:%u", value, filename, lineno);
1070 rule_add_token(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1072 rule_add_token(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1077 if (strcasecmp(key, "DRIVER") == 0) {
1078 if (op > OP_MATCH_MAX) {
1079 err(rules->udev, "invalid DRIVER operation\n");
1082 rule_add_token(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1087 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
1088 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1090 err(rules->udev, "error parsing ATTR attribute\n");
1093 if (op < OP_MATCH_MAX) {
1094 rule_add_token(&rule_tmp, TK_M_ATTR, op, value, attr);
1096 rule_add_token(&rule_tmp, TK_A_ATTR, op, value, attr);
1102 if (strcasecmp(key, "KERNELS") == 0 ||
1103 strcasecmp(key, "ID") == 0) {
1104 if (op > OP_MATCH_MAX) {
1105 err(rules->udev, "invalid KERNELS operation\n");
1108 rule_add_token(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1113 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
1114 strcasecmp(key, "BUS") == 0) {
1115 if (op > OP_MATCH_MAX) {
1116 err(rules->udev, "invalid SUBSYSTEMS operation\n");
1119 rule_add_token(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1124 if (strcasecmp(key, "DRIVERS") == 0) {
1125 if (op > OP_MATCH_MAX) {
1126 err(rules->udev, "invalid DRIVERS operation\n");
1129 rule_add_token(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1134 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1135 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1136 if (op > OP_MATCH_MAX) {
1137 err(rules->udev, "invalid ATTRS operation\n");
1140 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1142 err(rules->udev, "error parsing ATTRS attribute\n");
1145 if (strncmp(attr, "device/", 7) == 0)
1146 err(rules->udev, "the 'device' link may not be available in a future kernel, "
1147 "please fix it in %s:%u", filename, lineno);
1148 else if (strstr(attr, "../") != NULL)
1149 err(rules->udev, "do not reference parent sysfs directories directly, "
1150 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1151 rule_add_token(&rule_tmp, TK_M_ATTRS, op, value, attr);
1156 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
1157 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1159 err(rules->udev, "error parsing ENV attribute\n");
1162 if (strncmp(attr, "PHYSDEV", 7) == 0)
1164 if (op < OP_MATCH_MAX) {
1165 if (rule_add_token(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1168 if (rule_add_token(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1175 if (strcasecmp(key, "PROGRAM") == 0) {
1176 rule_add_token(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1181 if (strcasecmp(key, "RESULT") == 0) {
1182 if (op > OP_MATCH_MAX) {
1183 err(rules->udev, "invalid RESULT operation\n");
1186 rule_add_token(&rule_tmp, TK_M_RESULT, op, value, NULL);
1191 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
1192 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1193 if (attr != NULL && strstr(attr, "program")) {
1194 dbg(rules->udev, "IMPORT will be executed\n");
1195 rule_add_token(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1197 } else if (attr != NULL && strstr(attr, "file")) {
1198 dbg(rules->udev, "IMPORT will be included as file\n");
1199 rule_add_token(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1201 } else if (attr != NULL && strstr(attr, "parent")) {
1202 dbg(rules->udev, "IMPORT will include the parent values\n");
1203 rule_add_token(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1206 /* figure it out if it is executable */
1207 char file[UTIL_PATH_SIZE];
1209 struct stat statbuf;
1211 util_strlcpy(file, value, sizeof(file));
1212 pos = strchr(file, ' ');
1216 /* allow programs in /lib/udev called without the path */
1217 if (strchr(file, '/') == NULL) {
1218 util_strlcpy(file, UDEV_PREFIX "/lib/udev/", sizeof(file));
1219 util_strlcat(file, value, sizeof(file));
1220 pos = strchr(file, ' ');
1225 dbg(rules->udev, "IMPORT auto mode for '%s'\n", file);
1226 if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
1227 dbg(rules->udev, "IMPORT will be executed (autotype)\n");
1228 rule_add_token(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1231 dbg(rules->udev, "IMPORT will be included as file (autotype)\n");
1232 rule_add_token(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1239 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
1242 if (op > OP_MATCH_MAX) {
1243 err(rules->udev, "invalid TEST operation\n");
1246 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1248 mode = strtol(attr, NULL, 8);
1249 rule_add_token(&rule_tmp, TK_M_TEST, op, value, &mode);
1251 rule_add_token(&rule_tmp, TK_M_TEST, op, value, NULL);
1257 if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
1260 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1261 if (attr != NULL && strstr(attr, "ignore_error"))
1263 rule_add_token(&rule_tmp, TK_A_RUN, op, value, &flag);
1268 if (strcasecmp(key, "WAIT_FOR") == 0 || strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
1269 rule_add_token(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1274 if (strcasecmp(key, "LABEL") == 0) {
1275 rule_tmp.rule.rule.label_off = add_string(rules, value);
1280 if (strcasecmp(key, "GOTO") == 0) {
1281 rule_add_token(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1286 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
1287 if (op < OP_MATCH_MAX) {
1288 rule_add_token(&rule_tmp, TK_M_NAME, op, value, NULL);
1290 if (value[0] == '\0')
1291 dbg(rules->udev, "name empty, node creation suppressed\n");
1292 rule_add_token(&rule_tmp, TK_A_NAME, op, value, NULL);
1293 attr = get_key_attribute(rules->udev, key + sizeof("NAME")-1);
1295 if (strstr(attr, "all_partitions") != NULL) {
1296 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1298 dbg(rules->udev, "creation of partition nodes requested\n");
1299 rule_add_token(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1301 if (strstr(attr, "ignore_remove") != NULL) {
1302 dbg(rules->udev, "remove event should be ignored\n");
1303 rule_add_token(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1310 if (strcasecmp(key, "SYMLINK") == 0) {
1311 if (op < OP_MATCH_MAX)
1312 rule_add_token(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1314 rule_add_token(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1319 if (strcasecmp(key, "OWNER") == 0) {
1323 uid = strtoul(value, &endptr, 10);
1324 if (endptr[0] == '\0') {
1325 rule_add_token(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1326 } else if (rules->resolve_names && strchr("$%", value[0]) == NULL) {
1327 uid = add_uid(rules, value);
1328 rule_add_token(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1330 rule_add_token(&rule_tmp, TK_A_OWNER, op, value, NULL);
1336 if (strcasecmp(key, "GROUP") == 0) {
1340 gid = strtoul(value, &endptr, 10);
1341 if (endptr[0] == '\0') {
1342 rule_add_token(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1343 } else if (rules->resolve_names && strchr("$%", value[0]) == NULL) {
1344 gid = add_gid(rules, value);
1345 rule_add_token(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1347 rule_add_token(&rule_tmp, TK_A_GROUP, op, value, NULL);
1353 if (strcasecmp(key, "MODE") == 0) {
1357 mode = strtol(value, &endptr, 8);
1358 if (endptr[0] == '\0')
1359 rule_add_token(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1361 rule_add_token(&rule_tmp, TK_A_MODE, op, value, NULL);
1366 if (strcasecmp(key, "OPTIONS") == 0) {
1369 if (strstr(value, "last_rule") != NULL) {
1370 dbg(rules->udev, "last rule to be applied\n");
1371 rule_add_token(&rule_tmp, TK_A_LAST_RULE, 0, NULL, NULL);
1373 if (strstr(value, "ignore_device") != NULL) {
1374 dbg(rules->udev, "device should be ignored\n");
1375 rule_add_token(&rule_tmp, TK_A_IGNORE_DEVICE, 0, NULL, NULL);
1377 if (strstr(value, "ignore_remove") != NULL) {
1378 dbg(rules->udev, "remove event should be ignored\n");
1379 rule_add_token(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1381 pos = strstr(value, "link_priority=");
1383 int prio = atoi(&pos[strlen("link_priority=")]);
1385 rule_add_token(&rule_tmp, TK_A_DEVLINK_PRIO, 0, NULL, &prio);
1386 dbg(rules->udev, "link priority=%i\n", prio);
1388 pos = strstr(value, "event_timeout=");
1390 int tout = atoi(&pos[strlen("event_timeout=")]);
1392 rule_add_token(&rule_tmp, TK_A_EVENT_TIMEOUT, 0, NULL, &tout);
1393 dbg(rules->udev, "event timout=%i\n", tout);
1395 pos = strstr(value, "string_escape=");
1397 pos = &pos[strlen("string_escape=")];
1398 if (strncmp(pos, "none", strlen("none")) == 0)
1399 rule_add_token(&rule_tmp, TK_A_STRING_ESCAPE_NONE, 0, NULL, NULL);
1400 else if (strncmp(pos, "replace", strlen("replace")) == 0)
1401 rule_add_token(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, 0, NULL, NULL);
1403 if (strstr(value, "all_partitions") != NULL) {
1404 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1406 rule_add_token(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1407 dbg(rules->udev, "creation of partition nodes requested\n");
1412 err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno);
1416 err(rules->udev, "PHYSDEV* values are deprecated and not available on recent kernels, \n"
1417 "please fix it in %s:%u", filename, lineno);
1419 /* skip line if not any valid key was found */
1423 /* add rule token */
1424 if (add_token(rules, &rule_tmp.rule) != 0)
1427 /* add tokens to list, sorted by type */
1428 if (sort_token(rules, &rule_tmp) != 0)
1432 err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
1436 static int parse_file(struct udev_rules *rules, const char *filename)
1439 unsigned int filename_off;
1440 unsigned int first_token;
1441 char line[UTIL_LINE_SIZE];
1445 info(rules->udev, "reading '%s' as rules file\n", filename);
1447 f = fopen(filename, "r");
1451 filename_off = add_string(rules, filename);
1452 first_token = rules->token_cur;
1454 while(fgets(line, sizeof(line), f) != NULL) {
1458 /* skip whitespace */
1461 while (isspace(key[0]))
1472 /* continue reading if backslash+newline is found */
1473 while (line[len-2] == '\\') {
1474 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1480 if (len+1 >= sizeof(line)) {
1481 err(rules->udev, "line too long '%s':%u, ignored\n", filename, line_nr);
1484 add_rule(rules, key, filename, filename_off, line_nr);
1488 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1489 for (i = first_token+1; i < rules->token_cur; i++) {
1490 if (rules->tokens[i].type == TK_A_GOTO) {
1491 char *label = &rules->buf[rules->tokens[i].key.value_off];
1494 for (j = i+1; j < rules->token_cur; j++) {
1495 if (rules->tokens[j].type != TK_RULE)
1497 if (rules->tokens[j].rule.label_off == 0)
1499 if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0)
1501 rules->tokens[i].key.rule_goto = j;
1503 if (rules->tokens[i].key.rule_goto == 0)
1504 err(rules->udev, "GOTO '%s' has no matching label in: '%s'\n", label, filename);
1510 static int add_matching_files(struct udev *udev, struct udev_list_node *file_list, const char *dirname, const char *suffix)
1514 char filename[UTIL_PATH_SIZE];
1516 dbg(udev, "open directory '%s'\n", dirname);
1517 dir = opendir(dirname);
1519 err(udev, "unable to open '%s': %m\n", dirname);
1525 if (ent == NULL || ent->d_name[0] == '\0')
1528 if ((ent->d_name[0] == '.') || (ent->d_name[0] == '#'))
1531 /* look for file matching with specified suffix */
1532 if (suffix != NULL) {
1535 ext = strrchr(ent->d_name, '.');
1538 if (strcmp(ext, suffix) != 0)
1541 dbg(udev, "put file '%s/%s' into list\n", dirname, ent->d_name);
1543 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
1544 filename[sizeof(filename)-1] = '\0';
1545 udev_list_entry_add(udev, file_list, filename, NULL, 1, 1);
1552 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1554 struct udev_rules *rules;
1555 struct stat statbuf;
1556 char filename[PATH_MAX];
1557 struct udev_list_node file_list;
1558 struct udev_list_entry *file_loop, *file_tmp;
1559 unsigned int prev_rule;
1560 struct token end_token;
1563 rules = malloc(sizeof(struct udev_rules));
1566 memset(rules, 0x00, sizeof(struct udev_rules));
1568 rules->resolve_names = resolve_names;
1569 udev_list_init(&file_list);
1571 /* init token array and string buffer */
1572 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1573 if (rules->tokens == NULL)
1575 rules->token_max = PREALLOC_TOKEN;
1576 rules->buf = malloc(PREALLOC_STRBUF);
1577 if (rules->buf == NULL)
1579 rules->buf_max = PREALLOC_STRBUF;
1580 /* offset 0 is always '\0' */
1581 rules->buf[0] = '\0';
1583 info(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1584 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1586 if (udev_get_rules_path(udev) != NULL) {
1587 /* custom rules location for testing */
1588 add_matching_files(udev, &file_list, udev_get_rules_path(udev), ".rules");
1590 struct udev_list_node sort_list;
1591 struct udev_list_entry *sort_loop, *sort_tmp;
1593 /* read user/custom rules */
1594 add_matching_files(udev, &file_list, SYSCONFDIR "/udev/rules.d", ".rules");
1596 /* read dynamic/temporary rules */
1597 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
1598 util_strlcat(filename, "/.udev/rules.d", sizeof(filename));
1599 if (stat(filename, &statbuf) != 0) {
1600 util_create_path(udev, filename);
1601 udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
1602 mkdir(filename, 0755);
1603 udev_selinux_resetfscreatecon(udev);
1605 udev_list_init(&sort_list);
1606 add_matching_files(udev, &sort_list, filename, ".rules");
1608 /* read default rules */
1609 add_matching_files(udev, &sort_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules");
1611 /* sort all rules files by basename into list of files */
1612 udev_list_entry_foreach_safe(sort_loop, sort_tmp, udev_list_get_entry(&sort_list)) {
1613 const char *sort_name = udev_list_entry_get_name(sort_loop);
1614 const char *sort_base = strrchr(sort_name, '/');
1616 if (sort_base == NULL)
1619 udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1620 const char *file_name = udev_list_entry_get_name(file_loop);
1621 const char *file_base = strrchr(file_name, '/');
1623 if (file_base == NULL)
1625 if (strcmp(file_base, sort_base) == 0) {
1626 info(udev, "rule file basename '%s' already added, ignoring '%s'\n",
1627 file_name, sort_name);
1628 udev_list_entry_remove(sort_loop);
1632 if (strcmp(file_base, sort_base) > 0)
1635 if (sort_loop != NULL)
1636 udev_list_entry_move_before(sort_loop, file_loop);
1640 /* parse list of files */
1641 udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1642 const char *file_name = udev_list_entry_get_name(file_loop);
1644 if (stat(file_name, &statbuf) == 0 && statbuf.st_size > 0)
1645 parse_file(rules, file_name);
1647 info(udev, "can not read '%s'\n", file_name);
1648 udev_list_entry_remove(file_loop);
1651 memset(&end_token, 0x00, sizeof(struct token));
1652 end_token.type = TK_END;
1653 add_token(rules, &end_token);
1655 /* link all TK_RULE tokens to be able to fast-forward to next TK_RULE */
1657 for (i = 1; i < rules->token_cur; i++) {
1658 if (rules->tokens[i].type == TK_RULE) {
1659 rules->tokens[prev_rule].rule.next_rule = i;
1664 /* shrink allocated token and string buffer */
1665 if (rules->token_cur < rules->token_max) {
1666 struct token *tokens;
1668 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1669 if (tokens != NULL || rules->token_cur == 0) {
1670 rules->tokens = tokens;
1671 rules->token_max = rules->token_cur;
1674 if (rules->buf_cur < rules->buf_max) {
1677 buf = realloc(rules->buf, rules->buf_cur);
1678 if (buf != NULL || rules->buf_cur == 0) {
1680 rules->buf_max = rules->buf_cur;
1683 info(udev, "shrunk to %lu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1684 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1686 /* cleanup uid/gid cache */
1689 rules->uids_cur = 0;
1690 rules->uids_max = 0;
1693 rules->gids_cur = 0;
1694 rules->gids_max = 0;
1700 void udev_rules_unref(struct udev_rules *rules)
1704 free(rules->tokens);
1711 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1713 char *key_value = &rules->buf[token->key.value_off];
1720 switch (token->key.glob) {
1722 match = (strcmp(key_value, val) == 0);
1725 match = (fnmatch(key_value, val, 0) == 0);
1732 split = &rules->buf[token->key.value_off];
1737 next = strchr(split, '|');
1739 size_t matchlen = (size_t)(next - split);
1741 match = (matchlen == len && strncmp(split, val, matchlen) == 0);
1745 match = (strcmp(split, val) == 0);
1754 char value[UTIL_PATH_SIZE];
1756 util_strlcpy(value, &rules->buf[token->key.value_off], sizeof(value));
1758 while (key_value != NULL) {
1759 pos = strchr(key_value, '|');
1764 dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str[token->type], key_value, val);
1765 match = (fnmatch(key_value, val, 0) == 0);
1773 match = (val[0] != '\0');
1780 if (match && (token->key.op == OP_MATCH)) {
1781 dbg(rules->udev, "%s is true (matching value)\n", token_str[token->type]);
1784 if (!match && (token->key.op == OP_NOMATCH)) {
1785 dbg(rules->udev, "%s is true (non-matching value)\n", token_str[token->type]);
1788 dbg(rules->udev, "%s is not true\n", token_str[token->type]);
1792 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1794 char attr[UTIL_PATH_SIZE];
1795 const char *key_name = &rules->buf[cur->key.attr_off];
1796 const char *key_value = &rules->buf[cur->key.value_off];
1797 char value[UTIL_NAME_SIZE] = "";
1800 util_strlcpy(attr, key_name, sizeof(attr));
1801 util_resolve_subsys_kernel(event->udev, attr, value, sizeof(value), 1);
1802 if (value[0] == '\0') {
1805 val = udev_device_get_sysattr_value(dev, key_name);
1807 util_strlcpy(value, val, sizeof(value));
1812 /* strip trailing whitespace of value, if not asked to match for it */
1813 len = strlen(key_value);
1814 if (len > 0 && !isspace(key_value[len-1])) {
1815 len = strlen(value);
1816 while (len > 0 && isspace(value[--len]))
1818 dbg(rules->udev, "removed trailing whitespace from '%s'\n", value);
1820 return match_key(rules, cur, value);
1829 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event)
1834 if (rules->tokens == NULL)
1837 /* loop through token list, match, run actions or forward to next rule */
1838 cur = &rules->tokens[0];
1840 while (cur != NULL && cur->type != TK_END) {
1841 enum escape_type esc = ESCAPE_UNSET;
1844 dump_token(rules, cur);
1845 switch (cur->type) {
1852 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1856 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1860 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1865 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
1866 struct udev_list_entry *list_entry;
1869 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1870 const char *devlink;
1872 devlink = &udev_list_entry_get_name(list_entry)[devlen];
1873 if (match_key(rules, cur, devlink) == 0) {
1883 if (match_key(rules, cur, event->name) != 0)
1888 struct udev_list_entry *list_entry;
1889 const char *key_name = &rules->buf[cur->key.attr_off];
1892 list_entry = udev_device_get_properties_list_entry(event->dev);
1893 list_entry = udev_list_entry_get_by_name(list_entry, key_name);
1894 value = udev_list_entry_get_value(list_entry);
1895 if (value == NULL) {
1896 dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name);
1899 if (match_key(rules, cur, value))
1903 case TK_M_SUBSYSTEM:
1904 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1908 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1913 char filename[UTIL_PATH_SIZE];
1916 util_strlcpy(filename, &rules->buf[cur->key.value_off], sizeof(filename));
1917 udev_event_apply_format(event, filename, sizeof(filename));
1918 found = (wait_for_file(event->dev, filename, 10) == 0);
1919 if (!found && (cur->key.op != OP_NOMATCH))
1924 if (match_attr(rules, event->dev, event, cur) != 0)
1928 case TK_M_SUBSYSTEMS:
1934 /* get whole sequence of parent matches */
1936 while (next->type < TK_M_PARENTS_MAX)
1939 /* loop over parents */
1940 event->dev_parent = event->dev;
1944 dbg(event->udev, "parent: '%s'\n", udev_device_get_syspath(event->dev_parent));
1945 /* loop over sequence of parent match keys */
1946 for (key = cur; key < next; key++ ) {
1947 dump_token(rules, key);
1950 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1953 case TK_M_SUBSYSTEMS:
1954 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1958 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1962 if (match_attr(rules, event->dev_parent, event, key) != 0)
1968 dbg(event->udev, "parent key matched\n");
1970 dbg(event->udev, "all parent keys matched\n");
1971 /* all keys matched */
1975 event->dev_parent = udev_device_get_parent(event->dev_parent);
1976 if (event->dev_parent == NULL)
1979 /* move behind our sequence of parent match keys */
1985 char filename[UTIL_PATH_SIZE];
1986 struct stat statbuf;
1989 util_strlcpy(filename, &rules->buf[cur->key.value_off], sizeof(filename));
1990 udev_event_apply_format(event, filename, sizeof(filename));
1991 if (util_resolve_subsys_kernel(event->udev, NULL, filename, sizeof(filename), 0) != 0)
1992 if (filename[0] != '/') {
1993 char tmp[UTIL_PATH_SIZE];
1995 util_strlcpy(tmp, udev_device_get_syspath(event->dev), sizeof(tmp));
1996 util_strlcat(tmp, "/", sizeof(tmp));
1997 util_strlcat(tmp, filename, sizeof(tmp));
1998 util_strlcpy(filename, tmp, sizeof(filename));
2001 attr_subst_subdir(filename, sizeof(filename));
2003 match = (stat(filename, &statbuf) == 0);
2004 info(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
2005 if (match && cur->key.mode > 0) {
2006 match = ((statbuf.st_mode & cur->key.mode) > 0);
2007 info(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
2008 match ? "matches" : "does not match", cur->key.mode);
2010 if (match && cur->key.op == OP_NOMATCH)
2012 if (!match && cur->key.op == OP_MATCH)
2018 char program[UTIL_PATH_SIZE];
2020 char result[UTIL_PATH_SIZE];
2022 free(event->program_result);
2023 event->program_result = NULL;
2024 util_strlcpy(program, &rules->buf[cur->key.value_off], sizeof(program));
2025 udev_event_apply_format(event, program, sizeof(program));
2026 envp = udev_device_get_properties_envp(event->dev);
2027 if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
2028 if (cur->key.op != OP_NOMATCH)
2033 util_remove_trailing_chars(result, '\n');
2034 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2035 count = util_replace_chars(result, ALLOWED_CHARS_INPUT);
2037 info(event->udev, "%i character(s) replaced\n" , count);
2039 event->program_result = strdup(result);
2040 dbg(event->udev, "storing result '%s'\n", event->program_result);
2041 if (cur->key.op == OP_NOMATCH)
2046 case TK_M_IMPORT_FILE:
2048 char import[UTIL_PATH_SIZE];
2050 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2051 udev_event_apply_format(event, import, sizeof(import));
2052 if (import_file_into_properties(event->dev, import) != 0)
2053 if (cur->key.op != OP_NOMATCH)
2057 case TK_M_IMPORT_PROG:
2059 char import[UTIL_PATH_SIZE];
2061 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2062 udev_event_apply_format(event, import, sizeof(import));
2063 if (import_program_into_properties(event->dev, import) != 0)
2064 if (cur->key.op != OP_NOMATCH)
2068 case TK_M_IMPORT_PARENT:
2070 char import[UTIL_PATH_SIZE];
2072 util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
2073 udev_event_apply_format(event, import, sizeof(import));
2074 if (import_parent_into_properties(event->dev, import) != 0)
2075 if (cur->key.op != OP_NOMATCH)
2080 if (match_key(rules, cur, event->program_result) != 0)
2084 case TK_A_IGNORE_DEVICE:
2085 event->ignore_device = 1;
2088 case TK_A_STRING_ESCAPE_NONE:
2091 case TK_A_STRING_ESCAPE_REPLACE:
2092 esc = ESCAPE_REPLACE;
2094 case TK_A_NUM_FAKE_PART:
2095 if (strcmp(udev_device_get_subsystem(event->dev), "block") != 0)
2097 if (udev_device_get_sysnum(event->dev) != NULL)
2099 udev_device_set_num_fake_partitions(event->dev, cur->key.num_fake_part);
2101 case TK_A_DEVLINK_PRIO:
2102 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2106 char owner[UTIL_NAME_SIZE];
2108 if (event->owner_final)
2110 if (cur->key.op == OP_ASSIGN_FINAL)
2111 event->owner_final = 1;
2112 util_strlcpy(owner, &rules->buf[cur->key.value_off], sizeof(owner));
2113 udev_event_apply_format(event, owner, sizeof(owner));
2114 event->uid = util_lookup_user(event->udev, owner);
2119 char group[UTIL_NAME_SIZE];
2121 if (event->group_final)
2123 if (cur->key.op == OP_ASSIGN_FINAL)
2124 event->group_final = 1;
2125 util_strlcpy(group, &rules->buf[cur->key.value_off], sizeof(group));
2126 udev_event_apply_format(event, group, sizeof(group));
2127 event->gid = util_lookup_group(event->udev, group);
2132 char mode[UTIL_NAME_SIZE];
2135 if (event->mode_final)
2137 if (cur->key.op == OP_ASSIGN_FINAL)
2138 event->mode_final = 1;
2139 util_strlcpy(mode, &rules->buf[cur->key.value_off], sizeof(mode));
2140 udev_event_apply_format(event, mode, sizeof(mode));
2141 event->mode = strtol(mode, &endptr, 8);
2142 if (endptr[0] != '\0') {
2143 err(event->udev, "invalide mode '%s' set default mode 0660\n", mode);
2149 if (event->owner_final)
2151 if (cur->key.op == OP_ASSIGN_FINAL)
2152 event->owner_final = 1;
2153 event->uid = cur->key.uid;
2156 if (event->group_final)
2158 if (cur->key.op == OP_ASSIGN_FINAL)
2159 event->group_final = 1;
2160 event->gid = cur->key.gid;
2163 if (event->mode_final)
2165 if (cur->key.op == OP_ASSIGN_FINAL)
2166 event->mode_final = 1;
2167 event->mode = cur->key.mode;
2171 const char *name = &rules->buf[cur->key.attr_off];
2172 char *value = &rules->buf[cur->key.value_off];
2174 if (value[0] != '\0') {
2175 char temp_value[UTIL_NAME_SIZE];
2176 struct udev_list_entry *entry;
2178 util_strlcpy(temp_value, value, sizeof(temp_value));
2179 udev_event_apply_format(event, temp_value, sizeof(temp_value));
2180 entry = udev_device_add_property(event->dev, name, temp_value);
2182 udev_list_entry_set_flag(entry, 1);
2184 udev_device_add_property(event->dev, name, NULL);
2190 const char *name = &rules->buf[cur->key.value_off];
2191 char name_str[UTIL_PATH_SIZE];
2194 if (event->name_final)
2196 if (cur->key.op == OP_ASSIGN_FINAL)
2197 event->name_final = 1;
2198 if (name[0] == '\0') {
2203 util_strlcpy(name_str, name, sizeof(name_str));
2204 udev_event_apply_format(event, name_str, sizeof(name_str));
2205 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2206 count = util_replace_chars(name_str, ALLOWED_CHARS_FILE);
2208 info(event->udev, "%i character(s) replaced\n", count);
2210 event->name = strdup(name_str);
2216 char temp[UTIL_PATH_SIZE];
2217 char filename[UTIL_PATH_SIZE];
2221 if (event->devlink_final)
2223 if (major(udev_device_get_devnum(event->dev)) == 0)
2225 if (cur->key.op == OP_ASSIGN_FINAL)
2226 event->devlink_final = 1;
2227 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2228 udev_device_cleanup_devlinks_list(event->dev);
2230 /* allow multiple symlinks separated by spaces */
2231 util_strlcpy(temp, &rules->buf[cur->key.value_off], sizeof(temp));
2232 udev_event_apply_format(event, temp, sizeof(temp));
2233 if (esc == ESCAPE_UNSET)
2234 count = util_replace_chars(temp, ALLOWED_CHARS_FILE " ");
2235 else if (esc == ESCAPE_REPLACE)
2236 count = util_replace_chars(temp, ALLOWED_CHARS_FILE);
2238 info(event->udev, "%i character(s) replaced\n" , count);
2239 dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
2241 while (isspace(pos[0]))
2243 next = strchr(pos, ' ');
2246 info(event->udev, "add symlink '%s'\n", pos);
2247 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
2248 util_strlcat(filename, "/", sizeof(filename));
2249 util_strlcat(filename, pos, sizeof(filename));
2250 udev_device_add_devlink(event->dev, filename);
2251 while (isspace(next[1]))
2254 next = strchr(pos, ' ');
2256 if (pos[0] != '\0') {
2257 info(event->udev, "add symlink '%s'\n", pos);
2258 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
2259 util_strlcat(filename, "/", sizeof(filename));
2260 util_strlcat(filename, pos, sizeof(filename));
2261 udev_device_add_devlink(event->dev, filename);
2265 case TK_A_EVENT_TIMEOUT:
2266 udev_device_set_event_timeout(event->dev, cur->key.event_timeout);
2268 case TK_A_IGNORE_REMOVE:
2269 udev_device_set_ignore_remove(event->dev, 1);
2273 const char *key_name = &rules->buf[cur->key.attr_off];
2274 char attr[UTIL_PATH_SIZE];
2275 char value[UTIL_NAME_SIZE];
2278 util_strlcpy(attr, key_name, sizeof(attr));
2279 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0) {
2280 util_strlcpy(attr, udev_device_get_syspath(event->dev), sizeof(attr));
2281 util_strlcat(attr, "/", sizeof(attr));
2282 util_strlcat(attr, key_name, sizeof(attr));
2285 attr_subst_subdir(attr, sizeof(attr));
2287 util_strlcpy(value, &rules->buf[cur->key.value_off], sizeof(value));
2288 udev_event_apply_format(event, value, sizeof(value));
2289 info(event->udev, "writing '%s' to sysfs file '%s'\n", value, attr);
2290 f = fopen(attr, "w");
2293 if (fprintf(f, "%s", value) <= 0)
2294 err(event->udev, "error writing ATTR{%s}: %m\n", attr);
2297 err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);
2303 struct udev_list_entry *list_entry;
2305 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2306 udev_list_cleanup_entries(event->udev, &event->run_list);
2307 list_entry = udev_list_entry_add(event->udev, &event->run_list,
2308 &rules->buf[cur->key.value_off], NULL, 1, 0);
2309 if (cur->key.ignore_error)
2310 udev_list_entry_set_flag(list_entry, 1);
2314 cur = &rules->tokens[cur->key.rule_goto];
2316 case TK_A_LAST_RULE:
2319 case TK_M_PARENTS_MAX:
2323 err(rules->udev, "wrong type %u\n", cur->type);
2330 /* fast-forward to next rule */
2331 idx = rule->rule.next_rule;
2334 dbg(rules->udev, "forward to rule: %u\n", idx);
2335 cur = &rules->tokens[idx];