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.
35 #include "libsysfs/sysfs/libsysfs.h"
37 #include "udev_libc_wrapper.h"
39 #include "udev_utils.h"
40 #include "udev_version.h"
42 #include "udev_rules.h"
46 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
47 static int strcmp_pattern(const char *p, const char *s)
52 return (p[0] != '\0');
63 while ((p[0] != '\0') && (p[0] != ']')) {
66 if ((s[0] >= p[0]) && (s[0] <= p[2]))
70 match = (p[0] == s[0]);
74 while ((p[0] != '\0') && (p[0] != ']'))
77 return strcmp_pattern(p+1, s+1);
83 if (strcmp_pattern(p, s+1))
84 return strcmp_pattern(p+1, s);
92 if ((p[0] == s[0]) || (p[0] == '?'))
93 return strcmp_pattern(p+1, s+1);
99 /* extract possible {attr} and move str behind it */
100 static char *get_format_attribute(char **str)
105 if (*str[0] == '{') {
106 pos = strchr(*str, '}');
108 err("missing closing brace for format");
114 dbg("attribute='%s', str='%s'", attr, *str);
119 /* extract possible format length and move str behind it*/
120 static int get_format_len(char **str)
125 if (isdigit(*str[0])) {
126 num = (int) strtoul(*str, &tail, 10);
129 dbg("format length=%i", num);
132 err("format parsing error '%s'", *str);
138 static int get_key(char **line, char **key, char **value)
147 if (strchr(linepos, '\\')) {
148 dbg("escaped characters are not supported, skip");
152 /* skip whitespace */
153 while (isspace(linepos[0]))
160 if (linepos[0] == '\0')
162 if (isspace(linepos[0]))
164 if (linepos[0] == '=')
172 /* skip whitespace */
173 while (isspace(linepos[0]))
177 if (linepos[0] == '"') {
179 temp = strchr(linepos, '"');
181 dbg("missing closing quote");
184 dbg("value is quoted");
186 } else if (linepos[0] == '\'') {
188 temp = strchr(linepos, '\'');
190 dbg("missing closing quote");
193 dbg("value is quoted");
195 } else if (linepos[0] == '\0') {
196 dbg("value is empty");
199 while (temp[0] && !isspace(temp[0]))
208 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
210 char line[LINE_SIZE];
219 /* loop through the whole buffer */
222 while (cur < bufsize) {
223 count = buf_get_line(buf, bufsize, cur);
228 if (count >= sizeof(line)) {
229 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
233 /* eat the whitespace */
234 while ((count > 0) && isspace(bufline[0])) {
241 /* see if this is a comment */
242 if (bufline[0] == COMMENT_CHARACTER)
245 strlcpy(line, bufline, count+1);
248 if (get_key(&linepos, &variable, &value) == 0) {
249 dbg("import '%s=%s'", variable, value);
250 name_list_key_add(&udev->env_list, variable, value);
251 setenv(variable, value, 1);
258 static int import_file_into_env(struct udevice *udev, const char *filename)
263 if (file_map(filename, &buf, &bufsize) != 0) {
264 err("can't open '%s'", filename);
267 import_keys_into_env(udev, buf, bufsize);
268 file_unmap(buf, bufsize);
273 static int import_program_into_env(struct udevice *udev, const char *program)
278 if (execute_program(program, udev->subsystem, result, sizeof(result), &reslen) != 0)
280 return import_keys_into_env(udev, result, reslen);
283 /* finds the lowest positive N such that <name>N isn't present in the udevdb
284 * if <name> doesn't exist, 0 is returned, N otherwise
286 static int find_free_number(struct udevice *udev, const char *name)
288 char devpath[PATH_SIZE];
289 char filename[PATH_SIZE];
292 strlcpy(filename, name, sizeof(filename));
294 dbg("look for existing node '%s'", filename);
295 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
296 dbg("free num=%d", num);
302 info("find_free_number gone crazy (num=%d), aborted", num);
305 snprintf(filename, sizeof(filename), "%s%d", name, num);
306 filename[sizeof(filename)-1] = '\0';
310 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
311 const char *name, char *value, size_t len)
313 struct sysfs_attribute *tmpattr;
315 dbg("look for device attribute '%s'", name);
317 dbg("look for class attribute '%s/%s'", class_dev->path, name);
318 tmpattr = sysfs_get_classdev_attr(class_dev, name);
323 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
324 tmpattr = sysfs_get_device_attr(sysfs_device, name);
331 strlcpy(value, tmpattr->value, len);
332 remove_trailing_char(value, '\n');
334 dbg("found attribute '%s'", tmpattr->path);
338 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
339 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
341 char temp[PATH_SIZE];
342 char temp2[PATH_SIZE];
343 char *head, *tail, *pos, *cpos, *attr, *rest;
346 unsigned int next_free_number;
347 struct sysfs_class_device *class_dev_parent;
365 static const struct subst_map {
368 enum subst_type type;
370 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
371 { .name = "id", .fmt = 'b', .type = SUBST_ID },
372 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
373 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL_NAME },
374 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
375 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
376 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
377 { .name = "sysfs", .fmt = 's', .type = SUBST_SYSFS },
378 { .name = "enum", .fmt = 'e', .type = SUBST_ENUM },
379 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
380 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
381 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
382 { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
383 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
386 enum subst_type type;
387 const struct subst_map *subst;
392 while (head[0] != '\0') {
393 if (head[0] == '$') {
394 /* substitute named variable */
397 if (head[1] == '$') {
398 strlcpy(temp, head+2, sizeof(temp));
399 strlcpy(head+1, temp, maxsize);
404 for (subst = map; subst->name; subst++) {
405 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
407 tail = head + strlen(subst->name)+1;
408 dbg("will substitute format name '%s'", subst->name);
413 else if (head[0] == '%') {
414 /* substitute format char */
417 if (head[1] == '%') {
418 strlcpy(temp, head+2, sizeof(temp));
419 strlcpy(head+1, temp, maxsize);
425 len = get_format_len(&tail);
426 for (subst = map; subst->name; subst++) {
427 if (tail[0] == subst->fmt) {
430 dbg("will substitute format char '%c'", subst->fmt);
439 attr = get_format_attribute(&tail);
440 strlcpy(temp, tail, sizeof(temp));
441 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
442 type ,string, tail, class_dev, sysfs_device);
446 strlcat(string, udev->devpath, maxsize);
447 dbg("substitute devpath '%s'", udev->devpath);
450 strlcat(string, udev->bus_id, maxsize);
451 dbg("substitute bus_id '%s'", udev->bus_id);
453 case SUBST_KERNEL_NAME:
454 strlcat(string, udev->kernel_name, maxsize);
455 dbg("substitute kernel name '%s'", udev->kernel_name);
457 case SUBST_KERNEL_NUMBER:
458 strlcat(string, udev->kernel_number, maxsize);
459 dbg("substitute kernel number '%s'", udev->kernel_number);
462 sprintf(temp2, "%d", major(udev->devt));
463 strlcat(string, temp2, maxsize);
464 dbg("substitute major number '%s'", temp2);
467 sprintf(temp2, "%d", minor(udev->devt));
468 strlcat(string, temp2, maxsize);
469 dbg("substitute minor number '%s'", temp2);
472 if (udev->program_result[0] == '\0')
474 /* get part part of the result string */
477 i = strtoul(attr, &rest, 10);
479 dbg("request part #%d of result string", i);
480 cpos = udev->program_result;
482 while (cpos[0] != '\0' && !isspace(cpos[0]))
484 while (isspace(cpos[0]))
488 err("requested part of result string not found");
491 strlcpy(temp2, cpos, sizeof(temp2));
492 /* %{2+}c copies the whole string from the second part on */
493 if (rest[0] != '+') {
494 cpos = strchr(temp2, ' ');
498 strlcat(string, temp2, maxsize);
499 dbg("substitute part of result string '%s'", temp2);
501 strlcat(string, udev->program_result, maxsize);
502 dbg("substitute result string '%s'", udev->program_result);
507 dbg("missing attribute");
510 if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
511 struct sysfs_device *parent_device;
513 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
514 parent_device = sysfs_get_device_parent(sysfs_device);
515 while (parent_device) {
516 dbg("looking at '%s'", parent_device->path);
517 if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
519 parent_device = sysfs_get_device_parent(parent_device);
524 /* strip trailing whitespace of sysfs value */
526 while (i > 0 && isspace(temp2[i-1]))
528 replace_untrusted_chars(temp2);
529 strlcat(string, temp2, maxsize);
530 dbg("substitute sysfs value '%s'", temp2);
533 next_free_number = find_free_number(udev, string);
534 if (next_free_number > 0) {
535 sprintf(temp2, "%d", next_free_number);
536 strlcat(string, temp2, maxsize);
542 class_dev_parent = sysfs_get_classdev_parent(class_dev);
543 if (class_dev_parent != NULL) {
544 struct udevice udev_parent;
546 dbg("found parent '%s', get the node name", class_dev_parent->path);
547 udev_init_device(&udev_parent, NULL, NULL, NULL);
548 /* lookup the name in the udev_db with the DEVPATH of the parent */
549 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
550 strlcat(string, udev_parent.name, maxsize);
551 dbg("substitute parent node name'%s'", udev_parent.name);
553 dbg("parent not found in database");
554 udev_cleanup_device(&udev_parent);
557 case SUBST_TEMP_NODE:
558 if (udev->tmp_node[0] == '\0') {
559 dbg("create temporary device node for callout");
560 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
561 udev_root, major(udev->devt), minor(udev->devt));
562 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
563 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
565 strlcat(string, udev->tmp_node, maxsize);
566 dbg("substitute temporary device node name '%s'", udev->tmp_node);
569 strlcat(string, udev_root, maxsize);
570 dbg("substitute udev_root '%s'", udev_root);
573 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
575 strlcat(string, temp2, maxsize);
576 dbg("substitute MODALIAS '%s'", temp2);
580 dbg("missing attribute");
586 strlcat(string, pos, maxsize);
587 dbg("substitute env '%s=%s'", attr, pos);
590 err("unknown substitution type=%i", type);
593 /* possibly truncate to format-char specified length */
596 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
598 strlcat(string, temp, maxsize);
602 static int match_key(const char *key, const char *key_val, enum key_operation key_op, const char *val)
606 if (key_op == KEY_OP_UNSET)
609 dbg("check for %s '%s' <-> '%s'", key, key_val, val);
610 match = (strcmp_pattern(key_val, val) == 0);
611 if (match && (key_op != KEY_OP_NOMATCH)) {
612 dbg("%s key is matching (matching value)", key);
615 if (!match && (key_op == KEY_OP_NOMATCH)) {
616 dbg("%s key is matching, (non matching value)", key);
620 dbg("%s key is not matching", key);
624 static int match_rule(struct udevice *udev, struct udev_rule *rule,
625 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
627 struct sysfs_device *parent_device = sysfs_device;
629 if (match_key(KEY_ACTION, rule->action, rule->action_operation, udev->action))
632 if (match_key(KEY_KERNEL, rule->kernel_name, rule->kernel_operation, udev->kernel_name))
635 if (match_key(KEY_SUBSYSTEM, rule->subsystem, rule->subsystem_operation, udev->subsystem))
638 if (match_key(KEY_DEVPATH, rule->devpath, rule->devpath_operation, udev->devpath))
641 if (rule->modalias_operation != KEY_OP_UNSET) {
642 char value[NAME_SIZE];
644 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
645 dbg(KEY_MODALIAS " value not found");
648 if (match_key(KEY_MODALIAS, rule->modalias, rule->modalias_operation, value))
652 if (rule->env_pair_count) {
655 dbg("check for " KEY_ENV " pairs");
656 for (i = 0; i < rule->env_pair_count; i++) {
657 struct key_pair *pair;
660 pair = &rule->env_pair[i];
661 value = getenv(pair->name);
663 dbg(KEY_ENV "{'%s'} is not found", pair->name);
666 dbg("check %i " KEY_ENV " keys", rule->env_pair_count);
667 if (match_key(pair->name, pair->value, pair->operation, value))
670 dbg("all %i " KEY_ENV " keys matched", rule->env_pair_count);
673 /* walk up the chain of physical devices and find a match */
675 /* check for matching driver */
676 if (rule->driver_operation != KEY_OP_UNSET) {
677 if (parent_device == NULL) {
678 dbg("device has no sysfs_device");
681 if (match_key(KEY_BUS, rule->driver, rule->driver_operation, parent_device->driver_name))
685 /* check for matching bus value */
686 if (rule->bus_operation != KEY_OP_UNSET) {
687 if (parent_device == NULL) {
688 dbg("device has no sysfs_device");
691 if (match_key(KEY_BUS, rule->bus, rule->bus_operation, parent_device->bus))
695 /* check for matching bus id */
696 if (rule->id_operation != KEY_OP_UNSET) {
697 if (parent_device == NULL) {
698 dbg("device has no sysfs_device");
701 if (match_key(KEY_ID, rule->id, rule->id_operation, parent_device->bus_id))
705 /* check for matching sysfs pairs */
706 if (rule->sysfs_pair_count) {
709 dbg("check " KEY_SYSFS " pairs");
710 for (i = 0; i < rule->sysfs_pair_count; i++) {
711 struct key_pair *pair;
712 char value[VALUE_SIZE];
715 pair = &rule->sysfs_pair[i];
716 if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
719 /* strip trailing whitespace of value, if not asked to match for it */
720 len = strlen(pair->value);
721 if (len && !isspace(pair->value[len-1])) {
723 while (len > 0 && isspace(value[len-1]))
725 dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
728 dbg("check %i " KEY_SYSFS " keys", rule->sysfs_pair_count);
729 if (match_key(pair->name, pair->value, pair->operation, value))
732 dbg("all %i " KEY_SYSFS " keys matched", rule->sysfs_pair_count);
735 /* found matching physical device */
738 dbg("try parent sysfs device");
739 parent_device = sysfs_get_device_parent(parent_device);
740 if (parent_device == NULL)
742 dbg("look at sysfs_device->path='%s'", parent_device->path);
743 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
746 /* import variables from file into environment */
747 if (rule->import_operation != KEY_OP_UNSET) {
748 char import[PATH_SIZE];
751 strlcpy(import, rule->import, sizeof(import));
752 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
753 dbg("check for " KEY_IMPORT " import='%s'", import);
754 if (rule->import_exec) {
755 dbg("run executable file import='%s'", import);
756 rc = import_program_into_env(udev, import);
758 dbg("import file import='%s'", import);
759 rc = import_file_into_env(udev, import);
762 dbg(KEY_IMPORT " failed");
763 if (rule->import_operation != KEY_OP_NOMATCH)
766 dbg(KEY_IMPORT " '%s' imported", rule->import);
767 dbg(KEY_IMPORT " key is true");
770 /* execute external program */
771 if (rule->program_operation != KEY_OP_UNSET) {
772 char program[PATH_SIZE];
773 char result[PATH_SIZE];
775 strlcpy(program, rule->program, sizeof(program));
776 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
777 dbg("check for " KEY_PROGRAM " program='%s", program);
778 if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) {
779 dbg(KEY_PROGRAM " is not matching");
780 if (rule->program_operation != KEY_OP_NOMATCH)
783 dbg(KEY_PROGRAM " matches");
784 remove_trailing_char(result, '\n');
785 replace_untrusted_chars(result);
786 dbg("result is '%s'", result);
787 strlcpy(udev->program_result, result, sizeof(udev->program_result));
788 dbg(KEY_PROGRAM " returned successful");
789 if (rule->program_operation == KEY_OP_NOMATCH)
792 dbg(KEY_PROGRAM " key is true");
795 /* check for matching result of external program */
796 if (match_key(KEY_RESULT, rule->result, rule->result_operation, udev->program_result))
806 int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
808 struct sysfs_class_device *class_dev_parent;
809 struct sysfs_device *sysfs_device = NULL;
810 struct udev_rule *rule;
812 dbg("class_dev->name='%s'", class_dev->name);
814 /* Figure out where the "device"-symlink is at. For char devices this will
815 * always be in the class_dev->path. On block devices, only the main block
816 * device will have the device symlink in it's path. All partition devices
817 * need to look at the symlink in its parent directory.
819 class_dev_parent = sysfs_get_classdev_parent(class_dev);
820 if (class_dev_parent != NULL) {
821 dbg("given class device has a parent, use this instead");
822 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
824 sysfs_device = sysfs_get_classdev_device(class_dev);
828 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
829 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
830 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
833 dbg("udev->kernel_name='%s'", udev->kernel_name);
835 /* look for a matching rule to apply */
836 udev_rules_iter_init();
838 rule = udev_rules_iter_next();
842 if (udev->name_set && rule->name_operation != KEY_OP_UNSET) {
843 dbg("node name already set, rule ignored");
848 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
850 if (rule->ignore_device) {
851 info("configured rule in '%s[%i]' applied, '%s' is ignored",
852 rule->config_file, rule->config_line, udev->kernel_name);
853 udev->ignore_device = 1;
856 if (rule->ignore_remove) {
857 udev->ignore_remove = 1;
858 dbg("remove event should be ignored");
860 /* apply all_partitions option only at a main block device */
861 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
862 udev->partitions = rule->partitions;
863 dbg("creation of partition nodes requested");
866 /* apply permissions */
867 if (!udev->mode_final && rule->mode != 0000) {
868 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
869 udev->mode_final = 1;
870 udev->mode = rule->mode;
871 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
873 if (!udev->owner_final && rule->owner[0] != '\0') {
874 if (rule->owner_operation == KEY_OP_ASSIGN_FINAL)
875 udev->owner_final = 1;
876 strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
877 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
878 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
880 if (!udev->group_final && rule->group[0] != '\0') {
881 if (rule->group_operation == KEY_OP_ASSIGN_FINAL)
882 udev->group_final = 1;
883 strlcpy(udev->group, rule->group, sizeof(udev->group));
884 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
885 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
888 /* collect symlinks */
889 if (!udev->symlink_final && rule->symlink_operation != KEY_OP_UNSET) {
890 char temp[PATH_SIZE];
893 if (rule->symlink_operation == KEY_OP_ASSIGN_FINAL)
894 udev->symlink_final = 1;
895 if (rule->symlink_operation == KEY_OP_ASSIGN || rule->symlink_operation == KEY_OP_ASSIGN_FINAL) {
896 struct name_entry *name_loop;
897 struct name_entry *temp_loop;
899 info("reset symlink list");
900 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
901 list_del(&name_loop->node);
905 if (rule->symlink[0] != '\0') {
906 info("configured rule in '%s[%i]' applied, added symlink '%s'",
907 rule->config_file, rule->config_line, rule->symlink);
908 strlcpy(temp, rule->symlink, sizeof(temp));
909 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
911 /* add multiple symlinks separated by spaces */
913 next = strchr(temp, ' ');
916 info("add symlink '%s'", pos);
917 name_list_add(&udev->symlink_list, pos, 0);
919 next = strchr(pos, ' ');
921 info("add symlink '%s'", pos);
922 name_list_add(&udev->symlink_list, pos, 0);
926 /* set name, later rules with name set will be ignored */
927 if (rule->name_operation != KEY_OP_UNSET) {
929 if (rule->name[0] == '\0') {
930 info("configured rule in '%s[%i]' applied, node handling for '%s' supressed",
931 rule->config_file, rule->config_line, udev->kernel_name);
933 strlcpy(udev->name, rule->name, sizeof(udev->name));
934 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
935 strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
936 udev->config_line = rule->config_line;
938 info("configured rule in '%s:%i' applied, '%s' becomes '%s'",
939 rule->config_file, rule->config_line, udev->kernel_name, rule->name);
940 if (udev->type != DEV_NET)
941 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
942 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
946 if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
947 char program[PATH_SIZE];
949 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
951 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
952 struct name_entry *name_loop;
953 struct name_entry *temp_loop;
955 info("reset run list");
956 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
957 list_del(&name_loop->node);
961 if (rule->run[0] != '\0') {
962 strlcpy(program, rule->run, sizeof(program));
963 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
964 dbg("add run '%s'", program);
965 name_list_add(&udev->run_list, program, 0);
969 if (rule->last_rule) {
970 dbg("last rule to be applied");
976 if (udev->name[0] == '\0') {
977 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
978 info("no rule found, use kernel name '%s'", udev->name);
981 if (udev->tmp_node[0] != '\0') {
982 dbg("removing temporary device node");
983 unlink_secure(udev->tmp_node);
984 udev->tmp_node[0] = '\0';
990 int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device)
992 struct udev_rule *rule;
994 /* look for a matching rule to apply */
995 udev_rules_iter_init();
997 rule = udev_rules_iter_next();
1001 dbg("process rule");
1002 if (rule->name_operation != KEY_OP_UNSET || rule->symlink_operation != KEY_OP_UNSET ||
1003 rule->mode != 0000 || rule->owner[0] != '\0' || rule->group[0] != '\0') {
1004 dbg("skip rule that names a device");
1008 if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
1009 if (rule->ignore_device) {
1010 info("configured rule in '%s[%i]' applied, '%s' is ignored",
1011 rule->config_file, rule->config_line, udev->kernel_name);
1012 udev->ignore_device = 1;
1016 if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
1017 char program[PATH_SIZE];
1019 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
1020 struct name_entry *name_loop;
1021 struct name_entry *temp_loop;
1023 info("reset run list");
1024 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1025 list_del(&name_loop->node);
1029 if (rule->run[0] != '\0') {
1030 strlcpy(program, rule->run, sizeof(program));
1031 apply_format(udev, program, sizeof(program), NULL, sysfs_device);
1032 dbg("add run '%s'", program);
1033 name_list_add(&udev->run_list, program, 0);
1035 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
1039 if (rule->last_rule) {
1040 dbg("last rule to be applied");