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"
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-netlink.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
) {
271 UdevRuleToken
*i
, *next
;
275 LIST_FOREACH_SAFE(tokens
, i
, next
, rule_line
->tokens
)
276 udev_rule_token_free(i
);
278 rule_line
->tokens
= NULL
;
281 static UdevRuleLine
* udev_rule_line_free(UdevRuleLine
*rule_line
) {
285 udev_rule_line_clear_tokens(rule_line
);
287 if (rule_line
->rule_file
) {
288 if (rule_line
->rule_file
->current_line
== rule_line
)
289 rule_line
->rule_file
->current_line
= rule_line
->rule_lines_prev
;
291 LIST_REMOVE(rule_lines
, rule_line
->rule_file
->rule_lines
, rule_line
);
294 free(rule_line
->line
);
295 return mfree(rule_line
);
298 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine
*, udev_rule_line_free
);
300 static void udev_rule_file_free(UdevRuleFile
*rule_file
) {
301 UdevRuleLine
*i
, *next
;
306 LIST_FOREACH_SAFE(rule_lines
, i
, next
, rule_file
->rule_lines
)
307 udev_rule_line_free(i
);
309 free(rule_file
->filename
);
313 UdevRules
*udev_rules_free(UdevRules
*rules
) {
314 UdevRuleFile
*i
, *next
;
319 LIST_FOREACH_SAFE(rule_files
, i
, next
, rules
->rule_files
)
320 udev_rule_file_free(i
);
322 hashmap_free_free_key(rules
->known_users
);
323 hashmap_free_free_key(rules
->known_groups
);
327 static int rule_resolve_user(UdevRules
*rules
, const char *name
, uid_t
*ret
) {
328 _cleanup_free_
char *n
= NULL
;
336 val
= hashmap_get(rules
->known_users
, name
);
338 *ret
= PTR_TO_UID(val
);
342 r
= get_user_creds(&name
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
344 log_unknown_owner(NULL
, rules
, r
, "user", name
);
353 r
= hashmap_ensure_put(&rules
->known_users
, &string_hash_ops
, n
, UID_TO_PTR(uid
));
362 static int rule_resolve_group(UdevRules
*rules
, const char *name
, gid_t
*ret
) {
363 _cleanup_free_
char *n
= NULL
;
371 val
= hashmap_get(rules
->known_groups
, name
);
373 *ret
= PTR_TO_GID(val
);
377 r
= get_group_creds(&name
, &gid
, USER_CREDS_ALLOW_MISSING
);
379 log_unknown_owner(NULL
, rules
, r
, "group", name
);
388 r
= hashmap_ensure_put(&rules
->known_groups
, &string_hash_ops
, n
, GID_TO_PTR(gid
));
397 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
401 return SUBST_TYPE_SUBSYS
;
402 if (strchr(str
, '%') || strchr(str
, '$'))
403 return SUBST_TYPE_FORMAT
;
404 return SUBST_TYPE_PLAIN
;
407 static void rule_line_append_token(UdevRuleLine
*rule_line
, UdevRuleToken
*token
) {
411 if (rule_line
->current_token
)
412 LIST_APPEND(tokens
, rule_line
->current_token
, token
);
414 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
416 rule_line
->current_token
= token
;
419 static int rule_line_add_token(UdevRuleLine
*rule_line
, UdevRuleTokenType type
, UdevRuleOperatorType op
, char *value
, void *data
) {
420 UdevRuleToken
*token
;
421 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
422 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
423 bool remove_trailing_whitespace
= false;
427 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
428 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
430 if (type
< _TK_M_MAX
) {
432 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
434 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
435 match_type
= MATCH_TYPE_SUBSYSTEM
;
436 else if (isempty(value
))
437 match_type
= MATCH_TYPE_EMPTY
;
438 else if (streq(value
, "?*")) {
439 /* Convert KEY=="?*" -> KEY!="" */
440 match_type
= MATCH_TYPE_EMPTY
;
441 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
442 } else if (string_is_glob(value
))
443 match_type
= MATCH_TYPE_GLOB
;
445 match_type
= MATCH_TYPE_PLAIN
;
447 if (type
< TK_M_TEST
|| type
== TK_M_RESULT
) {
448 /* Convert value string to nulstr. */
449 bool bar
= true, empty
= false;
452 for (a
= b
= value
; *a
!= '\0'; a
++) {
466 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
474 if (match_type
== MATCH_TYPE_GLOB
)
475 match_type
= MATCH_TYPE_GLOB_WITH_EMPTY
;
476 if (match_type
== MATCH_TYPE_PLAIN
)
477 match_type
= MATCH_TYPE_PLAIN_WITH_EMPTY
;
482 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
487 if (len
> 0 && !isspace(value
[len
- 1]))
488 remove_trailing_whitespace
= true;
490 subst_type
= rule_get_substitution_type(data
);
493 token
= new(UdevRuleToken
, 1);
497 *token
= (UdevRuleToken
) {
502 .match_type
= match_type
,
503 .attr_subst_type
= subst_type
,
504 .attr_match_remove_trailing_whitespace
= remove_trailing_whitespace
,
507 rule_line_append_token(rule_line
, token
);
509 if (token
->type
== TK_A_NAME
)
510 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
512 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
513 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
514 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
515 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
517 else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
)
518 SET_FLAG(rule_line
->type
, LINE_HAS_STATIC_NODE
, true);
520 else if (token
->type
>= _TK_A_MIN
||
521 IN_SET(token
->type
, TK_M_PROGRAM
,
522 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
523 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
524 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
529 static void check_value_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
, bool nonempty
) {
533 if (nonempty
&& isempty(value
))
534 log_token_invalid_value(rules
, key
, value
, (size_t) 0, "empty value");
535 else if (udev_check_format(value
, &offset
, &hint
) < 0)
536 log_token_invalid_value(rules
, key
, value
, offset
+ 1, hint
);
539 static int check_attr_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
) {
544 return log_token_invalid_attr(rules
, key
);
545 if (udev_check_format(value
, &offset
, &hint
) < 0)
546 log_token_invalid_attr_format(rules
, key
, value
, offset
+ 1, hint
);
550 static int parse_token(UdevRules
*rules
, const char *key
, char *attr
, UdevRuleOperatorType op
, char *value
) {
551 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
552 UdevRuleLine
*rule_line
;
556 assert(rules
->current_file
);
557 assert(rules
->current_file
->current_line
);
561 rule_line
= rules
->current_file
->current_line
;
563 if (streq(key
, "ACTION")) {
565 return log_token_invalid_attr(rules
, key
);
567 return log_token_invalid_op(rules
, key
);
569 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
);
570 } else if (streq(key
, "DEVPATH")) {
572 return log_token_invalid_attr(rules
, key
);
574 return log_token_invalid_op(rules
, key
);
576 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
);
577 } else if (streq(key
, "KERNEL")) {
579 return log_token_invalid_attr(rules
, key
);
581 return log_token_invalid_op(rules
, key
);
583 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
);
584 } else if (streq(key
, "SYMLINK")) {
586 return log_token_invalid_attr(rules
, key
);
588 return log_token_invalid_op(rules
, key
);
591 check_value_format_and_warn(rules
, key
, value
, false);
592 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
);
594 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
);
595 } else if (streq(key
, "NAME")) {
597 return log_token_invalid_attr(rules
, key
);
599 return log_token_invalid_op(rules
, key
);
601 log_token_warning(rules
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key
);
606 if (streq(value
, "%k"))
607 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
608 "Ignoring NAME=\"%%k\", as it will take no effect.");
610 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
611 "Ignoring NAME=\"\", as udev will not delete any network interfaces.");
612 check_value_format_and_warn(rules
, key
, value
, false);
614 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
);
616 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
);
617 } else if (streq(key
, "ENV")) {
619 return log_token_invalid_attr(rules
, key
);
621 return log_token_invalid_op(rules
, key
);
622 if (op
== OP_ASSIGN_FINAL
) {
623 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
629 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
630 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
631 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
632 "Invalid ENV attribute. '%s' cannot be set.", attr
);
634 check_value_format_and_warn(rules
, key
, value
, false);
636 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
);
638 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
);
639 } else if (streq(key
, "CONST")) {
640 if (isempty(attr
) || !STR_IN_SET(attr
, "arch", "virt"))
641 return log_token_invalid_attr(rules
, key
);
643 return log_token_invalid_op(rules
, key
);
644 r
= rule_line_add_token(rule_line
, TK_M_CONST
, op
, value
, attr
);
645 } else if (streq(key
, "TAG")) {
647 return log_token_invalid_attr(rules
, key
);
648 if (op
== OP_ASSIGN_FINAL
) {
649 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key
);
654 check_value_format_and_warn(rules
, key
, value
, true);
656 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
);
658 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
);
659 } else if (streq(key
, "SUBSYSTEM")) {
661 return log_token_invalid_attr(rules
, key
);
663 return log_token_invalid_op(rules
, key
);
665 if (STR_IN_SET(value
, "bus", "class"))
666 log_token_warning(rules
, "\"%s\" must be specified as \"subsystem\".", value
);
668 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
669 } else if (streq(key
, "DRIVER")) {
671 return log_token_invalid_attr(rules
, key
);
673 return log_token_invalid_op(rules
, key
);
675 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
);
676 } else if (streq(key
, "ATTR")) {
677 r
= check_attr_format_and_warn(rules
, key
, attr
);
681 return log_token_invalid_op(rules
, key
);
682 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
683 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
688 check_value_format_and_warn(rules
, key
, value
, false);
689 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
);
691 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
);
692 } else if (streq(key
, "SYSCTL")) {
693 r
= check_attr_format_and_warn(rules
, key
, attr
);
697 return log_token_invalid_op(rules
, key
);
698 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
699 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '='.", key
);
704 check_value_format_and_warn(rules
, key
, value
, false);
705 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
);
707 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
);
708 } else if (streq(key
, "KERNELS")) {
710 return log_token_invalid_attr(rules
, key
);
712 return log_token_invalid_op(rules
, key
);
714 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
);
715 } else if (streq(key
, "SUBSYSTEMS")) {
717 return log_token_invalid_attr(rules
, key
);
719 return log_token_invalid_op(rules
, key
);
721 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
);
722 } else if (streq(key
, "DRIVERS")) {
724 return log_token_invalid_attr(rules
, key
);
726 return log_token_invalid_op(rules
, key
);
728 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
);
729 } else if (streq(key
, "ATTRS")) {
730 r
= check_attr_format_and_warn(rules
, key
, attr
);
734 return log_token_invalid_op(rules
, key
);
736 if (startswith(attr
, "device/"))
737 log_token_warning(rules
, "'device' link may not be available in future kernels.");
738 if (strstr(attr
, "../"))
739 log_token_warning(rules
, "Direct reference to parent sysfs directory, may break in future kernels.");
741 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
);
742 } else if (streq(key
, "TAGS")) {
744 return log_token_invalid_attr(rules
, key
);
746 return log_token_invalid_op(rules
, key
);
748 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
);
749 } else if (streq(key
, "TEST")) {
750 mode_t mode
= MODE_INVALID
;
752 if (!isempty(attr
)) {
753 r
= parse_mode(attr
, &mode
);
755 return log_token_error_errno(rules
, r
, "Failed to parse mode '%s': %m", attr
);
757 check_value_format_and_warn(rules
, key
, value
, true);
759 return log_token_invalid_op(rules
, key
);
761 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
));
762 } else if (streq(key
, "PROGRAM")) {
764 return log_token_invalid_attr(rules
, key
);
765 check_value_format_and_warn(rules
, key
, value
, true);
767 return log_token_invalid_op(rules
, key
);
771 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
);
772 } else if (streq(key
, "IMPORT")) {
774 return log_token_invalid_attr(rules
, key
);
775 check_value_format_and_warn(rules
, key
, value
, true);
777 return log_token_invalid_op(rules
, key
);
781 if (streq(attr
, "file"))
782 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
783 else if (streq(attr
, "program")) {
784 UdevBuiltinCommand cmd
;
786 cmd
= udev_builtin_lookup(value
);
788 log_token_debug(rules
,"Found builtin command '%s' for %s, replacing attribute", value
, key
);
789 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
791 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
);
792 } else if (streq(attr
, "builtin")) {
793 UdevBuiltinCommand cmd
;
795 cmd
= udev_builtin_lookup(value
);
797 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
798 "Unknown builtin command: %s", value
);
799 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
800 } else if (streq(attr
, "db"))
801 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
);
802 else if (streq(attr
, "cmdline"))
803 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
804 else if (streq(attr
, "parent"))
805 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
807 return log_token_invalid_attr(rules
, key
);
808 } else if (streq(key
, "RESULT")) {
810 return log_token_invalid_attr(rules
, key
);
812 return log_token_invalid_op(rules
, key
);
814 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
);
815 } else if (streq(key
, "OPTIONS")) {
819 return log_token_invalid_attr(rules
, key
);
820 if (is_match
|| op
== OP_REMOVE
)
821 return log_token_invalid_op(rules
, key
);
825 if (streq(value
, "string_escape=none"))
826 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
827 else if (streq(value
, "string_escape=replace"))
828 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
829 else if (streq(value
, "db_persist"))
830 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
);
831 else if (streq(value
, "watch"))
832 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1));
833 else if (streq(value
, "nowatch"))
834 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0));
835 else if ((tmp
= startswith(value
, "static_node=")))
836 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
);
837 else if ((tmp
= startswith(value
, "link_priority="))) {
840 r
= safe_atoi(tmp
, &prio
);
842 return log_token_error_errno(rules
, r
, "Failed to parse link priority '%s': %m", tmp
);
843 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
));
844 } else if ((tmp
= startswith(value
, "log_level="))) {
847 if (streq(tmp
, "reset"))
850 level
= log_level_from_string(tmp
);
852 return log_token_error_errno(rules
, level
, "Failed to parse log level '%s': %m", tmp
);
854 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_LOG_LEVEL
, op
, NULL
, INT_TO_PTR(level
));
856 log_token_warning(rules
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
859 } else if (streq(key
, "OWNER")) {
863 return log_token_invalid_attr(rules
, key
);
864 if (is_match
|| op
== OP_REMOVE
)
865 return log_token_invalid_op(rules
, key
);
867 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
871 if (parse_uid(value
, &uid
) >= 0)
872 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
873 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
874 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
875 r
= rule_resolve_user(rules
, value
, &uid
);
877 return log_token_error_errno(rules
, r
, "Failed to resolve user name '%s': %m", value
);
879 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
880 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
881 check_value_format_and_warn(rules
, key
, value
, true);
882 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
);
884 log_token_debug(rules
, "User name resolution is disabled, ignoring %s=%s", key
, value
);
887 } else if (streq(key
, "GROUP")) {
891 return log_token_invalid_attr(rules
, key
);
892 if (is_match
|| op
== OP_REMOVE
)
893 return log_token_invalid_op(rules
, key
);
895 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
899 if (parse_gid(value
, &gid
) >= 0)
900 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
901 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
902 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
903 r
= rule_resolve_group(rules
, value
, &gid
);
905 return log_token_error_errno(rules
, r
, "Failed to resolve group name '%s': %m", value
);
907 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
908 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
909 check_value_format_and_warn(rules
, key
, value
, true);
910 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
);
912 log_token_debug(rules
, "Resolving group name is disabled, ignoring GROUP=\"%s\"", value
);
915 } else if (streq(key
, "MODE")) {
919 return log_token_invalid_attr(rules
, key
);
920 if (is_match
|| op
== OP_REMOVE
)
921 return log_token_invalid_op(rules
, key
);
923 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '='.", key
);
927 if (parse_mode(value
, &mode
) >= 0)
928 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
));
930 check_value_format_and_warn(rules
, key
, value
, true);
931 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
);
933 } else if (streq(key
, "SECLABEL")) {
935 return log_token_invalid_attr(rules
, key
);
936 check_value_format_and_warn(rules
, key
, value
, true);
937 if (is_match
|| op
== OP_REMOVE
)
938 return log_token_invalid_op(rules
, key
);
939 if (op
== OP_ASSIGN_FINAL
) {
940 log_token_warning(rules
, "%s key takes '=' or '+=' operator, assuming '='.", key
);
944 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, attr
);
945 } else if (streq(key
, "RUN")) {
946 if (is_match
|| op
== OP_REMOVE
)
947 return log_token_invalid_op(rules
, key
);
948 check_value_format_and_warn(rules
, key
, value
, true);
949 if (!attr
|| streq(attr
, "program"))
950 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
);
951 else if (streq(attr
, "builtin")) {
952 UdevBuiltinCommand cmd
;
954 cmd
= udev_builtin_lookup(value
);
956 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
957 "Unknown builtin command '%s', ignoring", value
);
958 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
960 return log_token_invalid_attr(rules
, key
);
961 } else if (streq(key
, "GOTO")) {
963 return log_token_invalid_attr(rules
, key
);
965 return log_token_invalid_op(rules
, key
);
966 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
967 log_token_warning(rules
, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value
);
971 rule_line
->goto_label
= value
;
972 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
974 } else if (streq(key
, "LABEL")) {
976 return log_token_invalid_attr(rules
, key
);
978 return log_token_invalid_op(rules
, key
);
980 rule_line
->label
= value
;
981 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
984 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'", key
);
991 static UdevRuleOperatorType
parse_operator(const char *op
) {
994 if (startswith(op
, "=="))
996 if (startswith(op
, "!="))
998 if (startswith(op
, "+="))
1000 if (startswith(op
, "-="))
1002 if (startswith(op
, "="))
1004 if (startswith(op
, ":="))
1005 return OP_ASSIGN_FINAL
;
1007 return _OP_TYPE_INVALID
;
1010 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
) {
1011 char *key_begin
, *key_end
, *attr
, *tmp
;
1012 UdevRuleOperatorType op
;
1021 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
1023 if (isempty(key_begin
))
1026 for (key_end
= key_begin
; ; key_end
++) {
1027 if (key_end
[0] == '\0')
1029 if (strchr(WHITESPACE
"={", key_end
[0]))
1031 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
1034 if (key_end
[0] == '{') {
1036 tmp
= strchr(attr
, '}');
1045 tmp
= skip_leading_chars(tmp
, NULL
);
1046 op
= parse_operator(tmp
);
1052 tmp
+= op
== OP_ASSIGN
? 1 : 2;
1053 tmp
= skip_leading_chars(tmp
, NULL
);
1054 r
= udev_rule_parse_value(tmp
, ret_value
, line
);
1058 *ret_key
= key_begin
;
1064 static void sort_tokens(UdevRuleLine
*rule_line
) {
1065 UdevRuleToken
*head_old
;
1069 head_old
= TAKE_PTR(rule_line
->tokens
);
1070 rule_line
->current_token
= NULL
;
1072 while (!LIST_IS_EMPTY(head_old
)) {
1073 UdevRuleToken
*t
, *min_token
= NULL
;
1075 LIST_FOREACH(tokens
, t
, head_old
)
1076 if (!min_token
|| min_token
->type
> t
->type
)
1079 LIST_REMOVE(tokens
, head_old
, min_token
);
1080 rule_line_append_token(rule_line
, min_token
);
1084 static int rule_add_line(UdevRules
*rules
, const char *line_str
, unsigned line_nr
) {
1085 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
1086 _cleanup_free_
char *line
= NULL
;
1087 UdevRuleFile
*rule_file
;
1092 assert(rules
->current_file
);
1095 rule_file
= rules
->current_file
;
1097 if (isempty(line_str
))
1100 /* We use memdup_suffix0() here, since we want to add a second NUL byte to the end, since possibly
1101 * some parsers might turn this into a "nulstr", which requires an extra NUL at the end. */
1102 line
= memdup_suffix0(line_str
, strlen(line_str
) + 1);
1106 rule_line
= new(UdevRuleLine
, 1);
1110 *rule_line
= (UdevRuleLine
) {
1111 .line
= TAKE_PTR(line
),
1112 .line_number
= line_nr
,
1113 .rule_file
= rule_file
,
1116 if (rule_file
->current_line
)
1117 LIST_APPEND(rule_lines
, rule_file
->current_line
, rule_line
);
1119 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
1121 rule_file
->current_line
= rule_line
;
1123 for (p
= rule_line
->line
; !isempty(p
); ) {
1124 char *key
, *attr
, *value
;
1125 UdevRuleOperatorType op
;
1127 r
= parse_line(&p
, &key
, &attr
, &op
, &value
);
1129 return log_token_error_errno(rules
, r
, "Invalid key/value pair, ignoring.");
1133 r
= parse_token(rules
, key
, attr
, op
, value
);
1138 if (rule_line
->type
== 0) {
1139 log_token_warning(rules
, "The line takes no effect, ignoring.");
1143 sort_tokens(rule_line
);
1144 TAKE_PTR(rule_line
);
1148 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
1149 UdevRuleLine
*line
, *line_next
, *i
;
1153 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1154 LIST_FOREACH_SAFE(rule_lines
, line
, line_next
, rule_file
->rule_lines
) {
1155 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
1158 LIST_FOREACH(rule_lines
, i
, line
->rule_lines_next
)
1159 if (streq_ptr(i
->label
, line
->goto_label
)) {
1160 line
->goto_line
= i
;
1164 if (!line
->goto_line
) {
1165 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
1166 rule_file
->filename
, line
->line_number
, line
->goto_label
);
1168 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
1169 line
->goto_label
= NULL
;
1171 if ((line
->type
& ~LINE_HAS_LABEL
) == 0) {
1172 log_notice("%s:%u: The line takes no effect any more, dropping",
1173 rule_file
->filename
, line
->line_number
);
1174 if (line
->type
== LINE_HAS_LABEL
)
1175 udev_rule_line_clear_tokens(line
);
1177 udev_rule_line_free(line
);
1183 int udev_rules_parse_file(UdevRules
*rules
, const char *filename
) {
1184 _cleanup_free_
char *continuation
= NULL
, *name
= NULL
;
1185 _cleanup_fclose_
FILE *f
= NULL
;
1186 UdevRuleFile
*rule_file
;
1187 bool ignore_line
= false;
1188 unsigned line_nr
= 0;
1191 f
= fopen(filename
, "re");
1193 if (errno
== ENOENT
)
1199 (void) fd_warn_permissions(filename
, fileno(f
));
1201 if (null_or_empty_fd(fileno(f
))) {
1202 log_debug("Skipping empty file: %s", filename
);
1206 log_debug("Reading rules file: %s", filename
);
1208 name
= strdup(filename
);
1212 rule_file
= new(UdevRuleFile
, 1);
1216 *rule_file
= (UdevRuleFile
) {
1217 .filename
= TAKE_PTR(name
),
1220 if (rules
->current_file
)
1221 LIST_APPEND(rule_files
, rules
->current_file
, rule_file
);
1223 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1225 rules
->current_file
= rule_file
;
1228 _cleanup_free_
char *buf
= NULL
;
1232 r
= read_line(f
, UDEV_LINE_SIZE
, &buf
);
1239 line
= skip_leading_chars(buf
, NULL
);
1246 if (continuation
&& !ignore_line
) {
1247 if (strlen(continuation
) + len
>= UDEV_LINE_SIZE
)
1250 if (!strextend(&continuation
, line
))
1254 line
= continuation
;
1259 if (len
> 0 && line
[len
- 1] == '\\') {
1263 line
[len
- 1] = '\0';
1264 if (!continuation
) {
1265 continuation
= strdup(line
);
1274 log_error("%s:%u: Line is too long, ignored", filename
, line_nr
);
1276 (void) rule_add_line(rules
, line
, line_nr
);
1278 continuation
= mfree(continuation
);
1279 ignore_line
= false;
1282 rule_resolve_goto(rule_file
);
1286 UdevRules
* udev_rules_new(ResolveNameTiming resolve_name_timing
) {
1287 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1289 UdevRules
*rules
= new(UdevRules
, 1);
1293 *rules
= (UdevRules
) {
1294 .resolve_name_timing
= resolve_name_timing
,
1300 int udev_rules_load(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1301 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1302 _cleanup_strv_free_
char **files
= NULL
;
1306 rules
= udev_rules_new(resolve_name_timing
);
1310 (void) udev_rules_check_timestamp(rules
);
1312 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1314 return log_debug_errno(r
, "Failed to enumerate rules files: %m");
1316 STRV_FOREACH(f
, files
) {
1317 r
= udev_rules_parse_file(rules
, *f
);
1319 log_debug_errno(r
, "Failed to read rules file %s, ignoring: %m", *f
);
1322 *ret_rules
= TAKE_PTR(rules
);
1326 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1330 return paths_check_timestamp(RULES_DIRS
, &rules
->dirs_ts_usec
, true);
1333 static bool token_match_string(UdevRuleToken
*token
, const char *str
) {
1334 const char *i
, *value
;
1338 assert(token
->value
);
1339 assert(token
->type
< _TK_M_MAX
);
1341 str
= strempty(str
);
1342 value
= token
->value
;
1344 switch (token
->match_type
) {
1345 case MATCH_TYPE_EMPTY
:
1346 match
= isempty(str
);
1348 case MATCH_TYPE_SUBSYSTEM
:
1349 match
= STR_IN_SET(str
, "subsystem", "class", "bus");
1351 case MATCH_TYPE_PLAIN_WITH_EMPTY
:
1357 case MATCH_TYPE_PLAIN
:
1358 NULSTR_FOREACH(i
, value
)
1359 if (streq(i
, str
)) {
1364 case MATCH_TYPE_GLOB_WITH_EMPTY
:
1370 case MATCH_TYPE_GLOB
:
1371 NULSTR_FOREACH(i
, value
)
1372 if ((fnmatch(i
, str
, 0) == 0)) {
1378 assert_not_reached();
1381 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1384 static bool token_match_attr(UdevRules
*rules
, UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
1385 char nbuf
[UDEV_NAME_SIZE
], vbuf
[UDEV_NAME_SIZE
];
1386 const char *name
, *value
;
1391 assert(IN_SET(token
->type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
));
1397 switch (token
->attr_subst_type
) {
1398 case SUBST_TYPE_FORMAT
:
1399 (void) udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false, &truncated
);
1401 log_rule_debug(dev
, rules
,
1402 "The sysfs attribute name '%s' is truncated while substituting into '%s', "
1403 "assuming the %s key does not match.", nbuf
, name
,
1404 token
->type
== TK_M_ATTR
? "ATTR" : "ATTRS");
1410 case SUBST_TYPE_PLAIN
:
1411 if (device_get_sysattr_value_maybe_from_netlink(dev
, &event
->rtnl
, name
, &value
) < 0)
1414 case SUBST_TYPE_SUBSYS
:
1415 if (udev_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1420 assert_not_reached();
1423 /* remove trailing whitespace, if not asked to match for it */
1424 if (token
->attr_match_remove_trailing_whitespace
) {
1425 if (value
!= vbuf
) {
1426 strscpy(vbuf
, sizeof(vbuf
), value
);
1430 delete_trailing_chars(vbuf
, NULL
);
1433 return token_match_string(token
, value
);
1436 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
1445 key
= skip_leading_chars(line
, NULL
);
1447 /* comment or empty line */
1448 if (IN_SET(key
[0], '#', '\0')) {
1449 *ret_key
= *ret_value
= NULL
;
1453 /* split key/value */
1454 val
= strchr(key
, '=');
1459 key
= strstrip(key
);
1463 val
= strstrip(val
);
1468 if (IN_SET(val
[0], '"', '\'')) {
1470 if (len
== 1 || val
[len
-1] != val
[0])
1481 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
1482 const char *key
, *val
;
1489 r
= sd_device_get_parent(dev
, &parent
);
1495 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
1496 if (fnmatch(filter
, key
, 0) != 0)
1498 r
= device_add_property(dev
, key
, val
);
1506 static int attr_subst_subdir(char attr
[static UDEV_PATH_SIZE
]) {
1507 _cleanup_closedir_
DIR *dir
= NULL
;
1508 char buf
[UDEV_PATH_SIZE
], *p
;
1515 tail
= strstr(attr
, "/*/");
1519 len
= tail
- attr
+ 1; /* include slash at the end */
1520 tail
+= 2; /* include slash at the beginning */
1524 size
-= strnpcpy_full(&p
, size
, attr
, len
, &truncated
);
1532 FOREACH_DIRENT_ALL(de
, dir
, break) {
1533 if (de
->d_name
[0] == '.')
1536 strscpyl_full(p
, size
, &truncated
, de
->d_name
, tail
, NULL
);
1540 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
1550 static int udev_rule_apply_token_to_event(
1554 usec_t timeout_usec
,
1556 Hashmap
*properties_list
) {
1558 UdevRuleToken
*token
;
1565 /* This returns the following values:
1566 * 0 on the current token does not match the event,
1567 * 1 on the current token matches the event, and
1568 * negative errno on some critical errors. */
1570 token
= rules
->current_file
->current_line
->current_token
;
1572 switch (token
->type
) {
1574 sd_device_action_t a
;
1576 r
= sd_device_get_action(dev
, &a
);
1578 return log_rule_error_errno(dev
, rules
, r
, "Failed to get uevent action type: %m");
1580 return token_match_string(token
, device_action_to_string(a
));
1582 case TK_M_DEVPATH
: {
1585 r
= sd_device_get_devpath(dev
, &val
);
1587 return log_rule_error_errno(dev
, rules
, r
, "Failed to get devpath: %m");
1589 return token_match_string(token
, val
);
1592 case TK_M_PARENTS_KERNEL
: {
1595 r
= sd_device_get_sysname(dev
, &val
);
1597 return log_rule_error_errno(dev
, rules
, r
, "Failed to get sysname: %m");
1599 return token_match_string(token
, val
);
1601 case TK_M_DEVLINK
: {
1604 FOREACH_DEVICE_DEVLINK(dev
, val
)
1605 if (token_match_string(token
, strempty(startswith(val
, "/dev/"))))
1606 return token
->op
== OP_MATCH
;
1607 return token
->op
== OP_NOMATCH
;
1610 return token_match_string(token
, event
->name
);
1614 if (sd_device_get_property_value(dev
, token
->data
, &val
) < 0)
1615 val
= hashmap_get(properties_list
, token
->data
);
1617 return token_match_string(token
, val
);
1620 const char *val
, *k
= token
->data
;
1622 if (streq(k
, "arch"))
1623 val
= architecture_to_string(uname_architecture());
1624 else if (streq(k
, "virt"))
1625 val
= virtualization_to_string(detect_virtualization());
1627 assert_not_reached();
1628 return token_match_string(token
, val
);
1631 case TK_M_PARENTS_TAG
: {
1634 FOREACH_DEVICE_TAG(dev
, val
)
1635 if (token_match_string(token
, val
))
1636 return token
->op
== OP_MATCH
;
1637 return token
->op
== OP_NOMATCH
;
1639 case TK_M_SUBSYSTEM
:
1640 case TK_M_PARENTS_SUBSYSTEM
: {
1643 r
= sd_device_get_subsystem(dev
, &val
);
1647 return log_rule_error_errno(dev
, rules
, r
, "Failed to get subsystem: %m");
1649 return token_match_string(token
, val
);
1652 case TK_M_PARENTS_DRIVER
: {
1655 r
= sd_device_get_driver(dev
, &val
);
1659 return log_rule_error_errno(dev
, rules
, r
, "Failed to get driver: %m");
1661 return token_match_string(token
, val
);
1664 case TK_M_PARENTS_ATTR
:
1665 return token_match_attr(rules
, token
, dev
, event
);
1667 _cleanup_free_
char *value
= NULL
;
1668 char buf
[UDEV_PATH_SIZE
];
1671 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
1673 log_rule_debug(dev
, rules
, "The sysctl entry name '%s' is truncated while substituting into '%s', "
1674 "assuming the SYSCTL key does not match.", buf
, (const char*) token
->data
);
1678 r
= sysctl_read(sysctl_normalize(buf
), &value
);
1679 if (r
< 0 && r
!= -ENOENT
)
1680 return log_rule_error_errno(dev
, rules
, r
, "Failed to read sysctl '%s': %m", buf
);
1682 return token_match_string(token
, strstrip(value
));
1685 mode_t mode
= PTR_TO_MODE(token
->data
);
1686 char buf
[UDEV_PATH_SIZE
];
1687 struct stat statbuf
;
1688 bool match
, truncated
;
1690 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1692 log_rule_debug(dev
, rules
, "The file name '%s' is truncated while substituting into '%s', "
1693 "assuming the TEST key does not match", buf
, token
->value
);
1697 if (!path_is_absolute(buf
) &&
1698 udev_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
1699 char tmp
[UDEV_PATH_SIZE
];
1702 r
= sd_device_get_syspath(dev
, &val
);
1704 return log_rule_error_errno(dev
, rules
, r
, "Failed to get syspath: %m");
1706 strscpy_full(tmp
, sizeof(tmp
), buf
, &truncated
);
1708 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", tmp
, NULL
);
1713 r
= attr_subst_subdir(buf
);
1715 return token
->op
== OP_NOMATCH
;
1717 return log_rule_error_errno(dev
, rules
, r
, "Failed to test for the existence of '%s': %m", buf
);
1719 if (stat(buf
, &statbuf
) < 0)
1720 return token
->op
== OP_NOMATCH
;
1722 if (mode
== MODE_INVALID
)
1723 return token
->op
== OP_MATCH
;
1725 match
= (statbuf
.st_mode
& mode
) > 0;
1726 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1728 case TK_M_PROGRAM
: {
1729 char buf
[UDEV_PATH_SIZE
], result
[UDEV_LINE_SIZE
];
1733 event
->program_result
= mfree(event
->program_result
);
1734 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1736 log_rule_debug(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
1737 "assuming the PROGRAM key does not match.", buf
, token
->value
);
1741 log_rule_debug(dev
, rules
, "Running PROGRAM '%s'", buf
);
1743 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof(result
), NULL
);
1746 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute \"%s\": %m", buf
);
1747 else /* returned value is positive when program fails */
1748 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error)", buf
, r
);
1749 return token
->op
== OP_NOMATCH
;
1752 delete_trailing_chars(result
, "\n");
1753 count
= udev_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1755 log_rule_debug(dev
, rules
, "Replaced %zu character(s) in result of \"%s\"",
1758 event
->program_result
= strdup(result
);
1759 return token
->op
== OP_MATCH
;
1761 case TK_M_IMPORT_FILE
: {
1762 _cleanup_fclose_
FILE *f
= NULL
;
1763 char buf
[UDEV_PATH_SIZE
];
1766 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1768 log_rule_debug(dev
, rules
, "The file name '%s' to be imported is truncated while substituting into '%s', "
1769 "assuming the IMPORT key does not match.", buf
, token
->value
);
1773 log_rule_debug(dev
, rules
, "Importing properties from '%s'", buf
);
1775 f
= fopen(buf
, "re");
1777 if (errno
!= ENOENT
)
1778 return log_rule_error_errno(dev
, rules
, errno
,
1779 "Failed to open '%s': %m", buf
);
1780 return token
->op
== OP_NOMATCH
;
1784 _cleanup_free_
char *line
= NULL
;
1787 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1789 log_rule_debug_errno(dev
, rules
, r
,
1790 "Failed to read '%s', ignoring: %m", buf
);
1791 return token
->op
== OP_NOMATCH
;
1796 r
= get_property_from_string(line
, &key
, &value
);
1798 log_rule_debug_errno(dev
, rules
, r
,
1799 "Failed to parse key and value from '%s', ignoring: %m",
1806 r
= device_add_property(dev
, key
, value
);
1808 return log_rule_error_errno(dev
, rules
, r
,
1809 "Failed to add property %s=%s: %m",
1813 return token
->op
== OP_MATCH
;
1815 case TK_M_IMPORT_PROGRAM
: {
1816 _cleanup_strv_free_
char **lines
= NULL
;
1817 char buf
[UDEV_PATH_SIZE
], result
[UDEV_LINE_SIZE
], **line
;
1820 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1822 log_rule_debug(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
1823 "assuming the IMPORT key does not match.", buf
, token
->value
);
1827 log_rule_debug(dev
, rules
, "Importing properties from results of '%s'", buf
);
1829 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof result
, &truncated
);
1832 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute '%s', ignoring: %m", buf
);
1833 else /* returned value is positive when program fails */
1834 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
1835 return token
->op
== OP_NOMATCH
;
1841 /* Drop the last line. */
1842 for (char *p
= buf
+ strlen(buf
) - 1; p
>= buf
; p
--)
1843 if (strchr(NEWLINE
, *p
)) {
1850 r
= strv_split_newlines_full(&lines
, result
, EXTRACT_RETAIN_ESCAPE
);
1854 log_rule_warning_errno(dev
, rules
, r
,
1855 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf
);
1859 STRV_FOREACH(line
, lines
) {
1862 r
= get_property_from_string(*line
, &key
, &value
);
1864 log_rule_debug_errno(dev
, rules
, r
,
1865 "Failed to parse key and value from '%s', ignoring: %m",
1872 r
= device_add_property(dev
, key
, value
);
1874 return log_rule_error_errno(dev
, rules
, r
,
1875 "Failed to add property %s=%s: %m",
1879 return token
->op
== OP_MATCH
;
1881 case TK_M_IMPORT_BUILTIN
: {
1882 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
1883 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
1884 unsigned mask
= 1U << (int) cmd
;
1885 char buf
[UDEV_PATH_SIZE
];
1888 if (udev_builtin_run_once(cmd
)) {
1889 /* check if we ran already */
1890 if (event
->builtin_run
& mask
) {
1891 log_rule_debug(dev
, rules
, "Skipping builtin '%s' in IMPORT key",
1892 udev_builtin_name(cmd
));
1893 /* return the result from earlier run */
1894 return token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
);
1897 event
->builtin_run
|= mask
;
1900 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1902 log_rule_debug(dev
, rules
, "The builtin command '%s' is truncated while substituting into '%s', "
1903 "assuming the IMPORT key does not match", buf
, token
->value
);
1907 log_rule_debug(dev
, rules
, "Importing properties from results of builtin command '%s'", buf
);
1909 r
= udev_builtin_run(dev
, &event
->rtnl
, cmd
, buf
, false);
1911 /* remember failure */
1912 log_rule_debug_errno(dev
, rules
, r
, "Failed to run builtin '%s': %m", buf
);
1913 event
->builtin_ret
|= mask
;
1915 return token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
);
1917 case TK_M_IMPORT_DB
: {
1920 if (!event
->dev_db_clone
)
1921 return token
->op
== OP_NOMATCH
;
1922 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
1924 return token
->op
== OP_NOMATCH
;
1926 return log_rule_error_errno(dev
, rules
, r
,
1927 "Failed to get property '%s' from database: %m",
1930 r
= device_add_property(dev
, token
->value
, val
);
1932 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1934 return token
->op
== OP_MATCH
;
1936 case TK_M_IMPORT_CMDLINE
: {
1937 _cleanup_free_
char *value
= NULL
;
1939 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
|PROC_CMDLINE_IGNORE_EFI_OPTIONS
, &value
);
1941 return log_rule_error_errno(dev
, rules
, r
,
1942 "Failed to read '%s' option from /proc/cmdline: %m",
1945 return token
->op
== OP_NOMATCH
;
1947 r
= device_add_property(dev
, token
->value
, value
?: "1");
1949 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1950 token
->value
, value
?: "1");
1951 return token
->op
== OP_MATCH
;
1953 case TK_M_IMPORT_PARENT
: {
1954 char buf
[UDEV_PATH_SIZE
];
1957 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
1959 log_rule_debug(dev
, rules
, "The property name '%s' is truncated while substituting into '%s', "
1960 "assuming the IMPORT key does not match.", buf
, token
->value
);
1964 r
= import_parent_into_properties(dev
, buf
);
1966 return log_rule_error_errno(dev
, rules
, r
,
1967 "Failed to import properties '%s' from parent: %m",
1969 return token
->op
== (r
> 0 ? OP_MATCH
: OP_NOMATCH
);
1972 return token_match_string(token
, event
->program_result
);
1973 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
1974 event
->esc
= ESCAPE_NONE
;
1976 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
1977 event
->esc
= ESCAPE_REPLACE
;
1979 case TK_A_OPTIONS_DB_PERSIST
:
1980 device_set_db_persist(dev
);
1982 case TK_A_OPTIONS_INOTIFY_WATCH
:
1983 if (event
->inotify_watch_final
)
1985 if (token
->op
== OP_ASSIGN_FINAL
)
1986 event
->inotify_watch_final
= true;
1988 event
->inotify_watch
= token
->data
;
1990 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
1991 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
1993 case TK_A_OPTIONS_LOG_LEVEL
: {
1994 int level
= PTR_TO_INT(token
->data
);
1997 level
= event
->default_log_level
;
1999 log_set_max_level(level
);
2001 if (level
== LOG_DEBUG
&& !event
->log_level_was_debug
) {
2002 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
2003 log_device_uevent(dev
, "The log level is changed to 'debug' while processing device");
2004 event
->log_level_was_debug
= true;
2010 char owner
[UDEV_NAME_SIZE
];
2011 const char *ow
= owner
;
2014 if (event
->owner_final
)
2016 if (token
->op
== OP_ASSIGN_FINAL
)
2017 event
->owner_final
= true;
2019 (void) udev_event_apply_format(event
, token
->value
, owner
, sizeof(owner
), false, &truncated
);
2021 log_rule_warning(dev
, rules
, "The user name '%s' is truncated while substituting into '%s', "
2022 "refusing to apply the OWNER key.", owner
, token
->value
);
2026 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2028 log_unknown_owner(dev
, rules
, r
, "user", owner
);
2030 log_rule_debug(dev
, rules
, "OWNER %s(%u)", owner
, event
->uid
);
2034 char group
[UDEV_NAME_SIZE
];
2035 const char *gr
= group
;
2038 if (event
->group_final
)
2040 if (token
->op
== OP_ASSIGN_FINAL
)
2041 event
->group_final
= true;
2043 (void) udev_event_apply_format(event
, token
->value
, group
, sizeof(group
), false, &truncated
);
2045 log_rule_warning(dev
, rules
, "The group name '%s' is truncated while substituting into '%s', "
2046 "refusing to apply the GROUP key.", group
, token
->value
);
2050 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2052 log_unknown_owner(dev
, rules
, r
, "group", group
);
2054 log_rule_debug(dev
, rules
, "GROUP %s(%u)", group
, event
->gid
);
2058 char mode_str
[UDEV_NAME_SIZE
];
2061 if (event
->mode_final
)
2063 if (token
->op
== OP_ASSIGN_FINAL
)
2064 event
->mode_final
= true;
2066 (void) udev_event_apply_format(event
, token
->value
, mode_str
, sizeof(mode_str
), false, &truncated
);
2068 log_rule_warning(dev
, rules
, "The mode '%s' is truncated while substituting into %s, "
2069 "refusing to apply the MODE key.", mode_str
, token
->value
);
2073 r
= parse_mode(mode_str
, &event
->mode
);
2075 log_rule_error_errno(dev
, rules
, r
, "Failed to parse mode '%s', ignoring: %m", mode_str
);
2077 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
2081 if (event
->owner_final
)
2083 if (token
->op
== OP_ASSIGN_FINAL
)
2084 event
->owner_final
= true;
2087 event
->uid
= PTR_TO_UID(token
->data
);
2088 log_rule_debug(dev
, rules
, "OWNER %u", event
->uid
);
2091 if (event
->group_final
)
2093 if (token
->op
== OP_ASSIGN_FINAL
)
2094 event
->group_final
= true;
2097 event
->gid
= PTR_TO_GID(token
->data
);
2098 log_rule_debug(dev
, rules
, "GROUP %u", event
->gid
);
2101 if (event
->mode_final
)
2103 if (token
->op
== OP_ASSIGN_FINAL
)
2104 event
->mode_final
= true;
2107 event
->mode
= PTR_TO_MODE(token
->data
);
2108 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
2110 case TK_A_SECLABEL
: {
2111 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2112 char label_str
[UDEV_LINE_SIZE
] = {};
2115 name
= strdup(token
->data
);
2119 (void) udev_event_apply_format(event
, token
->value
, label_str
, sizeof(label_str
), false, &truncated
);
2121 log_rule_warning(dev
, rules
, "The security label '%s' is truncated while substituting into '%s', "
2122 "refusing to apply the SECLABEL key.", label_str
, token
->value
);
2126 if (!isempty(label_str
))
2127 label
= strdup(label_str
);
2129 label
= strdup(token
->value
);
2133 if (token
->op
== OP_ASSIGN
)
2134 ordered_hashmap_clear_free_free(event
->seclabel_list
);
2136 r
= ordered_hashmap_ensure_put(&event
->seclabel_list
, NULL
, name
, label
);
2140 return log_rule_error_errno(dev
, rules
, r
, "Failed to store SECLABEL{%s}='%s': %m", name
, label
);;
2142 log_rule_debug(dev
, rules
, "SECLABEL{%s}='%s'", name
, label
);
2149 const char *val
, *name
= token
->data
;
2150 char value_new
[UDEV_NAME_SIZE
], *p
= value_new
;
2151 size_t count
, l
= sizeof(value_new
);
2154 if (isempty(token
->value
)) {
2155 if (token
->op
== OP_ADD
)
2157 r
= device_add_property(dev
, name
, NULL
);
2159 return log_rule_error_errno(dev
, rules
, r
, "Failed to remove property '%s': %m", name
);
2163 if (token
->op
== OP_ADD
&&
2164 sd_device_get_property_value(dev
, name
, &val
) >= 0) {
2165 l
= strpcpyl_full(&p
, l
, &truncated
, val
, " ", NULL
);
2167 log_rule_warning(dev
, rules
, "The buffer for the property '%s' is full, "
2168 "refusing to append the new value '%s'.", name
, token
->value
);
2173 (void) udev_event_apply_format(event
, token
->value
, p
, l
, false, &truncated
);
2175 log_rule_warning(dev
, rules
, "The property value '%s' is truncated while substituting into '%s', "
2176 "refusing to add property '%s'.", p
, token
->value
, name
);
2180 if (event
->esc
== ESCAPE_REPLACE
) {
2181 count
= udev_replace_chars(p
, NULL
);
2183 log_rule_debug(dev
, rules
, "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"",
2184 count
, name
, token
->op
== OP_ADD
? "+" : "", token
->value
);
2187 r
= device_add_property(dev
, name
, value_new
);
2189 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m", name
, value_new
);
2193 char buf
[UDEV_PATH_SIZE
];
2196 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2198 log_rule_warning(dev
, rules
, "The tag name '%s' is truncated while substituting into '%s',"
2199 "refusing to %s the tag.", buf
, token
->value
,
2200 token
->op
== OP_REMOVE
? "remove" : "add");
2204 if (token
->op
== OP_ASSIGN
)
2205 device_cleanup_tags(dev
);
2207 if (buf
[strspn(buf
, ALPHANUMERICAL
"-_")] != '\0') {
2208 log_rule_error(dev
, rules
, "Invalid tag name '%s', ignoring", buf
);
2211 if (token
->op
== OP_REMOVE
)
2212 device_remove_tag(dev
, buf
);
2214 r
= device_add_tag(dev
, buf
, true);
2216 return log_rule_error_errno(dev
, rules
, r
, "Failed to add tag '%s': %m", buf
);
2221 char buf
[UDEV_PATH_SIZE
];
2225 if (event
->name_final
)
2227 if (token
->op
== OP_ASSIGN_FINAL
)
2228 event
->name_final
= true;
2230 if (sd_device_get_ifindex(dev
, NULL
) < 0) {
2231 log_rule_error(dev
, rules
,
2232 "Only network interfaces can be renamed, ignoring NAME=\"%s\".",
2237 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2239 log_rule_warning(dev
, rules
, "The network interface name '%s' is truncated while substituting into '%s', "
2240 "refusing to set the name.", buf
, token
->value
);
2244 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2245 if (naming_scheme_has(NAMING_REPLACE_STRICTLY
))
2246 count
= udev_replace_ifname(buf
);
2248 count
= udev_replace_chars(buf
, "/");
2250 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of NAME=\"%s\"",
2251 count
, token
->value
);
2253 r
= free_and_strdup_warn(&event
->name
, buf
);
2257 log_rule_debug(dev
, rules
, "NAME '%s'", event
->name
);
2260 case TK_A_DEVLINK
: {
2261 char buf
[UDEV_PATH_SIZE
], *p
;
2265 if (event
->devlink_final
)
2267 if (sd_device_get_devnum(dev
, NULL
) < 0)
2269 if (token
->op
== OP_ASSIGN_FINAL
)
2270 event
->devlink_final
= true;
2271 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2272 device_cleanup_devlinks(dev
);
2274 /* allow multiple symlinks separated by spaces */
2275 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), event
->esc
!= ESCAPE_NONE
, &truncated
);
2277 log_rule_warning(dev
, rules
, "The symbolic link path '%s' is truncated while substituting into '%s', "
2278 "refusing to add the device symbolic link.", buf
, token
->value
);
2282 if (event
->esc
== ESCAPE_UNSET
)
2283 count
= udev_replace_chars(buf
, "/ ");
2284 else if (event
->esc
== ESCAPE_REPLACE
)
2285 count
= udev_replace_chars(buf
, "/");
2289 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of SYMLINK=\"%s\"",
2290 count
, token
->value
);
2292 p
= skip_leading_chars(buf
, NULL
);
2293 while (!isempty(p
)) {
2294 char filename
[UDEV_PATH_SIZE
], *next
;
2296 next
= strchr(p
, ' ');
2299 next
= skip_leading_chars(next
, NULL
);
2302 strscpyl_full(filename
, sizeof(filename
), &truncated
, "/dev/", p
, NULL
);
2306 r
= device_add_devlink(dev
, filename
);
2308 return log_rule_error_errno(dev
, rules
, r
, "Failed to add devlink '%s': %m", filename
);
2310 log_rule_debug(dev
, rules
, "LINK '%s'", p
);
2316 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2317 const char *val
, *key_name
= token
->data
;
2320 if (udev_resolve_subsys_kernel(key_name
, buf
, sizeof(buf
), false) < 0 &&
2321 sd_device_get_syspath(dev
, &val
) >= 0) {
2322 strscpyl_full(buf
, sizeof(buf
), &truncated
, val
, "/", key_name
, NULL
);
2324 log_rule_warning(dev
, rules
,
2325 "The path to the attribute '%s/%s' is too long, refusing to set the attribute.",
2331 r
= attr_subst_subdir(buf
);
2333 log_rule_error_errno(dev
, rules
, r
, "Could not find file matches '%s', ignoring: %m", buf
);
2336 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2338 log_rule_warning(dev
, rules
, "The attribute value '%s' is truncated while substituting into '%s', "
2339 "refusing to set the attribute '%s'", value
, token
->value
, buf
);
2343 log_rule_debug(dev
, rules
, "ATTR '%s' writing '%s'", buf
, value
);
2344 r
= write_string_file(buf
, value
,
2345 WRITE_STRING_FILE_VERIFY_ON_FAILURE
|
2346 WRITE_STRING_FILE_DISABLE_BUFFER
|
2347 WRITE_STRING_FILE_AVOID_NEWLINE
|
2348 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE
);
2350 log_rule_error_errno(dev
, rules
, r
, "Failed to write ATTR{%s}, ignoring: %m", buf
);
2354 char buf
[UDEV_PATH_SIZE
], value
[UDEV_NAME_SIZE
];
2357 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false, &truncated
);
2359 log_rule_warning(dev
, rules
, "The sysctl entry name '%s' is truncated while substituting into '%s', "
2360 "refusing to set the sysctl entry.", buf
, (const char*) token
->data
);
2364 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false, &truncated
);
2366 log_rule_warning(dev
, rules
, "The sysctl value '%s' is truncated while substituting into '%s', "
2367 "refusing to set the sysctl entry '%s'", value
, token
->value
, buf
);
2371 sysctl_normalize(buf
);
2372 log_rule_debug(dev
, rules
, "SYSCTL '%s' writing '%s'", buf
, value
);
2373 r
= sysctl_write(buf
, value
);
2375 log_rule_error_errno(dev
, rules
, r
, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf
, value
);
2378 case TK_A_RUN_BUILTIN
:
2379 case TK_A_RUN_PROGRAM
: {
2380 _cleanup_free_
char *cmd
= NULL
;
2381 char buf
[UDEV_PATH_SIZE
];
2384 if (event
->run_final
)
2386 if (token
->op
== OP_ASSIGN_FINAL
)
2387 event
->run_final
= true;
2389 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2390 ordered_hashmap_clear_free_key(event
->run_list
);
2392 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false, &truncated
);
2394 log_rule_warning(dev
, rules
, "The command '%s' is truncated while substituting into '%s', "
2395 "refusing to invoke the command.", buf
, token
->value
);
2403 r
= ordered_hashmap_ensure_put(&event
->run_list
, NULL
, cmd
, token
->data
);
2407 return log_rule_error_errno(dev
, rules
, r
, "Failed to store command '%s': %m", cmd
);
2411 log_rule_debug(dev
, rules
, "RUN '%s'", token
->value
);
2414 case TK_A_OPTIONS_STATIC_NODE
:
2415 /* do nothing for events. */
2418 assert_not_reached();
2424 static bool token_is_for_parents(UdevRuleToken
*token
) {
2425 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
2428 static int udev_rule_apply_parent_token_to_event(
2431 int timeout_signal
) {
2434 UdevRuleToken
*head
;
2437 line
= rules
->current_file
->current_line
;
2438 head
= rules
->current_file
->current_line
->current_token
;
2439 event
->dev_parent
= event
->dev
;
2441 LIST_FOREACH(tokens
, line
->current_token
, head
) {
2442 if (!token_is_for_parents(line
->current_token
))
2443 return true; /* All parent tokens match. */
2444 r
= udev_rule_apply_token_to_event(rules
, event
->dev_parent
, event
, 0, timeout_signal
, NULL
);
2450 if (!line
->current_token
)
2451 /* All parent tokens match. But no assign tokens in the line. Hmm... */
2454 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
2455 event
->dev_parent
= NULL
;
2461 static int udev_rule_apply_line_to_event(
2464 usec_t timeout_usec
,
2466 Hashmap
*properties_list
,
2467 UdevRuleLine
**next_line
) {
2469 UdevRuleLine
*line
= rules
->current_file
->current_line
;
2470 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
2471 UdevRuleToken
*token
, *next_token
;
2472 bool parents_done
= false;
2473 sd_device_action_t action
;
2476 r
= sd_device_get_action(event
->dev
, &action
);
2480 if (action
!= SD_DEVICE_REMOVE
) {
2481 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
2482 mask
|= LINE_HAS_DEVLINK
;
2484 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
2485 mask
|= LINE_HAS_NAME
;
2488 if ((line
->type
& mask
) == 0)
2491 event
->esc
= ESCAPE_UNSET
;
2493 DEVICE_TRACE_POINT(rules_apply_line
, event
->dev
, line
->rule_file
->filename
, line
->line_number
);
2495 LIST_FOREACH_SAFE(tokens
, token
, next_token
, line
->tokens
) {
2496 line
->current_token
= token
;
2498 if (token_is_for_parents(token
)) {
2502 r
= udev_rule_apply_parent_token_to_event(rules
, event
, timeout_signal
);
2506 parents_done
= true;
2510 r
= udev_rule_apply_token_to_event(rules
, event
->dev
, event
, timeout_usec
, timeout_signal
, properties_list
);
2515 if (line
->goto_line
)
2516 *next_line
= line
->goto_line
;
2521 int udev_rules_apply_to_event(
2524 usec_t timeout_usec
,
2526 Hashmap
*properties_list
) {
2529 UdevRuleLine
*next_line
;
2535 LIST_FOREACH(rule_files
, file
, rules
->rule_files
) {
2536 rules
->current_file
= file
;
2537 LIST_FOREACH_SAFE(rule_lines
, file
->current_line
, next_line
, file
->rule_lines
) {
2538 r
= udev_rule_apply_line_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
, &next_line
);
2547 static int apply_static_dev_perms(const char *devnode
, uid_t uid
, gid_t gid
, mode_t mode
, char **tags
) {
2548 char device_node
[UDEV_PATH_SIZE
], tags_dir
[UDEV_PATH_SIZE
], tag_symlink
[UDEV_PATH_SIZE
];
2549 _cleanup_free_
char *unescaped_filename
= NULL
;
2556 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
&& !tags
)
2559 strscpyl(device_node
, sizeof(device_node
), "/dev/", devnode
, NULL
);
2560 if (stat(device_node
, &stats
) < 0) {
2561 if (errno
!= ENOENT
)
2562 return log_error_errno(errno
, "Failed to stat %s: %m", device_node
);
2566 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
)) {
2567 log_warning("%s is neither block nor character device, ignoring.", device_node
);
2571 if (!strv_isempty(tags
)) {
2572 unescaped_filename
= xescape(devnode
, "/.");
2573 if (!unescaped_filename
)
2577 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2578 STRV_FOREACH(t
, tags
) {
2579 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2580 r
= mkdir_p(tags_dir
, 0755);
2582 return log_error_errno(r
, "Failed to create %s: %m", tags_dir
);
2584 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2585 r
= symlink(device_node
, tag_symlink
);
2586 if (r
< 0 && errno
!= EEXIST
)
2587 return log_error_errno(errno
, "Failed to create symlink %s -> %s: %m",
2588 tag_symlink
, device_node
);
2591 /* don't touch the permissions if only the tags were set */
2592 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
)
2595 if (mode
== MODE_INVALID
)
2596 mode
= gid_is_valid(gid
) ? 0660 : 0600;
2597 if (!uid_is_valid(uid
))
2599 if (!gid_is_valid(gid
))
2602 r
= chmod_and_chown(device_node
, mode
, uid
, gid
);
2606 return log_error_errno(r
, "Failed to chown '%s' %u %u: %m", device_node
, uid
, gid
);
2608 log_debug("chown '%s' %u:%u with mode %#o", device_node
, uid
, gid
, mode
);
2610 (void) utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2614 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
2615 UdevRuleToken
*token
;
2616 _cleanup_strv_free_
char **tags
= NULL
;
2617 uid_t uid
= UID_INVALID
;
2618 gid_t gid
= GID_INVALID
;
2619 mode_t mode
= MODE_INVALID
;
2624 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
2627 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
2628 if (token
->type
== TK_A_OWNER_ID
)
2629 uid
= PTR_TO_UID(token
->data
);
2630 else if (token
->type
== TK_A_GROUP_ID
)
2631 gid
= PTR_TO_GID(token
->data
);
2632 else if (token
->type
== TK_A_MODE_ID
)
2633 mode
= PTR_TO_MODE(token
->data
);
2634 else if (token
->type
== TK_A_TAG
) {
2635 r
= strv_extend(&tags
, token
->value
);
2638 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
2639 r
= apply_static_dev_perms(token
->value
, uid
, gid
, mode
, tags
);
2647 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2654 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2655 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
2656 r
= udev_rule_line_apply_static_dev_perms(line
);