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"
35 #include "path-util.h"
37 #include "string-util.h"
39 #include "sysctl-util.h"
41 #include "user-util.h"
44 #define PREALLOC_TOKEN 2048
47 unsigned int name_off
;
54 static const char* const rules_dirs
[] = {
57 UDEVLIBEXECDIR
"/rules.d",
66 /* every key in the rules file becomes a token */
68 unsigned int token_cur
;
69 unsigned int token_max
;
71 /* all key strings are copied and de-duplicated in a single continuous string buffer */
72 struct strbuf
*strbuf
;
74 /* during rule parsing, uid/gid lookup results are cached */
76 unsigned int uids_cur
;
77 unsigned int uids_max
;
79 unsigned int gids_cur
;
80 unsigned int gids_max
;
83 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
84 return rules
->strbuf
->buf
+ off
;
87 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
88 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
91 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
105 enum string_glob_type
{
107 GL_PLAIN
, /* no special chars */
108 GL_GLOB
, /* shell globs ?,*,[] */
109 GL_SPLIT
, /* multi-value A|B */
110 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
111 GL_SOMETHING
, /* commonly used "?*" */
114 enum string_subst_type
{
121 /* tokens of a rule are sorted/handled in this order */
126 TK_M_ACTION
, /* val */
127 TK_M_DEVPATH
, /* val */
128 TK_M_KERNEL
, /* val */
129 TK_M_DEVLINK
, /* val */
131 TK_M_ENV
, /* val, attr */
133 TK_M_SUBSYSTEM
, /* val */
134 TK_M_DRIVER
, /* val */
135 TK_M_WAITFOR
, /* val */
136 TK_M_ATTR
, /* val, attr */
137 TK_M_SYSCTL
, /* val, attr */
140 TK_M_KERNELS
, /* val */
141 TK_M_SUBSYSTEMS
, /* val */
142 TK_M_DRIVERS
, /* val */
143 TK_M_ATTRS
, /* val, attr */
147 TK_M_TEST
, /* val, mode_t */
148 TK_M_PROGRAM
, /* val */
149 TK_M_IMPORT_FILE
, /* val */
150 TK_M_IMPORT_PROG
, /* val */
151 TK_M_IMPORT_BUILTIN
, /* val */
152 TK_M_IMPORT_DB
, /* val */
153 TK_M_IMPORT_CMDLINE
, /* val */
154 TK_M_IMPORT_PARENT
, /* val */
155 TK_M_RESULT
, /* val */
158 TK_A_STRING_ESCAPE_NONE
,
159 TK_A_STRING_ESCAPE_REPLACE
,
161 TK_A_INOTIFY_WATCH
, /* int */
162 TK_A_DEVLINK_PRIO
, /* int */
163 TK_A_OWNER
, /* val */
164 TK_A_GROUP
, /* val */
166 TK_A_OWNER_ID
, /* uid_t */
167 TK_A_GROUP_ID
, /* gid_t */
168 TK_A_MODE_ID
, /* mode_t */
170 TK_A_STATIC_NODE
, /* val */
171 TK_A_SECLABEL
, /* val, attr */
172 TK_A_ENV
, /* val, attr */
174 TK_A_DEVLINK
, /* val */
175 TK_A_ATTR
, /* val, attr */
176 TK_A_SYSCTL
, /* val, attr */
177 TK_A_RUN_BUILTIN
, /* val, bool */
178 TK_A_RUN_PROGRAM
, /* val, bool */
179 TK_A_GOTO
, /* size_t */
184 /* we try to pack stuff in a way that we take only 12 bytes per token */
187 unsigned char type
; /* same in rule and key */
189 enum token_type type
:8;
191 bool has_static_node
:1;
192 unsigned int unused
:6;
193 unsigned short token_count
;
194 unsigned int label_off
;
195 unsigned short filename_off
;
196 unsigned short filename_line
;
199 enum token_type type
:8;
200 enum operation_type op
:8;
201 enum string_glob_type glob
:8;
202 enum string_subst_type subst
:4;
203 enum string_subst_type attrsubst
:4;
204 unsigned int value_off
;
206 unsigned int attr_off
;
207 unsigned int rule_goto
;
213 enum udev_builtin_cmd builtin_cmd
;
221 struct udev_rules
*rules
;
223 struct token token
[MAX_TK
];
224 unsigned int token_cur
;
228 static const char *operation_str(enum operation_type type
) {
229 static const char *operation_strs
[] = {
230 [OP_UNSET
] = "UNSET",
231 [OP_MATCH
] = "match",
232 [OP_NOMATCH
] = "nomatch",
233 [OP_MATCH_MAX
] = "MATCH_MAX",
236 [OP_REMOVE
] = "remove",
237 [OP_ASSIGN
] = "assign",
238 [OP_ASSIGN_FINAL
] = "assign-final",
241 return operation_strs
[type
];
244 static const char *string_glob_str(enum string_glob_type type
) {
245 static const char *string_glob_strs
[] = {
246 [GL_UNSET
] = "UNSET",
247 [GL_PLAIN
] = "plain",
249 [GL_SPLIT
] = "split",
250 [GL_SPLIT_GLOB
] = "split-glob",
251 [GL_SOMETHING
] = "split-glob",
254 return string_glob_strs
[type
];
257 static const char *token_str(enum token_type type
) {
258 static const char *token_strs
[] = {
259 [TK_UNSET
] = "UNSET",
262 [TK_M_ACTION
] = "M ACTION",
263 [TK_M_DEVPATH
] = "M DEVPATH",
264 [TK_M_KERNEL
] = "M KERNEL",
265 [TK_M_DEVLINK
] = "M DEVLINK",
266 [TK_M_NAME
] = "M NAME",
267 [TK_M_ENV
] = "M ENV",
268 [TK_M_TAG
] = "M TAG",
269 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
270 [TK_M_DRIVER
] = "M DRIVER",
271 [TK_M_WAITFOR
] = "M WAITFOR",
272 [TK_M_ATTR
] = "M ATTR",
273 [TK_M_SYSCTL
] = "M SYSCTL",
275 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
276 [TK_M_KERNELS
] = "M KERNELS",
277 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
278 [TK_M_DRIVERS
] = "M DRIVERS",
279 [TK_M_ATTRS
] = "M ATTRS",
280 [TK_M_TAGS
] = "M TAGS",
281 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
283 [TK_M_TEST
] = "M TEST",
284 [TK_M_PROGRAM
] = "M PROGRAM",
285 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
286 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
287 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
288 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
289 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
290 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
291 [TK_M_RESULT
] = "M RESULT",
292 [TK_M_MAX
] = "M MAX",
294 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
295 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
296 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
297 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
298 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
299 [TK_A_OWNER
] = "A OWNER",
300 [TK_A_GROUP
] = "A GROUP",
301 [TK_A_MODE
] = "A MODE",
302 [TK_A_OWNER_ID
] = "A OWNER_ID",
303 [TK_A_GROUP_ID
] = "A GROUP_ID",
304 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
305 [TK_A_SECLABEL
] = "A SECLABEL",
306 [TK_A_MODE_ID
] = "A MODE_ID",
307 [TK_A_ENV
] = "A ENV",
308 [TK_A_TAG
] = "A ENV",
309 [TK_A_NAME
] = "A NAME",
310 [TK_A_DEVLINK
] = "A DEVLINK",
311 [TK_A_ATTR
] = "A ATTR",
312 [TK_A_SYSCTL
] = "A SYSCTL",
313 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
314 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
315 [TK_A_GOTO
] = "A GOTO",
320 return token_strs
[type
];
323 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
324 enum token_type type
= token
->type
;
325 enum operation_type op
= token
->key
.op
;
326 enum string_glob_type glob
= token
->key
.glob
;
327 const char *value
= str(rules
, token
->key
.value_off
);
328 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
333 const char *tks_ptr
= (char *)rules
->tokens
;
334 const char *tk_ptr
= (char *)token
;
335 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
337 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
338 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
339 idx
, token
->rule
.token_count
,
340 &rules
->buf
[token
->rule
.label_off
]);
352 case TK_M_SUBSYSTEMS
:
356 case TK_M_IMPORT_FILE
:
357 case TK_M_IMPORT_PROG
:
359 case TK_M_IMPORT_CMDLINE
:
360 case TK_M_IMPORT_PARENT
:
367 case TK_A_RUN_BUILTIN
:
368 case TK_A_RUN_PROGRAM
:
369 log_debug("%s %s '%s'(%s)",
370 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
372 case TK_M_IMPORT_BUILTIN
:
373 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
382 log_debug("%s %s '%s' '%s'(%s)",
383 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
387 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
389 case TK_A_STRING_ESCAPE_NONE
:
390 case TK_A_STRING_ESCAPE_REPLACE
:
391 case TK_A_DB_PERSIST
:
392 log_debug("%s", token_str(type
));
395 log_debug("%s %s '%s'(%s) %#o",
396 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
398 case TK_A_INOTIFY_WATCH
:
399 log_debug("%s %u", token_str(type
), token
->key
.watch
);
401 case TK_A_DEVLINK_PRIO
:
402 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
405 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
408 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
411 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
413 case TK_A_STATIC_NODE
:
414 log_debug("%s '%s'", token_str(type
), value
);
417 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
420 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
423 log_debug("* %s", token_str(type
));
425 case TK_M_PARENTS_MIN
:
426 case TK_M_PARENTS_MAX
:
429 log_debug("unknown type %u", type
);
434 static void dump_rules(struct udev_rules
*rules
) {
437 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
439 rules
->token_cur
* sizeof(struct token
),
442 for (i
= 0; i
< rules
->token_cur
; i
++)
443 dump_token(rules
, &rules
->tokens
[i
]);
446 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
447 static inline void dump_rules(struct udev_rules
*rules
) {}
450 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
451 /* grow buffer if needed */
452 if (rules
->token_cur
+1 >= rules
->token_max
) {
453 struct token
*tokens
;
456 /* double the buffer size */
457 add
= rules
->token_max
;
461 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
464 rules
->tokens
= tokens
;
465 rules
->token_max
+= add
;
467 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
472 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
478 /* lookup, if we know it already */
479 for (i
= 0; i
< rules
->uids_cur
; i
++) {
480 off
= rules
->uids
[i
].name_off
;
481 if (streq(rules_str(rules
, off
), owner
)) {
482 uid
= rules
->uids
[i
].uid
;
486 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
488 if (r
== -ENOENT
|| r
== -ESRCH
)
489 log_error("specified user '%s' unknown", owner
);
491 log_error_errno(r
, "error resolving user '%s': %m", owner
);
494 /* grow buffer if needed */
495 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
496 struct uid_gid
*uids
;
499 /* double the buffer size */
500 add
= rules
->uids_max
;
504 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
508 rules
->uids_max
+= add
;
510 rules
->uids
[rules
->uids_cur
].uid
= uid
;
511 off
= rules_add_string(rules
, owner
);
514 rules
->uids
[rules
->uids_cur
].name_off
= off
;
519 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
525 /* lookup, if we know it already */
526 for (i
= 0; i
< rules
->gids_cur
; i
++) {
527 off
= rules
->gids
[i
].name_off
;
528 if (streq(rules_str(rules
, off
), group
)) {
529 gid
= rules
->gids
[i
].gid
;
533 r
= get_group_creds(&group
, &gid
);
535 if (r
== -ENOENT
|| r
== -ESRCH
)
536 log_error("specified group '%s' unknown", group
);
538 log_error_errno(r
, "error resolving group '%s': %m", group
);
541 /* grow buffer if needed */
542 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
543 struct uid_gid
*gids
;
546 /* double the buffer size */
547 add
= rules
->gids_max
;
551 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
555 rules
->gids_max
+= add
;
557 rules
->gids
[rules
->gids_cur
].gid
= gid
;
558 off
= rules_add_string(rules
, group
);
561 rules
->gids
[rules
->gids_cur
].name_off
= off
;
566 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
573 while (isspace(key
[0]))
576 /* comment or empty line */
577 if (key
[0] == '#' || key
[0] == '\0')
580 /* split key/value */
581 val
= strchr(key
, '=');
588 while (isspace(val
[0]))
595 while (isspace(key
[len
-1]))
599 /* terminate value */
603 while (isspace(val
[len
-1]))
611 if (val
[0] == '"' || val
[0] == '\'') {
612 if (val
[len
-1] != val
[0]) {
613 log_debug("inconsistent quoting: '%s', skip", line
);
620 udev_device_add_property(dev
, key
, val
);
625 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
627 char line
[UTIL_LINE_SIZE
];
629 f
= fopen(filename
, "re");
632 while (fgets(line
, sizeof(line
), f
) != NULL
)
633 import_property_from_string(dev
, line
);
638 static int import_program_into_properties(struct udev_event
*event
,
640 usec_t timeout_warn_usec
,
641 const char *program
) {
642 char result
[UTIL_LINE_SIZE
];
646 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
651 while (line
!= NULL
) {
654 pos
= strchr(line
, '\n');
659 import_property_from_string(event
->dev
, line
);
665 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
666 struct udev_device
*dev_parent
;
667 struct udev_list_entry
*list_entry
;
672 dev_parent
= udev_device_get_parent(dev
);
673 if (dev_parent
== NULL
)
676 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
677 const char *key
= udev_list_entry_get_name(list_entry
);
678 const char *val
= udev_list_entry_get_value(list_entry
);
680 if (fnmatch(filter
, key
, 0) == 0)
681 udev_device_add_property(dev
, key
, val
);
686 static int attr_subst_subdir(char *attr
, size_t len
) {
689 if (strstr(attr
, "/*/")) {
691 char dirname
[UTIL_PATH_SIZE
];
695 strscpy(dirname
, sizeof(dirname
), attr
);
696 pos
= strstr(dirname
, "/*/");
701 dir
= opendir(dirname
);
705 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
708 if (dent
->d_name
[0] == '.')
710 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
711 if (stat(attr
, &stats
) == 0) {
723 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
728 if (linepos
== NULL
|| linepos
[0] == '\0')
731 /* skip whitespace */
732 while (isspace(linepos
[0]) || linepos
[0] == ',')
736 if (linepos
[0] == '\0')
742 if (linepos
[0] == '\0')
744 if (isspace(linepos
[0]))
746 if (linepos
[0] == '=')
748 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
749 if (linepos
[1] == '=')
753 /* remember end of key */
756 /* skip whitespace after key */
757 while (isspace(linepos
[0]))
759 if (linepos
[0] == '\0')
762 /* get operation type */
763 if (linepos
[0] == '=' && linepos
[1] == '=') {
766 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
769 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
772 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
775 } else if (linepos
[0] == '=') {
778 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
779 *op
= OP_ASSIGN_FINAL
;
787 /* skip whitespace after operator */
788 while (isspace(linepos
[0]))
790 if (linepos
[0] == '\0')
794 if (linepos
[0] == '"')
801 temp
= strchr(linepos
, '"');
807 /* move line to next key */
812 /* extract possible KEY{attr} */
813 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
817 attr
= strchr(str
, '{');
820 pos
= strchr(attr
, '}');
822 log_error("missing closing brace for format");
831 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
832 enum operation_type op
,
833 const char *value
, const void *data
) {
834 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
835 const char *attr
= NULL
;
837 memzero(token
, sizeof(struct token
));
849 case TK_M_SUBSYSTEMS
:
853 case TK_M_IMPORT_FILE
:
854 case TK_M_IMPORT_PROG
:
856 case TK_M_IMPORT_CMDLINE
:
857 case TK_M_IMPORT_PARENT
:
867 case TK_A_STATIC_NODE
:
868 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
870 case TK_M_IMPORT_BUILTIN
:
871 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
872 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
883 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
884 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
887 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
889 token
->key
.mode
= *(mode_t
*)data
;
891 case TK_A_STRING_ESCAPE_NONE
:
892 case TK_A_STRING_ESCAPE_REPLACE
:
893 case TK_A_DB_PERSIST
:
895 case TK_A_RUN_BUILTIN
:
896 case TK_A_RUN_PROGRAM
:
897 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
898 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
900 case TK_A_INOTIFY_WATCH
:
901 case TK_A_DEVLINK_PRIO
:
902 token
->key
.devlink_prio
= *(int *)data
;
905 token
->key
.uid
= *(uid_t
*)data
;
908 token
->key
.gid
= *(gid_t
*)data
;
911 token
->key
.mode
= *(mode_t
*)data
;
914 case TK_M_PARENTS_MIN
:
915 case TK_M_PARENTS_MAX
:
919 log_error("wrong type %u", type
);
923 if (value
!= NULL
&& type
< TK_M_MAX
) {
924 /* check if we need to split or call fnmatch() while matching rules */
925 enum string_glob_type glob
;
929 has_split
= (strchr(value
, '|') != NULL
);
930 has_glob
= string_is_glob(value
);
931 if (has_split
&& has_glob
) {
932 glob
= GL_SPLIT_GLOB
;
933 } else if (has_split
) {
935 } else if (has_glob
) {
936 if (streq(value
, "?*"))
943 token
->key
.glob
= glob
;
946 if (value
!= NULL
&& type
> TK_M_MAX
) {
947 /* check if assigned value has substitution chars */
949 token
->key
.subst
= SB_SUBSYS
;
950 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
951 token
->key
.subst
= SB_FORMAT
;
953 token
->key
.subst
= SB_NONE
;
957 /* check if property/attribute name has substitution chars */
959 token
->key
.attrsubst
= SB_SUBSYS
;
960 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
961 token
->key
.attrsubst
= SB_FORMAT
;
963 token
->key
.attrsubst
= SB_NONE
;
966 token
->key
.type
= type
;
968 rule_tmp
->token_cur
++;
969 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
970 log_error("temporary rule array too small");
976 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
978 unsigned int start
= 0;
979 unsigned int end
= rule_tmp
->token_cur
;
981 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
982 enum token_type next_val
= TK_UNSET
;
983 unsigned int next_idx
= 0;
986 /* find smallest value */
987 for (j
= start
; j
< end
; j
++) {
988 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
990 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
991 next_val
= rule_tmp
->token
[j
].type
;
996 /* add token and mark done */
997 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
999 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1002 if (next_idx
== start
)
1004 if (next_idx
+1 == end
)
1010 static int add_rule(struct udev_rules
*rules
, char *line
,
1011 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1014 struct rule_tmp rule_tmp
= {
1016 .rule
.type
= TK_RULE
,
1019 /* the offset in the rule is limited to unsigned short */
1020 if (filename_off
< USHRT_MAX
)
1021 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1022 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1028 enum operation_type op
;
1030 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1031 /* Avoid erroring on trailing whitespace. This is probably rare
1032 * so save the work for the error case instead of always trying
1033 * to strip the trailing whitespace with strstrip(). */
1034 while (isblank(*linepos
))
1037 /* If we aren't at the end of the line, this is a parsing error.
1038 * Make a best effort to describe where the problem is. */
1039 if (!strchr(NEWLINE
, *linepos
)) {
1040 char buf
[2] = {*linepos
};
1041 _cleanup_free_
char *tmp
;
1044 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1045 filename
, lineno
, linepos
- line
+ 1, tmp
);
1046 if (*linepos
== '#')
1047 log_error("hint: comments can only start at beginning of line");
1052 if (streq(key
, "ACTION")) {
1053 if (op
> OP_MATCH_MAX
) {
1054 log_error("invalid ACTION operation");
1057 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1061 if (streq(key
, "DEVPATH")) {
1062 if (op
> OP_MATCH_MAX
) {
1063 log_error("invalid DEVPATH operation");
1066 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1070 if (streq(key
, "KERNEL")) {
1071 if (op
> OP_MATCH_MAX
) {
1072 log_error("invalid KERNEL operation");
1075 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1079 if (streq(key
, "SUBSYSTEM")) {
1080 if (op
> OP_MATCH_MAX
) {
1081 log_error("invalid SUBSYSTEM operation");
1084 /* bus, class, subsystem events should all be the same */
1085 if (streq(value
, "subsystem") ||
1086 streq(value
, "bus") ||
1087 streq(value
, "class")) {
1088 if (streq(value
, "bus") || streq(value
, "class"))
1089 log_error("'%s' must be specified as 'subsystem' "
1090 "please fix it in %s:%u", value
, filename
, lineno
);
1091 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1093 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1097 if (streq(key
, "DRIVER")) {
1098 if (op
> OP_MATCH_MAX
) {
1099 log_error("invalid DRIVER operation");
1102 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1106 if (startswith(key
, "ATTR{")) {
1107 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1109 log_error("error parsing ATTR attribute");
1112 if (op
== OP_REMOVE
) {
1113 log_error("invalid ATTR operation");
1116 if (op
< OP_MATCH_MAX
)
1117 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1119 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1123 if (startswith(key
, "SYSCTL{")) {
1124 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SYSCTL"));
1126 log_error("error parsing SYSCTL attribute");
1129 if (op
== OP_REMOVE
) {
1130 log_error("invalid SYSCTL operation");
1133 if (op
< OP_MATCH_MAX
)
1134 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1136 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1140 if (startswith(key
, "SECLABEL{")) {
1141 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1143 log_error("error parsing SECLABEL attribute");
1146 if (op
== OP_REMOVE
) {
1147 log_error("invalid SECLABEL operation");
1151 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1155 if (streq(key
, "KERNELS")) {
1156 if (op
> OP_MATCH_MAX
) {
1157 log_error("invalid KERNELS operation");
1160 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1164 if (streq(key
, "SUBSYSTEMS")) {
1165 if (op
> OP_MATCH_MAX
) {
1166 log_error("invalid SUBSYSTEMS operation");
1169 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1173 if (streq(key
, "DRIVERS")) {
1174 if (op
> OP_MATCH_MAX
) {
1175 log_error("invalid DRIVERS operation");
1178 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1182 if (startswith(key
, "ATTRS{")) {
1183 if (op
> OP_MATCH_MAX
) {
1184 log_error("invalid ATTRS operation");
1187 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1189 log_error("error parsing ATTRS attribute");
1192 if (startswith(attr
, "device/"))
1193 log_error("the 'device' link may not be available in a future kernel, "
1194 "please fix it in %s:%u", filename
, lineno
);
1195 else if (strstr(attr
, "../") != NULL
)
1196 log_error("do not reference parent sysfs directories directly, "
1197 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1198 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1202 if (streq(key
, "TAGS")) {
1203 if (op
> OP_MATCH_MAX
) {
1204 log_error("invalid TAGS operation");
1207 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1211 if (startswith(key
, "ENV{")) {
1212 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1214 log_error("error parsing ENV attribute");
1217 if (op
== OP_REMOVE
) {
1218 log_error("invalid ENV operation");
1221 if (op
< OP_MATCH_MAX
) {
1222 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1225 static const char *blacklist
[] = {
1240 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1241 if (!streq(attr
, blacklist
[i
]))
1243 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1246 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1252 if (streq(key
, "TAG")) {
1253 if (op
< OP_MATCH_MAX
)
1254 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1256 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1260 if (streq(key
, "PROGRAM")) {
1261 if (op
== OP_REMOVE
) {
1262 log_error("invalid PROGRAM operation");
1265 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1269 if (streq(key
, "RESULT")) {
1270 if (op
> OP_MATCH_MAX
) {
1271 log_error("invalid RESULT operation");
1274 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1278 if (startswith(key
, "IMPORT")) {
1279 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1281 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1284 if (op
== OP_REMOVE
) {
1285 log_error("invalid IMPORT operation");
1288 if (streq(attr
, "program")) {
1289 /* find known built-in command */
1290 if (value
[0] != '/') {
1291 enum udev_builtin_cmd cmd
;
1293 cmd
= udev_builtin_lookup(value
);
1294 if (cmd
< UDEV_BUILTIN_MAX
) {
1295 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1296 value
, filename
, lineno
);
1297 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1301 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1302 } else if (streq(attr
, "builtin")) {
1303 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1305 if (cmd
< UDEV_BUILTIN_MAX
)
1306 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1308 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1309 } else if (streq(attr
, "file")) {
1310 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1311 } else if (streq(attr
, "db")) {
1312 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1313 } else if (streq(attr
, "cmdline")) {
1314 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1315 } else if (streq(attr
, "parent")) {
1316 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1318 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1322 if (startswith(key
, "TEST")) {
1325 if (op
> OP_MATCH_MAX
) {
1326 log_error("invalid TEST operation");
1329 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1331 mode
= strtol(attr
, NULL
, 8);
1332 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1334 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1339 if (startswith(key
, "RUN")) {
1340 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1343 if (op
== OP_REMOVE
) {
1344 log_error("invalid RUN operation");
1348 if (streq(attr
, "builtin")) {
1349 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1351 if (cmd
< UDEV_BUILTIN_MAX
)
1352 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1354 log_error("RUN{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1355 } else if (streq(attr
, "program")) {
1356 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1358 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1360 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1366 if (streq(key
, "LABEL")) {
1367 if (op
== OP_REMOVE
) {
1368 log_error("invalid LABEL operation");
1371 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1375 if (streq(key
, "GOTO")) {
1376 if (op
== OP_REMOVE
) {
1377 log_error("invalid GOTO operation");
1380 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1384 if (startswith(key
, "NAME")) {
1385 if (op
== OP_REMOVE
) {
1386 log_error("invalid NAME operation");
1389 if (op
< OP_MATCH_MAX
) {
1390 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1392 if (streq(value
, "%k")) {
1393 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1394 "please remove it from %s:%u\n", filename
, lineno
);
1397 if (value
[0] == '\0') {
1398 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1399 "please remove it from %s:%u\n", filename
, lineno
);
1402 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1404 rule_tmp
.rule
.rule
.can_set_name
= true;
1408 if (streq(key
, "SYMLINK")) {
1409 if (op
== OP_REMOVE
) {
1410 log_error("invalid SYMLINK operation");
1413 if (op
< OP_MATCH_MAX
)
1414 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1416 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1417 rule_tmp
.rule
.rule
.can_set_name
= true;
1421 if (streq(key
, "OWNER")) {
1425 if (op
== OP_REMOVE
) {
1426 log_error("invalid OWNER operation");
1430 uid
= strtoul(value
, &endptr
, 10);
1431 if (endptr
[0] == '\0') {
1432 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1433 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1434 uid
= add_uid(rules
, value
);
1435 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1436 } else if (rules
->resolve_names
>= 0)
1437 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1439 rule_tmp
.rule
.rule
.can_set_name
= true;
1443 if (streq(key
, "GROUP")) {
1447 if (op
== OP_REMOVE
) {
1448 log_error("invalid GROUP operation");
1452 gid
= strtoul(value
, &endptr
, 10);
1453 if (endptr
[0] == '\0') {
1454 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1455 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1456 gid
= add_gid(rules
, value
);
1457 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1458 } else if (rules
->resolve_names
>= 0)
1459 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1461 rule_tmp
.rule
.rule
.can_set_name
= true;
1465 if (streq(key
, "MODE")) {
1469 if (op
== OP_REMOVE
) {
1470 log_error("invalid MODE operation");
1474 mode
= strtol(value
, &endptr
, 8);
1475 if (endptr
[0] == '\0')
1476 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1478 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1479 rule_tmp
.rule
.rule
.can_set_name
= true;
1483 if (streq(key
, "OPTIONS")) {
1486 if (op
== OP_REMOVE
) {
1487 log_error("invalid OPTIONS operation");
1491 pos
= strstr(value
, "link_priority=");
1493 int prio
= atoi(&pos
[strlen("link_priority=")]);
1495 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1498 pos
= strstr(value
, "string_escape=");
1500 pos
= &pos
[strlen("string_escape=")];
1501 if (startswith(pos
, "none"))
1502 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1503 else if (startswith(pos
, "replace"))
1504 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1507 pos
= strstr(value
, "db_persist");
1509 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1511 pos
= strstr(value
, "nowatch");
1515 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1517 pos
= strstr(value
, "watch");
1521 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1525 pos
= strstr(value
, "static_node=");
1527 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1528 rule_tmp
.rule
.rule
.has_static_node
= true;
1534 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1538 /* add rule token */
1539 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1540 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1543 /* add tokens to list, sorted by type */
1544 if (sort_token(rules
, &rule_tmp
) != 0)
1549 log_error("invalid rule '%s:%u'", filename
, lineno
);
1553 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1554 _cleanup_fclose_
FILE *f
= NULL
;
1555 unsigned int first_token
;
1556 unsigned int filename_off
;
1557 char line
[UTIL_LINE_SIZE
];
1561 f
= fopen(filename
, "re");
1563 if (errno
== ENOENT
)
1569 if (null_or_empty_fd(fileno(f
))) {
1570 log_debug("Skipping empty file: %s", filename
);
1573 log_debug("Reading rules file: %s", filename
);
1575 first_token
= rules
->token_cur
;
1576 filename_off
= rules_add_string(rules
, filename
);
1578 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1582 /* skip whitespace */
1585 while (isspace(key
[0]))
1596 /* continue reading if backslash+newline is found */
1597 while (line
[len
-2] == '\\') {
1598 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1600 if (strlen(&line
[len
-2]) < 2)
1606 if (len
+1 >= sizeof(line
)) {
1607 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1610 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1613 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1614 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1615 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1616 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1619 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1620 if (rules
->tokens
[j
].type
!= TK_RULE
)
1622 if (rules
->tokens
[j
].rule
.label_off
== 0)
1624 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1626 rules
->tokens
[i
].key
.rule_goto
= j
;
1629 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1630 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1636 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1637 struct udev_rules
*rules
;
1638 struct udev_list file_list
;
1639 struct token end_token
;
1643 rules
= new0(struct udev_rules
, 1);
1647 rules
->resolve_names
= resolve_names
;
1648 udev_list_init(udev
, &file_list
, true);
1650 /* init token array and string buffer */
1651 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1652 if (rules
->tokens
== NULL
)
1653 return udev_rules_unref(rules
);
1654 rules
->token_max
= PREALLOC_TOKEN
;
1656 rules
->strbuf
= strbuf_new();
1658 return udev_rules_unref(rules
);
1660 udev_rules_check_timestamp(rules
);
1662 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1664 log_error_errno(r
, "failed to enumerate rules files: %m");
1665 return udev_rules_unref(rules
);
1669 * The offset value in the rules strct is limited; add all
1670 * rules file names to the beginning of the string buffer.
1672 STRV_FOREACH(f
, files
)
1673 rules_add_string(rules
, *f
);
1675 STRV_FOREACH(f
, files
)
1676 parse_file(rules
, *f
);
1680 memzero(&end_token
, sizeof(struct token
));
1681 end_token
.type
= TK_END
;
1682 add_token(rules
, &end_token
);
1683 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1684 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1686 /* cleanup temporary strbuf data */
1687 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1688 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1689 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1690 strbuf_complete(rules
->strbuf
);
1692 /* cleanup uid/gid cache */
1693 rules
->uids
= mfree(rules
->uids
);
1694 rules
->uids_cur
= 0;
1695 rules
->uids_max
= 0;
1696 rules
->gids
= mfree(rules
->gids
);
1697 rules
->gids_cur
= 0;
1698 rules
->gids_max
= 0;
1704 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1707 free(rules
->tokens
);
1708 strbuf_cleanup(rules
->strbuf
);
1715 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1719 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1722 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1723 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1730 switch (token
->key
.glob
) {
1732 match
= (streq(key_value
, val
));
1735 match
= (fnmatch(key_value
, val
, 0) == 0);
1742 s
= rules_str(rules
, token
->key
.value_off
);
1747 next
= strchr(s
, '|');
1749 size_t matchlen
= (size_t)(next
- s
);
1751 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1755 match
= (streq(s
, val
));
1764 char value
[UTIL_PATH_SIZE
];
1766 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1768 while (key_value
!= NULL
) {
1769 pos
= strchr(key_value
, '|');
1774 match
= (fnmatch(key_value
, val
, 0) == 0);
1782 match
= (val
[0] != '\0');
1788 if (match
&& (token
->key
.op
== OP_MATCH
))
1790 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1795 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1797 char nbuf
[UTIL_NAME_SIZE
];
1799 char vbuf
[UTIL_NAME_SIZE
];
1802 name
= rules_str(rules
, cur
->key
.attr_off
);
1803 switch (cur
->key
.attrsubst
) {
1805 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1809 value
= udev_device_get_sysattr_value(dev
, name
);
1814 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1822 /* remove trailing whitespace, if not asked to match for it */
1823 len
= strlen(value
);
1824 if (len
> 0 && isspace(value
[len
-1])) {
1825 const char *key_value
;
1828 key_value
= rules_str(rules
, cur
->key
.value_off
);
1829 klen
= strlen(key_value
);
1830 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1831 if (value
!= vbuf
) {
1832 strscpy(vbuf
, sizeof(vbuf
), value
);
1835 while (len
> 0 && isspace(vbuf
[--len
]))
1840 return match_key(rules
, cur
, value
);
1849 int udev_rules_apply_to_event(struct udev_rules
*rules
,
1850 struct udev_event
*event
,
1851 usec_t timeout_usec
,
1852 usec_t timeout_warn_usec
,
1853 struct udev_list
*properties_list
) {
1856 enum escape_type esc
= ESCAPE_UNSET
;
1859 if (rules
->tokens
== NULL
)
1862 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1863 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1864 udev_device_get_ifindex(event
->dev
) > 0));
1866 /* loop through token list, match, run actions or forward to next rule */
1867 cur
= &rules
->tokens
[0];
1870 dump_token(rules
, cur
);
1871 switch (cur
->type
) {
1875 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1876 if (!can_set_name
&& rule
->rule
.can_set_name
)
1881 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1885 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1889 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1892 case TK_M_DEVLINK
: {
1893 struct udev_list_entry
*list_entry
;
1896 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1897 const char *devlink
;
1899 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1900 if (match_key(rules
, cur
, devlink
) == 0) {
1910 if (match_key(rules
, cur
, event
->name
) != 0)
1914 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1917 value
= udev_device_get_property_value(event
->dev
, key_name
);
1919 /* check global properties */
1920 if (!value
&& properties_list
) {
1921 struct udev_list_entry
*list_entry
;
1923 list_entry
= udev_list_get_entry(properties_list
);
1924 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1925 if (list_entry
!= NULL
)
1926 value
= udev_list_entry_get_value(list_entry
);
1931 if (match_key(rules
, cur
, value
))
1936 struct udev_list_entry
*list_entry
;
1939 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1940 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1945 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1946 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1950 case TK_M_SUBSYSTEM
:
1951 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1955 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1959 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1963 char filename
[UTIL_PATH_SIZE
];
1964 _cleanup_free_
char *value
= NULL
;
1967 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
1968 sysctl_normalize(filename
);
1969 if (sysctl_read(filename
, &value
) < 0)
1972 len
= strlen(value
);
1973 while (len
> 0 && isspace(value
[--len
]))
1975 if (match_key(rules
, cur
, value
) != 0)
1980 case TK_M_SUBSYSTEMS
:
1986 /* get whole sequence of parent matches */
1988 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1991 /* loop over parents */
1992 event
->dev_parent
= event
->dev
;
1996 /* loop over sequence of parent match keys */
1997 for (key
= cur
; key
< next
; key
++ ) {
1998 dump_token(rules
, key
);
2001 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2004 case TK_M_SUBSYSTEMS
:
2005 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2009 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2013 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2017 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
2019 if (match
&& key
->key
.op
== OP_NOMATCH
)
2021 if (!match
&& key
->key
.op
== OP_MATCH
)
2032 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2033 if (event
->dev_parent
== NULL
)
2036 /* move behind our sequence of parent match keys */
2041 char filename
[UTIL_PATH_SIZE
];
2042 struct stat statbuf
;
2045 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2046 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2047 if (filename
[0] != '/') {
2048 char tmp
[UTIL_PATH_SIZE
];
2050 strscpy(tmp
, sizeof(tmp
), filename
);
2051 strscpyl(filename
, sizeof(filename
),
2052 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2055 attr_subst_subdir(filename
, sizeof(filename
));
2057 match
= (stat(filename
, &statbuf
) == 0);
2058 if (match
&& cur
->key
.mode
> 0)
2059 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2060 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2062 if (!match
&& cur
->key
.op
== OP_MATCH
)
2066 case TK_M_PROGRAM
: {
2067 char program
[UTIL_PATH_SIZE
];
2068 char result
[UTIL_LINE_SIZE
];
2070 event
->program_result
= mfree(event
->program_result
);
2071 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2072 log_debug("PROGRAM '%s' %s:%u",
2074 rules_str(rules
, rule
->rule
.filename_off
),
2075 rule
->rule
.filename_line
);
2077 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
2078 if (cur
->key
.op
!= OP_NOMATCH
)
2083 util_remove_trailing_chars(result
, '\n');
2084 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2085 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2087 log_debug("%i character(s) replaced" , count
);
2089 event
->program_result
= strdup(result
);
2090 if (cur
->key
.op
== OP_NOMATCH
)
2095 case TK_M_IMPORT_FILE
: {
2096 char import
[UTIL_PATH_SIZE
];
2098 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2099 if (import_file_into_properties(event
->dev
, import
) != 0)
2100 if (cur
->key
.op
!= OP_NOMATCH
)
2104 case TK_M_IMPORT_PROG
: {
2105 char import
[UTIL_PATH_SIZE
];
2107 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2108 log_debug("IMPORT '%s' %s:%u",
2110 rules_str(rules
, rule
->rule
.filename_off
),
2111 rule
->rule
.filename_line
);
2113 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
2114 if (cur
->key
.op
!= OP_NOMATCH
)
2118 case TK_M_IMPORT_BUILTIN
: {
2119 char command
[UTIL_PATH_SIZE
];
2121 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2122 /* check if we ran already */
2123 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2124 log_debug("IMPORT builtin skip '%s' %s:%u",
2125 udev_builtin_name(cur
->key
.builtin_cmd
),
2126 rules_str(rules
, rule
->rule
.filename_off
),
2127 rule
->rule
.filename_line
);
2128 /* return the result from earlier run */
2129 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2130 if (cur
->key
.op
!= OP_NOMATCH
)
2135 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2138 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2139 log_debug("IMPORT builtin '%s' %s:%u",
2140 udev_builtin_name(cur
->key
.builtin_cmd
),
2141 rules_str(rules
, rule
->rule
.filename_off
),
2142 rule
->rule
.filename_line
);
2144 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2145 /* remember failure */
2146 log_debug("IMPORT builtin '%s' returned non-zero",
2147 udev_builtin_name(cur
->key
.builtin_cmd
));
2148 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2149 if (cur
->key
.op
!= OP_NOMATCH
)
2154 case TK_M_IMPORT_DB
: {
2155 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2158 value
= udev_device_get_property_value(event
->dev_db
, key
);
2160 udev_device_add_property(event
->dev
, key
, value
);
2162 if (cur
->key
.op
!= OP_NOMATCH
)
2167 case TK_M_IMPORT_CMDLINE
: {
2169 bool imported
= false;
2171 f
= fopen("/proc/cmdline", "re");
2175 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2176 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2179 pos
= strstr(cmdline
, key
);
2182 if (pos
[0] == '\0' || isspace(pos
[0])) {
2183 /* we import simple flags as 'FLAG=1' */
2184 udev_device_add_property(event
->dev
, key
, "1");
2186 } else if (pos
[0] == '=') {
2191 while (pos
[0] != '\0' && !isspace(pos
[0]))
2194 udev_device_add_property(event
->dev
, key
, value
);
2201 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2205 case TK_M_IMPORT_PARENT
: {
2206 char import
[UTIL_PATH_SIZE
];
2208 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2209 if (import_parent_into_properties(event
->dev
, import
) != 0)
2210 if (cur
->key
.op
!= OP_NOMATCH
)
2215 if (match_key(rules
, cur
, event
->program_result
) != 0)
2218 case TK_A_STRING_ESCAPE_NONE
:
2221 case TK_A_STRING_ESCAPE_REPLACE
:
2222 esc
= ESCAPE_REPLACE
;
2224 case TK_A_DB_PERSIST
:
2225 udev_device_set_db_persist(event
->dev
);
2227 case TK_A_INOTIFY_WATCH
:
2228 if (event
->inotify_watch_final
)
2230 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2231 event
->inotify_watch_final
= true;
2232 event
->inotify_watch
= cur
->key
.watch
;
2234 case TK_A_DEVLINK_PRIO
:
2235 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2238 char owner
[UTIL_NAME_SIZE
];
2239 const char *ow
= owner
;
2242 if (event
->owner_final
)
2244 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2245 event
->owner_final
= true;
2246 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2247 event
->owner_set
= true;
2248 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2250 if (r
== -ENOENT
|| r
== -ESRCH
)
2251 log_error("specified user '%s' unknown", owner
);
2253 log_error_errno(r
, "error resolving user '%s': %m", owner
);
2257 log_debug("OWNER %u %s:%u",
2259 rules_str(rules
, rule
->rule
.filename_off
),
2260 rule
->rule
.filename_line
);
2264 char group
[UTIL_NAME_SIZE
];
2265 const char *gr
= group
;
2268 if (event
->group_final
)
2270 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2271 event
->group_final
= true;
2272 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2273 event
->group_set
= true;
2274 r
= get_group_creds(&gr
, &event
->gid
);
2276 if (r
== -ENOENT
|| r
== -ESRCH
)
2277 log_error("specified group '%s' unknown", group
);
2279 log_error_errno(r
, "error resolving group '%s': %m", group
);
2283 log_debug("GROUP %u %s:%u",
2285 rules_str(rules
, rule
->rule
.filename_off
),
2286 rule
->rule
.filename_line
);
2290 char mode_str
[UTIL_NAME_SIZE
];
2294 if (event
->mode_final
)
2296 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2297 mode
= strtol(mode_str
, &endptr
, 8);
2298 if (endptr
[0] != '\0') {
2299 log_error("ignoring invalid mode '%s'", mode_str
);
2302 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2303 event
->mode_final
= true;
2304 event
->mode_set
= true;
2306 log_debug("MODE %#o %s:%u",
2308 rules_str(rules
, rule
->rule
.filename_off
),
2309 rule
->rule
.filename_line
);
2313 if (event
->owner_final
)
2315 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2316 event
->owner_final
= true;
2317 event
->owner_set
= true;
2318 event
->uid
= cur
->key
.uid
;
2319 log_debug("OWNER %u %s:%u",
2321 rules_str(rules
, rule
->rule
.filename_off
),
2322 rule
->rule
.filename_line
);
2325 if (event
->group_final
)
2327 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2328 event
->group_final
= true;
2329 event
->group_set
= true;
2330 event
->gid
= cur
->key
.gid
;
2331 log_debug("GROUP %u %s:%u",
2333 rules_str(rules
, rule
->rule
.filename_off
),
2334 rule
->rule
.filename_line
);
2337 if (event
->mode_final
)
2339 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2340 event
->mode_final
= true;
2341 event
->mode_set
= true;
2342 event
->mode
= cur
->key
.mode
;
2343 log_debug("MODE %#o %s:%u",
2345 rules_str(rules
, rule
->rule
.filename_off
),
2346 rule
->rule
.filename_line
);
2348 case TK_A_SECLABEL
: {
2349 const char *name
, *label
;
2351 name
= rules_str(rules
, cur
->key
.attr_off
);
2352 label
= rules_str(rules
, cur
->key
.value_off
);
2353 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2354 udev_list_cleanup(&event
->seclabel_list
);
2355 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2356 log_debug("SECLABEL{%s}='%s' %s:%u",
2358 rules_str(rules
, rule
->rule
.filename_off
),
2359 rule
->rule
.filename_line
);
2363 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2364 char *value
= rules_str(rules
, cur
->key
.value_off
);
2365 char value_new
[UTIL_NAME_SIZE
];
2366 const char *value_old
= NULL
;
2368 if (value
[0] == '\0') {
2369 if (cur
->key
.op
== OP_ADD
)
2371 udev_device_add_property(event
->dev
, name
, NULL
);
2375 if (cur
->key
.op
== OP_ADD
)
2376 value_old
= udev_device_get_property_value(event
->dev
, name
);
2378 char temp
[UTIL_NAME_SIZE
];
2380 /* append value separated by space */
2381 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2382 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2384 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2386 udev_device_add_property(event
->dev
, name
, value_new
);
2390 char tag
[UTIL_PATH_SIZE
];
2393 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2394 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2395 udev_device_cleanup_tags_list(event
->dev
);
2396 for (p
= tag
; *p
!= '\0'; p
++) {
2397 if ((*p
>= 'a' && *p
<= 'z') ||
2398 (*p
>= 'A' && *p
<= 'Z') ||
2399 (*p
>= '0' && *p
<= '9') ||
2400 *p
== '-' || *p
== '_')
2402 log_error("ignoring invalid tag name '%s'", tag
);
2405 if (cur
->key
.op
== OP_REMOVE
)
2406 udev_device_remove_tag(event
->dev
, tag
);
2408 udev_device_add_tag(event
->dev
, tag
);
2412 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2414 char name_str
[UTIL_PATH_SIZE
];
2417 if (event
->name_final
)
2419 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2420 event
->name_final
= true;
2421 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2422 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2423 count
= util_replace_chars(name_str
, "/");
2425 log_debug("%i character(s) replaced", count
);
2427 if (major(udev_device_get_devnum(event
->dev
)) &&
2428 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2429 log_error("NAME=\"%s\" ignored, kernel device nodes "
2430 "can not be renamed; please fix it in %s:%u\n", name
,
2431 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2434 free_and_strdup(&event
->name
, name_str
);
2435 log_debug("NAME '%s' %s:%u",
2437 rules_str(rules
, rule
->rule
.filename_off
),
2438 rule
->rule
.filename_line
);
2441 case TK_A_DEVLINK
: {
2442 char temp
[UTIL_PATH_SIZE
];
2443 char filename
[UTIL_PATH_SIZE
];
2447 if (event
->devlink_final
)
2449 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2451 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2452 event
->devlink_final
= true;
2453 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2454 udev_device_cleanup_devlinks_list(event
->dev
);
2456 /* allow multiple symlinks separated by spaces */
2457 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2458 if (esc
== ESCAPE_UNSET
)
2459 count
= util_replace_chars(temp
, "/ ");
2460 else if (esc
== ESCAPE_REPLACE
)
2461 count
= util_replace_chars(temp
, "/");
2463 log_debug("%i character(s) replaced" , count
);
2465 while (isspace(pos
[0]))
2467 next
= strchr(pos
, ' ');
2468 while (next
!= NULL
) {
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
);
2474 while (isspace(next
[1]))
2477 next
= strchr(pos
, ' ');
2479 if (pos
[0] != '\0') {
2480 log_debug("LINK '%s' %s:%u", pos
,
2481 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2482 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2483 udev_device_add_devlink(event
->dev
, filename
);
2488 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2489 char attr
[UTIL_PATH_SIZE
];
2490 char value
[UTIL_NAME_SIZE
];
2493 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2494 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2495 attr_subst_subdir(attr
, sizeof(attr
));
2497 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2498 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2499 rules_str(rules
, rule
->rule
.filename_off
),
2500 rule
->rule
.filename_line
);
2501 f
= fopen(attr
, "we");
2503 if (fprintf(f
, "%s", value
) <= 0)
2504 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2507 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2512 char filename
[UTIL_PATH_SIZE
];
2513 char value
[UTIL_NAME_SIZE
];
2516 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
2517 sysctl_normalize(filename
);
2518 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2519 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2520 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2521 r
= sysctl_write(filename
, value
);
2523 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2526 case TK_A_RUN_BUILTIN
:
2527 case TK_A_RUN_PROGRAM
: {
2528 struct udev_list_entry
*entry
;
2530 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2531 udev_list_cleanup(&event
->run_list
);
2532 log_debug("RUN '%s' %s:%u",
2533 rules_str(rules
, cur
->key
.value_off
),
2534 rules_str(rules
, rule
->rule
.filename_off
),
2535 rule
->rule
.filename_line
);
2536 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2537 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2541 if (cur
->key
.rule_goto
== 0)
2543 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2548 case TK_M_PARENTS_MIN
:
2549 case TK_M_PARENTS_MAX
:
2552 log_error("wrong type %u", cur
->type
);
2559 /* fast-forward to next rule */
2560 cur
= rule
+ rule
->rule
.token_count
;
2564 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2570 _cleanup_strv_free_
char **tags
= NULL
;
2573 _cleanup_free_
char *path
= NULL
;
2576 if (rules
->tokens
== NULL
)
2579 cur
= &rules
->tokens
[0];
2582 switch (cur
->type
) {
2587 /* skip rules without a static_node tag */
2588 if (!rule
->rule
.has_static_node
)
2594 tags
= strv_free(tags
);
2603 mode
= cur
->key
.mode
;
2606 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2611 case TK_A_STATIC_NODE
: {
2612 char device_node
[UTIL_PATH_SIZE
];
2613 char tags_dir
[UTIL_PATH_SIZE
];
2614 char tag_symlink
[UTIL_PATH_SIZE
];
2617 /* we assure, that the permissions tokens are sorted before the static token */
2619 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2622 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2623 if (stat(device_node
, &stats
) != 0)
2625 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2628 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2630 STRV_FOREACH(t
, tags
) {
2631 _cleanup_free_
char *unescaped_filename
= NULL
;
2633 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2634 r
= mkdir_p(tags_dir
, 0755);
2636 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2638 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2640 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2641 r
= symlink(device_node
, tag_symlink
);
2642 if (r
< 0 && errno
!= EEXIST
)
2643 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2644 tag_symlink
, device_node
);
2650 /* don't touch the permissions if only the tags were set */
2651 if (mode
== 0 && uid
== 0 && gid
== 0)
2660 if (mode
!= (stats
.st_mode
& 01777)) {
2661 r
= chmod(device_node
, mode
);
2663 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2666 log_debug("chmod '%s' %#o", device_node
, mode
);
2669 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2670 r
= chown(device_node
, uid
, gid
);
2672 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2675 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2678 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2688 /* fast-forward to next rule */
2689 cur
= rule
+ rule
->rule
.token_count
;
2696 fchmod(fileno(f
), 0644);
2697 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2699 unlink("/run/udev/static_node-tags");