]>
Commit | Line | Data |
---|---|---|
796b06c2 KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
1298001e | 4 | Copyright 2012 Kay Sievers <kay@vrfy.org> |
796b06c2 KS |
5 | |
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
07630cea | 20 | #include <ctype.h> |
796b06c2 | 21 | #include <getopt.h> |
07630cea | 22 | #include <stdlib.h> |
796b06c2 KS |
23 | #include <string.h> |
24 | ||
b5efdb8a | 25 | #include "alloc-util.h" |
796b06c2 | 26 | #include "conf-files.h" |
0d39fa9c | 27 | #include "fileio.h" |
f4f15635 | 28 | #include "fs-util.h" |
8b516fde | 29 | #include "hwdb-internal.h" |
d640c07d | 30 | #include "hwdb-util.h" |
07630cea LP |
31 | #include "strbuf.h" |
32 | #include "string-util.h" | |
33 | #include "udev.h" | |
34 | #include "util.h" | |
796b06c2 KS |
35 | |
36 | /* | |
37 | * Generic udev properties, key/value database based on modalias strings. | |
38 | * Uses a Patricia/radix trie to index all matches for efficient lookup. | |
39 | */ | |
40 | ||
41 | static const char * const conf_file_dirs[] = { | |
2001208c | 42 | "/etc/udev/hwdb.d", |
796b06c2 KS |
43 | UDEVLIBEXECDIR "/hwdb.d", |
44 | NULL | |
45 | }; | |
46 | ||
47 | /* in-memory trie objects */ | |
48 | struct trie { | |
49 | struct trie_node *root; | |
50 | struct strbuf *strings; | |
51 | ||
52 | size_t nodes_count; | |
53 | size_t children_count; | |
54 | size_t values_count; | |
55 | }; | |
56 | ||
57 | struct trie_node { | |
58 | /* prefix, common part for all children of this node */ | |
59 | size_t prefix_off; | |
60 | ||
61 | /* sorted array of pointers to children nodes */ | |
62 | struct trie_child_entry *children; | |
63 | uint8_t children_count; | |
64 | ||
65 | /* sorted array of key/value pairs */ | |
66 | struct trie_value_entry *values; | |
67 | size_t values_count; | |
68 | }; | |
69 | ||
70 | /* children array item with char (0-255) index */ | |
71 | struct trie_child_entry { | |
72 | uint8_t c; | |
73 | struct trie_node *child; | |
74 | }; | |
75 | ||
76 | /* value array item with key/value pairs */ | |
77 | struct trie_value_entry { | |
78 | size_t key_off; | |
79 | size_t value_off; | |
80 | }; | |
81 | ||
82 | static int trie_children_cmp(const void *v1, const void *v2) { | |
83 | const struct trie_child_entry *n1 = v1; | |
84 | const struct trie_child_entry *n2 = v2; | |
85 | ||
86 | return n1->c - n2->c; | |
87 | } | |
88 | ||
89 | static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) { | |
90 | struct trie_child_entry *child; | |
796b06c2 KS |
91 | |
92 | /* extend array, add new entry, sort for bisection */ | |
93 | child = realloc(node->children, (node->children_count + 1) * sizeof(struct trie_child_entry)); | |
8c62ecf1 ZJS |
94 | if (!child) |
95 | return -ENOMEM; | |
96 | ||
796b06c2 KS |
97 | node->children = child; |
98 | trie->children_count++; | |
99 | node->children[node->children_count].c = c; | |
100 | node->children[node->children_count].child = node_child; | |
101 | node->children_count++; | |
102 | qsort(node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp); | |
103 | trie->nodes_count++; | |
8c62ecf1 ZJS |
104 | |
105 | return 0; | |
796b06c2 KS |
106 | } |
107 | ||
108 | static struct trie_node *node_lookup(const struct trie_node *node, uint8_t c) { | |
109 | struct trie_child_entry *child; | |
110 | struct trie_child_entry search; | |
111 | ||
112 | search.c = c; | |
113 | child = bsearch(&search, node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp); | |
114 | if (child) | |
115 | return child->child; | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | static void trie_node_cleanup(struct trie_node *node) { | |
120 | size_t i; | |
121 | ||
122 | for (i = 0; i < node->children_count; i++) | |
123 | trie_node_cleanup(node->children[i].child); | |
124 | free(node->children); | |
125 | free(node->values); | |
126 | free(node); | |
127 | } | |
128 | ||
129 | static int trie_values_cmp(const void *v1, const void *v2, void *arg) { | |
130 | const struct trie_value_entry *val1 = v1; | |
131 | const struct trie_value_entry *val2 = v2; | |
132 | struct trie *trie = arg; | |
133 | ||
134 | return strcmp(trie->strings->buf + val1->key_off, | |
135 | trie->strings->buf + val2->key_off); | |
136 | } | |
137 | ||
138 | static int trie_node_add_value(struct trie *trie, struct trie_node *node, | |
139 | const char *key, const char *value) { | |
c225f2ff | 140 | ssize_t k, v; |
796b06c2 | 141 | struct trie_value_entry *val; |
796b06c2 KS |
142 | |
143 | k = strbuf_add_string(trie->strings, key, strlen(key)); | |
c225f2ff KS |
144 | if (k < 0) |
145 | return k; | |
796b06c2 | 146 | v = strbuf_add_string(trie->strings, value, strlen(value)); |
c225f2ff KS |
147 | if (v < 0) |
148 | return v; | |
149 | ||
150 | if (node->values_count) { | |
151 | struct trie_value_entry search = { | |
152 | .key_off = k, | |
153 | .value_off = v, | |
154 | }; | |
155 | ||
156 | val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie); | |
157 | if (val) { | |
158 | /* replace existing earlier key with new value */ | |
159 | val->value_off = v; | |
160 | return 0; | |
161 | } | |
796b06c2 KS |
162 | } |
163 | ||
164 | /* extend array, add new entry, sort for bisection */ | |
165 | val = realloc(node->values, (node->values_count + 1) * sizeof(struct trie_value_entry)); | |
166 | if (!val) | |
167 | return -ENOMEM; | |
168 | trie->values_count++; | |
169 | node->values = val; | |
170 | node->values[node->values_count].key_off = k; | |
171 | node->values[node->values_count].value_off = v; | |
172 | node->values_count++; | |
173 | qsort_r(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie); | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static int trie_insert(struct trie *trie, struct trie_node *node, const char *search, | |
178 | const char *key, const char *value) { | |
179 | size_t i = 0; | |
180 | int err = 0; | |
181 | ||
182 | for (;;) { | |
183 | size_t p; | |
184 | uint8_t c; | |
8c62ecf1 | 185 | struct trie_node *child; |
796b06c2 KS |
186 | |
187 | for (p = 0; (c = trie->strings->buf[node->prefix_off + p]); p++) { | |
7fd1b19b | 188 | _cleanup_free_ char *s = NULL; |
796b06c2 | 189 | ssize_t off; |
7fd1b19b | 190 | _cleanup_free_ struct trie_node *new_child = NULL; |
796b06c2 KS |
191 | |
192 | if (c == search[i + p]) | |
193 | continue; | |
194 | ||
195 | /* split node */ | |
955d98c9 | 196 | new_child = new0(struct trie_node, 1); |
8c62ecf1 ZJS |
197 | if (!new_child) |
198 | return -ENOMEM; | |
796b06c2 KS |
199 | |
200 | /* move values from parent to child */ | |
8c62ecf1 ZJS |
201 | new_child->prefix_off = node->prefix_off + p+1; |
202 | new_child->children = node->children; | |
203 | new_child->children_count = node->children_count; | |
204 | new_child->values = node->values; | |
205 | new_child->values_count = node->values_count; | |
796b06c2 KS |
206 | |
207 | /* update parent; use strdup() because the source gets realloc()d */ | |
208 | s = strndup(trie->strings->buf + node->prefix_off, p); | |
8c62ecf1 ZJS |
209 | if (!s) |
210 | return -ENOMEM; | |
211 | ||
796b06c2 | 212 | off = strbuf_add_string(trie->strings, s, p); |
8c62ecf1 ZJS |
213 | if (off < 0) |
214 | return off; | |
215 | ||
796b06c2 KS |
216 | node->prefix_off = off; |
217 | node->children = NULL; | |
218 | node->children_count = 0; | |
219 | node->values = NULL; | |
220 | node->values_count = 0; | |
8c62ecf1 | 221 | err = node_add_child(trie, node, new_child, c); |
796b06c2 | 222 | if (err) |
8c62ecf1 ZJS |
223 | return err; |
224 | ||
225 | new_child = NULL; /* avoid cleanup */ | |
796b06c2 KS |
226 | break; |
227 | } | |
228 | i += p; | |
229 | ||
230 | c = search[i]; | |
231 | if (c == '\0') | |
232 | return trie_node_add_value(trie, node, key, value); | |
233 | ||
234 | child = node_lookup(node, c); | |
235 | if (!child) { | |
236 | ssize_t off; | |
237 | ||
238 | /* new child */ | |
955d98c9 | 239 | child = new0(struct trie_node, 1); |
8c62ecf1 ZJS |
240 | if (!child) |
241 | return -ENOMEM; | |
242 | ||
796b06c2 KS |
243 | off = strbuf_add_string(trie->strings, search + i+1, strlen(search + i+1)); |
244 | if (off < 0) { | |
8c62ecf1 ZJS |
245 | free(child); |
246 | return off; | |
796b06c2 | 247 | } |
8c62ecf1 | 248 | |
796b06c2 KS |
249 | child->prefix_off = off; |
250 | err = node_add_child(trie, node, child, c); | |
8c62ecf1 ZJS |
251 | if (err) { |
252 | free(child); | |
253 | return err; | |
254 | } | |
255 | ||
796b06c2 KS |
256 | return trie_node_add_value(trie, child, key, value); |
257 | } | |
258 | ||
259 | node = child; | |
260 | i++; | |
261 | } | |
796b06c2 KS |
262 | } |
263 | ||
264 | struct trie_f { | |
265 | FILE *f; | |
266 | struct trie *trie; | |
267 | uint64_t strings_off; | |
268 | ||
269 | uint64_t nodes_count; | |
270 | uint64_t children_count; | |
271 | uint64_t values_count; | |
272 | }; | |
273 | ||
274 | /* calculate the storage space for the nodes, children arrays, value arrays */ | |
275 | static void trie_store_nodes_size(struct trie_f *trie, struct trie_node *node) { | |
276 | uint64_t i; | |
277 | ||
278 | for (i = 0; i < node->children_count; i++) | |
279 | trie_store_nodes_size(trie, node->children[i].child); | |
280 | ||
281 | trie->strings_off += sizeof(struct trie_node_f); | |
282 | for (i = 0; i < node->children_count; i++) | |
283 | trie->strings_off += sizeof(struct trie_child_entry_f); | |
284 | for (i = 0; i < node->values_count; i++) | |
285 | trie->strings_off += sizeof(struct trie_value_entry_f); | |
286 | } | |
287 | ||
288 | static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) { | |
289 | uint64_t i; | |
290 | struct trie_node_f n = { | |
291 | .prefix_off = htole64(trie->strings_off + node->prefix_off), | |
292 | .children_count = node->children_count, | |
293 | .values_count = htole64(node->values_count), | |
294 | }; | |
2001208c | 295 | struct trie_child_entry_f *children = NULL; |
796b06c2 KS |
296 | int64_t node_off; |
297 | ||
298 | if (node->children_count) { | |
299 | children = new0(struct trie_child_entry_f, node->children_count); | |
300 | if (!children) | |
301 | return -ENOMEM; | |
302 | } | |
303 | ||
304 | /* post-order recursion */ | |
305 | for (i = 0; i < node->children_count; i++) { | |
306 | int64_t child_off; | |
307 | ||
308 | child_off = trie_store_nodes(trie, node->children[i].child); | |
ef89eef7 ZJS |
309 | if (child_off < 0) { |
310 | free(children); | |
796b06c2 | 311 | return child_off; |
ef89eef7 | 312 | } |
796b06c2 KS |
313 | children[i].c = node->children[i].c; |
314 | children[i].child_off = htole64(child_off); | |
315 | } | |
316 | ||
317 | /* write node */ | |
318 | node_off = ftello(trie->f); | |
319 | fwrite(&n, sizeof(struct trie_node_f), 1, trie->f); | |
320 | trie->nodes_count++; | |
321 | ||
322 | /* append children array */ | |
323 | if (node->children_count) { | |
324 | fwrite(children, sizeof(struct trie_child_entry_f), node->children_count, trie->f); | |
325 | trie->children_count += node->children_count; | |
326 | free(children); | |
327 | } | |
328 | ||
329 | /* append values array */ | |
330 | for (i = 0; i < node->values_count; i++) { | |
331 | struct trie_value_entry_f v = { | |
332 | .key_off = htole64(trie->strings_off + node->values[i].key_off), | |
333 | .value_off = htole64(trie->strings_off + node->values[i].value_off), | |
334 | }; | |
335 | ||
336 | fwrite(&v, sizeof(struct trie_value_entry_f), 1, trie->f); | |
337 | trie->values_count++; | |
338 | } | |
339 | ||
340 | return node_off; | |
341 | } | |
342 | ||
343 | static int trie_store(struct trie *trie, const char *filename) { | |
344 | struct trie_f t = { | |
345 | .trie = trie, | |
346 | }; | |
d4f1ef44 | 347 | _cleanup_free_ char *filename_tmp = NULL; |
796b06c2 KS |
348 | int64_t pos; |
349 | int64_t root_off; | |
350 | int64_t size; | |
351 | struct trie_header_f h = { | |
352 | .signature = HWDB_SIG, | |
353 | .tool_version = htole64(atoi(VERSION)), | |
354 | .header_size = htole64(sizeof(struct trie_header_f)), | |
355 | .node_size = htole64(sizeof(struct trie_node_f)), | |
356 | .child_entry_size = htole64(sizeof(struct trie_child_entry_f)), | |
357 | .value_entry_size = htole64(sizeof(struct trie_value_entry_f)), | |
358 | }; | |
359 | int err; | |
360 | ||
361 | /* calculate size of header, nodes, children entries, value entries */ | |
362 | t.strings_off = sizeof(struct trie_header_f); | |
363 | trie_store_nodes_size(&t, trie->root); | |
364 | ||
365 | err = fopen_temporary(filename , &t.f, &filename_tmp); | |
366 | if (err < 0) | |
367 | return err; | |
368 | fchmod(fileno(t.f), 0444); | |
369 | ||
370 | /* write nodes */ | |
f901aaad TG |
371 | err = fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET); |
372 | if (err < 0) { | |
373 | fclose(t.f); | |
374 | unlink_noerrno(filename_tmp); | |
375 | return -errno; | |
376 | } | |
796b06c2 KS |
377 | root_off = trie_store_nodes(&t, trie->root); |
378 | h.nodes_root_off = htole64(root_off); | |
379 | pos = ftello(t.f); | |
380 | h.nodes_len = htole64(pos - sizeof(struct trie_header_f)); | |
381 | ||
382 | /* write string buffer */ | |
383 | fwrite(trie->strings->buf, trie->strings->len, 1, t.f); | |
384 | h.strings_len = htole64(trie->strings->len); | |
385 | ||
386 | /* write header */ | |
387 | size = ftello(t.f); | |
388 | h.file_size = htole64(size); | |
f901aaad TG |
389 | err = fseeko(t.f, 0, SEEK_SET); |
390 | if (err < 0) { | |
391 | fclose(t.f); | |
392 | unlink_noerrno(filename_tmp); | |
393 | return -errno; | |
394 | } | |
796b06c2 KS |
395 | fwrite(&h, sizeof(struct trie_header_f), 1, t.f); |
396 | err = ferror(t.f); | |
397 | if (err) | |
398 | err = -errno; | |
399 | fclose(t.f); | |
400 | if (err < 0 || rename(filename_tmp, filename) < 0) { | |
d4f1ef44 LP |
401 | unlink_noerrno(filename_tmp); |
402 | return err < 0 ? err : -errno; | |
796b06c2 KS |
403 | } |
404 | ||
9f6445e3 | 405 | log_debug("=== trie on-disk ==="); |
1fa2f38f | 406 | log_debug("size: %8"PRIi64" bytes", size); |
9f6445e3 | 407 | log_debug("header: %8zu bytes", sizeof(struct trie_header_f)); |
90b2de37 ZJS |
408 | log_debug("nodes: %8"PRIu64" bytes (%8"PRIu64")", |
409 | t.nodes_count * sizeof(struct trie_node_f), t.nodes_count); | |
410 | log_debug("child pointers: %8"PRIu64" bytes (%8"PRIu64")", | |
411 | t.children_count * sizeof(struct trie_child_entry_f), t.children_count); | |
412 | log_debug("value pointers: %8"PRIu64" bytes (%8"PRIu64")", | |
413 | t.values_count * sizeof(struct trie_value_entry_f), t.values_count); | |
414 | log_debug("string store: %8zu bytes", trie->strings->len); | |
415 | log_debug("strings start: %8"PRIu64, t.strings_off); | |
d4f1ef44 LP |
416 | |
417 | return 0; | |
796b06c2 KS |
418 | } |
419 | ||
06639c09 KS |
420 | static int insert_data(struct trie *trie, struct udev_list *match_list, |
421 | char *line, const char *filename) { | |
422 | char *value; | |
423 | struct udev_list_entry *entry; | |
424 | ||
425 | value = strchr(line, '='); | |
426 | if (!value) { | |
9f6445e3 | 427 | log_error("Error, key/value pair expected but got '%s' in '%s':", line, filename); |
06639c09 KS |
428 | return -EINVAL; |
429 | } | |
430 | ||
431 | value[0] = '\0'; | |
432 | value++; | |
433 | ||
36afca67 PH |
434 | /* libudev requires properties to start with a space */ |
435 | while (isblank(line[0]) && isblank(line[1])) | |
436 | line++; | |
437 | ||
06639c09 | 438 | if (line[0] == '\0' || value[0] == '\0') { |
9f6445e3 | 439 | log_error("Error, empty key or value '%s' in '%s':", line, filename); |
06639c09 KS |
440 | return -EINVAL; |
441 | } | |
442 | ||
443 | udev_list_entry_foreach(entry, udev_list_get_entry(match_list)) | |
444 | trie_insert(trie, trie->root, udev_list_entry_get_name(entry), line, value); | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
449 | static int import_file(struct udev *udev, struct trie *trie, const char *filename) { | |
450 | enum { | |
451 | HW_MATCH, | |
452 | HW_DATA, | |
453 | HW_NONE, | |
454 | } state = HW_NONE; | |
796b06c2 KS |
455 | FILE *f; |
456 | char line[LINE_MAX]; | |
06639c09 KS |
457 | struct udev_list match_list; |
458 | ||
459 | udev_list_init(udev, &match_list, false); | |
796b06c2 KS |
460 | |
461 | f = fopen(filename, "re"); | |
462 | if (f == NULL) | |
463 | return -errno; | |
464 | ||
796b06c2 KS |
465 | while (fgets(line, sizeof(line), f)) { |
466 | size_t len; | |
06639c09 | 467 | char *pos; |
796b06c2 | 468 | |
06639c09 | 469 | /* comment line */ |
796b06c2 KS |
470 | if (line[0] == '#') |
471 | continue; | |
472 | ||
06639c09 KS |
473 | /* strip trailing comment */ |
474 | pos = strchr(line, '#'); | |
475 | if (pos) | |
476 | pos[0] = '\0'; | |
796b06c2 | 477 | |
06639c09 | 478 | /* strip trailing whitespace */ |
796b06c2 | 479 | len = strlen(line); |
06639c09 KS |
480 | while (len > 0 && isspace(line[len-1])) |
481 | len--; | |
482 | line[len] = '\0'; | |
483 | ||
484 | switch (state) { | |
485 | case HW_NONE: | |
486 | if (len == 0) | |
487 | break; | |
488 | ||
489 | if (line[0] == ' ') { | |
9f6445e3 | 490 | log_error("Error, MATCH expected but got '%s' in '%s':", line, filename); |
06639c09 KS |
491 | break; |
492 | } | |
796b06c2 | 493 | |
06639c09 KS |
494 | /* start of record, first match */ |
495 | state = HW_MATCH; | |
496 | udev_list_entry_add(&match_list, line, NULL); | |
497 | break; | |
796b06c2 | 498 | |
06639c09 KS |
499 | case HW_MATCH: |
500 | if (len == 0) { | |
9f6445e3 | 501 | log_error("Error, DATA expected but got empty line in '%s':", filename); |
06639c09 KS |
502 | state = HW_NONE; |
503 | udev_list_cleanup(&match_list); | |
504 | break; | |
505 | } | |
796b06c2 | 506 | |
06639c09 KS |
507 | /* another match */ |
508 | if (line[0] != ' ') { | |
509 | udev_list_entry_add(&match_list, line, NULL); | |
510 | break; | |
511 | } | |
512 | ||
513 | /* first data */ | |
514 | state = HW_DATA; | |
515 | insert_data(trie, &match_list, line, filename); | |
516 | break; | |
517 | ||
518 | case HW_DATA: | |
519 | /* end of record */ | |
520 | if (len == 0) { | |
521 | state = HW_NONE; | |
522 | udev_list_cleanup(&match_list); | |
523 | break; | |
524 | } | |
525 | ||
526 | if (line[0] != ' ') { | |
9f6445e3 | 527 | log_error("Error, DATA expected but got '%s' in '%s':", line, filename); |
06639c09 KS |
528 | state = HW_NONE; |
529 | udev_list_cleanup(&match_list); | |
530 | break; | |
531 | } | |
3cf7b686 | 532 | |
06639c09 KS |
533 | insert_data(trie, &match_list, line, filename); |
534 | break; | |
535 | }; | |
796b06c2 | 536 | } |
06639c09 | 537 | |
796b06c2 | 538 | fclose(f); |
06639c09 | 539 | udev_list_cleanup(&match_list); |
796b06c2 KS |
540 | return 0; |
541 | } | |
542 | ||
543 | static void help(void) { | |
e32a4e1e | 544 | printf("Usage: udevadm hwdb OPTIONS\n" |
7643ac9a | 545 | " -u,--update update the hardware database\n" |
33488f19 | 546 | " --usr generate in " UDEVLIBEXECDIR " instead of /etc/udev\n" |
7643ac9a ZJS |
547 | " -t,--test=MODALIAS query database and print result\n" |
548 | " -r,--root=PATH alternative root path in the filesystem\n" | |
549 | " -h,--help\n\n"); | |
796b06c2 KS |
550 | } |
551 | ||
552 | static int adm_hwdb(struct udev *udev, int argc, char *argv[]) { | |
33488f19 MP |
553 | enum { |
554 | ARG_USR = 0x100, | |
555 | }; | |
556 | ||
796b06c2 | 557 | static const struct option options[] = { |
7643ac9a | 558 | { "update", no_argument, NULL, 'u' }, |
33488f19 | 559 | { "usr", no_argument, NULL, ARG_USR }, |
7643ac9a ZJS |
560 | { "test", required_argument, NULL, 't' }, |
561 | { "root", required_argument, NULL, 'r' }, | |
562 | { "help", no_argument, NULL, 'h' }, | |
796b06c2 KS |
563 | {} |
564 | }; | |
23b72453 | 565 | const char *test = NULL; |
3e49f2ec | 566 | const char *root = ""; |
33488f19 | 567 | const char *hwdb_bin_dir = "/etc/udev"; |
796b06c2 | 568 | bool update = false; |
23b72453 | 569 | struct trie *trie = NULL; |
7643ac9a | 570 | int err, c; |
796b06c2 KS |
571 | int rc = EXIT_SUCCESS; |
572 | ||
7643ac9a ZJS |
573 | while ((c = getopt_long(argc, argv, "ut:r:h", options, NULL)) >= 0) |
574 | switch(c) { | |
796b06c2 KS |
575 | case 'u': |
576 | update = true; | |
577 | break; | |
33488f19 MP |
578 | case ARG_USR: |
579 | hwdb_bin_dir = UDEVLIBEXECDIR; | |
580 | break; | |
23b72453 KS |
581 | case 't': |
582 | test = optarg; | |
583 | break; | |
3e49f2ec KS |
584 | case 'r': |
585 | root = optarg; | |
586 | break; | |
796b06c2 KS |
587 | case 'h': |
588 | help(); | |
589 | return EXIT_SUCCESS; | |
7643ac9a ZJS |
590 | case '?': |
591 | return EXIT_FAILURE; | |
592 | default: | |
593 | assert_not_reached("Unknown option"); | |
796b06c2 | 594 | } |
796b06c2 | 595 | |
23b72453 | 596 | if (!update && !test) { |
7643ac9a ZJS |
597 | log_error("Either --update or --test must be used"); |
598 | return EXIT_FAILURE; | |
796b06c2 KS |
599 | } |
600 | ||
23b72453 KS |
601 | if (update) { |
602 | char **files, **f; | |
3e49f2ec | 603 | _cleanup_free_ char *hwdb_bin = NULL; |
796b06c2 | 604 | |
955d98c9 | 605 | trie = new0(struct trie, 1); |
23b72453 KS |
606 | if (!trie) { |
607 | rc = EXIT_FAILURE; | |
608 | goto out; | |
609 | } | |
796b06c2 | 610 | |
23b72453 KS |
611 | /* string store */ |
612 | trie->strings = strbuf_new(); | |
613 | if (!trie->strings) { | |
614 | rc = EXIT_FAILURE; | |
615 | goto out; | |
616 | } | |
796b06c2 | 617 | |
23b72453 | 618 | /* index */ |
955d98c9 | 619 | trie->root = new0(struct trie_node, 1); |
23b72453 KS |
620 | if (!trie->root) { |
621 | rc = EXIT_FAILURE; | |
622 | goto out; | |
623 | } | |
624 | trie->nodes_count++; | |
625 | ||
844ec79b | 626 | err = conf_files_list_strv(&files, ".hwdb", root, conf_file_dirs); |
23b72453 | 627 | if (err < 0) { |
da927ba9 | 628 | log_error_errno(err, "failed to enumerate hwdb files: %m"); |
23b72453 KS |
629 | rc = EXIT_FAILURE; |
630 | goto out; | |
631 | } | |
632 | STRV_FOREACH(f, files) { | |
633 | log_debug("reading file '%s'", *f); | |
06639c09 | 634 | import_file(udev, trie, *f); |
23b72453 KS |
635 | } |
636 | strv_free(files); | |
637 | ||
638 | strbuf_complete(trie->strings); | |
639 | ||
9f6445e3 LP |
640 | log_debug("=== trie in-memory ==="); |
641 | log_debug("nodes: %8zu bytes (%8zu)", | |
3a4431ef | 642 | trie->nodes_count * sizeof(struct trie_node), trie->nodes_count); |
9f6445e3 | 643 | log_debug("children arrays: %8zu bytes (%8zu)", |
3a4431ef | 644 | trie->children_count * sizeof(struct trie_child_entry), trie->children_count); |
9f6445e3 | 645 | log_debug("values arrays: %8zu bytes (%8zu)", |
3a4431ef | 646 | trie->values_count * sizeof(struct trie_value_entry), trie->values_count); |
9f6445e3 | 647 | log_debug("strings: %8zu bytes", |
3a4431ef | 648 | trie->strings->len); |
9f6445e3 | 649 | log_debug("strings incoming: %8zu bytes (%8zu)", |
3a4431ef | 650 | trie->strings->in_len, trie->strings->in_count); |
9f6445e3 | 651 | log_debug("strings dedup'ed: %8zu bytes (%8zu)", |
3a4431ef | 652 | trie->strings->dedup_len, trie->strings->dedup_count); |
23b72453 | 653 | |
33488f19 MP |
654 | hwdb_bin = strjoin(root, "/", hwdb_bin_dir, "/hwdb.bin", NULL); |
655 | if (!hwdb_bin) { | |
3e49f2ec KS |
656 | rc = EXIT_FAILURE; |
657 | goto out; | |
658 | } | |
659 | mkdir_parents(hwdb_bin, 0755); | |
660 | err = trie_store(trie, hwdb_bin); | |
23b72453 | 661 | if (err < 0) { |
da927ba9 | 662 | log_error_errno(err, "Failure writing database %s: %m", hwdb_bin); |
23b72453 KS |
663 | rc = EXIT_FAILURE; |
664 | } | |
796b06c2 | 665 | } |
23b72453 KS |
666 | |
667 | if (test) { | |
4afd3348 | 668 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
d640c07d | 669 | int r; |
23b72453 | 670 | |
d640c07d TG |
671 | r = sd_hwdb_new(&hwdb); |
672 | if (r >= 0) { | |
673 | const char *key, *value; | |
23b72453 | 674 | |
d640c07d TG |
675 | SD_HWDB_FOREACH_PROPERTY(hwdb, test, key, value) |
676 | printf("%s=%s\n", key, value); | |
23b72453 | 677 | } |
796b06c2 | 678 | } |
796b06c2 | 679 | out: |
23b72453 KS |
680 | if (trie) { |
681 | if (trie->root) | |
682 | trie_node_cleanup(trie->root); | |
683 | strbuf_cleanup(trie->strings); | |
684 | free(trie); | |
685 | } | |
796b06c2 KS |
686 | return rc; |
687 | } | |
688 | ||
689 | const struct udevadm_cmd udevadm_hwdb = { | |
690 | .name = "hwdb", | |
691 | .cmd = adm_hwdb, | |
796b06c2 | 692 | }; |