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} */
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);
138 static int namedev_parse_rules(const char *filename, void *data)
140 char line[LINE_SIZE];
151 int program_given = 0;
154 struct config_device dev;
156 if (file_map(filename, &buf, &bufsize) == 0) {
157 dbg("reading '%s' as rules file", filename);
159 dbg("can't open '%s' as rules file", filename);
163 /* loop through the whole file */
166 while (cur < bufsize) {
167 count = buf_get_line(buf, bufsize, cur);
172 if (count >= LINE_SIZE) {
173 info("line too long, rule skipped %s, line %d",
178 /* eat the whitespace */
179 while ((count > 0) && isspace(bufline[0])) {
186 /* see if this is a comment */
187 if (bufline[0] == COMMENT_CHARACTER)
190 strncpy(line, bufline, count);
192 dbg_parse("read '%s'", line);
194 /* get all known keys */
195 memset(&dev, 0x00, sizeof(struct config_device));
200 retval = parse_get_pair(&temp, &temp2, &temp3);
204 if (strcasecmp(temp2, FIELD_BUS) == 0) {
205 strfieldcpy(dev.bus, temp3);
210 if (strcasecmp(temp2, FIELD_ID) == 0) {
211 strfieldcpy(dev.id, temp3);
216 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
217 strfieldcpy(dev.place, temp3);
222 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
223 struct sysfs_pair *pair = &dev.sysfs_pair[0];
224 int sysfs_pair_num = 0;
226 /* find first unused pair */
227 while (pair->file[0] != '\0') {
229 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
236 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
238 dbg("error parsing " FIELD_SYSFS " attribute");
241 strfieldcpy(pair->file, attr);
242 strfieldcpy(pair->value, temp3);
248 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
249 strfieldcpy(dev.kernel, temp3);
254 if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
255 strfieldcpy(dev.subsystem, temp3);
260 if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
261 strfieldcpy(dev.driver, temp3);
266 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
268 strfieldcpy(dev.program, temp3);
273 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
274 strfieldcpy(dev.result, temp3);
279 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
280 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
281 if (attr != NULL && strcasecmp(attr, ATTR_PARTITIONS) == 0) {
282 dbg_parse("creation of partition nodes requested");
283 dev.partitions = PARTITIONS_COUNT;
285 strfieldcpy(dev.name, temp3);
290 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
291 strfieldcpy(dev.symlink, temp3);
296 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
297 strfieldcpy(dev.owner, temp3);
302 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
303 strfieldcpy(dev.group, temp3);
308 if (strcasecmp(temp2, FIELD_MODE) == 0) {
309 dev.mode = strtol(temp3, NULL, 8);
314 dbg("unknown type of field '%s'", temp2);
318 /* skip line if not any valid key was found */
322 /* simple plausibility checks for given keys */
323 if ((dev.sysfs_pair[0].file[0] == '\0') ^
324 (dev.sysfs_pair[0].value[0] == '\0')) {
325 info("inconsistency in " FIELD_SYSFS " key");
329 if ((dev.result[0] != '\0') && (program_given == 0)) {
330 info(FIELD_RESULT " is only useful when "
331 FIELD_PROGRAM " is called in any rule before");
335 dev.config_line = lineno;
336 strfieldcpy(dev.config_file, filename);
337 retval = add_config_dev(&dev);
339 dbg("add_config_dev returned with error %d", retval);
342 info("parse error %s, line %d:%d, rule skipped",
343 filename, lineno, temp - line);
347 file_unmap(buf, bufsize);
351 static int namedev_parse_permissions(const char *filename, void *data)
353 char line[LINE_SIZE];
362 struct perm_device dev;
365 if (file_map(filename, &buf, &bufsize) == 0) {
366 dbg("reading '%s' as permissions file", filename);
368 dbg("can't open '%s' as permissions file", filename);
372 /* loop through the whole file */
375 while (cur < bufsize) {
376 count = buf_get_line(buf, bufsize, cur);
381 if (count >= LINE_SIZE) {
382 info("line too long, rule skipped %s, line %d",
387 /* eat the whitespace */
388 while ((count > 0) && isspace(bufline[0])) {
395 /* see if this is a comment */
396 if (bufline[0] == COMMENT_CHARACTER)
399 strncpy(line, bufline, count);
401 dbg_parse("read '%s'", line);
404 memset(&dev, 0x00, sizeof(struct perm_device));
407 temp2 = strsep(&temp, ":");
409 dbg("cannot parse line '%s'", line);
412 strfieldcpy(dev.name, temp2);
414 temp2 = strsep(&temp, ":");
416 dbg("cannot parse line '%s'", line);
419 strfieldcpy(dev.owner, temp2);
421 temp2 = strsep(&temp, ":");
423 dbg("cannot parse line '%s'", line);
426 strfieldcpy(dev.group, temp2);
429 dbg("cannot parse line '%s'", line);
432 dev.mode = strtol(temp, NULL, 8);
434 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
435 dev.name, dev.owner, dev.group, dev.mode);
437 retval = add_perm_dev(&dev);
439 dbg("add_perm_dev returned with error %d", retval);
445 file_unmap(buf, bufsize);
449 int namedev_init_rules(void)
453 stat(udev_rules_filename, &stats);
454 if ((stats.st_mode & S_IFMT) != S_IFDIR)
455 return namedev_parse_rules(udev_rules_filename, NULL);
457 return call_foreach_file(namedev_parse_rules, udev_rules_filename,
458 RULEFILE_SUFFIX, NULL);
461 int namedev_init_permissions(void)
465 stat(udev_permissions_filename, &stats);
466 if ((stats.st_mode & S_IFMT) != S_IFDIR)
467 return namedev_parse_permissions(udev_permissions_filename, NULL);
469 return call_foreach_file(namedev_parse_permissions, udev_permissions_filename,
470 PERMFILE_SUFFIX, NULL);