1 /* SPDX-License-Identifier: GPL-2.0+ */
16 #include "alloc-util.h"
17 #include "conf-files.h"
18 #include "device-private.h"
19 #include "device-util.h"
20 #include "dirent-util.h"
25 #include "glob-util.h"
26 #include "libudev-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "proc-cmdline.h"
31 #include "stat-util.h"
32 #include "stdio-util.h"
34 #include "string-util.h"
37 #include "sysctl-util.h"
38 #include "udev-builtin.h"
40 #include "user-util.h"
43 #define PREALLOC_TOKEN 2048
53 static const char* const rules_dirs
[] = {
56 UDEVLIBEXECDIR
"/rules.d",
62 ResolveNameTiming resolve_name_timing
;
64 /* every key in the rules file becomes a token */
69 /* all key strings are copied and de-duplicated in a single continuous string buffer */
70 struct strbuf
*strbuf
;
72 /* during rule parsing, uid/gid lookup results are cached */
81 static char *rules_str(UdevRules
*rules
, unsigned off
) {
82 return rules
->strbuf
->buf
+ off
;
85 static unsigned rules_add_string(UdevRules
*rules
, const char *s
) {
86 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
89 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
103 enum string_glob_type
{
105 GL_PLAIN
, /* no special chars */
106 GL_GLOB
, /* shell globs ?,*,[] */
107 GL_SPLIT
, /* multi-value A|B */
108 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
109 GL_SOMETHING
, /* commonly used "?*" */
112 enum string_subst_type
{
119 /* tokens of a rule are sorted/handled in this order */
124 TK_M_ACTION
, /* val */
125 TK_M_DEVPATH
, /* val */
126 TK_M_KERNEL
, /* val */
127 TK_M_DEVLINK
, /* val */
129 TK_M_ENV
, /* val, attr */
131 TK_M_SUBSYSTEM
, /* val */
132 TK_M_DRIVER
, /* val */
133 TK_M_WAITFOR
, /* val */
134 TK_M_ATTR
, /* val, attr */
135 TK_M_SYSCTL
, /* val, attr */
138 TK_M_KERNELS
, /* val */
139 TK_M_SUBSYSTEMS
, /* val */
140 TK_M_DRIVERS
, /* val */
141 TK_M_ATTRS
, /* val, attr */
145 TK_M_TEST
, /* val, mode_t */
146 TK_M_PROGRAM
, /* val */
147 TK_M_IMPORT_FILE
, /* val */
148 TK_M_IMPORT_PROG
, /* val */
149 TK_M_IMPORT_BUILTIN
, /* val */
150 TK_M_IMPORT_DB
, /* val */
151 TK_M_IMPORT_CMDLINE
, /* val */
152 TK_M_IMPORT_PARENT
, /* val */
153 TK_M_RESULT
, /* val */
156 TK_A_STRING_ESCAPE_NONE
,
157 TK_A_STRING_ESCAPE_REPLACE
,
159 TK_A_INOTIFY_WATCH
, /* int */
160 TK_A_DEVLINK_PRIO
, /* int */
161 TK_A_OWNER
, /* val */
162 TK_A_GROUP
, /* val */
164 TK_A_OWNER_ID
, /* uid_t */
165 TK_A_GROUP_ID
, /* gid_t */
166 TK_A_MODE_ID
, /* mode_t */
168 TK_A_STATIC_NODE
, /* val */
169 TK_A_SECLABEL
, /* val, attr */
170 TK_A_ENV
, /* val, attr */
172 TK_A_DEVLINK
, /* val */
173 TK_A_ATTR
, /* val, attr */
174 TK_A_SYSCTL
, /* val, attr */
175 TK_A_RUN_BUILTIN
, /* val, bool */
176 TK_A_RUN_PROGRAM
, /* val, bool */
177 TK_A_GOTO
, /* size_t */
182 /* we try to pack stuff in a way that we take only 12 bytes per token */
185 unsigned char type
; /* same in rule and key */
187 enum token_type type
:8;
189 bool has_static_node
:1;
191 unsigned short token_count
;
193 unsigned short filename_off
;
194 unsigned short filename_line
;
197 enum token_type type
:8;
198 enum operation_type op
:8;
199 enum string_glob_type glob
:8;
200 enum string_subst_type subst
:4;
201 enum string_subst_type attrsubst
:4;
211 enum udev_builtin_cmd builtin_cmd
;
221 struct token token
[MAX_TK
];
225 #if ENABLE_DEBUG_UDEV
226 static const char *operation_str(enum operation_type type
) {
227 static const char *operation_strs
[] = {
228 [OP_UNSET
] = "UNSET",
229 [OP_MATCH
] = "match",
230 [OP_NOMATCH
] = "nomatch",
231 [OP_MATCH_MAX
] = "MATCH_MAX",
234 [OP_REMOVE
] = "remove",
235 [OP_ASSIGN
] = "assign",
236 [OP_ASSIGN_FINAL
] = "assign-final",
239 return operation_strs
[type
];
242 static const char *string_glob_str(enum string_glob_type type
) {
243 static const char *string_glob_strs
[] = {
244 [GL_UNSET
] = "UNSET",
245 [GL_PLAIN
] = "plain",
247 [GL_SPLIT
] = "split",
248 [GL_SPLIT_GLOB
] = "split-glob",
249 [GL_SOMETHING
] = "split-glob",
252 return string_glob_strs
[type
];
255 static const char *token_str(enum token_type type
) {
256 static const char *token_strs
[] = {
257 [TK_UNSET
] = "UNSET",
260 [TK_M_ACTION
] = "M ACTION",
261 [TK_M_DEVPATH
] = "M DEVPATH",
262 [TK_M_KERNEL
] = "M KERNEL",
263 [TK_M_DEVLINK
] = "M DEVLINK",
264 [TK_M_NAME
] = "M NAME",
265 [TK_M_ENV
] = "M ENV",
266 [TK_M_TAG
] = "M TAG",
267 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
268 [TK_M_DRIVER
] = "M DRIVER",
269 [TK_M_WAITFOR
] = "M WAITFOR",
270 [TK_M_ATTR
] = "M ATTR",
271 [TK_M_SYSCTL
] = "M SYSCTL",
273 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
274 [TK_M_KERNELS
] = "M KERNELS",
275 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
276 [TK_M_DRIVERS
] = "M DRIVERS",
277 [TK_M_ATTRS
] = "M ATTRS",
278 [TK_M_TAGS
] = "M TAGS",
279 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
281 [TK_M_TEST
] = "M TEST",
282 [TK_M_PROGRAM
] = "M PROGRAM",
283 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
284 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
285 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
286 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
287 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
288 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
289 [TK_M_RESULT
] = "M RESULT",
290 [TK_M_MAX
] = "M MAX",
292 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
293 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
294 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
295 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
296 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
297 [TK_A_OWNER
] = "A OWNER",
298 [TK_A_GROUP
] = "A GROUP",
299 [TK_A_MODE
] = "A MODE",
300 [TK_A_OWNER_ID
] = "A OWNER_ID",
301 [TK_A_GROUP_ID
] = "A GROUP_ID",
302 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
303 [TK_A_SECLABEL
] = "A SECLABEL",
304 [TK_A_MODE_ID
] = "A MODE_ID",
305 [TK_A_ENV
] = "A ENV",
306 [TK_A_TAG
] = "A ENV",
307 [TK_A_NAME
] = "A NAME",
308 [TK_A_DEVLINK
] = "A DEVLINK",
309 [TK_A_ATTR
] = "A ATTR",
310 [TK_A_SYSCTL
] = "A SYSCTL",
311 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
312 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
313 [TK_A_GOTO
] = "A GOTO",
318 return token_strs
[type
];
321 static void dump_token(UdevRules
*rules
, struct token
*token
) {
322 enum token_type type
= token
->type
;
323 enum operation_type op
= token
->key
.op
;
324 enum string_glob_type glob
= token
->key
.glob
;
325 const char *value
= rules_str(rules
, token
->key
.value_off
);
326 const char *attr
= &rules
->strbuf
->buf
[token
->key
.attr_off
];
331 const char *tks_ptr
= (char *)rules
->tokens
;
332 const char *tk_ptr
= (char *)token
;
333 unsigned idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
335 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
336 &rules
->strbuf
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
337 idx
, token
->rule
.token_count
,
338 &rules
->strbuf
->buf
[token
->rule
.label_off
]);
350 case TK_M_SUBSYSTEMS
:
354 case TK_M_IMPORT_FILE
:
355 case TK_M_IMPORT_PROG
:
357 case TK_M_IMPORT_CMDLINE
:
358 case TK_M_IMPORT_PARENT
:
365 case TK_A_RUN_BUILTIN
:
366 case TK_A_RUN_PROGRAM
:
367 log_debug("%s %s '%s'(%s)",
368 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
370 case TK_M_IMPORT_BUILTIN
:
371 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
380 log_debug("%s %s '%s' '%s'(%s)",
381 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
385 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
387 case TK_A_STRING_ESCAPE_NONE
:
388 case TK_A_STRING_ESCAPE_REPLACE
:
389 case TK_A_DB_PERSIST
:
390 log_debug("%s", token_str(type
));
393 log_debug("%s %s '%s'(%s) %#o",
394 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
396 case TK_A_INOTIFY_WATCH
:
397 log_debug("%s %u", token_str(type
), token
->key
.watch
);
399 case TK_A_DEVLINK_PRIO
:
400 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
403 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
406 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
409 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
411 case TK_A_STATIC_NODE
:
412 log_debug("%s '%s'", token_str(type
), value
);
415 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
418 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
421 log_debug("* %s", token_str(type
));
423 case TK_M_PARENTS_MIN
:
424 case TK_M_PARENTS_MAX
:
427 log_debug("Unknown token type %u", type
);
432 static void dump_rules(UdevRules
*rules
) {
435 log_debug("Dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings",
437 rules
->token_cur
* sizeof(struct token
),
438 rules
->strbuf
->nodes_count
,
440 for (i
= 0; i
< rules
->token_cur
; i
++)
441 dump_token(rules
, &rules
->tokens
[i
]);
444 static void dump_token(UdevRules
*rules
, struct token
*token
) {}
445 static void dump_rules(UdevRules
*rules
) {}
446 #endif /* ENABLE_DEBUG_UDEV */
448 static int add_token(UdevRules
*rules
, struct token
*token
) {
449 /* grow buffer if needed */
450 if (rules
->token_cur
+1 >= rules
->token_max
) {
451 struct token
*tokens
;
454 /* double the buffer size */
455 add
= rules
->token_max
;
459 tokens
= reallocarray(rules
->tokens
, rules
->token_max
+ add
, sizeof(struct token
));
462 rules
->tokens
= tokens
;
463 rules
->token_max
+= add
;
465 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
470 static void log_unknown_owner(sd_device
*dev
, int error
, const char *entity
, const char *owner
) {
471 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
472 log_device_error(dev
, "Specified %s '%s' unknown", entity
, owner
);
474 log_device_error_errno(dev
, error
, "Failed to resolve %s '%s': %m", entity
, owner
);
477 static uid_t
add_uid(UdevRules
*rules
, const char *owner
) {
483 /* lookup, if we know it already */
484 for (i
= 0; i
< rules
->uids_cur
; i
++) {
485 off
= rules
->uids
[i
].name_off
;
486 if (streq(rules_str(rules
, off
), owner
)) {
487 uid
= rules
->uids
[i
].uid
;
491 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
493 log_unknown_owner(NULL
, r
, "user", owner
);
495 /* grow buffer if needed */
496 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
497 struct uid_gid
*uids
;
500 /* double the buffer size */
501 add
= rules
->uids_max
;
505 uids
= reallocarray(rules
->uids
, rules
->uids_max
+ add
, sizeof(struct uid_gid
));
509 rules
->uids_max
+= add
;
511 rules
->uids
[rules
->uids_cur
].uid
= uid
;
512 off
= rules_add_string(rules
, owner
);
515 rules
->uids
[rules
->uids_cur
].name_off
= off
;
520 static gid_t
add_gid(UdevRules
*rules
, const char *group
) {
526 /* lookup, if we know it already */
527 for (i
= 0; i
< rules
->gids_cur
; i
++) {
528 off
= rules
->gids
[i
].name_off
;
529 if (streq(rules_str(rules
, off
), group
)) {
530 gid
= rules
->gids
[i
].gid
;
534 r
= get_group_creds(&group
, &gid
, USER_CREDS_ALLOW_MISSING
);
536 log_unknown_owner(NULL
, r
, "group", group
);
538 /* grow buffer if needed */
539 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
540 struct uid_gid
*gids
;
543 /* double the buffer size */
544 add
= rules
->gids_max
;
548 gids
= reallocarray(rules
->gids
, rules
->gids_max
+ add
, sizeof(struct uid_gid
));
552 rules
->gids_max
+= add
;
554 rules
->gids
[rules
->gids_cur
].gid
= gid
;
555 off
= rules_add_string(rules
, group
);
558 rules
->gids
[rules
->gids_cur
].name_off
= off
;
563 static int import_property_from_string(sd_device
*dev
, char *line
) {
570 while (isspace(key
[0]))
573 /* comment or empty line */
574 if (IN_SET(key
[0], '#', '\0'))
577 /* split key/value */
578 val
= strchr(key
, '=');
585 while (isspace(val
[0]))
592 while (isspace(key
[len
-1]))
596 /* terminate value */
600 while (isspace(val
[len
-1]))
608 if (IN_SET(val
[0], '"', '\'')) {
609 if (len
== 1 || val
[len
-1] != val
[0])
610 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
611 "Inconsistent quoting: '%s', skip",
617 return device_add_property(dev
, key
, val
);
620 static int import_file_into_properties(sd_device
*dev
, const char *filename
) {
621 _cleanup_fclose_
FILE *f
= NULL
;
624 f
= fopen(filename
, "re");
629 _cleanup_free_
char *line
= NULL
;
631 r
= read_line(f
, LONG_LINE_MAX
, &line
);
637 (void) import_property_from_string(dev
, line
);
643 static int import_program_into_properties(UdevEvent
*event
,
645 const char *program
) {
646 char result
[UTIL_LINE_SIZE
];
650 r
= udev_event_spawn(event
, timeout_usec
, false, program
, result
, sizeof result
);
660 pos
= strchr(line
, '\n');
665 (void) import_property_from_string(event
->dev
, line
);
671 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
672 const char *key
, *val
;
679 r
= sd_device_get_parent(dev
, &parent
);
683 FOREACH_DEVICE_PROPERTY(parent
, key
, val
)
684 if (fnmatch(filter
, key
, 0) == 0)
685 device_add_property(dev
, key
, val
);
689 static void attr_subst_subdir(char *attr
, size_t len
) {
690 const char *pos
, *tail
, *path
;
691 _cleanup_closedir_
DIR *dir
= NULL
;
694 pos
= strstr(attr
, "/*/");
699 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
704 FOREACH_DIRENT_ALL(dent
, dir
, break)
705 if (dent
->d_name
[0] != '.') {
706 char n
[strlen(dent
->d_name
) + strlen(tail
) + 1];
708 strscpyl(n
, sizeof n
, dent
->d_name
, tail
, NULL
);
709 if (faccessat(dirfd(dir
), n
, F_OK
, 0) == 0) {
710 strscpyl(attr
, len
, path
, n
, NULL
);
716 static int get_key(char **line
, char **key
, enum operation_type
*op
, char **value
) {
722 if (!linepos
|| linepos
[0] == '\0')
725 /* skip whitespace */
726 while (isspace(linepos
[0]) || linepos
[0] == ',')
730 if (linepos
[0] == '\0')
736 if (linepos
[0] == '\0')
738 if (isspace(linepos
[0]))
740 if (linepos
[0] == '=')
742 if (IN_SET(linepos
[0], '+', '-', '!', ':'))
743 if (linepos
[1] == '=')
747 /* remember end of key */
750 /* skip whitespace after key */
751 while (isspace(linepos
[0]))
753 if (linepos
[0] == '\0')
756 /* get operation type */
757 if (linepos
[0] == '=' && linepos
[1] == '=') {
760 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
763 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
766 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
769 } else if (linepos
[0] == '=') {
772 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
773 *op
= OP_ASSIGN_FINAL
;
781 /* skip whitespace after operator */
782 while (isspace(linepos
[0]))
784 if (linepos
[0] == '\0')
788 if (linepos
[0] == '"')
795 for (i
= 0, j
= 0; ; i
++, j
++) {
797 if (linepos
[i
] == '"')
800 if (linepos
[i
] == '\0')
803 /* double quotes can be escaped */
804 if (linepos
[i
] == '\\')
805 if (linepos
[i
+1] == '"')
808 linepos
[j
] = linepos
[i
];
812 /* move line to next key */
813 *line
= linepos
+ i
+ 1;
817 /* extract possible KEY{attr} */
818 static const char *get_key_attribute(char *str
) {
822 attr
= strchr(str
, '{');
825 pos
= strchr(attr
, '}');
827 log_error("Missing closing brace for format");
836 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
837 enum operation_type op
,
838 const char *value
, const void *data
) {
839 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
840 const char *attr
= NULL
;
842 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
))
845 memzero(token
, sizeof(struct token
));
857 case TK_M_SUBSYSTEMS
:
861 case TK_M_IMPORT_FILE
:
862 case TK_M_IMPORT_PROG
:
864 case TK_M_IMPORT_CMDLINE
:
865 case TK_M_IMPORT_PARENT
:
875 case TK_A_STATIC_NODE
:
876 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
878 case TK_M_IMPORT_BUILTIN
:
879 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
880 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
891 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
892 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
895 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
897 token
->key
.mode
= *(mode_t
*)data
;
899 case TK_A_STRING_ESCAPE_NONE
:
900 case TK_A_STRING_ESCAPE_REPLACE
:
901 case TK_A_DB_PERSIST
:
903 case TK_A_RUN_BUILTIN
:
904 case TK_A_RUN_PROGRAM
:
905 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
906 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
908 case TK_A_INOTIFY_WATCH
:
909 case TK_A_DEVLINK_PRIO
:
910 token
->key
.devlink_prio
= *(int *)data
;
913 token
->key
.uid
= *(uid_t
*)data
;
916 token
->key
.gid
= *(gid_t
*)data
;
919 token
->key
.mode
= *(mode_t
*)data
;
922 case TK_M_PARENTS_MIN
:
923 case TK_M_PARENTS_MAX
:
927 assert_not_reached("wrong type");
930 if (value
&& type
< TK_M_MAX
) {
931 /* check if we need to split or call fnmatch() while matching rules */
932 enum string_glob_type glob
;
933 bool has_split
, has_glob
;
935 has_split
= strchr(value
, '|');
936 has_glob
= string_is_glob(value
);
937 if (has_split
&& has_glob
)
938 glob
= GL_SPLIT_GLOB
;
942 if (streq(value
, "?*"))
949 token
->key
.glob
= glob
;
952 if (value
&& type
> TK_M_MAX
) {
953 /* check if assigned value has substitution chars */
955 token
->key
.subst
= SB_SUBSYS
;
956 else if (strchr(value
, '%') || strchr(value
, '$'))
957 token
->key
.subst
= SB_FORMAT
;
959 token
->key
.subst
= SB_NONE
;
963 /* check if property/attribute name has substitution chars */
965 token
->key
.attrsubst
= SB_SUBSYS
;
966 else if (strchr(attr
, '%') || strchr(attr
, '$'))
967 token
->key
.attrsubst
= SB_FORMAT
;
969 token
->key
.attrsubst
= SB_NONE
;
972 token
->key
.type
= type
;
974 rule_tmp
->token_cur
++;
979 static int sort_token(UdevRules
*rules
, struct rule_tmp
*rule_tmp
) {
982 unsigned end
= rule_tmp
->token_cur
;
984 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
985 enum token_type next_val
= TK_UNSET
;
986 unsigned next_idx
= 0;
989 /* find smallest value */
990 for (j
= start
; j
< end
; j
++) {
991 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
993 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
994 next_val
= rule_tmp
->token
[j
].type
;
999 /* add token and mark done */
1000 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1002 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1005 if (next_idx
== start
)
1007 if (next_idx
+1 == end
)
1013 #define LOG_RULE_FULL(level, fmt, ...) log_full(level, "%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1014 #define LOG_RULE_ERROR(fmt, ...) LOG_RULE_FULL(LOG_ERR, fmt, ##__VA_ARGS__)
1015 #define LOG_RULE_WARNING(fmt, ...) LOG_RULE_FULL(LOG_WARNING, fmt, ##__VA_ARGS__)
1016 #define LOG_RULE_DEBUG(fmt, ...) LOG_RULE_FULL(LOG_DEBUG, fmt, ##__VA_ARGS__)
1017 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1018 #define LOG_AND_RETURN_ADD_KEY LOG_AND_RETURN("Temporary rule array too small, aborting event processing with %u items", rule_tmp.token_cur);
1020 static void add_rule(UdevRules
*rules
, char *line
,
1021 const char *filename
, unsigned filename_off
, unsigned lineno
) {
1024 struct rule_tmp rule_tmp
= {
1026 .rule
.type
= TK_RULE
,
1030 /* the offset in the rule is limited to unsigned short */
1031 if (filename_off
< USHRT_MAX
)
1032 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1033 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1039 enum operation_type op
;
1041 if (get_key(&linepos
, &key
, &op
, &value
) != 0) {
1042 /* Avoid erroring on trailing whitespace. This is probably rare
1043 * so save the work for the error case instead of always trying
1044 * to strip the trailing whitespace with strstrip(). */
1045 while (isblank(*linepos
))
1048 /* If we aren't at the end of the line, this is a parsing error.
1049 * Make a best effort to describe where the problem is. */
1050 if (!strchr(NEWLINE
, *linepos
)) {
1051 char buf
[2] = {*linepos
};
1052 _cleanup_free_
char *tmp
;
1055 LOG_RULE_ERROR("Invalid key/value pair, starting at character %tu ('%s')", linepos
- line
+ 1, tmp
);
1056 if (*linepos
== '#')
1057 LOG_RULE_ERROR("Hint: comments can only start at beginning of line");
1062 if (streq(key
, "ACTION")) {
1063 if (op
> OP_MATCH_MAX
)
1064 LOG_AND_RETURN("Invalid %s operation", key
);
1066 if (rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
) < 0)
1067 LOG_AND_RETURN_ADD_KEY
;
1069 } else if (streq(key
, "DEVPATH")) {
1070 if (op
> OP_MATCH_MAX
)
1071 LOG_AND_RETURN("Invalid %s operation", key
);
1073 if (rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
) < 0)
1074 LOG_AND_RETURN_ADD_KEY
;
1076 } else if (streq(key
, "KERNEL")) {
1077 if (op
> OP_MATCH_MAX
)
1078 LOG_AND_RETURN("Invalid %s operation", key
);
1080 if (rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
) < 0)
1081 LOG_AND_RETURN_ADD_KEY
;
1083 } else if (streq(key
, "SUBSYSTEM")) {
1084 if (op
> OP_MATCH_MAX
)
1085 LOG_AND_RETURN("Invalid %s operation", key
);
1087 /* bus, class, subsystem events should all be the same */
1088 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1089 if (!streq(value
, "subsystem"))
1090 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1092 r
= rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1094 r
= rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1096 LOG_AND_RETURN_ADD_KEY
;
1098 } else if (streq(key
, "DRIVER")) {
1099 if (op
> OP_MATCH_MAX
)
1100 LOG_AND_RETURN("Invalid %s operation", key
);
1102 if (rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
) < 0)
1103 LOG_AND_RETURN_ADD_KEY
;
1105 } else if (startswith(key
, "ATTR{")) {
1106 attr
= get_key_attribute(key
+ STRLEN("ATTR"));
1108 LOG_AND_RETURN("Failed to parse %s attribute", "ATTR");
1110 if (op
== OP_REMOVE
)
1111 LOG_AND_RETURN("Invalid %s operation", "ATTR");
1113 if (op
< OP_MATCH_MAX
)
1114 r
= rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1116 r
= rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1118 LOG_AND_RETURN_ADD_KEY
;
1120 } else if (startswith(key
, "SYSCTL{")) {
1121 attr
= get_key_attribute(key
+ STRLEN("SYSCTL"));
1123 LOG_AND_RETURN("Failed to parse %s attribute", "ATTR");
1125 if (op
== OP_REMOVE
)
1126 LOG_AND_RETURN("Invalid %s operation", "ATTR");
1128 if (op
< OP_MATCH_MAX
)
1129 r
= rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1131 r
= rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1133 LOG_AND_RETURN_ADD_KEY
;
1135 } else if (startswith(key
, "SECLABEL{")) {
1136 attr
= get_key_attribute(key
+ STRLEN("SECLABEL"));
1138 LOG_AND_RETURN("Failed to parse %s attribute", "SECLABEL");
1140 if (op
== OP_REMOVE
)
1141 LOG_AND_RETURN("Invalid %s operation", "SECLABEL");
1143 if (rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
) < 0)
1144 LOG_AND_RETURN_ADD_KEY
;
1146 } else if (streq(key
, "KERNELS")) {
1147 if (op
> OP_MATCH_MAX
)
1148 LOG_AND_RETURN("Invalid %s operation", key
);
1150 if (rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
) < 0)
1151 LOG_AND_RETURN_ADD_KEY
;
1153 } else if (streq(key
, "SUBSYSTEMS")) {
1154 if (op
> OP_MATCH_MAX
)
1155 LOG_AND_RETURN("Invalid %s operation", key
);
1157 if (rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
) < 0)
1158 LOG_AND_RETURN_ADD_KEY
;
1160 } else if (streq(key
, "DRIVERS")) {
1161 if (op
> OP_MATCH_MAX
)
1162 LOG_AND_RETURN("Invalid %s operation", key
);
1164 if (rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
) < 0)
1165 LOG_AND_RETURN_ADD_KEY
;
1167 } else if (startswith(key
, "ATTRS{")) {
1168 if (op
> OP_MATCH_MAX
)
1169 LOG_AND_RETURN("Invalid %s operation", "ATTRS");
1171 attr
= get_key_attribute(key
+ STRLEN("ATTRS"));
1173 LOG_AND_RETURN("Failed to parse %s attribute", "ATTRS");
1175 if (startswith(attr
, "device/"))
1176 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1177 if (strstr(attr
, "../"))
1178 LOG_RULE_WARNING("Direct reference to parent sysfs directory, may break in future kernels; please fix");
1179 if (rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
) < 0)
1180 LOG_AND_RETURN_ADD_KEY
;
1182 } else if (streq(key
, "TAGS")) {
1183 if (op
> OP_MATCH_MAX
)
1184 LOG_AND_RETURN("Invalid %s operation", key
);
1186 if (rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
) < 0)
1187 LOG_AND_RETURN_ADD_KEY
;
1189 } else if (startswith(key
, "ENV{")) {
1190 attr
= get_key_attribute(key
+ STRLEN("ENV"));
1192 LOG_AND_RETURN("Failed to parse %s attribute", "ENV");
1194 if (op
== OP_REMOVE
)
1195 LOG_AND_RETURN("Invalid %s operation", "ENV");
1197 if (op
< OP_MATCH_MAX
)
1198 r
= rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1200 if (STR_IN_SET(attr
,
1212 LOG_AND_RETURN("Invalid ENV attribute, '%s' cannot be set", attr
);
1214 r
= rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1217 LOG_AND_RETURN_ADD_KEY
;
1219 } else if (streq(key
, "TAG")) {
1220 if (op
< OP_MATCH_MAX
)
1221 r
= rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1223 r
= rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1225 LOG_AND_RETURN_ADD_KEY
;
1227 } else if (streq(key
, "PROGRAM")) {
1228 if (op
== OP_REMOVE
)
1229 LOG_AND_RETURN("Invalid %s operation", key
);
1231 if (rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
) < 0)
1232 LOG_AND_RETURN_ADD_KEY
;
1234 } else if (streq(key
, "RESULT")) {
1235 if (op
> OP_MATCH_MAX
)
1236 LOG_AND_RETURN("Invalid %s operation", key
);
1238 if (rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
) < 0)
1239 LOG_AND_RETURN_ADD_KEY
;
1241 } else if (startswith(key
, "IMPORT")) {
1242 attr
= get_key_attribute(key
+ STRLEN("IMPORT"));
1244 LOG_RULE_WARNING("Ignoring IMPORT{} with missing type");
1247 if (op
== OP_REMOVE
)
1248 LOG_AND_RETURN("Invalid %s operation", "IMPORT");
1250 if (streq(attr
, "program")) {
1251 /* find known built-in command */
1252 if (value
[0] != '/') {
1253 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1256 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1257 if (rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
) < 0)
1258 LOG_AND_RETURN_ADD_KEY
;
1262 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1263 } else if (streq(attr
, "builtin")) {
1264 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1267 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown, ignoring", value
);
1270 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1271 } else if (streq(attr
, "file"))
1272 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1273 else if (streq(attr
, "db"))
1274 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1275 else if (streq(attr
, "cmdline"))
1276 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1277 else if (streq(attr
, "parent"))
1278 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1280 LOG_RULE_ERROR("Ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1284 LOG_AND_RETURN_ADD_KEY
;
1286 } else if (startswith(key
, "TEST")) {
1289 if (op
> OP_MATCH_MAX
)
1290 LOG_AND_RETURN("Invalid %s operation", "TEST");
1292 attr
= get_key_attribute(key
+ STRLEN("TEST"));
1294 mode
= strtol(attr
, NULL
, 8);
1295 r
= rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1297 r
= rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1299 LOG_AND_RETURN_ADD_KEY
;
1301 } else if (startswith(key
, "RUN")) {
1302 attr
= get_key_attribute(key
+ STRLEN("RUN"));
1305 if (op
== OP_REMOVE
)
1306 LOG_AND_RETURN("Invalid %s operation", "RUN");
1308 if (streq(attr
, "builtin")) {
1309 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1312 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown, ignoring", value
);
1315 r
= rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1316 } else if (streq(attr
, "program")) {
1317 const enum udev_builtin_cmd cmd
= _UDEV_BUILTIN_MAX
;
1319 r
= rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1321 LOG_RULE_ERROR("Ignoring unknown %s{} type '%s'", "RUN", attr
);
1325 LOG_AND_RETURN_ADD_KEY
;
1327 } else if (streq(key
, "LABEL")) {
1328 if (op
== OP_REMOVE
)
1329 LOG_AND_RETURN("Invalid %s operation", key
);
1331 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1333 } else if (streq(key
, "GOTO")) {
1334 if (op
== OP_REMOVE
)
1335 LOG_AND_RETURN("Invalid %s operation", key
);
1337 if (rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
) < 0)
1338 LOG_AND_RETURN_ADD_KEY
;
1340 } else if (startswith(key
, "NAME")) {
1341 if (op
== OP_REMOVE
)
1342 LOG_AND_RETURN("Invalid %s operation", key
);
1344 if (op
< OP_MATCH_MAX
)
1345 r
= rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1347 if (streq(value
, "%k")) {
1348 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1351 if (isempty(value
)) {
1352 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1355 r
= rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1358 LOG_AND_RETURN_ADD_KEY
;
1359 rule_tmp
.rule
.rule
.can_set_name
= true;
1361 } else if (streq(key
, "SYMLINK")) {
1362 if (op
== OP_REMOVE
)
1363 LOG_AND_RETURN("Invalid %s operation", key
);
1365 if (op
< OP_MATCH_MAX
)
1366 r
= rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1368 r
= rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1370 LOG_AND_RETURN_ADD_KEY
;
1371 rule_tmp
.rule
.rule
.can_set_name
= true;
1373 } else if (streq(key
, "OWNER")) {
1377 if (op
== OP_REMOVE
)
1378 LOG_AND_RETURN("Invalid %s operation", key
);
1380 uid
= strtoul(value
, &endptr
, 10);
1381 if (endptr
[0] == '\0')
1382 r
= rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1383 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&& !strchr("$%", value
[0])) {
1384 uid
= add_uid(rules
, value
);
1385 r
= rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1386 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1387 r
= rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1389 LOG_RULE_ERROR("Invalid %s operation", key
);
1393 LOG_AND_RETURN_ADD_KEY
;
1395 rule_tmp
.rule
.rule
.can_set_name
= true;
1397 } else if (streq(key
, "GROUP")) {
1401 if (op
== OP_REMOVE
)
1402 LOG_AND_RETURN("Invalid %s operation", key
);
1404 gid
= strtoul(value
, &endptr
, 10);
1405 if (endptr
[0] == '\0')
1406 r
= rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1407 else if ((rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
) && !strchr("$%", value
[0])) {
1408 gid
= add_gid(rules
, value
);
1409 r
= rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1410 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1411 r
= rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1413 LOG_RULE_ERROR("Invalid %s operation", key
);
1417 LOG_AND_RETURN_ADD_KEY
;
1419 rule_tmp
.rule
.rule
.can_set_name
= true;
1421 } else if (streq(key
, "MODE")) {
1425 if (op
== OP_REMOVE
)
1426 LOG_AND_RETURN("Invalid %s operation", key
);
1428 mode
= strtol(value
, &endptr
, 8);
1429 if (endptr
[0] == '\0')
1430 r
= rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1432 r
= rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1434 LOG_AND_RETURN_ADD_KEY
;
1436 rule_tmp
.rule
.rule
.can_set_name
= true;
1438 } else if (streq(key
, "OPTIONS")) {
1441 if (op
== OP_REMOVE
)
1442 LOG_AND_RETURN("Invalid %s operation", key
);
1444 pos
= strstr(value
, "link_priority=");
1446 int prio
= atoi(pos
+ STRLEN("link_priority="));
1448 if (rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
) < 0)
1449 LOG_AND_RETURN_ADD_KEY
;
1452 pos
= strstr(value
, "string_escape=");
1454 pos
+= STRLEN("string_escape=");
1455 if (startswith(pos
, "none"))
1456 r
= rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1457 else if (startswith(pos
, "replace"))
1458 r
= rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1460 LOG_RULE_ERROR("OPTIONS: unknown string_escape mode '%s', ignoring", pos
);
1464 LOG_AND_RETURN_ADD_KEY
;
1467 pos
= strstr(value
, "db_persist");
1469 if (rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
) < 0)
1470 LOG_AND_RETURN_ADD_KEY
;
1472 pos
= strstr(value
, "nowatch");
1474 static const int zero
= 0;
1475 if (rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &zero
) < 0)
1476 LOG_AND_RETURN_ADD_KEY
;
1478 static const int one
= 1;
1479 pos
= strstr(value
, "watch");
1481 if (rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &one
) < 0)
1482 LOG_AND_RETURN_ADD_KEY
;
1485 pos
= strstr(value
, "static_node=");
1487 pos
+= STRLEN("static_node=");
1488 if (rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
) < 0)
1489 LOG_AND_RETURN_ADD_KEY
;
1490 rule_tmp
.rule
.rule
.has_static_node
= true;
1494 LOG_AND_RETURN("Unknown key '%s'", key
);
1497 /* add rule token and sort tokens */
1498 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1499 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1500 LOG_RULE_ERROR("Failed to add rule token");
1503 static int parse_file(UdevRules
*rules
, const char *filename
) {
1504 _cleanup_fclose_
FILE *f
= NULL
;
1505 unsigned first_token
;
1506 unsigned filename_off
;
1507 char line
[UTIL_LINE_SIZE
];
1511 f
= fopen(filename
, "re");
1513 if (errno
== ENOENT
)
1519 if (null_or_empty_fd(fileno(f
))) {
1520 log_debug("Skipping empty file: %s", filename
);
1523 log_debug("Reading rules file: %s", filename
);
1525 first_token
= rules
->token_cur
;
1526 filename_off
= rules_add_string(rules
, filename
);
1528 while (fgets(line
, sizeof(line
), f
)) {
1532 /* skip whitespace */
1535 while (isspace(key
[0]))
1546 /* continue reading if backslash+newline is found */
1547 while (line
[len
-2] == '\\') {
1548 if (!fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
))
1550 if (strlen(&line
[len
-2]) < 2)
1556 if (len
+1 >= sizeof(line
)) {
1557 log_error("Line too long '%s':%u, ignored", filename
, line_nr
);
1560 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1563 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1564 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1565 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1566 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1569 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1570 if (rules
->tokens
[j
].type
!= TK_RULE
)
1572 if (rules
->tokens
[j
].rule
.label_off
== 0)
1574 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1576 rules
->tokens
[i
].key
.rule_goto
= j
;
1579 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1580 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1586 int udev_rules_new(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1587 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1588 _cleanup_strv_free_
char **files
= NULL
;
1592 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1594 rules
= new(UdevRules
, 1);
1598 *rules
= (UdevRules
) {
1599 .resolve_name_timing
= resolve_name_timing
,
1602 /* init token array and string buffer */
1603 rules
->tokens
= malloc_multiply(PREALLOC_TOKEN
, sizeof(struct token
));
1606 rules
->token_max
= PREALLOC_TOKEN
;
1608 rules
->strbuf
= strbuf_new();
1612 udev_rules_check_timestamp(rules
);
1614 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1616 return log_error_errno(r
, "Failed to enumerate rules files: %m");
1619 * The offset value in the rules strct is limited; add all
1620 * rules file names to the beginning of the string buffer.
1622 STRV_FOREACH(f
, files
)
1623 rules_add_string(rules
, *f
);
1625 STRV_FOREACH(f
, files
)
1626 parse_file(rules
, *f
);
1628 struct token end_token
= { .type
= TK_END
};
1629 add_token(rules
, &end_token
);
1630 log_debug("Rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1631 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1633 /* cleanup temporary strbuf data */
1634 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1635 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1636 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1637 strbuf_complete(rules
->strbuf
);
1639 /* cleanup uid/gid cache */
1640 rules
->uids
= mfree(rules
->uids
);
1641 rules
->uids_cur
= 0;
1642 rules
->uids_max
= 0;
1643 rules
->gids
= mfree(rules
->gids
);
1644 rules
->gids_cur
= 0;
1645 rules
->gids_max
= 0;
1648 *ret_rules
= TAKE_PTR(rules
);
1652 UdevRules
*udev_rules_free(UdevRules
*rules
) {
1655 free(rules
->tokens
);
1656 strbuf_cleanup(rules
->strbuf
);
1659 return mfree(rules
);
1662 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1666 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1669 static int match_key(UdevRules
*rules
, struct token
*token
, const char *val
) {
1670 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1677 switch (token
->key
.glob
) {
1679 match
= (streq(key_value
, val
));
1682 match
= (fnmatch(key_value
, val
, 0) == 0);
1689 s
= rules_str(rules
, token
->key
.value_off
);
1694 next
= strchr(s
, '|');
1696 size_t matchlen
= (size_t)(next
- s
);
1698 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1702 match
= (streq(s
, val
));
1711 char value
[UTIL_PATH_SIZE
];
1713 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1716 pos
= strchr(key_value
, '|');
1721 match
= (fnmatch(key_value
, val
, 0) == 0);
1729 match
= (val
[0] != '\0');
1735 if (match
&& (token
->key
.op
== OP_MATCH
))
1737 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1742 static int match_attr(UdevRules
*rules
, sd_device
*dev
, UdevEvent
*event
, struct token
*cur
) {
1743 char nbuf
[UTIL_NAME_SIZE
], vbuf
[UTIL_NAME_SIZE
];
1744 const char *name
, *value
;
1747 name
= rules_str(rules
, cur
->key
.attr_off
);
1748 switch (cur
->key
.attrsubst
) {
1750 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1754 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1758 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) != 0)
1766 /* remove trailing whitespace, if not asked to match for it */
1767 len
= strlen(value
);
1768 if (len
> 0 && isspace(value
[len
-1])) {
1769 const char *key_value
;
1772 key_value
= rules_str(rules
, cur
->key
.value_off
);
1773 klen
= strlen(key_value
);
1774 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1775 if (value
!= vbuf
) {
1776 strscpy(vbuf
, sizeof(vbuf
), value
);
1779 while (len
> 0 && isspace(vbuf
[--len
]))
1784 return match_key(rules
, cur
, value
);
1793 int udev_rules_apply_to_event(
1796 usec_t timeout_usec
,
1797 Hashmap
*properties_list
) {
1798 sd_device
*dev
= event
->dev
;
1799 enum escape_type esc
= ESCAPE_UNSET
;
1800 struct token
*cur
, *rule
;
1801 const char *action
, *val
;
1808 r
= sd_device_get_property_value(dev
, "ACTION", &action
);
1812 can_set_name
= (!streq(action
, "remove") &&
1813 (sd_device_get_devnum(dev
, NULL
) >= 0 ||
1814 sd_device_get_ifindex(dev
, NULL
) >= 0));
1816 /* loop through token list, match, run actions or forward to next rule */
1817 cur
= &rules
->tokens
[0];
1820 dump_token(rules
, cur
);
1821 switch (cur
->type
) {
1825 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1826 if (!can_set_name
&& rule
->rule
.can_set_name
)
1831 if (match_key(rules
, cur
, action
) != 0)
1835 if (sd_device_get_devpath(dev
, &val
) < 0)
1837 if (match_key(rules
, cur
, val
) != 0)
1841 if (sd_device_get_sysname(dev
, &val
) < 0)
1843 if (match_key(rules
, cur
, val
) != 0)
1846 case TK_M_DEVLINK
: {
1847 const char *devlink
;
1850 FOREACH_DEVICE_DEVLINK(dev
, devlink
)
1851 if (match_key(rules
, cur
, devlink
+ STRLEN("/dev/")) == 0) {
1861 if (match_key(rules
, cur
, event
->name
) != 0)
1865 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1867 if (sd_device_get_property_value(dev
, key_name
, &val
) < 0) {
1868 /* check global properties */
1869 if (properties_list
)
1870 val
= hashmap_get(properties_list
, key_name
);
1875 if (match_key(rules
, cur
, strempty(val
)))
1883 FOREACH_DEVICE_TAG(dev
, tag
)
1884 if (streq(rules_str(rules
, cur
->key
.value_off
), tag
)) {
1889 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1890 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1894 case TK_M_SUBSYSTEM
:
1895 if (sd_device_get_subsystem(dev
, &val
) < 0)
1897 if (match_key(rules
, cur
, val
) != 0)
1901 if (sd_device_get_driver(dev
, &val
) < 0)
1903 if (match_key(rules
, cur
, val
) != 0)
1907 if (match_attr(rules
, dev
, event
, cur
) != 0)
1911 char filename
[UTIL_PATH_SIZE
];
1912 _cleanup_free_
char *value
= NULL
;
1915 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1916 sysctl_normalize(filename
);
1917 if (sysctl_read(filename
, &value
) < 0)
1920 len
= strlen(value
);
1921 while (len
> 0 && isspace(value
[--len
]))
1923 if (match_key(rules
, cur
, value
) != 0)
1928 case TK_M_SUBSYSTEMS
:
1934 /* get whole sequence of parent matches */
1936 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1939 /* loop over parents */
1940 event
->dev_parent
= dev
;
1944 /* loop over sequence of parent match keys */
1945 for (key
= cur
; key
< next
; key
++ ) {
1946 dump_token(rules
, key
);
1949 if (sd_device_get_sysname(event
->dev_parent
, &val
) < 0)
1951 if (match_key(rules
, key
, val
) != 0)
1954 case TK_M_SUBSYSTEMS
:
1955 if (sd_device_get_subsystem(event
->dev_parent
, &val
) < 0)
1957 if (match_key(rules
, key
, val
) != 0)
1961 if (sd_device_get_driver(event
->dev_parent
, &val
) < 0)
1963 if (match_key(rules
, key
, val
) != 0)
1967 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1971 bool match
= sd_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1973 if (match
&& key
->key
.op
== OP_NOMATCH
)
1975 if (!match
&& key
->key
.op
== OP_MATCH
)
1986 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
1987 event
->dev_parent
= NULL
;
1991 /* move behind our sequence of parent match keys */
1996 char filename
[UTIL_PATH_SIZE
];
1997 struct stat statbuf
;
2000 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
2001 if (util_resolve_subsys_kernel(filename
, filename
, sizeof(filename
), false) != 0) {
2002 if (filename
[0] != '/') {
2003 char tmp
[UTIL_PATH_SIZE
];
2005 if (sd_device_get_syspath(dev
, &val
) < 0)
2008 strscpy(tmp
, sizeof(tmp
), filename
);
2009 strscpyl(filename
, sizeof(filename
), val
, "/", tmp
, NULL
);
2012 attr_subst_subdir(filename
, sizeof(filename
));
2014 match
= (stat(filename
, &statbuf
) == 0);
2015 if (match
&& cur
->key
.mode
> 0)
2016 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2017 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2019 if (!match
&& cur
->key
.op
== OP_MATCH
)
2023 case TK_M_PROGRAM
: {
2024 char program
[UTIL_PATH_SIZE
], result
[UTIL_LINE_SIZE
];
2026 event
->program_result
= mfree(event
->program_result
);
2027 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
2028 log_device_debug(dev
, "PROGRAM '%s' %s:%u",
2030 rules_str(rules
, rule
->rule
.filename_off
),
2031 rule
->rule
.filename_line
);
2033 if (udev_event_spawn(event
, timeout_usec
, true, program
, result
, sizeof(result
)) != 0) {
2034 if (cur
->key
.op
!= OP_NOMATCH
)
2039 delete_trailing_chars(result
, "\n");
2040 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2041 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2043 log_device_debug(dev
, "Replaced %i character(s) from result of '%s'" , count
, program
);
2045 event
->program_result
= strdup(result
);
2046 if (cur
->key
.op
== OP_NOMATCH
)
2051 case TK_M_IMPORT_FILE
: {
2052 char import
[UTIL_PATH_SIZE
];
2054 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2055 if (import_file_into_properties(dev
, import
) != 0)
2056 if (cur
->key
.op
!= OP_NOMATCH
)
2060 case TK_M_IMPORT_PROG
: {
2061 char import
[UTIL_PATH_SIZE
];
2063 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2064 log_device_debug(dev
, "IMPORT '%s' %s:%u",
2066 rules_str(rules
, rule
->rule
.filename_off
),
2067 rule
->rule
.filename_line
);
2069 if (import_program_into_properties(event
, timeout_usec
, import
) != 0)
2070 if (cur
->key
.op
!= OP_NOMATCH
)
2074 case TK_M_IMPORT_BUILTIN
: {
2075 char command
[UTIL_PATH_SIZE
];
2077 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2078 /* check if we ran already */
2079 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2080 log_device_debug(dev
, "IMPORT builtin skip '%s' %s:%u",
2081 udev_builtin_name(cur
->key
.builtin_cmd
),
2082 rules_str(rules
, rule
->rule
.filename_off
),
2083 rule
->rule
.filename_line
);
2084 /* return the result from earlier run */
2085 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2086 if (cur
->key
.op
!= OP_NOMATCH
)
2091 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2094 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2095 log_device_debug(dev
, "IMPORT builtin '%s' %s:%u",
2096 udev_builtin_name(cur
->key
.builtin_cmd
),
2097 rules_str(rules
, rule
->rule
.filename_off
),
2098 rule
->rule
.filename_line
);
2100 r
= udev_builtin_run(dev
, cur
->key
.builtin_cmd
, command
, false);
2102 /* remember failure */
2103 log_device_debug_errno(dev
, r
, "IMPORT builtin '%s' fails: %m",
2104 udev_builtin_name(cur
->key
.builtin_cmd
));
2105 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2106 if (cur
->key
.op
!= OP_NOMATCH
)
2111 case TK_M_IMPORT_DB
: {
2114 key
= rules_str(rules
, cur
->key
.value_off
);
2115 if (event
->dev_db_clone
&&
2116 sd_device_get_property_value(event
->dev_db_clone
, key
, &val
) >= 0)
2117 device_add_property(dev
, key
, val
);
2118 else if (cur
->key
.op
!= OP_NOMATCH
)
2122 case TK_M_IMPORT_CMDLINE
: {
2123 _cleanup_free_
char *value
= NULL
;
2124 bool imported
= false;
2127 key
= rules_str(rules
, cur
->key
.value_off
);
2128 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2130 log_device_debug_errno(dev
, r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2135 device_add_property(dev
, key
, value
);
2137 /* we import simple flags as 'FLAG=1' */
2138 device_add_property(dev
, key
, "1");
2141 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2145 case TK_M_IMPORT_PARENT
: {
2146 char import
[UTIL_PATH_SIZE
];
2148 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2149 if (import_parent_into_properties(dev
, import
) != 0)
2150 if (cur
->key
.op
!= OP_NOMATCH
)
2155 if (match_key(rules
, cur
, event
->program_result
) != 0)
2158 case TK_A_STRING_ESCAPE_NONE
:
2161 case TK_A_STRING_ESCAPE_REPLACE
:
2162 esc
= ESCAPE_REPLACE
;
2164 case TK_A_DB_PERSIST
:
2165 device_set_db_persist(dev
);
2167 case TK_A_INOTIFY_WATCH
:
2168 if (event
->inotify_watch_final
)
2170 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2171 event
->inotify_watch_final
= true;
2172 event
->inotify_watch
= cur
->key
.watch
;
2174 case TK_A_DEVLINK_PRIO
:
2175 device_set_devlink_priority(dev
, cur
->key
.devlink_prio
);
2178 char owner
[UTIL_NAME_SIZE
];
2179 const char *ow
= owner
;
2181 if (event
->owner_final
)
2183 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2184 event
->owner_final
= true;
2185 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2186 event
->owner_set
= true;
2187 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2189 log_unknown_owner(dev
, r
, "user", owner
);
2192 log_device_debug(dev
, "OWNER %u %s:%u",
2194 rules_str(rules
, rule
->rule
.filename_off
),
2195 rule
->rule
.filename_line
);
2199 char group
[UTIL_NAME_SIZE
];
2200 const char *gr
= group
;
2202 if (event
->group_final
)
2204 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2205 event
->group_final
= true;
2206 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2207 event
->group_set
= true;
2208 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2210 log_unknown_owner(dev
, r
, "group", group
);
2213 log_device_debug(dev
, "GROUP %u %s:%u",
2215 rules_str(rules
, rule
->rule
.filename_off
),
2216 rule
->rule
.filename_line
);
2220 char mode_str
[UTIL_NAME_SIZE
];
2223 if (event
->mode_final
)
2225 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2226 r
= parse_mode(mode_str
, &mode
);
2228 log_device_error_errno(dev
, r
, "Failed to parse mode '%s': %m", mode_str
);
2231 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2232 event
->mode_final
= true;
2233 event
->mode_set
= true;
2235 log_device_debug(dev
, "MODE %#o %s:%u",
2237 rules_str(rules
, rule
->rule
.filename_off
),
2238 rule
->rule
.filename_line
);
2242 if (event
->owner_final
)
2244 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2245 event
->owner_final
= true;
2246 event
->owner_set
= true;
2247 event
->uid
= cur
->key
.uid
;
2248 log_device_debug(dev
, "OWNER %u %s:%u",
2250 rules_str(rules
, rule
->rule
.filename_off
),
2251 rule
->rule
.filename_line
);
2254 if (event
->group_final
)
2256 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2257 event
->group_final
= true;
2258 event
->group_set
= true;
2259 event
->gid
= cur
->key
.gid
;
2260 log_device_debug(dev
, "GROUP %u %s:%u",
2262 rules_str(rules
, rule
->rule
.filename_off
),
2263 rule
->rule
.filename_line
);
2266 if (event
->mode_final
)
2268 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2269 event
->mode_final
= true;
2270 event
->mode_set
= true;
2271 event
->mode
= cur
->key
.mode
;
2272 log_device_debug(dev
, "MODE %#o %s:%u",
2274 rules_str(rules
, rule
->rule
.filename_off
),
2275 rule
->rule
.filename_line
);
2277 case TK_A_SECLABEL
: {
2278 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2279 char label_str
[UTIL_LINE_SIZE
] = {};
2281 name
= strdup(rules_str(rules
, cur
->key
.attr_off
));
2285 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2286 if (!isempty(label_str
))
2287 label
= strdup(label_str
);
2289 label
= strdup(rules_str(rules
, cur
->key
.value_off
));
2293 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2294 hashmap_clear_free_free(event
->seclabel_list
);
2296 r
= hashmap_ensure_allocated(&event
->seclabel_list
, NULL
);
2300 r
= hashmap_put(event
->seclabel_list
, name
, label
);
2303 log_device_debug(dev
, "SECLABEL{%s}='%s' %s:%u",
2305 rules_str(rules
, rule
->rule
.filename_off
),
2306 rule
->rule
.filename_line
);
2307 name
= label
= NULL
;
2312 char value_new
[UTIL_NAME_SIZE
];
2313 const char *name
, *value_old
;
2315 name
= rules_str(rules
, cur
->key
.attr_off
);
2316 val
= rules_str(rules
, cur
->key
.value_off
);
2317 if (val
[0] == '\0') {
2318 if (cur
->key
.op
== OP_ADD
)
2320 device_add_property(dev
, name
, NULL
);
2324 if (cur
->key
.op
== OP_ADD
&&
2325 sd_device_get_property_value(dev
, name
, &value_old
) >= 0) {
2326 char temp
[UTIL_NAME_SIZE
];
2328 /* append value separated by space */
2329 udev_event_apply_format(event
, val
, temp
, sizeof(temp
), false);
2330 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2332 udev_event_apply_format(event
, val
, value_new
, sizeof(value_new
), false);
2334 device_add_property(dev
, name
, value_new
);
2338 char tag
[UTIL_PATH_SIZE
];
2341 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2342 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2343 device_cleanup_tags(dev
);
2344 for (p
= tag
; *p
!= '\0'; p
++) {
2345 if ((*p
>= 'a' && *p
<= 'z') ||
2346 (*p
>= 'A' && *p
<= 'Z') ||
2347 (*p
>= '0' && *p
<= '9') ||
2348 IN_SET(*p
, '-', '_'))
2350 log_device_error(dev
, "Ignoring invalid tag name '%s'", tag
);
2353 if (cur
->key
.op
== OP_REMOVE
)
2354 device_remove_tag(dev
, tag
);
2356 device_add_tag(dev
, tag
);
2360 char name_str
[UTIL_PATH_SIZE
];
2364 name
= rules_str(rules
, cur
->key
.value_off
);
2365 if (event
->name_final
)
2367 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2368 event
->name_final
= true;
2369 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2370 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2371 count
= util_replace_chars(name_str
, "/");
2373 log_device_debug(dev
, "Replaced %i character(s) from result of NAME=\"%s\"", count
, name
);
2375 if (sd_device_get_devnum(dev
, NULL
) >= 0 &&
2376 (sd_device_get_devname(dev
, &val
) < 0 ||
2377 !streq(name_str
, val
+ STRLEN("/dev/")))) {
2378 log_device_error(dev
, "Kernel device nodes cannot be renamed, ignoring NAME=\"%s\"; please fix it in %s:%u\n",
2380 rules_str(rules
, rule
->rule
.filename_off
),
2381 rule
->rule
.filename_line
);
2384 if (free_and_strdup(&event
->name
, name_str
) < 0)
2387 log_device_debug(dev
, "NAME '%s' %s:%u",
2389 rules_str(rules
, rule
->rule
.filename_off
),
2390 rule
->rule
.filename_line
);
2393 case TK_A_DEVLINK
: {
2394 char temp
[UTIL_PATH_SIZE
], filename
[UTIL_PATH_SIZE
], *pos
, *next
;
2397 if (event
->devlink_final
)
2399 if (sd_device_get_devnum(dev
, NULL
) < 0)
2401 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2402 event
->devlink_final
= true;
2403 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2404 device_cleanup_devlinks(dev
);
2406 /* allow multiple symlinks separated by spaces */
2407 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2408 if (esc
== ESCAPE_UNSET
)
2409 count
= util_replace_chars(temp
, "/ ");
2410 else if (esc
== ESCAPE_REPLACE
)
2411 count
= util_replace_chars(temp
, "/");
2413 log_device_debug(dev
, "Replaced %i character(s) from result of LINK" , count
);
2415 while (isspace(pos
[0]))
2417 next
= strchr(pos
, ' ');
2420 log_device_debug(dev
, "LINK '%s' %s:%u", pos
,
2421 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2422 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2423 device_add_devlink(dev
, filename
);
2424 while (isspace(next
[1]))
2427 next
= strchr(pos
, ' ');
2429 if (pos
[0] != '\0') {
2430 log_device_debug(dev
, "LINK '%s' %s:%u", pos
,
2431 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2432 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2433 device_add_devlink(dev
, filename
);
2438 char attr
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2439 _cleanup_fclose_
FILE *f
= NULL
;
2440 const char *key_name
;
2442 key_name
= rules_str(rules
, cur
->key
.attr_off
);
2443 if (util_resolve_subsys_kernel(key_name
, attr
, sizeof(attr
), false) != 0 &&
2444 sd_device_get_syspath(dev
, &val
) >= 0)
2445 strscpyl(attr
, sizeof(attr
), val
, "/", key_name
, NULL
);
2446 attr_subst_subdir(attr
, sizeof(attr
));
2448 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2449 log_device_debug(dev
, "ATTR '%s' writing '%s' %s:%u", attr
, value
,
2450 rules_str(rules
, rule
->rule
.filename_off
),
2451 rule
->rule
.filename_line
);
2452 f
= fopen(attr
, "we");
2454 log_device_error_errno(dev
, errno
, "Failed to open ATTR{%s} for writing: %m", attr
);
2455 else if (fprintf(f
, "%s", value
) <= 0)
2456 log_device_error_errno(dev
, errno
, "Failed to write ATTR{%s}: %m", attr
);
2460 char filename
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2462 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2463 sysctl_normalize(filename
);
2464 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2465 log_device_debug(dev
, "SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2466 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2467 r
= sysctl_write(filename
, value
);
2469 log_device_error_errno(dev
, r
, "Failed to write SYSCTL{%s}='%s': %m", filename
, value
);
2472 case TK_A_RUN_BUILTIN
:
2473 case TK_A_RUN_PROGRAM
: {
2474 _cleanup_free_
char *cmd
= NULL
;
2476 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2477 hashmap_clear_free_key(event
->run_list
);
2479 r
= hashmap_ensure_allocated(&event
->run_list
, NULL
);
2483 cmd
= strdup(rules_str(rules
, cur
->key
.value_off
));
2487 r
= hashmap_put(event
->run_list
, cmd
, INT_TO_PTR(cur
->key
.builtin_cmd
));
2493 log_device_debug(dev
, "RUN '%s' %s:%u",
2494 rules_str(rules
, cur
->key
.value_off
),
2495 rules_str(rules
, rule
->rule
.filename_off
),
2496 rule
->rule
.filename_line
);
2500 if (cur
->key
.rule_goto
== 0)
2502 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2507 case TK_M_PARENTS_MIN
:
2508 case TK_M_PARENTS_MAX
:
2511 log_device_error(dev
, "Wrong type %u", cur
->type
);
2518 /* fast-forward to next rule */
2519 cur
= rule
+ rule
->rule
.token_count
;
2525 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2531 _cleanup_strv_free_
char **tags
= NULL
;
2534 _cleanup_free_
char *path
= NULL
;
2540 cur
= &rules
->tokens
[0];
2543 switch (cur
->type
) {
2548 /* skip rules without a static_node tag */
2549 if (!rule
->rule
.has_static_node
)
2555 tags
= strv_free(tags
);
2564 mode
= cur
->key
.mode
;
2567 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2572 case TK_A_STATIC_NODE
: {
2573 char device_node
[UTIL_PATH_SIZE
];
2574 char tags_dir
[UTIL_PATH_SIZE
];
2575 char tag_symlink
[UTIL_PATH_SIZE
];
2578 /* we assure, that the permissions tokens are sorted before the static token */
2580 if (mode
== 0 && uid
== 0 && gid
== 0 && !tags
)
2583 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2584 if (stat(device_node
, &stats
) != 0)
2586 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2589 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2591 STRV_FOREACH(t
, tags
) {
2592 _cleanup_free_
char *unescaped_filename
= NULL
;
2594 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2595 r
= mkdir_p(tags_dir
, 0755);
2597 return log_error_errno(r
, "Failed to create %s: %m", tags_dir
);
2599 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2601 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2602 r
= symlink(device_node
, tag_symlink
);
2603 if (r
< 0 && errno
!= EEXIST
)
2604 return log_error_errno(errno
, "Failed to create symlink %s -> %s: %m",
2605 tag_symlink
, device_node
);
2609 /* don't touch the permissions if only the tags were set */
2610 if (mode
== 0 && uid
== 0 && gid
== 0)
2619 if (mode
!= (stats
.st_mode
& 01777)) {
2620 r
= chmod(device_node
, mode
);
2622 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2625 log_debug("chmod '%s' %#o", device_node
, mode
);
2628 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2629 r
= chown(device_node
, uid
, gid
);
2631 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2632 device_node
, uid
, gid
);
2634 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2637 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2647 /* fast-forward to next rule */
2648 cur
= rule
+ rule
->rule
.token_count
;
2655 fchmod(fileno(f
), 0644);
2656 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2657 unlink_noerrno("/run/udev/static_node-tags");
2658 unlink_noerrno(path
);