1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Kay Sievers <kay@vrfy.org>
13 #include "alloc-util.h"
14 #include "conf-files.h"
17 #include "hwdb-internal.h"
18 #include "hwdb-util.h"
22 #include "string-util.h"
24 #include "udevadm-util.h"
28 * Generic udev properties, key/value database based on modalias strings.
29 * Uses a Patricia/radix trie to index all matches for efficient lookup.
32 static const char * const conf_file_dirs
[] = {
34 UDEVLIBEXECDIR
"/hwdb.d",
38 /* in-memory trie objects */
40 struct trie_node
*root
;
41 struct strbuf
*strings
;
44 size_t children_count
;
49 /* prefix, common part for all children of this node */
52 /* sorted array of pointers to children nodes */
53 struct trie_child_entry
*children
;
54 uint8_t children_count
;
56 /* sorted array of key/value pairs */
57 struct trie_value_entry
*values
;
61 /* children array item with char (0-255) index */
62 struct trie_child_entry
{
64 struct trie_node
*child
;
67 /* value array item with key/value pairs */
68 struct trie_value_entry
{
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
;
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
;
83 /* extend array, add new entry, sort for bisection */
84 child
= reallocarray(node
->children
, node
->children_count
+ 1, sizeof(struct trie_child_entry
));
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
);
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
;
104 child
= bsearch_safe(&search
,
105 node
->children
, node
->children_count
, sizeof(struct trie_child_entry
),
112 static void trie_node_cleanup(struct trie_node
*node
) {
115 for (i
= 0; i
< node
->children_count
; i
++)
116 trie_node_cleanup(node
->children
[i
].child
);
117 free(node
->children
);
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
;
127 return strcmp(trie
->strings
->buf
+ val1
->key_off
,
128 trie
->strings
->buf
+ val2
->key_off
);
131 static int trie_node_add_value(struct trie
*trie
, struct trie_node
*node
,
132 const char *key
, const char *value
) {
134 struct trie_value_entry
*val
;
136 k
= strbuf_add_string(trie
->strings
, key
, strlen(key
));
139 v
= strbuf_add_string(trie
->strings
, value
, strlen(value
));
143 if (node
->values_count
) {
144 struct trie_value_entry search
= {
149 val
= xbsearch_r(&search
, node
->values
, node
->values_count
, sizeof(struct trie_value_entry
), trie_values_cmp
, trie
);
151 /* replace existing earlier key with new value */
157 /* extend array, add new entry, sort for bisection */
158 val
= reallocarray(node
->values
, node
->values_count
+ 1, sizeof(struct trie_value_entry
));
161 trie
->values_count
++;
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
);
170 static int trie_insert(struct trie
*trie
, struct trie_node
*node
, const char *search
,
171 const char *key
, const char *value
) {
178 struct trie_node
*child
;
180 for (p
= 0; (c
= trie
->strings
->buf
[node
->prefix_off
+ p
]); p
++) {
181 _cleanup_free_
char *s
= NULL
;
183 _cleanup_free_
struct trie_node
*new_child
= NULL
;
185 if (c
== search
[i
+ p
])
189 new_child
= new0(struct trie_node
, 1);
193 /* move values from parent to child */
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
;
200 /* update parent; use strdup() because the source gets realloc()d */
201 s
= strndup(trie
->strings
->buf
+ node
->prefix_off
, p
);
205 off
= strbuf_add_string(trie
->strings
, s
, p
);
209 node
->prefix_off
= off
;
210 node
->children
= NULL
;
211 node
->children_count
= 0;
213 node
->values_count
= 0;
214 err
= node_add_child(trie
, node
, new_child
, c
);
218 new_child
= NULL
; /* avoid cleanup */
225 return trie_node_add_value(trie
, node
, key
, value
);
227 child
= node_lookup(node
, c
);
232 child
= new0(struct trie_node
, 1);
236 off
= strbuf_add_string(trie
->strings
, search
+ i
+1, strlen(search
+ i
+1));
242 child
->prefix_off
= off
;
243 err
= node_add_child(trie
, node
, child
, c
);
249 return trie_node_add_value(trie
, child
, key
, value
);
260 uint64_t strings_off
;
262 uint64_t nodes_count
;
263 uint64_t children_count
;
264 uint64_t values_count
;
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
) {
271 for (i
= 0; i
< node
->children_count
; i
++)
272 trie_store_nodes_size(trie
, node
->children
[i
].child
);
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
);
281 static int64_t trie_store_nodes(struct trie_f
*trie
, struct trie_node
*node
) {
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
),
288 struct trie_child_entry_f
*children
= NULL
;
291 if (node
->children_count
) {
292 children
= new0(struct trie_child_entry_f
, node
->children_count
);
297 /* post-order recursion */
298 for (i
= 0; i
< node
->children_count
; i
++) {
301 child_off
= trie_store_nodes(trie
, node
->children
[i
].child
);
306 children
[i
].c
= node
->children
[i
].c
;
307 children
[i
].child_off
= htole64(child_off
);
311 node_off
= ftello(trie
->f
);
312 fwrite(&n
, sizeof(struct trie_node_f
), 1, trie
->f
);
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
;
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
),
329 fwrite(&v
, sizeof(struct trie_value_entry_f
), 1, trie
->f
);
330 trie
->values_count
++;
336 static int trie_store(struct trie
*trie
, const char *filename
) {
340 _cleanup_free_
char *filename_tmp
= NULL
;
344 struct trie_header_f h
= {
345 .signature
= HWDB_SIG
,
346 .tool_version
= htole64(atoi(PACKAGE_VERSION
)),
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
)),
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
);
358 err
= fopen_temporary(filename
, &t
.f
, &filename_tmp
);
361 fchmod(fileno(t
.f
), 0444);
364 if (fseeko(t
.f
, sizeof(struct trie_header_f
), SEEK_SET
) < 0)
366 root_off
= trie_store_nodes(&t
, trie
->root
);
367 h
.nodes_root_off
= htole64(root_off
);
369 h
.nodes_len
= htole64(pos
- sizeof(struct trie_header_f
));
371 /* write string buffer */
372 fwrite(trie
->strings
->buf
, trie
->strings
->len
, 1, t
.f
);
373 h
.strings_len
= htole64(trie
->strings
->len
);
377 h
.file_size
= htole64(size
);
378 if (fseeko(t
.f
, 0, SEEK_SET
< 0))
380 fwrite(&h
, sizeof(struct trie_header_f
), 1, t
.f
);
386 if (fsync(fileno(t
.f
)) < 0)
388 if (rename(filename_tmp
, filename
) < 0)
391 /* write succeeded */
394 log_debug("=== trie on-disk ===");
395 log_debug("size: %8"PRIi64
" bytes", size
);
396 log_debug("header: %8zu bytes", sizeof(struct trie_header_f
));
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
);
411 unlink(filename_tmp
);
415 static int insert_data(struct trie
*trie
, struct udev_list
*match_list
,
416 char *line
, const char *filename
) {
418 struct udev_list_entry
*entry
;
420 value
= strchr(line
, '=');
422 log_error("Error, key/value pair expected but got '%s' in '%s':", line
, filename
);
429 /* libudev requires properties to start with a space */
430 while (isblank(line
[0]) && isblank(line
[1]))
433 if (line
[0] == '\0' || value
[0] == '\0') {
434 log_error("Error, empty key or value '%s' in '%s':", line
, filename
);
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
);
444 static int import_file(struct udev
*udev
, struct trie
*trie
, const char *filename
) {
452 struct udev_list match_list
;
454 udev_list_init(udev
, &match_list
, false);
456 f
= fopen(filename
, "re");
460 while (fgets(line
, sizeof(line
), f
)) {
468 /* strip trailing comment */
469 pos
= strchr(line
, '#');
473 /* strip trailing whitespace */
475 while (len
> 0 && isspace(line
[len
-1]))
484 if (line
[0] == ' ') {
485 log_error("Error, MATCH expected but got '%s' in '%s':", line
, filename
);
489 /* start of record, first match */
491 udev_list_entry_add(&match_list
, line
, NULL
);
496 log_error("Error, DATA expected but got empty line in '%s':", filename
);
498 udev_list_cleanup(&match_list
);
503 if (line
[0] != ' ') {
504 udev_list_entry_add(&match_list
, line
, NULL
);
510 insert_data(trie
, &match_list
, line
, filename
);
517 udev_list_cleanup(&match_list
);
521 if (line
[0] != ' ') {
522 log_error("Error, DATA expected but got '%s' in '%s':", line
, filename
);
524 udev_list_cleanup(&match_list
);
528 insert_data(trie
, &match_list
, line
, filename
);
534 udev_list_cleanup(&match_list
);
538 static void help(void) {
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"
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
);
552 static int adm_hwdb(struct udev
*udev
, int argc
, char *argv
[]) {
557 static const struct option options
[] = {
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' },
566 const char *test
= NULL
;
567 const char *root
= "";
568 const char *hwdb_bin_dir
= "/etc/udev";
570 struct trie
*trie
= NULL
;
572 int rc
= EXIT_SUCCESS
;
574 while ((c
= getopt_long(argc
, argv
, "ut:r:Vh", options
, NULL
)) >= 0)
580 hwdb_bin_dir
= UDEVLIBEXECDIR
;
597 assert_not_reached("Unknown option");
600 if (!update
&& !test
) {
601 log_error("Either --update or --test must be used");
607 _cleanup_free_
char *hwdb_bin
= NULL
;
609 trie
= new0(struct trie
, 1);
616 trie
->strings
= strbuf_new();
617 if (!trie
->strings
) {
623 trie
->root
= new0(struct trie_node
, 1);
630 err
= conf_files_list_strv(&files
, ".hwdb", root
, 0, conf_file_dirs
);
632 log_error_errno(err
, "failed to enumerate hwdb files: %m");
636 STRV_FOREACH(f
, files
) {
637 log_debug("reading file '%s'", *f
);
638 import_file(udev
, trie
, *f
);
642 strbuf_complete(trie
->strings
);
644 log_debug("=== trie in-memory ===");
645 log_debug("nodes: %8zu bytes (%8zu)",
646 trie
->nodes_count
* sizeof(struct trie_node
), trie
->nodes_count
);
647 log_debug("children arrays: %8zu bytes (%8zu)",
648 trie
->children_count
* sizeof(struct trie_child_entry
), trie
->children_count
);
649 log_debug("values arrays: %8zu bytes (%8zu)",
650 trie
->values_count
* sizeof(struct trie_value_entry
), trie
->values_count
);
651 log_debug("strings: %8zu bytes",
653 log_debug("strings incoming: %8zu bytes (%8zu)",
654 trie
->strings
->in_len
, trie
->strings
->in_count
);
655 log_debug("strings dedup'ed: %8zu bytes (%8zu)",
656 trie
->strings
->dedup_len
, trie
->strings
->dedup_count
);
658 hwdb_bin
= strjoin(root
, "/", hwdb_bin_dir
, "/hwdb.bin");
664 mkdir_parents_label(hwdb_bin
, 0755);
666 err
= trie_store(trie
, hwdb_bin
);
668 log_error_errno(err
, "Failure writing database %s: %m", hwdb_bin
);
672 (void) label_fix(hwdb_bin
, 0);
676 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
679 r
= sd_hwdb_new(&hwdb
);
681 const char *key
, *value
;
683 SD_HWDB_FOREACH_PROPERTY(hwdb
, test
, key
, value
)
684 printf("%s=%s\n", key
, value
);
690 trie_node_cleanup(trie
->root
);
691 strbuf_cleanup(trie
->strings
);
697 const struct udevadm_cmd udevadm_hwdb
= {