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/>.
35 #define PREALLOC_TOKEN 2048
36 #define PREALLOC_STRBUF 32 * 1024
37 #define PREALLOC_TRIE 256
40 unsigned int name_off
;
48 /* this node's first child */
49 unsigned int child_idx
;
50 /* the next child of our parent node's child list */
51 unsigned int next_child_idx
;
52 /* this node's last child (shortcut for append) */
53 unsigned int last_child_idx
;
54 unsigned int value_off
;
55 unsigned short value_len
;
63 /* every key in the rules file becomes a token */
65 unsigned int token_cur
;
66 unsigned int token_max
;
68 /* all key strings are copied to a single string buffer */
72 unsigned int buf_count
;
74 /* during rule parsing, strings are indexed to find duplicates */
75 struct trie_node
*trie_nodes
;
76 unsigned int trie_nodes_cur
;
77 unsigned int trie_nodes_max
;
79 /* during rule parsing, uid/gid lookup results are cached */
81 unsigned int uids_cur
;
82 unsigned int uids_max
;
84 unsigned int gids_cur
;
85 unsigned int gids_max
;
88 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
101 enum string_glob_type
{
103 GL_PLAIN
, /* no special chars */
104 GL_GLOB
, /* shell globs ?,*,[] */
105 GL_SPLIT
, /* multi-value A|B */
106 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
107 GL_SOMETHING
, /* commonly used "?*" */
110 enum string_subst_type
{
117 /* tokens of a rule are sorted/handled in this order */
122 TK_M_ACTION
, /* val */
123 TK_M_DEVPATH
, /* val */
124 TK_M_KERNEL
, /* val */
125 TK_M_DEVLINK
, /* val */
127 TK_M_ENV
, /* val, attr */
129 TK_M_SUBSYSTEM
, /* val */
130 TK_M_DRIVER
, /* val */
131 TK_M_WAITFOR
, /* val */
132 TK_M_ATTR
, /* val, attr */
135 TK_M_KERNELS
, /* val */
136 TK_M_SUBSYSTEMS
, /* val */
137 TK_M_DRIVERS
, /* val */
138 TK_M_ATTRS
, /* val, attr */
141 TK_M_TEST
, /* val, mode_t */
142 TK_M_PROGRAM
, /* val */
143 TK_M_IMPORT_FILE
, /* val */
144 TK_M_IMPORT_PROG
, /* 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
,
153 TK_A_INOTIFY_WATCH
, /* int */
154 TK_A_DEVLINK_PRIO
, /* int */
155 TK_A_OWNER
, /* val */
156 TK_A_GROUP
, /* val */
158 TK_A_OWNER_ID
, /* uid_t */
159 TK_A_GROUP_ID
, /* gid_t */
160 TK_A_MODE_ID
, /* mode_t */
161 TK_A_STATIC_NODE
, /* val */
162 TK_A_ENV
, /* val, attr */
165 TK_A_DEVLINK
, /* val */
166 TK_A_EVENT_TIMEOUT
, /* int */
167 TK_A_ATTR
, /* val, attr */
168 TK_A_RUN
, /* val, bool */
169 TK_A_GOTO
, /* size_t */
174 /* we try to pack stuff in a way that we take only 12 bytes per token */
177 unsigned char type
; /* same in rule and key */
179 enum token_type type
:8;
181 bool has_static_node
:1;
182 unsigned int unused
:6;
183 unsigned short token_count
;
184 unsigned int label_off
;
185 unsigned short filename_off
;
186 unsigned short filename_line
;
189 enum token_type type
:8;
190 enum operation_type op
:8;
191 enum string_glob_type glob
:8;
192 enum string_subst_type subst
:4;
193 enum string_subst_type attrsubst
:4;
194 unsigned int value_off
;
196 unsigned int attr_off
;
199 unsigned int rule_goto
;
213 struct udev_rules
*rules
;
215 struct token token
[MAX_TK
];
216 unsigned int token_cur
;
220 static const char *operation_str(enum operation_type type
)
222 static const char *operation_strs
[] = {
223 [OP_UNSET
] = "UNSET",
224 [OP_MATCH
] = "match",
225 [OP_NOMATCH
] = "nomatch",
226 [OP_MATCH_MAX
] = "MATCH_MAX",
229 [OP_ASSIGN
] = "assign",
230 [OP_ASSIGN_FINAL
] = "assign-final",
233 return operation_strs
[type
];
236 static const char *string_glob_str(enum string_glob_type type
)
238 static const char *string_glob_strs
[] = {
239 [GL_UNSET
] = "UNSET",
240 [GL_PLAIN
] = "plain",
242 [GL_SPLIT
] = "split",
243 [GL_SPLIT_GLOB
] = "split-glob",
244 [GL_SOMETHING
] = "split-glob",
247 return string_glob_strs
[type
];
250 static const char *token_str(enum token_type type
)
252 static const char *token_strs
[] = {
253 [TK_UNSET
] = "UNSET",
256 [TK_M_ACTION
] = "M ACTION",
257 [TK_M_DEVPATH
] = "M DEVPATH",
258 [TK_M_KERNEL
] = "M KERNEL",
259 [TK_M_DEVLINK
] = "M DEVLINK",
260 [TK_M_NAME
] = "M NAME",
261 [TK_M_ENV
] = "M ENV",
262 [TK_M_TAG
] = "M TAG",
263 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
264 [TK_M_DRIVER
] = "M DRIVER",
265 [TK_M_WAITFOR
] = "M WAITFOR",
266 [TK_M_ATTR
] = "M ATTR",
268 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
269 [TK_M_KERNELS
] = "M KERNELS",
270 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
271 [TK_M_DRIVERS
] = "M DRIVERS",
272 [TK_M_ATTRS
] = "M ATTRS",
273 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
275 [TK_M_TEST
] = "M TEST",
276 [TK_M_PROGRAM
] = "M PROGRAM",
277 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
278 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
279 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
280 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
281 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
282 [TK_M_RESULT
] = "M RESULT",
283 [TK_M_MAX
] = "M MAX",
285 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
286 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
287 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
288 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
289 [TK_A_OWNER
] = "A OWNER",
290 [TK_A_GROUP
] = "A GROUP",
291 [TK_A_MODE
] = "A MODE",
292 [TK_A_OWNER_ID
] = "A OWNER_ID",
293 [TK_A_GROUP_ID
] = "A GROUP_ID",
294 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
295 [TK_A_MODE_ID
] = "A MODE_ID",
296 [TK_A_ENV
] = "A ENV",
297 [TK_A_TAG
] = "A ENV",
298 [TK_A_NAME
] = "A NAME",
299 [TK_A_DEVLINK
] = "A DEVLINK",
300 [TK_A_EVENT_TIMEOUT
] = "A EVENT_TIMEOUT",
301 [TK_A_ATTR
] = "A ATTR",
302 [TK_A_RUN
] = "A RUN",
303 [TK_A_GOTO
] = "A GOTO",
308 return token_strs
[type
];
311 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
313 enum token_type type
= token
->type
;
314 enum operation_type op
= token
->key
.op
;
315 enum string_glob_type glob
= token
->key
.glob
;
316 const char *value
= &rules
->buf
[token
->key
.value_off
];
317 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
322 const char *tks_ptr
= (char *)rules
->tokens
;
323 const char *tk_ptr
= (char *)token
;
324 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
326 dbg(rules
->udev
, "* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
327 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
328 idx
, token
->rule
.token_count
,
329 &rules
->buf
[token
->rule
.label_off
]);
341 case TK_M_SUBSYSTEMS
:
344 case TK_M_IMPORT_FILE
:
345 case TK_M_IMPORT_PROG
:
347 case TK_M_IMPORT_CMDLINE
:
348 case TK_M_IMPORT_PARENT
:
356 dbg(rules
->udev
, "%s %s '%s'(%s)\n",
357 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
364 dbg(rules
->udev
, "%s %s '%s' '%s'(%s)\n",
365 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
369 dbg(rules
->udev
, "%s %s '%s'\n", token_str(type
), operation_str(op
), value
);
371 case TK_A_STRING_ESCAPE_NONE
:
372 case TK_A_STRING_ESCAPE_REPLACE
:
373 dbg(rules
->udev
, "%s\n", token_str(type
));
376 dbg(rules
->udev
, "%s %s '%s'(%s) %#o\n",
377 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
379 case TK_A_INOTIFY_WATCH
:
380 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.watch
);
382 case TK_A_DEVLINK_PRIO
:
383 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.devlink_prio
);
386 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.uid
);
389 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.gid
);
392 dbg(rules
->udev
, "%s %s %#o\n", token_str(type
), operation_str(op
), token
->key
.mode
);
394 case TK_A_STATIC_NODE
:
395 dbg(rules
->udev
, "%s '%s'\n", token_str(type
), value
);
397 case TK_A_EVENT_TIMEOUT
:
398 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.event_timeout
);
401 dbg(rules
->udev
, "%s '%s' %u\n", token_str(type
), value
, token
->key
.rule_goto
);
404 dbg(rules
->udev
, "* %s\n", token_str(type
));
406 case TK_M_PARENTS_MIN
:
407 case TK_M_PARENTS_MAX
:
410 dbg(rules
->udev
, "unknown type %u\n", type
);
415 static void dump_rules(struct udev_rules
*rules
)
419 dbg(rules
->udev
, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
421 rules
->token_cur
* sizeof(struct token
),
424 for(i
= 0; i
< rules
->token_cur
; i
++)
425 dump_token(rules
, &rules
->tokens
[i
]);
428 static inline const char *operation_str(enum operation_type type
) { return NULL
; }
429 static inline const char *token_str(enum token_type type
) { return NULL
; }
430 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
431 static inline void dump_rules(struct udev_rules
*rules
) {}
432 #endif /* ENABLE_DEBUG */
434 static int add_new_string(struct udev_rules
*rules
, const char *str
, size_t bytes
)
438 /* grow buffer if needed */
439 if (rules
->buf_cur
+ bytes
+1 >= rules
->buf_max
) {
443 /* double the buffer size */
444 add
= rules
->buf_max
;
448 buf
= realloc(rules
->buf
, rules
->buf_max
+ add
);
451 dbg(rules
->udev
, "extend buffer from %zu to %zu\n", rules
->buf_max
, rules
->buf_max
+ add
);
453 rules
->buf_max
+= add
;
455 off
= rules
->buf_cur
;
456 memcpy(&rules
->buf
[rules
->buf_cur
], str
, bytes
);
457 rules
->buf_cur
+= bytes
;
462 static int add_string(struct udev_rules
*rules
, const char *str
)
464 unsigned int node_idx
;
465 struct trie_node
*new_node
;
466 unsigned int new_node_idx
;
471 struct trie_node
*parent
;
473 /* walk trie, start from last character of str to find matching tails */
477 for (depth
= 0; depth
<= len
; depth
++) {
478 struct trie_node
*node
;
479 unsigned int child_idx
;
481 node
= &rules
->trie_nodes
[node_idx
];
482 off
= node
->value_off
+ node
->value_len
- len
;
484 /* match against current node */
485 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(&rules
->buf
[off
], str
, len
) == 0))
488 /* lookup child node */
489 key
= str
[len
- 1 - depth
];
490 child_idx
= node
->child_idx
;
491 while (child_idx
> 0) {
492 struct trie_node
*child
;
494 child
= &rules
->trie_nodes
[child_idx
];
495 if (child
->key
== key
)
497 child_idx
= child
->next_child_idx
;
501 node_idx
= child_idx
;
504 /* string not found, add it */
505 off
= add_new_string(rules
, str
, len
+ 1);
507 /* grow trie nodes if needed */
508 if (rules
->trie_nodes_cur
>= rules
->trie_nodes_max
) {
509 struct trie_node
*nodes
;
512 /* double the buffer size */
513 add
= rules
->trie_nodes_max
;
517 nodes
= realloc(rules
->trie_nodes
, (rules
->trie_nodes_max
+ add
) * sizeof(struct trie_node
));
520 dbg(rules
->udev
, "extend trie nodes from %u to %u\n",
521 rules
->trie_nodes_max
, rules
->trie_nodes_max
+ add
);
522 rules
->trie_nodes
= nodes
;
523 rules
->trie_nodes_max
+= add
;
527 new_node_idx
= rules
->trie_nodes_cur
;
528 rules
->trie_nodes_cur
++;
529 new_node
= &rules
->trie_nodes
[new_node_idx
];
530 memset(new_node
, 0x00, sizeof(struct trie_node
));
531 new_node
->value_off
= off
;
532 new_node
->value_len
= len
;
535 /* join the parent's child list */
536 parent
= &rules
->trie_nodes
[node_idx
];
537 if (parent
->child_idx
== 0) {
538 parent
->child_idx
= new_node_idx
;
540 struct trie_node
*last_child
;
542 last_child
= &rules
->trie_nodes
[parent
->last_child_idx
];
543 last_child
->next_child_idx
= new_node_idx
;
545 parent
->last_child_idx
= new_node_idx
;
549 static int add_token(struct udev_rules
*rules
, struct token
*token
)
551 /* grow buffer if needed */
552 if (rules
->token_cur
+1 >= rules
->token_max
) {
553 struct token
*tokens
;
556 /* double the buffer size */
557 add
= rules
->token_max
;
561 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
564 dbg(rules
->udev
, "extend tokens from %u to %u\n", rules
->token_max
, rules
->token_max
+ add
);
565 rules
->tokens
= tokens
;
566 rules
->token_max
+= add
;
568 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
573 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
579 /* lookup, if we know it already */
580 for (i
= 0; i
< rules
->uids_cur
; i
++) {
581 off
= rules
->uids
[i
].name_off
;
582 if (strcmp(&rules
->buf
[off
], owner
) == 0) {
583 uid
= rules
->uids
[i
].uid
;
584 dbg(rules
->udev
, "return existing %u for '%s'\n", uid
, owner
);
588 uid
= util_lookup_user(rules
->udev
, owner
);
590 /* grow buffer if needed */
591 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
592 struct uid_gid
*uids
;
595 /* double the buffer size */
596 add
= rules
->uids_max
;
600 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
603 dbg(rules
->udev
, "extend uids from %u to %u\n", rules
->uids_max
, rules
->uids_max
+ add
);
605 rules
->uids_max
+= add
;
607 rules
->uids
[rules
->uids_cur
].uid
= uid
;
608 off
= add_string(rules
, owner
);
611 rules
->uids
[rules
->uids_cur
].name_off
= off
;
616 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
622 /* lookup, if we know it already */
623 for (i
= 0; i
< rules
->gids_cur
; i
++) {
624 off
= rules
->gids
[i
].name_off
;
625 if (strcmp(&rules
->buf
[off
], group
) == 0) {
626 gid
= rules
->gids
[i
].gid
;
627 dbg(rules
->udev
, "return existing %u for '%s'\n", gid
, group
);
631 gid
= util_lookup_group(rules
->udev
, group
);
633 /* grow buffer if needed */
634 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
635 struct uid_gid
*gids
;
638 /* double the buffer size */
639 add
= rules
->gids_max
;
643 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
646 dbg(rules
->udev
, "extend gids from %u to %u\n", rules
->gids_max
, rules
->gids_max
+ add
);
648 rules
->gids_max
+= add
;
650 rules
->gids
[rules
->gids_cur
].gid
= gid
;
651 off
= add_string(rules
, group
);
654 rules
->gids
[rules
->gids_cur
].name_off
= off
;
659 static int import_property_from_string(struct udev_device
*dev
, char *line
)
661 struct udev
*udev
= udev_device_get_udev(dev
);
668 while (isspace(key
[0]))
671 /* comment or empty line */
672 if (key
[0] == '#' || key
[0] == '\0')
675 /* split key/value */
676 val
= strchr(key
, '=');
683 while (isspace(val
[0]))
690 while (isspace(key
[len
-1]))
694 /* terminate value */
698 while (isspace(val
[len
-1]))
706 if (val
[0] == '"' || val
[0] == '\'') {
707 if (val
[len
-1] != val
[0]) {
708 info(udev
, "inconsistent quoting: '%s', skip\n", line
);
715 dbg(udev
, "adding '%s'='%s'\n", key
, val
);
717 /* handle device, renamed by external tool, returning new path */
718 if (strcmp(key
, "DEVPATH") == 0) {
719 char syspath
[UTIL_PATH_SIZE
];
721 info(udev
, "updating devpath from '%s' to '%s'\n",
722 udev_device_get_devpath(dev
), val
);
723 util_strscpyl(syspath
, sizeof(syspath
), udev_get_sys_path(udev
), val
, NULL
);
724 udev_device_set_syspath(dev
, syspath
);
726 struct udev_list_entry
*entry
;
728 entry
= udev_device_add_property(dev
, key
, val
);
729 /* store in db, skip private keys */
731 udev_list_entry_set_flags(entry
, 1);
736 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
739 char line
[UTIL_LINE_SIZE
];
741 f
= fopen(filename
, "r");
744 while (fgets(line
, sizeof(line
), f
) != NULL
)
745 import_property_from_string(dev
, line
);
750 static int import_program_into_properties(struct udev_device
*dev
, const char *program
)
752 struct udev
*udev
= udev_device_get_udev(dev
);
754 char result
[UTIL_LINE_SIZE
];
758 envp
= udev_device_get_properties_envp(dev
);
759 if (util_run_program(udev
, program
, envp
, result
, sizeof(result
), &reslen
, NULL
, false) != 0)
763 while (line
!= NULL
) {
766 pos
= strchr(line
, '\n');
771 import_property_from_string(dev
, line
);
777 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
779 struct udev
*udev
= udev_device_get_udev(dev
);
780 struct udev_device
*dev_parent
;
781 struct udev_list_entry
*list_entry
;
783 dev_parent
= udev_device_get_parent(dev
);
784 if (dev_parent
== NULL
)
787 dbg(udev
, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent
));
788 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
789 const char *key
= udev_list_entry_get_name(list_entry
);
790 const char *val
= udev_list_entry_get_value(list_entry
);
792 if (fnmatch(filter
, key
, 0) == 0) {
793 struct udev_list_entry
*entry
;
795 dbg(udev
, "import key '%s=%s'\n", key
, val
);
796 entry
= udev_device_add_property(dev
, key
, val
);
797 /* store in db, skip private keys */
799 udev_list_entry_set_flags(entry
, 1);
805 #define WAIT_LOOP_PER_SECOND 50
806 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
808 struct udev
*udev
= udev_device_get_udev(dev
);
809 char filepath
[UTIL_PATH_SIZE
];
810 char devicepath
[UTIL_PATH_SIZE
];
812 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
814 /* a relative path is a device attribute */
815 devicepath
[0] = '\0';
816 if (file
[0] != '/') {
817 util_strscpyl(devicepath
, sizeof(devicepath
),
818 udev_get_sys_path(udev
), udev_device_get_devpath(dev
), NULL
);
819 util_strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
823 dbg(udev
, "will wait %i sec for '%s'\n", timeout
, file
);
825 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
828 if (stat(file
, &stats
) == 0) {
829 info(udev
, "file '%s' appeared after %i loops\n", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
832 /* make sure, the device did not disappear in the meantime */
833 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
834 info(udev
, "device disappeared while waiting for '%s'\n", file
);
837 info(udev
, "wait for '%s' for %i mseconds\n", file
, 1000 / WAIT_LOOP_PER_SECOND
);
838 nanosleep(&duration
, NULL
);
840 info(udev
, "waiting for '%s' failed\n", file
);
844 static int attr_subst_subdir(char *attr
, size_t len
)
848 if (strstr(attr
, "/*/")) {
850 char dirname
[UTIL_PATH_SIZE
];
854 util_strscpy(dirname
, sizeof(dirname
), attr
);
855 pos
= strstr(dirname
, "/*/");
860 dir
= opendir(dirname
);
864 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
867 if (dent
->d_name
[0] == '.')
869 util_strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
870 if (stat(attr
, &stats
) == 0) {
882 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
888 if (linepos
== NULL
|| linepos
[0] == '\0')
891 /* skip whitespace */
892 while (isspace(linepos
[0]) || linepos
[0] == ',')
896 if (linepos
[0] == '\0')
902 if (linepos
[0] == '\0')
904 if (isspace(linepos
[0]))
906 if (linepos
[0] == '=')
908 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
909 if (linepos
[1] == '=')
913 /* remember end of key */
916 /* skip whitespace after key */
917 while (isspace(linepos
[0]))
919 if (linepos
[0] == '\0')
922 /* get operation type */
923 if (linepos
[0] == '=' && linepos
[1] == '=') {
926 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
929 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
932 } else if (linepos
[0] == '=') {
935 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
936 *op
= OP_ASSIGN_FINAL
;
944 /* skip whitespace after operator */
945 while (isspace(linepos
[0]))
947 if (linepos
[0] == '\0')
951 if (linepos
[0] == '"')
958 temp
= strchr(linepos
, '"');
963 dbg(udev
, "%s '%s'-'%s'\n", operation_str(*op
), *key
, *value
);
965 /* move line to next key */
970 /* extract possible KEY{attr} */
971 static char *get_key_attribute(struct udev
*udev
, char *str
)
976 attr
= strchr(str
, '{');
979 pos
= strchr(attr
, '}');
981 err(udev
, "missing closing brace for format\n");
985 dbg(udev
, "attribute='%s'\n", attr
);
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
:
1013 case TK_M_IMPORT_FILE
:
1014 case TK_M_IMPORT_PROG
:
1015 case TK_M_IMPORT_DB
:
1016 case TK_M_IMPORT_CMDLINE
:
1017 case TK_M_IMPORT_PARENT
:
1026 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1034 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1035 token
->key
.attr_off
= add_string(rule_tmp
->rules
, attr
);
1038 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1039 token
->key
.devlink_unique
= *(int *)data
;
1042 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1044 token
->key
.mode
= *(mode_t
*)data
;
1046 case TK_A_STRING_ESCAPE_NONE
:
1047 case TK_A_STRING_ESCAPE_REPLACE
:
1050 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1051 token
->key
.fail_on_error
= *(int *)data
;
1053 case TK_A_INOTIFY_WATCH
:
1054 case TK_A_DEVLINK_PRIO
:
1055 token
->key
.devlink_prio
= *(int *)data
;
1058 token
->key
.uid
= *(uid_t
*)data
;
1061 token
->key
.gid
= *(gid_t
*)data
;
1064 token
->key
.mode
= *(mode_t
*)data
;
1066 case TK_A_STATIC_NODE
:
1067 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1069 case TK_A_EVENT_TIMEOUT
:
1070 token
->key
.event_timeout
= *(int *)data
;
1073 case TK_M_PARENTS_MIN
:
1074 case TK_M_PARENTS_MAX
:
1078 err(rule_tmp
->rules
->udev
, "wrong type %u\n", type
);
1082 if (value
!= NULL
&& type
< TK_M_MAX
) {
1083 /* check if we need to split or call fnmatch() while matching rules */
1084 enum string_glob_type glob
;
1088 has_split
= (strchr(value
, '|') != NULL
);
1089 has_glob
= (strchr(value
, '*') != NULL
|| strchr(value
, '?') != NULL
|| strchr(value
, '[') != NULL
);
1090 if (has_split
&& has_glob
) {
1091 glob
= GL_SPLIT_GLOB
;
1092 } else if (has_split
) {
1094 } else if (has_glob
) {
1095 if (strcmp(value
, "?*") == 0)
1096 glob
= GL_SOMETHING
;
1102 token
->key
.glob
= glob
;
1105 if (value
!= NULL
&& type
> TK_M_MAX
) {
1106 /* check if assigned value has substitution chars */
1107 if (value
[0] == '[')
1108 token
->key
.subst
= SB_SUBSYS
;
1109 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
1110 token
->key
.subst
= SB_FORMAT
;
1112 token
->key
.subst
= SB_NONE
;
1116 /* check if property/attribut name has substitution chars */
1118 token
->key
.attrsubst
= SB_SUBSYS
;
1119 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
1120 token
->key
.attrsubst
= SB_FORMAT
;
1122 token
->key
.attrsubst
= SB_NONE
;
1125 token
->key
.type
= type
;
1127 rule_tmp
->token_cur
++;
1128 if (rule_tmp
->token_cur
>= ARRAY_SIZE(rule_tmp
->token
)) {
1129 err(rule_tmp
->rules
->udev
, "temporary rule array too small\n");
1135 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1138 unsigned int start
= 0;
1139 unsigned int end
= rule_tmp
->token_cur
;
1141 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1142 enum token_type next_val
= TK_UNSET
;
1143 unsigned int next_idx
= 0;
1146 /* find smallest value */
1147 for (j
= start
; j
< end
; j
++) {
1148 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1150 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1151 next_val
= rule_tmp
->token
[j
].type
;
1156 /* add token and mark done */
1157 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1159 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1162 if (next_idx
== start
)
1164 if (next_idx
+1 == end
)
1170 static int add_rule(struct udev_rules
*rules
, char *line
,
1171 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1175 struct rule_tmp rule_tmp
;
1176 bool bus_warn
= false;
1177 bool sysfs_warn
= false;
1178 bool id_warn
= false;
1180 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1181 rule_tmp
.rules
= rules
;
1182 rule_tmp
.rule
.type
= TK_RULE
;
1183 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1184 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1190 enum operation_type op
;
1192 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0)
1195 if (strcmp(key
, "ACTION") == 0) {
1196 if (op
> OP_MATCH_MAX
) {
1197 err(rules
->udev
, "invalid ACTION operation\n");
1200 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1204 if (strcmp(key
, "DEVPATH") == 0) {
1205 if (op
> OP_MATCH_MAX
) {
1206 err(rules
->udev
, "invalid DEVPATH operation\n");
1209 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1213 if (strcmp(key
, "KERNEL") == 0) {
1214 if (op
> OP_MATCH_MAX
) {
1215 err(rules
->udev
, "invalid KERNEL operation\n");
1218 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1222 if (strcmp(key
, "SUBSYSTEM") == 0) {
1223 if (op
> OP_MATCH_MAX
) {
1224 err(rules
->udev
, "invalid SUBSYSTEM operation\n");
1227 /* bus, class, subsystem events should all be the same */
1228 if (strcmp(value
, "subsystem") == 0 ||
1229 strcmp(value
, "bus") == 0 ||
1230 strcmp(value
, "class") == 0) {
1231 if (strcmp(value
, "bus") == 0 || strcmp(value
, "class") == 0)
1232 err(rules
->udev
, "'%s' must be specified as 'subsystem' \n"
1233 "please fix it in %s:%u", value
, filename
, lineno
);
1234 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1236 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1240 if (strcmp(key
, "DRIVER") == 0) {
1241 if (op
> OP_MATCH_MAX
) {
1242 err(rules
->udev
, "invalid DRIVER operation\n");
1245 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1249 if (strncmp(key
, "ATTR{", sizeof("ATTR{")-1) == 0) {
1250 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1252 err(rules
->udev
, "error parsing ATTR attribute\n");
1255 if (op
< OP_MATCH_MAX
) {
1256 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1258 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1263 if (strcmp(key
, "KERNELS") == 0) {
1264 if (op
> OP_MATCH_MAX
) {
1265 err(rules
->udev
, "invalid KERNELS operation\n");
1268 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1272 if (strcmp(key
, "ID") == 0) {
1275 err(rules
->udev
, "ID= will be removed in a future udev version, "
1276 "please use KERNEL= to match the event device, or KERNELS= "
1277 "to match a parent device, in %s:%u\n", filename
, lineno
);
1279 if (op
> OP_MATCH_MAX
) {
1280 err(rules
->udev
, "invalid KERNELS operation\n");
1283 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1287 if (strcmp(key
, "SUBSYSTEMS") == 0) {
1288 if (op
> OP_MATCH_MAX
) {
1289 err(rules
->udev
, "invalid SUBSYSTEMS operation\n");
1292 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1296 if (strcmp(key
, "BUS") == 0) {
1299 err(rules
->udev
, "BUS= will be removed in a future udev version, "
1300 "please use SUBSYSTEM= to match the event device, or SUBSYSTEMS= "
1301 "to match a parent device, in %s:%u\n", filename
, lineno
);
1303 if (op
> OP_MATCH_MAX
) {
1304 err(rules
->udev
, "invalid SUBSYSTEMS operation\n");
1307 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1311 if (strcmp(key
, "DRIVERS") == 0) {
1312 if (op
> OP_MATCH_MAX
) {
1313 err(rules
->udev
, "invalid DRIVERS operation\n");
1316 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1320 if (strncmp(key
, "ATTRS{", sizeof("ATTRS{")-1) == 0) {
1321 if (op
> OP_MATCH_MAX
) {
1322 err(rules
->udev
, "invalid ATTRS operation\n");
1325 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1327 err(rules
->udev
, "error parsing ATTRS attribute\n");
1330 if (strncmp(attr
, "device/", 7) == 0)
1331 err(rules
->udev
, "the 'device' link may not be available in a future kernel, "
1332 "please fix it in %s:%u", filename
, lineno
);
1333 else if (strstr(attr
, "../") != NULL
)
1334 err(rules
->udev
, "do not reference parent sysfs directories directly, "
1335 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1336 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1340 if (strncmp(key
, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1343 err(rules
->udev
, "SYSFS{}= will be removed in a future udev version, "
1344 "please use ATTR{}= to match the event device, or ATTRS{}= "
1345 "to match a parent device, in %s:%u\n", filename
, lineno
);
1347 if (op
> OP_MATCH_MAX
) {
1348 err(rules
->udev
, "invalid ATTRS operation\n");
1351 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1353 err(rules
->udev
, "error parsing ATTRS attribute\n");
1356 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1360 if (strncmp(key
, "ENV{", sizeof("ENV{")-1) == 0) {
1361 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1363 err(rules
->udev
, "error parsing ENV attribute\n");
1366 if (op
< OP_MATCH_MAX
) {
1367 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1370 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1376 if (strcmp(key
, "TAG") == 0) {
1377 if (op
< OP_MATCH_MAX
)
1378 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1380 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1384 if (strcmp(key
, "PROGRAM") == 0) {
1385 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1389 if (strcmp(key
, "RESULT") == 0) {
1390 if (op
> OP_MATCH_MAX
) {
1391 err(rules
->udev
, "invalid RESULT operation\n");
1394 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1398 if (strncmp(key
, "IMPORT", sizeof("IMPORT")-1) == 0) {
1399 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1400 if (attr
!= NULL
&& strstr(attr
, "program")) {
1401 dbg(rules
->udev
, "IMPORT will be executed\n");
1402 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1403 } else if (attr
!= NULL
&& strstr(attr
, "file")) {
1404 dbg(rules
->udev
, "IMPORT will be included as file\n");
1405 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1406 } else if (attr
!= NULL
&& strstr(attr
, "db")) {
1407 dbg(rules
->udev
, "IMPORT will include db values\n");
1408 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1409 } else if (attr
!= NULL
&& strstr(attr
, "cmdline")) {
1410 dbg(rules
->udev
, "IMPORT will include db values\n");
1411 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1412 } else if (attr
!= NULL
&& strstr(attr
, "parent")) {
1413 dbg(rules
->udev
, "IMPORT will include the parent values\n");
1414 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1416 /* figure it out if it is executable */
1417 char file
[UTIL_PATH_SIZE
];
1419 struct stat statbuf
;
1421 /* allow programs in /lib/udev called without the path */
1422 if (value
[0] != '/')
1423 util_strscpyl(file
, sizeof(file
), LIBEXECDIR
"/", value
, NULL
);
1425 util_strscpy(file
, sizeof(file
), value
);
1426 pos
= strchr(file
, ' ');
1429 dbg(rules
->udev
, "IMPORT auto mode for '%s'\n", file
);
1430 if (stat(file
, &statbuf
) == 0 && (statbuf
.st_mode
& S_IXUSR
)) {
1431 dbg(rules
->udev
, "IMPORT will be executed (autotype)\n");
1432 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1434 dbg(rules
->udev
, "IMPORT will be included as file (autotype)\n");
1435 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1441 if (strncmp(key
, "TEST", sizeof("TEST")-1) == 0) {
1444 if (op
> OP_MATCH_MAX
) {
1445 err(rules
->udev
, "invalid TEST operation\n");
1448 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1450 mode
= strtol(attr
, NULL
, 8);
1451 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1453 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1458 if (strncmp(key
, "RUN", sizeof("RUN")-1) == 0) {
1461 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1462 if (attr
!= NULL
&& strstr(attr
, "fail_event_on_error"))
1464 rule_add_key(&rule_tmp
, TK_A_RUN
, op
, value
, &flag
);
1468 if (strcmp(key
, "WAIT_FOR") == 0 || strcmp(key
, "WAIT_FOR_SYSFS") == 0) {
1469 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1473 if (strcmp(key
, "LABEL") == 0) {
1474 rule_tmp
.rule
.rule
.label_off
= add_string(rules
, value
);
1478 if (strcmp(key
, "GOTO") == 0) {
1479 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1483 if (strncmp(key
, "NAME", sizeof("NAME")-1) == 0) {
1484 if (op
< OP_MATCH_MAX
) {
1485 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1487 if (strcmp(value
, "%k") == 0) {
1488 err(rules
->udev
, "NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1489 "please remove it from %s:%u\n", filename
, lineno
);
1492 if (value
[0] == '\0') {
1493 info(rules
->udev
, "NAME=\"\" is ignored, because udev will not delete any device nodes, "
1494 "please remove it from %s:%u\n", filename
, lineno
);
1497 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1499 rule_tmp
.rule
.rule
.can_set_name
= true;
1503 if (strncmp(key
, "SYMLINK", sizeof("SYMLINK")-1) == 0) {
1504 if (op
< OP_MATCH_MAX
) {
1505 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1509 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("SYMLINK")-1);
1510 if (attr
!= NULL
&& strstr(attr
, "unique") != NULL
)
1512 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, &flag
);
1514 rule_tmp
.rule
.rule
.can_set_name
= true;
1518 if (strcmp(key
, "OWNER") == 0) {
1522 uid
= strtoul(value
, &endptr
, 10);
1523 if (endptr
[0] == '\0') {
1524 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1525 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1526 uid
= add_uid(rules
, value
);
1527 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1528 } else if (rules
->resolve_names
>= 0) {
1529 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1531 rule_tmp
.rule
.rule
.can_set_name
= true;
1535 if (strcmp(key
, "GROUP") == 0) {
1539 gid
= strtoul(value
, &endptr
, 10);
1540 if (endptr
[0] == '\0') {
1541 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1542 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1543 gid
= add_gid(rules
, value
);
1544 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1545 } else if (rules
->resolve_names
>= 0) {
1546 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1548 rule_tmp
.rule
.rule
.can_set_name
= true;
1552 if (strcmp(key
, "MODE") == 0) {
1556 mode
= strtol(value
, &endptr
, 8);
1557 if (endptr
[0] == '\0')
1558 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1560 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1561 rule_tmp
.rule
.rule
.can_set_name
= true;
1565 if (strcmp(key
, "OPTIONS") == 0) {
1568 pos
= strstr(value
, "link_priority=");
1570 int prio
= atoi(&pos
[strlen("link_priority=")]);
1572 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1573 dbg(rules
->udev
, "link priority=%i\n", prio
);
1575 pos
= strstr(value
, "event_timeout=");
1577 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1579 rule_add_key(&rule_tmp
, TK_A_EVENT_TIMEOUT
, op
, NULL
, &tout
);
1580 dbg(rules
->udev
, "event timeout=%i\n", tout
);
1582 pos
= strstr(value
, "string_escape=");
1584 pos
= &pos
[strlen("string_escape=")];
1585 if (strncmp(pos
, "none", strlen("none")) == 0)
1586 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1587 else if (strncmp(pos
, "replace", strlen("replace")) == 0)
1588 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1590 pos
= strstr(value
, "nowatch");
1594 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1595 dbg(rules
->udev
, "inotify watch of device disabled\n");
1597 pos
= strstr(value
, "watch");
1601 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1602 dbg(rules
->udev
, "inotify watch of device requested\n");
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;
1612 err(rules
->udev
, "unknown key '%s' in %s:%u\n", key
, filename
, lineno
);
1615 /* add rule token */
1616 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1617 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1620 /* add tokens to list, sorted by type */
1621 if (sort_token(rules
, &rule_tmp
) != 0)
1626 err(rules
->udev
, "invalid rule '%s:%u'\n", filename
, lineno
);
1630 static int parse_file(struct udev_rules
*rules
, const char *filename
, unsigned short filename_off
)
1633 unsigned int first_token
;
1634 char line
[UTIL_LINE_SIZE
];
1638 info(rules
->udev
, "reading '%s' as rules file\n", filename
);
1640 f
= fopen(filename
, "r");
1644 first_token
= rules
->token_cur
;
1646 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1650 /* skip whitespace */
1653 while (isspace(key
[0]))
1664 /* continue reading if backslash+newline is found */
1665 while (line
[len
-2] == '\\') {
1666 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1668 if (strlen(&line
[len
-2]) < 2)
1674 if (len
+1 >= sizeof(line
)) {
1675 err(rules
->udev
, "line too long '%s':%u, ignored\n", filename
, line_nr
);
1678 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1682 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1683 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1684 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1685 char *label
= &rules
->buf
[rules
->tokens
[i
].key
.value_off
];
1688 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1689 if (rules
->tokens
[j
].type
!= TK_RULE
)
1691 if (rules
->tokens
[j
].rule
.label_off
== 0)
1693 if (strcmp(label
, &rules
->buf
[rules
->tokens
[j
].rule
.label_off
]) != 0)
1695 rules
->tokens
[i
].key
.rule_goto
= j
;
1698 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1699 err(rules
->udev
, "GOTO '%s' has no matching label in: '%s'\n", label
, filename
);
1705 static int add_matching_files(struct udev
*udev
, struct udev_list_node
*file_list
, const char *dirname
, const char *suffix
)
1708 char filename
[UTIL_PATH_SIZE
];
1710 dbg(udev
, "open directory '%s'\n", dirname
);
1711 dir
= opendir(dirname
);
1713 info(udev
, "unable to open '%s': %m\n", dirname
);
1718 struct dirent
*dent
;
1720 dent
= readdir(dir
);
1721 if (dent
== NULL
|| dent
->d_name
[0] == '\0')
1724 if (dent
->d_name
[0] == '.')
1727 /* look for file matching with specified suffix */
1728 if (suffix
!= NULL
) {
1731 ext
= strrchr(dent
->d_name
, '.');
1734 if (strcmp(ext
, suffix
) != 0)
1737 util_strscpyl(filename
, sizeof(filename
), dirname
, "/", dent
->d_name
, NULL
);
1738 dbg(udev
, "put file '%s' into list\n", filename
);
1739 udev_list_entry_add(udev
, file_list
, filename
, NULL
, 1, 1);
1746 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1748 struct udev_rules
*rules
;
1749 struct stat statbuf
;
1750 struct udev_list_node file_list
;
1751 struct udev_list_entry
*file_loop
, *file_tmp
;
1752 struct token end_token
;
1754 rules
= calloc(1, sizeof(struct udev_rules
));
1758 rules
->resolve_names
= resolve_names
;
1759 udev_list_init(&file_list
);
1761 /* init token array and string buffer */
1762 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1763 if (rules
->tokens
== NULL
) {
1767 rules
->token_max
= PREALLOC_TOKEN
;
1769 rules
->buf
= malloc(PREALLOC_STRBUF
);
1770 if (rules
->buf
== NULL
) {
1771 free(rules
->tokens
);
1775 rules
->buf_max
= PREALLOC_STRBUF
;
1776 /* offset 0 is always '\0' */
1777 rules
->buf
[0] = '\0';
1779 dbg(udev
, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1780 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1782 rules
->trie_nodes
= malloc(PREALLOC_TRIE
* sizeof(struct trie_node
));
1783 if (rules
->trie_nodes
== NULL
) {
1785 free(rules
->tokens
);
1789 rules
->trie_nodes_max
= PREALLOC_TRIE
;
1790 /* offset 0 is the trie root, with an empty string */
1791 memset(rules
->trie_nodes
, 0x00, sizeof(struct trie_node
));
1792 rules
->trie_nodes_cur
= 1;
1794 if (udev_get_rules_path(udev
) != NULL
) {
1795 /* custom rules location for testing */
1796 add_matching_files(udev
, &file_list
, udev_get_rules_path(udev
), ".rules");
1798 char filename
[PATH_MAX
];
1799 struct udev_list_node sort_list
;
1800 struct udev_list_entry
*sort_loop
, *sort_tmp
;
1802 /* read user/custom rules */
1803 add_matching_files(udev
, &file_list
, SYSCONFDIR
"/udev/rules.d", ".rules");
1805 /* read dynamic/temporary rules */
1806 util_strscpyl(filename
, sizeof(filename
), udev_get_run_path(udev
), "/rules.d", NULL
);
1807 udev_list_init(&sort_list
);
1808 add_matching_files(udev
, &sort_list
, filename
, ".rules");
1810 /* read default rules */
1811 add_matching_files(udev
, &sort_list
, LIBEXECDIR
"/rules.d", ".rules");
1813 /* sort all rules files by basename into list of files */
1814 udev_list_entry_foreach_safe(sort_loop
, sort_tmp
, udev_list_get_entry(&sort_list
)) {
1815 const char *sort_name
= udev_list_entry_get_name(sort_loop
);
1816 const char *sort_base
= strrchr(sort_name
, '/');
1818 if (sort_base
== NULL
)
1820 /* sort entry into existing list */
1821 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1822 const char *file_name
= udev_list_entry_get_name(file_loop
);
1823 const char *file_base
= strrchr(file_name
, '/');
1825 if (file_base
== NULL
)
1827 if (strcmp(file_base
, sort_base
) == 0) {
1828 info(udev
, "rule file basename '%s' already added, ignoring '%s'\n",
1829 file_name
, sort_name
);
1830 udev_list_entry_delete(sort_loop
);
1834 if (strcmp(file_base
, sort_base
) > 0) {
1835 /* found later file, insert before */
1836 udev_list_entry_remove(sort_loop
);
1837 udev_list_entry_insert_before(sort_loop
, file_loop
);
1842 /* current file already handled */
1843 if (sort_loop
== NULL
)
1845 /* no later file, append to end of list */
1846 udev_list_entry_remove(sort_loop
);
1847 udev_list_entry_append(sort_loop
, &file_list
);
1851 /* add all filenames to the string buffer */
1852 udev_list_entry_foreach(file_loop
, udev_list_get_entry(&file_list
)) {
1853 const char *filename
= udev_list_entry_get_name(file_loop
);
1854 unsigned int filename_off
;
1856 filename_off
= add_string(rules
, filename
);
1857 /* the offset in the rule is limited to unsigned short */
1858 if (filename_off
< USHRT_MAX
)
1859 udev_list_entry_set_flags(file_loop
, filename_off
);
1862 /* parse list of files */
1863 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1864 const char *filename
= udev_list_entry_get_name(file_loop
);
1865 unsigned int filename_off
= udev_list_entry_get_flags(file_loop
);
1867 if (stat(filename
, &statbuf
) == 0 && statbuf
.st_size
> 0)
1868 parse_file(rules
, filename
, filename_off
);
1870 err(udev
, "can not read '%s'\n", filename
);
1871 udev_list_entry_delete(file_loop
);
1874 memset(&end_token
, 0x00, sizeof(struct token
));
1875 end_token
.type
= TK_END
;
1876 add_token(rules
, &end_token
);
1878 /* shrink allocated token and string buffer */
1879 if (rules
->token_cur
< rules
->token_max
) {
1880 struct token
*tokens
;
1882 tokens
= realloc(rules
->tokens
, rules
->token_cur
* sizeof(struct token
));
1883 if (tokens
!= NULL
|| rules
->token_cur
== 0) {
1884 rules
->tokens
= tokens
;
1885 rules
->token_max
= rules
->token_cur
;
1888 if (rules
->buf_cur
< rules
->buf_max
) {
1891 buf
= realloc(rules
->buf
, rules
->buf_cur
);
1892 if (buf
!= NULL
|| rules
->buf_cur
== 0) {
1894 rules
->buf_max
= rules
->buf_cur
;
1897 info(udev
, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1898 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1899 info(udev
, "temporary index used %zu bytes (%u * %zu bytes)\n",
1900 rules
->trie_nodes_cur
* sizeof(struct trie_node
),
1901 rules
->trie_nodes_cur
, sizeof(struct trie_node
));
1904 free(rules
->trie_nodes
);
1905 rules
->trie_nodes
= NULL
;
1906 rules
->trie_nodes_cur
= 0;
1907 rules
->trie_nodes_max
= 0;
1909 /* cleanup uid/gid cache */
1912 rules
->uids_cur
= 0;
1913 rules
->uids_max
= 0;
1916 rules
->gids_cur
= 0;
1917 rules
->gids_max
= 0;
1923 void udev_rules_unref(struct udev_rules
*rules
)
1927 free(rules
->tokens
);
1929 free(rules
->trie_nodes
);
1935 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1937 char *key_value
= &rules
->buf
[token
->key
.value_off
];
1944 switch (token
->key
.glob
) {
1946 match
= (strcmp(key_value
, val
) == 0);
1949 match
= (fnmatch(key_value
, val
, 0) == 0);
1956 split
= &rules
->buf
[token
->key
.value_off
];
1961 next
= strchr(split
, '|');
1963 size_t matchlen
= (size_t)(next
- split
);
1965 match
= (matchlen
== len
&& strncmp(split
, val
, matchlen
) == 0);
1969 match
= (strcmp(split
, val
) == 0);
1978 char value
[UTIL_PATH_SIZE
];
1980 util_strscpy(value
, sizeof(value
), &rules
->buf
[token
->key
.value_off
]);
1982 while (key_value
!= NULL
) {
1983 pos
= strchr(key_value
, '|');
1988 dbg(rules
->udev
, "match %s '%s' <-> '%s'\n", token_str(token
->type
), key_value
, val
);
1989 match
= (fnmatch(key_value
, val
, 0) == 0);
1997 match
= (val
[0] != '\0');
2003 if (match
&& (token
->key
.op
== OP_MATCH
)) {
2004 dbg(rules
->udev
, "%s is true (matching value)\n", token_str(token
->type
));
2007 if (!match
&& (token
->key
.op
== OP_NOMATCH
)) {
2008 dbg(rules
->udev
, "%s is true (non-matching value)\n", token_str(token
->type
));
2011 dbg(rules
->udev
, "%s is not true\n", token_str(token
->type
));
2015 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
2018 char nbuf
[UTIL_NAME_SIZE
];
2020 char vbuf
[UTIL_NAME_SIZE
];
2023 name
= &rules
->buf
[cur
->key
.attr_off
];
2024 switch (cur
->key
.attrsubst
) {
2026 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
2030 value
= udev_device_get_sysattr_value(dev
, name
);
2035 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
2043 /* remove trailing whitespace, if not asked to match for it */
2044 len
= strlen(value
);
2045 if (len
> 0 && isspace(value
[len
-1])) {
2046 const char *key_value
;
2049 key_value
= &rules
->buf
[cur
->key
.value_off
];
2050 klen
= strlen(key_value
);
2051 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
2052 if (value
!= vbuf
) {
2053 util_strscpy(vbuf
, sizeof(vbuf
), value
);
2056 while (len
> 0 && isspace(vbuf
[--len
]))
2058 dbg(rules
->udev
, "removed trailing whitespace from '%s'\n", value
);
2062 return match_key(rules
, cur
, value
);
2071 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
)
2075 enum escape_type esc
= ESCAPE_UNSET
;
2078 if (rules
->tokens
== NULL
)
2081 can_set_name
= ((strcmp(udev_device_get_action(event
->dev
), "remove") != 0) &&
2082 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
2083 udev_device_get_ifindex(event
->dev
) > 0));
2085 /* loop through token list, match, run actions or forward to next rule */
2086 cur
= &rules
->tokens
[0];
2089 dump_token(rules
, cur
);
2090 switch (cur
->type
) {
2094 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2095 if (!can_set_name
&& rule
->rule
.can_set_name
)
2100 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
2104 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
2108 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
2113 size_t devlen
= strlen(udev_get_dev_path(event
->udev
))+1;
2114 struct udev_list_entry
*list_entry
;
2117 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
2118 const char *devlink
;
2120 devlink
= &udev_list_entry_get_name(list_entry
)[devlen
];
2121 if (match_key(rules
, cur
, devlink
) == 0) {
2131 if (match_key(rules
, cur
, event
->name
) != 0)
2136 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2139 value
= udev_device_get_property_value(event
->dev
, key_name
);
2140 if (value
== NULL
) {
2141 dbg(event
->udev
, "ENV{%s} is not set, treat as empty\n", key_name
);
2144 if (match_key(rules
, cur
, value
))
2150 struct udev_list_entry
*list_entry
;
2153 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
2154 if (strcmp(&rules
->buf
[cur
->key
.value_off
], udev_list_entry_get_name(list_entry
)) == 0) {
2159 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
2163 case TK_M_SUBSYSTEM
:
2164 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
2168 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
2173 char filename
[UTIL_PATH_SIZE
];
2176 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2177 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
2178 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
2183 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
2187 case TK_M_SUBSYSTEMS
:
2193 /* get whole sequence of parent matches */
2195 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2198 /* loop over parents */
2199 event
->dev_parent
= event
->dev
;
2203 dbg(event
->udev
, "parent: '%s'\n", udev_device_get_syspath(event
->dev_parent
));
2204 /* loop over sequence of parent match keys */
2205 for (key
= cur
; key
< next
; key
++ ) {
2206 dump_token(rules
, key
);
2209 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2212 case TK_M_SUBSYSTEMS
:
2213 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2217 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2221 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2227 dbg(event
->udev
, "parent key matched\n");
2229 dbg(event
->udev
, "all parent keys matched\n");
2230 /* all keys matched */
2234 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2235 if (event
->dev_parent
== NULL
)
2238 /* move behind our sequence of parent match keys */
2244 char filename
[UTIL_PATH_SIZE
];
2245 struct stat statbuf
;
2248 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2249 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2250 if (filename
[0] != '/') {
2251 char tmp
[UTIL_PATH_SIZE
];
2253 util_strscpy(tmp
, sizeof(tmp
), filename
);
2254 util_strscpyl(filename
, sizeof(filename
),
2255 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2258 attr_subst_subdir(filename
, sizeof(filename
));
2260 match
= (stat(filename
, &statbuf
) == 0);
2261 dbg(event
->udev
, "'%s' %s", filename
, match
? "exists\n" : "does not exist\n");
2262 if (match
&& cur
->key
.mode
> 0) {
2263 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2264 dbg(event
->udev
, "'%s' has mode=%#o and %s %#o\n", filename
, statbuf
.st_mode
,
2265 match
? "matches" : "does not match", cur
->key
.mode
);
2267 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2269 if (!match
&& cur
->key
.op
== OP_MATCH
)
2275 char program
[UTIL_PATH_SIZE
];
2277 char result
[UTIL_PATH_SIZE
];
2279 free(event
->program_result
);
2280 event
->program_result
= NULL
;
2281 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], program
, sizeof(program
));
2282 envp
= udev_device_get_properties_envp(event
->dev
);
2283 info(event
->udev
, "PROGRAM '%s' %s:%u\n",
2285 &rules
->buf
[rule
->rule
.filename_off
],
2286 rule
->rule
.filename_line
);
2287 if (util_run_program(event
->udev
, program
, envp
, result
, sizeof(result
), NULL
, NULL
, false) != 0) {
2288 if (cur
->key
.op
!= OP_NOMATCH
)
2293 util_remove_trailing_chars(result
, '\n');
2294 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2295 count
= udev_util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2297 info(event
->udev
, "%i character(s) replaced\n" , count
);
2299 event
->program_result
= strdup(result
);
2300 dbg(event
->udev
, "storing result '%s'\n", event
->program_result
);
2301 if (cur
->key
.op
== OP_NOMATCH
)
2306 case TK_M_IMPORT_FILE
:
2308 char import
[UTIL_PATH_SIZE
];
2310 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2311 if (import_file_into_properties(event
->dev
, import
) != 0)
2312 if (cur
->key
.op
!= OP_NOMATCH
)
2316 case TK_M_IMPORT_PROG
:
2318 char import
[UTIL_PATH_SIZE
];
2320 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2321 info(event
->udev
, "IMPORT '%s' %s:%u\n",
2323 &rules
->buf
[rule
->rule
.filename_off
],
2324 rule
->rule
.filename_line
);
2325 if (import_program_into_properties(event
->dev
, import
) != 0)
2326 if (cur
->key
.op
!= OP_NOMATCH
)
2330 case TK_M_IMPORT_DB
:
2332 const char *key
= &rules
->buf
[cur
->key
.value_off
];
2335 value
= udev_device_get_property_value(event
->dev_db
, key
);
2336 if (value
!= NULL
) {
2337 struct udev_list_entry
*entry
;
2339 entry
= udev_device_add_property(event
->dev
, key
, value
);
2340 udev_list_entry_set_flags(entry
, 1);
2342 if (cur
->key
.op
!= OP_NOMATCH
)
2347 case TK_M_IMPORT_CMDLINE
:
2350 bool imported
= false;
2352 f
= fopen("/proc/cmdline", "r");
2356 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2357 const char *key
= &rules
->buf
[cur
->key
.value_off
];
2360 pos
= strstr(cmdline
, key
);
2362 struct udev_list_entry
*entry
;
2365 if (pos
[0] == '\0' || isspace(pos
[0])) {
2366 /* we import simple flags as 'FLAG=1' */
2367 entry
= udev_device_add_property(event
->dev
, key
, "1");
2368 udev_list_entry_set_flags(entry
, 1);
2370 } else if (pos
[0] == '=') {
2375 while (pos
[0] != '\0' && !isspace(pos
[0]))
2378 entry
= udev_device_add_property(event
->dev
, key
, value
);
2379 udev_list_entry_set_flags(entry
, 1);
2386 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2390 case TK_M_IMPORT_PARENT
:
2392 char import
[UTIL_PATH_SIZE
];
2394 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2395 if (import_parent_into_properties(event
->dev
, import
) != 0)
2396 if (cur
->key
.op
!= OP_NOMATCH
)
2401 if (match_key(rules
, cur
, event
->program_result
) != 0)
2404 case TK_A_STRING_ESCAPE_NONE
:
2407 case TK_A_STRING_ESCAPE_REPLACE
:
2408 esc
= ESCAPE_REPLACE
;
2410 case TK_A_INOTIFY_WATCH
:
2411 if (event
->inotify_watch_final
)
2413 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2414 event
->inotify_watch_final
= true;
2415 event
->inotify_watch
= cur
->key
.watch
;
2417 case TK_A_DEVLINK_PRIO
:
2418 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2422 char owner
[UTIL_NAME_SIZE
];
2424 if (event
->owner_final
)
2426 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2427 event
->owner_final
= true;
2428 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], owner
, sizeof(owner
));
2429 event
->uid
= util_lookup_user(event
->udev
, owner
);
2430 info(event
->udev
, "OWNER %u %s:%u\n",
2432 &rules
->buf
[rule
->rule
.filename_off
],
2433 rule
->rule
.filename_line
);
2438 char group
[UTIL_NAME_SIZE
];
2440 if (event
->group_final
)
2442 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2443 event
->group_final
= true;
2444 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], group
, sizeof(group
));
2445 event
->gid
= util_lookup_group(event
->udev
, group
);
2446 info(event
->udev
, "GROUP %u %s:%u\n",
2448 &rules
->buf
[rule
->rule
.filename_off
],
2449 rule
->rule
.filename_line
);
2454 char mode
[UTIL_NAME_SIZE
];
2457 if (event
->mode_final
)
2459 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2460 event
->mode_final
= true;
2461 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], mode
, sizeof(mode
));
2462 event
->mode
= strtol(mode
, &endptr
, 8);
2463 if (endptr
[0] != '\0') {
2464 err(event
->udev
, "invalide mode '%s' set default mode 0600\n", mode
);
2467 info(event
->udev
, "MODE %#o %s:%u\n",
2469 &rules
->buf
[rule
->rule
.filename_off
],
2470 rule
->rule
.filename_line
);
2474 if (event
->owner_final
)
2476 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2477 event
->owner_final
= true;
2478 event
->uid
= cur
->key
.uid
;
2479 info(event
->udev
, "OWNER %u %s:%u\n",
2481 &rules
->buf
[rule
->rule
.filename_off
],
2482 rule
->rule
.filename_line
);
2485 if (event
->group_final
)
2487 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2488 event
->group_final
= true;
2489 event
->gid
= cur
->key
.gid
;
2490 info(event
->udev
, "GROUP %u %s:%u\n",
2492 &rules
->buf
[rule
->rule
.filename_off
],
2493 rule
->rule
.filename_line
);
2496 if (event
->mode_final
)
2498 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2499 event
->mode_final
= true;
2500 event
->mode
= cur
->key
.mode
;
2501 info(event
->udev
, "MODE %#o %s:%u\n",
2503 &rules
->buf
[rule
->rule
.filename_off
],
2504 rule
->rule
.filename_line
);
2506 case TK_A_STATIC_NODE
:
2510 const char *name
= &rules
->buf
[cur
->key
.attr_off
];
2511 char *value
= &rules
->buf
[cur
->key
.value_off
];
2513 if (value
[0] != '\0') {
2514 char temp_value
[UTIL_NAME_SIZE
];
2515 struct udev_list_entry
*entry
;
2517 udev_event_apply_format(event
, value
, temp_value
, sizeof(temp_value
));
2518 entry
= udev_device_add_property(event
->dev
, name
, temp_value
);
2519 /* store in db, skip private keys */
2521 udev_list_entry_set_flags(entry
, 1);
2523 udev_device_add_property(event
->dev
, name
, NULL
);
2528 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2529 udev_device_cleanup_tags_list(event
->dev
);
2530 udev_device_add_tag(event
->dev
, &rules
->buf
[cur
->key
.value_off
]);
2534 const char *name
= &rules
->buf
[cur
->key
.value_off
];
2535 char name_str
[UTIL_PATH_SIZE
];
2538 if (event
->name_final
)
2540 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2541 event
->name_final
= true;
2542 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2543 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2544 count
= udev_util_replace_chars(name_str
, "/");
2546 info(event
->udev
, "%i character(s) replaced\n", count
);
2549 event
->name
= strdup(name_str
);
2550 info(event
->udev
, "NAME '%s' %s:%u\n",
2552 &rules
->buf
[rule
->rule
.filename_off
],
2553 rule
->rule
.filename_line
);
2558 char temp
[UTIL_PATH_SIZE
];
2559 char filename
[UTIL_PATH_SIZE
];
2563 if (event
->devlink_final
)
2565 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2567 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2568 event
->devlink_final
= true;
2569 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2570 udev_device_cleanup_devlinks_list(event
->dev
);
2572 /* allow multiple symlinks separated by spaces */
2573 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], temp
, sizeof(temp
));
2574 if (esc
== ESCAPE_UNSET
)
2575 count
= udev_util_replace_chars(temp
, "/ ");
2576 else if (esc
== ESCAPE_REPLACE
)
2577 count
= udev_util_replace_chars(temp
, "/");
2579 info(event
->udev
, "%i character(s) replaced\n" , count
);
2580 dbg(event
->udev
, "rule applied, added symlink(s) '%s'\n", temp
);
2582 while (isspace(pos
[0]))
2584 next
= strchr(pos
, ' ');
2585 while (next
!= NULL
) {
2587 info(event
->udev
, "LINK '%s' %s:%u\n", pos
,
2588 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2589 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2590 udev_device_add_devlink(event
->dev
, filename
, cur
->key
.devlink_unique
);
2591 while (isspace(next
[1]))
2594 next
= strchr(pos
, ' ');
2596 if (pos
[0] != '\0') {
2597 info(event
->udev
, "LINK '%s' %s:%u\n", pos
,
2598 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2599 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2600 udev_device_add_devlink(event
->dev
, filename
, cur
->key
.devlink_unique
);
2604 case TK_A_EVENT_TIMEOUT
:
2605 udev_device_set_event_timeout(event
->dev
, cur
->key
.event_timeout
);
2609 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2610 char attr
[UTIL_PATH_SIZE
];
2611 char value
[UTIL_NAME_SIZE
];
2614 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2615 util_strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2616 attr_subst_subdir(attr
, sizeof(attr
));
2618 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], value
, sizeof(value
));
2619 info(event
->udev
, "ATTR '%s' writing '%s' %s:%u\n", attr
, value
,
2620 &rules
->buf
[rule
->rule
.filename_off
],
2621 rule
->rule
.filename_line
);
2622 f
= fopen(attr
, "w");
2624 if (fprintf(f
, "%s", value
) <= 0)
2625 err(event
->udev
, "error writing ATTR{%s}: %m\n", attr
);
2628 err(event
->udev
, "error opening ATTR{%s} for writing: %m\n", attr
);
2634 struct udev_list_entry
*list_entry
;
2636 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2637 udev_list_cleanup_entries(event
->udev
, &event
->run_list
);
2638 info(event
->udev
, "RUN '%s' %s:%u\n",
2639 &rules
->buf
[cur
->key
.value_off
],
2640 &rules
->buf
[rule
->rule
.filename_off
],
2641 rule
->rule
.filename_line
);
2642 list_entry
= udev_list_entry_add(event
->udev
, &event
->run_list
,
2643 &rules
->buf
[cur
->key
.value_off
], NULL
, 1, 0);
2644 if (cur
->key
.fail_on_error
)
2645 udev_list_entry_set_flags(list_entry
, 1);
2649 if (cur
->key
.rule_goto
== 0)
2651 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2656 case TK_M_PARENTS_MIN
:
2657 case TK_M_PARENTS_MAX
:
2660 err(rules
->udev
, "wrong type %u\n", cur
->type
);
2667 /* fast-forward to next rule */
2668 cur
= rule
+ rule
->rule
.token_count
;
2669 dbg(rules
->udev
, "forward to rule: %u\n",
2670 (unsigned int) (cur
- rules
->tokens
));
2674 void udev_rules_apply_static_dev_perms(struct udev_rules
*rules
)
2682 if (rules
->tokens
== NULL
)
2685 cur
= &rules
->tokens
[0];
2688 switch (cur
->type
) {
2693 /* skip rules without a static_node tag */
2694 if (!rule
->rule
.has_static_node
)
2708 mode
= cur
->key
.mode
;
2710 case TK_A_STATIC_NODE
: {
2711 char filename
[UTIL_PATH_SIZE
];
2713 /* we assure, that the permissions tokens are sorted before the static token */
2714 if (mode
== 0 && uid
== 0 && gid
== 0)
2716 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(rules
->udev
), "/",
2717 &rules
->buf
[cur
->key
.value_off
], NULL
);
2718 if (stat(filename
, &stats
) != 0)
2720 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2722 if (mode
!= 0 && mode
!= (stats
.st_mode
& 0777)) {
2723 chmod(filename
, mode
);
2724 info(rules
->udev
, "chmod '%s' %#o\n", filename
, mode
);
2726 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2727 chown(filename
, uid
, gid
);
2728 info(rules
->udev
, "chown '%s' %u %u\n", filename
, uid
, gid
);
2730 utimensat(AT_FDCWD
, filename
, NULL
, 0);
2740 /* fast-forward to next rule */
2741 cur
= rule
+ rule
->rule
.token_count
;