]> git.ipfire.org Git - thirdparty/systemd.git/blame - namedev_parse.c
[PATCH] $local user
[thirdparty/systemd.git] / namedev_parse.c
CommitLineData
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
44static 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
57void 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', "
3e441450 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
69void 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
77static 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
102void 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
108void 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 */
117static 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 145static int namedev_parse_rules(char *filename)
19feb351 146{
3e441450 147 char line[LINE_SIZE];
148 char *bufline;
19feb351
GKH
149 int lineno;
150 char *temp;
151 char *temp2;
152 char *temp3;
bb738647 153 char *attr;
c81b35c0
KS
154 char *buf;
155 size_t bufsize;
156 size_t cur;
157 size_t count;
ac28b86d 158 int program_given = 0;
3e441450 159 int valid;
19feb351
GKH
160 int retval = 0;
161 struct config_device dev;
162
c81b35c0 163 if (file_map(filename, &buf, &bufsize) == 0) {
e41245cb 164 dbg("reading '%s' as rules file", filename);
e8baccca 165 } else {
c81b35c0
KS
166 dbg("can't open '%s' as rules file", filename);
167 return -1;
19feb351
GKH
168 }
169
170 /* loop through the whole file */
c81b35c0 171 cur = 0;
19feb351 172 lineno = 0;
3e441450 173 while (cur < bufsize) {
c81b35c0 174 count = buf_get_line(buf, bufsize, cur);
3e441450 175 bufline = &buf[cur];
c81b35c0 176 cur += count+1;
3e441450 177 lineno++;
19feb351 178
3e441450 179 if (count >= LINE_SIZE) {
180 info("line too long, rule skipped %s, line %d",
181 filename, lineno);
182 continue;
183 }
093bf8f4 184
3e441450 185 /* eat the whitespace */
3db7fa27 186 while ((count > 0) && isspace(bufline[0])) {
3e441450 187 bufline++;
188 count--;
189 }
3db7fa27
KS
190 if (count == 0)
191 continue;
3e441450 192
19feb351 193 /* see if this is a comment */
3e441450 194 if (bufline[0] == COMMENT_CHARACTER)
19feb351
GKH
195 continue;
196
3e441450 197 strncpy(line, bufline, count);
198 line[count] = '\0';
199 dbg_parse("read '%s'", line);
19feb351 200
d94df232 201 /* get all known keys */
3e441450 202 memset(&dev, 0x00, sizeof(struct config_device));
203 temp = line;
204 valid = 0;
205
d94df232 206 while (1) {
274812b5 207 retval = parse_get_pair(&temp, &temp2, &temp3);
19feb351
GKH
208 if (retval)
209 break;
3d150dfb 210
d94df232
KS
211 if (strcasecmp(temp2, FIELD_BUS) == 0) {
212 strfieldcpy(dev.bus, temp3);
3e441450 213 valid = 1;
d94df232
KS
214 continue;
215 }
216
217 if (strcasecmp(temp2, FIELD_ID) == 0) {
218 strfieldcpy(dev.id, temp3);
3e441450 219 valid = 1;
d94df232
KS
220 continue;
221 }
222
223 if (strcasecmp(temp2, FIELD_PLACE) == 0) {
224 strfieldcpy(dev.place, temp3);
3e441450 225 valid = 1;
d94df232
KS
226 continue;
227 }
228
229 if (strncasecmp(temp2, FIELD_SYSFS, sizeof(FIELD_SYSFS)-1) == 0) {
a8b01705
GKH
230 struct sysfs_pair *pair = &dev.sysfs_pair[0];
231 int sysfs_pair_num = 0;
232
233 /* find first unused pair */
234 while (pair->file[0] != '\0') {
235 ++sysfs_pair_num;
236 if (sysfs_pair_num >= MAX_SYSFS_PAIRS) {
237 pair = NULL;
238 break;
239 }
240 ++pair;
241 }
242 if (pair) {
bb738647
KS
243 attr = get_key_attribute(temp2 + sizeof(FIELD_SYSFS)-1);
244 if (attr == NULL) {
245 dbg("error parsing " FIELD_SYSFS " attribute");
246 continue;
247 }
248 strfieldcpy(pair->file, attr);
a8b01705 249 strfieldcpy(pair->value, temp3);
3e441450 250 valid = 1;
a8b01705 251 }
d94df232
KS
252 continue;
253 }
254
255 if (strcasecmp(temp2, FIELD_KERNEL) == 0) {
ac28b86d 256 strfieldcpy(dev.kernel, temp3);
3e441450 257 valid = 1;
d94df232
KS
258 continue;
259 }
260
261 if (strcasecmp(temp2, FIELD_PROGRAM) == 0) {
ac28b86d
KS
262 program_given = 1;
263 strfieldcpy(dev.program, temp3);
3e441450 264 valid = 1;
ac28b86d
KS
265 continue;
266 }
267
268 if (strcasecmp(temp2, FIELD_RESULT) == 0) {
269 strfieldcpy(dev.result, temp3);
3e441450 270 valid = 1;
d94df232
KS
271 continue;
272 }
273
50e5de03
KS
274 if (strncasecmp(temp2, FIELD_NAME, sizeof(FIELD_NAME)-1) == 0) {
275 attr = get_key_attribute(temp2 + sizeof(FIELD_NAME)-1);
3e441450 276 if (attr != NULL && strcasecmp(attr, ATTR_PARTITIONS) == 0) {
50e5de03
KS
277 dbg_parse("creation of partition nodes requested");
278 dev.partitions = PARTITIONS_COUNT;
279 }
d94df232 280 strfieldcpy(dev.name, temp3);
3e441450 281 valid = 1;
d94df232
KS
282 continue;
283 }
284
285 if (strcasecmp(temp2, FIELD_SYMLINK) == 0) {
3d150dfb 286 strfieldcpy(dev.symlink, temp3);
3e441450 287 valid = 1;
d94df232
KS
288 continue;
289 }
3d150dfb 290
e41016d3
KS
291 if (strcasecmp(temp2, FIELD_OWNER) == 0) {
292 strfieldcpy(dev.owner, temp3);
3e441450 293 valid = 1;
e41016d3
KS
294 continue;
295 }
296
297 if (strcasecmp(temp2, FIELD_GROUP) == 0) {
298 strfieldcpy(dev.group, temp3);
3e441450 299 valid = 1;
e41016d3
KS
300 continue;
301 }
302
303 if (strcasecmp(temp2, FIELD_MODE) == 0) {
304 dev.mode = strtol(temp3, NULL, 8);
3e441450 305 valid = 1;
e41016d3
KS
306 continue;
307 }
308
851cd18d 309 dbg("unknown type of field '%s'", temp2);
851cd18d 310 goto error;
d94df232
KS
311 }
312
3e441450 313 /* skip line if not any valid key was found */
314 if (!valid)
315 goto error;
316
e41016d3 317 /* simple plausibility checks for given keys */
ac28b86d
KS
318 if ((dev.sysfs_pair[0].file[0] == '\0') ^
319 (dev.sysfs_pair[0].value[0] == '\0')) {
3e441450 320 info("inconsistency in " FIELD_SYSFS " key");
ac28b86d
KS
321 goto error;
322 }
323
324 if ((dev.result[0] != '\0') && (program_given == 0)) {
3e441450 325 info(FIELD_RESULT " is only useful when "
326 FIELD_PROGRAM " is called in any rule before");
d94df232 327 goto error;
19feb351
GKH
328 }
329
54988802 330 dev.config_line = lineno;
bd5f8e7c 331 strfieldcpy(dev.config_file, filename);
19feb351
GKH
332 retval = add_config_dev(&dev);
333 if (retval) {
334 dbg("add_config_dev returned with error %d", retval);
d94df232 335 continue;
ac28b86d 336error:
3e441450 337 info("parse error %s, line %d:%d, rule skipped",
338 filename, lineno, temp - line);
19feb351
GKH
339 }
340 }
c81b35c0
KS
341
342 file_unmap(buf, bufsize);
19feb351 343 return retval;
d94df232 344}
19feb351 345
e41245cb 346static int namedev_parse_permissions(char *filename)
19feb351 347{
3e441450 348 char line[LINE_SIZE];
349 char *bufline;
19feb351
GKH
350 char *temp;
351 char *temp2;
c81b35c0
KS
352 char *buf;
353 size_t bufsize;
354 size_t cur;
355 size_t count;
19feb351 356 int retval = 0;
61219c75 357 struct perm_device dev;
3e441450 358 int lineno;
19feb351 359
c81b35c0 360 if (file_map(filename, &buf, &bufsize) == 0) {
e41245cb 361 dbg("reading '%s' as permissions file", filename);
e8baccca 362 } else {
e41245cb 363 dbg("can't open '%s' as permissions file", filename);
c81b35c0 364 return -1;
19feb351
GKH
365 }
366
367 /* loop through the whole file */
c81b35c0 368 cur = 0;
3e441450 369 lineno = 0;
370 while (cur < bufsize) {
c81b35c0 371 count = buf_get_line(buf, bufsize, cur);
3e441450 372 bufline = &buf[cur];
c81b35c0 373 cur += count+1;
3e441450 374 lineno++;
19feb351 375
3e441450 376 if (count >= LINE_SIZE) {
377 info("line too long, rule skipped %s, line %d",
378 filename, lineno);
379 continue;
380 }
19feb351 381
3e441450 382 /* eat the whitespace */
3db7fa27 383 while ((count > 0) && isspace(bufline[0])) {
3e441450 384 bufline++;
385 count--;
386 }
3db7fa27
KS
387 if (count == 0)
388 continue;
3e441450 389
19feb351 390 /* see if this is a comment */
3e441450 391 if (bufline[0] == COMMENT_CHARACTER)
19feb351
GKH
392 continue;
393
3e441450 394 strncpy(line, bufline, count);
395 line[count] = '\0';
396 dbg_parse("read '%s'", line);
19feb351
GKH
397
398 /* parse the line */
3e441450 399 memset(&dev, 0x00, sizeof(struct perm_device));
400 temp = line;
401
19feb351
GKH
402 temp2 = strsep(&temp, ":");
403 if (!temp2) {
404 dbg("cannot parse line '%s'", line);
405 continue;
406 }
c472e3c8 407 strfieldcpy(dev.name, temp2);
19feb351
GKH
408
409 temp2 = strsep(&temp, ":");
410 if (!temp2) {
411 dbg("cannot parse line '%s'", line);
412 continue;
413 }
c472e3c8 414 strfieldcpy(dev.owner, temp2);
19feb351
GKH
415
416 temp2 = strsep(&temp, ":");
417 if (!temp2) {
418 dbg("cannot parse line '%s'", line);
419 continue;
420 }
c472e3c8 421 strfieldcpy(dev.group, temp2);
19feb351
GKH
422
423 if (!temp) {
e41016d3 424 dbg("cannot parse line '%s'", line);
19feb351
GKH
425 continue;
426 }
427 dev.mode = strtol(temp, NULL, 8);
428
429 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
e41016d3
KS
430 dev.name, dev.owner, dev.group, dev.mode);
431
61219c75 432 retval = add_perm_dev(&dev);
19feb351 433 if (retval) {
3d150dfb 434 dbg("add_perm_dev returned with error %d", retval);
19feb351
GKH
435 goto exit;
436 }
437 }
438
439exit:
c81b35c0 440 file_unmap(buf, bufsize);
19feb351 441 return retval;
267f534d 442}
19feb351 443
4a539daf 444int namedev_init_rules()
e41245cb 445{
e41245cb 446 struct stat stats;
e41245cb 447
4a539daf 448 stat(udev_rules_filename, &stats);
e41245cb 449 if ((stats.st_mode & S_IFMT) != S_IFDIR)
4a539daf
KS
450 return namedev_parse_rules(udev_rules_filename);
451 else
452 return call_foreach_file(namedev_parse_rules,
453 udev_rules_filename, RULEFILE_SUFFIX);
e41245cb
KS
454}
455
456int namedev_init_permissions()
457{
4a539daf
KS
458 struct stat stats;
459
460 stat(udev_permissions_filename, &stats);
461 if ((stats.st_mode & S_IFMT) != S_IFDIR)
462 return namedev_parse_permissions(udev_permissions_filename);
463 else
464 return call_foreach_file(namedev_parse_permissions,
465 udev_permissions_filename, PERMFILE_SUFFIX);
e41245cb 466}