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.
25 /* define this to enable parsing debugging also */
26 /* #define DEBUG_PARSER */
41 static int add_config_dev(struct config_device *new_dev)
43 struct config_device *tmp_dev;
45 tmp_dev = malloc(sizeof(*tmp_dev));
48 memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
49 list_add_tail(&tmp_dev->node, &config_device_list);
50 //dump_config_dev(tmp_dev);
54 int get_pair(char **orig_string, char **left, char **right)
57 char *string = *orig_string;
62 /* eat any whitespace */
63 while (isspace(*string) || *string == ',')
66 /* split based on '=' */
67 temp = strsep(&string, "=");
72 /* take the right side and strip off the '"' */
73 while (isspace(*string))
80 temp = strsep(&string, "\"");
81 if (!string || *temp == '\0')
84 *orig_string = string;
89 void dump_config_dev(struct config_device *dev)
91 /*FIXME dump all sysfs's */
92 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
93 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
94 "kernel='%s', program='%s', result='%s'",
95 dev->name, dev->symlink, dev->bus, dev->place, dev->id,
96 dev->sysfs_pair[0].file, dev->sysfs_pair[0].value,
97 dev->kernel, dev->program, dev->result);
100 void dump_config_dev_list(void)
102 struct config_device *dev;
104 list_for_each_entry(dev, &config_device_list, node)
105 dump_config_dev(dev);
108 void dump_perm_dev(struct perm_device *dev)
110 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
111 dev->name, dev->owner, dev->group, dev->mode);
114 void dump_perm_dev_list(void)
116 struct perm_device *dev;
118 list_for_each_entry(dev, &perm_device_list, node)
123 int namedev_init_rules(void)
131 int program_given = 0;
133 struct config_device dev;
135 fd = fopen(udev_rules_filename, "r");
137 dbg("reading '%s' as rules file", udev_rules_filename);
139 dbg("can't open '%s' as a rules file", udev_rules_filename);
143 /* loop through the whole file */
147 temp = fgets(line, sizeof(line), fd);
151 dbg_parse("read '%s'", temp);
153 /* eat the whitespace */
154 while (isspace(*temp))
158 if ((*temp == '\0') || (*temp == '\n'))
161 /* see if this is a comment */
162 if (*temp == COMMENT_CHARACTER)
165 memset(&dev, 0x00, sizeof(struct config_device));
167 /* get all known keys */
169 retval = get_pair(&temp, &temp2, &temp3);
173 if (strcasecmp(temp2, FIELD_BUS) == 0) {
174 strfieldcpy(dev.bus, temp3);
178 if (strcasecmp(temp2, FIELD_ID) == 0) {
179 strfieldcpy(dev.id, temp3);
183 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
184 strfieldcpy(dev.place, temp3);
188 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
189 struct sysfs_pair *pair = &dev.sysfs_pair[0];
190 int sysfs_pair_num = 0;
192 /* find first unused pair */
193 while (pair->file[0] != '\0') {
195 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
202 /* remove prepended 'SYSFS_' */
203 strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
204 strfieldcpy(pair->value, temp3);
209 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
210 strfieldcpy(dev.kernel, temp3);
214 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
216 strfieldcpy(dev.program, temp3);
220 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
221 strfieldcpy(dev.result, temp3);
225 if (strcasecmp(temp2, FIELD_NAME) == 0) {
226 strfieldcpy(dev.name, temp3);
230 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
231 strfieldcpy(dev.symlink, temp3);
235 dbg("unknown type of field '%s'", temp2);
236 dbg("You might be using a rules file in the old format, please fix.");
240 /* simple plausibility check for given keys */
241 if ((dev.sysfs_pair[0].file[0] == '\0') ^
242 (dev.sysfs_pair[0].value[0] == '\0')) {
243 dbg("inconsistency in SYSFS_ key");
247 if ((dev.result[0] != '\0') && (program_given == 0)) {
248 dbg("RESULT is only useful when PROGRAM called in any rule before");
252 retval = add_config_dev(&dev);
254 dbg("add_config_dev returned with error %d", retval);
257 dbg("%s:%d:%d: parse error, rule skipped",
258 udev_rules_filename, lineno, temp - line);
266 int namedev_init_permissions(void)
273 struct perm_device dev;
275 fd = fopen(udev_permissions_filename, "r");
277 dbg("reading '%s' as permissions file", udev_permissions_filename);
279 dbg("can't open '%s' as permissions file", udev_permissions_filename);
283 /* loop through the whole file */
285 temp = fgets(line, sizeof(line), fd);
289 dbg_parse("read '%s'", temp);
291 /* eat the whitespace at the beginning of the line */
292 while (isspace(*temp))
296 if ((*temp == '\0') || (*temp == '\n'))
299 /* see if this is a comment */
300 if (*temp == COMMENT_CHARACTER)
303 memset(&dev, 0x00, sizeof(dev));
306 temp2 = strsep(&temp, ":");
308 dbg("cannot parse line '%s'", line);
311 strncpy(dev.name, temp2, sizeof(dev.name));
313 temp2 = strsep(&temp, ":");
315 dbg("cannot parse line '%s'", line);
318 strncpy(dev.owner, temp2, sizeof(dev.owner));
320 temp2 = strsep(&temp, ":");
322 dbg("cannot parse line '%s'", line);
325 strncpy(dev.group, temp2, sizeof(dev.owner));
328 dbg("cannot parse line: %s", line);
331 dev.mode = strtol(temp, NULL, 8);
333 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
334 dev.name, dev.owner, dev.group,
336 retval = add_perm_dev(&dev);
338 dbg("add_perm_dev returned with error %d", retval);