6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* define this to enable parsing debugging also */
26 /* #define DEBUG_PARSER */
39 #include "udev_utils.h"
43 LIST_HEAD(config_device_list);
45 static int add_config_dev(struct config_device *new_dev)
47 struct config_device *tmp_dev;
49 tmp_dev = malloc(sizeof(*tmp_dev));
52 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
53 list_add_tail(&tmp_dev->node, &config_device_list);
54 //dump_config_dev(tmp_dev);
58 void dump_config_dev(struct config_device *dev)
60 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
61 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
62 "kernel='%s', program='%s', result='%s'"
63 "owner='%s', group='%s', mode=%#o",
64 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
65 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
66 dev->kernel, dev->program, dev->result,
67 dev->owner, dev->group, dev->mode);
70 void dump_config_dev_list(void)
72 struct config_device *dev;
74 list_for_each_entry(dev, &config_device_list, node)
78 /* extract possible KEY{attr} */
79 static char *get_key_attribute(char *str)
84 attr = strchr(str, '{');
87 pos = strchr(attr, '}');
89 dbg("missing closing brace for format");
93 dbg("attribute='%s'", attr);
100 static int namedev_parse(const char *filename, void *data)
102 char line[LINE_SIZE];
113 int program_given = 0;
116 struct config_device dev;
118 if (file_map(filename, &buf, &bufsize) == 0) {
119 dbg("reading '%s' as rules file", filename);
121 dbg("can't open '%s' as rules file", filename);
125 /* loop through the whole file */
128 while (cur < bufsize) {
129 count = buf_get_line(buf, bufsize, cur);
134 if (count >= LINE_SIZE) {
135 info("line too long, rule skipped %s, line %d",
140 /* eat the whitespace */
141 while ((count > 0) && isspace(bufline[0])) {
148 /* see if this is a comment */
149 if (bufline[0] == COMMENT_CHARACTER)
152 strncpy(line, bufline, count);
154 dbg_parse("read '%s'", line);
156 /* get all known keys */
157 memset(&dev, 0x00, sizeof(struct config_device));
162 retval = parse_get_pair(&temp, &temp2, &temp3);
166 if (strcasecmp(temp2, FIELD_BUS) == 0) {
167 strfieldcpy(dev.bus, temp3);
172 if (strcasecmp(temp2, FIELD_ID) == 0) {
173 strfieldcpy(dev.id, temp3);
178 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
179 strfieldcpy(dev.place, temp3);
184 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
185 struct sysfs_pair *pair = &dev.sysfs_pair[0];
186 int sysfs_pair_num = 0;
188 /* find first unused pair */
189 while (pair->file[0] != '\0') {
191 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
198 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
200 dbg("error parsing " FIELD_SYSFS " attribute");
203 strfieldcpy(pair->file, attr);
204 strfieldcpy(pair->value, temp3);
210 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
211 strfieldcpy(dev.kernel, temp3);
216 if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
217 strfieldcpy(dev.subsystem, temp3);
222 if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
223 strfieldcpy(dev.driver, temp3);
228 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
230 strfieldcpy(dev.program, temp3);
235 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
236 strfieldcpy(dev.result, temp3);
241 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
242 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
244 if (strstr(attr, ATTR_PARTITIONS) != NULL) {
245 dbg_parse("creation of partition nodes requested");
246 dev.partitions = PARTITIONS_COUNT;
248 if (strstr(attr, ATTR_IGNORE_REMOVE) != NULL) {
249 dbg_parse("remove event should be ignored");
250 dev.ignore_remove = 1;
253 strfieldcpy(dev.name, temp3);
258 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
259 strfieldcpy(dev.symlink, temp3);
264 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
265 strfieldcpy(dev.owner, temp3);
270 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
271 strfieldcpy(dev.group, temp3);
276 if (strcasecmp(temp2, FIELD_MODE) == 0) {
277 dev.mode = strtol(temp3, NULL, 8);
282 dbg("unknown type of field '%s'", temp2);
286 /* skip line if not any valid key was found */
290 /* simple plausibility checks for given keys */
291 if ((dev.sysfs_pair[0].file[0] == '\0') ^
292 (dev.sysfs_pair[0].value[0] == '\0')) {
293 info("inconsistency in " FIELD_SYSFS " key");
297 if ((dev.result[0] != '\0') && (program_given == 0)) {
298 info(FIELD_RESULT " is only useful when "
299 FIELD_PROGRAM " is called in any rule before");
303 dev.config_line = lineno;
304 strfieldcpy(dev.config_file, filename);
305 retval = add_config_dev(&dev);
307 dbg("add_config_dev returned with error %d", retval);
310 info("parse error %s, line %d:%d, rule skipped",
311 filename, lineno, temp - line);
315 file_unmap(buf, bufsize);
319 int namedev_init(void)
324 if (stat(udev_rules_filename, &stats) != 0)
327 if ((stats.st_mode & S_IFMT) != S_IFDIR)
328 retval = namedev_parse(udev_rules_filename, NULL);
330 retval = call_foreach_file(namedev_parse, udev_rules_filename, RULEFILE_SUFFIX, NULL);