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 of the same type 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 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 attr = get_key_attribute(key + sizeof("ATTRS")-1);
375 err("error parsing ATTRS attribute");
378 if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
384 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
385 attr = get_key_attribute(key + sizeof("ENV")-1);
387 err("error parsing ENV attribute");
390 if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
396 if (strcasecmp(key, "PROGRAM") == 0) {
397 add_rule_key(rule, &rule->program, operation, value);
402 if (strcasecmp(key, "RESULT") == 0) {
403 if (operation != KEY_OP_MATCH &&
404 operation != KEY_OP_NOMATCH) {
405 err("invalid RESULT operation");
408 add_rule_key(rule, &rule->result, operation, value);
413 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
414 attr = get_key_attribute(key + sizeof("IMPORT")-1);
415 if (attr && strstr(attr, "program")) {
416 dbg("IMPORT will be executed");
417 rule->import_type = IMPORT_PROGRAM;
418 } else if (attr && strstr(attr, "file")) {
419 dbg("IMPORT will be included as file");
420 rule->import_type = IMPORT_FILE;
421 } else if (attr && strstr(attr, "parent")) {
422 dbg("IMPORT will include the parent values");
423 rule->import_type = IMPORT_PARENT;
425 /* figure it out if it is executable */
426 char file[PATH_SIZE];
430 strlcpy(file, value, sizeof(file));
431 pos = strchr(file, ' ');
435 /* allow programs in /lib/udev called without the path */
436 if (strchr(file, '/') == NULL) {
437 strlcpy(file, "/lib/udev/", sizeof(file));
438 strlcat(file, value, sizeof(file));
439 pos = strchr(file, ' ');
444 dbg("IMPORT auto mode for '%s'", file);
445 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
446 dbg("IMPORT is executable, will be executed (autotype)");
447 rule->import_type = IMPORT_PROGRAM;
449 dbg("IMPORT is not executable, will be included as file (autotype)");
450 rule->import_type = IMPORT_FILE;
453 add_rule_key(rule, &rule->import, operation, value);
458 if (strcasecmp(key, "RUN") == 0) {
459 add_rule_key(rule, &rule->run, operation, value);
464 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
465 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
470 if (strcasecmp(key, "LABEL") == 0) {
471 add_rule_key(rule, &rule->label, operation, value);
476 if (strcasecmp(key, "GOTO") == 0) {
477 add_rule_key(rule, &rule->goto_label, operation, value);
482 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
483 attr = get_key_attribute(key + sizeof("NAME")-1);
485 if (strstr(attr, "all_partitions") != NULL) {
486 dbg("creation of partition nodes requested");
487 rule->partitions = DEFAULT_PARTITIONS_COUNT;
489 if (strstr(attr, "ignore_remove") != NULL) {
490 dbg("remove event should be ignored");
491 rule->ignore_remove = 1;
494 if (value[0] == '\0')
495 dbg("name empty, node creation supressed");
496 add_rule_key(rule, &rule->name, operation, value);
500 if (strcasecmp(key, "SYMLINK") == 0) {
501 add_rule_key(rule, &rule->symlink, operation, value);
506 if (strcasecmp(key, "OWNER") == 0) {
508 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
510 strtoul(value, &endptr, 10);
511 if (endptr[0] != '\0') {
513 uid_t uid = lookup_user(value);
514 dbg("replacing username='%s' by id=%i", value, uid);
515 sprintf(owner, "%u", (unsigned int) uid);
516 add_rule_key(rule, &rule->owner, operation, owner);
521 add_rule_key(rule, &rule->owner, operation, value);
525 if (strcasecmp(key, "GROUP") == 0) {
527 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
529 strtoul(value, &endptr, 10);
530 if (endptr[0] != '\0') {
532 gid_t gid = lookup_group(value);
533 dbg("replacing groupname='%s' by id=%i", value, gid);
534 sprintf(group, "%u", (unsigned int) gid);
535 add_rule_key(rule, &rule->group, operation, group);
540 add_rule_key(rule, &rule->group, operation, value);
544 if (strcasecmp(key, "MODE") == 0) {
545 rule->mode = strtol(value, NULL, 8);
546 rule->mode_operation = operation;
551 if (strcasecmp(key, "OPTIONS") == 0) {
552 if (strstr(value, "last_rule") != NULL) {
553 dbg("last rule to be applied");
556 if (strstr(value, "ignore_device") != NULL) {
557 dbg("device should be ignored");
558 rule->ignore_device = 1;
560 if (strstr(value, "ignore_remove") != NULL) {
561 dbg("remove event should be ignored");
562 rule->ignore_remove = 1;
564 if (strstr(value, "all_partitions") != NULL) {
565 dbg("creation of partition nodes requested");
566 rule->partitions = DEFAULT_PARTITIONS_COUNT;
572 err("unknown key '%s' in %s:%u", key, filename, lineno);
575 /* skip line if not any valid key was found */
579 /* grow buffer and add rule */
580 rule_size = sizeof(struct udev_rule) + rule->bufsize;
581 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
582 dbg("add %zi padding bytes", padding);
583 rule_size += padding;
584 rule->bufsize += padding;
586 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
588 err("realloc failed");
591 dbg("adding rule to offset %zi", rules->bufsize);
592 memcpy(rules->buf + rules->bufsize, rule, rule_size);
593 rules->bufsize += rule_size;
600 err("invalid rule '%s:%u'", filename, lineno);
604 static int parse_file(struct udev_rules *rules, const char *filename)
606 char line[LINE_SIZE];
615 if (file_map(filename, &buf, &bufsize) != 0) {
616 err("can't open '%s' as rules file: %s", filename, strerror(errno));
619 dbg("reading '%s' as rules file", filename);
621 /* loop through the whole file */
624 while (cur < bufsize) {
627 count = buf_get_line(buf, bufsize, cur);
632 /* eat the whitespace */
633 while ((count > 0) && isspace(bufline[0])) {
640 /* see if this is a comment */
641 if (bufline[0] == COMMENT_CHARACTER)
644 if (count >= sizeof(line)) {
645 err("line too long, rule skipped '%s:%u'", filename, lineno);
649 /* skip backslash and newline from multiline rules */
650 for (i = j = 0; i < count; i++) {
651 if (bufline[i] == '\\' && bufline[i+1] == '\n')
654 line[j++] = bufline[i];
658 dbg("read '%s'", line);
659 add_to_rules(rules, line, filename, lineno);
662 file_unmap(buf, bufsize);
666 int udev_rules_init(struct udev_rules *rules, int resolve_names)
671 memset(rules, 0x00, sizeof(struct udev_rules));
672 rules->resolve_names = resolve_names;
674 /* parse rules file or all matching files in directory */
675 if (stat(udev_rules_filename, &stats) != 0)
678 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
679 dbg("parse single rules file '%s'", udev_rules_filename);
680 retval = parse_file(rules, udev_rules_filename);
682 struct name_entry *name_loop, *name_tmp;
683 LIST_HEAD(name_list);
685 dbg("parse rules directory '%s'", udev_rules_filename);
686 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
688 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
689 if (stat(name_loop->name, &stats) == 0) {
691 parse_file(rules, name_loop->name);
693 dbg("empty rules file '%s'", name_loop->name);
695 err("could not read '%s': %s", name_loop->name, strerror(errno));
696 list_del(&name_loop->node);
704 void udev_rules_cleanup(struct udev_rules *rules)