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"
31 #include "udev-builtin.h"
33 #include "user-util.h"
36 #define PREALLOC_TOKEN 2048
39 unsigned int name_off
;
46 static const char* const rules_dirs
[] = {
49 UDEVLIBEXECDIR
"/rules.d",
57 /* every key in the rules file becomes a token */
59 unsigned int token_cur
;
60 unsigned int token_max
;
62 /* all key strings are copied and de-duplicated in a single continuous string buffer */
63 struct strbuf
*strbuf
;
65 /* during rule parsing, uid/gid lookup results are cached */
67 unsigned int uids_cur
;
68 unsigned int uids_max
;
70 unsigned int gids_cur
;
71 unsigned int gids_max
;
74 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
75 return rules
->strbuf
->buf
+ off
;
78 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
79 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
82 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
96 enum string_glob_type
{
98 GL_PLAIN
, /* no special chars */
99 GL_GLOB
, /* shell globs ?,*,[] */
100 GL_SPLIT
, /* multi-value A|B */
101 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
102 GL_SOMETHING
, /* commonly used "?*" */
105 enum string_subst_type
{
112 /* tokens of a rule are sorted/handled in this order */
117 TK_M_ACTION
, /* val */
118 TK_M_DEVPATH
, /* val */
119 TK_M_KERNEL
, /* val */
120 TK_M_DEVLINK
, /* val */
122 TK_M_ENV
, /* val, attr */
124 TK_M_SUBSYSTEM
, /* val */
125 TK_M_DRIVER
, /* val */
126 TK_M_WAITFOR
, /* val */
127 TK_M_ATTR
, /* val, attr */
128 TK_M_SYSCTL
, /* val, attr */
131 TK_M_KERNELS
, /* val */
132 TK_M_SUBSYSTEMS
, /* val */
133 TK_M_DRIVERS
, /* val */
134 TK_M_ATTRS
, /* val, attr */
138 TK_M_TEST
, /* val, mode_t */
139 TK_M_PROGRAM
, /* val */
140 TK_M_IMPORT_FILE
, /* val */
141 TK_M_IMPORT_PROG
, /* val */
142 TK_M_IMPORT_BUILTIN
, /* val */
143 TK_M_IMPORT_DB
, /* val */
144 TK_M_IMPORT_CMDLINE
, /* val */
145 TK_M_IMPORT_PARENT
, /* val */
146 TK_M_RESULT
, /* val */
149 TK_A_STRING_ESCAPE_NONE
,
150 TK_A_STRING_ESCAPE_REPLACE
,
152 TK_A_INOTIFY_WATCH
, /* int */
153 TK_A_DEVLINK_PRIO
, /* int */
154 TK_A_OWNER
, /* val */
155 TK_A_GROUP
, /* val */
157 TK_A_OWNER_ID
, /* uid_t */
158 TK_A_GROUP_ID
, /* gid_t */
159 TK_A_MODE_ID
, /* mode_t */
161 TK_A_STATIC_NODE
, /* val */
162 TK_A_SECLABEL
, /* val, attr */
163 TK_A_ENV
, /* val, attr */
165 TK_A_DEVLINK
, /* val */
166 TK_A_ATTR
, /* val, attr */
167 TK_A_SYSCTL
, /* val, attr */
168 TK_A_RUN_BUILTIN
, /* val, bool */
169 TK_A_RUN_PROGRAM
, /* val, bool */
170 TK_A_GOTO
, /* size_t */
175 /* we try to pack stuff in a way that we take only 12 bytes per token */
178 unsigned char type
; /* same in rule and key */
180 enum token_type type
:8;
182 bool has_static_node
:1;
183 unsigned int unused
:6;
184 unsigned short token_count
;
185 unsigned int label_off
;
186 unsigned short filename_off
;
187 unsigned short filename_line
;
190 enum token_type type
:8;
191 enum operation_type op
:8;
192 enum string_glob_type glob
:8;
193 enum string_subst_type subst
:4;
194 enum string_subst_type attrsubst
:4;
195 unsigned int value_off
;
197 unsigned int attr_off
;
198 unsigned int rule_goto
;
204 enum udev_builtin_cmd builtin_cmd
;
212 struct udev_rules
*rules
;
214 struct token token
[MAX_TK
];
215 unsigned int token_cur
;
219 static const char *operation_str(enum operation_type type
) {
220 static const char *operation_strs
[] = {
221 [OP_UNSET
] = "UNSET",
222 [OP_MATCH
] = "match",
223 [OP_NOMATCH
] = "nomatch",
224 [OP_MATCH_MAX
] = "MATCH_MAX",
227 [OP_REMOVE
] = "remove",
228 [OP_ASSIGN
] = "assign",
229 [OP_ASSIGN_FINAL
] = "assign-final",
232 return operation_strs
[type
];
235 static const char *string_glob_str(enum string_glob_type type
) {
236 static const char *string_glob_strs
[] = {
237 [GL_UNSET
] = "UNSET",
238 [GL_PLAIN
] = "plain",
240 [GL_SPLIT
] = "split",
241 [GL_SPLIT_GLOB
] = "split-glob",
242 [GL_SOMETHING
] = "split-glob",
245 return string_glob_strs
[type
];
248 static const char *token_str(enum token_type type
) {
249 static const char *token_strs
[] = {
250 [TK_UNSET
] = "UNSET",
253 [TK_M_ACTION
] = "M ACTION",
254 [TK_M_DEVPATH
] = "M DEVPATH",
255 [TK_M_KERNEL
] = "M KERNEL",
256 [TK_M_DEVLINK
] = "M DEVLINK",
257 [TK_M_NAME
] = "M NAME",
258 [TK_M_ENV
] = "M ENV",
259 [TK_M_TAG
] = "M TAG",
260 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
261 [TK_M_DRIVER
] = "M DRIVER",
262 [TK_M_WAITFOR
] = "M WAITFOR",
263 [TK_M_ATTR
] = "M ATTR",
264 [TK_M_SYSCTL
] = "M SYSCTL",
266 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
267 [TK_M_KERNELS
] = "M KERNELS",
268 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
269 [TK_M_DRIVERS
] = "M DRIVERS",
270 [TK_M_ATTRS
] = "M ATTRS",
271 [TK_M_TAGS
] = "M TAGS",
272 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
274 [TK_M_TEST
] = "M TEST",
275 [TK_M_PROGRAM
] = "M PROGRAM",
276 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
277 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
278 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
279 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
280 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
281 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
282 [TK_M_RESULT
] = "M RESULT",
283 [TK_M_MAX
] = "M MAX",
285 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
286 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
287 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
288 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
289 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
290 [TK_A_OWNER
] = "A OWNER",
291 [TK_A_GROUP
] = "A GROUP",
292 [TK_A_MODE
] = "A MODE",
293 [TK_A_OWNER_ID
] = "A OWNER_ID",
294 [TK_A_GROUP_ID
] = "A GROUP_ID",
295 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
296 [TK_A_SECLABEL
] = "A SECLABEL",
297 [TK_A_MODE_ID
] = "A MODE_ID",
298 [TK_A_ENV
] = "A ENV",
299 [TK_A_TAG
] = "A ENV",
300 [TK_A_NAME
] = "A NAME",
301 [TK_A_DEVLINK
] = "A DEVLINK",
302 [TK_A_ATTR
] = "A ATTR",
303 [TK_A_SYSCTL
] = "A SYSCTL",
304 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
305 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
306 [TK_A_GOTO
] = "A GOTO",
311 return token_strs
[type
];
314 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
315 enum token_type type
= token
->type
;
316 enum operation_type op
= token
->key
.op
;
317 enum string_glob_type glob
= token
->key
.glob
;
318 const char *value
= rules_str(rules
, token
->key
.value_off
);
319 const char *attr
= &rules
->strbuf
->buf
[token
->key
.attr_off
];
324 const char *tks_ptr
= (char *)rules
->tokens
;
325 const char *tk_ptr
= (char *)token
;
326 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
328 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
329 &rules
->strbuf
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
330 idx
, token
->rule
.token_count
,
331 &rules
->strbuf
->buf
[token
->rule
.label_off
]);
343 case TK_M_SUBSYSTEMS
:
347 case TK_M_IMPORT_FILE
:
348 case TK_M_IMPORT_PROG
:
350 case TK_M_IMPORT_CMDLINE
:
351 case TK_M_IMPORT_PARENT
:
358 case TK_A_RUN_BUILTIN
:
359 case TK_A_RUN_PROGRAM
:
360 log_debug("%s %s '%s'(%s)",
361 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
363 case TK_M_IMPORT_BUILTIN
:
364 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
373 log_debug("%s %s '%s' '%s'(%s)",
374 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
378 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
380 case TK_A_STRING_ESCAPE_NONE
:
381 case TK_A_STRING_ESCAPE_REPLACE
:
382 case TK_A_DB_PERSIST
:
383 log_debug("%s", token_str(type
));
386 log_debug("%s %s '%s'(%s) %#o",
387 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
389 case TK_A_INOTIFY_WATCH
:
390 log_debug("%s %u", token_str(type
), token
->key
.watch
);
392 case TK_A_DEVLINK_PRIO
:
393 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
396 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
399 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
402 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
404 case TK_A_STATIC_NODE
:
405 log_debug("%s '%s'", token_str(type
), value
);
408 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
411 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
414 log_debug("* %s", token_str(type
));
416 case TK_M_PARENTS_MIN
:
417 case TK_M_PARENTS_MAX
:
420 log_debug("unknown type %u", type
);
425 static void dump_rules(struct udev_rules
*rules
) {
428 log_debug("dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings",
430 rules
->token_cur
* sizeof(struct token
),
431 rules
->strbuf
->nodes_count
,
433 for (i
= 0; i
< rules
->token_cur
; i
++)
434 dump_token(rules
, &rules
->tokens
[i
]);
437 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
438 static inline void dump_rules(struct udev_rules
*rules
) {}
441 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
442 /* grow buffer if needed */
443 if (rules
->token_cur
+1 >= rules
->token_max
) {
444 struct token
*tokens
;
447 /* double the buffer size */
448 add
= rules
->token_max
;
452 tokens
= reallocarray(rules
->tokens
, rules
->token_max
+ add
, sizeof(struct token
));
455 rules
->tokens
= tokens
;
456 rules
->token_max
+= add
;
458 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
463 static void log_unknown_owner(int error
, const char *entity
, const char *owner
) {
464 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
465 log_error("Specified %s '%s' unknown", entity
, owner
);
467 log_error_errno(error
, "Error resolving %s '%s': %m", entity
, owner
);
470 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
476 /* lookup, if we know it already */
477 for (i
= 0; i
< rules
->uids_cur
; i
++) {
478 off
= rules
->uids
[i
].name_off
;
479 if (streq(rules_str(rules
, off
), owner
)) {
480 uid
= rules
->uids
[i
].uid
;
484 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
486 log_unknown_owner(r
, "user", owner
);
488 /* grow buffer if needed */
489 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
490 struct uid_gid
*uids
;
493 /* double the buffer size */
494 add
= rules
->uids_max
;
498 uids
= reallocarray(rules
->uids
, rules
->uids_max
+ add
, sizeof(struct uid_gid
));
502 rules
->uids_max
+= add
;
504 rules
->uids
[rules
->uids_cur
].uid
= uid
;
505 off
= rules_add_string(rules
, owner
);
508 rules
->uids
[rules
->uids_cur
].name_off
= off
;
513 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
519 /* lookup, if we know it already */
520 for (i
= 0; i
< rules
->gids_cur
; i
++) {
521 off
= rules
->gids
[i
].name_off
;
522 if (streq(rules_str(rules
, off
), group
)) {
523 gid
= rules
->gids
[i
].gid
;
527 r
= get_group_creds(&group
, &gid
, USER_CREDS_ALLOW_MISSING
);
529 log_unknown_owner(r
, "group", group
);
531 /* grow buffer if needed */
532 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
533 struct uid_gid
*gids
;
536 /* double the buffer size */
537 add
= rules
->gids_max
;
541 gids
= reallocarray(rules
->gids
, rules
->gids_max
+ add
, sizeof(struct uid_gid
));
545 rules
->gids_max
+= add
;
547 rules
->gids
[rules
->gids_cur
].gid
= gid
;
548 off
= rules_add_string(rules
, group
);
551 rules
->gids
[rules
->gids_cur
].name_off
= off
;
556 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
563 while (isspace(key
[0]))
566 /* comment or empty line */
567 if (IN_SET(key
[0], '#', '\0'))
570 /* split key/value */
571 val
= strchr(key
, '=');
578 while (isspace(val
[0]))
585 while (isspace(key
[len
-1]))
589 /* terminate value */
593 while (isspace(val
[len
-1]))
601 if (IN_SET(val
[0], '"', '\'')) {
602 if (len
== 1 || val
[len
-1] != val
[0]) {
603 log_debug("inconsistent quoting: '%s', skip", line
);
610 udev_device_add_property(dev
, key
, val
);
615 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
617 char line
[UTIL_LINE_SIZE
];
619 f
= fopen(filename
, "re");
622 while (fgets(line
, sizeof(line
), f
) != NULL
)
623 import_property_from_string(dev
, line
);
628 static int import_program_into_properties(struct udev_event
*event
,
630 usec_t timeout_warn_usec
,
631 const char *program
) {
632 char result
[UTIL_LINE_SIZE
];
636 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
641 while (line
!= NULL
) {
644 pos
= strchr(line
, '\n');
649 import_property_from_string(event
->dev
, line
);
655 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
656 struct udev_device
*dev_parent
;
657 struct udev_list_entry
*list_entry
;
662 dev_parent
= udev_device_get_parent(dev
);
663 if (dev_parent
== NULL
)
666 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
667 const char *key
= udev_list_entry_get_name(list_entry
);
668 const char *val
= udev_list_entry_get_value(list_entry
);
670 if (fnmatch(filter
, key
, 0) == 0)
671 udev_device_add_property(dev
, key
, val
);
676 static void attr_subst_subdir(char *attr
, size_t len
) {
677 const char *pos
, *tail
, *path
;
678 _cleanup_closedir_
DIR *dir
= NULL
;
681 pos
= strstr(attr
, "/*/");
686 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
691 FOREACH_DIRENT_ALL(dent
, dir
, break)
692 if (dent
->d_name
[0] != '.') {
693 char n
[strlen(dent
->d_name
) + strlen(tail
) + 1];
695 strscpyl(n
, sizeof n
, dent
->d_name
, tail
, NULL
);
696 if (faccessat(dirfd(dir
), n
, F_OK
, 0) == 0) {
697 strscpyl(attr
, len
, path
, n
, NULL
);
703 static int get_key(char **line
, char **key
, enum operation_type
*op
, char **value
) {
709 if (linepos
== NULL
|| linepos
[0] == '\0')
712 /* skip whitespace */
713 while (isspace(linepos
[0]) || linepos
[0] == ',')
717 if (linepos
[0] == '\0')
723 if (linepos
[0] == '\0')
725 if (isspace(linepos
[0]))
727 if (linepos
[0] == '=')
729 if (IN_SET(linepos
[0], '+', '-', '!', ':'))
730 if (linepos
[1] == '=')
734 /* remember end of key */
737 /* skip whitespace after key */
738 while (isspace(linepos
[0]))
740 if (linepos
[0] == '\0')
743 /* get operation type */
744 if (linepos
[0] == '=' && linepos
[1] == '=') {
747 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
750 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
753 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
756 } else if (linepos
[0] == '=') {
759 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
760 *op
= OP_ASSIGN_FINAL
;
768 /* skip whitespace after operator */
769 while (isspace(linepos
[0]))
771 if (linepos
[0] == '\0')
775 if (linepos
[0] == '"')
782 for (i
= 0, j
= 0; ; i
++, j
++) {
784 if (linepos
[i
] == '"')
787 if (linepos
[i
] == '\0')
790 /* double quotes can be escaped */
791 if (linepos
[i
] == '\\')
792 if (linepos
[i
+1] == '"')
795 linepos
[j
] = linepos
[i
];
799 /* move line to next key */
800 *line
= linepos
+ i
+ 1;
804 /* extract possible KEY{attr} */
805 static const char *get_key_attribute(char *str
) {
809 attr
= strchr(str
, '{');
812 pos
= strchr(attr
, '}');
814 log_error("Missing closing brace for format");
823 static void rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
824 enum operation_type op
,
825 const char *value
, const void *data
) {
826 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
827 const char *attr
= NULL
;
829 assert(rule_tmp
->token_cur
< ELEMENTSOF(rule_tmp
->token
));
830 memzero(token
, sizeof(struct token
));
842 case TK_M_SUBSYSTEMS
:
846 case TK_M_IMPORT_FILE
:
847 case TK_M_IMPORT_PROG
:
849 case TK_M_IMPORT_CMDLINE
:
850 case TK_M_IMPORT_PARENT
:
860 case TK_A_STATIC_NODE
:
861 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
863 case TK_M_IMPORT_BUILTIN
:
864 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
865 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
876 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
877 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
880 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
882 token
->key
.mode
= *(mode_t
*)data
;
884 case TK_A_STRING_ESCAPE_NONE
:
885 case TK_A_STRING_ESCAPE_REPLACE
:
886 case TK_A_DB_PERSIST
:
888 case TK_A_RUN_BUILTIN
:
889 case TK_A_RUN_PROGRAM
:
890 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
891 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
893 case TK_A_INOTIFY_WATCH
:
894 case TK_A_DEVLINK_PRIO
:
895 token
->key
.devlink_prio
= *(int *)data
;
898 token
->key
.uid
= *(uid_t
*)data
;
901 token
->key
.gid
= *(gid_t
*)data
;
904 token
->key
.mode
= *(mode_t
*)data
;
907 case TK_M_PARENTS_MIN
:
908 case TK_M_PARENTS_MAX
:
912 assert_not_reached("wrong type");
915 if (value
!= NULL
&& type
< TK_M_MAX
) {
916 /* check if we need to split or call fnmatch() while matching rules */
917 enum string_glob_type glob
;
921 has_split
= (strchr(value
, '|') != NULL
);
922 has_glob
= string_is_glob(value
);
923 if (has_split
&& has_glob
) {
924 glob
= GL_SPLIT_GLOB
;
925 } else if (has_split
) {
927 } else if (has_glob
) {
928 if (streq(value
, "?*"))
935 token
->key
.glob
= glob
;
938 if (value
!= NULL
&& type
> TK_M_MAX
) {
939 /* check if assigned value has substitution chars */
941 token
->key
.subst
= SB_SUBSYS
;
942 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
943 token
->key
.subst
= SB_FORMAT
;
945 token
->key
.subst
= SB_NONE
;
949 /* check if property/attribute name has substitution chars */
951 token
->key
.attrsubst
= SB_SUBSYS
;
952 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
953 token
->key
.attrsubst
= SB_FORMAT
;
955 token
->key
.attrsubst
= SB_NONE
;
958 token
->key
.type
= type
;
960 rule_tmp
->token_cur
++;
963 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
965 unsigned int start
= 0;
966 unsigned int end
= rule_tmp
->token_cur
;
968 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
969 enum token_type next_val
= TK_UNSET
;
970 unsigned int next_idx
= 0;
973 /* find smallest value */
974 for (j
= start
; j
< end
; j
++) {
975 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
977 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
978 next_val
= rule_tmp
->token
[j
].type
;
983 /* add token and mark done */
984 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
986 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
989 if (next_idx
== start
)
991 if (next_idx
+1 == end
)
997 #define LOG_RULE_ERROR(fmt, ...) log_error("Invalid rule %s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
998 #define LOG_RULE_WARNING(fmt, ...) log_warning("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
999 #define LOG_RULE_DEBUG(fmt, ...) log_debug("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1000 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1002 static void add_rule(struct udev_rules
*rules
, char *line
,
1003 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1006 struct rule_tmp rule_tmp
= {
1008 .rule
.type
= TK_RULE
,
1011 /* the offset in the rule is limited to unsigned short */
1012 if (filename_off
< USHRT_MAX
)
1013 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1014 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1020 enum operation_type op
;
1022 if (get_key(&linepos
, &key
, &op
, &value
) != 0) {
1023 /* Avoid erroring on trailing whitespace. This is probably rare
1024 * so save the work for the error case instead of always trying
1025 * to strip the trailing whitespace with strstrip(). */
1026 while (isblank(*linepos
))
1029 /* If we aren't at the end of the line, this is a parsing error.
1030 * Make a best effort to describe where the problem is. */
1031 if (!strchr(NEWLINE
, *linepos
)) {
1032 char buf
[2] = {*linepos
};
1033 _cleanup_free_
char *tmp
;
1036 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1037 filename
, lineno
, linepos
- line
+ 1, tmp
);
1038 if (*linepos
== '#')
1039 log_error("hint: comments can only start at beginning of line");
1044 if (rule_tmp
.token_cur
>= ELEMENTSOF(rule_tmp
.token
))
1045 LOG_AND_RETURN("temporary rule array too small, aborting event processing with %u items", rule_tmp
.token_cur
);
1047 if (streq(key
, "ACTION")) {
1048 if (op
> OP_MATCH_MAX
)
1049 LOG_AND_RETURN("invalid %s operation", key
);
1051 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1053 } else if (streq(key
, "DEVPATH")) {
1054 if (op
> OP_MATCH_MAX
)
1055 LOG_AND_RETURN("invalid %s operation", key
);
1057 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1059 } else if (streq(key
, "KERNEL")) {
1060 if (op
> OP_MATCH_MAX
)
1061 LOG_AND_RETURN("invalid %s operation", key
);
1063 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1065 } else if (streq(key
, "SUBSYSTEM")) {
1066 if (op
> OP_MATCH_MAX
)
1067 LOG_AND_RETURN("invalid %s operation", key
);
1069 /* bus, class, subsystem events should all be the same */
1070 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1071 if (!streq(value
, "subsystem"))
1072 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1074 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1076 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1078 } else if (streq(key
, "DRIVER")) {
1079 if (op
> OP_MATCH_MAX
)
1080 LOG_AND_RETURN("invalid %s operation", key
);
1082 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1084 } else if (startswith(key
, "ATTR{")) {
1085 attr
= get_key_attribute(key
+ STRLEN("ATTR"));
1087 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1089 if (op
== OP_REMOVE
)
1090 LOG_AND_RETURN("invalid %s operation", "ATTR");
1092 if (op
< OP_MATCH_MAX
)
1093 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1095 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1097 } else if (startswith(key
, "SYSCTL{")) {
1098 attr
= get_key_attribute(key
+ STRLEN("SYSCTL"));
1100 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1102 if (op
== OP_REMOVE
)
1103 LOG_AND_RETURN("invalid %s operation", "ATTR");
1105 if (op
< OP_MATCH_MAX
)
1106 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1108 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1110 } else if (startswith(key
, "SECLABEL{")) {
1111 attr
= get_key_attribute(key
+ STRLEN("SECLABEL"));
1113 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1115 if (op
== OP_REMOVE
)
1116 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1118 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1120 } else if (streq(key
, "KERNELS")) {
1121 if (op
> OP_MATCH_MAX
)
1122 LOG_AND_RETURN("invalid %s operation", key
);
1124 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1126 } else if (streq(key
, "SUBSYSTEMS")) {
1127 if (op
> OP_MATCH_MAX
)
1128 LOG_AND_RETURN("invalid %s operation", key
);
1130 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1132 } else if (streq(key
, "DRIVERS")) {
1133 if (op
> OP_MATCH_MAX
)
1134 LOG_AND_RETURN("invalid %s operation", key
);
1136 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1138 } else if (startswith(key
, "ATTRS{")) {
1139 if (op
> OP_MATCH_MAX
)
1140 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1142 attr
= get_key_attribute(key
+ STRLEN("ATTRS"));
1144 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1146 if (startswith(attr
, "device/"))
1147 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1148 if (strstr(attr
, "../") != NULL
)
1149 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1150 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1152 } else if (streq(key
, "TAGS")) {
1153 if (op
> OP_MATCH_MAX
)
1154 LOG_AND_RETURN("invalid %s operation", key
);
1156 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1158 } else if (startswith(key
, "ENV{")) {
1159 attr
= get_key_attribute(key
+ STRLEN("ENV"));
1161 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1163 if (op
== OP_REMOVE
)
1164 LOG_AND_RETURN("invalid %s operation", "ENV");
1166 if (op
< OP_MATCH_MAX
)
1167 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1169 if (STR_IN_SET(attr
,
1181 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr
);
1183 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1186 } else if (streq(key
, "TAG")) {
1187 if (op
< OP_MATCH_MAX
)
1188 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1190 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1192 } else if (streq(key
, "PROGRAM")) {
1193 if (op
== OP_REMOVE
)
1194 LOG_AND_RETURN("invalid %s operation", key
);
1196 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1198 } else if (streq(key
, "RESULT")) {
1199 if (op
> OP_MATCH_MAX
)
1200 LOG_AND_RETURN("invalid %s operation", key
);
1202 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1204 } else if (startswith(key
, "IMPORT")) {
1205 attr
= get_key_attribute(key
+ STRLEN("IMPORT"));
1207 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1210 if (op
== OP_REMOVE
)
1211 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1213 if (streq(attr
, "program")) {
1214 /* find known built-in command */
1215 if (value
[0] != '/') {
1216 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1218 if (cmd
< UDEV_BUILTIN_MAX
) {
1219 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1220 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1224 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1225 } else if (streq(attr
, "builtin")) {
1226 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1228 if (cmd
>= UDEV_BUILTIN_MAX
)
1229 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1231 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1232 } else if (streq(attr
, "file"))
1233 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1234 else if (streq(attr
, "db"))
1235 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1236 else if (streq(attr
, "cmdline"))
1237 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1238 else if (streq(attr
, "parent"))
1239 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1241 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1243 } else if (startswith(key
, "TEST")) {
1246 if (op
> OP_MATCH_MAX
)
1247 LOG_AND_RETURN("invalid %s operation", "TEST");
1249 attr
= get_key_attribute(key
+ STRLEN("TEST"));
1251 mode
= strtol(attr
, NULL
, 8);
1252 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1254 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1256 } else if (startswith(key
, "RUN")) {
1257 attr
= get_key_attribute(key
+ STRLEN("RUN"));
1260 if (op
== OP_REMOVE
)
1261 LOG_AND_RETURN("invalid %s operation", "RUN");
1263 if (streq(attr
, "builtin")) {
1264 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1266 if (cmd
< UDEV_BUILTIN_MAX
)
1267 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1269 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1270 } else if (streq(attr
, "program")) {
1271 const enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1273 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1275 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr
);
1277 } else if (streq(key
, "LABEL")) {
1278 if (op
== OP_REMOVE
)
1279 LOG_AND_RETURN("invalid %s operation", key
);
1281 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1283 } else if (streq(key
, "GOTO")) {
1284 if (op
== OP_REMOVE
)
1285 LOG_AND_RETURN("invalid %s operation", key
);
1287 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1289 } else if (startswith(key
, "NAME")) {
1290 if (op
== OP_REMOVE
)
1291 LOG_AND_RETURN("invalid %s operation", key
);
1293 if (op
< OP_MATCH_MAX
)
1294 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1296 if (streq(value
, "%k")) {
1297 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1300 if (isempty(value
)) {
1301 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1304 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1306 rule_tmp
.rule
.rule
.can_set_name
= true;
1308 } else if (streq(key
, "SYMLINK")) {
1309 if (op
== OP_REMOVE
)
1310 LOG_AND_RETURN("invalid %s operation", key
);
1312 if (op
< OP_MATCH_MAX
)
1313 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1315 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1316 rule_tmp
.rule
.rule
.can_set_name
= true;
1318 } else if (streq(key
, "OWNER")) {
1322 if (op
== OP_REMOVE
)
1323 LOG_AND_RETURN("invalid %s operation", key
);
1325 uid
= strtoul(value
, &endptr
, 10);
1326 if (endptr
[0] == '\0')
1327 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1328 else if (rules
->resolve_names
> 0 && strchr("$%", value
[0]) == NULL
) {
1329 uid
= add_uid(rules
, value
);
1330 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1331 } else if (rules
->resolve_names
>= 0)
1332 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1334 rule_tmp
.rule
.rule
.can_set_name
= true;
1336 } else if (streq(key
, "GROUP")) {
1340 if (op
== OP_REMOVE
)
1341 LOG_AND_RETURN("invalid %s operation", key
);
1343 gid
= strtoul(value
, &endptr
, 10);
1344 if (endptr
[0] == '\0')
1345 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1346 else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1347 gid
= add_gid(rules
, value
);
1348 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1349 } else if (rules
->resolve_names
>= 0)
1350 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1352 rule_tmp
.rule
.rule
.can_set_name
= true;
1354 } else if (streq(key
, "MODE")) {
1358 if (op
== OP_REMOVE
)
1359 LOG_AND_RETURN("invalid %s operation", key
);
1361 mode
= strtol(value
, &endptr
, 8);
1362 if (endptr
[0] == '\0')
1363 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1365 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1366 rule_tmp
.rule
.rule
.can_set_name
= true;
1368 } else if (streq(key
, "OPTIONS")) {
1371 if (op
== OP_REMOVE
)
1372 LOG_AND_RETURN("invalid %s operation", key
);
1374 pos
= strstr(value
, "link_priority=");
1376 int prio
= atoi(pos
+ STRLEN("link_priority="));
1378 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1381 pos
= strstr(value
, "string_escape=");
1383 pos
+= STRLEN("string_escape=");
1384 if (startswith(pos
, "none"))
1385 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1386 else if (startswith(pos
, "replace"))
1387 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1390 pos
= strstr(value
, "db_persist");
1392 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1394 pos
= strstr(value
, "nowatch");
1398 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1400 pos
= strstr(value
, "watch");
1404 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1408 pos
= strstr(value
, "static_node=");
1410 pos
+= STRLEN("static_node=");
1411 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1412 rule_tmp
.rule
.rule
.has_static_node
= true;
1416 LOG_AND_RETURN("unknown key '%s'", key
);
1419 /* add rule token and sort tokens */
1420 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1421 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1422 LOG_RULE_ERROR("failed to add rule token");
1425 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1426 _cleanup_fclose_
FILE *f
= NULL
;
1427 unsigned int first_token
;
1428 unsigned int filename_off
;
1429 char line
[UTIL_LINE_SIZE
];
1433 f
= fopen(filename
, "re");
1435 if (errno
== ENOENT
)
1441 if (null_or_empty_fd(fileno(f
))) {
1442 log_debug("Skipping empty file: %s", filename
);
1445 log_debug("Reading rules file: %s", filename
);
1447 first_token
= rules
->token_cur
;
1448 filename_off
= rules_add_string(rules
, filename
);
1450 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1454 /* skip whitespace */
1457 while (isspace(key
[0]))
1468 /* continue reading if backslash+newline is found */
1469 while (line
[len
-2] == '\\') {
1470 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1472 if (strlen(&line
[len
-2]) < 2)
1478 if (len
+1 >= sizeof(line
)) {
1479 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1482 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1485 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1486 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1487 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1488 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1491 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1492 if (rules
->tokens
[j
].type
!= TK_RULE
)
1494 if (rules
->tokens
[j
].rule
.label_off
== 0)
1496 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1498 rules
->tokens
[i
].key
.rule_goto
= j
;
1501 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1502 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1508 struct udev_rules
*udev_rules_new(int resolve_names
) {
1509 struct udev_rules
*rules
;
1510 struct udev_list file_list
;
1511 struct token end_token
;
1515 rules
= new0(struct udev_rules
, 1);
1518 rules
->resolve_names
= resolve_names
;
1519 udev_list_init(NULL
, &file_list
, true);
1521 /* init token array and string buffer */
1522 rules
->tokens
= malloc_multiply(PREALLOC_TOKEN
, sizeof(struct token
));
1523 if (rules
->tokens
== NULL
)
1524 return udev_rules_unref(rules
);
1525 rules
->token_max
= PREALLOC_TOKEN
;
1527 rules
->strbuf
= strbuf_new();
1529 return udev_rules_unref(rules
);
1531 udev_rules_check_timestamp(rules
);
1533 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1535 log_error_errno(r
, "failed to enumerate rules files: %m");
1536 return udev_rules_unref(rules
);
1540 * The offset value in the rules strct is limited; add all
1541 * rules file names to the beginning of the string buffer.
1543 STRV_FOREACH(f
, files
)
1544 rules_add_string(rules
, *f
);
1546 STRV_FOREACH(f
, files
)
1547 parse_file(rules
, *f
);
1551 memzero(&end_token
, sizeof(struct token
));
1552 end_token
.type
= TK_END
;
1553 add_token(rules
, &end_token
);
1554 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1555 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1557 /* cleanup temporary strbuf data */
1558 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1559 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1560 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1561 strbuf_complete(rules
->strbuf
);
1563 /* cleanup uid/gid cache */
1564 rules
->uids
= mfree(rules
->uids
);
1565 rules
->uids_cur
= 0;
1566 rules
->uids_max
= 0;
1567 rules
->gids
= mfree(rules
->gids
);
1568 rules
->gids_cur
= 0;
1569 rules
->gids_max
= 0;
1575 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1578 free(rules
->tokens
);
1579 strbuf_cleanup(rules
->strbuf
);
1582 return mfree(rules
);
1585 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1589 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1592 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1593 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1600 switch (token
->key
.glob
) {
1602 match
= (streq(key_value
, val
));
1605 match
= (fnmatch(key_value
, val
, 0) == 0);
1612 s
= rules_str(rules
, token
->key
.value_off
);
1617 next
= strchr(s
, '|');
1619 size_t matchlen
= (size_t)(next
- s
);
1621 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1625 match
= (streq(s
, val
));
1634 char value
[UTIL_PATH_SIZE
];
1636 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1638 while (key_value
!= NULL
) {
1639 pos
= strchr(key_value
, '|');
1644 match
= (fnmatch(key_value
, val
, 0) == 0);
1652 match
= (val
[0] != '\0');
1658 if (match
&& (token
->key
.op
== OP_MATCH
))
1660 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1665 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1667 char nbuf
[UTIL_NAME_SIZE
];
1669 char vbuf
[UTIL_NAME_SIZE
];
1672 name
= rules_str(rules
, cur
->key
.attr_off
);
1673 switch (cur
->key
.attrsubst
) {
1675 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1679 value
= udev_device_get_sysattr_value(dev
, name
);
1684 if (util_resolve_subsys_kernel(name
, vbuf
, sizeof(vbuf
), 1) != 0)
1692 /* remove trailing whitespace, if not asked to match for it */
1693 len
= strlen(value
);
1694 if (len
> 0 && isspace(value
[len
-1])) {
1695 const char *key_value
;
1698 key_value
= rules_str(rules
, cur
->key
.value_off
);
1699 klen
= strlen(key_value
);
1700 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1701 if (value
!= vbuf
) {
1702 strscpy(vbuf
, sizeof(vbuf
), value
);
1705 while (len
> 0 && isspace(vbuf
[--len
]))
1710 return match_key(rules
, cur
, value
);
1719 void udev_rules_apply_to_event(struct udev_rules
*rules
,
1720 struct udev_event
*event
,
1721 usec_t timeout_usec
,
1722 usec_t timeout_warn_usec
,
1723 struct udev_list
*properties_list
) {
1726 enum escape_type esc
= ESCAPE_UNSET
;
1730 if (rules
->tokens
== NULL
)
1733 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1734 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1735 udev_device_get_ifindex(event
->dev
) > 0));
1737 /* loop through token list, match, run actions or forward to next rule */
1738 cur
= &rules
->tokens
[0];
1741 dump_token(rules
, cur
);
1742 switch (cur
->type
) {
1746 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1747 if (!can_set_name
&& rule
->rule
.can_set_name
)
1752 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1756 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1760 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1763 case TK_M_DEVLINK
: {
1764 struct udev_list_entry
*list_entry
;
1767 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1768 const char *devlink
;
1770 devlink
= udev_list_entry_get_name(list_entry
) + STRLEN("/dev/");
1771 if (match_key(rules
, cur
, devlink
) == 0) {
1781 if (match_key(rules
, cur
, event
->name
) != 0)
1785 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1788 value
= udev_device_get_property_value(event
->dev
, key_name
);
1790 /* check global properties */
1791 if (!value
&& properties_list
) {
1792 struct udev_list_entry
*list_entry
;
1794 list_entry
= udev_list_get_entry(properties_list
);
1795 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1796 if (list_entry
!= NULL
)
1797 value
= udev_list_entry_get_value(list_entry
);
1802 if (match_key(rules
, cur
, value
))
1807 struct udev_list_entry
*list_entry
;
1810 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1811 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1816 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1817 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1821 case TK_M_SUBSYSTEM
:
1822 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1826 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1830 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1834 char filename
[UTIL_PATH_SIZE
];
1835 _cleanup_free_
char *value
= NULL
;
1838 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1839 sysctl_normalize(filename
);
1840 if (sysctl_read(filename
, &value
) < 0)
1843 len
= strlen(value
);
1844 while (len
> 0 && isspace(value
[--len
]))
1846 if (match_key(rules
, cur
, value
) != 0)
1851 case TK_M_SUBSYSTEMS
:
1857 /* get whole sequence of parent matches */
1859 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1862 /* loop over parents */
1863 event
->dev_parent
= event
->dev
;
1867 /* loop over sequence of parent match keys */
1868 for (key
= cur
; key
< next
; key
++ ) {
1869 dump_token(rules
, key
);
1872 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1875 case TK_M_SUBSYSTEMS
:
1876 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1880 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1884 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1888 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1890 if (match
&& key
->key
.op
== OP_NOMATCH
)
1892 if (!match
&& key
->key
.op
== OP_MATCH
)
1903 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1904 if (event
->dev_parent
== NULL
)
1907 /* move behind our sequence of parent match keys */
1912 char filename
[UTIL_PATH_SIZE
];
1913 struct stat statbuf
;
1916 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1917 if (util_resolve_subsys_kernel(filename
, filename
, sizeof(filename
), 0) != 0) {
1918 if (filename
[0] != '/') {
1919 char tmp
[UTIL_PATH_SIZE
];
1921 strscpy(tmp
, sizeof(tmp
), filename
);
1922 strscpyl(filename
, sizeof(filename
),
1923 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
1926 attr_subst_subdir(filename
, sizeof(filename
));
1928 match
= (stat(filename
, &statbuf
) == 0);
1929 if (match
&& cur
->key
.mode
> 0)
1930 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1931 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1933 if (!match
&& cur
->key
.op
== OP_MATCH
)
1937 case TK_M_PROGRAM
: {
1938 char program
[UTIL_PATH_SIZE
];
1939 char result
[UTIL_LINE_SIZE
];
1941 event
->program_result
= mfree(event
->program_result
);
1942 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1943 log_debug("PROGRAM '%s' %s:%u",
1945 rules_str(rules
, rule
->rule
.filename_off
),
1946 rule
->rule
.filename_line
);
1948 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
1949 if (cur
->key
.op
!= OP_NOMATCH
)
1954 delete_trailing_chars(result
, "\n");
1955 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1956 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1958 log_debug("%i character(s) replaced" , count
);
1960 event
->program_result
= strdup(result
);
1961 if (cur
->key
.op
== OP_NOMATCH
)
1966 case TK_M_IMPORT_FILE
: {
1967 char import
[UTIL_PATH_SIZE
];
1969 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1970 if (import_file_into_properties(event
->dev
, import
) != 0)
1971 if (cur
->key
.op
!= OP_NOMATCH
)
1975 case TK_M_IMPORT_PROG
: {
1976 char import
[UTIL_PATH_SIZE
];
1978 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1979 log_debug("IMPORT '%s' %s:%u",
1981 rules_str(rules
, rule
->rule
.filename_off
),
1982 rule
->rule
.filename_line
);
1984 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
1985 if (cur
->key
.op
!= OP_NOMATCH
)
1989 case TK_M_IMPORT_BUILTIN
: {
1990 char command
[UTIL_PATH_SIZE
];
1992 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
1993 /* check if we ran already */
1994 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
1995 log_debug("IMPORT builtin skip '%s' %s:%u",
1996 udev_builtin_name(cur
->key
.builtin_cmd
),
1997 rules_str(rules
, rule
->rule
.filename_off
),
1998 rule
->rule
.filename_line
);
1999 /* return the result from earlier run */
2000 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2001 if (cur
->key
.op
!= OP_NOMATCH
)
2006 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2009 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2010 log_debug("IMPORT builtin '%s' %s:%u",
2011 udev_builtin_name(cur
->key
.builtin_cmd
),
2012 rules_str(rules
, rule
->rule
.filename_off
),
2013 rule
->rule
.filename_line
);
2015 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2016 /* remember failure */
2017 log_debug("IMPORT builtin '%s' returned non-zero",
2018 udev_builtin_name(cur
->key
.builtin_cmd
));
2019 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2020 if (cur
->key
.op
!= OP_NOMATCH
)
2025 case TK_M_IMPORT_DB
: {
2026 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2029 value
= udev_device_get_property_value(event
->dev_db
, key
);
2031 udev_device_add_property(event
->dev
, key
, value
);
2033 if (cur
->key
.op
!= OP_NOMATCH
)
2038 case TK_M_IMPORT_CMDLINE
: {
2039 _cleanup_free_
char *value
= NULL
;
2040 bool imported
= false;
2043 key
= rules_str(rules
, cur
->key
.value_off
);
2045 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2047 log_debug_errno(r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2052 udev_device_add_property(event
->dev
, key
, value
);
2054 /* we import simple flags as 'FLAG=1' */
2055 udev_device_add_property(event
->dev
, key
, "1");
2058 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2062 case TK_M_IMPORT_PARENT
: {
2063 char import
[UTIL_PATH_SIZE
];
2065 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2066 if (import_parent_into_properties(event
->dev
, import
) != 0)
2067 if (cur
->key
.op
!= OP_NOMATCH
)
2072 if (match_key(rules
, cur
, event
->program_result
) != 0)
2075 case TK_A_STRING_ESCAPE_NONE
:
2078 case TK_A_STRING_ESCAPE_REPLACE
:
2079 esc
= ESCAPE_REPLACE
;
2081 case TK_A_DB_PERSIST
:
2082 udev_device_set_db_persist(event
->dev
);
2084 case TK_A_INOTIFY_WATCH
:
2085 if (event
->inotify_watch_final
)
2087 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2088 event
->inotify_watch_final
= true;
2089 event
->inotify_watch
= cur
->key
.watch
;
2091 case TK_A_DEVLINK_PRIO
:
2092 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2095 char owner
[UTIL_NAME_SIZE
];
2096 const char *ow
= owner
;
2098 if (event
->owner_final
)
2100 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2101 event
->owner_final
= true;
2102 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2103 event
->owner_set
= true;
2104 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2106 log_unknown_owner(r
, "user", owner
);
2109 log_debug("OWNER %u %s:%u",
2111 rules_str(rules
, rule
->rule
.filename_off
),
2112 rule
->rule
.filename_line
);
2116 char group
[UTIL_NAME_SIZE
];
2117 const char *gr
= group
;
2119 if (event
->group_final
)
2121 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2122 event
->group_final
= true;
2123 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2124 event
->group_set
= true;
2125 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2127 log_unknown_owner(r
, "group", group
);
2130 log_debug("GROUP %u %s:%u",
2132 rules_str(rules
, rule
->rule
.filename_off
),
2133 rule
->rule
.filename_line
);
2137 char mode_str
[UTIL_NAME_SIZE
];
2141 if (event
->mode_final
)
2143 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2144 mode
= strtol(mode_str
, &endptr
, 8);
2145 if (endptr
[0] != '\0') {
2146 log_error("ignoring invalid mode '%s'", mode_str
);
2149 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2150 event
->mode_final
= true;
2151 event
->mode_set
= true;
2153 log_debug("MODE %#o %s:%u",
2155 rules_str(rules
, rule
->rule
.filename_off
),
2156 rule
->rule
.filename_line
);
2160 if (event
->owner_final
)
2162 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2163 event
->owner_final
= true;
2164 event
->owner_set
= true;
2165 event
->uid
= cur
->key
.uid
;
2166 log_debug("OWNER %u %s:%u",
2168 rules_str(rules
, rule
->rule
.filename_off
),
2169 rule
->rule
.filename_line
);
2172 if (event
->group_final
)
2174 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2175 event
->group_final
= true;
2176 event
->group_set
= true;
2177 event
->gid
= cur
->key
.gid
;
2178 log_debug("GROUP %u %s:%u",
2180 rules_str(rules
, rule
->rule
.filename_off
),
2181 rule
->rule
.filename_line
);
2184 if (event
->mode_final
)
2186 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2187 event
->mode_final
= true;
2188 event
->mode_set
= true;
2189 event
->mode
= cur
->key
.mode
;
2190 log_debug("MODE %#o %s:%u",
2192 rules_str(rules
, rule
->rule
.filename_off
),
2193 rule
->rule
.filename_line
);
2195 case TK_A_SECLABEL
: {
2196 char label_str
[UTIL_LINE_SIZE
] = {};
2197 const char *name
, *label
;
2199 name
= rules_str(rules
, cur
->key
.attr_off
);
2200 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2201 if (label_str
[0] != '\0')
2204 label
= rules_str(rules
, cur
->key
.value_off
);
2206 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2207 udev_list_cleanup(&event
->seclabel_list
);
2208 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2209 log_debug("SECLABEL{%s}='%s' %s:%u",
2211 rules_str(rules
, rule
->rule
.filename_off
),
2212 rule
->rule
.filename_line
);
2216 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2217 char *value
= rules_str(rules
, cur
->key
.value_off
);
2218 char value_new
[UTIL_NAME_SIZE
];
2219 const char *value_old
= NULL
;
2221 if (value
[0] == '\0') {
2222 if (cur
->key
.op
== OP_ADD
)
2224 udev_device_add_property(event
->dev
, name
, NULL
);
2228 if (cur
->key
.op
== OP_ADD
)
2229 value_old
= udev_device_get_property_value(event
->dev
, name
);
2231 char temp
[UTIL_NAME_SIZE
];
2233 /* append value separated by space */
2234 udev_event_apply_format(event
, value
, temp
, sizeof(temp
), false);
2235 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2237 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
), false);
2239 udev_device_add_property(event
->dev
, name
, value_new
);
2243 char tag
[UTIL_PATH_SIZE
];
2246 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2247 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2248 udev_device_cleanup_tags_list(event
->dev
);
2249 for (p
= tag
; *p
!= '\0'; p
++) {
2250 if ((*p
>= 'a' && *p
<= 'z') ||
2251 (*p
>= 'A' && *p
<= 'Z') ||
2252 (*p
>= '0' && *p
<= '9') ||
2253 IN_SET(*p
, '-', '_'))
2255 log_error("ignoring invalid tag name '%s'", tag
);
2258 if (cur
->key
.op
== OP_REMOVE
)
2259 udev_device_remove_tag(event
->dev
, tag
);
2261 udev_device_add_tag(event
->dev
, tag
);
2265 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2267 char name_str
[UTIL_PATH_SIZE
];
2270 if (event
->name_final
)
2272 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2273 event
->name_final
= true;
2274 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2275 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2276 count
= util_replace_chars(name_str
, "/");
2278 log_debug("%i character(s) replaced", count
);
2280 if (major(udev_device_get_devnum(event
->dev
)) &&
2281 !streq(name_str
, udev_device_get_devnode(event
->dev
) + STRLEN("/dev/"))) {
2282 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2284 rules_str(rules
, rule
->rule
.filename_off
),
2285 rule
->rule
.filename_line
);
2288 if (free_and_strdup(&event
->name
, name_str
) < 0) {
2292 log_debug("NAME '%s' %s:%u",
2294 rules_str(rules
, rule
->rule
.filename_off
),
2295 rule
->rule
.filename_line
);
2298 case TK_A_DEVLINK
: {
2299 char temp
[UTIL_PATH_SIZE
];
2300 char filename
[UTIL_PATH_SIZE
];
2304 if (event
->devlink_final
)
2306 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2308 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2309 event
->devlink_final
= true;
2310 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2311 udev_device_cleanup_devlinks_list(event
->dev
);
2313 /* allow multiple symlinks separated by spaces */
2314 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2315 if (esc
== ESCAPE_UNSET
)
2316 count
= util_replace_chars(temp
, "/ ");
2317 else if (esc
== ESCAPE_REPLACE
)
2318 count
= util_replace_chars(temp
, "/");
2320 log_debug("%i character(s) replaced" , count
);
2322 while (isspace(pos
[0]))
2324 next
= strchr(pos
, ' ');
2325 while (next
!= NULL
) {
2327 log_debug("LINK '%s' %s:%u", pos
,
2328 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2329 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2330 udev_device_add_devlink(event
->dev
, filename
);
2331 while (isspace(next
[1]))
2334 next
= strchr(pos
, ' ');
2336 if (pos
[0] != '\0') {
2337 log_debug("LINK '%s' %s:%u", pos
,
2338 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2339 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2340 udev_device_add_devlink(event
->dev
, filename
);
2345 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2346 char attr
[UTIL_PATH_SIZE
];
2347 char value
[UTIL_NAME_SIZE
];
2348 _cleanup_fclose_
FILE *f
= NULL
;
2350 if (util_resolve_subsys_kernel(key_name
, attr
, sizeof(attr
), 0) != 0)
2351 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2352 attr_subst_subdir(attr
, sizeof(attr
));
2354 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2355 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2356 rules_str(rules
, rule
->rule
.filename_off
),
2357 rule
->rule
.filename_line
);
2358 f
= fopen(attr
, "we");
2360 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2361 else if (fprintf(f
, "%s", value
) <= 0)
2362 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2366 char filename
[UTIL_PATH_SIZE
];
2367 char value
[UTIL_NAME_SIZE
];
2369 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2370 sysctl_normalize(filename
);
2371 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2372 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2373 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2374 r
= sysctl_write(filename
, value
);
2376 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2379 case TK_A_RUN_BUILTIN
:
2380 case TK_A_RUN_PROGRAM
: {
2381 struct udev_list_entry
*entry
;
2383 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2384 udev_list_cleanup(&event
->run_list
);
2385 log_debug("RUN '%s' %s:%u",
2386 rules_str(rules
, cur
->key
.value_off
),
2387 rules_str(rules
, rule
->rule
.filename_off
),
2388 rule
->rule
.filename_line
);
2389 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2390 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2394 if (cur
->key
.rule_goto
== 0)
2396 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2401 case TK_M_PARENTS_MIN
:
2402 case TK_M_PARENTS_MAX
:
2405 log_error("wrong type %u", cur
->type
);
2412 /* fast-forward to next rule */
2413 cur
= rule
+ rule
->rule
.token_count
;
2417 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2423 _cleanup_strv_free_
char **tags
= NULL
;
2426 _cleanup_free_
char *path
= NULL
;
2429 if (rules
->tokens
== NULL
)
2432 cur
= &rules
->tokens
[0];
2435 switch (cur
->type
) {
2440 /* skip rules without a static_node tag */
2441 if (!rule
->rule
.has_static_node
)
2447 tags
= strv_free(tags
);
2456 mode
= cur
->key
.mode
;
2459 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2464 case TK_A_STATIC_NODE
: {
2465 char device_node
[UTIL_PATH_SIZE
];
2466 char tags_dir
[UTIL_PATH_SIZE
];
2467 char tag_symlink
[UTIL_PATH_SIZE
];
2470 /* we assure, that the permissions tokens are sorted before the static token */
2472 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2475 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2476 if (stat(device_node
, &stats
) != 0)
2478 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2481 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2483 STRV_FOREACH(t
, tags
) {
2484 _cleanup_free_
char *unescaped_filename
= NULL
;
2486 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2487 r
= mkdir_p(tags_dir
, 0755);
2489 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2491 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2493 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2494 r
= symlink(device_node
, tag_symlink
);
2495 if (r
< 0 && errno
!= EEXIST
)
2496 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2497 tag_symlink
, device_node
);
2501 /* don't touch the permissions if only the tags were set */
2502 if (mode
== 0 && uid
== 0 && gid
== 0)
2511 if (mode
!= (stats
.st_mode
& 01777)) {
2512 r
= chmod(device_node
, mode
);
2514 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2517 log_debug("chmod '%s' %#o", device_node
, mode
);
2520 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2521 r
= chown(device_node
, uid
, gid
);
2523 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2524 device_node
, uid
, gid
);
2526 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2529 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2539 /* fast-forward to next rule */
2540 cur
= rule
+ rule
->rule
.token_count
;
2547 fchmod(fileno(f
), 0644);
2548 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2549 unlink_noerrno("/run/udev/static_node-tags");
2550 unlink_noerrno(path
);