]>
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> | |
33 | #include <fcntl.h> | |
34 | #include <ctype.h> | |
35 | #include <unistd.h> | |
36 | #include <errno.h> | |
37 | ||
38 | #include "udev.h" | |
54988802 | 39 | #include "logging.h" |
19feb351 GKH |
40 | #include "namedev.h" |
41 | ||
a8b01705 GKH |
42 | static int add_config_dev(struct config_device *new_dev) |
43 | { | |
44 | struct config_device *tmp_dev; | |
45 | ||
46 | tmp_dev = malloc(sizeof(*tmp_dev)); | |
47 | if (tmp_dev == NULL) | |
48 | return -ENOMEM; | |
49 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
50 | list_add_tail(&tmp_dev->node, &config_device_list); | |
51 | //dump_config_dev(tmp_dev); | |
52 | return 0; | |
53 | } | |
54 | ||
19feb351 GKH |
55 | void dump_config_dev(struct config_device *dev) |
56 | { | |
ac28b86d KS |
57 | /*FIXME dump all sysfs's */ |
58 | dbg_parse("name='%s', symlink='%s', bus='%s', place='%s', id='%s', " | |
59 | "sysfs_file[0]='%s', sysfs_value[0]='%s', " | |
60 | "kernel='%s', program='%s', result='%s'", | |
61 | dev->name, dev->symlink, dev->bus, dev->place, dev->id, | |
62 | dev->sysfs_pair[0].file, dev->sysfs_pair[0].value, | |
63 | dev->kernel, dev->program, dev->result); | |
19feb351 GKH |
64 | } |
65 | ||
66 | void dump_config_dev_list(void) | |
67 | { | |
843d1a84 | 68 | struct config_device *dev; |
19feb351 | 69 | |
843d1a84 | 70 | list_for_each_entry(dev, &config_device_list, node) |
19feb351 | 71 | dump_config_dev(dev); |
19feb351 | 72 | } |
61219c75 GKH |
73 | |
74 | void dump_perm_dev(struct perm_device *dev) | |
75 | { | |
76 | dbg_parse("name='%s', owner='%s', group='%s', mode=%#o", | |
77 | dev->name, dev->owner, dev->group, dev->mode); | |
78 | } | |
79 | ||
80 | void dump_perm_dev_list(void) | |
81 | { | |
843d1a84 | 82 | struct perm_device *dev; |
61219c75 | 83 | |
843d1a84 | 84 | list_for_each_entry(dev, &perm_device_list, node) |
61219c75 | 85 | dump_perm_dev(dev); |
61219c75 GKH |
86 | } |
87 | ||
e8baccca | 88 | int namedev_init_rules(void) |
19feb351 GKH |
89 | { |
90 | char line[255]; | |
91 | int lineno; | |
92 | char *temp; | |
93 | char *temp2; | |
94 | char *temp3; | |
95 | FILE *fd; | |
ac28b86d | 96 | int program_given = 0; |
19feb351 GKH |
97 | int retval = 0; |
98 | struct config_device dev; | |
99 | ||
e8baccca GKH |
100 | fd = fopen(udev_rules_filename, "r"); |
101 | if (fd != NULL) { | |
102 | dbg("reading '%s' as rules file", udev_rules_filename); | |
103 | } else { | |
104 | dbg("can't open '%s' as a rules file", udev_rules_filename); | |
19feb351 GKH |
105 | return -ENODEV; |
106 | } | |
107 | ||
108 | /* loop through the whole file */ | |
109 | lineno = 0; | |
110 | while (1) { | |
111 | /* get a line */ | |
112 | temp = fgets(line, sizeof(line), fd); | |
113 | if (temp == NULL) | |
114 | goto exit; | |
115 | lineno++; | |
19feb351 GKH |
116 | dbg_parse("read '%s'", temp); |
117 | ||
093bf8f4 RK |
118 | /* eat the whitespace */ |
119 | while (isspace(*temp)) | |
120 | ++temp; | |
121 | ||
19feb351 | 122 | /* empty line? */ |
e15b5ed5 | 123 | if ((*temp == '\0') || (*temp == '\n')) |
19feb351 GKH |
124 | continue; |
125 | ||
126 | /* see if this is a comment */ | |
127 | if (*temp == COMMENT_CHARACTER) | |
128 | continue; | |
129 | ||
130 | memset(&dev, 0x00, sizeof(struct config_device)); | |
131 | ||
d94df232 KS |
132 | /* get all known keys */ |
133 | while (1) { | |
274812b5 | 134 | retval = parse_get_pair(&temp, &temp2, &temp3); |
19feb351 GKH |
135 | if (retval) |
136 | break; | |
3d150dfb | 137 | |
d94df232 KS |
138 | if (strcasecmp(temp2, FIELD_BUS) == 0) { |
139 | strfieldcpy(dev.bus, temp3); | |
140 | continue; | |
141 | } | |
142 | ||
143 | if (strcasecmp(temp2, FIELD_ID) == 0) { | |
144 | strfieldcpy(dev.id, temp3); | |
145 | continue; | |
146 | } | |
147 | ||
148 | if (strcasecmp(temp2, FIELD_PLACE) == 0) { | |
149 | strfieldcpy(dev.place, temp3); | |
150 | continue; | |
151 | } | |
152 | ||
153 | if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) { | |
a8b01705 GKH |
154 | struct sysfs_pair *pair = &dev.sysfs_pair[0]; |
155 | int sysfs_pair_num = 0; | |
156 | ||
157 | /* find first unused pair */ | |
158 | while (pair->file[0] != '\0') { | |
159 | ++sysfs_pair_num; | |
160 | if (sysfs_pair_num >= MAX_SYSFS_PAIRS) { | |
161 | pair = NULL; | |
162 | break; | |
163 | } | |
164 | ++pair; | |
165 | } | |
166 | if (pair) { | |
167 | /* remove prepended 'SYSFS_' */ | |
168 | strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1); | |
169 | strfieldcpy(pair->value, temp3); | |
170 | } | |
d94df232 KS |
171 | continue; |
172 | } | |
173 | ||
174 | if (strcasecmp(temp2, FIELD_KERNEL) == 0) { | |
ac28b86d | 175 | strfieldcpy(dev.kernel, temp3); |
d94df232 KS |
176 | continue; |
177 | } | |
178 | ||
179 | if (strcasecmp(temp2, FIELD_PROGRAM) == 0) { | |
ac28b86d KS |
180 | program_given = 1; |
181 | strfieldcpy(dev.program, temp3); | |
182 | continue; | |
183 | } | |
184 | ||
185 | if (strcasecmp(temp2, FIELD_RESULT) == 0) { | |
186 | strfieldcpy(dev.result, temp3); | |
d94df232 KS |
187 | continue; |
188 | } | |
189 | ||
190 | if (strcasecmp(temp2, FIELD_NAME) == 0) { | |
191 | strfieldcpy(dev.name, temp3); | |
192 | continue; | |
193 | } | |
194 | ||
195 | if (strcasecmp(temp2, FIELD_SYMLINK) == 0) { | |
3d150dfb | 196 | strfieldcpy(dev.symlink, temp3); |
d94df232 KS |
197 | continue; |
198 | } | |
3d150dfb | 199 | |
851cd18d GKH |
200 | dbg("unknown type of field '%s'", temp2); |
201 | dbg("You might be using a rules file in the old format, please fix."); | |
202 | goto error; | |
d94df232 KS |
203 | } |
204 | ||
ac28b86d KS |
205 | /* simple plausibility check for given keys */ |
206 | if ((dev.sysfs_pair[0].file[0] == '\0') ^ | |
207 | (dev.sysfs_pair[0].value[0] == '\0')) { | |
208 | dbg("inconsistency in SYSFS_ key"); | |
209 | goto error; | |
210 | } | |
211 | ||
212 | if ((dev.result[0] != '\0') && (program_given == 0)) { | |
213 | dbg("RESULT is only useful when PROGRAM called in any rule before"); | |
d94df232 | 214 | goto error; |
19feb351 GKH |
215 | } |
216 | ||
54988802 | 217 | dev.config_line = lineno; |
19feb351 GKH |
218 | retval = add_config_dev(&dev); |
219 | if (retval) { | |
220 | dbg("add_config_dev returned with error %d", retval); | |
d94df232 | 221 | continue; |
ac28b86d | 222 | error: |
851cd18d | 223 | dbg("%s:%d:%d: parse error, rule skipped", |
ac28b86d | 224 | udev_rules_filename, lineno, temp - line); |
19feb351 GKH |
225 | } |
226 | } | |
19feb351 GKH |
227 | exit: |
228 | fclose(fd); | |
229 | return retval; | |
d94df232 | 230 | } |
19feb351 GKH |
231 | |
232 | int namedev_init_permissions(void) | |
233 | { | |
234 | char line[255]; | |
235 | char *temp; | |
236 | char *temp2; | |
237 | FILE *fd; | |
238 | int retval = 0; | |
61219c75 | 239 | struct perm_device dev; |
19feb351 | 240 | |
3836a3c4 | 241 | fd = fopen(udev_permissions_filename, "r"); |
e8baccca | 242 | if (fd != NULL) { |
3836a3c4 | 243 | dbg("reading '%s' as permissions file", udev_permissions_filename); |
e8baccca | 244 | } else { |
3836a3c4 | 245 | dbg("can't open '%s' as permissions file", udev_permissions_filename); |
19feb351 GKH |
246 | return -ENODEV; |
247 | } | |
248 | ||
249 | /* loop through the whole file */ | |
250 | while (1) { | |
251 | temp = fgets(line, sizeof(line), fd); | |
252 | if (temp == NULL) | |
253 | break; | |
254 | ||
255 | dbg_parse("read '%s'", temp); | |
256 | ||
257 | /* eat the whitespace at the beginning of the line */ | |
258 | while (isspace(*temp)) | |
259 | ++temp; | |
260 | ||
261 | /* empty line? */ | |
e15b5ed5 | 262 | if ((*temp == '\0') || (*temp == '\n')) |
19feb351 GKH |
263 | continue; |
264 | ||
265 | /* see if this is a comment */ | |
266 | if (*temp == COMMENT_CHARACTER) | |
267 | continue; | |
268 | ||
269 | memset(&dev, 0x00, sizeof(dev)); | |
270 | ||
271 | /* parse the line */ | |
272 | temp2 = strsep(&temp, ":"); | |
273 | if (!temp2) { | |
274 | dbg("cannot parse line '%s'", line); | |
275 | continue; | |
276 | } | |
277 | strncpy(dev.name, temp2, sizeof(dev.name)); | |
278 | ||
279 | temp2 = strsep(&temp, ":"); | |
280 | if (!temp2) { | |
281 | dbg("cannot parse line '%s'", line); | |
282 | continue; | |
283 | } | |
284 | strncpy(dev.owner, temp2, sizeof(dev.owner)); | |
285 | ||
286 | temp2 = strsep(&temp, ":"); | |
287 | if (!temp2) { | |
288 | dbg("cannot parse line '%s'", line); | |
289 | continue; | |
290 | } | |
eb7a964a | 291 | strncpy(dev.group, temp2, sizeof(dev.group)); |
19feb351 GKH |
292 | |
293 | if (!temp) { | |
294 | dbg("cannot parse line: %s", line); | |
295 | continue; | |
296 | } | |
297 | dev.mode = strtol(temp, NULL, 8); | |
298 | ||
299 | dbg_parse("name='%s', owner='%s', group='%s', mode=%#o", | |
300 | dev.name, dev.owner, dev.group, | |
301 | dev.mode); | |
61219c75 | 302 | retval = add_perm_dev(&dev); |
19feb351 | 303 | if (retval) { |
3d150dfb | 304 | dbg("add_perm_dev returned with error %d", retval); |
19feb351 GKH |
305 | goto exit; |
306 | } | |
307 | } | |
308 | ||
309 | exit: | |
310 | fclose(fd); | |
311 | return retval; | |
267f534d | 312 | } |
19feb351 | 313 |