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.
26 /* define this to enable parsing debugging also */
27 /* #define DEBUG_PARSER */
39 #include "udev_libc_wrapper.h"
41 #include "udev_utils.h"
45 LIST_HEAD(config_device_list);
47 static int add_config_dev(struct config_device *new_dev)
49 struct config_device *tmp_dev;
51 tmp_dev = malloc(sizeof(*tmp_dev));
54 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
55 list_add_tail(&tmp_dev->node, &config_device_list);
56 //dump_config_dev(tmp_dev);
60 void dump_config_dev(struct config_device *dev)
62 dbg_parse("name='%s', symlink='%s', bus='%s', id='%s', "
63 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
64 "kernel='%s', program='%s', result='%s'"
65 "owner='%s', group='%s', mode=%#o",
66 dev->name, dev->symlink, dev->bus, dev->id,
67 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
68 dev->kernel, dev->program, dev->result,
69 dev->owner, dev->group, dev->mode);
72 void dump_config_dev_list(void)
74 struct config_device *dev;
76 list_for_each_entry(dev, &config_device_list, node)
80 /* extract possible KEY{attr} */
81 static char *get_key_attribute(char *str)
86 attr = strchr(str, '{');
89 pos = strchr(attr, '}');
91 dbg("missing closing brace for format");
95 dbg("attribute='%s'", attr);
102 static int namedev_parse(struct udevice *udev, const char *filename)
104 char line[LINE_SIZE];
115 int program_given = 0;
118 struct config_device dev;
120 if (file_map(filename, &buf, &bufsize) == 0) {
121 dbg("reading '%s' as rules file", filename);
123 dbg("can't open '%s' as rules file", filename);
127 /* loop through the whole file */
130 while (cur < bufsize) {
133 count = buf_get_line(buf, bufsize, cur);
138 if (count >= sizeof(line)) {
139 info("line too long, rule skipped %s, line %d", filename, lineno);
143 /* eat the whitespace */
144 while ((count > 0) && isspace(bufline[0])) {
151 /* see if this is a comment */
152 if (bufline[0] == COMMENT_CHARACTER)
155 /* skip backslash and newline from multi line rules */
156 for (i = j = 0; i < count; i++) {
157 if (bufline[i] == '\\' && bufline[i+1] == '\n')
160 line[j++] = bufline[i];
163 dbg_parse("read '%s'", line);
165 /* get all known keys */
166 memset(&dev, 0x00, sizeof(struct config_device));
171 retval = parse_get_pair(&temp, &temp2, &temp3);
175 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
176 strlcpy(dev.kernel, temp3, sizeof(dev.kernel));
181 if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
182 strlcpy(dev.subsystem, temp3, sizeof(dev.subsystem));
187 if (strcasecmp(temp2, FIELD_BUS) == 0) {
188 strlcpy(dev.bus, temp3, sizeof(dev.bus));
193 if (strcasecmp(temp2, FIELD_ID) == 0) {
194 strlcpy(dev.id, temp3, sizeof(dev.id));
199 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
200 struct sysfs_pair *pair = &dev.sysfs_pair[0];
201 int sysfs_pair_num = 0;
203 /* find first unused pair */
204 while (pair->file[0] != '\0') {
206 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
213 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
215 dbg("error parsing " FIELD_SYSFS " attribute");
218 strlcpy(pair->file, attr, sizeof(pair->file));
219 strlcpy(pair->value, temp3, sizeof(pair->value));
225 if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
226 strlcpy(dev.driver, temp3, sizeof(dev.driver));
231 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
233 strlcpy(dev.program, temp3, sizeof(dev.program));
238 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
239 strlcpy(dev.result, temp3, sizeof(dev.result));
244 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
245 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
246 /* FIXME: remove old style options and make OPTIONS= mandatory */
248 if (strstr(attr, OPTION_PARTITIONS) != NULL) {
249 dbg_parse("creation of partition nodes requested");
250 dev.partitions = DEFAULT_PARTITIONS_COUNT;
252 if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) {
253 dbg_parse("remove event should be ignored");
254 dev.ignore_remove = 1;
257 if (temp3[0] != '\0')
258 strlcpy(dev.name, temp3, sizeof(dev.name));
260 dev.ignore_device = 1;
265 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
266 strlcpy(dev.symlink, temp3, sizeof(dev.symlink));
271 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
272 strlcpy(dev.owner, temp3, sizeof(dev.owner));
277 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
278 strlcpy(dev.group, temp3, sizeof(dev.group));
283 if (strcasecmp(temp2, FIELD_MODE) == 0) {
284 dev.mode = strtol(temp3, NULL, 8);
289 if (strcasecmp(temp2, FIELD_OPTIONS) == 0) {
290 if (strstr(temp3, OPTION_IGNORE_DEVICE) != NULL) {
291 dbg_parse("device should be ignored");
292 dev.ignore_device = 1;
294 if (strstr(temp3, OPTION_IGNORE_REMOVE) != NULL) {
295 dbg_parse("remove event should be ignored");
296 dev.ignore_remove = 1;
298 if (strstr(temp3, OPTION_PARTITIONS) != NULL) {
299 dbg_parse("creation of partition nodes requested");
300 dev.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 ((dev.sysfs_pair[0].file[0] == '\0') ^
316 (dev.sysfs_pair[0].value[0] == '\0')) {
317 info("inconsistency in " FIELD_SYSFS " key");
321 if ((dev.result[0] != '\0') && (program_given == 0)) {
322 info(FIELD_RESULT " is only useful when "
323 FIELD_PROGRAM " is called in any rule before");
327 dev.config_line = lineno;
328 strlcpy(dev.config_file, filename, sizeof(dev.config_file));
329 retval = add_config_dev(&dev);
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 namedev_init(void)
348 if (stat(udev_rules_filename, &stats) != 0)
351 if ((stats.st_mode & S_IFMT) != S_IFDIR)
352 retval = namedev_parse(NULL, udev_rules_filename);
354 retval = call_foreach_file(namedev_parse, NULL, udev_rules_filename, RULEFILE_SUFFIX);
359 void namedev_close(void)
361 struct config_device *dev;
362 struct config_device *temp_dev;
364 list_for_each_entry_safe(dev, temp_dev, &config_device_list, node) {
365 list_del(&dev->node);