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