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)
96 while (isspace(linepos[0]) || linepos[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]))
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]))
157 if (linepos[0] == '"')
163 temp = strchr(linepos, '"');
168 dbg("value='%s'", *value);
170 /* move line to next key */
176 /* extract possible KEY{attr} */
177 static char *get_key_attribute(char *str)
182 attr = strchr(str, '{');
185 pos = strchr(attr, '}');
187 err("missing closing brace for format");
191 dbg("attribute='%s'", attr);
198 static int add_rule_key(struct udev_rule *rule, struct key *key,
199 enum key_operation operation, const char *value)
201 size_t val_len = strnlen(value, PATH_SIZE);
203 key->operation = operation;
205 key->val_off = rule->bufsize;
206 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
207 rule->bufsize += val_len+1;
212 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
213 enum key_operation operation, const char *key, const char *value)
215 size_t key_len = strnlen(key, PATH_SIZE);
217 if (pairs->count >= PAIRS_MAX) {
218 err("skip, too many keys in a single rule");
222 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
224 /* add the key-name of the pair */
225 pairs->keys[pairs->count].key_name_off = rule->bufsize;
226 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
227 rule->bufsize += key_len+1;
234 static int add_to_rules(struct udev_rules *rules, char *line)
236 struct udev_rule *rule;
244 /* get all the keys */
245 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
247 err("malloc failed");
256 enum key_operation operation = KEY_OP_UNSET;
258 retval = get_key(&linepos, &key, &operation, &value);
262 if (strcasecmp(key, "LABEL") == 0) {
263 add_rule_key(rule, &rule->label, operation, value);
268 if (strcasecmp(key, "GOTO") == 0) {
269 add_rule_key(rule, &rule->goto_label, operation, value);
274 if (strcasecmp(key, "KERNEL") == 0) {
275 add_rule_key(rule, &rule->kernel_name, operation, value);
280 if (strcasecmp(key, "SUBSYSTEM") == 0) {
281 add_rule_key(rule, &rule->subsystem, operation, value);
286 if (strcasecmp(key, "ACTION") == 0) {
287 add_rule_key(rule, &rule->action, operation, value);
292 if (strcasecmp(key, "DEVPATH") == 0) {
293 add_rule_key(rule, &rule->devpath, operation, value);
298 if (strcasecmp(key, "BUS") == 0) {
299 add_rule_key(rule, &rule->bus, operation, value);
304 if (strcasecmp(key, "ID") == 0) {
305 add_rule_key(rule, &rule->id, operation, value);
310 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
311 attr = get_key_attribute(key + sizeof("SYSFS")-1);
313 err("error parsing SYSFS attribute in '%s'", line);
316 add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
321 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
322 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
327 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
328 attr = get_key_attribute(key + sizeof("ENV")-1);
330 err("error parsing ENV attribute");
333 add_rule_key_pair(rule, &rule->env, operation, attr, value);
338 if (strcasecmp(key, "MODALIAS") == 0) {
339 add_rule_key(rule, &rule->modalias, operation, value);
344 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
345 attr = get_key_attribute(key + sizeof("IMPORT")-1);
346 if (attr && strstr(attr, "program")) {
347 dbg("IMPORT will be executed");
348 rule->import_type = IMPORT_PROGRAM;
349 } else if (attr && strstr(attr, "file")) {
350 dbg("IMPORT will be included as file");
351 rule->import_type = IMPORT_FILE;
352 } else if (attr && strstr(attr, "parent")) {
353 dbg("IMPORT will include the parent values");
354 rule->import_type = IMPORT_PARENT;
356 /* figure it out if it is executable */
357 char file[PATH_SIZE];
361 strlcpy(file, value, sizeof(file));
362 pos = strchr(file, ' ');
365 dbg("IMPORT auto mode for '%s'", file);
366 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
367 dbg("IMPORT is executable, will be executed (autotype)");
368 rule->import_type = IMPORT_PROGRAM;
370 dbg("IMPORT is not executable, will be included as file (autotype)");
371 rule->import_type = IMPORT_FILE;
374 add_rule_key(rule, &rule->import, operation, value);
379 if (strcasecmp(key, "DRIVER") == 0) {
380 add_rule_key(rule, &rule->driver, operation, value);
385 if (strcasecmp(key, "RESULT") == 0) {
386 add_rule_key(rule, &rule->result, operation, value);
391 if (strcasecmp(key, "PROGRAM") == 0) {
392 add_rule_key(rule, &rule->program, operation, value);
397 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
398 attr = get_key_attribute(key + sizeof("NAME")-1);
400 if (strstr(attr, "all_partitions") != NULL) {
401 dbg("creation of partition nodes requested");
402 rule->partitions = DEFAULT_PARTITIONS_COUNT;
404 if (strstr(attr, "ignore_remove") != NULL) {
405 dbg("remove event should be ignored");
406 rule->ignore_remove = 1;
409 if (value[0] == '\0') {
410 dbg("name empty device should be ignored");
411 rule->name.operation = operation;
412 rule->ignore_device = 1;
414 add_rule_key(rule, &rule->name, operation, value);
418 if (strcasecmp(key, "SYMLINK") == 0) {
419 add_rule_key(rule, &rule->symlink, operation, value);
424 if (strcasecmp(key, "OWNER") == 0) {
426 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
428 strtoul(value, &endptr, 10);
429 if (endptr[0] != '\0') {
431 uid_t uid = lookup_user(value);
432 dbg("replacing username='%s' by id=%i", value, uid);
433 sprintf(owner, "%li", uid);
434 add_rule_key(rule, &rule->owner, operation, owner);
439 add_rule_key(rule, &rule->owner, operation, value);
443 if (strcasecmp(key, "GROUP") == 0) {
445 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
447 strtoul(value, &endptr, 10);
448 if (endptr[0] != '\0') {
450 gid_t gid = lookup_group(value);
451 dbg("replacing groupname='%s' by id=%i", value, gid);
452 sprintf(group, "%li", gid);
453 add_rule_key(rule, &rule->group, operation, group);
458 add_rule_key(rule, &rule->group, operation, value);
462 if (strcasecmp(key, "MODE") == 0) {
463 rule->mode = strtol(value, NULL, 8);
464 rule->mode_operation = operation;
469 if (strcasecmp(key, "RUN") == 0) {
470 add_rule_key(rule, &rule->run, operation, value);
475 if (strcasecmp(key, "OPTIONS") == 0) {
476 if (strstr(value, "last_rule") != NULL) {
477 dbg("last rule to be applied");
480 if (strstr(value, "ignore_device") != NULL) {
481 dbg("device should be ignored");
482 rule->ignore_device = 1;
484 if (strstr(value, "ignore_remove") != NULL) {
485 dbg("remove event should be ignored");
486 rule->ignore_remove = 1;
488 if (strstr(value, "all_partitions") != NULL) {
489 dbg("creation of partition nodes requested");
490 rule->partitions = DEFAULT_PARTITIONS_COUNT;
496 err("unknown key '%s', in '%s'", key, line);
499 /* skip line if not any valid key was found */
501 err("invalid rule '%s'", line);
505 /* grow buffer and add rule */
506 rule_size = sizeof(struct udev_rule) + rule->bufsize;
507 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
508 dbg("add %zi padding bytes", padding);
509 rule_size += padding;
510 rule->bufsize += padding;
512 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
514 err("realloc failed");
517 dbg("adding rule to offset %zi", rules->bufsize);
518 memcpy(rules->buf + rules->bufsize, rule, rule_size);
519 rules->bufsize += rule_size;
525 static int parse_file(struct udev_rules *rules, const char *filename)
527 char line[LINE_SIZE];
536 if (file_map(filename, &buf, &bufsize) != 0) {
537 err("can't open '%s' as rules file", filename);
540 dbg("reading '%s' as rules file", filename);
542 /* loop through the whole file */
545 while (cur < bufsize) {
548 count = buf_get_line(buf, bufsize, cur);
553 if (count >= sizeof(line)) {
554 info("line too long, rule skipped %s, line %d", filename, lineno);
558 /* eat the whitespace */
559 while ((count > 0) && isspace(bufline[0])) {
566 /* see if this is a comment */
567 if (bufline[0] == COMMENT_CHARACTER)
570 /* skip backslash and newline from multi line rules */
571 for (i = j = 0; i < count; i++) {
572 if (bufline[i] == '\\' && bufline[i+1] == '\n')
575 line[j++] = bufline[i];
579 dbg("read '%s'", line);
580 add_to_rules(rules, line);
583 file_unmap(buf, bufsize);
587 static int rules_map(struct udev_rules *rules, const char *filename)
589 if (file_map(filename, &rules->buf, &rules->bufsize)) {
593 if (rules->bufsize == 0) {
594 file_unmap(rules->buf, rules->bufsize);
603 int udev_rules_init(struct udev_rules *rules, int resolve_names)
605 char comp[PATH_SIZE];
609 memset(rules, 0x00, sizeof(struct udev_rules));
610 rules->resolve_names = resolve_names;
612 /* check for precompiled rules */
613 strlcpy(comp, udev_rules_filename, sizeof(comp));
614 strlcat(comp, ".compiled", sizeof(comp));
615 if (stat(comp, &stats) == 0) {
616 dbg("map compiled rules '%s'", comp);
617 if (rules_map(rules, comp) == 0)
621 if (stat(udev_rules_filename, &stats) != 0)
624 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
625 dbg("parse single rules file '%s'", udev_rules_filename);
626 retval = parse_file(rules, udev_rules_filename);
628 struct name_entry *name_loop, *name_tmp;
629 LIST_HEAD(name_list);
631 dbg("parse rules directory '%s'", udev_rules_filename);
632 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
634 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
635 parse_file(rules, name_loop->name);
636 list_del(&name_loop->node);
643 void udev_rules_close(struct udev_rules *rules)
646 file_unmap(rules->buf, rules->bufsize);