2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "path-util.h"
34 #include "conf-files.h"
38 #include "sysctl-util.h"
40 #define PREALLOC_TOKEN 2048
43 unsigned int name_off
;
50 static const char* const rules_dirs
[] = {
53 UDEVLIBEXECDIR
"/rules.d",
61 /* every key in the rules file becomes a token */
63 unsigned int token_cur
;
64 unsigned int token_max
;
66 /* all key strings are copied and de-duplicated in a single continuous string buffer */
67 struct strbuf
*strbuf
;
69 /* during rule parsing, uid/gid lookup results are cached */
71 unsigned int uids_cur
;
72 unsigned int uids_max
;
74 unsigned int gids_cur
;
75 unsigned int gids_max
;
78 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
79 return rules
->strbuf
->buf
+ off
;
82 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
83 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
86 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
100 enum string_glob_type
{
102 GL_PLAIN
, /* no special chars */
103 GL_GLOB
, /* shell globs ?,*,[] */
104 GL_SPLIT
, /* multi-value A|B */
105 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
106 GL_SOMETHING
, /* commonly used "?*" */
109 enum string_subst_type
{
116 /* tokens of a rule are sorted/handled in this order */
121 TK_M_ACTION
, /* val */
122 TK_M_DEVPATH
, /* val */
123 TK_M_KERNEL
, /* val */
124 TK_M_DEVLINK
, /* val */
126 TK_M_ENV
, /* val, attr */
128 TK_M_SUBSYSTEM
, /* val */
129 TK_M_DRIVER
, /* val */
130 TK_M_WAITFOR
, /* val */
131 TK_M_ATTR
, /* val, attr */
132 TK_M_SYSCTL
, /* val, attr */
135 TK_M_KERNELS
, /* val */
136 TK_M_SUBSYSTEMS
, /* val */
137 TK_M_DRIVERS
, /* val */
138 TK_M_ATTRS
, /* val, attr */
142 TK_M_TEST
, /* val, mode_t */
143 TK_M_PROGRAM
, /* val */
144 TK_M_IMPORT_FILE
, /* val */
145 TK_M_IMPORT_PROG
, /* val */
146 TK_M_IMPORT_BUILTIN
, /* val */
147 TK_M_IMPORT_DB
, /* val */
148 TK_M_IMPORT_CMDLINE
, /* val */
149 TK_M_IMPORT_PARENT
, /* val */
150 TK_M_RESULT
, /* val */
153 TK_A_STRING_ESCAPE_NONE
,
154 TK_A_STRING_ESCAPE_REPLACE
,
156 TK_A_INOTIFY_WATCH
, /* int */
157 TK_A_DEVLINK_PRIO
, /* int */
158 TK_A_OWNER
, /* val */
159 TK_A_GROUP
, /* val */
161 TK_A_OWNER_ID
, /* uid_t */
162 TK_A_GROUP_ID
, /* gid_t */
163 TK_A_MODE_ID
, /* mode_t */
165 TK_A_STATIC_NODE
, /* val */
166 TK_A_SECLABEL
, /* val, attr */
167 TK_A_ENV
, /* val, attr */
169 TK_A_DEVLINK
, /* val */
170 TK_A_ATTR
, /* val, attr */
171 TK_A_SYSCTL
, /* val, attr */
172 TK_A_RUN_BUILTIN
, /* val, bool */
173 TK_A_RUN_PROGRAM
, /* val, bool */
174 TK_A_GOTO
, /* size_t */
179 /* we try to pack stuff in a way that we take only 12 bytes per token */
182 unsigned char type
; /* same in rule and key */
184 enum token_type type
:8;
186 bool has_static_node
:1;
187 unsigned int unused
:6;
188 unsigned short token_count
;
189 unsigned int label_off
;
190 unsigned short filename_off
;
191 unsigned short filename_line
;
194 enum token_type type
:8;
195 enum operation_type op
:8;
196 enum string_glob_type glob
:8;
197 enum string_subst_type subst
:4;
198 enum string_subst_type attrsubst
:4;
199 unsigned int value_off
;
201 unsigned int attr_off
;
202 unsigned int rule_goto
;
208 enum udev_builtin_cmd builtin_cmd
;
216 struct udev_rules
*rules
;
218 struct token token
[MAX_TK
];
219 unsigned int token_cur
;
223 static const char *operation_str(enum operation_type type
) {
224 static const char *operation_strs
[] = {
225 [OP_UNSET
] = "UNSET",
226 [OP_MATCH
] = "match",
227 [OP_NOMATCH
] = "nomatch",
228 [OP_MATCH_MAX
] = "MATCH_MAX",
231 [OP_REMOVE
] = "remove",
232 [OP_ASSIGN
] = "assign",
233 [OP_ASSIGN_FINAL
] = "assign-final",
236 return operation_strs
[type
];
239 static const char *string_glob_str(enum string_glob_type type
) {
240 static const char *string_glob_strs
[] = {
241 [GL_UNSET
] = "UNSET",
242 [GL_PLAIN
] = "plain",
244 [GL_SPLIT
] = "split",
245 [GL_SPLIT_GLOB
] = "split-glob",
246 [GL_SOMETHING
] = "split-glob",
249 return string_glob_strs
[type
];
252 static const char *token_str(enum token_type type
) {
253 static const char *token_strs
[] = {
254 [TK_UNSET
] = "UNSET",
257 [TK_M_ACTION
] = "M ACTION",
258 [TK_M_DEVPATH
] = "M DEVPATH",
259 [TK_M_KERNEL
] = "M KERNEL",
260 [TK_M_DEVLINK
] = "M DEVLINK",
261 [TK_M_NAME
] = "M NAME",
262 [TK_M_ENV
] = "M ENV",
263 [TK_M_TAG
] = "M TAG",
264 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
265 [TK_M_DRIVER
] = "M DRIVER",
266 [TK_M_WAITFOR
] = "M WAITFOR",
267 [TK_M_ATTR
] = "M ATTR",
268 [TK_M_SYSCTL
] = "M SYSCTL",
270 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
271 [TK_M_KERNELS
] = "M KERNELS",
272 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
273 [TK_M_DRIVERS
] = "M DRIVERS",
274 [TK_M_ATTRS
] = "M ATTRS",
275 [TK_M_TAGS
] = "M TAGS",
276 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
278 [TK_M_TEST
] = "M TEST",
279 [TK_M_PROGRAM
] = "M PROGRAM",
280 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
281 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
282 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
283 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
284 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
285 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
286 [TK_M_RESULT
] = "M RESULT",
287 [TK_M_MAX
] = "M MAX",
289 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
290 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
291 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
292 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
293 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
294 [TK_A_OWNER
] = "A OWNER",
295 [TK_A_GROUP
] = "A GROUP",
296 [TK_A_MODE
] = "A MODE",
297 [TK_A_OWNER_ID
] = "A OWNER_ID",
298 [TK_A_GROUP_ID
] = "A GROUP_ID",
299 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
300 [TK_A_SECLABEL
] = "A SECLABEL",
301 [TK_A_MODE_ID
] = "A MODE_ID",
302 [TK_A_ENV
] = "A ENV",
303 [TK_A_TAG
] = "A ENV",
304 [TK_A_NAME
] = "A NAME",
305 [TK_A_DEVLINK
] = "A DEVLINK",
306 [TK_A_ATTR
] = "A ATTR",
307 [TK_A_SYSCTL
] = "A SYSCTL",
308 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
309 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
310 [TK_A_GOTO
] = "A GOTO",
315 return token_strs
[type
];
318 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
319 enum token_type type
= token
->type
;
320 enum operation_type op
= token
->key
.op
;
321 enum string_glob_type glob
= token
->key
.glob
;
322 const char *value
= str(rules
, token
->key
.value_off
);
323 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
328 const char *tks_ptr
= (char *)rules
->tokens
;
329 const char *tk_ptr
= (char *)token
;
330 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
332 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
333 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
334 idx
, token
->rule
.token_count
,
335 &rules
->buf
[token
->rule
.label_off
]);
347 case TK_M_SUBSYSTEMS
:
351 case TK_M_IMPORT_FILE
:
352 case TK_M_IMPORT_PROG
:
354 case TK_M_IMPORT_CMDLINE
:
355 case TK_M_IMPORT_PARENT
:
362 case TK_A_RUN_BUILTIN
:
363 case TK_A_RUN_PROGRAM
:
364 log_debug("%s %s '%s'(%s)",
365 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
367 case TK_M_IMPORT_BUILTIN
:
368 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
377 log_debug("%s %s '%s' '%s'(%s)",
378 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
382 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
384 case TK_A_STRING_ESCAPE_NONE
:
385 case TK_A_STRING_ESCAPE_REPLACE
:
386 case TK_A_DB_PERSIST
:
387 log_debug("%s", token_str(type
));
390 log_debug("%s %s '%s'(%s) %#o",
391 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
393 case TK_A_INOTIFY_WATCH
:
394 log_debug("%s %u", token_str(type
), token
->key
.watch
);
396 case TK_A_DEVLINK_PRIO
:
397 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
400 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
403 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
406 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
408 case TK_A_STATIC_NODE
:
409 log_debug("%s '%s'", token_str(type
), value
);
412 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
415 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
418 log_debug("* %s", token_str(type
));
420 case TK_M_PARENTS_MIN
:
421 case TK_M_PARENTS_MAX
:
424 log_debug("unknown type %u", type
);
429 static void dump_rules(struct udev_rules
*rules
) {
432 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
434 rules
->token_cur
* sizeof(struct token
),
437 for (i
= 0; i
< rules
->token_cur
; i
++)
438 dump_token(rules
, &rules
->tokens
[i
]);
441 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
442 static inline void dump_rules(struct udev_rules
*rules
) {}
445 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
446 /* grow buffer if needed */
447 if (rules
->token_cur
+1 >= rules
->token_max
) {
448 struct token
*tokens
;
451 /* double the buffer size */
452 add
= rules
->token_max
;
456 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
459 rules
->tokens
= tokens
;
460 rules
->token_max
+= add
;
462 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
467 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
473 /* lookup, if we know it already */
474 for (i
= 0; i
< rules
->uids_cur
; i
++) {
475 off
= rules
->uids
[i
].name_off
;
476 if (streq(rules_str(rules
, off
), owner
)) {
477 uid
= rules
->uids
[i
].uid
;
481 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
483 if (r
== -ENOENT
|| r
== -ESRCH
)
484 log_error("specified user '%s' unknown", owner
);
486 log_error_errno(r
, "error resolving user '%s': %m", owner
);
489 /* grow buffer if needed */
490 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
491 struct uid_gid
*uids
;
494 /* double the buffer size */
495 add
= rules
->uids_max
;
499 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
503 rules
->uids_max
+= add
;
505 rules
->uids
[rules
->uids_cur
].uid
= uid
;
506 off
= rules_add_string(rules
, owner
);
509 rules
->uids
[rules
->uids_cur
].name_off
= off
;
514 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
520 /* lookup, if we know it already */
521 for (i
= 0; i
< rules
->gids_cur
; i
++) {
522 off
= rules
->gids
[i
].name_off
;
523 if (streq(rules_str(rules
, off
), group
)) {
524 gid
= rules
->gids
[i
].gid
;
528 r
= get_group_creds(&group
, &gid
);
530 if (r
== -ENOENT
|| r
== -ESRCH
)
531 log_error("specified group '%s' unknown", group
);
533 log_error_errno(r
, "error resolving group '%s': %m", group
);
536 /* grow buffer if needed */
537 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
538 struct uid_gid
*gids
;
541 /* double the buffer size */
542 add
= rules
->gids_max
;
546 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
550 rules
->gids_max
+= add
;
552 rules
->gids
[rules
->gids_cur
].gid
= gid
;
553 off
= rules_add_string(rules
, group
);
556 rules
->gids
[rules
->gids_cur
].name_off
= off
;
561 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
568 while (isspace(key
[0]))
571 /* comment or empty line */
572 if (key
[0] == '#' || key
[0] == '\0')
575 /* split key/value */
576 val
= strchr(key
, '=');
583 while (isspace(val
[0]))
590 while (isspace(key
[len
-1]))
594 /* terminate value */
598 while (isspace(val
[len
-1]))
606 if (val
[0] == '"' || val
[0] == '\'') {
607 if (val
[len
-1] != val
[0]) {
608 log_debug("inconsistent quoting: '%s', skip", line
);
615 udev_device_add_property(dev
, key
, val
);
620 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
622 char line
[UTIL_LINE_SIZE
];
624 f
= fopen(filename
, "re");
627 while (fgets(line
, sizeof(line
), f
) != NULL
)
628 import_property_from_string(dev
, line
);
633 static int import_program_into_properties(struct udev_event
*event
,
635 usec_t timeout_warn_usec
,
636 const char *program
) {
637 char result
[UTIL_LINE_SIZE
];
641 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
646 while (line
!= NULL
) {
649 pos
= strchr(line
, '\n');
654 import_property_from_string(event
->dev
, line
);
660 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
661 struct udev_device
*dev_parent
;
662 struct udev_list_entry
*list_entry
;
667 dev_parent
= udev_device_get_parent(dev
);
668 if (dev_parent
== NULL
)
671 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
672 const char *key
= udev_list_entry_get_name(list_entry
);
673 const char *val
= udev_list_entry_get_value(list_entry
);
675 if (fnmatch(filter
, key
, 0) == 0) {
676 udev_device_add_property(dev
, key
, val
);
682 static int attr_subst_subdir(char *attr
, size_t len
) {
685 if (strstr(attr
, "/*/")) {
687 char dirname
[UTIL_PATH_SIZE
];
691 strscpy(dirname
, sizeof(dirname
), attr
);
692 pos
= strstr(dirname
, "/*/");
697 dir
= opendir(dirname
);
701 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
704 if (dent
->d_name
[0] == '.')
706 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
707 if (stat(attr
, &stats
) == 0) {
719 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
724 if (linepos
== NULL
|| linepos
[0] == '\0')
727 /* skip whitespace */
728 while (isspace(linepos
[0]) || linepos
[0] == ',')
732 if (linepos
[0] == '\0')
738 if (linepos
[0] == '\0')
740 if (isspace(linepos
[0]))
742 if (linepos
[0] == '=')
744 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
745 if (linepos
[1] == '=')
749 /* remember end of key */
752 /* skip whitespace after key */
753 while (isspace(linepos
[0]))
755 if (linepos
[0] == '\0')
758 /* get operation type */
759 if (linepos
[0] == '=' && linepos
[1] == '=') {
762 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
765 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
768 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
771 } else if (linepos
[0] == '=') {
774 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
775 *op
= OP_ASSIGN_FINAL
;
783 /* skip whitespace after operator */
784 while (isspace(linepos
[0]))
786 if (linepos
[0] == '\0')
790 if (linepos
[0] == '"')
797 temp
= strchr(linepos
, '"');
803 /* move line to next key */
808 /* extract possible KEY{attr} */
809 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
813 attr
= strchr(str
, '{');
816 pos
= strchr(attr
, '}');
818 log_error("missing closing brace for format");
827 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
828 enum operation_type op
,
829 const char *value
, const void *data
) {
830 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
831 const char *attr
= NULL
;
833 memzero(token
, sizeof(struct token
));
845 case TK_M_SUBSYSTEMS
:
849 case TK_M_IMPORT_FILE
:
850 case TK_M_IMPORT_PROG
:
852 case TK_M_IMPORT_CMDLINE
:
853 case TK_M_IMPORT_PARENT
:
863 case TK_A_STATIC_NODE
:
864 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
866 case TK_M_IMPORT_BUILTIN
:
867 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
868 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
879 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
880 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
883 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
885 token
->key
.mode
= *(mode_t
*)data
;
887 case TK_A_STRING_ESCAPE_NONE
:
888 case TK_A_STRING_ESCAPE_REPLACE
:
889 case TK_A_DB_PERSIST
:
891 case TK_A_RUN_BUILTIN
:
892 case TK_A_RUN_PROGRAM
:
893 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
894 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
896 case TK_A_INOTIFY_WATCH
:
897 case TK_A_DEVLINK_PRIO
:
898 token
->key
.devlink_prio
= *(int *)data
;
901 token
->key
.uid
= *(uid_t
*)data
;
904 token
->key
.gid
= *(gid_t
*)data
;
907 token
->key
.mode
= *(mode_t
*)data
;
910 case TK_M_PARENTS_MIN
:
911 case TK_M_PARENTS_MAX
:
915 log_error("wrong type %u", type
);
919 if (value
!= NULL
&& type
< TK_M_MAX
) {
920 /* check if we need to split or call fnmatch() while matching rules */
921 enum string_glob_type glob
;
925 has_split
= (strchr(value
, '|') != NULL
);
926 has_glob
= string_is_glob(value
);
927 if (has_split
&& has_glob
) {
928 glob
= GL_SPLIT_GLOB
;
929 } else if (has_split
) {
931 } else if (has_glob
) {
932 if (streq(value
, "?*"))
939 token
->key
.glob
= glob
;
942 if (value
!= NULL
&& type
> TK_M_MAX
) {
943 /* check if assigned value has substitution chars */
945 token
->key
.subst
= SB_SUBSYS
;
946 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
947 token
->key
.subst
= SB_FORMAT
;
949 token
->key
.subst
= SB_NONE
;
953 /* check if property/attribute name has substitution chars */
955 token
->key
.attrsubst
= SB_SUBSYS
;
956 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
957 token
->key
.attrsubst
= SB_FORMAT
;
959 token
->key
.attrsubst
= SB_NONE
;
962 token
->key
.type
= type
;
964 rule_tmp
->token_cur
++;
965 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
966 log_error("temporary rule array too small");
972 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
974 unsigned int start
= 0;
975 unsigned int end
= rule_tmp
->token_cur
;
977 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
978 enum token_type next_val
= TK_UNSET
;
979 unsigned int next_idx
= 0;
982 /* find smallest value */
983 for (j
= start
; j
< end
; j
++) {
984 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
986 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
987 next_val
= rule_tmp
->token
[j
].type
;
992 /* add token and mark done */
993 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
995 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
998 if (next_idx
== start
)
1000 if (next_idx
+1 == end
)
1006 static int add_rule(struct udev_rules
*rules
, char *line
,
1007 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1010 struct rule_tmp rule_tmp
= {
1012 .rule
.type
= TK_RULE
,
1015 /* the offset in the rule is limited to unsigned short */
1016 if (filename_off
< USHRT_MAX
)
1017 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1018 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1024 enum operation_type op
;
1026 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1027 /* Avoid erroring on trailing whitespace. This is probably rare
1028 * so save the work for the error case instead of always trying
1029 * to strip the trailing whitespace with strstrip(). */
1030 while (isblank(*linepos
))
1033 /* If we aren't at the end of the line, this is a parsing error.
1034 * Make a best effort to describe where the problem is. */
1035 if (!strchr(NEWLINE
, *linepos
)) {
1036 char buf
[2] = {*linepos
};
1037 _cleanup_free_
char *tmp
;
1040 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1041 filename
, lineno
, linepos
- line
+ 1, tmp
);
1042 if (*linepos
== '#')
1043 log_error("hint: comments can only start at beginning of line");
1048 if (streq(key
, "ACTION")) {
1049 if (op
> OP_MATCH_MAX
) {
1050 log_error("invalid ACTION operation");
1053 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1057 if (streq(key
, "DEVPATH")) {
1058 if (op
> OP_MATCH_MAX
) {
1059 log_error("invalid DEVPATH operation");
1062 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1066 if (streq(key
, "KERNEL")) {
1067 if (op
> OP_MATCH_MAX
) {
1068 log_error("invalid KERNEL operation");
1071 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1075 if (streq(key
, "SUBSYSTEM")) {
1076 if (op
> OP_MATCH_MAX
) {
1077 log_error("invalid SUBSYSTEM operation");
1080 /* bus, class, subsystem events should all be the same */
1081 if (streq(value
, "subsystem") ||
1082 streq(value
, "bus") ||
1083 streq(value
, "class")) {
1084 if (streq(value
, "bus") || streq(value
, "class"))
1085 log_error("'%s' must be specified as 'subsystem' "
1086 "please fix it in %s:%u", value
, filename
, lineno
);
1087 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1089 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1093 if (streq(key
, "DRIVER")) {
1094 if (op
> OP_MATCH_MAX
) {
1095 log_error("invalid DRIVER operation");
1098 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1102 if (startswith(key
, "ATTR{")) {
1103 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1105 log_error("error parsing ATTR attribute");
1108 if (op
== OP_REMOVE
) {
1109 log_error("invalid ATTR operation");
1112 if (op
< OP_MATCH_MAX
)
1113 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1115 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1119 if (startswith(key
, "SYSCTL{")) {
1120 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SYSCTL"));
1122 log_error("error parsing SYSCTL attribute");
1125 if (op
== OP_REMOVE
) {
1126 log_error("invalid SYSCTL operation");
1129 if (op
< OP_MATCH_MAX
)
1130 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1132 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1136 if (startswith(key
, "SECLABEL{")) {
1137 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1139 log_error("error parsing SECLABEL attribute");
1142 if (op
== OP_REMOVE
) {
1143 log_error("invalid SECLABEL operation");
1147 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1151 if (streq(key
, "KERNELS")) {
1152 if (op
> OP_MATCH_MAX
) {
1153 log_error("invalid KERNELS operation");
1156 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1160 if (streq(key
, "SUBSYSTEMS")) {
1161 if (op
> OP_MATCH_MAX
) {
1162 log_error("invalid SUBSYSTEMS operation");
1165 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1169 if (streq(key
, "DRIVERS")) {
1170 if (op
> OP_MATCH_MAX
) {
1171 log_error("invalid DRIVERS operation");
1174 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1178 if (startswith(key
, "ATTRS{")) {
1179 if (op
> OP_MATCH_MAX
) {
1180 log_error("invalid ATTRS operation");
1183 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1185 log_error("error parsing ATTRS attribute");
1188 if (startswith(attr
, "device/"))
1189 log_error("the 'device' link may not be available in a future kernel, "
1190 "please fix it in %s:%u", filename
, lineno
);
1191 else if (strstr(attr
, "../") != NULL
)
1192 log_error("do not reference parent sysfs directories directly, "
1193 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1194 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1198 if (streq(key
, "TAGS")) {
1199 if (op
> OP_MATCH_MAX
) {
1200 log_error("invalid TAGS operation");
1203 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1207 if (startswith(key
, "ENV{")) {
1208 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1210 log_error("error parsing ENV attribute");
1213 if (op
== OP_REMOVE
) {
1214 log_error("invalid ENV operation");
1217 if (op
< OP_MATCH_MAX
) {
1218 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1221 static const char *blacklist
[] = {
1236 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1237 if (!streq(attr
, blacklist
[i
]))
1239 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1242 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1248 if (streq(key
, "TAG")) {
1249 if (op
< OP_MATCH_MAX
)
1250 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1252 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1256 if (streq(key
, "PROGRAM")) {
1257 if (op
== OP_REMOVE
) {
1258 log_error("invalid PROGRAM operation");
1261 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1265 if (streq(key
, "RESULT")) {
1266 if (op
> OP_MATCH_MAX
) {
1267 log_error("invalid RESULT operation");
1270 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1274 if (startswith(key
, "IMPORT")) {
1275 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1277 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1280 if (op
== OP_REMOVE
) {
1281 log_error("invalid IMPORT operation");
1284 if (streq(attr
, "program")) {
1285 /* find known built-in command */
1286 if (value
[0] != '/') {
1287 enum udev_builtin_cmd cmd
;
1289 cmd
= udev_builtin_lookup(value
);
1290 if (cmd
< UDEV_BUILTIN_MAX
) {
1291 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1292 value
, filename
, lineno
);
1293 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1297 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1298 } else if (streq(attr
, "builtin")) {
1299 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1301 if (cmd
< UDEV_BUILTIN_MAX
)
1302 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1304 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1305 } else if (streq(attr
, "file")) {
1306 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1307 } else if (streq(attr
, "db")) {
1308 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1309 } else if (streq(attr
, "cmdline")) {
1310 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1311 } else if (streq(attr
, "parent")) {
1312 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1314 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1318 if (startswith(key
, "TEST")) {
1321 if (op
> OP_MATCH_MAX
) {
1322 log_error("invalid TEST operation");
1325 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1327 mode
= strtol(attr
, NULL
, 8);
1328 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1330 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1335 if (startswith(key
, "RUN")) {
1336 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1339 if (op
== OP_REMOVE
) {
1340 log_error("invalid RUN operation");
1344 if (streq(attr
, "builtin")) {
1345 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1347 if (cmd
< UDEV_BUILTIN_MAX
)
1348 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1350 log_error("RUN{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1351 } else if (streq(attr
, "program")) {
1352 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1354 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1356 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1362 if (streq(key
, "LABEL")) {
1363 if (op
== OP_REMOVE
) {
1364 log_error("invalid LABEL operation");
1367 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1371 if (streq(key
, "GOTO")) {
1372 if (op
== OP_REMOVE
) {
1373 log_error("invalid GOTO operation");
1376 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1380 if (startswith(key
, "NAME")) {
1381 if (op
== OP_REMOVE
) {
1382 log_error("invalid NAME operation");
1385 if (op
< OP_MATCH_MAX
) {
1386 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1388 if (streq(value
, "%k")) {
1389 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1390 "please remove it from %s:%u\n", filename
, lineno
);
1393 if (value
[0] == '\0') {
1394 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1395 "please remove it from %s:%u\n", filename
, lineno
);
1398 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1400 rule_tmp
.rule
.rule
.can_set_name
= true;
1404 if (streq(key
, "SYMLINK")) {
1405 if (op
== OP_REMOVE
) {
1406 log_error("invalid SYMLINK operation");
1409 if (op
< OP_MATCH_MAX
)
1410 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1412 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1413 rule_tmp
.rule
.rule
.can_set_name
= true;
1417 if (streq(key
, "OWNER")) {
1421 if (op
== OP_REMOVE
) {
1422 log_error("invalid OWNER operation");
1426 uid
= strtoul(value
, &endptr
, 10);
1427 if (endptr
[0] == '\0') {
1428 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1429 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1430 uid
= add_uid(rules
, value
);
1431 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1432 } else if (rules
->resolve_names
>= 0) {
1433 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1435 rule_tmp
.rule
.rule
.can_set_name
= true;
1439 if (streq(key
, "GROUP")) {
1443 if (op
== OP_REMOVE
) {
1444 log_error("invalid GROUP operation");
1448 gid
= strtoul(value
, &endptr
, 10);
1449 if (endptr
[0] == '\0') {
1450 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1451 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1452 gid
= add_gid(rules
, value
);
1453 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1454 } else if (rules
->resolve_names
>= 0) {
1455 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1457 rule_tmp
.rule
.rule
.can_set_name
= true;
1461 if (streq(key
, "MODE")) {
1465 if (op
== OP_REMOVE
) {
1466 log_error("invalid MODE operation");
1470 mode
= strtol(value
, &endptr
, 8);
1471 if (endptr
[0] == '\0')
1472 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1474 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1475 rule_tmp
.rule
.rule
.can_set_name
= true;
1479 if (streq(key
, "OPTIONS")) {
1482 if (op
== OP_REMOVE
) {
1483 log_error("invalid OPTIONS operation");
1487 pos
= strstr(value
, "link_priority=");
1489 int prio
= atoi(&pos
[strlen("link_priority=")]);
1491 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1494 pos
= strstr(value
, "string_escape=");
1496 pos
= &pos
[strlen("string_escape=")];
1497 if (startswith(pos
, "none"))
1498 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1499 else if (startswith(pos
, "replace"))
1500 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1503 pos
= strstr(value
, "db_persist");
1505 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1507 pos
= strstr(value
, "nowatch");
1511 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1513 pos
= strstr(value
, "watch");
1517 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1521 pos
= strstr(value
, "static_node=");
1523 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1524 rule_tmp
.rule
.rule
.has_static_node
= true;
1530 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1534 /* add rule token */
1535 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1536 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1539 /* add tokens to list, sorted by type */
1540 if (sort_token(rules
, &rule_tmp
) != 0)
1545 log_error("invalid rule '%s:%u'", filename
, lineno
);
1549 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1550 _cleanup_fclose_
FILE *f
= NULL
;
1551 unsigned int first_token
;
1552 unsigned int filename_off
;
1553 char line
[UTIL_LINE_SIZE
];
1557 f
= fopen(filename
, "re");
1559 if (errno
== ENOENT
)
1565 if (null_or_empty_fd(fileno(f
))) {
1566 log_debug("Skipping empty file: %s", filename
);
1569 log_debug("Reading rules file: %s", filename
);
1571 first_token
= rules
->token_cur
;
1572 filename_off
= rules_add_string(rules
, filename
);
1574 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1578 /* skip whitespace */
1581 while (isspace(key
[0]))
1592 /* continue reading if backslash+newline is found */
1593 while (line
[len
-2] == '\\') {
1594 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1596 if (strlen(&line
[len
-2]) < 2)
1602 if (len
+1 >= sizeof(line
)) {
1603 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1606 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1609 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1610 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1611 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1612 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1615 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1616 if (rules
->tokens
[j
].type
!= TK_RULE
)
1618 if (rules
->tokens
[j
].rule
.label_off
== 0)
1620 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1622 rules
->tokens
[i
].key
.rule_goto
= j
;
1625 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1626 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1632 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1633 struct udev_rules
*rules
;
1634 struct udev_list file_list
;
1635 struct token end_token
;
1639 rules
= new0(struct udev_rules
, 1);
1643 rules
->resolve_names
= resolve_names
;
1644 udev_list_init(udev
, &file_list
, true);
1646 /* init token array and string buffer */
1647 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1648 if (rules
->tokens
== NULL
)
1649 return udev_rules_unref(rules
);
1650 rules
->token_max
= PREALLOC_TOKEN
;
1652 rules
->strbuf
= strbuf_new();
1654 return udev_rules_unref(rules
);
1656 udev_rules_check_timestamp(rules
);
1658 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1660 log_error_errno(r
, "failed to enumerate rules files: %m");
1661 return udev_rules_unref(rules
);
1665 * The offset value in the rules strct is limited; add all
1666 * rules file names to the beginning of the string buffer.
1668 STRV_FOREACH(f
, files
)
1669 rules_add_string(rules
, *f
);
1671 STRV_FOREACH(f
, files
)
1672 parse_file(rules
, *f
);
1676 memzero(&end_token
, sizeof(struct token
));
1677 end_token
.type
= TK_END
;
1678 add_token(rules
, &end_token
);
1679 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1680 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1682 /* cleanup temporary strbuf data */
1683 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1684 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1685 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1686 strbuf_complete(rules
->strbuf
);
1688 /* cleanup uid/gid cache */
1691 rules
->uids_cur
= 0;
1692 rules
->uids_max
= 0;
1695 rules
->gids_cur
= 0;
1696 rules
->gids_max
= 0;
1702 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1705 free(rules
->tokens
);
1706 strbuf_cleanup(rules
->strbuf
);
1713 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1717 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1720 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1721 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1728 switch (token
->key
.glob
) {
1730 match
= (streq(key_value
, val
));
1733 match
= (fnmatch(key_value
, val
, 0) == 0);
1740 s
= rules_str(rules
, token
->key
.value_off
);
1745 next
= strchr(s
, '|');
1747 size_t matchlen
= (size_t)(next
- s
);
1749 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1753 match
= (streq(s
, val
));
1762 char value
[UTIL_PATH_SIZE
];
1764 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1766 while (key_value
!= NULL
) {
1767 pos
= strchr(key_value
, '|');
1772 match
= (fnmatch(key_value
, val
, 0) == 0);
1780 match
= (val
[0] != '\0');
1786 if (match
&& (token
->key
.op
== OP_MATCH
))
1788 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1793 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1795 char nbuf
[UTIL_NAME_SIZE
];
1797 char vbuf
[UTIL_NAME_SIZE
];
1800 name
= rules_str(rules
, cur
->key
.attr_off
);
1801 switch (cur
->key
.attrsubst
) {
1803 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1807 value
= udev_device_get_sysattr_value(dev
, name
);
1812 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1820 /* remove trailing whitespace, if not asked to match for it */
1821 len
= strlen(value
);
1822 if (len
> 0 && isspace(value
[len
-1])) {
1823 const char *key_value
;
1826 key_value
= rules_str(rules
, cur
->key
.value_off
);
1827 klen
= strlen(key_value
);
1828 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1829 if (value
!= vbuf
) {
1830 strscpy(vbuf
, sizeof(vbuf
), value
);
1833 while (len
> 0 && isspace(vbuf
[--len
]))
1838 return match_key(rules
, cur
, value
);
1847 int udev_rules_apply_to_event(struct udev_rules
*rules
,
1848 struct udev_event
*event
,
1849 usec_t timeout_usec
,
1850 usec_t timeout_warn_usec
,
1851 struct udev_list
*properties_list
) {
1854 enum escape_type esc
= ESCAPE_UNSET
;
1857 if (rules
->tokens
== NULL
)
1860 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1861 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1862 udev_device_get_ifindex(event
->dev
) > 0));
1864 /* loop through token list, match, run actions or forward to next rule */
1865 cur
= &rules
->tokens
[0];
1868 dump_token(rules
, cur
);
1869 switch (cur
->type
) {
1873 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1874 if (!can_set_name
&& rule
->rule
.can_set_name
)
1879 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1883 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1887 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1890 case TK_M_DEVLINK
: {
1891 struct udev_list_entry
*list_entry
;
1894 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1895 const char *devlink
;
1897 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1898 if (match_key(rules
, cur
, devlink
) == 0) {
1908 if (match_key(rules
, cur
, event
->name
) != 0)
1912 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1915 value
= udev_device_get_property_value(event
->dev
, key_name
);
1917 /* check global properties */
1918 if (!value
&& properties_list
) {
1919 struct udev_list_entry
*list_entry
;
1921 list_entry
= udev_list_get_entry(properties_list
);
1922 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1923 if (list_entry
!= NULL
)
1924 value
= udev_list_entry_get_value(list_entry
);
1929 if (match_key(rules
, cur
, value
))
1934 struct udev_list_entry
*list_entry
;
1937 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1938 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1943 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
1947 case TK_M_SUBSYSTEM
:
1948 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1952 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1956 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1960 char filename
[UTIL_PATH_SIZE
];
1961 _cleanup_free_
char *value
= NULL
;
1964 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
1965 sysctl_normalize(filename
);
1966 if (sysctl_read(filename
, &value
) < 0)
1969 len
= strlen(value
);
1970 while (len
> 0 && isspace(value
[--len
]))
1972 if (match_key(rules
, cur
, value
) != 0)
1977 case TK_M_SUBSYSTEMS
:
1983 /* get whole sequence of parent matches */
1985 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1988 /* loop over parents */
1989 event
->dev_parent
= event
->dev
;
1993 /* loop over sequence of parent match keys */
1994 for (key
= cur
; key
< next
; key
++ ) {
1995 dump_token(rules
, key
);
1998 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2001 case TK_M_SUBSYSTEMS
:
2002 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2006 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2010 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2014 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
2016 if (match
&& key
->key
.op
== OP_NOMATCH
)
2018 if (!match
&& key
->key
.op
== OP_MATCH
)
2029 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2030 if (event
->dev_parent
== NULL
)
2033 /* move behind our sequence of parent match keys */
2038 char filename
[UTIL_PATH_SIZE
];
2039 struct stat statbuf
;
2042 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2043 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2044 if (filename
[0] != '/') {
2045 char tmp
[UTIL_PATH_SIZE
];
2047 strscpy(tmp
, sizeof(tmp
), filename
);
2048 strscpyl(filename
, sizeof(filename
),
2049 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2052 attr_subst_subdir(filename
, sizeof(filename
));
2054 match
= (stat(filename
, &statbuf
) == 0);
2055 if (match
&& cur
->key
.mode
> 0)
2056 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2057 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2059 if (!match
&& cur
->key
.op
== OP_MATCH
)
2063 case TK_M_PROGRAM
: {
2064 char program
[UTIL_PATH_SIZE
];
2065 char result
[UTIL_LINE_SIZE
];
2067 free(event
->program_result
);
2068 event
->program_result
= NULL
;
2069 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2070 log_debug("PROGRAM '%s' %s:%u",
2072 rules_str(rules
, rule
->rule
.filename_off
),
2073 rule
->rule
.filename_line
);
2075 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
2076 if (cur
->key
.op
!= OP_NOMATCH
)
2081 util_remove_trailing_chars(result
, '\n');
2082 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2083 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2085 log_debug("%i character(s) replaced" , count
);
2087 event
->program_result
= strdup(result
);
2088 if (cur
->key
.op
== OP_NOMATCH
)
2093 case TK_M_IMPORT_FILE
: {
2094 char import
[UTIL_PATH_SIZE
];
2096 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2097 if (import_file_into_properties(event
->dev
, import
) != 0)
2098 if (cur
->key
.op
!= OP_NOMATCH
)
2102 case TK_M_IMPORT_PROG
: {
2103 char import
[UTIL_PATH_SIZE
];
2105 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2106 log_debug("IMPORT '%s' %s:%u",
2108 rules_str(rules
, rule
->rule
.filename_off
),
2109 rule
->rule
.filename_line
);
2111 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
2112 if (cur
->key
.op
!= OP_NOMATCH
)
2116 case TK_M_IMPORT_BUILTIN
: {
2117 char command
[UTIL_PATH_SIZE
];
2119 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2120 /* check if we ran already */
2121 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2122 log_debug("IMPORT builtin skip '%s' %s:%u",
2123 udev_builtin_name(cur
->key
.builtin_cmd
),
2124 rules_str(rules
, rule
->rule
.filename_off
),
2125 rule
->rule
.filename_line
);
2126 /* return the result from earlier run */
2127 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2128 if (cur
->key
.op
!= OP_NOMATCH
)
2133 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2136 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2137 log_debug("IMPORT builtin '%s' %s:%u",
2138 udev_builtin_name(cur
->key
.builtin_cmd
),
2139 rules_str(rules
, rule
->rule
.filename_off
),
2140 rule
->rule
.filename_line
);
2142 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2143 /* remember failure */
2144 log_debug("IMPORT builtin '%s' returned non-zero",
2145 udev_builtin_name(cur
->key
.builtin_cmd
));
2146 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2147 if (cur
->key
.op
!= OP_NOMATCH
)
2152 case TK_M_IMPORT_DB
: {
2153 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2156 value
= udev_device_get_property_value(event
->dev_db
, key
);
2158 udev_device_add_property(event
->dev
, key
, value
);
2160 if (cur
->key
.op
!= OP_NOMATCH
)
2165 case TK_M_IMPORT_CMDLINE
: {
2167 bool imported
= false;
2169 f
= fopen("/proc/cmdline", "re");
2173 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2174 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2177 pos
= strstr(cmdline
, key
);
2180 if (pos
[0] == '\0' || isspace(pos
[0])) {
2181 /* we import simple flags as 'FLAG=1' */
2182 udev_device_add_property(event
->dev
, key
, "1");
2184 } else if (pos
[0] == '=') {
2189 while (pos
[0] != '\0' && !isspace(pos
[0]))
2192 udev_device_add_property(event
->dev
, key
, value
);
2199 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2203 case TK_M_IMPORT_PARENT
: {
2204 char import
[UTIL_PATH_SIZE
];
2206 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2207 if (import_parent_into_properties(event
->dev
, import
) != 0)
2208 if (cur
->key
.op
!= OP_NOMATCH
)
2213 if (match_key(rules
, cur
, event
->program_result
) != 0)
2216 case TK_A_STRING_ESCAPE_NONE
:
2219 case TK_A_STRING_ESCAPE_REPLACE
:
2220 esc
= ESCAPE_REPLACE
;
2222 case TK_A_DB_PERSIST
:
2223 udev_device_set_db_persist(event
->dev
);
2225 case TK_A_INOTIFY_WATCH
:
2226 if (event
->inotify_watch_final
)
2228 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2229 event
->inotify_watch_final
= true;
2230 event
->inotify_watch
= cur
->key
.watch
;
2232 case TK_A_DEVLINK_PRIO
:
2233 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2236 char owner
[UTIL_NAME_SIZE
];
2237 const char *ow
= owner
;
2240 if (event
->owner_final
)
2242 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2243 event
->owner_final
= true;
2244 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2245 event
->owner_set
= true;
2246 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2248 if (r
== -ENOENT
|| r
== -ESRCH
)
2249 log_error("specified user '%s' unknown", owner
);
2251 log_error_errno(r
, "error resolving user '%s': %m", owner
);
2255 log_debug("OWNER %u %s:%u",
2257 rules_str(rules
, rule
->rule
.filename_off
),
2258 rule
->rule
.filename_line
);
2262 char group
[UTIL_NAME_SIZE
];
2263 const char *gr
= group
;
2266 if (event
->group_final
)
2268 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2269 event
->group_final
= true;
2270 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2271 event
->group_set
= true;
2272 r
= get_group_creds(&gr
, &event
->gid
);
2274 if (r
== -ENOENT
|| r
== -ESRCH
)
2275 log_error("specified group '%s' unknown", group
);
2277 log_error_errno(r
, "error resolving group '%s': %m", group
);
2281 log_debug("GROUP %u %s:%u",
2283 rules_str(rules
, rule
->rule
.filename_off
),
2284 rule
->rule
.filename_line
);
2288 char mode_str
[UTIL_NAME_SIZE
];
2292 if (event
->mode_final
)
2294 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2295 mode
= strtol(mode_str
, &endptr
, 8);
2296 if (endptr
[0] != '\0') {
2297 log_error("ignoring invalid mode '%s'", mode_str
);
2300 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2301 event
->mode_final
= true;
2302 event
->mode_set
= true;
2304 log_debug("MODE %#o %s:%u",
2306 rules_str(rules
, rule
->rule
.filename_off
),
2307 rule
->rule
.filename_line
);
2311 if (event
->owner_final
)
2313 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2314 event
->owner_final
= true;
2315 event
->owner_set
= true;
2316 event
->uid
= cur
->key
.uid
;
2317 log_debug("OWNER %u %s:%u",
2319 rules_str(rules
, rule
->rule
.filename_off
),
2320 rule
->rule
.filename_line
);
2323 if (event
->group_final
)
2325 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2326 event
->group_final
= true;
2327 event
->group_set
= true;
2328 event
->gid
= cur
->key
.gid
;
2329 log_debug("GROUP %u %s:%u",
2331 rules_str(rules
, rule
->rule
.filename_off
),
2332 rule
->rule
.filename_line
);
2335 if (event
->mode_final
)
2337 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2338 event
->mode_final
= true;
2339 event
->mode_set
= true;
2340 event
->mode
= cur
->key
.mode
;
2341 log_debug("MODE %#o %s:%u",
2343 rules_str(rules
, rule
->rule
.filename_off
),
2344 rule
->rule
.filename_line
);
2346 case TK_A_SECLABEL
: {
2347 const char *name
, *label
;
2349 name
= rules_str(rules
, cur
->key
.attr_off
);
2350 label
= rules_str(rules
, cur
->key
.value_off
);
2351 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2352 udev_list_cleanup(&event
->seclabel_list
);
2353 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2354 log_debug("SECLABEL{%s}='%s' %s:%u",
2356 rules_str(rules
, rule
->rule
.filename_off
),
2357 rule
->rule
.filename_line
);
2361 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2362 char *value
= rules_str(rules
, cur
->key
.value_off
);
2363 char value_new
[UTIL_NAME_SIZE
];
2364 const char *value_old
= NULL
;
2366 if (value
[0] == '\0') {
2367 if (cur
->key
.op
== OP_ADD
)
2369 udev_device_add_property(event
->dev
, name
, NULL
);
2373 if (cur
->key
.op
== OP_ADD
)
2374 value_old
= udev_device_get_property_value(event
->dev
, name
);
2376 char temp
[UTIL_NAME_SIZE
];
2378 /* append value separated by space */
2379 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2380 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2382 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2384 udev_device_add_property(event
->dev
, name
, value_new
);
2388 char tag
[UTIL_PATH_SIZE
];
2391 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2392 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2393 udev_device_cleanup_tags_list(event
->dev
);
2394 for (p
= tag
; *p
!= '\0'; p
++) {
2395 if ((*p
>= 'a' && *p
<= 'z') ||
2396 (*p
>= 'A' && *p
<= 'Z') ||
2397 (*p
>= '0' && *p
<= '9') ||
2398 *p
== '-' || *p
== '_')
2400 log_error("ignoring invalid tag name '%s'", tag
);
2403 if (cur
->key
.op
== OP_REMOVE
)
2404 udev_device_remove_tag(event
->dev
, tag
);
2406 udev_device_add_tag(event
->dev
, tag
);
2410 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2412 char name_str
[UTIL_PATH_SIZE
];
2415 if (event
->name_final
)
2417 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2418 event
->name_final
= true;
2419 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2420 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2421 count
= util_replace_chars(name_str
, "/");
2423 log_debug("%i character(s) replaced", count
);
2425 if (major(udev_device_get_devnum(event
->dev
)) &&
2426 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2427 log_error("NAME=\"%s\" ignored, kernel device nodes "
2428 "can not be renamed; please fix it in %s:%u\n", name
,
2429 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2433 event
->name
= strdup(name_str
);
2434 log_debug("NAME '%s' %s:%u",
2436 rules_str(rules
, rule
->rule
.filename_off
),
2437 rule
->rule
.filename_line
);
2440 case TK_A_DEVLINK
: {
2441 char temp
[UTIL_PATH_SIZE
];
2442 char filename
[UTIL_PATH_SIZE
];
2446 if (event
->devlink_final
)
2448 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2450 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2451 event
->devlink_final
= true;
2452 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2453 udev_device_cleanup_devlinks_list(event
->dev
);
2455 /* allow multiple symlinks separated by spaces */
2456 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2457 if (esc
== ESCAPE_UNSET
)
2458 count
= util_replace_chars(temp
, "/ ");
2459 else if (esc
== ESCAPE_REPLACE
)
2460 count
= util_replace_chars(temp
, "/");
2462 log_debug("%i character(s) replaced" , count
);
2464 while (isspace(pos
[0]))
2466 next
= strchr(pos
, ' ');
2467 while (next
!= NULL
) {
2469 log_debug("LINK '%s' %s:%u", pos
,
2470 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2471 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2472 udev_device_add_devlink(event
->dev
, filename
);
2473 while (isspace(next
[1]))
2476 next
= strchr(pos
, ' ');
2478 if (pos
[0] != '\0') {
2479 log_debug("LINK '%s' %s:%u", pos
,
2480 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2481 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2482 udev_device_add_devlink(event
->dev
, filename
);
2487 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2488 char attr
[UTIL_PATH_SIZE
];
2489 char value
[UTIL_NAME_SIZE
];
2492 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2493 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2494 attr_subst_subdir(attr
, sizeof(attr
));
2496 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2497 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2498 rules_str(rules
, rule
->rule
.filename_off
),
2499 rule
->rule
.filename_line
);
2500 f
= fopen(attr
, "we");
2502 if (fprintf(f
, "%s", value
) <= 0)
2503 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2506 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2511 char filename
[UTIL_PATH_SIZE
];
2512 char value
[UTIL_NAME_SIZE
];
2515 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
2516 sysctl_normalize(filename
);
2517 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2518 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2519 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2520 r
= sysctl_write(filename
, value
);
2522 log_error("error writing SYSCTL{%s}='%s': %s", filename
, value
, strerror(-r
));
2525 case TK_A_RUN_BUILTIN
:
2526 case TK_A_RUN_PROGRAM
: {
2527 struct udev_list_entry
*entry
;
2529 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2530 udev_list_cleanup(&event
->run_list
);
2531 log_debug("RUN '%s' %s:%u",
2532 rules_str(rules
, cur
->key
.value_off
),
2533 rules_str(rules
, rule
->rule
.filename_off
),
2534 rule
->rule
.filename_line
);
2535 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2536 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2540 if (cur
->key
.rule_goto
== 0)
2542 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2547 case TK_M_PARENTS_MIN
:
2548 case TK_M_PARENTS_MAX
:
2551 log_error("wrong type %u", cur
->type
);
2558 /* fast-forward to next rule */
2559 cur
= rule
+ rule
->rule
.token_count
;
2563 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2569 _cleanup_strv_free_
char **tags
= NULL
;
2572 _cleanup_free_
char *path
= NULL
;
2575 if (rules
->tokens
== NULL
)
2578 cur
= &rules
->tokens
[0];
2581 switch (cur
->type
) {
2586 /* skip rules without a static_node tag */
2587 if (!rule
->rule
.has_static_node
)
2603 mode
= cur
->key
.mode
;
2606 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2611 case TK_A_STATIC_NODE
: {
2612 char device_node
[UTIL_PATH_SIZE
];
2613 char tags_dir
[UTIL_PATH_SIZE
];
2614 char tag_symlink
[UTIL_PATH_SIZE
];
2617 /* we assure, that the permissions tokens are sorted before the static token */
2619 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2622 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2623 if (stat(device_node
, &stats
) != 0)
2625 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2628 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2630 STRV_FOREACH(t
, tags
) {
2631 _cleanup_free_
char *unescaped_filename
= NULL
;
2633 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2634 r
= mkdir_p(tags_dir
, 0755);
2636 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2638 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2640 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2641 r
= symlink(device_node
, tag_symlink
);
2642 if (r
< 0 && errno
!= EEXIST
)
2643 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2644 tag_symlink
, device_node
);
2650 /* don't touch the permissions if only the tags were set */
2651 if (mode
== 0 && uid
== 0 && gid
== 0)
2660 if (mode
!= (stats
.st_mode
& 01777)) {
2661 r
= chmod(device_node
, mode
);
2663 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2666 log_debug("chmod '%s' %#o", device_node
, mode
);
2669 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2670 r
= chown(device_node
, uid
, gid
);
2672 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2675 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2678 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2688 /* fast-forward to next rule */
2689 cur
= rule
+ rule
->rule
.token_count
;
2696 fchmod(fileno(f
), 0644);
2697 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2699 unlink("/run/udev/static_node-tags");