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 */
39 #include "udev_utils.h"
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 /* if we already have that entry, just update the values */
83 list_for_each_entry(dev, &perm_device_list, node) {
84 if (strcmp(new_dev->name, dev->name) != 0)
87 /* don't overwrite values from earlier entries */
88 if (dev->mode == 0000)
89 dev->mode = new_dev->mode;
90 if (dev->owner[0] == '\0')
91 strfieldcpy(dev->owner, new_dev->owner);
92 if (dev->owner[0] == '\0')
93 strfieldcpy(dev->group, new_dev->group);
98 /* not found, add new structure to the perm list */
99 tmp_dev = malloc(sizeof(*tmp_dev));
103 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
104 list_add_tail(&tmp_dev->node, &perm_device_list);
105 /* dump_perm_dev(tmp_dev); */
110 void dump_perm_dev(struct perm_device *dev)
112 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
113 dev->name, dev->owner, dev->group, dev->mode);
116 void dump_perm_dev_list(void)
118 struct perm_device *dev;
120 list_for_each_entry(dev, &perm_device_list, node)
124 /* extract possible KEY{attr} */
125 static char *get_key_attribute(char *str)
130 attr = strchr(str, '{');
133 pos = strchr(attr, '}');
135 dbg("missing closing brace for format");
139 dbg("attribute='%s'", attr);
146 static int namedev_parse_rules(const char *filename, void *data)
148 char line[LINE_SIZE];
159 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 */
174 while (cur < bufsize) {
175 count = buf_get_line(buf, bufsize, cur);
180 if (count >= LINE_SIZE) {
181 info("line too long, rule skipped %s, line %d",
186 /* eat the whitespace */
187 while ((count > 0) && isspace(bufline[0])) {
194 /* see if this is a comment */
195 if (bufline[0] == COMMENT_CHARACTER)
198 strncpy(line, bufline, count);
200 dbg_parse("read '%s'", line);
202 /* get all known keys */
203 memset(&dev, 0x00, sizeof(struct config_device));
208 retval = parse_get_pair(&temp, &temp2, &temp3);
212 if (strcasecmp(temp2, FIELD_BUS) == 0) {
213 strfieldcpy(dev.bus, temp3);
218 if (strcasecmp(temp2, FIELD_ID) == 0) {
219 strfieldcpy(dev.id, temp3);
224 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
225 strfieldcpy(dev.place, temp3);
230 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
231 struct sysfs_pair *pair = &dev.sysfs_pair[0];
232 int sysfs_pair_num = 0;
234 /* find first unused pair */
235 while (pair->file[0] != '\0') {
237 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
244 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
246 dbg("error parsing " FIELD_SYSFS " attribute");
249 strfieldcpy(pair->file, attr);
250 strfieldcpy(pair->value, temp3);
256 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
257 strfieldcpy(dev.kernel, temp3);
262 if (strcasecmp(temp2, FIELD_SUBSYSTEM) == 0) {
263 strfieldcpy(dev.subsystem, temp3);
268 if (strcasecmp(temp2, FIELD_DRIVER) == 0) {
269 strfieldcpy(dev.driver, temp3);
274 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
276 strfieldcpy(dev.program, temp3);
281 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
282 strfieldcpy(dev.result, temp3);
287 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
288 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
290 if (strstr(attr, ATTR_PARTITIONS) != NULL) {
291 dbg_parse("creation of partition nodes requested");
292 dev.partitions = PARTITIONS_COUNT;
294 if (strstr(attr, ATTR_IGNORE_REMOVE) != NULL) {
295 dbg_parse("remove event should be ignored");
296 dev.ignore_remove = 1;
299 strfieldcpy(dev.name, temp3);
304 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
305 strfieldcpy(dev.symlink, temp3);
310 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
311 strfieldcpy(dev.owner, temp3);
316 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
317 strfieldcpy(dev.group, temp3);
322 if (strcasecmp(temp2, FIELD_MODE) == 0) {
323 dev.mode = strtol(temp3, NULL, 8);
328 dbg("unknown type of field '%s'", temp2);
332 /* skip line if not any valid key was found */
336 /* simple plausibility checks for given keys */
337 if ((dev.sysfs_pair[0].file[0] == '\0') ^
338 (dev.sysfs_pair[0].value[0] == '\0')) {
339 info("inconsistency in " FIELD_SYSFS " key");
343 if ((dev.result[0] != '\0') && (program_given == 0)) {
344 info(FIELD_RESULT " is only useful when "
345 FIELD_PROGRAM " is called in any rule before");
349 dev.config_line = lineno;
350 strfieldcpy(dev.config_file, filename);
351 retval = add_config_dev(&dev);
353 dbg("add_config_dev returned with error %d", retval);
356 info("parse error %s, line %d:%d, rule skipped",
357 filename, lineno, temp - line);
361 file_unmap(buf, bufsize);
365 static int namedev_parse_permissions(const char *filename, void *data)
367 char line[LINE_SIZE];
376 struct perm_device dev;
379 if (file_map(filename, &buf, &bufsize) == 0) {
380 dbg("reading '%s' as permissions file", filename);
382 dbg("can't open '%s' as permissions file", filename);
386 /* loop through the whole file */
389 while (cur < bufsize) {
390 count = buf_get_line(buf, bufsize, cur);
395 if (count >= LINE_SIZE) {
396 info("line too long, rule skipped %s, line %d",
401 /* eat the whitespace */
402 while ((count > 0) && isspace(bufline[0])) {
409 /* see if this is a comment */
410 if (bufline[0] == COMMENT_CHARACTER)
413 strncpy(line, bufline, count);
415 dbg_parse("read '%s'", line);
418 memset(&dev, 0x00, sizeof(struct perm_device));
421 temp2 = strsep(&temp, ":");
423 dbg("cannot parse line '%s'", line);
426 strfieldcpy(dev.name, temp2);
428 temp2 = strsep(&temp, ":");
430 dbg("cannot parse line '%s'", line);
433 strfieldcpy(dev.owner, temp2);
435 temp2 = strsep(&temp, ":");
437 dbg("cannot parse line '%s'", line);
440 strfieldcpy(dev.group, temp2);
443 dbg("cannot parse line '%s'", line);
446 dev.mode = strtol(temp, NULL, 8);
448 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
449 dev.name, dev.owner, dev.group, dev.mode);
451 retval = add_perm_dev(&dev);
453 dbg("add_perm_dev returned with error %d", retval);
459 file_unmap(buf, bufsize);
463 int namedev_init_rules(void)
467 stat(udev_rules_filename, &stats);
468 if ((stats.st_mode & S_IFMT) != S_IFDIR)
469 return namedev_parse_rules(udev_rules_filename, NULL);
471 return call_foreach_file(namedev_parse_rules, udev_rules_filename,
472 RULEFILE_SUFFIX, NULL);
475 int namedev_init_permissions(void)
479 stat(udev_permissions_filename, &stats);
480 if ((stats.st_mode & S_IFMT) != S_IFDIR)
481 return namedev_parse_permissions(udev_permissions_filename, NULL);
483 return call_foreach_file(namedev_parse_permissions, udev_permissions_filename,
484 PERMFILE_SUFFIX, NULL);