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 */
40 #include "udev_utils.h"
44 LIST_HEAD(config_device_list);
46 static int add_config_dev(struct config_device *new_dev)
48 struct config_device *tmp_dev;
50 tmp_dev = malloc(sizeof(*tmp_dev));
53 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
54 list_add_tail(&tmp_dev->node, &config_device_list);
55 //dump_config_dev(tmp_dev);
59 void dump_config_dev(struct config_device *dev)
61 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
62 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
63 "kernel='%s', program='%s', result='%s'"
64 "owner='%s', group='%s', mode=%#o",
65 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
66 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
67 dev->kernel, dev->program, dev->result,
68 dev->owner, dev->group, dev->mode);
71 void dump_config_dev_list(void)
73 struct config_device *dev;
75 list_for_each_entry(dev, &config_device_list, node)
79 /* extract possible KEY{attr} */
80 static char *get_key_attribute(char *str)
85 attr = strchr(str, '{');
88 pos = strchr(attr, '}');
90 dbg("missing closing brace for format");
94 dbg("attribute='%s'", attr);
101 static int namedev_parse(const char *filename, void *data)
103 char line[LINE_SIZE];
114 int program_given = 0;
117 struct config_device dev;
119 if (file_map(filename, &buf, &bufsize) == 0) {
120 dbg("reading '%s' as rules file", filename);
122 dbg("can't open '%s' as rules file", filename);
126 /* loop through the whole file */
129 while (cur < bufsize) {
132 count = buf_get_line(buf, bufsize, cur);
137 if (count >= LINE_SIZE) {
138 info("line too long, rule skipped %s, line %d", filename, lineno);
142 /* eat the whitespace */
143 while ((count > 0) && isspace(bufline[0])) {
150 /* see if this is a comment */
151 if (bufline[0] == COMMENT_CHARACTER)
154 /* skip backslash and newline from multi line rules */
155 for (i = j = 0; i < count; i++) {
156 if (bufline[i] == '\\' || bufline[i] == '\n')
159 line[j++] = bufline[i];
162 dbg_parse("read '%s'", line);
164 /* get all known keys */
165 memset(&dev, 0x00, sizeof(struct config_device));
170 retval = parse_get_pair(&temp, &temp2, &temp3);
174 if (strcasecmp(temp2, FIELD_BUS) == 0) {
175 strfieldcpy(dev.bus, temp3);
180 if (strcasecmp(temp2, FIELD_ID) == 0) {
181 strfieldcpy(dev.id, temp3);
186 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
187 strfieldcpy(dev.place, temp3);
192 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
193 struct sysfs_pair *pair = &dev.sysfs_pair[0];
194 int sysfs_pair_num = 0;
196 /* find first unused pair */
197 while (pair->file[0] != '\0') {
199 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
206 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
208 dbg("error parsing " FIELD_SYSFS " attribute");
211 strfieldcpy(pair->file, attr);
212 strfieldcpy(pair->value, temp3);
218 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
219 strfieldcpy(dev.kernel, temp3);
224 if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
225 strfieldcpy(dev.subsystem, temp3);
230 if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
231 strfieldcpy(dev.driver, temp3);
236 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
238 strfieldcpy(dev.program, temp3);
243 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
244 strfieldcpy(dev.result, temp3);
249 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
250 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
251 /* FIXME: remove old style options and make OPTIONS= mandatory */
253 if (strstr(attr, ATTR_PARTITIONS) != NULL) {
254 dbg_parse("creation of partition nodes requested");
255 dev.partitions = DEFAULT_PARTITIONS_COUNT;
257 if (strstr(attr, ATTR_IGNORE_REMOVE) != NULL) {
258 dbg_parse("remove event should be ignored");
259 dev.ignore_remove = 1;
262 strfieldcpy(dev.name, temp3);
267 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
268 strfieldcpy(dev.symlink, temp3);
273 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
274 strfieldcpy(dev.owner, temp3);
279 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
280 strfieldcpy(dev.group, temp3);
285 if (strcasecmp(temp2, FIELD_MODE) == 0) {
286 dev.mode = strtol(temp3, NULL, 8);
291 if (strcasecmp(temp2, FIELD_OPTIONS) == 0) {
292 if (strstr(temp3, ATTR_IGNORE_DEVICE) != NULL) {
293 dbg_parse("device should be ignored");
294 dev.ignore_device = 1;
296 if (strstr(temp3, ATTR_IGNORE_REMOVE) != NULL) {
297 dbg_parse("remove event should be ignored");
298 dev.ignore_remove = 1;
300 if (strstr(temp3, ATTR_PARTITIONS) != NULL) {
301 dbg_parse("creation of partition nodes requested");
302 dev.partitions = DEFAULT_PARTITIONS_COUNT;
308 dbg("unknown type of field '%s'", temp2);
312 /* skip line if not any valid key was found */
316 /* simple plausibility checks for given keys */
317 if ((dev.sysfs_pair[0].file[0] == '\0') ^
318 (dev.sysfs_pair[0].value[0] == '\0')) {
319 info("inconsistency in " FIELD_SYSFS " key");
323 if ((dev.result[0] != '\0') && (program_given == 0)) {
324 info(FIELD_RESULT " is only useful when "
325 FIELD_PROGRAM " is called in any rule before");
329 dev.config_line = lineno;
330 strfieldcpy(dev.config_file, filename);
331 retval = add_config_dev(&dev);
333 dbg("add_config_dev returned with error %d", retval);
336 info("parse error %s, line %d:%d, rule skipped",
337 filename, lineno, temp - line);
341 file_unmap(buf, bufsize);
345 int namedev_init(void)
350 if (stat(udev_rules_filename, &stats) != 0)
353 if ((stats.st_mode & S_IFMT) != S_IFDIR)
354 retval = namedev_parse(udev_rules_filename, NULL);
356 retval = call_foreach_file(namedev_parse, udev_rules_filename, RULEFILE_SUFFIX, NULL);
361 void namedev_close(void)
363 struct config_device *dev;
365 list_for_each_entry(dev, &config_device_list, node) {
366 list_del(&dev->node);