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 "path-util.h"
29 #include "proc-cmdline.h"
30 #include "stat-util.h"
31 #include "stdio-util.h"
33 #include "string-util.h"
36 #include "sysctl-util.h"
37 #include "udev-builtin.h"
39 #include "user-util.h"
42 #define PREALLOC_TOKEN 2048
52 static const char* const rules_dirs
[] = {
55 UDEVLIBEXECDIR
"/rules.d",
61 ResolveNameTiming resolve_name_timing
;
63 /* every key in the rules file becomes a token */
68 /* all key strings are copied and de-duplicated in a single continuous string buffer */
69 struct strbuf
*strbuf
;
71 /* during rule parsing, uid/gid lookup results are cached */
80 static char *rules_str(struct udev_rules
*rules
, unsigned off
) {
81 return rules
->strbuf
->buf
+ off
;
84 static unsigned rules_add_string(struct udev_rules
*rules
, const char *s
) {
85 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
88 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
102 enum string_glob_type
{
104 GL_PLAIN
, /* no special chars */
105 GL_GLOB
, /* shell globs ?,*,[] */
106 GL_SPLIT
, /* multi-value A|B */
107 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
108 GL_SOMETHING
, /* commonly used "?*" */
111 enum string_subst_type
{
118 /* tokens of a rule are sorted/handled in this order */
123 TK_M_ACTION
, /* val */
124 TK_M_DEVPATH
, /* val */
125 TK_M_KERNEL
, /* val */
126 TK_M_DEVLINK
, /* val */
128 TK_M_ENV
, /* val, attr */
130 TK_M_SUBSYSTEM
, /* val */
131 TK_M_DRIVER
, /* val */
132 TK_M_WAITFOR
, /* val */
133 TK_M_ATTR
, /* val, attr */
134 TK_M_SYSCTL
, /* val, attr */
137 TK_M_KERNELS
, /* val */
138 TK_M_SUBSYSTEMS
, /* val */
139 TK_M_DRIVERS
, /* val */
140 TK_M_ATTRS
, /* val, attr */
144 TK_M_TEST
, /* val, mode_t */
145 TK_M_PROGRAM
, /* val */
146 TK_M_IMPORT_FILE
, /* val */
147 TK_M_IMPORT_PROG
, /* val */
148 TK_M_IMPORT_BUILTIN
, /* val */
149 TK_M_IMPORT_DB
, /* val */
150 TK_M_IMPORT_CMDLINE
, /* val */
151 TK_M_IMPORT_PARENT
, /* val */
152 TK_M_RESULT
, /* val */
155 TK_A_STRING_ESCAPE_NONE
,
156 TK_A_STRING_ESCAPE_REPLACE
,
158 TK_A_INOTIFY_WATCH
, /* int */
159 TK_A_DEVLINK_PRIO
, /* int */
160 TK_A_OWNER
, /* val */
161 TK_A_GROUP
, /* val */
163 TK_A_OWNER_ID
, /* uid_t */
164 TK_A_GROUP_ID
, /* gid_t */
165 TK_A_MODE_ID
, /* mode_t */
167 TK_A_STATIC_NODE
, /* val */
168 TK_A_SECLABEL
, /* val, attr */
169 TK_A_ENV
, /* val, attr */
171 TK_A_DEVLINK
, /* val */
172 TK_A_ATTR
, /* val, attr */
173 TK_A_SYSCTL
, /* val, attr */
174 TK_A_RUN_BUILTIN
, /* val, bool */
175 TK_A_RUN_PROGRAM
, /* val, bool */
176 TK_A_GOTO
, /* size_t */
181 /* we try to pack stuff in a way that we take only 12 bytes per token */
184 unsigned char type
; /* same in rule and key */
186 enum token_type type
:8;
188 bool has_static_node
:1;
190 unsigned short token_count
;
192 unsigned short filename_off
;
193 unsigned short filename_line
;
196 enum token_type type
:8;
197 enum operation_type op
:8;
198 enum string_glob_type glob
:8;
199 enum string_subst_type subst
:4;
200 enum string_subst_type attrsubst
:4;
210 enum udev_builtin_cmd builtin_cmd
;
218 struct udev_rules
*rules
;
220 struct token token
[MAX_TK
];
225 static const char *operation_str(enum operation_type type
) {
226 static const char *operation_strs
[] = {
227 [OP_UNSET
] = "UNSET",
228 [OP_MATCH
] = "match",
229 [OP_NOMATCH
] = "nomatch",
230 [OP_MATCH_MAX
] = "MATCH_MAX",
233 [OP_REMOVE
] = "remove",
234 [OP_ASSIGN
] = "assign",
235 [OP_ASSIGN_FINAL
] = "assign-final",
238 return operation_strs
[type
];
241 static const char *string_glob_str(enum string_glob_type type
) {
242 static const char *string_glob_strs
[] = {
243 [GL_UNSET
] = "UNSET",
244 [GL_PLAIN
] = "plain",
246 [GL_SPLIT
] = "split",
247 [GL_SPLIT_GLOB
] = "split-glob",
248 [GL_SOMETHING
] = "split-glob",
251 return string_glob_strs
[type
];
254 static const char *token_str(enum token_type type
) {
255 static const char *token_strs
[] = {
256 [TK_UNSET
] = "UNSET",
259 [TK_M_ACTION
] = "M ACTION",
260 [TK_M_DEVPATH
] = "M DEVPATH",
261 [TK_M_KERNEL
] = "M KERNEL",
262 [TK_M_DEVLINK
] = "M DEVLINK",
263 [TK_M_NAME
] = "M NAME",
264 [TK_M_ENV
] = "M ENV",
265 [TK_M_TAG
] = "M TAG",
266 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
267 [TK_M_DRIVER
] = "M DRIVER",
268 [TK_M_WAITFOR
] = "M WAITFOR",
269 [TK_M_ATTR
] = "M ATTR",
270 [TK_M_SYSCTL
] = "M SYSCTL",
272 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
273 [TK_M_KERNELS
] = "M KERNELS",
274 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
275 [TK_M_DRIVERS
] = "M DRIVERS",
276 [TK_M_ATTRS
] = "M ATTRS",
277 [TK_M_TAGS
] = "M TAGS",
278 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
280 [TK_M_TEST
] = "M TEST",
281 [TK_M_PROGRAM
] = "M PROGRAM",
282 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
283 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
284 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
285 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
286 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
287 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
288 [TK_M_RESULT
] = "M RESULT",
289 [TK_M_MAX
] = "M MAX",
291 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
292 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
293 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
294 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
295 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
296 [TK_A_OWNER
] = "A OWNER",
297 [TK_A_GROUP
] = "A GROUP",
298 [TK_A_MODE
] = "A MODE",
299 [TK_A_OWNER_ID
] = "A OWNER_ID",
300 [TK_A_GROUP_ID
] = "A GROUP_ID",
301 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
302 [TK_A_SECLABEL
] = "A SECLABEL",
303 [TK_A_MODE_ID
] = "A MODE_ID",
304 [TK_A_ENV
] = "A ENV",
305 [TK_A_TAG
] = "A ENV",
306 [TK_A_NAME
] = "A NAME",
307 [TK_A_DEVLINK
] = "A DEVLINK",
308 [TK_A_ATTR
] = "A ATTR",
309 [TK_A_SYSCTL
] = "A SYSCTL",
310 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
311 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
312 [TK_A_GOTO
] = "A GOTO",
317 return token_strs
[type
];
320 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
321 enum token_type type
= token
->type
;
322 enum operation_type op
= token
->key
.op
;
323 enum string_glob_type glob
= token
->key
.glob
;
324 const char *value
= rules_str(rules
, token
->key
.value_off
);
325 const char *attr
= &rules
->strbuf
->buf
[token
->key
.attr_off
];
330 const char *tks_ptr
= (char *)rules
->tokens
;
331 const char *tk_ptr
= (char *)token
;
332 unsigned idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
334 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
335 &rules
->strbuf
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
336 idx
, token
->rule
.token_count
,
337 &rules
->strbuf
->buf
[token
->rule
.label_off
]);
349 case TK_M_SUBSYSTEMS
:
353 case TK_M_IMPORT_FILE
:
354 case TK_M_IMPORT_PROG
:
356 case TK_M_IMPORT_CMDLINE
:
357 case TK_M_IMPORT_PARENT
:
364 case TK_A_RUN_BUILTIN
:
365 case TK_A_RUN_PROGRAM
:
366 log_debug("%s %s '%s'(%s)",
367 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
369 case TK_M_IMPORT_BUILTIN
:
370 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
379 log_debug("%s %s '%s' '%s'(%s)",
380 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
384 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
386 case TK_A_STRING_ESCAPE_NONE
:
387 case TK_A_STRING_ESCAPE_REPLACE
:
388 case TK_A_DB_PERSIST
:
389 log_debug("%s", token_str(type
));
392 log_debug("%s %s '%s'(%s) %#o",
393 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
395 case TK_A_INOTIFY_WATCH
:
396 log_debug("%s %u", token_str(type
), token
->key
.watch
);
398 case TK_A_DEVLINK_PRIO
:
399 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
402 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
405 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
408 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
410 case TK_A_STATIC_NODE
:
411 log_debug("%s '%s'", token_str(type
), value
);
414 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
417 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
420 log_debug("* %s", token_str(type
));
422 case TK_M_PARENTS_MIN
:
423 case TK_M_PARENTS_MAX
:
426 log_debug("unknown type %u", type
);
431 static void dump_rules(struct udev_rules
*rules
) {
434 log_debug("dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings",
436 rules
->token_cur
* sizeof(struct token
),
437 rules
->strbuf
->nodes_count
,
439 for (i
= 0; i
< rules
->token_cur
; i
++)
440 dump_token(rules
, &rules
->tokens
[i
]);
443 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
444 static inline void dump_rules(struct udev_rules
*rules
) {}
447 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
448 /* grow buffer if needed */
449 if (rules
->token_cur
+1 >= rules
->token_max
) {
450 struct token
*tokens
;
453 /* double the buffer size */
454 add
= rules
->token_max
;
458 tokens
= reallocarray(rules
->tokens
, rules
->token_max
+ add
, sizeof(struct token
));
461 rules
->tokens
= tokens
;
462 rules
->token_max
+= add
;
464 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
469 static void log_unknown_owner(int error
, const char *entity
, const char *owner
) {
470 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
471 log_error("Specified %s '%s' unknown", entity
, owner
);
473 log_error_errno(error
, "Error resolving %s '%s': %m", entity
, owner
);
476 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
482 /* lookup, if we know it already */
483 for (i
= 0; i
< rules
->uids_cur
; i
++) {
484 off
= rules
->uids
[i
].name_off
;
485 if (streq(rules_str(rules
, off
), owner
)) {
486 uid
= rules
->uids
[i
].uid
;
490 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
492 log_unknown_owner(r
, "user", owner
);
494 /* grow buffer if needed */
495 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
496 struct uid_gid
*uids
;
499 /* double the buffer size */
500 add
= rules
->uids_max
;
504 uids
= reallocarray(rules
->uids
, rules
->uids_max
+ add
, sizeof(struct uid_gid
));
508 rules
->uids_max
+= add
;
510 rules
->uids
[rules
->uids_cur
].uid
= uid
;
511 off
= rules_add_string(rules
, owner
);
514 rules
->uids
[rules
->uids_cur
].name_off
= off
;
519 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
525 /* lookup, if we know it already */
526 for (i
= 0; i
< rules
->gids_cur
; i
++) {
527 off
= rules
->gids
[i
].name_off
;
528 if (streq(rules_str(rules
, off
), group
)) {
529 gid
= rules
->gids
[i
].gid
;
533 r
= get_group_creds(&group
, &gid
, USER_CREDS_ALLOW_MISSING
);
535 log_unknown_owner(r
, "group", group
);
537 /* grow buffer if needed */
538 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
539 struct uid_gid
*gids
;
542 /* double the buffer size */
543 add
= rules
->gids_max
;
547 gids
= reallocarray(rules
->gids
, rules
->gids_max
+ add
, sizeof(struct uid_gid
));
551 rules
->gids_max
+= add
;
553 rules
->gids
[rules
->gids_cur
].gid
= gid
;
554 off
= rules_add_string(rules
, group
);
557 rules
->gids
[rules
->gids_cur
].name_off
= off
;
562 static int import_property_from_string(sd_device
*dev
, char *line
) {
569 while (isspace(key
[0]))
572 /* comment or empty line */
573 if (IN_SET(key
[0], '#', '\0'))
576 /* split key/value */
577 val
= strchr(key
, '=');
584 while (isspace(val
[0]))
591 while (isspace(key
[len
-1]))
595 /* terminate value */
599 while (isspace(val
[len
-1]))
607 if (IN_SET(val
[0], '"', '\'')) {
608 if (len
== 1 || val
[len
-1] != val
[0]) {
609 log_debug("inconsistent quoting: '%s', skip", line
);
616 return device_add_property(dev
, key
, val
);
619 static int import_file_into_properties(sd_device
*dev
, const char *filename
) {
620 _cleanup_fclose_
FILE *f
= NULL
;
623 f
= fopen(filename
, "re");
628 _cleanup_free_
char *line
= NULL
;
630 r
= read_line(f
, LONG_LINE_MAX
, &line
);
636 (void) import_property_from_string(dev
, line
);
642 static int import_program_into_properties(struct udev_event
*event
,
644 const char *program
) {
645 char result
[UTIL_LINE_SIZE
];
649 err
= udev_event_spawn(event
, timeout_usec
, true, program
, result
, sizeof(result
));
654 while (line
!= NULL
) {
657 pos
= strchr(line
, '\n');
662 (void) import_property_from_string(event
->dev
, line
);
668 static int import_parent_into_properties(sd_device
*dev
, const char *filter
) {
669 const char *key
, *val
;
676 r
= sd_device_get_parent(dev
, &parent
);
680 FOREACH_DEVICE_PROPERTY(parent
, key
, val
)
681 if (fnmatch(filter
, key
, 0) == 0)
682 device_add_property(dev
, key
, val
);
686 static void attr_subst_subdir(char *attr
, size_t len
) {
687 const char *pos
, *tail
, *path
;
688 _cleanup_closedir_
DIR *dir
= NULL
;
691 pos
= strstr(attr
, "/*/");
696 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
701 FOREACH_DIRENT_ALL(dent
, dir
, break)
702 if (dent
->d_name
[0] != '.') {
703 char n
[strlen(dent
->d_name
) + strlen(tail
) + 1];
705 strscpyl(n
, sizeof n
, dent
->d_name
, tail
, NULL
);
706 if (faccessat(dirfd(dir
), n
, F_OK
, 0) == 0) {
707 strscpyl(attr
, len
, path
, n
, NULL
);
713 static int get_key(char **line
, char **key
, enum operation_type
*op
, char **value
) {
719 if (linepos
== NULL
|| linepos
[0] == '\0')
722 /* skip whitespace */
723 while (isspace(linepos
[0]) || linepos
[0] == ',')
727 if (linepos
[0] == '\0')
733 if (linepos
[0] == '\0')
735 if (isspace(linepos
[0]))
737 if (linepos
[0] == '=')
739 if (IN_SET(linepos
[0], '+', '-', '!', ':'))
740 if (linepos
[1] == '=')
744 /* remember end of key */
747 /* skip whitespace after key */
748 while (isspace(linepos
[0]))
750 if (linepos
[0] == '\0')
753 /* get operation type */
754 if (linepos
[0] == '=' && linepos
[1] == '=') {
757 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
760 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
763 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
766 } else if (linepos
[0] == '=') {
769 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
770 *op
= OP_ASSIGN_FINAL
;
778 /* skip whitespace after operator */
779 while (isspace(linepos
[0]))
781 if (linepos
[0] == '\0')
785 if (linepos
[0] == '"')
792 for (i
= 0, j
= 0; ; i
++, j
++) {
794 if (linepos
[i
] == '"')
797 if (linepos
[i
] == '\0')
800 /* double quotes can be escaped */
801 if (linepos
[i
] == '\\')
802 if (linepos
[i
+1] == '"')
805 linepos
[j
] = linepos
[i
];
809 /* move line to next key */
810 *line
= linepos
+ i
+ 1;
814 /* extract possible KEY{attr} */
815 static const char *get_key_attribute(char *str
) {
819 attr
= strchr(str
, '{');
822 pos
= strchr(attr
, '}');
824 log_error("Missing closing brace for format");
833 static void rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
834 enum operation_type op
,
835 const char *value
, const void *data
) {
836 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
837 const char *attr
= NULL
;
839 assert(rule_tmp
->token_cur
< ELEMENTSOF(rule_tmp
->token
));
840 memzero(token
, sizeof(struct token
));
852 case TK_M_SUBSYSTEMS
:
856 case TK_M_IMPORT_FILE
:
857 case TK_M_IMPORT_PROG
:
859 case TK_M_IMPORT_CMDLINE
:
860 case TK_M_IMPORT_PARENT
:
870 case TK_A_STATIC_NODE
:
871 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
873 case TK_M_IMPORT_BUILTIN
:
874 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
875 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
886 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
887 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
890 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
892 token
->key
.mode
= *(mode_t
*)data
;
894 case TK_A_STRING_ESCAPE_NONE
:
895 case TK_A_STRING_ESCAPE_REPLACE
:
896 case TK_A_DB_PERSIST
:
898 case TK_A_RUN_BUILTIN
:
899 case TK_A_RUN_PROGRAM
:
900 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
901 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
903 case TK_A_INOTIFY_WATCH
:
904 case TK_A_DEVLINK_PRIO
:
905 token
->key
.devlink_prio
= *(int *)data
;
908 token
->key
.uid
= *(uid_t
*)data
;
911 token
->key
.gid
= *(gid_t
*)data
;
914 token
->key
.mode
= *(mode_t
*)data
;
917 case TK_M_PARENTS_MIN
:
918 case TK_M_PARENTS_MAX
:
922 assert_not_reached("wrong type");
925 if (value
!= NULL
&& type
< TK_M_MAX
) {
926 /* check if we need to split or call fnmatch() while matching rules */
927 enum string_glob_type glob
;
931 has_split
= (strchr(value
, '|') != NULL
);
932 has_glob
= string_is_glob(value
);
933 if (has_split
&& has_glob
) {
934 glob
= GL_SPLIT_GLOB
;
935 } else if (has_split
) {
937 } else if (has_glob
) {
938 if (streq(value
, "?*"))
945 token
->key
.glob
= glob
;
948 if (value
!= NULL
&& type
> TK_M_MAX
) {
949 /* check if assigned value has substitution chars */
951 token
->key
.subst
= SB_SUBSYS
;
952 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
953 token
->key
.subst
= SB_FORMAT
;
955 token
->key
.subst
= SB_NONE
;
959 /* check if property/attribute name has substitution chars */
961 token
->key
.attrsubst
= SB_SUBSYS
;
962 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
963 token
->key
.attrsubst
= SB_FORMAT
;
965 token
->key
.attrsubst
= SB_NONE
;
968 token
->key
.type
= type
;
970 rule_tmp
->token_cur
++;
973 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
976 unsigned end
= rule_tmp
->token_cur
;
978 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
979 enum token_type next_val
= TK_UNSET
;
980 unsigned next_idx
= 0;
983 /* find smallest value */
984 for (j
= start
; j
< end
; j
++) {
985 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
987 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
988 next_val
= rule_tmp
->token
[j
].type
;
993 /* add token and mark done */
994 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
996 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
999 if (next_idx
== start
)
1001 if (next_idx
+1 == end
)
1007 #define LOG_RULE_ERROR(fmt, ...) log_error("Invalid rule %s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1008 #define LOG_RULE_WARNING(fmt, ...) log_warning("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1009 #define LOG_RULE_DEBUG(fmt, ...) log_debug("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1010 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1012 static void add_rule(struct udev_rules
*rules
, char *line
,
1013 const char *filename
, unsigned filename_off
, unsigned lineno
) {
1016 struct rule_tmp rule_tmp
= {
1018 .rule
.type
= TK_RULE
,
1021 /* the offset in the rule is limited to unsigned short */
1022 if (filename_off
< USHRT_MAX
)
1023 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1024 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1030 enum operation_type op
;
1032 if (get_key(&linepos
, &key
, &op
, &value
) != 0) {
1033 /* Avoid erroring on trailing whitespace. This is probably rare
1034 * so save the work for the error case instead of always trying
1035 * to strip the trailing whitespace with strstrip(). */
1036 while (isblank(*linepos
))
1039 /* If we aren't at the end of the line, this is a parsing error.
1040 * Make a best effort to describe where the problem is. */
1041 if (!strchr(NEWLINE
, *linepos
)) {
1042 char buf
[2] = {*linepos
};
1043 _cleanup_free_
char *tmp
;
1046 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1047 filename
, lineno
, linepos
- line
+ 1, tmp
);
1048 if (*linepos
== '#')
1049 log_error("hint: comments can only start at beginning of line");
1054 if (rule_tmp
.token_cur
>= ELEMENTSOF(rule_tmp
.token
))
1055 LOG_AND_RETURN("temporary rule array too small, aborting event processing with %u items", rule_tmp
.token_cur
);
1057 if (streq(key
, "ACTION")) {
1058 if (op
> OP_MATCH_MAX
)
1059 LOG_AND_RETURN("invalid %s operation", key
);
1061 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1063 } else if (streq(key
, "DEVPATH")) {
1064 if (op
> OP_MATCH_MAX
)
1065 LOG_AND_RETURN("invalid %s operation", key
);
1067 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1069 } else if (streq(key
, "KERNEL")) {
1070 if (op
> OP_MATCH_MAX
)
1071 LOG_AND_RETURN("invalid %s operation", key
);
1073 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1075 } else if (streq(key
, "SUBSYSTEM")) {
1076 if (op
> OP_MATCH_MAX
)
1077 LOG_AND_RETURN("invalid %s operation", key
);
1079 /* bus, class, subsystem events should all be the same */
1080 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1081 if (!streq(value
, "subsystem"))
1082 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1084 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1086 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1088 } else if (streq(key
, "DRIVER")) {
1089 if (op
> OP_MATCH_MAX
)
1090 LOG_AND_RETURN("invalid %s operation", key
);
1092 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1094 } else if (startswith(key
, "ATTR{")) {
1095 attr
= get_key_attribute(key
+ STRLEN("ATTR"));
1097 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1099 if (op
== OP_REMOVE
)
1100 LOG_AND_RETURN("invalid %s operation", "ATTR");
1102 if (op
< OP_MATCH_MAX
)
1103 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1105 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1107 } else if (startswith(key
, "SYSCTL{")) {
1108 attr
= get_key_attribute(key
+ STRLEN("SYSCTL"));
1110 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1112 if (op
== OP_REMOVE
)
1113 LOG_AND_RETURN("invalid %s operation", "ATTR");
1115 if (op
< OP_MATCH_MAX
)
1116 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1118 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1120 } else if (startswith(key
, "SECLABEL{")) {
1121 attr
= get_key_attribute(key
+ STRLEN("SECLABEL"));
1123 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1125 if (op
== OP_REMOVE
)
1126 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1128 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1130 } else if (streq(key
, "KERNELS")) {
1131 if (op
> OP_MATCH_MAX
)
1132 LOG_AND_RETURN("invalid %s operation", key
);
1134 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1136 } else if (streq(key
, "SUBSYSTEMS")) {
1137 if (op
> OP_MATCH_MAX
)
1138 LOG_AND_RETURN("invalid %s operation", key
);
1140 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1142 } else if (streq(key
, "DRIVERS")) {
1143 if (op
> OP_MATCH_MAX
)
1144 LOG_AND_RETURN("invalid %s operation", key
);
1146 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1148 } else if (startswith(key
, "ATTRS{")) {
1149 if (op
> OP_MATCH_MAX
)
1150 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1152 attr
= get_key_attribute(key
+ STRLEN("ATTRS"));
1154 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1156 if (startswith(attr
, "device/"))
1157 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1158 if (strstr(attr
, "../") != NULL
)
1159 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1160 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1162 } else if (streq(key
, "TAGS")) {
1163 if (op
> OP_MATCH_MAX
)
1164 LOG_AND_RETURN("invalid %s operation", key
);
1166 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1168 } else if (startswith(key
, "ENV{")) {
1169 attr
= get_key_attribute(key
+ STRLEN("ENV"));
1171 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1173 if (op
== OP_REMOVE
)
1174 LOG_AND_RETURN("invalid %s operation", "ENV");
1176 if (op
< OP_MATCH_MAX
)
1177 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1179 if (STR_IN_SET(attr
,
1191 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr
);
1193 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1196 } else if (streq(key
, "TAG")) {
1197 if (op
< OP_MATCH_MAX
)
1198 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1200 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1202 } else if (streq(key
, "PROGRAM")) {
1203 if (op
== OP_REMOVE
)
1204 LOG_AND_RETURN("invalid %s operation", key
);
1206 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1208 } else if (streq(key
, "RESULT")) {
1209 if (op
> OP_MATCH_MAX
)
1210 LOG_AND_RETURN("invalid %s operation", key
);
1212 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1214 } else if (startswith(key
, "IMPORT")) {
1215 attr
= get_key_attribute(key
+ STRLEN("IMPORT"));
1217 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1220 if (op
== OP_REMOVE
)
1221 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1223 if (streq(attr
, "program")) {
1224 /* find known built-in command */
1225 if (value
[0] != '/') {
1226 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1229 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1230 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1234 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1235 } else if (streq(attr
, "builtin")) {
1236 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1239 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1241 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1242 } else if (streq(attr
, "file"))
1243 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1244 else if (streq(attr
, "db"))
1245 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1246 else if (streq(attr
, "cmdline"))
1247 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1248 else if (streq(attr
, "parent"))
1249 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1251 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1253 } else if (startswith(key
, "TEST")) {
1256 if (op
> OP_MATCH_MAX
)
1257 LOG_AND_RETURN("invalid %s operation", "TEST");
1259 attr
= get_key_attribute(key
+ STRLEN("TEST"));
1261 mode
= strtol(attr
, NULL
, 8);
1262 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1264 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1266 } else if (startswith(key
, "RUN")) {
1267 attr
= get_key_attribute(key
+ STRLEN("RUN"));
1270 if (op
== OP_REMOVE
)
1271 LOG_AND_RETURN("invalid %s operation", "RUN");
1273 if (streq(attr
, "builtin")) {
1274 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1277 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1279 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1280 } else if (streq(attr
, "program")) {
1281 const enum udev_builtin_cmd cmd
= _UDEV_BUILTIN_MAX
;
1283 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1285 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr
);
1287 } else if (streq(key
, "LABEL")) {
1288 if (op
== OP_REMOVE
)
1289 LOG_AND_RETURN("invalid %s operation", key
);
1291 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1293 } else if (streq(key
, "GOTO")) {
1294 if (op
== OP_REMOVE
)
1295 LOG_AND_RETURN("invalid %s operation", key
);
1297 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1299 } else if (startswith(key
, "NAME")) {
1300 if (op
== OP_REMOVE
)
1301 LOG_AND_RETURN("invalid %s operation", key
);
1303 if (op
< OP_MATCH_MAX
)
1304 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1306 if (streq(value
, "%k")) {
1307 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1310 if (isempty(value
)) {
1311 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1314 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
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 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1325 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1326 rule_tmp
.rule
.rule
.can_set_name
= true;
1328 } else if (streq(key
, "OWNER")) {
1332 if (op
== OP_REMOVE
)
1333 LOG_AND_RETURN("invalid %s operation", key
);
1335 uid
= strtoul(value
, &endptr
, 10);
1336 if (endptr
[0] == '\0')
1337 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]) == NULL
) {
1339 uid
= add_uid(rules
, value
);
1340 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1341 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1342 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1344 rule_tmp
.rule
.rule
.can_set_name
= true;
1346 } else if (streq(key
, "GROUP")) {
1350 if (op
== OP_REMOVE
)
1351 LOG_AND_RETURN("invalid %s operation", key
);
1353 gid
= strtoul(value
, &endptr
, 10);
1354 if (endptr
[0] == '\0')
1355 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1356 else if ((rules
->resolve_name_timing
== RESOLVE_NAME_EARLY
) && strchr("$%", value
[0]) == NULL
) {
1357 gid
= add_gid(rules
, value
);
1358 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1359 } else if (rules
->resolve_name_timing
!= RESOLVE_NAME_NEVER
)
1360 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1362 rule_tmp
.rule
.rule
.can_set_name
= true;
1364 } else if (streq(key
, "MODE")) {
1368 if (op
== OP_REMOVE
)
1369 LOG_AND_RETURN("invalid %s operation", key
);
1371 mode
= strtol(value
, &endptr
, 8);
1372 if (endptr
[0] == '\0')
1373 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1375 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1376 rule_tmp
.rule
.rule
.can_set_name
= true;
1378 } else if (streq(key
, "OPTIONS")) {
1381 if (op
== OP_REMOVE
)
1382 LOG_AND_RETURN("invalid %s operation", key
);
1384 pos
= strstr(value
, "link_priority=");
1386 int prio
= atoi(pos
+ STRLEN("link_priority="));
1388 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1391 pos
= strstr(value
, "string_escape=");
1393 pos
+= STRLEN("string_escape=");
1394 if (startswith(pos
, "none"))
1395 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1396 else if (startswith(pos
, "replace"))
1397 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1400 pos
= strstr(value
, "db_persist");
1402 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1404 pos
= strstr(value
, "nowatch");
1406 static const int zero
= 0;
1407 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &zero
);
1409 static const int one
= 1;
1410 pos
= strstr(value
, "watch");
1412 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &one
);
1415 pos
= strstr(value
, "static_node=");
1417 pos
+= STRLEN("static_node=");
1418 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1419 rule_tmp
.rule
.rule
.has_static_node
= true;
1423 LOG_AND_RETURN("unknown key '%s'", key
);
1426 /* add rule token and sort tokens */
1427 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1428 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1429 LOG_RULE_ERROR("failed to add rule token");
1432 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1433 _cleanup_fclose_
FILE *f
= NULL
;
1434 unsigned first_token
;
1435 unsigned filename_off
;
1436 char line
[UTIL_LINE_SIZE
];
1440 f
= fopen(filename
, "re");
1442 if (errno
== ENOENT
)
1448 if (null_or_empty_fd(fileno(f
))) {
1449 log_debug("Skipping empty file: %s", filename
);
1452 log_debug("Reading rules file: %s", filename
);
1454 first_token
= rules
->token_cur
;
1455 filename_off
= rules_add_string(rules
, filename
);
1457 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1461 /* skip whitespace */
1464 while (isspace(key
[0]))
1475 /* continue reading if backslash+newline is found */
1476 while (line
[len
-2] == '\\') {
1477 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1479 if (strlen(&line
[len
-2]) < 2)
1485 if (len
+1 >= sizeof(line
)) {
1486 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1489 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1492 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1493 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1494 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1495 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1498 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1499 if (rules
->tokens
[j
].type
!= TK_RULE
)
1501 if (rules
->tokens
[j
].rule
.label_off
== 0)
1503 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1505 rules
->tokens
[i
].key
.rule_goto
= j
;
1508 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1509 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1515 struct udev_rules
*udev_rules_new(ResolveNameTiming resolve_name_timing
) {
1516 struct udev_rules
*rules
;
1517 struct token end_token
;
1521 assert(resolve_name_timing
>= 0 && resolve_name_timing
< _RESOLVE_NAME_TIMING_MAX
);
1523 rules
= new(struct udev_rules
, 1);
1527 *rules
= (struct udev_rules
) {
1528 .resolve_name_timing
= resolve_name_timing
,
1531 /* init token array and string buffer */
1532 rules
->tokens
= malloc_multiply(PREALLOC_TOKEN
, sizeof(struct token
));
1533 if (rules
->tokens
== NULL
)
1534 return udev_rules_unref(rules
);
1535 rules
->token_max
= PREALLOC_TOKEN
;
1537 rules
->strbuf
= strbuf_new();
1539 return udev_rules_unref(rules
);
1541 udev_rules_check_timestamp(rules
);
1543 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1545 log_error_errno(r
, "failed to enumerate rules files: %m");
1546 return udev_rules_unref(rules
);
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
);
1561 memzero(&end_token
, sizeof(struct token
));
1562 end_token
.type
= TK_END
;
1563 add_token(rules
, &end_token
);
1564 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1565 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1567 /* cleanup temporary strbuf data */
1568 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1569 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1570 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1571 strbuf_complete(rules
->strbuf
);
1573 /* cleanup uid/gid cache */
1574 rules
->uids
= mfree(rules
->uids
);
1575 rules
->uids_cur
= 0;
1576 rules
->uids_max
= 0;
1577 rules
->gids
= mfree(rules
->gids
);
1578 rules
->gids_cur
= 0;
1579 rules
->gids_max
= 0;
1585 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1588 free(rules
->tokens
);
1589 strbuf_cleanup(rules
->strbuf
);
1592 return mfree(rules
);
1595 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1599 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1602 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1603 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1610 switch (token
->key
.glob
) {
1612 match
= (streq(key_value
, val
));
1615 match
= (fnmatch(key_value
, val
, 0) == 0);
1622 s
= rules_str(rules
, token
->key
.value_off
);
1627 next
= strchr(s
, '|');
1629 size_t matchlen
= (size_t)(next
- s
);
1631 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1635 match
= (streq(s
, val
));
1644 char value
[UTIL_PATH_SIZE
];
1646 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1648 while (key_value
!= NULL
) {
1649 pos
= strchr(key_value
, '|');
1654 match
= (fnmatch(key_value
, val
, 0) == 0);
1662 match
= (val
[0] != '\0');
1668 if (match
&& (token
->key
.op
== OP_MATCH
))
1670 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1675 static int match_attr(struct udev_rules
*rules
, sd_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1676 char nbuf
[UTIL_NAME_SIZE
], vbuf
[UTIL_NAME_SIZE
];
1677 const char *name
, *value
;
1680 name
= rules_str(rules
, cur
->key
.attr_off
);
1681 switch (cur
->key
.attrsubst
) {
1683 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1687 if (sd_device_get_sysattr_value(dev
, name
, &value
) < 0)
1691 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), true) != 0)
1699 /* remove trailing whitespace, if not asked to match for it */
1700 len
= strlen(value
);
1701 if (len
> 0 && isspace(value
[len
-1])) {
1702 const char *key_value
;
1705 key_value
= rules_str(rules
, cur
->key
.value_off
);
1706 klen
= strlen(key_value
);
1707 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1708 if (value
!= vbuf
) {
1709 strscpy(vbuf
, sizeof(vbuf
), value
);
1712 while (len
> 0 && isspace(vbuf
[--len
]))
1717 return match_key(rules
, cur
, value
);
1726 int udev_rules_apply_to_event(
1727 struct udev_rules
*rules
,
1728 struct udev_event
*event
,
1729 usec_t timeout_usec
,
1730 Hashmap
*properties_list
) {
1731 sd_device
*dev
= event
->dev
;
1732 enum escape_type esc
= ESCAPE_UNSET
;
1733 struct token
*cur
, *rule
;
1734 const char *action
, *val
;
1741 r
= sd_device_get_property_value(dev
, "ACTION", &action
);
1745 can_set_name
= (!streq(action
, "remove") &&
1746 (sd_device_get_devnum(dev
, NULL
) >= 0 ||
1747 sd_device_get_ifindex(dev
, NULL
) >= 0));
1749 /* loop through token list, match, run actions or forward to next rule */
1750 cur
= &rules
->tokens
[0];
1753 dump_token(rules
, cur
);
1754 switch (cur
->type
) {
1758 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1759 if (!can_set_name
&& rule
->rule
.can_set_name
)
1764 if (match_key(rules
, cur
, action
) != 0)
1768 if (sd_device_get_devpath(dev
, &val
) < 0)
1770 if (match_key(rules
, cur
, val
) != 0)
1774 if (sd_device_get_sysname(dev
, &val
) < 0)
1776 if (match_key(rules
, cur
, val
) != 0)
1779 case TK_M_DEVLINK
: {
1780 const char *devlink
;
1783 FOREACH_DEVICE_DEVLINK(dev
, devlink
)
1784 if (match_key(rules
, cur
, devlink
+ STRLEN("/dev/")) == 0) {
1794 if (match_key(rules
, cur
, event
->name
) != 0)
1798 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1800 if (sd_device_get_property_value(dev
, key_name
, &val
) < 0) {
1801 /* check global properties */
1802 if (properties_list
)
1803 val
= hashmap_get(properties_list
, key_name
);
1808 if (match_key(rules
, cur
, strempty(val
)))
1816 FOREACH_DEVICE_TAG(dev
, tag
)
1817 if (streq(rules_str(rules
, cur
->key
.value_off
), tag
)) {
1822 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1823 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1827 case TK_M_SUBSYSTEM
:
1828 if (sd_device_get_subsystem(dev
, &val
) < 0)
1830 if (match_key(rules
, cur
, val
) != 0)
1834 if (sd_device_get_driver(dev
, &val
) < 0)
1836 if (match_key(rules
, cur
, val
) != 0)
1840 if (match_attr(rules
, dev
, event
, cur
) != 0)
1844 char filename
[UTIL_PATH_SIZE
];
1845 _cleanup_free_
char *value
= NULL
;
1848 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1849 sysctl_normalize(filename
);
1850 if (sysctl_read(filename
, &value
) < 0)
1853 len
= strlen(value
);
1854 while (len
> 0 && isspace(value
[--len
]))
1856 if (match_key(rules
, cur
, value
) != 0)
1861 case TK_M_SUBSYSTEMS
:
1867 /* get whole sequence of parent matches */
1869 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1872 /* loop over parents */
1873 event
->dev_parent
= dev
;
1877 /* loop over sequence of parent match keys */
1878 for (key
= cur
; key
< next
; key
++ ) {
1879 dump_token(rules
, key
);
1882 if (sd_device_get_sysname(event
->dev_parent
, &val
) < 0)
1884 if (match_key(rules
, key
, val
) != 0)
1887 case TK_M_SUBSYSTEMS
:
1888 if (sd_device_get_subsystem(event
->dev_parent
, &val
) < 0)
1890 if (match_key(rules
, key
, val
) != 0)
1894 if (sd_device_get_driver(event
->dev_parent
, &val
) < 0)
1896 if (match_key(rules
, key
, val
) != 0)
1900 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1904 bool match
= sd_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1906 if (match
&& key
->key
.op
== OP_NOMATCH
)
1908 if (!match
&& key
->key
.op
== OP_MATCH
)
1919 if (sd_device_get_parent(event
->dev_parent
, &event
->dev_parent
) < 0) {
1920 event
->dev_parent
= NULL
;
1924 /* move behind our sequence of parent match keys */
1929 char filename
[UTIL_PATH_SIZE
];
1930 struct stat statbuf
;
1933 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1934 if (util_resolve_subsys_kernel(filename
, filename
, sizeof(filename
), false) != 0) {
1935 if (filename
[0] != '/') {
1936 char tmp
[UTIL_PATH_SIZE
];
1938 if (sd_device_get_syspath(dev
, &val
) < 0)
1941 strscpy(tmp
, sizeof(tmp
), filename
);
1942 strscpyl(filename
, sizeof(filename
), val
, "/", tmp
, NULL
);
1945 attr_subst_subdir(filename
, sizeof(filename
));
1947 match
= (stat(filename
, &statbuf
) == 0);
1948 if (match
&& cur
->key
.mode
> 0)
1949 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1950 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1952 if (!match
&& cur
->key
.op
== OP_MATCH
)
1956 case TK_M_PROGRAM
: {
1957 char program
[UTIL_PATH_SIZE
], result
[UTIL_LINE_SIZE
];
1959 event
->program_result
= mfree(event
->program_result
);
1960 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1961 log_debug("PROGRAM '%s' %s:%u",
1963 rules_str(rules
, rule
->rule
.filename_off
),
1964 rule
->rule
.filename_line
);
1966 if (udev_event_spawn(event
, timeout_usec
, true, program
, result
, sizeof(result
)) < 0) {
1967 if (cur
->key
.op
!= OP_NOMATCH
)
1972 delete_trailing_chars(result
, "\n");
1973 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1974 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1976 log_debug("%i character(s) replaced" , count
);
1978 event
->program_result
= strdup(result
);
1979 if (cur
->key
.op
== OP_NOMATCH
)
1984 case TK_M_IMPORT_FILE
: {
1985 char import
[UTIL_PATH_SIZE
];
1987 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1988 if (import_file_into_properties(dev
, import
) != 0)
1989 if (cur
->key
.op
!= OP_NOMATCH
)
1993 case TK_M_IMPORT_PROG
: {
1994 char import
[UTIL_PATH_SIZE
];
1996 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1997 log_debug("IMPORT '%s' %s:%u",
1999 rules_str(rules
, rule
->rule
.filename_off
),
2000 rule
->rule
.filename_line
);
2002 if (import_program_into_properties(event
, timeout_usec
, import
) != 0)
2003 if (cur
->key
.op
!= OP_NOMATCH
)
2007 case TK_M_IMPORT_BUILTIN
: {
2008 char command
[UTIL_PATH_SIZE
];
2010 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2011 /* check if we ran already */
2012 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2013 log_debug("IMPORT builtin skip '%s' %s:%u",
2014 udev_builtin_name(cur
->key
.builtin_cmd
),
2015 rules_str(rules
, rule
->rule
.filename_off
),
2016 rule
->rule
.filename_line
);
2017 /* return the result from earlier run */
2018 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2019 if (cur
->key
.op
!= OP_NOMATCH
)
2024 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2027 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2028 log_debug("IMPORT builtin '%s' %s:%u",
2029 udev_builtin_name(cur
->key
.builtin_cmd
),
2030 rules_str(rules
, rule
->rule
.filename_off
),
2031 rule
->rule
.filename_line
);
2033 r
= udev_builtin_run(dev
, cur
->key
.builtin_cmd
, command
, false);
2035 /* remember failure */
2036 log_debug_errno(r
, "IMPORT builtin '%s' fails: %m",
2037 udev_builtin_name(cur
->key
.builtin_cmd
));
2038 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2039 if (cur
->key
.op
!= OP_NOMATCH
)
2044 case TK_M_IMPORT_DB
: {
2047 key
= rules_str(rules
, cur
->key
.value_off
);
2048 if (event
->dev_db_clone
&&
2049 sd_device_get_property_value(event
->dev_db_clone
, key
, &val
) >= 0)
2050 device_add_property(dev
, key
, val
);
2051 else if (cur
->key
.op
!= OP_NOMATCH
)
2055 case TK_M_IMPORT_CMDLINE
: {
2056 _cleanup_free_
char *value
= NULL
;
2057 bool imported
= false;
2060 key
= rules_str(rules
, cur
->key
.value_off
);
2061 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2063 log_debug_errno(r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2068 device_add_property(dev
, key
, value
);
2070 /* we import simple flags as 'FLAG=1' */
2071 device_add_property(dev
, key
, "1");
2074 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2078 case TK_M_IMPORT_PARENT
: {
2079 char import
[UTIL_PATH_SIZE
];
2081 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2082 if (import_parent_into_properties(dev
, import
) != 0)
2083 if (cur
->key
.op
!= OP_NOMATCH
)
2088 if (match_key(rules
, cur
, event
->program_result
) != 0)
2091 case TK_A_STRING_ESCAPE_NONE
:
2094 case TK_A_STRING_ESCAPE_REPLACE
:
2095 esc
= ESCAPE_REPLACE
;
2097 case TK_A_DB_PERSIST
:
2098 device_set_db_persist(dev
);
2100 case TK_A_INOTIFY_WATCH
:
2101 if (event
->inotify_watch_final
)
2103 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2104 event
->inotify_watch_final
= true;
2105 event
->inotify_watch
= cur
->key
.watch
;
2107 case TK_A_DEVLINK_PRIO
:
2108 device_set_devlink_priority(dev
, cur
->key
.devlink_prio
);
2111 char owner
[UTIL_NAME_SIZE
];
2112 const char *ow
= owner
;
2114 if (event
->owner_final
)
2116 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2117 event
->owner_final
= true;
2118 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2119 event
->owner_set
= true;
2120 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2122 log_unknown_owner(r
, "user", owner
);
2125 log_debug("OWNER %u %s:%u",
2127 rules_str(rules
, rule
->rule
.filename_off
),
2128 rule
->rule
.filename_line
);
2132 char group
[UTIL_NAME_SIZE
];
2133 const char *gr
= group
;
2135 if (event
->group_final
)
2137 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2138 event
->group_final
= true;
2139 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2140 event
->group_set
= true;
2141 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2143 log_unknown_owner(r
, "group", group
);
2146 log_debug("GROUP %u %s:%u",
2148 rules_str(rules
, rule
->rule
.filename_off
),
2149 rule
->rule
.filename_line
);
2153 char mode_str
[UTIL_NAME_SIZE
], *endptr
;
2156 if (event
->mode_final
)
2158 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2159 mode
= strtol(mode_str
, &endptr
, 8);
2160 if (endptr
[0] != '\0') {
2161 log_error("ignoring invalid mode '%s'", mode_str
);
2164 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2165 event
->mode_final
= true;
2166 event
->mode_set
= true;
2168 log_debug("MODE %#o %s:%u",
2170 rules_str(rules
, rule
->rule
.filename_off
),
2171 rule
->rule
.filename_line
);
2175 if (event
->owner_final
)
2177 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2178 event
->owner_final
= true;
2179 event
->owner_set
= true;
2180 event
->uid
= cur
->key
.uid
;
2181 log_debug("OWNER %u %s:%u",
2183 rules_str(rules
, rule
->rule
.filename_off
),
2184 rule
->rule
.filename_line
);
2187 if (event
->group_final
)
2189 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2190 event
->group_final
= true;
2191 event
->group_set
= true;
2192 event
->gid
= cur
->key
.gid
;
2193 log_debug("GROUP %u %s:%u",
2195 rules_str(rules
, rule
->rule
.filename_off
),
2196 rule
->rule
.filename_line
);
2199 if (event
->mode_final
)
2201 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2202 event
->mode_final
= true;
2203 event
->mode_set
= true;
2204 event
->mode
= cur
->key
.mode
;
2205 log_debug("MODE %#o %s:%u",
2207 rules_str(rules
, rule
->rule
.filename_off
),
2208 rule
->rule
.filename_line
);
2210 case TK_A_SECLABEL
: {
2211 _cleanup_free_
char *name
= NULL
, *label
= NULL
;
2212 char label_str
[UTIL_LINE_SIZE
] = {};
2214 name
= strdup(rules_str(rules
, cur
->key
.attr_off
));
2218 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2219 if (!isempty(label_str
))
2220 label
= strdup(label_str
);
2222 label
= strdup(rules_str(rules
, cur
->key
.value_off
));
2226 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2227 hashmap_clear_free_free(event
->seclabel_list
);
2229 r
= hashmap_ensure_allocated(&event
->seclabel_list
, NULL
);
2233 r
= hashmap_put(event
->seclabel_list
, name
, label
);
2237 name
= label
= NULL
;
2239 log_debug("SECLABEL{%s}='%s' %s:%u",
2241 rules_str(rules
, rule
->rule
.filename_off
),
2242 rule
->rule
.filename_line
);
2246 char value_new
[UTIL_NAME_SIZE
];
2247 const char *name
, *value_old
;
2249 name
= rules_str(rules
, cur
->key
.attr_off
);
2250 val
= rules_str(rules
, cur
->key
.value_off
);
2251 if (val
[0] == '\0') {
2252 if (cur
->key
.op
== OP_ADD
)
2254 device_add_property(dev
, name
, NULL
);
2258 if (cur
->key
.op
== OP_ADD
&&
2259 sd_device_get_property_value(dev
, name
, &value_old
) >= 0) {
2260 char temp
[UTIL_NAME_SIZE
];
2262 /* append value separated by space */
2263 udev_event_apply_format(event
, val
, temp
, sizeof(temp
), false);
2264 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2266 udev_event_apply_format(event
, val
, value_new
, sizeof(value_new
), false);
2268 device_add_property(dev
, name
, value_new
);
2272 char tag
[UTIL_PATH_SIZE
];
2275 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2276 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2277 device_cleanup_tags(dev
);
2278 for (p
= tag
; *p
!= '\0'; p
++) {
2279 if ((*p
>= 'a' && *p
<= 'z') ||
2280 (*p
>= 'A' && *p
<= 'Z') ||
2281 (*p
>= '0' && *p
<= '9') ||
2282 IN_SET(*p
, '-', '_'))
2284 log_error("ignoring invalid tag name '%s'", tag
);
2287 if (cur
->key
.op
== OP_REMOVE
)
2288 device_remove_tag(dev
, tag
);
2290 device_add_tag(dev
, tag
);
2294 char name_str
[UTIL_PATH_SIZE
];
2298 name
= rules_str(rules
, cur
->key
.value_off
);
2299 if (event
->name_final
)
2301 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2302 event
->name_final
= true;
2303 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2304 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2305 count
= util_replace_chars(name_str
, "/");
2307 log_debug("%i character(s) replaced", count
);
2309 if (sd_device_get_devnum(dev
, NULL
) >= 0 &&
2310 (sd_device_get_devname(dev
, &val
) < 0 ||
2311 !streq(name_str
, val
+ STRLEN("/dev/")))) {
2312 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2314 rules_str(rules
, rule
->rule
.filename_off
),
2315 rule
->rule
.filename_line
);
2318 if (free_and_strdup(&event
->name
, name_str
) < 0)
2321 log_debug("NAME '%s' %s:%u",
2323 rules_str(rules
, rule
->rule
.filename_off
),
2324 rule
->rule
.filename_line
);
2327 case TK_A_DEVLINK
: {
2328 char temp
[UTIL_PATH_SIZE
], filename
[UTIL_PATH_SIZE
], *pos
, *next
;
2331 if (event
->devlink_final
)
2333 if (sd_device_get_devnum(dev
, NULL
) < 0)
2335 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2336 event
->devlink_final
= true;
2337 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2338 device_cleanup_devlinks(dev
);
2340 /* allow multiple symlinks separated by spaces */
2341 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2342 if (esc
== ESCAPE_UNSET
)
2343 count
= util_replace_chars(temp
, "/ ");
2344 else if (esc
== ESCAPE_REPLACE
)
2345 count
= util_replace_chars(temp
, "/");
2347 log_debug("%i character(s) replaced" , count
);
2349 while (isspace(pos
[0]))
2351 next
= strchr(pos
, ' ');
2354 log_debug("LINK '%s' %s:%u", pos
,
2355 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2356 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2357 device_add_devlink(dev
, filename
);
2358 while (isspace(next
[1]))
2361 next
= strchr(pos
, ' ');
2363 if (pos
[0] != '\0') {
2364 log_debug("LINK '%s' %s:%u", pos
,
2365 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2366 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2367 device_add_devlink(dev
, filename
);
2372 char attr
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2373 _cleanup_fclose_
FILE *f
= NULL
;
2374 const char *key_name
;
2376 key_name
= rules_str(rules
, cur
->key
.attr_off
);
2377 if (util_resolve_subsys_kernel(key_name
, attr
, sizeof(attr
), false) != 0 &&
2378 sd_device_get_syspath(dev
, &val
) >= 0)
2379 strscpyl(attr
, sizeof(attr
), val
, "/", key_name
, NULL
);
2380 attr_subst_subdir(attr
, sizeof(attr
));
2382 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2383 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2384 rules_str(rules
, rule
->rule
.filename_off
),
2385 rule
->rule
.filename_line
);
2386 f
= fopen(attr
, "we");
2388 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2389 else if (fprintf(f
, "%s", value
) <= 0)
2390 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2394 char filename
[UTIL_PATH_SIZE
], value
[UTIL_NAME_SIZE
];
2396 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2397 sysctl_normalize(filename
);
2398 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2399 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2400 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2401 r
= sysctl_write(filename
, value
);
2403 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2406 case TK_A_RUN_BUILTIN
:
2407 case TK_A_RUN_PROGRAM
: {
2408 _cleanup_free_
char *cmd
= NULL
;
2410 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
)) {
2413 while ((p
= hashmap_steal_first_key(event
->run_list
)))
2417 r
= hashmap_ensure_allocated(&event
->run_list
, NULL
);
2421 cmd
= strdup(rules_str(rules
, cur
->key
.value_off
));
2425 r
= hashmap_put(event
->run_list
, cmd
, INT_TO_PTR(cur
->key
.builtin_cmd
));
2431 log_debug("RUN '%s' %s:%u",
2432 rules_str(rules
, cur
->key
.value_off
),
2433 rules_str(rules
, rule
->rule
.filename_off
),
2434 rule
->rule
.filename_line
);
2438 if (cur
->key
.rule_goto
== 0)
2440 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2445 case TK_M_PARENTS_MIN
:
2446 case TK_M_PARENTS_MAX
:
2449 log_error("wrong type %u", cur
->type
);
2456 /* fast-forward to next rule */
2457 cur
= rule
+ rule
->rule
.token_count
;
2463 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2469 _cleanup_strv_free_
char **tags
= NULL
;
2472 _cleanup_free_
char *path
= NULL
;
2475 if (rules
->tokens
== NULL
)
2478 cur
= &rules
->tokens
[0];
2481 switch (cur
->type
) {
2486 /* skip rules without a static_node tag */
2487 if (!rule
->rule
.has_static_node
)
2493 tags
= strv_free(tags
);
2502 mode
= cur
->key
.mode
;
2505 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2510 case TK_A_STATIC_NODE
: {
2511 char device_node
[UTIL_PATH_SIZE
];
2512 char tags_dir
[UTIL_PATH_SIZE
];
2513 char tag_symlink
[UTIL_PATH_SIZE
];
2516 /* we assure, that the permissions tokens are sorted before the static token */
2518 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2521 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2522 if (stat(device_node
, &stats
) != 0)
2524 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2527 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2529 STRV_FOREACH(t
, tags
) {
2530 _cleanup_free_
char *unescaped_filename
= NULL
;
2532 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2533 r
= mkdir_p(tags_dir
, 0755);
2535 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2537 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2539 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2540 r
= symlink(device_node
, tag_symlink
);
2541 if (r
< 0 && errno
!= EEXIST
)
2542 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2543 tag_symlink
, device_node
);
2547 /* don't touch the permissions if only the tags were set */
2548 if (mode
== 0 && uid
== 0 && gid
== 0)
2557 if (mode
!= (stats
.st_mode
& 01777)) {
2558 r
= chmod(device_node
, mode
);
2560 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2563 log_debug("chmod '%s' %#o", device_node
, mode
);
2566 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2567 r
= chown(device_node
, uid
, gid
);
2569 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2570 device_node
, uid
, gid
);
2572 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2575 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2585 /* fast-forward to next rule */
2586 cur
= rule
+ rule
->rule
.token_count
;
2593 fchmod(fileno(f
), 0644);
2594 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2595 unlink_noerrno("/run/udev/static_node-tags");
2596 unlink_noerrno(path
);