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="", KEY:="" */
99 enum string_glob_type
{
101 GL_PLAIN
, /* no special chars */
102 GL_GLOB
, /* shell globs ?,*,[] */
103 GL_SPLIT
, /* multi-value A|B */
104 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
105 GL_SOMETHING
, /* commonly used "?*" */
108 enum string_subst_type
{
115 /* tokens of a rule are sorted/handled in this order */
120 TK_M_ACTION
, /* val */
121 TK_M_DEVPATH
, /* val */
122 TK_M_KERNEL
, /* val */
123 TK_M_DEVLINK
, /* val */
125 TK_M_ENV
, /* val, attr */
127 TK_M_SUBSYSTEM
, /* val */
128 TK_M_DRIVER
, /* val */
129 TK_M_WAITFOR
, /* val */
130 TK_M_ATTR
, /* val, attr */
133 TK_M_KERNELS
, /* val */
134 TK_M_SUBSYSTEMS
, /* val */
135 TK_M_DRIVERS
, /* val */
136 TK_M_ATTRS
, /* val, attr */
140 TK_M_TEST
, /* val, mode_t */
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
;
205 enum udev_builtin_cmd builtin_cmd
;
213 struct udev_rules
*rules
;
215 struct token token
[MAX_TK
];
216 unsigned int token_cur
;
220 static const char *operation_str(enum operation_type type
) {
221 static const char *operation_strs
[] = {
222 [OP_UNSET
] = "UNSET",
223 [OP_MATCH
] = "match",
224 [OP_NOMATCH
] = "nomatch",
225 [OP_MATCH_MAX
] = "MATCH_MAX",
228 [OP_REMOVE
] = "remove",
229 [OP_ASSIGN
] = "assign",
230 [OP_ASSIGN_FINAL
] = "assign-final",
233 return operation_strs
[type
];
236 static const char *string_glob_str(enum string_glob_type type
) {
237 static const char *string_glob_strs
[] = {
238 [GL_UNSET
] = "UNSET",
239 [GL_PLAIN
] = "plain",
241 [GL_SPLIT
] = "split",
242 [GL_SPLIT_GLOB
] = "split-glob",
243 [GL_SOMETHING
] = "split-glob",
246 return string_glob_strs
[type
];
249 static const char *token_str(enum token_type type
) {
250 static const char *token_strs
[] = {
251 [TK_UNSET
] = "UNSET",
254 [TK_M_ACTION
] = "M ACTION",
255 [TK_M_DEVPATH
] = "M DEVPATH",
256 [TK_M_KERNEL
] = "M KERNEL",
257 [TK_M_DEVLINK
] = "M DEVLINK",
258 [TK_M_NAME
] = "M NAME",
259 [TK_M_ENV
] = "M ENV",
260 [TK_M_TAG
] = "M TAG",
261 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
262 [TK_M_DRIVER
] = "M DRIVER",
263 [TK_M_WAITFOR
] = "M WAITFOR",
264 [TK_M_ATTR
] = "M ATTR",
266 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
267 [TK_M_KERNELS
] = "M KERNELS",
268 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
269 [TK_M_DRIVERS
] = "M DRIVERS",
270 [TK_M_ATTRS
] = "M ATTRS",
271 [TK_M_TAGS
] = "M TAGS",
272 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
274 [TK_M_TEST
] = "M TEST",
275 [TK_M_PROGRAM
] = "M PROGRAM",
276 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
277 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
278 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
279 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
280 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
281 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
282 [TK_M_RESULT
] = "M RESULT",
283 [TK_M_MAX
] = "M MAX",
285 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
286 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
287 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
288 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
289 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
290 [TK_A_OWNER
] = "A OWNER",
291 [TK_A_GROUP
] = "A GROUP",
292 [TK_A_MODE
] = "A MODE",
293 [TK_A_OWNER_ID
] = "A OWNER_ID",
294 [TK_A_GROUP_ID
] = "A GROUP_ID",
295 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
296 [TK_A_SECLABEL
] = "A SECLABEL",
297 [TK_A_MODE_ID
] = "A MODE_ID",
298 [TK_A_ENV
] = "A ENV",
299 [TK_A_TAG
] = "A ENV",
300 [TK_A_NAME
] = "A NAME",
301 [TK_A_DEVLINK
] = "A DEVLINK",
302 [TK_A_ATTR
] = "A ATTR",
303 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
304 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
305 [TK_A_GOTO
] = "A GOTO",
310 return token_strs
[type
];
313 static void dump_token(struct udev_rules
*rules
, struct token
*token
) {
314 enum token_type type
= token
->type
;
315 enum operation_type op
= token
->key
.op
;
316 enum string_glob_type glob
= token
->key
.glob
;
317 const char *value
= str(rules
, token
->key
.value_off
);
318 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
323 const char *tks_ptr
= (char *)rules
->tokens
;
324 const char *tk_ptr
= (char *)token
;
325 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
327 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
328 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
329 idx
, token
->rule
.token_count
,
330 &rules
->buf
[token
->rule
.label_off
]);
342 case TK_M_SUBSYSTEMS
:
346 case TK_M_IMPORT_FILE
:
347 case TK_M_IMPORT_PROG
:
349 case TK_M_IMPORT_CMDLINE
:
350 case TK_M_IMPORT_PARENT
:
357 case TK_A_RUN_BUILTIN
:
358 case TK_A_RUN_PROGRAM
:
359 log_debug("%s %s '%s'(%s)",
360 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
362 case TK_M_IMPORT_BUILTIN
:
363 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
370 log_debug("%s %s '%s' '%s'(%s)",
371 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
375 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
377 case TK_A_STRING_ESCAPE_NONE
:
378 case TK_A_STRING_ESCAPE_REPLACE
:
379 case TK_A_DB_PERSIST
:
380 log_debug("%s", token_str(type
));
383 log_debug("%s %s '%s'(%s) %#o",
384 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
386 case TK_A_INOTIFY_WATCH
:
387 log_debug("%s %u", token_str(type
), token
->key
.watch
);
389 case TK_A_DEVLINK_PRIO
:
390 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
393 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
396 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
399 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
401 case TK_A_STATIC_NODE
:
402 log_debug("%s '%s'", token_str(type
), value
);
405 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
408 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
411 log_debug("* %s", token_str(type
));
413 case TK_M_PARENTS_MIN
:
414 case TK_M_PARENTS_MAX
:
417 log_debug("unknown type %u", type
);
422 static void dump_rules(struct udev_rules
*rules
) {
425 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
427 rules
->token_cur
* sizeof(struct token
),
430 for (i
= 0; i
< rules
->token_cur
; i
++)
431 dump_token(rules
, &rules
->tokens
[i
]);
434 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
435 static inline void dump_rules(struct udev_rules
*rules
) {}
438 static int add_token(struct udev_rules
*rules
, struct token
*token
) {
439 /* grow buffer if needed */
440 if (rules
->token_cur
+1 >= rules
->token_max
) {
441 struct token
*tokens
;
444 /* double the buffer size */
445 add
= rules
->token_max
;
449 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
452 rules
->tokens
= tokens
;
453 rules
->token_max
+= add
;
455 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
460 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
) {
466 /* lookup, if we know it already */
467 for (i
= 0; i
< rules
->uids_cur
; i
++) {
468 off
= rules
->uids
[i
].name_off
;
469 if (streq(rules_str(rules
, off
), owner
)) {
470 uid
= rules
->uids
[i
].uid
;
474 r
= get_user_creds(&owner
, &uid
, NULL
, NULL
, NULL
);
476 if (r
== -ENOENT
|| r
== -ESRCH
)
477 log_error("specified user '%s' unknown\n", owner
);
479 log_error("error resolving user '%s': %s\n", owner
, strerror(-r
));
482 /* grow buffer if needed */
483 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
484 struct uid_gid
*uids
;
487 /* double the buffer size */
488 add
= rules
->uids_max
;
492 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
496 rules
->uids_max
+= add
;
498 rules
->uids
[rules
->uids_cur
].uid
= uid
;
499 off
= rules_add_string(rules
, owner
);
502 rules
->uids
[rules
->uids_cur
].name_off
= off
;
507 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
) {
513 /* lookup, if we know it already */
514 for (i
= 0; i
< rules
->gids_cur
; i
++) {
515 off
= rules
->gids
[i
].name_off
;
516 if (streq(rules_str(rules
, off
), group
)) {
517 gid
= rules
->gids
[i
].gid
;
521 r
= get_group_creds(&group
, &gid
);
523 if (r
== -ENOENT
|| r
== -ESRCH
)
524 log_error("specified group '%s' unknown\n", group
);
526 log_error("error resolving group '%s': %s\n", group
, strerror(-r
));
529 /* grow buffer if needed */
530 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
531 struct uid_gid
*gids
;
534 /* double the buffer size */
535 add
= rules
->gids_max
;
539 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
543 rules
->gids_max
+= add
;
545 rules
->gids
[rules
->gids_cur
].gid
= gid
;
546 off
= rules_add_string(rules
, group
);
549 rules
->gids
[rules
->gids_cur
].name_off
= off
;
554 static int import_property_from_string(struct udev_device
*dev
, char *line
) {
558 struct udev_list_entry
*entry
;
562 while (isspace(key
[0]))
565 /* comment or empty line */
566 if (key
[0] == '#' || key
[0] == '\0')
569 /* split key/value */
570 val
= strchr(key
, '=');
577 while (isspace(val
[0]))
584 while (isspace(key
[len
-1]))
588 /* terminate value */
592 while (isspace(val
[len
-1]))
600 if (val
[0] == '"' || val
[0] == '\'') {
601 if (val
[len
-1] != val
[0]) {
602 log_debug("inconsistent quoting: '%s', skip", line
);
609 entry
= udev_device_add_property(dev
, key
, val
);
610 /* store in db, skip private keys */
612 udev_list_entry_set_num(entry
, true);
617 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
,
632 usec_t timeout_warn_usec
,
633 const char *program
, const sigset_t
*sigmask
) {
634 struct udev_device
*dev
= event
->dev
;
636 char result
[UTIL_LINE_SIZE
];
640 envp
= udev_device_get_properties_envp(dev
);
641 err
= udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, program
, envp
, sigmask
, result
, sizeof(result
));
646 while (line
!= NULL
) {
649 pos
= strchr(line
, '\n');
654 import_property_from_string(dev
, line
);
660 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
) {
661 struct udev_device
*dev_parent
;
662 struct udev_list_entry
*list_entry
;
664 dev_parent
= udev_device_get_parent(dev
);
665 if (dev_parent
== NULL
)
668 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
669 const char *key
= udev_list_entry_get_name(list_entry
);
670 const char *val
= udev_list_entry_get_value(list_entry
);
672 if (fnmatch(filter
, key
, 0) == 0) {
673 struct udev_list_entry
*entry
;
675 entry
= udev_device_add_property(dev
, key
, val
);
676 /* store in db, skip private keys */
678 udev_list_entry_set_num(entry
, true);
684 #define WAIT_LOOP_PER_SECOND 50
685 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
) {
722 if (strstr(attr
, "/*/")) {
724 char dirname
[UTIL_PATH_SIZE
];
728 strscpy(dirname
, sizeof(dirname
), attr
);
729 pos
= strstr(dirname
, "/*/");
734 dir
= opendir(dirname
);
738 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
741 if (dent
->d_name
[0] == '.')
743 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
744 if (stat(attr
, &stats
) == 0) {
756 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
) {
761 if (linepos
== NULL
|| linepos
[0] == '\0')
764 /* skip whitespace */
765 while (isspace(linepos
[0]) || linepos
[0] == ',')
769 if (linepos
[0] == '\0')
775 if (linepos
[0] == '\0')
777 if (isspace(linepos
[0]))
779 if (linepos
[0] == '=')
781 if ((linepos
[0] == '+') || (linepos
[0] == '-') || (linepos
[0] == '!') || (linepos
[0] == ':'))
782 if (linepos
[1] == '=')
786 /* remember end of key */
789 /* skip whitespace after key */
790 while (isspace(linepos
[0]))
792 if (linepos
[0] == '\0')
795 /* get operation type */
796 if (linepos
[0] == '=' && linepos
[1] == '=') {
799 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
802 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
805 } else if (linepos
[0] == '-' && linepos
[1] == '=') {
808 } else if (linepos
[0] == '=') {
811 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
812 *op
= OP_ASSIGN_FINAL
;
820 /* skip whitespace after operator */
821 while (isspace(linepos
[0]))
823 if (linepos
[0] == '\0')
827 if (linepos
[0] == '"')
834 temp
= strchr(linepos
, '"');
840 /* move line to next key */
845 /* extract possible KEY{attr} */
846 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
) {
867 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
868 const char *attr
= NULL
;
870 memzero(token
, sizeof(struct token
));
882 case TK_M_SUBSYSTEMS
:
886 case TK_M_IMPORT_FILE
:
887 case TK_M_IMPORT_PROG
:
889 case TK_M_IMPORT_CMDLINE
:
890 case TK_M_IMPORT_PARENT
:
900 case TK_A_STATIC_NODE
:
901 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
903 case TK_M_IMPORT_BUILTIN
:
904 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
905 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
914 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
915 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
918 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
920 token
->key
.mode
= *(mode_t
*)data
;
922 case TK_A_STRING_ESCAPE_NONE
:
923 case TK_A_STRING_ESCAPE_REPLACE
:
924 case TK_A_DB_PERSIST
:
926 case TK_A_RUN_BUILTIN
:
927 case TK_A_RUN_PROGRAM
:
928 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
929 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
931 case TK_A_INOTIFY_WATCH
:
932 case TK_A_DEVLINK_PRIO
:
933 token
->key
.devlink_prio
= *(int *)data
;
936 token
->key
.uid
= *(uid_t
*)data
;
939 token
->key
.gid
= *(gid_t
*)data
;
942 token
->key
.mode
= *(mode_t
*)data
;
945 case TK_M_PARENTS_MIN
:
946 case TK_M_PARENTS_MAX
:
950 log_error("wrong type %u", type
);
954 if (value
!= NULL
&& type
< TK_M_MAX
) {
955 /* check if we need to split or call fnmatch() while matching rules */
956 enum string_glob_type glob
;
960 has_split
= (strchr(value
, '|') != NULL
);
961 has_glob
= string_is_glob(value
);
962 if (has_split
&& has_glob
) {
963 glob
= GL_SPLIT_GLOB
;
964 } else if (has_split
) {
966 } else if (has_glob
) {
967 if (streq(value
, "?*"))
974 token
->key
.glob
= glob
;
977 if (value
!= NULL
&& type
> TK_M_MAX
) {
978 /* check if assigned value has substitution chars */
980 token
->key
.subst
= SB_SUBSYS
;
981 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
982 token
->key
.subst
= SB_FORMAT
;
984 token
->key
.subst
= SB_NONE
;
988 /* check if property/attribute name has substitution chars */
990 token
->key
.attrsubst
= SB_SUBSYS
;
991 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
992 token
->key
.attrsubst
= SB_FORMAT
;
994 token
->key
.attrsubst
= SB_NONE
;
997 token
->key
.type
= type
;
999 rule_tmp
->token_cur
++;
1000 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
1001 log_error("temporary rule array too small");
1007 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
) {
1009 unsigned int start
= 0;
1010 unsigned int end
= rule_tmp
->token_cur
;
1012 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1013 enum token_type next_val
= TK_UNSET
;
1014 unsigned int next_idx
= 0;
1017 /* find smallest value */
1018 for (j
= start
; j
< end
; j
++) {
1019 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1021 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1022 next_val
= rule_tmp
->token
[j
].type
;
1027 /* add token and mark done */
1028 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1030 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1033 if (next_idx
== start
)
1035 if (next_idx
+1 == end
)
1041 static int add_rule(struct udev_rules
*rules
, char *line
,
1042 const char *filename
, unsigned int filename_off
, unsigned int lineno
) {
1045 struct rule_tmp rule_tmp
;
1047 memzero(&rule_tmp
, sizeof(struct rule_tmp
));
1048 rule_tmp
.rules
= rules
;
1049 rule_tmp
.rule
.type
= TK_RULE
;
1050 /* the offset in the rule is limited to unsigned short */
1051 if (filename_off
< USHRT_MAX
)
1052 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1053 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1059 enum operation_type op
;
1061 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1062 /* Avoid erroring on trailing whitespace. This is probably rare
1063 * so save the work for the error case instead of always trying
1064 * to strip the trailing whitespace with strstrip(). */
1065 while (isblank(*linepos
))
1068 /* If we aren't at the end of the line, this is a parsing error.
1069 * Make a best effort to describe where the problem is. */
1070 if (*linepos
!= '\n') {
1071 char buf
[2] = {linepos
[1]};
1072 _cleanup_free_
char *tmp
;
1075 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')\n",
1076 filename
, lineno
, linepos
- line
+ 1, tmp
);
1077 if (linepos
[1] == '#')
1078 log_error("hint: comments can only start at beginning of line");
1083 if (streq(key
, "ACTION")) {
1084 if (op
> OP_MATCH_MAX
) {
1085 log_error("invalid ACTION operation");
1088 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1092 if (streq(key
, "DEVPATH")) {
1093 if (op
> OP_MATCH_MAX
) {
1094 log_error("invalid DEVPATH operation");
1097 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1101 if (streq(key
, "KERNEL")) {
1102 if (op
> OP_MATCH_MAX
) {
1103 log_error("invalid KERNEL operation");
1106 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1110 if (streq(key
, "SUBSYSTEM")) {
1111 if (op
> OP_MATCH_MAX
) {
1112 log_error("invalid SUBSYSTEM operation");
1115 /* bus, class, subsystem events should all be the same */
1116 if (streq(value
, "subsystem") ||
1117 streq(value
, "bus") ||
1118 streq(value
, "class")) {
1119 if (streq(value
, "bus") || streq(value
, "class"))
1120 log_error("'%s' must be specified as 'subsystem' "
1121 "please fix it in %s:%u", value
, filename
, lineno
);
1122 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1124 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1128 if (streq(key
, "DRIVER")) {
1129 if (op
> OP_MATCH_MAX
) {
1130 log_error("invalid DRIVER operation");
1133 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1137 if (startswith(key
, "ATTR{")) {
1138 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTR"));
1140 log_error("error parsing ATTR attribute");
1143 if (op
== OP_REMOVE
) {
1144 log_error("invalid ATTR operation");
1147 if (op
< OP_MATCH_MAX
) {
1148 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1150 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1155 if (startswith(key
, "SECLABEL{")) {
1156 attr
= get_key_attribute(rules
->udev
, key
+ strlen("SECLABEL"));
1158 log_error("error parsing SECLABEL attribute");
1161 if (op
== OP_REMOVE
) {
1162 log_error("invalid SECLABEL operation");
1166 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1170 if (streq(key
, "KERNELS")) {
1171 if (op
> OP_MATCH_MAX
) {
1172 log_error("invalid KERNELS operation");
1175 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1179 if (streq(key
, "SUBSYSTEMS")) {
1180 if (op
> OP_MATCH_MAX
) {
1181 log_error("invalid SUBSYSTEMS operation");
1184 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1188 if (streq(key
, "DRIVERS")) {
1189 if (op
> OP_MATCH_MAX
) {
1190 log_error("invalid DRIVERS operation");
1193 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1197 if (startswith(key
, "ATTRS{")) {
1198 if (op
> OP_MATCH_MAX
) {
1199 log_error("invalid ATTRS operation");
1202 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ATTRS"));
1204 log_error("error parsing ATTRS attribute");
1207 if (startswith(attr
, "device/"))
1208 log_error("the 'device' link may not be available in a future kernel, "
1209 "please fix it in %s:%u", filename
, lineno
);
1210 else if (strstr(attr
, "../") != NULL
)
1211 log_error("do not reference parent sysfs directories directly, "
1212 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1213 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1217 if (streq(key
, "TAGS")) {
1218 if (op
> OP_MATCH_MAX
) {
1219 log_error("invalid TAGS operation");
1222 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1226 if (startswith(key
, "ENV{")) {
1227 attr
= get_key_attribute(rules
->udev
, key
+ strlen("ENV"));
1229 log_error("error parsing ENV attribute");
1232 if (op
== OP_REMOVE
) {
1233 log_error("invalid ENV operation");
1236 if (op
< OP_MATCH_MAX
) {
1237 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1240 static const char *blacklist
[] = {
1255 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1256 if (!streq(attr
, blacklist
[i
]))
1258 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1261 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1267 if (streq(key
, "TAG")) {
1268 if (op
< OP_MATCH_MAX
)
1269 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1271 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1275 if (streq(key
, "PROGRAM")) {
1276 if (op
== OP_REMOVE
) {
1277 log_error("invalid PROGRAM operation");
1280 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1284 if (streq(key
, "RESULT")) {
1285 if (op
> OP_MATCH_MAX
) {
1286 log_error("invalid RESULT operation");
1289 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1293 if (startswith(key
, "IMPORT")) {
1294 attr
= get_key_attribute(rules
->udev
, key
+ strlen("IMPORT"));
1296 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1299 if (op
== OP_REMOVE
) {
1300 log_error("invalid IMPORT operation");
1303 if (streq(attr
, "program")) {
1304 /* find known built-in command */
1305 if (value
[0] != '/') {
1306 enum udev_builtin_cmd cmd
;
1308 cmd
= udev_builtin_lookup(value
);
1309 if (cmd
< UDEV_BUILTIN_MAX
) {
1310 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1311 value
, filename
, lineno
);
1312 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1316 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1317 } else if (streq(attr
, "builtin")) {
1318 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1320 if (cmd
< UDEV_BUILTIN_MAX
)
1321 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1323 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1324 } else if (streq(attr
, "file")) {
1325 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1326 } else if (streq(attr
, "db")) {
1327 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1328 } else if (streq(attr
, "cmdline")) {
1329 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1330 } else if (streq(attr
, "parent")) {
1331 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1333 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1337 if (startswith(key
, "TEST")) {
1340 if (op
> OP_MATCH_MAX
) {
1341 log_error("invalid TEST operation");
1344 attr
= get_key_attribute(rules
->udev
, key
+ strlen("TEST"));
1346 mode
= strtol(attr
, NULL
, 8);
1347 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1349 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1354 if (startswith(key
, "RUN")) {
1355 attr
= get_key_attribute(rules
->udev
, key
+ strlen("RUN"));
1358 if (op
== OP_REMOVE
) {
1359 log_error("invalid RUN operation");
1363 if (streq(attr
, "builtin")) {
1364 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1366 if (cmd
< UDEV_BUILTIN_MAX
)
1367 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1369 log_error("RUN{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1370 } else if (streq(attr
, "program")) {
1371 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1373 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1375 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1381 if (streq(key
, "WAIT_FOR") || streq(key
, "WAIT_FOR_SYSFS")) {
1382 if (op
== OP_REMOVE
) {
1383 log_error("invalid WAIT_FOR/WAIT_FOR_SYSFS operation");
1386 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1390 if (streq(key
, "LABEL")) {
1391 if (op
== OP_REMOVE
) {
1392 log_error("invalid LABEL operation");
1395 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1399 if (streq(key
, "GOTO")) {
1400 if (op
== OP_REMOVE
) {
1401 log_error("invalid GOTO operation");
1404 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1408 if (startswith(key
, "NAME")) {
1409 if (op
== OP_REMOVE
) {
1410 log_error("invalid NAME operation");
1413 if (op
< OP_MATCH_MAX
) {
1414 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1416 if (streq(value
, "%k")) {
1417 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1418 "please remove it from %s:%u\n", filename
, lineno
);
1421 if (value
[0] == '\0') {
1422 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1423 "please remove it from %s:%u\n", filename
, lineno
);
1426 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1428 rule_tmp
.rule
.rule
.can_set_name
= true;
1432 if (streq(key
, "SYMLINK")) {
1433 if (op
== OP_REMOVE
) {
1434 log_error("invalid SYMLINK operation");
1437 if (op
< OP_MATCH_MAX
)
1438 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1440 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1441 rule_tmp
.rule
.rule
.can_set_name
= true;
1445 if (streq(key
, "OWNER")) {
1449 if (op
== OP_REMOVE
) {
1450 log_error("invalid OWNER operation");
1454 uid
= strtoul(value
, &endptr
, 10);
1455 if (endptr
[0] == '\0') {
1456 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1457 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1458 uid
= add_uid(rules
, value
);
1459 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1460 } else if (rules
->resolve_names
>= 0) {
1461 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1463 rule_tmp
.rule
.rule
.can_set_name
= true;
1467 if (streq(key
, "GROUP")) {
1471 if (op
== OP_REMOVE
) {
1472 log_error("invalid GROUP operation");
1476 gid
= strtoul(value
, &endptr
, 10);
1477 if (endptr
[0] == '\0') {
1478 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1479 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1480 gid
= add_gid(rules
, value
);
1481 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1482 } else if (rules
->resolve_names
>= 0) {
1483 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1485 rule_tmp
.rule
.rule
.can_set_name
= true;
1489 if (streq(key
, "MODE")) {
1493 if (op
== OP_REMOVE
) {
1494 log_error("invalid MODE operation");
1498 mode
= strtol(value
, &endptr
, 8);
1499 if (endptr
[0] == '\0')
1500 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1502 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1503 rule_tmp
.rule
.rule
.can_set_name
= true;
1507 if (streq(key
, "OPTIONS")) {
1510 if (op
== OP_REMOVE
) {
1511 log_error("invalid OPTIONS operation");
1515 pos
= strstr(value
, "link_priority=");
1517 int prio
= atoi(&pos
[strlen("link_priority=")]);
1519 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1522 pos
= strstr(value
, "string_escape=");
1524 pos
= &pos
[strlen("string_escape=")];
1525 if (startswith(pos
, "none"))
1526 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1527 else if (startswith(pos
, "replace"))
1528 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1531 pos
= strstr(value
, "db_persist");
1533 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1535 pos
= strstr(value
, "nowatch");
1539 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1541 pos
= strstr(value
, "watch");
1545 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1549 pos
= strstr(value
, "static_node=");
1551 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1552 rule_tmp
.rule
.rule
.has_static_node
= true;
1558 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1562 /* add rule token */
1563 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1564 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1567 /* add tokens to list, sorted by type */
1568 if (sort_token(rules
, &rule_tmp
) != 0)
1573 log_error("invalid rule '%s:%u'", filename
, lineno
);
1577 static int parse_file(struct udev_rules
*rules
, const char *filename
) {
1578 _cleanup_fclose_
FILE *f
= NULL
;
1579 unsigned int first_token
;
1580 unsigned int filename_off
;
1581 char line
[UTIL_LINE_SIZE
];
1585 f
= fopen(filename
, "re");
1587 if (errno
== ENOENT
)
1593 if (null_or_empty_fd(fileno(f
))) {
1594 log_debug("Skipping empty file: %s", filename
);
1597 log_debug("Reading rules file: %s", filename
);
1599 first_token
= rules
->token_cur
;
1600 filename_off
= rules_add_string(rules
, filename
);
1602 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1606 /* skip whitespace */
1609 while (isspace(key
[0]))
1620 /* continue reading if backslash+newline is found */
1621 while (line
[len
-2] == '\\') {
1622 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1624 if (strlen(&line
[len
-2]) < 2)
1630 if (len
+1 >= sizeof(line
)) {
1631 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1634 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1637 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1638 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1639 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1640 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1643 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1644 if (rules
->tokens
[j
].type
!= TK_RULE
)
1646 if (rules
->tokens
[j
].rule
.label_off
== 0)
1648 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1650 rules
->tokens
[i
].key
.rule_goto
= j
;
1653 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1654 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1660 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
) {
1661 struct udev_rules
*rules
;
1662 struct udev_list file_list
;
1663 struct token end_token
;
1667 rules
= new0(struct udev_rules
, 1);
1671 rules
->resolve_names
= resolve_names
;
1672 udev_list_init(udev
, &file_list
, true);
1674 /* init token array and string buffer */
1675 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1676 if (rules
->tokens
== NULL
)
1677 return udev_rules_unref(rules
);
1678 rules
->token_max
= PREALLOC_TOKEN
;
1680 rules
->strbuf
= strbuf_new();
1682 return udev_rules_unref(rules
);
1684 udev_rules_check_timestamp(rules
);
1686 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1688 log_error_errno(-r
, "failed to enumerate rules files: %m");
1689 return udev_rules_unref(rules
);
1693 * The offset value in the rules strct is limited; add all
1694 * rules file names to the beginning of the string buffer.
1696 STRV_FOREACH(f
, files
)
1697 rules_add_string(rules
, *f
);
1699 STRV_FOREACH(f
, files
)
1700 parse_file(rules
, *f
);
1704 memzero(&end_token
, sizeof(struct token
));
1705 end_token
.type
= TK_END
;
1706 add_token(rules
, &end_token
);
1707 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1708 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1710 /* cleanup temporary strbuf data */
1711 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1712 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1713 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1714 strbuf_complete(rules
->strbuf
);
1716 /* cleanup uid/gid cache */
1719 rules
->uids_cur
= 0;
1720 rules
->uids_max
= 0;
1723 rules
->gids_cur
= 0;
1724 rules
->gids_max
= 0;
1730 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
) {
1733 free(rules
->tokens
);
1734 strbuf_cleanup(rules
->strbuf
);
1741 bool udev_rules_check_timestamp(struct udev_rules
*rules
) {
1745 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1748 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
) {
1749 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1756 switch (token
->key
.glob
) {
1758 match
= (streq(key_value
, val
));
1761 match
= (fnmatch(key_value
, val
, 0) == 0);
1768 s
= rules_str(rules
, token
->key
.value_off
);
1773 next
= strchr(s
, '|');
1775 size_t matchlen
= (size_t)(next
- s
);
1777 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1781 match
= (streq(s
, val
));
1790 char value
[UTIL_PATH_SIZE
];
1792 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1794 while (key_value
!= NULL
) {
1795 pos
= strchr(key_value
, '|');
1800 match
= (fnmatch(key_value
, val
, 0) == 0);
1808 match
= (val
[0] != '\0');
1814 if (match
&& (token
->key
.op
== OP_MATCH
))
1816 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1821 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
) {
1823 char nbuf
[UTIL_NAME_SIZE
];
1825 char vbuf
[UTIL_NAME_SIZE
];
1828 name
= rules_str(rules
, cur
->key
.attr_off
);
1829 switch (cur
->key
.attrsubst
) {
1831 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1835 value
= udev_device_get_sysattr_value(dev
, name
);
1840 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1848 /* remove trailing whitespace, if not asked to match for it */
1849 len
= strlen(value
);
1850 if (len
> 0 && isspace(value
[len
-1])) {
1851 const char *key_value
;
1854 key_value
= rules_str(rules
, cur
->key
.value_off
);
1855 klen
= strlen(key_value
);
1856 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1857 if (value
!= vbuf
) {
1858 strscpy(vbuf
, sizeof(vbuf
), value
);
1861 while (len
> 0 && isspace(vbuf
[--len
]))
1866 return match_key(rules
, cur
, value
);
1875 int udev_rules_apply_to_event(struct udev_rules
*rules
,
1876 struct udev_event
*event
,
1877 usec_t timeout_usec
,
1878 usec_t timeout_warn_usec
,
1879 struct udev_list
*properties_list
,
1880 const sigset_t
*sigmask
) {
1883 enum escape_type esc
= ESCAPE_UNSET
;
1886 if (rules
->tokens
== NULL
)
1889 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1890 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1891 udev_device_get_ifindex(event
->dev
) > 0));
1893 /* loop through token list, match, run actions or forward to next rule */
1894 cur
= &rules
->tokens
[0];
1897 dump_token(rules
, cur
);
1898 switch (cur
->type
) {
1902 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1903 if (!can_set_name
&& rule
->rule
.can_set_name
)
1908 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1912 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1916 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1919 case TK_M_DEVLINK
: {
1920 struct udev_list_entry
*list_entry
;
1923 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1924 const char *devlink
;
1926 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1927 if (match_key(rules
, cur
, devlink
) == 0) {
1937 if (match_key(rules
, cur
, event
->name
) != 0)
1941 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1944 value
= udev_device_get_property_value(event
->dev
, key_name
);
1946 /* check global properties */
1947 if (!value
&& properties_list
) {
1948 struct udev_list_entry
*list_entry
;
1950 list_entry
= udev_list_get_entry(properties_list
);
1951 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1952 if (list_entry
!= NULL
)
1953 value
= udev_list_entry_get_value(list_entry
);
1958 if (match_key(rules
, cur
, value
))
1963 struct udev_list_entry
*list_entry
;
1966 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1967 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1972 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
1976 case TK_M_SUBSYSTEM
:
1977 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1981 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1984 case TK_M_WAITFOR
: {
1985 char filename
[UTIL_PATH_SIZE
];
1988 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
1989 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
1990 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
1995 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1999 case TK_M_SUBSYSTEMS
:
2005 /* get whole sequence of parent matches */
2007 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2010 /* loop over parents */
2011 event
->dev_parent
= event
->dev
;
2015 /* loop over sequence of parent match keys */
2016 for (key
= cur
; key
< next
; key
++ ) {
2017 dump_token(rules
, key
);
2020 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2023 case TK_M_SUBSYSTEMS
:
2024 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2028 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2032 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2036 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
2038 if (match
&& key
->key
.op
== OP_NOMATCH
)
2040 if (!match
&& key
->key
.op
== OP_MATCH
)
2051 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2052 if (event
->dev_parent
== NULL
)
2055 /* move behind our sequence of parent match keys */
2060 char filename
[UTIL_PATH_SIZE
];
2061 struct stat statbuf
;
2064 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2065 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2066 if (filename
[0] != '/') {
2067 char tmp
[UTIL_PATH_SIZE
];
2069 strscpy(tmp
, sizeof(tmp
), filename
);
2070 strscpyl(filename
, sizeof(filename
),
2071 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2074 attr_subst_subdir(filename
, sizeof(filename
));
2076 match
= (stat(filename
, &statbuf
) == 0);
2077 if (match
&& cur
->key
.mode
> 0)
2078 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2079 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2081 if (!match
&& cur
->key
.op
== OP_MATCH
)
2085 case TK_M_PROGRAM
: {
2086 char program
[UTIL_PATH_SIZE
];
2088 char result
[UTIL_LINE_SIZE
];
2090 free(event
->program_result
);
2091 event
->program_result
= NULL
;
2092 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2093 envp
= udev_device_get_properties_envp(event
->dev
);
2094 log_debug("PROGRAM '%s' %s:%u",
2096 rules_str(rules
, rule
->rule
.filename_off
),
2097 rule
->rule
.filename_line
);
2099 if (udev_event_spawn(event
, timeout_usec
, timeout_warn_usec
, program
, envp
, sigmask
, result
, sizeof(result
)) < 0) {
2100 if (cur
->key
.op
!= OP_NOMATCH
)
2105 util_remove_trailing_chars(result
, '\n');
2106 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2107 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2109 log_debug("%i character(s) replaced" , count
);
2111 event
->program_result
= strdup(result
);
2112 if (cur
->key
.op
== OP_NOMATCH
)
2117 case TK_M_IMPORT_FILE
: {
2118 char import
[UTIL_PATH_SIZE
];
2120 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2121 if (import_file_into_properties(event
->dev
, import
) != 0)
2122 if (cur
->key
.op
!= OP_NOMATCH
)
2126 case TK_M_IMPORT_PROG
: {
2127 char import
[UTIL_PATH_SIZE
];
2129 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2130 log_debug("IMPORT '%s' %s:%u",
2132 rules_str(rules
, rule
->rule
.filename_off
),
2133 rule
->rule
.filename_line
);
2135 if (import_program_into_properties(event
, timeout_usec
, timeout_warn_usec
, import
, sigmask
) != 0)
2136 if (cur
->key
.op
!= OP_NOMATCH
)
2140 case TK_M_IMPORT_BUILTIN
: {
2141 char command
[UTIL_PATH_SIZE
];
2143 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2144 /* check if we ran already */
2145 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2146 log_debug("IMPORT builtin skip '%s' %s:%u",
2147 udev_builtin_name(cur
->key
.builtin_cmd
),
2148 rules_str(rules
, rule
->rule
.filename_off
),
2149 rule
->rule
.filename_line
);
2150 /* return the result from earlier run */
2151 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2152 if (cur
->key
.op
!= OP_NOMATCH
)
2157 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2160 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2161 log_debug("IMPORT builtin '%s' %s:%u",
2162 udev_builtin_name(cur
->key
.builtin_cmd
),
2163 rules_str(rules
, rule
->rule
.filename_off
),
2164 rule
->rule
.filename_line
);
2166 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2167 /* remember failure */
2168 log_debug("IMPORT builtin '%s' returned non-zero",
2169 udev_builtin_name(cur
->key
.builtin_cmd
));
2170 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2171 if (cur
->key
.op
!= OP_NOMATCH
)
2176 case TK_M_IMPORT_DB
: {
2177 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2180 value
= udev_device_get_property_value(event
->dev_db
, key
);
2181 if (value
!= NULL
) {
2182 struct udev_list_entry
*entry
;
2184 entry
= udev_device_add_property(event
->dev
, key
, value
);
2185 udev_list_entry_set_num(entry
, true);
2187 if (cur
->key
.op
!= OP_NOMATCH
)
2192 case TK_M_IMPORT_CMDLINE
: {
2194 bool imported
= false;
2196 f
= fopen("/proc/cmdline", "re");
2200 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2201 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2204 pos
= strstr(cmdline
, key
);
2206 struct udev_list_entry
*entry
;
2209 if (pos
[0] == '\0' || isspace(pos
[0])) {
2210 /* we import simple flags as 'FLAG=1' */
2211 entry
= udev_device_add_property(event
->dev
, key
, "1");
2212 udev_list_entry_set_num(entry
, true);
2214 } else if (pos
[0] == '=') {
2219 while (pos
[0] != '\0' && !isspace(pos
[0]))
2222 entry
= udev_device_add_property(event
->dev
, key
, value
);
2223 udev_list_entry_set_num(entry
, true);
2230 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2234 case TK_M_IMPORT_PARENT
: {
2235 char import
[UTIL_PATH_SIZE
];
2237 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2238 if (import_parent_into_properties(event
->dev
, import
) != 0)
2239 if (cur
->key
.op
!= OP_NOMATCH
)
2244 if (match_key(rules
, cur
, event
->program_result
) != 0)
2247 case TK_A_STRING_ESCAPE_NONE
:
2250 case TK_A_STRING_ESCAPE_REPLACE
:
2251 esc
= ESCAPE_REPLACE
;
2253 case TK_A_DB_PERSIST
:
2254 udev_device_set_db_persist(event
->dev
);
2256 case TK_A_INOTIFY_WATCH
:
2257 if (event
->inotify_watch_final
)
2259 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2260 event
->inotify_watch_final
= true;
2261 event
->inotify_watch
= cur
->key
.watch
;
2263 case TK_A_DEVLINK_PRIO
:
2264 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2267 char owner
[UTIL_NAME_SIZE
];
2268 const char *ow
= owner
;
2271 if (event
->owner_final
)
2273 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2274 event
->owner_final
= true;
2275 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2276 event
->owner_set
= true;
2277 r
= get_user_creds(&ow
, &event
->uid
, NULL
, NULL
, NULL
);
2279 if (r
== -ENOENT
|| r
== -ESRCH
)
2280 log_error("specified user '%s' unknown\n", owner
);
2282 log_error("error resolving user '%s': %s\n", owner
, strerror(-r
));
2286 log_debug("OWNER %u %s:%u",
2288 rules_str(rules
, rule
->rule
.filename_off
),
2289 rule
->rule
.filename_line
);
2293 char group
[UTIL_NAME_SIZE
];
2294 const char *gr
= group
;
2297 if (event
->group_final
)
2299 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2300 event
->group_final
= true;
2301 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2302 event
->group_set
= true;
2303 r
= get_group_creds(&gr
, &event
->gid
);
2305 if (r
== -ENOENT
|| r
== -ESRCH
)
2306 log_error("specified group '%s' unknown\n", group
);
2308 log_error("error resolving group '%s': %s\n", group
, strerror(-r
));
2312 log_debug("GROUP %u %s:%u",
2314 rules_str(rules
, rule
->rule
.filename_off
),
2315 rule
->rule
.filename_line
);
2319 char mode_str
[UTIL_NAME_SIZE
];
2323 if (event
->mode_final
)
2325 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2326 mode
= strtol(mode_str
, &endptr
, 8);
2327 if (endptr
[0] != '\0') {
2328 log_error("ignoring invalid mode '%s'", mode_str
);
2331 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2332 event
->mode_final
= true;
2333 event
->mode_set
= true;
2335 log_debug("MODE %#o %s:%u",
2337 rules_str(rules
, rule
->rule
.filename_off
),
2338 rule
->rule
.filename_line
);
2342 if (event
->owner_final
)
2344 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2345 event
->owner_final
= true;
2346 event
->owner_set
= true;
2347 event
->uid
= cur
->key
.uid
;
2348 log_debug("OWNER %u %s:%u",
2350 rules_str(rules
, rule
->rule
.filename_off
),
2351 rule
->rule
.filename_line
);
2354 if (event
->group_final
)
2356 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2357 event
->group_final
= true;
2358 event
->group_set
= true;
2359 event
->gid
= cur
->key
.gid
;
2360 log_debug("GROUP %u %s:%u",
2362 rules_str(rules
, rule
->rule
.filename_off
),
2363 rule
->rule
.filename_line
);
2366 if (event
->mode_final
)
2368 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2369 event
->mode_final
= true;
2370 event
->mode_set
= true;
2371 event
->mode
= cur
->key
.mode
;
2372 log_debug("MODE %#o %s:%u",
2374 rules_str(rules
, rule
->rule
.filename_off
),
2375 rule
->rule
.filename_line
);
2377 case TK_A_SECLABEL
: {
2378 const char *name
, *label
;
2380 name
= rules_str(rules
, cur
->key
.attr_off
);
2381 label
= rules_str(rules
, cur
->key
.value_off
);
2382 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2383 udev_list_cleanup(&event
->seclabel_list
);
2384 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2385 log_debug("SECLABEL{%s}='%s' %s:%u",
2387 rules_str(rules
, rule
->rule
.filename_off
),
2388 rule
->rule
.filename_line
);
2392 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2393 char *value
= rules_str(rules
, cur
->key
.value_off
);
2394 char value_new
[UTIL_NAME_SIZE
];
2395 const char *value_old
= NULL
;
2396 struct udev_list_entry
*entry
;
2398 if (value
[0] == '\0') {
2399 if (cur
->key
.op
== OP_ADD
)
2401 udev_device_add_property(event
->dev
, name
, NULL
);
2405 if (cur
->key
.op
== OP_ADD
)
2406 value_old
= udev_device_get_property_value(event
->dev
, name
);
2408 char temp
[UTIL_NAME_SIZE
];
2410 /* append value separated by space */
2411 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2412 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2414 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2416 entry
= udev_device_add_property(event
->dev
, name
, value_new
);
2417 /* store in db, skip private keys */
2419 udev_list_entry_set_num(entry
, true);
2423 char tag
[UTIL_PATH_SIZE
];
2426 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2427 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2428 udev_device_cleanup_tags_list(event
->dev
);
2429 for (p
= tag
; *p
!= '\0'; p
++) {
2430 if ((*p
>= 'a' && *p
<= 'z') ||
2431 (*p
>= 'A' && *p
<= 'Z') ||
2432 (*p
>= '0' && *p
<= '9') ||
2433 *p
== '-' || *p
== '_')
2435 log_error("ignoring invalid tag name '%s'", tag
);
2438 if (cur
->key
.op
== OP_REMOVE
)
2439 udev_device_remove_tag(event
->dev
, tag
);
2441 udev_device_add_tag(event
->dev
, tag
);
2445 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2447 char name_str
[UTIL_PATH_SIZE
];
2450 if (event
->name_final
)
2452 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2453 event
->name_final
= true;
2454 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2455 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2456 count
= util_replace_chars(name_str
, "/");
2458 log_debug("%i character(s) replaced", count
);
2460 if (major(udev_device_get_devnum(event
->dev
)) &&
2461 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2462 log_error("NAME=\"%s\" ignored, kernel device nodes "
2463 "can not be renamed; please fix it in %s:%u\n", name
,
2464 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2468 event
->name
= strdup(name_str
);
2469 log_debug("NAME '%s' %s:%u",
2471 rules_str(rules
, rule
->rule
.filename_off
),
2472 rule
->rule
.filename_line
);
2475 case TK_A_DEVLINK
: {
2476 char temp
[UTIL_PATH_SIZE
];
2477 char filename
[UTIL_PATH_SIZE
];
2481 if (event
->devlink_final
)
2483 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2485 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2486 event
->devlink_final
= true;
2487 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2488 udev_device_cleanup_devlinks_list(event
->dev
);
2490 /* allow multiple symlinks separated by spaces */
2491 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2492 if (esc
== ESCAPE_UNSET
)
2493 count
= util_replace_chars(temp
, "/ ");
2494 else if (esc
== ESCAPE_REPLACE
)
2495 count
= util_replace_chars(temp
, "/");
2497 log_debug("%i character(s) replaced" , count
);
2499 while (isspace(pos
[0]))
2501 next
= strchr(pos
, ' ');
2502 while (next
!= NULL
) {
2504 log_debug("LINK '%s' %s:%u", pos
,
2505 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2506 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2507 udev_device_add_devlink(event
->dev
, filename
);
2508 while (isspace(next
[1]))
2511 next
= strchr(pos
, ' ');
2513 if (pos
[0] != '\0') {
2514 log_debug("LINK '%s' %s:%u", pos
,
2515 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2516 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2517 udev_device_add_devlink(event
->dev
, filename
);
2522 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2523 char attr
[UTIL_PATH_SIZE
];
2524 char value
[UTIL_NAME_SIZE
];
2527 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2528 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2529 attr_subst_subdir(attr
, sizeof(attr
));
2531 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2532 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2533 rules_str(rules
, rule
->rule
.filename_off
),
2534 rule
->rule
.filename_line
);
2535 f
= fopen(attr
, "we");
2537 if (fprintf(f
, "%s", value
) <= 0)
2538 log_error("error writing ATTR{%s}: %m", attr
);
2541 log_error("error opening ATTR{%s} for writing: %m", attr
);
2545 case TK_A_RUN_BUILTIN
:
2546 case TK_A_RUN_PROGRAM
: {
2547 struct udev_list_entry
*entry
;
2549 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2550 udev_list_cleanup(&event
->run_list
);
2551 log_debug("RUN '%s' %s:%u",
2552 rules_str(rules
, cur
->key
.value_off
),
2553 rules_str(rules
, rule
->rule
.filename_off
),
2554 rule
->rule
.filename_line
);
2555 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2556 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2560 if (cur
->key
.rule_goto
== 0)
2562 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2567 case TK_M_PARENTS_MIN
:
2568 case TK_M_PARENTS_MAX
:
2571 log_error("wrong type %u", cur
->type
);
2578 /* fast-forward to next rule */
2579 cur
= rule
+ rule
->rule
.token_count
;
2583 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
) {
2589 _cleanup_strv_free_
char **tags
= NULL
;
2592 _cleanup_free_
char *path
= NULL
;
2595 if (rules
->tokens
== NULL
)
2598 cur
= &rules
->tokens
[0];
2601 switch (cur
->type
) {
2606 /* skip rules without a static_node tag */
2607 if (!rule
->rule
.has_static_node
)
2623 mode
= cur
->key
.mode
;
2626 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2631 case TK_A_STATIC_NODE
: {
2632 char device_node
[UTIL_PATH_SIZE
];
2633 char tags_dir
[UTIL_PATH_SIZE
];
2634 char tag_symlink
[UTIL_PATH_SIZE
];
2637 /* we assure, that the permissions tokens are sorted before the static token */
2639 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2642 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2643 if (stat(device_node
, &stats
) != 0)
2645 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2648 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2650 STRV_FOREACH(t
, tags
) {
2651 _cleanup_free_
char *unescaped_filename
= NULL
;
2653 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2654 r
= mkdir_p(tags_dir
, 0755);
2656 log_error_errno(-r
, "failed to create %s: %m", tags_dir
);
2660 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2662 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2663 r
= symlink(device_node
, tag_symlink
);
2664 if (r
< 0 && errno
!= EEXIST
) {
2665 log_error("failed to create symlink %s -> %s: %m", tag_symlink
, device_node
);
2672 /* don't touch the permissions if only the tags were set */
2673 if (mode
== 0 && uid
== 0 && gid
== 0)
2682 if (mode
!= (stats
.st_mode
& 01777)) {
2683 r
= chmod(device_node
, mode
);
2685 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2688 log_debug("chmod '%s' %#o", device_node
, mode
);
2691 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2692 r
= chown(device_node
, uid
, gid
);
2694 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2697 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2700 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2710 /* fast-forward to next rule */
2711 cur
= rule
+ rule
->rule
.token_count
;
2718 fchmod(fileno(f
), 0644);
2719 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2721 unlink("/run/udev/static_node-tags");