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, "LABEL") == 0) {
267 add_rule_key(rule, &rule->label, operation, value);
272 if (strcasecmp(key, "GOTO") == 0) {
273 add_rule_key(rule, &rule->goto_label, operation, value);
278 if (strcasecmp(key, "KERNEL") == 0) {
279 if (operation != KEY_OP_MATCH &&
280 operation != KEY_OP_NOMATCH) {
281 err("invalid KERNEL operation");
284 add_rule_key(rule, &rule->kernel_name, operation, value);
289 if (strcasecmp(key, "SUBSYSTEM") == 0) {
290 if (operation != KEY_OP_MATCH &&
291 operation != KEY_OP_NOMATCH) {
292 err("invalid SUBSYSTEM operation");
295 add_rule_key(rule, &rule->subsystem, operation, value);
300 if (strcasecmp(key, "ACTION") == 0) {
301 if (operation != KEY_OP_MATCH &&
302 operation != KEY_OP_NOMATCH) {
303 err("invalid ACTION operation");
306 add_rule_key(rule, &rule->action, operation, value);
311 if (strcasecmp(key, "DEVPATH") == 0) {
312 if (operation != KEY_OP_MATCH &&
313 operation != KEY_OP_NOMATCH) {
314 err("invalid DEVPATH operation");
317 add_rule_key(rule, &rule->devpath, operation, value);
322 if (strcasecmp(key, "BUS") == 0) {
323 if (operation != KEY_OP_MATCH &&
324 operation != KEY_OP_NOMATCH) {
325 err("invalid BUS operation");
328 add_rule_key(rule, &rule->bus, operation, value);
333 if (strcasecmp(key, "ID") == 0) {
334 if (operation != KEY_OP_MATCH &&
335 operation != KEY_OP_NOMATCH) {
336 err("invalid ID operation");
339 add_rule_key(rule, &rule->id, operation, value);
344 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
345 if (operation != KEY_OP_MATCH &&
346 operation != KEY_OP_NOMATCH) {
347 err("invalid SYSFS operation");
350 attr = get_key_attribute(key + sizeof("SYSFS")-1);
352 err("error parsing SYSFS attribute in '%s'", line);
355 add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
360 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
361 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
366 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
367 attr = get_key_attribute(key + sizeof("ENV")-1);
369 err("error parsing ENV attribute");
372 add_rule_key_pair(rule, &rule->env, operation, attr, value);
377 if (strcasecmp(key, "MODALIAS") == 0) {
378 add_rule_key(rule, &rule->modalias, operation, value);
383 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
384 attr = get_key_attribute(key + sizeof("IMPORT")-1);
385 if (attr && strstr(attr, "program")) {
386 dbg("IMPORT will be executed");
387 rule->import_type = IMPORT_PROGRAM;
388 } else if (attr && strstr(attr, "file")) {
389 dbg("IMPORT will be included as file");
390 rule->import_type = IMPORT_FILE;
391 } else if (attr && strstr(attr, "parent")) {
392 dbg("IMPORT will include the parent values");
393 rule->import_type = IMPORT_PARENT;
395 /* figure it out if it is executable */
396 char file[PATH_SIZE];
400 strlcpy(file, value, sizeof(file));
401 pos = strchr(file, ' ');
405 /* allow programs in /lib/udev called without the path */
406 if (strchr(file, '/') == NULL) {
407 strlcpy(file, "/lib/udev/", sizeof(file));
408 strlcat(file, value, sizeof(file));
409 pos = strchr(file, ' ');
414 dbg("IMPORT auto mode for '%s'", file);
415 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
416 dbg("IMPORT is executable, will be executed (autotype)");
417 rule->import_type = IMPORT_PROGRAM;
419 dbg("IMPORT is not executable, will be included as file (autotype)");
420 rule->import_type = IMPORT_FILE;
423 add_rule_key(rule, &rule->import, operation, value);
428 if (strcasecmp(key, "DRIVER") == 0) {
429 if (operation != KEY_OP_MATCH &&
430 operation != KEY_OP_NOMATCH) {
431 err("invalid DRIVER operation");
434 add_rule_key(rule, &rule->driver, operation, value);
439 if (strcasecmp(key, "RESULT") == 0) {
440 if (operation != KEY_OP_MATCH &&
441 operation != KEY_OP_NOMATCH) {
442 err("invalid RESULT operation");
445 add_rule_key(rule, &rule->result, operation, value);
450 if (strcasecmp(key, "PROGRAM") == 0) {
451 add_rule_key(rule, &rule->program, operation, value);
456 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
457 attr = get_key_attribute(key + sizeof("NAME")-1);
459 if (strstr(attr, "all_partitions") != NULL) {
460 dbg("creation of partition nodes requested");
461 rule->partitions = DEFAULT_PARTITIONS_COUNT;
463 if (strstr(attr, "ignore_remove") != NULL) {
464 dbg("remove event should be ignored");
465 rule->ignore_remove = 1;
468 if (value[0] == '\0')
469 dbg("name empty, node creation supressed");
470 add_rule_key(rule, &rule->name, operation, value);
474 if (strcasecmp(key, "SYMLINK") == 0) {
475 add_rule_key(rule, &rule->symlink, operation, value);
480 if (strcasecmp(key, "OWNER") == 0) {
482 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
484 strtoul(value, &endptr, 10);
485 if (endptr[0] != '\0') {
487 uid_t uid = lookup_user(value);
488 dbg("replacing username='%s' by id=%i", value, uid);
489 sprintf(owner, "%u", (unsigned int) uid);
490 add_rule_key(rule, &rule->owner, operation, owner);
495 add_rule_key(rule, &rule->owner, operation, value);
499 if (strcasecmp(key, "GROUP") == 0) {
501 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
503 strtoul(value, &endptr, 10);
504 if (endptr[0] != '\0') {
506 gid_t gid = lookup_group(value);
507 dbg("replacing groupname='%s' by id=%i", value, gid);
508 sprintf(group, "%u", (unsigned int) gid);
509 add_rule_key(rule, &rule->group, operation, group);
514 add_rule_key(rule, &rule->group, operation, value);
518 if (strcasecmp(key, "MODE") == 0) {
519 rule->mode = strtol(value, NULL, 8);
520 rule->mode_operation = operation;
525 if (strcasecmp(key, "RUN") == 0) {
526 add_rule_key(rule, &rule->run, operation, value);
531 if (strcasecmp(key, "OPTIONS") == 0) {
532 if (strstr(value, "last_rule") != NULL) {
533 dbg("last rule to be applied");
536 if (strstr(value, "ignore_device") != NULL) {
537 dbg("device should be ignored");
538 rule->ignore_device = 1;
540 if (strstr(value, "ignore_remove") != NULL) {
541 dbg("remove event should be ignored");
542 rule->ignore_remove = 1;
544 if (strstr(value, "all_partitions") != NULL) {
545 dbg("creation of partition nodes requested");
546 rule->partitions = DEFAULT_PARTITIONS_COUNT;
552 err("unknown key '%s'", key);
555 /* skip line if not any valid key was found */
559 /* grow buffer and add rule */
560 rule_size = sizeof(struct udev_rule) + rule->bufsize;
561 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
562 dbg("add %zi padding bytes", padding);
563 rule_size += padding;
564 rule->bufsize += padding;
566 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
568 err("realloc failed");
571 dbg("adding rule to offset %zi", rules->bufsize);
572 memcpy(rules->buf + rules->bufsize, rule, rule_size);
573 rules->bufsize += rule_size;
580 err("invalid rule '%s:%u'", filename, lineno);
584 static int parse_file(struct udev_rules *rules, const char *filename)
586 char line[LINE_SIZE];
595 if (file_map(filename, &buf, &bufsize) != 0) {
596 err("can't open '%s' as rules file: %s", filename, strerror(errno));
599 dbg("reading '%s' as rules file", filename);
601 /* loop through the whole file */
604 while (cur < bufsize) {
607 count = buf_get_line(buf, bufsize, cur);
612 if (count >= sizeof(line)) {
613 err("line too long, rule skipped '%s:%u'", filename, lineno);
617 /* eat the whitespace */
618 while ((count > 0) && isspace(bufline[0])) {
625 /* see if this is a comment */
626 if (bufline[0] == COMMENT_CHARACTER)
629 /* skip backslash and newline from multi line rules */
630 for (i = j = 0; i < count; i++) {
631 if (bufline[i] == '\\' && bufline[i+1] == '\n')
634 line[j++] = bufline[i];
638 dbg("read '%s'", line);
639 add_to_rules(rules, line, filename, lineno);
642 file_unmap(buf, bufsize);
646 int udev_rules_init(struct udev_rules *rules, int resolve_names)
651 memset(rules, 0x00, sizeof(struct udev_rules));
652 rules->resolve_names = resolve_names;
654 /* parse rules file or all matching files in directory */
655 if (stat(udev_rules_filename, &stats) != 0)
658 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
659 dbg("parse single rules file '%s'", udev_rules_filename);
660 retval = parse_file(rules, udev_rules_filename);
662 struct name_entry *name_loop, *name_tmp;
663 LIST_HEAD(name_list);
665 dbg("parse rules directory '%s'", udev_rules_filename);
666 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
668 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
669 if (stat(name_loop->name, &stats) == 0) {
671 parse_file(rules, name_loop->name);
673 dbg("empty rules file '%s'", name_loop->name);
675 err("could not read '%s': %s", name_loop->name, strerror(errno));
676 list_del(&name_loop->node);
684 void udev_rules_cleanup(struct udev_rules *rules)