1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "alloc-util.h"
33 #include "conf-files.h"
34 #include "dirent-util.h"
38 #include "glob-util.h"
39 #include "path-util.h"
40 #include "proc-cmdline.h"
41 #include "stat-util.h"
42 #include "stdio-util.h"
44 #include "string-util.h"
46 #include "sysctl-util.h"
48 #include "user-util.h"
51 #define PREALLOC_TOKEN 2048
54 unsigned int name_off
;
61 static const char* const rules_dirs
[] = {
64 UDEVLIBEXECDIR
"/rules.d",
73 /* every key in the rules file becomes a token */
75 unsigned int token_cur
;
76 unsigned int token_max
;
78 /* all key strings are copied and de-duplicated in a single continuous string buffer */
79 struct strbuf
*strbuf
;
81 /* during rule parsing, uid/gid lookup results are cached */
83 unsigned int uids_cur
;
84 unsigned int uids_max
;
86 unsigned int gids_cur
;
87 unsigned int gids_max
;
90 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
91 return rules
->strbuf
->buf
+ off
;
94 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
95 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
98 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
112 enum string_glob_type
{
114 GL_PLAIN
, /* no special chars */
115 GL_GLOB
, /* shell globs ?,*,[] */
116 GL_SPLIT
, /* multi-value A|B */
117 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
118 GL_SOMETHING
, /* commonly used "?*" */
121 enum string_subst_type
{
128 /* tokens of a rule are sorted/handled in this order */
133 TK_M_ACTION
, /* val */
134 TK_M_DEVPATH
, /* val */
135 TK_M_KERNEL
, /* val */
136 TK_M_DEVLINK
, /* val */
138 TK_M_ENV
, /* val, attr */
140 TK_M_SUBSYSTEM
, /* val */
141 TK_M_DRIVER
, /* val */
142 TK_M_WAITFOR
, /* val */
143 TK_M_ATTR
, /* val, attr */
144 TK_M_SYSCTL
, /* val, attr */
147 TK_M_KERNELS
, /* val */
148 TK_M_SUBSYSTEMS
, /* val */
149 TK_M_DRIVERS
, /* val */
150 TK_M_ATTRS
, /* val, attr */
154 TK_M_TEST
, /* val, mode_t */
155 TK_M_PROGRAM
, /* val */
156 TK_M_IMPORT_FILE
, /* val */
157 TK_M_IMPORT_PROG
, /* val */
158 TK_M_IMPORT_BUILTIN
, /* val */
159 TK_M_IMPORT_DB
, /* val */
160 TK_M_IMPORT_CMDLINE
, /* val */
161 TK_M_IMPORT_PARENT
, /* val */
162 TK_M_RESULT
, /* val */
165 TK_A_STRING_ESCAPE_NONE
,
166 TK_A_STRING_ESCAPE_REPLACE
,
168 TK_A_INOTIFY_WATCH
, /* int */
169 TK_A_DEVLINK_PRIO
, /* int */
170 TK_A_OWNER
, /* val */
171 TK_A_GROUP
, /* val */
173 TK_A_OWNER_ID
, /* uid_t */
174 TK_A_GROUP_ID
, /* gid_t */
175 TK_A_MODE_ID
, /* mode_t */
177 TK_A_STATIC_NODE
, /* val */
178 TK_A_SECLABEL
, /* val, attr */
179 TK_A_ENV
, /* val, attr */
181 TK_A_DEVLINK
, /* val */
182 TK_A_ATTR
, /* val, attr */
183 TK_A_SYSCTL
, /* val, attr */
184 TK_A_RUN_BUILTIN
, /* val, bool */
185 TK_A_RUN_PROGRAM
, /* val, bool */
186 TK_A_GOTO
, /* size_t */
191 /* we try to pack stuff in a way that we take only 12 bytes per token */
194 unsigned char type
; /* same in rule and key */
196 enum token_type type
:8;
198 bool has_static_node
:1;
199 unsigned int unused
:6;
200 unsigned short token_count
;
201 unsigned int label_off
;
202 unsigned short filename_off
;
203 unsigned short filename_line
;
206 enum token_type type
:8;
207 enum operation_type op
:8;
208 enum string_glob_type glob
:8;
209 enum string_subst_type subst
:4;
210 enum string_subst_type attrsubst
:4;
211 unsigned int value_off
;
213 unsigned int attr_off
;
214 unsigned int rule_goto
;
220 enum udev_builtin_cmd builtin_cmd
;
228 struct udev_rules
*rules
;
230 struct token token
[MAX_TK
];
231 unsigned int token_cur
;
235 static const char *operation_str(enum operation_type type
) {
236 static const char *operation_strs
[] = {
237 [OP_UNSET
] = "UNSET",
238 [OP_MATCH
] = "match",
239 [OP_NOMATCH
] = "nomatch",
240 [OP_MATCH_MAX
] = "MATCH_MAX",
243 [OP_REMOVE
] = "remove",
244 [OP_ASSIGN
] = "assign",
245 [OP_ASSIGN_FINAL
] = "assign-final",
248 return operation_strs
[type
];
251 static const char *string_glob_str(enum string_glob_type type
) {
252 static const char *string_glob_strs
[] = {
253 [GL_UNSET
] = "UNSET",
254 [GL_PLAIN
] = "plain",
256 [GL_SPLIT
] = "split",
257 [GL_SPLIT_GLOB
] = "split-glob",
258 [GL_SOMETHING
] = "split-glob",
261 return string_glob_strs
[type
];
264 static const char *token_str(enum token_type type
) {
265 static const char *token_strs
[] = {
266 [TK_UNSET
] = "UNSET",
269 [TK_M_ACTION
] = "M ACTION",
270 [TK_M_DEVPATH
] = "M DEVPATH",
271 [TK_M_KERNEL
] = "M KERNEL",
272 [TK_M_DEVLINK
] = "M DEVLINK",
273 [TK_M_NAME
] = "M NAME",
274 [TK_M_ENV
] = "M ENV",
275 [TK_M_TAG
] = "M TAG",
276 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
277 [TK_M_DRIVER
] = "M DRIVER",
278 [TK_M_WAITFOR
] = "M WAITFOR",
279 [TK_M_ATTR
] = "M ATTR",
280 [TK_M_SYSCTL
] = "M SYSCTL",
282 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
283 [TK_M_KERNELS
] = "M KERNELS",
284 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
285 [TK_M_DRIVERS
] = "M DRIVERS",
286 [TK_M_ATTRS
] = "M ATTRS",
287 [TK_M_TAGS
] = "M TAGS",
288 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
290 [TK_M_TEST
] = "M TEST",
291 [TK_M_PROGRAM
] = "M PROGRAM",
292 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
293 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
294 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
295 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
296 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
297 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
298 [TK_M_RESULT
] = "M RESULT",
299 [TK_M_MAX
] = "M MAX",
301 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
302 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
303 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
304 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
305 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
306 [TK_A_OWNER
] = "A OWNER",
307 [TK_A_GROUP
] = "A GROUP",
308 [TK_A_MODE
] = "A MODE",
309 [TK_A_OWNER_ID
] = "A OWNER_ID",
310 [TK_A_GROUP_ID
] = "A GROUP_ID",
311 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
312 [TK_A_SECLABEL
] = "A SECLABEL",
313 [TK_A_MODE_ID
] = "A MODE_ID",
314 [TK_A_ENV
] = "A ENV",
315 [TK_A_TAG
] = "A ENV",
316 [TK_A_NAME
] = "A NAME",
317 [TK_A_DEVLINK
] = "A DEVLINK",
318 [TK_A_ATTR
] = "A ATTR",
319 [TK_A_SYSCTL
] = "A SYSCTL",
320 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
321 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
322 [TK_A_GOTO
] = "A GOTO",
327 return token_strs
[type
];
330 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
331 enum token_type type
= token
->type
;
332 enum operation_type op
= token
->key
.op
;
333 enum string_glob_type glob
= token
->key
.glob
;
334 const char *value
= rules_str(rules
, token
->key
.value_off
);
335 const char *attr
= &rules
->strbuf
->buf
[token
->key
.attr_off
];
340 const char *tks_ptr
= (char *)rules
->tokens
;
341 const char *tk_ptr
= (char *)token
;
342 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
344 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
345 &rules
->strbuf
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
346 idx
, token
->rule
.token_count
,
347 &rules
->strbuf
->buf
[token
->rule
.label_off
]);
359 case TK_M_SUBSYSTEMS
:
363 case TK_M_IMPORT_FILE
:
364 case TK_M_IMPORT_PROG
:
366 case TK_M_IMPORT_CMDLINE
:
367 case TK_M_IMPORT_PARENT
:
374 case TK_A_RUN_BUILTIN
:
375 case TK_A_RUN_PROGRAM
:
376 log_debug("%s %s '%s'(%s)",
377 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
379 case TK_M_IMPORT_BUILTIN
:
380 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
389 log_debug("%s %s '%s' '%s'(%s)",
390 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
394 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
396 case TK_A_STRING_ESCAPE_NONE
:
397 case TK_A_STRING_ESCAPE_REPLACE
:
398 case TK_A_DB_PERSIST
:
399 log_debug("%s", token_str(type
));
402 log_debug("%s %s '%s'(%s) %#o",
403 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
405 case TK_A_INOTIFY_WATCH
:
406 log_debug("%s %u", token_str(type
), token
->key
.watch
);
408 case TK_A_DEVLINK_PRIO
:
409 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
412 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
415 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
418 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
420 case TK_A_STATIC_NODE
:
421 log_debug("%s '%s'", token_str(type
), value
);
424 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
427 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
430 log_debug("* %s", token_str(type
));
432 case TK_M_PARENTS_MIN
:
433 case TK_M_PARENTS_MAX
:
436 log_debug("unknown type %u", type
);
441 static void dump_rules(struct udev_rules
*rules
) {
444 log_debug("dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings",
446 rules
->token_cur
* sizeof(struct token
),
447 rules
->strbuf
->nodes_count
,
449 for (i
= 0; i
< rules
->token_cur
; i
++)
450 dump_token(rules
, &rules
->tokens
[i
]);
453 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
454 static inline void dump_rules(struct udev_rules
*rules
) {}
457 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
458 /* grow buffer if needed */
459 if (rules
->token_cur
+1 >= rules
->token_max
) {
460 struct token
*tokens
;
463 /* double the buffer size */
464 add
= rules
->token_max
;
468 tokens
= reallocarray(rules
->tokens
, rules
->token_max
+ add
, sizeof(struct token
));
471 rules
->tokens
= tokens
;
472 rules
->token_max
+= add
;
474 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
479 static void log_unknown_owner(int error
, const char *entity
, const char *owner
) {
480 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
481 log_error("Specified %s '%s' unknown", entity
, owner
);
483 log_error_errno(error
, "Error resolving %s '%s': %m", entity
, owner
);
486 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
492 /* lookup, if we know it already */
493 for (i
= 0; i
< rules
->uids_cur
; i
++) {
494 off
= rules
->uids
[i
].name_off
;
495 if (streq(rules_str(rules
, off
), owner
)) {
496 uid
= rules
->uids
[i
].uid
;
500 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
502 log_unknown_owner(r
, "user", owner
);
504 /* grow buffer if needed */
505 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
506 struct uid_gid
*uids
;
509 /* double the buffer size */
510 add
= rules
->uids_max
;
514 uids
= reallocarray(rules
->uids
, rules
->uids_max
+ add
, sizeof(struct uid_gid
));
518 rules
->uids_max
+= add
;
520 rules
->uids
[rules
->uids_cur
].uid
= uid
;
521 off
= rules_add_string(rules
, owner
);
524 rules
->uids
[rules
->uids_cur
].name_off
= off
;
529 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
535 /* lookup, if we know it already */
536 for (i
= 0; i
< rules
->gids_cur
; i
++) {
537 off
= rules
->gids
[i
].name_off
;
538 if (streq(rules_str(rules
, off
), group
)) {
539 gid
= rules
->gids
[i
].gid
;
543 r
= get_group_creds(&group
, &gid
);
545 log_unknown_owner(r
, "group", group
);
547 /* grow buffer if needed */
548 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
549 struct uid_gid
*gids
;
552 /* double the buffer size */
553 add
= rules
->gids_max
;
557 gids
= reallocarray(rules
->gids
, rules
->gids_max
+ add
, sizeof(struct uid_gid
));
561 rules
->gids_max
+= add
;
563 rules
->gids
[rules
->gids_cur
].gid
= gid
;
564 off
= rules_add_string(rules
, group
);
567 rules
->gids
[rules
->gids_cur
].name_off
= off
;
572 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
579 while (isspace(key
[0]))
582 /* comment or empty line */
583 if (IN_SET(key
[0], '#', '\0'))
586 /* split key/value */
587 val
= strchr(key
, '=');
594 while (isspace(val
[0]))
601 while (isspace(key
[len
-1]))
605 /* terminate value */
609 while (isspace(val
[len
-1]))
617 if (IN_SET(val
[0], '"', '\'')) {
618 if (len
== 1 || val
[len
-1] != val
[0]) {
619 log_debug("inconsistent quoting: '%s', skip", line
);
626 udev_device_add_property(dev
, key
, val
);
631 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
633 char line
[UTIL_LINE_SIZE
];
635 f
= fopen(filename
, "re");
638 while (fgets(line
, sizeof(line
), f
) != NULL
)
639 import_property_from_string(dev
, line
);
644 static int import_program_into_properties(struct udev_event
*event
,
646 usec_t timeout_warn_usec
,
647 const char *program
) {
648 char result
[UTIL_LINE_SIZE
];
652 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
657 while (line
!= NULL
) {
660 pos
= strchr(line
, '\n');
665 import_property_from_string(event
->dev
, line
);
671 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
672 struct udev_device
*dev_parent
;
673 struct udev_list_entry
*list_entry
;
678 dev_parent
= udev_device_get_parent(dev
);
679 if (dev_parent
== NULL
)
682 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
683 const char *key
= udev_list_entry_get_name(list_entry
);
684 const char *val
= udev_list_entry_get_value(list_entry
);
686 if (fnmatch(filter
, key
, 0) == 0)
687 udev_device_add_property(dev
, key
, val
);
692 static void attr_subst_subdir(char *attr
, size_t len
) {
693 const char *pos
, *tail
, *path
;
694 _cleanup_closedir_
DIR *dir
= NULL
;
697 pos
= strstr(attr
, "/*/");
702 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
707 FOREACH_DIRENT_ALL(dent
, dir
, break)
708 if (dent
->d_name
[0] != '.') {
709 char n
[strlen(dent
->d_name
) + strlen(tail
) + 1];
711 strscpyl(n
, sizeof n
, dent
->d_name
, tail
, NULL
);
712 if (faccessat(dirfd(dir
), n
, F_OK
, 0) == 0) {
713 strscpyl(attr
, len
, path
, n
, NULL
);
719 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
725 if (linepos
== NULL
|| linepos
[0] == '\0')
728 /* skip whitespace */
729 while (isspace(linepos
[0]) || linepos
[0] == ',')
733 if (linepos
[0] == '\0')
739 if (linepos
[0] == '\0')
741 if (isspace(linepos
[0]))
743 if (linepos
[0] == '=')
745 if (IN_SET(linepos
[0], '+', '-', '!', ':'))
746 if (linepos
[1] == '=')
750 /* remember end of key */
753 /* skip whitespace after key */
754 while (isspace(linepos
[0]))
756 if (linepos
[0] == '\0')
759 /* get operation type */
760 if (linepos
[0] == '=' && linepos
[1] == '=') {
763 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
766 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
769 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
772 } else if (linepos
[0] == '=') {
775 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
776 *op
= OP_ASSIGN_FINAL
;
784 /* skip whitespace after operator */
785 while (isspace(linepos
[0]))
787 if (linepos
[0] == '\0')
791 if (linepos
[0] == '"')
798 for (i
= 0, j
= 0; ; i
++, j
++) {
800 if (linepos
[i
] == '"')
803 if (linepos
[i
] == '\0')
806 /* double quotes can be escaped */
807 if (linepos
[i
] == '\\')
808 if (linepos
[i
+1] == '"')
811 linepos
[j
] = linepos
[i
];
815 /* move line to next key */
816 *line
= linepos
+ i
+ 1;
820 /* extract possible KEY{attr} */
821 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
825 attr
= strchr(str
, '{');
828 pos
= strchr(attr
, '}');
830 log_error("Missing closing brace for format");
839 static void rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
840 enum operation_type op
,
841 const char *value
, const void *data
) {
842 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
843 const char *attr
= NULL
;
845 assert(rule_tmp
->token_cur
< ELEMENTSOF(rule_tmp
->token
));
846 memzero(token
, sizeof(struct token
));
858 case TK_M_SUBSYSTEMS
:
862 case TK_M_IMPORT_FILE
:
863 case TK_M_IMPORT_PROG
:
865 case TK_M_IMPORT_CMDLINE
:
866 case TK_M_IMPORT_PARENT
:
876 case TK_A_STATIC_NODE
:
877 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
879 case TK_M_IMPORT_BUILTIN
:
880 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
881 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
892 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
893 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
896 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
898 token
->key
.mode
= *(mode_t
*)data
;
900 case TK_A_STRING_ESCAPE_NONE
:
901 case TK_A_STRING_ESCAPE_REPLACE
:
902 case TK_A_DB_PERSIST
:
904 case TK_A_RUN_BUILTIN
:
905 case TK_A_RUN_PROGRAM
:
906 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
907 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
909 case TK_A_INOTIFY_WATCH
:
910 case TK_A_DEVLINK_PRIO
:
911 token
->key
.devlink_prio
= *(int *)data
;
914 token
->key
.uid
= *(uid_t
*)data
;
917 token
->key
.gid
= *(gid_t
*)data
;
920 token
->key
.mode
= *(mode_t
*)data
;
923 case TK_M_PARENTS_MIN
:
924 case TK_M_PARENTS_MAX
:
928 assert_not_reached("wrong type");
931 if (value
!= NULL
&& type
< TK_M_MAX
) {
932 /* check if we need to split or call fnmatch() while matching rules */
933 enum string_glob_type glob
;
937 has_split
= (strchr(value
, '|') != NULL
);
938 has_glob
= string_is_glob(value
);
939 if (has_split
&& has_glob
) {
940 glob
= GL_SPLIT_GLOB
;
941 } else if (has_split
) {
943 } else if (has_glob
) {
944 if (streq(value
, "?*"))
951 token
->key
.glob
= glob
;
954 if (value
!= NULL
&& type
> TK_M_MAX
) {
955 /* check if assigned value has substitution chars */
957 token
->key
.subst
= SB_SUBSYS
;
958 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
959 token
->key
.subst
= SB_FORMAT
;
961 token
->key
.subst
= SB_NONE
;
965 /* check if property/attribute name has substitution chars */
967 token
->key
.attrsubst
= SB_SUBSYS
;
968 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
969 token
->key
.attrsubst
= SB_FORMAT
;
971 token
->key
.attrsubst
= SB_NONE
;
974 token
->key
.type
= type
;
976 rule_tmp
->token_cur
++;
979 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
981 unsigned int start
= 0;
982 unsigned int end
= rule_tmp
->token_cur
;
984 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
985 enum token_type next_val
= TK_UNSET
;
986 unsigned int next_idx
= 0;
989 /* find smallest value */
990 for (j
= start
; j
< end
; j
++) {
991 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
993 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
994 next_val
= rule_tmp
->token
[j
].type
;
999 /* add token and mark done */
1000 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1002 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1005 if (next_idx
== start
)
1007 if (next_idx
+1 == end
)
1013 #define LOG_RULE_ERROR(fmt, ...) log_error("Invalid rule %s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1014 #define LOG_RULE_WARNING(fmt, ...) log_warning("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1015 #define LOG_RULE_DEBUG(fmt, ...) log_debug("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1016 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1018 static void add_rule(struct udev_rules
*rules
, char *line
,
1019 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1022 struct rule_tmp rule_tmp
= {
1024 .rule
.type
= TK_RULE
,
1027 /* the offset in the rule is limited to unsigned short */
1028 if (filename_off
< USHRT_MAX
)
1029 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1030 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1036 enum operation_type op
;
1038 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1039 /* Avoid erroring on trailing whitespace. This is probably rare
1040 * so save the work for the error case instead of always trying
1041 * to strip the trailing whitespace with strstrip(). */
1042 while (isblank(*linepos
))
1045 /* If we aren't at the end of the line, this is a parsing error.
1046 * Make a best effort to describe where the problem is. */
1047 if (!strchr(NEWLINE
, *linepos
)) {
1048 char buf
[2] = {*linepos
};
1049 _cleanup_free_
char *tmp
;
1052 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1053 filename
, lineno
, linepos
- line
+ 1, tmp
);
1054 if (*linepos
== '#')
1055 log_error("hint: comments can only start at beginning of line");
1060 if (rule_tmp
.token_cur
>= ELEMENTSOF(rule_tmp
.token
))
1061 LOG_AND_RETURN("temporary rule array too small, aborting event processing with %u items", rule_tmp
.token_cur
);
1063 if (streq(key
, "ACTION")) {
1064 if (op
> OP_MATCH_MAX
)
1065 LOG_AND_RETURN("invalid %s operation", key
);
1067 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1069 } else if (streq(key
, "DEVPATH")) {
1070 if (op
> OP_MATCH_MAX
)
1071 LOG_AND_RETURN("invalid %s operation", key
);
1073 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1075 } else if (streq(key
, "KERNEL")) {
1076 if (op
> OP_MATCH_MAX
)
1077 LOG_AND_RETURN("invalid %s operation", key
);
1079 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1081 } else if (streq(key
, "SUBSYSTEM")) {
1082 if (op
> OP_MATCH_MAX
)
1083 LOG_AND_RETURN("invalid %s operation", key
);
1085 /* bus, class, subsystem events should all be the same */
1086 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1087 if (!streq(value
, "subsystem"))
1088 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1090 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1092 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1094 } else if (streq(key
, "DRIVER")) {
1095 if (op
> OP_MATCH_MAX
)
1096 LOG_AND_RETURN("invalid %s operation", key
);
1098 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1100 } else if (startswith(key
, "ATTR{")) {
1101 attr
= get_key_attribute(rules
->udev
,
1102 key
+ STRLEN("ATTR"));
1104 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1106 if (op
== OP_REMOVE
)
1107 LOG_AND_RETURN("invalid %s operation", "ATTR");
1109 if (op
< OP_MATCH_MAX
)
1110 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1112 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1114 } else if (startswith(key
, "SYSCTL{")) {
1115 attr
= get_key_attribute(rules
->udev
,
1116 key
+ STRLEN("SYSCTL"));
1118 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1120 if (op
== OP_REMOVE
)
1121 LOG_AND_RETURN("invalid %s operation", "ATTR");
1123 if (op
< OP_MATCH_MAX
)
1124 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1126 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1128 } else if (startswith(key
, "SECLABEL{")) {
1129 attr
= get_key_attribute(rules
->udev
,
1130 key
+ STRLEN("SECLABEL"));
1132 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1134 if (op
== OP_REMOVE
)
1135 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1137 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1139 } else if (streq(key
, "KERNELS")) {
1140 if (op
> OP_MATCH_MAX
)
1141 LOG_AND_RETURN("invalid %s operation", key
);
1143 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1145 } else if (streq(key
, "SUBSYSTEMS")) {
1146 if (op
> OP_MATCH_MAX
)
1147 LOG_AND_RETURN("invalid %s operation", key
);
1149 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1151 } else if (streq(key
, "DRIVERS")) {
1152 if (op
> OP_MATCH_MAX
)
1153 LOG_AND_RETURN("invalid %s operation", key
);
1155 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1157 } else if (startswith(key
, "ATTRS{")) {
1158 if (op
> OP_MATCH_MAX
)
1159 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1161 attr
= get_key_attribute(rules
->udev
,
1162 key
+ STRLEN("ATTRS"));
1164 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1166 if (startswith(attr
, "device/"))
1167 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1168 if (strstr(attr
, "../") != NULL
)
1169 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1170 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1172 } else if (streq(key
, "TAGS")) {
1173 if (op
> OP_MATCH_MAX
)
1174 LOG_AND_RETURN("invalid %s operation", key
);
1176 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1178 } else if (startswith(key
, "ENV{")) {
1179 attr
= get_key_attribute(rules
->udev
,
1180 key
+ STRLEN("ENV"));
1182 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1184 if (op
== OP_REMOVE
)
1185 LOG_AND_RETURN("invalid %s operation", "ENV");
1187 if (op
< OP_MATCH_MAX
)
1188 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1190 if (STR_IN_SET(attr
,
1202 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr
);
1204 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1207 } else if (streq(key
, "TAG")) {
1208 if (op
< OP_MATCH_MAX
)
1209 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1211 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1213 } else if (streq(key
, "PROGRAM")) {
1214 if (op
== OP_REMOVE
)
1215 LOG_AND_RETURN("invalid %s operation", key
);
1217 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1219 } else if (streq(key
, "RESULT")) {
1220 if (op
> OP_MATCH_MAX
)
1221 LOG_AND_RETURN("invalid %s operation", key
);
1223 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1225 } else if (startswith(key
, "IMPORT")) {
1226 attr
= get_key_attribute(rules
->udev
,
1227 key
+ STRLEN("IMPORT"));
1229 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1232 if (op
== OP_REMOVE
)
1233 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1235 if (streq(attr
, "program")) {
1236 /* find known built-in command */
1237 if (value
[0] != '/') {
1238 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1240 if (cmd
< UDEV_BUILTIN_MAX
) {
1241 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1242 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1246 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1247 } else if (streq(attr
, "builtin")) {
1248 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1250 if (cmd
>= UDEV_BUILTIN_MAX
)
1251 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1253 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1254 } else if (streq(attr
, "file"))
1255 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1256 else if (streq(attr
, "db"))
1257 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1258 else if (streq(attr
, "cmdline"))
1259 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1260 else if (streq(attr
, "parent"))
1261 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1263 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1265 } else if (startswith(key
, "TEST")) {
1268 if (op
> OP_MATCH_MAX
)
1269 LOG_AND_RETURN("invalid %s operation", "TEST");
1271 attr
= get_key_attribute(rules
->udev
,
1272 key
+ STRLEN("TEST"));
1274 mode
= strtol(attr
, NULL
, 8);
1275 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1277 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1279 } else if (startswith(key
, "RUN")) {
1280 attr
= get_key_attribute(rules
->udev
,
1281 key
+ STRLEN("RUN"));
1284 if (op
== OP_REMOVE
)
1285 LOG_AND_RETURN("invalid %s operation", "RUN");
1287 if (streq(attr
, "builtin")) {
1288 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1290 if (cmd
< UDEV_BUILTIN_MAX
)
1291 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1293 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1294 } else if (streq(attr
, "program")) {
1295 const enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1297 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1299 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr
);
1301 } else if (streq(key
, "LABEL")) {
1302 if (op
== OP_REMOVE
)
1303 LOG_AND_RETURN("invalid %s operation", key
);
1305 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1307 } else if (streq(key
, "GOTO")) {
1308 if (op
== OP_REMOVE
)
1309 LOG_AND_RETURN("invalid %s operation", key
);
1311 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1313 } else if (startswith(key
, "NAME")) {
1314 if (op
== OP_REMOVE
)
1315 LOG_AND_RETURN("invalid %s operation", key
);
1317 if (op
< OP_MATCH_MAX
)
1318 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1320 if (streq(value
, "%k")) {
1321 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1324 if (isempty(value
)) {
1325 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1328 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1330 rule_tmp
.rule
.rule
.can_set_name
= true;
1332 } else if (streq(key
, "SYMLINK")) {
1333 if (op
== OP_REMOVE
)
1334 LOG_AND_RETURN("invalid %s operation", key
);
1336 if (op
< OP_MATCH_MAX
)
1337 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1339 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1340 rule_tmp
.rule
.rule
.can_set_name
= true;
1342 } else if (streq(key
, "OWNER")) {
1346 if (op
== OP_REMOVE
)
1347 LOG_AND_RETURN("invalid %s operation", key
);
1349 uid
= strtoul(value
, &endptr
, 10);
1350 if (endptr
[0] == '\0')
1351 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1352 else if (rules
->resolve_names
> 0 && strchr("$%", value
[0]) == NULL
) {
1353 uid
= add_uid(rules
, value
);
1354 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1355 } else if (rules
->resolve_names
>= 0)
1356 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1358 rule_tmp
.rule
.rule
.can_set_name
= true;
1360 } else if (streq(key
, "GROUP")) {
1364 if (op
== OP_REMOVE
)
1365 LOG_AND_RETURN("invalid %s operation", key
);
1367 gid
= strtoul(value
, &endptr
, 10);
1368 if (endptr
[0] == '\0')
1369 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1370 else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1371 gid
= add_gid(rules
, value
);
1372 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1373 } else if (rules
->resolve_names
>= 0)
1374 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1376 rule_tmp
.rule
.rule
.can_set_name
= true;
1378 } else if (streq(key
, "MODE")) {
1382 if (op
== OP_REMOVE
)
1383 LOG_AND_RETURN("invalid %s operation", key
);
1385 mode
= strtol(value
, &endptr
, 8);
1386 if (endptr
[0] == '\0')
1387 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1389 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1390 rule_tmp
.rule
.rule
.can_set_name
= true;
1392 } else if (streq(key
, "OPTIONS")) {
1395 if (op
== OP_REMOVE
)
1396 LOG_AND_RETURN("invalid %s operation", key
);
1398 pos
= strstr(value
, "link_priority=");
1400 int prio
= atoi(pos
+ STRLEN("link_priority="));
1402 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1405 pos
= strstr(value
, "string_escape=");
1407 pos
+= STRLEN("string_escape=");
1408 if (startswith(pos
, "none"))
1409 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1410 else if (startswith(pos
, "replace"))
1411 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1414 pos
= strstr(value
, "db_persist");
1416 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1418 pos
= strstr(value
, "nowatch");
1422 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1424 pos
= strstr(value
, "watch");
1428 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1432 pos
= strstr(value
, "static_node=");
1434 pos
+= STRLEN("static_node=");
1435 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1436 rule_tmp
.rule
.rule
.has_static_node
= true;
1440 LOG_AND_RETURN("unknown key '%s'", key
);
1443 /* add rule token and sort tokens */
1444 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1445 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1446 LOG_RULE_ERROR("failed to add rule token");
1449 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1450 _cleanup_fclose_
FILE *f
= NULL
;
1451 unsigned int first_token
;
1452 unsigned int filename_off
;
1453 char line
[UTIL_LINE_SIZE
];
1457 f
= fopen(filename
, "re");
1459 if (errno
== ENOENT
)
1465 if (null_or_empty_fd(fileno(f
))) {
1466 log_debug("Skipping empty file: %s", filename
);
1469 log_debug("Reading rules file: %s", filename
);
1471 first_token
= rules
->token_cur
;
1472 filename_off
= rules_add_string(rules
, filename
);
1474 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1478 /* skip whitespace */
1481 while (isspace(key
[0]))
1492 /* continue reading if backslash+newline is found */
1493 while (line
[len
-2] == '\\') {
1494 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1496 if (strlen(&line
[len
-2]) < 2)
1502 if (len
+1 >= sizeof(line
)) {
1503 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1506 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1509 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1510 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1511 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1512 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1515 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1516 if (rules
->tokens
[j
].type
!= TK_RULE
)
1518 if (rules
->tokens
[j
].rule
.label_off
== 0)
1520 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1522 rules
->tokens
[i
].key
.rule_goto
= j
;
1525 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1526 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1532 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1533 struct udev_rules
*rules
;
1534 struct udev_list file_list
;
1535 struct token end_token
;
1539 rules
= new0(struct udev_rules
, 1);
1543 rules
->resolve_names
= resolve_names
;
1544 udev_list_init(udev
, &file_list
, true);
1546 /* init token array and string buffer */
1547 rules
->tokens
= malloc_multiply(PREALLOC_TOKEN
, sizeof(struct token
));
1548 if (rules
->tokens
== NULL
)
1549 return udev_rules_unref(rules
);
1550 rules
->token_max
= PREALLOC_TOKEN
;
1552 rules
->strbuf
= strbuf_new();
1554 return udev_rules_unref(rules
);
1556 udev_rules_check_timestamp(rules
);
1558 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1560 log_error_errno(r
, "failed to enumerate rules files: %m");
1561 return udev_rules_unref(rules
);
1565 * The offset value in the rules strct is limited; add all
1566 * rules file names to the beginning of the string buffer.
1568 STRV_FOREACH(f
, files
)
1569 rules_add_string(rules
, *f
);
1571 STRV_FOREACH(f
, files
)
1572 parse_file(rules
, *f
);
1576 memzero(&end_token
, sizeof(struct token
));
1577 end_token
.type
= TK_END
;
1578 add_token(rules
, &end_token
);
1579 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1580 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1582 /* cleanup temporary strbuf data */
1583 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1584 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1585 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1586 strbuf_complete(rules
->strbuf
);
1588 /* cleanup uid/gid cache */
1589 rules
->uids
= mfree(rules
->uids
);
1590 rules
->uids_cur
= 0;
1591 rules
->uids_max
= 0;
1592 rules
->gids
= mfree(rules
->gids
);
1593 rules
->gids_cur
= 0;
1594 rules
->gids_max
= 0;
1600 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1603 free(rules
->tokens
);
1604 strbuf_cleanup(rules
->strbuf
);
1607 return mfree(rules
);
1610 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1614 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1617 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1618 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1625 switch (token
->key
.glob
) {
1627 match
= (streq(key_value
, val
));
1630 match
= (fnmatch(key_value
, val
, 0) == 0);
1637 s
= rules_str(rules
, token
->key
.value_off
);
1642 next
= strchr(s
, '|');
1644 size_t matchlen
= (size_t)(next
- s
);
1646 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1650 match
= (streq(s
, val
));
1659 char value
[UTIL_PATH_SIZE
];
1661 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1663 while (key_value
!= NULL
) {
1664 pos
= strchr(key_value
, '|');
1669 match
= (fnmatch(key_value
, val
, 0) == 0);
1677 match
= (val
[0] != '\0');
1683 if (match
&& (token
->key
.op
== OP_MATCH
))
1685 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1690 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1692 char nbuf
[UTIL_NAME_SIZE
];
1694 char vbuf
[UTIL_NAME_SIZE
];
1697 name
= rules_str(rules
, cur
->key
.attr_off
);
1698 switch (cur
->key
.attrsubst
) {
1700 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1704 value
= udev_device_get_sysattr_value(dev
, name
);
1709 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1717 /* remove trailing whitespace, if not asked to match for it */
1718 len
= strlen(value
);
1719 if (len
> 0 && isspace(value
[len
-1])) {
1720 const char *key_value
;
1723 key_value
= rules_str(rules
, cur
->key
.value_off
);
1724 klen
= strlen(key_value
);
1725 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1726 if (value
!= vbuf
) {
1727 strscpy(vbuf
, sizeof(vbuf
), value
);
1730 while (len
> 0 && isspace(vbuf
[--len
]))
1735 return match_key(rules
, cur
, value
);
1744 void udev_rules_apply_to_event(struct udev_rules
*rules
,
1745 struct udev_event
*event
,
1746 usec_t timeout_usec
,
1747 usec_t timeout_warn_usec
,
1748 struct udev_list
*properties_list
) {
1751 enum escape_type esc
= ESCAPE_UNSET
;
1755 if (rules
->tokens
== NULL
)
1758 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1759 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1760 udev_device_get_ifindex(event
->dev
) > 0));
1762 /* loop through token list, match, run actions or forward to next rule */
1763 cur
= &rules
->tokens
[0];
1766 dump_token(rules
, cur
);
1767 switch (cur
->type
) {
1771 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1772 if (!can_set_name
&& rule
->rule
.can_set_name
)
1777 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1781 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1785 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1788 case TK_M_DEVLINK
: {
1789 struct udev_list_entry
*list_entry
;
1792 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1793 const char *devlink
;
1795 devlink
= udev_list_entry_get_name(list_entry
) + STRLEN("/dev/");
1796 if (match_key(rules
, cur
, devlink
) == 0) {
1806 if (match_key(rules
, cur
, event
->name
) != 0)
1810 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1813 value
= udev_device_get_property_value(event
->dev
, key_name
);
1815 /* check global properties */
1816 if (!value
&& properties_list
) {
1817 struct udev_list_entry
*list_entry
;
1819 list_entry
= udev_list_get_entry(properties_list
);
1820 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1821 if (list_entry
!= NULL
)
1822 value
= udev_list_entry_get_value(list_entry
);
1827 if (match_key(rules
, cur
, value
))
1832 struct udev_list_entry
*list_entry
;
1835 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1836 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1841 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1842 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1846 case TK_M_SUBSYSTEM
:
1847 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1851 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1855 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1859 char filename
[UTIL_PATH_SIZE
];
1860 _cleanup_free_
char *value
= NULL
;
1863 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1864 sysctl_normalize(filename
);
1865 if (sysctl_read(filename
, &value
) < 0)
1868 len
= strlen(value
);
1869 while (len
> 0 && isspace(value
[--len
]))
1871 if (match_key(rules
, cur
, value
) != 0)
1876 case TK_M_SUBSYSTEMS
:
1882 /* get whole sequence of parent matches */
1884 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1887 /* loop over parents */
1888 event
->dev_parent
= event
->dev
;
1892 /* loop over sequence of parent match keys */
1893 for (key
= cur
; key
< next
; key
++ ) {
1894 dump_token(rules
, key
);
1897 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1900 case TK_M_SUBSYSTEMS
:
1901 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1905 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1909 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1913 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1915 if (match
&& key
->key
.op
== OP_NOMATCH
)
1917 if (!match
&& key
->key
.op
== OP_MATCH
)
1928 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1929 if (event
->dev_parent
== NULL
)
1932 /* move behind our sequence of parent match keys */
1937 char filename
[UTIL_PATH_SIZE
];
1938 struct stat statbuf
;
1941 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1942 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
1943 if (filename
[0] != '/') {
1944 char tmp
[UTIL_PATH_SIZE
];
1946 strscpy(tmp
, sizeof(tmp
), filename
);
1947 strscpyl(filename
, sizeof(filename
),
1948 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
1951 attr_subst_subdir(filename
, sizeof(filename
));
1953 match
= (stat(filename
, &statbuf
) == 0);
1954 if (match
&& cur
->key
.mode
> 0)
1955 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1956 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1958 if (!match
&& cur
->key
.op
== OP_MATCH
)
1962 case TK_M_PROGRAM
: {
1963 char program
[UTIL_PATH_SIZE
];
1964 char result
[UTIL_LINE_SIZE
];
1966 event
->program_result
= mfree(event
->program_result
);
1967 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1968 log_debug("PROGRAM '%s' %s:%u",
1970 rules_str(rules
, rule
->rule
.filename_off
),
1971 rule
->rule
.filename_line
);
1973 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
1974 if (cur
->key
.op
!= OP_NOMATCH
)
1979 delete_trailing_chars(result
, "\n");
1980 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1981 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1983 log_debug("%i character(s) replaced" , count
);
1985 event
->program_result
= strdup(result
);
1986 if (cur
->key
.op
== OP_NOMATCH
)
1991 case TK_M_IMPORT_FILE
: {
1992 char import
[UTIL_PATH_SIZE
];
1994 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1995 if (import_file_into_properties(event
->dev
, import
) != 0)
1996 if (cur
->key
.op
!= OP_NOMATCH
)
2000 case TK_M_IMPORT_PROG
: {
2001 char import
[UTIL_PATH_SIZE
];
2003 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2004 log_debug("IMPORT '%s' %s:%u",
2006 rules_str(rules
, rule
->rule
.filename_off
),
2007 rule
->rule
.filename_line
);
2009 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
2010 if (cur
->key
.op
!= OP_NOMATCH
)
2014 case TK_M_IMPORT_BUILTIN
: {
2015 char command
[UTIL_PATH_SIZE
];
2017 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2018 /* check if we ran already */
2019 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2020 log_debug("IMPORT builtin skip '%s' %s:%u",
2021 udev_builtin_name(cur
->key
.builtin_cmd
),
2022 rules_str(rules
, rule
->rule
.filename_off
),
2023 rule
->rule
.filename_line
);
2024 /* return the result from earlier run */
2025 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2026 if (cur
->key
.op
!= OP_NOMATCH
)
2031 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2034 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2035 log_debug("IMPORT builtin '%s' %s:%u",
2036 udev_builtin_name(cur
->key
.builtin_cmd
),
2037 rules_str(rules
, rule
->rule
.filename_off
),
2038 rule
->rule
.filename_line
);
2040 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2041 /* remember failure */
2042 log_debug("IMPORT builtin '%s' returned non-zero",
2043 udev_builtin_name(cur
->key
.builtin_cmd
));
2044 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2045 if (cur
->key
.op
!= OP_NOMATCH
)
2050 case TK_M_IMPORT_DB
: {
2051 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2054 value
= udev_device_get_property_value(event
->dev_db
, key
);
2056 udev_device_add_property(event
->dev
, key
, value
);
2058 if (cur
->key
.op
!= OP_NOMATCH
)
2063 case TK_M_IMPORT_CMDLINE
: {
2064 _cleanup_free_
char *value
= NULL
;
2065 bool imported
= false;
2068 key
= rules_str(rules
, cur
->key
.value_off
);
2070 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2072 log_debug_errno(r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2077 udev_device_add_property(event
->dev
, key
, value
);
2079 /* we import simple flags as 'FLAG=1' */
2080 udev_device_add_property(event
->dev
, key
, "1");
2083 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2087 case TK_M_IMPORT_PARENT
: {
2088 char import
[UTIL_PATH_SIZE
];
2090 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2091 if (import_parent_into_properties(event
->dev
, import
) != 0)
2092 if (cur
->key
.op
!= OP_NOMATCH
)
2097 if (match_key(rules
, cur
, event
->program_result
) != 0)
2100 case TK_A_STRING_ESCAPE_NONE
:
2103 case TK_A_STRING_ESCAPE_REPLACE
:
2104 esc
= ESCAPE_REPLACE
;
2106 case TK_A_DB_PERSIST
:
2107 udev_device_set_db_persist(event
->dev
);
2109 case TK_A_INOTIFY_WATCH
:
2110 if (event
->inotify_watch_final
)
2112 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2113 event
->inotify_watch_final
= true;
2114 event
->inotify_watch
= cur
->key
.watch
;
2116 case TK_A_DEVLINK_PRIO
:
2117 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2120 char owner
[UTIL_NAME_SIZE
];
2121 const char *ow
= owner
;
2123 if (event
->owner_final
)
2125 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2126 event
->owner_final
= true;
2127 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2128 event
->owner_set
= true;
2129 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2131 log_unknown_owner(r
, "user", owner
);
2134 log_debug("OWNER %u %s:%u",
2136 rules_str(rules
, rule
->rule
.filename_off
),
2137 rule
->rule
.filename_line
);
2141 char group
[UTIL_NAME_SIZE
];
2142 const char *gr
= group
;
2144 if (event
->group_final
)
2146 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2147 event
->group_final
= true;
2148 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2149 event
->group_set
= true;
2150 r
= get_group_creds(&gr
, &event
->gid
);
2152 log_unknown_owner(r
, "group", group
);
2155 log_debug("GROUP %u %s:%u",
2157 rules_str(rules
, rule
->rule
.filename_off
),
2158 rule
->rule
.filename_line
);
2162 char mode_str
[UTIL_NAME_SIZE
];
2166 if (event
->mode_final
)
2168 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2169 mode
= strtol(mode_str
, &endptr
, 8);
2170 if (endptr
[0] != '\0') {
2171 log_error("ignoring invalid mode '%s'", mode_str
);
2174 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2175 event
->mode_final
= true;
2176 event
->mode_set
= true;
2178 log_debug("MODE %#o %s:%u",
2180 rules_str(rules
, rule
->rule
.filename_off
),
2181 rule
->rule
.filename_line
);
2185 if (event
->owner_final
)
2187 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2188 event
->owner_final
= true;
2189 event
->owner_set
= true;
2190 event
->uid
= cur
->key
.uid
;
2191 log_debug("OWNER %u %s:%u",
2193 rules_str(rules
, rule
->rule
.filename_off
),
2194 rule
->rule
.filename_line
);
2197 if (event
->group_final
)
2199 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2200 event
->group_final
= true;
2201 event
->group_set
= true;
2202 event
->gid
= cur
->key
.gid
;
2203 log_debug("GROUP %u %s:%u",
2205 rules_str(rules
, rule
->rule
.filename_off
),
2206 rule
->rule
.filename_line
);
2209 if (event
->mode_final
)
2211 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2212 event
->mode_final
= true;
2213 event
->mode_set
= true;
2214 event
->mode
= cur
->key
.mode
;
2215 log_debug("MODE %#o %s:%u",
2217 rules_str(rules
, rule
->rule
.filename_off
),
2218 rule
->rule
.filename_line
);
2220 case TK_A_SECLABEL
: {
2221 char label_str
[UTIL_LINE_SIZE
] = {};
2222 const char *name
, *label
;
2224 name
= rules_str(rules
, cur
->key
.attr_off
);
2225 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2226 if (label_str
[0] != '\0')
2229 label
= rules_str(rules
, cur
->key
.value_off
);
2231 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2232 udev_list_cleanup(&event
->seclabel_list
);
2233 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2234 log_debug("SECLABEL{%s}='%s' %s:%u",
2236 rules_str(rules
, rule
->rule
.filename_off
),
2237 rule
->rule
.filename_line
);
2241 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2242 char *value
= rules_str(rules
, cur
->key
.value_off
);
2243 char value_new
[UTIL_NAME_SIZE
];
2244 const char *value_old
= NULL
;
2246 if (value
[0] == '\0') {
2247 if (cur
->key
.op
== OP_ADD
)
2249 udev_device_add_property(event
->dev
, name
, NULL
);
2253 if (cur
->key
.op
== OP_ADD
)
2254 value_old
= udev_device_get_property_value(event
->dev
, name
);
2256 char temp
[UTIL_NAME_SIZE
];
2258 /* append value separated by space */
2259 udev_event_apply_format(event
, value
, temp
, sizeof(temp
), false);
2260 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2262 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
), false);
2264 udev_device_add_property(event
->dev
, name
, value_new
);
2268 char tag
[UTIL_PATH_SIZE
];
2271 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2272 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2273 udev_device_cleanup_tags_list(event
->dev
);
2274 for (p
= tag
; *p
!= '\0'; p
++) {
2275 if ((*p
>= 'a' && *p
<= 'z') ||
2276 (*p
>= 'A' && *p
<= 'Z') ||
2277 (*p
>= '0' && *p
<= '9') ||
2278 IN_SET(*p
, '-', '_'))
2280 log_error("ignoring invalid tag name '%s'", tag
);
2283 if (cur
->key
.op
== OP_REMOVE
)
2284 udev_device_remove_tag(event
->dev
, tag
);
2286 udev_device_add_tag(event
->dev
, tag
);
2290 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2292 char name_str
[UTIL_PATH_SIZE
];
2295 if (event
->name_final
)
2297 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2298 event
->name_final
= true;
2299 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2300 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2301 count
= util_replace_chars(name_str
, "/");
2303 log_debug("%i character(s) replaced", count
);
2305 if (major(udev_device_get_devnum(event
->dev
)) &&
2306 !streq(name_str
, udev_device_get_devnode(event
->dev
) + STRLEN("/dev/"))) {
2307 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2309 rules_str(rules
, rule
->rule
.filename_off
),
2310 rule
->rule
.filename_line
);
2313 if (free_and_strdup(&event
->name
, name_str
) < 0) {
2317 log_debug("NAME '%s' %s:%u",
2319 rules_str(rules
, rule
->rule
.filename_off
),
2320 rule
->rule
.filename_line
);
2323 case TK_A_DEVLINK
: {
2324 char temp
[UTIL_PATH_SIZE
];
2325 char filename
[UTIL_PATH_SIZE
];
2329 if (event
->devlink_final
)
2331 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2333 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2334 event
->devlink_final
= true;
2335 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2336 udev_device_cleanup_devlinks_list(event
->dev
);
2338 /* allow multiple symlinks separated by spaces */
2339 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2340 if (esc
== ESCAPE_UNSET
)
2341 count
= util_replace_chars(temp
, "/ ");
2342 else if (esc
== ESCAPE_REPLACE
)
2343 count
= util_replace_chars(temp
, "/");
2345 log_debug("%i character(s) replaced" , count
);
2347 while (isspace(pos
[0]))
2349 next
= strchr(pos
, ' ');
2350 while (next
!= NULL
) {
2352 log_debug("LINK '%s' %s:%u", pos
,
2353 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2354 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2355 udev_device_add_devlink(event
->dev
, filename
);
2356 while (isspace(next
[1]))
2359 next
= strchr(pos
, ' ');
2361 if (pos
[0] != '\0') {
2362 log_debug("LINK '%s' %s:%u", pos
,
2363 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2364 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2365 udev_device_add_devlink(event
->dev
, filename
);
2370 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2371 char attr
[UTIL_PATH_SIZE
];
2372 char value
[UTIL_NAME_SIZE
];
2373 _cleanup_fclose_
FILE *f
= NULL
;
2375 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2376 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2377 attr_subst_subdir(attr
, sizeof(attr
));
2379 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2380 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2381 rules_str(rules
, rule
->rule
.filename_off
),
2382 rule
->rule
.filename_line
);
2383 f
= fopen(attr
, "we");
2385 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2386 else if (fprintf(f
, "%s", value
) <= 0)
2387 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2391 char filename
[UTIL_PATH_SIZE
];
2392 char value
[UTIL_NAME_SIZE
];
2394 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2395 sysctl_normalize(filename
);
2396 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2397 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2398 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2399 r
= sysctl_write(filename
, value
);
2401 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2404 case TK_A_RUN_BUILTIN
:
2405 case TK_A_RUN_PROGRAM
: {
2406 struct udev_list_entry
*entry
;
2408 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2409 udev_list_cleanup(&event
->run_list
);
2410 log_debug("RUN '%s' %s:%u",
2411 rules_str(rules
, cur
->key
.value_off
),
2412 rules_str(rules
, rule
->rule
.filename_off
),
2413 rule
->rule
.filename_line
);
2414 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2415 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2419 if (cur
->key
.rule_goto
== 0)
2421 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2426 case TK_M_PARENTS_MIN
:
2427 case TK_M_PARENTS_MAX
:
2430 log_error("wrong type %u", cur
->type
);
2437 /* fast-forward to next rule */
2438 cur
= rule
+ rule
->rule
.token_count
;
2442 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2448 _cleanup_strv_free_
char **tags
= NULL
;
2451 _cleanup_free_
char *path
= NULL
;
2454 if (rules
->tokens
== NULL
)
2457 cur
= &rules
->tokens
[0];
2460 switch (cur
->type
) {
2465 /* skip rules without a static_node tag */
2466 if (!rule
->rule
.has_static_node
)
2472 tags
= strv_free(tags
);
2481 mode
= cur
->key
.mode
;
2484 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2489 case TK_A_STATIC_NODE
: {
2490 char device_node
[UTIL_PATH_SIZE
];
2491 char tags_dir
[UTIL_PATH_SIZE
];
2492 char tag_symlink
[UTIL_PATH_SIZE
];
2495 /* we assure, that the permissions tokens are sorted before the static token */
2497 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2500 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2501 if (stat(device_node
, &stats
) != 0)
2503 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2506 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2508 STRV_FOREACH(t
, tags
) {
2509 _cleanup_free_
char *unescaped_filename
= NULL
;
2511 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2512 r
= mkdir_p(tags_dir
, 0755);
2514 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2516 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2518 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2519 r
= symlink(device_node
, tag_symlink
);
2520 if (r
< 0 && errno
!= EEXIST
)
2521 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2522 tag_symlink
, device_node
);
2526 /* don't touch the permissions if only the tags were set */
2527 if (mode
== 0 && uid
== 0 && gid
== 0)
2536 if (mode
!= (stats
.st_mode
& 01777)) {
2537 r
= chmod(device_node
, mode
);
2539 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2542 log_debug("chmod '%s' %#o", device_node
, mode
);
2545 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2546 r
= chown(device_node
, uid
, gid
);
2548 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2549 device_node
, uid
, gid
);
2551 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2554 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2564 /* fast-forward to next rule */
2565 cur
= rule
+ rule
->rule
.token_count
;
2572 fchmod(fileno(f
), 0644);
2573 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2574 unlink_noerrno("/run/udev/static_node-tags");
2575 unlink_noerrno(path
);