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