1 /* SPDX-License-Identifier: GPL-2.0-or-later */
9 #include "alloc-util.h"
10 #include "architecture.h"
12 #include "conf-files.h"
13 #include "conf-parser.h"
14 #include "confidential-virt.h"
15 #include "constants.h"
16 #include "device-private.h"
17 #include "device-util.h"
18 #include "dirent-util.h"
19 #include "errno-util.h"
21 #include "extract-word.h"
24 #include "format-util.h"
26 #include "glob-util.h"
29 #include "memstream-util.h"
30 #include "netif-naming-scheme.h"
31 #include "nulstr-util.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "proc-cmdline.h"
35 #include "socket-util.h"
36 #include "stat-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
41 #include "sysctl-util.h"
42 #include "syslog-util.h"
43 #include "udev-builtin.h"
44 #include "udev-dump.h"
45 #include "udev-event.h"
46 #include "udev-format.h"
47 #include "udev-node.h"
48 #include "udev-rules.h"
49 #include "udev-spawn.h"
50 #include "udev-trace.h"
51 #include "udev-util.h"
52 #include "udev-worker.h"
53 #include "user-record.h"
54 #include "user-util.h"
58 #define RULES_DIRS ((const char* const*) CONF_PATHS_STRV("udev/rules.d"))
66 OP_ASSIGN_FINAL
, /* := */
68 _OP_TYPE_INVALID
= -EINVAL
,
69 } UdevRuleOperatorType
;
72 MATCH_TYPE_EMPTY
, /* empty string */
73 MATCH_TYPE_PLAIN
, /* no special characters */
74 MATCH_TYPE_PLAIN_WITH_EMPTY
, /* no special characters with empty string, e.g., "|foo" */
75 MATCH_TYPE_GLOB
, /* shell globs ?,*,[] */
76 MATCH_TYPE_GLOB_WITH_EMPTY
, /* shell globs ?,*,[] with empty string, e.g., "|foo*" */
77 MATCH_TYPE_SUBSYSTEM
, /* "subsystem", "bus", or "class" */
80 _MATCH_TYPE_MASK
= (1 << 5) - 1,
81 MATCH_REMOVE_TRAILING_WHITESPACE
= 1 << 5, /* Remove trailing whitespaces in attribute */
82 MATCH_CASE_INSENSITIVE
= 1 << 6, /* string or pattern is matched case-insensitively */
83 _MATCH_TYPE_INVALID
= -EINVAL
,
86 assert_cc(_MATCH_TYPE_MAX
<= _MATCH_TYPE_MASK
);
89 SUBST_TYPE_PLAIN
, /* no substitution */
90 SUBST_TYPE_FORMAT
, /* % or $ */
91 SUBST_TYPE_SUBSYS
, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
93 _SUBST_TYPE_INVALID
= -EINVAL
,
94 } UdevRuleSubstituteType
;
97 /* lvalues which take match or nomatch operator */
98 TK_M_ACTION
, /* string, device_get_action() */
99 TK_M_DEVPATH
, /* path, sd_device_get_devpath() */
100 TK_M_KERNEL
, /* string, sd_device_get_sysname() */
101 TK_M_DEVLINK
, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */
102 TK_M_NAME
, /* string, name of network interface */
103 TK_M_ENV
, /* string, device property, takes key through attribute */
104 TK_M_CONST
, /* string, system-specific hard-coded constant */
105 TK_M_TAG
, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */
106 TK_M_SUBSYSTEM
, /* string, sd_device_get_subsystem() */
107 TK_M_DRIVER
, /* string, sd_device_get_driver() */
108 TK_M_ATTR
, /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */
109 TK_M_SYSCTL
, /* string, takes kernel parameter through attribute */
111 /* matches parent parameters */
112 TK_M_PARENTS_KERNEL
, /* string */
113 TK_M_PARENTS_SUBSYSTEM
, /* string */
114 TK_M_PARENTS_DRIVER
, /* string */
115 TK_M_PARENTS_ATTR
, /* string */
116 TK_M_PARENTS_TAG
, /* strv */
118 TK_M_TEST
, /* path, optionally mode_t can be specified by attribute, test the existence of a file */
119 TK_M_PROGRAM
, /* string, execute a program */
120 TK_M_IMPORT_FILE
, /* path */
121 TK_M_IMPORT_PROGRAM
, /* string, import properties from the result of program */
122 TK_M_IMPORT_BUILTIN
, /* string, import properties from the result of built-in command */
123 TK_M_IMPORT_DB
, /* string, import properties from database */
124 TK_M_IMPORT_CMDLINE
, /* string, kernel command line */
125 TK_M_IMPORT_PARENT
, /* string, parent property */
126 TK_M_RESULT
, /* string, result of TK_M_PROGRAM */
128 #define _TK_M_MAX (TK_M_RESULT + 1)
129 #define _TK_A_MIN _TK_M_MAX
131 /* lvalues which take one of assign operators */
132 TK_A_OPTIONS_DUMP
, /* no argument */
133 TK_A_OPTIONS_STRING_ESCAPE_NONE
, /* no argument */
134 TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, /* no argument */
135 TK_A_OPTIONS_DB_PERSIST
, /* no argument */
136 TK_A_OPTIONS_INOTIFY_WATCH
, /* boolean */
137 TK_A_OPTIONS_DEVLINK_PRIORITY
, /* int */
138 TK_A_OPTIONS_LOG_LEVEL
, /* string of log level or "reset" */
139 TK_A_OWNER
, /* user name */
140 TK_A_GROUP
, /* group name */
141 TK_A_MODE
, /* mode string */
142 TK_A_OWNER_ID
, /* uid_t */
143 TK_A_GROUP_ID
, /* gid_t */
144 TK_A_MODE_ID
, /* mode_t */
145 TK_A_TAG
, /* string */
146 TK_A_OPTIONS_STATIC_NODE
, /* device path, /dev/... */
147 TK_A_SECLABEL
, /* string with attribute */
148 TK_A_ENV
, /* string with attribute */
149 TK_A_NAME
, /* ifname */
150 TK_A_DEVLINK
, /* string */
151 TK_A_ATTR
, /* string with attribute */
152 TK_A_SYSCTL
, /* string with attribute */
153 TK_A_RUN_BUILTIN
, /* string */
154 TK_A_RUN_PROGRAM
, /* string */
157 _TK_TYPE_INVALID
= -EINVAL
,
161 LINE_HAS_NAME
= 1 << 0, /* has NAME= */
162 LINE_HAS_DEVLINK
= 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */
163 LINE_HAS_STATIC_NODE
= 1 << 2, /* has OPTIONS=static_node */
164 LINE_HAS_GOTO
= 1 << 3, /* has GOTO= */
165 LINE_HAS_LABEL
= 1 << 4, /* has LABEL= */
166 LINE_UPDATE_SOMETHING
= 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */
167 LINE_IS_REFERENCED
= 1 << 6, /* is referenced by GOTO */
170 typedef struct UdevRuleFile UdevRuleFile
;
171 typedef struct UdevRuleLine UdevRuleLine
;
172 typedef struct UdevRuleToken UdevRuleToken
;
174 struct UdevRuleToken
{
175 UdevRuleTokenType type
:8;
176 UdevRuleOperatorType op
:8;
177 UdevRuleMatchType match_type
:8;
178 UdevRuleSubstituteType attr_subst_type
:8;
182 const char *token_str
; /* original token string for logging */
184 UdevRuleLine
*rule_line
;
185 LIST_FIELDS(UdevRuleToken
, tokens
);
188 struct UdevRuleLine
{
190 char *line_for_logging
;
191 unsigned line_number
;
192 UdevRuleLineType type
;
195 const char *goto_label
;
196 UdevRuleLine
*goto_line
;
198 UdevRuleFile
*rule_file
;
199 LIST_HEAD(UdevRuleToken
, tokens
);
200 LIST_FIELDS(UdevRuleLine
, rule_lines
);
203 struct UdevRuleFile
{
205 unsigned issues
; /* used by "udevadm verify" */
208 LIST_HEAD(UdevRuleLine
, rule_lines
);
209 LIST_FIELDS(UdevRuleFile
, rule_files
);
213 ResolveNameTiming resolve_name_timing
;
214 Hashmap
*known_users
;
215 Hashmap
*known_groups
;
216 Hashmap
*stats_by_path
;
217 LIST_HEAD(UdevRuleFile
, rule_files
);
220 #define LINE_GET_RULES(line) \
221 ASSERT_PTR(ASSERT_PTR(ASSERT_PTR(line)->rule_file)->rules)
223 static bool token_is_for_parents(UdevRuleToken
*token
) {
224 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
227 /*** Logging helpers ***/
229 #define _log_udev_rule_file_full(device, device_u, file, file_u, line_nr, level, level_u, error, fmt, ...) \
231 int level_u = (level); \
232 sd_device *device_u = (device); \
233 UdevRuleFile *file_u = (file); \
235 if (!device_u && file_u) \
236 file_u->issues |= (1U << level_u); \
238 log_device_full_errno_zerook( \
239 device_u, level_u, error, "%s:%u " fmt, \
240 strna(file_u ? file_u->filename : NULL), \
241 line_nr, ##__VA_ARGS__); \
244 #define log_udev_rule_file_full(device, file, line_nr, level, error, fmt, ...) \
245 _log_udev_rule_file_full(device, UNIQ_T(d, UNIQ), file, UNIQ_T(f, UNIQ), line_nr, level, UNIQ_T(l, UNIQ), error, fmt, ##__VA_ARGS__)
247 #define _log_udev_rule_line_full(device, line, line_u, ...) \
249 UdevRuleLine *line_u = ASSERT_PTR(line); \
251 log_udev_rule_file_full( \
253 line_u->rule_file, line_u->line_number, \
257 #define log_udev_rule_line_full(device, line, ...) \
258 _log_udev_rule_line_full(device, line, UNIQ_T(l, UNIQ), __VA_ARGS__)
260 /* Mainly used when applying tokens to the event device. */
261 #define _log_event_full_errno_zerook(event, event_u, token, token_u, level, error, fmt, ...) \
263 UdevEvent *event_u = ASSERT_PTR(event); \
264 UdevRuleToken *token_u = ASSERT_PTR(token); \
266 log_udev_rule_line_full( \
267 token_is_for_parents(token_u) ? event_u->dev_parent : event_u->dev, \
268 token_u->rule_line, \
269 level, error, "%s: " fmt, \
270 token_u->token_str, ##__VA_ARGS__); \
273 #define log_event_full_errno_zerook(event, token, ...) \
274 _log_event_full_errno_zerook(event, UNIQ_T(e, UNIQ), token, UNIQ_T(t, UNIQ), __VA_ARGS__)
276 #define _log_event_full_errno(event, token, level, error, error_u, ...) \
278 int error_u = (error); \
279 ASSERT_NON_ZERO(error_u); \
280 log_event_full_errno_zerook( \
281 event, token, level, error_u, \
285 #define log_event_full_errno(event, token, level, error, ...) \
286 _log_event_full_errno(event, token, level, error, UNIQ_T(e, UNIQ), __VA_ARGS__)
288 #define log_event_full(event, token, level, ...) (void) log_event_full_errno_zerook(event, token, level, 0, __VA_ARGS__)
290 #define log_event_debug(event, token, ...) log_event_full(event, token, LOG_DEBUG, __VA_ARGS__)
291 #define log_event_info(event, token, ...) log_event_full(event, token, LOG_INFO, __VA_ARGS__)
292 #define log_event_notice(event, token, ...) log_event_full(event, token, LOG_NOTICE, __VA_ARGS__)
293 #define log_event_warning(event, token, ...) log_event_full(event, token, LOG_WARNING, __VA_ARGS__)
294 #define log_event_error(event, token, ...) log_event_full(event, token, LOG_ERR, __VA_ARGS__)
296 #define log_event_debug_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_DEBUG, error, __VA_ARGS__)
297 #define log_event_info_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_INFO, error, __VA_ARGS__)
298 #define log_event_notice_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_NOTICE, error, __VA_ARGS__)
299 #define log_event_warning_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_WARNING, error, __VA_ARGS__)
300 #define log_event_error_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_ERR, error, __VA_ARGS__)
302 #define _log_event_trace_errno(event, event_u, ...) \
304 UdevEvent *event_u = ASSERT_PTR(event); \
307 log_event_debug_errno(event_u, __VA_ARGS__) : \
311 #define log_event_trace_errno(event, ...) \
312 _log_event_trace_errno(event, UNIQ_T(e, UNIQ), __VA_ARGS__)
314 #define _log_event_trace(event, event_u, ...) \
316 UdevEvent *event_u = ASSERT_PTR(event); \
319 log_event_debug(event_u, __VA_ARGS__) : \
323 #define log_event_trace(event, ...) \
324 _log_event_trace(event, UNIQ_T(e, UNIQ), __VA_ARGS__)
326 #define _log_event_result(event, token, result, result_u) \
328 bool result_u = (result); \
330 log_event_trace(event, token, "%s", \
331 result_u ? "PASS" : "FAIL"); \
335 #define log_event_result(event, token, result) \
336 _log_event_result(event, token, result, UNIQ_T(r, UNIQ))
338 #define log_event_done(event, token) \
340 log_event_trace(event, token, "DONE"); \
344 #define _log_event_final_set(event, token, token_u) \
346 UdevRuleToken *token_u = ASSERT_PTR(token); \
348 log_event_trace(event, token_u, \
349 "Already assigned final value, ignoring further %s.", \
350 token_u->op == OP_REMOVE ? "modification" : "assignment"); \
354 #define log_event_final_set(event, token) \
355 _log_event_final_set(event, token, UNIQ_T(t, UNIQ))
357 #define _log_event_truncated(event, token, token_u, what, format) \
359 UdevRuleToken *token_u = ASSERT_PTR(token); \
361 token_u->type < _TK_M_MAX ? \
362 log_event_debug(event, token_u, \
363 "The %s is truncated while substituting into \"%s\", assuming the token fails.", \
364 what, (const char*) format) : \
367 "The %s is truncated while substituting into \"%s\", refusing to apply the token.", \
368 what, (const char*) format); \
371 #define log_event_truncated(event, token, what, format) \
372 _log_event_truncated(event, token, UNIQ_T(t, UNIQ), what, format)
374 #define _log_event_line(event, event_u, line, ...) \
376 UdevEvent *event_u = ASSERT_PTR(event); \
379 log_udev_rule_line_full( \
380 event_u->dev, line, \
381 LOG_DEBUG, 0, __VA_ARGS__) : \
385 #define log_event_line(event, line, ...) \
386 _log_event_line(event, UNIQ_T(e, UNIQ), line, __VA_ARGS__)
388 /* Mainly used when parsing .rules files. */
389 #define log_file_full_errno_zerook(...) \
390 log_udev_rule_file_full(NULL, __VA_ARGS__)
392 #define log_file_error(file, line_nr, ...) \
393 log_file_full_errno_zerook(file, line_nr, LOG_ERR, 0, __VA_ARGS__)
395 #define log_line_full_errno_zerook(...) \
396 log_udev_rule_line_full(NULL, __VA_ARGS__)
398 #define log_line_full_errno(line, level, error, ...) \
399 log_udev_rule_line_full(NULL, line, level, error, __VA_ARGS__)
401 #define log_line_full(line, level, ...) (void) log_line_full_errno_zerook(line, level, 0, __VA_ARGS__)
403 #define log_line_debug(line, ...) log_line_full(line, LOG_DEBUG, __VA_ARGS__)
404 #define log_line_info(line, ...) log_line_full(line, LOG_INFO, __VA_ARGS__)
405 #define log_line_notice(line, ...) log_line_full(line, LOG_NOTICE, __VA_ARGS__)
406 #define log_line_warning(line, ...) log_line_full(line, LOG_WARNING, __VA_ARGS__)
407 #define log_line_error(line, ...) log_line_full(line, LOG_ERR, __VA_ARGS__)
409 #define log_line_debug_errno(line, error, ...) log_line_full_errno(line, LOG_DEBUG, error, __VA_ARGS__)
410 #define log_line_info_errno(line, error, ...) log_line_full_errno(line, LOG_INFO, error, __VA_ARGS__)
411 #define log_line_notice_errno(line, error, ...) log_line_full_errno(line, LOG_NOTICE, error, __VA_ARGS__)
412 #define log_line_warning_errno(line, error, ...) log_line_full_errno(line, LOG_WARNING, error, __VA_ARGS__)
413 #define log_line_error_errno(line, error, ...) log_line_full_errno(line, LOG_ERR, error, __VA_ARGS__)
415 #define _log_line_invalid_token(line, key, type) \
416 log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \
417 "Invalid %s for %s.", type, key)
419 #define log_line_invalid_op(line, key) _log_line_invalid_token(line, key, "operator")
420 #define log_line_invalid_attr(line, key) _log_line_invalid_token(line, key, "attribute")
421 #define log_line_invalid_prefix(line, key) _log_line_invalid_token(line, key, "prefix 'i'")
423 #define log_line_invalid_attr_format(line, key, attr, offset, hint) \
424 log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \
425 "Invalid attribute \"%s\" for %s (char %zu: %s), ignoring.", \
426 attr, key, offset, hint)
427 #define log_line_invalid_value(line, key, value, offset, hint) \
428 log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \
429 "Invalid value \"%s\" for %s (char %zu: %s), ignoring.", \
430 value, key, offset, hint)
432 /*** Other functions ***/
434 static UdevRuleToken
* udev_rule_token_free(UdevRuleToken
*token
) {
438 if (token
->rule_line
)
439 LIST_REMOVE(tokens
, token
->rule_line
->tokens
, token
);
444 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleToken
*, udev_rule_token_free
);
446 static void udev_rule_line_clear_tokens(UdevRuleLine
*rule_line
) {
449 LIST_FOREACH(tokens
, i
, rule_line
->tokens
)
450 udev_rule_token_free(i
);
453 static UdevRuleLine
* udev_rule_line_free(UdevRuleLine
*rule_line
) {
457 udev_rule_line_clear_tokens(rule_line
);
459 if (rule_line
->rule_file
)
460 LIST_REMOVE(rule_lines
, rule_line
->rule_file
->rule_lines
, rule_line
);
462 free(rule_line
->line
);
463 free(rule_line
->line_for_logging
);
464 return mfree(rule_line
);
467 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine
*, udev_rule_line_free
);
469 static UdevRuleFile
* udev_rule_file_free(UdevRuleFile
*rule_file
) {
473 LIST_FOREACH(rule_lines
, i
, rule_file
->rule_lines
)
474 udev_rule_line_free(i
);
476 if (rule_file
->rules
)
477 LIST_REMOVE(rule_files
, rule_file
->rules
->rule_files
, rule_file
);
479 free(rule_file
->filename
);
480 return mfree(rule_file
);
483 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleFile
*, udev_rule_file_free
);
485 UdevRules
* udev_rules_free(UdevRules
*rules
) {
489 LIST_FOREACH(rule_files
, i
, rules
->rule_files
)
490 udev_rule_file_free(i
);
492 hashmap_free(rules
->known_users
);
493 hashmap_free(rules
->known_groups
);
494 hashmap_free(rules
->stats_by_path
);
498 static int rule_resolve_user(UdevRuleLine
*rule_line
, const char *name
, uid_t
*ret
) {
499 Hashmap
**known_users
= &LINE_GET_RULES(rule_line
)->known_users
;
505 void *val
= hashmap_get(*known_users
, name
);
507 *ret
= PTR_TO_UID(val
);
511 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
512 r
= userdb_by_name(name
, &USERDB_MATCH_ROOT_AND_SYSTEM
,
513 USERDB_SUPPRESS_SHADOW
| USERDB_PARSE_NUMERIC
| USERDB_SYNTHESIZE_NUMERIC
,
516 return log_line_error_errno(rule_line
, r
, "Unknown user '%s', ignoring.", name
);
518 return log_line_error_errno(rule_line
, r
, "User '%s' is not a system user, ignoring.", name
);
520 return log_line_error_errno(rule_line
, r
, "Failed to resolve user '%s', ignoring: %m", name
);
522 _cleanup_free_
char *n
= strdup(name
);
526 r
= hashmap_ensure_put(known_users
, &string_hash_ops_free
, n
, UID_TO_PTR(ur
->uid
));
535 static int rule_resolve_group(UdevRuleLine
*rule_line
, const char *name
, gid_t
*ret
) {
536 Hashmap
**known_groups
= &LINE_GET_RULES(rule_line
)->known_groups
;
542 void *val
= hashmap_get(*known_groups
, name
);
544 *ret
= PTR_TO_GID(val
);
548 _cleanup_(group_record_unrefp
) GroupRecord
*gr
= NULL
;
549 r
= groupdb_by_name(name
, &USERDB_MATCH_ROOT_AND_SYSTEM
,
550 USERDB_SUPPRESS_SHADOW
| USERDB_PARSE_NUMERIC
| USERDB_SYNTHESIZE_NUMERIC
,
553 return log_line_error_errno(rule_line
, r
, "Unknown group '%s', ignoring.", name
);
555 return log_line_error_errno(rule_line
, r
, "Group '%s' is not a system group, ignoring.", name
);
557 return log_line_error_errno(rule_line
, r
, "Failed to resolve group '%s', ignoring: %m", name
);
559 _cleanup_free_
char *n
= strdup(name
);
563 r
= hashmap_ensure_put(known_groups
, &string_hash_ops_free
, n
, GID_TO_PTR(gr
->gid
));
572 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
576 return SUBST_TYPE_SUBSYS
;
577 if (strchr(str
, '%') || strchr(str
, '$'))
578 return SUBST_TYPE_FORMAT
;
579 return SUBST_TYPE_PLAIN
;
582 static bool type_has_nulstr_value(UdevRuleTokenType type
) {
583 return type
< TK_M_TEST
|| type
== TK_M_RESULT
;
586 static int rule_line_add_token(
587 UdevRuleLine
*rule_line
,
588 UdevRuleTokenType type
,
589 UdevRuleOperatorType op
,
592 bool is_case_insensitive
,
593 const char *token_str
) {
595 _cleanup_(udev_rule_token_freep
) UdevRuleToken
*token
= NULL
;
596 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
597 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
600 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
601 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
603 if (type
< _TK_M_MAX
) {
605 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
607 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
608 match_type
= MATCH_TYPE_SUBSYSTEM
;
609 else if (isempty(value
))
610 match_type
= MATCH_TYPE_EMPTY
;
611 else if (streq(value
, "?*")) {
612 /* Convert KEY=="?*" -> KEY!="" */
613 match_type
= MATCH_TYPE_EMPTY
;
614 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
615 } else if (string_is_glob(value
))
616 match_type
= MATCH_TYPE_GLOB
;
618 match_type
= MATCH_TYPE_PLAIN
;
620 if (type_has_nulstr_value(type
)) {
621 /* Convert value string to nulstr. */
622 bool bar
= true, empty
= false;
625 for (a
= b
= value
; *a
!= '\0'; a
++) {
639 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
647 if (match_type
== MATCH_TYPE_GLOB
)
648 match_type
= MATCH_TYPE_GLOB_WITH_EMPTY
;
649 if (match_type
== MATCH_TYPE_PLAIN
)
650 match_type
= MATCH_TYPE_PLAIN_WITH_EMPTY
;
655 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
660 assert(match_type
>= 0 && match_type
< _MATCH_TYPE_MAX
);
663 if (len
> 0 && !isspace(value
[len
- 1]))
664 match_type
|= MATCH_REMOVE_TRAILING_WHITESPACE
;
666 subst_type
= rule_get_substitution_type(data
);
669 SET_FLAG(match_type
, MATCH_CASE_INSENSITIVE
, is_case_insensitive
);
671 token
= new(UdevRuleToken
, 1);
675 *token
= (UdevRuleToken
) {
680 .match_type
= match_type
,
681 .attr_subst_type
= subst_type
,
682 .token_str
= token_str
,
683 .rule_line
= rule_line
,
686 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
688 if (token
->type
== TK_A_NAME
)
689 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
691 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
692 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
693 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
694 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
696 else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
)
697 SET_FLAG(rule_line
->type
, LINE_HAS_STATIC_NODE
, true);
699 else if (token
->type
>= _TK_A_MIN
||
700 IN_SET(token
->type
, TK_M_PROGRAM
,
701 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
702 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
703 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
709 static void check_value_format_and_warn(UdevRuleLine
*line
, const char *key
, const char *value
, bool nonempty
) {
713 if (nonempty
&& isempty(value
))
714 log_line_invalid_value(line
, key
, value
, (size_t) 0, "empty value");
715 else if (udev_check_format(value
, &offset
, &hint
) < 0)
716 log_line_invalid_value(line
, key
, value
, offset
+ 1, hint
);
719 static int check_attr_format_and_warn(UdevRuleLine
*line
, const char *key
, const char *value
) {
724 return log_line_invalid_attr(line
, key
);
725 if (udev_check_format(value
, &offset
, &hint
) < 0)
726 log_line_invalid_attr_format(line
, key
, value
, offset
+ 1, hint
);
730 static int parse_token(
731 UdevRuleLine
*rule_line
,
734 UdevRuleOperatorType op
,
736 bool is_case_insensitive
,
737 const char *token_str
) {
739 ResolveNameTiming resolve_name_timing
= LINE_GET_RULES(rule_line
)->resolve_name_timing
;
740 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
746 if (!is_match
&& is_case_insensitive
)
747 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
748 "Invalid prefix 'i' for '%s'. The 'i' prefix can be specified only for '==' or '!=' operator.", key
);
750 if (streq(key
, "ACTION")) {
752 return log_line_invalid_attr(rule_line
, key
);
754 return log_line_invalid_op(rule_line
, key
);
756 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
, is_case_insensitive
, token_str
);
757 } else if (streq(key
, "DEVPATH")) {
759 return log_line_invalid_attr(rule_line
, key
);
761 return log_line_invalid_op(rule_line
, key
);
763 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
, is_case_insensitive
, token_str
);
764 } else if (streq(key
, "KERNEL")) {
766 return log_line_invalid_attr(rule_line
, key
);
768 return log_line_invalid_op(rule_line
, key
);
770 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
, is_case_insensitive
, token_str
);
771 } else if (streq(key
, "SYMLINK")) {
773 return log_line_invalid_attr(rule_line
, key
);
775 check_value_format_and_warn(rule_line
, key
, value
, false);
776 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
778 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
, is_case_insensitive
, token_str
);
779 } else if (streq(key
, "NAME")) {
781 return log_line_invalid_attr(rule_line
, key
);
783 return log_line_invalid_op(rule_line
, key
);
785 log_line_warning(rule_line
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key
);
790 if (streq(value
, "%k"))
791 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
792 "Ignoring NAME=\"%%k\", as it will take no effect.");
794 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
795 "Ignoring NAME=\"\", as udev will not delete any network interfaces.");
796 check_value_format_and_warn(rule_line
, key
, value
, false);
798 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
800 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
, is_case_insensitive
, token_str
);
801 } else if (streq(key
, "ENV")) {
803 return log_line_invalid_attr(rule_line
, key
);
805 return log_line_invalid_op(rule_line
, key
);
806 if (op
== OP_ASSIGN_FINAL
) {
807 log_line_warning(rule_line
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
812 if (!device_property_can_set(attr
))
813 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
814 "Invalid ENV attribute. '%s' cannot be set.", attr
);
816 check_value_format_and_warn(rule_line
, key
, value
, false);
818 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
, /* is_case_insensitive = */ false, token_str
);
820 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
, is_case_insensitive
, token_str
);
821 } else if (streq(key
, "CONST")) {
822 if (isempty(attr
) || !STR_IN_SET(attr
, "arch", "virt"))
823 return log_line_invalid_attr(rule_line
, key
);
825 return log_line_invalid_op(rule_line
, key
);
826 r
= rule_line_add_token(rule_line
, TK_M_CONST
, op
, value
, attr
, is_case_insensitive
, token_str
);
827 } else if (streq(key
, "TAG")) {
829 return log_line_invalid_attr(rule_line
, key
);
830 if (op
== OP_ASSIGN_FINAL
) {
831 log_line_warning(rule_line
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
836 check_value_format_and_warn(rule_line
, key
, value
, true);
838 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
840 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
, is_case_insensitive
, token_str
);
841 } else if (streq(key
, "SUBSYSTEM")) {
843 return log_line_invalid_attr(rule_line
, key
);
845 return log_line_invalid_op(rule_line
, key
);
847 if (STR_IN_SET(value
, "bus", "class"))
848 log_line_warning(rule_line
, "\"%s\" must be specified as \"subsystem\".", value
);
850 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
, is_case_insensitive
, token_str
);
851 } else if (streq(key
, "DRIVER")) {
853 return log_line_invalid_attr(rule_line
, key
);
855 return log_line_invalid_op(rule_line
, key
);
857 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
, is_case_insensitive
, token_str
);
858 } else if (streq(key
, "ATTR")) {
859 r
= check_attr_format_and_warn(rule_line
, key
, attr
);
863 return log_line_invalid_op(rule_line
, key
);
864 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
865 log_line_warning(rule_line
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
870 check_value_format_and_warn(rule_line
, key
, value
, false);
871 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
, /* is_case_insensitive = */ false, token_str
);
873 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
, is_case_insensitive
, token_str
);
874 } else if (streq(key
, "SYSCTL")) {
875 r
= check_attr_format_and_warn(rule_line
, key
, attr
);
879 return log_line_invalid_op(rule_line
, key
);
880 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
881 log_line_warning(rule_line
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
886 check_value_format_and_warn(rule_line
, key
, value
, false);
887 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
, /* is_case_insensitive = */ false, token_str
);
889 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
, is_case_insensitive
, token_str
);
890 } else if (streq(key
, "KERNELS")) {
892 return log_line_invalid_attr(rule_line
, key
);
894 return log_line_invalid_op(rule_line
, key
);
896 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
, is_case_insensitive
, token_str
);
897 } else if (streq(key
, "SUBSYSTEMS")) {
899 return log_line_invalid_attr(rule_line
, key
);
901 return log_line_invalid_op(rule_line
, key
);
903 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
, is_case_insensitive
, token_str
);
904 } else if (streq(key
, "DRIVERS")) {
906 return log_line_invalid_attr(rule_line
, key
);
908 return log_line_invalid_op(rule_line
, key
);
910 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
, is_case_insensitive
, token_str
);
911 } else if (streq(key
, "ATTRS")) {
912 r
= check_attr_format_and_warn(rule_line
, key
, attr
);
916 return log_line_invalid_op(rule_line
, key
);
918 if (startswith(attr
, "device/"))
919 log_line_warning(rule_line
, "'device' link may not be available in future kernels.");
920 if (strstr(attr
, "../"))
921 log_line_warning(rule_line
, "Direct reference to parent sysfs directory, may break in future kernels.");
923 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
, is_case_insensitive
, token_str
);
924 } else if (streq(key
, "TAGS")) {
926 return log_line_invalid_attr(rule_line
, key
);
928 return log_line_invalid_op(rule_line
, key
);
930 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
, is_case_insensitive
, token_str
);
931 } else if (streq(key
, "TEST")) {
932 mode_t mode
= MODE_INVALID
;
934 if (!isempty(attr
)) {
935 r
= parse_mode(attr
, &mode
);
937 return log_line_error_errno(rule_line
, r
, "Failed to parse mode '%s': %m", attr
);
939 check_value_format_and_warn(rule_line
, key
, value
, true);
941 return log_line_invalid_op(rule_line
, key
);
942 if (is_case_insensitive
)
943 return log_line_invalid_prefix(rule_line
, key
);
945 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
), is_case_insensitive
, token_str
);
946 } else if (streq(key
, "PROGRAM")) {
948 return log_line_invalid_attr(rule_line
, key
);
949 check_value_format_and_warn(rule_line
, key
, value
, true);
951 return log_line_invalid_op(rule_line
, key
);
954 if (is_case_insensitive
)
955 return log_line_invalid_prefix(rule_line
, key
);
957 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
, /* is_case_insensitive */ false, token_str
);
958 } else if (streq(key
, "IMPORT")) {
960 return log_line_invalid_attr(rule_line
, key
);
961 check_value_format_and_warn(rule_line
, key
, value
, true);
963 return log_line_invalid_op(rule_line
, key
);
966 if (is_case_insensitive
)
967 return log_line_invalid_prefix(rule_line
, key
);
969 if (streq(attr
, "file"))
970 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
971 else if (streq(attr
, "program")) {
972 UdevBuiltinCommand cmd
;
974 cmd
= udev_builtin_lookup(value
);
976 log_line_debug(rule_line
, "Found builtin command '%s' for %s, replacing attribute.", value
, key
);
977 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
), /* is_case_insensitive = */ false, token_str
);
979 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
980 } else if (streq(attr
, "builtin")) {
981 UdevBuiltinCommand cmd
;
983 cmd
= udev_builtin_lookup(value
);
985 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
986 "Unknown builtin command: %s", value
);
987 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
), /* is_case_insensitive = */ false, token_str
);
988 } else if (streq(attr
, "db"))
989 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
990 else if (streq(attr
, "cmdline"))
991 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
992 else if (streq(attr
, "parent"))
993 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
995 return log_line_invalid_attr(rule_line
, key
);
996 } else if (streq(key
, "RESULT")) {
998 return log_line_invalid_attr(rule_line
, key
);
1000 return log_line_invalid_op(rule_line
, key
);
1002 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
, is_case_insensitive
, token_str
);
1003 } else if (streq(key
, "OPTIONS")) {
1007 return log_line_invalid_attr(rule_line
, key
);
1008 if (is_match
|| op
== OP_REMOVE
)
1009 return log_line_invalid_op(rule_line
, key
);
1013 if (streq(value
, "dump"))
1014 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DUMP
, op
, NULL
, NULL
, /* is_case_insensitive = */ false, token_str
);
1015 else if (streq(value
, "string_escape=none"))
1016 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
, /* is_case_insensitive = */ false, token_str
);
1017 else if (streq(value
, "string_escape=replace"))
1018 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
, /* is_case_insensitive = */ false, token_str
);
1019 else if (streq(value
, "db_persist"))
1020 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
, /* is_case_insensitive = */ false, token_str
);
1021 else if (streq(value
, "watch"))
1022 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1), /* is_case_insensitive = */ false, token_str
);
1023 else if (streq(value
, "nowatch"))
1024 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0), /* is_case_insensitive = */ false, token_str
);
1025 else if ((tmp
= startswith(value
, "static_node=")))
1026 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
, /* is_case_insensitive = */ false, token_str
);
1027 else if ((tmp
= startswith(value
, "link_priority="))) {
1030 r
= safe_atoi(tmp
, &prio
);
1032 return log_line_error_errno(rule_line
, r
, "Failed to parse link priority '%s': %m", tmp
);
1033 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
), /* is_case_insensitive = */ false, token_str
);
1034 } else if ((tmp
= startswith(value
, "log_level="))) {
1037 if (streq(tmp
, "reset"))
1040 level
= log_level_from_string(tmp
);
1042 return log_line_error_errno(rule_line
, level
, "Failed to parse log level '%s': %m", tmp
);
1044 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_LOG_LEVEL
, op
, NULL
, INT_TO_PTR(level
), /* is_case_insensitive = */ false, token_str
);
1046 log_line_warning(rule_line
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
1049 } else if (streq(key
, "OWNER")) {
1051 return log_line_invalid_attr(rule_line
, key
);
1052 if (is_match
|| op
== OP_REMOVE
)
1053 return log_line_invalid_op(rule_line
, key
);
1055 log_line_warning(rule_line
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
1059 if (in_charset(value
, DIGITS
) ||
1060 (resolve_name_timing
== RESOLVE_NAME_EARLY
&&
1061 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
)) {
1062 uid_t uid
= UID_INVALID
; /* avoid false maybe-uninitialized warning */
1064 r
= rule_resolve_user(rule_line
, value
, &uid
);
1067 assert(uid_is_valid(uid
));
1069 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
), /* is_case_insensitive = */ false, token_str
);
1070 } else if (resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
1071 check_value_format_and_warn(rule_line
, key
, value
, true);
1072 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
1074 log_line_debug(rule_line
, "User name resolution is disabled, ignoring %s=\"%s\".", key
, value
);
1077 } else if (streq(key
, "GROUP")) {
1079 return log_line_invalid_attr(rule_line
, key
);
1080 if (is_match
|| op
== OP_REMOVE
)
1081 return log_line_invalid_op(rule_line
, key
);
1083 log_line_warning(rule_line
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
1087 if (in_charset(value
, DIGITS
) ||
1088 (resolve_name_timing
== RESOLVE_NAME_EARLY
&&
1089 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
)) {
1090 gid_t gid
= GID_INVALID
; /* avoid false maybe-uninitialized warning */
1092 r
= rule_resolve_group(rule_line
, value
, &gid
);
1095 assert(gid_is_valid(gid
));
1097 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
), /* is_case_insensitive = */ false, token_str
);
1098 } else if (resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
1099 check_value_format_and_warn(rule_line
, key
, value
, true);
1100 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
1102 log_line_debug(rule_line
, "Resolving group name is disabled, ignoring GROUP=\"%s\".", value
);
1105 } else if (streq(key
, "MODE")) {
1109 return log_line_invalid_attr(rule_line
, key
);
1110 if (is_match
|| op
== OP_REMOVE
)
1111 return log_line_invalid_op(rule_line
, key
);
1113 log_line_warning(rule_line
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
1117 if (parse_mode(value
, &mode
) >= 0)
1118 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
), /* is_case_insensitive = */ false, token_str
);
1120 check_value_format_and_warn(rule_line
, key
, value
, true);
1121 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
1123 } else if (streq(key
, "SECLABEL")) {
1125 return log_line_invalid_attr(rule_line
, key
);
1126 check_value_format_and_warn(rule_line
, key
, value
, true);
1127 if (is_match
|| op
== OP_REMOVE
)
1128 return log_line_invalid_op(rule_line
, key
);
1129 if (op
== OP_ASSIGN_FINAL
) {
1130 log_line_warning(rule_line
, "%s key takes '=' or '+=' operator, assuming '='.", key
);
1134 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, attr
, /* is_case_insensitive = */ false, token_str
);
1135 } else if (streq(key
, "RUN")) {
1136 if (is_match
|| op
== OP_REMOVE
)
1137 return log_line_invalid_op(rule_line
, key
);
1138 check_value_format_and_warn(rule_line
, key
, value
, true);
1139 if (!attr
|| streq(attr
, "program"))
1140 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
, /* is_case_insensitive = */ false, token_str
);
1141 else if (streq(attr
, "builtin")) {
1142 UdevBuiltinCommand cmd
;
1144 cmd
= udev_builtin_lookup(value
);
1146 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
),
1147 "Unknown builtin command '%s', ignoring.", value
);
1148 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
), /* is_case_insensitive = */ false, token_str
);
1150 return log_line_invalid_attr(rule_line
, key
);
1151 } else if (streq(key
, "GOTO")) {
1153 return log_line_invalid_attr(rule_line
, key
);
1154 if (op
!= OP_ASSIGN
)
1155 return log_line_invalid_op(rule_line
, key
);
1156 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
1157 log_line_warning(rule_line
, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value
);
1161 rule_line
->goto_label
= value
;
1162 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
1164 } else if (streq(key
, "LABEL")) {
1166 return log_line_invalid_attr(rule_line
, key
);
1167 if (op
!= OP_ASSIGN
)
1168 return log_line_invalid_op(rule_line
, key
);
1169 if (FLAGS_SET(rule_line
->type
, LINE_HAS_LABEL
))
1170 log_line_warning(rule_line
, "Contains multiple LABEL keys, ignoring LABEL=\"%s\".",
1173 rule_line
->label
= value
;
1174 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
1177 return log_line_error_errno(rule_line
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'.", key
);
1184 static UdevRuleOperatorType
parse_operator(const char *op
) {
1187 if (startswith(op
, "=="))
1189 if (startswith(op
, "!="))
1191 if (startswith(op
, "+="))
1193 if (startswith(op
, "-="))
1195 if (startswith(op
, "="))
1197 if (startswith(op
, ":="))
1198 return OP_ASSIGN_FINAL
;
1200 return _OP_TYPE_INVALID
;
1203 static void check_token_delimiters(UdevRuleLine
*rule_line
, const char *line
) {
1207 bool ws_before_comma
= false, ws_after_comma
= false;
1210 for (p
= line
; !isempty(p
); ++p
) {
1213 else if (strchr(WHITESPACE
, *p
)) {
1215 ws_after_comma
= true;
1217 ws_before_comma
= true;
1222 if (line
== rule_line
->line
) {
1223 /* this is the first token of the rule */
1225 log_line_notice(rule_line
, "style: stray leading comma.");
1226 } else if (isempty(p
)) {
1227 /* there are no more tokens in the rule */
1229 log_line_notice(rule_line
, "style: stray trailing comma.");
1231 /* single comma is expected */
1233 log_line_notice(rule_line
, "style: a comma between tokens is expected.");
1234 else if (n_comma
> 1)
1235 log_line_notice(rule_line
, "style: more than one comma between tokens.");
1237 /* whitespace after comma is expected */
1239 if (ws_before_comma
)
1240 log_line_notice(rule_line
, "style: stray whitespace before comma.");
1241 if (!ws_after_comma
)
1242 log_line_notice(rule_line
, "style: whitespace after comma is expected.");
1243 } else if (!ws_before_comma
&& !ws_after_comma
)
1244 log_line_notice(rule_line
, "style: whitespace between tokens is expected.");
1248 int udev_rule_parse_value(char *str
, char **ret_value
, char **ret_endpos
, bool *ret_is_case_insensitive
) {
1250 bool is_escaped
= false, is_case_insensitive
= false;
1255 assert(ret_is_case_insensitive
);
1257 /* check if string is prefixed with:
1259 * - "i" for case insensitive match
1261 * Note both e and i can be set but do not allow duplicates ("eei", "eii"). */
1262 for (const char *k
= str
; *k
!= '"' && k
< str
+ 2; k
++)
1263 if (*k
== 'e' && !is_escaped
)
1265 else if (*k
== 'i' && !is_case_insensitive
)
1266 is_case_insensitive
= true;
1270 /* value must be double quotated */
1271 str
+= is_escaped
+ is_case_insensitive
;
1276 /* unescape double quotation '\"'->'"' */
1277 for (j
= str
, i
= str
+ 1; *i
!= '"'; i
++, j
++) {
1280 if (i
[0] == '\\' && i
[1] == '"')
1286 * The return value must be terminated by two subsequent NULs
1287 * so it could be safely interpreted as nulstr.
1291 _cleanup_free_
char *unescaped
= NULL
;
1294 /* find the end position of value */
1295 for (i
= str
+ 1; *i
!= '"'; i
++) {
1303 l
= cunescape_length(str
+ 1, i
- (str
+ 1), 0, &unescaped
);
1307 assert(l
<= i
- (str
+ 1));
1308 memcpy(str
, unescaped
, l
+ 1);
1310 * The return value must be terminated by two subsequent NULs
1311 * so it could be safely interpreted as nulstr.
1317 *ret_endpos
= i
+ 1;
1318 *ret_is_case_insensitive
= is_case_insensitive
;
1322 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
, bool *ret_is_case_insensitive
) {
1323 char *key_begin
, *key_end
, *attr
, *tmp
;
1324 UdevRuleOperatorType op
;
1332 assert(ret_is_case_insensitive
);
1334 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
1336 if (isempty(key_begin
))
1339 for (key_end
= key_begin
; ; key_end
++) {
1340 if (key_end
[0] == '\0')
1342 if (strchr(WHITESPACE
"={", key_end
[0]))
1344 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
1347 if (key_end
[0] == '{') {
1349 tmp
= strchr(attr
, '}');
1358 tmp
= skip_leading_chars(tmp
, NULL
);
1359 op
= parse_operator(tmp
);
1365 tmp
+= op
== OP_ASSIGN
? 1 : 2;
1366 tmp
= skip_leading_chars(tmp
, NULL
);
1367 r
= udev_rule_parse_value(tmp
, ret_value
, line
, ret_is_case_insensitive
);
1371 *ret_key
= key_begin
;
1377 static void check_tokens_order(UdevRuleLine
*rule_line
) {
1378 bool has_result
= false;
1382 LIST_FOREACH(tokens
, t
, rule_line
->tokens
)
1383 if (t
->type
== TK_M_RESULT
)
1385 else if (has_result
&& t
->type
== TK_M_PROGRAM
) {
1386 log_line_warning(rule_line
, "Reordering RESULT check after PROGRAM assignment.");
1391 static void sort_tokens(UdevRuleLine
*rule_line
) {
1394 UdevRuleToken
*old_tokens
= TAKE_PTR(rule_line
->tokens
);
1396 while (old_tokens
) {
1397 UdevRuleToken
*min_token
= NULL
;
1399 LIST_FOREACH(tokens
, t
, old_tokens
)
1400 if (!min_token
|| min_token
->type
> t
->type
)
1403 LIST_REMOVE(tokens
, old_tokens
, min_token
);
1404 LIST_APPEND(tokens
, rule_line
->tokens
, min_token
);
1408 static int rule_add_line(UdevRuleFile
*rule_file
, const char *line
, unsigned line_nr
, bool extra_checks
) {
1409 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
1419 rule_line
= new(UdevRuleLine
, 1);
1423 *rule_line
= (UdevRuleLine
) {
1424 .line
= strdup(line
),
1425 .line_for_logging
= strdup(line
),
1426 .line_number
= line_nr
,
1428 if (!rule_line
->line
|| !rule_line
->line_for_logging
)
1431 rule_line
->rule_file
= rule_file
;
1432 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
1434 for (p
= rule_line
->line
; !isempty(p
); ) {
1435 char *key
, *attr
, *value
;
1436 UdevRuleOperatorType op
;
1437 bool is_case_insensitive
;
1440 check_token_delimiters(rule_line
, p
);
1442 r
= parse_line(&p
, &key
, &attr
, &op
, &value
, &is_case_insensitive
);
1444 return log_line_error_errno(rule_line
, r
, "Invalid key/value pair, ignoring.");
1448 char *token_str
= rule_line
->line_for_logging
+ (key
- rule_line
->line
);
1449 token_str
[p
- key
] = '\0';
1450 r
= parse_token(rule_line
, key
, attr
, op
, value
, is_case_insensitive
, token_str
);
1455 if (rule_line
->type
== 0) {
1456 log_line_warning(rule_line
, "The line has no effect, ignoring.");
1461 check_tokens_order(rule_line
);
1463 sort_tokens(rule_line
);
1464 TAKE_PTR(rule_line
);
1468 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
1471 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1472 LIST_FOREACH(rule_lines
, line
, rule_file
->rule_lines
) {
1473 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
1476 LIST_FOREACH(rule_lines
, i
, line
->rule_lines_next
)
1477 if (streq_ptr(i
->label
, line
->goto_label
)) {
1478 line
->goto_line
= i
;
1479 SET_FLAG(i
->type
, LINE_IS_REFERENCED
, true);
1483 if (!line
->goto_line
) {
1484 log_line_error(line
, "GOTO=\"%s\" has no matching label, ignoring.",
1487 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
1488 line
->goto_label
= NULL
;
1490 if ((line
->type
& ~(LINE_HAS_LABEL
|LINE_IS_REFERENCED
)) == 0) {
1491 log_line_warning(line
, "The line has no effect any more, dropping.");
1492 /* LINE_IS_REFERENCED implies LINE_HAS_LABEL */
1493 if (line
->type
& LINE_HAS_LABEL
)
1494 udev_rule_line_clear_tokens(line
);
1496 udev_rule_line_free(line
);
1502 static bool token_data_is_string(UdevRuleTokenType type
) {
1503 return IN_SET(type
, TK_M_ENV
,
1514 static bool token_type_and_data_eq(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1518 return a
->type
== b
->type
&&
1519 (token_data_is_string(a
->type
) ? streq_ptr(a
->data
, b
->data
) : (a
->data
== b
->data
));
1522 static bool nulstr_eq(const char *a
, const char *b
) {
1523 NULSTR_FOREACH(i
, a
)
1524 if (!nulstr_contains(b
, i
))
1527 NULSTR_FOREACH(i
, b
)
1528 if (!nulstr_contains(a
, i
))
1534 static bool token_type_and_value_eq(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1538 if (a
->type
!= b
->type
||
1539 a
->match_type
!= b
->match_type
)
1542 /* token value is ignored for certain match types */
1543 if (IN_SET(a
->match_type
, MATCH_TYPE_EMPTY
, MATCH_TYPE_SUBSYSTEM
))
1546 return type_has_nulstr_value(a
->type
) ? nulstr_eq(a
->value
, b
->value
) :
1547 streq_ptr(a
->value
, b
->value
);
1550 static bool conflicting_op(UdevRuleOperatorType a
, UdevRuleOperatorType b
) {
1551 return (a
== OP_MATCH
&& b
== OP_NOMATCH
) ||
1552 (a
== OP_NOMATCH
&& b
== OP_MATCH
);
1555 /* test whether all fields besides UdevRuleOperatorType of two tokens match */
1556 static bool tokens_eq(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1560 return a
->attr_subst_type
== b
->attr_subst_type
&&
1561 token_type_and_value_eq(a
, b
) &&
1562 token_type_and_data_eq(a
, b
);
1565 static bool nulstr_tokens_conflict(const UdevRuleToken
*a
, const UdevRuleToken
*b
) {
1569 if (!(a
->type
== b
->type
&&
1570 type_has_nulstr_value(a
->type
) &&
1572 a
->op
== OP_MATCH
&&
1573 a
->match_type
== b
->match_type
&&
1574 a
->attr_subst_type
== b
->attr_subst_type
&&
1575 token_type_and_data_eq(a
, b
)))
1578 if (a
->match_type
== MATCH_TYPE_PLAIN
) {
1579 NULSTR_FOREACH(i
, a
->value
)
1580 if (nulstr_contains(b
->value
, i
))
1585 if (a
->match_type
== MATCH_TYPE_GLOB
) {
1586 NULSTR_FOREACH(i
, a
->value
) {
1587 size_t i_n
= strcspn(i
, GLOB_CHARS
);
1590 NULSTR_FOREACH(j
, b
->value
) {
1591 size_t j_n
= strcspn(j
, GLOB_CHARS
);
1592 if (j_n
== 0 || strneq(i
, j
, MIN(i_n
, j_n
)))
1603 static void udev_check_unused_labels(UdevRuleLine
*line
) {
1606 if (FLAGS_SET(line
->type
, LINE_HAS_LABEL
) &&
1607 !FLAGS_SET(line
->type
, LINE_IS_REFERENCED
))
1608 log_line_notice(line
, "style: LABEL=\"%s\" is unused.", line
->label
);
1611 static void udev_check_conflicts_duplicates(UdevRuleLine
*line
) {
1614 bool conflicts
= false, duplicates
= false;
1616 LIST_FOREACH(tokens
, token
, line
->tokens
)
1617 LIST_FOREACH(tokens
, i
, token
->tokens_next
) {
1618 bool new_conflicts
= false, new_duplicates
= false;
1620 if (tokens_eq(token
, i
)) {
1621 if (!duplicates
&& token
->op
== i
->op
)
1622 new_duplicates
= true;
1623 if (!conflicts
&& conflicting_op(token
->op
, i
->op
))
1624 new_conflicts
= true;
1625 } else if (!conflicts
&& nulstr_tokens_conflict(token
, i
))
1626 new_conflicts
= true;
1630 if (new_duplicates
) {
1631 duplicates
= new_duplicates
;
1632 log_line_warning(line
, "duplicate expressions.");
1634 if (new_conflicts
) {
1635 conflicts
= new_conflicts
;
1636 log_line_error(line
, "conflicting match expressions, the line has no effect.");
1638 if (conflicts
&& duplicates
)
1643 static void udev_check_rule_line(UdevRuleLine
*line
) {
1644 udev_check_unused_labels(line
);
1645 udev_check_conflicts_duplicates(line
);
1648 int udev_rules_parse_file(UdevRules
*rules
, const char *filename
, bool extra_checks
, UdevRuleFile
**ret
) {
1649 _cleanup_(udev_rule_file_freep
) UdevRuleFile
*rule_file
= NULL
;
1650 _cleanup_free_
char *name
= NULL
;
1651 _cleanup_fclose_
FILE *f
= NULL
;
1658 f
= fopen(filename
, "re");
1663 if (errno
== ENOENT
)
1666 return log_warning_errno(errno
, "Failed to open %s, ignoring: %m", filename
);
1669 if (fstat(fileno(f
), &st
) < 0)
1670 return log_warning_errno(errno
, "Failed to stat %s, ignoring: %m", filename
);
1672 if (null_or_empty(&st
)) {
1673 log_debug("Skipping empty file: %s", filename
);
1679 r
= hashmap_put_stats_by_path(&rules
->stats_by_path
, filename
, &st
);
1681 return log_warning_errno(r
, "Failed to save stat for %s, ignoring: %m", filename
);
1683 (void) fd_warn_permissions(filename
, fileno(f
));
1685 log_debug("Reading rules file: %s", filename
);
1687 name
= strdup(filename
);
1691 rule_file
= new(UdevRuleFile
, 1);
1695 *rule_file
= (UdevRuleFile
) {
1696 .filename
= TAKE_PTR(name
),
1700 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1702 _cleanup_free_
char *continuation
= NULL
;
1703 unsigned line_nr
= 0, current_line_nr
= 0;
1704 bool ignore_line
= false;
1706 _cleanup_free_
char *buf
= NULL
;
1710 r
= read_line(f
, UDEV_LINE_SIZE
, &buf
);
1718 line_nr
= current_line_nr
;
1720 line
= skip_leading_chars(buf
, NULL
);
1722 /* Lines beginning with '#' are ignored regardless of line continuation. */
1728 if (continuation
&& !ignore_line
) {
1729 if (strlen(continuation
) + len
>= UDEV_LINE_SIZE
)
1732 if (!strextend(&continuation
, line
))
1736 line
= continuation
;
1741 if (len
> 0 && line
[len
- 1] == '\\') {
1745 line
[len
- 1] = '\0';
1746 if (!continuation
) {
1747 continuation
= strdup(line
);
1756 log_file_error(rule_file
, line_nr
, "Line is too long, ignored.");
1758 (void) rule_add_line(rule_file
, line
, line_nr
, extra_checks
);
1760 continuation
= mfree(continuation
);
1761 ignore_line
= false;
1765 log_file_error(rule_file
, line_nr
,
1766 "Unexpected EOF after line continuation, line ignored.");
1768 rule_resolve_goto(rule_file
);
1771 LIST_FOREACH(rule_lines
, line
, rule_file
->rule_lines
)
1772 udev_check_rule_line(line
);
1777 TAKE_PTR(rule_file
);
1781 unsigned udev_rule_file_get_issues(UdevRuleFile
*rule_file
) {
1784 return rule_file
->issues
;
1787 UdevRules
* udev_rules_new(ResolveNameTiming resolve_name_timing
) {
1788 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1790 UdevRules
*rules
= new(UdevRules
, 1);
1794 *rules
= (UdevRules
) {
1795 .resolve_name_timing
= resolve_name_timing
,
1801 int udev_rules_load(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
, char * const *extra
) {
1802 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1803 _cleanup_strv_free_
char **files
= NULL
, **directories
= NULL
;
1806 rules
= udev_rules_new(resolve_name_timing
);
1810 if (!strv_isempty(extra
)) {
1811 directories
= strv_copy(extra
);
1816 r
= strv_extend_strv(&directories
, CONF_PATHS_STRV("udev/rules.d"), /* filter_duplicates = */ false);
1820 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, (const char* const*) directories
);
1822 return log_debug_errno(r
, "Failed to enumerate rules files: %m");
1824 STRV_FOREACH(f
, files
) {
1825 r
= udev_rules_parse_file(rules
, *f
, /* extra_checks = */ false, NULL
);
1827 log_debug_errno(r
, "Failed to read rules file %s, ignoring: %m", *f
);
1830 *ret_rules
= TAKE_PTR(rules
);
1834 bool udev_rules_should_reload(UdevRules
*rules
) {
1835 _cleanup_hashmap_free_ Hashmap
*stats_by_path
= NULL
;
1841 r
= config_get_stats_by_path(".rules", NULL
, 0, RULES_DIRS
, /* check_dropins = */ false, &stats_by_path
);
1843 log_warning_errno(r
, "Failed to get stats of udev rules, ignoring: %m");
1847 if (!stats_by_path_equal(rules
->stats_by_path
, stats_by_path
)) {
1848 log_debug("Udev rules need reloading");
1855 static bool apply_format_full(
1857 UdevRuleToken
*token
,
1861 bool replace_whitespace
,
1870 bool truncated
= false;
1871 (void) udev_event_apply_format(event
, format
, result
, result_size
, replace_whitespace
, &truncated
);
1873 log_event_truncated(event
, token
, what
, format
);
1877 if (event
->trace
&& !streq(format
, result
))
1878 log_event_trace(event
, token
, "Format substitution: \"%s\" -> \"%s\"", format
, result
);
1883 static bool apply_format_value(
1885 UdevRuleToken
*token
,
1890 return apply_format_full(event
, token
, token
->value
, result
, result_size
, /* replace_whitespace = */ false, what
);
1893 static bool apply_format_attr(
1895 UdevRuleToken
*token
,
1900 return apply_format_full(event
, token
, token
->data
, result
, result_size
, /* replace_whitespace = */ false, what
);
1903 static bool token_match_string(UdevEvent
*event
, UdevRuleToken
*token
, const char *str
, bool log_result
) {
1905 bool match
= false, case_insensitive
;
1909 assert(token
->value
);
1910 assert(token
->type
< _TK_M_MAX
);
1912 str
= strempty(str
);
1913 value
= token
->value
;
1914 case_insensitive
= FLAGS_SET(token
->match_type
, MATCH_CASE_INSENSITIVE
);
1916 switch (token
->match_type
& _MATCH_TYPE_MASK
) {
1917 case MATCH_TYPE_EMPTY
:
1918 match
= isempty(str
);
1920 case MATCH_TYPE_SUBSYSTEM
:
1921 if (case_insensitive
)
1922 match
= STRCASE_IN_SET(str
, "subsystem", "class", "bus");
1924 match
= STR_IN_SET(str
, "subsystem", "class", "bus");
1926 case MATCH_TYPE_PLAIN_WITH_EMPTY
:
1932 case MATCH_TYPE_PLAIN
:
1933 NULSTR_FOREACH(i
, value
)
1934 if (case_insensitive
? strcaseeq(i
, str
) : streq(i
, str
)) {
1939 case MATCH_TYPE_GLOB_WITH_EMPTY
:
1945 case MATCH_TYPE_GLOB
:
1946 NULSTR_FOREACH(i
, value
)
1947 if ((fnmatch(i
, str
, case_insensitive
? FNM_CASEFOLD
: 0) == 0)) {
1953 assert_not_reached();
1956 bool result
= token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1959 switch (token
->match_type
& _MATCH_TYPE_MASK
) {
1960 case MATCH_TYPE_EMPTY
:
1961 log_event_trace(event
, token
, "String \"%s\" is%s empty%s",
1962 strempty(str
), match
? "" : " not",
1963 log_result
? result
? ": PASS" : ": FAIL" : ".");
1965 case MATCH_TYPE_SUBSYSTEM
:
1966 log_event_trace(event
, token
,
1967 "String \"%s\" matches %s of \"subsystem\", \"class\", or \"bus\"%s",
1968 strempty(str
), match
? "one" : "neither",
1969 log_result
? result
? ": PASS" : ": FAIL" : ".");
1971 case MATCH_TYPE_PLAIN_WITH_EMPTY
:
1972 case MATCH_TYPE_PLAIN
:
1973 case MATCH_TYPE_GLOB_WITH_EMPTY
:
1974 case MATCH_TYPE_GLOB
: {
1975 _cleanup_free_
char *joined
= NULL
;
1978 if (IN_SET(token
->match_type
& _MATCH_TYPE_MASK
, MATCH_TYPE_PLAIN_WITH_EMPTY
, MATCH_TYPE_GLOB_WITH_EMPTY
)) {
1979 (void) strextend_with_separator(&joined
, ", ", "\"\"");
1983 NULSTR_FOREACH(i
, value
) {
1984 (void) strextendf_with_separator(&joined
, ", ", "\"%s\"", i
);
1989 log_event_trace(event
, token
, "String \"%s\" %s %s%s",
1991 match
? (c
> 1 ? "matches one of" : "matches") : (c
> 1 ? "matches neither of" : "does not match"),
1993 log_result
? result
? ": PASS" : ": FAIL" : ".");
1997 assert_not_reached();
2003 static bool token_match_attr(UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
2004 char nbuf
[UDEV_NAME_SIZE
], vbuf
[UDEV_NAME_SIZE
];
2005 const char *name
, *value
;
2009 assert(IN_SET(token
->type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
));
2015 switch (token
->attr_subst_type
) {
2016 case SUBST_TYPE_FORMAT
:
2017 if (!apply_format_attr(event
, token
, nbuf
, sizeof(nbuf
), "sysfs attribute name"))
2022 case SUBST_TYPE_PLAIN
:
2023 r
= sd_device_get_sysattr_value(dev
, name
, &value
);
2025 log_event_trace_errno(event
, token
, r
, "Cannot read sysfs attribute%s%s%s: %m",
2026 name
!= token
->data
? " \"" : "",
2027 name
!= token
->data
? name
: "",
2028 name
!= token
->data
? "\"" : "");
2032 /* remove trailing whitespace, if not asked to match for it */
2033 if (FLAGS_SET(token
->match_type
, MATCH_REMOVE_TRAILING_WHITESPACE
)) {
2034 strscpy(vbuf
, sizeof(vbuf
), value
);
2035 value
= delete_trailing_chars(vbuf
, NULL
);
2038 return token_match_string(event
, token
, value
, /* log_result = */ true);
2040 case SUBST_TYPE_SUBSYS
:
2041 r
= udev_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true);
2043 log_event_trace_errno(event
, token
, r
, "Cannot read sysfs attribute: %m");
2047 /* remove trailing whitespace, if not asked to match for it */
2048 if (FLAGS_SET(token
->match_type
, MATCH_REMOVE_TRAILING_WHITESPACE
))
2049 delete_trailing_chars(vbuf
, NULL
);
2051 return token_match_string(event
, token
, vbuf
, /* log_result = */ true);
2054 assert_not_reached();
2058 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
2067 key
= skip_leading_chars(line
, NULL
);
2069 /* comment or empty line */
2070 if (IN_SET(key
[0], '#', '\0')) {
2071 *ret_key
= *ret_value
= NULL
;
2075 /* split key/value */
2076 val
= strchr(key
, '=');
2081 key
= strstrip(key
);
2085 val
= strstrip(val
);
2090 if (IN_SET(val
[0], '"', '\'')) {
2092 if (len
== 1 || val
[len
-1] != val
[0])
2103 static int attr_subst_subdir(char attr
[static UDEV_PATH_SIZE
]) {
2104 _cleanup_closedir_
DIR *dir
= NULL
;
2105 char buf
[UDEV_PATH_SIZE
], *p
;
2112 tail
= strstr(attr
, "/*/");
2116 len
= tail
- attr
+ 1; /* include slash at the end */
2117 tail
+= 2; /* include slash at the beginning */
2121 size
-= strnpcpy_full(&p
, size
, attr
, len
, &truncated
);
2129 FOREACH_DIRENT_ALL(de
, dir
, break) {
2130 if (de
->d_name
[0] == '.')
2133 strscpyl_full(p
, size
, &truncated
, de
->d_name
, tail
, NULL
);
2137 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
2147 static size_t udev_replace_ifname_strict(char *str
) {
2148 size_t replaced
= 0;
2152 /* See ifname_valid_full(). */
2154 for (char *p
= str
; *p
!= '\0'; p
++)
2155 if (!ifname_valid_char(*p
)) {
2163 static void udev_replace_ifname(UdevEvent
*event
, UdevRuleToken
*token
, char *buf
) {
2169 if (naming_scheme_has(NAMING_REPLACE_STRICTLY
))
2170 count
= udev_replace_ifname_strict(buf
);
2172 count
= udev_replace_chars(buf
, "/");
2174 log_event_trace(event
, token
,
2175 "Replaced %zu character(s) from network interface name, results to \"%s\"",
2179 static void udev_replace_chars_and_log(UdevEvent
*event
, UdevRuleToken
*token
, char *buf
, const char *allow
, const char *what
) {
2185 size_t count
= udev_replace_chars(buf
, allow
);
2187 log_event_trace(event
, token
,
2188 "Replaced %zu character(s) from %s, results to \"%s\"",
2192 static int udev_rule_apply_token_to_event(
2193 UdevRuleToken
*token
,
2203 /* This returns the following values:
2204 * 0 on the current token does not match the event,
2205 * 1 on the current token matches the event, and
2206 * negative errno on some critical errors. */
2208 switch (token
->type
) {
2210 sd_device_action_t a
;
2212 r
= sd_device_get_action(dev
, &a
);
2214 return log_event_error_errno(event
, token
, r
, "Failed to get uevent action type: %m");
2216 return token_match_string(event
, token
, device_action_to_string(a
), /* log_result = */ true);
2218 case TK_M_DEVPATH
: {
2221 r
= sd_device_get_devpath(dev
, &val
);
2223 return log_event_error_errno(event
, token
, r
, "Failed to get devpath: %m");
2225 return token_match_string(event
, token
, val
, /* log_result = */ true);
2228 case TK_M_PARENTS_KERNEL
: {
2231 r
= sd_device_get_sysname(dev
, &val
);
2233 return log_event_error_errno(event
, token
, r
, "Failed to get sysname: %m");
2235 return token_match_string(event
, token
, val
, /* log_result = */ true);
2238 FOREACH_DEVICE_DEVLINK(dev
, val
)
2239 if (token_match_string(event
, token
, strempty(startswith(val
, "/dev/")), /* log_result = */ false) == (token
->op
== OP_MATCH
))
2240 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2241 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2244 return token_match_string(event
, token
, event
->name
, /* log_result = */ true);
2247 const char *val
= NULL
;
2249 (void) device_get_property_value_with_fallback(dev
, token
->data
, event
->worker
? event
->worker
->properties
: NULL
, &val
);
2251 return token_match_string(event
, token
, val
, /* log_result = */ true);
2254 const char *val
, *k
= token
->data
;
2256 if (streq(k
, "arch"))
2257 val
= architecture_to_string(uname_architecture());
2258 else if (streq(k
, "virt"))
2259 val
= virtualization_to_string(detect_virtualization());
2260 else if (streq(k
, "cvm"))
2261 val
= confidential_virtualization_to_string(detect_confidential_virtualization());
2263 assert_not_reached();
2264 return token_match_string(event
, token
, val
, /* log_result = */ true);
2267 case TK_M_PARENTS_TAG
:
2268 FOREACH_DEVICE_CURRENT_TAG(dev
, val
)
2269 if (token_match_string(event
, token
, val
, /* log_result = */ false) == (token
->op
== OP_MATCH
))
2270 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2271 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2273 case TK_M_SUBSYSTEM
:
2274 case TK_M_PARENTS_SUBSYSTEM
: {
2277 r
= sd_device_get_subsystem(dev
, &val
);
2281 return log_event_error_errno(event
, token
, r
, "Failed to get subsystem: %m");
2283 return token_match_string(event
, token
, val
, /* log_result = */ true);
2286 case TK_M_PARENTS_DRIVER
: {
2289 r
= sd_device_get_driver(dev
, &val
);
2293 return log_event_error_errno(event
, token
, r
, "Failed to get driver: %m");
2295 return token_match_string(event
, token
, val
, /* log_result = */ true);
2298 case TK_M_PARENTS_ATTR
:
2299 return token_match_attr(token
, dev
, event
);
2302 _cleanup_free_
char *value
= NULL
;
2303 char buf
[UDEV_PATH_SIZE
];
2305 if (!apply_format_attr(event
, token
, buf
, sizeof(buf
), "sysctl entry name"))
2308 r
= sysctl_read(sysctl_normalize(buf
), &value
);
2309 if (r
< 0 && r
!= -ENOENT
)
2310 return log_event_error_errno(event
, token
, r
, "Failed to read sysctl \"%s\": %m", buf
);
2312 return token_match_string(event
, token
, strstrip(value
), /* log_result = */ true);
2315 mode_t mode
= PTR_TO_MODE(token
->data
);
2316 char buf
[UDEV_PATH_SIZE
];
2317 struct stat statbuf
;
2320 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "file name"))
2323 if (!path_is_absolute(buf
) &&
2324 udev_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
2325 char tmp
[UDEV_PATH_SIZE
];
2328 r
= sd_device_get_syspath(dev
, &val
);
2330 return log_event_error_errno(event
, token
, r
, "Failed to get syspath: %m");
2333 strscpy_full(tmp
, sizeof(tmp
), buf
, &truncated
);
2335 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", tmp
, NULL
);
2337 return log_event_result(event
, token
, false);
2340 r
= attr_subst_subdir(buf
);
2342 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2344 return log_event_error_errno(event
, token
, r
, "Failed to test for the existence of \"%s\": %m", buf
);
2346 if (stat(buf
, &statbuf
) < 0) {
2347 if (errno
!= ENOENT
)
2348 log_event_warning_errno(event
, token
, errno
, "Failed to stat \"%s\", ignoring: %m", buf
);
2349 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2352 if (mode
== MODE_INVALID
)
2353 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2355 match
= (statbuf
.st_mode
& mode
) > 0;
2356 return log_event_result(event
, token
, token
->op
== (match
? OP_MATCH
: OP_NOMATCH
));
2358 case TK_M_PROGRAM
: {
2359 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
2361 event
->program_result
= mfree(event
->program_result
);
2363 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "command"))
2366 log_event_debug(event
, token
, "Running command \"%s\"", buf
);
2368 r
= udev_event_spawn(event
, /* accept_failure = */ true, buf
, result
, sizeof(result
), NULL
);
2371 log_event_warning_errno(event
, token
, r
, "Failed to execute \"%s\": %m", buf
);
2372 else /* returned value is positive when program fails */
2373 log_event_debug(event
, token
, "Command \"%s\" returned %d (error)", buf
, r
);
2374 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2377 delete_trailing_chars(result
, "\n");
2378 udev_replace_chars_and_log(event
, token
, result
, UDEV_ALLOWED_CHARS_INPUT
, "command output");
2380 event
->program_result
= strdup(result
);
2381 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2383 case TK_M_IMPORT_FILE
: {
2384 _cleanup_fclose_
FILE *f
= NULL
;
2385 char buf
[UDEV_PATH_SIZE
];
2387 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "file name to be imported"))
2390 log_event_debug(event
, token
, "Importing properties from \"%s\"", buf
);
2392 f
= fopen(buf
, "re");
2394 if (errno
!= ENOENT
)
2395 return log_event_error_errno(event
, token
, errno
, "Failed to open \"%s\": %m", buf
);
2396 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2400 _cleanup_free_
char *line
= NULL
;
2403 r
= read_line(f
, LONG_LINE_MAX
, &line
);
2405 log_event_debug_errno(event
, token
, r
, "Failed to read \"%s\", ignoring: %m", buf
);
2406 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2409 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2411 r
= get_property_from_string(line
, &key
, &value
);
2413 log_event_debug_errno(event
, token
, r
,
2414 "Failed to parse key and value from \"%s\", ignoring: %m",
2421 r
= device_add_property(dev
, key
, value
);
2423 return log_event_error_errno(event
, token
, r
,
2424 "Failed to add property %s=%s: %m",
2426 log_event_trace(event
, token
, "Imported property \"%s=%s\".", key
, value
);
2429 assert_not_reached();
2431 case TK_M_IMPORT_PROGRAM
: {
2432 _cleanup_strv_free_
char **lines
= NULL
;
2433 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
2436 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "command"))
2439 log_event_debug(event
, token
, "Importing properties from results of \"%s\"", buf
);
2441 r
= udev_event_spawn(event
, /* accept_failure = */ true, buf
, result
, sizeof result
, &truncated
);
2444 log_event_warning_errno(event
, token
, r
, "Failed to execute \"%s\", ignoring: %m", buf
);
2445 else /* returned value is positive when program fails */
2446 log_event_debug(event
, token
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
2447 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2451 log_event_debug(event
, token
, "Result of \"%s\" is too long and truncated, ignoring the last line of the result.", buf
);
2453 /* Drop the last line. */
2455 for (char *p
= PTR_SUB1(buf
+ strlen(buf
), buf
); p
; p
= PTR_SUB1(p
, buf
))
2456 if (strchr(NEWLINE
, *p
)) {
2465 r
= strv_split_newlines_full(&lines
, result
, EXTRACT_RETAIN_ESCAPE
);
2469 log_event_warning_errno(event
, token
, r
,
2470 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf
);
2471 return log_event_result(event
, token
, false);
2474 STRV_FOREACH(line
, lines
) {
2477 r
= get_property_from_string(*line
, &key
, &value
);
2479 log_event_debug_errno(event
, token
, r
,
2480 "Failed to parse key and value from \"%s\", ignoring: %m",
2487 r
= device_add_property(dev
, key
, value
);
2489 return log_event_error_errno(event
, token
, r
,
2490 "Failed to add property %s=%s: %m",
2492 log_event_trace(event
, token
, "Imported property \"%s=%s\".", key
, value
);
2495 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2497 case TK_M_IMPORT_BUILTIN
: {
2498 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
2499 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
2500 unsigned mask
= 1U << (int) cmd
;
2501 char buf
[UDEV_LINE_SIZE
];
2503 if (udev_builtin_run_once(cmd
)) {
2504 /* check if we ran already */
2505 if (event
->builtin_run
& mask
) {
2506 log_event_debug(event
, token
, "Builtin command \"%s\" has already run, skipping.",
2507 udev_builtin_name(cmd
));
2508 /* return the result from earlier run */
2509 return log_event_result(event
, token
, token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
));
2512 event
->builtin_run
|= mask
;
2515 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "builtin command"))
2518 log_event_debug(event
, token
, "Importing properties from results of builtin command \"%s\".", buf
);
2520 r
= udev_builtin_run(event
, cmd
, buf
);
2522 /* remember failure */
2523 log_event_debug_errno(event
, token
, r
, "Failed to run builtin \"%s\": %m", buf
);
2524 event
->builtin_ret
|= mask
;
2526 return log_event_result(event
, token
, token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
));
2528 case TK_M_IMPORT_DB
: {
2531 if (!event
->dev_db_clone
)
2532 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2533 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
2535 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2537 return log_event_error_errno(event
, token
, r
,
2538 "Failed to get property \"%s\" from database: %m",
2541 r
= device_add_property(dev
, token
->value
, val
);
2543 return log_event_error_errno(event
, token
, r
, "Failed to add property \"%s=%s\": %m",
2545 log_event_trace(event
, token
, "Imported property \"%s=%s\".", token
->value
, val
);
2547 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2549 case TK_M_IMPORT_CMDLINE
: {
2550 _cleanup_free_
char *value
= NULL
;
2552 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2554 return log_event_error_errno(event
, token
, r
,
2555 "Failed to read \"%s\" option from /proc/cmdline: %m",
2558 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2560 const char *val
= value
?: "1";
2561 r
= device_add_property(dev
, token
->value
, val
);
2563 return log_event_error_errno(event
, token
, r
, "Failed to add property \"%s=%s\": %m",
2565 log_event_trace(event
, token
, "Imported property \"%s=%s\".", token
->value
, val
);
2567 return log_event_result(event
, token
, token
->op
== OP_MATCH
);
2569 case TK_M_IMPORT_PARENT
: {
2570 char buf
[UDEV_PATH_SIZE
];
2572 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "property name"))
2576 r
= sd_device_get_parent(dev
, &parent
);
2577 if (ERRNO_IS_NEG_DEVICE_ABSENT(r
))
2578 return log_event_result(event
, token
, token
->op
== OP_NOMATCH
);
2580 return log_event_error_errno(event
, token
, r
, "Failed to get parent device: %m");
2583 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
2584 if (fnmatch(buf
, key
, 0) != 0)
2587 r
= device_add_property(dev
, key
, val
);
2589 return log_event_error_errno(event
, token
, r
, "Failed to add property \"%s=%s\": %m", key
, val
);
2590 log_event_trace(event
, token
, "Imported property \"%s=%s\".", key
, val
);
2594 return log_event_result(event
, token
, token
->op
== (have
? OP_MATCH
: OP_NOMATCH
));
2597 return token_match_string(event
, token
, event
->program_result
, /* log_result = */ true);
2599 case TK_A_OPTIONS_DUMP
: {
2600 log_event_info(event
, token
, "Dumping current state:");
2602 _cleanup_(memstream_done
) MemStream m
= {};
2603 FILE *f
= memstream_init(&m
);
2607 (void) dump_event(event
, SD_JSON_FORMAT_OFF
, f
);
2609 _cleanup_free_
char *buf
= NULL
;
2610 r
= memstream_finalize(&m
, &buf
, NULL
);
2612 log_event_warning_errno(event
, token
, r
, "Failed to finalize memory stream, ignoring: %m");
2614 log_info("%s", buf
);
2616 log_event_info(event
, token
, "DONE");
2619 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
2620 event
->esc
= ESCAPE_NONE
;
2621 return log_event_done(event
, token
);
2623 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
2624 event
->esc
= ESCAPE_REPLACE
;
2625 return log_event_done(event
, token
);
2627 case TK_A_OPTIONS_DB_PERSIST
:
2628 device_set_db_persist(dev
);
2629 return log_event_done(event
, token
);
2631 case TK_A_OPTIONS_INOTIFY_WATCH
:
2632 if (event
->inotify_watch_final
)
2633 return log_event_final_set(event
, token
);
2635 if (token
->op
== OP_ASSIGN_FINAL
)
2636 event
->inotify_watch_final
= true;
2638 event
->inotify_watch
= token
->data
;
2639 return log_event_done(event
, token
);
2641 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
2642 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
2643 return log_event_done(event
, token
);
2645 case TK_A_OPTIONS_LOG_LEVEL
: {
2646 int level
= PTR_TO_INT(token
->data
);
2649 level
= event
->default_log_level
;
2651 if (event
->event_mode
== EVENT_UDEV_WORKER
)
2652 log_set_max_level(level
);
2654 _cleanup_free_
char *level_str
= NULL
;
2655 (void) log_level_to_string_alloc(level
, &level_str
);
2656 log_event_debug(event
, token
, "Running in test mode, skipping changing maximum log level to %s.", strna(level_str
));
2659 if (level
== LOG_DEBUG
&& !event
->log_level_was_debug
) {
2660 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
2661 if (event
->event_mode
== EVENT_UDEV_WORKER
)
2662 log_device_uevent(dev
, "The log level is changed to 'debug' while processing device");
2663 event
->log_level_was_debug
= true;
2666 return log_event_done(event
, token
);
2669 char owner
[UDEV_NAME_SIZE
];
2671 if (event
->owner_final
)
2672 return log_event_final_set(event
, token
);
2674 if (token
->op
== OP_ASSIGN_FINAL
)
2675 event
->owner_final
= true;
2677 if (!apply_format_value(event
, token
, owner
, sizeof(owner
), "user name"))
2680 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
2681 r
= userdb_by_name(owner
, &USERDB_MATCH_ROOT_AND_SYSTEM
,
2682 USERDB_SUPPRESS_SHADOW
| USERDB_PARSE_NUMERIC
| USERDB_SYNTHESIZE_NUMERIC
,
2685 log_event_error_errno(event
, token
, r
, "Unknown user \"%s\", ignoring.", owner
);
2686 else if (r
== -ENOEXEC
)
2687 log_event_error(event
, token
, "User \"%s\" is not a system user, ignoring.", owner
);
2689 log_event_error_errno(event
, token
, r
, "Failed to resolve user \"%s\", ignoring: %m", owner
);
2691 event
->uid
= ur
->uid
;
2692 log_event_debug(event
, token
, "Set owner: %s("UID_FMT
")", owner
, event
->uid
);
2697 char group
[UDEV_NAME_SIZE
];
2699 if (event
->group_final
)
2700 return log_event_final_set(event
, token
);
2702 if (token
->op
== OP_ASSIGN_FINAL
)
2703 event
->group_final
= true;
2705 if (!apply_format_value(event
, token
, group
, sizeof(group
), "group name"))
2708 _cleanup_(group_record_unrefp
) GroupRecord
*gr
= NULL
;
2709 r
= groupdb_by_name(group
, &USERDB_MATCH_ROOT_AND_SYSTEM
,
2710 USERDB_SUPPRESS_SHADOW
| USERDB_PARSE_NUMERIC
| USERDB_SYNTHESIZE_NUMERIC
,
2713 log_event_error_errno(event
, token
, r
, "Unknown group \"%s\", ignoring.", group
);
2714 else if (r
== -ENOEXEC
)
2715 log_event_error(event
, token
, "Group \"%s\" is not a system group, ignoring.", group
);
2717 log_event_error_errno(event
, token
, r
, "Failed to resolve group \"%s\", ignoring: %m", group
);
2719 event
->gid
= gr
->gid
;
2720 log_event_debug(event
, token
, "Set group: %s("GID_FMT
")", group
, event
->gid
);
2725 char mode_str
[UDEV_NAME_SIZE
];
2727 if (event
->mode_final
)
2728 return log_event_final_set(event
, token
);
2730 if (token
->op
== OP_ASSIGN_FINAL
)
2731 event
->mode_final
= true;
2733 if (!apply_format_value(event
, token
, mode_str
, sizeof(mode_str
), "mode"))
2736 r
= parse_mode(mode_str
, &event
->mode
);
2738 log_event_error_errno(event
, token
, r
, "Failed to parse mode \"%s\", ignoring: %m", mode_str
);
2740 log_event_debug(event
, token
, "Set mode: %#o", event
->mode
);
2744 if (event
->owner_final
)
2745 return log_event_final_set(event
, token
);
2747 if (token
->op
== OP_ASSIGN_FINAL
)
2748 event
->owner_final
= true;
2750 event
->uid
= PTR_TO_UID(ASSERT_PTR(token
->data
));
2751 log_event_debug(event
, token
, "Set owner ID: %u", event
->uid
);
2755 if (event
->group_final
)
2756 return log_event_final_set(event
, token
);
2758 if (token
->op
== OP_ASSIGN_FINAL
)
2759 event
->group_final
= true;
2761 event
->gid
= PTR_TO_GID(ASSERT_PTR(token
->data
));
2762 log_event_debug(event
, token
, "Set group ID: %u", event
->gid
);
2766 if (event
->mode_final
)
2767 return log_event_final_set(event
, token
);
2769 if (token
->op
== OP_ASSIGN_FINAL
)
2770 event
->mode_final
= true;
2772 event
->mode
= PTR_TO_MODE(ASSERT_PTR(token
->data
));
2773 log_event_debug(event
, token
, "Set mode: %#o", event
->mode
);
2776 case TK_A_SECLABEL
: {
2777 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2778 char label_str
[UDEV_LINE_SIZE
] = {};
2780 name
= strdup(token
->data
);
2784 if (!apply_format_value(event
, token
, label_str
, sizeof(label_str
), "security label"))
2787 if (!isempty(label_str
))
2788 label
= strdup(label_str
);
2790 label
= strdup(token
->value
);
2794 if (token
->op
== OP_ASSIGN
)
2795 ordered_hashmap_clear(event
->seclabel_list
);
2797 r
= ordered_hashmap_ensure_put(&event
->seclabel_list
, &trivial_hash_ops_free_free
, name
, label
);
2801 return log_event_error_errno(event
, token
, r
, "Failed to store security label \"%s=%s\": %m", name
, label
);
2803 log_event_debug(event
, token
, "Set security label: %s=%s", name
, label
);
2810 const char *val
, *name
= token
->data
;
2811 char value_new
[UDEV_NAME_SIZE
], *p
= value_new
;
2812 size_t l
= sizeof(value_new
);
2814 if (isempty(token
->value
)) {
2815 if (token
->op
== OP_ADD
)
2816 return log_event_done(event
, token
);
2818 r
= device_add_property(dev
, name
, NULL
);
2820 return log_event_error_errno(event
, token
, r
, "Failed to remove property \"%s\": %m", name
);
2821 log_event_trace(event
, token
, "Removed property \"%s\".", name
);
2825 if (token
->op
== OP_ADD
&&
2826 device_get_property_value_with_fallback(dev
, name
, event
->worker
? event
->worker
->properties
: NULL
, &val
) >= 0) {
2828 l
= strpcpyl_full(&p
, l
, &truncated
, val
, " ", NULL
);
2830 log_event_warning(event
, token
,
2831 "The buffer for the property is full, refusing to append new property \"%s=%s\".",
2832 name
, token
->value
);
2837 if (!apply_format_value(event
, token
, p
, l
, "property value"))
2840 if (event
->esc
== ESCAPE_REPLACE
)
2841 udev_replace_chars_and_log(event
, token
, p
, /* allow = */ NULL
, "property value");
2843 r
= device_add_property(dev
, name
, value_new
);
2845 return log_event_error_errno(event
, token
, r
, "Failed to set property \"%s=%s\": %m", name
, value_new
);
2846 log_event_trace(event
, token
, "Set property \"%s=%s\".", name
, value_new
);
2850 char buf
[UDEV_PATH_SIZE
];
2852 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "tag name"))
2855 if (token
->op
== OP_REMOVE
) {
2856 device_remove_tag(dev
, buf
);
2857 log_event_trace(event
, token
, "Removed tag \"%s\".", buf
);
2861 assert(IN_SET(token
->op
, OP_ASSIGN
, OP_ADD
));
2863 if (token
->op
== OP_ASSIGN
)
2864 device_cleanup_tags(dev
);
2866 r
= device_add_tag(dev
, buf
, /* both = */ true);
2870 log_event_warning_errno(event
, token
, r
, "Failed to %s tag \"%s\", ignoring: %m",
2871 token
->op
== OP_ASSIGN
? "set" : "add", buf
);
2873 log_event_trace(event
, token
, "%s tag \"%s\".", token
->op
== OP_ASSIGN
? "Set" : "Added", buf
);
2877 char buf
[UDEV_PATH_SIZE
];
2879 if (event
->name_final
)
2880 return log_event_final_set(event
, token
);
2882 if (token
->op
== OP_ASSIGN_FINAL
)
2883 event
->name_final
= true;
2885 if (sd_device_get_ifindex(dev
, NULL
) < 0) {
2886 log_event_warning(event
, token
, "Only network interfaces can be renamed, ignoring.");
2890 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "network interface name"))
2893 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
))
2894 udev_replace_ifname(event
, token
, buf
);
2896 r
= free_and_strdup_warn(&event
->name
, buf
);
2900 log_event_debug(event
, token
, "Set network interface name: %s", event
->name
);
2903 case TK_A_DEVLINK
: {
2904 char buf
[UDEV_PATH_SIZE
];
2906 if (event
->devlink_final
)
2907 return log_event_final_set(event
, token
);
2909 if (sd_device_get_devnum(dev
, NULL
) < 0) {
2910 log_event_debug(event
, token
, "Device does not have device node, ignoring to manage device node symlink.");
2914 if (token
->op
== OP_ASSIGN_FINAL
)
2915 event
->devlink_final
= true;
2917 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2918 device_cleanup_devlinks(dev
);
2920 if (!apply_format_full(event
, token
, token
->value
, buf
, sizeof(buf
),
2921 /* replace_whitespace = */ event
->esc
!= ESCAPE_NONE
,
2922 "symbolic link path"))
2925 /* By default or string_escape=none, allow multiple symlinks separated by spaces. */
2926 if (event
->esc
== ESCAPE_UNSET
)
2927 udev_replace_chars_and_log(event
, token
, buf
, /* allow = */ "/ ", "device node symlink");
2928 else if (event
->esc
== ESCAPE_REPLACE
)
2929 udev_replace_chars_and_log(event
, token
, buf
, /* allow = */ "/", "device node symlink");
2931 for (const char *p
= buf
;;) {
2932 _cleanup_free_
char *path
= NULL
;
2934 r
= extract_first_word(&p
, &path
, NULL
, EXTRACT_RETAIN_ESCAPE
);
2938 log_warning_errno(r
, "Failed to extract first path in device node symlinks, ignoring: %m");
2944 if (token
->op
== OP_REMOVE
) {
2945 r
= device_remove_devlink(dev
, path
);
2949 log_event_warning_errno(event
, token
, r
, "Failed to remove device node symlink \"%s\", ignoring: %m", path
);
2951 log_event_debug(event
, token
, "Removed device node symlink \"%s\"", path
);
2953 r
= device_add_devlink(dev
, path
);
2957 log_event_warning_errno(event
, token
, r
, "Failed to add device node symlink \"%s\", ignoring: %m", path
);
2959 log_event_debug(event
, token
, "Added device node symlink \"%s\".", path
);
2962 assert_not_reached();
2965 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2966 const char *key
= token
->data
;
2968 /* First, try to resolve "[<SUBSYSTEM>/<KERNEL>]<attribute>" format. */
2969 r
= udev_resolve_subsys_kernel(key
, buf
, sizeof(buf
), false);
2972 if (ERRNO_IS_NEG_DEVICE_ABSENT(r
)) {
2973 log_event_warning_errno(event
, token
, r
, "Failed to resolve sysfs attribute \"%s\", ignoring: %m", key
);
2977 /* If not, make the path to sysfs attribute absolute, to make '*' resolvable by attr_subst_subdir(). */
2978 const char *syspath
;
2979 r
= sd_device_get_syspath(dev
, &syspath
);
2981 return log_event_error_errno(event
, token
, r
, "Failed to get syspath: %m");
2984 strscpyl_full(buf
, sizeof(buf
), &truncated
, syspath
, "/", key
, NULL
);
2986 log_event_warning(event
, token
,
2987 "The path to the attribute \"%s/%s\" is too long, refusing to set the attribute.",
2992 /* Resolve '*' in the path. */
2993 r
= attr_subst_subdir(buf
);
2995 log_event_error_errno(event
, token
, r
, "Could not find file matches \"%s\", ignoring: %m", buf
);
3000 /* Then, make the path relative again. This also checks if the path being inside of the sysfs. */
3001 _cleanup_free_
char *resolved
= NULL
;
3002 _cleanup_close_
int fd
= -EBADF
;
3003 r
= device_chase(dev
, buf
, /* flags = */ 0, &resolved
, &fd
);
3005 log_event_error_errno(event
, token
, r
, "Could not chase sysfs attribute \"%s\", ignoring: %m", buf
);
3009 /* Apply formatting to the value. */
3010 if (!apply_format_value(event
, token
, value
, sizeof(value
), "attribute value"))
3013 if (EVENT_MODE_DESTRUCTIVE(event
)) {
3014 log_event_debug(event
, token
, "Writing \"%s\" to sysfs attribute \"%s\".", value
, resolved
);
3015 r
= sd_device_set_sysattr_value(dev
, resolved
, value
);
3017 log_event_error_errno(event
, token
, r
, "Failed to write \"%s\" to sysfs attribute \"%s\", ignoring: %m", value
, resolved
);
3019 event_cache_written_sysattr(event
, resolved
, value
);
3020 log_event_done(event
, token
);
3023 log_event_debug(event
, token
, "Running in test mode, skipping writing \"%s\" to sysfs attribute \"%s\".", value
, resolved
);
3025 /* We assume the attribute is writable if the path points to a regular file, and cache
3026 * the value to make it shown by OPTIONS="dump" or obtained by later ATTR match token. */
3027 r
= fd_verify_regular(fd
);
3028 if (r
< 0 && !ERRNO_IS_NEG_PRIVILEGE(r
))
3029 log_event_error_errno(event
, token
, r
, "Failed to verify sysfs attribute \"%s\" is a regular file: %m", resolved
);
3031 event_cache_written_sysattr(event
, resolved
, value
);
3033 _cleanup_free_
char *copied
= strdup(value
);
3037 r
= device_cache_sysattr_value(dev
, resolved
, value
, /* error = */ 0);
3039 log_event_warning_errno(event
, token
, r
, "Failed to cache sysfs attribute \"%s\", ignoring: %m", resolved
);
3049 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
3051 if (!apply_format_attr(event
, token
, buf
, sizeof(buf
), "sysctl entry name"))
3054 if (!apply_format_value(event
, token
, value
, sizeof(value
), "sysctl value"))
3057 sysctl_normalize(buf
);
3059 if (EVENT_MODE_DESTRUCTIVE(event
)) {
3060 log_event_debug(event
, token
, "Writing \"%s\" to sysctl entry \"%s\".", value
, buf
);
3061 r
= sysctl_write(buf
, value
);
3063 log_event_error_errno(event
, token
, r
, "Failed to write \"%s\" to sysctl entry \"%s\", ignoring: %m", value
, buf
);
3065 event_cache_written_sysctl(event
, buf
, value
);
3066 log_event_done(event
, token
);
3069 log_event_debug(event
, token
, "Running in test mode, skipping writing \"%s\" to sysctl entry \"%s\".", value
, buf
);
3071 _cleanup_free_
char *path
= path_join("/proc/sys/", buf
);
3075 r
= verify_regular_at(AT_FDCWD
, path
, /* follow = */ true);
3076 if (r
< 0 && !ERRNO_IS_NEG_PRIVILEGE(r
))
3077 log_event_error_errno(event
, token
, r
, "Failed to verify sysctl entry \"%s\" is a regular file: %m", buf
);
3079 event_cache_written_sysctl(event
, buf
, value
);
3083 case TK_A_RUN_BUILTIN
:
3084 case TK_A_RUN_PROGRAM
: {
3085 _cleanup_free_
char *cmd
= NULL
;
3086 char buf
[UDEV_LINE_SIZE
];
3088 if (event
->run_final
)
3089 return log_event_final_set(event
, token
);
3091 if (token
->op
== OP_ASSIGN_FINAL
)
3092 event
->run_final
= true;
3094 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
3095 ordered_hashmap_clear(event
->run_list
);
3097 if (!apply_format_value(event
, token
, buf
, sizeof(buf
), "command"))
3104 r
= ordered_hashmap_ensure_put(&event
->run_list
, &trivial_hash_ops_free
, cmd
, token
->data
);
3106 return log_event_error_errno(event
, token
, r
, "Failed to store command \"%s\": %m", cmd
);
3108 log_event_debug(event
, token
, "Set command: %s", cmd
);
3112 case TK_A_OPTIONS_STATIC_NODE
:
3113 /* do nothing for events. */
3117 assert_not_reached();
3120 assert_not_reached();
3123 static int udev_rule_apply_parent_token_to_event(UdevRuleToken
*head_token
, UdevEvent
*event
) {
3127 assert(token_is_for_parents(head_token
));
3130 UdevRuleLine
*line
= head_token
->rule_line
;
3132 _cleanup_free_
char *joined
= NULL
;
3134 LIST_FOREACH(tokens
, token
, head_token
)
3135 if (token_is_for_parents(token
))
3136 (void) strextend_with_separator(&joined
, ", ", token
->token_str
);
3140 log_event_line(event
, line
, "Checking conditions for parent devices (including self): %s", strna(joined
));
3143 event
->dev_parent
= ASSERT_PTR(event
->dev
);
3146 LIST_FOREACH(tokens
, token
, head_token
) {
3147 if (!token_is_for_parents(token
)) {
3148 r
= 1; /* avoid false maybe-uninitialized warning */
3149 break; /* All parent tokens match. */
3152 r
= udev_rule_apply_token_to_event(token
, event
->dev_parent
, event
);
3160 const char *s
= NULL
;
3161 (void) sd_device_get_syspath(event
->dev_parent
, &s
);
3162 log_event_line(event
, line
, "Parent device \"%s\" passed all parent conditions.", strna(s
));
3167 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
3168 event
->dev_parent
= NULL
;
3169 log_event_line(event
, line
, "No parent device passed parent conditions.");
3175 static int udev_rule_apply_line_to_event(
3178 UdevRuleLine
**next_line
) {
3180 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
3181 bool parents_done
= false;
3182 sd_device_action_t action
;
3189 r
= sd_device_get_action(event
->dev
, &action
);
3193 if (action
!= SD_DEVICE_REMOVE
) {
3194 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
3195 mask
|= LINE_HAS_DEVLINK
;
3197 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
3198 mask
|= LINE_HAS_NAME
;
3201 if ((line
->type
& mask
) == 0)
3204 event
->esc
= ESCAPE_UNSET
;
3206 DEVICE_TRACE_POINT(rules_apply_line
, event
->dev
, line
->rule_file
->filename
, line
->line_number
);
3208 LIST_FOREACH(tokens
, token
, line
->tokens
) {
3209 if (token_is_for_parents(token
)) {
3213 r
= udev_rule_apply_parent_token_to_event(token
, event
);
3217 parents_done
= true;
3221 r
= udev_rule_apply_token_to_event(token
, event
->dev
, event
);
3226 if (line
->goto_line
) {
3227 log_event_line(event
, line
, "GOTO=%s", strna(line
->goto_label
));
3228 *next_line
= line
->goto_line
; /* update next_line only when the line has GOTO token. */
3234 int udev_rules_apply_to_event(UdevRules
*rules
, UdevEvent
*event
) {
3240 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
3241 LIST_FOREACH_WITH_NEXT(rule_lines
, line
, next_line
, file
->rule_lines
) {
3242 r
= udev_rule_apply_line_to_event(line
, event
, &next_line
);
3250 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
3251 _cleanup_strv_free_
char **tags
= NULL
;
3252 uid_t uid
= UID_INVALID
;
3253 gid_t gid
= GID_INVALID
;
3254 mode_t mode
= MODE_INVALID
;
3259 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
3262 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
3263 if (token
->type
== TK_A_OWNER_ID
)
3264 uid
= PTR_TO_UID(token
->data
);
3265 else if (token
->type
== TK_A_GROUP_ID
)
3266 gid
= PTR_TO_GID(token
->data
);
3267 else if (token
->type
== TK_A_MODE_ID
)
3268 mode
= PTR_TO_MODE(token
->data
);
3269 else if (token
->type
== TK_A_TAG
) {
3270 r
= strv_extend(&tags
, token
->value
);
3273 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
3274 r
= static_node_apply_permissions(token
->value
, mode
, uid
, gid
, tags
);
3282 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
3287 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
3288 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
3289 r
= udev_rule_line_apply_static_dev_perms(line
);
3297 static const char* const resolve_name_timing_table
[_RESOLVE_NAME_TIMING_MAX
] = {
3298 [RESOLVE_NAME_NEVER
] = "never",
3299 [RESOLVE_NAME_LATE
] = "late",
3300 [RESOLVE_NAME_EARLY
] = "early",
3303 DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing
, ResolveNameTiming
);