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)
86 while (isspace(linepos[0]))
90 temp = strchr(linepos, '=');
91 if (temp == NULL || temp == linepos)
97 /* get a quoted value */
98 if (linepos[0] == '"' || linepos[0] == '\'') {
99 temp = strchr(&linepos[1], linepos[0]);
102 *value = &linepos[1];
108 temp = strchr(linepos, '\n');
116 static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
118 char line[LINE_SIZE];
127 /* loop through the whole buffer */
130 while (cur < bufsize) {
131 count = buf_get_line(buf, bufsize, cur);
136 if (count >= sizeof(line)) {
137 err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
141 /* eat the whitespace */
142 while ((count > 0) && isspace(bufline[0])) {
149 /* see if this is a comment */
150 if (bufline[0] == COMMENT_CHARACTER)
153 memcpy(line, bufline, count);
157 if (get_key(&linepos, &variable, &value) == 0) {
158 dbg("import '%s=%s'", variable, value);
160 /* handle device, renamed by external tool, returning new path */
161 if (strcmp(variable, "DEVPATH") == 0) {
162 info("updating devpath from '%s' to '%s'", udev->dev->devpath, value);
163 sysfs_device_set_values(udev->dev, value, NULL, NULL);
165 name_list_key_add(&udev->env_list, variable, value);
166 setenv(variable, value, 1);
173 static int import_file_into_env(struct udevice *udev, const char *filename)
178 if (file_map(filename, &buf, &bufsize) != 0) {
179 err("can't open '%s': %s", filename, strerror(errno));
182 import_keys_into_env(udev, buf, bufsize);
183 file_unmap(buf, bufsize);
188 static int import_program_into_env(struct udevice *udev, const char *program)
193 if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_INFO)) != 0)
195 return import_keys_into_env(udev, result, reslen);
198 static int import_parent_into_env(struct udevice *udev, const char *filter)
200 struct sysfs_device *dev_parent;
203 dev_parent = sysfs_device_get_parent(udev->dev);
204 if (dev_parent != NULL) {
205 struct udevice *udev_parent;
206 struct name_entry *name_loop;
208 dbg("found parent '%s', get the node name", dev_parent->devpath);
209 udev_parent = udev_device_init(NULL);
210 if (udev_parent == NULL)
212 /* import the udev_db of the parent */
213 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
214 dbg("import stored parent env '%s'", udev_parent->name);
215 list_for_each_entry(name_loop, &udev_parent->env_list, node) {
216 char name[NAME_SIZE];
219 strlcpy(name, name_loop->name, sizeof(name));
220 pos = strchr(name, '=');
224 if (fnmatch(filter, name, 0) == 0) {
225 dbg("import key '%s'", name_loop->name);
226 name_list_add(&udev->env_list, name_loop->name, 0);
227 setenv(name, pos, 1);
229 dbg("skip key '%s'", name_loop->name);
234 dbg("parent not found in database");
235 udev_device_cleanup(udev_parent);
241 #define WAIT_LOOP_PER_SECOND 50
242 static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
244 char devicepath[PATH_SIZE];
245 char filepath[PATH_SIZE];
247 int loop = timeout * WAIT_LOOP_PER_SECOND;
249 strlcpy(devicepath, sysfs_path, sizeof(devicepath));
250 strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
251 strlcpy(filepath, devicepath, sizeof(filepath));
252 strlcat(filepath, "/", sizeof(filepath));
253 strlcat(filepath, file, sizeof(filepath));
255 dbg("will wait %i sec for '%s'", timeout, filepath);
258 if (stat(filepath, &stats) == 0) {
259 info("file '%s' appeared after %i loops", filepath, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
262 /* make sure, the device did not disappear in the meantime */
263 if (stat(devicepath, &stats) != 0) {
264 info("device disappeared while waiting for '%s'", filepath);
267 info("wait for '%s' for %i mseconds", filepath, 1000 / WAIT_LOOP_PER_SECOND);
268 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
270 err("waiting for '%s' failed", filepath);
274 void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
276 char temp[PATH_SIZE];
277 char temp2[PATH_SIZE];
278 char *head, *tail, *pos, *cpos, *attr, *rest;
298 static const struct subst_map {
301 enum subst_type type;
303 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
304 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
305 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
306 { .name = "id", .fmt = 'b', .type = SUBST_ID },
307 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
308 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
309 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
310 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
311 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
312 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
313 { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
314 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
315 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
316 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
319 enum subst_type type;
320 const struct subst_map *subst;
325 while (head[0] != '\0') {
326 if (head[0] == '$') {
327 /* substitute named variable */
330 if (head[1] == '$') {
331 strlcpy(temp, head+2, sizeof(temp));
332 strlcpy(head+1, temp, maxsize);
337 for (subst = map; subst->name; subst++) {
338 if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
340 tail = head + strlen(subst->name)+1;
341 dbg("will substitute format name '%s'", subst->name);
346 err("unknown format variable '%s'", head);
347 } else if (head[0] == '%') {
348 /* substitute format char */
351 if (head[1] == '%') {
352 strlcpy(temp, head+2, sizeof(temp));
353 strlcpy(head+1, temp, maxsize);
359 len = get_format_len(&tail);
360 for (subst = map; subst->name; subst++) {
361 if (tail[0] == subst->fmt) {
364 dbg("will substitute format char '%c'", subst->fmt);
369 err("unknown format char '%c'", tail[0]);
375 attr = get_format_attribute(&tail);
376 strlcpy(temp, tail, sizeof(temp));
377 dbg("format=%i, string='%s', tail='%s'", type ,string, tail);
381 strlcat(string, udev->dev->devpath, maxsize);
382 dbg("substitute devpath '%s'", udev->dev->devpath);
385 strlcat(string, udev->dev->kernel, maxsize);
386 dbg("substitute kernel name '%s'", udev->dev->kernel);
388 case SUBST_KERNEL_NUMBER:
389 strlcat(string, udev->dev->kernel_number, maxsize);
390 dbg("substitute kernel number '%s'", udev->dev->kernel_number);
393 if (udev->dev_parent != NULL) {
394 strlcat(string, udev->dev_parent->kernel, maxsize);
395 dbg("substitute id '%s'", udev->dev_parent->kernel);
399 sprintf(temp2, "%d", major(udev->devt));
400 strlcat(string, temp2, maxsize);
401 dbg("substitute major number '%s'", temp2);
404 sprintf(temp2, "%d", minor(udev->devt));
405 strlcat(string, temp2, maxsize);
406 dbg("substitute minor number '%s'", temp2);
409 if (udev->program_result[0] == '\0')
411 /* get part part of the result string */
414 i = strtoul(attr, &rest, 10);
416 dbg("request part #%d of result string", i);
417 cpos = udev->program_result;
419 while (cpos[0] != '\0' && !isspace(cpos[0]))
421 while (isspace(cpos[0]))
425 err("requested part of result string not found");
428 strlcpy(temp2, cpos, sizeof(temp2));
429 /* %{2+}c copies the whole string from the second part on */
430 if (rest[0] != '+') {
431 cpos = strchr(temp2, ' ');
435 strlcat(string, temp2, maxsize);
436 dbg("substitute part of result string '%s'", temp2);
438 strlcat(string, udev->program_result, maxsize);
439 dbg("substitute result string '%s'", udev->program_result);
444 err("missing file parameter for attr");
446 const char *value = NULL;
449 /* first try the current device, other matches may have selected */
450 if (udev->dev_parent != NULL && udev->dev_parent != udev->dev)
451 value = sysfs_attr_get_value(udev->dev_parent->devpath, attr);
453 /* look at all devices along the chain of parents */
455 struct sysfs_device *dev_parent = udev->dev;
458 dbg("looking at '%s'", dev_parent->devpath);
459 value = sysfs_attr_get_value(dev_parent->devpath, attr);
461 strlcpy(temp2, value, sizeof(temp2));
464 dev_parent = sysfs_device_get_parent(dev_parent);
465 } while (dev_parent != NULL);
471 /* strip trailing whitespace, and replace unwanted characters */
472 size = strlcpy(temp2, value, sizeof(temp2));
473 if (size >= sizeof(temp2))
474 size = sizeof(temp2)-1;
475 while (size > 0 && isspace(temp2[size-1]))
476 temp2[--size] = '\0';
477 count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
479 info("%i character(s) replaced" , count);
480 strlcat(string, temp2, maxsize);
481 dbg("substitute sysfs value '%s'", temp2);
486 struct sysfs_device *dev_parent;
488 dev_parent = sysfs_device_get_parent(udev->dev);
489 if (dev_parent != NULL) {
490 struct udevice *udev_parent;
492 dbg("found parent '%s', get the node name", dev_parent->devpath);
493 udev_parent = udev_device_init(NULL);
494 if (udev_parent != NULL) {
495 /* lookup the name in the udev_db with the DEVPATH of the parent */
496 if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
497 strlcat(string, udev_parent->name, maxsize);
498 dbg("substitute parent node name'%s'", udev_parent->name);
500 dbg("parent not found in database");
501 udev_device_cleanup(udev_parent);
506 case SUBST_TEMP_NODE:
507 if (udev->tmp_node[0] == '\0' && major(udev->devt) > 0) {
508 dbg("create temporary device node for callout");
509 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
510 udev_root, major(udev->devt), minor(udev->devt));
511 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
512 udev_node_mknod(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
514 strlcat(string, udev->tmp_node, maxsize);
515 dbg("substitute temporary device node name '%s'", udev->tmp_node);
518 strlcat(string, udev_root, maxsize);
519 dbg("substitute udev_root '%s'", udev_root);
522 strlcat(string, sysfs_path, maxsize);
523 dbg("substitute sysfs_path '%s'", sysfs_path);
527 dbg("missing attribute");
532 dbg("env '%s' not available", attr);
535 dbg("substitute env '%s=%s'", attr, pos);
536 strlcat(string, pos, maxsize);
539 err("unknown substitution type=%i", type);
542 /* possibly truncate to format-char specified length */
543 if (len >= 0 && len < (int)strlen(head)) {
545 dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
547 strlcat(string, temp, maxsize);
551 static char *key_val(struct udev_rule *rule, struct key *key)
553 return rule->buf + key->val_off;
556 static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
558 return rule->buf + pair->key_name_off;
561 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
563 char value[PATH_SIZE];
568 if (key->operation != KEY_OP_MATCH &&
569 key->operation != KEY_OP_NOMATCH)
572 /* look for a matching string, parts are separated by '|' */
573 strlcpy(value, rule->buf + key->val_off, sizeof(value));
575 dbg("key %s value='%s'", key_name, key_value);
577 pos = strchr(key_value, '|');
583 dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
584 match = (fnmatch(key_value, val, 0) == 0);
591 if (match && (key->operation == KEY_OP_MATCH)) {
592 dbg("%s is true (matching value)", key_name);
595 if (!match && (key->operation == KEY_OP_NOMATCH)) {
596 dbg("%s is true (non-matching value)", key_name);
602 /* match a single rule against a given device and possibly its parent devices */
603 static int match_rule(struct udevice *udev, struct udev_rule *rule)
607 if (match_key("ACTION", rule, &rule->action, udev->action))
610 if (match_key("KERNEL", rule, &rule->kernel, udev->dev->kernel))
613 if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->dev->subsystem))
616 if (match_key("DEVPATH", rule, &rule->devpath, udev->dev->devpath))
619 if (match_key("DRIVER", rule, &rule->driver, udev->dev->driver))
622 /* match NAME against a value assigned by an earlier rule */
623 if (match_key("NAME", rule, &rule->name, udev->name))
626 for (i = 0; i < rule->env.count; i++) {
627 struct key_pair *pair = &rule->env.keys[i];
629 /* we only check for matches, assignments will be handled later */
630 if (pair->key.operation == KEY_OP_MATCH ||
631 pair->key.operation == KEY_OP_NOMATCH) {
632 const char *key_name = key_pair_name(rule, pair);
633 const char *value = getenv(key_name);
636 dbg("ENV{'%s'} is not set, treat as empty", key_name);
639 if (match_key("ENV", rule, &pair->key, value))
644 if (rule->test.operation != KEY_OP_UNSET) {
645 char filename[PATH_SIZE];
649 strlcpy(filename, key_val(rule, &rule->test), sizeof(filename));
650 udev_rules_apply_format(udev, filename, sizeof(filename));
652 match = (stat(filename, &statbuf) == 0);
653 info("'%s' %s", filename, match ? "exists" : "does not exist");
654 if (match && rule->test_mode_mask > 0) {
655 match = ((statbuf.st_mode & rule->test_mode_mask) > 0);
656 info("'%s' has mode=%#o and %s %#o", filename, statbuf.st_mode,
657 match ? "matches" : "does not match",
658 rule->test_mode_mask);
660 if (match && rule->test.operation == KEY_OP_NOMATCH)
662 if (!match && rule->test.operation == KEY_OP_MATCH)
664 dbg("TEST key is true");
667 if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
670 found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
671 if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
675 /* check for matching sysfs attribute pairs */
676 for (i = 0; i < rule->attr.count; i++) {
677 struct key_pair *pair = &rule->attr.keys[i];
679 if (pair->key.operation == KEY_OP_MATCH ||
680 pair->key.operation == KEY_OP_NOMATCH) {
681 const char *key_name = key_pair_name(rule, pair);
682 const char *key_value = key_val(rule, &pair->key);
684 char val[VALUE_SIZE];
687 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
690 strlcpy(val, value, sizeof(val));
692 /* strip trailing whitespace of value, if not asked to match for it */
693 len = strlen(key_value);
694 if (len > 0 && !isspace(key_value[len-1])) {
696 while (len > 0 && isspace(val[len-1]))
698 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
701 if (match_key("ATTR", rule, &pair->key, val))
706 /* walk up the chain of parent devices and find a match */
707 udev->dev_parent = udev->dev;
709 /* check for matching kernel device name */
710 if (match_key("KERNELS", rule, &rule->kernels, udev->dev_parent->kernel))
713 /* check for matching subsystem value */
714 if (match_key("SUBSYSTEMS", rule, &rule->subsystems, udev->dev_parent->subsystem))
717 /* check for matching driver */
718 if (match_key("DRIVERS", rule, &rule->drivers, udev->dev_parent->driver))
721 /* check for matching sysfs attribute pairs */
722 for (i = 0; i < rule->attrs.count; i++) {
723 struct key_pair *pair = &rule->attrs.keys[i];
725 if (pair->key.operation == KEY_OP_MATCH ||
726 pair->key.operation == KEY_OP_NOMATCH) {
727 const char *key_name = key_pair_name(rule, pair);
728 const char *key_value = key_val(rule, &pair->key);
730 char val[VALUE_SIZE];
733 value = sysfs_attr_get_value(udev->dev_parent->devpath, key_name);
735 value = sysfs_attr_get_value(udev->dev->devpath, key_name);
738 strlcpy(val, value, sizeof(val));
740 /* strip trailing whitespace of value, if not asked to match for it */
741 len = strlen(key_value);
742 if (len > 0 && !isspace(key_value[len-1])) {
744 while (len > 0 && isspace(val[len-1]))
746 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val)-len, val);
749 if (match_key("ATTRS", rule, &pair->key, val))
754 /* found matching device */
757 /* move to parent device */
758 dbg("try parent sysfs device");
759 udev->dev_parent = sysfs_device_get_parent(udev->dev_parent);
760 if (udev->dev_parent == NULL)
762 dbg("looking at dev_parent->devpath='%s'", udev->dev_parent->devpath);
763 dbg("looking at dev_parent->kernel='%s'", udev->dev_parent->kernel);
766 /* execute external program */
767 if (rule->program.operation != KEY_OP_UNSET) {
768 char program[PATH_SIZE];
769 char result[PATH_SIZE];
771 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
772 udev_rules_apply_format(udev, program, sizeof(program));
773 if (run_program(program, udev->dev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_INFO)) != 0) {
774 dbg("PROGRAM is false");
775 udev->program_result[0] = '\0';
776 if (rule->program.operation != KEY_OP_NOMATCH)
781 dbg("PROGRAM matches");
782 remove_trailing_chars(result, '\n');
783 count = replace_chars(result, ALLOWED_CHARS_INPUT);
785 info("%i character(s) replaced" , count);
786 dbg("result is '%s'", result);
787 strlcpy(udev->program_result, result, sizeof(udev->program_result));
788 dbg("PROGRAM returned successful");
789 if (rule->program.operation == KEY_OP_NOMATCH)
792 dbg("PROGRAM key is true");
795 /* check for matching result of external program */
796 if (match_key("RESULT", rule, &rule->result, udev->program_result))
799 /* import variables returned from program or or file into environment */
800 if (rule->import.operation != KEY_OP_UNSET) {
801 char import[PATH_SIZE];
804 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
805 udev_rules_apply_format(udev, import, sizeof(import));
806 dbg("check for IMPORT import='%s'", import);
807 if (rule->import_type == IMPORT_PROGRAM) {
808 rc = import_program_into_env(udev, import);
809 } else if (rule->import_type == IMPORT_FILE) {
810 dbg("import file import='%s'", import);
811 rc = import_file_into_env(udev, import);
812 } else if (rule->import_type == IMPORT_PARENT) {
813 dbg("import parent import='%s'", import);
814 rc = import_parent_into_env(udev, import);
817 dbg("IMPORT failed");
818 if (rule->import.operation != KEY_OP_NOMATCH)
821 dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
822 dbg("IMPORT key is true");
825 /* rule matches, if we have ENV assignments export it */
826 for (i = 0; i < rule->env.count; i++) {
827 struct key_pair *pair = &rule->env.keys[i];
829 if (pair->key.operation == KEY_OP_ASSIGN) {
830 char temp_value[NAME_SIZE];
831 const char *key_name = key_pair_name(rule, pair);
832 const char *value = key_val(rule, &pair->key);
834 /* make sure we don't write to the same string we possibly read from */
835 strlcpy(temp_value, value, sizeof(temp_value));
836 udev_rules_apply_format(udev, temp_value, NAME_SIZE);
838 if (temp_value[0] == '\0') {
839 name_list_key_remove(&udev->env_list, key_name);
841 info("unset ENV '%s'", key_name);
843 char *key_value = name_list_key_add(&udev->env_list, key_name, temp_value);
845 if (key_value == NULL)
848 info("set ENV '%s'", key_value);
853 /* if we have ATTR assignments, write value to sysfs file */
854 for (i = 0; i < rule->attr.count; i++) {
855 struct key_pair *pair = &rule->attr.keys[i];
857 if (pair->key.operation == KEY_OP_ASSIGN) {
858 const char *key_name = key_pair_name(rule, pair);
859 char attr[PATH_SIZE];
860 char value[NAME_SIZE];
863 strlcpy(attr, sysfs_path, sizeof(attr));
864 strlcat(attr, udev->dev->devpath, sizeof(attr));
865 strlcat(attr, "/", sizeof(attr));
866 strlcat(attr, key_name, sizeof(attr));
867 strlcpy(value, key_val(rule, &pair->key), sizeof(value));
868 udev_rules_apply_format(udev, value, sizeof(value));
869 info("writing '%s' to sysfs file '%s'", value, attr);
870 f = fopen(attr, "w");
873 if (fprintf(f, "%s", value) <= 0)
874 err("error writing ATTR{%s}: %s", attr, strerror(errno));
877 err("error opening ATTR{%s} for writing: %s", attr, strerror(errno));
886 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
888 struct udev_rule *rule;
891 dbg("udev->dev->devpath='%s'", udev->dev->devpath);
892 dbg("udev->dev->kernel='%s'", udev->dev->kernel);
894 /* look for a matching rule to apply */
895 udev_rules_iter_init(rules);
897 rule = udev_rules_iter_next(rules);
902 (rule->name.operation == KEY_OP_ASSIGN ||
903 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
904 rule->name.operation == KEY_OP_ADD)) {
905 dbg("node name already set, rule ignored");
910 if (match_rule(udev, rule) == 0) {
912 if (rule->ignore_device) {
913 info("rule applied, '%s' is ignored", udev->dev->kernel);
914 udev->ignore_device = 1;
917 if (rule->ignore_remove) {
918 udev->ignore_remove = 1;
919 dbg("remove event should be ignored");
921 if (rule->link_priority != 0) {
922 udev->link_priority = rule->link_priority;
923 info("link_priority=%i", udev->link_priority);
925 /* apply all_partitions option only at a main block device */
926 if (rule->partitions &&
927 strcmp(udev->dev->subsystem, "block") == 0 && udev->dev->kernel_number[0] == '\0') {
928 udev->partitions = rule->partitions;
929 dbg("creation of partition nodes requested");
932 /* apply permissions */
933 if (!udev->mode_final && rule->mode != 0000) {
934 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
935 udev->mode_final = 1;
936 udev->mode = rule->mode;
937 dbg("applied mode=%#o to '%s'", rule->mode, udev->dev->kernel);
939 if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
940 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
941 udev->owner_final = 1;
942 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
943 udev_rules_apply_format(udev, udev->owner, sizeof(udev->owner));
944 dbg("applied owner='%s' to '%s'", udev->owner, udev->dev->kernel);
946 if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
947 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
948 udev->group_final = 1;
949 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
950 udev_rules_apply_format(udev, udev->group, sizeof(udev->group));
951 dbg("applied group='%s' to '%s'", udev->group, udev->dev->kernel);
954 /* collect symlinks */
955 if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
956 char temp[PATH_SIZE];
960 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
961 udev->symlink_final = 1;
962 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
963 info("reset symlink list");
964 name_list_cleanup(&udev->symlink_list);
966 /* allow multiple symlinks separated by spaces */
967 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
968 udev_rules_apply_format(udev, temp, sizeof(temp));
969 count = replace_chars(temp, ALLOWED_CHARS_FILE " ");
971 info("%i character(s) replaced" , count);
972 dbg("rule applied, added symlink(s) '%s'", temp);
974 while (isspace(pos[0]))
976 next = strchr(pos, ' ');
979 info("add symlink '%s'", pos);
980 name_list_add(&udev->symlink_list, pos, 0);
981 while (isspace(next[1]))
984 next = strchr(pos, ' ');
986 if (pos[0] != '\0') {
987 info("add symlink '%s'", pos);
988 name_list_add(&udev->symlink_list, pos, 0);
992 /* set name, later rules with name set will be ignored */
993 if (rule->name.operation == KEY_OP_ASSIGN ||
994 rule->name.operation == KEY_OP_ASSIGN_FINAL ||
995 rule->name.operation == KEY_OP_ADD) {
999 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
1000 udev_rules_apply_format(udev, udev->name, sizeof(udev->name));
1001 count = replace_chars(udev->name, ALLOWED_CHARS_FILE);
1003 info("%i character(s) replaced", count);
1005 info("rule applied, '%s' becomes '%s'", udev->dev->kernel, udev->name);
1006 if (strcmp(udev->dev->subsystem, "net") != 0)
1007 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1008 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1011 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1012 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1013 udev->run_final = 1;
1014 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1015 info("reset run list");
1016 name_list_cleanup(&udev->run_list);
1018 dbg("add run '%s'", key_val(rule, &rule->run));
1019 name_list_add(&udev->run_list, key_val(rule, &rule->run), 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->dev->kernel, 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)
1050 struct udev_rule *rule;
1052 dbg("udev->kernel='%s'", udev->dev->kernel);
1054 /* look for a matching rule to apply */
1055 udev_rules_iter_init(rules);
1057 rule = udev_rules_iter_next(rules);
1061 dbg("process rule");
1062 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1063 rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET ||
1064 rule->group.operation != KEY_OP_UNSET) {
1065 dbg("skip rule that names a device");
1069 if (match_rule(udev, rule) == 0) {
1070 if (rule->ignore_device) {
1071 info("rule applied, '%s' is ignored", udev->dev->kernel);
1072 udev->ignore_device = 1;
1076 if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1077 if (rule->run.operation == KEY_OP_ASSIGN ||
1078 rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1079 info("reset run list");
1080 name_list_cleanup(&udev->run_list);
1082 dbg("add run '%s'", key_val(rule, &rule->run));
1083 name_list_add(&udev->run_list, key_val(rule, &rule->run), 0);
1084 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1088 if (rule->last_rule) {
1089 dbg("last rule to be applied");
1093 if (rule->goto_label.operation != KEY_OP_UNSET) {
1094 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1095 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));