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 LIST_HEAD(udev_rule_list);
42 static int add_config_dev(struct udev_rule *rule)
44 struct udev_rule *tmp_rule;
46 tmp_rule = malloc(sizeof(*tmp_rule));
49 memcpy(tmp_rule, rule, sizeof(struct udev_rule));
50 list_add_tail(&tmp_rule->node, &udev_rule_list);
52 dbg("name='%s', symlink='%s', bus='%s', id='%s', "
53 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
54 "kernel='%s', program='%s', result='%s', "
55 "owner='%s', group='%s', mode=%#o, "
56 "all_partions=%u, ignore_remove=%u, ignore_device=%u, last_rule=%u",
57 rule->name, rule->symlink, rule->bus, rule->id,
58 rule->sysfs_pair[0].name, rule->sysfs_pair[0].value,
59 rule->kernel, rule->program, rule->result, rule->owner, rule->group, rule->mode,
60 rule->partitions, rule->ignore_remove, rule->ignore_device, rule->last_rule);
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 rules_parse(const char *filename)
179 char line[LINE_SIZE];
188 int program_given = 0;
191 struct udev_rule rule;
193 if (file_map(filename, &buf, &bufsize) != 0) {
194 err("can't open '%s' as rules file", filename);
197 dbg("reading '%s' as rules file", filename);
199 /* loop through the whole file */
202 while (cur < bufsize) {
205 count = buf_get_line(buf, bufsize, cur);
210 if (count >= sizeof(line)) {
211 info("line too long, rule skipped %s, line %d", filename, lineno);
215 /* eat the whitespace */
216 while ((count > 0) && isspace(bufline[0])) {
223 /* see if this is a comment */
224 if (bufline[0] == COMMENT_CHARACTER)
227 /* skip backslash and newline from multi line rules */
228 for (i = j = 0; i < count; i++) {
229 if (bufline[i] == '\\' && bufline[i+1] == '\n')
232 line[j++] = bufline[i];
235 dbg("read '%s'", line);
237 /* get all known keys */
238 memset(&rule, 0x00, sizeof(struct udev_rule));
245 enum key_operation operation = KEY_OP_UNSET;
247 retval = get_key(&linepos, &key, &operation, &value);
251 if (strcasecmp(key, KEY_KERNEL) == 0) {
252 strlcpy(rule.kernel, value, sizeof(rule.kernel));
253 rule.kernel_operation = operation;
258 if (strcasecmp(key, KEY_SUBSYSTEM) == 0) {
259 strlcpy(rule.subsystem, value, sizeof(rule.subsystem));
260 rule.subsystem_operation = operation;
265 if (strcasecmp(key, KEY_ACTION) == 0) {
266 strlcpy(rule.action, value, sizeof(rule.action));
267 rule.action_operation = operation;
272 if (strcasecmp(key, KEY_BUS) == 0) {
273 strlcpy(rule.bus, value, sizeof(rule.bus));
274 rule.bus_operation = operation;
279 if (strcasecmp(key, KEY_ID) == 0) {
280 strlcpy(rule.id, value, sizeof(rule.id));
281 rule.id_operation = operation;
286 if (strncasecmp(key, KEY_SYSFS, sizeof(KEY_SYSFS)-1) == 0) {
287 struct key_pair *pair;
289 if (rule.sysfs_pair_count >= KEY_SYSFS_PAIRS_MAX) {
290 err("skip rule, to many " KEY_SYSFS " keys in a single rule");
293 pair = &rule.sysfs_pair[rule.sysfs_pair_count];
294 attr = get_key_attribute(key + sizeof(KEY_SYSFS)-1);
296 err("error parsing " KEY_SYSFS " attribute");
299 strlcpy(pair->name, attr, sizeof(pair->name));
300 strlcpy(pair->value, value, sizeof(pair->value));
301 pair->operation = operation;
302 rule.sysfs_pair_count++;
307 if (strncasecmp(key, KEY_ENV, sizeof(KEY_ENV)-1) == 0) {
308 struct key_pair *pair;
310 if (rule.env_pair_count >= KEY_ENV_PAIRS_MAX) {
311 err("skip rule, to many " KEY_ENV " keys in a single rule");
314 pair = &rule.env_pair[rule.env_pair_count];
315 attr = get_key_attribute(key + sizeof(KEY_ENV)-1);
317 err("error parsing " KEY_ENV " attribute");
320 strlcpy(pair->name, attr, sizeof(pair->name));
321 strlcpy(pair->value, value, sizeof(pair->value));
322 pair->operation = operation;
323 rule.env_pair_count++;
328 if (strcasecmp(key, KEY_DRIVER) == 0) {
329 strlcpy(rule.driver, value, sizeof(rule.driver));
330 rule.driver_operation = operation;
335 if (strcasecmp(key, KEY_RESULT) == 0) {
336 strlcpy(rule.result, value, sizeof(rule.result));
337 rule.result_operation = operation;
342 if (strcasecmp(key, KEY_PROGRAM) == 0) {
343 strlcpy(rule.program, value, sizeof(rule.program));
344 rule.program_operation = operation;
350 if (strncasecmp(key, KEY_NAME, sizeof(KEY_NAME)-1) == 0) {
351 attr = get_key_attribute(key + sizeof(KEY_NAME)-1);
352 /* FIXME: remove old style options and make OPTIONS= mandatory */
354 if (strstr(attr, OPTION_PARTITIONS) != NULL) {
355 dbg("creation of partition nodes requested");
356 rule.partitions = DEFAULT_PARTITIONS_COUNT;
358 if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
359 dbg("remove event should be ignored");
360 rule.ignore_remove = 1;
363 if (value[0] != '\0')
364 strlcpy(rule.name, value, sizeof(rule.name));
366 rule.ignore_device = 1;
371 if (strcasecmp(key, KEY_SYMLINK) == 0) {
372 strlcpy(rule.symlink, value, sizeof(rule.symlink));
373 rule.symlink_operation = operation;
378 if (strcasecmp(key, KEY_OWNER) == 0) {
379 strlcpy(rule.owner, value, sizeof(rule.owner));
380 rule.owner_operation = operation;
385 if (strcasecmp(key, KEY_GROUP) == 0) {
386 strlcpy(rule.group, value, sizeof(rule.group));
387 rule.group_operation = operation;
392 if (strcasecmp(key, KEY_MODE) == 0) {
393 rule.mode = strtol(value, NULL, 8);
394 rule.mode_operation = operation;
399 if (strcasecmp(key, KEY_RUN) == 0) {
400 strlcpy(rule.run, value, sizeof(rule.run));
401 rule.run_operation = operation;
406 if (strcasecmp(key, KEY_OPTIONS) == 0) {
407 if (strstr(value, OPTION_LAST_RULE) != NULL) {
408 dbg("last rule to be applied");
411 if (strstr(value, OPTION_IGNORE_DEVICE) != NULL) {
412 dbg("device should be ignored");
413 rule.ignore_device = 1;
415 if (strstr(value, OPTION_IGNORE_REMOVE) != NULL) {
416 dbg("remove event should be ignored");
417 rule.ignore_remove = 1;
419 if (strstr(value, OPTION_PARTITIONS) != NULL) {
420 dbg("creation of partition nodes requested");
421 rule.partitions = DEFAULT_PARTITIONS_COUNT;
427 err("unknown key '%s'", key);
431 /* skip line if not any valid key was found */
435 if ((rule.result[0] != '\0') && (program_given == 0)) {
436 info(KEY_RESULT " is only useful when " KEY_PROGRAM " is called in any rule before");
440 rule.config_line = lineno;
441 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
442 retval = add_config_dev(&rule);
444 dbg("add_config_dev returned with error %d", retval);
447 err("parse error %s, line %d:%d, rule skipped",
448 filename, lineno, (int) (linepos - line));
452 file_unmap(buf, bufsize);
456 int udev_rules_init(void)
461 if (stat(udev_rules_filename, &stats) != 0)
464 if ((stats.st_mode & S_IFMT) != S_IFDIR)
465 retval = rules_parse(udev_rules_filename);
467 struct name_entry *name_loop, *name_tmp;
468 LIST_HEAD(name_list);
470 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
472 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
473 rules_parse(name_loop->name);
474 list_del(&name_loop->node);
481 void udev_rules_close(void)
483 struct udev_rule *rule;
484 struct udev_rule *temp_rule;
486 list_for_each_entry_safe(rule, temp_rule, &udev_rule_list, node) {
487 list_del(&rule->node);