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='%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, 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, value, sizeof(rule.kernel));
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 (strcasecmp(key, KEY_IMPORT) == 0) {
379 strlcpy(rule.import, value, sizeof(rule.import));
380 rule.import_operation = operation;
385 if (strcasecmp(key, KEY_DRIVER) == 0) {
386 strlcpy(rule.driver, value, sizeof(rule.driver));
387 rule.driver_operation = operation;
392 if (strcasecmp(key, KEY_RESULT) == 0) {
393 strlcpy(rule.result, value, sizeof(rule.result));
394 rule.result_operation = operation;
399 if (strcasecmp(key, KEY_PROGRAM) == 0) {
400 strlcpy(rule.program, value, sizeof(rule.program));
401 rule.program_operation = operation;
407 if (strncasecmp(key, KEY_NAME, sizeof(KEY_NAME)-1) == 0) {
408 attr = get_key_attribute(key + sizeof(KEY_NAME)-1);
410 if (strstr(attr, OPTION_PARTITIONS) != NULL) {
411 dbg("creation of partition nodes requested");
412 rule.partitions = DEFAULT_PARTITIONS_COUNT;
414 /* FIXME: remove old style option and make OPTIONS= mandatory */
415 if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
416 dbg("remove event should be ignored");
417 rule.ignore_remove = 1;
420 rule.name_operation = operation;
421 strlcpy(rule.name, value, sizeof(rule.name));
426 if (strcasecmp(key, KEY_SYMLINK) == 0) {
427 strlcpy(rule.symlink, value, sizeof(rule.symlink));
428 rule.symlink_operation = operation;
433 if (strcasecmp(key, KEY_OWNER) == 0) {
434 strlcpy(rule.owner, value, sizeof(rule.owner));
435 rule.owner_operation = operation;
440 if (strcasecmp(key, KEY_GROUP) == 0) {
441 strlcpy(rule.group, value, sizeof(rule.group));
442 rule.group_operation = operation;
447 if (strcasecmp(key, KEY_MODE) == 0) {
448 rule.mode = strtol(value, NULL, 8);
449 rule.mode_operation = operation;
454 if (strcasecmp(key, KEY_RUN) == 0) {
455 strlcpy(rule.run, value, sizeof(rule.run));
456 rule.run_operation = operation;
461 if (strcasecmp(key, KEY_OPTIONS) == 0) {
462 if (strstr(value, OPTION_LAST_RULE) != NULL) {
463 dbg("last rule to be applied");
466 if (strstr(value, OPTION_IGNORE_DEVICE) != NULL) {
467 dbg("device should be ignored");
468 rule.ignore_device = 1;
470 if (strstr(value, OPTION_IGNORE_REMOVE) != NULL) {
471 dbg("remove event should be ignored");
472 rule.ignore_remove = 1;
474 if (strstr(value, OPTION_PARTITIONS) != NULL) {
475 dbg("creation of partition nodes requested");
476 rule.partitions = DEFAULT_PARTITIONS_COUNT;
482 err("unknown key '%s'", key);
486 /* skip line if not any valid key was found */
490 if ((rule.result[0] != '\0') && (program_given == 0)) {
491 info(KEY_RESULT " is only useful when " KEY_PROGRAM " is called in any rule before");
495 rule.config_line = lineno;
496 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
497 retval = add_rule_to_list(&rule);
499 dbg("add_rule_to_list returned with error %d", retval);
502 err("parse error %s, line %d:%d, rule skipped",
503 filename, lineno, (int) (linepos - line));
507 file_unmap(buf, bufsize);
511 static int rules_map(const char *filename)
516 if (file_map(filename, &buf, &size))
520 rules_array = (struct udev_rule *) buf;
521 rules_array_size = size;
522 rules_count = size / sizeof(struct udev_rule);
523 dbg("found %zi compiled rules", rules_count);
528 int udev_rules_init(void)
530 char comp[PATH_SIZE];
534 strlcpy(comp, udev_rules_filename, sizeof(comp));
535 strlcat(comp, ".compiled", sizeof(comp));
536 if (stat(comp, &stats) == 0) {
537 dbg("parse compiled rules '%s'", comp);
538 return rules_map(comp);
541 if (stat(udev_rules_filename, &stats) != 0)
544 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
545 dbg("parse single rules file '%s'", udev_rules_filename);
546 retval = rules_parse(udev_rules_filename);
548 struct name_entry *name_loop, *name_tmp;
549 LIST_HEAD(name_list);
551 dbg("parse rules directory '%s'", udev_rules_filename);
552 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
554 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
555 rules_parse(name_loop->name);
556 list_del(&name_loop->node);
563 void udev_rules_close(void)
565 struct udev_rule *rule;
566 struct udev_rule *temp_rule;
569 file_unmap(rules_array, rules_array_size);
571 list_for_each_entry_safe(rule, temp_rule, &rules_list, node) {
572 list_del(&rule->node);