]>
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* const conf_file_dirs
[] = {
26 UDEVLIBEXECDIR
"/hwdb.d",
31 * Generic udev properties, key-value database based on modalias strings.
32 * Uses a Patricia/radix trie to index all matches for efficient lookup.
35 /* in-memory trie objects */
37 struct trie_node
*root
;
38 struct strbuf
*strings
;
41 size_t children_count
;
46 /* prefix, common part for all children of this node */
49 /* sorted array of pointers to children nodes */
50 struct trie_child_entry
*children
;
51 uint8_t children_count
;
53 /* sorted array of key-value pairs */
54 struct trie_value_entry
*values
;
58 /* children array item with char (0-255) index */
59 struct trie_child_entry
{
61 struct trie_node
*child
;
64 /* value array item with key-value pairs */
65 struct trie_value_entry
{
70 uint16_t file_priority
;
73 static int trie_children_cmp(const struct trie_child_entry
*a
, const struct trie_child_entry
*b
) {
74 return CMP(a
->c
, b
->c
);
77 static int node_add_child(struct trie
*trie
, struct trie_node
*node
, struct trie_node
*node_child
, uint8_t c
) {
78 struct trie_child_entry
*child
;
80 /* extend array, add new entry, sort for bisection */
81 child
= reallocarray(node
->children
, node
->children_count
+ 1, sizeof(struct trie_child_entry
));
85 node
->children
= child
;
86 trie
->children_count
++;
87 node
->children
[node
->children_count
].c
= c
;
88 node
->children
[node
->children_count
].child
= node_child
;
89 node
->children_count
++;
90 typesafe_qsort(node
->children
, node
->children_count
, trie_children_cmp
);
96 static struct trie_node
*node_lookup(const struct trie_node
*node
, uint8_t c
) {
97 struct trie_child_entry
*child
;
98 struct trie_child_entry search
;
101 child
= typesafe_bsearch(&search
, node
->children
, node
->children_count
, trie_children_cmp
);
107 static void trie_node_cleanup(struct trie_node
*node
) {
111 for (size_t i
= 0; i
< node
->children_count
; i
++)
112 trie_node_cleanup(node
->children
[i
].child
);
113 free(node
->children
);
118 static struct trie
* trie_free(struct trie
*trie
) {
122 trie_node_cleanup(trie
->root
);
123 strbuf_free(trie
->strings
);
127 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie
*, trie_free
);
129 static int trie_values_cmp(const struct trie_value_entry
*a
, const struct trie_value_entry
*b
, struct trie
*trie
) {
130 return strcmp(trie
->strings
->buf
+ a
->key_off
,
131 trie
->strings
->buf
+ b
->key_off
);
134 static int trie_node_add_value(struct trie
*trie
, struct trie_node
*node
,
135 const char *key
, const char *value
,
136 const char *filename
, uint16_t file_priority
, uint32_t line_number
, bool compat
) {
137 ssize_t k
, v
, fn
= 0;
138 struct trie_value_entry
*val
;
140 k
= strbuf_add_string(trie
->strings
, key
, strlen(key
));
143 v
= strbuf_add_string(trie
->strings
, value
, strlen(value
));
148 fn
= strbuf_add_string(trie
->strings
, filename
, strlen(filename
));
153 if (node
->values_count
) {
154 struct trie_value_entry search
= {
159 val
= typesafe_bsearch_r(&search
, node
->values
, node
->values_count
, trie_values_cmp
, trie
);
161 /* At this point we have 2 identical properties on the same match-string.
162 * Since we process files in order, we just replace the previous value. */
164 val
->filename_off
= fn
;
165 val
->file_priority
= file_priority
;
166 val
->line_number
= line_number
;
171 /* extend array, add new entry, sort for bisection */
172 val
= reallocarray(node
->values
, node
->values_count
+ 1, sizeof(struct trie_value_entry
));
175 trie
->values_count
++;
177 node
->values
[node
->values_count
] = (struct trie_value_entry
) {
181 .file_priority
= file_priority
,
182 .line_number
= line_number
,
184 node
->values_count
++;
185 typesafe_qsort_r(node
->values
, node
->values_count
, trie_values_cmp
, trie
);
189 static int trie_insert(struct trie
*trie
, struct trie_node
*node
, const char *search
,
190 const char *key
, const char *value
,
191 const char *filename
, uint16_t file_priority
, uint32_t line_number
, bool compat
) {
194 for (size_t i
= 0;; i
++) {
197 struct trie_node
*child
;
199 for (p
= 0; (c
= trie
->strings
->buf
[node
->prefix_off
+ p
]); p
++) {
200 _cleanup_free_
struct trie_node
*new_child
= NULL
;
201 _cleanup_free_
char *s
= NULL
;
204 if (c
== search
[i
+ p
])
208 new_child
= new(struct trie_node
, 1);
212 /* move values from parent to child */
213 *new_child
= (struct trie_node
) {
214 .prefix_off
= node
->prefix_off
+ p
+1,
215 .children
= node
->children
,
216 .children_count
= node
->children_count
,
217 .values
= node
->values
,
218 .values_count
= node
->values_count
,
221 /* update parent; use strdup() because the source gets realloc()d */
222 s
= strndup(trie
->strings
->buf
+ node
->prefix_off
, p
);
226 off
= strbuf_add_string(trie
->strings
, s
, p
);
230 *node
= (struct trie_node
) {
233 r
= node_add_child(trie
, node
, new_child
, c
);
237 new_child
= NULL
; /* avoid cleanup */
244 return trie_node_add_value(trie
, node
, key
, value
, filename
, file_priority
, line_number
, compat
);
246 child
= node_lookup(node
, c
);
248 _cleanup_free_
struct trie_node
*new_child
= NULL
;
252 new_child
= new(struct trie_node
, 1);
256 off
= strbuf_add_string(trie
->strings
, search
+ i
+1, strlen(search
+ i
+1));
260 *new_child
= (struct trie_node
) {
264 r
= node_add_child(trie
, node
, new_child
, c
);
268 child
= TAKE_PTR(new_child
);
269 return trie_node_add_value(trie
, child
, key
, value
, filename
, file_priority
, line_number
, compat
);
279 uint64_t strings_off
;
281 uint64_t nodes_count
;
282 uint64_t children_count
;
283 uint64_t values_count
;
286 /* calculate the storage space for the nodes, children arrays, value arrays */
287 static void trie_store_nodes_size(struct trie_f
*trie
, struct trie_node
*node
, bool compat
) {
288 for (uint64_t i
= 0; i
< node
->children_count
; i
++)
289 trie_store_nodes_size(trie
, node
->children
[i
].child
, compat
);
291 trie
->strings_off
+= sizeof(struct trie_node_f
);
292 for (uint64_t i
= 0; i
< node
->children_count
; i
++)
293 trie
->strings_off
+= sizeof(struct trie_child_entry_f
);
294 for (uint64_t i
= 0; i
< node
->values_count
; i
++)
295 trie
->strings_off
+= compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
);
298 static int64_t trie_store_nodes(struct trie_f
*trie
, struct trie_node
*node
, bool compat
) {
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
),
304 _cleanup_free_
struct trie_child_entry_f
*children
= NULL
;
307 if (node
->children_count
) {
308 children
= new(struct trie_child_entry_f
, node
->children_count
);
313 /* post-order recursion */
314 for (uint64_t i
= 0; i
< node
->children_count
; i
++) {
317 child_off
= trie_store_nodes(trie
, node
->children
[i
].child
, compat
);
321 children
[i
] = (struct trie_child_entry_f
) {
322 .c
= node
->children
[i
].c
,
323 .child_off
= htole64(child_off
),
328 node_off
= ftello(trie
->f
);
329 fwrite(&n
, sizeof(struct trie_node_f
), 1, trie
->f
);
332 /* append children array */
333 if (node
->children_count
) {
334 fwrite(children
, sizeof(struct trie_child_entry_f
), node
->children_count
, trie
->f
);
335 trie
->children_count
+= node
->children_count
;
338 /* append values array */
339 for (uint64_t i
= 0; i
< node
->values_count
; i
++) {
340 struct trie_value_entry2_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 .filename_off
= htole64(trie
->strings_off
+ node
->values
[i
].filename_off
),
344 .line_number
= htole32(node
->values
[i
].line_number
),
345 .file_priority
= htole16(node
->values
[i
].file_priority
),
348 fwrite(&v
, compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
), 1, trie
->f
);
350 trie
->values_count
+= node
->values_count
;
355 static int trie_store(struct trie
*trie
, const char *filename
, bool compat
) {
359 _cleanup_free_
char *filename_tmp
= NULL
;
363 struct trie_header_f h
= {
364 .signature
= HWDB_SIG
,
365 .tool_version
= htole64(PROJECT_VERSION
),
366 .header_size
= htole64(sizeof(struct trie_header_f
)),
367 .node_size
= htole64(sizeof(struct trie_node_f
)),
368 .child_entry_size
= htole64(sizeof(struct trie_child_entry_f
)),
369 .value_entry_size
= htole64(compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
)),
373 /* calculate size of header, nodes, children entries, value entries */
374 t
.strings_off
= sizeof(struct trie_header_f
);
375 trie_store_nodes_size(&t
, trie
->root
, compat
);
377 r
= fopen_temporary(filename
, &t
.f
, &filename_tmp
);
380 (void) fchmod(fileno(t
.f
), 0444);
383 if (fseeko(t
.f
, sizeof(struct trie_header_f
), SEEK_SET
) < 0)
386 root_off
= trie_store_nodes(&t
, trie
->root
, compat
);
387 h
.nodes_root_off
= htole64(root_off
);
389 h
.nodes_len
= htole64(pos
- sizeof(struct trie_header_f
));
391 /* write string buffer */
392 fwrite(trie
->strings
->buf
, trie
->strings
->len
, 1, t
.f
);
393 h
.strings_len
= htole64(trie
->strings
->len
);
397 h
.file_size
= htole64(size
);
398 if (fseeko(t
.f
, 0, SEEK_SET
) < 0)
400 fwrite(&h
, sizeof(struct trie_header_f
), 1, t
.f
);
406 if (fsync(fileno(t
.f
)) < 0)
408 if (rename(filename_tmp
, filename
) < 0)
411 /* write succeeded */
414 log_debug("=== trie on-disk ===");
415 log_debug("size: %8"PRIi64
" bytes", size
);
416 log_debug("header: %8zu bytes", sizeof(struct trie_header_f
));
417 log_debug("nodes: %8"PRIu64
" bytes (%8"PRIu64
")",
418 t
.nodes_count
* sizeof(struct trie_node_f
), t
.nodes_count
);
419 log_debug("child pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
420 t
.children_count
* sizeof(struct trie_child_entry_f
), t
.children_count
);
421 log_debug("value pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
422 t
.values_count
* (compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
)), t
.values_count
);
423 log_debug("string store: %8zu bytes", trie
->strings
->len
);
424 log_debug("strings start: %8"PRIu64
, t
.strings_off
);
430 (void) unlink(filename_tmp
);
434 static int insert_data(struct trie
*trie
, char **match_list
, char *line
, const char *filename
,
435 uint16_t file_priority
, uint32_t line_number
, bool compat
) {
438 assert(line
[0] == ' ');
440 value
= strchr(line
, '=');
442 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
443 "Key-value pair expected but got \"%s\", ignoring.", line
);
448 /* Replace multiple leading spaces by a single space */
449 while (isblank(line
[0]) && isblank(line
[1]))
452 if (isempty(line
+ 1))
453 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
454 "Empty key in \"%s=%s\", ignoring.",
457 STRV_FOREACH(entry
, match_list
)
458 trie_insert(trie
, trie
->root
, *entry
, line
, value
, filename
, file_priority
, line_number
, compat
);
463 static int import_file(struct trie
*trie
, const char *filename
, uint16_t file_priority
, bool compat
) {
469 _cleanup_fclose_
FILE *f
= NULL
;
470 _cleanup_strv_free_
char **match_list
= NULL
;
471 uint32_t line_number
= 0;
474 f
= fopen(filename
, "re");
479 _cleanup_free_
char *line
= NULL
;
483 r
= read_line_full(f
, LONG_LINE_MAX
, READ_LINE_NOT_A_TTY
, &line
);
495 /* strip trailing comment */
496 pos
= strchr(line
, '#');
500 /* strip trailing whitespace */
502 while (len
> 0 && isspace(line
[len
-1]))
511 if (line
[0] == ' ') {
512 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
513 "Match expected but got indented property \"%s\", ignoring line.", line
);
517 /* start of record, first match */
520 err
= strv_extend(&match_list
, line
);
528 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
529 "Property expected, ignoring record with no properties.");
531 match_list
= strv_free(match_list
);
535 if (line
[0] != ' ') {
537 err
= strv_extend(&match_list
, line
);
546 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
, compat
);
555 match_list
= strv_free(match_list
);
559 if (line
[0] != ' ') {
560 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
561 "Property or empty line expected, got \"%s\", ignoring record.", line
);
563 match_list
= strv_free(match_list
);
567 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
, compat
);
574 if (state
== HW_MATCH
)
575 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, 0,
576 "Property expected, ignoring record with no properties.");
581 int hwdb_update(const char *root
, const char *hwdb_bin_dir
, bool strict
, bool compat
) {
582 _cleanup_free_
char *hwdb_bin
= NULL
;
583 _cleanup_(trie_freep
) struct trie
*trie
= NULL
;
584 _cleanup_strv_free_
char **files
= NULL
;
585 uint16_t file_priority
= 1;
588 /* The argument 'compat' controls the format version of database. If false, then hwdb.bin will be
589 * created with additional information such that priority, line number, and filename of database
590 * source. If true, then hwdb.bin will be created without the information. systemd-hwdb command
591 * should set the argument false, and 'udevadm hwdb' command should set it true. */
593 trie
= new0(struct trie
, 1);
598 trie
->strings
= strbuf_new();
603 trie
->root
= new0(struct trie_node
, 1);
609 err
= conf_files_list_strv(&files
, ".hwdb", root
, 0, conf_file_dirs
);
611 return log_error_errno(err
, "Failed to enumerate hwdb files: %m");
613 STRV_FOREACH(f
, files
) {
614 log_debug("Reading file \"%s\"", *f
);
615 err
= import_file(trie
, *f
, file_priority
++, compat
);
616 if (err
< 0 && strict
)
620 strbuf_complete(trie
->strings
);
622 log_debug("=== trie in-memory ===");
623 log_debug("nodes: %8zu bytes (%8zu)",
624 trie
->nodes_count
* sizeof(struct trie_node
), trie
->nodes_count
);
625 log_debug("children arrays: %8zu bytes (%8zu)",
626 trie
->children_count
* sizeof(struct trie_child_entry
), trie
->children_count
);
627 log_debug("values arrays: %8zu bytes (%8zu)",
628 trie
->values_count
* sizeof(struct trie_value_entry
), trie
->values_count
);
629 log_debug("strings: %8zu bytes",
631 log_debug("strings incoming: %8zu bytes (%8zu)",
632 trie
->strings
->in_len
, trie
->strings
->in_count
);
633 log_debug("strings dedup'ed: %8zu bytes (%8zu)",
634 trie
->strings
->dedup_len
, trie
->strings
->dedup_count
);
636 hwdb_bin
= path_join(root
, hwdb_bin_dir
?: "/etc/udev", "hwdb.bin");
640 (void) mkdir_parents_label(hwdb_bin
, 0755);
641 err
= trie_store(trie
, hwdb_bin
, compat
);
643 return log_error_errno(err
, "Failed to write database %s: %m", hwdb_bin
);
645 err
= label_fix(hwdb_bin
, 0);
652 int hwdb_query(const char *modalias
, const char *root
) {
653 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
654 const char *key
, *value
;
660 NULSTR_FOREACH(p
, hwdb_bin_paths
) {
661 _cleanup_free_
char *hwdb_bin
= NULL
;
663 hwdb_bin
= path_join(root
, p
);
667 r
= sd_hwdb_new_from_path(hwdb_bin
, &hwdb
);
672 r
= sd_hwdb_new(&hwdb
);
676 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
)
677 printf("%s=%s\n", key
, value
);
682 bool hwdb_should_reload(sd_hwdb
*hwdb
) {
691 /* if hwdb.bin doesn't exist anywhere, we need to update */
692 NULSTR_FOREACH(p
, hwdb_bin_paths
)
693 if (stat(p
, &st
) >= 0) {
700 if (timespec_load(&hwdb
->st
.st_mtim
) != timespec_load(&st
.st_mtim
))