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)
59 rule = (struct udev_rule *) (rules->buf + rules->current);
60 rules->current += sizeof(struct udev_rule) + rule->bufsize;
65 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
75 while (isspace(linepos[0]) || linepos[0] == ',')
82 if (linepos[0] == '\0')
84 if (isspace(linepos[0]))
86 if (linepos[0] == '=')
88 if (linepos[0] == '+')
90 if (linepos[0] == '!')
92 if (linepos[0] == ':')
96 /* remember end of key */
99 /* skip whitespace after key */
100 while (isspace(linepos[0]))
103 /* get operation type */
104 if (linepos[0] == '=' && linepos[1] == '=') {
105 *operation = KEY_OP_MATCH;
107 dbg("operator=match");
108 } else if (linepos[0] == '!' && linepos[1] == '=') {
109 *operation = KEY_OP_NOMATCH;
111 dbg("operator=nomatch");
112 } else if (linepos[0] == '+' && linepos[1] == '=') {
113 *operation = KEY_OP_ADD;
116 } else if (linepos[0] == '=') {
117 *operation = KEY_OP_ASSIGN;
119 dbg("operator=assign");
120 } else if (linepos[0] == ':' && linepos[1] == '=') {
121 *operation = KEY_OP_ASSIGN_FINAL;
123 dbg("operator=assign_final");
129 dbg("key='%s'", *key);
131 /* skip whitespace after operator */
132 while (isspace(linepos[0]))
136 if (linepos[0] == '"')
142 temp = strchr(linepos, '"');
147 dbg("value='%s'", *value);
149 /* move line to next key */
155 /* extract possible KEY{attr} */
156 static char *get_key_attribute(char *str)
161 attr = strchr(str, '{');
164 pos = strchr(attr, '}');
166 err("missing closing brace for format");
170 dbg("attribute='%s'", attr);
177 static int add_rule_key(struct udev_rule *rule, struct key *key,
178 enum key_operation operation, const char *value)
180 size_t val_len = strnlen(value, PATH_SIZE);
182 key->operation = operation;
184 key->val_off = rule->bufsize;
185 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
186 rule->bufsize += val_len+1;
191 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
192 enum key_operation operation, const char *key, const char *value)
194 size_t key_len = strnlen(key, PATH_SIZE);
196 if (pairs->count >= PAIRS_MAX) {
197 err("skip, too many keys in a single rule");
201 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
203 /* add the key-name of the pair */
204 pairs->keys[pairs->count].key_name_off = rule->bufsize;
205 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
206 rule->bufsize += key_len+1;
213 static int add_to_rules(struct udev_rules *rules, char *line)
215 struct udev_rule *rule;
222 /* get all the keys */
223 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
225 err("malloc failed");
234 enum key_operation operation = KEY_OP_UNSET;
236 retval = get_key(&linepos, &key, &operation, &value);
240 if (strcasecmp(key, "KERNEL") == 0) {
241 add_rule_key(rule, &rule->kernel_name, operation, value);
246 if (strcasecmp(key, "SUBSYSTEM") == 0) {
247 add_rule_key(rule, &rule->subsystem, operation, value);
252 if (strcasecmp(key, "ACTION") == 0) {
253 add_rule_key(rule, &rule->action, operation, value);
258 if (strcasecmp(key, "DEVPATH") == 0) {
259 add_rule_key(rule, &rule->devpath, operation, value);
264 if (strcasecmp(key, "BUS") == 0) {
265 add_rule_key(rule, &rule->bus, operation, value);
270 if (strcasecmp(key, "ID") == 0) {
271 add_rule_key(rule, &rule->id, operation, value);
276 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
277 attr = get_key_attribute(key + sizeof("SYSFS")-1);
279 err("error parsing SYSFS attribute in '%s'", line);
282 add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
287 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
288 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
293 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
294 attr = get_key_attribute(key + sizeof("ENV")-1);
296 err("error parsing ENV attribute");
299 add_rule_key_pair(rule, &rule->env, operation, attr, value);
304 if (strcasecmp(key, "MODALIAS") == 0) {
305 add_rule_key(rule, &rule->modalias, operation, value);
310 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
311 attr = get_key_attribute(key + sizeof("IMPORT")-1);
312 if (attr && strstr(attr, "program")) {
313 dbg("IMPORT will be executed");
314 rule->import_type = IMPORT_PROGRAM;
315 } else if (attr && strstr(attr, "file")) {
316 dbg("IMPORT will be included as file");
317 rule->import_type = IMPORT_FILE;
318 } else if (attr && strstr(attr, "parent")) {
319 dbg("IMPORT will include the parent values");
320 rule->import_type = IMPORT_PARENT;
322 /* figure it out if it is executable */
323 char file[PATH_SIZE];
327 strlcpy(file, value, sizeof(file));
328 pos = strchr(file, ' ');
331 dbg("IMPORT auto mode for '%s'", file);
332 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
333 dbg("IMPORT is executable, will be executed (autotype)");
334 rule->import_type = IMPORT_PROGRAM;
336 dbg("IMPORT is not executable, will be included as file (autotype)");
337 rule->import_type = IMPORT_FILE;
340 add_rule_key(rule, &rule->import, operation, value);
345 if (strcasecmp(key, "DRIVER") == 0) {
346 add_rule_key(rule, &rule->driver, operation, value);
351 if (strcasecmp(key, "RESULT") == 0) {
352 add_rule_key(rule, &rule->result, operation, value);
357 if (strcasecmp(key, "PROGRAM") == 0) {
358 add_rule_key(rule, &rule->program, operation, value);
363 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
364 attr = get_key_attribute(key + sizeof("NAME")-1);
366 if (strstr(attr, "all_partitions") != NULL) {
367 dbg("creation of partition nodes requested");
368 rule->partitions = DEFAULT_PARTITIONS_COUNT;
370 if (strstr(attr, "ignore_remove") != NULL) {
371 dbg("remove event should be ignored");
372 rule->ignore_remove = 1;
375 if (value[0] == '\0') {
376 dbg("name empty device should be ignored");
377 rule->name.operation = operation;
378 rule->ignore_device = 1;
380 add_rule_key(rule, &rule->name, operation, value);
384 if (strcasecmp(key, "SYMLINK") == 0) {
385 add_rule_key(rule, &rule->symlink, operation, value);
390 if (strcasecmp(key, "OWNER") == 0) {
392 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
394 strtoul(value, &endptr, 10);
395 if (endptr[0] != '\0') {
397 uid_t uid = lookup_user(value);
398 dbg("replacing username='%s' by id=%i", value, uid);
399 sprintf(owner, "%li", uid);
400 add_rule_key(rule, &rule->owner, operation, owner);
405 add_rule_key(rule, &rule->owner, operation, value);
409 if (strcasecmp(key, "GROUP") == 0) {
411 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
413 strtoul(value, &endptr, 10);
414 if (endptr[0] != '\0') {
416 gid_t gid = lookup_group(value);
417 dbg("replacing groupname='%s' by id=%i", value, gid);
418 sprintf(group, "%li", gid);
419 add_rule_key(rule, &rule->group, operation, group);
424 add_rule_key(rule, &rule->group, operation, value);
428 if (strcasecmp(key, "MODE") == 0) {
429 rule->mode = strtol(value, NULL, 8);
430 rule->mode_operation = operation;
435 if (strcasecmp(key, "RUN") == 0) {
436 add_rule_key(rule, &rule->run, operation, value);
441 if (strcasecmp(key, "OPTIONS") == 0) {
442 if (strstr(value, "last_rule") != NULL) {
443 dbg("last rule to be applied");
446 if (strstr(value, "ignore_device") != NULL) {
447 dbg("device should be ignored");
448 rule->ignore_device = 1;
450 if (strstr(value, "ignore_remove") != NULL) {
451 dbg("remove event should be ignored");
452 rule->ignore_remove = 1;
454 if (strstr(value, "all_partitions") != NULL) {
455 dbg("creation of partition nodes requested");
456 rule->partitions = DEFAULT_PARTITIONS_COUNT;
462 err("unknown key '%s', in '%s'", key, line);
465 /* skip line if not any valid key was found */
467 err("invalid rule '%s'", line);
471 /* grow buffer and add rule */
472 rule_size = sizeof(struct udev_rule) + rule->bufsize;
473 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
475 err("realloc failed");
478 memcpy(rules->buf + rules->bufsize, rule, rule_size);
479 rules->bufsize += rule_size;
485 static int parse_file(struct udev_rules *rules, const char *filename)
487 char line[LINE_SIZE];
496 if (file_map(filename, &buf, &bufsize) != 0) {
497 err("can't open '%s' as rules file", filename);
500 dbg("reading '%s' as rules file", filename);
502 /* loop through the whole file */
505 while (cur < bufsize) {
508 count = buf_get_line(buf, bufsize, cur);
513 if (count >= sizeof(line)) {
514 info("line too long, rule skipped %s, line %d", filename, lineno);
518 /* eat the whitespace */
519 while ((count > 0) && isspace(bufline[0])) {
526 /* see if this is a comment */
527 if (bufline[0] == COMMENT_CHARACTER)
530 /* skip backslash and newline from multi line rules */
531 for (i = j = 0; i < count; i++) {
532 if (bufline[i] == '\\' && bufline[i+1] == '\n')
535 line[j++] = bufline[i];
539 dbg("read '%s'", line);
540 add_to_rules(rules, line);
543 file_unmap(buf, bufsize);
547 static int rules_map(struct udev_rules *rules, const char *filename)
549 if (file_map(filename, &rules->buf, &rules->bufsize)) {
553 if (rules->bufsize == 0) {
554 file_unmap(rules->buf, rules->bufsize);
563 int udev_rules_init(struct udev_rules *rules, int resolve_names)
565 char comp[PATH_SIZE];
569 memset(rules, 0x00, sizeof(struct udev_rules));
570 rules->resolve_names = resolve_names;
572 /* check for precompiled rules */
573 strlcpy(comp, udev_rules_filename, sizeof(comp));
574 strlcat(comp, ".compiled", sizeof(comp));
575 if (stat(comp, &stats) == 0) {
576 dbg("map compiled rules '%s'", comp);
577 if (rules_map(rules, comp) == 0)
581 if (stat(udev_rules_filename, &stats) != 0)
584 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
585 dbg("parse single rules file '%s'", udev_rules_filename);
586 retval = parse_file(rules, udev_rules_filename);
588 struct name_entry *name_loop, *name_tmp;
589 LIST_HEAD(name_list);
591 dbg("parse rules directory '%s'", udev_rules_filename);
592 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
594 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
595 parse_file(rules, name_loop->name);
596 list_del(&name_loop->node);
603 void udev_rules_close(struct udev_rules *rules)
606 file_unmap(rules->buf, rules->bufsize);