2 * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "conf-files.h"
36 #define PREALLOC_TOKEN 2048
37 #define PREALLOC_STRBUF 32 * 1024
38 #define PREALLOC_TRIE 256
41 unsigned int name_off;
49 /* this node's first child */
50 unsigned int child_idx;
51 /* the next child of our parent node's child list */
52 unsigned int next_child_idx;
53 /* this node's last child (shortcut for append) */
54 unsigned int last_child_idx;
55 unsigned int value_off;
56 unsigned short value_len;
63 unsigned long long *dirs_ts_usec;
66 /* every key in the rules file becomes a token */
68 unsigned int token_cur;
69 unsigned int token_max;
71 /* all key strings are copied to a single string buffer */
75 unsigned int buf_count;
77 /* during rule parsing, strings are indexed and de-duplicated */
78 struct trie_node *trie_nodes;
79 unsigned int trie_nodes_cur;
80 unsigned int trie_nodes_max;
82 /* during rule parsing, uid/gid lookup results are cached */
84 unsigned int uids_cur;
85 unsigned int uids_max;
87 unsigned int gids_cur;
88 unsigned int gids_max;
91 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
104 enum string_glob_type {
106 GL_PLAIN, /* no special chars */
107 GL_GLOB, /* shell globs ?,*,[] */
108 GL_SPLIT, /* multi-value A|B */
109 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
110 GL_SOMETHING, /* commonly used "?*" */
113 enum string_subst_type {
120 /* tokens of a rule are sorted/handled in this order */
125 TK_M_ACTION, /* val */
126 TK_M_DEVPATH, /* val */
127 TK_M_KERNEL, /* val */
128 TK_M_DEVLINK, /* val */
130 TK_M_ENV, /* val, attr */
132 TK_M_SUBSYSTEM, /* val */
133 TK_M_DRIVER, /* val */
134 TK_M_WAITFOR, /* val */
135 TK_M_ATTR, /* val, attr */
138 TK_M_KERNELS, /* val */
139 TK_M_SUBSYSTEMS, /* val */
140 TK_M_DRIVERS, /* val */
141 TK_M_ATTRS, /* val, attr */
145 TK_M_TEST, /* val, mode_t */
146 TK_M_EVENT_TIMEOUT, /* int */
147 TK_M_PROGRAM, /* val */
148 TK_M_IMPORT_FILE, /* val */
149 TK_M_IMPORT_PROG, /* val */
150 TK_M_IMPORT_BUILTIN, /* val */
151 TK_M_IMPORT_DB, /* val */
152 TK_M_IMPORT_CMDLINE, /* val */
153 TK_M_IMPORT_PARENT, /* val */
154 TK_M_RESULT, /* val */
157 TK_A_STRING_ESCAPE_NONE,
158 TK_A_STRING_ESCAPE_REPLACE,
160 TK_A_INOTIFY_WATCH, /* int */
161 TK_A_DEVLINK_PRIO, /* int */
162 TK_A_OWNER, /* val */
163 TK_A_GROUP, /* val */
165 TK_A_OWNER_ID, /* uid_t */
166 TK_A_GROUP_ID, /* gid_t */
167 TK_A_MODE_ID, /* mode_t */
168 TK_A_STATIC_NODE, /* val */
169 TK_A_ENV, /* val, attr */
172 TK_A_DEVLINK, /* val */
173 TK_A_ATTR, /* val, attr */
174 TK_A_RUN_BUILTIN, /* val, bool */
175 TK_A_RUN_PROGRAM, /* val, bool */
176 TK_A_GOTO, /* size_t */
181 /* we try to pack stuff in a way that we take only 12 bytes per token */
184 unsigned char type; /* same in rule and key */
186 enum token_type type:8;
188 bool has_static_node:1;
189 unsigned int unused:6;
190 unsigned short token_count;
191 unsigned int label_off;
192 unsigned short filename_off;
193 unsigned short filename_line;
196 enum token_type type:8;
197 enum operation_type op:8;
198 enum string_glob_type glob:8;
199 enum string_subst_type subst:4;
200 enum string_subst_type attrsubst:4;
201 unsigned int value_off;
203 unsigned int attr_off;
205 unsigned int rule_goto;
212 enum udev_builtin_cmd builtin_cmd;
220 struct udev_rules *rules;
222 struct token token[MAX_TK];
223 unsigned int token_cur;
227 static const char *operation_str(enum operation_type type)
229 static const char *operation_strs[] = {
230 [OP_UNSET] = "UNSET",
231 [OP_MATCH] = "match",
232 [OP_NOMATCH] = "nomatch",
233 [OP_MATCH_MAX] = "MATCH_MAX",
236 [OP_ASSIGN] = "assign",
237 [OP_ASSIGN_FINAL] = "assign-final",
240 return operation_strs[type];
243 static const char *string_glob_str(enum string_glob_type type)
245 static const char *string_glob_strs[] = {
246 [GL_UNSET] = "UNSET",
247 [GL_PLAIN] = "plain",
249 [GL_SPLIT] = "split",
250 [GL_SPLIT_GLOB] = "split-glob",
251 [GL_SOMETHING] = "split-glob",
254 return string_glob_strs[type];
257 static const char *token_str(enum token_type type)
259 static const char *token_strs[] = {
260 [TK_UNSET] = "UNSET",
263 [TK_M_ACTION] = "M ACTION",
264 [TK_M_DEVPATH] = "M DEVPATH",
265 [TK_M_KERNEL] = "M KERNEL",
266 [TK_M_DEVLINK] = "M DEVLINK",
267 [TK_M_NAME] = "M NAME",
268 [TK_M_ENV] = "M ENV",
269 [TK_M_TAG] = "M TAG",
270 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
271 [TK_M_DRIVER] = "M DRIVER",
272 [TK_M_WAITFOR] = "M WAITFOR",
273 [TK_M_ATTR] = "M ATTR",
275 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
276 [TK_M_KERNELS] = "M KERNELS",
277 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
278 [TK_M_DRIVERS] = "M DRIVERS",
279 [TK_M_ATTRS] = "M ATTRS",
280 [TK_M_TAGS] = "M TAGS",
281 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
283 [TK_M_TEST] = "M TEST",
284 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
285 [TK_M_PROGRAM] = "M PROGRAM",
286 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
287 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
288 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
289 [TK_M_IMPORT_DB] = "M IMPORT_DB",
290 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
291 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
292 [TK_M_RESULT] = "M RESULT",
293 [TK_M_MAX] = "M MAX",
295 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
296 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
297 [TK_A_DB_PERSIST] = "A DB_PERSIST",
298 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
299 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
300 [TK_A_OWNER] = "A OWNER",
301 [TK_A_GROUP] = "A GROUP",
302 [TK_A_MODE] = "A MODE",
303 [TK_A_OWNER_ID] = "A OWNER_ID",
304 [TK_A_GROUP_ID] = "A GROUP_ID",
305 [TK_A_STATIC_NODE] = "A STATIC_NODE",
306 [TK_A_MODE_ID] = "A MODE_ID",
307 [TK_A_ENV] = "A ENV",
308 [TK_A_TAG] = "A ENV",
309 [TK_A_NAME] = "A NAME",
310 [TK_A_DEVLINK] = "A DEVLINK",
311 [TK_A_ATTR] = "A ATTR",
312 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
313 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
314 [TK_A_GOTO] = "A GOTO",
319 return token_strs[type];
322 static void dump_token(struct udev_rules *rules, struct token *token)
324 enum token_type type = token->type;
325 enum operation_type op = token->key.op;
326 enum string_glob_type glob = token->key.glob;
327 const char *value = &rules->buf[token->key.value_off];
328 const char *attr = &rules->buf[token->key.attr_off];
333 const char *tks_ptr = (char *)rules->tokens;
334 const char *tk_ptr = (char *)token;
335 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
337 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
338 &rules->buf[token->rule.filename_off], token->rule.filename_line,
339 idx, token->rule.token_count,
340 &rules->buf[token->rule.label_off]);
352 case TK_M_SUBSYSTEMS:
356 case TK_M_IMPORT_FILE:
357 case TK_M_IMPORT_PROG:
359 case TK_M_IMPORT_CMDLINE:
360 case TK_M_IMPORT_PARENT:
367 case TK_A_RUN_BUILTIN:
368 case TK_A_RUN_PROGRAM:
369 log_debug("%s %s '%s'(%s)\n",
370 token_str(type), operation_str(op), value, string_glob_str(glob));
372 case TK_M_IMPORT_BUILTIN:
373 log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
380 log_debug("%s %s '%s' '%s'(%s)\n",
381 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
385 log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value);
387 case TK_A_STRING_ESCAPE_NONE:
388 case TK_A_STRING_ESCAPE_REPLACE:
389 case TK_A_DB_PERSIST:
390 log_debug("%s\n", token_str(type));
393 log_debug("%s %s '%s'(%s) %#o\n",
394 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
396 case TK_A_INOTIFY_WATCH:
397 log_debug("%s %u\n", token_str(type), token->key.watch);
399 case TK_A_DEVLINK_PRIO:
400 log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
403 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
406 log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
409 log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
411 case TK_A_STATIC_NODE:
412 log_debug("%s '%s'\n", token_str(type), value);
414 case TK_M_EVENT_TIMEOUT:
415 log_debug("%s %u\n", token_str(type), token->key.event_timeout);
418 log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
421 log_debug("* %s\n", token_str(type));
423 case TK_M_PARENTS_MIN:
424 case TK_M_PARENTS_MAX:
427 log_debug("unknown type %u\n", type);
432 static void dump_rules(struct udev_rules *rules)
436 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
438 rules->token_cur * sizeof(struct token),
441 for(i = 0; i < rules->token_cur; i++)
442 dump_token(rules, &rules->tokens[i]);
445 static inline const char *operation_str(enum operation_type type) { return NULL; }
446 static inline const char *token_str(enum token_type type) { return NULL; }
447 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
448 static inline void dump_rules(struct udev_rules *rules) {}
451 static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
455 /* grow buffer if needed */
456 if (rules->buf_cur + bytes+1 >= rules->buf_max) {
460 /* double the buffer size */
461 add = rules->buf_max;
465 buf = realloc(rules->buf, rules->buf_max + add);
469 rules->buf_max += add;
471 off = rules->buf_cur;
472 memcpy(&rules->buf[rules->buf_cur], str, bytes);
473 rules->buf_cur += bytes;
478 static int add_string(struct udev_rules *rules, const char *str)
480 unsigned int node_idx;
481 struct trie_node *new_node;
482 unsigned int new_node_idx;
487 struct trie_node *parent;
489 /* walk trie, start from last character of str to find matching tails */
493 for (depth = 0; depth <= len; depth++) {
494 struct trie_node *node;
495 unsigned int child_idx;
497 node = &rules->trie_nodes[node_idx];
498 off = node->value_off + node->value_len - len;
500 /* match against current node */
501 if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0))
504 /* lookup child node */
505 key = str[len - 1 - depth];
506 child_idx = node->child_idx;
507 while (child_idx > 0) {
508 struct trie_node *child;
510 child = &rules->trie_nodes[child_idx];
511 if (child->key == key)
513 child_idx = child->next_child_idx;
517 node_idx = child_idx;
520 /* string not found, add it */
521 off = add_new_string(rules, str, len + 1);
523 /* grow trie nodes if needed */
524 if (rules->trie_nodes_cur >= rules->trie_nodes_max) {
525 struct trie_node *nodes;
528 /* double the buffer size */
529 add = rules->trie_nodes_max;
533 nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node));
536 rules->trie_nodes = nodes;
537 rules->trie_nodes_max += add;
541 new_node_idx = rules->trie_nodes_cur;
542 rules->trie_nodes_cur++;
543 new_node = &rules->trie_nodes[new_node_idx];
544 memset(new_node, 0x00, sizeof(struct trie_node));
545 new_node->value_off = off;
546 new_node->value_len = len;
549 /* join the parent's child list */
550 parent = &rules->trie_nodes[node_idx];
551 if (parent->child_idx == 0) {
552 parent->child_idx = new_node_idx;
554 struct trie_node *last_child;
556 last_child = &rules->trie_nodes[parent->last_child_idx];
557 last_child->next_child_idx = new_node_idx;
559 parent->last_child_idx = new_node_idx;
563 static int add_token(struct udev_rules *rules, struct token *token)
565 /* grow buffer if needed */
566 if (rules->token_cur+1 >= rules->token_max) {
567 struct token *tokens;
570 /* double the buffer size */
571 add = rules->token_max;
575 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
578 rules->tokens = tokens;
579 rules->token_max += add;
581 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
586 static uid_t add_uid(struct udev_rules *rules, const char *owner)
592 /* lookup, if we know it already */
593 for (i = 0; i < rules->uids_cur; i++) {
594 off = rules->uids[i].name_off;
595 if (streq(&rules->buf[off], owner)) {
596 uid = rules->uids[i].uid;
600 uid = util_lookup_user(rules->udev, owner);
602 /* grow buffer if needed */
603 if (rules->uids_cur+1 >= rules->uids_max) {
604 struct uid_gid *uids;
607 /* double the buffer size */
608 add = rules->uids_max;
612 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
616 rules->uids_max += add;
618 rules->uids[rules->uids_cur].uid = uid;
619 off = add_string(rules, owner);
622 rules->uids[rules->uids_cur].name_off = off;
627 static gid_t add_gid(struct udev_rules *rules, const char *group)
633 /* lookup, if we know it already */
634 for (i = 0; i < rules->gids_cur; i++) {
635 off = rules->gids[i].name_off;
636 if (streq(&rules->buf[off], group)) {
637 gid = rules->gids[i].gid;
641 gid = util_lookup_group(rules->udev, group);
643 /* grow buffer if needed */
644 if (rules->gids_cur+1 >= rules->gids_max) {
645 struct uid_gid *gids;
648 /* double the buffer size */
649 add = rules->gids_max;
653 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
657 rules->gids_max += add;
659 rules->gids[rules->gids_cur].gid = gid;
660 off = add_string(rules, group);
663 rules->gids[rules->gids_cur].name_off = off;
668 static int import_property_from_string(struct udev_device *dev, char *line)
676 while (isspace(key[0]))
679 /* comment or empty line */
680 if (key[0] == '#' || key[0] == '\0')
683 /* split key/value */
684 val = strchr(key, '=');
691 while (isspace(val[0]))
698 while (isspace(key[len-1]))
702 /* terminate value */
706 while (isspace(val[len-1]))
714 if (val[0] == '"' || val[0] == '\'') {
715 if (val[len-1] != val[0]) {
716 log_debug("inconsistent quoting: '%s', skip\n", line);
723 /* handle device, renamed by external tool, returning new path */
724 if (streq(key, "DEVPATH")) {
725 char syspath[UTIL_PATH_SIZE];
727 log_debug("updating devpath from '%s' to '%s'\n",
728 udev_device_get_devpath(dev), val);
729 util_strscpyl(syspath, sizeof(syspath), "/sys", val, NULL);
730 udev_device_set_syspath(dev, syspath);
732 struct udev_list_entry *entry;
734 entry = udev_device_add_property(dev, key, val);
735 /* store in db, skip private keys */
737 udev_list_entry_set_num(entry, true);
742 static int import_file_into_properties(struct udev_device *dev, const char *filename)
745 char line[UTIL_LINE_SIZE];
747 f = fopen(filename, "r");
750 while (fgets(line, sizeof(line), f) != NULL)
751 import_property_from_string(dev, line);
756 static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
758 struct udev_device *dev = event->dev;
760 char result[UTIL_LINE_SIZE];
764 envp = udev_device_get_properties_envp(dev);
765 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
770 while (line != NULL) {
773 pos = strchr(line, '\n');
778 import_property_from_string(dev, line);
784 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
786 struct udev_device *dev_parent;
787 struct udev_list_entry *list_entry;
789 dev_parent = udev_device_get_parent(dev);
790 if (dev_parent == NULL)
793 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
794 const char *key = udev_list_entry_get_name(list_entry);
795 const char *val = udev_list_entry_get_value(list_entry);
797 if (fnmatch(filter, key, 0) == 0) {
798 struct udev_list_entry *entry;
800 entry = udev_device_add_property(dev, key, val);
801 /* store in db, skip private keys */
803 udev_list_entry_set_num(entry, true);
809 #define WAIT_LOOP_PER_SECOND 50
810 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
812 char filepath[UTIL_PATH_SIZE];
813 char devicepath[UTIL_PATH_SIZE];
815 int loop = timeout * WAIT_LOOP_PER_SECOND;
817 /* a relative path is a device attribute */
818 devicepath[0] = '\0';
819 if (file[0] != '/') {
820 util_strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
821 util_strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
826 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
829 if (stat(file, &stats) == 0) {
830 log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
833 /* make sure, the device did not disappear in the meantime */
834 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
835 log_debug("device disappeared while waiting for '%s'\n", file);
838 log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
839 nanosleep(&duration, NULL);
841 log_debug("waiting for '%s' failed\n", file);
845 static int attr_subst_subdir(char *attr, size_t len)
849 if (strstr(attr, "/*/")) {
851 char dirname[UTIL_PATH_SIZE];
855 util_strscpy(dirname, sizeof(dirname), attr);
856 pos = strstr(dirname, "/*/");
861 dir = opendir(dirname);
865 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
868 if (dent->d_name[0] == '.')
870 util_strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
871 if (stat(attr, &stats) == 0) {
883 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
889 if (linepos == NULL || linepos[0] == '\0')
892 /* skip whitespace */
893 while (isspace(linepos[0]) || linepos[0] == ',')
897 if (linepos[0] == '\0')
903 if (linepos[0] == '\0')
905 if (isspace(linepos[0]))
907 if (linepos[0] == '=')
909 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
910 if (linepos[1] == '=')
914 /* remember end of key */
917 /* skip whitespace after key */
918 while (isspace(linepos[0]))
920 if (linepos[0] == '\0')
923 /* get operation type */
924 if (linepos[0] == '=' && linepos[1] == '=') {
927 } else if (linepos[0] == '!' && linepos[1] == '=') {
930 } else if (linepos[0] == '+' && linepos[1] == '=') {
933 } else if (linepos[0] == '=') {
936 } else if (linepos[0] == ':' && linepos[1] == '=') {
937 *op = OP_ASSIGN_FINAL;
945 /* skip whitespace after operator */
946 while (isspace(linepos[0]))
948 if (linepos[0] == '\0')
952 if (linepos[0] == '"')
959 temp = strchr(linepos, '"');
965 /* move line to next key */
970 /* extract possible KEY{attr} */
971 static const char *get_key_attribute(struct udev *udev, char *str)
976 attr = strchr(str, '{');
979 pos = strchr(attr, '}');
981 log_error("missing closing brace for format\n");
990 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
991 enum operation_type op,
992 const char *value, const void *data)
994 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
995 const char *attr = NULL;
997 memset(token, 0x00, sizeof(struct token));
1003 case TK_M_SUBSYSTEM:
1009 case TK_M_SUBSYSTEMS:
1013 case TK_M_IMPORT_FILE:
1014 case TK_M_IMPORT_PROG:
1015 case TK_M_IMPORT_DB:
1016 case TK_M_IMPORT_CMDLINE:
1017 case TK_M_IMPORT_PARENT:
1026 token->key.value_off = add_string(rule_tmp->rules, value);
1028 case TK_M_IMPORT_BUILTIN:
1029 token->key.value_off = add_string(rule_tmp->rules, value);
1030 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
1038 token->key.value_off = add_string(rule_tmp->rules, value);
1039 token->key.attr_off = add_string(rule_tmp->rules, attr);
1042 token->key.value_off = add_string(rule_tmp->rules, value);
1043 token->key.devlink_unique = *(int *)data;
1046 token->key.value_off = add_string(rule_tmp->rules, value);
1048 token->key.mode = *(mode_t *)data;
1050 case TK_A_STRING_ESCAPE_NONE:
1051 case TK_A_STRING_ESCAPE_REPLACE:
1052 case TK_A_DB_PERSIST:
1054 case TK_A_RUN_BUILTIN:
1055 case TK_A_RUN_PROGRAM:
1056 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
1057 token->key.value_off = add_string(rule_tmp->rules, value);
1059 case TK_A_INOTIFY_WATCH:
1060 case TK_A_DEVLINK_PRIO:
1061 token->key.devlink_prio = *(int *)data;
1064 token->key.uid = *(uid_t *)data;
1067 token->key.gid = *(gid_t *)data;
1070 token->key.mode = *(mode_t *)data;
1072 case TK_A_STATIC_NODE:
1073 token->key.value_off = add_string(rule_tmp->rules, value);
1075 case TK_M_EVENT_TIMEOUT:
1076 token->key.event_timeout = *(int *)data;
1079 case TK_M_PARENTS_MIN:
1080 case TK_M_PARENTS_MAX:
1084 log_error("wrong type %u\n", type);
1088 if (value != NULL && type < TK_M_MAX) {
1089 /* check if we need to split or call fnmatch() while matching rules */
1090 enum string_glob_type glob;
1094 has_split = (strchr(value, '|') != NULL);
1095 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
1096 if (has_split && has_glob) {
1097 glob = GL_SPLIT_GLOB;
1098 } else if (has_split) {
1100 } else if (has_glob) {
1101 if (streq(value, "?*"))
1102 glob = GL_SOMETHING;
1108 token->key.glob = glob;
1111 if (value != NULL && type > TK_M_MAX) {
1112 /* check if assigned value has substitution chars */
1113 if (value[0] == '[')
1114 token->key.subst = SB_SUBSYS;
1115 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
1116 token->key.subst = SB_FORMAT;
1118 token->key.subst = SB_NONE;
1122 /* check if property/attribut name has substitution chars */
1124 token->key.attrsubst = SB_SUBSYS;
1125 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
1126 token->key.attrsubst = SB_FORMAT;
1128 token->key.attrsubst = SB_NONE;
1131 token->key.type = type;
1133 rule_tmp->token_cur++;
1134 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
1135 log_error("temporary rule array too small\n");
1141 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1144 unsigned int start = 0;
1145 unsigned int end = rule_tmp->token_cur;
1147 for (i = 0; i < rule_tmp->token_cur; i++) {
1148 enum token_type next_val = TK_UNSET;
1149 unsigned int next_idx = 0;
1152 /* find smallest value */
1153 for (j = start; j < end; j++) {
1154 if (rule_tmp->token[j].type == TK_UNSET)
1156 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1157 next_val = rule_tmp->token[j].type;
1162 /* add token and mark done */
1163 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1165 rule_tmp->token[next_idx].type = TK_UNSET;
1168 if (next_idx == start)
1170 if (next_idx+1 == end)
1176 static int add_rule(struct udev_rules *rules, char *line,
1177 const char *filename, unsigned int filename_off, unsigned int lineno)
1181 struct rule_tmp rule_tmp;
1183 memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1184 rule_tmp.rules = rules;
1185 rule_tmp.rule.type = TK_RULE;
1186 /* the offset in the rule is limited to unsigned short */
1187 if (filename_off < USHRT_MAX)
1188 rule_tmp.rule.rule.filename_off = filename_off;
1189 rule_tmp.rule.rule.filename_line = lineno;
1195 enum operation_type op;
1197 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1200 if (streq(key, "ACTION")) {
1201 if (op > OP_MATCH_MAX) {
1202 log_error("invalid ACTION operation\n");
1205 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1209 if (streq(key, "DEVPATH")) {
1210 if (op > OP_MATCH_MAX) {
1211 log_error("invalid DEVPATH operation\n");
1214 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1218 if (streq(key, "KERNEL")) {
1219 if (op > OP_MATCH_MAX) {
1220 log_error("invalid KERNEL operation\n");
1223 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1227 if (streq(key, "SUBSYSTEM")) {
1228 if (op > OP_MATCH_MAX) {
1229 log_error("invalid SUBSYSTEM operation\n");
1232 /* bus, class, subsystem events should all be the same */
1233 if (streq(value, "subsystem") ||
1234 streq(value, "bus") ||
1235 streq(value, "class")) {
1236 if (streq(value, "bus") || streq(value, "class"))
1237 log_error("'%s' must be specified as 'subsystem' \n"
1238 "please fix it in %s:%u", value, filename, lineno);
1239 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1241 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1245 if (streq(key, "DRIVER")) {
1246 if (op > OP_MATCH_MAX) {
1247 log_error("invalid DRIVER operation\n");
1250 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1254 if (startswith(key, "ATTR{")) {
1255 attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1257 log_error("error parsing ATTR attribute\n");
1260 if (op < OP_MATCH_MAX) {
1261 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1263 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1268 if (streq(key, "KERNELS")) {
1269 if (op > OP_MATCH_MAX) {
1270 log_error("invalid KERNELS operation\n");
1273 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1277 if (streq(key, "SUBSYSTEMS")) {
1278 if (op > OP_MATCH_MAX) {
1279 log_error("invalid SUBSYSTEMS operation\n");
1282 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1286 if (streq(key, "DRIVERS")) {
1287 if (op > OP_MATCH_MAX) {
1288 log_error("invalid DRIVERS operation\n");
1291 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1295 if (startswith(key, "ATTRS{")) {
1296 if (op > OP_MATCH_MAX) {
1297 log_error("invalid ATTRS operation\n");
1300 attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1302 log_error("error parsing ATTRS attribute\n");
1305 if (startswith(attr, "device/"))
1306 log_error("the 'device' link may not be available in a future kernel, "
1307 "please fix it in %s:%u", filename, lineno);
1308 else if (strstr(attr, "../") != NULL)
1309 log_error("do not reference parent sysfs directories directly, "
1310 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1311 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1315 if (streq(key, "TAGS")) {
1316 if (op > OP_MATCH_MAX) {
1317 log_error("invalid TAGS operation\n");
1320 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1324 if (startswith(key, "ENV{")) {
1325 attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1327 log_error("error parsing ENV attribute\n");
1330 if (op < OP_MATCH_MAX) {
1331 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1334 static const char *blacklist[] = {
1349 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1350 if (!streq(attr, blacklist[i]))
1352 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
1355 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1361 if (streq(key, "TAG")) {
1362 if (op < OP_MATCH_MAX)
1363 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1365 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1369 if (streq(key, "PROGRAM")) {
1370 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1374 if (streq(key, "RESULT")) {
1375 if (op > OP_MATCH_MAX) {
1376 log_error("invalid RESULT operation\n");
1379 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1383 if (startswith(key, "IMPORT")) {
1384 attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1386 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1389 if (streq(attr, "program")) {
1390 /* find known built-in command */
1391 if (value[0] != '/') {
1392 enum udev_builtin_cmd cmd;
1394 cmd = udev_builtin_lookup(value);
1395 if (cmd < UDEV_BUILTIN_MAX) {
1396 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1397 value, filename, lineno);
1398 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1402 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1403 } else if (streq(attr, "builtin")) {
1404 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1406 if (cmd < UDEV_BUILTIN_MAX)
1407 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1409 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1410 } else if (streq(attr, "file")) {
1411 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1412 } else if (streq(attr, "db")) {
1413 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1414 } else if (streq(attr, "cmdline")) {
1415 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1416 } else if (streq(attr, "parent")) {
1417 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1419 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno);
1423 if (startswith(key, "TEST")) {
1426 if (op > OP_MATCH_MAX) {
1427 log_error("invalid TEST operation\n");
1430 attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1432 mode = strtol(attr, NULL, 8);
1433 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1435 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1440 if (startswith(key, "RUN")) {
1441 attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1445 if (streq(attr, "builtin")) {
1446 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1448 if (cmd < UDEV_BUILTIN_MAX)
1449 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1451 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
1452 } else if (streq(attr, "program")) {
1453 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1455 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1457 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno);
1463 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
1464 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1468 if (streq(key, "LABEL")) {
1469 rule_tmp.rule.rule.label_off = add_string(rules, value);
1473 if (streq(key, "GOTO")) {
1474 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1478 if (startswith(key, "NAME")) {
1479 if (op < OP_MATCH_MAX) {
1480 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1482 if (streq(value, "%k")) {
1483 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1484 "please remove it from %s:%u\n", filename, lineno);
1487 if (value[0] == '\0') {
1488 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1489 "please remove it from %s:%u\n", filename, lineno);
1492 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1494 rule_tmp.rule.rule.can_set_name = true;
1498 if (startswith(key, "SYMLINK")) {
1499 if (op < OP_MATCH_MAX) {
1500 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1504 attr = get_key_attribute(rules->udev, key + sizeof("SYMLINK")-1);
1505 if (attr != NULL && strstr(attr, "unique") != NULL)
1507 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, &flag);
1509 rule_tmp.rule.rule.can_set_name = true;
1513 if (streq(key, "OWNER")) {
1517 uid = strtoul(value, &endptr, 10);
1518 if (endptr[0] == '\0') {
1519 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1520 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1521 uid = add_uid(rules, value);
1522 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1523 } else if (rules->resolve_names >= 0) {
1524 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1526 rule_tmp.rule.rule.can_set_name = true;
1530 if (streq(key, "GROUP")) {
1534 gid = strtoul(value, &endptr, 10);
1535 if (endptr[0] == '\0') {
1536 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1537 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1538 gid = add_gid(rules, value);
1539 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1540 } else if (rules->resolve_names >= 0) {
1541 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1543 rule_tmp.rule.rule.can_set_name = true;
1547 if (streq(key, "MODE")) {
1551 mode = strtol(value, &endptr, 8);
1552 if (endptr[0] == '\0')
1553 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1555 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1556 rule_tmp.rule.rule.can_set_name = true;
1560 if (streq(key, "OPTIONS")) {
1563 pos = strstr(value, "link_priority=");
1565 int prio = atoi(&pos[strlen("link_priority=")]);
1567 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1570 pos = strstr(value, "event_timeout=");
1572 int tout = atoi(&pos[strlen("event_timeout=")]);
1574 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
1577 pos = strstr(value, "string_escape=");
1579 pos = &pos[strlen("string_escape=")];
1580 if (startswith(pos, "none"))
1581 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1582 else if (startswith(pos, "replace"))
1583 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1586 pos = strstr(value, "db_persist");
1588 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1590 pos = strstr(value, "nowatch");
1594 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1596 pos = strstr(value, "watch");
1600 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1604 pos = strstr(value, "static_node=");
1606 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1607 rule_tmp.rule.rule.has_static_node = true;
1613 log_error("unknown key '%s' in %s:%u\n", key, filename, lineno);
1617 /* add rule token */
1618 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1619 if (add_token(rules, &rule_tmp.rule) != 0)
1622 /* add tokens to list, sorted by type */
1623 if (sort_token(rules, &rule_tmp) != 0)
1628 log_error("invalid rule '%s:%u'\n", filename, lineno);
1632 static int parse_file(struct udev_rules *rules, const char *filename)
1635 unsigned int first_token;
1636 unsigned int filename_off;
1637 char line[UTIL_LINE_SIZE];
1641 if (null_or_empty_path(filename)) {
1642 log_debug("skip empty file: %s\n", filename);
1645 log_debug("read rules file: %s\n", filename);
1647 f = fopen(filename, "r");
1651 first_token = rules->token_cur;
1652 filename_off = add_string(rules, filename);
1654 while (fgets(line, sizeof(line), f) != NULL) {
1658 /* skip whitespace */
1661 while (isspace(key[0]))
1672 /* continue reading if backslash+newline is found */
1673 while (line[len-2] == '\\') {
1674 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1676 if (strlen(&line[len-2]) < 2)
1682 if (len+1 >= sizeof(line)) {
1683 log_error("line too long '%s':%u, ignored\n", filename, line_nr);
1686 add_rule(rules, key, filename, filename_off, line_nr);
1690 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1691 for (i = first_token+1; i < rules->token_cur; i++) {
1692 if (rules->tokens[i].type == TK_A_GOTO) {
1693 char *label = &rules->buf[rules->tokens[i].key.value_off];
1696 for (j = i+1; j < rules->token_cur; j++) {
1697 if (rules->tokens[j].type != TK_RULE)
1699 if (rules->tokens[j].rule.label_off == 0)
1701 if (!streq(label, &rules->buf[rules->tokens[j].rule.label_off]))
1703 rules->tokens[i].key.rule_goto = j;
1706 if (rules->tokens[i].key.rule_goto == 0)
1707 log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename);
1713 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1715 struct udev_rules *rules;
1716 struct udev_list file_list;
1717 struct token end_token;
1721 rules = calloc(1, sizeof(struct udev_rules));
1725 rules->resolve_names = resolve_names;
1726 udev_list_init(udev, &file_list, true);
1728 /* init token array and string buffer */
1729 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1730 if (rules->tokens == NULL) {
1734 rules->token_max = PREALLOC_TOKEN;
1736 rules->buf = malloc(PREALLOC_STRBUF);
1737 if (rules->buf == NULL) {
1738 free(rules->tokens);
1742 rules->buf_max = PREALLOC_STRBUF;
1743 /* offset 0 is always '\0' */
1744 rules->buf[0] = '\0';
1747 rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
1748 if (rules->trie_nodes == NULL) {
1750 free(rules->tokens);
1754 rules->trie_nodes_max = PREALLOC_TRIE;
1755 /* offset 0 is the trie root, with an empty string */
1756 memset(rules->trie_nodes, 0x00, sizeof(struct trie_node));
1757 rules->trie_nodes_cur = 1;
1759 rules->dirs = strv_new(TEST_PREFIX SYSCONFDIR "/udev/rules.d",
1760 TEST_PREFIX "/run/udev/rules.d",
1761 TEST_PREFIX UDEVLIBEXECDIR "/rules.d",
1764 log_error("failed to build config directory array");
1767 if (!strv_path_canonicalize(rules->dirs)) {
1768 log_error("failed to canonicalize config directories\n");
1771 strv_uniq(rules->dirs);
1772 r = conf_files_list_strv(&files, ".rules", (const char **)rules->dirs);
1774 log_error("failed to enumerate rules files: %s\n", strerror(-r));
1777 rules->dirs_ts_usec = calloc(strv_length(rules->dirs), sizeof(long long));
1780 * The offset value in the rules strct is limited; add all
1781 * rules file names to the beginning of the string buffer.
1783 STRV_FOREACH(f, files)
1784 add_string(rules, *f);
1786 STRV_FOREACH(f, files)
1787 parse_file(rules, *f);
1791 memset(&end_token, 0x00, sizeof(struct token));
1792 end_token.type = TK_END;
1793 add_token(rules, &end_token);
1795 /* shrink allocated token and string buffer */
1796 if (rules->token_cur < rules->token_max) {
1797 struct token *tokens;
1799 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1800 if (tokens != NULL || rules->token_cur == 0) {
1801 rules->tokens = tokens;
1802 rules->token_max = rules->token_cur;
1805 if (rules->buf_cur < rules->buf_max) {
1808 buf = realloc(rules->buf, rules->buf_cur);
1809 if (buf != NULL || rules->buf_cur == 0) {
1811 rules->buf_max = rules->buf_cur;
1814 log_debug("rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1815 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1816 log_debug("temporary index used %zu bytes (%u * %zu bytes)\n",
1817 rules->trie_nodes_cur * sizeof(struct trie_node),
1818 rules->trie_nodes_cur, sizeof(struct trie_node));
1821 free(rules->trie_nodes);
1822 rules->trie_nodes = NULL;
1823 rules->trie_nodes_cur = 0;
1824 rules->trie_nodes_max = 0;
1826 /* cleanup uid/gid cache */
1829 rules->uids_cur = 0;
1830 rules->uids_max = 0;
1833 rules->gids_cur = 0;
1834 rules->gids_max = 0;
1840 struct udev_rules *udev_rules_unref(struct udev_rules *rules)
1844 free(rules->tokens);
1846 free(rules->trie_nodes);
1849 strv_free(rules->dirs);
1850 free(rules->dirs_ts_usec);
1855 bool udev_rules_check_timestamp(struct udev_rules *rules)
1858 bool changed = false;
1860 for (i = 0; rules->dirs[i]; i++) {
1863 if (stat(rules->dirs[i], &stats) < 0)
1866 if (rules->dirs_ts_usec[i] == ts_usec(&stats.st_mtim))
1870 if (rules->dirs_ts_usec[i] != 0) {
1871 log_debug("reload - timestamp of '%s' changed\n", rules->dirs[i]);
1875 /* update timestamp */
1876 rules->dirs_ts_usec[i] = ts_usec(&stats.st_mtim);
1882 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1884 char *key_value = &rules->buf[token->key.value_off];
1891 switch (token->key.glob) {
1893 match = (streq(key_value, val));
1896 match = (fnmatch(key_value, val, 0) == 0);
1903 s = &rules->buf[token->key.value_off];
1908 next = strchr(s, '|');
1910 size_t matchlen = (size_t)(next - s);
1912 match = (matchlen == len && strncmp(s, val, matchlen) == 0);
1916 match = (streq(s, val));
1925 char value[UTIL_PATH_SIZE];
1927 util_strscpy(value, sizeof(value), &rules->buf[token->key.value_off]);
1929 while (key_value != NULL) {
1930 pos = strchr(key_value, '|');
1935 match = (fnmatch(key_value, val, 0) == 0);
1943 match = (val[0] != '\0');
1949 if (match && (token->key.op == OP_MATCH))
1951 if (!match && (token->key.op == OP_NOMATCH))
1956 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1959 char nbuf[UTIL_NAME_SIZE];
1961 char vbuf[UTIL_NAME_SIZE];
1964 name = &rules->buf[cur->key.attr_off];
1965 switch (cur->key.attrsubst) {
1967 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1971 value = udev_device_get_sysattr_value(dev, name);
1976 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1984 /* remove trailing whitespace, if not asked to match for it */
1985 len = strlen(value);
1986 if (len > 0 && isspace(value[len-1])) {
1987 const char *key_value;
1990 key_value = &rules->buf[cur->key.value_off];
1991 klen = strlen(key_value);
1992 if (klen > 0 && !isspace(key_value[klen-1])) {
1993 if (value != vbuf) {
1994 util_strscpy(vbuf, sizeof(vbuf), value);
1997 while (len > 0 && isspace(vbuf[--len]))
2002 return match_key(rules, cur, value);
2011 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
2015 enum escape_type esc = ESCAPE_UNSET;
2018 if (rules->tokens == NULL)
2021 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
2022 (major(udev_device_get_devnum(event->dev)) > 0 ||
2023 udev_device_get_ifindex(event->dev) > 0));
2025 /* loop through token list, match, run actions or forward to next rule */
2026 cur = &rules->tokens[0];
2029 dump_token(rules, cur);
2030 switch (cur->type) {
2034 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2035 if (!can_set_name && rule->rule.can_set_name)
2040 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
2044 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
2048 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
2051 case TK_M_DEVLINK: {
2052 struct udev_list_entry *list_entry;
2055 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
2056 const char *devlink;
2058 devlink = udev_list_entry_get_name(list_entry) + strlen(TEST_PREFIX "/dev/");
2059 if (match_key(rules, cur, devlink) == 0) {
2069 if (match_key(rules, cur, event->name) != 0)
2073 const char *key_name = &rules->buf[cur->key.attr_off];
2076 value = udev_device_get_property_value(event->dev, key_name);
2079 if (match_key(rules, cur, value))
2084 struct udev_list_entry *list_entry;
2087 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
2088 if (streq(&rules->buf[cur->key.value_off], udev_list_entry_get_name(list_entry))) {
2093 if (!match && (cur->key.op != OP_NOMATCH))
2097 case TK_M_SUBSYSTEM:
2098 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
2102 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2105 case TK_M_WAITFOR: {
2106 char filename[UTIL_PATH_SIZE];
2109 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2110 found = (wait_for_file(event->dev, filename, 10) == 0);
2111 if (!found && (cur->key.op != OP_NOMATCH))
2116 if (match_attr(rules, event->dev, event, cur) != 0)
2120 case TK_M_SUBSYSTEMS:
2126 /* get whole sequence of parent matches */
2128 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2131 /* loop over parents */
2132 event->dev_parent = event->dev;
2136 /* loop over sequence of parent match keys */
2137 for (key = cur; key < next; key++ ) {
2138 dump_token(rules, key);
2141 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2144 case TK_M_SUBSYSTEMS:
2145 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2149 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2153 if (match_attr(rules, event->dev_parent, event, key) != 0)
2157 bool match = udev_device_has_tag(event->dev_parent, &rules->buf[cur->key.value_off]);
2159 if (match && key->key.op == OP_NOMATCH)
2161 if (!match && key->key.op == OP_MATCH)
2172 event->dev_parent = udev_device_get_parent(event->dev_parent);
2173 if (event->dev_parent == NULL)
2176 /* move behind our sequence of parent match keys */
2181 char filename[UTIL_PATH_SIZE];
2182 struct stat statbuf;
2185 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2186 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2187 if (filename[0] != '/') {
2188 char tmp[UTIL_PATH_SIZE];
2190 util_strscpy(tmp, sizeof(tmp), filename);
2191 util_strscpyl(filename, sizeof(filename),
2192 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2195 attr_subst_subdir(filename, sizeof(filename));
2197 match = (stat(filename, &statbuf) == 0);
2198 if (match && cur->key.mode > 0)
2199 match = ((statbuf.st_mode & cur->key.mode) > 0);
2200 if (match && cur->key.op == OP_NOMATCH)
2202 if (!match && cur->key.op == OP_MATCH)
2206 case TK_M_EVENT_TIMEOUT:
2207 log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout);
2208 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2210 case TK_M_PROGRAM: {
2211 char program[UTIL_PATH_SIZE];
2213 char result[UTIL_PATH_SIZE];
2215 free(event->program_result);
2216 event->program_result = NULL;
2217 udev_event_apply_format(event, &rules->buf[cur->key.value_off], program, sizeof(program));
2218 envp = udev_device_get_properties_envp(event->dev);
2219 log_debug("PROGRAM '%s' %s:%u\n",
2221 &rules->buf[rule->rule.filename_off],
2222 rule->rule.filename_line);
2224 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2225 if (cur->key.op != OP_NOMATCH)
2230 util_remove_trailing_chars(result, '\n');
2231 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2232 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2234 log_debug("%i character(s) replaced\n" , count);
2236 event->program_result = strdup(result);
2237 if (cur->key.op == OP_NOMATCH)
2242 case TK_M_IMPORT_FILE: {
2243 char import[UTIL_PATH_SIZE];
2245 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2246 if (import_file_into_properties(event->dev, import) != 0)
2247 if (cur->key.op != OP_NOMATCH)
2251 case TK_M_IMPORT_PROG: {
2252 char import[UTIL_PATH_SIZE];
2254 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2255 log_debug("IMPORT '%s' %s:%u\n",
2257 &rules->buf[rule->rule.filename_off],
2258 rule->rule.filename_line);
2260 if (import_program_into_properties(event, import, sigmask) != 0)
2261 if (cur->key.op != OP_NOMATCH)
2265 case TK_M_IMPORT_BUILTIN: {
2266 char command[UTIL_PATH_SIZE];
2268 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2269 /* check if we ran already */
2270 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
2271 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2272 udev_builtin_name(cur->key.builtin_cmd),
2273 &rules->buf[rule->rule.filename_off],
2274 rule->rule.filename_line);
2275 /* return the result from earlier run */
2276 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2277 if (cur->key.op != OP_NOMATCH)
2282 event->builtin_run |= (1 << cur->key.builtin_cmd);
2285 udev_event_apply_format(event, &rules->buf[cur->key.value_off], command, sizeof(command));
2286 log_debug("IMPORT builtin '%s' %s:%u\n",
2287 udev_builtin_name(cur->key.builtin_cmd),
2288 &rules->buf[rule->rule.filename_off],
2289 rule->rule.filename_line);
2291 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2292 /* remember failure */
2293 log_debug("IMPORT builtin '%s' returned non-zero\n",
2294 udev_builtin_name(cur->key.builtin_cmd));
2295 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2296 if (cur->key.op != OP_NOMATCH)
2301 case TK_M_IMPORT_DB: {
2302 const char *key = &rules->buf[cur->key.value_off];
2305 value = udev_device_get_property_value(event->dev_db, key);
2306 if (value != NULL) {
2307 struct udev_list_entry *entry;
2309 entry = udev_device_add_property(event->dev, key, value);
2310 udev_list_entry_set_num(entry, true);
2312 if (cur->key.op != OP_NOMATCH)
2317 case TK_M_IMPORT_CMDLINE: {
2319 bool imported = false;
2321 f = fopen("/proc/cmdline", "r");
2325 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2326 const char *key = &rules->buf[cur->key.value_off];
2329 pos = strstr(cmdline, key);
2331 struct udev_list_entry *entry;
2334 if (pos[0] == '\0' || isspace(pos[0])) {
2335 /* we import simple flags as 'FLAG=1' */
2336 entry = udev_device_add_property(event->dev, key, "1");
2337 udev_list_entry_set_num(entry, true);
2339 } else if (pos[0] == '=') {
2344 while (pos[0] != '\0' && !isspace(pos[0]))
2347 entry = udev_device_add_property(event->dev, key, value);
2348 udev_list_entry_set_num(entry, true);
2355 if (!imported && cur->key.op != OP_NOMATCH)
2359 case TK_M_IMPORT_PARENT: {
2360 char import[UTIL_PATH_SIZE];
2362 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2363 if (import_parent_into_properties(event->dev, import) != 0)
2364 if (cur->key.op != OP_NOMATCH)
2369 if (match_key(rules, cur, event->program_result) != 0)
2372 case TK_A_STRING_ESCAPE_NONE:
2375 case TK_A_STRING_ESCAPE_REPLACE:
2376 esc = ESCAPE_REPLACE;
2378 case TK_A_DB_PERSIST:
2379 udev_device_set_db_persist(event->dev);
2381 case TK_A_INOTIFY_WATCH:
2382 if (event->inotify_watch_final)
2384 if (cur->key.op == OP_ASSIGN_FINAL)
2385 event->inotify_watch_final = true;
2386 event->inotify_watch = cur->key.watch;
2388 case TK_A_DEVLINK_PRIO:
2389 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2392 char owner[UTIL_NAME_SIZE];
2394 if (event->owner_final)
2396 if (cur->key.op == OP_ASSIGN_FINAL)
2397 event->owner_final = true;
2398 udev_event_apply_format(event, &rules->buf[cur->key.value_off], owner, sizeof(owner));
2399 event->uid = util_lookup_user(event->udev, owner);
2400 log_debug("OWNER %u %s:%u\n",
2402 &rules->buf[rule->rule.filename_off],
2403 rule->rule.filename_line);
2407 char group[UTIL_NAME_SIZE];
2409 if (event->group_final)
2411 if (cur->key.op == OP_ASSIGN_FINAL)
2412 event->group_final = true;
2413 udev_event_apply_format(event, &rules->buf[cur->key.value_off], group, sizeof(group));
2414 event->gid = util_lookup_group(event->udev, group);
2415 log_debug("GROUP %u %s:%u\n",
2417 &rules->buf[rule->rule.filename_off],
2418 rule->rule.filename_line);
2422 char mode_str[UTIL_NAME_SIZE];
2426 if (event->mode_final)
2428 udev_event_apply_format(event, &rules->buf[cur->key.value_off], mode_str, sizeof(mode_str));
2429 mode = strtol(mode_str, &endptr, 8);
2430 if (endptr[0] != '\0') {
2431 log_error("ignoring invalid mode '%s'\n", mode_str);
2434 if (cur->key.op == OP_ASSIGN_FINAL)
2435 event->mode_final = true;
2436 event->mode_set = true;
2438 log_debug("MODE %#o %s:%u\n",
2440 &rules->buf[rule->rule.filename_off],
2441 rule->rule.filename_line);
2445 if (event->owner_final)
2447 if (cur->key.op == OP_ASSIGN_FINAL)
2448 event->owner_final = true;
2449 event->uid = cur->key.uid;
2450 log_debug("OWNER %u %s:%u\n",
2452 &rules->buf[rule->rule.filename_off],
2453 rule->rule.filename_line);
2456 if (event->group_final)
2458 if (cur->key.op == OP_ASSIGN_FINAL)
2459 event->group_final = true;
2460 event->gid = cur->key.gid;
2461 log_debug("GROUP %u %s:%u\n",
2463 &rules->buf[rule->rule.filename_off],
2464 rule->rule.filename_line);
2467 if (event->mode_final)
2469 if (cur->key.op == OP_ASSIGN_FINAL)
2470 event->mode_final = true;
2471 event->mode_set = true;
2472 event->mode = cur->key.mode;
2473 log_debug("MODE %#o %s:%u\n",
2475 &rules->buf[rule->rule.filename_off],
2476 rule->rule.filename_line);
2479 const char *name = &rules->buf[cur->key.attr_off];
2480 char *value = &rules->buf[cur->key.value_off];
2482 if (value[0] != '\0') {
2483 char temp_value[UTIL_NAME_SIZE];
2484 struct udev_list_entry *entry;
2486 udev_event_apply_format(event, value, temp_value, sizeof(temp_value));
2487 entry = udev_device_add_property(event->dev, name, temp_value);
2488 /* store in db, skip private keys */
2490 udev_list_entry_set_num(entry, true);
2492 udev_device_add_property(event->dev, name, NULL);
2497 char tag[UTIL_PATH_SIZE];
2500 udev_event_apply_format(event, &rules->buf[cur->key.value_off], tag, sizeof(tag));
2501 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2502 udev_device_cleanup_tags_list(event->dev);
2503 for (p = tag; *p != '\0'; p++) {
2504 if ((*p >= 'a' && *p <= 'z') ||
2505 (*p >= 'A' && *p <= 'Z') ||
2506 (*p >= '0' && *p <= '9') ||
2507 *p == '-' || *p == '_')
2509 log_error("ignoring invalid tag name '%s'\n", tag);
2512 udev_device_add_tag(event->dev, tag);
2516 const char *name = &rules->buf[cur->key.value_off];
2518 char name_str[UTIL_PATH_SIZE];
2521 if (event->name_final)
2523 if (cur->key.op == OP_ASSIGN_FINAL)
2524 event->name_final = true;
2525 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2526 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2527 count = util_replace_chars(name_str, "/");
2529 log_debug("%i character(s) replaced\n", count);
2531 if (major(udev_device_get_devnum(event->dev)) &&
2532 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen(TEST_PREFIX "/dev/")))) {
2533 log_error("NAME=\"%s\" ignored, kernel device nodes "
2534 "can not be renamed; please fix it in %s:%u\n", name,
2535 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2539 event->name = strdup(name_str);
2540 log_debug("NAME '%s' %s:%u\n",
2542 &rules->buf[rule->rule.filename_off],
2543 rule->rule.filename_line);
2546 case TK_A_DEVLINK: {
2547 char temp[UTIL_PATH_SIZE];
2548 char filename[UTIL_PATH_SIZE];
2552 if (event->devlink_final)
2554 if (major(udev_device_get_devnum(event->dev)) == 0)
2556 if (cur->key.op == OP_ASSIGN_FINAL)
2557 event->devlink_final = true;
2558 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2559 udev_device_cleanup_devlinks_list(event->dev);
2561 /* allow multiple symlinks separated by spaces */
2562 udev_event_apply_format(event, &rules->buf[cur->key.value_off], temp, sizeof(temp));
2563 if (esc == ESCAPE_UNSET)
2564 count = util_replace_chars(temp, "/ ");
2565 else if (esc == ESCAPE_REPLACE)
2566 count = util_replace_chars(temp, "/");
2568 log_debug("%i character(s) replaced\n" , count);
2570 while (isspace(pos[0]))
2572 next = strchr(pos, ' ');
2573 while (next != NULL) {
2575 log_debug("LINK '%s' %s:%u\n", pos,
2576 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2577 util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/dev/", pos, NULL);
2578 udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique);
2579 while (isspace(next[1]))
2582 next = strchr(pos, ' ');
2584 if (pos[0] != '\0') {
2585 log_debug("LINK '%s' %s:%u\n", pos,
2586 &rules->buf[rule->rule.filename_off], rule->rule.filename_line);
2587 util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/dev/", pos, NULL);
2588 udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique);
2593 const char *key_name = &rules->buf[cur->key.attr_off];
2594 char attr[UTIL_PATH_SIZE];
2595 char value[UTIL_NAME_SIZE];
2598 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2599 util_strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2600 attr_subst_subdir(attr, sizeof(attr));
2602 udev_event_apply_format(event, &rules->buf[cur->key.value_off], value, sizeof(value));
2603 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
2604 &rules->buf[rule->rule.filename_off],
2605 rule->rule.filename_line);
2606 f = fopen(attr, "w");
2608 if (fprintf(f, "%s", value) <= 0)
2609 log_error("error writing ATTR{%s}: %m\n", attr);
2612 log_error("error opening ATTR{%s} for writing: %m\n", attr);
2616 case TK_A_RUN_BUILTIN:
2617 case TK_A_RUN_PROGRAM: {
2618 struct udev_list_entry *entry;
2620 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2621 udev_list_cleanup(&event->run_list);
2622 log_debug("RUN '%s' %s:%u\n",
2623 &rules->buf[cur->key.value_off],
2624 &rules->buf[rule->rule.filename_off],
2625 rule->rule.filename_line);
2626 entry = udev_list_entry_add(&event->run_list, &rules->buf[cur->key.value_off], NULL);
2627 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2631 if (cur->key.rule_goto == 0)
2633 cur = &rules->tokens[cur->key.rule_goto];
2638 case TK_M_PARENTS_MIN:
2639 case TK_M_PARENTS_MAX:
2642 log_error("wrong type %u\n", cur->type);
2649 /* fast-forward to next rule */
2650 cur = rule + rule->rule.token_count;
2654 void udev_rules_apply_static_dev_perms(struct udev_rules *rules)
2662 if (rules->tokens == NULL)
2665 cur = &rules->tokens[0];
2668 switch (cur->type) {
2673 /* skip rules without a static_node tag */
2674 if (!rule->rule.has_static_node)
2688 mode = cur->key.mode;
2690 case TK_A_STATIC_NODE: {
2691 char filename[UTIL_PATH_SIZE];
2694 /* we assure, that the permissions tokens are sorted before the static token */
2695 if (mode == 0 && uid == 0 && gid == 0)
2697 util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/dev/",
2698 &rules->buf[cur->key.value_off], NULL);
2699 if (stat(filename, &stats) != 0)
2701 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2709 if (mode != (stats.st_mode & 01777)) {
2710 chmod(filename, mode);
2711 log_debug("chmod '%s' %#o\n", filename, mode);
2714 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2715 chown(filename, uid, gid);
2716 log_debug("chown '%s' %u %u\n", filename, uid, gid);
2719 utimensat(AT_FDCWD, filename, NULL, 0);
2729 /* fast-forward to next rule */
2730 cur = rule + rule->rule.token_count;