6 * Copyright (C) 2003 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.
24 /* define this to enable parsing debugging */
25 /* #define DEBUG_PARSER */
39 static int add_config_dev(struct config_device *new_dev)
41 struct config_device *tmp_dev;
43 tmp_dev = malloc(sizeof(*tmp_dev));
46 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
47 list_add_tail(&tmp_dev->node, &config_device_list);
48 //dump_config_dev(tmp_dev);
52 int get_pair(char **orig_string, char **left, char **right)
55 char *string = *orig_string;
60 /* eat any whitespace */
61 while (isspace(*string) || *string == ',')
64 /* split based on '=' */
65 temp = strsep(&string, "=");
70 /* take the right side and strip off the '"' */
71 while (isspace(*string))
78 temp = strsep(&string, "\"");
79 if (!string || *temp == '\0')
82 *orig_string = string;
87 void dump_config_dev(struct config_device *dev)
91 dbg_parse("KERNEL name='%s'", dev->name);
94 dbg_parse("LABEL name='%s', bus='%s', sysfs_file[0]='%s', sysfs_value[0]='%s'",
95 dev->name, dev->bus, dev->sysfs_pair[0].file, dev->sysfs_pair[0].value);
98 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
99 dev->name, dev->bus, dev->id);
102 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
103 dev->name, dev->bus, dev->place);
106 dbg_parse("REPLACE name=%s, kernel_name=%s",
107 dev->name, dev->kernel_name);
110 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s'",
111 dev->name, dev->bus, dev->exec_program, dev->id);
114 dbg_parse("unknown type of method");
118 void dump_config_dev_list(void)
120 struct list_head *tmp;
122 list_for_each(tmp, &config_device_list) {
123 struct config_device *dev = list_entry(tmp, struct config_device, node);
124 dump_config_dev(dev);
128 void dump_perm_dev(struct perm_device *dev)
130 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
131 dev->name, dev->owner, dev->group, dev->mode);
134 void dump_perm_dev_list(void)
136 struct list_head *tmp;
138 list_for_each(tmp, &perm_device_list) {
139 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
145 int namedev_init_rules(void)
154 struct config_device dev;
156 fd = fopen(udev_rules_filename, "r");
158 dbg("reading '%s' as rules file", udev_rules_filename);
160 dbg("can't open '%s' as a rules file", udev_rules_filename);
164 /* loop through the whole file */
168 temp = fgets(line, sizeof(line), fd);
172 dbg_parse("read '%s'", temp);
174 /* eat the whitespace */
175 while (isspace(*temp))
179 if ((*temp == '\0') || (*temp == '\n'))
182 /* see if this is a comment */
183 if (*temp == COMMENT_CHARACTER)
186 memset(&dev, 0x00, sizeof(struct config_device));
189 temp2 = strsep(&temp, ",");
191 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
196 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
201 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
206 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
211 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
216 dbg_parse("unknown type of method '%s'", temp2);
219 /* get all known keys */
221 retval = get_pair(&temp, &temp2, &temp3);
225 if (strcasecmp(temp2, FIELD_BUS) == 0) {
226 strfieldcpy(dev.bus, temp3);
230 if (strcasecmp(temp2, FIELD_ID) == 0) {
231 strfieldcpy(dev.id, temp3);
235 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
236 strfieldcpy(dev.place, temp3);
240 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
241 struct sysfs_pair *pair = &dev.sysfs_pair[0];
242 int sysfs_pair_num = 0;
244 /* find first unused pair */
245 while (pair->file[0] != '\0') {
247 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
254 /* remove prepended 'SYSFS_' */
255 strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
256 strfieldcpy(pair->value, temp3);
261 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
262 strfieldcpy(dev.kernel_name, temp3);
266 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
267 strfieldcpy(dev.exec_program, temp3);
271 if (strcasecmp(temp2, FIELD_NAME) == 0) {
272 strfieldcpy(dev.name, temp3);
276 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
277 strfieldcpy(dev.symlink, temp3);
281 dbg_parse("unknown type of field '%s'", temp2);
284 /* check presence of keys according to method type */
287 dbg_parse(TYPE_LABEL " name='%s', bus='%s', "
288 "sysfs_file[0]='%s', sysfs_value[0]='%s', symlink='%s'",
289 dev.name, dev.bus, dev.sysfs_pair[0].file,
290 dev.sysfs_pair[0].value, dev.symlink);
291 if ((*dev.name == '\0') ||
292 (*dev.sysfs_pair[0].file == '\0') ||
293 (*dev.sysfs_pair[0].value == '\0'))
297 dbg_parse(TYPE_NUMBER "name='%s', bus='%s', id='%s', symlink='%s'",
298 dev.name, dev.bus, dev.id, dev.symlink);
299 if ((*dev.name == '\0') ||
300 (*dev.bus == '\0') ||
305 dbg_parse(TYPE_TOPOLOGY "name='%s', bus='%s', "
306 "place='%s', symlink='%s'",
307 dev.name, dev.bus, dev.place, dev.symlink);
308 if ((*dev.name == '\0') ||
309 (*dev.bus == '\0') ||
310 (*dev.place == '\0'))
314 dbg_parse(TYPE_REPLACE "name='%s', kernel_name='%s', symlink='%s'",
315 dev.name, dev.kernel_name, dev.symlink);
316 if ((*dev.name == '\0') ||
317 (*dev.kernel_name == '\0'))
321 dbg_parse(TYPE_CALLOUT "name='%s', bus='%s', program='%s', "
322 "id='%s', symlink='%s'",
323 dev.name, dev.bus, dev.exec_program,
324 dev.id, dev.symlink);
325 if ((*dev.name == '\0') ||
327 (*dev.exec_program == '\0'))
331 dbg_parse("unknown type of method");
335 retval = add_config_dev(&dev);
337 dbg("add_config_dev returned with error %d", retval);
342 dbg_parse("%s:%d:%Zd: field missing or parse error", udev_rules_filename,
343 lineno, temp - line);
349 int namedev_init_permissions(void)
356 struct perm_device dev;
358 fd = fopen(udev_permissions_filename, "r");
360 dbg("reading '%s' as permissions file", udev_permissions_filename);
362 dbg("can't open '%s' as permissions file", udev_permissions_filename);
366 /* loop through the whole file */
368 temp = fgets(line, sizeof(line), fd);
372 dbg_parse("read '%s'", temp);
374 /* eat the whitespace at the beginning of the line */
375 while (isspace(*temp))
379 if ((*temp == '\0') || (*temp == '\n'))
382 /* see if this is a comment */
383 if (*temp == COMMENT_CHARACTER)
386 memset(&dev, 0x00, sizeof(dev));
389 temp2 = strsep(&temp, ":");
391 dbg("cannot parse line '%s'", line);
394 strncpy(dev.name, temp2, sizeof(dev.name));
396 temp2 = strsep(&temp, ":");
398 dbg("cannot parse line '%s'", line);
401 strncpy(dev.owner, temp2, sizeof(dev.owner));
403 temp2 = strsep(&temp, ":");
405 dbg("cannot parse line '%s'", line);
408 strncpy(dev.group, temp2, sizeof(dev.owner));
411 dbg("cannot parse line: %s", line);
414 dev.mode = strtol(temp, NULL, 8);
416 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
417 dev.name, dev.owner, dev.group,
419 retval = add_perm_dev(&dev);
421 dbg("add_perm_dev returned with error %d", retval);