6 * Copyright (C) 2003,2004 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 */
44 static int add_config_dev(struct config_device
*new_dev
)
46 struct config_device
*tmp_dev
;
48 tmp_dev
= malloc(sizeof(*tmp_dev
));
51 memcpy(tmp_dev
, new_dev
, sizeof(*tmp_dev
));
52 list_add_tail(&tmp_dev
->node
, &config_device_list
);
53 //dump_config_dev(tmp_dev);
57 void dump_config_dev(struct config_device
*dev
)
59 dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', "
60 "sysfs_file[0]='%s', sysfs_value[0]='%s', "
61 "kernel='%s', program='%s', result='%s'",
62 "owner='%s', group='%s', mode=%#o",
63 dev
->name
, dev
->symlink
, dev
->bus
, dev
->place
, dev
->id
,
64 dev
->sysfs_pair
[0].file
, dev
->sysfs_pair
[0].value
,
65 dev
->kernel
, dev
->program
, dev
->result
,
66 dev
->owner
, dev
->group
, dev
->mode
);
69 void dump_config_dev_list(void)
71 struct config_device
*dev
;
73 list_for_each_entry(dev
, &config_device_list
, node
)
77 static int add_perm_dev(struct perm_device
*new_dev
)
79 struct perm_device
*dev
;
80 struct perm_device
*tmp_dev
;
82 /* update the values if we already have the device */
83 list_for_each_entry(dev
, &perm_device_list
, node
) {
84 if (strcmp(new_dev
->name
, dev
->name
) != 0)
87 set_empty_perms(dev
, new_dev
->mode
, new_dev
->owner
, new_dev
->group
);
91 /* not found, add new structure to the perm list */
92 tmp_dev
= malloc(sizeof(*tmp_dev
));
96 memcpy(tmp_dev
, new_dev
, sizeof(*tmp_dev
));
97 list_add_tail(&tmp_dev
->node
, &perm_device_list
);
98 //dump_perm_dev(tmp_dev);
102 void dump_perm_dev(struct perm_device
*dev
)
104 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
105 dev
->name
, dev
->owner
, dev
->group
, dev
->mode
);
108 void dump_perm_dev_list(void)
110 struct perm_device
*dev
;
112 list_for_each_entry(dev
, &perm_device_list
, node
)
116 /* extract possible KEY{attr} or KEY_attr */
117 static char *get_key_attribute(char *str
)
122 attr
= strchr(str
, '{');
125 pos
= strchr(attr
, '}');
127 dbg("missing closing brace for format");
131 dbg("attribute='%s'", attr
);
135 attr
= strchr(str
, '_');
138 dbg("attribute='%s'", attr
);
145 static int namedev_parse_rules(char *filename
)
157 int program_given
= 0;
159 struct config_device dev
;
161 if (file_map(filename
, &buf
, &bufsize
) == 0) {
162 dbg("reading '%s' as rules file", filename
);
164 dbg("can't open '%s' as rules file", filename
);
168 /* loop through the whole file */
172 count
= buf_get_line(buf
, bufsize
, cur
);
174 strncpy(line
, buf
+ cur
, count
);
183 dbg_parse("read '%s'", temp
);
185 /* eat the whitespace */
186 while (isspace(*temp
))
190 if ((*temp
== '\0') || (*temp
== '\n'))
193 /* see if this is a comment */
194 if (*temp
== COMMENT_CHARACTER
)
197 memset(&dev
, 0x00, sizeof(struct config_device
));
199 /* get all known keys */
201 retval
= parse_get_pair(&temp
, &temp2
, &temp3
);
205 if (strcasecmp(temp2
, FIELD_BUS
) == 0) {
206 strfieldcpy(dev
.bus
, temp3
);
210 if (strcasecmp(temp2
, FIELD_ID
) == 0) {
211 strfieldcpy(dev
.id
, temp3
);
215 if (strcasecmp(temp2
, FIELD_PLACE
) == 0) {
216 strfieldcpy(dev
.place
, temp3
);
220 if (strncasecmp(temp2
, FIELD_SYSFS
, sizeof(FIELD_SYSFS
)-1) == 0) {
221 struct sysfs_pair
*pair
= &dev
.sysfs_pair
[0];
222 int sysfs_pair_num
= 0;
224 /* find first unused pair */
225 while (pair
->file
[0] != '\0') {
227 if (sysfs_pair_num
>= MAX_SYSFS_PAIRS
) {
234 attr
= get_key_attribute(temp2
+ sizeof(FIELD_SYSFS
)-1);
236 dbg("error parsing " FIELD_SYSFS
" attribute");
239 strfieldcpy(pair
->file
, attr
);
240 strfieldcpy(pair
->value
, temp3
);
245 if (strcasecmp(temp2
, FIELD_KERNEL
) == 0) {
246 strfieldcpy(dev
.kernel
, temp3
);
250 if (strcasecmp(temp2
, FIELD_PROGRAM
) == 0) {
252 strfieldcpy(dev
.program
, temp3
);
256 if (strcasecmp(temp2
, FIELD_RESULT
) == 0) {
257 strfieldcpy(dev
.result
, temp3
);
261 if (strncasecmp(temp2
, FIELD_NAME
, sizeof(FIELD_NAME
)-1) == 0) {
262 attr
= get_key_attribute(temp2
+ sizeof(FIELD_NAME
)-1);
264 if (strcasecmp(attr
, ATTR_PARTITIONS
) == 0) {
265 dbg_parse("creation of partition nodes requested");
266 dev
.partitions
= PARTITIONS_COUNT
;
268 strfieldcpy(dev
.name
, temp3
);
272 if (strcasecmp(temp2
, FIELD_SYMLINK
) == 0) {
273 strfieldcpy(dev
.symlink
, temp3
);
277 if (strcasecmp(temp2
, FIELD_OWNER
) == 0) {
278 strfieldcpy(dev
.owner
, temp3
);
282 if (strcasecmp(temp2
, FIELD_GROUP
) == 0) {
283 strfieldcpy(dev
.group
, temp3
);
287 if (strcasecmp(temp2
, FIELD_MODE
) == 0) {
288 dev
.mode
= strtol(temp3
, NULL
, 8);
292 dbg("unknown type of field '%s'", temp2
);
296 /* simple plausibility checks for given keys */
297 if ((dev
.sysfs_pair
[0].file
[0] == '\0') ^
298 (dev
.sysfs_pair
[0].value
[0] == '\0')) {
299 dbg("inconsistency in " FIELD_SYSFS
" key");
303 if ((dev
.result
[0] != '\0') && (program_given
== 0)) {
304 dbg(FIELD_RESULT
" is only useful when "
305 FIELD_PROGRAM
" is called in any rule before");
309 dev
.config_line
= lineno
;
310 strfieldcpy(dev
.config_file
, filename
);
311 retval
= add_config_dev(&dev
);
313 dbg("add_config_dev returned with error %d", retval
);
316 dbg("%s:%d:%d: parse error, rule skipped",
317 filename
, lineno
, temp
- line
);
321 file_unmap(buf
, bufsize
);
325 static int namedev_parse_permissions(char *filename
)
335 struct perm_device dev
;
337 if (file_map(filename
, &buf
, &bufsize
) == 0) {
338 dbg("reading '%s' as permissions file", filename
);
340 dbg("can't open '%s' as permissions file", filename
);
344 /* loop through the whole file */
347 count
= buf_get_line(buf
, bufsize
, cur
);
349 strncpy(line
, buf
+ cur
, count
);
357 dbg_parse("read '%s'", temp
);
359 /* eat the whitespace at the beginning of the line */
360 while (isspace(*temp
))
364 if ((*temp
== '\0') || (*temp
== '\n'))
367 /* see if this is a comment */
368 if (*temp
== COMMENT_CHARACTER
)
371 memset(&dev
, 0x00, sizeof(dev
));
374 temp2
= strsep(&temp
, ":");
376 dbg("cannot parse line '%s'", line
);
379 strfieldcpy(dev
.name
, temp2
);
381 temp2
= strsep(&temp
, ":");
383 dbg("cannot parse line '%s'", line
);
386 strfieldcpy(dev
.owner
, temp2
);
388 temp2
= strsep(&temp
, ":");
390 dbg("cannot parse line '%s'", line
);
393 strfieldcpy(dev
.group
, temp2
);
396 dbg("cannot parse line '%s'", line
);
399 dev
.mode
= strtol(temp
, NULL
, 8);
401 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
402 dev
.name
, dev
.owner
, dev
.group
, dev
.mode
);
404 retval
= add_perm_dev(&dev
);
406 dbg("add_perm_dev returned with error %d", retval
);
412 file_unmap(buf
, bufsize
);
416 int namedev_init_rules()
420 stat(udev_rules_filename
, &stats
);
421 if ((stats
.st_mode
& S_IFMT
) != S_IFDIR
)
422 return namedev_parse_rules(udev_rules_filename
);
424 return call_foreach_file(namedev_parse_rules
,
425 udev_rules_filename
, RULEFILE_SUFFIX
);
428 int namedev_init_permissions()
432 stat(udev_permissions_filename
, &stats
);
433 if ((stats
.st_mode
& S_IFMT
) != S_IFDIR
)
434 return namedev_parse_permissions(udev_permissions_filename
);
436 return call_foreach_file(namedev_parse_permissions
,
437 udev_permissions_filename
, PERMFILE_SUFFIX
);