]> git.ipfire.org Git - thirdparty/systemd.git/blob - namedev_parse.c
[PATCH] dev_d.c file sorting and cleanup
[thirdparty/systemd.git] / namedev_parse.c
1 /*
2 * namedev_parse.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003,2004 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 #ifdef DEBUG
25 /* define this to enable parsing debugging also */
26 /* #define DEBUG_PARSER */
27 #endif
28
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37
38 #include "udev.h"
39 #include "udev_lib.h"
40 #include "logging.h"
41 #include "namedev.h"
42
43
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
57 void dump_config_dev(struct config_device *dev)
58 {
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);
67 }
68
69 void dump_config_dev_list(void)
70 {
71 struct config_device *dev;
72
73 list_for_each_entry(dev, &config_device_list, node)
74 dump_config_dev(dev);
75 }
76
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
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 {
110 struct perm_device *dev;
111
112 list_for_each_entry(dev, &perm_device_list, node)
113 dump_perm_dev(dev);
114 }
115
116 /* extract possible KEY{attr} or KEY_attr */
117 static char *get_key_attribute(char *str)
118 {
119 char *pos;
120 char *attr;
121
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
135 attr = strchr(str, '_');
136 if (attr != NULL) {
137 attr++;
138 dbg("attribute='%s'", attr);
139 return attr;
140 }
141
142 return NULL;
143 }
144
145 static int namedev_parse_rules(char *filename)
146 {
147 char line[255];
148 int lineno;
149 char *temp;
150 char *temp2;
151 char *temp3;
152 char *attr;
153 char *buf;
154 size_t bufsize;
155 size_t cur;
156 size_t count;
157 int program_given = 0;
158 int retval = 0;
159 struct config_device dev;
160
161 if (file_map(filename, &buf, &bufsize) == 0) {
162 dbg("reading '%s' as rules file", filename);
163 } else {
164 dbg("can't open '%s' as rules file", filename);
165 return -1;
166 }
167
168 /* loop through the whole file */
169 cur = 0;
170 lineno = 0;
171 while (1) {
172 count = buf_get_line(buf, bufsize, cur);
173
174 strncpy(line, buf + cur, count);
175 line[count] = '\0';
176 temp = line;
177 lineno++;
178
179 cur += count+1;
180 if (cur > bufsize)
181 break;
182
183 dbg_parse("read '%s'", temp);
184
185 /* eat the whitespace */
186 while (isspace(*temp))
187 ++temp;
188
189 /* empty line? */
190 if ((*temp == '\0') || (*temp == '\n'))
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
199 /* get all known keys */
200 while (1) {
201 retval = parse_get_pair(&temp, &temp2, &temp3);
202 if (retval)
203 break;
204
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) {
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) {
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);
240 strfieldcpy(pair->value, temp3);
241 }
242 continue;
243 }
244
245 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
246 strfieldcpy(dev.kernel, temp3);
247 continue;
248 }
249
250 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
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);
258 continue;
259 }
260
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 }
268 strfieldcpy(dev.name, temp3);
269 continue;
270 }
271
272 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
273 strfieldcpy(dev.symlink, temp3);
274 continue;
275 }
276
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
292 dbg("unknown type of field '%s'", temp2);
293 goto error;
294 }
295
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");
300 goto error;
301 }
302
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");
306 goto error;
307 }
308
309 dev.config_line = lineno;
310 strfieldcpy(dev.config_file, filename);
311 retval = add_config_dev(&dev);
312 if (retval) {
313 dbg("add_config_dev returned with error %d", retval);
314 continue;
315 error:
316 dbg("%s:%d:%d: parse error, rule skipped",
317 filename, lineno, temp - line);
318 }
319 }
320
321 file_unmap(buf, bufsize);
322 return retval;
323 }
324
325 static int namedev_parse_permissions(char *filename)
326 {
327 char line[255];
328 char *temp;
329 char *temp2;
330 char *buf;
331 size_t bufsize;
332 size_t cur;
333 size_t count;
334 int retval = 0;
335 struct perm_device dev;
336
337 if (file_map(filename, &buf, &bufsize) == 0) {
338 dbg("reading '%s' as permissions file", filename);
339 } else {
340 dbg("can't open '%s' as permissions file", filename);
341 return -1;
342 }
343
344 /* loop through the whole file */
345 cur = 0;
346 while (1) {
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)
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? */
364 if ((*temp == '\0') || (*temp == '\n'))
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 }
379 strfieldcpy(dev.name, temp2);
380
381 temp2 = strsep(&temp, ":");
382 if (!temp2) {
383 dbg("cannot parse line '%s'", line);
384 continue;
385 }
386 strfieldcpy(dev.owner, temp2);
387
388 temp2 = strsep(&temp, ":");
389 if (!temp2) {
390 dbg("cannot parse line '%s'", line);
391 continue;
392 }
393 strfieldcpy(dev.group, temp2);
394
395 if (!temp) {
396 dbg("cannot parse line '%s'", line);
397 continue;
398 }
399 dev.mode = strtol(temp, NULL, 8);
400
401 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
402 dev.name, dev.owner, dev.group, dev.mode);
403
404 retval = add_perm_dev(&dev);
405 if (retval) {
406 dbg("add_perm_dev returned with error %d", retval);
407 goto exit;
408 }
409 }
410
411 exit:
412 file_unmap(buf, bufsize);
413 return retval;
414 }
415
416 int namedev_init_rules()
417 {
418 struct stat stats;
419
420 stat(udev_rules_filename, &stats);
421 if ((stats.st_mode & S_IFMT) != S_IFDIR)
422 return namedev_parse_rules(udev_rules_filename);
423 else
424 return call_foreach_file(namedev_parse_rules,
425 udev_rules_filename, RULEFILE_SUFFIX);
426 }
427
428 int namedev_init_permissions()
429 {
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);
438 }