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("IGNORE name='%s', kernel_name='%s'",
115 dev->name, dev->kernel_name);
118 dbg_parse("unknown type of method");
122 void dump_config_dev_list(void)
124 struct config_device *dev;
126 list_for_each_entry(dev, &config_device_list, node)
127 dump_config_dev(dev);
130 void dump_perm_dev(struct perm_device *dev)
132 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
133 dev->name, dev->owner, dev->group, dev->mode);
136 void dump_perm_dev_list(void)
138 struct perm_device *dev;
140 list_for_each_entry(dev, &perm_device_list, 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 if (strcasecmp(temp2, TYPE_IGNORE) == 0) {
221 dbg_parse("unknown type of method '%s'", temp2);
224 /* get all known keys */
226 retval = get_pair(&temp, &temp2, &temp3);
230 if (strcasecmp(temp2, FIELD_BUS) == 0) {
231 strfieldcpy(dev.bus, temp3);
235 if (strcasecmp(temp2, FIELD_ID) == 0) {
236 strfieldcpy(dev.id, temp3);
240 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
241 strfieldcpy(dev.place, temp3);
245 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
246 struct sysfs_pair *pair = &dev.sysfs_pair[0];
247 int sysfs_pair_num = 0;
249 /* find first unused pair */
250 while (pair->file[0] != '\0') {
252 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
259 /* remove prepended 'SYSFS_' */
260 strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
261 strfieldcpy(pair->value, temp3);
266 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
267 strfieldcpy(dev.kernel_name, temp3);
271 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
272 strfieldcpy(dev.exec_program, temp3);
276 if (strcasecmp(temp2, FIELD_NAME) == 0) {
277 strfieldcpy(dev.name, temp3);
281 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
282 strfieldcpy(dev.symlink, temp3);
286 dbg_parse("unknown type of field '%s'", temp2);
289 /* check presence of keys according to method type */
292 dbg_parse(TYPE_LABEL " name='%s', bus='%s', "
293 "sysfs_file[0]='%s', sysfs_value[0]='%s', symlink='%s'",
294 dev.name, dev.bus, dev.sysfs_pair[0].file,
295 dev.sysfs_pair[0].value, dev.symlink);
296 if ((*dev.name == '\0') ||
297 (*dev.sysfs_pair[0].file == '\0') ||
298 (*dev.sysfs_pair[0].value == '\0'))
302 dbg_parse(TYPE_NUMBER " name='%s', bus='%s', id='%s', symlink='%s'",
303 dev.name, dev.bus, dev.id, dev.symlink);
304 if ((*dev.name == '\0') ||
305 (*dev.bus == '\0') ||
310 dbg_parse(TYPE_TOPOLOGY " name='%s', bus='%s', "
311 "place='%s', symlink='%s'",
312 dev.name, dev.bus, dev.place, dev.symlink);
313 if ((*dev.name == '\0') ||
314 (*dev.bus == '\0') ||
315 (*dev.place == '\0'))
319 dbg_parse(TYPE_REPLACE " name='%s', kernel_name='%s', symlink='%s'",
320 dev.name, dev.kernel_name, dev.symlink);
321 if ((*dev.name == '\0') ||
322 (*dev.kernel_name == '\0'))
326 dbg_parse(TYPE_CALLOUT " name='%s', bus='%s', program='%s', "
327 "id='%s', symlink='%s'",
328 dev.name, dev.bus, dev.exec_program,
329 dev.id, dev.symlink);
330 if ((*dev.name == '\0') ||
332 (*dev.exec_program == '\0'))
336 dbg_parse(TYPE_IGNORE "name='%s', kernel_name='%s'",
337 dev.name, dev.kernel_name);
338 if ((*dev.kernel_name == '\0'))
342 dbg_parse("unknown type of method");
346 retval = add_config_dev(&dev);
348 dbg("add_config_dev returned with error %d", retval);
353 dbg_parse("%s:%d:%Zd: field missing or parse error", udev_rules_filename,
354 lineno, temp - line);
360 int namedev_init_permissions(void)
367 struct perm_device dev;
369 fd = fopen(udev_permissions_filename, "r");
371 dbg("reading '%s' as permissions file", udev_permissions_filename);
373 dbg("can't open '%s' as permissions file", udev_permissions_filename);
377 /* loop through the whole file */
379 temp = fgets(line, sizeof(line), fd);
383 dbg_parse("read '%s'", temp);
385 /* eat the whitespace at the beginning of the line */
386 while (isspace(*temp))
390 if ((*temp == '\0') || (*temp == '\n'))
393 /* see if this is a comment */
394 if (*temp == COMMENT_CHARACTER)
397 memset(&dev, 0x00, sizeof(dev));
400 temp2 = strsep(&temp, ":");
402 dbg("cannot parse line '%s'", line);
405 strncpy(dev.name, temp2, sizeof(dev.name));
407 temp2 = strsep(&temp, ":");
409 dbg("cannot parse line '%s'", line);
412 strncpy(dev.owner, temp2, sizeof(dev.owner));
414 temp2 = strsep(&temp, ":");
416 dbg("cannot parse line '%s'", line);
419 strncpy(dev.group, temp2, sizeof(dev.owner));
422 dbg("cannot parse line: %s", line);
425 dev.mode = strtol(temp, NULL, 8);
427 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
428 dev.name, dev.owner, dev.group,
430 retval = add_perm_dev(&dev);
432 dbg("add_perm_dev returned with error %d", retval);