1 /* SPDX-License-Identifier: GPL-2.0+ */
16 #include "alloc-util.h"
17 #include "conf-files.h"
18 #include "dirent-util.h"
22 #include "glob-util.h"
23 #include "path-util.h"
24 #include "proc-cmdline.h"
25 #include "stat-util.h"
26 #include "stdio-util.h"
28 #include "string-util.h"
30 #include "sysctl-util.h"
32 #include "user-util.h"
35 #define PREALLOC_TOKEN 2048
38 unsigned int name_off
;
45 static const char* const rules_dirs
[] = {
48 UDEVLIBEXECDIR
"/rules.d",
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(struct udev
*udev
, 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(struct udev
*udev
, 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(rules
->udev
, &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(rules
->udev
,
1086 key
+ STRLEN("ATTR"));
1088 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1090 if (op
== OP_REMOVE
)
1091 LOG_AND_RETURN("invalid %s operation", "ATTR");
1093 if (op
< OP_MATCH_MAX
)
1094 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1096 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1098 } else if (startswith(key
, "SYSCTL{")) {
1099 attr
= get_key_attribute(rules
->udev
,
1100 key
+ STRLEN("SYSCTL"));
1102 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1104 if (op
== OP_REMOVE
)
1105 LOG_AND_RETURN("invalid %s operation", "ATTR");
1107 if (op
< OP_MATCH_MAX
)
1108 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1110 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1112 } else if (startswith(key
, "SECLABEL{")) {
1113 attr
= get_key_attribute(rules
->udev
,
1114 key
+ STRLEN("SECLABEL"));
1116 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1118 if (op
== OP_REMOVE
)
1119 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1121 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1123 } else if (streq(key
, "KERNELS")) {
1124 if (op
> OP_MATCH_MAX
)
1125 LOG_AND_RETURN("invalid %s operation", key
);
1127 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1129 } else if (streq(key
, "SUBSYSTEMS")) {
1130 if (op
> OP_MATCH_MAX
)
1131 LOG_AND_RETURN("invalid %s operation", key
);
1133 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1135 } else if (streq(key
, "DRIVERS")) {
1136 if (op
> OP_MATCH_MAX
)
1137 LOG_AND_RETURN("invalid %s operation", key
);
1139 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1141 } else if (startswith(key
, "ATTRS{")) {
1142 if (op
> OP_MATCH_MAX
)
1143 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1145 attr
= get_key_attribute(rules
->udev
,
1146 key
+ STRLEN("ATTRS"));
1148 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1150 if (startswith(attr
, "device/"))
1151 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1152 if (strstr(attr
, "../") != NULL
)
1153 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1154 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1156 } else if (streq(key
, "TAGS")) {
1157 if (op
> OP_MATCH_MAX
)
1158 LOG_AND_RETURN("invalid %s operation", key
);
1160 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1162 } else if (startswith(key
, "ENV{")) {
1163 attr
= get_key_attribute(rules
->udev
,
1164 key
+ STRLEN("ENV"));
1166 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1168 if (op
== OP_REMOVE
)
1169 LOG_AND_RETURN("invalid %s operation", "ENV");
1171 if (op
< OP_MATCH_MAX
)
1172 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1174 if (STR_IN_SET(attr
,
1186 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr
);
1188 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1191 } else if (streq(key
, "TAG")) {
1192 if (op
< OP_MATCH_MAX
)
1193 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1195 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1197 } else if (streq(key
, "PROGRAM")) {
1198 if (op
== OP_REMOVE
)
1199 LOG_AND_RETURN("invalid %s operation", key
);
1201 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1203 } else if (streq(key
, "RESULT")) {
1204 if (op
> OP_MATCH_MAX
)
1205 LOG_AND_RETURN("invalid %s operation", key
);
1207 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1209 } else if (startswith(key
, "IMPORT")) {
1210 attr
= get_key_attribute(rules
->udev
,
1211 key
+ STRLEN("IMPORT"));
1213 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1216 if (op
== OP_REMOVE
)
1217 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1219 if (streq(attr
, "program")) {
1220 /* find known built-in command */
1221 if (value
[0] != '/') {
1222 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1224 if (cmd
< UDEV_BUILTIN_MAX
) {
1225 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1226 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1230 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1231 } else if (streq(attr
, "builtin")) {
1232 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1234 if (cmd
>= UDEV_BUILTIN_MAX
)
1235 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1237 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1238 } else if (streq(attr
, "file"))
1239 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1240 else if (streq(attr
, "db"))
1241 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1242 else if (streq(attr
, "cmdline"))
1243 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1244 else if (streq(attr
, "parent"))
1245 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1247 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1249 } else if (startswith(key
, "TEST")) {
1252 if (op
> OP_MATCH_MAX
)
1253 LOG_AND_RETURN("invalid %s operation", "TEST");
1255 attr
= get_key_attribute(rules
->udev
,
1256 key
+ STRLEN("TEST"));
1258 mode
= strtol(attr
, NULL
, 8);
1259 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1261 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1263 } else if (startswith(key
, "RUN")) {
1264 attr
= get_key_attribute(rules
->udev
,
1265 key
+ STRLEN("RUN"));
1268 if (op
== OP_REMOVE
)
1269 LOG_AND_RETURN("invalid %s operation", "RUN");
1271 if (streq(attr
, "builtin")) {
1272 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1274 if (cmd
< UDEV_BUILTIN_MAX
)
1275 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1277 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1278 } else if (streq(attr
, "program")) {
1279 const enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1281 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1283 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr
);
1285 } else if (streq(key
, "LABEL")) {
1286 if (op
== OP_REMOVE
)
1287 LOG_AND_RETURN("invalid %s operation", key
);
1289 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1291 } else if (streq(key
, "GOTO")) {
1292 if (op
== OP_REMOVE
)
1293 LOG_AND_RETURN("invalid %s operation", key
);
1295 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1297 } else if (startswith(key
, "NAME")) {
1298 if (op
== OP_REMOVE
)
1299 LOG_AND_RETURN("invalid %s operation", key
);
1301 if (op
< OP_MATCH_MAX
)
1302 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1304 if (streq(value
, "%k")) {
1305 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1308 if (isempty(value
)) {
1309 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1312 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1314 rule_tmp
.rule
.rule
.can_set_name
= true;
1316 } else if (streq(key
, "SYMLINK")) {
1317 if (op
== OP_REMOVE
)
1318 LOG_AND_RETURN("invalid %s operation", key
);
1320 if (op
< OP_MATCH_MAX
)
1321 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1323 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1324 rule_tmp
.rule
.rule
.can_set_name
= true;
1326 } else if (streq(key
, "OWNER")) {
1330 if (op
== OP_REMOVE
)
1331 LOG_AND_RETURN("invalid %s operation", key
);
1333 uid
= strtoul(value
, &endptr
, 10);
1334 if (endptr
[0] == '\0')
1335 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1336 else if (rules
->resolve_names
> 0 && strchr("$%", value
[0]) == NULL
) {
1337 uid
= add_uid(rules
, value
);
1338 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1339 } else if (rules
->resolve_names
>= 0)
1340 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1342 rule_tmp
.rule
.rule
.can_set_name
= true;
1344 } else if (streq(key
, "GROUP")) {
1348 if (op
== OP_REMOVE
)
1349 LOG_AND_RETURN("invalid %s operation", key
);
1351 gid
= strtoul(value
, &endptr
, 10);
1352 if (endptr
[0] == '\0')
1353 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1354 else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1355 gid
= add_gid(rules
, value
);
1356 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1357 } else if (rules
->resolve_names
>= 0)
1358 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1360 rule_tmp
.rule
.rule
.can_set_name
= true;
1362 } else if (streq(key
, "MODE")) {
1366 if (op
== OP_REMOVE
)
1367 LOG_AND_RETURN("invalid %s operation", key
);
1369 mode
= strtol(value
, &endptr
, 8);
1370 if (endptr
[0] == '\0')
1371 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1373 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1374 rule_tmp
.rule
.rule
.can_set_name
= true;
1376 } else if (streq(key
, "OPTIONS")) {
1379 if (op
== OP_REMOVE
)
1380 LOG_AND_RETURN("invalid %s operation", key
);
1382 pos
= strstr(value
, "link_priority=");
1384 int prio
= atoi(pos
+ STRLEN("link_priority="));
1386 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1389 pos
= strstr(value
, "string_escape=");
1391 pos
+= STRLEN("string_escape=");
1392 if (startswith(pos
, "none"))
1393 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1394 else if (startswith(pos
, "replace"))
1395 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1398 pos
= strstr(value
, "db_persist");
1400 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1402 pos
= strstr(value
, "nowatch");
1406 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1408 pos
= strstr(value
, "watch");
1412 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1416 pos
= strstr(value
, "static_node=");
1418 pos
+= STRLEN("static_node=");
1419 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1420 rule_tmp
.rule
.rule
.has_static_node
= true;
1424 LOG_AND_RETURN("unknown key '%s'", key
);
1427 /* add rule token and sort tokens */
1428 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1429 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1430 LOG_RULE_ERROR("failed to add rule token");
1433 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1434 _cleanup_fclose_
FILE *f
= NULL
;
1435 unsigned int first_token
;
1436 unsigned int filename_off
;
1437 char line
[UTIL_LINE_SIZE
];
1441 f
= fopen(filename
, "re");
1443 if (errno
== ENOENT
)
1449 if (null_or_empty_fd(fileno(f
))) {
1450 log_debug("Skipping empty file: %s", filename
);
1453 log_debug("Reading rules file: %s", filename
);
1455 first_token
= rules
->token_cur
;
1456 filename_off
= rules_add_string(rules
, filename
);
1458 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1462 /* skip whitespace */
1465 while (isspace(key
[0]))
1476 /* continue reading if backslash+newline is found */
1477 while (line
[len
-2] == '\\') {
1478 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1480 if (strlen(&line
[len
-2]) < 2)
1486 if (len
+1 >= sizeof(line
)) {
1487 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1490 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1493 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1494 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1495 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1496 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1499 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1500 if (rules
->tokens
[j
].type
!= TK_RULE
)
1502 if (rules
->tokens
[j
].rule
.label_off
== 0)
1504 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1506 rules
->tokens
[i
].key
.rule_goto
= j
;
1509 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1510 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1516 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1517 struct udev_rules
*rules
;
1518 struct udev_list file_list
;
1519 struct token end_token
;
1523 rules
= new0(struct udev_rules
, 1);
1527 rules
->resolve_names
= resolve_names
;
1528 udev_list_init(udev
, &file_list
, true);
1530 /* init token array and string buffer */
1531 rules
->tokens
= malloc_multiply(PREALLOC_TOKEN
, sizeof(struct token
));
1532 if (rules
->tokens
== NULL
)
1533 return udev_rules_unref(rules
);
1534 rules
->token_max
= PREALLOC_TOKEN
;
1536 rules
->strbuf
= strbuf_new();
1538 return udev_rules_unref(rules
);
1540 udev_rules_check_timestamp(rules
);
1542 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1544 log_error_errno(r
, "failed to enumerate rules files: %m");
1545 return udev_rules_unref(rules
);
1549 * The offset value in the rules strct is limited; add all
1550 * rules file names to the beginning of the string buffer.
1552 STRV_FOREACH(f
, files
)
1553 rules_add_string(rules
, *f
);
1555 STRV_FOREACH(f
, files
)
1556 parse_file(rules
, *f
);
1560 memzero(&end_token
, sizeof(struct token
));
1561 end_token
.type
= TK_END
;
1562 add_token(rules
, &end_token
);
1563 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1564 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1566 /* cleanup temporary strbuf data */
1567 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1568 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1569 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1570 strbuf_complete(rules
->strbuf
);
1572 /* cleanup uid/gid cache */
1573 rules
->uids
= mfree(rules
->uids
);
1574 rules
->uids_cur
= 0;
1575 rules
->uids_max
= 0;
1576 rules
->gids
= mfree(rules
->gids
);
1577 rules
->gids_cur
= 0;
1578 rules
->gids_max
= 0;
1584 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1587 free(rules
->tokens
);
1588 strbuf_cleanup(rules
->strbuf
);
1591 return mfree(rules
);
1594 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1598 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1601 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1602 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1609 switch (token
->key
.glob
) {
1611 match
= (streq(key_value
, val
));
1614 match
= (fnmatch(key_value
, val
, 0) == 0);
1621 s
= rules_str(rules
, token
->key
.value_off
);
1626 next
= strchr(s
, '|');
1628 size_t matchlen
= (size_t)(next
- s
);
1630 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1634 match
= (streq(s
, val
));
1643 char value
[UTIL_PATH_SIZE
];
1645 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1647 while (key_value
!= NULL
) {
1648 pos
= strchr(key_value
, '|');
1653 match
= (fnmatch(key_value
, val
, 0) == 0);
1661 match
= (val
[0] != '\0');
1667 if (match
&& (token
->key
.op
== OP_MATCH
))
1669 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1674 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1676 char nbuf
[UTIL_NAME_SIZE
];
1678 char vbuf
[UTIL_NAME_SIZE
];
1681 name
= rules_str(rules
, cur
->key
.attr_off
);
1682 switch (cur
->key
.attrsubst
) {
1684 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1688 value
= udev_device_get_sysattr_value(dev
, name
);
1693 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1701 /* remove trailing whitespace, if not asked to match for it */
1702 len
= strlen(value
);
1703 if (len
> 0 && isspace(value
[len
-1])) {
1704 const char *key_value
;
1707 key_value
= rules_str(rules
, cur
->key
.value_off
);
1708 klen
= strlen(key_value
);
1709 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1710 if (value
!= vbuf
) {
1711 strscpy(vbuf
, sizeof(vbuf
), value
);
1714 while (len
> 0 && isspace(vbuf
[--len
]))
1719 return match_key(rules
, cur
, value
);
1728 void udev_rules_apply_to_event(struct udev_rules
*rules
,
1729 struct udev_event
*event
,
1730 usec_t timeout_usec
,
1731 usec_t timeout_warn_usec
,
1732 struct udev_list
*properties_list
) {
1735 enum escape_type esc
= ESCAPE_UNSET
;
1739 if (rules
->tokens
== NULL
)
1742 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1743 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1744 udev_device_get_ifindex(event
->dev
) > 0));
1746 /* loop through token list, match, run actions or forward to next rule */
1747 cur
= &rules
->tokens
[0];
1750 dump_token(rules
, cur
);
1751 switch (cur
->type
) {
1755 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1756 if (!can_set_name
&& rule
->rule
.can_set_name
)
1761 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1765 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1769 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1772 case TK_M_DEVLINK
: {
1773 struct udev_list_entry
*list_entry
;
1776 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1777 const char *devlink
;
1779 devlink
= udev_list_entry_get_name(list_entry
) + STRLEN("/dev/");
1780 if (match_key(rules
, cur
, devlink
) == 0) {
1790 if (match_key(rules
, cur
, event
->name
) != 0)
1794 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1797 value
= udev_device_get_property_value(event
->dev
, key_name
);
1799 /* check global properties */
1800 if (!value
&& properties_list
) {
1801 struct udev_list_entry
*list_entry
;
1803 list_entry
= udev_list_get_entry(properties_list
);
1804 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1805 if (list_entry
!= NULL
)
1806 value
= udev_list_entry_get_value(list_entry
);
1811 if (match_key(rules
, cur
, value
))
1816 struct udev_list_entry
*list_entry
;
1819 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1820 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1825 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1826 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1830 case TK_M_SUBSYSTEM
:
1831 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1835 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1839 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1843 char filename
[UTIL_PATH_SIZE
];
1844 _cleanup_free_
char *value
= NULL
;
1847 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1848 sysctl_normalize(filename
);
1849 if (sysctl_read(filename
, &value
) < 0)
1852 len
= strlen(value
);
1853 while (len
> 0 && isspace(value
[--len
]))
1855 if (match_key(rules
, cur
, value
) != 0)
1860 case TK_M_SUBSYSTEMS
:
1866 /* get whole sequence of parent matches */
1868 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1871 /* loop over parents */
1872 event
->dev_parent
= event
->dev
;
1876 /* loop over sequence of parent match keys */
1877 for (key
= cur
; key
< next
; key
++ ) {
1878 dump_token(rules
, key
);
1881 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1884 case TK_M_SUBSYSTEMS
:
1885 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1889 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1893 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1897 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1899 if (match
&& key
->key
.op
== OP_NOMATCH
)
1901 if (!match
&& key
->key
.op
== OP_MATCH
)
1912 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1913 if (event
->dev_parent
== NULL
)
1916 /* move behind our sequence of parent match keys */
1921 char filename
[UTIL_PATH_SIZE
];
1922 struct stat statbuf
;
1925 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1926 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
1927 if (filename
[0] != '/') {
1928 char tmp
[UTIL_PATH_SIZE
];
1930 strscpy(tmp
, sizeof(tmp
), filename
);
1931 strscpyl(filename
, sizeof(filename
),
1932 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
1935 attr_subst_subdir(filename
, sizeof(filename
));
1937 match
= (stat(filename
, &statbuf
) == 0);
1938 if (match
&& cur
->key
.mode
> 0)
1939 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1940 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1942 if (!match
&& cur
->key
.op
== OP_MATCH
)
1946 case TK_M_PROGRAM
: {
1947 char program
[UTIL_PATH_SIZE
];
1948 char result
[UTIL_LINE_SIZE
];
1950 event
->program_result
= mfree(event
->program_result
);
1951 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1952 log_debug("PROGRAM '%s' %s:%u",
1954 rules_str(rules
, rule
->rule
.filename_off
),
1955 rule
->rule
.filename_line
);
1957 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
1958 if (cur
->key
.op
!= OP_NOMATCH
)
1963 delete_trailing_chars(result
, "\n");
1964 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
1965 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1967 log_debug("%i character(s) replaced" , count
);
1969 event
->program_result
= strdup(result
);
1970 if (cur
->key
.op
== OP_NOMATCH
)
1975 case TK_M_IMPORT_FILE
: {
1976 char import
[UTIL_PATH_SIZE
];
1978 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1979 if (import_file_into_properties(event
->dev
, import
) != 0)
1980 if (cur
->key
.op
!= OP_NOMATCH
)
1984 case TK_M_IMPORT_PROG
: {
1985 char import
[UTIL_PATH_SIZE
];
1987 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1988 log_debug("IMPORT '%s' %s:%u",
1990 rules_str(rules
, rule
->rule
.filename_off
),
1991 rule
->rule
.filename_line
);
1993 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
1994 if (cur
->key
.op
!= OP_NOMATCH
)
1998 case TK_M_IMPORT_BUILTIN
: {
1999 char command
[UTIL_PATH_SIZE
];
2001 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2002 /* check if we ran already */
2003 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2004 log_debug("IMPORT builtin skip '%s' %s:%u",
2005 udev_builtin_name(cur
->key
.builtin_cmd
),
2006 rules_str(rules
, rule
->rule
.filename_off
),
2007 rule
->rule
.filename_line
);
2008 /* return the result from earlier run */
2009 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2010 if (cur
->key
.op
!= OP_NOMATCH
)
2015 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2018 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2019 log_debug("IMPORT builtin '%s' %s:%u",
2020 udev_builtin_name(cur
->key
.builtin_cmd
),
2021 rules_str(rules
, rule
->rule
.filename_off
),
2022 rule
->rule
.filename_line
);
2024 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2025 /* remember failure */
2026 log_debug("IMPORT builtin '%s' returned non-zero",
2027 udev_builtin_name(cur
->key
.builtin_cmd
));
2028 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2029 if (cur
->key
.op
!= OP_NOMATCH
)
2034 case TK_M_IMPORT_DB
: {
2035 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2038 value
= udev_device_get_property_value(event
->dev_db
, key
);
2040 udev_device_add_property(event
->dev
, key
, value
);
2042 if (cur
->key
.op
!= OP_NOMATCH
)
2047 case TK_M_IMPORT_CMDLINE
: {
2048 _cleanup_free_
char *value
= NULL
;
2049 bool imported
= false;
2052 key
= rules_str(rules
, cur
->key
.value_off
);
2054 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &value
);
2056 log_debug_errno(r
, "Failed to read %s from /proc/cmdline, ignoring: %m", key
);
2061 udev_device_add_property(event
->dev
, key
, value
);
2063 /* we import simple flags as 'FLAG=1' */
2064 udev_device_add_property(event
->dev
, key
, "1");
2067 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2071 case TK_M_IMPORT_PARENT
: {
2072 char import
[UTIL_PATH_SIZE
];
2074 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2075 if (import_parent_into_properties(event
->dev
, import
) != 0)
2076 if (cur
->key
.op
!= OP_NOMATCH
)
2081 if (match_key(rules
, cur
, event
->program_result
) != 0)
2084 case TK_A_STRING_ESCAPE_NONE
:
2087 case TK_A_STRING_ESCAPE_REPLACE
:
2088 esc
= ESCAPE_REPLACE
;
2090 case TK_A_DB_PERSIST
:
2091 udev_device_set_db_persist(event
->dev
);
2093 case TK_A_INOTIFY_WATCH
:
2094 if (event
->inotify_watch_final
)
2096 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2097 event
->inotify_watch_final
= true;
2098 event
->inotify_watch
= cur
->key
.watch
;
2100 case TK_A_DEVLINK_PRIO
:
2101 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2104 char owner
[UTIL_NAME_SIZE
];
2105 const char *ow
= owner
;
2107 if (event
->owner_final
)
2109 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2110 event
->owner_final
= true;
2111 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2112 event
->owner_set
= true;
2113 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
, USER_CREDS_ALLOW_MISSING
);
2115 log_unknown_owner(r
, "user", owner
);
2118 log_debug("OWNER %u %s:%u",
2120 rules_str(rules
, rule
->rule
.filename_off
),
2121 rule
->rule
.filename_line
);
2125 char group
[UTIL_NAME_SIZE
];
2126 const char *gr
= group
;
2128 if (event
->group_final
)
2130 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2131 event
->group_final
= true;
2132 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2133 event
->group_set
= true;
2134 r
= get_group_creds(&gr
, &event
->gid
, USER_CREDS_ALLOW_MISSING
);
2136 log_unknown_owner(r
, "group", group
);
2139 log_debug("GROUP %u %s:%u",
2141 rules_str(rules
, rule
->rule
.filename_off
),
2142 rule
->rule
.filename_line
);
2146 char mode_str
[UTIL_NAME_SIZE
];
2150 if (event
->mode_final
)
2152 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2153 mode
= strtol(mode_str
, &endptr
, 8);
2154 if (endptr
[0] != '\0') {
2155 log_error("ignoring invalid mode '%s'", mode_str
);
2158 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2159 event
->mode_final
= true;
2160 event
->mode_set
= true;
2162 log_debug("MODE %#o %s:%u",
2164 rules_str(rules
, rule
->rule
.filename_off
),
2165 rule
->rule
.filename_line
);
2169 if (event
->owner_final
)
2171 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2172 event
->owner_final
= true;
2173 event
->owner_set
= true;
2174 event
->uid
= cur
->key
.uid
;
2175 log_debug("OWNER %u %s:%u",
2177 rules_str(rules
, rule
->rule
.filename_off
),
2178 rule
->rule
.filename_line
);
2181 if (event
->group_final
)
2183 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2184 event
->group_final
= true;
2185 event
->group_set
= true;
2186 event
->gid
= cur
->key
.gid
;
2187 log_debug("GROUP %u %s:%u",
2189 rules_str(rules
, rule
->rule
.filename_off
),
2190 rule
->rule
.filename_line
);
2193 if (event
->mode_final
)
2195 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2196 event
->mode_final
= true;
2197 event
->mode_set
= true;
2198 event
->mode
= cur
->key
.mode
;
2199 log_debug("MODE %#o %s:%u",
2201 rules_str(rules
, rule
->rule
.filename_off
),
2202 rule
->rule
.filename_line
);
2204 case TK_A_SECLABEL
: {
2205 char label_str
[UTIL_LINE_SIZE
] = {};
2206 const char *name
, *label
;
2208 name
= rules_str(rules
, cur
->key
.attr_off
);
2209 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2210 if (label_str
[0] != '\0')
2213 label
= rules_str(rules
, cur
->key
.value_off
);
2215 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2216 udev_list_cleanup(&event
->seclabel_list
);
2217 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2218 log_debug("SECLABEL{%s}='%s' %s:%u",
2220 rules_str(rules
, rule
->rule
.filename_off
),
2221 rule
->rule
.filename_line
);
2225 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2226 char *value
= rules_str(rules
, cur
->key
.value_off
);
2227 char value_new
[UTIL_NAME_SIZE
];
2228 const char *value_old
= NULL
;
2230 if (value
[0] == '\0') {
2231 if (cur
->key
.op
== OP_ADD
)
2233 udev_device_add_property(event
->dev
, name
, NULL
);
2237 if (cur
->key
.op
== OP_ADD
)
2238 value_old
= udev_device_get_property_value(event
->dev
, name
);
2240 char temp
[UTIL_NAME_SIZE
];
2242 /* append value separated by space */
2243 udev_event_apply_format(event
, value
, temp
, sizeof(temp
), false);
2244 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2246 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
), false);
2248 udev_device_add_property(event
->dev
, name
, value_new
);
2252 char tag
[UTIL_PATH_SIZE
];
2255 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2256 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2257 udev_device_cleanup_tags_list(event
->dev
);
2258 for (p
= tag
; *p
!= '\0'; p
++) {
2259 if ((*p
>= 'a' && *p
<= 'z') ||
2260 (*p
>= 'A' && *p
<= 'Z') ||
2261 (*p
>= '0' && *p
<= '9') ||
2262 IN_SET(*p
, '-', '_'))
2264 log_error("ignoring invalid tag name '%s'", tag
);
2267 if (cur
->key
.op
== OP_REMOVE
)
2268 udev_device_remove_tag(event
->dev
, tag
);
2270 udev_device_add_tag(event
->dev
, tag
);
2274 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2276 char name_str
[UTIL_PATH_SIZE
];
2279 if (event
->name_final
)
2281 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2282 event
->name_final
= true;
2283 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2284 if (IN_SET(esc
, ESCAPE_UNSET
, ESCAPE_REPLACE
)) {
2285 count
= util_replace_chars(name_str
, "/");
2287 log_debug("%i character(s) replaced", count
);
2289 if (major(udev_device_get_devnum(event
->dev
)) &&
2290 !streq(name_str
, udev_device_get_devnode(event
->dev
) + STRLEN("/dev/"))) {
2291 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2293 rules_str(rules
, rule
->rule
.filename_off
),
2294 rule
->rule
.filename_line
);
2297 if (free_and_strdup(&event
->name
, name_str
) < 0) {
2301 log_debug("NAME '%s' %s:%u",
2303 rules_str(rules
, rule
->rule
.filename_off
),
2304 rule
->rule
.filename_line
);
2307 case TK_A_DEVLINK
: {
2308 char temp
[UTIL_PATH_SIZE
];
2309 char filename
[UTIL_PATH_SIZE
];
2313 if (event
->devlink_final
)
2315 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2317 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2318 event
->devlink_final
= true;
2319 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2320 udev_device_cleanup_devlinks_list(event
->dev
);
2322 /* allow multiple symlinks separated by spaces */
2323 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2324 if (esc
== ESCAPE_UNSET
)
2325 count
= util_replace_chars(temp
, "/ ");
2326 else if (esc
== ESCAPE_REPLACE
)
2327 count
= util_replace_chars(temp
, "/");
2329 log_debug("%i character(s) replaced" , count
);
2331 while (isspace(pos
[0]))
2333 next
= strchr(pos
, ' ');
2334 while (next
!= NULL
) {
2336 log_debug("LINK '%s' %s:%u", pos
,
2337 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2338 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2339 udev_device_add_devlink(event
->dev
, filename
);
2340 while (isspace(next
[1]))
2343 next
= strchr(pos
, ' ');
2345 if (pos
[0] != '\0') {
2346 log_debug("LINK '%s' %s:%u", pos
,
2347 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2348 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2349 udev_device_add_devlink(event
->dev
, filename
);
2354 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2355 char attr
[UTIL_PATH_SIZE
];
2356 char value
[UTIL_NAME_SIZE
];
2357 _cleanup_fclose_
FILE *f
= NULL
;
2359 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2360 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2361 attr_subst_subdir(attr
, sizeof(attr
));
2363 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2364 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2365 rules_str(rules
, rule
->rule
.filename_off
),
2366 rule
->rule
.filename_line
);
2367 f
= fopen(attr
, "we");
2369 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2370 else if (fprintf(f
, "%s", value
) <= 0)
2371 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2375 char filename
[UTIL_PATH_SIZE
];
2376 char value
[UTIL_NAME_SIZE
];
2378 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2379 sysctl_normalize(filename
);
2380 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2381 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2382 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2383 r
= sysctl_write(filename
, value
);
2385 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2388 case TK_A_RUN_BUILTIN
:
2389 case TK_A_RUN_PROGRAM
: {
2390 struct udev_list_entry
*entry
;
2392 if (IN_SET(cur
->key
.op
, OP_ASSIGN
, OP_ASSIGN_FINAL
))
2393 udev_list_cleanup(&event
->run_list
);
2394 log_debug("RUN '%s' %s:%u",
2395 rules_str(rules
, cur
->key
.value_off
),
2396 rules_str(rules
, rule
->rule
.filename_off
),
2397 rule
->rule
.filename_line
);
2398 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2399 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2403 if (cur
->key
.rule_goto
== 0)
2405 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2410 case TK_M_PARENTS_MIN
:
2411 case TK_M_PARENTS_MAX
:
2414 log_error("wrong type %u", cur
->type
);
2421 /* fast-forward to next rule */
2422 cur
= rule
+ rule
->rule
.token_count
;
2426 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2432 _cleanup_strv_free_
char **tags
= NULL
;
2435 _cleanup_free_
char *path
= NULL
;
2438 if (rules
->tokens
== NULL
)
2441 cur
= &rules
->tokens
[0];
2444 switch (cur
->type
) {
2449 /* skip rules without a static_node tag */
2450 if (!rule
->rule
.has_static_node
)
2456 tags
= strv_free(tags
);
2465 mode
= cur
->key
.mode
;
2468 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2473 case TK_A_STATIC_NODE
: {
2474 char device_node
[UTIL_PATH_SIZE
];
2475 char tags_dir
[UTIL_PATH_SIZE
];
2476 char tag_symlink
[UTIL_PATH_SIZE
];
2479 /* we assure, that the permissions tokens are sorted before the static token */
2481 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2484 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2485 if (stat(device_node
, &stats
) != 0)
2487 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2490 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2492 STRV_FOREACH(t
, tags
) {
2493 _cleanup_free_
char *unescaped_filename
= NULL
;
2495 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2496 r
= mkdir_p(tags_dir
, 0755);
2498 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2500 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2502 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2503 r
= symlink(device_node
, tag_symlink
);
2504 if (r
< 0 && errno
!= EEXIST
)
2505 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2506 tag_symlink
, device_node
);
2510 /* don't touch the permissions if only the tags were set */
2511 if (mode
== 0 && uid
== 0 && gid
== 0)
2520 if (mode
!= (stats
.st_mode
& 01777)) {
2521 r
= chmod(device_node
, mode
);
2523 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2526 log_debug("chmod '%s' %#o", device_node
, mode
);
2529 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2530 r
= chown(device_node
, uid
, gid
);
2532 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2533 device_node
, uid
, gid
);
2535 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2538 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2548 /* fast-forward to next rule */
2549 cur
= rule
+ rule
->rule
.token_count
;
2556 fchmod(fileno(f
), 0644);
2557 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2558 unlink_noerrno("/run/udev/static_node-tags");
2559 unlink_noerrno(path
);