1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 #include "alloc-util.h"
6 #include "architecture.h"
7 #include "conf-files.h"
8 #include "conf-parser.h"
10 #include "device-private.h"
11 #include "device-util.h"
12 #include "dirent-util.h"
15 #include "format-util.h"
17 #include "glob-util.h"
20 #include "netif-naming-scheme.h"
21 #include "nulstr-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "proc-cmdline.h"
25 #include "stat-util.h"
28 #include "sysctl-util.h"
29 #include "syslog-util.h"
30 #include "udev-builtin.h"
31 #include "udev-event.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
);
180 ResolveNameTiming resolve_name_timing
;
181 Hashmap
*known_users
;
182 Hashmap
*known_groups
;
183 Hashmap
*stats_by_path
;
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
);
318 hashmap_free(rules
->stats_by_path
);
322 static int rule_resolve_user(UdevRules
*rules
, const char *name
, uid_t
*ret
) {
323 _cleanup_free_
char *n
= NULL
;
331 val
= hashmap_get(rules
->known_users
, name
);
333 *ret
= PTR_TO_UID(val
);
337 r
= get_user_creds(&name
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
339 log_unknown_owner(NULL
, rules
, r
, "user", name
);
348 r
= hashmap_ensure_put(&rules
->known_users
, &string_hash_ops
, n
, UID_TO_PTR(uid
));
357 static int rule_resolve_group(UdevRules
*rules
, const char *name
, gid_t
*ret
) {
358 _cleanup_free_
char *n
= NULL
;
366 val
= hashmap_get(rules
->known_groups
, name
);
368 *ret
= PTR_TO_GID(val
);
372 r
= get_group_creds(&name
, &gid
, USER_CREDS_ALLOW_MISSING
);
374 log_unknown_owner(NULL
, rules
, r
, "group", name
);
383 r
= hashmap_ensure_put(&rules
->known_groups
, &string_hash_ops
, n
, GID_TO_PTR(gid
));
392 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
396 return SUBST_TYPE_SUBSYS
;
397 if (strchr(str
, '%') || strchr(str
, '$'))
398 return SUBST_TYPE_FORMAT
;
399 return SUBST_TYPE_PLAIN
;
402 static void rule_line_append_token(UdevRuleLine
*rule_line
, UdevRuleToken
*token
) {
406 if (rule_line
->current_token
)
407 LIST_APPEND(tokens
, rule_line
->current_token
, token
);
409 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
411 rule_line
->current_token
= token
;
414 static int rule_line_add_token(UdevRuleLine
*rule_line
, UdevRuleTokenType type
, UdevRuleOperatorType op
, char *value
, void *data
) {
415 UdevRuleToken
*token
;
416 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
417 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
418 bool remove_trailing_whitespace
= false;
422 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
423 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
425 if (type
< _TK_M_MAX
) {
427 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
429 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
430 match_type
= MATCH_TYPE_SUBSYSTEM
;
431 else if (isempty(value
))
432 match_type
= MATCH_TYPE_EMPTY
;
433 else if (streq(value
, "?*")) {
434 /* Convert KEY=="?*" -> KEY!="" */
435 match_type
= MATCH_TYPE_EMPTY
;
436 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
437 } else if (string_is_glob(value
))
438 match_type
= MATCH_TYPE_GLOB
;
440 match_type
= MATCH_TYPE_PLAIN
;
442 if (type
< TK_M_TEST
|| type
== TK_M_RESULT
) {
443 /* Convert value string to nulstr. */
444 bool bar
= true, empty
= false;
447 for (a
= b
= value
; *a
!= '\0'; a
++) {
461 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
469 if (match_type
== MATCH_TYPE_GLOB
)
470 match_type
= MATCH_TYPE_GLOB_WITH_EMPTY
;
471 if (match_type
== MATCH_TYPE_PLAIN
)
472 match_type
= MATCH_TYPE_PLAIN_WITH_EMPTY
;
477 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
482 if (len
> 0 && !isspace(value
[len
- 1]))
483 remove_trailing_whitespace
= true;
485 subst_type
= rule_get_substitution_type(data
);
488 token
= new(UdevRuleToken
, 1);
492 *token
= (UdevRuleToken
) {
497 .match_type
= match_type
,
498 .attr_subst_type
= subst_type
,
499 .attr_match_remove_trailing_whitespace
= remove_trailing_whitespace
,
502 rule_line_append_token(rule_line
, token
);
504 if (token
->type
== TK_A_NAME
)
505 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
507 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
508 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
509 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
510 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
512 else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
)
513 SET_FLAG(rule_line
->type
, LINE_HAS_STATIC_NODE
, true);
515 else if (token
->type
>= _TK_A_MIN
||
516 IN_SET(token
->type
, TK_M_PROGRAM
,
517 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
518 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
519 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
524 static void check_value_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
, bool nonempty
) {
528 if (nonempty
&& isempty(value
))
529 log_token_invalid_value(rules
, key
, value
, (size_t) 0, "empty value");
530 else if (udev_check_format(value
, &offset
, &hint
) < 0)
531 log_token_invalid_value(rules
, key
, value
, offset
+ 1, hint
);
534 static int check_attr_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
) {
539 return log_token_invalid_attr(rules
, key
);
540 if (udev_check_format(value
, &offset
, &hint
) < 0)
541 log_token_invalid_attr_format(rules
, key
, value
, offset
+ 1, hint
);
545 static int parse_token(UdevRules
*rules
, const char *key
, char *attr
, UdevRuleOperatorType op
, char *value
) {
546 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
547 UdevRuleLine
*rule_line
;
551 assert(rules
->current_file
);
552 assert(rules
->current_file
->current_line
);
556 rule_line
= rules
->current_file
->current_line
;
558 if (streq(key
, "ACTION")) {
560 return log_token_invalid_attr(rules
, key
);
562 return log_token_invalid_op(rules
, key
);
564 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
);
565 } else if (streq(key
, "DEVPATH")) {
567 return log_token_invalid_attr(rules
, key
);
569 return log_token_invalid_op(rules
, key
);
571 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
);
572 } else if (streq(key
, "KERNEL")) {
574 return log_token_invalid_attr(rules
, key
);
576 return log_token_invalid_op(rules
, key
);
578 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
);
579 } else if (streq(key
, "SYMLINK")) {
581 return log_token_invalid_attr(rules
, key
);
583 return log_token_invalid_op(rules
, key
);
586 check_value_format_and_warn(rules
, key
, value
, false);
587 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
);
589 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
);
590 } else if (streq(key
, "NAME")) {
592 return log_token_invalid_attr(rules
, key
);
594 return log_token_invalid_op(rules
, key
);
596 log_token_warning(rules
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key
);
601 if (streq(value
, "%k"))
602 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
603 "Ignoring NAME=\"%%k\", as it will take no effect.");
605 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
606 "Ignoring NAME=\"\", as udev will not delete any network interfaces.");
607 check_value_format_and_warn(rules
, key
, value
, false);
609 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
);
611 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
);
612 } else if (streq(key
, "ENV")) {
614 return log_token_invalid_attr(rules
, key
);
616 return log_token_invalid_op(rules
, key
);
617 if (op
== OP_ASSIGN_FINAL
) {
618 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
624 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
625 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
626 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
627 "Invalid ENV attribute. '%s' cannot be set.", attr
);
629 check_value_format_and_warn(rules
, key
, value
, false);
631 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
);
633 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
);
634 } else if (streq(key
, "CONST")) {
635 if (isempty(attr
) || !STR_IN_SET(attr
, "arch", "virt"))
636 return log_token_invalid_attr(rules
, key
);
638 return log_token_invalid_op(rules
, key
);
639 r
= rule_line_add_token(rule_line
, TK_M_CONST
, op
, value
, attr
);
640 } else if (streq(key
, "TAG")) {
642 return log_token_invalid_attr(rules
, key
);
643 if (op
== OP_ASSIGN_FINAL
) {
644 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
649 check_value_format_and_warn(rules
, key
, value
, true);
651 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
);
653 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
);
654 } else if (streq(key
, "SUBSYSTEM")) {
656 return log_token_invalid_attr(rules
, key
);
658 return log_token_invalid_op(rules
, key
);
660 if (STR_IN_SET(value
, "bus", "class"))
661 log_token_warning(rules
, "\"%s\" must be specified as \"subsystem\".", value
);
663 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
664 } else if (streq(key
, "DRIVER")) {
666 return log_token_invalid_attr(rules
, key
);
668 return log_token_invalid_op(rules
, key
);
670 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
);
671 } else if (streq(key
, "ATTR")) {
672 r
= check_attr_format_and_warn(rules
, key
, attr
);
676 return log_token_invalid_op(rules
, key
);
677 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
678 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
683 check_value_format_and_warn(rules
, key
, value
, false);
684 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
);
686 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
);
687 } else if (streq(key
, "SYSCTL")) {
688 r
= check_attr_format_and_warn(rules
, key
, attr
);
692 return log_token_invalid_op(rules
, key
);
693 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
694 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
699 check_value_format_and_warn(rules
, key
, value
, false);
700 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
);
702 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
);
703 } else if (streq(key
, "KERNELS")) {
705 return log_token_invalid_attr(rules
, key
);
707 return log_token_invalid_op(rules
, key
);
709 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
);
710 } else if (streq(key
, "SUBSYSTEMS")) {
712 return log_token_invalid_attr(rules
, key
);
714 return log_token_invalid_op(rules
, key
);
716 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
);
717 } else if (streq(key
, "DRIVERS")) {
719 return log_token_invalid_attr(rules
, key
);
721 return log_token_invalid_op(rules
, key
);
723 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
);
724 } else if (streq(key
, "ATTRS")) {
725 r
= check_attr_format_and_warn(rules
, key
, attr
);
729 return log_token_invalid_op(rules
, key
);
731 if (startswith(attr
, "device/"))
732 log_token_warning(rules
, "'device' link may not be available in future kernels.");
733 if (strstr(attr
, "../"))
734 log_token_warning(rules
, "Direct reference to parent sysfs directory, may break in future kernels.");
736 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
);
737 } else if (streq(key
, "TAGS")) {
739 return log_token_invalid_attr(rules
, key
);
741 return log_token_invalid_op(rules
, key
);
743 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
);
744 } else if (streq(key
, "TEST")) {
745 mode_t mode
= MODE_INVALID
;
747 if (!isempty(attr
)) {
748 r
= parse_mode(attr
, &mode
);
750 return log_token_error_errno(rules
, r
, "Failed to parse mode '%s': %m", attr
);
752 check_value_format_and_warn(rules
, key
, value
, true);
754 return log_token_invalid_op(rules
, key
);
756 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
));
757 } else if (streq(key
, "PROGRAM")) {
759 return log_token_invalid_attr(rules
, key
);
760 check_value_format_and_warn(rules
, key
, value
, true);
762 return log_token_invalid_op(rules
, key
);
766 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
);
767 } else if (streq(key
, "IMPORT")) {
769 return log_token_invalid_attr(rules
, key
);
770 check_value_format_and_warn(rules
, key
, value
, true);
772 return log_token_invalid_op(rules
, key
);
776 if (streq(attr
, "file"))
777 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
778 else if (streq(attr
, "program")) {
779 UdevBuiltinCommand cmd
;
781 cmd
= udev_builtin_lookup(value
);
783 log_token_debug(rules
,"Found builtin command '%s' for %s, replacing attribute", value
, key
);
784 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
786 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
);
787 } else if (streq(attr
, "builtin")) {
788 UdevBuiltinCommand cmd
;
790 cmd
= udev_builtin_lookup(value
);
792 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
793 "Unknown builtin command: %s", value
);
794 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
795 } else if (streq(attr
, "db"))
796 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
);
797 else if (streq(attr
, "cmdline"))
798 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
799 else if (streq(attr
, "parent"))
800 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
802 return log_token_invalid_attr(rules
, key
);
803 } else if (streq(key
, "RESULT")) {
805 return log_token_invalid_attr(rules
, key
);
807 return log_token_invalid_op(rules
, key
);
809 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
);
810 } else if (streq(key
, "OPTIONS")) {
814 return log_token_invalid_attr(rules
, key
);
815 if (is_match
|| op
== OP_REMOVE
)
816 return log_token_invalid_op(rules
, key
);
820 if (streq(value
, "string_escape=none"))
821 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
822 else if (streq(value
, "string_escape=replace"))
823 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
824 else if (streq(value
, "db_persist"))
825 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
);
826 else if (streq(value
, "watch"))
827 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1));
828 else if (streq(value
, "nowatch"))
829 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0));
830 else if ((tmp
= startswith(value
, "static_node=")))
831 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
);
832 else if ((tmp
= startswith(value
, "link_priority="))) {
835 r
= safe_atoi(tmp
, &prio
);
837 return log_token_error_errno(rules
, r
, "Failed to parse link priority '%s': %m", tmp
);
838 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
));
839 } else if ((tmp
= startswith(value
, "log_level="))) {
842 if (streq(tmp
, "reset"))
845 level
= log_level_from_string(tmp
);
847 return log_token_error_errno(rules
, level
, "Failed to parse log level '%s': %m", tmp
);
849 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_LOG_LEVEL
, op
, NULL
, INT_TO_PTR(level
));
851 log_token_warning(rules
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
854 } else if (streq(key
, "OWNER")) {
858 return log_token_invalid_attr(rules
, key
);
859 if (is_match
|| op
== OP_REMOVE
)
860 return log_token_invalid_op(rules
, key
);
862 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
866 if (parse_uid(value
, &uid
) >= 0)
867 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
868 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
869 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
870 r
= rule_resolve_user(rules
, value
, &uid
);
872 return log_token_error_errno(rules
, r
, "Failed to resolve user name '%s': %m", value
);
874 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
875 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
876 check_value_format_and_warn(rules
, key
, value
, true);
877 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
);
879 log_token_debug(rules
, "User name resolution is disabled, ignoring %s=%s", key
, value
);
882 } else if (streq(key
, "GROUP")) {
886 return log_token_invalid_attr(rules
, key
);
887 if (is_match
|| op
== OP_REMOVE
)
888 return log_token_invalid_op(rules
, key
);
890 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
894 if (parse_gid(value
, &gid
) >= 0)
895 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
896 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
897 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
898 r
= rule_resolve_group(rules
, value
, &gid
);
900 return log_token_error_errno(rules
, r
, "Failed to resolve group name '%s': %m", value
);
902 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
903 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
904 check_value_format_and_warn(rules
, key
, value
, true);
905 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
);
907 log_token_debug(rules
, "Resolving group name is disabled, ignoring GROUP=\"%s\"", value
);
910 } else if (streq(key
, "MODE")) {
914 return log_token_invalid_attr(rules
, key
);
915 if (is_match
|| op
== OP_REMOVE
)
916 return log_token_invalid_op(rules
, key
);
918 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
922 if (parse_mode(value
, &mode
) >= 0)
923 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
));
925 check_value_format_and_warn(rules
, key
, value
, true);
926 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
);
928 } else if (streq(key
, "SECLABEL")) {
930 return log_token_invalid_attr(rules
, key
);
931 check_value_format_and_warn(rules
, key
, value
, true);
932 if (is_match
|| op
== OP_REMOVE
)
933 return log_token_invalid_op(rules
, key
);
934 if (op
== OP_ASSIGN_FINAL
) {
935 log_token_warning(rules
, "%s key takes '=' or '+=' operator, assuming '='.", key
);
939 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, attr
);
940 } else if (streq(key
, "RUN")) {
941 if (is_match
|| op
== OP_REMOVE
)
942 return log_token_invalid_op(rules
, key
);
943 check_value_format_and_warn(rules
, key
, value
, true);
944 if (!attr
|| streq(attr
, "program"))
945 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
);
946 else if (streq(attr
, "builtin")) {
947 UdevBuiltinCommand cmd
;
949 cmd
= udev_builtin_lookup(value
);
951 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
952 "Unknown builtin command '%s', ignoring", value
);
953 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
955 return log_token_invalid_attr(rules
, key
);
956 } else if (streq(key
, "GOTO")) {
958 return log_token_invalid_attr(rules
, key
);
960 return log_token_invalid_op(rules
, key
);
961 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
962 log_token_warning(rules
, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value
);
966 rule_line
->goto_label
= value
;
967 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
969 } else if (streq(key
, "LABEL")) {
971 return log_token_invalid_attr(rules
, key
);
973 return log_token_invalid_op(rules
, key
);
975 rule_line
->label
= value
;
976 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
979 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'", key
);
986 static UdevRuleOperatorType
parse_operator(const char *op
) {
989 if (startswith(op
, "=="))
991 if (startswith(op
, "!="))
993 if (startswith(op
, "+="))
995 if (startswith(op
, "-="))
997 if (startswith(op
, "="))
999 if (startswith(op
, ":="))
1000 return OP_ASSIGN_FINAL
;
1002 return _OP_TYPE_INVALID
;
1005 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
) {
1006 char *key_begin
, *key_end
, *attr
, *tmp
;
1007 UdevRuleOperatorType op
;
1016 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
1018 if (isempty(key_begin
))
1021 for (key_end
= key_begin
; ; key_end
++) {
1022 if (key_end
[0] == '\0')
1024 if (strchr(WHITESPACE
"={", key_end
[0]))
1026 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
1029 if (key_end
[0] == '{') {
1031 tmp
= strchr(attr
, '}');
1040 tmp
= skip_leading_chars(tmp
, NULL
);
1041 op
= parse_operator(tmp
);
1047 tmp
+= op
== OP_ASSIGN
? 1 : 2;
1048 tmp
= skip_leading_chars(tmp
, NULL
);
1049 r
= udev_rule_parse_value(tmp
, ret_value
, line
);
1053 *ret_key
= key_begin
;
1059 static void sort_tokens(UdevRuleLine
*rule_line
) {
1062 UdevRuleToken
*old_tokens
= TAKE_PTR(rule_line
->tokens
);
1063 rule_line
->current_token
= NULL
;
1065 while (old_tokens
) {
1066 UdevRuleToken
*min_token
= NULL
;
1068 LIST_FOREACH(tokens
, t
, old_tokens
)
1069 if (!min_token
|| min_token
->type
> t
->type
)
1072 LIST_REMOVE(tokens
, old_tokens
, min_token
);
1073 rule_line_append_token(rule_line
, min_token
);
1077 static int rule_add_line(UdevRules
*rules
, const char *line_str
, unsigned line_nr
) {
1078 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
1079 _cleanup_free_
char *line
= NULL
;
1080 UdevRuleFile
*rule_file
;
1085 assert(rules
->current_file
);
1088 rule_file
= rules
->current_file
;
1090 if (isempty(line_str
))
1093 /* We use memdup_suffix0() here, since we want to add a second NUL byte to the end, since possibly
1094 * some parsers might turn this into a "nulstr", which requires an extra NUL at the end. */
1095 line
= memdup_suffix0(line_str
, strlen(line_str
) + 1);
1099 rule_line
= new(UdevRuleLine
, 1);
1103 *rule_line
= (UdevRuleLine
) {
1104 .line
= TAKE_PTR(line
),
1105 .line_number
= line_nr
,
1106 .rule_file
= rule_file
,
1109 if (rule_file
->current_line
)
1110 LIST_APPEND(rule_lines
, rule_file
->current_line
, rule_line
);
1112 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
1114 rule_file
->current_line
= rule_line
;
1116 for (p
= rule_line
->line
; !isempty(p
); ) {
1117 char *key
, *attr
, *value
;
1118 UdevRuleOperatorType op
;
1120 r
= parse_line(&p
, &key
, &attr
, &op
, &value
);
1122 return log_token_error_errno(rules
, r
, "Invalid key/value pair, ignoring.");
1126 r
= parse_token(rules
, key
, attr
, op
, value
);
1131 if (rule_line
->type
== 0) {
1132 log_token_warning(rules
, "The line takes no effect, ignoring.");
1136 sort_tokens(rule_line
);
1137 TAKE_PTR(rule_line
);
1141 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
1144 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1145 LIST_FOREACH(rule_lines
, line
, rule_file
->rule_lines
) {
1146 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
1149 LIST_FOREACH(rule_lines
, i
, line
->rule_lines_next
)
1150 if (streq_ptr(i
->label
, line
->goto_label
)) {
1151 line
->goto_line
= i
;
1155 if (!line
->goto_line
) {
1156 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
1157 rule_file
->filename
, line
->line_number
, line
->goto_label
);
1159 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
1160 line
->goto_label
= NULL
;
1162 if ((line
->type
& ~LINE_HAS_LABEL
) == 0) {
1163 log_notice("%s:%u: The line takes no effect any more, dropping",
1164 rule_file
->filename
, line
->line_number
);
1165 if (line
->type
== LINE_HAS_LABEL
)
1166 udev_rule_line_clear_tokens(line
);
1168 udev_rule_line_free(line
);
1174 int udev_rules_parse_file(UdevRules
*rules
, const char *filename
) {
1175 _cleanup_free_
char *continuation
= NULL
, *name
= NULL
;
1176 _cleanup_fclose_
FILE *f
= NULL
;
1177 UdevRuleFile
*rule_file
;
1178 bool ignore_line
= false;
1179 unsigned line_nr
= 0;
1183 f
= fopen(filename
, "re");
1185 if (errno
== ENOENT
)
1188 return log_warning_errno(errno
, "Failed to open %s, ignoring: %m", filename
);
1191 if (fstat(fileno(f
), &st
) < 0)
1192 return log_warning_errno(errno
, "Failed to stat %s, ignoring: %m", filename
);
1194 if (null_or_empty(&st
)) {
1195 log_debug("Skipping empty file: %s", filename
);
1199 r
= hashmap_put_stats_by_path(&rules
->stats_by_path
, filename
, &st
);
1201 return log_warning_errno(errno
, "Failed to save stat for %s, ignoring: %m", filename
);
1203 (void) fd_warn_permissions(filename
, fileno(f
));
1205 log_debug("Reading rules file: %s", filename
);
1207 name
= strdup(filename
);
1211 rule_file
= new(UdevRuleFile
, 1);
1215 *rule_file
= (UdevRuleFile
) {
1216 .filename
= TAKE_PTR(name
),
1219 if (rules
->current_file
)
1220 LIST_APPEND(rule_files
, rules
->current_file
, rule_file
);
1222 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1224 rules
->current_file
= rule_file
;
1227 _cleanup_free_
char *buf
= NULL
;
1231 r
= read_line(f
, UDEV_LINE_SIZE
, &buf
);
1238 line
= skip_leading_chars(buf
, NULL
);
1245 if (continuation
&& !ignore_line
) {
1246 if (strlen(continuation
) + len
>= UDEV_LINE_SIZE
)
1249 if (!strextend(&continuation
, line
))
1253 line
= continuation
;
1258 if (len
> 0 && line
[len
- 1] == '\\') {
1262 line
[len
- 1] = '\0';
1263 if (!continuation
) {
1264 continuation
= strdup(line
);
1273 log_error("%s:%u: Line is too long, ignored", filename
, line_nr
);
1275 (void) rule_add_line(rules
, line
, line_nr
);
1277 continuation
= mfree(continuation
);
1278 ignore_line
= false;
1281 rule_resolve_goto(rule_file
);
1285 UdevRules
* udev_rules_new(ResolveNameTiming resolve_name_timing
) {
1286 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1288 UdevRules
*rules
= new(UdevRules
, 1);
1292 *rules
= (UdevRules
) {
1293 .resolve_name_timing
= resolve_name_timing
,
1299 int udev_rules_load(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1300 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1301 _cleanup_strv_free_
char **files
= NULL
;
1304 rules
= udev_rules_new(resolve_name_timing
);
1308 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1310 return log_debug_errno(r
, "Failed to enumerate rules files: %m");
1312 STRV_FOREACH(f
, files
) {
1313 r
= udev_rules_parse_file(rules
, *f
);
1315 log_debug_errno(r
, "Failed to read rules file %s, ignoring: %m", *f
);
1318 *ret_rules
= TAKE_PTR(rules
);
1322 bool udev_rules_should_reload(UdevRules
*rules
) {
1323 _cleanup_hashmap_free_ Hashmap
*stats_by_path
= NULL
;
1329 r
= config_get_stats_by_path(".rules", NULL
, 0, RULES_DIRS
, /* check_dropins = */ false, &stats_by_path
);
1331 log_warning_errno(r
, "Failed to get stats of udev rules, ignoring: %m");
1335 if (!stats_by_path_equal(rules
->stats_by_path
, stats_by_path
)) {
1336 log_debug("Udev rules need reloading");
1343 static bool token_match_string(UdevRuleToken
*token
, const char *str
) {
1344 const char *i
, *value
;
1348 assert(token
->value
);
1349 assert(token
->type
< _TK_M_MAX
);
1351 str
= strempty(str
);
1352 value
= token
->value
;
1354 switch (token
->match_type
) {
1355 case MATCH_TYPE_EMPTY
:
1356 match
= isempty(str
);
1358 case MATCH_TYPE_SUBSYSTEM
:
1359 match
= STR_IN_SET(str
, "subsystem", "class", "bus");
1361 case MATCH_TYPE_PLAIN_WITH_EMPTY
:
1367 case MATCH_TYPE_PLAIN
:
1368 NULSTR_FOREACH(i
, value
)
1369 if (streq(i
, str
)) {
1374 case MATCH_TYPE_GLOB_WITH_EMPTY
:
1380 case MATCH_TYPE_GLOB
:
1381 NULSTR_FOREACH(i
, value
)
1382 if ((fnmatch(i
, str
, 0) == 0)) {
1388 assert_not_reached();
1391 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1394 static bool token_match_attr(UdevRules
*rules
, UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
1395 char nbuf
[UDEV_NAME_SIZE
], vbuf
[UDEV_NAME_SIZE
];
1396 const char *name
, *value
;
1401 assert(IN_SET(token
->type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
));
1407 switch (token
->attr_subst_type
) {
1408 case SUBST_TYPE_FORMAT
:
1409 (void) udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false, &truncated
);
1411 log_rule_debug(dev
, rules
,
1412 "The sysfs attribute name '%s' is truncated while substituting into '%s', "
1413 "assuming the %s key does not match.", nbuf
, name
,
1414 token
->type
== TK_M_ATTR
? "ATTR" : "ATTRS");
1420 case SUBST_TYPE_PLAIN
:
1421 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1424 case SUBST_TYPE_SUBSYS
:
1425 if (udev_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1430 assert_not_reached();
1433 /* remove trailing whitespace, if not asked to match for it */
1434 if (token
->attr_match_remove_trailing_whitespace
) {
1435 if (value
!= vbuf
) {
1436 strscpy(vbuf
, sizeof(vbuf
), value
);
1440 delete_trailing_chars(vbuf
, NULL
);
1443 return token_match_string(token
, value
);
1446 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
1455 key
= skip_leading_chars(line
, NULL
);
1457 /* comment or empty line */
1458 if (IN_SET(key
[0], '#', '\0')) {
1459 *ret_key
= *ret_value
= NULL
;
1463 /* split key/value */
1464 val
= strchr(key
, '=');
1469 key
= strstrip(key
);
1473 val
= strstrip(val
);
1478 if (IN_SET(val
[0], '"', '\'')) {
1480 if (len
== 1 || val
[len
-1] != val
[0])
1491 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
1492 const char *key
, *val
;
1499 r
= sd_device_get_parent(dev
, &parent
);
1505 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
1506 if (fnmatch(filter
, key
, 0) != 0)
1508 r
= device_add_property(dev
, key
, val
);
1516 static int attr_subst_subdir(char attr
[static UDEV_PATH_SIZE
]) {
1517 _cleanup_closedir_
DIR *dir
= NULL
;
1518 char buf
[UDEV_PATH_SIZE
], *p
;
1525 tail
= strstr(attr
, "/*/");
1529 len
= tail
- attr
+ 1; /* include slash at the end */
1530 tail
+= 2; /* include slash at the beginning */
1534 size
-= strnpcpy_full(&p
, size
, attr
, len
, &truncated
);
1542 FOREACH_DIRENT_ALL(de
, dir
, break) {
1543 if (de
->d_name
[0] == '.')
1546 strscpyl_full(p
, size
, &truncated
, de
->d_name
, tail
, NULL
);
1550 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
1560 static int udev_rule_apply_token_to_event(
1564 usec_t timeout_usec
,
1566 Hashmap
*properties_list
) {
1568 UdevRuleToken
*token
;
1575 /* This returns the following values:
1576 * 0 on the current token does not match the event,
1577 * 1 on the current token matches the event, and
1578 * negative errno on some critical errors. */
1580 token
= rules
->current_file
->current_line
->current_token
;
1582 switch (token
->type
) {
1584 sd_device_action_t a
;
1586 r
= sd_device_get_action(dev
, &a
);
1588 return log_rule_error_errno(dev
, rules
, r
, "Failed to get uevent action type: %m");
1590 return token_match_string(token
, device_action_to_string(a
));
1592 case TK_M_DEVPATH
: {
1595 r
= sd_device_get_devpath(dev
, &val
);
1597 return log_rule_error_errno(dev
, rules
, r
, "Failed to get devpath: %m");
1599 return token_match_string(token
, val
);
1602 case TK_M_PARENTS_KERNEL
: {
1605 r
= sd_device_get_sysname(dev
, &val
);
1607 return log_rule_error_errno(dev
, rules
, r
, "Failed to get sysname: %m");
1609 return token_match_string(token
, val
);
1611 case TK_M_DEVLINK
: {
1614 FOREACH_DEVICE_DEVLINK(dev
, val
)
1615 if (token_match_string(token
, strempty(startswith(val
, "/dev/"))))
1616 return token
->op
== OP_MATCH
;
1617 return token
->op
== OP_NOMATCH
;
1620 return token_match_string(token
, event
->name
);
1624 if (sd_device_get_property_value(dev
, token
->data
, &val
) < 0)
1625 val
= hashmap_get(properties_list
, token
->data
);
1627 return token_match_string(token
, val
);
1630 const char *val
, *k
= token
->data
;
1632 if (streq(k
, "arch"))
1633 val
= architecture_to_string(uname_architecture());
1634 else if (streq(k
, "virt"))
1635 val
= virtualization_to_string(detect_virtualization());
1637 assert_not_reached();
1638 return token_match_string(token
, val
);
1641 case TK_M_PARENTS_TAG
: {
1644 FOREACH_DEVICE_TAG(dev
, val
)
1645 if (token_match_string(token
, val
))
1646 return token
->op
== OP_MATCH
;
1647 return token
->op
== OP_NOMATCH
;
1649 case TK_M_SUBSYSTEM
:
1650 case TK_M_PARENTS_SUBSYSTEM
: {
1653 r
= sd_device_get_subsystem(dev
, &val
);
1657 return log_rule_error_errno(dev
, rules
, r
, "Failed to get subsystem: %m");
1659 return token_match_string(token
, val
);
1662 case TK_M_PARENTS_DRIVER
: {
1665 r
= sd_device_get_driver(dev
, &val
);
1669 return log_rule_error_errno(dev
, rules
, r
, "Failed to get driver: %m");
1671 return token_match_string(token
, val
);
1674 case TK_M_PARENTS_ATTR
:
1675 return token_match_attr(rules
, token
, dev
, event
);
1677 _cleanup_free_
char *value
= NULL
;
1678 char buf
[UDEV_PATH_SIZE
];
1681 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
1683 log_rule_debug(dev
, rules
, "The sysctl entry name '%s' is truncated while substituting into '%s', "
1684 "assuming the SYSCTL key does not match.", buf
, (const char*) token
->data
);
1688 r
= sysctl_read(sysctl_normalize(buf
), &value
);
1689 if (r
< 0 && r
!= -ENOENT
)
1690 return log_rule_error_errno(dev
, rules
, r
, "Failed to read sysctl '%s': %m", buf
);
1692 return token_match_string(token
, strstrip(value
));
1695 mode_t mode
= PTR_TO_MODE(token
->data
);
1696 char buf
[UDEV_PATH_SIZE
];
1697 struct stat statbuf
;
1698 bool match
, truncated
;
1700 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1702 log_rule_debug(dev
, rules
, "The file name '%s' is truncated while substituting into '%s', "
1703 "assuming the TEST key does not match", buf
, token
->value
);
1707 if (!path_is_absolute(buf
) &&
1708 udev_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
1709 char tmp
[UDEV_PATH_SIZE
];
1712 r
= sd_device_get_syspath(dev
, &val
);
1714 return log_rule_error_errno(dev
, rules
, r
, "Failed to get syspath: %m");
1716 strscpy_full(tmp
, sizeof(tmp
), buf
, &truncated
);
1718 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", tmp
, NULL
);
1723 r
= attr_subst_subdir(buf
);
1725 return token
->op
== OP_NOMATCH
;
1727 return log_rule_error_errno(dev
, rules
, r
, "Failed to test for the existence of '%s': %m", buf
);
1729 if (stat(buf
, &statbuf
) < 0)
1730 return token
->op
== OP_NOMATCH
;
1732 if (mode
== MODE_INVALID
)
1733 return token
->op
== OP_MATCH
;
1735 match
= (statbuf
.st_mode
& mode
) > 0;
1736 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1738 case TK_M_PROGRAM
: {
1739 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
1743 event
->program_result
= mfree(event
->program_result
);
1744 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1746 log_rule_debug(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
1747 "assuming the PROGRAM key does not match.", buf
, token
->value
);
1751 log_rule_debug(dev
, rules
, "Running PROGRAM '%s'", buf
);
1753 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof(result
), NULL
);
1756 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute \"%s\": %m", buf
);
1757 else /* returned value is positive when program fails */
1758 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error)", buf
, r
);
1759 return token
->op
== OP_NOMATCH
;
1762 delete_trailing_chars(result
, "\n");
1763 count
= udev_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1765 log_rule_debug(dev
, rules
, "Replaced %zu character(s) in result of \"%s\"",
1768 event
->program_result
= strdup(result
);
1769 return token
->op
== OP_MATCH
;
1771 case TK_M_IMPORT_FILE
: {
1772 _cleanup_fclose_
FILE *f
= NULL
;
1773 char buf
[UDEV_PATH_SIZE
];
1776 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1778 log_rule_debug(dev
, rules
, "The file name '%s' to be imported is truncated while substituting into '%s', "
1779 "assuming the IMPORT key does not match.", buf
, token
->value
);
1783 log_rule_debug(dev
, rules
, "Importing properties from '%s'", buf
);
1785 f
= fopen(buf
, "re");
1787 if (errno
!= ENOENT
)
1788 return log_rule_error_errno(dev
, rules
, errno
,
1789 "Failed to open '%s': %m", buf
);
1790 return token
->op
== OP_NOMATCH
;
1794 _cleanup_free_
char *line
= NULL
;
1797 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1799 log_rule_debug_errno(dev
, rules
, r
,
1800 "Failed to read '%s', ignoring: %m", buf
);
1801 return token
->op
== OP_NOMATCH
;
1806 r
= get_property_from_string(line
, &key
, &value
);
1808 log_rule_debug_errno(dev
, rules
, r
,
1809 "Failed to parse key and value from '%s', ignoring: %m",
1816 r
= device_add_property(dev
, key
, value
);
1818 return log_rule_error_errno(dev
, rules
, r
,
1819 "Failed to add property %s=%s: %m",
1823 return token
->op
== OP_MATCH
;
1825 case TK_M_IMPORT_PROGRAM
: {
1826 _cleanup_strv_free_
char **lines
= NULL
;
1827 char buf
[UDEV_LINE_SIZE
], result
[UDEV_LINE_SIZE
];
1830 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1832 log_rule_debug(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
1833 "assuming the IMPORT key does not match.", buf
, token
->value
);
1837 log_rule_debug(dev
, rules
, "Importing properties from results of '%s'", buf
);
1839 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof result
, &truncated
);
1842 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute '%s', ignoring: %m", buf
);
1843 else /* returned value is positive when program fails */
1844 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
1845 return token
->op
== OP_NOMATCH
;
1851 /* Drop the last line. */
1852 for (char *p
= PTR_SUB1(buf
+ strlen(buf
), buf
); p
; p
= PTR_SUB1(p
, buf
))
1853 if (strchr(NEWLINE
, *p
)) {
1860 r
= strv_split_newlines_full(&lines
, result
, EXTRACT_RETAIN_ESCAPE
);
1864 log_rule_warning_errno(dev
, rules
, r
,
1865 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf
);
1869 STRV_FOREACH(line
, lines
) {
1872 r
= get_property_from_string(*line
, &key
, &value
);
1874 log_rule_debug_errno(dev
, rules
, r
,
1875 "Failed to parse key and value from '%s', ignoring: %m",
1882 r
= device_add_property(dev
, key
, value
);
1884 return log_rule_error_errno(dev
, rules
, r
,
1885 "Failed to add property %s=%s: %m",
1889 return token
->op
== OP_MATCH
;
1891 case TK_M_IMPORT_BUILTIN
: {
1892 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
1893 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
1894 unsigned mask
= 1U << (int) cmd
;
1895 char buf
[UDEV_LINE_SIZE
];
1898 if (udev_builtin_run_once(cmd
)) {
1899 /* check if we ran already */
1900 if (event
->builtin_run
& mask
) {
1901 log_rule_debug(dev
, rules
, "Skipping builtin '%s' in IMPORT key",
1902 udev_builtin_name(cmd
));
1903 /* return the result from earlier run */
1904 return token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
);
1907 event
->builtin_run
|= mask
;
1910 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1912 log_rule_debug(dev
, rules
, "The builtin command '%s' is truncated while substituting into '%s', "
1913 "assuming the IMPORT key does not match", buf
, token
->value
);
1917 log_rule_debug(dev
, rules
, "Importing properties from results of builtin command '%s'", buf
);
1919 r
= udev_builtin_run(dev
, &event
->rtnl
, cmd
, buf
, false);
1921 /* remember failure */
1922 log_rule_debug_errno(dev
, rules
, r
, "Failed to run builtin '%s': %m", buf
);
1923 event
->builtin_ret
|= mask
;
1925 return token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
);
1927 case TK_M_IMPORT_DB
: {
1930 if (!event
->dev_db_clone
)
1931 return token
->op
== OP_NOMATCH
;
1932 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
1934 return token
->op
== OP_NOMATCH
;
1936 return log_rule_error_errno(dev
, rules
, r
,
1937 "Failed to get property '%s' from database: %m",
1940 r
= device_add_property(dev
, token
->value
, val
);
1942 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1944 return token
->op
== OP_MATCH
;
1946 case TK_M_IMPORT_CMDLINE
: {
1947 _cleanup_free_
char *value
= NULL
;
1949 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
|PROC_CMDLINE_IGNORE_EFI_OPTIONS
, &value
);
1951 return log_rule_error_errno(dev
, rules
, r
,
1952 "Failed to read '%s' option from /proc/cmdline: %m",
1955 return token
->op
== OP_NOMATCH
;
1957 r
= device_add_property(dev
, token
->value
, value
?: "1");
1959 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1960 token
->value
, value
?: "1");
1961 return token
->op
== OP_MATCH
;
1963 case TK_M_IMPORT_PARENT
: {
1964 char buf
[UDEV_PATH_SIZE
];
1967 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1969 log_rule_debug(dev
, rules
, "The property name '%s' is truncated while substituting into '%s', "
1970 "assuming the IMPORT key does not match.", buf
, token
->value
);
1974 r
= import_parent_into_properties(dev
, buf
);
1976 return log_rule_error_errno(dev
, rules
, r
,
1977 "Failed to import properties '%s' from parent: %m",
1979 return token
->op
== (r
> 0 ? OP_MATCH
: OP_NOMATCH
);
1982 return token_match_string(token
, event
->program_result
);
1983 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
1984 event
->esc
= ESCAPE_NONE
;
1986 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
1987 event
->esc
= ESCAPE_REPLACE
;
1989 case TK_A_OPTIONS_DB_PERSIST
:
1990 device_set_db_persist(dev
);
1992 case TK_A_OPTIONS_INOTIFY_WATCH
:
1993 if (event
->inotify_watch_final
)
1995 if (token
->op
== OP_ASSIGN_FINAL
)
1996 event
->inotify_watch_final
= true;
1998 event
->inotify_watch
= token
->data
;
2000 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
2001 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
2003 case TK_A_OPTIONS_LOG_LEVEL
: {
2004 int level
= PTR_TO_INT(token
->data
);
2007 level
= event
->default_log_level
;
2009 log_set_max_level(level
);
2011 if (level
== LOG_DEBUG
&& !event
->log_level_was_debug
) {
2012 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
2013 log_device_uevent(dev
, "The log level is changed to 'debug' while processing device");
2014 event
->log_level_was_debug
= true;
2020 char owner
[UDEV_NAME_SIZE
];
2021 const char *ow
= owner
;
2024 if (event
->owner_final
)
2026 if (token
->op
== OP_ASSIGN_FINAL
)
2027 event
->owner_final
= true;
2029 (void) udev_event_apply_format(event
, token
->value
, owner
, sizeof(owner
), false, &truncated
);
2031 log_rule_warning(dev
, rules
, "The user name '%s' is truncated while substituting into '%s', "
2032 "refusing to apply the OWNER key.", owner
, token
->value
);
2036 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2038 log_unknown_owner(dev
, rules
, r
, "user", owner
);
2040 log_rule_debug(dev
, rules
, "OWNER %s(%u)", owner
, event
->uid
);
2044 char group
[UDEV_NAME_SIZE
];
2045 const char *gr
= group
;
2048 if (event
->group_final
)
2050 if (token
->op
== OP_ASSIGN_FINAL
)
2051 event
->group_final
= true;
2053 (void) udev_event_apply_format(event
, token
->value
, group
, sizeof(group
), false, &truncated
);
2055 log_rule_warning(dev
, rules
, "The group name '%s' is truncated while substituting into '%s', "
2056 "refusing to apply the GROUP key.", group
, token
->value
);
2060 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2062 log_unknown_owner(dev
, rules
, r
, "group", group
);
2064 log_rule_debug(dev
, rules
, "GROUP %s(%u)", group
, event
->gid
);
2068 char mode_str
[UDEV_NAME_SIZE
];
2071 if (event
->mode_final
)
2073 if (token
->op
== OP_ASSIGN_FINAL
)
2074 event
->mode_final
= true;
2076 (void) udev_event_apply_format(event
, token
->value
, mode_str
, sizeof(mode_str
), false, &truncated
);
2078 log_rule_warning(dev
, rules
, "The mode '%s' is truncated while substituting into %s, "
2079 "refusing to apply the MODE key.", mode_str
, token
->value
);
2083 r
= parse_mode(mode_str
, &event
->mode
);
2085 log_rule_error_errno(dev
, rules
, r
, "Failed to parse mode '%s', ignoring: %m", mode_str
);
2087 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
2091 if (event
->owner_final
)
2093 if (token
->op
== OP_ASSIGN_FINAL
)
2094 event
->owner_final
= true;
2097 event
->uid
= PTR_TO_UID(token
->data
);
2098 log_rule_debug(dev
, rules
, "OWNER %u", event
->uid
);
2101 if (event
->group_final
)
2103 if (token
->op
== OP_ASSIGN_FINAL
)
2104 event
->group_final
= true;
2107 event
->gid
= PTR_TO_GID(token
->data
);
2108 log_rule_debug(dev
, rules
, "GROUP %u", event
->gid
);
2111 if (event
->mode_final
)
2113 if (token
->op
== OP_ASSIGN_FINAL
)
2114 event
->mode_final
= true;
2117 event
->mode
= PTR_TO_MODE(token
->data
);
2118 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
2120 case TK_A_SECLABEL
: {
2121 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2122 char label_str
[UDEV_LINE_SIZE
] = {};
2125 name
= strdup(token
->data
);
2129 (void) udev_event_apply_format(event
, token
->value
, label_str
, sizeof(label_str
), false, &truncated
);
2131 log_rule_warning(dev
, rules
, "The security label '%s' is truncated while substituting into '%s', "
2132 "refusing to apply the SECLABEL key.", label_str
, token
->value
);
2136 if (!isempty(label_str
))
2137 label
= strdup(label_str
);
2139 label
= strdup(token
->value
);
2143 if (token
->op
== OP_ASSIGN
)
2144 ordered_hashmap_clear_free_free(event
->seclabel_list
);
2146 r
= ordered_hashmap_ensure_put(&event
->seclabel_list
, NULL
, name
, label
);
2150 return log_rule_error_errno(dev
, rules
, r
, "Failed to store SECLABEL{%s}='%s': %m", name
, label
);
2152 log_rule_debug(dev
, rules
, "SECLABEL{%s}='%s'", name
, label
);
2159 const char *val
, *name
= token
->data
;
2160 char value_new
[UDEV_NAME_SIZE
], *p
= value_new
;
2161 size_t count
, l
= sizeof(value_new
);
2164 if (isempty(token
->value
)) {
2165 if (token
->op
== OP_ADD
)
2167 r
= device_add_property(dev
, name
, NULL
);
2169 return log_rule_error_errno(dev
, rules
, r
, "Failed to remove property '%s': %m", name
);
2173 if (token
->op
== OP_ADD
&&
2174 sd_device_get_property_value(dev
, name
, &val
) >= 0) {
2175 l
= strpcpyl_full(&p
, l
, &truncated
, val
, " ", NULL
);
2177 log_rule_warning(dev
, rules
, "The buffer for the property '%s' is full, "
2178 "refusing to append the new value '%s'.", name
, token
->value
);
2183 (void) udev_event_apply_format(event
, token
->value
, p
, l
, false, &truncated
);
2185 log_rule_warning(dev
, rules
, "The property value '%s' is truncated while substituting into '%s', "
2186 "refusing to add property '%s'.", p
, token
->value
, name
);
2190 if (event
->esc
== ESCAPE_REPLACE
) {
2191 count
= udev_replace_chars(p
, NULL
);
2193 log_rule_debug(dev
, rules
, "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"",
2194 count
, name
, token
->op
== OP_ADD
? "+" : "", token
->value
);
2197 r
= device_add_property(dev
, name
, value_new
);
2199 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m", name
, value_new
);
2203 char buf
[UDEV_PATH_SIZE
];
2206 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2208 log_rule_warning(dev
, rules
, "The tag name '%s' is truncated while substituting into '%s',"
2209 "refusing to %s the tag.", buf
, token
->value
,
2210 token
->op
== OP_REMOVE
? "remove" : "add");
2214 if (token
->op
== OP_ASSIGN
)
2215 device_cleanup_tags(dev
);
2217 if (buf
[strspn(buf
, ALPHANUMERICAL
"-_")] != '\0') {
2218 log_rule_error(dev
, rules
, "Invalid tag name '%s', ignoring", buf
);
2221 if (token
->op
== OP_REMOVE
)
2222 device_remove_tag(dev
, buf
);
2224 r
= device_add_tag(dev
, buf
, true);
2226 return log_rule_error_errno(dev
, rules
, r
, "Failed to add tag '%s': %m", buf
);
2231 char buf
[UDEV_PATH_SIZE
];
2235 if (event
->name_final
)
2237 if (token
->op
== OP_ASSIGN_FINAL
)
2238 event
->name_final
= true;
2240 if (sd_device_get_ifindex(dev
, NULL
) < 0) {
2241 log_rule_error(dev
, rules
,
2242 "Only network interfaces can be renamed, ignoring NAME=\"%s\".",
2247 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2249 log_rule_warning(dev
, rules
, "The network interface name '%s' is truncated while substituting into '%s', "
2250 "refusing to set the name.", buf
, token
->value
);
2254 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2255 if (naming_scheme_has(NAMING_REPLACE_STRICTLY
))
2256 count
= udev_replace_ifname(buf
);
2258 count
= udev_replace_chars(buf
, "/");
2260 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of NAME=\"%s\"",
2261 count
, token
->value
);
2263 r
= free_and_strdup_warn(&event
->name
, buf
);
2267 log_rule_debug(dev
, rules
, "NAME '%s'", event
->name
);
2270 case TK_A_DEVLINK
: {
2271 char buf
[UDEV_PATH_SIZE
], *p
;
2275 if (event
->devlink_final
)
2277 if (sd_device_get_devnum(dev
, NULL
) < 0)
2279 if (token
->op
== OP_ASSIGN_FINAL
)
2280 event
->devlink_final
= true;
2281 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2282 device_cleanup_devlinks(dev
);
2284 /* allow multiple symlinks separated by spaces */
2285 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), event
->esc
!= ESCAPE_NONE
, &truncated
);
2287 log_rule_warning(dev
, rules
, "The symbolic link path '%s' is truncated while substituting into '%s', "
2288 "refusing to add the device symbolic link.", buf
, token
->value
);
2292 if (event
->esc
== ESCAPE_UNSET
)
2293 count
= udev_replace_chars(buf
, "/ ");
2294 else if (event
->esc
== ESCAPE_REPLACE
)
2295 count
= udev_replace_chars(buf
, "/");
2299 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of SYMLINK=\"%s\"",
2300 count
, token
->value
);
2302 p
= skip_leading_chars(buf
, NULL
);
2303 while (!isempty(p
)) {
2304 char filename
[UDEV_PATH_SIZE
], *next
;
2306 next
= strchr(p
, ' ');
2309 next
= skip_leading_chars(next
, NULL
);
2312 strscpyl_full(filename
, sizeof(filename
), &truncated
, "/dev/", p
, NULL
);
2316 r
= device_add_devlink(dev
, filename
);
2318 return log_rule_error_errno(dev
, rules
, r
, "Failed to add devlink '%s': %m", filename
);
2320 log_rule_debug(dev
, rules
, "LINK '%s'", p
);
2326 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2327 const char *val
, *key_name
= token
->data
;
2330 if (udev_resolve_subsys_kernel(key_name
, buf
, sizeof(buf
), false) < 0 &&
2331 sd_device_get_syspath(dev
, &val
) >= 0) {
2332 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", key_name
, NULL
);
2334 log_rule_warning(dev
, rules
,
2335 "The path to the attribute '%s/%s' is too long, refusing to set the attribute.",
2341 r
= attr_subst_subdir(buf
);
2343 log_rule_error_errno(dev
, rules
, r
, "Could not find file matches '%s', ignoring: %m", buf
);
2346 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2348 log_rule_warning(dev
, rules
, "The attribute value '%s' is truncated while substituting into '%s', "
2349 "refusing to set the attribute '%s'", value
, token
->value
, buf
);
2353 log_rule_debug(dev
, rules
, "ATTR '%s' writing '%s'", buf
, value
);
2354 r
= write_string_file(buf
, value
,
2355 WRITE_STRING_FILE_VERIFY_ON_FAILURE
|
2356 WRITE_STRING_FILE_DISABLE_BUFFER
|
2357 WRITE_STRING_FILE_AVOID_NEWLINE
|
2358 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE
);
2360 log_rule_error_errno(dev
, rules
, r
, "Failed to write ATTR{%s}, ignoring: %m", buf
);
2364 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2367 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
2369 log_rule_warning(dev
, rules
, "The sysctl entry name '%s' is truncated while substituting into '%s', "
2370 "refusing to set the sysctl entry.", buf
, (const char*) token
->data
);
2374 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2376 log_rule_warning(dev
, rules
, "The sysctl value '%s' is truncated while substituting into '%s', "
2377 "refusing to set the sysctl entry '%s'", value
, token
->value
, buf
);
2381 sysctl_normalize(buf
);
2382 log_rule_debug(dev
, rules
, "SYSCTL '%s' writing '%s'", buf
, value
);
2383 r
= sysctl_write(buf
, value
);
2385 log_rule_error_errno(dev
, rules
, r
, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf
, value
);
2388 case TK_A_RUN_BUILTIN
:
2389 case TK_A_RUN_PROGRAM
: {
2390 _cleanup_free_
char *cmd
= NULL
;
2391 char buf
[UDEV_LINE_SIZE
];
2394 if (event
->run_final
)
2396 if (token
->op
== OP_ASSIGN_FINAL
)
2397 event
->run_final
= true;
2399 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2400 ordered_hashmap_clear_free_key(event
->run_list
);
2402 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2404 log_rule_warning(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
2405 "refusing to invoke the command.", buf
, token
->value
);
2413 r
= ordered_hashmap_ensure_put(&event
->run_list
, NULL
, cmd
, token
->data
);
2417 return log_rule_error_errno(dev
, rules
, r
, "Failed to store command '%s': %m", cmd
);
2421 log_rule_debug(dev
, rules
, "RUN '%s'", token
->value
);
2424 case TK_A_OPTIONS_STATIC_NODE
:
2425 /* do nothing for events. */
2428 assert_not_reached();
2434 static bool token_is_for_parents(UdevRuleToken
*token
) {
2435 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
2438 static int udev_rule_apply_parent_token_to_event(
2441 int timeout_signal
) {
2444 UdevRuleToken
*head
;
2448 assert(rules
->current_file
);
2451 line
= ASSERT_PTR(rules
->current_file
->current_line
);
2452 head
= ASSERT_PTR(rules
->current_file
->current_line
->current_token
);
2453 event
->dev_parent
= ASSERT_PTR(event
->dev
);
2456 LIST_FOREACH(tokens
, token
, head
) {
2457 if (!token_is_for_parents(token
))
2458 return true; /* All parent tokens match. */
2460 line
->current_token
= token
;
2461 r
= udev_rule_apply_token_to_event(rules
, event
->dev_parent
, event
, 0, timeout_signal
, NULL
);
2468 /* All parent tokens match, and no more token (except for GOTO) in the line. */
2471 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
2472 event
->dev_parent
= NULL
;
2478 static int udev_rule_apply_line_to_event(
2481 usec_t timeout_usec
,
2483 Hashmap
*properties_list
,
2484 UdevRuleLine
**next_line
) {
2486 UdevRuleLine
*line
= rules
->current_file
->current_line
;
2487 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
2488 bool parents_done
= false;
2489 sd_device_action_t action
;
2492 r
= sd_device_get_action(event
->dev
, &action
);
2496 if (action
!= SD_DEVICE_REMOVE
) {
2497 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
2498 mask
|= LINE_HAS_DEVLINK
;
2500 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
2501 mask
|= LINE_HAS_NAME
;
2504 if ((line
->type
& mask
) == 0)
2507 event
->esc
= ESCAPE_UNSET
;
2509 DEVICE_TRACE_POINT(rules_apply_line
, event
->dev
, line
->rule_file
->filename
, line
->line_number
);
2511 LIST_FOREACH(tokens
, token
, line
->tokens
) {
2512 line
->current_token
= token
;
2514 if (token_is_for_parents(token
)) {
2518 r
= udev_rule_apply_parent_token_to_event(rules
, event
, timeout_signal
);
2522 parents_done
= true;
2526 r
= udev_rule_apply_token_to_event(rules
, event
->dev
, event
, timeout_usec
, timeout_signal
, properties_list
);
2531 if (line
->goto_line
)
2532 *next_line
= line
->goto_line
;
2537 int udev_rules_apply_to_event(
2540 usec_t timeout_usec
,
2542 Hashmap
*properties_list
) {
2549 LIST_FOREACH(rule_files
, file
, rules
->rule_files
) {
2550 rules
->current_file
= file
;
2551 LIST_FOREACH_WITH_NEXT(rule_lines
, line
, next_line
, file
->rule_lines
) {
2552 file
->current_line
= line
;
2553 r
= udev_rule_apply_line_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
, &next_line
);
2562 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
2563 _cleanup_strv_free_
char **tags
= NULL
;
2564 uid_t uid
= UID_INVALID
;
2565 gid_t gid
= GID_INVALID
;
2566 mode_t mode
= MODE_INVALID
;
2571 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
2574 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
2575 if (token
->type
== TK_A_OWNER_ID
)
2576 uid
= PTR_TO_UID(token
->data
);
2577 else if (token
->type
== TK_A_GROUP_ID
)
2578 gid
= PTR_TO_GID(token
->data
);
2579 else if (token
->type
== TK_A_MODE_ID
)
2580 mode
= PTR_TO_MODE(token
->data
);
2581 else if (token
->type
== TK_A_TAG
) {
2582 r
= strv_extend(&tags
, token
->value
);
2585 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
2586 r
= static_node_apply_permissions(token
->value
, mode
, uid
, gid
, tags
);
2594 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2599 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2600 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
2601 r
= udev_rule_line_apply_static_dev_perms(line
);