1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 #include "alloc-util.h"
6 #include "architecture.h"
7 #include "conf-files.h"
9 #include "device-private.h"
10 #include "device-util.h"
11 #include "dirent-util.h"
14 #include "format-util.h"
16 #include "glob-util.h"
19 #include "netif-naming-scheme.h"
20 #include "nulstr-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "proc-cmdline.h"
24 #include "stat-util.h"
27 #include "sysctl-util.h"
28 #include "syslog-util.h"
29 #include "udev-builtin.h"
30 #include "udev-event.h"
31 #include "udev-netlink.h"
32 #include "udev-node.h"
33 #include "udev-rules.h"
34 #include "udev-util.h"
35 #include "user-util.h"
38 #define RULES_DIRS (const char* const*) CONF_PATHS_STRV("udev/rules.d")
46 OP_ASSIGN_FINAL
, /* := */
48 _OP_TYPE_INVALID
= -EINVAL
,
49 } UdevRuleOperatorType
;
52 MATCH_TYPE_EMPTY
, /* empty string */
53 MATCH_TYPE_PLAIN
, /* no special characters */
54 MATCH_TYPE_PLAIN_WITH_EMPTY
, /* no special characters with empty string, e.g., "|foo" */
55 MATCH_TYPE_GLOB
, /* shell globs ?,*,[] */
56 MATCH_TYPE_GLOB_WITH_EMPTY
, /* shell globs ?,*,[] with empty string, e.g., "|foo*" */
57 MATCH_TYPE_SUBSYSTEM
, /* "subsystem", "bus", or "class" */
59 _MATCH_TYPE_INVALID
= -EINVAL
,
63 SUBST_TYPE_PLAIN
, /* no substitution */
64 SUBST_TYPE_FORMAT
, /* % or $ */
65 SUBST_TYPE_SUBSYS
, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
67 _SUBST_TYPE_INVALID
= -EINVAL
,
68 } UdevRuleSubstituteType
;
71 /* lvalues which take match or nomatch operator */
72 TK_M_ACTION
, /* string, device_get_action() */
73 TK_M_DEVPATH
, /* path, sd_device_get_devpath() */
74 TK_M_KERNEL
, /* string, sd_device_get_sysname() */
75 TK_M_DEVLINK
, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */
76 TK_M_NAME
, /* string, name of network interface */
77 TK_M_ENV
, /* string, device property, takes key through attribute */
78 TK_M_CONST
, /* string, system-specific hard-coded constant */
79 TK_M_TAG
, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */
80 TK_M_SUBSYSTEM
, /* string, sd_device_get_subsystem() */
81 TK_M_DRIVER
, /* string, sd_device_get_driver() */
82 TK_M_ATTR
, /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */
83 TK_M_SYSCTL
, /* string, takes kernel parameter through attribute */
85 /* matches parent parameters */
86 TK_M_PARENTS_KERNEL
, /* string */
87 TK_M_PARENTS_SUBSYSTEM
, /* string */
88 TK_M_PARENTS_DRIVER
, /* string */
89 TK_M_PARENTS_ATTR
, /* string */
90 TK_M_PARENTS_TAG
, /* strv */
92 TK_M_TEST
, /* path, optionally mode_t can be specified by attribute, test the existence of a file */
93 TK_M_PROGRAM
, /* string, execute a program */
94 TK_M_IMPORT_FILE
, /* path */
95 TK_M_IMPORT_PROGRAM
, /* string, import properties from the result of program */
96 TK_M_IMPORT_BUILTIN
, /* string, import properties from the result of built-in command */
97 TK_M_IMPORT_DB
, /* string, import properties from database */
98 TK_M_IMPORT_CMDLINE
, /* string, kernel command line */
99 TK_M_IMPORT_PARENT
, /* string, parent property */
100 TK_M_RESULT
, /* string, result of TK_M_PROGRAM */
102 #define _TK_M_MAX (TK_M_RESULT + 1)
103 #define _TK_A_MIN _TK_M_MAX
105 /* lvalues which take one of assign operators */
106 TK_A_OPTIONS_STRING_ESCAPE_NONE
, /* no argument */
107 TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, /* no argument */
108 TK_A_OPTIONS_DB_PERSIST
, /* no argument */
109 TK_A_OPTIONS_INOTIFY_WATCH
, /* boolean */
110 TK_A_OPTIONS_DEVLINK_PRIORITY
, /* int */
111 TK_A_OPTIONS_LOG_LEVEL
, /* string of log level or "reset" */
112 TK_A_OWNER
, /* user name */
113 TK_A_GROUP
, /* group name */
114 TK_A_MODE
, /* mode string */
115 TK_A_OWNER_ID
, /* uid_t */
116 TK_A_GROUP_ID
, /* gid_t */
117 TK_A_MODE_ID
, /* mode_t */
118 TK_A_TAG
, /* string */
119 TK_A_OPTIONS_STATIC_NODE
, /* device path, /dev/... */
120 TK_A_SECLABEL
, /* string with attribute */
121 TK_A_ENV
, /* string with attribute */
122 TK_A_NAME
, /* ifname */
123 TK_A_DEVLINK
, /* string */
124 TK_A_ATTR
, /* string with attribute */
125 TK_A_SYSCTL
, /* string with attribute */
126 TK_A_RUN_BUILTIN
, /* string */
127 TK_A_RUN_PROGRAM
, /* string */
130 _TK_TYPE_INVALID
= -EINVAL
,
134 LINE_HAS_NAME
= 1 << 0, /* has NAME= */
135 LINE_HAS_DEVLINK
= 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */
136 LINE_HAS_STATIC_NODE
= 1 << 2, /* has OPTIONS=static_node */
137 LINE_HAS_GOTO
= 1 << 3, /* has GOTO= */
138 LINE_HAS_LABEL
= 1 << 4, /* has LABEL= */
139 LINE_UPDATE_SOMETHING
= 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */
142 typedef struct UdevRuleFile UdevRuleFile
;
143 typedef struct UdevRuleLine UdevRuleLine
;
144 typedef struct UdevRuleToken UdevRuleToken
;
146 struct UdevRuleToken
{
147 UdevRuleTokenType type
:8;
148 UdevRuleOperatorType op
:8;
149 UdevRuleMatchType match_type
:8;
150 UdevRuleSubstituteType attr_subst_type
:7;
151 bool attr_match_remove_trailing_whitespace
:1;
154 LIST_FIELDS(UdevRuleToken
, tokens
);
157 struct UdevRuleLine
{
159 unsigned line_number
;
160 UdevRuleLineType type
;
163 const char *goto_label
;
164 UdevRuleLine
*goto_line
;
166 UdevRuleFile
*rule_file
;
167 UdevRuleToken
*current_token
;
168 LIST_HEAD(UdevRuleToken
, tokens
);
169 LIST_FIELDS(UdevRuleLine
, rule_lines
);
172 struct UdevRuleFile
{
174 UdevRuleLine
*current_line
;
175 LIST_HEAD(UdevRuleLine
, rule_lines
);
176 LIST_FIELDS(UdevRuleFile
, rule_files
);
181 ResolveNameTiming resolve_name_timing
;
182 Hashmap
*known_users
;
183 Hashmap
*known_groups
;
184 UdevRuleFile
*current_file
;
185 LIST_HEAD(UdevRuleFile
, rule_files
);
188 /*** Logging helpers ***/
190 #define log_rule_full_errno_zerook(device, rules, level, error, fmt, ...) \
192 UdevRules *_r = (rules); \
193 UdevRuleFile *_f = _r ? _r->current_file : NULL; \
194 UdevRuleLine *_l = _f ? _f->current_line : NULL; \
195 const char *_n = _f ? _f->filename : NULL; \
197 log_device_full_errno_zerook( \
198 device, level, error, "%s:%u " fmt, \
199 strna(_n), _l ? _l->line_number : 0, \
203 #define log_rule_full_errno(device, rules, level, error, fmt, ...) \
205 int _error = (error); \
206 ASSERT_NON_ZERO(_error); \
207 log_rule_full_errno_zerook( \
208 device, rules, level, _error, fmt, ##__VA_ARGS__); \
211 #define log_rule_full(device, rules, level, ...) (void) log_rule_full_errno_zerook(device, rules, level, 0, __VA_ARGS__)
213 #define log_rule_debug(device, rules, ...) log_rule_full(device, rules, LOG_DEBUG, __VA_ARGS__)
214 #define log_rule_info(device, rules, ...) log_rule_full(device, rules, LOG_INFO, __VA_ARGS__)
215 #define log_rule_notice(device, rules, ...) log_rule_full(device, rules, LOG_NOTICE, __VA_ARGS__)
216 #define log_rule_warning(device, rules, ...) log_rule_full(device, rules, LOG_WARNING, __VA_ARGS__)
217 #define log_rule_error(device, rules, ...) log_rule_full(device, rules, LOG_ERR, __VA_ARGS__)
219 #define log_rule_debug_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_DEBUG, error, __VA_ARGS__)
220 #define log_rule_info_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_INFO, error, __VA_ARGS__)
221 #define log_rule_notice_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_NOTICE, error, __VA_ARGS__)
222 #define log_rule_warning_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_WARNING, error, __VA_ARGS__)
223 #define log_rule_error_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_ERR, error, __VA_ARGS__)
225 #define log_token_full_errno_zerook(rules, level, error, ...) log_rule_full_errno_zerook(NULL, rules, level, error, __VA_ARGS__)
226 #define log_token_full_errno(rules, level, error, ...) log_rule_full_errno(NULL, rules, level, error, __VA_ARGS__)
227 #define log_token_full(rules, level, ...) (void) log_token_full_errno_zerook(rules, level, 0, __VA_ARGS__)
229 #define log_token_debug(rules, ...) log_token_full(rules, LOG_DEBUG, __VA_ARGS__)
230 #define log_token_info(rules, ...) log_token_full(rules, LOG_INFO, __VA_ARGS__)
231 #define log_token_notice(rules, ...) log_token_full(rules, LOG_NOTICE, __VA_ARGS__)
232 #define log_token_warning(rules, ...) log_token_full(rules, LOG_WARNING, __VA_ARGS__)
233 #define log_token_error(rules, ...) log_token_full(rules, LOG_ERR, __VA_ARGS__)
235 #define log_token_debug_errno(rules, error, ...) log_token_full_errno(rules, LOG_DEBUG, error, __VA_ARGS__)
236 #define log_token_info_errno(rules, error, ...) log_token_full_errno(rules, LOG_INFO, error, __VA_ARGS__)
237 #define log_token_notice_errno(rules, error, ...) log_token_full_errno(rules, LOG_NOTICE, error, __VA_ARGS__)
238 #define log_token_warning_errno(rules, error, ...) log_token_full_errno(rules, LOG_WARNING, error, __VA_ARGS__)
239 #define log_token_error_errno(rules, error, ...) log_token_full_errno(rules, LOG_ERR, error, __VA_ARGS__)
241 #define _log_token_invalid(rules, key, type) \
242 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
243 "Invalid %s for %s.", type, key)
245 #define log_token_invalid_op(rules, key) _log_token_invalid(rules, key, "operator")
246 #define log_token_invalid_attr(rules, key) _log_token_invalid(rules, key, "attribute")
248 #define log_token_invalid_attr_format(rules, key, attr, offset, hint) \
249 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
250 "Invalid attribute \"%s\" for %s (char %zu: %s), ignoring.", \
251 attr, key, offset, hint)
252 #define log_token_invalid_value(rules, key, value, offset, hint) \
253 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
254 "Invalid value \"%s\" for %s (char %zu: %s), ignoring.", \
255 value, key, offset, hint)
257 static void log_unknown_owner(sd_device
*dev
, UdevRules
*rules
, int error
, const char *entity
, const char *name
) {
258 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
259 log_rule_error(dev
, rules
, "Unknown %s '%s', ignoring", entity
, name
);
261 log_rule_error_errno(dev
, rules
, error
, "Failed to resolve %s '%s', ignoring: %m", entity
, name
);
264 /*** Other functions ***/
266 static void udev_rule_token_free(UdevRuleToken
*token
) {
270 static void udev_rule_line_clear_tokens(UdevRuleLine
*rule_line
) {
273 LIST_FOREACH(tokens
, i
, rule_line
->tokens
)
274 udev_rule_token_free(i
);
276 rule_line
->tokens
= NULL
;
279 static UdevRuleLine
* udev_rule_line_free(UdevRuleLine
*rule_line
) {
283 udev_rule_line_clear_tokens(rule_line
);
285 if (rule_line
->rule_file
) {
286 if (rule_line
->rule_file
->current_line
== rule_line
)
287 rule_line
->rule_file
->current_line
= rule_line
->rule_lines_prev
;
289 LIST_REMOVE(rule_lines
, rule_line
->rule_file
->rule_lines
, rule_line
);
292 free(rule_line
->line
);
293 return mfree(rule_line
);
296 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine
*, udev_rule_line_free
);
298 static void udev_rule_file_free(UdevRuleFile
*rule_file
) {
302 LIST_FOREACH(rule_lines
, i
, rule_file
->rule_lines
)
303 udev_rule_line_free(i
);
305 free(rule_file
->filename
);
309 UdevRules
*udev_rules_free(UdevRules
*rules
) {
313 LIST_FOREACH(rule_files
, i
, rules
->rule_files
)
314 udev_rule_file_free(i
);
316 hashmap_free_free_key(rules
->known_users
);
317 hashmap_free_free_key(rules
->known_groups
);
321 static int rule_resolve_user(UdevRules
*rules
, const char *name
, uid_t
*ret
) {
322 _cleanup_free_
char *n
= NULL
;
330 val
= hashmap_get(rules
->known_users
, name
);
332 *ret
= PTR_TO_UID(val
);
336 r
= get_user_creds(&name
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
338 log_unknown_owner(NULL
, rules
, r
, "user", name
);
347 r
= hashmap_ensure_put(&rules
->known_users
, &string_hash_ops
, n
, UID_TO_PTR(uid
));
356 static int rule_resolve_group(UdevRules
*rules
, const char *name
, gid_t
*ret
) {
357 _cleanup_free_
char *n
= NULL
;
365 val
= hashmap_get(rules
->known_groups
, name
);
367 *ret
= PTR_TO_GID(val
);
371 r
= get_group_creds(&name
, &gid
, USER_CREDS_ALLOW_MISSING
);
373 log_unknown_owner(NULL
, rules
, r
, "group", name
);
382 r
= hashmap_ensure_put(&rules
->known_groups
, &string_hash_ops
, n
, GID_TO_PTR(gid
));
391 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
395 return SUBST_TYPE_SUBSYS
;
396 if (strchr(str
, '%') || strchr(str
, '$'))
397 return SUBST_TYPE_FORMAT
;
398 return SUBST_TYPE_PLAIN
;
401 static void rule_line_append_token(UdevRuleLine
*rule_line
, UdevRuleToken
*token
) {
405 if (rule_line
->current_token
)
406 LIST_APPEND(tokens
, rule_line
->current_token
, token
);
408 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
410 rule_line
->current_token
= token
;
413 static int rule_line_add_token(UdevRuleLine
*rule_line
, UdevRuleTokenType type
, UdevRuleOperatorType op
, char *value
, void *data
) {
414 UdevRuleToken
*token
;
415 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
416 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
417 bool remove_trailing_whitespace
= false;
421 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
422 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
424 if (type
< _TK_M_MAX
) {
426 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
428 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
429 match_type
= MATCH_TYPE_SUBSYSTEM
;
430 else if (isempty(value
))
431 match_type
= MATCH_TYPE_EMPTY
;
432 else if (streq(value
, "?*")) {
433 /* Convert KEY=="?*" -> KEY!="" */
434 match_type
= MATCH_TYPE_EMPTY
;
435 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
436 } else if (string_is_glob(value
))
437 match_type
= MATCH_TYPE_GLOB
;
439 match_type
= MATCH_TYPE_PLAIN
;
441 if (type
< TK_M_TEST
|| type
== TK_M_RESULT
) {
442 /* Convert value string to nulstr. */
443 bool bar
= true, empty
= false;
446 for (a
= b
= value
; *a
!= '\0'; a
++) {
460 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
468 if (match_type
== MATCH_TYPE_GLOB
)
469 match_type
= MATCH_TYPE_GLOB_WITH_EMPTY
;
470 if (match_type
== MATCH_TYPE_PLAIN
)
471 match_type
= MATCH_TYPE_PLAIN_WITH_EMPTY
;
476 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
481 if (len
> 0 && !isspace(value
[len
- 1]))
482 remove_trailing_whitespace
= true;
484 subst_type
= rule_get_substitution_type(data
);
487 token
= new(UdevRuleToken
, 1);
491 *token
= (UdevRuleToken
) {
496 .match_type
= match_type
,
497 .attr_subst_type
= subst_type
,
498 .attr_match_remove_trailing_whitespace
= remove_trailing_whitespace
,
501 rule_line_append_token(rule_line
, token
);
503 if (token
->type
== TK_A_NAME
)
504 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
506 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
507 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
508 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
509 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
511 else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
)
512 SET_FLAG(rule_line
->type
, LINE_HAS_STATIC_NODE
, true);
514 else if (token
->type
>= _TK_A_MIN
||
515 IN_SET(token
->type
, TK_M_PROGRAM
,
516 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
517 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
518 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
523 static void check_value_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
, bool nonempty
) {
527 if (nonempty
&& isempty(value
))
528 log_token_invalid_value(rules
, key
, value
, (size_t) 0, "empty value");
529 else if (udev_check_format(value
, &offset
, &hint
) < 0)
530 log_token_invalid_value(rules
, key
, value
, offset
+ 1, hint
);
533 static int check_attr_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
) {
538 return log_token_invalid_attr(rules
, key
);
539 if (udev_check_format(value
, &offset
, &hint
) < 0)
540 log_token_invalid_attr_format(rules
, key
, value
, offset
+ 1, hint
);
544 static int parse_token(UdevRules
*rules
, const char *key
, char *attr
, UdevRuleOperatorType op
, char *value
) {
545 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
546 UdevRuleLine
*rule_line
;
550 assert(rules
->current_file
);
551 assert(rules
->current_file
->current_line
);
555 rule_line
= rules
->current_file
->current_line
;
557 if (streq(key
, "ACTION")) {
559 return log_token_invalid_attr(rules
, key
);
561 return log_token_invalid_op(rules
, key
);
563 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
);
564 } else if (streq(key
, "DEVPATH")) {
566 return log_token_invalid_attr(rules
, key
);
568 return log_token_invalid_op(rules
, key
);
570 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
);
571 } else if (streq(key
, "KERNEL")) {
573 return log_token_invalid_attr(rules
, key
);
575 return log_token_invalid_op(rules
, key
);
577 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
);
578 } else if (streq(key
, "SYMLINK")) {
580 return log_token_invalid_attr(rules
, key
);
582 return log_token_invalid_op(rules
, key
);
585 check_value_format_and_warn(rules
, key
, value
, false);
586 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
);
588 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
);
589 } else if (streq(key
, "NAME")) {
591 return log_token_invalid_attr(rules
, key
);
593 return log_token_invalid_op(rules
, key
);
595 log_token_warning(rules
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key
);
600 if (streq(value
, "%k"))
601 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
602 "Ignoring NAME=\"%%k\", as it will take no effect.");
604 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
605 "Ignoring NAME=\"\", as udev will not delete any network interfaces.");
606 check_value_format_and_warn(rules
, key
, value
, false);
608 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
);
610 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
);
611 } else if (streq(key
, "ENV")) {
613 return log_token_invalid_attr(rules
, key
);
615 return log_token_invalid_op(rules
, key
);
616 if (op
== OP_ASSIGN_FINAL
) {
617 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
623 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
624 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
625 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
626 "Invalid ENV attribute. '%s' cannot be set.", attr
);
628 check_value_format_and_warn(rules
, key
, value
, false);
630 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
);
632 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
);
633 } else if (streq(key
, "CONST")) {
634 if (isempty(attr
) || !STR_IN_SET(attr
, "arch", "virt"))
635 return log_token_invalid_attr(rules
, key
);
637 return log_token_invalid_op(rules
, key
);
638 r
= rule_line_add_token(rule_line
, TK_M_CONST
, op
, value
, attr
);
639 } else if (streq(key
, "TAG")) {
641 return log_token_invalid_attr(rules
, key
);
642 if (op
== OP_ASSIGN_FINAL
) {
643 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
648 check_value_format_and_warn(rules
, key
, value
, true);
650 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
);
652 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
);
653 } else if (streq(key
, "SUBSYSTEM")) {
655 return log_token_invalid_attr(rules
, key
);
657 return log_token_invalid_op(rules
, key
);
659 if (STR_IN_SET(value
, "bus", "class"))
660 log_token_warning(rules
, "\"%s\" must be specified as \"subsystem\".", value
);
662 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
663 } else if (streq(key
, "DRIVER")) {
665 return log_token_invalid_attr(rules
, key
);
667 return log_token_invalid_op(rules
, key
);
669 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
);
670 } else if (streq(key
, "ATTR")) {
671 r
= check_attr_format_and_warn(rules
, key
, attr
);
675 return log_token_invalid_op(rules
, key
);
676 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
677 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
682 check_value_format_and_warn(rules
, key
, value
, false);
683 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
);
685 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
);
686 } else if (streq(key
, "SYSCTL")) {
687 r
= check_attr_format_and_warn(rules
, key
, attr
);
691 return log_token_invalid_op(rules
, key
);
692 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
693 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
698 check_value_format_and_warn(rules
, key
, value
, false);
699 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
);
701 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
);
702 } else if (streq(key
, "KERNELS")) {
704 return log_token_invalid_attr(rules
, key
);
706 return log_token_invalid_op(rules
, key
);
708 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
);
709 } else if (streq(key
, "SUBSYSTEMS")) {
711 return log_token_invalid_attr(rules
, key
);
713 return log_token_invalid_op(rules
, key
);
715 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
);
716 } else if (streq(key
, "DRIVERS")) {
718 return log_token_invalid_attr(rules
, key
);
720 return log_token_invalid_op(rules
, key
);
722 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
);
723 } else if (streq(key
, "ATTRS")) {
724 r
= check_attr_format_and_warn(rules
, key
, attr
);
728 return log_token_invalid_op(rules
, key
);
730 if (startswith(attr
, "device/"))
731 log_token_warning(rules
, "'device' link may not be available in future kernels.");
732 if (strstr(attr
, "../"))
733 log_token_warning(rules
, "Direct reference to parent sysfs directory, may break in future kernels.");
735 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
);
736 } else if (streq(key
, "TAGS")) {
738 return log_token_invalid_attr(rules
, key
);
740 return log_token_invalid_op(rules
, key
);
742 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
);
743 } else if (streq(key
, "TEST")) {
744 mode_t mode
= MODE_INVALID
;
746 if (!isempty(attr
)) {
747 r
= parse_mode(attr
, &mode
);
749 return log_token_error_errno(rules
, r
, "Failed to parse mode '%s': %m", attr
);
751 check_value_format_and_warn(rules
, key
, value
, true);
753 return log_token_invalid_op(rules
, key
);
755 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
));
756 } else if (streq(key
, "PROGRAM")) {
758 return log_token_invalid_attr(rules
, key
);
759 check_value_format_and_warn(rules
, key
, value
, true);
761 return log_token_invalid_op(rules
, key
);
765 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
);
766 } else if (streq(key
, "IMPORT")) {
768 return log_token_invalid_attr(rules
, key
);
769 check_value_format_and_warn(rules
, key
, value
, true);
771 return log_token_invalid_op(rules
, key
);
775 if (streq(attr
, "file"))
776 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
777 else if (streq(attr
, "program")) {
778 UdevBuiltinCommand cmd
;
780 cmd
= udev_builtin_lookup(value
);
782 log_token_debug(rules
,"Found builtin command '%s' for %s, replacing attribute", value
, key
);
783 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
785 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
);
786 } else if (streq(attr
, "builtin")) {
787 UdevBuiltinCommand cmd
;
789 cmd
= udev_builtin_lookup(value
);
791 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
792 "Unknown builtin command: %s", value
);
793 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
794 } else if (streq(attr
, "db"))
795 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
);
796 else if (streq(attr
, "cmdline"))
797 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
798 else if (streq(attr
, "parent"))
799 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
801 return log_token_invalid_attr(rules
, key
);
802 } else if (streq(key
, "RESULT")) {
804 return log_token_invalid_attr(rules
, key
);
806 return log_token_invalid_op(rules
, key
);
808 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
);
809 } else if (streq(key
, "OPTIONS")) {
813 return log_token_invalid_attr(rules
, key
);
814 if (is_match
|| op
== OP_REMOVE
)
815 return log_token_invalid_op(rules
, key
);
819 if (streq(value
, "string_escape=none"))
820 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
821 else if (streq(value
, "string_escape=replace"))
822 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
823 else if (streq(value
, "db_persist"))
824 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
);
825 else if (streq(value
, "watch"))
826 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1));
827 else if (streq(value
, "nowatch"))
828 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0));
829 else if ((tmp
= startswith(value
, "static_node=")))
830 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
);
831 else if ((tmp
= startswith(value
, "link_priority="))) {
834 r
= safe_atoi(tmp
, &prio
);
836 return log_token_error_errno(rules
, r
, "Failed to parse link priority '%s': %m", tmp
);
837 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
));
838 } else if ((tmp
= startswith(value
, "log_level="))) {
841 if (streq(tmp
, "reset"))
844 level
= log_level_from_string(tmp
);
846 return log_token_error_errno(rules
, level
, "Failed to parse log level '%s': %m", tmp
);
848 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_LOG_LEVEL
, op
, NULL
, INT_TO_PTR(level
));
850 log_token_warning(rules
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
853 } else if (streq(key
, "OWNER")) {
857 return log_token_invalid_attr(rules
, key
);
858 if (is_match
|| op
== OP_REMOVE
)
859 return log_token_invalid_op(rules
, key
);
861 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
865 if (parse_uid(value
, &uid
) >= 0)
866 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
867 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
868 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
869 r
= rule_resolve_user(rules
, value
, &uid
);
871 return log_token_error_errno(rules
, r
, "Failed to resolve user name '%s': %m", value
);
873 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
874 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
875 check_value_format_and_warn(rules
, key
, value
, true);
876 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
);
878 log_token_debug(rules
, "User name resolution is disabled, ignoring %s=%s", key
, value
);
881 } else if (streq(key
, "GROUP")) {
885 return log_token_invalid_attr(rules
, key
);
886 if (is_match
|| op
== OP_REMOVE
)
887 return log_token_invalid_op(rules
, key
);
889 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
893 if (parse_gid(value
, &gid
) >= 0)
894 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
895 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
896 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
897 r
= rule_resolve_group(rules
, value
, &gid
);
899 return log_token_error_errno(rules
, r
, "Failed to resolve group name '%s': %m", value
);
901 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
902 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
903 check_value_format_and_warn(rules
, key
, value
, true);
904 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
);
906 log_token_debug(rules
, "Resolving group name is disabled, ignoring GROUP=\"%s\"", value
);
909 } else if (streq(key
, "MODE")) {
913 return log_token_invalid_attr(rules
, key
);
914 if (is_match
|| op
== OP_REMOVE
)
915 return log_token_invalid_op(rules
, key
);
917 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
921 if (parse_mode(value
, &mode
) >= 0)
922 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
));
924 check_value_format_and_warn(rules
, key
, value
, true);
925 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
);
927 } else if (streq(key
, "SECLABEL")) {
929 return log_token_invalid_attr(rules
, key
);
930 check_value_format_and_warn(rules
, key
, value
, true);
931 if (is_match
|| op
== OP_REMOVE
)
932 return log_token_invalid_op(rules
, key
);
933 if (op
== OP_ASSIGN_FINAL
) {
934 log_token_warning(rules
, "%s key takes '=' or '+=' operator, assuming '='.", key
);
938 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, attr
);
939 } else if (streq(key
, "RUN")) {
940 if (is_match
|| op
== OP_REMOVE
)
941 return log_token_invalid_op(rules
, key
);
942 check_value_format_and_warn(rules
, key
, value
, true);
943 if (!attr
|| streq(attr
, "program"))
944 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
);
945 else if (streq(attr
, "builtin")) {
946 UdevBuiltinCommand cmd
;
948 cmd
= udev_builtin_lookup(value
);
950 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
951 "Unknown builtin command '%s', ignoring", value
);
952 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
954 return log_token_invalid_attr(rules
, key
);
955 } else if (streq(key
, "GOTO")) {
957 return log_token_invalid_attr(rules
, key
);
959 return log_token_invalid_op(rules
, key
);
960 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
961 log_token_warning(rules
, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value
);
965 rule_line
->goto_label
= value
;
966 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
968 } else if (streq(key
, "LABEL")) {
970 return log_token_invalid_attr(rules
, key
);
972 return log_token_invalid_op(rules
, key
);
974 rule_line
->label
= value
;
975 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
978 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'", key
);
985 static UdevRuleOperatorType
parse_operator(const char *op
) {
988 if (startswith(op
, "=="))
990 if (startswith(op
, "!="))
992 if (startswith(op
, "+="))
994 if (startswith(op
, "-="))
996 if (startswith(op
, "="))
998 if (startswith(op
, ":="))
999 return OP_ASSIGN_FINAL
;
1001 return _OP_TYPE_INVALID
;
1004 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
) {
1005 char *key_begin
, *key_end
, *attr
, *tmp
;
1006 UdevRuleOperatorType op
;
1015 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
1017 if (isempty(key_begin
))
1020 for (key_end
= key_begin
; ; key_end
++) {
1021 if (key_end
[0] == '\0')
1023 if (strchr(WHITESPACE
"={", key_end
[0]))
1025 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
1028 if (key_end
[0] == '{') {
1030 tmp
= strchr(attr
, '}');
1039 tmp
= skip_leading_chars(tmp
, NULL
);
1040 op
= parse_operator(tmp
);
1046 tmp
+= op
== OP_ASSIGN
? 1 : 2;
1047 tmp
= skip_leading_chars(tmp
, NULL
);
1048 r
= udev_rule_parse_value(tmp
, ret_value
, line
);
1052 *ret_key
= key_begin
;
1058 static void sort_tokens(UdevRuleLine
*rule_line
) {
1061 UdevRuleToken
*old_tokens
= TAKE_PTR(rule_line
->tokens
);
1062 rule_line
->current_token
= NULL
;
1064 while (old_tokens
) {
1065 UdevRuleToken
*min_token
= NULL
;
1067 LIST_FOREACH(tokens
, t
, old_tokens
)
1068 if (!min_token
|| min_token
->type
> t
->type
)
1071 LIST_REMOVE(tokens
, old_tokens
, min_token
);
1072 rule_line_append_token(rule_line
, min_token
);
1076 static int rule_add_line(UdevRules
*rules
, const char *line_str
, unsigned line_nr
) {
1077 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
1078 _cleanup_free_
char *line
= NULL
;
1079 UdevRuleFile
*rule_file
;
1084 assert(rules
->current_file
);
1087 rule_file
= rules
->current_file
;
1089 if (isempty(line_str
))
1092 /* We use memdup_suffix0() here, since we want to add a second NUL byte to the end, since possibly
1093 * some parsers might turn this into a "nulstr", which requires an extra NUL at the end. */
1094 line
= memdup_suffix0(line_str
, strlen(line_str
) + 1);
1098 rule_line
= new(UdevRuleLine
, 1);
1102 *rule_line
= (UdevRuleLine
) {
1103 .line
= TAKE_PTR(line
),
1104 .line_number
= line_nr
,
1105 .rule_file
= rule_file
,
1108 if (rule_file
->current_line
)
1109 LIST_APPEND(rule_lines
, rule_file
->current_line
, rule_line
);
1111 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
1113 rule_file
->current_line
= rule_line
;
1115 for (p
= rule_line
->line
; !isempty(p
); ) {
1116 char *key
, *attr
, *value
;
1117 UdevRuleOperatorType op
;
1119 r
= parse_line(&p
, &key
, &attr
, &op
, &value
);
1121 return log_token_error_errno(rules
, r
, "Invalid key/value pair, ignoring.");
1125 r
= parse_token(rules
, key
, attr
, op
, value
);
1130 if (rule_line
->type
== 0) {
1131 log_token_warning(rules
, "The line takes no effect, ignoring.");
1135 sort_tokens(rule_line
);
1136 TAKE_PTR(rule_line
);
1140 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
1143 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1144 LIST_FOREACH(rule_lines
, line
, rule_file
->rule_lines
) {
1145 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
1148 LIST_FOREACH(rule_lines
, i
, line
->rule_lines_next
)
1149 if (streq_ptr(i
->label
, line
->goto_label
)) {
1150 line
->goto_line
= i
;
1154 if (!line
->goto_line
) {
1155 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
1156 rule_file
->filename
, line
->line_number
, line
->goto_label
);
1158 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
1159 line
->goto_label
= NULL
;
1161 if ((line
->type
& ~LINE_HAS_LABEL
) == 0) {
1162 log_notice("%s:%u: The line takes no effect any more, dropping",
1163 rule_file
->filename
, line
->line_number
);
1164 if (line
->type
== LINE_HAS_LABEL
)
1165 udev_rule_line_clear_tokens(line
);
1167 udev_rule_line_free(line
);
1173 int udev_rules_parse_file(UdevRules
*rules
, const char *filename
) {
1174 _cleanup_free_
char *continuation
= NULL
, *name
= NULL
;
1175 _cleanup_fclose_
FILE *f
= NULL
;
1176 UdevRuleFile
*rule_file
;
1177 bool ignore_line
= false;
1178 unsigned line_nr
= 0;
1181 f
= fopen(filename
, "re");
1183 if (errno
== ENOENT
)
1189 (void) fd_warn_permissions(filename
, fileno(f
));
1191 if (null_or_empty_fd(fileno(f
))) {
1192 log_debug("Skipping empty file: %s", filename
);
1196 log_debug("Reading rules file: %s", filename
);
1198 name
= strdup(filename
);
1202 rule_file
= new(UdevRuleFile
, 1);
1206 *rule_file
= (UdevRuleFile
) {
1207 .filename
= TAKE_PTR(name
),
1210 if (rules
->current_file
)
1211 LIST_APPEND(rule_files
, rules
->current_file
, rule_file
);
1213 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1215 rules
->current_file
= rule_file
;
1218 _cleanup_free_
char *buf
= NULL
;
1222 r
= read_line(f
, UDEV_LINE_SIZE
, &buf
);
1229 line
= skip_leading_chars(buf
, NULL
);
1236 if (continuation
&& !ignore_line
) {
1237 if (strlen(continuation
) + len
>= UDEV_LINE_SIZE
)
1240 if (!strextend(&continuation
, line
))
1244 line
= continuation
;
1249 if (len
> 0 && line
[len
- 1] == '\\') {
1253 line
[len
- 1] = '\0';
1254 if (!continuation
) {
1255 continuation
= strdup(line
);
1264 log_error("%s:%u: Line is too long, ignored", filename
, line_nr
);
1266 (void) rule_add_line(rules
, line
, line_nr
);
1268 continuation
= mfree(continuation
);
1269 ignore_line
= false;
1272 rule_resolve_goto(rule_file
);
1276 UdevRules
* udev_rules_new(ResolveNameTiming resolve_name_timing
) {
1277 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1279 UdevRules
*rules
= new(UdevRules
, 1);
1283 *rules
= (UdevRules
) {
1284 .resolve_name_timing
= resolve_name_timing
,
1290 int udev_rules_load(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1291 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1292 _cleanup_strv_free_
char **files
= NULL
;
1295 rules
= udev_rules_new(resolve_name_timing
);
1299 (void) udev_rules_check_timestamp(rules
);
1301 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1303 return log_debug_errno(r
, "Failed to enumerate rules files: %m");
1305 STRV_FOREACH(f
, files
) {
1306 r
= udev_rules_parse_file(rules
, *f
);
1308 log_debug_errno(r
, "Failed to read rules file %s, ignoring: %m", *f
);
1311 *ret_rules
= TAKE_PTR(rules
);
1315 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1319 return paths_check_timestamp(RULES_DIRS
, &rules
->dirs_ts_usec
, true);
1322 static bool token_match_string(UdevRuleToken
*token
, const char *str
) {
1323 const char *i
, *value
;
1327 assert(token
->value
);
1328 assert(token
->type
< _TK_M_MAX
);
1330 str
= strempty(str
);
1331 value
= token
->value
;
1333 switch (token
->match_type
) {
1334 case MATCH_TYPE_EMPTY
:
1335 match
= isempty(str
);
1337 case MATCH_TYPE_SUBSYSTEM
:
1338 match
= STR_IN_SET(str
, "subsystem", "class", "bus");
1340 case MATCH_TYPE_PLAIN_WITH_EMPTY
:
1346 case MATCH_TYPE_PLAIN
:
1347 NULSTR_FOREACH(i
, value
)
1348 if (streq(i
, str
)) {
1353 case MATCH_TYPE_GLOB_WITH_EMPTY
:
1359 case MATCH_TYPE_GLOB
:
1360 NULSTR_FOREACH(i
, value
)
1361 if ((fnmatch(i
, str
, 0) == 0)) {
1367 assert_not_reached();
1370 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1373 static bool token_match_attr(UdevRules
*rules
, UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
1374 char nbuf
[UDEV_NAME_SIZE
], vbuf
[UDEV_NAME_SIZE
];
1375 const char *name
, *value
;
1380 assert(IN_SET(token
->type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
));
1386 switch (token
->attr_subst_type
) {
1387 case SUBST_TYPE_FORMAT
:
1388 (void) udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false, &truncated
);
1390 log_rule_debug(dev
, rules
,
1391 "The sysfs attribute name '%s' is truncated while substituting into '%s', "
1392 "assuming the %s key does not match.", nbuf
, name
,
1393 token
->type
== TK_M_ATTR
? "ATTR" : "ATTRS");
1399 case SUBST_TYPE_PLAIN
:
1400 if (device_get_sysattr_value_maybe_from_netlink(dev
, &event
->rtnl
, name
, &value
) < 0)
1403 case SUBST_TYPE_SUBSYS
:
1404 if (udev_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1409 assert_not_reached();
1412 /* remove trailing whitespace, if not asked to match for it */
1413 if (token
->attr_match_remove_trailing_whitespace
) {
1414 if (value
!= vbuf
) {
1415 strscpy(vbuf
, sizeof(vbuf
), value
);
1419 delete_trailing_chars(vbuf
, NULL
);
1422 return token_match_string(token
, value
);
1425 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
1434 key
= skip_leading_chars(line
, NULL
);
1436 /* comment or empty line */
1437 if (IN_SET(key
[0], '#', '\0')) {
1438 *ret_key
= *ret_value
= NULL
;
1442 /* split key/value */
1443 val
= strchr(key
, '=');
1448 key
= strstrip(key
);
1452 val
= strstrip(val
);
1457 if (IN_SET(val
[0], '"', '\'')) {
1459 if (len
== 1 || val
[len
-1] != val
[0])
1470 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
1471 const char *key
, *val
;
1478 r
= sd_device_get_parent(dev
, &parent
);
1484 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
1485 if (fnmatch(filter
, key
, 0) != 0)
1487 r
= device_add_property(dev
, key
, val
);
1495 static int attr_subst_subdir(char attr
[static UDEV_PATH_SIZE
]) {
1496 _cleanup_closedir_
DIR *dir
= NULL
;
1497 char buf
[UDEV_PATH_SIZE
], *p
;
1504 tail
= strstr(attr
, "/*/");
1508 len
= tail
- attr
+ 1; /* include slash at the end */
1509 tail
+= 2; /* include slash at the beginning */
1513 size
-= strnpcpy_full(&p
, size
, attr
, len
, &truncated
);
1521 FOREACH_DIRENT_ALL(de
, dir
, break) {
1522 if (de
->d_name
[0] == '.')
1525 strscpyl_full(p
, size
, &truncated
, de
->d_name
, tail
, NULL
);
1529 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
1539 static int udev_rule_apply_token_to_event(
1543 usec_t timeout_usec
,
1545 Hashmap
*properties_list
) {
1547 UdevRuleToken
*token
;
1554 /* This returns the following values:
1555 * 0 on the current token does not match the event,
1556 * 1 on the current token matches the event, and
1557 * negative errno on some critical errors. */
1559 token
= rules
->current_file
->current_line
->current_token
;
1561 switch (token
->type
) {
1563 sd_device_action_t a
;
1565 r
= sd_device_get_action(dev
, &a
);
1567 return log_rule_error_errno(dev
, rules
, r
, "Failed to get uevent action type: %m");
1569 return token_match_string(token
, device_action_to_string(a
));
1571 case TK_M_DEVPATH
: {
1574 r
= sd_device_get_devpath(dev
, &val
);
1576 return log_rule_error_errno(dev
, rules
, r
, "Failed to get devpath: %m");
1578 return token_match_string(token
, val
);
1581 case TK_M_PARENTS_KERNEL
: {
1584 r
= sd_device_get_sysname(dev
, &val
);
1586 return log_rule_error_errno(dev
, rules
, r
, "Failed to get sysname: %m");
1588 return token_match_string(token
, val
);
1590 case TK_M_DEVLINK
: {
1593 FOREACH_DEVICE_DEVLINK(dev
, val
)
1594 if (token_match_string(token
, strempty(startswith(val
, "/dev/"))))
1595 return token
->op
== OP_MATCH
;
1596 return token
->op
== OP_NOMATCH
;
1599 return token_match_string(token
, event
->name
);
1603 if (sd_device_get_property_value(dev
, token
->data
, &val
) < 0)
1604 val
= hashmap_get(properties_list
, token
->data
);
1606 return token_match_string(token
, val
);
1609 const char *val
, *k
= token
->data
;
1611 if (streq(k
, "arch"))
1612 val
= architecture_to_string(uname_architecture());
1613 else if (streq(k
, "virt"))
1614 val
= virtualization_to_string(detect_virtualization());
1616 assert_not_reached();
1617 return token_match_string(token
, val
);
1620 case TK_M_PARENTS_TAG
: {
1623 FOREACH_DEVICE_TAG(dev
, val
)
1624 if (token_match_string(token
, val
))
1625 return token
->op
== OP_MATCH
;
1626 return token
->op
== OP_NOMATCH
;
1628 case TK_M_SUBSYSTEM
:
1629 case TK_M_PARENTS_SUBSYSTEM
: {
1632 r
= sd_device_get_subsystem(dev
, &val
);
1636 return log_rule_error_errno(dev
, rules
, r
, "Failed to get subsystem: %m");
1638 return token_match_string(token
, val
);
1641 case TK_M_PARENTS_DRIVER
: {
1644 r
= sd_device_get_driver(dev
, &val
);
1648 return log_rule_error_errno(dev
, rules
, r
, "Failed to get driver: %m");
1650 return token_match_string(token
, val
);
1653 case TK_M_PARENTS_ATTR
:
1654 return token_match_attr(rules
, token
, dev
, event
);
1656 _cleanup_free_
char *value
= NULL
;
1657 char buf
[UDEV_PATH_SIZE
];
1660 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
1662 log_rule_debug(dev
, rules
, "The sysctl entry name '%s' is truncated while substituting into '%s', "
1663 "assuming the SYSCTL key does not match.", buf
, (const char*) token
->data
);
1667 r
= sysctl_read(sysctl_normalize(buf
), &value
);
1668 if (r
< 0 && r
!= -ENOENT
)
1669 return log_rule_error_errno(dev
, rules
, r
, "Failed to read sysctl '%s': %m", buf
);
1671 return token_match_string(token
, strstrip(value
));
1674 mode_t mode
= PTR_TO_MODE(token
->data
);
1675 char buf
[UDEV_PATH_SIZE
];
1676 struct stat statbuf
;
1677 bool match
, truncated
;
1679 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1681 log_rule_debug(dev
, rules
, "The file name '%s' is truncated while substituting into '%s', "
1682 "assuming the TEST key does not match", buf
, token
->value
);
1686 if (!path_is_absolute(buf
) &&
1687 udev_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
1688 char tmp
[UDEV_PATH_SIZE
];
1691 r
= sd_device_get_syspath(dev
, &val
);
1693 return log_rule_error_errno(dev
, rules
, r
, "Failed to get syspath: %m");
1695 strscpy_full(tmp
, sizeof(tmp
), buf
, &truncated
);
1697 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", tmp
, NULL
);
1702 r
= attr_subst_subdir(buf
);
1704 return token
->op
== OP_NOMATCH
;
1706 return log_rule_error_errno(dev
, rules
, r
, "Failed to test for the existence of '%s': %m", buf
);
1708 if (stat(buf
, &statbuf
) < 0)
1709 return token
->op
== OP_NOMATCH
;
1711 if (mode
== MODE_INVALID
)
1712 return token
->op
== OP_MATCH
;
1714 match
= (statbuf
.st_mode
& mode
) > 0;
1715 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1717 case TK_M_PROGRAM
: {
1718 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
1722 event
->program_result
= mfree(event
->program_result
);
1723 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1725 log_rule_debug(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
1726 "assuming the PROGRAM key does not match.", buf
, token
->value
);
1730 log_rule_debug(dev
, rules
, "Running PROGRAM '%s'", buf
);
1732 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof(result
), NULL
);
1735 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute \"%s\": %m", buf
);
1736 else /* returned value is positive when program fails */
1737 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error)", buf
, r
);
1738 return token
->op
== OP_NOMATCH
;
1741 delete_trailing_chars(result
, "\n");
1742 count
= udev_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1744 log_rule_debug(dev
, rules
, "Replaced %zu character(s) in result of \"%s\"",
1747 event
->program_result
= strdup(result
);
1748 return token
->op
== OP_MATCH
;
1750 case TK_M_IMPORT_FILE
: {
1751 _cleanup_fclose_
FILE *f
= NULL
;
1752 char buf
[UDEV_PATH_SIZE
];
1755 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1757 log_rule_debug(dev
, rules
, "The file name '%s' to be imported is truncated while substituting into '%s', "
1758 "assuming the IMPORT key does not match.", buf
, token
->value
);
1762 log_rule_debug(dev
, rules
, "Importing properties from '%s'", buf
);
1764 f
= fopen(buf
, "re");
1766 if (errno
!= ENOENT
)
1767 return log_rule_error_errno(dev
, rules
, errno
,
1768 "Failed to open '%s': %m", buf
);
1769 return token
->op
== OP_NOMATCH
;
1773 _cleanup_free_
char *line
= NULL
;
1776 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1778 log_rule_debug_errno(dev
, rules
, r
,
1779 "Failed to read '%s', ignoring: %m", buf
);
1780 return token
->op
== OP_NOMATCH
;
1785 r
= get_property_from_string(line
, &key
, &value
);
1787 log_rule_debug_errno(dev
, rules
, r
,
1788 "Failed to parse key and value from '%s', ignoring: %m",
1795 r
= device_add_property(dev
, key
, value
);
1797 return log_rule_error_errno(dev
, rules
, r
,
1798 "Failed to add property %s=%s: %m",
1802 return token
->op
== OP_MATCH
;
1804 case TK_M_IMPORT_PROGRAM
: {
1805 _cleanup_strv_free_
char **lines
= NULL
;
1806 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
1809 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1811 log_rule_debug(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
1812 "assuming the IMPORT key does not match.", buf
, token
->value
);
1816 log_rule_debug(dev
, rules
, "Importing properties from results of '%s'", buf
);
1818 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof result
, &truncated
);
1821 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute '%s', ignoring: %m", buf
);
1822 else /* returned value is positive when program fails */
1823 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
1824 return token
->op
== OP_NOMATCH
;
1830 /* Drop the last line. */
1831 for (char *p
= PTR_SUB1(buf
+ strlen(buf
), buf
); p
; p
= PTR_SUB1(p
, buf
))
1832 if (strchr(NEWLINE
, *p
)) {
1839 r
= strv_split_newlines_full(&lines
, result
, EXTRACT_RETAIN_ESCAPE
);
1843 log_rule_warning_errno(dev
, rules
, r
,
1844 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf
);
1848 STRV_FOREACH(line
, lines
) {
1851 r
= get_property_from_string(*line
, &key
, &value
);
1853 log_rule_debug_errno(dev
, rules
, r
,
1854 "Failed to parse key and value from '%s', ignoring: %m",
1861 r
= device_add_property(dev
, key
, value
);
1863 return log_rule_error_errno(dev
, rules
, r
,
1864 "Failed to add property %s=%s: %m",
1868 return token
->op
== OP_MATCH
;
1870 case TK_M_IMPORT_BUILTIN
: {
1871 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
1872 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
1873 unsigned mask
= 1U << (int) cmd
;
1874 char buf
[UDEV_LINE_SIZE
];
1877 if (udev_builtin_run_once(cmd
)) {
1878 /* check if we ran already */
1879 if (event
->builtin_run
& mask
) {
1880 log_rule_debug(dev
, rules
, "Skipping builtin '%s' in IMPORT key",
1881 udev_builtin_name(cmd
));
1882 /* return the result from earlier run */
1883 return token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
);
1886 event
->builtin_run
|= mask
;
1889 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1891 log_rule_debug(dev
, rules
, "The builtin command '%s' is truncated while substituting into '%s', "
1892 "assuming the IMPORT key does not match", buf
, token
->value
);
1896 log_rule_debug(dev
, rules
, "Importing properties from results of builtin command '%s'", buf
);
1898 r
= udev_builtin_run(dev
, &event
->rtnl
, cmd
, buf
, false);
1900 /* remember failure */
1901 log_rule_debug_errno(dev
, rules
, r
, "Failed to run builtin '%s': %m", buf
);
1902 event
->builtin_ret
|= mask
;
1904 return token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
);
1906 case TK_M_IMPORT_DB
: {
1909 if (!event
->dev_db_clone
)
1910 return token
->op
== OP_NOMATCH
;
1911 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
1913 return token
->op
== OP_NOMATCH
;
1915 return log_rule_error_errno(dev
, rules
, r
,
1916 "Failed to get property '%s' from database: %m",
1919 r
= device_add_property(dev
, token
->value
, val
);
1921 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1923 return token
->op
== OP_MATCH
;
1925 case TK_M_IMPORT_CMDLINE
: {
1926 _cleanup_free_
char *value
= NULL
;
1928 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
|PROC_CMDLINE_IGNORE_EFI_OPTIONS
, &value
);
1930 return log_rule_error_errno(dev
, rules
, r
,
1931 "Failed to read '%s' option from /proc/cmdline: %m",
1934 return token
->op
== OP_NOMATCH
;
1936 r
= device_add_property(dev
, token
->value
, value
?: "1");
1938 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1939 token
->value
, value
?: "1");
1940 return token
->op
== OP_MATCH
;
1942 case TK_M_IMPORT_PARENT
: {
1943 char buf
[UDEV_PATH_SIZE
];
1946 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1948 log_rule_debug(dev
, rules
, "The property name '%s' is truncated while substituting into '%s', "
1949 "assuming the IMPORT key does not match.", buf
, token
->value
);
1953 r
= import_parent_into_properties(dev
, buf
);
1955 return log_rule_error_errno(dev
, rules
, r
,
1956 "Failed to import properties '%s' from parent: %m",
1958 return token
->op
== (r
> 0 ? OP_MATCH
: OP_NOMATCH
);
1961 return token_match_string(token
, event
->program_result
);
1962 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
1963 event
->esc
= ESCAPE_NONE
;
1965 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
1966 event
->esc
= ESCAPE_REPLACE
;
1968 case TK_A_OPTIONS_DB_PERSIST
:
1969 device_set_db_persist(dev
);
1971 case TK_A_OPTIONS_INOTIFY_WATCH
:
1972 if (event
->inotify_watch_final
)
1974 if (token
->op
== OP_ASSIGN_FINAL
)
1975 event
->inotify_watch_final
= true;
1977 event
->inotify_watch
= token
->data
;
1979 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
1980 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
1982 case TK_A_OPTIONS_LOG_LEVEL
: {
1983 int level
= PTR_TO_INT(token
->data
);
1986 level
= event
->default_log_level
;
1988 log_set_max_level(level
);
1990 if (level
== LOG_DEBUG
&& !event
->log_level_was_debug
) {
1991 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
1992 log_device_uevent(dev
, "The log level is changed to 'debug' while processing device");
1993 event
->log_level_was_debug
= true;
1999 char owner
[UDEV_NAME_SIZE
];
2000 const char *ow
= owner
;
2003 if (event
->owner_final
)
2005 if (token
->op
== OP_ASSIGN_FINAL
)
2006 event
->owner_final
= true;
2008 (void) udev_event_apply_format(event
, token
->value
, owner
, sizeof(owner
), false, &truncated
);
2010 log_rule_warning(dev
, rules
, "The user name '%s' is truncated while substituting into '%s', "
2011 "refusing to apply the OWNER key.", owner
, token
->value
);
2015 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2017 log_unknown_owner(dev
, rules
, r
, "user", owner
);
2019 log_rule_debug(dev
, rules
, "OWNER %s(%u)", owner
, event
->uid
);
2023 char group
[UDEV_NAME_SIZE
];
2024 const char *gr
= group
;
2027 if (event
->group_final
)
2029 if (token
->op
== OP_ASSIGN_FINAL
)
2030 event
->group_final
= true;
2032 (void) udev_event_apply_format(event
, token
->value
, group
, sizeof(group
), false, &truncated
);
2034 log_rule_warning(dev
, rules
, "The group name '%s' is truncated while substituting into '%s', "
2035 "refusing to apply the GROUP key.", group
, token
->value
);
2039 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2041 log_unknown_owner(dev
, rules
, r
, "group", group
);
2043 log_rule_debug(dev
, rules
, "GROUP %s(%u)", group
, event
->gid
);
2047 char mode_str
[UDEV_NAME_SIZE
];
2050 if (event
->mode_final
)
2052 if (token
->op
== OP_ASSIGN_FINAL
)
2053 event
->mode_final
= true;
2055 (void) udev_event_apply_format(event
, token
->value
, mode_str
, sizeof(mode_str
), false, &truncated
);
2057 log_rule_warning(dev
, rules
, "The mode '%s' is truncated while substituting into %s, "
2058 "refusing to apply the MODE key.", mode_str
, token
->value
);
2062 r
= parse_mode(mode_str
, &event
->mode
);
2064 log_rule_error_errno(dev
, rules
, r
, "Failed to parse mode '%s', ignoring: %m", mode_str
);
2066 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
2070 if (event
->owner_final
)
2072 if (token
->op
== OP_ASSIGN_FINAL
)
2073 event
->owner_final
= true;
2076 event
->uid
= PTR_TO_UID(token
->data
);
2077 log_rule_debug(dev
, rules
, "OWNER %u", event
->uid
);
2080 if (event
->group_final
)
2082 if (token
->op
== OP_ASSIGN_FINAL
)
2083 event
->group_final
= true;
2086 event
->gid
= PTR_TO_GID(token
->data
);
2087 log_rule_debug(dev
, rules
, "GROUP %u", event
->gid
);
2090 if (event
->mode_final
)
2092 if (token
->op
== OP_ASSIGN_FINAL
)
2093 event
->mode_final
= true;
2096 event
->mode
= PTR_TO_MODE(token
->data
);
2097 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
2099 case TK_A_SECLABEL
: {
2100 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2101 char label_str
[UDEV_LINE_SIZE
] = {};
2104 name
= strdup(token
->data
);
2108 (void) udev_event_apply_format(event
, token
->value
, label_str
, sizeof(label_str
), false, &truncated
);
2110 log_rule_warning(dev
, rules
, "The security label '%s' is truncated while substituting into '%s', "
2111 "refusing to apply the SECLABEL key.", label_str
, token
->value
);
2115 if (!isempty(label_str
))
2116 label
= strdup(label_str
);
2118 label
= strdup(token
->value
);
2122 if (token
->op
== OP_ASSIGN
)
2123 ordered_hashmap_clear_free_free(event
->seclabel_list
);
2125 r
= ordered_hashmap_ensure_put(&event
->seclabel_list
, NULL
, name
, label
);
2129 return log_rule_error_errno(dev
, rules
, r
, "Failed to store SECLABEL{%s}='%s': %m", name
, label
);;
2131 log_rule_debug(dev
, rules
, "SECLABEL{%s}='%s'", name
, label
);
2138 const char *val
, *name
= token
->data
;
2139 char value_new
[UDEV_NAME_SIZE
], *p
= value_new
;
2140 size_t count
, l
= sizeof(value_new
);
2143 if (isempty(token
->value
)) {
2144 if (token
->op
== OP_ADD
)
2146 r
= device_add_property(dev
, name
, NULL
);
2148 return log_rule_error_errno(dev
, rules
, r
, "Failed to remove property '%s': %m", name
);
2152 if (token
->op
== OP_ADD
&&
2153 sd_device_get_property_value(dev
, name
, &val
) >= 0) {
2154 l
= strpcpyl_full(&p
, l
, &truncated
, val
, " ", NULL
);
2156 log_rule_warning(dev
, rules
, "The buffer for the property '%s' is full, "
2157 "refusing to append the new value '%s'.", name
, token
->value
);
2162 (void) udev_event_apply_format(event
, token
->value
, p
, l
, false, &truncated
);
2164 log_rule_warning(dev
, rules
, "The property value '%s' is truncated while substituting into '%s', "
2165 "refusing to add property '%s'.", p
, token
->value
, name
);
2169 if (event
->esc
== ESCAPE_REPLACE
) {
2170 count
= udev_replace_chars(p
, NULL
);
2172 log_rule_debug(dev
, rules
, "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"",
2173 count
, name
, token
->op
== OP_ADD
? "+" : "", token
->value
);
2176 r
= device_add_property(dev
, name
, value_new
);
2178 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m", name
, value_new
);
2182 char buf
[UDEV_PATH_SIZE
];
2185 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2187 log_rule_warning(dev
, rules
, "The tag name '%s' is truncated while substituting into '%s',"
2188 "refusing to %s the tag.", buf
, token
->value
,
2189 token
->op
== OP_REMOVE
? "remove" : "add");
2193 if (token
->op
== OP_ASSIGN
)
2194 device_cleanup_tags(dev
);
2196 if (buf
[strspn(buf
, ALPHANUMERICAL
"-_")] != '\0') {
2197 log_rule_error(dev
, rules
, "Invalid tag name '%s', ignoring", buf
);
2200 if (token
->op
== OP_REMOVE
)
2201 device_remove_tag(dev
, buf
);
2203 r
= device_add_tag(dev
, buf
, true);
2205 return log_rule_error_errno(dev
, rules
, r
, "Failed to add tag '%s': %m", buf
);
2210 char buf
[UDEV_PATH_SIZE
];
2214 if (event
->name_final
)
2216 if (token
->op
== OP_ASSIGN_FINAL
)
2217 event
->name_final
= true;
2219 if (sd_device_get_ifindex(dev
, NULL
) < 0) {
2220 log_rule_error(dev
, rules
,
2221 "Only network interfaces can be renamed, ignoring NAME=\"%s\".",
2226 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2228 log_rule_warning(dev
, rules
, "The network interface name '%s' is truncated while substituting into '%s', "
2229 "refusing to set the name.", buf
, token
->value
);
2233 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2234 if (naming_scheme_has(NAMING_REPLACE_STRICTLY
))
2235 count
= udev_replace_ifname(buf
);
2237 count
= udev_replace_chars(buf
, "/");
2239 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of NAME=\"%s\"",
2240 count
, token
->value
);
2242 r
= free_and_strdup_warn(&event
->name
, buf
);
2246 log_rule_debug(dev
, rules
, "NAME '%s'", event
->name
);
2249 case TK_A_DEVLINK
: {
2250 char buf
[UDEV_PATH_SIZE
], *p
;
2254 if (event
->devlink_final
)
2256 if (sd_device_get_devnum(dev
, NULL
) < 0)
2258 if (token
->op
== OP_ASSIGN_FINAL
)
2259 event
->devlink_final
= true;
2260 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2261 device_cleanup_devlinks(dev
);
2263 /* allow multiple symlinks separated by spaces */
2264 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), event
->esc
!= ESCAPE_NONE
, &truncated
);
2266 log_rule_warning(dev
, rules
, "The symbolic link path '%s' is truncated while substituting into '%s', "
2267 "refusing to add the device symbolic link.", buf
, token
->value
);
2271 if (event
->esc
== ESCAPE_UNSET
)
2272 count
= udev_replace_chars(buf
, "/ ");
2273 else if (event
->esc
== ESCAPE_REPLACE
)
2274 count
= udev_replace_chars(buf
, "/");
2278 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of SYMLINK=\"%s\"",
2279 count
, token
->value
);
2281 p
= skip_leading_chars(buf
, NULL
);
2282 while (!isempty(p
)) {
2283 char filename
[UDEV_PATH_SIZE
], *next
;
2285 next
= strchr(p
, ' ');
2288 next
= skip_leading_chars(next
, NULL
);
2291 strscpyl_full(filename
, sizeof(filename
), &truncated
, "/dev/", p
, NULL
);
2295 r
= device_add_devlink(dev
, filename
);
2297 return log_rule_error_errno(dev
, rules
, r
, "Failed to add devlink '%s': %m", filename
);
2299 log_rule_debug(dev
, rules
, "LINK '%s'", p
);
2305 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2306 const char *val
, *key_name
= token
->data
;
2309 if (udev_resolve_subsys_kernel(key_name
, buf
, sizeof(buf
), false) < 0 &&
2310 sd_device_get_syspath(dev
, &val
) >= 0) {
2311 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", key_name
, NULL
);
2313 log_rule_warning(dev
, rules
,
2314 "The path to the attribute '%s/%s' is too long, refusing to set the attribute.",
2320 r
= attr_subst_subdir(buf
);
2322 log_rule_error_errno(dev
, rules
, r
, "Could not find file matches '%s', ignoring: %m", buf
);
2325 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2327 log_rule_warning(dev
, rules
, "The attribute value '%s' is truncated while substituting into '%s', "
2328 "refusing to set the attribute '%s'", value
, token
->value
, buf
);
2332 log_rule_debug(dev
, rules
, "ATTR '%s' writing '%s'", buf
, value
);
2333 r
= write_string_file(buf
, value
,
2334 WRITE_STRING_FILE_VERIFY_ON_FAILURE
|
2335 WRITE_STRING_FILE_DISABLE_BUFFER
|
2336 WRITE_STRING_FILE_AVOID_NEWLINE
|
2337 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE
);
2339 log_rule_error_errno(dev
, rules
, r
, "Failed to write ATTR{%s}, ignoring: %m", buf
);
2343 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2346 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
2348 log_rule_warning(dev
, rules
, "The sysctl entry name '%s' is truncated while substituting into '%s', "
2349 "refusing to set the sysctl entry.", buf
, (const char*) token
->data
);
2353 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2355 log_rule_warning(dev
, rules
, "The sysctl value '%s' is truncated while substituting into '%s', "
2356 "refusing to set the sysctl entry '%s'", value
, token
->value
, buf
);
2360 sysctl_normalize(buf
);
2361 log_rule_debug(dev
, rules
, "SYSCTL '%s' writing '%s'", buf
, value
);
2362 r
= sysctl_write(buf
, value
);
2364 log_rule_error_errno(dev
, rules
, r
, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf
, value
);
2367 case TK_A_RUN_BUILTIN
:
2368 case TK_A_RUN_PROGRAM
: {
2369 _cleanup_free_
char *cmd
= NULL
;
2370 char buf
[UDEV_LINE_SIZE
];
2373 if (event
->run_final
)
2375 if (token
->op
== OP_ASSIGN_FINAL
)
2376 event
->run_final
= true;
2378 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2379 ordered_hashmap_clear_free_key(event
->run_list
);
2381 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2383 log_rule_warning(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
2384 "refusing to invoke the command.", buf
, token
->value
);
2392 r
= ordered_hashmap_ensure_put(&event
->run_list
, NULL
, cmd
, token
->data
);
2396 return log_rule_error_errno(dev
, rules
, r
, "Failed to store command '%s': %m", cmd
);
2400 log_rule_debug(dev
, rules
, "RUN '%s'", token
->value
);
2403 case TK_A_OPTIONS_STATIC_NODE
:
2404 /* do nothing for events. */
2407 assert_not_reached();
2413 static bool token_is_for_parents(UdevRuleToken
*token
) {
2414 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
2417 static int udev_rule_apply_parent_token_to_event(
2420 int timeout_signal
) {
2423 UdevRuleToken
*head
;
2427 assert(rules
->current_file
);
2430 line
= ASSERT_PTR(rules
->current_file
->current_line
);
2431 head
= ASSERT_PTR(rules
->current_file
->current_line
->current_token
);
2432 event
->dev_parent
= ASSERT_PTR(event
->dev
);
2435 LIST_FOREACH(tokens
, token
, head
) {
2436 if (!token_is_for_parents(token
))
2437 return true; /* All parent tokens match. */
2439 line
->current_token
= token
;
2440 r
= udev_rule_apply_token_to_event(rules
, event
->dev_parent
, event
, 0, timeout_signal
, NULL
);
2447 /* All parent tokens match, and no more token (except for GOTO) in the line. */
2450 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
2451 event
->dev_parent
= NULL
;
2457 static int udev_rule_apply_line_to_event(
2460 usec_t timeout_usec
,
2462 Hashmap
*properties_list
,
2463 UdevRuleLine
**next_line
) {
2465 UdevRuleLine
*line
= rules
->current_file
->current_line
;
2466 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
2467 bool parents_done
= false;
2468 sd_device_action_t action
;
2471 r
= sd_device_get_action(event
->dev
, &action
);
2475 if (action
!= SD_DEVICE_REMOVE
) {
2476 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
2477 mask
|= LINE_HAS_DEVLINK
;
2479 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
2480 mask
|= LINE_HAS_NAME
;
2483 if ((line
->type
& mask
) == 0)
2486 event
->esc
= ESCAPE_UNSET
;
2488 DEVICE_TRACE_POINT(rules_apply_line
, event
->dev
, line
->rule_file
->filename
, line
->line_number
);
2490 LIST_FOREACH(tokens
, token
, line
->tokens
) {
2491 line
->current_token
= token
;
2493 if (token_is_for_parents(token
)) {
2497 r
= udev_rule_apply_parent_token_to_event(rules
, event
, timeout_signal
);
2501 parents_done
= true;
2505 r
= udev_rule_apply_token_to_event(rules
, event
->dev
, event
, timeout_usec
, timeout_signal
, properties_list
);
2510 if (line
->goto_line
)
2511 *next_line
= line
->goto_line
;
2516 int udev_rules_apply_to_event(
2519 usec_t timeout_usec
,
2521 Hashmap
*properties_list
) {
2528 LIST_FOREACH(rule_files
, file
, rules
->rule_files
) {
2529 rules
->current_file
= file
;
2530 LIST_FOREACH_WITH_NEXT(rule_lines
, line
, next_line
, file
->rule_lines
) {
2531 file
->current_line
= line
;
2532 r
= udev_rule_apply_line_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
, &next_line
);
2541 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
2542 _cleanup_strv_free_
char **tags
= NULL
;
2543 uid_t uid
= UID_INVALID
;
2544 gid_t gid
= GID_INVALID
;
2545 mode_t mode
= MODE_INVALID
;
2550 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
2553 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
2554 if (token
->type
== TK_A_OWNER_ID
)
2555 uid
= PTR_TO_UID(token
->data
);
2556 else if (token
->type
== TK_A_GROUP_ID
)
2557 gid
= PTR_TO_GID(token
->data
);
2558 else if (token
->type
== TK_A_MODE_ID
)
2559 mode
= PTR_TO_MODE(token
->data
);
2560 else if (token
->type
== TK_A_TAG
) {
2561 r
= strv_extend(&tags
, token
->value
);
2564 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
2565 r
= static_node_apply_permissions(token
->value
, mode
, uid
, gid
, tags
);
2573 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2578 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2579 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
2580 r
= udev_rule_line_apply_static_dev_perms(line
);