1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "conf-files.h"
13 #include "hwdb-internal.h"
14 #include "hwdb-util.h"
17 #include "path-util.h"
18 #include "selinux-util.h"
20 #include "string-util.h"
22 #include "terminal-util.h"
27 * Generic udev properties, key-value database based on modalias strings.
28 * Uses a Patricia/radix trie to index all matches for efficient lookup.
31 static const char *arg_hwdb_bin_dir
= "/etc/udev";
32 static const char *arg_root
= "";
33 static bool arg_strict
;
35 static const char * const conf_file_dirs
[] = {
37 UDEVLIBEXECDIR
"/hwdb.d",
41 /* in-memory trie objects */
43 struct trie_node
*root
;
44 struct strbuf
*strings
;
47 size_t children_count
;
52 /* prefix, common part for all children of this node */
55 /* sorted array of pointers to children nodes */
56 struct trie_child_entry
*children
;
57 uint8_t children_count
;
59 /* sorted array of key-value pairs */
60 struct trie_value_entry
*values
;
64 /* children array item with char (0-255) index */
65 struct trie_child_entry
{
67 struct trie_node
*child
;
70 /* value array item with key-value pairs */
71 struct trie_value_entry
{
76 uint16_t file_priority
;
79 static int trie_children_cmp(const void *v1
, const void *v2
) {
80 const struct trie_child_entry
*n1
= v1
;
81 const struct trie_child_entry
*n2
= v2
;
86 static int node_add_child(struct trie
*trie
, struct trie_node
*node
, struct trie_node
*node_child
, uint8_t c
) {
87 struct trie_child_entry
*child
;
89 /* extend array, add new entry, sort for bisection */
90 child
= reallocarray(node
->children
, node
->children_count
+ 1, sizeof(struct trie_child_entry
));
94 node
->children
= child
;
95 trie
->children_count
++;
96 node
->children
[node
->children_count
].c
= c
;
97 node
->children
[node
->children_count
].child
= node_child
;
98 node
->children_count
++;
99 qsort(node
->children
, node
->children_count
, sizeof(struct trie_child_entry
), trie_children_cmp
);
105 static struct trie_node
*node_lookup(const struct trie_node
*node
, uint8_t c
) {
106 struct trie_child_entry
*child
;
107 struct trie_child_entry search
;
110 child
= bsearch_safe(&search
, node
->children
, node
->children_count
, sizeof(struct trie_child_entry
), trie_children_cmp
);
116 static void trie_node_cleanup(struct trie_node
*node
) {
119 for (i
= 0; i
< node
->children_count
; i
++)
120 trie_node_cleanup(node
->children
[i
].child
);
121 free(node
->children
);
126 static void trie_free(struct trie
*trie
) {
131 trie_node_cleanup(trie
->root
);
133 strbuf_cleanup(trie
->strings
);
137 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie
*, trie_free
);
139 static int trie_values_cmp(const void *v1
, const void *v2
, void *arg
) {
140 const struct trie_value_entry
*val1
= v1
;
141 const struct trie_value_entry
*val2
= v2
;
142 struct trie
*trie
= arg
;
144 return strcmp(trie
->strings
->buf
+ val1
->key_off
,
145 trie
->strings
->buf
+ val2
->key_off
);
148 static int trie_node_add_value(struct trie
*trie
, struct trie_node
*node
,
149 const char *key
, const char *value
,
150 const char *filename
, uint16_t file_priority
, uint32_t line_number
) {
152 struct trie_value_entry
*val
;
154 k
= strbuf_add_string(trie
->strings
, key
, strlen(key
));
157 v
= strbuf_add_string(trie
->strings
, value
, strlen(value
));
160 fn
= strbuf_add_string(trie
->strings
, filename
, strlen(filename
));
164 if (node
->values_count
) {
165 struct trie_value_entry search
= {
170 val
= xbsearch_r(&search
, node
->values
, node
->values_count
, sizeof(struct trie_value_entry
), trie_values_cmp
, trie
);
172 /* At this point we have 2 identical properties on the same match-string.
173 * Since we process files in order, we just replace the previous value.
176 val
->filename_off
= fn
;
177 val
->file_priority
= file_priority
;
178 val
->line_number
= line_number
;
183 /* extend array, add new entry, sort for bisection */
184 val
= reallocarray(node
->values
, node
->values_count
+ 1, sizeof(struct trie_value_entry
));
187 trie
->values_count
++;
189 node
->values
[node
->values_count
].key_off
= k
;
190 node
->values
[node
->values_count
].value_off
= v
;
191 node
->values
[node
->values_count
].filename_off
= fn
;
192 node
->values
[node
->values_count
].file_priority
= file_priority
;
193 node
->values
[node
->values_count
].line_number
= line_number
;
194 node
->values_count
++;
195 qsort_r(node
->values
, node
->values_count
, sizeof(struct trie_value_entry
), trie_values_cmp
, trie
);
199 static int trie_insert(struct trie
*trie
, struct trie_node
*node
, const char *search
,
200 const char *key
, const char *value
,
201 const char *filename
, uint16_t file_priority
, uint32_t line_number
) {
208 struct trie_node
*child
;
210 for (p
= 0; (c
= trie
->strings
->buf
[node
->prefix_off
+ p
]); p
++) {
211 _cleanup_free_
char *s
= NULL
;
213 _cleanup_free_
struct trie_node
*new_child
= NULL
;
215 if (c
== search
[i
+ p
])
219 new_child
= new0(struct trie_node
, 1);
223 /* move values from parent to child */
224 new_child
->prefix_off
= node
->prefix_off
+ p
+1;
225 new_child
->children
= node
->children
;
226 new_child
->children_count
= node
->children_count
;
227 new_child
->values
= node
->values
;
228 new_child
->values_count
= node
->values_count
;
230 /* update parent; use strdup() because the source gets realloc()d */
231 s
= strndup(trie
->strings
->buf
+ node
->prefix_off
, p
);
235 off
= strbuf_add_string(trie
->strings
, s
, p
);
239 node
->prefix_off
= off
;
240 node
->children
= NULL
;
241 node
->children_count
= 0;
243 node
->values_count
= 0;
244 r
= node_add_child(trie
, node
, new_child
, c
);
248 new_child
= NULL
; /* avoid cleanup */
255 return trie_node_add_value(trie
, node
, key
, value
, filename
, file_priority
, line_number
);
257 child
= node_lookup(node
, c
);
262 child
= new0(struct trie_node
, 1);
266 off
= strbuf_add_string(trie
->strings
, search
+ i
+1, strlen(search
+ i
+1));
272 child
->prefix_off
= off
;
273 r
= node_add_child(trie
, node
, child
, c
);
279 return trie_node_add_value(trie
, child
, key
, value
, filename
, file_priority
, line_number
);
290 uint64_t strings_off
;
292 uint64_t nodes_count
;
293 uint64_t children_count
;
294 uint64_t values_count
;
297 /* calculate the storage space for the nodes, children arrays, value arrays */
298 static void trie_store_nodes_size(struct trie_f
*trie
, struct trie_node
*node
) {
301 for (i
= 0; i
< node
->children_count
; i
++)
302 trie_store_nodes_size(trie
, node
->children
[i
].child
);
304 trie
->strings_off
+= sizeof(struct trie_node_f
);
305 for (i
= 0; i
< node
->children_count
; i
++)
306 trie
->strings_off
+= sizeof(struct trie_child_entry_f
);
307 for (i
= 0; i
< node
->values_count
; i
++)
308 trie
->strings_off
+= sizeof(struct trie_value_entry2_f
);
311 static int64_t trie_store_nodes(struct trie_f
*trie
, struct trie_node
*node
) {
313 struct trie_node_f n
= {
314 .prefix_off
= htole64(trie
->strings_off
+ node
->prefix_off
),
315 .children_count
= node
->children_count
,
316 .values_count
= htole64(node
->values_count
),
318 _cleanup_free_
struct trie_child_entry_f
*children
= NULL
;
321 if (node
->children_count
) {
322 children
= new(struct trie_child_entry_f
, node
->children_count
);
327 /* post-order recursion */
328 for (i
= 0; i
< node
->children_count
; i
++) {
331 child_off
= trie_store_nodes(trie
, node
->children
[i
].child
);
335 children
[i
] = (struct trie_child_entry_f
) {
336 .c
= node
->children
[i
].c
,
337 .child_off
= htole64(child_off
),
342 node_off
= ftello(trie
->f
);
343 fwrite(&n
, sizeof(struct trie_node_f
), 1, trie
->f
);
346 /* append children array */
347 if (node
->children_count
) {
348 fwrite(children
, sizeof(struct trie_child_entry_f
), node
->children_count
, trie
->f
);
349 trie
->children_count
+= node
->children_count
;
352 /* append values array */
353 for (i
= 0; i
< node
->values_count
; i
++) {
354 struct trie_value_entry2_f v
= {
355 .key_off
= htole64(trie
->strings_off
+ node
->values
[i
].key_off
),
356 .value_off
= htole64(trie
->strings_off
+ node
->values
[i
].value_off
),
357 .filename_off
= htole64(trie
->strings_off
+ node
->values
[i
].filename_off
),
358 .line_number
= htole32(node
->values
[i
].line_number
),
359 .file_priority
= htole16(node
->values
[i
].file_priority
),
362 fwrite(&v
, sizeof(struct trie_value_entry2_f
), 1, trie
->f
);
364 trie
->values_count
+= node
->values_count
;
369 static int trie_store(struct trie
*trie
, const char *filename
) {
373 _cleanup_free_
char *filename_tmp
= NULL
;
377 struct trie_header_f h
= {
378 .signature
= HWDB_SIG
,
379 .tool_version
= htole64(atoi(PACKAGE_VERSION
)),
380 .header_size
= htole64(sizeof(struct trie_header_f
)),
381 .node_size
= htole64(sizeof(struct trie_node_f
)),
382 .child_entry_size
= htole64(sizeof(struct trie_child_entry_f
)),
383 .value_entry_size
= htole64(sizeof(struct trie_value_entry2_f
)),
387 /* calculate size of header, nodes, children entries, value entries */
388 t
.strings_off
= sizeof(struct trie_header_f
);
389 trie_store_nodes_size(&t
, trie
->root
);
391 r
= fopen_temporary(filename
, &t
.f
, &filename_tmp
);
394 fchmod(fileno(t
.f
), 0444);
397 if (fseeko(t
.f
, sizeof(struct trie_header_f
), SEEK_SET
) < 0)
400 root_off
= trie_store_nodes(&t
, trie
->root
);
401 h
.nodes_root_off
= htole64(root_off
);
403 h
.nodes_len
= htole64(pos
- sizeof(struct trie_header_f
));
405 /* write string buffer */
406 fwrite(trie
->strings
->buf
, trie
->strings
->len
, 1, t
.f
);
407 h
.strings_len
= htole64(trie
->strings
->len
);
411 h
.file_size
= htole64(size
);
412 if (fseeko(t
.f
, 0, SEEK_SET
) < 0)
414 fwrite(&h
, sizeof(struct trie_header_f
), 1, t
.f
);
420 if (fsync(fileno(t
.f
)) < 0)
422 if (rename(filename_tmp
, filename
) < 0)
425 /* write succeeded */
428 log_debug("=== trie on-disk ===");
429 log_debug("size: %8"PRIi64
" bytes", size
);
430 log_debug("header: %8zu bytes", sizeof(struct trie_header_f
));
431 log_debug("nodes: %8"PRIu64
" bytes (%8"PRIu64
")",
432 t
.nodes_count
* sizeof(struct trie_node_f
), t
.nodes_count
);
433 log_debug("child pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
434 t
.children_count
* sizeof(struct trie_child_entry_f
), t
.children_count
);
435 log_debug("value pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
436 t
.values_count
* sizeof(struct trie_value_entry2_f
), t
.values_count
);
437 log_debug("string store: %8zu bytes", trie
->strings
->len
);
438 log_debug("strings start: %8"PRIu64
, t
.strings_off
);
444 unlink(filename_tmp
);
448 static int insert_data(struct trie
*trie
, char **match_list
, char *line
,
449 const char *filename
, uint16_t file_priority
, uint32_t line_number
) {
450 char *value
, **entry
;
452 assert(line
[0] == ' ');
454 value
= strchr(line
, '=');
456 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
457 "Key-value pair expected but got \"%s\", ignoring", line
);
462 /* Replace multiple leading spaces by a single space */
463 while (isblank(line
[0]) && isblank(line
[1]))
466 if (isempty(line
+ 1) || isempty(value
))
467 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
468 "Empty %s in \"%s=%s\", ignoring",
469 isempty(line
+ 1) ? "key" : "value",
472 STRV_FOREACH(entry
, match_list
)
473 trie_insert(trie
, trie
->root
, *entry
, line
, value
, filename
, file_priority
, line_number
);
478 static int import_file(struct trie
*trie
, const char *filename
, uint16_t file_priority
) {
484 _cleanup_fclose_
FILE *f
= NULL
;
486 _cleanup_strv_free_
char **match_list
= NULL
;
487 uint32_t line_number
= 0;
491 f
= fopen(filename
, "re");
495 while (fgets(line
, sizeof(line
), f
)) {
505 /* strip trailing comment */
506 pos
= strchr(line
, '#');
510 /* strip trailing whitespace */
512 while (len
> 0 && isspace(line
[len
-1]))
521 if (line
[0] == ' ') {
522 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
523 "Match expected but got indented property \"%s\", ignoring line", line
);
528 /* start of record, first match */
531 match
= strdup(line
);
535 err
= strv_consume(&match_list
, match
);
543 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
544 "Property expected, ignoring record with no properties");
547 strv_clear(match_list
);
551 if (line
[0] != ' ') {
553 match
= strdup(line
);
557 err
= strv_consume(&match_list
, match
);
566 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
);
575 strv_clear(match_list
);
579 if (line
[0] != ' ') {
580 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
581 "Property or empty line expected, got \"%s\", ignoring record", line
);
584 strv_clear(match_list
);
588 err
= insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
);
595 if (state
== HW_MATCH
)
596 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
597 "Property expected, ignoring record with no properties");
602 static int hwdb_query(int argc
, char *argv
[], void *userdata
) {
603 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
604 const char *key
, *value
;
605 const char *modalias
;
613 r
= sd_hwdb_new(&hwdb
);
617 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
)
618 printf("%s=%s\n", key
, value
);
623 static int hwdb_update(int argc
, char *argv
[], void *userdata
) {
624 _cleanup_free_
char *hwdb_bin
= NULL
;
625 _cleanup_(trie_freep
) struct trie
*trie
= NULL
;
626 _cleanup_strv_free_
char **files
= NULL
;
628 uint16_t file_priority
= 1;
631 trie
= new0(struct trie
, 1);
636 trie
->strings
= strbuf_new();
641 trie
->root
= new0(struct trie_node
, 1);
647 err
= conf_files_list_strv(&files
, ".hwdb", arg_root
, 0, conf_file_dirs
);
649 return log_error_errno(err
, "Failed to enumerate hwdb files: %m");
651 STRV_FOREACH(f
, files
) {
652 log_debug("Reading file \"%s\"", *f
);
653 err
= import_file(trie
, *f
, file_priority
++);
654 if (err
< 0 && arg_strict
)
658 strbuf_complete(trie
->strings
);
660 log_debug("=== trie in-memory ===");
661 log_debug("nodes: %8zu bytes (%8zu)",
662 trie
->nodes_count
* sizeof(struct trie_node
), trie
->nodes_count
);
663 log_debug("children arrays: %8zu bytes (%8zu)",
664 trie
->children_count
* sizeof(struct trie_child_entry
), trie
->children_count
);
665 log_debug("values arrays: %8zu bytes (%8zu)",
666 trie
->values_count
* sizeof(struct trie_value_entry
), trie
->values_count
);
667 log_debug("strings: %8zu bytes",
669 log_debug("strings incoming: %8zu bytes (%8zu)",
670 trie
->strings
->in_len
, trie
->strings
->in_count
);
671 log_debug("strings dedup'ed: %8zu bytes (%8zu)",
672 trie
->strings
->dedup_len
, trie
->strings
->dedup_count
);
674 hwdb_bin
= path_join(arg_root
, arg_hwdb_bin_dir
, "hwdb.bin");
678 mkdir_parents_label(hwdb_bin
, 0755);
679 err
= trie_store(trie
, hwdb_bin
);
681 return log_error_errno(err
, "Failure writing database %s: %m", hwdb_bin
);
683 err
= label_fix(hwdb_bin
, 0);
690 static int help(void) {
691 _cleanup_free_
char *link
= NULL
;
694 r
= terminal_urlify_man("systemd-hwdb", "8", &link
);
698 printf("%s OPTIONS COMMAND\n\n"
699 "Update or query the hardware database.\n\n"
700 " -h --help Show this help\n"
701 " --version Show package version\n"
702 " -s --strict When updating, return non-zero exit value on any parsing error\n"
703 " --usr Generate in " UDEVLIBEXECDIR
" instead of /etc/udev\n"
704 " -r --root=PATH Alternative root path in the filesystem\n\n"
706 " update Update the hwdb database\n"
707 " query MODALIAS Query database and print result\n"
708 "\nSee the %s for details.\n"
709 , program_invocation_short_name
716 static int parse_argv(int argc
, char *argv
[]) {
722 static const struct option options
[] = {
723 { "help", no_argument
, NULL
, 'h' },
724 { "version", no_argument
, NULL
, ARG_VERSION
},
725 { "usr", no_argument
, NULL
, ARG_USR
},
726 { "strict", no_argument
, NULL
, 's' },
727 { "root", required_argument
, NULL
, 'r' },
736 while ((c
= getopt_long(argc
, argv
, "ust:r:h", options
, NULL
)) >= 0) {
746 arg_hwdb_bin_dir
= UDEVLIBEXECDIR
;
761 assert_not_reached("Unknown option");
768 static int hwdb_main(int argc
, char *argv
[]) {
769 static const Verb verbs
[] = {
770 { "update", 1, 1, 0, hwdb_update
},
771 { "query", 2, 2, 0, hwdb_query
},
775 return dispatch_verb(argc
, argv
, verbs
, NULL
);
778 int main (int argc
, char *argv
[]) {
781 log_parse_environment();
784 r
= parse_argv(argc
, argv
);
790 r
= hwdb_main(argc
, argv
);
793 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;