2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-10-22 18:23:08 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
2012-11-12 19:36:23 +01:00
|
|
|
Copyright 2012 Kay Sievers <kay@vrfy.org>
|
2012-10-22 18:23:08 +02:00
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <ctype.h>
|
2012-10-22 18:23:08 +02:00
|
|
|
#include <getopt.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdlib.h>
|
2012-10-22 18:23:08 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2012-10-22 18:23:08 +02:00
|
|
|
#include "conf-files.h"
|
2015-10-26 18:05:03 +01:00
|
|
|
#include "fileio.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "fs-util.h"
|
2014-12-03 17:32:40 +01:00
|
|
|
#include "hwdb-internal.h"
|
2014-12-15 19:48:21 +01:00
|
|
|
#include "hwdb-util.h"
|
2016-07-08 17:43:05 +02:00
|
|
|
#include "label.h"
|
|
|
|
#include "mkdir.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "strbuf.h"
|
|
|
|
#include "string-util.h"
|
|
|
|
#include "udev.h"
|
2017-12-05 15:30:10 +01:00
|
|
|
#include "udevadm-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "util.h"
|
2012-10-22 18:23:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic udev properties, key/value database based on modalias strings.
|
|
|
|
* Uses a Patricia/radix trie to index all matches for efficient lookup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char * const conf_file_dirs[] = {
|
2012-10-27 20:59:01 +02:00
|
|
|
"/etc/udev/hwdb.d",
|
2012-10-22 18:23:08 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
/* sorted array of key/value pairs */
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* value array item with key/value pairs */
|
|
|
|
struct trie_value_entry {
|
|
|
|
size_t key_off;
|
|
|
|
size_t value_off;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int trie_children_cmp(const void *v1, const void *v2) {
|
|
|
|
const struct trie_child_entry *n1 = v1;
|
|
|
|
const struct trie_child_entry *n2 = v2;
|
|
|
|
|
|
|
|
return n1->c - n2->c;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2013-04-05 23:39:46 +02:00
|
|
|
if (!child)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
node->children = child;
|
|
|
|
trie->children_count++;
|
|
|
|
node->children[node->children_count].c = c;
|
|
|
|
node->children[node->children_count].child = node_child;
|
|
|
|
node->children_count++;
|
|
|
|
qsort(node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp);
|
|
|
|
trie->nodes_count++;
|
2013-04-05 23:39:46 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
child = bsearch(&search, node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp);
|
|
|
|
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 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,
|
|
|
|
const char *key, const char *value) {
|
2012-10-25 21:39:01 +02:00
|
|
|
ssize_t k, v;
|
2012-10-22 18:23:08 +02:00
|
|
|
struct trie_value_entry *val;
|
|
|
|
|
|
|
|
k = strbuf_add_string(trie->strings, key, strlen(key));
|
2012-10-25 21:39:01 +02:00
|
|
|
if (k < 0)
|
|
|
|
return k;
|
2012-10-22 18:23:08 +02:00
|
|
|
v = strbuf_add_string(trie->strings, value, strlen(value));
|
2012-10-25 21:39:01 +02:00
|
|
|
if (v < 0)
|
|
|
|
return v;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
/* replace existing earlier key with new value */
|
|
|
|
val->value_off = v;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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));
|
2012-10-22 18:23:08 +02: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;
|
|
|
|
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,
|
|
|
|
const char *key, const char *value) {
|
|
|
|
size_t i = 0;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t p;
|
|
|
|
uint8_t c;
|
2013-04-05 23:39:46 +02:00
|
|
|
struct trie_node *child;
|
2012-10-22 18:23:08 +02:00
|
|
|
|
|
|
|
for (p = 0; (c = trie->strings->buf[node->prefix_off + p]); p++) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ char *s = NULL;
|
2012-10-22 18:23:08 +02:00
|
|
|
ssize_t off;
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ struct trie_node *new_child = NULL;
|
2012-10-22 18:23:08 +02:00
|
|
|
|
|
|
|
if (c == search[i + p])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* split node */
|
2014-02-13 14:45:51 +01:00
|
|
|
new_child = new0(struct trie_node, 1);
|
2013-04-05 23:39:46 +02:00
|
|
|
if (!new_child)
|
|
|
|
return -ENOMEM;
|
2012-10-22 18:23:08 +02:00
|
|
|
|
|
|
|
/* move values from parent to child */
|
2013-04-05 23:39:46 +02:00
|
|
|
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;
|
2012-10-22 18:23:08 +02:00
|
|
|
|
|
|
|
/* update parent; use strdup() because the source gets realloc()d */
|
|
|
|
s = strndup(trie->strings->buf + node->prefix_off, p);
|
2013-04-05 23:39:46 +02:00
|
|
|
if (!s)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
off = strbuf_add_string(trie->strings, s, p);
|
2013-04-05 23:39:46 +02:00
|
|
|
if (off < 0)
|
|
|
|
return off;
|
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
node->prefix_off = off;
|
|
|
|
node->children = NULL;
|
|
|
|
node->children_count = 0;
|
|
|
|
node->values = NULL;
|
|
|
|
node->values_count = 0;
|
2013-04-05 23:39:46 +02:00
|
|
|
err = node_add_child(trie, node, new_child, c);
|
2012-10-22 18:23:08 +02:00
|
|
|
if (err)
|
2013-04-05 23:39:46 +02:00
|
|
|
return err;
|
|
|
|
|
|
|
|
new_child = NULL; /* avoid cleanup */
|
2012-10-22 18:23:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += p;
|
|
|
|
|
|
|
|
c = search[i];
|
|
|
|
if (c == '\0')
|
|
|
|
return trie_node_add_value(trie, node, key, value);
|
|
|
|
|
|
|
|
child = node_lookup(node, c);
|
|
|
|
if (!child) {
|
|
|
|
ssize_t off;
|
|
|
|
|
|
|
|
/* new child */
|
2014-02-13 14:45:51 +01:00
|
|
|
child = new0(struct trie_node, 1);
|
2013-04-05 23:39:46 +02:00
|
|
|
if (!child)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
off = strbuf_add_string(trie->strings, search + i+1, strlen(search + i+1));
|
|
|
|
if (off < 0) {
|
2013-04-05 23:39:46 +02:00
|
|
|
free(child);
|
|
|
|
return off;
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
2013-04-05 23:39:46 +02:00
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
child->prefix_off = off;
|
|
|
|
err = node_add_child(trie, node, child, c);
|
2013-04-05 23:39:46 +02:00
|
|
|
if (err) {
|
|
|
|
free(child);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
return trie_node_add_value(trie, child, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
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++)
|
|
|
|
trie->strings_off += sizeof(struct trie_value_entry_f);
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
2012-10-27 20:59:01 +02:00
|
|
|
struct trie_child_entry_f *children = NULL;
|
2012-10-22 18:23:08 +02:00
|
|
|
int64_t node_off;
|
|
|
|
|
|
|
|
if (node->children_count) {
|
|
|
|
children = new0(struct trie_child_entry_f, node->children_count);
|
|
|
|
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);
|
2013-07-19 01:36:55 +02:00
|
|
|
if (child_off < 0) {
|
|
|
|
free(children);
|
2012-10-22 18:23:08 +02:00
|
|
|
return child_off;
|
2013-07-19 01:36:55 +02:00
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
children[i].c = node->children[i].c;
|
|
|
|
children[i].child_off = htole64(child_off);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
free(children);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* append values array */
|
|
|
|
for (i = 0; i < node->values_count; i++) {
|
|
|
|
struct trie_value_entry_f v = {
|
|
|
|
.key_off = htole64(trie->strings_off + node->values[i].key_off),
|
|
|
|
.value_off = htole64(trie->strings_off + node->values[i].value_off),
|
|
|
|
};
|
|
|
|
|
|
|
|
fwrite(&v, sizeof(struct trie_value_entry_f), 1, trie->f);
|
|
|
|
trie->values_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node_off;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int trie_store(struct trie *trie, const char *filename) {
|
|
|
|
struct trie_f t = {
|
|
|
|
.trie = trie,
|
|
|
|
};
|
2014-02-19 17:51:08 +01:00
|
|
|
_cleanup_free_ char *filename_tmp = NULL;
|
2012-10-22 18:23:08 +02:00
|
|
|
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)),
|
2012-10-22 18:23:08 +02: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)),
|
|
|
|
.value_entry_size = htole64(sizeof(struct trie_value_entry_f)),
|
|
|
|
};
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* 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
|
|
|
err = fopen_temporary(filename, &t.f, &filename_tmp);
|
2012-10-22 18:23:08 +02:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
fchmod(fileno(t.f), 0444);
|
|
|
|
|
|
|
|
/* write nodes */
|
2017-08-17 18:09:44 +02:00
|
|
|
if (fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET) < 0)
|
|
|
|
goto error_fclose;
|
2012-10-22 18:23:08 +02: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);
|
2017-08-17 18:09:44 +02:00
|
|
|
if (fseeko(t.f, 0, SEEK_SET < 0))
|
|
|
|
goto error_fclose;
|
2012-10-22 18:23:08 +02: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 */
|
2012-10-22 18:23:08 +02:00
|
|
|
fclose(t.f);
|
|
|
|
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("=== trie on-disk ===");
|
2015-01-21 04:22:15 +01:00
|
|
|
log_debug("size: %8"PRIi64" bytes", size);
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("header: %8zu bytes", sizeof(struct trie_header_f));
|
2014-02-04 01:31:53 +01:00
|
|
|
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")",
|
|
|
|
t.values_count * sizeof(struct trie_value_entry_f), t.values_count);
|
|
|
|
log_debug("string store: %8zu bytes", trie->strings->len);
|
|
|
|
log_debug("strings start: %8"PRIu64, t.strings_off);
|
2014-02-19 17:51:08 +01:00
|
|
|
|
|
|
|
return 0;
|
2017-08-17 18:09:44 +02:00
|
|
|
|
|
|
|
error_fclose:
|
|
|
|
err = -errno;
|
|
|
|
fclose(t.f);
|
|
|
|
unlink(filename_tmp);
|
|
|
|
return err;
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
static int insert_data(struct trie *trie, struct udev_list *match_list,
|
|
|
|
char *line, const char *filename) {
|
|
|
|
char *value;
|
|
|
|
struct udev_list_entry *entry;
|
|
|
|
|
|
|
|
value = strchr(line, '=');
|
|
|
|
if (!value) {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("Error, key/value pair expected but got '%s' in '%s':", line, filename);
|
2013-07-10 14:59:24 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[0] = '\0';
|
|
|
|
value++;
|
|
|
|
|
2014-11-27 06:19:44 +01:00
|
|
|
/* libudev requires properties to start with a space */
|
|
|
|
while (isblank(line[0]) && isblank(line[1]))
|
|
|
|
line++;
|
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
if (line[0] == '\0' || value[0] == '\0') {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("Error, empty key or value '%s' in '%s':", line, filename);
|
2013-07-10 14:59:24 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
udev_list_entry_foreach(entry, udev_list_get_entry(match_list))
|
|
|
|
trie_insert(trie, trie->root, udev_list_entry_get_name(entry), line, value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int import_file(struct udev *udev, struct trie *trie, const char *filename) {
|
|
|
|
enum {
|
|
|
|
HW_MATCH,
|
|
|
|
HW_DATA,
|
|
|
|
HW_NONE,
|
|
|
|
} state = HW_NONE;
|
2012-10-22 18:23:08 +02:00
|
|
|
FILE *f;
|
|
|
|
char line[LINE_MAX];
|
2013-07-10 14:59:24 +02:00
|
|
|
struct udev_list match_list;
|
|
|
|
|
|
|
|
udev_list_init(udev, &match_list, false);
|
2012-10-22 18:23:08 +02:00
|
|
|
|
|
|
|
f = fopen(filename, "re");
|
|
|
|
if (f == NULL)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), f)) {
|
|
|
|
size_t len;
|
2013-07-10 14:59:24 +02:00
|
|
|
char *pos;
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
/* comment line */
|
2012-10-22 18:23:08 +02:00
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
/* strip trailing comment */
|
|
|
|
pos = strchr(line, '#');
|
|
|
|
if (pos)
|
|
|
|
pos[0] = '\0';
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
/* strip trailing whitespace */
|
2012-10-22 18:23:08 +02:00
|
|
|
len = strlen(line);
|
2013-07-10 14:59:24 +02:00
|
|
|
while (len > 0 && isspace(line[len-1]))
|
|
|
|
len--;
|
|
|
|
line[len] = '\0';
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case HW_NONE:
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (line[0] == ' ') {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("Error, MATCH expected but got '%s' in '%s':", line, filename);
|
2013-07-10 14:59:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
/* start of record, first match */
|
|
|
|
state = HW_MATCH;
|
|
|
|
udev_list_entry_add(&match_list, line, NULL);
|
|
|
|
break;
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
case HW_MATCH:
|
|
|
|
if (len == 0) {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("Error, DATA expected but got empty line in '%s':", filename);
|
2013-07-10 14:59:24 +02:00
|
|
|
state = HW_NONE;
|
|
|
|
udev_list_cleanup(&match_list);
|
|
|
|
break;
|
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
/* another match */
|
|
|
|
if (line[0] != ' ') {
|
|
|
|
udev_list_entry_add(&match_list, line, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first data */
|
|
|
|
state = HW_DATA;
|
|
|
|
insert_data(trie, &match_list, line, filename);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HW_DATA:
|
|
|
|
/* end of record */
|
|
|
|
if (len == 0) {
|
|
|
|
state = HW_NONE;
|
|
|
|
udev_list_cleanup(&match_list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] != ' ') {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("Error, DATA expected but got '%s' in '%s':", line, filename);
|
2013-07-10 14:59:24 +02:00
|
|
|
state = HW_NONE;
|
|
|
|
udev_list_cleanup(&match_list);
|
|
|
|
break;
|
|
|
|
}
|
2013-07-01 19:39:50 +02:00
|
|
|
|
2013-07-10 14:59:24 +02:00
|
|
|
insert_data(trie, &match_list, line, filename);
|
|
|
|
break;
|
|
|
|
};
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
2013-07-10 14:59:24 +02:00
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
fclose(f);
|
2013-07-10 14:59:24 +02:00
|
|
|
udev_list_cleanup(&match_list);
|
2012-10-22 18:23:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void help(void) {
|
2017-12-05 15:30:10 +01:00
|
|
|
printf("%s hwdb [OPTIONS]\n\n"
|
|
|
|
" -h --help Print this message\n"
|
|
|
|
" -V --version Print version of the program\n"
|
|
|
|
" -u --update Update the hardware database\n"
|
|
|
|
" --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
|
|
|
|
" -t --test=MODALIAS Query database and print result\n"
|
|
|
|
" -r --root=PATH Alternative root path in the filesystem\n\n"
|
|
|
|
"NOTE:\n"
|
|
|
|
"The sub-command 'hwdb' is deprecated, and is left for backwards compatibility.\n"
|
|
|
|
"Please use systemd-hwdb instead.\n"
|
|
|
|
, program_invocation_short_name);
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
|
2014-10-17 15:01:01 +02:00
|
|
|
enum {
|
|
|
|
ARG_USR = 0x100,
|
|
|
|
};
|
|
|
|
|
2012-10-22 18:23:08 +02:00
|
|
|
static const struct option options[] = {
|
2017-12-05 15:30:10 +01:00
|
|
|
{ "update", no_argument, NULL, 'u' },
|
|
|
|
{ "usr", no_argument, NULL, ARG_USR },
|
|
|
|
{ "test", required_argument, NULL, 't' },
|
|
|
|
{ "root", required_argument, NULL, 'r' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-10-22 18:23:08 +02:00
|
|
|
{}
|
|
|
|
};
|
2012-10-28 04:41:15 +01:00
|
|
|
const char *test = NULL;
|
2013-02-08 12:00:05 +01:00
|
|
|
const char *root = "";
|
2014-10-17 15:01:01 +02:00
|
|
|
const char *hwdb_bin_dir = "/etc/udev";
|
2012-10-22 18:23:08 +02:00
|
|
|
bool update = false;
|
2012-10-28 04:41:15 +01:00
|
|
|
struct trie *trie = NULL;
|
2013-12-18 03:48:14 +01:00
|
|
|
int err, c;
|
2012-10-22 18:23:08 +02:00
|
|
|
int rc = EXIT_SUCCESS;
|
|
|
|
|
2017-12-05 15:30:10 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "ut:r:Vh", options, NULL)) >= 0)
|
2013-12-18 03:48:14 +01:00
|
|
|
switch(c) {
|
2012-10-22 18:23:08 +02:00
|
|
|
case 'u':
|
|
|
|
update = true;
|
|
|
|
break;
|
2014-10-17 15:01:01 +02:00
|
|
|
case ARG_USR:
|
|
|
|
hwdb_bin_dir = UDEVLIBEXECDIR;
|
|
|
|
break;
|
2012-10-28 04:41:15 +01:00
|
|
|
case 't':
|
|
|
|
test = optarg;
|
|
|
|
break;
|
2013-02-08 12:00:05 +01:00
|
|
|
case 'r':
|
|
|
|
root = optarg;
|
|
|
|
break;
|
2017-12-05 15:30:10 +01:00
|
|
|
case 'V':
|
|
|
|
print_version();
|
|
|
|
return EXIT_SUCCESS;
|
2012-10-22 18:23:08 +02:00
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
return EXIT_SUCCESS;
|
2013-12-18 03:48:14 +01:00
|
|
|
case '?':
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown option");
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
|
|
|
|
2012-10-28 04:41:15 +01:00
|
|
|
if (!update && !test) {
|
2013-12-18 03:48:14 +01:00
|
|
|
log_error("Either --update or --test must be used");
|
|
|
|
return EXIT_FAILURE;
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
|
|
|
|
2012-10-28 04:41:15 +01:00
|
|
|
if (update) {
|
|
|
|
char **files, **f;
|
2013-02-08 12:00:05 +01:00
|
|
|
_cleanup_free_ char *hwdb_bin = NULL;
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2014-02-13 14:45:51 +01:00
|
|
|
trie = new0(struct trie, 1);
|
2012-10-28 04:41:15 +01:00
|
|
|
if (!trie) {
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2012-10-28 04:41:15 +01:00
|
|
|
/* string store */
|
|
|
|
trie->strings = strbuf_new();
|
|
|
|
if (!trie->strings) {
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
|
2012-10-28 04:41:15 +01:00
|
|
|
/* index */
|
2014-02-13 14:45:51 +01:00
|
|
|
trie->root = new0(struct trie_node, 1);
|
2012-10-28 04:41:15 +01:00
|
|
|
if (!trie->root) {
|
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
trie->nodes_count++;
|
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
err = conf_files_list_strv(&files, ".hwdb", root, 0, conf_file_dirs);
|
2012-10-28 04:41:15 +01:00
|
|
|
if (err < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(err, "failed to enumerate hwdb files: %m");
|
2012-10-28 04:41:15 +01:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
STRV_FOREACH(f, files) {
|
|
|
|
log_debug("reading file '%s'", *f);
|
2013-07-10 14:59:24 +02:00
|
|
|
import_file(udev, trie, *f);
|
2012-10-28 04:41:15 +01:00
|
|
|
}
|
|
|
|
strv_free(files);
|
|
|
|
|
|
|
|
strbuf_complete(trie->strings);
|
|
|
|
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("=== trie in-memory ===");
|
|
|
|
log_debug("nodes: %8zu bytes (%8zu)",
|
2012-10-31 01:55:51 +01:00
|
|
|
trie->nodes_count * sizeof(struct trie_node), trie->nodes_count);
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("children arrays: %8zu bytes (%8zu)",
|
2012-10-31 01:55:51 +01:00
|
|
|
trie->children_count * sizeof(struct trie_child_entry), trie->children_count);
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("values arrays: %8zu bytes (%8zu)",
|
2012-10-31 01:55:51 +01:00
|
|
|
trie->values_count * sizeof(struct trie_value_entry), trie->values_count);
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("strings: %8zu bytes",
|
2012-10-31 01:55:51 +01:00
|
|
|
trie->strings->len);
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("strings incoming: %8zu bytes (%8zu)",
|
2012-10-31 01:55:51 +01:00
|
|
|
trie->strings->in_len, trie->strings->in_count);
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("strings dedup'ed: %8zu bytes (%8zu)",
|
2012-10-31 01:55:51 +01:00
|
|
|
trie->strings->dedup_len, trie->strings->dedup_count);
|
2012-10-28 04:41:15 +01:00
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
hwdb_bin = strjoin(root, "/", hwdb_bin_dir, "/hwdb.bin");
|
2014-10-17 15:01:01 +02:00
|
|
|
if (!hwdb_bin) {
|
2013-02-08 12:00:05 +01:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-07-08 17:43:05 +02:00
|
|
|
|
|
|
|
mkdir_parents_label(hwdb_bin, 0755);
|
|
|
|
|
2013-02-08 12:00:05 +01:00
|
|
|
err = trie_store(trie, hwdb_bin);
|
2012-10-28 04:41:15 +01:00
|
|
|
if (err < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(err, "Failure writing database %s: %m", hwdb_bin);
|
2012-10-28 04:41:15 +01:00
|
|
|
rc = EXIT_FAILURE;
|
|
|
|
}
|
2016-07-08 17:43:05 +02:00
|
|
|
|
|
|
|
label_fix(hwdb_bin, false, false);
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
2012-10-28 04:41:15 +01:00
|
|
|
|
|
|
|
if (test) {
|
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-15 19:48:21 +01:00
|
|
|
int r;
|
2012-10-28 04:41:15 +01:00
|
|
|
|
2014-12-15 19:48:21 +01:00
|
|
|
r = sd_hwdb_new(&hwdb);
|
|
|
|
if (r >= 0) {
|
|
|
|
const char *key, *value;
|
2012-10-28 04:41:15 +01:00
|
|
|
|
2014-12-15 19:48:21 +01:00
|
|
|
SD_HWDB_FOREACH_PROPERTY(hwdb, test, key, value)
|
|
|
|
printf("%s=%s\n", key, value);
|
2012-10-28 04:41:15 +01:00
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
}
|
|
|
|
out:
|
2012-10-28 04:41:15 +01:00
|
|
|
if (trie) {
|
|
|
|
if (trie->root)
|
|
|
|
trie_node_cleanup(trie->root);
|
|
|
|
strbuf_cleanup(trie->strings);
|
|
|
|
free(trie);
|
|
|
|
}
|
2012-10-22 18:23:08 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct udevadm_cmd udevadm_hwdb = {
|
|
|
|
.name = "hwdb",
|
|
|
|
.cmd = adm_hwdb,
|
|
|
|
};
|