1 /* SPDX-License-Identifier: GPL-2.0+ */
16 #include "alloc-util.h"
17 #include "conf-files.h"
19 #include "device-private.h"
20 #include "device-util.h"
21 #include "dirent-util.h"
26 #include "glob-util.h"
27 #include "libudev-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "proc-cmdline.h"
32 #include "stat-util.h"
33 #include "stdio-util.h"
35 #include "string-util.h"
38 #include "sysctl-util.h"
39 #include "udev-builtin.h"
41 #include "user-util.h"
44 #define PREALLOC_TOKEN 2048
45 #define RULES_DIRS (const char* const*) CONF_PATHS_STRV("udev/rules.d")
57 ResolveNameTiming resolve_name_timing
;
59 /* every key in the rules file becomes a token */
64 /* all key strings are copied and de-duplicated in a single continuous string buffer */
65 struct strbuf
*strbuf
;
67 /* during rule parsing, uid/gid lookup results are cached */
76 static char *rules_str(UdevRules
*rules
, unsigned off
) {
77 return rules
->strbuf
->buf
+ off
;
80 static unsigned rules_add_string(UdevRules
*rules
, const char *s
) {
81 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
84 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
98 enum string_glob_type
{
100 GL_PLAIN
, /* no special chars */
101 GL_GLOB
, /* shell globs ?,*,[] */
102 GL_SPLIT
, /* multi-value A|B */
103 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
104 GL_SOMETHING
, /* commonly used "?*" */
107 enum string_subst_type
{
114 /* tokens of a rule are sorted/handled in this order */
119 TK_M_ACTION
, /* val */
120 TK_M_DEVPATH
, /* val */
121 TK_M_KERNEL
, /* val */
122 TK_M_DEVLINK
, /* val */
124 TK_M_ENV
, /* val, attr */
126 TK_M_SUBSYSTEM
, /* val */
127 TK_M_DRIVER
, /* val */
128 TK_M_WAITFOR
, /* val */
129 TK_M_ATTR
, /* val, attr */
130 TK_M_SYSCTL
, /* val, attr */
133 TK_M_KERNELS
, /* val */
134 TK_M_SUBSYSTEMS
, /* val */
135 TK_M_DRIVERS
, /* val */
136 TK_M_ATTRS
, /* val, attr */
140 TK_M_TEST
, /* val, mode_t */
141 TK_M_PROGRAM
, /* val */
142 TK_M_IMPORT_FILE
, /* val */
143 TK_M_IMPORT_PROG
, /* val */
144 TK_M_IMPORT_BUILTIN
, /* val */
145 TK_M_IMPORT_DB
, /* val */
146 TK_M_IMPORT_CMDLINE
, /* val */
147 TK_M_IMPORT_PARENT
, /* val */
148 TK_M_RESULT
, /* val */
151 TK_A_STRING_ESCAPE_NONE
,
152 TK_A_STRING_ESCAPE_REPLACE
,
154 TK_A_INOTIFY_WATCH
, /* int */
155 TK_A_DEVLINK_PRIO
, /* int */
156 TK_A_OWNER
, /* val */
157 TK_A_GROUP
, /* val */
159 TK_A_OWNER_ID
, /* uid_t */
160 TK_A_GROUP_ID
, /* gid_t */
161 TK_A_MODE_ID
, /* mode_t */
163 TK_A_STATIC_NODE
, /* val */
164 TK_A_SECLABEL
, /* val, attr */
165 TK_A_ENV
, /* val, attr */
167 TK_A_DEVLINK
, /* val */
168 TK_A_ATTR
, /* val, attr */
169 TK_A_SYSCTL
, /* val, attr */
170 TK_A_RUN_BUILTIN
, /* val, bool */
171 TK_A_RUN_PROGRAM
, /* val, bool */
172 TK_A_GOTO
, /* size_t */
177 /* we try to pack stuff in a way that we take only 12 bytes per token */
180 unsigned char type
; /* same in rule and key */
182 enum token_type type
:8;
184 bool has_static_node
:1;
186 unsigned short token_count
;
188 unsigned short filename_off
;
189 unsigned short filename_line
;
192 enum token_type type
:8;
193 enum operation_type op
:8;
194 enum string_glob_type glob
:8;
195 enum string_subst_type subst
:4;
196 enum string_subst_type attrsubst
:4;
206 enum udev_builtin_cmd builtin_cmd
;
216 struct token token
[MAX_TK
];
220 #if ENABLE_DEBUG_UDEV
221 static const char *operation_str(enum operation_type type
) {
222 static const char *operation_strs
[] = {
223 [OP_UNSET
] = "UNSET",
224 [OP_MATCH
] = "match",
225 [OP_NOMATCH
] = "nomatch",
226 [OP_MATCH_MAX
] = "MATCH_MAX",
229 [OP_REMOVE
] = "remove",
230 [OP_ASSIGN
] = "assign",
231 [OP_ASSIGN_FINAL
] = "assign-final",
234 return operation_strs
[type
];
237 static const char *string_glob_str(enum string_glob_type type
) {
238 static const char *string_glob_strs
[] = {
239 [GL_UNSET
] = "UNSET",
240 [GL_PLAIN
] = "plain",
242 [GL_SPLIT
] = "split",
243 [GL_SPLIT_GLOB
] = "split-glob",
244 [GL_SOMETHING
] = "split-glob",
247 return string_glob_strs
[type
];
250 static const char *token_str(enum token_type type
) {
251 static const char *token_strs
[] = {
252 [TK_UNSET
] = "UNSET",
255 [TK_M_ACTION
] = "M ACTION",
256 [TK_M_DEVPATH
] = "M DEVPATH",
257 [TK_M_KERNEL
] = "M KERNEL",
258 [TK_M_DEVLINK
] = "M DEVLINK",
259 [TK_M_NAME
] = "M NAME",
260 [TK_M_ENV
] = "M ENV",
261 [TK_M_TAG
] = "M TAG",
262 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
263 [TK_M_DRIVER
] = "M DRIVER",
264 [TK_M_WAITFOR
] = "M WAITFOR",
265 [TK_M_ATTR
] = "M ATTR",
266 [TK_M_SYSCTL
] = "M SYSCTL",
268 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
269 [TK_M_KERNELS
] = "M KERNELS",
270 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
271 [TK_M_DRIVERS
] = "M DRIVERS",
272 [TK_M_ATTRS
] = "M ATTRS",
273 [TK_M_TAGS
] = "M TAGS",
274 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
276 [TK_M_TEST
] = "M TEST",
277 [TK_M_PROGRAM
] = "M PROGRAM",
278 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
279 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
280 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
281 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
282 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
283 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
284 [TK_M_RESULT
] = "M RESULT",
285 [TK_M_MAX
] = "M MAX",
287 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
288 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
289 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
290 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
291 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
292 [TK_A_OWNER
] = "A OWNER",
293 [TK_A_GROUP
] = "A GROUP",
294 [TK_A_MODE
] = "A MODE",
295 [TK_A_OWNER_ID
] = "A OWNER_ID",
296 [TK_A_GROUP_ID
] = "A GROUP_ID",
297 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
298 [TK_A_SECLABEL
] = "A SECLABEL",
299 [TK_A_MODE_ID
] = "A MODE_ID",
300 [TK_A_ENV
] = "A ENV",
301 [TK_A_TAG
] = "A ENV",
302 [TK_A_NAME
] = "A NAME",
303 [TK_A_DEVLINK
] = "A DEVLINK",
304 [TK_A_ATTR
] = "A ATTR",
305 [TK_A_SYSCTL
] = "A SYSCTL",
306 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
307 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
308 [TK_A_GOTO
] = "A GOTO",
313 return token_strs
[type
];
316 static void dump_token(UdevRules
*rules
, struct token
*token
) {
317 enum token_type type
= token
->type
;
318 enum operation_type op
= token
->key
.op
;
319 enum string_glob_type glob
= token
->key
.glob
;
320 const char *value
= rules_str(rules
, token
->key
.value_off
);
321 const char *attr
= &rules
->strbuf
->buf
[token
->key
.attr_off
];
326 const char *tks_ptr
= (char *)rules
->tokens
;
327 const char *tk_ptr
= (char *)token
;
328 unsigned idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
330 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
331 &rules
->strbuf
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
332 idx
, token
->rule
.token_count
,
333 &rules
->strbuf
->buf
[token
->rule
.label_off
]);
345 case TK_M_SUBSYSTEMS
:
349 case TK_M_IMPORT_FILE
:
350 case TK_M_IMPORT_PROG
:
352 case TK_M_IMPORT_CMDLINE
:
353 case TK_M_IMPORT_PARENT
:
360 case TK_A_RUN_BUILTIN
:
361 case TK_A_RUN_PROGRAM
:
362 log_debug("%s %s '%s'(%s)",
363 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
365 case TK_M_IMPORT_BUILTIN
:
366 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
375 log_debug("%s %s '%s' '%s'(%s)",
376 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
380 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
382 case TK_A_STRING_ESCAPE_NONE
:
383 case TK_A_STRING_ESCAPE_REPLACE
:
384 case TK_A_DB_PERSIST
:
385 log_debug("%s", token_str(type
));
388 log_debug("%s %s '%s'(%s) %#o",
389 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
391 case TK_A_INOTIFY_WATCH
:
392 log_debug("%s %u", token_str(type
), token
->key
.watch
);
394 case TK_A_DEVLINK_PRIO
:
395 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
398 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
401 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
404 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
406 case TK_A_STATIC_NODE
:
407 log_debug("%s '%s'", token_str(type
), value
);
410 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
413 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
416 log_debug("* %s", token_str(type
));
418 case TK_M_PARENTS_MIN
:
419 case TK_M_PARENTS_MAX
:
422 log_debug("Unknown token type %u", type
);
427 static void dump_rules(UdevRules
*rules
) {
430 log_debug("Dumping %zu (%zu bytes) tokens, %zu (%zu bytes) strings",
432 rules
->token_cur
* sizeof(struct token
),
433 rules
->strbuf
->nodes_count
,
435 for (i
= 0; i
< rules
->token_cur
; i
++)
436 dump_token(rules
, &rules
->tokens
[i
]);
439 static void dump_token(UdevRules
*rules
, struct token
*token
) {}
440 static void dump_rules(UdevRules
*rules
) {}
441 #endif /* ENABLE_DEBUG_UDEV */
443 static int add_token(UdevRules
*rules
, struct token
*token
) {
444 /* grow buffer if needed */
445 if (!GREEDY_REALLOC(rules
->tokens
, rules
->token_max
, rules
->token_cur
+ 1))
448 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
453 static void log_unknown_owner(sd_device
*dev
, int error
, const char *entity
, const char *owner
) {
454 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
455 log_device_error(dev
, "Specified %s '%s' unknown", entity
, owner
);
457 log_device_error_errno(dev
, error
, "Failed to resolve %s '%s': %m", entity
, owner
);
460 static uid_t
add_uid(UdevRules
*rules
, const char *owner
) {
466 /* lookup, if we know it already */
467 for (i
= 0; i
< rules
->uids_cur
; i
++) {
468 off
= rules
->uids
[i
].name_off
;
469 if (streq(rules_str(rules
, off
), owner
))
470 return rules
->uids
[i
].uid
;
472 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
474 log_unknown_owner(NULL
, r
, "user", owner
);
476 /* grow buffer if needed */
477 if (!GREEDY_REALLOC(rules
->uids
, rules
->uids_max
, rules
->uids_cur
+ 1))
480 rules
->uids
[rules
->uids_cur
].uid
= uid
;
481 off
= rules_add_string(rules
, owner
);
484 rules
->uids
[rules
->uids_cur
].name_off
= off
;
489 static gid_t
add_gid(UdevRules
*rules
, const char *group
) {
495 /* lookup, if we know it already */
496 for (i
= 0; i
< rules
->gids_cur
; i
++) {
497 off
= rules
->gids
[i
].name_off
;
498 if (streq(rules_str(rules
, off
), group
))
499 return rules
->gids
[i
].gid
;
501 r
= get_group_creds(&group
, &gid
, USER_CREDS_ALLOW_MISSING
);
503 log_unknown_owner(NULL
, r
, "group", group
);
505 /* grow buffer if needed */
506 if (!GREEDY_REALLOC(rules
->gids
, rules
->gids_max
, rules
->gids_cur
+ 1))
509 rules
->gids
[rules
->gids_cur
].gid
= gid
;
510 off
= rules_add_string(rules
, group
);
513 rules
->gids
[rules
->gids_cur
].name_off
= off
;
518 static int import_property_from_string(sd_device
*dev
, char *line
) {
525 while (isspace(key
[0]))
528 /* comment or empty line */
529 if (IN_SET(key
[0], '#', '\0'))
532 /* split key/value */
533 val
= strchr(key
, '=');
540 while (isspace(val
[0]))
547 while (isspace(key
[len
-1]))
551 /* terminate value */
555 while (isspace(val
[len
-1]))
563 if (IN_SET(val
[0], '"', '\'')) {
564 if (len
== 1 || val
[len
-1] != val
[0])
565 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
566 "Inconsistent quoting: '%s', skip",
572 return device_add_property(dev
, key
, val
);
575 static int import_file_into_properties(sd_device
*dev
, const char *filename
) {
576 _cleanup_fclose_
FILE *f
= NULL
;
579 f
= fopen(filename
, "re");
584 _cleanup_free_
char *line
= NULL
;
586 r
= read_line(f
, LONG_LINE_MAX
, &line
);
592 (void) import_property_from_string(dev
, line
);
598 static int import_program_into_properties(UdevEvent
*event
,
600 const char *program
) {
601 char result
[UTIL_LINE_SIZE
];
605 r
= udev_event_spawn(event
, timeout_usec
, false, program
, result
, sizeof result
);
615 pos
= strchr(line
, '\n');
620 (void) import_property_from_string(event
->dev
, line
);
626 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
627 const char *key
, *val
;
634 r
= sd_device_get_parent(dev
, &parent
);
638 FOREACH_DEVICE_PROPERTY(parent
, key
, val
)
639 if (fnmatch(filter
, key
, 0) == 0)
640 device_add_property(dev
, key
, val
);
644 static void attr_subst_subdir(char *attr
, size_t len
) {
645 const char *pos
, *tail
, *path
;
646 _cleanup_closedir_
DIR *dir
= NULL
;
649 pos
= strstr(attr
, "/*/");
654 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
659 FOREACH_DIRENT_ALL(dent
, dir
, break)
660 if (dent
->d_name
[0] != '.') {
661 char n
[strlen(dent
->d_name
) + strlen(tail
) + 1];
663 strscpyl(n
, sizeof n
, dent
->d_name
, tail
, NULL
);
664 if (faccessat(dirfd(dir
), n
, F_OK
, 0) == 0) {
665 strscpyl(attr
, len
, path
, n
, NULL
);
671 static int get_key(char **line
, char **key
, enum operation_type
*op
, char **value
) {
677 if (!linepos
|| linepos
[0] == '\0')
680 /* skip whitespace */
681 while (isspace(linepos
[0]) || linepos
[0] == ',')
685 if (linepos
[0] == '\0')
691 if (linepos
[0] == '\0')
693 if (isspace(linepos
[0]))
695 if (linepos
[0] == '=')
697 if (IN_SET(linepos
[0], '+', '-', '!', ':'))
698 if (linepos
[1] == '=')
702 /* remember end of key */
705 /* skip whitespace after key */
706 while (isspace(linepos
[0]))
708 if (linepos
[0] == '\0')
711 /* get operation type */
712 if (linepos
[0] == '=' && linepos
[1] == '=') {
715 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
718 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
721 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
724 } else if (linepos
[0] == '=') {
727 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
728 *op
= OP_ASSIGN_FINAL
;
736 /* skip whitespace after operator */
737 while (isspace(linepos
[0]))
739 if (linepos
[0] == '\0')
743 if (linepos
[0] == '"')
750 for (i
= 0, j
= 0; ; i
++, j
++) {
752 if (linepos
[i
] == '"')
755 if (linepos
[i
] == '\0')
758 /* double quotes can be escaped */
759 if (linepos
[i
] == '\\')
760 if (linepos
[i
+1] == '"')
763 linepos
[j
] = linepos
[i
];
767 /* move line to next key */
768 *line
= linepos
+ i
+ 1;
772 /* extract possible KEY{attr} */
773 static const char *get_key_attribute(char *str
) {
777 attr
= strchr(str
, '{');
780 pos
= strchr(attr
, '}');
782 log_error("Missing closing brace for format");
791 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
792 enum operation_type op
,
793 const char *value
, const void *data
) {
794 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
795 const char *attr
= NULL
;
797 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
))
800 memzero(token
, sizeof(struct token
));
812 case TK_M_SUBSYSTEMS
:
816 case TK_M_IMPORT_FILE
:
817 case TK_M_IMPORT_PROG
:
819 case TK_M_IMPORT_CMDLINE
:
820 case TK_M_IMPORT_PARENT
:
830 case TK_A_STATIC_NODE
:
831 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
833 case TK_M_IMPORT_BUILTIN
:
834 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
835 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
846 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
847 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
850 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
852 token
->key
.mode
= *(mode_t
*)data
;
854 case TK_A_STRING_ESCAPE_NONE
:
855 case TK_A_STRING_ESCAPE_REPLACE
:
856 case TK_A_DB_PERSIST
:
858 case TK_A_RUN_BUILTIN
:
859 case TK_A_RUN_PROGRAM
:
860 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
861 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
863 case TK_A_INOTIFY_WATCH
:
864 case TK_A_DEVLINK_PRIO
:
865 token
->key
.devlink_prio
= *(int *)data
;
868 token
->key
.uid
= *(uid_t
*)data
;
871 token
->key
.gid
= *(gid_t
*)data
;
874 token
->key
.mode
= *(mode_t
*)data
;
877 case TK_M_PARENTS_MIN
:
878 case TK_M_PARENTS_MAX
:
882 assert_not_reached("wrong type");
885 if (value
&& type
< TK_M_MAX
) {
886 /* check if we need to split or call fnmatch() while matching rules */
887 enum string_glob_type glob
;
888 bool has_split
, has_glob
;
890 has_split
= strchr(value
, '|');
891 has_glob
= string_is_glob(value
);
892 if (has_split
&& has_glob
)
893 glob
= GL_SPLIT_GLOB
;
897 if (streq(value
, "?*"))
904 token
->key
.glob
= glob
;
907 if (value
&& type
> TK_M_MAX
) {
908 /* check if assigned value has substitution chars */
910 token
->key
.subst
= SB_SUBSYS
;
911 else if (strchr(value
, '%') || strchr(value
, '$'))
912 token
->key
.subst
= SB_FORMAT
;
914 token
->key
.subst
= SB_NONE
;
918 /* check if property/attribute name has substitution chars */
920 token
->key
.attrsubst
= SB_SUBSYS
;
921 else if (strchr(attr
, '%') || strchr(attr
, '$'))
922 token
->key
.attrsubst
= SB_FORMAT
;
924 token
->key
.attrsubst
= SB_NONE
;
927 token
->key
.type
= type
;
929 rule_tmp
->token_cur
++;
934 static int sort_token(UdevRules
*rules
, struct rule_tmp
*rule_tmp
) {
937 size_t end
= rule_tmp
->token_cur
;
940 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
941 enum token_type next_val
= TK_UNSET
;
945 /* find smallest value */
946 for (j
= start
; j
< end
; j
++) {
947 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
949 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
950 next_val
= rule_tmp
->token
[j
].type
;
955 /* add token and mark done */
956 r
= add_token(rules
, &rule_tmp
->token
[next_idx
]);
959 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
962 if (next_idx
== start
)
964 if (next_idx
+1 == end
)
970 #define LOG_RULE_FULL(level, fmt, ...) log_full(level, "%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
971 #define LOG_RULE_ERROR(fmt, ...) LOG_RULE_FULL(LOG_ERR, fmt, ##__VA_ARGS__)
972 #define LOG_RULE_WARNING(fmt, ...) LOG_RULE_FULL(LOG_WARNING, fmt, ##__VA_ARGS__)
973 #define LOG_RULE_DEBUG(fmt, ...) LOG_RULE_FULL(LOG_DEBUG, fmt, ##__VA_ARGS__)
974 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
975 #define LOG_AND_RETURN_ADD_KEY LOG_AND_RETURN("Temporary rule array too small, aborting event processing with %zu items", rule_tmp.token_cur);
977 static void add_rule(UdevRules
*rules
, char *line
,
978 const char *filename
, unsigned filename_off
, unsigned lineno
) {
981 struct rule_tmp rule_tmp
= {
983 .rule
.type
= TK_RULE
,
987 /* the offset in the rule is limited to unsigned short */
988 if (filename_off
< USHRT_MAX
)
989 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
990 rule_tmp
.rule
.rule
.filename_line
= lineno
;
996 enum operation_type op
;
998 if (get_key(&linepos
, &key
, &op
, &value
) < 0) {
999 /* Avoid erroring on trailing whitespace. This is probably rare
1000 * so save the work for the error case instead of always trying
1001 * to strip the trailing whitespace with strstrip(). */
1002 while (isblank(*linepos
))
1005 /* If we aren't at the end of the line, this is a parsing error.
1006 * Make a best effort to describe where the problem is. */
1007 if (!strchr(NEWLINE
, *linepos
)) {
1008 char buf
[2] = {*linepos
};
1009 _cleanup_free_
char *tmp
;
1012 LOG_RULE_ERROR("Invalid key/value pair, starting at character %tu ('%s')", linepos
- line
+ 1, tmp
);
1013 if (*linepos
== '#')
1014 LOG_RULE_ERROR("Hint: comments can only start at beginning of line");
1019 if (streq(key
, "ACTION")) {
1020 if (op
> OP_MATCH_MAX
)
1021 LOG_AND_RETURN("Invalid %s operation", key
);
1023 if (rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
) < 0)
1024 LOG_AND_RETURN_ADD_KEY
;
1026 } else if (streq(key
, "DEVPATH")) {
1027 if (op
> OP_MATCH_MAX
)
1028 LOG_AND_RETURN("Invalid %s operation", key
);
1030 if (rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
) < 0)
1031 LOG_AND_RETURN_ADD_KEY
;
1033 } else if (streq(key
, "KERNEL")) {
1034 if (op
> OP_MATCH_MAX
)
1035 LOG_AND_RETURN("Invalid %s operation", key
);
1037 if (rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
) < 0)
1038 LOG_AND_RETURN_ADD_KEY
;
1040 } else if (streq(key
, "SUBSYSTEM")) {
1041 if (op
> OP_MATCH_MAX
)
1042 LOG_AND_RETURN("Invalid %s operation", key
);
1044 /* bus, class, subsystem events should all be the same */
1045 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1046 if (!streq(value
, "subsystem"))
1047 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1049 r
= rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1051 r
= rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1053 LOG_AND_RETURN_ADD_KEY
;
1055 } else if (streq(key
, "DRIVER")) {
1056 if (op
> OP_MATCH_MAX
)
1057 LOG_AND_RETURN("Invalid %s operation", key
);
1059 if (rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
) < 0)
1060 LOG_AND_RETURN_ADD_KEY
;
1062 } else if (startswith(key
, "ATTR{")) {
1063 attr
= get_key_attribute(key
+ STRLEN("ATTR"));
1065 LOG_AND_RETURN("Failed to parse %s attribute", "ATTR");
1067 if (op
== OP_REMOVE
)
1068 LOG_AND_RETURN("Invalid %s operation", "ATTR");
1070 if (op
< OP_MATCH_MAX
)
1071 r
= rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1073 r
= rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1075 LOG_AND_RETURN_ADD_KEY
;
1077 } else if (startswith(key
, "SYSCTL{")) {
1078 attr
= get_key_attribute(key
+ STRLEN("SYSCTL"));
1080 LOG_AND_RETURN("Failed to parse %s attribute", "ATTR");
1082 if (op
== OP_REMOVE
)
1083 LOG_AND_RETURN("Invalid %s operation", "ATTR");
1085 if (op
< OP_MATCH_MAX
)
1086 r
= rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1088 r
= rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1090 LOG_AND_RETURN_ADD_KEY
;
1092 } else if (startswith(key
, "SECLABEL{")) {
1093 attr
= get_key_attribute(key
+ STRLEN("SECLABEL"));
1095 LOG_AND_RETURN("Failed to parse %s attribute", "SECLABEL");
1097 if (op
== OP_REMOVE
)
1098 LOG_AND_RETURN("Invalid %s operation", "SECLABEL");
1100 if (rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
) < 0)
1101 LOG_AND_RETURN_ADD_KEY
;
1103 } else if (streq(key
, "KERNELS")) {
1104 if (op
> OP_MATCH_MAX
)
1105 LOG_AND_RETURN("Invalid %s operation", key
);
1107 if (rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
) < 0)
1108 LOG_AND_RETURN_ADD_KEY
;
1110 } else if (streq(key
, "SUBSYSTEMS")) {
1111 if (op
> OP_MATCH_MAX
)
1112 LOG_AND_RETURN("Invalid %s operation", key
);
1114 if (rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
) < 0)
1115 LOG_AND_RETURN_ADD_KEY
;
1117 } else if (streq(key
, "DRIVERS")) {
1118 if (op
> OP_MATCH_MAX
)
1119 LOG_AND_RETURN("Invalid %s operation", key
);
1121 if (rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
) < 0)
1122 LOG_AND_RETURN_ADD_KEY
;
1124 } else if (startswith(key
, "ATTRS{")) {
1125 if (op
> OP_MATCH_MAX
)
1126 LOG_AND_RETURN("Invalid %s operation", "ATTRS");
1128 attr
= get_key_attribute(key
+ STRLEN("ATTRS"));
1130 LOG_AND_RETURN("Failed to parse %s attribute", "ATTRS");
1132 if (startswith(attr
, "device/"))
1133 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1134 if (strstr(attr
, "../"))
1135 LOG_RULE_WARNING("Direct reference to parent sysfs directory, may break in future kernels; please fix");
1136 if (rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
) < 0)
1137 LOG_AND_RETURN_ADD_KEY
;
1139 } else if (streq(key
, "TAGS")) {
1140 if (op
> OP_MATCH_MAX
)
1141 LOG_AND_RETURN("Invalid %s operation", key
);
1143 if (rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
) < 0)
1144 LOG_AND_RETURN_ADD_KEY
;
1146 } else if (startswith(key
, "ENV{")) {
1147 attr
= get_key_attribute(key
+ STRLEN("ENV"));
1149 LOG_AND_RETURN("Failed to parse %s attribute", "ENV");
1151 if (op
== OP_REMOVE
)
1152 LOG_AND_RETURN("Invalid %s operation", "ENV");
1154 if (op
< OP_MATCH_MAX
)
1155 r
= rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1157 if (STR_IN_SET(attr
,
1169 LOG_AND_RETURN("Invalid ENV attribute, '%s' cannot be set", attr
);
1171 r
= rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1174 LOG_AND_RETURN_ADD_KEY
;
1176 } else if (streq(key
, "TAG")) {
1177 if (op
< OP_MATCH_MAX
)
1178 r
= rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1180 r
= rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1182 LOG_AND_RETURN_ADD_KEY
;
1184 } else if (streq(key
, "PROGRAM")) {
1185 if (op
== OP_REMOVE
)
1186 LOG_AND_RETURN("Invalid %s operation", key
);
1188 if (rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
) < 0)
1189 LOG_AND_RETURN_ADD_KEY
;
1191 } else if (streq(key
, "RESULT")) {
1192 if (op
> OP_MATCH_MAX
)
1193 LOG_AND_RETURN("Invalid %s operation", key
);
1195 if (rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
) < 0)
1196 LOG_AND_RETURN_ADD_KEY
;
1198 } else if (startswith(key
, "IMPORT")) {
1199 attr
= get_key_attribute(key
+ STRLEN("IMPORT"));
1201 LOG_RULE_WARNING("Ignoring IMPORT{} with missing type");
1204 if (op
== OP_REMOVE
)
1205 LOG_AND_RETURN("Invalid %s operation", "IMPORT");
1207 if (streq(attr
, "program")) {
1208 /* find known built-in command */
1209 if (value
[0] != '/') {
1210 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1213 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1214 if (rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
) < 0)
1215 LOG_AND_RETURN_ADD_KEY
;
1219 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1220 } else if (streq(attr
, "builtin")) {
1221 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1224 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown, ignoring", value
);
1227 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1228 } else if (streq(attr
, "file"))
1229 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1230 else if (streq(attr
, "db"))
1231 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1232 else if (streq(attr
, "cmdline"))
1233 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1234 else if (streq(attr
, "parent"))
1235 r
= rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1237 LOG_RULE_ERROR("Ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1241 LOG_AND_RETURN_ADD_KEY
;
1243 } else if (startswith(key
, "TEST")) {
1246 if (op
> OP_MATCH_MAX
)
1247 LOG_AND_RETURN("Invalid %s operation", "TEST");
1249 attr
= get_key_attribute(key
+ STRLEN("TEST"));
1251 mode
= strtol(attr
, NULL
, 8);
1252 r
= rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1254 r
= rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1256 LOG_AND_RETURN_ADD_KEY
;
1258 } else if (startswith(key
, "RUN")) {
1259 attr
= get_key_attribute(key
+ STRLEN("RUN"));
1262 if (op
== OP_REMOVE
)
1263 LOG_AND_RETURN("Invalid %s operation", "RUN");
1265 if (streq(attr
, "builtin")) {
1266 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1269 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown, ignoring", value
);
1272 r
= rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1273 } else if (streq(attr
, "program")) {
1274 const enum udev_builtin_cmd cmd
= _UDEV_BUILTIN_MAX
;
1276 r
= rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1278 LOG_RULE_ERROR("Ignoring unknown %s{} type '%s'", "RUN", attr
);
1282 LOG_AND_RETURN_ADD_KEY
;
1284 } else if (streq(key
, "LABEL")) {
1285 if (op
== OP_REMOVE
)
1286 LOG_AND_RETURN("Invalid %s operation", key
);
1288 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1290 } else if (streq(key
, "GOTO")) {
1291 if (op
== OP_REMOVE
)
1292 LOG_AND_RETURN("Invalid %s operation", key
);
1294 if (rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
) < 0)
1295 LOG_AND_RETURN_ADD_KEY
;
1297 } else if (startswith(key
, "NAME")) {
1298 if (op
== OP_REMOVE
)
1299 LOG_AND_RETURN("Invalid %s operation", key
);
1301 if (op
< OP_MATCH_MAX
)
1302 r
= rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1304 if (streq(value
, "%k")) {
1305 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1308 if (isempty(value
)) {
1309 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1312 r
= rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1315 LOG_AND_RETURN_ADD_KEY
;
1316 rule_tmp
.rule
.rule
.can_set_name
= true;
1318 } else if (streq(key
, "SYMLINK")) {
1319 if (op
== OP_REMOVE
)
1320 LOG_AND_RETURN("Invalid %s operation", key
);
1322 if (op
< OP_MATCH_MAX
)
1323 r
= rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1325 r
= rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1327 LOG_AND_RETURN_ADD_KEY
;
1328 rule_tmp
.rule
.rule
.can_set_name
= true;
1330 } else if (streq(key
, "OWNER")) {
1333 if (op
== OP_REMOVE
)
1334 LOG_AND_RETURN("Invalid %s operation", key
);
1336 if (parse_uid(value
, &uid
) >= 0)
1337 r
= rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1338 else if (rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
&& !strchr("$%", value
[0])) {
1339 uid
= add_uid(rules
, value
);
1340 r
= rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1341 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1342 r
= rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1344 LOG_RULE_DEBUG("Resolving user name is disabled, ignoring %s=%s", key
, value
);
1348 LOG_AND_RETURN_ADD_KEY
;
1350 rule_tmp
.rule
.rule
.can_set_name
= true;
1352 } else if (streq(key
, "GROUP")) {
1355 if (op
== OP_REMOVE
)
1356 LOG_AND_RETURN("Invalid %s operation", key
);
1358 if (parse_gid(value
, &gid
) >= 0)
1359 r
= rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1360 else if ((rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
) && !strchr("$%", value
[0])) {
1361 gid
= add_gid(rules
, value
);
1362 r
= rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1363 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1364 r
= rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1366 LOG_RULE_DEBUG("Resolving group name is disabled, ignoring %s=%s", key
, value
);
1370 LOG_AND_RETURN_ADD_KEY
;
1372 rule_tmp
.rule
.rule
.can_set_name
= true;
1374 } else if (streq(key
, "MODE")) {
1378 if (op
== OP_REMOVE
)
1379 LOG_AND_RETURN("Invalid %s operation", key
);
1381 mode
= strtol(value
, &endptr
, 8);
1382 if (endptr
[0] == '\0')
1383 r
= rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1385 r
= rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1387 LOG_AND_RETURN_ADD_KEY
;
1389 rule_tmp
.rule
.rule
.can_set_name
= true;
1391 } else if (streq(key
, "OPTIONS")) {
1394 if (op
== OP_REMOVE
)
1395 LOG_AND_RETURN("Invalid %s operation", key
);
1397 pos
= strstr(value
, "link_priority=");
1399 int prio
= atoi(pos
+ STRLEN("link_priority="));
1401 if (rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
) < 0)
1402 LOG_AND_RETURN_ADD_KEY
;
1405 pos
= strstr(value
, "string_escape=");
1407 pos
+= STRLEN("string_escape=");
1408 if (startswith(pos
, "none"))
1409 r
= rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1410 else if (startswith(pos
, "replace"))
1411 r
= rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1413 LOG_RULE_ERROR("OPTIONS: unknown string_escape mode '%s', ignoring", pos
);
1417 LOG_AND_RETURN_ADD_KEY
;
1420 pos
= strstr(value
, "db_persist");
1422 if (rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
) < 0)
1423 LOG_AND_RETURN_ADD_KEY
;
1425 pos
= strstr(value
, "nowatch");
1427 static const int zero
= 0;
1428 if (rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &zero
) < 0)
1429 LOG_AND_RETURN_ADD_KEY
;
1431 static const int one
= 1;
1432 pos
= strstr(value
, "watch");
1434 if (rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &one
) < 0)
1435 LOG_AND_RETURN_ADD_KEY
;
1438 pos
= strstr(value
, "static_node=");
1440 pos
+= STRLEN("static_node=");
1441 if (rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
) < 0)
1442 LOG_AND_RETURN_ADD_KEY
;
1443 rule_tmp
.rule
.rule
.has_static_node
= true;
1447 LOG_AND_RETURN("Unknown key '%s'", key
);
1450 /* add rule token and sort tokens */
1451 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1452 if (add_token(rules
, &rule_tmp
.rule
) < 0 || sort_token(rules
, &rule_tmp
) < 0)
1453 LOG_RULE_ERROR("Failed to add rule token");
1456 static int parse_file(UdevRules
*rules
, const char *filename
) {
1457 _cleanup_free_
char *continuation
= NULL
;
1458 _cleanup_fclose_
FILE *f
= NULL
;
1459 bool ignore_line
= false;
1460 size_t first_token
, i
;
1461 unsigned filename_off
;
1464 f
= fopen(filename
, "re");
1466 if (errno
== ENOENT
)
1472 if (null_or_empty_fd(fileno(f
))) {
1473 log_debug("Skipping empty file: %s", filename
);
1476 log_debug("Reading rules file: %s", filename
);
1478 first_token
= rules
->token_cur
;
1479 filename_off
= rules_add_string(rules
, filename
);
1482 _cleanup_free_
char *buf
= NULL
;
1486 r
= read_line(f
, UTIL_LINE_SIZE
, &buf
);
1493 line
= buf
+ strspn(buf
, WHITESPACE
);
1500 if (continuation
&& !ignore_line
) {
1501 if (strlen(continuation
) + len
>= UTIL_LINE_SIZE
)
1504 if (!strextend(&continuation
, line
, NULL
))
1508 line
= continuation
;
1513 if (len
> 0 && line
[len
- 1] == '\\') {
1517 line
[len
- 1] = '\0';
1518 if (!continuation
) {
1519 continuation
= strdup(line
);
1528 log_error("Line too long '%s':%u, ignored", filename
, line_nr
);
1530 add_rule(rules
, line
, filename
, filename_off
, line_nr
);
1532 continuation
= mfree(continuation
);
1533 ignore_line
= false;
1536 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1537 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1538 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1539 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1542 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1543 if (rules
->tokens
[j
].type
!= TK_RULE
)
1545 if (rules
->tokens
[j
].rule
.label_off
== 0)
1547 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1549 rules
->tokens
[i
].key
.rule_goto
= j
;
1552 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1553 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1559 int udev_rules_new(UdevRules
**ret_rules
, ResolveNameTiming resolve_name_timing
) {
1560 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
1561 _cleanup_strv_free_
char **files
= NULL
;
1565 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1567 rules
= new(UdevRules
, 1);
1571 *rules
= (UdevRules
) {
1572 .resolve_name_timing
= resolve_name_timing
,
1575 /* init token array and string buffer */
1576 rules
->tokens
= new(struct token
, PREALLOC_TOKEN
);
1579 rules
->token_max
= PREALLOC_TOKEN
;
1581 rules
->strbuf
= strbuf_new();
1585 udev_rules_check_timestamp(rules
);
1587 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, RULES_DIRS
);
1589 return log_error_errno(r
, "Failed to enumerate rules files: %m");
1592 * The offset value in the rules strct is limited; add all
1593 * rules file names to the beginning of the string buffer.
1595 STRV_FOREACH(f
, files
)
1596 rules_add_string(rules
, *f
);
1598 STRV_FOREACH(f
, files
)
1599 parse_file(rules
, *f
);
1601 struct token end_token
= { .type
= TK_END
};
1602 add_token(rules
, &end_token
);
1603 log_debug("Rules contain %zu bytes tokens (%zu * %zu bytes), %zu bytes strings",
1604 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1606 /* cleanup temporary strbuf data */
1607 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1608 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1609 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1610 strbuf_complete(rules
->strbuf
);
1612 /* cleanup uid/gid cache */
1613 rules
->uids
= mfree(rules
->uids
);
1614 rules
->uids_cur
= 0;
1615 rules
->uids_max
= 0;
1616 rules
->gids
= mfree(rules
->gids
);
1617 rules
->gids_cur
= 0;
1618 rules
->gids_max
= 0;
1621 *ret_rules
= TAKE_PTR(rules
);
1625 UdevRules
*udev_rules_free(UdevRules
*rules
) {
1628 free(rules
->tokens
);
1629 strbuf_cleanup(rules
->strbuf
);
1632 return mfree(rules
);
1635 bool udev_rules_check_timestamp(UdevRules
*rules
) {
1639 return paths_check_timestamp(RULES_DIRS
, &rules
->dirs_ts_usec
, true);
1642 static bool match_key(UdevRules
*rules
, struct token
*token
, const char *val
) {
1643 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1650 switch (token
->key
.glob
) {
1652 match
= streq(key_value
, val
);
1655 match
= (fnmatch(key_value
, val
, 0) == 0);
1662 s
= rules_str(rules
, token
->key
.value_off
);
1667 next
= strchr(s
, '|');
1669 size_t matchlen
= (size_t)(next
- s
);
1671 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1675 match
= streq(s
, val
);
1684 char value
[UTIL_PATH_SIZE
];
1686 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1689 pos
= strchr(key_value
, '|');
1694 match
= (fnmatch(key_value
, val
, 0) == 0);
1702 match
= (val
[0] != '\0');
1708 return token
->key
.op
== (match
? OP_MATCH
: OP_NOMATCH
);
1711 static bool match_attr(UdevRules
*rules
, sd_device
*dev
, UdevEvent
*event
, struct token
*cur
) {
1712 char nbuf
[UTIL_NAME_SIZE
], vbuf
[UTIL_NAME_SIZE
];
1713 const char *name
, *value
;
1716 name
= rules_str(rules
, cur
->key
.attr_off
);
1717 switch (cur
->key
.attrsubst
) {
1719 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1723 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1727 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) < 0)
1735 /* remove trailing whitespace, if not asked to match for it */
1736 len
= strlen(value
);
1737 if (len
> 0 && isspace(value
[len
-1])) {
1738 const char *key_value
;
1741 key_value
= rules_str(rules
, cur
->key
.value_off
);
1742 klen
= strlen(key_value
);
1743 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1744 if (value
!= vbuf
) {
1745 strscpy(vbuf
, sizeof(vbuf
), value
);
1748 while (len
> 0 && isspace(vbuf
[--len
]))
1753 return match_key(rules
, cur
, value
);
1762 int udev_rules_apply_to_event(
1765 usec_t timeout_usec
,
1766 Hashmap
*properties_list
) {
1767 sd_device
*dev
= event
->dev
;
1768 enum escape_type esc
= ESCAPE_UNSET
;
1769 struct token
*cur
, *rule
;
1770 const char *action
, *val
;
1777 r
= sd_device_get_property_value(dev
, "ACTION", &action
);
1781 can_set_name
= (!streq(action
, "remove") &&
1782 (sd_device_get_devnum(dev
, NULL
) >= 0 ||
1783 sd_device_get_ifindex(dev
, NULL
) >= 0));
1785 /* loop through token list, match, run actions or forward to next rule */
1786 cur
= &rules
->tokens
[0];
1789 dump_token(rules
, cur
);
1790 switch (cur
->type
) {
1794 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1795 if (!can_set_name
&& rule
->rule
.can_set_name
)
1800 if (!match_key(rules
, cur
, action
))
1804 if (sd_device_get_devpath(dev
, &val
) < 0)
1806 if (!match_key(rules
, cur
, val
))
1810 if (sd_device_get_sysname(dev
, &val
) < 0)
1812 if (!match_key(rules
, cur
, val
))
1815 case TK_M_DEVLINK
: {
1816 const char *devlink
;
1819 FOREACH_DEVICE_DEVLINK(dev
, devlink
)
1820 if (match_key(rules
, cur
, devlink
+ STRLEN("/dev/"))) {
1830 if (!match_key(rules
, cur
, event
->name
))
1834 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1836 if (sd_device_get_property_value(dev
, key_name
, &val
) < 0) {
1837 /* check global properties */
1838 if (properties_list
)
1839 val
= hashmap_get(properties_list
, key_name
);
1844 if (!match_key(rules
, cur
, strempty(val
)))
1852 FOREACH_DEVICE_TAG(dev
, tag
)
1853 if (streq(rules_str(rules
, cur
->key
.value_off
), tag
)) {
1858 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1859 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1863 case TK_M_SUBSYSTEM
:
1864 if (sd_device_get_subsystem(dev
, &val
) < 0)
1866 if (!match_key(rules
, cur
, val
))
1870 if (sd_device_get_driver(dev
, &val
) < 0)
1872 if (!match_key(rules
, cur
, val
))
1876 if (!match_attr(rules
, dev
, event
, cur
))
1880 char filename
[UTIL_PATH_SIZE
];
1881 _cleanup_free_
char *value
= NULL
;
1884 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1885 sysctl_normalize(filename
);
1886 if (sysctl_read(filename
, &value
) < 0)
1889 len
= strlen(value
);
1890 while (len
> 0 && isspace(value
[--len
]))
1892 if (!match_key(rules
, cur
, value
))
1897 case TK_M_SUBSYSTEMS
:
1903 /* get whole sequence of parent matches */
1905 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1908 /* loop over parents */
1909 event
->dev_parent
= dev
;
1913 /* loop over sequence of parent match keys */
1914 for (key
= cur
; key
< next
; key
++ ) {
1915 dump_token(rules
, key
);
1918 if (sd_device_get_sysname(event
->dev_parent
, &val
) < 0)
1920 if (!match_key(rules
, key
, val
))
1923 case TK_M_SUBSYSTEMS
:
1924 if (sd_device_get_subsystem(event
->dev_parent
, &val
) < 0)
1926 if (!match_key(rules
, key
, val
))
1930 if (sd_device_get_driver(event
->dev_parent
, &val
) < 0)
1932 if (!match_key(rules
, key
, val
))
1936 if (!match_attr(rules
, event
->dev_parent
, event
, key
))
1940 bool match
= sd_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1942 if (match
&& key
->key
.op
== OP_NOMATCH
)
1944 if (!match
&& key
->key
.op
== OP_MATCH
)
1955 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
1956 event
->dev_parent
= NULL
;
1960 /* move behind our sequence of parent match keys */
1965 char filename
[UTIL_PATH_SIZE
];
1966 struct stat statbuf
;
1969 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1970 if (util_resolve_subsys_kernel(filename
, filename
, sizeof(filename
), false) < 0) {
1971 if (filename
[0] != '/') {
1972 char tmp
[UTIL_PATH_SIZE
];
1974 if (sd_device_get_syspath(dev
, &val
) < 0)
1977 strscpy(tmp
, sizeof(tmp
), filename
);
1978 strscpyl(filename
, sizeof(filename
), val
, "/", tmp
, NULL
);
1981 attr_subst_subdir(filename
, sizeof(filename
));
1983 match
= (stat(filename
, &statbuf
) == 0);
1984 if (match
&& cur
->key
.mode
> 0)
1985 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1986 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1988 if (!match
&& cur
->key
.op
== OP_MATCH
)
1992 case TK_M_PROGRAM
: {
1993 char program
[UTIL_PATH_SIZE
], result
[UTIL_LINE_SIZE
];
1995 event
->program_result
= mfree(event
->program_result
);
1996 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1997 log_device_debug(dev
, "PROGRAM '%s' %s:%u",
1999 rules_str(rules
, rule
->rule
.filename_off
),
2000 rule
->rule
.filename_line
);
2002 if (udev_event_spawn(event
, timeout_usec
, true, program
, result
, sizeof(result
)) != 0) {
2003 if (cur
->key
.op
!= OP_NOMATCH
)
2008 delete_trailing_chars(result
, "\n");
2009 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2010 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2012 log_device_debug(dev
, "Replaced %i character(s) from result of '%s'" , count
, program
);
2014 event
->program_result
= strdup(result
);
2015 if (cur
->key
.op
== OP_NOMATCH
)
2020 case TK_M_IMPORT_FILE
: {
2021 char import
[UTIL_PATH_SIZE
];
2023 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2024 if (import_file_into_properties(dev
, import
) < 0)
2025 if (cur
->key
.op
!= OP_NOMATCH
)
2029 case TK_M_IMPORT_PROG
: {
2030 char import
[UTIL_PATH_SIZE
];
2032 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2033 log_device_debug(dev
, "IMPORT '%s' %s:%u",
2035 rules_str(rules
, rule
->rule
.filename_off
),
2036 rule
->rule
.filename_line
);
2038 if (import_program_into_properties(event
, timeout_usec
, import
) < 0)
2039 if (cur
->key
.op
!= OP_NOMATCH
)
2043 case TK_M_IMPORT_BUILTIN
: {
2044 char command
[UTIL_PATH_SIZE
];
2046 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2047 /* check if we ran already */
2048 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2049 log_device_debug(dev
, "IMPORT builtin skip '%s' %s:%u",
2050 udev_builtin_name(cur
->key
.builtin_cmd
),
2051 rules_str(rules
, rule
->rule
.filename_off
),
2052 rule
->rule
.filename_line
);
2053 /* return the result from earlier run */
2054 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2055 if (cur
->key
.op
!= OP_NOMATCH
)
2060 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2063 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2064 log_device_debug(dev
, "IMPORT builtin '%s' %s:%u",
2065 udev_builtin_name(cur
->key
.builtin_cmd
),
2066 rules_str(rules
, rule
->rule
.filename_off
),
2067 rule
->rule
.filename_line
);
2069 r
= udev_builtin_run(dev
, cur
->key
.builtin_cmd
, command
, false);
2071 /* remember failure */
2072 log_device_debug_errno(dev
, r
, "IMPORT builtin '%s' fails: %m",
2073 udev_builtin_name(cur
->key
.builtin_cmd
));
2074 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2075 if (cur
->key
.op
!= OP_NOMATCH
)
2080 case TK_M_IMPORT_DB
: {
2083 key
= rules_str(rules
, cur
->key
.value_off
);
2084 if (event
->dev_db_clone
&&
2085 sd_device_get_property_value(event
->dev_db_clone
, key
, &val
) >= 0)
2086 device_add_property(dev
, key
, val
);
2087 else if (cur
->key
.op
!= OP_NOMATCH
)
2091 case TK_M_IMPORT_CMDLINE
: {
2092 _cleanup_free_
char *value
= NULL
;
2093 bool imported
= false;
2096 key
= rules_str(rules
, cur
->key
.value_off
);
2097 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2099 log_device_debug_errno(dev
, r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2104 device_add_property(dev
, key
, value
);
2106 /* we import simple flags as 'FLAG=1' */
2107 device_add_property(dev
, key
, "1");
2110 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2114 case TK_M_IMPORT_PARENT
: {
2115 char import
[UTIL_PATH_SIZE
];
2117 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2118 if (import_parent_into_properties(dev
, import
) < 0)
2119 if (cur
->key
.op
!= OP_NOMATCH
)
2124 if (!match_key(rules
, cur
, event
->program_result
))
2127 case TK_A_STRING_ESCAPE_NONE
:
2130 case TK_A_STRING_ESCAPE_REPLACE
:
2131 esc
= ESCAPE_REPLACE
;
2133 case TK_A_DB_PERSIST
:
2134 device_set_db_persist(dev
);
2136 case TK_A_INOTIFY_WATCH
:
2137 if (event
->inotify_watch_final
)
2139 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2140 event
->inotify_watch_final
= true;
2141 event
->inotify_watch
= cur
->key
.watch
;
2143 case TK_A_DEVLINK_PRIO
:
2144 device_set_devlink_priority(dev
, cur
->key
.devlink_prio
);
2147 char owner
[UTIL_NAME_SIZE
];
2148 const char *ow
= owner
;
2150 if (event
->owner_final
)
2152 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2153 event
->owner_final
= true;
2154 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2155 event
->owner_set
= true;
2156 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2158 log_unknown_owner(dev
, r
, "user", owner
);
2161 log_device_debug(dev
, "OWNER %u %s:%u",
2163 rules_str(rules
, rule
->rule
.filename_off
),
2164 rule
->rule
.filename_line
);
2168 char group
[UTIL_NAME_SIZE
];
2169 const char *gr
= group
;
2171 if (event
->group_final
)
2173 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2174 event
->group_final
= true;
2175 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2176 event
->group_set
= true;
2177 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2179 log_unknown_owner(dev
, r
, "group", group
);
2182 log_device_debug(dev
, "GROUP %u %s:%u",
2184 rules_str(rules
, rule
->rule
.filename_off
),
2185 rule
->rule
.filename_line
);
2189 char mode_str
[UTIL_NAME_SIZE
];
2192 if (event
->mode_final
)
2194 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2195 r
= parse_mode(mode_str
, &mode
);
2197 log_device_error_errno(dev
, r
, "Failed to parse mode '%s': %m", mode_str
);
2200 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2201 event
->mode_final
= true;
2202 event
->mode_set
= true;
2204 log_device_debug(dev
, "MODE %#o %s:%u",
2206 rules_str(rules
, rule
->rule
.filename_off
),
2207 rule
->rule
.filename_line
);
2211 if (event
->owner_final
)
2213 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2214 event
->owner_final
= true;
2215 event
->owner_set
= true;
2216 event
->uid
= cur
->key
.uid
;
2217 log_device_debug(dev
, "OWNER %u %s:%u",
2219 rules_str(rules
, rule
->rule
.filename_off
),
2220 rule
->rule
.filename_line
);
2223 if (event
->group_final
)
2225 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2226 event
->group_final
= true;
2227 event
->group_set
= true;
2228 event
->gid
= cur
->key
.gid
;
2229 log_device_debug(dev
, "GROUP %u %s:%u",
2231 rules_str(rules
, rule
->rule
.filename_off
),
2232 rule
->rule
.filename_line
);
2235 if (event
->mode_final
)
2237 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2238 event
->mode_final
= true;
2239 event
->mode_set
= true;
2240 event
->mode
= cur
->key
.mode
;
2241 log_device_debug(dev
, "MODE %#o %s:%u",
2243 rules_str(rules
, rule
->rule
.filename_off
),
2244 rule
->rule
.filename_line
);
2246 case TK_A_SECLABEL
: {
2247 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2248 char label_str
[UTIL_LINE_SIZE
] = {};
2250 name
= strdup(rules_str(rules
, cur
->key
.attr_off
));
2254 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2255 if (!isempty(label_str
))
2256 label
= strdup(label_str
);
2258 label
= strdup(rules_str(rules
, cur
->key
.value_off
));
2262 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2263 hashmap_clear_free_free(event
->seclabel_list
);
2265 r
= hashmap_ensure_allocated(&event
->seclabel_list
, NULL
);
2269 r
= hashmap_put(event
->seclabel_list
, name
, label
);
2272 log_device_debug(dev
, "SECLABEL{%s}='%s' %s:%u",
2274 rules_str(rules
, rule
->rule
.filename_off
),
2275 rule
->rule
.filename_line
);
2276 name
= label
= NULL
;
2281 char value_new
[UTIL_NAME_SIZE
];
2282 const char *name
, *value_old
;
2284 name
= rules_str(rules
, cur
->key
.attr_off
);
2285 val
= rules_str(rules
, cur
->key
.value_off
);
2286 if (val
[0] == '\0') {
2287 if (cur
->key
.op
== OP_ADD
)
2289 device_add_property(dev
, name
, NULL
);
2293 if (cur
->key
.op
== OP_ADD
&&
2294 sd_device_get_property_value(dev
, name
, &value_old
) >= 0) {
2295 char temp
[UTIL_NAME_SIZE
];
2297 /* append value separated by space */
2298 udev_event_apply_format(event
, val
, temp
, sizeof(temp
), false);
2299 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2301 udev_event_apply_format(event
, val
, value_new
, sizeof(value_new
), false);
2303 device_add_property(dev
, name
, value_new
);
2307 char tag
[UTIL_PATH_SIZE
];
2310 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2311 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2312 device_cleanup_tags(dev
);
2313 for (p
= tag
; *p
!= '\0'; p
++) {
2314 if ((*p
>= 'a' && *p
<= 'z') ||
2315 (*p
>= 'A' && *p
<= 'Z') ||
2316 (*p
>= '0' && *p
<= '9') ||
2317 IN_SET(*p
, '-', '_'))
2319 log_device_error(dev
, "Ignoring invalid tag name '%s'", tag
);
2322 if (cur
->key
.op
== OP_REMOVE
)
2323 device_remove_tag(dev
, tag
);
2325 device_add_tag(dev
, tag
);
2329 char name_str
[UTIL_PATH_SIZE
];
2333 name
= rules_str(rules
, cur
->key
.value_off
);
2334 if (event
->name_final
)
2336 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2337 event
->name_final
= true;
2338 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2339 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2340 count
= util_replace_chars(name_str
, "/");
2342 log_device_debug(dev
, "Replaced %i character(s) from result of NAME=\"%s\"", count
, name
);
2344 if (sd_device_get_devnum(dev
, NULL
) >= 0 &&
2345 (sd_device_get_devname(dev
, &val
) < 0 ||
2346 !streq(name_str
, val
+ STRLEN("/dev/")))) {
2347 log_device_error(dev
, "Kernel device nodes cannot be renamed, ignoring NAME=\"%s\"; please fix it in %s:%u\n",
2349 rules_str(rules
, rule
->rule
.filename_off
),
2350 rule
->rule
.filename_line
);
2353 if (free_and_strdup(&event
->name
, name_str
) < 0)
2356 log_device_debug(dev
, "NAME '%s' %s:%u",
2358 rules_str(rules
, rule
->rule
.filename_off
),
2359 rule
->rule
.filename_line
);
2362 case TK_A_DEVLINK
: {
2363 char temp
[UTIL_PATH_SIZE
], filename
[UTIL_PATH_SIZE
], *pos
, *next
;
2366 if (event
->devlink_final
)
2368 if (sd_device_get_devnum(dev
, NULL
) < 0)
2370 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2371 event
->devlink_final
= true;
2372 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2373 device_cleanup_devlinks(dev
);
2375 /* allow multiple symlinks separated by spaces */
2376 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2377 if (esc
== ESCAPE_UNSET
)
2378 count
= util_replace_chars(temp
, "/ ");
2379 else if (esc
== ESCAPE_REPLACE
)
2380 count
= util_replace_chars(temp
, "/");
2382 log_device_debug(dev
, "Replaced %i character(s) from result of LINK" , count
);
2384 while (isspace(pos
[0]))
2386 next
= strchr(pos
, ' ');
2389 log_device_debug(dev
, "LINK '%s' %s:%u", pos
,
2390 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2391 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2392 device_add_devlink(dev
, filename
);
2393 while (isspace(next
[1]))
2396 next
= strchr(pos
, ' ');
2398 if (pos
[0] != '\0') {
2399 log_device_debug(dev
, "LINK '%s' %s:%u", pos
,
2400 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2401 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2402 device_add_devlink(dev
, filename
);
2407 char attr
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2408 _cleanup_fclose_
FILE *f
= NULL
;
2409 const char *key_name
;
2411 key_name
= rules_str(rules
, cur
->key
.attr_off
);
2412 if (util_resolve_subsys_kernel(key_name
, attr
, sizeof(attr
), false) < 0 &&
2413 sd_device_get_syspath(dev
, &val
) >= 0)
2414 strscpyl(attr
, sizeof(attr
), val
, "/", key_name
, NULL
);
2415 attr_subst_subdir(attr
, sizeof(attr
));
2417 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2418 log_device_debug(dev
, "ATTR '%s' writing '%s' %s:%u", attr
, value
,
2419 rules_str(rules
, rule
->rule
.filename_off
),
2420 rule
->rule
.filename_line
);
2421 f
= fopen(attr
, "we");
2423 log_device_error_errno(dev
, errno
, "Failed to open ATTR{%s} for writing: %m", attr
);
2424 else if (fprintf(f
, "%s", value
) <= 0)
2425 log_device_error_errno(dev
, errno
, "Failed to write ATTR{%s}: %m", attr
);
2429 char filename
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2431 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2432 sysctl_normalize(filename
);
2433 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2434 log_device_debug(dev
, "SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2435 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2436 r
= sysctl_write(filename
, value
);
2438 log_device_error_errno(dev
, r
, "Failed to write SYSCTL{%s}='%s': %m", filename
, value
);
2441 case TK_A_RUN_BUILTIN
:
2442 case TK_A_RUN_PROGRAM
: {
2443 _cleanup_free_
char *cmd
= NULL
;
2445 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2446 hashmap_clear_free_key(event
->run_list
);
2448 r
= hashmap_ensure_allocated(&event
->run_list
, NULL
);
2452 cmd
= strdup(rules_str(rules
, cur
->key
.value_off
));
2456 r
= hashmap_put(event
->run_list
, cmd
, INT_TO_PTR(cur
->key
.builtin_cmd
));
2462 log_device_debug(dev
, "RUN '%s' %s:%u",
2463 rules_str(rules
, cur
->key
.value_off
),
2464 rules_str(rules
, rule
->rule
.filename_off
),
2465 rule
->rule
.filename_line
);
2469 if (cur
->key
.rule_goto
== 0)
2471 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2476 case TK_M_PARENTS_MIN
:
2477 case TK_M_PARENTS_MAX
:
2480 log_device_error(dev
, "Wrong type %u", cur
->type
);
2487 /* fast-forward to next rule */
2488 cur
= rule
+ rule
->rule
.token_count
;
2494 int udev_rules_apply_static_dev_perms(UdevRules
*rules
) {
2500 _cleanup_strv_free_
char **tags
= NULL
;
2503 _cleanup_free_
char *path
= NULL
;
2509 cur
= &rules
->tokens
[0];
2512 switch (cur
->type
) {
2517 /* skip rules without a static_node tag */
2518 if (!rule
->rule
.has_static_node
)
2524 tags
= strv_free(tags
);
2533 mode
= cur
->key
.mode
;
2536 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2541 case TK_A_STATIC_NODE
: {
2542 char device_node
[UTIL_PATH_SIZE
];
2543 char tags_dir
[UTIL_PATH_SIZE
];
2544 char tag_symlink
[UTIL_PATH_SIZE
];
2547 /* we assure, that the permissions tokens are sorted before the static token */
2549 if (mode
== 0 && uid
== 0 && gid
== 0 && !tags
)
2552 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2553 if (stat(device_node
, &stats
) < 0)
2555 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2558 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2560 STRV_FOREACH(t
, tags
) {
2561 _cleanup_free_
char *unescaped_filename
= NULL
;
2563 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2564 r
= mkdir_p(tags_dir
, 0755);
2566 return log_error_errno(r
, "Failed to create %s: %m", tags_dir
);
2568 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2570 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2571 r
= symlink(device_node
, tag_symlink
);
2572 if (r
< 0 && errno
!= EEXIST
)
2573 return log_error_errno(errno
, "Failed to create symlink %s -> %s: %m",
2574 tag_symlink
, device_node
);
2578 /* don't touch the permissions if only the tags were set */
2579 if (mode
== 0 && uid
== 0 && gid
== 0)
2588 if (mode
!= (stats
.st_mode
& 01777)) {
2589 r
= chmod(device_node
, mode
);
2591 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2594 log_debug("chmod '%s' %#o", device_node
, mode
);
2597 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2598 r
= chown(device_node
, uid
, gid
);
2600 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2601 device_node
, uid
, gid
);
2603 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2606 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2616 /* fast-forward to next rule */
2617 cur
= rule
+ rule
->rule
.token_count
;
2624 fchmod(fileno(f
), 0644);
2625 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2626 unlink_noerrno("/run/udev/static_node-tags");
2627 unlink_noerrno(path
);