2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "path-util.h"
34 #include "conf-files.h"
39 #define PREALLOC_TOKEN 2048
42 unsigned int name_off
;
49 static const char* const rules_dirs
[] = {
52 UDEVLIBEXECDIR
"/rules.d",
60 /* every key in the rules file becomes a token */
62 unsigned int token_cur
;
63 unsigned int token_max
;
65 /* all key strings are copied and de-duplicated in a single continuous string buffer */
66 struct strbuf
*strbuf
;
68 /* during rule parsing, uid/gid lookup results are cached */
70 unsigned int uids_cur
;
71 unsigned int uids_max
;
73 unsigned int gids_cur
;
74 unsigned int gids_max
;
77 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
78 return rules
->strbuf
->buf
+ off
;
81 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
82 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
85 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
98 enum string_glob_type
{
100 GL_PLAIN
, /* no special chars */
101 GL_GLOB
, /* shell globs ?,*,[] */
102 GL_SPLIT
, /* multi-value A|B */
103 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
104 GL_SOMETHING
, /* commonly used "?*" */
107 enum string_subst_type
{
114 /* tokens of a rule are sorted/handled in this order */
119 TK_M_ACTION
, /* val */
120 TK_M_DEVPATH
, /* val */
121 TK_M_KERNEL
, /* val */
122 TK_M_DEVLINK
, /* val */
124 TK_M_ENV
, /* val, attr */
126 TK_M_SUBSYSTEM
, /* val */
127 TK_M_DRIVER
, /* val */
128 TK_M_WAITFOR
, /* val */
129 TK_M_ATTR
, /* val, attr */
132 TK_M_KERNELS
, /* val */
133 TK_M_SUBSYSTEMS
, /* val */
134 TK_M_DRIVERS
, /* val */
135 TK_M_ATTRS
, /* val, attr */
139 TK_M_TEST
, /* val, mode_t */
140 TK_M_EVENT_TIMEOUT
, /* int */
141 TK_M_PROGRAM
, /* val */
142 TK_M_IMPORT_FILE
, /* val */
143 TK_M_IMPORT_PROG
, /* val */
144 TK_M_IMPORT_BUILTIN
, /* val */
145 TK_M_IMPORT_DB
, /* val */
146 TK_M_IMPORT_CMDLINE
, /* val */
147 TK_M_IMPORT_PARENT
, /* val */
148 TK_M_RESULT
, /* val */
151 TK_A_STRING_ESCAPE_NONE
,
152 TK_A_STRING_ESCAPE_REPLACE
,
154 TK_A_INOTIFY_WATCH
, /* int */
155 TK_A_DEVLINK_PRIO
, /* int */
156 TK_A_OWNER
, /* val */
157 TK_A_GROUP
, /* val */
159 TK_A_OWNER_ID
, /* uid_t */
160 TK_A_GROUP_ID
, /* gid_t */
161 TK_A_MODE_ID
, /* mode_t */
163 TK_A_STATIC_NODE
, /* val */
164 TK_A_SECLABEL
, /* val, attr */
165 TK_A_ENV
, /* val, attr */
167 TK_A_DEVLINK
, /* val */
168 TK_A_ATTR
, /* val, attr */
169 TK_A_RUN_BUILTIN
, /* val, bool */
170 TK_A_RUN_PROGRAM
, /* val, bool */
171 TK_A_GOTO
, /* size_t */
176 /* we try to pack stuff in a way that we take only 12 bytes per token */
179 unsigned char type
; /* same in rule and key */
181 enum token_type type
:8;
183 bool has_static_node
:1;
184 unsigned int unused
:6;
185 unsigned short token_count
;
186 unsigned int label_off
;
187 unsigned short filename_off
;
188 unsigned short filename_line
;
191 enum token_type type
:8;
192 enum operation_type op
:8;
193 enum string_glob_type glob
:8;
194 enum string_subst_type subst
:4;
195 enum string_subst_type attrsubst
:4;
196 unsigned int value_off
;
198 unsigned int attr_off
;
199 unsigned int rule_goto
;
206 enum udev_builtin_cmd builtin_cmd
;
214 struct udev_rules
*rules
;
216 struct token token
[MAX_TK
];
217 unsigned int token_cur
;
221 static const char *operation_str(enum operation_type type
)
223 static const char *operation_strs
[] = {
224 [OP_UNSET
] = "UNSET",
225 [OP_MATCH
] = "match",
226 [OP_NOMATCH
] = "nomatch",
227 [OP_MATCH_MAX
] = "MATCH_MAX",
230 [OP_ASSIGN
] = "assign",
231 [OP_ASSIGN_FINAL
] = "assign-final",
234 return operation_strs
[type
];
237 static const char *string_glob_str(enum string_glob_type type
)
239 static const char *string_glob_strs
[] = {
240 [GL_UNSET
] = "UNSET",
241 [GL_PLAIN
] = "plain",
243 [GL_SPLIT
] = "split",
244 [GL_SPLIT_GLOB
] = "split-glob",
245 [GL_SOMETHING
] = "split-glob",
248 return string_glob_strs
[type
];
251 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",
269 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
270 [TK_M_KERNELS
] = "M KERNELS",
271 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
272 [TK_M_DRIVERS
] = "M DRIVERS",
273 [TK_M_ATTRS
] = "M ATTRS",
274 [TK_M_TAGS
] = "M TAGS",
275 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
277 [TK_M_TEST
] = "M TEST",
278 [TK_M_EVENT_TIMEOUT
] = "M EVENT_TIMEOUT",
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_RUN_BUILTIN
] = "A RUN_BUILTIN",
308 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
309 [TK_A_GOTO
] = "A GOTO",
314 return token_strs
[type
];
317 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
);
375 log_debug("%s %s '%s' '%s'(%s)",
376 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
380 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
382 case TK_A_STRING_ESCAPE_NONE
:
383 case TK_A_STRING_ESCAPE_REPLACE
:
384 case TK_A_DB_PERSIST
:
385 log_debug("%s", token_str(type
));
388 log_debug("%s %s '%s'(%s) %#o",
389 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
391 case TK_A_INOTIFY_WATCH
:
392 log_debug("%s %u", token_str(type
), token
->key
.watch
);
394 case TK_A_DEVLINK_PRIO
:
395 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
398 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
401 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
404 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
406 case TK_A_STATIC_NODE
:
407 log_debug("%s '%s'", token_str(type
), value
);
410 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
412 case TK_M_EVENT_TIMEOUT
:
413 log_debug("%s %u", token_str(type
), token
->key
.event_timeout
);
416 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
419 log_debug("* %s", token_str(type
));
421 case TK_M_PARENTS_MIN
:
422 case TK_M_PARENTS_MAX
:
425 log_debug("unknown type %u", type
);
430 static void dump_rules(struct udev_rules
*rules
)
434 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
436 rules
->token_cur
* sizeof(struct token
),
439 for (i
= 0; i
< rules
->token_cur
; i
++)
440 dump_token(rules
, &rules
->tokens
[i
]);
443 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
444 static inline void dump_rules(struct udev_rules
*rules
) {}
447 static int add_token(struct udev_rules
*rules
, struct token
*token
)
449 /* grow buffer if needed */
450 if (rules
->token_cur
+1 >= rules
->token_max
) {
451 struct token
*tokens
;
454 /* double the buffer size */
455 add
= rules
->token_max
;
459 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
462 rules
->tokens
= tokens
;
463 rules
->token_max
+= add
;
465 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
470 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
476 /* lookup, if we know it already */
477 for (i
= 0; i
< rules
->uids_cur
; i
++) {
478 off
= rules
->uids
[i
].name_off
;
479 if (streq(rules_str(rules
, off
), owner
)) {
480 uid
= rules
->uids
[i
].uid
;
484 uid
= util_lookup_user(rules
->udev
, owner
);
486 /* grow buffer if needed */
487 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
488 struct uid_gid
*uids
;
491 /* double the buffer size */
492 add
= rules
->uids_max
;
496 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
500 rules
->uids_max
+= add
;
502 rules
->uids
[rules
->uids_cur
].uid
= uid
;
503 off
= rules_add_string(rules
, owner
);
506 rules
->uids
[rules
->uids_cur
].name_off
= off
;
511 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
517 /* lookup, if we know it already */
518 for (i
= 0; i
< rules
->gids_cur
; i
++) {
519 off
= rules
->gids
[i
].name_off
;
520 if (streq(rules_str(rules
, off
), group
)) {
521 gid
= rules
->gids
[i
].gid
;
525 gid
= util_lookup_group(rules
->udev
, group
);
527 /* grow buffer if needed */
528 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
529 struct uid_gid
*gids
;
532 /* double the buffer size */
533 add
= rules
->gids_max
;
537 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
541 rules
->gids_max
+= add
;
543 rules
->gids
[rules
->gids_cur
].gid
= gid
;
544 off
= rules_add_string(rules
, group
);
547 rules
->gids
[rules
->gids_cur
].name_off
= off
;
552 static int import_property_from_string(struct udev_device
*dev
, char *line
)
557 struct udev_list_entry
*entry
;
561 while (isspace(key
[0]))
564 /* comment or empty line */
565 if (key
[0] == '#' || key
[0] == '\0')
568 /* split key/value */
569 val
= strchr(key
, '=');
576 while (isspace(val
[0]))
583 while (isspace(key
[len
-1]))
587 /* terminate value */
591 while (isspace(val
[len
-1]))
599 if (val
[0] == '"' || val
[0] == '\'') {
600 if (val
[len
-1] != val
[0]) {
601 log_debug("inconsistent quoting: '%s', skip", line
);
608 entry
= udev_device_add_property(dev
, key
, val
);
609 /* store in db, skip private keys */
611 udev_list_entry_set_num(entry
, true);
616 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
619 char line
[UTIL_LINE_SIZE
];
621 f
= fopen(filename
, "re");
624 while (fgets(line
, sizeof(line
), f
) != NULL
)
625 import_property_from_string(dev
, line
);
630 static int import_program_into_properties(struct udev_event
*event
, const char *program
, const sigset_t
*sigmask
)
632 struct udev_device
*dev
= event
->dev
;
634 char result
[UTIL_LINE_SIZE
];
638 envp
= udev_device_get_properties_envp(dev
);
639 err
= udev_event_spawn(event
, program
, envp
, sigmask
, result
, sizeof(result
));
644 while (line
!= NULL
) {
647 pos
= strchr(line
, '\n');
652 import_property_from_string(dev
, line
);
658 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
660 struct udev_device
*dev_parent
;
661 struct udev_list_entry
*list_entry
;
663 dev_parent
= udev_device_get_parent(dev
);
664 if (dev_parent
== NULL
)
667 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
668 const char *key
= udev_list_entry_get_name(list_entry
);
669 const char *val
= udev_list_entry_get_value(list_entry
);
671 if (fnmatch(filter
, key
, 0) == 0) {
672 struct udev_list_entry
*entry
;
674 entry
= udev_device_add_property(dev
, key
, val
);
675 /* store in db, skip private keys */
677 udev_list_entry_set_num(entry
, true);
683 #define WAIT_LOOP_PER_SECOND 50
684 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
686 char filepath
[UTIL_PATH_SIZE
];
687 char devicepath
[UTIL_PATH_SIZE
];
689 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
691 /* a relative path is a device attribute */
692 devicepath
[0] = '\0';
693 if (file
[0] != '/') {
694 strscpyl(devicepath
, sizeof(devicepath
), udev_device_get_syspath(dev
), NULL
);
695 strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
700 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
703 if (stat(file
, &stats
) == 0) {
704 log_debug("file '%s' appeared after %i loops", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
707 /* make sure, the device did not disappear in the meantime */
708 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
709 log_debug("device disappeared while waiting for '%s'", file
);
712 log_debug("wait for '%s' for %i mseconds", file
, 1000 / WAIT_LOOP_PER_SECOND
);
713 nanosleep(&duration
, NULL
);
715 log_debug("waiting for '%s' failed", file
);
719 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
)
763 if (linepos
== NULL
|| linepos
[0] == '\0')
766 /* skip whitespace */
767 while (isspace(linepos
[0]) || linepos
[0] == ',')
771 if (linepos
[0] == '\0')
777 if (linepos
[0] == '\0')
779 if (isspace(linepos
[0]))
781 if (linepos
[0] == '=')
783 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
784 if (linepos
[1] == '=')
788 /* remember end of key */
791 /* skip whitespace after key */
792 while (isspace(linepos
[0]))
794 if (linepos
[0] == '\0')
797 /* get operation type */
798 if (linepos
[0] == '=' && linepos
[1] == '=') {
801 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
804 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
807 } else if (linepos
[0] == '=') {
810 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
811 *op
= OP_ASSIGN_FINAL
;
819 /* skip whitespace after operator */
820 while (isspace(linepos
[0]))
822 if (linepos
[0] == '\0')
826 if (linepos
[0] == '"')
833 temp
= strchr(linepos
, '"');
839 /* move line to next key */
844 /* extract possible KEY{attr} */
845 static const char *get_key_attribute(struct udev
*udev
, char *str
)
850 attr
= strchr(str
, '{');
853 pos
= strchr(attr
, '}');
855 log_error("missing closing brace for format");
864 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
865 enum operation_type op
,
866 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
;
915 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
916 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
919 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
921 token
->key
.mode
= *(mode_t
*)data
;
923 case TK_A_STRING_ESCAPE_NONE
:
924 case TK_A_STRING_ESCAPE_REPLACE
:
925 case TK_A_DB_PERSIST
:
927 case TK_A_RUN_BUILTIN
:
928 case TK_A_RUN_PROGRAM
:
929 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
930 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
932 case TK_A_INOTIFY_WATCH
:
933 case TK_A_DEVLINK_PRIO
:
934 token
->key
.devlink_prio
= *(int *)data
;
937 token
->key
.uid
= *(uid_t
*)data
;
940 token
->key
.gid
= *(gid_t
*)data
;
943 token
->key
.mode
= *(mode_t
*)data
;
945 case TK_M_EVENT_TIMEOUT
:
946 token
->key
.event_timeout
= *(int *)data
;
949 case TK_M_PARENTS_MIN
:
950 case TK_M_PARENTS_MAX
:
954 log_error("wrong type %u", type
);
958 if (value
!= NULL
&& type
< TK_M_MAX
) {
959 /* check if we need to split or call fnmatch() while matching rules */
960 enum string_glob_type glob
;
964 has_split
= (strchr(value
, '|') != NULL
);
965 has_glob
= string_is_glob(value
);
966 if (has_split
&& has_glob
) {
967 glob
= GL_SPLIT_GLOB
;
968 } else if (has_split
) {
970 } else if (has_glob
) {
971 if (streq(value
, "?*"))
978 token
->key
.glob
= glob
;
981 if (value
!= NULL
&& type
> TK_M_MAX
) {
982 /* check if assigned value has substitution chars */
984 token
->key
.subst
= SB_SUBSYS
;
985 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
986 token
->key
.subst
= SB_FORMAT
;
988 token
->key
.subst
= SB_NONE
;
992 /* check if property/attribute name has substitution chars */
994 token
->key
.attrsubst
= SB_SUBSYS
;
995 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
996 token
->key
.attrsubst
= SB_FORMAT
;
998 token
->key
.attrsubst
= SB_NONE
;
1001 token
->key
.type
= type
;
1003 rule_tmp
->token_cur
++;
1004 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
1005 log_error("temporary rule array too small");
1011 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1014 unsigned int start
= 0;
1015 unsigned int end
= rule_tmp
->token_cur
;
1017 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1018 enum token_type next_val
= TK_UNSET
;
1019 unsigned int next_idx
= 0;
1022 /* find smallest value */
1023 for (j
= start
; j
< end
; j
++) {
1024 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1026 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1027 next_val
= rule_tmp
->token
[j
].type
;
1032 /* add token and mark done */
1033 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1035 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1038 if (next_idx
== start
)
1040 if (next_idx
+1 == end
)
1046 static int add_rule(struct udev_rules
*rules
, char *line
,
1047 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1051 struct rule_tmp rule_tmp
;
1053 memzero(&rule_tmp
, sizeof(struct rule_tmp
));
1054 rule_tmp
.rules
= rules
;
1055 rule_tmp
.rule
.type
= TK_RULE
;
1056 /* the offset in the rule is limited to unsigned short */
1057 if (filename_off
< USHRT_MAX
)
1058 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1059 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1065 enum operation_type op
;
1067 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1068 /* Avoid erroring on trailing whitespace. This is probably rare
1069 * so save the work for the error case instead of always trying
1070 * to strip the trailing whitespace with strstrip(). */
1071 while (isblank(*linepos
))
1074 /* If we aren't at the end of the line, this is a parsing error.
1075 * Make a best effort to describe where the problem is. */
1076 if (*linepos
!= '\n') {
1077 char buf
[2] = {linepos
[1]};
1078 _cleanup_free_
char *tmp
;
1081 log_error("invalid key/value pair in file %s on line %u,"
1082 "starting at character %tu ('%s')\n",
1083 filename
, lineno
, linepos
- line
+ 1, tmp
);
1084 if (linepos
[1] == '#')
1085 log_error("hint: comments can only start at beginning of line");
1090 if (streq(key
, "ACTION")) {
1091 if (op
> OP_MATCH_MAX
) {
1092 log_error("invalid ACTION operation");
1095 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1099 if (streq(key
, "DEVPATH")) {
1100 if (op
> OP_MATCH_MAX
) {
1101 log_error("invalid DEVPATH operation");
1104 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1108 if (streq(key
, "KERNEL")) {
1109 if (op
> OP_MATCH_MAX
) {
1110 log_error("invalid KERNEL operation");
1113 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1117 if (streq(key
, "SUBSYSTEM")) {
1118 if (op
> OP_MATCH_MAX
) {
1119 log_error("invalid SUBSYSTEM operation");
1122 /* bus, class, subsystem events should all be the same */
1123 if (streq(value
, "subsystem") ||
1124 streq(value
, "bus") ||
1125 streq(value
, "class")) {
1126 if (streq(value
, "bus") || streq(value
, "class"))
1127 log_error("'%s' must be specified as 'subsystem' "
1128 "please fix it in %s:%u", value
, filename
, lineno
);
1129 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1131 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1135 if (streq(key
, "DRIVER")) {
1136 if (op
> OP_MATCH_MAX
) {
1137 log_error("invalid DRIVER operation");
1140 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1144 if (startswith(key
, "ATTR{")) {
1145 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1147 log_error("error parsing ATTR attribute");
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
);
1158 if (startswith(key
, "SECLABEL{")) {
1159 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1161 log_error("error parsing SECLABEL attribute");
1165 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1169 if (streq(key
, "KERNELS")) {
1170 if (op
> OP_MATCH_MAX
) {
1171 log_error("invalid KERNELS operation");
1174 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1178 if (streq(key
, "SUBSYSTEMS")) {
1179 if (op
> OP_MATCH_MAX
) {
1180 log_error("invalid SUBSYSTEMS operation");
1183 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1187 if (streq(key
, "DRIVERS")) {
1188 if (op
> OP_MATCH_MAX
) {
1189 log_error("invalid DRIVERS operation");
1192 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1196 if (startswith(key
, "ATTRS{")) {
1197 if (op
> OP_MATCH_MAX
) {
1198 log_error("invalid ATTRS operation");
1201 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1203 log_error("error parsing ATTRS attribute");
1206 if (startswith(attr
, "device/"))
1207 log_error("the 'device' link may not be available in a future kernel, "
1208 "please fix it in %s:%u", filename
, lineno
);
1209 else if (strstr(attr
, "../") != NULL
)
1210 log_error("do not reference parent sysfs directories directly, "
1211 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1212 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1216 if (streq(key
, "TAGS")) {
1217 if (op
> OP_MATCH_MAX
) {
1218 log_error("invalid TAGS operation");
1221 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1225 if (startswith(key
, "ENV{")) {
1226 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1228 log_error("error parsing ENV attribute");
1231 if (op
< OP_MATCH_MAX
) {
1232 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1235 static const char *blacklist
[] = {
1250 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1251 if (!streq(attr
, blacklist
[i
]))
1253 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1256 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1262 if (streq(key
, "TAG")) {
1263 if (op
< OP_MATCH_MAX
)
1264 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1266 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1270 if (streq(key
, "PROGRAM")) {
1271 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1275 if (streq(key
, "RESULT")) {
1276 if (op
> OP_MATCH_MAX
) {
1277 log_error("invalid RESULT operation");
1280 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1284 if (startswith(key
, "IMPORT")) {
1285 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1287 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1290 if (streq(attr
, "program")) {
1291 /* find known built-in command */
1292 if (value
[0] != '/') {
1293 enum udev_builtin_cmd cmd
;
1295 cmd
= udev_builtin_lookup(value
);
1296 if (cmd
< UDEV_BUILTIN_MAX
) {
1297 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1298 value
, filename
, lineno
);
1299 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1303 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1304 } else if (streq(attr
, "builtin")) {
1305 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1307 if (cmd
< UDEV_BUILTIN_MAX
)
1308 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1310 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1311 } else if (streq(attr
, "file")) {
1312 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1313 } else if (streq(attr
, "db")) {
1314 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1315 } else if (streq(attr
, "cmdline")) {
1316 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1317 } else if (streq(attr
, "parent")) {
1318 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1320 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1324 if (startswith(key
, "TEST")) {
1327 if (op
> OP_MATCH_MAX
) {
1328 log_error("invalid TEST operation");
1331 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1333 mode
= strtol(attr
, NULL
, 8);
1334 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1336 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1341 if (startswith(key
, "RUN")) {
1342 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1346 if (streq(attr
, "builtin")) {
1347 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1349 if (cmd
< UDEV_BUILTIN_MAX
)
1350 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1352 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1353 } else if (streq(attr
, "program")) {
1354 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1356 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1358 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1364 if (streq(key
, "WAIT_FOR") || streq(key
, "WAIT_FOR_SYSFS")) {
1365 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1369 if (streq(key
, "LABEL")) {
1370 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1374 if (streq(key
, "GOTO")) {
1375 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1379 if (startswith(key
, "NAME")) {
1380 if (op
< OP_MATCH_MAX
) {
1381 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1383 if (streq(value
, "%k")) {
1384 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1385 "please remove it from %s:%u\n", filename
, lineno
);
1388 if (value
[0] == '\0') {
1389 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1390 "please remove it from %s:%u\n", filename
, lineno
);
1393 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1395 rule_tmp
.rule
.rule
.can_set_name
= true;
1399 if (streq(key
, "SYMLINK")) {
1400 if (op
< OP_MATCH_MAX
)
1401 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1403 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1404 rule_tmp
.rule
.rule
.can_set_name
= true;
1408 if (streq(key
, "OWNER")) {
1412 uid
= strtoul(value
, &endptr
, 10);
1413 if (endptr
[0] == '\0') {
1414 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1415 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1416 uid
= add_uid(rules
, value
);
1417 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1418 } else if (rules
->resolve_names
>= 0) {
1419 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1421 rule_tmp
.rule
.rule
.can_set_name
= true;
1425 if (streq(key
, "GROUP")) {
1429 gid
= strtoul(value
, &endptr
, 10);
1430 if (endptr
[0] == '\0') {
1431 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1432 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1433 gid
= add_gid(rules
, value
);
1434 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1435 } else if (rules
->resolve_names
>= 0) {
1436 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1438 rule_tmp
.rule
.rule
.can_set_name
= true;
1442 if (streq(key
, "MODE")) {
1446 mode
= strtol(value
, &endptr
, 8);
1447 if (endptr
[0] == '\0')
1448 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1450 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1451 rule_tmp
.rule
.rule
.can_set_name
= true;
1455 if (streq(key
, "OPTIONS")) {
1458 pos
= strstr(value
, "link_priority=");
1460 int prio
= atoi(&pos
[strlen("link_priority=")]);
1462 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1465 pos
= strstr(value
, "event_timeout=");
1467 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1469 rule_add_key(&rule_tmp
, TK_M_EVENT_TIMEOUT
, op
, NULL
, &tout
);
1472 pos
= strstr(value
, "string_escape=");
1474 pos
= &pos
[strlen("string_escape=")];
1475 if (startswith(pos
, "none"))
1476 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1477 else if (startswith(pos
, "replace"))
1478 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1481 pos
= strstr(value
, "db_persist");
1483 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1485 pos
= strstr(value
, "nowatch");
1489 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1491 pos
= strstr(value
, "watch");
1495 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1499 pos
= strstr(value
, "static_node=");
1501 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1502 rule_tmp
.rule
.rule
.has_static_node
= true;
1508 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1512 /* add rule token */
1513 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1514 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1517 /* add tokens to list, sorted by type */
1518 if (sort_token(rules
, &rule_tmp
) != 0)
1523 log_error("invalid rule '%s:%u'", filename
, lineno
);
1527 static int parse_file(struct udev_rules
*rules
, const char *filename
)
1530 unsigned int first_token
;
1531 unsigned int filename_off
;
1532 char line
[UTIL_LINE_SIZE
];
1536 if (null_or_empty_path(filename
)) {
1537 log_debug("skip empty file: %s", filename
);
1540 log_debug("read rules file: %s", filename
);
1542 f
= fopen(filename
, "re");
1546 first_token
= rules
->token_cur
;
1547 filename_off
= rules_add_string(rules
, filename
);
1549 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1553 /* skip whitespace */
1556 while (isspace(key
[0]))
1567 /* continue reading if backslash+newline is found */
1568 while (line
[len
-2] == '\\') {
1569 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1571 if (strlen(&line
[len
-2]) < 2)
1577 if (len
+1 >= sizeof(line
)) {
1578 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1581 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1585 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1586 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1587 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1588 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1591 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1592 if (rules
->tokens
[j
].type
!= TK_RULE
)
1594 if (rules
->tokens
[j
].rule
.label_off
== 0)
1596 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1598 rules
->tokens
[i
].key
.rule_goto
= j
;
1601 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1602 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1608 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1610 struct udev_rules
*rules
;
1611 struct udev_list file_list
;
1612 struct token end_token
;
1616 rules
= new0(struct udev_rules
, 1);
1620 rules
->resolve_names
= resolve_names
;
1621 udev_list_init(udev
, &file_list
, true);
1623 /* init token array and string buffer */
1624 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1625 if (rules
->tokens
== NULL
)
1626 return udev_rules_unref(rules
);
1627 rules
->token_max
= PREALLOC_TOKEN
;
1629 rules
->strbuf
= strbuf_new();
1631 return udev_rules_unref(rules
);
1633 udev_rules_check_timestamp(rules
);
1635 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1637 log_error("failed to enumerate rules files: %s", strerror(-r
));
1638 return udev_rules_unref(rules
);
1642 * The offset value in the rules strct is limited; add all
1643 * rules file names to the beginning of the string buffer.
1645 STRV_FOREACH(f
, files
)
1646 rules_add_string(rules
, *f
);
1648 STRV_FOREACH(f
, files
)
1649 parse_file(rules
, *f
);
1653 memzero(&end_token
, sizeof(struct token
));
1654 end_token
.type
= TK_END
;
1655 add_token(rules
, &end_token
);
1656 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1657 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1659 /* cleanup temporary strbuf data */
1660 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1661 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1662 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1663 strbuf_complete(rules
->strbuf
);
1665 /* cleanup uid/gid cache */
1668 rules
->uids_cur
= 0;
1669 rules
->uids_max
= 0;
1672 rules
->gids_cur
= 0;
1673 rules
->gids_max
= 0;
1679 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
)
1683 free(rules
->tokens
);
1684 strbuf_cleanup(rules
->strbuf
);
1691 bool udev_rules_check_timestamp(struct udev_rules
*rules
)
1696 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1699 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1701 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1708 switch (token
->key
.glob
) {
1710 match
= (streq(key_value
, val
));
1713 match
= (fnmatch(key_value
, val
, 0) == 0);
1720 s
= rules_str(rules
, token
->key
.value_off
);
1725 next
= strchr(s
, '|');
1727 size_t matchlen
= (size_t)(next
- s
);
1729 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1733 match
= (streq(s
, val
));
1742 char value
[UTIL_PATH_SIZE
];
1744 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1746 while (key_value
!= NULL
) {
1747 pos
= strchr(key_value
, '|');
1752 match
= (fnmatch(key_value
, val
, 0) == 0);
1760 match
= (val
[0] != '\0');
1766 if (match
&& (token
->key
.op
== OP_MATCH
))
1768 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1773 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1776 char nbuf
[UTIL_NAME_SIZE
];
1778 char vbuf
[UTIL_NAME_SIZE
];
1781 name
= rules_str(rules
, cur
->key
.attr_off
);
1782 switch (cur
->key
.attrsubst
) {
1784 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1788 value
= udev_device_get_sysattr_value(dev
, name
);
1793 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1801 /* remove trailing whitespace, if not asked to match for it */
1802 len
= strlen(value
);
1803 if (len
> 0 && isspace(value
[len
-1])) {
1804 const char *key_value
;
1807 key_value
= rules_str(rules
, cur
->key
.value_off
);
1808 klen
= strlen(key_value
);
1809 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1810 if (value
!= vbuf
) {
1811 strscpy(vbuf
, sizeof(vbuf
), value
);
1814 while (len
> 0 && isspace(vbuf
[--len
]))
1819 return match_key(rules
, cur
, value
);
1828 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
, const sigset_t
*sigmask
)
1832 enum escape_type esc
= ESCAPE_UNSET
;
1835 if (rules
->tokens
== NULL
)
1838 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1839 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1840 udev_device_get_ifindex(event
->dev
) > 0));
1842 /* loop through token list, match, run actions or forward to next rule */
1843 cur
= &rules
->tokens
[0];
1846 dump_token(rules
, cur
);
1847 switch (cur
->type
) {
1851 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1852 if (!can_set_name
&& rule
->rule
.can_set_name
)
1857 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1861 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1865 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1868 case TK_M_DEVLINK
: {
1869 struct udev_list_entry
*list_entry
;
1872 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1873 const char *devlink
;
1875 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1876 if (match_key(rules
, cur
, devlink
) == 0) {
1886 if (match_key(rules
, cur
, event
->name
) != 0)
1890 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1893 value
= udev_device_get_property_value(event
->dev
, key_name
);
1896 if (match_key(rules
, cur
, value
))
1901 struct udev_list_entry
*list_entry
;
1904 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1905 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1910 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
1914 case TK_M_SUBSYSTEM
:
1915 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1919 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1922 case TK_M_WAITFOR
: {
1923 char filename
[UTIL_PATH_SIZE
];
1926 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
1927 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
1928 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
1933 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1937 case TK_M_SUBSYSTEMS
:
1943 /* get whole sequence of parent matches */
1945 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1948 /* loop over parents */
1949 event
->dev_parent
= event
->dev
;
1953 /* loop over sequence of parent match keys */
1954 for (key
= cur
; key
< next
; key
++ ) {
1955 dump_token(rules
, key
);
1958 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1961 case TK_M_SUBSYSTEMS
:
1962 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1966 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1970 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1974 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1976 if (match
&& key
->key
.op
== OP_NOMATCH
)
1978 if (!match
&& key
->key
.op
== OP_MATCH
)
1989 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1990 if (event
->dev_parent
== NULL
)
1993 /* move behind our sequence of parent match keys */
1998 char filename
[UTIL_PATH_SIZE
];
1999 struct stat statbuf
;
2002 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2003 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2004 if (filename
[0] != '/') {
2005 char tmp
[UTIL_PATH_SIZE
];
2007 strscpy(tmp
, sizeof(tmp
), filename
);
2008 strscpyl(filename
, sizeof(filename
),
2009 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2012 attr_subst_subdir(filename
, sizeof(filename
));
2014 match
= (stat(filename
, &statbuf
) == 0);
2015 if (match
&& cur
->key
.mode
> 0)
2016 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2017 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2019 if (!match
&& cur
->key
.op
== OP_MATCH
)
2023 case TK_M_EVENT_TIMEOUT
:
2024 log_debug("OPTIONS event_timeout=%u", cur
->key
.event_timeout
);
2025 event
->timeout_usec
= cur
->key
.event_timeout
* 1000 * 1000;
2027 case TK_M_PROGRAM
: {
2028 char program
[UTIL_PATH_SIZE
];
2030 char result
[UTIL_PATH_SIZE
];
2032 free(event
->program_result
);
2033 event
->program_result
= NULL
;
2034 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2035 envp
= udev_device_get_properties_envp(event
->dev
);
2036 log_debug("PROGRAM '%s' %s:%u",
2038 rules_str(rules
, rule
->rule
.filename_off
),
2039 rule
->rule
.filename_line
);
2041 if (udev_event_spawn(event
, program
, envp
, sigmask
, result
, sizeof(result
)) < 0) {
2042 if (cur
->key
.op
!= OP_NOMATCH
)
2047 util_remove_trailing_chars(result
, '\n');
2048 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2049 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2051 log_debug("%i character(s) replaced" , count
);
2053 event
->program_result
= strdup(result
);
2054 if (cur
->key
.op
== OP_NOMATCH
)
2059 case TK_M_IMPORT_FILE
: {
2060 char import
[UTIL_PATH_SIZE
];
2062 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2063 if (import_file_into_properties(event
->dev
, import
) != 0)
2064 if (cur
->key
.op
!= OP_NOMATCH
)
2068 case TK_M_IMPORT_PROG
: {
2069 char import
[UTIL_PATH_SIZE
];
2071 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2072 log_debug("IMPORT '%s' %s:%u",
2074 rules_str(rules
, rule
->rule
.filename_off
),
2075 rule
->rule
.filename_line
);
2077 if (import_program_into_properties(event
, import
, sigmask
) != 0)
2078 if (cur
->key
.op
!= OP_NOMATCH
)
2082 case TK_M_IMPORT_BUILTIN
: {
2083 char command
[UTIL_PATH_SIZE
];
2085 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2086 /* check if we ran already */
2087 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2088 log_debug("IMPORT builtin skip '%s' %s:%u",
2089 udev_builtin_name(cur
->key
.builtin_cmd
),
2090 rules_str(rules
, rule
->rule
.filename_off
),
2091 rule
->rule
.filename_line
);
2092 /* return the result from earlier run */
2093 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2094 if (cur
->key
.op
!= OP_NOMATCH
)
2099 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2102 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2103 log_debug("IMPORT builtin '%s' %s:%u",
2104 udev_builtin_name(cur
->key
.builtin_cmd
),
2105 rules_str(rules
, rule
->rule
.filename_off
),
2106 rule
->rule
.filename_line
);
2108 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2109 /* remember failure */
2110 log_debug("IMPORT builtin '%s' returned non-zero",
2111 udev_builtin_name(cur
->key
.builtin_cmd
));
2112 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2113 if (cur
->key
.op
!= OP_NOMATCH
)
2118 case TK_M_IMPORT_DB
: {
2119 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2122 value
= udev_device_get_property_value(event
->dev_db
, key
);
2123 if (value
!= NULL
) {
2124 struct udev_list_entry
*entry
;
2126 entry
= udev_device_add_property(event
->dev
, key
, value
);
2127 udev_list_entry_set_num(entry
, true);
2129 if (cur
->key
.op
!= OP_NOMATCH
)
2134 case TK_M_IMPORT_CMDLINE
: {
2136 bool imported
= false;
2138 f
= fopen("/proc/cmdline", "re");
2142 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2143 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2146 pos
= strstr(cmdline
, key
);
2148 struct udev_list_entry
*entry
;
2151 if (pos
[0] == '\0' || isspace(pos
[0])) {
2152 /* we import simple flags as 'FLAG=1' */
2153 entry
= udev_device_add_property(event
->dev
, key
, "1");
2154 udev_list_entry_set_num(entry
, true);
2156 } else if (pos
[0] == '=') {
2161 while (pos
[0] != '\0' && !isspace(pos
[0]))
2164 entry
= udev_device_add_property(event
->dev
, key
, value
);
2165 udev_list_entry_set_num(entry
, true);
2172 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2176 case TK_M_IMPORT_PARENT
: {
2177 char import
[UTIL_PATH_SIZE
];
2179 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2180 if (import_parent_into_properties(event
->dev
, import
) != 0)
2181 if (cur
->key
.op
!= OP_NOMATCH
)
2186 if (match_key(rules
, cur
, event
->program_result
) != 0)
2189 case TK_A_STRING_ESCAPE_NONE
:
2192 case TK_A_STRING_ESCAPE_REPLACE
:
2193 esc
= ESCAPE_REPLACE
;
2195 case TK_A_DB_PERSIST
:
2196 udev_device_set_db_persist(event
->dev
);
2198 case TK_A_INOTIFY_WATCH
:
2199 if (event
->inotify_watch_final
)
2201 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2202 event
->inotify_watch_final
= true;
2203 event
->inotify_watch
= cur
->key
.watch
;
2205 case TK_A_DEVLINK_PRIO
:
2206 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2209 char owner
[UTIL_NAME_SIZE
];
2211 if (event
->owner_final
)
2213 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2214 event
->owner_final
= true;
2215 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2216 event
->owner_set
= true;
2217 event
->uid
= util_lookup_user(event
->udev
, owner
);
2218 log_debug("OWNER %u %s:%u",
2220 rules_str(rules
, rule
->rule
.filename_off
),
2221 rule
->rule
.filename_line
);
2225 char group
[UTIL_NAME_SIZE
];
2227 if (event
->group_final
)
2229 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2230 event
->group_final
= true;
2231 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2232 event
->group_set
= true;
2233 event
->gid
= util_lookup_group(event
->udev
, group
);
2234 log_debug("GROUP %u %s:%u",
2236 rules_str(rules
, rule
->rule
.filename_off
),
2237 rule
->rule
.filename_line
);
2241 char mode_str
[UTIL_NAME_SIZE
];
2245 if (event
->mode_final
)
2247 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2248 mode
= strtol(mode_str
, &endptr
, 8);
2249 if (endptr
[0] != '\0') {
2250 log_error("ignoring invalid mode '%s'", mode_str
);
2253 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2254 event
->mode_final
= true;
2255 event
->mode_set
= true;
2257 log_debug("MODE %#o %s:%u",
2259 rules_str(rules
, rule
->rule
.filename_off
),
2260 rule
->rule
.filename_line
);
2264 if (event
->owner_final
)
2266 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2267 event
->owner_final
= true;
2268 event
->owner_set
= true;
2269 event
->uid
= cur
->key
.uid
;
2270 log_debug("OWNER %u %s:%u",
2272 rules_str(rules
, rule
->rule
.filename_off
),
2273 rule
->rule
.filename_line
);
2276 if (event
->group_final
)
2278 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2279 event
->group_final
= true;
2280 event
->group_set
= true;
2281 event
->gid
= cur
->key
.gid
;
2282 log_debug("GROUP %u %s:%u",
2284 rules_str(rules
, rule
->rule
.filename_off
),
2285 rule
->rule
.filename_line
);
2288 if (event
->mode_final
)
2290 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2291 event
->mode_final
= true;
2292 event
->mode_set
= true;
2293 event
->mode
= cur
->key
.mode
;
2294 log_debug("MODE %#o %s:%u",
2296 rules_str(rules
, rule
->rule
.filename_off
),
2297 rule
->rule
.filename_line
);
2299 case TK_A_SECLABEL
: {
2300 const char *name
, *label
;
2302 name
= rules_str(rules
, cur
->key
.attr_off
);
2303 label
= rules_str(rules
, cur
->key
.value_off
);
2304 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2305 udev_list_cleanup(&event
->seclabel_list
);
2306 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2307 log_debug("SECLABEL{%s}='%s' %s:%u",
2309 rules_str(rules
, rule
->rule
.filename_off
),
2310 rule
->rule
.filename_line
);
2314 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2315 char *value
= rules_str(rules
, cur
->key
.value_off
);
2316 char value_new
[UTIL_NAME_SIZE
];
2317 const char *value_old
= NULL
;
2318 struct udev_list_entry
*entry
;
2320 if (value
[0] == '\0') {
2321 if (cur
->key
.op
== OP_ADD
)
2323 udev_device_add_property(event
->dev
, name
, NULL
);
2327 if (cur
->key
.op
== OP_ADD
)
2328 value_old
= udev_device_get_property_value(event
->dev
, name
);
2330 char temp
[UTIL_NAME_SIZE
];
2332 /* append value separated by space */
2333 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2334 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2336 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2338 entry
= udev_device_add_property(event
->dev
, name
, value_new
);
2339 /* store in db, skip private keys */
2341 udev_list_entry_set_num(entry
, true);
2345 char tag
[UTIL_PATH_SIZE
];
2348 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2349 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2350 udev_device_cleanup_tags_list(event
->dev
);
2351 for (p
= tag
; *p
!= '\0'; p
++) {
2352 if ((*p
>= 'a' && *p
<= 'z') ||
2353 (*p
>= 'A' && *p
<= 'Z') ||
2354 (*p
>= '0' && *p
<= '9') ||
2355 *p
== '-' || *p
== '_')
2357 log_error("ignoring invalid tag name '%s'", tag
);
2360 udev_device_add_tag(event
->dev
, tag
);
2364 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2366 char name_str
[UTIL_PATH_SIZE
];
2369 if (event
->name_final
)
2371 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2372 event
->name_final
= true;
2373 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2374 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2375 count
= util_replace_chars(name_str
, "/");
2377 log_debug("%i character(s) replaced", count
);
2379 if (major(udev_device_get_devnum(event
->dev
)) &&
2380 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2381 log_error("NAME=\"%s\" ignored, kernel device nodes "
2382 "can not be renamed; please fix it in %s:%u\n", name
,
2383 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2387 event
->name
= strdup(name_str
);
2388 log_debug("NAME '%s' %s:%u",
2390 rules_str(rules
, rule
->rule
.filename_off
),
2391 rule
->rule
.filename_line
);
2394 case TK_A_DEVLINK
: {
2395 char temp
[UTIL_PATH_SIZE
];
2396 char filename
[UTIL_PATH_SIZE
];
2400 if (event
->devlink_final
)
2402 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2404 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2405 event
->devlink_final
= true;
2406 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2407 udev_device_cleanup_devlinks_list(event
->dev
);
2409 /* allow multiple symlinks separated by spaces */
2410 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2411 if (esc
== ESCAPE_UNSET
)
2412 count
= util_replace_chars(temp
, "/ ");
2413 else if (esc
== ESCAPE_REPLACE
)
2414 count
= util_replace_chars(temp
, "/");
2416 log_debug("%i character(s) replaced" , count
);
2418 while (isspace(pos
[0]))
2420 next
= strchr(pos
, ' ');
2421 while (next
!= NULL
) {
2423 log_debug("LINK '%s' %s:%u", pos
,
2424 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2425 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2426 udev_device_add_devlink(event
->dev
, filename
);
2427 while (isspace(next
[1]))
2430 next
= strchr(pos
, ' ');
2432 if (pos
[0] != '\0') {
2433 log_debug("LINK '%s' %s:%u", pos
,
2434 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2435 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2436 udev_device_add_devlink(event
->dev
, filename
);
2441 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2442 char attr
[UTIL_PATH_SIZE
];
2443 char value
[UTIL_NAME_SIZE
];
2446 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2447 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2448 attr_subst_subdir(attr
, sizeof(attr
));
2450 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2451 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2452 rules_str(rules
, rule
->rule
.filename_off
),
2453 rule
->rule
.filename_line
);
2454 f
= fopen(attr
, "we");
2456 if (fprintf(f
, "%s", value
) <= 0)
2457 log_error("error writing ATTR{%s}: %m", attr
);
2460 log_error("error opening ATTR{%s} for writing: %m", attr
);
2464 case TK_A_RUN_BUILTIN
:
2465 case TK_A_RUN_PROGRAM
: {
2466 struct udev_list_entry
*entry
;
2468 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2469 udev_list_cleanup(&event
->run_list
);
2470 log_debug("RUN '%s' %s:%u",
2471 rules_str(rules
, cur
->key
.value_off
),
2472 rules_str(rules
, rule
->rule
.filename_off
),
2473 rule
->rule
.filename_line
);
2474 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2475 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2479 if (cur
->key
.rule_goto
== 0)
2481 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2486 case TK_M_PARENTS_MIN
:
2487 case TK_M_PARENTS_MAX
:
2490 log_error("wrong type %u", cur
->type
);
2497 /* fast-forward to next rule */
2498 cur
= rule
+ rule
->rule
.token_count
;
2502 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
)
2509 _cleanup_strv_free_
char **tags
= NULL
;
2512 _cleanup_free_
char *path
= NULL
;
2515 if (rules
->tokens
== NULL
)
2518 cur
= &rules
->tokens
[0];
2521 switch (cur
->type
) {
2526 /* skip rules without a static_node tag */
2527 if (!rule
->rule
.has_static_node
)
2543 mode
= cur
->key
.mode
;
2546 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2551 case TK_A_STATIC_NODE
: {
2552 char device_node
[UTIL_PATH_SIZE
];
2553 char tags_dir
[UTIL_PATH_SIZE
];
2554 char tag_symlink
[UTIL_PATH_SIZE
];
2557 /* we assure, that the permissions tokens are sorted before the static token */
2558 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2561 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2563 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2565 STRV_FOREACH(t
, tags
) {
2566 _cleanup_free_
char *unescaped_filename
= NULL
;
2568 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2569 r
= mkdir_p(tags_dir
, 0755);
2571 log_error("failed to create %s: %s", tags_dir
, strerror(-r
));
2575 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2577 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2578 r
= symlink(device_node
, tag_symlink
);
2579 if (r
< 0 && errno
!= EEXIST
) {
2580 log_error("failed to create symlink %s -> %s: %m", tag_symlink
, device_node
);
2587 /* don't touch the permissions if only the tags were set */
2588 if (mode
== 0 && uid
== 0 && gid
== 0)
2591 if (stat(device_node
, &stats
) != 0)
2593 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2602 if (mode
!= (stats
.st_mode
& 01777)) {
2603 r
= chmod(device_node
, mode
);
2605 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2608 log_debug("chmod '%s' %#o", device_node
, mode
);
2611 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2612 r
= chown(device_node
, uid
, gid
);
2614 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2617 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2620 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2630 /* fast-forward to next rule */
2631 cur
= rule
+ rule
->rule
.token_count
;
2638 fchmod(fileno(f
), 0644);
2639 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2641 unlink("/run/udev/static_node-tags");