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)
160 int program_given = 0;
162 struct config_device dev;
164 if (file_map(filename, &buf, &bufsize) == 0) {
165 dbg("reading '%s' as rules file", filename);
167 dbg("can't open '%s' as rules file", filename);
171 /* loop through the whole file */
175 count = buf_get_line(buf, bufsize, cur);
177 strncpy(line, buf + cur, count);
186 dbg_parse("read '%s'", temp);
188 /* eat the whitespace */
189 while (isspace(*temp))
193 if ((*temp == '\0') || (*temp == '\n'))
196 /* see if this is a comment */
197 if (*temp == COMMENT_CHARACTER)
200 memset(&dev, 0x00, sizeof(struct config_device));
202 /* get all known keys */
204 retval = parse_get_pair(&temp, &temp2, &temp3);
208 if (strcasecmp(temp2, FIELD_BUS) == 0) {
209 strfieldcpy(dev.bus, temp3);
213 if (strcasecmp(temp2, FIELD_ID) == 0) {
214 strfieldcpy(dev.id, temp3);
218 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
219 strfieldcpy(dev.place, temp3);
223 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
224 struct sysfs_pair *pair = &dev.sysfs_pair[0];
225 int sysfs_pair_num = 0;
227 /* find first unused pair */
228 while (pair->file[0] != '\0') {
230 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
237 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
239 dbg("error parsing " FIELD_SYSFS " attribute");
242 strfieldcpy(pair->file, attr);
243 strfieldcpy(pair->value, temp3);
248 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
249 strfieldcpy(dev.kernel, temp3);
253 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
255 strfieldcpy(dev.program, temp3);
259 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
260 strfieldcpy(dev.result, temp3);
264 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
265 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
267 if (strcasecmp(attr, ATTR_PARTITIONS) == 0) {
268 dbg_parse("creation of partition nodes requested");
269 dev.partitions = PARTITIONS_COUNT;
271 strfieldcpy(dev.name, temp3);
275 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
276 strfieldcpy(dev.symlink, temp3);
280 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
281 strfieldcpy(dev.owner, temp3);
285 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
286 strfieldcpy(dev.group, temp3);
290 if (strcasecmp(temp2, FIELD_MODE) == 0) {
291 dev.mode = strtol(temp3, NULL, 8);
295 dbg("unknown type of field '%s'", temp2);
299 /* simple plausibility checks for given keys */
300 if ((dev.sysfs_pair[0].file[0] == '\0') ^
301 (dev.sysfs_pair[0].value[0] == '\0')) {
302 dbg("inconsistency in " FIELD_SYSFS " key");
306 if ((dev.result[0] != '\0') && (program_given == 0)) {
307 dbg(FIELD_RESULT " is only useful when "
308 FIELD_PROGRAM " is called in any rule before");
312 dev.config_line = lineno;
313 strfieldcpy(dev.config_file, filename);
314 retval = add_config_dev(&dev);
316 dbg("add_config_dev returned with error %d", retval);
319 dbg("%s:%d:%d: parse error, rule skipped",
320 filename, lineno, temp - line);
324 file_unmap(buf, bufsize);
328 static int namedev_parse_permissions(char *filename)
338 struct perm_device dev;
340 if (file_map(filename, &buf, &bufsize) == 0) {
341 dbg("reading '%s' as permissions file", filename);
343 dbg("can't open '%s' as permissions file", filename);
347 /* loop through the whole file */
350 count = buf_get_line(buf, bufsize, cur);
352 strncpy(line, buf + cur, count);
360 dbg_parse("read '%s'", temp);
362 /* eat the whitespace at the beginning of the line */
363 while (isspace(*temp))
367 if ((*temp == '\0') || (*temp == '\n'))
370 /* see if this is a comment */
371 if (*temp == COMMENT_CHARACTER)
374 memset(&dev, 0x00, sizeof(dev));
377 temp2 = strsep(&temp, ":");
379 dbg("cannot parse line '%s'", line);
382 strfieldcpy(dev.name, temp2);
384 temp2 = strsep(&temp, ":");
386 dbg("cannot parse line '%s'", line);
389 strfieldcpy(dev.owner, temp2);
391 temp2 = strsep(&temp, ":");
393 dbg("cannot parse line '%s'", line);
396 strfieldcpy(dev.group, temp2);
399 dbg("cannot parse line '%s'", line);
402 dev.mode = strtol(temp, NULL, 8);
404 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
405 dev.name, dev.owner, dev.group, dev.mode);
407 retval = add_perm_dev(&dev);
409 dbg("add_perm_dev returned with error %d", retval);
415 file_unmap(buf, bufsize);
420 struct list_head list;
421 char name[NAME_SIZE];
424 /* sort files in lexical order */
425 static int file_list_insert(char *filename)
427 struct files *loop_file;
428 struct files *new_file;
430 list_for_each_entry(loop_file, &file_list, list) {
431 if (strcmp(loop_file->name, filename) > 0) {
436 new_file = malloc(sizeof(struct files));
437 if (new_file == NULL) {
442 strfieldcpy(new_file->name, filename);
443 list_add_tail(&new_file->list, &loop_file->list);
447 /* calls function for file or every file found in directory */
448 static int call_foreach_file(int parser (char *f) , char *filename, char *extension)
453 char file[NAME_SIZE];
455 struct files *loop_file;
456 struct files *tmp_file;
458 /* look if we have a plain file or a directory to scan */
459 stat(filename, &stats);
460 if ((stats.st_mode & S_IFMT) != S_IFDIR)
461 return parser(filename);
463 /* sort matching filename into list */
464 dbg("open config as directory '%s'", filename);
465 dir = opendir(filename);
468 if (ent == NULL || ent->d_name[0] == '\0')
471 dbg("found file '%s'", ent->d_name);
472 ext = strrchr(ent->d_name, '.');
476 if (strcmp(ext, extension) == 0) {
477 dbg("put file in list '%s'", ent->d_name);
478 file_list_insert(ent->d_name);
482 /* parse every file in the list */
483 list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
484 strfieldcpy(file, filename);
485 strfieldcat(file, loop_file->name);
487 list_del(&loop_file->list);
495 int namedev_init_rules()
497 return call_foreach_file(namedev_parse_rules, udev_rules_filename, RULEFILE_EXT);
500 int namedev_init_permissions()
502 return call_foreach_file(namedev_parse_permissions, udev_permissions_filename, PERMFILE_EXT);