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 static int import_parent_into_env(struct udevice *udev, struct sysfs_class_device *class_dev, const char *filter)
286 struct sysfs_class_device *parent = sysfs_get_classdev_parent(class_dev);
289 if (parent != NULL) {
290 struct udevice udev_parent;
291 struct name_entry *name_loop;
293 dbg("found parent '%s', get the node name", parent->path);
294 udev_init_device(&udev_parent, NULL, NULL, NULL);
295 /* import the udev_db of the parent */
296 if (udev_db_get_device(&udev_parent, &parent->path[strlen(sysfs_path)]) == 0) {
297 dbg("import stored parent env '%s'", udev_parent.name);
298 list_for_each_entry(name_loop, &udev_parent.env_list, node) {
299 char name[NAME_SIZE];
302 strlcpy(name, name_loop->name, sizeof(name));
303 pos = strchr(name, '=');
307 if (strcmp_pattern(filter, name) == 0) {
308 dbg("import key '%s'", name_loop->name);
309 name_list_add(&udev->env_list, name_loop->name, 0);
310 setenv(name, pos, 1);
312 dbg("skip key '%s'", name_loop->name);
317 dbg("parent not found in database");
318 udev_cleanup_device(&udev_parent);
324 /* finds the lowest positive N such that <name>N isn't present in the udevdb
325 * if <name> doesn't exist, 0 is returned, N otherwise
327 static int find_free_number(struct udevice *udev, const char *name)
329 char devpath[PATH_SIZE];
330 char filename[PATH_SIZE];
333 strlcpy(filename, name, sizeof(filename));
335 dbg("look for existing node '%s'", filename);
336 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
337 dbg("free num=%d", num);
343 info("find_free_number gone crazy (num=%d), aborted", num);
346 snprintf(filename, sizeof(filename), "%s%d", name, num);
347 filename[sizeof(filename)-1] = '\0';
351 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
352 const char *name, char *value, size_t len)
354 struct sysfs_attribute *tmpattr;
356 dbg("look for device attribute '%s'", name);
358 dbg("look for class attribute '%s/%s'", class_dev->path, name);
359 tmpattr = sysfs_get_classdev_attr(class_dev, name);
364 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
365 tmpattr = sysfs_get_device_attr(sysfs_device, name);
372 strlcpy(value, tmpattr->value, len);
373 remove_trailing_char(value, '\n');
375 dbg("found attribute '%s'", tmpattr->path);
379 #define WAIT_LOOP_PER_SECOND 20
380 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
382 char filename[PATH_SIZE];
384 int loop = timeout * WAIT_LOOP_PER_SECOND;
386 snprintf(filename, sizeof(filename), "%s%s/%s", sysfs_path, udev->devpath, file);
387 filename[sizeof(filename)-1] = '\0';
388 dbg("wait %i sec for '%s'", timeout, filename);
391 if (stat(filename, &stats) == 0) {
392 dbg("file appeared after %i loops", (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
395 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
397 dbg("waiting for '%s' failed", filename);
401 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
402 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
404 char temp[PATH_SIZE];
405 char temp2[PATH_SIZE];
406 char *head, *tail, *pos, *cpos, *attr, *rest;
409 unsigned int next_free_number;
410 struct sysfs_class_device *class_dev_parent;
428 static const struct subst_map {
431 enum subst_type type;
433 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
434 { .name = "id", .fmt = 'b', .type = SUBST_ID },
435 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
436 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL_NAME },
437 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
438 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
439 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
440 { .name = "sysfs", .fmt = 's', .type = SUBST_SYSFS },
441 { .name = "enum", .fmt = 'e', .type = SUBST_ENUM },
442 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
443 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
444 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
445 { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
446 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
449 enum subst_type type;
450 const struct subst_map *subst;
455 while (head[0] != '\0') {
456 if (head[0] == '$') {
457 /* substitute named variable */
460 if (head[1] == '$') {
461 strlcpy(temp, head+2, sizeof(temp));
462 strlcpy(head+1, temp, maxsize);
467 for (subst = map; subst->name; subst++) {
468 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
470 tail = head + strlen(subst->name)+1;
471 dbg("will substitute format name '%s'", subst->name);
476 else if (head[0] == '%') {
477 /* substitute format char */
480 if (head[1] == '%') {
481 strlcpy(temp, head+2, sizeof(temp));
482 strlcpy(head+1, temp, maxsize);
488 len = get_format_len(&tail);
489 for (subst = map; subst->name; subst++) {
490 if (tail[0] == subst->fmt) {
493 dbg("will substitute format char '%c'", subst->fmt);
502 attr = get_format_attribute(&tail);
503 strlcpy(temp, tail, sizeof(temp));
504 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
505 type ,string, tail, class_dev, sysfs_device);
509 strlcat(string, udev->devpath, maxsize);
510 dbg("substitute devpath '%s'", udev->devpath);
513 strlcat(string, udev->bus_id, maxsize);
514 dbg("substitute bus_id '%s'", udev->bus_id);
516 case SUBST_KERNEL_NAME:
517 strlcat(string, udev->kernel_name, maxsize);
518 dbg("substitute kernel name '%s'", udev->kernel_name);
520 case SUBST_KERNEL_NUMBER:
521 strlcat(string, udev->kernel_number, maxsize);
522 dbg("substitute kernel number '%s'", udev->kernel_number);
525 sprintf(temp2, "%d", major(udev->devt));
526 strlcat(string, temp2, maxsize);
527 dbg("substitute major number '%s'", temp2);
530 sprintf(temp2, "%d", minor(udev->devt));
531 strlcat(string, temp2, maxsize);
532 dbg("substitute minor number '%s'", temp2);
535 if (udev->program_result[0] == '\0')
537 /* get part part of the result string */
540 i = strtoul(attr, &rest, 10);
542 dbg("request part #%d of result string", i);
543 cpos = udev->program_result;
545 while (cpos[0] != '\0' && !isspace(cpos[0]))
547 while (isspace(cpos[0]))
551 err("requested part of result string not found");
554 strlcpy(temp2, cpos, sizeof(temp2));
555 /* %{2+}c copies the whole string from the second part on */
556 if (rest[0] != '+') {
557 cpos = strchr(temp2, ' ');
561 strlcat(string, temp2, maxsize);
562 dbg("substitute part of result string '%s'", temp2);
564 strlcat(string, udev->program_result, maxsize);
565 dbg("substitute result string '%s'", udev->program_result);
570 dbg("missing attribute");
573 if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
574 struct sysfs_device *parent_device;
576 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
577 parent_device = sysfs_get_device_parent(sysfs_device);
578 while (parent_device) {
579 dbg("looking at '%s'", parent_device->path);
580 if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
582 parent_device = sysfs_get_device_parent(parent_device);
587 /* strip trailing whitespace of sysfs value */
589 while (i > 0 && isspace(temp2[i-1]))
591 replace_untrusted_chars(temp2);
592 strlcat(string, temp2, maxsize);
593 dbg("substitute sysfs value '%s'", temp2);
596 next_free_number = find_free_number(udev, string);
597 if (next_free_number > 0) {
598 sprintf(temp2, "%d", next_free_number);
599 strlcat(string, temp2, maxsize);
605 class_dev_parent = sysfs_get_classdev_parent(class_dev);
606 if (class_dev_parent != NULL) {
607 struct udevice udev_parent;
609 dbg("found parent '%s', get the node name", class_dev_parent->path);
610 udev_init_device(&udev_parent, NULL, NULL, NULL);
611 /* lookup the name in the udev_db with the DEVPATH of the parent */
612 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
613 strlcat(string, udev_parent.name, maxsize);
614 dbg("substitute parent node name'%s'", udev_parent.name);
616 dbg("parent not found in database");
617 udev_cleanup_device(&udev_parent);
620 case SUBST_TEMP_NODE:
621 if (udev->tmp_node[0] == '\0') {
622 dbg("create temporary device node for callout");
623 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
624 udev_root, major(udev->devt), minor(udev->devt));
625 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
626 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
628 strlcat(string, udev->tmp_node, maxsize);
629 dbg("substitute temporary device node name '%s'", udev->tmp_node);
632 strlcat(string, udev_root, maxsize);
633 dbg("substitute udev_root '%s'", udev_root);
636 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
638 strlcat(string, temp2, maxsize);
639 dbg("substitute MODALIAS '%s'", temp2);
643 dbg("missing attribute");
648 dbg("env '%s' not avialable", attr);
651 dbg("substitute env '%s=%s'", attr, pos);
652 strlcat(string, pos, maxsize);
655 err("unknown substitution type=%i", type);
658 /* possibly truncate to format-char specified length */
661 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
663 strlcat(string, temp, maxsize);
667 static char *key_val(struct udev_rule *rule, struct key *key)
669 return rule->buf + key->val_off;
672 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
674 return rule->buf + pair->key_name_off;
677 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
680 char value[PATH_SIZE];
684 if (key->operation == KEY_OP_UNSET)
687 strlcpy(value, rule->buf + key->val_off, sizeof(value));
690 dbg("key %s value='%s'", key_name, key_value);
692 pos = strchr(key_value, '|');
697 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
698 match = (strcmp_pattern(key_value, val) == 0);
699 if (match && (key->operation != KEY_OP_NOMATCH)) {
700 dbg("%s is true (matching value)", key_name);
703 if (!match && (key->operation == KEY_OP_NOMATCH)) {
704 dbg("%s is true (non-matching value)", key_name);
709 dbg("%s is false", key_name);
713 static int match_rule(struct udevice *udev, struct udev_rule *rule,
714 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
716 struct sysfs_device *parent_device = sysfs_device;
718 if (match_key("ACTION", rule, &rule->action, udev->action))
721 if (match_key("KERNEL", rule, &rule->kernel_name, udev->kernel_name))
724 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->subsystem))
727 if (match_key("DEVPATH", rule, &rule->devpath, udev->devpath))
730 if (rule->modalias.operation != KEY_OP_UNSET) {
731 char value[NAME_SIZE];
733 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
734 dbg("MODALIAS value not found");
737 if (match_key("MODALIAS", rule, &rule->modalias, value))
741 if (rule->env.count) {
744 dbg("check %i ENV keys", rule->env.count);
745 for (i = 0; i < rule->env.count; i++) {
746 struct key_pair *pair = &rule->env.keys[i];
747 const char *key_name = key_pair_name(rule, pair);
748 const char *value = getenv(key_name);
751 dbg("ENV{'%s'} is not set", key_name);
754 if (match_key("ENV", rule, &pair->key, value))
757 dbg("all %i ENV keys matched", rule->env.count);
760 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
763 match = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
764 if (match && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH)) {
765 dbg("WAIT_FOR_SYSFS is true (matching value)");
768 if (!match && (rule->wait_for_sysfs.operation == KEY_OP_NOMATCH)) {
769 dbg("WAIT_FOR_SYSFS is true, (non matching value)");
772 dbg("WAIT_FOR_SYSFS is false");
776 /* walk up the chain of physical devices and find a match */
778 /* check for matching driver */
779 if (rule->driver.operation != KEY_OP_UNSET) {
780 if (parent_device == NULL) {
781 dbg("device has no sysfs_device");
784 if (match_key("DRIVER", rule, &rule->driver, parent_device->driver_name))
788 /* check for matching bus value */
789 if (rule->bus.operation != KEY_OP_UNSET) {
790 if (parent_device == NULL) {
791 dbg("device has no sysfs_device");
794 if (match_key("BUS", rule, &rule->bus, parent_device->bus))
798 /* check for matching bus id */
799 if (rule->id.operation != KEY_OP_UNSET) {
800 if (parent_device == NULL) {
801 dbg("device has no sysfs_device");
804 if (match_key("ID", rule, &rule->id, parent_device->bus_id))
808 /* check for matching sysfs pairs */
809 if (rule->sysfs.count) {
812 dbg("check %i SYSFS keys", rule->sysfs.count);
813 for (i = 0; i < rule->sysfs.count; i++) {
814 struct key_pair *pair = &rule->sysfs.keys[i];
815 const char *key_name = key_pair_name(rule, pair);
816 const char *key_value = key_val(rule, &pair->key);
817 char value[VALUE_SIZE];
820 if (find_sysfs_attribute(class_dev, parent_device, key_name, value, sizeof(value)) != 0)
823 /* strip trailing whitespace of value, if not asked to match for it */
824 len = strlen(key_value);
825 if (len && !isspace(key_value[len-1])) {
827 while (len > 0 && isspace(value[len-1]))
829 dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
832 if (match_key("SYSFS", rule, &pair->key, value))
835 dbg("all %i SYSFS keys matched", rule->sysfs.count);
838 /* found matching physical device */
841 dbg("try parent sysfs device");
842 parent_device = sysfs_get_device_parent(parent_device);
843 if (parent_device == NULL)
845 dbg("look at sysfs_device->path='%s'", parent_device->path);
846 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
849 /* import variables from file into environment */
850 if (rule->import.operation != KEY_OP_UNSET) {
851 char import[PATH_SIZE];
854 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
855 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
856 dbg("check for IMPORT import='%s'", import);
857 if (rule->import_type == IMPORT_PROGRAM) {
858 dbg("run executable file import='%s'", import);
859 rc = import_program_into_env(udev, import);
860 } else if (rule->import_type == IMPORT_FILE) {
861 dbg("import file import='%s'", import);
862 rc = import_file_into_env(udev, import);
863 } else if (rule->import_type == IMPORT_PARENT && class_dev) {
864 dbg("import parent import='%s'", import);
865 rc = import_parent_into_env(udev, class_dev, import);
868 dbg("IMPORT failed");
869 if (rule->import.operation != KEY_OP_NOMATCH)
872 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
873 dbg("IMPORT key is true");
876 /* execute external program */
877 if (rule->program.operation != KEY_OP_UNSET) {
878 char program[PATH_SIZE];
879 char result[PATH_SIZE];
881 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
882 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
883 dbg("check for PROGRAM program='%s", program);
884 if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) {
885 dbg("PROGRAM is false");
886 if (rule->program.operation != KEY_OP_NOMATCH)
889 dbg("PROGRAM matches");
890 remove_trailing_char(result, '\n');
891 replace_untrusted_chars(result);
892 dbg("result is '%s'", result);
893 strlcpy(udev->program_result, result, sizeof(udev->program_result));
894 dbg("PROGRAM returned successful");
895 if (rule->program.operation == KEY_OP_NOMATCH)
898 dbg("PROGRAM key is true");
901 /* check for matching result of external program */
902 if (match_key("RESULT", rule, &rule->result, udev->program_result))
912 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct sysfs_class_device *class_dev)
914 struct sysfs_class_device *class_dev_parent;
915 struct sysfs_device *sysfs_device = NULL;
916 struct udev_rule *rule;
919 dbg("class_dev->name='%s'", class_dev->name);
921 /* Figure out where the "device"-symlink is at. For char devices this will
922 * always be in the class_dev->path. On block devices, only the main block
923 * device will have the device symlink in it's path. All partition devices
924 * need to look at the symlink in its parent directory.
926 class_dev_parent = sysfs_get_classdev_parent(class_dev);
927 if (class_dev_parent != NULL) {
928 dbg("given class device has a parent, use this instead");
929 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
931 sysfs_device = sysfs_get_classdev_device(class_dev);
935 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
936 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
937 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
940 dbg("udev->kernel_name='%s'", udev->kernel_name);
942 /* look for a matching rule to apply */
943 udev_rules_iter_init(rules);
945 rule = udev_rules_iter_next(rules);
949 if (name_set && rule->name.operation != KEY_OP_UNSET) {
950 dbg("node name already set, rule ignored");
955 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
957 if (rule->ignore_device) {
958 info("rule applied, '%s' is ignored", udev->kernel_name);
959 udev->ignore_device = 1;
962 if (rule->ignore_remove) {
963 udev->ignore_remove = 1;
964 dbg("remove event should be ignored");
966 /* apply all_partitions option only at a main block device */
967 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
968 udev->partitions = rule->partitions;
969 dbg("creation of partition nodes requested");
972 /* apply permissions */
973 if (!udev->mode_final && rule->mode != 0000) {
974 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
975 udev->mode_final = 1;
976 udev->mode = rule->mode;
977 dbg("applied mode=%#o to '%s'", rule->mode, udev->kernel_name);
979 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
980 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
981 udev->owner_final = 1;
982 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
983 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
984 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
986 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
987 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
988 udev->group_final = 1;
989 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
990 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
991 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
994 /* collect symlinks */
995 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
996 char temp[PATH_SIZE];
999 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
1000 udev->symlink_final = 1;
1001 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
1002 struct name_entry *name_loop;
1003 struct name_entry *temp_loop;
1005 info("reset symlink list");
1006 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
1007 list_del(&name_loop->node);
1011 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
1012 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
1013 info("rule applied, added symlink '%s'", temp);
1015 /* add multiple symlinks separated by spaces */
1017 while (isspace(pos[0]))
1019 next = strchr(pos, ' ');
1022 info("add symlink '%s'", pos);
1023 name_list_add(&udev->symlink_list, pos, 0);
1024 while (isspace(next[1]))
1027 next = strchr(pos, ' ');
1029 if (pos[0] != '\0') {
1030 info("add symlink '%s'", pos);
1031 name_list_add(&udev->symlink_list, pos, 0);
1035 /* set name, later rules with name set will be ignored */
1036 if (rule->name.operation != KEY_OP_UNSET) {
1038 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
1039 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
1041 info("rule applied, '%s' becomes '%s'", udev->kernel_name, udev->name);
1042 if (udev->type != DEV_NET)
1043 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1044 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1047 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1048 char program[PATH_SIZE];
1050 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1051 udev->run_final = 1;
1052 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1053 struct name_entry *name_loop;
1054 struct name_entry *temp_loop;
1056 info("reset run list");
1057 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1058 list_del(&name_loop->node);
1062 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1063 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
1064 dbg("add run '%s'", program);
1065 name_list_add(&udev->run_list, program, 0);
1068 if (rule->last_rule) {
1069 dbg("last rule to be applied");
1073 if (rule->goto_label.operation != KEY_OP_UNSET) {
1074 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1075 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1081 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
1082 info("no rule found, will use kernel name '%s'", udev->name);
1085 if (udev->tmp_node[0] != '\0') {
1086 dbg("removing temporary device node");
1087 unlink_secure(udev->tmp_node);
1088 udev->tmp_node[0] = '\0';
1094 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sysfs_device *sysfs_device)
1096 struct udev_rule *rule;
1098 /* look for a matching rule to apply */
1099 udev_rules_iter_init(rules);
1101 rule = udev_rules_iter_next(rules);
1105 dbg("process rule");
1106 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1107 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1108 dbg("skip rule that names a device");
1112 if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
1113 if (rule->ignore_device) {
1114 info("rule applied, '%s' is ignored", udev->kernel_name);
1115 udev->ignore_device = 1;
1119 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1120 char program[PATH_SIZE];
1122 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1123 struct name_entry *name_loop;
1124 struct name_entry *temp_loop;
1126 info("reset run list");
1127 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1128 list_del(&name_loop->node);
1132 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1133 apply_format(udev, program, sizeof(program), NULL, sysfs_device);
1134 dbg("add run '%s'", program);
1135 name_list_add(&udev->run_list, program, 0);
1136 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1140 if (rule->last_rule) {
1141 dbg("last rule to be applied");