libudev-hwdb.c revision f4443fa5976eca51661947dd4df60847213f27fa
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2012 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2008 Alan Jenkins <alan.christopher.jenkins@googlemail.com>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering * SECTION:libudev-hwdb
c34255bdb217c2a1d3ac6348252437ab8be9ca46Lennart Poettering * @short_description: retrieve properties from the hardware database
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Libudev hardware database interface.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Opaque object representing the hardware database.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void linebuf_init(struct linebuf *buf) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char *linebuf_get(struct linebuf *buf) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool linebuf_add(struct linebuf *buf, const char *s, size_t len) {
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering if (buf->len + len >= sizeof(buf->bytes))
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic bool linebuf_add_char(struct linebuf *buf, char c)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void linebuf_rem(struct linebuf *buf, size_t count) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void linebuf_rem_char(struct linebuf *buf) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const struct trie_child_entry_f *trie_node_children(struct udev_hwdb *hwdb, const struct trie_node_f *node) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return (const struct trie_child_entry_f *)((const char *)node + le64toh(hwdb->head->node_size));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const struct trie_value_entry_f *trie_node_values(struct udev_hwdb *hwdb, const struct trie_node_f *node) {
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering base += node->children_count * le64toh(hwdb->head->child_entry_size);
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering return (const struct trie_value_entry_f *)base;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const struct trie_node_f *trie_node_from_off(struct udev_hwdb *hwdb, le64_t off) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return (const struct trie_node_f *)(hwdb->map + le64toh(off));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char *trie_string(struct udev_hwdb *hwdb, le64_t off) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int trie_children_cmp_f(const void *v1, const void *v2) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const struct trie_child_entry_f *n1 = v1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const struct trie_child_entry_f *n2 = v2;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const struct trie_node_f *node_lookup_f(struct udev_hwdb *hwdb, const struct trie_node_f *node, uint8_t c) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering child = bsearch(&search, trie_node_children(hwdb, node), node->children_count,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering le64toh(hwdb->head->child_entry_size), trie_children_cmp_f);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return trie_node_from_off(hwdb, child->child_off);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidtstatic int hwdb_add_property(struct udev_hwdb *hwdb, const char *key, const char *value) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* TODO: add sub-matches (+) against DMI data */
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (udev_list_entry_add(&hwdb->properties_list, key+1, value) == NULL)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int trie_fnmatch_f(struct udev_hwdb *hwdb, const struct trie_node_f *node, size_t p,
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering struct linebuf *buf, const char *search) {
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering prefix = trie_string(hwdb, node->prefix_off);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering for (i = 0; i < node->children_count; i++) {
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering const struct trie_child_entry_f *child = &trie_node_children(hwdb, node)[i];
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering err = trie_fnmatch_f(hwdb, trie_node_from_off(hwdb, child->child_off), 0, buf, search);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (le64toh(node->values_count) && fnmatch(linebuf_get(buf), search, 0) == 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt for (i = 0; i < le64toh(node->values_count); i++) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[i].key_off),
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering trie_string(hwdb, trie_node_values(hwdb, node)[i].value_off));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int trie_search_f(struct udev_hwdb *hwdb, const char *search) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering node = trie_node_from_off(hwdb, hwdb->head->nodes_root_off);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering for (; (c = trie_string(hwdb, node->prefix_off)[p]); p++) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return trie_fnmatch_f(hwdb, node, p, &buf, search + i + p);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (c != search[i + p])
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (n = 0; n < le64toh(node->values_count); n++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[n].key_off),
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering trie_string(hwdb, trie_node_values(hwdb, node)[n].value_off));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering child = node_lookup_f(hwdb, node, search[i]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * udev_hwdb_new:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * @udev: udev library context
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Create a hardware database context to query properties for devices.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Returns: a hwdb context.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_list_init(udev, &hwdb->properties_list, true);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering log_debug("error reading %s: %m", HWDB_BIN);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering if (fstat(fileno(hwdb->f), &hwdb->st) < 0 ||
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering (size_t)hwdb->st.st_size < offsetof(struct trie_header_f, strings_len) + 8) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("error reading %s: %m", HWDB_BIN);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering hwdb->map = mmap(0, hwdb->st.st_size, PROT_READ, MAP_SHARED, fileno(hwdb->f), 0);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering log_debug("error mapping %s: %m", HWDB_BIN);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (memcmp(hwdb->map, sig, sizeof(hwdb->head->signature)) != 0 ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (size_t)hwdb->st.st_size != le64toh(hwdb->head->file_size)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("error recognizing the format of %s", HWDB_BIN);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("tool version: %llu", (unsigned long long)le64toh(hwdb->head->tool_version));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("file size: %8llu bytes\n", (unsigned long long)hwdb->st.st_size);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("header size %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->header_size));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("strings %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->strings_len));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("nodes %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->nodes_len));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * udev_hwdb_ref:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * @hwdb: context
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering * Take a reference of a hwdb context.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Returns: the passed enumeration context
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt * udev_hwdb_unref:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * @hwdb: context
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Drop a reference of a hwdb context. If the refcount reaches zero,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek * all resources of the hwdb context will be released.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Returns: the passed hwdb context if it has still an active reference, or #NULL otherwise.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering munmap((void *)hwdb->map, hwdb->st.st_size);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_list_cleanup(&hwdb->properties_list);
return NULL;
if (!hwdb)
if (!hwdb->f)
_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) {
int err;
return NULL;
if (err < 0) {
return NULL;