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 /** Finds the lowest positive N such that <name>N isn't present in
139 * $(udevroot) either as a file or a symlink.
141 * @param name Name to check for
142 * @return 0 if <name> didn't exist and N otherwise.
144 static int find_free_number(struct udevice *udev, const char *name)
146 char devpath[PATH_SIZE];
147 char filename[PATH_SIZE];
150 strlcpy(filename, name, sizeof(filename));
152 dbg("look for existing node '%s'", filename);
153 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
154 dbg("free num=%d", num);
160 info("find_free_number gone crazy (num=%d), aborted", num);
163 snprintf(filename, sizeof(filename), "%s%d", name, num);
164 filename[sizeof(filename)-1] = '\0';
168 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
169 const char *name, char *value, size_t len)
171 struct sysfs_attribute *tmpattr;
173 dbg("look for device attribute '%s'", name);
175 dbg("look for class attribute '%s/%s'", class_dev->path, name);
176 tmpattr = sysfs_get_classdev_attr(class_dev, name);
181 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
182 tmpattr = sysfs_get_device_attr(sysfs_device, name);
189 strlcpy(value, tmpattr->value, len);
190 remove_trailing_char(value, '\n');
192 dbg("found attribute '%s'", tmpattr->path);
196 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
197 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
199 char temp[PATH_SIZE];
200 char temp2[PATH_SIZE];
201 char *head, *tail, *cpos, *attr, *rest;
204 unsigned int next_free_number;
205 struct sysfs_class_device *class_dev_parent;
222 static const struct subst_map {
225 enum subst_type type;
227 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
228 { .name = "id", .fmt = 'b', .type = SUBST_ID },
229 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
230 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL_NAME },
231 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
232 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
233 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
234 { .name = "sysfs", .fmt = 's', .type = SUBST_SYSFS },
235 { .name = "enum", .fmt = 'e', .type = SUBST_ENUM },
236 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
237 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
238 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
239 { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
242 enum subst_type type;
243 const struct subst_map *subst;
248 while (head[0] != '\0') {
249 if (head[0] == '$') {
250 /* substitute named variable */
253 if (head[1] == '$') {
254 strlcpy(temp, head+2, sizeof(temp));
255 strlcpy(head+1, temp, maxsize);
260 for (subst = map; subst->name; subst++) {
261 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
263 tail = head + strlen(subst->name)+1;
264 dbg("will substitute format name '%s'", subst->name);
269 else if (head[0] == '%') {
270 /* substitute format char */
273 if (head[1] == '%') {
274 strlcpy(temp, head+2, sizeof(temp));
275 strlcpy(head+1, temp, maxsize);
281 len = get_format_len(&tail);
282 for (subst = map; subst->name; subst++) {
283 if (tail[0] == subst->fmt) {
286 dbg("will substitute format char '%c'", subst->fmt);
295 attr = get_format_attribute(&tail);
296 strlcpy(temp, tail, sizeof(temp));
297 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
298 type ,string, tail, class_dev, sysfs_device);
302 strlcat(string, udev->devpath, maxsize);
303 dbg("substitute devpath '%s'", udev->devpath);
306 strlcat(string, udev->bus_id, maxsize);
307 dbg("substitute bus_id '%s'", udev->bus_id);
309 case SUBST_KERNEL_NAME:
310 strlcat(string, udev->kernel_name, maxsize);
311 dbg("substitute kernel name '%s'", udev->kernel_name);
313 case SUBST_KERNEL_NUMBER:
314 strlcat(string, udev->kernel_number, maxsize);
315 dbg("substitute kernel number '%s'", udev->kernel_number);
318 sprintf(temp2, "%d", major(udev->devt));
319 strlcat(string, temp2, maxsize);
320 dbg("substitute major number '%s'", temp2);
323 sprintf(temp2, "%d", minor(udev->devt));
324 strlcat(string, temp2, maxsize);
325 dbg("substitute minor number '%s'", temp2);
328 if (udev->program_result[0] == '\0')
330 /* get part part of the result string */
333 i = strtoul(attr, &rest, 10);
335 dbg("request part #%d of result string", i);
336 cpos = udev->program_result;
338 while (cpos[0] != '\0' && !isspace(cpos[0]))
340 while (isspace(cpos[0]))
344 err("requested part of result string not found");
347 strlcpy(temp2, cpos, sizeof(temp2));
348 /* %{2+}c copies the whole string from the second part on */
349 if (rest[0] != '+') {
350 cpos = strchr(temp2, ' ');
354 strlcat(string, temp2, maxsize);
355 dbg("substitute part of result string '%s'", temp2);
357 strlcat(string, udev->program_result, maxsize);
358 dbg("substitute result string '%s'", udev->program_result);
363 dbg("missing attribute");
366 if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
367 struct sysfs_device *parent_device;
369 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
370 parent_device = sysfs_get_device_parent(sysfs_device);
371 while (parent_device) {
372 dbg("looking at '%s'", parent_device->path);
373 if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
375 parent_device = sysfs_get_device_parent(parent_device);
380 /* strip trailing whitespace of sysfs value */
382 while (i > 0 && isspace(temp2[i-1]))
384 replace_untrusted_chars(temp2);
385 strlcat(string, temp2, maxsize);
386 dbg("substitute sysfs value '%s'", temp2);
389 next_free_number = find_free_number(udev, string);
390 if (next_free_number > 0) {
391 sprintf(temp2, "%d", next_free_number);
392 strlcat(string, temp2, maxsize);
398 class_dev_parent = sysfs_get_classdev_parent(class_dev);
399 if (class_dev_parent != NULL) {
400 struct udevice udev_parent;
402 dbg("found parent '%s', get the node name", class_dev_parent->path);
403 udev_init_device(&udev_parent, NULL, NULL, NULL);
404 /* lookup the name in the udev_db with the DEVPATH of the parent */
405 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
406 strlcat(string, udev_parent.name, maxsize);
407 dbg("substitute parent node name'%s'", udev_parent.name);
409 dbg("parent not found in database");
410 udev_cleanup_device(&udev_parent);
413 case SUBST_TEMP_NODE:
414 if (udev->tmp_node[0] == '\0') {
415 dbg("create temporary device node for callout");
416 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
417 udev_root, major(udev->devt), minor(udev->devt));
418 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
419 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
421 strlcat(string, udev->tmp_node, maxsize);
422 dbg("substitute temporary device node name '%s'", udev->tmp_node);
425 strlcat(string, udev_root, maxsize);
426 dbg("substitute udev_root '%s'", udev_root);
429 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
431 strlcat(string, temp2, maxsize);
432 dbg("substitute MODALIAS '%s'", temp2);
435 err("unknown substitution type=%i", type);
438 /* possibly truncate to format-char specified length */
441 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
444 strlcat(string, temp, maxsize);
448 static int execute_program_pipe(const char *command, const char *subsystem, char *value, int len)
457 char *argv[(sizeof(arg) / 2) + 1];
461 strlcpy(arg, command, sizeof(arg));
463 if (strchr(arg, ' ')) {
465 while (pos != NULL) {
466 if (pos[0] == '\'') {
467 /* don't separate if in apostrophes */
469 argv[i] = strsep(&pos, "\'");
470 while (pos && pos[0] == ' ')
473 argv[i] = strsep(&pos, " ");
475 dbg("arg[%i] '%s'", i, argv[i]);
479 dbg("execute '%s' with parsed arguments", arg);
482 argv[1] = (char *) subsystem;
484 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
487 retval = pipe(pipefds);
496 /* child dup2 write side of pipe to STDOUT */
497 devnull = open("/dev/null", O_RDWR);
499 dup2(devnull, STDIN_FILENO);
500 dup2(devnull, STDERR_FILENO);
503 dup2(pipefds[1], STDOUT_FILENO);
504 retval = execv(arg, argv);
505 err("exec of program failed");
508 err("fork of '%s' failed", arg);
512 /* parent reads from pipefds[0] */
517 count = read(pipefds[0], value + i, len - i-1);
519 err("read failed with '%s'", strerror(errno));
528 err("result len %d too short", len);
536 waitpid(pid, &status, 0);
538 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
539 dbg("exec program status 0x%x", status);
545 remove_trailing_char(value, '\n');
546 dbg("result is '%s'", value);
547 replace_untrusted_chars(value);
554 static int match_rule(struct udevice *udev, struct udev_rule *rule,
555 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
557 struct sysfs_device *parent_device = sysfs_device;
559 if (rule->action_operation != KEY_OP_UNSET) {
560 dbg("check for " KEY_ACTION " rule->action='%s' udev->action='%s'",
561 rule->action, udev->action);
562 if (strcmp_pattern(rule->action, udev->action) != 0) {
563 dbg(KEY_ACTION " is not matching");
564 if (rule->action_operation != KEY_OP_NOMATCH)
567 dbg(KEY_ACTION " matches");
568 if (rule->action_operation == KEY_OP_NOMATCH)
571 dbg(KEY_ACTION " key is true");
574 if (rule->kernel_operation != KEY_OP_UNSET) {
575 dbg("check for " KEY_KERNEL " rule->kernel='%s' udev_kernel_name='%s'",
576 rule->kernel, udev->kernel_name);
577 if (strcmp_pattern(rule->kernel, udev->kernel_name) != 0) {
578 dbg(KEY_KERNEL " is not matching");
579 if (rule->kernel_operation != KEY_OP_NOMATCH)
582 dbg(KEY_KERNEL " matches");
583 if (rule->kernel_operation == KEY_OP_NOMATCH)
586 dbg(KEY_KERNEL " key is true");
589 if (rule->subsystem_operation != KEY_OP_UNSET) {
590 dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' udev->subsystem='%s'",
591 rule->subsystem, udev->subsystem);
592 if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
593 dbg(KEY_SUBSYSTEM " is not matching");
594 if (rule->subsystem_operation != KEY_OP_NOMATCH)
597 dbg(KEY_SUBSYSTEM " matches");
598 if (rule->subsystem_operation == KEY_OP_NOMATCH)
601 dbg(KEY_SUBSYSTEM " key is true");
604 if (rule->devpath_operation != KEY_OP_UNSET) {
605 dbg("check for " KEY_DEVPATH " rule->devpath='%s' udev->devpath='%s'",
606 rule->devpath, udev->devpath);
607 if (strcmp_pattern(rule->devpath, udev->devpath) != 0) {
608 dbg(KEY_DEVPATH " is not matching");
609 if (rule->devpath_operation != KEY_OP_NOMATCH)
612 dbg(KEY_DEVPATH " matches");
613 if (rule->devpath_operation == KEY_OP_NOMATCH)
616 dbg(KEY_DEVPATH " key is true");
619 if (rule->modalias_operation != KEY_OP_UNSET) {
620 char value[NAME_SIZE];
622 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
623 dbg(KEY_MODALIAS " value not found");
626 dbg("check for " KEY_MODALIAS " rule->modalias='%s' modalias='%s'",
627 rule->modalias, value);
628 if (strcmp_pattern(rule->modalias, value) != 0) {
629 dbg(KEY_MODALIAS " is not matching");
630 if (rule->modalias_operation != KEY_OP_NOMATCH)
633 dbg(KEY_MODALIAS " matches");
634 if (rule->modalias_operation == KEY_OP_NOMATCH)
637 dbg(KEY_MODALIAS " key is true");
640 if (rule->env_pair_count) {
643 dbg("check for " KEY_ENV " pairs");
644 for (i = 0; i < rule->env_pair_count; i++) {
645 struct key_pair *pair;
648 pair = &rule->env_pair[i];
649 value = getenv(pair->name);
651 dbg(KEY_ENV "{'%s'} is not found", pair->name);
654 if (strcmp_pattern(pair->value, value) != 0) {
655 dbg(KEY_ENV "{'%s'} is not matching", pair->name);
656 if (pair->operation != KEY_OP_NOMATCH)
659 dbg(KEY_ENV "{'%s'} matches", pair->name);
660 if (pair->operation == KEY_OP_NOMATCH)
664 dbg(KEY_ENV " key is true");
667 /* walk up the chain of physical devices and find a match */
669 /* check for matching driver */
670 if (rule->driver_operation != KEY_OP_UNSET) {
671 if (parent_device == NULL) {
672 dbg("device has no sysfs_device");
675 dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
676 rule->driver, parent_device->driver_name);
677 if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) {
678 dbg(KEY_DRIVER " is not matching");
679 if (rule->driver_operation != KEY_OP_NOMATCH)
682 dbg(KEY_DRIVER " matches");
683 if (rule->driver_operation == KEY_OP_NOMATCH)
686 dbg(KEY_DRIVER " key is true");
689 /* check for matching bus value */
690 if (rule->bus_operation != KEY_OP_UNSET) {
691 if (parent_device == NULL) {
692 dbg("device has no sysfs_device");
695 dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
696 rule->bus, parent_device->bus);
697 if (strcmp_pattern(rule->bus, parent_device->bus) != 0) {
698 dbg(KEY_BUS " is not matching");
699 if (rule->bus_operation != KEY_OP_NOMATCH)
702 dbg(KEY_BUS " matches");
703 if (rule->bus_operation == KEY_OP_NOMATCH)
706 dbg(KEY_BUS " key is true");
709 /* check for matching bus id */
710 if (rule->id_operation != KEY_OP_UNSET) {
711 if (parent_device == NULL) {
712 dbg("device has no sysfs_device");
715 dbg("check " KEY_ID);
716 if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) {
717 dbg(KEY_ID " is not matching");
718 if (rule->id_operation != KEY_OP_NOMATCH)
721 dbg(KEY_ID " matches");
722 if (rule->id_operation == KEY_OP_NOMATCH)
725 dbg(KEY_ID " key is true");
728 /* check for matching sysfs pairs */
729 if (rule->sysfs_pair_count) {
732 dbg("check " KEY_SYSFS " pairs");
733 for (i = 0; i < rule->sysfs_pair_count; i++) {
734 struct key_pair *pair;
735 char value[VALUE_SIZE];
738 pair = &rule->sysfs_pair[i];
739 if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
742 /* strip trailing whitespace of value, if not asked to match for it */
743 len = strlen(pair->value);
744 if (len && !isspace(pair->value[len-1])) {
746 while (len > 0 && isspace(value[len-1]))
748 dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value);
751 dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
752 if (strcmp_pattern(pair->value, value) != 0) {
753 dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
754 if (pair->operation != KEY_OP_NOMATCH)
757 dbg(KEY_SYSFS "{'%s'} matches", pair->name);
758 if (pair->operation == KEY_OP_NOMATCH)
762 dbg(KEY_SYSFS " keys are true");
765 /* found matching physical device */
768 dbg("try parent sysfs device");
769 parent_device = sysfs_get_device_parent(parent_device);
770 if (parent_device == NULL)
772 dbg("look at sysfs_device->path='%s'", parent_device->path);
773 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
776 /* execute external program */
777 if (rule->program_operation != KEY_OP_UNSET) {
778 char program[PATH_SIZE];
780 dbg("check " KEY_PROGRAM);
781 strlcpy(program, rule->program, sizeof(program));
782 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
783 if (execute_program_pipe(program, udev->subsystem,
784 udev->program_result, sizeof(udev->program_result)) != 0) {
785 dbg(KEY_PROGRAM " returned nonzero");
786 if (rule->program_operation != KEY_OP_NOMATCH)
789 dbg(KEY_PROGRAM " returned successful");
790 if (rule->program_operation == KEY_OP_NOMATCH)
793 dbg(KEY_PROGRAM " key is true");
796 /* check for matching result of external program */
797 if (rule->result_operation != KEY_OP_UNSET) {
798 dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
799 rule->result, udev->program_result);
800 if (strcmp_pattern(rule->result, udev->program_result) != 0) {
801 dbg(KEY_RESULT " is not matching");
802 if (rule->result_operation != KEY_OP_NOMATCH)
805 dbg(KEY_RESULT " matches");
806 if (rule->result_operation == KEY_OP_NOMATCH)
809 dbg(KEY_RESULT " key is true");
819 int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
821 struct sysfs_class_device *class_dev_parent;
822 struct sysfs_device *sysfs_device = NULL;
823 struct udev_rule *rule;
825 dbg("class_dev->name='%s'", class_dev->name);
827 /* Figure out where the "device"-symlink is at. For char devices this will
828 * always be in the class_dev->path. On block devices, only the main block
829 * device will have the device symlink in it's path. All partition devices
830 * need to look at the symlink in its parent directory.
832 class_dev_parent = sysfs_get_classdev_parent(class_dev);
833 if (class_dev_parent != NULL) {
834 dbg("given class device has a parent, use this instead");
835 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
837 sysfs_device = sysfs_get_classdev_device(class_dev);
841 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
842 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
843 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
846 dbg("udev->kernel_name='%s'", udev->kernel_name);
848 /* look for a matching rule to apply */
849 list_for_each_entry(rule, &udev_rule_list, node) {
850 if (udev->name_set && rule->name_operation != KEY_OP_UNSET) {
851 dbg("node name already set, rule ignored");
856 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
858 if (rule->ignore_device) {
859 info("configured rule in '%s[%i]' applied, '%s' is ignored",
860 rule->config_file, rule->config_line, udev->kernel_name);
861 udev->ignore_device = 1;
864 if (rule->ignore_remove) {
865 udev->ignore_remove = 1;
866 dbg("remove event should be ignored");
868 /* apply all_partitions option only at a main block device */
869 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
870 udev->partitions = rule->partitions;
871 dbg("creation of partition nodes requested");
874 /* apply permissions */
875 if (!udev->mode_final && rule->mode != 0000) {
876 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
877 udev->mode_final = 1;
878 udev->mode = rule->mode;
879 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
881 if (!udev->owner_final && rule->owner[0] != '\0') {
882 if (rule->owner_operation == KEY_OP_ASSIGN_FINAL)
883 udev->owner_final = 1;
884 strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
885 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
886 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
888 if (!udev->group_final && rule->group[0] != '\0') {
889 if (rule->group_operation == KEY_OP_ASSIGN_FINAL)
890 udev->group_final = 1;
891 strlcpy(udev->group, rule->group, sizeof(udev->group));
892 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
893 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
896 /* collect symlinks */
897 if (!udev->symlink_final && rule->symlink_operation != KEY_OP_UNSET) {
898 char temp[PATH_SIZE];
901 if (rule->symlink_operation == KEY_OP_ASSIGN_FINAL)
902 udev->symlink_final = 1;
903 if (rule->symlink_operation == KEY_OP_ASSIGN || rule->symlink_operation == KEY_OP_ASSIGN_FINAL) {
904 struct name_entry *name_loop;
905 struct name_entry *temp_loop;
907 info("reset symlink list");
908 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
909 list_del(&name_loop->node);
913 if (rule->symlink[0] != '\0') {
914 info("configured rule in '%s[%i]' applied, added symlink '%s'",
915 rule->config_file, rule->config_line, rule->symlink);
916 strlcpy(temp, rule->symlink, sizeof(temp));
917 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
919 /* add multiple symlinks separated by spaces */
921 next = strchr(temp, ' ');
924 info("add symlink '%s'", pos);
925 name_list_add(&udev->symlink_list, pos, 0);
927 next = strchr(pos, ' ');
929 info("add symlink '%s'", pos);
930 name_list_add(&udev->symlink_list, pos, 0);
934 /* set name, later rules with name set will be ignored */
935 if (rule->name_operation != KEY_OP_UNSET) {
937 if (rule->name[0] == '\0') {
938 info("configured rule in '%s[%i]' applied, node handling for '%s' supressed",
939 rule->config_file, rule->config_line, udev->kernel_name);
941 strlcpy(udev->name, rule->name, sizeof(udev->name));
942 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
943 strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
944 udev->config_line = rule->config_line;
946 info("configured rule in '%s:%i' applied, '%s' becomes '%s'",
947 rule->config_file, rule->config_line, udev->kernel_name, rule->name);
948 if (udev->type != DEV_NET)
949 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
950 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
954 if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
955 char program[PATH_SIZE];
957 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
959 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
960 struct name_entry *name_loop;
961 struct name_entry *temp_loop;
963 info("reset run list");
964 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
965 list_del(&name_loop->node);
969 if (rule->run[0] != '\0') {
970 strlcpy(program, rule->run, sizeof(program));
971 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
972 dbg("add run '%s'", program);
973 name_list_add(&udev->run_list, program, 0);
977 if (rule->last_rule) {
978 dbg("last rule to be applied");
984 if (udev->name[0] == '\0') {
985 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
986 info("no rule found, use kernel name '%s'", udev->name);
989 if (udev->tmp_node[0] != '\0') {
990 dbg("removing temporary device node");
991 unlink_secure(udev->tmp_node);
992 udev->tmp_node[0] = '\0';
998 int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device)
1000 struct udev_rule *rule;
1002 /* look for a matching rule to apply */
1003 list_for_each_entry(rule, &udev_rule_list, node) {
1004 dbg("process rule");
1006 if (rule->run_operation == KEY_OP_UNSET)
1009 if (rule->name_operation != KEY_OP_UNSET || rule->symlink_operation != KEY_OP_UNSET ||
1010 rule->mode != 0000 || rule->owner[0] != '\0' || rule->group[0] != '\0') {
1011 dbg("skip rule that names a device");
1015 if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
1016 if (rule->ignore_device) {
1017 info("configured rule in '%s[%i]' applied, '%s' is ignored",
1018 rule->config_file, rule->config_line, udev->kernel_name);
1019 udev->ignore_device = 1;
1023 if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
1024 char program[PATH_SIZE];
1026 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
1027 struct name_entry *name_loop;
1028 struct name_entry *temp_loop;
1030 info("reset run list");
1031 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1032 list_del(&name_loop->node);
1036 if (rule->run[0] != '\0') {
1037 strlcpy(program, rule->run, sizeof(program));
1038 apply_format(udev, program, sizeof(program), NULL, sysfs_device);
1039 dbg("add run '%s'", program);
1040 name_list_add(&udev->run_list, program, 0);
1042 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
1046 if (rule->last_rule) {
1047 dbg("last rule to be applied");