1 /* SPDX-License-Identifier: GPL-2.0+ */
5 #include "alloc-util.h"
6 #include "conf-files.h"
8 #include "device-util.h"
9 #include "dirent-util.h"
13 #include "format-util.h"
15 #include "glob-util.h"
16 #include "libudev-util.h"
18 #include "nulstr-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "proc-cmdline.h"
22 #include "stat-util.h"
25 #include "sysctl-util.h"
26 #include "udev-builtin.h"
27 #include "udev-event.h"
28 #include "udev-rules.h"
29 #include "user-util.h"
31 #define RULES_DIRS (const char* const*) CONF_PATHS_STRV("udev/rules.d")
33 static void udev_rule_token_free(UdevRuleToken
*token
) {
37 static void udev_rule_line_clear_tokens(UdevRuleLine
*rule_line
) {
38 UdevRuleToken
*i
, *next
;
42 LIST_FOREACH_SAFE(tokens
, i
, next
, rule_line
->tokens
)
43 udev_rule_token_free(i
);
45 rule_line
->tokens
= NULL
;
48 static void udev_rule_line_free(UdevRuleLine
*rule_line
) {
52 udev_rule_line_clear_tokens(rule_line
);
54 if (rule_line
->rule_file
) {
55 if (rule_line
->rule_file
->current_line
== rule_line
)
56 rule_line
->rule_file
->current_line
= rule_line
->rule_lines_prev
;
58 LIST_REMOVE(rule_lines
, rule_line
->rule_file
->rule_lines
, rule_line
);
61 free(rule_line
->line
);
65 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine
*, udev_rule_line_free
);
67 static void udev_rule_file_free(UdevRuleFile
*rule_file
) {
68 UdevRuleLine
*i
, *next
;
73 LIST_FOREACH_SAFE(rule_lines
, i
, next
, rule_file
->rule_lines
)
74 udev_rule_line_free(i
);
76 free(rule_file
->filename
);
80 UdevRules
*udev_rules_free(UdevRules
*rules
) {
81 UdevRuleFile
*i
, *next
;
86 LIST_FOREACH_SAFE(rule_files
, i
, next
, rules
->rule_files
)
87 udev_rule_file_free(i
);
89 hashmap_free_free_key(rules
->known_users
);
90 hashmap_free_free_key(rules
->known_groups
);
94 static void log_unknown_owner(sd_device
*dev
, UdevRules
*rules
, int error
, const char *entity
, const char *name
) {
95 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
96 log_rule_error(dev
, rules
, "Unknown %s '%s', ignoring", entity
, name
);
98 log_rule_error_errno(dev
, rules
, error
, "Failed to resolve %s '%s', ignoring: %m", entity
, name
);
101 static int rule_resolve_user(UdevRules
*rules
, const char *name
, uid_t
*ret
) {
102 _cleanup_free_
char *n
= NULL
;
110 val
= hashmap_get(rules
->known_users
, name
);
112 *ret
= PTR_TO_UID(val
);
116 r
= get_user_creds(&name
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
118 log_unknown_owner(NULL
, rules
, r
, "user", name
);
127 r
= hashmap_ensure_allocated(&rules
->known_users
, &string_hash_ops
);
131 r
= hashmap_put(rules
->known_users
, n
, UID_TO_PTR(uid
));
140 static int rule_resolve_group(UdevRules
*rules
, const char *name
, gid_t
*ret
) {
141 _cleanup_free_
char *n
= NULL
;
149 val
= hashmap_get(rules
->known_groups
, name
);
151 *ret
= PTR_TO_GID(val
);
155 r
= get_group_creds(&name
, &gid
, USER_CREDS_ALLOW_MISSING
);
157 log_unknown_owner(NULL
, rules
, r
, "group", name
);
166 r
= hashmap_ensure_allocated(&rules
->known_groups
, &string_hash_ops
);
170 r
= hashmap_put(rules
->known_groups
, n
, GID_TO_PTR(gid
));
179 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
183 return SUBST_TYPE_SUBSYS
;
184 if (strchr(str
, '%') || strchr(str
, '$'))
185 return SUBST_TYPE_FORMAT
;
186 return SUBST_TYPE_PLAIN
;
189 static void rule_line_append_token(UdevRuleLine
*rule_line
, UdevRuleToken
*token
) {
193 if (rule_line
->current_token
)
194 LIST_APPEND(tokens
, rule_line
->current_token
, token
);
196 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
198 rule_line
->current_token
= token
;
201 static int rule_line_add_token(UdevRuleLine
*rule_line
, UdevRuleTokenType type
, UdevRuleOperatorType op
, char *value
, void *data
) {
202 UdevRuleToken
*token
;
203 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
204 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
205 bool remove_trailing_whitespace
= false;
209 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
210 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
212 if (type
< _TK_M_MAX
) {
214 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
216 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
217 match_type
= MATCH_TYPE_SUBSYSTEM
;
218 else if (isempty(value
))
219 match_type
= MATCH_TYPE_EMPTY
;
220 else if (streq(value
, "?*")) {
221 /* Convert KEY=="?*" -> KEY!="" */
222 match_type
= MATCH_TYPE_EMPTY
;
223 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
224 } else if (string_is_glob(value
))
225 match_type
= MATCH_TYPE_GLOB
;
227 match_type
= MATCH_TYPE_PLAIN
;
229 if (type
< TK_M_TEST
|| type
== TK_M_RESULT
) {
230 /* Convert value string to nulstr. */
232 if (len
> 1 && (value
[len
- 1] == '|' || strstr(value
, "||"))) {
233 /* In this case, just replacing '|' -> '\0' does not work... */
234 _cleanup_free_
char *tmp
= NULL
;
242 for (i
= tmp
, j
= value
; *i
!= '\0'; i
++)
254 /* Simple conversion. */
257 for (i
= value
; *i
!= '\0'; i
++)
264 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
269 if (len
> 0 && !isspace(value
[len
- 1]))
270 remove_trailing_whitespace
= true;
272 subst_type
= rule_get_substitution_type((const char*) data
);
275 token
= new(UdevRuleToken
, 1);
279 *token
= (UdevRuleToken
) {
284 .match_type
= match_type
,
285 .attr_subst_type
= subst_type
,
286 .attr_match_remove_trailing_whitespace
= remove_trailing_whitespace
,
289 rule_line_append_token(rule_line
, token
);
291 if (token
->type
== TK_A_NAME
)
292 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
294 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
295 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
296 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
297 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
299 else if (token
->type
>= _TK_A_MIN
||
301 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
302 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
303 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
308 static int parse_token(UdevRules
*rules
, const char *key
, char *attr
, UdevRuleOperatorType op
, char *value
) {
309 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
310 UdevRuleLine
*rule_line
;
314 assert(rules
->current_file
);
315 assert(rules
->current_file
->current_line
);
319 rule_line
= rules
->current_file
->current_line
;
321 if (streq(key
, "ACTION")) {
323 return log_token_invalid_attr(rules
, key
);
325 return log_token_invalid_op(rules
, key
);
327 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
);
328 } else if (streq(key
, "DEVPATH")) {
330 return log_token_invalid_attr(rules
, key
);
332 return log_token_invalid_op(rules
, key
);
334 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
);
335 } else if (streq(key
, "KERNEL")) {
337 return log_token_invalid_attr(rules
, key
);
339 return log_token_invalid_op(rules
, key
);
341 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
);
342 } else if (streq(key
, "SYMLINK")) {
344 return log_token_invalid_attr(rules
, key
);
346 return log_token_invalid_op(rules
, key
);
349 if (udev_check_format(value
) < 0)
350 log_token_invalid_value(rules
, key
, value
);
352 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
);
354 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
);
355 } else if (streq(key
, "NAME")) {
357 return log_token_invalid_attr(rules
, key
);
359 return log_token_invalid_op(rules
, key
);
361 log_token_warning(rules
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '=', but please fix it.", key
);
366 if (streq(value
, "%k"))
367 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
368 "Ignoring NAME=\"%%k\" is ignored, as it breaks kernel supplied names.");
370 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
371 "Ignoring NAME=\"\", as udev will not delete any device nodes.");
372 if (udev_check_format(value
) < 0)
373 log_token_invalid_value(rules
, key
, value
);
375 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
);
377 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
);
378 } else if (streq(key
, "ENV")) {
380 return log_token_invalid_attr(rules
, key
);
382 return log_token_invalid_op(rules
, key
);
383 if (op
== OP_ASSIGN_FINAL
) {
384 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '=', but please fix it.", key
);
390 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
391 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
392 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
393 "Invalid ENV attribute. '%s' cannot be set.", attr
);
395 if (udev_check_format(value
) < 0)
396 log_token_invalid_value(rules
, key
, value
);
397 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
);
399 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
);
400 } else if (streq(key
, "TAG")) {
402 return log_token_invalid_attr(rules
, key
);
403 if (op
== OP_ASSIGN_FINAL
) {
404 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '=', but please fix it.", key
);
409 if (isempty(value
) || udev_check_format(value
) < 0)
410 log_token_invalid_value(rules
, key
, value
);
411 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
);
413 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
);
414 } else if (streq(key
, "SUBSYSTEM")) {
416 return log_token_invalid_attr(rules
, key
);
418 return log_token_invalid_op(rules
, key
);
420 if (STR_IN_SET(value
, "bus", "class"))
421 log_token_warning(rules
, "'%s' must be specified as 'subsystem'; please fix it", value
);
423 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
424 } else if (streq(key
, "DRIVER")) {
426 return log_token_invalid_attr(rules
, key
);
428 return log_token_invalid_op(rules
, key
);
430 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
);
431 } else if (streq(key
, "ATTR")) {
433 return log_token_invalid_attr(rules
, key
);
434 if (udev_check_format(attr
) < 0)
435 log_token_invalid_attr_format(rules
, key
, attr
);
437 return log_token_invalid_op(rules
, key
);
438 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
439 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '=', but please fix it.", key
);
444 if (udev_check_format(value
) < 0)
445 log_token_invalid_value(rules
, key
, value
);
447 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
);
449 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
);
450 } else if (streq(key
, "SYSCTL")) {
452 return log_token_invalid_attr(rules
, key
);
453 if (udev_check_format(attr
) < 0)
454 log_token_invalid_attr_format(rules
, key
, attr
);
456 return log_token_invalid_op(rules
, key
);
457 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
458 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '=', but please fix it.", key
);
463 if (udev_check_format(value
) < 0)
464 log_token_invalid_value(rules
, key
, value
);
466 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
);
468 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
);
469 } else if (streq(key
, "KERNELS")) {
471 return log_token_invalid_attr(rules
, key
);
473 return log_token_invalid_op(rules
, key
);
475 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
);
476 } else if (streq(key
, "SUBSYSTEMS")) {
478 return log_token_invalid_attr(rules
, key
);
480 return log_token_invalid_op(rules
, key
);
482 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
);
483 } else if (streq(key
, "DRIVERS")) {
485 return log_token_invalid_attr(rules
, key
);
487 return log_token_invalid_op(rules
, key
);
489 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
);
490 } else if (streq(key
, "ATTRS")) {
492 return log_token_invalid_attr(rules
, key
);
493 if (udev_check_format(attr
) < 0)
494 log_token_invalid_attr_format(rules
, key
, attr
);
496 return log_token_invalid_op(rules
, key
);
498 if (startswith(attr
, "device/"))
499 log_token_warning(rules
, "'device' link may not be available in future kernels; please fix it.");
500 if (strstr(attr
, "../"))
501 log_token_warning(rules
, "Direct reference to parent sysfs directory, may break in future kernels; please fix it.");
503 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
);
504 } else if (streq(key
, "TAGS")) {
506 return log_token_invalid_attr(rules
, key
);
508 return log_token_invalid_op(rules
, key
);
510 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
);
511 } else if (streq(key
, "TEST")) {
512 mode_t mode
= MODE_INVALID
;
514 if (!isempty(attr
)) {
515 r
= parse_mode(attr
, &mode
);
517 return log_token_error_errno(rules
, r
, "Failed to parse mode '%s': %m", attr
);
519 if (isempty(value
) || udev_check_format(value
) < 0)
520 log_token_invalid_value(rules
, key
, value
);
522 return log_token_invalid_op(rules
, key
);
524 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
));
525 } else if (streq(key
, "PROGRAM")) {
527 return log_token_invalid_attr(rules
, key
);
528 if (isempty(value
) || udev_check_format(value
) < 0)
529 log_token_invalid_value(rules
, key
, value
);
531 return log_token_invalid_op(rules
, key
);
534 log_token_debug(rules
, "Operator '=' is specified to %s key, assuming '=='.", key
);
536 log_token_warning(rules
, "%s key takes '==' or '!=' operator, assuming '==', but please fix it.", key
);
540 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
);
541 } else if (streq(key
, "IMPORT")) {
543 return log_token_invalid_attr(rules
, key
);
544 if (isempty(value
) || udev_check_format(value
) < 0)
545 log_token_invalid_value(rules
, key
, value
);
547 return log_token_invalid_op(rules
, key
);
550 log_token_debug(rules
, "Operator '=' is specified to %s key, assuming '=='.", key
);
552 log_token_warning(rules
, "%s key takes '==' or '!=' operator, assuming '==', but please fix it.", key
);
556 if (streq(attr
, "file"))
557 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
558 else if (streq(attr
, "program")) {
559 UdevBuiltinCommand cmd
;
561 cmd
= udev_builtin_lookup(value
);
563 log_token_debug(rules
,"Found builtin command '%s' for %s, replacing attribute", value
, key
);
564 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
566 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
);
567 } else if (streq(attr
, "builtin")) {
568 UdevBuiltinCommand cmd
;
570 cmd
= udev_builtin_lookup(value
);
572 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
573 "Unknown builtin command: %s", value
);
574 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
575 } else if (streq(attr
, "db"))
576 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
);
577 else if (streq(attr
, "cmdline"))
578 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
579 else if (streq(attr
, "parent"))
580 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
582 return log_token_invalid_attr(rules
, key
);
583 } else if (streq(key
, "RESULT")) {
585 return log_token_invalid_attr(rules
, key
);
587 return log_token_invalid_op(rules
, key
);
589 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
);
590 } else if (streq(key
, "OPTIONS")) {
594 return log_token_invalid_attr(rules
, key
);
595 if (is_match
|| op
== OP_REMOVE
)
596 return log_token_invalid_op(rules
, key
);
598 log_token_debug(rules
, "Operator '+=' is specified to %s key, assuming '='.", key
);
602 if (streq(value
, "string_escape=none"))
603 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
604 else if (streq(value
, "string_escape=replace"))
605 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
606 else if (streq(value
, "db_persist"))
607 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
);
608 else if (streq(value
, "watch"))
609 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1));
610 else if (streq(value
, "nowatch"))
611 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0));
612 else if ((tmp
= startswith(value
, "static_node=")))
613 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
);
614 else if ((tmp
= startswith(value
, "link_priority="))) {
617 r
= safe_atoi(tmp
, &prio
);
619 return log_token_error_errno(rules
, r
, "Failed to parse link priority '%s': %m", tmp
);
620 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
));
622 log_token_warning(rules
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
625 } else if (streq(key
, "OWNER")) {
629 return log_token_invalid_attr(rules
, key
);
630 if (is_match
|| op
== OP_REMOVE
)
631 return log_token_invalid_op(rules
, key
);
633 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
637 if (parse_uid(value
, &uid
) >= 0)
638 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
639 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
640 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
641 r
= rule_resolve_user(rules
, value
, &uid
);
643 return log_token_error_errno(rules
, r
, "Failed to resolve user name '%s': %m", value
);
645 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
646 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
647 if (isempty(value
) || udev_check_format(value
) < 0)
648 log_token_invalid_value(rules
, key
, value
);
649 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
);
651 log_token_debug(rules
, "Resolving user name is disabled, ignoring %s=%s", key
, value
);
654 } else if (streq(key
, "GROUP")) {
658 return log_token_invalid_attr(rules
, key
);
659 if (is_match
|| op
== OP_REMOVE
)
660 return log_token_invalid_op(rules
, key
);
662 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
666 if (parse_gid(value
, &gid
) >= 0)
667 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
668 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
669 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
670 r
= rule_resolve_group(rules
, value
, &gid
);
672 return log_token_error_errno(rules
, r
, "Failed to resolve group name '%s': %m", value
);
674 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
675 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
676 if (isempty(value
) || udev_check_format(value
) < 0)
677 log_token_invalid_value(rules
, key
, value
);
678 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
);
680 log_token_debug(rules
, "Resolving group name is disabled, ignoring %s=%s", key
, value
);
683 } else if (streq(key
, "MODE")) {
687 return log_token_invalid_attr(rules
, key
);
688 if (is_match
|| op
== OP_REMOVE
)
689 return log_token_invalid_op(rules
, key
);
691 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
695 if (parse_mode(value
, &mode
) >= 0)
696 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
));
698 if (isempty(value
) || udev_check_format(value
) < 0)
699 log_token_invalid_value(rules
, key
, value
);
700 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
);
702 } else if (streq(key
, "SECLABEL")) {
704 return log_token_invalid_attr(rules
, key
);
705 if (isempty(value
) || udev_check_format(value
) < 0)
706 log_token_invalid_value(rules
, key
, value
);
707 if (is_match
|| op
== OP_REMOVE
)
708 return log_token_invalid_op(rules
, key
);
709 if (op
== OP_ASSIGN_FINAL
) {
710 log_token_warning(rules
, "%s key takes '=' or '+=' operator, assuming '=', but please fix it.", key
);
714 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, NULL
);
715 } else if (streq(key
, "RUN")) {
716 if (is_match
|| op
== OP_REMOVE
)
717 return log_token_invalid_op(rules
, key
);
718 if (isempty(value
) || udev_check_format(value
) < 0)
719 log_token_invalid_value(rules
, key
, value
);
720 if (!attr
|| streq(attr
, "program"))
721 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
);
722 else if (streq(attr
, "builtin")) {
723 UdevBuiltinCommand cmd
;
725 cmd
= udev_builtin_lookup(value
);
727 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
728 "Unknown builtin command '%s', ignoring", value
);
729 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
731 return log_token_invalid_attr(rules
, key
);
732 } else if (streq(key
, "GOTO")) {
734 return log_token_invalid_attr(rules
, key
);
736 return log_token_invalid_op(rules
, key
);
737 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
738 log_token_warning(rules
, "Contains multiple GOTO key, ignoring GOTO=\"%s\".", value
);
742 rule_line
->goto_label
= value
;
743 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
745 } else if (streq(key
, "LABEL")) {
747 return log_token_invalid_attr(rules
, key
);
749 return log_token_invalid_op(rules
, key
);
751 rule_line
->label
= value
;
752 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
755 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'", key
);
762 static UdevRuleOperatorType
parse_operator(const char *op
) {
765 if (startswith(op
, "=="))
767 if (startswith(op
, "!="))
769 if (startswith(op
, "+="))
771 if (startswith(op
, "-="))
773 if (startswith(op
, "="))
775 if (startswith(op
, ":="))
776 return OP_ASSIGN_FINAL
;
778 return _OP_TYPE_INVALID
;
781 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
) {
782 char *key_begin
, *key_end
, *attr
, *tmp
, *value
, *i
, *j
;
783 UdevRuleOperatorType op
;
791 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
793 if (isempty(key_begin
))
796 for (key_end
= key_begin
; ; key_end
++) {
797 if (key_end
[0] == '\0')
799 if (strchr(WHITESPACE
"={", key_end
[0]))
801 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
804 if (key_end
[0] == '{') {
806 tmp
= strchr(attr
, '}');
815 tmp
= skip_leading_chars(tmp
, NULL
);
816 op
= parse_operator(tmp
);
822 tmp
+= op
== OP_ASSIGN
? 1 : 2;
823 value
= skip_leading_chars(tmp
, NULL
);
825 /* value must be double quotated */
830 /* unescape double quotation '\"' -> '"' */
831 for (i
= j
= value
; ; i
++, j
++) {
836 if (i
[0] == '\\' && i
[1] == '"')
843 *ret_key
= key_begin
;
850 static void sort_tokens(UdevRuleLine
*rule_line
) {
851 UdevRuleToken
*head_old
;
855 head_old
= TAKE_PTR(rule_line
->tokens
);
856 rule_line
->current_token
= NULL
;
858 while (!LIST_IS_EMPTY(head_old
)) {
859 UdevRuleToken
*t
, *min_token
= NULL
;
861 LIST_FOREACH(tokens
, t
, head_old
)
862 if (!min_token
|| min_token
->type
> t
->type
)
865 LIST_REMOVE(tokens
, head_old
, min_token
);
866 rule_line_append_token(rule_line
, min_token
);
870 static int rule_add_line(UdevRules
*rules
, const char *line_str
, unsigned line_nr
) {
871 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
872 _cleanup_free_
char *line
= NULL
;
873 UdevRuleFile
*rule_file
;
878 assert(rules
->current_file
);
881 rule_file
= rules
->current_file
;
883 if (isempty(line_str
))
886 line
= strdup(line_str
);
890 rule_line
= new(UdevRuleLine
, 1);
894 *rule_line
= (UdevRuleLine
) {
895 .line
= TAKE_PTR(line
),
896 .line_number
= line_nr
,
897 .rule_file
= rule_file
,
900 if (rule_file
->current_line
)
901 LIST_APPEND(rule_lines
, rule_file
->current_line
, rule_line
);
903 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
905 rule_file
->current_line
= rule_line
;
907 for (p
= rule_line
->line
; !isempty(p
); ) {
908 char *key
, *attr
, *value
;
909 UdevRuleOperatorType op
;
911 r
= parse_line(&p
, &key
, &attr
, &op
, &value
);
913 return log_token_error_errno(rules
, r
, "Invalid key/value pair, ignoring.");
917 r
= parse_token(rules
, key
, attr
, op
, value
);
922 if (rule_line
->type
== 0) {
923 log_token_warning(rules
, "The line takes no effect, ignoring.");
927 sort_tokens(rule_line
);
932 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
933 UdevRuleLine
*line
, *line_next
, *i
;
937 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
938 LIST_FOREACH_SAFE(rule_lines
, line
, line_next
, rule_file
->rule_lines
) {
939 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
942 LIST_FOREACH_AFTER(rule_lines
, i
, line
)
943 if (streq_ptr(i
->label
, line
->goto_label
)) {
948 if (!line
->goto_line
) {
949 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
950 rule_file
->filename
, line
->line_number
, line
->goto_label
);
952 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
953 line
->goto_label
= NULL
;
955 if ((line
->type
& ~LINE_HAS_LABEL
) == 0) {
956 log_notice("%s:%u: The line takes no effect any more, dropping",
957 rule_file
->filename
, line
->line_number
);
958 if (line
->type
== LINE_HAS_LABEL
)
959 udev_rule_line_clear_tokens(line
);
961 udev_rule_line_free(line
);
967 static int parse_file(UdevRules
*rules
, const char *filename
) {
968 _cleanup_free_
char *continuation
= NULL
, *name
= NULL
;
969 _cleanup_fclose_
FILE *f
= NULL
;
970 UdevRuleFile
*rule_file
;
971 bool ignore_line
= false;
972 unsigned line_nr
= 0;
975 f
= fopen(filename
, "re");
983 if (null_or_empty_fd(fileno(f
))) {
984 log_debug("Skipping empty file: %s", filename
);
988 log_debug("Reading rules file: %s", filename
);
990 name
= strdup(filename
);
994 rule_file
= new(UdevRuleFile
, 1);
998 *rule_file
= (UdevRuleFile
) {
999 .filename
= TAKE_PTR(name
),
1002 if (rules
->current_file
)
1003 LIST_APPEND(rule_files
, rules
->current_file
, rule_file
);
1005 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1007 rules
->current_file
= rule_file
;
1010 _cleanup_free_
char *buf
= NULL
;
1014 r
= read_line(f
, UTIL_LINE_SIZE
, &buf
);
1021 line
= skip_leading_chars(buf
, NULL
);
1028 if (continuation
&& !ignore_line
) {
1029 if (strlen(continuation
) + len
>= UTIL_LINE_SIZE
)
1032 if (!strextend(&continuation
, line
, NULL
))
1036 line
= continuation
;
1041 if (len
> 0 && line
[len
- 1] == '\\') {
1045 line
[len
- 1] = '\0';
1046 if (!continuation
) {
1047 continuation
= strdup(line
);
1056 log_error("%s:%u: Line is too long, ignored", filename
, line_nr
);
1058 (void) rule_add_line(rules
, line
, line_nr
);
1060 continuation
= mfree(continuation
);
1061 ignore_line
= false;
1064 rule_resolve_goto(rule_file
);
1068 int udev_rules_new(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1069 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1070 _cleanup_strv_free_
char **files
= NULL
;
1074 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1076 rules
= new(UdevRules
, 1);
1080 *rules
= (UdevRules
) {
1081 .resolve_name_timing
= resolve_name_timing
,
1084 (void) udev_rules_check_timestamp(rules
);
1086 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1088 return log_error_errno(r
, "Failed to enumerate rules files: %m");
1090 STRV_FOREACH(f
, files
)
1091 (void) parse_file(rules
, *f
);
1093 *ret_rules
= TAKE_PTR(rules
);
1097 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1101 return paths_check_timestamp(RULES_DIRS
, &rules
->dirs_ts_usec
, true);
1104 static bool token_match_string(UdevRuleToken
*token
, const char *str
) {
1105 const char *i
, *value
;
1109 assert(token
->value
);
1110 assert(token
->type
< _TK_M_MAX
);
1112 str
= strempty(str
);
1113 value
= token
->value
;
1115 switch (token
->match_type
) {
1116 case MATCH_TYPE_EMPTY
:
1117 match
= isempty(str
);
1119 case MATCH_TYPE_SUBSYSTEM
:
1120 value
= "subsystem\0class\0bus\0";
1122 case MATCH_TYPE_PLAIN
:
1123 NULSTR_FOREACH(i
, value
)
1124 if (streq(i
, str
)) {
1129 case MATCH_TYPE_GLOB
:
1130 NULSTR_FOREACH(i
, value
)
1131 if ((fnmatch(i
, str
, 0) == 0)) {
1137 assert_not_reached("Invalid match type");
1140 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1143 static bool token_match_attr(UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
1144 char nbuf
[UTIL_NAME_SIZE
], vbuf
[UTIL_NAME_SIZE
];
1145 const char *name
, *value
;
1151 name
= (const char*) token
->data
;
1153 switch (token
->attr_subst_type
) {
1154 case SUBST_TYPE_FORMAT
:
1155 (void) udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1158 case SUBST_TYPE_PLAIN
:
1159 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1162 case SUBST_TYPE_SUBSYS
:
1163 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1168 assert_not_reached("Invalid attribute substitution type");
1171 /* remove trailing whitespace, if not asked to match for it */
1172 if (token
->attr_match_remove_trailing_whitespace
) {
1173 if (value
!= vbuf
) {
1174 strscpy(vbuf
, sizeof(vbuf
), value
);
1178 delete_trailing_chars(vbuf
, NULL
);
1181 return token_match_string(token
, value
);
1184 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
1193 key
= skip_leading_chars(line
, NULL
);
1195 /* comment or empty line */
1196 if (IN_SET(key
[0], '#', '\0'))
1199 /* split key/value */
1200 val
= strchr(key
, '=');
1205 key
= strstrip(key
);
1209 val
= strstrip(val
);
1214 if (IN_SET(val
[0], '"', '\'')) {
1216 if (len
== 1 || val
[len
-1] != val
[0])
1227 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
1228 const char *key
, *val
;
1235 r
= sd_device_get_parent(dev
, &parent
);
1241 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
1242 if (fnmatch(filter
, key
, 0) != 0)
1244 r
= device_add_property(dev
, key
, val
);
1252 static int attr_subst_subdir(char attr
[static UTIL_PATH_SIZE
]) {
1253 _cleanup_closedir_
DIR *dir
= NULL
;
1254 struct dirent
*dent
;
1255 char buf
[UTIL_PATH_SIZE
], *p
;
1259 tail
= strstr(attr
, "/*/");
1263 len
= tail
- attr
+ 1; /* include slash at the end */
1264 tail
+= 2; /* include slash at the beginning */
1268 size
-= strnpcpy(&p
, size
, attr
, len
);
1274 FOREACH_DIRENT_ALL(dent
, dir
, break) {
1275 if (dent
->d_name
[0] == '.')
1278 strscpyl(p
, size
, dent
->d_name
, tail
, NULL
);
1279 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
1289 static int udev_rule_apply_token_to_event(
1293 usec_t timeout_usec
,
1294 Hashmap
*properties_list
) {
1296 UdevRuleToken
*token
;
1297 char buf
[UTIL_PATH_SIZE
];
1307 /* This returns the following values:
1308 * 0 on the current token does not match the event,
1309 * 1 on the current token matches the event, and
1310 * negative errno on some critical errors. */
1312 token
= rules
->current_file
->current_line
->current_token
;
1314 switch (token
->type
) {
1318 r
= device_get_action(dev
, &a
);
1320 return log_rule_error_errno(dev
, rules
, r
, "Failed to get uevent action type: %m");
1322 return token_match_string(token
, device_action_to_string(a
));
1325 r
= sd_device_get_devpath(dev
, &val
);
1327 return log_rule_error_errno(dev
, rules
, r
, "Failed to get devpath: %m");
1329 return token_match_string(token
, val
);
1331 case TK_M_PARENTS_KERNEL
:
1332 r
= sd_device_get_sysname(dev
, &val
);
1334 return log_rule_error_errno(dev
, rules
, r
, "Failed to get sysname: %m");
1336 return token_match_string(token
, val
);
1338 FOREACH_DEVICE_DEVLINK(dev
, val
)
1339 if (token_match_string(token
, strempty(startswith(val
, "/dev/"))))
1340 return token
->op
== OP_MATCH
;
1341 return token
->op
== OP_NOMATCH
;
1343 return token_match_string(token
, event
->name
);
1345 if (sd_device_get_property_value(dev
, (const char*) token
->data
, &val
) < 0)
1346 val
= hashmap_get(properties_list
, token
->data
);
1348 return token_match_string(token
, val
);
1350 case TK_M_PARENTS_TAG
:
1351 FOREACH_DEVICE_TAG(dev
, val
)
1352 if (token_match_string(token
, val
))
1353 return token
->op
== OP_MATCH
;
1354 return token
->op
== OP_NOMATCH
;
1355 case TK_M_SUBSYSTEM
:
1356 case TK_M_PARENTS_SUBSYSTEM
:
1357 r
= sd_device_get_subsystem(dev
, &val
);
1361 return log_rule_error_errno(dev
, rules
, r
, "Failed to get subsystem: %m");
1363 return token_match_string(token
, val
);
1365 case TK_M_PARENTS_DRIVER
:
1366 r
= sd_device_get_driver(dev
, &val
);
1370 return log_rule_error_errno(dev
, rules
, r
, "Failed to get driver: %m");
1372 return token_match_string(token
, val
);
1374 case TK_M_PARENTS_ATTR
:
1375 return token_match_attr(token
, dev
, event
);
1377 _cleanup_free_
char *value
= NULL
;
1379 (void) udev_event_apply_format(event
, (const char*) token
->data
, buf
, sizeof(buf
), false);
1380 r
= sysctl_read(sysctl_normalize(buf
), &value
);
1381 if (r
< 0 && r
!= -ENOENT
)
1382 return log_rule_error_errno(dev
, rules
, r
, "Failed to read sysctl '%s': %m", buf
);
1384 return token_match_string(token
, strstrip(value
));
1387 mode_t mode
= PTR_TO_MODE(token
->data
);
1388 struct stat statbuf
;
1390 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1391 if (!path_is_absolute(buf
) &&
1392 util_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
1393 char tmp
[UTIL_PATH_SIZE
];
1395 r
= sd_device_get_syspath(dev
, &val
);
1397 return log_rule_error_errno(dev
, rules
, r
, "Failed to get syspath: %m");
1399 strscpy(tmp
, sizeof(tmp
), buf
);
1400 strscpyl(buf
, sizeof(buf
), val
, "/", tmp
, NULL
);
1403 r
= attr_subst_subdir(buf
);
1405 return token
->op
== OP_NOMATCH
;
1407 return log_rule_error_errno(dev
, rules
, r
, "Failed to test the existence of '%s': %m", buf
);
1409 if (stat(buf
, &statbuf
) < 0)
1410 return token
->op
== OP_NOMATCH
;
1412 if (mode
== MODE_INVALID
)
1413 return token
->op
== OP_MATCH
;
1415 match
= (((statbuf
.st_mode
^ mode
) & 07777) == 0);
1416 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1418 case TK_M_PROGRAM
: {
1419 char result
[UTIL_LINE_SIZE
];
1421 event
->program_result
= mfree(event
->program_result
);
1422 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1423 log_rule_debug(dev
, rules
, "Running PROGRAM '%s'", buf
);
1425 r
= udev_event_spawn(event
, timeout_usec
, true, buf
, result
, sizeof(result
));
1427 return log_rule_error_errno(dev
, rules
, r
, "Failed to execute '%s': %m", buf
);
1429 return token
->op
== OP_NOMATCH
;
1431 delete_trailing_chars(result
, "\n");
1432 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1434 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of '%s'",
1437 event
->program_result
= strdup(result
);
1438 return token
->op
== OP_MATCH
;
1440 case TK_M_IMPORT_FILE
: {
1441 _cleanup_fclose_
FILE *f
= NULL
;
1443 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1444 log_rule_debug(dev
, rules
, "Importing properties from '%s'", buf
);
1446 f
= fopen(buf
, "re");
1448 if (errno
!= ENOENT
)
1449 return log_rule_error_errno(dev
, rules
, errno
,
1450 "Failed to open '%s': %m", buf
);
1451 return token
->op
== OP_NOMATCH
;
1455 _cleanup_free_
char *line
= NULL
;
1458 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1460 log_rule_debug_errno(dev
, rules
, r
,
1461 "Failed to read '%s', ignoring: %m", buf
);
1462 return token
->op
== OP_NOMATCH
;
1467 r
= get_property_from_string(line
, &key
, &value
);
1469 log_rule_debug_errno(dev
, rules
, r
,
1470 "Failed to parse key and value from '%s', ignoring: %m",
1475 r
= device_add_property(dev
, key
, value
);
1477 return log_rule_error_errno(dev
, rules
, r
,
1478 "Failed to add property %s=%s: %m",
1482 return token
->op
== OP_MATCH
;
1484 case TK_M_IMPORT_PROGRAM
: {
1485 char result
[UTIL_LINE_SIZE
], *line
, *pos
;
1487 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1488 log_rule_debug(dev
, rules
, "Importing properties from results of '%s'", buf
);
1490 r
= udev_event_spawn(event
, timeout_usec
, true, buf
, result
, sizeof result
);
1492 return log_rule_error_errno(dev
, rules
, r
, "Failed to execute '%s': %m", buf
);
1494 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
1495 return token
->op
== OP_NOMATCH
;
1498 for (line
= result
; !isempty(line
); line
= pos
) {
1501 pos
= strchr(line
, '\n');
1505 r
= get_property_from_string(line
, &key
, &value
);
1507 log_rule_debug_errno(dev
, rules
, r
,
1508 "Failed to parse key and value from '%s', ignoring: %m",
1513 r
= device_add_property(dev
, key
, value
);
1515 return log_rule_error_errno(dev
, rules
, r
,
1516 "Failed to add property %s=%s: %m",
1520 return token
->op
== OP_MATCH
;
1522 case TK_M_IMPORT_BUILTIN
: {
1523 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
1524 unsigned mask
= 1U << (int) cmd
;
1526 if (udev_builtin_run_once(cmd
)) {
1527 /* check if we ran already */
1528 if (event
->builtin_run
& mask
) {
1529 log_rule_debug(dev
, rules
, "Skipping builtin '%s' in IMPORT key",
1530 udev_builtin_name(cmd
));
1531 /* return the result from earlier run */
1532 return token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
);
1535 event
->builtin_run
|= mask
;
1538 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1539 log_rule_debug(dev
, rules
, "Importing properties from results of builtin command '%s'", buf
);
1541 r
= udev_builtin_run(dev
, cmd
, buf
, false);
1543 /* remember failure */
1544 log_rule_debug_errno(dev
, rules
, r
, "Failed to run builtin '%s': %m", buf
);
1545 event
->builtin_ret
|= mask
;
1547 return token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
);
1549 case TK_M_IMPORT_DB
: {
1550 if (!event
->dev_db_clone
)
1551 return token
->op
== OP_NOMATCH
;
1552 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
1554 return token
->op
== OP_NOMATCH
;
1556 return log_rule_error_errno(dev
, rules
, r
,
1557 "Failed to get property '%s' from database: %m",
1560 r
= device_add_property(dev
, token
->value
, val
);
1562 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1564 return token
->op
== OP_MATCH
;
1566 case TK_M_IMPORT_CMDLINE
: {
1567 _cleanup_free_
char *value
= NULL
;
1569 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
1571 return log_rule_error_errno(dev
, rules
, r
,
1572 "Failed to read '%s' option from /proc/cmdline: %m",
1575 return token
->op
== OP_NOMATCH
;
1577 r
= device_add_property(dev
, token
->value
, value
?: "1");
1579 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1580 token
->value
, value
?: "1");
1581 return token
->op
== OP_MATCH
;
1583 case TK_M_IMPORT_PARENT
: {
1584 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1585 r
= import_parent_into_properties(dev
, buf
);
1587 return log_rule_error_errno(dev
, rules
, r
,
1588 "Failed to import properties '%s' from parent: %m",
1590 return token
->op
== (r
> 0 ? OP_MATCH
: OP_NOMATCH
);
1593 return token_match_string(token
, event
->program_result
);
1594 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
1595 event
->esc
= ESCAPE_NONE
;
1597 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
1598 event
->esc
= ESCAPE_REPLACE
;
1600 case TK_A_OPTIONS_DB_PERSIST
:
1601 device_set_db_persist(dev
);
1603 case TK_A_OPTIONS_INOTIFY_WATCH
:
1604 if (event
->inotify_watch_final
)
1606 if (token
->op
== OP_ASSIGN_FINAL
)
1607 event
->inotify_watch_final
= true;
1609 event
->inotify_watch
= token
->data
;
1611 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
1612 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
1615 char owner
[UTIL_NAME_SIZE
];
1616 const char *ow
= owner
;
1618 if (event
->owner_final
)
1620 if (token
->op
== OP_ASSIGN_FINAL
)
1621 event
->owner_final
= true;
1623 (void) udev_event_apply_format(event
, token
->value
, owner
, sizeof(owner
), false);
1624 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
1626 log_unknown_owner(dev
, rules
, r
, "user", owner
);
1628 log_rule_debug(dev
, rules
, "OWNER %s(%u)", owner
, event
->uid
);
1632 char group
[UTIL_NAME_SIZE
];
1633 const char *gr
= group
;
1635 if (event
->group_final
)
1637 if (token
->op
== OP_ASSIGN_FINAL
)
1638 event
->group_final
= true;
1640 (void) udev_event_apply_format(event
, token
->value
, group
, sizeof(group
), false);
1641 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
1643 log_unknown_owner(dev
, rules
, r
, "group", group
);
1645 log_rule_debug(dev
, rules
, "GROUP %s(%u)", group
, event
->gid
);
1649 char mode_str
[UTIL_NAME_SIZE
];
1651 if (event
->mode_final
)
1653 if (token
->op
== OP_ASSIGN_FINAL
)
1654 event
->mode_final
= true;
1656 (void) udev_event_apply_format(event
, token
->value
, mode_str
, sizeof(mode_str
), false);
1657 r
= parse_mode(mode_str
, &event
->mode
);
1659 log_rule_error_errno(dev
, rules
, r
, "Failed to parse mode '%s', ignoring: %m", mode_str
);
1661 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
1665 if (event
->owner_final
)
1667 if (token
->op
== OP_ASSIGN_FINAL
)
1668 event
->owner_final
= true;
1671 event
->uid
= PTR_TO_UID(token
->data
);
1672 log_rule_debug(dev
, rules
, "OWNER %u", event
->uid
);
1675 if (event
->group_final
)
1677 if (token
->op
== OP_ASSIGN_FINAL
)
1678 event
->group_final
= true;
1681 event
->gid
= PTR_TO_GID(token
->data
);
1682 log_rule_debug(dev
, rules
, "GROUP %u", event
->gid
);
1685 if (event
->mode_final
)
1687 if (token
->op
== OP_ASSIGN_FINAL
)
1688 event
->mode_final
= true;
1691 event
->mode
= PTR_TO_MODE(token
->data
);
1692 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
1694 case TK_A_SECLABEL
: {
1695 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
1696 char label_str
[UTIL_LINE_SIZE
] = {};
1698 name
= strdup((const char*) token
->data
);
1702 (void) udev_event_apply_format(event
, token
->value
, label_str
, sizeof(label_str
), false);
1703 if (!isempty(label_str
))
1704 label
= strdup(label_str
);
1706 label
= strdup(token
->value
);
1710 if (token
->op
== OP_ASSIGN
)
1711 ordered_hashmap_clear_free_free(event
->seclabel_list
);
1713 r
= ordered_hashmap_ensure_allocated(&event
->seclabel_list
, NULL
);
1717 r
= ordered_hashmap_put(event
->seclabel_list
, name
, label
);
1720 log_rule_debug(dev
, rules
, "SECLABEL{%s}='%s'", name
, label
);
1721 name
= label
= NULL
;
1725 const char *name
= (const char*) token
->data
;
1726 char value_new
[UTIL_NAME_SIZE
], *p
= value_new
;
1727 size_t l
= sizeof(value_new
);
1729 if (isempty(token
->value
)) {
1730 if (token
->op
== OP_ADD
)
1732 r
= device_add_property(dev
, name
, NULL
);
1734 return log_rule_error_errno(dev
, rules
, r
, "Failed to remove property '%s': %m", name
);
1738 if (token
->op
== OP_ADD
&&
1739 sd_device_get_property_value(dev
, name
, &val
) >= 0)
1740 l
= strpcpyl(&p
, l
, val
, " ", NULL
);
1742 (void) udev_event_apply_format(event
, token
->value
, p
, l
, false);
1744 r
= device_add_property(dev
, name
, value_new
);
1746 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m", name
, value_new
);
1750 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1751 if (token
->op
== OP_ASSIGN
)
1752 device_cleanup_tags(dev
);
1754 if (buf
[strspn(buf
, ALPHANUMERICAL
"-_")] != '\0') {
1755 log_rule_error(dev
, rules
, "Invalid tag name '%s', ignoring", buf
);
1758 if (token
->op
== OP_REMOVE
)
1759 device_remove_tag(dev
, buf
);
1761 r
= device_add_tag(dev
, buf
);
1763 return log_rule_error_errno(dev
, rules
, r
, "Failed to add tag '%s': %m", buf
);
1768 if (event
->name_final
)
1770 if (token
->op
== OP_ASSIGN_FINAL
)
1771 event
->name_final
= true;
1773 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1774 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1775 count
= util_replace_chars(buf
, "/");
1777 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of NAME=\"%s\"",
1778 count
, token
->value
);
1780 if (sd_device_get_devnum(dev
, NULL
) >= 0 &&
1781 (sd_device_get_devname(dev
, &val
) < 0 ||
1782 !streq_ptr(buf
, startswith(val
, "/dev/")))) {
1783 log_rule_error(dev
, rules
,
1784 "Kernel device nodes cannot be renamed, ignoring NAME=\"%s\"; please fix it.",
1788 if (free_and_strdup(&event
->name
, buf
) < 0)
1791 log_rule_debug(dev
, rules
, "NAME '%s'", event
->name
);
1794 case TK_A_DEVLINK
: {
1797 if (event
->devlink_final
)
1799 if (sd_device_get_devnum(dev
, NULL
) < 0)
1801 if (token
->op
== OP_ASSIGN_FINAL
)
1802 event
->devlink_final
= true;
1803 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
1804 device_cleanup_devlinks(dev
);
1806 /* allow multiple symlinks separated by spaces */
1807 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), event
->esc
!= ESCAPE_NONE
);
1808 if (event
->esc
== ESCAPE_UNSET
)
1809 count
= util_replace_chars(buf
, "/ ");
1810 else if (event
->esc
== ESCAPE_REPLACE
)
1811 count
= util_replace_chars(buf
, "/");
1815 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of LINK", count
);
1817 p
= skip_leading_chars(buf
, NULL
);
1818 while (!isempty(p
)) {
1819 char filename
[UTIL_PATH_SIZE
], *next
;
1821 next
= strchr(p
, ' ');
1824 next
= skip_leading_chars(next
, NULL
);
1827 strscpyl(filename
, sizeof(filename
), "/dev/", p
, NULL
);
1828 r
= device_add_devlink(dev
, filename
);
1830 return log_rule_error_errno(dev
, rules
, r
, "Failed to add devlink '%s': %m", filename
);
1832 log_rule_debug(dev
, rules
, "LINK '%s'", p
);
1838 const char *key_name
= (const char*) token
->data
;
1839 char value
[UTIL_NAME_SIZE
];
1841 if (util_resolve_subsys_kernel(key_name
, buf
, sizeof(buf
), false) < 0 &&
1842 sd_device_get_syspath(dev
, &val
) >= 0)
1843 strscpyl(buf
, sizeof(buf
), val
, "/", key_name
, NULL
);
1845 r
= attr_subst_subdir(buf
);
1847 log_rule_error_errno(dev
, rules
, r
, "Could not find file matches '%s', ignoring: %m", buf
);
1850 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false);
1852 log_rule_debug(dev
, rules
, "ATTR '%s' writing '%s'", buf
, value
);
1853 r
= write_string_file(buf
, value
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1855 log_rule_error_errno(dev
, rules
, r
, "Failed to write ATTR{%s}, ignoring: %m", buf
);
1859 char value
[UTIL_NAME_SIZE
];
1861 (void) udev_event_apply_format(event
, (const char*) token
->data
, buf
, sizeof(buf
), false);
1862 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false);
1863 sysctl_normalize(buf
);
1864 log_rule_debug(dev
, rules
, "SYSCTL '%s' writing '%s'", buf
, value
);
1865 r
= sysctl_write(buf
, value
);
1867 log_rule_error_errno(dev
, rules
, r
, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf
, value
);
1870 case TK_A_RUN_BUILTIN
:
1871 case TK_A_RUN_PROGRAM
: {
1872 _cleanup_free_
char *cmd
= NULL
;
1874 if (event
->run_final
)
1876 if (token
->op
== OP_ASSIGN_FINAL
)
1877 event
->run_final
= true;
1879 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
1880 ordered_hashmap_clear_free_key(event
->run_list
);
1882 r
= ordered_hashmap_ensure_allocated(&event
->run_list
, NULL
);
1886 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1892 r
= ordered_hashmap_put(event
->run_list
, cmd
, token
->data
);
1898 log_rule_debug(dev
, rules
, "RUN '%s'", token
->value
);
1901 case TK_A_OPTIONS_STATIC_NODE
:
1902 /* do nothing for events. */
1905 assert_not_reached("Invalid token type");
1911 static bool token_is_for_parents(UdevRuleToken
*token
) {
1912 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
1915 static int udev_rule_apply_parent_token_to_event(
1920 UdevRuleToken
*head
;
1923 line
= rules
->current_file
->current_line
;
1924 head
= rules
->current_file
->current_line
->current_token
;
1925 event
->dev_parent
= event
->dev
;
1927 LIST_FOREACH(tokens
, line
->current_token
, head
) {
1928 if (!token_is_for_parents(line
->current_token
))
1929 return true; /* All parent tokens match. */
1930 r
= udev_rule_apply_token_to_event(rules
, event
->dev_parent
, event
, 0, NULL
);
1936 if (!line
->current_token
)
1937 /* All parent tokens match. But no assign tokens in the line. Hmm... */
1940 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
1941 event
->dev_parent
= NULL
;
1947 static int udev_rule_apply_line_to_event(
1950 usec_t timeout_usec
,
1951 Hashmap
*properties_list
,
1952 UdevRuleLine
**next_line
) {
1954 UdevRuleLine
*line
= rules
->current_file
->current_line
;
1955 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
1956 UdevRuleToken
*token
, *next_token
;
1957 bool parents_done
= false;
1958 DeviceAction action
;
1961 r
= device_get_action(event
->dev
, &action
);
1965 if (action
!= DEVICE_ACTION_REMOVE
) {
1966 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
1967 mask
|= LINE_HAS_DEVLINK
;
1969 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
1970 mask
|= LINE_HAS_NAME
;
1973 if ((line
->type
& mask
) == 0)
1976 event
->esc
= ESCAPE_UNSET
;
1977 LIST_FOREACH_SAFE(tokens
, token
, next_token
, line
->tokens
) {
1978 line
->current_token
= token
;
1980 if (token_is_for_parents(token
)) {
1984 r
= udev_rule_apply_parent_token_to_event(rules
, event
);
1988 parents_done
= true;
1992 r
= udev_rule_apply_token_to_event(rules
, event
->dev
, event
, timeout_usec
, properties_list
);
1997 if (line
->goto_line
)
1998 *next_line
= line
->goto_line
;
2003 int udev_rules_apply_to_event(
2006 usec_t timeout_usec
,
2007 Hashmap
*properties_list
) {
2010 UdevRuleLine
*next_line
;
2016 LIST_FOREACH(rule_files
, file
, rules
->rule_files
) {
2017 rules
->current_file
= file
;
2018 LIST_FOREACH_SAFE(rule_lines
, file
->current_line
, next_line
, file
->rule_lines
) {
2019 r
= udev_rule_apply_line_to_event(rules
, event
, timeout_usec
, properties_list
, &next_line
);
2028 static int apply_static_dev_perms(const char *devnode
, uid_t uid
, gid_t gid
, mode_t mode
, char **tags
) {
2029 char device_node
[UTIL_PATH_SIZE
], tags_dir
[UTIL_PATH_SIZE
], tag_symlink
[UTIL_PATH_SIZE
];
2030 _cleanup_free_
char *unescaped_filename
= NULL
;
2037 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
&& !tags
)
2040 strscpyl(device_node
, sizeof(device_node
), "/dev/", devnode
, NULL
);
2041 if (stat(device_node
, &stats
) < 0) {
2042 if (errno
!= ENOENT
)
2043 return log_error_errno(errno
, "Failed to stat %s: %m", device_node
);
2047 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
)) {
2048 log_warning("%s is neither block nor character device, ignoring.", device_node
);
2052 if (!strv_isempty(tags
)) {
2053 unescaped_filename
= xescape(devnode
, "/.");
2054 if (!unescaped_filename
)
2058 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2059 STRV_FOREACH(t
, tags
) {
2060 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2061 r
= mkdir_p(tags_dir
, 0755);
2063 return log_error_errno(r
, "Failed to create %s: %m", tags_dir
);
2065 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2066 r
= symlink(device_node
, tag_symlink
);
2067 if (r
< 0 && errno
!= EEXIST
)
2068 return log_error_errno(errno
, "Failed to create symlink %s -> %s: %m",
2069 tag_symlink
, device_node
);
2072 /* don't touch the permissions if only the tags were set */
2073 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
)
2076 if (mode
== MODE_INVALID
)
2077 mode
= gid_is_valid(gid
) ? 0660 : 0600;
2078 if (!uid_is_valid(uid
))
2080 if (!gid_is_valid(gid
))
2083 r
= chmod_and_chown(device_node
, mode
, uid
, gid
);
2085 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2086 device_node
, uid
, gid
);
2088 log_debug("chown '%s' %u:%u", device_node
, uid
, gid
);
2090 (void) utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2094 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
2095 UdevRuleToken
*token
;
2096 _cleanup_free_
char **tags
= NULL
;
2097 uid_t uid
= UID_INVALID
;
2098 gid_t gid
= GID_INVALID
;
2099 mode_t mode
= MODE_INVALID
;
2104 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
2107 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
2108 if (token
->type
== TK_A_OWNER_ID
)
2109 uid
= PTR_TO_UID(token
->data
);
2110 else if (token
->type
== TK_A_GROUP_ID
)
2111 gid
= PTR_TO_GID(token
->data
);
2112 else if (token
->type
== TK_A_MODE_ID
)
2113 mode
= PTR_TO_MODE(token
->data
);
2114 else if (token
->type
== TK_A_TAG
) {
2115 r
= strv_extend(&tags
, token
->value
);
2118 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
2119 r
= apply_static_dev_perms(token
->value
, uid
, gid
, mode
, tags
);
2127 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2134 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2135 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
2136 r
= udev_rule_line_apply_static_dev_perms(line
);