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 struct udev_rule *rule;
244 /* get all the keys */
245 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
247 err("malloc failed");
256 enum key_operation operation = KEY_OP_UNSET;
258 retval = get_key(&linepos, &key, &operation, &value);
262 if (strcasecmp(key, "ACTION") == 0) {
263 if (operation != KEY_OP_MATCH &&
264 operation != KEY_OP_NOMATCH) {
265 err("invalid ACTION operation");
268 add_rule_key(rule, &rule->action, operation, value);
273 if (strcasecmp(key, "DEVPATH") == 0) {
274 if (operation != KEY_OP_MATCH &&
275 operation != KEY_OP_NOMATCH) {
276 err("invalid DEVPATH operation");
279 add_rule_key(rule, &rule->devpath, operation, value);
284 if (strcasecmp(key, "KERNEL") == 0) {
285 if (operation != KEY_OP_MATCH &&
286 operation != KEY_OP_NOMATCH) {
287 err("invalid KERNEL operation");
290 add_rule_key(rule, &rule->kernel, operation, value);
295 if (strcasecmp(key, "SUBSYSTEM") == 0) {
296 if (operation != KEY_OP_MATCH &&
297 operation != KEY_OP_NOMATCH) {
298 err("invalid SUBSYSTEM operation");
301 /* bus, class, subsystem events should all be the same */
302 if (strcmp(value, "subsystem") == 0 ||
303 strcmp(value, "bus") == 0 ||
304 strcmp(value, "class") == 0) {
305 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
306 err("'%s' must be specified as 'subsystem' "
307 "please fix it in %s:%u", value, filename, lineno);
308 add_rule_key(rule, &rule->subsystem, operation, "subsystem|class|bus");
310 add_rule_key(rule, &rule->subsystem, operation, value);
315 if (strcasecmp(key, "DRIVER") == 0) {
316 if (operation != KEY_OP_MATCH &&
317 operation != KEY_OP_NOMATCH) {
318 err("invalid DRIVER operation");
321 add_rule_key(rule, &rule->driver, operation, value);
326 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
327 attr = get_key_attribute(key + sizeof("ATTR")-1);
329 err("error parsing ATTR attribute");
332 if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
338 if (strcasecmp(key, "KERNELS") == 0 ||
339 strcasecmp(key, "ID") == 0) {
340 if (operation != KEY_OP_MATCH &&
341 operation != KEY_OP_NOMATCH) {
342 err("invalid KERNELS operation");
345 add_rule_key(rule, &rule->kernels, operation, value);
350 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
351 strcasecmp(key, "BUS") == 0) {
352 if (operation != KEY_OP_MATCH &&
353 operation != KEY_OP_NOMATCH) {
354 err("invalid SUBSYSTEMS operation");
357 add_rule_key(rule, &rule->subsystems, operation, value);
362 if (strcasecmp(key, "DRIVERS") == 0) {
363 if (operation != KEY_OP_MATCH &&
364 operation != KEY_OP_NOMATCH) {
365 err("invalid DRIVERS operation");
368 add_rule_key(rule, &rule->drivers, operation, value);
373 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
374 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
375 if (operation != KEY_OP_MATCH &&
376 operation != KEY_OP_NOMATCH) {
377 err("invalid ATTRS operation");
380 attr = get_key_attribute(key + sizeof("ATTRS")-1);
382 err("error parsing ATTRS attribute");
385 if (strncmp(attr, "device/", 7) == 0)
386 err("the 'device' link is deprecated and will be removed from a future kernel, "
387 "please fix it in %s:%u", filename, lineno);
388 else if (strstr(attr, "../") != NULL)
389 err("do not reference parent sysfs directories directly, that may break with a future kernel, "
390 "please fix it in %s:%u", filename, lineno);
391 if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
397 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
398 attr = get_key_attribute(key + sizeof("ENV")-1);
400 err("error parsing ENV attribute");
403 if (strncmp(attr, "PHYSDEV", 7) == 0)
405 if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
411 if (strcasecmp(key, "PROGRAM") == 0) {
412 add_rule_key(rule, &rule->program, operation, value);
417 if (strcasecmp(key, "RESULT") == 0) {
418 if (operation != KEY_OP_MATCH &&
419 operation != KEY_OP_NOMATCH) {
420 err("invalid RESULT operation");
423 add_rule_key(rule, &rule->result, operation, value);
428 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
429 attr = get_key_attribute(key + sizeof("IMPORT")-1);
430 if (attr && strstr(attr, "program")) {
431 dbg("IMPORT will be executed");
432 rule->import_type = IMPORT_PROGRAM;
433 } else if (attr && strstr(attr, "file")) {
434 dbg("IMPORT will be included as file");
435 rule->import_type = IMPORT_FILE;
436 } else if (attr && strstr(attr, "parent")) {
437 dbg("IMPORT will include the parent values");
438 rule->import_type = IMPORT_PARENT;
440 /* figure it out if it is executable */
441 char file[PATH_SIZE];
445 strlcpy(file, value, sizeof(file));
446 pos = strchr(file, ' ');
450 /* allow programs in /lib/udev called without the path */
451 if (strchr(file, '/') == NULL) {
452 strlcpy(file, "/lib/udev/", sizeof(file));
453 strlcat(file, value, sizeof(file));
454 pos = strchr(file, ' ');
459 dbg("IMPORT auto mode for '%s'", file);
460 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
461 dbg("IMPORT is executable, will be executed (autotype)");
462 rule->import_type = IMPORT_PROGRAM;
464 dbg("IMPORT is not executable, will be included as file (autotype)");
465 rule->import_type = IMPORT_FILE;
468 add_rule_key(rule, &rule->import, operation, value);
473 if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
474 attr = get_key_attribute(key + sizeof("TEST")-1);
476 rule->test_mode_mask = strtol(attr, NULL, 8);
477 add_rule_key(rule, &rule->test, operation, value);
482 if (strcasecmp(key, "RUN") == 0) {
483 add_rule_key(rule, &rule->run, operation, value);
488 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
489 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
494 if (strcasecmp(key, "LABEL") == 0) {
495 add_rule_key(rule, &rule->label, operation, value);
500 if (strcasecmp(key, "GOTO") == 0) {
501 add_rule_key(rule, &rule->goto_label, operation, value);
506 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
507 attr = get_key_attribute(key + sizeof("NAME")-1);
509 if (strstr(attr, "all_partitions") != NULL) {
510 dbg("creation of partition nodes requested");
511 rule->partitions = DEFAULT_PARTITIONS_COUNT;
513 if (strstr(attr, "ignore_remove") != NULL) {
514 dbg("remove event should be ignored");
515 rule->ignore_remove = 1;
518 if (value[0] == '\0')
519 dbg("name empty, node creation supressed");
520 add_rule_key(rule, &rule->name, operation, value);
524 if (strcasecmp(key, "SYMLINK") == 0) {
525 add_rule_key(rule, &rule->symlink, operation, value);
530 if (strcasecmp(key, "OWNER") == 0) {
532 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
534 strtoul(value, &endptr, 10);
535 if (endptr[0] != '\0') {
537 uid_t uid = lookup_user(value);
538 dbg("replacing username='%s' by id=%i", value, uid);
539 sprintf(owner, "%u", (unsigned int) uid);
540 add_rule_key(rule, &rule->owner, operation, owner);
545 add_rule_key(rule, &rule->owner, operation, value);
549 if (strcasecmp(key, "GROUP") == 0) {
551 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
553 strtoul(value, &endptr, 10);
554 if (endptr[0] != '\0') {
556 gid_t gid = lookup_group(value);
557 dbg("replacing groupname='%s' by id=%i", value, gid);
558 sprintf(group, "%u", (unsigned int) gid);
559 add_rule_key(rule, &rule->group, operation, group);
564 add_rule_key(rule, &rule->group, operation, value);
568 if (strcasecmp(key, "MODE") == 0) {
569 rule->mode = strtol(value, NULL, 8);
570 rule->mode_operation = operation;
575 if (strcasecmp(key, "OPTIONS") == 0) {
578 if (strstr(value, "last_rule") != NULL) {
579 dbg("last rule to be applied");
582 if (strstr(value, "ignore_device") != NULL) {
583 dbg("device should be ignored");
584 rule->ignore_device = 1;
586 if (strstr(value, "ignore_remove") != NULL) {
587 dbg("remove event should be ignored");
588 rule->ignore_remove = 1;
590 pos = strstr(value, "link_priority=");
592 rule->link_priority = atoi(&pos[strlen("link_priority=")]);
593 info("link priority=%i", rule->link_priority);
595 if (strstr(value, "all_partitions") != NULL) {
596 dbg("creation of partition nodes requested");
597 rule->partitions = DEFAULT_PARTITIONS_COUNT;
603 err("unknown key '%s' in %s:%u", key, filename, lineno);
606 if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
607 err("PHYSDEV* values are deprecated and will be removed from a future kernel, "
608 "please fix it in %s:%u", filename, lineno);
610 /* skip line if not any valid key was found */
614 /* grow buffer and add rule */
615 rule_size = sizeof(struct udev_rule) + rule->bufsize;
616 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
617 dbg("add %zi padding bytes", padding);
618 rule_size += padding;
619 rule->bufsize += padding;
621 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
623 err("realloc failed");
626 dbg("adding rule to offset %zi", rules->bufsize);
627 memcpy(rules->buf + rules->bufsize, rule, rule_size);
628 rules->bufsize += rule_size;
635 err("invalid rule '%s:%u'", filename, lineno);
639 static int parse_file(struct udev_rules *rules, const char *filename)
641 char line[LINE_SIZE];
650 if (file_map(filename, &buf, &bufsize) != 0) {
651 err("can't open '%s' as rules file: %s", filename, strerror(errno));
654 info("reading '%s' as rules file", filename);
656 /* loop through the whole file */
659 while (cur < bufsize) {
662 count = buf_get_line(buf, bufsize, cur);
667 /* eat the whitespace */
668 while ((count > 0) && isspace(bufline[0])) {
675 /* see if this is a comment */
676 if (bufline[0] == COMMENT_CHARACTER)
679 if (count >= sizeof(line)) {
680 err("line too long, rule skipped '%s:%u'", filename, lineno);
684 /* skip backslash and newline from multiline rules */
685 for (i = j = 0; i < count; i++) {
686 if (bufline[i] == '\\' && bufline[i+1] == '\n')
689 line[j++] = bufline[i];
693 dbg("read '%s'", line);
694 add_to_rules(rules, line, filename, lineno);
697 file_unmap(buf, bufsize);
701 int udev_rules_init(struct udev_rules *rules, int resolve_names)
706 memset(rules, 0x00, sizeof(struct udev_rules));
707 rules->resolve_names = resolve_names;
709 /* parse rules file or all matching files in directory */
710 if (stat(udev_rules_dir, &stats) != 0)
713 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
714 dbg("parse single rules file '%s'", udev_rules_dir);
715 retval = parse_file(rules, udev_rules_dir);
717 struct name_entry *name_loop, *name_tmp;
718 LIST_HEAD(name_list);
720 dbg("parse rules directory '%s'", udev_rules_dir);
721 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
723 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
724 if (stat(name_loop->name, &stats) == 0) {
726 parse_file(rules, name_loop->name);
728 dbg("empty rules file '%s'", name_loop->name);
730 err("could not read '%s': %s", name_loop->name, strerror(errno));
731 list_del(&name_loop->node);
739 void udev_rules_cleanup(struct udev_rules *rules)