2 * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "path-util.h"
35 #include "conf-files.h"
37 #define PREALLOC_TOKEN 2048
38 #define PREALLOC_STRBUF 32 * 1024
39 #define PREALLOC_TRIE 256
42 unsigned int name_off
;
50 /* this node's first child */
51 unsigned int child_idx
;
52 /* the next child of our parent node's child list */
53 unsigned int next_child_idx
;
54 /* this node's last child (shortcut for append) */
55 unsigned int last_child_idx
;
56 unsigned int value_off
;
57 unsigned short value_len
;
64 unsigned long long *dirs_ts_usec
;
67 /* every key in the rules file becomes a token */
69 unsigned int token_cur
;
70 unsigned int token_max
;
72 /* all key strings are copied to a single string buffer */
76 unsigned int buf_count
;
78 /* during rule parsing, strings are indexed and de-duplicated */
79 struct trie_node
*trie_nodes
;
80 unsigned int trie_nodes_cur
;
81 unsigned int trie_nodes_max
;
83 /* during rule parsing, uid/gid lookup results are cached */
85 unsigned int uids_cur
;
86 unsigned int uids_max
;
88 unsigned int gids_cur
;
89 unsigned int gids_max
;
92 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
105 enum string_glob_type
{
107 GL_PLAIN
, /* no special chars */
108 GL_GLOB
, /* shell globs ?,*,[] */
109 GL_SPLIT
, /* multi-value A|B */
110 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
111 GL_SOMETHING
, /* commonly used "?*" */
114 enum string_subst_type
{
121 /* tokens of a rule are sorted/handled in this order */
126 TK_M_ACTION
, /* val */
127 TK_M_DEVPATH
, /* val */
128 TK_M_KERNEL
, /* val */
129 TK_M_DEVLINK
, /* val */
131 TK_M_ENV
, /* val, attr */
133 TK_M_SUBSYSTEM
, /* val */
134 TK_M_DRIVER
, /* val */
135 TK_M_WAITFOR
, /* val */
136 TK_M_ATTR
, /* val, attr */
139 TK_M_KERNELS
, /* val */
140 TK_M_SUBSYSTEMS
, /* val */
141 TK_M_DRIVERS
, /* val */
142 TK_M_ATTRS
, /* val, attr */
146 TK_M_TEST
, /* val, mode_t */
147 TK_M_EVENT_TIMEOUT
, /* int */
148 TK_M_PROGRAM
, /* val */
149 TK_M_IMPORT_FILE
, /* val */
150 TK_M_IMPORT_PROG
, /* val */
151 TK_M_IMPORT_BUILTIN
, /* val */
152 TK_M_IMPORT_DB
, /* val */
153 TK_M_IMPORT_CMDLINE
, /* val */
154 TK_M_IMPORT_PARENT
, /* val */
155 TK_M_RESULT
, /* val */
158 TK_A_STRING_ESCAPE_NONE
,
159 TK_A_STRING_ESCAPE_REPLACE
,
161 TK_A_INOTIFY_WATCH
, /* int */
162 TK_A_DEVLINK_PRIO
, /* int */
163 TK_A_OWNER
, /* val */
164 TK_A_GROUP
, /* val */
166 TK_A_OWNER_ID
, /* uid_t */
167 TK_A_GROUP_ID
, /* gid_t */
168 TK_A_MODE_ID
, /* mode_t */
169 TK_A_STATIC_NODE
, /* val */
170 TK_A_ENV
, /* val, attr */
173 TK_A_DEVLINK
, /* val */
174 TK_A_ATTR
, /* val, attr */
175 TK_A_RUN_BUILTIN
, /* val, bool */
176 TK_A_RUN_PROGRAM
, /* val, bool */
177 TK_A_GOTO
, /* size_t */
182 /* we try to pack stuff in a way that we take only 12 bytes per token */
185 unsigned char type
; /* same in rule and key */
187 enum token_type type
:8;
189 bool has_static_node
:1;
190 unsigned int unused
:6;
191 unsigned short token_count
;
192 unsigned int label_off
;
193 unsigned short filename_off
;
194 unsigned short filename_line
;
197 enum token_type type
:8;
198 enum operation_type op
:8;
199 enum string_glob_type glob
:8;
200 enum string_subst_type subst
:4;
201 enum string_subst_type attrsubst
:4;
202 unsigned int value_off
;
204 unsigned int attr_off
;
206 unsigned int rule_goto
;
213 enum udev_builtin_cmd builtin_cmd
;
221 struct udev_rules
*rules
;
223 struct token token
[MAX_TK
];
224 unsigned int token_cur
;
228 static const char *operation_str(enum operation_type type
)
230 static const char *operation_strs
[] = {
231 [OP_UNSET
] = "UNSET",
232 [OP_MATCH
] = "match",
233 [OP_NOMATCH
] = "nomatch",
234 [OP_MATCH_MAX
] = "MATCH_MAX",
237 [OP_ASSIGN
] = "assign",
238 [OP_ASSIGN_FINAL
] = "assign-final",
241 return operation_strs
[type
];
244 static const char *string_glob_str(enum string_glob_type type
)
246 static const char *string_glob_strs
[] = {
247 [GL_UNSET
] = "UNSET",
248 [GL_PLAIN
] = "plain",
250 [GL_SPLIT
] = "split",
251 [GL_SPLIT_GLOB
] = "split-glob",
252 [GL_SOMETHING
] = "split-glob",
255 return string_glob_strs
[type
];
258 static const char *token_str(enum token_type type
)
260 static const char *token_strs
[] = {
261 [TK_UNSET
] = "UNSET",
264 [TK_M_ACTION
] = "M ACTION",
265 [TK_M_DEVPATH
] = "M DEVPATH",
266 [TK_M_KERNEL
] = "M KERNEL",
267 [TK_M_DEVLINK
] = "M DEVLINK",
268 [TK_M_NAME
] = "M NAME",
269 [TK_M_ENV
] = "M ENV",
270 [TK_M_TAG
] = "M TAG",
271 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
272 [TK_M_DRIVER
] = "M DRIVER",
273 [TK_M_WAITFOR
] = "M WAITFOR",
274 [TK_M_ATTR
] = "M ATTR",
276 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
277 [TK_M_KERNELS
] = "M KERNELS",
278 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
279 [TK_M_DRIVERS
] = "M DRIVERS",
280 [TK_M_ATTRS
] = "M ATTRS",
281 [TK_M_TAGS
] = "M TAGS",
282 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
284 [TK_M_TEST
] = "M TEST",
285 [TK_M_EVENT_TIMEOUT
] = "M EVENT_TIMEOUT",
286 [TK_M_PROGRAM
] = "M PROGRAM",
287 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
288 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
289 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
290 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
291 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
292 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
293 [TK_M_RESULT
] = "M RESULT",
294 [TK_M_MAX
] = "M MAX",
296 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
297 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
298 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
299 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
300 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
301 [TK_A_OWNER
] = "A OWNER",
302 [TK_A_GROUP
] = "A GROUP",
303 [TK_A_MODE
] = "A MODE",
304 [TK_A_OWNER_ID
] = "A OWNER_ID",
305 [TK_A_GROUP_ID
] = "A GROUP_ID",
306 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
307 [TK_A_MODE_ID
] = "A MODE_ID",
308 [TK_A_ENV
] = "A ENV",
309 [TK_A_TAG
] = "A ENV",
310 [TK_A_NAME
] = "A NAME",
311 [TK_A_DEVLINK
] = "A DEVLINK",
312 [TK_A_ATTR
] = "A ATTR",
313 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
314 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
315 [TK_A_GOTO
] = "A GOTO",
320 return token_strs
[type
];
323 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
325 enum token_type type
= token
->type
;
326 enum operation_type op
= token
->key
.op
;
327 enum string_glob_type glob
= token
->key
.glob
;
328 const char *value
= &rules
->buf
[token
->key
.value_off
];
329 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
334 const char *tks_ptr
= (char *)rules
->tokens
;
335 const char *tk_ptr
= (char *)token
;
336 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
338 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
339 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
340 idx
, token
->rule
.token_count
,
341 &rules
->buf
[token
->rule
.label_off
]);
353 case TK_M_SUBSYSTEMS
:
357 case TK_M_IMPORT_FILE
:
358 case TK_M_IMPORT_PROG
:
360 case TK_M_IMPORT_CMDLINE
:
361 case TK_M_IMPORT_PARENT
:
368 case TK_A_RUN_BUILTIN
:
369 case TK_A_RUN_PROGRAM
:
370 log_debug("%s %s '%s'(%s)\n",
371 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
373 case TK_M_IMPORT_BUILTIN
:
374 log_debug("%s %i '%s'\n", token_str(type
), token
->key
.builtin_cmd
, value
);
381 log_debug("%s %s '%s' '%s'(%s)\n",
382 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
386 log_debug("%s %s '%s'\n", token_str(type
), operation_str(op
), value
);
388 case TK_A_STRING_ESCAPE_NONE
:
389 case TK_A_STRING_ESCAPE_REPLACE
:
390 case TK_A_DB_PERSIST
:
391 log_debug("%s\n", token_str(type
));
394 log_debug("%s %s '%s'(%s) %#o\n",
395 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
397 case TK_A_INOTIFY_WATCH
:
398 log_debug("%s %u\n", token_str(type
), token
->key
.watch
);
400 case TK_A_DEVLINK_PRIO
:
401 log_debug("%s %u\n", token_str(type
), token
->key
.devlink_prio
);
404 log_debug("%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.uid
);
407 log_debug("%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.gid
);
410 log_debug("%s %s %#o\n", token_str(type
), operation_str(op
), token
->key
.mode
);
412 case TK_A_STATIC_NODE
:
413 log_debug("%s '%s'\n", token_str(type
), value
);
415 case TK_M_EVENT_TIMEOUT
:
416 log_debug("%s %u\n", token_str(type
), token
->key
.event_timeout
);
419 log_debug("%s '%s' %u\n", token_str(type
), value
, token
->key
.rule_goto
);
422 log_debug("* %s\n", token_str(type
));
424 case TK_M_PARENTS_MIN
:
425 case TK_M_PARENTS_MAX
:
428 log_debug("unknown type %u\n", type
);
433 static void dump_rules(struct udev_rules
*rules
)
437 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
439 rules
->token_cur
* sizeof(struct token
),
442 for(i
= 0; i
< rules
->token_cur
; i
++)
443 dump_token(rules
, &rules
->tokens
[i
]);
446 static inline const char *operation_str(enum operation_type type
) { return NULL
; }
447 static inline const char *token_str(enum token_type type
) { return NULL
; }
448 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
449 static inline void dump_rules(struct udev_rules
*rules
) {}
452 static int add_new_string(struct udev_rules
*rules
, const char *str
, size_t bytes
)
456 /* grow buffer if needed */
457 if (rules
->buf_cur
+ bytes
+1 >= rules
->buf_max
) {
461 /* double the buffer size */
462 add
= rules
->buf_max
;
466 buf
= realloc(rules
->buf
, rules
->buf_max
+ add
);
470 rules
->buf_max
+= add
;
472 off
= rules
->buf_cur
;
473 memcpy(&rules
->buf
[rules
->buf_cur
], str
, bytes
);
474 rules
->buf_cur
+= bytes
;
479 static int add_string(struct udev_rules
*rules
, const char *str
)
481 unsigned int node_idx
;
482 struct trie_node
*new_node
;
483 unsigned int new_node_idx
;
488 struct trie_node
*parent
;
490 /* walk trie, start from last character of str to find matching tails */
494 for (depth
= 0; depth
<= len
; depth
++) {
495 struct trie_node
*node
;
496 unsigned int child_idx
;
498 node
= &rules
->trie_nodes
[node_idx
];
499 off
= node
->value_off
+ node
->value_len
- len
;
501 /* match against current node */
502 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(&rules
->buf
[off
], str
, len
) == 0))
505 /* lookup child node */
506 key
= str
[len
- 1 - depth
];
507 child_idx
= node
->child_idx
;
508 while (child_idx
> 0) {
509 struct trie_node
*child
;
511 child
= &rules
->trie_nodes
[child_idx
];
512 if (child
->key
== key
)
514 child_idx
= child
->next_child_idx
;
518 node_idx
= child_idx
;
521 /* string not found, add it */
522 off
= add_new_string(rules
, str
, len
+ 1);
524 /* grow trie nodes if needed */
525 if (rules
->trie_nodes_cur
>= rules
->trie_nodes_max
) {
526 struct trie_node
*nodes
;
529 /* double the buffer size */
530 add
= rules
->trie_nodes_max
;
534 nodes
= realloc(rules
->trie_nodes
, (rules
->trie_nodes_max
+ add
) * sizeof(struct trie_node
));
537 rules
->trie_nodes
= nodes
;
538 rules
->trie_nodes_max
+= add
;
542 new_node_idx
= rules
->trie_nodes_cur
;
543 rules
->trie_nodes_cur
++;
544 new_node
= &rules
->trie_nodes
[new_node_idx
];
545 memset(new_node
, 0x00, sizeof(struct trie_node
));
546 new_node
->value_off
= off
;
547 new_node
->value_len
= len
;
550 /* join the parent's child list */
551 parent
= &rules
->trie_nodes
[node_idx
];
552 if (parent
->child_idx
== 0) {
553 parent
->child_idx
= new_node_idx
;
555 struct trie_node
*last_child
;
557 last_child
= &rules
->trie_nodes
[parent
->last_child_idx
];
558 last_child
->next_child_idx
= new_node_idx
;
560 parent
->last_child_idx
= new_node_idx
;
564 static int add_token(struct udev_rules
*rules
, struct token
*token
)
566 /* grow buffer if needed */
567 if (rules
->token_cur
+1 >= rules
->token_max
) {
568 struct token
*tokens
;
571 /* double the buffer size */
572 add
= rules
->token_max
;
576 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
579 rules
->tokens
= tokens
;
580 rules
->token_max
+= add
;
582 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
587 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
593 /* lookup, if we know it already */
594 for (i
= 0; i
< rules
->uids_cur
; i
++) {
595 off
= rules
->uids
[i
].name_off
;
596 if (streq(&rules
->buf
[off
], owner
)) {
597 uid
= rules
->uids
[i
].uid
;
601 uid
= util_lookup_user(rules
->udev
, owner
);
603 /* grow buffer if needed */
604 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
605 struct uid_gid
*uids
;
608 /* double the buffer size */
609 add
= rules
->uids_max
;
613 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
617 rules
->uids_max
+= add
;
619 rules
->uids
[rules
->uids_cur
].uid
= uid
;
620 off
= add_string(rules
, owner
);
623 rules
->uids
[rules
->uids_cur
].name_off
= off
;
628 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
634 /* lookup, if we know it already */
635 for (i
= 0; i
< rules
->gids_cur
; i
++) {
636 off
= rules
->gids
[i
].name_off
;
637 if (streq(&rules
->buf
[off
], group
)) {
638 gid
= rules
->gids
[i
].gid
;
642 gid
= util_lookup_group(rules
->udev
, group
);
644 /* grow buffer if needed */
645 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
646 struct uid_gid
*gids
;
649 /* double the buffer size */
650 add
= rules
->gids_max
;
654 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
658 rules
->gids_max
+= add
;
660 rules
->gids
[rules
->gids_cur
].gid
= gid
;
661 off
= add_string(rules
, group
);
664 rules
->gids
[rules
->gids_cur
].name_off
= off
;
669 static int import_property_from_string(struct udev_device
*dev
, char *line
)
677 while (isspace(key
[0]))
680 /* comment or empty line */
681 if (key
[0] == '#' || key
[0] == '\0')
684 /* split key/value */
685 val
= strchr(key
, '=');
692 while (isspace(val
[0]))
699 while (isspace(key
[len
-1]))
703 /* terminate value */
707 while (isspace(val
[len
-1]))
715 if (val
[0] == '"' || val
[0] == '\'') {
716 if (val
[len
-1] != val
[0]) {
717 log_debug("inconsistent quoting: '%s', skip\n", line
);
724 /* handle device, renamed by external tool, returning new path */
725 if (streq(key
, "DEVPATH")) {
726 char syspath
[UTIL_PATH_SIZE
];
728 log_debug("updating devpath from '%s' to '%s'\n",
729 udev_device_get_devpath(dev
), val
);
730 util_strscpyl(syspath
, sizeof(syspath
), "/sys", val
, NULL
);
731 udev_device_set_syspath(dev
, syspath
);
733 struct udev_list_entry
*entry
;
735 entry
= udev_device_add_property(dev
, key
, val
);
736 /* store in db, skip private keys */
738 udev_list_entry_set_num(entry
, true);
743 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
746 char line
[UTIL_LINE_SIZE
];
748 f
= fopen(filename
, "re");
751 while (fgets(line
, sizeof(line
), f
) != NULL
)
752 import_property_from_string(dev
, line
);
757 static int import_program_into_properties(struct udev_event
*event
, const char *program
, const sigset_t
*sigmask
)
759 struct udev_device
*dev
= event
->dev
;
761 char result
[UTIL_LINE_SIZE
];
765 envp
= udev_device_get_properties_envp(dev
);
766 err
= udev_event_spawn(event
, program
, envp
, sigmask
, result
, sizeof(result
));
771 while (line
!= NULL
) {
774 pos
= strchr(line
, '\n');
779 import_property_from_string(dev
, line
);
785 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
787 struct udev_device
*dev_parent
;
788 struct udev_list_entry
*list_entry
;
790 dev_parent
= udev_device_get_parent(dev
);
791 if (dev_parent
== NULL
)
794 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
795 const char *key
= udev_list_entry_get_name(list_entry
);
796 const char *val
= udev_list_entry_get_value(list_entry
);
798 if (fnmatch(filter
, key
, 0) == 0) {
799 struct udev_list_entry
*entry
;
801 entry
= udev_device_add_property(dev
, key
, val
);
802 /* store in db, skip private keys */
804 udev_list_entry_set_num(entry
, true);
810 #define WAIT_LOOP_PER_SECOND 50
811 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
813 char filepath
[UTIL_PATH_SIZE
];
814 char devicepath
[UTIL_PATH_SIZE
];
816 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
818 /* a relative path is a device attribute */
819 devicepath
[0] = '\0';
820 if (file
[0] != '/') {
821 util_strscpyl(devicepath
, sizeof(devicepath
), udev_device_get_syspath(dev
), NULL
);
822 util_strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
827 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
830 if (stat(file
, &stats
) == 0) {
831 log_debug("file '%s' appeared after %i loops\n", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
834 /* make sure, the device did not disappear in the meantime */
835 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
836 log_debug("device disappeared while waiting for '%s'\n", file
);
839 log_debug("wait for '%s' for %i mseconds\n", file
, 1000 / WAIT_LOOP_PER_SECOND
);
840 nanosleep(&duration
, NULL
);
842 log_debug("waiting for '%s' failed\n", file
);
846 static int attr_subst_subdir(char *attr
, size_t len
)
850 if (strstr(attr
, "/*/")) {
852 char dirname
[UTIL_PATH_SIZE
];
856 util_strscpy(dirname
, sizeof(dirname
), attr
);
857 pos
= strstr(dirname
, "/*/");
862 dir
= opendir(dirname
);
866 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
869 if (dent
->d_name
[0] == '.')
871 util_strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
872 if (stat(attr
, &stats
) == 0) {
884 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
890 if (linepos
== NULL
|| linepos
[0] == '\0')
893 /* skip whitespace */
894 while (isspace(linepos
[0]) || linepos
[0] == ',')
898 if (linepos
[0] == '\0')
904 if (linepos
[0] == '\0')
906 if (isspace(linepos
[0]))
908 if (linepos
[0] == '=')
910 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
911 if (linepos
[1] == '=')
915 /* remember end of key */
918 /* skip whitespace after key */
919 while (isspace(linepos
[0]))
921 if (linepos
[0] == '\0')
924 /* get operation type */
925 if (linepos
[0] == '=' && linepos
[1] == '=') {
928 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
931 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
934 } else if (linepos
[0] == '=') {
937 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
938 *op
= OP_ASSIGN_FINAL
;
946 /* skip whitespace after operator */
947 while (isspace(linepos
[0]))
949 if (linepos
[0] == '\0')
953 if (linepos
[0] == '"')
960 temp
= strchr(linepos
, '"');
966 /* move line to next key */
971 /* extract possible KEY{attr} */
972 static const char *get_key_attribute(struct udev
*udev
, char *str
)
977 attr
= strchr(str
, '{');
980 pos
= strchr(attr
, '}');
982 log_error("missing closing brace for format\n");
991 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
992 enum operation_type op
,
993 const char *value
, const void *data
)
995 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
996 const char *attr
= NULL
;
998 memset(token
, 0x00, sizeof(struct token
));
1004 case TK_M_SUBSYSTEM
:
1010 case TK_M_SUBSYSTEMS
:
1014 case TK_M_IMPORT_FILE
:
1015 case TK_M_IMPORT_PROG
:
1016 case TK_M_IMPORT_DB
:
1017 case TK_M_IMPORT_CMDLINE
:
1018 case TK_M_IMPORT_PARENT
:
1027 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1029 case TK_M_IMPORT_BUILTIN
:
1030 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1031 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
1039 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1040 token
->key
.attr_off
= add_string(rule_tmp
->rules
, attr
);
1043 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1044 token
->key
.devlink_unique
= *(int *)data
;
1047 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1049 token
->key
.mode
= *(mode_t
*)data
;
1051 case TK_A_STRING_ESCAPE_NONE
:
1052 case TK_A_STRING_ESCAPE_REPLACE
:
1053 case TK_A_DB_PERSIST
:
1055 case TK_A_RUN_BUILTIN
:
1056 case TK_A_RUN_PROGRAM
:
1057 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
1058 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1060 case TK_A_INOTIFY_WATCH
:
1061 case TK_A_DEVLINK_PRIO
:
1062 token
->key
.devlink_prio
= *(int *)data
;
1065 token
->key
.uid
= *(uid_t
*)data
;
1068 token
->key
.gid
= *(gid_t
*)data
;
1071 token
->key
.mode
= *(mode_t
*)data
;
1073 case TK_A_STATIC_NODE
:
1074 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1076 case TK_M_EVENT_TIMEOUT
:
1077 token
->key
.event_timeout
= *(int *)data
;
1080 case TK_M_PARENTS_MIN
:
1081 case TK_M_PARENTS_MAX
:
1085 log_error("wrong type %u\n", type
);
1089 if (value
!= NULL
&& type
< TK_M_MAX
) {
1090 /* check if we need to split or call fnmatch() while matching rules */
1091 enum string_glob_type glob
;
1095 has_split
= (strchr(value
, '|') != NULL
);
1096 has_glob
= (strchr(value
, '*') != NULL
|| strchr(value
, '?') != NULL
|| strchr(value
, '[') != NULL
);
1097 if (has_split
&& has_glob
) {
1098 glob
= GL_SPLIT_GLOB
;
1099 } else if (has_split
) {
1101 } else if (has_glob
) {
1102 if (streq(value
, "?*"))
1103 glob
= GL_SOMETHING
;
1109 token
->key
.glob
= glob
;
1112 if (value
!= NULL
&& type
> TK_M_MAX
) {
1113 /* check if assigned value has substitution chars */
1114 if (value
[0] == '[')
1115 token
->key
.subst
= SB_SUBSYS
;
1116 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
1117 token
->key
.subst
= SB_FORMAT
;
1119 token
->key
.subst
= SB_NONE
;
1123 /* check if property/attribut name has substitution chars */
1125 token
->key
.attrsubst
= SB_SUBSYS
;
1126 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
1127 token
->key
.attrsubst
= SB_FORMAT
;
1129 token
->key
.attrsubst
= SB_NONE
;
1132 token
->key
.type
= type
;
1134 rule_tmp
->token_cur
++;
1135 if (rule_tmp
->token_cur
>= ELEMENTSOF(rule_tmp
->token
)) {
1136 log_error("temporary rule array too small\n");
1142 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1145 unsigned int start
= 0;
1146 unsigned int end
= rule_tmp
->token_cur
;
1148 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1149 enum token_type next_val
= TK_UNSET
;
1150 unsigned int next_idx
= 0;
1153 /* find smallest value */
1154 for (j
= start
; j
< end
; j
++) {
1155 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1157 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1158 next_val
= rule_tmp
->token
[j
].type
;
1163 /* add token and mark done */
1164 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1166 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1169 if (next_idx
== start
)
1171 if (next_idx
+1 == end
)
1177 static int add_rule(struct udev_rules
*rules
, char *line
,
1178 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1182 struct rule_tmp rule_tmp
;
1184 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1185 rule_tmp
.rules
= rules
;
1186 rule_tmp
.rule
.type
= TK_RULE
;
1187 /* the offset in the rule is limited to unsigned short */
1188 if (filename_off
< USHRT_MAX
)
1189 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1190 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1196 enum operation_type op
;
1198 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0)
1201 if (streq(key
, "ACTION")) {
1202 if (op
> OP_MATCH_MAX
) {
1203 log_error("invalid ACTION operation\n");
1206 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1210 if (streq(key
, "DEVPATH")) {
1211 if (op
> OP_MATCH_MAX
) {
1212 log_error("invalid DEVPATH operation\n");
1215 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1219 if (streq(key
, "KERNEL")) {
1220 if (op
> OP_MATCH_MAX
) {
1221 log_error("invalid KERNEL operation\n");
1224 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1228 if (streq(key
, "SUBSYSTEM")) {
1229 if (op
> OP_MATCH_MAX
) {
1230 log_error("invalid SUBSYSTEM operation\n");
1233 /* bus, class, subsystem events should all be the same */
1234 if (streq(value
, "subsystem") ||
1235 streq(value
, "bus") ||
1236 streq(value
, "class")) {
1237 if (streq(value
, "bus") || streq(value
, "class"))
1238 log_error("'%s' must be specified as 'subsystem' \n"
1239 "please fix it in %s:%u", value
, filename
, lineno
);
1240 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1242 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1246 if (streq(key
, "DRIVER")) {
1247 if (op
> OP_MATCH_MAX
) {
1248 log_error("invalid DRIVER operation\n");
1251 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1255 if (startswith(key
, "ATTR{")) {
1256 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1258 log_error("error parsing ATTR attribute\n");
1261 if (op
< OP_MATCH_MAX
) {
1262 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1264 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1269 if (streq(key
, "KERNELS")) {
1270 if (op
> OP_MATCH_MAX
) {
1271 log_error("invalid KERNELS operation\n");
1274 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1278 if (streq(key
, "SUBSYSTEMS")) {
1279 if (op
> OP_MATCH_MAX
) {
1280 log_error("invalid SUBSYSTEMS operation\n");
1283 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1287 if (streq(key
, "DRIVERS")) {
1288 if (op
> OP_MATCH_MAX
) {
1289 log_error("invalid DRIVERS operation\n");
1292 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1296 if (startswith(key
, "ATTRS{")) {
1297 if (op
> OP_MATCH_MAX
) {
1298 log_error("invalid ATTRS operation\n");
1301 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1303 log_error("error parsing ATTRS attribute\n");
1306 if (startswith(attr
, "device/"))
1307 log_error("the 'device' link may not be available in a future kernel, "
1308 "please fix it in %s:%u", filename
, lineno
);
1309 else if (strstr(attr
, "../") != NULL
)
1310 log_error("do not reference parent sysfs directories directly, "
1311 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1312 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1316 if (streq(key
, "TAGS")) {
1317 if (op
> OP_MATCH_MAX
) {
1318 log_error("invalid TAGS operation\n");
1321 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1325 if (startswith(key
, "ENV{")) {
1326 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1328 log_error("error parsing ENV attribute\n");
1331 if (op
< OP_MATCH_MAX
) {
1332 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1335 static const char *blacklist
[] = {
1350 for (i
= 0; i
< ELEMENTSOF(blacklist
); i
++) {
1351 if (!streq(attr
, blacklist
[i
]))
1353 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr
, filename
, lineno
);
1356 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1362 if (streq(key
, "TAG")) {
1363 if (op
< OP_MATCH_MAX
)
1364 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1366 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1370 if (streq(key
, "PROGRAM")) {
1371 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1375 if (streq(key
, "RESULT")) {
1376 if (op
> OP_MATCH_MAX
) {
1377 log_error("invalid RESULT operation\n");
1380 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1384 if (startswith(key
, "IMPORT")) {
1385 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1387 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename
, lineno
);
1390 if (streq(attr
, "program")) {
1391 /* find known built-in command */
1392 if (value
[0] != '/') {
1393 enum udev_builtin_cmd cmd
;
1395 cmd
= udev_builtin_lookup(value
);
1396 if (cmd
< UDEV_BUILTIN_MAX
) {
1397 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1398 value
, filename
, lineno
);
1399 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1403 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1404 } else if (streq(attr
, "builtin")) {
1405 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1407 if (cmd
< UDEV_BUILTIN_MAX
)
1408 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1410 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value
, filename
, lineno
);
1411 } else if (streq(attr
, "file")) {
1412 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1413 } else if (streq(attr
, "db")) {
1414 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1415 } else if (streq(attr
, "cmdline")) {
1416 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1417 } else if (streq(attr
, "parent")) {
1418 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1420 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename
, lineno
);
1424 if (startswith(key
, "TEST")) {
1427 if (op
> OP_MATCH_MAX
) {
1428 log_error("invalid TEST operation\n");
1431 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1433 mode
= strtol(attr
, NULL
, 8);
1434 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1436 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1441 if (startswith(key
, "RUN")) {
1442 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1446 if (streq(attr
, "builtin")) {
1447 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1449 if (cmd
< UDEV_BUILTIN_MAX
)
1450 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1452 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value
, filename
, lineno
);
1453 } else if (streq(attr
, "program")) {
1454 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1456 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1458 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename
, lineno
);
1464 if (streq(key
, "WAIT_FOR") || streq(key
, "WAIT_FOR_SYSFS")) {
1465 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1469 if (streq(key
, "LABEL")) {
1470 rule_tmp
.rule
.rule
.label_off
= add_string(rules
, value
);
1474 if (streq(key
, "GOTO")) {
1475 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1479 if (startswith(key
, "NAME")) {
1480 if (op
< OP_MATCH_MAX
) {
1481 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1483 if (streq(value
, "%k")) {
1484 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1485 "please remove it from %s:%u\n", filename
, lineno
);
1488 if (value
[0] == '\0') {
1489 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1490 "please remove it from %s:%u\n", filename
, lineno
);
1493 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1495 rule_tmp
.rule
.rule
.can_set_name
= true;
1499 if (startswith(key
, "SYMLINK")) {
1500 if (op
< OP_MATCH_MAX
) {
1501 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1505 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("SYMLINK")-1);
1506 if (attr
!= NULL
&& strstr(attr
, "unique") != NULL
)
1508 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, &flag
);
1510 rule_tmp
.rule
.rule
.can_set_name
= true;
1514 if (streq(key
, "OWNER")) {
1518 uid
= strtoul(value
, &endptr
, 10);
1519 if (endptr
[0] == '\0') {
1520 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1521 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1522 uid
= add_uid(rules
, value
);
1523 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1524 } else if (rules
->resolve_names
>= 0) {
1525 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1527 rule_tmp
.rule
.rule
.can_set_name
= true;
1531 if (streq(key
, "GROUP")) {
1535 gid
= strtoul(value
, &endptr
, 10);
1536 if (endptr
[0] == '\0') {
1537 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1538 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1539 gid
= add_gid(rules
, value
);
1540 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1541 } else if (rules
->resolve_names
>= 0) {
1542 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1544 rule_tmp
.rule
.rule
.can_set_name
= true;
1548 if (streq(key
, "MODE")) {
1552 mode
= strtol(value
, &endptr
, 8);
1553 if (endptr
[0] == '\0')
1554 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1556 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1557 rule_tmp
.rule
.rule
.can_set_name
= true;
1561 if (streq(key
, "OPTIONS")) {
1564 pos
= strstr(value
, "link_priority=");
1566 int prio
= atoi(&pos
[strlen("link_priority=")]);
1568 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1571 pos
= strstr(value
, "event_timeout=");
1573 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1575 rule_add_key(&rule_tmp
, TK_M_EVENT_TIMEOUT
, op
, NULL
, &tout
);
1578 pos
= strstr(value
, "string_escape=");
1580 pos
= &pos
[strlen("string_escape=")];
1581 if (startswith(pos
, "none"))
1582 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1583 else if (startswith(pos
, "replace"))
1584 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1587 pos
= strstr(value
, "db_persist");
1589 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1591 pos
= strstr(value
, "nowatch");
1595 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1597 pos
= strstr(value
, "watch");
1601 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1605 pos
= strstr(value
, "static_node=");
1607 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1608 rule_tmp
.rule
.rule
.has_static_node
= true;
1614 log_error("unknown key '%s' in %s:%u\n", key
, filename
, lineno
);
1618 /* add rule token */
1619 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1620 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1623 /* add tokens to list, sorted by type */
1624 if (sort_token(rules
, &rule_tmp
) != 0)
1629 log_error("invalid rule '%s:%u'\n", filename
, lineno
);
1633 static int parse_file(struct udev_rules
*rules
, const char *filename
)
1636 unsigned int first_token
;
1637 unsigned int filename_off
;
1638 char line
[UTIL_LINE_SIZE
];
1642 if (null_or_empty_path(filename
)) {
1643 log_debug("skip empty file: %s\n", filename
);
1646 log_debug("read rules file: %s\n", filename
);
1648 f
= fopen(filename
, "re");
1652 first_token
= rules
->token_cur
;
1653 filename_off
= add_string(rules
, filename
);
1655 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1659 /* skip whitespace */
1662 while (isspace(key
[0]))
1673 /* continue reading if backslash+newline is found */
1674 while (line
[len
-2] == '\\') {
1675 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1677 if (strlen(&line
[len
-2]) < 2)
1683 if (len
+1 >= sizeof(line
)) {
1684 log_error("line too long '%s':%u, ignored\n", filename
, line_nr
);
1687 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1691 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1692 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1693 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1694 char *label
= &rules
->buf
[rules
->tokens
[i
].key
.value_off
];
1697 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1698 if (rules
->tokens
[j
].type
!= TK_RULE
)
1700 if (rules
->tokens
[j
].rule
.label_off
== 0)
1702 if (!streq(label
, &rules
->buf
[rules
->tokens
[j
].rule
.label_off
]))
1704 rules
->tokens
[i
].key
.rule_goto
= j
;
1707 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1708 log_error("GOTO '%s' has no matching label in: '%s'\n", label
, filename
);
1714 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1716 struct udev_rules
*rules
;
1717 struct udev_list file_list
;
1718 struct token end_token
;
1722 rules
= calloc(1, sizeof(struct udev_rules
));
1726 rules
->resolve_names
= resolve_names
;
1727 udev_list_init(udev
, &file_list
, true);
1729 /* init token array and string buffer */
1730 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1731 if (rules
->tokens
== NULL
) {
1735 rules
->token_max
= PREALLOC_TOKEN
;
1737 rules
->buf
= malloc(PREALLOC_STRBUF
);
1738 if (rules
->buf
== NULL
) {
1739 free(rules
->tokens
);
1743 rules
->buf_max
= PREALLOC_STRBUF
;
1744 /* offset 0 is always '\0' */
1745 rules
->buf
[0] = '\0';
1748 rules
->trie_nodes
= malloc(PREALLOC_TRIE
* sizeof(struct trie_node
));
1749 if (rules
->trie_nodes
== NULL
) {
1751 free(rules
->tokens
);
1755 rules
->trie_nodes_max
= PREALLOC_TRIE
;
1756 /* offset 0 is the trie root, with an empty string */
1757 memset(rules
->trie_nodes
, 0x00, sizeof(struct trie_node
));
1758 rules
->trie_nodes_cur
= 1;
1760 rules
->dirs
= strv_new(SYSCONFDIR
"/udev/rules.d",
1761 "/run/udev/rules.d",
1762 UDEVLIBEXECDIR
"/rules.d",
1765 log_error("failed to build config directory array");
1768 if (!path_strv_canonicalize(rules
->dirs
)) {
1769 log_error("failed to canonicalize config directories\n");
1772 strv_uniq(rules
->dirs
);
1774 rules
->dirs_ts_usec
= calloc(strv_length(rules
->dirs
), sizeof(long long));
1775 udev_rules_check_timestamp(rules
);
1777 r
= conf_files_list_strv(&files
, ".rules", (const char **)rules
->dirs
);
1779 log_error("failed to enumerate rules files: %s\n", strerror(-r
));
1784 * The offset value in the rules strct is limited; add all
1785 * rules file names to the beginning of the string buffer.
1787 STRV_FOREACH(f
, files
)
1788 add_string(rules
, *f
);
1790 STRV_FOREACH(f
, files
)
1791 parse_file(rules
, *f
);
1795 memset(&end_token
, 0x00, sizeof(struct token
));
1796 end_token
.type
= TK_END
;
1797 add_token(rules
, &end_token
);
1799 /* shrink allocated token and string buffer */
1800 if (rules
->token_cur
< rules
->token_max
) {
1801 struct token
*tokens
;
1803 tokens
= realloc(rules
->tokens
, rules
->token_cur
* sizeof(struct token
));
1804 if (tokens
!= NULL
|| rules
->token_cur
== 0) {
1805 rules
->tokens
= tokens
;
1806 rules
->token_max
= rules
->token_cur
;
1809 if (rules
->buf_cur
< rules
->buf_max
) {
1812 buf
= realloc(rules
->buf
, rules
->buf_cur
);
1813 if (buf
!= NULL
|| rules
->buf_cur
== 0) {
1815 rules
->buf_max
= rules
->buf_cur
;
1818 log_debug("rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1819 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1820 log_debug("temporary index used %zu bytes (%u * %zu bytes)\n",
1821 rules
->trie_nodes_cur
* sizeof(struct trie_node
),
1822 rules
->trie_nodes_cur
, sizeof(struct trie_node
));
1825 free(rules
->trie_nodes
);
1826 rules
->trie_nodes
= NULL
;
1827 rules
->trie_nodes_cur
= 0;
1828 rules
->trie_nodes_max
= 0;
1830 /* cleanup uid/gid cache */
1833 rules
->uids_cur
= 0;
1834 rules
->uids_max
= 0;
1837 rules
->gids_cur
= 0;
1838 rules
->gids_max
= 0;
1844 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
)
1848 free(rules
->tokens
);
1850 free(rules
->trie_nodes
);
1853 strv_free(rules
->dirs
);
1854 free(rules
->dirs_ts_usec
);
1859 bool udev_rules_check_timestamp(struct udev_rules
*rules
)
1862 bool changed
= false;
1867 for (i
= 0; rules
->dirs
[i
]; i
++) {
1870 if (stat(rules
->dirs
[i
], &stats
) < 0)
1873 if (rules
->dirs_ts_usec
[i
] == ts_usec(&stats
.st_mtim
))
1877 if (rules
->dirs_ts_usec
[i
] != 0) {
1878 log_debug("reload - timestamp of '%s' changed\n", rules
->dirs
[i
]);
1882 /* update timestamp */
1883 rules
->dirs_ts_usec
[i
] = ts_usec(&stats
.st_mtim
);
1889 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1891 char *key_value
= &rules
->buf
[token
->key
.value_off
];
1898 switch (token
->key
.glob
) {
1900 match
= (streq(key_value
, val
));
1903 match
= (fnmatch(key_value
, val
, 0) == 0);
1910 s
= &rules
->buf
[token
->key
.value_off
];
1915 next
= strchr(s
, '|');
1917 size_t matchlen
= (size_t)(next
- s
);
1919 match
= (matchlen
== len
&& strncmp(s
, val
, matchlen
) == 0);
1923 match
= (streq(s
, val
));
1932 char value
[UTIL_PATH_SIZE
];
1934 util_strscpy(value
, sizeof(value
), &rules
->buf
[token
->key
.value_off
]);
1936 while (key_value
!= NULL
) {
1937 pos
= strchr(key_value
, '|');
1942 match
= (fnmatch(key_value
, val
, 0) == 0);
1950 match
= (val
[0] != '\0');
1956 if (match
&& (token
->key
.op
== OP_MATCH
))
1958 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1963 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1966 char nbuf
[UTIL_NAME_SIZE
];
1968 char vbuf
[UTIL_NAME_SIZE
];
1971 name
= &rules
->buf
[cur
->key
.attr_off
];
1972 switch (cur
->key
.attrsubst
) {
1974 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1978 value
= udev_device_get_sysattr_value(dev
, name
);
1983 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1991 /* remove trailing whitespace, if not asked to match for it */
1992 len
= strlen(value
);
1993 if (len
> 0 && isspace(value
[len
-1])) {
1994 const char *key_value
;
1997 key_value
= &rules
->buf
[cur
->key
.value_off
];
1998 klen
= strlen(key_value
);
1999 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
2000 if (value
!= vbuf
) {
2001 util_strscpy(vbuf
, sizeof(vbuf
), value
);
2004 while (len
> 0 && isspace(vbuf
[--len
]))
2009 return match_key(rules
, cur
, value
);
2018 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
, const sigset_t
*sigmask
)
2022 enum escape_type esc
= ESCAPE_UNSET
;
2025 if (rules
->tokens
== NULL
)
2028 can_set_name
= ((!streq(udev_device_get_action(event
->dev
), "remove")) &&
2029 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
2030 udev_device_get_ifindex(event
->dev
) > 0));
2032 /* loop through token list, match, run actions or forward to next rule */
2033 cur
= &rules
->tokens
[0];
2036 dump_token(rules
, cur
);
2037 switch (cur
->type
) {
2041 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2042 if (!can_set_name
&& rule
->rule
.can_set_name
)
2047 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
2051 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
2055 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
2058 case TK_M_DEVLINK
: {
2059 struct udev_list_entry
*list_entry
;
2062 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
2063 const char *devlink
;
2065 devlink
= udev_list_entry_get_name(list_entry
) + strlen("/dev/");
2066 if (match_key(rules
, cur
, devlink
) == 0) {
2076 if (match_key(rules
, cur
, event
->name
) != 0)
2080 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2083 value
= udev_device_get_property_value(event
->dev
, key_name
);
2086 if (match_key(rules
, cur
, value
))
2091 struct udev_list_entry
*list_entry
;
2094 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
2095 if (streq(&rules
->buf
[cur
->key
.value_off
], udev_list_entry_get_name(list_entry
))) {
2100 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
2104 case TK_M_SUBSYSTEM
:
2105 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
2109 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
2112 case TK_M_WAITFOR
: {
2113 char filename
[UTIL_PATH_SIZE
];
2116 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2117 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
2118 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
2123 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
2127 case TK_M_SUBSYSTEMS
:
2133 /* get whole sequence of parent matches */
2135 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2138 /* loop over parents */
2139 event
->dev_parent
= event
->dev
;
2143 /* loop over sequence of parent match keys */
2144 for (key
= cur
; key
< next
; key
++ ) {
2145 dump_token(rules
, key
);
2148 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2151 case TK_M_SUBSYSTEMS
:
2152 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2156 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2160 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2164 bool match
= udev_device_has_tag(event
->dev_parent
, &rules
->buf
[cur
->key
.value_off
]);
2166 if (match
&& key
->key
.op
== OP_NOMATCH
)
2168 if (!match
&& key
->key
.op
== OP_MATCH
)
2179 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2180 if (event
->dev_parent
== NULL
)
2183 /* move behind our sequence of parent match keys */
2188 char filename
[UTIL_PATH_SIZE
];
2189 struct stat statbuf
;
2192 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2193 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2194 if (filename
[0] != '/') {
2195 char tmp
[UTIL_PATH_SIZE
];
2197 util_strscpy(tmp
, sizeof(tmp
), filename
);
2198 util_strscpyl(filename
, sizeof(filename
),
2199 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2202 attr_subst_subdir(filename
, sizeof(filename
));
2204 match
= (stat(filename
, &statbuf
) == 0);
2205 if (match
&& cur
->key
.mode
> 0)
2206 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2207 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2209 if (!match
&& cur
->key
.op
== OP_MATCH
)
2213 case TK_M_EVENT_TIMEOUT
:
2214 log_debug("OPTIONS event_timeout=%u\n", cur
->key
.event_timeout
);
2215 event
->timeout_usec
= cur
->key
.event_timeout
* 1000 * 1000;
2217 case TK_M_PROGRAM
: {
2218 char program
[UTIL_PATH_SIZE
];
2220 char result
[UTIL_PATH_SIZE
];
2222 free(event
->program_result
);
2223 event
->program_result
= NULL
;
2224 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], program
, sizeof(program
));
2225 envp
= udev_device_get_properties_envp(event
->dev
);
2226 log_debug("PROGRAM '%s' %s:%u\n",
2228 &rules
->buf
[rule
->rule
.filename_off
],
2229 rule
->rule
.filename_line
);
2231 if (udev_event_spawn(event
, program
, envp
, sigmask
, result
, sizeof(result
)) < 0) {
2232 if (cur
->key
.op
!= OP_NOMATCH
)
2237 util_remove_trailing_chars(result
, '\n');
2238 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2239 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2241 log_debug("%i character(s) replaced\n" , count
);
2243 event
->program_result
= strdup(result
);
2244 if (cur
->key
.op
== OP_NOMATCH
)
2249 case TK_M_IMPORT_FILE
: {
2250 char import
[UTIL_PATH_SIZE
];
2252 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2253 if (import_file_into_properties(event
->dev
, import
) != 0)
2254 if (cur
->key
.op
!= OP_NOMATCH
)
2258 case TK_M_IMPORT_PROG
: {
2259 char import
[UTIL_PATH_SIZE
];
2261 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2262 log_debug("IMPORT '%s' %s:%u\n",
2264 &rules
->buf
[rule
->rule
.filename_off
],
2265 rule
->rule
.filename_line
);
2267 if (import_program_into_properties(event
, import
, sigmask
) != 0)
2268 if (cur
->key
.op
!= OP_NOMATCH
)
2272 case TK_M_IMPORT_BUILTIN
: {
2273 char command
[UTIL_PATH_SIZE
];
2275 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2276 /* check if we ran already */
2277 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2278 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2279 udev_builtin_name(cur
->key
.builtin_cmd
),
2280 &rules
->buf
[rule
->rule
.filename_off
],
2281 rule
->rule
.filename_line
);
2282 /* return the result from earlier run */
2283 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2284 if (cur
->key
.op
!= OP_NOMATCH
)
2289 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2292 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], command
, sizeof(command
));
2293 log_debug("IMPORT builtin '%s' %s:%u\n",
2294 udev_builtin_name(cur
->key
.builtin_cmd
),
2295 &rules
->buf
[rule
->rule
.filename_off
],
2296 rule
->rule
.filename_line
);
2298 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2299 /* remember failure */
2300 log_debug("IMPORT builtin '%s' returned non-zero\n",
2301 udev_builtin_name(cur
->key
.builtin_cmd
));
2302 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2303 if (cur
->key
.op
!= OP_NOMATCH
)
2308 case TK_M_IMPORT_DB
: {
2309 const char *key
= &rules
->buf
[cur
->key
.value_off
];
2312 value
= udev_device_get_property_value(event
->dev_db
, key
);
2313 if (value
!= NULL
) {
2314 struct udev_list_entry
*entry
;
2316 entry
= udev_device_add_property(event
->dev
, key
, value
);
2317 udev_list_entry_set_num(entry
, true);
2319 if (cur
->key
.op
!= OP_NOMATCH
)
2324 case TK_M_IMPORT_CMDLINE
: {
2326 bool imported
= false;
2328 f
= fopen("/proc/cmdline", "re");
2332 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2333 const char *key
= &rules
->buf
[cur
->key
.value_off
];
2336 pos
= strstr(cmdline
, key
);
2338 struct udev_list_entry
*entry
;
2341 if (pos
[0] == '\0' || isspace(pos
[0])) {
2342 /* we import simple flags as 'FLAG=1' */
2343 entry
= udev_device_add_property(event
->dev
, key
, "1");
2344 udev_list_entry_set_num(entry
, true);
2346 } else if (pos
[0] == '=') {
2351 while (pos
[0] != '\0' && !isspace(pos
[0]))
2354 entry
= udev_device_add_property(event
->dev
, key
, value
);
2355 udev_list_entry_set_num(entry
, true);
2362 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2366 case TK_M_IMPORT_PARENT
: {
2367 char import
[UTIL_PATH_SIZE
];
2369 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2370 if (import_parent_into_properties(event
->dev
, import
) != 0)
2371 if (cur
->key
.op
!= OP_NOMATCH
)
2376 if (match_key(rules
, cur
, event
->program_result
) != 0)
2379 case TK_A_STRING_ESCAPE_NONE
:
2382 case TK_A_STRING_ESCAPE_REPLACE
:
2383 esc
= ESCAPE_REPLACE
;
2385 case TK_A_DB_PERSIST
:
2386 udev_device_set_db_persist(event
->dev
);
2388 case TK_A_INOTIFY_WATCH
:
2389 if (event
->inotify_watch_final
)
2391 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2392 event
->inotify_watch_final
= true;
2393 event
->inotify_watch
= cur
->key
.watch
;
2395 case TK_A_DEVLINK_PRIO
:
2396 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2399 char owner
[UTIL_NAME_SIZE
];
2401 if (event
->owner_final
)
2403 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2404 event
->owner_final
= true;
2405 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], owner
, sizeof(owner
));
2406 event
->uid
= util_lookup_user(event
->udev
, owner
);
2407 log_debug("OWNER %u %s:%u\n",
2409 &rules
->buf
[rule
->rule
.filename_off
],
2410 rule
->rule
.filename_line
);
2414 char group
[UTIL_NAME_SIZE
];
2416 if (event
->group_final
)
2418 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2419 event
->group_final
= true;
2420 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], group
, sizeof(group
));
2421 event
->gid
= util_lookup_group(event
->udev
, group
);
2422 log_debug("GROUP %u %s:%u\n",
2424 &rules
->buf
[rule
->rule
.filename_off
],
2425 rule
->rule
.filename_line
);
2429 char mode_str
[UTIL_NAME_SIZE
];
2433 if (event
->mode_final
)
2435 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], mode_str
, sizeof(mode_str
));
2436 mode
= strtol(mode_str
, &endptr
, 8);
2437 if (endptr
[0] != '\0') {
2438 log_error("ignoring invalid mode '%s'\n", mode_str
);
2441 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2442 event
->mode_final
= true;
2443 event
->mode_set
= true;
2445 log_debug("MODE %#o %s:%u\n",
2447 &rules
->buf
[rule
->rule
.filename_off
],
2448 rule
->rule
.filename_line
);
2452 if (event
->owner_final
)
2454 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2455 event
->owner_final
= true;
2456 event
->uid
= cur
->key
.uid
;
2457 log_debug("OWNER %u %s:%u\n",
2459 &rules
->buf
[rule
->rule
.filename_off
],
2460 rule
->rule
.filename_line
);
2463 if (event
->group_final
)
2465 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2466 event
->group_final
= true;
2467 event
->gid
= cur
->key
.gid
;
2468 log_debug("GROUP %u %s:%u\n",
2470 &rules
->buf
[rule
->rule
.filename_off
],
2471 rule
->rule
.filename_line
);
2474 if (event
->mode_final
)
2476 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2477 event
->mode_final
= true;
2478 event
->mode_set
= true;
2479 event
->mode
= cur
->key
.mode
;
2480 log_debug("MODE %#o %s:%u\n",
2482 &rules
->buf
[rule
->rule
.filename_off
],
2483 rule
->rule
.filename_line
);
2486 const char *name
= &rules
->buf
[cur
->key
.attr_off
];
2487 char *value
= &rules
->buf
[cur
->key
.value_off
];
2489 if (value
[0] != '\0') {
2490 char temp_value
[UTIL_NAME_SIZE
];
2491 struct udev_list_entry
*entry
;
2493 udev_event_apply_format(event
, value
, temp_value
, sizeof(temp_value
));
2494 entry
= udev_device_add_property(event
->dev
, name
, temp_value
);
2495 /* store in db, skip private keys */
2497 udev_list_entry_set_num(entry
, true);
2499 udev_device_add_property(event
->dev
, name
, NULL
);
2504 char tag
[UTIL_PATH_SIZE
];
2507 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], tag
, sizeof(tag
));
2508 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2509 udev_device_cleanup_tags_list(event
->dev
);
2510 for (p
= tag
; *p
!= '\0'; p
++) {
2511 if ((*p
>= 'a' && *p
<= 'z') ||
2512 (*p
>= 'A' && *p
<= 'Z') ||
2513 (*p
>= '0' && *p
<= '9') ||
2514 *p
== '-' || *p
== '_')
2516 log_error("ignoring invalid tag name '%s'\n", tag
);
2519 udev_device_add_tag(event
->dev
, tag
);
2523 const char *name
= &rules
->buf
[cur
->key
.value_off
];
2525 char name_str
[UTIL_PATH_SIZE
];
2528 if (event
->name_final
)
2530 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2531 event
->name_final
= true;
2532 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2533 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2534 count
= util_replace_chars(name_str
, "/");
2536 log_debug("%i character(s) replaced\n", count
);
2538 if (major(udev_device_get_devnum(event
->dev
)) &&
2539 (!streq(name_str
, udev_device_get_devnode(event
->dev
) + strlen("/dev/")))) {
2540 log_error("NAME=\"%s\" ignored, kernel device nodes "
2541 "can not be renamed; please fix it in %s:%u\n", name
,
2542 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2546 event
->name
= strdup(name_str
);
2547 log_debug("NAME '%s' %s:%u\n",
2549 &rules
->buf
[rule
->rule
.filename_off
],
2550 rule
->rule
.filename_line
);
2553 case TK_A_DEVLINK
: {
2554 char temp
[UTIL_PATH_SIZE
];
2555 char filename
[UTIL_PATH_SIZE
];
2559 if (event
->devlink_final
)
2561 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2563 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2564 event
->devlink_final
= true;
2565 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2566 udev_device_cleanup_devlinks_list(event
->dev
);
2568 /* allow multiple symlinks separated by spaces */
2569 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], temp
, sizeof(temp
));
2570 if (esc
== ESCAPE_UNSET
)
2571 count
= util_replace_chars(temp
, "/ ");
2572 else if (esc
== ESCAPE_REPLACE
)
2573 count
= util_replace_chars(temp
, "/");
2575 log_debug("%i character(s) replaced\n" , count
);
2577 while (isspace(pos
[0]))
2579 next
= strchr(pos
, ' ');
2580 while (next
!= NULL
) {
2582 log_debug("LINK '%s' %s:%u\n", pos
,
2583 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2584 util_strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2585 udev_device_add_devlink(event
->dev
, filename
, cur
->key
.devlink_unique
);
2586 while (isspace(next
[1]))
2589 next
= strchr(pos
, ' ');
2591 if (pos
[0] != '\0') {
2592 log_debug("LINK '%s' %s:%u\n", pos
,
2593 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2594 util_strscpyl(filename
, sizeof(filename
), "/dev/", pos
, NULL
);
2595 udev_device_add_devlink(event
->dev
, filename
, cur
->key
.devlink_unique
);
2600 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2601 char attr
[UTIL_PATH_SIZE
];
2602 char value
[UTIL_NAME_SIZE
];
2605 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2606 util_strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2607 attr_subst_subdir(attr
, sizeof(attr
));
2609 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], value
, sizeof(value
));
2610 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr
, value
,
2611 &rules
->buf
[rule
->rule
.filename_off
],
2612 rule
->rule
.filename_line
);
2613 f
= fopen(attr
, "we");
2615 if (fprintf(f
, "%s", value
) <= 0)
2616 log_error("error writing ATTR{%s}: %m\n", attr
);
2619 log_error("error opening ATTR{%s} for writing: %m\n", attr
);
2623 case TK_A_RUN_BUILTIN
:
2624 case TK_A_RUN_PROGRAM
: {
2625 struct udev_list_entry
*entry
;
2627 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2628 udev_list_cleanup(&event
->run_list
);
2629 log_debug("RUN '%s' %s:%u\n",
2630 &rules
->buf
[cur
->key
.value_off
],
2631 &rules
->buf
[rule
->rule
.filename_off
],
2632 rule
->rule
.filename_line
);
2633 entry
= udev_list_entry_add(&event
->run_list
, &rules
->buf
[cur
->key
.value_off
], NULL
);
2634 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2638 if (cur
->key
.rule_goto
== 0)
2640 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2645 case TK_M_PARENTS_MIN
:
2646 case TK_M_PARENTS_MAX
:
2649 log_error("wrong type %u\n", cur
->type
);
2656 /* fast-forward to next rule */
2657 cur
= rule
+ rule
->rule
.token_count
;
2661 void udev_rules_apply_static_dev_perms(struct udev_rules
*rules
)
2669 if (rules
->tokens
== NULL
)
2672 cur
= &rules
->tokens
[0];
2675 switch (cur
->type
) {
2680 /* skip rules without a static_node tag */
2681 if (!rule
->rule
.has_static_node
)
2695 mode
= cur
->key
.mode
;
2697 case TK_A_STATIC_NODE
: {
2698 char filename
[UTIL_PATH_SIZE
];
2701 /* we assure, that the permissions tokens are sorted before the static token */
2702 if (mode
== 0 && uid
== 0 && gid
== 0)
2704 util_strscpyl(filename
, sizeof(filename
), "/dev/",
2705 &rules
->buf
[cur
->key
.value_off
], NULL
);
2706 if (stat(filename
, &stats
) != 0)
2708 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2716 if (mode
!= (stats
.st_mode
& 01777)) {
2717 chmod(filename
, mode
);
2718 log_debug("chmod '%s' %#o\n", filename
, mode
);
2721 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2722 chown(filename
, uid
, gid
);
2723 log_debug("chown '%s' %u %u\n", filename
, uid
, gid
);
2726 utimensat(AT_FDCWD
, filename
, NULL
, 0);
2736 /* fast-forward to next rule */
2737 cur
= rule
+ rule
->rule
.token_count
;