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