]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/hwdb-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "conf-files.h"
12 #include "hwdb-internal.h"
13 #include "hwdb-util.h"
15 #include "mkdir-label.h"
16 #include "nulstr-util.h"
17 #include "path-util.h"
18 #include "sort-util.h"
20 #include "string-util.h"
22 #include "tmpfile-util.h"
24 static const char *default_hwdb_bin_dir
= "/etc/udev";
25 static const char * const conf_file_dirs
[] = {
27 UDEVLIBEXECDIR
"/hwdb.d",
32 * Generic udev properties, key-value database based on modalias strings.
33 * Uses a Patricia/radix trie to index all matches for efficient lookup.
36 /* in-memory trie objects */
38 struct trie_node
*root
;
39 struct strbuf
*strings
;
42 size_t children_count
;
47 /* prefix, common part for all children of this node */
50 /* sorted array of pointers to children nodes */
51 struct trie_child_entry
*children
;
52 uint8_t children_count
;
54 /* sorted array of key-value pairs */
55 struct trie_value_entry
*values
;
59 /* children array item with char (0-255) index */
60 struct trie_child_entry
{
62 struct trie_node
*child
;
65 /* value array item with key-value pairs */
66 struct trie_value_entry
{
71 uint16_t file_priority
;
74 static int trie_children_cmp(const struct trie_child_entry
*a
, const struct trie_child_entry
*b
) {
75 return CMP(a
->c
, b
->c
);
78 static int node_add_child(struct trie
*trie
, struct trie_node
*node
, struct trie_node
*node_child
, uint8_t c
) {
79 struct trie_child_entry
*child
;
81 /* extend array, add new entry, sort for bisection */
82 child
= reallocarray(node
->children
, node
->children_count
+ 1, sizeof(struct trie_child_entry
));
86 node
->children
= child
;
87 trie
->children_count
++;
88 node
->children
[node
->children_count
].c
= c
;
89 node
->children
[node
->children_count
].child
= node_child
;
90 node
->children_count
++;
91 typesafe_qsort(node
->children
, node
->children_count
, trie_children_cmp
);
97 static struct trie_node
*node_lookup(const struct trie_node
*node
, uint8_t c
) {
98 struct trie_child_entry
*child
;
99 struct trie_child_entry search
;
102 child
= typesafe_bsearch(&search
, node
->children
, node
->children_count
, trie_children_cmp
);
108 static void trie_node_cleanup(struct trie_node
*node
) {
112 for (size_t i
= 0; i
< node
->children_count
; i
++)
113 trie_node_cleanup(node
->children
[i
].child
);
114 free(node
->children
);
119 static struct trie
* trie_free(struct trie
*trie
) {
123 trie_node_cleanup(trie
->root
);
124 strbuf_free(trie
->strings
);
128 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie
*, trie_free
);
130 static int trie_values_cmp(const struct trie_value_entry
*a
, const struct trie_value_entry
*b
, struct trie
*trie
) {
131 return strcmp(trie
->strings
->buf
+ a
->key_off
,
132 trie
->strings
->buf
+ b
->key_off
);
135 static int trie_node_add_value(struct trie
*trie
, struct trie_node
*node
,
136 const char *key
, const char *value
,
137 const char *filename
, uint16_t file_priority
, uint32_t line_number
, bool compat
) {
138 ssize_t k
, v
, fn
= 0;
139 struct trie_value_entry
*val
;
141 k
= strbuf_add_string(trie
->strings
, key
, strlen(key
));
144 v
= strbuf_add_string(trie
->strings
, value
, strlen(value
));
149 fn
= strbuf_add_string(trie
->strings
, filename
, strlen(filename
));
154 if (node
->values_count
) {
155 struct trie_value_entry search
= {
160 val
= typesafe_bsearch_r(&search
, node
->values
, node
->values_count
, trie_values_cmp
, trie
);
162 /* At this point we have 2 identical properties on the same match-string.
163 * Since we process files in order, we just replace the previous value. */
165 val
->filename_off
= fn
;
166 val
->file_priority
= file_priority
;
167 val
->line_number
= line_number
;
172 /* extend array, add new entry, sort for bisection */
173 val
= reallocarray(node
->values
, node
->values_count
+ 1, sizeof(struct trie_value_entry
));
176 trie
->values_count
++;
178 node
->values
[node
->values_count
] = (struct trie_value_entry
) {
182 .file_priority
= file_priority
,
183 .line_number
= line_number
,
185 node
->values_count
++;
186 typesafe_qsort_r(node
->values
, node
->values_count
, trie_values_cmp
, trie
);
190 static int trie_insert(struct trie
*trie
, struct trie_node
*node
, const char *search
,
191 const char *key
, const char *value
,
192 const char *filename
, uint16_t file_priority
, uint32_t line_number
, bool compat
) {
195 for (size_t i
= 0;; i
++) {
198 struct trie_node
*child
;
200 for (p
= 0; (c
= trie
->strings
->buf
[node
->prefix_off
+ p
]); p
++) {
201 _cleanup_free_
struct trie_node
*new_child
= NULL
;
202 _cleanup_free_
char *s
= NULL
;
205 if (c
== search
[i
+ p
])
209 new_child
= new(struct trie_node
, 1);
213 /* move values from parent to child */
214 *new_child
= (struct trie_node
) {
215 .prefix_off
= node
->prefix_off
+ p
+1,
216 .children
= node
->children
,
217 .children_count
= node
->children_count
,
218 .values
= node
->values
,
219 .values_count
= node
->values_count
,
222 /* update parent; use strdup() because the source gets realloc()d */
223 s
= strndup(trie
->strings
->buf
+ node
->prefix_off
, p
);
227 off
= strbuf_add_string(trie
->strings
, s
, p
);
231 *node
= (struct trie_node
) {
234 r
= node_add_child(trie
, node
, new_child
, c
);
238 new_child
= NULL
; /* avoid cleanup */
245 return trie_node_add_value(trie
, node
, key
, value
, filename
, file_priority
, line_number
, compat
);
247 child
= node_lookup(node
, c
);
249 _cleanup_free_
struct trie_node
*new_child
= NULL
;
253 new_child
= new(struct trie_node
, 1);
257 off
= strbuf_add_string(trie
->strings
, search
+ i
+1, strlen(search
+ i
+1));
261 *new_child
= (struct trie_node
) {
265 r
= node_add_child(trie
, node
, new_child
, c
);
269 child
= TAKE_PTR(new_child
);
270 return trie_node_add_value(trie
, child
, key
, value
, filename
, file_priority
, line_number
, compat
);
280 uint64_t strings_off
;
282 uint64_t nodes_count
;
283 uint64_t children_count
;
284 uint64_t values_count
;
287 /* calculate the storage space for the nodes, children arrays, value arrays */
288 static void trie_store_nodes_size(struct trie_f
*trie
, struct trie_node
*node
, bool compat
) {
289 for (uint64_t i
= 0; i
< node
->children_count
; i
++)
290 trie_store_nodes_size(trie
, node
->children
[i
].child
, compat
);
292 trie
->strings_off
+= sizeof(struct trie_node_f
);
293 for (uint64_t i
= 0; i
< node
->children_count
; i
++)
294 trie
->strings_off
+= sizeof(struct trie_child_entry_f
);
295 for (uint64_t i
= 0; i
< node
->values_count
; i
++)
296 trie
->strings_off
+= compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
);
299 static int64_t trie_store_nodes(struct trie_f
*trie
, struct trie_node
*node
, bool compat
) {
300 struct trie_node_f n
= {
301 .prefix_off
= htole64(trie
->strings_off
+ node
->prefix_off
),
302 .children_count
= node
->children_count
,
303 .values_count
= htole64(node
->values_count
),
305 _cleanup_free_
struct trie_child_entry_f
*children
= NULL
;
308 if (node
->children_count
) {
309 children
= new(struct trie_child_entry_f
, node
->children_count
);
314 /* post-order recursion */
315 for (uint64_t i
= 0; i
< node
->children_count
; i
++) {
318 child_off
= trie_store_nodes(trie
, node
->children
[i
].child
, compat
);
322 children
[i
] = (struct trie_child_entry_f
) {
323 .c
= node
->children
[i
].c
,
324 .child_off
= htole64(child_off
),
329 node_off
= ftello(trie
->f
);
330 fwrite(&n
, sizeof(struct trie_node_f
), 1, trie
->f
);
333 /* append children array */
334 if (node
->children_count
) {
335 fwrite(children
, sizeof(struct trie_child_entry_f
), node
->children_count
, trie
->f
);
336 trie
->children_count
+= node
->children_count
;
339 /* append values array */
340 for (uint64_t i
= 0; i
< node
->values_count
; i
++) {
341 struct trie_value_entry2_f v
= {
342 .key_off
= htole64(trie
->strings_off
+ node
->values
[i
].key_off
),
343 .value_off
= htole64(trie
->strings_off
+ node
->values
[i
].value_off
),
344 .filename_off
= htole64(trie
->strings_off
+ node
->values
[i
].filename_off
),
345 .line_number
= htole32(node
->values
[i
].line_number
),
346 .file_priority
= htole16(node
->values
[i
].file_priority
),
349 fwrite(&v
, compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
), 1, trie
->f
);
351 trie
->values_count
+= node
->values_count
;
356 static int trie_store(struct trie
*trie
, const char *filename
, bool compat
) {
360 _cleanup_free_
char *filename_tmp
= NULL
;
364 struct trie_header_f h
= {
365 .signature
= HWDB_SIG
,
366 .tool_version
= htole64(PROJECT_VERSION
),
367 .header_size
= htole64(sizeof(struct trie_header_f
)),
368 .node_size
= htole64(sizeof(struct trie_node_f
)),
369 .child_entry_size
= htole64(sizeof(struct trie_child_entry_f
)),
370 .value_entry_size
= htole64(compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
)),
374 /* calculate size of header, nodes, children entries, value entries */
375 t
.strings_off
= sizeof(struct trie_header_f
);
376 trie_store_nodes_size(&t
, trie
->root
, compat
);
378 r
= fopen_temporary(filename
, &t
.f
, &filename_tmp
);
381 (void) fchmod(fileno(t
.f
), 0444);
384 if (fseeko(t
.f
, sizeof(struct trie_header_f
), SEEK_SET
) < 0)
387 root_off
= trie_store_nodes(&t
, trie
->root
, compat
);
388 h
.nodes_root_off
= htole64(root_off
);
390 h
.nodes_len
= htole64(pos
- sizeof(struct trie_header_f
));
392 /* write string buffer */
393 fwrite(trie
->strings
->buf
, trie
->strings
->len
, 1, t
.f
);
394 h
.strings_len
= htole64(trie
->strings
->len
);
398 h
.file_size
= htole64(size
);
399 if (fseeko(t
.f
, 0, SEEK_SET
) < 0)
401 fwrite(&h
, sizeof(struct trie_header_f
), 1, t
.f
);
407 if (fsync(fileno(t
.f
)) < 0)
409 if (rename(filename_tmp
, filename
) < 0)
412 /* write succeeded */
415 log_debug("=== trie on-disk ===");
416 log_debug("size: %8"PRIi64
" bytes", size
);
417 log_debug("header: %8zu bytes", sizeof(struct trie_header_f
));
418 log_debug("nodes: %8"PRIu64
" bytes (%8"PRIu64
")",
419 t
.nodes_count
* sizeof(struct trie_node_f
), t
.nodes_count
);
420 log_debug("child pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
421 t
.children_count
* sizeof(struct trie_child_entry_f
), t
.children_count
);
422 log_debug("value pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
423 t
.values_count
* (compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
)), t
.values_count
);
424 log_debug("string store: %8zu bytes", trie
->strings
->len
);
425 log_debug("strings start: %8"PRIu64
, t
.strings_off
);
431 (void) unlink(filename_tmp
);
435 static int insert_data(struct trie
*trie
, char **match_list
, char *line
, const char *filename
,
436 uint16_t file_priority
, uint32_t line_number
, bool compat
) {
439 assert(line
[0] == ' ');
441 value
= strchr(line
, '=');
443 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
444 "Key-value pair expected but got \"%s\", ignoring.", line
);
449 /* Replace multiple leading spaces by a single space */
450 while (isblank(line
[0]) && isblank(line
[1]))
453 if (isempty(line
+ 1))
454 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
455 "Empty key in \"%s=%s\", ignoring.",
458 STRV_FOREACH(entry
, match_list
)
459 trie_insert(trie
, trie
->root
, *entry
, line
, value
, filename
, file_priority
, line_number
, compat
);
464 static int import_file(struct trie
*trie
, const char *filename
, uint16_t file_priority
, bool compat
) {
470 _cleanup_fclose_
FILE *f
= NULL
;
471 _cleanup_strv_free_
char **match_list
= NULL
;
472 uint32_t line_number
= 0;
475 f
= fopen(filename
, "re");
480 _cleanup_free_
char *line
= NULL
;
484 r
= read_line_full(f
, LONG_LINE_MAX
, READ_LINE_NOT_A_TTY
, &line
);
496 /* strip trailing comment */
497 pos
= strchr(line
, '#');
501 /* strip trailing whitespace */
503 while (len
> 0 && isspace(line
[len
-1]))
512 if (line
[0] == ' ') {
513 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
514 "Match expected but got indented property \"%s\", ignoring line.", line
);
518 /* start of record, first match */
521 err
= strv_extend(&match_list
, line
);
529 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
530 "Property expected, ignoring record with no properties.");
532 match_list
= strv_free(match_list
);
536 if (line
[0] != ' ') {
538 err
= strv_extend(&match_list
, line
);
547 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
, compat
);
556 match_list
= strv_free(match_list
);
560 if (line
[0] != ' ') {
561 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
562 "Property or empty line expected, got \"%s\", ignoring record.", line
);
564 match_list
= strv_free(match_list
);
568 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
, compat
);
575 if (state
== HW_MATCH
)
576 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, 0,
577 "Property expected, ignoring record with no properties.");
582 int hwdb_update(const char *root
, const char *hwdb_bin_dir
, bool strict
, bool compat
) {
583 _cleanup_free_
char *hwdb_bin
= NULL
;
584 _cleanup_(trie_freep
) struct trie
*trie
= NULL
;
585 _cleanup_strv_free_
char **files
= NULL
;
586 uint16_t file_priority
= 1;
589 /* The argument 'compat' controls the format version of database. If false, then hwdb.bin will be
590 * created with additional information such that priority, line number, and filename of database
591 * source. If true, then hwdb.bin will be created without the information. systemd-hwdb command
592 * should set the argument false, and 'udevadm hwdb' command should set it true. */
594 trie
= new0(struct trie
, 1);
599 trie
->strings
= strbuf_new();
604 trie
->root
= new0(struct trie_node
, 1);
610 err
= conf_files_list_strv(&files
, ".hwdb", root
, 0, conf_file_dirs
);
612 return log_error_errno(err
, "Failed to enumerate hwdb files: %m");
614 STRV_FOREACH(f
, files
) {
615 log_debug("Reading file \"%s\"", *f
);
616 err
= import_file(trie
, *f
, file_priority
++, compat
);
617 if (err
< 0 && strict
)
621 strbuf_complete(trie
->strings
);
623 log_debug("=== trie in-memory ===");
624 log_debug("nodes: %8zu bytes (%8zu)",
625 trie
->nodes_count
* sizeof(struct trie_node
), trie
->nodes_count
);
626 log_debug("children arrays: %8zu bytes (%8zu)",
627 trie
->children_count
* sizeof(struct trie_child_entry
), trie
->children_count
);
628 log_debug("values arrays: %8zu bytes (%8zu)",
629 trie
->values_count
* sizeof(struct trie_value_entry
), trie
->values_count
);
630 log_debug("strings: %8zu bytes",
632 log_debug("strings incoming: %8zu bytes (%8zu)",
633 trie
->strings
->in_len
, trie
->strings
->in_count
);
634 log_debug("strings dedup'ed: %8zu bytes (%8zu)",
635 trie
->strings
->dedup_len
, trie
->strings
->dedup_count
);
637 hwdb_bin
= path_join(root
, hwdb_bin_dir
?: default_hwdb_bin_dir
, "hwdb.bin");
641 (void) mkdir_parents_label(hwdb_bin
, 0755);
642 err
= trie_store(trie
, hwdb_bin
, compat
);
644 return log_error_errno(err
, "Failed to write database %s: %m", hwdb_bin
);
646 err
= label_fix(hwdb_bin
, 0);
653 int hwdb_query(const char *modalias
) {
654 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
655 const char *key
, *value
;
660 r
= sd_hwdb_new(&hwdb
);
664 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
)
665 printf("%s=%s\n", key
, value
);
670 bool hwdb_validate(sd_hwdb
*hwdb
) {
680 /* if hwdb.bin doesn't exist anywhere, we need to update */
681 NULSTR_FOREACH(p
, hwdb_bin_paths
)
682 if (stat(p
, &st
) >= 0) {
689 if (timespec_load(&hwdb
->st
.st_mtim
) != timespec_load(&st
.st_mtim
))