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