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)
89 /*FIXME dump all sysfs's */
90 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
91 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
92 "kernel='%s', program='%s', result='%s'",
93 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
94 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
95 dev->kernel, dev->program, dev->result);
98 void dump_config_dev_list(void)
100 struct config_device *dev;
102 list_for_each_entry(dev, &config_device_list, node)
103 dump_config_dev(dev);
106 void dump_perm_dev(struct perm_device *dev)
108 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
109 dev->name, dev->owner, dev->group, dev->mode);
112 void dump_perm_dev_list(void)
114 struct perm_device *dev;
116 list_for_each_entry(dev, &perm_device_list, node)
121 int namedev_init_rules(void)
129 int program_given = 0;
131 struct config_device dev;
133 fd = fopen(udev_rules_filename, "r");
135 dbg("reading '%s' as rules file", udev_rules_filename);
137 dbg("can't open '%s' as a rules file", udev_rules_filename);
141 /* loop through the whole file */
145 temp = fgets(line, sizeof(line), fd);
149 dbg_parse("read '%s'", temp);
151 /* eat the whitespace */
152 while (isspace(*temp))
156 if ((*temp == '\0') || (*temp == '\n'))
159 /* see if this is a comment */
160 if (*temp == COMMENT_CHARACTER)
163 memset(&dev, 0x00, sizeof(struct config_device));
165 /* get all known keys */
167 retval = get_pair(&temp, &temp2, &temp3);
171 if (strcasecmp(temp2, FIELD_BUS) == 0) {
172 strfieldcpy(dev.bus, temp3);
176 if (strcasecmp(temp2, FIELD_ID) == 0) {
177 strfieldcpy(dev.id, temp3);
181 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
182 strfieldcpy(dev.place, temp3);
186 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
187 struct sysfs_pair *pair = &dev.sysfs_pair[0];
188 int sysfs_pair_num = 0;
190 /* find first unused pair */
191 while (pair->file[0] != '\0') {
193 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
200 /* remove prepended 'SYSFS_' */
201 strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
202 strfieldcpy(pair->value, temp3);
207 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
208 strfieldcpy(dev.kernel, temp3);
212 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
214 strfieldcpy(dev.program, temp3);
218 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
219 strfieldcpy(dev.result, temp3);
223 if (strcasecmp(temp2, FIELD_NAME) == 0) {
224 strfieldcpy(dev.name, temp3);
228 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
229 strfieldcpy(dev.symlink, temp3);
233 dbg_parse("unknown type of field '%s'", temp2);
236 /* simple plausibility check for given keys */
237 if ((dev.sysfs_pair[0].file[0] == '\0') ^
238 (dev.sysfs_pair[0].value[0] == '\0')) {
239 dbg("inconsistency in SYSFS_ key");
243 if ((dev.result[0] != '\0') && (program_given == 0)) {
244 dbg("RESULT is only useful when PROGRAM called in any rule before");
248 retval = add_config_dev(&dev);
250 dbg("add_config_dev returned with error %d", retval);
253 dbg("%s:%d:%Zd: parse error, rule skipped",
254 udev_rules_filename, lineno, temp - line);
262 int namedev_init_permissions(void)
269 struct perm_device dev;
271 fd = fopen(udev_permissions_filename, "r");
273 dbg("reading '%s' as permissions file", udev_permissions_filename);
275 dbg("can't open '%s' as permissions file", udev_permissions_filename);
279 /* loop through the whole file */
281 temp = fgets(line, sizeof(line), fd);
285 dbg_parse("read '%s'", temp);
287 /* eat the whitespace at the beginning of the line */
288 while (isspace(*temp))
292 if ((*temp == '\0') || (*temp == '\n'))
295 /* see if this is a comment */
296 if (*temp == COMMENT_CHARACTER)
299 memset(&dev, 0x00, sizeof(dev));
302 temp2 = strsep(&temp, ":");
304 dbg("cannot parse line '%s'", line);
307 strncpy(dev.name, temp2, sizeof(dev.name));
309 temp2 = strsep(&temp, ":");
311 dbg("cannot parse line '%s'", line);
314 strncpy(dev.owner, temp2, sizeof(dev.owner));
316 temp2 = strsep(&temp, ":");
318 dbg("cannot parse line '%s'", line);
321 strncpy(dev.group, temp2, sizeof(dev.owner));
324 dbg("cannot parse line: %s", line);
327 dev.mode = strtol(temp, NULL, 8);
329 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
330 dev.name, dev.owner, dev.group,
332 retval = add_perm_dev(&dev);
334 dbg("add_perm_dev returned with error %d", retval);