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.
33 #include "libsysfs/sysfs/libsysfs.h"
35 #include "udev_libc_wrapper.h"
37 #include "udev_utils.h"
38 #include "udev_version.h"
40 #include "udev_rules.h"
44 /* extract possible {attr} and move str behind it */
45 static char *get_format_attribute(char **str)
51 pos = strchr(*str, '}');
53 err("missing closing brace for format");
59 dbg("attribute='%s', str='%s'", attr, *str);
64 /* extract possible format length and move str behind it*/
65 static int get_format_len(char **str)
70 if (isdigit(*str[0])) {
71 num = (int) strtoul(*str, &tail, 10);
74 dbg("format length=%i", num);
77 err("format parsing error '%s'", *str);
83 static int get_key(char **line, char **key, char **value)
92 if (strchr(linepos, '\\')) {
93 dbg("escaped characters are not supported, skip");
98 while (isspace(linepos[0]))
105 if (linepos[0] == '\0')
107 if (isspace(linepos[0]))
109 if (linepos[0] == '=')
117 /* skip whitespace */
118 while (isspace(linepos[0]))
122 if (linepos[0] == '"') {
124 temp = strchr(linepos, '"');
126 dbg("missing closing quote");
129 dbg("value is quoted");
131 } else if (linepos[0] == '\'') {
133 temp = strchr(linepos, '\'');
135 dbg("missing closing quote");
138 dbg("value is quoted");
140 } else if (linepos[0] == '\0') {
141 dbg("value is empty");
144 while (temp[0] && !isspace(temp[0]))
153 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
155 char line[LINE_SIZE];
164 /* loop through the whole buffer */
167 while (cur < bufsize) {
168 count = buf_get_line(buf, bufsize, cur);
173 if (count >= sizeof(line)) {
174 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
178 /* eat the whitespace */
179 while ((count > 0) && isspace(bufline[0])) {
186 /* see if this is a comment */
187 if (bufline[0] == COMMENT_CHARACTER)
190 memcpy(line, bufline, count);
194 if (get_key(&linepos, &variable, &value) == 0) {
195 dbg("import '%s=%s'", variable, value);
196 name_list_key_add(&udev->env_list, variable, value);
197 setenv(variable, value, 1);
204 static int import_file_into_env(struct udevice *udev, const char *filename)
209 if (file_map(filename, &buf, &bufsize) != 0) {
210 err("can't open '%s'", filename);
213 import_keys_into_env(udev, buf, bufsize);
214 file_unmap(buf, bufsize);
219 static int import_program_into_env(struct udevice *udev, const char *program)
224 if (execute_program(program, udev->subsystem, result, sizeof(result), &reslen) != 0)
226 return import_keys_into_env(udev, result, reslen);
229 static int import_parent_into_env(struct udevice *udev, struct sysfs_class_device *class_dev, const char *filter)
231 struct sysfs_class_device *parent = sysfs_get_classdev_parent(class_dev);
234 if (parent != NULL) {
235 struct udevice udev_parent;
236 struct name_entry *name_loop;
238 dbg("found parent '%s', get the node name", parent->path);
239 udev_init_device(&udev_parent, NULL, NULL, NULL);
240 /* import the udev_db of the parent */
241 if (udev_db_get_device(&udev_parent, &parent->path[strlen(sysfs_path)]) == 0) {
242 dbg("import stored parent env '%s'", udev_parent.name);
243 list_for_each_entry(name_loop, &udev_parent.env_list, node) {
244 char name[NAME_SIZE];
247 strlcpy(name, name_loop->name, sizeof(name));
248 pos = strchr(name, '=');
252 if (strcmp_pattern(filter, name) == 0) {
253 dbg("import key '%s'", name_loop->name);
254 name_list_add(&udev->env_list, name_loop->name, 0);
255 setenv(name, pos, 1);
257 dbg("skip key '%s'", name_loop->name);
262 dbg("parent not found in database");
263 udev_cleanup_device(&udev_parent);
269 /* finds the lowest positive N such that <name>N isn't present in the udevdb
270 * if <name> doesn't exist, 0 is returned, N otherwise
272 static int find_free_number(struct udevice *udev, const char *name)
274 char devpath[PATH_SIZE];
275 char filename[PATH_SIZE];
278 strlcpy(filename, name, sizeof(filename));
280 dbg("look for existing node '%s'", filename);
281 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
282 dbg("free num=%d", num);
288 info("find_free_number gone crazy (num=%d), aborted", num);
291 snprintf(filename, sizeof(filename), "%s%d", name, num);
292 filename[sizeof(filename)-1] = '\0';
296 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
297 const char *name, char *value, size_t len)
299 struct sysfs_class_device *class_dev_parent;
300 struct sysfs_attribute *tmpattr;
302 dbg("look for device attribute '%s'", name);
304 dbg("look for class attribute '%s/%s'", class_dev->path, name);
305 tmpattr = sysfs_get_classdev_attr(class_dev, name);
308 class_dev_parent = sysfs_get_classdev_parent(class_dev);
309 if (class_dev_parent) {
310 tmpattr = sysfs_get_classdev_attr(class_dev_parent, name);
316 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
317 tmpattr = sysfs_get_device_attr(sysfs_device, name);
324 strlcpy(value, tmpattr->value, len);
325 remove_trailing_char(value, '\n');
327 dbg("found attribute '%s'", tmpattr->path);
331 #define WAIT_LOOP_PER_SECOND 20
332 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
334 char filename[PATH_SIZE];
336 int loop = timeout * WAIT_LOOP_PER_SECOND;
338 snprintf(filename, sizeof(filename), "%s%s/%s", sysfs_path, udev->devpath, file);
339 filename[sizeof(filename)-1] = '\0';
340 dbg("wait %i sec for '%s'", timeout, filename);
343 if (stat(filename, &stats) == 0) {
344 dbg("file appeared after %i loops", (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
347 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
349 dbg("waiting for '%s' failed", filename);
353 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
354 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
356 char temp[PATH_SIZE];
357 char temp2[PATH_SIZE];
358 char *head, *tail, *pos, *cpos, *attr, *rest;
361 unsigned int next_free_number;
362 struct sysfs_class_device *class_dev_parent;
380 static const struct subst_map {
383 enum subst_type type;
385 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
386 { .name = "id", .fmt = 'b', .type = SUBST_ID },
387 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
388 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL_NAME },
389 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
390 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
391 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
392 { .name = "sysfs", .fmt = 's', .type = SUBST_SYSFS },
393 { .name = "enum", .fmt = 'e', .type = SUBST_ENUM },
394 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
395 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
396 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
397 { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
398 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
401 enum subst_type type;
402 const struct subst_map *subst;
407 while (head[0] != '\0') {
408 if (head[0] == '$') {
409 /* substitute named variable */
412 if (head[1] == '$') {
413 strlcpy(temp, head+2, sizeof(temp));
414 strlcpy(head+1, temp, maxsize);
419 for (subst = map; subst->name; subst++) {
420 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
422 tail = head + strlen(subst->name)+1;
423 dbg("will substitute format name '%s'", subst->name);
428 else if (head[0] == '%') {
429 /* substitute format char */
432 if (head[1] == '%') {
433 strlcpy(temp, head+2, sizeof(temp));
434 strlcpy(head+1, temp, maxsize);
440 len = get_format_len(&tail);
441 for (subst = map; subst->name; subst++) {
442 if (tail[0] == subst->fmt) {
445 dbg("will substitute format char '%c'", subst->fmt);
454 attr = get_format_attribute(&tail);
455 strlcpy(temp, tail, sizeof(temp));
456 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
457 type ,string, tail, class_dev, sysfs_device);
461 strlcat(string, udev->devpath, maxsize);
462 dbg("substitute devpath '%s'", udev->devpath);
465 strlcat(string, udev->bus_id, maxsize);
466 dbg("substitute bus_id '%s'", udev->bus_id);
468 case SUBST_KERNEL_NAME:
469 strlcat(string, udev->kernel_name, maxsize);
470 dbg("substitute kernel name '%s'", udev->kernel_name);
472 case SUBST_KERNEL_NUMBER:
473 strlcat(string, udev->kernel_number, maxsize);
474 dbg("substitute kernel number '%s'", udev->kernel_number);
477 sprintf(temp2, "%d", major(udev->devt));
478 strlcat(string, temp2, maxsize);
479 dbg("substitute major number '%s'", temp2);
482 sprintf(temp2, "%d", minor(udev->devt));
483 strlcat(string, temp2, maxsize);
484 dbg("substitute minor number '%s'", temp2);
487 if (udev->program_result[0] == '\0')
489 /* get part part of the result string */
492 i = strtoul(attr, &rest, 10);
494 dbg("request part #%d of result string", i);
495 cpos = udev->program_result;
497 while (cpos[0] != '\0' && !isspace(cpos[0]))
499 while (isspace(cpos[0]))
503 err("requested part of result string not found");
506 strlcpy(temp2, cpos, sizeof(temp2));
507 /* %{2+}c copies the whole string from the second part on */
508 if (rest[0] != '+') {
509 cpos = strchr(temp2, ' ');
513 strlcat(string, temp2, maxsize);
514 dbg("substitute part of result string '%s'", temp2);
516 strlcat(string, udev->program_result, maxsize);
517 dbg("substitute result string '%s'", udev->program_result);
522 dbg("missing attribute");
525 if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
526 struct sysfs_device *parent_device;
528 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
529 parent_device = sysfs_get_device_parent(sysfs_device);
530 while (parent_device) {
531 dbg("looking at '%s'", parent_device->path);
532 if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
534 parent_device = sysfs_get_device_parent(parent_device);
539 /* strip trailing whitespace of sysfs value */
541 while (i > 0 && isspace(temp2[i-1]))
543 replace_untrusted_chars(temp2);
544 strlcat(string, temp2, maxsize);
545 dbg("substitute sysfs value '%s'", temp2);
548 next_free_number = find_free_number(udev, string);
549 if (next_free_number > 0) {
550 sprintf(temp2, "%d", next_free_number);
551 strlcat(string, temp2, maxsize);
557 class_dev_parent = sysfs_get_classdev_parent(class_dev);
558 if (class_dev_parent != NULL) {
559 struct udevice udev_parent;
561 dbg("found parent '%s', get the node name", class_dev_parent->path);
562 udev_init_device(&udev_parent, NULL, NULL, NULL);
563 /* lookup the name in the udev_db with the DEVPATH of the parent */
564 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
565 strlcat(string, udev_parent.name, maxsize);
566 dbg("substitute parent node name'%s'", udev_parent.name);
568 dbg("parent not found in database");
569 udev_cleanup_device(&udev_parent);
572 case SUBST_TEMP_NODE:
573 if (udev->tmp_node[0] == '\0') {
574 dbg("create temporary device node for callout");
575 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
576 udev_root, major(udev->devt), minor(udev->devt));
577 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
578 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
580 strlcat(string, udev->tmp_node, maxsize);
581 dbg("substitute temporary device node name '%s'", udev->tmp_node);
584 strlcat(string, udev_root, maxsize);
585 dbg("substitute udev_root '%s'", udev_root);
588 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
590 strlcat(string, temp2, maxsize);
591 dbg("substitute MODALIAS '%s'", temp2);
595 dbg("missing attribute");
600 dbg("env '%s' not avialable", attr);
603 dbg("substitute env '%s=%s'", attr, pos);
604 strlcat(string, pos, maxsize);
607 err("unknown substitution type=%i", type);
610 /* possibly truncate to format-char specified length */
613 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
615 strlcat(string, temp, maxsize);
619 static char *key_val(struct udev_rule *rule, struct key *key)
621 return rule->buf + key->val_off;
624 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
626 return rule->buf + pair->key_name_off;
629 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
632 char value[PATH_SIZE];
636 if (key->operation == KEY_OP_UNSET)
639 strlcpy(value, rule->buf + key->val_off, sizeof(value));
642 dbg("key %s value='%s'", key_name, key_value);
644 pos = strchr(key_value, '|');
649 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
650 match = (strcmp_pattern(key_value, val) == 0);
651 if (match && (key->operation != KEY_OP_NOMATCH)) {
652 dbg("%s is true (matching value)", key_name);
655 if (!match && (key->operation == KEY_OP_NOMATCH)) {
656 dbg("%s is true (non-matching value)", key_name);
661 dbg("%s is false", key_name);
665 static int match_rule(struct udevice *udev, struct udev_rule *rule,
666 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
668 struct sysfs_device *parent_device = sysfs_device;
670 if (match_key("ACTION", rule, &rule->action, udev->action))
673 if (match_key("KERNEL", rule, &rule->kernel_name, udev->kernel_name))
676 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->subsystem))
679 if (match_key("DEVPATH", rule, &rule->devpath, udev->devpath))
682 if (rule->modalias.operation != KEY_OP_UNSET) {
683 char value[NAME_SIZE];
685 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
686 dbg("MODALIAS value not found");
689 if (match_key("MODALIAS", rule, &rule->modalias, value))
693 if (rule->env.count) {
696 dbg("check %i ENV keys", rule->env.count);
697 for (i = 0; i < rule->env.count; i++) {
698 struct key_pair *pair = &rule->env.keys[i];
699 const char *key_name = key_pair_name(rule, pair);
700 const char *value = getenv(key_name);
703 dbg("ENV{'%s'} is not set", key_name);
706 if (match_key("ENV", rule, &pair->key, value))
709 dbg("all %i ENV keys matched", rule->env.count);
712 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
715 match = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
716 if (match && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH)) {
717 dbg("WAIT_FOR_SYSFS is true (matching value)");
720 if (!match && (rule->wait_for_sysfs.operation == KEY_OP_NOMATCH)) {
721 dbg("WAIT_FOR_SYSFS is true, (non matching value)");
724 dbg("WAIT_FOR_SYSFS is false");
728 /* walk up the chain of physical devices and find a match */
730 /* check for matching driver */
731 if (rule->driver.operation != KEY_OP_UNSET) {
732 if (parent_device == NULL) {
733 dbg("device has no sysfs_device");
736 if (match_key("DRIVER", rule, &rule->driver, parent_device->driver_name))
740 /* check for matching bus value */
741 if (rule->bus.operation != KEY_OP_UNSET) {
742 if (parent_device == NULL) {
743 dbg("device has no sysfs_device");
746 if (match_key("BUS", rule, &rule->bus, parent_device->bus))
750 /* check for matching bus id */
751 if (rule->id.operation != KEY_OP_UNSET) {
752 if (parent_device == NULL) {
753 dbg("device has no sysfs_device");
756 if (match_key("ID", rule, &rule->id, parent_device->bus_id))
760 /* check for matching sysfs pairs */
761 if (rule->sysfs.count) {
764 dbg("check %i SYSFS keys", rule->sysfs.count);
765 for (i = 0; i < rule->sysfs.count; i++) {
766 struct key_pair *pair = &rule->sysfs.keys[i];
767 const char *key_name = key_pair_name(rule, pair);
768 const char *key_value = key_val(rule, &pair->key);
769 char value[VALUE_SIZE];
772 if (find_sysfs_attribute(class_dev, parent_device, key_name, value, sizeof(value)) != 0)
775 /* strip trailing whitespace of value, if not asked to match for it */
776 len = strlen(key_value);
777 if (len && !isspace(key_value[len-1])) {
779 while (len > 0 && isspace(value[len-1]))
781 dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
784 if (match_key("SYSFS", rule, &pair->key, value))
787 dbg("all %i SYSFS keys matched", rule->sysfs.count);
790 /* found matching physical device */
793 dbg("try parent sysfs device");
794 parent_device = sysfs_get_device_parent(parent_device);
795 if (parent_device == NULL)
797 dbg("look at sysfs_device->path='%s'", parent_device->path);
798 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
801 /* import variables from file into environment */
802 if (rule->import.operation != KEY_OP_UNSET) {
803 char import[PATH_SIZE];
806 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
807 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
808 dbg("check for IMPORT import='%s'", import);
809 if (rule->import_type == IMPORT_PROGRAM) {
810 dbg("run executable file import='%s'", import);
811 rc = import_program_into_env(udev, import);
812 } else if (rule->import_type == IMPORT_FILE) {
813 dbg("import file import='%s'", import);
814 rc = import_file_into_env(udev, import);
815 } else if (rule->import_type == IMPORT_PARENT && class_dev) {
816 dbg("import parent import='%s'", import);
817 rc = import_parent_into_env(udev, class_dev, import);
820 dbg("IMPORT failed");
821 if (rule->import.operation != KEY_OP_NOMATCH)
824 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
825 dbg("IMPORT key is true");
828 /* execute external program */
829 if (rule->program.operation != KEY_OP_UNSET) {
830 char program[PATH_SIZE];
831 char result[PATH_SIZE];
833 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
834 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
835 dbg("check for PROGRAM program='%s", program);
836 if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) {
837 dbg("PROGRAM is false");
838 if (rule->program.operation != KEY_OP_NOMATCH)
841 dbg("PROGRAM matches");
842 remove_trailing_char(result, '\n');
843 replace_untrusted_chars(result);
844 dbg("result is '%s'", result);
845 strlcpy(udev->program_result, result, sizeof(udev->program_result));
846 dbg("PROGRAM returned successful");
847 if (rule->program.operation == KEY_OP_NOMATCH)
850 dbg("PROGRAM key is true");
853 /* check for matching result of external program */
854 if (match_key("RESULT", rule, &rule->result, udev->program_result))
864 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct sysfs_class_device *class_dev)
866 struct sysfs_class_device *class_dev_parent;
867 struct sysfs_device *sysfs_device = NULL;
868 struct udev_rule *rule;
871 dbg("class_dev->name='%s'", class_dev->name);
873 /* Figure out where the "device"-symlink is at. For char devices this will
874 * always be in the class_dev->path. On block devices, only the main block
875 * device will have the device symlink in it's path. All partition devices
876 * need to look at the symlink in its parent directory.
878 class_dev_parent = sysfs_get_classdev_parent(class_dev);
879 if (class_dev_parent != NULL) {
880 dbg("given class device has a parent, use this instead");
881 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
883 sysfs_device = sysfs_get_classdev_device(class_dev);
887 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
888 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
889 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
892 dbg("udev->kernel_name='%s'", udev->kernel_name);
894 /* look for a matching rule to apply */
895 udev_rules_iter_init(rules);
897 rule = udev_rules_iter_next(rules);
901 if (name_set && rule->name.operation != KEY_OP_UNSET) {
902 dbg("node name already set, rule ignored");
907 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
909 if (rule->ignore_device) {
910 info("rule applied, '%s' is ignored", udev->kernel_name);
911 udev->ignore_device = 1;
914 if (rule->ignore_remove) {
915 udev->ignore_remove = 1;
916 dbg("remove event should be ignored");
918 /* apply all_partitions option only at a main block device */
919 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
920 udev->partitions = rule->partitions;
921 dbg("creation of partition nodes requested");
924 /* apply permissions */
925 if (!udev->mode_final && rule->mode != 0000) {
926 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
927 udev->mode_final = 1;
928 udev->mode = rule->mode;
929 dbg("applied mode=%#o to '%s'", rule->mode, udev->kernel_name);
931 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
932 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
933 udev->owner_final = 1;
934 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
935 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
936 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
938 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
939 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
940 udev->group_final = 1;
941 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
942 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
943 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
946 /* collect symlinks */
947 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
948 char temp[PATH_SIZE];
951 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
952 udev->symlink_final = 1;
953 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
954 struct name_entry *name_loop;
955 struct name_entry *temp_loop;
957 info("reset symlink list");
958 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
959 list_del(&name_loop->node);
963 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
964 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
965 info("rule applied, added symlink '%s'", temp);
967 /* add multiple symlinks separated by spaces */
969 while (isspace(pos[0]))
971 next = strchr(pos, ' ');
974 info("add symlink '%s'", pos);
975 name_list_add(&udev->symlink_list, pos, 0);
976 while (isspace(next[1]))
979 next = strchr(pos, ' ');
981 if (pos[0] != '\0') {
982 info("add symlink '%s'", pos);
983 name_list_add(&udev->symlink_list, pos, 0);
987 /* set name, later rules with name set will be ignored */
988 if (rule->name.operation != KEY_OP_UNSET) {
990 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
991 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
993 info("rule applied, '%s' becomes '%s'", udev->kernel_name, udev->name);
994 if (udev->type != DEV_NET)
995 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
996 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
999 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1000 char program[PATH_SIZE];
1002 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1003 udev->run_final = 1;
1004 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1005 struct name_entry *name_loop;
1006 struct name_entry *temp_loop;
1008 info("reset run list");
1009 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1010 list_del(&name_loop->node);
1014 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1015 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
1016 dbg("add run '%s'", program);
1017 name_list_add(&udev->run_list, program, 0);
1020 if (rule->last_rule) {
1021 dbg("last rule to be applied");
1025 if (rule->goto_label.operation != KEY_OP_UNSET) {
1026 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1027 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1033 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
1034 info("no rule found, will use kernel name '%s'", udev->name);
1037 if (udev->tmp_node[0] != '\0') {
1038 dbg("removing temporary device node");
1039 unlink_secure(udev->tmp_node);
1040 udev->tmp_node[0] = '\0';
1046 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev,
1047 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_dev)
1049 struct udev_rule *rule;
1051 if (class_dev && !sysfs_dev)
1052 sysfs_dev = sysfs_get_classdev_device(class_dev);
1054 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
1055 sysfs_dev->path, sysfs_dev->bus_id, sysfs_dev->bus);
1056 strlcpy(udev->bus_id, sysfs_dev->bus_id, sizeof(udev->bus_id));
1059 dbg("udev->kernel_name='%s'", udev->kernel_name);
1061 /* look for a matching rule to apply */
1062 udev_rules_iter_init(rules);
1064 rule = udev_rules_iter_next(rules);
1068 dbg("process rule");
1069 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1070 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1071 dbg("skip rule that names a device");
1075 if (match_rule(udev, rule, class_dev, sysfs_dev) == 0) {
1076 if (rule->ignore_device) {
1077 info("rule applied, '%s' is ignored", udev->kernel_name);
1078 udev->ignore_device = 1;
1082 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1083 char program[PATH_SIZE];
1085 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1086 struct name_entry *name_loop;
1087 struct name_entry *temp_loop;
1089 info("reset run list");
1090 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1091 list_del(&name_loop->node);
1095 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1096 apply_format(udev, program, sizeof(program), class_dev, sysfs_dev);
1097 dbg("add run '%s'", program);
1098 name_list_add(&udev->run_list, program, 0);
1099 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1103 if (rule->last_rule) {
1104 dbg("last rule to be applied");
1108 if (rule->goto_label.operation != KEY_OP_UNSET) {
1109 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1110 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));