4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include "udev_rules.h"
39 /* extract possible {attr} and move str behind it */
40 static char *get_format_attribute(char **str)
46 pos = strchr(*str, '}');
48 err("missing closing brace for format");
54 dbg("attribute='%s', str='%s'", attr, *str);
59 /* extract possible format length and move str behind it*/
60 static int get_format_len(char **str)
65 if (isdigit(*str[0])) {
66 num = (int) strtoul(*str, &tail, 10);
69 dbg("format length=%i", num);
72 err("format parsing error '%s'", *str);
78 static int get_key(char **line, char **key, char **value)
87 if (strchr(linepos, '\\')) {
88 dbg("escaped characters are not supported, skip");
93 while (isspace(linepos[0]))
100 if (linepos[0] == '\0')
102 if (isspace(linepos[0]))
104 if (linepos[0] == '=')
112 /* skip whitespace */
113 while (isspace(linepos[0]))
117 if (linepos[0] == '"') {
119 temp = strchr(linepos, '"');
121 dbg("missing closing quote");
124 dbg("value is quoted");
126 } else if (linepos[0] == '\'') {
128 temp = strchr(linepos, '\'');
130 dbg("missing closing quote");
133 dbg("value is quoted");
135 } else if (linepos[0] == '\0') {
136 dbg("value is empty");
139 while (temp[0] && !isspace(temp[0]))
148 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
150 char line[LINE_SIZE];
159 /* loop through the whole buffer */
162 while (cur < bufsize) {
163 count = buf_get_line(buf, bufsize, cur);
168 if (count >= sizeof(line)) {
169 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
173 /* eat the whitespace */
174 while ((count > 0) && isspace(bufline[0])) {
181 /* see if this is a comment */
182 if (bufline[0] == COMMENT_CHARACTER)
185 memcpy(line, bufline, count);
189 if (get_key(&linepos, &variable, &value) == 0) {
190 dbg("import '%s=%s'", variable, value);
191 name_list_key_add(&udev->env_list, variable, value);
192 setenv(variable, value, 1);
199 static int import_file_into_env(struct udevice *udev, const char *filename)
204 if (file_map(filename, &buf, &bufsize) != 0) {
205 err("can't open '%s': %s", filename, strerror(errno));
208 import_keys_into_env(udev, buf, bufsize);
209 file_unmap(buf, bufsize);
214 static int import_program_into_env(struct udevice *udev, const char *program)
219 if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_INFO)) != 0)
221 return import_keys_into_env(udev, result, reslen);
224 static int import_parent_into_env(struct udevice *udev, const char *filter)
226 struct sysfs_device *dev_parent;
229 dev_parent = sysfs_device_get_parent(udev->dev);
230 if (dev_parent != NULL) {
231 struct udevice *udev_parent;
232 struct name_entry *name_loop;
234 dbg("found parent '%s', get the node name", dev_parent->devpath);
235 udev_parent = udev_device_init();
236 if (udev_parent == NULL)
238 /* import the udev_db of the parent */
239 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
240 dbg("import stored parent env '%s'", udev_parent->name);
241 list_for_each_entry(name_loop, &udev_parent->env_list, node) {
242 char name[NAME_SIZE];
245 strlcpy(name, name_loop->name, sizeof(name));
246 pos = strchr(name, '=');
250 if (fnmatch(filter, name, 0) == 0) {
251 dbg("import key '%s'", name_loop->name);
252 name_list_add(&udev->env_list, name_loop->name, 0);
253 setenv(name, pos, 1);
255 dbg("skip key '%s'", name_loop->name);
260 dbg("parent not found in database");
261 udev_device_cleanup(udev_parent);
267 #define WAIT_LOOP_PER_SECOND 50
268 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
270 char devicepath[PATH_SIZE];
271 char filepath[PATH_SIZE];
273 int loop = timeout * WAIT_LOOP_PER_SECOND;
275 strlcpy(devicepath, sysfs_path, sizeof(devicepath));
276 strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
277 strlcpy(filepath, devicepath, sizeof(filepath));
278 strlcat(filepath, "/", sizeof(filepath));
279 strlcat(filepath, file, sizeof(filepath));
281 dbg("will wait %i sec for '%s'", timeout, filepath);
284 if (stat(filepath, &stats) == 0) {
285 info("file '%s' appeared after %i loops", filepath, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
288 /* make sure the device does not have disappeared in the meantime */
289 if (stat(devicepath, &stats) != 0) {
290 info("device disappeared while waiting for '%s'", filepath);
293 info("wait for '%s' for %i mseconds", filepath, 1000 / WAIT_LOOP_PER_SECOND);
294 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
296 err("waiting for '%s' failed", filepath);
300 void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
302 char temp[PATH_SIZE];
303 char temp2[PATH_SIZE];
304 char *head, *tail, *pos, *cpos, *attr, *rest;
323 static const struct subst_map {
326 enum subst_type type;
328 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
329 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
330 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
331 { .name = "id", .fmt = 'b', .type = SUBST_ID },
332 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
333 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
334 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
335 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
336 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
337 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
338 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
339 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
340 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
343 enum subst_type type;
344 const struct subst_map *subst;
349 while (head[0] != '\0') {
350 if (head[0] == '$') {
351 /* substitute named variable */
354 if (head[1] == '$') {
355 strlcpy(temp, head+2, sizeof(temp));
356 strlcpy(head+1, temp, maxsize);
361 for (subst = map; subst->name; subst++) {
362 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
364 tail = head + strlen(subst->name)+1;
365 dbg("will substitute format name '%s'", subst->name);
369 } else if (head[0] == '%') {
370 /* substitute format char */
373 if (head[1] == '%') {
374 strlcpy(temp, head+2, sizeof(temp));
375 strlcpy(head+1, temp, maxsize);
381 len = get_format_len(&tail);
382 for (subst = map; subst->name; subst++) {
383 if (tail[0] == subst->fmt) {
386 dbg("will substitute format char '%c'", subst->fmt);
395 attr = get_format_attribute(&tail);
396 strlcpy(temp, tail, sizeof(temp));
397 dbg("format=%i, string='%s', tail='%s'", type ,string, tail);
401 strlcat(string, udev->dev->devpath, maxsize);
402 dbg("substitute devpath '%s'", udev->dev->devpath);
405 strlcat(string, udev->dev->kernel, maxsize);
406 dbg("substitute kernel name '%s'", udev->dev->kernel);
408 case SUBST_KERNEL_NUMBER:
409 strlcat(string, udev->dev->kernel_number, maxsize);
410 dbg("substitute kernel number '%s'", udev->dev->kernel_number);
413 if (udev->dev_parent != NULL) {
414 strlcat(string, udev->dev_parent->kernel, maxsize);
415 dbg("substitute id '%s'", udev->dev_parent->kernel);
419 sprintf(temp2, "%d", major(udev->devt));
420 strlcat(string, temp2, maxsize);
421 dbg("substitute major number '%s'", temp2);
424 sprintf(temp2, "%d", minor(udev->devt));
425 strlcat(string, temp2, maxsize);
426 dbg("substitute minor number '%s'", temp2);
429 if (udev->program_result[0] == '\0')
431 /* get part part of the result string */
434 i = strtoul(attr, &rest, 10);
436 dbg("request part #%d of result string", i);
437 cpos = udev->program_result;
439 while (cpos[0] != '\0' && !isspace(cpos[0]))
441 while (isspace(cpos[0]))
445 err("requested part of result string not found");
448 strlcpy(temp2, cpos, sizeof(temp2));
449 /* %{2+}c copies the whole string from the second part on */
450 if (rest[0] != '+') {
451 cpos = strchr(temp2, ' ');
455 strlcat(string, temp2, maxsize);
456 dbg("substitute part of result string '%s'", temp2);
458 strlcat(string, udev->program_result, maxsize);
459 dbg("substitute result string '%s'", udev->program_result);
464 dbg("missing attribute");
467 struct sysfs_device *dev_parent;
470 dev_parent = udev->dev;
472 dbg("looking at '%s'", dev_parent->devpath);
473 value = sysfs_attr_get_value(dev_parent->devpath, attr);
475 strlcpy(temp2, value, sizeof(temp2));
478 dev_parent = sysfs_device_get_parent(dev_parent);
479 } while (dev_parent != NULL);
481 /* strip trailing whitespace of sysfs value */
483 while (i > 0 && isspace(temp2[i-1]))
485 count = replace_untrusted_chars(temp2);
487 info("%i untrusted character(s) replaced" , count);
488 strlcat(string, temp2, maxsize);
489 dbg("substitute sysfs value '%s'", temp2);
494 struct sysfs_device *dev_parent;
496 dev_parent = sysfs_device_get_parent(udev->dev);
497 if (dev_parent != NULL) {
498 struct udevice *udev_parent;
500 dbg("found parent '%s', get the node name", dev_parent->devpath);
501 udev_parent = udev_device_init();
502 if (udev_parent != NULL) {
503 /* lookup the name in the udev_db with the DEVPATH of the parent */
504 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
505 strlcat(string, udev_parent->name, maxsize);
506 dbg("substitute parent node name'%s'", udev_parent->name);
508 dbg("parent not found in database");
509 udev_device_cleanup(udev_parent);
514 case SUBST_TEMP_NODE:
515 if (udev->tmp_node[0] == '\0') {
516 dbg("create temporary device node for callout");
517 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
518 udev_root, major(udev->devt), minor(udev->devt));
519 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
520 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
522 strlcat(string, udev->tmp_node, maxsize);
523 dbg("substitute temporary device node name '%s'", udev->tmp_node);
526 strlcat(string, udev_root, maxsize);
527 dbg("substitute udev_root '%s'", udev_root);
531 dbg("missing attribute");
536 dbg("env '%s' not available", attr);
539 dbg("substitute env '%s=%s'", attr, pos);
540 strlcat(string, pos, maxsize);
543 err("unknown substitution type=%i", type);
546 /* possibly truncate to format-char specified length */
549 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
551 strlcat(string, temp, maxsize);
555 static char *key_val(struct udev_rule *rule, struct key *key)
557 return rule->buf + key->val_off;
560 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
562 return rule->buf + pair->key_name_off;
565 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
568 char value[PATH_SIZE];
572 if (key->operation != KEY_OP_MATCH &&
573 key->operation != KEY_OP_NOMATCH)
576 strlcpy(value, rule->buf + key->val_off, sizeof(value));
579 dbg("key %s value='%s'", key_name, key_value);
581 pos = strchr(key_value, '|');
586 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
587 match = (fnmatch(key_value, val, 0) == 0);
588 if (match && (key->operation != KEY_OP_NOMATCH)) {
589 dbg("%s is true (matching value)", key_name);
592 if (!match && (key->operation == KEY_OP_NOMATCH)) {
593 dbg("%s is true (non-matching value)", key_name);
598 dbg("%s is false", key_name);
602 /* match a single rule against a given device and possibly its parent devices */
603 static int match_rule(struct udevice *udev, struct udev_rule *rule)
607 if (match_key("ACTION", rule, &rule->action, udev->action))
610 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
613 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
616 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
619 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
622 /* match NAME against a value assigned by an earlier rule */
623 if (match_key("NAME", rule, &rule->name, udev->name))
626 for (i = 0; i < rule->env.count; i++) {
627 struct key_pair *pair = &rule->env.keys[i];
629 /* we only check for matches, assignments will be handled later */
630 if (pair->key.operation == KEY_OP_MATCH ||
631 pair->key.operation == KEY_OP_NOMATCH) {
632 const char *key_name = key_pair_name(rule, pair);
633 const char *value = getenv(key_name);
636 dbg("ENV{'%s'} is not set, treat as empty", key_name);
639 if (match_key("ENV", rule, &pair->key, value))
644 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
647 found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
648 if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
652 /* check for matching sysfs attribute pairs */
653 for (i = 0; i < rule->attr.count; i++) {
654 struct key_pair *pair = &rule->attr.keys[i];
656 if (pair->key.operation == KEY_OP_MATCH ||
657 pair->key.operation == KEY_OP_NOMATCH) {
658 const char *key_name = key_pair_name(rule, pair);
659 const char *key_value = key_val(rule, &pair->key);
661 char val[VALUE_SIZE];
664 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
667 strlcpy(val, value, sizeof(val));
669 /* strip trailing whitespace of value, if not asked to match for it */
670 len = strlen(key_value);
671 if (len > 0 && !isspace(key_value[len-1])) {
673 while (len > 0 && isspace(val[len-1]))
675 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
678 if (match_key("ATTR", rule, &pair->key, val))
683 /* walk up the chain of parent devices and find a match */
684 udev->dev_parent = udev->dev;
686 /* check for matching kernel device name */
687 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
690 /* check for matching subsystem value */
691 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
694 /* check for matching driver */
695 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
698 /* check for matching sysfs attrubute pairs */
699 for (i = 0; i < rule->attrs.count; i++) {
700 struct key_pair *pair = &rule->attrs.keys[i];
702 if (pair->key.operation == KEY_OP_MATCH ||
703 pair->key.operation == KEY_OP_NOMATCH) {
704 const char *key_name = key_pair_name(rule, pair);
705 const char *key_value = key_val(rule, &pair->key);
707 char val[VALUE_SIZE];
710 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
712 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
715 strlcpy(val, value, sizeof(val));
717 /* strip trailing whitespace of value, if not asked to match for it */
718 len = strlen(key_value);
719 if (len > 0 && !isspace(key_value[len-1])) {
721 while (len > 0 && isspace(val[len-1]))
723 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
726 if (match_key("ATTRS", rule, &pair->key, val))
731 /* found matching device */
734 /* move to parent device */
735 dbg("try parent sysfs device");
736 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
737 if (udev->dev_parent == NULL)
739 dbg("looking at dev_parent->devpath='%s'", udev->dev_parent->devpath);
740 dbg("looking at dev_parent->kernel='%s'", udev->dev_parent->kernel);
743 /* execute external program */
744 if (rule->program.operation != KEY_OP_UNSET) {
745 char program[PATH_SIZE];
746 char result[PATH_SIZE];
748 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
749 udev_rules_apply_format(udev, program, sizeof(program));
750 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_INFO)) != 0) {
751 dbg("PROGRAM is false");
752 udev->program_result[0] = '\0';
753 if (rule->program.operation != KEY_OP_NOMATCH)
758 dbg("PROGRAM matches");
759 remove_trailing_chars(result, '\n');
760 count = replace_untrusted_chars(result);
762 info("%i untrusted character(s) replaced" , count);
763 dbg("result is '%s'", result);
764 strlcpy(udev->program_result, result, sizeof(udev->program_result));
765 dbg("PROGRAM returned successful");
766 if (rule->program.operation == KEY_OP_NOMATCH)
769 dbg("PROGRAM key is true");
772 /* check for matching result of external program */
773 if (match_key("RESULT", rule, &rule->result, udev->program_result))
776 /* import variables returned from program or or file into environment */
777 if (rule->import.operation != KEY_OP_UNSET) {
778 char import[PATH_SIZE];
781 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
782 udev_rules_apply_format(udev, import, sizeof(import));
783 dbg("check for IMPORT import='%s'", import);
784 if (rule->import_type == IMPORT_PROGRAM) {
785 rc = import_program_into_env(udev, import);
786 } else if (rule->import_type == IMPORT_FILE) {
787 dbg("import file import='%s'", import);
788 rc = import_file_into_env(udev, import);
789 } else if (rule->import_type == IMPORT_PARENT) {
790 dbg("import parent import='%s'", import);
791 rc = import_parent_into_env(udev, import);
794 dbg("IMPORT failed");
795 if (rule->import.operation != KEY_OP_NOMATCH)
798 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
799 dbg("IMPORT key is true");
802 /* rule matches, if we have ENV assignments export it */
803 for (i = 0; i < rule->env.count; i++) {
804 struct key_pair *pair = &rule->env.keys[i];
806 if (pair->key.operation == KEY_OP_ASSIGN) {
807 const char *key_name = key_pair_name(rule, pair);
808 const char *value = key_val(rule, &pair->key);
809 char *key_value = name_list_key_add(&udev->env_list, key_name, value);
810 if (key_value == NULL)
813 udev_rules_apply_format(udev, key_value, NAME_SIZE);
815 dbg("export ENV '%s'", key_value);
819 /* if we have ATTR assignements write value to sysfs file */
820 for (i = 0; i < rule->attr.count; i++) {
821 struct key_pair *pair = &rule->attr.keys[i];
823 if (pair->key.operation == KEY_OP_ASSIGN) {
824 const char *key_name = key_pair_name(rule, pair);
825 const char *key_value = key_val(rule, &pair->key);
826 char attr[PATH_SIZE];
829 strlcpy(attr, sysfs_path, sizeof(attr));
830 strlcat(attr, udev->dev->devpath, sizeof(attr));
831 strlcat(attr, "/", sizeof(attr));
832 strlcat(attr, key_name, sizeof(attr));
833 dbg("write '%s' to '%s'", key_value, attr);
834 f = fopen(attr, "w");
836 if (fprintf(f, "%s\n", key_value) <= 0)
837 err("error writing ATTR{%s}: %s", attr, strerror(errno));
840 err("error opening ATTR{%s} for writing: %s", attr, strerror(errno));
849 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
851 struct udev_rule *rule;
854 dbg("udev->dev->devpath='%s'", udev->dev->devpath);
855 dbg("udev->dev->kernel='%s'", udev->dev->kernel);
857 /* look for a matching rule to apply */
858 udev_rules_iter_init(rules);
860 rule = udev_rules_iter_next(rules);
865 (rule->name.operation == KEY_OP_ASSIGN ||
866 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
867 rule->name.operation == KEY_OP_ADD)) {
868 dbg("node name already set, rule ignored");
873 if (match_rule(udev, rule) == 0) {
875 if (rule->ignore_device) {
876 info("rule applied, '%s' is ignored", udev->dev->kernel);
877 udev->ignore_device = 1;
880 if (rule->ignore_remove) {
881 udev->ignore_remove = 1;
882 dbg("remove event should be ignored");
884 /* apply all_partitions option only at a main block device */
885 if (rule->partitions &&
886 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
887 udev->partitions = rule->partitions;
888 dbg("creation of partition nodes requested");
891 /* apply permissions */
892 if (!udev->mode_final && rule->mode != 0000) {
893 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
894 udev->mode_final = 1;
895 udev->mode = rule->mode;
896 dbg("applied mode=%#o to '%s'", rule->mode, udev->dev->kernel);
898 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
899 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
900 udev->owner_final = 1;
901 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
902 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
903 dbg("applied owner='%s' to '%s'", udev->owner, udev->dev->kernel);
905 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
906 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
907 udev->group_final = 1;
908 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
909 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
910 dbg("applied group='%s' to '%s'", udev->group, udev->dev->kernel);
913 /* collect symlinks */
914 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
915 char temp[PATH_SIZE];
919 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
920 udev->symlink_final = 1;
921 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
922 info("reset symlink list");
923 name_list_cleanup(&udev->symlink_list);
925 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
926 udev_rules_apply_format(udev, temp, sizeof(temp));
927 count = replace_untrusted_chars(temp);
929 info("%i untrusted character(s) replaced" , count);
930 dbg("rule applied, added symlink(s) '%s'", temp);
932 /* add multiple symlinks separated by spaces */
934 while (isspace(pos[0]))
936 next = strchr(pos, ' ');
939 info("add symlink '%s'", pos);
940 name_list_add(&udev->symlink_list, pos, 0);
941 while (isspace(next[1]))
944 next = strchr(pos, ' ');
946 if (pos[0] != '\0') {
947 info("add symlink '%s'", pos);
948 name_list_add(&udev->symlink_list, pos, 0);
952 /* set name, later rules with name set will be ignored */
953 if (rule->name.operation == KEY_OP_ASSIGN ||
954 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
955 rule->name.operation == KEY_OP_ADD) {
959 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
960 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
961 count = replace_untrusted_chars(udev->name);
963 info("%i untrusted character(s) replaced", count);
965 info("rule applied, '%s' becomes '%s'", udev->dev->kernel, udev->name);
966 if (strcmp(udev->dev->subsystem, "net") != 0)
967 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
968 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
971 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
972 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
974 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
975 info("reset run list");
976 name_list_cleanup(&udev->run_list);
978 dbg("add run '%s'", key_val(rule, &rule->run));
979 name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
982 if (rule->last_rule) {
983 dbg("last rule to be applied");
987 if (rule->goto_label.operation != KEY_OP_UNSET) {
988 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
989 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
995 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
996 info("no node name set, will use kernel name '%s'", udev->name);
999 if (udev->tmp_node[0] != '\0') {
1000 dbg("removing temporary device node");
1001 unlink_secure(udev->tmp_node);
1002 udev->tmp_node[0] = '\0';
1008 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1010 struct udev_rule *rule;
1012 dbg("udev->kernel='%s'", udev->dev->kernel);
1014 /* look for a matching rule to apply */
1015 udev_rules_iter_init(rules);
1017 rule = udev_rules_iter_next(rules);
1021 dbg("process rule");
1022 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1023 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1024 dbg("skip rule that names a device");
1028 if (match_rule(udev, rule) == 0) {
1029 if (rule->ignore_device) {
1030 info("rule applied, '%s' is ignored", udev->dev->kernel);
1031 udev->ignore_device = 1;
1035 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1036 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1037 info("reset run list");
1038 name_list_cleanup(&udev->run_list);
1040 dbg("add run '%s'", key_val(rule, &rule->run));
1041 name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1042 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1046 if (rule->last_rule) {
1047 dbg("last rule to be applied");
1051 if (rule->goto_label.operation != KEY_OP_UNSET) {
1052 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1053 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));