]> git.ipfire.org Git - thirdparty/systemd.git/blob - namedev_parse.c
[PATCH] v012 release
[thirdparty/systemd.git] / namedev_parse.c
1 /*
2 * namedev_parse.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
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
24 /* define this to enable parsing debugging */
25 /* #define DEBUG_PARSER */
26
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <errno.h>
35
36 #include "udev.h"
37 #include "namedev.h"
38
39 static int add_config_dev(struct config_device *new_dev)
40 {
41 struct config_device *tmp_dev;
42
43 tmp_dev = malloc(sizeof(*tmp_dev));
44 if (tmp_dev == NULL)
45 return -ENOMEM;
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);
49 return 0;
50 }
51
52 int get_pair(char **orig_string, char **left, char **right)
53 {
54 char *temp;
55 char *string = *orig_string;
56
57 if (!string)
58 return -ENODEV;
59
60 /* eat any whitespace */
61 while (isspace(*string) || *string == ',')
62 ++string;
63
64 /* split based on '=' */
65 temp = strsep(&string, "=");
66 *left = temp;
67 if (!string)
68 return -ENODEV;
69
70 /* take the right side and strip off the '"' */
71 while (isspace(*string))
72 ++string;
73 if (*string == '"')
74 ++string;
75 else
76 return -ENODEV;
77
78 temp = strsep(&string, "\"");
79 if (!string || *temp == '\0')
80 return -ENODEV;
81 *right = temp;
82 *orig_string = string;
83
84 return 0;
85 }
86
87 void dump_config_dev(struct config_device *dev)
88 {
89 switch (dev->type) {
90 case KERNEL_NAME:
91 dbg_parse("KERNEL name='%s'", dev->name);
92 break;
93 case LABEL:
94 dbg_parse("LABEL name='%s', bus='%s', sysfs_file[0]='%s', sysfs_value[0]='%s'",
95 dev->name, dev->bus, dev->sysfs_pair[0].file, dev->sysfs_pair[0].value);
96 break;
97 case NUMBER:
98 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
99 dev->name, dev->bus, dev->id);
100 break;
101 case TOPOLOGY:
102 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
103 dev->name, dev->bus, dev->place);
104 break;
105 case REPLACE:
106 dbg_parse("REPLACE name=%s, kernel_name=%s",
107 dev->name, dev->kernel_name);
108 break;
109 case CALLOUT:
110 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s'",
111 dev->name, dev->bus, dev->exec_program, dev->id);
112 break;
113 default:
114 dbg_parse("unknown type of method");
115 }
116 }
117
118 void dump_config_dev_list(void)
119 {
120 struct list_head *tmp;
121
122 list_for_each(tmp, &config_device_list) {
123 struct config_device *dev = list_entry(tmp, struct config_device, node);
124 dump_config_dev(dev);
125 }
126 }
127
128 void dump_perm_dev(struct perm_device *dev)
129 {
130 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
131 dev->name, dev->owner, dev->group, dev->mode);
132 }
133
134 void dump_perm_dev_list(void)
135 {
136 struct list_head *tmp;
137
138 list_for_each(tmp, &perm_device_list) {
139 struct perm_device *dev = list_entry(tmp, struct perm_device, node);
140 dump_perm_dev(dev);
141 }
142 }
143
144
145 int namedev_init_rules(void)
146 {
147 char line[255];
148 int lineno;
149 char *temp;
150 char *temp2;
151 char *temp3;
152 FILE *fd;
153 int retval = 0;
154 struct config_device dev;
155
156 fd = fopen(udev_rules_filename, "r");
157 if (fd != NULL) {
158 dbg("reading '%s' as rules file", udev_rules_filename);
159 } else {
160 dbg("can't open '%s' as a rules file", udev_rules_filename);
161 return -ENODEV;
162 }
163
164 /* loop through the whole file */
165 lineno = 0;
166 while (1) {
167 /* get a line */
168 temp = fgets(line, sizeof(line), fd);
169 if (temp == NULL)
170 goto exit;
171 lineno++;
172 dbg_parse("read '%s'", temp);
173
174 /* eat the whitespace */
175 while (isspace(*temp))
176 ++temp;
177
178 /* empty line? */
179 if ((*temp == '\0') || (*temp == '\n'))
180 continue;
181
182 /* see if this is a comment */
183 if (*temp == COMMENT_CHARACTER)
184 continue;
185
186 memset(&dev, 0x00, sizeof(struct config_device));
187
188 /* get the method */
189 temp2 = strsep(&temp, ",");
190
191 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
192 dev.type = LABEL;
193 goto keys;
194 }
195
196 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
197 dev.type = NUMBER;
198 goto keys;
199 }
200
201 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
202 dev.type = TOPOLOGY;
203 goto keys;
204 }
205
206 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
207 dev.type = REPLACE;
208 goto keys;
209 }
210
211 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
212 dev.type = CALLOUT;
213 goto keys;
214 }
215
216 dbg_parse("unknown type of method '%s'", temp2);
217 goto error;
218 keys:
219 /* get all known keys */
220 while (1) {
221 retval = get_pair(&temp, &temp2, &temp3);
222 if (retval)
223 break;
224
225 if (strcasecmp(temp2, FIELD_BUS) == 0) {
226 strfieldcpy(dev.bus, temp3);
227 continue;
228 }
229
230 if (strcasecmp(temp2, FIELD_ID) == 0) {
231 strfieldcpy(dev.id, temp3);
232 continue;
233 }
234
235 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
236 strfieldcpy(dev.place, temp3);
237 continue;
238 }
239
240 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
241 struct sysfs_pair *pair = &dev.sysfs_pair[0];
242 int sysfs_pair_num = 0;
243
244 /* find first unused pair */
245 while (pair->file[0] != '\0') {
246 ++sysfs_pair_num;
247 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
248 pair = NULL;
249 break;
250 }
251 ++pair;
252 }
253 if (pair) {
254 /* remove prepended 'SYSFS_' */
255 strfieldcpy(pair->file, temp2 + sizeof(FIELD_SYSFS)-1);
256 strfieldcpy(pair->value, temp3);
257 }
258 continue;
259 }
260
261 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
262 strfieldcpy(dev.kernel_name, temp3);
263 continue;
264 }
265
266 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
267 strfieldcpy(dev.exec_program, temp3);
268 continue;
269 }
270
271 if (strcasecmp(temp2, FIELD_NAME) == 0) {
272 strfieldcpy(dev.name, temp3);
273 continue;
274 }
275
276 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
277 strfieldcpy(dev.symlink, temp3);
278 continue;
279 }
280
281 dbg_parse("unknown type of field '%s'", temp2);
282 }
283
284 /* check presence of keys according to method type */
285 switch (dev.type) {
286 case LABEL:
287 dbg_parse(TYPE_LABEL " name='%s', bus='%s', "
288 "sysfs_file[0]='%s', sysfs_value[0]='%s', symlink='%s'",
289 dev.name, dev.bus, dev.sysfs_pair[0].file,
290 dev.sysfs_pair[0].value, dev.symlink);
291 if ((*dev.name == '\0') ||
292 (*dev.sysfs_pair[0].file == '\0') ||
293 (*dev.sysfs_pair[0].value == '\0'))
294 goto error;
295 break;
296 case NUMBER:
297 dbg_parse(TYPE_NUMBER " name='%s', bus='%s', id='%s', symlink='%s'",
298 dev.name, dev.bus, dev.id, dev.symlink);
299 if ((*dev.name == '\0') ||
300 (*dev.bus == '\0') ||
301 (*dev.id == '\0'))
302 goto error;
303 break;
304 case TOPOLOGY:
305 dbg_parse(TYPE_TOPOLOGY " name='%s', bus='%s', "
306 "place='%s', symlink='%s'",
307 dev.name, dev.bus, dev.place, dev.symlink);
308 if ((*dev.name == '\0') ||
309 (*dev.bus == '\0') ||
310 (*dev.place == '\0'))
311 goto error;
312 break;
313 case REPLACE:
314 dbg_parse(TYPE_REPLACE " name='%s', kernel_name='%s', symlink='%s'",
315 dev.name, dev.kernel_name, dev.symlink);
316 if ((*dev.name == '\0') ||
317 (*dev.kernel_name == '\0'))
318 goto error;
319 break;
320 case CALLOUT:
321 dbg_parse(TYPE_CALLOUT " name='%s', bus='%s', program='%s', "
322 "id='%s', symlink='%s'",
323 dev.name, dev.bus, dev.exec_program,
324 dev.id, dev.symlink);
325 if ((*dev.name == '\0') ||
326 (*dev.id == '\0') ||
327 (*dev.exec_program == '\0'))
328 goto error;
329 break;
330 default:
331 dbg_parse("unknown type of method");
332 goto error;
333 }
334
335 retval = add_config_dev(&dev);
336 if (retval) {
337 dbg("add_config_dev returned with error %d", retval);
338 continue;
339 }
340 }
341 error:
342 dbg_parse("%s:%d:%Zd: field missing or parse error", udev_rules_filename,
343 lineno, temp - line);
344 exit:
345 fclose(fd);
346 return retval;
347 }
348
349 int namedev_init_permissions(void)
350 {
351 char line[255];
352 char *temp;
353 char *temp2;
354 FILE *fd;
355 int retval = 0;
356 struct perm_device dev;
357
358 fd = fopen(udev_permissions_filename, "r");
359 if (fd != NULL) {
360 dbg("reading '%s' as permissions file", udev_permissions_filename);
361 } else {
362 dbg("can't open '%s' as permissions file", udev_permissions_filename);
363 return -ENODEV;
364 }
365
366 /* loop through the whole file */
367 while (1) {
368 temp = fgets(line, sizeof(line), fd);
369 if (temp == NULL)
370 break;
371
372 dbg_parse("read '%s'", temp);
373
374 /* eat the whitespace at the beginning of the line */
375 while (isspace(*temp))
376 ++temp;
377
378 /* empty line? */
379 if ((*temp == '\0') || (*temp == '\n'))
380 continue;
381
382 /* see if this is a comment */
383 if (*temp == COMMENT_CHARACTER)
384 continue;
385
386 memset(&dev, 0x00, sizeof(dev));
387
388 /* parse the line */
389 temp2 = strsep(&temp, ":");
390 if (!temp2) {
391 dbg("cannot parse line '%s'", line);
392 continue;
393 }
394 strncpy(dev.name, temp2, sizeof(dev.name));
395
396 temp2 = strsep(&temp, ":");
397 if (!temp2) {
398 dbg("cannot parse line '%s'", line);
399 continue;
400 }
401 strncpy(dev.owner, temp2, sizeof(dev.owner));
402
403 temp2 = strsep(&temp, ":");
404 if (!temp2) {
405 dbg("cannot parse line '%s'", line);
406 continue;
407 }
408 strncpy(dev.group, temp2, sizeof(dev.owner));
409
410 if (!temp) {
411 dbg("cannot parse line: %s", line);
412 continue;
413 }
414 dev.mode = strtol(temp, NULL, 8);
415
416 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
417 dev.name, dev.owner, dev.group,
418 dev.mode);
419 retval = add_perm_dev(&dev);
420 if (retval) {
421 dbg("add_perm_dev returned with error %d", retval);
422 goto exit;
423 }
424 }
425
426 exit:
427 fclose(fd);
428 return retval;
429 }
430
431