2 * Copyright (C) 2008 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/>.
33 #define PREALLOC_TOKEN 2048
34 #define PREALLOC_STRBUF 32 * 1024
35 #define PREALLOC_TRIE 256
38 unsigned int name_off
;
46 /* this node's first child */
47 unsigned int child_idx
;
48 /* the next child of our parent node's child list */
49 unsigned int next_child_idx
;
50 /* this node's last child (shortcut for append) */
51 unsigned int last_child_idx
;
52 unsigned int value_off
;
53 unsigned short value_len
;
61 /* every key in the rules file becomes a token */
63 unsigned int token_cur
;
64 unsigned int token_max
;
66 /* all key strings are copied to a single string buffer */
70 unsigned int buf_count
;
72 /* during rule parsing, strings are indexed to find duplicates */
73 struct trie_node
*trie_nodes
;
74 unsigned int trie_nodes_cur
;
75 unsigned int trie_nodes_max
;
77 /* during rule parsing, uid/gid lookup results are cached */
79 unsigned int uids_cur
;
80 unsigned int uids_max
;
82 unsigned int gids_cur
;
83 unsigned int gids_max
;
86 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
99 enum string_glob_type
{
101 GL_PLAIN
, /* no special chars */
102 GL_GLOB
, /* shell globs ?,*,[] */
103 GL_SPLIT
, /* multi-value A|B */
104 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
105 GL_SOMETHING
, /* commonly used "?*" */
109 /* tokens of a rule are sorted/handled in this order */
114 TK_M_ACTION
, /* val */
115 TK_M_DEVPATH
, /* val */
116 TK_M_KERNEL
, /* val */
117 TK_M_DEVLINK
, /* val */
119 TK_M_ENV
, /* val, attr */
120 TK_M_SUBSYSTEM
, /* val */
121 TK_M_DRIVER
, /* val */
122 TK_M_WAITFOR
, /* val */
123 TK_M_ATTR
, /* val, attr */
126 TK_M_KERNELS
, /* val */
127 TK_M_SUBSYSTEMS
, /* val */
128 TK_M_DRIVERS
, /* val */
129 TK_M_ATTRS
, /* val, attr */
132 TK_M_TEST
, /* val, mode_t */
133 TK_M_PROGRAM
, /* val */
134 TK_M_IMPORT_FILE
, /* val */
135 TK_M_IMPORT_PROG
, /* val */
136 TK_M_IMPORT_PARENT
, /* val */
137 TK_M_RESULT
, /* val */
141 TK_A_STRING_ESCAPE_NONE
,
142 TK_A_STRING_ESCAPE_REPLACE
,
143 TK_A_INOTIFY_WATCH
, /* int */
144 TK_A_NUM_FAKE_PART
, /* int */
145 TK_A_DEVLINK_PRIO
, /* int */
146 TK_A_OWNER
, /* val */
147 TK_A_GROUP
, /* val */
149 TK_A_OWNER_ID
, /* uid_t */
150 TK_A_GROUP_ID
, /* gid_t */
151 TK_A_MODE_ID
, /* mode_t */
152 TK_A_ENV
, /* val, attr */
154 TK_A_DEVLINK
, /* val */
155 TK_A_EVENT_TIMEOUT
, /* int */
157 TK_A_ATTR
, /* val, attr */
158 TK_A_RUN
, /* val, bool */
159 TK_A_GOTO
, /* size_t */
165 /* we try to pack stuff in a way that we take only 12 bytes per token */
168 unsigned char type
; /* same as in rule and key */
172 unsigned short token_count
;
173 unsigned int label_off
;
174 unsigned short filename_off
;
175 unsigned short filename_line
;
182 unsigned int value_off
;
184 unsigned int attr_off
;
186 unsigned int rule_goto
;
201 struct udev_rules
*rules
;
203 struct token token
[MAX_TK
];
204 unsigned int token_cur
;
208 static const char *operation_str(enum operation_type type
)
210 static const char *operation_strs
[] = {
211 [OP_UNSET
] = "UNSET",
212 [OP_MATCH
] = "match",
213 [OP_NOMATCH
] = "nomatch",
214 [OP_MATCH_MAX
] = "MATCH_MAX",
217 [OP_ASSIGN
] = "assign",
218 [OP_ASSIGN_FINAL
] = "assign-final",
221 return operation_strs
[type
];
224 static const char *string_glob_str(enum string_glob_type type
)
226 static const char *string_glob_strs
[] = {
227 [GL_UNSET
] = "UNSET",
228 [GL_PLAIN
] = "plain",
230 [GL_SPLIT
] = "split",
231 [GL_SPLIT_GLOB
] = "split-glob",
232 [GL_SOMETHING
] = "split-glob",
233 [GL_FORMAT
] = "format",
236 return string_glob_strs
[type
];
239 static const char *token_str(enum token_type type
)
241 static const char *token_strs
[] = {
242 [TK_UNSET
] = "UNSET",
245 [TK_M_ACTION
] = "M ACTION",
246 [TK_M_DEVPATH
] = "M DEVPATH",
247 [TK_M_KERNEL
] = "M KERNEL",
248 [TK_M_DEVLINK
] = "M DEVLINK",
249 [TK_M_NAME
] = "M NAME",
250 [TK_M_ENV
] = "M ENV",
251 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
252 [TK_M_DRIVER
] = "M DRIVER",
253 [TK_M_WAITFOR
] = "M WAITFOR",
254 [TK_M_ATTR
] = "M ATTR",
256 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
257 [TK_M_KERNELS
] = "M KERNELS",
258 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
259 [TK_M_DRIVERS
] = "M DRIVERS",
260 [TK_M_ATTRS
] = "M ATTRS",
261 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
263 [TK_M_TEST
] = "M TEST",
264 [TK_M_PROGRAM
] = "M PROGRAM",
265 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
266 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
267 [TK_M_IMPORT_PARENT
] = "M MPORT_PARENT",
268 [TK_M_RESULT
] = "M RESULT",
269 [TK_M_MAX
] = "M MAX",
271 [TK_A_IGNORE_DEVICE
] = "A IGNORE_DEVICE",
272 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
273 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
274 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
275 [TK_A_NUM_FAKE_PART
] = "A NUM_FAKE_PART",
276 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
277 [TK_A_OWNER
] = "A OWNER",
278 [TK_A_GROUP
] = "A GROUP",
279 [TK_A_MODE
] = "A MODE",
280 [TK_A_OWNER_ID
] = "A OWNER_ID",
281 [TK_A_GROUP_ID
] = "A GROUP_ID",
282 [TK_A_MODE_ID
] = "A MODE_ID",
283 [TK_A_ENV
] = "A ENV",
284 [TK_A_NAME
] = "A NAME",
285 [TK_A_DEVLINK
] = "A DEVLINK",
286 [TK_A_EVENT_TIMEOUT
] = "A EVENT_TIMEOUT",
287 [TK_A_IGNORE_REMOVE
] = "A IGNORE_REMOVE",
288 [TK_A_ATTR
] = "A ATTR",
289 [TK_A_RUN
] = "A RUN",
290 [TK_A_GOTO
] = "A GOTO",
291 [TK_A_LAST_RULE
] = "A LAST_RULE",
296 return token_strs
[type
];
299 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
301 enum token_type type
= token
->type
;
302 enum operation_type op
= token
->key
.op
;
303 enum string_glob_type glob
= token
->key
.glob
;
304 const char *value
= &rules
->buf
[token
->key
.value_off
];
305 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
310 const char *tks_ptr
= (char *)rules
->tokens
;
311 const char *tk_ptr
= (char *)token
;
312 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
314 dbg(rules
->udev
, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n",
315 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
316 idx
, token
->rule
.token_count
,
317 &rules
->buf
[token
->rule
.label_off
],
330 case TK_M_SUBSYSTEMS
:
333 case TK_M_IMPORT_FILE
:
334 case TK_M_IMPORT_PROG
:
335 case TK_M_IMPORT_PARENT
:
343 dbg(rules
->udev
, "%s %s '%s'(%s)\n",
344 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
351 dbg(rules
->udev
, "%s %s '%s' '%s'(%s)\n",
352 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
354 case TK_A_IGNORE_DEVICE
:
355 case TK_A_STRING_ESCAPE_NONE
:
356 case TK_A_STRING_ESCAPE_REPLACE
:
358 case TK_A_IGNORE_REMOVE
:
359 dbg(rules
->udev
, "%s\n", token_str(type
));
362 dbg(rules
->udev
, "%s %s '%s'(%s) %#o\n",
363 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
365 case TK_A_INOTIFY_WATCH
:
366 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.watch
);
368 case TK_A_NUM_FAKE_PART
:
369 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.num_fake_part
);
371 case TK_A_DEVLINK_PRIO
:
372 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.devlink_prio
);
375 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.uid
);
378 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.gid
);
381 dbg(rules
->udev
, "%s %s %#o\n", token_str(type
), operation_str(op
), token
->key
.mode
);
383 case TK_A_EVENT_TIMEOUT
:
384 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.event_timeout
);
387 dbg(rules
->udev
, "%s '%s' %u\n", token_str(type
), value
, token
->key
.rule_goto
);
390 dbg(rules
->udev
, "* %s\n", token_str(type
));
392 case TK_M_PARENTS_MIN
:
393 case TK_M_PARENTS_MAX
:
396 dbg(rules
->udev
, "unknown type %u\n", type
);
401 static void dump_rules(struct udev_rules
*rules
)
405 dbg(rules
->udev
, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
407 rules
->token_cur
* sizeof(struct token
),
410 for(i
= 0; i
< rules
->token_cur
; i
++)
411 dump_token(rules
, &rules
->tokens
[i
]);
414 static inline const char *operation_str(enum operation_type type
) { return NULL
; }
415 static inline const char *token_str(enum token_type type
) { return NULL
; }
416 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
417 static inline void dump_rules(struct udev_rules
*rules
) {}
420 static int add_new_string(struct udev_rules
*rules
, const char *str
, size_t bytes
)
424 /* grow buffer if needed */
425 if (rules
->buf_cur
+ bytes
+1 >= rules
->buf_max
) {
429 /* double the buffer size */
430 add
= rules
->buf_max
;
434 buf
= realloc(rules
->buf
, rules
->buf_max
+ add
);
437 dbg(rules
->udev
, "extend buffer from %zu to %zu\n", rules
->buf_max
, rules
->buf_max
+ add
);
439 rules
->buf_max
+= add
;
441 off
= rules
->buf_cur
;
442 memcpy(&rules
->buf
[rules
->buf_cur
], str
, bytes
);
443 rules
->buf_cur
+= bytes
;
448 static int add_string(struct udev_rules
*rules
, const char *str
)
450 unsigned int node_idx
;
451 struct trie_node
*new_node
;
452 unsigned int new_node_idx
;
457 struct trie_node
*parent
;
459 /* walk trie, start from last character of str to find matching tails */
463 for (depth
= 0; depth
<= len
; depth
++) {
464 struct trie_node
*node
;
465 unsigned int child_idx
;
467 node
= &rules
->trie_nodes
[node_idx
];
468 off
= node
->value_off
+ node
->value_len
- len
;
470 /* match against current node */
471 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(&rules
->buf
[off
], str
, len
) == 0))
474 /* lookup child node */
475 key
= str
[len
- 1 - depth
];
476 child_idx
= node
->child_idx
;
477 while (child_idx
> 0) {
478 struct trie_node
*child
;
480 child
= &rules
->trie_nodes
[child_idx
];
481 if (child
->key
== key
)
483 child_idx
= child
->next_child_idx
;
487 node_idx
= child_idx
;
490 /* string not found, add it */
491 off
= add_new_string(rules
, str
, len
+ 1);
493 /* grow trie nodes if needed */
494 if (rules
->trie_nodes_cur
>= rules
->trie_nodes_max
) {
495 struct trie_node
*nodes
;
498 /* double the buffer size */
499 add
= rules
->trie_nodes_max
;
503 nodes
= realloc(rules
->trie_nodes
, (rules
->trie_nodes_max
+ add
) * sizeof(struct trie_node
));
506 dbg(rules
->udev
, "extend trie nodes from %u to %u\n",
507 rules
->trie_nodes_max
, rules
->trie_nodes_max
+ add
);
508 rules
->trie_nodes
= nodes
;
509 rules
->trie_nodes_max
+= add
;
513 new_node_idx
= rules
->trie_nodes_cur
;
514 rules
->trie_nodes_cur
++;
515 new_node
= &rules
->trie_nodes
[new_node_idx
];
516 memset(new_node
, 0x00, sizeof(struct trie_node
));
517 new_node
->value_off
= off
;
518 new_node
->value_len
= len
;
521 /* join the parent's child list */
522 parent
= &rules
->trie_nodes
[node_idx
];
523 if (parent
->child_idx
== 0) {
524 parent
->child_idx
= new_node_idx
;
526 struct trie_node
*last_child
;
528 last_child
= &rules
->trie_nodes
[parent
->last_child_idx
];
529 last_child
->next_child_idx
= new_node_idx
;
531 parent
->last_child_idx
= new_node_idx
;
535 static int add_token(struct udev_rules
*rules
, struct token
*token
)
537 /* grow buffer if needed */
538 if (rules
->token_cur
+1 >= rules
->token_max
) {
539 struct token
*tokens
;
542 /* double the buffer size */
543 add
= rules
->token_max
;
547 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
550 dbg(rules
->udev
, "extend tokens from %u to %u\n", rules
->token_max
, rules
->token_max
+ add
);
551 rules
->tokens
= tokens
;
552 rules
->token_max
+= add
;
554 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
559 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
565 /* lookup, if we know it already */
566 for (i
= 0; i
< rules
->uids_cur
; i
++) {
567 off
= rules
->uids
[i
].name_off
;
568 if (strcmp(&rules
->buf
[off
], owner
) == 0) {
569 uid
= rules
->uids
[i
].uid
;
570 dbg(rules
->udev
, "return existing %u for '%s'\n", uid
, owner
);
574 uid
= util_lookup_user(rules
->udev
, owner
);
576 /* grow buffer if needed */
577 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
578 struct uid_gid
*uids
;
581 /* double the buffer size */
582 add
= rules
->uids_max
;
586 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
589 dbg(rules
->udev
, "extend uids from %u to %u\n", rules
->uids_max
, rules
->uids_max
+ add
);
591 rules
->uids_max
+= add
;
593 rules
->uids
[rules
->uids_cur
].uid
= uid
;
594 off
= add_string(rules
, owner
);
597 rules
->uids
[rules
->uids_cur
].name_off
= off
;
602 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
608 /* lookup, if we know it already */
609 for (i
= 0; i
< rules
->gids_cur
; i
++) {
610 off
= rules
->gids
[i
].name_off
;
611 if (strcmp(&rules
->buf
[off
], group
) == 0) {
612 gid
= rules
->gids
[i
].gid
;
613 dbg(rules
->udev
, "return existing %u for '%s'\n", gid
, group
);
617 gid
= util_lookup_group(rules
->udev
, group
);
619 /* grow buffer if needed */
620 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
621 struct uid_gid
*gids
;
624 /* double the buffer size */
625 add
= rules
->gids_max
;
629 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
632 dbg(rules
->udev
, "extend gids from %u to %u\n", rules
->gids_max
, rules
->gids_max
+ add
);
634 rules
->gids_max
+= add
;
636 rules
->gids
[rules
->gids_cur
].gid
= gid
;
637 off
= add_string(rules
, group
);
640 rules
->gids
[rules
->gids_cur
].name_off
= off
;
645 static int import_property_from_string(struct udev_device
*dev
, char *line
)
647 struct udev
*udev
= udev_device_get_udev(dev
);
654 while (isspace(key
[0]))
657 /* comment or empty line */
658 if (key
[0] == '#' || key
[0] == '\0')
661 /* split key/value */
662 val
= strchr(key
, '=');
669 while (isspace(val
[0]))
676 while (isspace(key
[len
-1]))
680 /* terminate value */
684 while (isspace(val
[len
-1]))
692 if (val
[0] == '"' || val
[0] == '\'') {
693 if (val
[len
-1] != val
[0]) {
694 info(udev
, "inconsistent quoting: '%s', skip\n", line
);
701 dbg(udev
, "adding '%s'='%s'\n", key
, val
);
703 /* handle device, renamed by external tool, returning new path */
704 if (strcmp(key
, "DEVPATH") == 0) {
705 char syspath
[UTIL_PATH_SIZE
];
707 info(udev
, "updating devpath from '%s' to '%s'\n",
708 udev_device_get_devpath(dev
), val
);
709 util_strlcpy(syspath
, udev_get_sys_path(udev
), sizeof(syspath
));
710 util_strlcat(syspath
, val
, sizeof(syspath
));
711 udev_device_set_syspath(dev
, syspath
);
713 struct udev_list_entry
*entry
;
715 entry
= udev_device_add_property(dev
, key
, val
);
717 udev_list_entry_set_flag(entry
, 1);
722 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
725 char line
[UTIL_LINE_SIZE
];
727 f
= fopen(filename
, "r");
730 while (fgets(line
, sizeof(line
), f
) != NULL
)
731 import_property_from_string(dev
, line
);
736 static int import_program_into_properties(struct udev_device
*dev
, const char *program
)
738 struct udev
*udev
= udev_device_get_udev(dev
);
744 envp
= udev_device_get_properties_envp(dev
);
745 if (util_run_program(udev
, program
, envp
, result
, sizeof(result
), &reslen
) != 0)
749 while (line
!= NULL
) {
752 pos
= strchr(line
, '\n');
757 import_property_from_string(dev
, line
);
763 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
765 struct udev
*udev
= udev_device_get_udev(dev
);
766 struct udev_device
*dev_parent
;
767 struct udev_list_entry
*list_entry
;
769 dev_parent
= udev_device_get_parent(dev
);
770 if (dev_parent
== NULL
)
773 dbg(udev
, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent
));
774 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
775 const char *key
= udev_list_entry_get_name(list_entry
);
776 const char *val
= udev_list_entry_get_value(list_entry
);
778 if (fnmatch(filter
, key
, 0) == 0) {
779 struct udev_list_entry
*entry
;
781 dbg(udev
, "import key '%s=%s'\n", key
, val
);
782 entry
= udev_device_add_property(dev
, key
, val
);
784 udev_list_entry_set_flag(entry
, 1);
790 #define WAIT_LOOP_PER_SECOND 50
791 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
793 struct udev
*udev
= udev_device_get_udev(dev
);
794 char filepath
[UTIL_PATH_SIZE
];
795 char devicepath
[UTIL_PATH_SIZE
];
797 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
799 /* a relative path is a device attribute */
800 devicepath
[0] = '\0';
801 if (file
[0] != '/') {
802 util_strlcpy(devicepath
, udev_get_sys_path(udev
), sizeof(devicepath
));
803 util_strlcat(devicepath
, udev_device_get_devpath(dev
), sizeof(devicepath
));
805 util_strlcpy(filepath
, devicepath
, sizeof(filepath
));
806 util_strlcat(filepath
, "/", sizeof(filepath
));
807 util_strlcat(filepath
, file
, sizeof(filepath
));
811 dbg(udev
, "will wait %i sec for '%s'\n", timeout
, file
);
814 if (stat(file
, &stats
) == 0) {
815 info(udev
, "file '%s' appeared after %i loops\n", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
818 /* make sure, the device did not disappear in the meantime */
819 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
820 info(udev
, "device disappeared while waiting for '%s'\n", file
);
823 info(udev
, "wait for '%s' for %i mseconds\n", file
, 1000 / WAIT_LOOP_PER_SECOND
);
824 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
826 info(udev
, "waiting for '%s' failed\n", file
);
830 static int attr_subst_subdir(char *attr
, size_t len
)
835 pos
= strstr(attr
, "/*/");
837 char str
[UTIL_PATH_SIZE
];
841 util_strlcpy(str
, &pos
[2], sizeof(str
));
846 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
849 if (dent
->d_name
[0] == '.')
851 util_strlcat(attr
, dent
->d_name
, len
);
852 util_strlcat(attr
, str
, len
);
853 if (stat(attr
, &stats
) == 0) {
862 util_strlcat(attr
, str
, len
);
868 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
874 if (linepos
== NULL
&& linepos
[0] == '\0')
877 /* skip whitespace */
878 while (isspace(linepos
[0]) || linepos
[0] == ',')
882 if (linepos
[0] == '\0')
888 if (linepos
[0] == '\0')
890 if (isspace(linepos
[0]))
892 if (linepos
[0] == '=')
894 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
895 if (linepos
[1] == '=')
899 /* remember end of key */
902 /* skip whitespace after key */
903 while (isspace(linepos
[0]))
905 if (linepos
[0] == '\0')
908 /* get operation type */
909 if (linepos
[0] == '=' && linepos
[1] == '=') {
912 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
915 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
918 } else if (linepos
[0] == '=') {
921 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
922 *op
= OP_ASSIGN_FINAL
;
930 /* skip whitespace after operator */
931 while (isspace(linepos
[0]))
933 if (linepos
[0] == '\0')
937 if (linepos
[0] == '"')
944 temp
= strchr(linepos
, '"');
949 dbg(udev
, "%s '%s'-'%s'\n", operation_str(*op
), *key
, *value
);
951 /* move line to next key */
956 /* extract possible KEY{attr} */
957 static char *get_key_attribute(struct udev
*udev
, char *str
)
962 attr
= strchr(str
, '{');
965 pos
= strchr(attr
, '}');
967 err(udev
, "missing closing brace for format\n");
971 dbg(udev
, "attribute='%s'\n", attr
);
977 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
978 enum operation_type op
,
979 const char *value
, const void *data
)
981 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
982 const char *attr
= data
;
983 enum string_glob_type glob
;
985 memset(token
, 0x00, sizeof(struct token
));
997 case TK_M_SUBSYSTEMS
:
1000 case TK_M_IMPORT_FILE
:
1001 case TK_M_IMPORT_PROG
:
1002 case TK_M_IMPORT_PARENT
:
1010 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1017 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1018 token
->key
.attr_off
= add_string(rule_tmp
->rules
, attr
);
1021 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1023 token
->key
.mode
= *(mode_t
*)data
;
1025 case TK_A_IGNORE_DEVICE
:
1026 case TK_A_STRING_ESCAPE_NONE
:
1027 case TK_A_STRING_ESCAPE_REPLACE
:
1028 case TK_A_IGNORE_REMOVE
:
1029 case TK_A_LAST_RULE
:
1032 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1033 token
->key
.ignore_error
= *(int *)data
;
1035 case TK_A_INOTIFY_WATCH
:
1036 case TK_A_NUM_FAKE_PART
:
1037 case TK_A_DEVLINK_PRIO
:
1038 token
->key
.devlink_prio
= *(int *)data
;
1041 token
->key
.uid
= *(uid_t
*)data
;
1044 token
->key
.gid
= *(gid_t
*)data
;
1047 token
->key
.mode
= *(mode_t
*)data
;
1049 case TK_A_EVENT_TIMEOUT
:
1050 token
->key
.event_timeout
= *(int *)data
;
1053 case TK_M_PARENTS_MIN
:
1054 case TK_M_PARENTS_MAX
:
1058 err(rule_tmp
->rules
->udev
, "wrong type %u\n", type
);
1063 if (value
!= NULL
) {
1064 if (type
< TK_M_MAX
) {
1065 /* check if we need to split or call fnmatch() while matching rules */
1069 has_split
= (strchr(value
, '|') != NULL
);
1070 has_glob
= (strchr(value
, '*') != NULL
|| strchr(value
, '?') != NULL
||
1071 strchr(value
, '[') != NULL
|| strchr(value
, ']') != NULL
);
1072 if (has_split
&& has_glob
) {
1073 glob
= GL_SPLIT_GLOB
;
1074 } else if (has_split
) {
1076 } else if (has_glob
) {
1077 if (strcmp(value
, "?*") == 0)
1078 glob
= GL_SOMETHING
;
1083 /* check if we need to substitute format strings for matching rules */
1084 if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
1089 token
->key
.type
= type
;
1091 token
->key
.glob
= glob
;
1092 rule_tmp
->token_cur
++;
1093 if (rule_tmp
->token_cur
>= ARRAY_SIZE(rule_tmp
->token
)) {
1094 err(rule_tmp
->rules
->udev
, "temporary rule array too small\n");
1100 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1103 unsigned int start
= 0;
1104 unsigned int end
= rule_tmp
->token_cur
;
1106 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1107 enum token_type next_val
= TK_UNSET
;
1108 unsigned int next_idx
;
1111 /* find smallest value */
1112 for (j
= start
; j
< end
; j
++) {
1113 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1115 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1116 next_val
= rule_tmp
->token
[j
].type
;
1121 /* add token and mark done */
1122 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1124 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1127 if (next_idx
== start
)
1129 if (next_idx
+1 == end
)
1135 static int add_rule(struct udev_rules
*rules
, char *line
,
1136 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1140 struct rule_tmp rule_tmp
;
1142 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1143 rule_tmp
.rules
= rules
;
1144 rule_tmp
.rule
.type
= TK_RULE
;
1145 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1146 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1152 enum operation_type op
;
1154 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0)
1157 if (strcasecmp(key
, "ACTION") == 0) {
1158 if (op
> OP_MATCH_MAX
) {
1159 err(rules
->udev
, "invalid ACTION operation\n");
1162 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1166 if (strcasecmp(key
, "DEVPATH") == 0) {
1167 if (op
> OP_MATCH_MAX
) {
1168 err(rules
->udev
, "invalid DEVPATH operation\n");
1171 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1175 if (strcasecmp(key
, "KERNEL") == 0) {
1176 if (op
> OP_MATCH_MAX
) {
1177 err(rules
->udev
, "invalid KERNEL operation\n");
1180 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1184 if (strcasecmp(key
, "SUBSYSTEM") == 0) {
1185 if (op
> OP_MATCH_MAX
) {
1186 err(rules
->udev
, "invalid SUBSYSTEM operation\n");
1189 /* bus, class, subsystem events should all be the same */
1190 if (strcmp(value
, "subsystem") == 0 ||
1191 strcmp(value
, "bus") == 0 ||
1192 strcmp(value
, "class") == 0) {
1193 if (strcmp(value
, "bus") == 0 || strcmp(value
, "class") == 0)
1194 err(rules
->udev
, "'%s' must be specified as 'subsystem' \n"
1195 "please fix it in %s:%u", value
, filename
, lineno
);
1196 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1198 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1202 if (strcasecmp(key
, "DRIVER") == 0) {
1203 if (op
> OP_MATCH_MAX
) {
1204 err(rules
->udev
, "invalid DRIVER operation\n");
1207 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1211 if (strncasecmp(key
, "ATTR{", sizeof("ATTR{")-1) == 0) {
1212 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1214 err(rules
->udev
, "error parsing ATTR attribute\n");
1217 if (op
< OP_MATCH_MAX
) {
1218 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1220 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1225 if (strcasecmp(key
, "KERNELS") == 0 ||
1226 strcasecmp(key
, "ID") == 0) {
1227 if (op
> OP_MATCH_MAX
) {
1228 err(rules
->udev
, "invalid KERNELS operation\n");
1231 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1235 if (strcasecmp(key
, "SUBSYSTEMS") == 0 ||
1236 strcasecmp(key
, "BUS") == 0) {
1237 if (op
> OP_MATCH_MAX
) {
1238 err(rules
->udev
, "invalid SUBSYSTEMS operation\n");
1241 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1245 if (strcasecmp(key
, "DRIVERS") == 0) {
1246 if (op
> OP_MATCH_MAX
) {
1247 err(rules
->udev
, "invalid DRIVERS operation\n");
1250 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1254 if (strncasecmp(key
, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1255 strncasecmp(key
, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1256 if (op
> OP_MATCH_MAX
) {
1257 err(rules
->udev
, "invalid ATTRS operation\n");
1260 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1262 err(rules
->udev
, "error parsing ATTRS attribute\n");
1265 if (strncmp(attr
, "device/", 7) == 0)
1266 err(rules
->udev
, "the 'device' link may not be available in a future kernel, "
1267 "please fix it in %s:%u", filename
, lineno
);
1268 else if (strstr(attr
, "../") != NULL
)
1269 err(rules
->udev
, "do not reference parent sysfs directories directly, "
1270 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1271 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1275 if (strncasecmp(key
, "ENV{", sizeof("ENV{")-1) == 0) {
1276 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1278 err(rules
->udev
, "error parsing ENV attribute\n");
1281 if (op
< OP_MATCH_MAX
) {
1282 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1285 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1291 if (strcasecmp(key
, "PROGRAM") == 0) {
1292 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1296 if (strcasecmp(key
, "RESULT") == 0) {
1297 if (op
> OP_MATCH_MAX
) {
1298 err(rules
->udev
, "invalid RESULT operation\n");
1301 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1305 if (strncasecmp(key
, "IMPORT", sizeof("IMPORT")-1) == 0) {
1306 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1307 if (attr
!= NULL
&& strstr(attr
, "program")) {
1308 dbg(rules
->udev
, "IMPORT will be executed\n");
1309 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1310 } else if (attr
!= NULL
&& strstr(attr
, "file")) {
1311 dbg(rules
->udev
, "IMPORT will be included as file\n");
1312 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1313 } else if (attr
!= NULL
&& strstr(attr
, "parent")) {
1314 dbg(rules
->udev
, "IMPORT will include the parent values\n");
1315 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1317 /* figure it out if it is executable */
1318 char file
[UTIL_PATH_SIZE
];
1320 struct stat statbuf
;
1322 util_strlcpy(file
, value
, sizeof(file
));
1323 pos
= strchr(file
, ' ');
1327 /* allow programs in /lib/udev called without the path */
1328 if (strchr(file
, '/') == NULL
) {
1329 util_strlcpy(file
, UDEV_PREFIX
"/lib/udev/", sizeof(file
));
1330 util_strlcat(file
, value
, sizeof(file
));
1331 pos
= strchr(file
, ' ');
1336 dbg(rules
->udev
, "IMPORT auto mode for '%s'\n", file
);
1337 if (!lstat(file
, &statbuf
) && (statbuf
.st_mode
& S_IXUSR
)) {
1338 dbg(rules
->udev
, "IMPORT will be executed (autotype)\n");
1339 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1341 dbg(rules
->udev
, "IMPORT will be included as file (autotype)\n");
1342 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1348 if (strncasecmp(key
, "TEST", sizeof("TEST")-1) == 0) {
1351 if (op
> OP_MATCH_MAX
) {
1352 err(rules
->udev
, "invalid TEST operation\n");
1355 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1357 mode
= strtol(attr
, NULL
, 8);
1358 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1360 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1365 if (strncasecmp(key
, "RUN", sizeof("RUN")-1) == 0) {
1368 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1369 if (attr
!= NULL
&& strstr(attr
, "ignore_error"))
1371 rule_add_key(&rule_tmp
, TK_A_RUN
, op
, value
, &flag
);
1375 if (strcasecmp(key
, "WAIT_FOR") == 0 || strcasecmp(key
, "WAIT_FOR_SYSFS") == 0) {
1376 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1380 if (strcasecmp(key
, "LABEL") == 0) {
1381 rule_tmp
.rule
.rule
.label_off
= add_string(rules
, value
);
1385 if (strcasecmp(key
, "GOTO") == 0) {
1386 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1390 if (strncasecmp(key
, "NAME", sizeof("NAME")-1) == 0) {
1391 if (op
< OP_MATCH_MAX
) {
1392 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1394 if (value
[0] == '\0')
1395 info(rules
->udev
, "name empty, node creation suppressed\n");
1396 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1397 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("NAME")-1);
1399 if (strstr(attr
, "all_partitions") != NULL
) {
1400 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1402 dbg(rules
->udev
, "creation of partition nodes requested\n");
1403 rule_add_key(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1405 if (strstr(attr
, "ignore_remove") != NULL
) {
1406 dbg(rules
->udev
, "remove event should be ignored\n");
1407 rule_add_key(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1411 rule_tmp
.rule
.rule
.flags
= 1;
1415 if (strcasecmp(key
, "SYMLINK") == 0) {
1416 if (op
< OP_MATCH_MAX
)
1417 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1419 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1420 rule_tmp
.rule
.rule
.flags
= 1;
1424 if (strcasecmp(key
, "OWNER") == 0) {
1428 uid
= strtoul(value
, &endptr
, 10);
1429 if (endptr
[0] == '\0') {
1430 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1431 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1432 uid
= add_uid(rules
, value
);
1433 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1434 } else if (rules
->resolve_names
>= 0) {
1435 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1437 rule_tmp
.rule
.rule
.flags
= 1;
1441 if (strcasecmp(key
, "GROUP") == 0) {
1445 gid
= strtoul(value
, &endptr
, 10);
1446 if (endptr
[0] == '\0') {
1447 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1448 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1449 gid
= add_gid(rules
, value
);
1450 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1451 } else if (rules
->resolve_names
>= 0) {
1452 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1454 rule_tmp
.rule
.rule
.flags
= 1;
1458 if (strcasecmp(key
, "MODE") == 0) {
1462 mode
= strtol(value
, &endptr
, 8);
1463 if (endptr
[0] == '\0')
1464 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1466 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1467 rule_tmp
.rule
.rule
.flags
= 1;
1471 if (strcasecmp(key
, "OPTIONS") == 0) {
1474 if (strstr(value
, "last_rule") != NULL
) {
1475 dbg(rules
->udev
, "last rule to be applied\n");
1476 rule_add_key(&rule_tmp
, TK_A_LAST_RULE
, 0, NULL
, NULL
);
1478 if (strstr(value
, "ignore_device") != NULL
) {
1479 dbg(rules
->udev
, "device should be ignored\n");
1480 rule_add_key(&rule_tmp
, TK_A_IGNORE_DEVICE
, 0, NULL
, NULL
);
1482 if (strstr(value
, "ignore_remove") != NULL
) {
1483 dbg(rules
->udev
, "remove event should be ignored\n");
1484 rule_add_key(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1486 pos
= strstr(value
, "link_priority=");
1488 int prio
= atoi(&pos
[strlen("link_priority=")]);
1490 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, 0, NULL
, &prio
);
1491 dbg(rules
->udev
, "link priority=%i\n", prio
);
1493 pos
= strstr(value
, "event_timeout=");
1495 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1497 rule_add_key(&rule_tmp
, TK_A_EVENT_TIMEOUT
, 0, NULL
, &tout
);
1498 dbg(rules
->udev
, "event timout=%i\n", tout
);
1500 pos
= strstr(value
, "string_escape=");
1502 pos
= &pos
[strlen("string_escape=")];
1503 if (strncmp(pos
, "none", strlen("none")) == 0)
1504 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, 0, NULL
, NULL
);
1505 else if (strncmp(pos
, "replace", strlen("replace")) == 0)
1506 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, 0, NULL
, NULL
);
1508 if (strstr(value
, "all_partitions") != NULL
) {
1509 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1511 rule_add_key(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1512 dbg(rules
->udev
, "creation of partition nodes requested\n");
1514 pos
= strstr(value
, "nowatch");
1518 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, 0, NULL
, &off
);
1519 dbg(rules
->udev
, "inotify watch of device disabled\n");
1521 pos
= strstr(value
, "watch");
1525 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, 0, NULL
, &on
);
1526 dbg(rules
->udev
, "inotify watch of device requested\n");
1531 err(rules
->udev
, "unknown key '%s' in %s:%u\n", key
, filename
, lineno
);
1534 /* add rule token */
1535 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1536 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1539 /* add tokens to list, sorted by type */
1540 if (sort_token(rules
, &rule_tmp
) != 0)
1545 err(rules
->udev
, "invalid rule '%s:%u'\n", filename
, lineno
);
1549 static int parse_file(struct udev_rules
*rules
, const char *filename
, unsigned short filename_off
)
1552 unsigned int first_token
;
1553 char line
[UTIL_LINE_SIZE
];
1557 info(rules
->udev
, "reading '%s' as rules file\n", filename
);
1559 f
= fopen(filename
, "r");
1563 first_token
= rules
->token_cur
;
1565 while(fgets(line
, sizeof(line
), f
) != NULL
) {
1569 /* skip whitespace */
1572 while (isspace(key
[0]))
1583 /* continue reading if backslash+newline is found */
1584 while (line
[len
-2] == '\\') {
1585 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1591 if (len
+1 >= sizeof(line
)) {
1592 err(rules
->udev
, "line too long '%s':%u, ignored\n", filename
, line_nr
);
1595 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1599 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1600 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1601 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1602 char *label
= &rules
->buf
[rules
->tokens
[i
].key
.value_off
];
1605 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1606 if (rules
->tokens
[j
].type
!= TK_RULE
)
1608 if (rules
->tokens
[j
].rule
.label_off
== 0)
1610 if (strcmp(label
, &rules
->buf
[rules
->tokens
[j
].rule
.label_off
]) != 0)
1612 rules
->tokens
[i
].key
.rule_goto
= j
;
1615 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1616 err(rules
->udev
, "GOTO '%s' has no matching label in: '%s'\n", label
, filename
);
1622 static int add_matching_files(struct udev
*udev
, struct udev_list_node
*file_list
, const char *dirname
, const char *suffix
)
1625 char filename
[UTIL_PATH_SIZE
];
1627 dbg(udev
, "open directory '%s'\n", dirname
);
1628 dir
= opendir(dirname
);
1630 info(udev
, "unable to open '%s': %m\n", dirname
);
1635 struct dirent
*dent
;
1637 dent
= readdir(dir
);
1638 if (dent
== NULL
|| dent
->d_name
[0] == '\0')
1641 if (dent
->d_name
[0] == '.')
1644 /* look for file matching with specified suffix */
1645 if (suffix
!= NULL
) {
1648 ext
= strrchr(dent
->d_name
, '.');
1651 if (strcmp(ext
, suffix
) != 0)
1654 dbg(udev
, "put file '%s/%s' into list\n", dirname
, dent
->d_name
);
1656 snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, dent
->d_name
);
1657 filename
[sizeof(filename
)-1] = '\0';
1658 udev_list_entry_add(udev
, file_list
, filename
, NULL
, 1, 1);
1665 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1667 struct udev_rules
*rules
;
1668 struct stat statbuf
;
1669 struct udev_list_node file_list
;
1670 struct udev_list_entry
*file_loop
, *file_tmp
;
1671 struct token end_token
;
1673 rules
= malloc(sizeof(struct udev_rules
));
1676 memset(rules
, 0x00, sizeof(struct udev_rules
));
1678 rules
->resolve_names
= resolve_names
;
1679 udev_list_init(&file_list
);
1681 /* init token array and string buffer */
1682 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1683 if (rules
->tokens
== NULL
)
1685 rules
->token_max
= PREALLOC_TOKEN
;
1687 rules
->buf
= malloc(PREALLOC_STRBUF
);
1688 if (rules
->buf
== NULL
)
1690 rules
->buf_max
= PREALLOC_STRBUF
;
1691 /* offset 0 is always '\0' */
1692 rules
->buf
[0] = '\0';
1694 dbg(udev
, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1695 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1697 rules
->trie_nodes
= malloc(PREALLOC_TRIE
* sizeof(struct trie_node
));
1698 if (rules
->trie_nodes
== NULL
)
1700 rules
->trie_nodes_max
= PREALLOC_TRIE
;
1701 /* offset 0 is the trie root, with an empty string */
1702 memset(rules
->trie_nodes
, 0x00, sizeof(struct trie_node
));
1703 rules
->trie_nodes_cur
= 1;
1705 if (udev_get_rules_path(udev
) != NULL
) {
1706 /* custom rules location for testing */
1707 add_matching_files(udev
, &file_list
, udev_get_rules_path(udev
), ".rules");
1709 char filename
[PATH_MAX
];
1710 struct udev_list_node sort_list
;
1711 struct udev_list_entry
*sort_loop
, *sort_tmp
;
1713 /* read user/custom rules */
1714 add_matching_files(udev
, &file_list
, SYSCONFDIR
"/udev/rules.d", ".rules");
1716 /* read dynamic/temporary rules */
1717 util_strlcpy(filename
, udev_get_dev_path(udev
), sizeof(filename
));
1718 util_strlcat(filename
, "/.udev/rules.d", sizeof(filename
));
1719 if (stat(filename
, &statbuf
) != 0) {
1720 util_create_path(udev
, filename
);
1721 udev_selinux_setfscreatecon(udev
, filename
, S_IFDIR
|0755);
1722 mkdir(filename
, 0755);
1723 udev_selinux_resetfscreatecon(udev
);
1725 udev_list_init(&sort_list
);
1726 add_matching_files(udev
, &sort_list
, filename
, ".rules");
1728 /* read default rules */
1729 add_matching_files(udev
, &sort_list
, UDEV_PREFIX
"/lib/udev/rules.d", ".rules");
1731 /* sort all rules files by basename into list of files */
1732 udev_list_entry_foreach_safe(sort_loop
, sort_tmp
, udev_list_get_entry(&sort_list
)) {
1733 const char *sort_name
= udev_list_entry_get_name(sort_loop
);
1734 const char *sort_base
= strrchr(sort_name
, '/');
1736 if (sort_base
== NULL
)
1738 /* sort entry into existing list */
1739 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1740 const char *file_name
= udev_list_entry_get_name(file_loop
);
1741 const char *file_base
= strrchr(file_name
, '/');
1743 if (file_base
== NULL
)
1745 if (strcmp(file_base
, sort_base
) == 0) {
1746 info(udev
, "rule file basename '%s' already added, ignoring '%s'\n",
1747 file_name
, sort_name
);
1748 udev_list_entry_delete(sort_loop
);
1752 if (strcmp(file_base
, sort_base
) > 0) {
1753 /* found later file, insert before */
1754 udev_list_entry_remove(sort_loop
);
1755 udev_list_entry_insert_before(sort_loop
, file_loop
);
1760 /* current file already handled */
1761 if (sort_loop
== NULL
)
1763 /* no later file, append to end of list */
1764 udev_list_entry_remove(sort_loop
);
1765 udev_list_entry_append(sort_loop
, &file_list
);
1769 /* add all filenames to the string buffer */
1770 udev_list_entry_foreach(file_loop
, udev_list_get_entry(&file_list
)) {
1771 const char *filename
= udev_list_entry_get_name(file_loop
);
1772 unsigned int filename_off
;
1774 filename_off
= add_string(rules
, filename
);
1775 /* the offset in the rule is limited to unsigned short */
1776 if (filename_off
< USHRT_MAX
)
1777 udev_list_entry_set_flag(file_loop
, filename_off
);
1780 /* parse list of files */
1781 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1782 const char *filename
= udev_list_entry_get_name(file_loop
);
1783 unsigned int filename_off
= udev_list_entry_get_flag(file_loop
);
1785 if (stat(filename
, &statbuf
) == 0 && statbuf
.st_size
> 0)
1786 parse_file(rules
, filename
, filename_off
);
1788 info(udev
, "can not read '%s'\n", filename
);
1789 udev_list_entry_delete(file_loop
);
1792 memset(&end_token
, 0x00, sizeof(struct token
));
1793 end_token
.type
= TK_END
;
1794 add_token(rules
, &end_token
);
1796 /* shrink allocated token and string buffer */
1797 if (rules
->token_cur
< rules
->token_max
) {
1798 struct token
*tokens
;
1800 tokens
= realloc(rules
->tokens
, rules
->token_cur
* sizeof(struct token
));
1801 if (tokens
!= NULL
|| rules
->token_cur
== 0) {
1802 rules
->tokens
= tokens
;
1803 rules
->token_max
= rules
->token_cur
;
1806 if (rules
->buf_cur
< rules
->buf_max
) {
1809 buf
= realloc(rules
->buf
, rules
->buf_cur
);
1810 if (buf
!= NULL
|| rules
->buf_cur
== 0) {
1812 rules
->buf_max
= rules
->buf_cur
;
1815 info(udev
, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1816 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1817 info(udev
, "temporary index used %zu bytes (%u * %zu bytes)\n",
1818 rules
->trie_nodes_cur
* sizeof(struct trie_node
),
1819 rules
->trie_nodes_cur
, sizeof(struct trie_node
));
1822 free(rules
->trie_nodes
);
1823 rules
->trie_nodes
= NULL
;
1824 rules
->trie_nodes_cur
= 0;
1825 rules
->trie_nodes_max
= 0;
1827 /* cleanup uid/gid cache */
1830 rules
->uids_cur
= 0;
1831 rules
->uids_max
= 0;
1834 rules
->gids_cur
= 0;
1835 rules
->gids_max
= 0;
1841 void udev_rules_unref(struct udev_rules
*rules
)
1845 free(rules
->tokens
);
1847 free(rules
->trie_nodes
);
1853 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1855 char *key_value
= &rules
->buf
[token
->key
.value_off
];
1862 switch (token
->key
.glob
) {
1864 match
= (strcmp(key_value
, val
) == 0);
1867 match
= (fnmatch(key_value
, val
, 0) == 0);
1874 split
= &rules
->buf
[token
->key
.value_off
];
1879 next
= strchr(split
, '|');
1881 size_t matchlen
= (size_t)(next
- split
);
1883 match
= (matchlen
== len
&& strncmp(split
, val
, matchlen
) == 0);
1887 match
= (strcmp(split
, val
) == 0);
1896 char value
[UTIL_PATH_SIZE
];
1898 util_strlcpy(value
, &rules
->buf
[token
->key
.value_off
], sizeof(value
));
1900 while (key_value
!= NULL
) {
1901 pos
= strchr(key_value
, '|');
1906 dbg(rules
->udev
, "match %s '%s' <-> '%s'\n", token_str(token
->type
), key_value
, val
);
1907 match
= (fnmatch(key_value
, val
, 0) == 0);
1915 match
= (val
[0] != '\0');
1922 if (match
&& (token
->key
.op
== OP_MATCH
)) {
1923 dbg(rules
->udev
, "%s is true (matching value)\n", token_str(token
->type
));
1926 if (!match
&& (token
->key
.op
== OP_NOMATCH
)) {
1927 dbg(rules
->udev
, "%s is true (non-matching value)\n", token_str(token
->type
));
1930 dbg(rules
->udev
, "%s is not true\n", token_str(token
->type
));
1934 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1936 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
1937 const char *key_value
= &rules
->buf
[cur
->key
.value_off
];
1938 char value
[UTIL_NAME_SIZE
];
1942 if (key_name
[0] == '[') {
1943 char attr
[UTIL_PATH_SIZE
];
1945 util_strlcpy(attr
, key_name
, sizeof(attr
));
1946 util_resolve_subsys_kernel(event
->udev
, attr
, value
, sizeof(value
), 1);
1948 if (value
[0] == '\0') {
1951 val
= udev_device_get_sysattr_value(dev
, key_name
);
1954 util_strlcpy(value
, val
, sizeof(value
));
1957 /* strip trailing whitespace of value, if not asked to match for it */
1958 len
= strlen(key_value
);
1959 if (len
> 0 && !isspace(key_value
[len
-1])) {
1960 len
= strlen(value
);
1961 while (len
> 0 && isspace(value
[--len
]))
1963 dbg(rules
->udev
, "removed trailing whitespace from '%s'\n", value
);
1965 return match_key(rules
, cur
, value
);
1974 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
)
1978 enum escape_type esc
= ESCAPE_UNSET
;
1981 if (rules
->tokens
== NULL
)
1984 can_set_name
= ((strcmp(udev_device_get_action(event
->dev
), "add") == 0 ||
1985 strcmp(udev_device_get_action(event
->dev
), "change") == 0) &&
1986 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
1987 strcmp(udev_device_get_subsystem(event
->dev
), "net") == 0));
1989 /* loop through token list, match, run actions or forward to next rule */
1990 cur
= &rules
->tokens
[0];
1993 dump_token(rules
, cur
);
1994 switch (cur
->type
) {
1998 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1999 if (!can_set_name
&& rule
->rule
.flags
)
2004 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
2008 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
2012 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
2017 size_t devlen
= strlen(udev_get_dev_path(event
->udev
))+1;
2018 struct udev_list_entry
*list_entry
;
2021 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
2022 const char *devlink
;
2024 devlink
= &udev_list_entry_get_name(list_entry
)[devlen
];
2025 if (match_key(rules
, cur
, devlink
) == 0) {
2035 if (match_key(rules
, cur
, event
->name
) != 0)
2040 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2043 value
= udev_device_get_property_value(event
->dev
, key_name
);
2044 if (value
== NULL
) {
2045 dbg(event
->udev
, "ENV{%s} is not set, treat as empty\n", key_name
);
2048 if (match_key(rules
, cur
, value
))
2052 case TK_M_SUBSYSTEM
:
2053 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
2057 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
2062 char filename
[UTIL_PATH_SIZE
];
2065 util_strlcpy(filename
, &rules
->buf
[cur
->key
.value_off
], sizeof(filename
));
2066 udev_event_apply_format(event
, filename
, sizeof(filename
));
2067 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
2068 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
2073 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
2077 case TK_M_SUBSYSTEMS
:
2083 /* get whole sequence of parent matches */
2085 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2088 /* loop over parents */
2089 event
->dev_parent
= event
->dev
;
2093 dbg(event
->udev
, "parent: '%s'\n", udev_device_get_syspath(event
->dev_parent
));
2094 /* loop over sequence of parent match keys */
2095 for (key
= cur
; key
< next
; key
++ ) {
2096 dump_token(rules
, key
);
2099 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2102 case TK_M_SUBSYSTEMS
:
2103 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2107 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2111 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2117 dbg(event
->udev
, "parent key matched\n");
2119 dbg(event
->udev
, "all parent keys matched\n");
2120 /* all keys matched */
2124 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2125 if (event
->dev_parent
== NULL
)
2128 /* move behind our sequence of parent match keys */
2134 char filename
[UTIL_PATH_SIZE
];
2135 struct stat statbuf
;
2138 util_strlcpy(filename
, &rules
->buf
[cur
->key
.value_off
], sizeof(filename
));
2139 udev_event_apply_format(event
, filename
, sizeof(filename
));
2140 if (util_resolve_subsys_kernel(event
->udev
, NULL
, filename
, sizeof(filename
), 0) != 0)
2141 if (filename
[0] != '/') {
2142 char tmp
[UTIL_PATH_SIZE
];
2144 util_strlcpy(tmp
, udev_device_get_syspath(event
->dev
), sizeof(tmp
));
2145 util_strlcat(tmp
, "/", sizeof(tmp
));
2146 util_strlcat(tmp
, filename
, sizeof(tmp
));
2147 util_strlcpy(filename
, tmp
, sizeof(filename
));
2150 attr_subst_subdir(filename
, sizeof(filename
));
2152 match
= (stat(filename
, &statbuf
) == 0);
2153 dbg(event
->udev
, "'%s' %s", filename
, match
? "exists\n" : "does not exist\n");
2154 if (match
&& cur
->key
.mode
> 0) {
2155 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2156 dbg(event
->udev
, "'%s' has mode=%#o and %s %#o\n", filename
, statbuf
.st_mode
,
2157 match
? "matches" : "does not match", cur
->key
.mode
);
2159 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2161 if (!match
&& cur
->key
.op
== OP_MATCH
)
2167 char program
[UTIL_PATH_SIZE
];
2169 char result
[UTIL_PATH_SIZE
];
2171 free(event
->program_result
);
2172 event
->program_result
= NULL
;
2173 util_strlcpy(program
, &rules
->buf
[cur
->key
.value_off
], sizeof(program
));
2174 udev_event_apply_format(event
, program
, sizeof(program
));
2175 envp
= udev_device_get_properties_envp(event
->dev
);
2176 info(event
->udev
, "PROGRAM '%s' %s:%u\n",
2178 &rules
->buf
[rule
->rule
.filename_off
],
2179 rule
->rule
.filename_line
);
2180 if (util_run_program(event
->udev
, program
, envp
, result
, sizeof(result
), NULL
) != 0) {
2181 if (cur
->key
.op
!= OP_NOMATCH
)
2186 util_remove_trailing_chars(result
, '\n');
2187 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2188 count
= udev_util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2190 info(event
->udev
, "%i character(s) replaced\n" , count
);
2192 event
->program_result
= strdup(result
);
2193 dbg(event
->udev
, "storing result '%s'\n", event
->program_result
);
2194 if (cur
->key
.op
== OP_NOMATCH
)
2199 case TK_M_IMPORT_FILE
:
2201 char import
[UTIL_PATH_SIZE
];
2203 util_strlcpy(import
, &rules
->buf
[cur
->key
.value_off
], sizeof(import
));
2204 udev_event_apply_format(event
, import
, sizeof(import
));
2205 if (import_file_into_properties(event
->dev
, import
) != 0)
2206 if (cur
->key
.op
!= OP_NOMATCH
)
2210 case TK_M_IMPORT_PROG
:
2212 char import
[UTIL_PATH_SIZE
];
2214 util_strlcpy(import
, &rules
->buf
[cur
->key
.value_off
], sizeof(import
));
2215 udev_event_apply_format(event
, import
, sizeof(import
));
2216 info(event
->udev
, "IMPORT '%s' %s:%u\n",
2218 &rules
->buf
[rule
->rule
.filename_off
],
2219 rule
->rule
.filename_line
);
2220 if (import_program_into_properties(event
->dev
, import
) != 0)
2221 if (cur
->key
.op
!= OP_NOMATCH
)
2225 case TK_M_IMPORT_PARENT
:
2227 char import
[UTIL_PATH_SIZE
];
2229 util_strlcpy(import
, &rules
->buf
[cur
->key
.value_off
], sizeof(import
));
2230 udev_event_apply_format(event
, import
, sizeof(import
));
2231 if (import_parent_into_properties(event
->dev
, import
) != 0)
2232 if (cur
->key
.op
!= OP_NOMATCH
)
2237 if (match_key(rules
, cur
, event
->program_result
) != 0)
2241 case TK_A_IGNORE_DEVICE
:
2242 event
->ignore_device
= 1;
2245 case TK_A_STRING_ESCAPE_NONE
:
2248 case TK_A_STRING_ESCAPE_REPLACE
:
2249 esc
= ESCAPE_REPLACE
;
2251 case TK_A_NUM_FAKE_PART
:
2252 if (strcmp(udev_device_get_subsystem(event
->dev
), "block") != 0)
2254 if (udev_device_get_sysnum(event
->dev
) != NULL
)
2256 udev_device_set_num_fake_partitions(event
->dev
, cur
->key
.num_fake_part
);
2258 case TK_A_INOTIFY_WATCH
:
2259 event
->inotify_watch
= cur
->key
.watch
;
2261 case TK_A_DEVLINK_PRIO
:
2262 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2266 char owner
[UTIL_NAME_SIZE
];
2268 if (event
->owner_final
)
2270 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2271 event
->owner_final
= 1;
2272 util_strlcpy(owner
, &rules
->buf
[cur
->key
.value_off
], sizeof(owner
));
2273 udev_event_apply_format(event
, owner
, sizeof(owner
));
2274 event
->uid
= util_lookup_user(event
->udev
, owner
);
2275 info(event
->udev
, "OWNER %u %s:%u\n",
2277 &rules
->buf
[rule
->rule
.filename_off
],
2278 rule
->rule
.filename_line
);
2283 char group
[UTIL_NAME_SIZE
];
2285 if (event
->group_final
)
2287 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2288 event
->group_final
= 1;
2289 util_strlcpy(group
, &rules
->buf
[cur
->key
.value_off
], sizeof(group
));
2290 udev_event_apply_format(event
, group
, sizeof(group
));
2291 event
->gid
= util_lookup_group(event
->udev
, group
);
2292 info(event
->udev
, "GROUP %u %s:%u\n",
2294 &rules
->buf
[rule
->rule
.filename_off
],
2295 rule
->rule
.filename_line
);
2300 char mode
[UTIL_NAME_SIZE
];
2303 if (event
->mode_final
)
2305 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2306 event
->mode_final
= 1;
2307 util_strlcpy(mode
, &rules
->buf
[cur
->key
.value_off
], sizeof(mode
));
2308 udev_event_apply_format(event
, mode
, sizeof(mode
));
2309 event
->mode
= strtol(mode
, &endptr
, 8);
2310 if (endptr
[0] != '\0') {
2311 err(event
->udev
, "invalide mode '%s' set default mode 0660\n", mode
);
2314 info(event
->udev
, "MODE %#o %s:%u\n",
2316 &rules
->buf
[rule
->rule
.filename_off
],
2317 rule
->rule
.filename_line
);
2321 if (event
->owner_final
)
2323 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2324 event
->owner_final
= 1;
2325 event
->uid
= cur
->key
.uid
;
2326 info(event
->udev
, "OWNER %u %s:%u\n",
2328 &rules
->buf
[rule
->rule
.filename_off
],
2329 rule
->rule
.filename_line
);
2332 if (event
->group_final
)
2334 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2335 event
->group_final
= 1;
2336 event
->gid
= cur
->key
.gid
;
2337 info(event
->udev
, "GROUP %u %s:%u\n",
2339 &rules
->buf
[rule
->rule
.filename_off
],
2340 rule
->rule
.filename_line
);
2343 if (event
->mode_final
)
2345 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2346 event
->mode_final
= 1;
2347 event
->mode
= cur
->key
.mode
;
2348 info(event
->udev
, "MODE %#o %s:%u\n",
2350 &rules
->buf
[rule
->rule
.filename_off
],
2351 rule
->rule
.filename_line
);
2355 const char *name
= &rules
->buf
[cur
->key
.attr_off
];
2356 char *value
= &rules
->buf
[cur
->key
.value_off
];
2358 if (value
[0] != '\0') {
2359 char temp_value
[UTIL_NAME_SIZE
];
2360 struct udev_list_entry
*entry
;
2362 util_strlcpy(temp_value
, value
, sizeof(temp_value
));
2363 udev_event_apply_format(event
, temp_value
, sizeof(temp_value
));
2364 entry
= udev_device_add_property(event
->dev
, name
, temp_value
);
2366 udev_list_entry_set_flag(entry
, 1);
2368 udev_device_add_property(event
->dev
, name
, NULL
);
2374 const char *name
= &rules
->buf
[cur
->key
.value_off
];
2375 char name_str
[UTIL_PATH_SIZE
];
2378 if (event
->name_final
)
2380 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2381 event
->name_final
= 1;
2382 util_strlcpy(name_str
, name
, sizeof(name_str
));
2383 udev_event_apply_format(event
, name_str
, sizeof(name_str
));
2384 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2385 count
= udev_util_replace_chars(name_str
, "/");
2387 info(event
->udev
, "%i character(s) replaced\n", count
);
2390 event
->name
= strdup(name_str
);
2391 info(event
->udev
, "NAME '%s' %s:%u\n",
2393 &rules
->buf
[rule
->rule
.filename_off
],
2394 rule
->rule
.filename_line
);
2399 char temp
[UTIL_PATH_SIZE
];
2400 char filename
[UTIL_PATH_SIZE
];
2404 if (event
->devlink_final
)
2406 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2408 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2409 event
->devlink_final
= 1;
2410 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2411 udev_device_cleanup_devlinks_list(event
->dev
);
2413 /* allow multiple symlinks separated by spaces */
2414 util_strlcpy(temp
, &rules
->buf
[cur
->key
.value_off
], sizeof(temp
));
2415 udev_event_apply_format(event
, temp
, sizeof(temp
));
2416 if (esc
== ESCAPE_UNSET
)
2417 count
= udev_util_replace_chars(temp
, "/ ");
2418 else if (esc
== ESCAPE_REPLACE
)
2419 count
= udev_util_replace_chars(temp
, "/");
2421 info(event
->udev
, "%i character(s) replaced\n" , count
);
2422 dbg(event
->udev
, "rule applied, added symlink(s) '%s'\n", temp
);
2424 while (isspace(pos
[0]))
2426 next
= strchr(pos
, ' ');
2429 info(event
->udev
, "LINK '%s' %s:%u\n",
2431 &rules
->buf
[rule
->rule
.filename_off
],
2432 rule
->rule
.filename_line
);
2433 util_strlcpy(filename
, udev_get_dev_path(event
->udev
), sizeof(filename
));
2434 util_strlcat(filename
, "/", sizeof(filename
));
2435 util_strlcat(filename
, pos
, sizeof(filename
));
2436 udev_device_add_devlink(event
->dev
, filename
);
2437 while (isspace(next
[1]))
2440 next
= strchr(pos
, ' ');
2442 if (pos
[0] != '\0') {
2443 info(event
->udev
, "LINK '%s' %s:%u\n",
2445 &rules
->buf
[rule
->rule
.filename_off
],
2446 rule
->rule
.filename_line
);
2447 util_strlcpy(filename
, udev_get_dev_path(event
->udev
), sizeof(filename
));
2448 util_strlcat(filename
, "/", sizeof(filename
));
2449 util_strlcat(filename
, pos
, sizeof(filename
));
2450 udev_device_add_devlink(event
->dev
, filename
);
2454 case TK_A_EVENT_TIMEOUT
:
2455 udev_device_set_event_timeout(event
->dev
, cur
->key
.event_timeout
);
2457 case TK_A_IGNORE_REMOVE
:
2458 udev_device_set_ignore_remove(event
->dev
, 1);
2462 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2463 char attr
[UTIL_PATH_SIZE
];
2464 char value
[UTIL_NAME_SIZE
];
2467 util_strlcpy(attr
, key_name
, sizeof(attr
));
2468 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0) {
2469 util_strlcpy(attr
, udev_device_get_syspath(event
->dev
), sizeof(attr
));
2470 util_strlcat(attr
, "/", sizeof(attr
));
2471 util_strlcat(attr
, key_name
, sizeof(attr
));
2474 attr_subst_subdir(attr
, sizeof(attr
));
2476 util_strlcpy(value
, &rules
->buf
[cur
->key
.value_off
], sizeof(value
));
2477 udev_event_apply_format(event
, value
, sizeof(value
));
2478 info(event
->udev
, "ATTR '%s' writing '%s' %s:%u\n", attr
, value
,
2479 &rules
->buf
[rule
->rule
.filename_off
],
2480 rule
->rule
.filename_line
);
2481 f
= fopen(attr
, "w");
2483 if (fprintf(f
, "%s", value
) <= 0)
2484 err(event
->udev
, "error writing ATTR{%s}: %m\n", attr
);
2487 err(event
->udev
, "error opening ATTR{%s} for writing: %m\n", attr
);
2493 struct udev_list_entry
*list_entry
;
2495 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2496 udev_list_cleanup_entries(event
->udev
, &event
->run_list
);
2497 info(event
->udev
, "RUN '%s' %s:%u\n",
2498 &rules
->buf
[cur
->key
.value_off
],
2499 &rules
->buf
[rule
->rule
.filename_off
],
2500 rule
->rule
.filename_line
);
2501 list_entry
= udev_list_entry_add(event
->udev
, &event
->run_list
,
2502 &rules
->buf
[cur
->key
.value_off
], NULL
, 1, 0);
2503 if (cur
->key
.ignore_error
)
2504 udev_list_entry_set_flag(list_entry
, 1);
2508 if (cur
->key
.rule_goto
== 0)
2510 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2512 case TK_A_LAST_RULE
:
2516 case TK_M_PARENTS_MIN
:
2517 case TK_M_PARENTS_MAX
:
2520 err(rules
->udev
, "wrong type %u\n", cur
->type
);
2527 /* fast-forward to next rule */
2528 cur
= rule
+ rule
->rule
.token_count
;
2529 dbg(rules
->udev
, "forward to rule: %u\n",
2530 (unsigned int) (cur
- rules
->tokens
));