1 /* SPDX-License-Identifier: GPL-2.0+ */
16 #include "alloc-util.h"
17 #include "conf-files.h"
18 #include "dirent-util.h"
22 #include "glob-util.h"
23 #include "path-util.h"
24 #include "proc-cmdline.h"
25 #include "stat-util.h"
26 #include "stdio-util.h"
28 #include "string-util.h"
30 #include "sysctl-util.h"
32 #include "user-util.h"
35 #define PREALLOC_TOKEN 2048
38 unsigned int name_off
;
45 static const char* const rules_dirs
[] = {
48 UDEVLIBEXECDIR
"/rules.d",
56 /* every key in the rules file becomes a token */
58 unsigned int token_cur
;
59 unsigned int token_max
;
61 /* all key strings are copied and de-duplicated in a single continuous string buffer */
62 struct strbuf
*strbuf
;
64 /* during rule parsing, uid/gid lookup results are cached */
66 unsigned int uids_cur
;
67 unsigned int uids_max
;
69 unsigned int gids_cur
;
70 unsigned int gids_max
;
73 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
74 return rules
->strbuf
->buf
+ off
;
77 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
78 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
81 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
95 enum string_glob_type
{
97 GL_PLAIN
, /* no special chars */
98 GL_GLOB
, /* shell globs ?,*,[] */
99 GL_SPLIT
, /* multi-value A|B */
100 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
101 GL_SOMETHING
, /* commonly used "?*" */
104 enum string_subst_type
{
111 /* tokens of a rule are sorted/handled in this order */
116 TK_M_ACTION
, /* val */
117 TK_M_DEVPATH
, /* val */
118 TK_M_KERNEL
, /* val */
119 TK_M_DEVLINK
, /* val */
121 TK_M_ENV
, /* val, attr */
123 TK_M_SUBSYSTEM
, /* val */
124 TK_M_DRIVER
, /* val */
125 TK_M_WAITFOR
, /* val */
126 TK_M_ATTR
, /* val, attr */
127 TK_M_SYSCTL
, /* val, attr */
130 TK_M_KERNELS
, /* val */
131 TK_M_SUBSYSTEMS
, /* val */
132 TK_M_DRIVERS
, /* val */
133 TK_M_ATTRS
, /* val, attr */
137 TK_M_TEST
, /* val, mode_t */
138 TK_M_PROGRAM
, /* val */
139 TK_M_IMPORT_FILE
, /* val */
140 TK_M_IMPORT_PROG
, /* val */
141 TK_M_IMPORT_BUILTIN
, /* val */
142 TK_M_IMPORT_DB
, /* val */
143 TK_M_IMPORT_CMDLINE
, /* val */
144 TK_M_IMPORT_PARENT
, /* val */
145 TK_M_RESULT
, /* val */
148 TK_A_STRING_ESCAPE_NONE
,
149 TK_A_STRING_ESCAPE_REPLACE
,
151 TK_A_INOTIFY_WATCH
, /* int */
152 TK_A_DEVLINK_PRIO
, /* int */
153 TK_A_OWNER
, /* val */
154 TK_A_GROUP
, /* val */
156 TK_A_OWNER_ID
, /* uid_t */
157 TK_A_GROUP_ID
, /* gid_t */
158 TK_A_MODE_ID
, /* mode_t */
160 TK_A_STATIC_NODE
, /* val */
161 TK_A_SECLABEL
, /* val, attr */
162 TK_A_ENV
, /* val, attr */
164 TK_A_DEVLINK
, /* val */
165 TK_A_ATTR
, /* val, attr */
166 TK_A_SYSCTL
, /* val, attr */
167 TK_A_RUN_BUILTIN
, /* val, bool */
168 TK_A_RUN_PROGRAM
, /* val, bool */
169 TK_A_GOTO
, /* size_t */
174 /* we try to pack stuff in a way that we take only 12 bytes per token */
177 unsigned char type
; /* same in rule and key */
179 enum token_type type
:8;
181 bool has_static_node
:1;
182 unsigned int unused
:6;
183 unsigned short token_count
;
184 unsigned int label_off
;
185 unsigned short filename_off
;
186 unsigned short filename_line
;
189 enum token_type type
:8;
190 enum operation_type op
:8;
191 enum string_glob_type glob
:8;
192 enum string_subst_type subst
:4;
193 enum string_subst_type attrsubst
:4;
194 unsigned int value_off
;
196 unsigned int attr_off
;
197 unsigned int rule_goto
;
203 enum udev_builtin_cmd builtin_cmd
;
211 struct udev_rules
*rules
;
213 struct token token
[MAX_TK
];
214 unsigned int token_cur
;
218 static const char *operation_str(enum operation_type type
) {
219 static const char *operation_strs
[] = {
220 [OP_UNSET
] = "UNSET",
221 [OP_MATCH
] = "match",
222 [OP_NOMATCH
] = "nomatch",
223 [OP_MATCH_MAX
] = "MATCH_MAX",
226 [OP_REMOVE
] = "remove",
227 [OP_ASSIGN
] = "assign",
228 [OP_ASSIGN_FINAL
] = "assign-final",
231 return operation_strs
[type
];
234 static const char *string_glob_str(enum string_glob_type type
) {
235 static const char *string_glob_strs
[] = {
236 [GL_UNSET
] = "UNSET",
237 [GL_PLAIN
] = "plain",
239 [GL_SPLIT
] = "split",
240 [GL_SPLIT_GLOB
] = "split-glob",
241 [GL_SOMETHING
] = "split-glob",
244 return string_glob_strs
[type
];
247 static const char *token_str(enum token_type type
) {
248 static const char *token_strs
[] = {
249 [TK_UNSET
] = "UNSET",
252 [TK_M_ACTION
] = "M ACTION",
253 [TK_M_DEVPATH
] = "M DEVPATH",
254 [TK_M_KERNEL
] = "M KERNEL",
255 [TK_M_DEVLINK
] = "M DEVLINK",
256 [TK_M_NAME
] = "M NAME",
257 [TK_M_ENV
] = "M ENV",
258 [TK_M_TAG
] = "M TAG",
259 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
260 [TK_M_DRIVER
] = "M DRIVER",
261 [TK_M_WAITFOR
] = "M WAITFOR",
262 [TK_M_ATTR
] = "M ATTR",
263 [TK_M_SYSCTL
] = "M SYSCTL",
265 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
266 [TK_M_KERNELS
] = "M KERNELS",
267 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
268 [TK_M_DRIVERS
] = "M DRIVERS",
269 [TK_M_ATTRS
] = "M ATTRS",
270 [TK_M_TAGS
] = "M TAGS",
271 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
273 [TK_M_TEST
] = "M TEST",
274 [TK_M_PROGRAM
] = "M PROGRAM",
275 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
276 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
277 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
278 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
279 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
280 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
281 [TK_M_RESULT
] = "M RESULT",
282 [TK_M_MAX
] = "M MAX",
284 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
285 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
286 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
287 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
288 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
289 [TK_A_OWNER
] = "A OWNER",
290 [TK_A_GROUP
] = "A GROUP",
291 [TK_A_MODE
] = "A MODE",
292 [TK_A_OWNER_ID
] = "A OWNER_ID",
293 [TK_A_GROUP_ID
] = "A GROUP_ID",
294 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
295 [TK_A_SECLABEL
] = "A SECLABEL",
296 [TK_A_MODE_ID
] = "A MODE_ID",
297 [TK_A_ENV
] = "A ENV",
298 [TK_A_TAG
] = "A ENV",
299 [TK_A_NAME
] = "A NAME",
300 [TK_A_DEVLINK
] = "A DEVLINK",
301 [TK_A_ATTR
] = "A ATTR",
302 [TK_A_SYSCTL
] = "A SYSCTL",
303 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
304 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
305 [TK_A_GOTO
] = "A GOTO",
310 return token_strs
[type
];
313 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
314 enum token_type type
= token
->type
;
315 enum operation_type op
= token
->key
.op
;
316 enum string_glob_type glob
= token
->key
.glob
;
317 const char *value
= rules_str(rules
, token
->key
.value_off
);
318 const char *attr
= &rules
->strbuf
->buf
[token
->key
.attr_off
];
323 const char *tks_ptr
= (char *)rules
->tokens
;
324 const char *tk_ptr
= (char *)token
;
325 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
327 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
328 &rules
->strbuf
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
329 idx
, token
->rule
.token_count
,
330 &rules
->strbuf
->buf
[token
->rule
.label_off
]);
342 case TK_M_SUBSYSTEMS
:
346 case TK_M_IMPORT_FILE
:
347 case TK_M_IMPORT_PROG
:
349 case TK_M_IMPORT_CMDLINE
:
350 case TK_M_IMPORT_PARENT
:
357 case TK_A_RUN_BUILTIN
:
358 case TK_A_RUN_PROGRAM
:
359 log_debug("%s %s '%s'(%s)",
360 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
362 case TK_M_IMPORT_BUILTIN
:
363 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
372 log_debug("%s %s '%s' '%s'(%s)",
373 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
377 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
379 case TK_A_STRING_ESCAPE_NONE
:
380 case TK_A_STRING_ESCAPE_REPLACE
:
381 case TK_A_DB_PERSIST
:
382 log_debug("%s", token_str(type
));
385 log_debug("%s %s '%s'(%s) %#o",
386 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
388 case TK_A_INOTIFY_WATCH
:
389 log_debug("%s %u", token_str(type
), token
->key
.watch
);
391 case TK_A_DEVLINK_PRIO
:
392 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
395 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
398 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
401 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
403 case TK_A_STATIC_NODE
:
404 log_debug("%s '%s'", token_str(type
), value
);
407 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
410 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
413 log_debug("* %s", token_str(type
));
415 case TK_M_PARENTS_MIN
:
416 case TK_M_PARENTS_MAX
:
419 log_debug("unknown type %u", type
);
424 static void dump_rules(struct udev_rules
*rules
) {
427 log_debug("dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings",
429 rules
->token_cur
* sizeof(struct token
),
430 rules
->strbuf
->nodes_count
,
432 for (i
= 0; i
< rules
->token_cur
; i
++)
433 dump_token(rules
, &rules
->tokens
[i
]);
436 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
437 static inline void dump_rules(struct udev_rules
*rules
) {}
440 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
441 /* grow buffer if needed */
442 if (rules
->token_cur
+1 >= rules
->token_max
) {
443 struct token
*tokens
;
446 /* double the buffer size */
447 add
= rules
->token_max
;
451 tokens
= reallocarray(rules
->tokens
, rules
->token_max
+ add
, sizeof(struct token
));
454 rules
->tokens
= tokens
;
455 rules
->token_max
+= add
;
457 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
462 static void log_unknown_owner(int error
, const char *entity
, const char *owner
) {
463 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
464 log_error("Specified %s '%s' unknown", entity
, owner
);
466 log_error_errno(error
, "Error resolving %s '%s': %m", entity
, owner
);
469 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
475 /* lookup, if we know it already */
476 for (i
= 0; i
< rules
->uids_cur
; i
++) {
477 off
= rules
->uids
[i
].name_off
;
478 if (streq(rules_str(rules
, off
), owner
)) {
479 uid
= rules
->uids
[i
].uid
;
483 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
485 log_unknown_owner(r
, "user", owner
);
487 /* grow buffer if needed */
488 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
489 struct uid_gid
*uids
;
492 /* double the buffer size */
493 add
= rules
->uids_max
;
497 uids
= reallocarray(rules
->uids
, rules
->uids_max
+ add
, sizeof(struct uid_gid
));
501 rules
->uids_max
+= add
;
503 rules
->uids
[rules
->uids_cur
].uid
= uid
;
504 off
= rules_add_string(rules
, owner
);
507 rules
->uids
[rules
->uids_cur
].name_off
= off
;
512 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
518 /* lookup, if we know it already */
519 for (i
= 0; i
< rules
->gids_cur
; i
++) {
520 off
= rules
->gids
[i
].name_off
;
521 if (streq(rules_str(rules
, off
), group
)) {
522 gid
= rules
->gids
[i
].gid
;
526 r
= get_group_creds(&group
, &gid
, USER_CREDS_ALLOW_MISSING
);
528 log_unknown_owner(r
, "group", group
);
530 /* grow buffer if needed */
531 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
532 struct uid_gid
*gids
;
535 /* double the buffer size */
536 add
= rules
->gids_max
;
540 gids
= reallocarray(rules
->gids
, rules
->gids_max
+ add
, sizeof(struct uid_gid
));
544 rules
->gids_max
+= add
;
546 rules
->gids
[rules
->gids_cur
].gid
= gid
;
547 off
= rules_add_string(rules
, group
);
550 rules
->gids
[rules
->gids_cur
].name_off
= off
;
555 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
562 while (isspace(key
[0]))
565 /* comment or empty line */
566 if (IN_SET(key
[0], '#', '\0'))
569 /* split key/value */
570 val
= strchr(key
, '=');
577 while (isspace(val
[0]))
584 while (isspace(key
[len
-1]))
588 /* terminate value */
592 while (isspace(val
[len
-1]))
600 if (IN_SET(val
[0], '"', '\'')) {
601 if (len
== 1 || val
[len
-1] != val
[0]) {
602 log_debug("inconsistent quoting: '%s', skip", line
);
609 udev_device_add_property(dev
, key
, val
);
614 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
616 char line
[UTIL_LINE_SIZE
];
618 f
= fopen(filename
, "re");
621 while (fgets(line
, sizeof(line
), f
) != NULL
)
622 import_property_from_string(dev
, line
);
627 static int import_program_into_properties(struct udev_event
*event
,
629 usec_t timeout_warn_usec
,
630 const char *program
) {
631 char result
[UTIL_LINE_SIZE
];
635 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
640 while (line
!= NULL
) {
643 pos
= strchr(line
, '\n');
648 import_property_from_string(event
->dev
, line
);
654 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
655 struct udev_device
*dev_parent
;
656 struct udev_list_entry
*list_entry
;
661 dev_parent
= udev_device_get_parent(dev
);
662 if (dev_parent
== NULL
)
665 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
666 const char *key
= udev_list_entry_get_name(list_entry
);
667 const char *val
= udev_list_entry_get_value(list_entry
);
669 if (fnmatch(filter
, key
, 0) == 0)
670 udev_device_add_property(dev
, key
, val
);
675 static void attr_subst_subdir(char *attr
, size_t len
) {
676 const char *pos
, *tail
, *path
;
677 _cleanup_closedir_
DIR *dir
= NULL
;
680 pos
= strstr(attr
, "/*/");
685 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
690 FOREACH_DIRENT_ALL(dent
, dir
, break)
691 if (dent
->d_name
[0] != '.') {
692 char n
[strlen(dent
->d_name
) + strlen(tail
) + 1];
694 strscpyl(n
, sizeof n
, dent
->d_name
, tail
, NULL
);
695 if (faccessat(dirfd(dir
), n
, F_OK
, 0) == 0) {
696 strscpyl(attr
, len
, path
, n
, NULL
);
702 static int get_key(char **line
, char **key
, enum operation_type
*op
, char **value
) {
708 if (linepos
== NULL
|| linepos
[0] == '\0')
711 /* skip whitespace */
712 while (isspace(linepos
[0]) || linepos
[0] == ',')
716 if (linepos
[0] == '\0')
722 if (linepos
[0] == '\0')
724 if (isspace(linepos
[0]))
726 if (linepos
[0] == '=')
728 if (IN_SET(linepos
[0], '+', '-', '!', ':'))
729 if (linepos
[1] == '=')
733 /* remember end of key */
736 /* skip whitespace after key */
737 while (isspace(linepos
[0]))
739 if (linepos
[0] == '\0')
742 /* get operation type */
743 if (linepos
[0] == '=' && linepos
[1] == '=') {
746 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
749 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
752 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
755 } else if (linepos
[0] == '=') {
758 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
759 *op
= OP_ASSIGN_FINAL
;
767 /* skip whitespace after operator */
768 while (isspace(linepos
[0]))
770 if (linepos
[0] == '\0')
774 if (linepos
[0] == '"')
781 for (i
= 0, j
= 0; ; i
++, j
++) {
783 if (linepos
[i
] == '"')
786 if (linepos
[i
] == '\0')
789 /* double quotes can be escaped */
790 if (linepos
[i
] == '\\')
791 if (linepos
[i
+1] == '"')
794 linepos
[j
] = linepos
[i
];
798 /* move line to next key */
799 *line
= linepos
+ i
+ 1;
803 /* extract possible KEY{attr} */
804 static const char *get_key_attribute(char *str
) {
808 attr
= strchr(str
, '{');
811 pos
= strchr(attr
, '}');
813 log_error("Missing closing brace for format");
822 static void rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
823 enum operation_type op
,
824 const char *value
, const void *data
) {
825 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
826 const char *attr
= NULL
;
828 assert(rule_tmp
->token_cur
< ELEMENTSOF(rule_tmp
->token
));
829 memzero(token
, sizeof(struct token
));
841 case TK_M_SUBSYSTEMS
:
845 case TK_M_IMPORT_FILE
:
846 case TK_M_IMPORT_PROG
:
848 case TK_M_IMPORT_CMDLINE
:
849 case TK_M_IMPORT_PARENT
:
859 case TK_A_STATIC_NODE
:
860 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
862 case TK_M_IMPORT_BUILTIN
:
863 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
864 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
875 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
876 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
879 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
881 token
->key
.mode
= *(mode_t
*)data
;
883 case TK_A_STRING_ESCAPE_NONE
:
884 case TK_A_STRING_ESCAPE_REPLACE
:
885 case TK_A_DB_PERSIST
:
887 case TK_A_RUN_BUILTIN
:
888 case TK_A_RUN_PROGRAM
:
889 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
890 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
892 case TK_A_INOTIFY_WATCH
:
893 case TK_A_DEVLINK_PRIO
:
894 token
->key
.devlink_prio
= *(int *)data
;
897 token
->key
.uid
= *(uid_t
*)data
;
900 token
->key
.gid
= *(gid_t
*)data
;
903 token
->key
.mode
= *(mode_t
*)data
;
906 case TK_M_PARENTS_MIN
:
907 case TK_M_PARENTS_MAX
:
911 assert_not_reached("wrong type");
914 if (value
!= NULL
&& type
< TK_M_MAX
) {
915 /* check if we need to split or call fnmatch() while matching rules */
916 enum string_glob_type glob
;
920 has_split
= (strchr(value
, '|') != NULL
);
921 has_glob
= string_is_glob(value
);
922 if (has_split
&& has_glob
) {
923 glob
= GL_SPLIT_GLOB
;
924 } else if (has_split
) {
926 } else if (has_glob
) {
927 if (streq(value
, "?*"))
934 token
->key
.glob
= glob
;
937 if (value
!= NULL
&& type
> TK_M_MAX
) {
938 /* check if assigned value has substitution chars */
940 token
->key
.subst
= SB_SUBSYS
;
941 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
942 token
->key
.subst
= SB_FORMAT
;
944 token
->key
.subst
= SB_NONE
;
948 /* check if property/attribute name has substitution chars */
950 token
->key
.attrsubst
= SB_SUBSYS
;
951 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
952 token
->key
.attrsubst
= SB_FORMAT
;
954 token
->key
.attrsubst
= SB_NONE
;
957 token
->key
.type
= type
;
959 rule_tmp
->token_cur
++;
962 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
964 unsigned int start
= 0;
965 unsigned int end
= rule_tmp
->token_cur
;
967 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
968 enum token_type next_val
= TK_UNSET
;
969 unsigned int next_idx
= 0;
972 /* find smallest value */
973 for (j
= start
; j
< end
; j
++) {
974 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
976 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
977 next_val
= rule_tmp
->token
[j
].type
;
982 /* add token and mark done */
983 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
985 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
988 if (next_idx
== start
)
990 if (next_idx
+1 == end
)
996 #define LOG_RULE_ERROR(fmt, ...) log_error("Invalid rule %s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
997 #define LOG_RULE_WARNING(fmt, ...) log_warning("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
998 #define LOG_RULE_DEBUG(fmt, ...) log_debug("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
999 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1001 static void add_rule(struct udev_rules
*rules
, char *line
,
1002 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1005 struct rule_tmp rule_tmp
= {
1007 .rule
.type
= TK_RULE
,
1010 /* the offset in the rule is limited to unsigned short */
1011 if (filename_off
< USHRT_MAX
)
1012 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1013 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1019 enum operation_type op
;
1021 if (get_key(&linepos
, &key
, &op
, &value
) != 0) {
1022 /* Avoid erroring on trailing whitespace. This is probably rare
1023 * so save the work for the error case instead of always trying
1024 * to strip the trailing whitespace with strstrip(). */
1025 while (isblank(*linepos
))
1028 /* If we aren't at the end of the line, this is a parsing error.
1029 * Make a best effort to describe where the problem is. */
1030 if (!strchr(NEWLINE
, *linepos
)) {
1031 char buf
[2] = {*linepos
};
1032 _cleanup_free_
char *tmp
;
1035 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1036 filename
, lineno
, linepos
- line
+ 1, tmp
);
1037 if (*linepos
== '#')
1038 log_error("hint: comments can only start at beginning of line");
1043 if (rule_tmp
.token_cur
>= ELEMENTSOF(rule_tmp
.token
))
1044 LOG_AND_RETURN("temporary rule array too small, aborting event processing with %u items", rule_tmp
.token_cur
);
1046 if (streq(key
, "ACTION")) {
1047 if (op
> OP_MATCH_MAX
)
1048 LOG_AND_RETURN("invalid %s operation", key
);
1050 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1052 } else if (streq(key
, "DEVPATH")) {
1053 if (op
> OP_MATCH_MAX
)
1054 LOG_AND_RETURN("invalid %s operation", key
);
1056 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1058 } else if (streq(key
, "KERNEL")) {
1059 if (op
> OP_MATCH_MAX
)
1060 LOG_AND_RETURN("invalid %s operation", key
);
1062 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1064 } else if (streq(key
, "SUBSYSTEM")) {
1065 if (op
> OP_MATCH_MAX
)
1066 LOG_AND_RETURN("invalid %s operation", key
);
1068 /* bus, class, subsystem events should all be the same */
1069 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1070 if (!streq(value
, "subsystem"))
1071 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1073 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1075 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1077 } else if (streq(key
, "DRIVER")) {
1078 if (op
> OP_MATCH_MAX
)
1079 LOG_AND_RETURN("invalid %s operation", key
);
1081 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1083 } else if (startswith(key
, "ATTR{")) {
1084 attr
= get_key_attribute(key
+ STRLEN("ATTR"));
1086 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1088 if (op
== OP_REMOVE
)
1089 LOG_AND_RETURN("invalid %s operation", "ATTR");
1091 if (op
< OP_MATCH_MAX
)
1092 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1094 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1096 } else if (startswith(key
, "SYSCTL{")) {
1097 attr
= get_key_attribute(key
+ STRLEN("SYSCTL"));
1099 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1101 if (op
== OP_REMOVE
)
1102 LOG_AND_RETURN("invalid %s operation", "ATTR");
1104 if (op
< OP_MATCH_MAX
)
1105 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1107 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1109 } else if (startswith(key
, "SECLABEL{")) {
1110 attr
= get_key_attribute(key
+ STRLEN("SECLABEL"));
1112 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1114 if (op
== OP_REMOVE
)
1115 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1117 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1119 } else if (streq(key
, "KERNELS")) {
1120 if (op
> OP_MATCH_MAX
)
1121 LOG_AND_RETURN("invalid %s operation", key
);
1123 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1125 } else if (streq(key
, "SUBSYSTEMS")) {
1126 if (op
> OP_MATCH_MAX
)
1127 LOG_AND_RETURN("invalid %s operation", key
);
1129 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1131 } else if (streq(key
, "DRIVERS")) {
1132 if (op
> OP_MATCH_MAX
)
1133 LOG_AND_RETURN("invalid %s operation", key
);
1135 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1137 } else if (startswith(key
, "ATTRS{")) {
1138 if (op
> OP_MATCH_MAX
)
1139 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1141 attr
= get_key_attribute(key
+ STRLEN("ATTRS"));
1143 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1145 if (startswith(attr
, "device/"))
1146 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1147 if (strstr(attr
, "../") != NULL
)
1148 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1149 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1151 } else if (streq(key
, "TAGS")) {
1152 if (op
> OP_MATCH_MAX
)
1153 LOG_AND_RETURN("invalid %s operation", key
);
1155 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1157 } else if (startswith(key
, "ENV{")) {
1158 attr
= get_key_attribute(key
+ STRLEN("ENV"));
1160 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1162 if (op
== OP_REMOVE
)
1163 LOG_AND_RETURN("invalid %s operation", "ENV");
1165 if (op
< OP_MATCH_MAX
)
1166 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1168 if (STR_IN_SET(attr
,
1180 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr
);
1182 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1185 } else if (streq(key
, "TAG")) {
1186 if (op
< OP_MATCH_MAX
)
1187 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1189 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1191 } else if (streq(key
, "PROGRAM")) {
1192 if (op
== OP_REMOVE
)
1193 LOG_AND_RETURN("invalid %s operation", key
);
1195 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1197 } else if (streq(key
, "RESULT")) {
1198 if (op
> OP_MATCH_MAX
)
1199 LOG_AND_RETURN("invalid %s operation", key
);
1201 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1203 } else if (startswith(key
, "IMPORT")) {
1204 attr
= get_key_attribute(key
+ STRLEN("IMPORT"));
1206 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1209 if (op
== OP_REMOVE
)
1210 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1212 if (streq(attr
, "program")) {
1213 /* find known built-in command */
1214 if (value
[0] != '/') {
1215 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1217 if (cmd
< UDEV_BUILTIN_MAX
) {
1218 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1219 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1223 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1224 } else if (streq(attr
, "builtin")) {
1225 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1227 if (cmd
>= UDEV_BUILTIN_MAX
)
1228 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1230 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1231 } else if (streq(attr
, "file"))
1232 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1233 else if (streq(attr
, "db"))
1234 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1235 else if (streq(attr
, "cmdline"))
1236 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1237 else if (streq(attr
, "parent"))
1238 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1240 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1242 } else if (startswith(key
, "TEST")) {
1245 if (op
> OP_MATCH_MAX
)
1246 LOG_AND_RETURN("invalid %s operation", "TEST");
1248 attr
= get_key_attribute(key
+ STRLEN("TEST"));
1250 mode
= strtol(attr
, NULL
, 8);
1251 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1253 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1255 } else if (startswith(key
, "RUN")) {
1256 attr
= get_key_attribute(key
+ STRLEN("RUN"));
1259 if (op
== OP_REMOVE
)
1260 LOG_AND_RETURN("invalid %s operation", "RUN");
1262 if (streq(attr
, "builtin")) {
1263 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1265 if (cmd
< UDEV_BUILTIN_MAX
)
1266 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1268 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1269 } else if (streq(attr
, "program")) {
1270 const enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1272 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1274 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr
);
1276 } else if (streq(key
, "LABEL")) {
1277 if (op
== OP_REMOVE
)
1278 LOG_AND_RETURN("invalid %s operation", key
);
1280 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1282 } else if (streq(key
, "GOTO")) {
1283 if (op
== OP_REMOVE
)
1284 LOG_AND_RETURN("invalid %s operation", key
);
1286 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1288 } else if (startswith(key
, "NAME")) {
1289 if (op
== OP_REMOVE
)
1290 LOG_AND_RETURN("invalid %s operation", key
);
1292 if (op
< OP_MATCH_MAX
)
1293 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1295 if (streq(value
, "%k")) {
1296 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1299 if (isempty(value
)) {
1300 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1303 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1305 rule_tmp
.rule
.rule
.can_set_name
= true;
1307 } else if (streq(key
, "SYMLINK")) {
1308 if (op
== OP_REMOVE
)
1309 LOG_AND_RETURN("invalid %s operation", key
);
1311 if (op
< OP_MATCH_MAX
)
1312 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1314 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1315 rule_tmp
.rule
.rule
.can_set_name
= true;
1317 } else if (streq(key
, "OWNER")) {
1321 if (op
== OP_REMOVE
)
1322 LOG_AND_RETURN("invalid %s operation", key
);
1324 uid
= strtoul(value
, &endptr
, 10);
1325 if (endptr
[0] == '\0')
1326 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1327 else if (rules
->resolve_names
> 0 && strchr("$%", value
[0]) == NULL
) {
1328 uid
= add_uid(rules
, value
);
1329 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1330 } else if (rules
->resolve_names
>= 0)
1331 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1333 rule_tmp
.rule
.rule
.can_set_name
= true;
1335 } else if (streq(key
, "GROUP")) {
1339 if (op
== OP_REMOVE
)
1340 LOG_AND_RETURN("invalid %s operation", key
);
1342 gid
= strtoul(value
, &endptr
, 10);
1343 if (endptr
[0] == '\0')
1344 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1345 else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1346 gid
= add_gid(rules
, value
);
1347 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1348 } else if (rules
->resolve_names
>= 0)
1349 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1351 rule_tmp
.rule
.rule
.can_set_name
= true;
1353 } else if (streq(key
, "MODE")) {
1357 if (op
== OP_REMOVE
)
1358 LOG_AND_RETURN("invalid %s operation", key
);
1360 mode
= strtol(value
, &endptr
, 8);
1361 if (endptr
[0] == '\0')
1362 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1364 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1365 rule_tmp
.rule
.rule
.can_set_name
= true;
1367 } else if (streq(key
, "OPTIONS")) {
1370 if (op
== OP_REMOVE
)
1371 LOG_AND_RETURN("invalid %s operation", key
);
1373 pos
= strstr(value
, "link_priority=");
1375 int prio
= atoi(pos
+ STRLEN("link_priority="));
1377 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1380 pos
= strstr(value
, "string_escape=");
1382 pos
+= STRLEN("string_escape=");
1383 if (startswith(pos
, "none"))
1384 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1385 else if (startswith(pos
, "replace"))
1386 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1389 pos
= strstr(value
, "db_persist");
1391 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1393 pos
= strstr(value
, "nowatch");
1397 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1399 pos
= strstr(value
, "watch");
1403 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1407 pos
= strstr(value
, "static_node=");
1409 pos
+= STRLEN("static_node=");
1410 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1411 rule_tmp
.rule
.rule
.has_static_node
= true;
1415 LOG_AND_RETURN("unknown key '%s'", key
);
1418 /* add rule token and sort tokens */
1419 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1420 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1421 LOG_RULE_ERROR("failed to add rule token");
1424 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1425 _cleanup_fclose_
FILE *f
= NULL
;
1426 unsigned int first_token
;
1427 unsigned int filename_off
;
1428 char line
[UTIL_LINE_SIZE
];
1432 f
= fopen(filename
, "re");
1434 if (errno
== ENOENT
)
1440 if (null_or_empty_fd(fileno(f
))) {
1441 log_debug("Skipping empty file: %s", filename
);
1444 log_debug("Reading rules file: %s", filename
);
1446 first_token
= rules
->token_cur
;
1447 filename_off
= rules_add_string(rules
, filename
);
1449 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1453 /* skip whitespace */
1456 while (isspace(key
[0]))
1467 /* continue reading if backslash+newline is found */
1468 while (line
[len
-2] == '\\') {
1469 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1471 if (strlen(&line
[len
-2]) < 2)
1477 if (len
+1 >= sizeof(line
)) {
1478 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1481 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1484 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1485 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1486 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1487 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1490 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1491 if (rules
->tokens
[j
].type
!= TK_RULE
)
1493 if (rules
->tokens
[j
].rule
.label_off
== 0)
1495 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1497 rules
->tokens
[i
].key
.rule_goto
= j
;
1500 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1501 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1507 struct udev_rules
*udev_rules_new(int resolve_names
) {
1508 struct udev_rules
*rules
;
1509 struct udev_list file_list
;
1510 struct token end_token
;
1514 rules
= new0(struct udev_rules
, 1);
1517 rules
->resolve_names
= resolve_names
;
1518 udev_list_init(NULL
, &file_list
, true);
1520 /* init token array and string buffer */
1521 rules
->tokens
= malloc_multiply(PREALLOC_TOKEN
, sizeof(struct token
));
1522 if (rules
->tokens
== NULL
)
1523 return udev_rules_unref(rules
);
1524 rules
->token_max
= PREALLOC_TOKEN
;
1526 rules
->strbuf
= strbuf_new();
1528 return udev_rules_unref(rules
);
1530 udev_rules_check_timestamp(rules
);
1532 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1534 log_error_errno(r
, "failed to enumerate rules files: %m");
1535 return udev_rules_unref(rules
);
1539 * The offset value in the rules strct is limited; add all
1540 * rules file names to the beginning of the string buffer.
1542 STRV_FOREACH(f
, files
)
1543 rules_add_string(rules
, *f
);
1545 STRV_FOREACH(f
, files
)
1546 parse_file(rules
, *f
);
1550 memzero(&end_token
, sizeof(struct token
));
1551 end_token
.type
= TK_END
;
1552 add_token(rules
, &end_token
);
1553 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1554 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1556 /* cleanup temporary strbuf data */
1557 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1558 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1559 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1560 strbuf_complete(rules
->strbuf
);
1562 /* cleanup uid/gid cache */
1563 rules
->uids
= mfree(rules
->uids
);
1564 rules
->uids_cur
= 0;
1565 rules
->uids_max
= 0;
1566 rules
->gids
= mfree(rules
->gids
);
1567 rules
->gids_cur
= 0;
1568 rules
->gids_max
= 0;
1574 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1577 free(rules
->tokens
);
1578 strbuf_cleanup(rules
->strbuf
);
1581 return mfree(rules
);
1584 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1588 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1591 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1592 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1599 switch (token
->key
.glob
) {
1601 match
= (streq(key_value
, val
));
1604 match
= (fnmatch(key_value
, val
, 0) == 0);
1611 s
= rules_str(rules
, token
->key
.value_off
);
1616 next
= strchr(s
, '|');
1618 size_t matchlen
= (size_t)(next
- s
);
1620 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1624 match
= (streq(s
, val
));
1633 char value
[UTIL_PATH_SIZE
];
1635 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1637 while (key_value
!= NULL
) {
1638 pos
= strchr(key_value
, '|');
1643 match
= (fnmatch(key_value
, val
, 0) == 0);
1651 match
= (val
[0] != '\0');
1657 if (match
&& (token
->key
.op
== OP_MATCH
))
1659 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1664 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1666 char nbuf
[UTIL_NAME_SIZE
];
1668 char vbuf
[UTIL_NAME_SIZE
];
1671 name
= rules_str(rules
, cur
->key
.attr_off
);
1672 switch (cur
->key
.attrsubst
) {
1674 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1678 value
= udev_device_get_sysattr_value(dev
, name
);
1683 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), 1) != 0)
1691 /* remove trailing whitespace, if not asked to match for it */
1692 len
= strlen(value
);
1693 if (len
> 0 && isspace(value
[len
-1])) {
1694 const char *key_value
;
1697 key_value
= rules_str(rules
, cur
->key
.value_off
);
1698 klen
= strlen(key_value
);
1699 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1700 if (value
!= vbuf
) {
1701 strscpy(vbuf
, sizeof(vbuf
), value
);
1704 while (len
> 0 && isspace(vbuf
[--len
]))
1709 return match_key(rules
, cur
, value
);
1718 void udev_rules_apply_to_event(struct udev_rules
*rules
,
1719 struct udev_event
*event
,
1720 usec_t timeout_usec
,
1721 usec_t timeout_warn_usec
,
1722 struct udev_list
*properties_list
) {
1725 enum escape_type esc
= ESCAPE_UNSET
;
1729 if (rules
->tokens
== NULL
)
1732 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1733 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1734 udev_device_get_ifindex(event
->dev
) > 0));
1736 /* loop through token list, match, run actions or forward to next rule */
1737 cur
= &rules
->tokens
[0];
1740 dump_token(rules
, cur
);
1741 switch (cur
->type
) {
1745 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1746 if (!can_set_name
&& rule
->rule
.can_set_name
)
1751 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1755 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1759 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1762 case TK_M_DEVLINK
: {
1763 struct udev_list_entry
*list_entry
;
1766 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1767 const char *devlink
;
1769 devlink
= udev_list_entry_get_name(list_entry
) + STRLEN("/dev/");
1770 if (match_key(rules
, cur
, devlink
) == 0) {
1780 if (match_key(rules
, cur
, event
->name
) != 0)
1784 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1787 value
= udev_device_get_property_value(event
->dev
, key_name
);
1789 /* check global properties */
1790 if (!value
&& properties_list
) {
1791 struct udev_list_entry
*list_entry
;
1793 list_entry
= udev_list_get_entry(properties_list
);
1794 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1795 if (list_entry
!= NULL
)
1796 value
= udev_list_entry_get_value(list_entry
);
1801 if (match_key(rules
, cur
, value
))
1806 struct udev_list_entry
*list_entry
;
1809 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1810 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1815 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1816 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1820 case TK_M_SUBSYSTEM
:
1821 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1825 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1829 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1833 char filename
[UTIL_PATH_SIZE
];
1834 _cleanup_free_
char *value
= NULL
;
1837 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1838 sysctl_normalize(filename
);
1839 if (sysctl_read(filename
, &value
) < 0)
1842 len
= strlen(value
);
1843 while (len
> 0 && isspace(value
[--len
]))
1845 if (match_key(rules
, cur
, value
) != 0)
1850 case TK_M_SUBSYSTEMS
:
1856 /* get whole sequence of parent matches */
1858 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1861 /* loop over parents */
1862 event
->dev_parent
= event
->dev
;
1866 /* loop over sequence of parent match keys */
1867 for (key
= cur
; key
< next
; key
++ ) {
1868 dump_token(rules
, key
);
1871 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1874 case TK_M_SUBSYSTEMS
:
1875 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1879 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1883 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1887 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1889 if (match
&& key
->key
.op
== OP_NOMATCH
)
1891 if (!match
&& key
->key
.op
== OP_MATCH
)
1902 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1903 if (event
->dev_parent
== NULL
)
1906 /* move behind our sequence of parent match keys */
1911 char filename
[UTIL_PATH_SIZE
];
1912 struct stat statbuf
;
1915 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1916 if (util_resolve_subsys_kernel(filename
, filename
, sizeof(filename
), 0) != 0) {
1917 if (filename
[0] != '/') {
1918 char tmp
[UTIL_PATH_SIZE
];
1920 strscpy(tmp
, sizeof(tmp
), filename
);
1921 strscpyl(filename
, sizeof(filename
),
1922 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
1925 attr_subst_subdir(filename
, sizeof(filename
));
1927 match
= (stat(filename
, &statbuf
) == 0);
1928 if (match
&& cur
->key
.mode
> 0)
1929 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1930 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1932 if (!match
&& cur
->key
.op
== OP_MATCH
)
1936 case TK_M_PROGRAM
: {
1937 char program
[UTIL_PATH_SIZE
];
1938 char result
[UTIL_LINE_SIZE
];
1940 event
->program_result
= mfree(event
->program_result
);
1941 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1942 log_debug("PROGRAM '%s' %s:%u",
1944 rules_str(rules
, rule
->rule
.filename_off
),
1945 rule
->rule
.filename_line
);
1947 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
1948 if (cur
->key
.op
!= OP_NOMATCH
)
1953 delete_trailing_chars(result
, "\n");
1954 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1955 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1957 log_debug("%i character(s) replaced" , count
);
1959 event
->program_result
= strdup(result
);
1960 if (cur
->key
.op
== OP_NOMATCH
)
1965 case TK_M_IMPORT_FILE
: {
1966 char import
[UTIL_PATH_SIZE
];
1968 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1969 if (import_file_into_properties(event
->dev
, import
) != 0)
1970 if (cur
->key
.op
!= OP_NOMATCH
)
1974 case TK_M_IMPORT_PROG
: {
1975 char import
[UTIL_PATH_SIZE
];
1977 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1978 log_debug("IMPORT '%s' %s:%u",
1980 rules_str(rules
, rule
->rule
.filename_off
),
1981 rule
->rule
.filename_line
);
1983 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
1984 if (cur
->key
.op
!= OP_NOMATCH
)
1988 case TK_M_IMPORT_BUILTIN
: {
1989 char command
[UTIL_PATH_SIZE
];
1991 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
1992 /* check if we ran already */
1993 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
1994 log_debug("IMPORT builtin skip '%s' %s:%u",
1995 udev_builtin_name(cur
->key
.builtin_cmd
),
1996 rules_str(rules
, rule
->rule
.filename_off
),
1997 rule
->rule
.filename_line
);
1998 /* return the result from earlier run */
1999 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2000 if (cur
->key
.op
!= OP_NOMATCH
)
2005 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2008 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2009 log_debug("IMPORT builtin '%s' %s:%u",
2010 udev_builtin_name(cur
->key
.builtin_cmd
),
2011 rules_str(rules
, rule
->rule
.filename_off
),
2012 rule
->rule
.filename_line
);
2014 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2015 /* remember failure */
2016 log_debug("IMPORT builtin '%s' returned non-zero",
2017 udev_builtin_name(cur
->key
.builtin_cmd
));
2018 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2019 if (cur
->key
.op
!= OP_NOMATCH
)
2024 case TK_M_IMPORT_DB
: {
2025 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2028 value
= udev_device_get_property_value(event
->dev_db
, key
);
2030 udev_device_add_property(event
->dev
, key
, value
);
2032 if (cur
->key
.op
!= OP_NOMATCH
)
2037 case TK_M_IMPORT_CMDLINE
: {
2038 _cleanup_free_
char *value
= NULL
;
2039 bool imported
= false;
2042 key
= rules_str(rules
, cur
->key
.value_off
);
2044 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2046 log_debug_errno(r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2051 udev_device_add_property(event
->dev
, key
, value
);
2053 /* we import simple flags as 'FLAG=1' */
2054 udev_device_add_property(event
->dev
, key
, "1");
2057 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2061 case TK_M_IMPORT_PARENT
: {
2062 char import
[UTIL_PATH_SIZE
];
2064 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2065 if (import_parent_into_properties(event
->dev
, import
) != 0)
2066 if (cur
->key
.op
!= OP_NOMATCH
)
2071 if (match_key(rules
, cur
, event
->program_result
) != 0)
2074 case TK_A_STRING_ESCAPE_NONE
:
2077 case TK_A_STRING_ESCAPE_REPLACE
:
2078 esc
= ESCAPE_REPLACE
;
2080 case TK_A_DB_PERSIST
:
2081 udev_device_set_db_persist(event
->dev
);
2083 case TK_A_INOTIFY_WATCH
:
2084 if (event
->inotify_watch_final
)
2086 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2087 event
->inotify_watch_final
= true;
2088 event
->inotify_watch
= cur
->key
.watch
;
2090 case TK_A_DEVLINK_PRIO
:
2091 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2094 char owner
[UTIL_NAME_SIZE
];
2095 const char *ow
= owner
;
2097 if (event
->owner_final
)
2099 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2100 event
->owner_final
= true;
2101 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2102 event
->owner_set
= true;
2103 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2105 log_unknown_owner(r
, "user", owner
);
2108 log_debug("OWNER %u %s:%u",
2110 rules_str(rules
, rule
->rule
.filename_off
),
2111 rule
->rule
.filename_line
);
2115 char group
[UTIL_NAME_SIZE
];
2116 const char *gr
= group
;
2118 if (event
->group_final
)
2120 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2121 event
->group_final
= true;
2122 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2123 event
->group_set
= true;
2124 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2126 log_unknown_owner(r
, "group", group
);
2129 log_debug("GROUP %u %s:%u",
2131 rules_str(rules
, rule
->rule
.filename_off
),
2132 rule
->rule
.filename_line
);
2136 char mode_str
[UTIL_NAME_SIZE
];
2140 if (event
->mode_final
)
2142 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2143 mode
= strtol(mode_str
, &endptr
, 8);
2144 if (endptr
[0] != '\0') {
2145 log_error("ignoring invalid mode '%s'", mode_str
);
2148 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2149 event
->mode_final
= true;
2150 event
->mode_set
= true;
2152 log_debug("MODE %#o %s:%u",
2154 rules_str(rules
, rule
->rule
.filename_off
),
2155 rule
->rule
.filename_line
);
2159 if (event
->owner_final
)
2161 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2162 event
->owner_final
= true;
2163 event
->owner_set
= true;
2164 event
->uid
= cur
->key
.uid
;
2165 log_debug("OWNER %u %s:%u",
2167 rules_str(rules
, rule
->rule
.filename_off
),
2168 rule
->rule
.filename_line
);
2171 if (event
->group_final
)
2173 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2174 event
->group_final
= true;
2175 event
->group_set
= true;
2176 event
->gid
= cur
->key
.gid
;
2177 log_debug("GROUP %u %s:%u",
2179 rules_str(rules
, rule
->rule
.filename_off
),
2180 rule
->rule
.filename_line
);
2183 if (event
->mode_final
)
2185 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2186 event
->mode_final
= true;
2187 event
->mode_set
= true;
2188 event
->mode
= cur
->key
.mode
;
2189 log_debug("MODE %#o %s:%u",
2191 rules_str(rules
, rule
->rule
.filename_off
),
2192 rule
->rule
.filename_line
);
2194 case TK_A_SECLABEL
: {
2195 char label_str
[UTIL_LINE_SIZE
] = {};
2196 const char *name
, *label
;
2198 name
= rules_str(rules
, cur
->key
.attr_off
);
2199 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2200 if (label_str
[0] != '\0')
2203 label
= rules_str(rules
, cur
->key
.value_off
);
2205 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2206 udev_list_cleanup(&event
->seclabel_list
);
2207 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2208 log_debug("SECLABEL{%s}='%s' %s:%u",
2210 rules_str(rules
, rule
->rule
.filename_off
),
2211 rule
->rule
.filename_line
);
2215 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2216 char *value
= rules_str(rules
, cur
->key
.value_off
);
2217 char value_new
[UTIL_NAME_SIZE
];
2218 const char *value_old
= NULL
;
2220 if (value
[0] == '\0') {
2221 if (cur
->key
.op
== OP_ADD
)
2223 udev_device_add_property(event
->dev
, name
, NULL
);
2227 if (cur
->key
.op
== OP_ADD
)
2228 value_old
= udev_device_get_property_value(event
->dev
, name
);
2230 char temp
[UTIL_NAME_SIZE
];
2232 /* append value separated by space */
2233 udev_event_apply_format(event
, value
, temp
, sizeof(temp
), false);
2234 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2236 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
), false);
2238 udev_device_add_property(event
->dev
, name
, value_new
);
2242 char tag
[UTIL_PATH_SIZE
];
2245 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2246 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2247 udev_device_cleanup_tags_list(event
->dev
);
2248 for (p
= tag
; *p
!= '\0'; p
++) {
2249 if ((*p
>= 'a' && *p
<= 'z') ||
2250 (*p
>= 'A' && *p
<= 'Z') ||
2251 (*p
>= '0' && *p
<= '9') ||
2252 IN_SET(*p
, '-', '_'))
2254 log_error("ignoring invalid tag name '%s'", tag
);
2257 if (cur
->key
.op
== OP_REMOVE
)
2258 udev_device_remove_tag(event
->dev
, tag
);
2260 udev_device_add_tag(event
->dev
, tag
);
2264 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2266 char name_str
[UTIL_PATH_SIZE
];
2269 if (event
->name_final
)
2271 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2272 event
->name_final
= true;
2273 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2274 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2275 count
= util_replace_chars(name_str
, "/");
2277 log_debug("%i character(s) replaced", count
);
2279 if (major(udev_device_get_devnum(event
->dev
)) &&
2280 !streq(name_str
, udev_device_get_devnode(event
->dev
) + STRLEN("/dev/"))) {
2281 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2283 rules_str(rules
, rule
->rule
.filename_off
),
2284 rule
->rule
.filename_line
);
2287 if (free_and_strdup(&event
->name
, name_str
) < 0) {
2291 log_debug("NAME '%s' %s:%u",
2293 rules_str(rules
, rule
->rule
.filename_off
),
2294 rule
->rule
.filename_line
);
2297 case TK_A_DEVLINK
: {
2298 char temp
[UTIL_PATH_SIZE
];
2299 char filename
[UTIL_PATH_SIZE
];
2303 if (event
->devlink_final
)
2305 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2307 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2308 event
->devlink_final
= true;
2309 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2310 udev_device_cleanup_devlinks_list(event
->dev
);
2312 /* allow multiple symlinks separated by spaces */
2313 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2314 if (esc
== ESCAPE_UNSET
)
2315 count
= util_replace_chars(temp
, "/ ");
2316 else if (esc
== ESCAPE_REPLACE
)
2317 count
= util_replace_chars(temp
, "/");
2319 log_debug("%i character(s) replaced" , count
);
2321 while (isspace(pos
[0]))
2323 next
= strchr(pos
, ' ');
2324 while (next
!= NULL
) {
2326 log_debug("LINK '%s' %s:%u", pos
,
2327 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2328 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2329 udev_device_add_devlink(event
->dev
, filename
);
2330 while (isspace(next
[1]))
2333 next
= strchr(pos
, ' ');
2335 if (pos
[0] != '\0') {
2336 log_debug("LINK '%s' %s:%u", pos
,
2337 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2338 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2339 udev_device_add_devlink(event
->dev
, filename
);
2344 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2345 char attr
[UTIL_PATH_SIZE
];
2346 char value
[UTIL_NAME_SIZE
];
2347 _cleanup_fclose_
FILE *f
= NULL
;
2349 if (util_resolve_subsys_kernel(key_name
, attr
, sizeof(attr
), 0) != 0)
2350 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2351 attr_subst_subdir(attr
, sizeof(attr
));
2353 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2354 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2355 rules_str(rules
, rule
->rule
.filename_off
),
2356 rule
->rule
.filename_line
);
2357 f
= fopen(attr
, "we");
2359 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2360 else if (fprintf(f
, "%s", value
) <= 0)
2361 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2365 char filename
[UTIL_PATH_SIZE
];
2366 char value
[UTIL_NAME_SIZE
];
2368 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2369 sysctl_normalize(filename
);
2370 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2371 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2372 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2373 r
= sysctl_write(filename
, value
);
2375 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2378 case TK_A_RUN_BUILTIN
:
2379 case TK_A_RUN_PROGRAM
: {
2380 struct udev_list_entry
*entry
;
2382 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2383 udev_list_cleanup(&event
->run_list
);
2384 log_debug("RUN '%s' %s:%u",
2385 rules_str(rules
, cur
->key
.value_off
),
2386 rules_str(rules
, rule
->rule
.filename_off
),
2387 rule
->rule
.filename_line
);
2388 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2389 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2393 if (cur
->key
.rule_goto
== 0)
2395 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2400 case TK_M_PARENTS_MIN
:
2401 case TK_M_PARENTS_MAX
:
2404 log_error("wrong type %u", cur
->type
);
2411 /* fast-forward to next rule */
2412 cur
= rule
+ rule
->rule
.token_count
;
2416 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2422 _cleanup_strv_free_
char **tags
= NULL
;
2425 _cleanup_free_
char *path
= NULL
;
2428 if (rules
->tokens
== NULL
)
2431 cur
= &rules
->tokens
[0];
2434 switch (cur
->type
) {
2439 /* skip rules without a static_node tag */
2440 if (!rule
->rule
.has_static_node
)
2446 tags
= strv_free(tags
);
2455 mode
= cur
->key
.mode
;
2458 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2463 case TK_A_STATIC_NODE
: {
2464 char device_node
[UTIL_PATH_SIZE
];
2465 char tags_dir
[UTIL_PATH_SIZE
];
2466 char tag_symlink
[UTIL_PATH_SIZE
];
2469 /* we assure, that the permissions tokens are sorted before the static token */
2471 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2474 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2475 if (stat(device_node
, &stats
) != 0)
2477 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2480 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2482 STRV_FOREACH(t
, tags
) {
2483 _cleanup_free_
char *unescaped_filename
= NULL
;
2485 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2486 r
= mkdir_p(tags_dir
, 0755);
2488 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2490 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2492 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2493 r
= symlink(device_node
, tag_symlink
);
2494 if (r
< 0 && errno
!= EEXIST
)
2495 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2496 tag_symlink
, device_node
);
2500 /* don't touch the permissions if only the tags were set */
2501 if (mode
== 0 && uid
== 0 && gid
== 0)
2510 if (mode
!= (stats
.st_mode
& 01777)) {
2511 r
= chmod(device_node
, mode
);
2513 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2516 log_debug("chmod '%s' %#o", device_node
, mode
);
2519 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2520 r
= chown(device_node
, uid
, gid
);
2522 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2523 device_node
, uid
, gid
);
2525 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2528 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2538 /* fast-forward to next rule */
2539 cur
= rule
+ rule
->rule
.token_count
;
2546 fchmod(fileno(f
), 0644);
2547 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2548 unlink_noerrno("/run/udev/static_node-tags");
2549 unlink_noerrno(path
);