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 "alloc-util.h"
33 #include "conf-files.h"
36 #include "glob-util.h"
37 #include "path-util.h"
38 #include "stat-util.h"
40 #include "string-util.h"
42 #include "sysctl-util.h"
44 #include "user-util.h"
47 #define PREALLOC_TOKEN 2048
50 unsigned int name_off
;
57 static const char* const rules_dirs
[] = {
60 UDEVLIBEXECDIR
"/rules.d",
69 /* every key in the rules file becomes a token */
71 unsigned int token_cur
;
72 unsigned int token_max
;
74 /* all key strings are copied and de-duplicated in a single continuous string buffer */
75 struct strbuf
*strbuf
;
77 /* during rule parsing, uid/gid lookup results are cached */
79 unsigned int uids_cur
;
80 unsigned int uids_max
;
82 unsigned int gids_cur
;
83 unsigned int gids_max
;
86 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
87 return rules
->strbuf
->buf
+ off
;
90 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
91 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
94 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
108 enum string_glob_type
{
110 GL_PLAIN
, /* no special chars */
111 GL_GLOB
, /* shell globs ?,*,[] */
112 GL_SPLIT
, /* multi-value A|B */
113 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
114 GL_SOMETHING
, /* commonly used "?*" */
117 enum string_subst_type
{
124 /* tokens of a rule are sorted/handled in this order */
129 TK_M_ACTION
, /* val */
130 TK_M_DEVPATH
, /* val */
131 TK_M_KERNEL
, /* val */
132 TK_M_DEVLINK
, /* val */
134 TK_M_ENV
, /* val, attr */
136 TK_M_SUBSYSTEM
, /* val */
137 TK_M_DRIVER
, /* val */
138 TK_M_WAITFOR
, /* val */
139 TK_M_ATTR
, /* val, attr */
140 TK_M_SYSCTL
, /* val, attr */
143 TK_M_KERNELS
, /* val */
144 TK_M_SUBSYSTEMS
, /* val */
145 TK_M_DRIVERS
, /* val */
146 TK_M_ATTRS
, /* val, attr */
150 TK_M_TEST
, /* val, mode_t */
151 TK_M_PROGRAM
, /* val */
152 TK_M_IMPORT_FILE
, /* val */
153 TK_M_IMPORT_PROG
, /* val */
154 TK_M_IMPORT_BUILTIN
, /* val */
155 TK_M_IMPORT_DB
, /* val */
156 TK_M_IMPORT_CMDLINE
, /* val */
157 TK_M_IMPORT_PARENT
, /* val */
158 TK_M_RESULT
, /* val */
161 TK_A_STRING_ESCAPE_NONE
,
162 TK_A_STRING_ESCAPE_REPLACE
,
164 TK_A_INOTIFY_WATCH
, /* int */
165 TK_A_DEVLINK_PRIO
, /* int */
166 TK_A_OWNER
, /* val */
167 TK_A_GROUP
, /* val */
169 TK_A_OWNER_ID
, /* uid_t */
170 TK_A_GROUP_ID
, /* gid_t */
171 TK_A_MODE_ID
, /* mode_t */
173 TK_A_STATIC_NODE
, /* val */
174 TK_A_SECLABEL
, /* val, attr */
175 TK_A_ENV
, /* val, attr */
177 TK_A_DEVLINK
, /* val */
178 TK_A_ATTR
, /* val, attr */
179 TK_A_SYSCTL
, /* val, attr */
180 TK_A_RUN_BUILTIN
, /* val, bool */
181 TK_A_RUN_PROGRAM
, /* val, bool */
182 TK_A_GOTO
, /* size_t */
187 /* we try to pack stuff in a way that we take only 12 bytes per token */
190 unsigned char type
; /* same in rule and key */
192 enum token_type type
:8;
194 bool has_static_node
:1;
195 unsigned int unused
:6;
196 unsigned short token_count
;
197 unsigned int label_off
;
198 unsigned short filename_off
;
199 unsigned short filename_line
;
202 enum token_type type
:8;
203 enum operation_type op
:8;
204 enum string_glob_type glob
:8;
205 enum string_subst_type subst
:4;
206 enum string_subst_type attrsubst
:4;
207 unsigned int value_off
;
209 unsigned int attr_off
;
210 unsigned int rule_goto
;
216 enum udev_builtin_cmd builtin_cmd
;
224 struct udev_rules
*rules
;
226 struct token token
[MAX_TK
];
227 unsigned int token_cur
;
231 static const char *operation_str(enum operation_type type
) {
232 static const char *operation_strs
[] = {
233 [OP_UNSET
] = "UNSET",
234 [OP_MATCH
] = "match",
235 [OP_NOMATCH
] = "nomatch",
236 [OP_MATCH_MAX
] = "MATCH_MAX",
239 [OP_REMOVE
] = "remove",
240 [OP_ASSIGN
] = "assign",
241 [OP_ASSIGN_FINAL
] = "assign-final",
244 return operation_strs
[type
];
247 static const char *string_glob_str(enum string_glob_type type
) {
248 static const char *string_glob_strs
[] = {
249 [GL_UNSET
] = "UNSET",
250 [GL_PLAIN
] = "plain",
252 [GL_SPLIT
] = "split",
253 [GL_SPLIT_GLOB
] = "split-glob",
254 [GL_SOMETHING
] = "split-glob",
257 return string_glob_strs
[type
];
260 static const char *token_str(enum token_type type
) {
261 static const char *token_strs
[] = {
262 [TK_UNSET
] = "UNSET",
265 [TK_M_ACTION
] = "M ACTION",
266 [TK_M_DEVPATH
] = "M DEVPATH",
267 [TK_M_KERNEL
] = "M KERNEL",
268 [TK_M_DEVLINK
] = "M DEVLINK",
269 [TK_M_NAME
] = "M NAME",
270 [TK_M_ENV
] = "M ENV",
271 [TK_M_TAG
] = "M TAG",
272 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
273 [TK_M_DRIVER
] = "M DRIVER",
274 [TK_M_WAITFOR
] = "M WAITFOR",
275 [TK_M_ATTR
] = "M ATTR",
276 [TK_M_SYSCTL
] = "M SYSCTL",
278 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
279 [TK_M_KERNELS
] = "M KERNELS",
280 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
281 [TK_M_DRIVERS
] = "M DRIVERS",
282 [TK_M_ATTRS
] = "M ATTRS",
283 [TK_M_TAGS
] = "M TAGS",
284 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
286 [TK_M_TEST
] = "M TEST",
287 [TK_M_PROGRAM
] = "M PROGRAM",
288 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
289 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
290 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
291 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
292 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
293 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
294 [TK_M_RESULT
] = "M RESULT",
295 [TK_M_MAX
] = "M MAX",
297 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
298 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
299 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
300 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
301 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
302 [TK_A_OWNER
] = "A OWNER",
303 [TK_A_GROUP
] = "A GROUP",
304 [TK_A_MODE
] = "A MODE",
305 [TK_A_OWNER_ID
] = "A OWNER_ID",
306 [TK_A_GROUP_ID
] = "A GROUP_ID",
307 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
308 [TK_A_SECLABEL
] = "A SECLABEL",
309 [TK_A_MODE_ID
] = "A MODE_ID",
310 [TK_A_ENV
] = "A ENV",
311 [TK_A_TAG
] = "A ENV",
312 [TK_A_NAME
] = "A NAME",
313 [TK_A_DEVLINK
] = "A DEVLINK",
314 [TK_A_ATTR
] = "A ATTR",
315 [TK_A_SYSCTL
] = "A SYSCTL",
316 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
317 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
318 [TK_A_GOTO
] = "A GOTO",
323 return token_strs
[type
];
326 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
327 enum token_type type
= token
->type
;
328 enum operation_type op
= token
->key
.op
;
329 enum string_glob_type glob
= token
->key
.glob
;
330 const char *value
= str(rules
, token
->key
.value_off
);
331 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
336 const char *tks_ptr
= (char *)rules
->tokens
;
337 const char *tk_ptr
= (char *)token
;
338 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
340 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
341 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
342 idx
, token
->rule
.token_count
,
343 &rules
->buf
[token
->rule
.label_off
]);
355 case TK_M_SUBSYSTEMS
:
359 case TK_M_IMPORT_FILE
:
360 case TK_M_IMPORT_PROG
:
362 case TK_M_IMPORT_CMDLINE
:
363 case TK_M_IMPORT_PARENT
:
370 case TK_A_RUN_BUILTIN
:
371 case TK_A_RUN_PROGRAM
:
372 log_debug("%s %s '%s'(%s)",
373 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
375 case TK_M_IMPORT_BUILTIN
:
376 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
385 log_debug("%s %s '%s' '%s'(%s)",
386 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
390 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
392 case TK_A_STRING_ESCAPE_NONE
:
393 case TK_A_STRING_ESCAPE_REPLACE
:
394 case TK_A_DB_PERSIST
:
395 log_debug("%s", token_str(type
));
398 log_debug("%s %s '%s'(%s) %#o",
399 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
401 case TK_A_INOTIFY_WATCH
:
402 log_debug("%s %u", token_str(type
), token
->key
.watch
);
404 case TK_A_DEVLINK_PRIO
:
405 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
408 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
411 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
414 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
416 case TK_A_STATIC_NODE
:
417 log_debug("%s '%s'", token_str(type
), value
);
420 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
423 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
426 log_debug("* %s", token_str(type
));
428 case TK_M_PARENTS_MIN
:
429 case TK_M_PARENTS_MAX
:
432 log_debug("unknown type %u", type
);
437 static void dump_rules(struct udev_rules
*rules
) {
440 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
442 rules
->token_cur
* sizeof(struct token
),
445 for (i
= 0; i
< rules
->token_cur
; i
++)
446 dump_token(rules
, &rules
->tokens
[i
]);
449 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
450 static inline void dump_rules(struct udev_rules
*rules
) {}
453 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
454 /* grow buffer if needed */
455 if (rules
->token_cur
+1 >= rules
->token_max
) {
456 struct token
*tokens
;
459 /* double the buffer size */
460 add
= rules
->token_max
;
464 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
467 rules
->tokens
= tokens
;
468 rules
->token_max
+= add
;
470 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
475 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
481 /* lookup, if we know it already */
482 for (i
= 0; i
< rules
->uids_cur
; i
++) {
483 off
= rules
->uids
[i
].name_off
;
484 if (streq(rules_str(rules
, off
), owner
)) {
485 uid
= rules
->uids
[i
].uid
;
489 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
491 if (r
== -ENOENT
|| r
== -ESRCH
)
492 log_error("specified user '%s' unknown", owner
);
494 log_error_errno(r
, "error resolving user '%s': %m", owner
);
497 /* grow buffer if needed */
498 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
499 struct uid_gid
*uids
;
502 /* double the buffer size */
503 add
= rules
->uids_max
;
507 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
511 rules
->uids_max
+= add
;
513 rules
->uids
[rules
->uids_cur
].uid
= uid
;
514 off
= rules_add_string(rules
, owner
);
517 rules
->uids
[rules
->uids_cur
].name_off
= off
;
522 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
528 /* lookup, if we know it already */
529 for (i
= 0; i
< rules
->gids_cur
; i
++) {
530 off
= rules
->gids
[i
].name_off
;
531 if (streq(rules_str(rules
, off
), group
)) {
532 gid
= rules
->gids
[i
].gid
;
536 r
= get_group_creds(&group
, &gid
);
538 if (r
== -ENOENT
|| r
== -ESRCH
)
539 log_error("specified group '%s' unknown", group
);
541 log_error_errno(r
, "error resolving group '%s': %m", group
);
544 /* grow buffer if needed */
545 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
546 struct uid_gid
*gids
;
549 /* double the buffer size */
550 add
= rules
->gids_max
;
554 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
558 rules
->gids_max
+= add
;
560 rules
->gids
[rules
->gids_cur
].gid
= gid
;
561 off
= rules_add_string(rules
, group
);
564 rules
->gids
[rules
->gids_cur
].name_off
= off
;
569 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
576 while (isspace(key
[0]))
579 /* comment or empty line */
580 if (key
[0] == '#' || key
[0] == '\0')
583 /* split key/value */
584 val
= strchr(key
, '=');
591 while (isspace(val
[0]))
598 while (isspace(key
[len
-1]))
602 /* terminate value */
606 while (isspace(val
[len
-1]))
614 if (val
[0] == '"' || val
[0] == '\'') {
615 if (val
[len
-1] != val
[0]) {
616 log_debug("inconsistent quoting: '%s', skip", line
);
623 udev_device_add_property(dev
, key
, val
);
628 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
630 char line
[UTIL_LINE_SIZE
];
632 f
= fopen(filename
, "re");
635 while (fgets(line
, sizeof(line
), f
) != NULL
)
636 import_property_from_string(dev
, line
);
641 static int import_program_into_properties(struct udev_event
*event
,
643 usec_t timeout_warn_usec
,
644 const char *program
) {
645 char result
[UTIL_LINE_SIZE
];
649 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
654 while (line
!= NULL
) {
657 pos
= strchr(line
, '\n');
662 import_property_from_string(event
->dev
, line
);
668 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
669 struct udev_device
*dev_parent
;
670 struct udev_list_entry
*list_entry
;
675 dev_parent
= udev_device_get_parent(dev
);
676 if (dev_parent
== NULL
)
679 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
680 const char *key
= udev_list_entry_get_name(list_entry
);
681 const char *val
= udev_list_entry_get_value(list_entry
);
683 if (fnmatch(filter
, key
, 0) == 0)
684 udev_device_add_property(dev
, key
, val
);
689 static int attr_subst_subdir(char *attr
, size_t len
) {
692 if (strstr(attr
, "/*/")) {
694 char dirname
[UTIL_PATH_SIZE
];
698 strscpy(dirname
, sizeof(dirname
), attr
);
699 pos
= strstr(dirname
, "/*/");
704 dir
= opendir(dirname
);
708 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
711 if (dent
->d_name
[0] == '.')
713 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
714 if (stat(attr
, &stats
) == 0) {
726 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
731 if (linepos
== NULL
|| linepos
[0] == '\0')
734 /* skip whitespace */
735 while (isspace(linepos
[0]) || linepos
[0] == ',')
739 if (linepos
[0] == '\0')
745 if (linepos
[0] == '\0')
747 if (isspace(linepos
[0]))
749 if (linepos
[0] == '=')
751 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
752 if (linepos
[1] == '=')
756 /* remember end of key */
759 /* skip whitespace after key */
760 while (isspace(linepos
[0]))
762 if (linepos
[0] == '\0')
765 /* get operation type */
766 if (linepos
[0] == '=' && linepos
[1] == '=') {
769 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
772 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
775 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
778 } else if (linepos
[0] == '=') {
781 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
782 *op
= OP_ASSIGN_FINAL
;
790 /* skip whitespace after operator */
791 while (isspace(linepos
[0]))
793 if (linepos
[0] == '\0')
797 if (linepos
[0] == '"')
804 temp
= strchr(linepos
, '"');
810 /* move line to next key */
815 /* extract possible KEY{attr} */
816 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
820 attr
= strchr(str
, '{');
823 pos
= strchr(attr
, '}');
825 log_error("missing closing brace for format");
834 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
835 enum operation_type op
,
836 const char *value
, const void *data
) {
837 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
838 const char *attr
= NULL
;
840 memzero(token
, sizeof(struct token
));
852 case TK_M_SUBSYSTEMS
:
856 case TK_M_IMPORT_FILE
:
857 case TK_M_IMPORT_PROG
:
859 case TK_M_IMPORT_CMDLINE
:
860 case TK_M_IMPORT_PARENT
:
870 case TK_A_STATIC_NODE
:
871 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
873 case TK_M_IMPORT_BUILTIN
:
874 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
875 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
886 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
887 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
890 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
892 token
->key
.mode
= *(mode_t
*)data
;
894 case TK_A_STRING_ESCAPE_NONE
:
895 case TK_A_STRING_ESCAPE_REPLACE
:
896 case TK_A_DB_PERSIST
:
898 case TK_A_RUN_BUILTIN
:
899 case TK_A_RUN_PROGRAM
:
900 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
901 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
903 case TK_A_INOTIFY_WATCH
:
904 case TK_A_DEVLINK_PRIO
:
905 token
->key
.devlink_prio
= *(int *)data
;
908 token
->key
.uid
= *(uid_t
*)data
;
911 token
->key
.gid
= *(gid_t
*)data
;
914 token
->key
.mode
= *(mode_t
*)data
;
917 case TK_M_PARENTS_MIN
:
918 case TK_M_PARENTS_MAX
:
922 log_error("wrong type %u", type
);
926 if (value
!= NULL
&& type
< TK_M_MAX
) {
927 /* check if we need to split or call fnmatch() while matching rules */
928 enum string_glob_type glob
;
932 has_split
= (strchr(value
, '|') != NULL
);
933 has_glob
= string_is_glob(value
);
934 if (has_split
&& has_glob
) {
935 glob
= GL_SPLIT_GLOB
;
936 } else if (has_split
) {
938 } else if (has_glob
) {
939 if (streq(value
, "?*"))
946 token
->key
.glob
= glob
;
949 if (value
!= NULL
&& type
> TK_M_MAX
) {
950 /* check if assigned value has substitution chars */
952 token
->key
.subst
= SB_SUBSYS
;
953 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
954 token
->key
.subst
= SB_FORMAT
;
956 token
->key
.subst
= SB_NONE
;
960 /* check if property/attribute name has substitution chars */
962 token
->key
.attrsubst
= SB_SUBSYS
;
963 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
964 token
->key
.attrsubst
= SB_FORMAT
;
966 token
->key
.attrsubst
= SB_NONE
;
969 token
->key
.type
= type
;
971 rule_tmp
->token_cur
++;
972 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
973 log_error("temporary rule array too small");
979 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
981 unsigned int start
= 0;
982 unsigned int end
= rule_tmp
->token_cur
;
984 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
985 enum token_type next_val
= TK_UNSET
;
986 unsigned int next_idx
= 0;
989 /* find smallest value */
990 for (j
= start
; j
< end
; j
++) {
991 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
993 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
994 next_val
= rule_tmp
->token
[j
].type
;
999 /* add token and mark done */
1000 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1002 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1005 if (next_idx
== start
)
1007 if (next_idx
+1 == end
)
1013 static int add_rule(struct udev_rules
*rules
, char *line
,
1014 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1017 struct rule_tmp rule_tmp
= {
1019 .rule
.type
= TK_RULE
,
1022 /* the offset in the rule is limited to unsigned short */
1023 if (filename_off
< USHRT_MAX
)
1024 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1025 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1031 enum operation_type op
;
1033 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1034 /* Avoid erroring on trailing whitespace. This is probably rare
1035 * so save the work for the error case instead of always trying
1036 * to strip the trailing whitespace with strstrip(). */
1037 while (isblank(*linepos
))
1040 /* If we aren't at the end of the line, this is a parsing error.
1041 * Make a best effort to describe where the problem is. */
1042 if (!strchr(NEWLINE
, *linepos
)) {
1043 char buf
[2] = {*linepos
};
1044 _cleanup_free_
char *tmp
;
1047 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1048 filename
, lineno
, linepos
- line
+ 1, tmp
);
1049 if (*linepos
== '#')
1050 log_error("hint: comments can only start at beginning of line");
1055 if (streq(key
, "ACTION")) {
1056 if (op
> OP_MATCH_MAX
) {
1057 log_error("invalid ACTION operation");
1060 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1064 if (streq(key
, "DEVPATH")) {
1065 if (op
> OP_MATCH_MAX
) {
1066 log_error("invalid DEVPATH operation");
1069 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1073 if (streq(key
, "KERNEL")) {
1074 if (op
> OP_MATCH_MAX
) {
1075 log_error("invalid KERNEL operation");
1078 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1082 if (streq(key
, "SUBSYSTEM")) {
1083 if (op
> OP_MATCH_MAX
) {
1084 log_error("invalid SUBSYSTEM operation");
1087 /* bus, class, subsystem events should all be the same */
1088 if (streq(value
, "subsystem") ||
1089 streq(value
, "bus") ||
1090 streq(value
, "class")) {
1091 if (streq(value
, "bus") || streq(value
, "class"))
1092 log_error("'%s' must be specified as 'subsystem' "
1093 "please fix it in %s:%u", value
, filename
, lineno
);
1094 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1096 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1100 if (streq(key
, "DRIVER")) {
1101 if (op
> OP_MATCH_MAX
) {
1102 log_error("invalid DRIVER operation");
1105 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1109 if (startswith(key
, "ATTR{")) {
1110 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1112 log_error("error parsing ATTR attribute");
1115 if (op
== OP_REMOVE
) {
1116 log_error("invalid ATTR operation");
1119 if (op
< OP_MATCH_MAX
)
1120 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1122 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1126 if (startswith(key
, "SYSCTL{")) {
1127 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SYSCTL"));
1129 log_error("error parsing SYSCTL attribute");
1132 if (op
== OP_REMOVE
) {
1133 log_error("invalid SYSCTL operation");
1136 if (op
< OP_MATCH_MAX
)
1137 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1139 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1143 if (startswith(key
, "SECLABEL{")) {
1144 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1146 log_error("error parsing SECLABEL attribute");
1149 if (op
== OP_REMOVE
) {
1150 log_error("invalid SECLABEL operation");
1154 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1158 if (streq(key
, "KERNELS")) {
1159 if (op
> OP_MATCH_MAX
) {
1160 log_error("invalid KERNELS operation");
1163 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1167 if (streq(key
, "SUBSYSTEMS")) {
1168 if (op
> OP_MATCH_MAX
) {
1169 log_error("invalid SUBSYSTEMS operation");
1172 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1176 if (streq(key
, "DRIVERS")) {
1177 if (op
> OP_MATCH_MAX
) {
1178 log_error("invalid DRIVERS operation");
1181 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1185 if (startswith(key
, "ATTRS{")) {
1186 if (op
> OP_MATCH_MAX
) {
1187 log_error("invalid ATTRS operation");
1190 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1192 log_error("error parsing ATTRS attribute");
1195 if (startswith(attr
, "device/"))
1196 log_error("the 'device' link may not be available in a future kernel, "
1197 "please fix it in %s:%u", filename
, lineno
);
1198 else if (strstr(attr
, "../") != NULL
)
1199 log_error("do not reference parent sysfs directories directly, "
1200 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1201 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1205 if (streq(key
, "TAGS")) {
1206 if (op
> OP_MATCH_MAX
) {
1207 log_error("invalid TAGS operation");
1210 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1214 if (startswith(key
, "ENV{")) {
1215 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1217 log_error("error parsing ENV attribute");
1220 if (op
== OP_REMOVE
) {
1221 log_error("invalid ENV operation");
1224 if (op
< OP_MATCH_MAX
) {
1225 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1228 static const char *blacklist
[] = {
1243 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1244 if (!streq(attr
, blacklist
[i
]))
1246 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1249 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1255 if (streq(key
, "TAG")) {
1256 if (op
< OP_MATCH_MAX
)
1257 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1259 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1263 if (streq(key
, "PROGRAM")) {
1264 if (op
== OP_REMOVE
) {
1265 log_error("invalid PROGRAM operation");
1268 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1272 if (streq(key
, "RESULT")) {
1273 if (op
> OP_MATCH_MAX
) {
1274 log_error("invalid RESULT operation");
1277 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1281 if (startswith(key
, "IMPORT")) {
1282 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1284 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1287 if (op
== OP_REMOVE
) {
1288 log_error("invalid IMPORT operation");
1291 if (streq(attr
, "program")) {
1292 /* find known built-in command */
1293 if (value
[0] != '/') {
1294 enum udev_builtin_cmd cmd
;
1296 cmd
= udev_builtin_lookup(value
);
1297 if (cmd
< UDEV_BUILTIN_MAX
) {
1298 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1299 value
, filename
, lineno
);
1300 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1304 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1305 } else if (streq(attr
, "builtin")) {
1306 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1308 if (cmd
< UDEV_BUILTIN_MAX
)
1309 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1311 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1312 } else if (streq(attr
, "file")) {
1313 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1314 } else if (streq(attr
, "db")) {
1315 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1316 } else if (streq(attr
, "cmdline")) {
1317 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1318 } else if (streq(attr
, "parent")) {
1319 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1321 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1325 if (startswith(key
, "TEST")) {
1328 if (op
> OP_MATCH_MAX
) {
1329 log_error("invalid TEST operation");
1332 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1334 mode
= strtol(attr
, NULL
, 8);
1335 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1337 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1342 if (startswith(key
, "RUN")) {
1343 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1346 if (op
== OP_REMOVE
) {
1347 log_error("invalid RUN operation");
1351 if (streq(attr
, "builtin")) {
1352 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1354 if (cmd
< UDEV_BUILTIN_MAX
)
1355 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1357 log_error("RUN{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1358 } else if (streq(attr
, "program")) {
1359 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1361 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1363 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1369 if (streq(key
, "LABEL")) {
1370 if (op
== OP_REMOVE
) {
1371 log_error("invalid LABEL operation");
1374 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1378 if (streq(key
, "GOTO")) {
1379 if (op
== OP_REMOVE
) {
1380 log_error("invalid GOTO operation");
1383 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1387 if (startswith(key
, "NAME")) {
1388 if (op
== OP_REMOVE
) {
1389 log_error("invalid NAME operation");
1392 if (op
< OP_MATCH_MAX
) {
1393 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1395 if (streq(value
, "%k")) {
1396 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1397 "please remove it from %s:%u\n", filename
, lineno
);
1400 if (value
[0] == '\0') {
1401 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1402 "please remove it from %s:%u\n", filename
, lineno
);
1405 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1407 rule_tmp
.rule
.rule
.can_set_name
= true;
1411 if (streq(key
, "SYMLINK")) {
1412 if (op
== OP_REMOVE
) {
1413 log_error("invalid SYMLINK operation");
1416 if (op
< OP_MATCH_MAX
)
1417 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1419 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1420 rule_tmp
.rule
.rule
.can_set_name
= true;
1424 if (streq(key
, "OWNER")) {
1428 if (op
== OP_REMOVE
) {
1429 log_error("invalid OWNER operation");
1433 uid
= strtoul(value
, &endptr
, 10);
1434 if (endptr
[0] == '\0') {
1435 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1436 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1437 uid
= add_uid(rules
, value
);
1438 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1439 } else if (rules
->resolve_names
>= 0)
1440 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1442 rule_tmp
.rule
.rule
.can_set_name
= true;
1446 if (streq(key
, "GROUP")) {
1450 if (op
== OP_REMOVE
) {
1451 log_error("invalid GROUP operation");
1455 gid
= strtoul(value
, &endptr
, 10);
1456 if (endptr
[0] == '\0') {
1457 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1458 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1459 gid
= add_gid(rules
, value
);
1460 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1461 } else if (rules
->resolve_names
>= 0)
1462 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1464 rule_tmp
.rule
.rule
.can_set_name
= true;
1468 if (streq(key
, "MODE")) {
1472 if (op
== OP_REMOVE
) {
1473 log_error("invalid MODE operation");
1477 mode
= strtol(value
, &endptr
, 8);
1478 if (endptr
[0] == '\0')
1479 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1481 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1482 rule_tmp
.rule
.rule
.can_set_name
= true;
1486 if (streq(key
, "OPTIONS")) {
1489 if (op
== OP_REMOVE
) {
1490 log_error("invalid OPTIONS operation");
1494 pos
= strstr(value
, "link_priority=");
1496 int prio
= atoi(&pos
[strlen("link_priority=")]);
1498 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1501 pos
= strstr(value
, "string_escape=");
1503 pos
= &pos
[strlen("string_escape=")];
1504 if (startswith(pos
, "none"))
1505 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1506 else if (startswith(pos
, "replace"))
1507 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1510 pos
= strstr(value
, "db_persist");
1512 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1514 pos
= strstr(value
, "nowatch");
1518 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1520 pos
= strstr(value
, "watch");
1524 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1528 pos
= strstr(value
, "static_node=");
1530 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1531 rule_tmp
.rule
.rule
.has_static_node
= true;
1537 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1541 /* add rule token */
1542 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1543 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1546 /* add tokens to list, sorted by type */
1547 if (sort_token(rules
, &rule_tmp
) != 0)
1552 log_error("invalid rule '%s:%u'", filename
, lineno
);
1556 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1557 _cleanup_fclose_
FILE *f
= NULL
;
1558 unsigned int first_token
;
1559 unsigned int filename_off
;
1560 char line
[UTIL_LINE_SIZE
];
1564 f
= fopen(filename
, "re");
1566 if (errno
== ENOENT
)
1572 if (null_or_empty_fd(fileno(f
))) {
1573 log_debug("Skipping empty file: %s", filename
);
1576 log_debug("Reading rules file: %s", filename
);
1578 first_token
= rules
->token_cur
;
1579 filename_off
= rules_add_string(rules
, filename
);
1581 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1585 /* skip whitespace */
1588 while (isspace(key
[0]))
1599 /* continue reading if backslash+newline is found */
1600 while (line
[len
-2] == '\\') {
1601 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1603 if (strlen(&line
[len
-2]) < 2)
1609 if (len
+1 >= sizeof(line
)) {
1610 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1613 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1616 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1617 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1618 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1619 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1622 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1623 if (rules
->tokens
[j
].type
!= TK_RULE
)
1625 if (rules
->tokens
[j
].rule
.label_off
== 0)
1627 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1629 rules
->tokens
[i
].key
.rule_goto
= j
;
1632 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1633 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1639 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1640 struct udev_rules
*rules
;
1641 struct udev_list file_list
;
1642 struct token end_token
;
1646 rules
= new0(struct udev_rules
, 1);
1650 rules
->resolve_names
= resolve_names
;
1651 udev_list_init(udev
, &file_list
, true);
1653 /* init token array and string buffer */
1654 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1655 if (rules
->tokens
== NULL
)
1656 return udev_rules_unref(rules
);
1657 rules
->token_max
= PREALLOC_TOKEN
;
1659 rules
->strbuf
= strbuf_new();
1661 return udev_rules_unref(rules
);
1663 udev_rules_check_timestamp(rules
);
1665 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1667 log_error_errno(r
, "failed to enumerate rules files: %m");
1668 return udev_rules_unref(rules
);
1672 * The offset value in the rules strct is limited; add all
1673 * rules file names to the beginning of the string buffer.
1675 STRV_FOREACH(f
, files
)
1676 rules_add_string(rules
, *f
);
1678 STRV_FOREACH(f
, files
)
1679 parse_file(rules
, *f
);
1683 memzero(&end_token
, sizeof(struct token
));
1684 end_token
.type
= TK_END
;
1685 add_token(rules
, &end_token
);
1686 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1687 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1689 /* cleanup temporary strbuf data */
1690 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1691 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1692 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1693 strbuf_complete(rules
->strbuf
);
1695 /* cleanup uid/gid cache */
1696 rules
->uids
= mfree(rules
->uids
);
1697 rules
->uids_cur
= 0;
1698 rules
->uids_max
= 0;
1699 rules
->gids
= mfree(rules
->gids
);
1700 rules
->gids_cur
= 0;
1701 rules
->gids_max
= 0;
1707 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1710 free(rules
->tokens
);
1711 strbuf_cleanup(rules
->strbuf
);
1718 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1722 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1725 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1726 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1733 switch (token
->key
.glob
) {
1735 match
= (streq(key_value
, val
));
1738 match
= (fnmatch(key_value
, val
, 0) == 0);
1745 s
= rules_str(rules
, token
->key
.value_off
);
1750 next
= strchr(s
, '|');
1752 size_t matchlen
= (size_t)(next
- s
);
1754 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1758 match
= (streq(s
, val
));
1767 char value
[UTIL_PATH_SIZE
];
1769 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1771 while (key_value
!= NULL
) {
1772 pos
= strchr(key_value
, '|');
1777 match
= (fnmatch(key_value
, val
, 0) == 0);
1785 match
= (val
[0] != '\0');
1791 if (match
&& (token
->key
.op
== OP_MATCH
))
1793 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1798 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1800 char nbuf
[UTIL_NAME_SIZE
];
1802 char vbuf
[UTIL_NAME_SIZE
];
1805 name
= rules_str(rules
, cur
->key
.attr_off
);
1806 switch (cur
->key
.attrsubst
) {
1808 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1812 value
= udev_device_get_sysattr_value(dev
, name
);
1817 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1825 /* remove trailing whitespace, if not asked to match for it */
1826 len
= strlen(value
);
1827 if (len
> 0 && isspace(value
[len
-1])) {
1828 const char *key_value
;
1831 key_value
= rules_str(rules
, cur
->key
.value_off
);
1832 klen
= strlen(key_value
);
1833 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1834 if (value
!= vbuf
) {
1835 strscpy(vbuf
, sizeof(vbuf
), value
);
1838 while (len
> 0 && isspace(vbuf
[--len
]))
1843 return match_key(rules
, cur
, value
);
1852 int udev_rules_apply_to_event(struct udev_rules
*rules
,
1853 struct udev_event
*event
,
1854 usec_t timeout_usec
,
1855 usec_t timeout_warn_usec
,
1856 struct udev_list
*properties_list
) {
1859 enum escape_type esc
= ESCAPE_UNSET
;
1862 if (rules
->tokens
== NULL
)
1865 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1866 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1867 udev_device_get_ifindex(event
->dev
) > 0));
1869 /* loop through token list, match, run actions or forward to next rule */
1870 cur
= &rules
->tokens
[0];
1873 dump_token(rules
, cur
);
1874 switch (cur
->type
) {
1878 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1879 if (!can_set_name
&& rule
->rule
.can_set_name
)
1884 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1888 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1892 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1895 case TK_M_DEVLINK
: {
1896 struct udev_list_entry
*list_entry
;
1899 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1900 const char *devlink
;
1902 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1903 if (match_key(rules
, cur
, devlink
) == 0) {
1913 if (match_key(rules
, cur
, event
->name
) != 0)
1917 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1920 value
= udev_device_get_property_value(event
->dev
, key_name
);
1922 /* check global properties */
1923 if (!value
&& properties_list
) {
1924 struct udev_list_entry
*list_entry
;
1926 list_entry
= udev_list_get_entry(properties_list
);
1927 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1928 if (list_entry
!= NULL
)
1929 value
= udev_list_entry_get_value(list_entry
);
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
)) ||
1949 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1953 case TK_M_SUBSYSTEM
:
1954 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1958 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1962 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1966 char filename
[UTIL_PATH_SIZE
];
1967 _cleanup_free_
char *value
= NULL
;
1970 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
1971 sysctl_normalize(filename
);
1972 if (sysctl_read(filename
, &value
) < 0)
1975 len
= strlen(value
);
1976 while (len
> 0 && isspace(value
[--len
]))
1978 if (match_key(rules
, cur
, value
) != 0)
1983 case TK_M_SUBSYSTEMS
:
1989 /* get whole sequence of parent matches */
1991 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1994 /* loop over parents */
1995 event
->dev_parent
= event
->dev
;
1999 /* loop over sequence of parent match keys */
2000 for (key
= cur
; key
< next
; key
++ ) {
2001 dump_token(rules
, key
);
2004 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2007 case TK_M_SUBSYSTEMS
:
2008 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2012 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2016 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2020 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
2022 if (match
&& key
->key
.op
== OP_NOMATCH
)
2024 if (!match
&& key
->key
.op
== OP_MATCH
)
2035 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2036 if (event
->dev_parent
== NULL
)
2039 /* move behind our sequence of parent match keys */
2044 char filename
[UTIL_PATH_SIZE
];
2045 struct stat statbuf
;
2048 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2049 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2050 if (filename
[0] != '/') {
2051 char tmp
[UTIL_PATH_SIZE
];
2053 strscpy(tmp
, sizeof(tmp
), filename
);
2054 strscpyl(filename
, sizeof(filename
),
2055 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2058 attr_subst_subdir(filename
, sizeof(filename
));
2060 match
= (stat(filename
, &statbuf
) == 0);
2061 if (match
&& cur
->key
.mode
> 0)
2062 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2063 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2065 if (!match
&& cur
->key
.op
== OP_MATCH
)
2069 case TK_M_PROGRAM
: {
2070 char program
[UTIL_PATH_SIZE
];
2071 char result
[UTIL_LINE_SIZE
];
2073 event
->program_result
= mfree(event
->program_result
);
2074 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2075 log_debug("PROGRAM '%s' %s:%u",
2077 rules_str(rules
, rule
->rule
.filename_off
),
2078 rule
->rule
.filename_line
);
2080 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
2081 if (cur
->key
.op
!= OP_NOMATCH
)
2086 util_remove_trailing_chars(result
, '\n');
2087 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2088 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2090 log_debug("%i character(s) replaced" , count
);
2092 event
->program_result
= strdup(result
);
2093 if (cur
->key
.op
== OP_NOMATCH
)
2098 case TK_M_IMPORT_FILE
: {
2099 char import
[UTIL_PATH_SIZE
];
2101 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2102 if (import_file_into_properties(event
->dev
, import
) != 0)
2103 if (cur
->key
.op
!= OP_NOMATCH
)
2107 case TK_M_IMPORT_PROG
: {
2108 char import
[UTIL_PATH_SIZE
];
2110 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2111 log_debug("IMPORT '%s' %s:%u",
2113 rules_str(rules
, rule
->rule
.filename_off
),
2114 rule
->rule
.filename_line
);
2116 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
2117 if (cur
->key
.op
!= OP_NOMATCH
)
2121 case TK_M_IMPORT_BUILTIN
: {
2122 char command
[UTIL_PATH_SIZE
];
2124 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2125 /* check if we ran already */
2126 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2127 log_debug("IMPORT builtin skip '%s' %s:%u",
2128 udev_builtin_name(cur
->key
.builtin_cmd
),
2129 rules_str(rules
, rule
->rule
.filename_off
),
2130 rule
->rule
.filename_line
);
2131 /* return the result from earlier run */
2132 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2133 if (cur
->key
.op
!= OP_NOMATCH
)
2138 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2141 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2142 log_debug("IMPORT builtin '%s' %s:%u",
2143 udev_builtin_name(cur
->key
.builtin_cmd
),
2144 rules_str(rules
, rule
->rule
.filename_off
),
2145 rule
->rule
.filename_line
);
2147 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2148 /* remember failure */
2149 log_debug("IMPORT builtin '%s' returned non-zero",
2150 udev_builtin_name(cur
->key
.builtin_cmd
));
2151 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2152 if (cur
->key
.op
!= OP_NOMATCH
)
2157 case TK_M_IMPORT_DB
: {
2158 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2161 value
= udev_device_get_property_value(event
->dev_db
, key
);
2163 udev_device_add_property(event
->dev
, key
, value
);
2165 if (cur
->key
.op
!= OP_NOMATCH
)
2170 case TK_M_IMPORT_CMDLINE
: {
2172 bool imported
= false;
2174 f
= fopen("/proc/cmdline", "re");
2178 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2179 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2182 pos
= strstr(cmdline
, key
);
2185 if (pos
[0] == '\0' || isspace(pos
[0])) {
2186 /* we import simple flags as 'FLAG=1' */
2187 udev_device_add_property(event
->dev
, key
, "1");
2189 } else if (pos
[0] == '=') {
2194 while (pos
[0] != '\0' && !isspace(pos
[0]))
2197 udev_device_add_property(event
->dev
, key
, value
);
2204 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2208 case TK_M_IMPORT_PARENT
: {
2209 char import
[UTIL_PATH_SIZE
];
2211 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2212 if (import_parent_into_properties(event
->dev
, import
) != 0)
2213 if (cur
->key
.op
!= OP_NOMATCH
)
2218 if (match_key(rules
, cur
, event
->program_result
) != 0)
2221 case TK_A_STRING_ESCAPE_NONE
:
2224 case TK_A_STRING_ESCAPE_REPLACE
:
2225 esc
= ESCAPE_REPLACE
;
2227 case TK_A_DB_PERSIST
:
2228 udev_device_set_db_persist(event
->dev
);
2230 case TK_A_INOTIFY_WATCH
:
2231 if (event
->inotify_watch_final
)
2233 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2234 event
->inotify_watch_final
= true;
2235 event
->inotify_watch
= cur
->key
.watch
;
2237 case TK_A_DEVLINK_PRIO
:
2238 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2241 char owner
[UTIL_NAME_SIZE
];
2242 const char *ow
= owner
;
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 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2253 if (r
== -ENOENT
|| r
== -ESRCH
)
2254 log_error("specified user '%s' unknown", owner
);
2256 log_error_errno(r
, "error resolving user '%s': %m", owner
);
2260 log_debug("OWNER %u %s:%u",
2262 rules_str(rules
, rule
->rule
.filename_off
),
2263 rule
->rule
.filename_line
);
2267 char group
[UTIL_NAME_SIZE
];
2268 const char *gr
= group
;
2271 if (event
->group_final
)
2273 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2274 event
->group_final
= true;
2275 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2276 event
->group_set
= true;
2277 r
= get_group_creds(&gr
, &event
->gid
);
2279 if (r
== -ENOENT
|| r
== -ESRCH
)
2280 log_error("specified group '%s' unknown", group
);
2282 log_error_errno(r
, "error resolving group '%s': %m", group
);
2286 log_debug("GROUP %u %s:%u",
2288 rules_str(rules
, rule
->rule
.filename_off
),
2289 rule
->rule
.filename_line
);
2293 char mode_str
[UTIL_NAME_SIZE
];
2297 if (event
->mode_final
)
2299 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2300 mode
= strtol(mode_str
, &endptr
, 8);
2301 if (endptr
[0] != '\0') {
2302 log_error("ignoring invalid mode '%s'", mode_str
);
2305 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2306 event
->mode_final
= true;
2307 event
->mode_set
= true;
2309 log_debug("MODE %#o %s:%u",
2311 rules_str(rules
, rule
->rule
.filename_off
),
2312 rule
->rule
.filename_line
);
2316 if (event
->owner_final
)
2318 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2319 event
->owner_final
= true;
2320 event
->owner_set
= true;
2321 event
->uid
= cur
->key
.uid
;
2322 log_debug("OWNER %u %s:%u",
2324 rules_str(rules
, rule
->rule
.filename_off
),
2325 rule
->rule
.filename_line
);
2328 if (event
->group_final
)
2330 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2331 event
->group_final
= true;
2332 event
->group_set
= true;
2333 event
->gid
= cur
->key
.gid
;
2334 log_debug("GROUP %u %s:%u",
2336 rules_str(rules
, rule
->rule
.filename_off
),
2337 rule
->rule
.filename_line
);
2340 if (event
->mode_final
)
2342 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2343 event
->mode_final
= true;
2344 event
->mode_set
= true;
2345 event
->mode
= cur
->key
.mode
;
2346 log_debug("MODE %#o %s:%u",
2348 rules_str(rules
, rule
->rule
.filename_off
),
2349 rule
->rule
.filename_line
);
2351 case TK_A_SECLABEL
: {
2352 const char *name
, *label
;
2354 name
= rules_str(rules
, cur
->key
.attr_off
);
2355 label
= rules_str(rules
, cur
->key
.value_off
);
2356 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2357 udev_list_cleanup(&event
->seclabel_list
);
2358 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2359 log_debug("SECLABEL{%s}='%s' %s:%u",
2361 rules_str(rules
, rule
->rule
.filename_off
),
2362 rule
->rule
.filename_line
);
2366 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2367 char *value
= rules_str(rules
, cur
->key
.value_off
);
2368 char value_new
[UTIL_NAME_SIZE
];
2369 const char *value_old
= NULL
;
2371 if (value
[0] == '\0') {
2372 if (cur
->key
.op
== OP_ADD
)
2374 udev_device_add_property(event
->dev
, name
, NULL
);
2378 if (cur
->key
.op
== OP_ADD
)
2379 value_old
= udev_device_get_property_value(event
->dev
, name
);
2381 char temp
[UTIL_NAME_SIZE
];
2383 /* append value separated by space */
2384 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2385 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2387 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2389 udev_device_add_property(event
->dev
, name
, value_new
);
2393 char tag
[UTIL_PATH_SIZE
];
2396 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2397 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2398 udev_device_cleanup_tags_list(event
->dev
);
2399 for (p
= tag
; *p
!= '\0'; p
++) {
2400 if ((*p
>= 'a' && *p
<= 'z') ||
2401 (*p
>= 'A' && *p
<= 'Z') ||
2402 (*p
>= '0' && *p
<= '9') ||
2403 *p
== '-' || *p
== '_')
2405 log_error("ignoring invalid tag name '%s'", tag
);
2408 if (cur
->key
.op
== OP_REMOVE
)
2409 udev_device_remove_tag(event
->dev
, tag
);
2411 udev_device_add_tag(event
->dev
, tag
);
2415 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2417 char name_str
[UTIL_PATH_SIZE
];
2420 if (event
->name_final
)
2422 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2423 event
->name_final
= true;
2424 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2425 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2426 count
= util_replace_chars(name_str
, "/");
2428 log_debug("%i character(s) replaced", count
);
2430 if (major(udev_device_get_devnum(event
->dev
)) &&
2431 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2432 log_error("NAME=\"%s\" ignored, kernel device nodes "
2433 "can not be renamed; please fix it in %s:%u\n", name
,
2434 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2437 free_and_strdup(&event
->name
, name_str
);
2438 log_debug("NAME '%s' %s:%u",
2440 rules_str(rules
, rule
->rule
.filename_off
),
2441 rule
->rule
.filename_line
);
2444 case TK_A_DEVLINK
: {
2445 char temp
[UTIL_PATH_SIZE
];
2446 char filename
[UTIL_PATH_SIZE
];
2450 if (event
->devlink_final
)
2452 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2454 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2455 event
->devlink_final
= true;
2456 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2457 udev_device_cleanup_devlinks_list(event
->dev
);
2459 /* allow multiple symlinks separated by spaces */
2460 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2461 if (esc
== ESCAPE_UNSET
)
2462 count
= util_replace_chars(temp
, "/ ");
2463 else if (esc
== ESCAPE_REPLACE
)
2464 count
= util_replace_chars(temp
, "/");
2466 log_debug("%i character(s) replaced" , count
);
2468 while (isspace(pos
[0]))
2470 next
= strchr(pos
, ' ');
2471 while (next
!= NULL
) {
2473 log_debug("LINK '%s' %s:%u", pos
,
2474 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2475 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2476 udev_device_add_devlink(event
->dev
, filename
);
2477 while (isspace(next
[1]))
2480 next
= strchr(pos
, ' ');
2482 if (pos
[0] != '\0') {
2483 log_debug("LINK '%s' %s:%u", pos
,
2484 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2485 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2486 udev_device_add_devlink(event
->dev
, filename
);
2491 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2492 char attr
[UTIL_PATH_SIZE
];
2493 char value
[UTIL_NAME_SIZE
];
2496 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2497 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2498 attr_subst_subdir(attr
, sizeof(attr
));
2500 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2501 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2502 rules_str(rules
, rule
->rule
.filename_off
),
2503 rule
->rule
.filename_line
);
2504 f
= fopen(attr
, "we");
2506 if (fprintf(f
, "%s", value
) <= 0)
2507 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2510 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2515 char filename
[UTIL_PATH_SIZE
];
2516 char value
[UTIL_NAME_SIZE
];
2519 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
2520 sysctl_normalize(filename
);
2521 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2522 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2523 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2524 r
= sysctl_write(filename
, value
);
2526 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2529 case TK_A_RUN_BUILTIN
:
2530 case TK_A_RUN_PROGRAM
: {
2531 struct udev_list_entry
*entry
;
2533 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2534 udev_list_cleanup(&event
->run_list
);
2535 log_debug("RUN '%s' %s:%u",
2536 rules_str(rules
, cur
->key
.value_off
),
2537 rules_str(rules
, rule
->rule
.filename_off
),
2538 rule
->rule
.filename_line
);
2539 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2540 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2544 if (cur
->key
.rule_goto
== 0)
2546 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2551 case TK_M_PARENTS_MIN
:
2552 case TK_M_PARENTS_MAX
:
2555 log_error("wrong type %u", cur
->type
);
2562 /* fast-forward to next rule */
2563 cur
= rule
+ rule
->rule
.token_count
;
2567 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2573 _cleanup_strv_free_
char **tags
= NULL
;
2576 _cleanup_free_
char *path
= NULL
;
2579 if (rules
->tokens
== NULL
)
2582 cur
= &rules
->tokens
[0];
2585 switch (cur
->type
) {
2590 /* skip rules without a static_node tag */
2591 if (!rule
->rule
.has_static_node
)
2597 tags
= strv_free(tags
);
2606 mode
= cur
->key
.mode
;
2609 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2614 case TK_A_STATIC_NODE
: {
2615 char device_node
[UTIL_PATH_SIZE
];
2616 char tags_dir
[UTIL_PATH_SIZE
];
2617 char tag_symlink
[UTIL_PATH_SIZE
];
2620 /* we assure, that the permissions tokens are sorted before the static token */
2622 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2625 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2626 if (stat(device_node
, &stats
) != 0)
2628 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2631 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2633 STRV_FOREACH(t
, tags
) {
2634 _cleanup_free_
char *unescaped_filename
= NULL
;
2636 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2637 r
= mkdir_p(tags_dir
, 0755);
2639 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2641 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2643 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2644 r
= symlink(device_node
, tag_symlink
);
2645 if (r
< 0 && errno
!= EEXIST
)
2646 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2647 tag_symlink
, device_node
);
2653 /* don't touch the permissions if only the tags were set */
2654 if (mode
== 0 && uid
== 0 && gid
== 0)
2663 if (mode
!= (stats
.st_mode
& 01777)) {
2664 r
= chmod(device_node
, mode
);
2666 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2669 log_debug("chmod '%s' %#o", device_node
, mode
);
2672 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2673 r
= chown(device_node
, uid
, gid
);
2675 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2678 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2681 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2691 /* fast-forward to next rule */
2692 cur
= rule
+ rule
->rule
.token_count
;
2699 fchmod(fileno(f
), 0644);
2700 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2702 unlink("/run/udev/static_node-tags");