2 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include "udev_rules.h"
33 void udev_rules_iter_init(struct udev_rules *rules)
35 dbg("bufsize=%zi", rules->bufsize);
39 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
41 static struct udev_rule *rule;
46 dbg("current=%zi", rules->current);
47 if (rules->current >= rules->bufsize) {
53 rule = (struct udev_rule *) (rules->buf + rules->current);
54 rules->current += sizeof(struct udev_rule) + rule->bufsize;
59 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
61 static struct udev_rule *rule;
64 dbg("current=%zi", rules->current);
65 if (rules->current >= rules->bufsize) {
69 rule = (struct udev_rule *) (rules->buf + rules->current);
71 if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
72 dbg("moving forward, looking for label '%s'", label);
73 rules->current += sizeof(struct udev_rule) + rule->bufsize;
77 dbg("found label '%s'", label);
81 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
87 if (linepos == NULL && linepos[0] == '\0')
91 while (isspace(linepos[0]) || linepos[0] == ',')
95 if (linepos[0] == '\0')
101 if (linepos[0] == '\0')
103 if (isspace(linepos[0]))
105 if (linepos[0] == '=')
107 if (linepos[0] == '+')
109 if (linepos[0] == '!')
111 if (linepos[0] == ':')
115 /* remember end of key */
118 /* skip whitespace after key */
119 while (isspace(linepos[0]))
121 if (linepos[0] == '\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]))
155 if (linepos[0] == '\0')
159 if (linepos[0] == '"')
165 temp = strchr(linepos, '"');
170 dbg("value='%s'", *value);
172 /* move line to next key */
178 /* extract possible KEY{attr} */
179 static char *get_key_attribute(char *str)
184 attr = strchr(str, '{');
187 pos = strchr(attr, '}');
189 err("missing closing brace for format");
193 dbg("attribute='%s'", attr);
200 static int add_rule_key(struct udev_rule *rule, struct key *key,
201 enum key_operation operation, const char *value)
203 size_t val_len = strnlen(value, PATH_SIZE);
205 key->operation = operation;
207 key->val_off = rule->bufsize;
208 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
209 rule->bufsize += val_len+1;
214 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
215 enum key_operation operation, const char *key, const char *value)
217 size_t key_len = strnlen(key, PATH_SIZE);
219 if (pairs->count >= PAIRS_MAX) {
220 err("skip, too many keys of the same type in a single rule");
224 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
226 /* add the key-name of the pair */
227 pairs->keys[pairs->count].key_name_off = rule->bufsize;
228 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
229 rule->bufsize += key_len+1;
236 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
238 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, "ACTION") == 0) {
265 if (operation != KEY_OP_MATCH &&
266 operation != KEY_OP_NOMATCH) {
267 err("invalid ACTION operation");
270 add_rule_key(rule, &rule->action, operation, value);
275 if (strcasecmp(key, "DEVPATH") == 0) {
276 if (operation != KEY_OP_MATCH &&
277 operation != KEY_OP_NOMATCH) {
278 err("invalid DEVPATH operation");
281 add_rule_key(rule, &rule->devpath, operation, value);
286 if (strcasecmp(key, "KERNEL") == 0) {
287 if (operation != KEY_OP_MATCH &&
288 operation != KEY_OP_NOMATCH) {
289 err("invalid KERNEL operation");
292 add_rule_key(rule, &rule->kernel, operation, value);
297 if (strcasecmp(key, "SUBSYSTEM") == 0) {
298 if (operation != KEY_OP_MATCH &&
299 operation != KEY_OP_NOMATCH) {
300 err("invalid SUBSYSTEM operation");
303 add_rule_key(rule, &rule->subsystem, operation, value);
308 if (strcasecmp(key, "DRIVER") == 0) {
309 if (operation != KEY_OP_MATCH &&
310 operation != KEY_OP_NOMATCH) {
311 err("invalid DRIVER operation");
314 err("DRIVER== will change in a future relase, "
315 "please use DRIVERS== in %s:%u", filename, lineno);
316 /* FIXME: this should be rule->driver to match only the event device */
317 add_rule_key(rule, &rule->drivers, operation, value);
322 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
323 attr = get_key_attribute(key + sizeof("ATTR")-1);
325 err("error parsing ATTR attribute");
328 if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
334 if (strcasecmp(key, "KERNELS") == 0 ||
335 strcasecmp(key, "ID") == 0) {
336 if (operation != KEY_OP_MATCH &&
337 operation != KEY_OP_NOMATCH) {
338 err("invalid KERNELS operation");
341 add_rule_key(rule, &rule->kernels, operation, value);
346 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
347 strcasecmp(key, "BUS") == 0) {
348 if (operation != KEY_OP_MATCH &&
349 operation != KEY_OP_NOMATCH) {
350 err("invalid SUBSYSTEMS operation");
353 add_rule_key(rule, &rule->subsystems, operation, value);
358 if (strcasecmp(key, "DRIVERS") == 0) {
359 if (operation != KEY_OP_MATCH &&
360 operation != KEY_OP_NOMATCH) {
361 err("invalid DRIVERS operation");
364 add_rule_key(rule, &rule->drivers, operation, value);
369 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
370 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
371 attr = get_key_attribute(key + sizeof("ATTRS")-1);
373 err("error parsing ATTRS attribute");
376 if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
382 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
383 attr = get_key_attribute(key + sizeof("ENV")-1);
385 err("error parsing ENV attribute");
388 if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
394 if (strcasecmp(key, "PROGRAM") == 0) {
395 add_rule_key(rule, &rule->program, operation, value);
400 if (strcasecmp(key, "RESULT") == 0) {
401 if (operation != KEY_OP_MATCH &&
402 operation != KEY_OP_NOMATCH) {
403 err("invalid RESULT operation");
406 add_rule_key(rule, &rule->result, operation, value);
411 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
412 attr = get_key_attribute(key + sizeof("IMPORT")-1);
413 if (attr && strstr(attr, "program")) {
414 dbg("IMPORT will be executed");
415 rule->import_type = IMPORT_PROGRAM;
416 } else if (attr && strstr(attr, "file")) {
417 dbg("IMPORT will be included as file");
418 rule->import_type = IMPORT_FILE;
419 } else if (attr && strstr(attr, "parent")) {
420 dbg("IMPORT will include the parent values");
421 rule->import_type = IMPORT_PARENT;
423 /* figure it out if it is executable */
424 char file[PATH_SIZE];
428 strlcpy(file, value, sizeof(file));
429 pos = strchr(file, ' ');
433 /* allow programs in /lib/udev called without the path */
434 if (strchr(file, '/') == NULL) {
435 strlcpy(file, "/lib/udev/", sizeof(file));
436 strlcat(file, value, sizeof(file));
437 pos = strchr(file, ' ');
442 dbg("IMPORT auto mode for '%s'", file);
443 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
444 dbg("IMPORT is executable, will be executed (autotype)");
445 rule->import_type = IMPORT_PROGRAM;
447 dbg("IMPORT is not executable, will be included as file (autotype)");
448 rule->import_type = IMPORT_FILE;
451 add_rule_key(rule, &rule->import, operation, value);
456 if (strcasecmp(key, "RUN") == 0) {
457 add_rule_key(rule, &rule->run, operation, value);
462 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
463 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
468 if (strcasecmp(key, "LABEL") == 0) {
469 add_rule_key(rule, &rule->label, operation, value);
474 if (strcasecmp(key, "GOTO") == 0) {
475 add_rule_key(rule, &rule->goto_label, operation, value);
480 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
481 attr = get_key_attribute(key + sizeof("NAME")-1);
483 if (strstr(attr, "all_partitions") != NULL) {
484 dbg("creation of partition nodes requested");
485 rule->partitions = DEFAULT_PARTITIONS_COUNT;
487 if (strstr(attr, "ignore_remove") != NULL) {
488 dbg("remove event should be ignored");
489 rule->ignore_remove = 1;
492 if (value[0] == '\0')
493 dbg("name empty, node creation supressed");
494 add_rule_key(rule, &rule->name, operation, value);
498 if (strcasecmp(key, "SYMLINK") == 0) {
499 add_rule_key(rule, &rule->symlink, operation, value);
504 if (strcasecmp(key, "OWNER") == 0) {
506 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
508 strtoul(value, &endptr, 10);
509 if (endptr[0] != '\0') {
511 uid_t uid = lookup_user(value);
512 dbg("replacing username='%s' by id=%i", value, uid);
513 sprintf(owner, "%u", (unsigned int) uid);
514 add_rule_key(rule, &rule->owner, operation, owner);
519 add_rule_key(rule, &rule->owner, operation, value);
523 if (strcasecmp(key, "GROUP") == 0) {
525 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
527 strtoul(value, &endptr, 10);
528 if (endptr[0] != '\0') {
530 gid_t gid = lookup_group(value);
531 dbg("replacing groupname='%s' by id=%i", value, gid);
532 sprintf(group, "%u", (unsigned int) gid);
533 add_rule_key(rule, &rule->group, operation, group);
538 add_rule_key(rule, &rule->group, operation, value);
542 if (strcasecmp(key, "MODE") == 0) {
543 rule->mode = strtol(value, NULL, 8);
544 rule->mode_operation = operation;
549 if (strcasecmp(key, "OPTIONS") == 0) {
550 if (strstr(value, "last_rule") != NULL) {
551 dbg("last rule to be applied");
554 if (strstr(value, "ignore_device") != NULL) {
555 dbg("device should be ignored");
556 rule->ignore_device = 1;
558 if (strstr(value, "ignore_remove") != NULL) {
559 dbg("remove event should be ignored");
560 rule->ignore_remove = 1;
562 if (strstr(value, "all_partitions") != NULL) {
563 dbg("creation of partition nodes requested");
564 rule->partitions = DEFAULT_PARTITIONS_COUNT;
570 err("unknown key '%s' in %s:%u", key, filename, lineno);
573 /* skip line if not any valid key was found */
577 /* grow buffer and add rule */
578 rule_size = sizeof(struct udev_rule) + rule->bufsize;
579 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
580 dbg("add %zi padding bytes", padding);
581 rule_size += padding;
582 rule->bufsize += padding;
584 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
586 err("realloc failed");
589 dbg("adding rule to offset %zi", rules->bufsize);
590 memcpy(rules->buf + rules->bufsize, rule, rule_size);
591 rules->bufsize += rule_size;
598 err("invalid rule '%s:%u'", filename, lineno);
602 static int parse_file(struct udev_rules *rules, const char *filename)
604 char line[LINE_SIZE];
613 if (file_map(filename, &buf, &bufsize) != 0) {
614 err("can't open '%s' as rules file: %s", filename, strerror(errno));
617 dbg("reading '%s' as rules file", filename);
619 /* loop through the whole file */
622 while (cur < bufsize) {
625 count = buf_get_line(buf, bufsize, cur);
630 /* eat the whitespace */
631 while ((count > 0) && isspace(bufline[0])) {
638 /* see if this is a comment */
639 if (bufline[0] == COMMENT_CHARACTER)
642 if (count >= sizeof(line)) {
643 err("line too long, rule skipped '%s:%u'", filename, lineno);
647 /* skip backslash and newline from multiline rules */
648 for (i = j = 0; i < count; i++) {
649 if (bufline[i] == '\\' && bufline[i+1] == '\n')
652 line[j++] = bufline[i];
656 dbg("read '%s'", line);
657 add_to_rules(rules, line, filename, lineno);
660 file_unmap(buf, bufsize);
664 int udev_rules_init(struct udev_rules *rules, int resolve_names)
669 memset(rules, 0x00, sizeof(struct udev_rules));
670 rules->resolve_names = resolve_names;
672 /* parse rules file or all matching files in directory */
673 if (stat(udev_rules_filename, &stats) != 0)
676 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
677 dbg("parse single rules file '%s'", udev_rules_filename);
678 retval = parse_file(rules, udev_rules_filename);
680 struct name_entry *name_loop, *name_tmp;
681 LIST_HEAD(name_list);
683 dbg("parse rules directory '%s'", udev_rules_filename);
684 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
686 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
687 if (stat(name_loop->name, &stats) == 0) {
689 parse_file(rules, name_loop->name);
691 dbg("empty rules file '%s'", name_loop->name);
693 err("could not read '%s': %s", name_loop->name, strerror(errno));
694 list_del(&name_loop->node);
702 void udev_rules_cleanup(struct udev_rules *rules)