]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-rules.c
Merge pull request #20020 from anitazha/oomd_with_mem
[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-rules.h"
33 #include "udev-util.h"
34 #include "user-util.h"
35 #include "virt.h"
36
37 #define RULES_DIRS (const char* const*) CONF_PATHS_STRV("udev/rules.d")
38
39 typedef enum {
40 OP_MATCH, /* == */
41 OP_NOMATCH, /* != */
42 OP_ADD, /* += */
43 OP_REMOVE, /* -= */
44 OP_ASSIGN, /* = */
45 OP_ASSIGN_FINAL, /* := */
46 _OP_TYPE_MAX,
47 _OP_TYPE_INVALID = -EINVAL,
48 } UdevRuleOperatorType;
49
50 typedef enum {
51 MATCH_TYPE_EMPTY, /* empty string */
52 MATCH_TYPE_PLAIN, /* no special characters */
53 MATCH_TYPE_PLAIN_WITH_EMPTY, /* no special characters with empty string, e.g., "|foo" */
54 MATCH_TYPE_GLOB, /* shell globs ?,*,[] */
55 MATCH_TYPE_GLOB_WITH_EMPTY, /* shell globs ?,*,[] with empty string, e.g., "|foo*" */
56 MATCH_TYPE_SUBSYSTEM, /* "subsystem", "bus", or "class" */
57 _MATCH_TYPE_MAX,
58 _MATCH_TYPE_INVALID = -EINVAL,
59 } UdevRuleMatchType;
60
61 typedef enum {
62 SUBST_TYPE_PLAIN, /* no substitution */
63 SUBST_TYPE_FORMAT, /* % or $ */
64 SUBST_TYPE_SUBSYS, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
65 _SUBST_TYPE_MAX,
66 _SUBST_TYPE_INVALID = -EINVAL,
67 } UdevRuleSubstituteType;
68
69 typedef enum {
70 /* lvalues which take match or nomatch operator */
71 TK_M_ACTION, /* string, device_get_action() */
72 TK_M_DEVPATH, /* path, sd_device_get_devpath() */
73 TK_M_KERNEL, /* string, sd_device_get_sysname() */
74 TK_M_DEVLINK, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */
75 TK_M_NAME, /* string, name of network interface */
76 TK_M_ENV, /* string, device property, takes key through attribute */
77 TK_M_CONST, /* string, system-specific hard-coded constant */
78 TK_M_TAG, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */
79 TK_M_SUBSYSTEM, /* string, sd_device_get_subsystem() */
80 TK_M_DRIVER, /* string, sd_device_get_driver() */
81 TK_M_ATTR, /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */
82 TK_M_SYSCTL, /* string, takes kernel parameter through attribute */
83
84 /* matches parent parameters */
85 TK_M_PARENTS_KERNEL, /* string */
86 TK_M_PARENTS_SUBSYSTEM, /* string */
87 TK_M_PARENTS_DRIVER, /* string */
88 TK_M_PARENTS_ATTR, /* string */
89 TK_M_PARENTS_TAG, /* strv */
90
91 TK_M_TEST, /* path, optionally mode_t can be specified by attribute, test the existence of a file */
92 TK_M_PROGRAM, /* string, execute a program */
93 TK_M_IMPORT_FILE, /* path */
94 TK_M_IMPORT_PROGRAM, /* string, import properties from the result of program */
95 TK_M_IMPORT_BUILTIN, /* string, import properties from the result of built-in command */
96 TK_M_IMPORT_DB, /* string, import properties from database */
97 TK_M_IMPORT_CMDLINE, /* string, kernel command line */
98 TK_M_IMPORT_PARENT, /* string, parent property */
99 TK_M_RESULT, /* string, result of TK_M_PROGRAM */
100
101 #define _TK_M_MAX (TK_M_RESULT + 1)
102 #define _TK_A_MIN _TK_M_MAX
103
104 /* lvalues which take one of assign operators */
105 TK_A_OPTIONS_STRING_ESCAPE_NONE, /* no argument */
106 TK_A_OPTIONS_STRING_ESCAPE_REPLACE, /* no argument */
107 TK_A_OPTIONS_DB_PERSIST, /* no argument */
108 TK_A_OPTIONS_INOTIFY_WATCH, /* boolean */
109 TK_A_OPTIONS_DEVLINK_PRIORITY, /* int */
110 TK_A_OPTIONS_LOG_LEVEL, /* string of log level or "reset" */
111 TK_A_OWNER, /* user name */
112 TK_A_GROUP, /* group name */
113 TK_A_MODE, /* mode string */
114 TK_A_OWNER_ID, /* uid_t */
115 TK_A_GROUP_ID, /* gid_t */
116 TK_A_MODE_ID, /* mode_t */
117 TK_A_TAG, /* string */
118 TK_A_OPTIONS_STATIC_NODE, /* device path, /dev/... */
119 TK_A_SECLABEL, /* string with attribute */
120 TK_A_ENV, /* string with attribute */
121 TK_A_NAME, /* ifname */
122 TK_A_DEVLINK, /* string */
123 TK_A_ATTR, /* string with attribute */
124 TK_A_SYSCTL, /* string with attribute */
125 TK_A_RUN_BUILTIN, /* string */
126 TK_A_RUN_PROGRAM, /* string */
127
128 _TK_TYPE_MAX,
129 _TK_TYPE_INVALID = -EINVAL,
130 } UdevRuleTokenType;
131
132 typedef enum {
133 LINE_HAS_NAME = 1 << 0, /* has NAME= */
134 LINE_HAS_DEVLINK = 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */
135 LINE_HAS_STATIC_NODE = 1 << 2, /* has OPTIONS=static_node */
136 LINE_HAS_GOTO = 1 << 3, /* has GOTO= */
137 LINE_HAS_LABEL = 1 << 4, /* has LABEL= */
138 LINE_UPDATE_SOMETHING = 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */
139 } UdevRuleLineType;
140
141 typedef struct UdevRuleFile UdevRuleFile;
142 typedef struct UdevRuleLine UdevRuleLine;
143 typedef struct UdevRuleToken UdevRuleToken;
144
145 struct UdevRuleToken {
146 UdevRuleTokenType type:8;
147 UdevRuleOperatorType op:8;
148 UdevRuleMatchType match_type:8;
149 UdevRuleSubstituteType attr_subst_type:7;
150 bool attr_match_remove_trailing_whitespace:1;
151 const char *value;
152 void *data;
153 LIST_FIELDS(UdevRuleToken, tokens);
154 };
155
156 struct UdevRuleLine {
157 char *line;
158 unsigned line_number;
159 UdevRuleLineType type;
160
161 const char *label;
162 const char *goto_label;
163 UdevRuleLine *goto_line;
164
165 UdevRuleFile *rule_file;
166 UdevRuleToken *current_token;
167 LIST_HEAD(UdevRuleToken, tokens);
168 LIST_FIELDS(UdevRuleLine, rule_lines);
169 };
170
171 struct UdevRuleFile {
172 char *filename;
173 UdevRuleLine *current_line;
174 LIST_HEAD(UdevRuleLine, rule_lines);
175 LIST_FIELDS(UdevRuleFile, rule_files);
176 };
177
178 struct UdevRules {
179 usec_t dirs_ts_usec;
180 ResolveNameTiming resolve_name_timing;
181 Hashmap *known_users;
182 Hashmap *known_groups;
183 UdevRuleFile *current_file;
184 LIST_HEAD(UdevRuleFile, rule_files);
185 };
186
187 /*** Logging helpers ***/
188
189 #define log_rule_full_errno_zerook(device, rules, level, error, fmt, ...) \
190 ({ \
191 UdevRules *_r = (rules); \
192 UdevRuleFile *_f = _r ? _r->current_file : NULL; \
193 UdevRuleLine *_l = _f ? _f->current_line : NULL; \
194 const char *_n = _f ? _f->filename : NULL; \
195 \
196 log_device_full_errno_zerook( \
197 device, level, error, "%s:%u " fmt, \
198 strna(_n), _l ? _l->line_number : 0, \
199 ##__VA_ARGS__); \
200 })
201
202 #define log_rule_full_errno(device, rules, level, error, fmt, ...) \
203 ({ \
204 int _error = (error); \
205 ASSERT_NON_ZERO(_error); \
206 log_rule_full_errno_zerook( \
207 device, rules, level, _error, fmt, ##__VA_ARGS__); \
208 })
209
210 #define log_rule_full(device, rules, level, ...) (void) log_rule_full_errno_zerook(device, rules, level, 0, __VA_ARGS__)
211
212 #define log_rule_debug(device, rules, ...) log_rule_full(device, rules, LOG_DEBUG, __VA_ARGS__)
213 #define log_rule_info(device, rules, ...) log_rule_full(device, rules, LOG_INFO, __VA_ARGS__)
214 #define log_rule_notice(device, rules, ...) log_rule_full(device, rules, LOG_NOTICE, __VA_ARGS__)
215 #define log_rule_warning(device, rules, ...) log_rule_full(device, rules, LOG_WARNING, __VA_ARGS__)
216 #define log_rule_error(device, rules, ...) log_rule_full(device, rules, LOG_ERR, __VA_ARGS__)
217
218 #define log_rule_debug_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_DEBUG, error, __VA_ARGS__)
219 #define log_rule_info_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_INFO, error, __VA_ARGS__)
220 #define log_rule_notice_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_NOTICE, error, __VA_ARGS__)
221 #define log_rule_warning_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_WARNING, error, __VA_ARGS__)
222 #define log_rule_error_errno(device, rules, error, ...) log_rule_full_errno(device, rules, LOG_ERR, error, __VA_ARGS__)
223
224 #define log_token_full_errno_zerook(rules, level, error, ...) log_rule_full_errno_zerook(NULL, rules, level, error, __VA_ARGS__)
225 #define log_token_full_errno(rules, level, error, ...) log_rule_full_errno(NULL, rules, level, error, __VA_ARGS__)
226 #define log_token_full(rules, level, ...) (void) log_token_full_errno_zerook(rules, level, 0, __VA_ARGS__)
227
228 #define log_token_debug(rules, ...) log_token_full(rules, LOG_DEBUG, __VA_ARGS__)
229 #define log_token_info(rules, ...) log_token_full(rules, LOG_INFO, __VA_ARGS__)
230 #define log_token_notice(rules, ...) log_token_full(rules, LOG_NOTICE, __VA_ARGS__)
231 #define log_token_warning(rules, ...) log_token_full(rules, LOG_WARNING, __VA_ARGS__)
232 #define log_token_error(rules, ...) log_token_full(rules, LOG_ERR, __VA_ARGS__)
233
234 #define log_token_debug_errno(rules, error, ...) log_token_full_errno(rules, LOG_DEBUG, error, __VA_ARGS__)
235 #define log_token_info_errno(rules, error, ...) log_token_full_errno(rules, LOG_INFO, error, __VA_ARGS__)
236 #define log_token_notice_errno(rules, error, ...) log_token_full_errno(rules, LOG_NOTICE, error, __VA_ARGS__)
237 #define log_token_warning_errno(rules, error, ...) log_token_full_errno(rules, LOG_WARNING, error, __VA_ARGS__)
238 #define log_token_error_errno(rules, error, ...) log_token_full_errno(rules, LOG_ERR, error, __VA_ARGS__)
239
240 #define _log_token_invalid(rules, key, type) \
241 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
242 "Invalid %s for %s.", type, key)
243
244 #define log_token_invalid_op(rules, key) _log_token_invalid(rules, key, "operator")
245 #define log_token_invalid_attr(rules, key) _log_token_invalid(rules, key, "attribute")
246
247 #define log_token_invalid_attr_format(rules, key, attr, offset, hint) \
248 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
249 "Invalid attribute \"%s\" for %s (char %zu: %s), ignoring.", \
250 attr, key, offset, hint)
251 #define log_token_invalid_value(rules, key, value, offset, hint) \
252 log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), \
253 "Invalid value \"%s\" for %s (char %zu: %s), ignoring.", \
254 value, key, offset, hint)
255
256 static void log_unknown_owner(sd_device *dev, UdevRules *rules, int error, const char *entity, const char *name) {
257 if (IN_SET(abs(error), ENOENT, ESRCH))
258 log_rule_error(dev, rules, "Unknown %s '%s', ignoring", entity, name);
259 else
260 log_rule_error_errno(dev, rules, error, "Failed to resolve %s '%s', ignoring: %m", entity, name);
261 }
262
263 /*** Other functions ***/
264
265 static void udev_rule_token_free(UdevRuleToken *token) {
266 free(token);
267 }
268
269 static void udev_rule_line_clear_tokens(UdevRuleLine *rule_line) {
270 UdevRuleToken *i, *next;
271
272 assert(rule_line);
273
274 LIST_FOREACH_SAFE(tokens, i, next, rule_line->tokens)
275 udev_rule_token_free(i);
276
277 rule_line->tokens = NULL;
278 }
279
280 static UdevRuleLine* udev_rule_line_free(UdevRuleLine *rule_line) {
281 if (!rule_line)
282 return NULL;
283
284 udev_rule_line_clear_tokens(rule_line);
285
286 if (rule_line->rule_file) {
287 if (rule_line->rule_file->current_line == rule_line)
288 rule_line->rule_file->current_line = rule_line->rule_lines_prev;
289
290 LIST_REMOVE(rule_lines, rule_line->rule_file->rule_lines, rule_line);
291 }
292
293 free(rule_line->line);
294 return mfree(rule_line);
295 }
296
297 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine*, udev_rule_line_free);
298
299 static void udev_rule_file_free(UdevRuleFile *rule_file) {
300 UdevRuleLine *i, *next;
301
302 if (!rule_file)
303 return;
304
305 LIST_FOREACH_SAFE(rule_lines, i, next, rule_file->rule_lines)
306 udev_rule_line_free(i);
307
308 free(rule_file->filename);
309 free(rule_file);
310 }
311
312 UdevRules *udev_rules_free(UdevRules *rules) {
313 UdevRuleFile *i, *next;
314
315 if (!rules)
316 return NULL;
317
318 LIST_FOREACH_SAFE(rule_files, i, next, rules->rule_files)
319 udev_rule_file_free(i);
320
321 hashmap_free_free_key(rules->known_users);
322 hashmap_free_free_key(rules->known_groups);
323 return mfree(rules);
324 }
325
326 static int rule_resolve_user(UdevRules *rules, const char *name, uid_t *ret) {
327 _cleanup_free_ char *n = NULL;
328 uid_t uid;
329 void *val;
330 int r;
331
332 assert(rules);
333 assert(name);
334
335 val = hashmap_get(rules->known_users, name);
336 if (val) {
337 *ret = PTR_TO_UID(val);
338 return 0;
339 }
340
341 r = get_user_creds(&name, &uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
342 if (r < 0) {
343 log_unknown_owner(NULL, rules, r, "user", name);
344 *ret = UID_INVALID;
345 return 0;
346 }
347
348 n = strdup(name);
349 if (!n)
350 return -ENOMEM;
351
352 r = hashmap_ensure_put(&rules->known_users, &string_hash_ops, n, UID_TO_PTR(uid));
353 if (r < 0)
354 return r;
355
356 TAKE_PTR(n);
357 *ret = uid;
358 return 0;
359 }
360
361 static int rule_resolve_group(UdevRules *rules, const char *name, gid_t *ret) {
362 _cleanup_free_ char *n = NULL;
363 gid_t gid;
364 void *val;
365 int r;
366
367 assert(rules);
368 assert(name);
369
370 val = hashmap_get(rules->known_groups, name);
371 if (val) {
372 *ret = PTR_TO_GID(val);
373 return 0;
374 }
375
376 r = get_group_creds(&name, &gid, USER_CREDS_ALLOW_MISSING);
377 if (r < 0) {
378 log_unknown_owner(NULL, rules, r, "group", name);
379 *ret = GID_INVALID;
380 return 0;
381 }
382
383 n = strdup(name);
384 if (!n)
385 return -ENOMEM;
386
387 r = hashmap_ensure_put(&rules->known_groups, &string_hash_ops, n, GID_TO_PTR(gid));
388 if (r < 0)
389 return r;
390
391 TAKE_PTR(n);
392 *ret = gid;
393 return 0;
394 }
395
396 static UdevRuleSubstituteType rule_get_substitution_type(const char *str) {
397 assert(str);
398
399 if (str[0] == '[')
400 return SUBST_TYPE_SUBSYS;
401 if (strchr(str, '%') || strchr(str, '$'))
402 return SUBST_TYPE_FORMAT;
403 return SUBST_TYPE_PLAIN;
404 }
405
406 static void rule_line_append_token(UdevRuleLine *rule_line, UdevRuleToken *token) {
407 assert(rule_line);
408 assert(token);
409
410 if (rule_line->current_token)
411 LIST_APPEND(tokens, rule_line->current_token, token);
412 else
413 LIST_APPEND(tokens, rule_line->tokens, token);
414
415 rule_line->current_token = token;
416 }
417
418 static int rule_line_add_token(UdevRuleLine *rule_line, UdevRuleTokenType type, UdevRuleOperatorType op, char *value, void *data) {
419 UdevRuleToken *token;
420 UdevRuleMatchType match_type = _MATCH_TYPE_INVALID;
421 UdevRuleSubstituteType subst_type = _SUBST_TYPE_INVALID;
422 bool remove_trailing_whitespace = false;
423 size_t len;
424
425 assert(rule_line);
426 assert(type >= 0 && type < _TK_TYPE_MAX);
427 assert(op >= 0 && op < _OP_TYPE_MAX);
428
429 if (type < _TK_M_MAX) {
430 assert(value);
431 assert(IN_SET(op, OP_MATCH, OP_NOMATCH));
432
433 if (type == TK_M_SUBSYSTEM && STR_IN_SET(value, "subsystem", "bus", "class"))
434 match_type = MATCH_TYPE_SUBSYSTEM;
435 else if (isempty(value))
436 match_type = MATCH_TYPE_EMPTY;
437 else if (streq(value, "?*")) {
438 /* Convert KEY=="?*" -> KEY!="" */
439 match_type = MATCH_TYPE_EMPTY;
440 op = op == OP_MATCH ? OP_NOMATCH : OP_MATCH;
441 } else if (string_is_glob(value))
442 match_type = MATCH_TYPE_GLOB;
443 else
444 match_type = MATCH_TYPE_PLAIN;
445
446 if (type < TK_M_TEST || type == TK_M_RESULT) {
447 /* Convert value string to nulstr. */
448 bool bar = true, empty = false;
449 char *a, *b;
450
451 for (a = b = value; *a != '\0'; a++) {
452 if (*a != '|') {
453 *b++ = *a;
454 bar = false;
455 } else {
456 if (bar)
457 empty = true;
458 else
459 *b++ = '\0';
460 bar = true;
461 }
462 }
463 *b = '\0';
464
465 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
466 if (b < a)
467 b[1] = '\0';
468
469 if (bar)
470 empty = true;
471
472 if (empty) {
473 if (match_type == MATCH_TYPE_GLOB)
474 match_type = MATCH_TYPE_GLOB_WITH_EMPTY;
475 if (match_type == MATCH_TYPE_PLAIN)
476 match_type = MATCH_TYPE_PLAIN_WITH_EMPTY;
477 }
478 }
479 }
480
481 if (IN_SET(type, TK_M_ATTR, TK_M_PARENTS_ATTR)) {
482 assert(value);
483 assert(data);
484
485 len = strlen(value);
486 if (len > 0 && !isspace(value[len - 1]))
487 remove_trailing_whitespace = true;
488
489 subst_type = rule_get_substitution_type(data);
490 }
491
492 token = new(UdevRuleToken, 1);
493 if (!token)
494 return -ENOMEM;
495
496 *token = (UdevRuleToken) {
497 .type = type,
498 .op = op,
499 .value = value,
500 .data = data,
501 .match_type = match_type,
502 .attr_subst_type = subst_type,
503 .attr_match_remove_trailing_whitespace = remove_trailing_whitespace,
504 };
505
506 rule_line_append_token(rule_line, token);
507
508 if (token->type == TK_A_NAME)
509 SET_FLAG(rule_line->type, LINE_HAS_NAME, true);
510
511 else if (IN_SET(token->type, TK_A_DEVLINK,
512 TK_A_OWNER, TK_A_GROUP, TK_A_MODE,
513 TK_A_OWNER_ID, TK_A_GROUP_ID, TK_A_MODE_ID))
514 SET_FLAG(rule_line->type, LINE_HAS_DEVLINK, true);
515
516 else if (token->type == TK_A_OPTIONS_STATIC_NODE)
517 SET_FLAG(rule_line->type, LINE_HAS_STATIC_NODE, true);
518
519 else if (token->type >= _TK_A_MIN ||
520 IN_SET(token->type, TK_M_PROGRAM,
521 TK_M_IMPORT_FILE, TK_M_IMPORT_PROGRAM, TK_M_IMPORT_BUILTIN,
522 TK_M_IMPORT_DB, TK_M_IMPORT_CMDLINE, TK_M_IMPORT_PARENT))
523 SET_FLAG(rule_line->type, LINE_UPDATE_SOMETHING, true);
524
525 return 0;
526 }
527
528 static void check_value_format_and_warn(UdevRules *rules, const char *key, const char *value, bool nonempty) {
529 size_t offset;
530 const char *hint;
531
532 if (nonempty && isempty(value))
533 log_token_invalid_value(rules, key, value, (size_t) 0, "empty value");
534 else if (udev_check_format(value, &offset, &hint) < 0)
535 log_token_invalid_value(rules, key, value, offset + 1, hint);
536 }
537
538 static int check_attr_format_and_warn(UdevRules *rules, const char *key, const char *value) {
539 size_t offset;
540 const char *hint;
541
542 if (isempty(value))
543 return log_token_invalid_attr(rules, key);
544 if (udev_check_format(value, &offset, &hint) < 0)
545 log_token_invalid_attr_format(rules, key, value, offset + 1, hint);
546 return 0;
547 }
548
549 static int parse_token(UdevRules *rules, const char *key, char *attr, UdevRuleOperatorType op, char *value) {
550 bool is_match = IN_SET(op, OP_MATCH, OP_NOMATCH);
551 UdevRuleLine *rule_line;
552 int r;
553
554 assert(rules);
555 assert(rules->current_file);
556 assert(rules->current_file->current_line);
557 assert(key);
558 assert(value);
559
560 rule_line = rules->current_file->current_line;
561
562 if (streq(key, "ACTION")) {
563 if (attr)
564 return log_token_invalid_attr(rules, key);
565 if (!is_match)
566 return log_token_invalid_op(rules, key);
567
568 r = rule_line_add_token(rule_line, TK_M_ACTION, op, value, NULL);
569 } else if (streq(key, "DEVPATH")) {
570 if (attr)
571 return log_token_invalid_attr(rules, key);
572 if (!is_match)
573 return log_token_invalid_op(rules, key);
574
575 r = rule_line_add_token(rule_line, TK_M_DEVPATH, op, value, NULL);
576 } else if (streq(key, "KERNEL")) {
577 if (attr)
578 return log_token_invalid_attr(rules, key);
579 if (!is_match)
580 return log_token_invalid_op(rules, key);
581
582 r = rule_line_add_token(rule_line, TK_M_KERNEL, op, value, NULL);
583 } else if (streq(key, "SYMLINK")) {
584 if (attr)
585 return log_token_invalid_attr(rules, key);
586 if (op == OP_REMOVE)
587 return log_token_invalid_op(rules, key);
588
589 if (!is_match) {
590 check_value_format_and_warn(rules, key, value, false);
591 r = rule_line_add_token(rule_line, TK_A_DEVLINK, op, value, NULL);
592 } else
593 r = rule_line_add_token(rule_line, TK_M_DEVLINK, op, value, NULL);
594 } else if (streq(key, "NAME")) {
595 if (attr)
596 return log_token_invalid_attr(rules, key);
597 if (op == OP_REMOVE)
598 return log_token_invalid_op(rules, key);
599 if (op == OP_ADD) {
600 log_token_warning(rules, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key);
601 op = OP_ASSIGN;
602 }
603
604 if (!is_match) {
605 if (streq(value, "%k"))
606 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
607 "Ignoring NAME=\"%%k\", as it will take no effect.");
608 if (isempty(value))
609 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
610 "Ignoring NAME=\"\", as udev will not delete any network interfaces.");
611 check_value_format_and_warn(rules, key, value, false);
612
613 r = rule_line_add_token(rule_line, TK_A_NAME, op, value, NULL);
614 } else
615 r = rule_line_add_token(rule_line, TK_M_NAME, op, value, NULL);
616 } else if (streq(key, "ENV")) {
617 if (isempty(attr))
618 return log_token_invalid_attr(rules, key);
619 if (op == OP_REMOVE)
620 return log_token_invalid_op(rules, key);
621 if (op == OP_ASSIGN_FINAL) {
622 log_token_warning(rules, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key);
623 op = OP_ASSIGN;
624 }
625
626 if (!is_match) {
627 if (STR_IN_SET(attr,
628 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
629 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
630 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
631 "Invalid ENV attribute. '%s' cannot be set.", attr);
632
633 check_value_format_and_warn(rules, key, value, false);
634
635 r = rule_line_add_token(rule_line, TK_A_ENV, op, value, attr);
636 } else
637 r = rule_line_add_token(rule_line, TK_M_ENV, op, value, attr);
638 } else if (streq(key, "CONST")) {
639 if (isempty(attr) || !STR_IN_SET(attr, "arch", "virt"))
640 return log_token_invalid_attr(rules, key);
641 if (!is_match)
642 return log_token_invalid_op(rules, key);
643 r = rule_line_add_token(rule_line, TK_M_CONST, op, value, attr);
644 } else if (streq(key, "TAG")) {
645 if (attr)
646 return log_token_invalid_attr(rules, key);
647 if (op == OP_ASSIGN_FINAL) {
648 log_token_warning(rules, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key);
649 op = OP_ASSIGN;
650 }
651
652 if (!is_match) {
653 check_value_format_and_warn(rules, key, value, true);
654
655 r = rule_line_add_token(rule_line, TK_A_TAG, op, value, NULL);
656 } else
657 r = rule_line_add_token(rule_line, TK_M_TAG, op, value, NULL);
658 } else if (streq(key, "SUBSYSTEM")) {
659 if (attr)
660 return log_token_invalid_attr(rules, key);
661 if (!is_match)
662 return log_token_invalid_op(rules, key);
663
664 if (STR_IN_SET(value, "bus", "class"))
665 log_token_warning(rules, "\"%s\" must be specified as \"subsystem\".", value);
666
667 r = rule_line_add_token(rule_line, TK_M_SUBSYSTEM, op, value, NULL);
668 } else if (streq(key, "DRIVER")) {
669 if (attr)
670 return log_token_invalid_attr(rules, key);
671 if (!is_match)
672 return log_token_invalid_op(rules, key);
673
674 r = rule_line_add_token(rule_line, TK_M_DRIVER, op, value, NULL);
675 } else if (streq(key, "ATTR")) {
676 r = check_attr_format_and_warn(rules, key, attr);
677 if (r < 0)
678 return r;
679 if (op == OP_REMOVE)
680 return log_token_invalid_op(rules, key);
681 if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) {
682 log_token_warning(rules, "%s key takes '==', '!=', or '=' operator, assuming '='.", key);
683 op = OP_ASSIGN;
684 }
685
686 if (!is_match) {
687 check_value_format_and_warn(rules, key, value, false);
688 r = rule_line_add_token(rule_line, TK_A_ATTR, op, value, attr);
689 } else
690 r = rule_line_add_token(rule_line, TK_M_ATTR, op, value, attr);
691 } else if (streq(key, "SYSCTL")) {
692 r = check_attr_format_and_warn(rules, key, attr);
693 if (r < 0)
694 return r;
695 if (op == OP_REMOVE)
696 return log_token_invalid_op(rules, key);
697 if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) {
698 log_token_warning(rules, "%s key takes '==', '!=', or '=' operator, assuming '='.", key);
699 op = OP_ASSIGN;
700 }
701
702 if (!is_match) {
703 check_value_format_and_warn(rules, key, value, false);
704 r = rule_line_add_token(rule_line, TK_A_SYSCTL, op, value, attr);
705 } else
706 r = rule_line_add_token(rule_line, TK_M_SYSCTL, op, value, attr);
707 } else if (streq(key, "KERNELS")) {
708 if (attr)
709 return log_token_invalid_attr(rules, key);
710 if (!is_match)
711 return log_token_invalid_op(rules, key);
712
713 r = rule_line_add_token(rule_line, TK_M_PARENTS_KERNEL, op, value, NULL);
714 } else if (streq(key, "SUBSYSTEMS")) {
715 if (attr)
716 return log_token_invalid_attr(rules, key);
717 if (!is_match)
718 return log_token_invalid_op(rules, key);
719
720 r = rule_line_add_token(rule_line, TK_M_PARENTS_SUBSYSTEM, op, value, NULL);
721 } else if (streq(key, "DRIVERS")) {
722 if (attr)
723 return log_token_invalid_attr(rules, key);
724 if (!is_match)
725 return log_token_invalid_op(rules, key);
726
727 r = rule_line_add_token(rule_line, TK_M_PARENTS_DRIVER, op, value, NULL);
728 } else if (streq(key, "ATTRS")) {
729 r = check_attr_format_and_warn(rules, key, attr);
730 if (r < 0)
731 return r;
732 if (!is_match)
733 return log_token_invalid_op(rules, key);
734
735 if (startswith(attr, "device/"))
736 log_token_warning(rules, "'device' link may not be available in future kernels.");
737 if (strstr(attr, "../"))
738 log_token_warning(rules, "Direct reference to parent sysfs directory, may break in future kernels.");
739
740 r = rule_line_add_token(rule_line, TK_M_PARENTS_ATTR, op, value, attr);
741 } else if (streq(key, "TAGS")) {
742 if (attr)
743 return log_token_invalid_attr(rules, key);
744 if (!is_match)
745 return log_token_invalid_op(rules, key);
746
747 r = rule_line_add_token(rule_line, TK_M_PARENTS_TAG, op, value, NULL);
748 } else if (streq(key, "TEST")) {
749 mode_t mode = MODE_INVALID;
750
751 if (!isempty(attr)) {
752 r = parse_mode(attr, &mode);
753 if (r < 0)
754 return log_token_error_errno(rules, r, "Failed to parse mode '%s': %m", attr);
755 }
756 check_value_format_and_warn(rules, key, value, true);
757 if (!is_match)
758 return log_token_invalid_op(rules, key);
759
760 r = rule_line_add_token(rule_line, TK_M_TEST, op, value, MODE_TO_PTR(mode));
761 } else if (streq(key, "PROGRAM")) {
762 if (attr)
763 return log_token_invalid_attr(rules, key);
764 check_value_format_and_warn(rules, key, value, true);
765 if (op == OP_REMOVE)
766 return log_token_invalid_op(rules, key);
767 if (!is_match)
768 op = OP_MATCH;
769
770 r = rule_line_add_token(rule_line, TK_M_PROGRAM, op, value, NULL);
771 } else if (streq(key, "IMPORT")) {
772 if (isempty(attr))
773 return log_token_invalid_attr(rules, key);
774 check_value_format_and_warn(rules, key, value, true);
775 if (op == OP_REMOVE)
776 return log_token_invalid_op(rules, key);
777 if (!is_match)
778 op = OP_MATCH;
779
780 if (streq(attr, "file"))
781 r = rule_line_add_token(rule_line, TK_M_IMPORT_FILE, op, value, NULL);
782 else if (streq(attr, "program")) {
783 UdevBuiltinCommand cmd;
784
785 cmd = udev_builtin_lookup(value);
786 if (cmd >= 0) {
787 log_token_debug(rules,"Found builtin command '%s' for %s, replacing attribute", value, key);
788 r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd));
789 } else
790 r = rule_line_add_token(rule_line, TK_M_IMPORT_PROGRAM, op, value, NULL);
791 } else if (streq(attr, "builtin")) {
792 UdevBuiltinCommand cmd;
793
794 cmd = udev_builtin_lookup(value);
795 if (cmd < 0)
796 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
797 "Unknown builtin command: %s", value);
798 r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd));
799 } else if (streq(attr, "db"))
800 r = rule_line_add_token(rule_line, TK_M_IMPORT_DB, op, value, NULL);
801 else if (streq(attr, "cmdline"))
802 r = rule_line_add_token(rule_line, TK_M_IMPORT_CMDLINE, op, value, NULL);
803 else if (streq(attr, "parent"))
804 r = rule_line_add_token(rule_line, TK_M_IMPORT_PARENT, op, value, NULL);
805 else
806 return log_token_invalid_attr(rules, key);
807 } else if (streq(key, "RESULT")) {
808 if (attr)
809 return log_token_invalid_attr(rules, key);
810 if (!is_match)
811 return log_token_invalid_op(rules, key);
812
813 r = rule_line_add_token(rule_line, TK_M_RESULT, op, value, NULL);
814 } else if (streq(key, "OPTIONS")) {
815 char *tmp;
816
817 if (attr)
818 return log_token_invalid_attr(rules, key);
819 if (is_match || op == OP_REMOVE)
820 return log_token_invalid_op(rules, key);
821 if (op == OP_ADD)
822 op = OP_ASSIGN;
823
824 if (streq(value, "string_escape=none"))
825 r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_NONE, op, NULL, NULL);
826 else if (streq(value, "string_escape=replace"))
827 r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_REPLACE, op, NULL, NULL);
828 else if (streq(value, "db_persist"))
829 r = rule_line_add_token(rule_line, TK_A_OPTIONS_DB_PERSIST, op, NULL, NULL);
830 else if (streq(value, "watch"))
831 r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(1));
832 else if (streq(value, "nowatch"))
833 r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(0));
834 else if ((tmp = startswith(value, "static_node=")))
835 r = rule_line_add_token(rule_line, TK_A_OPTIONS_STATIC_NODE, op, tmp, NULL);
836 else if ((tmp = startswith(value, "link_priority="))) {
837 int prio;
838
839 r = safe_atoi(tmp, &prio);
840 if (r < 0)
841 return log_token_error_errno(rules, r, "Failed to parse link priority '%s': %m", tmp);
842 r = rule_line_add_token(rule_line, TK_A_OPTIONS_DEVLINK_PRIORITY, op, NULL, INT_TO_PTR(prio));
843 } else if ((tmp = startswith(value, "log_level="))) {
844 int level;
845
846 if (streq(tmp, "reset"))
847 level = -1;
848 else {
849 level = log_level_from_string(tmp);
850 if (level < 0)
851 return log_token_error_errno(rules, level, "Failed to parse log level '%s': %m", tmp);
852 }
853 r = rule_line_add_token(rule_line, TK_A_OPTIONS_LOG_LEVEL, op, NULL, INT_TO_PTR(level));
854 } else {
855 log_token_warning(rules, "Invalid value for OPTIONS key, ignoring: '%s'", value);
856 return 0;
857 }
858 } else if (streq(key, "OWNER")) {
859 uid_t uid;
860
861 if (attr)
862 return log_token_invalid_attr(rules, key);
863 if (is_match || op == OP_REMOVE)
864 return log_token_invalid_op(rules, key);
865 if (op == OP_ADD) {
866 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
867 op = OP_ASSIGN;
868 }
869
870 if (parse_uid(value, &uid) >= 0)
871 r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid));
872 else if (rules->resolve_name_timing == RESOLVE_NAME_EARLY &&
873 rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) {
874 r = rule_resolve_user(rules, value, &uid);
875 if (r < 0)
876 return log_token_error_errno(rules, r, "Failed to resolve user name '%s': %m", value);
877
878 r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid));
879 } else if (rules->resolve_name_timing != RESOLVE_NAME_NEVER) {
880 check_value_format_and_warn(rules, key, value, true);
881 r = rule_line_add_token(rule_line, TK_A_OWNER, op, value, NULL);
882 } else {
883 log_token_debug(rules, "User name resolution is disabled, ignoring %s=%s", key, value);
884 return 0;
885 }
886 } else if (streq(key, "GROUP")) {
887 gid_t gid;
888
889 if (attr)
890 return log_token_invalid_attr(rules, key);
891 if (is_match || op == OP_REMOVE)
892 return log_token_invalid_op(rules, key);
893 if (op == OP_ADD) {
894 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
895 op = OP_ASSIGN;
896 }
897
898 if (parse_gid(value, &gid) >= 0)
899 r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid));
900 else if (rules->resolve_name_timing == RESOLVE_NAME_EARLY &&
901 rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) {
902 r = rule_resolve_group(rules, value, &gid);
903 if (r < 0)
904 return log_token_error_errno(rules, r, "Failed to resolve group name '%s': %m", value);
905
906 r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid));
907 } else if (rules->resolve_name_timing != RESOLVE_NAME_NEVER) {
908 check_value_format_and_warn(rules, key, value, true);
909 r = rule_line_add_token(rule_line, TK_A_GROUP, op, value, NULL);
910 } else {
911 log_token_debug(rules, "Resolving group name is disabled, ignoring GROUP=\"%s\"", value);
912 return 0;
913 }
914 } else if (streq(key, "MODE")) {
915 mode_t mode;
916
917 if (attr)
918 return log_token_invalid_attr(rules, key);
919 if (is_match || op == OP_REMOVE)
920 return log_token_invalid_op(rules, key);
921 if (op == OP_ADD) {
922 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
923 op = OP_ASSIGN;
924 }
925
926 if (parse_mode(value, &mode) >= 0)
927 r = rule_line_add_token(rule_line, TK_A_MODE_ID, op, NULL, MODE_TO_PTR(mode));
928 else {
929 check_value_format_and_warn(rules, key, value, true);
930 r = rule_line_add_token(rule_line, TK_A_MODE, op, value, NULL);
931 }
932 } else if (streq(key, "SECLABEL")) {
933 if (isempty(attr))
934 return log_token_invalid_attr(rules, key);
935 check_value_format_and_warn(rules, key, value, true);
936 if (is_match || op == OP_REMOVE)
937 return log_token_invalid_op(rules, key);
938 if (op == OP_ASSIGN_FINAL) {
939 log_token_warning(rules, "%s key takes '=' or '+=' operator, assuming '='.", key);
940 op = OP_ASSIGN;
941 }
942
943 r = rule_line_add_token(rule_line, TK_A_SECLABEL, op, value, attr);
944 } else if (streq(key, "RUN")) {
945 if (is_match || op == OP_REMOVE)
946 return log_token_invalid_op(rules, key);
947 check_value_format_and_warn(rules, key, value, true);
948 if (!attr || streq(attr, "program"))
949 r = rule_line_add_token(rule_line, TK_A_RUN_PROGRAM, op, value, NULL);
950 else if (streq(attr, "builtin")) {
951 UdevBuiltinCommand cmd;
952
953 cmd = udev_builtin_lookup(value);
954 if (cmd < 0)
955 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
956 "Unknown builtin command '%s', ignoring", value);
957 r = rule_line_add_token(rule_line, TK_A_RUN_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd));
958 } else
959 return log_token_invalid_attr(rules, key);
960 } else if (streq(key, "GOTO")) {
961 if (attr)
962 return log_token_invalid_attr(rules, key);
963 if (op != OP_ASSIGN)
964 return log_token_invalid_op(rules, key);
965 if (FLAGS_SET(rule_line->type, LINE_HAS_GOTO)) {
966 log_token_warning(rules, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value);
967 return 0;
968 }
969
970 rule_line->goto_label = value;
971 SET_FLAG(rule_line->type, LINE_HAS_GOTO, true);
972 return 1;
973 } else if (streq(key, "LABEL")) {
974 if (attr)
975 return log_token_invalid_attr(rules, key);
976 if (op != OP_ASSIGN)
977 return log_token_invalid_op(rules, key);
978
979 rule_line->label = value;
980 SET_FLAG(rule_line->type, LINE_HAS_LABEL, true);
981 return 1;
982 } else
983 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), "Invalid key '%s'", key);
984 if (r < 0)
985 return log_oom();
986
987 return 1;
988 }
989
990 static UdevRuleOperatorType parse_operator(const char *op) {
991 assert(op);
992
993 if (startswith(op, "=="))
994 return OP_MATCH;
995 if (startswith(op, "!="))
996 return OP_NOMATCH;
997 if (startswith(op, "+="))
998 return OP_ADD;
999 if (startswith(op, "-="))
1000 return OP_REMOVE;
1001 if (startswith(op, "="))
1002 return OP_ASSIGN;
1003 if (startswith(op, ":="))
1004 return OP_ASSIGN_FINAL;
1005
1006 return _OP_TYPE_INVALID;
1007 }
1008
1009 static int parse_line(char **line, char **ret_key, char **ret_attr, UdevRuleOperatorType *ret_op, char **ret_value) {
1010 char *key_begin, *key_end, *attr, *tmp;
1011 UdevRuleOperatorType op;
1012 int r;
1013
1014 assert(line);
1015 assert(*line);
1016 assert(ret_key);
1017 assert(ret_op);
1018 assert(ret_value);
1019
1020 key_begin = skip_leading_chars(*line, WHITESPACE ",");
1021
1022 if (isempty(key_begin))
1023 return 0;
1024
1025 for (key_end = key_begin; ; key_end++) {
1026 if (key_end[0] == '\0')
1027 return -EINVAL;
1028 if (strchr(WHITESPACE "={", key_end[0]))
1029 break;
1030 if (strchr("+-!:", key_end[0]) && key_end[1] == '=')
1031 break;
1032 }
1033 if (key_end[0] == '{') {
1034 attr = key_end + 1;
1035 tmp = strchr(attr, '}');
1036 if (!tmp)
1037 return -EINVAL;
1038 *tmp++ = '\0';
1039 } else {
1040 attr = NULL;
1041 tmp = key_end;
1042 }
1043
1044 tmp = skip_leading_chars(tmp, NULL);
1045 op = parse_operator(tmp);
1046 if (op < 0)
1047 return -EINVAL;
1048
1049 key_end[0] = '\0';
1050
1051 tmp += op == OP_ASSIGN ? 1 : 2;
1052 tmp = skip_leading_chars(tmp, NULL);
1053 r = udev_rule_parse_value(tmp, ret_value, line);
1054 if (r < 0)
1055 return r;
1056
1057 *ret_key = key_begin;
1058 *ret_attr = attr;
1059 *ret_op = op;
1060 return 1;
1061 }
1062
1063 static void sort_tokens(UdevRuleLine *rule_line) {
1064 UdevRuleToken *head_old;
1065
1066 assert(rule_line);
1067
1068 head_old = TAKE_PTR(rule_line->tokens);
1069 rule_line->current_token = NULL;
1070
1071 while (!LIST_IS_EMPTY(head_old)) {
1072 UdevRuleToken *t, *min_token = NULL;
1073
1074 LIST_FOREACH(tokens, t, head_old)
1075 if (!min_token || min_token->type > t->type)
1076 min_token = t;
1077
1078 LIST_REMOVE(tokens, head_old, min_token);
1079 rule_line_append_token(rule_line, min_token);
1080 }
1081 }
1082
1083 static int rule_add_line(UdevRules *rules, const char *line_str, unsigned line_nr) {
1084 _cleanup_(udev_rule_line_freep) UdevRuleLine *rule_line = NULL;
1085 _cleanup_free_ char *line = NULL;
1086 UdevRuleFile *rule_file;
1087 char *p;
1088 int r;
1089
1090 assert(rules);
1091 assert(rules->current_file);
1092 assert(line_str);
1093
1094 rule_file = rules->current_file;
1095
1096 if (isempty(line_str))
1097 return 0;
1098
1099 /* We use memdup_suffix0() here, since we want to add a second NUL byte to the end, since possibly
1100 * some parsers might turn this into a "nulstr", which requires an extra NUL at the end. */
1101 line = memdup_suffix0(line_str, strlen(line_str) + 1);
1102 if (!line)
1103 return log_oom();
1104
1105 rule_line = new(UdevRuleLine, 1);
1106 if (!rule_line)
1107 return log_oom();
1108
1109 *rule_line = (UdevRuleLine) {
1110 .line = TAKE_PTR(line),
1111 .line_number = line_nr,
1112 .rule_file = rule_file,
1113 };
1114
1115 if (rule_file->current_line)
1116 LIST_APPEND(rule_lines, rule_file->current_line, rule_line);
1117 else
1118 LIST_APPEND(rule_lines, rule_file->rule_lines, rule_line);
1119
1120 rule_file->current_line = rule_line;
1121
1122 for (p = rule_line->line; !isempty(p); ) {
1123 char *key, *attr, *value;
1124 UdevRuleOperatorType op;
1125
1126 r = parse_line(&p, &key, &attr, &op, &value);
1127 if (r < 0)
1128 return log_token_error_errno(rules, r, "Invalid key/value pair, ignoring.");
1129 if (r == 0)
1130 break;
1131
1132 r = parse_token(rules, key, attr, op, value);
1133 if (r < 0)
1134 return r;
1135 }
1136
1137 if (rule_line->type == 0) {
1138 log_token_warning(rules, "The line takes no effect, ignoring.");
1139 return 0;
1140 }
1141
1142 sort_tokens(rule_line);
1143 TAKE_PTR(rule_line);
1144 return 0;
1145 }
1146
1147 static void rule_resolve_goto(UdevRuleFile *rule_file) {
1148 UdevRuleLine *line, *line_next, *i;
1149
1150 assert(rule_file);
1151
1152 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1153 LIST_FOREACH_SAFE(rule_lines, line, line_next, rule_file->rule_lines) {
1154 if (!FLAGS_SET(line->type, LINE_HAS_GOTO))
1155 continue;
1156
1157 LIST_FOREACH_AFTER(rule_lines, i, line)
1158 if (streq_ptr(i->label, line->goto_label)) {
1159 line->goto_line = i;
1160 break;
1161 }
1162
1163 if (!line->goto_line) {
1164 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
1165 rule_file->filename, line->line_number, line->goto_label);
1166
1167 SET_FLAG(line->type, LINE_HAS_GOTO, false);
1168 line->goto_label = NULL;
1169
1170 if ((line->type & ~LINE_HAS_LABEL) == 0) {
1171 log_notice("%s:%u: The line takes no effect any more, dropping",
1172 rule_file->filename, line->line_number);
1173 if (line->type == LINE_HAS_LABEL)
1174 udev_rule_line_clear_tokens(line);
1175 else
1176 udev_rule_line_free(line);
1177 }
1178 }
1179 }
1180 }
1181
1182 int udev_rules_parse_file(UdevRules *rules, const char *filename) {
1183 _cleanup_free_ char *continuation = NULL, *name = NULL;
1184 _cleanup_fclose_ FILE *f = NULL;
1185 UdevRuleFile *rule_file;
1186 bool ignore_line = false;
1187 unsigned line_nr = 0;
1188 int r;
1189
1190 f = fopen(filename, "re");
1191 if (!f) {
1192 if (errno == ENOENT)
1193 return 0;
1194
1195 return -errno;
1196 }
1197
1198 (void) fd_warn_permissions(filename, fileno(f));
1199
1200 if (null_or_empty_fd(fileno(f))) {
1201 log_debug("Skipping empty file: %s", filename);
1202 return 0;
1203 }
1204
1205 log_debug("Reading rules file: %s", filename);
1206
1207 name = strdup(filename);
1208 if (!name)
1209 return log_oom();
1210
1211 rule_file = new(UdevRuleFile, 1);
1212 if (!rule_file)
1213 return log_oom();
1214
1215 *rule_file = (UdevRuleFile) {
1216 .filename = TAKE_PTR(name),
1217 };
1218
1219 if (rules->current_file)
1220 LIST_APPEND(rule_files, rules->current_file, rule_file);
1221 else
1222 LIST_APPEND(rule_files, rules->rule_files, rule_file);
1223
1224 rules->current_file = rule_file;
1225
1226 for (;;) {
1227 _cleanup_free_ char *buf = NULL;
1228 size_t len;
1229 char *line;
1230
1231 r = read_line(f, UDEV_LINE_SIZE, &buf);
1232 if (r < 0)
1233 return r;
1234 if (r == 0)
1235 break;
1236
1237 line_nr++;
1238 line = skip_leading_chars(buf, NULL);
1239
1240 if (line[0] == '#')
1241 continue;
1242
1243 len = strlen(line);
1244
1245 if (continuation && !ignore_line) {
1246 if (strlen(continuation) + len >= UDEV_LINE_SIZE)
1247 ignore_line = true;
1248
1249 if (!strextend(&continuation, line))
1250 return log_oom();
1251
1252 if (!ignore_line) {
1253 line = continuation;
1254 len = strlen(line);
1255 }
1256 }
1257
1258 if (len > 0 && line[len - 1] == '\\') {
1259 if (ignore_line)
1260 continue;
1261
1262 line[len - 1] = '\0';
1263 if (!continuation) {
1264 continuation = strdup(line);
1265 if (!continuation)
1266 return log_oom();
1267 }
1268
1269 continue;
1270 }
1271
1272 if (ignore_line)
1273 log_error("%s:%u: Line is too long, ignored", filename, line_nr);
1274 else if (len > 0)
1275 (void) rule_add_line(rules, line, line_nr);
1276
1277 continuation = mfree(continuation);
1278 ignore_line = false;
1279 }
1280
1281 rule_resolve_goto(rule_file);
1282 return 0;
1283 }
1284
1285 UdevRules* udev_rules_new(ResolveNameTiming resolve_name_timing) {
1286 assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX);
1287
1288 UdevRules *rules = new(UdevRules, 1);
1289 if (!rules)
1290 return NULL;
1291
1292 *rules = (UdevRules) {
1293 .resolve_name_timing = resolve_name_timing,
1294 };
1295
1296 return rules;
1297 }
1298
1299 int udev_rules_load(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing) {
1300 _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
1301 _cleanup_strv_free_ char **files = NULL;
1302 char **f;
1303 int r;
1304
1305 rules = udev_rules_new(resolve_name_timing);
1306 if (!rules)
1307 return -ENOMEM;
1308
1309 (void) udev_rules_check_timestamp(rules);
1310
1311 r = conf_files_list_strv(&files, ".rules", NULL, 0, RULES_DIRS);
1312 if (r < 0)
1313 return log_debug_errno(r, "Failed to enumerate rules files: %m");
1314
1315 STRV_FOREACH(f, files) {
1316 r = udev_rules_parse_file(rules, *f);
1317 if (r < 0)
1318 log_debug_errno(r, "Failed to read rules file %s, ignoring: %m", *f);
1319 }
1320
1321 *ret_rules = TAKE_PTR(rules);
1322 return 0;
1323 }
1324
1325 bool udev_rules_check_timestamp(UdevRules *rules) {
1326 if (!rules)
1327 return false;
1328
1329 return paths_check_timestamp(RULES_DIRS, &rules->dirs_ts_usec, true);
1330 }
1331
1332 static bool token_match_string(UdevRuleToken *token, const char *str) {
1333 const char *i, *value;
1334 bool match = false;
1335
1336 assert(token);
1337 assert(token->value);
1338 assert(token->type < _TK_M_MAX);
1339
1340 str = strempty(str);
1341 value = token->value;
1342
1343 switch (token->match_type) {
1344 case MATCH_TYPE_EMPTY:
1345 match = isempty(str);
1346 break;
1347 case MATCH_TYPE_SUBSYSTEM:
1348 match = STR_IN_SET(str, "subsystem", "class", "bus");
1349 break;
1350 case MATCH_TYPE_PLAIN_WITH_EMPTY:
1351 if (isempty(str)) {
1352 match = true;
1353 break;
1354 }
1355 _fallthrough_;
1356 case MATCH_TYPE_PLAIN:
1357 NULSTR_FOREACH(i, value)
1358 if (streq(i, str)) {
1359 match = true;
1360 break;
1361 }
1362 break;
1363 case MATCH_TYPE_GLOB_WITH_EMPTY:
1364 if (isempty(str)) {
1365 match = true;
1366 break;
1367 }
1368 _fallthrough_;
1369 case MATCH_TYPE_GLOB:
1370 NULSTR_FOREACH(i, value)
1371 if ((fnmatch(i, str, 0) == 0)) {
1372 match = true;
1373 break;
1374 }
1375 break;
1376 default:
1377 assert_not_reached("Invalid match type");
1378 }
1379
1380 return token->op == (match ? OP_MATCH : OP_NOMATCH);
1381 }
1382
1383 static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *event) {
1384 char nbuf[UDEV_NAME_SIZE], vbuf[UDEV_NAME_SIZE];
1385 const char *name, *value;
1386
1387 assert(token);
1388 assert(dev);
1389 assert(event);
1390
1391 name = token->data;
1392
1393 switch (token->attr_subst_type) {
1394 case SUBST_TYPE_FORMAT:
1395 (void) udev_event_apply_format(event, name, nbuf, sizeof(nbuf), false);
1396 name = nbuf;
1397 _fallthrough_;
1398 case SUBST_TYPE_PLAIN:
1399 if (sd_device_get_sysattr_value(dev, name, &value) < 0)
1400 return false;
1401 break;
1402 case SUBST_TYPE_SUBSYS:
1403 if (udev_resolve_subsys_kernel(name, vbuf, sizeof(vbuf), true) < 0)
1404 return false;
1405 value = vbuf;
1406 break;
1407 default:
1408 assert_not_reached("Invalid attribute substitution type");
1409 }
1410
1411 /* remove trailing whitespace, if not asked to match for it */
1412 if (token->attr_match_remove_trailing_whitespace) {
1413 if (value != vbuf) {
1414 strscpy(vbuf, sizeof(vbuf), value);
1415 value = vbuf;
1416 }
1417
1418 delete_trailing_chars(vbuf, NULL);
1419 }
1420
1421 return token_match_string(token, value);
1422 }
1423
1424 static int get_property_from_string(char *line, char **ret_key, char **ret_value) {
1425 char *key, *val;
1426 size_t len;
1427
1428 assert(line);
1429 assert(ret_key);
1430 assert(ret_value);
1431
1432 /* find key */
1433 key = skip_leading_chars(line, NULL);
1434
1435 /* comment or empty line */
1436 if (IN_SET(key[0], '#', '\0')) {
1437 *ret_key = *ret_value = NULL;
1438 return 0;
1439 }
1440
1441 /* split key/value */
1442 val = strchr(key, '=');
1443 if (!val)
1444 return -EINVAL;
1445 *val++ = '\0';
1446
1447 key = strstrip(key);
1448 if (isempty(key))
1449 return -EINVAL;
1450
1451 val = strstrip(val);
1452 if (isempty(val))
1453 return -EINVAL;
1454
1455 /* unquote */
1456 if (IN_SET(val[0], '"', '\'')) {
1457 len = strlen(val);
1458 if (len == 1 || val[len-1] != val[0])
1459 return -EINVAL;
1460 val[len-1] = '\0';
1461 val++;
1462 }
1463
1464 *ret_key = key;
1465 *ret_value = val;
1466 return 1;
1467 }
1468
1469 static int import_parent_into_properties(sd_device *dev, const char *filter) {
1470 const char *key, *val;
1471 sd_device *parent;
1472 int r;
1473
1474 assert(dev);
1475 assert(filter);
1476
1477 r = sd_device_get_parent(dev, &parent);
1478 if (r == -ENOENT)
1479 return 0;
1480 if (r < 0)
1481 return r;
1482
1483 FOREACH_DEVICE_PROPERTY(parent, key, val) {
1484 if (fnmatch(filter, key, 0) != 0)
1485 continue;
1486 r = device_add_property(dev, key, val);
1487 if (r < 0)
1488 return r;
1489 }
1490
1491 return 1;
1492 }
1493
1494 static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) {
1495 _cleanup_closedir_ DIR *dir = NULL;
1496 struct dirent *dent;
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(dent, dir, break) {
1519 if (dent->d_name[0] == '.')
1520 continue;
1521
1522 strscpyl(p, size, dent->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("Invalid CONST key");
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, 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("Invalid token type");
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 }