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"
33 void udev_rules_iter_init(struct udev_rules *rules)
35 dbg("bufsize=%zi", rules->bufsize);
39 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
41 static struct udev_rule *rule;
46 dbg("current=%zi", rules->current);
47 if (rules->current >= rules->bufsize) {
53 rule = (struct udev_rule *) (rules->buf + rules->current);
54 rules->current += sizeof(struct udev_rule) + rule->bufsize;
59 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
61 static struct udev_rule *rule;
64 dbg("current=%zi", rules->current);
65 if (rules->current >= rules->bufsize) {
69 rule = (struct udev_rule *) (rules->buf + rules->current);
71 if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
72 dbg("moving forward, looking for label '%s'", label);
73 rules->current += sizeof(struct udev_rule) + rule->bufsize;
77 dbg("found label '%s'", label);
81 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
87 if (linepos == NULL && linepos[0] == '\0')
91 while (isspace(linepos[0]) || linepos[0] == ',')
95 if (linepos[0] == '\0')
101 if (linepos[0] == '\0')
103 if (isspace(linepos[0]))
105 if (linepos[0] == '=')
107 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
108 if (linepos[1] == '=')
112 /* remember end of key */
115 /* skip whitespace after key */
116 while (isspace(linepos[0]))
118 if (linepos[0] == '\0')
121 /* get operation type */
122 if (linepos[0] == '=' && linepos[1] == '=') {
123 *operation = KEY_OP_MATCH;
125 dbg("operator=match");
126 } else if (linepos[0] == '!' && linepos[1] == '=') {
127 *operation = KEY_OP_NOMATCH;
129 dbg("operator=nomatch");
130 } else if (linepos[0] == '+' && linepos[1] == '=') {
131 *operation = KEY_OP_ADD;
134 } else if (linepos[0] == '=') {
135 *operation = KEY_OP_ASSIGN;
137 dbg("operator=assign");
138 } else if (linepos[0] == ':' && linepos[1] == '=') {
139 *operation = KEY_OP_ASSIGN_FINAL;
141 dbg("operator=assign_final");
147 dbg("key='%s'", *key);
149 /* skip whitespace after operator */
150 while (isspace(linepos[0]))
152 if (linepos[0] == '\0')
156 if (linepos[0] == '"')
162 temp = strchr(linepos, '"');
167 dbg("value='%s'", *value);
169 /* move line to next key */
175 /* extract possible KEY{attr} */
176 static char *get_key_attribute(char *str)
181 attr = strchr(str, '{');
184 pos = strchr(attr, '}');
186 err("missing closing brace for format");
190 dbg("attribute='%s'", attr);
197 static int add_rule_key(struct udev_rule *rule, struct key *key,
198 enum key_operation operation, const char *value)
200 size_t val_len = strnlen(value, PATH_SIZE);
202 key->operation = operation;
204 key->val_off = rule->bufsize;
205 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
206 rule->bufsize += val_len+1;
211 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
212 enum key_operation operation, const char *key, const char *value)
214 size_t key_len = strnlen(key, PATH_SIZE);
216 if (pairs->count >= PAIRS_MAX) {
217 err("skip, too many keys of the same type in a single rule");
221 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
223 /* add the key-name of the pair */
224 pairs->keys[pairs->count].key_name_off = rule->bufsize;
225 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
226 rule->bufsize += key_len+1;
233 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
235 char buf[sizeof(struct udev_rule) + LINE_SIZE];
236 struct udev_rule *rule;
245 memset(buf, 0x00, sizeof(buf));
246 rule = (struct udev_rule *) buf;
250 /* get all the keys */
254 enum key_operation operation = KEY_OP_UNSET;
256 retval = get_key(&linepos, &key, &operation, &value);
260 if (strcasecmp(key, "ACTION") == 0) {
261 if (operation != KEY_OP_MATCH &&
262 operation != KEY_OP_NOMATCH) {
263 err("invalid ACTION operation");
266 add_rule_key(rule, &rule->action, operation, value);
271 if (strcasecmp(key, "DEVPATH") == 0) {
272 if (operation != KEY_OP_MATCH &&
273 operation != KEY_OP_NOMATCH) {
274 err("invalid DEVPATH operation");
277 add_rule_key(rule, &rule->devpath, operation, value);
282 if (strcasecmp(key, "KERNEL") == 0) {
283 if (operation != KEY_OP_MATCH &&
284 operation != KEY_OP_NOMATCH) {
285 err("invalid KERNEL operation");
288 add_rule_key(rule, &rule->kernel, operation, value);
293 if (strcasecmp(key, "SUBSYSTEM") == 0) {
294 if (operation != KEY_OP_MATCH &&
295 operation != KEY_OP_NOMATCH) {
296 err("invalid SUBSYSTEM operation");
299 /* bus, class, subsystem events should all be the same */
300 if (strcmp(value, "subsystem") == 0 ||
301 strcmp(value, "bus") == 0 ||
302 strcmp(value, "class") == 0) {
303 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
304 err("'%s' must be specified as 'subsystem' "
305 "please fix it in %s:%u", value, filename, lineno);
306 add_rule_key(rule, &rule->subsystem, operation, "subsystem|class|bus");
308 add_rule_key(rule, &rule->subsystem, operation, value);
313 if (strcasecmp(key, "DRIVER") == 0) {
314 if (operation != KEY_OP_MATCH &&
315 operation != KEY_OP_NOMATCH) {
316 err("invalid DRIVER operation");
319 add_rule_key(rule, &rule->driver, operation, value);
324 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
325 attr = get_key_attribute(key + sizeof("ATTR")-1);
327 err("error parsing ATTR attribute");
330 if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
336 if (strcasecmp(key, "KERNELS") == 0 ||
337 strcasecmp(key, "ID") == 0) {
338 if (operation != KEY_OP_MATCH &&
339 operation != KEY_OP_NOMATCH) {
340 err("invalid KERNELS operation");
343 add_rule_key(rule, &rule->kernels, operation, value);
348 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
349 strcasecmp(key, "BUS") == 0) {
350 if (operation != KEY_OP_MATCH &&
351 operation != KEY_OP_NOMATCH) {
352 err("invalid SUBSYSTEMS operation");
355 add_rule_key(rule, &rule->subsystems, operation, value);
360 if (strcasecmp(key, "DRIVERS") == 0) {
361 if (operation != KEY_OP_MATCH &&
362 operation != KEY_OP_NOMATCH) {
363 err("invalid DRIVERS operation");
366 add_rule_key(rule, &rule->drivers, operation, value);
371 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
372 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
373 if (operation != KEY_OP_MATCH &&
374 operation != KEY_OP_NOMATCH) {
375 err("invalid ATTRS operation");
378 attr = get_key_attribute(key + sizeof("ATTRS")-1);
380 err("error parsing ATTRS attribute");
383 if (strncmp(attr, "device/", 7) == 0)
384 err("the 'device' link is deprecated and will be removed from a future kernel, "
385 "please fix it in %s:%u", filename, lineno);
386 else if (strstr(attr, "../") != NULL)
387 err("do not reference parent sysfs directories directly, that may break with a future kernel, "
388 "please fix it in %s:%u", filename, lineno);
389 if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
395 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
396 attr = get_key_attribute(key + sizeof("ENV")-1);
398 err("error parsing ENV attribute");
401 if (strncmp(attr, "PHYSDEV", 7) == 0)
403 if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
409 if (strcasecmp(key, "PROGRAM") == 0) {
410 add_rule_key(rule, &rule->program, operation, value);
415 if (strcasecmp(key, "RESULT") == 0) {
416 if (operation != KEY_OP_MATCH &&
417 operation != KEY_OP_NOMATCH) {
418 err("invalid RESULT operation");
421 add_rule_key(rule, &rule->result, operation, value);
426 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
427 attr = get_key_attribute(key + sizeof("IMPORT")-1);
428 if (attr != NULL && strstr(attr, "program")) {
429 dbg("IMPORT will be executed");
430 rule->import_type = IMPORT_PROGRAM;
431 } else if (attr != NULL && strstr(attr, "file")) {
432 dbg("IMPORT will be included as file");
433 rule->import_type = IMPORT_FILE;
434 } else if (attr != NULL && strstr(attr, "parent")) {
435 dbg("IMPORT will include the parent values");
436 rule->import_type = IMPORT_PARENT;
438 /* figure it out if it is executable */
439 char file[PATH_SIZE];
443 strlcpy(file, value, sizeof(file));
444 pos = strchr(file, ' ');
448 /* allow programs in /lib/udev called without the path */
449 if (strchr(file, '/') == NULL) {
450 strlcpy(file, "/lib/udev/", sizeof(file));
451 strlcat(file, value, sizeof(file));
452 pos = strchr(file, ' ');
457 dbg("IMPORT auto mode for '%s'", file);
458 if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
459 dbg("IMPORT is executable, will be executed (autotype)");
460 rule->import_type = IMPORT_PROGRAM;
462 dbg("IMPORT is not executable, will be included as file (autotype)");
463 rule->import_type = IMPORT_FILE;
466 add_rule_key(rule, &rule->import, operation, value);
471 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
472 attr = get_key_attribute(key + sizeof("TEST")-1);
474 rule->test_mode_mask = strtol(attr, NULL, 8);
475 add_rule_key(rule, &rule->test, operation, value);
480 if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
481 attr = get_key_attribute(key + sizeof("RUN")-1);
483 if (strstr(attr, "ignore_error"))
484 rule->run_ignore_error = 1;
486 add_rule_key(rule, &rule->run, operation, value);
491 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
492 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
497 if (strcasecmp(key, "LABEL") == 0) {
498 add_rule_key(rule, &rule->label, operation, value);
503 if (strcasecmp(key, "GOTO") == 0) {
504 add_rule_key(rule, &rule->goto_label, operation, value);
509 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
510 attr = get_key_attribute(key + sizeof("NAME")-1);
512 if (strstr(attr, "all_partitions") != NULL) {
513 dbg("creation of partition nodes requested");
514 rule->partitions = DEFAULT_PARTITIONS_COUNT;
516 if (strstr(attr, "ignore_remove") != NULL) {
517 dbg("remove event should be ignored");
518 rule->ignore_remove = 1;
521 if (value[0] == '\0')
522 dbg("name empty, node creation supressed");
523 add_rule_key(rule, &rule->name, operation, value);
527 if (strcasecmp(key, "SYMLINK") == 0) {
528 add_rule_key(rule, &rule->symlink, operation, value);
533 if (strcasecmp(key, "OWNER") == 0) {
535 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
537 strtoul(value, &endptr, 10);
538 if (endptr[0] != '\0') {
540 uid_t uid = lookup_user(value);
541 dbg("replacing username='%s' by id=%i", value, uid);
542 sprintf(owner, "%u", (unsigned int) uid);
543 add_rule_key(rule, &rule->owner, operation, owner);
548 add_rule_key(rule, &rule->owner, operation, value);
552 if (strcasecmp(key, "GROUP") == 0) {
554 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
556 strtoul(value, &endptr, 10);
557 if (endptr[0] != '\0') {
559 gid_t gid = lookup_group(value);
560 dbg("replacing groupname='%s' by id=%i", value, gid);
561 sprintf(group, "%u", (unsigned int) gid);
562 add_rule_key(rule, &rule->group, operation, group);
567 add_rule_key(rule, &rule->group, operation, value);
571 if (strcasecmp(key, "MODE") == 0) {
572 rule->mode = strtol(value, NULL, 8);
573 rule->mode_operation = operation;
578 if (strcasecmp(key, "OPTIONS") == 0) {
581 if (strstr(value, "last_rule") != NULL) {
582 dbg("last rule to be applied");
585 if (strstr(value, "ignore_device") != NULL) {
586 dbg("device should be ignored");
587 rule->ignore_device = 1;
589 if (strstr(value, "ignore_remove") != NULL) {
590 dbg("remove event should be ignored");
591 rule->ignore_remove = 1;
593 pos = strstr(value, "link_priority=");
595 rule->link_priority = atoi(&pos[strlen("link_priority=")]);
596 dbg("link priority=%i", rule->link_priority);
598 pos = strstr(value, "string_escape=");
600 pos = &pos[strlen("string_escape=")];
601 if (strncmp(pos, "none", strlen("none")) == 0)
602 rule->string_escape = ESCAPE_NONE;
603 else if (strncmp(pos, "replace", strlen("replace")) == 0)
604 rule->string_escape = ESCAPE_REPLACE;
606 if (strstr(value, "all_partitions") != NULL) {
607 dbg("creation of partition nodes requested");
608 rule->partitions = DEFAULT_PARTITIONS_COUNT;
614 err("unknown key '%s' in %s:%u", key, filename, lineno);
617 if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
618 err("PHYSDEV* values are deprecated and will be removed from a future kernel, "
619 "please fix it in %s:%u", filename, lineno);
621 /* skip line if not any valid key was found */
625 /* grow buffer and add rule */
626 rule_size = sizeof(struct udev_rule) + rule->bufsize;
627 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
628 dbg("add %zi padding bytes", padding);
629 rule_size += padding;
630 rule->bufsize += padding;
632 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
634 err("realloc failed");
637 dbg("adding rule to offset %zi", rules->bufsize);
638 memcpy(rules->buf + rules->bufsize, rule, rule_size);
639 rules->bufsize += rule_size;
644 err("invalid rule '%s:%u'", filename, lineno);
648 static int parse_file(struct udev_rules *rules, const char *filename)
650 char line[LINE_SIZE];
659 if (file_map(filename, &buf, &bufsize) != 0) {
660 err("can't open '%s' as rules file: %s", filename, strerror(errno));
663 info("reading '%s' as rules file", filename);
665 /* loop through the whole file */
668 while (cur < bufsize) {
671 count = buf_get_line(buf, bufsize, cur);
676 /* eat the whitespace */
677 while ((count > 0) && isspace(bufline[0])) {
684 /* see if this is a comment */
685 if (bufline[0] == COMMENT_CHARACTER)
688 if (count >= sizeof(line)) {
689 err("line too long, rule skipped '%s:%u'", filename, lineno);
693 /* skip backslash and newline from multiline rules */
694 for (i = j = 0; i < count; i++) {
695 if (bufline[i] == '\\' && bufline[i+1] == '\n')
698 line[j++] = bufline[i];
702 dbg("read '%s'", line);
703 add_to_rules(rules, line, filename, lineno);
706 file_unmap(buf, bufsize);
710 int udev_rules_init(struct udev_rules *rules, int resolve_names)
713 char filename[PATH_MAX];
714 LIST_HEAD(name_list);
716 struct name_entry *name_loop, *name_tmp;
717 struct name_entry *dyn_loop, *dyn_tmp;
720 memset(rules, 0x00, sizeof(struct udev_rules));
721 rules->resolve_names = resolve_names;
723 /* read main config from single file or all files in a directory */
724 if (stat(udev_rules_dir, &statbuf) != 0)
726 if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
727 dbg("parse single rules file '%s'", udev_rules_dir);
728 name_list_add(&name_list, udev_rules_dir, 1);
730 dbg("parse rules directory '%s'", udev_rules_dir);
731 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
734 /* read dynamic rules directory */
735 strlcpy(filename, udev_root, sizeof(filename));
736 strlcat(filename, "/"RULES_DYN_DIR, sizeof(filename));
737 if (stat(filename, &statbuf) != 0) {
738 create_path(filename);
739 mkdir(filename, 0755);
741 add_matching_files(&dyn_list, filename, RULESFILE_SUFFIX);
743 /* sort dynamic rules files by basename into list of files */
744 list_for_each_entry_safe(dyn_loop, dyn_tmp, &dyn_list, node) {
745 const char *dyn_base = strrchr(dyn_loop->name, '/');
747 if (dyn_base == NULL)
750 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
751 const char *name_base = strrchr(name_loop->name, '/');
753 if (name_base == NULL)
756 if (strcmp(name_base, dyn_base) > 0) {
757 list_move_tail(&dyn_loop->node, &name_loop->node);
763 /* parse list of files */
764 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
765 if (stat(name_loop->name, &statbuf) == 0) {
767 parse_file(rules, name_loop->name);
769 dbg("empty rules file '%s'", name_loop->name);
771 err("could not read '%s': %s", name_loop->name, strerror(errno));
772 list_del(&name_loop->node);
779 void udev_rules_cleanup(struct udev_rules *rules)