6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "udev_libc_wrapper.h"
36 #include "udev_utils.h"
38 #include "udev_rules.h"
41 void udev_rules_iter_init(struct udev_rules *rules)
43 dbg("bufsize=%zi", rules->bufsize);
47 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
49 static struct udev_rule *rule;
54 dbg("current=%zi", rules->current);
55 if (rules->current >= rules->bufsize) {
61 rule = (struct udev_rule *) (rules->buf + rules->current);
62 rules->current += sizeof(struct udev_rule) + rule->bufsize;
67 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
69 static struct udev_rule *rule;
72 dbg("current=%zi", rules->current);
73 if (rules->current >= rules->bufsize) {
77 rule = (struct udev_rule *) (rules->buf + rules->current);
79 if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
80 dbg("moving forward, looking for label '%s'", label);
81 rules->current += sizeof(struct udev_rule) + rule->bufsize;
85 dbg("found label '%s'", label);
89 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
99 while (isspace(linepos[0]) || linepos[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]))
127 /* get operation type */
128 if (linepos[0] == '=' && linepos[1] == '=') {
129 *operation = KEY_OP_MATCH;
131 dbg("operator=match");
132 } else if (linepos[0] == '!' && linepos[1] == '=') {
133 *operation = KEY_OP_NOMATCH;
135 dbg("operator=nomatch");
136 } else if (linepos[0] == '+' && linepos[1] == '=') {
137 *operation = KEY_OP_ADD;
140 } else if (linepos[0] == '=') {
141 *operation = KEY_OP_ASSIGN;
143 dbg("operator=assign");
144 } else if (linepos[0] == ':' && linepos[1] == '=') {
145 *operation = KEY_OP_ASSIGN_FINAL;
147 dbg("operator=assign_final");
153 dbg("key='%s'", *key);
155 /* skip whitespace after operator */
156 while (isspace(linepos[0]))
160 if (linepos[0] == '"')
166 temp = strchr(linepos, '"');
171 dbg("value='%s'", *value);
173 /* move line to next key */
179 /* extract possible KEY{attr} */
180 static char *get_key_attribute(char *str)
185 attr = strchr(str, '{');
188 pos = strchr(attr, '}');
190 err("missing closing brace for format");
194 dbg("attribute='%s'", attr);
201 static int add_rule_key(struct udev_rule *rule, struct key *key,
202 enum key_operation operation, const char *value)
204 size_t val_len = strnlen(value, PATH_SIZE);
206 key->operation = operation;
208 key->val_off = rule->bufsize;
209 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
210 rule->bufsize += val_len+1;
215 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
216 enum key_operation operation, const char *key, const char *value)
218 size_t key_len = strnlen(key, PATH_SIZE);
220 if (pairs->count >= PAIRS_MAX) {
221 err("skip, too many keys in a single rule");
225 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
227 /* add the key-name of the pair */
228 pairs->keys[pairs->count].key_name_off = rule->bufsize;
229 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
230 rule->bufsize += key_len+1;
237 static int add_to_rules(struct udev_rules *rules, char *line)
239 struct udev_rule *rule;
246 /* get all the keys */
247 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
249 err("malloc failed");
258 enum key_operation operation = KEY_OP_UNSET;
260 retval = get_key(&linepos, &key, &operation, &value);
264 if (strcasecmp(key, "LABEL") == 0) {
265 add_rule_key(rule, &rule->label, operation, value);
270 if (strcasecmp(key, "GOTO") == 0) {
271 add_rule_key(rule, &rule->goto_label, operation, value);
276 if (strcasecmp(key, "KERNEL") == 0) {
277 add_rule_key(rule, &rule->kernel_name, operation, value);
282 if (strcasecmp(key, "SUBSYSTEM") == 0) {
283 add_rule_key(rule, &rule->subsystem, operation, value);
288 if (strcasecmp(key, "ACTION") == 0) {
289 add_rule_key(rule, &rule->action, operation, value);
294 if (strcasecmp(key, "DEVPATH") == 0) {
295 add_rule_key(rule, &rule->devpath, operation, value);
300 if (strcasecmp(key, "BUS") == 0) {
301 add_rule_key(rule, &rule->bus, operation, value);
306 if (strcasecmp(key, "ID") == 0) {
307 add_rule_key(rule, &rule->id, operation, value);
312 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
313 attr = get_key_attribute(key + sizeof("SYSFS")-1);
315 err("error parsing SYSFS attribute in '%s'", line);
318 add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
323 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
324 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
329 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
330 attr = get_key_attribute(key + sizeof("ENV")-1);
332 err("error parsing ENV attribute");
335 add_rule_key_pair(rule, &rule->env, operation, attr, value);
340 if (strcasecmp(key, "MODALIAS") == 0) {
341 add_rule_key(rule, &rule->modalias, operation, value);
346 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
347 attr = get_key_attribute(key + sizeof("IMPORT")-1);
348 if (attr && strstr(attr, "program")) {
349 dbg("IMPORT will be executed");
350 rule->import_type = IMPORT_PROGRAM;
351 } else if (attr && strstr(attr, "file")) {
352 dbg("IMPORT will be included as file");
353 rule->import_type = IMPORT_FILE;
354 } else if (attr && strstr(attr, "parent")) {
355 dbg("IMPORT will include the parent values");
356 rule->import_type = IMPORT_PARENT;
358 /* figure it out if it is executable */
359 char file[PATH_SIZE];
363 strlcpy(file, value, sizeof(file));
364 pos = strchr(file, ' ');
367 dbg("IMPORT auto mode for '%s'", file);
368 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
369 dbg("IMPORT is executable, will be executed (autotype)");
370 rule->import_type = IMPORT_PROGRAM;
372 dbg("IMPORT is not executable, will be included as file (autotype)");
373 rule->import_type = IMPORT_FILE;
376 add_rule_key(rule, &rule->import, operation, value);
381 if (strcasecmp(key, "DRIVER") == 0) {
382 add_rule_key(rule, &rule->driver, operation, value);
387 if (strcasecmp(key, "RESULT") == 0) {
388 add_rule_key(rule, &rule->result, operation, value);
393 if (strcasecmp(key, "PROGRAM") == 0) {
394 add_rule_key(rule, &rule->program, operation, value);
399 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
400 attr = get_key_attribute(key + sizeof("NAME")-1);
402 if (strstr(attr, "all_partitions") != NULL) {
403 dbg("creation of partition nodes requested");
404 rule->partitions = DEFAULT_PARTITIONS_COUNT;
406 if (strstr(attr, "ignore_remove") != NULL) {
407 dbg("remove event should be ignored");
408 rule->ignore_remove = 1;
411 if (value[0] == '\0') {
412 dbg("name empty device should be ignored");
413 rule->name.operation = operation;
414 rule->ignore_device = 1;
416 add_rule_key(rule, &rule->name, operation, value);
420 if (strcasecmp(key, "SYMLINK") == 0) {
421 add_rule_key(rule, &rule->symlink, operation, value);
426 if (strcasecmp(key, "OWNER") == 0) {
428 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
430 strtoul(value, &endptr, 10);
431 if (endptr[0] != '\0') {
433 uid_t uid = lookup_user(value);
434 dbg("replacing username='%s' by id=%i", value, uid);
435 sprintf(owner, "%li", uid);
436 add_rule_key(rule, &rule->owner, operation, owner);
441 add_rule_key(rule, &rule->owner, operation, value);
445 if (strcasecmp(key, "GROUP") == 0) {
447 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
449 strtoul(value, &endptr, 10);
450 if (endptr[0] != '\0') {
452 gid_t gid = lookup_group(value);
453 dbg("replacing groupname='%s' by id=%i", value, gid);
454 sprintf(group, "%li", gid);
455 add_rule_key(rule, &rule->group, operation, group);
460 add_rule_key(rule, &rule->group, operation, value);
464 if (strcasecmp(key, "MODE") == 0) {
465 rule->mode = strtol(value, NULL, 8);
466 rule->mode_operation = operation;
471 if (strcasecmp(key, "RUN") == 0) {
472 add_rule_key(rule, &rule->run, operation, value);
477 if (strcasecmp(key, "OPTIONS") == 0) {
478 if (strstr(value, "last_rule") != NULL) {
479 dbg("last rule to be applied");
482 if (strstr(value, "ignore_device") != NULL) {
483 dbg("device should be ignored");
484 rule->ignore_device = 1;
486 if (strstr(value, "ignore_remove") != NULL) {
487 dbg("remove event should be ignored");
488 rule->ignore_remove = 1;
490 if (strstr(value, "all_partitions") != NULL) {
491 dbg("creation of partition nodes requested");
492 rule->partitions = DEFAULT_PARTITIONS_COUNT;
498 err("unknown key '%s', in '%s'", key, line);
501 /* skip line if not any valid key was found */
503 err("invalid rule '%s'", line);
507 /* grow buffer and add rule */
508 rule_size = sizeof(struct udev_rule) + rule->bufsize;
509 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
511 err("realloc failed");
514 dbg("adding rule to offset %zi", rules->bufsize);
515 memcpy(rules->buf + rules->bufsize, rule, rule_size);
516 rules->bufsize += rule_size;
522 static int parse_file(struct udev_rules *rules, const char *filename)
524 char line[LINE_SIZE];
533 if (file_map(filename, &buf, &bufsize) != 0) {
534 err("can't open '%s' as rules file", filename);
537 dbg("reading '%s' as rules file", filename);
539 /* loop through the whole file */
542 while (cur < bufsize) {
545 count = buf_get_line(buf, bufsize, cur);
550 if (count >= sizeof(line)) {
551 info("line too long, rule skipped %s, line %d", filename, lineno);
555 /* eat the whitespace */
556 while ((count > 0) && isspace(bufline[0])) {
563 /* see if this is a comment */
564 if (bufline[0] == COMMENT_CHARACTER)
567 /* skip backslash and newline from multi line rules */
568 for (i = j = 0; i < count; i++) {
569 if (bufline[i] == '\\' && bufline[i+1] == '\n')
572 line[j++] = bufline[i];
576 dbg("read '%s'", line);
577 add_to_rules(rules, line);
580 file_unmap(buf, bufsize);
584 static int rules_map(struct udev_rules *rules, const char *filename)
586 if (file_map(filename, &rules->buf, &rules->bufsize)) {
590 if (rules->bufsize == 0) {
591 file_unmap(rules->buf, rules->bufsize);
600 int udev_rules_init(struct udev_rules *rules, int resolve_names)
602 char comp[PATH_SIZE];
606 memset(rules, 0x00, sizeof(struct udev_rules));
607 rules->resolve_names = resolve_names;
609 /* check for precompiled rules */
610 strlcpy(comp, udev_rules_filename, sizeof(comp));
611 strlcat(comp, ".compiled", sizeof(comp));
612 if (stat(comp, &stats) == 0) {
613 dbg("map compiled rules '%s'", comp);
614 if (rules_map(rules, comp) == 0)
618 if (stat(udev_rules_filename, &stats) != 0)
621 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
622 dbg("parse single rules file '%s'", udev_rules_filename);
623 retval = parse_file(rules, udev_rules_filename);
625 struct name_entry *name_loop, *name_tmp;
626 LIST_HEAD(name_list);
628 dbg("parse rules directory '%s'", udev_rules_filename);
629 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
631 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
632 parse_file(rules, name_loop->name);
633 list_del(&name_loop->node);
640 void udev_rules_close(struct udev_rules *rules)
643 file_unmap(rules->buf, rules->bufsize);