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_exec = 1;
315 } else if (attr && strstr(attr, "file")) {
316 dbg("IMPORT will be included as file");
318 /* figure it out if it is executable */
319 char file[PATH_SIZE];
323 strlcpy(file, value, sizeof(file));
324 pos = strchr(file, ' ');
327 dbg("IMPORT auto mode for '%s'", file);
328 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
329 dbg("IMPORT is executable, will be executed");
330 rule->import_exec = 1;
333 add_rule_key(rule, &rule->import, operation, value);
338 if (strcasecmp(key, "DRIVER") == 0) {
339 add_rule_key(rule, &rule->driver, operation, value);
344 if (strcasecmp(key, "RESULT") == 0) {
345 add_rule_key(rule, &rule->result, operation, value);
350 if (strcasecmp(key, "PROGRAM") == 0) {
351 add_rule_key(rule, &rule->program, operation, value);
356 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
357 attr = get_key_attribute(key + sizeof("NAME")-1);
359 if (strstr(attr, "all_partitions") != NULL) {
360 dbg("creation of partition nodes requested");
361 rule->partitions = DEFAULT_PARTITIONS_COUNT;
363 if (strstr(attr, "ignore_remove") != NULL) {
364 dbg("remove event should be ignored");
365 rule->ignore_remove = 1;
368 if (value[0] == '\0') {
369 dbg("name empty device should be ignored");
370 rule->name.operation = operation;
371 rule->ignore_device = 1;
373 add_rule_key(rule, &rule->name, operation, value);
377 if (strcasecmp(key, "SYMLINK") == 0) {
378 add_rule_key(rule, &rule->symlink, operation, value);
383 if (strcasecmp(key, "OWNER") == 0) {
385 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
387 strtoul(value, &endptr, 10);
388 if (endptr[0] != '\0') {
390 uid_t uid = lookup_user(value);
391 dbg("replacing username='%s' by id=%i", value, uid);
392 sprintf(owner, "%li", uid);
393 add_rule_key(rule, &rule->owner, operation, owner);
398 add_rule_key(rule, &rule->owner, operation, value);
402 if (strcasecmp(key, "GROUP") == 0) {
404 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
406 strtoul(value, &endptr, 10);
407 if (endptr[0] != '\0') {
409 gid_t gid = lookup_group(value);
410 dbg("replacing groupname='%s' by id=%i", value, gid);
411 sprintf(group, "%li", gid);
412 add_rule_key(rule, &rule->group, operation, group);
417 add_rule_key(rule, &rule->group, operation, value);
421 if (strcasecmp(key, "MODE") == 0) {
422 rule->mode = strtol(value, NULL, 8);
423 rule->mode_operation = operation;
428 if (strcasecmp(key, "RUN") == 0) {
429 add_rule_key(rule, &rule->run, operation, value);
434 if (strcasecmp(key, "OPTIONS") == 0) {
435 if (strstr(value, "last_rule") != NULL) {
436 dbg("last rule to be applied");
439 if (strstr(value, "ignore_device") != NULL) {
440 dbg("device should be ignored");
441 rule->ignore_device = 1;
443 if (strstr(value, "ignore_remove") != NULL) {
444 dbg("remove event should be ignored");
445 rule->ignore_remove = 1;
447 if (strstr(value, "all_partitions") != NULL) {
448 dbg("creation of partition nodes requested");
449 rule->partitions = DEFAULT_PARTITIONS_COUNT;
455 err("unknown key '%s', in '%s'", key, line);
458 /* skip line if not any valid key was found */
460 err("invalid rule '%s'", line);
464 /* grow buffer and add rule */
465 rule_size = sizeof(struct udev_rule) + rule->bufsize;
466 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
468 err("realloc failed");
471 memcpy(rules->buf + rules->bufsize, rule, rule_size);
472 rules->bufsize += rule_size;
478 static int parse_file(struct udev_rules *rules, const char *filename)
480 char line[LINE_SIZE];
489 if (file_map(filename, &buf, &bufsize) != 0) {
490 err("can't open '%s' as rules file", filename);
493 dbg("reading '%s' as rules file", filename);
495 /* loop through the whole file */
498 while (cur < bufsize) {
501 count = buf_get_line(buf, bufsize, cur);
506 if (count >= sizeof(line)) {
507 info("line too long, rule skipped %s, line %d", filename, lineno);
511 /* eat the whitespace */
512 while ((count > 0) && isspace(bufline[0])) {
519 /* see if this is a comment */
520 if (bufline[0] == COMMENT_CHARACTER)
523 /* skip backslash and newline from multi line rules */
524 for (i = j = 0; i < count; i++) {
525 if (bufline[i] == '\\' && bufline[i+1] == '\n')
528 line[j++] = bufline[i];
532 dbg("read '%s'", line);
533 add_to_rules(rules, line);
536 file_unmap(buf, bufsize);
540 static int rules_map(struct udev_rules *rules, const char *filename)
542 if (file_map(filename, &rules->buf, &rules->bufsize)) {
546 if (rules->bufsize == 0) {
547 file_unmap(rules->buf, rules->bufsize);
556 int udev_rules_init(struct udev_rules *rules, int resolve_names)
558 char comp[PATH_SIZE];
562 memset(rules, 0x00, sizeof(struct udev_rules));
563 rules->resolve_names = resolve_names;
565 /* check for precompiled rules */
566 strlcpy(comp, udev_rules_filename, sizeof(comp));
567 strlcat(comp, ".compiled", sizeof(comp));
568 if (stat(comp, &stats) == 0) {
569 dbg("map compiled rules '%s'", comp);
570 if (rules_map(rules, comp) == 0)
574 if (stat(udev_rules_filename, &stats) != 0)
577 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
578 dbg("parse single rules file '%s'", udev_rules_filename);
579 retval = parse_file(rules, udev_rules_filename);
581 struct name_entry *name_loop, *name_tmp;
582 LIST_HEAD(name_list);
584 dbg("parse rules directory '%s'", udev_rules_filename);
585 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
587 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
588 parse_file(rules, name_loop->name);
589 list_del(&name_loop->node);
596 void udev_rules_close(struct udev_rules *rules)
599 file_unmap(rules->buf, rules->bufsize);