6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include "libsysfs/sysfs/libsysfs.h"
38 #include "udev_libc_wrapper.h"
40 #include "udev_utils.h"
41 #include "udev_version.h"
43 #include "udev_rules.h"
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p, const char *s)
53 return (p[0] != '\0');
64 while ((p[0] != '\0') && (p[0] != ']')) {
67 if ((s[0] >= p[0]) && (s[0] <= p[2]))
71 match = (p[0] == s[0]);
75 while ((p[0] != '\0') && (p[0] != ']'))
78 return strcmp_pattern(p+1, s+1);
84 if (strcmp_pattern(p, s+1))
85 return strcmp_pattern(p+1, s);
93 if ((p[0] == s[0]) || (p[0] == '?'))
94 return strcmp_pattern(p+1, s+1);
100 /* extract possible {attr} and move str behind it */
101 static char *get_format_attribute(char **str)
106 if (*str[0] == '{') {
107 pos = strchr(*str, '}');
109 err("missing closing brace for format");
115 dbg("attribute='%s', str='%s'", attr, *str);
120 /* extract possible format length and move str behind it*/
121 static int get_format_len(char **str)
126 if (isdigit(*str[0])) {
127 num = (int) strtoul(*str, &tail, 10);
130 dbg("format length=%i", num);
133 err("format parsing error '%s'", *str);
139 static int get_key(char **line, char **key, char **value)
148 if (strchr(linepos, '\\')) {
149 dbg("escaped characters are not supported, skip");
153 /* skip whitespace */
154 while (isspace(linepos[0]))
161 if (linepos[0] == '\0')
163 if (isspace(linepos[0]))
165 if (linepos[0] == '=')
173 /* skip whitespace */
174 while (isspace(linepos[0]))
178 if (linepos[0] == '"') {
180 temp = strchr(linepos, '"');
182 dbg("missing closing quote");
185 dbg("value is quoted");
187 } else if (linepos[0] == '\'') {
189 temp = strchr(linepos, '\'');
191 dbg("missing closing quote");
194 dbg("value is quoted");
196 } else if (linepos[0] == '\0') {
197 dbg("value is empty");
200 while (temp[0] && !isspace(temp[0]))
209 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
211 char line[LINE_SIZE];
220 /* loop through the whole buffer */
223 while (cur < bufsize) {
224 count = buf_get_line(buf, bufsize, cur);
229 if (count >= sizeof(line)) {
230 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
234 /* eat the whitespace */
235 while ((count > 0) && isspace(bufline[0])) {
242 /* see if this is a comment */
243 if (bufline[0] == COMMENT_CHARACTER)
246 strlcpy(line, bufline, count+1);
249 if (get_key(&linepos, &variable, &value) == 0) {
250 dbg("import '%s=%s'", variable, value);
251 name_list_key_add(&udev->env_list, variable, value);
252 setenv(variable, value, 1);
259 static int import_file_into_env(struct udevice *udev, const char *filename)
264 if (file_map(filename, &buf, &bufsize) != 0) {
265 err("can't open '%s'", filename);
268 import_keys_into_env(udev, buf, bufsize);
269 file_unmap(buf, bufsize);
274 static int import_program_into_env(struct udevice *udev, const char *program)
279 if (execute_program(program, udev->subsystem, result, sizeof(result), &reslen) != 0)
281 return import_keys_into_env(udev, result, reslen);
284 /* finds the lowest positive N such that <name>N isn't present in the udevdb
285 * if <name> doesn't exist, 0 is returned, N otherwise
287 static int find_free_number(struct udevice *udev, const char *name)
289 char devpath[PATH_SIZE];
290 char filename[PATH_SIZE];
293 strlcpy(filename, name, sizeof(filename));
295 dbg("look for existing node '%s'", filename);
296 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
297 dbg("free num=%d", num);
303 info("find_free_number gone crazy (num=%d), aborted", num);
306 snprintf(filename, sizeof(filename), "%s%d", name, num);
307 filename[sizeof(filename)-1] = '\0';
311 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
312 const char *name, char *value, size_t len)
314 struct sysfs_attribute *tmpattr;
316 dbg("look for device attribute '%s'", name);
318 dbg("look for class attribute '%s/%s'", class_dev->path, name);
319 tmpattr = sysfs_get_classdev_attr(class_dev, name);
324 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
325 tmpattr = sysfs_get_device_attr(sysfs_device, name);
332 strlcpy(value, tmpattr->value, len);
333 remove_trailing_char(value, '\n');
335 dbg("found attribute '%s'", tmpattr->path);
339 #define WAIT_LOOP_PER_SECOND 20
340 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
342 char filename[PATH_SIZE];
344 int loop = timeout * WAIT_LOOP_PER_SECOND;
346 snprintf(filename, sizeof(filename), "%s%s/%s", sysfs_path, udev->devpath, file);
347 filename[sizeof(filename)-1] = '\0';
348 dbg("wait %i sec for '%s'", timeout, filename);
351 if (stat(filename, &stats) == 0) {
352 dbg("file appeared after %i loops", (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
355 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
357 dbg("waiting for '%s' failed", filename);
361 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
362 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
364 char temp[PATH_SIZE];
365 char temp2[PATH_SIZE];
366 char *head, *tail, *pos, *cpos, *attr, *rest;
369 unsigned int next_free_number;
370 struct sysfs_class_device *class_dev_parent;
388 static const struct subst_map {
391 enum subst_type type;
393 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
394 { .name = "id", .fmt = 'b', .type = SUBST_ID },
395 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
396 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL_NAME },
397 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
398 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
399 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
400 { .name = "sysfs", .fmt = 's', .type = SUBST_SYSFS },
401 { .name = "enum", .fmt = 'e', .type = SUBST_ENUM },
402 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
403 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
404 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
405 { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
406 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
409 enum subst_type type;
410 const struct subst_map *subst;
415 while (head[0] != '\0') {
416 if (head[0] == '$') {
417 /* substitute named variable */
420 if (head[1] == '$') {
421 strlcpy(temp, head+2, sizeof(temp));
422 strlcpy(head+1, temp, maxsize);
427 for (subst = map; subst->name; subst++) {
428 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
430 tail = head + strlen(subst->name)+1;
431 dbg("will substitute format name '%s'", subst->name);
436 else if (head[0] == '%') {
437 /* substitute format char */
440 if (head[1] == '%') {
441 strlcpy(temp, head+2, sizeof(temp));
442 strlcpy(head+1, temp, maxsize);
448 len = get_format_len(&tail);
449 for (subst = map; subst->name; subst++) {
450 if (tail[0] == subst->fmt) {
453 dbg("will substitute format char '%c'", subst->fmt);
462 attr = get_format_attribute(&tail);
463 strlcpy(temp, tail, sizeof(temp));
464 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
465 type ,string, tail, class_dev, sysfs_device);
469 strlcat(string, udev->devpath, maxsize);
470 dbg("substitute devpath '%s'", udev->devpath);
473 strlcat(string, udev->bus_id, maxsize);
474 dbg("substitute bus_id '%s'", udev->bus_id);
476 case SUBST_KERNEL_NAME:
477 strlcat(string, udev->kernel_name, maxsize);
478 dbg("substitute kernel name '%s'", udev->kernel_name);
480 case SUBST_KERNEL_NUMBER:
481 strlcat(string, udev->kernel_number, maxsize);
482 dbg("substitute kernel number '%s'", udev->kernel_number);
485 sprintf(temp2, "%d", major(udev->devt));
486 strlcat(string, temp2, maxsize);
487 dbg("substitute major number '%s'", temp2);
490 sprintf(temp2, "%d", minor(udev->devt));
491 strlcat(string, temp2, maxsize);
492 dbg("substitute minor number '%s'", temp2);
495 if (udev->program_result[0] == '\0')
497 /* get part part of the result string */
500 i = strtoul(attr, &rest, 10);
502 dbg("request part #%d of result string", i);
503 cpos = udev->program_result;
505 while (cpos[0] != '\0' && !isspace(cpos[0]))
507 while (isspace(cpos[0]))
511 err("requested part of result string not found");
514 strlcpy(temp2, cpos, sizeof(temp2));
515 /* %{2+}c copies the whole string from the second part on */
516 if (rest[0] != '+') {
517 cpos = strchr(temp2, ' ');
521 strlcat(string, temp2, maxsize);
522 dbg("substitute part of result string '%s'", temp2);
524 strlcat(string, udev->program_result, maxsize);
525 dbg("substitute result string '%s'", udev->program_result);
530 dbg("missing attribute");
533 if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
534 struct sysfs_device *parent_device;
536 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
537 parent_device = sysfs_get_device_parent(sysfs_device);
538 while (parent_device) {
539 dbg("looking at '%s'", parent_device->path);
540 if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
542 parent_device = sysfs_get_device_parent(parent_device);
547 /* strip trailing whitespace of sysfs value */
549 while (i > 0 && isspace(temp2[i-1]))
551 replace_untrusted_chars(temp2);
552 strlcat(string, temp2, maxsize);
553 dbg("substitute sysfs value '%s'", temp2);
556 next_free_number = find_free_number(udev, string);
557 if (next_free_number > 0) {
558 sprintf(temp2, "%d", next_free_number);
559 strlcat(string, temp2, maxsize);
565 class_dev_parent = sysfs_get_classdev_parent(class_dev);
566 if (class_dev_parent != NULL) {
567 struct udevice udev_parent;
569 dbg("found parent '%s', get the node name", class_dev_parent->path);
570 udev_init_device(&udev_parent, NULL, NULL, NULL);
571 /* lookup the name in the udev_db with the DEVPATH of the parent */
572 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
573 strlcat(string, udev_parent.name, maxsize);
574 dbg("substitute parent node name'%s'", udev_parent.name);
576 dbg("parent not found in database");
577 udev_cleanup_device(&udev_parent);
580 case SUBST_TEMP_NODE:
581 if (udev->tmp_node[0] == '\0') {
582 dbg("create temporary device node for callout");
583 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
584 udev_root, major(udev->devt), minor(udev->devt));
585 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
586 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
588 strlcat(string, udev->tmp_node, maxsize);
589 dbg("substitute temporary device node name '%s'", udev->tmp_node);
592 strlcat(string, udev_root, maxsize);
593 dbg("substitute udev_root '%s'", udev_root);
596 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
598 strlcat(string, temp2, maxsize);
599 dbg("substitute MODALIAS '%s'", temp2);
603 dbg("missing attribute");
609 strlcat(string, pos, maxsize);
610 dbg("substitute env '%s=%s'", attr, pos);
613 err("unknown substitution type=%i", type);
616 /* possibly truncate to format-char specified length */
619 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
621 strlcat(string, temp, maxsize);
625 static char *key_val(struct udev_rule *rule, struct key *key)
627 return rule->buf + key->val_off;
630 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
632 return rule->buf + pair->key_name_off;
635 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
640 if (key->operation == KEY_OP_UNSET)
643 key_value = rule->buf + key->val_off;
645 dbg("check for %s '%s' <-> '%s'", key_name, key_value, val);
646 match = (strcmp_pattern(key_value, val) == 0);
647 if (match && (key->operation != KEY_OP_NOMATCH)) {
648 dbg("%s is matching (matching value)", key_name);
651 if (!match && (key->operation == KEY_OP_NOMATCH)) {
652 dbg("%s is matching, (non matching value)", key_name);
656 dbg("%s is not matching", key_name);
660 static int match_rule(struct udevice *udev, struct udev_rule *rule,
661 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
663 struct sysfs_device *parent_device = sysfs_device;
665 if (match_key("ACTION", rule, &rule->action, udev->action))
668 if (match_key("KERNEL", rule, &rule->kernel_name, udev->kernel_name))
671 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->subsystem))
674 if (match_key("DEVPATH", rule, &rule->devpath, udev->devpath))
677 if (rule->modalias.operation != KEY_OP_UNSET) {
678 char value[NAME_SIZE];
680 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
681 dbg("MODALIAS value not found");
684 if (match_key("MODALIAS", rule, &rule->modalias, value))
688 if (rule->env.count) {
691 dbg("check %i ENV keys", rule->env.count);
692 for (i = 0; i < rule->env.count; i++) {
693 struct key_pair *pair = &rule->env.keys[i];
694 const char *key_name = key_pair_name(rule, pair);
695 const char *value = getenv(key_name);
698 dbg("ENV{'%s'} is not found", key_name);
701 if (match_key("ENV", rule, &pair->key, value))
704 dbg("all %i ENV keys matched", rule->env.count);
707 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
710 match = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
711 if (match && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH)) {
712 dbg("WAIT_FOR_SYSFS is matching (matching value)");
715 if (!match && (rule->wait_for_sysfs.operation == KEY_OP_NOMATCH)) {
716 dbg("WAIT_FOR_SYSFS is matching, (non matching value)");
719 dbg("WAIT_FOR_SYSFS is not matching");
723 /* walk up the chain of physical devices and find a match */
725 /* check for matching driver */
726 if (rule->driver.operation != KEY_OP_UNSET) {
727 if (parent_device == NULL) {
728 dbg("device has no sysfs_device");
731 if (match_key("DRIVER", rule, &rule->driver, parent_device->driver_name))
735 /* check for matching bus value */
736 if (rule->bus.operation != KEY_OP_UNSET) {
737 if (parent_device == NULL) {
738 dbg("device has no sysfs_device");
741 if (match_key("BUS", rule, &rule->bus, parent_device->bus))
745 /* check for matching bus id */
746 if (rule->id.operation != KEY_OP_UNSET) {
747 if (parent_device == NULL) {
748 dbg("device has no sysfs_device");
751 if (match_key("ID", rule, &rule->id, parent_device->bus_id))
755 /* check for matching sysfs pairs */
756 if (rule->sysfs.count) {
759 dbg("check %i SYSFS keys", rule->sysfs.count);
760 for (i = 0; i < rule->sysfs.count; i++) {
761 struct key_pair *pair = &rule->sysfs.keys[i];
762 const char *key_name = key_pair_name(rule, pair);
763 const char *key_value = key_val(rule, &pair->key);
764 char value[VALUE_SIZE];
767 if (find_sysfs_attribute(class_dev, parent_device, key_name, value, sizeof(value)) != 0)
770 /* strip trailing whitespace of value, if not asked to match for it */
771 len = strlen(key_value);
772 if (len && !isspace(key_value[len-1])) {
774 while (len > 0 && isspace(value[len-1]))
776 dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
779 if (match_key("SYSFS", rule, &pair->key, value))
782 dbg("all %i SYSFS keys matched", rule->sysfs.count);
785 /* found matching physical device */
788 dbg("try parent sysfs device");
789 parent_device = sysfs_get_device_parent(parent_device);
790 if (parent_device == NULL)
792 dbg("look at sysfs_device->path='%s'", parent_device->path);
793 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
796 /* import variables from file into environment */
797 if (rule->import.operation != KEY_OP_UNSET) {
798 char import[PATH_SIZE];
801 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
802 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
803 dbg("check for IMPORT import='%s'", import);
804 if (rule->import_exec) {
805 dbg("run executable file import='%s'", import);
806 rc = import_program_into_env(udev, import);
808 dbg("import file import='%s'", import);
809 rc = import_file_into_env(udev, import);
812 dbg("IMPORT failed");
813 if (rule->import.operation != KEY_OP_NOMATCH)
816 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
817 dbg("IMPORT key is true");
820 /* execute external program */
821 if (rule->program.operation != KEY_OP_UNSET) {
822 char program[PATH_SIZE];
823 char result[PATH_SIZE];
825 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
826 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
827 dbg("check for PROGRAM program='%s", program);
828 if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) {
829 dbg("PROGRAM is not matching");
830 if (rule->program.operation != KEY_OP_NOMATCH)
833 dbg("PROGRAM matches");
834 remove_trailing_char(result, '\n');
835 replace_untrusted_chars(result);
836 dbg("result is '%s'", result);
837 strlcpy(udev->program_result, result, sizeof(udev->program_result));
838 dbg("PROGRAM returned successful");
839 if (rule->program.operation == KEY_OP_NOMATCH)
842 dbg("PROGRAM key is true");
845 /* check for matching result of external program */
846 if (match_key("RESULT", rule, &rule->result, udev->program_result))
856 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct sysfs_class_device *class_dev)
858 struct sysfs_class_device *class_dev_parent;
859 struct sysfs_device *sysfs_device = NULL;
860 struct udev_rule *rule;
863 dbg("class_dev->name='%s'", class_dev->name);
865 /* Figure out where the "device"-symlink is at. For char devices this will
866 * always be in the class_dev->path. On block devices, only the main block
867 * device will have the device symlink in it's path. All partition devices
868 * need to look at the symlink in its parent directory.
870 class_dev_parent = sysfs_get_classdev_parent(class_dev);
871 if (class_dev_parent != NULL) {
872 dbg("given class device has a parent, use this instead");
873 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
875 sysfs_device = sysfs_get_classdev_device(class_dev);
879 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
880 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
881 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
884 dbg("udev->kernel_name='%s'", udev->kernel_name);
886 /* look for a matching rule to apply */
887 udev_rules_iter_init(rules);
889 rule = udev_rules_iter_next(rules);
893 if (name_set && rule->name.operation != KEY_OP_UNSET) {
894 dbg("node name already set, rule ignored");
899 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
901 if (rule->ignore_device) {
902 info("rule applied, '%s' is ignored", udev->kernel_name);
903 udev->ignore_device = 1;
906 if (rule->ignore_remove) {
907 udev->ignore_remove = 1;
908 dbg("remove event should be ignored");
910 /* apply all_partitions option only at a main block device */
911 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
912 udev->partitions = rule->partitions;
913 dbg("creation of partition nodes requested");
916 /* apply permissions */
917 if (!udev->mode_final && rule->mode != 0000) {
918 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
919 udev->mode_final = 1;
920 udev->mode = rule->mode;
921 dbg("applied mode=%#o to '%s'", rule->mode, udev->kernel_name);
923 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
924 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
925 udev->owner_final = 1;
926 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
927 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
928 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
930 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
931 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
932 udev->group_final = 1;
933 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
934 apply_format(udev, key_val(rule, &rule->group), sizeof(udev->group), class_dev, sysfs_device);
935 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
938 /* collect symlinks */
939 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
940 char temp[PATH_SIZE];
943 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
944 udev->symlink_final = 1;
945 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
946 struct name_entry *name_loop;
947 struct name_entry *temp_loop;
949 info("reset symlink list");
950 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
951 list_del(&name_loop->node);
955 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
956 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
957 info("rule applied, added symlink '%s'", temp);
959 /* add multiple symlinks separated by spaces */
961 while (isspace(pos[0]))
963 next = strchr(pos, ' ');
966 info("add symlink '%s'", pos);
967 name_list_add(&udev->symlink_list, pos, 0);
968 while (isspace(next[1]))
971 next = strchr(pos, ' ');
973 if (pos[0] != '\0') {
974 info("add symlink '%s'", pos);
975 name_list_add(&udev->symlink_list, pos, 0);
979 /* set name, later rules with name set will be ignored */
980 if (rule->name.operation != KEY_OP_UNSET) {
982 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
983 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
985 info("rule applied, '%s' becomes '%s'", udev->kernel_name, udev->name);
986 if (udev->type != DEV_NET)
987 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
988 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
991 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
992 char program[PATH_SIZE];
994 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
996 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
997 struct name_entry *name_loop;
998 struct name_entry *temp_loop;
1000 info("reset run list");
1001 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1002 list_del(&name_loop->node);
1006 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1007 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
1008 dbg("add run '%s'", program);
1009 name_list_add(&udev->run_list, program, 0);
1012 if (rule->last_rule) {
1013 dbg("last rule to be applied");
1020 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
1021 info("no rule found, will use kernel name '%s'", udev->name);
1024 if (udev->tmp_node[0] != '\0') {
1025 dbg("removing temporary device node");
1026 unlink_secure(udev->tmp_node);
1027 udev->tmp_node[0] = '\0';
1033 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sysfs_device *sysfs_device)
1035 struct udev_rule *rule;
1037 /* look for a matching rule to apply */
1038 udev_rules_iter_init(rules);
1040 rule = udev_rules_iter_next(rules);
1044 dbg("process rule");
1045 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1046 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1047 dbg("skip rule that names a device");
1051 if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
1052 if (rule->ignore_device) {
1053 info("rule applied, '%s' is ignored", udev->kernel_name);
1054 udev->ignore_device = 1;
1058 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1059 char program[PATH_SIZE];
1061 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1062 struct name_entry *name_loop;
1063 struct name_entry *temp_loop;
1065 info("reset run list");
1066 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1067 list_del(&name_loop->node);
1071 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1072 apply_format(udev, program, sizeof(program), NULL, sysfs_device);
1073 dbg("add run '%s'", program);
1074 name_list_add(&udev->run_list, program, 0);
1075 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1079 if (rule->last_rule) {
1080 dbg("last rule to be applied");