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] == '+')
109 if (linepos[0] == '!')
111 if (linepos[0] == ':')
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");
129 } else if (linepos[0] == '!' && linepos[1] == '=') {
130 *operation = KEY_OP_NOMATCH;
132 dbg("operator=nomatch");
133 } else if (linepos[0] == '+' && linepos[1] == '=') {
134 *operation = KEY_OP_ADD;
137 } else if (linepos[0] == '=') {
138 *operation = KEY_OP_ASSIGN;
140 dbg("operator=assign");
141 } else if (linepos[0] == ':' && linepos[1] == '=') {
142 *operation = KEY_OP_ASSIGN_FINAL;
144 dbg("operator=assign_final");
150 dbg("key='%s'", *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'", *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");
193 dbg("attribute='%s'", 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");
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 struct udev_rule *rule;
247 /* get all the keys */
248 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
250 err("malloc failed");
259 enum key_operation operation = KEY_OP_UNSET;
261 retval = get_key(&linepos, &key, &operation, &value);
265 if (strcasecmp(key, "ACTION") == 0) {
266 if (operation != KEY_OP_MATCH &&
267 operation != KEY_OP_NOMATCH) {
268 err("invalid ACTION operation");
271 add_rule_key(rule, &rule->action, operation, value);
276 if (strcasecmp(key, "DEVPATH") == 0) {
277 if (operation != KEY_OP_MATCH &&
278 operation != KEY_OP_NOMATCH) {
279 err("invalid DEVPATH operation");
282 add_rule_key(rule, &rule->devpath, operation, value);
287 if (strcasecmp(key, "KERNEL") == 0) {
288 if (operation != KEY_OP_MATCH &&
289 operation != KEY_OP_NOMATCH) {
290 err("invalid KERNEL operation");
293 add_rule_key(rule, &rule->kernel, operation, value);
298 if (strcasecmp(key, "SUBSYSTEM") == 0) {
299 if (operation != KEY_OP_MATCH &&
300 operation != KEY_OP_NOMATCH) {
301 err("invalid SUBSYSTEM operation");
304 add_rule_key(rule, &rule->subsystem, operation, value);
309 if (strcasecmp(key, "DRIVER") == 0) {
310 if (operation != KEY_OP_MATCH &&
311 operation != KEY_OP_NOMATCH) {
312 err("invalid DRIVER operation");
315 add_rule_key(rule, &rule->driver, operation, value);
320 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
321 attr = get_key_attribute(key + sizeof("ATTR")-1);
323 err("error parsing ATTR attribute");
326 if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
332 if (strcasecmp(key, "KERNELS") == 0 ||
333 strcasecmp(key, "ID") == 0) {
334 if (operation != KEY_OP_MATCH &&
335 operation != KEY_OP_NOMATCH) {
336 err("invalid KERNELS operation");
339 add_rule_key(rule, &rule->kernels, operation, value);
344 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
345 strcasecmp(key, "BUS") == 0) {
346 if (operation != KEY_OP_MATCH &&
347 operation != KEY_OP_NOMATCH) {
348 err("invalid SUBSYSTEMS operation");
351 add_rule_key(rule, &rule->subsystems, operation, value);
356 if (strcasecmp(key, "DRIVERS") == 0) {
357 if (operation != KEY_OP_MATCH &&
358 operation != KEY_OP_NOMATCH) {
359 err("invalid DRIVERS operation");
362 add_rule_key(rule, &rule->drivers, operation, value);
367 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
368 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
369 if (operation != KEY_OP_MATCH &&
370 operation != KEY_OP_NOMATCH) {
371 err("invalid ATTRS operation");
374 attr = get_key_attribute(key + sizeof("ATTRS")-1);
376 err("error parsing ATTRS attribute");
379 if (strncmp(attr, "device/", 7) == 0)
380 err("the 'device' link is deprecated and will be removed from a future kernel, "
381 "please fix it in %s:%u", filename, lineno);
382 else if (strchr(attr, '/') != NULL)
383 err("do not reference parent sysfs directories directly, that may break with a future kernel, "
384 "please fix it in %s:%u", filename, lineno);
385 if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
391 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
392 attr = get_key_attribute(key + sizeof("ENV")-1);
394 err("error parsing ENV attribute");
397 if (strncmp(attr, "PHYSDEV", 7) == 0)
399 if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
405 if (strcasecmp(key, "PROGRAM") == 0) {
406 add_rule_key(rule, &rule->program, operation, value);
411 if (strcasecmp(key, "RESULT") == 0) {
412 if (operation != KEY_OP_MATCH &&
413 operation != KEY_OP_NOMATCH) {
414 err("invalid RESULT operation");
417 add_rule_key(rule, &rule->result, operation, value);
422 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
423 attr = get_key_attribute(key + sizeof("IMPORT")-1);
424 if (attr && strstr(attr, "program")) {
425 dbg("IMPORT will be executed");
426 rule->import_type = IMPORT_PROGRAM;
427 } else if (attr && strstr(attr, "file")) {
428 dbg("IMPORT will be included as file");
429 rule->import_type = IMPORT_FILE;
430 } else if (attr && strstr(attr, "parent")) {
431 dbg("IMPORT will include the parent values");
432 rule->import_type = IMPORT_PARENT;
434 /* figure it out if it is executable */
435 char file[PATH_SIZE];
439 strlcpy(file, value, sizeof(file));
440 pos = strchr(file, ' ');
444 /* allow programs in /lib/udev called without the path */
445 if (strchr(file, '/') == NULL) {
446 strlcpy(file, "/lib/udev/", sizeof(file));
447 strlcat(file, value, sizeof(file));
448 pos = strchr(file, ' ');
453 dbg("IMPORT auto mode for '%s'", file);
454 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
455 dbg("IMPORT is executable, will be executed (autotype)");
456 rule->import_type = IMPORT_PROGRAM;
458 dbg("IMPORT is not executable, will be included as file (autotype)");
459 rule->import_type = IMPORT_FILE;
462 add_rule_key(rule, &rule->import, operation, value);
467 if (strcasecmp(key, "RUN") == 0) {
468 add_rule_key(rule, &rule->run, operation, value);
473 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
474 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
479 if (strcasecmp(key, "LABEL") == 0) {
480 add_rule_key(rule, &rule->label, operation, value);
485 if (strcasecmp(key, "GOTO") == 0) {
486 add_rule_key(rule, &rule->goto_label, operation, value);
491 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
492 attr = get_key_attribute(key + sizeof("NAME")-1);
494 if (strstr(attr, "all_partitions") != NULL) {
495 dbg("creation of partition nodes requested");
496 rule->partitions = DEFAULT_PARTITIONS_COUNT;
498 if (strstr(attr, "ignore_remove") != NULL) {
499 dbg("remove event should be ignored");
500 rule->ignore_remove = 1;
503 if (value[0] == '\0')
504 dbg("name empty, node creation supressed");
505 add_rule_key(rule, &rule->name, operation, value);
509 if (strcasecmp(key, "SYMLINK") == 0) {
510 add_rule_key(rule, &rule->symlink, operation, value);
515 if (strcasecmp(key, "OWNER") == 0) {
517 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
519 strtoul(value, &endptr, 10);
520 if (endptr[0] != '\0') {
522 uid_t uid = lookup_user(value);
523 dbg("replacing username='%s' by id=%i", value, uid);
524 sprintf(owner, "%u", (unsigned int) uid);
525 add_rule_key(rule, &rule->owner, operation, owner);
530 add_rule_key(rule, &rule->owner, operation, value);
534 if (strcasecmp(key, "GROUP") == 0) {
536 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
538 strtoul(value, &endptr, 10);
539 if (endptr[0] != '\0') {
541 gid_t gid = lookup_group(value);
542 dbg("replacing groupname='%s' by id=%i", value, gid);
543 sprintf(group, "%u", (unsigned int) gid);
544 add_rule_key(rule, &rule->group, operation, group);
549 add_rule_key(rule, &rule->group, operation, value);
553 if (strcasecmp(key, "MODE") == 0) {
554 rule->mode = strtol(value, NULL, 8);
555 rule->mode_operation = operation;
560 if (strcasecmp(key, "OPTIONS") == 0) {
561 if (strstr(value, "last_rule") != NULL) {
562 dbg("last rule to be applied");
565 if (strstr(value, "ignore_device") != NULL) {
566 dbg("device should be ignored");
567 rule->ignore_device = 1;
569 if (strstr(value, "ignore_remove") != NULL) {
570 dbg("remove event should be ignored");
571 rule->ignore_remove = 1;
573 if (strstr(value, "all_partitions") != NULL) {
574 dbg("creation of partition nodes requested");
575 rule->partitions = DEFAULT_PARTITIONS_COUNT;
581 err("unknown key '%s' in %s:%u", key, filename, lineno);
584 if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
585 err("PHYSDEV* values are deprecated and will be removed from a future kernel, "
586 "please fix it in %s:%u", filename, lineno);
588 /* skip line if not any valid key was found */
592 /* grow buffer and add rule */
593 rule_size = sizeof(struct udev_rule) + rule->bufsize;
594 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
595 dbg("add %zi padding bytes", padding);
596 rule_size += padding;
597 rule->bufsize += padding;
599 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
601 err("realloc failed");
604 dbg("adding rule to offset %zi", rules->bufsize);
605 memcpy(rules->buf + rules->bufsize, rule, rule_size);
606 rules->bufsize += rule_size;
613 err("invalid rule '%s:%u'", filename, lineno);
617 static int parse_file(struct udev_rules *rules, const char *filename)
619 char line[LINE_SIZE];
628 if (file_map(filename, &buf, &bufsize) != 0) {
629 err("can't open '%s' as rules file: %s", filename, strerror(errno));
632 info("reading '%s' as rules file", filename);
634 /* loop through the whole file */
637 while (cur < bufsize) {
640 count = buf_get_line(buf, bufsize, cur);
645 /* eat the whitespace */
646 while ((count > 0) && isspace(bufline[0])) {
653 /* see if this is a comment */
654 if (bufline[0] == COMMENT_CHARACTER)
657 if (count >= sizeof(line)) {
658 err("line too long, rule skipped '%s:%u'", filename, lineno);
662 /* skip backslash and newline from multiline rules */
663 for (i = j = 0; i < count; i++) {
664 if (bufline[i] == '\\' && bufline[i+1] == '\n')
667 line[j++] = bufline[i];
671 dbg("read '%s'", line);
672 add_to_rules(rules, line, filename, lineno);
675 file_unmap(buf, bufsize);
679 int udev_rules_init(struct udev_rules *rules, int resolve_names)
684 memset(rules, 0x00, sizeof(struct udev_rules));
685 rules->resolve_names = resolve_names;
687 /* parse rules file or all matching files in directory */
688 if (stat(udev_rules_dir, &stats) != 0)
691 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
692 dbg("parse single rules file '%s'", udev_rules_dir);
693 retval = parse_file(rules, udev_rules_dir);
695 struct name_entry *name_loop, *name_tmp;
696 LIST_HEAD(name_list);
698 dbg("parse rules directory '%s'", udev_rules_dir);
699 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
701 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
702 if (stat(name_loop->name, &stats) == 0) {
704 parse_file(rules, name_loop->name);
706 dbg("empty rules file '%s'", name_loop->name);
708 err("could not read '%s': %s", name_loop->name, strerror(errno));
709 list_del(&name_loop->node);
717 void udev_rules_cleanup(struct udev_rules *rules)