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 */
47 static int add_config_dev(struct config_device *new_dev)
49 struct config_device *tmp_dev;
51 tmp_dev = malloc(sizeof(*tmp_dev));
54 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
55 list_add_tail(&tmp_dev->node, &config_device_list);
56 //dump_config_dev(tmp_dev);
60 void dump_config_dev(struct config_device *dev)
62 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
63 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
64 "kernel='%s', program='%s', result='%s'",
65 "owner='%s', group='%s', mode=%#o",
66 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
67 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
68 dev->kernel, dev->program, dev->result,;
69 dev->owner, dev->group, dev->mode);
72 void dump_config_dev_list(void)
74 struct config_device *dev;
76 list_for_each_entry(dev, &config_device_list, node)
80 static int add_perm_dev(struct perm_device *new_dev)
82 struct perm_device *dev;
83 struct perm_device *tmp_dev;
85 /* update the values if we already have the device */
86 list_for_each_entry(dev, &perm_device_list, node) {
87 if (strcmp(new_dev->name, dev->name) != 0)
90 set_empty_perms(dev, new_dev->mode, new_dev->owner, new_dev->group);
94 /* not found, add new structure to the perm list */
95 tmp_dev = malloc(sizeof(*tmp_dev));
99 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
100 list_add_tail(&tmp_dev->node, &perm_device_list);
101 //dump_perm_dev(tmp_dev);
105 void dump_perm_dev(struct perm_device *dev)
107 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
108 dev->name, dev->owner, dev->group, dev->mode);
111 void dump_perm_dev_list(void)
113 struct perm_device *dev;
115 list_for_each_entry(dev, &perm_device_list, node)
119 /* extract possible KEY{attr} or KEY_attr */
120 static char *get_key_attribute(char *str)
125 attr = strchr(str, '{');
128 pos = strchr(attr, '}');
130 dbg("missing closing brace for format");
134 dbg("attribute='%s'", attr);
138 attr = strchr(str, '_');
141 dbg("attribute='%s'", attr);
148 static int namedev_parse_rules(char *filename)
157 int program_given = 0;
159 struct config_device dev;
161 fd = fopen(filename, "r");
163 dbg("reading '%s' as rules file", filename);
165 dbg("can't open '%s' as a rules file", filename);
169 /* loop through the whole file */
173 temp = fgets(line, sizeof(line), fd);
177 dbg_parse("read '%s'", temp);
179 /* eat the whitespace */
180 while (isspace(*temp))
184 if ((*temp == '\0') || (*temp == '\n'))
187 /* see if this is a comment */
188 if (*temp == COMMENT_CHARACTER)
191 memset(&dev, 0x00, sizeof(struct config_device));
193 /* get all known keys */
195 retval = parse_get_pair(&temp, &temp2, &temp3);
199 if (strcasecmp(temp2, FIELD_BUS) == 0) {
200 strfieldcpy(dev.bus, temp3);
204 if (strcasecmp(temp2, FIELD_ID) == 0) {
205 strfieldcpy(dev.id, temp3);
209 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
210 strfieldcpy(dev.place, temp3);
214 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
215 struct sysfs_pair *pair = &dev.sysfs_pair[0];
216 int sysfs_pair_num = 0;
218 /* find first unused pair */
219 while (pair->file[0] != '\0') {
221 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
228 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
230 dbg("error parsing " FIELD_SYSFS " attribute");
233 strfieldcpy(pair->file, attr);
234 strfieldcpy(pair->value, temp3);
239 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
240 strfieldcpy(dev.kernel, temp3);
244 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
246 strfieldcpy(dev.program, temp3);
250 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
251 strfieldcpy(dev.result, temp3);
255 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
256 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
258 if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
259 dbg_parse("creation of partition nodes requested");
260 dev.partitions = PARTITIONS_COUNT;
262 strfieldcpy(dev.name, temp3);
266 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
267 strfieldcpy(dev.symlink, temp3);
271 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
272 strfieldcpy(dev.owner, temp3);
276 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
277 strfieldcpy(dev.group, temp3);
281 if (strcasecmp(temp2, FIELD_MODE) == 0) {
282 dev.mode = strtol(temp3, NULL, 8);
286 dbg("unknown type of field '%s'", temp2);
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 dbg("inconsistency in " FIELD_SYSFS " key");
297 if ((dev.result[0] != '\0') && (program_given == 0)) {
298 dbg(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 dbg("%s:%d:%d: parse error, rule skipped",
311 filename, lineno, temp - line);
319 static int namedev_parse_permissions(char *filename)
326 struct perm_device dev;
328 fd = fopen(filename, "r");
330 dbg("reading '%s' as permissions file", filename);
332 dbg("can't open '%s' as permissions file", filename);
336 /* loop through the whole file */
338 temp = fgets(line, sizeof(line), fd);
342 dbg_parse("read '%s'", temp);
344 /* eat the whitespace at the beginning of the line */
345 while (isspace(*temp))
349 if ((*temp == '\0') || (*temp == '\n'))
352 /* see if this is a comment */
353 if (*temp == COMMENT_CHARACTER)
356 memset(&dev, 0x00, sizeof(dev));
359 temp2 = strsep(&temp, ":");
361 dbg("cannot parse line '%s'", line);
364 strfieldcpy(dev.name, temp2);
366 temp2 = strsep(&temp, ":");
368 dbg("cannot parse line '%s'", line);
371 strfieldcpy(dev.owner, temp2);
373 temp2 = strsep(&temp, ":");
375 dbg("cannot parse line '%s'", line);
378 strfieldcpy(dev.group, temp2);
381 dbg("cannot parse line '%s'", line);
384 dev.mode = strtol(temp, NULL, 8);
386 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
387 dev.name, dev.owner, dev.group, dev.mode);
389 retval = add_perm_dev(&dev);
391 dbg("add_perm_dev returned with error %d", retval);
402 struct list_head list;
403 char name[NAME_SIZE];
406 /* sort files in lexical order */
407 static int file_list_insert(char *filename)
409 struct files *loop_file;
410 struct files *new_file;
412 list_for_each_entry(loop_file, &file_list, list) {
413 if (strcmp(loop_file->name, filename) > 0) {
418 new_file = malloc(sizeof(struct files));
419 if (new_file == NULL) {
424 strfieldcpy(new_file->name, filename);
425 list_add_tail(&new_file->list, &loop_file->list);
429 /* calls function for file or every file found in directory */
430 static int call_foreach_file(int parser (char *f) , char *filename, char *extension)
435 char file[NAME_SIZE];
437 struct files *loop_file;
438 struct files *tmp_file;
440 /* look if we have a plain file or a directory to scan */
441 stat(filename, &stats);
442 if ((stats.st_mode & S_IFMT) != S_IFDIR)
443 return parser(filename);
445 /* sort matching filename into list */
446 dbg("open config as directory '%s'", filename);
447 dir = opendir(filename);
450 if (ent == NULL || ent->d_name[0] == '\0')
453 dbg("found file '%s'", ent->d_name);
454 ext = strrchr(ent->d_name, '.');
458 if (strcmp(ext, extension) == 0) {
459 dbg("put file in list '%s'", ent->d_name);
460 file_list_insert(ent->d_name);
464 /* parse every file in the list */
465 list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
466 strfieldcpy(file, filename);
467 strfieldcat(file, loop_file->name);
469 list_del(&loop_file->list);
477 int namedev_init_rules()
479 return call_foreach_file(namedev_parse_rules, udev_rules_filename, RULEFILE_EXT);
482 int namedev_init_permissions()
484 return call_foreach_file(namedev_parse_permissions, udev_permissions_filename, PERMFILE_EXT);