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/>.
31 #include "alloc-util.h"
32 #include "conf-files.h"
33 #include "dirent-util.h"
37 #include "glob-util.h"
38 #include "path-util.h"
39 #include "stat-util.h"
40 #include "stdio-util.h"
42 #include "string-util.h"
44 #include "sysctl-util.h"
46 #include "user-util.h"
49 #define PREALLOC_TOKEN 2048
52 unsigned int name_off
;
59 static const char* const rules_dirs
[] = {
62 UDEVLIBEXECDIR
"/rules.d",
71 /* every key in the rules file becomes a token */
73 unsigned int token_cur
;
74 unsigned int token_max
;
76 /* all key strings are copied and de-duplicated in a single continuous string buffer */
77 struct strbuf
*strbuf
;
79 /* during rule parsing, uid/gid lookup results are cached */
81 unsigned int uids_cur
;
82 unsigned int uids_max
;
84 unsigned int gids_cur
;
85 unsigned int gids_max
;
88 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
89 return rules
->strbuf
->buf
+ off
;
92 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
93 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
96 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
110 enum string_glob_type
{
112 GL_PLAIN
, /* no special chars */
113 GL_GLOB
, /* shell globs ?,*,[] */
114 GL_SPLIT
, /* multi-value A|B */
115 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
116 GL_SOMETHING
, /* commonly used "?*" */
119 enum string_subst_type
{
126 /* tokens of a rule are sorted/handled in this order */
131 TK_M_ACTION
, /* val */
132 TK_M_DEVPATH
, /* val */
133 TK_M_KERNEL
, /* val */
134 TK_M_DEVLINK
, /* val */
136 TK_M_ENV
, /* val, attr */
138 TK_M_SUBSYSTEM
, /* val */
139 TK_M_DRIVER
, /* val */
140 TK_M_WAITFOR
, /* val */
141 TK_M_ATTR
, /* val, attr */
142 TK_M_SYSCTL
, /* val, attr */
145 TK_M_KERNELS
, /* val */
146 TK_M_SUBSYSTEMS
, /* val */
147 TK_M_DRIVERS
, /* val */
148 TK_M_ATTRS
, /* val, attr */
152 TK_M_TEST
, /* val, mode_t */
153 TK_M_PROGRAM
, /* val */
154 TK_M_IMPORT_FILE
, /* val */
155 TK_M_IMPORT_PROG
, /* val */
156 TK_M_IMPORT_BUILTIN
, /* val */
157 TK_M_IMPORT_DB
, /* val */
158 TK_M_IMPORT_CMDLINE
, /* val */
159 TK_M_IMPORT_PARENT
, /* val */
160 TK_M_RESULT
, /* val */
163 TK_A_STRING_ESCAPE_NONE
,
164 TK_A_STRING_ESCAPE_REPLACE
,
166 TK_A_INOTIFY_WATCH
, /* int */
167 TK_A_DEVLINK_PRIO
, /* int */
168 TK_A_OWNER
, /* val */
169 TK_A_GROUP
, /* val */
171 TK_A_OWNER_ID
, /* uid_t */
172 TK_A_GROUP_ID
, /* gid_t */
173 TK_A_MODE_ID
, /* mode_t */
175 TK_A_STATIC_NODE
, /* val */
176 TK_A_SECLABEL
, /* val, attr */
177 TK_A_ENV
, /* val, attr */
179 TK_A_DEVLINK
, /* val */
180 TK_A_ATTR
, /* val, attr */
181 TK_A_SYSCTL
, /* val, attr */
182 TK_A_RUN_BUILTIN
, /* val, bool */
183 TK_A_RUN_PROGRAM
, /* val, bool */
184 TK_A_GOTO
, /* size_t */
189 /* we try to pack stuff in a way that we take only 12 bytes per token */
192 unsigned char type
; /* same in rule and key */
194 enum token_type type
:8;
196 bool has_static_node
:1;
197 unsigned int unused
:6;
198 unsigned short token_count
;
199 unsigned int label_off
;
200 unsigned short filename_off
;
201 unsigned short filename_line
;
204 enum token_type type
:8;
205 enum operation_type op
:8;
206 enum string_glob_type glob
:8;
207 enum string_subst_type subst
:4;
208 enum string_subst_type attrsubst
:4;
209 unsigned int value_off
;
211 unsigned int attr_off
;
212 unsigned int rule_goto
;
218 enum udev_builtin_cmd builtin_cmd
;
226 struct udev_rules
*rules
;
228 struct token token
[MAX_TK
];
229 unsigned int token_cur
;
233 static const char *operation_str(enum operation_type type
) {
234 static const char *operation_strs
[] = {
235 [OP_UNSET
] = "UNSET",
236 [OP_MATCH
] = "match",
237 [OP_NOMATCH
] = "nomatch",
238 [OP_MATCH_MAX
] = "MATCH_MAX",
241 [OP_REMOVE
] = "remove",
242 [OP_ASSIGN
] = "assign",
243 [OP_ASSIGN_FINAL
] = "assign-final",
246 return operation_strs
[type
];
249 static const char *string_glob_str(enum string_glob_type type
) {
250 static const char *string_glob_strs
[] = {
251 [GL_UNSET
] = "UNSET",
252 [GL_PLAIN
] = "plain",
254 [GL_SPLIT
] = "split",
255 [GL_SPLIT_GLOB
] = "split-glob",
256 [GL_SOMETHING
] = "split-glob",
259 return string_glob_strs
[type
];
262 static const char *token_str(enum token_type type
) {
263 static const char *token_strs
[] = {
264 [TK_UNSET
] = "UNSET",
267 [TK_M_ACTION
] = "M ACTION",
268 [TK_M_DEVPATH
] = "M DEVPATH",
269 [TK_M_KERNEL
] = "M KERNEL",
270 [TK_M_DEVLINK
] = "M DEVLINK",
271 [TK_M_NAME
] = "M NAME",
272 [TK_M_ENV
] = "M ENV",
273 [TK_M_TAG
] = "M TAG",
274 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
275 [TK_M_DRIVER
] = "M DRIVER",
276 [TK_M_WAITFOR
] = "M WAITFOR",
277 [TK_M_ATTR
] = "M ATTR",
278 [TK_M_SYSCTL
] = "M SYSCTL",
280 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
281 [TK_M_KERNELS
] = "M KERNELS",
282 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
283 [TK_M_DRIVERS
] = "M DRIVERS",
284 [TK_M_ATTRS
] = "M ATTRS",
285 [TK_M_TAGS
] = "M TAGS",
286 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
288 [TK_M_TEST
] = "M TEST",
289 [TK_M_PROGRAM
] = "M PROGRAM",
290 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
291 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
292 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
293 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
294 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
295 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
296 [TK_M_RESULT
] = "M RESULT",
297 [TK_M_MAX
] = "M MAX",
299 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
300 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
301 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
302 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
303 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
304 [TK_A_OWNER
] = "A OWNER",
305 [TK_A_GROUP
] = "A GROUP",
306 [TK_A_MODE
] = "A MODE",
307 [TK_A_OWNER_ID
] = "A OWNER_ID",
308 [TK_A_GROUP_ID
] = "A GROUP_ID",
309 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
310 [TK_A_SECLABEL
] = "A SECLABEL",
311 [TK_A_MODE_ID
] = "A MODE_ID",
312 [TK_A_ENV
] = "A ENV",
313 [TK_A_TAG
] = "A ENV",
314 [TK_A_NAME
] = "A NAME",
315 [TK_A_DEVLINK
] = "A DEVLINK",
316 [TK_A_ATTR
] = "A ATTR",
317 [TK_A_SYSCTL
] = "A SYSCTL",
318 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
319 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
320 [TK_A_GOTO
] = "A GOTO",
325 return token_strs
[type
];
328 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
329 enum token_type type
= token
->type
;
330 enum operation_type op
= token
->key
.op
;
331 enum string_glob_type glob
= token
->key
.glob
;
332 const char *value
= rules_str(rules
, token
->key
.value_off
);
333 const char *attr
= &rules
->strbuf
->buf
[token
->key
.attr_off
];
338 const char *tks_ptr
= (char *)rules
->tokens
;
339 const char *tk_ptr
= (char *)token
;
340 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
342 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
343 &rules
->strbuf
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
344 idx
, token
->rule
.token_count
,
345 &rules
->strbuf
->buf
[token
->rule
.label_off
]);
357 case TK_M_SUBSYSTEMS
:
361 case TK_M_IMPORT_FILE
:
362 case TK_M_IMPORT_PROG
:
364 case TK_M_IMPORT_CMDLINE
:
365 case TK_M_IMPORT_PARENT
:
372 case TK_A_RUN_BUILTIN
:
373 case TK_A_RUN_PROGRAM
:
374 log_debug("%s %s '%s'(%s)",
375 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
377 case TK_M_IMPORT_BUILTIN
:
378 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
387 log_debug("%s %s '%s' '%s'(%s)",
388 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
392 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
394 case TK_A_STRING_ESCAPE_NONE
:
395 case TK_A_STRING_ESCAPE_REPLACE
:
396 case TK_A_DB_PERSIST
:
397 log_debug("%s", token_str(type
));
400 log_debug("%s %s '%s'(%s) %#o",
401 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
403 case TK_A_INOTIFY_WATCH
:
404 log_debug("%s %u", token_str(type
), token
->key
.watch
);
406 case TK_A_DEVLINK_PRIO
:
407 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
410 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
413 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
416 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
418 case TK_A_STATIC_NODE
:
419 log_debug("%s '%s'", token_str(type
), value
);
422 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
425 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
428 log_debug("* %s", token_str(type
));
430 case TK_M_PARENTS_MIN
:
431 case TK_M_PARENTS_MAX
:
434 log_debug("unknown type %u", type
);
439 static void dump_rules(struct udev_rules
*rules
) {
442 log_debug("dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings",
444 rules
->token_cur
* sizeof(struct token
),
445 rules
->strbuf
->nodes_count
,
447 for (i
= 0; i
< rules
->token_cur
; i
++)
448 dump_token(rules
, &rules
->tokens
[i
]);
451 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
452 static inline void dump_rules(struct udev_rules
*rules
) {}
455 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
456 /* grow buffer if needed */
457 if (rules
->token_cur
+1 >= rules
->token_max
) {
458 struct token
*tokens
;
461 /* double the buffer size */
462 add
= rules
->token_max
;
466 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
469 rules
->tokens
= tokens
;
470 rules
->token_max
+= add
;
472 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
477 static void log_unknown_owner(int error
, const char *entity
, const char *owner
) {
478 if (IN_SET(abs(error
), ENOENT
, ESRCH
))
479 log_error("Specified %s '%s' unknown", entity
, owner
);
481 log_error_errno(error
, "Error resolving %s '%s': %m", entity
, owner
);
484 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
490 /* lookup, if we know it already */
491 for (i
= 0; i
< rules
->uids_cur
; i
++) {
492 off
= rules
->uids
[i
].name_off
;
493 if (streq(rules_str(rules
, off
), owner
)) {
494 uid
= rules
->uids
[i
].uid
;
498 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
500 log_unknown_owner(r
, "user", owner
);
502 /* grow buffer if needed */
503 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
504 struct uid_gid
*uids
;
507 /* double the buffer size */
508 add
= rules
->uids_max
;
512 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
516 rules
->uids_max
+= add
;
518 rules
->uids
[rules
->uids_cur
].uid
= uid
;
519 off
= rules_add_string(rules
, owner
);
522 rules
->uids
[rules
->uids_cur
].name_off
= off
;
527 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
533 /* lookup, if we know it already */
534 for (i
= 0; i
< rules
->gids_cur
; i
++) {
535 off
= rules
->gids
[i
].name_off
;
536 if (streq(rules_str(rules
, off
), group
)) {
537 gid
= rules
->gids
[i
].gid
;
541 r
= get_group_creds(&group
, &gid
);
543 log_unknown_owner(r
, "group", group
);
545 /* grow buffer if needed */
546 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
547 struct uid_gid
*gids
;
550 /* double the buffer size */
551 add
= rules
->gids_max
;
555 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
559 rules
->gids_max
+= add
;
561 rules
->gids
[rules
->gids_cur
].gid
= gid
;
562 off
= rules_add_string(rules
, group
);
565 rules
->gids
[rules
->gids_cur
].name_off
= off
;
570 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
577 while (isspace(key
[0]))
580 /* comment or empty line */
581 if (key
[0] == '#' || key
[0] == '\0')
584 /* split key/value */
585 val
= strchr(key
, '=');
592 while (isspace(val
[0]))
599 while (isspace(key
[len
-1]))
603 /* terminate value */
607 while (isspace(val
[len
-1]))
615 if (val
[0] == '"' || val
[0] == '\'') {
616 if (len
== 1 || val
[len
-1] != val
[0]) {
617 log_debug("inconsistent quoting: '%s', skip", line
);
624 udev_device_add_property(dev
, key
, val
);
629 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
631 char line
[UTIL_LINE_SIZE
];
633 f
= fopen(filename
, "re");
636 while (fgets(line
, sizeof(line
), f
) != NULL
)
637 import_property_from_string(dev
, line
);
642 static int import_program_into_properties(struct udev_event
*event
,
644 usec_t timeout_warn_usec
,
645 const char *program
) {
646 char result
[UTIL_LINE_SIZE
];
650 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
655 while (line
!= NULL
) {
658 pos
= strchr(line
, '\n');
663 import_property_from_string(event
->dev
, line
);
669 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
670 struct udev_device
*dev_parent
;
671 struct udev_list_entry
*list_entry
;
676 dev_parent
= udev_device_get_parent(dev
);
677 if (dev_parent
== NULL
)
680 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
681 const char *key
= udev_list_entry_get_name(list_entry
);
682 const char *val
= udev_list_entry_get_value(list_entry
);
684 if (fnmatch(filter
, key
, 0) == 0)
685 udev_device_add_property(dev
, key
, val
);
690 static void attr_subst_subdir(char *attr
, size_t len
) {
691 const char *pos
, *tail
, *path
;
692 _cleanup_closedir_
DIR *dir
= NULL
;
695 pos
= strstr(attr
, "/*/");
700 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
705 FOREACH_DIRENT_ALL(dent
, dir
, break)
706 if (dent
->d_name
[0] != '.') {
707 char n
[strlen(dent
->d_name
) + strlen(tail
) + 1];
709 strscpyl(n
, sizeof n
, dent
->d_name
, tail
, NULL
);
710 if (faccessat(dirfd(dir
), n
, F_OK
, 0) == 0) {
711 strscpyl(attr
, len
, path
, n
, NULL
);
717 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
722 if (linepos
== NULL
|| linepos
[0] == '\0')
725 /* skip whitespace */
726 while (isspace(linepos
[0]) || linepos
[0] == ',')
730 if (linepos
[0] == '\0')
736 if (linepos
[0] == '\0')
738 if (isspace(linepos
[0]))
740 if (linepos
[0] == '=')
742 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
743 if (linepos
[1] == '=')
747 /* remember end of key */
750 /* skip whitespace after key */
751 while (isspace(linepos
[0]))
753 if (linepos
[0] == '\0')
756 /* get operation type */
757 if (linepos
[0] == '=' && linepos
[1] == '=') {
760 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
763 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
766 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
769 } else if (linepos
[0] == '=') {
772 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
773 *op
= OP_ASSIGN_FINAL
;
781 /* skip whitespace after operator */
782 while (isspace(linepos
[0]))
784 if (linepos
[0] == '\0')
788 if (linepos
[0] == '"')
795 temp
= strchr(linepos
, '"');
801 /* move line to next key */
806 /* extract possible KEY{attr} */
807 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
811 attr
= strchr(str
, '{');
814 pos
= strchr(attr
, '}');
816 log_error("Missing closing brace for format");
825 static void rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
826 enum operation_type op
,
827 const char *value
, const void *data
) {
828 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
829 const char *attr
= NULL
;
831 assert(rule_tmp
->token_cur
< ELEMENTSOF(rule_tmp
->token
));
832 memzero(token
, sizeof(struct token
));
844 case TK_M_SUBSYSTEMS
:
848 case TK_M_IMPORT_FILE
:
849 case TK_M_IMPORT_PROG
:
851 case TK_M_IMPORT_CMDLINE
:
852 case TK_M_IMPORT_PARENT
:
862 case TK_A_STATIC_NODE
:
863 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
865 case TK_M_IMPORT_BUILTIN
:
866 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
867 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
878 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
879 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
882 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
884 token
->key
.mode
= *(mode_t
*)data
;
886 case TK_A_STRING_ESCAPE_NONE
:
887 case TK_A_STRING_ESCAPE_REPLACE
:
888 case TK_A_DB_PERSIST
:
890 case TK_A_RUN_BUILTIN
:
891 case TK_A_RUN_PROGRAM
:
892 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
893 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
895 case TK_A_INOTIFY_WATCH
:
896 case TK_A_DEVLINK_PRIO
:
897 token
->key
.devlink_prio
= *(int *)data
;
900 token
->key
.uid
= *(uid_t
*)data
;
903 token
->key
.gid
= *(gid_t
*)data
;
906 token
->key
.mode
= *(mode_t
*)data
;
909 case TK_M_PARENTS_MIN
:
910 case TK_M_PARENTS_MAX
:
914 assert_not_reached("wrong type");
917 if (value
!= NULL
&& type
< TK_M_MAX
) {
918 /* check if we need to split or call fnmatch() while matching rules */
919 enum string_glob_type glob
;
923 has_split
= (strchr(value
, '|') != NULL
);
924 has_glob
= string_is_glob(value
);
925 if (has_split
&& has_glob
) {
926 glob
= GL_SPLIT_GLOB
;
927 } else if (has_split
) {
929 } else if (has_glob
) {
930 if (streq(value
, "?*"))
937 token
->key
.glob
= glob
;
940 if (value
!= NULL
&& type
> TK_M_MAX
) {
941 /* check if assigned value has substitution chars */
943 token
->key
.subst
= SB_SUBSYS
;
944 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
945 token
->key
.subst
= SB_FORMAT
;
947 token
->key
.subst
= SB_NONE
;
951 /* check if property/attribute name has substitution chars */
953 token
->key
.attrsubst
= SB_SUBSYS
;
954 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
955 token
->key
.attrsubst
= SB_FORMAT
;
957 token
->key
.attrsubst
= SB_NONE
;
960 token
->key
.type
= type
;
962 rule_tmp
->token_cur
++;
965 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
967 unsigned int start
= 0;
968 unsigned int end
= rule_tmp
->token_cur
;
970 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
971 enum token_type next_val
= TK_UNSET
;
972 unsigned int next_idx
= 0;
975 /* find smallest value */
976 for (j
= start
; j
< end
; j
++) {
977 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
979 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
980 next_val
= rule_tmp
->token
[j
].type
;
985 /* add token and mark done */
986 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
988 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
991 if (next_idx
== start
)
993 if (next_idx
+1 == end
)
999 #define LOG_RULE_ERROR(fmt, ...) log_error("Invalid rule %s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1000 #define LOG_RULE_WARNING(fmt, ...) log_warning("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1001 #define LOG_RULE_DEBUG(fmt, ...) log_debug("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1002 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1004 static void add_rule(struct udev_rules
*rules
, char *line
,
1005 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1008 struct rule_tmp rule_tmp
= {
1010 .rule
.type
= TK_RULE
,
1013 /* the offset in the rule is limited to unsigned short */
1014 if (filename_off
< USHRT_MAX
)
1015 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1016 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1022 enum operation_type op
;
1024 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1025 /* Avoid erroring on trailing whitespace. This is probably rare
1026 * so save the work for the error case instead of always trying
1027 * to strip the trailing whitespace with strstrip(). */
1028 while (isblank(*linepos
))
1031 /* If we aren't at the end of the line, this is a parsing error.
1032 * Make a best effort to describe where the problem is. */
1033 if (!strchr(NEWLINE
, *linepos
)) {
1034 char buf
[2] = {*linepos
};
1035 _cleanup_free_
char *tmp
;
1038 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1039 filename
, lineno
, linepos
- line
+ 1, tmp
);
1040 if (*linepos
== '#')
1041 log_error("hint: comments can only start at beginning of line");
1046 if (rule_tmp
.token_cur
>= ELEMENTSOF(rule_tmp
.token
))
1047 LOG_AND_RETURN("temporary rule array too small, aborting event processing with %u items", rule_tmp
.token_cur
);
1049 if (streq(key
, "ACTION")) {
1050 if (op
> OP_MATCH_MAX
)
1051 LOG_AND_RETURN("invalid %s operation", key
);
1053 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1055 } else if (streq(key
, "DEVPATH")) {
1056 if (op
> OP_MATCH_MAX
)
1057 LOG_AND_RETURN("invalid %s operation", key
);
1059 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1061 } else if (streq(key
, "KERNEL")) {
1062 if (op
> OP_MATCH_MAX
)
1063 LOG_AND_RETURN("invalid %s operation", key
);
1065 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1067 } else if (streq(key
, "SUBSYSTEM")) {
1068 if (op
> OP_MATCH_MAX
)
1069 LOG_AND_RETURN("invalid %s operation", key
);
1071 /* bus, class, subsystem events should all be the same */
1072 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1073 if (!streq(value
, "subsystem"))
1074 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1076 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1078 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1080 } else if (streq(key
, "DRIVER")) {
1081 if (op
> OP_MATCH_MAX
)
1082 LOG_AND_RETURN("invalid %s operation", key
);
1084 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1086 } else if (startswith(key
, "ATTR{")) {
1087 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1089 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1091 if (op
== OP_REMOVE
)
1092 LOG_AND_RETURN("invalid %s operation", "ATTR");
1094 if (op
< OP_MATCH_MAX
)
1095 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1097 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1099 } else if (startswith(key
, "SYSCTL{")) {
1100 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SYSCTL"));
1102 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1104 if (op
== OP_REMOVE
)
1105 LOG_AND_RETURN("invalid %s operation", "ATTR");
1107 if (op
< OP_MATCH_MAX
)
1108 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1110 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1112 } else if (startswith(key
, "SECLABEL{")) {
1113 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1115 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1117 if (op
== OP_REMOVE
)
1118 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1120 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1122 } else if (streq(key
, "KERNELS")) {
1123 if (op
> OP_MATCH_MAX
)
1124 LOG_AND_RETURN("invalid %s operation", key
);
1126 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1128 } else if (streq(key
, "SUBSYSTEMS")) {
1129 if (op
> OP_MATCH_MAX
)
1130 LOG_AND_RETURN("invalid %s operation", key
);
1132 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1134 } else if (streq(key
, "DRIVERS")) {
1135 if (op
> OP_MATCH_MAX
)
1136 LOG_AND_RETURN("invalid %s operation", key
);
1138 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1140 } else if (startswith(key
, "ATTRS{")) {
1141 if (op
> OP_MATCH_MAX
)
1142 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1144 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1146 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1148 if (startswith(attr
, "device/"))
1149 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1150 if (strstr(attr
, "../") != NULL
)
1151 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1152 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1154 } else if (streq(key
, "TAGS")) {
1155 if (op
> OP_MATCH_MAX
)
1156 LOG_AND_RETURN("invalid %s operation", key
);
1158 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1160 } else if (startswith(key
, "ENV{")) {
1161 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1163 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1165 if (op
== OP_REMOVE
)
1166 LOG_AND_RETURN("invalid %s operation", "ENV");
1168 if (op
< OP_MATCH_MAX
)
1169 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1171 if (STR_IN_SET(attr
,
1183 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr
);
1185 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1188 } else if (streq(key
, "TAG")) {
1189 if (op
< OP_MATCH_MAX
)
1190 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1192 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1194 } else if (streq(key
, "PROGRAM")) {
1195 if (op
== OP_REMOVE
)
1196 LOG_AND_RETURN("invalid %s operation", key
);
1198 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1200 } else if (streq(key
, "RESULT")) {
1201 if (op
> OP_MATCH_MAX
)
1202 LOG_AND_RETURN("invalid %s operation", key
);
1204 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1206 } else if (startswith(key
, "IMPORT")) {
1207 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1209 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1212 if (op
== OP_REMOVE
)
1213 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1215 if (streq(attr
, "program")) {
1216 /* find known built-in command */
1217 if (value
[0] != '/') {
1218 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1220 if (cmd
< UDEV_BUILTIN_MAX
) {
1221 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1222 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1226 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1227 } else if (streq(attr
, "builtin")) {
1228 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1230 if (cmd
>= UDEV_BUILTIN_MAX
)
1231 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1233 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1234 } else if (streq(attr
, "file"))
1235 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1236 else if (streq(attr
, "db"))
1237 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1238 else if (streq(attr
, "cmdline"))
1239 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1240 else if (streq(attr
, "parent"))
1241 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1243 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1245 } else if (startswith(key
, "TEST")) {
1248 if (op
> OP_MATCH_MAX
)
1249 LOG_AND_RETURN("invalid %s operation", "TEST");
1251 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1253 mode
= strtol(attr
, NULL
, 8);
1254 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1256 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1258 } else if (startswith(key
, "RUN")) {
1259 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1262 if (op
== OP_REMOVE
)
1263 LOG_AND_RETURN("invalid %s operation", "RUN");
1265 if (streq(attr
, "builtin")) {
1266 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1268 if (cmd
< UDEV_BUILTIN_MAX
)
1269 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1271 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1272 } else if (streq(attr
, "program")) {
1273 const enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1275 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1277 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr
);
1279 } else if (streq(key
, "LABEL")) {
1280 if (op
== OP_REMOVE
)
1281 LOG_AND_RETURN("invalid %s operation", key
);
1283 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1285 } else if (streq(key
, "GOTO")) {
1286 if (op
== OP_REMOVE
)
1287 LOG_AND_RETURN("invalid %s operation", key
);
1289 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1291 } else if (startswith(key
, "NAME")) {
1292 if (op
== OP_REMOVE
)
1293 LOG_AND_RETURN("invalid %s operation", key
);
1295 if (op
< OP_MATCH_MAX
)
1296 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1298 if (streq(value
, "%k")) {
1299 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1302 if (isempty(value
)) {
1303 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1306 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1308 rule_tmp
.rule
.rule
.can_set_name
= true;
1310 } else if (streq(key
, "SYMLINK")) {
1311 if (op
== OP_REMOVE
)
1312 LOG_AND_RETURN("invalid %s operation", key
);
1314 if (op
< OP_MATCH_MAX
)
1315 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1317 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1318 rule_tmp
.rule
.rule
.can_set_name
= true;
1320 } else if (streq(key
, "OWNER")) {
1324 if (op
== OP_REMOVE
)
1325 LOG_AND_RETURN("invalid %s operation", key
);
1327 uid
= strtoul(value
, &endptr
, 10);
1328 if (endptr
[0] == '\0')
1329 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1330 else if (rules
->resolve_names
> 0 && strchr("$%", value
[0]) == NULL
) {
1331 uid
= add_uid(rules
, value
);
1332 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1333 } else if (rules
->resolve_names
>= 0)
1334 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1336 rule_tmp
.rule
.rule
.can_set_name
= true;
1338 } else if (streq(key
, "GROUP")) {
1342 if (op
== OP_REMOVE
)
1343 LOG_AND_RETURN("invalid %s operation", key
);
1345 gid
= strtoul(value
, &endptr
, 10);
1346 if (endptr
[0] == '\0')
1347 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1348 else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1349 gid
= add_gid(rules
, value
);
1350 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1351 } else if (rules
->resolve_names
>= 0)
1352 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1354 rule_tmp
.rule
.rule
.can_set_name
= true;
1356 } else if (streq(key
, "MODE")) {
1360 if (op
== OP_REMOVE
)
1361 LOG_AND_RETURN("invalid %s operation", key
);
1363 mode
= strtol(value
, &endptr
, 8);
1364 if (endptr
[0] == '\0')
1365 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1367 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1368 rule_tmp
.rule
.rule
.can_set_name
= true;
1370 } else if (streq(key
, "OPTIONS")) {
1373 if (op
== OP_REMOVE
)
1374 LOG_AND_RETURN("invalid %s operation", key
);
1376 pos
= strstr(value
, "link_priority=");
1378 int prio
= atoi(pos
+ strlen("link_priority="));
1380 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1383 pos
= strstr(value
, "string_escape=");
1385 pos
+= strlen("string_escape=");
1386 if (startswith(pos
, "none"))
1387 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1388 else if (startswith(pos
, "replace"))
1389 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1392 pos
= strstr(value
, "db_persist");
1394 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1396 pos
= strstr(value
, "nowatch");
1400 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1402 pos
= strstr(value
, "watch");
1406 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1410 pos
= strstr(value
, "static_node=");
1412 pos
+= strlen("static_node=");
1413 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1414 rule_tmp
.rule
.rule
.has_static_node
= true;
1418 LOG_AND_RETURN("unknown key '%s'", key
);
1421 /* add rule token and sort tokens */
1422 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1423 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1424 LOG_RULE_ERROR("failed to add rule token");
1427 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1428 _cleanup_fclose_
FILE *f
= NULL
;
1429 unsigned int first_token
;
1430 unsigned int filename_off
;
1431 char line
[UTIL_LINE_SIZE
];
1435 f
= fopen(filename
, "re");
1437 if (errno
== ENOENT
)
1443 if (null_or_empty_fd(fileno(f
))) {
1444 log_debug("Skipping empty file: %s", filename
);
1447 log_debug("Reading rules file: %s", filename
);
1449 first_token
= rules
->token_cur
;
1450 filename_off
= rules_add_string(rules
, filename
);
1452 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1456 /* skip whitespace */
1459 while (isspace(key
[0]))
1470 /* continue reading if backslash+newline is found */
1471 while (line
[len
-2] == '\\') {
1472 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1474 if (strlen(&line
[len
-2]) < 2)
1480 if (len
+1 >= sizeof(line
)) {
1481 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1484 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1487 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1488 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1489 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1490 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1493 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1494 if (rules
->tokens
[j
].type
!= TK_RULE
)
1496 if (rules
->tokens
[j
].rule
.label_off
== 0)
1498 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1500 rules
->tokens
[i
].key
.rule_goto
= j
;
1503 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1504 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1510 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1511 struct udev_rules
*rules
;
1512 struct udev_list file_list
;
1513 struct token end_token
;
1517 rules
= new0(struct udev_rules
, 1);
1521 rules
->resolve_names
= resolve_names
;
1522 udev_list_init(udev
, &file_list
, true);
1524 /* init token array and string buffer */
1525 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1526 if (rules
->tokens
== NULL
)
1527 return udev_rules_unref(rules
);
1528 rules
->token_max
= PREALLOC_TOKEN
;
1530 rules
->strbuf
= strbuf_new();
1532 return udev_rules_unref(rules
);
1534 udev_rules_check_timestamp(rules
);
1536 r
= conf_files_list_strv(&files
, ".rules", NULL
, 0, rules_dirs
);
1538 log_error_errno(r
, "failed to enumerate rules files: %m");
1539 return udev_rules_unref(rules
);
1543 * The offset value in the rules strct is limited; add all
1544 * rules file names to the beginning of the string buffer.
1546 STRV_FOREACH(f
, files
)
1547 rules_add_string(rules
, *f
);
1549 STRV_FOREACH(f
, files
)
1550 parse_file(rules
, *f
);
1554 memzero(&end_token
, sizeof(struct token
));
1555 end_token
.type
= TK_END
;
1556 add_token(rules
, &end_token
);
1557 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1558 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1560 /* cleanup temporary strbuf data */
1561 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1562 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1563 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1564 strbuf_complete(rules
->strbuf
);
1566 /* cleanup uid/gid cache */
1567 rules
->uids
= mfree(rules
->uids
);
1568 rules
->uids_cur
= 0;
1569 rules
->uids_max
= 0;
1570 rules
->gids
= mfree(rules
->gids
);
1571 rules
->gids_cur
= 0;
1572 rules
->gids_max
= 0;
1578 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1581 free(rules
->tokens
);
1582 strbuf_cleanup(rules
->strbuf
);
1585 return mfree(rules
);
1588 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1592 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1595 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1596 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1603 switch (token
->key
.glob
) {
1605 match
= (streq(key_value
, val
));
1608 match
= (fnmatch(key_value
, val
, 0) == 0);
1615 s
= rules_str(rules
, token
->key
.value_off
);
1620 next
= strchr(s
, '|');
1622 size_t matchlen
= (size_t)(next
- s
);
1624 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1628 match
= (streq(s
, val
));
1637 char value
[UTIL_PATH_SIZE
];
1639 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1641 while (key_value
!= NULL
) {
1642 pos
= strchr(key_value
, '|');
1647 match
= (fnmatch(key_value
, val
, 0) == 0);
1655 match
= (val
[0] != '\0');
1661 if (match
&& (token
->key
.op
== OP_MATCH
))
1663 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1668 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1670 char nbuf
[UTIL_NAME_SIZE
];
1672 char vbuf
[UTIL_NAME_SIZE
];
1675 name
= rules_str(rules
, cur
->key
.attr_off
);
1676 switch (cur
->key
.attrsubst
) {
1678 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
), false);
1682 value
= udev_device_get_sysattr_value(dev
, name
);
1687 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1695 /* remove trailing whitespace, if not asked to match for it */
1696 len
= strlen(value
);
1697 if (len
> 0 && isspace(value
[len
-1])) {
1698 const char *key_value
;
1701 key_value
= rules_str(rules
, cur
->key
.value_off
);
1702 klen
= strlen(key_value
);
1703 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1704 if (value
!= vbuf
) {
1705 strscpy(vbuf
, sizeof(vbuf
), value
);
1708 while (len
> 0 && isspace(vbuf
[--len
]))
1713 return match_key(rules
, cur
, value
);
1722 void udev_rules_apply_to_event(struct udev_rules
*rules
,
1723 struct udev_event
*event
,
1724 usec_t timeout_usec
,
1725 usec_t timeout_warn_usec
,
1726 struct udev_list
*properties_list
) {
1729 enum escape_type esc
= ESCAPE_UNSET
;
1732 if (rules
->tokens
== NULL
)
1735 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1736 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1737 udev_device_get_ifindex(event
->dev
) > 0));
1739 /* loop through token list, match, run actions or forward to next rule */
1740 cur
= &rules
->tokens
[0];
1743 dump_token(rules
, cur
);
1744 switch (cur
->type
) {
1748 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1749 if (!can_set_name
&& rule
->rule
.can_set_name
)
1754 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1758 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1762 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1765 case TK_M_DEVLINK
: {
1766 struct udev_list_entry
*list_entry
;
1769 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1770 const char *devlink
;
1772 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1773 if (match_key(rules
, cur
, devlink
) == 0) {
1783 if (match_key(rules
, cur
, event
->name
) != 0)
1787 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1790 value
= udev_device_get_property_value(event
->dev
, key_name
);
1792 /* check global properties */
1793 if (!value
&& properties_list
) {
1794 struct udev_list_entry
*list_entry
;
1796 list_entry
= udev_list_get_entry(properties_list
);
1797 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1798 if (list_entry
!= NULL
)
1799 value
= udev_list_entry_get_value(list_entry
);
1804 if (match_key(rules
, cur
, value
))
1809 struct udev_list_entry
*list_entry
;
1812 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1813 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1818 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1819 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1823 case TK_M_SUBSYSTEM
:
1824 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1828 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1832 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1836 char filename
[UTIL_PATH_SIZE
];
1837 _cleanup_free_
char *value
= NULL
;
1840 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
1841 sysctl_normalize(filename
);
1842 if (sysctl_read(filename
, &value
) < 0)
1845 len
= strlen(value
);
1846 while (len
> 0 && isspace(value
[--len
]))
1848 if (match_key(rules
, cur
, value
) != 0)
1853 case TK_M_SUBSYSTEMS
:
1859 /* get whole sequence of parent matches */
1861 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1864 /* loop over parents */
1865 event
->dev_parent
= event
->dev
;
1869 /* loop over sequence of parent match keys */
1870 for (key
= cur
; key
< next
; key
++ ) {
1871 dump_token(rules
, key
);
1874 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1877 case TK_M_SUBSYSTEMS
:
1878 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1882 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1886 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1890 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1892 if (match
&& key
->key
.op
== OP_NOMATCH
)
1894 if (!match
&& key
->key
.op
== OP_MATCH
)
1905 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1906 if (event
->dev_parent
== NULL
)
1909 /* move behind our sequence of parent match keys */
1914 char filename
[UTIL_PATH_SIZE
];
1915 struct stat statbuf
;
1918 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
), false);
1919 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
1920 if (filename
[0] != '/') {
1921 char tmp
[UTIL_PATH_SIZE
];
1923 strscpy(tmp
, sizeof(tmp
), filename
);
1924 strscpyl(filename
, sizeof(filename
),
1925 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
1928 attr_subst_subdir(filename
, sizeof(filename
));
1930 match
= (stat(filename
, &statbuf
) == 0);
1931 if (match
&& cur
->key
.mode
> 0)
1932 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1933 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1935 if (!match
&& cur
->key
.op
== OP_MATCH
)
1939 case TK_M_PROGRAM
: {
1940 char program
[UTIL_PATH_SIZE
];
1941 char result
[UTIL_LINE_SIZE
];
1943 event
->program_result
= mfree(event
->program_result
);
1944 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
), false);
1945 log_debug("PROGRAM '%s' %s:%u",
1947 rules_str(rules
, rule
->rule
.filename_off
),
1948 rule
->rule
.filename_line
);
1950 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
1951 if (cur
->key
.op
!= OP_NOMATCH
)
1956 util_remove_trailing_chars(result
, '\n');
1957 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
1958 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1960 log_debug("%i character(s) replaced" , count
);
1962 event
->program_result
= strdup(result
);
1963 if (cur
->key
.op
== OP_NOMATCH
)
1968 case TK_M_IMPORT_FILE
: {
1969 char import
[UTIL_PATH_SIZE
];
1971 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1972 if (import_file_into_properties(event
->dev
, import
) != 0)
1973 if (cur
->key
.op
!= OP_NOMATCH
)
1977 case TK_M_IMPORT_PROG
: {
1978 char import
[UTIL_PATH_SIZE
];
1980 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
1981 log_debug("IMPORT '%s' %s:%u",
1983 rules_str(rules
, rule
->rule
.filename_off
),
1984 rule
->rule
.filename_line
);
1986 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
1987 if (cur
->key
.op
!= OP_NOMATCH
)
1991 case TK_M_IMPORT_BUILTIN
: {
1992 char command
[UTIL_PATH_SIZE
];
1994 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
1995 /* check if we ran already */
1996 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
1997 log_debug("IMPORT builtin skip '%s' %s:%u",
1998 udev_builtin_name(cur
->key
.builtin_cmd
),
1999 rules_str(rules
, rule
->rule
.filename_off
),
2000 rule
->rule
.filename_line
);
2001 /* return the result from earlier run */
2002 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2003 if (cur
->key
.op
!= OP_NOMATCH
)
2008 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2011 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
), false);
2012 log_debug("IMPORT builtin '%s' %s:%u",
2013 udev_builtin_name(cur
->key
.builtin_cmd
),
2014 rules_str(rules
, rule
->rule
.filename_off
),
2015 rule
->rule
.filename_line
);
2017 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2018 /* remember failure */
2019 log_debug("IMPORT builtin '%s' returned non-zero",
2020 udev_builtin_name(cur
->key
.builtin_cmd
));
2021 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2022 if (cur
->key
.op
!= OP_NOMATCH
)
2027 case TK_M_IMPORT_DB
: {
2028 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2031 value
= udev_device_get_property_value(event
->dev_db
, key
);
2033 udev_device_add_property(event
->dev
, key
, value
);
2035 if (cur
->key
.op
!= OP_NOMATCH
)
2040 case TK_M_IMPORT_CMDLINE
: {
2041 _cleanup_fclose_
FILE *f
= NULL
;
2042 bool imported
= false;
2044 f
= fopen("/proc/cmdline", "re");
2048 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2049 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2052 pos
= strstr(cmdline
, key
);
2056 if (pos
[0] == '\0' || isspace(pos
[0]))
2057 /* we import simple flags as 'FLAG=1' */
2058 udev_device_add_property(event
->dev
, key
, "1");
2059 else if (pos
[0] == '=') {
2064 while (pos
[0] != '\0' && !isspace(pos
[0]))
2067 udev_device_add_property(event
->dev
, key
, value
);
2072 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2076 case TK_M_IMPORT_PARENT
: {
2077 char import
[UTIL_PATH_SIZE
];
2079 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
), false);
2080 if (import_parent_into_properties(event
->dev
, import
) != 0)
2081 if (cur
->key
.op
!= OP_NOMATCH
)
2086 if (match_key(rules
, cur
, event
->program_result
) != 0)
2089 case TK_A_STRING_ESCAPE_NONE
:
2092 case TK_A_STRING_ESCAPE_REPLACE
:
2093 esc
= ESCAPE_REPLACE
;
2095 case TK_A_DB_PERSIST
:
2096 udev_device_set_db_persist(event
->dev
);
2098 case TK_A_INOTIFY_WATCH
:
2099 if (event
->inotify_watch_final
)
2101 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2102 event
->inotify_watch_final
= true;
2103 event
->inotify_watch
= cur
->key
.watch
;
2105 case TK_A_DEVLINK_PRIO
:
2106 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2109 char owner
[UTIL_NAME_SIZE
];
2110 const char *ow
= owner
;
2113 if (event
->owner_final
)
2115 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2116 event
->owner_final
= true;
2117 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
), false);
2118 event
->owner_set
= true;
2119 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2121 log_unknown_owner(r
, "user", owner
);
2124 log_debug("OWNER %u %s:%u",
2126 rules_str(rules
, rule
->rule
.filename_off
),
2127 rule
->rule
.filename_line
);
2131 char group
[UTIL_NAME_SIZE
];
2132 const char *gr
= group
;
2135 if (event
->group_final
)
2137 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2138 event
->group_final
= true;
2139 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
), false);
2140 event
->group_set
= true;
2141 r
= get_group_creds(&gr
, &event
->gid
);
2143 log_unknown_owner(r
, "group", group
);
2146 log_debug("GROUP %u %s:%u",
2148 rules_str(rules
, rule
->rule
.filename_off
),
2149 rule
->rule
.filename_line
);
2153 char mode_str
[UTIL_NAME_SIZE
];
2157 if (event
->mode_final
)
2159 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
), false);
2160 mode
= strtol(mode_str
, &endptr
, 8);
2161 if (endptr
[0] != '\0') {
2162 log_error("ignoring invalid mode '%s'", mode_str
);
2165 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2166 event
->mode_final
= true;
2167 event
->mode_set
= true;
2169 log_debug("MODE %#o %s:%u",
2171 rules_str(rules
, rule
->rule
.filename_off
),
2172 rule
->rule
.filename_line
);
2176 if (event
->owner_final
)
2178 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2179 event
->owner_final
= true;
2180 event
->owner_set
= true;
2181 event
->uid
= cur
->key
.uid
;
2182 log_debug("OWNER %u %s:%u",
2184 rules_str(rules
, rule
->rule
.filename_off
),
2185 rule
->rule
.filename_line
);
2188 if (event
->group_final
)
2190 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2191 event
->group_final
= true;
2192 event
->group_set
= true;
2193 event
->gid
= cur
->key
.gid
;
2194 log_debug("GROUP %u %s:%u",
2196 rules_str(rules
, rule
->rule
.filename_off
),
2197 rule
->rule
.filename_line
);
2200 if (event
->mode_final
)
2202 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2203 event
->mode_final
= true;
2204 event
->mode_set
= true;
2205 event
->mode
= cur
->key
.mode
;
2206 log_debug("MODE %#o %s:%u",
2208 rules_str(rules
, rule
->rule
.filename_off
),
2209 rule
->rule
.filename_line
);
2211 case TK_A_SECLABEL
: {
2212 char label_str
[UTIL_LINE_SIZE
] = {};
2213 const char *name
, *label
;
2215 name
= rules_str(rules
, cur
->key
.attr_off
);
2216 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), label_str
, sizeof(label_str
), false);
2217 if (label_str
[0] != '\0')
2220 label
= rules_str(rules
, cur
->key
.value_off
);
2222 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2223 udev_list_cleanup(&event
->seclabel_list
);
2224 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2225 log_debug("SECLABEL{%s}='%s' %s:%u",
2227 rules_str(rules
, rule
->rule
.filename_off
),
2228 rule
->rule
.filename_line
);
2232 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2233 char *value
= rules_str(rules
, cur
->key
.value_off
);
2234 char value_new
[UTIL_NAME_SIZE
];
2235 const char *value_old
= NULL
;
2237 if (value
[0] == '\0') {
2238 if (cur
->key
.op
== OP_ADD
)
2240 udev_device_add_property(event
->dev
, name
, NULL
);
2244 if (cur
->key
.op
== OP_ADD
)
2245 value_old
= udev_device_get_property_value(event
->dev
, name
);
2247 char temp
[UTIL_NAME_SIZE
];
2249 /* append value separated by space */
2250 udev_event_apply_format(event
, value
, temp
, sizeof(temp
), false);
2251 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2253 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
), false);
2255 udev_device_add_property(event
->dev
, name
, value_new
);
2259 char tag
[UTIL_PATH_SIZE
];
2262 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
), false);
2263 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2264 udev_device_cleanup_tags_list(event
->dev
);
2265 for (p
= tag
; *p
!= '\0'; p
++) {
2266 if ((*p
>= 'a' && *p
<= 'z') ||
2267 (*p
>= 'A' && *p
<= 'Z') ||
2268 (*p
>= '0' && *p
<= '9') ||
2269 *p
== '-' || *p
== '_')
2271 log_error("ignoring invalid tag name '%s'", tag
);
2274 if (cur
->key
.op
== OP_REMOVE
)
2275 udev_device_remove_tag(event
->dev
, tag
);
2277 udev_device_add_tag(event
->dev
, tag
);
2281 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2283 char name_str
[UTIL_PATH_SIZE
];
2286 if (event
->name_final
)
2288 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2289 event
->name_final
= true;
2290 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
), false);
2291 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2292 count
= util_replace_chars(name_str
, "/");
2294 log_debug("%i character(s) replaced", count
);
2296 if (major(udev_device_get_devnum(event
->dev
)) &&
2297 !streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/"))) {
2298 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2300 rules_str(rules
, rule
->rule
.filename_off
),
2301 rule
->rule
.filename_line
);
2304 if (free_and_strdup(&event
->name
, name_str
) < 0) {
2308 log_debug("NAME '%s' %s:%u",
2310 rules_str(rules
, rule
->rule
.filename_off
),
2311 rule
->rule
.filename_line
);
2314 case TK_A_DEVLINK
: {
2315 char temp
[UTIL_PATH_SIZE
];
2316 char filename
[UTIL_PATH_SIZE
];
2320 if (event
->devlink_final
)
2322 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2324 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2325 event
->devlink_final
= true;
2326 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2327 udev_device_cleanup_devlinks_list(event
->dev
);
2329 /* allow multiple symlinks separated by spaces */
2330 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
), esc
!= ESCAPE_NONE
);
2331 if (esc
== ESCAPE_UNSET
)
2332 count
= util_replace_chars(temp
, "/ ");
2333 else if (esc
== ESCAPE_REPLACE
)
2334 count
= util_replace_chars(temp
, "/");
2336 log_debug("%i character(s) replaced" , count
);
2338 while (isspace(pos
[0]))
2340 next
= strchr(pos
, ' ');
2341 while (next
!= NULL
) {
2343 log_debug("LINK '%s' %s:%u", pos
,
2344 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2345 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2346 udev_device_add_devlink(event
->dev
, filename
);
2347 while (isspace(next
[1]))
2350 next
= strchr(pos
, ' ');
2352 if (pos
[0] != '\0') {
2353 log_debug("LINK '%s' %s:%u", pos
,
2354 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2355 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2356 udev_device_add_devlink(event
->dev
, filename
);
2361 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2362 char attr
[UTIL_PATH_SIZE
];
2363 char value
[UTIL_NAME_SIZE
];
2364 _cleanup_fclose_
FILE *f
= NULL
;
2366 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2367 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2368 attr_subst_subdir(attr
, sizeof(attr
));
2370 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2371 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2372 rules_str(rules
, rule
->rule
.filename_off
),
2373 rule
->rule
.filename_line
);
2374 f
= fopen(attr
, "we");
2376 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2377 else if (fprintf(f
, "%s", value
) <= 0)
2378 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2382 char filename
[UTIL_PATH_SIZE
];
2383 char value
[UTIL_NAME_SIZE
];
2386 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
), false);
2387 sysctl_normalize(filename
);
2388 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
), false);
2389 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2390 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2391 r
= sysctl_write(filename
, value
);
2393 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2396 case TK_A_RUN_BUILTIN
:
2397 case TK_A_RUN_PROGRAM
: {
2398 struct udev_list_entry
*entry
;
2400 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2401 udev_list_cleanup(&event
->run_list
);
2402 log_debug("RUN '%s' %s:%u",
2403 rules_str(rules
, cur
->key
.value_off
),
2404 rules_str(rules
, rule
->rule
.filename_off
),
2405 rule
->rule
.filename_line
);
2406 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2407 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2411 if (cur
->key
.rule_goto
== 0)
2413 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2418 case TK_M_PARENTS_MIN
:
2419 case TK_M_PARENTS_MAX
:
2422 log_error("wrong type %u", cur
->type
);
2429 /* fast-forward to next rule */
2430 cur
= rule
+ rule
->rule
.token_count
;
2434 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2440 _cleanup_strv_free_
char **tags
= NULL
;
2443 _cleanup_free_
char *path
= NULL
;
2446 if (rules
->tokens
== NULL
)
2449 cur
= &rules
->tokens
[0];
2452 switch (cur
->type
) {
2457 /* skip rules without a static_node tag */
2458 if (!rule
->rule
.has_static_node
)
2464 tags
= strv_free(tags
);
2473 mode
= cur
->key
.mode
;
2476 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2481 case TK_A_STATIC_NODE
: {
2482 char device_node
[UTIL_PATH_SIZE
];
2483 char tags_dir
[UTIL_PATH_SIZE
];
2484 char tag_symlink
[UTIL_PATH_SIZE
];
2487 /* we assure, that the permissions tokens are sorted before the static token */
2489 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2492 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2493 if (stat(device_node
, &stats
) != 0)
2495 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2498 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2500 STRV_FOREACH(t
, tags
) {
2501 _cleanup_free_
char *unescaped_filename
= NULL
;
2503 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2504 r
= mkdir_p(tags_dir
, 0755);
2506 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2508 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2510 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2511 r
= symlink(device_node
, tag_symlink
);
2512 if (r
< 0 && errno
!= EEXIST
)
2513 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2514 tag_symlink
, device_node
);
2518 /* don't touch the permissions if only the tags were set */
2519 if (mode
== 0 && uid
== 0 && gid
== 0)
2528 if (mode
!= (stats
.st_mode
& 01777)) {
2529 r
= chmod(device_node
, mode
);
2531 return log_error_errno(errno
, "Failed to chmod '%s' %#o: %m",
2534 log_debug("chmod '%s' %#o", device_node
, mode
);
2537 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2538 r
= chown(device_node
, uid
, gid
);
2540 return log_error_errno(errno
, "Failed to chown '%s' %u %u: %m",
2541 device_node
, uid
, gid
);
2543 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2546 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2556 /* fast-forward to next rule */
2557 cur
= rule
+ rule
->rule
.token_count
;
2564 fchmod(fileno(f
), 0644);
2565 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2566 unlink_noerrno("/run/udev/static_node-tags");
2567 unlink_noerrno(path
);