2 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 #include "udev_rules.h"
37 /* extract possible {attr} and move str behind it */
38 static char *get_format_attribute(char **str)
44 pos = strchr(*str, '}');
46 err("missing closing brace for format");
52 dbg("attribute='%s', str='%s'", attr, *str);
57 /* extract possible format length and move str behind it*/
58 static int get_format_len(char **str)
63 if (isdigit(*str[0])) {
64 num = (int) strtoul(*str, &tail, 10);
67 dbg("format length=%i", num);
70 err("format parsing error '%s'", *str);
76 static int get_key(char **line, char **key, char **value)
85 if (strchr(linepos, '\\')) {
86 dbg("escaped characters are not supported, skip");
91 while (isspace(linepos[0]))
98 if (linepos[0] == '\0')
100 if (isspace(linepos[0]))
102 if (linepos[0] == '=')
110 /* skip whitespace */
111 while (isspace(linepos[0]))
115 if (linepos[0] == '"') {
117 temp = strchr(linepos, '"');
119 dbg("missing closing quote");
122 dbg("value is quoted");
124 } else if (linepos[0] == '\'') {
126 temp = strchr(linepos, '\'');
128 dbg("missing closing quote");
131 dbg("value is quoted");
133 } else if (linepos[0] == '\0') {
134 dbg("value is empty");
137 while (temp[0] && !isspace(temp[0]))
146 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
148 char line[LINE_SIZE];
157 /* loop through the whole buffer */
160 while (cur < bufsize) {
161 count = buf_get_line(buf, bufsize, cur);
166 if (count >= sizeof(line)) {
167 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
171 /* eat the whitespace */
172 while ((count > 0) && isspace(bufline[0])) {
179 /* see if this is a comment */
180 if (bufline[0] == COMMENT_CHARACTER)
183 memcpy(line, bufline, count);
187 if (get_key(&linepos, &variable, &value) == 0) {
188 dbg("import '%s=%s'", variable, value);
190 /* handle device, renamed by external tool, returning new path */
191 if (strcmp(variable, "DEVPATH") == 0) {
192 info("updating devpath from '%s' to '%s'", udev->dev->devpath, value);
193 sysfs_device_set_values(udev->dev, value, NULL, NULL);
195 name_list_key_add(&udev->env_list, variable, value);
196 setenv(variable, value, 1);
203 static int import_file_into_env(struct udevice *udev, const char *filename)
208 if (file_map(filename, &buf, &bufsize) != 0) {
209 err("can't open '%s': %s", filename, strerror(errno));
212 import_keys_into_env(udev, buf, bufsize);
213 file_unmap(buf, bufsize);
218 static int import_program_into_env(struct udevice *udev, const char *program)
223 if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_INFO)) != 0)
225 return import_keys_into_env(udev, result, reslen);
228 static int import_parent_into_env(struct udevice *udev, const char *filter)
230 struct sysfs_device *dev_parent;
233 dev_parent = sysfs_device_get_parent(udev->dev);
234 if (dev_parent != NULL) {
235 struct udevice *udev_parent;
236 struct name_entry *name_loop;
238 dbg("found parent '%s', get the node name", dev_parent->devpath);
239 udev_parent = udev_device_init(NULL);
240 if (udev_parent == NULL)
242 /* import the udev_db of the parent */
243 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
244 dbg("import stored parent env '%s'", udev_parent->name);
245 list_for_each_entry(name_loop, &udev_parent->env_list, node) {
246 char name[NAME_SIZE];
249 strlcpy(name, name_loop->name, sizeof(name));
250 pos = strchr(name, '=');
254 if (fnmatch(filter, name, 0) == 0) {
255 dbg("import key '%s'", name_loop->name);
256 name_list_add(&udev->env_list, name_loop->name, 0);
257 setenv(name, pos, 1);
259 dbg("skip key '%s'", name_loop->name);
264 dbg("parent not found in database");
265 udev_device_cleanup(udev_parent);
271 #define WAIT_LOOP_PER_SECOND 50
272 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
274 char devicepath[PATH_SIZE];
275 char filepath[PATH_SIZE];
277 int loop = timeout * WAIT_LOOP_PER_SECOND;
279 strlcpy(devicepath, sysfs_path, sizeof(devicepath));
280 strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
281 strlcpy(filepath, devicepath, sizeof(filepath));
282 strlcat(filepath, "/", sizeof(filepath));
283 strlcat(filepath, file, sizeof(filepath));
285 dbg("will wait %i sec for '%s'", timeout, filepath);
288 if (stat(filepath, &stats) == 0) {
289 info("file '%s' appeared after %i loops", filepath, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
292 /* make sure the device does not have disappeared in the meantime */
293 if (stat(devicepath, &stats) != 0) {
294 info("device disappeared while waiting for '%s'", filepath);
297 info("wait for '%s' for %i mseconds", filepath, 1000 / WAIT_LOOP_PER_SECOND);
298 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
300 err("waiting for '%s' failed", filepath);
304 void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
306 char temp[PATH_SIZE];
307 char temp2[PATH_SIZE];
308 char *head, *tail, *pos, *cpos, *attr, *rest;
327 static const struct subst_map {
330 enum subst_type type;
332 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
333 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
334 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
335 { .name = "id", .fmt = 'b', .type = SUBST_ID },
336 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
337 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
338 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
339 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
340 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
341 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
342 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
343 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
344 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
347 enum subst_type type;
348 const struct subst_map *subst;
353 while (head[0] != '\0') {
354 if (head[0] == '$') {
355 /* substitute named variable */
358 if (head[1] == '$') {
359 strlcpy(temp, head+2, sizeof(temp));
360 strlcpy(head+1, temp, maxsize);
365 for (subst = map; subst->name; subst++) {
366 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
368 tail = head + strlen(subst->name)+1;
369 dbg("will substitute format name '%s'", subst->name);
374 err("unknown format variable '%s'", head);
375 } else if (head[0] == '%') {
376 /* substitute format char */
379 if (head[1] == '%') {
380 strlcpy(temp, head+2, sizeof(temp));
381 strlcpy(head+1, temp, maxsize);
387 len = get_format_len(&tail);
388 for (subst = map; subst->name; subst++) {
389 if (tail[0] == subst->fmt) {
392 dbg("will substitute format char '%c'", subst->fmt);
397 err("unknown format char '%c'", tail[0]);
403 attr = get_format_attribute(&tail);
404 strlcpy(temp, tail, sizeof(temp));
405 dbg("format=%i, string='%s', tail='%s'", type ,string, tail);
409 strlcat(string, udev->dev->devpath, maxsize);
410 dbg("substitute devpath '%s'", udev->dev->devpath);
413 strlcat(string, udev->dev->kernel, maxsize);
414 dbg("substitute kernel name '%s'", udev->dev->kernel);
416 case SUBST_KERNEL_NUMBER:
417 strlcat(string, udev->dev->kernel_number, maxsize);
418 dbg("substitute kernel number '%s'", udev->dev->kernel_number);
421 if (udev->dev_parent != NULL) {
422 strlcat(string, udev->dev_parent->kernel, maxsize);
423 dbg("substitute id '%s'", udev->dev_parent->kernel);
427 sprintf(temp2, "%d", major(udev->devt));
428 strlcat(string, temp2, maxsize);
429 dbg("substitute major number '%s'", temp2);
432 sprintf(temp2, "%d", minor(udev->devt));
433 strlcat(string, temp2, maxsize);
434 dbg("substitute minor number '%s'", temp2);
437 if (udev->program_result[0] == '\0')
439 /* get part part of the result string */
442 i = strtoul(attr, &rest, 10);
444 dbg("request part #%d of result string", i);
445 cpos = udev->program_result;
447 while (cpos[0] != '\0' && !isspace(cpos[0]))
449 while (isspace(cpos[0]))
453 err("requested part of result string not found");
456 strlcpy(temp2, cpos, sizeof(temp2));
457 /* %{2+}c copies the whole string from the second part on */
458 if (rest[0] != '+') {
459 cpos = strchr(temp2, ' ');
463 strlcat(string, temp2, maxsize);
464 dbg("substitute part of result string '%s'", temp2);
466 strlcat(string, udev->program_result, maxsize);
467 dbg("substitute result string '%s'", udev->program_result);
472 err("missing file parameter for attr");
474 const char *value = NULL;
477 /* first try the current device, other matches may have selected */
478 if (udev->dev_parent != NULL && udev->dev_parent != udev->dev)
479 value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
481 /* look at all devices along the chain of parents */
483 struct sysfs_device *dev_parent = udev->dev;
486 dbg("looking at '%s'", dev_parent->devpath);
487 value = sysfs_attr_get_value(dev_parent->devpath, attr);
489 strlcpy(temp2, value, sizeof(temp2));
492 dev_parent = sysfs_device_get_parent(dev_parent);
493 } while (dev_parent != NULL);
499 /* strip trailing whitespace and replace untrusted characters of sysfs value */
500 size = strlcpy(temp2, value, sizeof(temp2));
501 if (size >= sizeof(temp2))
502 size = sizeof(temp2)-1;
503 while (size > 0 && isspace(temp2[size-1]))
504 temp2[--size] = '\0';
505 count = replace_untrusted_chars(temp2);
507 info("%i untrusted character(s) replaced" , count);
508 strlcat(string, temp2, maxsize);
509 dbg("substitute sysfs value '%s'", temp2);
514 struct sysfs_device *dev_parent;
516 dev_parent = sysfs_device_get_parent(udev->dev);
517 if (dev_parent != NULL) {
518 struct udevice *udev_parent;
520 dbg("found parent '%s', get the node name", dev_parent->devpath);
521 udev_parent = udev_device_init(NULL);
522 if (udev_parent != NULL) {
523 /* lookup the name in the udev_db with the DEVPATH of the parent */
524 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
525 strlcat(string, udev_parent->name, maxsize);
526 dbg("substitute parent node name'%s'", udev_parent->name);
528 dbg("parent not found in database");
529 udev_device_cleanup(udev_parent);
534 case SUBST_TEMP_NODE:
535 if (udev->tmp_node[0] == '\0') {
536 dbg("create temporary device node for callout");
537 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
538 udev_root, major(udev->devt), minor(udev->devt));
539 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
540 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
542 strlcat(string, udev->tmp_node, maxsize);
543 dbg("substitute temporary device node name '%s'", udev->tmp_node);
546 strlcat(string, udev_root, maxsize);
547 dbg("substitute udev_root '%s'", udev_root);
551 dbg("missing attribute");
556 dbg("env '%s' not available", attr);
559 dbg("substitute env '%s=%s'", attr, pos);
560 strlcat(string, pos, maxsize);
563 err("unknown substitution type=%i", type);
566 /* possibly truncate to format-char specified length */
567 if (len >= 0 && len < (int)strlen(head)) {
569 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
571 strlcat(string, temp, maxsize);
575 static char *key_val(struct udev_rule *rule, struct key *key)
577 return rule->buf + key->val_off;
580 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
582 return rule->buf + pair->key_name_off;
585 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
588 char value[PATH_SIZE];
592 if (key->operation != KEY_OP_MATCH &&
593 key->operation != KEY_OP_NOMATCH)
596 strlcpy(value, rule->buf + key->val_off, sizeof(value));
599 dbg("key %s value='%s'", key_name, key_value);
601 pos = strchr(key_value, '|');
606 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
607 match = (fnmatch(key_value, val, 0) == 0);
608 if (match && (key->operation != KEY_OP_NOMATCH)) {
609 dbg("%s is true (matching value)", key_name);
612 if (!match && (key->operation == KEY_OP_NOMATCH)) {
613 dbg("%s is true (non-matching value)", key_name);
618 dbg("%s is false", key_name);
622 /* match a single rule against a given device and possibly its parent devices */
623 static int match_rule(struct udevice *udev, struct udev_rule *rule)
627 if (match_key("ACTION", rule, &rule->action, udev->action))
630 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
633 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
636 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
639 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
642 /* match NAME against a value assigned by an earlier rule */
643 if (match_key("NAME", rule, &rule->name, udev->name))
646 for (i = 0; i < rule->env.count; i++) {
647 struct key_pair *pair = &rule->env.keys[i];
649 /* we only check for matches, assignments will be handled later */
650 if (pair->key.operation == KEY_OP_MATCH ||
651 pair->key.operation == KEY_OP_NOMATCH) {
652 const char *key_name = key_pair_name(rule, pair);
653 const char *value = getenv(key_name);
656 dbg("ENV{'%s'} is not set, treat as empty", key_name);
659 if (match_key("ENV", rule, &pair->key, value))
664 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
667 found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
668 if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
672 /* check for matching sysfs attribute pairs */
673 for (i = 0; i < rule->attr.count; i++) {
674 struct key_pair *pair = &rule->attr.keys[i];
676 if (pair->key.operation == KEY_OP_MATCH ||
677 pair->key.operation == KEY_OP_NOMATCH) {
678 const char *key_name = key_pair_name(rule, pair);
679 const char *key_value = key_val(rule, &pair->key);
681 char val[VALUE_SIZE];
684 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
687 strlcpy(val, value, sizeof(val));
689 /* strip trailing whitespace of value, if not asked to match for it */
690 len = strlen(key_value);
691 if (len > 0 && !isspace(key_value[len-1])) {
693 while (len > 0 && isspace(val[len-1]))
695 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
698 if (match_key("ATTR", rule, &pair->key, val))
703 /* walk up the chain of parent devices and find a match */
704 udev->dev_parent = udev->dev;
706 /* check for matching kernel device name */
707 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
710 /* check for matching subsystem value */
711 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
714 /* check for matching driver */
715 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
718 /* check for matching sysfs attribute pairs */
719 for (i = 0; i < rule->attrs.count; i++) {
720 struct key_pair *pair = &rule->attrs.keys[i];
722 if (pair->key.operation == KEY_OP_MATCH ||
723 pair->key.operation == KEY_OP_NOMATCH) {
724 const char *key_name = key_pair_name(rule, pair);
725 const char *key_value = key_val(rule, &pair->key);
727 char val[VALUE_SIZE];
730 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
732 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
735 strlcpy(val, value, sizeof(val));
737 /* strip trailing whitespace of value, if not asked to match for it */
738 len = strlen(key_value);
739 if (len > 0 && !isspace(key_value[len-1])) {
741 while (len > 0 && isspace(val[len-1]))
743 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
746 if (match_key("ATTRS", rule, &pair->key, val))
751 /* found matching device */
754 /* move to parent device */
755 dbg("try parent sysfs device");
756 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
757 if (udev->dev_parent == NULL)
759 dbg("looking at dev_parent->devpath='%s'", udev->dev_parent->devpath);
760 dbg("looking at dev_parent->kernel='%s'", udev->dev_parent->kernel);
763 /* execute external program */
764 if (rule->program.operation != KEY_OP_UNSET) {
765 char program[PATH_SIZE];
766 char result[PATH_SIZE];
768 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
769 udev_rules_apply_format(udev, program, sizeof(program));
770 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_INFO)) != 0) {
771 dbg("PROGRAM is false");
772 udev->program_result[0] = '\0';
773 if (rule->program.operation != KEY_OP_NOMATCH)
778 dbg("PROGRAM matches");
779 remove_trailing_chars(result, '\n');
780 count = replace_untrusted_chars(result);
782 info("%i untrusted character(s) replaced" , count);
783 dbg("result is '%s'", result);
784 strlcpy(udev->program_result, result, sizeof(udev->program_result));
785 dbg("PROGRAM returned successful");
786 if (rule->program.operation == KEY_OP_NOMATCH)
789 dbg("PROGRAM key is true");
792 /* check for matching result of external program */
793 if (match_key("RESULT", rule, &rule->result, udev->program_result))
796 /* import variables returned from program or or file into environment */
797 if (rule->import.operation != KEY_OP_UNSET) {
798 char import[PATH_SIZE];
801 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
802 udev_rules_apply_format(udev, import, sizeof(import));
803 dbg("check for IMPORT import='%s'", import);
804 if (rule->import_type == IMPORT_PROGRAM) {
805 rc = import_program_into_env(udev, import);
806 } else if (rule->import_type == IMPORT_FILE) {
807 dbg("import file import='%s'", import);
808 rc = import_file_into_env(udev, import);
809 } else if (rule->import_type == IMPORT_PARENT) {
810 dbg("import parent import='%s'", import);
811 rc = import_parent_into_env(udev, import);
814 dbg("IMPORT failed");
815 if (rule->import.operation != KEY_OP_NOMATCH)
818 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
819 dbg("IMPORT key is true");
822 /* rule matches, if we have ENV assignments export it */
823 for (i = 0; i < rule->env.count; i++) {
824 struct key_pair *pair = &rule->env.keys[i];
826 if (pair->key.operation == KEY_OP_ASSIGN) {
827 char temp_value[NAME_SIZE];
828 const char *key_name = key_pair_name(rule, pair);
829 const char *value = key_val(rule, &pair->key);
832 /* make sure we don't write to the same string we possibly read from */
833 strlcpy(temp_value, value, sizeof(temp_value));
834 udev_rules_apply_format(udev, temp_value, NAME_SIZE);
836 key_value = name_list_key_add(&udev->env_list, key_name, temp_value);
837 if (key_value == NULL)
841 dbg("export ENV '%s'", key_value);
845 /* if we have ATTR assignements write value to sysfs file */
846 for (i = 0; i < rule->attr.count; i++) {
847 struct key_pair *pair = &rule->attr.keys[i];
849 if (pair->key.operation == KEY_OP_ASSIGN) {
850 const char *key_name = key_pair_name(rule, pair);
851 char attr[PATH_SIZE];
852 char value[NAME_SIZE];
855 strlcpy(attr, sysfs_path, sizeof(attr));
856 strlcat(attr, udev->dev->devpath, sizeof(attr));
857 strlcat(attr, "/", sizeof(attr));
858 strlcat(attr, key_name, sizeof(attr));
859 strlcpy(value, key_val(rule, &pair->key), sizeof(value));
860 udev_rules_apply_format(udev, value, sizeof(value));
861 info("writing '%s' to sysfs file '%s'", value, attr);
862 f = fopen(attr, "w");
864 if (fprintf(f, "%s", value) <= 0)
865 err("error writing ATTR{%s}: %s", attr, strerror(errno));
868 err("error opening ATTR{%s} for writing: %s", attr, strerror(errno));
877 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
879 struct udev_rule *rule;
882 dbg("udev->dev->devpath='%s'", udev->dev->devpath);
883 dbg("udev->dev->kernel='%s'", udev->dev->kernel);
885 /* look for a matching rule to apply */
886 udev_rules_iter_init(rules);
888 rule = udev_rules_iter_next(rules);
893 (rule->name.operation == KEY_OP_ASSIGN ||
894 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
895 rule->name.operation == KEY_OP_ADD)) {
896 dbg("node name already set, rule ignored");
901 if (match_rule(udev, rule) == 0) {
903 if (rule->ignore_device) {
904 info("rule applied, '%s' is ignored", udev->dev->kernel);
905 udev->ignore_device = 1;
908 if (rule->ignore_remove) {
909 udev->ignore_remove = 1;
910 dbg("remove event should be ignored");
912 if (rule->link_priority) {
913 udev->link_priority = rule->link_priority;
914 info("link_priority=%i", udev->link_priority);
916 /* apply all_partitions option only at a main block device */
917 if (rule->partitions &&
918 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
919 udev->partitions = rule->partitions;
920 dbg("creation of partition nodes requested");
923 /* apply permissions */
924 if (!udev->mode_final && rule->mode != 0000) {
925 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
926 udev->mode_final = 1;
927 udev->mode = rule->mode;
928 dbg("applied mode=%#o to '%s'", rule->mode, udev->dev->kernel);
930 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
931 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
932 udev->owner_final = 1;
933 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
934 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
935 dbg("applied owner='%s' to '%s'", udev->owner, udev->dev->kernel);
937 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
938 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
939 udev->group_final = 1;
940 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
941 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
942 dbg("applied group='%s' to '%s'", udev->group, udev->dev->kernel);
945 /* collect symlinks */
946 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
947 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 info("reset symlink list");
955 name_list_cleanup(&udev->symlink_list);
957 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
958 udev_rules_apply_format(udev, temp, sizeof(temp));
959 count = replace_untrusted_chars(temp);
961 info("%i untrusted character(s) replaced" , count);
962 dbg("rule applied, added symlink(s) '%s'", temp);
964 /* add multiple symlinks separated by spaces */
966 while (isspace(pos[0]))
968 next = strchr(pos, ' ');
971 info("add symlink '%s'", pos);
972 name_list_add(&udev->symlink_list, pos, 0);
973 while (isspace(next[1]))
976 next = strchr(pos, ' ');
978 if (pos[0] != '\0') {
979 info("add symlink '%s'", pos);
980 name_list_add(&udev->symlink_list, pos, 0);
984 /* set name, later rules with name set will be ignored */
985 if (rule->name.operation == KEY_OP_ASSIGN ||
986 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
987 rule->name.operation == KEY_OP_ADD) {
991 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
992 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
993 count = replace_untrusted_chars(udev->name);
995 info("%i untrusted character(s) replaced", count);
997 info("rule applied, '%s' becomes '%s'", udev->dev->kernel, udev->name);
998 if (strcmp(udev->dev->subsystem, "net") != 0)
999 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1000 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1003 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
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 info("reset run list");
1008 name_list_cleanup(&udev->run_list);
1010 dbg("add run '%s'", key_val(rule, &rule->run));
1011 name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1014 if (rule->last_rule) {
1015 dbg("last rule to be applied");
1019 if (rule->goto_label.operation != KEY_OP_UNSET) {
1020 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1021 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1027 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
1028 info("no node name set, will use kernel name '%s'", udev->name);
1031 if (udev->tmp_node[0] != '\0') {
1032 dbg("removing temporary device node");
1033 unlink_secure(udev->tmp_node);
1034 udev->tmp_node[0] = '\0';
1040 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1042 struct udev_rule *rule;
1044 dbg("udev->kernel='%s'", udev->dev->kernel);
1046 /* look for a matching rule to apply */
1047 udev_rules_iter_init(rules);
1049 rule = udev_rules_iter_next(rules);
1053 dbg("process rule");
1054 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1055 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1056 dbg("skip rule that names a device");
1060 if (match_rule(udev, rule) == 0) {
1061 if (rule->ignore_device) {
1062 info("rule applied, '%s' is ignored", udev->dev->kernel);
1063 udev->ignore_device = 1;
1067 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1068 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1069 info("reset run list");
1070 name_list_cleanup(&udev->run_list);
1072 dbg("add run '%s'", key_val(rule, &rule->run));
1073 name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1074 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1078 if (rule->last_rule) {
1079 dbg("last rule to be applied");
1083 if (rule->goto_label.operation != KEY_OP_UNSET) {
1084 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1085 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));