4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "libsysfs/sysfs/libsysfs.h"
36 #include "udev_libc_wrapper.h"
38 #include "udev_utils.h"
39 #include "udev_version.h"
41 #include "udev_rules.h"
45 /* extract possible {attr} and move str behind it */
46 static char *get_format_attribute(char **str)
52 pos = strchr(*str, '}');
54 err("missing closing brace for format");
60 dbg("attribute='%s', str='%s'", attr, *str);
65 /* extract possible format length and move str behind it*/
66 static int get_format_len(char **str)
71 if (isdigit(*str[0])) {
72 num = (int) strtoul(*str, &tail, 10);
75 dbg("format length=%i", num);
78 err("format parsing error '%s'", *str);
84 static int get_key(char **line, char **key, char **value)
93 if (strchr(linepos, '\\')) {
94 dbg("escaped characters are not supported, skip");
99 while (isspace(linepos[0]))
106 if (linepos[0] == '\0')
108 if (isspace(linepos[0]))
110 if (linepos[0] == '=')
118 /* skip whitespace */
119 while (isspace(linepos[0]))
123 if (linepos[0] == '"') {
125 temp = strchr(linepos, '"');
127 dbg("missing closing quote");
130 dbg("value is quoted");
132 } else if (linepos[0] == '\'') {
134 temp = strchr(linepos, '\'');
136 dbg("missing closing quote");
139 dbg("value is quoted");
141 } else if (linepos[0] == '\0') {
142 dbg("value is empty");
145 while (temp[0] && !isspace(temp[0]))
154 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
156 char line[LINE_SIZE];
165 /* loop through the whole buffer */
168 while (cur < bufsize) {
169 count = buf_get_line(buf, bufsize, cur);
174 if (count >= sizeof(line)) {
175 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
179 /* eat the whitespace */
180 while ((count > 0) && isspace(bufline[0])) {
187 /* see if this is a comment */
188 if (bufline[0] == COMMENT_CHARACTER)
191 memcpy(line, bufline, count);
195 if (get_key(&linepos, &variable, &value) == 0) {
196 dbg("import '%s=%s'", variable, value);
197 name_list_key_add(&udev->env_list, variable, value);
198 setenv(variable, value, 1);
205 static int import_file_into_env(struct udevice *udev, const char *filename)
210 if (file_map(filename, &buf, &bufsize) != 0) {
211 err("can't open '%s'", filename);
214 import_keys_into_env(udev, buf, bufsize);
215 file_unmap(buf, bufsize);
220 static int import_program_into_env(struct udevice *udev, const char *program)
225 if (run_program(program, udev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_DEBUG)) != 0)
227 return import_keys_into_env(udev, result, reslen);
230 static int import_parent_into_env(struct udevice *udev, struct sysfs_class_device *class_dev, const char *filter)
232 struct sysfs_class_device *parent = sysfs_get_classdev_parent(class_dev);
235 if (parent != NULL) {
236 struct udevice udev_parent;
237 struct name_entry *name_loop;
239 dbg("found parent '%s', get the node name", parent->path);
240 udev_init_device(&udev_parent, NULL, NULL, NULL);
241 /* import the udev_db of the parent */
242 if (udev_db_get_device(&udev_parent, &parent->path[strlen(sysfs_path)]) == 0) {
243 dbg("import stored parent env '%s'", udev_parent.name);
244 list_for_each_entry(name_loop, &udev_parent.env_list, node) {
245 char name[NAME_SIZE];
248 strlcpy(name, name_loop->name, sizeof(name));
249 pos = strchr(name, '=');
253 if (strcmp_pattern(filter, name) == 0) {
254 dbg("import key '%s'", name_loop->name);
255 name_list_add(&udev->env_list, name_loop->name, 0);
256 setenv(name, pos, 1);
258 dbg("skip key '%s'", name_loop->name);
263 dbg("parent not found in database");
264 udev_cleanup_device(&udev_parent);
270 /* finds the lowest positive N such that <name>N isn't present in the udevdb
271 * if <name> doesn't exist, 0 is returned, N otherwise
273 static int find_free_number(const char *name)
275 char devpath[PATH_SIZE];
276 char filename[PATH_SIZE];
279 strlcpy(filename, name, sizeof(filename));
281 dbg("look for existing node '%s'", filename);
282 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
283 dbg("free num=%d", num);
289 info("find_free_number gone crazy (num=%d), aborted", num);
292 snprintf(filename, sizeof(filename), "%s%d", name, num);
293 filename[sizeof(filename)-1] = '\0';
297 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
298 const char *name, char *value, size_t len)
300 struct sysfs_class_device *class_dev_parent;
301 struct sysfs_attribute *tmpattr;
303 dbg("look for device attribute '%s'", name);
305 dbg("look for class attribute '%s/%s'", class_dev->path, name);
306 tmpattr = sysfs_get_classdev_attr(class_dev, name);
309 class_dev_parent = sysfs_get_classdev_parent(class_dev);
310 if (class_dev_parent) {
311 tmpattr = sysfs_get_classdev_attr(class_dev_parent, name);
317 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
318 tmpattr = sysfs_get_device_attr(sysfs_device, name);
325 strlcpy(value, tmpattr->value, len);
326 remove_trailing_char(value, '\n');
328 dbg("found attribute '%s'", tmpattr->path);
332 #define WAIT_LOOP_PER_SECOND 20
333 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
335 char filename[PATH_SIZE];
337 int loop = timeout * WAIT_LOOP_PER_SECOND;
339 snprintf(filename, sizeof(filename), "%s%s/%s", sysfs_path, udev->devpath, file);
340 filename[sizeof(filename)-1] = '\0';
341 dbg("wait %i sec for '%s'", timeout, filename);
344 if (stat(filename, &stats) == 0) {
345 dbg("file appeared after %i loops", (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
348 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
350 dbg("waiting for '%s' failed", filename);
354 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
355 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
357 char temp[PATH_SIZE];
358 char temp2[PATH_SIZE];
359 char *head, *tail, *pos, *cpos, *attr, *rest;
362 unsigned int next_free_number;
363 struct sysfs_class_device *class_dev_parent;
381 static const struct subst_map {
384 enum subst_type type;
386 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
387 { .name = "id", .fmt = 'b', .type = SUBST_ID },
388 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
389 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL_NAME },
390 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
391 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
392 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
393 { .name = "sysfs", .fmt = 's', .type = SUBST_SYSFS },
394 { .name = "enum", .fmt = 'e', .type = SUBST_ENUM },
395 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
396 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
397 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
398 { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
399 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
402 enum subst_type type;
403 const struct subst_map *subst;
408 while (head[0] != '\0') {
409 if (head[0] == '$') {
410 /* substitute named variable */
413 if (head[1] == '$') {
414 strlcpy(temp, head+2, sizeof(temp));
415 strlcpy(head+1, temp, maxsize);
420 for (subst = map; subst->name; subst++) {
421 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
423 tail = head + strlen(subst->name)+1;
424 dbg("will substitute format name '%s'", subst->name);
429 else if (head[0] == '%') {
430 /* substitute format char */
433 if (head[1] == '%') {
434 strlcpy(temp, head+2, sizeof(temp));
435 strlcpy(head+1, temp, maxsize);
441 len = get_format_len(&tail);
442 for (subst = map; subst->name; subst++) {
443 if (tail[0] == subst->fmt) {
446 dbg("will substitute format char '%c'", subst->fmt);
455 attr = get_format_attribute(&tail);
456 strlcpy(temp, tail, sizeof(temp));
457 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
458 type ,string, tail, class_dev, sysfs_device);
462 strlcat(string, udev->devpath, maxsize);
463 dbg("substitute devpath '%s'", udev->devpath);
466 strlcat(string, udev->bus_id, maxsize);
467 dbg("substitute bus_id '%s'", udev->bus_id);
469 case SUBST_KERNEL_NAME:
470 strlcat(string, udev->kernel_name, maxsize);
471 dbg("substitute kernel name '%s'", udev->kernel_name);
473 case SUBST_KERNEL_NUMBER:
474 strlcat(string, udev->kernel_number, maxsize);
475 dbg("substitute kernel number '%s'", udev->kernel_number);
478 sprintf(temp2, "%d", major(udev->devt));
479 strlcat(string, temp2, maxsize);
480 dbg("substitute major number '%s'", temp2);
483 sprintf(temp2, "%d", minor(udev->devt));
484 strlcat(string, temp2, maxsize);
485 dbg("substitute minor number '%s'", temp2);
488 if (udev->program_result[0] == '\0')
490 /* get part part of the result string */
493 i = strtoul(attr, &rest, 10);
495 dbg("request part #%d of result string", i);
496 cpos = udev->program_result;
498 while (cpos[0] != '\0' && !isspace(cpos[0]))
500 while (isspace(cpos[0]))
504 err("requested part of result string not found");
507 strlcpy(temp2, cpos, sizeof(temp2));
508 /* %{2+}c copies the whole string from the second part on */
509 if (rest[0] != '+') {
510 cpos = strchr(temp2, ' ');
514 strlcat(string, temp2, maxsize);
515 dbg("substitute part of result string '%s'", temp2);
517 strlcat(string, udev->program_result, maxsize);
518 dbg("substitute result string '%s'", udev->program_result);
523 dbg("missing attribute");
526 if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
527 struct sysfs_device *parent_device;
529 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
530 parent_device = sysfs_get_device_parent(sysfs_device);
531 while (parent_device) {
532 dbg("looking at '%s'", parent_device->path);
533 if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
535 parent_device = sysfs_get_device_parent(parent_device);
540 /* strip trailing whitespace of sysfs value */
542 while (i > 0 && isspace(temp2[i-1]))
544 replace_untrusted_chars(temp2);
545 strlcat(string, temp2, maxsize);
546 dbg("substitute sysfs value '%s'", temp2);
549 next_free_number = find_free_number(string);
550 if (next_free_number > 0) {
551 sprintf(temp2, "%d", next_free_number);
552 strlcat(string, temp2, maxsize);
558 class_dev_parent = sysfs_get_classdev_parent(class_dev);
559 if (class_dev_parent != NULL) {
560 struct udevice udev_parent;
562 dbg("found parent '%s', get the node name", class_dev_parent->path);
563 udev_init_device(&udev_parent, NULL, NULL, NULL);
564 /* lookup the name in the udev_db with the DEVPATH of the parent */
565 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
566 strlcat(string, udev_parent.name, maxsize);
567 dbg("substitute parent node name'%s'", udev_parent.name);
569 dbg("parent not found in database");
570 udev_cleanup_device(&udev_parent);
573 case SUBST_TEMP_NODE:
574 if (udev->tmp_node[0] == '\0') {
575 dbg("create temporary device node for callout");
576 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
577 udev_root, major(udev->devt), minor(udev->devt));
578 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
579 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
581 strlcat(string, udev->tmp_node, maxsize);
582 dbg("substitute temporary device node name '%s'", udev->tmp_node);
585 strlcat(string, udev_root, maxsize);
586 dbg("substitute udev_root '%s'", udev_root);
589 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
591 strlcat(string, temp2, maxsize);
592 dbg("substitute MODALIAS '%s'", temp2);
596 dbg("missing attribute");
601 dbg("env '%s' not avialable", attr);
604 dbg("substitute env '%s=%s'", attr, pos);
605 strlcat(string, pos, maxsize);
608 err("unknown substitution type=%i", type);
611 /* possibly truncate to format-char specified length */
614 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
616 strlcat(string, temp, maxsize);
620 static char *key_val(struct udev_rule *rule, struct key *key)
622 return rule->buf + key->val_off;
625 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
627 return rule->buf + pair->key_name_off;
630 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
633 char value[PATH_SIZE];
637 if (key->operation == KEY_OP_UNSET)
640 strlcpy(value, rule->buf + key->val_off, sizeof(value));
643 dbg("key %s value='%s'", key_name, key_value);
645 pos = strchr(key_value, '|');
650 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
651 match = (strcmp_pattern(key_value, val) == 0);
652 if (match && (key->operation != KEY_OP_NOMATCH)) {
653 dbg("%s is true (matching value)", key_name);
656 if (!match && (key->operation == KEY_OP_NOMATCH)) {
657 dbg("%s is true (non-matching value)", key_name);
662 dbg("%s is false", key_name);
666 static int match_rule(struct udevice *udev, struct udev_rule *rule,
667 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
669 struct sysfs_device *parent_device = sysfs_device;
671 if (match_key("ACTION", rule, &rule->action, udev->action))
674 if (match_key("KERNEL", rule, &rule->kernel_name, udev->kernel_name))
677 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->subsystem))
680 if (match_key("DEVPATH", rule, &rule->devpath, udev->devpath))
683 if (rule->modalias.operation != KEY_OP_UNSET) {
684 char value[NAME_SIZE];
686 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
687 dbg("MODALIAS value not found");
690 if (match_key("MODALIAS", rule, &rule->modalias, value))
694 if (rule->env.count) {
697 dbg("check %i ENV keys", rule->env.count);
698 for (i = 0; i < rule->env.count; i++) {
699 struct key_pair *pair = &rule->env.keys[i];
700 const char *key_name = key_pair_name(rule, pair);
701 const char *value = getenv(key_name);
704 dbg("ENV{'%s'} is not set", key_name);
707 if (match_key("ENV", rule, &pair->key, value))
710 dbg("all %i ENV keys matched", rule->env.count);
713 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
716 match = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
717 if (match && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH)) {
718 dbg("WAIT_FOR_SYSFS is true (matching value)");
721 if (!match && (rule->wait_for_sysfs.operation == KEY_OP_NOMATCH)) {
722 dbg("WAIT_FOR_SYSFS is true, (non matching value)");
725 dbg("WAIT_FOR_SYSFS is false");
729 /* walk up the chain of physical devices and find a match */
731 /* check for matching driver */
732 if (rule->driver.operation != KEY_OP_UNSET) {
733 if (parent_device == NULL) {
734 dbg("device has no sysfs_device");
737 if (match_key("DRIVER", rule, &rule->driver, parent_device->driver_name))
741 /* check for matching bus value */
742 if (rule->bus.operation != KEY_OP_UNSET) {
743 if (parent_device == NULL) {
744 dbg("device has no sysfs_device");
747 if (match_key("BUS", rule, &rule->bus, parent_device->bus))
751 /* check for matching bus id */
752 if (rule->id.operation != KEY_OP_UNSET) {
753 if (parent_device == NULL) {
754 dbg("device has no sysfs_device");
757 if (match_key("ID", rule, &rule->id, parent_device->bus_id))
761 /* check for matching sysfs pairs */
762 if (rule->sysfs.count) {
765 dbg("check %i SYSFS keys", rule->sysfs.count);
766 for (i = 0; i < rule->sysfs.count; i++) {
767 struct key_pair *pair = &rule->sysfs.keys[i];
768 const char *key_name = key_pair_name(rule, pair);
769 const char *key_value = key_val(rule, &pair->key);
770 char value[VALUE_SIZE];
773 if (find_sysfs_attribute(class_dev, parent_device, key_name, value, sizeof(value)) != 0)
776 /* strip trailing whitespace of value, if not asked to match for it */
777 len = strlen(key_value);
778 if (len && !isspace(key_value[len-1])) {
780 while (len > 0 && isspace(value[len-1]))
782 dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
785 if (match_key("SYSFS", rule, &pair->key, value))
788 dbg("all %i SYSFS keys matched", rule->sysfs.count);
791 /* found matching physical device */
794 dbg("try parent sysfs device");
795 parent_device = sysfs_get_device_parent(parent_device);
796 if (parent_device == NULL)
798 dbg("look at sysfs_device->path='%s'", parent_device->path);
799 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
802 /* import variables from file into environment */
803 if (rule->import.operation != KEY_OP_UNSET) {
804 char import[PATH_SIZE];
807 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
808 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
809 dbg("check for IMPORT import='%s'", import);
810 if (rule->import_type == IMPORT_PROGRAM) {
811 info("IMPORT executes '%s'", import);
812 rc = import_program_into_env(udev, import);
813 } else if (rule->import_type == IMPORT_FILE) {
814 dbg("import file import='%s'", import);
815 rc = import_file_into_env(udev, import);
816 } else if (rule->import_type == IMPORT_PARENT && class_dev) {
817 dbg("import parent import='%s'", import);
818 rc = import_parent_into_env(udev, class_dev, import);
821 dbg("IMPORT failed");
822 if (rule->import.operation != KEY_OP_NOMATCH)
825 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
826 dbg("IMPORT key is true");
829 /* execute external program */
830 if (rule->program.operation != KEY_OP_UNSET) {
831 char program[PATH_SIZE];
832 char result[PATH_SIZE];
834 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
835 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
836 info("PROGRAM key executes '%s", program);
837 if (run_program(program, udev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_DEBUG)) != 0) {
838 dbg("PROGRAM is false");
839 udev->program_result[0] = '\0';
840 if (rule->program.operation != KEY_OP_NOMATCH)
843 dbg("PROGRAM matches");
844 remove_trailing_char(result, '\n');
845 replace_untrusted_chars(result);
846 dbg("result is '%s'", result);
847 strlcpy(udev->program_result, result, sizeof(udev->program_result));
848 dbg("PROGRAM returned successful");
849 if (rule->program.operation == KEY_OP_NOMATCH)
852 dbg("PROGRAM key is true");
855 /* check for matching result of external program */
856 if (match_key("RESULT", rule, &rule->result, udev->program_result))
866 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct sysfs_class_device *class_dev)
868 struct sysfs_class_device *class_dev_parent;
869 struct sysfs_device *sysfs_device = NULL;
870 struct udev_rule *rule;
873 dbg("class_dev->name='%s'", class_dev->name);
875 /* Figure out where the "device"-symlink is at. For char devices this will
876 * always be in the class_dev->path. On block devices, only the main block
877 * device will have the device symlink in it's path. All partition devices
878 * need to look at the symlink in its parent directory.
880 class_dev_parent = sysfs_get_classdev_parent(class_dev);
881 if (class_dev_parent != NULL) {
882 dbg("given class device has a parent, use this instead");
883 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
885 sysfs_device = sysfs_get_classdev_device(class_dev);
889 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
890 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
891 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
894 dbg("udev->kernel_name='%s'", udev->kernel_name);
896 /* look for a matching rule to apply */
897 udev_rules_iter_init(rules);
899 rule = udev_rules_iter_next(rules);
903 if (name_set && rule->name.operation != KEY_OP_UNSET) {
904 dbg("node name already set, rule ignored");
909 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
911 if (rule->ignore_device) {
912 info("rule applied, '%s' is ignored", udev->kernel_name);
913 udev->ignore_device = 1;
916 if (rule->ignore_remove) {
917 udev->ignore_remove = 1;
918 dbg("remove event should be ignored");
920 /* apply all_partitions option only at a main block device */
921 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
922 udev->partitions = rule->partitions;
923 dbg("creation of partition nodes requested");
926 /* apply permissions */
927 if (!udev->mode_final && rule->mode != 0000) {
928 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
929 udev->mode_final = 1;
930 udev->mode = rule->mode;
931 dbg("applied mode=%#o to '%s'", rule->mode, udev->kernel_name);
933 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
934 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
935 udev->owner_final = 1;
936 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
937 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
938 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
940 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
941 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
942 udev->group_final = 1;
943 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
944 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
945 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
948 /* collect symlinks */
949 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
950 char temp[PATH_SIZE];
953 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
954 udev->symlink_final = 1;
955 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
956 struct name_entry *name_loop;
957 struct name_entry *temp_loop;
959 info("reset symlink list");
960 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
961 list_del(&name_loop->node);
965 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
966 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
967 info("rule applied, added symlink '%s'", temp);
969 /* add multiple symlinks separated by spaces */
971 while (isspace(pos[0]))
973 next = strchr(pos, ' ');
976 info("add symlink '%s'", pos);
977 name_list_add(&udev->symlink_list, pos, 0);
978 while (isspace(next[1]))
981 next = strchr(pos, ' ');
983 if (pos[0] != '\0') {
984 info("add symlink '%s'", pos);
985 name_list_add(&udev->symlink_list, pos, 0);
989 /* set name, later rules with name set will be ignored */
990 if (rule->name.operation != KEY_OP_UNSET) {
992 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
993 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
995 info("rule applied, '%s' becomes '%s'", udev->kernel_name, udev->name);
996 if (udev->type != DEV_NET)
997 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
998 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1001 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1002 char program[PATH_SIZE];
1004 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1005 udev->run_final = 1;
1006 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1007 struct name_entry *name_loop;
1008 struct name_entry *temp_loop;
1010 info("reset run list");
1011 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1012 list_del(&name_loop->node);
1016 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1017 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
1018 dbg("add run '%s'", program);
1019 name_list_add(&udev->run_list, program, 0);
1022 if (rule->last_rule) {
1023 dbg("last rule to be applied");
1027 if (rule->goto_label.operation != KEY_OP_UNSET) {
1028 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1029 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1035 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
1036 info("no node name set, will use kernel name '%s'", udev->name);
1039 if (udev->tmp_node[0] != '\0') {
1040 dbg("removing temporary device node");
1041 unlink_secure(udev->tmp_node);
1042 udev->tmp_node[0] = '\0';
1048 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev,
1049 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_dev)
1051 struct udev_rule *rule;
1053 if (class_dev && !sysfs_dev)
1054 sysfs_dev = sysfs_get_classdev_device(class_dev);
1056 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
1057 sysfs_dev->path, sysfs_dev->bus_id, sysfs_dev->bus);
1058 strlcpy(udev->bus_id, sysfs_dev->bus_id, sizeof(udev->bus_id));
1061 dbg("udev->kernel_name='%s'", udev->kernel_name);
1063 /* look for a matching rule to apply */
1064 udev_rules_iter_init(rules);
1066 rule = udev_rules_iter_next(rules);
1070 dbg("process rule");
1071 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1072 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1073 dbg("skip rule that names a device");
1077 if (match_rule(udev, rule, class_dev, sysfs_dev) == 0) {
1078 if (rule->ignore_device) {
1079 info("rule applied, '%s' is ignored", udev->kernel_name);
1080 udev->ignore_device = 1;
1084 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1085 char program[PATH_SIZE];
1087 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1088 struct name_entry *name_loop;
1089 struct name_entry *temp_loop;
1091 info("reset run list");
1092 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1093 list_del(&name_loop->node);
1097 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1098 apply_format(udev, program, sizeof(program), class_dev, sysfs_dev);
1099 dbg("add run '%s'", program);
1100 name_list_add(&udev->run_list, program, 0);
1101 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1105 if (rule->last_rule) {
1106 dbg("last rule to be applied");
1110 if (rule->goto_label.operation != KEY_OP_UNSET) {
1111 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1112 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));