2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "path-util.h"
34 #include "conf-files.h"
38 #include "sysctl-util.h"
40 #define PREALLOC_TOKEN 2048
43 unsigned int name_off
;
50 static const char* const rules_dirs
[] = {
53 UDEVLIBEXECDIR
"/rules.d",
61 /* every key in the rules file becomes a token */
63 unsigned int token_cur
;
64 unsigned int token_max
;
66 /* all key strings are copied and de-duplicated in a single continuous string buffer */
67 struct strbuf
*strbuf
;
69 /* during rule parsing, uid/gid lookup results are cached */
71 unsigned int uids_cur
;
72 unsigned int uids_max
;
74 unsigned int gids_cur
;
75 unsigned int gids_max
;
78 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
79 return rules
->strbuf
->buf
+ off
;
82 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
83 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
86 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
100 enum string_glob_type
{
102 GL_PLAIN
, /* no special chars */
103 GL_GLOB
, /* shell globs ?,*,[] */
104 GL_SPLIT
, /* multi-value A|B */
105 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
106 GL_SOMETHING
, /* commonly used "?*" */
109 enum string_subst_type
{
116 /* tokens of a rule are sorted/handled in this order */
121 TK_M_ACTION
, /* val */
122 TK_M_DEVPATH
, /* val */
123 TK_M_KERNEL
, /* val */
124 TK_M_DEVLINK
, /* val */
126 TK_M_ENV
, /* val, attr */
128 TK_M_SUBSYSTEM
, /* val */
129 TK_M_DRIVER
, /* val */
130 TK_M_WAITFOR
, /* val */
131 TK_M_ATTR
, /* val, attr */
132 TK_M_SYSCTL
, /* val, attr */
135 TK_M_KERNELS
, /* val */
136 TK_M_SUBSYSTEMS
, /* val */
137 TK_M_DRIVERS
, /* val */
138 TK_M_ATTRS
, /* val, attr */
142 TK_M_TEST
, /* val, mode_t */
143 TK_M_PROGRAM
, /* val */
144 TK_M_IMPORT_FILE
, /* val */
145 TK_M_IMPORT_PROG
, /* val */
146 TK_M_IMPORT_BUILTIN
, /* val */
147 TK_M_IMPORT_DB
, /* val */
148 TK_M_IMPORT_CMDLINE
, /* val */
149 TK_M_IMPORT_PARENT
, /* val */
150 TK_M_RESULT
, /* val */
153 TK_A_STRING_ESCAPE_NONE
,
154 TK_A_STRING_ESCAPE_REPLACE
,
156 TK_A_INOTIFY_WATCH
, /* int */
157 TK_A_DEVLINK_PRIO
, /* int */
158 TK_A_OWNER
, /* val */
159 TK_A_GROUP
, /* val */
161 TK_A_OWNER_ID
, /* uid_t */
162 TK_A_GROUP_ID
, /* gid_t */
163 TK_A_MODE_ID
, /* mode_t */
165 TK_A_STATIC_NODE
, /* val */
166 TK_A_SECLABEL
, /* val, attr */
167 TK_A_ENV
, /* val, attr */
169 TK_A_DEVLINK
, /* val */
170 TK_A_ATTR
, /* val, attr */
171 TK_A_SYSCTL
, /* val, attr */
172 TK_A_RUN_BUILTIN
, /* val, bool */
173 TK_A_RUN_PROGRAM
, /* val, bool */
174 TK_A_GOTO
, /* size_t */
179 /* we try to pack stuff in a way that we take only 12 bytes per token */
182 unsigned char type
; /* same in rule and key */
184 enum token_type type
:8;
186 bool has_static_node
:1;
187 unsigned int unused
:6;
188 unsigned short token_count
;
189 unsigned int label_off
;
190 unsigned short filename_off
;
191 unsigned short filename_line
;
194 enum token_type type
:8;
195 enum operation_type op
:8;
196 enum string_glob_type glob
:8;
197 enum string_subst_type subst
:4;
198 enum string_subst_type attrsubst
:4;
199 unsigned int value_off
;
201 unsigned int attr_off
;
202 unsigned int rule_goto
;
208 enum udev_builtin_cmd builtin_cmd
;
216 struct udev_rules
*rules
;
218 struct token token
[MAX_TK
];
219 unsigned int token_cur
;
223 static const char *operation_str(enum operation_type type
) {
224 static const char *operation_strs
[] = {
225 [OP_UNSET
] = "UNSET",
226 [OP_MATCH
] = "match",
227 [OP_NOMATCH
] = "nomatch",
228 [OP_MATCH_MAX
] = "MATCH_MAX",
231 [OP_REMOVE
] = "remove",
232 [OP_ASSIGN
] = "assign",
233 [OP_ASSIGN_FINAL
] = "assign-final",
236 return operation_strs
[type
];
239 static const char *string_glob_str(enum string_glob_type type
) {
240 static const char *string_glob_strs
[] = {
241 [GL_UNSET
] = "UNSET",
242 [GL_PLAIN
] = "plain",
244 [GL_SPLIT
] = "split",
245 [GL_SPLIT_GLOB
] = "split-glob",
246 [GL_SOMETHING
] = "split-glob",
249 return string_glob_strs
[type
];
252 static const char *token_str(enum token_type type
) {
253 static const char *token_strs
[] = {
254 [TK_UNSET
] = "UNSET",
257 [TK_M_ACTION
] = "M ACTION",
258 [TK_M_DEVPATH
] = "M DEVPATH",
259 [TK_M_KERNEL
] = "M KERNEL",
260 [TK_M_DEVLINK
] = "M DEVLINK",
261 [TK_M_NAME
] = "M NAME",
262 [TK_M_ENV
] = "M ENV",
263 [TK_M_TAG
] = "M TAG",
264 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
265 [TK_M_DRIVER
] = "M DRIVER",
266 [TK_M_WAITFOR
] = "M WAITFOR",
267 [TK_M_ATTR
] = "M ATTR",
268 [TK_M_SYSCTL
] = "M SYSCTL",
270 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
271 [TK_M_KERNELS
] = "M KERNELS",
272 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
273 [TK_M_DRIVERS
] = "M DRIVERS",
274 [TK_M_ATTRS
] = "M ATTRS",
275 [TK_M_TAGS
] = "M TAGS",
276 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
278 [TK_M_TEST
] = "M TEST",
279 [TK_M_PROGRAM
] = "M PROGRAM",
280 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
281 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
282 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
283 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
284 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
285 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
286 [TK_M_RESULT
] = "M RESULT",
287 [TK_M_MAX
] = "M MAX",
289 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
290 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
291 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
292 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
293 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
294 [TK_A_OWNER
] = "A OWNER",
295 [TK_A_GROUP
] = "A GROUP",
296 [TK_A_MODE
] = "A MODE",
297 [TK_A_OWNER_ID
] = "A OWNER_ID",
298 [TK_A_GROUP_ID
] = "A GROUP_ID",
299 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
300 [TK_A_SECLABEL
] = "A SECLABEL",
301 [TK_A_MODE_ID
] = "A MODE_ID",
302 [TK_A_ENV
] = "A ENV",
303 [TK_A_TAG
] = "A ENV",
304 [TK_A_NAME
] = "A NAME",
305 [TK_A_DEVLINK
] = "A DEVLINK",
306 [TK_A_ATTR
] = "A ATTR",
307 [TK_A_SYSCTL
] = "A SYSCTL",
308 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
309 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
310 [TK_A_GOTO
] = "A GOTO",
315 return token_strs
[type
];
318 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
319 enum token_type type
= token
->type
;
320 enum operation_type op
= token
->key
.op
;
321 enum string_glob_type glob
= token
->key
.glob
;
322 const char *value
= str(rules
, token
->key
.value_off
);
323 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
328 const char *tks_ptr
= (char *)rules
->tokens
;
329 const char *tk_ptr
= (char *)token
;
330 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
332 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
333 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
334 idx
, token
->rule
.token_count
,
335 &rules
->buf
[token
->rule
.label_off
]);
347 case TK_M_SUBSYSTEMS
:
351 case TK_M_IMPORT_FILE
:
352 case TK_M_IMPORT_PROG
:
354 case TK_M_IMPORT_CMDLINE
:
355 case TK_M_IMPORT_PARENT
:
362 case TK_A_RUN_BUILTIN
:
363 case TK_A_RUN_PROGRAM
:
364 log_debug("%s %s '%s'(%s)",
365 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
367 case TK_M_IMPORT_BUILTIN
:
368 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
377 log_debug("%s %s '%s' '%s'(%s)",
378 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
382 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
384 case TK_A_STRING_ESCAPE_NONE
:
385 case TK_A_STRING_ESCAPE_REPLACE
:
386 case TK_A_DB_PERSIST
:
387 log_debug("%s", token_str(type
));
390 log_debug("%s %s '%s'(%s) %#o",
391 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
393 case TK_A_INOTIFY_WATCH
:
394 log_debug("%s %u", token_str(type
), token
->key
.watch
);
396 case TK_A_DEVLINK_PRIO
:
397 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
400 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
403 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
406 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
408 case TK_A_STATIC_NODE
:
409 log_debug("%s '%s'", token_str(type
), value
);
412 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
415 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
418 log_debug("* %s", token_str(type
));
420 case TK_M_PARENTS_MIN
:
421 case TK_M_PARENTS_MAX
:
424 log_debug("unknown type %u", type
);
429 static void dump_rules(struct udev_rules
*rules
) {
432 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
434 rules
->token_cur
* sizeof(struct token
),
437 for (i
= 0; i
< rules
->token_cur
; i
++)
438 dump_token(rules
, &rules
->tokens
[i
]);
441 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
442 static inline void dump_rules(struct udev_rules
*rules
) {}
445 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
446 /* grow buffer if needed */
447 if (rules
->token_cur
+1 >= rules
->token_max
) {
448 struct token
*tokens
;
451 /* double the buffer size */
452 add
= rules
->token_max
;
456 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
459 rules
->tokens
= tokens
;
460 rules
->token_max
+= add
;
462 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
467 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
473 /* lookup, if we know it already */
474 for (i
= 0; i
< rules
->uids_cur
; i
++) {
475 off
= rules
->uids
[i
].name_off
;
476 if (streq(rules_str(rules
, off
), owner
)) {
477 uid
= rules
->uids
[i
].uid
;
481 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
483 if (r
== -ENOENT
|| r
== -ESRCH
)
484 log_error("specified user '%s' unknown", owner
);
486 log_error_errno(r
, "error resolving user '%s': %m", owner
);
489 /* grow buffer if needed */
490 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
491 struct uid_gid
*uids
;
494 /* double the buffer size */
495 add
= rules
->uids_max
;
499 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
503 rules
->uids_max
+= add
;
505 rules
->uids
[rules
->uids_cur
].uid
= uid
;
506 off
= rules_add_string(rules
, owner
);
509 rules
->uids
[rules
->uids_cur
].name_off
= off
;
514 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
520 /* lookup, if we know it already */
521 for (i
= 0; i
< rules
->gids_cur
; i
++) {
522 off
= rules
->gids
[i
].name_off
;
523 if (streq(rules_str(rules
, off
), group
)) {
524 gid
= rules
->gids
[i
].gid
;
528 r
= get_group_creds(&group
, &gid
);
530 if (r
== -ENOENT
|| r
== -ESRCH
)
531 log_error("specified group '%s' unknown", group
);
533 log_error_errno(r
, "error resolving group '%s': %m", group
);
536 /* grow buffer if needed */
537 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
538 struct uid_gid
*gids
;
541 /* double the buffer size */
542 add
= rules
->gids_max
;
546 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
550 rules
->gids_max
+= add
;
552 rules
->gids
[rules
->gids_cur
].gid
= gid
;
553 off
= rules_add_string(rules
, group
);
556 rules
->gids
[rules
->gids_cur
].name_off
= off
;
561 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
568 while (isspace(key
[0]))
571 /* comment or empty line */
572 if (key
[0] == '#' || key
[0] == '\0')
575 /* split key/value */
576 val
= strchr(key
, '=');
583 while (isspace(val
[0]))
590 while (isspace(key
[len
-1]))
594 /* terminate value */
598 while (isspace(val
[len
-1]))
606 if (val
[0] == '"' || val
[0] == '\'') {
607 if (val
[len
-1] != val
[0]) {
608 log_debug("inconsistent quoting: '%s', skip", line
);
615 udev_device_add_property(dev
, key
, val
);
620 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
622 char line
[UTIL_LINE_SIZE
];
624 f
= fopen(filename
, "re");
627 while (fgets(line
, sizeof(line
), f
) != NULL
)
628 import_property_from_string(dev
, line
);
633 static int import_program_into_properties(struct udev_event
*event
,
635 usec_t timeout_warn_usec
,
636 const char *program
) {
637 struct udev_device
*dev
= event
->dev
;
639 char result
[UTIL_LINE_SIZE
];
643 envp
= udev_device_get_properties_envp(dev
);
644 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, program
, envp
, result
, sizeof(result
));
649 while (line
!= NULL
) {
652 pos
= strchr(line
, '\n');
657 import_property_from_string(dev
, line
);
663 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
664 struct udev_device
*dev_parent
;
665 struct udev_list_entry
*list_entry
;
670 dev_parent
= udev_device_get_parent(dev
);
671 if (dev_parent
== NULL
)
674 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
675 const char *key
= udev_list_entry_get_name(list_entry
);
676 const char *val
= udev_list_entry_get_value(list_entry
);
678 if (fnmatch(filter
, key
, 0) == 0) {
679 udev_device_add_property(dev
, key
, val
);
685 #define WAIT_LOOP_PER_SECOND 50
686 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
) {
687 char filepath
[UTIL_PATH_SIZE
];
688 char devicepath
[UTIL_PATH_SIZE
];
690 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
692 /* a relative path is a device attribute */
693 devicepath
[0] = '\0';
694 if (file
[0] != '/') {
695 strscpyl(devicepath
, sizeof(devicepath
), udev_device_get_syspath(dev
), NULL
);
696 strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
701 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
704 if (stat(file
, &stats
) == 0) {
705 log_debug("file '%s' appeared after %i loops", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
708 /* make sure, the device did not disappear in the meantime */
709 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
710 log_debug("device disappeared while waiting for '%s'", file
);
713 log_debug("wait for '%s' for %i mseconds", file
, 1000 / WAIT_LOOP_PER_SECOND
);
714 nanosleep(&duration
, NULL
);
716 log_debug("waiting for '%s' failed", file
);
720 static int attr_subst_subdir(char *attr
, size_t len
) {
723 if (strstr(attr
, "/*/")) {
725 char dirname
[UTIL_PATH_SIZE
];
729 strscpy(dirname
, sizeof(dirname
), attr
);
730 pos
= strstr(dirname
, "/*/");
735 dir
= opendir(dirname
);
739 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
742 if (dent
->d_name
[0] == '.')
744 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
745 if (stat(attr
, &stats
) == 0) {
757 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
762 if (linepos
== NULL
|| linepos
[0] == '\0')
765 /* skip whitespace */
766 while (isspace(linepos
[0]) || linepos
[0] == ',')
770 if (linepos
[0] == '\0')
776 if (linepos
[0] == '\0')
778 if (isspace(linepos
[0]))
780 if (linepos
[0] == '=')
782 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
783 if (linepos
[1] == '=')
787 /* remember end of key */
790 /* skip whitespace after key */
791 while (isspace(linepos
[0]))
793 if (linepos
[0] == '\0')
796 /* get operation type */
797 if (linepos
[0] == '=' && linepos
[1] == '=') {
800 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
803 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
806 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
809 } else if (linepos
[0] == '=') {
812 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
813 *op
= OP_ASSIGN_FINAL
;
821 /* skip whitespace after operator */
822 while (isspace(linepos
[0]))
824 if (linepos
[0] == '\0')
828 if (linepos
[0] == '"')
835 temp
= strchr(linepos
, '"');
841 /* move line to next key */
846 /* extract possible KEY{attr} */
847 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
851 attr
= strchr(str
, '{');
854 pos
= strchr(attr
, '}');
856 log_error("missing closing brace for format");
865 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
866 enum operation_type op
,
867 const char *value
, const void *data
) {
868 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
869 const char *attr
= NULL
;
871 memzero(token
, sizeof(struct token
));
883 case TK_M_SUBSYSTEMS
:
887 case TK_M_IMPORT_FILE
:
888 case TK_M_IMPORT_PROG
:
890 case TK_M_IMPORT_CMDLINE
:
891 case TK_M_IMPORT_PARENT
:
901 case TK_A_STATIC_NODE
:
902 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
904 case TK_M_IMPORT_BUILTIN
:
905 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
906 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
917 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
918 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
921 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
923 token
->key
.mode
= *(mode_t
*)data
;
925 case TK_A_STRING_ESCAPE_NONE
:
926 case TK_A_STRING_ESCAPE_REPLACE
:
927 case TK_A_DB_PERSIST
:
929 case TK_A_RUN_BUILTIN
:
930 case TK_A_RUN_PROGRAM
:
931 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
932 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
934 case TK_A_INOTIFY_WATCH
:
935 case TK_A_DEVLINK_PRIO
:
936 token
->key
.devlink_prio
= *(int *)data
;
939 token
->key
.uid
= *(uid_t
*)data
;
942 token
->key
.gid
= *(gid_t
*)data
;
945 token
->key
.mode
= *(mode_t
*)data
;
948 case TK_M_PARENTS_MIN
:
949 case TK_M_PARENTS_MAX
:
953 log_error("wrong type %u", type
);
957 if (value
!= NULL
&& type
< TK_M_MAX
) {
958 /* check if we need to split or call fnmatch() while matching rules */
959 enum string_glob_type glob
;
963 has_split
= (strchr(value
, '|') != NULL
);
964 has_glob
= string_is_glob(value
);
965 if (has_split
&& has_glob
) {
966 glob
= GL_SPLIT_GLOB
;
967 } else if (has_split
) {
969 } else if (has_glob
) {
970 if (streq(value
, "?*"))
977 token
->key
.glob
= glob
;
980 if (value
!= NULL
&& type
> TK_M_MAX
) {
981 /* check if assigned value has substitution chars */
983 token
->key
.subst
= SB_SUBSYS
;
984 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
985 token
->key
.subst
= SB_FORMAT
;
987 token
->key
.subst
= SB_NONE
;
991 /* check if property/attribute name has substitution chars */
993 token
->key
.attrsubst
= SB_SUBSYS
;
994 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
995 token
->key
.attrsubst
= SB_FORMAT
;
997 token
->key
.attrsubst
= SB_NONE
;
1000 token
->key
.type
= type
;
1002 rule_tmp
->token_cur
++;
1003 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
1004 log_error("temporary rule array too small");
1010 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
1012 unsigned int start
= 0;
1013 unsigned int end
= rule_tmp
->token_cur
;
1015 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1016 enum token_type next_val
= TK_UNSET
;
1017 unsigned int next_idx
= 0;
1020 /* find smallest value */
1021 for (j
= start
; j
< end
; j
++) {
1022 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1024 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1025 next_val
= rule_tmp
->token
[j
].type
;
1030 /* add token and mark done */
1031 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1033 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1036 if (next_idx
== start
)
1038 if (next_idx
+1 == end
)
1044 static int add_rule(struct udev_rules
*rules
, char *line
,
1045 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1048 struct rule_tmp rule_tmp
= {
1050 .rule
.type
= TK_RULE
,
1053 /* the offset in the rule is limited to unsigned short */
1054 if (filename_off
< USHRT_MAX
)
1055 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1056 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1062 enum operation_type op
;
1064 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1065 /* Avoid erroring on trailing whitespace. This is probably rare
1066 * so save the work for the error case instead of always trying
1067 * to strip the trailing whitespace with strstrip(). */
1068 while (isblank(*linepos
))
1071 /* If we aren't at the end of the line, this is a parsing error.
1072 * Make a best effort to describe where the problem is. */
1073 if (!strchr(NEWLINE
, *linepos
)) {
1074 char buf
[2] = {*linepos
};
1075 _cleanup_free_
char *tmp
;
1078 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1079 filename
, lineno
, linepos
- line
+ 1, tmp
);
1080 if (*linepos
== '#')
1081 log_error("hint: comments can only start at beginning of line");
1086 if (streq(key
, "ACTION")) {
1087 if (op
> OP_MATCH_MAX
) {
1088 log_error("invalid ACTION operation");
1091 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1095 if (streq(key
, "DEVPATH")) {
1096 if (op
> OP_MATCH_MAX
) {
1097 log_error("invalid DEVPATH operation");
1100 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1104 if (streq(key
, "KERNEL")) {
1105 if (op
> OP_MATCH_MAX
) {
1106 log_error("invalid KERNEL operation");
1109 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1113 if (streq(key
, "SUBSYSTEM")) {
1114 if (op
> OP_MATCH_MAX
) {
1115 log_error("invalid SUBSYSTEM operation");
1118 /* bus, class, subsystem events should all be the same */
1119 if (streq(value
, "subsystem") ||
1120 streq(value
, "bus") ||
1121 streq(value
, "class")) {
1122 if (streq(value
, "bus") || streq(value
, "class"))
1123 log_error("'%s' must be specified as 'subsystem' "
1124 "please fix it in %s:%u", value
, filename
, lineno
);
1125 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1127 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1131 if (streq(key
, "DRIVER")) {
1132 if (op
> OP_MATCH_MAX
) {
1133 log_error("invalid DRIVER operation");
1136 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1140 if (startswith(key
, "ATTR{")) {
1141 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1143 log_error("error parsing ATTR attribute");
1146 if (op
== OP_REMOVE
) {
1147 log_error("invalid ATTR operation");
1150 if (op
< OP_MATCH_MAX
)
1151 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1153 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1157 if (startswith(key
, "SYSCTL{")) {
1158 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SYSCTL"));
1160 log_error("error parsing SYSCTL attribute");
1163 if (op
== OP_REMOVE
) {
1164 log_error("invalid SYSCTL operation");
1167 if (op
< OP_MATCH_MAX
)
1168 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1170 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1174 if (startswith(key
, "SECLABEL{")) {
1175 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1177 log_error("error parsing SECLABEL attribute");
1180 if (op
== OP_REMOVE
) {
1181 log_error("invalid SECLABEL operation");
1185 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1189 if (streq(key
, "KERNELS")) {
1190 if (op
> OP_MATCH_MAX
) {
1191 log_error("invalid KERNELS operation");
1194 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1198 if (streq(key
, "SUBSYSTEMS")) {
1199 if (op
> OP_MATCH_MAX
) {
1200 log_error("invalid SUBSYSTEMS operation");
1203 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1207 if (streq(key
, "DRIVERS")) {
1208 if (op
> OP_MATCH_MAX
) {
1209 log_error("invalid DRIVERS operation");
1212 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1216 if (startswith(key
, "ATTRS{")) {
1217 if (op
> OP_MATCH_MAX
) {
1218 log_error("invalid ATTRS operation");
1221 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1223 log_error("error parsing ATTRS attribute");
1226 if (startswith(attr
, "device/"))
1227 log_error("the 'device' link may not be available in a future kernel, "
1228 "please fix it in %s:%u", filename
, lineno
);
1229 else if (strstr(attr
, "../") != NULL
)
1230 log_error("do not reference parent sysfs directories directly, "
1231 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1232 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1236 if (streq(key
, "TAGS")) {
1237 if (op
> OP_MATCH_MAX
) {
1238 log_error("invalid TAGS operation");
1241 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1245 if (startswith(key
, "ENV{")) {
1246 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1248 log_error("error parsing ENV attribute");
1251 if (op
== OP_REMOVE
) {
1252 log_error("invalid ENV operation");
1255 if (op
< OP_MATCH_MAX
) {
1256 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1259 static const char *blacklist
[] = {
1274 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1275 if (!streq(attr
, blacklist
[i
]))
1277 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1280 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1286 if (streq(key
, "TAG")) {
1287 if (op
< OP_MATCH_MAX
)
1288 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1290 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1294 if (streq(key
, "PROGRAM")) {
1295 if (op
== OP_REMOVE
) {
1296 log_error("invalid PROGRAM operation");
1299 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1303 if (streq(key
, "RESULT")) {
1304 if (op
> OP_MATCH_MAX
) {
1305 log_error("invalid RESULT operation");
1308 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1312 if (startswith(key
, "IMPORT")) {
1313 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1315 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1318 if (op
== OP_REMOVE
) {
1319 log_error("invalid IMPORT operation");
1322 if (streq(attr
, "program")) {
1323 /* find known built-in command */
1324 if (value
[0] != '/') {
1325 enum udev_builtin_cmd cmd
;
1327 cmd
= udev_builtin_lookup(value
);
1328 if (cmd
< UDEV_BUILTIN_MAX
) {
1329 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1330 value
, filename
, lineno
);
1331 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1335 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1336 } else if (streq(attr
, "builtin")) {
1337 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1339 if (cmd
< UDEV_BUILTIN_MAX
)
1340 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1342 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1343 } else if (streq(attr
, "file")) {
1344 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1345 } else if (streq(attr
, "db")) {
1346 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1347 } else if (streq(attr
, "cmdline")) {
1348 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1349 } else if (streq(attr
, "parent")) {
1350 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1352 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1356 if (startswith(key
, "TEST")) {
1359 if (op
> OP_MATCH_MAX
) {
1360 log_error("invalid TEST operation");
1363 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1365 mode
= strtol(attr
, NULL
, 8);
1366 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1368 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1373 if (startswith(key
, "RUN")) {
1374 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1377 if (op
== OP_REMOVE
) {
1378 log_error("invalid RUN operation");
1382 if (streq(attr
, "builtin")) {
1383 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1385 if (cmd
< UDEV_BUILTIN_MAX
)
1386 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1388 log_error("RUN{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1389 } else if (streq(attr
, "program")) {
1390 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1392 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1394 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1400 if (streq(key
, "WAIT_FOR") || streq(key
, "WAIT_FOR_SYSFS")) {
1401 if (op
== OP_REMOVE
) {
1402 log_error("invalid WAIT_FOR/WAIT_FOR_SYSFS operation");
1405 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1409 if (streq(key
, "LABEL")) {
1410 if (op
== OP_REMOVE
) {
1411 log_error("invalid LABEL operation");
1414 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1418 if (streq(key
, "GOTO")) {
1419 if (op
== OP_REMOVE
) {
1420 log_error("invalid GOTO operation");
1423 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1427 if (startswith(key
, "NAME")) {
1428 if (op
== OP_REMOVE
) {
1429 log_error("invalid NAME operation");
1432 if (op
< OP_MATCH_MAX
) {
1433 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1435 if (streq(value
, "%k")) {
1436 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1437 "please remove it from %s:%u\n", filename
, lineno
);
1440 if (value
[0] == '\0') {
1441 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1442 "please remove it from %s:%u\n", filename
, lineno
);
1445 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1447 rule_tmp
.rule
.rule
.can_set_name
= true;
1451 if (streq(key
, "SYMLINK")) {
1452 if (op
== OP_REMOVE
) {
1453 log_error("invalid SYMLINK operation");
1456 if (op
< OP_MATCH_MAX
)
1457 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1459 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1460 rule_tmp
.rule
.rule
.can_set_name
= true;
1464 if (streq(key
, "OWNER")) {
1468 if (op
== OP_REMOVE
) {
1469 log_error("invalid OWNER operation");
1473 uid
= strtoul(value
, &endptr
, 10);
1474 if (endptr
[0] == '\0') {
1475 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1476 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1477 uid
= add_uid(rules
, value
);
1478 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1479 } else if (rules
->resolve_names
>= 0) {
1480 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1482 rule_tmp
.rule
.rule
.can_set_name
= true;
1486 if (streq(key
, "GROUP")) {
1490 if (op
== OP_REMOVE
) {
1491 log_error("invalid GROUP operation");
1495 gid
= strtoul(value
, &endptr
, 10);
1496 if (endptr
[0] == '\0') {
1497 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1498 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1499 gid
= add_gid(rules
, value
);
1500 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1501 } else if (rules
->resolve_names
>= 0) {
1502 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1504 rule_tmp
.rule
.rule
.can_set_name
= true;
1508 if (streq(key
, "MODE")) {
1512 if (op
== OP_REMOVE
) {
1513 log_error("invalid MODE operation");
1517 mode
= strtol(value
, &endptr
, 8);
1518 if (endptr
[0] == '\0')
1519 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1521 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1522 rule_tmp
.rule
.rule
.can_set_name
= true;
1526 if (streq(key
, "OPTIONS")) {
1529 if (op
== OP_REMOVE
) {
1530 log_error("invalid OPTIONS operation");
1534 pos
= strstr(value
, "link_priority=");
1536 int prio
= atoi(&pos
[strlen("link_priority=")]);
1538 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1541 pos
= strstr(value
, "string_escape=");
1543 pos
= &pos
[strlen("string_escape=")];
1544 if (startswith(pos
, "none"))
1545 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1546 else if (startswith(pos
, "replace"))
1547 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1550 pos
= strstr(value
, "db_persist");
1552 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1554 pos
= strstr(value
, "nowatch");
1558 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1560 pos
= strstr(value
, "watch");
1564 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1568 pos
= strstr(value
, "static_node=");
1570 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1571 rule_tmp
.rule
.rule
.has_static_node
= true;
1577 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1581 /* add rule token */
1582 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1583 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1586 /* add tokens to list, sorted by type */
1587 if (sort_token(rules
, &rule_tmp
) != 0)
1592 log_error("invalid rule '%s:%u'", filename
, lineno
);
1596 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1597 _cleanup_fclose_
FILE *f
= NULL
;
1598 unsigned int first_token
;
1599 unsigned int filename_off
;
1600 char line
[UTIL_LINE_SIZE
];
1604 f
= fopen(filename
, "re");
1606 if (errno
== ENOENT
)
1612 if (null_or_empty_fd(fileno(f
))) {
1613 log_debug("Skipping empty file: %s", filename
);
1616 log_debug("Reading rules file: %s", filename
);
1618 first_token
= rules
->token_cur
;
1619 filename_off
= rules_add_string(rules
, filename
);
1621 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1625 /* skip whitespace */
1628 while (isspace(key
[0]))
1639 /* continue reading if backslash+newline is found */
1640 while (line
[len
-2] == '\\') {
1641 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1643 if (strlen(&line
[len
-2]) < 2)
1649 if (len
+1 >= sizeof(line
)) {
1650 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1653 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1656 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1657 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1658 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1659 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1662 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1663 if (rules
->tokens
[j
].type
!= TK_RULE
)
1665 if (rules
->tokens
[j
].rule
.label_off
== 0)
1667 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1669 rules
->tokens
[i
].key
.rule_goto
= j
;
1672 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1673 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1679 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1680 struct udev_rules
*rules
;
1681 struct udev_list file_list
;
1682 struct token end_token
;
1686 rules
= new0(struct udev_rules
, 1);
1690 rules
->resolve_names
= resolve_names
;
1691 udev_list_init(udev
, &file_list
, true);
1693 /* init token array and string buffer */
1694 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1695 if (rules
->tokens
== NULL
)
1696 return udev_rules_unref(rules
);
1697 rules
->token_max
= PREALLOC_TOKEN
;
1699 rules
->strbuf
= strbuf_new();
1701 return udev_rules_unref(rules
);
1703 udev_rules_check_timestamp(rules
);
1705 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1707 log_error_errno(r
, "failed to enumerate rules files: %m");
1708 return udev_rules_unref(rules
);
1712 * The offset value in the rules strct is limited; add all
1713 * rules file names to the beginning of the string buffer.
1715 STRV_FOREACH(f
, files
)
1716 rules_add_string(rules
, *f
);
1718 STRV_FOREACH(f
, files
)
1719 parse_file(rules
, *f
);
1723 memzero(&end_token
, sizeof(struct token
));
1724 end_token
.type
= TK_END
;
1725 add_token(rules
, &end_token
);
1726 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1727 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1729 /* cleanup temporary strbuf data */
1730 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1731 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1732 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1733 strbuf_complete(rules
->strbuf
);
1735 /* cleanup uid/gid cache */
1738 rules
->uids_cur
= 0;
1739 rules
->uids_max
= 0;
1742 rules
->gids_cur
= 0;
1743 rules
->gids_max
= 0;
1749 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1752 free(rules
->tokens
);
1753 strbuf_cleanup(rules
->strbuf
);
1760 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1764 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1767 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1768 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1775 switch (token
->key
.glob
) {
1777 match
= (streq(key_value
, val
));
1780 match
= (fnmatch(key_value
, val
, 0) == 0);
1787 s
= rules_str(rules
, token
->key
.value_off
);
1792 next
= strchr(s
, '|');
1794 size_t matchlen
= (size_t)(next
- s
);
1796 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1800 match
= (streq(s
, val
));
1809 char value
[UTIL_PATH_SIZE
];
1811 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1813 while (key_value
!= NULL
) {
1814 pos
= strchr(key_value
, '|');
1819 match
= (fnmatch(key_value
, val
, 0) == 0);
1827 match
= (val
[0] != '\0');
1833 if (match
&& (token
->key
.op
== OP_MATCH
))
1835 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1840 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1842 char nbuf
[UTIL_NAME_SIZE
];
1844 char vbuf
[UTIL_NAME_SIZE
];
1847 name
= rules_str(rules
, cur
->key
.attr_off
);
1848 switch (cur
->key
.attrsubst
) {
1850 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1854 value
= udev_device_get_sysattr_value(dev
, name
);
1859 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1867 /* remove trailing whitespace, if not asked to match for it */
1868 len
= strlen(value
);
1869 if (len
> 0 && isspace(value
[len
-1])) {
1870 const char *key_value
;
1873 key_value
= rules_str(rules
, cur
->key
.value_off
);
1874 klen
= strlen(key_value
);
1875 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1876 if (value
!= vbuf
) {
1877 strscpy(vbuf
, sizeof(vbuf
), value
);
1880 while (len
> 0 && isspace(vbuf
[--len
]))
1885 return match_key(rules
, cur
, value
);
1894 int udev_rules_apply_to_event(struct udev_rules
*rules
,
1895 struct udev_event
*event
,
1896 usec_t timeout_usec
,
1897 usec_t timeout_warn_usec
,
1898 struct udev_list
*properties_list
) {
1901 enum escape_type esc
= ESCAPE_UNSET
;
1904 if (rules
->tokens
== NULL
)
1907 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1908 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1909 udev_device_get_ifindex(event
->dev
) > 0));
1911 /* loop through token list, match, run actions or forward to next rule */
1912 cur
= &rules
->tokens
[0];
1915 dump_token(rules
, cur
);
1916 switch (cur
->type
) {
1920 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1921 if (!can_set_name
&& rule
->rule
.can_set_name
)
1926 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1930 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1934 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1937 case TK_M_DEVLINK
: {
1938 struct udev_list_entry
*list_entry
;
1941 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1942 const char *devlink
;
1944 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1945 if (match_key(rules
, cur
, devlink
) == 0) {
1955 if (match_key(rules
, cur
, event
->name
) != 0)
1959 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1962 value
= udev_device_get_property_value(event
->dev
, key_name
);
1964 /* check global properties */
1965 if (!value
&& properties_list
) {
1966 struct udev_list_entry
*list_entry
;
1968 list_entry
= udev_list_get_entry(properties_list
);
1969 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1970 if (list_entry
!= NULL
)
1971 value
= udev_list_entry_get_value(list_entry
);
1976 if (match_key(rules
, cur
, value
))
1981 struct udev_list_entry
*list_entry
;
1984 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1985 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1990 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
1994 case TK_M_SUBSYSTEM
:
1995 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1999 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
2002 case TK_M_WAITFOR
: {
2003 char filename
[UTIL_PATH_SIZE
];
2006 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2007 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
2008 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
2013 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
2017 char filename
[UTIL_PATH_SIZE
];
2018 _cleanup_free_
char *value
= NULL
;
2021 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
2022 sysctl_normalize(filename
);
2023 if (sysctl_read(filename
, &value
) < 0)
2026 len
= strlen(value
);
2027 while (len
> 0 && isspace(value
[--len
]))
2029 if (match_key(rules
, cur
, value
) != 0)
2034 case TK_M_SUBSYSTEMS
:
2040 /* get whole sequence of parent matches */
2042 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2045 /* loop over parents */
2046 event
->dev_parent
= event
->dev
;
2050 /* loop over sequence of parent match keys */
2051 for (key
= cur
; key
< next
; key
++ ) {
2052 dump_token(rules
, key
);
2055 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2058 case TK_M_SUBSYSTEMS
:
2059 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2063 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2067 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2071 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
2073 if (match
&& key
->key
.op
== OP_NOMATCH
)
2075 if (!match
&& key
->key
.op
== OP_MATCH
)
2086 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2087 if (event
->dev_parent
== NULL
)
2090 /* move behind our sequence of parent match keys */
2095 char filename
[UTIL_PATH_SIZE
];
2096 struct stat statbuf
;
2099 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2100 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2101 if (filename
[0] != '/') {
2102 char tmp
[UTIL_PATH_SIZE
];
2104 strscpy(tmp
, sizeof(tmp
), filename
);
2105 strscpyl(filename
, sizeof(filename
),
2106 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2109 attr_subst_subdir(filename
, sizeof(filename
));
2111 match
= (stat(filename
, &statbuf
) == 0);
2112 if (match
&& cur
->key
.mode
> 0)
2113 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2114 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2116 if (!match
&& cur
->key
.op
== OP_MATCH
)
2120 case TK_M_PROGRAM
: {
2121 char program
[UTIL_PATH_SIZE
];
2123 char result
[UTIL_LINE_SIZE
];
2125 free(event
->program_result
);
2126 event
->program_result
= NULL
;
2127 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2128 envp
= udev_device_get_properties_envp(event
->dev
);
2129 log_debug("PROGRAM '%s' %s:%u",
2131 rules_str(rules
, rule
->rule
.filename_off
),
2132 rule
->rule
.filename_line
);
2134 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, program
, envp
, result
, sizeof(result
)) < 0) {
2135 if (cur
->key
.op
!= OP_NOMATCH
)
2140 util_remove_trailing_chars(result
, '\n');
2141 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2142 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2144 log_debug("%i character(s) replaced" , count
);
2146 event
->program_result
= strdup(result
);
2147 if (cur
->key
.op
== OP_NOMATCH
)
2152 case TK_M_IMPORT_FILE
: {
2153 char import
[UTIL_PATH_SIZE
];
2155 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2156 if (import_file_into_properties(event
->dev
, import
) != 0)
2157 if (cur
->key
.op
!= OP_NOMATCH
)
2161 case TK_M_IMPORT_PROG
: {
2162 char import
[UTIL_PATH_SIZE
];
2164 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2165 log_debug("IMPORT '%s' %s:%u",
2167 rules_str(rules
, rule
->rule
.filename_off
),
2168 rule
->rule
.filename_line
);
2170 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
2171 if (cur
->key
.op
!= OP_NOMATCH
)
2175 case TK_M_IMPORT_BUILTIN
: {
2176 char command
[UTIL_PATH_SIZE
];
2178 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2179 /* check if we ran already */
2180 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2181 log_debug("IMPORT builtin skip '%s' %s:%u",
2182 udev_builtin_name(cur
->key
.builtin_cmd
),
2183 rules_str(rules
, rule
->rule
.filename_off
),
2184 rule
->rule
.filename_line
);
2185 /* return the result from earlier run */
2186 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2187 if (cur
->key
.op
!= OP_NOMATCH
)
2192 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2195 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2196 log_debug("IMPORT builtin '%s' %s:%u",
2197 udev_builtin_name(cur
->key
.builtin_cmd
),
2198 rules_str(rules
, rule
->rule
.filename_off
),
2199 rule
->rule
.filename_line
);
2201 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2202 /* remember failure */
2203 log_debug("IMPORT builtin '%s' returned non-zero",
2204 udev_builtin_name(cur
->key
.builtin_cmd
));
2205 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2206 if (cur
->key
.op
!= OP_NOMATCH
)
2211 case TK_M_IMPORT_DB
: {
2212 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2215 value
= udev_device_get_property_value(event
->dev_db
, key
);
2217 udev_device_add_property(event
->dev
, key
, value
);
2219 if (cur
->key
.op
!= OP_NOMATCH
)
2224 case TK_M_IMPORT_CMDLINE
: {
2226 bool imported
= false;
2228 f
= fopen("/proc/cmdline", "re");
2232 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2233 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2236 pos
= strstr(cmdline
, key
);
2239 if (pos
[0] == '\0' || isspace(pos
[0])) {
2240 /* we import simple flags as 'FLAG=1' */
2241 udev_device_add_property(event
->dev
, key
, "1");
2243 } else if (pos
[0] == '=') {
2248 while (pos
[0] != '\0' && !isspace(pos
[0]))
2251 udev_device_add_property(event
->dev
, key
, value
);
2258 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2262 case TK_M_IMPORT_PARENT
: {
2263 char import
[UTIL_PATH_SIZE
];
2265 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2266 if (import_parent_into_properties(event
->dev
, import
) != 0)
2267 if (cur
->key
.op
!= OP_NOMATCH
)
2272 if (match_key(rules
, cur
, event
->program_result
) != 0)
2275 case TK_A_STRING_ESCAPE_NONE
:
2278 case TK_A_STRING_ESCAPE_REPLACE
:
2279 esc
= ESCAPE_REPLACE
;
2281 case TK_A_DB_PERSIST
:
2282 udev_device_set_db_persist(event
->dev
);
2284 case TK_A_INOTIFY_WATCH
:
2285 if (event
->inotify_watch_final
)
2287 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2288 event
->inotify_watch_final
= true;
2289 event
->inotify_watch
= cur
->key
.watch
;
2291 case TK_A_DEVLINK_PRIO
:
2292 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2295 char owner
[UTIL_NAME_SIZE
];
2296 const char *ow
= owner
;
2299 if (event
->owner_final
)
2301 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2302 event
->owner_final
= true;
2303 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2304 event
->owner_set
= true;
2305 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2307 if (r
== -ENOENT
|| r
== -ESRCH
)
2308 log_error("specified user '%s' unknown", owner
);
2310 log_error_errno(r
, "error resolving user '%s': %m", owner
);
2314 log_debug("OWNER %u %s:%u",
2316 rules_str(rules
, rule
->rule
.filename_off
),
2317 rule
->rule
.filename_line
);
2321 char group
[UTIL_NAME_SIZE
];
2322 const char *gr
= group
;
2325 if (event
->group_final
)
2327 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2328 event
->group_final
= true;
2329 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2330 event
->group_set
= true;
2331 r
= get_group_creds(&gr
, &event
->gid
);
2333 if (r
== -ENOENT
|| r
== -ESRCH
)
2334 log_error("specified group '%s' unknown", group
);
2336 log_error_errno(r
, "error resolving group '%s': %m", group
);
2340 log_debug("GROUP %u %s:%u",
2342 rules_str(rules
, rule
->rule
.filename_off
),
2343 rule
->rule
.filename_line
);
2347 char mode_str
[UTIL_NAME_SIZE
];
2351 if (event
->mode_final
)
2353 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2354 mode
= strtol(mode_str
, &endptr
, 8);
2355 if (endptr
[0] != '\0') {
2356 log_error("ignoring invalid mode '%s'", mode_str
);
2359 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2360 event
->mode_final
= true;
2361 event
->mode_set
= true;
2363 log_debug("MODE %#o %s:%u",
2365 rules_str(rules
, rule
->rule
.filename_off
),
2366 rule
->rule
.filename_line
);
2370 if (event
->owner_final
)
2372 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2373 event
->owner_final
= true;
2374 event
->owner_set
= true;
2375 event
->uid
= cur
->key
.uid
;
2376 log_debug("OWNER %u %s:%u",
2378 rules_str(rules
, rule
->rule
.filename_off
),
2379 rule
->rule
.filename_line
);
2382 if (event
->group_final
)
2384 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2385 event
->group_final
= true;
2386 event
->group_set
= true;
2387 event
->gid
= cur
->key
.gid
;
2388 log_debug("GROUP %u %s:%u",
2390 rules_str(rules
, rule
->rule
.filename_off
),
2391 rule
->rule
.filename_line
);
2394 if (event
->mode_final
)
2396 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2397 event
->mode_final
= true;
2398 event
->mode_set
= true;
2399 event
->mode
= cur
->key
.mode
;
2400 log_debug("MODE %#o %s:%u",
2402 rules_str(rules
, rule
->rule
.filename_off
),
2403 rule
->rule
.filename_line
);
2405 case TK_A_SECLABEL
: {
2406 const char *name
, *label
;
2408 name
= rules_str(rules
, cur
->key
.attr_off
);
2409 label
= rules_str(rules
, cur
->key
.value_off
);
2410 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2411 udev_list_cleanup(&event
->seclabel_list
);
2412 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2413 log_debug("SECLABEL{%s}='%s' %s:%u",
2415 rules_str(rules
, rule
->rule
.filename_off
),
2416 rule
->rule
.filename_line
);
2420 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2421 char *value
= rules_str(rules
, cur
->key
.value_off
);
2422 char value_new
[UTIL_NAME_SIZE
];
2423 const char *value_old
= NULL
;
2425 if (value
[0] == '\0') {
2426 if (cur
->key
.op
== OP_ADD
)
2428 udev_device_add_property(event
->dev
, name
, NULL
);
2432 if (cur
->key
.op
== OP_ADD
)
2433 value_old
= udev_device_get_property_value(event
->dev
, name
);
2435 char temp
[UTIL_NAME_SIZE
];
2437 /* append value separated by space */
2438 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2439 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2441 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2443 udev_device_add_property(event
->dev
, name
, value_new
);
2447 char tag
[UTIL_PATH_SIZE
];
2450 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2451 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2452 udev_device_cleanup_tags_list(event
->dev
);
2453 for (p
= tag
; *p
!= '\0'; p
++) {
2454 if ((*p
>= 'a' && *p
<= 'z') ||
2455 (*p
>= 'A' && *p
<= 'Z') ||
2456 (*p
>= '0' && *p
<= '9') ||
2457 *p
== '-' || *p
== '_')
2459 log_error("ignoring invalid tag name '%s'", tag
);
2462 if (cur
->key
.op
== OP_REMOVE
)
2463 udev_device_remove_tag(event
->dev
, tag
);
2465 udev_device_add_tag(event
->dev
, tag
);
2469 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2471 char name_str
[UTIL_PATH_SIZE
];
2474 if (event
->name_final
)
2476 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2477 event
->name_final
= true;
2478 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2479 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2480 count
= util_replace_chars(name_str
, "/");
2482 log_debug("%i character(s) replaced", count
);
2484 if (major(udev_device_get_devnum(event
->dev
)) &&
2485 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2486 log_error("NAME=\"%s\" ignored, kernel device nodes "
2487 "can not be renamed; please fix it in %s:%u\n", name
,
2488 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2492 event
->name
= strdup(name_str
);
2493 log_debug("NAME '%s' %s:%u",
2495 rules_str(rules
, rule
->rule
.filename_off
),
2496 rule
->rule
.filename_line
);
2499 case TK_A_DEVLINK
: {
2500 char temp
[UTIL_PATH_SIZE
];
2501 char filename
[UTIL_PATH_SIZE
];
2505 if (event
->devlink_final
)
2507 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2509 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2510 event
->devlink_final
= true;
2511 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2512 udev_device_cleanup_devlinks_list(event
->dev
);
2514 /* allow multiple symlinks separated by spaces */
2515 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2516 if (esc
== ESCAPE_UNSET
)
2517 count
= util_replace_chars(temp
, "/ ");
2518 else if (esc
== ESCAPE_REPLACE
)
2519 count
= util_replace_chars(temp
, "/");
2521 log_debug("%i character(s) replaced" , count
);
2523 while (isspace(pos
[0]))
2525 next
= strchr(pos
, ' ');
2526 while (next
!= NULL
) {
2528 log_debug("LINK '%s' %s:%u", pos
,
2529 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2530 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2531 udev_device_add_devlink(event
->dev
, filename
);
2532 while (isspace(next
[1]))
2535 next
= strchr(pos
, ' ');
2537 if (pos
[0] != '\0') {
2538 log_debug("LINK '%s' %s:%u", pos
,
2539 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2540 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2541 udev_device_add_devlink(event
->dev
, filename
);
2546 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2547 char attr
[UTIL_PATH_SIZE
];
2548 char value
[UTIL_NAME_SIZE
];
2551 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2552 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2553 attr_subst_subdir(attr
, sizeof(attr
));
2555 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2556 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2557 rules_str(rules
, rule
->rule
.filename_off
),
2558 rule
->rule
.filename_line
);
2559 f
= fopen(attr
, "we");
2561 if (fprintf(f
, "%s", value
) <= 0)
2562 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2565 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2570 char filename
[UTIL_PATH_SIZE
];
2571 char value
[UTIL_NAME_SIZE
];
2574 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
2575 sysctl_normalize(filename
);
2576 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2577 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2578 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2579 r
= sysctl_write(filename
, value
);
2581 log_error("error writing SYSCTL{%s}='%s': %s", filename
, value
, strerror(-r
));
2584 case TK_A_RUN_BUILTIN
:
2585 case TK_A_RUN_PROGRAM
: {
2586 struct udev_list_entry
*entry
;
2588 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2589 udev_list_cleanup(&event
->run_list
);
2590 log_debug("RUN '%s' %s:%u",
2591 rules_str(rules
, cur
->key
.value_off
),
2592 rules_str(rules
, rule
->rule
.filename_off
),
2593 rule
->rule
.filename_line
);
2594 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2595 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2599 if (cur
->key
.rule_goto
== 0)
2601 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2606 case TK_M_PARENTS_MIN
:
2607 case TK_M_PARENTS_MAX
:
2610 log_error("wrong type %u", cur
->type
);
2617 /* fast-forward to next rule */
2618 cur
= rule
+ rule
->rule
.token_count
;
2622 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2628 _cleanup_strv_free_
char **tags
= NULL
;
2631 _cleanup_free_
char *path
= NULL
;
2634 if (rules
->tokens
== NULL
)
2637 cur
= &rules
->tokens
[0];
2640 switch (cur
->type
) {
2645 /* skip rules without a static_node tag */
2646 if (!rule
->rule
.has_static_node
)
2662 mode
= cur
->key
.mode
;
2665 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2670 case TK_A_STATIC_NODE
: {
2671 char device_node
[UTIL_PATH_SIZE
];
2672 char tags_dir
[UTIL_PATH_SIZE
];
2673 char tag_symlink
[UTIL_PATH_SIZE
];
2676 /* we assure, that the permissions tokens are sorted before the static token */
2678 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2681 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2682 if (stat(device_node
, &stats
) != 0)
2684 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2687 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2689 STRV_FOREACH(t
, tags
) {
2690 _cleanup_free_
char *unescaped_filename
= NULL
;
2692 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2693 r
= mkdir_p(tags_dir
, 0755);
2695 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2697 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2699 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2700 r
= symlink(device_node
, tag_symlink
);
2701 if (r
< 0 && errno
!= EEXIST
)
2702 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2703 tag_symlink
, device_node
);
2709 /* don't touch the permissions if only the tags were set */
2710 if (mode
== 0 && uid
== 0 && gid
== 0)
2719 if (mode
!= (stats
.st_mode
& 01777)) {
2720 r
= chmod(device_node
, mode
);
2722 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2725 log_debug("chmod '%s' %#o", device_node
, mode
);
2728 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2729 r
= chown(device_node
, uid
, gid
);
2731 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2734 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2737 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2747 /* fast-forward to next rule */
2748 cur
= rule
+ rule
->rule
.token_count
;
2755 fchmod(fileno(f
), 0644);
2756 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2758 unlink("/run/udev/static_node-tags");