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 "nulstr-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "proc-cmdline.h"
24 #include "stat-util.h"
27 #include "sysctl-util.h"
28 #include "syslog-util.h"
29 #include "udev-builtin.h"
30 #include "udev-event.h"
31 #include "udev-rules.h"
32 #include "udev-util.h"
33 #include "user-util.h"
36 #define RULES_DIRS (const char* const*) CONF_PATHS_STRV("udev/rules.d")
44 OP_ASSIGN_FINAL
, /* := */
46 _OP_TYPE_INVALID
= -EINVAL
,
47 } UdevRuleOperatorType
;
50 MATCH_TYPE_EMPTY
, /* empty string */
51 MATCH_TYPE_PLAIN
, /* no special characters */
52 MATCH_TYPE_PLAIN_WITH_EMPTY
, /* no special characters with empty string, e.g., "|foo" */
53 MATCH_TYPE_GLOB
, /* shell globs ?,*,[] */
54 MATCH_TYPE_GLOB_WITH_EMPTY
, /* shell globs ?,*,[] with empty string, e.g., "|foo*" */
55 MATCH_TYPE_SUBSYSTEM
, /* "subsystem", "bus", or "class" */
57 _MATCH_TYPE_INVALID
= -EINVAL
,
61 SUBST_TYPE_PLAIN
, /* no substitution */
62 SUBST_TYPE_FORMAT
, /* % or $ */
63 SUBST_TYPE_SUBSYS
, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
65 _SUBST_TYPE_INVALID
= -EINVAL
,
66 } UdevRuleSubstituteType
;
69 /* lvalues which take match or nomatch operator */
70 TK_M_ACTION
, /* string, device_get_action() */
71 TK_M_DEVPATH
, /* path, sd_device_get_devpath() */
72 TK_M_KERNEL
, /* string, sd_device_get_sysname() */
73 TK_M_DEVLINK
, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */
74 TK_M_NAME
, /* string, name of network interface */
75 TK_M_ENV
, /* string, device property, takes key through attribute */
76 TK_M_CONST
, /* string, system-specific hard-coded constant */
77 TK_M_TAG
, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */
78 TK_M_SUBSYSTEM
, /* string, sd_device_get_subsystem() */
79 TK_M_DRIVER
, /* string, sd_device_get_driver() */
80 TK_M_ATTR
, /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */
81 TK_M_SYSCTL
, /* string, takes kernel parameter through attribute */
83 /* matches parent parameters */
84 TK_M_PARENTS_KERNEL
, /* string */
85 TK_M_PARENTS_SUBSYSTEM
, /* string */
86 TK_M_PARENTS_DRIVER
, /* string */
87 TK_M_PARENTS_ATTR
, /* string */
88 TK_M_PARENTS_TAG
, /* strv */
90 TK_M_TEST
, /* path, optionally mode_t can be specified by attribute, test the existence of a file */
91 TK_M_PROGRAM
, /* string, execute a program */
92 TK_M_IMPORT_FILE
, /* path */
93 TK_M_IMPORT_PROGRAM
, /* string, import properties from the result of program */
94 TK_M_IMPORT_BUILTIN
, /* string, import properties from the result of built-in command */
95 TK_M_IMPORT_DB
, /* string, import properties from database */
96 TK_M_IMPORT_CMDLINE
, /* string, kernel command line */
97 TK_M_IMPORT_PARENT
, /* string, parent property */
98 TK_M_RESULT
, /* string, result of TK_M_PROGRAM */
100 #define _TK_M_MAX (TK_M_RESULT + 1)
101 #define _TK_A_MIN _TK_M_MAX
103 /* lvalues which take one of assign operators */
104 TK_A_OPTIONS_STRING_ESCAPE_NONE
, /* no argument */
105 TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, /* no argument */
106 TK_A_OPTIONS_DB_PERSIST
, /* no argument */
107 TK_A_OPTIONS_INOTIFY_WATCH
, /* boolean */
108 TK_A_OPTIONS_DEVLINK_PRIORITY
, /* int */
109 TK_A_OPTIONS_LOG_LEVEL
, /* string of log level or "reset" */
110 TK_A_OWNER
, /* user name */
111 TK_A_GROUP
, /* group name */
112 TK_A_MODE
, /* mode string */
113 TK_A_OWNER_ID
, /* uid_t */
114 TK_A_GROUP_ID
, /* gid_t */
115 TK_A_MODE_ID
, /* mode_t */
116 TK_A_TAG
, /* string */
117 TK_A_OPTIONS_STATIC_NODE
, /* device path, /dev/... */
118 TK_A_SECLABEL
, /* string with attribute */
119 TK_A_ENV
, /* string with attribute */
120 TK_A_NAME
, /* ifname */
121 TK_A_DEVLINK
, /* string */
122 TK_A_ATTR
, /* string with attribute */
123 TK_A_SYSCTL
, /* string with attribute */
124 TK_A_RUN_BUILTIN
, /* string */
125 TK_A_RUN_PROGRAM
, /* string */
128 _TK_TYPE_INVALID
= -EINVAL
,
132 LINE_HAS_NAME
= 1 << 0, /* has NAME= */
133 LINE_HAS_DEVLINK
= 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */
134 LINE_HAS_STATIC_NODE
= 1 << 2, /* has OPTIONS=static_node */
135 LINE_HAS_GOTO
= 1 << 3, /* has GOTO= */
136 LINE_HAS_LABEL
= 1 << 4, /* has LABEL= */
137 LINE_UPDATE_SOMETHING
= 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */
140 typedef struct UdevRuleFile UdevRuleFile
;
141 typedef struct UdevRuleLine UdevRuleLine
;
142 typedef struct UdevRuleToken UdevRuleToken
;
144 struct UdevRuleToken
{
145 UdevRuleTokenType type
:8;
146 UdevRuleOperatorType op
:8;
147 UdevRuleMatchType match_type
:8;
148 UdevRuleSubstituteType attr_subst_type
:7;
149 bool attr_match_remove_trailing_whitespace
:1;
152 LIST_FIELDS(UdevRuleToken
, tokens
);
155 struct UdevRuleLine
{
157 unsigned line_number
;
158 UdevRuleLineType type
;
161 const char *goto_label
;
162 UdevRuleLine
*goto_line
;
164 UdevRuleFile
*rule_file
;
165 UdevRuleToken
*current_token
;
166 LIST_HEAD(UdevRuleToken
, tokens
);
167 LIST_FIELDS(UdevRuleLine
, rule_lines
);
170 struct UdevRuleFile
{
172 UdevRuleLine
*current_line
;
173 LIST_HEAD(UdevRuleLine
, rule_lines
);
174 LIST_FIELDS(UdevRuleFile
, rule_files
);
179 ResolveNameTiming resolve_name_timing
;
180 Hashmap
*known_users
;
181 Hashmap
*known_groups
;
182 UdevRuleFile
*current_file
;
183 LIST_HEAD(UdevRuleFile
, rule_files
);
186 /*** Logging helpers ***/
188 #define log_rule_full_errno_zerook(device, rules, level, error, fmt, ...) \
190 UdevRules *_r = (rules); \
191 UdevRuleFile *_f = _r ? _r->current_file : NULL; \
192 UdevRuleLine *_l = _f ? _f->current_line : NULL; \
193 const char *_n = _f ? _f->filename : NULL; \
195 log_device_full_errno_zerook( \
196 device, level, error, "%s:%u " fmt, \
197 strna(_n), _l ? _l->line_number : 0, \
201 #define log_rule_full_errno(device, rules, level, error, fmt, ...) \
203 int _error = (error); \
204 ASSERT_NON_ZERO(_error); \
205 log_rule_full_errno_zerook( \
206 device, rules, level, _error, fmt, ##__VA_ARGS__); \
209 #define log_rule_full(device, rules, level, ...) (void) log_rule_full_errno_zerook(device, rules, level, 0, __VA_ARGS__)
211 #define log_rule_debug(device, rules, ...) log_rule_full(device, rules, LOG_DEBUG, __VA_ARGS__)
212 #define log_rule_info(device, rules, ...) log_rule_full(device, rules, LOG_INFO, __VA_ARGS__)
213 #define log_rule_notice(device, rules, ...) log_rule_full(device, rules, LOG_NOTICE, __VA_ARGS__)
214 #define log_rule_warning(device, rules, ...) log_rule_full(device, rules, LOG_WARNING, __VA_ARGS__)
215 #define log_rule_error(device, rules, ...) log_rule_full(device, rules, LOG_ERR, __VA_ARGS__)
217 #define log_rule_debug_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_DEBUG, error, __VA_ARGS__)
218 #define log_rule_info_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_INFO, error, __VA_ARGS__)
219 #define log_rule_notice_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_NOTICE, error, __VA_ARGS__)
220 #define log_rule_warning_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_WARNING, error, __VA_ARGS__)
221 #define log_rule_error_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_ERR, error, __VA_ARGS__)
223 #define log_token_full_errno_zerook(rules, level, error, ...) log_rule_full_errno_zerook(NULL, rules, level, error, __VA_ARGS__)
224 #define log_token_full_errno(rules, level, error, ...) log_rule_full_errno(NULL, rules, level, error, __VA_ARGS__)
225 #define log_token_full(rules, level, ...) (void) log_token_full_errno_zerook(rules, level, 0, __VA_ARGS__)
227 #define log_token_debug(rules, ...) log_token_full(rules, LOG_DEBUG, __VA_ARGS__)
228 #define log_token_info(rules, ...) log_token_full(rules, LOG_INFO, __VA_ARGS__)
229 #define log_token_notice(rules, ...) log_token_full(rules, LOG_NOTICE, __VA_ARGS__)
230 #define log_token_warning(rules, ...) log_token_full(rules, LOG_WARNING, __VA_ARGS__)
231 #define log_token_error(rules, ...) log_token_full(rules, LOG_ERR, __VA_ARGS__)
233 #define log_token_debug_errno(rules, error, ...) log_token_full_errno(rules, LOG_DEBUG, error, __VA_ARGS__)
234 #define log_token_info_errno(rules, error, ...) log_token_full_errno(rules, LOG_INFO, error, __VA_ARGS__)
235 #define log_token_notice_errno(rules, error, ...) log_token_full_errno(rules, LOG_NOTICE, error, __VA_ARGS__)
236 #define log_token_warning_errno(rules, error, ...) log_token_full_errno(rules, LOG_WARNING, error, __VA_ARGS__)
237 #define log_token_error_errno(rules, error, ...) log_token_full_errno(rules, LOG_ERR, error, __VA_ARGS__)
239 #define _log_token_invalid(rules, key, type) \
240 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
241 "Invalid %s for %s.", type, key)
243 #define log_token_invalid_op(rules, key) _log_token_invalid(rules, key, "operator")
244 #define log_token_invalid_attr(rules, key) _log_token_invalid(rules, key, "attribute")
246 #define log_token_invalid_attr_format(rules, key, attr, offset, hint) \
247 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
248 "Invalid attribute \"%s\" for %s (char %zu: %s), ignoring, but please fix it.", \
249 attr, key, offset, hint)
250 #define log_token_invalid_value(rules, key, value, offset, hint) \
251 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
252 "Invalid value \"%s\" for %s (char %zu: %s), ignoring, but please fix it.", \
253 value, key, offset, hint)
255 static void log_unknown_owner(sd_device
*dev
, UdevRules
*rules
, int error
, const char *entity
, const char *name
) {
256 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
257 log_rule_error(dev
, rules
, "Unknown %s '%s', ignoring", entity
, name
);
259 log_rule_error_errno(dev
, rules
, error
, "Failed to resolve %s '%s', ignoring: %m", entity
, name
);
262 /*** Other functions ***/
264 static void udev_rule_token_free(UdevRuleToken
*token
) {
268 static void udev_rule_line_clear_tokens(UdevRuleLine
*rule_line
) {
269 UdevRuleToken
*i
, *next
;
273 LIST_FOREACH_SAFE(tokens
, i
, next
, 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
) {
299 UdevRuleLine
*i
, *next
;
304 LIST_FOREACH_SAFE(rule_lines
, i
, next
, rule_file
->rule_lines
)
305 udev_rule_line_free(i
);
307 free(rule_file
->filename
);
311 UdevRules
*udev_rules_free(UdevRules
*rules
) {
312 UdevRuleFile
*i
, *next
;
317 LIST_FOREACH_SAFE(rule_files
, i
, next
, rules
->rule_files
)
318 udev_rule_file_free(i
);
320 hashmap_free_free_key(rules
->known_users
);
321 hashmap_free_free_key(rules
->known_groups
);
325 static int rule_resolve_user(UdevRules
*rules
, const char *name
, uid_t
*ret
) {
326 _cleanup_free_
char *n
= NULL
;
334 val
= hashmap_get(rules
->known_users
, name
);
336 *ret
= PTR_TO_UID(val
);
340 r
= get_user_creds(&name
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
342 log_unknown_owner(NULL
, rules
, r
, "user", name
);
351 r
= hashmap_ensure_put(&rules
->known_users
, &string_hash_ops
, n
, UID_TO_PTR(uid
));
360 static int rule_resolve_group(UdevRules
*rules
, const char *name
, gid_t
*ret
) {
361 _cleanup_free_
char *n
= NULL
;
369 val
= hashmap_get(rules
->known_groups
, name
);
371 *ret
= PTR_TO_GID(val
);
375 r
= get_group_creds(&name
, &gid
, USER_CREDS_ALLOW_MISSING
);
377 log_unknown_owner(NULL
, rules
, r
, "group", name
);
386 r
= hashmap_ensure_put(&rules
->known_groups
, &string_hash_ops
, n
, GID_TO_PTR(gid
));
395 static UdevRuleSubstituteType
rule_get_substitution_type(const char *str
) {
399 return SUBST_TYPE_SUBSYS
;
400 if (strchr(str
, '%') || strchr(str
, '$'))
401 return SUBST_TYPE_FORMAT
;
402 return SUBST_TYPE_PLAIN
;
405 static void rule_line_append_token(UdevRuleLine
*rule_line
, UdevRuleToken
*token
) {
409 if (rule_line
->current_token
)
410 LIST_APPEND(tokens
, rule_line
->current_token
, token
);
412 LIST_APPEND(tokens
, rule_line
->tokens
, token
);
414 rule_line
->current_token
= token
;
417 static int rule_line_add_token(UdevRuleLine
*rule_line
, UdevRuleTokenType type
, UdevRuleOperatorType op
, char *value
, void *data
) {
418 UdevRuleToken
*token
;
419 UdevRuleMatchType match_type
= _MATCH_TYPE_INVALID
;
420 UdevRuleSubstituteType subst_type
= _SUBST_TYPE_INVALID
;
421 bool remove_trailing_whitespace
= false;
425 assert(type
>= 0 && type
< _TK_TYPE_MAX
);
426 assert(op
>= 0 && op
< _OP_TYPE_MAX
);
428 if (type
< _TK_M_MAX
) {
430 assert(IN_SET(op
, OP_MATCH
, OP_NOMATCH
));
432 if (type
== TK_M_SUBSYSTEM
&& STR_IN_SET(value
, "subsystem", "bus", "class"))
433 match_type
= MATCH_TYPE_SUBSYSTEM
;
434 else if (isempty(value
))
435 match_type
= MATCH_TYPE_EMPTY
;
436 else if (streq(value
, "?*")) {
437 /* Convert KEY=="?*" -> KEY!="" */
438 match_type
= MATCH_TYPE_EMPTY
;
439 op
= op
== OP_MATCH
? OP_NOMATCH
: OP_MATCH
;
440 } else if (string_is_glob(value
))
441 match_type
= MATCH_TYPE_GLOB
;
443 match_type
= MATCH_TYPE_PLAIN
;
445 if (type
< TK_M_TEST
|| type
== TK_M_RESULT
) {
446 /* Convert value string to nulstr. */
447 bool bar
= true, empty
= false;
450 for (a
= b
= value
; *a
!= '\0'; a
++) {
464 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
472 if (match_type
== MATCH_TYPE_GLOB
)
473 match_type
= MATCH_TYPE_GLOB_WITH_EMPTY
;
474 if (match_type
== MATCH_TYPE_PLAIN
)
475 match_type
= MATCH_TYPE_PLAIN_WITH_EMPTY
;
480 if (IN_SET(type
, TK_M_ATTR
, TK_M_PARENTS_ATTR
)) {
485 if (len
> 0 && !isspace(value
[len
- 1]))
486 remove_trailing_whitespace
= true;
488 subst_type
= rule_get_substitution_type(data
);
491 token
= new(UdevRuleToken
, 1);
495 *token
= (UdevRuleToken
) {
500 .match_type
= match_type
,
501 .attr_subst_type
= subst_type
,
502 .attr_match_remove_trailing_whitespace
= remove_trailing_whitespace
,
505 rule_line_append_token(rule_line
, token
);
507 if (token
->type
== TK_A_NAME
)
508 SET_FLAG(rule_line
->type
, LINE_HAS_NAME
, true);
510 else if (IN_SET(token
->type
, TK_A_DEVLINK
,
511 TK_A_OWNER
, TK_A_GROUP
, TK_A_MODE
,
512 TK_A_OWNER_ID
, TK_A_GROUP_ID
, TK_A_MODE_ID
))
513 SET_FLAG(rule_line
->type
, LINE_HAS_DEVLINK
, true);
515 else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
)
516 SET_FLAG(rule_line
->type
, LINE_HAS_STATIC_NODE
, true);
518 else if (token
->type
>= _TK_A_MIN
||
519 IN_SET(token
->type
, TK_M_PROGRAM
,
520 TK_M_IMPORT_FILE
, TK_M_IMPORT_PROGRAM
, TK_M_IMPORT_BUILTIN
,
521 TK_M_IMPORT_DB
, TK_M_IMPORT_CMDLINE
, TK_M_IMPORT_PARENT
))
522 SET_FLAG(rule_line
->type
, LINE_UPDATE_SOMETHING
, true);
527 static void check_value_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
, bool nonempty
) {
531 if (nonempty
&& isempty(value
))
532 log_token_invalid_value(rules
, key
, value
, (size_t) 0, "empty value");
533 else if (udev_check_format(value
, &offset
, &hint
) < 0)
534 log_token_invalid_value(rules
, key
, value
, offset
+ 1, hint
);
537 static int check_attr_format_and_warn(UdevRules
*rules
, const char *key
, const char *value
) {
542 return log_token_invalid_attr(rules
, key
);
543 if (udev_check_format(value
, &offset
, &hint
) < 0)
544 log_token_invalid_attr_format(rules
, key
, value
, offset
+ 1, hint
);
548 static int parse_token(UdevRules
*rules
, const char *key
, char *attr
, UdevRuleOperatorType op
, char *value
) {
549 bool is_match
= IN_SET(op
, OP_MATCH
, OP_NOMATCH
);
550 UdevRuleLine
*rule_line
;
554 assert(rules
->current_file
);
555 assert(rules
->current_file
->current_line
);
559 rule_line
= rules
->current_file
->current_line
;
561 if (streq(key
, "ACTION")) {
563 return log_token_invalid_attr(rules
, key
);
565 return log_token_invalid_op(rules
, key
);
567 r
= rule_line_add_token(rule_line
, TK_M_ACTION
, op
, value
, NULL
);
568 } else if (streq(key
, "DEVPATH")) {
570 return log_token_invalid_attr(rules
, key
);
572 return log_token_invalid_op(rules
, key
);
574 r
= rule_line_add_token(rule_line
, TK_M_DEVPATH
, op
, value
, NULL
);
575 } else if (streq(key
, "KERNEL")) {
577 return log_token_invalid_attr(rules
, key
);
579 return log_token_invalid_op(rules
, key
);
581 r
= rule_line_add_token(rule_line
, TK_M_KERNEL
, op
, value
, NULL
);
582 } else if (streq(key
, "SYMLINK")) {
584 return log_token_invalid_attr(rules
, key
);
586 return log_token_invalid_op(rules
, key
);
589 check_value_format_and_warn(rules
, key
, value
, false);
590 r
= rule_line_add_token(rule_line
, TK_A_DEVLINK
, op
, value
, NULL
);
592 r
= rule_line_add_token(rule_line
, TK_M_DEVLINK
, op
, value
, NULL
);
593 } else if (streq(key
, "NAME")) {
595 return log_token_invalid_attr(rules
, key
);
597 return log_token_invalid_op(rules
, key
);
599 log_token_warning(rules
, "%s key takes '==', '!=', '=', or ':=' operator, assuming '=', but please fix it.", key
);
604 if (streq(value
, "%k"))
605 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
606 "NAME=\"%%k\" is ignored, as it breaks kernel supplied names.");
608 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
609 "Ignoring NAME=\"\", as udev will not delete any device nodes.");
610 check_value_format_and_warn(rules
, key
, value
, false);
612 r
= rule_line_add_token(rule_line
, TK_A_NAME
, op
, value
, NULL
);
614 r
= rule_line_add_token(rule_line
, TK_M_NAME
, op
, value
, NULL
);
615 } else if (streq(key
, "ENV")) {
617 return log_token_invalid_attr(rules
, key
);
619 return log_token_invalid_op(rules
, key
);
620 if (op
== OP_ASSIGN_FINAL
) {
621 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '=', but please fix it.", key
);
627 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
628 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
629 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
630 "Invalid ENV attribute. '%s' cannot be set.", attr
);
632 check_value_format_and_warn(rules
, key
, value
, false);
634 r
= rule_line_add_token(rule_line
, TK_A_ENV
, op
, value
, attr
);
636 r
= rule_line_add_token(rule_line
, TK_M_ENV
, op
, value
, attr
);
637 } else if (streq(key
, "CONST")) {
638 if (isempty(attr
) || !STR_IN_SET(attr
, "arch", "virt"))
639 return log_token_invalid_attr(rules
, key
);
641 return log_token_invalid_op(rules
, key
);
642 r
= rule_line_add_token(rule_line
, TK_M_CONST
, op
, value
, attr
);
643 } else if (streq(key
, "TAG")) {
645 return log_token_invalid_attr(rules
, key
);
646 if (op
== OP_ASSIGN_FINAL
) {
647 log_token_warning(rules
, "%s key takes '==', '!=', '=', or '+=' operator, assuming '=', but please fix it.", key
);
652 check_value_format_and_warn(rules
, key
, value
, true);
654 r
= rule_line_add_token(rule_line
, TK_A_TAG
, op
, value
, NULL
);
656 r
= rule_line_add_token(rule_line
, TK_M_TAG
, op
, value
, NULL
);
657 } else if (streq(key
, "SUBSYSTEM")) {
659 return log_token_invalid_attr(rules
, key
);
661 return log_token_invalid_op(rules
, key
);
663 if (STR_IN_SET(value
, "bus", "class"))
664 log_token_warning(rules
, "'%s' must be specified as 'subsystem'; please fix it", value
);
666 r
= rule_line_add_token(rule_line
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
667 } else if (streq(key
, "DRIVER")) {
669 return log_token_invalid_attr(rules
, key
);
671 return log_token_invalid_op(rules
, key
);
673 r
= rule_line_add_token(rule_line
, TK_M_DRIVER
, op
, value
, NULL
);
674 } else if (streq(key
, "ATTR")) {
675 r
= check_attr_format_and_warn(rules
, key
, attr
);
679 return log_token_invalid_op(rules
, key
);
680 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
681 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '=', but please fix it.", key
);
686 check_value_format_and_warn(rules
, key
, value
, false);
687 r
= rule_line_add_token(rule_line
, TK_A_ATTR
, op
, value
, attr
);
689 r
= rule_line_add_token(rule_line
, TK_M_ATTR
, op
, value
, attr
);
690 } else if (streq(key
, "SYSCTL")) {
691 r
= check_attr_format_and_warn(rules
, key
, attr
);
695 return log_token_invalid_op(rules
, key
);
696 if (IN_SET(op
, OP_ADD
, OP_ASSIGN_FINAL
)) {
697 log_token_warning(rules
, "%s key takes '==', '!=', or '=' operator, assuming '=', but please fix it.", key
);
702 check_value_format_and_warn(rules
, key
, value
, false);
703 r
= rule_line_add_token(rule_line
, TK_A_SYSCTL
, op
, value
, attr
);
705 r
= rule_line_add_token(rule_line
, TK_M_SYSCTL
, op
, value
, attr
);
706 } else if (streq(key
, "KERNELS")) {
708 return log_token_invalid_attr(rules
, key
);
710 return log_token_invalid_op(rules
, key
);
712 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_KERNEL
, op
, value
, NULL
);
713 } else if (streq(key
, "SUBSYSTEMS")) {
715 return log_token_invalid_attr(rules
, key
);
717 return log_token_invalid_op(rules
, key
);
719 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_SUBSYSTEM
, op
, value
, NULL
);
720 } else if (streq(key
, "DRIVERS")) {
722 return log_token_invalid_attr(rules
, key
);
724 return log_token_invalid_op(rules
, key
);
726 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_DRIVER
, op
, value
, NULL
);
727 } else if (streq(key
, "ATTRS")) {
728 r
= check_attr_format_and_warn(rules
, key
, attr
);
732 return log_token_invalid_op(rules
, key
);
734 if (startswith(attr
, "device/"))
735 log_token_warning(rules
, "'device' link may not be available in future kernels; please fix it.");
736 if (strstr(attr
, "../"))
737 log_token_warning(rules
, "Direct reference to parent sysfs directory, may break in future kernels; please fix it.");
739 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_ATTR
, op
, value
, attr
);
740 } else if (streq(key
, "TAGS")) {
742 return log_token_invalid_attr(rules
, key
);
744 return log_token_invalid_op(rules
, key
);
746 r
= rule_line_add_token(rule_line
, TK_M_PARENTS_TAG
, op
, value
, NULL
);
747 } else if (streq(key
, "TEST")) {
748 mode_t mode
= MODE_INVALID
;
750 if (!isempty(attr
)) {
751 r
= parse_mode(attr
, &mode
);
753 return log_token_error_errno(rules
, r
, "Failed to parse mode '%s': %m", attr
);
755 check_value_format_and_warn(rules
, key
, value
, true);
757 return log_token_invalid_op(rules
, key
);
759 r
= rule_line_add_token(rule_line
, TK_M_TEST
, op
, value
, MODE_TO_PTR(mode
));
760 } else if (streq(key
, "PROGRAM")) {
762 return log_token_invalid_attr(rules
, key
);
763 check_value_format_and_warn(rules
, key
, value
, true);
765 return log_token_invalid_op(rules
, key
);
769 r
= rule_line_add_token(rule_line
, TK_M_PROGRAM
, op
, value
, NULL
);
770 } else if (streq(key
, "IMPORT")) {
772 return log_token_invalid_attr(rules
, key
);
773 check_value_format_and_warn(rules
, key
, value
, true);
775 return log_token_invalid_op(rules
, key
);
779 if (streq(attr
, "file"))
780 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
781 else if (streq(attr
, "program")) {
782 UdevBuiltinCommand cmd
;
784 cmd
= udev_builtin_lookup(value
);
786 log_token_debug(rules
,"Found builtin command '%s' for %s, replacing attribute", value
, key
);
787 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
789 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PROGRAM
, op
, value
, NULL
);
790 } else if (streq(attr
, "builtin")) {
791 UdevBuiltinCommand cmd
;
793 cmd
= udev_builtin_lookup(value
);
795 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
796 "Unknown builtin command: %s", value
);
797 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
798 } else if (streq(attr
, "db"))
799 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_DB
, op
, value
, NULL
);
800 else if (streq(attr
, "cmdline"))
801 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
802 else if (streq(attr
, "parent"))
803 r
= rule_line_add_token(rule_line
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
805 return log_token_invalid_attr(rules
, key
);
806 } else if (streq(key
, "RESULT")) {
808 return log_token_invalid_attr(rules
, key
);
810 return log_token_invalid_op(rules
, key
);
812 r
= rule_line_add_token(rule_line
, TK_M_RESULT
, op
, value
, NULL
);
813 } else if (streq(key
, "OPTIONS")) {
817 return log_token_invalid_attr(rules
, key
);
818 if (is_match
|| op
== OP_REMOVE
)
819 return log_token_invalid_op(rules
, key
);
823 if (streq(value
, "string_escape=none"))
824 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
825 else if (streq(value
, "string_escape=replace"))
826 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
827 else if (streq(value
, "db_persist"))
828 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DB_PERSIST
, op
, NULL
, NULL
);
829 else if (streq(value
, "watch"))
830 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(1));
831 else if (streq(value
, "nowatch"))
832 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_INOTIFY_WATCH
, op
, NULL
, INT_TO_PTR(0));
833 else if ((tmp
= startswith(value
, "static_node=")))
834 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_STATIC_NODE
, op
, tmp
, NULL
);
835 else if ((tmp
= startswith(value
, "link_priority="))) {
838 r
= safe_atoi(tmp
, &prio
);
840 return log_token_error_errno(rules
, r
, "Failed to parse link priority '%s': %m", tmp
);
841 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_DEVLINK_PRIORITY
, op
, NULL
, INT_TO_PTR(prio
));
842 } else if ((tmp
= startswith(value
, "log_level="))) {
845 if (streq(tmp
, "reset"))
848 level
= log_level_from_string(tmp
);
850 return log_token_error_errno(rules
, level
, "Failed to parse log level '%s': %m", tmp
);
852 r
= rule_line_add_token(rule_line
, TK_A_OPTIONS_LOG_LEVEL
, op
, NULL
, INT_TO_PTR(level
));
854 log_token_warning(rules
, "Invalid value for OPTIONS key, ignoring: '%s'", value
);
857 } else if (streq(key
, "OWNER")) {
861 return log_token_invalid_attr(rules
, key
);
862 if (is_match
|| op
== OP_REMOVE
)
863 return log_token_invalid_op(rules
, key
);
865 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
869 if (parse_uid(value
, &uid
) >= 0)
870 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
871 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
872 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
873 r
= rule_resolve_user(rules
, value
, &uid
);
875 return log_token_error_errno(rules
, r
, "Failed to resolve user name '%s': %m", value
);
877 r
= rule_line_add_token(rule_line
, TK_A_OWNER_ID
, op
, NULL
, UID_TO_PTR(uid
));
878 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
879 check_value_format_and_warn(rules
, key
, value
, true);
880 r
= rule_line_add_token(rule_line
, TK_A_OWNER
, op
, value
, NULL
);
882 log_token_debug(rules
, "User name resolution is disabled, ignoring %s=%s", key
, value
);
885 } else if (streq(key
, "GROUP")) {
889 return log_token_invalid_attr(rules
, key
);
890 if (is_match
|| op
== OP_REMOVE
)
891 return log_token_invalid_op(rules
, key
);
893 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
897 if (parse_gid(value
, &gid
) >= 0)
898 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
899 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&&
900 rule_get_substitution_type(value
) == SUBST_TYPE_PLAIN
) {
901 r
= rule_resolve_group(rules
, value
, &gid
);
903 return log_token_error_errno(rules
, r
, "Failed to resolve group name '%s': %m", value
);
905 r
= rule_line_add_token(rule_line
, TK_A_GROUP_ID
, op
, NULL
, GID_TO_PTR(gid
));
906 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
) {
907 check_value_format_and_warn(rules
, key
, value
, true);
908 r
= rule_line_add_token(rule_line
, TK_A_GROUP
, op
, value
, NULL
);
910 log_token_debug(rules
, "Resolving group name is disabled, ignoring %s=%s", key
, value
);
913 } else if (streq(key
, "MODE")) {
917 return log_token_invalid_attr(rules
, key
);
918 if (is_match
|| op
== OP_REMOVE
)
919 return log_token_invalid_op(rules
, key
);
921 log_token_warning(rules
, "%s key takes '=' or ':=' operator, assuming '=', but please fix it.", key
);
925 if (parse_mode(value
, &mode
) >= 0)
926 r
= rule_line_add_token(rule_line
, TK_A_MODE_ID
, op
, NULL
, MODE_TO_PTR(mode
));
928 check_value_format_and_warn(rules
, key
, value
, true);
929 r
= rule_line_add_token(rule_line
, TK_A_MODE
, op
, value
, NULL
);
931 } else if (streq(key
, "SECLABEL")) {
933 return log_token_invalid_attr(rules
, key
);
934 check_value_format_and_warn(rules
, key
, value
, true);
935 if (is_match
|| op
== OP_REMOVE
)
936 return log_token_invalid_op(rules
, key
);
937 if (op
== OP_ASSIGN_FINAL
) {
938 log_token_warning(rules
, "%s key takes '=' or '+=' operator, assuming '=', but please fix it.", key
);
942 r
= rule_line_add_token(rule_line
, TK_A_SECLABEL
, op
, value
, attr
);
943 } else if (streq(key
, "RUN")) {
944 if (is_match
|| op
== OP_REMOVE
)
945 return log_token_invalid_op(rules
, key
);
946 check_value_format_and_warn(rules
, key
, value
, true);
947 if (!attr
|| streq(attr
, "program"))
948 r
= rule_line_add_token(rule_line
, TK_A_RUN_PROGRAM
, op
, value
, NULL
);
949 else if (streq(attr
, "builtin")) {
950 UdevBuiltinCommand cmd
;
952 cmd
= udev_builtin_lookup(value
);
954 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
),
955 "Unknown builtin command '%s', ignoring", value
);
956 r
= rule_line_add_token(rule_line
, TK_A_RUN_BUILTIN
, op
, value
, UDEV_BUILTIN_CMD_TO_PTR(cmd
));
958 return log_token_invalid_attr(rules
, key
);
959 } else if (streq(key
, "GOTO")) {
961 return log_token_invalid_attr(rules
, key
);
963 return log_token_invalid_op(rules
, key
);
964 if (FLAGS_SET(rule_line
->type
, LINE_HAS_GOTO
)) {
965 log_token_warning(rules
, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value
);
969 rule_line
->goto_label
= value
;
970 SET_FLAG(rule_line
->type
, LINE_HAS_GOTO
, true);
972 } else if (streq(key
, "LABEL")) {
974 return log_token_invalid_attr(rules
, key
);
976 return log_token_invalid_op(rules
, key
);
978 rule_line
->label
= value
;
979 SET_FLAG(rule_line
->type
, LINE_HAS_LABEL
, true);
982 return log_token_error_errno(rules
, SYNTHETIC_ERRNO(EINVAL
), "Invalid key '%s'", key
);
989 static UdevRuleOperatorType
parse_operator(const char *op
) {
992 if (startswith(op
, "=="))
994 if (startswith(op
, "!="))
996 if (startswith(op
, "+="))
998 if (startswith(op
, "-="))
1000 if (startswith(op
, "="))
1002 if (startswith(op
, ":="))
1003 return OP_ASSIGN_FINAL
;
1005 return _OP_TYPE_INVALID
;
1008 static int parse_line(char **line
, char **ret_key
, char **ret_attr
, UdevRuleOperatorType
*ret_op
, char **ret_value
) {
1009 char *key_begin
, *key_end
, *attr
, *tmp
;
1010 UdevRuleOperatorType op
;
1019 key_begin
= skip_leading_chars(*line
, WHITESPACE
",");
1021 if (isempty(key_begin
))
1024 for (key_end
= key_begin
; ; key_end
++) {
1025 if (key_end
[0] == '\0')
1027 if (strchr(WHITESPACE
"={", key_end
[0]))
1029 if (strchr("+-!:", key_end
[0]) && key_end
[1] == '=')
1032 if (key_end
[0] == '{') {
1034 tmp
= strchr(attr
, '}');
1043 tmp
= skip_leading_chars(tmp
, NULL
);
1044 op
= parse_operator(tmp
);
1050 tmp
+= op
== OP_ASSIGN
? 1 : 2;
1051 tmp
= skip_leading_chars(tmp
, NULL
);
1052 r
= udev_rule_parse_value(tmp
, ret_value
, line
);
1056 *ret_key
= key_begin
;
1062 static void sort_tokens(UdevRuleLine
*rule_line
) {
1063 UdevRuleToken
*head_old
;
1067 head_old
= TAKE_PTR(rule_line
->tokens
);
1068 rule_line
->current_token
= NULL
;
1070 while (!LIST_IS_EMPTY(head_old
)) {
1071 UdevRuleToken
*t
, *min_token
= NULL
;
1073 LIST_FOREACH(tokens
, t
, head_old
)
1074 if (!min_token
|| min_token
->type
> t
->type
)
1077 LIST_REMOVE(tokens
, head_old
, min_token
);
1078 rule_line_append_token(rule_line
, min_token
);
1082 static int rule_add_line(UdevRules
*rules
, const char *line_str
, unsigned line_nr
) {
1083 _cleanup_(udev_rule_line_freep
) UdevRuleLine
*rule_line
= NULL
;
1084 _cleanup_free_
char *line
= NULL
;
1085 UdevRuleFile
*rule_file
;
1090 assert(rules
->current_file
);
1093 rule_file
= rules
->current_file
;
1095 if (isempty(line_str
))
1098 /* We use memdup_suffix0() here, since we want to add a second NUL byte to the end, since possibly
1099 * some parsers might turn this into a "nulstr", which requires an extra NUL at the end. */
1100 line
= memdup_suffix0(line_str
, strlen(line_str
) + 1);
1104 rule_line
= new(UdevRuleLine
, 1);
1108 *rule_line
= (UdevRuleLine
) {
1109 .line
= TAKE_PTR(line
),
1110 .line_number
= line_nr
,
1111 .rule_file
= rule_file
,
1114 if (rule_file
->current_line
)
1115 LIST_APPEND(rule_lines
, rule_file
->current_line
, rule_line
);
1117 LIST_APPEND(rule_lines
, rule_file
->rule_lines
, rule_line
);
1119 rule_file
->current_line
= rule_line
;
1121 for (p
= rule_line
->line
; !isempty(p
); ) {
1122 char *key
, *attr
, *value
;
1123 UdevRuleOperatorType op
;
1125 r
= parse_line(&p
, &key
, &attr
, &op
, &value
);
1127 return log_token_error_errno(rules
, r
, "Invalid key/value pair, ignoring.");
1131 r
= parse_token(rules
, key
, attr
, op
, value
);
1136 if (rule_line
->type
== 0) {
1137 log_token_warning(rules
, "The line takes no effect, ignoring.");
1141 sort_tokens(rule_line
);
1142 TAKE_PTR(rule_line
);
1146 static void rule_resolve_goto(UdevRuleFile
*rule_file
) {
1147 UdevRuleLine
*line
, *line_next
, *i
;
1151 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1152 LIST_FOREACH_SAFE(rule_lines
, line
, line_next
, rule_file
->rule_lines
) {
1153 if (!FLAGS_SET(line
->type
, LINE_HAS_GOTO
))
1156 LIST_FOREACH_AFTER(rule_lines
, i
, line
)
1157 if (streq_ptr(i
->label
, line
->goto_label
)) {
1158 line
->goto_line
= i
;
1162 if (!line
->goto_line
) {
1163 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
1164 rule_file
->filename
, line
->line_number
, line
->goto_label
);
1166 SET_FLAG(line
->type
, LINE_HAS_GOTO
, false);
1167 line
->goto_label
= NULL
;
1169 if ((line
->type
& ~LINE_HAS_LABEL
) == 0) {
1170 log_notice("%s:%u: The line takes no effect any more, dropping",
1171 rule_file
->filename
, line
->line_number
);
1172 if (line
->type
== LINE_HAS_LABEL
)
1173 udev_rule_line_clear_tokens(line
);
1175 udev_rule_line_free(line
);
1181 int udev_rules_parse_file(UdevRules
*rules
, const char *filename
) {
1182 _cleanup_free_
char *continuation
= NULL
, *name
= NULL
;
1183 _cleanup_fclose_
FILE *f
= NULL
;
1184 UdevRuleFile
*rule_file
;
1185 bool ignore_line
= false;
1186 unsigned line_nr
= 0;
1189 f
= fopen(filename
, "re");
1191 if (errno
== ENOENT
)
1197 (void) fd_warn_permissions(filename
, fileno(f
));
1199 if (null_or_empty_fd(fileno(f
))) {
1200 log_debug("Skipping empty file: %s", filename
);
1204 log_debug("Reading rules file: %s", filename
);
1206 name
= strdup(filename
);
1210 rule_file
= new(UdevRuleFile
, 1);
1214 *rule_file
= (UdevRuleFile
) {
1215 .filename
= TAKE_PTR(name
),
1218 if (rules
->current_file
)
1219 LIST_APPEND(rule_files
, rules
->current_file
, rule_file
);
1221 LIST_APPEND(rule_files
, rules
->rule_files
, rule_file
);
1223 rules
->current_file
= rule_file
;
1226 _cleanup_free_
char *buf
= NULL
;
1230 r
= read_line(f
, UDEV_LINE_SIZE
, &buf
);
1237 line
= skip_leading_chars(buf
, NULL
);
1244 if (continuation
&& !ignore_line
) {
1245 if (strlen(continuation
) + len
>= UDEV_LINE_SIZE
)
1248 if (!strextend(&continuation
, line
))
1252 line
= continuation
;
1257 if (len
> 0 && line
[len
- 1] == '\\') {
1261 line
[len
- 1] = '\0';
1262 if (!continuation
) {
1263 continuation
= strdup(line
);
1272 log_error("%s:%u: Line is too long, ignored", filename
, line_nr
);
1274 (void) rule_add_line(rules
, line
, line_nr
);
1276 continuation
= mfree(continuation
);
1277 ignore_line
= false;
1280 rule_resolve_goto(rule_file
);
1284 UdevRules
* udev_rules_new(ResolveNameTiming resolve_name_timing
) {
1285 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1287 UdevRules
*rules
= new(UdevRules
, 1);
1291 *rules
= (UdevRules
) {
1292 .resolve_name_timing
= resolve_name_timing
,
1298 int udev_rules_load(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1299 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1300 _cleanup_strv_free_
char **files
= NULL
;
1304 rules
= udev_rules_new(resolve_name_timing
);
1308 (void) udev_rules_check_timestamp(rules
);
1310 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1312 return log_debug_errno(r
, "Failed to enumerate rules files: %m");
1314 STRV_FOREACH(f
, files
) {
1315 r
= udev_rules_parse_file(rules
, *f
);
1317 log_debug_errno(r
, "Failed to read rules file %s, ignoring: %m", *f
);
1320 *ret_rules
= TAKE_PTR(rules
);
1324 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1328 return paths_check_timestamp(RULES_DIRS
, &rules
->dirs_ts_usec
, true);
1331 static bool token_match_string(UdevRuleToken
*token
, const char *str
) {
1332 const char *i
, *value
;
1336 assert(token
->value
);
1337 assert(token
->type
< _TK_M_MAX
);
1339 str
= strempty(str
);
1340 value
= token
->value
;
1342 switch (token
->match_type
) {
1343 case MATCH_TYPE_EMPTY
:
1344 match
= isempty(str
);
1346 case MATCH_TYPE_SUBSYSTEM
:
1347 match
= STR_IN_SET(str
, "subsystem", "class", "bus");
1349 case MATCH_TYPE_PLAIN_WITH_EMPTY
:
1355 case MATCH_TYPE_PLAIN
:
1356 NULSTR_FOREACH(i
, value
)
1357 if (streq(i
, str
)) {
1362 case MATCH_TYPE_GLOB_WITH_EMPTY
:
1368 case MATCH_TYPE_GLOB
:
1369 NULSTR_FOREACH(i
, value
)
1370 if ((fnmatch(i
, str
, 0) == 0)) {
1376 assert_not_reached("Invalid match type");
1379 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1382 static bool token_match_attr(UdevRuleToken
*token
, sd_device
*dev
, UdevEvent
*event
) {
1383 char nbuf
[UDEV_NAME_SIZE
], vbuf
[UDEV_NAME_SIZE
];
1384 const char *name
, *value
;
1392 switch (token
->attr_subst_type
) {
1393 case SUBST_TYPE_FORMAT
:
1394 (void) udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1397 case SUBST_TYPE_PLAIN
:
1398 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1401 case SUBST_TYPE_SUBSYS
:
1402 if (udev_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1407 assert_not_reached("Invalid attribute substitution type");
1410 /* remove trailing whitespace, if not asked to match for it */
1411 if (token
->attr_match_remove_trailing_whitespace
) {
1412 if (value
!= vbuf
) {
1413 strscpy(vbuf
, sizeof(vbuf
), value
);
1417 delete_trailing_chars(vbuf
, NULL
);
1420 return token_match_string(token
, value
);
1423 static int get_property_from_string(char *line
, char **ret_key
, char **ret_value
) {
1432 key
= skip_leading_chars(line
, NULL
);
1434 /* comment or empty line */
1435 if (IN_SET(key
[0], '#', '\0')) {
1436 *ret_key
= *ret_value
= NULL
;
1440 /* split key/value */
1441 val
= strchr(key
, '=');
1446 key
= strstrip(key
);
1450 val
= strstrip(val
);
1455 if (IN_SET(val
[0], '"', '\'')) {
1457 if (len
== 1 || val
[len
-1] != val
[0])
1468 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
1469 const char *key
, *val
;
1476 r
= sd_device_get_parent(dev
, &parent
);
1482 FOREACH_DEVICE_PROPERTY(parent
, key
, val
) {
1483 if (fnmatch(filter
, key
, 0) != 0)
1485 r
= device_add_property(dev
, key
, val
);
1493 static int attr_subst_subdir(char attr
[static UDEV_PATH_SIZE
]) {
1494 _cleanup_closedir_
DIR *dir
= NULL
;
1495 struct dirent
*dent
;
1496 char buf
[UDEV_PATH_SIZE
], *p
;
1502 tail
= strstr(attr
, "/*/");
1506 len
= tail
- attr
+ 1; /* include slash at the end */
1507 tail
+= 2; /* include slash at the beginning */
1511 size
-= strnpcpy(&p
, size
, attr
, len
);
1517 FOREACH_DIRENT_ALL(dent
, dir
, break) {
1518 if (dent
->d_name
[0] == '.')
1521 strscpyl(p
, size
, dent
->d_name
, tail
, NULL
);
1522 if (faccessat(dirfd(dir
), p
, F_OK
, 0) < 0)
1532 static int udev_rule_apply_token_to_event(
1536 usec_t timeout_usec
,
1538 Hashmap
*properties_list
) {
1540 UdevRuleToken
*token
;
1541 char buf
[UDEV_PATH_SIZE
];
1551 /* This returns the following values:
1552 * 0 on the current token does not match the event,
1553 * 1 on the current token matches the event, and
1554 * negative errno on some critical errors. */
1556 token
= rules
->current_file
->current_line
->current_token
;
1558 switch (token
->type
) {
1560 sd_device_action_t a
;
1562 r
= sd_device_get_action(dev
, &a
);
1564 return log_rule_error_errno(dev
, rules
, r
, "Failed to get uevent action type: %m");
1566 return token_match_string(token
, device_action_to_string(a
));
1569 r
= sd_device_get_devpath(dev
, &val
);
1571 return log_rule_error_errno(dev
, rules
, r
, "Failed to get devpath: %m");
1573 return token_match_string(token
, val
);
1575 case TK_M_PARENTS_KERNEL
:
1576 r
= sd_device_get_sysname(dev
, &val
);
1578 return log_rule_error_errno(dev
, rules
, r
, "Failed to get sysname: %m");
1580 return token_match_string(token
, val
);
1582 FOREACH_DEVICE_DEVLINK(dev
, val
)
1583 if (token_match_string(token
, strempty(startswith(val
, "/dev/"))))
1584 return token
->op
== OP_MATCH
;
1585 return token
->op
== OP_NOMATCH
;
1587 return token_match_string(token
, event
->name
);
1589 if (sd_device_get_property_value(dev
, token
->data
, &val
) < 0)
1590 val
= hashmap_get(properties_list
, token
->data
);
1592 return token_match_string(token
, val
);
1594 const char *k
= token
->data
;
1596 if (streq(k
, "arch"))
1597 val
= architecture_to_string(uname_architecture());
1598 else if (streq(k
, "virt"))
1599 val
= virtualization_to_string(detect_virtualization());
1601 assert_not_reached("Invalid CONST key");
1602 return token_match_string(token
, val
);
1605 case TK_M_PARENTS_TAG
:
1606 FOREACH_DEVICE_TAG(dev
, val
)
1607 if (token_match_string(token
, val
))
1608 return token
->op
== OP_MATCH
;
1609 return token
->op
== OP_NOMATCH
;
1610 case TK_M_SUBSYSTEM
:
1611 case TK_M_PARENTS_SUBSYSTEM
:
1612 r
= sd_device_get_subsystem(dev
, &val
);
1616 return log_rule_error_errno(dev
, rules
, r
, "Failed to get subsystem: %m");
1618 return token_match_string(token
, val
);
1620 case TK_M_PARENTS_DRIVER
:
1621 r
= sd_device_get_driver(dev
, &val
);
1625 return log_rule_error_errno(dev
, rules
, r
, "Failed to get driver: %m");
1627 return token_match_string(token
, val
);
1629 case TK_M_PARENTS_ATTR
:
1630 return token_match_attr(token
, dev
, event
);
1632 _cleanup_free_
char *value
= NULL
;
1634 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false);
1635 r
= sysctl_read(sysctl_normalize(buf
), &value
);
1636 if (r
< 0 && r
!= -ENOENT
)
1637 return log_rule_error_errno(dev
, rules
, r
, "Failed to read sysctl '%s': %m", buf
);
1639 return token_match_string(token
, strstrip(value
));
1642 mode_t mode
= PTR_TO_MODE(token
->data
);
1643 struct stat statbuf
;
1645 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1646 if (!path_is_absolute(buf
) &&
1647 udev_resolve_subsys_kernel(buf
, buf
, sizeof(buf
), false) < 0) {
1648 char tmp
[UDEV_PATH_SIZE
];
1650 r
= sd_device_get_syspath(dev
, &val
);
1652 return log_rule_error_errno(dev
, rules
, r
, "Failed to get syspath: %m");
1654 strscpy(tmp
, sizeof(tmp
), buf
);
1655 strscpyl(buf
, sizeof(buf
), val
, "/", tmp
, NULL
);
1658 r
= attr_subst_subdir(buf
);
1660 return token
->op
== OP_NOMATCH
;
1662 return log_rule_error_errno(dev
, rules
, r
, "Failed to test for the existence of '%s': %m", buf
);
1664 if (stat(buf
, &statbuf
) < 0)
1665 return token
->op
== OP_NOMATCH
;
1667 if (mode
== MODE_INVALID
)
1668 return token
->op
== OP_MATCH
;
1670 match
= (statbuf
.st_mode
& mode
) > 0;
1671 return token
->op
== (match
? OP_MATCH
: OP_NOMATCH
);
1673 case TK_M_PROGRAM
: {
1674 char result
[UDEV_LINE_SIZE
];
1676 event
->program_result
= mfree(event
->program_result
);
1677 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1678 log_rule_debug(dev
, rules
, "Running PROGRAM '%s'", buf
);
1680 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof(result
));
1683 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute \"%s\": %m", buf
);
1684 else /* returned value is positive when program fails */
1685 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error)", buf
, r
);
1686 return token
->op
== OP_NOMATCH
;
1689 delete_trailing_chars(result
, "\n");
1690 count
= udev_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1692 log_rule_debug(dev
, rules
, "Replaced %zu character(s) in result of \"%s\"",
1695 event
->program_result
= strdup(result
);
1696 return token
->op
== OP_MATCH
;
1698 case TK_M_IMPORT_FILE
: {
1699 _cleanup_fclose_
FILE *f
= NULL
;
1701 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1702 log_rule_debug(dev
, rules
, "Importing properties from '%s'", buf
);
1704 f
= fopen(buf
, "re");
1706 if (errno
!= ENOENT
)
1707 return log_rule_error_errno(dev
, rules
, errno
,
1708 "Failed to open '%s': %m", buf
);
1709 return token
->op
== OP_NOMATCH
;
1713 _cleanup_free_
char *line
= NULL
;
1716 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1718 log_rule_debug_errno(dev
, rules
, r
,
1719 "Failed to read '%s', ignoring: %m", buf
);
1720 return token
->op
== OP_NOMATCH
;
1725 r
= get_property_from_string(line
, &key
, &value
);
1727 log_rule_debug_errno(dev
, rules
, r
,
1728 "Failed to parse key and value from '%s', ignoring: %m",
1735 r
= device_add_property(dev
, key
, value
);
1737 return log_rule_error_errno(dev
, rules
, r
,
1738 "Failed to add property %s=%s: %m",
1742 return token
->op
== OP_MATCH
;
1744 case TK_M_IMPORT_PROGRAM
: {
1745 _cleanup_strv_free_
char **lines
= NULL
;
1746 char result
[UDEV_LINE_SIZE
], **line
;
1748 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1749 log_rule_debug(dev
, rules
, "Importing properties from results of '%s'", buf
);
1751 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, true, buf
, result
, sizeof result
);
1754 log_rule_warning_errno(dev
, rules
, r
, "Failed to execute '%s', ignoring: %m", buf
);
1755 else /* returned value is positive when program fails */
1756 log_rule_debug(dev
, rules
, "Command \"%s\" returned %d (error), ignoring", buf
, r
);
1757 return token
->op
== OP_NOMATCH
;
1760 r
= strv_split_newlines_full(&lines
, result
, EXTRACT_RETAIN_ESCAPE
);
1762 log_rule_warning_errno(dev
, rules
, r
,
1763 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf
);
1765 STRV_FOREACH(line
, lines
) {
1768 r
= get_property_from_string(*line
, &key
, &value
);
1770 log_rule_debug_errno(dev
, rules
, r
,
1771 "Failed to parse key and value from '%s', ignoring: %m",
1778 r
= device_add_property(dev
, key
, value
);
1780 return log_rule_error_errno(dev
, rules
, r
,
1781 "Failed to add property %s=%s: %m",
1785 return token
->op
== OP_MATCH
;
1787 case TK_M_IMPORT_BUILTIN
: {
1788 UdevBuiltinCommand cmd
= PTR_TO_UDEV_BUILTIN_CMD(token
->data
);
1789 assert(cmd
>= 0 && cmd
< _UDEV_BUILTIN_MAX
);
1790 unsigned mask
= 1U << (int) cmd
;
1792 if (udev_builtin_run_once(cmd
)) {
1793 /* check if we ran already */
1794 if (event
->builtin_run
& mask
) {
1795 log_rule_debug(dev
, rules
, "Skipping builtin '%s' in IMPORT key",
1796 udev_builtin_name(cmd
));
1797 /* return the result from earlier run */
1798 return token
->op
== (event
->builtin_ret
& mask
? OP_NOMATCH
: OP_MATCH
);
1801 event
->builtin_run
|= mask
;
1804 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1805 log_rule_debug(dev
, rules
, "Importing properties from results of builtin command '%s'", buf
);
1807 r
= udev_builtin_run(dev
, cmd
, buf
, false);
1809 /* remember failure */
1810 log_rule_debug_errno(dev
, rules
, r
, "Failed to run builtin '%s': %m", buf
);
1811 event
->builtin_ret
|= mask
;
1813 return token
->op
== (r
>= 0 ? OP_MATCH
: OP_NOMATCH
);
1815 case TK_M_IMPORT_DB
: {
1816 if (!event
->dev_db_clone
)
1817 return token
->op
== OP_NOMATCH
;
1818 r
= sd_device_get_property_value(event
->dev_db_clone
, token
->value
, &val
);
1820 return token
->op
== OP_NOMATCH
;
1822 return log_rule_error_errno(dev
, rules
, r
,
1823 "Failed to get property '%s' from database: %m",
1826 r
= device_add_property(dev
, token
->value
, val
);
1828 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1830 return token
->op
== OP_MATCH
;
1832 case TK_M_IMPORT_CMDLINE
: {
1833 _cleanup_free_
char *value
= NULL
;
1835 r
= proc_cmdline_get_key(token
->value
, PROC_CMDLINE_VALUE_OPTIONAL
|PROC_CMDLINE_IGNORE_EFI_OPTIONS
, &value
);
1837 return log_rule_error_errno(dev
, rules
, r
,
1838 "Failed to read '%s' option from /proc/cmdline: %m",
1841 return token
->op
== OP_NOMATCH
;
1843 r
= device_add_property(dev
, token
->value
, value
?: "1");
1845 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m",
1846 token
->value
, value
?: "1");
1847 return token
->op
== OP_MATCH
;
1849 case TK_M_IMPORT_PARENT
: {
1850 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
1851 r
= import_parent_into_properties(dev
, buf
);
1853 return log_rule_error_errno(dev
, rules
, r
,
1854 "Failed to import properties '%s' from parent: %m",
1856 return token
->op
== (r
> 0 ? OP_MATCH
: OP_NOMATCH
);
1859 return token_match_string(token
, event
->program_result
);
1860 case TK_A_OPTIONS_STRING_ESCAPE_NONE
:
1861 event
->esc
= ESCAPE_NONE
;
1863 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE
:
1864 event
->esc
= ESCAPE_REPLACE
;
1866 case TK_A_OPTIONS_DB_PERSIST
:
1867 device_set_db_persist(dev
);
1869 case TK_A_OPTIONS_INOTIFY_WATCH
:
1870 if (event
->inotify_watch_final
)
1872 if (token
->op
== OP_ASSIGN_FINAL
)
1873 event
->inotify_watch_final
= true;
1875 event
->inotify_watch
= token
->data
;
1877 case TK_A_OPTIONS_DEVLINK_PRIORITY
:
1878 device_set_devlink_priority(dev
, PTR_TO_INT(token
->data
));
1880 case TK_A_OPTIONS_LOG_LEVEL
: {
1881 int level
= PTR_TO_INT(token
->data
);
1884 level
= event
->default_log_level
;
1886 log_set_max_level(level
);
1888 if (level
== LOG_DEBUG
&& !event
->log_level_was_debug
) {
1889 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
1890 log_device_uevent(dev
, "The log level is changed to 'debug' while processing device");
1891 event
->log_level_was_debug
= true;
1897 char owner
[UDEV_NAME_SIZE
];
1898 const char *ow
= owner
;
1900 if (event
->owner_final
)
1902 if (token
->op
== OP_ASSIGN_FINAL
)
1903 event
->owner_final
= true;
1905 (void) udev_event_apply_format(event
, token
->value
, owner
, sizeof(owner
), false);
1906 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
1908 log_unknown_owner(dev
, rules
, r
, "user", owner
);
1910 log_rule_debug(dev
, rules
, "OWNER %s(%u)", owner
, event
->uid
);
1914 char group
[UDEV_NAME_SIZE
];
1915 const char *gr
= group
;
1917 if (event
->group_final
)
1919 if (token
->op
== OP_ASSIGN_FINAL
)
1920 event
->group_final
= true;
1922 (void) udev_event_apply_format(event
, token
->value
, group
, sizeof(group
), false);
1923 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
1925 log_unknown_owner(dev
, rules
, r
, "group", group
);
1927 log_rule_debug(dev
, rules
, "GROUP %s(%u)", group
, event
->gid
);
1931 char mode_str
[UDEV_NAME_SIZE
];
1933 if (event
->mode_final
)
1935 if (token
->op
== OP_ASSIGN_FINAL
)
1936 event
->mode_final
= true;
1938 (void) udev_event_apply_format(event
, token
->value
, mode_str
, sizeof(mode_str
), false);
1939 r
= parse_mode(mode_str
, &event
->mode
);
1941 log_rule_error_errno(dev
, rules
, r
, "Failed to parse mode '%s', ignoring: %m", mode_str
);
1943 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
1947 if (event
->owner_final
)
1949 if (token
->op
== OP_ASSIGN_FINAL
)
1950 event
->owner_final
= true;
1953 event
->uid
= PTR_TO_UID(token
->data
);
1954 log_rule_debug(dev
, rules
, "OWNER %u", event
->uid
);
1957 if (event
->group_final
)
1959 if (token
->op
== OP_ASSIGN_FINAL
)
1960 event
->group_final
= true;
1963 event
->gid
= PTR_TO_GID(token
->data
);
1964 log_rule_debug(dev
, rules
, "GROUP %u", event
->gid
);
1967 if (event
->mode_final
)
1969 if (token
->op
== OP_ASSIGN_FINAL
)
1970 event
->mode_final
= true;
1973 event
->mode
= PTR_TO_MODE(token
->data
);
1974 log_rule_debug(dev
, rules
, "MODE %#o", event
->mode
);
1976 case TK_A_SECLABEL
: {
1977 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
1978 char label_str
[UDEV_LINE_SIZE
] = {};
1980 name
= strdup(token
->data
);
1984 (void) udev_event_apply_format(event
, token
->value
, label_str
, sizeof(label_str
), false);
1985 if (!isempty(label_str
))
1986 label
= strdup(label_str
);
1988 label
= strdup(token
->value
);
1992 if (token
->op
== OP_ASSIGN
)
1993 ordered_hashmap_clear_free_free(event
->seclabel_list
);
1995 r
= ordered_hashmap_ensure_put(&event
->seclabel_list
, NULL
, name
, label
);
1999 return log_rule_error_errno(dev
, rules
, r
, "Failed to store SECLABEL{%s}='%s': %m", name
, label
);;
2001 log_rule_debug(dev
, rules
, "SECLABEL{%s}='%s'", name
, label
);
2008 const char *name
= token
->data
;
2009 char value_new
[UDEV_NAME_SIZE
], *p
= value_new
;
2010 size_t l
= sizeof(value_new
);
2012 if (isempty(token
->value
)) {
2013 if (token
->op
== OP_ADD
)
2015 r
= device_add_property(dev
, name
, NULL
);
2017 return log_rule_error_errno(dev
, rules
, r
, "Failed to remove property '%s': %m", name
);
2021 if (token
->op
== OP_ADD
&&
2022 sd_device_get_property_value(dev
, name
, &val
) >= 0)
2023 l
= strpcpyl(&p
, l
, val
, " ", NULL
);
2025 (void) udev_event_apply_format(event
, token
->value
, p
, l
, false);
2027 r
= device_add_property(dev
, name
, value_new
);
2029 return log_rule_error_errno(dev
, rules
, r
, "Failed to add property '%s=%s': %m", name
, value_new
);
2033 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
2034 if (token
->op
== OP_ASSIGN
)
2035 device_cleanup_tags(dev
);
2037 if (buf
[strspn(buf
, ALPHANUMERICAL
"-_")] != '\0') {
2038 log_rule_error(dev
, rules
, "Invalid tag name '%s', ignoring", buf
);
2041 if (token
->op
== OP_REMOVE
)
2042 device_remove_tag(dev
, buf
);
2044 r
= device_add_tag(dev
, buf
, true);
2046 return log_rule_error_errno(dev
, rules
, r
, "Failed to add tag '%s': %m", buf
);
2051 if (event
->name_final
)
2053 if (token
->op
== OP_ASSIGN_FINAL
)
2054 event
->name_final
= true;
2056 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
2057 if (IN_SET(event
->esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2058 count
= udev_replace_chars(buf
, "/");
2060 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of NAME=\"%s\"",
2061 count
, token
->value
);
2063 if (sd_device_get_devnum(dev
, NULL
) >= 0 &&
2064 (sd_device_get_devname(dev
, &val
) < 0 ||
2065 !streq_ptr(buf
, path_startswith(val
, "/dev/")))) {
2066 log_rule_error(dev
, rules
,
2067 "Kernel device nodes cannot be renamed, ignoring NAME=\"%s\"; please fix it.",
2071 r
= free_and_strdup_warn(&event
->name
, buf
);
2075 log_rule_debug(dev
, rules
, "NAME '%s'", event
->name
);
2078 case TK_A_DEVLINK
: {
2081 if (event
->devlink_final
)
2083 if (sd_device_get_devnum(dev
, NULL
) < 0)
2085 if (token
->op
== OP_ASSIGN_FINAL
)
2086 event
->devlink_final
= true;
2087 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2088 device_cleanup_devlinks(dev
);
2090 /* allow multiple symlinks separated by spaces */
2091 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), event
->esc
!= ESCAPE_NONE
);
2092 if (event
->esc
== ESCAPE_UNSET
)
2093 count
= udev_replace_chars(buf
, "/ ");
2094 else if (event
->esc
== ESCAPE_REPLACE
)
2095 count
= udev_replace_chars(buf
, "/");
2099 log_rule_debug(dev
, rules
, "Replaced %zu character(s) from result of LINK", count
);
2101 p
= skip_leading_chars(buf
, NULL
);
2102 while (!isempty(p
)) {
2103 char filename
[UDEV_PATH_SIZE
], *next
;
2105 next
= strchr(p
, ' ');
2108 next
= skip_leading_chars(next
, NULL
);
2111 strscpyl(filename
, sizeof(filename
), "/dev/", p
, NULL
);
2112 r
= device_add_devlink(dev
, filename
);
2114 return log_rule_error_errno(dev
, rules
, r
, "Failed to add devlink '%s': %m", filename
);
2116 log_rule_debug(dev
, rules
, "LINK '%s'", p
);
2122 const char *key_name
= token
->data
;
2123 char value
[UDEV_NAME_SIZE
];
2125 if (udev_resolve_subsys_kernel(key_name
, buf
, sizeof(buf
), false) < 0 &&
2126 sd_device_get_syspath(dev
, &val
) >= 0)
2127 strscpyl(buf
, sizeof(buf
), val
, "/", key_name
, NULL
);
2129 r
= attr_subst_subdir(buf
);
2131 log_rule_error_errno(dev
, rules
, r
, "Could not find file matches '%s', ignoring: %m", buf
);
2134 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false);
2136 log_rule_debug(dev
, rules
, "ATTR '%s' writing '%s'", buf
, value
);
2137 r
= write_string_file(buf
, value
,
2138 WRITE_STRING_FILE_VERIFY_ON_FAILURE
|
2139 WRITE_STRING_FILE_DISABLE_BUFFER
|
2140 WRITE_STRING_FILE_AVOID_NEWLINE
|
2141 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE
);
2143 log_rule_error_errno(dev
, rules
, r
, "Failed to write ATTR{%s}, ignoring: %m", buf
);
2147 char value
[UDEV_NAME_SIZE
];
2149 (void) udev_event_apply_format(event
, token
->data
, buf
, sizeof(buf
), false);
2150 (void) udev_event_apply_format(event
, token
->value
, value
, sizeof(value
), false);
2151 sysctl_normalize(buf
);
2152 log_rule_debug(dev
, rules
, "SYSCTL '%s' writing '%s'", buf
, value
);
2153 r
= sysctl_write(buf
, value
);
2155 log_rule_error_errno(dev
, rules
, r
, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf
, value
);
2158 case TK_A_RUN_BUILTIN
:
2159 case TK_A_RUN_PROGRAM
: {
2160 _cleanup_free_
char *cmd
= NULL
;
2162 if (event
->run_final
)
2164 if (token
->op
== OP_ASSIGN_FINAL
)
2165 event
->run_final
= true;
2167 if (IN_SET(token
->op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2168 ordered_hashmap_clear_free_key(event
->run_list
);
2170 (void) udev_event_apply_format(event
, token
->value
, buf
, sizeof(buf
), false);
2176 r
= ordered_hashmap_ensure_put(&event
->run_list
, NULL
, cmd
, token
->data
);
2180 return log_rule_error_errno(dev
, rules
, r
, "Failed to store command '%s': %m", cmd
);
2184 log_rule_debug(dev
, rules
, "RUN '%s'", token
->value
);
2187 case TK_A_OPTIONS_STATIC_NODE
:
2188 /* do nothing for events. */
2191 assert_not_reached("Invalid token type");
2197 static bool token_is_for_parents(UdevRuleToken
*token
) {
2198 return token
->type
>= TK_M_PARENTS_KERNEL
&& token
->type
<= TK_M_PARENTS_TAG
;
2201 static int udev_rule_apply_parent_token_to_event(
2204 int timeout_signal
) {
2207 UdevRuleToken
*head
;
2210 line
= rules
->current_file
->current_line
;
2211 head
= rules
->current_file
->current_line
->current_token
;
2212 event
->dev_parent
= event
->dev
;
2214 LIST_FOREACH(tokens
, line
->current_token
, head
) {
2215 if (!token_is_for_parents(line
->current_token
))
2216 return true; /* All parent tokens match. */
2217 r
= udev_rule_apply_token_to_event(rules
, event
->dev_parent
, event
, 0, timeout_signal
, NULL
);
2223 if (!line
->current_token
)
2224 /* All parent tokens match. But no assign tokens in the line. Hmm... */
2227 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
2228 event
->dev_parent
= NULL
;
2234 static int udev_rule_apply_line_to_event(
2237 usec_t timeout_usec
,
2239 Hashmap
*properties_list
,
2240 UdevRuleLine
**next_line
) {
2242 UdevRuleLine
*line
= rules
->current_file
->current_line
;
2243 UdevRuleLineType mask
= LINE_HAS_GOTO
| LINE_UPDATE_SOMETHING
;
2244 UdevRuleToken
*token
, *next_token
;
2245 bool parents_done
= false;
2246 sd_device_action_t action
;
2249 r
= sd_device_get_action(event
->dev
, &action
);
2253 if (action
!= SD_DEVICE_REMOVE
) {
2254 if (sd_device_get_devnum(event
->dev
, NULL
) >= 0)
2255 mask
|= LINE_HAS_DEVLINK
;
2257 if (sd_device_get_ifindex(event
->dev
, NULL
) >= 0)
2258 mask
|= LINE_HAS_NAME
;
2261 if ((line
->type
& mask
) == 0)
2264 event
->esc
= ESCAPE_UNSET
;
2266 DEVICE_TRACE_POINT(rules_apply_line
, event
->dev
, line
->rule_file
->filename
, line
->line_number
);
2268 LIST_FOREACH_SAFE(tokens
, token
, next_token
, line
->tokens
) {
2269 line
->current_token
= token
;
2271 if (token_is_for_parents(token
)) {
2275 r
= udev_rule_apply_parent_token_to_event(rules
, event
, timeout_signal
);
2279 parents_done
= true;
2283 r
= udev_rule_apply_token_to_event(rules
, event
->dev
, event
, timeout_usec
, timeout_signal
, properties_list
);
2288 if (line
->goto_line
)
2289 *next_line
= line
->goto_line
;
2294 int udev_rules_apply_to_event(
2297 usec_t timeout_usec
,
2299 Hashmap
*properties_list
) {
2302 UdevRuleLine
*next_line
;
2308 LIST_FOREACH(rule_files
, file
, rules
->rule_files
) {
2309 rules
->current_file
= file
;
2310 LIST_FOREACH_SAFE(rule_lines
, file
->current_line
, next_line
, file
->rule_lines
) {
2311 r
= udev_rule_apply_line_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
, &next_line
);
2320 static int apply_static_dev_perms(const char *devnode
, uid_t uid
, gid_t gid
, mode_t mode
, char **tags
) {
2321 char device_node
[UDEV_PATH_SIZE
], tags_dir
[UDEV_PATH_SIZE
], tag_symlink
[UDEV_PATH_SIZE
];
2322 _cleanup_free_
char *unescaped_filename
= NULL
;
2329 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
&& !tags
)
2332 strscpyl(device_node
, sizeof(device_node
), "/dev/", devnode
, NULL
);
2333 if (stat(device_node
, &stats
) < 0) {
2334 if (errno
!= ENOENT
)
2335 return log_error_errno(errno
, "Failed to stat %s: %m", device_node
);
2339 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
)) {
2340 log_warning("%s is neither block nor character device, ignoring.", device_node
);
2344 if (!strv_isempty(tags
)) {
2345 unescaped_filename
= xescape(devnode
, "/.");
2346 if (!unescaped_filename
)
2350 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2351 STRV_FOREACH(t
, tags
) {
2352 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2353 r
= mkdir_p(tags_dir
, 0755);
2355 return log_error_errno(r
, "Failed to create %s: %m", tags_dir
);
2357 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2358 r
= symlink(device_node
, tag_symlink
);
2359 if (r
< 0 && errno
!= EEXIST
)
2360 return log_error_errno(errno
, "Failed to create symlink %s -> %s: %m",
2361 tag_symlink
, device_node
);
2364 /* don't touch the permissions if only the tags were set */
2365 if (uid
== UID_INVALID
&& gid
== GID_INVALID
&& mode
== MODE_INVALID
)
2368 if (mode
== MODE_INVALID
)
2369 mode
= gid_is_valid(gid
) ? 0660 : 0600;
2370 if (!uid_is_valid(uid
))
2372 if (!gid_is_valid(gid
))
2375 r
= chmod_and_chown(device_node
, mode
, uid
, gid
);
2379 return log_error_errno(r
, "Failed to chown '%s' %u %u: %m", device_node
, uid
, gid
);
2381 log_debug("chown '%s' %u:%u with mode %#o", device_node
, uid
, gid
, mode
);
2383 (void) utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2387 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine
*rule_line
) {
2388 UdevRuleToken
*token
;
2389 _cleanup_strv_free_
char **tags
= NULL
;
2390 uid_t uid
= UID_INVALID
;
2391 gid_t gid
= GID_INVALID
;
2392 mode_t mode
= MODE_INVALID
;
2397 if (!FLAGS_SET(rule_line
->type
, LINE_HAS_STATIC_NODE
))
2400 LIST_FOREACH(tokens
, token
, rule_line
->tokens
)
2401 if (token
->type
== TK_A_OWNER_ID
)
2402 uid
= PTR_TO_UID(token
->data
);
2403 else if (token
->type
== TK_A_GROUP_ID
)
2404 gid
= PTR_TO_GID(token
->data
);
2405 else if (token
->type
== TK_A_MODE_ID
)
2406 mode
= PTR_TO_MODE(token
->data
);
2407 else if (token
->type
== TK_A_TAG
) {
2408 r
= strv_extend(&tags
, token
->value
);
2411 } else if (token
->type
== TK_A_OPTIONS_STATIC_NODE
) {
2412 r
= apply_static_dev_perms(token
->value
, uid
, gid
, mode
, tags
);
2420 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2427 LIST_FOREACH(rule_files
, file
, rules
->rule_files
)
2428 LIST_FOREACH(rule_lines
, line
, file
->rule_lines
) {
2429 r
= udev_rule_line_apply_static_dev_perms(line
);