2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-12-16 00:48:24 +01:00
|
|
|
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <ctype.h>
|
2014-12-16 00:48:24 +01:00
|
|
|
#include <getopt.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <stdlib.h>
|
2014-12-16 00:48:24 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-12-16 00:48:24 +01:00
|
|
|
#include "conf-files.h"
|
2015-10-26 18:05:03 +01:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "fileio.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "fs-util.h"
|
2014-12-16 00:48:24 +01:00
|
|
|
#include "hwdb-internal.h"
|
|
|
|
#include "hwdb-util.h"
|
2016-06-07 19:47:41 +02:00
|
|
|
#include "label.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "mkdir.h"
|
2017-06-01 03:21:11 +02:00
|
|
|
#include "path-util.h"
|
2016-06-07 19:47:41 +02:00
|
|
|
#include "selinux-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "strbuf.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "strv.h"
|
2018-08-09 10:32:31 +02:00
|
|
|
#include "terminal-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "verbs.h"
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
/*
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
* Generic udev properties, key-value database based on modalias strings.
|
2014-12-16 00:48:24 +01:00
|
|
|
* Uses a Patricia/radix trie to index all matches for efficient lookup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *arg_hwdb_bin_dir = "/etc/udev";
|
|
|
|
static const char *arg_root = "";
|
2018-03-27 05:24:01 +02:00
|
|
|
static bool arg_strict;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
static const char * const conf_file_dirs[] = {
|
|
|
|
"/etc/udev/hwdb.d",
|
|
|
|
UDEVLIBEXECDIR "/hwdb.d",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* in-memory trie objects */
|
|
|
|
struct trie {
|
|
|
|
struct trie_node *root;
|
|
|
|
struct strbuf *strings;
|
|
|
|
|
|
|
|
size_t nodes_count;
|
|
|
|
size_t children_count;
|
|
|
|
size_t values_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct trie_node {
|
|
|
|
/* prefix, common part for all children of this node */
|
|
|
|
size_t prefix_off;
|
|
|
|
|
|
|
|
/* sorted array of pointers to children nodes */
|
|
|
|
struct trie_child_entry *children;
|
|
|
|
uint8_t children_count;
|
|
|
|
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
/* sorted array of key-value pairs */
|
2014-12-16 00:48:24 +01:00
|
|
|
struct trie_value_entry *values;
|
|
|
|
size_t values_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* children array item with char (0-255) index */
|
|
|
|
struct trie_child_entry {
|
|
|
|
uint8_t c;
|
|
|
|
struct trie_node *child;
|
|
|
|
};
|
|
|
|
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
/* value array item with key-value pairs */
|
2014-12-16 00:48:24 +01:00
|
|
|
struct trie_value_entry {
|
|
|
|
size_t key_off;
|
|
|
|
size_t value_off;
|
2016-09-21 14:22:49 +02:00
|
|
|
size_t filename_off;
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
uint32_t line_number;
|
|
|
|
uint16_t file_priority;
|
2014-12-16 00:48:24 +01:00
|
|
|
};
|
|
|
|
|
2018-09-18 01:39:24 +02:00
|
|
|
static int trie_children_cmp(const struct trie_child_entry *a, const struct trie_child_entry *b) {
|
|
|
|
return CMP(a->c, b->c);
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) {
|
|
|
|
struct trie_child_entry *child;
|
|
|
|
|
|
|
|
/* extend array, add new entry, sort for bisection */
|
2018-02-27 19:09:22 +01:00
|
|
|
child = reallocarray(node->children, node->children_count + 1, sizeof(struct trie_child_entry));
|
2014-12-16 00:48:24 +01:00
|
|
|
if (!child)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
node->children = child;
|
|
|
|
trie->children_count++;
|
|
|
|
node->children[node->children_count].c = c;
|
|
|
|
node->children[node->children_count].child = node_child;
|
|
|
|
node->children_count++;
|
2018-09-18 01:39:24 +02:00
|
|
|
typesafe_qsort(node->children, node->children_count, trie_children_cmp);
|
2014-12-16 00:48:24 +01:00
|
|
|
trie->nodes_count++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct trie_node *node_lookup(const struct trie_node *node, uint8_t c) {
|
|
|
|
struct trie_child_entry *child;
|
|
|
|
struct trie_child_entry search;
|
|
|
|
|
|
|
|
search.c = c;
|
2018-09-18 01:39:24 +02:00
|
|
|
child = bsearch_safe(&search, node->children, node->children_count, sizeof(struct trie_child_entry), (comparison_fn_t) trie_children_cmp);
|
2014-12-16 00:48:24 +01:00
|
|
|
if (child)
|
|
|
|
return child->child;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trie_node_cleanup(struct trie_node *node) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < node->children_count; i++)
|
|
|
|
trie_node_cleanup(node->children[i].child);
|
|
|
|
free(node->children);
|
|
|
|
free(node->values);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trie_free(struct trie *trie) {
|
|
|
|
if (!trie)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (trie->root)
|
|
|
|
trie_node_cleanup(trie->root);
|
|
|
|
|
|
|
|
strbuf_cleanup(trie->strings);
|
|
|
|
free(trie);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct trie*, trie_free);
|
|
|
|
|
|
|
|
static int trie_values_cmp(const void *v1, const void *v2, void *arg) {
|
|
|
|
const struct trie_value_entry *val1 = v1;
|
|
|
|
const struct trie_value_entry *val2 = v2;
|
|
|
|
struct trie *trie = arg;
|
|
|
|
|
|
|
|
return strcmp(trie->strings->buf + val1->key_off,
|
|
|
|
trie->strings->buf + val2->key_off);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int trie_node_add_value(struct trie *trie, struct trie_node *node,
|
2016-09-21 14:22:49 +02:00
|
|
|
const char *key, const char *value,
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
const char *filename, uint16_t file_priority, uint32_t line_number) {
|
2016-09-21 14:22:49 +02:00
|
|
|
ssize_t k, v, fn;
|
2014-12-16 00:48:24 +01:00
|
|
|
struct trie_value_entry *val;
|
|
|
|
|
|
|
|
k = strbuf_add_string(trie->strings, key, strlen(key));
|
|
|
|
if (k < 0)
|
|
|
|
return k;
|
|
|
|
v = strbuf_add_string(trie->strings, value, strlen(value));
|
2016-09-21 14:22:49 +02:00
|
|
|
if (v < 0)
|
|
|
|
return v;
|
|
|
|
fn = strbuf_add_string(trie->strings, filename, strlen(filename));
|
2016-10-27 03:22:26 +02:00
|
|
|
if (fn < 0)
|
|
|
|
return fn;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
if (node->values_count) {
|
|
|
|
struct trie_value_entry search = {
|
|
|
|
.key_off = k,
|
|
|
|
.value_off = v,
|
|
|
|
};
|
|
|
|
|
|
|
|
val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie);
|
|
|
|
if (val) {
|
2016-11-29 20:46:40 +01:00
|
|
|
/* At this point we have 2 identical properties on the same match-string.
|
|
|
|
* Since we process files in order, we just replace the previous value.
|
2016-09-21 14:22:49 +02:00
|
|
|
*/
|
2014-12-16 00:48:24 +01:00
|
|
|
val->value_off = v;
|
2016-09-21 14:22:49 +02:00
|
|
|
val->filename_off = fn;
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
val->file_priority = file_priority;
|
2016-09-21 14:22:49 +02:00
|
|
|
val->line_number = line_number;
|
2014-12-16 00:48:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extend array, add new entry, sort for bisection */
|
2018-02-27 19:09:22 +01:00
|
|
|
val = reallocarray(node->values, node->values_count + 1, sizeof(struct trie_value_entry));
|
2014-12-16 00:48:24 +01:00
|
|
|
if (!val)
|
|
|
|
return -ENOMEM;
|
|
|
|
trie->values_count++;
|
|
|
|
node->values = val;
|
|
|
|
node->values[node->values_count].key_off = k;
|
|
|
|
node->values[node->values_count].value_off = v;
|
2016-09-21 14:22:49 +02:00
|
|
|
node->values[node->values_count].filename_off = fn;
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
node->values[node->values_count].file_priority = file_priority;
|
2016-09-21 14:22:49 +02:00
|
|
|
node->values[node->values_count].line_number = line_number;
|
2014-12-16 00:48:24 +01:00
|
|
|
node->values_count++;
|
|
|
|
qsort_r(node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int trie_insert(struct trie *trie, struct trie_node *node, const char *search,
|
2016-09-21 14:22:49 +02:00
|
|
|
const char *key, const char *value,
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
const char *filename, uint16_t file_priority, uint32_t line_number) {
|
2014-12-16 00:48:24 +01:00
|
|
|
size_t i = 0;
|
2016-11-30 02:44:22 +01:00
|
|
|
int r = 0;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t p;
|
|
|
|
uint8_t c;
|
|
|
|
struct trie_node *child;
|
|
|
|
|
|
|
|
for (p = 0; (c = trie->strings->buf[node->prefix_off + p]); p++) {
|
|
|
|
_cleanup_free_ char *s = NULL;
|
|
|
|
ssize_t off;
|
|
|
|
_cleanup_free_ struct trie_node *new_child = NULL;
|
|
|
|
|
|
|
|
if (c == search[i + p])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* split node */
|
|
|
|
new_child = new0(struct trie_node, 1);
|
|
|
|
if (!new_child)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* move values from parent to child */
|
|
|
|
new_child->prefix_off = node->prefix_off + p+1;
|
|
|
|
new_child->children = node->children;
|
|
|
|
new_child->children_count = node->children_count;
|
|
|
|
new_child->values = node->values;
|
|
|
|
new_child->values_count = node->values_count;
|
|
|
|
|
|
|
|
/* update parent; use strdup() because the source gets realloc()d */
|
|
|
|
s = strndup(trie->strings->buf + node->prefix_off, p);
|
|
|
|
if (!s)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
off = strbuf_add_string(trie->strings, s, p);
|
|
|
|
if (off < 0)
|
|
|
|
return off;
|
|
|
|
|
|
|
|
node->prefix_off = off;
|
|
|
|
node->children = NULL;
|
|
|
|
node->children_count = 0;
|
|
|
|
node->values = NULL;
|
|
|
|
node->values_count = 0;
|
2016-11-30 02:44:22 +01:00
|
|
|
r = node_add_child(trie, node, new_child, c);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
new_child = NULL; /* avoid cleanup */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += p;
|
|
|
|
|
|
|
|
c = search[i];
|
|
|
|
if (c == '\0')
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
return trie_node_add_value(trie, node, key, value, filename, file_priority, line_number);
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
child = node_lookup(node, c);
|
|
|
|
if (!child) {
|
|
|
|
ssize_t off;
|
|
|
|
|
|
|
|
/* new child */
|
|
|
|
child = new0(struct trie_node, 1);
|
|
|
|
if (!child)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
off = strbuf_add_string(trie->strings, search + i+1, strlen(search + i+1));
|
|
|
|
if (off < 0) {
|
|
|
|
free(child);
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
child->prefix_off = off;
|
2016-11-30 02:44:22 +01:00
|
|
|
r = node_add_child(trie, node, child, c);
|
|
|
|
if (r < 0) {
|
2014-12-16 00:48:24 +01:00
|
|
|
free(child);
|
2016-11-30 02:44:22 +01:00
|
|
|
return r;
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
return trie_node_add_value(trie, child, key, value, filename, file_priority, line_number);
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node = child;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct trie_f {
|
|
|
|
FILE *f;
|
|
|
|
struct trie *trie;
|
|
|
|
uint64_t strings_off;
|
|
|
|
|
|
|
|
uint64_t nodes_count;
|
|
|
|
uint64_t children_count;
|
|
|
|
uint64_t values_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* calculate the storage space for the nodes, children arrays, value arrays */
|
|
|
|
static void trie_store_nodes_size(struct trie_f *trie, struct trie_node *node) {
|
|
|
|
uint64_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < node->children_count; i++)
|
|
|
|
trie_store_nodes_size(trie, node->children[i].child);
|
|
|
|
|
|
|
|
trie->strings_off += sizeof(struct trie_node_f);
|
|
|
|
for (i = 0; i < node->children_count; i++)
|
|
|
|
trie->strings_off += sizeof(struct trie_child_entry_f);
|
|
|
|
for (i = 0; i < node->values_count; i++)
|
2016-09-21 14:22:49 +02:00
|
|
|
trie->strings_off += sizeof(struct trie_value_entry2_f);
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) {
|
|
|
|
uint64_t i;
|
|
|
|
struct trie_node_f n = {
|
|
|
|
.prefix_off = htole64(trie->strings_off + node->prefix_off),
|
|
|
|
.children_count = node->children_count,
|
|
|
|
.values_count = htole64(node->values_count),
|
|
|
|
};
|
2016-11-30 02:44:22 +01:00
|
|
|
_cleanup_free_ struct trie_child_entry_f *children = NULL;
|
2014-12-16 00:48:24 +01:00
|
|
|
int64_t node_off;
|
|
|
|
|
|
|
|
if (node->children_count) {
|
2016-11-30 02:44:22 +01:00
|
|
|
children = new(struct trie_child_entry_f, node->children_count);
|
2014-12-16 00:48:24 +01:00
|
|
|
if (!children)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* post-order recursion */
|
|
|
|
for (i = 0; i < node->children_count; i++) {
|
|
|
|
int64_t child_off;
|
|
|
|
|
|
|
|
child_off = trie_store_nodes(trie, node->children[i].child);
|
2016-11-30 02:44:22 +01:00
|
|
|
if (child_off < 0)
|
2014-12-16 00:48:24 +01:00
|
|
|
return child_off;
|
2016-11-30 02:44:22 +01:00
|
|
|
|
|
|
|
children[i] = (struct trie_child_entry_f) {
|
|
|
|
.c = node->children[i].c,
|
|
|
|
.child_off = htole64(child_off),
|
|
|
|
};
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write node */
|
|
|
|
node_off = ftello(trie->f);
|
|
|
|
fwrite(&n, sizeof(struct trie_node_f), 1, trie->f);
|
|
|
|
trie->nodes_count++;
|
|
|
|
|
|
|
|
/* append children array */
|
|
|
|
if (node->children_count) {
|
|
|
|
fwrite(children, sizeof(struct trie_child_entry_f), node->children_count, trie->f);
|
|
|
|
trie->children_count += node->children_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* append values array */
|
|
|
|
for (i = 0; i < node->values_count; i++) {
|
2016-09-21 14:22:49 +02:00
|
|
|
struct trie_value_entry2_f v = {
|
2014-12-16 00:48:24 +01:00
|
|
|
.key_off = htole64(trie->strings_off + node->values[i].key_off),
|
|
|
|
.value_off = htole64(trie->strings_off + node->values[i].value_off),
|
2016-09-21 14:22:49 +02:00
|
|
|
.filename_off = htole64(trie->strings_off + node->values[i].filename_off),
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
.line_number = htole32(node->values[i].line_number),
|
|
|
|
.file_priority = htole16(node->values[i].file_priority),
|
2014-12-16 00:48:24 +01:00
|
|
|
};
|
|
|
|
|
2016-09-21 14:22:49 +02:00
|
|
|
fwrite(&v, sizeof(struct trie_value_entry2_f), 1, trie->f);
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
2016-11-30 02:44:22 +01:00
|
|
|
trie->values_count += node->values_count;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
return node_off;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int trie_store(struct trie *trie, const char *filename) {
|
|
|
|
struct trie_f t = {
|
|
|
|
.trie = trie,
|
|
|
|
};
|
|
|
|
_cleanup_free_ char *filename_tmp = NULL;
|
|
|
|
int64_t pos;
|
|
|
|
int64_t root_off;
|
|
|
|
int64_t size;
|
|
|
|
struct trie_header_f h = {
|
|
|
|
.signature = HWDB_SIG,
|
2017-04-03 18:15:33 +02:00
|
|
|
.tool_version = htole64(atoi(PACKAGE_VERSION)),
|
2014-12-16 00:48:24 +01:00
|
|
|
.header_size = htole64(sizeof(struct trie_header_f)),
|
|
|
|
.node_size = htole64(sizeof(struct trie_node_f)),
|
|
|
|
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)),
|
2016-09-21 14:22:49 +02:00
|
|
|
.value_entry_size = htole64(sizeof(struct trie_value_entry2_f)),
|
2014-12-16 00:48:24 +01:00
|
|
|
};
|
2016-11-29 21:27:40 +01:00
|
|
|
int r;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
/* calculate size of header, nodes, children entries, value entries */
|
|
|
|
t.strings_off = sizeof(struct trie_header_f);
|
|
|
|
trie_store_nodes_size(&t, trie->root);
|
|
|
|
|
2017-08-17 18:09:44 +02:00
|
|
|
r = fopen_temporary(filename, &t.f, &filename_tmp);
|
2016-11-29 21:27:40 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-16 00:48:24 +01:00
|
|
|
fchmod(fileno(t.f), 0444);
|
|
|
|
|
|
|
|
/* write nodes */
|
2016-11-29 21:27:40 +01:00
|
|
|
if (fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET) < 0)
|
2017-08-17 18:09:44 +02:00
|
|
|
goto error_fclose;
|
2016-11-29 21:27:40 +01:00
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
root_off = trie_store_nodes(&t, trie->root);
|
|
|
|
h.nodes_root_off = htole64(root_off);
|
|
|
|
pos = ftello(t.f);
|
|
|
|
h.nodes_len = htole64(pos - sizeof(struct trie_header_f));
|
|
|
|
|
|
|
|
/* write string buffer */
|
|
|
|
fwrite(trie->strings->buf, trie->strings->len, 1, t.f);
|
|
|
|
h.strings_len = htole64(trie->strings->len);
|
|
|
|
|
|
|
|
/* write header */
|
|
|
|
size = ftello(t.f);
|
|
|
|
h.file_size = htole64(size);
|
2016-11-29 21:27:40 +01:00
|
|
|
if (fseeko(t.f, 0, SEEK_SET) < 0)
|
2017-08-17 18:09:44 +02:00
|
|
|
goto error_fclose;
|
2014-12-16 00:48:24 +01:00
|
|
|
fwrite(&h, sizeof(struct trie_header_f), 1, t.f);
|
2017-08-17 18:09:44 +02:00
|
|
|
|
|
|
|
if (ferror(t.f))
|
|
|
|
goto error_fclose;
|
|
|
|
if (fflush(t.f) < 0)
|
|
|
|
goto error_fclose;
|
|
|
|
if (fsync(fileno(t.f)) < 0)
|
|
|
|
goto error_fclose;
|
|
|
|
if (rename(filename_tmp, filename) < 0)
|
|
|
|
goto error_fclose;
|
|
|
|
|
|
|
|
/* write succeeded */
|
|
|
|
fclose(t.f);
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
log_debug("=== trie on-disk ===");
|
2015-01-21 04:22:15 +01:00
|
|
|
log_debug("size: %8"PRIi64" bytes", size);
|
2014-12-16 00:48:24 +01:00
|
|
|
log_debug("header: %8zu bytes", sizeof(struct trie_header_f));
|
|
|
|
log_debug("nodes: %8"PRIu64" bytes (%8"PRIu64")",
|
|
|
|
t.nodes_count * sizeof(struct trie_node_f), t.nodes_count);
|
|
|
|
log_debug("child pointers: %8"PRIu64" bytes (%8"PRIu64")",
|
|
|
|
t.children_count * sizeof(struct trie_child_entry_f), t.children_count);
|
|
|
|
log_debug("value pointers: %8"PRIu64" bytes (%8"PRIu64")",
|
2016-09-21 14:22:49 +02:00
|
|
|
t.values_count * sizeof(struct trie_value_entry2_f), t.values_count);
|
2014-12-16 00:48:24 +01:00
|
|
|
log_debug("string store: %8zu bytes", trie->strings->len);
|
|
|
|
log_debug("strings start: %8"PRIu64, t.strings_off);
|
|
|
|
return 0;
|
2016-11-29 21:27:40 +01:00
|
|
|
|
2017-08-17 18:09:44 +02:00
|
|
|
error_fclose:
|
2016-11-29 21:27:40 +01:00
|
|
|
r = -errno;
|
|
|
|
fclose(t.f);
|
|
|
|
unlink(filename_tmp);
|
|
|
|
return r;
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:22:49 +02:00
|
|
|
static int insert_data(struct trie *trie, char **match_list, char *line,
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
const char *filename, uint16_t file_priority, uint32_t line_number) {
|
2014-12-16 00:48:24 +01:00
|
|
|
char *value, **entry;
|
|
|
|
|
2016-12-01 17:32:41 +01:00
|
|
|
assert(line[0] == ' ');
|
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
value = strchr(line, '=');
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
if (!value)
|
|
|
|
return log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
|
|
|
|
"Key-value pair expected but got \"%s\", ignoring", line);
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
value[0] = '\0';
|
|
|
|
value++;
|
|
|
|
|
2016-12-01 17:32:41 +01:00
|
|
|
/* Replace multiple leading spaces by a single space */
|
2014-12-16 00:48:24 +01:00
|
|
|
while (isblank(line[0]) && isblank(line[1]))
|
|
|
|
line++;
|
|
|
|
|
2016-12-01 17:32:41 +01:00
|
|
|
if (isempty(line + 1) || isempty(value))
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
return log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
|
2016-12-01 17:32:41 +01:00
|
|
|
"Empty %s in \"%s=%s\", ignoring",
|
|
|
|
isempty(line + 1) ? "key" : "value",
|
|
|
|
line, value);
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
STRV_FOREACH(entry, match_list)
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
trie_insert(trie, trie->root, *entry, line, value, filename, file_priority, line_number);
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
static int import_file(struct trie *trie, const char *filename, uint16_t file_priority) {
|
2014-12-16 00:48:24 +01:00
|
|
|
enum {
|
|
|
|
HW_NONE,
|
|
|
|
HW_MATCH,
|
|
|
|
HW_DATA,
|
|
|
|
} state = HW_NONE;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
char line[LINE_MAX];
|
|
|
|
_cleanup_strv_free_ char **match_list = NULL;
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
uint32_t line_number = 0;
|
2014-12-16 00:48:24 +01:00
|
|
|
char *match = NULL;
|
2018-03-27 05:24:01 +02:00
|
|
|
int r = 0, err;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
f = fopen(filename, "re");
|
|
|
|
if (!f)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), f)) {
|
|
|
|
size_t len;
|
|
|
|
char *pos;
|
|
|
|
|
2016-09-21 14:22:49 +02:00
|
|
|
++line_number;
|
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
/* comment line */
|
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* strip trailing comment */
|
|
|
|
pos = strchr(line, '#');
|
|
|
|
if (pos)
|
|
|
|
pos[0] = '\0';
|
|
|
|
|
|
|
|
/* strip trailing whitespace */
|
|
|
|
len = strlen(line);
|
|
|
|
while (len > 0 && isspace(line[len-1]))
|
|
|
|
len--;
|
|
|
|
line[len] = '\0';
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case HW_NONE:
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (line[0] == ' ') {
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
|
|
|
|
"Match expected but got indented property \"%s\", ignoring line", line);
|
2018-03-27 05:24:01 +02:00
|
|
|
r = -EINVAL;
|
2014-12-16 00:48:24 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start of record, first match */
|
|
|
|
state = HW_MATCH;
|
|
|
|
|
|
|
|
match = strdup(line);
|
|
|
|
if (!match)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
err = strv_consume(&match_list, match);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HW_MATCH:
|
|
|
|
if (len == 0) {
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
|
|
|
|
"Property expected, ignoring record with no properties");
|
2018-03-27 05:24:01 +02:00
|
|
|
r = -EINVAL;
|
2014-12-16 00:48:24 +01:00
|
|
|
state = HW_NONE;
|
|
|
|
strv_clear(match_list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] != ' ') {
|
2016-12-01 17:46:40 +01:00
|
|
|
/* another match */
|
2014-12-16 00:48:24 +01:00
|
|
|
match = strdup(line);
|
|
|
|
if (!match)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
err = strv_consume(&match_list, match);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first data */
|
|
|
|
state = HW_DATA;
|
2018-03-27 05:24:01 +02:00
|
|
|
err = insert_data(trie, match_list, line, filename, file_priority, line_number);
|
|
|
|
if (err < 0)
|
|
|
|
r = err;
|
2014-12-16 00:48:24 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HW_DATA:
|
|
|
|
if (len == 0) {
|
2016-12-01 17:46:40 +01:00
|
|
|
/* end of record */
|
2014-12-16 00:48:24 +01:00
|
|
|
state = HW_NONE;
|
|
|
|
strv_clear(match_list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] != ' ') {
|
hwdb: improve and test syntax error messages
Since syntax error are non-fatal, downgrade them to warnings.
Use log_syntax to have uniform formatting including the line number.
State machine states like DATA and MATCH are internal, user-facing
messages should use the names from hwdb(7): match, property, record.
Also change "key/value" to "key-value", since there's no alternative
here, both parts must be present.
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:2] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:5] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:9] Property expected, ignoring record with no properties
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:11] Key/value pair expected but got " NO_VALUE", ignoring
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:18] Property or empty line expected, got "BAD:7:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:22] Property or empty line expected, got "BAD:8:match at wrong place", ignoring record
[/tmp/tmp.KFwEhm74n4/etc/udev/hwdb.d/10-bad.hwdb:23] Match expected but got indented property " Z=z", ignoring line
squash! hwdb: improve syntax error messages
2016-12-01 16:55:32 +01:00
|
|
|
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
|
|
|
|
"Property or empty line expected, got \"%s\", ignoring record", line);
|
2018-03-27 05:24:01 +02:00
|
|
|
r = -EINVAL;
|
2014-12-16 00:48:24 +01:00
|
|
|
state = HW_NONE;
|
|
|
|
strv_clear(match_list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
err = insert_data(trie, match_list, line, filename, file_priority, line_number);
|
|
|
|
if (err < 0)
|
|
|
|
r = err;
|
2014-12-16 00:48:24 +01:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-01 17:46:40 +01:00
|
|
|
if (state == HW_MATCH)
|
|
|
|
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
|
|
|
|
"Property expected, ignoring record with no properties");
|
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
return r;
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 03:01:27 +01:00
|
|
|
static int hwdb_query(int argc, char *argv[], void *userdata) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
|
2014-12-16 00:48:24 +01:00
|
|
|
const char *key, *value;
|
|
|
|
const char *modalias;
|
|
|
|
int r;
|
|
|
|
|
2014-12-19 03:01:27 +01:00
|
|
|
assert(argc >= 2);
|
|
|
|
assert(argv);
|
2014-12-16 00:48:24 +01:00
|
|
|
|
2014-12-19 03:01:27 +01:00
|
|
|
modalias = argv[1];
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
r = sd_hwdb_new(&hwdb);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value)
|
|
|
|
printf("%s=%s\n", key, value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-19 03:01:27 +01:00
|
|
|
static int hwdb_update(int argc, char *argv[], void *userdata) {
|
2014-12-16 00:48:24 +01:00
|
|
|
_cleanup_free_ char *hwdb_bin = NULL;
|
|
|
|
_cleanup_(trie_freep) struct trie *trie = NULL;
|
2016-11-30 02:44:22 +01:00
|
|
|
_cleanup_strv_free_ char **files = NULL;
|
|
|
|
char **f;
|
hwdb, sd-hwdb: rework priority comparison when loading properties
We cannot compare filenames directly, because paths are not sortable
lexicographically, e.g. /etc/udev is "later" (has higher priority)
than /usr/lib/udev.
The on-disk format is changed to have a separate field for "file priority",
which is stored when writing the binary file, and then loaded and used in
comparisons. For data in the previous format (as generated by systemd 232),
this information is not available, and we use a trick where the offset into the
string table is used as a proxy for priority. Most of the time strings are
stored in the order in which the files were processed. This is not entirely
reliable, but is good enough to properly order /usr/lib and /etc/, which are
the two most common cases. This hack is included because it allows proper
parsing of files until the binary hwdb is regenerated.
Instead of adding a new field, I reduced the size of line_number from 64 to 32
bits, and added a 16 bit priority field, and 16 bits of padding. Adding a new
field of 16 bytes would significantly screw up alignment and increase file
size, and line number realistically don't need more than ~20 bits.
Fixes #4750.
2016-11-30 02:26:35 +01:00
|
|
|
uint16_t file_priority = 1;
|
2018-03-27 05:24:01 +02:00
|
|
|
int r = 0, err;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
trie = new0(struct trie, 1);
|
|
|
|
if (!trie)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* string store */
|
|
|
|
trie->strings = strbuf_new();
|
|
|
|
if (!trie->strings)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* index */
|
|
|
|
trie->root = new0(struct trie_node, 1);
|
|
|
|
if (!trie->root)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
trie->nodes_count++;
|
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
err = conf_files_list_strv(&files, ".hwdb", arg_root, 0, conf_file_dirs);
|
|
|
|
if (err < 0)
|
|
|
|
return log_error_errno(err, "Failed to enumerate hwdb files: %m");
|
2014-12-16 00:48:24 +01:00
|
|
|
|
|
|
|
STRV_FOREACH(f, files) {
|
2016-11-30 02:44:22 +01:00
|
|
|
log_debug("Reading file \"%s\"", *f);
|
2018-03-27 05:24:01 +02:00
|
|
|
err = import_file(trie, *f, file_priority++);
|
|
|
|
if (err < 0 && arg_strict)
|
|
|
|
r = err;
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_complete(trie->strings);
|
|
|
|
|
|
|
|
log_debug("=== trie in-memory ===");
|
|
|
|
log_debug("nodes: %8zu bytes (%8zu)",
|
|
|
|
trie->nodes_count * sizeof(struct trie_node), trie->nodes_count);
|
|
|
|
log_debug("children arrays: %8zu bytes (%8zu)",
|
|
|
|
trie->children_count * sizeof(struct trie_child_entry), trie->children_count);
|
|
|
|
log_debug("values arrays: %8zu bytes (%8zu)",
|
|
|
|
trie->values_count * sizeof(struct trie_value_entry), trie->values_count);
|
|
|
|
log_debug("strings: %8zu bytes",
|
|
|
|
trie->strings->len);
|
|
|
|
log_debug("strings incoming: %8zu bytes (%8zu)",
|
|
|
|
trie->strings->in_len, trie->strings->in_count);
|
|
|
|
log_debug("strings dedup'ed: %8zu bytes (%8zu)",
|
|
|
|
trie->strings->dedup_len, trie->strings->dedup_count);
|
|
|
|
|
2017-06-01 03:21:11 +02:00
|
|
|
hwdb_bin = path_join(arg_root, arg_hwdb_bin_dir, "hwdb.bin");
|
2014-12-16 00:48:24 +01:00
|
|
|
if (!hwdb_bin)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-06-07 19:47:41 +02:00
|
|
|
mkdir_parents_label(hwdb_bin, 0755);
|
2018-03-27 05:24:01 +02:00
|
|
|
err = trie_store(trie, hwdb_bin);
|
|
|
|
if (err < 0)
|
|
|
|
return log_error_errno(err, "Failure writing database %s: %m", hwdb_bin);
|
|
|
|
|
|
|
|
err = label_fix(hwdb_bin, 0);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2014-12-16 00:48:24 +01:00
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
return r;
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("systemd-hwdb", "8", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
printf("%s OPTIONS COMMAND\n\n"
|
2015-02-12 13:17:08 +01:00
|
|
|
"Update or query the hardware database.\n\n"
|
2018-04-24 12:11:10 +02:00
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
|
|
|
" -s --strict When updating, return non-zero exit value on any parsing error\n"
|
|
|
|
" --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
|
|
|
|
" -r --root=PATH Alternative root path in the filesystem\n\n"
|
2014-12-16 00:48:24 +01:00
|
|
|
"Commands:\n"
|
2018-04-24 12:11:10 +02:00
|
|
|
" update Update the hwdb database\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
" query MODALIAS Query database and print result\n"
|
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
enum {
|
2015-02-12 13:17:08 +01:00
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_USR,
|
2014-12-16 00:48:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
2015-02-12 13:17:08 +01:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "usr", no_argument, NULL, ARG_USR },
|
2018-03-27 05:24:01 +02:00
|
|
|
{ "strict", no_argument, NULL, 's' },
|
2015-02-12 13:17:08 +01:00
|
|
|
{ "root", required_argument, NULL, 'r' },
|
2014-12-16 00:48:24 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "ust:r:h", options, NULL)) >= 0) {
|
2014-12-16 00:48:24 +01:00
|
|
|
switch(c) {
|
2015-02-12 13:17:08 +01:00
|
|
|
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2015-02-12 13:17:08 +01:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2015-02-12 13:17:08 +01:00
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
case ARG_USR:
|
|
|
|
arg_hwdb_bin_dir = UDEVLIBEXECDIR;
|
|
|
|
break;
|
2015-02-12 13:17:08 +01:00
|
|
|
|
2018-03-27 05:24:01 +02:00
|
|
|
case 's':
|
|
|
|
arg_strict = true;
|
|
|
|
break;
|
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
case 'r':
|
|
|
|
arg_root = optarg;
|
|
|
|
break;
|
2015-02-12 13:17:08 +01:00
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
2015-02-12 13:17:08 +01:00
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown option");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hwdb_main(int argc, char *argv[]) {
|
2018-03-12 06:05:08 +01:00
|
|
|
static const Verb verbs[] = {
|
2014-12-19 03:01:27 +01:00
|
|
|
{ "update", 1, 1, 0, hwdb_update },
|
2015-02-12 13:17:08 +01:00
|
|
|
{ "query", 2, 2, 0, hwdb_query },
|
2014-12-19 03:01:27 +01:00
|
|
|
{},
|
2014-12-16 00:48:24 +01:00
|
|
|
};
|
|
|
|
|
2014-12-19 03:01:27 +01:00
|
|
|
return dispatch_verb(argc, argv, verbs, NULL);
|
2014-12-16 00:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
goto finish;
|
|
|
|
|
2016-06-07 19:47:41 +02:00
|
|
|
mac_selinux_init();
|
|
|
|
|
2014-12-16 00:48:24 +01:00
|
|
|
r = hwdb_main(argc, argv);
|
|
|
|
|
|
|
|
finish:
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|