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 inline void dump_token(UdevRules
*rules
, struct token
*token
) {}
445 static inline 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 void 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 assert(rule_tmp
->token_cur
< ELEMENTSOF(rule_tmp
->token
));
843 memzero(token
, sizeof(struct token
));
855 case TK_M_SUBSYSTEMS
:
859 case TK_M_IMPORT_FILE
:
860 case TK_M_IMPORT_PROG
:
862 case TK_M_IMPORT_CMDLINE
:
863 case TK_M_IMPORT_PARENT
:
873 case TK_A_STATIC_NODE
:
874 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
876 case TK_M_IMPORT_BUILTIN
:
877 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
878 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
889 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
890 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
893 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
895 token
->key
.mode
= *(mode_t
*)data
;
897 case TK_A_STRING_ESCAPE_NONE
:
898 case TK_A_STRING_ESCAPE_REPLACE
:
899 case TK_A_DB_PERSIST
:
901 case TK_A_RUN_BUILTIN
:
902 case TK_A_RUN_PROGRAM
:
903 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
904 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
906 case TK_A_INOTIFY_WATCH
:
907 case TK_A_DEVLINK_PRIO
:
908 token
->key
.devlink_prio
= *(int *)data
;
911 token
->key
.uid
= *(uid_t
*)data
;
914 token
->key
.gid
= *(gid_t
*)data
;
917 token
->key
.mode
= *(mode_t
*)data
;
920 case TK_M_PARENTS_MIN
:
921 case TK_M_PARENTS_MAX
:
925 assert_not_reached("wrong type");
928 if (value
&& type
< TK_M_MAX
) {
929 /* check if we need to split or call fnmatch() while matching rules */
930 enum string_glob_type glob
;
931 bool has_split
, has_glob
;
933 has_split
= strchr(value
, '|');
934 has_glob
= string_is_glob(value
);
935 if (has_split
&& has_glob
)
936 glob
= GL_SPLIT_GLOB
;
940 if (streq(value
, "?*"))
947 token
->key
.glob
= glob
;
950 if (value
&& type
> TK_M_MAX
) {
951 /* check if assigned value has substitution chars */
953 token
->key
.subst
= SB_SUBSYS
;
954 else if (strchr(value
, '%') || strchr(value
, '$'))
955 token
->key
.subst
= SB_FORMAT
;
957 token
->key
.subst
= SB_NONE
;
961 /* check if property/attribute name has substitution chars */
963 token
->key
.attrsubst
= SB_SUBSYS
;
964 else if (strchr(attr
, '%') || strchr(attr
, '$'))
965 token
->key
.attrsubst
= SB_FORMAT
;
967 token
->key
.attrsubst
= SB_NONE
;
970 token
->key
.type
= type
;
972 rule_tmp
->token_cur
++;
975 static int sort_token(UdevRules
*rules
, struct rule_tmp
*rule_tmp
) {
978 unsigned end
= rule_tmp
->token_cur
;
980 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
981 enum token_type next_val
= TK_UNSET
;
982 unsigned next_idx
= 0;
985 /* find smallest value */
986 for (j
= start
; j
< end
; j
++) {
987 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
989 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
990 next_val
= rule_tmp
->token
[j
].type
;
995 /* add token and mark done */
996 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
998 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1001 if (next_idx
== start
)
1003 if (next_idx
+1 == end
)
1009 #define LOG_RULE_FULL(level, fmt, ...) log_full(level, "%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1010 #define LOG_RULE_ERROR(fmt, ...) LOG_RULE_FULL(LOG_ERR, fmt, ##__VA_ARGS__)
1011 #define LOG_RULE_WARNING(fmt, ...) LOG_RULE_FULL(LOG_WARNING, fmt, ##__VA_ARGS__)
1012 #define LOG_RULE_DEBUG(fmt, ...) LOG_RULE_FULL(LOG_DEBUG, fmt, ##__VA_ARGS__)
1013 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1015 static void add_rule(UdevRules
*rules
, char *line
,
1016 const char *filename
, unsigned filename_off
, unsigned lineno
) {
1019 struct rule_tmp rule_tmp
= {
1021 .rule
.type
= TK_RULE
,
1024 /* the offset in the rule is limited to unsigned short */
1025 if (filename_off
< USHRT_MAX
)
1026 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1027 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1033 enum operation_type op
;
1035 if (get_key(&linepos
, &key
, &op
, &value
) != 0) {
1036 /* Avoid erroring on trailing whitespace. This is probably rare
1037 * so save the work for the error case instead of always trying
1038 * to strip the trailing whitespace with strstrip(). */
1039 while (isblank(*linepos
))
1042 /* If we aren't at the end of the line, this is a parsing error.
1043 * Make a best effort to describe where the problem is. */
1044 if (!strchr(NEWLINE
, *linepos
)) {
1045 char buf
[2] = {*linepos
};
1046 _cleanup_free_
char *tmp
;
1049 LOG_RULE_ERROR("Invalid key/value pair, starting at character %tu ('%s')", linepos
- line
+ 1, tmp
);
1050 if (*linepos
== '#')
1051 LOG_RULE_ERROR("Hint: comments can only start at beginning of line");
1056 if (rule_tmp
.token_cur
>= ELEMENTSOF(rule_tmp
.token
))
1057 LOG_AND_RETURN("Temporary rule array too small, aborting event processing with %u items", rule_tmp
.token_cur
);
1059 if (streq(key
, "ACTION")) {
1060 if (op
> OP_MATCH_MAX
)
1061 LOG_AND_RETURN("Invalid %s operation", key
);
1063 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1065 } else if (streq(key
, "DEVPATH")) {
1066 if (op
> OP_MATCH_MAX
)
1067 LOG_AND_RETURN("Invalid %s operation", key
);
1069 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1071 } else if (streq(key
, "KERNEL")) {
1072 if (op
> OP_MATCH_MAX
)
1073 LOG_AND_RETURN("Invalid %s operation", key
);
1075 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1077 } else if (streq(key
, "SUBSYSTEM")) {
1078 if (op
> OP_MATCH_MAX
)
1079 LOG_AND_RETURN("Invalid %s operation", key
);
1081 /* bus, class, subsystem events should all be the same */
1082 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1083 if (!streq(value
, "subsystem"))
1084 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1086 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1088 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1090 } else if (streq(key
, "DRIVER")) {
1091 if (op
> OP_MATCH_MAX
)
1092 LOG_AND_RETURN("Invalid %s operation", key
);
1094 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1096 } else if (startswith(key
, "ATTR{")) {
1097 attr
= get_key_attribute(key
+ STRLEN("ATTR"));
1099 LOG_AND_RETURN("Failed to parse %s attribute", "ATTR");
1101 if (op
== OP_REMOVE
)
1102 LOG_AND_RETURN("Invalid %s operation", "ATTR");
1104 if (op
< OP_MATCH_MAX
)
1105 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1107 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1109 } else if (startswith(key
, "SYSCTL{")) {
1110 attr
= get_key_attribute(key
+ STRLEN("SYSCTL"));
1112 LOG_AND_RETURN("Failed to parse %s attribute", "ATTR");
1114 if (op
== OP_REMOVE
)
1115 LOG_AND_RETURN("Invalid %s operation", "ATTR");
1117 if (op
< OP_MATCH_MAX
)
1118 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1120 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1122 } else if (startswith(key
, "SECLABEL{")) {
1123 attr
= get_key_attribute(key
+ STRLEN("SECLABEL"));
1125 LOG_AND_RETURN("Failed to parse %s attribute", "SECLABEL");
1127 if (op
== OP_REMOVE
)
1128 LOG_AND_RETURN("Invalid %s operation", "SECLABEL");
1130 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1132 } else if (streq(key
, "KERNELS")) {
1133 if (op
> OP_MATCH_MAX
)
1134 LOG_AND_RETURN("Invalid %s operation", key
);
1136 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1138 } else if (streq(key
, "SUBSYSTEMS")) {
1139 if (op
> OP_MATCH_MAX
)
1140 LOG_AND_RETURN("Invalid %s operation", key
);
1142 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1144 } else if (streq(key
, "DRIVERS")) {
1145 if (op
> OP_MATCH_MAX
)
1146 LOG_AND_RETURN("Invalid %s operation", key
);
1148 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1150 } else if (startswith(key
, "ATTRS{")) {
1151 if (op
> OP_MATCH_MAX
)
1152 LOG_AND_RETURN("Invalid %s operation", "ATTRS");
1154 attr
= get_key_attribute(key
+ STRLEN("ATTRS"));
1156 LOG_AND_RETURN("Failed to parse %s attribute", "ATTRS");
1158 if (startswith(attr
, "device/"))
1159 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1160 if (strstr(attr
, "../"))
1161 LOG_RULE_WARNING("Direct reference to parent sysfs directory, may break in future kernels; please fix");
1162 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1164 } else if (streq(key
, "TAGS")) {
1165 if (op
> OP_MATCH_MAX
)
1166 LOG_AND_RETURN("Invalid %s operation", key
);
1168 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1170 } else if (startswith(key
, "ENV{")) {
1171 attr
= get_key_attribute(key
+ STRLEN("ENV"));
1173 LOG_AND_RETURN("Failed to parse %s attribute", "ENV");
1175 if (op
== OP_REMOVE
)
1176 LOG_AND_RETURN("Invalid %s operation", "ENV");
1178 if (op
< OP_MATCH_MAX
)
1179 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1181 if (STR_IN_SET(attr
,
1193 LOG_AND_RETURN("Invalid ENV attribute, '%s' cannot be set", attr
);
1195 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1198 } else if (streq(key
, "TAG")) {
1199 if (op
< OP_MATCH_MAX
)
1200 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1202 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1204 } else if (streq(key
, "PROGRAM")) {
1205 if (op
== OP_REMOVE
)
1206 LOG_AND_RETURN("Invalid %s operation", key
);
1208 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1210 } else if (streq(key
, "RESULT")) {
1211 if (op
> OP_MATCH_MAX
)
1212 LOG_AND_RETURN("Invalid %s operation", key
);
1214 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1216 } else if (startswith(key
, "IMPORT")) {
1217 attr
= get_key_attribute(key
+ STRLEN("IMPORT"));
1219 LOG_RULE_WARNING("Ignoring IMPORT{} with missing type");
1222 if (op
== OP_REMOVE
)
1223 LOG_AND_RETURN("Invalid %s operation", "IMPORT");
1225 if (streq(attr
, "program")) {
1226 /* find known built-in command */
1227 if (value
[0] != '/') {
1228 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1231 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1232 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1236 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1237 } else if (streq(attr
, "builtin")) {
1238 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1241 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1243 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1244 } else if (streq(attr
, "file"))
1245 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1246 else if (streq(attr
, "db"))
1247 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1248 else if (streq(attr
, "cmdline"))
1249 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1250 else if (streq(attr
, "parent"))
1251 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1253 LOG_RULE_ERROR("Ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1255 } else if (startswith(key
, "TEST")) {
1258 if (op
> OP_MATCH_MAX
)
1259 LOG_AND_RETURN("Invalid %s operation", "TEST");
1261 attr
= get_key_attribute(key
+ STRLEN("TEST"));
1263 mode
= strtol(attr
, NULL
, 8);
1264 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1266 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1268 } else if (startswith(key
, "RUN")) {
1269 attr
= get_key_attribute(key
+ STRLEN("RUN"));
1272 if (op
== OP_REMOVE
)
1273 LOG_AND_RETURN("Invalid %s operation", "RUN");
1275 if (streq(attr
, "builtin")) {
1276 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1279 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1281 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1282 } else if (streq(attr
, "program")) {
1283 const enum udev_builtin_cmd cmd
= _UDEV_BUILTIN_MAX
;
1285 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1287 LOG_RULE_ERROR("Ignoring unknown %s{} type '%s'", "RUN", attr
);
1289 } else if (streq(key
, "LABEL")) {
1290 if (op
== OP_REMOVE
)
1291 LOG_AND_RETURN("Invalid %s operation", key
);
1293 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1295 } else if (streq(key
, "GOTO")) {
1296 if (op
== OP_REMOVE
)
1297 LOG_AND_RETURN("Invalid %s operation", key
);
1299 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1301 } else if (startswith(key
, "NAME")) {
1302 if (op
== OP_REMOVE
)
1303 LOG_AND_RETURN("Invalid %s operation", key
);
1305 if (op
< OP_MATCH_MAX
)
1306 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1308 if (streq(value
, "%k")) {
1309 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1312 if (isempty(value
)) {
1313 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1316 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1318 rule_tmp
.rule
.rule
.can_set_name
= true;
1320 } else if (streq(key
, "SYMLINK")) {
1321 if (op
== OP_REMOVE
)
1322 LOG_AND_RETURN("Invalid %s operation", key
);
1324 if (op
< OP_MATCH_MAX
)
1325 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1327 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1328 rule_tmp
.rule
.rule
.can_set_name
= true;
1330 } else if (streq(key
, "OWNER")) {
1334 if (op
== OP_REMOVE
)
1335 LOG_AND_RETURN("Invalid %s operation", key
);
1337 uid
= strtoul(value
, &endptr
, 10);
1338 if (endptr
[0] == '\0')
1339 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1340 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&& !strchr("$%", value
[0])) {
1341 uid
= add_uid(rules
, value
);
1342 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1343 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1344 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1346 rule_tmp
.rule
.rule
.can_set_name
= true;
1348 } else if (streq(key
, "GROUP")) {
1352 if (op
== OP_REMOVE
)
1353 LOG_AND_RETURN("Invalid %s operation", key
);
1355 gid
= strtoul(value
, &endptr
, 10);
1356 if (endptr
[0] == '\0')
1357 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1358 else if ((rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
) && !strchr("$%", value
[0])) {
1359 gid
= add_gid(rules
, value
);
1360 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1361 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1362 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1364 rule_tmp
.rule
.rule
.can_set_name
= true;
1366 } else if (streq(key
, "MODE")) {
1370 if (op
== OP_REMOVE
)
1371 LOG_AND_RETURN("Invalid %s operation", key
);
1373 mode
= strtol(value
, &endptr
, 8);
1374 if (endptr
[0] == '\0')
1375 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1377 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1378 rule_tmp
.rule
.rule
.can_set_name
= true;
1380 } else if (streq(key
, "OPTIONS")) {
1383 if (op
== OP_REMOVE
)
1384 LOG_AND_RETURN("Invalid %s operation", key
);
1386 pos
= strstr(value
, "link_priority=");
1388 int prio
= atoi(pos
+ STRLEN("link_priority="));
1390 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1393 pos
= strstr(value
, "string_escape=");
1395 pos
+= STRLEN("string_escape=");
1396 if (startswith(pos
, "none"))
1397 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1398 else if (startswith(pos
, "replace"))
1399 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1402 pos
= strstr(value
, "db_persist");
1404 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1406 pos
= strstr(value
, "nowatch");
1408 static const int zero
= 0;
1409 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &zero
);
1411 static const int one
= 1;
1412 pos
= strstr(value
, "watch");
1414 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &one
);
1417 pos
= strstr(value
, "static_node=");
1419 pos
+= STRLEN("static_node=");
1420 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1421 rule_tmp
.rule
.rule
.has_static_node
= true;
1425 LOG_AND_RETURN("Unknown key '%s'", key
);
1428 /* add rule token and sort tokens */
1429 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1430 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1431 LOG_RULE_ERROR("Failed to add rule token");
1434 static int parse_file(UdevRules
*rules
, const char *filename
) {
1435 _cleanup_fclose_
FILE *f
= NULL
;
1436 unsigned first_token
;
1437 unsigned filename_off
;
1438 char line
[UTIL_LINE_SIZE
];
1442 f
= fopen(filename
, "re");
1444 if (errno
== ENOENT
)
1450 if (null_or_empty_fd(fileno(f
))) {
1451 log_debug("Skipping empty file: %s", filename
);
1454 log_debug("Reading rules file: %s", filename
);
1456 first_token
= rules
->token_cur
;
1457 filename_off
= rules_add_string(rules
, filename
);
1459 while (fgets(line
, sizeof(line
), f
)) {
1463 /* skip whitespace */
1466 while (isspace(key
[0]))
1477 /* continue reading if backslash+newline is found */
1478 while (line
[len
-2] == '\\') {
1479 if (!fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
))
1481 if (strlen(&line
[len
-2]) < 2)
1487 if (len
+1 >= sizeof(line
)) {
1488 log_error("Line too long '%s':%u, ignored", filename
, line_nr
);
1491 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1494 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1495 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1496 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1497 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1500 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1501 if (rules
->tokens
[j
].type
!= TK_RULE
)
1503 if (rules
->tokens
[j
].rule
.label_off
== 0)
1505 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1507 rules
->tokens
[i
].key
.rule_goto
= j
;
1510 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1511 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1517 int udev_rules_new(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1518 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1519 _cleanup_strv_free_
char **files
= NULL
;
1523 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1525 rules
= new(UdevRules
, 1);
1529 *rules
= (UdevRules
) {
1530 .resolve_name_timing
= resolve_name_timing
,
1533 /* init token array and string buffer */
1534 rules
->tokens
= malloc_multiply(PREALLOC_TOKEN
, sizeof(struct token
));
1537 rules
->token_max
= PREALLOC_TOKEN
;
1539 rules
->strbuf
= strbuf_new();
1543 udev_rules_check_timestamp(rules
);
1545 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1547 return log_error_errno(r
, "Failed to enumerate rules files: %m");
1550 * The offset value in the rules strct is limited; add all
1551 * rules file names to the beginning of the string buffer.
1553 STRV_FOREACH(f
, files
)
1554 rules_add_string(rules
, *f
);
1556 STRV_FOREACH(f
, files
)
1557 parse_file(rules
, *f
);
1559 struct token end_token
= { .type
= TK_END
};
1560 add_token(rules
, &end_token
);
1561 log_debug("Rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1562 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1564 /* cleanup temporary strbuf data */
1565 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1566 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1567 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1568 strbuf_complete(rules
->strbuf
);
1570 /* cleanup uid/gid cache */
1571 rules
->uids
= mfree(rules
->uids
);
1572 rules
->uids_cur
= 0;
1573 rules
->uids_max
= 0;
1574 rules
->gids
= mfree(rules
->gids
);
1575 rules
->gids_cur
= 0;
1576 rules
->gids_max
= 0;
1579 *ret_rules
= TAKE_PTR(rules
);
1583 UdevRules
*udev_rules_free(UdevRules
*rules
) {
1586 free(rules
->tokens
);
1587 strbuf_cleanup(rules
->strbuf
);
1590 return mfree(rules
);
1593 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1597 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1600 static int match_key(UdevRules
*rules
, struct token
*token
, const char *val
) {
1601 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1608 switch (token
->key
.glob
) {
1610 match
= (streq(key_value
, val
));
1613 match
= (fnmatch(key_value
, val
, 0) == 0);
1620 s
= rules_str(rules
, token
->key
.value_off
);
1625 next
= strchr(s
, '|');
1627 size_t matchlen
= (size_t)(next
- s
);
1629 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1633 match
= (streq(s
, val
));
1642 char value
[UTIL_PATH_SIZE
];
1644 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1647 pos
= strchr(key_value
, '|');
1652 match
= (fnmatch(key_value
, val
, 0) == 0);
1660 match
= (val
[0] != '\0');
1666 if (match
&& (token
->key
.op
== OP_MATCH
))
1668 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1673 static int match_attr(UdevRules
*rules
, sd_device
*dev
, UdevEvent
*event
, struct token
*cur
) {
1674 char nbuf
[UTIL_NAME_SIZE
], vbuf
[UTIL_NAME_SIZE
];
1675 const char *name
, *value
;
1678 name
= rules_str(rules
, cur
->key
.attr_off
);
1679 switch (cur
->key
.attrsubst
) {
1681 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1685 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1689 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) != 0)
1697 /* remove trailing whitespace, if not asked to match for it */
1698 len
= strlen(value
);
1699 if (len
> 0 && isspace(value
[len
-1])) {
1700 const char *key_value
;
1703 key_value
= rules_str(rules
, cur
->key
.value_off
);
1704 klen
= strlen(key_value
);
1705 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1706 if (value
!= vbuf
) {
1707 strscpy(vbuf
, sizeof(vbuf
), value
);
1710 while (len
> 0 && isspace(vbuf
[--len
]))
1715 return match_key(rules
, cur
, value
);
1724 int udev_rules_apply_to_event(
1727 usec_t timeout_usec
,
1728 Hashmap
*properties_list
) {
1729 sd_device
*dev
= event
->dev
;
1730 enum escape_type esc
= ESCAPE_UNSET
;
1731 struct token
*cur
, *rule
;
1732 const char *action
, *val
;
1739 r
= sd_device_get_property_value(dev
, "ACTION", &action
);
1743 can_set_name
= (!streq(action
, "remove") &&
1744 (sd_device_get_devnum(dev
, NULL
) >= 0 ||
1745 sd_device_get_ifindex(dev
, NULL
) >= 0));
1747 /* loop through token list, match, run actions or forward to next rule */
1748 cur
= &rules
->tokens
[0];
1751 dump_token(rules
, cur
);
1752 switch (cur
->type
) {
1756 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1757 if (!can_set_name
&& rule
->rule
.can_set_name
)
1762 if (match_key(rules
, cur
, action
) != 0)
1766 if (sd_device_get_devpath(dev
, &val
) < 0)
1768 if (match_key(rules
, cur
, val
) != 0)
1772 if (sd_device_get_sysname(dev
, &val
) < 0)
1774 if (match_key(rules
, cur
, val
) != 0)
1777 case TK_M_DEVLINK
: {
1778 const char *devlink
;
1781 FOREACH_DEVICE_DEVLINK(dev
, devlink
)
1782 if (match_key(rules
, cur
, devlink
+ STRLEN("/dev/")) == 0) {
1792 if (match_key(rules
, cur
, event
->name
) != 0)
1796 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1798 if (sd_device_get_property_value(dev
, key_name
, &val
) < 0) {
1799 /* check global properties */
1800 if (properties_list
)
1801 val
= hashmap_get(properties_list
, key_name
);
1806 if (match_key(rules
, cur
, strempty(val
)))
1814 FOREACH_DEVICE_TAG(dev
, tag
)
1815 if (streq(rules_str(rules
, cur
->key
.value_off
), tag
)) {
1820 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1821 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1825 case TK_M_SUBSYSTEM
:
1826 if (sd_device_get_subsystem(dev
, &val
) < 0)
1828 if (match_key(rules
, cur
, val
) != 0)
1832 if (sd_device_get_driver(dev
, &val
) < 0)
1834 if (match_key(rules
, cur
, val
) != 0)
1838 if (match_attr(rules
, dev
, event
, cur
) != 0)
1842 char filename
[UTIL_PATH_SIZE
];
1843 _cleanup_free_
char *value
= NULL
;
1846 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1847 sysctl_normalize(filename
);
1848 if (sysctl_read(filename
, &value
) < 0)
1851 len
= strlen(value
);
1852 while (len
> 0 && isspace(value
[--len
]))
1854 if (match_key(rules
, cur
, value
) != 0)
1859 case TK_M_SUBSYSTEMS
:
1865 /* get whole sequence of parent matches */
1867 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1870 /* loop over parents */
1871 event
->dev_parent
= dev
;
1875 /* loop over sequence of parent match keys */
1876 for (key
= cur
; key
< next
; key
++ ) {
1877 dump_token(rules
, key
);
1880 if (sd_device_get_sysname(event
->dev_parent
, &val
) < 0)
1882 if (match_key(rules
, key
, val
) != 0)
1885 case TK_M_SUBSYSTEMS
:
1886 if (sd_device_get_subsystem(event
->dev_parent
, &val
) < 0)
1888 if (match_key(rules
, key
, val
) != 0)
1892 if (sd_device_get_driver(event
->dev_parent
, &val
) < 0)
1894 if (match_key(rules
, key
, val
) != 0)
1898 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1902 bool match
= sd_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1904 if (match
&& key
->key
.op
== OP_NOMATCH
)
1906 if (!match
&& key
->key
.op
== OP_MATCH
)
1917 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
1918 event
->dev_parent
= NULL
;
1922 /* move behind our sequence of parent match keys */
1927 char filename
[UTIL_PATH_SIZE
];
1928 struct stat statbuf
;
1931 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1932 if (util_resolve_subsys_kernel(filename
, filename
, sizeof(filename
), false) != 0) {
1933 if (filename
[0] != '/') {
1934 char tmp
[UTIL_PATH_SIZE
];
1936 if (sd_device_get_syspath(dev
, &val
) < 0)
1939 strscpy(tmp
, sizeof(tmp
), filename
);
1940 strscpyl(filename
, sizeof(filename
), val
, "/", tmp
, NULL
);
1943 attr_subst_subdir(filename
, sizeof(filename
));
1945 match
= (stat(filename
, &statbuf
) == 0);
1946 if (match
&& cur
->key
.mode
> 0)
1947 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1948 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1950 if (!match
&& cur
->key
.op
== OP_MATCH
)
1954 case TK_M_PROGRAM
: {
1955 char program
[UTIL_PATH_SIZE
], result
[UTIL_LINE_SIZE
];
1957 event
->program_result
= mfree(event
->program_result
);
1958 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1959 log_device_debug(dev
, "PROGRAM '%s' %s:%u",
1961 rules_str(rules
, rule
->rule
.filename_off
),
1962 rule
->rule
.filename_line
);
1964 if (udev_event_spawn(event
, timeout_usec
, true, program
, result
, sizeof(result
)) != 0) {
1965 if (cur
->key
.op
!= OP_NOMATCH
)
1970 delete_trailing_chars(result
, "\n");
1971 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1972 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1974 log_device_debug(dev
, "Replaced %i character(s) from result of '%s'" , count
, program
);
1976 event
->program_result
= strdup(result
);
1977 if (cur
->key
.op
== OP_NOMATCH
)
1982 case TK_M_IMPORT_FILE
: {
1983 char import
[UTIL_PATH_SIZE
];
1985 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1986 if (import_file_into_properties(dev
, import
) != 0)
1987 if (cur
->key
.op
!= OP_NOMATCH
)
1991 case TK_M_IMPORT_PROG
: {
1992 char import
[UTIL_PATH_SIZE
];
1994 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1995 log_device_debug(dev
, "IMPORT '%s' %s:%u",
1997 rules_str(rules
, rule
->rule
.filename_off
),
1998 rule
->rule
.filename_line
);
2000 if (import_program_into_properties(event
, timeout_usec
, import
) != 0)
2001 if (cur
->key
.op
!= OP_NOMATCH
)
2005 case TK_M_IMPORT_BUILTIN
: {
2006 char command
[UTIL_PATH_SIZE
];
2008 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2009 /* check if we ran already */
2010 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2011 log_device_debug(dev
, "IMPORT builtin skip '%s' %s:%u",
2012 udev_builtin_name(cur
->key
.builtin_cmd
),
2013 rules_str(rules
, rule
->rule
.filename_off
),
2014 rule
->rule
.filename_line
);
2015 /* return the result from earlier run */
2016 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2017 if (cur
->key
.op
!= OP_NOMATCH
)
2022 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2025 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2026 log_device_debug(dev
, "IMPORT builtin '%s' %s:%u",
2027 udev_builtin_name(cur
->key
.builtin_cmd
),
2028 rules_str(rules
, rule
->rule
.filename_off
),
2029 rule
->rule
.filename_line
);
2031 r
= udev_builtin_run(dev
, cur
->key
.builtin_cmd
, command
, false);
2033 /* remember failure */
2034 log_device_debug_errno(dev
, r
, "IMPORT builtin '%s' fails: %m",
2035 udev_builtin_name(cur
->key
.builtin_cmd
));
2036 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2037 if (cur
->key
.op
!= OP_NOMATCH
)
2042 case TK_M_IMPORT_DB
: {
2045 key
= rules_str(rules
, cur
->key
.value_off
);
2046 if (event
->dev_db_clone
&&
2047 sd_device_get_property_value(event
->dev_db_clone
, key
, &val
) >= 0)
2048 device_add_property(dev
, key
, val
);
2049 else if (cur
->key
.op
!= OP_NOMATCH
)
2053 case TK_M_IMPORT_CMDLINE
: {
2054 _cleanup_free_
char *value
= NULL
;
2055 bool imported
= false;
2058 key
= rules_str(rules
, cur
->key
.value_off
);
2059 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2061 log_device_debug_errno(dev
, r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2066 device_add_property(dev
, key
, value
);
2068 /* we import simple flags as 'FLAG=1' */
2069 device_add_property(dev
, key
, "1");
2072 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2076 case TK_M_IMPORT_PARENT
: {
2077 char import
[UTIL_PATH_SIZE
];
2079 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2080 if (import_parent_into_properties(dev
, import
) != 0)
2081 if (cur
->key
.op
!= OP_NOMATCH
)
2086 if (match_key(rules
, cur
, event
->program_result
) != 0)
2089 case TK_A_STRING_ESCAPE_NONE
:
2092 case TK_A_STRING_ESCAPE_REPLACE
:
2093 esc
= ESCAPE_REPLACE
;
2095 case TK_A_DB_PERSIST
:
2096 device_set_db_persist(dev
);
2098 case TK_A_INOTIFY_WATCH
:
2099 if (event
->inotify_watch_final
)
2101 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2102 event
->inotify_watch_final
= true;
2103 event
->inotify_watch
= cur
->key
.watch
;
2105 case TK_A_DEVLINK_PRIO
:
2106 device_set_devlink_priority(dev
, cur
->key
.devlink_prio
);
2109 char owner
[UTIL_NAME_SIZE
];
2110 const char *ow
= owner
;
2112 if (event
->owner_final
)
2114 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2115 event
->owner_final
= true;
2116 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2117 event
->owner_set
= true;
2118 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2120 log_unknown_owner(dev
, r
, "user", owner
);
2123 log_device_debug(dev
, "OWNER %u %s:%u",
2125 rules_str(rules
, rule
->rule
.filename_off
),
2126 rule
->rule
.filename_line
);
2130 char group
[UTIL_NAME_SIZE
];
2131 const char *gr
= group
;
2133 if (event
->group_final
)
2135 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2136 event
->group_final
= true;
2137 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2138 event
->group_set
= true;
2139 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2141 log_unknown_owner(dev
, r
, "group", group
);
2144 log_device_debug(dev
, "GROUP %u %s:%u",
2146 rules_str(rules
, rule
->rule
.filename_off
),
2147 rule
->rule
.filename_line
);
2151 char mode_str
[UTIL_NAME_SIZE
];
2154 if (event
->mode_final
)
2156 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2157 r
= parse_mode(mode_str
, &mode
);
2159 log_device_error_errno(dev
, r
, "Failed to parse mode '%s': %m", mode_str
);
2162 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2163 event
->mode_final
= true;
2164 event
->mode_set
= true;
2166 log_device_debug(dev
, "MODE %#o %s:%u",
2168 rules_str(rules
, rule
->rule
.filename_off
),
2169 rule
->rule
.filename_line
);
2173 if (event
->owner_final
)
2175 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2176 event
->owner_final
= true;
2177 event
->owner_set
= true;
2178 event
->uid
= cur
->key
.uid
;
2179 log_device_debug(dev
, "OWNER %u %s:%u",
2181 rules_str(rules
, rule
->rule
.filename_off
),
2182 rule
->rule
.filename_line
);
2185 if (event
->group_final
)
2187 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2188 event
->group_final
= true;
2189 event
->group_set
= true;
2190 event
->gid
= cur
->key
.gid
;
2191 log_device_debug(dev
, "GROUP %u %s:%u",
2193 rules_str(rules
, rule
->rule
.filename_off
),
2194 rule
->rule
.filename_line
);
2197 if (event
->mode_final
)
2199 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2200 event
->mode_final
= true;
2201 event
->mode_set
= true;
2202 event
->mode
= cur
->key
.mode
;
2203 log_device_debug(dev
, "MODE %#o %s:%u",
2205 rules_str(rules
, rule
->rule
.filename_off
),
2206 rule
->rule
.filename_line
);
2208 case TK_A_SECLABEL
: {
2209 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2210 char label_str
[UTIL_LINE_SIZE
] = {};
2212 name
= strdup(rules_str(rules
, cur
->key
.attr_off
));
2216 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2217 if (!isempty(label_str
))
2218 label
= strdup(label_str
);
2220 label
= strdup(rules_str(rules
, cur
->key
.value_off
));
2224 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2225 hashmap_clear_free_free(event
->seclabel_list
);
2227 r
= hashmap_ensure_allocated(&event
->seclabel_list
, NULL
);
2231 r
= hashmap_put(event
->seclabel_list
, name
, label
);
2235 name
= label
= NULL
;
2237 log_device_debug(dev
, "SECLABEL{%s}='%s' %s:%u",
2239 rules_str(rules
, rule
->rule
.filename_off
),
2240 rule
->rule
.filename_line
);
2244 char value_new
[UTIL_NAME_SIZE
];
2245 const char *name
, *value_old
;
2247 name
= rules_str(rules
, cur
->key
.attr_off
);
2248 val
= rules_str(rules
, cur
->key
.value_off
);
2249 if (val
[0] == '\0') {
2250 if (cur
->key
.op
== OP_ADD
)
2252 device_add_property(dev
, name
, NULL
);
2256 if (cur
->key
.op
== OP_ADD
&&
2257 sd_device_get_property_value(dev
, name
, &value_old
) >= 0) {
2258 char temp
[UTIL_NAME_SIZE
];
2260 /* append value separated by space */
2261 udev_event_apply_format(event
, val
, temp
, sizeof(temp
), false);
2262 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2264 udev_event_apply_format(event
, val
, value_new
, sizeof(value_new
), false);
2266 device_add_property(dev
, name
, value_new
);
2270 char tag
[UTIL_PATH_SIZE
];
2273 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2274 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2275 device_cleanup_tags(dev
);
2276 for (p
= tag
; *p
!= '\0'; p
++) {
2277 if ((*p
>= 'a' && *p
<= 'z') ||
2278 (*p
>= 'A' && *p
<= 'Z') ||
2279 (*p
>= '0' && *p
<= '9') ||
2280 IN_SET(*p
, '-', '_'))
2282 log_device_error(dev
, "Ignoring invalid tag name '%s'", tag
);
2285 if (cur
->key
.op
== OP_REMOVE
)
2286 device_remove_tag(dev
, tag
);
2288 device_add_tag(dev
, tag
);
2292 char name_str
[UTIL_PATH_SIZE
];
2296 name
= rules_str(rules
, cur
->key
.value_off
);
2297 if (event
->name_final
)
2299 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2300 event
->name_final
= true;
2301 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2302 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2303 count
= util_replace_chars(name_str
, "/");
2305 log_device_debug(dev
, "Replaced %i character(s) from result of NAME=\"%s\"", count
, name
);
2307 if (sd_device_get_devnum(dev
, NULL
) >= 0 &&
2308 (sd_device_get_devname(dev
, &val
) < 0 ||
2309 !streq(name_str
, val
+ STRLEN("/dev/")))) {
2310 log_device_error(dev
, "Kernel device nodes cannot be renamed, ignoring NAME=\"%s\"; please fix it in %s:%u\n",
2312 rules_str(rules
, rule
->rule
.filename_off
),
2313 rule
->rule
.filename_line
);
2316 if (free_and_strdup(&event
->name
, name_str
) < 0)
2319 log_device_debug(dev
, "NAME '%s' %s:%u",
2321 rules_str(rules
, rule
->rule
.filename_off
),
2322 rule
->rule
.filename_line
);
2325 case TK_A_DEVLINK
: {
2326 char temp
[UTIL_PATH_SIZE
], filename
[UTIL_PATH_SIZE
], *pos
, *next
;
2329 if (event
->devlink_final
)
2331 if (sd_device_get_devnum(dev
, NULL
) < 0)
2333 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2334 event
->devlink_final
= true;
2335 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2336 device_cleanup_devlinks(dev
);
2338 /* allow multiple symlinks separated by spaces */
2339 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2340 if (esc
== ESCAPE_UNSET
)
2341 count
= util_replace_chars(temp
, "/ ");
2342 else if (esc
== ESCAPE_REPLACE
)
2343 count
= util_replace_chars(temp
, "/");
2345 log_device_debug(dev
, "Replaced %i character(s) from result of LINK" , count
);
2347 while (isspace(pos
[0]))
2349 next
= strchr(pos
, ' ');
2352 log_device_debug(dev
, "LINK '%s' %s:%u", pos
,
2353 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2354 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2355 device_add_devlink(dev
, filename
);
2356 while (isspace(next
[1]))
2359 next
= strchr(pos
, ' ');
2361 if (pos
[0] != '\0') {
2362 log_device_debug(dev
, "LINK '%s' %s:%u", pos
,
2363 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2364 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2365 device_add_devlink(dev
, filename
);
2370 char attr
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2371 _cleanup_fclose_
FILE *f
= NULL
;
2372 const char *key_name
;
2374 key_name
= rules_str(rules
, cur
->key
.attr_off
);
2375 if (util_resolve_subsys_kernel(key_name
, attr
, sizeof(attr
), false) != 0 &&
2376 sd_device_get_syspath(dev
, &val
) >= 0)
2377 strscpyl(attr
, sizeof(attr
), val
, "/", key_name
, NULL
);
2378 attr_subst_subdir(attr
, sizeof(attr
));
2380 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2381 log_device_debug(dev
, "ATTR '%s' writing '%s' %s:%u", attr
, value
,
2382 rules_str(rules
, rule
->rule
.filename_off
),
2383 rule
->rule
.filename_line
);
2384 f
= fopen(attr
, "we");
2386 log_device_error_errno(dev
, errno
, "Failed to open ATTR{%s} for writing: %m", attr
);
2387 else if (fprintf(f
, "%s", value
) <= 0)
2388 log_device_error_errno(dev
, errno
, "Failed to write ATTR{%s}: %m", attr
);
2392 char filename
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2394 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2395 sysctl_normalize(filename
);
2396 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2397 log_device_debug(dev
, "SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2398 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2399 r
= sysctl_write(filename
, value
);
2401 log_device_error_errno(dev
, r
, "Failed to write SYSCTL{%s}='%s': %m", filename
, value
);
2404 case TK_A_RUN_BUILTIN
:
2405 case TK_A_RUN_PROGRAM
: {
2406 _cleanup_free_
char *cmd
= NULL
;
2408 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2409 hashmap_clear_free_key(event
->run_list
);
2411 r
= hashmap_ensure_allocated(&event
->run_list
, NULL
);
2415 cmd
= strdup(rules_str(rules
, cur
->key
.value_off
));
2419 r
= hashmap_put(event
->run_list
, cmd
, INT_TO_PTR(cur
->key
.builtin_cmd
));
2425 log_device_debug(dev
, "RUN '%s' %s:%u",
2426 rules_str(rules
, cur
->key
.value_off
),
2427 rules_str(rules
, rule
->rule
.filename_off
),
2428 rule
->rule
.filename_line
);
2432 if (cur
->key
.rule_goto
== 0)
2434 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2439 case TK_M_PARENTS_MIN
:
2440 case TK_M_PARENTS_MAX
:
2443 log_device_error(dev
, "Wrong type %u", cur
->type
);
2450 /* fast-forward to next rule */
2451 cur
= rule
+ rule
->rule
.token_count
;
2457 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2463 _cleanup_strv_free_
char **tags
= NULL
;
2466 _cleanup_free_
char *path
= NULL
;
2472 cur
= &rules
->tokens
[0];
2475 switch (cur
->type
) {
2480 /* skip rules without a static_node tag */
2481 if (!rule
->rule
.has_static_node
)
2487 tags
= strv_free(tags
);
2496 mode
= cur
->key
.mode
;
2499 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2504 case TK_A_STATIC_NODE
: {
2505 char device_node
[UTIL_PATH_SIZE
];
2506 char tags_dir
[UTIL_PATH_SIZE
];
2507 char tag_symlink
[UTIL_PATH_SIZE
];
2510 /* we assure, that the permissions tokens are sorted before the static token */
2512 if (mode
== 0 && uid
== 0 && gid
== 0 && !tags
)
2515 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2516 if (stat(device_node
, &stats
) != 0)
2518 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2521 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2523 STRV_FOREACH(t
, tags
) {
2524 _cleanup_free_
char *unescaped_filename
= NULL
;
2526 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2527 r
= mkdir_p(tags_dir
, 0755);
2529 return log_error_errno(r
, "Failed to create %s: %m", tags_dir
);
2531 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2533 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2534 r
= symlink(device_node
, tag_symlink
);
2535 if (r
< 0 && errno
!= EEXIST
)
2536 return log_error_errno(errno
, "Failed to create symlink %s -> %s: %m",
2537 tag_symlink
, device_node
);
2541 /* don't touch the permissions if only the tags were set */
2542 if (mode
== 0 && uid
== 0 && gid
== 0)
2551 if (mode
!= (stats
.st_mode
& 01777)) {
2552 r
= chmod(device_node
, mode
);
2554 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2557 log_debug("chmod '%s' %#o", device_node
, mode
);
2560 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2561 r
= chown(device_node
, uid
, gid
);
2563 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2564 device_node
, uid
, gid
);
2566 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2569 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2579 /* fast-forward to next rule */
2580 cur
= rule
+ rule
->rule
.token_count
;
2587 fchmod(fileno(f
), 0644);
2588 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2589 unlink_noerrno("/run/udev/static_node-tags");
2590 unlink_noerrno(path
);