]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-rules.c
Merge pull request #23855 from keszybz/drop-list-is-empty
[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 "fd-util.h"
13 #include "fileio.h"
14 #include "format-util.h"
15 #include "fs-util.h"
16 #include "glob-util.h"
17 #include "list.h"
18 #include "mkdir.h"
19 #include "netif-naming-scheme.h"
20 #include "nulstr-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "proc-cmdline.h"
24 #include "stat-util.h"
25 #include "strv.h"
26 #include "strxcpyx.h"
27 #include "sysctl-util.h"
28 #include "syslog-util.h"
29 #include "udev-builtin.h"
30 #include "udev-event.h"
31 #include "udev-netlink.h"
32 #include "udev-node.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 assert(rule_line);
272
273 LIST_FOREACH(tokens, i, rule_line->tokens)
274 udev_rule_token_free(i);
275
276 rule_line->tokens = NULL;
277 }
278
279 static UdevRuleLine* udev_rule_line_free(UdevRuleLine *rule_line) {
280 if (!rule_line)
281 return NULL;
282
283 udev_rule_line_clear_tokens(rule_line);
284
285 if (rule_line->rule_file) {
286 if (rule_line->rule_file->current_line == rule_line)
287 rule_line->rule_file->current_line = rule_line->rule_lines_prev;
288
289 LIST_REMOVE(rule_lines, rule_line->rule_file->rule_lines, rule_line);
290 }
291
292 free(rule_line->line);
293 return mfree(rule_line);
294 }
295
296 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine*, udev_rule_line_free);
297
298 static void udev_rule_file_free(UdevRuleFile *rule_file) {
299 if (!rule_file)
300 return;
301
302 LIST_FOREACH(rule_lines, i, rule_file->rule_lines)
303 udev_rule_line_free(i);
304
305 free(rule_file->filename);
306 free(rule_file);
307 }
308
309 UdevRules *udev_rules_free(UdevRules *rules) {
310 if (!rules)
311 return NULL;
312
313 LIST_FOREACH(rule_files, i, rules->rule_files)
314 udev_rule_file_free(i);
315
316 hashmap_free_free_key(rules->known_users);
317 hashmap_free_free_key(rules->known_groups);
318 return mfree(rules);
319 }
320
321 static int rule_resolve_user(UdevRules *rules, const char *name, uid_t *ret) {
322 _cleanup_free_ char *n = NULL;
323 uid_t uid;
324 void *val;
325 int r;
326
327 assert(rules);
328 assert(name);
329
330 val = hashmap_get(rules->known_users, name);
331 if (val) {
332 *ret = PTR_TO_UID(val);
333 return 0;
334 }
335
336 r = get_user_creds(&name, &uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
337 if (r < 0) {
338 log_unknown_owner(NULL, rules, r, "user", name);
339 *ret = UID_INVALID;
340 return 0;
341 }
342
343 n = strdup(name);
344 if (!n)
345 return -ENOMEM;
346
347 r = hashmap_ensure_put(&rules->known_users, &string_hash_ops, n, UID_TO_PTR(uid));
348 if (r < 0)
349 return r;
350
351 TAKE_PTR(n);
352 *ret = uid;
353 return 0;
354 }
355
356 static int rule_resolve_group(UdevRules *rules, const char *name, gid_t *ret) {
357 _cleanup_free_ char *n = NULL;
358 gid_t gid;
359 void *val;
360 int r;
361
362 assert(rules);
363 assert(name);
364
365 val = hashmap_get(rules->known_groups, name);
366 if (val) {
367 *ret = PTR_TO_GID(val);
368 return 0;
369 }
370
371 r = get_group_creds(&name, &gid, USER_CREDS_ALLOW_MISSING);
372 if (r < 0) {
373 log_unknown_owner(NULL, rules, r, "group", name);
374 *ret = GID_INVALID;
375 return 0;
376 }
377
378 n = strdup(name);
379 if (!n)
380 return -ENOMEM;
381
382 r = hashmap_ensure_put(&rules->known_groups, &string_hash_ops, n, GID_TO_PTR(gid));
383 if (r < 0)
384 return r;
385
386 TAKE_PTR(n);
387 *ret = gid;
388 return 0;
389 }
390
391 static UdevRuleSubstituteType rule_get_substitution_type(const char *str) {
392 assert(str);
393
394 if (str[0] == '[')
395 return SUBST_TYPE_SUBSYS;
396 if (strchr(str, '%') || strchr(str, '$'))
397 return SUBST_TYPE_FORMAT;
398 return SUBST_TYPE_PLAIN;
399 }
400
401 static void rule_line_append_token(UdevRuleLine *rule_line, UdevRuleToken *token) {
402 assert(rule_line);
403 assert(token);
404
405 if (rule_line->current_token)
406 LIST_APPEND(tokens, rule_line->current_token, token);
407 else
408 LIST_APPEND(tokens, rule_line->tokens, token);
409
410 rule_line->current_token = token;
411 }
412
413 static int rule_line_add_token(UdevRuleLine *rule_line, UdevRuleTokenType type, UdevRuleOperatorType op, char *value, void *data) {
414 UdevRuleToken *token;
415 UdevRuleMatchType match_type = _MATCH_TYPE_INVALID;
416 UdevRuleSubstituteType subst_type = _SUBST_TYPE_INVALID;
417 bool remove_trailing_whitespace = false;
418 size_t len;
419
420 assert(rule_line);
421 assert(type >= 0 && type < _TK_TYPE_MAX);
422 assert(op >= 0 && op < _OP_TYPE_MAX);
423
424 if (type < _TK_M_MAX) {
425 assert(value);
426 assert(IN_SET(op, OP_MATCH, OP_NOMATCH));
427
428 if (type == TK_M_SUBSYSTEM && STR_IN_SET(value, "subsystem", "bus", "class"))
429 match_type = MATCH_TYPE_SUBSYSTEM;
430 else if (isempty(value))
431 match_type = MATCH_TYPE_EMPTY;
432 else if (streq(value, "?*")) {
433 /* Convert KEY=="?*" -> KEY!="" */
434 match_type = MATCH_TYPE_EMPTY;
435 op = op == OP_MATCH ? OP_NOMATCH : OP_MATCH;
436 } else if (string_is_glob(value))
437 match_type = MATCH_TYPE_GLOB;
438 else
439 match_type = MATCH_TYPE_PLAIN;
440
441 if (type < TK_M_TEST || type == TK_M_RESULT) {
442 /* Convert value string to nulstr. */
443 bool bar = true, empty = false;
444 char *a, *b;
445
446 for (a = b = value; *a != '\0'; a++) {
447 if (*a != '|') {
448 *b++ = *a;
449 bar = false;
450 } else {
451 if (bar)
452 empty = true;
453 else
454 *b++ = '\0';
455 bar = true;
456 }
457 }
458 *b = '\0';
459
460 /* Make sure the value is end, so NULSTR_FOREACH can read correct match */
461 if (b < a)
462 b[1] = '\0';
463
464 if (bar)
465 empty = true;
466
467 if (empty) {
468 if (match_type == MATCH_TYPE_GLOB)
469 match_type = MATCH_TYPE_GLOB_WITH_EMPTY;
470 if (match_type == MATCH_TYPE_PLAIN)
471 match_type = MATCH_TYPE_PLAIN_WITH_EMPTY;
472 }
473 }
474 }
475
476 if (IN_SET(type, TK_M_ATTR, TK_M_PARENTS_ATTR)) {
477 assert(value);
478 assert(data);
479
480 len = strlen(value);
481 if (len > 0 && !isspace(value[len - 1]))
482 remove_trailing_whitespace = true;
483
484 subst_type = rule_get_substitution_type(data);
485 }
486
487 token = new(UdevRuleToken, 1);
488 if (!token)
489 return -ENOMEM;
490
491 *token = (UdevRuleToken) {
492 .type = type,
493 .op = op,
494 .value = value,
495 .data = data,
496 .match_type = match_type,
497 .attr_subst_type = subst_type,
498 .attr_match_remove_trailing_whitespace = remove_trailing_whitespace,
499 };
500
501 rule_line_append_token(rule_line, token);
502
503 if (token->type == TK_A_NAME)
504 SET_FLAG(rule_line->type, LINE_HAS_NAME, true);
505
506 else if (IN_SET(token->type, TK_A_DEVLINK,
507 TK_A_OWNER, TK_A_GROUP, TK_A_MODE,
508 TK_A_OWNER_ID, TK_A_GROUP_ID, TK_A_MODE_ID))
509 SET_FLAG(rule_line->type, LINE_HAS_DEVLINK, true);
510
511 else if (token->type == TK_A_OPTIONS_STATIC_NODE)
512 SET_FLAG(rule_line->type, LINE_HAS_STATIC_NODE, true);
513
514 else if (token->type >= _TK_A_MIN ||
515 IN_SET(token->type, TK_M_PROGRAM,
516 TK_M_IMPORT_FILE, TK_M_IMPORT_PROGRAM, TK_M_IMPORT_BUILTIN,
517 TK_M_IMPORT_DB, TK_M_IMPORT_CMDLINE, TK_M_IMPORT_PARENT))
518 SET_FLAG(rule_line->type, LINE_UPDATE_SOMETHING, true);
519
520 return 0;
521 }
522
523 static void check_value_format_and_warn(UdevRules *rules, const char *key, const char *value, bool nonempty) {
524 size_t offset;
525 const char *hint;
526
527 if (nonempty && isempty(value))
528 log_token_invalid_value(rules, key, value, (size_t) 0, "empty value");
529 else if (udev_check_format(value, &offset, &hint) < 0)
530 log_token_invalid_value(rules, key, value, offset + 1, hint);
531 }
532
533 static int check_attr_format_and_warn(UdevRules *rules, const char *key, const char *value) {
534 size_t offset;
535 const char *hint;
536
537 if (isempty(value))
538 return log_token_invalid_attr(rules, key);
539 if (udev_check_format(value, &offset, &hint) < 0)
540 log_token_invalid_attr_format(rules, key, value, offset + 1, hint);
541 return 0;
542 }
543
544 static int parse_token(UdevRules *rules, const char *key, char *attr, UdevRuleOperatorType op, char *value) {
545 bool is_match = IN_SET(op, OP_MATCH, OP_NOMATCH);
546 UdevRuleLine *rule_line;
547 int r;
548
549 assert(rules);
550 assert(rules->current_file);
551 assert(rules->current_file->current_line);
552 assert(key);
553 assert(value);
554
555 rule_line = rules->current_file->current_line;
556
557 if (streq(key, "ACTION")) {
558 if (attr)
559 return log_token_invalid_attr(rules, key);
560 if (!is_match)
561 return log_token_invalid_op(rules, key);
562
563 r = rule_line_add_token(rule_line, TK_M_ACTION, op, value, NULL);
564 } else if (streq(key, "DEVPATH")) {
565 if (attr)
566 return log_token_invalid_attr(rules, key);
567 if (!is_match)
568 return log_token_invalid_op(rules, key);
569
570 r = rule_line_add_token(rule_line, TK_M_DEVPATH, op, value, NULL);
571 } else if (streq(key, "KERNEL")) {
572 if (attr)
573 return log_token_invalid_attr(rules, key);
574 if (!is_match)
575 return log_token_invalid_op(rules, key);
576
577 r = rule_line_add_token(rule_line, TK_M_KERNEL, op, value, NULL);
578 } else if (streq(key, "SYMLINK")) {
579 if (attr)
580 return log_token_invalid_attr(rules, key);
581 if (op == OP_REMOVE)
582 return log_token_invalid_op(rules, key);
583
584 if (!is_match) {
585 check_value_format_and_warn(rules, key, value, false);
586 r = rule_line_add_token(rule_line, TK_A_DEVLINK, op, value, NULL);
587 } else
588 r = rule_line_add_token(rule_line, TK_M_DEVLINK, op, value, NULL);
589 } else if (streq(key, "NAME")) {
590 if (attr)
591 return log_token_invalid_attr(rules, key);
592 if (op == OP_REMOVE)
593 return log_token_invalid_op(rules, key);
594 if (op == OP_ADD) {
595 log_token_warning(rules, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key);
596 op = OP_ASSIGN;
597 }
598
599 if (!is_match) {
600 if (streq(value, "%k"))
601 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
602 "Ignoring NAME=\"%%k\", as it will take no effect.");
603 if (isempty(value))
604 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
605 "Ignoring NAME=\"\", as udev will not delete any network interfaces.");
606 check_value_format_and_warn(rules, key, value, false);
607
608 r = rule_line_add_token(rule_line, TK_A_NAME, op, value, NULL);
609 } else
610 r = rule_line_add_token(rule_line, TK_M_NAME, op, value, NULL);
611 } else if (streq(key, "ENV")) {
612 if (isempty(attr))
613 return log_token_invalid_attr(rules, key);
614 if (op == OP_REMOVE)
615 return log_token_invalid_op(rules, key);
616 if (op == OP_ASSIGN_FINAL) {
617 log_token_warning(rules, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key);
618 op = OP_ASSIGN;
619 }
620
621 if (!is_match) {
622 if (STR_IN_SET(attr,
623 "ACTION", "DEVLINKS", "DEVNAME", "DEVPATH", "DEVTYPE", "DRIVER",
624 "IFINDEX", "MAJOR", "MINOR", "SEQNUM", "SUBSYSTEM", "TAGS"))
625 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
626 "Invalid ENV attribute. '%s' cannot be set.", attr);
627
628 check_value_format_and_warn(rules, key, value, false);
629
630 r = rule_line_add_token(rule_line, TK_A_ENV, op, value, attr);
631 } else
632 r = rule_line_add_token(rule_line, TK_M_ENV, op, value, attr);
633 } else if (streq(key, "CONST")) {
634 if (isempty(attr) || !STR_IN_SET(attr, "arch", "virt"))
635 return log_token_invalid_attr(rules, key);
636 if (!is_match)
637 return log_token_invalid_op(rules, key);
638 r = rule_line_add_token(rule_line, TK_M_CONST, op, value, attr);
639 } else if (streq(key, "TAG")) {
640 if (attr)
641 return log_token_invalid_attr(rules, key);
642 if (op == OP_ASSIGN_FINAL) {
643 log_token_warning(rules, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key);
644 op = OP_ASSIGN;
645 }
646
647 if (!is_match) {
648 check_value_format_and_warn(rules, key, value, true);
649
650 r = rule_line_add_token(rule_line, TK_A_TAG, op, value, NULL);
651 } else
652 r = rule_line_add_token(rule_line, TK_M_TAG, op, value, NULL);
653 } else if (streq(key, "SUBSYSTEM")) {
654 if (attr)
655 return log_token_invalid_attr(rules, key);
656 if (!is_match)
657 return log_token_invalid_op(rules, key);
658
659 if (STR_IN_SET(value, "bus", "class"))
660 log_token_warning(rules, "\"%s\" must be specified as \"subsystem\".", value);
661
662 r = rule_line_add_token(rule_line, TK_M_SUBSYSTEM, op, value, NULL);
663 } else if (streq(key, "DRIVER")) {
664 if (attr)
665 return log_token_invalid_attr(rules, key);
666 if (!is_match)
667 return log_token_invalid_op(rules, key);
668
669 r = rule_line_add_token(rule_line, TK_M_DRIVER, op, value, NULL);
670 } else if (streq(key, "ATTR")) {
671 r = check_attr_format_and_warn(rules, key, attr);
672 if (r < 0)
673 return r;
674 if (op == OP_REMOVE)
675 return log_token_invalid_op(rules, key);
676 if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) {
677 log_token_warning(rules, "%s key takes '==', '!=', or '=' operator, assuming '='.", key);
678 op = OP_ASSIGN;
679 }
680
681 if (!is_match) {
682 check_value_format_and_warn(rules, key, value, false);
683 r = rule_line_add_token(rule_line, TK_A_ATTR, op, value, attr);
684 } else
685 r = rule_line_add_token(rule_line, TK_M_ATTR, op, value, attr);
686 } else if (streq(key, "SYSCTL")) {
687 r = check_attr_format_and_warn(rules, key, attr);
688 if (r < 0)
689 return r;
690 if (op == OP_REMOVE)
691 return log_token_invalid_op(rules, key);
692 if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) {
693 log_token_warning(rules, "%s key takes '==', '!=', or '=' operator, assuming '='.", key);
694 op = OP_ASSIGN;
695 }
696
697 if (!is_match) {
698 check_value_format_and_warn(rules, key, value, false);
699 r = rule_line_add_token(rule_line, TK_A_SYSCTL, op, value, attr);
700 } else
701 r = rule_line_add_token(rule_line, TK_M_SYSCTL, op, value, attr);
702 } else if (streq(key, "KERNELS")) {
703 if (attr)
704 return log_token_invalid_attr(rules, key);
705 if (!is_match)
706 return log_token_invalid_op(rules, key);
707
708 r = rule_line_add_token(rule_line, TK_M_PARENTS_KERNEL, op, value, NULL);
709 } else if (streq(key, "SUBSYSTEMS")) {
710 if (attr)
711 return log_token_invalid_attr(rules, key);
712 if (!is_match)
713 return log_token_invalid_op(rules, key);
714
715 r = rule_line_add_token(rule_line, TK_M_PARENTS_SUBSYSTEM, op, value, NULL);
716 } else if (streq(key, "DRIVERS")) {
717 if (attr)
718 return log_token_invalid_attr(rules, key);
719 if (!is_match)
720 return log_token_invalid_op(rules, key);
721
722 r = rule_line_add_token(rule_line, TK_M_PARENTS_DRIVER, op, value, NULL);
723 } else if (streq(key, "ATTRS")) {
724 r = check_attr_format_and_warn(rules, key, attr);
725 if (r < 0)
726 return r;
727 if (!is_match)
728 return log_token_invalid_op(rules, key);
729
730 if (startswith(attr, "device/"))
731 log_token_warning(rules, "'device' link may not be available in future kernels.");
732 if (strstr(attr, "../"))
733 log_token_warning(rules, "Direct reference to parent sysfs directory, may break in future kernels.");
734
735 r = rule_line_add_token(rule_line, TK_M_PARENTS_ATTR, op, value, attr);
736 } else if (streq(key, "TAGS")) {
737 if (attr)
738 return log_token_invalid_attr(rules, key);
739 if (!is_match)
740 return log_token_invalid_op(rules, key);
741
742 r = rule_line_add_token(rule_line, TK_M_PARENTS_TAG, op, value, NULL);
743 } else if (streq(key, "TEST")) {
744 mode_t mode = MODE_INVALID;
745
746 if (!isempty(attr)) {
747 r = parse_mode(attr, &mode);
748 if (r < 0)
749 return log_token_error_errno(rules, r, "Failed to parse mode '%s': %m", attr);
750 }
751 check_value_format_and_warn(rules, key, value, true);
752 if (!is_match)
753 return log_token_invalid_op(rules, key);
754
755 r = rule_line_add_token(rule_line, TK_M_TEST, op, value, MODE_TO_PTR(mode));
756 } else if (streq(key, "PROGRAM")) {
757 if (attr)
758 return log_token_invalid_attr(rules, key);
759 check_value_format_and_warn(rules, key, value, true);
760 if (op == OP_REMOVE)
761 return log_token_invalid_op(rules, key);
762 if (!is_match)
763 op = OP_MATCH;
764
765 r = rule_line_add_token(rule_line, TK_M_PROGRAM, op, value, NULL);
766 } else if (streq(key, "IMPORT")) {
767 if (isempty(attr))
768 return log_token_invalid_attr(rules, key);
769 check_value_format_and_warn(rules, key, value, true);
770 if (op == OP_REMOVE)
771 return log_token_invalid_op(rules, key);
772 if (!is_match)
773 op = OP_MATCH;
774
775 if (streq(attr, "file"))
776 r = rule_line_add_token(rule_line, TK_M_IMPORT_FILE, op, value, NULL);
777 else if (streq(attr, "program")) {
778 UdevBuiltinCommand cmd;
779
780 cmd = udev_builtin_lookup(value);
781 if (cmd >= 0) {
782 log_token_debug(rules,"Found builtin command '%s' for %s, replacing attribute", value, key);
783 r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd));
784 } else
785 r = rule_line_add_token(rule_line, TK_M_IMPORT_PROGRAM, op, value, NULL);
786 } else if (streq(attr, "builtin")) {
787 UdevBuiltinCommand cmd;
788
789 cmd = udev_builtin_lookup(value);
790 if (cmd < 0)
791 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
792 "Unknown builtin command: %s", value);
793 r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd));
794 } else if (streq(attr, "db"))
795 r = rule_line_add_token(rule_line, TK_M_IMPORT_DB, op, value, NULL);
796 else if (streq(attr, "cmdline"))
797 r = rule_line_add_token(rule_line, TK_M_IMPORT_CMDLINE, op, value, NULL);
798 else if (streq(attr, "parent"))
799 r = rule_line_add_token(rule_line, TK_M_IMPORT_PARENT, op, value, NULL);
800 else
801 return log_token_invalid_attr(rules, key);
802 } else if (streq(key, "RESULT")) {
803 if (attr)
804 return log_token_invalid_attr(rules, key);
805 if (!is_match)
806 return log_token_invalid_op(rules, key);
807
808 r = rule_line_add_token(rule_line, TK_M_RESULT, op, value, NULL);
809 } else if (streq(key, "OPTIONS")) {
810 char *tmp;
811
812 if (attr)
813 return log_token_invalid_attr(rules, key);
814 if (is_match || op == OP_REMOVE)
815 return log_token_invalid_op(rules, key);
816 if (op == OP_ADD)
817 op = OP_ASSIGN;
818
819 if (streq(value, "string_escape=none"))
820 r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_NONE, op, NULL, NULL);
821 else if (streq(value, "string_escape=replace"))
822 r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_REPLACE, op, NULL, NULL);
823 else if (streq(value, "db_persist"))
824 r = rule_line_add_token(rule_line, TK_A_OPTIONS_DB_PERSIST, op, NULL, NULL);
825 else if (streq(value, "watch"))
826 r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(1));
827 else if (streq(value, "nowatch"))
828 r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(0));
829 else if ((tmp = startswith(value, "static_node=")))
830 r = rule_line_add_token(rule_line, TK_A_OPTIONS_STATIC_NODE, op, tmp, NULL);
831 else if ((tmp = startswith(value, "link_priority="))) {
832 int prio;
833
834 r = safe_atoi(tmp, &prio);
835 if (r < 0)
836 return log_token_error_errno(rules, r, "Failed to parse link priority '%s': %m", tmp);
837 r = rule_line_add_token(rule_line, TK_A_OPTIONS_DEVLINK_PRIORITY, op, NULL, INT_TO_PTR(prio));
838 } else if ((tmp = startswith(value, "log_level="))) {
839 int level;
840
841 if (streq(tmp, "reset"))
842 level = -1;
843 else {
844 level = log_level_from_string(tmp);
845 if (level < 0)
846 return log_token_error_errno(rules, level, "Failed to parse log level '%s': %m", tmp);
847 }
848 r = rule_line_add_token(rule_line, TK_A_OPTIONS_LOG_LEVEL, op, NULL, INT_TO_PTR(level));
849 } else {
850 log_token_warning(rules, "Invalid value for OPTIONS key, ignoring: '%s'", value);
851 return 0;
852 }
853 } else if (streq(key, "OWNER")) {
854 uid_t uid;
855
856 if (attr)
857 return log_token_invalid_attr(rules, key);
858 if (is_match || op == OP_REMOVE)
859 return log_token_invalid_op(rules, key);
860 if (op == OP_ADD) {
861 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
862 op = OP_ASSIGN;
863 }
864
865 if (parse_uid(value, &uid) >= 0)
866 r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid));
867 else if (rules->resolve_name_timing == RESOLVE_NAME_EARLY &&
868 rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) {
869 r = rule_resolve_user(rules, value, &uid);
870 if (r < 0)
871 return log_token_error_errno(rules, r, "Failed to resolve user name '%s': %m", value);
872
873 r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid));
874 } else if (rules->resolve_name_timing != RESOLVE_NAME_NEVER) {
875 check_value_format_and_warn(rules, key, value, true);
876 r = rule_line_add_token(rule_line, TK_A_OWNER, op, value, NULL);
877 } else {
878 log_token_debug(rules, "User name resolution is disabled, ignoring %s=%s", key, value);
879 return 0;
880 }
881 } else if (streq(key, "GROUP")) {
882 gid_t gid;
883
884 if (attr)
885 return log_token_invalid_attr(rules, key);
886 if (is_match || op == OP_REMOVE)
887 return log_token_invalid_op(rules, key);
888 if (op == OP_ADD) {
889 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
890 op = OP_ASSIGN;
891 }
892
893 if (parse_gid(value, &gid) >= 0)
894 r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid));
895 else if (rules->resolve_name_timing == RESOLVE_NAME_EARLY &&
896 rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) {
897 r = rule_resolve_group(rules, value, &gid);
898 if (r < 0)
899 return log_token_error_errno(rules, r, "Failed to resolve group name '%s': %m", value);
900
901 r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid));
902 } else if (rules->resolve_name_timing != RESOLVE_NAME_NEVER) {
903 check_value_format_and_warn(rules, key, value, true);
904 r = rule_line_add_token(rule_line, TK_A_GROUP, op, value, NULL);
905 } else {
906 log_token_debug(rules, "Resolving group name is disabled, ignoring GROUP=\"%s\"", value);
907 return 0;
908 }
909 } else if (streq(key, "MODE")) {
910 mode_t mode;
911
912 if (attr)
913 return log_token_invalid_attr(rules, key);
914 if (is_match || op == OP_REMOVE)
915 return log_token_invalid_op(rules, key);
916 if (op == OP_ADD) {
917 log_token_warning(rules, "%s key takes '=' or ':=' operator, assuming '='.", key);
918 op = OP_ASSIGN;
919 }
920
921 if (parse_mode(value, &mode) >= 0)
922 r = rule_line_add_token(rule_line, TK_A_MODE_ID, op, NULL, MODE_TO_PTR(mode));
923 else {
924 check_value_format_and_warn(rules, key, value, true);
925 r = rule_line_add_token(rule_line, TK_A_MODE, op, value, NULL);
926 }
927 } else if (streq(key, "SECLABEL")) {
928 if (isempty(attr))
929 return log_token_invalid_attr(rules, key);
930 check_value_format_and_warn(rules, key, value, true);
931 if (is_match || op == OP_REMOVE)
932 return log_token_invalid_op(rules, key);
933 if (op == OP_ASSIGN_FINAL) {
934 log_token_warning(rules, "%s key takes '=' or '+=' operator, assuming '='.", key);
935 op = OP_ASSIGN;
936 }
937
938 r = rule_line_add_token(rule_line, TK_A_SECLABEL, op, value, attr);
939 } else if (streq(key, "RUN")) {
940 if (is_match || op == OP_REMOVE)
941 return log_token_invalid_op(rules, key);
942 check_value_format_and_warn(rules, key, value, true);
943 if (!attr || streq(attr, "program"))
944 r = rule_line_add_token(rule_line, TK_A_RUN_PROGRAM, op, value, NULL);
945 else if (streq(attr, "builtin")) {
946 UdevBuiltinCommand cmd;
947
948 cmd = udev_builtin_lookup(value);
949 if (cmd < 0)
950 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL),
951 "Unknown builtin command '%s', ignoring", value);
952 r = rule_line_add_token(rule_line, TK_A_RUN_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd));
953 } else
954 return log_token_invalid_attr(rules, key);
955 } else if (streq(key, "GOTO")) {
956 if (attr)
957 return log_token_invalid_attr(rules, key);
958 if (op != OP_ASSIGN)
959 return log_token_invalid_op(rules, key);
960 if (FLAGS_SET(rule_line->type, LINE_HAS_GOTO)) {
961 log_token_warning(rules, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value);
962 return 0;
963 }
964
965 rule_line->goto_label = value;
966 SET_FLAG(rule_line->type, LINE_HAS_GOTO, true);
967 return 1;
968 } else if (streq(key, "LABEL")) {
969 if (attr)
970 return log_token_invalid_attr(rules, key);
971 if (op != OP_ASSIGN)
972 return log_token_invalid_op(rules, key);
973
974 rule_line->label = value;
975 SET_FLAG(rule_line->type, LINE_HAS_LABEL, true);
976 return 1;
977 } else
978 return log_token_error_errno(rules, SYNTHETIC_ERRNO(EINVAL), "Invalid key '%s'", key);
979 if (r < 0)
980 return log_oom();
981
982 return 1;
983 }
984
985 static UdevRuleOperatorType parse_operator(const char *op) {
986 assert(op);
987
988 if (startswith(op, "=="))
989 return OP_MATCH;
990 if (startswith(op, "!="))
991 return OP_NOMATCH;
992 if (startswith(op, "+="))
993 return OP_ADD;
994 if (startswith(op, "-="))
995 return OP_REMOVE;
996 if (startswith(op, "="))
997 return OP_ASSIGN;
998 if (startswith(op, ":="))
999 return OP_ASSIGN_FINAL;
1000
1001 return _OP_TYPE_INVALID;
1002 }
1003
1004 static int parse_line(char **line, char **ret_key, char **ret_attr, UdevRuleOperatorType *ret_op, char **ret_value) {
1005 char *key_begin, *key_end, *attr, *tmp;
1006 UdevRuleOperatorType op;
1007 int r;
1008
1009 assert(line);
1010 assert(*line);
1011 assert(ret_key);
1012 assert(ret_op);
1013 assert(ret_value);
1014
1015 key_begin = skip_leading_chars(*line, WHITESPACE ",");
1016
1017 if (isempty(key_begin))
1018 return 0;
1019
1020 for (key_end = key_begin; ; key_end++) {
1021 if (key_end[0] == '\0')
1022 return -EINVAL;
1023 if (strchr(WHITESPACE "={", key_end[0]))
1024 break;
1025 if (strchr("+-!:", key_end[0]) && key_end[1] == '=')
1026 break;
1027 }
1028 if (key_end[0] == '{') {
1029 attr = key_end + 1;
1030 tmp = strchr(attr, '}');
1031 if (!tmp)
1032 return -EINVAL;
1033 *tmp++ = '\0';
1034 } else {
1035 attr = NULL;
1036 tmp = key_end;
1037 }
1038
1039 tmp = skip_leading_chars(tmp, NULL);
1040 op = parse_operator(tmp);
1041 if (op < 0)
1042 return -EINVAL;
1043
1044 key_end[0] = '\0';
1045
1046 tmp += op == OP_ASSIGN ? 1 : 2;
1047 tmp = skip_leading_chars(tmp, NULL);
1048 r = udev_rule_parse_value(tmp, ret_value, line);
1049 if (r < 0)
1050 return r;
1051
1052 *ret_key = key_begin;
1053 *ret_attr = attr;
1054 *ret_op = op;
1055 return 1;
1056 }
1057
1058 static void sort_tokens(UdevRuleLine *rule_line) {
1059 assert(rule_line);
1060
1061 UdevRuleToken *old_tokens = TAKE_PTR(rule_line->tokens);
1062 rule_line->current_token = NULL;
1063
1064 while (old_tokens) {
1065 UdevRuleToken *min_token = NULL;
1066
1067 LIST_FOREACH(tokens, t, old_tokens)
1068 if (!min_token || min_token->type > t->type)
1069 min_token = t;
1070
1071 LIST_REMOVE(tokens, old_tokens, min_token);
1072 rule_line_append_token(rule_line, min_token);
1073 }
1074 }
1075
1076 static int rule_add_line(UdevRules *rules, const char *line_str, unsigned line_nr) {
1077 _cleanup_(udev_rule_line_freep) UdevRuleLine *rule_line = NULL;
1078 _cleanup_free_ char *line = NULL;
1079 UdevRuleFile *rule_file;
1080 char *p;
1081 int r;
1082
1083 assert(rules);
1084 assert(rules->current_file);
1085 assert(line_str);
1086
1087 rule_file = rules->current_file;
1088
1089 if (isempty(line_str))
1090 return 0;
1091
1092 /* We use memdup_suffix0() here, since we want to add a second NUL byte to the end, since possibly
1093 * some parsers might turn this into a "nulstr", which requires an extra NUL at the end. */
1094 line = memdup_suffix0(line_str, strlen(line_str) + 1);
1095 if (!line)
1096 return log_oom();
1097
1098 rule_line = new(UdevRuleLine, 1);
1099 if (!rule_line)
1100 return log_oom();
1101
1102 *rule_line = (UdevRuleLine) {
1103 .line = TAKE_PTR(line),
1104 .line_number = line_nr,
1105 .rule_file = rule_file,
1106 };
1107
1108 if (rule_file->current_line)
1109 LIST_APPEND(rule_lines, rule_file->current_line, rule_line);
1110 else
1111 LIST_APPEND(rule_lines, rule_file->rule_lines, rule_line);
1112
1113 rule_file->current_line = rule_line;
1114
1115 for (p = rule_line->line; !isempty(p); ) {
1116 char *key, *attr, *value;
1117 UdevRuleOperatorType op;
1118
1119 r = parse_line(&p, &key, &attr, &op, &value);
1120 if (r < 0)
1121 return log_token_error_errno(rules, r, "Invalid key/value pair, ignoring.");
1122 if (r == 0)
1123 break;
1124
1125 r = parse_token(rules, key, attr, op, value);
1126 if (r < 0)
1127 return r;
1128 }
1129
1130 if (rule_line->type == 0) {
1131 log_token_warning(rules, "The line takes no effect, ignoring.");
1132 return 0;
1133 }
1134
1135 sort_tokens(rule_line);
1136 TAKE_PTR(rule_line);
1137 return 0;
1138 }
1139
1140 static void rule_resolve_goto(UdevRuleFile *rule_file) {
1141 assert(rule_file);
1142
1143 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1144 LIST_FOREACH(rule_lines, line, rule_file->rule_lines) {
1145 if (!FLAGS_SET(line->type, LINE_HAS_GOTO))
1146 continue;
1147
1148 LIST_FOREACH(rule_lines, i, line->rule_lines_next)
1149 if (streq_ptr(i->label, line->goto_label)) {
1150 line->goto_line = i;
1151 break;
1152 }
1153
1154 if (!line->goto_line) {
1155 log_error("%s:%u: GOTO=\"%s\" has no matching label, ignoring",
1156 rule_file->filename, line->line_number, line->goto_label);
1157
1158 SET_FLAG(line->type, LINE_HAS_GOTO, false);
1159 line->goto_label = NULL;
1160
1161 if ((line->type & ~LINE_HAS_LABEL) == 0) {
1162 log_notice("%s:%u: The line takes no effect any more, dropping",
1163 rule_file->filename, line->line_number);
1164 if (line->type == LINE_HAS_LABEL)
1165 udev_rule_line_clear_tokens(line);
1166 else
1167 udev_rule_line_free(line);
1168 }
1169 }
1170 }
1171 }
1172
1173 int udev_rules_parse_file(UdevRules *rules, const char *filename) {
1174 _cleanup_free_ char *continuation = NULL, *name = NULL;
1175 _cleanup_fclose_ FILE *f = NULL;
1176 UdevRuleFile *rule_file;
1177 bool ignore_line = false;
1178 unsigned line_nr = 0;
1179 int r;
1180
1181 f = fopen(filename, "re");
1182 if (!f) {
1183 if (errno == ENOENT)
1184 return 0;
1185
1186 return -errno;
1187 }
1188
1189 (void) fd_warn_permissions(filename, fileno(f));
1190
1191 if (null_or_empty_fd(fileno(f))) {
1192 log_debug("Skipping empty file: %s", filename);
1193 return 0;
1194 }
1195
1196 log_debug("Reading rules file: %s", filename);
1197
1198 name = strdup(filename);
1199 if (!name)
1200 return log_oom();
1201
1202 rule_file = new(UdevRuleFile, 1);
1203 if (!rule_file)
1204 return log_oom();
1205
1206 *rule_file = (UdevRuleFile) {
1207 .filename = TAKE_PTR(name),
1208 };
1209
1210 if (rules->current_file)
1211 LIST_APPEND(rule_files, rules->current_file, rule_file);
1212 else
1213 LIST_APPEND(rule_files, rules->rule_files, rule_file);
1214
1215 rules->current_file = rule_file;
1216
1217 for (;;) {
1218 _cleanup_free_ char *buf = NULL;
1219 size_t len;
1220 char *line;
1221
1222 r = read_line(f, UDEV_LINE_SIZE, &buf);
1223 if (r < 0)
1224 return r;
1225 if (r == 0)
1226 break;
1227
1228 line_nr++;
1229 line = skip_leading_chars(buf, NULL);
1230
1231 if (line[0] == '#')
1232 continue;
1233
1234 len = strlen(line);
1235
1236 if (continuation && !ignore_line) {
1237 if (strlen(continuation) + len >= UDEV_LINE_SIZE)
1238 ignore_line = true;
1239
1240 if (!strextend(&continuation, line))
1241 return log_oom();
1242
1243 if (!ignore_line) {
1244 line = continuation;
1245 len = strlen(line);
1246 }
1247 }
1248
1249 if (len > 0 && line[len - 1] == '\\') {
1250 if (ignore_line)
1251 continue;
1252
1253 line[len - 1] = '\0';
1254 if (!continuation) {
1255 continuation = strdup(line);
1256 if (!continuation)
1257 return log_oom();
1258 }
1259
1260 continue;
1261 }
1262
1263 if (ignore_line)
1264 log_error("%s:%u: Line is too long, ignored", filename, line_nr);
1265 else if (len > 0)
1266 (void) rule_add_line(rules, line, line_nr);
1267
1268 continuation = mfree(continuation);
1269 ignore_line = false;
1270 }
1271
1272 rule_resolve_goto(rule_file);
1273 return 0;
1274 }
1275
1276 UdevRules* udev_rules_new(ResolveNameTiming resolve_name_timing) {
1277 assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX);
1278
1279 UdevRules *rules = new(UdevRules, 1);
1280 if (!rules)
1281 return NULL;
1282
1283 *rules = (UdevRules) {
1284 .resolve_name_timing = resolve_name_timing,
1285 };
1286
1287 return rules;
1288 }
1289
1290 int udev_rules_load(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing) {
1291 _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
1292 _cleanup_strv_free_ char **files = NULL;
1293 int r;
1294
1295 rules = udev_rules_new(resolve_name_timing);
1296 if (!rules)
1297 return -ENOMEM;
1298
1299 (void) udev_rules_check_timestamp(rules);
1300
1301 r = conf_files_list_strv(&files, ".rules", NULL, 0, RULES_DIRS);
1302 if (r < 0)
1303 return log_debug_errno(r, "Failed to enumerate rules files: %m");
1304
1305 STRV_FOREACH(f, files) {
1306 r = udev_rules_parse_file(rules, *f);
1307 if (r < 0)
1308 log_debug_errno(r, "Failed to read rules file %s, ignoring: %m", *f);
1309 }
1310
1311 *ret_rules = TAKE_PTR(rules);
1312 return 0;
1313 }
1314
1315 bool udev_rules_check_timestamp(UdevRules *rules) {
1316 if (!rules)
1317 return false;
1318
1319 return paths_check_timestamp(RULES_DIRS, &rules->dirs_ts_usec, true);
1320 }
1321
1322 static bool token_match_string(UdevRuleToken *token, const char *str) {
1323 const char *i, *value;
1324 bool match = false;
1325
1326 assert(token);
1327 assert(token->value);
1328 assert(token->type < _TK_M_MAX);
1329
1330 str = strempty(str);
1331 value = token->value;
1332
1333 switch (token->match_type) {
1334 case MATCH_TYPE_EMPTY:
1335 match = isempty(str);
1336 break;
1337 case MATCH_TYPE_SUBSYSTEM:
1338 match = STR_IN_SET(str, "subsystem", "class", "bus");
1339 break;
1340 case MATCH_TYPE_PLAIN_WITH_EMPTY:
1341 if (isempty(str)) {
1342 match = true;
1343 break;
1344 }
1345 _fallthrough_;
1346 case MATCH_TYPE_PLAIN:
1347 NULSTR_FOREACH(i, value)
1348 if (streq(i, str)) {
1349 match = true;
1350 break;
1351 }
1352 break;
1353 case MATCH_TYPE_GLOB_WITH_EMPTY:
1354 if (isempty(str)) {
1355 match = true;
1356 break;
1357 }
1358 _fallthrough_;
1359 case MATCH_TYPE_GLOB:
1360 NULSTR_FOREACH(i, value)
1361 if ((fnmatch(i, str, 0) == 0)) {
1362 match = true;
1363 break;
1364 }
1365 break;
1366 default:
1367 assert_not_reached();
1368 }
1369
1370 return token->op == (match ? OP_MATCH : OP_NOMATCH);
1371 }
1372
1373 static bool token_match_attr(UdevRules *rules, UdevRuleToken *token, sd_device *dev, UdevEvent *event) {
1374 char nbuf[UDEV_NAME_SIZE], vbuf[UDEV_NAME_SIZE];
1375 const char *name, *value;
1376 bool truncated;
1377
1378 assert(rules);
1379 assert(token);
1380 assert(IN_SET(token->type, TK_M_ATTR, TK_M_PARENTS_ATTR));
1381 assert(dev);
1382 assert(event);
1383
1384 name = token->data;
1385
1386 switch (token->attr_subst_type) {
1387 case SUBST_TYPE_FORMAT:
1388 (void) udev_event_apply_format(event, name, nbuf, sizeof(nbuf), false, &truncated);
1389 if (truncated) {
1390 log_rule_debug(dev, rules,
1391 "The sysfs attribute name '%s' is truncated while substituting into '%s', "
1392 "assuming the %s key does not match.", nbuf, name,
1393 token->type == TK_M_ATTR ? "ATTR" : "ATTRS");
1394 return false;
1395 }
1396
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 bool truncated;
1501
1502 assert(attr);
1503
1504 tail = strstr(attr, "/*/");
1505 if (!tail)
1506 return 0;
1507
1508 len = tail - attr + 1; /* include slash at the end */
1509 tail += 2; /* include slash at the beginning */
1510
1511 p = buf;
1512 size = sizeof(buf);
1513 size -= strnpcpy_full(&p, size, attr, len, &truncated);
1514 if (truncated)
1515 return -ENOENT;
1516
1517 dir = opendir(buf);
1518 if (!dir)
1519 return -errno;
1520
1521 FOREACH_DIRENT_ALL(de, dir, break) {
1522 if (de->d_name[0] == '.')
1523 continue;
1524
1525 strscpyl_full(p, size, &truncated, de->d_name, tail, NULL);
1526 if (truncated)
1527 continue;
1528
1529 if (faccessat(dirfd(dir), p, F_OK, 0) < 0)
1530 continue;
1531
1532 strcpy(attr, buf);
1533 return 0;
1534 }
1535
1536 return -ENOENT;
1537 }
1538
1539 static int udev_rule_apply_token_to_event(
1540 UdevRules *rules,
1541 sd_device *dev,
1542 UdevEvent *event,
1543 usec_t timeout_usec,
1544 int timeout_signal,
1545 Hashmap *properties_list) {
1546
1547 UdevRuleToken *token;
1548 int r;
1549
1550 assert(rules);
1551 assert(dev);
1552 assert(event);
1553
1554 /* This returns the following values:
1555 * 0 on the current token does not match the event,
1556 * 1 on the current token matches the event, and
1557 * negative errno on some critical errors. */
1558
1559 token = rules->current_file->current_line->current_token;
1560
1561 switch (token->type) {
1562 case TK_M_ACTION: {
1563 sd_device_action_t a;
1564
1565 r = sd_device_get_action(dev, &a);
1566 if (r < 0)
1567 return log_rule_error_errno(dev, rules, r, "Failed to get uevent action type: %m");
1568
1569 return token_match_string(token, device_action_to_string(a));
1570 }
1571 case TK_M_DEVPATH: {
1572 const char *val;
1573
1574 r = sd_device_get_devpath(dev, &val);
1575 if (r < 0)
1576 return log_rule_error_errno(dev, rules, r, "Failed to get devpath: %m");
1577
1578 return token_match_string(token, val);
1579 }
1580 case TK_M_KERNEL:
1581 case TK_M_PARENTS_KERNEL: {
1582 const char *val;
1583
1584 r = sd_device_get_sysname(dev, &val);
1585 if (r < 0)
1586 return log_rule_error_errno(dev, rules, r, "Failed to get sysname: %m");
1587
1588 return token_match_string(token, val);
1589 }
1590 case TK_M_DEVLINK: {
1591 const char *val;
1592
1593 FOREACH_DEVICE_DEVLINK(dev, val)
1594 if (token_match_string(token, strempty(startswith(val, "/dev/"))))
1595 return token->op == OP_MATCH;
1596 return token->op == OP_NOMATCH;
1597 }
1598 case TK_M_NAME:
1599 return token_match_string(token, event->name);
1600 case TK_M_ENV: {
1601 const char *val;
1602
1603 if (sd_device_get_property_value(dev, token->data, &val) < 0)
1604 val = hashmap_get(properties_list, token->data);
1605
1606 return token_match_string(token, val);
1607 }
1608 case TK_M_CONST: {
1609 const char *val, *k = token->data;
1610
1611 if (streq(k, "arch"))
1612 val = architecture_to_string(uname_architecture());
1613 else if (streq(k, "virt"))
1614 val = virtualization_to_string(detect_virtualization());
1615 else
1616 assert_not_reached();
1617 return token_match_string(token, val);
1618 }
1619 case TK_M_TAG:
1620 case TK_M_PARENTS_TAG: {
1621 const char *val;
1622
1623 FOREACH_DEVICE_TAG(dev, val)
1624 if (token_match_string(token, val))
1625 return token->op == OP_MATCH;
1626 return token->op == OP_NOMATCH;
1627 }
1628 case TK_M_SUBSYSTEM:
1629 case TK_M_PARENTS_SUBSYSTEM: {
1630 const char *val;
1631
1632 r = sd_device_get_subsystem(dev, &val);
1633 if (r == -ENOENT)
1634 val = NULL;
1635 else if (r < 0)
1636 return log_rule_error_errno(dev, rules, r, "Failed to get subsystem: %m");
1637
1638 return token_match_string(token, val);
1639 }
1640 case TK_M_DRIVER:
1641 case TK_M_PARENTS_DRIVER: {
1642 const char *val;
1643
1644 r = sd_device_get_driver(dev, &val);
1645 if (r == -ENOENT)
1646 val = NULL;
1647 else if (r < 0)
1648 return log_rule_error_errno(dev, rules, r, "Failed to get driver: %m");
1649
1650 return token_match_string(token, val);
1651 }
1652 case TK_M_ATTR:
1653 case TK_M_PARENTS_ATTR:
1654 return token_match_attr(rules, token, dev, event);
1655 case TK_M_SYSCTL: {
1656 _cleanup_free_ char *value = NULL;
1657 char buf[UDEV_PATH_SIZE];
1658 bool truncated;
1659
1660 (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false, &truncated);
1661 if (truncated) {
1662 log_rule_debug(dev, rules, "The sysctl entry name '%s' is truncated while substituting into '%s', "
1663 "assuming the SYSCTL key does not match.", buf, (const char*) token->data);
1664 return false;
1665 }
1666
1667 r = sysctl_read(sysctl_normalize(buf), &value);
1668 if (r < 0 && r != -ENOENT)
1669 return log_rule_error_errno(dev, rules, r, "Failed to read sysctl '%s': %m", buf);
1670
1671 return token_match_string(token, strstrip(value));
1672 }
1673 case TK_M_TEST: {
1674 mode_t mode = PTR_TO_MODE(token->data);
1675 char buf[UDEV_PATH_SIZE];
1676 struct stat statbuf;
1677 bool match, truncated;
1678
1679 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
1680 if (truncated) {
1681 log_rule_debug(dev, rules, "The file name '%s' is truncated while substituting into '%s', "
1682 "assuming the TEST key does not match", buf, token->value);
1683 return false;
1684 }
1685
1686 if (!path_is_absolute(buf) &&
1687 udev_resolve_subsys_kernel(buf, buf, sizeof(buf), false) < 0) {
1688 char tmp[UDEV_PATH_SIZE];
1689 const char *val;
1690
1691 r = sd_device_get_syspath(dev, &val);
1692 if (r < 0)
1693 return log_rule_error_errno(dev, rules, r, "Failed to get syspath: %m");
1694
1695 strscpy_full(tmp, sizeof(tmp), buf, &truncated);
1696 assert(!truncated);
1697 strscpyl_full(buf, sizeof(buf), &truncated, val, "/", tmp, NULL);
1698 if (truncated)
1699 return false;
1700 }
1701
1702 r = attr_subst_subdir(buf);
1703 if (r == -ENOENT)
1704 return token->op == OP_NOMATCH;
1705 if (r < 0)
1706 return log_rule_error_errno(dev, rules, r, "Failed to test for the existence of '%s': %m", buf);
1707
1708 if (stat(buf, &statbuf) < 0)
1709 return token->op == OP_NOMATCH;
1710
1711 if (mode == MODE_INVALID)
1712 return token->op == OP_MATCH;
1713
1714 match = (statbuf.st_mode & mode) > 0;
1715 return token->op == (match ? OP_MATCH : OP_NOMATCH);
1716 }
1717 case TK_M_PROGRAM: {
1718 char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE];
1719 bool truncated;
1720 size_t count;
1721
1722 event->program_result = mfree(event->program_result);
1723 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
1724 if (truncated) {
1725 log_rule_debug(dev, rules, "The command '%s' is truncated while substituting into '%s', "
1726 "assuming the PROGRAM key does not match.", buf, token->value);
1727 return false;
1728 }
1729
1730 log_rule_debug(dev, rules, "Running PROGRAM '%s'", buf);
1731
1732 r = udev_event_spawn(event, timeout_usec, timeout_signal, true, buf, result, sizeof(result), NULL);
1733 if (r != 0) {
1734 if (r < 0)
1735 log_rule_warning_errno(dev, rules, r, "Failed to execute \"%s\": %m", buf);
1736 else /* returned value is positive when program fails */
1737 log_rule_debug(dev, rules, "Command \"%s\" returned %d (error)", buf, r);
1738 return token->op == OP_NOMATCH;
1739 }
1740
1741 delete_trailing_chars(result, "\n");
1742 count = udev_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
1743 if (count > 0)
1744 log_rule_debug(dev, rules, "Replaced %zu character(s) in result of \"%s\"",
1745 count, buf);
1746
1747 event->program_result = strdup(result);
1748 return token->op == OP_MATCH;
1749 }
1750 case TK_M_IMPORT_FILE: {
1751 _cleanup_fclose_ FILE *f = NULL;
1752 char buf[UDEV_PATH_SIZE];
1753 bool truncated;
1754
1755 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
1756 if (truncated) {
1757 log_rule_debug(dev, rules, "The file name '%s' to be imported is truncated while substituting into '%s', "
1758 "assuming the IMPORT key does not match.", buf, token->value);
1759 return false;
1760 }
1761
1762 log_rule_debug(dev, rules, "Importing properties from '%s'", buf);
1763
1764 f = fopen(buf, "re");
1765 if (!f) {
1766 if (errno != ENOENT)
1767 return log_rule_error_errno(dev, rules, errno,
1768 "Failed to open '%s': %m", buf);
1769 return token->op == OP_NOMATCH;
1770 }
1771
1772 for (;;) {
1773 _cleanup_free_ char *line = NULL;
1774 char *key, *value;
1775
1776 r = read_line(f, LONG_LINE_MAX, &line);
1777 if (r < 0) {
1778 log_rule_debug_errno(dev, rules, r,
1779 "Failed to read '%s', ignoring: %m", buf);
1780 return token->op == OP_NOMATCH;
1781 }
1782 if (r == 0)
1783 break;
1784
1785 r = get_property_from_string(line, &key, &value);
1786 if (r < 0) {
1787 log_rule_debug_errno(dev, rules, r,
1788 "Failed to parse key and value from '%s', ignoring: %m",
1789 line);
1790 continue;
1791 }
1792 if (r == 0)
1793 continue;
1794
1795 r = device_add_property(dev, key, value);
1796 if (r < 0)
1797 return log_rule_error_errno(dev, rules, r,
1798 "Failed to add property %s=%s: %m",
1799 key, value);
1800 }
1801
1802 return token->op == OP_MATCH;
1803 }
1804 case TK_M_IMPORT_PROGRAM: {
1805 _cleanup_strv_free_ char **lines = NULL;
1806 char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE];
1807 bool truncated;
1808
1809 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
1810 if (truncated) {
1811 log_rule_debug(dev, rules, "The command '%s' is truncated while substituting into '%s', "
1812 "assuming the IMPORT key does not match.", buf, token->value);
1813 return false;
1814 }
1815
1816 log_rule_debug(dev, rules, "Importing properties from results of '%s'", buf);
1817
1818 r = udev_event_spawn(event, timeout_usec, timeout_signal, true, buf, result, sizeof result, &truncated);
1819 if (r != 0) {
1820 if (r < 0)
1821 log_rule_warning_errno(dev, rules, r, "Failed to execute '%s', ignoring: %m", buf);
1822 else /* returned value is positive when program fails */
1823 log_rule_debug(dev, rules, "Command \"%s\" returned %d (error), ignoring", buf, r);
1824 return token->op == OP_NOMATCH;
1825 }
1826
1827 if (truncated) {
1828 bool found = false;
1829
1830 /* Drop the last line. */
1831 for (char *p = PTR_SUB1(buf + strlen(buf), buf); p; p = PTR_SUB1(p, buf))
1832 if (strchr(NEWLINE, *p)) {
1833 *p = '\0';
1834 found = true;
1835 } else if (found)
1836 break;
1837 }
1838
1839 r = strv_split_newlines_full(&lines, result, EXTRACT_RETAIN_ESCAPE);
1840 if (r == -ENOMEM)
1841 return log_oom();
1842 if (r < 0) {
1843 log_rule_warning_errno(dev, rules, r,
1844 "Failed to extract lines from result of command \"%s\", ignoring: %m", buf);
1845 return false;
1846 }
1847
1848 STRV_FOREACH(line, lines) {
1849 char *key, *value;
1850
1851 r = get_property_from_string(*line, &key, &value);
1852 if (r < 0) {
1853 log_rule_debug_errno(dev, rules, r,
1854 "Failed to parse key and value from '%s', ignoring: %m",
1855 *line);
1856 continue;
1857 }
1858 if (r == 0)
1859 continue;
1860
1861 r = device_add_property(dev, key, value);
1862 if (r < 0)
1863 return log_rule_error_errno(dev, rules, r,
1864 "Failed to add property %s=%s: %m",
1865 key, value);
1866 }
1867
1868 return token->op == OP_MATCH;
1869 }
1870 case TK_M_IMPORT_BUILTIN: {
1871 UdevBuiltinCommand cmd = PTR_TO_UDEV_BUILTIN_CMD(token->data);
1872 assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
1873 unsigned mask = 1U << (int) cmd;
1874 char buf[UDEV_LINE_SIZE];
1875 bool truncated;
1876
1877 if (udev_builtin_run_once(cmd)) {
1878 /* check if we ran already */
1879 if (event->builtin_run & mask) {
1880 log_rule_debug(dev, rules, "Skipping builtin '%s' in IMPORT key",
1881 udev_builtin_name(cmd));
1882 /* return the result from earlier run */
1883 return token->op == (event->builtin_ret & mask ? OP_NOMATCH : OP_MATCH);
1884 }
1885 /* mark as ran */
1886 event->builtin_run |= mask;
1887 }
1888
1889 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
1890 if (truncated) {
1891 log_rule_debug(dev, rules, "The builtin command '%s' is truncated while substituting into '%s', "
1892 "assuming the IMPORT key does not match", buf, token->value);
1893 return false;
1894 }
1895
1896 log_rule_debug(dev, rules, "Importing properties from results of builtin command '%s'", buf);
1897
1898 r = udev_builtin_run(dev, &event->rtnl, cmd, buf, false);
1899 if (r < 0) {
1900 /* remember failure */
1901 log_rule_debug_errno(dev, rules, r, "Failed to run builtin '%s': %m", buf);
1902 event->builtin_ret |= mask;
1903 }
1904 return token->op == (r >= 0 ? OP_MATCH : OP_NOMATCH);
1905 }
1906 case TK_M_IMPORT_DB: {
1907 const char *val;
1908
1909 if (!event->dev_db_clone)
1910 return token->op == OP_NOMATCH;
1911 r = sd_device_get_property_value(event->dev_db_clone, token->value, &val);
1912 if (r == -ENOENT)
1913 return token->op == OP_NOMATCH;
1914 if (r < 0)
1915 return log_rule_error_errno(dev, rules, r,
1916 "Failed to get property '%s' from database: %m",
1917 token->value);
1918
1919 r = device_add_property(dev, token->value, val);
1920 if (r < 0)
1921 return log_rule_error_errno(dev, rules, r, "Failed to add property '%s=%s': %m",
1922 token->value, val);
1923 return token->op == OP_MATCH;
1924 }
1925 case TK_M_IMPORT_CMDLINE: {
1926 _cleanup_free_ char *value = NULL;
1927
1928 r = proc_cmdline_get_key(token->value, PROC_CMDLINE_VALUE_OPTIONAL|PROC_CMDLINE_IGNORE_EFI_OPTIONS, &value);
1929 if (r < 0)
1930 return log_rule_error_errno(dev, rules, r,
1931 "Failed to read '%s' option from /proc/cmdline: %m",
1932 token->value);
1933 if (r == 0)
1934 return token->op == OP_NOMATCH;
1935
1936 r = device_add_property(dev, token->value, value ?: "1");
1937 if (r < 0)
1938 return log_rule_error_errno(dev, rules, r, "Failed to add property '%s=%s': %m",
1939 token->value, value ?: "1");
1940 return token->op == OP_MATCH;
1941 }
1942 case TK_M_IMPORT_PARENT: {
1943 char buf[UDEV_PATH_SIZE];
1944 bool truncated;
1945
1946 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
1947 if (truncated) {
1948 log_rule_debug(dev, rules, "The property name '%s' is truncated while substituting into '%s', "
1949 "assuming the IMPORT key does not match.", buf, token->value);
1950 return false;
1951 }
1952
1953 r = import_parent_into_properties(dev, buf);
1954 if (r < 0)
1955 return log_rule_error_errno(dev, rules, r,
1956 "Failed to import properties '%s' from parent: %m",
1957 buf);
1958 return token->op == (r > 0 ? OP_MATCH : OP_NOMATCH);
1959 }
1960 case TK_M_RESULT:
1961 return token_match_string(token, event->program_result);
1962 case TK_A_OPTIONS_STRING_ESCAPE_NONE:
1963 event->esc = ESCAPE_NONE;
1964 break;
1965 case TK_A_OPTIONS_STRING_ESCAPE_REPLACE:
1966 event->esc = ESCAPE_REPLACE;
1967 break;
1968 case TK_A_OPTIONS_DB_PERSIST:
1969 device_set_db_persist(dev);
1970 break;
1971 case TK_A_OPTIONS_INOTIFY_WATCH:
1972 if (event->inotify_watch_final)
1973 break;
1974 if (token->op == OP_ASSIGN_FINAL)
1975 event->inotify_watch_final = true;
1976
1977 event->inotify_watch = token->data;
1978 break;
1979 case TK_A_OPTIONS_DEVLINK_PRIORITY:
1980 device_set_devlink_priority(dev, PTR_TO_INT(token->data));
1981 break;
1982 case TK_A_OPTIONS_LOG_LEVEL: {
1983 int level = PTR_TO_INT(token->data);
1984
1985 if (level < 0)
1986 level = event->default_log_level;
1987
1988 log_set_max_level(level);
1989
1990 if (level == LOG_DEBUG && !event->log_level_was_debug) {
1991 /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */
1992 log_device_uevent(dev, "The log level is changed to 'debug' while processing device");
1993 event->log_level_was_debug = true;
1994 }
1995
1996 break;
1997 }
1998 case TK_A_OWNER: {
1999 char owner[UDEV_NAME_SIZE];
2000 const char *ow = owner;
2001 bool truncated;
2002
2003 if (event->owner_final)
2004 break;
2005 if (token->op == OP_ASSIGN_FINAL)
2006 event->owner_final = true;
2007
2008 (void) udev_event_apply_format(event, token->value, owner, sizeof(owner), false, &truncated);
2009 if (truncated) {
2010 log_rule_warning(dev, rules, "The user name '%s' is truncated while substituting into '%s', "
2011 "refusing to apply the OWNER key.", owner, token->value);
2012 break;
2013 }
2014
2015 r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING);
2016 if (r < 0)
2017 log_unknown_owner(dev, rules, r, "user", owner);
2018 else
2019 log_rule_debug(dev, rules, "OWNER %s(%u)", owner, event->uid);
2020 break;
2021 }
2022 case TK_A_GROUP: {
2023 char group[UDEV_NAME_SIZE];
2024 const char *gr = group;
2025 bool truncated;
2026
2027 if (event->group_final)
2028 break;
2029 if (token->op == OP_ASSIGN_FINAL)
2030 event->group_final = true;
2031
2032 (void) udev_event_apply_format(event, token->value, group, sizeof(group), false, &truncated);
2033 if (truncated) {
2034 log_rule_warning(dev, rules, "The group name '%s' is truncated while substituting into '%s', "
2035 "refusing to apply the GROUP key.", group, token->value);
2036 break;
2037 }
2038
2039 r = get_group_creds(&gr, &event->gid, USER_CREDS_ALLOW_MISSING);
2040 if (r < 0)
2041 log_unknown_owner(dev, rules, r, "group", group);
2042 else
2043 log_rule_debug(dev, rules, "GROUP %s(%u)", group, event->gid);
2044 break;
2045 }
2046 case TK_A_MODE: {
2047 char mode_str[UDEV_NAME_SIZE];
2048 bool truncated;
2049
2050 if (event->mode_final)
2051 break;
2052 if (token->op == OP_ASSIGN_FINAL)
2053 event->mode_final = true;
2054
2055 (void) udev_event_apply_format(event, token->value, mode_str, sizeof(mode_str), false, &truncated);
2056 if (truncated) {
2057 log_rule_warning(dev, rules, "The mode '%s' is truncated while substituting into %s, "
2058 "refusing to apply the MODE key.", mode_str, token->value);
2059 break;
2060 }
2061
2062 r = parse_mode(mode_str, &event->mode);
2063 if (r < 0)
2064 log_rule_error_errno(dev, rules, r, "Failed to parse mode '%s', ignoring: %m", mode_str);
2065 else
2066 log_rule_debug(dev, rules, "MODE %#o", event->mode);
2067 break;
2068 }
2069 case TK_A_OWNER_ID:
2070 if (event->owner_final)
2071 break;
2072 if (token->op == OP_ASSIGN_FINAL)
2073 event->owner_final = true;
2074 if (!token->data)
2075 break;
2076 event->uid = PTR_TO_UID(token->data);
2077 log_rule_debug(dev, rules, "OWNER %u", event->uid);
2078 break;
2079 case TK_A_GROUP_ID:
2080 if (event->group_final)
2081 break;
2082 if (token->op == OP_ASSIGN_FINAL)
2083 event->group_final = true;
2084 if (!token->data)
2085 break;
2086 event->gid = PTR_TO_GID(token->data);
2087 log_rule_debug(dev, rules, "GROUP %u", event->gid);
2088 break;
2089 case TK_A_MODE_ID:
2090 if (event->mode_final)
2091 break;
2092 if (token->op == OP_ASSIGN_FINAL)
2093 event->mode_final = true;
2094 if (!token->data)
2095 break;
2096 event->mode = PTR_TO_MODE(token->data);
2097 log_rule_debug(dev, rules, "MODE %#o", event->mode);
2098 break;
2099 case TK_A_SECLABEL: {
2100 _cleanup_free_ char *name = NULL, *label = NULL;
2101 char label_str[UDEV_LINE_SIZE] = {};
2102 bool truncated;
2103
2104 name = strdup(token->data);
2105 if (!name)
2106 return log_oom();
2107
2108 (void) udev_event_apply_format(event, token->value, label_str, sizeof(label_str), false, &truncated);
2109 if (truncated) {
2110 log_rule_warning(dev, rules, "The security label '%s' is truncated while substituting into '%s', "
2111 "refusing to apply the SECLABEL key.", label_str, token->value);
2112 break;
2113 }
2114
2115 if (!isempty(label_str))
2116 label = strdup(label_str);
2117 else
2118 label = strdup(token->value);
2119 if (!label)
2120 return log_oom();
2121
2122 if (token->op == OP_ASSIGN)
2123 ordered_hashmap_clear_free_free(event->seclabel_list);
2124
2125 r = ordered_hashmap_ensure_put(&event->seclabel_list, NULL, name, label);
2126 if (r == -ENOMEM)
2127 return log_oom();
2128 if (r < 0)
2129 return log_rule_error_errno(dev, rules, r, "Failed to store SECLABEL{%s}='%s': %m", name, label);;
2130
2131 log_rule_debug(dev, rules, "SECLABEL{%s}='%s'", name, label);
2132
2133 TAKE_PTR(name);
2134 TAKE_PTR(label);
2135 break;
2136 }
2137 case TK_A_ENV: {
2138 const char *val, *name = token->data;
2139 char value_new[UDEV_NAME_SIZE], *p = value_new;
2140 size_t count, l = sizeof(value_new);
2141 bool truncated;
2142
2143 if (isempty(token->value)) {
2144 if (token->op == OP_ADD)
2145 break;
2146 r = device_add_property(dev, name, NULL);
2147 if (r < 0)
2148 return log_rule_error_errno(dev, rules, r, "Failed to remove property '%s': %m", name);
2149 break;
2150 }
2151
2152 if (token->op == OP_ADD &&
2153 sd_device_get_property_value(dev, name, &val) >= 0) {
2154 l = strpcpyl_full(&p, l, &truncated, val, " ", NULL);
2155 if (truncated) {
2156 log_rule_warning(dev, rules, "The buffer for the property '%s' is full, "
2157 "refusing to append the new value '%s'.", name, token->value);
2158 break;
2159 }
2160 }
2161
2162 (void) udev_event_apply_format(event, token->value, p, l, false, &truncated);
2163 if (truncated) {
2164 log_rule_warning(dev, rules, "The property value '%s' is truncated while substituting into '%s', "
2165 "refusing to add property '%s'.", p, token->value, name);
2166 break;
2167 }
2168
2169 if (event->esc == ESCAPE_REPLACE) {
2170 count = udev_replace_chars(p, NULL);
2171 if (count > 0)
2172 log_rule_debug(dev, rules, "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"",
2173 count, name, token->op == OP_ADD ? "+" : "", token->value);
2174 }
2175
2176 r = device_add_property(dev, name, value_new);
2177 if (r < 0)
2178 return log_rule_error_errno(dev, rules, r, "Failed to add property '%s=%s': %m", name, value_new);
2179 break;
2180 }
2181 case TK_A_TAG: {
2182 char buf[UDEV_PATH_SIZE];
2183 bool truncated;
2184
2185 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
2186 if (truncated) {
2187 log_rule_warning(dev, rules, "The tag name '%s' is truncated while substituting into '%s',"
2188 "refusing to %s the tag.", buf, token->value,
2189 token->op == OP_REMOVE ? "remove" : "add");
2190 break;
2191 }
2192
2193 if (token->op == OP_ASSIGN)
2194 device_cleanup_tags(dev);
2195
2196 if (buf[strspn(buf, ALPHANUMERICAL "-_")] != '\0') {
2197 log_rule_error(dev, rules, "Invalid tag name '%s', ignoring", buf);
2198 break;
2199 }
2200 if (token->op == OP_REMOVE)
2201 device_remove_tag(dev, buf);
2202 else {
2203 r = device_add_tag(dev, buf, true);
2204 if (r < 0)
2205 return log_rule_error_errno(dev, rules, r, "Failed to add tag '%s': %m", buf);
2206 }
2207 break;
2208 }
2209 case TK_A_NAME: {
2210 char buf[UDEV_PATH_SIZE];
2211 bool truncated;
2212 size_t count;
2213
2214 if (event->name_final)
2215 break;
2216 if (token->op == OP_ASSIGN_FINAL)
2217 event->name_final = true;
2218
2219 if (sd_device_get_ifindex(dev, NULL) < 0) {
2220 log_rule_error(dev, rules,
2221 "Only network interfaces can be renamed, ignoring NAME=\"%s\".",
2222 token->value);
2223 break;
2224 }
2225
2226 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
2227 if (truncated) {
2228 log_rule_warning(dev, rules, "The network interface name '%s' is truncated while substituting into '%s', "
2229 "refusing to set the name.", buf, token->value);
2230 break;
2231 }
2232
2233 if (IN_SET(event->esc, ESCAPE_UNSET, ESCAPE_REPLACE)) {
2234 if (naming_scheme_has(NAMING_REPLACE_STRICTLY))
2235 count = udev_replace_ifname(buf);
2236 else
2237 count = udev_replace_chars(buf, "/");
2238 if (count > 0)
2239 log_rule_debug(dev, rules, "Replaced %zu character(s) from result of NAME=\"%s\"",
2240 count, token->value);
2241 }
2242 r = free_and_strdup_warn(&event->name, buf);
2243 if (r < 0)
2244 return r;
2245
2246 log_rule_debug(dev, rules, "NAME '%s'", event->name);
2247 break;
2248 }
2249 case TK_A_DEVLINK: {
2250 char buf[UDEV_PATH_SIZE], *p;
2251 bool truncated;
2252 size_t count;
2253
2254 if (event->devlink_final)
2255 break;
2256 if (sd_device_get_devnum(dev, NULL) < 0)
2257 break;
2258 if (token->op == OP_ASSIGN_FINAL)
2259 event->devlink_final = true;
2260 if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL))
2261 device_cleanup_devlinks(dev);
2262
2263 /* allow multiple symlinks separated by spaces */
2264 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), event->esc != ESCAPE_NONE, &truncated);
2265 if (truncated) {
2266 log_rule_warning(dev, rules, "The symbolic link path '%s' is truncated while substituting into '%s', "
2267 "refusing to add the device symbolic link.", buf, token->value);
2268 break;
2269 }
2270
2271 if (event->esc == ESCAPE_UNSET)
2272 count = udev_replace_chars(buf, "/ ");
2273 else if (event->esc == ESCAPE_REPLACE)
2274 count = udev_replace_chars(buf, "/");
2275 else
2276 count = 0;
2277 if (count > 0)
2278 log_rule_debug(dev, rules, "Replaced %zu character(s) from result of SYMLINK=\"%s\"",
2279 count, token->value);
2280
2281 p = skip_leading_chars(buf, NULL);
2282 while (!isempty(p)) {
2283 char filename[UDEV_PATH_SIZE], *next;
2284
2285 next = strchr(p, ' ');
2286 if (next) {
2287 *next++ = '\0';
2288 next = skip_leading_chars(next, NULL);
2289 }
2290
2291 strscpyl_full(filename, sizeof(filename), &truncated, "/dev/", p, NULL);
2292 if (truncated)
2293 continue;
2294
2295 r = device_add_devlink(dev, filename);
2296 if (r < 0)
2297 return log_rule_error_errno(dev, rules, r, "Failed to add devlink '%s': %m", filename);
2298
2299 log_rule_debug(dev, rules, "LINK '%s'", p);
2300 p = next;
2301 }
2302 break;
2303 }
2304 case TK_A_ATTR: {
2305 char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
2306 const char *val, *key_name = token->data;
2307 bool truncated;
2308
2309 if (udev_resolve_subsys_kernel(key_name, buf, sizeof(buf), false) < 0 &&
2310 sd_device_get_syspath(dev, &val) >= 0) {
2311 strscpyl_full(buf, sizeof(buf), &truncated, val, "/", key_name, NULL);
2312 if (truncated) {
2313 log_rule_warning(dev, rules,
2314 "The path to the attribute '%s/%s' is too long, refusing to set the attribute.",
2315 val, key_name);
2316 break;
2317 }
2318 }
2319
2320 r = attr_subst_subdir(buf);
2321 if (r < 0) {
2322 log_rule_error_errno(dev, rules, r, "Could not find file matches '%s', ignoring: %m", buf);
2323 break;
2324 }
2325 (void) udev_event_apply_format(event, token->value, value, sizeof(value), false, &truncated);
2326 if (truncated) {
2327 log_rule_warning(dev, rules, "The attribute value '%s' is truncated while substituting into '%s', "
2328 "refusing to set the attribute '%s'", value, token->value, buf);
2329 break;
2330 }
2331
2332 log_rule_debug(dev, rules, "ATTR '%s' writing '%s'", buf, value);
2333 r = write_string_file(buf, value,
2334 WRITE_STRING_FILE_VERIFY_ON_FAILURE |
2335 WRITE_STRING_FILE_DISABLE_BUFFER |
2336 WRITE_STRING_FILE_AVOID_NEWLINE |
2337 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE);
2338 if (r < 0)
2339 log_rule_error_errno(dev, rules, r, "Failed to write ATTR{%s}, ignoring: %m", buf);
2340 break;
2341 }
2342 case TK_A_SYSCTL: {
2343 char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
2344 bool truncated;
2345
2346 (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false, &truncated);
2347 if (truncated) {
2348 log_rule_warning(dev, rules, "The sysctl entry name '%s' is truncated while substituting into '%s', "
2349 "refusing to set the sysctl entry.", buf, (const char*) token->data);
2350 break;
2351 }
2352
2353 (void) udev_event_apply_format(event, token->value, value, sizeof(value), false, &truncated);
2354 if (truncated) {
2355 log_rule_warning(dev, rules, "The sysctl value '%s' is truncated while substituting into '%s', "
2356 "refusing to set the sysctl entry '%s'", value, token->value, buf);
2357 break;
2358 }
2359
2360 sysctl_normalize(buf);
2361 log_rule_debug(dev, rules, "SYSCTL '%s' writing '%s'", buf, value);
2362 r = sysctl_write(buf, value);
2363 if (r < 0)
2364 log_rule_error_errno(dev, rules, r, "Failed to write SYSCTL{%s}='%s', ignoring: %m", buf, value);
2365 break;
2366 }
2367 case TK_A_RUN_BUILTIN:
2368 case TK_A_RUN_PROGRAM: {
2369 _cleanup_free_ char *cmd = NULL;
2370 char buf[UDEV_LINE_SIZE];
2371 bool truncated;
2372
2373 if (event->run_final)
2374 break;
2375 if (token->op == OP_ASSIGN_FINAL)
2376 event->run_final = true;
2377
2378 if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL))
2379 ordered_hashmap_clear_free_key(event->run_list);
2380
2381 (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated);
2382 if (truncated) {
2383 log_rule_warning(dev, rules, "The command '%s' is truncated while substituting into '%s', "
2384 "refusing to invoke the command.", buf, token->value);
2385 break;
2386 }
2387
2388 cmd = strdup(buf);
2389 if (!cmd)
2390 return log_oom();
2391
2392 r = ordered_hashmap_ensure_put(&event->run_list, NULL, cmd, token->data);
2393 if (r == -ENOMEM)
2394 return log_oom();
2395 if (r < 0)
2396 return log_rule_error_errno(dev, rules, r, "Failed to store command '%s': %m", cmd);
2397
2398 TAKE_PTR(cmd);
2399
2400 log_rule_debug(dev, rules, "RUN '%s'", token->value);
2401 break;
2402 }
2403 case TK_A_OPTIONS_STATIC_NODE:
2404 /* do nothing for events. */
2405 break;
2406 default:
2407 assert_not_reached();
2408 }
2409
2410 return true;
2411 }
2412
2413 static bool token_is_for_parents(UdevRuleToken *token) {
2414 return token->type >= TK_M_PARENTS_KERNEL && token->type <= TK_M_PARENTS_TAG;
2415 }
2416
2417 static int udev_rule_apply_parent_token_to_event(
2418 UdevRules *rules,
2419 UdevEvent *event,
2420 int timeout_signal) {
2421
2422 UdevRuleLine *line;
2423 UdevRuleToken *head;
2424 int r;
2425
2426 assert(rules);
2427 assert(rules->current_file);
2428 assert(event);
2429
2430 line = ASSERT_PTR(rules->current_file->current_line);
2431 head = ASSERT_PTR(rules->current_file->current_line->current_token);
2432 event->dev_parent = ASSERT_PTR(event->dev);
2433
2434 for (;;) {
2435 LIST_FOREACH(tokens, token, head) {
2436 if (!token_is_for_parents(token))
2437 return true; /* All parent tokens match. */
2438
2439 line->current_token = token;
2440 r = udev_rule_apply_token_to_event(rules, event->dev_parent, event, 0, timeout_signal, NULL);
2441 if (r < 0)
2442 return r;
2443 if (r == 0)
2444 break;
2445 }
2446 if (r > 0)
2447 /* All parent tokens match, and no more token (except for GOTO) in the line. */
2448 return true;
2449
2450 if (sd_device_get_parent(event->dev_parent, &event->dev_parent) < 0) {
2451 event->dev_parent = NULL;
2452 return false;
2453 }
2454 }
2455 }
2456
2457 static int udev_rule_apply_line_to_event(
2458 UdevRules *rules,
2459 UdevEvent *event,
2460 usec_t timeout_usec,
2461 int timeout_signal,
2462 Hashmap *properties_list,
2463 UdevRuleLine **next_line) {
2464
2465 UdevRuleLine *line = rules->current_file->current_line;
2466 UdevRuleLineType mask = LINE_HAS_GOTO | LINE_UPDATE_SOMETHING;
2467 bool parents_done = false;
2468 sd_device_action_t action;
2469 int r;
2470
2471 r = sd_device_get_action(event->dev, &action);
2472 if (r < 0)
2473 return r;
2474
2475 if (action != SD_DEVICE_REMOVE) {
2476 if (sd_device_get_devnum(event->dev, NULL) >= 0)
2477 mask |= LINE_HAS_DEVLINK;
2478
2479 if (sd_device_get_ifindex(event->dev, NULL) >= 0)
2480 mask |= LINE_HAS_NAME;
2481 }
2482
2483 if ((line->type & mask) == 0)
2484 return 0;
2485
2486 event->esc = ESCAPE_UNSET;
2487
2488 DEVICE_TRACE_POINT(rules_apply_line, event->dev, line->rule_file->filename, line->line_number);
2489
2490 LIST_FOREACH(tokens, token, line->tokens) {
2491 line->current_token = token;
2492
2493 if (token_is_for_parents(token)) {
2494 if (parents_done)
2495 continue;
2496
2497 r = udev_rule_apply_parent_token_to_event(rules, event, timeout_signal);
2498 if (r <= 0)
2499 return r;
2500
2501 parents_done = true;
2502 continue;
2503 }
2504
2505 r = udev_rule_apply_token_to_event(rules, event->dev, event, timeout_usec, timeout_signal, properties_list);
2506 if (r <= 0)
2507 return r;
2508 }
2509
2510 if (line->goto_line)
2511 *next_line = line->goto_line;
2512
2513 return 0;
2514 }
2515
2516 int udev_rules_apply_to_event(
2517 UdevRules *rules,
2518 UdevEvent *event,
2519 usec_t timeout_usec,
2520 int timeout_signal,
2521 Hashmap *properties_list) {
2522
2523 int r;
2524
2525 assert(rules);
2526 assert(event);
2527
2528 LIST_FOREACH(rule_files, file, rules->rule_files) {
2529 rules->current_file = file;
2530 LIST_FOREACH_WITH_NEXT(rule_lines, line, next_line, file->rule_lines) {
2531 file->current_line = line;
2532 r = udev_rule_apply_line_to_event(rules, event, timeout_usec, timeout_signal, properties_list, &next_line);
2533 if (r < 0)
2534 return r;
2535 }
2536 }
2537
2538 return 0;
2539 }
2540
2541 static int udev_rule_line_apply_static_dev_perms(UdevRuleLine *rule_line) {
2542 _cleanup_strv_free_ char **tags = NULL;
2543 uid_t uid = UID_INVALID;
2544 gid_t gid = GID_INVALID;
2545 mode_t mode = MODE_INVALID;
2546 int r;
2547
2548 assert(rule_line);
2549
2550 if (!FLAGS_SET(rule_line->type, LINE_HAS_STATIC_NODE))
2551 return 0;
2552
2553 LIST_FOREACH(tokens, token, rule_line->tokens)
2554 if (token->type == TK_A_OWNER_ID)
2555 uid = PTR_TO_UID(token->data);
2556 else if (token->type == TK_A_GROUP_ID)
2557 gid = PTR_TO_GID(token->data);
2558 else if (token->type == TK_A_MODE_ID)
2559 mode = PTR_TO_MODE(token->data);
2560 else if (token->type == TK_A_TAG) {
2561 r = strv_extend(&tags, token->value);
2562 if (r < 0)
2563 return log_oom();
2564 } else if (token->type == TK_A_OPTIONS_STATIC_NODE) {
2565 r = static_node_apply_permissions(token->value, mode, uid, gid, tags);
2566 if (r < 0)
2567 return r;
2568 }
2569
2570 return 0;
2571 }
2572
2573 int udev_rules_apply_static_dev_perms(UdevRules *rules) {
2574 int r;
2575
2576 assert(rules);
2577
2578 LIST_FOREACH(rule_files, file, rules->rule_files)
2579 LIST_FOREACH(rule_lines, line, file->rule_lines) {
2580 r = udev_rule_line_apply_static_dev_perms(line);
2581 if (r < 0)
2582 return r;
2583 }
2584
2585 return 0;
2586 }