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 */
42 static int add_config_dev(struct config_device *new_dev)
44 struct config_device *tmp_dev;
46 tmp_dev = malloc(sizeof(*tmp_dev));
49 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
50 list_add_tail(&tmp_dev->node, &config_device_list);
51 //dump_config_dev(tmp_dev);
55 void dump_config_dev(struct config_device *dev)
57 /*FIXME dump all sysfs's */
58 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
59 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
60 "kernel='%s', program='%s', result='%s'",
61 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
62 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
63 dev->kernel, dev->program, dev->result);
66 void dump_config_dev_list(void)
68 struct config_device *dev;
70 list_for_each_entry(dev, &config_device_list, node)
74 void dump_perm_dev(struct perm_device *dev)
76 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
77 dev->name, dev->owner, dev->group, dev->mode);
80 void dump_perm_dev_list(void)
82 struct perm_device *dev;
84 list_for_each_entry(dev, &perm_device_list, node)
88 /* extract possible KEY{attr} or KEY_attr */
89 static char *get_key_attribute(char *str)
94 attr = strchr(str, '{');
97 pos = strchr(attr, '}');
99 dbg("missing closing brace for format");
103 dbg("attribute='%s'", attr);
107 attr = strchr(str, '_');
110 dbg("attribute='%s'", attr);
117 int namedev_init_rules(void)
126 int program_given = 0;
128 struct config_device dev;
130 fd = fopen(udev_rules_filename, "r");
132 dbg("reading '%s' as rules file", udev_rules_filename);
134 dbg("can't open '%s' as a rules file", udev_rules_filename);
138 /* loop through the whole file */
142 temp = fgets(line, sizeof(line), fd);
146 dbg_parse("read '%s'", temp);
148 /* eat the whitespace */
149 while (isspace(*temp))
153 if ((*temp == '\0') || (*temp == '\n'))
156 /* see if this is a comment */
157 if (*temp == COMMENT_CHARACTER)
160 memset(&dev, 0x00, sizeof(struct config_device));
162 /* get all known keys */
164 retval = parse_get_pair(&temp, &temp2, &temp3);
168 if (strcasecmp(temp2, FIELD_BUS) == 0) {
169 strfieldcpy(dev.bus, temp3);
173 if (strcasecmp(temp2, FIELD_ID) == 0) {
174 strfieldcpy(dev.id, temp3);
178 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
179 strfieldcpy(dev.place, temp3);
183 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
184 struct sysfs_pair *pair = &dev.sysfs_pair[0];
185 int sysfs_pair_num = 0;
187 /* find first unused pair */
188 while (pair->file[0] != '\0') {
190 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
197 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
199 dbg("error parsing " FIELD_SYSFS " attribute");
202 strfieldcpy(pair->file, attr);
203 strfieldcpy(pair->value, temp3);
208 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
209 strfieldcpy(dev.kernel, temp3);
213 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
215 strfieldcpy(dev.program, temp3);
219 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
220 strfieldcpy(dev.result, temp3);
224 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
225 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
227 if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
228 dbg_parse("creation of partition nodes requested");
229 dev.partitions = PARTITIONS_COUNT;
231 strfieldcpy(dev.name, temp3);
235 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
236 strfieldcpy(dev.symlink, temp3);
240 dbg("unknown type of field '%s'", temp2);
241 dbg("You might be using a rules file in the old format, please fix.");
245 /* simple plausibility check for given keys */
246 if ((dev.sysfs_pair[0].file[0] == '\0') ^
247 (dev.sysfs_pair[0].value[0] == '\0')) {
248 dbg("inconsistency in " FIELD_SYSFS " key");
252 if ((dev.result[0] != '\0') && (program_given == 0)) {
253 dbg(FIELD_RESULT " is only useful when "
254 FIELD_PROGRAM " is called in any rule before");
258 dev.config_line = lineno;
259 retval = add_config_dev(&dev);
261 dbg("add_config_dev returned with error %d", retval);
264 dbg("%s:%d:%d: parse error, rule skipped",
265 udev_rules_filename, lineno, temp - line);
273 int namedev_init_permissions(void)
280 struct perm_device dev;
282 fd = fopen(udev_permissions_filename, "r");
284 dbg("reading '%s' as permissions file", udev_permissions_filename);
286 dbg("can't open '%s' as permissions file", udev_permissions_filename);
290 /* loop through the whole file */
292 temp = fgets(line, sizeof(line), fd);
296 dbg_parse("read '%s'", temp);
298 /* eat the whitespace at the beginning of the line */
299 while (isspace(*temp))
303 if ((*temp == '\0') || (*temp == '\n'))
306 /* see if this is a comment */
307 if (*temp == COMMENT_CHARACTER)
310 memset(&dev, 0x00, sizeof(dev));
313 temp2 = strsep(&temp, ":");
315 dbg("cannot parse line '%s'", line);
318 strncpy(dev.name, temp2, sizeof(dev.name));
320 temp2 = strsep(&temp, ":");
322 dbg("cannot parse line '%s'", line);
325 strncpy(dev.owner, temp2, sizeof(dev.owner));
327 temp2 = strsep(&temp, ":");
329 dbg("cannot parse line '%s'", line);
332 strncpy(dev.group, temp2, sizeof(dev.group));
335 dbg("cannot parse line: %s", line);
338 dev.mode = strtol(temp, NULL, 8);
340 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
341 dev.name, dev.owner, dev.group,
343 retval = add_perm_dev(&dev);
345 dbg("add_perm_dev returned with error %d", retval);