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 /* extract possible KEY{attr} */
77 static char *get_key_attribute(char *str)
82 attr = strchr(str, '{');
85 pos = strchr(attr, '}');
87 dbg("missing closing brace for format");
91 dbg("attribute='%s'", attr);
98 static int rules_parse(struct udevice *udev, const char *filename)
100 char line[LINE_SIZE];
111 int program_given = 0;
114 struct udev_rule rule;
116 if (file_map(filename, &buf, &bufsize) == 0) {
117 dbg("reading '%s' as rules file", filename);
119 dbg("can't open '%s' as rules file", filename);
123 /* loop through the whole file */
126 while (cur < bufsize) {
129 count = buf_get_line(buf, bufsize, cur);
134 if (count >= sizeof(line)) {
135 info("line too long, rule skipped %s, line %d", filename, lineno);
139 /* eat the whitespace */
140 while ((count > 0) && isspace(bufline[0])) {
147 /* see if this is a comment */
148 if (bufline[0] == COMMENT_CHARACTER)
151 /* skip backslash and newline from multi line rules */
152 for (i = j = 0; i < count; i++) {
153 if (bufline[i] == '\\' && bufline[i+1] == '\n')
156 line[j++] = bufline[i];
159 dbg("read '%s'", line);
161 /* get all known keys */
162 memset(&rule, 0x00, sizeof(struct udev_rule));
167 retval = parse_get_pair(&temp, &temp2, &temp3);
171 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
172 strlcpy(rule.kernel, temp3, sizeof(rule.kernel));
177 if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
178 strlcpy(rule.subsystem, temp3, sizeof(rule.subsystem));
183 if (strcasecmp(temp2, FIELD_BUS) == 0) {
184 strlcpy(rule.bus, temp3, sizeof(rule.bus));
189 if (strcasecmp(temp2, FIELD_ID) == 0) {
190 strlcpy(rule.id, temp3, sizeof(rule.id));
195 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
196 struct sysfs_pair *pair = &rule.sysfs_pair[0];
197 int sysfs_pair_num = 0;
199 /* find first unused pair */
200 while (pair->file[0] != '\0') {
202 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
209 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
211 dbg("error parsing " FIELD_SYSFS " attribute");
214 strlcpy(pair->file, attr, sizeof(pair->file));
215 strlcpy(pair->value, temp3, sizeof(pair->value));
221 if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
222 strlcpy(rule.driver, temp3, sizeof(rule.driver));
227 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
229 strlcpy(rule.program, temp3, sizeof(rule.program));
234 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
235 strlcpy(rule.result, temp3, sizeof(rule.result));
240 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
241 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
242 /* FIXME: remove old style options and make OPTIONS= mandatory */
244 if (strstr(attr, OPTION_PARTITIONS) != NULL) {
245 dbg("creation of partition nodes requested");
246 rule.partitions = DEFAULT_PARTITIONS_COUNT;
248 if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
249 dbg("remove event should be ignored");
250 rule.ignore_remove = 1;
253 if (temp3[0] != '\0')
254 strlcpy(rule.name, temp3, sizeof(rule.name));
256 rule.ignore_device = 1;
261 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
262 strlcpy(rule.symlink, temp3, sizeof(rule.symlink));
267 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
268 strlcpy(rule.owner, temp3, sizeof(rule.owner));
273 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
274 strlcpy(rule.group, temp3, sizeof(rule.group));
279 if (strcasecmp(temp2, FIELD_MODE) == 0) {
280 rule.mode = strtol(temp3, NULL, 8);
285 if (strcasecmp(temp2, FIELD_OPTIONS) == 0) {
286 if (strstr(temp3, OPTION_LAST_RULE) != NULL) {
287 dbg("last rule to be applied");
290 if (strstr(temp3, OPTION_IGNORE_DEVICE) != NULL) {
291 dbg("device should be ignored");
292 rule.ignore_device = 1;
294 if (strstr(temp3, OPTION_IGNORE_REMOVE) != NULL) {
295 dbg("remove event should be ignored");
296 rule.ignore_remove = 1;
298 if (strstr(temp3, OPTION_PARTITIONS) != NULL) {
299 dbg("creation of partition nodes requested");
300 rule.partitions = DEFAULT_PARTITIONS_COUNT;
306 dbg("unknown type of field '%s'", temp2);
310 /* skip line if not any valid key was found */
314 /* simple plausibility checks for given keys */
315 if ((rule.sysfs_pair[0].file[0] == '\0') ^
316 (rule.sysfs_pair[0].value[0] == '\0')) {
317 info("inconsistency in " FIELD_SYSFS " key");
321 if ((rule.result[0] != '\0') && (program_given == 0)) {
322 info(FIELD_RESULT " is only useful when "
323 FIELD_PROGRAM " is called in any rule before");
327 rule.config_line = lineno;
328 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
329 retval = add_config_dev(&rule);
331 dbg("add_config_dev returned with error %d", retval);
334 info("parse error %s, line %d:%d, rule skipped",
335 filename, lineno, (int) (temp - line));
339 file_unmap(buf, bufsize);
343 int udev_rules_init(void)
348 if (stat(udev_rules_filename, &stats) != 0)
351 if ((stats.st_mode & S_IFMT) != S_IFDIR)
352 retval = rules_parse(NULL, udev_rules_filename);
354 retval = call_foreach_file(rules_parse, NULL, udev_rules_filename, RULEFILE_SUFFIX);
359 void udev_rules_close(void)
361 struct udev_rule *rule;
362 struct udev_rule *temp_rule;
364 list_for_each_entry_safe(rule, temp_rule, &udev_rule_list, node) {
365 list_del(&rule->node);