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"
39 #define PREALLOC_TOKEN 2048
42 unsigned int name_off
;
49 static const char* const rules_dirs
[] = {
52 UDEVLIBEXECDIR
"/rules.d",
60 /* every key in the rules file becomes a token */
62 unsigned int token_cur
;
63 unsigned int token_max
;
65 /* all key strings are copied and de-duplicated in a single continuous string buffer */
66 struct strbuf
*strbuf
;
68 /* during rule parsing, uid/gid lookup results are cached */
70 unsigned int uids_cur
;
71 unsigned int uids_max
;
73 unsigned int gids_cur
;
74 unsigned int gids_max
;
77 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
78 return rules
->strbuf
->buf
+ off
;
81 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
82 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
85 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
99 enum string_glob_type
{
101 GL_PLAIN
, /* no special chars */
102 GL_GLOB
, /* shell globs ?,*,[] */
103 GL_SPLIT
, /* multi-value A|B */
104 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
105 GL_SOMETHING
, /* commonly used "?*" */
108 enum string_subst_type
{
115 /* tokens of a rule are sorted/handled in this order */
120 TK_M_ACTION
, /* val */
121 TK_M_DEVPATH
, /* val */
122 TK_M_KERNEL
, /* val */
123 TK_M_DEVLINK
, /* val */
125 TK_M_ENV
, /* val, attr */
127 TK_M_SUBSYSTEM
, /* val */
128 TK_M_DRIVER
, /* val */
129 TK_M_WAITFOR
, /* val */
130 TK_M_ATTR
, /* val, attr */
133 TK_M_KERNELS
, /* val */
134 TK_M_SUBSYSTEMS
, /* val */
135 TK_M_DRIVERS
, /* val */
136 TK_M_ATTRS
, /* val, attr */
140 TK_M_TEST
, /* val, mode_t */
141 TK_M_PROGRAM
, /* val */
142 TK_M_IMPORT_FILE
, /* val */
143 TK_M_IMPORT_PROG
, /* val */
144 TK_M_IMPORT_BUILTIN
, /* val */
145 TK_M_IMPORT_DB
, /* val */
146 TK_M_IMPORT_CMDLINE
, /* val */
147 TK_M_IMPORT_PARENT
, /* val */
148 TK_M_RESULT
, /* val */
151 TK_A_STRING_ESCAPE_NONE
,
152 TK_A_STRING_ESCAPE_REPLACE
,
154 TK_A_INOTIFY_WATCH
, /* int */
155 TK_A_DEVLINK_PRIO
, /* int */
156 TK_A_OWNER
, /* val */
157 TK_A_GROUP
, /* val */
159 TK_A_OWNER_ID
, /* uid_t */
160 TK_A_GROUP_ID
, /* gid_t */
161 TK_A_MODE_ID
, /* mode_t */
163 TK_A_STATIC_NODE
, /* val */
164 TK_A_SECLABEL
, /* val, attr */
165 TK_A_ENV
, /* val, attr */
167 TK_A_DEVLINK
, /* val */
168 TK_A_ATTR
, /* val, attr */
169 TK_A_RUN_BUILTIN
, /* val, bool */
170 TK_A_RUN_PROGRAM
, /* val, bool */
171 TK_A_GOTO
, /* size_t */
176 /* we try to pack stuff in a way that we take only 12 bytes per token */
179 unsigned char type
; /* same in rule and key */
181 enum token_type type
:8;
183 bool has_static_node
:1;
184 unsigned int unused
:6;
185 unsigned short token_count
;
186 unsigned int label_off
;
187 unsigned short filename_off
;
188 unsigned short filename_line
;
191 enum token_type type
:8;
192 enum operation_type op
:8;
193 enum string_glob_type glob
:8;
194 enum string_subst_type subst
:4;
195 enum string_subst_type attrsubst
:4;
196 unsigned int value_off
;
198 unsigned int attr_off
;
199 unsigned int rule_goto
;
205 enum udev_builtin_cmd builtin_cmd
;
213 struct udev_rules
*rules
;
215 struct token token
[MAX_TK
];
216 unsigned int token_cur
;
220 static const char *operation_str(enum operation_type type
) {
221 static const char *operation_strs
[] = {
222 [OP_UNSET
] = "UNSET",
223 [OP_MATCH
] = "match",
224 [OP_NOMATCH
] = "nomatch",
225 [OP_MATCH_MAX
] = "MATCH_MAX",
228 [OP_REMOVE
] = "remove",
229 [OP_ASSIGN
] = "assign",
230 [OP_ASSIGN_FINAL
] = "assign-final",
233 return operation_strs
[type
];
236 static const char *string_glob_str(enum string_glob_type type
) {
237 static const char *string_glob_strs
[] = {
238 [GL_UNSET
] = "UNSET",
239 [GL_PLAIN
] = "plain",
241 [GL_SPLIT
] = "split",
242 [GL_SPLIT_GLOB
] = "split-glob",
243 [GL_SOMETHING
] = "split-glob",
246 return string_glob_strs
[type
];
249 static const char *token_str(enum token_type type
) {
250 static const char *token_strs
[] = {
251 [TK_UNSET
] = "UNSET",
254 [TK_M_ACTION
] = "M ACTION",
255 [TK_M_DEVPATH
] = "M DEVPATH",
256 [TK_M_KERNEL
] = "M KERNEL",
257 [TK_M_DEVLINK
] = "M DEVLINK",
258 [TK_M_NAME
] = "M NAME",
259 [TK_M_ENV
] = "M ENV",
260 [TK_M_TAG
] = "M TAG",
261 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
262 [TK_M_DRIVER
] = "M DRIVER",
263 [TK_M_WAITFOR
] = "M WAITFOR",
264 [TK_M_ATTR
] = "M ATTR",
266 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
267 [TK_M_KERNELS
] = "M KERNELS",
268 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
269 [TK_M_DRIVERS
] = "M DRIVERS",
270 [TK_M_ATTRS
] = "M ATTRS",
271 [TK_M_TAGS
] = "M TAGS",
272 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
274 [TK_M_TEST
] = "M TEST",
275 [TK_M_PROGRAM
] = "M PROGRAM",
276 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
277 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
278 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
279 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
280 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
281 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
282 [TK_M_RESULT
] = "M RESULT",
283 [TK_M_MAX
] = "M MAX",
285 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
286 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
287 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
288 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
289 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
290 [TK_A_OWNER
] = "A OWNER",
291 [TK_A_GROUP
] = "A GROUP",
292 [TK_A_MODE
] = "A MODE",
293 [TK_A_OWNER_ID
] = "A OWNER_ID",
294 [TK_A_GROUP_ID
] = "A GROUP_ID",
295 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
296 [TK_A_SECLABEL
] = "A SECLABEL",
297 [TK_A_MODE_ID
] = "A MODE_ID",
298 [TK_A_ENV
] = "A ENV",
299 [TK_A_TAG
] = "A ENV",
300 [TK_A_NAME
] = "A NAME",
301 [TK_A_DEVLINK
] = "A DEVLINK",
302 [TK_A_ATTR
] = "A ATTR",
303 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
304 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
305 [TK_A_GOTO
] = "A GOTO",
310 return token_strs
[type
];
313 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
314 enum token_type type
= token
->type
;
315 enum operation_type op
= token
->key
.op
;
316 enum string_glob_type glob
= token
->key
.glob
;
317 const char *value
= str(rules
, token
->key
.value_off
);
318 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
323 const char *tks_ptr
= (char *)rules
->tokens
;
324 const char *tk_ptr
= (char *)token
;
325 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
327 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
328 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
329 idx
, token
->rule
.token_count
,
330 &rules
->buf
[token
->rule
.label_off
]);
342 case TK_M_SUBSYSTEMS
:
346 case TK_M_IMPORT_FILE
:
347 case TK_M_IMPORT_PROG
:
349 case TK_M_IMPORT_CMDLINE
:
350 case TK_M_IMPORT_PARENT
:
357 case TK_A_RUN_BUILTIN
:
358 case TK_A_RUN_PROGRAM
:
359 log_debug("%s %s '%s'(%s)",
360 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
362 case TK_M_IMPORT_BUILTIN
:
363 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
370 log_debug("%s %s '%s' '%s'(%s)",
371 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
375 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
377 case TK_A_STRING_ESCAPE_NONE
:
378 case TK_A_STRING_ESCAPE_REPLACE
:
379 case TK_A_DB_PERSIST
:
380 log_debug("%s", token_str(type
));
383 log_debug("%s %s '%s'(%s) %#o",
384 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
386 case TK_A_INOTIFY_WATCH
:
387 log_debug("%s %u", token_str(type
), token
->key
.watch
);
389 case TK_A_DEVLINK_PRIO
:
390 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
393 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
396 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
399 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
401 case TK_A_STATIC_NODE
:
402 log_debug("%s '%s'", token_str(type
), value
);
405 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
408 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
411 log_debug("* %s", token_str(type
));
413 case TK_M_PARENTS_MIN
:
414 case TK_M_PARENTS_MAX
:
417 log_debug("unknown type %u", type
);
422 static void dump_rules(struct udev_rules
*rules
) {
425 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
427 rules
->token_cur
* sizeof(struct token
),
430 for (i
= 0; i
< rules
->token_cur
; i
++)
431 dump_token(rules
, &rules
->tokens
[i
]);
434 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
435 static inline void dump_rules(struct udev_rules
*rules
) {}
438 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
439 /* grow buffer if needed */
440 if (rules
->token_cur
+1 >= rules
->token_max
) {
441 struct token
*tokens
;
444 /* double the buffer size */
445 add
= rules
->token_max
;
449 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
452 rules
->tokens
= tokens
;
453 rules
->token_max
+= add
;
455 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
460 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
465 /* lookup, if we know it already */
466 for (i
= 0; i
< rules
->uids_cur
; i
++) {
467 off
= rules
->uids
[i
].name_off
;
468 if (streq(rules_str(rules
, off
), owner
)) {
469 uid
= rules
->uids
[i
].uid
;
473 uid
= util_lookup_user(rules
->udev
, owner
);
475 /* grow buffer if needed */
476 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
477 struct uid_gid
*uids
;
480 /* double the buffer size */
481 add
= rules
->uids_max
;
485 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
489 rules
->uids_max
+= add
;
491 rules
->uids
[rules
->uids_cur
].uid
= uid
;
492 off
= rules_add_string(rules
, owner
);
495 rules
->uids
[rules
->uids_cur
].name_off
= off
;
500 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
505 /* lookup, if we know it already */
506 for (i
= 0; i
< rules
->gids_cur
; i
++) {
507 off
= rules
->gids
[i
].name_off
;
508 if (streq(rules_str(rules
, off
), group
)) {
509 gid
= rules
->gids
[i
].gid
;
513 gid
= util_lookup_group(rules
->udev
, group
);
515 /* grow buffer if needed */
516 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
517 struct uid_gid
*gids
;
520 /* double the buffer size */
521 add
= rules
->gids_max
;
525 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
529 rules
->gids_max
+= add
;
531 rules
->gids
[rules
->gids_cur
].gid
= gid
;
532 off
= rules_add_string(rules
, group
);
535 rules
->gids
[rules
->gids_cur
].name_off
= off
;
540 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
544 struct udev_list_entry
*entry
;
548 while (isspace(key
[0]))
551 /* comment or empty line */
552 if (key
[0] == '#' || key
[0] == '\0')
555 /* split key/value */
556 val
= strchr(key
, '=');
563 while (isspace(val
[0]))
570 while (isspace(key
[len
-1]))
574 /* terminate value */
578 while (isspace(val
[len
-1]))
586 if (val
[0] == '"' || val
[0] == '\'') {
587 if (val
[len
-1] != val
[0]) {
588 log_debug("inconsistent quoting: '%s', skip", line
);
595 entry
= udev_device_add_property(dev
, key
, val
);
596 /* store in db, skip private keys */
598 udev_list_entry_set_num(entry
, true);
603 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
605 char line
[UTIL_LINE_SIZE
];
607 f
= fopen(filename
, "re");
610 while (fgets(line
, sizeof(line
), f
) != NULL
)
611 import_property_from_string(dev
, line
);
616 static int import_program_into_properties(struct udev_event
*event
,
618 usec_t timeout_warn_usec
,
619 const char *program
, const sigset_t
*sigmask
) {
620 struct udev_device
*dev
= event
->dev
;
622 char result
[UTIL_LINE_SIZE
];
626 envp
= udev_device_get_properties_envp(dev
);
627 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, program
, envp
, sigmask
, result
, sizeof(result
));
632 while (line
!= NULL
) {
635 pos
= strchr(line
, '\n');
640 import_property_from_string(dev
, line
);
646 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
647 struct udev_device
*dev_parent
;
648 struct udev_list_entry
*list_entry
;
650 dev_parent
= udev_device_get_parent(dev
);
651 if (dev_parent
== NULL
)
654 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
655 const char *key
= udev_list_entry_get_name(list_entry
);
656 const char *val
= udev_list_entry_get_value(list_entry
);
658 if (fnmatch(filter
, key
, 0) == 0) {
659 struct udev_list_entry
*entry
;
661 entry
= udev_device_add_property(dev
, key
, val
);
662 /* store in db, skip private keys */
664 udev_list_entry_set_num(entry
, true);
670 #define WAIT_LOOP_PER_SECOND 50
671 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
) {
672 char filepath
[UTIL_PATH_SIZE
];
673 char devicepath
[UTIL_PATH_SIZE
];
675 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
677 /* a relative path is a device attribute */
678 devicepath
[0] = '\0';
679 if (file
[0] != '/') {
680 strscpyl(devicepath
, sizeof(devicepath
), udev_device_get_syspath(dev
), NULL
);
681 strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
686 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
689 if (stat(file
, &stats
) == 0) {
690 log_debug("file '%s' appeared after %i loops", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
693 /* make sure, the device did not disappear in the meantime */
694 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
695 log_debug("device disappeared while waiting for '%s'", file
);
698 log_debug("wait for '%s' for %i mseconds", file
, 1000 / WAIT_LOOP_PER_SECOND
);
699 nanosleep(&duration
, NULL
);
701 log_debug("waiting for '%s' failed", file
);
705 static int attr_subst_subdir(char *attr
, size_t len
) {
708 if (strstr(attr
, "/*/")) {
710 char dirname
[UTIL_PATH_SIZE
];
714 strscpy(dirname
, sizeof(dirname
), attr
);
715 pos
= strstr(dirname
, "/*/");
720 dir
= opendir(dirname
);
724 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
727 if (dent
->d_name
[0] == '.')
729 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
730 if (stat(attr
, &stats
) == 0) {
742 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
747 if (linepos
== NULL
|| linepos
[0] == '\0')
750 /* skip whitespace */
751 while (isspace(linepos
[0]) || linepos
[0] == ',')
755 if (linepos
[0] == '\0')
761 if (linepos
[0] == '\0')
763 if (isspace(linepos
[0]))
765 if (linepos
[0] == '=')
767 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
768 if (linepos
[1] == '=')
772 /* remember end of key */
775 /* skip whitespace after key */
776 while (isspace(linepos
[0]))
778 if (linepos
[0] == '\0')
781 /* get operation type */
782 if (linepos
[0] == '=' && linepos
[1] == '=') {
785 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
788 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
791 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
794 } else if (linepos
[0] == '=') {
797 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
798 *op
= OP_ASSIGN_FINAL
;
806 /* skip whitespace after operator */
807 while (isspace(linepos
[0]))
809 if (linepos
[0] == '\0')
813 if (linepos
[0] == '"')
820 temp
= strchr(linepos
, '"');
826 /* move line to next key */
831 /* extract possible KEY{attr} */
832 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
836 attr
= strchr(str
, '{');
839 pos
= strchr(attr
, '}');
841 log_error("missing closing brace for format");
850 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
851 enum operation_type op
,
852 const char *value
, const void *data
) {
853 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
854 const char *attr
= NULL
;
856 memzero(token
, sizeof(struct token
));
868 case TK_M_SUBSYSTEMS
:
872 case TK_M_IMPORT_FILE
:
873 case TK_M_IMPORT_PROG
:
875 case TK_M_IMPORT_CMDLINE
:
876 case TK_M_IMPORT_PARENT
:
886 case TK_A_STATIC_NODE
:
887 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
889 case TK_M_IMPORT_BUILTIN
:
890 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
891 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
900 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
901 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
904 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
906 token
->key
.mode
= *(mode_t
*)data
;
908 case TK_A_STRING_ESCAPE_NONE
:
909 case TK_A_STRING_ESCAPE_REPLACE
:
910 case TK_A_DB_PERSIST
:
912 case TK_A_RUN_BUILTIN
:
913 case TK_A_RUN_PROGRAM
:
914 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
915 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
917 case TK_A_INOTIFY_WATCH
:
918 case TK_A_DEVLINK_PRIO
:
919 token
->key
.devlink_prio
= *(int *)data
;
922 token
->key
.uid
= *(uid_t
*)data
;
925 token
->key
.gid
= *(gid_t
*)data
;
928 token
->key
.mode
= *(mode_t
*)data
;
931 case TK_M_PARENTS_MIN
:
932 case TK_M_PARENTS_MAX
:
936 log_error("wrong type %u", type
);
940 if (value
!= NULL
&& type
< TK_M_MAX
) {
941 /* check if we need to split or call fnmatch() while matching rules */
942 enum string_glob_type glob
;
946 has_split
= (strchr(value
, '|') != NULL
);
947 has_glob
= string_is_glob(value
);
948 if (has_split
&& has_glob
) {
949 glob
= GL_SPLIT_GLOB
;
950 } else if (has_split
) {
952 } else if (has_glob
) {
953 if (streq(value
, "?*"))
960 token
->key
.glob
= glob
;
963 if (value
!= NULL
&& type
> TK_M_MAX
) {
964 /* check if assigned value has substitution chars */
966 token
->key
.subst
= SB_SUBSYS
;
967 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
968 token
->key
.subst
= SB_FORMAT
;
970 token
->key
.subst
= SB_NONE
;
974 /* check if property/attribute name has substitution chars */
976 token
->key
.attrsubst
= SB_SUBSYS
;
977 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
978 token
->key
.attrsubst
= SB_FORMAT
;
980 token
->key
.attrsubst
= SB_NONE
;
983 token
->key
.type
= type
;
985 rule_tmp
->token_cur
++;
986 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
987 log_error("temporary rule array too small");
993 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
995 unsigned int start
= 0;
996 unsigned int end
= rule_tmp
->token_cur
;
998 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
999 enum token_type next_val
= TK_UNSET
;
1000 unsigned int next_idx
= 0;
1003 /* find smallest value */
1004 for (j
= start
; j
< end
; j
++) {
1005 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1007 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1008 next_val
= rule_tmp
->token
[j
].type
;
1013 /* add token and mark done */
1014 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1016 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1019 if (next_idx
== start
)
1021 if (next_idx
+1 == end
)
1027 static int add_rule(struct udev_rules
*rules
, char *line
,
1028 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1031 struct rule_tmp rule_tmp
;
1033 memzero(&rule_tmp
, sizeof(struct rule_tmp
));
1034 rule_tmp
.rules
= rules
;
1035 rule_tmp
.rule
.type
= TK_RULE
;
1036 /* the offset in the rule is limited to unsigned short */
1037 if (filename_off
< USHRT_MAX
)
1038 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1039 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1045 enum operation_type op
;
1047 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1048 /* Avoid erroring on trailing whitespace. This is probably rare
1049 * so save the work for the error case instead of always trying
1050 * to strip the trailing whitespace with strstrip(). */
1051 while (isblank(*linepos
))
1054 /* If we aren't at the end of the line, this is a parsing error.
1055 * Make a best effort to describe where the problem is. */
1056 if (*linepos
!= '\n') {
1057 char buf
[2] = {linepos
[1]};
1058 _cleanup_free_
char *tmp
;
1061 log_error("invalid key/value pair in file %s on line %u,"
1062 "starting at character %tu ('%s')\n",
1063 filename
, lineno
, linepos
- line
+ 1, tmp
);
1064 if (linepos
[1] == '#')
1065 log_error("hint: comments can only start at beginning of line");
1070 if (streq(key
, "ACTION")) {
1071 if (op
> OP_MATCH_MAX
) {
1072 log_error("invalid ACTION operation");
1075 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1079 if (streq(key
, "DEVPATH")) {
1080 if (op
> OP_MATCH_MAX
) {
1081 log_error("invalid DEVPATH operation");
1084 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1088 if (streq(key
, "KERNEL")) {
1089 if (op
> OP_MATCH_MAX
) {
1090 log_error("invalid KERNEL operation");
1093 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1097 if (streq(key
, "SUBSYSTEM")) {
1098 if (op
> OP_MATCH_MAX
) {
1099 log_error("invalid SUBSYSTEM operation");
1102 /* bus, class, subsystem events should all be the same */
1103 if (streq(value
, "subsystem") ||
1104 streq(value
, "bus") ||
1105 streq(value
, "class")) {
1106 if (streq(value
, "bus") || streq(value
, "class"))
1107 log_error("'%s' must be specified as 'subsystem' "
1108 "please fix it in %s:%u", value
, filename
, lineno
);
1109 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1111 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1115 if (streq(key
, "DRIVER")) {
1116 if (op
> OP_MATCH_MAX
) {
1117 log_error("invalid DRIVER operation");
1120 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1124 if (startswith(key
, "ATTR{")) {
1125 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1127 log_error("error parsing ATTR attribute");
1130 if (op
== OP_REMOVE
) {
1131 log_error("invalid ATTR operation");
1134 if (op
< OP_MATCH_MAX
) {
1135 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1137 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1142 if (startswith(key
, "SECLABEL{")) {
1143 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1145 log_error("error parsing SECLABEL attribute");
1148 if (op
== OP_REMOVE
) {
1149 log_error("invalid SECLABEL operation");
1153 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1157 if (streq(key
, "KERNELS")) {
1158 if (op
> OP_MATCH_MAX
) {
1159 log_error("invalid KERNELS operation");
1162 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1166 if (streq(key
, "SUBSYSTEMS")) {
1167 if (op
> OP_MATCH_MAX
) {
1168 log_error("invalid SUBSYSTEMS operation");
1171 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1175 if (streq(key
, "DRIVERS")) {
1176 if (op
> OP_MATCH_MAX
) {
1177 log_error("invalid DRIVERS operation");
1180 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1184 if (startswith(key
, "ATTRS{")) {
1185 if (op
> OP_MATCH_MAX
) {
1186 log_error("invalid ATTRS operation");
1189 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1191 log_error("error parsing ATTRS attribute");
1194 if (startswith(attr
, "device/"))
1195 log_error("the 'device' link may not be available in a future kernel, "
1196 "please fix it in %s:%u", filename
, lineno
);
1197 else if (strstr(attr
, "../") != NULL
)
1198 log_error("do not reference parent sysfs directories directly, "
1199 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1200 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1204 if (streq(key
, "TAGS")) {
1205 if (op
> OP_MATCH_MAX
) {
1206 log_error("invalid TAGS operation");
1209 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1213 if (startswith(key
, "ENV{")) {
1214 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1216 log_error("error parsing ENV attribute");
1219 if (op
== OP_REMOVE
) {
1220 log_error("invalid ENV operation");
1223 if (op
< OP_MATCH_MAX
) {
1224 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1227 static const char *blacklist
[] = {
1242 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1243 if (!streq(attr
, blacklist
[i
]))
1245 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1248 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1254 if (streq(key
, "TAG")) {
1255 if (op
< OP_MATCH_MAX
)
1256 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1258 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1262 if (streq(key
, "PROGRAM")) {
1263 if (op
== OP_REMOVE
) {
1264 log_error("invalid PROGRAM operation");
1267 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1271 if (streq(key
, "RESULT")) {
1272 if (op
> OP_MATCH_MAX
) {
1273 log_error("invalid RESULT operation");
1276 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1280 if (startswith(key
, "IMPORT")) {
1281 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1283 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1286 if (op
== OP_REMOVE
) {
1287 log_error("invalid IMPORT operation");
1290 if (streq(attr
, "program")) {
1291 /* find known built-in command */
1292 if (value
[0] != '/') {
1293 enum udev_builtin_cmd cmd
;
1295 cmd
= udev_builtin_lookup(value
);
1296 if (cmd
< UDEV_BUILTIN_MAX
) {
1297 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1298 value
, filename
, lineno
);
1299 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1303 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1304 } else if (streq(attr
, "builtin")) {
1305 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1307 if (cmd
< UDEV_BUILTIN_MAX
)
1308 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1310 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1311 } else if (streq(attr
, "file")) {
1312 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1313 } else if (streq(attr
, "db")) {
1314 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1315 } else if (streq(attr
, "cmdline")) {
1316 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1317 } else if (streq(attr
, "parent")) {
1318 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1320 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1324 if (startswith(key
, "TEST")) {
1327 if (op
> OP_MATCH_MAX
) {
1328 log_error("invalid TEST operation");
1331 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1333 mode
= strtol(attr
, NULL
, 8);
1334 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1336 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1341 if (startswith(key
, "RUN")) {
1342 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1345 if (op
== OP_REMOVE
) {
1346 log_error("invalid RUN operation");
1350 if (streq(attr
, "builtin")) {
1351 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1353 if (cmd
< UDEV_BUILTIN_MAX
)
1354 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1356 log_error("RUN{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1357 } else if (streq(attr
, "program")) {
1358 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1360 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1362 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1368 if (streq(key
, "WAIT_FOR") || streq(key
, "WAIT_FOR_SYSFS")) {
1369 if (op
== OP_REMOVE
) {
1370 log_error("invalid WAIT_FOR/WAIT_FOR_SYSFS operation");
1373 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1377 if (streq(key
, "LABEL")) {
1378 if (op
== OP_REMOVE
) {
1379 log_error("invalid LABEL operation");
1382 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1386 if (streq(key
, "GOTO")) {
1387 if (op
== OP_REMOVE
) {
1388 log_error("invalid GOTO operation");
1391 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1395 if (startswith(key
, "NAME")) {
1396 if (op
== OP_REMOVE
) {
1397 log_error("invalid NAME operation");
1400 if (op
< OP_MATCH_MAX
) {
1401 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1403 if (streq(value
, "%k")) {
1404 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1405 "please remove it from %s:%u\n", filename
, lineno
);
1408 if (value
[0] == '\0') {
1409 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1410 "please remove it from %s:%u\n", filename
, lineno
);
1413 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1415 rule_tmp
.rule
.rule
.can_set_name
= true;
1419 if (streq(key
, "SYMLINK")) {
1420 if (op
== OP_REMOVE
) {
1421 log_error("invalid SYMLINK operation");
1424 if (op
< OP_MATCH_MAX
)
1425 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1427 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1428 rule_tmp
.rule
.rule
.can_set_name
= true;
1432 if (streq(key
, "OWNER")) {
1436 if (op
== OP_REMOVE
) {
1437 log_error("invalid OWNER operation");
1441 uid
= strtoul(value
, &endptr
, 10);
1442 if (endptr
[0] == '\0') {
1443 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1444 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1445 uid
= add_uid(rules
, value
);
1446 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1447 } else if (rules
->resolve_names
>= 0) {
1448 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1450 rule_tmp
.rule
.rule
.can_set_name
= true;
1454 if (streq(key
, "GROUP")) {
1458 if (op
== OP_REMOVE
) {
1459 log_error("invalid GROUP operation");
1463 gid
= strtoul(value
, &endptr
, 10);
1464 if (endptr
[0] == '\0') {
1465 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1466 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1467 gid
= add_gid(rules
, value
);
1468 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1469 } else if (rules
->resolve_names
>= 0) {
1470 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1472 rule_tmp
.rule
.rule
.can_set_name
= true;
1476 if (streq(key
, "MODE")) {
1480 if (op
== OP_REMOVE
) {
1481 log_error("invalid MODE operation");
1485 mode
= strtol(value
, &endptr
, 8);
1486 if (endptr
[0] == '\0')
1487 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1489 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1490 rule_tmp
.rule
.rule
.can_set_name
= true;
1494 if (streq(key
, "OPTIONS")) {
1497 if (op
== OP_REMOVE
) {
1498 log_error("invalid OPTIONS operation");
1502 pos
= strstr(value
, "link_priority=");
1504 int prio
= atoi(&pos
[strlen("link_priority=")]);
1506 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1509 pos
= strstr(value
, "string_escape=");
1511 pos
= &pos
[strlen("string_escape=")];
1512 if (startswith(pos
, "none"))
1513 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1514 else if (startswith(pos
, "replace"))
1515 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1518 pos
= strstr(value
, "db_persist");
1520 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1522 pos
= strstr(value
, "nowatch");
1526 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1528 pos
= strstr(value
, "watch");
1532 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1536 pos
= strstr(value
, "static_node=");
1538 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1539 rule_tmp
.rule
.rule
.has_static_node
= true;
1545 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1549 /* add rule token */
1550 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1551 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1554 /* add tokens to list, sorted by type */
1555 if (sort_token(rules
, &rule_tmp
) != 0)
1560 log_error("invalid rule '%s:%u'", filename
, lineno
);
1564 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1566 unsigned int first_token
;
1567 unsigned int filename_off
;
1568 char line
[UTIL_LINE_SIZE
];
1572 f
= fopen(filename
, "re");
1574 if (errno
== ENOENT
)
1580 if (null_or_empty_fd(fileno(f
))) {
1581 log_debug("Skipping empty file: %s", filename
);
1584 log_debug("Reading rules file: %s", filename
);
1586 first_token
= rules
->token_cur
;
1587 filename_off
= rules_add_string(rules
, filename
);
1589 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1593 /* skip whitespace */
1596 while (isspace(key
[0]))
1607 /* continue reading if backslash+newline is found */
1608 while (line
[len
-2] == '\\') {
1609 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1611 if (strlen(&line
[len
-2]) < 2)
1617 if (len
+1 >= sizeof(line
)) {
1618 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1621 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1625 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1626 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1627 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1628 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1631 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1632 if (rules
->tokens
[j
].type
!= TK_RULE
)
1634 if (rules
->tokens
[j
].rule
.label_off
== 0)
1636 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1638 rules
->tokens
[i
].key
.rule_goto
= j
;
1641 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1642 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1648 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1649 struct udev_rules
*rules
;
1650 struct udev_list file_list
;
1651 struct token end_token
;
1655 rules
= new0(struct udev_rules
, 1);
1659 rules
->resolve_names
= resolve_names
;
1660 udev_list_init(udev
, &file_list
, true);
1662 /* init token array and string buffer */
1663 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1664 if (rules
->tokens
== NULL
)
1665 return udev_rules_unref(rules
);
1666 rules
->token_max
= PREALLOC_TOKEN
;
1668 rules
->strbuf
= strbuf_new();
1670 return udev_rules_unref(rules
);
1672 udev_rules_check_timestamp(rules
);
1674 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1676 log_error("failed to enumerate rules files: %s", strerror(-r
));
1677 return udev_rules_unref(rules
);
1681 * The offset value in the rules strct is limited; add all
1682 * rules file names to the beginning of the string buffer.
1684 STRV_FOREACH(f
, files
)
1685 rules_add_string(rules
, *f
);
1687 STRV_FOREACH(f
, files
)
1688 parse_file(rules
, *f
);
1692 memzero(&end_token
, sizeof(struct token
));
1693 end_token
.type
= TK_END
;
1694 add_token(rules
, &end_token
);
1695 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1696 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1698 /* cleanup temporary strbuf data */
1699 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1700 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1701 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1702 strbuf_complete(rules
->strbuf
);
1704 /* cleanup uid/gid cache */
1707 rules
->uids_cur
= 0;
1708 rules
->uids_max
= 0;
1711 rules
->gids_cur
= 0;
1712 rules
->gids_max
= 0;
1718 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1721 free(rules
->tokens
);
1722 strbuf_cleanup(rules
->strbuf
);
1729 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1733 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1736 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1737 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1744 switch (token
->key
.glob
) {
1746 match
= (streq(key_value
, val
));
1749 match
= (fnmatch(key_value
, val
, 0) == 0);
1756 s
= rules_str(rules
, token
->key
.value_off
);
1761 next
= strchr(s
, '|');
1763 size_t matchlen
= (size_t)(next
- s
);
1765 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1769 match
= (streq(s
, val
));
1778 char value
[UTIL_PATH_SIZE
];
1780 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1782 while (key_value
!= NULL
) {
1783 pos
= strchr(key_value
, '|');
1788 match
= (fnmatch(key_value
, val
, 0) == 0);
1796 match
= (val
[0] != '\0');
1802 if (match
&& (token
->key
.op
== OP_MATCH
))
1804 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1809 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1811 char nbuf
[UTIL_NAME_SIZE
];
1813 char vbuf
[UTIL_NAME_SIZE
];
1816 name
= rules_str(rules
, cur
->key
.attr_off
);
1817 switch (cur
->key
.attrsubst
) {
1819 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1823 value
= udev_device_get_sysattr_value(dev
, name
);
1828 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1836 /* remove trailing whitespace, if not asked to match for it */
1837 len
= strlen(value
);
1838 if (len
> 0 && isspace(value
[len
-1])) {
1839 const char *key_value
;
1842 key_value
= rules_str(rules
, cur
->key
.value_off
);
1843 klen
= strlen(key_value
);
1844 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1845 if (value
!= vbuf
) {
1846 strscpy(vbuf
, sizeof(vbuf
), value
);
1849 while (len
> 0 && isspace(vbuf
[--len
]))
1854 return match_key(rules
, cur
, value
);
1863 int udev_rules_apply_to_event(struct udev_rules
*rules
,
1864 struct udev_event
*event
,
1865 usec_t timeout_usec
,
1866 usec_t timeout_warn_usec
,
1867 const sigset_t
*sigmask
) {
1870 enum escape_type esc
= ESCAPE_UNSET
;
1873 if (rules
->tokens
== NULL
)
1876 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1877 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1878 udev_device_get_ifindex(event
->dev
) > 0));
1880 /* loop through token list, match, run actions or forward to next rule */
1881 cur
= &rules
->tokens
[0];
1884 dump_token(rules
, cur
);
1885 switch (cur
->type
) {
1889 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1890 if (!can_set_name
&& rule
->rule
.can_set_name
)
1895 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1899 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1903 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1906 case TK_M_DEVLINK
: {
1907 struct udev_list_entry
*list_entry
;
1910 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1911 const char *devlink
;
1913 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1914 if (match_key(rules
, cur
, devlink
) == 0) {
1924 if (match_key(rules
, cur
, event
->name
) != 0)
1928 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1931 value
= udev_device_get_property_value(event
->dev
, key_name
);
1934 if (match_key(rules
, cur
, value
))
1939 struct udev_list_entry
*list_entry
;
1942 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1943 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1948 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
1952 case TK_M_SUBSYSTEM
:
1953 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1957 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1960 case TK_M_WAITFOR
: {
1961 char filename
[UTIL_PATH_SIZE
];
1964 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
1965 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
1966 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
1971 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1975 case TK_M_SUBSYSTEMS
:
1981 /* get whole sequence of parent matches */
1983 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1986 /* loop over parents */
1987 event
->dev_parent
= event
->dev
;
1991 /* loop over sequence of parent match keys */
1992 for (key
= cur
; key
< next
; key
++ ) {
1993 dump_token(rules
, key
);
1996 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1999 case TK_M_SUBSYSTEMS
:
2000 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2004 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2008 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2012 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
2014 if (match
&& key
->key
.op
== OP_NOMATCH
)
2016 if (!match
&& key
->key
.op
== OP_MATCH
)
2027 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2028 if (event
->dev_parent
== NULL
)
2031 /* move behind our sequence of parent match keys */
2036 char filename
[UTIL_PATH_SIZE
];
2037 struct stat statbuf
;
2040 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2041 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2042 if (filename
[0] != '/') {
2043 char tmp
[UTIL_PATH_SIZE
];
2045 strscpy(tmp
, sizeof(tmp
), filename
);
2046 strscpyl(filename
, sizeof(filename
),
2047 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2050 attr_subst_subdir(filename
, sizeof(filename
));
2052 match
= (stat(filename
, &statbuf
) == 0);
2053 if (match
&& cur
->key
.mode
> 0)
2054 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2055 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2057 if (!match
&& cur
->key
.op
== OP_MATCH
)
2061 case TK_M_PROGRAM
: {
2062 char program
[UTIL_PATH_SIZE
];
2064 char result
[UTIL_LINE_SIZE
];
2066 free(event
->program_result
);
2067 event
->program_result
= NULL
;
2068 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2069 envp
= udev_device_get_properties_envp(event
->dev
);
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
, program
, envp
, sigmask
, 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
, sigmask
) != 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
);
2157 if (value
!= NULL
) {
2158 struct udev_list_entry
*entry
;
2160 entry
= udev_device_add_property(event
->dev
, key
, value
);
2161 udev_list_entry_set_num(entry
, true);
2163 if (cur
->key
.op
!= OP_NOMATCH
)
2168 case TK_M_IMPORT_CMDLINE
: {
2170 bool imported
= false;
2172 f
= fopen("/proc/cmdline", "re");
2176 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2177 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2180 pos
= strstr(cmdline
, key
);
2182 struct udev_list_entry
*entry
;
2185 if (pos
[0] == '\0' || isspace(pos
[0])) {
2186 /* we import simple flags as 'FLAG=1' */
2187 entry
= udev_device_add_property(event
->dev
, key
, "1");
2188 udev_list_entry_set_num(entry
, true);
2190 } else if (pos
[0] == '=') {
2195 while (pos
[0] != '\0' && !isspace(pos
[0]))
2198 entry
= udev_device_add_property(event
->dev
, key
, value
);
2199 udev_list_entry_set_num(entry
, true);
2206 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2210 case TK_M_IMPORT_PARENT
: {
2211 char import
[UTIL_PATH_SIZE
];
2213 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2214 if (import_parent_into_properties(event
->dev
, import
) != 0)
2215 if (cur
->key
.op
!= OP_NOMATCH
)
2220 if (match_key(rules
, cur
, event
->program_result
) != 0)
2223 case TK_A_STRING_ESCAPE_NONE
:
2226 case TK_A_STRING_ESCAPE_REPLACE
:
2227 esc
= ESCAPE_REPLACE
;
2229 case TK_A_DB_PERSIST
:
2230 udev_device_set_db_persist(event
->dev
);
2232 case TK_A_INOTIFY_WATCH
:
2233 if (event
->inotify_watch_final
)
2235 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2236 event
->inotify_watch_final
= true;
2237 event
->inotify_watch
= cur
->key
.watch
;
2239 case TK_A_DEVLINK_PRIO
:
2240 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2243 char owner
[UTIL_NAME_SIZE
];
2245 if (event
->owner_final
)
2247 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2248 event
->owner_final
= true;
2249 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2250 event
->owner_set
= true;
2251 event
->uid
= util_lookup_user(event
->udev
, owner
);
2252 log_debug("OWNER %u %s:%u",
2254 rules_str(rules
, rule
->rule
.filename_off
),
2255 rule
->rule
.filename_line
);
2259 char group
[UTIL_NAME_SIZE
];
2261 if (event
->group_final
)
2263 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2264 event
->group_final
= true;
2265 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2266 event
->group_set
= true;
2267 event
->gid
= util_lookup_group(event
->udev
, group
);
2268 log_debug("GROUP %u %s:%u",
2270 rules_str(rules
, rule
->rule
.filename_off
),
2271 rule
->rule
.filename_line
);
2275 char mode_str
[UTIL_NAME_SIZE
];
2279 if (event
->mode_final
)
2281 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2282 mode
= strtol(mode_str
, &endptr
, 8);
2283 if (endptr
[0] != '\0') {
2284 log_error("ignoring invalid mode '%s'", mode_str
);
2287 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2288 event
->mode_final
= true;
2289 event
->mode_set
= true;
2291 log_debug("MODE %#o %s:%u",
2293 rules_str(rules
, rule
->rule
.filename_off
),
2294 rule
->rule
.filename_line
);
2298 if (event
->owner_final
)
2300 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2301 event
->owner_final
= true;
2302 event
->owner_set
= true;
2303 event
->uid
= cur
->key
.uid
;
2304 log_debug("OWNER %u %s:%u",
2306 rules_str(rules
, rule
->rule
.filename_off
),
2307 rule
->rule
.filename_line
);
2310 if (event
->group_final
)
2312 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2313 event
->group_final
= true;
2314 event
->group_set
= true;
2315 event
->gid
= cur
->key
.gid
;
2316 log_debug("GROUP %u %s:%u",
2318 rules_str(rules
, rule
->rule
.filename_off
),
2319 rule
->rule
.filename_line
);
2322 if (event
->mode_final
)
2324 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2325 event
->mode_final
= true;
2326 event
->mode_set
= true;
2327 event
->mode
= cur
->key
.mode
;
2328 log_debug("MODE %#o %s:%u",
2330 rules_str(rules
, rule
->rule
.filename_off
),
2331 rule
->rule
.filename_line
);
2333 case TK_A_SECLABEL
: {
2334 const char *name
, *label
;
2336 name
= rules_str(rules
, cur
->key
.attr_off
);
2337 label
= rules_str(rules
, cur
->key
.value_off
);
2338 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2339 udev_list_cleanup(&event
->seclabel_list
);
2340 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2341 log_debug("SECLABEL{%s}='%s' %s:%u",
2343 rules_str(rules
, rule
->rule
.filename_off
),
2344 rule
->rule
.filename_line
);
2348 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2349 char *value
= rules_str(rules
, cur
->key
.value_off
);
2350 char value_new
[UTIL_NAME_SIZE
];
2351 const char *value_old
= NULL
;
2352 struct udev_list_entry
*entry
;
2354 if (value
[0] == '\0') {
2355 if (cur
->key
.op
== OP_ADD
)
2357 udev_device_add_property(event
->dev
, name
, NULL
);
2361 if (cur
->key
.op
== OP_ADD
)
2362 value_old
= udev_device_get_property_value(event
->dev
, name
);
2364 char temp
[UTIL_NAME_SIZE
];
2366 /* append value separated by space */
2367 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2368 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2370 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2372 entry
= udev_device_add_property(event
->dev
, name
, value_new
);
2373 /* store in db, skip private keys */
2375 udev_list_entry_set_num(entry
, true);
2379 char tag
[UTIL_PATH_SIZE
];
2382 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2383 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2384 udev_device_cleanup_tags_list(event
->dev
);
2385 for (p
= tag
; *p
!= '\0'; p
++) {
2386 if ((*p
>= 'a' && *p
<= 'z') ||
2387 (*p
>= 'A' && *p
<= 'Z') ||
2388 (*p
>= '0' && *p
<= '9') ||
2389 *p
== '-' || *p
== '_')
2391 log_error("ignoring invalid tag name '%s'", tag
);
2394 if (cur
->key
.op
== OP_REMOVE
)
2395 udev_device_remove_tag(event
->dev
, tag
);
2397 udev_device_add_tag(event
->dev
, tag
);
2401 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2403 char name_str
[UTIL_PATH_SIZE
];
2406 if (event
->name_final
)
2408 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2409 event
->name_final
= true;
2410 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2411 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2412 count
= util_replace_chars(name_str
, "/");
2414 log_debug("%i character(s) replaced", count
);
2416 if (major(udev_device_get_devnum(event
->dev
)) &&
2417 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2418 log_error("NAME=\"%s\" ignored, kernel device nodes "
2419 "can not be renamed; please fix it in %s:%u\n", name
,
2420 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2424 event
->name
= strdup(name_str
);
2425 log_debug("NAME '%s' %s:%u",
2427 rules_str(rules
, rule
->rule
.filename_off
),
2428 rule
->rule
.filename_line
);
2431 case TK_A_DEVLINK
: {
2432 char temp
[UTIL_PATH_SIZE
];
2433 char filename
[UTIL_PATH_SIZE
];
2437 if (event
->devlink_final
)
2439 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2441 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2442 event
->devlink_final
= true;
2443 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2444 udev_device_cleanup_devlinks_list(event
->dev
);
2446 /* allow multiple symlinks separated by spaces */
2447 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2448 if (esc
== ESCAPE_UNSET
)
2449 count
= util_replace_chars(temp
, "/ ");
2450 else if (esc
== ESCAPE_REPLACE
)
2451 count
= util_replace_chars(temp
, "/");
2453 log_debug("%i character(s) replaced" , count
);
2455 while (isspace(pos
[0]))
2457 next
= strchr(pos
, ' ');
2458 while (next
!= NULL
) {
2460 log_debug("LINK '%s' %s:%u", pos
,
2461 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2462 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2463 udev_device_add_devlink(event
->dev
, filename
);
2464 while (isspace(next
[1]))
2467 next
= strchr(pos
, ' ');
2469 if (pos
[0] != '\0') {
2470 log_debug("LINK '%s' %s:%u", pos
,
2471 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2472 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2473 udev_device_add_devlink(event
->dev
, filename
);
2478 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2479 char attr
[UTIL_PATH_SIZE
];
2480 char value
[UTIL_NAME_SIZE
];
2483 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2484 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2485 attr_subst_subdir(attr
, sizeof(attr
));
2487 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2488 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2489 rules_str(rules
, rule
->rule
.filename_off
),
2490 rule
->rule
.filename_line
);
2491 f
= fopen(attr
, "we");
2493 if (fprintf(f
, "%s", value
) <= 0)
2494 log_error("error writing ATTR{%s}: %m", attr
);
2497 log_error("error opening ATTR{%s} for writing: %m", attr
);
2501 case TK_A_RUN_BUILTIN
:
2502 case TK_A_RUN_PROGRAM
: {
2503 struct udev_list_entry
*entry
;
2505 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2506 udev_list_cleanup(&event
->run_list
);
2507 log_debug("RUN '%s' %s:%u",
2508 rules_str(rules
, cur
->key
.value_off
),
2509 rules_str(rules
, rule
->rule
.filename_off
),
2510 rule
->rule
.filename_line
);
2511 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2512 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2516 if (cur
->key
.rule_goto
== 0)
2518 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2523 case TK_M_PARENTS_MIN
:
2524 case TK_M_PARENTS_MAX
:
2527 log_error("wrong type %u", cur
->type
);
2534 /* fast-forward to next rule */
2535 cur
= rule
+ rule
->rule
.token_count
;
2539 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2545 _cleanup_strv_free_
char **tags
= NULL
;
2548 _cleanup_free_
char *path
= NULL
;
2551 if (rules
->tokens
== NULL
)
2554 cur
= &rules
->tokens
[0];
2557 switch (cur
->type
) {
2562 /* skip rules without a static_node tag */
2563 if (!rule
->rule
.has_static_node
)
2579 mode
= cur
->key
.mode
;
2582 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2587 case TK_A_STATIC_NODE
: {
2588 char device_node
[UTIL_PATH_SIZE
];
2589 char tags_dir
[UTIL_PATH_SIZE
];
2590 char tag_symlink
[UTIL_PATH_SIZE
];
2593 /* we assure, that the permissions tokens are sorted before the static token */
2595 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2598 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2599 if (stat(device_node
, &stats
) != 0)
2601 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2604 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2606 STRV_FOREACH(t
, tags
) {
2607 _cleanup_free_
char *unescaped_filename
= NULL
;
2609 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2610 r
= mkdir_p(tags_dir
, 0755);
2612 log_error("failed to create %s: %s", tags_dir
, strerror(-r
));
2616 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2618 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2619 r
= symlink(device_node
, tag_symlink
);
2620 if (r
< 0 && errno
!= EEXIST
) {
2621 log_error("failed to create symlink %s -> %s: %m", tag_symlink
, device_node
);
2628 /* don't touch the permissions if only the tags were set */
2629 if (mode
== 0 && uid
== 0 && gid
== 0)
2638 if (mode
!= (stats
.st_mode
& 01777)) {
2639 r
= chmod(device_node
, mode
);
2641 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2644 log_debug("chmod '%s' %#o", device_node
, mode
);
2647 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2648 r
= chown(device_node
, uid
, gid
);
2650 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2653 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2656 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2666 /* fast-forward to next rule */
2667 cur
= rule
+ rule
->rule
.token_count
;
2674 fchmod(fileno(f
), 0644);
2675 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2677 unlink("/run/udev/static_node-tags");