2 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include "udev_rules.h"
31 #include "udev_selinux.h"
34 void udev_rules_iter_init(struct udev_rules *rules)
36 dbg("bufsize=%zi\n", rules->bufsize);
40 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
42 static struct udev_rule *rule;
47 dbg("current=%zi\n", rules->current);
48 if (rules->current >= rules->bufsize) {
49 dbg("no more rules\n");
54 rule = (struct udev_rule *) (rules->buf + rules->current);
55 rules->current += sizeof(struct udev_rule) + rule->bufsize;
60 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
62 static struct udev_rule *rule;
63 size_t start = rules->current;
66 dbg("current=%zi\n", rules->current);
67 if (rules->current >= rules->bufsize) {
68 err("LABEL='%s' not found, GOTO will be ignored\n", label);
69 rules->current = start;
72 rule = (struct udev_rule *) (rules->buf + rules->current);
74 if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
75 dbg("moving forward, looking for label '%s'\n", label);
76 rules->current += sizeof(struct udev_rule) + rule->bufsize;
80 dbg("found label '%s'\n", label);
84 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
90 if (linepos == NULL && linepos[0] == '\0')
94 while (isspace(linepos[0]) || linepos[0] == ',')
98 if (linepos[0] == '\0')
104 if (linepos[0] == '\0')
106 if (isspace(linepos[0]))
108 if (linepos[0] == '=')
110 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
111 if (linepos[1] == '=')
115 /* remember end of key */
118 /* skip whitespace after key */
119 while (isspace(linepos[0]))
121 if (linepos[0] == '\0')
124 /* get operation type */
125 if (linepos[0] == '=' && linepos[1] == '=') {
126 *operation = KEY_OP_MATCH;
128 dbg("operator=match\n");
129 } else if (linepos[0] == '!' && linepos[1] == '=') {
130 *operation = KEY_OP_NOMATCH;
132 dbg("operator=nomatch\n");
133 } else if (linepos[0] == '+' && linepos[1] == '=') {
134 *operation = KEY_OP_ADD;
136 dbg("operator=add\n");
137 } else if (linepos[0] == '=') {
138 *operation = KEY_OP_ASSIGN;
140 dbg("operator=assign\n");
141 } else if (linepos[0] == ':' && linepos[1] == '=') {
142 *operation = KEY_OP_ASSIGN_FINAL;
144 dbg("operator=assign_final\n");
150 dbg("key='%s'\n", *key);
152 /* skip whitespace after operator */
153 while (isspace(linepos[0]))
155 if (linepos[0] == '\0')
159 if (linepos[0] == '"')
165 temp = strchr(linepos, '"');
170 dbg("value='%s'\n", *value);
172 /* move line to next key */
178 /* extract possible KEY{attr} */
179 static char *get_key_attribute(char *str)
184 attr = strchr(str, '{');
187 pos = strchr(attr, '}');
189 err("missing closing brace for format\n");
193 dbg("attribute='%s'\n", attr);
200 static int add_rule_key(struct udev_rule *rule, struct key *key,
201 enum key_operation operation, const char *value)
203 size_t val_len = strnlen(value, PATH_SIZE);
205 key->operation = operation;
207 key->val_off = rule->bufsize;
208 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
209 rule->bufsize += val_len+1;
214 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
215 enum key_operation operation, const char *key, const char *value)
217 size_t key_len = strnlen(key, PATH_SIZE);
219 if (pairs->count >= PAIRS_MAX) {
220 err("skip, too many keys of the same type in a single rule\n");
224 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
226 /* add the key-name of the pair */
227 pairs->keys[pairs->count].key_name_off = rule->bufsize;
228 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
229 rule->bufsize += key_len+1;
236 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
238 char buf[sizeof(struct udev_rule) + LINE_SIZE];
239 struct udev_rule *rule;
248 memset(buf, 0x00, sizeof(buf));
249 rule = (struct udev_rule *) buf;
250 rule->event_timeout = -1;
254 /* get all the keys */
258 enum key_operation operation = KEY_OP_UNSET;
260 retval = get_key(&linepos, &key, &operation, &value);
264 if (strcasecmp(key, "ACTION") == 0) {
265 if (operation != KEY_OP_MATCH &&
266 operation != KEY_OP_NOMATCH) {
267 err("invalid ACTION operation\n");
270 add_rule_key(rule, &rule->action, operation, value);
275 if (strcasecmp(key, "DEVPATH") == 0) {
276 if (operation != KEY_OP_MATCH &&
277 operation != KEY_OP_NOMATCH) {
278 err("invalid DEVPATH operation\n");
281 add_rule_key(rule, &rule->devpath, operation, value);
286 if (strcasecmp(key, "KERNEL") == 0) {
287 if (operation != KEY_OP_MATCH &&
288 operation != KEY_OP_NOMATCH) {
289 err("invalid KERNEL operation\n");
292 add_rule_key(rule, &rule->kernel, operation, value);
297 if (strcasecmp(key, "SUBSYSTEM") == 0) {
298 if (operation != KEY_OP_MATCH &&
299 operation != KEY_OP_NOMATCH) {
300 err("invalid SUBSYSTEM operation\n");
303 /* bus, class, subsystem events should all be the same */
304 if (strcmp(value, "subsystem") == 0 ||
305 strcmp(value, "bus") == 0 ||
306 strcmp(value, "class") == 0) {
307 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
308 err("'%s' must be specified as 'subsystem' \n"
309 "please fix it in %s:%u", value, filename, lineno);
310 add_rule_key(rule, &rule->subsystem, operation, "subsystem|class|bus");
312 add_rule_key(rule, &rule->subsystem, operation, value);
317 if (strcasecmp(key, "DRIVER") == 0) {
318 if (operation != KEY_OP_MATCH &&
319 operation != KEY_OP_NOMATCH) {
320 err("invalid DRIVER operation\n");
323 add_rule_key(rule, &rule->driver, operation, value);
328 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
329 attr = get_key_attribute(key + sizeof("ATTR")-1);
331 err("error parsing ATTR attribute\n");
334 if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
340 if (strcasecmp(key, "KERNELS") == 0 ||
341 strcasecmp(key, "ID") == 0) {
342 if (operation != KEY_OP_MATCH &&
343 operation != KEY_OP_NOMATCH) {
344 err("invalid KERNELS operation\n");
347 add_rule_key(rule, &rule->kernels, operation, value);
352 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
353 strcasecmp(key, "BUS") == 0) {
354 if (operation != KEY_OP_MATCH &&
355 operation != KEY_OP_NOMATCH) {
356 err("invalid SUBSYSTEMS operation\n");
359 add_rule_key(rule, &rule->subsystems, operation, value);
364 if (strcasecmp(key, "DRIVERS") == 0) {
365 if (operation != KEY_OP_MATCH &&
366 operation != KEY_OP_NOMATCH) {
367 err("invalid DRIVERS operation\n");
370 add_rule_key(rule, &rule->drivers, operation, value);
375 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
376 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
377 if (operation != KEY_OP_MATCH &&
378 operation != KEY_OP_NOMATCH) {
379 err("invalid ATTRS operation\n");
382 attr = get_key_attribute(key + sizeof("ATTRS")-1);
384 err("error parsing ATTRS attribute\n");
387 if (strncmp(attr, "device/", 7) == 0)
388 err("the 'device' link is deprecated and will be removed from a future kernel, \n"
389 "please fix it in %s:%u", filename, lineno);
390 else if (strstr(attr, "../") != NULL)
391 err("do not reference parent sysfs directories directly, that may break with a future kernel, \n"
392 "please fix it in %s:%u", filename, lineno);
393 if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
399 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
400 attr = get_key_attribute(key + sizeof("ENV")-1);
402 err("error parsing ENV attribute\n");
405 if (strncmp(attr, "PHYSDEV", 7) == 0)
407 if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
413 if (strcasecmp(key, "PROGRAM") == 0) {
414 add_rule_key(rule, &rule->program, operation, value);
419 if (strcasecmp(key, "RESULT") == 0) {
420 if (operation != KEY_OP_MATCH &&
421 operation != KEY_OP_NOMATCH) {
422 err("invalid RESULT operation\n");
425 add_rule_key(rule, &rule->result, operation, value);
430 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
431 attr = get_key_attribute(key + sizeof("IMPORT")-1);
432 if (attr != NULL && strstr(attr, "program")) {
433 dbg("IMPORT will be executed\n");
434 rule->import_type = IMPORT_PROGRAM;
435 } else if (attr != NULL && strstr(attr, "file")) {
436 dbg("IMPORT will be included as file\n");
437 rule->import_type = IMPORT_FILE;
438 } else if (attr != NULL && strstr(attr, "parent")) {
439 dbg("IMPORT will include the parent values\n");
440 rule->import_type = IMPORT_PARENT;
442 /* figure it out if it is executable */
443 char file[PATH_SIZE];
447 strlcpy(file, value, sizeof(file));
448 pos = strchr(file, ' ');
452 /* allow programs in /lib/udev called without the path */
453 if (strchr(file, '/') == NULL) {
454 strlcpy(file, UDEV_PREFIX "/lib/udev/", sizeof(file));
455 strlcat(file, value, sizeof(file));
456 pos = strchr(file, ' ');
461 dbg("IMPORT auto mode for '%s'\n", file);
462 if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
463 dbg("IMPORT is executable, will be executed (autotype)\n");
464 rule->import_type = IMPORT_PROGRAM;
466 dbg("IMPORT is not executable, will be included as file (autotype)\n");
467 rule->import_type = IMPORT_FILE;
470 add_rule_key(rule, &rule->import, operation, value);
475 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
476 if (operation != KEY_OP_MATCH &&
477 operation != KEY_OP_NOMATCH) {
478 err("invalid TEST operation\n");
481 attr = get_key_attribute(key + sizeof("TEST")-1);
483 rule->test_mode_mask = strtol(attr, NULL, 8);
484 add_rule_key(rule, &rule->test, operation, value);
489 if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
490 attr = get_key_attribute(key + sizeof("RUN")-1);
492 if (strstr(attr, "ignore_error"))
493 rule->run_ignore_error = 1;
495 add_rule_key(rule, &rule->run, operation, value);
500 if (strcasecmp(key, "WAIT_FOR") == 0 || strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
501 add_rule_key(rule, &rule->wait_for, operation, value);
506 if (strcasecmp(key, "LABEL") == 0) {
507 add_rule_key(rule, &rule->label, operation, value);
512 if (strcasecmp(key, "GOTO") == 0) {
513 add_rule_key(rule, &rule->goto_label, operation, value);
518 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
519 attr = get_key_attribute(key + sizeof("NAME")-1);
521 if (strstr(attr, "all_partitions") != NULL) {
522 dbg("creation of partition nodes requested\n");
523 rule->partitions = DEFAULT_PARTITIONS_COUNT;
525 if (strstr(attr, "ignore_remove") != NULL) {
526 dbg("remove event should be ignored\n");
527 rule->ignore_remove = 1;
530 if (value[0] == '\0')
531 dbg("name empty, node creation supressed\n");
532 add_rule_key(rule, &rule->name, operation, value);
536 if (strcasecmp(key, "SYMLINK") == 0) {
537 if (operation == KEY_OP_MATCH ||
538 operation == KEY_OP_NOMATCH)
539 add_rule_key(rule, &rule->symlink_match, operation, value);
541 add_rule_key(rule, &rule->symlink, operation, value);
546 if (strcasecmp(key, "OWNER") == 0) {
548 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
550 strtoul(value, &endptr, 10);
551 if (endptr[0] != '\0') {
553 uid_t uid = lookup_user(value);
554 dbg("replacing username='%s' by id=%i\n", value, uid);
555 sprintf(owner, "%u", (unsigned int) uid);
556 add_rule_key(rule, &rule->owner, operation, owner);
561 add_rule_key(rule, &rule->owner, operation, value);
565 if (strcasecmp(key, "GROUP") == 0) {
567 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
569 strtoul(value, &endptr, 10);
570 if (endptr[0] != '\0') {
572 gid_t gid = lookup_group(value);
573 dbg("replacing groupname='%s' by id=%i\n", value, gid);
574 sprintf(group, "%u", (unsigned int) gid);
575 add_rule_key(rule, &rule->group, operation, group);
580 add_rule_key(rule, &rule->group, operation, value);
584 if (strcasecmp(key, "MODE") == 0) {
585 add_rule_key(rule, &rule->mode, operation, value);
590 if (strcasecmp(key, "OPTIONS") == 0) {
593 if (strstr(value, "last_rule") != NULL) {
594 dbg("last rule to be applied\n");
597 if (strstr(value, "ignore_device") != NULL) {
598 dbg("device should be ignored\n");
599 rule->ignore_device = 1;
601 if (strstr(value, "ignore_remove") != NULL) {
602 dbg("remove event should be ignored\n");
603 rule->ignore_remove = 1;
605 pos = strstr(value, "link_priority=");
607 rule->link_priority = atoi(&pos[strlen("link_priority=")]);
608 dbg("link priority=%i\n", rule->link_priority);
610 pos = strstr(value, "event_timeout=");
612 rule->event_timeout = atoi(&pos[strlen("event_timeout=")]);
613 dbg("event timout=%i\n", rule->event_timeout);
615 pos = strstr(value, "string_escape=");
617 pos = &pos[strlen("string_escape=")];
618 if (strncmp(pos, "none", strlen("none")) == 0)
619 rule->string_escape = ESCAPE_NONE;
620 else if (strncmp(pos, "replace", strlen("replace")) == 0)
621 rule->string_escape = ESCAPE_REPLACE;
623 if (strstr(value, "all_partitions") != NULL) {
624 dbg("creation of partition nodes requested\n");
625 rule->partitions = DEFAULT_PARTITIONS_COUNT;
631 err("unknown key '%s' in %s:%u\n", key, filename, lineno);
634 if (physdev && rule->wait_for.operation == KEY_OP_UNSET)
635 err("PHYSDEV* values are deprecated and will be removed from a future kernel, \n"
636 "please fix it in %s:%u", filename, lineno);
638 /* skip line if not any valid key was found */
642 /* grow buffer and add rule */
643 rule_size = sizeof(struct udev_rule) + rule->bufsize;
644 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
645 dbg("add %zi padding bytes\n", padding);
646 rule_size += padding;
647 rule->bufsize += padding;
649 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
651 err("realloc failed\n");
654 dbg("adding rule to offset %zi\n", rules->bufsize);
655 memcpy(rules->buf + rules->bufsize, rule, rule_size);
656 rules->bufsize += rule_size;
661 err("invalid rule '%s:%u'\n", filename, lineno);
665 static int parse_file(struct udev_rules *rules, const char *filename)
667 char line[LINE_SIZE];
676 if (file_map(filename, &buf, &bufsize) != 0) {
677 err("can't open '%s' as rules file: %s\n", filename, strerror(errno));
680 info("reading '%s' as rules file\n", filename);
682 /* loop through the whole file */
685 while (cur < bufsize) {
688 count = buf_get_line(buf, bufsize, cur);
693 /* eat the whitespace */
694 while ((count > 0) && isspace(bufline[0])) {
701 /* see if this is a comment */
702 if (bufline[0] == COMMENT_CHARACTER)
705 if (count >= sizeof(line)) {
706 err("line too long, rule skipped '%s:%u'\n", filename, lineno);
710 /* skip backslash and newline from multiline rules */
711 for (i = j = 0; i < count; i++) {
712 if (bufline[i] == '\\' && bufline[i+1] == '\n')
715 line[j++] = bufline[i];
719 dbg("read '%s'\n", line);
720 add_to_rules(rules, line, filename, lineno);
723 file_unmap(buf, bufsize);
727 int udev_rules_init(struct udev_rules *rules, int resolve_names)
730 char filename[PATH_MAX];
731 LIST_HEAD(name_list);
732 LIST_HEAD(sort_list);
733 struct name_entry *name_loop, *name_tmp;
734 struct name_entry *sort_loop, *sort_tmp;
737 memset(rules, 0x00, sizeof(struct udev_rules));
738 rules->resolve_names = resolve_names;
740 if (udev_rules_dir[0] != '\0') {
741 /* custom rules location for testing */
742 add_matching_files(&name_list, udev_rules_dir, ".rules");
744 /* read default rules */
745 add_matching_files(&name_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules");
747 /* read user/custom rules */
748 add_matching_files(&sort_list, SYSCONFDIR "/udev/rules.d", ".rules");
750 /* read dynamic/temporary rules */
751 strlcpy(filename, udev_root, sizeof(filename));
752 strlcat(filename, "/.udev/rules.d", sizeof(filename));
753 if (stat(filename, &statbuf) != 0) {
754 create_path(filename);
755 selinux_setfscreatecon(filename, NULL, S_IFDIR|0755);
756 mkdir(filename, 0755);
757 selinux_resetfscreatecon();
759 add_matching_files(&sort_list, filename, ".rules");
761 /* sort all rules files by basename into list of files */
762 list_for_each_entry_safe(sort_loop, sort_tmp, &sort_list, node) {
763 const char *sort_base = strrchr(sort_loop->name, '/');
765 if (sort_base == NULL)
768 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
769 const char *name_base = strrchr(name_loop->name, '/');
771 if (name_base == NULL)
774 if (strcmp(name_base, sort_base) > 0)
777 list_move_tail(&sort_loop->node, &name_loop->node);
781 /* parse list of files */
782 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
783 if (stat(name_loop->name, &statbuf) == 0) {
785 parse_file(rules, name_loop->name);
787 dbg("empty rules file '%s'\n", name_loop->name);
789 err("could not read '%s': %s\n", name_loop->name, strerror(errno));
790 list_del(&name_loop->node);
797 void udev_rules_cleanup(struct udev_rules *rules)