]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-hwdb/hwdb-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "conf-files.h"
12 #include "hwdb-internal.h"
13 #include "hwdb-util.h"
16 #include "path-util.h"
17 #include "sort-util.h"
19 #include "string-util.h"
21 #include "tmpfile-util.h"
23 static const char *default_hwdb_bin_dir
= "/etc/udev";
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
) {
113 for (i
= 0; i
< node
->children_count
; i
++)
114 trie_node_cleanup(node
->children
[i
].child
);
115 free(node
->children
);
120 static void trie_free(struct trie
*trie
) {
124 trie_node_cleanup(trie
->root
);
125 strbuf_cleanup(trie
->strings
);
129 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie
*, trie_free
);
131 static int trie_values_cmp(const struct trie_value_entry
*a
, const struct trie_value_entry
*b
, struct trie
*trie
) {
132 return strcmp(trie
->strings
->buf
+ a
->key_off
,
133 trie
->strings
->buf
+ b
->key_off
);
136 static int trie_node_add_value(struct trie
*trie
, struct trie_node
*node
,
137 const char *key
, const char *value
,
138 const char *filename
, uint16_t file_priority
, uint32_t line_number
, bool compat
) {
139 ssize_t k
, v
, fn
= 0;
140 struct trie_value_entry
*val
;
142 k
= strbuf_add_string(trie
->strings
, key
, strlen(key
));
145 v
= strbuf_add_string(trie
->strings
, value
, strlen(value
));
150 fn
= strbuf_add_string(trie
->strings
, filename
, strlen(filename
));
155 if (node
->values_count
) {
156 struct trie_value_entry search
= {
161 val
= typesafe_bsearch_r(&search
, node
->values
, node
->values_count
, trie_values_cmp
, trie
);
163 /* At this point we have 2 identical properties on the same match-string.
164 * Since we process files in order, we just replace the previous value. */
166 val
->filename_off
= fn
;
167 val
->file_priority
= file_priority
;
168 val
->line_number
= line_number
;
173 /* extend array, add new entry, sort for bisection */
174 val
= reallocarray(node
->values
, node
->values_count
+ 1, sizeof(struct trie_value_entry
));
177 trie
->values_count
++;
179 node
->values
[node
->values_count
] = (struct trie_value_entry
) {
183 .file_priority
= file_priority
,
184 .line_number
= line_number
,
186 node
->values_count
++;
187 typesafe_qsort_r(node
->values
, node
->values_count
, trie_values_cmp
, trie
);
191 static int trie_insert(struct trie
*trie
, struct trie_node
*node
, const char *search
,
192 const char *key
, const char *value
,
193 const char *filename
, uint16_t file_priority
, uint32_t line_number
, bool compat
) {
200 struct trie_node
*child
;
202 for (p
= 0; (c
= trie
->strings
->buf
[node
->prefix_off
+ p
]); p
++) {
203 _cleanup_free_
struct trie_node
*new_child
= NULL
;
204 _cleanup_free_
char *s
= NULL
;
207 if (c
== search
[i
+ p
])
211 new_child
= new(struct trie_node
, 1);
215 /* move values from parent to child */
216 *new_child
= (struct trie_node
) {
217 .prefix_off
= node
->prefix_off
+ p
+1,
218 .children
= node
->children
,
219 .children_count
= node
->children_count
,
220 .values
= node
->values
,
221 .values_count
= node
->values_count
,
224 /* update parent; use strdup() because the source gets realloc()d */
225 s
= strndup(trie
->strings
->buf
+ node
->prefix_off
, p
);
229 off
= strbuf_add_string(trie
->strings
, s
, p
);
233 *node
= (struct trie_node
) {
236 r
= node_add_child(trie
, node
, new_child
, c
);
240 new_child
= NULL
; /* avoid cleanup */
247 return trie_node_add_value(trie
, node
, key
, value
, filename
, file_priority
, line_number
, compat
);
249 child
= node_lookup(node
, c
);
251 _cleanup_free_
struct trie_node
*new_child
= NULL
;
255 new_child
= new(struct trie_node
, 1);
259 off
= strbuf_add_string(trie
->strings
, search
+ i
+1, strlen(search
+ i
+1));
263 *new_child
= (struct trie_node
) {
267 r
= node_add_child(trie
, node
, new_child
, c
);
271 child
= TAKE_PTR(new_child
);
272 return trie_node_add_value(trie
, child
, key
, value
, filename
, file_priority
, line_number
, compat
);
283 uint64_t strings_off
;
285 uint64_t nodes_count
;
286 uint64_t children_count
;
287 uint64_t values_count
;
290 /* calculate the storage space for the nodes, children arrays, value arrays */
291 static void trie_store_nodes_size(struct trie_f
*trie
, struct trie_node
*node
, bool compat
) {
294 for (i
= 0; i
< node
->children_count
; i
++)
295 trie_store_nodes_size(trie
, node
->children
[i
].child
, compat
);
297 trie
->strings_off
+= sizeof(struct trie_node_f
);
298 for (i
= 0; i
< node
->children_count
; i
++)
299 trie
->strings_off
+= sizeof(struct trie_child_entry_f
);
300 for (i
= 0; i
< node
->values_count
; i
++)
301 trie
->strings_off
+= compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
);
304 static int64_t trie_store_nodes(struct trie_f
*trie
, struct trie_node
*node
, bool compat
) {
306 struct trie_node_f n
= {
307 .prefix_off
= htole64(trie
->strings_off
+ node
->prefix_off
),
308 .children_count
= node
->children_count
,
309 .values_count
= htole64(node
->values_count
),
311 _cleanup_free_
struct trie_child_entry_f
*children
= NULL
;
314 if (node
->children_count
) {
315 children
= new(struct trie_child_entry_f
, node
->children_count
);
320 /* post-order recursion */
321 for (i
= 0; i
< node
->children_count
; i
++) {
324 child_off
= trie_store_nodes(trie
, node
->children
[i
].child
, compat
);
328 children
[i
] = (struct trie_child_entry_f
) {
329 .c
= node
->children
[i
].c
,
330 .child_off
= htole64(child_off
),
335 node_off
= ftello(trie
->f
);
336 fwrite(&n
, sizeof(struct trie_node_f
), 1, trie
->f
);
339 /* append children array */
340 if (node
->children_count
) {
341 fwrite(children
, sizeof(struct trie_child_entry_f
), node
->children_count
, trie
->f
);
342 trie
->children_count
+= node
->children_count
;
345 /* append values array */
346 for (i
= 0; i
< node
->values_count
; i
++) {
347 struct trie_value_entry2_f v
= {
348 .key_off
= htole64(trie
->strings_off
+ node
->values
[i
].key_off
),
349 .value_off
= htole64(trie
->strings_off
+ node
->values
[i
].value_off
),
350 .filename_off
= htole64(trie
->strings_off
+ node
->values
[i
].filename_off
),
351 .line_number
= htole32(node
->values
[i
].line_number
),
352 .file_priority
= htole16(node
->values
[i
].file_priority
),
355 fwrite(&v
, compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
), 1, trie
->f
);
357 trie
->values_count
+= node
->values_count
;
362 static int trie_store(struct trie
*trie
, const char *filename
, bool compat
) {
366 _cleanup_free_
char *filename_tmp
= NULL
;
370 struct trie_header_f h
= {
371 .signature
= HWDB_SIG
,
372 .tool_version
= htole64(PROJECT_VERSION
),
373 .header_size
= htole64(sizeof(struct trie_header_f
)),
374 .node_size
= htole64(sizeof(struct trie_node_f
)),
375 .child_entry_size
= htole64(sizeof(struct trie_child_entry_f
)),
376 .value_entry_size
= htole64(compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
)),
380 /* calculate size of header, nodes, children entries, value entries */
381 t
.strings_off
= sizeof(struct trie_header_f
);
382 trie_store_nodes_size(&t
, trie
->root
, compat
);
384 r
= fopen_temporary(filename
, &t
.f
, &filename_tmp
);
387 fchmod(fileno(t
.f
), 0444);
390 if (fseeko(t
.f
, sizeof(struct trie_header_f
), SEEK_SET
) < 0)
393 root_off
= trie_store_nodes(&t
, trie
->root
, compat
);
394 h
.nodes_root_off
= htole64(root_off
);
396 h
.nodes_len
= htole64(pos
- sizeof(struct trie_header_f
));
398 /* write string buffer */
399 fwrite(trie
->strings
->buf
, trie
->strings
->len
, 1, t
.f
);
400 h
.strings_len
= htole64(trie
->strings
->len
);
404 h
.file_size
= htole64(size
);
405 if (fseeko(t
.f
, 0, SEEK_SET
) < 0)
407 fwrite(&h
, sizeof(struct trie_header_f
), 1, t
.f
);
413 if (fsync(fileno(t
.f
)) < 0)
415 if (rename(filename_tmp
, filename
) < 0)
418 /* write succeeded */
421 log_debug("=== trie on-disk ===");
422 log_debug("size: %8"PRIi64
" bytes", size
);
423 log_debug("header: %8zu bytes", sizeof(struct trie_header_f
));
424 log_debug("nodes: %8"PRIu64
" bytes (%8"PRIu64
")",
425 t
.nodes_count
* sizeof(struct trie_node_f
), t
.nodes_count
);
426 log_debug("child pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
427 t
.children_count
* sizeof(struct trie_child_entry_f
), t
.children_count
);
428 log_debug("value pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
429 t
.values_count
* (compat
? sizeof(struct trie_value_entry_f
) : sizeof(struct trie_value_entry2_f
)), t
.values_count
);
430 log_debug("string store: %8zu bytes", trie
->strings
->len
);
431 log_debug("strings start: %8"PRIu64
, t
.strings_off
);
437 (void) unlink(filename_tmp
);
441 static int insert_data(struct trie
*trie
, char **match_list
, char *line
, const char *filename
,
442 uint16_t file_priority
, uint32_t line_number
, bool compat
) {
443 char *value
, **entry
;
445 assert(line
[0] == ' ');
447 value
= strchr(line
, '=');
449 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
450 "Key-value pair expected but got \"%s\", ignoring", line
);
455 /* Replace multiple leading spaces by a single space */
456 while (isblank(line
[0]) && isblank(line
[1]))
459 if (isempty(line
+ 1) || isempty(value
))
460 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
461 "Empty %s in \"%s=%s\", ignoring",
462 isempty(line
+ 1) ? "key" : "value",
465 STRV_FOREACH(entry
, match_list
)
466 trie_insert(trie
, trie
->root
, *entry
, line
, value
, filename
, file_priority
, line_number
, compat
);
471 static int import_file(struct trie
*trie
, const char *filename
, uint16_t file_priority
, bool compat
) {
477 _cleanup_fclose_
FILE *f
= NULL
;
478 _cleanup_strv_free_
char **match_list
= NULL
;
479 uint32_t line_number
= 0;
482 f
= fopen(filename
, "re");
487 _cleanup_free_
char *line
= NULL
;
491 r
= read_line_full(f
, LONG_LINE_MAX
, READ_LINE_NOT_A_TTY
, &line
);
503 /* strip trailing comment */
504 pos
= strchr(line
, '#');
508 /* strip trailing whitespace */
510 while (len
> 0 && isspace(line
[len
-1]))
519 if (line
[0] == ' ') {
520 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
521 "Match expected but got indented property \"%s\", ignoring line", line
);
525 /* start of record, first match */
528 err
= strv_extend(&match_list
, line
);
536 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
537 "Property expected, ignoring record with no properties");
539 match_list
= strv_free(match_list
);
543 if (line
[0] != ' ') {
545 err
= strv_extend(&match_list
, line
);
554 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
, compat
);
563 match_list
= strv_free(match_list
);
567 if (line
[0] != ' ') {
568 r
= log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, SYNTHETIC_ERRNO(EINVAL
),
569 "Property or empty line expected, got \"%s\", ignoring record", line
);
571 match_list
= strv_free(match_list
);
575 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
, compat
);
582 if (state
== HW_MATCH
)
583 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, 0,
584 "Property expected, ignoring record with no properties");
589 int hwdb_update(const char *root
, const char *hwdb_bin_dir
, bool strict
, bool compat
) {
590 _cleanup_free_
char *hwdb_bin
= NULL
;
591 _cleanup_(trie_freep
) struct trie
*trie
= NULL
;
592 _cleanup_strv_free_
char **files
= NULL
;
594 uint16_t file_priority
= 1;
597 /* The argument 'compat' controls the format version of database. If false, then hwdb.bin will be created with
598 * additional information such that priority, line number, and filename of database source. If true, then hwdb.bin
599 * will be created without the information. systemd-hwdb command should set the argument false, and 'udevadm hwdb'
600 * command should set it true. */
602 trie
= new0(struct trie
, 1);
607 trie
->strings
= strbuf_new();
612 trie
->root
= new0(struct trie_node
, 1);
618 err
= conf_files_list_strv(&files
, ".hwdb", root
, 0, conf_file_dirs
);
620 return log_error_errno(err
, "Failed to enumerate hwdb files: %m");
622 STRV_FOREACH(f
, files
) {
623 log_debug("Reading file \"%s\"", *f
);
624 err
= import_file(trie
, *f
, file_priority
++, compat
);
625 if (err
< 0 && strict
)
629 strbuf_complete(trie
->strings
);
631 log_debug("=== trie in-memory ===");
632 log_debug("nodes: %8zu bytes (%8zu)",
633 trie
->nodes_count
* sizeof(struct trie_node
), trie
->nodes_count
);
634 log_debug("children arrays: %8zu bytes (%8zu)",
635 trie
->children_count
* sizeof(struct trie_child_entry
), trie
->children_count
);
636 log_debug("values arrays: %8zu bytes (%8zu)",
637 trie
->values_count
* sizeof(struct trie_value_entry
), trie
->values_count
);
638 log_debug("strings: %8zu bytes",
640 log_debug("strings incoming: %8zu bytes (%8zu)",
641 trie
->strings
->in_len
, trie
->strings
->in_count
);
642 log_debug("strings dedup'ed: %8zu bytes (%8zu)",
643 trie
->strings
->dedup_len
, trie
->strings
->dedup_count
);
645 hwdb_bin
= path_join(root
, hwdb_bin_dir
?: default_hwdb_bin_dir
, "hwdb.bin");
649 mkdir_parents_label(hwdb_bin
, 0755);
650 err
= trie_store(trie
, hwdb_bin
, compat
);
652 return log_error_errno(err
, "Failed to write database %s: %m", hwdb_bin
);
654 err
= label_fix(hwdb_bin
, 0);
661 int hwdb_query(const char *modalias
) {
662 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
663 const char *key
, *value
;
668 r
= sd_hwdb_new(&hwdb
);
672 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
)
673 printf("%s=%s\n", key
, value
);