]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev_rules.c
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
);
189 name_list_key_add(&udev
->env_list
, variable
, value
);
190 setenv(variable
, value
, 1);
197 static int import_file_into_env(struct udevice
*udev
, const char *filename
)
202 if (file_map(filename
, &buf
, &bufsize
) != 0) {
203 err("can't open '%s': %s", filename
, strerror(errno
));
206 import_keys_into_env(udev
, buf
, bufsize
);
207 file_unmap(buf
, bufsize
);
212 static int import_program_into_env(struct udevice
*udev
, const char *program
)
217 if (run_program(program
, udev
->dev
->subsystem
, result
, sizeof(result
), &reslen
, (udev_log_priority
>= LOG_INFO
)) != 0)
219 return import_keys_into_env(udev
, result
, reslen
);
222 static int import_parent_into_env(struct udevice
*udev
, const char *filter
)
224 struct sysfs_device
*dev_parent
;
227 dev_parent
= sysfs_device_get_parent(udev
->dev
);
228 if (dev_parent
!= NULL
) {
229 struct udevice
*udev_parent
;
230 struct name_entry
*name_loop
;
232 dbg("found parent '%s', get the node name", dev_parent
->devpath
);
233 udev_parent
= udev_device_init();
234 if (udev_parent
== NULL
)
236 /* import the udev_db of the parent */
237 if (udev_db_get_device(udev_parent
, dev_parent
->devpath
) == 0) {
238 dbg("import stored parent env '%s'", udev_parent
->name
);
239 list_for_each_entry(name_loop
, &udev_parent
->env_list
, node
) {
240 char name
[NAME_SIZE
];
243 strlcpy(name
, name_loop
->name
, sizeof(name
));
244 pos
= strchr(name
, '=');
248 if (fnmatch(filter
, name
, 0) == 0) {
249 dbg("import key '%s'", name_loop
->name
);
250 name_list_add(&udev
->env_list
, name_loop
->name
, 0);
251 setenv(name
, pos
, 1);
253 dbg("skip key '%s'", name_loop
->name
);
258 dbg("parent not found in database");
259 udev_device_cleanup(udev_parent
);
265 #define WAIT_LOOP_PER_SECOND 50
266 static int wait_for_sysfs(struct udevice
*udev
, const char *file
, int timeout
)
268 char devicepath
[PATH_SIZE
];
269 char filepath
[PATH_SIZE
];
271 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
273 strlcpy(devicepath
, sysfs_path
, sizeof(devicepath
));
274 strlcat(devicepath
, udev
->dev
->devpath
, sizeof(devicepath
));
275 strlcpy(filepath
, devicepath
, sizeof(filepath
));
276 strlcat(filepath
, "/", sizeof(filepath
));
277 strlcat(filepath
, file
, sizeof(filepath
));
279 dbg("will wait %i sec for '%s'", timeout
, filepath
);
282 if (stat(filepath
, &stats
) == 0) {
283 info("file '%s' appeared after %i loops", filepath
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
286 /* make sure the device does not have disappeared in the meantime */
287 if (stat(devicepath
, &stats
) != 0) {
288 info("device disappeared while waiting for '%s'", filepath
);
291 info("wait for '%s' for %i mseconds", filepath
, 1000 / WAIT_LOOP_PER_SECOND
);
292 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
294 err("waiting for '%s' failed", filepath
);
298 void udev_rules_apply_format(struct udevice
*udev
, char *string
, size_t maxsize
)
300 char temp
[PATH_SIZE
];
301 char temp2
[PATH_SIZE
];
302 char *head
, *tail
, *pos
, *cpos
, *attr
, *rest
;
321 static const struct subst_map
{
324 enum subst_type type
;
326 { .name
= "devpath", .fmt
= 'p', .type
= SUBST_DEVPATH
},
327 { .name
= "number", .fmt
= 'n', .type
= SUBST_KERNEL_NUMBER
},
328 { .name
= "kernel", .fmt
= 'k', .type
= SUBST_KERNEL
},
329 { .name
= "id", .fmt
= 'b', .type
= SUBST_ID
},
330 { .name
= "major", .fmt
= 'M', .type
= SUBST_MAJOR
},
331 { .name
= "minor", .fmt
= 'm', .type
= SUBST_MINOR
},
332 { .name
= "result", .fmt
= 'c', .type
= SUBST_RESULT
},
333 { .name
= "attr", .fmt
= 's', .type
= SUBST_ATTR
},
334 { .name
= "sysfs", .fmt
= 's', .type
= SUBST_ATTR
},
335 { .name
= "parent", .fmt
= 'P', .type
= SUBST_PARENT
},
336 { .name
= "tempnode", .fmt
= 'N', .type
= SUBST_TEMP_NODE
},
337 { .name
= "root", .fmt
= 'r', .type
= SUBST_ROOT
},
338 { .name
= "env", .fmt
= 'E', .type
= SUBST_ENV
},
341 enum subst_type type
;
342 const struct subst_map
*subst
;
347 while (head
[0] != '\0') {
348 if (head
[0] == '$') {
349 /* substitute named variable */
352 if (head
[1] == '$') {
353 strlcpy(temp
, head
+2, sizeof(temp
));
354 strlcpy(head
+1, temp
, maxsize
);
359 for (subst
= map
; subst
->name
; subst
++) {
360 if (strncasecmp(&head
[1], subst
->name
, strlen(subst
->name
)) == 0) {
362 tail
= head
+ strlen(subst
->name
)+1;
363 dbg("will substitute format name '%s'", subst
->name
);
368 err("unknown format variable '%s'", head
);
369 } else if (head
[0] == '%') {
370 /* substitute format char */
373 if (head
[1] == '%') {
374 strlcpy(temp
, head
+2, sizeof(temp
));
375 strlcpy(head
+1, temp
, maxsize
);
381 len
= get_format_len(&tail
);
382 for (subst
= map
; subst
->name
; subst
++) {
383 if (tail
[0] == subst
->fmt
) {
386 dbg("will substitute format char '%c'", subst
->fmt
);
391 err("unknown format char '%c'", tail
[0]);
397 attr
= get_format_attribute(&tail
);
398 strlcpy(temp
, tail
, sizeof(temp
));
399 dbg("format=%i, string='%s', tail='%s'", type
,string
, tail
);
403 strlcat(string
, udev
->dev
->devpath
, maxsize
);
404 dbg("substitute devpath '%s'", udev
->dev
->devpath
);
407 strlcat(string
, udev
->dev
->kernel
, maxsize
);
408 dbg("substitute kernel name '%s'", udev
->dev
->kernel
);
410 case SUBST_KERNEL_NUMBER
:
411 strlcat(string
, udev
->dev
->kernel_number
, maxsize
);
412 dbg("substitute kernel number '%s'", udev
->dev
->kernel_number
);
415 if (udev
->dev_parent
!= NULL
) {
416 strlcat(string
, udev
->dev_parent
->kernel
, maxsize
);
417 dbg("substitute id '%s'", udev
->dev_parent
->kernel
);
421 sprintf(temp2
, "%d", major(udev
->devt
));
422 strlcat(string
, temp2
, maxsize
);
423 dbg("substitute major number '%s'", temp2
);
426 sprintf(temp2
, "%d", minor(udev
->devt
));
427 strlcat(string
, temp2
, maxsize
);
428 dbg("substitute minor number '%s'", temp2
);
431 if (udev
->program_result
[0] == '\0')
433 /* get part part of the result string */
436 i
= strtoul(attr
, &rest
, 10);
438 dbg("request part #%d of result string", i
);
439 cpos
= udev
->program_result
;
441 while (cpos
[0] != '\0' && !isspace(cpos
[0]))
443 while (isspace(cpos
[0]))
447 err("requested part of result string not found");
450 strlcpy(temp2
, cpos
, sizeof(temp2
));
451 /* %{2+}c copies the whole string from the second part on */
452 if (rest
[0] != '+') {
453 cpos
= strchr(temp2
, ' ');
457 strlcat(string
, temp2
, maxsize
);
458 dbg("substitute part of result string '%s'", temp2
);
460 strlcat(string
, udev
->program_result
, maxsize
);
461 dbg("substitute result string '%s'", udev
->program_result
);
466 err("missing file parameter for attr");
468 const char *value
= NULL
;
471 /* first try the current device, other matches may have selected */
472 if (udev
->dev_parent
!= NULL
&& udev
->dev_parent
!= udev
->dev
)
473 value
= sysfs_attr_get_value(udev
->dev_parent
->devpath
, attr
);
475 /* look at all devices along the chain of parents */
477 struct sysfs_device
*dev_parent
= udev
->dev
;
480 dbg("looking at '%s'", dev_parent
->devpath
);
481 value
= sysfs_attr_get_value(dev_parent
->devpath
, attr
);
483 strlcpy(temp2
, value
, sizeof(temp2
));
486 dev_parent
= sysfs_device_get_parent(dev_parent
);
487 } while (dev_parent
!= NULL
);
493 /* strip trailing whitespace and replace untrusted characters of sysfs value */
494 size
= strlcpy(temp2
, value
, sizeof(temp2
));
495 if (size
>= sizeof(temp2
))
496 size
= sizeof(temp2
)-1;
497 while (size
> 0 && isspace(temp2
[size
-1]))
498 temp2
[--size
] = '\0';
499 count
= replace_untrusted_chars(temp2
);
501 info("%i untrusted character(s) replaced" , count
);
502 strlcat(string
, temp2
, maxsize
);
503 dbg("substitute sysfs value '%s'", temp2
);
508 struct sysfs_device
*dev_parent
;
510 dev_parent
= sysfs_device_get_parent(udev
->dev
);
511 if (dev_parent
!= NULL
) {
512 struct udevice
*udev_parent
;
514 dbg("found parent '%s', get the node name", dev_parent
->devpath
);
515 udev_parent
= udev_device_init();
516 if (udev_parent
!= NULL
) {
517 /* lookup the name in the udev_db with the DEVPATH of the parent */
518 if (udev_db_get_device(udev_parent
, dev_parent
->devpath
) == 0) {
519 strlcat(string
, udev_parent
->name
, maxsize
);
520 dbg("substitute parent node name'%s'", udev_parent
->name
);
522 dbg("parent not found in database");
523 udev_device_cleanup(udev_parent
);
528 case SUBST_TEMP_NODE
:
529 if (udev
->tmp_node
[0] == '\0') {
530 dbg("create temporary device node for callout");
531 snprintf(udev
->tmp_node
, sizeof(udev
->tmp_node
), "%s/.tmp-%u-%u",
532 udev_root
, major(udev
->devt
), minor(udev
->devt
));
533 udev
->tmp_node
[sizeof(udev
->tmp_node
)-1] = '\0';
534 udev_node_mknod(udev
, udev
->tmp_node
, udev
->devt
, 0600, 0, 0);
536 strlcat(string
, udev
->tmp_node
, maxsize
);
537 dbg("substitute temporary device node name '%s'", udev
->tmp_node
);
540 strlcat(string
, udev_root
, maxsize
);
541 dbg("substitute udev_root '%s'", udev_root
);
545 dbg("missing attribute");
550 dbg("env '%s' not available", attr
);
553 dbg("substitute env '%s=%s'", attr
, pos
);
554 strlcat(string
, pos
, maxsize
);
557 err("unknown substitution type=%i", type
);
560 /* possibly truncate to format-char specified length */
561 if (len
>= 0 && len
< (int)strlen(head
)) {
563 dbg("truncate to %i chars, subtitution string becomes '%s'", len
, head
);
565 strlcat(string
, temp
, maxsize
);
569 static char *key_val(struct udev_rule
*rule
, struct key
*key
)
571 return rule
->buf
+ key
->val_off
;
574 static char *key_pair_name(struct udev_rule
*rule
, struct key_pair
*pair
)
576 return rule
->buf
+ pair
->key_name_off
;
579 static int match_key(const char *key_name
, struct udev_rule
*rule
, struct key
*key
, const char *val
)
582 char value
[PATH_SIZE
];
586 if (key
->operation
!= KEY_OP_MATCH
&&
587 key
->operation
!= KEY_OP_NOMATCH
)
590 strlcpy(value
, rule
->buf
+ key
->val_off
, sizeof(value
));
593 dbg("key %s value='%s'", key_name
, key_value
);
595 pos
= strchr(key_value
, '|');
600 dbg("match %s '%s' <-> '%s'", key_name
, key_value
, val
);
601 match
= (fnmatch(key_value
, val
, 0) == 0);
602 if (match
&& (key
->operation
!= KEY_OP_NOMATCH
)) {
603 dbg("%s is true (matching value)", key_name
);
606 if (!match
&& (key
->operation
== KEY_OP_NOMATCH
)) {
607 dbg("%s is true (non-matching value)", key_name
);
612 dbg("%s is false", key_name
);
616 /* match a single rule against a given device and possibly its parent devices */
617 static int match_rule(struct udevice
*udev
, struct udev_rule
*rule
)
621 if (match_key("ACTION", rule
, &rule
->action
, udev
->action
))
624 if (match_key("KERNEL", rule
, &rule
->kernel
, udev
->dev
->kernel
))
627 if (match_key("SUBSYSTEM", rule
, &rule
->subsystem
, udev
->dev
->subsystem
))
630 if (match_key("DEVPATH", rule
, &rule
->devpath
, udev
->dev
->devpath
))
633 if (match_key("DRIVER", rule
, &rule
->driver
, udev
->dev
->driver
))
636 /* match NAME against a value assigned by an earlier rule */
637 if (match_key("NAME", rule
, &rule
->name
, udev
->name
))
640 for (i
= 0; i
< rule
->env
.count
; i
++) {
641 struct key_pair
*pair
= &rule
->env
.keys
[i
];
643 /* we only check for matches, assignments will be handled later */
644 if (pair
->key
.operation
== KEY_OP_MATCH
||
645 pair
->key
.operation
== KEY_OP_NOMATCH
) {
646 const char *key_name
= key_pair_name(rule
, pair
);
647 const char *value
= getenv(key_name
);
650 dbg("ENV{'%s'} is not set, treat as empty", key_name
);
653 if (match_key("ENV", rule
, &pair
->key
, value
))
658 if (rule
->wait_for_sysfs
.operation
!= KEY_OP_UNSET
) {
661 found
= (wait_for_sysfs(udev
, key_val(rule
, &rule
->wait_for_sysfs
), 3) == 0);
662 if (!found
&& (rule
->wait_for_sysfs
.operation
!= KEY_OP_NOMATCH
))
666 /* check for matching sysfs attribute pairs */
667 for (i
= 0; i
< rule
->attr
.count
; i
++) {
668 struct key_pair
*pair
= &rule
->attr
.keys
[i
];
670 if (pair
->key
.operation
== KEY_OP_MATCH
||
671 pair
->key
.operation
== KEY_OP_NOMATCH
) {
672 const char *key_name
= key_pair_name(rule
, pair
);
673 const char *key_value
= key_val(rule
, &pair
->key
);
675 char val
[VALUE_SIZE
];
678 value
= sysfs_attr_get_value(udev
->dev
->devpath
, key_name
);
681 strlcpy(val
, value
, sizeof(val
));
683 /* strip trailing whitespace of value, if not asked to match for it */
684 len
= strlen(key_value
);
685 if (len
> 0 && !isspace(key_value
[len
-1])) {
687 while (len
> 0 && isspace(val
[len
-1]))
689 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val
)-len
, val
);
692 if (match_key("ATTR", rule
, &pair
->key
, val
))
697 /* walk up the chain of parent devices and find a match */
698 udev
->dev_parent
= udev
->dev
;
700 /* check for matching kernel device name */
701 if (match_key("KERNELS", rule
, &rule
->kernels
, udev
->dev_parent
->kernel
))
704 /* check for matching subsystem value */
705 if (match_key("SUBSYSTEMS", rule
, &rule
->subsystems
, udev
->dev_parent
->subsystem
))
708 /* check for matching driver */
709 if (match_key("DRIVERS", rule
, &rule
->drivers
, udev
->dev_parent
->driver
))
712 /* check for matching sysfs attribute pairs */
713 for (i
= 0; i
< rule
->attrs
.count
; i
++) {
714 struct key_pair
*pair
= &rule
->attrs
.keys
[i
];
716 if (pair
->key
.operation
== KEY_OP_MATCH
||
717 pair
->key
.operation
== KEY_OP_NOMATCH
) {
718 const char *key_name
= key_pair_name(rule
, pair
);
719 const char *key_value
= key_val(rule
, &pair
->key
);
721 char val
[VALUE_SIZE
];
724 value
= sysfs_attr_get_value(udev
->dev_parent
->devpath
, key_name
);
726 value
= sysfs_attr_get_value(udev
->dev
->devpath
, key_name
);
729 strlcpy(val
, value
, sizeof(val
));
731 /* strip trailing whitespace of value, if not asked to match for it */
732 len
= strlen(key_value
);
733 if (len
> 0 && !isspace(key_value
[len
-1])) {
735 while (len
> 0 && isspace(val
[len
-1]))
737 dbg("removed %zi trailing whitespace chars from '%s'", strlen(val
)-len
, val
);
740 if (match_key("ATTRS", rule
, &pair
->key
, val
))
745 /* found matching device */
748 /* move to parent device */
749 dbg("try parent sysfs device");
750 udev
->dev_parent
= sysfs_device_get_parent(udev
->dev_parent
);
751 if (udev
->dev_parent
== NULL
)
753 dbg("looking at dev_parent->devpath='%s'", udev
->dev_parent
->devpath
);
754 dbg("looking at dev_parent->kernel='%s'", udev
->dev_parent
->kernel
);
757 /* execute external program */
758 if (rule
->program
.operation
!= KEY_OP_UNSET
) {
759 char program
[PATH_SIZE
];
760 char result
[PATH_SIZE
];
762 strlcpy(program
, key_val(rule
, &rule
->program
), sizeof(program
));
763 udev_rules_apply_format(udev
, program
, sizeof(program
));
764 if (run_program(program
, udev
->dev
->subsystem
, result
, sizeof(result
), NULL
, (udev_log_priority
>= LOG_INFO
)) != 0) {
765 dbg("PROGRAM is false");
766 udev
->program_result
[0] = '\0';
767 if (rule
->program
.operation
!= KEY_OP_NOMATCH
)
772 dbg("PROGRAM matches");
773 remove_trailing_chars(result
, '\n');
774 count
= replace_untrusted_chars(result
);
776 info("%i untrusted character(s) replaced" , count
);
777 dbg("result is '%s'", result
);
778 strlcpy(udev
->program_result
, result
, sizeof(udev
->program_result
));
779 dbg("PROGRAM returned successful");
780 if (rule
->program
.operation
== KEY_OP_NOMATCH
)
783 dbg("PROGRAM key is true");
786 /* check for matching result of external program */
787 if (match_key("RESULT", rule
, &rule
->result
, udev
->program_result
))
790 /* import variables returned from program or or file into environment */
791 if (rule
->import
.operation
!= KEY_OP_UNSET
) {
792 char import
[PATH_SIZE
];
795 strlcpy(import
, key_val(rule
, &rule
->import
), sizeof(import
));
796 udev_rules_apply_format(udev
, import
, sizeof(import
));
797 dbg("check for IMPORT import='%s'", import
);
798 if (rule
->import_type
== IMPORT_PROGRAM
) {
799 rc
= import_program_into_env(udev
, import
);
800 } else if (rule
->import_type
== IMPORT_FILE
) {
801 dbg("import file import='%s'", import
);
802 rc
= import_file_into_env(udev
, import
);
803 } else if (rule
->import_type
== IMPORT_PARENT
) {
804 dbg("import parent import='%s'", import
);
805 rc
= import_parent_into_env(udev
, import
);
808 dbg("IMPORT failed");
809 if (rule
->import
.operation
!= KEY_OP_NOMATCH
)
812 dbg("IMPORT '%s' imported", key_val(rule
, &rule
->import
));
813 dbg("IMPORT key is true");
816 /* rule matches, if we have ENV assignments export it */
817 for (i
= 0; i
< rule
->env
.count
; i
++) {
818 struct key_pair
*pair
= &rule
->env
.keys
[i
];
820 if (pair
->key
.operation
== KEY_OP_ASSIGN
) {
821 char temp_value
[NAME_SIZE
];
822 const char *key_name
= key_pair_name(rule
, pair
);
823 const char *value
= key_val(rule
, &pair
->key
);
826 /* make sure we don't write to the same string we possibly read from */
827 strlcpy(temp_value
, value
, sizeof(temp_value
));
828 udev_rules_apply_format(udev
, temp_value
, NAME_SIZE
);
830 key_value
= name_list_key_add(&udev
->env_list
, key_name
, temp_value
);
831 if (key_value
== NULL
)
835 dbg("export ENV '%s'", key_value
);
839 /* if we have ATTR assignements write value to sysfs file */
840 for (i
= 0; i
< rule
->attr
.count
; i
++) {
841 struct key_pair
*pair
= &rule
->attr
.keys
[i
];
843 if (pair
->key
.operation
== KEY_OP_ASSIGN
) {
844 const char *key_name
= key_pair_name(rule
, pair
);
845 const char *key_value
= key_val(rule
, &pair
->key
);
846 char attr
[PATH_SIZE
];
849 strlcpy(attr
, sysfs_path
, sizeof(attr
));
850 strlcat(attr
, udev
->dev
->devpath
, sizeof(attr
));
851 strlcat(attr
, "/", sizeof(attr
));
852 strlcat(attr
, key_name
, sizeof(attr
));
853 dbg("write '%s' to '%s'", key_value
, attr
);
854 f
= fopen(attr
, "w");
856 if (fprintf(f
, "%s\n", key_value
) <= 0)
857 err("error writing ATTR{%s}: %s", attr
, strerror(errno
));
860 err("error opening ATTR{%s} for writing: %s", attr
, strerror(errno
));
869 int udev_rules_get_name(struct udev_rules
*rules
, struct udevice
*udev
)
871 struct udev_rule
*rule
;
874 dbg("udev->dev->devpath='%s'", udev
->dev
->devpath
);
875 dbg("udev->dev->kernel='%s'", udev
->dev
->kernel
);
877 /* look for a matching rule to apply */
878 udev_rules_iter_init(rules
);
880 rule
= udev_rules_iter_next(rules
);
885 (rule
->name
.operation
== KEY_OP_ASSIGN
||
886 rule
->name
.operation
== KEY_OP_ASSIGN_FINAL
||
887 rule
->name
.operation
== KEY_OP_ADD
)) {
888 dbg("node name already set, rule ignored");
893 if (match_rule(udev
, rule
) == 0) {
895 if (rule
->ignore_device
) {
896 info("rule applied, '%s' is ignored", udev
->dev
->kernel
);
897 udev
->ignore_device
= 1;
900 if (rule
->ignore_remove
) {
901 udev
->ignore_remove
= 1;
902 dbg("remove event should be ignored");
904 /* apply all_partitions option only at a main block device */
905 if (rule
->partitions
&&
906 strcmp(udev
->dev
->subsystem
, "block") == 0 && udev
->dev
->kernel_number
[0] == '\0') {
907 udev
->partitions
= rule
->partitions
;
908 dbg("creation of partition nodes requested");
911 /* apply permissions */
912 if (!udev
->mode_final
&& rule
->mode
!= 0000) {
913 if (rule
->mode_operation
== KEY_OP_ASSIGN_FINAL
)
914 udev
->mode_final
= 1;
915 udev
->mode
= rule
->mode
;
916 dbg("applied mode=%#o to '%s'", rule
->mode
, udev
->dev
->kernel
);
918 if (!udev
->owner_final
&& rule
->owner
.operation
!= KEY_OP_UNSET
) {
919 if (rule
->owner
.operation
== KEY_OP_ASSIGN_FINAL
)
920 udev
->owner_final
= 1;
921 strlcpy(udev
->owner
, key_val(rule
, &rule
->owner
), sizeof(udev
->owner
));
922 udev_rules_apply_format(udev
, udev
->owner
, sizeof(udev
->owner
));
923 dbg("applied owner='%s' to '%s'", udev
->owner
, udev
->dev
->kernel
);
925 if (!udev
->group_final
&& rule
->group
.operation
!= KEY_OP_UNSET
) {
926 if (rule
->group
.operation
== KEY_OP_ASSIGN_FINAL
)
927 udev
->group_final
= 1;
928 strlcpy(udev
->group
, key_val(rule
, &rule
->group
), sizeof(udev
->group
));
929 udev_rules_apply_format(udev
, udev
->group
, sizeof(udev
->group
));
930 dbg("applied group='%s' to '%s'", udev
->group
, udev
->dev
->kernel
);
933 /* collect symlinks */
934 if (!udev
->symlink_final
&& rule
->symlink
.operation
!= KEY_OP_UNSET
) {
935 char temp
[PATH_SIZE
];
939 if (rule
->symlink
.operation
== KEY_OP_ASSIGN_FINAL
)
940 udev
->symlink_final
= 1;
941 if (rule
->symlink
.operation
== KEY_OP_ASSIGN
|| rule
->symlink
.operation
== KEY_OP_ASSIGN_FINAL
) {
942 info("reset symlink list");
943 name_list_cleanup(&udev
->symlink_list
);
945 strlcpy(temp
, key_val(rule
, &rule
->symlink
), sizeof(temp
));
946 udev_rules_apply_format(udev
, temp
, sizeof(temp
));
947 count
= replace_untrusted_chars(temp
);
949 info("%i untrusted character(s) replaced" , count
);
950 dbg("rule applied, added symlink(s) '%s'", temp
);
952 /* add multiple symlinks separated by spaces */
954 while (isspace(pos
[0]))
956 next
= strchr(pos
, ' ');
959 info("add symlink '%s'", pos
);
960 name_list_add(&udev
->symlink_list
, pos
, 0);
961 while (isspace(next
[1]))
964 next
= strchr(pos
, ' ');
966 if (pos
[0] != '\0') {
967 info("add symlink '%s'", pos
);
968 name_list_add(&udev
->symlink_list
, pos
, 0);
972 /* set name, later rules with name set will be ignored */
973 if (rule
->name
.operation
== KEY_OP_ASSIGN
||
974 rule
->name
.operation
== KEY_OP_ASSIGN_FINAL
||
975 rule
->name
.operation
== KEY_OP_ADD
) {
979 strlcpy(udev
->name
, key_val(rule
, &rule
->name
), sizeof(udev
->name
));
980 udev_rules_apply_format(udev
, udev
->name
, sizeof(udev
->name
));
981 count
= replace_untrusted_chars(udev
->name
);
983 info("%i untrusted character(s) replaced", count
);
985 info("rule applied, '%s' becomes '%s'", udev
->dev
->kernel
, udev
->name
);
986 if (strcmp(udev
->dev
->subsystem
, "net") != 0)
987 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
988 udev
->name
, udev
->owner
, udev
->group
, udev
->mode
, udev
->partitions
);
991 if (!udev
->run_final
&& rule
->run
.operation
!= KEY_OP_UNSET
) {
992 if (rule
->run
.operation
== KEY_OP_ASSIGN_FINAL
)
994 if (rule
->run
.operation
== KEY_OP_ASSIGN
|| rule
->run
.operation
== KEY_OP_ASSIGN_FINAL
) {
995 info("reset run list");
996 name_list_cleanup(&udev
->run_list
);
998 dbg("add run '%s'", key_val(rule
, &rule
->run
));
999 name_list_add(&udev
->run_list
, key_val(rule
, &rule
->run
), 0);
1002 if (rule
->last_rule
) {
1003 dbg("last rule to be applied");
1007 if (rule
->goto_label
.operation
!= KEY_OP_UNSET
) {
1008 dbg("moving forward to label '%s'", key_val(rule
, &rule
->goto_label
));
1009 udev_rules_iter_label(rules
, key_val(rule
, &rule
->goto_label
));
1015 strlcpy(udev
->name
, udev
->dev
->kernel
, sizeof(udev
->name
));
1016 info("no node name set, will use kernel name '%s'", udev
->name
);
1019 if (udev
->tmp_node
[0] != '\0') {
1020 dbg("removing temporary device node");
1021 unlink_secure(udev
->tmp_node
);
1022 udev
->tmp_node
[0] = '\0';
1028 int udev_rules_get_run(struct udev_rules
*rules
, struct udevice
*udev
)
1030 struct udev_rule
*rule
;
1032 dbg("udev->kernel='%s'", udev
->dev
->kernel
);
1034 /* look for a matching rule to apply */
1035 udev_rules_iter_init(rules
);
1037 rule
= udev_rules_iter_next(rules
);
1041 dbg("process rule");
1042 if (rule
->name
.operation
!= KEY_OP_UNSET
|| rule
->symlink
.operation
!= KEY_OP_UNSET
||
1043 rule
->mode_operation
!= KEY_OP_UNSET
|| rule
->owner
.operation
!= KEY_OP_UNSET
|| rule
->group
.operation
!= KEY_OP_UNSET
) {
1044 dbg("skip rule that names a device");
1048 if (match_rule(udev
, rule
) == 0) {
1049 if (rule
->ignore_device
) {
1050 info("rule applied, '%s' is ignored", udev
->dev
->kernel
);
1051 udev
->ignore_device
= 1;
1055 if (!udev
->run_final
&& rule
->run
.operation
!= KEY_OP_UNSET
) {
1056 if (rule
->run
.operation
== KEY_OP_ASSIGN
|| rule
->run
.operation
== KEY_OP_ASSIGN_FINAL
) {
1057 info("reset run list");
1058 name_list_cleanup(&udev
->run_list
);
1060 dbg("add run '%s'", key_val(rule
, &rule
->run
));
1061 name_list_add(&udev
->run_list
, key_val(rule
, &rule
->run
), 0);
1062 if (rule
->run
.operation
== KEY_OP_ASSIGN_FINAL
)
1066 if (rule
->last_rule
) {
1067 dbg("last rule to be applied");
1071 if (rule
->goto_label
.operation
!= KEY_OP_UNSET
) {
1072 dbg("moving forward to label '%s'", key_val(rule
, &rule
->goto_label
));
1073 udev_rules_iter_label(rules
, key_val(rule
, &rule
->goto_label
));