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/>.
32 #include "conf-files.h"
34 #include "path-util.h"
37 #include "sysctl-util.h"
41 #define PREALLOC_TOKEN 2048
44 unsigned int name_off
;
51 static const char* const rules_dirs
[] = {
54 UDEVLIBEXECDIR
"/rules.d",
63 /* every key in the rules file becomes a token */
65 unsigned int token_cur
;
66 unsigned int token_max
;
68 /* all key strings are copied and de-duplicated in a single continuous string buffer */
69 struct strbuf
*strbuf
;
71 /* during rule parsing, uid/gid lookup results are cached */
73 unsigned int uids_cur
;
74 unsigned int uids_max
;
76 unsigned int gids_cur
;
77 unsigned int gids_max
;
80 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
81 return rules
->strbuf
->buf
+ off
;
84 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
85 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
88 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
102 enum string_glob_type
{
104 GL_PLAIN
, /* no special chars */
105 GL_GLOB
, /* shell globs ?,*,[] */
106 GL_SPLIT
, /* multi-value A|B */
107 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
108 GL_SOMETHING
, /* commonly used "?*" */
111 enum string_subst_type
{
118 /* tokens of a rule are sorted/handled in this order */
123 TK_M_ACTION
, /* val */
124 TK_M_DEVPATH
, /* val */
125 TK_M_KERNEL
, /* val */
126 TK_M_DEVLINK
, /* val */
128 TK_M_ENV
, /* val, attr */
130 TK_M_SUBSYSTEM
, /* val */
131 TK_M_DRIVER
, /* val */
132 TK_M_WAITFOR
, /* val */
133 TK_M_ATTR
, /* val, attr */
134 TK_M_SYSCTL
, /* val, attr */
137 TK_M_KERNELS
, /* val */
138 TK_M_SUBSYSTEMS
, /* val */
139 TK_M_DRIVERS
, /* val */
140 TK_M_ATTRS
, /* val, attr */
144 TK_M_TEST
, /* val, mode_t */
145 TK_M_PROGRAM
, /* val */
146 TK_M_IMPORT_FILE
, /* val */
147 TK_M_IMPORT_PROG
, /* val */
148 TK_M_IMPORT_BUILTIN
, /* val */
149 TK_M_IMPORT_DB
, /* val */
150 TK_M_IMPORT_CMDLINE
, /* val */
151 TK_M_IMPORT_PARENT
, /* val */
152 TK_M_RESULT
, /* val */
155 TK_A_STRING_ESCAPE_NONE
,
156 TK_A_STRING_ESCAPE_REPLACE
,
158 TK_A_INOTIFY_WATCH
, /* int */
159 TK_A_DEVLINK_PRIO
, /* int */
160 TK_A_OWNER
, /* val */
161 TK_A_GROUP
, /* val */
163 TK_A_OWNER_ID
, /* uid_t */
164 TK_A_GROUP_ID
, /* gid_t */
165 TK_A_MODE_ID
, /* mode_t */
167 TK_A_STATIC_NODE
, /* val */
168 TK_A_SECLABEL
, /* val, attr */
169 TK_A_ENV
, /* val, attr */
171 TK_A_DEVLINK
, /* val */
172 TK_A_ATTR
, /* val, attr */
173 TK_A_SYSCTL
, /* val, attr */
174 TK_A_RUN_BUILTIN
, /* val, bool */
175 TK_A_RUN_PROGRAM
, /* val, bool */
176 TK_A_GOTO
, /* size_t */
181 /* we try to pack stuff in a way that we take only 12 bytes per token */
184 unsigned char type
; /* same in rule and key */
186 enum token_type type
:8;
188 bool has_static_node
:1;
189 unsigned int unused
:6;
190 unsigned short token_count
;
191 unsigned int label_off
;
192 unsigned short filename_off
;
193 unsigned short filename_line
;
196 enum token_type type
:8;
197 enum operation_type op
:8;
198 enum string_glob_type glob
:8;
199 enum string_subst_type subst
:4;
200 enum string_subst_type attrsubst
:4;
201 unsigned int value_off
;
203 unsigned int attr_off
;
204 unsigned int rule_goto
;
210 enum udev_builtin_cmd builtin_cmd
;
218 struct udev_rules
*rules
;
220 struct token token
[MAX_TK
];
221 unsigned int token_cur
;
225 static const char *operation_str(enum operation_type type
) {
226 static const char *operation_strs
[] = {
227 [OP_UNSET
] = "UNSET",
228 [OP_MATCH
] = "match",
229 [OP_NOMATCH
] = "nomatch",
230 [OP_MATCH_MAX
] = "MATCH_MAX",
233 [OP_REMOVE
] = "remove",
234 [OP_ASSIGN
] = "assign",
235 [OP_ASSIGN_FINAL
] = "assign-final",
238 return operation_strs
[type
];
241 static const char *string_glob_str(enum string_glob_type type
) {
242 static const char *string_glob_strs
[] = {
243 [GL_UNSET
] = "UNSET",
244 [GL_PLAIN
] = "plain",
246 [GL_SPLIT
] = "split",
247 [GL_SPLIT_GLOB
] = "split-glob",
248 [GL_SOMETHING
] = "split-glob",
251 return string_glob_strs
[type
];
254 static const char *token_str(enum token_type type
) {
255 static const char *token_strs
[] = {
256 [TK_UNSET
] = "UNSET",
259 [TK_M_ACTION
] = "M ACTION",
260 [TK_M_DEVPATH
] = "M DEVPATH",
261 [TK_M_KERNEL
] = "M KERNEL",
262 [TK_M_DEVLINK
] = "M DEVLINK",
263 [TK_M_NAME
] = "M NAME",
264 [TK_M_ENV
] = "M ENV",
265 [TK_M_TAG
] = "M TAG",
266 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
267 [TK_M_DRIVER
] = "M DRIVER",
268 [TK_M_WAITFOR
] = "M WAITFOR",
269 [TK_M_ATTR
] = "M ATTR",
270 [TK_M_SYSCTL
] = "M SYSCTL",
272 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
273 [TK_M_KERNELS
] = "M KERNELS",
274 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
275 [TK_M_DRIVERS
] = "M DRIVERS",
276 [TK_M_ATTRS
] = "M ATTRS",
277 [TK_M_TAGS
] = "M TAGS",
278 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
280 [TK_M_TEST
] = "M TEST",
281 [TK_M_PROGRAM
] = "M PROGRAM",
282 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
283 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
284 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
285 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
286 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
287 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
288 [TK_M_RESULT
] = "M RESULT",
289 [TK_M_MAX
] = "M MAX",
291 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
292 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
293 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
294 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
295 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
296 [TK_A_OWNER
] = "A OWNER",
297 [TK_A_GROUP
] = "A GROUP",
298 [TK_A_MODE
] = "A MODE",
299 [TK_A_OWNER_ID
] = "A OWNER_ID",
300 [TK_A_GROUP_ID
] = "A GROUP_ID",
301 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
302 [TK_A_SECLABEL
] = "A SECLABEL",
303 [TK_A_MODE_ID
] = "A MODE_ID",
304 [TK_A_ENV
] = "A ENV",
305 [TK_A_TAG
] = "A ENV",
306 [TK_A_NAME
] = "A NAME",
307 [TK_A_DEVLINK
] = "A DEVLINK",
308 [TK_A_ATTR
] = "A ATTR",
309 [TK_A_SYSCTL
] = "A SYSCTL",
310 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
311 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
312 [TK_A_GOTO
] = "A GOTO",
317 return token_strs
[type
];
320 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
321 enum token_type type
= token
->type
;
322 enum operation_type op
= token
->key
.op
;
323 enum string_glob_type glob
= token
->key
.glob
;
324 const char *value
= str(rules
, token
->key
.value_off
);
325 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
330 const char *tks_ptr
= (char *)rules
->tokens
;
331 const char *tk_ptr
= (char *)token
;
332 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
334 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
335 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
336 idx
, token
->rule
.token_count
,
337 &rules
->buf
[token
->rule
.label_off
]);
349 case TK_M_SUBSYSTEMS
:
353 case TK_M_IMPORT_FILE
:
354 case TK_M_IMPORT_PROG
:
356 case TK_M_IMPORT_CMDLINE
:
357 case TK_M_IMPORT_PARENT
:
364 case TK_A_RUN_BUILTIN
:
365 case TK_A_RUN_PROGRAM
:
366 log_debug("%s %s '%s'(%s)",
367 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
369 case TK_M_IMPORT_BUILTIN
:
370 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
379 log_debug("%s %s '%s' '%s'(%s)",
380 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
384 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
386 case TK_A_STRING_ESCAPE_NONE
:
387 case TK_A_STRING_ESCAPE_REPLACE
:
388 case TK_A_DB_PERSIST
:
389 log_debug("%s", token_str(type
));
392 log_debug("%s %s '%s'(%s) %#o",
393 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
395 case TK_A_INOTIFY_WATCH
:
396 log_debug("%s %u", token_str(type
), token
->key
.watch
);
398 case TK_A_DEVLINK_PRIO
:
399 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
402 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
405 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
408 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
410 case TK_A_STATIC_NODE
:
411 log_debug("%s '%s'", token_str(type
), value
);
414 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
417 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
420 log_debug("* %s", token_str(type
));
422 case TK_M_PARENTS_MIN
:
423 case TK_M_PARENTS_MAX
:
426 log_debug("unknown type %u", type
);
431 static void dump_rules(struct udev_rules
*rules
) {
434 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
436 rules
->token_cur
* sizeof(struct token
),
439 for (i
= 0; i
< rules
->token_cur
; i
++)
440 dump_token(rules
, &rules
->tokens
[i
]);
443 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
444 static inline void dump_rules(struct udev_rules
*rules
) {}
447 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
448 /* grow buffer if needed */
449 if (rules
->token_cur
+1 >= rules
->token_max
) {
450 struct token
*tokens
;
453 /* double the buffer size */
454 add
= rules
->token_max
;
458 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
461 rules
->tokens
= tokens
;
462 rules
->token_max
+= add
;
464 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
469 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
475 /* lookup, if we know it already */
476 for (i
= 0; i
< rules
->uids_cur
; i
++) {
477 off
= rules
->uids
[i
].name_off
;
478 if (streq(rules_str(rules
, off
), owner
)) {
479 uid
= rules
->uids
[i
].uid
;
483 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
485 if (r
== -ENOENT
|| r
== -ESRCH
)
486 log_error("specified user '%s' unknown", owner
);
488 log_error_errno(r
, "error resolving user '%s': %m", owner
);
491 /* grow buffer if needed */
492 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
493 struct uid_gid
*uids
;
496 /* double the buffer size */
497 add
= rules
->uids_max
;
501 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
505 rules
->uids_max
+= add
;
507 rules
->uids
[rules
->uids_cur
].uid
= uid
;
508 off
= rules_add_string(rules
, owner
);
511 rules
->uids
[rules
->uids_cur
].name_off
= off
;
516 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
522 /* lookup, if we know it already */
523 for (i
= 0; i
< rules
->gids_cur
; i
++) {
524 off
= rules
->gids
[i
].name_off
;
525 if (streq(rules_str(rules
, off
), group
)) {
526 gid
= rules
->gids
[i
].gid
;
530 r
= get_group_creds(&group
, &gid
);
532 if (r
== -ENOENT
|| r
== -ESRCH
)
533 log_error("specified group '%s' unknown", group
);
535 log_error_errno(r
, "error resolving group '%s': %m", group
);
538 /* grow buffer if needed */
539 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
540 struct uid_gid
*gids
;
543 /* double the buffer size */
544 add
= rules
->gids_max
;
548 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
552 rules
->gids_max
+= add
;
554 rules
->gids
[rules
->gids_cur
].gid
= gid
;
555 off
= rules_add_string(rules
, group
);
558 rules
->gids
[rules
->gids_cur
].name_off
= off
;
563 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
570 while (isspace(key
[0]))
573 /* comment or empty line */
574 if (key
[0] == '#' || key
[0] == '\0')
577 /* split key/value */
578 val
= strchr(key
, '=');
585 while (isspace(val
[0]))
592 while (isspace(key
[len
-1]))
596 /* terminate value */
600 while (isspace(val
[len
-1]))
608 if (val
[0] == '"' || val
[0] == '\'') {
609 if (val
[len
-1] != val
[0]) {
610 log_debug("inconsistent quoting: '%s', skip", line
);
617 udev_device_add_property(dev
, key
, val
);
622 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
624 char line
[UTIL_LINE_SIZE
];
626 f
= fopen(filename
, "re");
629 while (fgets(line
, sizeof(line
), f
) != NULL
)
630 import_property_from_string(dev
, line
);
635 static int import_program_into_properties(struct udev_event
*event
,
637 usec_t timeout_warn_usec
,
638 const char *program
) {
639 char result
[UTIL_LINE_SIZE
];
643 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
648 while (line
!= NULL
) {
651 pos
= strchr(line
, '\n');
656 import_property_from_string(event
->dev
, line
);
662 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
663 struct udev_device
*dev_parent
;
664 struct udev_list_entry
*list_entry
;
669 dev_parent
= udev_device_get_parent(dev
);
670 if (dev_parent
== NULL
)
673 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
674 const char *key
= udev_list_entry_get_name(list_entry
);
675 const char *val
= udev_list_entry_get_value(list_entry
);
677 if (fnmatch(filter
, key
, 0) == 0)
678 udev_device_add_property(dev
, key
, val
);
683 static int attr_subst_subdir(char *attr
, size_t len
) {
686 if (strstr(attr
, "/*/")) {
688 char dirname
[UTIL_PATH_SIZE
];
692 strscpy(dirname
, sizeof(dirname
), attr
);
693 pos
= strstr(dirname
, "/*/");
698 dir
= opendir(dirname
);
702 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
705 if (dent
->d_name
[0] == '.')
707 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
708 if (stat(attr
, &stats
) == 0) {
720 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
725 if (linepos
== NULL
|| linepos
[0] == '\0')
728 /* skip whitespace */
729 while (isspace(linepos
[0]) || linepos
[0] == ',')
733 if (linepos
[0] == '\0')
739 if (linepos
[0] == '\0')
741 if (isspace(linepos
[0]))
743 if (linepos
[0] == '=')
745 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
746 if (linepos
[1] == '=')
750 /* remember end of key */
753 /* skip whitespace after key */
754 while (isspace(linepos
[0]))
756 if (linepos
[0] == '\0')
759 /* get operation type */
760 if (linepos
[0] == '=' && linepos
[1] == '=') {
763 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
766 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
769 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
772 } else if (linepos
[0] == '=') {
775 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
776 *op
= OP_ASSIGN_FINAL
;
784 /* skip whitespace after operator */
785 while (isspace(linepos
[0]))
787 if (linepos
[0] == '\0')
791 if (linepos
[0] == '"')
798 temp
= strchr(linepos
, '"');
804 /* move line to next key */
809 /* extract possible KEY{attr} */
810 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
814 attr
= strchr(str
, '{');
817 pos
= strchr(attr
, '}');
819 log_error("missing closing brace for format");
828 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
829 enum operation_type op
,
830 const char *value
, const void *data
) {
831 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
832 const char *attr
= NULL
;
834 memzero(token
, sizeof(struct token
));
846 case TK_M_SUBSYSTEMS
:
850 case TK_M_IMPORT_FILE
:
851 case TK_M_IMPORT_PROG
:
853 case TK_M_IMPORT_CMDLINE
:
854 case TK_M_IMPORT_PARENT
:
864 case TK_A_STATIC_NODE
:
865 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
867 case TK_M_IMPORT_BUILTIN
:
868 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
869 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
880 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
881 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
884 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
886 token
->key
.mode
= *(mode_t
*)data
;
888 case TK_A_STRING_ESCAPE_NONE
:
889 case TK_A_STRING_ESCAPE_REPLACE
:
890 case TK_A_DB_PERSIST
:
892 case TK_A_RUN_BUILTIN
:
893 case TK_A_RUN_PROGRAM
:
894 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
895 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
897 case TK_A_INOTIFY_WATCH
:
898 case TK_A_DEVLINK_PRIO
:
899 token
->key
.devlink_prio
= *(int *)data
;
902 token
->key
.uid
= *(uid_t
*)data
;
905 token
->key
.gid
= *(gid_t
*)data
;
908 token
->key
.mode
= *(mode_t
*)data
;
911 case TK_M_PARENTS_MIN
:
912 case TK_M_PARENTS_MAX
:
916 log_error("wrong type %u", type
);
920 if (value
!= NULL
&& type
< TK_M_MAX
) {
921 /* check if we need to split or call fnmatch() while matching rules */
922 enum string_glob_type glob
;
926 has_split
= (strchr(value
, '|') != NULL
);
927 has_glob
= string_is_glob(value
);
928 if (has_split
&& has_glob
) {
929 glob
= GL_SPLIT_GLOB
;
930 } else if (has_split
) {
932 } else if (has_glob
) {
933 if (streq(value
, "?*"))
940 token
->key
.glob
= glob
;
943 if (value
!= NULL
&& type
> TK_M_MAX
) {
944 /* check if assigned value has substitution chars */
946 token
->key
.subst
= SB_SUBSYS
;
947 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
948 token
->key
.subst
= SB_FORMAT
;
950 token
->key
.subst
= SB_NONE
;
954 /* check if property/attribute name has substitution chars */
956 token
->key
.attrsubst
= SB_SUBSYS
;
957 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
958 token
->key
.attrsubst
= SB_FORMAT
;
960 token
->key
.attrsubst
= SB_NONE
;
963 token
->key
.type
= type
;
965 rule_tmp
->token_cur
++;
966 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
967 log_error("temporary rule array too small");
973 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
975 unsigned int start
= 0;
976 unsigned int end
= rule_tmp
->token_cur
;
978 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
979 enum token_type next_val
= TK_UNSET
;
980 unsigned int next_idx
= 0;
983 /* find smallest value */
984 for (j
= start
; j
< end
; j
++) {
985 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
987 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
988 next_val
= rule_tmp
->token
[j
].type
;
993 /* add token and mark done */
994 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
996 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
999 if (next_idx
== start
)
1001 if (next_idx
+1 == end
)
1007 static int add_rule(struct udev_rules
*rules
, char *line
,
1008 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1011 struct rule_tmp rule_tmp
= {
1013 .rule
.type
= TK_RULE
,
1016 /* the offset in the rule is limited to unsigned short */
1017 if (filename_off
< USHRT_MAX
)
1018 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1019 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1025 enum operation_type op
;
1027 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1028 /* Avoid erroring on trailing whitespace. This is probably rare
1029 * so save the work for the error case instead of always trying
1030 * to strip the trailing whitespace with strstrip(). */
1031 while (isblank(*linepos
))
1034 /* If we aren't at the end of the line, this is a parsing error.
1035 * Make a best effort to describe where the problem is. */
1036 if (!strchr(NEWLINE
, *linepos
)) {
1037 char buf
[2] = {*linepos
};
1038 _cleanup_free_
char *tmp
;
1041 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1042 filename
, lineno
, linepos
- line
+ 1, tmp
);
1043 if (*linepos
== '#')
1044 log_error("hint: comments can only start at beginning of line");
1049 if (streq(key
, "ACTION")) {
1050 if (op
> OP_MATCH_MAX
) {
1051 log_error("invalid ACTION operation");
1054 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1058 if (streq(key
, "DEVPATH")) {
1059 if (op
> OP_MATCH_MAX
) {
1060 log_error("invalid DEVPATH operation");
1063 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1067 if (streq(key
, "KERNEL")) {
1068 if (op
> OP_MATCH_MAX
) {
1069 log_error("invalid KERNEL operation");
1072 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1076 if (streq(key
, "SUBSYSTEM")) {
1077 if (op
> OP_MATCH_MAX
) {
1078 log_error("invalid SUBSYSTEM operation");
1081 /* bus, class, subsystem events should all be the same */
1082 if (streq(value
, "subsystem") ||
1083 streq(value
, "bus") ||
1084 streq(value
, "class")) {
1085 if (streq(value
, "bus") || streq(value
, "class"))
1086 log_error("'%s' must be specified as 'subsystem' "
1087 "please fix it in %s:%u", value
, filename
, lineno
);
1088 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1090 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1094 if (streq(key
, "DRIVER")) {
1095 if (op
> OP_MATCH_MAX
) {
1096 log_error("invalid DRIVER operation");
1099 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1103 if (startswith(key
, "ATTR{")) {
1104 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1106 log_error("error parsing ATTR attribute");
1109 if (op
== OP_REMOVE
) {
1110 log_error("invalid ATTR operation");
1113 if (op
< OP_MATCH_MAX
)
1114 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1116 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1120 if (startswith(key
, "SYSCTL{")) {
1121 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SYSCTL"));
1123 log_error("error parsing SYSCTL attribute");
1126 if (op
== OP_REMOVE
) {
1127 log_error("invalid SYSCTL operation");
1130 if (op
< OP_MATCH_MAX
)
1131 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1133 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1137 if (startswith(key
, "SECLABEL{")) {
1138 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1140 log_error("error parsing SECLABEL attribute");
1143 if (op
== OP_REMOVE
) {
1144 log_error("invalid SECLABEL operation");
1148 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1152 if (streq(key
, "KERNELS")) {
1153 if (op
> OP_MATCH_MAX
) {
1154 log_error("invalid KERNELS operation");
1157 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1161 if (streq(key
, "SUBSYSTEMS")) {
1162 if (op
> OP_MATCH_MAX
) {
1163 log_error("invalid SUBSYSTEMS operation");
1166 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1170 if (streq(key
, "DRIVERS")) {
1171 if (op
> OP_MATCH_MAX
) {
1172 log_error("invalid DRIVERS operation");
1175 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1179 if (startswith(key
, "ATTRS{")) {
1180 if (op
> OP_MATCH_MAX
) {
1181 log_error("invalid ATTRS operation");
1184 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1186 log_error("error parsing ATTRS attribute");
1189 if (startswith(attr
, "device/"))
1190 log_error("the 'device' link may not be available in a future kernel, "
1191 "please fix it in %s:%u", filename
, lineno
);
1192 else if (strstr(attr
, "../") != NULL
)
1193 log_error("do not reference parent sysfs directories directly, "
1194 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1195 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1199 if (streq(key
, "TAGS")) {
1200 if (op
> OP_MATCH_MAX
) {
1201 log_error("invalid TAGS operation");
1204 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1208 if (startswith(key
, "ENV{")) {
1209 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1211 log_error("error parsing ENV attribute");
1214 if (op
== OP_REMOVE
) {
1215 log_error("invalid ENV operation");
1218 if (op
< OP_MATCH_MAX
) {
1219 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1222 static const char *blacklist
[] = {
1237 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1238 if (!streq(attr
, blacklist
[i
]))
1240 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1243 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1249 if (streq(key
, "TAG")) {
1250 if (op
< OP_MATCH_MAX
)
1251 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1253 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1257 if (streq(key
, "PROGRAM")) {
1258 if (op
== OP_REMOVE
) {
1259 log_error("invalid PROGRAM operation");
1262 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1266 if (streq(key
, "RESULT")) {
1267 if (op
> OP_MATCH_MAX
) {
1268 log_error("invalid RESULT operation");
1271 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1275 if (startswith(key
, "IMPORT")) {
1276 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1278 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1281 if (op
== OP_REMOVE
) {
1282 log_error("invalid IMPORT operation");
1285 if (streq(attr
, "program")) {
1286 /* find known built-in command */
1287 if (value
[0] != '/') {
1288 enum udev_builtin_cmd cmd
;
1290 cmd
= udev_builtin_lookup(value
);
1291 if (cmd
< UDEV_BUILTIN_MAX
) {
1292 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1293 value
, filename
, lineno
);
1294 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1298 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1299 } else if (streq(attr
, "builtin")) {
1300 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1302 if (cmd
< UDEV_BUILTIN_MAX
)
1303 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1305 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1306 } else if (streq(attr
, "file")) {
1307 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1308 } else if (streq(attr
, "db")) {
1309 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1310 } else if (streq(attr
, "cmdline")) {
1311 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1312 } else if (streq(attr
, "parent")) {
1313 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1315 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1319 if (startswith(key
, "TEST")) {
1322 if (op
> OP_MATCH_MAX
) {
1323 log_error("invalid TEST operation");
1326 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1328 mode
= strtol(attr
, NULL
, 8);
1329 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1331 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1336 if (startswith(key
, "RUN")) {
1337 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1340 if (op
== OP_REMOVE
) {
1341 log_error("invalid RUN operation");
1345 if (streq(attr
, "builtin")) {
1346 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1348 if (cmd
< UDEV_BUILTIN_MAX
)
1349 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1351 log_error("RUN{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1352 } else if (streq(attr
, "program")) {
1353 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1355 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1357 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1363 if (streq(key
, "LABEL")) {
1364 if (op
== OP_REMOVE
) {
1365 log_error("invalid LABEL operation");
1368 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1372 if (streq(key
, "GOTO")) {
1373 if (op
== OP_REMOVE
) {
1374 log_error("invalid GOTO operation");
1377 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1381 if (startswith(key
, "NAME")) {
1382 if (op
== OP_REMOVE
) {
1383 log_error("invalid NAME operation");
1386 if (op
< OP_MATCH_MAX
) {
1387 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1389 if (streq(value
, "%k")) {
1390 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1391 "please remove it from %s:%u\n", filename
, lineno
);
1394 if (value
[0] == '\0') {
1395 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1396 "please remove it from %s:%u\n", filename
, lineno
);
1399 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1401 rule_tmp
.rule
.rule
.can_set_name
= true;
1405 if (streq(key
, "SYMLINK")) {
1406 if (op
== OP_REMOVE
) {
1407 log_error("invalid SYMLINK operation");
1410 if (op
< OP_MATCH_MAX
)
1411 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1413 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1414 rule_tmp
.rule
.rule
.can_set_name
= true;
1418 if (streq(key
, "OWNER")) {
1422 if (op
== OP_REMOVE
) {
1423 log_error("invalid OWNER operation");
1427 uid
= strtoul(value
, &endptr
, 10);
1428 if (endptr
[0] == '\0') {
1429 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1430 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1431 uid
= add_uid(rules
, value
);
1432 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1433 } else if (rules
->resolve_names
>= 0)
1434 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1436 rule_tmp
.rule
.rule
.can_set_name
= true;
1440 if (streq(key
, "GROUP")) {
1444 if (op
== OP_REMOVE
) {
1445 log_error("invalid GROUP operation");
1449 gid
= strtoul(value
, &endptr
, 10);
1450 if (endptr
[0] == '\0') {
1451 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1452 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1453 gid
= add_gid(rules
, value
);
1454 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1455 } else if (rules
->resolve_names
>= 0)
1456 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1458 rule_tmp
.rule
.rule
.can_set_name
= true;
1462 if (streq(key
, "MODE")) {
1466 if (op
== OP_REMOVE
) {
1467 log_error("invalid MODE operation");
1471 mode
= strtol(value
, &endptr
, 8);
1472 if (endptr
[0] == '\0')
1473 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1475 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1476 rule_tmp
.rule
.rule
.can_set_name
= true;
1480 if (streq(key
, "OPTIONS")) {
1483 if (op
== OP_REMOVE
) {
1484 log_error("invalid OPTIONS operation");
1488 pos
= strstr(value
, "link_priority=");
1490 int prio
= atoi(&pos
[strlen("link_priority=")]);
1492 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1495 pos
= strstr(value
, "string_escape=");
1497 pos
= &pos
[strlen("string_escape=")];
1498 if (startswith(pos
, "none"))
1499 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1500 else if (startswith(pos
, "replace"))
1501 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1504 pos
= strstr(value
, "db_persist");
1506 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1508 pos
= strstr(value
, "nowatch");
1512 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1514 pos
= strstr(value
, "watch");
1518 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1522 pos
= strstr(value
, "static_node=");
1524 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1525 rule_tmp
.rule
.rule
.has_static_node
= true;
1531 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1535 /* add rule token */
1536 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1537 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1540 /* add tokens to list, sorted by type */
1541 if (sort_token(rules
, &rule_tmp
) != 0)
1546 log_error("invalid rule '%s:%u'", filename
, lineno
);
1550 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1551 _cleanup_fclose_
FILE *f
= NULL
;
1552 unsigned int first_token
;
1553 unsigned int filename_off
;
1554 char line
[UTIL_LINE_SIZE
];
1558 f
= fopen(filename
, "re");
1560 if (errno
== ENOENT
)
1566 if (null_or_empty_fd(fileno(f
))) {
1567 log_debug("Skipping empty file: %s", filename
);
1570 log_debug("Reading rules file: %s", filename
);
1572 first_token
= rules
->token_cur
;
1573 filename_off
= rules_add_string(rules
, filename
);
1575 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1579 /* skip whitespace */
1582 while (isspace(key
[0]))
1593 /* continue reading if backslash+newline is found */
1594 while (line
[len
-2] == '\\') {
1595 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1597 if (strlen(&line
[len
-2]) < 2)
1603 if (len
+1 >= sizeof(line
)) {
1604 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1607 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1610 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1611 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1612 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1613 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1616 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1617 if (rules
->tokens
[j
].type
!= TK_RULE
)
1619 if (rules
->tokens
[j
].rule
.label_off
== 0)
1621 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1623 rules
->tokens
[i
].key
.rule_goto
= j
;
1626 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1627 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1633 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1634 struct udev_rules
*rules
;
1635 struct udev_list file_list
;
1636 struct token end_token
;
1640 rules
= new0(struct udev_rules
, 1);
1644 rules
->resolve_names
= resolve_names
;
1645 udev_list_init(udev
, &file_list
, true);
1647 /* init token array and string buffer */
1648 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1649 if (rules
->tokens
== NULL
)
1650 return udev_rules_unref(rules
);
1651 rules
->token_max
= PREALLOC_TOKEN
;
1653 rules
->strbuf
= strbuf_new();
1655 return udev_rules_unref(rules
);
1657 udev_rules_check_timestamp(rules
);
1659 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1661 log_error_errno(r
, "failed to enumerate rules files: %m");
1662 return udev_rules_unref(rules
);
1666 * The offset value in the rules strct is limited; add all
1667 * rules file names to the beginning of the string buffer.
1669 STRV_FOREACH(f
, files
)
1670 rules_add_string(rules
, *f
);
1672 STRV_FOREACH(f
, files
)
1673 parse_file(rules
, *f
);
1677 memzero(&end_token
, sizeof(struct token
));
1678 end_token
.type
= TK_END
;
1679 add_token(rules
, &end_token
);
1680 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1681 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1683 /* cleanup temporary strbuf data */
1684 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1685 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1686 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1687 strbuf_complete(rules
->strbuf
);
1689 /* cleanup uid/gid cache */
1690 rules
->uids
= mfree(rules
->uids
);
1691 rules
->uids_cur
= 0;
1692 rules
->uids_max
= 0;
1693 rules
->gids
= mfree(rules
->gids
);
1694 rules
->gids_cur
= 0;
1695 rules
->gids_max
= 0;
1701 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1704 free(rules
->tokens
);
1705 strbuf_cleanup(rules
->strbuf
);
1712 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1716 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1719 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1720 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1727 switch (token
->key
.glob
) {
1729 match
= (streq(key_value
, val
));
1732 match
= (fnmatch(key_value
, val
, 0) == 0);
1739 s
= rules_str(rules
, token
->key
.value_off
);
1744 next
= strchr(s
, '|');
1746 size_t matchlen
= (size_t)(next
- s
);
1748 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1752 match
= (streq(s
, val
));
1761 char value
[UTIL_PATH_SIZE
];
1763 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1765 while (key_value
!= NULL
) {
1766 pos
= strchr(key_value
, '|');
1771 match
= (fnmatch(key_value
, val
, 0) == 0);
1779 match
= (val
[0] != '\0');
1785 if (match
&& (token
->key
.op
== OP_MATCH
))
1787 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1792 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1794 char nbuf
[UTIL_NAME_SIZE
];
1796 char vbuf
[UTIL_NAME_SIZE
];
1799 name
= rules_str(rules
, cur
->key
.attr_off
);
1800 switch (cur
->key
.attrsubst
) {
1802 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1806 value
= udev_device_get_sysattr_value(dev
, name
);
1811 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1819 /* remove trailing whitespace, if not asked to match for it */
1820 len
= strlen(value
);
1821 if (len
> 0 && isspace(value
[len
-1])) {
1822 const char *key_value
;
1825 key_value
= rules_str(rules
, cur
->key
.value_off
);
1826 klen
= strlen(key_value
);
1827 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1828 if (value
!= vbuf
) {
1829 strscpy(vbuf
, sizeof(vbuf
), value
);
1832 while (len
> 0 && isspace(vbuf
[--len
]))
1837 return match_key(rules
, cur
, value
);
1846 int udev_rules_apply_to_event(struct udev_rules
*rules
,
1847 struct udev_event
*event
,
1848 usec_t timeout_usec
,
1849 usec_t timeout_warn_usec
,
1850 struct udev_list
*properties_list
) {
1853 enum escape_type esc
= ESCAPE_UNSET
;
1856 if (rules
->tokens
== NULL
)
1859 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1860 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1861 udev_device_get_ifindex(event
->dev
) > 0));
1863 /* loop through token list, match, run actions or forward to next rule */
1864 cur
= &rules
->tokens
[0];
1867 dump_token(rules
, cur
);
1868 switch (cur
->type
) {
1872 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1873 if (!can_set_name
&& rule
->rule
.can_set_name
)
1878 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1882 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1886 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1889 case TK_M_DEVLINK
: {
1890 struct udev_list_entry
*list_entry
;
1893 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1894 const char *devlink
;
1896 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1897 if (match_key(rules
, cur
, devlink
) == 0) {
1907 if (match_key(rules
, cur
, event
->name
) != 0)
1911 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1914 value
= udev_device_get_property_value(event
->dev
, key_name
);
1916 /* check global properties */
1917 if (!value
&& properties_list
) {
1918 struct udev_list_entry
*list_entry
;
1920 list_entry
= udev_list_get_entry(properties_list
);
1921 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1922 if (list_entry
!= NULL
)
1923 value
= udev_list_entry_get_value(list_entry
);
1928 if (match_key(rules
, cur
, value
))
1933 struct udev_list_entry
*list_entry
;
1936 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1937 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1942 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1943 (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 event
->program_result
= mfree(event
->program_result
);
2068 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2069 log_debug("PROGRAM '%s' %s:%u",
2071 rules_str(rules
, rule
->rule
.filename_off
),
2072 rule
->rule
.filename_line
);
2074 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
2075 if (cur
->key
.op
!= OP_NOMATCH
)
2080 util_remove_trailing_chars(result
, '\n');
2081 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2082 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2084 log_debug("%i character(s) replaced" , count
);
2086 event
->program_result
= strdup(result
);
2087 if (cur
->key
.op
== OP_NOMATCH
)
2092 case TK_M_IMPORT_FILE
: {
2093 char import
[UTIL_PATH_SIZE
];
2095 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2096 if (import_file_into_properties(event
->dev
, import
) != 0)
2097 if (cur
->key
.op
!= OP_NOMATCH
)
2101 case TK_M_IMPORT_PROG
: {
2102 char import
[UTIL_PATH_SIZE
];
2104 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2105 log_debug("IMPORT '%s' %s:%u",
2107 rules_str(rules
, rule
->rule
.filename_off
),
2108 rule
->rule
.filename_line
);
2110 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
2111 if (cur
->key
.op
!= OP_NOMATCH
)
2115 case TK_M_IMPORT_BUILTIN
: {
2116 char command
[UTIL_PATH_SIZE
];
2118 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2119 /* check if we ran already */
2120 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2121 log_debug("IMPORT builtin skip '%s' %s:%u",
2122 udev_builtin_name(cur
->key
.builtin_cmd
),
2123 rules_str(rules
, rule
->rule
.filename_off
),
2124 rule
->rule
.filename_line
);
2125 /* return the result from earlier run */
2126 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2127 if (cur
->key
.op
!= OP_NOMATCH
)
2132 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2135 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2136 log_debug("IMPORT builtin '%s' %s:%u",
2137 udev_builtin_name(cur
->key
.builtin_cmd
),
2138 rules_str(rules
, rule
->rule
.filename_off
),
2139 rule
->rule
.filename_line
);
2141 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2142 /* remember failure */
2143 log_debug("IMPORT builtin '%s' returned non-zero",
2144 udev_builtin_name(cur
->key
.builtin_cmd
));
2145 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2146 if (cur
->key
.op
!= OP_NOMATCH
)
2151 case TK_M_IMPORT_DB
: {
2152 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2155 value
= udev_device_get_property_value(event
->dev_db
, key
);
2157 udev_device_add_property(event
->dev
, key
, value
);
2159 if (cur
->key
.op
!= OP_NOMATCH
)
2164 case TK_M_IMPORT_CMDLINE
: {
2166 bool imported
= false;
2168 f
= fopen("/proc/cmdline", "re");
2172 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2173 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2176 pos
= strstr(cmdline
, key
);
2179 if (pos
[0] == '\0' || isspace(pos
[0])) {
2180 /* we import simple flags as 'FLAG=1' */
2181 udev_device_add_property(event
->dev
, key
, "1");
2183 } else if (pos
[0] == '=') {
2188 while (pos
[0] != '\0' && !isspace(pos
[0]))
2191 udev_device_add_property(event
->dev
, key
, value
);
2198 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2202 case TK_M_IMPORT_PARENT
: {
2203 char import
[UTIL_PATH_SIZE
];
2205 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2206 if (import_parent_into_properties(event
->dev
, import
) != 0)
2207 if (cur
->key
.op
!= OP_NOMATCH
)
2212 if (match_key(rules
, cur
, event
->program_result
) != 0)
2215 case TK_A_STRING_ESCAPE_NONE
:
2218 case TK_A_STRING_ESCAPE_REPLACE
:
2219 esc
= ESCAPE_REPLACE
;
2221 case TK_A_DB_PERSIST
:
2222 udev_device_set_db_persist(event
->dev
);
2224 case TK_A_INOTIFY_WATCH
:
2225 if (event
->inotify_watch_final
)
2227 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2228 event
->inotify_watch_final
= true;
2229 event
->inotify_watch
= cur
->key
.watch
;
2231 case TK_A_DEVLINK_PRIO
:
2232 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2235 char owner
[UTIL_NAME_SIZE
];
2236 const char *ow
= owner
;
2239 if (event
->owner_final
)
2241 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2242 event
->owner_final
= true;
2243 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2244 event
->owner_set
= true;
2245 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2247 if (r
== -ENOENT
|| r
== -ESRCH
)
2248 log_error("specified user '%s' unknown", owner
);
2250 log_error_errno(r
, "error resolving user '%s': %m", owner
);
2254 log_debug("OWNER %u %s:%u",
2256 rules_str(rules
, rule
->rule
.filename_off
),
2257 rule
->rule
.filename_line
);
2261 char group
[UTIL_NAME_SIZE
];
2262 const char *gr
= group
;
2265 if (event
->group_final
)
2267 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2268 event
->group_final
= true;
2269 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2270 event
->group_set
= true;
2271 r
= get_group_creds(&gr
, &event
->gid
);
2273 if (r
== -ENOENT
|| r
== -ESRCH
)
2274 log_error("specified group '%s' unknown", group
);
2276 log_error_errno(r
, "error resolving group '%s': %m", group
);
2280 log_debug("GROUP %u %s:%u",
2282 rules_str(rules
, rule
->rule
.filename_off
),
2283 rule
->rule
.filename_line
);
2287 char mode_str
[UTIL_NAME_SIZE
];
2291 if (event
->mode_final
)
2293 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2294 mode
= strtol(mode_str
, &endptr
, 8);
2295 if (endptr
[0] != '\0') {
2296 log_error("ignoring invalid mode '%s'", mode_str
);
2299 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2300 event
->mode_final
= true;
2301 event
->mode_set
= true;
2303 log_debug("MODE %#o %s:%u",
2305 rules_str(rules
, rule
->rule
.filename_off
),
2306 rule
->rule
.filename_line
);
2310 if (event
->owner_final
)
2312 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2313 event
->owner_final
= true;
2314 event
->owner_set
= true;
2315 event
->uid
= cur
->key
.uid
;
2316 log_debug("OWNER %u %s:%u",
2318 rules_str(rules
, rule
->rule
.filename_off
),
2319 rule
->rule
.filename_line
);
2322 if (event
->group_final
)
2324 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2325 event
->group_final
= true;
2326 event
->group_set
= true;
2327 event
->gid
= cur
->key
.gid
;
2328 log_debug("GROUP %u %s:%u",
2330 rules_str(rules
, rule
->rule
.filename_off
),
2331 rule
->rule
.filename_line
);
2334 if (event
->mode_final
)
2336 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2337 event
->mode_final
= true;
2338 event
->mode_set
= true;
2339 event
->mode
= cur
->key
.mode
;
2340 log_debug("MODE %#o %s:%u",
2342 rules_str(rules
, rule
->rule
.filename_off
),
2343 rule
->rule
.filename_line
);
2345 case TK_A_SECLABEL
: {
2346 const char *name
, *label
;
2348 name
= rules_str(rules
, cur
->key
.attr_off
);
2349 label
= rules_str(rules
, cur
->key
.value_off
);
2350 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2351 udev_list_cleanup(&event
->seclabel_list
);
2352 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2353 log_debug("SECLABEL{%s}='%s' %s:%u",
2355 rules_str(rules
, rule
->rule
.filename_off
),
2356 rule
->rule
.filename_line
);
2360 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2361 char *value
= rules_str(rules
, cur
->key
.value_off
);
2362 char value_new
[UTIL_NAME_SIZE
];
2363 const char *value_old
= NULL
;
2365 if (value
[0] == '\0') {
2366 if (cur
->key
.op
== OP_ADD
)
2368 udev_device_add_property(event
->dev
, name
, NULL
);
2372 if (cur
->key
.op
== OP_ADD
)
2373 value_old
= udev_device_get_property_value(event
->dev
, name
);
2375 char temp
[UTIL_NAME_SIZE
];
2377 /* append value separated by space */
2378 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2379 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2381 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2383 udev_device_add_property(event
->dev
, name
, value_new
);
2387 char tag
[UTIL_PATH_SIZE
];
2390 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2391 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2392 udev_device_cleanup_tags_list(event
->dev
);
2393 for (p
= tag
; *p
!= '\0'; p
++) {
2394 if ((*p
>= 'a' && *p
<= 'z') ||
2395 (*p
>= 'A' && *p
<= 'Z') ||
2396 (*p
>= '0' && *p
<= '9') ||
2397 *p
== '-' || *p
== '_')
2399 log_error("ignoring invalid tag name '%s'", tag
);
2402 if (cur
->key
.op
== OP_REMOVE
)
2403 udev_device_remove_tag(event
->dev
, tag
);
2405 udev_device_add_tag(event
->dev
, tag
);
2409 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2411 char name_str
[UTIL_PATH_SIZE
];
2414 if (event
->name_final
)
2416 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2417 event
->name_final
= true;
2418 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2419 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2420 count
= util_replace_chars(name_str
, "/");
2422 log_debug("%i character(s) replaced", count
);
2424 if (major(udev_device_get_devnum(event
->dev
)) &&
2425 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2426 log_error("NAME=\"%s\" ignored, kernel device nodes "
2427 "can not be renamed; please fix it in %s:%u\n", name
,
2428 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2431 free_and_strdup(&event
->name
, name_str
);
2432 log_debug("NAME '%s' %s:%u",
2434 rules_str(rules
, rule
->rule
.filename_off
),
2435 rule
->rule
.filename_line
);
2438 case TK_A_DEVLINK
: {
2439 char temp
[UTIL_PATH_SIZE
];
2440 char filename
[UTIL_PATH_SIZE
];
2444 if (event
->devlink_final
)
2446 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2448 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2449 event
->devlink_final
= true;
2450 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2451 udev_device_cleanup_devlinks_list(event
->dev
);
2453 /* allow multiple symlinks separated by spaces */
2454 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2455 if (esc
== ESCAPE_UNSET
)
2456 count
= util_replace_chars(temp
, "/ ");
2457 else if (esc
== ESCAPE_REPLACE
)
2458 count
= util_replace_chars(temp
, "/");
2460 log_debug("%i character(s) replaced" , count
);
2462 while (isspace(pos
[0]))
2464 next
= strchr(pos
, ' ');
2465 while (next
!= NULL
) {
2467 log_debug("LINK '%s' %s:%u", pos
,
2468 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2469 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2470 udev_device_add_devlink(event
->dev
, filename
);
2471 while (isspace(next
[1]))
2474 next
= strchr(pos
, ' ');
2476 if (pos
[0] != '\0') {
2477 log_debug("LINK '%s' %s:%u", pos
,
2478 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2479 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2480 udev_device_add_devlink(event
->dev
, filename
);
2485 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2486 char attr
[UTIL_PATH_SIZE
];
2487 char value
[UTIL_NAME_SIZE
];
2490 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2491 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2492 attr_subst_subdir(attr
, sizeof(attr
));
2494 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2495 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2496 rules_str(rules
, rule
->rule
.filename_off
),
2497 rule
->rule
.filename_line
);
2498 f
= fopen(attr
, "we");
2500 if (fprintf(f
, "%s", value
) <= 0)
2501 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2504 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2509 char filename
[UTIL_PATH_SIZE
];
2510 char value
[UTIL_NAME_SIZE
];
2513 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
2514 sysctl_normalize(filename
);
2515 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2516 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2517 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2518 r
= sysctl_write(filename
, value
);
2520 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2523 case TK_A_RUN_BUILTIN
:
2524 case TK_A_RUN_PROGRAM
: {
2525 struct udev_list_entry
*entry
;
2527 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2528 udev_list_cleanup(&event
->run_list
);
2529 log_debug("RUN '%s' %s:%u",
2530 rules_str(rules
, cur
->key
.value_off
),
2531 rules_str(rules
, rule
->rule
.filename_off
),
2532 rule
->rule
.filename_line
);
2533 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2534 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2538 if (cur
->key
.rule_goto
== 0)
2540 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2545 case TK_M_PARENTS_MIN
:
2546 case TK_M_PARENTS_MAX
:
2549 log_error("wrong type %u", cur
->type
);
2556 /* fast-forward to next rule */
2557 cur
= rule
+ rule
->rule
.token_count
;
2561 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2567 _cleanup_strv_free_
char **tags
= NULL
;
2570 _cleanup_free_
char *path
= NULL
;
2573 if (rules
->tokens
== NULL
)
2576 cur
= &rules
->tokens
[0];
2579 switch (cur
->type
) {
2584 /* skip rules without a static_node tag */
2585 if (!rule
->rule
.has_static_node
)
2591 tags
= strv_free(tags
);
2600 mode
= cur
->key
.mode
;
2603 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2608 case TK_A_STATIC_NODE
: {
2609 char device_node
[UTIL_PATH_SIZE
];
2610 char tags_dir
[UTIL_PATH_SIZE
];
2611 char tag_symlink
[UTIL_PATH_SIZE
];
2614 /* we assure, that the permissions tokens are sorted before the static token */
2616 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2619 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2620 if (stat(device_node
, &stats
) != 0)
2622 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2625 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2627 STRV_FOREACH(t
, tags
) {
2628 _cleanup_free_
char *unescaped_filename
= NULL
;
2630 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2631 r
= mkdir_p(tags_dir
, 0755);
2633 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2635 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2637 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2638 r
= symlink(device_node
, tag_symlink
);
2639 if (r
< 0 && errno
!= EEXIST
)
2640 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2641 tag_symlink
, device_node
);
2647 /* don't touch the permissions if only the tags were set */
2648 if (mode
== 0 && uid
== 0 && gid
== 0)
2657 if (mode
!= (stats
.st_mode
& 01777)) {
2658 r
= chmod(device_node
, mode
);
2660 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2663 log_debug("chmod '%s' %#o", device_node
, mode
);
2666 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2667 r
= chown(device_node
, uid
, gid
);
2669 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2672 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2675 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2685 /* fast-forward to next rule */
2686 cur
= rule
+ rule
->rule
.token_count
;
2693 fchmod(fileno(f
), 0644);
2694 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2696 unlink("/run/udev/static_node-tags");