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) || *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 void dump_config_dev(struct config_device *dev)
78 dbg_parse("KERNEL name='%s'", dev->name);
81 dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s'",
82 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value);
85 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
86 dev->name, dev->bus, dev->id);
89 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
90 dev->name, dev->bus, dev->place);
93 dbg_parse("REPLACE name=%s, kernel_name=%s",
94 dev->name, dev->kernel_name);
97 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s'",
98 dev->name, dev->bus, dev->exec_program, dev->id);
101 dbg_parse("unknown type of method");
105 void dump_config_dev_list(void)
107 struct list_head *tmp;
109 list_for_each(tmp, &config_device_list) {
110 struct config_device *dev = list_entry(tmp, struct config_device, node);
111 dump_config_dev(dev);
115 void dump_perm_dev(struct perm_device *dev)
117 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
118 dev->name, dev->owner, dev->group, dev->mode);
121 void dump_perm_dev_list(void)
123 struct list_head *tmp;
125 list_for_each(tmp, &perm_device_list) {
126 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
132 int namedev_init_rules(void)
141 struct config_device dev;
143 fd = fopen(udev_rules_filename, "r");
145 dbg("reading '%s' as rules file", udev_rules_filename);
147 dbg("can't open '%s' as a rules file", udev_rules_filename);
151 /* loop through the whole file */
155 temp = fgets(line, sizeof(line), fd);
159 dbg_parse("read '%s'", temp);
165 /* see if this is a comment */
166 if (*temp == COMMENT_CHARACTER)
169 /* eat the whitespace */
170 while (isspace(*temp))
173 memset(&dev, 0x00, sizeof(struct config_device));
176 temp2 = strsep(&temp, ",");
178 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
183 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
188 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
193 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
198 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
203 dbg_parse("unknown type of method '%s'", temp2);
206 /* get all known keys */
208 retval = get_pair(&temp, &temp2, &temp3);
212 if (strcasecmp(temp2, FIELD_BUS) == 0) {
213 strfieldcpy(dev.bus, temp3);
217 if (strcasecmp(temp2, FIELD_ID) == 0) {
218 strfieldcpy(dev.id, temp3);
222 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
223 strfieldcpy(dev.place, temp3);
227 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
228 /* remove prepended 'SYSFS_' */
229 strfieldcpy(dev.sysfs_file, temp2 + sizeof(FIELD_SYSFS)-1);
230 strfieldcpy(dev.sysfs_value, temp3);
234 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
235 strfieldcpy(dev.kernel_name, temp3);
239 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
240 strfieldcpy(dev.exec_program, temp3);
244 if (strcasecmp(temp2, FIELD_NAME) == 0) {
245 strfieldcpy(dev.name, temp3);
249 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
250 strfieldcpy(dev.symlink, temp3);
254 dbg_parse("unknown type of field '%s'", temp2);
257 /* check presence of keys according to method type */
260 dbg_parse("LABEL name='%s', bus='%s', "
261 "sysfs_file='%s', sysfs_value='%s', symlink='%s'",
262 dev.name, dev.bus, dev.sysfs_file,
263 dev.sysfs_value, dev.symlink);
264 if ((*dev.name == '\0') ||
265 (*dev.bus == '\0') ||
266 (*dev.sysfs_file == '\0') ||
267 (*dev.sysfs_value == '\0'))
271 dbg_parse("NUMBER name='%s', bus='%s', id='%s', symlink='%s'",
272 dev.name, dev.bus, dev.id, dev.symlink);
273 if ((*dev.name == '\0') ||
274 (*dev.bus == '\0') ||
279 dbg_parse("TOPOLOGY name='%s', bus='%s', "
280 "place='%s', symlink='%s'",
281 dev.name, dev.bus, dev.place, dev.symlink);
282 if ((*dev.name == '\0') ||
283 (*dev.bus == '\0') ||
284 (*dev.place == '\0'))
288 dbg_parse("REPLACE name='%s', kernel_name='%s', symlink='%s'",
289 dev.name, dev.kernel_name, dev.symlink);
290 if ((*dev.name == '\0') ||
291 (*dev.kernel_name == '\0'))
295 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', "
296 "id='%s', symlink='%s'",
297 dev.name, dev.bus, dev.exec_program,
298 dev.id, dev.symlink);
299 if ((*dev.name == '\0') ||
300 (*dev.bus == '\0') ||
302 (*dev.exec_program == '\0'))
306 dbg_parse("xxx default method");
310 retval = add_config_dev(&dev);
312 dbg("add_config_dev returned with error %d", retval);
317 dbg_parse("%s:%d:%Zd: field missing or parse error", udev_rules_filename,
318 lineno, temp - line);
324 int namedev_init_permissions(void)
331 struct perm_device dev;
333 fd = fopen(udev_permissions_filename, "r");
335 dbg("reading '%s' as permissions file", udev_permissions_filename);
337 dbg("can't open '%s' as permissions file", udev_permissions_filename);
341 /* loop through the whole file */
343 temp = fgets(line, sizeof(line), fd);
347 dbg_parse("read '%s'", temp);
349 /* eat the whitespace at the beginning of the line */
350 while (isspace(*temp))
357 /* see if this is a comment */
358 if (*temp == COMMENT_CHARACTER)
361 memset(&dev, 0x00, sizeof(dev));
364 temp2 = strsep(&temp, ":");
366 dbg("cannot parse line '%s'", line);
369 strncpy(dev.name, temp2, sizeof(dev.name));
371 temp2 = strsep(&temp, ":");
373 dbg("cannot parse line '%s'", line);
376 strncpy(dev.owner, temp2, sizeof(dev.owner));
378 temp2 = strsep(&temp, ":");
380 dbg("cannot parse line '%s'", line);
383 strncpy(dev.group, temp2, sizeof(dev.owner));
386 dbg("cannot parse line: %s", line);
389 dev.mode = strtol(temp, NULL, 8);
391 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
392 dev.name, dev.owner, dev.group,
394 retval = add_perm_dev(&dev);
396 dbg("add_perm_dev returned with error %d", retval);