]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-rules.c
Merge pull request #20833 from pdmorrow/onfailure_env
[thirdparty/systemd.git] / src / udev / udev-rules.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <ctype.h>
4
5 #include "alloc-util.h"
6 #include "architecture.h"
7 #include "conf-files.h"
8 #include "def.h"
9 #include "device-private.h"
10 #include "device-util.h"
11 #include "dirent-util.h"
12 #include "escape.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "format-util.h"
16 #include "fs-util.h"
17 #include "glob-util.h"
18 #include "list.h"
19 #include "mkdir.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"
26 #include "strv.h"
27 #include "strxcpyx.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"
36 #include "virt.h"
37
38 #define RULES_DIRS (const char* const*) CONF_PATHS_STRV("udev/rules.d")
39
40 typedef enum {
41 OP_MATCH, /* == */
42 OP_NOMATCH, /* != */
43 OP_ADD, /* += */
44 OP_REMOVE, /* -= */
45 OP_ASSIGN, /* = */
46 OP_ASSIGN_FINAL, /* := */
47 _OP_TYPE_MAX,
48 _OP_TYPE_INVALID = -EINVAL,
49 } UdevRuleOperatorType;
50
51 typedef enum {
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" */
58 _MATCH_TYPE_MAX,
59 _MATCH_TYPE_INVALID = -EINVAL,
60 } UdevRuleMatchType;
61
62 typedef enum {
63 SUBST_TYPE_PLAIN, /* no substitution */
64 SUBST_TYPE_FORMAT, /* % or $ */
65 SUBST_TYPE_SUBSYS, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
66 _SUBST_TYPE_MAX,
67 _SUBST_TYPE_INVALID = -EINVAL,
68 } UdevRuleSubstituteType;
69
70 typedef enum {
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 */
84
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 */
91
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 */
101
102 #define _TK_M_MAX (TK_M_RESULT + 1)
103 #define _TK_A_MIN _TK_M_MAX
104
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 */
128
129 _TK_TYPE_MAX,
130 _TK_TYPE_INVALID = -EINVAL,
131 } UdevRuleTokenType;
132
133 typedef enum {
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 */
140 } UdevRuleLineType;
141
142 typedef struct UdevRuleFile UdevRuleFile;
143 typedef struct UdevRuleLine UdevRuleLine;
144 typedef struct UdevRuleToken UdevRuleToken;
145
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;
152 const char *value;
153 void *data;
154 LIST_FIELDS(UdevRuleToken, tokens);
155 };
156
157 struct UdevRuleLine {
158 char *line;
159 unsigned line_number;
160 UdevRuleLineType type;
161
162 const char *label;
163 const char *goto_label;
164 UdevRuleLine *goto_line;
165
166 UdevRuleFile *rule_file;
167 UdevRuleToken *current_token;
168 LIST_HEAD(UdevRuleToken, tokens);
169 LIST_FIELDS(UdevRuleLine, rule_lines);
170 };
171
172 struct UdevRuleFile {
173 char *filename;
174 UdevRuleLine *current_line;
175 LIST_HEAD(UdevRuleLine, rule_lines);
176 LIST_FIELDS(UdevRuleFile, rule_files);
177 };
178
179 struct UdevRules {
180 usec_t dirs_ts_usec;
181 ResolveNameTiming resolve_name_timing;
182 Hashmap *known_users;
183 Hashmap *known_groups;
184 UdevRuleFile *current_file;
185 LIST_HEAD(UdevRuleFile, rule_files);
186 };
187
188 /*** Logging helpers ***/
189
190 #define log_rule_full_errno_zerook(device, rules, level, error, fmt, ...) \
191 ({ \
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; \
196 \
197 log_device_full_errno_zerook( \
198 device, level, error, "%s:%u " fmt, \
199 strna(_n), _l ? _l->line_number : 0, \
200 ##__VA_ARGS__); \
201 })
202
203 #define log_rule_full_errno(device, rules, level, error, fmt, ...) \
204 ({ \
205 int _error = (error); \
206 ASSERT_NON_ZERO(_error); \
207 log_rule_full_errno_zerook( \
208 device, rules, level, _error, fmt, ##__VA_ARGS__); \
209 })
210
211 #define log_rule_full(device, rules, level, ...) (void) log_rule_full_errno_zerook(device, rules, level, 0, __VA_ARGS__)
212
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__)
218
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__)
224
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__)
228
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__)
234
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__)
240
241 #define _log_token_invalid(rules, key, type) \
242 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
243 "Invalid %s for %s.", type, key)
244
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")
247
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)
256
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);
260 else
261 log_rule_error_errno(dev, rules, error, "Failed to resolve %s '%s', ignoring: %m", entity, name);
262 }
263
264 /*** Other functions ***/
265
266 static void udev_rule_token_free(UdevRuleToken *token) {
267 free(token);
268 }
269
270 static void udev_rule_line_clear_tokens(UdevRuleLine *rule_line) {
271 UdevRuleToken *i, *next;
272
273 assert(rule_line);
274
275 LIST_FOREACH_SAFE(tokens, i, next, rule_line->tokens)
276 udev_rule_token_free(i);
277
278 rule_line->tokens = NULL;
279 }
280
281 static UdevRuleLine* udev_rule_line_free(UdevRuleLine *rule_line) {
282 if (!rule_line)
283 return NULL;
284
285 udev_rule_line_clear_tokens(rule_line);
286
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;
290
291 LIST_REMOVE(rule_lines, rule_line->rule_file->rule_lines, rule_line);
292 }
293
294 free(rule_line->line);
295 return mfree(rule_line);
296 }
297
298 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine*, udev_rule_line_free);
299
300 static void udev_rule_file_free(UdevRuleFile *rule_file) {
301 UdevRuleLine *i, *next;
302
303 if (!rule_file)
304 return;
305
306 LIST_FOREACH_SAFE(rule_lines, i, next, rule_file->rule_lines)
307 udev_rule_line_free(i);
308
309 free(rule_file->filename);
310 free(rule_file);
311 }
312
313 UdevRules *udev_rules_free(UdevRules *rules) {
314 UdevRuleFile *i, *next;
315
316 if (!rules)
317 return NULL;
318
319 LIST_FOREACH_SAFE(rule_files, i, next, rules->rule_files)
320 udev_rule_file_free(i);
321
322 hashmap_free_free_key(rules->known_users);
323 hashmap_free_free_key(rules->known_groups);
324 return mfree(rules);
325 }
326
327 static int rule_resolve_user(UdevRules *rules, const char *name, uid_t *ret) {
328 _cleanup_free_ char *n = NULL;
329 uid_t uid;
330 void *val;
331 int r;
332
333 assert(rules);
334 assert(name);
335
336 val = hashmap_get(rules->known_users, name);
337 if (val) {
338 *ret = PTR_TO_UID(val);
339 return 0;
340 }
341
342 r = get_user_creds(&name, &uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
343 if (r < 0) {
344 log_unknown_owner(NULL, rules, r, "user", name);
345 *ret = UID_INVALID;
346 return 0;
347 }
348
349 n = strdup(name);
350 if (!n)
351 return -ENOMEM;
352
353 r = hashmap_ensure_put(&rules->known_users, &string_hash_ops, n, UID_TO_PTR(uid));
354 if (r < 0)
355 return r;
356
357 TAKE_PTR(n);
358 *ret = uid;
359 return 0;
360 }
361
362 static int rule_resolve_group(UdevRules *rules, const char *name, gid_t *ret) {
363 _cleanup_free_ char *n = NULL;
364 gid_t gid;
365 void *val;
366 int r;
367
368 assert(rules);
369 assert(name);
370
371 val = hashmap_get(rules->known_groups, name);
372 if (val) {
373 *ret = PTR_TO_GID(val);
374 return 0;
375 }
376
377 r = get_group_creds(&name, &gid, USER_CREDS_ALLOW_MISSING);
378 if (r < 0) {
379 log_unknown_owner(NULL, rules, r, "group", name);
380 *ret = GID_INVALID;
381 return 0;
382 }
383
384 n = strdup(name);
385 if (!n)
386 return -ENOMEM;
387
388 r = hashmap_ensure_put(&rules->known_groups, &string_hash_ops, n, GID_TO_PTR(gid));
389 if (r < 0)
390 return r;
391
392 TAKE_PTR(n);
393 *ret = gid;
394 return 0;
395 }
396
397 static UdevRuleSubstituteType rule_get_substitution_type(const char *str) {
398 assert(str);
399
400 if (str[0] == '[')
401 return SUBST_TYPE_SUBSYS;
402 if (strchr(str, '%') || strchr(str, '$'))
403 return SUBST_TYPE_FORMAT;
404 return SUBST_TYPE_PLAIN;
405 }
406
407 static void rule_line_append_token(UdevRuleLine *rule_line, UdevRuleToken *token) {
408 assert(rule_line);
409 assert(token);
410
411 if (rule_line->current_token)
412 LIST_APPEND(tokens, rule_line->current_token, token);
413 else
414 LIST_APPEND(tokens, rule_line->tokens, token);
415
416 rule_line->current_token = token;
417 }
418
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;
424 size_t len;
425
426 assert(rule_line);
427 assert(type >= 0 && type < _TK_TYPE_MAX);
428 assert(op >= 0 && op < _OP_TYPE_MAX);
429
430 if (type < _TK_M_MAX) {
431 assert(value);
432 assert(IN_SET(op, OP_MATCH, OP_NOMATCH));
433
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;
444 else
445 match_type = MATCH_TYPE_PLAIN;
446
447 if (type < TK_M_TEST || type == TK_M_RESULT) {
448 /* Convert value string to nulstr. */
449 bool bar = true, empty = false;
450 char *a, *b;
451
452 for (a = b = value; *a != '\0'; a++) {
453 if (*a != '|') {
454 *b++ = *a;
455 bar = false;
456 } else {
457 if (bar)
458 empty = true;
459 else
460 *b++ = '\0';
461 bar = true;
462 }
463 }
464 *b = '\0';
465
466 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
467 if (b < a)
468 b[1] = '\0';
469
470 if (bar)
471 empty = true;
472
473 if (empty) {
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;
478 }
479 }
480 }
481
482 if (IN_SET(type, TK_M_ATTR, TK_M_PARENTS_ATTR)) {
483 assert(value);
484 assert(data);
485
486 len = strlen(value);
487 if (len > 0 && !isspace(value[len - 1]))
488 remove_trailing_whitespace = true;
489
490 subst_type = rule_get_substitution_type(data);
491 }
492
493 token = new(UdevRuleToken, 1);
494 if (!token)
495 return -ENOMEM;
496
497 *token = (UdevRuleToken) {
498 .type = type,
499 .op = op,
500 .value = value,
501 .data = data,
502 .match_type = match_type,
503 .attr_subst_type = subst_type,
504 .attr_match_remove_trailing_whitespace = remove_trailing_whitespace,
505 };
506
507 rule_line_append_token(rule_line, token);
508
509 if (token->type == TK_A_NAME)
510 SET_FLAG(rule_line->type, LINE_HAS_NAME, true);
511
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);
516
517 else if (token->type == TK_A_OPTIONS_STATIC_NODE)
518 SET_FLAG(rule_line->type, LINE_HAS_STATIC_NODE, true);
519
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);
525
526 return 0;
527 }
528
529 static void check_value_format_and_warn(UdevRules *rules, const char *key, const char *value, bool nonempty) {
530 size_t offset;
531 const char *hint;
532
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);
537 }
538
539 static int check_attr_format_and_warn(UdevRules *rules, const char *key, const char *value) {
540 size_t offset;
541 const char *hint;
542
543 if (isempty(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);
547 return 0;
548 }
549
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;
553 int r;
554
555 assert(rules);
556 assert(rules->current_file);
557 assert(rules->current_file->current_line);
558 assert(key);
559 assert(value);
560
561 rule_line = rules->current_file->current_line;
562
563 if (streq(key, "ACTION")) {
564 if (attr)
565 return log_token_invalid_attr(rules, key);
566 if (!is_match)
567 return log_token_invalid_op(rules, key);
568
569 r = rule_line_add_token(rule_line, TK_M_ACTION, op, value, NULL);
570 } else if (streq(key, "DEVPATH")) {
571 if (attr)
572 return log_token_invalid_attr(rules, key);
573 if (!is_match)
574 return log_token_invalid_op(rules, key);
575
576 r = rule_line_add_token(rule_line, TK_M_DEVPATH, op, value, NULL);
577 } else if (streq(key, "KERNEL")) {
578 if (attr)
579 return log_token_invalid_attr(rules, key);
580 if (!is_match)
581 return log_token_invalid_op(rules, key);
582
583 r = rule_line_add_token(rule_line, TK_M_KERNEL, op, value, NULL);
584 } else if (streq(key, "SYMLINK")) {
585 if (attr)
586 return log_token_invalid_attr(rules, key);
587 if (op == OP_REMOVE)
588 return log_token_invalid_op(rules, key);
589
590 if (!is_match) {
591 check_value_format_and_warn(rules, key, value, false);
592 r = rule_line_add_token(rule_line, TK_A_DEVLINK, op, value, NULL);
593 } else
594 r = rule_line_add_token(rule_line, TK_M_DEVLINK, op, value, NULL);
595 } else if (streq(key, "NAME")) {
596 if (attr)
597 return log_token_invalid_attr(rules, key);
598 if (op == OP_REMOVE)
599 return log_token_invalid_op(rules, key);
600 if (op == OP_ADD) {
601 log_token_warning(rules, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key);
602 op = OP_ASSIGN;
603 }
604
605 if (!is_match) {
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.");
609 if (isempty(value))
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);
613
614 r = rule_line_add_token(rule_line, TK_A_NAME, op, value, NULL);
615 } else
616 r = rule_line_add_token(rule_line, TK_M_NAME, op, value, NULL);
617 } else if (streq(key, "ENV")) {
618 if (isempty(attr))
619 return log_token_invalid_attr(rules, key);
620 if (op == OP_REMOVE)
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);
624 op = OP_ASSIGN;
625 }
626
627 if (!is_match) {
628 if (STR_IN_SET(attr,
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);
633
634 check_value_format_and_warn(rules, key, value, false);
635
636 r = rule_line_add_token(rule_line, TK_A_ENV, op, value, attr);
637 } else
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);
642 if (!is_match)
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")) {
646 if (attr)
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);
650 op = OP_ASSIGN;
651 }
652
653 if (!is_match) {
654 check_value_format_and_warn(rules, key, value, true);
655
656 r = rule_line_add_token(rule_line, TK_A_TAG, op, value, NULL);
657 } else
658 r = rule_line_add_token(rule_line, TK_M_TAG, op, value, NULL);
659 } else if (streq(key, "SUBSYSTEM")) {
660 if (attr)
661 return log_token_invalid_attr(rules, key);
662 if (!is_match)
663 return log_token_invalid_op(rules, key);
664
665 if (STR_IN_SET(value, "bus", "class"))
666 log_token_warning(rules, "\"%s\" must be specified as \"subsystem\".", value);
667
668 r = rule_line_add_token(rule_line, TK_M_SUBSYSTEM, op, value, NULL);
669 } else if (streq(key, "DRIVER")) {
670 if (attr)
671 return log_token_invalid_attr(rules, key);
672 if (!is_match)
673 return log_token_invalid_op(rules, key);
674
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);
678 if (r < 0)
679 return r;
680 if (op == OP_REMOVE)
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);
684 op = OP_ASSIGN;
685 }
686
687 if (!is_match) {
688 check_value_format_and_warn(rules, key, value, false);
689 r = rule_line_add_token(rule_line, TK_A_ATTR, op, value, attr);
690 } else
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);
694 if (r < 0)
695 return r;
696 if (op == OP_REMOVE)
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);
700 op = OP_ASSIGN;
701 }
702
703 if (!is_match) {
704 check_value_format_and_warn(rules, key, value, false);
705 r = rule_line_add_token(rule_line, TK_A_SYSCTL, op, value, attr);
706 } else
707 r = rule_line_add_token(rule_line, TK_M_SYSCTL, op, value, attr);
708 } else if (streq(key, "KERNELS")) {
709 if (attr)
710 return log_token_invalid_attr(rules, key);
711 if (!is_match)
712 return log_token_invalid_op(rules, key);
713
714 r = rule_line_add_token(rule_line, TK_M_PARENTS_KERNEL, op, value, NULL);
715 } else if (streq(key, "SUBSYSTEMS")) {
716 if (attr)
717 return log_token_invalid_attr(rules, key);
718 if (!is_match)
719 return log_token_invalid_op(rules, key);
720
721 r = rule_line_add_token(rule_line, TK_M_PARENTS_SUBSYSTEM, op, value, NULL);
722 } else if (streq(key, "DRIVERS")) {
723 if (attr)
724 return log_token_invalid_attr(rules, key);
725 if (!is_match)
726 return log_token_invalid_op(rules, key);
727
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);
731 if (r < 0)
732 return r;
733 if (!is_match)
734 return log_token_invalid_op(rules, key);
735
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.");
740
741 r = rule_line_add_token(rule_line, TK_M_PARENTS_ATTR, op, value, attr);
742 } else if (streq(key, "TAGS")) {
743 if (attr)
744 return log_token_invalid_attr(rules, key);
745 if (!is_match)
746 return log_token_invalid_op(rules, key);
747
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;
751
752 if (!isempty(attr)) {
753 r = parse_mode(attr, &mode);
754 if (r < 0)
755 return log_token_error_errno(rules, r, "Failed to parse mode '%s': %m", attr);
756 }
757 check_value_format_and_warn(rules, key, value, true);
758 if (!is_match)
759 return log_token_invalid_op(rules, key);
760
761 r = rule_line_add_token(rule_line, TK_M_TEST, op, value, MODE_TO_PTR(mode));
762 } else if (streq(key, "PROGRAM")) {
763 if (attr)
764 return log_token_invalid_attr(rules, key);
765 check_value_format_and_warn(rules, key, value, true);
766 if (op == OP_REMOVE)
767 return log_token_invalid_op(rules, key);
768 if (!is_match)
769 op = OP_MATCH;
770
771 r = rule_line_add_token(rule_line, TK_M_PROGRAM, op, value, NULL);
772 } else if (streq(key, "IMPORT")) {
773 if (isempty(attr))
774 return log_token_invalid_attr(rules, key);
775 check_value_format_and_warn(rules, key, value, true);
776 if (op == OP_REMOVE)
777 return log_token_invalid_op(rules, key);
778 if (!is_match)
779 op = OP_MATCH;
780
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;
785
786 cmd = udev_builtin_lookup(value);
787 if (cmd >= 0) {
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));
790 } else
791 r = rule_line_add_token(rule_line, TK_M_IMPORT_PROGRAM, op, value, NULL);
792 } else if (streq(attr, "builtin")) {
793 UdevBuiltinCommand cmd;
794
795 cmd = udev_builtin_lookup(value);
796 if (cmd < 0)
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);
806 else
807 return log_token_invalid_attr(rules, key);
808 } else if (streq(key, "RESULT")) {
809 if (attr)
810 return log_token_invalid_attr(rules, key);
811 if (!is_match)
812 return log_token_invalid_op(rules, key);
813
814 r = rule_line_add_token(rule_line, TK_M_RESULT, op, value, NULL);
815 } else if (streq(key, "OPTIONS")) {
816 char *tmp;
817
818 if (attr)
819 return log_token_invalid_attr(rules, key);
820 if (is_match || op == OP_REMOVE)
821 return log_token_invalid_op(rules, key);
822 if (op == OP_ADD)
823 op = OP_ASSIGN;
824
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="))) {
838 int prio;
839
840 r = safe_atoi(tmp, &prio);
841 if (r < 0)
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="))) {
845 int level;
846
847 if (streq(tmp, "reset"))
848 level = -1;
849 else {
850 level = log_level_from_string(tmp);
851 if (level < 0)
852 return log_token_error_errno(rules, level, "Failed to parse log level '%s': %m", tmp);
853 }
854 r = rule_line_add_token(rule_line, TK_A_OPTIONS_LOG_LEVEL, op, NULL, INT_TO_PTR(level));
855 } else {
856 log_token_warning(rules, "Invalid value for OPTIONS key, ignoring: '%s'", value);
857 return 0;
858 }
859 } else if (streq(key, "OWNER")) {
860 uid_t uid;
861
862 if (attr)
863 return log_token_invalid_attr(rules, key);
864 if (is_match || op == OP_REMOVE)
865 return log_token_invalid_op(rules, key);
866 if (op == OP_ADD) {
867 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
868 op = OP_ASSIGN;
869 }
870
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);
876 if (r < 0)
877 return log_token_error_errno(rules, r, "Failed to resolve user name '%s': %m", value);
878
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);
883 } else {
884 log_token_debug(rules, "User name resolution is disabled, ignoring %s=%s", key, value);
885 return 0;
886 }
887 } else if (streq(key, "GROUP")) {
888 gid_t gid;
889
890 if (attr)
891 return log_token_invalid_attr(rules, key);
892 if (is_match || op == OP_REMOVE)
893 return log_token_invalid_op(rules, key);
894 if (op == OP_ADD) {
895 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
896 op = OP_ASSIGN;
897 }
898
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);
904 if (r < 0)
905 return log_token_error_errno(rules, r, "Failed to resolve group name '%s': %m", value);
906
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);
911 } else {
912 log_token_debug(rules, "Resolving group name is disabled, ignoring GROUP=\"%s\"", value);
913 return 0;
914 }
915 } else if (streq(key, "MODE")) {
916 mode_t mode;
917
918 if (attr)
919 return log_token_invalid_attr(rules, key);
920 if (is_match || op == OP_REMOVE)
921 return log_token_invalid_op(rules, key);
922 if (op == OP_ADD) {
923 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
924 op = OP_ASSIGN;
925 }
926
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));
929 else {
930 check_value_format_and_warn(rules, key, value, true);
931 r = rule_line_add_token(rule_line, TK_A_MODE, op, value, NULL);
932 }
933 } else if (streq(key, "SECLABEL")) {
934 if (isempty(attr))
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);
941 op = OP_ASSIGN;
942 }
943
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;
953
954 cmd = udev_builtin_lookup(value);
955 if (cmd < 0)
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));
959 } else
960 return log_token_invalid_attr(rules, key);
961 } else if (streq(key, "GOTO")) {
962 if (attr)
963 return log_token_invalid_attr(rules, key);
964 if (op != OP_ASSIGN)
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);
968 return 0;
969 }
970
971 rule_line->goto_label = value;
972 SET_FLAG(rule_line->type, LINE_HAS_GOTO, true);
973 return 1;
974 } else if (streq(key, "LABEL")) {
975 if (attr)
976 return log_token_invalid_attr(rules, key);
977 if (op != OP_ASSIGN)
978 return log_token_invalid_op(rules, key);
979
980 rule_line->label = value;
981 SET_FLAG(rule_line->type, LINE_HAS_LABEL, true);
982 return 1;
983 } else
984 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), "Invalid key '%s'", key);
985 if (r < 0)
986 return log_oom();
987
988 return 1;
989 }
990
991 static UdevRuleOperatorType parse_operator(const char *op) {
992 assert(op);
993
994 if (startswith(op, "=="))
995 return OP_MATCH;
996 if (startswith(op, "!="))
997 return OP_NOMATCH;
998 if (startswith(op, "+="))
999 return OP_ADD;
1000 if (startswith(op, "-="))
1001 return OP_REMOVE;
1002 if (startswith(op, "="))
1003 return OP_ASSIGN;
1004 if (startswith(op, ":="))
1005 return OP_ASSIGN_FINAL;
1006
1007 return _OP_TYPE_INVALID;
1008 }
1009
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;
1013 int r;
1014
1015 assert(line);
1016 assert(*line);
1017 assert(ret_key);
1018 assert(ret_op);
1019 assert(ret_value);
1020
1021 key_begin = skip_leading_chars(*line, WHITESPACE ",");
1022
1023 if (isempty(key_begin))
1024 return 0;
1025
1026 for (key_end = key_begin; ; key_end++) {
1027 if (key_end[0] == '\0')
1028 return -EINVAL;
1029 if (strchr(WHITESPACE "={", key_end[0]))
1030 break;
1031 if (strchr("+-!:", key_end[0]) && key_end[1] == '=')
1032 break;
1033 }
1034 if (key_end[0] == '{') {
1035 attr = key_end + 1;
1036 tmp = strchr(attr, '}');
1037 if (!tmp)
1038 return -EINVAL;
1039 *tmp++ = '\0';
1040 } else {
1041 attr = NULL;
1042 tmp = key_end;
1043 }
1044
1045 tmp = skip_leading_chars(tmp, NULL);
1046 op = parse_operator(tmp);
1047 if (op < 0)
1048 return -EINVAL;
1049
1050 key_end[0] = '\0';
1051
1052 tmp += op == OP_ASSIGN ? 1 : 2;
1053 tmp = skip_leading_chars(tmp, NULL);
1054 r = udev_rule_parse_value(tmp, ret_value, line);
1055 if (r < 0)
1056 return r;
1057
1058 *ret_key = key_begin;
1059 *ret_attr = attr;
1060 *ret_op = op;
1061 return 1;
1062 }
1063
1064 static void sort_tokens(UdevRuleLine *rule_line) {
1065 UdevRuleToken *head_old;
1066
1067 assert(rule_line);
1068
1069 head_old = TAKE_PTR(rule_line->tokens);
1070 rule_line->current_token = NULL;
1071
1072 while (!LIST_IS_EMPTY(head_old)) {
1073 UdevRuleToken *t, *min_token = NULL;
1074
1075 LIST_FOREACH(tokens, t, head_old)
1076 if (!min_token || min_token->type > t->type)
1077 min_token = t;
1078
1079 LIST_REMOVE(tokens, head_old, min_token);
1080 rule_line_append_token(rule_line, min_token);
1081 }
1082 }
1083
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;
1088 char *p;
1089 int r;
1090
1091 assert(rules);
1092 assert(rules->current_file);
1093 assert(line_str);
1094
1095 rule_file = rules->current_file;
1096
1097 if (isempty(line_str))
1098 return 0;
1099
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);
1103 if (!line)
1104 return log_oom();
1105
1106 rule_line = new(UdevRuleLine, 1);
1107 if (!rule_line)
1108 return log_oom();
1109
1110 *rule_line = (UdevRuleLine) {
1111 .line = TAKE_PTR(line),
1112 .line_number = line_nr,
1113 .rule_file = rule_file,
1114 };
1115
1116 if (rule_file->current_line)
1117 LIST_APPEND(rule_lines, rule_file->current_line, rule_line);
1118 else
1119 LIST_APPEND(rule_lines, rule_file->rule_lines, rule_line);
1120
1121 rule_file->current_line = rule_line;
1122
1123 for (p = rule_line->line; !isempty(p); ) {
1124 char *key, *attr, *value;
1125 UdevRuleOperatorType op;
1126
1127 r = parse_line(&p, &key, &attr, &op, &value);
1128 if (r < 0)
1129 return log_token_error_errno(rules, r, "Invalid key/value pair, ignoring.");
1130 if (r == 0)
1131 break;
1132
1133 r = parse_token(rules, key, attr, op, value);
1134 if (r < 0)
1135 return r;
1136 }
1137
1138 if (rule_line->type == 0) {
1139 log_token_warning(rules, "The line takes no effect, ignoring.");
1140 return 0;
1141 }
1142
1143 sort_tokens(rule_line);
1144 TAKE_PTR(rule_line);
1145 return 0;
1146 }
1147
1148 static void rule_resolve_goto(UdevRuleFile *rule_file) {
1149 UdevRuleLine *line, *line_next, *i;
1150
1151 assert(rule_file);
1152
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))
1156 continue;
1157
1158 LIST_FOREACH(rule_lines, i, line->rule_lines_next)
1159 if (streq_ptr(i->label, line->goto_label)) {
1160 line->goto_line = i;
1161 break;
1162 }
1163
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);
1167
1168 SET_FLAG(line->type, LINE_HAS_GOTO, false);
1169 line->goto_label = NULL;
1170
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);
1176 else
1177 udev_rule_line_free(line);
1178 }
1179 }
1180 }
1181 }
1182
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;
1189 int r;
1190
1191 f = fopen(filename, "re");
1192 if (!f) {
1193 if (errno == ENOENT)
1194 return 0;
1195
1196 return -errno;
1197 }
1198
1199 (void) fd_warn_permissions(filename, fileno(f));
1200
1201 if (null_or_empty_fd(fileno(f))) {
1202 log_debug("Skipping empty file: %s", filename);
1203 return 0;
1204 }
1205
1206 log_debug("Reading rules file: %s", filename);
1207
1208 name = strdup(filename);
1209 if (!name)
1210 return log_oom();
1211
1212 rule_file = new(UdevRuleFile, 1);
1213 if (!rule_file)
1214 return log_oom();
1215
1216 *rule_file = (UdevRuleFile) {
1217 .filename = TAKE_PTR(name),
1218 };
1219
1220 if (rules->current_file)
1221 LIST_APPEND(rule_files, rules->current_file, rule_file);
1222 else
1223 LIST_APPEND(rule_files, rules->rule_files, rule_file);
1224
1225 rules->current_file = rule_file;
1226
1227 for (;;) {
1228 _cleanup_free_ char *buf = NULL;
1229 size_t len;
1230 char *line;
1231
1232 r = read_line(f, UDEV_LINE_SIZE, &buf);
1233 if (r < 0)
1234 return r;
1235 if (r == 0)
1236 break;
1237
1238 line_nr++;
1239 line = skip_leading_chars(buf, NULL);
1240
1241 if (line[0] == '#')
1242 continue;
1243
1244 len = strlen(line);
1245
1246 if (continuation && !ignore_line) {
1247 if (strlen(continuation) + len >= UDEV_LINE_SIZE)
1248 ignore_line = true;
1249
1250 if (!strextend(&continuation, line))
1251 return log_oom();
1252
1253 if (!ignore_line) {
1254 line = continuation;
1255 len = strlen(line);
1256 }
1257 }
1258
1259 if (len > 0 && line[len - 1] == '\\') {
1260 if (ignore_line)
1261 continue;
1262
1263 line[len - 1] = '\0';
1264 if (!continuation) {
1265 continuation = strdup(line);
1266 if (!continuation)
1267 return log_oom();
1268 }
1269
1270 continue;
1271 }
1272
1273 if (ignore_line)
1274 log_error("%s:%u: Line is too long, ignored", filename, line_nr);
1275 else if (len > 0)
1276 (void) rule_add_line(rules, line, line_nr);
1277
1278 continuation = mfree(continuation);
1279 ignore_line = false;
1280 }
1281
1282 rule_resolve_goto(rule_file);
1283 return 0;
1284 }
1285
1286 UdevRules* udev_rules_new(ResolveNameTiming resolve_name_timing) {
1287 assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX);
1288
1289 UdevRules *rules = new(UdevRules, 1);
1290 if (!rules)
1291 return NULL;
1292
1293 *rules = (UdevRules) {
1294 .resolve_name_timing = resolve_name_timing,
1295 };
1296
1297 return rules;
1298 }
1299
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;
1303 char **f;
1304 int r;
1305
1306 rules = udev_rules_new(resolve_name_timing);
1307 if (!rules)
1308 return -ENOMEM;
1309
1310 (void) udev_rules_check_timestamp(rules);
1311
1312 r = conf_files_list_strv(&files, ".rules", NULL, 0, RULES_DIRS);
1313 if (r < 0)
1314 return log_debug_errno(r, "Failed to enumerate rules files: %m");
1315
1316 STRV_FOREACH(f, files) {
1317 r = udev_rules_parse_file(rules, *f);
1318 if (r < 0)
1319 log_debug_errno(r, "Failed to read rules file %s, ignoring: %m", *f);
1320 }
1321
1322 *ret_rules = TAKE_PTR(rules);
1323 return 0;
1324 }
1325
1326 bool udev_rules_check_timestamp(UdevRules *rules) {
1327 if (!rules)
1328 return false;
1329
1330 return paths_check_timestamp(RULES_DIRS, &rules->dirs_ts_usec, true);
1331 }
1332
1333 static bool token_match_string(UdevRuleToken *token, const char *str) {
1334 const char *i, *value;
1335 bool match = false;
1336
1337 assert(token);
1338 assert(token->value);
1339 assert(token->type < _TK_M_MAX);
1340
1341 str = strempty(str);
1342 value = token->value;
1343
1344 switch (token->match_type) {
1345 case MATCH_TYPE_EMPTY:
1346 match = isempty(str);
1347 break;
1348 case MATCH_TYPE_SUBSYSTEM:
1349 match = STR_IN_SET(str, "subsystem", "class", "bus");
1350 break;
1351 case MATCH_TYPE_PLAIN_WITH_EMPTY:
1352 if (isempty(str)) {
1353 match = true;
1354 break;
1355 }
1356 _fallthrough_;
1357 case MATCH_TYPE_PLAIN:
1358 NULSTR_FOREACH(i, value)
1359 if (streq(i, str)) {
1360 match = true;
1361 break;
1362 }
1363 break;
1364 case MATCH_TYPE_GLOB_WITH_EMPTY:
1365 if (isempty(str)) {
1366 match = true;
1367 break;
1368 }
1369 _fallthrough_;
1370 case MATCH_TYPE_GLOB:
1371 NULSTR_FOREACH(i, value)
1372 if ((fnmatch(i, str, 0) == 0)) {
1373 match = true;
1374 break;
1375 }
1376 break;
1377 default:
1378 assert_not_reached();
1379 }
1380
1381 return token->op == (match ? OP_MATCH : OP_NOMATCH);
1382 }
1383
1384 static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *event) {
1385 char nbuf[UDEV_NAME_SIZE], vbuf[UDEV_NAME_SIZE];
1386 const char *name, *value;
1387
1388 assert(token);
1389 assert(dev);
1390 assert(event);
1391
1392 name = token->data;
1393
1394 switch (token->attr_subst_type) {
1395 case SUBST_TYPE_FORMAT:
1396 (void) udev_event_apply_format(event, name, nbuf, sizeof(nbuf), false);
1397 name = nbuf;
1398 _fallthrough_;
1399 case SUBST_TYPE_PLAIN:
1400 if (device_get_sysattr_value_maybe_from_netlink(dev, &event->rtnl, name, &value) < 0)
1401 return false;
1402 break;
1403 case SUBST_TYPE_SUBSYS:
1404 if (udev_resolve_subsys_kernel(name, vbuf, sizeof(vbuf), true) < 0)
1405 return false;
1406 value = vbuf;
1407 break;
1408 default:
1409 assert_not_reached();
1410 }
1411
1412 /* remove trailing whitespace, if not asked to match for it */
1413 if (token->attr_match_remove_trailing_whitespace) {
1414 if (value != vbuf) {
1415 strscpy(vbuf, sizeof(vbuf), value);
1416 value = vbuf;
1417 }
1418
1419 delete_trailing_chars(vbuf, NULL);
1420 }
1421
1422 return token_match_string(token, value);
1423 }
1424
1425 static int get_property_from_string(char *line, char **ret_key, char **ret_value) {
1426 char *key, *val;
1427 size_t len;
1428
1429 assert(line);
1430 assert(ret_key);
1431 assert(ret_value);
1432
1433 /* find key */
1434 key = skip_leading_chars(line, NULL);
1435
1436 /* comment or empty line */
1437 if (IN_SET(key[0], '#', '\0')) {
1438 *ret_key = *ret_value = NULL;
1439 return 0;
1440 }
1441
1442 /* split key/value */
1443 val = strchr(key, '=');
1444 if (!val)
1445 return -EINVAL;
1446 *val++ = '\0';
1447
1448 key = strstrip(key);
1449 if (isempty(key))
1450 return -EINVAL;
1451
1452 val = strstrip(val);
1453 if (isempty(val))
1454 return -EINVAL;
1455
1456 /* unquote */
1457 if (IN_SET(val[0], '"', '\'')) {
1458 len = strlen(val);
1459 if (len == 1 || val[len-1] != val[0])
1460 return -EINVAL;
1461 val[len-1] = '\0';
1462 val++;
1463 }
1464
1465 *ret_key = key;
1466 *ret_value = val;
1467 return 1;
1468 }
1469
1470 static int import_parent_into_properties(sd_device *dev, const char *filter) {
1471 const char *key, *val;
1472 sd_device *parent;
1473 int r;
1474
1475 assert(dev);
1476 assert(filter);
1477
1478 r = sd_device_get_parent(dev, &parent);
1479 if (r == -ENOENT)
1480 return 0;
1481 if (r < 0)
1482 return r;
1483
1484 FOREACH_DEVICE_PROPERTY(parent, key, val) {
1485 if (fnmatch(filter, key, 0) != 0)
1486 continue;
1487 r = device_add_property(dev, key, val);
1488 if (r < 0)
1489 return r;
1490 }
1491
1492 return 1;
1493 }
1494
1495 static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) {
1496 _cleanup_closedir_ DIR *dir = NULL;
1497 char buf[UDEV_PATH_SIZE], *p;
1498 const char *tail;
1499 size_t len, size;
1500
1501 assert(attr);
1502
1503 tail = strstr(attr, "/*/");
1504 if (!tail)
1505 return 0;
1506
1507 len = tail - attr + 1; /* include slash at the end */
1508 tail += 2; /* include slash at the beginning */
1509
1510 p = buf;
1511 size = sizeof(buf);
1512 size -= strnpcpy(&p, size, attr, len);
1513
1514 dir = opendir(buf);
1515 if (!dir)
1516 return -errno;
1517
1518 FOREACH_DIRENT_ALL(de, dir, break) {
1519 if (de->d_name[0] == '.')
1520 continue;
1521
1522 strscpyl(p, size, de->d_name, tail, NULL);
1523 if (faccessat(dirfd(dir), p, F_OK, 0) < 0)
1524 continue;
1525
1526 strcpy(attr, buf);
1527 return 0;
1528 }
1529
1530 return -ENOENT;
1531 }
1532
1533 static int udev_rule_apply_token_to_event(
1534 UdevRules *rules,
1535 sd_device *dev,
1536 UdevEvent *event,
1537 usec_t timeout_usec,
1538 int timeout_signal,
1539 Hashmap *properties_list) {
1540
1541 UdevRuleToken *token;
1542 int r;
1543
1544 assert(rules);
1545 assert(dev);
1546 assert(event);
1547
1548 /* This returns the following values:
1549 * 0 on the current token does not match the event,
1550 * 1 on the current token matches the event, and
1551 * negative errno on some critical errors. */
1552
1553 token = rules->current_file->current_line->current_token;
1554
1555 switch (token->type) {
1556 case TK_M_ACTION: {
1557 sd_device_action_t a;
1558
1559 r = sd_device_get_action(dev, &a);
1560 if (r < 0)
1561 return log_rule_error_errno(dev, rules, r, "Failed to get uevent action type: %m");
1562
1563 return token_match_string(token, device_action_to_string(a));
1564 }
1565 case TK_M_DEVPATH: {
1566 const char *val;
1567
1568 r = sd_device_get_devpath(dev, &val);
1569 if (r < 0)
1570 return log_rule_error_errno(dev, rules, r, "Failed to get devpath: %m");
1571
1572 return token_match_string(token, val);
1573 }
1574 case TK_M_KERNEL:
1575 case TK_M_PARENTS_KERNEL: {
1576 const char *val;
1577
1578 r = sd_device_get_sysname(dev, &val);
1579 if (r < 0)
1580 return log_rule_error_errno(dev, rules, r, "Failed to get sysname: %m");
1581
1582 return token_match_string(token, val);
1583 }
1584 case TK_M_DEVLINK: {
1585 const char *val;
1586
1587 FOREACH_DEVICE_DEVLINK(dev, val)
1588 if (token_match_string(token, strempty(startswith(val, "/dev/"))))
1589 return token->op == OP_MATCH;
1590 return token->op == OP_NOMATCH;
1591 }
1592 case TK_M_NAME:
1593 return token_match_string(token, event->name);
1594 case TK_M_ENV: {
1595 const char *val;
1596
1597 if (sd_device_get_property_value(dev, token->data, &val) < 0)
1598 val = hashmap_get(properties_list, token->data);
1599
1600 return token_match_string(token, val);
1601 }
1602 case TK_M_CONST: {
1603 const char *val, *k = token->data;
1604
1605 if (streq(k, "arch"))
1606 val = architecture_to_string(uname_architecture());
1607 else if (streq(k, "virt"))
1608 val = virtualization_to_string(detect_virtualization());
1609 else
1610 assert_not_reached();
1611 return token_match_string(token, val);
1612 }
1613 case TK_M_TAG:
1614 case TK_M_PARENTS_TAG: {
1615 const char *val;
1616
1617 FOREACH_DEVICE_TAG(dev, val)
1618 if (token_match_string(token, val))
1619 return token->op == OP_MATCH;
1620 return token->op == OP_NOMATCH;
1621 }
1622 case TK_M_SUBSYSTEM:
1623 case TK_M_PARENTS_SUBSYSTEM: {
1624 const char *val;
1625
1626 r = sd_device_get_subsystem(dev, &val);
1627 if (r == -ENOENT)
1628 val = NULL;
1629 else if (r < 0)
1630 return log_rule_error_errno(dev, rules, r, "Failed to get subsystem: %m");
1631
1632 return token_match_string(token, val);
1633 }
1634 case TK_M_DRIVER:
1635 case TK_M_PARENTS_DRIVER: {
1636 const char *val;
1637
1638 r = sd_device_get_driver(dev, &val);
1639 if (r == -ENOENT)
1640 val = NULL;
1641 else if (r < 0)
1642 return log_rule_error_errno(dev, rules, r, "Failed to get driver: %m");
1643
1644 return token_match_string(token, val);
1645 }
1646 case TK_M_ATTR:
1647 case TK_M_PARENTS_ATTR:
1648 return token_match_attr(token, dev, event);
1649 case TK_M_SYSCTL: {
1650 _cleanup_free_ char *value = NULL;
1651 char buf[UDEV_PATH_SIZE];
1652
1653 (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false);
1654 r = sysctl_read(sysctl_normalize(buf), &value);
1655 if (r < 0 && r != -ENOENT)
1656 return log_rule_error_errno(dev, rules, r, "Failed to read sysctl '%s': %m", buf);
1657
1658 return token_match_string(token, strstrip(value));
1659 }
1660 case TK_M_TEST: {
1661 mode_t mode = PTR_TO_MODE(token->data);
1662 char buf[UDEV_PATH_SIZE];
1663 struct stat statbuf;
1664 bool match;
1665
1666 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
1667 if (!path_is_absolute(buf) &&
1668 udev_resolve_subsys_kernel(buf, buf, sizeof(buf), false) < 0) {
1669 char tmp[UDEV_PATH_SIZE];
1670 const char *val;
1671
1672 r = sd_device_get_syspath(dev, &val);
1673 if (r < 0)
1674 return log_rule_error_errno(dev, rules, r, "Failed to get syspath: %m");
1675
1676 strscpy(tmp, sizeof(tmp), buf);
1677 strscpyl(buf, sizeof(buf), val, "/", tmp, NULL);
1678 }
1679
1680 r = attr_subst_subdir(buf);
1681 if (r == -ENOENT)
1682 return token->op == OP_NOMATCH;
1683 if (r < 0)
1684 return log_rule_error_errno(dev, rules, r, "Failed to test for the existence of '%s': %m", buf);
1685
1686 if (stat(buf, &statbuf) < 0)
1687 return token->op == OP_NOMATCH;
1688
1689 if (mode == MODE_INVALID)
1690 return token->op == OP_MATCH;
1691
1692 match = (statbuf.st_mode & mode) > 0;
1693 return token->op == (match ? OP_MATCH : OP_NOMATCH);
1694 }
1695 case TK_M_PROGRAM: {
1696 char buf[UDEV_PATH_SIZE], result[UDEV_LINE_SIZE];
1697 size_t count;
1698
1699 event->program_result = mfree(event->program_result);
1700 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
1701 log_rule_debug(dev, rules, "Running PROGRAM '%s'", buf);
1702
1703 r = udev_event_spawn(event, timeout_usec, timeout_signal, true, buf, result, sizeof(result));
1704 if (r != 0) {
1705 if (r < 0)
1706 log_rule_warning_errno(dev, rules, r, "Failed to execute \"%s\": %m", buf);
1707 else /* returned value is positive when program fails */
1708 log_rule_debug(dev, rules, "Command \"%s\" returned %d (error)", buf, r);
1709 return token->op == OP_NOMATCH;
1710 }
1711
1712 delete_trailing_chars(result, "\n");
1713 count = udev_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
1714 if (count > 0)
1715 log_rule_debug(dev, rules, "Replaced %zu character(s) in result of \"%s\"",
1716 count, buf);
1717
1718 event->program_result = strdup(result);
1719 return token->op == OP_MATCH;
1720 }
1721 case TK_M_IMPORT_FILE: {
1722 _cleanup_fclose_ FILE *f = NULL;
1723 char buf[UDEV_PATH_SIZE];
1724
1725 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
1726 log_rule_debug(dev, rules, "Importing properties from '%s'", buf);
1727
1728 f = fopen(buf, "re");
1729 if (!f) {
1730 if (errno != ENOENT)
1731 return log_rule_error_errno(dev, rules, errno,
1732 "Failed to open '%s': %m", buf);
1733 return token->op == OP_NOMATCH;
1734 }
1735
1736 for (;;) {
1737 _cleanup_free_ char *line = NULL;
1738 char *key, *value;
1739
1740 r = read_line(f, LONG_LINE_MAX, &line);
1741 if (r < 0) {
1742 log_rule_debug_errno(dev, rules, r,
1743 "Failed to read '%s', ignoring: %m", buf);
1744 return token->op == OP_NOMATCH;
1745 }
1746 if (r == 0)
1747 break;
1748
1749 r = get_property_from_string(line, &key, &value);
1750 if (r < 0) {
1751 log_rule_debug_errno(dev, rules, r,
1752 "Failed to parse key and value from '%s', ignoring: %m",
1753 line);
1754 continue;
1755 }
1756 if (r == 0)
1757 continue;
1758
1759 r = device_add_property(dev, key, value);
1760 if (r < 0)
1761 return log_rule_error_errno(dev, rules, r,
1762 "Failed to add property %s=%s: %m",
1763 key, value);
1764 }
1765
1766 return token->op == OP_MATCH;
1767 }
1768 case TK_M_IMPORT_PROGRAM: {
1769 _cleanup_strv_free_ char **lines = NULL;
1770 char buf[UDEV_PATH_SIZE], result[UDEV_LINE_SIZE], **line;
1771
1772 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
1773 log_rule_debug(dev, rules, "Importing properties from results of '%s'", buf);
1774
1775 r = udev_event_spawn(event, timeout_usec, timeout_signal, true, buf, result, sizeof result);
1776 if (r != 0) {
1777 if (r < 0)
1778 log_rule_warning_errno(dev, rules, r, "Failed to execute '%s', ignoring: %m", buf);
1779 else /* returned value is positive when program fails */
1780 log_rule_debug(dev, rules, "Command \"%s\" returned %d (error), ignoring", buf, r);
1781 return token->op == OP_NOMATCH;
1782 }
1783
1784 r = strv_split_newlines_full(&lines, result, EXTRACT_RETAIN_ESCAPE);
1785 if (r < 0)
1786 log_rule_warning_errno(dev, rules, r,
1787 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf);
1788
1789 STRV_FOREACH(line, lines) {
1790 char *key, *value;
1791
1792 r = get_property_from_string(*line, &key, &value);
1793 if (r < 0) {
1794 log_rule_debug_errno(dev, rules, r,
1795 "Failed to parse key and value from '%s', ignoring: %m",
1796 *line);
1797 continue;
1798 }
1799 if (r == 0)
1800 continue;
1801
1802 r = device_add_property(dev, key, value);
1803 if (r < 0)
1804 return log_rule_error_errno(dev, rules, r,
1805 "Failed to add property %s=%s: %m",
1806 key, value);
1807 }
1808
1809 return token->op == OP_MATCH;
1810 }
1811 case TK_M_IMPORT_BUILTIN: {
1812 UdevBuiltinCommand cmd = PTR_TO_UDEV_BUILTIN_CMD(token->data);
1813 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
1814 unsigned mask = 1U << (int) cmd;
1815 char buf[UDEV_PATH_SIZE];
1816
1817 if (udev_builtin_run_once(cmd)) {
1818 /* check if we ran already */
1819 if (event->builtin_run & mask) {
1820 log_rule_debug(dev, rules, "Skipping builtin '%s' in IMPORT key",
1821 udev_builtin_name(cmd));
1822 /* return the result from earlier run */
1823 return token->op == (event->builtin_ret & mask ? OP_NOMATCH : OP_MATCH);
1824 }
1825 /* mark as ran */
1826 event->builtin_run |= mask;
1827 }
1828
1829 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
1830 log_rule_debug(dev, rules, "Importing properties from results of builtin command '%s'", buf);
1831
1832 r = udev_builtin_run(dev, &event->rtnl, cmd, buf, false);
1833 if (r < 0) {
1834 /* remember failure */
1835 log_rule_debug_errno(dev, rules, r, "Failed to run builtin '%s': %m", buf);
1836 event->builtin_ret |= mask;
1837 }
1838 return token->op == (r >= 0 ? OP_MATCH : OP_NOMATCH);
1839 }
1840 case TK_M_IMPORT_DB: {
1841 const char *val;
1842
1843 if (!event->dev_db_clone)
1844 return token->op == OP_NOMATCH;
1845 r = sd_device_get_property_value(event->dev_db_clone, token->value, &val);
1846 if (r == -ENOENT)
1847 return token->op == OP_NOMATCH;
1848 if (r < 0)
1849 return log_rule_error_errno(dev, rules, r,
1850 "Failed to get property '%s' from database: %m",
1851 token->value);
1852
1853 r = device_add_property(dev, token->value, val);
1854 if (r < 0)
1855 return log_rule_error_errno(dev, rules, r, "Failed to add property '%s=%s': %m",
1856 token->value, val);
1857 return token->op == OP_MATCH;
1858 }
1859 case TK_M_IMPORT_CMDLINE: {
1860 _cleanup_free_ char *value = NULL;
1861
1862 r = proc_cmdline_get_key(token->value, PROC_CMDLINE_VALUE_OPTIONAL|PROC_CMDLINE_IGNORE_EFI_OPTIONS, &value);
1863 if (r < 0)
1864 return log_rule_error_errno(dev, rules, r,
1865 "Failed to read '%s' option from /proc/cmdline: %m",
1866 token->value);
1867 if (r == 0)
1868 return token->op == OP_NOMATCH;
1869
1870 r = device_add_property(dev, token->value, value ?: "1");
1871 if (r < 0)
1872 return log_rule_error_errno(dev, rules, r, "Failed to add property '%s=%s': %m",
1873 token->value, value ?: "1");
1874 return token->op == OP_MATCH;
1875 }
1876 case TK_M_IMPORT_PARENT: {
1877 char buf[UDEV_PATH_SIZE];
1878
1879 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
1880 r = import_parent_into_properties(dev, buf);
1881 if (r < 0)
1882 return log_rule_error_errno(dev, rules, r,
1883 "Failed to import properties '%s' from parent: %m",
1884 buf);
1885 return token->op == (r > 0 ? OP_MATCH : OP_NOMATCH);
1886 }
1887 case TK_M_RESULT:
1888 return token_match_string(token, event->program_result);
1889 case TK_A_OPTIONS_STRING_ESCAPE_NONE:
1890 event->esc = ESCAPE_NONE;
1891 break;
1892 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE:
1893 event->esc = ESCAPE_REPLACE;
1894 break;
1895 case TK_A_OPTIONS_DB_PERSIST:
1896 device_set_db_persist(dev);
1897 break;
1898 case TK_A_OPTIONS_INOTIFY_WATCH:
1899 if (event->inotify_watch_final)
1900 break;
1901 if (token->op == OP_ASSIGN_FINAL)
1902 event->inotify_watch_final = true;
1903
1904 event->inotify_watch = token->data;
1905 break;
1906 case TK_A_OPTIONS_DEVLINK_PRIORITY:
1907 device_set_devlink_priority(dev, PTR_TO_INT(token->data));
1908 break;
1909 case TK_A_OPTIONS_LOG_LEVEL: {
1910 int level = PTR_TO_INT(token->data);
1911
1912 if (level < 0)
1913 level = event->default_log_level;
1914
1915 log_set_max_level(level);
1916
1917 if (level == LOG_DEBUG && !event->log_level_was_debug) {
1918 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
1919 log_device_uevent(dev, "The log level is changed to 'debug' while processing device");
1920 event->log_level_was_debug = true;
1921 }
1922
1923 break;
1924 }
1925 case TK_A_OWNER: {
1926 char owner[UDEV_NAME_SIZE];
1927 const char *ow = owner;
1928
1929 if (event->owner_final)
1930 break;
1931 if (token->op == OP_ASSIGN_FINAL)
1932 event->owner_final = true;
1933
1934 (void) udev_event_apply_format(event, token->value, owner, sizeof(owner), false);
1935 r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
1936 if (r < 0)
1937 log_unknown_owner(dev, rules, r, "user", owner);
1938 else
1939 log_rule_debug(dev, rules, "OWNER %s(%u)", owner, event->uid);
1940 break;
1941 }
1942 case TK_A_GROUP: {
1943 char group[UDEV_NAME_SIZE];
1944 const char *gr = group;
1945
1946 if (event->group_final)
1947 break;
1948 if (token->op == OP_ASSIGN_FINAL)
1949 event->group_final = true;
1950
1951 (void) udev_event_apply_format(event, token->value, group, sizeof(group), false);
1952 r = get_group_creds(&gr, &event->gid, USER_CREDS_ALLOW_MISSING);
1953 if (r < 0)
1954 log_unknown_owner(dev, rules, r, "group", group);
1955 else
1956 log_rule_debug(dev, rules, "GROUP %s(%u)", group, event->gid);
1957 break;
1958 }
1959 case TK_A_MODE: {
1960 char mode_str[UDEV_NAME_SIZE];
1961
1962 if (event->mode_final)
1963 break;
1964 if (token->op == OP_ASSIGN_FINAL)
1965 event->mode_final = true;
1966
1967 (void) udev_event_apply_format(event, token->value, mode_str, sizeof(mode_str), false);
1968 r = parse_mode(mode_str, &event->mode);
1969 if (r < 0)
1970 log_rule_error_errno(dev, rules, r, "Failed to parse mode '%s', ignoring: %m", mode_str);
1971 else
1972 log_rule_debug(dev, rules, "MODE %#o", event->mode);
1973 break;
1974 }
1975 case TK_A_OWNER_ID:
1976 if (event->owner_final)
1977 break;
1978 if (token->op == OP_ASSIGN_FINAL)
1979 event->owner_final = true;
1980 if (!token->data)
1981 break;
1982 event->uid = PTR_TO_UID(token->data);
1983 log_rule_debug(dev, rules, "OWNER %u", event->uid);
1984 break;
1985 case TK_A_GROUP_ID:
1986 if (event->group_final)
1987 break;
1988 if (token->op == OP_ASSIGN_FINAL)
1989 event->group_final = true;
1990 if (!token->data)
1991 break;
1992 event->gid = PTR_TO_GID(token->data);
1993 log_rule_debug(dev, rules, "GROUP %u", event->gid);
1994 break;
1995 case TK_A_MODE_ID:
1996 if (event->mode_final)
1997 break;
1998 if (token->op == OP_ASSIGN_FINAL)
1999 event->mode_final = true;
2000 if (!token->data)
2001 break;
2002 event->mode = PTR_TO_MODE(token->data);
2003 log_rule_debug(dev, rules, "MODE %#o", event->mode);
2004 break;
2005 case TK_A_SECLABEL: {
2006 _cleanup_free_ char *name = NULL, *label = NULL;
2007 char label_str[UDEV_LINE_SIZE] = {};
2008
2009 name = strdup(token->data);
2010 if (!name)
2011 return log_oom();
2012
2013 (void) udev_event_apply_format(event, token->value, label_str, sizeof(label_str), false);
2014 if (!isempty(label_str))
2015 label = strdup(label_str);
2016 else
2017 label = strdup(token->value);
2018 if (!label)
2019 return log_oom();
2020
2021 if (token->op == OP_ASSIGN)
2022 ordered_hashmap_clear_free_free(event->seclabel_list);
2023
2024 r = ordered_hashmap_ensure_put(&event->seclabel_list, NULL, name, label);
2025 if (r == -ENOMEM)
2026 return log_oom();
2027 if (r < 0)
2028 return log_rule_error_errno(dev, rules, r, "Failed to store SECLABEL{%s}='%s': %m", name, label);;
2029
2030 log_rule_debug(dev, rules, "SECLABEL{%s}='%s'", name, label);
2031
2032 TAKE_PTR(name);
2033 TAKE_PTR(label);
2034 break;
2035 }
2036 case TK_A_ENV: {
2037 const char *val, *name = token->data;
2038 char value_new[UDEV_NAME_SIZE], *p = value_new;
2039 size_t count, l = sizeof(value_new);
2040
2041 if (isempty(token->value)) {
2042 if (token->op == OP_ADD)
2043 break;
2044 r = device_add_property(dev, name, NULL);
2045 if (r < 0)
2046 return log_rule_error_errno(dev, rules, r, "Failed to remove property '%s': %m", name);
2047 break;
2048 }
2049
2050 if (token->op == OP_ADD &&
2051 sd_device_get_property_value(dev, name, &val) >= 0)
2052 l = strpcpyl(&p, l, val, " ", NULL);
2053
2054 (void) udev_event_apply_format(event, token->value, p, l, false);
2055 if (event->esc == ESCAPE_REPLACE) {
2056 count = udev_replace_chars(p, NULL);
2057 if (count > 0)
2058 log_rule_debug(dev, rules, "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"",
2059 count, name, token->op == OP_ADD ? "+" : "", token->value);
2060 }
2061
2062 r = device_add_property(dev, name, value_new);
2063 if (r < 0)
2064 return log_rule_error_errno(dev, rules, r, "Failed to add property '%s=%s': %m", name, value_new);
2065 break;
2066 }
2067 case TK_A_TAG: {
2068 char buf[UDEV_PATH_SIZE];
2069
2070 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
2071 if (token->op == OP_ASSIGN)
2072 device_cleanup_tags(dev);
2073
2074 if (buf[strspn(buf, ALPHANUMERICAL "-_")] != '\0') {
2075 log_rule_error(dev, rules, "Invalid tag name '%s', ignoring", buf);
2076 break;
2077 }
2078 if (token->op == OP_REMOVE)
2079 device_remove_tag(dev, buf);
2080 else {
2081 r = device_add_tag(dev, buf, true);
2082 if (r < 0)
2083 return log_rule_error_errno(dev, rules, r, "Failed to add tag '%s': %m", buf);
2084 }
2085 break;
2086 }
2087 case TK_A_NAME: {
2088 char buf[UDEV_PATH_SIZE];
2089 size_t count;
2090
2091 if (event->name_final)
2092 break;
2093 if (token->op == OP_ASSIGN_FINAL)
2094 event->name_final = true;
2095
2096 if (sd_device_get_ifindex(dev, NULL) < 0) {
2097 log_rule_error(dev, rules,
2098 "Only network interfaces can be renamed, ignoring NAME=\"%s\".",
2099 token->value);
2100 break;
2101 }
2102
2103 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
2104 if (IN_SET(event->esc, ESCAPE_UNSET, ESCAPE_REPLACE)) {
2105 if (naming_scheme_has(NAMING_REPLACE_STRICTLY))
2106 count = udev_replace_ifname(buf);
2107 else
2108 count = udev_replace_chars(buf, "/");
2109 if (count > 0)
2110 log_rule_debug(dev, rules, "Replaced %zu character(s) from result of NAME=\"%s\"",
2111 count, token->value);
2112 }
2113 r = free_and_strdup_warn(&event->name, buf);
2114 if (r < 0)
2115 return r;
2116
2117 log_rule_debug(dev, rules, "NAME '%s'", event->name);
2118 break;
2119 }
2120 case TK_A_DEVLINK: {
2121 char buf[UDEV_PATH_SIZE], *p;
2122 size_t count;
2123
2124 if (event->devlink_final)
2125 break;
2126 if (sd_device_get_devnum(dev, NULL) < 0)
2127 break;
2128 if (token->op == OP_ASSIGN_FINAL)
2129 event->devlink_final = true;
2130 if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL))
2131 device_cleanup_devlinks(dev);
2132
2133 /* allow multiple symlinks separated by spaces */
2134 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), event->esc != ESCAPE_NONE);
2135 if (event->esc == ESCAPE_UNSET)
2136 count = udev_replace_chars(buf, "/ ");
2137 else if (event->esc == ESCAPE_REPLACE)
2138 count = udev_replace_chars(buf, "/");
2139 else
2140 count = 0;
2141 if (count > 0)
2142 log_rule_debug(dev, rules, "Replaced %zu character(s) from result of SYMLINK=\"%s\"",
2143 count, token->value);
2144
2145 p = skip_leading_chars(buf, NULL);
2146 while (!isempty(p)) {
2147 char filename[UDEV_PATH_SIZE], *next;
2148
2149 next = strchr(p, ' ');
2150 if (next) {
2151 *next++ = '\0';
2152 next = skip_leading_chars(next, NULL);
2153 }
2154
2155 strscpyl(filename, sizeof(filename), "/dev/", p, NULL);
2156 r = device_add_devlink(dev, filename);
2157 if (r < 0)
2158 return log_rule_error_errno(dev, rules, r, "Failed to add devlink '%s': %m", filename);
2159
2160 log_rule_debug(dev, rules, "LINK '%s'", p);
2161 p = next;
2162 }
2163 break;
2164 }
2165 case TK_A_ATTR: {
2166 char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
2167 const char *val, *key_name = token->data;
2168
2169 if (udev_resolve_subsys_kernel(key_name, buf, sizeof(buf), false) < 0 &&
2170 sd_device_get_syspath(dev, &val) >= 0)
2171 strscpyl(buf, sizeof(buf), val, "/", key_name, NULL);
2172
2173 r = attr_subst_subdir(buf);
2174 if (r < 0) {
2175 log_rule_error_errno(dev, rules, r, "Could not find file matches '%s', ignoring: %m", buf);
2176 break;
2177 }
2178 (void) udev_event_apply_format(event, token->value, value, sizeof(value), false);
2179
2180 log_rule_debug(dev, rules, "ATTR '%s' writing '%s'", buf, value);
2181 r = write_string_file(buf, value,
2182 WRITE_STRING_FILE_VERIFY_ON_FAILURE |
2183 WRITE_STRING_FILE_DISABLE_BUFFER |
2184 WRITE_STRING_FILE_AVOID_NEWLINE |
2185 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE);
2186 if (r < 0)
2187 log_rule_error_errno(dev, rules, r, "Failed to write ATTR{%s}, ignoring: %m", buf);
2188 break;
2189 }
2190 case TK_A_SYSCTL: {
2191 char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
2192
2193 (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false);
2194 (void) udev_event_apply_format(event, token->value, value, sizeof(value), false);
2195 sysctl_normalize(buf);
2196 log_rule_debug(dev, rules, "SYSCTL '%s' writing '%s'", buf, value);
2197 r = sysctl_write(buf, value);
2198 if (r < 0)
2199 log_rule_error_errno(dev, rules, r, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf, value);
2200 break;
2201 }
2202 case TK_A_RUN_BUILTIN:
2203 case TK_A_RUN_PROGRAM: {
2204 _cleanup_free_ char *cmd = NULL;
2205 char buf[UDEV_PATH_SIZE];
2206
2207 if (event->run_final)
2208 break;
2209 if (token->op == OP_ASSIGN_FINAL)
2210 event->run_final = true;
2211
2212 if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL))
2213 ordered_hashmap_clear_free_key(event->run_list);
2214
2215 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
2216
2217 cmd = strdup(buf);
2218 if (!cmd)
2219 return log_oom();
2220
2221 r = ordered_hashmap_ensure_put(&event->run_list, NULL, cmd, token->data);
2222 if (r == -ENOMEM)
2223 return log_oom();
2224 if (r < 0)
2225 return log_rule_error_errno(dev, rules, r, "Failed to store command '%s': %m", cmd);
2226
2227 TAKE_PTR(cmd);
2228
2229 log_rule_debug(dev, rules, "RUN '%s'", token->value);
2230 break;
2231 }
2232 case TK_A_OPTIONS_STATIC_NODE:
2233 /* do nothing for events. */
2234 break;
2235 default:
2236 assert_not_reached();
2237 }
2238
2239 return true;
2240 }
2241
2242 static bool token_is_for_parents(UdevRuleToken *token) {
2243 return token->type >= TK_M_PARENTS_KERNEL && token->type <= TK_M_PARENTS_TAG;
2244 }
2245
2246 static int udev_rule_apply_parent_token_to_event(
2247 UdevRules *rules,
2248 UdevEvent *event,
2249 int timeout_signal) {
2250
2251 UdevRuleLine *line;
2252 UdevRuleToken *head;
2253 int r;
2254
2255 line = rules->current_file->current_line;
2256 head = rules->current_file->current_line->current_token;
2257 event->dev_parent = event->dev;
2258 for (;;) {
2259 LIST_FOREACH(tokens, line->current_token, head) {
2260 if (!token_is_for_parents(line->current_token))
2261 return true; /* All parent tokens match. */
2262 r = udev_rule_apply_token_to_event(rules, event->dev_parent, event, 0, timeout_signal, NULL);
2263 if (r < 0)
2264 return r;
2265 if (r == 0)
2266 break;
2267 }
2268 if (!line->current_token)
2269 /* All parent tokens match. But no assign tokens in the line. Hmm... */
2270 return true;
2271
2272 if (sd_device_get_parent(event->dev_parent, &event->dev_parent) < 0) {
2273 event->dev_parent = NULL;
2274 return false;
2275 }
2276 }
2277 }
2278
2279 static int udev_rule_apply_line_to_event(
2280 UdevRules *rules,
2281 UdevEvent *event,
2282 usec_t timeout_usec,
2283 int timeout_signal,
2284 Hashmap *properties_list,
2285 UdevRuleLine **next_line) {
2286
2287 UdevRuleLine *line = rules->current_file->current_line;
2288 UdevRuleLineType mask = LINE_HAS_GOTO | LINE_UPDATE_SOMETHING;
2289 UdevRuleToken *token, *next_token;
2290 bool parents_done = false;
2291 sd_device_action_t action;
2292 int r;
2293
2294 r = sd_device_get_action(event->dev, &action);
2295 if (r < 0)
2296 return r;
2297
2298 if (action != SD_DEVICE_REMOVE) {
2299 if (sd_device_get_devnum(event->dev, NULL) >= 0)
2300 mask |= LINE_HAS_DEVLINK;
2301
2302 if (sd_device_get_ifindex(event->dev, NULL) >= 0)
2303 mask |= LINE_HAS_NAME;
2304 }
2305
2306 if ((line->type & mask) == 0)
2307 return 0;
2308
2309 event->esc = ESCAPE_UNSET;
2310
2311 DEVICE_TRACE_POINT(rules_apply_line, event->dev, line->rule_file->filename, line->line_number);
2312
2313 LIST_FOREACH_SAFE(tokens, token, next_token, line->tokens) {
2314 line->current_token = token;
2315
2316 if (token_is_for_parents(token)) {
2317 if (parents_done)
2318 continue;
2319
2320 r = udev_rule_apply_parent_token_to_event(rules, event, timeout_signal);
2321 if (r <= 0)
2322 return r;
2323
2324 parents_done = true;
2325 continue;
2326 }
2327
2328 r = udev_rule_apply_token_to_event(rules, event->dev, event, timeout_usec, timeout_signal, properties_list);
2329 if (r <= 0)
2330 return r;
2331 }
2332
2333 if (line->goto_line)
2334 *next_line = line->goto_line;
2335
2336 return 0;
2337 }
2338
2339 int udev_rules_apply_to_event(
2340 UdevRules *rules,
2341 UdevEvent *event,
2342 usec_t timeout_usec,
2343 int timeout_signal,
2344 Hashmap *properties_list) {
2345
2346 UdevRuleFile *file;
2347 UdevRuleLine *next_line;
2348 int r;
2349
2350 assert(rules);
2351 assert(event);
2352
2353 LIST_FOREACH(rule_files, file, rules->rule_files) {
2354 rules->current_file = file;
2355 LIST_FOREACH_SAFE(rule_lines, file->current_line, next_line, file->rule_lines) {
2356 r = udev_rule_apply_line_to_event(rules, event, timeout_usec, timeout_signal, properties_list, &next_line);
2357 if (r < 0)
2358 return r;
2359 }
2360 }
2361
2362 return 0;
2363 }
2364
2365 static int apply_static_dev_perms(const char *devnode, uid_t uid, gid_t gid, mode_t mode, char **tags) {
2366 char device_node[UDEV_PATH_SIZE], tags_dir[UDEV_PATH_SIZE], tag_symlink[UDEV_PATH_SIZE];
2367 _cleanup_free_ char *unescaped_filename = NULL;
2368 struct stat stats;
2369 char **t;
2370 int r;
2371
2372 assert(devnode);
2373
2374 if (uid == UID_INVALID && gid == GID_INVALID && mode == MODE_INVALID && !tags)
2375 return 0;
2376
2377 strscpyl(device_node, sizeof(device_node), "/dev/", devnode, NULL);
2378 if (stat(device_node, &stats) < 0) {
2379 if (errno != ENOENT)
2380 return log_error_errno(errno, "Failed to stat %s: %m", device_node);
2381 return 0;
2382 }
2383
2384 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode)) {
2385 log_warning("%s is neither block nor character device, ignoring.", device_node);
2386 return 0;
2387 }
2388
2389 if (!strv_isempty(tags)) {
2390 unescaped_filename = xescape(devnode, "/.");
2391 if (!unescaped_filename)
2392 return log_oom();
2393 }
2394
2395 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2396 STRV_FOREACH(t, tags) {
2397 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2398 r = mkdir_p(tags_dir, 0755);
2399 if (r < 0)
2400 return log_error_errno(r, "Failed to create %s: %m", tags_dir);
2401
2402 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2403 r = symlink(device_node, tag_symlink);
2404 if (r < 0 && errno != EEXIST)
2405 return log_error_errno(errno, "Failed to create symlink %s -> %s: %m",
2406 tag_symlink, device_node);
2407 }
2408
2409 /* don't touch the permissions if only the tags were set */
2410 if (uid == UID_INVALID && gid == GID_INVALID && mode == MODE_INVALID)
2411 return 0;
2412
2413 if (mode == MODE_INVALID)
2414 mode = gid_is_valid(gid) ? 0660 : 0600;
2415 if (!uid_is_valid(uid))
2416 uid = 0;
2417 if (!gid_is_valid(gid))
2418 gid = 0;
2419
2420 r = chmod_and_chown(device_node, mode, uid, gid);
2421 if (r == -ENOENT)
2422 return 0;
2423 if (r < 0)
2424 return log_error_errno(r, "Failed to chown '%s' %u %u: %m", device_node, uid, gid);
2425 else
2426 log_debug("chown '%s' %u:%u with mode %#o", device_node, uid, gid, mode);
2427
2428 (void) utimensat(AT_FDCWD, device_node, NULL, 0);
2429 return 0;
2430 }
2431
2432 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine *rule_line) {
2433 UdevRuleToken *token;
2434 _cleanup_strv_free_ char **tags = NULL;
2435 uid_t uid = UID_INVALID;
2436 gid_t gid = GID_INVALID;
2437 mode_t mode = MODE_INVALID;
2438 int r;
2439
2440 assert(rule_line);
2441
2442 if (!FLAGS_SET(rule_line->type, LINE_HAS_STATIC_NODE))
2443 return 0;
2444
2445 LIST_FOREACH(tokens, token, rule_line->tokens)
2446 if (token->type == TK_A_OWNER_ID)
2447 uid = PTR_TO_UID(token->data);
2448 else if (token->type == TK_A_GROUP_ID)
2449 gid = PTR_TO_GID(token->data);
2450 else if (token->type == TK_A_MODE_ID)
2451 mode = PTR_TO_MODE(token->data);
2452 else if (token->type == TK_A_TAG) {
2453 r = strv_extend(&tags, token->value);
2454 if (r < 0)
2455 return log_oom();
2456 } else if (token->type == TK_A_OPTIONS_STATIC_NODE) {
2457 r = apply_static_dev_perms(token->value, uid, gid, mode, tags);
2458 if (r < 0)
2459 return r;
2460 }
2461
2462 return 0;
2463 }
2464
2465 int udev_rules_apply_static_dev_perms(UdevRules *rules) {
2466 UdevRuleFile *file;
2467 UdevRuleLine *line;
2468 int r;
2469
2470 assert(rules);
2471
2472 LIST_FOREACH(rule_files, file, rules->rule_files)
2473 LIST_FOREACH(rule_lines, line, file->rule_lines) {
2474 r = udev_rule_line_apply_static_dev_perms(line);
2475 if (r < 0)
2476 return r;
2477 }
2478
2479 return 0;
2480 }