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 *new_rule)
44 struct udev_rule *tmp_rule;
46 tmp_rule = malloc(sizeof(*tmp_rule));
49 memcpy(tmp_rule, new_rule, sizeof(*tmp_rule));
50 list_add_tail(&tmp_rule->node, &udev_rule_list);
51 udev_rule_dump(tmp_rule);
56 void udev_rule_dump(struct udev_rule *rule)
58 dbg("name='%s', symlink='%s', bus='%s', id='%s', "
59 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
60 "kernel='%s', program='%s', result='%s'"
61 "owner='%s', group='%s', mode=%#o",
62 rule->name, rule->symlink, rule->bus, rule->id,
63 rule->sysfs_pair[0].file, rule->sysfs_pair[0].value,
64 rule->kernel, rule->program, rule->result,
65 rule->owner, rule->group, rule->mode);
68 void udev_rule_list_dump(void)
70 struct udev_rule *rule;
72 list_for_each_entry(rule, &udev_rule_list, node)
76 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
86 while (isspace(linepos[0]) || linepos[0] == ',')
93 if (linepos[0] == '\0')
95 if (isspace(linepos[0]))
97 if (linepos[0] == '=')
99 if (linepos[0] == '+')
101 if (linepos[0] == '!')
105 /* remember end of key */
108 /* skip whitespace after key */
109 while (isspace(linepos[0]))
112 /* get operation type */
113 if (linepos[0] == '=' && linepos[1] == '=') {
114 *operation = KEY_OP_MATCH;
116 dbg("operator=match");
117 } else if (linepos[0] == '!' && linepos[1] == '=') {
118 *operation = KEY_OP_NOMATCH;
120 dbg("operator=nomatch");
121 } else if (linepos[0] == '+' && linepos[1] == '=') {
122 *operation = KEY_OP_ADD;
125 } else if (linepos[0] == '=') {
126 *operation = KEY_OP_ASSIGN;
128 dbg("operator=assign");
134 dbg("key='%s'", *key);
136 /* skip whitespace after operator */
137 while (isspace(linepos[0]))
141 if (linepos[0] == '"')
147 temp = strchr(linepos, '"');
152 dbg("value='%s'", *value);
154 /* move line to next key */
160 /* extract possible KEY{attr} */
161 static char *get_key_attribute(char *str)
166 attr = strchr(str, '{');
169 pos = strchr(attr, '}');
171 dbg("missing closing brace for format");
175 dbg("attribute='%s'", attr);
182 static int rules_parse(struct udevice *udev, const char *filename)
184 char line[LINE_SIZE];
193 int program_given = 0;
196 struct udev_rule rule;
198 if (file_map(filename, &buf, &bufsize) != 0) {
199 dbg("can't open '%s' as rules file", filename);
202 dbg("reading '%s' as rules file", filename);
204 /* loop through the whole file */
207 while (cur < bufsize) {
210 count = buf_get_line(buf, bufsize, cur);
215 if (count >= sizeof(line)) {
216 info("line too long, rule skipped %s, line %d", filename, lineno);
220 /* eat the whitespace */
221 while ((count > 0) && isspace(bufline[0])) {
228 /* see if this is a comment */
229 if (bufline[0] == COMMENT_CHARACTER)
232 /* skip backslash and newline from multi line rules */
233 for (i = j = 0; i < count; i++) {
234 if (bufline[i] == '\\' && bufline[i+1] == '\n')
237 line[j++] = bufline[i];
240 dbg("read '%s'", line);
242 /* get all known keys */
243 memset(&rule, 0x00, sizeof(struct udev_rule));
250 enum key_operation operation = KEY_OP_UNKNOWN;
252 retval = get_key(&linepos, &key, &operation, &value);
256 if (strcasecmp(key, KEY_KERNEL) == 0) {
257 strlcpy(rule.kernel, value, sizeof(rule.kernel));
258 rule.kernel_operation = operation;
263 if (strcasecmp(key, KEY_SUBSYSTEM) == 0) {
264 strlcpy(rule.subsystem, value, sizeof(rule.subsystem));
265 rule.subsystem_operation = operation;
270 if (strcasecmp(key, KEY_BUS) == 0) {
271 strlcpy(rule.bus, value, sizeof(rule.bus));
272 rule.bus_operation = operation;
277 if (strcasecmp(key, KEY_ID) == 0) {
278 strlcpy(rule.id, value, sizeof(rule.id));
279 rule.id_operation = operation;
284 if (strncasecmp(key, KEY_SYSFS, sizeof(KEY_SYSFS)-1) == 0) {
285 struct sysfs_pair *pair = &rule.sysfs_pair[0];
286 int sysfs_pair_num = 0;
288 /* find first unused pair */
289 while (pair->file[0] != '\0') {
291 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
298 attr = get_key_attribute(key + sizeof(KEY_SYSFS)-1);
300 dbg("error parsing " KEY_SYSFS " attribute");
303 strlcpy(pair->file, attr, sizeof(pair->file));
304 strlcpy(pair->value, value, sizeof(pair->value));
305 pair->operation = operation;
311 if (strcasecmp(key, KEY_DRIVER) == 0) {
312 strlcpy(rule.driver, value, sizeof(rule.driver));
313 rule.driver_operation = operation;
318 if (strcasecmp(key, KEY_RESULT) == 0) {
319 strlcpy(rule.result, value, sizeof(rule.result));
320 rule.result_operation = operation;
325 if (strcasecmp(key, KEY_PROGRAM) == 0) {
326 strlcpy(rule.program, value, sizeof(rule.program));
327 rule.program_operation = operation;
333 if (strncasecmp(key, KEY_NAME, sizeof(KEY_NAME)-1) == 0) {
334 attr = get_key_attribute(key + sizeof(KEY_NAME)-1);
335 /* FIXME: remove old style options and make OPTIONS= mandatory */
337 if (strstr(attr, OPTION_PARTITIONS) != NULL) {
338 dbg("creation of partition nodes requested");
339 rule.partitions = DEFAULT_PARTITIONS_COUNT;
341 if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
342 dbg("remove event should be ignored");
343 rule.ignore_remove = 1;
346 if (value[0] != '\0')
347 strlcpy(rule.name, value, sizeof(rule.name));
349 rule.ignore_device = 1;
354 if (strcasecmp(key, KEY_SYMLINK) == 0) {
355 strlcpy(rule.symlink, value, sizeof(rule.symlink));
360 if (strcasecmp(key, KEY_OWNER) == 0) {
361 strlcpy(rule.owner, value, sizeof(rule.owner));
366 if (strcasecmp(key, KEY_GROUP) == 0) {
367 strlcpy(rule.group, value, sizeof(rule.group));
372 if (strcasecmp(key, KEY_MODE) == 0) {
373 rule.mode = strtol(value, NULL, 8);
378 if (strcasecmp(key, KEY_OPTIONS) == 0) {
379 if (strstr(value, OPTION_LAST_RULE) != NULL) {
380 dbg("last rule to be applied");
383 if (strstr(value, OPTION_IGNORE_DEVICE) != NULL) {
384 dbg("device should be ignored");
385 rule.ignore_device = 1;
387 if (strstr(value, OPTION_IGNORE_REMOVE) != NULL) {
388 dbg("remove event should be ignored");
389 rule.ignore_remove = 1;
391 if (strstr(value, OPTION_PARTITIONS) != NULL) {
392 dbg("creation of partition nodes requested");
393 rule.partitions = DEFAULT_PARTITIONS_COUNT;
399 dbg("unknown key '%s'", key);
403 /* skip line if not any valid key was found */
407 /* simple plausibility checks for given keys */
408 if ((rule.sysfs_pair[0].file[0] == '\0') ^
409 (rule.sysfs_pair[0].value[0] == '\0')) {
410 info("inconsistency in " KEY_SYSFS " key");
414 if ((rule.result[0] != '\0') && (program_given == 0)) {
415 info(KEY_RESULT " is only useful when "
416 KEY_PROGRAM " is called in any rule before");
420 rule.config_line = lineno;
421 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
422 retval = add_config_dev(&rule);
424 dbg("add_config_dev returned with error %d", retval);
427 info("parse error %s, line %d:%d, rule skipped",
428 filename, lineno, (int) (linepos - line));
432 file_unmap(buf, bufsize);
436 int udev_rules_init(void)
441 if (stat(udev_rules_filename, &stats) != 0)
444 if ((stats.st_mode & S_IFMT) != S_IFDIR)
445 retval = rules_parse(NULL, udev_rules_filename);
447 retval = call_foreach_file(rules_parse, NULL, udev_rules_filename, RULEFILE_SUFFIX);
452 void udev_rules_close(void)
454 struct udev_rule *rule;
455 struct udev_rule *temp_rule;
457 list_for_each_entry_safe(rule, temp_rule, &udev_rule_list, node) {
458 list_del(&rule->node);