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 int get_pair(char **orig_string, char **left, char **right)
42 char *string = *orig_string;
47 /* eat any whitespace */
48 while (isspace(*string))
51 /* split based on '=' */
52 temp = strsep(&string, "=");
57 /* take the right side and strip off the '"' */
58 while (isspace(*string))
65 temp = strsep(&string, "\"");
66 if (!string || *temp == '\0')
69 *orig_string = string;
74 static int get_value(const char *left, char **orig_string, char **ret_string)
79 retval = get_pair(orig_string, &left_string, ret_string);
82 if (strcasecmp(left_string, left) != 0)
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='%s', sysfs_value='%s'",
95 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_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);
173 dbg_parse("read '%s'", temp);
175 /* eat the whitespace at the beginning of the line */
176 while (isspace(*temp))
183 /* see if this is a comment */
184 if (*temp == COMMENT_CHARACTER)
187 memset(&dev, 0x00, sizeof(struct config_device));
190 temp2 = strsep(&temp, ",");
191 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
196 retval = get_value("BUS", &temp, &temp3);
199 strfieldcpy(dev.bus, temp3);
202 temp2 = strsep(&temp, ",");
203 retval = get_pair(&temp, &temp2, &temp3);
206 strfieldcpy(dev.sysfs_file, temp2);
207 strfieldcpy(dev.sysfs_value, temp3);
209 /* NAME="new_name" */
210 temp2 = strsep(&temp, ",");
211 retval = get_value("NAME", &temp, &temp3);
214 strfieldcpy(dev.name, temp3);
217 temp2 = strsep(&temp, ",");
218 retval = get_value("SYMLINK", &temp, &temp3);
220 strfieldcpy(dev.symlink, temp3);
222 dbg_parse("LABEL name='%s', bus='%s', "
223 "sysfs_file='%s', sysfs_value='%s', symlink='%s'",
224 dev.name, dev.bus, dev.sysfs_file,
225 dev.sysfs_value, dev.symlink);
228 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
233 retval = get_value("BUS", &temp, &temp3);
236 strfieldcpy(dev.bus, temp3);
239 temp2 = strsep(&temp, ",");
240 retval = get_value("ID", &temp, &temp3);
243 strfieldcpy(dev.id, temp3);
245 /* NAME="new_name" */
246 temp2 = strsep(&temp, ",");
247 retval = get_value("NAME", &temp, &temp3);
250 strfieldcpy(dev.name, temp3);
253 temp2 = strsep(&temp, ",");
254 retval = get_value("SYMLINK", &temp, &temp3);
256 strfieldcpy(dev.symlink, temp3);
258 dbg_parse("NUMBER name='%s', bus='%s', id='%s', symlink='%s'",
259 dev.name, dev.bus, dev.id, dev.symlink);
262 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
267 retval = get_value("BUS", &temp, &temp3);
270 strfieldcpy(dev.bus, temp3);
273 temp2 = strsep(&temp, ",");
274 retval = get_value("PLACE", &temp, &temp3);
277 strfieldcpy(dev.place, temp3);
279 /* NAME="new_name" */
280 temp2 = strsep(&temp, ",");
281 retval = get_value("NAME", &temp, &temp3);
284 strfieldcpy(dev.name, temp3);
287 temp2 = strsep(&temp, ",");
288 retval = get_value("SYMLINK", &temp, &temp3);
290 strfieldcpy(dev.symlink, temp3);
292 dbg_parse("TOPOLOGY name='%s', bus='%s', "
293 "place='%s', symlink='%s'",
294 dev.name, dev.bus, dev.place, dev.symlink);
297 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
301 /* KERNEL="kernel_name" */
302 retval = get_value("KERNEL", &temp, &temp3);
305 strfieldcpy(dev.kernel_name, temp3);
307 /* NAME="new_name" */
308 temp2 = strsep(&temp, ",");
309 retval = get_value("NAME", &temp, &temp3);
312 strfieldcpy(dev.name, temp3);
315 temp2 = strsep(&temp, ",");
316 retval = get_value("SYMLINK", &temp, &temp3);
318 strfieldcpy(dev.symlink, temp3);
320 dbg_parse("REPLACE name='%s', kernel_name='%s', symlink='%s'",
321 dev.name, dev.kernel_name, dev.symlink);
324 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
329 retval = get_value("BUS", &temp, &temp3);
332 strfieldcpy(dev.bus, temp3);
334 /* PROGRAM="executable" */
335 temp2 = strsep(&temp, ",");
336 retval = get_value("PROGRAM", &temp, &temp3);
339 strfieldcpy(dev.exec_program, temp3);
342 temp2 = strsep(&temp, ",");
343 retval = get_value("ID", &temp, &temp3);
346 strfieldcpy(dev.id, temp3);
348 /* NAME="new_name" */
349 temp2 = strsep(&temp, ",");
350 retval = get_value("NAME", &temp, &temp3);
353 strfieldcpy(dev.name, temp3);
356 temp2 = strsep(&temp, ",");
357 retval = get_value("SYMLINK", &temp, &temp3);
359 strfieldcpy(dev.symlink, temp3);
361 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', "
362 "id='%s', symlink='%s'",
363 dev.name, dev.bus, dev.exec_program,
364 dev.id, dev.symlink);
367 retval = add_config_dev(&dev);
369 dbg("add_config_dev returned with error %d", retval);
373 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_rules_filename,
374 lineno, temp - line, temp);
381 int namedev_init_permissions(void)
388 struct perm_device dev;
390 fd = fopen(udev_permissions_filename, "r");
392 dbg("reading '%s' as permissions file", udev_permissions_filename);
394 dbg("can't open '%s' as permissions file", udev_permissions_filename);
398 /* loop through the whole file */
400 temp = fgets(line, sizeof(line), fd);
404 dbg_parse("read '%s'", temp);
406 /* eat the whitespace at the beginning of the line */
407 while (isspace(*temp))
414 /* see if this is a comment */
415 if (*temp == COMMENT_CHARACTER)
418 memset(&dev, 0x00, sizeof(dev));
421 temp2 = strsep(&temp, ":");
423 dbg("cannot parse line '%s'", line);
426 strncpy(dev.name, temp2, sizeof(dev.name));
428 temp2 = strsep(&temp, ":");
430 dbg("cannot parse line '%s'", line);
433 strncpy(dev.owner, temp2, sizeof(dev.owner));
435 temp2 = strsep(&temp, ":");
437 dbg("cannot parse line '%s'", line);
440 strncpy(dev.group, temp2, sizeof(dev.owner));
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,
451 retval = add_perm_dev(&dev);
453 dbg("add_perm_dev returned with error %d", retval);