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