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"
18 #include "hwdb-internal.h"
19 #include "hwdb-util.h"
22 #include "path-util.h"
23 #include "selinux-util.h"
25 #include "string-util.h"
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 static const char *arg_hwdb_bin_dir
= "/etc/udev";
36 static const char *arg_root
= "";
38 static const char * const conf_file_dirs
[] = {
40 UDEVLIBEXECDIR
"/hwdb.d",
44 /* in-memory trie objects */
46 struct trie_node
*root
;
47 struct strbuf
*strings
;
50 size_t children_count
;
55 /* prefix, common part for all children of this node */
58 /* sorted array of pointers to children nodes */
59 struct trie_child_entry
*children
;
60 uint8_t children_count
;
62 /* sorted array of key-value pairs */
63 struct trie_value_entry
*values
;
67 /* children array item with char (0-255) index */
68 struct trie_child_entry
{
70 struct trie_node
*child
;
73 /* value array item with key-value pairs */
74 struct trie_value_entry
{
79 uint16_t file_priority
;
82 static int trie_children_cmp(const void *v1
, const void *v2
) {
83 const struct trie_child_entry
*n1
= v1
;
84 const struct trie_child_entry
*n2
= v2
;
89 static int node_add_child(struct trie
*trie
, struct trie_node
*node
, struct trie_node
*node_child
, uint8_t c
) {
90 struct trie_child_entry
*child
;
92 /* extend array, add new entry, sort for bisection */
93 child
= reallocarray(node
->children
, node
->children_count
+ 1, sizeof(struct trie_child_entry
));
97 node
->children
= child
;
98 trie
->children_count
++;
99 node
->children
[node
->children_count
].c
= c
;
100 node
->children
[node
->children_count
].child
= node_child
;
101 node
->children_count
++;
102 qsort(node
->children
, node
->children_count
, sizeof(struct trie_child_entry
), trie_children_cmp
);
108 static struct trie_node
*node_lookup(const struct trie_node
*node
, uint8_t c
) {
109 struct trie_child_entry
*child
;
110 struct trie_child_entry search
;
113 child
= bsearch_safe(&search
, node
->children
, node
->children_count
, sizeof(struct trie_child_entry
), trie_children_cmp
);
119 static void trie_node_cleanup(struct trie_node
*node
) {
122 for (i
= 0; i
< node
->children_count
; i
++)
123 trie_node_cleanup(node
->children
[i
].child
);
124 free(node
->children
);
129 static void trie_free(struct trie
*trie
) {
134 trie_node_cleanup(trie
->root
);
136 strbuf_cleanup(trie
->strings
);
140 DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie
*, trie_free
);
142 static int trie_values_cmp(const void *v1
, const void *v2
, void *arg
) {
143 const struct trie_value_entry
*val1
= v1
;
144 const struct trie_value_entry
*val2
= v2
;
145 struct trie
*trie
= arg
;
147 return strcmp(trie
->strings
->buf
+ val1
->key_off
,
148 trie
->strings
->buf
+ val2
->key_off
);
151 static int trie_node_add_value(struct trie
*trie
, struct trie_node
*node
,
152 const char *key
, const char *value
,
153 const char *filename
, uint16_t file_priority
, uint32_t line_number
) {
155 struct trie_value_entry
*val
;
157 k
= strbuf_add_string(trie
->strings
, key
, strlen(key
));
160 v
= strbuf_add_string(trie
->strings
, value
, strlen(value
));
163 fn
= strbuf_add_string(trie
->strings
, filename
, strlen(filename
));
167 if (node
->values_count
) {
168 struct trie_value_entry search
= {
173 val
= xbsearch_r(&search
, node
->values
, node
->values_count
, sizeof(struct trie_value_entry
), trie_values_cmp
, trie
);
175 /* At this point we have 2 identical properties on the same match-string.
176 * Since we process files in order, we just replace the previous value.
179 val
->filename_off
= fn
;
180 val
->file_priority
= file_priority
;
181 val
->line_number
= line_number
;
186 /* extend array, add new entry, sort for bisection */
187 val
= reallocarray(node
->values
, node
->values_count
+ 1, sizeof(struct trie_value_entry
));
190 trie
->values_count
++;
192 node
->values
[node
->values_count
].key_off
= k
;
193 node
->values
[node
->values_count
].value_off
= v
;
194 node
->values
[node
->values_count
].filename_off
= fn
;
195 node
->values
[node
->values_count
].file_priority
= file_priority
;
196 node
->values
[node
->values_count
].line_number
= line_number
;
197 node
->values_count
++;
198 qsort_r(node
->values
, node
->values_count
, sizeof(struct trie_value_entry
), trie_values_cmp
, trie
);
202 static int trie_insert(struct trie
*trie
, struct trie_node
*node
, const char *search
,
203 const char *key
, const char *value
,
204 const char *filename
, uint16_t file_priority
, uint32_t line_number
) {
211 struct trie_node
*child
;
213 for (p
= 0; (c
= trie
->strings
->buf
[node
->prefix_off
+ p
]); p
++) {
214 _cleanup_free_
char *s
= NULL
;
216 _cleanup_free_
struct trie_node
*new_child
= NULL
;
218 if (c
== search
[i
+ p
])
222 new_child
= new0(struct trie_node
, 1);
226 /* move values from parent to child */
227 new_child
->prefix_off
= node
->prefix_off
+ p
+1;
228 new_child
->children
= node
->children
;
229 new_child
->children_count
= node
->children_count
;
230 new_child
->values
= node
->values
;
231 new_child
->values_count
= node
->values_count
;
233 /* update parent; use strdup() because the source gets realloc()d */
234 s
= strndup(trie
->strings
->buf
+ node
->prefix_off
, p
);
238 off
= strbuf_add_string(trie
->strings
, s
, p
);
242 node
->prefix_off
= off
;
243 node
->children
= NULL
;
244 node
->children_count
= 0;
246 node
->values_count
= 0;
247 r
= node_add_child(trie
, node
, new_child
, c
);
251 new_child
= NULL
; /* avoid cleanup */
258 return trie_node_add_value(trie
, node
, key
, value
, filename
, file_priority
, line_number
);
260 child
= node_lookup(node
, c
);
265 child
= new0(struct trie_node
, 1);
269 off
= strbuf_add_string(trie
->strings
, search
+ i
+1, strlen(search
+ i
+1));
275 child
->prefix_off
= off
;
276 r
= node_add_child(trie
, node
, child
, c
);
282 return trie_node_add_value(trie
, child
, key
, value
, filename
, file_priority
, line_number
);
293 uint64_t strings_off
;
295 uint64_t nodes_count
;
296 uint64_t children_count
;
297 uint64_t values_count
;
300 /* calculate the storage space for the nodes, children arrays, value arrays */
301 static void trie_store_nodes_size(struct trie_f
*trie
, struct trie_node
*node
) {
304 for (i
= 0; i
< node
->children_count
; i
++)
305 trie_store_nodes_size(trie
, node
->children
[i
].child
);
307 trie
->strings_off
+= sizeof(struct trie_node_f
);
308 for (i
= 0; i
< node
->children_count
; i
++)
309 trie
->strings_off
+= sizeof(struct trie_child_entry_f
);
310 for (i
= 0; i
< node
->values_count
; i
++)
311 trie
->strings_off
+= sizeof(struct trie_value_entry2_f
);
314 static int64_t trie_store_nodes(struct trie_f
*trie
, struct trie_node
*node
) {
316 struct trie_node_f n
= {
317 .prefix_off
= htole64(trie
->strings_off
+ node
->prefix_off
),
318 .children_count
= node
->children_count
,
319 .values_count
= htole64(node
->values_count
),
321 _cleanup_free_
struct trie_child_entry_f
*children
= NULL
;
324 if (node
->children_count
) {
325 children
= new(struct trie_child_entry_f
, node
->children_count
);
330 /* post-order recursion */
331 for (i
= 0; i
< node
->children_count
; i
++) {
334 child_off
= trie_store_nodes(trie
, node
->children
[i
].child
);
338 children
[i
] = (struct trie_child_entry_f
) {
339 .c
= node
->children
[i
].c
,
340 .child_off
= htole64(child_off
),
345 node_off
= ftello(trie
->f
);
346 fwrite(&n
, sizeof(struct trie_node_f
), 1, trie
->f
);
349 /* append children array */
350 if (node
->children_count
) {
351 fwrite(children
, sizeof(struct trie_child_entry_f
), node
->children_count
, trie
->f
);
352 trie
->children_count
+= node
->children_count
;
355 /* append values array */
356 for (i
= 0; i
< node
->values_count
; i
++) {
357 struct trie_value_entry2_f v
= {
358 .key_off
= htole64(trie
->strings_off
+ node
->values
[i
].key_off
),
359 .value_off
= htole64(trie
->strings_off
+ node
->values
[i
].value_off
),
360 .filename_off
= htole64(trie
->strings_off
+ node
->values
[i
].filename_off
),
361 .line_number
= htole32(node
->values
[i
].line_number
),
362 .file_priority
= htole16(node
->values
[i
].file_priority
),
365 fwrite(&v
, sizeof(struct trie_value_entry2_f
), 1, trie
->f
);
367 trie
->values_count
+= node
->values_count
;
372 static int trie_store(struct trie
*trie
, const char *filename
) {
376 _cleanup_free_
char *filename_tmp
= NULL
;
380 struct trie_header_f h
= {
381 .signature
= HWDB_SIG
,
382 .tool_version
= htole64(atoi(PACKAGE_VERSION
)),
383 .header_size
= htole64(sizeof(struct trie_header_f
)),
384 .node_size
= htole64(sizeof(struct trie_node_f
)),
385 .child_entry_size
= htole64(sizeof(struct trie_child_entry_f
)),
386 .value_entry_size
= htole64(sizeof(struct trie_value_entry2_f
)),
390 /* calculate size of header, nodes, children entries, value entries */
391 t
.strings_off
= sizeof(struct trie_header_f
);
392 trie_store_nodes_size(&t
, trie
->root
);
394 r
= fopen_temporary(filename
, &t
.f
, &filename_tmp
);
397 fchmod(fileno(t
.f
), 0444);
400 if (fseeko(t
.f
, sizeof(struct trie_header_f
), SEEK_SET
) < 0)
403 root_off
= trie_store_nodes(&t
, trie
->root
);
404 h
.nodes_root_off
= htole64(root_off
);
406 h
.nodes_len
= htole64(pos
- sizeof(struct trie_header_f
));
408 /* write string buffer */
409 fwrite(trie
->strings
->buf
, trie
->strings
->len
, 1, t
.f
);
410 h
.strings_len
= htole64(trie
->strings
->len
);
414 h
.file_size
= htole64(size
);
415 if (fseeko(t
.f
, 0, SEEK_SET
) < 0)
417 fwrite(&h
, sizeof(struct trie_header_f
), 1, t
.f
);
423 if (fsync(fileno(t
.f
)) < 0)
425 if (rename(filename_tmp
, filename
) < 0)
428 /* write succeeded */
431 log_debug("=== trie on-disk ===");
432 log_debug("size: %8"PRIi64
" bytes", size
);
433 log_debug("header: %8zu bytes", sizeof(struct trie_header_f
));
434 log_debug("nodes: %8"PRIu64
" bytes (%8"PRIu64
")",
435 t
.nodes_count
* sizeof(struct trie_node_f
), t
.nodes_count
);
436 log_debug("child pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
437 t
.children_count
* sizeof(struct trie_child_entry_f
), t
.children_count
);
438 log_debug("value pointers: %8"PRIu64
" bytes (%8"PRIu64
")",
439 t
.values_count
* sizeof(struct trie_value_entry2_f
), t
.values_count
);
440 log_debug("string store: %8zu bytes", trie
->strings
->len
);
441 log_debug("strings start: %8"PRIu64
, t
.strings_off
);
447 unlink(filename_tmp
);
451 static int insert_data(struct trie
*trie
, char **match_list
, char *line
,
452 const char *filename
, uint16_t file_priority
, uint32_t line_number
) {
453 char *value
, **entry
;
455 assert(line
[0] == ' ');
457 value
= strchr(line
, '=');
459 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
460 "Key-value pair expected but got \"%s\", ignoring", line
);
465 /* Replace multiple leading spaces by a single space */
466 while (isblank(line
[0]) && isblank(line
[1]))
469 if (isempty(line
+ 1) || isempty(value
))
470 return log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
471 "Empty %s in \"%s=%s\", ignoring",
472 isempty(line
+ 1) ? "key" : "value",
475 STRV_FOREACH(entry
, match_list
)
476 trie_insert(trie
, trie
->root
, *entry
, line
, value
, filename
, file_priority
, line_number
);
481 static int import_file(struct trie
*trie
, const char *filename
, uint16_t file_priority
) {
487 _cleanup_fclose_
FILE *f
= NULL
;
489 _cleanup_strv_free_
char **match_list
= NULL
;
490 uint32_t line_number
= 0;
494 f
= fopen(filename
, "re");
498 while (fgets(line
, sizeof(line
), f
)) {
508 /* strip trailing comment */
509 pos
= strchr(line
, '#');
513 /* strip trailing whitespace */
515 while (len
> 0 && isspace(line
[len
-1]))
524 if (line
[0] == ' ') {
525 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
526 "Match expected but got indented property \"%s\", ignoring line", line
);
530 /* start of record, first match */
533 match
= strdup(line
);
537 r
= strv_consume(&match_list
, match
);
545 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
546 "Property expected, ignoring record with no properties");
549 strv_clear(match_list
);
553 if (line
[0] != ' ') {
555 match
= strdup(line
);
559 r
= strv_consume(&match_list
, match
);
568 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
);
583 strv_clear(match_list
);
587 insert_data(trie
, match_list
, line
, filename
, file_priority
, line_number
);
592 if (state
== HW_MATCH
)
593 log_syntax(NULL
, LOG_WARNING
, filename
, line_number
, EINVAL
,
594 "Property expected, ignoring record with no properties");
599 static int hwdb_query(int argc
, char *argv
[], void *userdata
) {
600 _cleanup_(sd_hwdb_unrefp
) sd_hwdb
*hwdb
= NULL
;
601 const char *key
, *value
;
602 const char *modalias
;
610 r
= sd_hwdb_new(&hwdb
);
614 SD_HWDB_FOREACH_PROPERTY(hwdb
, modalias
, key
, value
)
615 printf("%s=%s\n", key
, value
);
620 static int hwdb_update(int argc
, char *argv
[], void *userdata
) {
621 _cleanup_free_
char *hwdb_bin
= NULL
;
622 _cleanup_(trie_freep
) struct trie
*trie
= NULL
;
623 _cleanup_strv_free_
char **files
= NULL
;
625 uint16_t file_priority
= 1;
628 trie
= new0(struct trie
, 1);
633 trie
->strings
= strbuf_new();
638 trie
->root
= new0(struct trie_node
, 1);
644 r
= conf_files_list_strv(&files
, ".hwdb", arg_root
, 0, conf_file_dirs
);
646 return log_error_errno(r
, "Failed to enumerate hwdb files: %m");
648 STRV_FOREACH(f
, files
) {
649 log_debug("Reading file \"%s\"", *f
);
650 import_file(trie
, *f
, file_priority
++);
653 strbuf_complete(trie
->strings
);
655 log_debug("=== trie in-memory ===");
656 log_debug("nodes: %8zu bytes (%8zu)",
657 trie
->nodes_count
* sizeof(struct trie_node
), trie
->nodes_count
);
658 log_debug("children arrays: %8zu bytes (%8zu)",
659 trie
->children_count
* sizeof(struct trie_child_entry
), trie
->children_count
);
660 log_debug("values arrays: %8zu bytes (%8zu)",
661 trie
->values_count
* sizeof(struct trie_value_entry
), trie
->values_count
);
662 log_debug("strings: %8zu bytes",
664 log_debug("strings incoming: %8zu bytes (%8zu)",
665 trie
->strings
->in_len
, trie
->strings
->in_count
);
666 log_debug("strings dedup'ed: %8zu bytes (%8zu)",
667 trie
->strings
->dedup_len
, trie
->strings
->dedup_count
);
669 hwdb_bin
= path_join(arg_root
, arg_hwdb_bin_dir
, "hwdb.bin");
673 mkdir_parents_label(hwdb_bin
, 0755);
674 r
= trie_store(trie
, hwdb_bin
);
676 return log_error_errno(r
, "Failure writing database %s: %m", hwdb_bin
);
678 return label_fix(hwdb_bin
, 0);
681 static void help(void) {
682 printf("Usage: %s OPTIONS COMMAND\n\n"
683 "Update or query the hardware database.\n\n"
684 " -h --help Show this help\n"
685 " --version Show package version\n"
686 " --usr Generate in " UDEVLIBEXECDIR
" instead of /etc/udev\n"
687 " -r --root=PATH Alternative root path in the filesystem\n\n"
689 " update Update the hwdb database\n"
690 " query MODALIAS Query database and print result\n",
691 program_invocation_short_name
);
694 static int parse_argv(int argc
, char *argv
[]) {
700 static const struct option options
[] = {
701 { "help", no_argument
, NULL
, 'h' },
702 { "version", no_argument
, NULL
, ARG_VERSION
},
703 { "usr", no_argument
, NULL
, ARG_USR
},
704 { "root", required_argument
, NULL
, 'r' },
713 while ((c
= getopt_long(argc
, argv
, "ut:r:h", options
, NULL
)) >= 0) {
724 arg_hwdb_bin_dir
= UDEVLIBEXECDIR
;
735 assert_not_reached("Unknown option");
742 static int hwdb_main(int argc
, char *argv
[]) {
743 static const Verb verbs
[] = {
744 { "update", 1, 1, 0, hwdb_update
},
745 { "query", 2, 2, 0, hwdb_query
},
749 return dispatch_verb(argc
, argv
, verbs
, NULL
);
752 int main (int argc
, char *argv
[]) {
755 log_parse_environment();
758 r
= parse_argv(argc
, argv
);
764 r
= hwdb_main(argc
, argv
);
767 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;