]>
Commit | Line | Data |
---|---|---|
19feb351 GKH |
1 | /* |
2 | * namedev_parse.c | |
3 | * | |
4 | * Userspace devfs | |
5 | * | |
274812b5 | 6 | * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com> |
19feb351 GKH |
7 | * |
8 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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. | |
21 | * | |
22 | */ | |
23 | ||
851cd18d GKH |
24 | #ifdef DEBUG |
25 | /* define this to enable parsing debugging also */ | |
19feb351 | 26 | /* #define DEBUG_PARSER */ |
851cd18d | 27 | #endif |
19feb351 GKH |
28 | |
29 | #include <stddef.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <stdio.h> | |
19feb351 GKH |
33 | #include <ctype.h> |
34 | #include <unistd.h> | |
e41245cb | 35 | #include <sys/stat.h> |
19feb351 GKH |
36 | #include <errno.h> |
37 | ||
38 | #include "udev.h" | |
c81b35c0 | 39 | #include "udev_lib.h" |
54988802 | 40 | #include "logging.h" |
19feb351 GKH |
41 | #include "namedev.h" |
42 | ||
e41016d3 | 43 | |
a8b01705 GKH |
44 | static int add_config_dev(struct config_device *new_dev) |
45 | { | |
46 | struct config_device *tmp_dev; | |
47 | ||
48 | tmp_dev = malloc(sizeof(*tmp_dev)); | |
49 | if (tmp_dev == NULL) | |
50 | return -ENOMEM; | |
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); | |
54 | return 0; | |
55 | } | |
56 | ||
19feb351 GKH |
57 | void dump_config_dev(struct config_device *dev) |
58 | { | |
ac28b86d KS |
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'", | |
e41016d3 | 62 | "owner='%s', group='%s', mode=%#o", |
ac28b86d KS |
63 | dev->name, dev->symlink, dev->bus, dev->place, dev->id, |
64 | dev->sysfs_pair[0].file, dev->sysfs_pair[0].value, | |
2731fd98 | 65 | dev->kernel, dev->program, dev->result, |
e41016d3 | 66 | dev->owner, dev->group, dev->mode); |
19feb351 GKH |
67 | } |
68 | ||
69 | void dump_config_dev_list(void) | |
70 | { | |
843d1a84 | 71 | struct config_device *dev; |
19feb351 | 72 | |
843d1a84 | 73 | list_for_each_entry(dev, &config_device_list, node) |
19feb351 | 74 | dump_config_dev(dev); |
19feb351 | 75 | } |
61219c75 | 76 | |
e41016d3 KS |
77 | static int add_perm_dev(struct perm_device *new_dev) |
78 | { | |
79 | struct perm_device *dev; | |
80 | struct perm_device *tmp_dev; | |
81 | ||
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) | |
85 | continue; | |
86 | ||
87 | set_empty_perms(dev, new_dev->mode, new_dev->owner, new_dev->group); | |
88 | return 0; | |
89 | } | |
90 | ||
91 | /* not found, add new structure to the perm list */ | |
92 | tmp_dev = malloc(sizeof(*tmp_dev)); | |
93 | if (!tmp_dev) | |
94 | return -ENOMEM; | |
95 | ||
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); | |
99 | return 0; | |
100 | } | |
101 | ||
61219c75 GKH |
102 | void dump_perm_dev(struct perm_device *dev) |
103 | { | |
104 | dbg_parse("name='%s', owner='%s', group='%s', mode=%#o", | |
105 | dev->name, dev->owner, dev->group, dev->mode); | |
106 | } | |
107 | ||
108 | void dump_perm_dev_list(void) | |
109 | { | |
843d1a84 | 110 | struct perm_device *dev; |
61219c75 | 111 | |
843d1a84 | 112 | list_for_each_entry(dev, &perm_device_list, node) |
61219c75 | 113 | dump_perm_dev(dev); |
61219c75 GKH |
114 | } |
115 | ||
bb738647 KS |
116 | /* extract possible KEY{attr} or KEY_attr */ |
117 | static char *get_key_attribute(char *str) | |
118 | { | |
119 | char *pos; | |
120 | char *attr; | |
121 | ||
bb738647 KS |
122 | attr = strchr(str, '{'); |
123 | if (attr != NULL) { | |
124 | attr++; | |
125 | pos = strchr(attr, '}'); | |
126 | if (pos == NULL) { | |
127 | dbg("missing closing brace for format"); | |
128 | return NULL; | |
129 | } | |
130 | pos[0] = '\0'; | |
131 | dbg("attribute='%s'", attr); | |
132 | return attr; | |
133 | } | |
134 | ||
50e5de03 KS |
135 | attr = strchr(str, '_'); |
136 | if (attr != NULL) { | |
137 | attr++; | |
138 | dbg("attribute='%s'", attr); | |
139 | return attr; | |
140 | } | |
141 | ||
bb738647 KS |
142 | return NULL; |
143 | } | |
144 | ||
e41245cb | 145 | static int namedev_parse_rules(char *filename) |
19feb351 GKH |
146 | { |
147 | char line[255]; | |
148 | int lineno; | |
149 | char *temp; | |
150 | char *temp2; | |
151 | char *temp3; | |
bb738647 | 152 | char *attr; |
c81b35c0 KS |
153 | char *buf; |
154 | size_t bufsize; | |
155 | size_t cur; | |
156 | size_t count; | |
ac28b86d | 157 | int program_given = 0; |
19feb351 GKH |
158 | int retval = 0; |
159 | struct config_device dev; | |
160 | ||
c81b35c0 | 161 | if (file_map(filename, &buf, &bufsize) == 0) { |
e41245cb | 162 | dbg("reading '%s' as rules file", filename); |
e8baccca | 163 | } else { |
c81b35c0 KS |
164 | dbg("can't open '%s' as rules file", filename); |
165 | return -1; | |
19feb351 GKH |
166 | } |
167 | ||
168 | /* loop through the whole file */ | |
c81b35c0 | 169 | cur = 0; |
19feb351 GKH |
170 | lineno = 0; |
171 | while (1) { | |
c81b35c0 KS |
172 | count = buf_get_line(buf, bufsize, cur); |
173 | ||
174 | strncpy(line, buf + cur, count); | |
175 | line[count] = '\0'; | |
176 | temp = line; | |
19feb351 | 177 | lineno++; |
c81b35c0 KS |
178 | |
179 | cur += count+1; | |
180 | if (cur > bufsize) | |
181 | break; | |
182 | ||
19feb351 GKH |
183 | dbg_parse("read '%s'", temp); |
184 | ||
093bf8f4 RK |
185 | /* eat the whitespace */ |
186 | while (isspace(*temp)) | |
187 | ++temp; | |
188 | ||
19feb351 | 189 | /* empty line? */ |
e15b5ed5 | 190 | if ((*temp == '\0') || (*temp == '\n')) |
19feb351 GKH |
191 | continue; |
192 | ||
193 | /* see if this is a comment */ | |
194 | if (*temp == COMMENT_CHARACTER) | |
195 | continue; | |
196 | ||
197 | memset(&dev, 0x00, sizeof(struct config_device)); | |
198 | ||
d94df232 KS |
199 | /* get all known keys */ |
200 | while (1) { | |
274812b5 | 201 | retval = parse_get_pair(&temp, &temp2, &temp3); |
19feb351 GKH |
202 | if (retval) |
203 | break; | |
3d150dfb | 204 | |
d94df232 KS |
205 | if (strcasecmp(temp2, FIELD_BUS) == 0) { |
206 | strfieldcpy(dev.bus, temp3); | |
207 | continue; | |
208 | } | |
209 | ||
210 | if (strcasecmp(temp2, FIELD_ID) == 0) { | |
211 | strfieldcpy(dev.id, temp3); | |
212 | continue; | |
213 | } | |
214 | ||
215 | if (strcasecmp(temp2, FIELD_PLACE) == 0) { | |
216 | strfieldcpy(dev.place, temp3); | |
217 | continue; | |
218 | } | |
219 | ||
220 | if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) { | |
a8b01705 GKH |
221 | struct sysfs_pair *pair = &dev.sysfs_pair[0]; |
222 | int sysfs_pair_num = 0; | |
223 | ||
224 | /* find first unused pair */ | |
225 | while (pair->file[0] != '\0') { | |
226 | ++sysfs_pair_num; | |
227 | if (sysfs_pair_num >= MAX_SYSFS_PAIRS) { | |
228 | pair = NULL; | |
229 | break; | |
230 | } | |
231 | ++pair; | |
232 | } | |
233 | if (pair) { | |
bb738647 KS |
234 | attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1); |
235 | if (attr == NULL) { | |
236 | dbg("error parsing " FIELD_SYSFS " attribute"); | |
237 | continue; | |
238 | } | |
239 | strfieldcpy(pair->file, attr); | |
a8b01705 GKH |
240 | strfieldcpy(pair->value, temp3); |
241 | } | |
d94df232 KS |
242 | continue; |
243 | } | |
244 | ||
245 | if (strcasecmp(temp2, FIELD_KERNEL) == 0) { | |
ac28b86d | 246 | strfieldcpy(dev.kernel, temp3); |
d94df232 KS |
247 | continue; |
248 | } | |
249 | ||
250 | if (strcasecmp(temp2, FIELD_PROGRAM) == 0) { | |
ac28b86d KS |
251 | program_given = 1; |
252 | strfieldcpy(dev.program, temp3); | |
253 | continue; | |
254 | } | |
255 | ||
256 | if (strcasecmp(temp2, FIELD_RESULT) == 0) { | |
257 | strfieldcpy(dev.result, temp3); | |
d94df232 KS |
258 | continue; |
259 | } | |
260 | ||
50e5de03 KS |
261 | if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) { |
262 | attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1); | |
263 | if (attr != NULL) | |
264 | if (strcasecmp(attr, ATTR_PARTITIONS) == 0) { | |
265 | dbg_parse("creation of partition nodes requested"); | |
266 | dev.partitions = PARTITIONS_COUNT; | |
267 | } | |
d94df232 KS |
268 | strfieldcpy(dev.name, temp3); |
269 | continue; | |
270 | } | |
271 | ||
272 | if (strcasecmp(temp2, FIELD_SYMLINK) == 0) { | |
3d150dfb | 273 | strfieldcpy(dev.symlink, temp3); |
d94df232 KS |
274 | continue; |
275 | } | |
3d150dfb | 276 | |
e41016d3 KS |
277 | if (strcasecmp(temp2, FIELD_OWNER) == 0) { |
278 | strfieldcpy(dev.owner, temp3); | |
279 | continue; | |
280 | } | |
281 | ||
282 | if (strcasecmp(temp2, FIELD_GROUP) == 0) { | |
283 | strfieldcpy(dev.group, temp3); | |
284 | continue; | |
285 | } | |
286 | ||
287 | if (strcasecmp(temp2, FIELD_MODE) == 0) { | |
288 | dev.mode = strtol(temp3, NULL, 8); | |
289 | continue; | |
290 | } | |
291 | ||
851cd18d | 292 | dbg("unknown type of field '%s'", temp2); |
851cd18d | 293 | goto error; |
d94df232 KS |
294 | } |
295 | ||
e41016d3 | 296 | /* simple plausibility checks for given keys */ |
ac28b86d KS |
297 | if ((dev.sysfs_pair[0].file[0] == '\0') ^ |
298 | (dev.sysfs_pair[0].value[0] == '\0')) { | |
bb738647 | 299 | dbg("inconsistency in " FIELD_SYSFS " key"); |
ac28b86d KS |
300 | goto error; |
301 | } | |
302 | ||
303 | if ((dev.result[0] != '\0') && (program_given == 0)) { | |
bb738647 KS |
304 | dbg(FIELD_RESULT " is only useful when " |
305 | FIELD_PROGRAM " is called in any rule before"); | |
d94df232 | 306 | goto error; |
19feb351 GKH |
307 | } |
308 | ||
54988802 | 309 | dev.config_line = lineno; |
bd5f8e7c | 310 | strfieldcpy(dev.config_file, filename); |
19feb351 GKH |
311 | retval = add_config_dev(&dev); |
312 | if (retval) { | |
313 | dbg("add_config_dev returned with error %d", retval); | |
d94df232 | 314 | continue; |
ac28b86d | 315 | error: |
851cd18d | 316 | dbg("%s:%d:%d: parse error, rule skipped", |
e41245cb | 317 | filename, lineno, temp - line); |
19feb351 GKH |
318 | } |
319 | } | |
c81b35c0 KS |
320 | |
321 | file_unmap(buf, bufsize); | |
19feb351 | 322 | return retval; |
d94df232 | 323 | } |
19feb351 | 324 | |
e41245cb | 325 | static int namedev_parse_permissions(char *filename) |
19feb351 GKH |
326 | { |
327 | char line[255]; | |
328 | char *temp; | |
329 | char *temp2; | |
c81b35c0 KS |
330 | char *buf; |
331 | size_t bufsize; | |
332 | size_t cur; | |
333 | size_t count; | |
19feb351 | 334 | int retval = 0; |
61219c75 | 335 | struct perm_device dev; |
19feb351 | 336 | |
c81b35c0 | 337 | if (file_map(filename, &buf, &bufsize) == 0) { |
e41245cb | 338 | dbg("reading '%s' as permissions file", filename); |
e8baccca | 339 | } else { |
e41245cb | 340 | dbg("can't open '%s' as permissions file", filename); |
c81b35c0 | 341 | return -1; |
19feb351 GKH |
342 | } |
343 | ||
344 | /* loop through the whole file */ | |
c81b35c0 | 345 | cur = 0; |
19feb351 | 346 | while (1) { |
c81b35c0 KS |
347 | count = buf_get_line(buf, bufsize, cur); |
348 | ||
349 | strncpy(line, buf + cur, count); | |
350 | line[count] = '\0'; | |
351 | temp = line; | |
352 | ||
353 | cur += count+1; | |
354 | if (cur > bufsize) | |
19feb351 GKH |
355 | break; |
356 | ||
357 | dbg_parse("read '%s'", temp); | |
358 | ||
359 | /* eat the whitespace at the beginning of the line */ | |
360 | while (isspace(*temp)) | |
361 | ++temp; | |
362 | ||
363 | /* empty line? */ | |
e15b5ed5 | 364 | if ((*temp == '\0') || (*temp == '\n')) |
19feb351 GKH |
365 | continue; |
366 | ||
367 | /* see if this is a comment */ | |
368 | if (*temp == COMMENT_CHARACTER) | |
369 | continue; | |
370 | ||
371 | memset(&dev, 0x00, sizeof(dev)); | |
372 | ||
373 | /* parse the line */ | |
374 | temp2 = strsep(&temp, ":"); | |
375 | if (!temp2) { | |
376 | dbg("cannot parse line '%s'", line); | |
377 | continue; | |
378 | } | |
c472e3c8 | 379 | strfieldcpy(dev.name, temp2); |
19feb351 GKH |
380 | |
381 | temp2 = strsep(&temp, ":"); | |
382 | if (!temp2) { | |
383 | dbg("cannot parse line '%s'", line); | |
384 | continue; | |
385 | } | |
c472e3c8 | 386 | strfieldcpy(dev.owner, temp2); |
19feb351 GKH |
387 | |
388 | temp2 = strsep(&temp, ":"); | |
389 | if (!temp2) { | |
390 | dbg("cannot parse line '%s'", line); | |
391 | continue; | |
392 | } | |
c472e3c8 | 393 | strfieldcpy(dev.group, temp2); |
19feb351 GKH |
394 | |
395 | if (!temp) { | |
e41016d3 | 396 | dbg("cannot parse line '%s'", line); |
19feb351 GKH |
397 | continue; |
398 | } | |
399 | dev.mode = strtol(temp, NULL, 8); | |
400 | ||
401 | dbg_parse("name='%s', owner='%s', group='%s', mode=%#o", | |
e41016d3 KS |
402 | dev.name, dev.owner, dev.group, dev.mode); |
403 | ||
61219c75 | 404 | retval = add_perm_dev(&dev); |
19feb351 | 405 | if (retval) { |
3d150dfb | 406 | dbg("add_perm_dev returned with error %d", retval); |
19feb351 GKH |
407 | goto exit; |
408 | } | |
409 | } | |
410 | ||
411 | exit: | |
c81b35c0 | 412 | file_unmap(buf, bufsize); |
19feb351 | 413 | return retval; |
267f534d | 414 | } |
19feb351 | 415 | |
4a539daf | 416 | int namedev_init_rules() |
e41245cb | 417 | { |
e41245cb | 418 | struct stat stats; |
e41245cb | 419 | |
4a539daf | 420 | stat(udev_rules_filename, &stats); |
e41245cb | 421 | if ((stats.st_mode & S_IFMT) != S_IFDIR) |
4a539daf KS |
422 | return namedev_parse_rules(udev_rules_filename); |
423 | else | |
424 | return call_foreach_file(namedev_parse_rules, | |
425 | udev_rules_filename, RULEFILE_SUFFIX); | |
e41245cb KS |
426 | } |
427 | ||
428 | int namedev_init_permissions() | |
429 | { | |
4a539daf KS |
430 | struct stat stats; |
431 | ||
432 | stat(udev_permissions_filename, &stats); | |
433 | if ((stats.st_mode & S_IFMT) != S_IFDIR) | |
434 | return namedev_parse_permissions(udev_permissions_filename); | |
435 | else | |
436 | return call_foreach_file(namedev_parse_permissions, | |
437 | udev_permissions_filename, PERMFILE_SUFFIX); | |
e41245cb | 438 | } |