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 */
44 static int add_config_dev(struct config_device *new_dev)
46 struct config_device *tmp_dev;
48 tmp_dev = malloc(sizeof(*tmp_dev));
51 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
52 list_add_tail(&tmp_dev->node, &config_device_list);
53 //dump_config_dev(tmp_dev);
57 void dump_config_dev(struct config_device *dev)
59 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
60 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
61 "kernel='%s', program='%s', result='%s'"
62 "owner='%s', group='%s', mode=%#o",
63 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
64 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
65 dev->kernel, dev->program, dev->result,
66 dev->owner, dev->group, dev->mode);
69 void dump_config_dev_list(void)
71 struct config_device *dev;
73 list_for_each_entry(dev, &config_device_list, node)
77 static int add_perm_dev(struct perm_device *new_dev)
79 struct perm_device *dev;
80 struct perm_device *tmp_dev;
82 /* update the values if we already have the device */
83 list_for_each_entry(dev, &perm_device_list, node) {
84 if (strcmp(new_dev->name, dev->name) != 0)
87 set_empty_perms(dev, new_dev->mode, new_dev->owner, new_dev->group);
91 /* not found, add new structure to the perm list */
92 tmp_dev = malloc(sizeof(*tmp_dev));
96 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
97 list_add_tail(&tmp_dev->node, &perm_device_list);
98 //dump_perm_dev(tmp_dev);
102 void dump_perm_dev(struct perm_device *dev)
104 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
105 dev->name, dev->owner, dev->group, dev->mode);
108 void dump_perm_dev_list(void)
110 struct perm_device *dev;
112 list_for_each_entry(dev, &perm_device_list, node)
116 /* extract possible KEY{attr} or KEY_attr */
117 static char *get_key_attribute(char *str)
122 attr = strchr(str, '{');
125 pos = strchr(attr, '}');
127 dbg("missing closing brace for format");
131 dbg("attribute='%s'", attr);
135 attr = strchr(str, '_');
138 dbg("attribute='%s'", attr);
145 static int namedev_parse_rules(char *filename)
147 char line[LINE_SIZE];
158 int program_given = 0;
161 struct config_device dev;
163 if (file_map(filename, &buf, &bufsize) == 0) {
164 dbg("reading '%s' as rules file", filename);
166 dbg("can't open '%s' as rules file", filename);
170 /* loop through the whole file */
173 while (cur < bufsize) {
174 count = buf_get_line(buf, bufsize, cur);
179 if (count >= LINE_SIZE) {
180 info("line too long, rule skipped %s, line %d",
186 if (bufline[0] == '\0' || bufline[0] == '\n')
189 /* eat the whitespace */
190 while (isspace(bufline[0])) {
195 /* see if this is a comment */
196 if (bufline[0] == COMMENT_CHARACTER)
199 strncpy(line, bufline, count);
201 dbg_parse("read '%s'", line);
203 /* get all known keys */
204 memset(&dev, 0x00, sizeof(struct config_device));
209 retval = parse_get_pair(&temp, &temp2, &temp3);
213 if (strcasecmp(temp2, FIELD_BUS) == 0) {
214 strfieldcpy(dev.bus, temp3);
219 if (strcasecmp(temp2, FIELD_ID) == 0) {
220 strfieldcpy(dev.id, temp3);
225 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
226 strfieldcpy(dev.place, temp3);
231 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
232 struct sysfs_pair *pair = &dev.sysfs_pair[0];
233 int sysfs_pair_num = 0;
235 /* find first unused pair */
236 while (pair->file[0] != '\0') {
238 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
245 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
247 dbg("error parsing " FIELD_SYSFS " attribute");
250 strfieldcpy(pair->file, attr);
251 strfieldcpy(pair->value, temp3);
257 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
258 strfieldcpy(dev.kernel, temp3);
263 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
265 strfieldcpy(dev.program, temp3);
270 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
271 strfieldcpy(dev.result, temp3);
276 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
277 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
278 if (attr != NULL && strcasecmp(attr, ATTR_PARTITIONS) == 0) {
279 dbg_parse("creation of partition nodes requested");
280 dev.partitions = PARTITIONS_COUNT;
282 strfieldcpy(dev.name, temp3);
287 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
288 strfieldcpy(dev.symlink, temp3);
293 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
294 strfieldcpy(dev.owner, temp3);
299 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
300 strfieldcpy(dev.group, temp3);
305 if (strcasecmp(temp2, FIELD_MODE) == 0) {
306 dev.mode = strtol(temp3, NULL, 8);
311 dbg("unknown type of field '%s'", temp2);
315 /* skip line if not any valid key was found */
319 /* simple plausibility checks for given keys */
320 if ((dev.sysfs_pair[0].file[0] == '\0') ^
321 (dev.sysfs_pair[0].value[0] == '\0')) {
322 info("inconsistency in " FIELD_SYSFS " key");
326 if ((dev.result[0] != '\0') && (program_given == 0)) {
327 info(FIELD_RESULT " is only useful when "
328 FIELD_PROGRAM " is called in any rule before");
332 dev.config_line = lineno;
333 strfieldcpy(dev.config_file, filename);
334 retval = add_config_dev(&dev);
336 dbg("add_config_dev returned with error %d", retval);
339 info("parse error %s, line %d:%d, rule skipped",
340 filename, lineno, temp - line);
344 file_unmap(buf, bufsize);
348 static int namedev_parse_permissions(char *filename)
350 char line[LINE_SIZE];
359 struct perm_device dev;
362 if (file_map(filename, &buf, &bufsize) == 0) {
363 dbg("reading '%s' as permissions file", filename);
365 dbg("can't open '%s' as permissions file", filename);
369 /* loop through the whole file */
372 while (cur < bufsize) {
373 count = buf_get_line(buf, bufsize, cur);
378 if (count >= LINE_SIZE) {
379 info("line too long, rule skipped %s, line %d",
385 if (bufline[0] == '\0' || bufline[0] == '\n')
388 /* eat the whitespace */
389 while (isspace(bufline[0])) {
394 /* see if this is a comment */
395 if (bufline[0] == COMMENT_CHARACTER)
398 strncpy(line, bufline, count);
400 dbg_parse("read '%s'", line);
403 memset(&dev, 0x00, sizeof(struct perm_device));
406 temp2 = strsep(&temp, ":");
408 dbg("cannot parse line '%s'", line);
411 strfieldcpy(dev.name, temp2);
413 temp2 = strsep(&temp, ":");
415 dbg("cannot parse line '%s'", line);
418 strfieldcpy(dev.owner, temp2);
420 temp2 = strsep(&temp, ":");
422 dbg("cannot parse line '%s'", line);
425 strfieldcpy(dev.group, temp2);
428 dbg("cannot parse line '%s'", line);
431 dev.mode = strtol(temp, NULL, 8);
433 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
434 dev.name, dev.owner, dev.group, dev.mode);
436 retval = add_perm_dev(&dev);
438 dbg("add_perm_dev returned with error %d", retval);
444 file_unmap(buf, bufsize);
448 int namedev_init_rules()
452 stat(udev_rules_filename, &stats);
453 if ((stats.st_mode & S_IFMT) != S_IFDIR)
454 return namedev_parse_rules(udev_rules_filename);
456 return call_foreach_file(namedev_parse_rules,
457 udev_rules_filename, RULEFILE_SUFFIX);
460 int namedev_init_permissions()
464 stat(udev_permissions_filename, &stats);
465 if ((stats.st_mode & S_IFMT) != S_IFDIR)
466 return namedev_parse_permissions(udev_permissions_filename);
468 return call_foreach_file(namedev_parse_permissions,
469 udev_permissions_filename, PERMFILE_SUFFIX);