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);
216 dbg_parse("LABEL name='%s', bus='%s', "
217 "sysfs_file='%s', sysfs_value='%s'",
218 dev.name, dev.bus, dev.sysfs_file,
222 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
227 retval = get_value("BUS", &temp, &temp3);
230 strfieldcpy(dev.bus, temp3);
233 temp2 = strsep(&temp, ",");
234 retval = get_value("ID", &temp, &temp3);
237 strfieldcpy(dev.id, temp3);
239 /* NAME="new_name" */
240 temp2 = strsep(&temp, ",");
241 retval = get_value("NAME", &temp, &temp3);
244 strfieldcpy(dev.name, temp3);
246 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
247 dev.name, dev.bus, dev.id);
250 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
255 retval = get_value("BUS", &temp, &temp3);
258 strfieldcpy(dev.bus, temp3);
261 temp2 = strsep(&temp, ",");
262 retval = get_value("PLACE", &temp, &temp3);
265 strfieldcpy(dev.place, temp3);
267 /* NAME="new_name" */
268 temp2 = strsep(&temp, ",");
269 retval = get_value("NAME", &temp, &temp3);
272 strfieldcpy(dev.name, temp3);
274 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
275 dev.name, dev.bus, dev.place);
278 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
282 /* KERNEL="kernel_name" */
283 retval = get_value("KERNEL", &temp, &temp3);
286 strfieldcpy(dev.kernel_name, temp3);
288 /* NAME="new_name" */
289 temp2 = strsep(&temp, ",");
290 retval = get_value("NAME", &temp, &temp3);
293 strfieldcpy(dev.name, temp3);
294 dbg_parse("REPLACE name='%s', kernel_name='%s'",
295 dev.name, dev.kernel_name);
297 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
302 retval = get_value("BUS", &temp, &temp3);
305 strfieldcpy(dev.bus, temp3);
307 /* PROGRAM="executable" */
308 temp2 = strsep(&temp, ",");
309 retval = get_value("PROGRAM", &temp, &temp3);
312 strfieldcpy(dev.exec_program, temp3);
315 temp2 = strsep(&temp, ",");
316 retval = get_value("ID", &temp, &temp3);
319 strfieldcpy(dev.id, temp3);
321 /* NAME="new_name" */
322 temp2 = strsep(&temp, ",");
323 retval = get_value("NAME", &temp, &temp3);
326 strfieldcpy(dev.name, temp3);
327 dbg_parse("CALLOUT name='%s', program='%s'",
328 dev.name, dev.exec_program);
331 retval = add_config_dev(&dev);
333 dbg("add_config_dev returned with error %d", retval);
337 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_rules_filename,
338 lineno, temp - line, temp);
345 int namedev_init_permissions(void)
352 struct perm_device dev;
354 fd = fopen(udev_permissions_filename, "r");
356 dbg("reading '%s' as permissions file", udev_permissions_filename);
358 dbg("can't open '%s' as permissions file", udev_permissions_filename);
362 /* loop through the whole file */
364 temp = fgets(line, sizeof(line), fd);
368 dbg_parse("read '%s'", temp);
370 /* eat the whitespace at the beginning of the line */
371 while (isspace(*temp))
378 /* see if this is a comment */
379 if (*temp == COMMENT_CHARACTER)
382 memset(&dev, 0x00, sizeof(dev));
385 temp2 = strsep(&temp, ":");
387 dbg("cannot parse line '%s'", line);
390 strncpy(dev.name, temp2, sizeof(dev.name));
392 temp2 = strsep(&temp, ":");
394 dbg("cannot parse line '%s'", line);
397 strncpy(dev.owner, temp2, sizeof(dev.owner));
399 temp2 = strsep(&temp, ":");
401 dbg("cannot parse line '%s'", line);
404 strncpy(dev.group, temp2, sizeof(dev.owner));
407 dbg("cannot parse line: %s", line);
410 dev.mode = strtol(temp, NULL, 8);
412 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
413 dev.name, dev.owner, dev.group,
415 retval = add_perm_dev(&dev);
417 dbg("add_config_dev returned with error %d", retval);