2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "alloc-util.h"
33 #include "conf-files.h"
37 #include "glob-util.h"
38 #include "path-util.h"
39 #include "stat-util.h"
40 #include "stdio-util.h"
42 #include "string-util.h"
44 #include "sysctl-util.h"
46 #include "user-util.h"
49 #define PREALLOC_TOKEN 2048
52 unsigned int name_off
;
59 static const char* const rules_dirs
[] = {
62 UDEVLIBEXECDIR
"/rules.d",
71 /* every key in the rules file becomes a token */
73 unsigned int token_cur
;
74 unsigned int token_max
;
76 /* all key strings are copied and de-duplicated in a single continuous string buffer */
77 struct strbuf
*strbuf
;
79 /* during rule parsing, uid/gid lookup results are cached */
81 unsigned int uids_cur
;
82 unsigned int uids_max
;
84 unsigned int gids_cur
;
85 unsigned int gids_max
;
88 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
89 return rules
->strbuf
->buf
+ off
;
92 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
93 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
96 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
110 enum string_glob_type
{
112 GL_PLAIN
, /* no special chars */
113 GL_GLOB
, /* shell globs ?,*,[] */
114 GL_SPLIT
, /* multi-value A|B */
115 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
116 GL_SOMETHING
, /* commonly used "?*" */
119 enum string_subst_type
{
126 /* tokens of a rule are sorted/handled in this order */
131 TK_M_ACTION
, /* val */
132 TK_M_DEVPATH
, /* val */
133 TK_M_KERNEL
, /* val */
134 TK_M_DEVLINK
, /* val */
136 TK_M_ENV
, /* val, attr */
138 TK_M_SUBSYSTEM
, /* val */
139 TK_M_DRIVER
, /* val */
140 TK_M_WAITFOR
, /* val */
141 TK_M_ATTR
, /* val, attr */
142 TK_M_SYSCTL
, /* val, attr */
145 TK_M_KERNELS
, /* val */
146 TK_M_SUBSYSTEMS
, /* val */
147 TK_M_DRIVERS
, /* val */
148 TK_M_ATTRS
, /* val, attr */
152 TK_M_TEST
, /* val, mode_t */
153 TK_M_PROGRAM
, /* val */
154 TK_M_IMPORT_FILE
, /* val */
155 TK_M_IMPORT_PROG
, /* val */
156 TK_M_IMPORT_BUILTIN
, /* val */
157 TK_M_IMPORT_DB
, /* val */
158 TK_M_IMPORT_CMDLINE
, /* val */
159 TK_M_IMPORT_PARENT
, /* val */
160 TK_M_RESULT
, /* val */
163 TK_A_STRING_ESCAPE_NONE
,
164 TK_A_STRING_ESCAPE_REPLACE
,
166 TK_A_INOTIFY_WATCH
, /* int */
167 TK_A_DEVLINK_PRIO
, /* int */
168 TK_A_OWNER
, /* val */
169 TK_A_GROUP
, /* val */
171 TK_A_OWNER_ID
, /* uid_t */
172 TK_A_GROUP_ID
, /* gid_t */
173 TK_A_MODE_ID
, /* mode_t */
175 TK_A_STATIC_NODE
, /* val */
176 TK_A_SECLABEL
, /* val, attr */
177 TK_A_ENV
, /* val, attr */
179 TK_A_DEVLINK
, /* val */
180 TK_A_ATTR
, /* val, attr */
181 TK_A_SYSCTL
, /* val, attr */
182 TK_A_RUN_BUILTIN
, /* val, bool */
183 TK_A_RUN_PROGRAM
, /* val, bool */
184 TK_A_GOTO
, /* size_t */
189 /* we try to pack stuff in a way that we take only 12 bytes per token */
192 unsigned char type
; /* same in rule and key */
194 enum token_type type
:8;
196 bool has_static_node
:1;
197 unsigned int unused
:6;
198 unsigned short token_count
;
199 unsigned int label_off
;
200 unsigned short filename_off
;
201 unsigned short filename_line
;
204 enum token_type type
:8;
205 enum operation_type op
:8;
206 enum string_glob_type glob
:8;
207 enum string_subst_type subst
:4;
208 enum string_subst_type attrsubst
:4;
209 unsigned int value_off
;
211 unsigned int attr_off
;
212 unsigned int rule_goto
;
218 enum udev_builtin_cmd builtin_cmd
;
226 struct udev_rules
*rules
;
228 struct token token
[MAX_TK
];
229 unsigned int token_cur
;
233 static const char *operation_str(enum operation_type type
) {
234 static const char *operation_strs
[] = {
235 [OP_UNSET
] = "UNSET",
236 [OP_MATCH
] = "match",
237 [OP_NOMATCH
] = "nomatch",
238 [OP_MATCH_MAX
] = "MATCH_MAX",
241 [OP_REMOVE
] = "remove",
242 [OP_ASSIGN
] = "assign",
243 [OP_ASSIGN_FINAL
] = "assign-final",
246 return operation_strs
[type
];
249 static const char *string_glob_str(enum string_glob_type type
) {
250 static const char *string_glob_strs
[] = {
251 [GL_UNSET
] = "UNSET",
252 [GL_PLAIN
] = "plain",
254 [GL_SPLIT
] = "split",
255 [GL_SPLIT_GLOB
] = "split-glob",
256 [GL_SOMETHING
] = "split-glob",
259 return string_glob_strs
[type
];
262 static const char *token_str(enum token_type type
) {
263 static const char *token_strs
[] = {
264 [TK_UNSET
] = "UNSET",
267 [TK_M_ACTION
] = "M ACTION",
268 [TK_M_DEVPATH
] = "M DEVPATH",
269 [TK_M_KERNEL
] = "M KERNEL",
270 [TK_M_DEVLINK
] = "M DEVLINK",
271 [TK_M_NAME
] = "M NAME",
272 [TK_M_ENV
] = "M ENV",
273 [TK_M_TAG
] = "M TAG",
274 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
275 [TK_M_DRIVER
] = "M DRIVER",
276 [TK_M_WAITFOR
] = "M WAITFOR",
277 [TK_M_ATTR
] = "M ATTR",
278 [TK_M_SYSCTL
] = "M SYSCTL",
280 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
281 [TK_M_KERNELS
] = "M KERNELS",
282 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
283 [TK_M_DRIVERS
] = "M DRIVERS",
284 [TK_M_ATTRS
] = "M ATTRS",
285 [TK_M_TAGS
] = "M TAGS",
286 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
288 [TK_M_TEST
] = "M TEST",
289 [TK_M_PROGRAM
] = "M PROGRAM",
290 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
291 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
292 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
293 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
294 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
295 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
296 [TK_M_RESULT
] = "M RESULT",
297 [TK_M_MAX
] = "M MAX",
299 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
300 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
301 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
302 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
303 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
304 [TK_A_OWNER
] = "A OWNER",
305 [TK_A_GROUP
] = "A GROUP",
306 [TK_A_MODE
] = "A MODE",
307 [TK_A_OWNER_ID
] = "A OWNER_ID",
308 [TK_A_GROUP_ID
] = "A GROUP_ID",
309 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
310 [TK_A_SECLABEL
] = "A SECLABEL",
311 [TK_A_MODE_ID
] = "A MODE_ID",
312 [TK_A_ENV
] = "A ENV",
313 [TK_A_TAG
] = "A ENV",
314 [TK_A_NAME
] = "A NAME",
315 [TK_A_DEVLINK
] = "A DEVLINK",
316 [TK_A_ATTR
] = "A ATTR",
317 [TK_A_SYSCTL
] = "A SYSCTL",
318 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
319 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
320 [TK_A_GOTO
] = "A GOTO",
325 return token_strs
[type
];
328 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
329 enum token_type type
= token
->type
;
330 enum operation_type op
= token
->key
.op
;
331 enum string_glob_type glob
= token
->key
.glob
;
332 const char *value
= str(rules
, token
->key
.value_off
);
333 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
338 const char *tks_ptr
= (char *)rules
->tokens
;
339 const char *tk_ptr
= (char *)token
;
340 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
342 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
343 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
344 idx
, token
->rule
.token_count
,
345 &rules
->buf
[token
->rule
.label_off
]);
357 case TK_M_SUBSYSTEMS
:
361 case TK_M_IMPORT_FILE
:
362 case TK_M_IMPORT_PROG
:
364 case TK_M_IMPORT_CMDLINE
:
365 case TK_M_IMPORT_PARENT
:
372 case TK_A_RUN_BUILTIN
:
373 case TK_A_RUN_PROGRAM
:
374 log_debug("%s %s '%s'(%s)",
375 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
377 case TK_M_IMPORT_BUILTIN
:
378 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
387 log_debug("%s %s '%s' '%s'(%s)",
388 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
392 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
394 case TK_A_STRING_ESCAPE_NONE
:
395 case TK_A_STRING_ESCAPE_REPLACE
:
396 case TK_A_DB_PERSIST
:
397 log_debug("%s", token_str(type
));
400 log_debug("%s %s '%s'(%s) %#o",
401 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
403 case TK_A_INOTIFY_WATCH
:
404 log_debug("%s %u", token_str(type
), token
->key
.watch
);
406 case TK_A_DEVLINK_PRIO
:
407 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
410 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
413 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
416 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
418 case TK_A_STATIC_NODE
:
419 log_debug("%s '%s'", token_str(type
), value
);
422 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
425 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
428 log_debug("* %s", token_str(type
));
430 case TK_M_PARENTS_MIN
:
431 case TK_M_PARENTS_MAX
:
434 log_debug("unknown type %u", type
);
439 static void dump_rules(struct udev_rules
*rules
) {
442 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
444 rules
->token_cur
* sizeof(struct token
),
447 for (i
= 0; i
< rules
->token_cur
; i
++)
448 dump_token(rules
, &rules
->tokens
[i
]);
451 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
452 static inline void dump_rules(struct udev_rules
*rules
) {}
455 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
456 /* grow buffer if needed */
457 if (rules
->token_cur
+1 >= rules
->token_max
) {
458 struct token
*tokens
;
461 /* double the buffer size */
462 add
= rules
->token_max
;
466 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
469 rules
->tokens
= tokens
;
470 rules
->token_max
+= add
;
472 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
477 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
483 /* lookup, if we know it already */
484 for (i
= 0; i
< rules
->uids_cur
; i
++) {
485 off
= rules
->uids
[i
].name_off
;
486 if (streq(rules_str(rules
, off
), owner
)) {
487 uid
= rules
->uids
[i
].uid
;
491 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
493 if (r
== -ENOENT
|| r
== -ESRCH
)
494 log_error("specified user '%s' unknown", owner
);
496 log_error_errno(r
, "error resolving user '%s': %m", owner
);
499 /* grow buffer if needed */
500 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
501 struct uid_gid
*uids
;
504 /* double the buffer size */
505 add
= rules
->uids_max
;
509 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
513 rules
->uids_max
+= add
;
515 rules
->uids
[rules
->uids_cur
].uid
= uid
;
516 off
= rules_add_string(rules
, owner
);
519 rules
->uids
[rules
->uids_cur
].name_off
= off
;
524 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
530 /* lookup, if we know it already */
531 for (i
= 0; i
< rules
->gids_cur
; i
++) {
532 off
= rules
->gids
[i
].name_off
;
533 if (streq(rules_str(rules
, off
), group
)) {
534 gid
= rules
->gids
[i
].gid
;
538 r
= get_group_creds(&group
, &gid
);
540 if (r
== -ENOENT
|| r
== -ESRCH
)
541 log_error("specified group '%s' unknown", group
);
543 log_error_errno(r
, "error resolving group '%s': %m", group
);
546 /* grow buffer if needed */
547 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
548 struct uid_gid
*gids
;
551 /* double the buffer size */
552 add
= rules
->gids_max
;
556 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
560 rules
->gids_max
+= add
;
562 rules
->gids
[rules
->gids_cur
].gid
= gid
;
563 off
= rules_add_string(rules
, group
);
566 rules
->gids
[rules
->gids_cur
].name_off
= off
;
571 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
578 while (isspace(key
[0]))
581 /* comment or empty line */
582 if (key
[0] == '#' || key
[0] == '\0')
585 /* split key/value */
586 val
= strchr(key
, '=');
593 while (isspace(val
[0]))
600 while (isspace(key
[len
-1]))
604 /* terminate value */
608 while (isspace(val
[len
-1]))
616 if (val
[0] == '"' || val
[0] == '\'') {
617 if (val
[len
-1] != val
[0]) {
618 log_debug("inconsistent quoting: '%s', skip", line
);
625 udev_device_add_property(dev
, key
, val
);
630 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
) {
632 char line
[UTIL_LINE_SIZE
];
634 f
= fopen(filename
, "re");
637 while (fgets(line
, sizeof(line
), f
) != NULL
)
638 import_property_from_string(dev
, line
);
643 static int import_program_into_properties(struct udev_event
*event
,
645 usec_t timeout_warn_usec
,
646 const char *program
) {
647 char result
[UTIL_LINE_SIZE
];
651 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
));
656 while (line
!= NULL
) {
659 pos
= strchr(line
, '\n');
664 import_property_from_string(event
->dev
, line
);
670 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
671 struct udev_device
*dev_parent
;
672 struct udev_list_entry
*list_entry
;
677 dev_parent
= udev_device_get_parent(dev
);
678 if (dev_parent
== NULL
)
681 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
682 const char *key
= udev_list_entry_get_name(list_entry
);
683 const char *val
= udev_list_entry_get_value(list_entry
);
685 if (fnmatch(filter
, key
, 0) == 0)
686 udev_device_add_property(dev
, key
, val
);
691 static void attr_subst_subdir(char *attr
, size_t len
) {
692 const char *pos
, *tail
, *path
;
693 _cleanup_closedir_
DIR *dir
= NULL
;
696 pos
= strstr(attr
, "/*/");
701 path
= strndupa(attr
, pos
- attr
+ 1); /* include slash at end */
706 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
))
707 if (dent
->d_name
[0] != '.') {
708 char n
[strlen(dent
->d_name
) + 1 + strlen(tail
) + 1];
710 strscpyl(n
, sizeof n
, dent
->d_name
, "/", tail
, NULL
);
711 if (faccessat(dirfd(dir
), n
, F_OK
, 0)) {
712 strscpyl(attr
, len
, path
, n
, NULL
);
718 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
723 if (linepos
== NULL
|| linepos
[0] == '\0')
726 /* skip whitespace */
727 while (isspace(linepos
[0]) || linepos
[0] == ',')
731 if (linepos
[0] == '\0')
737 if (linepos
[0] == '\0')
739 if (isspace(linepos
[0]))
741 if (linepos
[0] == '=')
743 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
744 if (linepos
[1] == '=')
748 /* remember end of key */
751 /* skip whitespace after key */
752 while (isspace(linepos
[0]))
754 if (linepos
[0] == '\0')
757 /* get operation type */
758 if (linepos
[0] == '=' && linepos
[1] == '=') {
761 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
764 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
767 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
770 } else if (linepos
[0] == '=') {
773 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
774 *op
= OP_ASSIGN_FINAL
;
782 /* skip whitespace after operator */
783 while (isspace(linepos
[0]))
785 if (linepos
[0] == '\0')
789 if (linepos
[0] == '"')
796 temp
= strchr(linepos
, '"');
802 /* move line to next key */
807 /* extract possible KEY{attr} */
808 static const char *get_key_attribute(struct udev
*udev
, char *str
) {
812 attr
= strchr(str
, '{');
815 pos
= strchr(attr
, '}');
817 log_error("missing closing brace for format");
826 static void rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
827 enum operation_type op
,
828 const char *value
, const void *data
) {
829 struct token
*token
= rule_tmp
->token
+ rule_tmp
->token_cur
;
830 const char *attr
= NULL
;
832 assert(rule_tmp
->token_cur
< ELEMENTSOF(rule_tmp
->token
));
833 memzero(token
, sizeof(struct token
));
845 case TK_M_SUBSYSTEMS
:
849 case TK_M_IMPORT_FILE
:
850 case TK_M_IMPORT_PROG
:
852 case TK_M_IMPORT_CMDLINE
:
853 case TK_M_IMPORT_PARENT
:
863 case TK_A_STATIC_NODE
:
864 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
866 case TK_M_IMPORT_BUILTIN
:
867 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
868 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
879 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
880 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
883 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
885 token
->key
.mode
= *(mode_t
*)data
;
887 case TK_A_STRING_ESCAPE_NONE
:
888 case TK_A_STRING_ESCAPE_REPLACE
:
889 case TK_A_DB_PERSIST
:
891 case TK_A_RUN_BUILTIN
:
892 case TK_A_RUN_PROGRAM
:
893 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
894 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
896 case TK_A_INOTIFY_WATCH
:
897 case TK_A_DEVLINK_PRIO
:
898 token
->key
.devlink_prio
= *(int *)data
;
901 token
->key
.uid
= *(uid_t
*)data
;
904 token
->key
.gid
= *(gid_t
*)data
;
907 token
->key
.mode
= *(mode_t
*)data
;
910 case TK_M_PARENTS_MIN
:
911 case TK_M_PARENTS_MAX
:
915 assert_not_reached("wrong type");
918 if (value
!= NULL
&& type
< TK_M_MAX
) {
919 /* check if we need to split or call fnmatch() while matching rules */
920 enum string_glob_type glob
;
924 has_split
= (strchr(value
, '|') != NULL
);
925 has_glob
= string_is_glob(value
);
926 if (has_split
&& has_glob
) {
927 glob
= GL_SPLIT_GLOB
;
928 } else if (has_split
) {
930 } else if (has_glob
) {
931 if (streq(value
, "?*"))
938 token
->key
.glob
= glob
;
941 if (value
!= NULL
&& type
> TK_M_MAX
) {
942 /* check if assigned value has substitution chars */
944 token
->key
.subst
= SB_SUBSYS
;
945 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
946 token
->key
.subst
= SB_FORMAT
;
948 token
->key
.subst
= SB_NONE
;
952 /* check if property/attribute name has substitution chars */
954 token
->key
.attrsubst
= SB_SUBSYS
;
955 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
956 token
->key
.attrsubst
= SB_FORMAT
;
958 token
->key
.attrsubst
= SB_NONE
;
961 token
->key
.type
= type
;
963 rule_tmp
->token_cur
++;
966 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
968 unsigned int start
= 0;
969 unsigned int end
= rule_tmp
->token_cur
;
971 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
972 enum token_type next_val
= TK_UNSET
;
973 unsigned int next_idx
= 0;
976 /* find smallest value */
977 for (j
= start
; j
< end
; j
++) {
978 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
980 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
981 next_val
= rule_tmp
->token
[j
].type
;
986 /* add token and mark done */
987 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
989 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
992 if (next_idx
== start
)
994 if (next_idx
+1 == end
)
1000 #define LOG_RULE_ERROR(fmt, ...) log_error("Invalid rule %s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1001 #define LOG_RULE_WARNING(fmt, ...) log_warning("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1002 #define LOG_RULE_DEBUG(fmt, ...) log_debug("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1003 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1005 static void add_rule(struct udev_rules
*rules
, char *line
,
1006 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1009 struct rule_tmp rule_tmp
= {
1011 .rule
.type
= TK_RULE
,
1014 /* the offset in the rule is limited to unsigned short */
1015 if (filename_off
< USHRT_MAX
)
1016 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1017 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1023 enum operation_type op
;
1025 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1026 /* Avoid erroring on trailing whitespace. This is probably rare
1027 * so save the work for the error case instead of always trying
1028 * to strip the trailing whitespace with strstrip(). */
1029 while (isblank(*linepos
))
1032 /* If we aren't at the end of the line, this is a parsing error.
1033 * Make a best effort to describe where the problem is. */
1034 if (!strchr(NEWLINE
, *linepos
)) {
1035 char buf
[2] = {*linepos
};
1036 _cleanup_free_
char *tmp
;
1039 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1040 filename
, lineno
, linepos
- line
+ 1, tmp
);
1041 if (*linepos
== '#')
1042 log_error("hint: comments can only start at beginning of line");
1047 if (rule_tmp
.token_cur
>= ELEMENTSOF(rule_tmp
.token
))
1048 LOG_AND_RETURN("temporary rule array too small, aborting event processing with %u items", rule_tmp
.token_cur
);
1050 if (streq(key
, "ACTION")) {
1051 if (op
> OP_MATCH_MAX
)
1052 LOG_AND_RETURN("invalid %s operation", key
);
1054 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1056 } else if (streq(key
, "DEVPATH")) {
1057 if (op
> OP_MATCH_MAX
)
1058 LOG_AND_RETURN("invalid %s operation", key
);
1060 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1062 } else if (streq(key
, "KERNEL")) {
1063 if (op
> OP_MATCH_MAX
)
1064 LOG_AND_RETURN("invalid %s operation", key
);
1066 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1068 } else if (streq(key
, "SUBSYSTEM")) {
1069 if (op
> OP_MATCH_MAX
)
1070 LOG_AND_RETURN("invalid %s operation", key
);
1072 /* bus, class, subsystem events should all be the same */
1073 if (STR_IN_SET(value
, "subsystem", "bus", "class")) {
1074 if (!streq(value
, "subsystem"))
1075 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value
);
1077 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1079 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1081 } else if (streq(key
, "DRIVER")) {
1082 if (op
> OP_MATCH_MAX
)
1083 LOG_AND_RETURN("invalid %s operation", key
);
1085 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1087 } else if (startswith(key
, "ATTR{")) {
1088 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1090 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1092 if (op
== OP_REMOVE
)
1093 LOG_AND_RETURN("invalid %s operation", "ATTR");
1095 if (op
< OP_MATCH_MAX
)
1096 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1098 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1100 } else if (startswith(key
, "SYSCTL{")) {
1101 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SYSCTL"));
1103 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1105 if (op
== OP_REMOVE
)
1106 LOG_AND_RETURN("invalid %s operation", "ATTR");
1108 if (op
< OP_MATCH_MAX
)
1109 rule_add_key(&rule_tmp
, TK_M_SYSCTL
, op
, value
, attr
);
1111 rule_add_key(&rule_tmp
, TK_A_SYSCTL
, op
, value
, attr
);
1113 } else if (startswith(key
, "SECLABEL{")) {
1114 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1116 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1118 if (op
== OP_REMOVE
)
1119 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1121 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1123 } else if (streq(key
, "KERNELS")) {
1124 if (op
> OP_MATCH_MAX
)
1125 LOG_AND_RETURN("invalid %s operation", key
);
1127 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1129 } else if (streq(key
, "SUBSYSTEMS")) {
1130 if (op
> OP_MATCH_MAX
)
1131 LOG_AND_RETURN("invalid %s operation", key
);
1133 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1135 } else if (streq(key
, "DRIVERS")) {
1136 if (op
> OP_MATCH_MAX
)
1137 LOG_AND_RETURN("invalid %s operation", key
);
1139 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1141 } else if (startswith(key
, "ATTRS{")) {
1142 if (op
> OP_MATCH_MAX
)
1143 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1145 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1147 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1149 if (startswith(attr
, "device/"))
1150 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1151 if (strstr(attr
, "../") != NULL
)
1152 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1153 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1155 } else if (streq(key
, "TAGS")) {
1156 if (op
> OP_MATCH_MAX
)
1157 LOG_AND_RETURN("invalid %s operation", key
);
1159 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1161 } else if (startswith(key
, "ENV{")) {
1162 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1164 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1166 if (op
== OP_REMOVE
)
1167 LOG_AND_RETURN("invalid %s operation", "ENV");
1169 if (op
< OP_MATCH_MAX
)
1170 rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
);
1172 if (STR_IN_SET(attr
,
1184 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr
);
1186 rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
);
1189 } else if (streq(key
, "TAG")) {
1190 if (op
< OP_MATCH_MAX
)
1191 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1193 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1195 } else if (streq(key
, "PROGRAM")) {
1196 if (op
== OP_REMOVE
)
1197 LOG_AND_RETURN("invalid %s operation", key
);
1199 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1201 } else if (streq(key
, "RESULT")) {
1202 if (op
> OP_MATCH_MAX
)
1203 LOG_AND_RETURN("invalid %s operation", key
);
1205 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1207 } else if (startswith(key
, "IMPORT")) {
1208 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1210 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1213 if (op
== OP_REMOVE
)
1214 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1216 if (streq(attr
, "program")) {
1217 /* find known built-in command */
1218 if (value
[0] != '/') {
1219 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1221 if (cmd
< UDEV_BUILTIN_MAX
) {
1222 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value
);
1223 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1227 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1228 } else if (streq(attr
, "builtin")) {
1229 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1231 if (cmd
>= UDEV_BUILTIN_MAX
)
1232 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value
);
1234 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1235 } else if (streq(attr
, "file"))
1236 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1237 else if (streq(attr
, "db"))
1238 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1239 else if (streq(attr
, "cmdline"))
1240 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1241 else if (streq(attr
, "parent"))
1242 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1244 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr
);
1246 } else if (startswith(key
, "TEST")) {
1249 if (op
> OP_MATCH_MAX
)
1250 LOG_AND_RETURN("invalid %s operation", "TEST");
1252 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1254 mode
= strtol(attr
, NULL
, 8);
1255 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1257 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1259 } else if (startswith(key
, "RUN")) {
1260 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1263 if (op
== OP_REMOVE
)
1264 LOG_AND_RETURN("invalid %s operation", "RUN");
1266 if (streq(attr
, "builtin")) {
1267 const enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1269 if (cmd
< UDEV_BUILTIN_MAX
)
1270 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1272 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value
);
1273 } else if (streq(attr
, "program")) {
1274 const enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1276 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1278 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr
);
1280 } else if (streq(key
, "LABEL")) {
1281 if (op
== OP_REMOVE
)
1282 LOG_AND_RETURN("invalid %s operation", key
);
1284 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1286 } else if (streq(key
, "GOTO")) {
1287 if (op
== OP_REMOVE
)
1288 LOG_AND_RETURN("invalid %s operation", key
);
1290 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1292 } else if (startswith(key
, "NAME")) {
1293 if (op
== OP_REMOVE
)
1294 LOG_AND_RETURN("invalid %s operation", key
);
1296 if (op
< OP_MATCH_MAX
)
1297 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1299 if (streq(value
, "%k")) {
1300 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1303 if (isempty(value
)) {
1304 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1307 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1309 rule_tmp
.rule
.rule
.can_set_name
= true;
1311 } else if (streq(key
, "SYMLINK")) {
1312 if (op
== OP_REMOVE
)
1313 LOG_AND_RETURN("invalid %s operation", key
);
1315 if (op
< OP_MATCH_MAX
)
1316 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1318 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1319 rule_tmp
.rule
.rule
.can_set_name
= true;
1321 } else if (streq(key
, "OWNER")) {
1325 if (op
== OP_REMOVE
)
1326 LOG_AND_RETURN("invalid %s operation", key
);
1328 uid
= strtoul(value
, &endptr
, 10);
1329 if (endptr
[0] == '\0')
1330 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1331 else if (rules
->resolve_names
> 0 && strchr("$%", value
[0]) == NULL
) {
1332 uid
= add_uid(rules
, value
);
1333 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1334 } else if (rules
->resolve_names
>= 0)
1335 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1337 rule_tmp
.rule
.rule
.can_set_name
= true;
1339 } else if (streq(key
, "GROUP")) {
1343 if (op
== OP_REMOVE
)
1344 LOG_AND_RETURN("invalid %s operation", key
);
1346 gid
= strtoul(value
, &endptr
, 10);
1347 if (endptr
[0] == '\0')
1348 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1349 else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1350 gid
= add_gid(rules
, value
);
1351 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1352 } else if (rules
->resolve_names
>= 0)
1353 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1355 rule_tmp
.rule
.rule
.can_set_name
= true;
1357 } else if (streq(key
, "MODE")) {
1361 if (op
== OP_REMOVE
)
1362 LOG_AND_RETURN("invalid %s operation", key
);
1364 mode
= strtol(value
, &endptr
, 8);
1365 if (endptr
[0] == '\0')
1366 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1368 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1369 rule_tmp
.rule
.rule
.can_set_name
= true;
1371 } else if (streq(key
, "OPTIONS")) {
1374 if (op
== OP_REMOVE
)
1375 LOG_AND_RETURN("invalid %s operation", key
);
1377 pos
= strstr(value
, "link_priority=");
1379 int prio
= atoi(pos
+ strlen("link_priority="));
1381 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1384 pos
= strstr(value
, "string_escape=");
1386 pos
+= strlen("string_escape=");
1387 if (startswith(pos
, "none"))
1388 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1389 else if (startswith(pos
, "replace"))
1390 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1393 pos
= strstr(value
, "db_persist");
1395 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1397 pos
= strstr(value
, "nowatch");
1401 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1403 pos
= strstr(value
, "watch");
1407 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1411 pos
= strstr(value
, "static_node=");
1413 pos
+= strlen("static_node=");
1414 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, pos
, NULL
);
1415 rule_tmp
.rule
.rule
.has_static_node
= true;
1419 LOG_AND_RETURN("unknown key '%s'", key
);
1422 /* add rule token and sort tokens */
1423 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1424 if (add_token(rules
, &rule_tmp
.rule
) != 0 || sort_token(rules
, &rule_tmp
) != 0)
1425 LOG_RULE_ERROR("failed to add rule token");
1428 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1429 _cleanup_fclose_
FILE *f
= NULL
;
1430 unsigned int first_token
;
1431 unsigned int filename_off
;
1432 char line
[UTIL_LINE_SIZE
];
1436 f
= fopen(filename
, "re");
1438 if (errno
== ENOENT
)
1444 if (null_or_empty_fd(fileno(f
))) {
1445 log_debug("Skipping empty file: %s", filename
);
1448 log_debug("Reading rules file: %s", filename
);
1450 first_token
= rules
->token_cur
;
1451 filename_off
= rules_add_string(rules
, filename
);
1453 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1457 /* skip whitespace */
1460 while (isspace(key
[0]))
1471 /* continue reading if backslash+newline is found */
1472 while (line
[len
-2] == '\\') {
1473 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1475 if (strlen(&line
[len
-2]) < 2)
1481 if (len
+1 >= sizeof(line
)) {
1482 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1485 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1488 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1489 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1490 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1491 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1494 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1495 if (rules
->tokens
[j
].type
!= TK_RULE
)
1497 if (rules
->tokens
[j
].rule
.label_off
== 0)
1499 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1501 rules
->tokens
[i
].key
.rule_goto
= j
;
1504 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1505 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1511 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1512 struct udev_rules
*rules
;
1513 struct udev_list file_list
;
1514 struct token end_token
;
1518 rules
= new0(struct udev_rules
, 1);
1522 rules
->resolve_names
= resolve_names
;
1523 udev_list_init(udev
, &file_list
, true);
1525 /* init token array and string buffer */
1526 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1527 if (rules
->tokens
== NULL
)
1528 return udev_rules_unref(rules
);
1529 rules
->token_max
= PREALLOC_TOKEN
;
1531 rules
->strbuf
= strbuf_new();
1533 return udev_rules_unref(rules
);
1535 udev_rules_check_timestamp(rules
);
1537 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1539 log_error_errno(r
, "failed to enumerate rules files: %m");
1540 return udev_rules_unref(rules
);
1544 * The offset value in the rules strct is limited; add all
1545 * rules file names to the beginning of the string buffer.
1547 STRV_FOREACH(f
, files
)
1548 rules_add_string(rules
, *f
);
1550 STRV_FOREACH(f
, files
)
1551 parse_file(rules
, *f
);
1555 memzero(&end_token
, sizeof(struct token
));
1556 end_token
.type
= TK_END
;
1557 add_token(rules
, &end_token
);
1558 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1559 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1561 /* cleanup temporary strbuf data */
1562 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1563 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1564 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1565 strbuf_complete(rules
->strbuf
);
1567 /* cleanup uid/gid cache */
1568 rules
->uids
= mfree(rules
->uids
);
1569 rules
->uids_cur
= 0;
1570 rules
->uids_max
= 0;
1571 rules
->gids
= mfree(rules
->gids
);
1572 rules
->gids_cur
= 0;
1573 rules
->gids_max
= 0;
1579 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1582 free(rules
->tokens
);
1583 strbuf_cleanup(rules
->strbuf
);
1590 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1594 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1597 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1598 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1605 switch (token
->key
.glob
) {
1607 match
= (streq(key_value
, val
));
1610 match
= (fnmatch(key_value
, val
, 0) == 0);
1617 s
= rules_str(rules
, token
->key
.value_off
);
1622 next
= strchr(s
, '|');
1624 size_t matchlen
= (size_t)(next
- s
);
1626 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1630 match
= (streq(s
, val
));
1639 char value
[UTIL_PATH_SIZE
];
1641 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1643 while (key_value
!= NULL
) {
1644 pos
= strchr(key_value
, '|');
1649 match
= (fnmatch(key_value
, val
, 0) == 0);
1657 match
= (val
[0] != '\0');
1663 if (match
&& (token
->key
.op
== OP_MATCH
))
1665 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1670 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1672 char nbuf
[UTIL_NAME_SIZE
];
1674 char vbuf
[UTIL_NAME_SIZE
];
1677 name
= rules_str(rules
, cur
->key
.attr_off
);
1678 switch (cur
->key
.attrsubst
) {
1680 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1684 value
= udev_device_get_sysattr_value(dev
, name
);
1689 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1697 /* remove trailing whitespace, if not asked to match for it */
1698 len
= strlen(value
);
1699 if (len
> 0 && isspace(value
[len
-1])) {
1700 const char *key_value
;
1703 key_value
= rules_str(rules
, cur
->key
.value_off
);
1704 klen
= strlen(key_value
);
1705 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1706 if (value
!= vbuf
) {
1707 strscpy(vbuf
, sizeof(vbuf
), value
);
1710 while (len
> 0 && isspace(vbuf
[--len
]))
1715 return match_key(rules
, cur
, value
);
1724 void udev_rules_apply_to_event(struct udev_rules
*rules
,
1725 struct udev_event
*event
,
1726 usec_t timeout_usec
,
1727 usec_t timeout_warn_usec
,
1728 struct udev_list
*properties_list
) {
1731 enum escape_type esc
= ESCAPE_UNSET
;
1734 if (rules
->tokens
== NULL
)
1737 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1738 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1739 udev_device_get_ifindex(event
->dev
) > 0));
1741 /* loop through token list, match, run actions or forward to next rule */
1742 cur
= &rules
->tokens
[0];
1745 dump_token(rules
, cur
);
1746 switch (cur
->type
) {
1750 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1751 if (!can_set_name
&& rule
->rule
.can_set_name
)
1756 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1760 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1764 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1767 case TK_M_DEVLINK
: {
1768 struct udev_list_entry
*list_entry
;
1771 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1772 const char *devlink
;
1774 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1775 if (match_key(rules
, cur
, devlink
) == 0) {
1785 if (match_key(rules
, cur
, event
->name
) != 0)
1789 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1792 value
= udev_device_get_property_value(event
->dev
, key_name
);
1794 /* check global properties */
1795 if (!value
&& properties_list
) {
1796 struct udev_list_entry
*list_entry
;
1798 list_entry
= udev_list_get_entry(properties_list
);
1799 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1800 if (list_entry
!= NULL
)
1801 value
= udev_list_entry_get_value(list_entry
);
1806 if (match_key(rules
, cur
, value
))
1811 struct udev_list_entry
*list_entry
;
1814 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1815 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1820 if ((!match
&& (cur
->key
.op
!= OP_NOMATCH
)) ||
1821 (match
&& (cur
->key
.op
== OP_NOMATCH
)))
1825 case TK_M_SUBSYSTEM
:
1826 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1830 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1834 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1838 char filename
[UTIL_PATH_SIZE
];
1839 _cleanup_free_
char *value
= NULL
;
1842 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
1843 sysctl_normalize(filename
);
1844 if (sysctl_read(filename
, &value
) < 0)
1847 len
= strlen(value
);
1848 while (len
> 0 && isspace(value
[--len
]))
1850 if (match_key(rules
, cur
, value
) != 0)
1855 case TK_M_SUBSYSTEMS
:
1861 /* get whole sequence of parent matches */
1863 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1866 /* loop over parents */
1867 event
->dev_parent
= event
->dev
;
1871 /* loop over sequence of parent match keys */
1872 for (key
= cur
; key
< next
; key
++ ) {
1873 dump_token(rules
, key
);
1876 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1879 case TK_M_SUBSYSTEMS
:
1880 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1884 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1888 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1892 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1894 if (match
&& key
->key
.op
== OP_NOMATCH
)
1896 if (!match
&& key
->key
.op
== OP_MATCH
)
1907 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1908 if (event
->dev_parent
== NULL
)
1911 /* move behind our sequence of parent match keys */
1916 char filename
[UTIL_PATH_SIZE
];
1917 struct stat statbuf
;
1920 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
1921 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
1922 if (filename
[0] != '/') {
1923 char tmp
[UTIL_PATH_SIZE
];
1925 strscpy(tmp
, sizeof(tmp
), filename
);
1926 strscpyl(filename
, sizeof(filename
),
1927 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
1930 attr_subst_subdir(filename
, sizeof(filename
));
1932 match
= (stat(filename
, &statbuf
) == 0);
1933 if (match
&& cur
->key
.mode
> 0)
1934 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
1935 if (match
&& cur
->key
.op
== OP_NOMATCH
)
1937 if (!match
&& cur
->key
.op
== OP_MATCH
)
1941 case TK_M_PROGRAM
: {
1942 char program
[UTIL_PATH_SIZE
];
1943 char result
[UTIL_LINE_SIZE
];
1945 event
->program_result
= mfree(event
->program_result
);
1946 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
1947 log_debug("PROGRAM '%s' %s:%u",
1949 rules_str(rules
, rule
->rule
.filename_off
),
1950 rule
->rule
.filename_line
);
1952 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, true, program
, result
, sizeof(result
)) < 0) {
1953 if (cur
->key
.op
!= OP_NOMATCH
)
1958 util_remove_trailing_chars(result
, '\n');
1959 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
1960 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
1962 log_debug("%i character(s) replaced" , count
);
1964 event
->program_result
= strdup(result
);
1965 if (cur
->key
.op
== OP_NOMATCH
)
1970 case TK_M_IMPORT_FILE
: {
1971 char import
[UTIL_PATH_SIZE
];
1973 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
1974 if (import_file_into_properties(event
->dev
, import
) != 0)
1975 if (cur
->key
.op
!= OP_NOMATCH
)
1979 case TK_M_IMPORT_PROG
: {
1980 char import
[UTIL_PATH_SIZE
];
1982 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
1983 log_debug("IMPORT '%s' %s:%u",
1985 rules_str(rules
, rule
->rule
.filename_off
),
1986 rule
->rule
.filename_line
);
1988 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
) != 0)
1989 if (cur
->key
.op
!= OP_NOMATCH
)
1993 case TK_M_IMPORT_BUILTIN
: {
1994 char command
[UTIL_PATH_SIZE
];
1996 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
1997 /* check if we ran already */
1998 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
1999 log_debug("IMPORT builtin skip '%s' %s:%u",
2000 udev_builtin_name(cur
->key
.builtin_cmd
),
2001 rules_str(rules
, rule
->rule
.filename_off
),
2002 rule
->rule
.filename_line
);
2003 /* return the result from earlier run */
2004 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2005 if (cur
->key
.op
!= OP_NOMATCH
)
2010 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2013 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2014 log_debug("IMPORT builtin '%s' %s:%u",
2015 udev_builtin_name(cur
->key
.builtin_cmd
),
2016 rules_str(rules
, rule
->rule
.filename_off
),
2017 rule
->rule
.filename_line
);
2019 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2020 /* remember failure */
2021 log_debug("IMPORT builtin '%s' returned non-zero",
2022 udev_builtin_name(cur
->key
.builtin_cmd
));
2023 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2024 if (cur
->key
.op
!= OP_NOMATCH
)
2029 case TK_M_IMPORT_DB
: {
2030 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2033 value
= udev_device_get_property_value(event
->dev_db
, key
);
2035 udev_device_add_property(event
->dev
, key
, value
);
2037 if (cur
->key
.op
!= OP_NOMATCH
)
2042 case TK_M_IMPORT_CMDLINE
: {
2043 _cleanup_fclose_
FILE *f
= NULL
;
2044 bool imported
= false;
2046 f
= fopen("/proc/cmdline", "re");
2050 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2051 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2054 pos
= strstr(cmdline
, key
);
2058 if (pos
[0] == '\0' || isspace(pos
[0]))
2059 /* we import simple flags as 'FLAG=1' */
2060 udev_device_add_property(event
->dev
, key
, "1");
2061 else if (pos
[0] == '=') {
2066 while (pos
[0] != '\0' && !isspace(pos
[0]))
2069 udev_device_add_property(event
->dev
, key
, value
);
2074 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2078 case TK_M_IMPORT_PARENT
: {
2079 char import
[UTIL_PATH_SIZE
];
2081 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2082 if (import_parent_into_properties(event
->dev
, import
) != 0)
2083 if (cur
->key
.op
!= OP_NOMATCH
)
2088 if (match_key(rules
, cur
, event
->program_result
) != 0)
2091 case TK_A_STRING_ESCAPE_NONE
:
2094 case TK_A_STRING_ESCAPE_REPLACE
:
2095 esc
= ESCAPE_REPLACE
;
2097 case TK_A_DB_PERSIST
:
2098 udev_device_set_db_persist(event
->dev
);
2100 case TK_A_INOTIFY_WATCH
:
2101 if (event
->inotify_watch_final
)
2103 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2104 event
->inotify_watch_final
= true;
2105 event
->inotify_watch
= cur
->key
.watch
;
2107 case TK_A_DEVLINK_PRIO
:
2108 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2111 char owner
[UTIL_NAME_SIZE
];
2112 const char *ow
= owner
;
2115 if (event
->owner_final
)
2117 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2118 event
->owner_final
= true;
2119 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2120 event
->owner_set
= true;
2121 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2123 if (r
== -ENOENT
|| r
== -ESRCH
)
2124 log_error("specified user '%s' unknown", owner
);
2126 log_error_errno(r
, "error resolving user '%s': %m", owner
);
2130 log_debug("OWNER %u %s:%u",
2132 rules_str(rules
, rule
->rule
.filename_off
),
2133 rule
->rule
.filename_line
);
2137 char group
[UTIL_NAME_SIZE
];
2138 const char *gr
= group
;
2141 if (event
->group_final
)
2143 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2144 event
->group_final
= true;
2145 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2146 event
->group_set
= true;
2147 r
= get_group_creds(&gr
, &event
->gid
);
2149 if (r
== -ENOENT
|| r
== -ESRCH
)
2150 log_error("specified group '%s' unknown", group
);
2152 log_error_errno(r
, "error resolving group '%s': %m", group
);
2156 log_debug("GROUP %u %s:%u",
2158 rules_str(rules
, rule
->rule
.filename_off
),
2159 rule
->rule
.filename_line
);
2163 char mode_str
[UTIL_NAME_SIZE
];
2167 if (event
->mode_final
)
2169 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2170 mode
= strtol(mode_str
, &endptr
, 8);
2171 if (endptr
[0] != '\0') {
2172 log_error("ignoring invalid mode '%s'", mode_str
);
2175 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2176 event
->mode_final
= true;
2177 event
->mode_set
= true;
2179 log_debug("MODE %#o %s:%u",
2181 rules_str(rules
, rule
->rule
.filename_off
),
2182 rule
->rule
.filename_line
);
2186 if (event
->owner_final
)
2188 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2189 event
->owner_final
= true;
2190 event
->owner_set
= true;
2191 event
->uid
= cur
->key
.uid
;
2192 log_debug("OWNER %u %s:%u",
2194 rules_str(rules
, rule
->rule
.filename_off
),
2195 rule
->rule
.filename_line
);
2198 if (event
->group_final
)
2200 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2201 event
->group_final
= true;
2202 event
->group_set
= true;
2203 event
->gid
= cur
->key
.gid
;
2204 log_debug("GROUP %u %s:%u",
2206 rules_str(rules
, rule
->rule
.filename_off
),
2207 rule
->rule
.filename_line
);
2210 if (event
->mode_final
)
2212 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2213 event
->mode_final
= true;
2214 event
->mode_set
= true;
2215 event
->mode
= cur
->key
.mode
;
2216 log_debug("MODE %#o %s:%u",
2218 rules_str(rules
, rule
->rule
.filename_off
),
2219 rule
->rule
.filename_line
);
2221 case TK_A_SECLABEL
: {
2222 const char *name
, *label
;
2224 name
= rules_str(rules
, cur
->key
.attr_off
);
2225 label
= rules_str(rules
, cur
->key
.value_off
);
2226 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2227 udev_list_cleanup(&event
->seclabel_list
);
2228 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2229 log_debug("SECLABEL{%s}='%s' %s:%u",
2231 rules_str(rules
, rule
->rule
.filename_off
),
2232 rule
->rule
.filename_line
);
2236 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2237 char *value
= rules_str(rules
, cur
->key
.value_off
);
2238 char value_new
[UTIL_NAME_SIZE
];
2239 const char *value_old
= NULL
;
2241 if (value
[0] == '\0') {
2242 if (cur
->key
.op
== OP_ADD
)
2244 udev_device_add_property(event
->dev
, name
, NULL
);
2248 if (cur
->key
.op
== OP_ADD
)
2249 value_old
= udev_device_get_property_value(event
->dev
, name
);
2251 char temp
[UTIL_NAME_SIZE
];
2253 /* append value separated by space */
2254 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2255 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2257 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2259 udev_device_add_property(event
->dev
, name
, value_new
);
2263 char tag
[UTIL_PATH_SIZE
];
2266 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2267 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2268 udev_device_cleanup_tags_list(event
->dev
);
2269 for (p
= tag
; *p
!= '\0'; p
++) {
2270 if ((*p
>= 'a' && *p
<= 'z') ||
2271 (*p
>= 'A' && *p
<= 'Z') ||
2272 (*p
>= '0' && *p
<= '9') ||
2273 *p
== '-' || *p
== '_')
2275 log_error("ignoring invalid tag name '%s'", tag
);
2278 if (cur
->key
.op
== OP_REMOVE
)
2279 udev_device_remove_tag(event
->dev
, tag
);
2281 udev_device_add_tag(event
->dev
, tag
);
2285 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2287 char name_str
[UTIL_PATH_SIZE
];
2290 if (event
->name_final
)
2292 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2293 event
->name_final
= true;
2294 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2295 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2296 count
= util_replace_chars(name_str
, "/");
2298 log_debug("%i character(s) replaced", count
);
2300 if (major(udev_device_get_devnum(event
->dev
)) &&
2301 !streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/"))) {
2302 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2304 rules_str(rules
, rule
->rule
.filename_off
),
2305 rule
->rule
.filename_line
);
2308 if (free_and_strdup(&event
->name
, name_str
) < 0) {
2312 log_debug("NAME '%s' %s:%u",
2314 rules_str(rules
, rule
->rule
.filename_off
),
2315 rule
->rule
.filename_line
);
2318 case TK_A_DEVLINK
: {
2319 char temp
[UTIL_PATH_SIZE
];
2320 char filename
[UTIL_PATH_SIZE
];
2324 if (event
->devlink_final
)
2326 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2328 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2329 event
->devlink_final
= true;
2330 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2331 udev_device_cleanup_devlinks_list(event
->dev
);
2333 /* allow multiple symlinks separated by spaces */
2334 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2335 if (esc
== ESCAPE_UNSET
)
2336 count
= util_replace_chars(temp
, "/ ");
2337 else if (esc
== ESCAPE_REPLACE
)
2338 count
= util_replace_chars(temp
, "/");
2340 log_debug("%i character(s) replaced" , count
);
2342 while (isspace(pos
[0]))
2344 next
= strchr(pos
, ' ');
2345 while (next
!= NULL
) {
2347 log_debug("LINK '%s' %s:%u", pos
,
2348 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2349 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2350 udev_device_add_devlink(event
->dev
, filename
);
2351 while (isspace(next
[1]))
2354 next
= strchr(pos
, ' ');
2356 if (pos
[0] != '\0') {
2357 log_debug("LINK '%s' %s:%u", pos
,
2358 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2359 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2360 udev_device_add_devlink(event
->dev
, filename
);
2365 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2366 char attr
[UTIL_PATH_SIZE
];
2367 char value
[UTIL_NAME_SIZE
];
2368 _cleanup_fclose_
FILE *f
= NULL
;
2370 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2371 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2372 attr_subst_subdir(attr
, sizeof(attr
));
2374 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2375 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2376 rules_str(rules
, rule
->rule
.filename_off
),
2377 rule
->rule
.filename_line
);
2378 f
= fopen(attr
, "we");
2380 log_error_errno(errno
, "error opening ATTR{%s} for writing: %m", attr
);
2381 else if (fprintf(f
, "%s", value
) <= 0)
2382 log_error_errno(errno
, "error writing ATTR{%s}: %m", attr
);
2386 char filename
[UTIL_PATH_SIZE
];
2387 char value
[UTIL_NAME_SIZE
];
2390 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.attr_off
), filename
, sizeof(filename
));
2391 sysctl_normalize(filename
);
2392 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2393 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename
, value
,
2394 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2395 r
= sysctl_write(filename
, value
);
2397 log_error_errno(r
, "error writing SYSCTL{%s}='%s': %m", filename
, value
);
2400 case TK_A_RUN_BUILTIN
:
2401 case TK_A_RUN_PROGRAM
: {
2402 struct udev_list_entry
*entry
;
2404 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2405 udev_list_cleanup(&event
->run_list
);
2406 log_debug("RUN '%s' %s:%u",
2407 rules_str(rules
, cur
->key
.value_off
),
2408 rules_str(rules
, rule
->rule
.filename_off
),
2409 rule
->rule
.filename_line
);
2410 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2411 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2415 if (cur
->key
.rule_goto
== 0)
2417 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2422 case TK_M_PARENTS_MIN
:
2423 case TK_M_PARENTS_MAX
:
2426 log_error("wrong type %u", cur
->type
);
2433 /* fast-forward to next rule */
2434 cur
= rule
+ rule
->rule
.token_count
;
2438 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2444 _cleanup_strv_free_
char **tags
= NULL
;
2447 _cleanup_free_
char *path
= NULL
;
2450 if (rules
->tokens
== NULL
)
2453 cur
= &rules
->tokens
[0];
2456 switch (cur
->type
) {
2461 /* skip rules without a static_node tag */
2462 if (!rule
->rule
.has_static_node
)
2468 tags
= strv_free(tags
);
2477 mode
= cur
->key
.mode
;
2480 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2485 case TK_A_STATIC_NODE
: {
2486 char device_node
[UTIL_PATH_SIZE
];
2487 char tags_dir
[UTIL_PATH_SIZE
];
2488 char tag_symlink
[UTIL_PATH_SIZE
];
2491 /* we assure, that the permissions tokens are sorted before the static token */
2493 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2496 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2497 if (stat(device_node
, &stats
) != 0)
2499 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2502 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2504 STRV_FOREACH(t
, tags
) {
2505 _cleanup_free_
char *unescaped_filename
= NULL
;
2507 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2508 r
= mkdir_p(tags_dir
, 0755);
2510 return log_error_errno(r
, "failed to create %s: %m", tags_dir
);
2512 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2514 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2515 r
= symlink(device_node
, tag_symlink
);
2516 if (r
< 0 && errno
!= EEXIST
)
2517 return log_error_errno(errno
, "failed to create symlink %s -> %s: %m",
2518 tag_symlink
, device_node
);
2522 /* don't touch the permissions if only the tags were set */
2523 if (mode
== 0 && uid
== 0 && gid
== 0)
2532 if (mode
!= (stats
.st_mode
& 01777)) {
2533 r
= chmod(device_node
, mode
);
2535 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2538 log_debug("chmod '%s' %#o", device_node
, mode
);
2541 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2542 r
= chown(device_node
, uid
, gid
);
2544 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2547 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2550 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2560 /* fast-forward to next rule */
2561 cur
= rule
+ rule
->rule
.token_count
;
2568 fchmod(fileno(f
), 0644);
2569 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2570 unlink_noerrno("/run/udev/static_node-tags");
2571 unlink_noerrno(path
);