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].file, 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] == '!')
94 /* remember end of key */
97 /* skip whitespace after key */
98 while (isspace(linepos[0]))
101 /* get operation type */
102 if (linepos[0] == '=' && linepos[1] == '=') {
103 *operation = KEY_OP_MATCH;
105 dbg("operator=match");
106 } else if (linepos[0] == '!' && linepos[1] == '=') {
107 *operation = KEY_OP_NOMATCH;
109 dbg("operator=nomatch");
110 } else if (linepos[0] == '+' && linepos[1] == '=') {
111 *operation = KEY_OP_ADD;
114 } else if (linepos[0] == '=') {
115 *operation = KEY_OP_ASSIGN;
117 dbg("operator=assign");
123 dbg("key='%s'", *key);
125 /* skip whitespace after operator */
126 while (isspace(linepos[0]))
130 if (linepos[0] == '"')
136 temp = strchr(linepos, '"');
141 dbg("value='%s'", *value);
143 /* move line to next key */
149 /* extract possible KEY{attr} */
150 static char *get_key_attribute(char *str)
155 attr = strchr(str, '{');
158 pos = strchr(attr, '}');
160 dbg("missing closing brace for format");
164 dbg("attribute='%s'", attr);
171 static int rules_parse(struct udevice *udev, const char *filename)
173 char line[LINE_SIZE];
182 int program_given = 0;
185 struct udev_rule rule;
187 if (file_map(filename, &buf, &bufsize) != 0) {
188 dbg("can't open '%s' as rules file", filename);
191 dbg("reading '%s' as rules file", filename);
193 /* loop through the whole file */
196 while (cur < bufsize) {
199 count = buf_get_line(buf, bufsize, cur);
204 if (count >= sizeof(line)) {
205 info("line too long, rule skipped %s, line %d", filename, lineno);
209 /* eat the whitespace */
210 while ((count > 0) && isspace(bufline[0])) {
217 /* see if this is a comment */
218 if (bufline[0] == COMMENT_CHARACTER)
221 /* skip backslash and newline from multi line rules */
222 for (i = j = 0; i < count; i++) {
223 if (bufline[i] == '\\' && bufline[i+1] == '\n')
226 line[j++] = bufline[i];
229 dbg("read '%s'", line);
231 /* get all known keys */
232 memset(&rule, 0x00, sizeof(struct udev_rule));
239 enum key_operation operation = KEY_OP_UNKNOWN;
241 retval = get_key(&linepos, &key, &operation, &value);
245 if (strcasecmp(key, KEY_KERNEL) == 0) {
246 strlcpy(rule.kernel, value, sizeof(rule.kernel));
247 rule.kernel_operation = operation;
252 if (strcasecmp(key, KEY_SUBSYSTEM) == 0) {
253 strlcpy(rule.subsystem, value, sizeof(rule.subsystem));
254 rule.subsystem_operation = operation;
259 if (strcasecmp(key, KEY_BUS) == 0) {
260 strlcpy(rule.bus, value, sizeof(rule.bus));
261 rule.bus_operation = operation;
266 if (strcasecmp(key, KEY_ID) == 0) {
267 strlcpy(rule.id, value, sizeof(rule.id));
268 rule.id_operation = operation;
273 if (strncasecmp(key, KEY_SYSFS, sizeof(KEY_SYSFS)-1) == 0) {
274 struct sysfs_pair *pair = &rule.sysfs_pair[0];
275 int sysfs_pair_num = 0;
277 /* find first unused pair */
278 while (pair->file[0] != '\0') {
280 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
287 attr = get_key_attribute(key + sizeof(KEY_SYSFS)-1);
289 dbg("error parsing " KEY_SYSFS " attribute");
292 strlcpy(pair->file, attr, sizeof(pair->file));
293 strlcpy(pair->value, value, sizeof(pair->value));
294 pair->operation = operation;
300 if (strcasecmp(key, KEY_DRIVER) == 0) {
301 strlcpy(rule.driver, value, sizeof(rule.driver));
302 rule.driver_operation = operation;
307 if (strcasecmp(key, KEY_RESULT) == 0) {
308 strlcpy(rule.result, value, sizeof(rule.result));
309 rule.result_operation = operation;
314 if (strcasecmp(key, KEY_PROGRAM) == 0) {
315 strlcpy(rule.program, value, sizeof(rule.program));
316 rule.program_operation = operation;
322 if (strncasecmp(key, KEY_NAME, sizeof(KEY_NAME)-1) == 0) {
323 attr = get_key_attribute(key + sizeof(KEY_NAME)-1);
324 /* FIXME: remove old style options and make OPTIONS= mandatory */
326 if (strstr(attr, OPTION_PARTITIONS) != NULL) {
327 dbg("creation of partition nodes requested");
328 rule.partitions = DEFAULT_PARTITIONS_COUNT;
330 if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
331 dbg("remove event should be ignored");
332 rule.ignore_remove = 1;
335 if (value[0] != '\0')
336 strlcpy(rule.name, value, sizeof(rule.name));
338 rule.ignore_device = 1;
343 if (strcasecmp(key, KEY_SYMLINK) == 0) {
344 strlcpy(rule.symlink, value, sizeof(rule.symlink));
349 if (strcasecmp(key, KEY_OWNER) == 0) {
350 strlcpy(rule.owner, value, sizeof(rule.owner));
355 if (strcasecmp(key, KEY_GROUP) == 0) {
356 strlcpy(rule.group, value, sizeof(rule.group));
361 if (strcasecmp(key, KEY_MODE) == 0) {
362 rule.mode = strtol(value, NULL, 8);
367 if (strcasecmp(key, KEY_OPTIONS) == 0) {
368 if (strstr(value, OPTION_LAST_RULE) != NULL) {
369 dbg("last rule to be applied");
372 if (strstr(value, OPTION_IGNORE_DEVICE) != NULL) {
373 dbg("device should be ignored");
374 rule.ignore_device = 1;
376 if (strstr(value, OPTION_IGNORE_REMOVE) != NULL) {
377 dbg("remove event should be ignored");
378 rule.ignore_remove = 1;
380 if (strstr(value, OPTION_PARTITIONS) != NULL) {
381 dbg("creation of partition nodes requested");
382 rule.partitions = DEFAULT_PARTITIONS_COUNT;
388 dbg("unknown key '%s'", key);
392 /* skip line if not any valid key was found */
396 /* simple plausibility checks for given keys */
397 if ((rule.sysfs_pair[0].file[0] == '\0') ^
398 (rule.sysfs_pair[0].value[0] == '\0')) {
399 info("inconsistency in " KEY_SYSFS " key");
403 if ((rule.result[0] != '\0') && (program_given == 0)) {
404 info(KEY_RESULT " is only useful when "
405 KEY_PROGRAM " is called in any rule before");
409 rule.config_line = lineno;
410 strlcpy(rule.config_file, filename, sizeof(rule.config_file));
411 retval = add_config_dev(&rule);
413 dbg("add_config_dev returned with error %d", retval);
416 info("parse error %s, line %d:%d, rule skipped",
417 filename, lineno, (int) (linepos - line));
421 file_unmap(buf, bufsize);
425 int udev_rules_init(void)
430 if (stat(udev_rules_filename, &stats) != 0)
433 if ((stats.st_mode & S_IFMT) != S_IFDIR)
434 retval = rules_parse(NULL, udev_rules_filename);
436 retval = call_foreach_file(rules_parse, NULL, udev_rules_filename, RULEFILE_SUFFIX);
441 void udev_rules_close(void)
443 struct udev_rule *rule;
444 struct udev_rule *temp_rule;
446 list_for_each_entry_safe(rule, temp_rule, &udev_rule_list, node) {
447 list_del(&rule->node);