]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_rules_parse.c
rename config "filename" to "dir"
[thirdparty/systemd.git] / udev_rules_parse.c
1 /*
2 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 */
19
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28
29 #include "udev.h"
30 #include "udev_rules.h"
31
32
33 void udev_rules_iter_init(struct udev_rules *rules)
34 {
35 dbg("bufsize=%zi", rules->bufsize);
36 rules->current = 0;
37 }
38
39 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
40 {
41 static struct udev_rule *rule;
42
43 if (!rules)
44 return NULL;
45
46 dbg("current=%zi", rules->current);
47 if (rules->current >= rules->bufsize) {
48 dbg("no more rules");
49 return NULL;
50 }
51
52 /* get next rule */
53 rule = (struct udev_rule *) (rules->buf + rules->current);
54 rules->current += sizeof(struct udev_rule) + rule->bufsize;
55
56 return rule;
57 }
58
59 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
60 {
61 static struct udev_rule *rule;
62
63 next:
64 dbg("current=%zi", rules->current);
65 if (rules->current >= rules->bufsize) {
66 dbg("no more rules");
67 return NULL;
68 }
69 rule = (struct udev_rule *) (rules->buf + rules->current);
70
71 if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
72 dbg("moving forward, looking for label '%s'", label);
73 rules->current += sizeof(struct udev_rule) + rule->bufsize;
74 goto next;
75 }
76
77 dbg("found label '%s'", label);
78 return rule;
79 }
80
81 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
82 {
83 char *linepos;
84 char *temp;
85
86 linepos = *line;
87 if (linepos == NULL && linepos[0] == '\0')
88 return -1;
89
90 /* skip whitespace */
91 while (isspace(linepos[0]) || linepos[0] == ',')
92 linepos++;
93
94 /* get the key */
95 if (linepos[0] == '\0')
96 return -1;
97 *key = linepos;
98
99 while (1) {
100 linepos++;
101 if (linepos[0] == '\0')
102 return -1;
103 if (isspace(linepos[0]))
104 break;
105 if (linepos[0] == '=')
106 break;
107 if (linepos[0] == '+')
108 break;
109 if (linepos[0] == '!')
110 break;
111 if (linepos[0] == ':')
112 break;
113 }
114
115 /* remember end of key */
116 temp = linepos;
117
118 /* skip whitespace after key */
119 while (isspace(linepos[0]))
120 linepos++;
121 if (linepos[0] == '\0')
122 return -1;
123
124 /* get operation type */
125 if (linepos[0] == '=' && linepos[1] == '=') {
126 *operation = KEY_OP_MATCH;
127 linepos += 2;
128 dbg("operator=match");
129 } else if (linepos[0] == '!' && linepos[1] == '=') {
130 *operation = KEY_OP_NOMATCH;
131 linepos += 2;
132 dbg("operator=nomatch");
133 } else if (linepos[0] == '+' && linepos[1] == '=') {
134 *operation = KEY_OP_ADD;
135 linepos += 2;
136 dbg("operator=add");
137 } else if (linepos[0] == '=') {
138 *operation = KEY_OP_ASSIGN;
139 linepos++;
140 dbg("operator=assign");
141 } else if (linepos[0] == ':' && linepos[1] == '=') {
142 *operation = KEY_OP_ASSIGN_FINAL;
143 linepos += 2;
144 dbg("operator=assign_final");
145 } else
146 return -1;
147
148 /* terminate key */
149 temp[0] = '\0';
150 dbg("key='%s'", *key);
151
152 /* skip whitespace after operator */
153 while (isspace(linepos[0]))
154 linepos++;
155 if (linepos[0] == '\0')
156 return -1;
157
158 /* get the value*/
159 if (linepos[0] == '"')
160 linepos++;
161 else
162 return -1;
163 *value = linepos;
164
165 temp = strchr(linepos, '"');
166 if (!temp)
167 return -1;
168 temp[0] = '\0';
169 temp++;
170 dbg("value='%s'", *value);
171
172 /* move line to next key */
173 *line = temp;
174
175 return 0;
176 }
177
178 /* extract possible KEY{attr} */
179 static char *get_key_attribute(char *str)
180 {
181 char *pos;
182 char *attr;
183
184 attr = strchr(str, '{');
185 if (attr != NULL) {
186 attr++;
187 pos = strchr(attr, '}');
188 if (pos == NULL) {
189 err("missing closing brace for format");
190 return NULL;
191 }
192 pos[0] = '\0';
193 dbg("attribute='%s'", attr);
194 return attr;
195 }
196
197 return NULL;
198 }
199
200 static int add_rule_key(struct udev_rule *rule, struct key *key,
201 enum key_operation operation, const char *value)
202 {
203 size_t val_len = strnlen(value, PATH_SIZE);
204
205 key->operation = operation;
206
207 key->val_off = rule->bufsize;
208 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
209 rule->bufsize += val_len+1;
210
211 return 0;
212 }
213
214 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
215 enum key_operation operation, const char *key, const char *value)
216 {
217 size_t key_len = strnlen(key, PATH_SIZE);
218
219 if (pairs->count >= PAIRS_MAX) {
220 err("skip, too many keys of the same type in a single rule");
221 return -1;
222 }
223
224 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
225
226 /* add the key-name of the pair */
227 pairs->keys[pairs->count].key_name_off = rule->bufsize;
228 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
229 rule->bufsize += key_len+1;
230
231 pairs->count++;
232
233 return 0;
234 }
235
236 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
237 {
238 struct udev_rule *rule;
239 size_t rule_size;
240 int valid;
241 char *linepos;
242 char *attr;
243 size_t padding;
244 int physdev = 0;
245 int retval;
246
247 /* get all the keys */
248 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
249 if (!rule) {
250 err("malloc failed");
251 return -1;
252 }
253 linepos = line;
254 valid = 0;
255
256 while (1) {
257 char *key;
258 char *value;
259 enum key_operation operation = KEY_OP_UNSET;
260
261 retval = get_key(&linepos, &key, &operation, &value);
262 if (retval)
263 break;
264
265 if (strcasecmp(key, "ACTION") == 0) {
266 if (operation != KEY_OP_MATCH &&
267 operation != KEY_OP_NOMATCH) {
268 err("invalid ACTION operation");
269 goto invalid;
270 }
271 add_rule_key(rule, &rule->action, operation, value);
272 valid = 1;
273 continue;
274 }
275
276 if (strcasecmp(key, "DEVPATH") == 0) {
277 if (operation != KEY_OP_MATCH &&
278 operation != KEY_OP_NOMATCH) {
279 err("invalid DEVPATH operation");
280 goto invalid;
281 }
282 add_rule_key(rule, &rule->devpath, operation, value);
283 valid = 1;
284 continue;
285 }
286
287 if (strcasecmp(key, "KERNEL") == 0) {
288 if (operation != KEY_OP_MATCH &&
289 operation != KEY_OP_NOMATCH) {
290 err("invalid KERNEL operation");
291 goto invalid;
292 }
293 add_rule_key(rule, &rule->kernel, operation, value);
294 valid = 1;
295 continue;
296 }
297
298 if (strcasecmp(key, "SUBSYSTEM") == 0) {
299 if (operation != KEY_OP_MATCH &&
300 operation != KEY_OP_NOMATCH) {
301 err("invalid SUBSYSTEM operation");
302 goto invalid;
303 }
304 add_rule_key(rule, &rule->subsystem, operation, value);
305 valid = 1;
306 continue;
307 }
308
309 if (strcasecmp(key, "DRIVER") == 0) {
310 if (operation != KEY_OP_MATCH &&
311 operation != KEY_OP_NOMATCH) {
312 err("invalid DRIVER operation");
313 goto invalid;
314 }
315 err("DRIVER== will change in a future relase, "
316 "please use DRIVERS== in %s:%u", filename, lineno);
317 /* FIXME: this should be rule->driver to match only the event device */
318 add_rule_key(rule, &rule->drivers, operation, value);
319 valid = 1;
320 continue;
321 }
322
323 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
324 attr = get_key_attribute(key + sizeof("ATTR")-1);
325 if (attr == NULL) {
326 err("error parsing ATTR attribute");
327 goto invalid;
328 }
329 if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
330 goto invalid;
331 valid = 1;
332 continue;
333 }
334
335 if (strcasecmp(key, "KERNELS") == 0 ||
336 strcasecmp(key, "ID") == 0) {
337 if (operation != KEY_OP_MATCH &&
338 operation != KEY_OP_NOMATCH) {
339 err("invalid KERNELS operation");
340 goto invalid;
341 }
342 add_rule_key(rule, &rule->kernels, operation, value);
343 valid = 1;
344 continue;
345 }
346
347 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
348 strcasecmp(key, "BUS") == 0) {
349 if (operation != KEY_OP_MATCH &&
350 operation != KEY_OP_NOMATCH) {
351 err("invalid SUBSYSTEMS operation");
352 goto invalid;
353 }
354 add_rule_key(rule, &rule->subsystems, operation, value);
355 valid = 1;
356 continue;
357 }
358
359 if (strcasecmp(key, "DRIVERS") == 0) {
360 if (operation != KEY_OP_MATCH &&
361 operation != KEY_OP_NOMATCH) {
362 err("invalid DRIVERS operation");
363 goto invalid;
364 }
365 add_rule_key(rule, &rule->drivers, operation, value);
366 valid = 1;
367 continue;
368 }
369
370 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
371 strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
372 attr = get_key_attribute(key + sizeof("ATTRS")-1);
373 if (attr == NULL) {
374 err("error parsing ATTRS attribute");
375 goto invalid;
376 }
377 if (strncmp(attr, "device/", 7) == 0)
378 err("the 'device' link is deprecated and will be removed from a future kernel, "
379 "please fix it in %s:%u", filename, lineno);
380 else if (strchr(attr, '/') != NULL)
381 err("do not reference parent sysfs directories directly, that may break with a future kernel, "
382 "please fix it in %s:%u", filename, lineno);
383 if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
384 goto invalid;
385 valid = 1;
386 continue;
387 }
388
389 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
390 attr = get_key_attribute(key + sizeof("ENV")-1);
391 if (attr == NULL) {
392 err("error parsing ENV attribute");
393 goto invalid;
394 }
395 if (strncmp(attr, "PHYSDEV", 7) == 0)
396 physdev = 1;
397 if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
398 goto invalid;
399 valid = 1;
400 continue;
401 }
402
403 if (strcasecmp(key, "PROGRAM") == 0) {
404 add_rule_key(rule, &rule->program, operation, value);
405 valid = 1;
406 continue;
407 }
408
409 if (strcasecmp(key, "RESULT") == 0) {
410 if (operation != KEY_OP_MATCH &&
411 operation != KEY_OP_NOMATCH) {
412 err("invalid RESULT operation");
413 goto invalid;
414 }
415 add_rule_key(rule, &rule->result, operation, value);
416 valid = 1;
417 continue;
418 }
419
420 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
421 attr = get_key_attribute(key + sizeof("IMPORT")-1);
422 if (attr && strstr(attr, "program")) {
423 dbg("IMPORT will be executed");
424 rule->import_type = IMPORT_PROGRAM;
425 } else if (attr && strstr(attr, "file")) {
426 dbg("IMPORT will be included as file");
427 rule->import_type = IMPORT_FILE;
428 } else if (attr && strstr(attr, "parent")) {
429 dbg("IMPORT will include the parent values");
430 rule->import_type = IMPORT_PARENT;
431 } else {
432 /* figure it out if it is executable */
433 char file[PATH_SIZE];
434 char *pos;
435 struct stat stats;
436
437 strlcpy(file, value, sizeof(file));
438 pos = strchr(file, ' ');
439 if (pos)
440 pos[0] = '\0';
441
442 /* allow programs in /lib/udev called without the path */
443 if (strchr(file, '/') == NULL) {
444 strlcpy(file, "/lib/udev/", sizeof(file));
445 strlcat(file, value, sizeof(file));
446 pos = strchr(file, ' ');
447 if (pos)
448 pos[0] = '\0';
449 }
450
451 dbg("IMPORT auto mode for '%s'", file);
452 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
453 dbg("IMPORT is executable, will be executed (autotype)");
454 rule->import_type = IMPORT_PROGRAM;
455 } else {
456 dbg("IMPORT is not executable, will be included as file (autotype)");
457 rule->import_type = IMPORT_FILE;
458 }
459 }
460 add_rule_key(rule, &rule->import, operation, value);
461 valid = 1;
462 continue;
463 }
464
465 if (strcasecmp(key, "RUN") == 0) {
466 add_rule_key(rule, &rule->run, operation, value);
467 valid = 1;
468 continue;
469 }
470
471 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
472 add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
473 valid = 1;
474 continue;
475 }
476
477 if (strcasecmp(key, "LABEL") == 0) {
478 add_rule_key(rule, &rule->label, operation, value);
479 valid = 1;
480 continue;
481 }
482
483 if (strcasecmp(key, "GOTO") == 0) {
484 add_rule_key(rule, &rule->goto_label, operation, value);
485 valid = 1;
486 continue;
487 }
488
489 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
490 attr = get_key_attribute(key + sizeof("NAME")-1);
491 if (attr != NULL) {
492 if (strstr(attr, "all_partitions") != NULL) {
493 dbg("creation of partition nodes requested");
494 rule->partitions = DEFAULT_PARTITIONS_COUNT;
495 }
496 if (strstr(attr, "ignore_remove") != NULL) {
497 dbg("remove event should be ignored");
498 rule->ignore_remove = 1;
499 }
500 }
501 if (value[0] == '\0')
502 dbg("name empty, node creation supressed");
503 add_rule_key(rule, &rule->name, operation, value);
504 continue;
505 }
506
507 if (strcasecmp(key, "SYMLINK") == 0) {
508 add_rule_key(rule, &rule->symlink, operation, value);
509 valid = 1;
510 continue;
511 }
512
513 if (strcasecmp(key, "OWNER") == 0) {
514 valid = 1;
515 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
516 char *endptr;
517 strtoul(value, &endptr, 10);
518 if (endptr[0] != '\0') {
519 char owner[32];
520 uid_t uid = lookup_user(value);
521 dbg("replacing username='%s' by id=%i", value, uid);
522 sprintf(owner, "%u", (unsigned int) uid);
523 add_rule_key(rule, &rule->owner, operation, owner);
524 continue;
525 }
526 }
527
528 add_rule_key(rule, &rule->owner, operation, value);
529 continue;
530 }
531
532 if (strcasecmp(key, "GROUP") == 0) {
533 valid = 1;
534 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
535 char *endptr;
536 strtoul(value, &endptr, 10);
537 if (endptr[0] != '\0') {
538 char group[32];
539 gid_t gid = lookup_group(value);
540 dbg("replacing groupname='%s' by id=%i", value, gid);
541 sprintf(group, "%u", (unsigned int) gid);
542 add_rule_key(rule, &rule->group, operation, group);
543 continue;
544 }
545 }
546
547 add_rule_key(rule, &rule->group, operation, value);
548 continue;
549 }
550
551 if (strcasecmp(key, "MODE") == 0) {
552 rule->mode = strtol(value, NULL, 8);
553 rule->mode_operation = operation;
554 valid = 1;
555 continue;
556 }
557
558 if (strcasecmp(key, "OPTIONS") == 0) {
559 if (strstr(value, "last_rule") != NULL) {
560 dbg("last rule to be applied");
561 rule->last_rule = 1;
562 }
563 if (strstr(value, "ignore_device") != NULL) {
564 dbg("device should be ignored");
565 rule->ignore_device = 1;
566 }
567 if (strstr(value, "ignore_remove") != NULL) {
568 dbg("remove event should be ignored");
569 rule->ignore_remove = 1;
570 }
571 if (strstr(value, "all_partitions") != NULL) {
572 dbg("creation of partition nodes requested");
573 rule->partitions = DEFAULT_PARTITIONS_COUNT;
574 }
575 valid = 1;
576 continue;
577 }
578
579 err("unknown key '%s' in %s:%u", key, filename, lineno);
580 }
581
582 if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
583 err("PHYSDEV* values are deprecated and will be removed from a future kernel, "
584 "please fix it in %s:%u", filename, lineno);
585
586 /* skip line if not any valid key was found */
587 if (!valid)
588 goto invalid;
589
590 /* grow buffer and add rule */
591 rule_size = sizeof(struct udev_rule) + rule->bufsize;
592 padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
593 dbg("add %zi padding bytes", padding);
594 rule_size += padding;
595 rule->bufsize += padding;
596
597 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
598 if (!rules->buf) {
599 err("realloc failed");
600 goto exit;
601 }
602 dbg("adding rule to offset %zi", rules->bufsize);
603 memcpy(rules->buf + rules->bufsize, rule, rule_size);
604 rules->bufsize += rule_size;
605 exit:
606 free(rule);
607 return 0;
608
609 invalid:
610 free(rule);
611 err("invalid rule '%s:%u'", filename, lineno);
612 return -1;
613 }
614
615 static int parse_file(struct udev_rules *rules, const char *filename)
616 {
617 char line[LINE_SIZE];
618 char *bufline;
619 unsigned int lineno;
620 char *buf;
621 size_t bufsize;
622 size_t cur;
623 size_t count;
624 int retval = 0;
625
626 if (file_map(filename, &buf, &bufsize) != 0) {
627 err("can't open '%s' as rules file: %s", filename, strerror(errno));
628 return -1;
629 }
630 info("reading '%s' as rules file", filename);
631
632 /* loop through the whole file */
633 cur = 0;
634 lineno = 0;
635 while (cur < bufsize) {
636 unsigned int i, j;
637
638 count = buf_get_line(buf, bufsize, cur);
639 bufline = &buf[cur];
640 cur += count+1;
641 lineno++;
642
643 /* eat the whitespace */
644 while ((count > 0) && isspace(bufline[0])) {
645 bufline++;
646 count--;
647 }
648 if (count == 0)
649 continue;
650
651 /* see if this is a comment */
652 if (bufline[0] == COMMENT_CHARACTER)
653 continue;
654
655 if (count >= sizeof(line)) {
656 err("line too long, rule skipped '%s:%u'", filename, lineno);
657 continue;
658 }
659
660 /* skip backslash and newline from multiline rules */
661 for (i = j = 0; i < count; i++) {
662 if (bufline[i] == '\\' && bufline[i+1] == '\n')
663 continue;
664
665 line[j++] = bufline[i];
666 }
667 line[j] = '\0';
668
669 dbg("read '%s'", line);
670 add_to_rules(rules, line, filename, lineno);
671 }
672
673 file_unmap(buf, bufsize);
674 return retval;
675 }
676
677 int udev_rules_init(struct udev_rules *rules, int resolve_names)
678 {
679 struct stat stats;
680 int retval;
681
682 memset(rules, 0x00, sizeof(struct udev_rules));
683 rules->resolve_names = resolve_names;
684
685 /* parse rules file or all matching files in directory */
686 if (stat(udev_rules_dir, &stats) != 0)
687 return -1;
688
689 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
690 dbg("parse single rules file '%s'", udev_rules_dir);
691 retval = parse_file(rules, udev_rules_dir);
692 } else {
693 struct name_entry *name_loop, *name_tmp;
694 LIST_HEAD(name_list);
695
696 dbg("parse rules directory '%s'", udev_rules_dir);
697 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
698
699 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
700 if (stat(name_loop->name, &stats) == 0) {
701 if (stats.st_size)
702 parse_file(rules, name_loop->name);
703 else
704 dbg("empty rules file '%s'", name_loop->name);
705 } else
706 err("could not read '%s': %s", name_loop->name, strerror(errno));
707 list_del(&name_loop->node);
708 free(name_loop);
709 }
710 }
711
712 return retval;
713 }
714
715 void udev_rules_cleanup(struct udev_rules *rules)
716 {
717 if (rules->buf) {
718 free(rules->buf);
719 rules->buf = NULL;
720 }
721 }