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"
19 #include "nulstr-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "proc-cmdline.h"
23 #include "stat-util.h"
26 #include "sysctl-util.h"
27 #include "udev-builtin.h"
28 #include "udev-event.h"
29 #include "udev-rules.h"
30 #include "user-util.h"
32 #define RULES_DIRS (const char* const*) CONF_PATHS_STRV("udev/rules.d")
41 OP_ASSIGN_FINAL
, /* := */
44 } UdevRuleOperatorType
;
47 MATCH_TYPE_EMPTY
, /* empty string */
48 MATCH_TYPE_PLAIN
, /* no special characters */
49 MATCH_TYPE_GLOB
, /* shell globs ?,*,[] */
50 MATCH_TYPE_SUBSYSTEM
, /* "subsystem", "bus", or "class" */
52 _MATCH_TYPE_INVALID
= -1
56 SUBST_TYPE_PLAIN
, /* no substitution */
57 SUBST_TYPE_FORMAT
, /* % or $ */
58 SUBST_TYPE_SUBSYS
, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
60 _SUBST_TYPE_INVALID
= -1
61 } UdevRuleSubstituteType
;
64 /* lvalues which take match or nomatch operator */
65 TK_M_ACTION
, /* string, device_get_action() */
66 TK_M_DEVPATH
, /* path, sd_device_get_devpath() */
67 TK_M_KERNEL
, /* string, sd_device_get_sysname() */
68 TK_M_DEVLINK
, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */
69 TK_M_NAME
, /* string, name of network interface */
70 TK_M_ENV
, /* string, device property, takes key through attribute */
71 TK_M_TAG
, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */
72 TK_M_SUBSYSTEM
, /* string, sd_device_get_subsystem() */
73 TK_M_DRIVER
, /* string, sd_device_get_driver() */
74 TK_M_ATTR
, /* string, takes filename through attribute, sd_device_get_sysattr_value(), util_resolve_subsys_kernel(), etc. */
75 TK_M_SYSCTL
, /* string, takes kernel parameter through attribute */
77 /* matches parent paramters */
78 TK_M_PARENTS_KERNEL
, /* string */
79 TK_M_PARENTS_SUBSYSTEM
, /* string */
80 TK_M_PARENTS_DRIVER
, /* string */
81 TK_M_PARENTS_ATTR
, /* string */
82 TK_M_PARENTS_TAG
, /* strv */
84 TK_M_TEST
, /* path, optionally mode_t can be specified by attribute, test the existence of a file */
85 TK_M_PROGRAM
, /* string, execute a program */
86 TK_M_IMPORT_FILE
, /* path */
87 TK_M_IMPORT_PROGRAM
, /* string, import properties from the result of program */
88 TK_M_IMPORT_BUILTIN
, /* string, import properties from the result of built-in command */
89 TK_M_IMPORT_DB
, /* string, import properties from database */
90 TK_M_IMPORT_CMDLINE
, /* string, kernel command line */
91 TK_M_IMPORT_PARENT
, /* string, parent property */
92 TK_M_RESULT
, /* string, result of TK_M_PROGRAM */
94 #define _TK_M_MAX (TK_M_RESULT + 1)
95 #define _TK_A_MIN _TK_M_MAX
97 /* lvalues which take one of assign operators */
98 TK_A_OPTIONS_STRING_ESCAPE_NONE
, /* no argument */
99 TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, /* no argument */
100 TK_A_OPTIONS_DB_PERSIST
, /* no argument */
101 TK_A_OPTIONS_INOTIFY_WATCH
, /* boolean */
102 TK_A_OPTIONS_DEVLINK_PRIORITY
, /* int */
103 TK_A_OWNER
, /* user name */
104 TK_A_GROUP
, /* group name */
105 TK_A_MODE
, /* mode string */
106 TK_A_OWNER_ID
, /* uid_t */
107 TK_A_GROUP_ID
, /* gid_t */
108 TK_A_MODE_ID
, /* mode_t */
109 TK_A_TAG
, /* string */
110 TK_A_OPTIONS_STATIC_NODE
, /* device path, /dev/... */
111 TK_A_SECLABEL
, /* string with attribute */
112 TK_A_ENV
, /* string with attribute */
113 TK_A_NAME
, /* ifname */
114 TK_A_DEVLINK
, /* string */
115 TK_A_ATTR
, /* string with attribute */
116 TK_A_SYSCTL
, /* string with attribute */
117 TK_A_RUN_BUILTIN
, /* string */
118 TK_A_RUN_PROGRAM
, /* string */
121 _TK_TYPE_INVALID
= -1,
125 LINE_HAS_NAME
= 1 << 0, /* has NAME= */
126 LINE_HAS_DEVLINK
= 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */
127 LINE_HAS_STATIC_NODE
= 1 << 2, /* has OPTIONS=static_node */
128 LINE_HAS_GOTO
= 1 << 3, /* has GOTO= */
129 LINE_HAS_LABEL
= 1 << 4, /* has LABEL= */
130 LINE_UPDATE_SOMETHING
= 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */
133 typedef struct UdevRuleFile UdevRuleFile
;
134 typedef struct UdevRuleLine UdevRuleLine
;
135 typedef struct UdevRuleToken UdevRuleToken
;
137 struct UdevRuleToken
{
138 UdevRuleTokenType type
:8;
139 UdevRuleOperatorType op
:8;
140 UdevRuleMatchType match_type
:8;
141 UdevRuleSubstituteType attr_subst_type
:7;
142 bool attr_match_remove_trailing_whitespace
:1;
145 LIST_FIELDS(UdevRuleToken
, tokens
);
148 struct UdevRuleLine
{
150 unsigned line_number
;
151 UdevRuleLineType type
;
154 const char *goto_label
;
155 UdevRuleLine
*goto_line
;
157 UdevRuleFile
*rule_file
;
158 UdevRuleToken
*current_token
;
159 LIST_HEAD(UdevRuleToken
, tokens
);
160 LIST_FIELDS(UdevRuleLine
, rule_lines
);
163 struct UdevRuleFile
{
165 UdevRuleLine
*current_line
;
166 LIST_HEAD(UdevRuleLine
, rule_lines
);
167 LIST_FIELDS(UdevRuleFile
, rule_files
);
172 ResolveNameTiming resolve_name_timing
;
173 Hashmap
*known_users
;
174 Hashmap
*known_groups
;
175 UdevRuleFile
*current_file
;
176 LIST_HEAD(UdevRuleFile
, rule_files
);
180 /*** Logging helpers ***/
182 #define log_rule_full(device, rules, level, error, fmt, ...) \
184 UdevRules *_r = (rules); \
185 UdevRuleFile *_f = _r ? _r->current_file : NULL; \
186 UdevRuleLine *_l = _f ? _f->current_line : NULL; \
187 const char *_n = _f ? _f->filename : NULL; \
189 log_device_full(device, level, error, "%s:%u " fmt, \
190 strna(_n), _l ? _l->line_number : 0, \
194 #define log_rule_debug(device, rules, ...) log_rule_full(device, rules, LOG_DEBUG, 0, ##__VA_ARGS__)
195 #define log_rule_info(device, rules, ...) log_rule_full(device, rules, LOG_INFO, 0, ##__VA_ARGS__)
196 #define log_rule_notice(device, rules, ...) log_rule_full(device, rules, LOG_NOTICE, 0, ##__VA_ARGS__)
197 #define log_rule_warning(device, rules, ...) log_rule_full(device, rules, LOG_WARNING, 0, ##__VA_ARGS__)
198 #define log_rule_error(device, rules, ...) log_rule_full(device, rules, LOG_ERR, 0, ##__VA_ARGS__)
200 #define log_rule_debug_errno(device, rules, error, ...) log_rule_full(device, rules, LOG_DEBUG, error, ##__VA_ARGS__)
201 #define log_rule_info_errno(device, rules, error, ...) log_rule_full(device, rules, LOG_INFO, error, ##__VA_ARGS__)
202 #define log_rule_notice_errno(device, rules, error, ...) log_rule_full(device, rules, LOG_NOTICE, error, ##__VA_ARGS__)
203 #define log_rule_warning_errno(device, rules, error, ...) log_rule_full(device, rules, LOG_WARNING, error, ##__VA_ARGS__)
204 #define log_rule_error_errno(device, rules, error, ...) log_rule_full(device, rules, LOG_ERR, error, ##__VA_ARGS__)
206 #define log_token_full(rules, ...) log_rule_full(NULL, rules, ##__VA_ARGS__)
208 #define log_token_debug(rules, ...) log_token_full(rules, LOG_DEBUG, 0, ##__VA_ARGS__)
209 #define log_token_info(rules, ...) log_token_full(rules, LOG_INFO, 0, ##__VA_ARGS__)
210 #define log_token_notice(rules, ...) log_token_full(rules, LOG_NOTICE, 0, ##__VA_ARGS__)
211 #define log_token_warning(rules, ...) log_token_full(rules, LOG_WARNING, 0, ##__VA_ARGS__)
212 #define log_token_error(rules, ...) log_token_full(rules, LOG_ERR, 0, ##__VA_ARGS__)
214 #define log_token_debug_errno(rules, error, ...) log_token_full(rules, LOG_DEBUG, error, ##__VA_ARGS__)
215 #define log_token_info_errno(rules, error, ...) log_token_full(rules, LOG_INFO, error, ##__VA_ARGS__)
216 #define log_token_notice_errno(rules, error, ...) log_token_full(rules, LOG_NOTICE, error, ##__VA_ARGS__)
217 #define log_token_warning_errno(rules, error, ...) log_token_full(rules, LOG_WARNING, error, ##__VA_ARGS__)
218 #define log_token_error_errno(rules, error, ...) log_token_full(rules, LOG_ERR, error, ##__VA_ARGS__)
220 #define _log_token_invalid(rules, key, type) \
221 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
222 "Invalid %s for %s.", type, key)
224 #define log_token_invalid_op(rules, key) _log_token_invalid(rules, key, "operator")
225 #define log_token_invalid_attr(rules, key) _log_token_invalid(rules, key, "attribute")
227 #define log_token_invalid_attr_format(rules, key, attr, offset, hint) \
228 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
229 "Invalid attribute \"%s\" for %s (char %zu: %s), ignoring, but please fix it.", \
230 attr, key, offset, hint)
231 #define log_token_invalid_value(rules, key, value, offset, hint) \
232 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
233 "Invalid value \"%s\" for %s (char %zu: %s), ignoring, but please fix it.", \
234 value, key, offset, hint)
236 static void log_unknown_owner(sd_device
*dev
, UdevRules
*rules
, int error
, const char *entity
, const char *name
) {
237 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
238 log_rule_error(dev
, rules
, "Unknown %s '%s', ignoring", entity
, name
);
240 log_rule_error_errno(dev
, rules
, error
, "Failed to resolve %s '%s', ignoring: %m", entity
, name
);
243 /*** Other functions ***/
245 static void udev_rule_token_free(UdevRuleToken
*token
) {
249 static void udev_rule_line_clear_tokens(UdevRuleLine
*rule_line
) {
250 UdevRuleToken
*i
, *next
;
254 LIST_FOREACH_SAFE(tokens
, i
, next
, rule_line
->tokens
)
255 udev_rule_token_free(i
);
257 rule_line
->tokens
= NULL
;
260 static void udev_rule_line_free(UdevRuleLine
*rule_line
) {
264 udev_rule_line_clear_tokens(rule_line
);
266 if (rule_line
->rule_file
) {
267 if (rule_line
->rule_file
->current_line
== rule_line
)
268 rule_line
->rule_file
->current_line
= rule_line
->rule_lines_prev
;
270 LIST_REMOVE(rule_lines
, rule_line
->rule_file
->rule_lines
, rule_line
);
273 free(rule_line
->line
);
277 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine
*, udev_rule_line_free
);
279 static void udev_rule_file_free(UdevRuleFile
*rule_file
) {
280 UdevRuleLine
*i
, *next
;
285 LIST_FOREACH_SAFE(rule_lines
, i
, next
, rule_file
->rule_lines
)
286 udev_rule_line_free(i
);
288 free(rule_file
->filename
);
292 UdevRules
*udev_rules_free(UdevRules
*rules
) {
293 UdevRuleFile
*i
, *next
;
298 LIST_FOREACH_SAFE(rule_files
, i
, next
, rules
->rule_files
)
299 udev_rule_file_free(i
);
301 hashmap_free_free_key(rules
->known_users
);
302 hashmap_free_free_key(rules
->known_groups
);
306 static int rule_resolve_user(UdevRules
*rules
, const char *name
, uid_t
*ret
) {
307 _cleanup_free_
char *n
= NULL
;
315 val
= hashmap_get(rules
->known_users
, name
);
317 *ret
= PTR_TO_UID(val
);
321 r
= get_user_creds(&name
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
323 log_unknown_owner(NULL
, rules
, r
, "user", name
);
332 r
= hashmap_ensure_allocated(&rules
->known_users
, &string_hash_ops
);
336 r
= hashmap_put(rules
->known_users
, n
, UID_TO_PTR(uid
));
345 static int rule_resolve_group(UdevRules
*rules
, const char *name
, gid_t
*ret
) {
346 _cleanup_free_
char *n
= NULL
;
354 val
= hashmap_get(rules
->known_groups
, name
);
356 *ret
= PTR_TO_GID(val
);
360 r
= get_group_creds(&name
, &gid
, USER_CREDS_ALLOW_MISSING
);
362 log_unknown_owner(NULL
, rules
, r
, "group", name
);
371 r
= hashmap_ensure_allocated(&rules
->known_groups
, &string_hash_ops
);
375 r
= hashmap_put(rules
->known_groups
, n
, GID_TO_PTR(gid
));
384 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
388 return SUBST_TYPE_SUBSYS
;
389 if (strchr(str
, '%') || strchr(str
, '$'))
390 return SUBST_TYPE_FORMAT
;
391 return SUBST_TYPE_PLAIN
;
394 static void rule_line_append_token(UdevRuleLine
*rule_line
, UdevRuleToken
*token
) {
398 if (rule_line
->current_token
)
399 LIST_APPEND(tokens
, rule_line
->current_token
, token
);
401 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
403 rule_line
->current_token
= token
;
406 static int rule_line_add_token(UdevRuleLine
*rule_line
, UdevRuleTokenType type
, UdevRuleOperatorType op
, char *value
, void *data
) {
407 UdevRuleToken
*token
;
408 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
409 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
410 bool remove_trailing_whitespace
= false;
414 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
415 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
417 if (type
< _TK_M_MAX
) {
419 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
421 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
422 match_type
= MATCH_TYPE_SUBSYSTEM
;
423 else if (isempty(value
))
424 match_type
= MATCH_TYPE_EMPTY
;
425 else if (streq(value
, "?*")) {
426 /* Convert KEY=="?*" -> KEY!="" */
427 match_type
= MATCH_TYPE_EMPTY
;
428 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
429 } else if (string_is_glob(value
))
430 match_type
= MATCH_TYPE_GLOB
;
432 match_type
= MATCH_TYPE_PLAIN
;
434 if (type
< TK_M_TEST
|| type
== TK_M_RESULT
) {
435 /* Convert value string to nulstr. */
437 if (len
> 1 && (value
[len
- 1] == '|' || strstr(value
, "||"))) {
438 /* In this case, just replacing '|' -> '\0' does not work... */
439 _cleanup_free_
char *tmp
= NULL
;
447 for (i
= tmp
, j
= value
; *i
!= '\0'; i
++)
459 /* Simple conversion. */
462 for (i
= value
; *i
!= '\0'; i
++)
469 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
474 if (len
> 0 && !isspace(value
[len
- 1]))
475 remove_trailing_whitespace
= true;
477 subst_type
= rule_get_substitution_type((const char*) data
);
480 token
= new(UdevRuleToken
, 1);
484 *token
= (UdevRuleToken
) {
489 .match_type
= match_type
,
490 .attr_subst_type
= subst_type
,
491 .attr_match_remove_trailing_whitespace
= remove_trailing_whitespace
,
494 rule_line_append_token(rule_line
, token
);
496 if (token
->type
== TK_A_NAME
)
497 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
499 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
500 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
501 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
502 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
504 else if (token
->type
>= _TK_A_MIN
||
506 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
507 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
508 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
513 static void check_value_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
, bool nonempty
) {
517 if (nonempty
&& isempty(value
))
518 log_token_invalid_value(rules
, key
, value
, (size_t) 0, "empty value");
519 else if (udev_check_format(value
, &offset
, &hint
) < 0)
520 log_token_invalid_value(rules
, key
, value
, offset
+ 1, hint
);
523 static int check_attr_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
) {
528 return log_token_invalid_attr(rules
, key
);
529 if (udev_check_format(value
, &offset
, &hint
) < 0)
530 log_token_invalid_attr_format(rules
, key
, value
, offset
+ 1, hint
);
534 static int parse_token(UdevRules
*rules
, const char *key
, char *attr
, UdevRuleOperatorType op
, char *value
) {
535 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
536 UdevRuleLine
*rule_line
;
540 assert(rules
->current_file
);
541 assert(rules
->current_file
->current_line
);
545 rule_line
= rules
->current_file
->current_line
;
547 if (streq(key
, "ACTION")) {
549 return log_token_invalid_attr(rules
, key
);
551 return log_token_invalid_op(rules
, key
);
553 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
);
554 } else if (streq(key
, "DEVPATH")) {
556 return log_token_invalid_attr(rules
, key
);
558 return log_token_invalid_op(rules
, key
);
560 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
);
561 } else if (streq(key
, "KERNEL")) {
563 return log_token_invalid_attr(rules
, key
);
565 return log_token_invalid_op(rules
, key
);
567 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
);
568 } else if (streq(key
, "SYMLINK")) {
570 return log_token_invalid_attr(rules
, key
);
572 return log_token_invalid_op(rules
, key
);
575 check_value_format_and_warn(rules
, key
, value
, false);
576 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
);
578 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
);
579 } else if (streq(key
, "NAME")) {
581 return log_token_invalid_attr(rules
, key
);
583 return log_token_invalid_op(rules
, key
);
585 log_token_warning(rules
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '=', but please fix it.", key
);
590 if (streq(value
, "%k"))
591 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
592 "Ignoring NAME=\"%%k\" is ignored, as it breaks kernel supplied names.");
594 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
595 "Ignoring NAME=\"\", as udev will not delete any device nodes.");
596 check_value_format_and_warn(rules
, key
, value
, false);
598 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
);
600 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
);
601 } else if (streq(key
, "ENV")) {
603 return log_token_invalid_attr(rules
, key
);
605 return log_token_invalid_op(rules
, key
);
606 if (op
== OP_ASSIGN_FINAL
) {
607 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '=', but please fix it.", key
);
613 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
614 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
615 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
616 "Invalid ENV attribute. '%s' cannot be set.", attr
);
618 check_value_format_and_warn(rules
, key
, value
, false);
620 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
);
622 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
);
623 } else if (streq(key
, "TAG")) {
625 return log_token_invalid_attr(rules
, key
);
626 if (op
== OP_ASSIGN_FINAL
) {
627 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '=', but please fix it.", key
);
632 check_value_format_and_warn(rules
, key
, value
, true);
634 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
);
636 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
);
637 } else if (streq(key
, "SUBSYSTEM")) {
639 return log_token_invalid_attr(rules
, key
);
641 return log_token_invalid_op(rules
, key
);
643 if (STR_IN_SET(value
, "bus", "class"))
644 log_token_warning(rules
, "'%s' must be specified as 'subsystem'; please fix it", value
);
646 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
647 } else if (streq(key
, "DRIVER")) {
649 return log_token_invalid_attr(rules
, key
);
651 return log_token_invalid_op(rules
, key
);
653 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
);
654 } else if (streq(key
, "ATTR")) {
655 r
= check_attr_format_and_warn(rules
, key
, attr
);
659 return log_token_invalid_op(rules
, key
);
660 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
661 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '=', but please fix it.", key
);
666 check_value_format_and_warn(rules
, key
, value
, false);
667 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
);
669 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
);
670 } else if (streq(key
, "SYSCTL")) {
671 r
= check_attr_format_and_warn(rules
, key
, attr
);
675 return log_token_invalid_op(rules
, key
);
676 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
677 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '=', but please fix it.", key
);
682 check_value_format_and_warn(rules
, key
, value
, false);
683 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
);
685 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
);
686 } else if (streq(key
, "KERNELS")) {
688 return log_token_invalid_attr(rules
, key
);
690 return log_token_invalid_op(rules
, key
);
692 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
);
693 } else if (streq(key
, "SUBSYSTEMS")) {
695 return log_token_invalid_attr(rules
, key
);
697 return log_token_invalid_op(rules
, key
);
699 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
);
700 } else if (streq(key
, "DRIVERS")) {
702 return log_token_invalid_attr(rules
, key
);
704 return log_token_invalid_op(rules
, key
);
706 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
);
707 } else if (streq(key
, "ATTRS")) {
708 r
= check_attr_format_and_warn(rules
, key
, attr
);
712 return log_token_invalid_op(rules
, key
);
714 if (startswith(attr
, "device/"))
715 log_token_warning(rules
, "'device' link may not be available in future kernels; please fix it.");
716 if (strstr(attr
, "../"))
717 log_token_warning(rules
, "Direct reference to parent sysfs directory, may break in future kernels; please fix it.");
719 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
);
720 } else if (streq(key
, "TAGS")) {
722 return log_token_invalid_attr(rules
, key
);
724 return log_token_invalid_op(rules
, key
);
726 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
);
727 } else if (streq(key
, "TEST")) {
728 mode_t mode
= MODE_INVALID
;
730 if (!isempty(attr
)) {
731 r
= parse_mode(attr
, &mode
);
733 return log_token_error_errno(rules
, r
, "Failed to parse mode '%s': %m", attr
);
735 check_value_format_and_warn(rules
, key
, value
, true);
737 return log_token_invalid_op(rules
, key
);
739 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
));
740 } else if (streq(key
, "PROGRAM")) {
742 return log_token_invalid_attr(rules
, key
);
743 check_value_format_and_warn(rules
, key
, value
, true);
745 return log_token_invalid_op(rules
, key
);
748 log_token_debug(rules
, "Operator '=' is specified to %s key, assuming '=='.", key
);
750 log_token_warning(rules
, "%s key takes '==' or '!=' operator, assuming '==', but please fix it.", key
);
754 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
);
755 } else if (streq(key
, "IMPORT")) {
757 return log_token_invalid_attr(rules
, key
);
758 check_value_format_and_warn(rules
, key
, value
, true);
760 return log_token_invalid_op(rules
, key
);
763 log_token_debug(rules
, "Operator '=' is specified to %s key, assuming '=='.", key
);
765 log_token_warning(rules
, "%s key takes '==' or '!=' operator, assuming '==', but please fix it.", key
);
769 if (streq(attr
, "file"))
770 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
771 else if (streq(attr
, "program")) {
772 UdevBuiltinCommand cmd
;
774 cmd
= udev_builtin_lookup(value
);
776 log_token_debug(rules
,"Found builtin command '%s' for %s, replacing attribute", value
, key
);
777 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
779 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
);
780 } else if (streq(attr
, "builtin")) {
781 UdevBuiltinCommand cmd
;
783 cmd
= udev_builtin_lookup(value
);
785 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
786 "Unknown builtin command: %s", value
);
787 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
788 } else if (streq(attr
, "db"))
789 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
);
790 else if (streq(attr
, "cmdline"))
791 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
792 else if (streq(attr
, "parent"))
793 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
795 return log_token_invalid_attr(rules
, key
);
796 } else if (streq(key
, "RESULT")) {
798 return log_token_invalid_attr(rules
, key
);
800 return log_token_invalid_op(rules
, key
);
802 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
);
803 } else if (streq(key
, "OPTIONS")) {
807 return log_token_invalid_attr(rules
, key
);
808 if (is_match
|| op
== OP_REMOVE
)
809 return log_token_invalid_op(rules
, key
);
811 log_token_debug(rules
, "Operator '+=' is specified to %s key, assuming '='.", key
);
815 if (streq(value
, "string_escape=none"))
816 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
817 else if (streq(value
, "string_escape=replace"))
818 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
819 else if (streq(value
, "db_persist"))
820 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
);
821 else if (streq(value
, "watch"))
822 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1));
823 else if (streq(value
, "nowatch"))
824 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0));
825 else if ((tmp
= startswith(value
, "static_node=")))
826 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
);
827 else if ((tmp
= startswith(value
, "link_priority="))) {
830 r
= safe_atoi(tmp
, &prio
);
832 return log_token_error_errno(rules
, r
, "Failed to parse link priority '%s': %m", tmp
);
833 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
));
835 log_token_warning(rules
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
838 } else if (streq(key
, "OWNER")) {
842 return log_token_invalid_attr(rules
, key
);
843 if (is_match
|| op
== OP_REMOVE
)
844 return log_token_invalid_op(rules
, key
);
846 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
850 if (parse_uid(value
, &uid
) >= 0)
851 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
852 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
853 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
854 r
= rule_resolve_user(rules
, value
, &uid
);
856 return log_token_error_errno(rules
, r
, "Failed to resolve user name '%s': %m", value
);
858 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
859 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
860 check_value_format_and_warn(rules
, key
, value
, true);
861 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
);
863 log_token_debug(rules
, "Resolving user name is disabled, ignoring %s=%s", key
, value
);
866 } else if (streq(key
, "GROUP")) {
870 return log_token_invalid_attr(rules
, key
);
871 if (is_match
|| op
== OP_REMOVE
)
872 return log_token_invalid_op(rules
, key
);
874 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
878 if (parse_gid(value
, &gid
) >= 0)
879 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
880 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
881 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
882 r
= rule_resolve_group(rules
, value
, &gid
);
884 return log_token_error_errno(rules
, r
, "Failed to resolve group name '%s': %m", value
);
886 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
887 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
888 check_value_format_and_warn(rules
, key
, value
, true);
889 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
);
891 log_token_debug(rules
, "Resolving group name is disabled, ignoring %s=%s", key
, value
);
894 } else if (streq(key
, "MODE")) {
898 return log_token_invalid_attr(rules
, key
);
899 if (is_match
|| op
== OP_REMOVE
)
900 return log_token_invalid_op(rules
, key
);
902 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
906 if (parse_mode(value
, &mode
) >= 0)
907 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
));
909 check_value_format_and_warn(rules
, key
, value
, true);
910 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
);
912 } else if (streq(key
, "SECLABEL")) {
914 return log_token_invalid_attr(rules
, key
);
915 check_value_format_and_warn(rules
, key
, value
, true);
916 if (is_match
|| op
== OP_REMOVE
)
917 return log_token_invalid_op(rules
, key
);
918 if (op
== OP_ASSIGN_FINAL
) {
919 log_token_warning(rules
, "%s key takes '=' or '+=' operator, assuming '=', but please fix it.", key
);
923 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, NULL
);
924 } else if (streq(key
, "RUN")) {
925 if (is_match
|| op
== OP_REMOVE
)
926 return log_token_invalid_op(rules
, key
);
927 check_value_format_and_warn(rules
, key
, value
, true);
928 if (!attr
|| streq(attr
, "program"))
929 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
);
930 else if (streq(attr
, "builtin")) {
931 UdevBuiltinCommand cmd
;
933 cmd
= udev_builtin_lookup(value
);
935 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
936 "Unknown builtin command '%s', ignoring", value
);
937 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
939 return log_token_invalid_attr(rules
, key
);
940 } else if (streq(key
, "GOTO")) {
942 return log_token_invalid_attr(rules
, key
);
944 return log_token_invalid_op(rules
, key
);
945 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
946 log_token_warning(rules
, "Contains multiple GOTO key, ignoring GOTO=\"%s\".", value
);
950 rule_line
->goto_label
= value
;
951 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
953 } else if (streq(key
, "LABEL")) {
955 return log_token_invalid_attr(rules
, key
);
957 return log_token_invalid_op(rules
, key
);
959 rule_line
->label
= value
;
960 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
963 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'", key
);
970 static UdevRuleOperatorType
parse_operator(const char *op
) {
973 if (startswith(op
, "=="))
975 if (startswith(op
, "!="))
977 if (startswith(op
, "+="))
979 if (startswith(op
, "-="))
981 if (startswith(op
, "="))
983 if (startswith(op
, ":="))
984 return OP_ASSIGN_FINAL
;
986 return _OP_TYPE_INVALID
;
989 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
) {
990 char *key_begin
, *key_end
, *attr
, *tmp
, *value
, *i
, *j
;
991 UdevRuleOperatorType op
;
999 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
1001 if (isempty(key_begin
))
1004 for (key_end
= key_begin
; ; key_end
++) {
1005 if (key_end
[0] == '\0')
1007 if (strchr(WHITESPACE
"={", key_end
[0]))
1009 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
1012 if (key_end
[0] == '{') {
1014 tmp
= strchr(attr
, '}');
1023 tmp
= skip_leading_chars(tmp
, NULL
);
1024 op
= parse_operator(tmp
);
1030 tmp
+= op
== OP_ASSIGN
? 1 : 2;
1031 value
= skip_leading_chars(tmp
, NULL
);
1033 /* value must be double quotated */
1034 if (value
[0] != '"')
1038 /* unescape double quotation '\"' -> '"' */
1039 for (i
= j
= value
; ; i
++, j
++) {
1044 if (i
[0] == '\\' && i
[1] == '"')
1051 *ret_key
= key_begin
;
1058 static void sort_tokens(UdevRuleLine
*rule_line
) {
1059 UdevRuleToken
*head_old
;
1063 head_old
= TAKE_PTR(rule_line
->tokens
);
1064 rule_line
->current_token
= NULL
;
1066 while (!LIST_IS_EMPTY(head_old
)) {
1067 UdevRuleToken
*t
, *min_token
= NULL
;
1069 LIST_FOREACH(tokens
, t
, head_old
)
1070 if (!min_token
|| min_token
->type
> t
->type
)
1073 LIST_REMOVE(tokens
, head_old
, min_token
);
1074 rule_line_append_token(rule_line
, min_token
);
1078 static int rule_add_line(UdevRules
*rules
, const char *line_str
, unsigned line_nr
) {
1079 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
1080 _cleanup_free_
char *line
= NULL
;
1081 UdevRuleFile
*rule_file
;
1086 assert(rules
->current_file
);
1089 rule_file
= rules
->current_file
;
1091 if (isempty(line_str
))
1094 line
= strdup(line_str
);
1098 rule_line
= new(UdevRuleLine
, 1);
1102 *rule_line
= (UdevRuleLine
) {
1103 .line
= TAKE_PTR(line
),
1104 .line_number
= line_nr
,
1105 .rule_file
= rule_file
,
1108 if (rule_file
->current_line
)
1109 LIST_APPEND(rule_lines
, rule_file
->current_line
, rule_line
);
1111 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
1113 rule_file
->current_line
= rule_line
;
1115 for (p
= rule_line
->line
; !isempty(p
); ) {
1116 char *key
, *attr
, *value
;
1117 UdevRuleOperatorType op
;
1119 r
= parse_line(&p
, &key
, &attr
, &op
, &value
);
1121 return log_token_error_errno(rules
, r
, "Invalid key/value pair, ignoring.");
1125 r
= parse_token(rules
, key
, attr
, op
, value
);
1130 if (rule_line
->type
== 0) {
1131 log_token_warning(rules
, "The line takes no effect, ignoring.");
1135 sort_tokens(rule_line
);
1136 TAKE_PTR(rule_line
);
1140 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
1141 UdevRuleLine
*line
, *line_next
, *i
;
1145 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1146 LIST_FOREACH_SAFE(rule_lines
, line
, line_next
, rule_file
->rule_lines
) {
1147 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
1150 LIST_FOREACH_AFTER(rule_lines
, i
, line
)
1151 if (streq_ptr(i
->label
, line
->goto_label
)) {
1152 line
->goto_line
= i
;
1156 if (!line
->goto_line
) {
1157 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
1158 rule_file
->filename
, line
->line_number
, line
->goto_label
);
1160 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
1161 line
->goto_label
= NULL
;
1163 if ((line
->type
& ~LINE_HAS_LABEL
) == 0) {
1164 log_notice("%s:%u: The line takes no effect any more, dropping",
1165 rule_file
->filename
, line
->line_number
);
1166 if (line
->type
== LINE_HAS_LABEL
)
1167 udev_rule_line_clear_tokens(line
);
1169 udev_rule_line_free(line
);
1175 static int parse_file(UdevRules
*rules
, const char *filename
) {
1176 _cleanup_free_
char *continuation
= NULL
, *name
= NULL
;
1177 _cleanup_fclose_
FILE *f
= NULL
;
1178 UdevRuleFile
*rule_file
;
1179 bool ignore_line
= false;
1180 unsigned line_nr
= 0;
1183 f
= fopen(filename
, "re");
1185 if (errno
== ENOENT
)
1191 if (null_or_empty_fd(fileno(f
))) {
1192 log_debug("Skipping empty file: %s", filename
);
1196 log_debug("Reading rules file: %s", filename
);
1198 name
= strdup(filename
);
1202 rule_file
= new(UdevRuleFile
, 1);
1206 *rule_file
= (UdevRuleFile
) {
1207 .filename
= TAKE_PTR(name
),
1210 if (rules
->current_file
)
1211 LIST_APPEND(rule_files
, rules
->current_file
, rule_file
);
1213 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1215 rules
->current_file
= rule_file
;
1218 _cleanup_free_
char *buf
= NULL
;
1222 r
= read_line(f
, UTIL_LINE_SIZE
, &buf
);
1229 line
= skip_leading_chars(buf
, NULL
);
1236 if (continuation
&& !ignore_line
) {
1237 if (strlen(continuation
) + len
>= UTIL_LINE_SIZE
)
1240 if (!strextend(&continuation
, line
, NULL
))
1244 line
= continuation
;
1249 if (len
> 0 && line
[len
- 1] == '\\') {
1253 line
[len
- 1] = '\0';
1254 if (!continuation
) {
1255 continuation
= strdup(line
);
1264 log_error("%s:%u: Line is too long, ignored", filename
, line_nr
);
1266 (void) rule_add_line(rules
, line
, line_nr
);
1268 continuation
= mfree(continuation
);
1269 ignore_line
= false;
1272 rule_resolve_goto(rule_file
);
1276 int udev_rules_new(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1277 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1278 _cleanup_strv_free_
char **files
= NULL
;
1282 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1284 rules
= new(UdevRules
, 1);
1288 *rules
= (UdevRules
) {
1289 .resolve_name_timing
= resolve_name_timing
,
1292 (void) udev_rules_check_timestamp(rules
);
1294 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1296 return log_error_errno(r
, "Failed to enumerate rules files: %m");
1298 STRV_FOREACH(f
, files
)
1299 (void) parse_file(rules
, *f
);
1301 *ret_rules
= TAKE_PTR(rules
);
1305 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1309 return paths_check_timestamp(RULES_DIRS
, &rules
->dirs_ts_usec
, true);
1312 static bool token_match_string(UdevRuleToken
*token
, const char *str
) {
1313 const char *i
, *value
;
1317 assert(token
->value
);
1318 assert(token
->type
< _TK_M_MAX
);
1320 str
= strempty(str
);
1321 value
= token
->value
;
1323 switch (token
->match_type
) {
1324 case MATCH_TYPE_EMPTY
:
1325 match
= isempty(str
);
1327 case MATCH_TYPE_SUBSYSTEM
:
1328 value
= "subsystem\0class\0bus\0";
1330 case MATCH_TYPE_PLAIN
:
1331 NULSTR_FOREACH(i
, value
)
1332 if (streq(i
, str
)) {
1337 case MATCH_TYPE_GLOB
:
1338 NULSTR_FOREACH(i
, value
)
1339 if ((fnmatch(i
, str
, 0) == 0)) {
1345 assert_not_reached("Invalid match type");
1348 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1351 static bool token_match_attr(UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
1352 char nbuf
[UTIL_NAME_SIZE
], vbuf
[UTIL_NAME_SIZE
];
1353 const char *name
, *value
;
1359 name
= (const char*) token
->data
;
1361 switch (token
->attr_subst_type
) {
1362 case SUBST_TYPE_FORMAT
:
1363 (void) udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1366 case SUBST_TYPE_PLAIN
:
1367 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1370 case SUBST_TYPE_SUBSYS
:
1371 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1376 assert_not_reached("Invalid attribute substitution type");
1379 /* remove trailing whitespace, if not asked to match for it */
1380 if (token
->attr_match_remove_trailing_whitespace
) {
1381 if (value
!= vbuf
) {
1382 strscpy(vbuf
, sizeof(vbuf
), value
);
1386 delete_trailing_chars(vbuf
, NULL
);
1389 return token_match_string(token
, value
);
1392 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
1401 key
= skip_leading_chars(line
, NULL
);
1403 /* comment or empty line */
1404 if (IN_SET(key
[0], '#', '\0'))
1407 /* split key/value */
1408 val
= strchr(key
, '=');
1413 key
= strstrip(key
);
1417 val
= strstrip(val
);
1422 if (IN_SET(val
[0], '"', '\'')) {
1424 if (len
== 1 || val
[len
-1] != val
[0])
1435 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
1436 const char *key
, *val
;
1443 r
= sd_device_get_parent(dev
, &parent
);
1449 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
1450 if (fnmatch(filter
, key
, 0) != 0)
1452 r
= device_add_property(dev
, key
, val
);
1460 static int attr_subst_subdir(char attr
[static UTIL_PATH_SIZE
]) {
1461 _cleanup_closedir_
DIR *dir
= NULL
;
1462 struct dirent
*dent
;
1463 char buf
[UTIL_PATH_SIZE
], *p
;
1467 tail
= strstr(attr
, "/*/");
1471 len
= tail
- attr
+ 1; /* include slash at the end */
1472 tail
+= 2; /* include slash at the beginning */
1476 size
-= strnpcpy(&p
, size
, attr
, len
);
1482 FOREACH_DIRENT_ALL(dent
, dir
, break) {
1483 if (dent
->d_name
[0] == '.')
1486 strscpyl(p
, size
, dent
->d_name
, tail
, NULL
);
1487 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
1497 static int udev_rule_apply_token_to_event(
1501 usec_t timeout_usec
,
1502 Hashmap
*properties_list
) {
1504 UdevRuleToken
*token
;
1505 char buf
[UTIL_PATH_SIZE
];
1515 /* This returns the following values:
1516 * 0 on the current token does not match the event,
1517 * 1 on the current token matches the event, and
1518 * negative errno on some critical errors. */
1520 token
= rules
->current_file
->current_line
->current_token
;
1522 switch (token
->type
) {
1526 r
= device_get_action(dev
, &a
);
1528 return log_rule_error_errno(dev
, rules
, r
, "Failed to get uevent action type: %m");
1530 return token_match_string(token
, device_action_to_string(a
));
1533 r
= sd_device_get_devpath(dev
, &val
);
1535 return log_rule_error_errno(dev
, rules
, r
, "Failed to get devpath: %m");
1537 return token_match_string(token
, val
);
1539 case TK_M_PARENTS_KERNEL
:
1540 r
= sd_device_get_sysname(dev
, &val
);
1542 return log_rule_error_errno(dev
, rules
, r
, "Failed to get sysname: %m");
1544 return token_match_string(token
, val
);
1546 FOREACH_DEVICE_DEVLINK(dev
, val
)
1547 if (token_match_string(token
, strempty(startswith(val
, "/dev/"))))
1548 return token
->op
== OP_MATCH
;
1549 return token
->op
== OP_NOMATCH
;
1551 return token_match_string(token
, event
->name
);
1553 if (sd_device_get_property_value(dev
, (const char*) token
->data
, &val
) < 0)
1554 val
= hashmap_get(properties_list
, token
->data
);
1556 return token_match_string(token
, val
);
1558 case TK_M_PARENTS_TAG
:
1559 FOREACH_DEVICE_TAG(dev
, val
)
1560 if (token_match_string(token
, val
))
1561 return token
->op
== OP_MATCH
;
1562 return token
->op
== OP_NOMATCH
;
1563 case TK_M_SUBSYSTEM
:
1564 case TK_M_PARENTS_SUBSYSTEM
:
1565 r
= sd_device_get_subsystem(dev
, &val
);
1569 return log_rule_error_errno(dev
, rules
, r
, "Failed to get subsystem: %m");
1571 return token_match_string(token
, val
);
1573 case TK_M_PARENTS_DRIVER
:
1574 r
= sd_device_get_driver(dev
, &val
);
1578 return log_rule_error_errno(dev
, rules
, r
, "Failed to get driver: %m");
1580 return token_match_string(token
, val
);
1582 case TK_M_PARENTS_ATTR
:
1583 return token_match_attr(token
, dev
, event
);
1585 _cleanup_free_
char *value
= NULL
;
1587 (void) udev_event_apply_format(event
, (const char*) token
->data
, buf
, sizeof(buf
), false);
1588 r
= sysctl_read(sysctl_normalize(buf
), &value
);
1589 if (r
< 0 && r
!= -ENOENT
)
1590 return log_rule_error_errno(dev
, rules
, r
, "Failed to read sysctl '%s': %m", buf
);
1592 return token_match_string(token
, strstrip(value
));
1595 mode_t mode
= PTR_TO_MODE(token
->data
);
1596 struct stat statbuf
;
1598 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1599 if (!path_is_absolute(buf
) &&
1600 util_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
1601 char tmp
[UTIL_PATH_SIZE
];
1603 r
= sd_device_get_syspath(dev
, &val
);
1605 return log_rule_error_errno(dev
, rules
, r
, "Failed to get syspath: %m");
1607 strscpy(tmp
, sizeof(tmp
), buf
);
1608 strscpyl(buf
, sizeof(buf
), val
, "/", tmp
, NULL
);
1611 r
= attr_subst_subdir(buf
);
1613 return token
->op
== OP_NOMATCH
;
1615 return log_rule_error_errno(dev
, rules
, r
, "Failed to test the existence of '%s': %m", buf
);
1617 if (stat(buf
, &statbuf
) < 0)
1618 return token
->op
== OP_NOMATCH
;
1620 if (mode
== MODE_INVALID
)
1621 return token
->op
== OP_MATCH
;
1623 match
= (((statbuf
.st_mode
^ mode
) & 07777) == 0);
1624 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1626 case TK_M_PROGRAM
: {
1627 char result
[UTIL_LINE_SIZE
];
1629 event
->program_result
= mfree(event
->program_result
);
1630 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1631 log_rule_debug(dev
, rules
, "Running PROGRAM '%s'", buf
);
1633 r
= udev_event_spawn(event
, timeout_usec
, true, buf
, result
, sizeof(result
));
1635 return log_rule_error_errno(dev
, rules
, r
, "Failed to execute '%s': %m", buf
);
1637 return token
->op
== OP_NOMATCH
;
1639 delete_trailing_chars(result
, "\n");
1640 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1642 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of '%s'",
1645 event
->program_result
= strdup(result
);
1646 return token
->op
== OP_MATCH
;
1648 case TK_M_IMPORT_FILE
: {
1649 _cleanup_fclose_
FILE *f
= NULL
;
1651 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1652 log_rule_debug(dev
, rules
, "Importing properties from '%s'", buf
);
1654 f
= fopen(buf
, "re");
1656 if (errno
!= ENOENT
)
1657 return log_rule_error_errno(dev
, rules
, errno
,
1658 "Failed to open '%s': %m", buf
);
1659 return token
->op
== OP_NOMATCH
;
1663 _cleanup_free_
char *line
= NULL
;
1666 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1668 log_rule_debug_errno(dev
, rules
, r
,
1669 "Failed to read '%s', ignoring: %m", buf
);
1670 return token
->op
== OP_NOMATCH
;
1675 r
= get_property_from_string(line
, &key
, &value
);
1677 log_rule_debug_errno(dev
, rules
, r
,
1678 "Failed to parse key and value from '%s', ignoring: %m",
1683 r
= device_add_property(dev
, key
, value
);
1685 return log_rule_error_errno(dev
, rules
, r
,
1686 "Failed to add property %s=%s: %m",
1690 return token
->op
== OP_MATCH
;
1692 case TK_M_IMPORT_PROGRAM
: {
1693 char result
[UTIL_LINE_SIZE
], *line
, *pos
;
1695 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1696 log_rule_debug(dev
, rules
, "Importing properties from results of '%s'", buf
);
1698 r
= udev_event_spawn(event
, timeout_usec
, true, buf
, result
, sizeof result
);
1700 return log_rule_error_errno(dev
, rules
, r
, "Failed to execute '%s': %m", buf
);
1702 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
1703 return token
->op
== OP_NOMATCH
;
1706 for (line
= result
; !isempty(line
); line
= pos
) {
1709 pos
= strchr(line
, '\n');
1713 r
= get_property_from_string(line
, &key
, &value
);
1715 log_rule_debug_errno(dev
, rules
, r
,
1716 "Failed to parse key and value from '%s', ignoring: %m",
1721 r
= device_add_property(dev
, key
, value
);
1723 return log_rule_error_errno(dev
, rules
, r
,
1724 "Failed to add property %s=%s: %m",
1728 return token
->op
== OP_MATCH
;
1730 case TK_M_IMPORT_BUILTIN
: {
1731 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
1732 unsigned mask
= 1U << (int) cmd
;
1734 if (udev_builtin_run_once(cmd
)) {
1735 /* check if we ran already */
1736 if (event
->builtin_run
& mask
) {
1737 log_rule_debug(dev
, rules
, "Skipping builtin '%s' in IMPORT key",
1738 udev_builtin_name(cmd
));
1739 /* return the result from earlier run */
1740 return token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
);
1743 event
->builtin_run
|= mask
;
1746 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1747 log_rule_debug(dev
, rules
, "Importing properties from results of builtin command '%s'", buf
);
1749 r
= udev_builtin_run(dev
, cmd
, buf
, false);
1751 /* remember failure */
1752 log_rule_debug_errno(dev
, rules
, r
, "Failed to run builtin '%s': %m", buf
);
1753 event
->builtin_ret
|= mask
;
1755 return token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
);
1757 case TK_M_IMPORT_DB
: {
1758 if (!event
->dev_db_clone
)
1759 return token
->op
== OP_NOMATCH
;
1760 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
1762 return token
->op
== OP_NOMATCH
;
1764 return log_rule_error_errno(dev
, rules
, r
,
1765 "Failed to get property '%s' from database: %m",
1768 r
= device_add_property(dev
, token
->value
, val
);
1770 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1772 return token
->op
== OP_MATCH
;
1774 case TK_M_IMPORT_CMDLINE
: {
1775 _cleanup_free_
char *value
= NULL
;
1777 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
1779 return log_rule_error_errno(dev
, rules
, r
,
1780 "Failed to read '%s' option from /proc/cmdline: %m",
1783 return token
->op
== OP_NOMATCH
;
1785 r
= device_add_property(dev
, token
->value
, value
?: "1");
1787 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1788 token
->value
, value
?: "1");
1789 return token
->op
== OP_MATCH
;
1791 case TK_M_IMPORT_PARENT
: {
1792 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1793 r
= import_parent_into_properties(dev
, buf
);
1795 return log_rule_error_errno(dev
, rules
, r
,
1796 "Failed to import properties '%s' from parent: %m",
1798 return token
->op
== (r
> 0 ? OP_MATCH
: OP_NOMATCH
);
1801 return token_match_string(token
, event
->program_result
);
1802 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
1803 event
->esc
= ESCAPE_NONE
;
1805 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
1806 event
->esc
= ESCAPE_REPLACE
;
1808 case TK_A_OPTIONS_DB_PERSIST
:
1809 device_set_db_persist(dev
);
1811 case TK_A_OPTIONS_INOTIFY_WATCH
:
1812 if (event
->inotify_watch_final
)
1814 if (token
->op
== OP_ASSIGN_FINAL
)
1815 event
->inotify_watch_final
= true;
1817 event
->inotify_watch
= token
->data
;
1819 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
1820 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
1823 char owner
[UTIL_NAME_SIZE
];
1824 const char *ow
= owner
;
1826 if (event
->owner_final
)
1828 if (token
->op
== OP_ASSIGN_FINAL
)
1829 event
->owner_final
= true;
1831 (void) udev_event_apply_format(event
, token
->value
, owner
, sizeof(owner
), false);
1832 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
1834 log_unknown_owner(dev
, rules
, r
, "user", owner
);
1836 log_rule_debug(dev
, rules
, "OWNER %s(%u)", owner
, event
->uid
);
1840 char group
[UTIL_NAME_SIZE
];
1841 const char *gr
= group
;
1843 if (event
->group_final
)
1845 if (token
->op
== OP_ASSIGN_FINAL
)
1846 event
->group_final
= true;
1848 (void) udev_event_apply_format(event
, token
->value
, group
, sizeof(group
), false);
1849 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
1851 log_unknown_owner(dev
, rules
, r
, "group", group
);
1853 log_rule_debug(dev
, rules
, "GROUP %s(%u)", group
, event
->gid
);
1857 char mode_str
[UTIL_NAME_SIZE
];
1859 if (event
->mode_final
)
1861 if (token
->op
== OP_ASSIGN_FINAL
)
1862 event
->mode_final
= true;
1864 (void) udev_event_apply_format(event
, token
->value
, mode_str
, sizeof(mode_str
), false);
1865 r
= parse_mode(mode_str
, &event
->mode
);
1867 log_rule_error_errno(dev
, rules
, r
, "Failed to parse mode '%s', ignoring: %m", mode_str
);
1869 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
1873 if (event
->owner_final
)
1875 if (token
->op
== OP_ASSIGN_FINAL
)
1876 event
->owner_final
= true;
1879 event
->uid
= PTR_TO_UID(token
->data
);
1880 log_rule_debug(dev
, rules
, "OWNER %u", event
->uid
);
1883 if (event
->group_final
)
1885 if (token
->op
== OP_ASSIGN_FINAL
)
1886 event
->group_final
= true;
1889 event
->gid
= PTR_TO_GID(token
->data
);
1890 log_rule_debug(dev
, rules
, "GROUP %u", event
->gid
);
1893 if (event
->mode_final
)
1895 if (token
->op
== OP_ASSIGN_FINAL
)
1896 event
->mode_final
= true;
1899 event
->mode
= PTR_TO_MODE(token
->data
);
1900 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
1902 case TK_A_SECLABEL
: {
1903 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
1904 char label_str
[UTIL_LINE_SIZE
] = {};
1906 name
= strdup((const char*) token
->data
);
1910 (void) udev_event_apply_format(event
, token
->value
, label_str
, sizeof(label_str
), false);
1911 if (!isempty(label_str
))
1912 label
= strdup(label_str
);
1914 label
= strdup(token
->value
);
1918 if (token
->op
== OP_ASSIGN
)
1919 ordered_hashmap_clear_free_free(event
->seclabel_list
);
1921 r
= ordered_hashmap_ensure_allocated(&event
->seclabel_list
, NULL
);
1925 r
= ordered_hashmap_put(event
->seclabel_list
, name
, label
);
1928 log_rule_debug(dev
, rules
, "SECLABEL{%s}='%s'", name
, label
);
1929 name
= label
= NULL
;
1933 const char *name
= (const char*) token
->data
;
1934 char value_new
[UTIL_NAME_SIZE
], *p
= value_new
;
1935 size_t l
= sizeof(value_new
);
1937 if (isempty(token
->value
)) {
1938 if (token
->op
== OP_ADD
)
1940 r
= device_add_property(dev
, name
, NULL
);
1942 return log_rule_error_errno(dev
, rules
, r
, "Failed to remove property '%s': %m", name
);
1946 if (token
->op
== OP_ADD
&&
1947 sd_device_get_property_value(dev
, name
, &val
) >= 0)
1948 l
= strpcpyl(&p
, l
, val
, " ", NULL
);
1950 (void) udev_event_apply_format(event
, token
->value
, p
, l
, false);
1952 r
= device_add_property(dev
, name
, value_new
);
1954 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m", name
, value_new
);
1958 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1959 if (token
->op
== OP_ASSIGN
)
1960 device_cleanup_tags(dev
);
1962 if (buf
[strspn(buf
, ALPHANUMERICAL
"-_")] != '\0') {
1963 log_rule_error(dev
, rules
, "Invalid tag name '%s', ignoring", buf
);
1966 if (token
->op
== OP_REMOVE
)
1967 device_remove_tag(dev
, buf
);
1969 r
= device_add_tag(dev
, buf
);
1971 return log_rule_error_errno(dev
, rules
, r
, "Failed to add tag '%s': %m", buf
);
1976 if (event
->name_final
)
1978 if (token
->op
== OP_ASSIGN_FINAL
)
1979 event
->name_final
= true;
1981 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1982 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1983 count
= util_replace_chars(buf
, "/");
1985 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of NAME=\"%s\"",
1986 count
, token
->value
);
1988 if (sd_device_get_devnum(dev
, NULL
) >= 0 &&
1989 (sd_device_get_devname(dev
, &val
) < 0 ||
1990 !streq_ptr(buf
, startswith(val
, "/dev/")))) {
1991 log_rule_error(dev
, rules
,
1992 "Kernel device nodes cannot be renamed, ignoring NAME=\"%s\"; please fix it.",
1996 if (free_and_strdup(&event
->name
, buf
) < 0)
1999 log_rule_debug(dev
, rules
, "NAME '%s'", event
->name
);
2002 case TK_A_DEVLINK
: {
2005 if (event
->devlink_final
)
2007 if (sd_device_get_devnum(dev
, NULL
) < 0)
2009 if (token
->op
== OP_ASSIGN_FINAL
)
2010 event
->devlink_final
= true;
2011 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2012 device_cleanup_devlinks(dev
);
2014 /* allow multiple symlinks separated by spaces */
2015 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), event
->esc
!= ESCAPE_NONE
);
2016 if (event
->esc
== ESCAPE_UNSET
)
2017 count
= util_replace_chars(buf
, "/ ");
2018 else if (event
->esc
== ESCAPE_REPLACE
)
2019 count
= util_replace_chars(buf
, "/");
2023 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of LINK", count
);
2025 p
= skip_leading_chars(buf
, NULL
);
2026 while (!isempty(p
)) {
2027 char filename
[UTIL_PATH_SIZE
], *next
;
2029 next
= strchr(p
, ' ');
2032 next
= skip_leading_chars(next
, NULL
);
2035 strscpyl(filename
, sizeof(filename
), "/dev/", p
, NULL
);
2036 r
= device_add_devlink(dev
, filename
);
2038 return log_rule_error_errno(dev
, rules
, r
, "Failed to add devlink '%s': %m", filename
);
2040 log_rule_debug(dev
, rules
, "LINK '%s'", p
);
2046 const char *key_name
= (const char*) token
->data
;
2047 char value
[UTIL_NAME_SIZE
];
2049 if (util_resolve_subsys_kernel(key_name
, buf
, sizeof(buf
), false) < 0 &&
2050 sd_device_get_syspath(dev
, &val
) >= 0)
2051 strscpyl(buf
, sizeof(buf
), val
, "/", key_name
, NULL
);
2053 r
= attr_subst_subdir(buf
);
2055 log_rule_error_errno(dev
, rules
, r
, "Could not find file matches '%s', ignoring: %m", buf
);
2058 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false);
2060 log_rule_debug(dev
, rules
, "ATTR '%s' writing '%s'", buf
, value
);
2061 r
= write_string_file(buf
, value
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2063 log_rule_error_errno(dev
, rules
, r
, "Failed to write ATTR{%s}, ignoring: %m", buf
);
2067 char value
[UTIL_NAME_SIZE
];
2069 (void) udev_event_apply_format(event
, (const char*) token
->data
, buf
, sizeof(buf
), false);
2070 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false);
2071 sysctl_normalize(buf
);
2072 log_rule_debug(dev
, rules
, "SYSCTL '%s' writing '%s'", buf
, value
);
2073 r
= sysctl_write(buf
, value
);
2075 log_rule_error_errno(dev
, rules
, r
, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf
, value
);
2078 case TK_A_RUN_BUILTIN
:
2079 case TK_A_RUN_PROGRAM
: {
2080 _cleanup_free_
char *cmd
= NULL
;
2082 if (event
->run_final
)
2084 if (token
->op
== OP_ASSIGN_FINAL
)
2085 event
->run_final
= true;
2087 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2088 ordered_hashmap_clear_free_key(event
->run_list
);
2090 r
= ordered_hashmap_ensure_allocated(&event
->run_list
, NULL
);
2094 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
2100 r
= ordered_hashmap_put(event
->run_list
, cmd
, token
->data
);
2106 log_rule_debug(dev
, rules
, "RUN '%s'", token
->value
);
2109 case TK_A_OPTIONS_STATIC_NODE
:
2110 /* do nothing for events. */
2113 assert_not_reached("Invalid token type");
2119 static bool token_is_for_parents(UdevRuleToken
*token
) {
2120 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
2123 static int udev_rule_apply_parent_token_to_event(
2128 UdevRuleToken
*head
;
2131 line
= rules
->current_file
->current_line
;
2132 head
= rules
->current_file
->current_line
->current_token
;
2133 event
->dev_parent
= event
->dev
;
2135 LIST_FOREACH(tokens
, line
->current_token
, head
) {
2136 if (!token_is_for_parents(line
->current_token
))
2137 return true; /* All parent tokens match. */
2138 r
= udev_rule_apply_token_to_event(rules
, event
->dev_parent
, event
, 0, NULL
);
2144 if (!line
->current_token
)
2145 /* All parent tokens match. But no assign tokens in the line. Hmm... */
2148 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
2149 event
->dev_parent
= NULL
;
2155 static int udev_rule_apply_line_to_event(
2158 usec_t timeout_usec
,
2159 Hashmap
*properties_list
,
2160 UdevRuleLine
**next_line
) {
2162 UdevRuleLine
*line
= rules
->current_file
->current_line
;
2163 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
2164 UdevRuleToken
*token
, *next_token
;
2165 bool parents_done
= false;
2166 DeviceAction action
;
2169 r
= device_get_action(event
->dev
, &action
);
2173 if (action
!= DEVICE_ACTION_REMOVE
) {
2174 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
2175 mask
|= LINE_HAS_DEVLINK
;
2177 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
2178 mask
|= LINE_HAS_NAME
;
2181 if ((line
->type
& mask
) == 0)
2184 event
->esc
= ESCAPE_UNSET
;
2185 LIST_FOREACH_SAFE(tokens
, token
, next_token
, line
->tokens
) {
2186 line
->current_token
= token
;
2188 if (token_is_for_parents(token
)) {
2192 r
= udev_rule_apply_parent_token_to_event(rules
, event
);
2196 parents_done
= true;
2200 r
= udev_rule_apply_token_to_event(rules
, event
->dev
, event
, timeout_usec
, properties_list
);
2205 if (line
->goto_line
)
2206 *next_line
= line
->goto_line
;
2211 int udev_rules_apply_to_event(
2214 usec_t timeout_usec
,
2215 Hashmap
*properties_list
) {
2218 UdevRuleLine
*next_line
;
2224 LIST_FOREACH(rule_files
, file
, rules
->rule_files
) {
2225 rules
->current_file
= file
;
2226 LIST_FOREACH_SAFE(rule_lines
, file
->current_line
, next_line
, file
->rule_lines
) {
2227 r
= udev_rule_apply_line_to_event(rules
, event
, timeout_usec
, properties_list
, &next_line
);
2236 static int apply_static_dev_perms(const char *devnode
, uid_t uid
, gid_t gid
, mode_t mode
, char **tags
) {
2237 char device_node
[UTIL_PATH_SIZE
], tags_dir
[UTIL_PATH_SIZE
], tag_symlink
[UTIL_PATH_SIZE
];
2238 _cleanup_free_
char *unescaped_filename
= NULL
;
2245 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
&& !tags
)
2248 strscpyl(device_node
, sizeof(device_node
), "/dev/", devnode
, NULL
);
2249 if (stat(device_node
, &stats
) < 0) {
2250 if (errno
!= ENOENT
)
2251 return log_error_errno(errno
, "Failed to stat %s: %m", device_node
);
2255 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
)) {
2256 log_warning("%s is neither block nor character device, ignoring.", device_node
);
2260 if (!strv_isempty(tags
)) {
2261 unescaped_filename
= xescape(devnode
, "/.");
2262 if (!unescaped_filename
)
2266 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2267 STRV_FOREACH(t
, tags
) {
2268 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2269 r
= mkdir_p(tags_dir
, 0755);
2271 return log_error_errno(r
, "Failed to create %s: %m", tags_dir
);
2273 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2274 r
= symlink(device_node
, tag_symlink
);
2275 if (r
< 0 && errno
!= EEXIST
)
2276 return log_error_errno(errno
, "Failed to create symlink %s -> %s: %m",
2277 tag_symlink
, device_node
);
2280 /* don't touch the permissions if only the tags were set */
2281 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
)
2284 if (mode
== MODE_INVALID
)
2285 mode
= gid_is_valid(gid
) ? 0660 : 0600;
2286 if (!uid_is_valid(uid
))
2288 if (!gid_is_valid(gid
))
2291 r
= chmod_and_chown(device_node
, mode
, uid
, gid
);
2293 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2294 device_node
, uid
, gid
);
2296 log_debug("chown '%s' %u:%u", device_node
, uid
, gid
);
2298 (void) utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2302 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
2303 UdevRuleToken
*token
;
2304 _cleanup_free_
char **tags
= NULL
;
2305 uid_t uid
= UID_INVALID
;
2306 gid_t gid
= GID_INVALID
;
2307 mode_t mode
= MODE_INVALID
;
2312 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
2315 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
2316 if (token
->type
== TK_A_OWNER_ID
)
2317 uid
= PTR_TO_UID(token
->data
);
2318 else if (token
->type
== TK_A_GROUP_ID
)
2319 gid
= PTR_TO_GID(token
->data
);
2320 else if (token
->type
== TK_A_MODE_ID
)
2321 mode
= PTR_TO_MODE(token
->data
);
2322 else if (token
->type
== TK_A_TAG
) {
2323 r
= strv_extend(&tags
, token
->value
);
2326 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
2327 r
= apply_static_dev_perms(token
->value
, uid
, gid
, mode
, tags
);
2335 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2342 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2343 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
2344 r
= udev_rule_line_apply_static_dev_perms(line
);