]>
Commit | Line | Data |
---|---|---|
2232cac8 | 1 | /* |
5539f624 | 2 | * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org> |
023ed7b0 | 3 | * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk> |
2232cac8 | 4 | * |
55e9959b KS |
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. | |
3d6d12c6 | 9 | * |
55e9959b KS |
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. | |
2232cac8 | 14 | * |
55e9959b KS |
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/>. | |
2232cac8 GKH |
17 | */ |
18 | ||
19 | #include <stddef.h> | |
1449a55d | 20 | #include <limits.h> |
2232cac8 | 21 | #include <stdlib.h> |
6d87ee2e | 22 | #include <stdbool.h> |
2232cac8 GKH |
23 | #include <string.h> |
24 | #include <stdio.h> | |
25 | #include <fcntl.h> | |
26 | #include <ctype.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
5ee7ecfb | 29 | #include <dirent.h> |
cea61f5c | 30 | #include <fnmatch.h> |
959e8b5d | 31 | #include <time.h> |
2232cac8 | 32 | |
2232cac8 | 33 | #include "udev.h" |
2232cac8 | 34 | |
6880b25d KS |
35 | #define PREALLOC_TOKEN 2048 |
36 | #define PREALLOC_STRBUF 32 * 1024 | |
1449a55d | 37 | #define PREALLOC_TRIE 256 |
c7521974 | 38 | |
4052400f KS |
39 | struct uid_gid { |
40 | unsigned int name_off; | |
41 | union { | |
42 | uid_t uid; | |
43 | gid_t gid; | |
44 | }; | |
45 | }; | |
46 | ||
1449a55d | 47 | struct trie_node { |
023ed7b0 | 48 | /* this node's first child */ |
bcf44d55 | 49 | unsigned int child_idx; |
023ed7b0 KS |
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) */ | |
bcf44d55 | 53 | unsigned int last_child_idx; |
1449a55d | 54 | unsigned int value_off; |
bcf44d55 | 55 | unsigned short value_len; |
023ed7b0 | 56 | unsigned char key; |
1449a55d AJ |
57 | }; |
58 | ||
4052400f KS |
59 | struct udev_rules { |
60 | struct udev *udev; | |
61 | int resolve_names; | |
62 | ||
63 | /* every key in the rules file becomes a token */ | |
64 | struct token *tokens; | |
65 | unsigned int token_cur; | |
66 | unsigned int token_max; | |
67 | ||
68 | /* all key strings are copied to a single string buffer */ | |
69 | char *buf; | |
70 | size_t buf_cur; | |
71 | size_t buf_max; | |
72 | unsigned int buf_count; | |
73 | ||
bcf44d55 | 74 | /* during rule parsing, strings are indexed to find duplicates */ |
bcf44d55 KS |
75 | struct trie_node *trie_nodes; |
76 | unsigned int trie_nodes_cur; | |
77 | unsigned int trie_nodes_max; | |
1449a55d | 78 | |
bcf44d55 | 79 | /* during rule parsing, uid/gid lookup results are cached */ |
4052400f KS |
80 | struct uid_gid *uids; |
81 | unsigned int uids_cur; | |
82 | unsigned int uids_max; | |
83 | struct uid_gid *gids; | |
84 | unsigned int gids_cur; | |
85 | unsigned int gids_max; | |
86 | }; | |
87 | ||
1449a55d | 88 | /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */ |
b0f7409f KS |
89 | enum operation_type { |
90 | OP_UNSET, | |
91 | ||
92 | OP_MATCH, | |
93 | OP_NOMATCH, | |
94 | OP_MATCH_MAX, | |
95 | ||
96 | OP_ADD, | |
97 | OP_ASSIGN, | |
98 | OP_ASSIGN_FINAL, | |
c7521974 KS |
99 | }; |
100 | ||
ac218d9c KS |
101 | enum string_glob_type { |
102 | GL_UNSET, | |
db463fd3 KS |
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 "?*" */ | |
ac218d9c KS |
108 | }; |
109 | ||
32028733 KS |
110 | enum string_subst_type { |
111 | SB_UNSET, | |
112 | SB_NONE, | |
113 | SB_FORMAT, | |
114 | SB_SUBSYS, | |
115 | }; | |
116 | ||
154a7b84 | 117 | /* tokens of a rule are sorted/handled in this order */ |
6880b25d | 118 | enum token_type { |
ac218d9c | 119 | TK_UNSET, |
6880b25d KS |
120 | TK_RULE, |
121 | ||
6880b25d KS |
122 | TK_M_ACTION, /* val */ |
123 | TK_M_DEVPATH, /* val */ | |
124 | TK_M_KERNEL, /* val */ | |
125 | TK_M_DEVLINK, /* val */ | |
126 | TK_M_NAME, /* val */ | |
127 | TK_M_ENV, /* val, attr */ | |
3f896a2a | 128 | TK_M_TAG, /* val */ |
6880b25d KS |
129 | TK_M_SUBSYSTEM, /* val */ |
130 | TK_M_DRIVER, /* val */ | |
7aeeaedc | 131 | TK_M_WAITFOR, /* val */ |
6880b25d KS |
132 | TK_M_ATTR, /* val, attr */ |
133 | ||
d15fcce4 | 134 | TK_M_PARENTS_MIN, |
6880b25d KS |
135 | TK_M_KERNELS, /* val */ |
136 | TK_M_SUBSYSTEMS, /* val */ | |
137 | TK_M_DRIVERS, /* val */ | |
138 | TK_M_ATTRS, /* val, attr */ | |
b0f7409f | 139 | TK_M_PARENTS_MAX, |
6880b25d KS |
140 | |
141 | TK_M_TEST, /* val, mode_t */ | |
142 | TK_M_PROGRAM, /* val */ | |
143 | TK_M_IMPORT_FILE, /* val */ | |
144 | TK_M_IMPORT_PROG, /* val */ | |
5539f624 | 145 | TK_M_IMPORT_DB, /* val */ |
6880b25d KS |
146 | TK_M_IMPORT_PARENT, /* val */ |
147 | TK_M_RESULT, /* val */ | |
ac218d9c | 148 | TK_M_MAX, |
6880b25d | 149 | |
6880b25d KS |
150 | TK_A_STRING_ESCAPE_NONE, |
151 | TK_A_STRING_ESCAPE_REPLACE, | |
46f194cb | 152 | TK_A_INOTIFY_WATCH, /* int */ |
6880b25d KS |
153 | TK_A_DEVLINK_PRIO, /* int */ |
154 | TK_A_OWNER, /* val */ | |
155 | TK_A_GROUP, /* val */ | |
156 | TK_A_MODE, /* val */ | |
157 | TK_A_OWNER_ID, /* uid_t */ | |
158 | TK_A_GROUP_ID, /* gid_t */ | |
159 | TK_A_MODE_ID, /* mode_t */ | |
160 | TK_A_ENV, /* val, attr */ | |
28460195 | 161 | TK_A_TAG, /* val */ |
6880b25d KS |
162 | TK_A_NAME, /* val */ |
163 | TK_A_DEVLINK, /* val */ | |
164 | TK_A_EVENT_TIMEOUT, /* int */ | |
6880b25d KS |
165 | TK_A_ATTR, /* val, attr */ |
166 | TK_A_RUN, /* val, bool */ | |
167 | TK_A_GOTO, /* size_t */ | |
6880b25d KS |
168 | |
169 | TK_END, | |
c7521974 KS |
170 | }; |
171 | ||
3f3aa9f5 | 172 | /* we try to pack stuff in a way that we take only 12 bytes per token */ |
4052400f KS |
173 | struct token { |
174 | union { | |
3f3aa9f5 | 175 | unsigned char type; /* same as in rule and key */ |
4052400f | 176 | struct { |
a25d547d | 177 | enum token_type type:8; |
32028733 | 178 | unsigned int flags:8; |
3f3aa9f5 | 179 | unsigned short token_count; |
4052400f KS |
180 | unsigned int label_off; |
181 | unsigned short filename_off; | |
182 | unsigned short filename_line; | |
183 | } rule; | |
184 | struct { | |
a25d547d KS |
185 | enum token_type type:8; |
186 | enum operation_type op:8; | |
187 | enum string_glob_type glob:8; | |
32028733 KS |
188 | enum string_subst_type subst:4; |
189 | enum string_subst_type attrsubst:4; | |
4052400f KS |
190 | unsigned int value_off; |
191 | union { | |
192 | unsigned int attr_off; | |
6c29f2b9 KS |
193 | int devlink_unique; |
194 | int fail_on_error; | |
4052400f KS |
195 | unsigned int rule_goto; |
196 | mode_t mode; | |
197 | uid_t uid; | |
198 | gid_t gid; | |
4052400f KS |
199 | int devlink_prio; |
200 | int event_timeout; | |
46f194cb | 201 | int watch; |
4052400f KS |
202 | }; |
203 | } key; | |
204 | }; | |
205 | }; | |
206 | ||
207 | #define MAX_TK 64 | |
208 | struct rule_tmp { | |
209 | struct udev_rules *rules; | |
210 | struct token rule; | |
211 | struct token token[MAX_TK]; | |
212 | unsigned int token_cur; | |
213 | }; | |
214 | ||
045a3bc8 | 215 | #ifdef ENABLE_DEBUG |
5d6a1fa6 KS |
216 | static const char *operation_str(enum operation_type type) |
217 | { | |
218 | static const char *operation_strs[] = { | |
219 | [OP_UNSET] = "UNSET", | |
220 | [OP_MATCH] = "match", | |
221 | [OP_NOMATCH] = "nomatch", | |
222 | [OP_MATCH_MAX] = "MATCH_MAX", | |
223 | ||
224 | [OP_ADD] = "add", | |
225 | [OP_ASSIGN] = "assign", | |
226 | [OP_ASSIGN_FINAL] = "assign-final", | |
227 | } ; | |
228 | ||
229 | return operation_strs[type]; | |
230 | } | |
4052400f | 231 | |
5d6a1fa6 KS |
232 | static const char *string_glob_str(enum string_glob_type type) |
233 | { | |
234 | static const char *string_glob_strs[] = { | |
235 | [GL_UNSET] = "UNSET", | |
236 | [GL_PLAIN] = "plain", | |
237 | [GL_GLOB] = "glob", | |
238 | [GL_SPLIT] = "split", | |
239 | [GL_SPLIT_GLOB] = "split-glob", | |
240 | [GL_SOMETHING] = "split-glob", | |
5d6a1fa6 | 241 | }; |
4052400f | 242 | |
5d6a1fa6 KS |
243 | return string_glob_strs[type]; |
244 | } | |
245 | ||
246 | static const char *token_str(enum token_type type) | |
247 | { | |
248 | static const char *token_strs[] = { | |
249 | [TK_UNSET] = "UNSET", | |
250 | [TK_RULE] = "RULE", | |
251 | ||
252 | [TK_M_ACTION] = "M ACTION", | |
253 | [TK_M_DEVPATH] = "M DEVPATH", | |
254 | [TK_M_KERNEL] = "M KERNEL", | |
255 | [TK_M_DEVLINK] = "M DEVLINK", | |
256 | [TK_M_NAME] = "M NAME", | |
257 | [TK_M_ENV] = "M ENV", | |
3f896a2a | 258 | [TK_M_TAG] = "M TAG", |
5d6a1fa6 KS |
259 | [TK_M_SUBSYSTEM] = "M SUBSYSTEM", |
260 | [TK_M_DRIVER] = "M DRIVER", | |
261 | [TK_M_WAITFOR] = "M WAITFOR", | |
262 | [TK_M_ATTR] = "M ATTR", | |
263 | ||
264 | [TK_M_PARENTS_MIN] = "M PARENTS_MIN", | |
265 | [TK_M_KERNELS] = "M KERNELS", | |
266 | [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS", | |
267 | [TK_M_DRIVERS] = "M DRIVERS", | |
268 | [TK_M_ATTRS] = "M ATTRS", | |
269 | [TK_M_PARENTS_MAX] = "M PARENTS_MAX", | |
270 | ||
271 | [TK_M_TEST] = "M TEST", | |
272 | [TK_M_PROGRAM] = "M PROGRAM", | |
273 | [TK_M_IMPORT_FILE] = "M IMPORT_FILE", | |
274 | [TK_M_IMPORT_PROG] = "M IMPORT_PROG", | |
5539f624 | 275 | [TK_M_IMPORT_DB] = "M IMPORT_DB", |
214a6c79 | 276 | [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT", |
5d6a1fa6 KS |
277 | [TK_M_RESULT] = "M RESULT", |
278 | [TK_M_MAX] = "M MAX", | |
279 | ||
5d6a1fa6 KS |
280 | [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE", |
281 | [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE", | |
bd284db1 | 282 | [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH", |
5d6a1fa6 KS |
283 | [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO", |
284 | [TK_A_OWNER] = "A OWNER", | |
285 | [TK_A_GROUP] = "A GROUP", | |
286 | [TK_A_MODE] = "A MODE", | |
287 | [TK_A_OWNER_ID] = "A OWNER_ID", | |
288 | [TK_A_GROUP_ID] = "A GROUP_ID", | |
289 | [TK_A_MODE_ID] = "A MODE_ID", | |
290 | [TK_A_ENV] = "A ENV", | |
28460195 | 291 | [TK_A_TAG] = "A ENV", |
5d6a1fa6 KS |
292 | [TK_A_NAME] = "A NAME", |
293 | [TK_A_DEVLINK] = "A DEVLINK", | |
294 | [TK_A_EVENT_TIMEOUT] = "A EVENT_TIMEOUT", | |
5d6a1fa6 KS |
295 | [TK_A_ATTR] = "A ATTR", |
296 | [TK_A_RUN] = "A RUN", | |
297 | [TK_A_GOTO] = "A GOTO", | |
5d6a1fa6 KS |
298 | |
299 | [TK_END] = "END", | |
300 | }; | |
301 | ||
302 | return token_strs[type]; | |
303 | } | |
c7521974 | 304 | |
4052400f KS |
305 | static void dump_token(struct udev_rules *rules, struct token *token) |
306 | { | |
307 | enum token_type type = token->type; | |
308 | enum operation_type op = token->key.op; | |
309 | enum string_glob_type glob = token->key.glob; | |
310 | const char *value = &rules->buf[token->key.value_off]; | |
311 | const char *attr = &rules->buf[token->key.attr_off]; | |
154a7b84 | 312 | |
4052400f KS |
313 | switch (type) { |
314 | case TK_RULE: | |
315 | { | |
316 | const char *tks_ptr = (char *)rules->tokens; | |
317 | const char *tk_ptr = (char *)token; | |
5d6a1fa6 | 318 | unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token); |
154a7b84 | 319 | |
5d6a1fa6 | 320 | dbg(rules->udev, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n", |
4052400f | 321 | &rules->buf[token->rule.filename_off], token->rule.filename_line, |
5d6a1fa6 | 322 | idx, token->rule.token_count, |
4052400f KS |
323 | &rules->buf[token->rule.label_off], |
324 | token->rule.flags); | |
325 | break; | |
326 | } | |
327 | case TK_M_ACTION: | |
328 | case TK_M_DEVPATH: | |
329 | case TK_M_KERNEL: | |
330 | case TK_M_SUBSYSTEM: | |
331 | case TK_M_DRIVER: | |
332 | case TK_M_WAITFOR: | |
333 | case TK_M_DEVLINK: | |
334 | case TK_M_NAME: | |
335 | case TK_M_KERNELS: | |
336 | case TK_M_SUBSYSTEMS: | |
337 | case TK_M_DRIVERS: | |
338 | case TK_M_PROGRAM: | |
339 | case TK_M_IMPORT_FILE: | |
340 | case TK_M_IMPORT_PROG: | |
5539f624 | 341 | case TK_M_IMPORT_DB: |
4052400f KS |
342 | case TK_M_IMPORT_PARENT: |
343 | case TK_M_RESULT: | |
344 | case TK_A_NAME: | |
345 | case TK_A_DEVLINK: | |
346 | case TK_A_OWNER: | |
347 | case TK_A_GROUP: | |
348 | case TK_A_MODE: | |
349 | case TK_A_RUN: | |
350 | dbg(rules->udev, "%s %s '%s'(%s)\n", | |
5d6a1fa6 | 351 | token_str(type), operation_str(op), value, string_glob_str(glob)); |
4052400f KS |
352 | break; |
353 | case TK_M_ATTR: | |
354 | case TK_M_ATTRS: | |
355 | case TK_M_ENV: | |
356 | case TK_A_ATTR: | |
357 | case TK_A_ENV: | |
358 | dbg(rules->udev, "%s %s '%s' '%s'(%s)\n", | |
5d6a1fa6 | 359 | token_str(type), operation_str(op), attr, value, string_glob_str(glob)); |
4052400f | 360 | break; |
3f896a2a | 361 | case TK_M_TAG: |
28460195 KS |
362 | case TK_A_TAG: |
363 | dbg(rules->udev, "%s %s '%s'\n", token_str(type), operation_str(op), value); | |
364 | break; | |
4052400f KS |
365 | case TK_A_STRING_ESCAPE_NONE: |
366 | case TK_A_STRING_ESCAPE_REPLACE: | |
5d69a34c KS |
367 | dbg(rules->udev, "%s\n", token_str(type)); |
368 | break; | |
4052400f KS |
369 | case TK_M_TEST: |
370 | dbg(rules->udev, "%s %s '%s'(%s) %#o\n", | |
5d6a1fa6 | 371 | token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode); |
4052400f | 372 | break; |
46f194cb | 373 | case TK_A_INOTIFY_WATCH: |
2236ddae | 374 | dbg(rules->udev, "%s %u\n", token_str(type), token->key.watch); |
46f194cb | 375 | break; |
4052400f | 376 | case TK_A_DEVLINK_PRIO: |
5d6a1fa6 | 377 | dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.devlink_prio); |
4052400f KS |
378 | break; |
379 | case TK_A_OWNER_ID: | |
5d6a1fa6 | 380 | dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.uid); |
4052400f KS |
381 | break; |
382 | case TK_A_GROUP_ID: | |
5d6a1fa6 | 383 | dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.gid); |
4052400f KS |
384 | break; |
385 | case TK_A_MODE_ID: | |
5d6a1fa6 | 386 | dbg(rules->udev, "%s %s %#o\n", token_str(type), operation_str(op), token->key.mode); |
4052400f KS |
387 | break; |
388 | case TK_A_EVENT_TIMEOUT: | |
5d6a1fa6 | 389 | dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.event_timeout); |
4052400f KS |
390 | break; |
391 | case TK_A_GOTO: | |
5d6a1fa6 | 392 | dbg(rules->udev, "%s '%s' %u\n", token_str(type), value, token->key.rule_goto); |
4052400f KS |
393 | break; |
394 | case TK_END: | |
5d6a1fa6 | 395 | dbg(rules->udev, "* %s\n", token_str(type)); |
4052400f KS |
396 | break; |
397 | case TK_M_PARENTS_MIN: | |
398 | case TK_M_PARENTS_MAX: | |
399 | case TK_M_MAX: | |
400 | case TK_UNSET: | |
401 | dbg(rules->udev, "unknown type %u\n", type); | |
402 | break; | |
403 | } | |
404 | } | |
154a7b84 | 405 | |
4052400f KS |
406 | static void dump_rules(struct udev_rules *rules) |
407 | { | |
408 | unsigned int i; | |
154a7b84 | 409 | |
4052400f KS |
410 | dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n", |
411 | rules->token_cur, | |
412 | rules->token_cur * sizeof(struct token), | |
413 | rules->buf_count, | |
414 | rules->buf_cur); | |
415 | for(i = 0; i < rules->token_cur; i++) | |
416 | dump_token(rules, &rules->tokens[i]); | |
417 | } | |
418 | #else | |
5d6a1fa6 KS |
419 | static inline const char *operation_str(enum operation_type type) { return NULL; } |
420 | static inline const char *token_str(enum token_type type) { return NULL; } | |
4052400f KS |
421 | static inline void dump_token(struct udev_rules *rules, struct token *token) {} |
422 | static inline void dump_rules(struct udev_rules *rules) {} | |
045a3bc8 | 423 | #endif /* ENABLE_DEBUG */ |
c7521974 | 424 | |
1449a55d | 425 | static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes) |
c7521974 | 426 | { |
6880b25d | 427 | int off; |
c7521974 | 428 | |
34d6a259 | 429 | /* grow buffer if needed */ |
1449a55d | 430 | if (rules->buf_cur + bytes+1 >= rules->buf_max) { |
6880b25d KS |
431 | char *buf; |
432 | unsigned int add; | |
c7521974 | 433 | |
6880b25d KS |
434 | /* double the buffer size */ |
435 | add = rules->buf_max; | |
1449a55d AJ |
436 | if (add < bytes * 8) |
437 | add = bytes * 8; | |
c7521974 | 438 | |
6880b25d KS |
439 | buf = realloc(rules->buf, rules->buf_max + add); |
440 | if (buf == NULL) | |
441 | return -1; | |
86b57788 | 442 | dbg(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add); |
6880b25d KS |
443 | rules->buf = buf; |
444 | rules->buf_max += add; | |
c7521974 | 445 | } |
6880b25d | 446 | off = rules->buf_cur; |
1449a55d AJ |
447 | memcpy(&rules->buf[rules->buf_cur], str, bytes); |
448 | rules->buf_cur += bytes; | |
6880b25d KS |
449 | rules->buf_count++; |
450 | return off; | |
c7521974 KS |
451 | } |
452 | ||
1449a55d | 453 | static int add_string(struct udev_rules *rules, const char *str) |
c7521974 | 454 | { |
bcf44d55 KS |
455 | unsigned int node_idx; |
456 | struct trie_node *new_node; | |
457 | unsigned int new_node_idx; | |
1449a55d | 458 | unsigned char key; |
bcf44d55 | 459 | unsigned short len; |
1c8af93a | 460 | unsigned int depth; |
1449a55d | 461 | unsigned int off; |
023ed7b0 | 462 | struct trie_node *parent; |
1449a55d | 463 | |
023ed7b0 | 464 | /* walk trie, start from last character of str to find matching tails */ |
949075db | 465 | len = strlen(str); |
023ed7b0 | 466 | key = str[len-1]; |
949075db | 467 | node_idx = 0; |
023ed7b0 KS |
468 | for (depth = 0; depth <= len; depth++) { |
469 | struct trie_node *node; | |
470 | unsigned int child_idx; | |
471 | ||
472 | node = &rules->trie_nodes[node_idx]; | |
473 | off = node->value_off + node->value_len - len; | |
474 | ||
475 | /* match against current node */ | |
476 | if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0)) | |
477 | return off; | |
478 | ||
479 | /* lookup child node */ | |
480 | key = str[len - 1 - depth]; | |
481 | child_idx = node->child_idx; | |
482 | while (child_idx > 0) { | |
483 | struct trie_node *child; | |
484 | ||
485 | child = &rules->trie_nodes[child_idx]; | |
486 | if (child->key == key) | |
1449a55d | 487 | break; |
023ed7b0 | 488 | child_idx = child->next_child_idx; |
1449a55d | 489 | } |
023ed7b0 KS |
490 | if (child_idx == 0) |
491 | break; | |
492 | node_idx = child_idx; | |
1449a55d AJ |
493 | } |
494 | ||
495 | /* string not found, add it */ | |
496 | off = add_new_string(rules, str, len + 1); | |
497 | ||
bcf44d55 KS |
498 | /* grow trie nodes if needed */ |
499 | if (rules->trie_nodes_cur >= rules->trie_nodes_max) { | |
500 | struct trie_node *nodes; | |
501 | unsigned int add; | |
1449a55d AJ |
502 | |
503 | /* double the buffer size */ | |
bcf44d55 | 504 | add = rules->trie_nodes_max; |
1449a55d AJ |
505 | if (add < 8) |
506 | add = 8; | |
507 | ||
bcf44d55 KS |
508 | nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node)); |
509 | if (nodes == NULL) | |
1449a55d | 510 | return -1; |
bcf44d55 KS |
511 | dbg(rules->udev, "extend trie nodes from %u to %u\n", |
512 | rules->trie_nodes_max, rules->trie_nodes_max + add); | |
513 | rules->trie_nodes = nodes; | |
514 | rules->trie_nodes_max += add; | |
1449a55d AJ |
515 | } |
516 | ||
023ed7b0 | 517 | /* get a new node */ |
bcf44d55 KS |
518 | new_node_idx = rules->trie_nodes_cur; |
519 | rules->trie_nodes_cur++; | |
520 | new_node = &rules->trie_nodes[new_node_idx]; | |
023ed7b0 | 521 | memset(new_node, 0x00, sizeof(struct trie_node)); |
bcf44d55 KS |
522 | new_node->value_off = off; |
523 | new_node->value_len = len; | |
023ed7b0 | 524 | new_node->key = key; |
bcf44d55 | 525 | |
023ed7b0 KS |
526 | /* join the parent's child list */ |
527 | parent = &rules->trie_nodes[node_idx]; | |
528 | if (parent->child_idx == 0) { | |
529 | parent->child_idx = new_node_idx; | |
bcf44d55 | 530 | } else { |
023ed7b0 | 531 | struct trie_node *last_child; |
1449a55d | 532 | |
023ed7b0 KS |
533 | last_child = &rules->trie_nodes[parent->last_child_idx]; |
534 | last_child->next_child_idx = new_node_idx; | |
bcf44d55 | 535 | } |
023ed7b0 | 536 | parent->last_child_idx = new_node_idx; |
1449a55d AJ |
537 | return off; |
538 | } | |
539 | ||
540 | static int add_token(struct udev_rules *rules, struct token *token) | |
541 | { | |
34d6a259 | 542 | /* grow buffer if needed */ |
6880b25d KS |
543 | if (rules->token_cur+1 >= rules->token_max) { |
544 | struct token *tokens; | |
545 | unsigned int add; | |
c7521974 | 546 | |
6880b25d KS |
547 | /* double the buffer size */ |
548 | add = rules->token_max; | |
154a7b84 KS |
549 | if (add < 8) |
550 | add = 8; | |
c7521974 | 551 | |
6880b25d KS |
552 | tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token)); |
553 | if (tokens == NULL) | |
554 | return -1; | |
86b57788 | 555 | dbg(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add); |
6880b25d KS |
556 | rules->tokens = tokens; |
557 | rules->token_max += add; | |
c7521974 | 558 | } |
6880b25d KS |
559 | memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token)); |
560 | rules->token_cur++; | |
561 | return 0; | |
c7521974 | 562 | } |
a27cd06c | 563 | |
154a7b84 KS |
564 | static uid_t add_uid(struct udev_rules *rules, const char *owner) |
565 | { | |
566 | unsigned int i; | |
567 | uid_t uid; | |
568 | unsigned int off; | |
569 | ||
570 | /* lookup, if we know it already */ | |
571 | for (i = 0; i < rules->uids_cur; i++) { | |
572 | off = rules->uids[i].name_off; | |
573 | if (strcmp(&rules->buf[off], owner) == 0) { | |
574 | uid = rules->uids[i].uid; | |
575 | dbg(rules->udev, "return existing %u for '%s'\n", uid, owner); | |
576 | return uid; | |
577 | } | |
578 | } | |
579 | uid = util_lookup_user(rules->udev, owner); | |
580 | ||
581 | /* grow buffer if needed */ | |
582 | if (rules->uids_cur+1 >= rules->uids_max) { | |
583 | struct uid_gid *uids; | |
584 | unsigned int add; | |
585 | ||
586 | /* double the buffer size */ | |
587 | add = rules->uids_max; | |
588 | if (add < 1) | |
589 | add = 8; | |
590 | ||
591 | uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid)); | |
592 | if (uids == NULL) | |
593 | return uid; | |
86b57788 | 594 | dbg(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add); |
154a7b84 KS |
595 | rules->uids = uids; |
596 | rules->uids_max += add; | |
597 | } | |
598 | rules->uids[rules->uids_cur].uid = uid; | |
599 | off = add_string(rules, owner); | |
600 | if (off <= 0) | |
601 | return uid; | |
602 | rules->uids[rules->uids_cur].name_off = off; | |
603 | rules->uids_cur++; | |
604 | return uid; | |
605 | } | |
606 | ||
607 | static gid_t add_gid(struct udev_rules *rules, const char *group) | |
608 | { | |
609 | unsigned int i; | |
610 | gid_t gid; | |
611 | unsigned int off; | |
612 | ||
613 | /* lookup, if we know it already */ | |
614 | for (i = 0; i < rules->gids_cur; i++) { | |
615 | off = rules->gids[i].name_off; | |
616 | if (strcmp(&rules->buf[off], group) == 0) { | |
617 | gid = rules->gids[i].gid; | |
ac218d9c | 618 | dbg(rules->udev, "return existing %u for '%s'\n", gid, group); |
154a7b84 KS |
619 | return gid; |
620 | } | |
621 | } | |
622 | gid = util_lookup_group(rules->udev, group); | |
623 | ||
624 | /* grow buffer if needed */ | |
625 | if (rules->gids_cur+1 >= rules->gids_max) { | |
626 | struct uid_gid *gids; | |
627 | unsigned int add; | |
628 | ||
629 | /* double the buffer size */ | |
630 | add = rules->gids_max; | |
631 | if (add < 1) | |
632 | add = 8; | |
633 | ||
634 | gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid)); | |
635 | if (gids == NULL) | |
636 | return gid; | |
86b57788 | 637 | dbg(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add); |
154a7b84 KS |
638 | rules->gids = gids; |
639 | rules->gids_max += add; | |
640 | } | |
641 | rules->gids[rules->gids_cur].gid = gid; | |
642 | off = add_string(rules, group); | |
643 | if (off <= 0) | |
644 | return gid; | |
645 | rules->gids[rules->gids_cur].name_off = off; | |
646 | rules->gids_cur++; | |
647 | return gid; | |
648 | } | |
649 | ||
bb144ed1 | 650 | static int import_property_from_string(struct udev_device *dev, char *line) |
bd0ed2ff | 651 | { |
bb144ed1 KS |
652 | struct udev *udev = udev_device_get_udev(dev); |
653 | char *key; | |
654 | char *val; | |
655 | size_t len; | |
bd0ed2ff | 656 | |
bb144ed1 KS |
657 | /* find key */ |
658 | key = line; | |
659 | while (isspace(key[0])) | |
660 | key++; | |
bd0ed2ff | 661 | |
bb144ed1 KS |
662 | /* comment or empty line */ |
663 | if (key[0] == '#' || key[0] == '\0') | |
664 | return -1; | |
16511863 | 665 | |
bb144ed1 KS |
666 | /* split key/value */ |
667 | val = strchr(key, '='); | |
668 | if (val == NULL) | |
669 | return -1; | |
670 | val[0] = '\0'; | |
671 | val++; | |
aa8734ff | 672 | |
bb144ed1 KS |
673 | /* find value */ |
674 | while (isspace(val[0])) | |
675 | val++; | |
676 | ||
677 | /* terminate key */ | |
678 | len = strlen(key); | |
679 | if (len == 0) | |
680 | return -1; | |
681 | while (isspace(key[len-1])) | |
682 | len--; | |
683 | key[len] = '\0'; | |
684 | ||
685 | /* terminate value */ | |
686 | len = strlen(val); | |
687 | if (len == 0) | |
688 | return -1; | |
689 | while (isspace(val[len-1])) | |
690 | len--; | |
691 | val[len] = '\0'; | |
692 | ||
693 | if (len == 0) | |
694 | return -1; | |
695 | ||
696 | /* unquote */ | |
697 | if (val[0] == '"' || val[0] == '\'') { | |
698 | if (val[len-1] != val[0]) { | |
699 | info(udev, "inconsistent quoting: '%s', skip\n", line); | |
700 | return -1; | |
bd0ed2ff | 701 | } |
bb144ed1 KS |
702 | val[len-1] = '\0'; |
703 | val++; | |
704 | } | |
705 | ||
86b57788 | 706 | dbg(udev, "adding '%s'='%s'\n", key, val); |
bb144ed1 KS |
707 | |
708 | /* handle device, renamed by external tool, returning new path */ | |
709 | if (strcmp(key, "DEVPATH") == 0) { | |
710 | char syspath[UTIL_PATH_SIZE]; | |
711 | ||
712 | info(udev, "updating devpath from '%s' to '%s'\n", | |
713 | udev_device_get_devpath(dev), val); | |
065db052 | 714 | util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), val, NULL); |
bb144ed1 KS |
715 | udev_device_set_syspath(dev, syspath); |
716 | } else { | |
717 | struct udev_list_entry *entry; | |
718 | ||
719 | entry = udev_device_add_property(dev, key, val); | |
b25a9454 KS |
720 | /* store in db, skip private keys */ |
721 | if (key[0] != '.') | |
fbb31cd6 | 722 | udev_list_entry_set_flags(entry, 1); |
bd0ed2ff | 723 | } |
be4bedd1 KS |
724 | return 0; |
725 | } | |
726 | ||
6880b25d | 727 | static int import_file_into_properties(struct udev_device *dev, const char *filename) |
be4bedd1 | 728 | { |
bb144ed1 KS |
729 | FILE *f; |
730 | char line[UTIL_LINE_SIZE]; | |
be4bedd1 | 731 | |
bb144ed1 KS |
732 | f = fopen(filename, "r"); |
733 | if (f == NULL) | |
be4bedd1 | 734 | return -1; |
fc42bd5d | 735 | while (fgets(line, sizeof(line), f) != NULL) |
bb144ed1 KS |
736 | import_property_from_string(dev, line); |
737 | fclose(f); | |
be4bedd1 | 738 | return 0; |
bd0ed2ff KS |
739 | } |
740 | ||
6880b25d | 741 | static int import_program_into_properties(struct udev_device *dev, const char *program) |
319c6700 | 742 | { |
d0db192f KS |
743 | struct udev *udev = udev_device_get_udev(dev); |
744 | char **envp; | |
52761bb0 | 745 | char result[4096]; |
319c6700 | 746 | size_t reslen; |
bb144ed1 | 747 | char *line; |
319c6700 | 748 | |
d0db192f | 749 | envp = udev_device_get_properties_envp(dev); |
e85f5ec1 | 750 | if (util_run_program(udev, program, envp, result, sizeof(result), &reslen, NULL, false) != 0) |
319c6700 | 751 | return -1; |
bb144ed1 KS |
752 | |
753 | line = result; | |
754 | while (line != NULL) { | |
755 | char *pos; | |
756 | ||
757 | pos = strchr(line, '\n'); | |
758 | if (pos != NULL) { | |
759 | pos[0] = '\0'; | |
760 | pos = &pos[1]; | |
761 | } | |
762 | import_property_from_string(dev, line); | |
763 | line = pos; | |
764 | } | |
765 | return 0; | |
319c6700 KS |
766 | } |
767 | ||
6880b25d | 768 | static int import_parent_into_properties(struct udev_device *dev, const char *filter) |
0bfb84e1 | 769 | { |
bb144ed1 | 770 | struct udev *udev = udev_device_get_udev(dev); |
aa8734ff KS |
771 | struct udev_device *dev_parent; |
772 | struct udev_list_entry *list_entry; | |
0bfb84e1 | 773 | |
bb144ed1 | 774 | dev_parent = udev_device_get_parent(dev); |
aa8734ff KS |
775 | if (dev_parent == NULL) |
776 | return -1; | |
0bfb84e1 | 777 | |
bb144ed1 | 778 | dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent)); |
aa8734ff KS |
779 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) { |
780 | const char *key = udev_list_entry_get_name(list_entry); | |
781 | const char *val = udev_list_entry_get_value(list_entry); | |
0bfb84e1 | 782 | |
aa8734ff KS |
783 | if (fnmatch(filter, key, 0) == 0) { |
784 | struct udev_list_entry *entry; | |
e2ecb34f | 785 | |
bb144ed1 KS |
786 | dbg(udev, "import key '%s=%s'\n", key, val); |
787 | entry = udev_device_add_property(dev, key, val); | |
b25a9454 KS |
788 | /* store in db, skip private keys */ |
789 | if (key[0] != '.') | |
fbb31cd6 | 790 | udev_list_entry_set_flags(entry, 1); |
aa8734ff | 791 | } |
e2ecb34f | 792 | } |
aa8734ff | 793 | return 0; |
e2ecb34f KS |
794 | } |
795 | ||
c86be870 | 796 | #define WAIT_LOOP_PER_SECOND 50 |
6880b25d | 797 | static int wait_for_file(struct udev_device *dev, const char *file, int timeout) |
b2fe4b9a | 798 | { |
6880b25d | 799 | struct udev *udev = udev_device_get_udev(dev); |
17fcfb59 | 800 | char filepath[UTIL_PATH_SIZE]; |
1822e9b0 | 801 | char devicepath[UTIL_PATH_SIZE]; |
b2fe4b9a KS |
802 | struct stat stats; |
803 | int loop = timeout * WAIT_LOOP_PER_SECOND; | |
804 | ||
ea97dc37 | 805 | /* a relative path is a device attribute */ |
1822e9b0 | 806 | devicepath[0] = '\0'; |
ea97dc37 | 807 | if (file[0] != '/') { |
065db052 KS |
808 | util_strscpyl(devicepath, sizeof(devicepath), |
809 | udev_get_sys_path(udev), udev_device_get_devpath(dev), NULL); | |
810 | util_strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL); | |
ea97dc37 KS |
811 | file = filepath; |
812 | } | |
813 | ||
6880b25d | 814 | dbg(udev, "will wait %i sec for '%s'\n", timeout, file); |
b2fe4b9a | 815 | while (--loop) { |
959e8b5d DM |
816 | const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND }; |
817 | ||
656ba91e | 818 | /* lookup file */ |
ea97dc37 | 819 | if (stat(file, &stats) == 0) { |
6880b25d | 820 | info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1); |
b2fe4b9a KS |
821 | return 0; |
822 | } | |
eb5b8640 | 823 | /* make sure, the device did not disappear in the meantime */ |
ea97dc37 | 824 | if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) { |
6880b25d | 825 | info(udev, "device disappeared while waiting for '%s'\n", file); |
656ba91e KS |
826 | return -2; |
827 | } | |
6880b25d | 828 | info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND); |
959e8b5d | 829 | nanosleep(&duration, NULL); |
b2fe4b9a | 830 | } |
6880b25d | 831 | info(udev, "waiting for '%s' failed\n", file); |
b2fe4b9a KS |
832 | return -1; |
833 | } | |
834 | ||
0ea5e96e KS |
835 | static int attr_subst_subdir(char *attr, size_t len) |
836 | { | |
0ea5e96e KS |
837 | int found = 0; |
838 | ||
065db052 KS |
839 | if (strstr(attr, "/*/")) { |
840 | char *pos; | |
841 | char dirname[UTIL_PATH_SIZE]; | |
842 | const char *tail; | |
0ea5e96e KS |
843 | DIR *dir; |
844 | ||
065db052 KS |
845 | util_strscpy(dirname, sizeof(dirname), attr); |
846 | pos = strstr(dirname, "/*/"); | |
847 | if (pos == NULL) | |
848 | return -1; | |
849 | pos[0] = '\0'; | |
850 | tail = &pos[2]; | |
851 | dir = opendir(dirname); | |
0ea5e96e KS |
852 | if (dir != NULL) { |
853 | struct dirent *dent; | |
854 | ||
855 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { | |
856 | struct stat stats; | |
857 | ||
858 | if (dent->d_name[0] == '.') | |
859 | continue; | |
065db052 | 860 | util_strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL); |
0ea5e96e KS |
861 | if (stat(attr, &stats) == 0) { |
862 | found = 1; | |
863 | break; | |
864 | } | |
0ea5e96e KS |
865 | } |
866 | closedir(dir); | |
867 | } | |
0ea5e96e KS |
868 | } |
869 | ||
870 | return found; | |
871 | } | |
872 | ||
b0f7409f | 873 | static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) |
a0e5382d | 874 | { |
6880b25d KS |
875 | char *linepos; |
876 | char *temp; | |
a0e5382d | 877 | |
6880b25d | 878 | linepos = *line; |
9257ecfc | 879 | if (linepos == NULL || linepos[0] == '\0') |
6880b25d | 880 | return -1; |
a0e5382d | 881 | |
6880b25d KS |
882 | /* skip whitespace */ |
883 | while (isspace(linepos[0]) || linepos[0] == ',') | |
884 | linepos++; | |
aa8734ff | 885 | |
6880b25d KS |
886 | /* get the key */ |
887 | if (linepos[0] == '\0') | |
888 | return -1; | |
889 | *key = linepos; | |
81313e1b | 890 | |
6880b25d KS |
891 | while (1) { |
892 | linepos++; | |
893 | if (linepos[0] == '\0') | |
894 | return -1; | |
895 | if (isspace(linepos[0])) | |
81313e1b | 896 | break; |
6880b25d KS |
897 | if (linepos[0] == '=') |
898 | break; | |
899 | if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':')) | |
900 | if (linepos[1] == '=') | |
1ff1aa42 | 901 | break; |
953249a3 KS |
902 | } |
903 | ||
6880b25d KS |
904 | /* remember end of key */ |
905 | temp = linepos; | |
b2fe4b9a | 906 | |
6880b25d KS |
907 | /* skip whitespace after key */ |
908 | while (isspace(linepos[0])) | |
909 | linepos++; | |
910 | if (linepos[0] == '\0') | |
911 | return -1; | |
b2fe4b9a | 912 | |
6880b25d KS |
913 | /* get operation type */ |
914 | if (linepos[0] == '=' && linepos[1] == '=') { | |
b0f7409f | 915 | *op = OP_MATCH; |
6880b25d KS |
916 | linepos += 2; |
917 | } else if (linepos[0] == '!' && linepos[1] == '=') { | |
b0f7409f | 918 | *op = OP_NOMATCH; |
6880b25d KS |
919 | linepos += 2; |
920 | } else if (linepos[0] == '+' && linepos[1] == '=') { | |
b0f7409f | 921 | *op = OP_ADD; |
6880b25d KS |
922 | linepos += 2; |
923 | } else if (linepos[0] == '=') { | |
b0f7409f | 924 | *op = OP_ASSIGN; |
6880b25d KS |
925 | linepos++; |
926 | } else if (linepos[0] == ':' && linepos[1] == '=') { | |
b0f7409f | 927 | *op = OP_ASSIGN_FINAL; |
6880b25d KS |
928 | linepos += 2; |
929 | } else | |
930 | return -1; | |
d0c8cb7d | 931 | |
6880b25d KS |
932 | /* terminate key */ |
933 | temp[0] = '\0'; | |
95776dc6 | 934 | |
6880b25d KS |
935 | /* skip whitespace after operator */ |
936 | while (isspace(linepos[0])) | |
937 | linepos++; | |
938 | if (linepos[0] == '\0') | |
939 | return -1; | |
aa8734ff | 940 | |
ac218d9c | 941 | /* get the value */ |
6880b25d KS |
942 | if (linepos[0] == '"') |
943 | linepos++; | |
944 | else | |
945 | return -1; | |
946 | *value = linepos; | |
aa8734ff | 947 | |
ac218d9c | 948 | /* terminate */ |
6880b25d KS |
949 | temp = strchr(linepos, '"'); |
950 | if (!temp) | |
951 | return -1; | |
952 | temp[0] = '\0'; | |
953 | temp++; | |
5d6a1fa6 | 954 | dbg(udev, "%s '%s'-'%s'\n", operation_str(*op), *key, *value); |
aa8734ff | 955 | |
6880b25d KS |
956 | /* move line to next key */ |
957 | *line = temp; | |
958 | return 0; | |
959 | } | |
95776dc6 | 960 | |
6880b25d KS |
961 | /* extract possible KEY{attr} */ |
962 | static char *get_key_attribute(struct udev *udev, char *str) | |
963 | { | |
964 | char *pos; | |
965 | char *attr; | |
95776dc6 | 966 | |
6880b25d KS |
967 | attr = strchr(str, '{'); |
968 | if (attr != NULL) { | |
969 | attr++; | |
970 | pos = strchr(attr, '}'); | |
971 | if (pos == NULL) { | |
972 | err(udev, "missing closing brace for format\n"); | |
973 | return NULL; | |
95776dc6 | 974 | } |
6880b25d KS |
975 | pos[0] = '\0'; |
976 | dbg(udev, "attribute='%s'\n", attr); | |
977 | return attr; | |
95776dc6 | 978 | } |
6880b25d KS |
979 | return NULL; |
980 | } | |
95776dc6 | 981 | |
0ef254d5 KS |
982 | static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, |
983 | enum operation_type op, | |
984 | const char *value, const void *data) | |
6880b25d KS |
985 | { |
986 | struct token *token = &rule_tmp->token[rule_tmp->token_cur]; | |
32028733 | 987 | const char *attr = NULL; |
ac218d9c KS |
988 | |
989 | memset(token, 0x00, sizeof(struct token)); | |
6880b25d KS |
990 | |
991 | switch (type) { | |
6880b25d KS |
992 | case TK_M_ACTION: |
993 | case TK_M_DEVPATH: | |
994 | case TK_M_KERNEL: | |
995 | case TK_M_SUBSYSTEM: | |
996 | case TK_M_DRIVER: | |
7aeeaedc | 997 | case TK_M_WAITFOR: |
6880b25d KS |
998 | case TK_M_DEVLINK: |
999 | case TK_M_NAME: | |
1000 | case TK_M_KERNELS: | |
1001 | case TK_M_SUBSYSTEMS: | |
1002 | case TK_M_DRIVERS: | |
1003 | case TK_M_PROGRAM: | |
1004 | case TK_M_IMPORT_FILE: | |
1005 | case TK_M_IMPORT_PROG: | |
5539f624 | 1006 | case TK_M_IMPORT_DB: |
6880b25d KS |
1007 | case TK_M_IMPORT_PARENT: |
1008 | case TK_M_RESULT: | |
1009 | case TK_A_OWNER: | |
1010 | case TK_A_GROUP: | |
1011 | case TK_A_MODE: | |
1012 | case TK_A_NAME: | |
6880b25d | 1013 | case TK_A_GOTO: |
3f896a2a | 1014 | case TK_M_TAG: |
28460195 | 1015 | case TK_A_TAG: |
6880b25d | 1016 | token->key.value_off = add_string(rule_tmp->rules, value); |
e57e7bc1 | 1017 | break; |
6880b25d KS |
1018 | case TK_M_ENV: |
1019 | case TK_M_ATTR: | |
1020 | case TK_M_ATTRS: | |
1021 | case TK_A_ATTR: | |
1022 | case TK_A_ENV: | |
32028733 | 1023 | attr = data; |
6880b25d KS |
1024 | token->key.value_off = add_string(rule_tmp->rules, value); |
1025 | token->key.attr_off = add_string(rule_tmp->rules, attr); | |
1026 | break; | |
6c29f2b9 KS |
1027 | case TK_A_DEVLINK: |
1028 | token->key.value_off = add_string(rule_tmp->rules, value); | |
1029 | token->key.devlink_unique = *(int *)data; | |
1030 | break; | |
6880b25d | 1031 | case TK_M_TEST: |
6880b25d | 1032 | token->key.value_off = add_string(rule_tmp->rules, value); |
ac218d9c KS |
1033 | if (data != NULL) |
1034 | token->key.mode = *(mode_t *)data; | |
6880b25d | 1035 | break; |
6880b25d KS |
1036 | case TK_A_STRING_ESCAPE_NONE: |
1037 | case TK_A_STRING_ESCAPE_REPLACE: | |
5d69a34c | 1038 | break; |
6880b25d KS |
1039 | case TK_A_RUN: |
1040 | token->key.value_off = add_string(rule_tmp->rules, value); | |
6c29f2b9 | 1041 | token->key.fail_on_error = *(int *)data; |
6880b25d | 1042 | break; |
46f194cb | 1043 | case TK_A_INOTIFY_WATCH: |
6880b25d KS |
1044 | case TK_A_DEVLINK_PRIO: |
1045 | token->key.devlink_prio = *(int *)data; | |
1046 | break; | |
1047 | case TK_A_OWNER_ID: | |
1048 | token->key.uid = *(uid_t *)data; | |
1049 | break; | |
1050 | case TK_A_GROUP_ID: | |
1051 | token->key.gid = *(gid_t *)data; | |
1052 | break; | |
1053 | case TK_A_MODE_ID: | |
1054 | token->key.mode = *(mode_t *)data; | |
1055 | break; | |
1056 | case TK_A_EVENT_TIMEOUT: | |
1057 | token->key.event_timeout = *(int *)data; | |
1058 | break; | |
1059 | case TK_RULE: | |
d15fcce4 | 1060 | case TK_M_PARENTS_MIN: |
b0f7409f | 1061 | case TK_M_PARENTS_MAX: |
ac218d9c | 1062 | case TK_M_MAX: |
6880b25d | 1063 | case TK_END: |
ac218d9c | 1064 | case TK_UNSET: |
6880b25d KS |
1065 | err(rule_tmp->rules->udev, "wrong type %u\n", type); |
1066 | return -1; | |
724257d9 | 1067 | } |
ac218d9c | 1068 | |
fb045134 KS |
1069 | if (value != NULL && type < TK_M_MAX) { |
1070 | /* check if we need to split or call fnmatch() while matching rules */ | |
32028733 | 1071 | enum string_glob_type glob; |
fb045134 KS |
1072 | int has_split; |
1073 | int has_glob; | |
1074 | ||
1075 | has_split = (strchr(value, '|') != NULL); | |
32028733 | 1076 | has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL); |
fb045134 KS |
1077 | if (has_split && has_glob) { |
1078 | glob = GL_SPLIT_GLOB; | |
1079 | } else if (has_split) { | |
1080 | glob = GL_SPLIT; | |
1081 | } else if (has_glob) { | |
1082 | if (strcmp(value, "?*") == 0) | |
1083 | glob = GL_SOMETHING; | |
1084 | else | |
1085 | glob = GL_GLOB; | |
32028733 KS |
1086 | } else { |
1087 | glob = GL_PLAIN; | |
ac218d9c | 1088 | } |
32028733 KS |
1089 | token->key.glob = glob; |
1090 | } | |
1091 | ||
1092 | if (value != NULL && type > TK_M_MAX) { | |
1093 | /* check if assigned value has substitution chars */ | |
1094 | if (value[0] == '[') | |
1095 | token->key.subst = SB_SUBSYS; | |
1096 | else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL) | |
1097 | token->key.subst = SB_FORMAT; | |
1098 | else | |
1099 | token->key.subst = SB_NONE; | |
1100 | } | |
1101 | ||
1102 | if (attr != NULL) { | |
1103 | /* check if property/attribut name has substitution chars */ | |
1104 | if (attr[0] == '[') | |
1105 | token->key.attrsubst = SB_SUBSYS; | |
1106 | else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL) | |
1107 | token->key.attrsubst = SB_FORMAT; | |
1108 | else | |
1109 | token->key.attrsubst = SB_NONE; | |
ac218d9c KS |
1110 | } |
1111 | ||
0ef254d5 | 1112 | token->key.type = type; |
6880b25d KS |
1113 | token->key.op = op; |
1114 | rule_tmp->token_cur++; | |
1115 | if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) { | |
1116 | err(rule_tmp->rules->udev, "temporary rule array too small\n"); | |
1117 | return -1; | |
e57e7bc1 | 1118 | } |
6880b25d KS |
1119 | return 0; |
1120 | } | |
e57e7bc1 | 1121 | |
6880b25d KS |
1122 | static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) |
1123 | { | |
1124 | unsigned int i; | |
1125 | unsigned int start = 0; | |
1126 | unsigned int end = rule_tmp->token_cur; | |
1127 | ||
1128 | for (i = 0; i < rule_tmp->token_cur; i++) { | |
ac218d9c | 1129 | enum token_type next_val = TK_UNSET; |
78715f65 | 1130 | unsigned int next_idx = 0; |
6880b25d KS |
1131 | unsigned int j; |
1132 | ||
1133 | /* find smallest value */ | |
1134 | for (j = start; j < end; j++) { | |
ac218d9c | 1135 | if (rule_tmp->token[j].type == TK_UNSET) |
6880b25d | 1136 | continue; |
ac218d9c | 1137 | if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) { |
6880b25d KS |
1138 | next_val = rule_tmp->token[j].type; |
1139 | next_idx = j; | |
db6e59df | 1140 | } |
5618b561 | 1141 | } |
5618b561 | 1142 | |
6880b25d KS |
1143 | /* add token and mark done */ |
1144 | if (add_token(rules, &rule_tmp->token[next_idx]) != 0) | |
1145 | return -1; | |
ac218d9c | 1146 | rule_tmp->token[next_idx].type = TK_UNSET; |
956cf793 | 1147 | |
6880b25d KS |
1148 | /* shrink range */ |
1149 | if (next_idx == start) | |
1150 | start++; | |
1151 | if (next_idx+1 == end) | |
1152 | end--; | |
d0c8cb7d | 1153 | } |
e57e7bc1 | 1154 | return 0; |
724257d9 GKH |
1155 | } |
1156 | ||
6880b25d KS |
1157 | static int add_rule(struct udev_rules *rules, char *line, |
1158 | const char *filename, unsigned int filename_off, unsigned int lineno) | |
724257d9 | 1159 | { |
6880b25d KS |
1160 | char *linepos; |
1161 | char *attr; | |
6880b25d | 1162 | struct rule_tmp rule_tmp; |
6d87ee2e KS |
1163 | bool bus_warn = false; |
1164 | bool sysfs_warn = false; | |
1165 | bool id_warn = false; | |
724257d9 | 1166 | |
6880b25d KS |
1167 | memset(&rule_tmp, 0x00, sizeof(struct rule_tmp)); |
1168 | rule_tmp.rules = rules; | |
1169 | rule_tmp.rule.type = TK_RULE; | |
1170 | rule_tmp.rule.rule.filename_off = filename_off; | |
0ef254d5 | 1171 | rule_tmp.rule.rule.filename_line = lineno; |
724257d9 | 1172 | |
6880b25d | 1173 | linepos = line; |
6bf0ffe8 | 1174 | while (1) { |
6880b25d KS |
1175 | char *key; |
1176 | char *value; | |
ac218d9c | 1177 | enum operation_type op; |
6880b25d KS |
1178 | |
1179 | if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) | |
6bf0ffe8 KS |
1180 | break; |
1181 | ||
bd75fddb | 1182 | if (strcmp(key, "ACTION") == 0) { |
b0f7409f | 1183 | if (op > OP_MATCH_MAX) { |
6880b25d KS |
1184 | err(rules->udev, "invalid ACTION operation\n"); |
1185 | goto invalid; | |
1186 | } | |
0ef254d5 | 1187 | rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL); |
bf5d2964 KS |
1188 | continue; |
1189 | } | |
1190 | ||
bd75fddb | 1191 | if (strcmp(key, "DEVPATH") == 0) { |
b0f7409f | 1192 | if (op > OP_MATCH_MAX) { |
6880b25d KS |
1193 | err(rules->udev, "invalid DEVPATH operation\n"); |
1194 | goto invalid; | |
fd9efc00 | 1195 | } |
0ef254d5 | 1196 | rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL); |
6880b25d KS |
1197 | continue; |
1198 | } | |
c7521974 | 1199 | |
bd75fddb | 1200 | if (strcmp(key, "KERNEL") == 0) { |
b0f7409f | 1201 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1202 | err(rules->udev, "invalid KERNEL operation\n"); |
1203 | goto invalid; | |
1204 | } | |
0ef254d5 | 1205 | rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL); |
c7521974 KS |
1206 | continue; |
1207 | } | |
1208 | ||
bd75fddb | 1209 | if (strcmp(key, "SUBSYSTEM") == 0) { |
b0f7409f | 1210 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1211 | err(rules->udev, "invalid SUBSYSTEM operation\n"); |
1212 | goto invalid; | |
1213 | } | |
1214 | /* bus, class, subsystem events should all be the same */ | |
1215 | if (strcmp(value, "subsystem") == 0 || | |
1216 | strcmp(value, "bus") == 0 || | |
1217 | strcmp(value, "class") == 0) { | |
1218 | if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0) | |
1219 | err(rules->udev, "'%s' must be specified as 'subsystem' \n" | |
1220 | "please fix it in %s:%u", value, filename, lineno); | |
0ef254d5 | 1221 | rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL); |
c7521974 | 1222 | } else |
0ef254d5 | 1223 | rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL); |
c7521974 KS |
1224 | continue; |
1225 | } | |
1226 | ||
bd75fddb | 1227 | if (strcmp(key, "DRIVER") == 0) { |
b0f7409f | 1228 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1229 | err(rules->udev, "invalid DRIVER operation\n"); |
1230 | goto invalid; | |
1231 | } | |
0ef254d5 | 1232 | rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL); |
c7521974 KS |
1233 | continue; |
1234 | } | |
1235 | ||
bd75fddb | 1236 | if (strncmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) { |
6880b25d | 1237 | attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1); |
c7521974 KS |
1238 | if (attr == NULL) { |
1239 | err(rules->udev, "error parsing ATTR attribute\n"); | |
1240 | goto invalid; | |
1241 | } | |
b0f7409f | 1242 | if (op < OP_MATCH_MAX) { |
0ef254d5 | 1243 | rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr); |
6880b25d | 1244 | } else { |
0ef254d5 | 1245 | rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr); |
6880b25d | 1246 | } |
c7521974 KS |
1247 | continue; |
1248 | } | |
1249 | ||
6d87ee2e | 1250 | if (strcmp(key, "KERNELS") == 0) { |
b0f7409f | 1251 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1252 | err(rules->udev, "invalid KERNELS operation\n"); |
1253 | goto invalid; | |
1254 | } | |
0ef254d5 | 1255 | rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL); |
c7521974 KS |
1256 | continue; |
1257 | } | |
1258 | ||
6d87ee2e KS |
1259 | if (strcmp(key, "ID") == 0) { |
1260 | if (!id_warn) { | |
1261 | id_warn = true; | |
1262 | err(rules->udev, "ID= will be removed in a future udev version, " | |
1263 | "please use KERNEL= to match the event device, or KERNELS= " | |
1264 | "to match a parent device, in %s:%u\n", filename, lineno); | |
1265 | } | |
1266 | if (op > OP_MATCH_MAX) { | |
1267 | err(rules->udev, "invalid KERNELS operation\n"); | |
1268 | goto invalid; | |
1269 | } | |
1270 | rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL); | |
1271 | continue; | |
1272 | } | |
1273 | ||
1274 | if (strcmp(key, "SUBSYSTEMS") == 0) { | |
1275 | if (op > OP_MATCH_MAX) { | |
1276 | err(rules->udev, "invalid SUBSYSTEMS operation\n"); | |
1277 | goto invalid; | |
1278 | } | |
1279 | rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL); | |
1280 | continue; | |
1281 | } | |
1282 | ||
1283 | if (strcmp(key, "BUS") == 0) { | |
1284 | if (!bus_warn) { | |
1285 | bus_warn = true; | |
1286 | err(rules->udev, "BUS= will be removed in a future udev version, " | |
1287 | "please use SUBSYSTEM= to match the event device, or SUBSYSTEMS= " | |
1288 | "to match a parent device, in %s:%u\n", filename, lineno); | |
1289 | } | |
b0f7409f | 1290 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1291 | err(rules->udev, "invalid SUBSYSTEMS operation\n"); |
1292 | goto invalid; | |
1293 | } | |
0ef254d5 | 1294 | rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL); |
c7521974 KS |
1295 | continue; |
1296 | } | |
1297 | ||
bd75fddb | 1298 | if (strcmp(key, "DRIVERS") == 0) { |
b0f7409f | 1299 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1300 | err(rules->udev, "invalid DRIVERS operation\n"); |
1301 | goto invalid; | |
1302 | } | |
0ef254d5 | 1303 | rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL); |
c7521974 KS |
1304 | continue; |
1305 | } | |
1306 | ||
6d87ee2e | 1307 | if (strncmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0) { |
b0f7409f | 1308 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1309 | err(rules->udev, "invalid ATTRS operation\n"); |
1310 | goto invalid; | |
1311 | } | |
6880b25d | 1312 | attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1); |
c7521974 KS |
1313 | if (attr == NULL) { |
1314 | err(rules->udev, "error parsing ATTRS attribute\n"); | |
1315 | goto invalid; | |
1316 | } | |
1317 | if (strncmp(attr, "device/", 7) == 0) | |
1318 | err(rules->udev, "the 'device' link may not be available in a future kernel, " | |
1319 | "please fix it in %s:%u", filename, lineno); | |
1320 | else if (strstr(attr, "../") != NULL) | |
1321 | err(rules->udev, "do not reference parent sysfs directories directly, " | |
1322 | "it may break with a future kernel, please fix it in %s:%u", filename, lineno); | |
0ef254d5 | 1323 | rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr); |
c7521974 KS |
1324 | continue; |
1325 | } | |
1326 | ||
6d87ee2e KS |
1327 | if (strncmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) { |
1328 | if (!sysfs_warn) { | |
1329 | sysfs_warn = true; | |
1330 | err(rules->udev, "SYSFS{}= will be removed in a future udev version, " | |
1331 | "please use ATTR{}= to match the event device, or ATTRS{}= " | |
1332 | "to match a parent device, in %s:%u\n", filename, lineno); | |
1333 | } | |
1334 | if (op > OP_MATCH_MAX) { | |
1335 | err(rules->udev, "invalid ATTRS operation\n"); | |
1336 | goto invalid; | |
1337 | } | |
1338 | attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1); | |
1339 | if (attr == NULL) { | |
1340 | err(rules->udev, "error parsing ATTRS attribute\n"); | |
1341 | goto invalid; | |
1342 | } | |
1343 | rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr); | |
1344 | continue; | |
1345 | } | |
1346 | ||
bd75fddb | 1347 | if (strncmp(key, "ENV{", sizeof("ENV{")-1) == 0) { |
6880b25d | 1348 | attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1); |
c7521974 KS |
1349 | if (attr == NULL) { |
1350 | err(rules->udev, "error parsing ENV attribute\n"); | |
1351 | goto invalid; | |
1352 | } | |
b0f7409f | 1353 | if (op < OP_MATCH_MAX) { |
0ef254d5 | 1354 | if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0) |
6880b25d KS |
1355 | goto invalid; |
1356 | } else { | |
0ef254d5 | 1357 | if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0) |
6880b25d KS |
1358 | goto invalid; |
1359 | } | |
c7521974 KS |
1360 | continue; |
1361 | } | |
1362 | ||
28460195 | 1363 | if (strcmp(key, "TAG") == 0) { |
3f896a2a KS |
1364 | if (op < OP_MATCH_MAX) |
1365 | rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL); | |
1366 | else | |
1367 | rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL); | |
28460195 KS |
1368 | continue; |
1369 | } | |
1370 | ||
bd75fddb | 1371 | if (strcmp(key, "PROGRAM") == 0) { |
0ef254d5 | 1372 | rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL); |
c7521974 KS |
1373 | continue; |
1374 | } | |
1375 | ||
bd75fddb | 1376 | if (strcmp(key, "RESULT") == 0) { |
b0f7409f | 1377 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1378 | err(rules->udev, "invalid RESULT operation\n"); |
1379 | goto invalid; | |
1380 | } | |
0ef254d5 | 1381 | rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL); |
c7521974 KS |
1382 | continue; |
1383 | } | |
1384 | ||
bd75fddb | 1385 | if (strncmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) { |
6880b25d | 1386 | attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1); |
c7521974 KS |
1387 | if (attr != NULL && strstr(attr, "program")) { |
1388 | dbg(rules->udev, "IMPORT will be executed\n"); | |
0ef254d5 | 1389 | rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL); |
c7521974 KS |
1390 | } else if (attr != NULL && strstr(attr, "file")) { |
1391 | dbg(rules->udev, "IMPORT will be included as file\n"); | |
0ef254d5 | 1392 | rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL); |
5539f624 KS |
1393 | } else if (attr != NULL && strstr(attr, "db")) { |
1394 | dbg(rules->udev, "IMPORT will include db values\n"); | |
1395 | rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL); | |
c7521974 KS |
1396 | } else if (attr != NULL && strstr(attr, "parent")) { |
1397 | dbg(rules->udev, "IMPORT will include the parent values\n"); | |
0ef254d5 | 1398 | rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL); |
c7521974 KS |
1399 | } else { |
1400 | /* figure it out if it is executable */ | |
1401 | char file[UTIL_PATH_SIZE]; | |
1402 | char *pos; | |
1403 | struct stat statbuf; | |
1404 | ||
065db052 KS |
1405 | /* allow programs in /lib/udev called without the path */ |
1406 | if (value[0] != '/') | |
6133f343 | 1407 | util_strscpyl(file, sizeof(file), LIBEXECDIR "/", value, NULL); |
065db052 KS |
1408 | else |
1409 | util_strscpy(file, sizeof(file), value); | |
c7521974 KS |
1410 | pos = strchr(file, ' '); |
1411 | if (pos) | |
1412 | pos[0] = '\0'; | |
c7521974 | 1413 | dbg(rules->udev, "IMPORT auto mode for '%s'\n", file); |
065db052 | 1414 | if (stat(file, &statbuf) == 0 && (statbuf.st_mode & S_IXUSR)) { |
6880b25d | 1415 | dbg(rules->udev, "IMPORT will be executed (autotype)\n"); |
0ef254d5 | 1416 | rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL); |
c7521974 | 1417 | } else { |
6880b25d | 1418 | dbg(rules->udev, "IMPORT will be included as file (autotype)\n"); |
0ef254d5 | 1419 | rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL); |
c7521974 KS |
1420 | } |
1421 | } | |
c7521974 KS |
1422 | continue; |
1423 | } | |
1424 | ||
bd75fddb | 1425 | if (strncmp(key, "TEST", sizeof("TEST")-1) == 0) { |
6880b25d KS |
1426 | mode_t mode = 0; |
1427 | ||
b0f7409f | 1428 | if (op > OP_MATCH_MAX) { |
c7521974 KS |
1429 | err(rules->udev, "invalid TEST operation\n"); |
1430 | goto invalid; | |
1431 | } | |
6880b25d KS |
1432 | attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1); |
1433 | if (attr != NULL) { | |
1434 | mode = strtol(attr, NULL, 8); | |
0ef254d5 | 1435 | rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode); |
6880b25d | 1436 | } else { |
0ef254d5 | 1437 | rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL); |
6880b25d | 1438 | } |
c7521974 KS |
1439 | continue; |
1440 | } | |
1441 | ||
bd75fddb | 1442 | if (strncmp(key, "RUN", sizeof("RUN")-1) == 0) { |
6880b25d KS |
1443 | int flag = 0; |
1444 | ||
1445 | attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1); | |
16dd0aa9 | 1446 | if (attr != NULL && strstr(attr, "fail_event_on_error")) |
6880b25d | 1447 | flag = 1; |
0ef254d5 | 1448 | rule_add_key(&rule_tmp, TK_A_RUN, op, value, &flag); |
c7521974 KS |
1449 | continue; |
1450 | } | |
1451 | ||
bd75fddb | 1452 | if (strcmp(key, "WAIT_FOR") == 0 || strcmp(key, "WAIT_FOR_SYSFS") == 0) { |
0ef254d5 | 1453 | rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL); |
c7521974 KS |
1454 | continue; |
1455 | } | |
1456 | ||
bd75fddb | 1457 | if (strcmp(key, "LABEL") == 0) { |
6880b25d | 1458 | rule_tmp.rule.rule.label_off = add_string(rules, value); |
c7521974 KS |
1459 | continue; |
1460 | } | |
1461 | ||
bd75fddb | 1462 | if (strcmp(key, "GOTO") == 0) { |
0ef254d5 | 1463 | rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL); |
c7521974 KS |
1464 | continue; |
1465 | } | |
1466 | ||
bd75fddb | 1467 | if (strncmp(key, "NAME", sizeof("NAME")-1) == 0) { |
b0f7409f | 1468 | if (op < OP_MATCH_MAX) { |
0ef254d5 | 1469 | rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL); |
6880b25d | 1470 | } else { |
75cb1ac5 | 1471 | if (strcmp(value, "%k") == 0) { |
d0d7b8d0 | 1472 | err(rules->udev, "NAME=\"%%k\" is ignored, because it breaks kernel supplied names, " |
75cb1ac5 KS |
1473 | "please remove it from %s:%u\n", filename, lineno); |
1474 | continue; | |
1475 | } | |
d0d7b8d0 KS |
1476 | if (value[0] == '\0') { |
1477 | info(rules->udev, "NAME=\"\" is ignored, because udev will not delete any device nodes, " | |
1478 | "please remove it from %s:%u\n", filename, lineno); | |
75cb1ac5 | 1479 | continue; |
d0d7b8d0 | 1480 | } |
0ef254d5 | 1481 | rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL); |
c7521974 | 1482 | } |
00f98bd2 | 1483 | rule_tmp.rule.rule.flags = 1; |
c7521974 KS |
1484 | continue; |
1485 | } | |
1486 | ||
bcb8b231 | 1487 | if (strncmp(key, "SYMLINK", sizeof("SYMLINK")-1) == 0) { |
6c29f2b9 | 1488 | if (op < OP_MATCH_MAX) { |
0ef254d5 | 1489 | rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL); |
6c29f2b9 KS |
1490 | } else { |
1491 | int flag = 0; | |
1492 | ||
1493 | attr = get_key_attribute(rules->udev, key + sizeof("SYMLINK")-1); | |
1494 | if (attr != NULL && strstr(attr, "unique") != NULL) | |
1495 | flag = 1; | |
1496 | rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, &flag); | |
1497 | } | |
00f98bd2 | 1498 | rule_tmp.rule.rule.flags = 1; |
b0f7409f KS |
1499 | continue; |
1500 | } | |
c7521974 | 1501 | |
bd75fddb | 1502 | if (strcmp(key, "OWNER") == 0) { |
6880b25d KS |
1503 | uid_t uid; |
1504 | char *endptr; | |
1505 | ||
1506 | uid = strtoul(value, &endptr, 10); | |
1507 | if (endptr[0] == '\0') { | |
0ef254d5 | 1508 | rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid); |
5f03ed8a | 1509 | } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) { |
154a7b84 | 1510 | uid = add_uid(rules, value); |
0ef254d5 | 1511 | rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid); |
055e40ed | 1512 | } else if (rules->resolve_names >= 0) { |
0ef254d5 | 1513 | rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL); |
c7521974 | 1514 | } |
00f98bd2 | 1515 | rule_tmp.rule.rule.flags = 1; |
c7521974 KS |
1516 | continue; |
1517 | } | |
1518 | ||
bd75fddb | 1519 | if (strcmp(key, "GROUP") == 0) { |
6880b25d KS |
1520 | gid_t gid; |
1521 | char *endptr; | |
1522 | ||
1523 | gid = strtoul(value, &endptr, 10); | |
1524 | if (endptr[0] == '\0') { | |
0ef254d5 | 1525 | rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid); |
5f03ed8a | 1526 | } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) { |
154a7b84 | 1527 | gid = add_gid(rules, value); |
0ef254d5 | 1528 | rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid); |
055e40ed | 1529 | } else if (rules->resolve_names >= 0) { |
0ef254d5 | 1530 | rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL); |
c7521974 | 1531 | } |
00f98bd2 | 1532 | rule_tmp.rule.rule.flags = 1; |
c7521974 KS |
1533 | continue; |
1534 | } | |
1535 | ||
bd75fddb | 1536 | if (strcmp(key, "MODE") == 0) { |
6880b25d KS |
1537 | mode_t mode; |
1538 | char *endptr; | |
1539 | ||
1540 | mode = strtol(value, &endptr, 8); | |
1541 | if (endptr[0] == '\0') | |
0ef254d5 | 1542 | rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode); |
6880b25d | 1543 | else |
0ef254d5 | 1544 | rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL); |
00f98bd2 | 1545 | rule_tmp.rule.rule.flags = 1; |
c7521974 KS |
1546 | continue; |
1547 | } | |
1548 | ||
bd75fddb | 1549 | if (strcmp(key, "OPTIONS") == 0) { |
c7521974 KS |
1550 | const char *pos; |
1551 | ||
c7521974 KS |
1552 | pos = strstr(value, "link_priority="); |
1553 | if (pos != NULL) { | |
6880b25d KS |
1554 | int prio = atoi(&pos[strlen("link_priority=")]); |
1555 | ||
0ef254d5 | 1556 | rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, 0, NULL, &prio); |
6880b25d | 1557 | dbg(rules->udev, "link priority=%i\n", prio); |
c7521974 KS |
1558 | } |
1559 | pos = strstr(value, "event_timeout="); | |
1560 | if (pos != NULL) { | |
6880b25d KS |
1561 | int tout = atoi(&pos[strlen("event_timeout=")]); |
1562 | ||
0ef254d5 | 1563 | rule_add_key(&rule_tmp, TK_A_EVENT_TIMEOUT, 0, NULL, &tout); |
214a6c79 | 1564 | dbg(rules->udev, "event timeout=%i\n", tout); |
c7521974 KS |
1565 | } |
1566 | pos = strstr(value, "string_escape="); | |
1567 | if (pos != NULL) { | |
1568 | pos = &pos[strlen("string_escape=")]; | |
1569 | if (strncmp(pos, "none", strlen("none")) == 0) | |
0ef254d5 | 1570 | rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, 0, NULL, NULL); |
c7521974 | 1571 | else if (strncmp(pos, "replace", strlen("replace")) == 0) |
0ef254d5 | 1572 | rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, 0, NULL, NULL); |
c7521974 | 1573 | } |
46f194cb | 1574 | pos = strstr(value, "nowatch"); |
bd284db1 | 1575 | if (pos != NULL) { |
46f194cb KS |
1576 | const int off = 0; |
1577 | ||
1578 | rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, 0, NULL, &off); | |
1579 | dbg(rules->udev, "inotify watch of device disabled\n"); | |
1580 | } else { | |
1581 | pos = strstr(value, "watch"); | |
1582 | if (pos != NULL) { | |
1583 | const int on = 1; | |
1584 | ||
1585 | rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, 0, NULL, &on); | |
1586 | dbg(rules->udev, "inotify watch of device requested\n"); | |
1587 | } | |
bd284db1 | 1588 | } |
c7521974 KS |
1589 | continue; |
1590 | } | |
c7521974 KS |
1591 | err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno); |
1592 | } | |
1593 | ||
6880b25d | 1594 | /* add rule token */ |
3f3aa9f5 | 1595 | rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur; |
6880b25d KS |
1596 | if (add_token(rules, &rule_tmp.rule) != 0) |
1597 | goto invalid; | |
c7521974 | 1598 | |
6880b25d KS |
1599 | /* add tokens to list, sorted by type */ |
1600 | if (sort_token(rules, &rule_tmp) != 0) | |
1601 | goto invalid; | |
3f3aa9f5 | 1602 | |
6880b25d | 1603 | return 0; |
c7521974 KS |
1604 | invalid: |
1605 | err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno); | |
1606 | return -1; | |
1607 | } | |
1608 | ||
0ef254d5 | 1609 | static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off) |
c7521974 KS |
1610 | { |
1611 | FILE *f; | |
77f89e93 | 1612 | unsigned int first_token; |
c7521974 | 1613 | char line[UTIL_LINE_SIZE]; |
6880b25d KS |
1614 | int line_nr = 0; |
1615 | unsigned int i; | |
c7521974 | 1616 | |
c7521974 KS |
1617 | info(rules->udev, "reading '%s' as rules file\n", filename); |
1618 | ||
1619 | f = fopen(filename, "r"); | |
1620 | if (f == NULL) | |
1621 | return -1; | |
77f89e93 KS |
1622 | |
1623 | first_token = rules->token_cur; | |
1624 | ||
335e316a | 1625 | while (fgets(line, sizeof(line), f) != NULL) { |
c7521974 KS |
1626 | char *key; |
1627 | size_t len; | |
1628 | ||
1629 | /* skip whitespace */ | |
1630 | line_nr++; | |
1631 | key = line; | |
1632 | while (isspace(key[0])) | |
1633 | key++; | |
1634 | ||
1635 | /* comment */ | |
1636 | if (key[0] == '#') | |
1637 | continue; | |
1638 | ||
1639 | len = strlen(line); | |
1640 | if (len < 3) | |
1641 | continue; | |
1642 | ||
1643 | /* continue reading if backslash+newline is found */ | |
1644 | while (line[len-2] == '\\') { | |
1645 | if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL) | |
1646 | break; | |
6d691b2b FZ |
1647 | if (strlen(&line[len-2]) < 2) |
1648 | break; | |
c7521974 KS |
1649 | line_nr++; |
1650 | len = strlen(line); | |
1651 | } | |
1652 | ||
1653 | if (len+1 >= sizeof(line)) { | |
1654 | err(rules->udev, "line too long '%s':%u, ignored\n", filename, line_nr); | |
1655 | continue; | |
1656 | } | |
6880b25d | 1657 | add_rule(rules, key, filename, filename_off, line_nr); |
c7521974 KS |
1658 | } |
1659 | fclose(f); | |
1660 | ||
6880b25d | 1661 | /* link GOTOs to LABEL rules in this file to be able to fast-forward */ |
77f89e93 | 1662 | for (i = first_token+1; i < rules->token_cur; i++) { |
6880b25d KS |
1663 | if (rules->tokens[i].type == TK_A_GOTO) { |
1664 | char *label = &rules->buf[rules->tokens[i].key.value_off]; | |
1665 | unsigned int j; | |
1666 | ||
1667 | for (j = i+1; j < rules->token_cur; j++) { | |
1668 | if (rules->tokens[j].type != TK_RULE) | |
1669 | continue; | |
1670 | if (rules->tokens[j].rule.label_off == 0) | |
1671 | continue; | |
1672 | if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0) | |
1673 | continue; | |
1674 | rules->tokens[i].key.rule_goto = j; | |
0c377989 | 1675 | break; |
c7521974 | 1676 | } |
6880b25d KS |
1677 | if (rules->tokens[i].key.rule_goto == 0) |
1678 | err(rules->udev, "GOTO '%s' has no matching label in: '%s'\n", label, filename); | |
c7521974 KS |
1679 | } |
1680 | } | |
1681 | return 0; | |
1682 | } | |
1683 | ||
1684 | static int add_matching_files(struct udev *udev, struct udev_list_node *file_list, const char *dirname, const char *suffix) | |
1685 | { | |
c7521974 KS |
1686 | DIR *dir; |
1687 | char filename[UTIL_PATH_SIZE]; | |
1688 | ||
1689 | dbg(udev, "open directory '%s'\n", dirname); | |
1690 | dir = opendir(dirname); | |
1691 | if (dir == NULL) { | |
92e52e2f | 1692 | info(udev, "unable to open '%s': %m\n", dirname); |
c7521974 KS |
1693 | return -1; |
1694 | } | |
1695 | ||
1696 | while (1) { | |
78230c0d KS |
1697 | struct dirent *dent; |
1698 | ||
1699 | dent = readdir(dir); | |
1700 | if (dent == NULL || dent->d_name[0] == '\0') | |
c7521974 KS |
1701 | break; |
1702 | ||
78230c0d | 1703 | if (dent->d_name[0] == '.') |
c7521974 KS |
1704 | continue; |
1705 | ||
1706 | /* look for file matching with specified suffix */ | |
1707 | if (suffix != NULL) { | |
1708 | const char *ext; | |
1709 | ||
78230c0d | 1710 | ext = strrchr(dent->d_name, '.'); |
c7521974 KS |
1711 | if (ext == NULL) |
1712 | continue; | |
1713 | if (strcmp(ext, suffix) != 0) | |
1714 | continue; | |
1715 | } | |
e6c1a2bd KS |
1716 | util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL); |
1717 | dbg(udev, "put file '%s' into list\n", filename); | |
c7521974 KS |
1718 | udev_list_entry_add(udev, file_list, filename, NULL, 1, 1); |
1719 | } | |
1720 | ||
1721 | closedir(dir); | |
1722 | return 0; | |
1723 | } | |
1724 | ||
d7ddce18 | 1725 | struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) |
c7521974 | 1726 | { |
d7ddce18 | 1727 | struct udev_rules *rules; |
c7521974 | 1728 | struct stat statbuf; |
c7521974 KS |
1729 | struct udev_list_node file_list; |
1730 | struct udev_list_entry *file_loop, *file_tmp; | |
6880b25d | 1731 | struct token end_token; |
c7521974 | 1732 | |
6880b25d | 1733 | rules = malloc(sizeof(struct udev_rules)); |
d7ddce18 | 1734 | if (rules == NULL) |
6880b25d KS |
1735 | return NULL; |
1736 | memset(rules, 0x00, sizeof(struct udev_rules)); | |
c7521974 KS |
1737 | rules->udev = udev; |
1738 | rules->resolve_names = resolve_names; | |
1739 | udev_list_init(&file_list); | |
1740 | ||
6880b25d KS |
1741 | /* init token array and string buffer */ |
1742 | rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token)); | |
34d6a259 KS |
1743 | if (rules->tokens == NULL) |
1744 | return NULL; | |
1745 | rules->token_max = PREALLOC_TOKEN; | |
bcf44d55 | 1746 | |
6880b25d | 1747 | rules->buf = malloc(PREALLOC_STRBUF); |
34d6a259 KS |
1748 | if (rules->buf == NULL) |
1749 | return NULL; | |
1750 | rules->buf_max = PREALLOC_STRBUF; | |
1751 | /* offset 0 is always '\0' */ | |
1752 | rules->buf[0] = '\0'; | |
1753 | rules->buf_cur = 1; | |
86b57788 KS |
1754 | dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n", |
1755 | rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max); | |
6880b25d | 1756 | |
bcf44d55 KS |
1757 | rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node)); |
1758 | if (rules->trie_nodes == NULL) | |
1759 | return NULL; | |
1760 | rules->trie_nodes_max = PREALLOC_TRIE; | |
949075db | 1761 | /* offset 0 is the trie root, with an empty string */ |
023ed7b0 | 1762 | memset(rules->trie_nodes, 0x00, sizeof(struct trie_node)); |
bcf44d55 KS |
1763 | rules->trie_nodes_cur = 1; |
1764 | ||
c7521974 KS |
1765 | if (udev_get_rules_path(udev) != NULL) { |
1766 | /* custom rules location for testing */ | |
1767 | add_matching_files(udev, &file_list, udev_get_rules_path(udev), ".rules"); | |
1768 | } else { | |
0ef254d5 | 1769 | char filename[PATH_MAX]; |
c7521974 KS |
1770 | struct udev_list_node sort_list; |
1771 | struct udev_list_entry *sort_loop, *sort_tmp; | |
1772 | ||
1773 | /* read user/custom rules */ | |
1774 | add_matching_files(udev, &file_list, SYSCONFDIR "/udev/rules.d", ".rules"); | |
1775 | ||
1776 | /* read dynamic/temporary rules */ | |
065db052 | 1777 | util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL); |
c7521974 KS |
1778 | udev_list_init(&sort_list); |
1779 | add_matching_files(udev, &sort_list, filename, ".rules"); | |
1780 | ||
1781 | /* read default rules */ | |
6133f343 | 1782 | add_matching_files(udev, &sort_list, LIBEXECDIR "/rules.d", ".rules"); |
c7521974 KS |
1783 | |
1784 | /* sort all rules files by basename into list of files */ | |
1785 | udev_list_entry_foreach_safe(sort_loop, sort_tmp, udev_list_get_entry(&sort_list)) { | |
1786 | const char *sort_name = udev_list_entry_get_name(sort_loop); | |
1787 | const char *sort_base = strrchr(sort_name, '/'); | |
1788 | ||
1789 | if (sort_base == NULL) | |
1790 | continue; | |
1e78dcbe | 1791 | /* sort entry into existing list */ |
c7521974 KS |
1792 | udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) { |
1793 | const char *file_name = udev_list_entry_get_name(file_loop); | |
1794 | const char *file_base = strrchr(file_name, '/'); | |
1795 | ||
1796 | if (file_base == NULL) | |
1797 | continue; | |
1798 | if (strcmp(file_base, sort_base) == 0) { | |
1799 | info(udev, "rule file basename '%s' already added, ignoring '%s'\n", | |
1800 | file_name, sort_name); | |
1e78dcbe | 1801 | udev_list_entry_delete(sort_loop); |
c7521974 KS |
1802 | sort_loop = NULL; |
1803 | break; | |
1804 | } | |
1e78dcbe KS |
1805 | if (strcmp(file_base, sort_base) > 0) { |
1806 | /* found later file, insert before */ | |
1807 | udev_list_entry_remove(sort_loop); | |
1808 | udev_list_entry_insert_before(sort_loop, file_loop); | |
1809 | sort_loop = NULL; | |
c7521974 | 1810 | break; |
1e78dcbe | 1811 | } |
c7521974 | 1812 | } |
1e78dcbe KS |
1813 | /* current file already handled */ |
1814 | if (sort_loop == NULL) | |
1815 | continue; | |
1816 | /* no later file, append to end of list */ | |
1817 | udev_list_entry_remove(sort_loop); | |
1818 | udev_list_entry_append(sort_loop, &file_list); | |
c7521974 KS |
1819 | } |
1820 | } | |
1821 | ||
0ef254d5 KS |
1822 | /* add all filenames to the string buffer */ |
1823 | udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) { | |
1824 | const char *filename = udev_list_entry_get_name(file_loop); | |
1825 | unsigned int filename_off; | |
1826 | ||
1827 | filename_off = add_string(rules, filename); | |
1828 | /* the offset in the rule is limited to unsigned short */ | |
1829 | if (filename_off < USHRT_MAX) | |
fbb31cd6 | 1830 | udev_list_entry_set_flags(file_loop, filename_off); |
0ef254d5 KS |
1831 | } |
1832 | ||
c7521974 KS |
1833 | /* parse list of files */ |
1834 | udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) { | |
0ef254d5 | 1835 | const char *filename = udev_list_entry_get_name(file_loop); |
fbb31cd6 | 1836 | unsigned int filename_off = udev_list_entry_get_flags(file_loop); |
c7521974 | 1837 | |
0ef254d5 KS |
1838 | if (stat(filename, &statbuf) == 0 && statbuf.st_size > 0) |
1839 | parse_file(rules, filename, filename_off); | |
c7521974 | 1840 | else |
24d1fea8 | 1841 | err(udev, "can not read '%s'\n", filename); |
1e78dcbe | 1842 | udev_list_entry_delete(file_loop); |
c7521974 | 1843 | } |
6880b25d KS |
1844 | |
1845 | memset(&end_token, 0x00, sizeof(struct token)); | |
1846 | end_token.type = TK_END; | |
1847 | add_token(rules, &end_token); | |
1848 | ||
154a7b84 | 1849 | /* shrink allocated token and string buffer */ |
6880b25d KS |
1850 | if (rules->token_cur < rules->token_max) { |
1851 | struct token *tokens; | |
1852 | ||
1853 | tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token)); | |
1854 | if (tokens != NULL || rules->token_cur == 0) { | |
1855 | rules->tokens = tokens; | |
1856 | rules->token_max = rules->token_cur; | |
1857 | } | |
1858 | } | |
1859 | if (rules->buf_cur < rules->buf_max) { | |
1860 | char *buf; | |
1861 | ||
1862 | buf = realloc(rules->buf, rules->buf_cur); | |
1863 | if (buf != NULL || rules->buf_cur == 0) { | |
1864 | rules->buf = buf; | |
1865 | rules->buf_max = rules->buf_cur; | |
1866 | } | |
1867 | } | |
0dd9f015 | 1868 | info(udev, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n", |
6880b25d | 1869 | rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max); |
023ed7b0 KS |
1870 | info(udev, "temporary index used %zu bytes (%u * %zu bytes)\n", |
1871 | rules->trie_nodes_cur * sizeof(struct trie_node), | |
1872 | rules->trie_nodes_cur, sizeof(struct trie_node)); | |
1449a55d AJ |
1873 | |
1874 | /* cleanup trie */ | |
bcf44d55 KS |
1875 | free(rules->trie_nodes); |
1876 | rules->trie_nodes = NULL; | |
1877 | rules->trie_nodes_cur = 0; | |
1878 | rules->trie_nodes_max = 0; | |
6880b25d | 1879 | |
154a7b84 KS |
1880 | /* cleanup uid/gid cache */ |
1881 | free(rules->uids); | |
1882 | rules->uids = NULL; | |
1883 | rules->uids_cur = 0; | |
1884 | rules->uids_max = 0; | |
1885 | free(rules->gids); | |
1886 | rules->gids = NULL; | |
1887 | rules->gids_cur = 0; | |
1888 | rules->gids_max = 0; | |
1889 | ||
6880b25d | 1890 | dump_rules(rules); |
d7ddce18 | 1891 | return rules; |
c7521974 KS |
1892 | } |
1893 | ||
d7ddce18 | 1894 | void udev_rules_unref(struct udev_rules *rules) |
c7521974 | 1895 | { |
d7ddce18 KS |
1896 | if (rules == NULL) |
1897 | return; | |
6880b25d KS |
1898 | free(rules->tokens); |
1899 | free(rules->buf); | |
bcf44d55 | 1900 | free(rules->trie_nodes); |
154a7b84 KS |
1901 | free(rules->uids); |
1902 | free(rules->gids); | |
d7ddce18 | 1903 | free(rules); |
c7521974 | 1904 | } |
6880b25d KS |
1905 | |
1906 | static int match_key(struct udev_rules *rules, struct token *token, const char *val) | |
1907 | { | |
6880b25d KS |
1908 | char *key_value = &rules->buf[token->key.value_off]; |
1909 | char *pos; | |
1910 | int match = 0; | |
1911 | ||
1912 | if (val == NULL) | |
1913 | val = ""; | |
1914 | ||
ac218d9c KS |
1915 | switch (token->key.glob) { |
1916 | case GL_PLAIN: | |
1917 | match = (strcmp(key_value, val) == 0); | |
1918 | break; | |
1919 | case GL_GLOB: | |
1920 | match = (fnmatch(key_value, val, 0) == 0); | |
1921 | break; | |
1922 | case GL_SPLIT: | |
1923 | { | |
91a75e4a KS |
1924 | const char *split; |
1925 | size_t len; | |
6880b25d | 1926 | |
91a75e4a KS |
1927 | split = &rules->buf[token->key.value_off]; |
1928 | len = strlen(val); | |
1929 | while (1) { | |
1930 | const char *next; | |
1931 | ||
1932 | next = strchr(split, '|'); | |
1933 | if (next != NULL) { | |
1934 | size_t matchlen = (size_t)(next - split); | |
1935 | ||
1936 | match = (matchlen == len && strncmp(split, val, matchlen) == 0); | |
1937 | if (match) | |
1938 | break; | |
1939 | } else { | |
1940 | match = (strcmp(split, val) == 0); | |
ac218d9c | 1941 | break; |
91a75e4a KS |
1942 | } |
1943 | split = &next[1]; | |
6880b25d | 1944 | } |
ac218d9c | 1945 | break; |
6880b25d | 1946 | } |
ac218d9c KS |
1947 | case GL_SPLIT_GLOB: |
1948 | { | |
1949 | char value[UTIL_PATH_SIZE]; | |
1950 | ||
065db052 | 1951 | util_strscpy(value, sizeof(value), &rules->buf[token->key.value_off]); |
ac218d9c KS |
1952 | key_value = value; |
1953 | while (key_value != NULL) { | |
1954 | pos = strchr(key_value, '|'); | |
1955 | if (pos != NULL) { | |
1956 | pos[0] = '\0'; | |
1957 | pos = &pos[1]; | |
1958 | } | |
5d6a1fa6 | 1959 | dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str(token->type), key_value, val); |
ac218d9c KS |
1960 | match = (fnmatch(key_value, val, 0) == 0); |
1961 | if (match) | |
1962 | break; | |
1963 | key_value = pos; | |
1964 | } | |
1965 | break; | |
1966 | } | |
db463fd3 KS |
1967 | case GL_SOMETHING: |
1968 | match = (val[0] != '\0'); | |
1969 | break; | |
ac218d9c KS |
1970 | case GL_UNSET: |
1971 | return -1; | |
6880b25d KS |
1972 | } |
1973 | ||
b0f7409f | 1974 | if (match && (token->key.op == OP_MATCH)) { |
5d6a1fa6 | 1975 | dbg(rules->udev, "%s is true (matching value)\n", token_str(token->type)); |
6880b25d KS |
1976 | return 0; |
1977 | } | |
b0f7409f | 1978 | if (!match && (token->key.op == OP_NOMATCH)) { |
5d6a1fa6 | 1979 | dbg(rules->udev, "%s is true (non-matching value)\n", token_str(token->type)); |
6880b25d KS |
1980 | return 0; |
1981 | } | |
5d6a1fa6 | 1982 | dbg(rules->udev, "%s is not true\n", token_str(token->type)); |
6880b25d KS |
1983 | return -1; |
1984 | } | |
1985 | ||
1986 | static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) | |
1987 | { | |
32028733 KS |
1988 | const char *name; |
1989 | char nbuf[UTIL_NAME_SIZE]; | |
1990 | const char *value; | |
1991 | char vbuf[UTIL_NAME_SIZE]; | |
6880b25d KS |
1992 | size_t len; |
1993 | ||
32028733 KS |
1994 | name = &rules->buf[cur->key.attr_off]; |
1995 | switch (cur->key.attrsubst) { | |
1996 | case SB_FORMAT: | |
1997 | udev_event_apply_format(event, name, nbuf, sizeof(nbuf)); | |
1998 | name = nbuf; | |
1999 | /* fall through */ | |
2000 | case SB_NONE: | |
2001 | value = udev_device_get_sysattr_value(dev, name); | |
2002 | if (value == NULL) | |
aeb53ca3 | 2003 | return -1; |
32028733 KS |
2004 | break; |
2005 | case SB_SUBSYS: | |
2006 | if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0) | |
2007 | return -1; | |
2008 | value = vbuf; | |
2009 | break; | |
2010 | default: | |
2011 | return -1; | |
6880b25d | 2012 | } |
6880b25d | 2013 | |
32028733 KS |
2014 | /* remove trailing whitespace, if not asked to match for it */ |
2015 | len = strlen(value); | |
2016 | if (len > 0 && isspace(value[len-1])) { | |
2017 | const char *key_value; | |
2018 | size_t klen; | |
2019 | ||
2020 | key_value = &rules->buf[cur->key.value_off]; | |
2021 | klen = strlen(key_value); | |
2022 | if (klen > 0 && !isspace(key_value[klen-1])) { | |
2023 | if (value != vbuf) { | |
2024 | util_strscpy(vbuf, sizeof(vbuf), value); | |
2025 | value = vbuf; | |
2026 | } | |
2027 | while (len > 0 && isspace(vbuf[--len])) | |
2028 | vbuf[len] = '\0'; | |
2029 | dbg(rules->udev, "removed trailing whitespace from '%s'\n", value); | |
2030 | } | |
6880b25d | 2031 | } |
32028733 | 2032 | |
6880b25d KS |
2033 | return match_key(rules, cur, value); |
2034 | } | |
2035 | ||
2036 | enum escape_type { | |
2037 | ESCAPE_UNSET, | |
2038 | ESCAPE_NONE, | |
2039 | ESCAPE_REPLACE, | |
2040 | }; | |
2041 | ||
2042 | int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event) | |
2043 | { | |
6880b25d | 2044 | struct token *cur; |
34d6a259 | 2045 | struct token *rule; |
3e5c7595 | 2046 | enum escape_type esc = ESCAPE_UNSET; |
4b06c409 | 2047 | bool can_set_name; |
6880b25d KS |
2048 | |
2049 | if (rules->tokens == NULL) | |
2050 | return -1; | |
2051 | ||
4b06c409 | 2052 | can_set_name = ((strcmp(udev_device_get_action(event->dev), "remove") != 0) && |
00f98bd2 KS |
2053 | (major(udev_device_get_devnum(event->dev)) > 0 || |
2054 | strcmp(udev_device_get_subsystem(event->dev), "net") == 0)); | |
2055 | ||
6880b25d KS |
2056 | /* loop through token list, match, run actions or forward to next rule */ |
2057 | cur = &rules->tokens[0]; | |
34d6a259 | 2058 | rule = cur; |
6270756c | 2059 | while (1) { |
6880b25d KS |
2060 | dump_token(rules, cur); |
2061 | switch (cur->type) { | |
2062 | case TK_RULE: | |
2063 | /* current rule */ | |
2064 | rule = cur; | |
00f98bd2 KS |
2065 | /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */ |
2066 | if (!can_set_name && rule->rule.flags) | |
0c057e9c | 2067 | goto nomatch; |
6880b25d KS |
2068 | esc = ESCAPE_UNSET; |
2069 | break; | |
6880b25d KS |
2070 | case TK_M_ACTION: |
2071 | if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0) | |
2072 | goto nomatch; | |
2073 | break; | |
2074 | case TK_M_DEVPATH: | |
2075 | if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0) | |
2076 | goto nomatch; | |
2077 | break; | |
2078 | case TK_M_KERNEL: | |
2079 | if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0) | |
2080 | goto nomatch; | |
2081 | break; | |
2082 | case TK_M_DEVLINK: | |
2083 | { | |
2084 | size_t devlen = strlen(udev_get_dev_path(event->udev))+1; | |
2085 | struct udev_list_entry *list_entry; | |
2086 | int match = 0; | |
2087 | ||
2088 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) { | |
2089 | const char *devlink; | |
2090 | ||
2091 | devlink = &udev_list_entry_get_name(list_entry)[devlen]; | |
2092 | if (match_key(rules, cur, devlink) == 0) { | |
2093 | match = 1; | |
2094 | break; | |
2095 | } | |
2096 | } | |
2097 | if (!match) | |
2098 | goto nomatch; | |
2099 | break; | |
2100 | } | |
2101 | case TK_M_NAME: | |
2102 | if (match_key(rules, cur, event->name) != 0) | |
2103 | goto nomatch; | |
2104 | break; | |
2105 | case TK_M_ENV: | |
2106 | { | |
6880b25d KS |
2107 | const char *key_name = &rules->buf[cur->key.attr_off]; |
2108 | const char *value; | |
2109 | ||
3d7b2831 | 2110 | value = udev_device_get_property_value(event->dev, key_name); |
6880b25d KS |
2111 | if (value == NULL) { |
2112 | dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name); | |
2113 | value = ""; | |
2114 | } | |
2115 | if (match_key(rules, cur, value)) | |
2116 | goto nomatch; | |
2117 | break; | |
2118 | } | |
3f896a2a KS |
2119 | case TK_M_TAG: |
2120 | { | |
2121 | struct udev_list_entry *list_entry; | |
2122 | bool match = false; | |
2123 | ||
2124 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) { | |
2125 | if (strcmp(&rules->buf[cur->key.value_off], udev_list_entry_get_name(list_entry)) == 0) { | |
2126 | match = true; | |
2127 | break; | |
2128 | } | |
2129 | } | |
2130 | if (!match && (cur->key.op != OP_NOMATCH)) | |
2131 | goto nomatch; | |
2132 | break; | |
2133 | } | |
6880b25d KS |
2134 | case TK_M_SUBSYSTEM: |
2135 | if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0) | |
2136 | goto nomatch; | |
2137 | break; | |
2138 | case TK_M_DRIVER: | |
2139 | if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0) | |
2140 | goto nomatch; | |
2141 | break; | |
7aeeaedc AJ |
2142 | case TK_M_WAITFOR: |
2143 | { | |
2144 | char filename[UTIL_PATH_SIZE]; | |
2145 | int found; | |
2146 | ||
065db052 | 2147 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename)); |
7aeeaedc | 2148 | found = (wait_for_file(event->dev, filename, 10) == 0); |
b0f7409f | 2149 | if (!found && (cur->key.op != OP_NOMATCH)) |
7aeeaedc AJ |
2150 | goto nomatch; |
2151 | break; | |
2152 | } | |
6880b25d KS |
2153 | case TK_M_ATTR: |
2154 | if (match_attr(rules, event->dev, event, cur) != 0) | |
2155 | goto nomatch; | |
2156 | break; | |
2157 | case TK_M_KERNELS: | |
2158 | case TK_M_SUBSYSTEMS: | |
2159 | case TK_M_DRIVERS: | |
2160 | case TK_M_ATTRS: | |
2161 | { | |
2162 | struct token *next; | |
2163 | ||
2164 | /* get whole sequence of parent matches */ | |
2165 | next = cur; | |
d15fcce4 | 2166 | while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX) |
6880b25d KS |
2167 | next++; |
2168 | ||
2169 | /* loop over parents */ | |
2170 | event->dev_parent = event->dev; | |
2171 | while (1) { | |
2172 | struct token *key; | |
2173 | ||
2174 | dbg(event->udev, "parent: '%s'\n", udev_device_get_syspath(event->dev_parent)); | |
2175 | /* loop over sequence of parent match keys */ | |
2176 | for (key = cur; key < next; key++ ) { | |
2177 | dump_token(rules, key); | |
2178 | switch(key->type) { | |
2179 | case TK_M_KERNELS: | |
2180 | if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0) | |
2181 | goto try_parent; | |
2182 | break; | |
2183 | case TK_M_SUBSYSTEMS: | |
2184 | if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0) | |
2185 | goto try_parent; | |
2186 | break; | |
2187 | case TK_M_DRIVERS: | |
2188 | if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0) | |
2189 | goto try_parent; | |
2190 | break; | |
2191 | case TK_M_ATTRS: | |
2192 | if (match_attr(rules, event->dev_parent, event, key) != 0) | |
2193 | goto try_parent; | |
2194 | break; | |
2195 | default: | |
2196 | goto nomatch; | |
2197 | } | |
2198 | dbg(event->udev, "parent key matched\n"); | |
2199 | } | |
2200 | dbg(event->udev, "all parent keys matched\n"); | |
2201 | /* all keys matched */ | |
2202 | break; | |
2203 | ||
2204 | try_parent: | |
2205 | event->dev_parent = udev_device_get_parent(event->dev_parent); | |
2206 | if (event->dev_parent == NULL) | |
2207 | goto nomatch; | |
2208 | } | |
2209 | /* move behind our sequence of parent match keys */ | |
2210 | cur = next; | |
2211 | continue; | |
2212 | } | |
2213 | case TK_M_TEST: | |
2214 | { | |
2215 | char filename[UTIL_PATH_SIZE]; | |
2216 | struct stat statbuf; | |
2217 | int match; | |
2218 | ||
065db052 KS |
2219 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename)); |
2220 | if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) { | |
6880b25d KS |
2221 | if (filename[0] != '/') { |
2222 | char tmp[UTIL_PATH_SIZE]; | |
2223 | ||
065db052 KS |
2224 | util_strscpy(tmp, sizeof(tmp), filename); |
2225 | util_strscpyl(filename, sizeof(filename), | |
2226 | udev_device_get_syspath(event->dev), "/", tmp, NULL); | |
6880b25d | 2227 | } |
065db052 | 2228 | } |
6880b25d KS |
2229 | attr_subst_subdir(filename, sizeof(filename)); |
2230 | ||
2231 | match = (stat(filename, &statbuf) == 0); | |
86b57788 | 2232 | dbg(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n"); |
6880b25d KS |
2233 | if (match && cur->key.mode > 0) { |
2234 | match = ((statbuf.st_mode & cur->key.mode) > 0); | |
86b57788 KS |
2235 | dbg(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode, |
2236 | match ? "matches" : "does not match", cur->key.mode); | |
6880b25d | 2237 | } |
b0f7409f | 2238 | if (match && cur->key.op == OP_NOMATCH) |
6880b25d | 2239 | goto nomatch; |
b0f7409f | 2240 | if (!match && cur->key.op == OP_MATCH) |
6880b25d KS |
2241 | goto nomatch; |
2242 | break; | |
2243 | } | |
2244 | case TK_M_PROGRAM: | |
2245 | { | |
2246 | char program[UTIL_PATH_SIZE]; | |
2247 | char **envp; | |
2248 | char result[UTIL_PATH_SIZE]; | |
2249 | ||
40fd3bc8 KS |
2250 | free(event->program_result); |
2251 | event->program_result = NULL; | |
065db052 | 2252 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], program, sizeof(program)); |
6880b25d | 2253 | envp = udev_device_get_properties_envp(event->dev); |
08e7f229 KS |
2254 | info(event->udev, "PROGRAM '%s' %s:%u\n", |
2255 | program, | |
2256 | &rules->buf[rule->rule.filename_off], | |
2257 | rule->rule.filename_line); | |
e85f5ec1 | 2258 | if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL, NULL, false) != 0) { |
b0f7409f | 2259 | if (cur->key.op != OP_NOMATCH) |
6880b25d KS |
2260 | goto nomatch; |
2261 | } else { | |
2262 | int count; | |
2263 | ||
2264 | util_remove_trailing_chars(result, '\n'); | |
2265 | if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) { | |
92f43136 | 2266 | count = udev_util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT); |
6880b25d KS |
2267 | if (count > 0) |
2268 | info(event->udev, "%i character(s) replaced\n" , count); | |
2269 | } | |
40fd3bc8 KS |
2270 | event->program_result = strdup(result); |
2271 | dbg(event->udev, "storing result '%s'\n", event->program_result); | |
b0f7409f | 2272 | if (cur->key.op == OP_NOMATCH) |
6880b25d KS |
2273 | goto nomatch; |
2274 | } | |
2275 | break; | |
2276 | } | |
2277 | case TK_M_IMPORT_FILE: | |
2278 | { | |
2279 | char import[UTIL_PATH_SIZE]; | |
2280 | ||
065db052 | 2281 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import)); |
6880b25d | 2282 | if (import_file_into_properties(event->dev, import) != 0) |
b0f7409f | 2283 | if (cur->key.op != OP_NOMATCH) |
6880b25d KS |
2284 | goto nomatch; |
2285 | break; | |
2286 | } | |
2287 | case TK_M_IMPORT_PROG: | |
2288 | { | |
2289 | char import[UTIL_PATH_SIZE]; | |
2290 | ||
065db052 | 2291 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import)); |
08e7f229 KS |
2292 | info(event->udev, "IMPORT '%s' %s:%u\n", |
2293 | import, | |
2294 | &rules->buf[rule->rule.filename_off], | |
2295 | rule->rule.filename_line); | |
6880b25d | 2296 | if (import_program_into_properties(event->dev, import) != 0) |
b0f7409f | 2297 | if (cur->key.op != OP_NOMATCH) |
6880b25d KS |
2298 | goto nomatch; |
2299 | break; | |
2300 | } | |
5539f624 KS |
2301 | case TK_M_IMPORT_DB: |
2302 | { | |
2303 | const char *key = &rules->buf[cur->key.value_off]; | |
2304 | const char *value; | |
2305 | ||
2306 | value = udev_device_get_property_value(event->dev_db, key); | |
2307 | if (value != NULL) { | |
2308 | struct udev_list_entry *entry; | |
2309 | ||
2310 | entry = udev_device_add_property(event->dev, key, value); | |
2311 | udev_list_entry_set_flags(entry, 1); | |
2312 | } else { | |
2313 | if (cur->key.op != OP_NOMATCH) | |
2314 | goto nomatch; | |
2315 | } | |
2316 | break; | |
2317 | } | |
6880b25d KS |
2318 | case TK_M_IMPORT_PARENT: |
2319 | { | |
2320 | char import[UTIL_PATH_SIZE]; | |
2321 | ||
065db052 | 2322 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import)); |
6880b25d | 2323 | if (import_parent_into_properties(event->dev, import) != 0) |
b0f7409f | 2324 | if (cur->key.op != OP_NOMATCH) |
6880b25d KS |
2325 | goto nomatch; |
2326 | break; | |
2327 | } | |
2328 | case TK_M_RESULT: | |
2329 | if (match_key(rules, cur, event->program_result) != 0) | |
2330 | goto nomatch; | |
2331 | break; | |
6880b25d KS |
2332 | case TK_A_STRING_ESCAPE_NONE: |
2333 | esc = ESCAPE_NONE; | |
2334 | break; | |
2335 | case TK_A_STRING_ESCAPE_REPLACE: | |
2336 | esc = ESCAPE_REPLACE; | |
2337 | break; | |
bd284db1 | 2338 | case TK_A_INOTIFY_WATCH: |
46f194cb | 2339 | event->inotify_watch = cur->key.watch; |
bd284db1 | 2340 | break; |
6880b25d KS |
2341 | case TK_A_DEVLINK_PRIO: |
2342 | udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio); | |
2343 | break; | |
2344 | case TK_A_OWNER: | |
2345 | { | |
2346 | char owner[UTIL_NAME_SIZE]; | |
2347 | ||
2348 | if (event->owner_final) | |
2349 | break; | |
b0f7409f | 2350 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d | 2351 | event->owner_final = 1; |
065db052 | 2352 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], owner, sizeof(owner)); |
6880b25d | 2353 | event->uid = util_lookup_user(event->udev, owner); |
0ef254d5 KS |
2354 | info(event->udev, "OWNER %u %s:%u\n", |
2355 | event->uid, | |
2356 | &rules->buf[rule->rule.filename_off], | |
2357 | rule->rule.filename_line); | |
6880b25d KS |
2358 | break; |
2359 | } | |
2360 | case TK_A_GROUP: | |
2361 | { | |
2362 | char group[UTIL_NAME_SIZE]; | |
2363 | ||
2364 | if (event->group_final) | |
2365 | break; | |
b0f7409f | 2366 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d | 2367 | event->group_final = 1; |
065db052 | 2368 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], group, sizeof(group)); |
6880b25d | 2369 | event->gid = util_lookup_group(event->udev, group); |
0ef254d5 KS |
2370 | info(event->udev, "GROUP %u %s:%u\n", |
2371 | event->gid, | |
2372 | &rules->buf[rule->rule.filename_off], | |
2373 | rule->rule.filename_line); | |
6880b25d KS |
2374 | break; |
2375 | } | |
2376 | case TK_A_MODE: | |
2377 | { | |
2378 | char mode[UTIL_NAME_SIZE]; | |
2379 | char *endptr; | |
2380 | ||
2381 | if (event->mode_final) | |
2382 | break; | |
b0f7409f | 2383 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d | 2384 | event->mode_final = 1; |
065db052 | 2385 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], mode, sizeof(mode)); |
6880b25d KS |
2386 | event->mode = strtol(mode, &endptr, 8); |
2387 | if (endptr[0] != '\0') { | |
2388 | err(event->udev, "invalide mode '%s' set default mode 0660\n", mode); | |
2389 | event->mode = 0660; | |
2390 | } | |
0ef254d5 KS |
2391 | info(event->udev, "MODE %#o %s:%u\n", |
2392 | event->mode, | |
2393 | &rules->buf[rule->rule.filename_off], | |
2394 | rule->rule.filename_line); | |
6880b25d KS |
2395 | break; |
2396 | } | |
2397 | case TK_A_OWNER_ID: | |
2398 | if (event->owner_final) | |
2399 | break; | |
b0f7409f | 2400 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d KS |
2401 | event->owner_final = 1; |
2402 | event->uid = cur->key.uid; | |
0ef254d5 KS |
2403 | info(event->udev, "OWNER %u %s:%u\n", |
2404 | event->uid, | |
2405 | &rules->buf[rule->rule.filename_off], | |
2406 | rule->rule.filename_line); | |
6880b25d KS |
2407 | break; |
2408 | case TK_A_GROUP_ID: | |
2409 | if (event->group_final) | |
2410 | break; | |
b0f7409f | 2411 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d KS |
2412 | event->group_final = 1; |
2413 | event->gid = cur->key.gid; | |
0ef254d5 KS |
2414 | info(event->udev, "GROUP %u %s:%u\n", |
2415 | event->gid, | |
2416 | &rules->buf[rule->rule.filename_off], | |
2417 | rule->rule.filename_line); | |
6880b25d KS |
2418 | break; |
2419 | case TK_A_MODE_ID: | |
2420 | if (event->mode_final) | |
2421 | break; | |
b0f7409f | 2422 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d KS |
2423 | event->mode_final = 1; |
2424 | event->mode = cur->key.mode; | |
0ef254d5 KS |
2425 | info(event->udev, "MODE %#o %s:%u\n", |
2426 | event->mode, | |
2427 | &rules->buf[rule->rule.filename_off], | |
2428 | rule->rule.filename_line); | |
6880b25d KS |
2429 | break; |
2430 | case TK_A_ENV: | |
2431 | { | |
2432 | const char *name = &rules->buf[cur->key.attr_off]; | |
2433 | char *value = &rules->buf[cur->key.value_off]; | |
2434 | ||
2435 | if (value[0] != '\0') { | |
2436 | char temp_value[UTIL_NAME_SIZE]; | |
2437 | struct udev_list_entry *entry; | |
2438 | ||
065db052 | 2439 | udev_event_apply_format(event, value, temp_value, sizeof(temp_value)); |
6880b25d | 2440 | entry = udev_device_add_property(event->dev, name, temp_value); |
b25a9454 KS |
2441 | /* store in db, skip private keys */ |
2442 | if (name[0] != '.') | |
fbb31cd6 | 2443 | udev_list_entry_set_flags(entry, 1); |
6880b25d KS |
2444 | } else { |
2445 | udev_device_add_property(event->dev, name, NULL); | |
2446 | } | |
2447 | break; | |
2448 | } | |
28460195 | 2449 | case TK_A_TAG: |
1dbfbfbe KS |
2450 | if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL) |
2451 | udev_device_cleanup_tags_list(event->dev); | |
28460195 KS |
2452 | udev_device_add_tag(event->dev, &rules->buf[cur->key.value_off]); |
2453 | break; | |
6880b25d KS |
2454 | case TK_A_NAME: |
2455 | { | |
2456 | const char *name = &rules->buf[cur->key.value_off]; | |
b99028c9 | 2457 | char name_str[UTIL_PATH_SIZE]; |
6880b25d KS |
2458 | int count; |
2459 | ||
2460 | if (event->name_final) | |
2461 | break; | |
b0f7409f | 2462 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d | 2463 | event->name_final = 1; |
065db052 | 2464 | udev_event_apply_format(event, name, name_str, sizeof(name_str)); |
6880b25d | 2465 | if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) { |
92f43136 | 2466 | count = udev_util_replace_chars(name_str, "/"); |
6880b25d KS |
2467 | if (count > 0) |
2468 | info(event->udev, "%i character(s) replaced\n", count); | |
2469 | } | |
10b2d011 KS |
2470 | free(event->name); |
2471 | event->name = strdup(name_str); | |
2472 | info(event->udev, "NAME '%s' %s:%u\n", | |
2473 | event->name, | |
2474 | &rules->buf[rule->rule.filename_off], | |
2475 | rule->rule.filename_line); | |
6880b25d KS |
2476 | break; |
2477 | } | |
2478 | case TK_A_DEVLINK: | |
2479 | { | |
2480 | char temp[UTIL_PATH_SIZE]; | |
2481 | char filename[UTIL_PATH_SIZE]; | |
2482 | char *pos, *next; | |
2483 | int count = 0; | |
2484 | ||
2485 | if (event->devlink_final) | |
2486 | break; | |
548459e9 KS |
2487 | if (major(udev_device_get_devnum(event->dev)) == 0) |
2488 | break; | |
b0f7409f | 2489 | if (cur->key.op == OP_ASSIGN_FINAL) |
6880b25d | 2490 | event->devlink_final = 1; |
b0f7409f | 2491 | if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL) |
6880b25d KS |
2492 | udev_device_cleanup_devlinks_list(event->dev); |
2493 | ||
2494 | /* allow multiple symlinks separated by spaces */ | |
065db052 | 2495 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], temp, sizeof(temp)); |
6880b25d | 2496 | if (esc == ESCAPE_UNSET) |
92f43136 | 2497 | count = udev_util_replace_chars(temp, "/ "); |
6880b25d | 2498 | else if (esc == ESCAPE_REPLACE) |
92f43136 | 2499 | count = udev_util_replace_chars(temp, "/"); |
6880b25d KS |
2500 | if (count > 0) |
2501 | info(event->udev, "%i character(s) replaced\n" , count); | |
2502 | dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp); | |
2503 | pos = temp; | |
2504 | while (isspace(pos[0])) | |
2505 | pos++; | |
2506 | next = strchr(pos, ' '); | |
6c29f2b9 | 2507 | while (next != NULL) { |
6880b25d | 2508 | next[0] = '\0'; |
6c29f2b9 KS |
2509 | info(event->udev, "LINK '%s' %s:%u\n", pos, |
2510 | &rules->buf[rule->rule.filename_off], rule->rule.filename_line); | |
065db052 | 2511 | util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL); |
6c29f2b9 | 2512 | udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique); |
6880b25d KS |
2513 | while (isspace(next[1])) |
2514 | next++; | |
2515 | pos = &next[1]; | |
2516 | next = strchr(pos, ' '); | |
2517 | } | |
2518 | if (pos[0] != '\0') { | |
6c29f2b9 KS |
2519 | info(event->udev, "LINK '%s' %s:%u\n", pos, |
2520 | &rules->buf[rule->rule.filename_off], rule->rule.filename_line); | |
065db052 | 2521 | util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL); |
6c29f2b9 | 2522 | udev_device_add_devlink(event->dev, filename, cur->key.devlink_unique); |
6880b25d KS |
2523 | } |
2524 | } | |
2525 | break; | |
2526 | case TK_A_EVENT_TIMEOUT: | |
2527 | udev_device_set_event_timeout(event->dev, cur->key.event_timeout); | |
2528 | break; | |
6880b25d KS |
2529 | case TK_A_ATTR: |
2530 | { | |
2531 | const char *key_name = &rules->buf[cur->key.attr_off]; | |
2532 | char attr[UTIL_PATH_SIZE]; | |
2533 | char value[UTIL_NAME_SIZE]; | |
2534 | FILE *f; | |
2535 | ||
065db052 KS |
2536 | if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0) |
2537 | util_strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL); | |
6880b25d KS |
2538 | attr_subst_subdir(attr, sizeof(attr)); |
2539 | ||
065db052 | 2540 | udev_event_apply_format(event, &rules->buf[cur->key.value_off], value, sizeof(value)); |
86b57788 KS |
2541 | info(event->udev, "ATTR '%s' writing '%s' %s:%u\n", attr, value, |
2542 | &rules->buf[rule->rule.filename_off], | |
2543 | rule->rule.filename_line); | |
6880b25d KS |
2544 | f = fopen(attr, "w"); |
2545 | if (f != NULL) { | |
ec2dd02e KS |
2546 | if (fprintf(f, "%s", value) <= 0) |
2547 | err(event->udev, "error writing ATTR{%s}: %m\n", attr); | |
6880b25d KS |
2548 | fclose(f); |
2549 | } else { | |
2550 | err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr); | |
2551 | } | |
2552 | break; | |
2553 | } | |
2554 | case TK_A_RUN: | |
2555 | { | |
2556 | struct udev_list_entry *list_entry; | |
2557 | ||
b0f7409f | 2558 | if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL) |
6880b25d | 2559 | udev_list_cleanup_entries(event->udev, &event->run_list); |
0ef254d5 KS |
2560 | info(event->udev, "RUN '%s' %s:%u\n", |
2561 | &rules->buf[cur->key.value_off], | |
2562 | &rules->buf[rule->rule.filename_off], | |
2563 | rule->rule.filename_line); | |
6880b25d KS |
2564 | list_entry = udev_list_entry_add(event->udev, &event->run_list, |
2565 | &rules->buf[cur->key.value_off], NULL, 1, 0); | |
6c29f2b9 | 2566 | if (cur->key.fail_on_error) |
fbb31cd6 | 2567 | udev_list_entry_set_flags(list_entry, 1); |
6880b25d KS |
2568 | break; |
2569 | } | |
2570 | case TK_A_GOTO: | |
0c377989 KS |
2571 | if (cur->key.rule_goto == 0) |
2572 | break; | |
6880b25d KS |
2573 | cur = &rules->tokens[cur->key.rule_goto]; |
2574 | continue; | |
6270756c | 2575 | case TK_END: |
11ae7578 | 2576 | return 0; |
6880b25d | 2577 | |
d15fcce4 | 2578 | case TK_M_PARENTS_MIN: |
b0f7409f | 2579 | case TK_M_PARENTS_MAX: |
ac218d9c | 2580 | case TK_M_MAX: |
ac218d9c | 2581 | case TK_UNSET: |
6880b25d KS |
2582 | err(rules->udev, "wrong type %u\n", cur->type); |
2583 | goto nomatch; | |
2584 | } | |
2585 | ||
2586 | cur++; | |
2587 | continue; | |
2588 | nomatch: | |
2589 | /* fast-forward to next rule */ | |
3f3aa9f5 AJ |
2590 | cur = rule + rule->rule.token_count; |
2591 | dbg(rules->udev, "forward to rule: %u\n", | |
2592 | (unsigned int) (cur - rules->tokens)); | |
6880b25d | 2593 | } |
6880b25d | 2594 | } |