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.
35 #include "udev_rules.h"
38 /* extract possible {attr} and move str behind it */
39 static char *get_format_attribute(char **str)
45 pos = strchr(*str, '}');
47 err("missing closing brace for format");
53 dbg("attribute='%s', str='%s'", attr, *str);
58 /* extract possible format length and move str behind it*/
59 static int get_format_len(char **str)
64 if (isdigit(*str[0])) {
65 num = (int) strtoul(*str, &tail, 10);
68 dbg("format length=%i", num);
71 err("format parsing error '%s'", *str);
77 static int get_key(char **line, char **key, char **value)
86 if (strchr(linepos, '\\')) {
87 dbg("escaped characters are not supported, skip");
92 while (isspace(linepos[0]))
99 if (linepos[0] == '\0')
101 if (isspace(linepos[0]))
103 if (linepos[0] == '=')
111 /* skip whitespace */
112 while (isspace(linepos[0]))
116 if (linepos[0] == '"') {
118 temp = strchr(linepos, '"');
120 dbg("missing closing quote");
123 dbg("value is quoted");
125 } else if (linepos[0] == '\'') {
127 temp = strchr(linepos, '\'');
129 dbg("missing closing quote");
132 dbg("value is quoted");
134 } else if (linepos[0] == '\0') {
135 dbg("value is empty");
138 while (temp[0] && !isspace(temp[0]))
147 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
149 char line[LINE_SIZE];
158 /* loop through the whole buffer */
161 while (cur < bufsize) {
162 count = buf_get_line(buf, bufsize, cur);
167 if (count >= sizeof(line)) {
168 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
172 /* eat the whitespace */
173 while ((count > 0) && isspace(bufline[0])) {
180 /* see if this is a comment */
181 if (bufline[0] == COMMENT_CHARACTER)
184 memcpy(line, bufline, count);
188 if (get_key(&linepos, &variable, &value) == 0) {
189 dbg("import '%s=%s'", variable, value);
190 name_list_key_add(&udev->env_list, variable, value);
191 setenv(variable, value, 1);
198 static int import_file_into_env(struct udevice *udev, const char *filename)
203 if (file_map(filename, &buf, &bufsize) != 0) {
204 err("can't open '%s': %s", filename, strerror(errno));
207 import_keys_into_env(udev, buf, bufsize);
208 file_unmap(buf, bufsize);
213 static int import_program_into_env(struct udevice *udev, const char *program)
218 if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_INFO)) != 0)
220 return import_keys_into_env(udev, result, reslen);
223 static int import_parent_into_env(struct udevice *udev, const char *filter)
225 struct sysfs_device *dev_parent;
228 dev_parent = sysfs_device_get_parent(udev->dev);
229 if (dev_parent != NULL) {
230 struct udevice *udev_parent;
231 struct name_entry *name_loop;
233 dbg("found parent '%s', get the node name", dev_parent->devpath);
234 udev_parent = udev_device_init();
235 if (udev_parent == NULL)
237 /* import the udev_db of the parent */
238 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
239 dbg("import stored parent env '%s'", udev_parent->name);
240 list_for_each_entry(name_loop, &udev_parent->env_list, node) {
241 char name[NAME_SIZE];
244 strlcpy(name, name_loop->name, sizeof(name));
245 pos = strchr(name, '=');
249 if (strcmp_pattern(filter, name) == 0) {
250 dbg("import key '%s'", name_loop->name);
251 name_list_add(&udev->env_list, name_loop->name, 0);
252 setenv(name, pos, 1);
254 dbg("skip key '%s'", name_loop->name);
259 dbg("parent not found in database");
260 udev_device_cleanup(udev_parent);
266 static int match_name_and_get_number(const char *base, const char *devname)
272 baselen = strlen(base);
273 if (strncmp(base, devname, baselen) != 0)
275 if (devname[baselen] == '\0')
277 if (!isdigit(devname[baselen]))
279 num = strtoul(&devname[baselen], &endptr, 10);
280 if (endptr[0] != '\0')
285 /* finds the lowest positive device number such that <name>N isn't present in the udevdb
286 * if <name> doesn't exist, 0 is returned, N otherwise */
287 static int find_free_number(const char *base, const char *devpath)
289 char db_devpath[PATH_SIZE];
290 char filename[PATH_SIZE];
291 struct udevice *udev_db;
294 /* check if the device already owns a matching name */
295 udev_db = udev_device_init();
298 if (udev_db_get_device(udev_db, devpath) == 0) {
299 struct name_entry *name_loop;
302 devnum = match_name_and_get_number(base, udev_db->name);
305 dbg("device '%s', already has the node '%s' with num %u, use it", devpath, base, num);
308 list_for_each_entry(name_loop, &udev_db->symlink_list, node) {
309 devnum = match_name_and_get_number(base, name_loop->name);
312 dbg("device '%s', already has a symlink '%s' with num %u, use it", devpath, base, num);
318 /* just search the database again and again until a free name is found */
319 strlcpy(filename, base, sizeof(filename));
321 dbg("look for existing node '%s'", filename);
322 if (udev_db_lookup_name(filename, db_devpath, sizeof(db_devpath)) != 0) {
323 dbg("free num=%d", num);
329 err("find_free_number aborted at num=%d", num);
333 snprintf(filename, sizeof(filename), "%s%d", base, num);
334 filename[sizeof(filename)-1] = '\0';
338 udev_device_cleanup(udev_db);
342 #define WAIT_LOOP_PER_SECOND 50
343 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
345 char filename[PATH_SIZE];
347 int loop = timeout * WAIT_LOOP_PER_SECOND;
349 snprintf(filename, sizeof(filename), "%s%s/%s", sysfs_path, udev->dev->devpath, file);
350 filename[sizeof(filename)-1] = '\0';
351 dbg("wait %i sec for '%s'", timeout, filename);
354 if (stat(filename, &stats) == 0) {
355 info("file '%s' appeared after %i loops", filename, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
358 info("wait for %i mseconds", 1000 / WAIT_LOOP_PER_SECOND);
359 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
361 err("waiting for '%s' failed", filename);
365 void apply_format(struct udevice *udev, char *string, size_t maxsize)
367 char temp[PATH_SIZE];
368 char temp2[PATH_SIZE];
369 char *head, *tail, *pos, *cpos, *attr, *rest;
373 unsigned int next_free_number;
391 static const struct subst_map {
394 enum subst_type type;
396 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
397 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
398 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL_NAME },
399 { .name = "id", .fmt = 'b', .type = SUBST_ID },
400 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
401 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
402 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
403 { .name = "sysfs", .fmt = 's', .type = SUBST_SYSFS },
404 { .name = "enum", .fmt = 'e', .type = SUBST_ENUM },
405 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
406 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
407 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
408 { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
409 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
412 enum subst_type type;
413 const struct subst_map *subst;
418 while (head[0] != '\0') {
419 if (head[0] == '$') {
420 /* substitute named variable */
423 if (head[1] == '$') {
424 strlcpy(temp, head+2, sizeof(temp));
425 strlcpy(head+1, temp, maxsize);
430 for (subst = map; subst->name; subst++) {
431 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
433 tail = head + strlen(subst->name)+1;
434 dbg("will substitute format name '%s'", subst->name);
439 else if (head[0] == '%') {
440 /* substitute format char */
443 if (head[1] == '%') {
444 strlcpy(temp, head+2, sizeof(temp));
445 strlcpy(head+1, temp, maxsize);
451 len = get_format_len(&tail);
452 for (subst = map; subst->name; subst++) {
453 if (tail[0] == subst->fmt) {
456 dbg("will substitute format char '%c'", subst->fmt);
465 attr = get_format_attribute(&tail);
466 strlcpy(temp, tail, sizeof(temp));
467 dbg("format=%i, string='%s', tail='%s'", type ,string, tail);
471 strlcat(string, udev->dev->devpath, maxsize);
472 dbg("substitute devpath '%s'", udev->dev->devpath);
474 case SUBST_KERNEL_NAME:
475 strlcat(string, udev->dev->kernel_name, maxsize);
476 dbg("substitute kernel name '%s'", udev->dev->kernel_name);
478 case SUBST_KERNEL_NUMBER:
479 strlcat(string, udev->dev->kernel_number, maxsize);
480 dbg("substitute kernel number '%s'", udev->dev->kernel_number);
483 if (udev->dev_parent != NULL) {
484 strlcat(string, udev->dev_parent->kernel_name, maxsize);
485 dbg("substitute id '%s'", udev->dev_parent->kernel_name);
489 sprintf(temp2, "%d", major(udev->devt));
490 strlcat(string, temp2, maxsize);
491 dbg("substitute major number '%s'", temp2);
494 sprintf(temp2, "%d", minor(udev->devt));
495 strlcat(string, temp2, maxsize);
496 dbg("substitute minor number '%s'", temp2);
499 if (udev->program_result[0] == '\0')
501 /* get part part of the result string */
504 i = strtoul(attr, &rest, 10);
506 dbg("request part #%d of result string", i);
507 cpos = udev->program_result;
509 while (cpos[0] != '\0' && !isspace(cpos[0]))
511 while (isspace(cpos[0]))
515 err("requested part of result string not found");
518 strlcpy(temp2, cpos, sizeof(temp2));
519 /* %{2+}c copies the whole string from the second part on */
520 if (rest[0] != '+') {
521 cpos = strchr(temp2, ' ');
525 strlcat(string, temp2, maxsize);
526 dbg("substitute part of result string '%s'", temp2);
528 strlcat(string, udev->program_result, maxsize);
529 dbg("substitute result string '%s'", udev->program_result);
534 dbg("missing attribute");
537 struct sysfs_device *dev_parent;
540 dev_parent = udev->dev;
542 dbg("looking at '%s'", dev_parent->devpath);
543 value = sysfs_attr_get_value(dev_parent->devpath, attr);
545 strlcpy(temp2, value, sizeof(temp2));
548 dev_parent = sysfs_device_get_parent(dev_parent);
549 } while (dev_parent != NULL);
551 /* strip trailing whitespace of sysfs value */
553 while (i > 0 && isspace(temp2[i-1]))
555 count = replace_untrusted_chars(temp2);
557 info("%i untrusted character(s) replaced" , count);
558 strlcat(string, temp2, maxsize);
559 dbg("substitute sysfs value '%s'", temp2);
563 next_free_number = find_free_number(string, udev->dev->devpath);
564 if (next_free_number > 0) {
565 sprintf(temp2, "%d", next_free_number);
566 strlcat(string, temp2, maxsize);
571 struct sysfs_device *dev_parent;
573 dev_parent = sysfs_device_get_parent(udev->dev);
574 if (dev_parent != NULL) {
575 struct udevice *udev_parent;
577 dbg("found parent '%s', get the node name", dev_parent->devpath);
578 udev_parent = udev_device_init();
579 if (udev_parent != NULL) {
580 /* lookup the name in the udev_db with the DEVPATH of the parent */
581 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
582 strlcat(string, udev_parent->name, maxsize);
583 dbg("substitute parent node name'%s'", udev_parent->name);
585 dbg("parent not found in database");
586 udev_device_cleanup(udev_parent);
591 case SUBST_TEMP_NODE:
592 if (udev->tmp_node[0] == '\0') {
593 dbg("create temporary device node for callout");
594 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
595 udev_root, major(udev->devt), minor(udev->devt));
596 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
597 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
599 strlcat(string, udev->tmp_node, maxsize);
600 dbg("substitute temporary device node name '%s'", udev->tmp_node);
603 strlcat(string, udev_root, maxsize);
604 dbg("substitute udev_root '%s'", udev_root);
610 value = sysfs_attr_get_value(udev->dev->devpath, "modalias");
612 strlcat(string, value, maxsize);
613 dbg("substitute MODALIAS '%s'", temp2);
619 dbg("missing attribute");
624 dbg("env '%s' not available", attr);
627 dbg("substitute env '%s=%s'", attr, pos);
628 strlcat(string, pos, maxsize);
631 err("unknown substitution type=%i", type);
634 /* possibly truncate to format-char specified length */
637 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
639 strlcat(string, temp, maxsize);
643 static char *key_val(struct udev_rule *rule, struct key *key)
645 return rule->buf + key->val_off;
648 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
650 return rule->buf + pair->key_name_off;
653 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
656 char value[PATH_SIZE];
660 if (key->operation == KEY_OP_UNSET)
663 strlcpy(value, rule->buf + key->val_off, sizeof(value));
666 dbg("key %s value='%s'", key_name, key_value);
668 pos = strchr(key_value, '|');
673 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
674 match = (strcmp_pattern(key_value, val) == 0);
675 if (match && (key->operation != KEY_OP_NOMATCH)) {
676 dbg("%s is true (matching value)", key_name);
679 if (!match && (key->operation == KEY_OP_NOMATCH)) {
680 dbg("%s is true (non-matching value)", key_name);
685 dbg("%s is false", key_name);
689 /* match a single rule against a given device and possibly its parent devices */
690 static int match_rule(struct udevice *udev, struct udev_rule *rule)
694 if (match_key("ACTION", rule, &rule->action, udev->action))
697 if (match_key("KERNEL", rule, &rule->kernel_name, udev->dev->kernel_name))
700 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
703 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
706 if (rule->modalias.operation != KEY_OP_UNSET) {
709 value = sysfs_attr_get_value(udev->dev->devpath, "modalias");
711 dbg("MODALIAS value not found");
714 if (match_key("MODALIAS", rule, &rule->modalias, value))
718 for (i = 0; i < rule->env.count; i++) {
719 struct key_pair *pair = &rule->env.keys[i];
721 /* we only check for matches, assignments will be handled later */
722 if (pair->key.operation != KEY_OP_ASSIGN) {
723 const char *key_name = key_pair_name(rule, pair);
724 const char *value = getenv(key_name);
727 dbg("ENV{'%s'} is not set, treat as empty", key_name);
730 if (match_key("ENV", rule, &pair->key, value))
735 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
738 match = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
739 if (match && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH)) {
740 dbg("WAIT_FOR_SYSFS is true (matching value)");
743 if (!match && (rule->wait_for_sysfs.operation == KEY_OP_NOMATCH)) {
744 dbg("WAIT_FOR_SYSFS is true, (non matching value)");
747 dbg("WAIT_FOR_SYSFS is false");
751 /* walk up the chain of parent devices and find a match */
752 udev->dev_parent = udev->dev;
754 /* check for matching driver */
755 if (rule->driver.operation != KEY_OP_UNSET) {
756 if (match_key("DRIVER", rule, &rule->driver, udev->dev_parent->driver))
760 /* check for matching subsystem/bus value */
761 if (rule->bus.operation != KEY_OP_UNSET) {
762 if (match_key("BUS", rule, &rule->bus, udev->dev_parent->subsystem))
766 /* check for matching bus id (device name) */
767 if (rule->id.operation != KEY_OP_UNSET) {
768 if (match_key("ID", rule, &rule->id, udev->dev_parent->kernel_name))
772 /* check for matching sysfs pairs */
773 if (rule->sysfs.count) {
774 dbg("check %i SYSFS keys", rule->sysfs.count);
775 for (i = 0; i < rule->sysfs.count; i++) {
776 struct key_pair *pair = &rule->sysfs.keys[i];
777 const char *key_name = key_pair_name(rule, pair);
778 const char *key_value = key_val(rule, &pair->key);
780 char val[VALUE_SIZE];
783 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
786 strlcpy(val, value, sizeof(val));
788 /* strip trailing whitespace of value, if not asked to match for it */
789 len = strlen(key_value);
790 if (len > 0 && !isspace(key_value[len-1])) {
792 while (len > 0 && isspace(val[len-1]))
794 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
797 if (match_key("SYSFS", rule, &pair->key, val))
800 dbg("all %i SYSFS keys matched", rule->sysfs.count);
803 /* found matching device */
806 /* move to parent device */
807 dbg("try parent sysfs device");
808 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
809 if (udev->dev_parent == NULL)
811 dbg("looking at dev_parent->devpath='%s'", udev->dev_parent->devpath);
812 dbg("looking at dev_parent->bus_kernel_name='%s'", udev->dev_parent->kernel_name);
815 /* execute external program */
816 if (rule->program.operation != KEY_OP_UNSET) {
817 char program[PATH_SIZE];
818 char result[PATH_SIZE];
820 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
821 apply_format(udev, program, sizeof(program));
822 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_INFO)) != 0) {
823 dbg("PROGRAM is false");
824 udev->program_result[0] = '\0';
825 if (rule->program.operation != KEY_OP_NOMATCH)
830 dbg("PROGRAM matches");
831 remove_trailing_chars(result, '\n');
832 count = replace_untrusted_chars(result);
834 info("%i untrusted character(s) replaced" , count);
835 dbg("result is '%s'", result);
836 strlcpy(udev->program_result, result, sizeof(udev->program_result));
837 dbg("PROGRAM returned successful");
838 if (rule->program.operation == KEY_OP_NOMATCH)
841 dbg("PROGRAM key is true");
844 /* check for matching result of external program */
845 if (match_key("RESULT", rule, &rule->result, udev->program_result))
848 /* import variables returned from program or or file into environment */
849 if (rule->import.operation != KEY_OP_UNSET) {
850 char import[PATH_SIZE];
853 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
854 apply_format(udev, import, sizeof(import));
855 dbg("check for IMPORT import='%s'", import);
856 if (rule->import_type == IMPORT_PROGRAM) {
857 rc = import_program_into_env(udev, import);
858 } else if (rule->import_type == IMPORT_FILE) {
859 dbg("import file import='%s'", import);
860 rc = import_file_into_env(udev, import);
861 } else if (rule->import_type == IMPORT_PARENT) {
862 dbg("import parent import='%s'", import);
863 rc = import_parent_into_env(udev, import);
866 dbg("IMPORT failed");
867 if (rule->import.operation != KEY_OP_NOMATCH)
870 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
871 dbg("IMPORT key is true");
874 /* rule matches, if we have ENV assignments export it */
875 for (i = 0; i < rule->env.count; i++) {
876 struct key_pair *pair = &rule->env.keys[i];
878 if (pair->key.operation == KEY_OP_ASSIGN) {
879 const char *key_name = key_pair_name(rule, pair);
880 const char *value = key_val(rule, &pair->key);
882 name_list_key_add(&udev->env_list, key_name, value);
883 setenv(key_name, value, 1);
884 dbg("export ENV '%s=%s'", key_name, value);
894 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
896 struct udev_rule *rule;
899 dbg("udev->dev->devpath='%s'", udev->dev->devpath);
900 dbg("udev->dev->kernel_name='%s'", udev->dev->kernel_name);
902 /* look for a matching rule to apply */
903 udev_rules_iter_init(rules);
905 rule = udev_rules_iter_next(rules);
909 if (name_set && rule->name.operation != KEY_OP_UNSET) {
910 dbg("node name already set, rule ignored");
915 if (match_rule(udev, rule) == 0) {
917 if (rule->ignore_device) {
918 info("rule applied, '%s' is ignored", udev->dev->kernel_name);
919 udev->ignore_device = 1;
922 if (rule->ignore_remove) {
923 udev->ignore_remove = 1;
924 dbg("remove event should be ignored");
926 /* apply all_partitions option only at a main block device */
927 if (rule->partitions &&
928 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
929 udev->partitions = rule->partitions;
930 dbg("creation of partition nodes requested");
933 /* apply permissions */
934 if (!udev->mode_final && rule->mode != 0000) {
935 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
936 udev->mode_final = 1;
937 udev->mode = rule->mode;
938 dbg("applied mode=%#o to '%s'", rule->mode, udev->dev->kernel_name);
940 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
941 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
942 udev->owner_final = 1;
943 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
944 apply_format(udev, udev->owner, sizeof(udev->owner));
945 dbg("applied owner='%s' to '%s'", udev->owner, udev->dev->kernel_name);
947 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
948 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
949 udev->group_final = 1;
950 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
951 apply_format(udev, udev->group, sizeof(udev->group));
952 dbg("applied group='%s' to '%s'", udev->group, udev->dev->kernel_name);
955 /* collect symlinks */
956 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
957 char temp[PATH_SIZE];
961 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
962 udev->symlink_final = 1;
963 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
964 info("reset symlink list");
965 name_list_cleanup(&udev->symlink_list);
967 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
968 apply_format(udev, temp, sizeof(temp));
969 count = replace_untrusted_chars(temp);
971 info("%i untrusted character(s) replaced" , count);
972 dbg("rule applied, added symlink(s) '%s'", temp);
974 /* add multiple symlinks separated by spaces */
976 while (isspace(pos[0]))
978 next = strchr(pos, ' ');
981 info("add symlink '%s'", pos);
982 name_list_add(&udev->symlink_list, pos, 0);
983 while (isspace(next[1]))
986 next = strchr(pos, ' ');
988 if (pos[0] != '\0') {
989 info("add symlink '%s'", pos);
990 name_list_add(&udev->symlink_list, pos, 0);
994 /* set name, later rules with name set will be ignored */
995 if (rule->name.operation != KEY_OP_UNSET) {
998 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
999 apply_format(udev, udev->name, sizeof(udev->name));
1000 count = replace_untrusted_chars(udev->name);
1002 info("%i untrusted character(s) replaced", count);
1004 info("rule applied, '%s' becomes '%s'", udev->dev->kernel_name, udev->name);
1005 if (strcmp(udev->dev->subsystem, "net") != 0)
1006 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1007 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1010 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1011 char program[PATH_SIZE];
1013 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1014 udev->run_final = 1;
1015 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1016 info("reset run list");
1017 name_list_cleanup(&udev->run_list);
1019 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1020 dbg("add run '%s'", program);
1021 name_list_add(&udev->run_list, program, 0);
1024 if (rule->last_rule) {
1025 dbg("last rule to be applied");
1029 if (rule->goto_label.operation != KEY_OP_UNSET) {
1030 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1031 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1037 strlcpy(udev->name, udev->dev->kernel_name, sizeof(udev->name));
1038 info("no node name set, will use kernel name '%s'", udev->name);
1041 if (udev->tmp_node[0] != '\0') {
1042 dbg("removing temporary device node");
1043 unlink_secure(udev->tmp_node);
1044 udev->tmp_node[0] = '\0';
1050 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
1052 struct udev_rule *rule;
1054 dbg("udev->kernel_name='%s'", udev->dev->kernel_name);
1056 /* look for a matching rule to apply */
1057 udev_rules_iter_init(rules);
1059 rule = udev_rules_iter_next(rules);
1063 dbg("process rule");
1064 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1065 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1066 dbg("skip rule that names a device");
1070 if (match_rule(udev, rule) == 0) {
1071 if (rule->ignore_device) {
1072 info("rule applied, '%s' is ignored", udev->dev->kernel_name);
1073 udev->ignore_device = 1;
1077 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1078 char program[PATH_SIZE];
1080 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1081 info("reset run list");
1082 name_list_cleanup(&udev->run_list);
1084 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1085 apply_format(udev, program, sizeof(program));
1086 dbg("add run '%s'", program);
1087 name_list_add(&udev->run_list, program, 0);
1088 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1092 if (rule->last_rule) {
1093 dbg("last rule to be applied");
1097 if (rule->goto_label.operation != KEY_OP_UNSET) {
1098 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1099 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));