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) {
131 count = buf_get_line(buf, bufsize, cur);
136 if (count >= LINE_SIZE) {
137 info("line too long, rule skipped %s, line %d", filename, lineno);
141 /* eat the whitespace */
142 while ((count > 0) && isspace(bufline[0])) {
149 /* see if this is a comment */
150 if (bufline[0] == COMMENT_CHARACTER)
153 /* skip backslash and newline from multi line rules */
154 for (i = j = 0; i < count; i++) {
155 if (bufline[i] == '\\' || bufline[i] == '\n')
158 line[j++] = bufline[i];
161 dbg_parse("read '%s'", line);
163 /* get all known keys */
164 memset(&dev, 0x00, sizeof(struct config_device));
169 retval = parse_get_pair(&temp, &temp2, &temp3);
173 if (strcasecmp(temp2, FIELD_BUS) == 0) {
174 strfieldcpy(dev.bus, temp3);
179 if (strcasecmp(temp2, FIELD_ID) == 0) {
180 strfieldcpy(dev.id, temp3);
185 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
186 strfieldcpy(dev.place, temp3);
191 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
192 struct sysfs_pair *pair = &dev.sysfs_pair[0];
193 int sysfs_pair_num = 0;
195 /* find first unused pair */
196 while (pair->file[0] != '\0') {
198 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
205 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
207 dbg("error parsing " FIELD_SYSFS " attribute");
210 strfieldcpy(pair->file, attr);
211 strfieldcpy(pair->value, temp3);
217 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
218 strfieldcpy(dev.kernel, temp3);
223 if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
224 strfieldcpy(dev.subsystem, temp3);
229 if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
230 strfieldcpy(dev.driver, temp3);
235 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
237 strfieldcpy(dev.program, temp3);
242 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
243 strfieldcpy(dev.result, temp3);
248 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
249 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
251 if (strstr(attr, ATTR_PARTITIONS) != NULL) {
252 dbg_parse("creation of partition nodes requested");
253 dev.partitions = PARTITIONS_COUNT;
255 if (strstr(attr, ATTR_IGNORE_REMOVE) != NULL) {
256 dbg_parse("remove event should be ignored");
257 dev.ignore_remove = 1;
260 strfieldcpy(dev.name, temp3);
265 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
266 strfieldcpy(dev.symlink, temp3);
271 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
272 strfieldcpy(dev.owner, temp3);
277 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
278 strfieldcpy(dev.group, temp3);
283 if (strcasecmp(temp2, FIELD_MODE) == 0) {
284 dev.mode = strtol(temp3, NULL, 8);
289 dbg("unknown type of field '%s'", temp2);
293 /* skip line if not any valid key was found */
297 /* simple plausibility checks for given keys */
298 if ((dev.sysfs_pair[0].file[0] == '\0') ^
299 (dev.sysfs_pair[0].value[0] == '\0')) {
300 info("inconsistency in " FIELD_SYSFS " key");
304 if ((dev.result[0] != '\0') && (program_given == 0)) {
305 info(FIELD_RESULT " is only useful when "
306 FIELD_PROGRAM " is called in any rule before");
310 dev.config_line = lineno;
311 strfieldcpy(dev.config_file, filename);
312 retval = add_config_dev(&dev);
314 dbg("add_config_dev returned with error %d", retval);
317 info("parse error %s, line %d:%d, rule skipped",
318 filename, lineno, temp - line);
322 file_unmap(buf, bufsize);
326 int namedev_init(void)
331 if (stat(udev_rules_filename, &stats) != 0)
334 if ((stats.st_mode & S_IFMT) != S_IFDIR)
335 retval = namedev_parse(udev_rules_filename, NULL);
337 retval = call_foreach_file(namedev_parse, udev_rules_filename, RULEFILE_SUFFIX, NULL);