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.
31 #include "udev_libc_wrapper.h"
33 #include "udev_utils.h"
35 #include "udev_rules.h"
38 void udev_rules_iter_init(struct udev_rules *rules)
40 dbg("bufsize=%zi", rules->bufsize);
44 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
46 static struct udev_rule *rule;
51 dbg("current=%zi", rules->current);
52 if (rules->current >= rules->bufsize) {
58 rule = (struct udev_rule *) (rules->buf + rules->current);
59 rules->current += sizeof(struct udev_rule) + rule->bufsize;
64 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
66 static struct udev_rule *rule;
69 dbg("current=%zi", rules->current);
70 if (rules->current >= rules->bufsize) {
74 rule = (struct udev_rule *) (rules->buf + rules->current);
76 if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
77 dbg("moving forward, looking for label '%s'", label);
78 rules->current += sizeof(struct udev_rule) + rule->bufsize;
82 dbg("found label '%s'", label);
86 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
92 if (linepos == NULL && linepos[0] == '\0')
96 while (isspace(linepos[0]) || linepos[0] == ',')
100 if (linepos[0] == '\0')
106 if (linepos[0] == '\0')
108 if (isspace(linepos[0]))
110 if (linepos[0] == '=')
112 if (linepos[0] == '+')
114 if (linepos[0] == '!')
116 if (linepos[0] == ':')
120 /* remember end of key */
123 /* skip whitespace after key */
124 while (isspace(linepos[0]))
126 if (linepos[0] == '\0')
129 /* get operation type */
130 if (linepos[0] == '=' && linepos[1] == '=') {
131 *operation = KEY_OP_MATCH;
133 dbg("operator=match");
134 } else if (linepos[0] == '!' && linepos[1] == '=') {
135 *operation = KEY_OP_NOMATCH;
137 dbg("operator=nomatch");
138 } else if (linepos[0] == '+' && linepos[1] == '=') {
139 *operation = KEY_OP_ADD;
142 } else if (linepos[0] == '=') {
143 *operation = KEY_OP_ASSIGN;
145 dbg("operator=assign");
146 } else if (linepos[0] == ':' && linepos[1] == '=') {
147 *operation = KEY_OP_ASSIGN_FINAL;
149 dbg("operator=assign_final");
155 dbg("key='%s'", *key);
157 /* skip whitespace after operator */
158 while (isspace(linepos[0]))
160 if (linepos[0] == '\0')
164 if (linepos[0] == '"')
170 temp = strchr(linepos, '"');
175 dbg("value='%s'", *value);
177 /* move line to next key */
183 /* extract possible KEY{attr} */
184 static char *get_key_attribute(char *str)
189 attr = strchr(str, '{');
192 pos = strchr(attr, '}');
194 err("missing closing brace for format");
198 dbg("attribute='%s'", attr);
205 static int add_rule_key(struct udev_rule *rule, struct key *key,
206 enum key_operation operation, const char *value)
208 size_t val_len = strnlen(value, PATH_SIZE);
210 key->operation = operation;
212 key->val_off = rule->bufsize;
213 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
214 rule->bufsize += val_len+1;
219 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
220 enum key_operation operation, const char *key, const char *value)
222 size_t key_len = strnlen(key, PATH_SIZE);
224 if (pairs->count >= PAIRS_MAX) {
225 err("skip, too many keys in a single rule");
229 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
231 /* add the key-name of the pair */
232 pairs->keys[pairs->count].key_name_off = rule->bufsize;
233 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
234 rule->bufsize += key_len+1;
241 static int add_to_rules(struct udev_rules *rules, char *line)
243 struct udev_rule *rule;
251 /* get all the keys */
252 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
254 err("malloc failed");
263 enum key_operation operation = KEY_OP_UNSET;
265 retval = get_key(&linepos, &key, &operation, &value);
269 if (strcasecmp(key, "LABEL") == 0) {
270 add_rule_key(rule, &rule->label, operation, value);
275 if (strcasecmp(key, "GOTO") == 0) {
276 add_rule_key(rule, &rule->goto_label, operation, value);
281 if (strcasecmp(key, "KERNEL") == 0) {
282 add_rule_key(rule, &rule->kernel_name, operation, value);
287 if (strcasecmp(key, "SUBSYSTEM") == 0) {
288 add_rule_key(rule, &rule->subsystem, operation, value);
293 if (strcasecmp(key, "ACTION") == 0) {
294 add_rule_key(rule, &rule->action, operation, value);
299 if (strcasecmp(key, "DEVPATH") == 0) {
300 add_rule_key(rule, &rule->devpath, operation, value);
305 if (strcasecmp(key, "BUS") == 0) {
306 add_rule_key(rule, &rule->bus, operation, value);
311 if (strcasecmp(key, "ID") == 0) {
312 add_rule_key(rule, &rule->id, operation, value);
317 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
318 attr = get_key_attribute(key + sizeof("SYSFS")-1);
320 err("error parsing SYSFS attribute in '%s'", line);
323 add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
328 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
329 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
334 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
335 attr = get_key_attribute(key + sizeof("ENV")-1);
337 err("error parsing ENV attribute");
340 add_rule_key_pair(rule, &rule->env, operation, attr, value);
345 if (strcasecmp(key, "MODALIAS") == 0) {
346 add_rule_key(rule, &rule->modalias, operation, value);
351 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
352 attr = get_key_attribute(key + sizeof("IMPORT")-1);
353 if (attr && strstr(attr, "program")) {
354 dbg("IMPORT will be executed");
355 rule->import_type = IMPORT_PROGRAM;
356 } else if (attr && strstr(attr, "file")) {
357 dbg("IMPORT will be included as file");
358 rule->import_type = IMPORT_FILE;
359 } else if (attr && strstr(attr, "parent")) {
360 dbg("IMPORT will include the parent values");
361 rule->import_type = IMPORT_PARENT;
363 /* figure it out if it is executable */
364 char file[PATH_SIZE];
368 strlcpy(file, value, sizeof(file));
369 pos = strchr(file, ' ');
372 dbg("IMPORT auto mode for '%s'", file);
373 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
374 dbg("IMPORT is executable, will be executed (autotype)");
375 rule->import_type = IMPORT_PROGRAM;
377 dbg("IMPORT is not executable, will be included as file (autotype)");
378 rule->import_type = IMPORT_FILE;
381 add_rule_key(rule, &rule->import, operation, value);
386 if (strcasecmp(key, "DRIVER") == 0) {
387 add_rule_key(rule, &rule->driver, operation, value);
392 if (strcasecmp(key, "RESULT") == 0) {
393 add_rule_key(rule, &rule->result, operation, value);
398 if (strcasecmp(key, "PROGRAM") == 0) {
399 add_rule_key(rule, &rule->program, operation, value);
404 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
405 attr = get_key_attribute(key + sizeof("NAME")-1);
407 if (strstr(attr, "all_partitions") != NULL) {
408 dbg("creation of partition nodes requested");
409 rule->partitions = DEFAULT_PARTITIONS_COUNT;
411 if (strstr(attr, "ignore_remove") != NULL) {
412 dbg("remove event should be ignored");
413 rule->ignore_remove = 1;
416 if (value[0] == '\0') {
417 dbg("name empty device should be ignored");
418 rule->name.operation = operation;
419 rule->ignore_device = 1;
421 add_rule_key(rule, &rule->name, operation, value);
425 if (strcasecmp(key, "SYMLINK") == 0) {
426 add_rule_key(rule, &rule->symlink, operation, value);
431 if (strcasecmp(key, "OWNER") == 0) {
433 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
435 strtoul(value, &endptr, 10);
436 if (endptr[0] != '\0') {
438 uid_t uid = lookup_user(value);
439 dbg("replacing username='%s' by id=%i", value, uid);
440 sprintf(owner, "%li", uid);
441 add_rule_key(rule, &rule->owner, operation, owner);
446 add_rule_key(rule, &rule->owner, operation, value);
450 if (strcasecmp(key, "GROUP") == 0) {
452 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
454 strtoul(value, &endptr, 10);
455 if (endptr[0] != '\0') {
457 gid_t gid = lookup_group(value);
458 dbg("replacing groupname='%s' by id=%i", value, gid);
459 sprintf(group, "%li", gid);
460 add_rule_key(rule, &rule->group, operation, group);
465 add_rule_key(rule, &rule->group, operation, value);
469 if (strcasecmp(key, "MODE") == 0) {
470 rule->mode = strtol(value, NULL, 8);
471 rule->mode_operation = operation;
476 if (strcasecmp(key, "RUN") == 0) {
477 add_rule_key(rule, &rule->run, operation, value);
482 if (strcasecmp(key, "OPTIONS") == 0) {
483 if (strstr(value, "last_rule") != NULL) {
484 dbg("last rule to be applied");
487 if (strstr(value, "ignore_device") != NULL) {
488 dbg("device should be ignored");
489 rule->ignore_device = 1;
491 if (strstr(value, "ignore_remove") != NULL) {
492 dbg("remove event should be ignored");
493 rule->ignore_remove = 1;
495 if (strstr(value, "all_partitions") != NULL) {
496 dbg("creation of partition nodes requested");
497 rule->partitions = DEFAULT_PARTITIONS_COUNT;
503 err("unknown key '%s', in '%s'", key, line);
506 /* skip line if not any valid key was found */
508 err("invalid rule '%s'", line);
512 /* grow buffer and add rule */
513 rule_size = sizeof(struct udev_rule) + rule->bufsize;
514 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
515 dbg("add %zi padding bytes", padding);
516 rule_size += padding;
517 rule->bufsize += padding;
519 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
521 err("realloc failed");
524 dbg("adding rule to offset %zi", rules->bufsize);
525 memcpy(rules->buf + rules->bufsize, rule, rule_size);
526 rules->bufsize += rule_size;
532 static int parse_file(struct udev_rules *rules, const char *filename)
534 char line[LINE_SIZE];
543 if (file_map(filename, &buf, &bufsize) != 0) {
544 err("can't open '%s' as rules file", filename);
547 dbg("reading '%s' as rules file", filename);
549 /* loop through the whole file */
552 while (cur < bufsize) {
555 count = buf_get_line(buf, bufsize, cur);
560 if (count >= sizeof(line)) {
561 info("line too long, rule skipped %s, line %d", filename, lineno);
565 /* eat the whitespace */
566 while ((count > 0) && isspace(bufline[0])) {
573 /* see if this is a comment */
574 if (bufline[0] == COMMENT_CHARACTER)
577 /* skip backslash and newline from multi line rules */
578 for (i = j = 0; i < count; i++) {
579 if (bufline[i] == '\\' && bufline[i+1] == '\n')
582 line[j++] = bufline[i];
586 dbg("read '%s'", line);
587 add_to_rules(rules, line);
590 file_unmap(buf, bufsize);
594 static int rules_map(struct udev_rules *rules, const char *filename)
596 if (file_map(filename, &rules->buf, &rules->bufsize)) {
600 if (rules->bufsize == 0) {
601 file_unmap(rules->buf, rules->bufsize);
610 int udev_rules_init(struct udev_rules *rules, int read_compiled, int resolve_names)
612 char comp[PATH_SIZE];
616 memset(rules, 0x00, sizeof(struct udev_rules));
617 rules->resolve_names = resolve_names;
619 /* check for precompiled rules */
621 strlcpy(comp, udev_rules_filename, sizeof(comp));
622 strlcat(comp, ".compiled", sizeof(comp));
623 if (stat(comp, &stats) == 0) {
624 dbg("map compiled rules '%s'", comp);
625 if (rules_map(rules, comp) == 0)
630 /* parse rules file or all matching files in directory */
631 if (stat(udev_rules_filename, &stats) != 0)
634 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
635 dbg("parse single rules file '%s'", udev_rules_filename);
636 retval = parse_file(rules, udev_rules_filename);
638 struct name_entry *name_loop, *name_tmp;
639 LIST_HEAD(name_list);
641 dbg("parse rules directory '%s'", udev_rules_filename);
642 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
644 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
645 if (stat(name_loop->name, &stats) == 0) {
647 parse_file(rules, name_loop->name);
649 dbg("empty rules file '%s'", name_loop->name);
651 dbg("could not read '%s'", name_loop->name);
652 list_del(&name_loop->node);
660 void udev_rules_close(struct udev_rules *rules)
665 file_unmap(rules->buf, rules->bufsize);