2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "path-util.h"
34 #include "conf-files.h"
39 #define PREALLOC_TOKEN 2048
42 unsigned int name_off
;
49 static const char* const rules_dirs
[] = {
52 UDEVLIBEXECDIR
"/rules.d",
60 /* every key in the rules file becomes a token */
62 unsigned int token_cur
;
63 unsigned int token_max
;
65 /* all key strings are copied and de-duplicated in a single continuous string buffer */
66 struct strbuf
*strbuf
;
68 /* during rule parsing, uid/gid lookup results are cached */
70 unsigned int uids_cur
;
71 unsigned int uids_max
;
73 unsigned int gids_cur
;
74 unsigned int gids_max
;
77 static char *rules_str(struct udev_rules
*rules
, unsigned int off
) {
78 return rules
->strbuf
->buf
+ off
;
81 static unsigned int rules_add_string(struct udev_rules
*rules
, const char *s
) {
82 return strbuf_add_string(rules
->strbuf
, s
, strlen(s
));
85 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
98 enum string_glob_type
{
100 GL_PLAIN
, /* no special chars */
101 GL_GLOB
, /* shell globs ?,*,[] */
102 GL_SPLIT
, /* multi-value A|B */
103 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
104 GL_SOMETHING
, /* commonly used "?*" */
107 enum string_subst_type
{
114 /* tokens of a rule are sorted/handled in this order */
119 TK_M_ACTION
, /* val */
120 TK_M_DEVPATH
, /* val */
121 TK_M_KERNEL
, /* val */
122 TK_M_DEVLINK
, /* val */
124 TK_M_ENV
, /* val, attr */
126 TK_M_SUBSYSTEM
, /* val */
127 TK_M_DRIVER
, /* val */
128 TK_M_WAITFOR
, /* val */
129 TK_M_ATTR
, /* val, attr */
132 TK_M_KERNELS
, /* val */
133 TK_M_SUBSYSTEMS
, /* val */
134 TK_M_DRIVERS
, /* val */
135 TK_M_ATTRS
, /* val, attr */
139 TK_M_TEST
, /* val, mode_t */
140 TK_M_EVENT_TIMEOUT
, /* int */
141 TK_M_PROGRAM
, /* val */
142 TK_M_IMPORT_FILE
, /* val */
143 TK_M_IMPORT_PROG
, /* val */
144 TK_M_IMPORT_BUILTIN
, /* val */
145 TK_M_IMPORT_DB
, /* val */
146 TK_M_IMPORT_CMDLINE
, /* val */
147 TK_M_IMPORT_PARENT
, /* val */
148 TK_M_RESULT
, /* val */
151 TK_A_STRING_ESCAPE_NONE
,
152 TK_A_STRING_ESCAPE_REPLACE
,
154 TK_A_INOTIFY_WATCH
, /* int */
155 TK_A_DEVLINK_PRIO
, /* int */
156 TK_A_OWNER
, /* val */
157 TK_A_GROUP
, /* val */
159 TK_A_OWNER_ID
, /* uid_t */
160 TK_A_GROUP_ID
, /* gid_t */
161 TK_A_MODE_ID
, /* mode_t */
163 TK_A_STATIC_NODE
, /* val */
164 TK_A_SECLABEL
, /* val, attr */
165 TK_A_ENV
, /* val, attr */
167 TK_A_DEVLINK
, /* val */
168 TK_A_ATTR
, /* val, attr */
169 TK_A_RUN_BUILTIN
, /* val, bool */
170 TK_A_RUN_PROGRAM
, /* val, bool */
171 TK_A_GOTO
, /* size_t */
176 /* we try to pack stuff in a way that we take only 12 bytes per token */
179 unsigned char type
; /* same in rule and key */
181 enum token_type type
:8;
183 bool has_static_node
:1;
184 unsigned int unused
:6;
185 unsigned short token_count
;
186 unsigned int label_off
;
187 unsigned short filename_off
;
188 unsigned short filename_line
;
191 enum token_type type
:8;
192 enum operation_type op
:8;
193 enum string_glob_type glob
:8;
194 enum string_subst_type subst
:4;
195 enum string_subst_type attrsubst
:4;
196 unsigned int value_off
;
198 unsigned int attr_off
;
199 unsigned int rule_goto
;
206 enum udev_builtin_cmd builtin_cmd
;
214 struct udev_rules
*rules
;
216 struct token token
[MAX_TK
];
217 unsigned int token_cur
;
221 static const char *operation_str(enum operation_type type
)
223 static const char *operation_strs
[] = {
224 [OP_UNSET
] = "UNSET",
225 [OP_MATCH
] = "match",
226 [OP_NOMATCH
] = "nomatch",
227 [OP_MATCH_MAX
] = "MATCH_MAX",
230 [OP_ASSIGN
] = "assign",
231 [OP_ASSIGN_FINAL
] = "assign-final",
234 return operation_strs
[type
];
237 static const char *string_glob_str(enum string_glob_type type
)
239 static const char *string_glob_strs
[] = {
240 [GL_UNSET
] = "UNSET",
241 [GL_PLAIN
] = "plain",
243 [GL_SPLIT
] = "split",
244 [GL_SPLIT_GLOB
] = "split-glob",
245 [GL_SOMETHING
] = "split-glob",
248 return string_glob_strs
[type
];
251 static const char *token_str(enum token_type type
)
253 static const char *token_strs
[] = {
254 [TK_UNSET
] = "UNSET",
257 [TK_M_ACTION
] = "M ACTION",
258 [TK_M_DEVPATH
] = "M DEVPATH",
259 [TK_M_KERNEL
] = "M KERNEL",
260 [TK_M_DEVLINK
] = "M DEVLINK",
261 [TK_M_NAME
] = "M NAME",
262 [TK_M_ENV
] = "M ENV",
263 [TK_M_TAG
] = "M TAG",
264 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
265 [TK_M_DRIVER
] = "M DRIVER",
266 [TK_M_WAITFOR
] = "M WAITFOR",
267 [TK_M_ATTR
] = "M ATTR",
269 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
270 [TK_M_KERNELS
] = "M KERNELS",
271 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
272 [TK_M_DRIVERS
] = "M DRIVERS",
273 [TK_M_ATTRS
] = "M ATTRS",
274 [TK_M_TAGS
] = "M TAGS",
275 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
277 [TK_M_TEST
] = "M TEST",
278 [TK_M_EVENT_TIMEOUT
] = "M EVENT_TIMEOUT",
279 [TK_M_PROGRAM
] = "M PROGRAM",
280 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
281 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
282 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
283 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
284 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
285 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
286 [TK_M_RESULT
] = "M RESULT",
287 [TK_M_MAX
] = "M MAX",
289 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
290 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
291 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
292 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
293 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
294 [TK_A_OWNER
] = "A OWNER",
295 [TK_A_GROUP
] = "A GROUP",
296 [TK_A_MODE
] = "A MODE",
297 [TK_A_OWNER_ID
] = "A OWNER_ID",
298 [TK_A_GROUP_ID
] = "A GROUP_ID",
299 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
300 [TK_A_SECLABEL
] = "A SECLABEL",
301 [TK_A_MODE_ID
] = "A MODE_ID",
302 [TK_A_ENV
] = "A ENV",
303 [TK_A_TAG
] = "A ENV",
304 [TK_A_NAME
] = "A NAME",
305 [TK_A_DEVLINK
] = "A DEVLINK",
306 [TK_A_ATTR
] = "A ATTR",
307 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
308 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
309 [TK_A_GOTO
] = "A GOTO",
314 return token_strs
[type
];
317 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
319 enum token_type type
= token
->type
;
320 enum operation_type op
= token
->key
.op
;
321 enum string_glob_type glob
= token
->key
.glob
;
322 const char *value
= str(rules
, token
->key
.value_off
);
323 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
328 const char *tks_ptr
= (char *)rules
->tokens
;
329 const char *tk_ptr
= (char *)token
;
330 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
332 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
333 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
334 idx
, token
->rule
.token_count
,
335 &rules
->buf
[token
->rule
.label_off
]);
347 case TK_M_SUBSYSTEMS
:
351 case TK_M_IMPORT_FILE
:
352 case TK_M_IMPORT_PROG
:
354 case TK_M_IMPORT_CMDLINE
:
355 case TK_M_IMPORT_PARENT
:
362 case TK_A_RUN_BUILTIN
:
363 case TK_A_RUN_PROGRAM
:
364 log_debug("%s %s '%s'(%s)",
365 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
367 case TK_M_IMPORT_BUILTIN
:
368 log_debug("%s %i '%s'", token_str(type
), token
->key
.builtin_cmd
, value
);
375 log_debug("%s %s '%s' '%s'(%s)",
376 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
380 log_debug("%s %s '%s'", token_str(type
), operation_str(op
), value
);
382 case TK_A_STRING_ESCAPE_NONE
:
383 case TK_A_STRING_ESCAPE_REPLACE
:
384 case TK_A_DB_PERSIST
:
385 log_debug("%s", token_str(type
));
388 log_debug("%s %s '%s'(%s) %#o",
389 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
391 case TK_A_INOTIFY_WATCH
:
392 log_debug("%s %u", token_str(type
), token
->key
.watch
);
394 case TK_A_DEVLINK_PRIO
:
395 log_debug("%s %u", token_str(type
), token
->key
.devlink_prio
);
398 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.uid
);
401 log_debug("%s %s %u", token_str(type
), operation_str(op
), token
->key
.gid
);
404 log_debug("%s %s %#o", token_str(type
), operation_str(op
), token
->key
.mode
);
406 case TK_A_STATIC_NODE
:
407 log_debug("%s '%s'", token_str(type
), value
);
410 log_debug("%s %s '%s' '%s'", token_str(type
), operation_str(op
), attr
, value
);
412 case TK_M_EVENT_TIMEOUT
:
413 log_debug("%s %u", token_str(type
), token
->key
.event_timeout
);
416 log_debug("%s '%s' %u", token_str(type
), value
, token
->key
.rule_goto
);
419 log_debug("* %s", token_str(type
));
421 case TK_M_PARENTS_MIN
:
422 case TK_M_PARENTS_MAX
:
425 log_debug("unknown type %u", type
);
430 static void dump_rules(struct udev_rules
*rules
)
434 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
436 rules
->token_cur
* sizeof(struct token
),
439 for (i
= 0; i
< rules
->token_cur
; i
++)
440 dump_token(rules
, &rules
->tokens
[i
]);
443 static inline const char *operation_str(enum operation_type type
) { return NULL
; }
444 static inline const char *token_str(enum token_type type
) { return NULL
; }
445 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
446 static inline void dump_rules(struct udev_rules
*rules
) {}
449 static int add_token(struct udev_rules
*rules
, struct token
*token
)
451 /* grow buffer if needed */
452 if (rules
->token_cur
+1 >= rules
->token_max
) {
453 struct token
*tokens
;
456 /* double the buffer size */
457 add
= rules
->token_max
;
461 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
464 rules
->tokens
= tokens
;
465 rules
->token_max
+= add
;
467 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
472 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
478 /* lookup, if we know it already */
479 for (i
= 0; i
< rules
->uids_cur
; i
++) {
480 off
= rules
->uids
[i
].name_off
;
481 if (streq(rules_str(rules
, off
), owner
)) {
482 uid
= rules
->uids
[i
].uid
;
486 uid
= util_lookup_user(rules
->udev
, owner
);
488 /* grow buffer if needed */
489 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
490 struct uid_gid
*uids
;
493 /* double the buffer size */
494 add
= rules
->uids_max
;
498 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
502 rules
->uids_max
+= add
;
504 rules
->uids
[rules
->uids_cur
].uid
= uid
;
505 off
= rules_add_string(rules
, owner
);
508 rules
->uids
[rules
->uids_cur
].name_off
= off
;
513 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
519 /* lookup, if we know it already */
520 for (i
= 0; i
< rules
->gids_cur
; i
++) {
521 off
= rules
->gids
[i
].name_off
;
522 if (streq(rules_str(rules
, off
), group
)) {
523 gid
= rules
->gids
[i
].gid
;
527 gid
= util_lookup_group(rules
->udev
, group
);
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
)
559 struct udev_list_entry
*entry
;
563 while (isspace(key
[0]))
566 /* comment or empty line */
567 if (key
[0] == '#' || key
[0] == '\0')
570 /* split key/value */
571 val
= strchr(key
, '=');
578 while (isspace(val
[0]))
585 while (isspace(key
[len
-1]))
589 /* terminate value */
593 while (isspace(val
[len
-1]))
601 if (val
[0] == '"' || val
[0] == '\'') {
602 if (val
[len
-1] != val
[0]) {
603 log_debug("inconsistent quoting: '%s', skip", line
);
610 entry
= udev_device_add_property(dev
, key
, val
);
611 /* store in db, skip private keys */
613 udev_list_entry_set_num(entry
, true);
618 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
621 char line
[UTIL_LINE_SIZE
];
623 f
= fopen(filename
, "re");
626 while (fgets(line
, sizeof(line
), f
) != NULL
)
627 import_property_from_string(dev
, line
);
632 static int import_program_into_properties(struct udev_event
*event
, 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
, 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
)
662 struct udev_device
*dev_parent
;
663 struct udev_list_entry
*list_entry
;
665 dev_parent
= udev_device_get_parent(dev
);
666 if (dev_parent
== NULL
)
669 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
670 const char *key
= udev_list_entry_get_name(list_entry
);
671 const char *val
= udev_list_entry_get_value(list_entry
);
673 if (fnmatch(filter
, key
, 0) == 0) {
674 struct udev_list_entry
*entry
;
676 entry
= udev_device_add_property(dev
, key
, val
);
677 /* store in db, skip private keys */
679 udev_list_entry_set_num(entry
, true);
685 #define WAIT_LOOP_PER_SECOND 50
686 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
688 char filepath
[UTIL_PATH_SIZE
];
689 char devicepath
[UTIL_PATH_SIZE
];
691 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
693 /* a relative path is a device attribute */
694 devicepath
[0] = '\0';
695 if (file
[0] != '/') {
696 strscpyl(devicepath
, sizeof(devicepath
), udev_device_get_syspath(dev
), NULL
);
697 strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
702 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
705 if (stat(file
, &stats
) == 0) {
706 log_debug("file '%s' appeared after %i loops", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
709 /* make sure, the device did not disappear in the meantime */
710 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
711 log_debug("device disappeared while waiting for '%s'", file
);
714 log_debug("wait for '%s' for %i mseconds", file
, 1000 / WAIT_LOOP_PER_SECOND
);
715 nanosleep(&duration
, NULL
);
717 log_debug("waiting for '%s' failed", file
);
721 static int attr_subst_subdir(char *attr
, size_t len
)
725 if (strstr(attr
, "/*/")) {
727 char dirname
[UTIL_PATH_SIZE
];
731 strscpy(dirname
, sizeof(dirname
), attr
);
732 pos
= strstr(dirname
, "/*/");
737 dir
= opendir(dirname
);
741 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
744 if (dent
->d_name
[0] == '.')
746 strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
747 if (stat(attr
, &stats
) == 0) {
759 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
765 if (linepos
== NULL
|| linepos
[0] == '\0')
768 /* skip whitespace */
769 while (isspace(linepos
[0]) || linepos
[0] == ',')
773 if (linepos
[0] == '\0')
779 if (linepos
[0] == '\0')
781 if (isspace(linepos
[0]))
783 if (linepos
[0] == '=')
785 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
786 if (linepos
[1] == '=')
790 /* remember end of key */
793 /* skip whitespace after key */
794 while (isspace(linepos
[0]))
796 if (linepos
[0] == '\0')
799 /* get operation type */
800 if (linepos
[0] == '=' && linepos
[1] == '=') {
803 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
806 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
809 } else if (linepos
[0] == '=') {
812 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
813 *op
= OP_ASSIGN_FINAL
;
821 /* skip whitespace after operator */
822 while (isspace(linepos
[0]))
824 if (linepos
[0] == '\0')
828 if (linepos
[0] == '"')
835 temp
= strchr(linepos
, '"');
841 /* move line to next key */
846 /* extract possible KEY{attr} */
847 static const char *get_key_attribute(struct udev
*udev
, char *str
)
852 attr
= strchr(str
, '{');
855 pos
= strchr(attr
, '}');
857 log_error("missing closing brace for format");
866 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
867 enum operation_type op
,
868 const char *value
, const void *data
)
870 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
871 const char *attr
= NULL
;
873 memset(token
, 0x00, sizeof(struct token
));
885 case TK_M_SUBSYSTEMS
:
889 case TK_M_IMPORT_FILE
:
890 case TK_M_IMPORT_PROG
:
892 case TK_M_IMPORT_CMDLINE
:
893 case TK_M_IMPORT_PARENT
:
903 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
905 case TK_M_IMPORT_BUILTIN
:
906 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
907 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
916 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
917 token
->key
.attr_off
= rules_add_string(rule_tmp
->rules
, attr
);
920 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
922 token
->key
.mode
= *(mode_t
*)data
;
924 case TK_A_STRING_ESCAPE_NONE
:
925 case TK_A_STRING_ESCAPE_REPLACE
:
926 case TK_A_DB_PERSIST
:
928 case TK_A_RUN_BUILTIN
:
929 case TK_A_RUN_PROGRAM
:
930 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
931 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
933 case TK_A_INOTIFY_WATCH
:
934 case TK_A_DEVLINK_PRIO
:
935 token
->key
.devlink_prio
= *(int *)data
;
938 token
->key
.uid
= *(uid_t
*)data
;
941 token
->key
.gid
= *(gid_t
*)data
;
944 token
->key
.mode
= *(mode_t
*)data
;
946 case TK_A_STATIC_NODE
:
947 token
->key
.value_off
= rules_add_string(rule_tmp
->rules
, value
);
949 case TK_M_EVENT_TIMEOUT
:
950 token
->key
.event_timeout
= *(int *)data
;
953 case TK_M_PARENTS_MIN
:
954 case TK_M_PARENTS_MAX
:
958 log_error("wrong type %u", type
);
962 if (value
!= NULL
&& type
< TK_M_MAX
) {
963 /* check if we need to split or call fnmatch() while matching rules */
964 enum string_glob_type glob
;
968 has_split
= (strchr(value
, '|') != NULL
);
969 has_glob
= string_is_glob(value
);
970 if (has_split
&& has_glob
) {
971 glob
= GL_SPLIT_GLOB
;
972 } else if (has_split
) {
974 } else if (has_glob
) {
975 if (streq(value
, "?*"))
982 token
->key
.glob
= glob
;
985 if (value
!= NULL
&& type
> TK_M_MAX
) {
986 /* check if assigned value has substitution chars */
988 token
->key
.subst
= SB_SUBSYS
;
989 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
990 token
->key
.subst
= SB_FORMAT
;
992 token
->key
.subst
= SB_NONE
;
996 /* check if property/attribut name has substitution chars */
998 token
->key
.attrsubst
= SB_SUBSYS
;
999 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
1000 token
->key
.attrsubst
= SB_FORMAT
;
1002 token
->key
.attrsubst
= SB_NONE
;
1005 token
->key
.type
= type
;
1007 rule_tmp
->token_cur
++;
1008 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
1009 log_error("temporary rule array too small");
1015 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1018 unsigned int start
= 0;
1019 unsigned int end
= rule_tmp
->token_cur
;
1021 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1022 enum token_type next_val
= TK_UNSET
;
1023 unsigned int next_idx
= 0;
1026 /* find smallest value */
1027 for (j
= start
; j
< end
; j
++) {
1028 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1030 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1031 next_val
= rule_tmp
->token
[j
].type
;
1036 /* add token and mark done */
1037 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1039 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1042 if (next_idx
== start
)
1044 if (next_idx
+1 == end
)
1050 static int add_rule(struct udev_rules
*rules
, char *line
,
1051 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1055 struct rule_tmp rule_tmp
;
1057 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1058 rule_tmp
.rules
= rules
;
1059 rule_tmp
.rule
.type
= TK_RULE
;
1060 /* the offset in the rule is limited to unsigned short */
1061 if (filename_off
< USHRT_MAX
)
1062 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1063 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1069 enum operation_type op
;
1071 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0) {
1072 /* Avoid erroring on trailing whitespace. This is probably rare
1073 * so save the work for the error case instead of always trying
1074 * to strip the trailing whitespace with strstrip(). */
1075 while (isblank(*linepos
))
1078 /* If we aren't at the end of the line, this is a parsing error.
1079 * Make a best effort to describe where the problem is. */
1080 if (*linepos
!= '\n') {
1081 char buf
[2] = {linepos
[1]};
1082 _cleanup_free_
char *tmp
;
1085 log_error("invalid key/value pair in file %s on line %u,"
1086 "starting at character %tu ('%s')\n",
1087 filename
, lineno
, linepos
- line
+ 1, tmp
);
1088 if (linepos
[1] == '#')
1089 log_error("hint: comments can only start at beginning of line");
1094 if (streq(key
, "ACTION")) {
1095 if (op
> OP_MATCH_MAX
) {
1096 log_error("invalid ACTION operation");
1099 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1103 if (streq(key
, "DEVPATH")) {
1104 if (op
> OP_MATCH_MAX
) {
1105 log_error("invalid DEVPATH operation");
1108 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1112 if (streq(key
, "KERNEL")) {
1113 if (op
> OP_MATCH_MAX
) {
1114 log_error("invalid KERNEL operation");
1117 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1121 if (streq(key
, "SUBSYSTEM")) {
1122 if (op
> OP_MATCH_MAX
) {
1123 log_error("invalid SUBSYSTEM operation");
1126 /* bus, class, subsystem events should all be the same */
1127 if (streq(value
, "subsystem") ||
1128 streq(value
, "bus") ||
1129 streq(value
, "class")) {
1130 if (streq(value
, "bus") || streq(value
, "class"))
1131 log_error("'%s' must be specified as 'subsystem' "
1132 "please fix it in %s:%u", value
, filename
, lineno
);
1133 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1135 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1139 if (streq(key
, "DRIVER")) {
1140 if (op
> OP_MATCH_MAX
) {
1141 log_error("invalid DRIVER operation");
1144 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1148 if (startswith(key
, "ATTR{")) {
1149 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1151 log_error("error parsing ATTR attribute");
1154 if (op
< OP_MATCH_MAX
) {
1155 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1157 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1162 if (startswith(key
, "SECLABEL{")) {
1163 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("SECLABEL")-1);
1165 log_error("error parsing SECLABEL attribute");
1169 rule_add_key(&rule_tmp
, TK_A_SECLABEL
, op
, value
, attr
);
1173 if (streq(key
, "KERNELS")) {
1174 if (op
> OP_MATCH_MAX
) {
1175 log_error("invalid KERNELS operation");
1178 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1182 if (streq(key
, "SUBSYSTEMS")) {
1183 if (op
> OP_MATCH_MAX
) {
1184 log_error("invalid SUBSYSTEMS operation");
1187 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1191 if (streq(key
, "DRIVERS")) {
1192 if (op
> OP_MATCH_MAX
) {
1193 log_error("invalid DRIVERS operation");
1196 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1200 if (startswith(key
, "ATTRS{")) {
1201 if (op
> OP_MATCH_MAX
) {
1202 log_error("invalid ATTRS operation");
1205 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1207 log_error("error parsing ATTRS attribute");
1210 if (startswith(attr
, "device/"))
1211 log_error("the 'device' link may not be available in a future kernel, "
1212 "please fix it in %s:%u", filename
, lineno
);
1213 else if (strstr(attr
, "../") != NULL
)
1214 log_error("do not reference parent sysfs directories directly, "
1215 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1216 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1220 if (streq(key
, "TAGS")) {
1221 if (op
> OP_MATCH_MAX
) {
1222 log_error("invalid TAGS operation");
1225 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1229 if (startswith(key
, "ENV{")) {
1230 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1232 log_error("error parsing ENV attribute");
1235 if (op
< OP_MATCH_MAX
) {
1236 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1239 static const char *blacklist
[] = {
1254 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1255 if (!streq(attr
, blacklist
[i
]))
1257 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr
, filename
, lineno
);
1260 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1266 if (streq(key
, "TAG")) {
1267 if (op
< OP_MATCH_MAX
)
1268 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1270 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1274 if (streq(key
, "PROGRAM")) {
1275 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1279 if (streq(key
, "RESULT")) {
1280 if (op
> OP_MATCH_MAX
) {
1281 log_error("invalid RESULT operation");
1284 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1288 if (startswith(key
, "IMPORT")) {
1289 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1291 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename
, lineno
);
1294 if (streq(attr
, "program")) {
1295 /* find known built-in command */
1296 if (value
[0] != '/') {
1297 enum udev_builtin_cmd cmd
;
1299 cmd
= udev_builtin_lookup(value
);
1300 if (cmd
< UDEV_BUILTIN_MAX
) {
1301 log_debug("IMPORT found builtin '%s', replacing %s:%u",
1302 value
, filename
, lineno
);
1303 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1307 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1308 } else if (streq(attr
, "builtin")) {
1309 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1311 if (cmd
< UDEV_BUILTIN_MAX
)
1312 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1314 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1315 } else if (streq(attr
, "file")) {
1316 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1317 } else if (streq(attr
, "db")) {
1318 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1319 } else if (streq(attr
, "cmdline")) {
1320 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1321 } else if (streq(attr
, "parent")) {
1322 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1324 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename
, lineno
);
1328 if (startswith(key
, "TEST")) {
1331 if (op
> OP_MATCH_MAX
) {
1332 log_error("invalid TEST operation");
1335 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1337 mode
= strtol(attr
, NULL
, 8);
1338 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1340 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1345 if (startswith(key
, "RUN")) {
1346 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1350 if (streq(attr
, "builtin")) {
1351 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1353 if (cmd
< UDEV_BUILTIN_MAX
)
1354 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1356 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value
, filename
, lineno
);
1357 } else if (streq(attr
, "program")) {
1358 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1360 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1362 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename
, lineno
);
1368 if (streq(key
, "WAIT_FOR") || streq(key
, "WAIT_FOR_SYSFS")) {
1369 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1373 if (streq(key
, "LABEL")) {
1374 rule_tmp
.rule
.rule
.label_off
= rules_add_string(rules
, value
);
1378 if (streq(key
, "GOTO")) {
1379 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1383 if (startswith(key
, "NAME")) {
1384 if (op
< OP_MATCH_MAX
) {
1385 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1387 if (streq(value
, "%k")) {
1388 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1389 "please remove it from %s:%u\n", filename
, lineno
);
1392 if (value
[0] == '\0') {
1393 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1394 "please remove it from %s:%u\n", filename
, lineno
);
1397 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1399 rule_tmp
.rule
.rule
.can_set_name
= true;
1403 if (streq(key
, "SYMLINK")) {
1404 if (op
< OP_MATCH_MAX
)
1405 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1407 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1408 rule_tmp
.rule
.rule
.can_set_name
= true;
1412 if (streq(key
, "OWNER")) {
1416 uid
= strtoul(value
, &endptr
, 10);
1417 if (endptr
[0] == '\0') {
1418 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1419 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1420 uid
= add_uid(rules
, value
);
1421 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1422 } else if (rules
->resolve_names
>= 0) {
1423 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1425 rule_tmp
.rule
.rule
.can_set_name
= true;
1429 if (streq(key
, "GROUP")) {
1433 gid
= strtoul(value
, &endptr
, 10);
1434 if (endptr
[0] == '\0') {
1435 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1436 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1437 gid
= add_gid(rules
, value
);
1438 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1439 } else if (rules
->resolve_names
>= 0) {
1440 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1442 rule_tmp
.rule
.rule
.can_set_name
= true;
1446 if (streq(key
, "MODE")) {
1450 mode
= strtol(value
, &endptr
, 8);
1451 if (endptr
[0] == '\0')
1452 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1454 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1455 rule_tmp
.rule
.rule
.can_set_name
= true;
1459 if (streq(key
, "OPTIONS")) {
1462 pos
= strstr(value
, "link_priority=");
1464 int prio
= atoi(&pos
[strlen("link_priority=")]);
1466 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1469 pos
= strstr(value
, "event_timeout=");
1471 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1473 rule_add_key(&rule_tmp
, TK_M_EVENT_TIMEOUT
, op
, NULL
, &tout
);
1476 pos
= strstr(value
, "string_escape=");
1478 pos
= &pos
[strlen("string_escape=")];
1479 if (startswith(pos
, "none"))
1480 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1481 else if (startswith(pos
, "replace"))
1482 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1485 pos
= strstr(value
, "db_persist");
1487 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1489 pos
= strstr(value
, "nowatch");
1493 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1495 pos
= strstr(value
, "watch");
1499 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1503 pos
= strstr(value
, "static_node=");
1505 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1506 rule_tmp
.rule
.rule
.has_static_node
= true;
1512 log_error("unknown key '%s' in %s:%u", key
, filename
, lineno
);
1516 /* add rule token */
1517 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1518 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1521 /* add tokens to list, sorted by type */
1522 if (sort_token(rules
, &rule_tmp
) != 0)
1527 log_error("invalid rule '%s:%u'", filename
, lineno
);
1531 static int parse_file(struct udev_rules
*rules
, const char *filename
)
1534 unsigned int first_token
;
1535 unsigned int filename_off
;
1536 char line
[UTIL_LINE_SIZE
];
1540 if (null_or_empty_path(filename
)) {
1541 log_debug("skip empty file: %s", filename
);
1544 log_debug("read rules file: %s", filename
);
1546 f
= fopen(filename
, "re");
1550 first_token
= rules
->token_cur
;
1551 filename_off
= rules_add_string(rules
, filename
);
1553 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1557 /* skip whitespace */
1560 while (isspace(key
[0]))
1571 /* continue reading if backslash+newline is found */
1572 while (line
[len
-2] == '\\') {
1573 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1575 if (strlen(&line
[len
-2]) < 2)
1581 if (len
+1 >= sizeof(line
)) {
1582 log_error("line too long '%s':%u, ignored", filename
, line_nr
);
1585 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1589 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1590 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1591 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1592 char *label
= rules_str(rules
, rules
->tokens
[i
].key
.value_off
);
1595 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1596 if (rules
->tokens
[j
].type
!= TK_RULE
)
1598 if (rules
->tokens
[j
].rule
.label_off
== 0)
1600 if (!streq(label
, rules_str(rules
, rules
->tokens
[j
].rule
.label_off
)))
1602 rules
->tokens
[i
].key
.rule_goto
= j
;
1605 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1606 log_error("GOTO '%s' has no matching label in: '%s'", label
, filename
);
1612 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1614 struct udev_rules
*rules
;
1615 struct udev_list file_list
;
1616 struct token end_token
;
1620 rules
= calloc(1, sizeof(struct udev_rules
));
1624 rules
->resolve_names
= resolve_names
;
1625 udev_list_init(udev
, &file_list
, true);
1627 /* init token array and string buffer */
1628 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1629 if (rules
->tokens
== NULL
)
1630 return udev_rules_unref(rules
);
1631 rules
->token_max
= PREALLOC_TOKEN
;
1633 rules
->strbuf
= strbuf_new();
1635 return udev_rules_unref(rules
);
1637 udev_rules_check_timestamp(rules
);
1639 r
= conf_files_list_strv(&files
, ".rules", NULL
, rules_dirs
);
1641 log_error("failed to enumerate rules files: %s", strerror(-r
));
1642 return udev_rules_unref(rules
);
1646 * The offset value in the rules strct is limited; add all
1647 * rules file names to the beginning of the string buffer.
1649 STRV_FOREACH(f
, files
)
1650 rules_add_string(rules
, *f
);
1652 STRV_FOREACH(f
, files
)
1653 parse_file(rules
, *f
);
1657 memset(&end_token
, 0x00, sizeof(struct token
));
1658 end_token
.type
= TK_END
;
1659 add_token(rules
, &end_token
);
1660 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1661 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->strbuf
->len
);
1663 /* cleanup temporary strbuf data */
1664 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1665 rules
->strbuf
->in_count
, rules
->strbuf
->in_len
,
1666 rules
->strbuf
->dedup_count
, rules
->strbuf
->dedup_len
, rules
->strbuf
->nodes_count
);
1667 strbuf_complete(rules
->strbuf
);
1669 /* cleanup uid/gid cache */
1672 rules
->uids_cur
= 0;
1673 rules
->uids_max
= 0;
1676 rules
->gids_cur
= 0;
1677 rules
->gids_max
= 0;
1683 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
)
1687 free(rules
->tokens
);
1688 strbuf_cleanup(rules
->strbuf
);
1695 bool udev_rules_check_timestamp(struct udev_rules
*rules
)
1700 return paths_check_timestamp(rules_dirs
, &rules
->dirs_ts_usec
, true);
1703 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1705 char *key_value
= rules_str(rules
, token
->key
.value_off
);
1712 switch (token
->key
.glob
) {
1714 match
= (streq(key_value
, val
));
1717 match
= (fnmatch(key_value
, val
, 0) == 0);
1724 s
= rules_str(rules
, token
->key
.value_off
);
1729 next
= strchr(s
, '|');
1731 size_t matchlen
= (size_t)(next
- s
);
1733 match
= (matchlen
== len
&& strneq(s
, val
, matchlen
));
1737 match
= (streq(s
, val
));
1746 char value
[UTIL_PATH_SIZE
];
1748 strscpy(value
, sizeof(value
), rules_str(rules
, token
->key
.value_off
));
1750 while (key_value
!= NULL
) {
1751 pos
= strchr(key_value
, '|');
1756 match
= (fnmatch(key_value
, val
, 0) == 0);
1764 match
= (val
[0] != '\0');
1770 if (match
&& (token
->key
.op
== OP_MATCH
))
1772 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1777 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1780 char nbuf
[UTIL_NAME_SIZE
];
1782 char vbuf
[UTIL_NAME_SIZE
];
1785 name
= rules_str(rules
, cur
->key
.attr_off
);
1786 switch (cur
->key
.attrsubst
) {
1788 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1792 value
= udev_device_get_sysattr_value(dev
, name
);
1797 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1805 /* remove trailing whitespace, if not asked to match for it */
1806 len
= strlen(value
);
1807 if (len
> 0 && isspace(value
[len
-1])) {
1808 const char *key_value
;
1811 key_value
= rules_str(rules
, cur
->key
.value_off
);
1812 klen
= strlen(key_value
);
1813 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1814 if (value
!= vbuf
) {
1815 strscpy(vbuf
, sizeof(vbuf
), value
);
1818 while (len
> 0 && isspace(vbuf
[--len
]))
1823 return match_key(rules
, cur
, value
);
1832 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
, const sigset_t
*sigmask
)
1836 enum escape_type esc
= ESCAPE_UNSET
;
1839 if (rules
->tokens
== NULL
)
1842 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
1843 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1844 udev_device_get_ifindex(event
->dev
) > 0));
1846 /* loop through token list, match, run actions or forward to next rule */
1847 cur
= &rules
->tokens
[0];
1850 dump_token(rules
, cur
);
1851 switch (cur
->type
) {
1855 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1856 if (!can_set_name
&& rule
->rule
.can_set_name
)
1861 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1865 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1869 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1872 case TK_M_DEVLINK
: {
1873 struct udev_list_entry
*list_entry
;
1876 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1877 const char *devlink
;
1879 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
1880 if (match_key(rules
, cur
, devlink
) == 0) {
1890 if (match_key(rules
, cur
, event
->name
) != 0)
1894 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
1897 value
= udev_device_get_property_value(event
->dev
, key_name
);
1900 if (match_key(rules
, cur
, value
))
1905 struct udev_list_entry
*list_entry
;
1908 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
1909 if (streq(rules_str(rules
, cur
->key
.value_off
), udev_list_entry_get_name(list_entry
))) {
1914 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
1918 case TK_M_SUBSYSTEM
:
1919 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1923 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1926 case TK_M_WAITFOR
: {
1927 char filename
[UTIL_PATH_SIZE
];
1930 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
1931 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
1932 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
1937 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1941 case TK_M_SUBSYSTEMS
:
1947 /* get whole sequence of parent matches */
1949 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
1952 /* loop over parents */
1953 event
->dev_parent
= event
->dev
;
1957 /* loop over sequence of parent match keys */
1958 for (key
= cur
; key
< next
; key
++ ) {
1959 dump_token(rules
, key
);
1962 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1965 case TK_M_SUBSYSTEMS
:
1966 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1970 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1974 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1978 bool match
= udev_device_has_tag(event
->dev_parent
, rules_str(rules
, cur
->key
.value_off
));
1980 if (match
&& key
->key
.op
== OP_NOMATCH
)
1982 if (!match
&& key
->key
.op
== OP_MATCH
)
1993 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1994 if (event
->dev_parent
== NULL
)
1997 /* move behind our sequence of parent match keys */
2002 char filename
[UTIL_PATH_SIZE
];
2003 struct stat statbuf
;
2006 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), filename
, sizeof(filename
));
2007 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2008 if (filename
[0] != '/') {
2009 char tmp
[UTIL_PATH_SIZE
];
2011 strscpy(tmp
, sizeof(tmp
), filename
);
2012 strscpyl(filename
, sizeof(filename
),
2013 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2016 attr_subst_subdir(filename
, sizeof(filename
));
2018 match
= (stat(filename
, &statbuf
) == 0);
2019 if (match
&& cur
->key
.mode
> 0)
2020 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2021 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2023 if (!match
&& cur
->key
.op
== OP_MATCH
)
2027 case TK_M_EVENT_TIMEOUT
:
2028 log_debug("OPTIONS event_timeout=%u", cur
->key
.event_timeout
);
2029 event
->timeout_usec
= cur
->key
.event_timeout
* 1000 * 1000;
2031 case TK_M_PROGRAM
: {
2032 char program
[UTIL_PATH_SIZE
];
2034 char result
[UTIL_PATH_SIZE
];
2036 free(event
->program_result
);
2037 event
->program_result
= NULL
;
2038 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), program
, sizeof(program
));
2039 envp
= udev_device_get_properties_envp(event
->dev
);
2040 log_debug("PROGRAM '%s' %s:%u",
2042 rules_str(rules
, rule
->rule
.filename_off
),
2043 rule
->rule
.filename_line
);
2045 if (udev_event_spawn(event
, program
, envp
, sigmask
, result
, sizeof(result
)) < 0) {
2046 if (cur
->key
.op
!= OP_NOMATCH
)
2051 util_remove_trailing_chars(result
, '\n');
2052 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2053 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2055 log_debug("%i character(s) replaced" , count
);
2057 event
->program_result
= strdup(result
);
2058 if (cur
->key
.op
== OP_NOMATCH
)
2063 case TK_M_IMPORT_FILE
: {
2064 char import
[UTIL_PATH_SIZE
];
2066 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2067 if (import_file_into_properties(event
->dev
, import
) != 0)
2068 if (cur
->key
.op
!= OP_NOMATCH
)
2072 case TK_M_IMPORT_PROG
: {
2073 char import
[UTIL_PATH_SIZE
];
2075 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2076 log_debug("IMPORT '%s' %s:%u",
2078 rules_str(rules
, rule
->rule
.filename_off
),
2079 rule
->rule
.filename_line
);
2081 if (import_program_into_properties(event
, import
, sigmask
) != 0)
2082 if (cur
->key
.op
!= OP_NOMATCH
)
2086 case TK_M_IMPORT_BUILTIN
: {
2087 char command
[UTIL_PATH_SIZE
];
2089 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2090 /* check if we ran already */
2091 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2092 log_debug("IMPORT builtin skip '%s' %s:%u",
2093 udev_builtin_name(cur
->key
.builtin_cmd
),
2094 rules_str(rules
, rule
->rule
.filename_off
),
2095 rule
->rule
.filename_line
);
2096 /* return the result from earlier run */
2097 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2098 if (cur
->key
.op
!= OP_NOMATCH
)
2103 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2106 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), command
, sizeof(command
));
2107 log_debug("IMPORT builtin '%s' %s:%u",
2108 udev_builtin_name(cur
->key
.builtin_cmd
),
2109 rules_str(rules
, rule
->rule
.filename_off
),
2110 rule
->rule
.filename_line
);
2112 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2113 /* remember failure */
2114 log_debug("IMPORT builtin '%s' returned non-zero",
2115 udev_builtin_name(cur
->key
.builtin_cmd
));
2116 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2117 if (cur
->key
.op
!= OP_NOMATCH
)
2122 case TK_M_IMPORT_DB
: {
2123 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2126 value
= udev_device_get_property_value(event
->dev_db
, key
);
2127 if (value
!= NULL
) {
2128 struct udev_list_entry
*entry
;
2130 entry
= udev_device_add_property(event
->dev
, key
, value
);
2131 udev_list_entry_set_num(entry
, true);
2133 if (cur
->key
.op
!= OP_NOMATCH
)
2138 case TK_M_IMPORT_CMDLINE
: {
2140 bool imported
= false;
2142 f
= fopen("/proc/cmdline", "re");
2146 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2147 const char *key
= rules_str(rules
, cur
->key
.value_off
);
2150 pos
= strstr(cmdline
, key
);
2152 struct udev_list_entry
*entry
;
2155 if (pos
[0] == '\0' || isspace(pos
[0])) {
2156 /* we import simple flags as 'FLAG=1' */
2157 entry
= udev_device_add_property(event
->dev
, key
, "1");
2158 udev_list_entry_set_num(entry
, true);
2160 } else if (pos
[0] == '=') {
2165 while (pos
[0] != '\0' && !isspace(pos
[0]))
2168 entry
= udev_device_add_property(event
->dev
, key
, value
);
2169 udev_list_entry_set_num(entry
, true);
2176 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2180 case TK_M_IMPORT_PARENT
: {
2181 char import
[UTIL_PATH_SIZE
];
2183 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), import
, sizeof(import
));
2184 if (import_parent_into_properties(event
->dev
, import
) != 0)
2185 if (cur
->key
.op
!= OP_NOMATCH
)
2190 if (match_key(rules
, cur
, event
->program_result
) != 0)
2193 case TK_A_STRING_ESCAPE_NONE
:
2196 case TK_A_STRING_ESCAPE_REPLACE
:
2197 esc
= ESCAPE_REPLACE
;
2199 case TK_A_DB_PERSIST
:
2200 udev_device_set_db_persist(event
->dev
);
2202 case TK_A_INOTIFY_WATCH
:
2203 if (event
->inotify_watch_final
)
2205 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2206 event
->inotify_watch_final
= true;
2207 event
->inotify_watch
= cur
->key
.watch
;
2209 case TK_A_DEVLINK_PRIO
:
2210 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2213 char owner
[UTIL_NAME_SIZE
];
2215 if (event
->owner_final
)
2217 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2218 event
->owner_final
= true;
2219 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), owner
, sizeof(owner
));
2220 event
->owner_set
= true;
2221 event
->uid
= util_lookup_user(event
->udev
, owner
);
2222 log_debug("OWNER %u %s:%u",
2224 rules_str(rules
, rule
->rule
.filename_off
),
2225 rule
->rule
.filename_line
);
2229 char group
[UTIL_NAME_SIZE
];
2231 if (event
->group_final
)
2233 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2234 event
->group_final
= true;
2235 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), group
, sizeof(group
));
2236 event
->group_set
= true;
2237 event
->gid
= util_lookup_group(event
->udev
, group
);
2238 log_debug("GROUP %u %s:%u",
2240 rules_str(rules
, rule
->rule
.filename_off
),
2241 rule
->rule
.filename_line
);
2245 char mode_str
[UTIL_NAME_SIZE
];
2249 if (event
->mode_final
)
2251 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), mode_str
, sizeof(mode_str
));
2252 mode
= strtol(mode_str
, &endptr
, 8);
2253 if (endptr
[0] != '\0') {
2254 log_error("ignoring invalid mode '%s'", mode_str
);
2257 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2258 event
->mode_final
= true;
2259 event
->mode_set
= true;
2261 log_debug("MODE %#o %s:%u",
2263 rules_str(rules
, rule
->rule
.filename_off
),
2264 rule
->rule
.filename_line
);
2268 if (event
->owner_final
)
2270 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2271 event
->owner_final
= true;
2272 event
->owner_set
= true;
2273 event
->uid
= cur
->key
.uid
;
2274 log_debug("OWNER %u %s:%u",
2276 rules_str(rules
, rule
->rule
.filename_off
),
2277 rule
->rule
.filename_line
);
2280 if (event
->group_final
)
2282 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2283 event
->group_final
= true;
2284 event
->group_set
= true;
2285 event
->gid
= cur
->key
.gid
;
2286 log_debug("GROUP %u %s:%u",
2288 rules_str(rules
, rule
->rule
.filename_off
),
2289 rule
->rule
.filename_line
);
2292 if (event
->mode_final
)
2294 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2295 event
->mode_final
= true;
2296 event
->mode_set
= true;
2297 event
->mode
= cur
->key
.mode
;
2298 log_debug("MODE %#o %s:%u",
2300 rules_str(rules
, rule
->rule
.filename_off
),
2301 rule
->rule
.filename_line
);
2303 case TK_A_SECLABEL
: {
2304 const char *name
, *label
;
2306 name
= rules_str(rules
, cur
->key
.attr_off
);
2307 label
= rules_str(rules
, cur
->key
.value_off
);
2308 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2309 udev_list_cleanup(&event
->seclabel_list
);
2310 udev_list_entry_add(&event
->seclabel_list
, name
, label
);
2311 log_debug("SECLABEL{%s}='%s' %s:%u",
2313 rules_str(rules
, rule
->rule
.filename_off
),
2314 rule
->rule
.filename_line
);
2318 const char *name
= rules_str(rules
, cur
->key
.attr_off
);
2319 char *value
= rules_str(rules
, cur
->key
.value_off
);
2320 char value_new
[UTIL_NAME_SIZE
];
2321 const char *value_old
= NULL
;
2322 struct udev_list_entry
*entry
;
2324 if (value
[0] == '\0') {
2325 if (cur
->key
.op
== OP_ADD
)
2327 udev_device_add_property(event
->dev
, name
, NULL
);
2331 if (cur
->key
.op
== OP_ADD
)
2332 value_old
= udev_device_get_property_value(event
->dev
, name
);
2334 char temp
[UTIL_NAME_SIZE
];
2336 /* append value separated by space */
2337 udev_event_apply_format(event
, value
, temp
, sizeof(temp
));
2338 strscpyl(value_new
, sizeof(value_new
), value_old
, " ", temp
, NULL
);
2340 udev_event_apply_format(event
, value
, value_new
, sizeof(value_new
));
2342 entry
= udev_device_add_property(event
->dev
, name
, value_new
);
2343 /* store in db, skip private keys */
2345 udev_list_entry_set_num(entry
, true);
2349 char tag
[UTIL_PATH_SIZE
];
2352 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), tag
, sizeof(tag
));
2353 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2354 udev_device_cleanup_tags_list(event
->dev
);
2355 for (p
= tag
; *p
!= '\0'; p
++) {
2356 if ((*p
>= 'a' && *p
<= 'z') ||
2357 (*p
>= 'A' && *p
<= 'Z') ||
2358 (*p
>= '0' && *p
<= '9') ||
2359 *p
== '-' || *p
== '_')
2361 log_error("ignoring invalid tag name '%s'", tag
);
2364 udev_device_add_tag(event
->dev
, tag
);
2368 const char *name
= rules_str(rules
, cur
->key
.value_off
);
2370 char name_str
[UTIL_PATH_SIZE
];
2373 if (event
->name_final
)
2375 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2376 event
->name_final
= true;
2377 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2378 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2379 count
= util_replace_chars(name_str
, "/");
2381 log_debug("%i character(s) replaced", count
);
2383 if (major(udev_device_get_devnum(event
->dev
)) &&
2384 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2385 log_error("NAME=\"%s\" ignored, kernel device nodes "
2386 "can not be renamed; please fix it in %s:%u\n", name
,
2387 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2391 event
->name
= strdup(name_str
);
2392 log_debug("NAME '%s' %s:%u",
2394 rules_str(rules
, rule
->rule
.filename_off
),
2395 rule
->rule
.filename_line
);
2398 case TK_A_DEVLINK
: {
2399 char temp
[UTIL_PATH_SIZE
];
2400 char filename
[UTIL_PATH_SIZE
];
2404 if (event
->devlink_final
)
2406 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2408 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2409 event
->devlink_final
= true;
2410 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2411 udev_device_cleanup_devlinks_list(event
->dev
);
2413 /* allow multiple symlinks separated by spaces */
2414 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), temp
, sizeof(temp
));
2415 if (esc
== ESCAPE_UNSET
)
2416 count
= util_replace_chars(temp
, "/ ");
2417 else if (esc
== ESCAPE_REPLACE
)
2418 count
= util_replace_chars(temp
, "/");
2420 log_debug("%i character(s) replaced" , count
);
2422 while (isspace(pos
[0]))
2424 next
= strchr(pos
, ' ');
2425 while (next
!= NULL
) {
2427 log_debug("LINK '%s' %s:%u", pos
,
2428 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2429 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2430 udev_device_add_devlink(event
->dev
, filename
);
2431 while (isspace(next
[1]))
2434 next
= strchr(pos
, ' ');
2436 if (pos
[0] != '\0') {
2437 log_debug("LINK '%s' %s:%u", pos
,
2438 rules_str(rules
, rule
->rule
.filename_off
), rule
->rule
.filename_line
);
2439 strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2440 udev_device_add_devlink(event
->dev
, filename
);
2445 const char *key_name
= rules_str(rules
, cur
->key
.attr_off
);
2446 char attr
[UTIL_PATH_SIZE
];
2447 char value
[UTIL_NAME_SIZE
];
2450 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2451 strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2452 attr_subst_subdir(attr
, sizeof(attr
));
2454 udev_event_apply_format(event
, rules_str(rules
, cur
->key
.value_off
), value
, sizeof(value
));
2455 log_debug("ATTR '%s' writing '%s' %s:%u", attr
, value
,
2456 rules_str(rules
, rule
->rule
.filename_off
),
2457 rule
->rule
.filename_line
);
2458 f
= fopen(attr
, "we");
2460 if (fprintf(f
, "%s", value
) <= 0)
2461 log_error("error writing ATTR{%s}: %m", attr
);
2464 log_error("error opening ATTR{%s} for writing: %m", attr
);
2468 case TK_A_RUN_BUILTIN
:
2469 case TK_A_RUN_PROGRAM
: {
2470 struct udev_list_entry
*entry
;
2472 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2473 udev_list_cleanup(&event
->run_list
);
2474 log_debug("RUN '%s' %s:%u",
2475 rules_str(rules
, cur
->key
.value_off
),
2476 rules_str(rules
, rule
->rule
.filename_off
),
2477 rule
->rule
.filename_line
);
2478 entry
= udev_list_entry_add(&event
->run_list
, rules_str(rules
, cur
->key
.value_off
), NULL
);
2479 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2483 if (cur
->key
.rule_goto
== 0)
2485 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2490 case TK_M_PARENTS_MIN
:
2491 case TK_M_PARENTS_MAX
:
2494 log_error("wrong type %u", cur
->type
);
2501 /* fast-forward to next rule */
2502 cur
= rule
+ rule
->rule
.token_count
;
2506 int udev_rules_apply_static_dev_perms(struct udev_rules
*rules
)
2513 _cleanup_strv_free_
char **tags
= NULL
;
2516 _cleanup_free_
char *path
= NULL
;
2519 if (rules
->tokens
== NULL
)
2522 cur
= &rules
->tokens
[0];
2525 switch (cur
->type
) {
2530 /* skip rules without a static_node tag */
2531 if (!rule
->rule
.has_static_node
)
2547 mode
= cur
->key
.mode
;
2550 r
= strv_extend(&tags
, rules_str(rules
, cur
->key
.value_off
));
2555 case TK_A_STATIC_NODE
: {
2556 char device_node
[UTIL_PATH_SIZE
];
2557 char tags_dir
[UTIL_PATH_SIZE
];
2558 char tag_symlink
[UTIL_PATH_SIZE
];
2561 /* we assure, that the permissions tokens are sorted before the static token */
2562 if (mode
== 0 && uid
== 0 && gid
== 0 && tags
== NULL
)
2564 strscpyl(device_node
, sizeof(device_node
), "/dev/", rules_str(rules
, cur
->key
.value_off
), NULL
);
2565 if (stat(device_node
, &stats
) != 0)
2567 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2571 /* Export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2573 STRV_FOREACH(t
, tags
) {
2574 _cleanup_free_
char *unescaped_filename
= NULL
;
2576 strscpyl(tags_dir
, sizeof(tags_dir
), "/run/udev/static_node-tags/", *t
, "/", NULL
);
2577 r
= mkdir_p(tags_dir
, 0755);
2579 log_error("failed to create %s: %s", tags_dir
, strerror(-r
));
2583 unescaped_filename
= xescape(rules_str(rules
, cur
->key
.value_off
), "/.");
2585 strscpyl(tag_symlink
, sizeof(tag_symlink
), tags_dir
, unescaped_filename
, NULL
);
2586 r
= symlink(device_node
, tag_symlink
);
2587 if (r
< 0 && errno
!= EEXIST
) {
2588 log_error("failed to create symlink %s -> %s: %m", tag_symlink
, device_node
);
2595 /* don't touch the permissions if only the tags were set */
2596 if (mode
== 0 && uid
== 0 && gid
== 0)
2605 if (mode
!= (stats
.st_mode
& 01777)) {
2606 r
= chmod(device_node
, mode
);
2608 log_error("failed to chmod '%s' %#o", device_node
, mode
);
2611 log_debug("chmod '%s' %#o", device_node
, mode
);
2614 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2615 r
= chown(device_node
, uid
, gid
);
2617 log_error("failed to chown '%s' %u %u ", device_node
, uid
, gid
);
2620 log_debug("chown '%s' %u %u", device_node
, uid
, gid
);
2623 utimensat(AT_FDCWD
, device_node
, NULL
, 0);
2633 /* fast-forward to next rule */
2634 cur
= rule
+ rule
->rule
.token_count
;
2641 fchmod(fileno(f
), 0644);
2642 if (ferror(f
) || rename(path
, "/run/udev/static_node-tags") < 0) {
2644 unlink("/run/udev/static_node-tags");