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"
40 /* rules parsed from .rules files*/
41 static LIST_HEAD(rules_list);
42 static struct list_head *rules_list_current;
44 /* mapped compiled rules stored on disk */
45 static struct udev_rule *rules_array = NULL;
46 static size_t rules_array_current;
47 static size_t rules_array_size = 0;
49 static size_t rules_count = 0;
51 int udev_rules_iter_init(void)
53 rules_list_current = rules_list.next;
54 rules_array_current = 0;
59 struct udev_rule *udev_rules_iter_next(void)
61 static struct udev_rule *rule;
64 if (rules_array_current >= rules_count)
66 rule = &rules_array[rules_array_current];
67 rules_array_current++;
69 dbg("head=%p current=%p next=%p", &rules_list, rules_list_current, rules_list_current->next);
70 if (rules_list_current == &rules_list)
72 rule = list_entry(rules_list_current, struct udev_rule, node);
73 rules_list_current = rules_list_current->next;
78 static int add_rule_to_list(struct udev_rule *rule)
80 struct udev_rule *tmp_rule;
82 tmp_rule = malloc(sizeof(*tmp_rule));
85 memcpy(tmp_rule, rule, sizeof(struct udev_rule));
86 list_add_tail(&tmp_rule->node, &rules_list);
88 dbg("name='%s', symlink='%s', bus='%s', id='%s', "
89 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
90 "kernel_name='%s', program='%s', result='%s', "
91 "owner='%s', group='%s', mode=%#o, "
92 "all_partions=%u, ignore_remove=%u, ignore_device=%u, last_rule=%u",
93 rule->name, rule->symlink, rule->bus, rule->id,
94 rule->sysfs_pair[0].name, rule->sysfs_pair[0].value,
95 rule->kernel_name, rule->program, rule->result, rule->owner, rule->group, rule->mode,
96 rule->partitions, rule->ignore_remove, rule->ignore_device, rule->last_rule);
101 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
110 /* skip whitespace */
111 while (isspace(linepos[0]) || linepos[0] == ',')
118 if (linepos[0] == '\0')
120 if (isspace(linepos[0]))
122 if (linepos[0] == '=')
124 if (linepos[0] == '+')
126 if (linepos[0] == '!')
128 if (linepos[0] == ':')
132 /* remember end of key */
135 /* skip whitespace after key */
136 while (isspace(linepos[0]))
139 /* get operation type */
140 if (linepos[0] == '=' && linepos[1] == '=') {
141 *operation = KEY_OP_MATCH;
143 dbg("operator=match");
144 } else if (linepos[0] == '!' && linepos[1] == '=') {
145 *operation = KEY_OP_NOMATCH;
147 dbg("operator=nomatch");
148 } else if (linepos[0] == '+' && linepos[1] == '=') {
149 *operation = KEY_OP_ADD;
152 } else if (linepos[0] == '=') {
153 *operation = KEY_OP_ASSIGN;
155 dbg("operator=assign");
156 } else if (linepos[0] == ':' && linepos[1] == '=') {
157 *operation = KEY_OP_ASSIGN_FINAL;
159 dbg("operator=assign_final");
165 dbg("key='%s'", *key);
167 /* skip whitespace after operator */
168 while (isspace(linepos[0]))
172 if (linepos[0] == '"')
178 temp = strchr(linepos, '"');
183 dbg("value='%s'", *value);
185 /* move line to next key */
191 /* extract possible KEY{attr} */
192 static char *get_key_attribute(char *str)
197 attr = strchr(str, '{');
200 pos = strchr(attr, '}');
202 err("missing closing brace for format");
206 dbg("attribute='%s'", attr);
213 static int rules_parse(const char *filename)
215 char line[LINE_SIZE];
224 int program_given = 0;
227 struct udev_rule rule;
229 if (file_map(filename, &buf, &bufsize) != 0) {
230 err("can't open '%s' as rules file", filename);
233 dbg("reading '%s' as rules file", filename);
235 /* loop through the whole file */
238 while (cur < bufsize) {
241 count = buf_get_line(buf, bufsize, cur);
246 if (count >= sizeof(line)) {
247 info("line too long, rule skipped %s, line %d", filename, lineno);
251 /* eat the whitespace */
252 while ((count > 0) && isspace(bufline[0])) {
259 /* see if this is a comment */
260 if (bufline[0] == COMMENT_CHARACTER)
263 /* skip backslash and newline from multi line rules */
264 for (i = j = 0; i < count; i++) {
265 if (bufline[i] == '\\' && bufline[i+1] == '\n')
268 line[j++] = bufline[i];
271 dbg("read '%s'", line);
273 /* get all known keys */
274 memset(&rule, 0x00, sizeof(struct udev_rule));
281 enum key_operation operation = KEY_OP_UNSET;
283 retval = get_key(&linepos, &key, &operation, &value);
287 if (strcasecmp(key, KEY_KERNEL) == 0) {
288 strlcpy(rule.kernel_name, value, sizeof(rule.kernel_name));
289 rule.kernel_operation = operation;
294 if (strcasecmp(key, KEY_SUBSYSTEM) == 0) {
295 strlcpy(rule.subsystem, value, sizeof(rule.subsystem));
296 rule.subsystem_operation = operation;
301 if (strcasecmp(key, KEY_ACTION) == 0) {
302 strlcpy(rule.action, value, sizeof(rule.action));
303 rule.action_operation = operation;
308 if (strcasecmp(key, KEY_DEVPATH) == 0) {
309 strlcpy(rule.devpath, value, sizeof(rule.devpath));
310 rule.devpath_operation = operation;
315 if (strcasecmp(key, KEY_BUS) == 0) {
316 strlcpy(rule.bus, value, sizeof(rule.bus));
317 rule.bus_operation = operation;
322 if (strcasecmp(key, KEY_ID) == 0) {
323 strlcpy(rule.id, value, sizeof(rule.id));
324 rule.id_operation = operation;
329 if (strncasecmp(key, KEY_SYSFS, sizeof(KEY_SYSFS)-1) == 0) {
330 struct key_pair *pair;
332 if (rule.sysfs_pair_count >= KEY_SYSFS_PAIRS_MAX) {
333 err("skip rule, to many " KEY_SYSFS " keys in a single rule");
336 pair = &rule.sysfs_pair[rule.sysfs_pair_count];
337 attr = get_key_attribute(key + sizeof(KEY_SYSFS)-1);
339 err("error parsing " KEY_SYSFS " attribute");
342 strlcpy(pair->name, attr, sizeof(pair->name));
343 strlcpy(pair->value, value, sizeof(pair->value));
344 pair->operation = operation;
345 rule.sysfs_pair_count++;
350 if (strncasecmp(key, KEY_ENV, sizeof(KEY_ENV)-1) == 0) {
351 struct key_pair *pair;
353 if (rule.env_pair_count >= KEY_ENV_PAIRS_MAX) {
354 err("skip rule, to many " KEY_ENV " keys in a single rule");
357 pair = &rule.env_pair[rule.env_pair_count];
358 attr = get_key_attribute(key + sizeof(KEY_ENV)-1);
360 err("error parsing " KEY_ENV " attribute");
363 strlcpy(pair->name, attr, sizeof(pair->name));
364 strlcpy(pair->value, value, sizeof(pair->value));
365 pair->operation = operation;
366 rule.env_pair_count++;
371 if (strcasecmp(key, KEY_MODALIAS) == 0) {
372 strlcpy(rule.modalias, value, sizeof(rule.modalias));
373 rule.modalias_operation = operation;
378 if (strncasecmp(key, KEY_IMPORT, sizeof(KEY_IMPORT)-1) == 0) {
379 attr = get_key_attribute(key + sizeof(KEY_IMPORT)-1);
380 if (attr && strstr(attr, "program")) {
381 dbg(KEY_IMPORT" will be executed");
382 rule.import_exec = 1;
383 } else if (attr && strstr(attr, "file")) {
384 dbg(KEY_IMPORT" will be included as file");
386 /* figure it out if it is executable */
387 char file[PATH_SIZE];
391 strlcpy(file, value, sizeof(file));
392 pos = strchr(file, ' ');
395 dbg(KEY_IMPORT" auto mode for '%s'", file);
396 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
397 dbg(KEY_IMPORT" is executable, will be executed");
398 rule.import_exec = 1;
401 strlcpy(rule.import, value, sizeof(rule.import));
402 rule.import_operation = operation;
407 if (strcasecmp(key, KEY_DRIVER) == 0) {
408 strlcpy(rule.driver, value, sizeof(rule.driver));
409 rule.driver_operation = operation;
414 if (strcasecmp(key, KEY_RESULT) == 0) {
415 strlcpy(rule.result, value, sizeof(rule.result));
416 rule.result_operation = operation;
421 if (strcasecmp(key, KEY_PROGRAM) == 0) {
422 strlcpy(rule.program, value, sizeof(rule.program));
423 rule.program_operation = operation;
429 if (strncasecmp(key, KEY_NAME, sizeof(KEY_NAME)-1) == 0) {
430 attr = get_key_attribute(key + sizeof(KEY_NAME)-1);
432 if (strstr(attr, OPTION_PARTITIONS) != NULL) {
433 dbg("creation of partition nodes requested");
434 rule.partitions = DEFAULT_PARTITIONS_COUNT;
436 if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
437 dbg("remove event should be ignored");
438 rule.ignore_remove = 1;
441 rule.name_operation = operation;
442 strlcpy(rule.name, value, sizeof(rule.name));
447 if (strcasecmp(key, KEY_SYMLINK) == 0) {
448 strlcpy(rule.symlink, value, sizeof(rule.symlink));
449 rule.symlink_operation = operation;
454 if (strcasecmp(key, KEY_OWNER) == 0) {
455 strlcpy(rule.owner, value, sizeof(rule.owner));
456 rule.owner_operation = operation;
461 if (strcasecmp(key, KEY_GROUP) == 0) {
462 strlcpy(rule.group, value, sizeof(rule.group));
463 rule.group_operation = operation;
468 if (strcasecmp(key, KEY_MODE) == 0) {
469 rule.mode = strtol(value, NULL, 8);
470 rule.mode_operation = operation;
475 if (strcasecmp(key, KEY_RUN) == 0) {
476 strlcpy(rule.run, value, sizeof(rule.run));
477 rule.run_operation = operation;
482 if (strcasecmp(key, KEY_OPTIONS) == 0) {
483 if (strstr(value, OPTION_LAST_RULE) != NULL) {
484 dbg("last rule to be applied");
487 if (strstr(value, OPTION_IGNORE_DEVICE) != NULL) {
488 dbg("device should be ignored");
489 rule.ignore_device = 1;
491 if (strstr(value, OPTION_IGNORE_REMOVE) != NULL) {
492 dbg("remove event should be ignored");
493 rule.ignore_remove = 1;
495 if (strstr(value, OPTION_PARTITIONS) != NULL) {
496 dbg("creation of partition nodes requested");
497 rule.partitions = DEFAULT_PARTITIONS_COUNT;
503 err("unknown key '%s'", key);
507 /* skip line if not any valid key was found */
511 if ((rule.result[0] != '\0') && (program_given == 0)) {
512 info(KEY_RESULT " is only useful when " KEY_PROGRAM " is called in any rule before");
516 rule.config_line = lineno;
517 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
518 retval = add_rule_to_list(&rule);
520 dbg("add_rule_to_list returned with error %d", retval);
523 err("parse error %s, line %d:%d, rule skipped",
524 filename, lineno, (int) (linepos - line));
528 file_unmap(buf, bufsize);
532 static int rules_map(const char *filename)
537 if (file_map(filename, &buf, &size))
541 rules_array = (struct udev_rule *) buf;
542 rules_array_size = size;
543 rules_count = size / sizeof(struct udev_rule);
544 dbg("found %zi compiled rules", rules_count);
549 int udev_rules_init(void)
551 char comp[PATH_SIZE];
555 strlcpy(comp, udev_rules_filename, sizeof(comp));
556 strlcat(comp, ".compiled", sizeof(comp));
557 if (stat(comp, &stats) == 0) {
558 dbg("parse compiled rules '%s'", comp);
559 return rules_map(comp);
562 if (stat(udev_rules_filename, &stats) != 0)
565 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
566 dbg("parse single rules file '%s'", udev_rules_filename);
567 retval = rules_parse(udev_rules_filename);
569 struct name_entry *name_loop, *name_tmp;
570 LIST_HEAD(name_list);
572 dbg("parse rules directory '%s'", udev_rules_filename);
573 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
575 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
576 rules_parse(name_loop->name);
577 list_del(&name_loop->node);
584 void udev_rules_close(void)
586 struct udev_rule *rule;
587 struct udev_rule *temp_rule;
590 file_unmap(rules_array, rules_array_size);
592 list_for_each_entry_safe(rule, temp_rule, &rules_list, node) {
593 list_del(&rule->node);