1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 #include "alloc-util.h"
6 #include "architecture.h"
7 #include "conf-files.h"
8 #include "conf-parser.h"
9 #include "confidential-virt.h"
10 #include "constants.h"
11 #include "device-private.h"
12 #include "device-util.h"
13 #include "dirent-util.h"
17 #include "format-util.h"
19 #include "glob-util.h"
22 #include "netif-naming-scheme.h"
23 #include "nulstr-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "proc-cmdline.h"
27 #include "socket-util.h"
28 #include "stat-util.h"
29 #include "string-table.h"
32 #include "sysctl-util.h"
33 #include "syslog-util.h"
34 #include "udev-builtin.h"
35 #include "udev-event.h"
36 #include "udev-format.h"
37 #include "udev-node.h"
38 #include "udev-rules.h"
39 #include "udev-spawn.h"
40 #include "udev-trace.h"
41 #include "udev-util.h"
42 #include "user-util.h"
45 #define RULES_DIRS ((const char* const*) CONF_PATHS_STRV("udev/rules.d"))
53 OP_ASSIGN_FINAL
, /* := */
55 _OP_TYPE_INVALID
= -EINVAL
,
56 } UdevRuleOperatorType
;
59 MATCH_TYPE_EMPTY
, /* empty string */
60 MATCH_TYPE_PLAIN
, /* no special characters */
61 MATCH_TYPE_PLAIN_WITH_EMPTY
, /* no special characters with empty string, e.g., "|foo" */
62 MATCH_TYPE_GLOB
, /* shell globs ?,*,[] */
63 MATCH_TYPE_GLOB_WITH_EMPTY
, /* shell globs ?,*,[] with empty string, e.g., "|foo*" */
64 MATCH_TYPE_SUBSYSTEM
, /* "subsystem", "bus", or "class" */
66 _MATCH_TYPE_INVALID
= -EINVAL
,
70 SUBST_TYPE_PLAIN
, /* no substitution */
71 SUBST_TYPE_FORMAT
, /* % or $ */
72 SUBST_TYPE_SUBSYS
, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
74 _SUBST_TYPE_INVALID
= -EINVAL
,
75 } UdevRuleSubstituteType
;
78 /* lvalues which take match or nomatch operator */
79 TK_M_ACTION
, /* string, device_get_action() */
80 TK_M_DEVPATH
, /* path, sd_device_get_devpath() */
81 TK_M_KERNEL
, /* string, sd_device_get_sysname() */
82 TK_M_DEVLINK
, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */
83 TK_M_NAME
, /* string, name of network interface */
84 TK_M_ENV
, /* string, device property, takes key through attribute */
85 TK_M_CONST
, /* string, system-specific hard-coded constant */
86 TK_M_TAG
, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */
87 TK_M_SUBSYSTEM
, /* string, sd_device_get_subsystem() */
88 TK_M_DRIVER
, /* string, sd_device_get_driver() */
89 TK_M_ATTR
, /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */
90 TK_M_SYSCTL
, /* string, takes kernel parameter through attribute */
92 /* matches parent parameters */
93 TK_M_PARENTS_KERNEL
, /* string */
94 TK_M_PARENTS_SUBSYSTEM
, /* string */
95 TK_M_PARENTS_DRIVER
, /* string */
96 TK_M_PARENTS_ATTR
, /* string */
97 TK_M_PARENTS_TAG
, /* strv */
99 TK_M_TEST
, /* path, optionally mode_t can be specified by attribute, test the existence of a file */
100 TK_M_PROGRAM
, /* string, execute a program */
101 TK_M_IMPORT_FILE
, /* path */
102 TK_M_IMPORT_PROGRAM
, /* string, import properties from the result of program */
103 TK_M_IMPORT_BUILTIN
, /* string, import properties from the result of built-in command */
104 TK_M_IMPORT_DB
, /* string, import properties from database */
105 TK_M_IMPORT_CMDLINE
, /* string, kernel command line */
106 TK_M_IMPORT_PARENT
, /* string, parent property */
107 TK_M_RESULT
, /* string, result of TK_M_PROGRAM */
109 #define _TK_M_MAX (TK_M_RESULT + 1)
110 #define _TK_A_MIN _TK_M_MAX
112 /* lvalues which take one of assign operators */
113 TK_A_OPTIONS_STRING_ESCAPE_NONE
, /* no argument */
114 TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, /* no argument */
115 TK_A_OPTIONS_DB_PERSIST
, /* no argument */
116 TK_A_OPTIONS_INOTIFY_WATCH
, /* boolean */
117 TK_A_OPTIONS_DEVLINK_PRIORITY
, /* int */
118 TK_A_OPTIONS_LOG_LEVEL
, /* string of log level or "reset" */
119 TK_A_OWNER
, /* user name */
120 TK_A_GROUP
, /* group name */
121 TK_A_MODE
, /* mode string */
122 TK_A_OWNER_ID
, /* uid_t */
123 TK_A_GROUP_ID
, /* gid_t */
124 TK_A_MODE_ID
, /* mode_t */
125 TK_A_TAG
, /* string */
126 TK_A_OPTIONS_STATIC_NODE
, /* device path, /dev/... */
127 TK_A_SECLABEL
, /* string with attribute */
128 TK_A_ENV
, /* string with attribute */
129 TK_A_NAME
, /* ifname */
130 TK_A_DEVLINK
, /* string */
131 TK_A_ATTR
, /* string with attribute */
132 TK_A_SYSCTL
, /* string with attribute */
133 TK_A_RUN_BUILTIN
, /* string */
134 TK_A_RUN_PROGRAM
, /* string */
137 _TK_TYPE_INVALID
= -EINVAL
,
141 LINE_HAS_NAME
= 1 << 0, /* has NAME= */
142 LINE_HAS_DEVLINK
= 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */
143 LINE_HAS_STATIC_NODE
= 1 << 2, /* has OPTIONS=static_node */
144 LINE_HAS_GOTO
= 1 << 3, /* has GOTO= */
145 LINE_HAS_LABEL
= 1 << 4, /* has LABEL= */
146 LINE_UPDATE_SOMETHING
= 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */
147 LINE_IS_REFERENCED
= 1 << 6, /* is referenced by GOTO */
150 typedef struct UdevRuleFile UdevRuleFile
;
151 typedef struct UdevRuleLine UdevRuleLine
;
152 typedef struct UdevRuleToken UdevRuleToken
;
154 struct UdevRuleToken
{
155 UdevRuleTokenType type
:8;
156 UdevRuleOperatorType op
:8;
157 UdevRuleMatchType match_type
:8;
158 UdevRuleSubstituteType attr_subst_type
:7;
159 bool attr_match_remove_trailing_whitespace
:1;
163 UdevRuleLine
*rule_line
;
164 LIST_FIELDS(UdevRuleToken
, tokens
);
167 struct UdevRuleLine
{
169 unsigned line_number
;
170 UdevRuleLineType type
;
173 const char *goto_label
;
174 UdevRuleLine
*goto_line
;
176 UdevRuleFile
*rule_file
;
177 LIST_HEAD(UdevRuleToken
, tokens
);
178 LIST_FIELDS(UdevRuleLine
, rule_lines
);
181 struct UdevRuleFile
{
183 unsigned issues
; /* used by "udevadm verify" */
186 LIST_HEAD(UdevRuleLine
, rule_lines
);
187 LIST_FIELDS(UdevRuleFile
, rule_files
);
191 ResolveNameTiming resolve_name_timing
;
192 Hashmap
*known_users
;
193 Hashmap
*known_groups
;
194 Hashmap
*stats_by_path
;
195 LIST_HEAD(UdevRuleFile
, rule_files
);
198 #define LINE_GET_RULES(line) \
199 ASSERT_PTR(ASSERT_PTR(ASSERT_PTR(line)->rule_file)->rules)
201 /*** Logging helpers ***/
203 #define log_udev_rule_internal(device, file, line_nr, level, error, fmt, ...) \
206 sd_device *_dev = (device); \
207 UdevRuleFile *_f = (file); \
208 const char *_n = _f ? _f->filename : NULL; \
211 _f->issues |= (1U << _lv); \
213 log_device_full_errno_zerook( \
214 _dev, _lv, error, "%s:%u " fmt, \
215 strna(_n), line_nr, \
219 /* Mainly used when applying tokens to the event device. */
220 #define log_event_full_errno_zerook(device, token, ...) \
222 UdevRuleToken *_t = (token); \
223 UdevRuleLine *_l = _t ? _t->rule_line : NULL; \
225 log_udev_rule_internal( \
227 _l ? _l->rule_file : NULL, \
228 _l ? _l->line_number : 0, \
232 #define log_event_full_errno(device, token, level, error, ...) \
234 int _error = (error); \
235 ASSERT_NON_ZERO(_error); \
236 log_event_full_errno_zerook( \
237 device, token, level, _error, ##__VA_ARGS__); \
240 #define log_event_full(device, token, level, ...) (void) log_event_full_errno_zerook(device, token, level, 0, __VA_ARGS__)
242 #define log_event_debug(device, token, ...) log_event_full(device, token, LOG_DEBUG, __VA_ARGS__)
243 #define log_event_info(device, token, ...) log_event_full(device, token, LOG_INFO, __VA_ARGS__)
244 #define log_event_notice(device, token, ...) log_event_full(device, token, LOG_NOTICE, __VA_ARGS__)
245 #define log_event_warning(device, token, ...) log_event_full(device, token, LOG_WARNING, __VA_ARGS__)
246 #define log_event_error(device, token, ...) log_event_full(device, token, LOG_ERR, __VA_ARGS__)
248 #define log_event_debug_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_DEBUG, error, __VA_ARGS__)
249 #define log_event_info_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_INFO, error, __VA_ARGS__)
250 #define log_event_notice_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_NOTICE, error, __VA_ARGS__)
251 #define log_event_warning_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_WARNING, error, __VA_ARGS__)
252 #define log_event_error_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_ERR, error, __VA_ARGS__)
254 /* Mainly used when parsing .rules files. */
255 #define log_file_full_errno_zerook(...) \
256 log_udev_rule_internal(NULL, __VA_ARGS__)
258 #define log_file_error(file, line_nr, ...) \
259 log_file_full_errno_zerook(file, line_nr, LOG_ERR, 0, __VA_ARGS__)
261 #define log_line_full_errno_zerook(line, ...) \
263 UdevRuleLine *_l = (line); \
264 log_file_full_errno_zerook( \
265 _l ? _l->rule_file : NULL, \
266 _l ? _l->line_number : 0, \
270 #define log_line_full_errno(line, level, error, ...) \
272 int _error = (error); \
273 ASSERT_NON_ZERO(_error); \
274 log_line_full_errno_zerook( \
275 line, level, _error, ##__VA_ARGS__); \
278 #define log_line_full(line, level, ...) (void) log_line_full_errno_zerook(line, level, 0, __VA_ARGS__)
280 #define log_line_debug(line, ...) log_line_full(line, LOG_DEBUG, __VA_ARGS__)
281 #define log_line_info(line, ...) log_line_full(line, LOG_INFO, __VA_ARGS__)
282 #define log_line_notice(line, ...) log_line_full(line, LOG_NOTICE, __VA_ARGS__)
283 #define log_line_warning(line, ...) log_line_full(line, LOG_WARNING, __VA_ARGS__)
284 #define log_line_error(line, ...) log_line_full(line, LOG_ERR, __VA_ARGS__)
286 #define log_line_debug_errno(line, error, ...) log_line_full_errno(line, LOG_DEBUG, error, __VA_ARGS__)
287 #define log_line_info_errno(line, error, ...) log_line_full_errno(line, LOG_INFO, error, __VA_ARGS__)
288 #define log_line_notice_errno(line, error, ...) log_line_full_errno(line, LOG_NOTICE, error, __VA_ARGS__)
289 #define log_line_warning_errno(line, error, ...) log_line_full_errno(line, LOG_WARNING, error, __VA_ARGS__)
290 #define log_line_error_errno(line, error, ...) log_line_full_errno(line, LOG_ERR, error, __VA_ARGS__)
292 #define _log_line_invalid_token(line, key, type) \
293 log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \
294 "Invalid %s for %s.", type, key)
296 #define log_line_invalid_op(line, key) _log_line_invalid_token(line, key, "operator")
297 #define log_line_invalid_attr(line, key) _log_line_invalid_token(line, key, "attribute")
299 #define log_line_invalid_attr_format(line, key, attr, offset, hint) \
300 log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \
301 "Invalid attribute \"%s\" for %s (char %zu: %s), ignoring.", \
302 attr, key, offset, hint)
303 #define log_line_invalid_value(line, key, value, offset, hint) \
304 log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \
305 "Invalid value \"%s\" for %s (char %zu: %s), ignoring.", \
306 value, key, offset, hint)
308 static void log_unknown_owner(sd_device
*dev
, UdevRuleLine
*line
, int error
, const char *entity
, const char *name
) {
310 ASSERT_NON_ZERO(error
);
312 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
313 log_udev_rule_internal(dev
, line
->rule_file
, line
->line_number
, LOG_ERR
, error
,
314 "Unknown %s '%s', ignoring.", entity
, name
);
316 log_udev_rule_internal(dev
, line
->rule_file
, line
->line_number
, LOG_ERR
, error
,
317 "Failed to resolve %s '%s', ignoring: %m", entity
, name
);
320 static void log_event_truncated(
322 UdevRuleToken
*token
,
329 log_event_debug(dev
, token
,
330 "The %s is truncated while substituting into '%s', "
331 "assuming the %s key does not match.",
334 log_event_warning(dev
, token
,
335 "The %s is truncated while substituting into '%s', "
336 "refusing to apply the %s key.",
340 /*** Other functions ***/
342 static UdevRuleToken
*udev_rule_token_free(UdevRuleToken
*token
) {
346 if (token
->rule_line
)
347 LIST_REMOVE(tokens
, token
->rule_line
->tokens
, token
);
352 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleToken
*, udev_rule_token_free
);
354 static void udev_rule_line_clear_tokens(UdevRuleLine
*rule_line
) {
357 LIST_FOREACH(tokens
, i
, rule_line
->tokens
)
358 udev_rule_token_free(i
);
361 static UdevRuleLine
*udev_rule_line_free(UdevRuleLine
*rule_line
) {
365 udev_rule_line_clear_tokens(rule_line
);
367 if (rule_line
->rule_file
)
368 LIST_REMOVE(rule_lines
, rule_line
->rule_file
->rule_lines
, rule_line
);
370 free(rule_line
->line
);
371 return mfree(rule_line
);
374 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine
*, udev_rule_line_free
);
376 static UdevRuleFile
*udev_rule_file_free(UdevRuleFile
*rule_file
) {
380 LIST_FOREACH(rule_lines
, i
, rule_file
->rule_lines
)
381 udev_rule_line_free(i
);
383 if (rule_file
->rules
)
384 LIST_REMOVE(rule_files
, rule_file
->rules
->rule_files
, rule_file
);
386 free(rule_file
->filename
);
387 return mfree(rule_file
);
390 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleFile
*, udev_rule_file_free
);
392 UdevRules
*udev_rules_free(UdevRules
*rules
) {
396 LIST_FOREACH(rule_files
, i
, rules
->rule_files
)
397 udev_rule_file_free(i
);
399 hashmap_free_free_key(rules
->known_users
);
400 hashmap_free_free_key(rules
->known_groups
);
401 hashmap_free(rules
->stats_by_path
);
405 static int rule_resolve_user(UdevRuleLine
*rule_line
, const char *name
, uid_t
*ret
) {
406 Hashmap
**known_users
= &LINE_GET_RULES(rule_line
)->known_users
;
407 _cleanup_free_
char *n
= NULL
;
415 val
= hashmap_get(*known_users
, name
);
417 *ret
= PTR_TO_UID(val
);
421 r
= get_user_creds(&name
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
423 log_unknown_owner(NULL
, rule_line
, r
, "user", name
);
432 r
= hashmap_ensure_put(known_users
, &string_hash_ops
, n
, UID_TO_PTR(uid
));
441 static int rule_resolve_group(UdevRuleLine
*rule_line
, const char *name
, gid_t
*ret
) {
442 Hashmap
**known_groups
= &LINE_GET_RULES(rule_line
)->known_groups
;
443 _cleanup_free_
char *n
= NULL
;
451 val
= hashmap_get(*known_groups
, name
);
453 *ret
= PTR_TO_GID(val
);
457 r
= get_group_creds(&name
, &gid
, USER_CREDS_ALLOW_MISSING
);
459 log_unknown_owner(NULL
, rule_line
, r
, "group", name
);
468 r
= hashmap_ensure_put(known_groups
, &string_hash_ops
, n
, GID_TO_PTR(gid
));
477 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
481 return SUBST_TYPE_SUBSYS
;
482 if (strchr(str
, '%') || strchr(str
, '$'))
483 return SUBST_TYPE_FORMAT
;
484 return SUBST_TYPE_PLAIN
;
487 static bool type_has_nulstr_value(UdevRuleTokenType type
) {
488 return type
< TK_M_TEST
|| type
== TK_M_RESULT
;
491 static int rule_line_add_token(UdevRuleLine
*rule_line
, UdevRuleTokenType type
, UdevRuleOperatorType op
, char *value
, void *data
) {
492 _cleanup_(udev_rule_token_freep
) UdevRuleToken
*token
= NULL
;
493 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
494 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
495 bool remove_trailing_whitespace
= false;
499 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
500 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
502 if (type
< _TK_M_MAX
) {
504 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
506 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
507 match_type
= MATCH_TYPE_SUBSYSTEM
;
508 else if (isempty(value
))
509 match_type
= MATCH_TYPE_EMPTY
;
510 else if (streq(value
, "?*")) {
511 /* Convert KEY=="?*" -> KEY!="" */
512 match_type
= MATCH_TYPE_EMPTY
;
513 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
514 } else if (string_is_glob(value
))
515 match_type
= MATCH_TYPE_GLOB
;
517 match_type
= MATCH_TYPE_PLAIN
;
519 if (type_has_nulstr_value(type
)) {
520 /* Convert value string to nulstr. */
521 bool bar
= true, empty
= false;
524 for (a
= b
= value
; *a
!= '\0'; a
++) {
538 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
546 if (match_type
== MATCH_TYPE_GLOB
)
547 match_type
= MATCH_TYPE_GLOB_WITH_EMPTY
;
548 if (match_type
== MATCH_TYPE_PLAIN
)
549 match_type
= MATCH_TYPE_PLAIN_WITH_EMPTY
;
554 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
559 if (len
> 0 && !isspace(value
[len
- 1]))
560 remove_trailing_whitespace
= true;
562 subst_type
= rule_get_substitution_type(data
);
565 token
= new(UdevRuleToken
, 1);
569 *token
= (UdevRuleToken
) {
574 .match_type
= match_type
,
575 .attr_subst_type
= subst_type
,
576 .attr_match_remove_trailing_whitespace
= remove_trailing_whitespace
,
577 .rule_line
= rule_line
,
580 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
582 if (token
->type
== TK_A_NAME
)
583 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
585 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
586 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
587 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
588 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
590 else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
)
591 SET_FLAG(rule_line
->type
, LINE_HAS_STATIC_NODE
, true);
593 else if (token
->type
>= _TK_A_MIN
||
594 IN_SET(token
->type
, TK_M_PROGRAM
,
595 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
596 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
597 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
603 static void check_value_format_and_warn(UdevRuleLine
*line
, const char *key
, const char *value
, bool nonempty
) {
607 if (nonempty
&& isempty(value
))
608 log_line_invalid_value(line
, key
, value
, (size_t) 0, "empty value");
609 else if (udev_check_format(value
, &offset
, &hint
) < 0)
610 log_line_invalid_value(line
, key
, value
, offset
+ 1, hint
);
613 static int check_attr_format_and_warn(UdevRuleLine
*line
, const char *key
, const char *value
) {
618 return log_line_invalid_attr(line
, key
);
619 if (udev_check_format(value
, &offset
, &hint
) < 0)
620 log_line_invalid_attr_format(line
, key
, value
, offset
+ 1, hint
);
624 static int parse_token(UdevRuleLine
*rule_line
, const char *key
, char *attr
, UdevRuleOperatorType op
, char *value
) {
625 ResolveNameTiming resolve_name_timing
= LINE_GET_RULES(rule_line
)->resolve_name_timing
;
626 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
632 if (streq(key
, "ACTION")) {
634 return log_line_invalid_attr(rule_line
, key
);
636 return log_line_invalid_op(rule_line
, key
);
638 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
);
639 } else if (streq(key
, "DEVPATH")) {
641 return log_line_invalid_attr(rule_line
, key
);
643 return log_line_invalid_op(rule_line
, key
);
645 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
);
646 } else if (streq(key
, "KERNEL")) {
648 return log_line_invalid_attr(rule_line
, key
);
650 return log_line_invalid_op(rule_line
, key
);
652 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
);
653 } else if (streq(key
, "SYMLINK")) {
655 return log_line_invalid_attr(rule_line
, key
);
657 check_value_format_and_warn(rule_line
, key
, value
, false);
658 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
);
660 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
);
661 } else if (streq(key
, "NAME")) {
663 return log_line_invalid_attr(rule_line
, key
);
665 return log_line_invalid_op(rule_line
, key
);
667 log_line_warning(rule_line
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key
);
672 if (streq(value
, "%k"))
673 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
674 "Ignoring NAME=\"%%k\", as it will take no effect.");
676 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
677 "Ignoring NAME=\"\", as udev will not delete any network interfaces.");
678 check_value_format_and_warn(rule_line
, key
, value
, false);
680 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
);
682 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
);
683 } else if (streq(key
, "ENV")) {
685 return log_line_invalid_attr(rule_line
, key
);
687 return log_line_invalid_op(rule_line
, key
);
688 if (op
== OP_ASSIGN_FINAL
) {
689 log_line_warning(rule_line
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
695 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
696 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
697 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
698 "Invalid ENV attribute. '%s' cannot be set.", attr
);
700 check_value_format_and_warn(rule_line
, key
, value
, false);
702 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
);
704 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
);
705 } else if (streq(key
, "CONST")) {
706 if (isempty(attr
) || !STR_IN_SET(attr
, "arch", "virt"))
707 return log_line_invalid_attr(rule_line
, key
);
709 return log_line_invalid_op(rule_line
, key
);
710 r
= rule_line_add_token(rule_line
, TK_M_CONST
, op
, value
, attr
);
711 } else if (streq(key
, "TAG")) {
713 return log_line_invalid_attr(rule_line
, key
);
714 if (op
== OP_ASSIGN_FINAL
) {
715 log_line_warning(rule_line
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
720 check_value_format_and_warn(rule_line
, key
, value
, true);
722 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
);
724 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
);
725 } else if (streq(key
, "SUBSYSTEM")) {
727 return log_line_invalid_attr(rule_line
, key
);
729 return log_line_invalid_op(rule_line
, key
);
731 if (STR_IN_SET(value
, "bus", "class"))
732 log_line_warning(rule_line
, "\"%s\" must be specified as \"subsystem\".", value
);
734 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
735 } else if (streq(key
, "DRIVER")) {
737 return log_line_invalid_attr(rule_line
, key
);
739 return log_line_invalid_op(rule_line
, key
);
741 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
);
742 } else if (streq(key
, "ATTR")) {
743 r
= check_attr_format_and_warn(rule_line
, key
, attr
);
747 return log_line_invalid_op(rule_line
, key
);
748 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
749 log_line_warning(rule_line
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
754 check_value_format_and_warn(rule_line
, key
, value
, false);
755 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
);
757 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
);
758 } else if (streq(key
, "SYSCTL")) {
759 r
= check_attr_format_and_warn(rule_line
, key
, attr
);
763 return log_line_invalid_op(rule_line
, key
);
764 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
765 log_line_warning(rule_line
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
770 check_value_format_and_warn(rule_line
, key
, value
, false);
771 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
);
773 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
);
774 } else if (streq(key
, "KERNELS")) {
776 return log_line_invalid_attr(rule_line
, key
);
778 return log_line_invalid_op(rule_line
, key
);
780 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
);
781 } else if (streq(key
, "SUBSYSTEMS")) {
783 return log_line_invalid_attr(rule_line
, key
);
785 return log_line_invalid_op(rule_line
, key
);
787 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
);
788 } else if (streq(key
, "DRIVERS")) {
790 return log_line_invalid_attr(rule_line
, key
);
792 return log_line_invalid_op(rule_line
, key
);
794 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
);
795 } else if (streq(key
, "ATTRS")) {
796 r
= check_attr_format_and_warn(rule_line
, key
, attr
);
800 return log_line_invalid_op(rule_line
, key
);
802 if (startswith(attr
, "device/"))
803 log_line_warning(rule_line
, "'device' link may not be available in future kernels.");
804 if (strstr(attr
, "../"))
805 log_line_warning(rule_line
, "Direct reference to parent sysfs directory, may break in future kernels.");
807 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
);
808 } else if (streq(key
, "TAGS")) {
810 return log_line_invalid_attr(rule_line
, key
);
812 return log_line_invalid_op(rule_line
, key
);
814 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
);
815 } else if (streq(key
, "TEST")) {
816 mode_t mode
= MODE_INVALID
;
818 if (!isempty(attr
)) {
819 r
= parse_mode(attr
, &mode
);
821 return log_line_error_errno(rule_line
, r
, "Failed to parse mode '%s': %m", attr
);
823 check_value_format_and_warn(rule_line
, key
, value
, true);
825 return log_line_invalid_op(rule_line
, key
);
827 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
));
828 } else if (streq(key
, "PROGRAM")) {
830 return log_line_invalid_attr(rule_line
, key
);
831 check_value_format_and_warn(rule_line
, key
, value
, true);
833 return log_line_invalid_op(rule_line
, key
);
837 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
);
838 } else if (streq(key
, "IMPORT")) {
840 return log_line_invalid_attr(rule_line
, key
);
841 check_value_format_and_warn(rule_line
, key
, value
, true);
843 return log_line_invalid_op(rule_line
, key
);
847 if (streq(attr
, "file"))
848 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
849 else if (streq(attr
, "program")) {
850 UdevBuiltinCommand cmd
;
852 cmd
= udev_builtin_lookup(value
);
854 log_line_debug(rule_line
, "Found builtin command '%s' for %s, replacing attribute.", value
, key
);
855 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
857 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
);
858 } else if (streq(attr
, "builtin")) {
859 UdevBuiltinCommand cmd
;
861 cmd
= udev_builtin_lookup(value
);
863 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
864 "Unknown builtin command: %s", value
);
865 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
866 } else if (streq(attr
, "db"))
867 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
);
868 else if (streq(attr
, "cmdline"))
869 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
870 else if (streq(attr
, "parent"))
871 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
873 return log_line_invalid_attr(rule_line
, key
);
874 } else if (streq(key
, "RESULT")) {
876 return log_line_invalid_attr(rule_line
, key
);
878 return log_line_invalid_op(rule_line
, key
);
880 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
);
881 } else if (streq(key
, "OPTIONS")) {
885 return log_line_invalid_attr(rule_line
, key
);
886 if (is_match
|| op
== OP_REMOVE
)
887 return log_line_invalid_op(rule_line
, key
);
891 if (streq(value
, "string_escape=none"))
892 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
893 else if (streq(value
, "string_escape=replace"))
894 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
895 else if (streq(value
, "db_persist"))
896 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
);
897 else if (streq(value
, "watch"))
898 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1));
899 else if (streq(value
, "nowatch"))
900 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0));
901 else if ((tmp
= startswith(value
, "static_node=")))
902 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
);
903 else if ((tmp
= startswith(value
, "link_priority="))) {
906 r
= safe_atoi(tmp
, &prio
);
908 return log_line_error_errno(rule_line
, r
, "Failed to parse link priority '%s': %m", tmp
);
909 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
));
910 } else if ((tmp
= startswith(value
, "log_level="))) {
913 if (streq(tmp
, "reset"))
916 level
= log_level_from_string(tmp
);
918 return log_line_error_errno(rule_line
, level
, "Failed to parse log level '%s': %m", tmp
);
920 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_LOG_LEVEL
, op
, NULL
, INT_TO_PTR(level
));
922 log_line_warning(rule_line
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
925 } else if (streq(key
, "OWNER")) {
929 return log_line_invalid_attr(rule_line
, key
);
930 if (is_match
|| op
== OP_REMOVE
)
931 return log_line_invalid_op(rule_line
, key
);
933 log_line_warning(rule_line
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
937 if (parse_uid(value
, &uid
) >= 0)
938 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
939 else if (resolve_name_timing
== RESOLVE_NAME_EARLY
&&
940 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
941 r
= rule_resolve_user(rule_line
, value
, &uid
);
943 return log_line_error_errno(rule_line
, r
, "Failed to resolve user name '%s': %m", value
);
945 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
946 } else if (resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
947 check_value_format_and_warn(rule_line
, key
, value
, true);
948 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
);
950 log_line_debug(rule_line
, "User name resolution is disabled, ignoring %s=\"%s\".", key
, value
);
953 } else if (streq(key
, "GROUP")) {
957 return log_line_invalid_attr(rule_line
, key
);
958 if (is_match
|| op
== OP_REMOVE
)
959 return log_line_invalid_op(rule_line
, key
);
961 log_line_warning(rule_line
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
965 if (parse_gid(value
, &gid
) >= 0)
966 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
967 else if (resolve_name_timing
== RESOLVE_NAME_EARLY
&&
968 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
969 r
= rule_resolve_group(rule_line
, value
, &gid
);
971 return log_line_error_errno(rule_line
, r
, "Failed to resolve group name '%s': %m", value
);
973 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
974 } else if (resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
975 check_value_format_and_warn(rule_line
, key
, value
, true);
976 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
);
978 log_line_debug(rule_line
, "Resolving group name is disabled, ignoring GROUP=\"%s\".", value
);
981 } else if (streq(key
, "MODE")) {
985 return log_line_invalid_attr(rule_line
, key
);
986 if (is_match
|| op
== OP_REMOVE
)
987 return log_line_invalid_op(rule_line
, key
);
989 log_line_warning(rule_line
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
993 if (parse_mode(value
, &mode
) >= 0)
994 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
));
996 check_value_format_and_warn(rule_line
, key
, value
, true);
997 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
);
999 } else if (streq(key
, "SECLABEL")) {
1001 return log_line_invalid_attr(rule_line
, key
);
1002 check_value_format_and_warn(rule_line
, key
, value
, true);
1003 if (is_match
|| op
== OP_REMOVE
)
1004 return log_line_invalid_op(rule_line
, key
);
1005 if (op
== OP_ASSIGN_FINAL
) {
1006 log_line_warning(rule_line
, "%s key takes '=' or '+=' operator, assuming '='.", key
);
1010 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, attr
);
1011 } else if (streq(key
, "RUN")) {
1012 if (is_match
|| op
== OP_REMOVE
)
1013 return log_line_invalid_op(rule_line
, key
);
1014 check_value_format_and_warn(rule_line
, key
, value
, true);
1015 if (!attr
|| streq(attr
, "program"))
1016 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
);
1017 else if (streq(attr
, "builtin")) {
1018 UdevBuiltinCommand cmd
;
1020 cmd
= udev_builtin_lookup(value
);
1022 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
1023 "Unknown builtin command '%s', ignoring.", value
);
1024 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
1026 return log_line_invalid_attr(rule_line
, key
);
1027 } else if (streq(key
, "GOTO")) {
1029 return log_line_invalid_attr(rule_line
, key
);
1030 if (op
!= OP_ASSIGN
)
1031 return log_line_invalid_op(rule_line
, key
);
1032 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
1033 log_line_warning(rule_line
, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value
);
1037 rule_line
->goto_label
= value
;
1038 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
1040 } else if (streq(key
, "LABEL")) {
1042 return log_line_invalid_attr(rule_line
, key
);
1043 if (op
!= OP_ASSIGN
)
1044 return log_line_invalid_op(rule_line
, key
);
1045 if (FLAGS_SET(rule_line
->type
, LINE_HAS_LABEL
))
1046 log_line_warning(rule_line
, "Contains multiple LABEL keys, ignoring LABEL=\"%s\".",
1049 rule_line
->label
= value
;
1050 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
1053 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'.", key
);
1060 static UdevRuleOperatorType
parse_operator(const char *op
) {
1063 if (startswith(op
, "=="))
1065 if (startswith(op
, "!="))
1067 if (startswith(op
, "+="))
1069 if (startswith(op
, "-="))
1071 if (startswith(op
, "="))
1073 if (startswith(op
, ":="))
1074 return OP_ASSIGN_FINAL
;
1076 return _OP_TYPE_INVALID
;
1079 static void check_token_delimiters(UdevRuleLine
*rule_line
, const char *line
) {
1083 bool ws_before_comma
= false, ws_after_comma
= false;
1086 for (p
= line
; !isempty(p
); ++p
) {
1089 else if (strchr(WHITESPACE
, *p
)) {
1091 ws_after_comma
= true;
1093 ws_before_comma
= true;
1098 if (line
== rule_line
->line
) {
1099 /* this is the first token of the rule */
1101 log_line_notice(rule_line
, "style: stray leading comma.");
1102 } else if (isempty(p
)) {
1103 /* there are no more tokens in the rule */
1105 log_line_notice(rule_line
, "style: stray trailing comma.");
1107 /* single comma is expected */
1109 log_line_notice(rule_line
, "style: a comma between tokens is expected.");
1110 else if (n_comma
> 1)
1111 log_line_notice(rule_line
, "style: more than one comma between tokens.");
1113 /* whitespace after comma is expected */
1115 if (ws_before_comma
)
1116 log_line_notice(rule_line
, "style: stray whitespace before comma.");
1117 if (!ws_after_comma
)
1118 log_line_notice(rule_line
, "style: whitespace after comma is expected.");
1119 } else if (!ws_before_comma
&& !ws_after_comma
)
1120 log_line_notice(rule_line
, "style: whitespace between tokens is expected.");
1124 int udev_rule_parse_value(char *str
, char **ret_value
, char **ret_endpos
) {
1128 /* value must be double quotated */
1129 is_escaped
= str
[0] == 'e';
1135 /* unescape double quotation '\"'->'"' */
1136 for (j
= str
, i
= str
+ 1; *i
!= '"'; i
++, j
++) {
1139 if (i
[0] == '\\' && i
[1] == '"')
1145 * The return value must be terminated by two subsequent NULs
1146 * so it could be safely interpreted as nulstr.
1150 _cleanup_free_
char *unescaped
= NULL
;
1153 /* find the end position of value */
1154 for (i
= str
+ 1; *i
!= '"'; i
++) {
1162 l
= cunescape_length(str
+ 1, i
- (str
+ 1), 0, &unescaped
);
1166 assert(l
<= i
- (str
+ 1));
1167 memcpy(str
, unescaped
, l
+ 1);
1169 * The return value must be terminated by two subsequent NULs
1170 * so it could be safely interpreted as nulstr.
1176 *ret_endpos
= i
+ 1;
1180 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
) {
1181 char *key_begin
, *key_end
, *attr
, *tmp
;
1182 UdevRuleOperatorType op
;
1191 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
1193 if (isempty(key_begin
))
1196 for (key_end
= key_begin
; ; key_end
++) {
1197 if (key_end
[0] == '\0')
1199 if (strchr(WHITESPACE
"={", key_end
[0]))
1201 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
1204 if (key_end
[0] == '{') {
1206 tmp
= strchr(attr
, '}');
1215 tmp
= skip_leading_chars(tmp
, NULL
);
1216 op
= parse_operator(tmp
);
1222 tmp
+= op
== OP_ASSIGN
? 1 : 2;
1223 tmp
= skip_leading_chars(tmp
, NULL
);
1224 r
= udev_rule_parse_value(tmp
, ret_value
, line
);
1228 *ret_key
= key_begin
;
1234 static void check_tokens_order(UdevRuleLine
*rule_line
) {
1235 bool has_result
= false;
1239 LIST_FOREACH(tokens
, t
, rule_line
->tokens
)
1240 if (t
->type
== TK_M_RESULT
)
1242 else if (has_result
&& t
->type
== TK_M_PROGRAM
) {
1243 log_line_warning(rule_line
, "Reordering RESULT check after PROGRAM assignment.");
1248 static void sort_tokens(UdevRuleLine
*rule_line
) {
1251 UdevRuleToken
*old_tokens
= TAKE_PTR(rule_line
->tokens
);
1253 while (old_tokens
) {
1254 UdevRuleToken
*min_token
= NULL
;
1256 LIST_FOREACH(tokens
, t
, old_tokens
)
1257 if (!min_token
|| min_token
->type
> t
->type
)
1260 LIST_REMOVE(tokens
, old_tokens
, min_token
);
1261 LIST_APPEND(tokens
, rule_line
->tokens
, min_token
);
1265 static int rule_add_line(UdevRuleFile
*rule_file
, const char *line_str
, unsigned line_nr
, bool extra_checks
) {
1266 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
1267 _cleanup_free_
char *line
= NULL
;
1274 if (isempty(line_str
))
1277 line
= strdup(line_str
);
1281 rule_line
= new(UdevRuleLine
, 1);
1285 *rule_line
= (UdevRuleLine
) {
1286 .line
= TAKE_PTR(line
),
1287 .line_number
= line_nr
,
1288 .rule_file
= rule_file
,
1291 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
1293 for (p
= rule_line
->line
; !isempty(p
); ) {
1294 char *key
, *attr
, *value
;
1295 UdevRuleOperatorType op
;
1298 check_token_delimiters(rule_line
, p
);
1300 r
= parse_line(&p
, &key
, &attr
, &op
, &value
);
1302 return log_line_error_errno(rule_line
, r
, "Invalid key/value pair, ignoring.");
1306 r
= parse_token(rule_line
, key
, attr
, op
, value
);
1311 if (rule_line
->type
== 0) {
1312 log_line_warning(rule_line
, "The line has no effect, ignoring.");
1317 check_tokens_order(rule_line
);
1319 sort_tokens(rule_line
);
1320 TAKE_PTR(rule_line
);
1324 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
1327 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1328 LIST_FOREACH(rule_lines
, line
, rule_file
->rule_lines
) {
1329 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
1332 LIST_FOREACH(rule_lines
, i
, line
->rule_lines_next
)
1333 if (streq_ptr(i
->label
, line
->goto_label
)) {
1334 line
->goto_line
= i
;
1335 SET_FLAG(i
->type
, LINE_IS_REFERENCED
, true);
1339 if (!line
->goto_line
) {
1340 log_line_error(line
, "GOTO=\"%s\" has no matching label, ignoring.",
1343 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
1344 line
->goto_label
= NULL
;
1346 if ((line
->type
& ~(LINE_HAS_LABEL
|LINE_IS_REFERENCED
)) == 0) {
1347 log_line_warning(line
, "The line has no effect any more, dropping.");
1348 /* LINE_IS_REFERENCED implies LINE_HAS_LABEL */
1349 if (line
->type
& LINE_HAS_LABEL
)
1350 udev_rule_line_clear_tokens(line
);
1352 udev_rule_line_free(line
);
1358 static bool token_data_is_string(UdevRuleTokenType type
) {
1359 return IN_SET(type
, TK_M_ENV
,
1370 static bool token_type_and_data_eq(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1374 return a
->type
== b
->type
&&
1375 (token_data_is_string(a
->type
) ? streq_ptr(a
->data
, b
->data
) : (a
->data
== b
->data
));
1378 static bool nulstr_eq(const char *a
, const char *b
) {
1379 NULSTR_FOREACH(i
, a
)
1380 if (!nulstr_contains(b
, i
))
1383 NULSTR_FOREACH(i
, b
)
1384 if (!nulstr_contains(a
, i
))
1390 static bool token_type_and_value_eq(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1394 if (a
->type
!= b
->type
||
1395 a
->match_type
!= b
->match_type
)
1398 /* token value is ignored for certain match types */
1399 if (IN_SET(a
->match_type
, MATCH_TYPE_EMPTY
, MATCH_TYPE_SUBSYSTEM
))
1402 return type_has_nulstr_value(a
->type
) ? nulstr_eq(a
->value
, b
->value
) :
1403 streq_ptr(a
->value
, b
->value
);
1406 static bool conflicting_op(UdevRuleOperatorType a
, UdevRuleOperatorType b
) {
1407 return (a
== OP_MATCH
&& b
== OP_NOMATCH
) ||
1408 (a
== OP_NOMATCH
&& b
== OP_MATCH
);
1411 /* test whether all fields besides UdevRuleOperatorType of two tokens match */
1412 static bool tokens_eq(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1416 return a
->attr_subst_type
== b
->attr_subst_type
&&
1417 a
->attr_match_remove_trailing_whitespace
== b
->attr_match_remove_trailing_whitespace
&&
1418 token_type_and_value_eq(a
, b
) &&
1419 token_type_and_data_eq(a
, b
);
1422 static bool nulstr_tokens_conflict(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1426 if (!(a
->type
== b
->type
&&
1427 type_has_nulstr_value(a
->type
) &&
1429 a
->op
== OP_MATCH
&&
1430 a
->match_type
== b
->match_type
&&
1431 a
->attr_subst_type
== b
->attr_subst_type
&&
1432 a
->attr_match_remove_trailing_whitespace
== b
->attr_match_remove_trailing_whitespace
&&
1433 token_type_and_data_eq(a
, b
)))
1436 if (a
->match_type
== MATCH_TYPE_PLAIN
) {
1437 NULSTR_FOREACH(i
, a
->value
)
1438 if (nulstr_contains(b
->value
, i
))
1443 if (a
->match_type
== MATCH_TYPE_GLOB
) {
1444 NULSTR_FOREACH(i
, a
->value
) {
1445 size_t i_n
= strcspn(i
, GLOB_CHARS
);
1448 NULSTR_FOREACH(j
, b
->value
) {
1449 size_t j_n
= strcspn(j
, GLOB_CHARS
);
1450 if (j_n
== 0 || strneq(i
, j
, MIN(i_n
, j_n
)))
1461 static void udev_check_unused_labels(UdevRuleLine
*line
) {
1464 if (FLAGS_SET(line
->type
, LINE_HAS_LABEL
) &&
1465 !FLAGS_SET(line
->type
, LINE_IS_REFERENCED
))
1466 log_line_notice(line
, "style: LABEL=\"%s\" is unused.", line
->label
);
1469 static void udev_check_conflicts_duplicates(UdevRuleLine
*line
) {
1472 bool conflicts
= false, duplicates
= false;
1474 LIST_FOREACH(tokens
, token
, line
->tokens
)
1475 LIST_FOREACH(tokens
, i
, token
->tokens_next
) {
1476 bool new_conflicts
= false, new_duplicates
= false;
1478 if (tokens_eq(token
, i
)) {
1479 if (!duplicates
&& token
->op
== i
->op
)
1480 new_duplicates
= true;
1481 if (!conflicts
&& conflicting_op(token
->op
, i
->op
))
1482 new_conflicts
= true;
1483 } else if (!conflicts
&& nulstr_tokens_conflict(token
, i
))
1484 new_conflicts
= true;
1488 if (new_duplicates
) {
1489 duplicates
= new_duplicates
;
1490 log_line_warning(line
, "duplicate expressions.");
1492 if (new_conflicts
) {
1493 conflicts
= new_conflicts
;
1494 log_line_error(line
, "conflicting match expressions, the line has no effect.");
1496 if (conflicts
&& duplicates
)
1501 static void udev_check_rule_line(UdevRuleLine
*line
) {
1502 udev_check_unused_labels(line
);
1503 udev_check_conflicts_duplicates(line
);
1506 int udev_rules_parse_file(UdevRules
*rules
, const char *filename
, bool extra_checks
, UdevRuleFile
**ret
) {
1507 _cleanup_(udev_rule_file_freep
) UdevRuleFile
*rule_file
= NULL
;
1508 _cleanup_free_
char *continuation
= NULL
, *name
= NULL
;
1509 _cleanup_fclose_
FILE *f
= NULL
;
1510 bool ignore_line
= false;
1511 unsigned line_nr
= 0;
1518 f
= fopen(filename
, "re");
1523 if (errno
== ENOENT
)
1526 return log_warning_errno(errno
, "Failed to open %s, ignoring: %m", filename
);
1529 if (fstat(fileno(f
), &st
) < 0)
1530 return log_warning_errno(errno
, "Failed to stat %s, ignoring: %m", filename
);
1532 if (null_or_empty(&st
)) {
1533 log_debug("Skipping empty file: %s", filename
);
1539 r
= hashmap_put_stats_by_path(&rules
->stats_by_path
, filename
, &st
);
1541 return log_warning_errno(errno
, "Failed to save stat for %s, ignoring: %m", filename
);
1543 (void) fd_warn_permissions(filename
, fileno(f
));
1545 log_debug("Reading rules file: %s", filename
);
1547 name
= strdup(filename
);
1551 rule_file
= new(UdevRuleFile
, 1);
1555 *rule_file
= (UdevRuleFile
) {
1556 .filename
= TAKE_PTR(name
),
1560 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1563 _cleanup_free_
char *buf
= NULL
;
1567 r
= read_line(f
, UDEV_LINE_SIZE
, &buf
);
1574 line
= skip_leading_chars(buf
, NULL
);
1576 /* Lines beginning with '#' are ignored regardless of line continuation. */
1582 if (continuation
&& !ignore_line
) {
1583 if (strlen(continuation
) + len
>= UDEV_LINE_SIZE
)
1586 if (!strextend(&continuation
, line
))
1590 line
= continuation
;
1595 if (len
> 0 && line
[len
- 1] == '\\') {
1599 line
[len
- 1] = '\0';
1600 if (!continuation
) {
1601 continuation
= strdup(line
);
1610 log_file_error(rule_file
, line_nr
, "Line is too long, ignored.");
1612 (void) rule_add_line(rule_file
, line
, line_nr
, extra_checks
);
1614 continuation
= mfree(continuation
);
1615 ignore_line
= false;
1619 log_file_error(rule_file
, line_nr
,
1620 "Unexpected EOF after line continuation, line ignored.");
1622 rule_resolve_goto(rule_file
);
1625 LIST_FOREACH(rule_lines
, line
, rule_file
->rule_lines
)
1626 udev_check_rule_line(line
);
1631 TAKE_PTR(rule_file
);
1635 unsigned udev_rule_file_get_issues(UdevRuleFile
*rule_file
) {
1638 return rule_file
->issues
;
1641 UdevRules
* udev_rules_new(ResolveNameTiming resolve_name_timing
) {
1642 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1644 UdevRules
*rules
= new(UdevRules
, 1);
1648 *rules
= (UdevRules
) {
1649 .resolve_name_timing
= resolve_name_timing
,
1655 int udev_rules_load(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1656 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1657 _cleanup_strv_free_
char **files
= NULL
;
1660 rules
= udev_rules_new(resolve_name_timing
);
1664 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1666 return log_debug_errno(r
, "Failed to enumerate rules files: %m");
1668 STRV_FOREACH(f
, files
) {
1669 r
= udev_rules_parse_file(rules
, *f
, /* extra_checks = */ false, NULL
);
1671 log_debug_errno(r
, "Failed to read rules file %s, ignoring: %m", *f
);
1674 *ret_rules
= TAKE_PTR(rules
);
1678 bool udev_rules_should_reload(UdevRules
*rules
) {
1679 _cleanup_hashmap_free_ Hashmap
*stats_by_path
= NULL
;
1685 r
= config_get_stats_by_path(".rules", NULL
, 0, RULES_DIRS
, /* check_dropins = */ false, &stats_by_path
);
1687 log_warning_errno(r
, "Failed to get stats of udev rules, ignoring: %m");
1691 if (!stats_by_path_equal(rules
->stats_by_path
, stats_by_path
)) {
1692 log_debug("Udev rules need reloading");
1699 static bool token_match_string(UdevRuleToken
*token
, const char *str
) {
1704 assert(token
->value
);
1705 assert(token
->type
< _TK_M_MAX
);
1707 str
= strempty(str
);
1708 value
= token
->value
;
1710 switch (token
->match_type
) {
1711 case MATCH_TYPE_EMPTY
:
1712 match
= isempty(str
);
1714 case MATCH_TYPE_SUBSYSTEM
:
1715 match
= STR_IN_SET(str
, "subsystem", "class", "bus");
1717 case MATCH_TYPE_PLAIN_WITH_EMPTY
:
1723 case MATCH_TYPE_PLAIN
:
1724 NULSTR_FOREACH(i
, value
)
1725 if (streq(i
, str
)) {
1730 case MATCH_TYPE_GLOB_WITH_EMPTY
:
1736 case MATCH_TYPE_GLOB
:
1737 NULSTR_FOREACH(i
, value
)
1738 if ((fnmatch(i
, str
, 0) == 0)) {
1744 assert_not_reached();
1747 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1750 static bool token_match_attr(UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
1751 char nbuf
[UDEV_NAME_SIZE
], vbuf
[UDEV_NAME_SIZE
];
1752 const char *name
, *value
;
1756 assert(IN_SET(token
->type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
));
1762 switch (token
->attr_subst_type
) {
1763 case SUBST_TYPE_FORMAT
:
1764 (void) udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false, &truncated
);
1766 log_event_truncated(dev
, token
, "sysfs attribute name", name
,
1767 token
->type
== TK_M_ATTR
? "ATTR" : "ATTRS", /* is_match = */ true);
1773 case SUBST_TYPE_PLAIN
:
1774 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1777 case SUBST_TYPE_SUBSYS
:
1778 if (udev_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1783 assert_not_reached();
1786 /* remove trailing whitespace, if not asked to match for it */
1787 if (token
->attr_match_remove_trailing_whitespace
) {
1788 if (value
!= vbuf
) {
1789 strscpy(vbuf
, sizeof(vbuf
), value
);
1793 delete_trailing_chars(vbuf
, NULL
);
1796 return token_match_string(token
, value
);
1799 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
1808 key
= skip_leading_chars(line
, NULL
);
1810 /* comment or empty line */
1811 if (IN_SET(key
[0], '#', '\0')) {
1812 *ret_key
= *ret_value
= NULL
;
1816 /* split key/value */
1817 val
= strchr(key
, '=');
1822 key
= strstrip(key
);
1826 val
= strstrip(val
);
1831 if (IN_SET(val
[0], '"', '\'')) {
1833 if (len
== 1 || val
[len
-1] != val
[0])
1844 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
1851 r
= sd_device_get_parent(dev
, &parent
);
1857 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
1858 if (fnmatch(filter
, key
, 0) != 0)
1860 r
= device_add_property(dev
, key
, val
);
1868 static int attr_subst_subdir(char attr
[static UDEV_PATH_SIZE
]) {
1869 _cleanup_closedir_
DIR *dir
= NULL
;
1870 char buf
[UDEV_PATH_SIZE
], *p
;
1877 tail
= strstr(attr
, "/*/");
1881 len
= tail
- attr
+ 1; /* include slash at the end */
1882 tail
+= 2; /* include slash at the beginning */
1886 size
-= strnpcpy_full(&p
, size
, attr
, len
, &truncated
);
1894 FOREACH_DIRENT_ALL(de
, dir
, break) {
1895 if (de
->d_name
[0] == '.')
1898 strscpyl_full(p
, size
, &truncated
, de
->d_name
, tail
, NULL
);
1902 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
1912 static size_t udev_replace_ifname(char *str
) {
1913 size_t replaced
= 0;
1917 /* See ifname_valid_full(). */
1919 for (char *p
= str
; *p
!= '\0'; p
++)
1920 if (!ifname_valid_char(*p
)) {
1928 static int udev_rule_apply_token_to_event(
1929 UdevRuleToken
*token
,
1939 /* This returns the following values:
1940 * 0 on the current token does not match the event,
1941 * 1 on the current token matches the event, and
1942 * negative errno on some critical errors. */
1944 switch (token
->type
) {
1946 sd_device_action_t a
;
1948 r
= sd_device_get_action(dev
, &a
);
1950 return log_event_error_errno(dev
, token
, r
, "Failed to get uevent action type: %m");
1952 return token_match_string(token
, device_action_to_string(a
));
1954 case TK_M_DEVPATH
: {
1957 r
= sd_device_get_devpath(dev
, &val
);
1959 return log_event_error_errno(dev
, token
, r
, "Failed to get devpath: %m");
1961 return token_match_string(token
, val
);
1964 case TK_M_PARENTS_KERNEL
: {
1967 r
= sd_device_get_sysname(dev
, &val
);
1969 return log_event_error_errno(dev
, token
, r
, "Failed to get sysname: %m");
1971 return token_match_string(token
, val
);
1974 FOREACH_DEVICE_DEVLINK(dev
, val
)
1975 if (token_match_string(token
, strempty(startswith(val
, "/dev/"))) == (token
->op
== OP_MATCH
))
1976 return token
->op
== OP_MATCH
;
1977 return token
->op
== OP_NOMATCH
;
1979 return token_match_string(token
, event
->name
);
1981 const char *val
= NULL
;
1983 (void) device_get_property_value_with_fallback(dev
, token
->data
, event
->worker
? event
->worker
->properties
: NULL
, &val
);
1985 return token_match_string(token
, val
);
1988 const char *val
, *k
= token
->data
;
1990 if (streq(k
, "arch"))
1991 val
= architecture_to_string(uname_architecture());
1992 else if (streq(k
, "virt"))
1993 val
= virtualization_to_string(detect_virtualization());
1994 else if (streq(k
, "cvm"))
1995 val
= confidential_virtualization_to_string(detect_confidential_virtualization());
1997 assert_not_reached();
1998 return token_match_string(token
, val
);
2001 case TK_M_PARENTS_TAG
:
2002 FOREACH_DEVICE_CURRENT_TAG(dev
, val
)
2003 if (token_match_string(token
, val
) == (token
->op
== OP_MATCH
))
2004 return token
->op
== OP_MATCH
;
2005 return token
->op
== OP_NOMATCH
;
2006 case TK_M_SUBSYSTEM
:
2007 case TK_M_PARENTS_SUBSYSTEM
: {
2010 r
= sd_device_get_subsystem(dev
, &val
);
2014 return log_event_error_errno(dev
, token
, r
, "Failed to get subsystem: %m");
2016 return token_match_string(token
, val
);
2019 case TK_M_PARENTS_DRIVER
: {
2022 r
= sd_device_get_driver(dev
, &val
);
2026 return log_event_error_errno(dev
, token
, r
, "Failed to get driver: %m");
2028 return token_match_string(token
, val
);
2031 case TK_M_PARENTS_ATTR
:
2032 return token_match_attr(token
, dev
, event
);
2034 _cleanup_free_
char *value
= NULL
;
2035 char buf
[UDEV_PATH_SIZE
];
2038 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
2040 log_event_truncated(dev
, token
, "sysctl entry name", token
->data
, "SYSCTL", /* is_match = */ true);
2044 r
= sysctl_read(sysctl_normalize(buf
), &value
);
2045 if (r
< 0 && r
!= -ENOENT
)
2046 return log_event_error_errno(dev
, token
, r
, "Failed to read sysctl '%s': %m", buf
);
2048 return token_match_string(token
, strstrip(value
));
2051 mode_t mode
= PTR_TO_MODE(token
->data
);
2052 char buf
[UDEV_PATH_SIZE
];
2053 struct stat statbuf
;
2054 bool match
, truncated
;
2056 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2058 log_event_truncated(dev
, token
, "file name", token
->value
, "TEST", /* is_match = */ true);
2062 if (!path_is_absolute(buf
) &&
2063 udev_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
2064 char tmp
[UDEV_PATH_SIZE
];
2067 r
= sd_device_get_syspath(dev
, &val
);
2069 return log_event_error_errno(dev
, token
, r
, "Failed to get syspath: %m");
2071 strscpy_full(tmp
, sizeof(tmp
), buf
, &truncated
);
2073 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", tmp
, NULL
);
2078 r
= attr_subst_subdir(buf
);
2080 return token
->op
== OP_NOMATCH
;
2082 return log_event_error_errno(dev
, token
, r
, "Failed to test for the existence of '%s': %m", buf
);
2084 if (stat(buf
, &statbuf
) < 0)
2085 return token
->op
== OP_NOMATCH
;
2087 if (mode
== MODE_INVALID
)
2088 return token
->op
== OP_MATCH
;
2090 match
= (statbuf
.st_mode
& mode
) > 0;
2091 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
2093 case TK_M_PROGRAM
: {
2094 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
2098 event
->program_result
= mfree(event
->program_result
);
2099 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2101 log_event_truncated(dev
, token
, "command", token
->value
, "PROGRAM", /* is_match = */ true);
2105 log_event_debug(dev
, token
, "Running PROGRAM '%s'", buf
);
2107 r
= udev_event_spawn(event
, /* accept_failure = */ true, buf
, result
, sizeof(result
), NULL
);
2110 log_event_warning_errno(dev
, token
, r
, "Failed to execute \"%s\": %m", buf
);
2111 else /* returned value is positive when program fails */
2112 log_event_debug(dev
, token
, "Command \"%s\" returned %d (error)", buf
, r
);
2113 return token
->op
== OP_NOMATCH
;
2116 delete_trailing_chars(result
, "\n");
2117 count
= udev_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2119 log_event_debug(dev
, token
,
2120 "Replaced %zu character(s) in result of \"%s\"",
2123 event
->program_result
= strdup(result
);
2124 return token
->op
== OP_MATCH
;
2126 case TK_M_IMPORT_FILE
: {
2127 _cleanup_fclose_
FILE *f
= NULL
;
2128 char buf
[UDEV_PATH_SIZE
];
2131 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2133 log_event_truncated(dev
, token
, "file name to be imported", token
->value
, "IMPORT", /* is_match = */ true);
2137 log_event_debug(dev
, token
, "Importing properties from '%s'", buf
);
2139 f
= fopen(buf
, "re");
2141 if (errno
!= ENOENT
)
2142 return log_event_error_errno(dev
, token
, errno
, "Failed to open '%s': %m", buf
);
2143 return token
->op
== OP_NOMATCH
;
2147 _cleanup_free_
char *line
= NULL
;
2150 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2152 log_event_debug_errno(dev
, token
, r
, "Failed to read '%s', ignoring: %m", buf
);
2153 return token
->op
== OP_NOMATCH
;
2158 r
= get_property_from_string(line
, &key
, &value
);
2160 log_event_debug_errno(dev
, token
, r
,
2161 "Failed to parse key and value from '%s', ignoring: %m",
2168 r
= device_add_property(dev
, key
, value
);
2170 return log_event_error_errno(dev
, token
, r
,
2171 "Failed to add property %s=%s: %m",
2175 return token
->op
== OP_MATCH
;
2177 case TK_M_IMPORT_PROGRAM
: {
2178 _cleanup_strv_free_
char **lines
= NULL
;
2179 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
2182 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2184 log_event_truncated(dev
, token
, "command", token
->value
, "IMPORT", /* is_match = */ true);
2188 log_event_debug(dev
, token
, "Importing properties from results of '%s'", buf
);
2190 r
= udev_event_spawn(event
, /* accept_failure = */ true, buf
, result
, sizeof result
, &truncated
);
2193 log_event_warning_errno(dev
, token
, r
, "Failed to execute '%s', ignoring: %m", buf
);
2194 else /* returned value is positive when program fails */
2195 log_event_debug(dev
, token
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
2196 return token
->op
== OP_NOMATCH
;
2202 /* Drop the last line. */
2203 for (char *p
= PTR_SUB1(buf
+ strlen(buf
), buf
); p
; p
= PTR_SUB1(p
, buf
))
2204 if (strchr(NEWLINE
, *p
)) {
2211 r
= strv_split_newlines_full(&lines
, result
, EXTRACT_RETAIN_ESCAPE
);
2215 log_event_warning_errno(dev
, token
, r
,
2216 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf
);
2220 STRV_FOREACH(line
, lines
) {
2223 r
= get_property_from_string(*line
, &key
, &value
);
2225 log_event_debug_errno(dev
, token
, r
,
2226 "Failed to parse key and value from '%s', ignoring: %m",
2233 r
= device_add_property(dev
, key
, value
);
2235 return log_event_error_errno(dev
, token
, r
,
2236 "Failed to add property %s=%s: %m",
2240 return token
->op
== OP_MATCH
;
2242 case TK_M_IMPORT_BUILTIN
: {
2243 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
2244 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
2245 unsigned mask
= 1U << (int) cmd
;
2246 char buf
[UDEV_LINE_SIZE
];
2249 if (udev_builtin_run_once(cmd
)) {
2250 /* check if we ran already */
2251 if (event
->builtin_run
& mask
) {
2252 log_event_debug(dev
, token
, "Skipping builtin '%s' in IMPORT key",
2253 udev_builtin_name(cmd
));
2254 /* return the result from earlier run */
2255 return token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
);
2258 event
->builtin_run
|= mask
;
2261 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2263 log_event_truncated(dev
, token
, "builtin command", token
->value
, "IMPORT", /* is_match = */ true);
2267 log_event_debug(dev
, token
, "Importing properties from results of builtin command '%s'", buf
);
2269 r
= udev_builtin_run(event
, cmd
, buf
, false);
2271 /* remember failure */
2272 log_event_debug_errno(dev
, token
, r
, "Failed to run builtin '%s': %m", buf
);
2273 event
->builtin_ret
|= mask
;
2275 return token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
);
2277 case TK_M_IMPORT_DB
: {
2280 if (!event
->dev_db_clone
)
2281 return token
->op
== OP_NOMATCH
;
2282 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
2284 return token
->op
== OP_NOMATCH
;
2286 return log_event_error_errno(dev
, token
, r
,
2287 "Failed to get property '%s' from database: %m",
2290 r
= device_add_property(dev
, token
->value
, val
);
2292 return log_event_error_errno(dev
, token
, r
, "Failed to add property '%s=%s': %m",
2294 return token
->op
== OP_MATCH
;
2296 case TK_M_IMPORT_CMDLINE
: {
2297 _cleanup_free_
char *value
= NULL
;
2299 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
|PROC_CMDLINE_IGNORE_EFI_OPTIONS
, &value
);
2301 return log_event_error_errno(dev
, token
, r
,
2302 "Failed to read '%s' option from /proc/cmdline: %m",
2305 return token
->op
== OP_NOMATCH
;
2307 r
= device_add_property(dev
, token
->value
, value
?: "1");
2309 return log_event_error_errno(dev
, token
, r
, "Failed to add property '%s=%s': %m",
2310 token
->value
, value
?: "1");
2311 return token
->op
== OP_MATCH
;
2313 case TK_M_IMPORT_PARENT
: {
2314 char buf
[UDEV_PATH_SIZE
];
2317 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2319 log_event_truncated(dev
, token
, "property name", token
->value
, "IMPORT", /* is_match = */ true);
2323 r
= import_parent_into_properties(dev
, buf
);
2325 return log_event_error_errno(dev
, token
, r
,
2326 "Failed to import properties '%s' from parent: %m",
2328 return token
->op
== (r
> 0 ? OP_MATCH
: OP_NOMATCH
);
2331 return token_match_string(token
, event
->program_result
);
2332 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
2333 event
->esc
= ESCAPE_NONE
;
2335 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
2336 event
->esc
= ESCAPE_REPLACE
;
2338 case TK_A_OPTIONS_DB_PERSIST
:
2339 device_set_db_persist(dev
);
2341 case TK_A_OPTIONS_INOTIFY_WATCH
:
2342 if (event
->inotify_watch_final
)
2344 if (token
->op
== OP_ASSIGN_FINAL
)
2345 event
->inotify_watch_final
= true;
2347 event
->inotify_watch
= token
->data
;
2349 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
2350 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
2352 case TK_A_OPTIONS_LOG_LEVEL
: {
2353 int level
= PTR_TO_INT(token
->data
);
2356 level
= event
->default_log_level
;
2358 log_set_max_level(level
);
2360 if (level
== LOG_DEBUG
&& !event
->log_level_was_debug
) {
2361 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
2362 log_device_uevent(dev
, "The log level is changed to 'debug' while processing device");
2363 event
->log_level_was_debug
= true;
2369 char owner
[UDEV_NAME_SIZE
];
2370 const char *ow
= owner
;
2373 if (event
->owner_final
)
2375 if (token
->op
== OP_ASSIGN_FINAL
)
2376 event
->owner_final
= true;
2378 (void) udev_event_apply_format(event
, token
->value
, owner
, sizeof(owner
), false, &truncated
);
2380 log_event_truncated(dev
, token
, "user name", token
->value
, "OWNER", /* is_match = */ false);
2384 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2386 log_unknown_owner(dev
, token
->rule_line
, r
, "user", owner
);
2388 log_event_debug(dev
, token
, "OWNER %s(%u)", owner
, event
->uid
);
2392 char group
[UDEV_NAME_SIZE
];
2393 const char *gr
= group
;
2396 if (event
->group_final
)
2398 if (token
->op
== OP_ASSIGN_FINAL
)
2399 event
->group_final
= true;
2401 (void) udev_event_apply_format(event
, token
->value
, group
, sizeof(group
), false, &truncated
);
2403 log_event_truncated(dev
, token
, "group name", token
->value
, "GROUP", /* is_match = */ false);
2407 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2409 log_unknown_owner(dev
, token
->rule_line
, r
, "group", group
);
2411 log_event_debug(dev
, token
, "GROUP %s(%u)", group
, event
->gid
);
2415 char mode_str
[UDEV_NAME_SIZE
];
2418 if (event
->mode_final
)
2420 if (token
->op
== OP_ASSIGN_FINAL
)
2421 event
->mode_final
= true;
2423 (void) udev_event_apply_format(event
, token
->value
, mode_str
, sizeof(mode_str
), false, &truncated
);
2425 log_event_truncated(dev
, token
, "mode", token
->value
, "MODE", /* is_match = */ false);
2429 r
= parse_mode(mode_str
, &event
->mode
);
2431 log_event_error_errno(dev
, token
, r
, "Failed to parse mode '%s', ignoring: %m", mode_str
);
2433 log_event_debug(dev
, token
, "MODE %#o", event
->mode
);
2437 if (event
->owner_final
)
2439 if (token
->op
== OP_ASSIGN_FINAL
)
2440 event
->owner_final
= true;
2443 event
->uid
= PTR_TO_UID(token
->data
);
2444 log_event_debug(dev
, token
, "OWNER %u", event
->uid
);
2447 if (event
->group_final
)
2449 if (token
->op
== OP_ASSIGN_FINAL
)
2450 event
->group_final
= true;
2453 event
->gid
= PTR_TO_GID(token
->data
);
2454 log_event_debug(dev
, token
, "GROUP %u", event
->gid
);
2457 if (event
->mode_final
)
2459 if (token
->op
== OP_ASSIGN_FINAL
)
2460 event
->mode_final
= true;
2463 event
->mode
= PTR_TO_MODE(token
->data
);
2464 log_event_debug(dev
, token
, "MODE %#o", event
->mode
);
2466 case TK_A_SECLABEL
: {
2467 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2468 char label_str
[UDEV_LINE_SIZE
] = {};
2471 name
= strdup(token
->data
);
2475 (void) udev_event_apply_format(event
, token
->value
, label_str
, sizeof(label_str
), false, &truncated
);
2477 log_event_truncated(dev
, token
, "security label", token
->value
, "SECLABEL", /* is_match = */ false);
2481 if (!isempty(label_str
))
2482 label
= strdup(label_str
);
2484 label
= strdup(token
->value
);
2488 if (token
->op
== OP_ASSIGN
)
2489 ordered_hashmap_clear_free_free(event
->seclabel_list
);
2491 r
= ordered_hashmap_ensure_put(&event
->seclabel_list
, NULL
, name
, label
);
2495 return log_event_error_errno(dev
, token
, r
, "Failed to store SECLABEL{%s}='%s': %m", name
, label
);
2497 log_event_debug(dev
, token
, "SECLABEL{%s}='%s'", name
, label
);
2504 const char *val
, *name
= token
->data
;
2505 char value_new
[UDEV_NAME_SIZE
], *p
= value_new
;
2506 size_t count
, l
= sizeof(value_new
);
2509 if (isempty(token
->value
)) {
2510 if (token
->op
== OP_ADD
)
2512 r
= device_add_property(dev
, name
, NULL
);
2514 return log_event_error_errno(dev
, token
, r
, "Failed to remove property '%s': %m", name
);
2518 if (token
->op
== OP_ADD
&&
2519 device_get_property_value_with_fallback(dev
, name
, event
->worker
? event
->worker
->properties
: NULL
, &val
) >= 0) {
2520 l
= strpcpyl_full(&p
, l
, &truncated
, val
, " ", NULL
);
2522 log_event_warning(dev
, token
,
2523 "The buffer for the property '%s' is full, "
2524 "refusing to append the new value '%s'.", name
, token
->value
);
2529 (void) udev_event_apply_format(event
, token
->value
, p
, l
, false, &truncated
);
2531 _cleanup_free_
char *key_with_name
= strjoin("ENV{", name
, "}");
2532 log_event_truncated(dev
, token
, "property value", token
->value
,
2533 key_with_name
?: "ENV", /* is_match = */ false);
2537 if (event
->esc
== ESCAPE_REPLACE
) {
2538 count
= udev_replace_chars(p
, NULL
);
2540 log_event_debug(dev
, token
,
2541 "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"",
2542 count
, name
, token
->op
== OP_ADD
? "+" : "", token
->value
);
2545 r
= device_add_property(dev
, name
, value_new
);
2547 return log_event_error_errno(dev
, token
, r
, "Failed to add property '%s=%s': %m", name
, value_new
);
2551 char buf
[UDEV_PATH_SIZE
];
2554 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2556 log_event_truncated(dev
, token
, "tag name", token
->value
, "TAG", /* is_match = */ false);
2560 if (token
->op
== OP_ASSIGN
)
2561 device_cleanup_tags(dev
);
2563 if (token
->op
== OP_REMOVE
)
2564 device_remove_tag(dev
, buf
);
2566 r
= device_add_tag(dev
, buf
, true);
2570 log_event_warning_errno(dev
, token
, r
, "Failed to add tag '%s', ignoring: %m", buf
);
2575 char buf
[UDEV_PATH_SIZE
];
2579 if (event
->name_final
)
2581 if (token
->op
== OP_ASSIGN_FINAL
)
2582 event
->name_final
= true;
2584 if (sd_device_get_ifindex(dev
, NULL
) < 0) {
2585 log_event_error(dev
, token
,
2586 "Only network interfaces can be renamed, ignoring NAME=\"%s\".",
2591 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2593 log_event_truncated(dev
, token
, "network interface name", token
->value
, "NAME", /* is_match = */ false);
2597 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2598 if (naming_scheme_has(NAMING_REPLACE_STRICTLY
))
2599 count
= udev_replace_ifname(buf
);
2601 count
= udev_replace_chars(buf
, "/");
2603 log_event_debug(dev
, token
,
2604 "Replaced %zu character(s) from result of NAME=\"%s\"",
2605 count
, token
->value
);
2607 r
= free_and_strdup_warn(&event
->name
, buf
);
2611 log_event_debug(dev
, token
, "NAME '%s'", event
->name
);
2614 case TK_A_DEVLINK
: {
2615 char buf
[UDEV_PATH_SIZE
];
2619 if (event
->devlink_final
)
2621 if (sd_device_get_devnum(dev
, NULL
) < 0)
2623 if (token
->op
== OP_ASSIGN_FINAL
)
2624 event
->devlink_final
= true;
2625 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2626 device_cleanup_devlinks(dev
);
2628 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
),
2629 /* replace_whitespace = */ event
->esc
!= ESCAPE_NONE
, &truncated
);
2631 log_event_truncated(dev
, token
, "symbolic link path", token
->value
, "SYMLINK", /* is_match = */ false);
2635 /* By default or string_escape=none, allow multiple symlinks separated by spaces. */
2636 if (event
->esc
== ESCAPE_UNSET
)
2637 count
= udev_replace_chars(buf
, /* allow = */ "/ ");
2638 else if (event
->esc
== ESCAPE_REPLACE
)
2639 count
= udev_replace_chars(buf
, /* allow = */ "/");
2643 log_event_debug(dev
, token
,
2644 "Replaced %zu character(s) from result of SYMLINK=\"%s\"",
2645 count
, token
->value
);
2647 for (const char *p
= buf
;;) {
2648 _cleanup_free_
char *path
= NULL
;
2650 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_RETAIN_ESCAPE
);
2654 log_warning_errno(r
, "Failed to extract first path in SYMLINK=, ignoring: %m");
2660 if (token
->op
== OP_REMOVE
) {
2661 r
= device_remove_devlink(dev
, path
);
2665 log_event_warning_errno(dev
, token
, r
, "Failed to remove devlink '%s', ignoring: %m", path
);
2667 log_event_debug(dev
, token
, "Dropped SYMLINK '%s'", path
);
2669 r
= device_add_devlink(dev
, path
);
2673 log_event_warning_errno(dev
, token
, r
, "Failed to add devlink '%s', ignoring: %m", path
);
2675 log_event_debug(dev
, token
, "Added SYMLINK '%s'", path
);
2681 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2682 const char *val
, *key_name
= token
->data
;
2685 if (udev_resolve_subsys_kernel(key_name
, buf
, sizeof(buf
), false) < 0 &&
2686 sd_device_get_syspath(dev
, &val
) >= 0) {
2687 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", key_name
, NULL
);
2689 log_event_warning(dev
, token
,
2690 "The path to the attribute '%s/%s' is too long, refusing to set the attribute.",
2696 r
= attr_subst_subdir(buf
);
2698 log_event_error_errno(dev
, token
, r
, "Could not find file matches '%s', ignoring: %m", buf
);
2701 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2703 log_event_truncated(dev
, token
, "attribute value", token
->value
, "ATTR", /* is_match = */ false);
2707 log_event_debug(dev
, token
, "ATTR '%s' writing '%s'", buf
, value
);
2708 r
= write_string_file(buf
, value
,
2709 WRITE_STRING_FILE_VERIFY_ON_FAILURE
|
2710 WRITE_STRING_FILE_DISABLE_BUFFER
|
2711 WRITE_STRING_FILE_AVOID_NEWLINE
|
2712 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE
);
2714 log_event_error_errno(dev
, token
, r
, "Failed to write ATTR{%s}, ignoring: %m", buf
);
2718 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2721 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
2723 log_event_truncated(dev
, token
, "sysctl entry name", token
->data
, "SYSCTL", /* is_match = */ false);
2727 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2729 _cleanup_free_
char *key_with_name
= strjoin("SYSCTL{", buf
, "}");
2730 log_event_truncated(dev
, token
, "sysctl value", token
->value
,
2731 key_with_name
?: "SYSCTL", /* is_match = */ false);
2735 sysctl_normalize(buf
);
2736 log_event_debug(dev
, token
, "SYSCTL '%s' writing '%s'", buf
, value
);
2737 r
= sysctl_write(buf
, value
);
2739 log_event_error_errno(dev
, token
, r
, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf
, value
);
2742 case TK_A_RUN_BUILTIN
:
2743 case TK_A_RUN_PROGRAM
: {
2744 _cleanup_free_
char *cmd
= NULL
;
2745 char buf
[UDEV_LINE_SIZE
];
2748 if (event
->run_final
)
2750 if (token
->op
== OP_ASSIGN_FINAL
)
2751 event
->run_final
= true;
2753 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2754 ordered_hashmap_clear_free_key(event
->run_list
);
2756 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2758 log_event_truncated(dev
, token
, "command", token
->value
,
2759 token
->type
== TK_A_RUN_BUILTIN
? "RUN{builtin}" : "RUN{program}",
2760 /* is_match = */ false);
2768 r
= ordered_hashmap_ensure_put(&event
->run_list
, NULL
, cmd
, token
->data
);
2772 return log_event_error_errno(dev
, token
, r
, "Failed to store command '%s': %m", cmd
);
2776 log_event_debug(dev
, token
, "RUN '%s'", token
->value
);
2779 case TK_A_OPTIONS_STATIC_NODE
:
2780 /* do nothing for events. */
2783 assert_not_reached();
2789 static bool token_is_for_parents(UdevRuleToken
*token
) {
2790 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
2793 static int udev_rule_apply_parent_token_to_event(UdevRuleToken
*head_token
, UdevEvent
*event
) {
2799 event
->dev_parent
= ASSERT_PTR(event
->dev
);
2802 LIST_FOREACH(tokens
, token
, head_token
) {
2803 if (!token_is_for_parents(token
))
2804 return true; /* All parent tokens match. */
2806 r
= udev_rule_apply_token_to_event(token
, event
->dev_parent
, event
);
2813 /* All parent tokens match, and no more token (except for GOTO) in the line. */
2816 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
2817 event
->dev_parent
= NULL
;
2823 static int udev_rule_apply_line_to_event(
2826 UdevRuleLine
**next_line
) {
2828 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
2829 bool parents_done
= false;
2830 sd_device_action_t action
;
2837 r
= sd_device_get_action(event
->dev
, &action
);
2841 if (action
!= SD_DEVICE_REMOVE
) {
2842 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
2843 mask
|= LINE_HAS_DEVLINK
;
2845 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
2846 mask
|= LINE_HAS_NAME
;
2849 if ((line
->type
& mask
) == 0)
2852 event
->esc
= ESCAPE_UNSET
;
2854 DEVICE_TRACE_POINT(rules_apply_line
, event
->dev
, line
->rule_file
->filename
, line
->line_number
);
2856 LIST_FOREACH(tokens
, token
, line
->tokens
) {
2857 if (token_is_for_parents(token
)) {
2861 r
= udev_rule_apply_parent_token_to_event(token
, event
);
2865 parents_done
= true;
2869 r
= udev_rule_apply_token_to_event(token
, event
->dev
, event
);
2874 if (line
->goto_line
)
2875 *next_line
= line
->goto_line
; /* update next_line only when the line has GOTO token. */
2880 int udev_rules_apply_to_event(UdevRules
*rules
, UdevEvent
*event
) {
2886 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2887 LIST_FOREACH_WITH_NEXT(rule_lines
, line
, next_line
, file
->rule_lines
) {
2888 r
= udev_rule_apply_line_to_event(line
, event
, &next_line
);
2896 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
2897 _cleanup_strv_free_
char **tags
= NULL
;
2898 uid_t uid
= UID_INVALID
;
2899 gid_t gid
= GID_INVALID
;
2900 mode_t mode
= MODE_INVALID
;
2905 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
2908 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
2909 if (token
->type
== TK_A_OWNER_ID
)
2910 uid
= PTR_TO_UID(token
->data
);
2911 else if (token
->type
== TK_A_GROUP_ID
)
2912 gid
= PTR_TO_GID(token
->data
);
2913 else if (token
->type
== TK_A_MODE_ID
)
2914 mode
= PTR_TO_MODE(token
->data
);
2915 else if (token
->type
== TK_A_TAG
) {
2916 r
= strv_extend(&tags
, token
->value
);
2919 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
2920 r
= static_node_apply_permissions(token
->value
, mode
, uid
, gid
, tags
);
2928 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2933 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2934 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
2935 r
= udev_rule_line_apply_static_dev_perms(line
);
2943 static const char* const resolve_name_timing_table
[_RESOLVE_NAME_TIMING_MAX
] = {
2944 [RESOLVE_NAME_NEVER
] = "never",
2945 [RESOLVE_NAME_LATE
] = "late",
2946 [RESOLVE_NAME_EARLY
] = "early",
2949 DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing
, ResolveNameTiming
);