4 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include "udev_rules.h"
35 void udev_rules_iter_init(struct udev_rules *rules)
37 dbg("bufsize=%zi", rules->bufsize);
41 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
43 static struct udev_rule *rule;
48 dbg("current=%zi", rules->current);
49 if (rules->current >= rules->bufsize) {
55 rule = (struct udev_rule *) (rules->buf + rules->current);
56 rules->current += sizeof(struct udev_rule) + rule->bufsize;
61 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
63 static struct udev_rule *rule;
66 dbg("current=%zi", rules->current);
67 if (rules->current >= rules->bufsize) {
71 rule = (struct udev_rule *) (rules->buf + rules->current);
73 if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
74 dbg("moving forward, looking for label '%s'", label);
75 rules->current += sizeof(struct udev_rule) + rule->bufsize;
79 dbg("found label '%s'", label);
83 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
89 if (linepos == NULL && linepos[0] == '\0')
93 while (isspace(linepos[0]) || linepos[0] == ',')
97 if (linepos[0] == '\0')
103 if (linepos[0] == '\0')
105 if (isspace(linepos[0]))
107 if (linepos[0] == '=')
109 if (linepos[0] == '+')
111 if (linepos[0] == '!')
113 if (linepos[0] == ':')
117 /* remember end of key */
120 /* skip whitespace after key */
121 while (isspace(linepos[0]))
123 if (linepos[0] == '\0')
126 /* get operation type */
127 if (linepos[0] == '=' && linepos[1] == '=') {
128 *operation = KEY_OP_MATCH;
130 dbg("operator=match");
131 } else if (linepos[0] == '!' && linepos[1] == '=') {
132 *operation = KEY_OP_NOMATCH;
134 dbg("operator=nomatch");
135 } else if (linepos[0] == '+' && linepos[1] == '=') {
136 *operation = KEY_OP_ADD;
139 } else if (linepos[0] == '=') {
140 *operation = KEY_OP_ASSIGN;
142 dbg("operator=assign");
143 } else if (linepos[0] == ':' && linepos[1] == '=') {
144 *operation = KEY_OP_ASSIGN_FINAL;
146 dbg("operator=assign_final");
152 dbg("key='%s'", *key);
154 /* skip whitespace after operator */
155 while (isspace(linepos[0]))
157 if (linepos[0] == '\0')
161 if (linepos[0] == '"')
167 temp = strchr(linepos, '"');
172 dbg("value='%s'", *value);
174 /* move line to next key */
180 /* extract possible KEY{attr} */
181 static char *get_key_attribute(char *str)
186 attr = strchr(str, '{');
189 pos = strchr(attr, '}');
191 err("missing closing brace for format");
195 dbg("attribute='%s'", attr);
202 static int add_rule_key(struct udev_rule *rule, struct key *key,
203 enum key_operation operation, const char *value)
205 size_t val_len = strnlen(value, PATH_SIZE);
207 key->operation = operation;
209 key->val_off = rule->bufsize;
210 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
211 rule->bufsize += val_len+1;
216 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
217 enum key_operation operation, const char *key, const char *value)
219 size_t key_len = strnlen(key, PATH_SIZE);
221 if (pairs->count >= PAIRS_MAX) {
222 err("skip, too many keys in a single rule");
226 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
228 /* add the key-name of the pair */
229 pairs->keys[pairs->count].key_name_off = rule->bufsize;
230 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
231 rule->bufsize += key_len+1;
238 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
240 struct udev_rule *rule;
248 /* get all the keys */
249 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
251 err("malloc failed");
260 enum key_operation operation = KEY_OP_UNSET;
262 retval = get_key(&linepos, &key, &operation, &value);
266 if (strcasecmp(key, "ACTION") == 0) {
267 if (operation != KEY_OP_MATCH &&
268 operation != KEY_OP_NOMATCH) {
269 err("invalid ACTION operation");
272 add_rule_key(rule, &rule->action, operation, value);
277 if (strcasecmp(key, "DEVPATH") == 0) {
278 if (operation != KEY_OP_MATCH &&
279 operation != KEY_OP_NOMATCH) {
280 err("invalid DEVPATH operation");
283 add_rule_key(rule, &rule->devpath, operation, value);
288 if (strcasecmp(key, "KERNEL") == 0) {
289 if (operation != KEY_OP_MATCH &&
290 operation != KEY_OP_NOMATCH) {
291 err("invalid KERNEL operation");
294 add_rule_key(rule, &rule->kernel, operation, value);
299 if (strcasecmp(key, "SUBSYSTEM") == 0) {
300 if (operation != KEY_OP_MATCH &&
301 operation != KEY_OP_NOMATCH) {
302 err("invalid SUBSYSTEM operation");
305 add_rule_key(rule, &rule->subsystem, operation, value);
310 if (strcasecmp(key, "DRIVER") == 0) {
311 if (operation != KEY_OP_MATCH &&
312 operation != KEY_OP_NOMATCH) {
313 err("invalid DRIVER operation");
316 err("DRIVER== will change in a future relase, "
317 "please use DRIVERS== in %s:%u", filename, lineno);
318 /* FIXME: this should be rule->driver to match only the event device */
319 add_rule_key(rule, &rule->drivers, operation, value);
324 if (strncasecmp(key, "ATTR", sizeof("ATTR")-1) == 0) {
325 if (operation != KEY_OP_MATCH &&
326 operation != KEY_OP_NOMATCH) {
327 err("invalid ATTR operation");
330 attr = get_key_attribute(key + sizeof("ATTR")-1);
332 err("error parsing ATTR attribute in '%s'", line);
335 add_rule_key_pair(rule, &rule->attr, operation, attr, value);
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");
347 add_rule_key(rule, &rule->kernels, operation, value);
352 if (strcasecmp(key, "SUBSYTEMS") == 0 ||
353 strcasecmp(key, "BUS") == 0) {
354 if (operation != KEY_OP_MATCH &&
355 operation != KEY_OP_NOMATCH) {
356 err("invalid SUBSYSTEMS operation");
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");
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 ATTRSS operation");
382 attr = get_key_attribute(key + sizeof("ATTRS")-1);
384 err("error parsing ATTRS attribute in '%s'", line);
387 add_rule_key_pair(rule, &rule->attrs, operation, attr, value);
392 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
393 attr = get_key_attribute(key + sizeof("ENV")-1);
395 err("error parsing ENV attribute");
398 add_rule_key_pair(rule, &rule->env, operation, attr, value);
403 if (strcasecmp(key, "PROGRAM") == 0) {
404 add_rule_key(rule, &rule->program, operation, value);
409 if (strcasecmp(key, "RESULT") == 0) {
410 if (operation != KEY_OP_MATCH &&
411 operation != KEY_OP_NOMATCH) {
412 err("invalid RESULT operation");
415 add_rule_key(rule, &rule->result, operation, value);
420 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
421 attr = get_key_attribute(key + sizeof("IMPORT")-1);
422 if (attr && strstr(attr, "program")) {
423 dbg("IMPORT will be executed");
424 rule->import_type = IMPORT_PROGRAM;
425 } else if (attr && strstr(attr, "file")) {
426 dbg("IMPORT will be included as file");
427 rule->import_type = IMPORT_FILE;
428 } else if (attr && strstr(attr, "parent")) {
429 dbg("IMPORT will include the parent values");
430 rule->import_type = IMPORT_PARENT;
432 /* figure it out if it is executable */
433 char file[PATH_SIZE];
437 strlcpy(file, value, sizeof(file));
438 pos = strchr(file, ' ');
442 /* allow programs in /lib/udev called without the path */
443 if (strchr(file, '/') == NULL) {
444 strlcpy(file, "/lib/udev/", sizeof(file));
445 strlcat(file, value, sizeof(file));
446 pos = strchr(file, ' ');
451 dbg("IMPORT auto mode for '%s'", file);
452 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
453 dbg("IMPORT is executable, will be executed (autotype)");
454 rule->import_type = IMPORT_PROGRAM;
456 dbg("IMPORT is not executable, will be included as file (autotype)");
457 rule->import_type = IMPORT_FILE;
460 add_rule_key(rule, &rule->import, operation, value);
465 if (strcasecmp(key, "RUN") == 0) {
466 add_rule_key(rule, &rule->run, operation, value);
471 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
472 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
477 if (strcasecmp(key, "LABEL") == 0) {
478 add_rule_key(rule, &rule->label, operation, value);
483 if (strcasecmp(key, "GOTO") == 0) {
484 add_rule_key(rule, &rule->goto_label, operation, value);
489 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
490 attr = get_key_attribute(key + sizeof("NAME")-1);
492 if (strstr(attr, "all_partitions") != NULL) {
493 dbg("creation of partition nodes requested");
494 rule->partitions = DEFAULT_PARTITIONS_COUNT;
496 if (strstr(attr, "ignore_remove") != NULL) {
497 dbg("remove event should be ignored");
498 rule->ignore_remove = 1;
501 if (value[0] == '\0')
502 dbg("name empty, node creation supressed");
503 add_rule_key(rule, &rule->name, operation, value);
507 if (strcasecmp(key, "SYMLINK") == 0) {
508 add_rule_key(rule, &rule->symlink, operation, value);
513 if (strcasecmp(key, "OWNER") == 0) {
515 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
517 strtoul(value, &endptr, 10);
518 if (endptr[0] != '\0') {
520 uid_t uid = lookup_user(value);
521 dbg("replacing username='%s' by id=%i", value, uid);
522 sprintf(owner, "%u", (unsigned int) uid);
523 add_rule_key(rule, &rule->owner, operation, owner);
528 add_rule_key(rule, &rule->owner, operation, value);
532 if (strcasecmp(key, "GROUP") == 0) {
534 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
536 strtoul(value, &endptr, 10);
537 if (endptr[0] != '\0') {
539 gid_t gid = lookup_group(value);
540 dbg("replacing groupname='%s' by id=%i", value, gid);
541 sprintf(group, "%u", (unsigned int) gid);
542 add_rule_key(rule, &rule->group, operation, group);
547 add_rule_key(rule, &rule->group, operation, value);
551 if (strcasecmp(key, "MODE") == 0) {
552 rule->mode = strtol(value, NULL, 8);
553 rule->mode_operation = operation;
558 if (strcasecmp(key, "OPTIONS") == 0) {
559 if (strstr(value, "last_rule") != NULL) {
560 dbg("last rule to be applied");
563 if (strstr(value, "ignore_device") != NULL) {
564 dbg("device should be ignored");
565 rule->ignore_device = 1;
567 if (strstr(value, "ignore_remove") != NULL) {
568 dbg("remove event should be ignored");
569 rule->ignore_remove = 1;
571 if (strstr(value, "all_partitions") != NULL) {
572 dbg("creation of partition nodes requested");
573 rule->partitions = DEFAULT_PARTITIONS_COUNT;
579 err("unknown key '%s' in %s:%u", key, filename, lineno);
582 /* skip line if not any valid key was found */
586 /* grow buffer and add rule */
587 rule_size = sizeof(struct udev_rule) + rule->bufsize;
588 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
589 dbg("add %zi padding bytes", padding);
590 rule_size += padding;
591 rule->bufsize += padding;
593 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
595 err("realloc failed");
598 dbg("adding rule to offset %zi", rules->bufsize);
599 memcpy(rules->buf + rules->bufsize, rule, rule_size);
600 rules->bufsize += rule_size;
607 err("invalid rule '%s:%u'", filename, lineno);
611 static int parse_file(struct udev_rules *rules, const char *filename)
613 char line[LINE_SIZE];
622 if (file_map(filename, &buf, &bufsize) != 0) {
623 err("can't open '%s' as rules file: %s", filename, strerror(errno));
626 dbg("reading '%s' as rules file", filename);
628 /* loop through the whole file */
631 while (cur < bufsize) {
634 count = buf_get_line(buf, bufsize, cur);
639 /* eat the whitespace */
640 while ((count > 0) && isspace(bufline[0])) {
647 /* see if this is a comment */
648 if (bufline[0] == COMMENT_CHARACTER)
651 if (count >= sizeof(line)) {
652 err("line too long, rule skipped '%s:%u'", filename, lineno);
656 /* skip backslash and newline from multiline rules */
657 for (i = j = 0; i < count; i++) {
658 if (bufline[i] == '\\' && bufline[i+1] == '\n')
661 line[j++] = bufline[i];
665 dbg("read '%s'", line);
666 add_to_rules(rules, line, filename, lineno);
669 file_unmap(buf, bufsize);
673 int udev_rules_init(struct udev_rules *rules, int resolve_names)
678 memset(rules, 0x00, sizeof(struct udev_rules));
679 rules->resolve_names = resolve_names;
681 /* parse rules file or all matching files in directory */
682 if (stat(udev_rules_filename, &stats) != 0)
685 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
686 dbg("parse single rules file '%s'", udev_rules_filename);
687 retval = parse_file(rules, udev_rules_filename);
689 struct name_entry *name_loop, *name_tmp;
690 LIST_HEAD(name_list);
692 dbg("parse rules directory '%s'", udev_rules_filename);
693 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
695 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
696 if (stat(name_loop->name, &stats) == 0) {
698 parse_file(rules, name_loop->name);
700 dbg("empty rules file '%s'", name_loop->name);
702 err("could not read '%s': %s", name_loop->name, strerror(errno));
703 list_del(&name_loop->node);
711 void udev_rules_cleanup(struct udev_rules *rules)