[systemd-commits] 3 commits - Makefile.am src/libsystemd src/libudev src/systemd src/udev test/udev-test.pl

Tom Gundersen tomegun at kemper.freedesktop.org
Thu Dec 11 05:28:20 PST 2014


 Makefile.am                            |   10 
 src/libsystemd/sd-hwdb/Makefile        |    1 
 src/libsystemd/sd-hwdb/hwdb-internal.h |   70 ++++
 src/libsystemd/sd-hwdb/hwdb-util.h     |   31 ++
 src/libsystemd/sd-hwdb/sd-hwdb.c       |  471 +++++++++++++++++++++++++++++++++
 src/libudev/libudev-hwdb-def.h         |   74 -----
 src/libudev/libudev-hwdb.c             |  327 +---------------------
 src/systemd/sd-hwdb.h                  |   46 +++
 src/udev/udevadm-hwdb.c                |    2 
 test/udev-test.pl                      |    4 
 10 files changed, 656 insertions(+), 380 deletions(-)

New commits:
commit 02cd084db7b50a45a76bcdfe03668061bf7c79d6
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Dec 11 14:17:35 2014 +0100

    tests: udev - assume /etc/machine-id rather than /etc/hosts
    
    On todays machines /etc/hosts is not mandatory. /etc/machine-id is though, so let's rely on that instead.
    
    This makes the udev tests pass again for me.

diff --git a/test/udev-test.pl b/test/udev-test.pl
index 3e05b61..d9b7967 100755
--- a/test/udev-test.pl
+++ b/test/udev-test.pl
@@ -1233,8 +1233,8 @@ EOF
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                 exp_name        => "there",
                 rules           => <<EOF
-TEST=="/etc/hosts", SYMLINK+="there"
-TEST!="/etc/hosts", SYMLINK+="notthere"
+TEST=="/etc/machine-id", SYMLINK+="there"
+TEST!="/etc/machine-id", SYMLINK+="notthere"
 EOF
         },
         {

commit 8b516fdea74127327b0945bb50690bd70c6b6692
Author: Tom Gundersen <teg at jklm.no>
Date:   Wed Dec 3 17:32:40 2014 +0100

    libudev: make libudev-hwdb a wrapper around sd-hwdb

diff --git a/Makefile.am b/Makefile.am
index 7ad67fc..5c8f6a6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3605,7 +3605,8 @@ test_libudev_SOURCES = \
 test_libudev_LDADD = \
 	libsystemd-label.la \
 	libudev-internal.la \
-	libsystemd-shared.la
+	libsystemd-shared.la \
+	libsystemd-internal.la
 
 test_udev_SOURCES = \
 	src/test/test-udev.c
diff --git a/src/libudev/libudev-hwdb-def.h b/src/libudev/libudev-hwdb-def.h
deleted file mode 100644
index b76a13f..0000000
--- a/src/libudev/libudev-hwdb-def.h
+++ /dev/null
@@ -1,74 +0,0 @@
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Kay Sievers <kay at vrfy.org>
-
-  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/>.
-***/
-
-#ifndef _LIBUDEV_HWDB_DEF_H_
-#define _LIBUDEV_HWDB_DEF_H_
-
-#include "sparse-endian.h"
-
-#define HWDB_SIG { 'K', 'S', 'L', 'P', 'H', 'H', 'R', 'H' }
-
-/* on-disk trie objects */
-struct trie_header_f {
-        uint8_t signature[8];
-
-        /* version of tool which created the file */
-        le64_t tool_version;
-        le64_t file_size;
-
-        /* size of structures to allow them to grow */
-        le64_t header_size;
-        le64_t node_size;
-        le64_t child_entry_size;
-        le64_t value_entry_size;
-
-        /* offset of the root trie node */
-        le64_t nodes_root_off;
-
-        /* size of the nodes and string section */
-        le64_t nodes_len;
-        le64_t strings_len;
-} _packed_;
-
-struct trie_node_f {
-        /* prefix of lookup string, shared by all children  */
-        le64_t prefix_off;
-        /* size of children entry array appended to the node */
-        uint8_t children_count;
-        uint8_t padding[7];
-        /* size of value entry array appended to the node */
-        le64_t values_count;
-} _packed_;
-
-/* array of child entries, follows directly the node record */
-struct trie_child_entry_f {
-        /* index of the child node */
-        uint8_t c;
-        uint8_t padding[7];
-        /* offset of the child node */
-        le64_t child_off;
-} _packed_;
-
-/* array of value entries, follows directly the node record/child array */
-struct trie_value_entry_f {
-        le64_t key_off;
-        le64_t value_off;
-} _packed_;
-
-#endif
diff --git a/src/libudev/libudev-hwdb.c b/src/libudev/libudev-hwdb.c
index 05a6858..7a21b3d 100644
--- a/src/libudev/libudev-hwdb.c
+++ b/src/libudev/libudev-hwdb.c
@@ -1,8 +1,7 @@
 /***
   This file is part of systemd.
 
-  Copyright 2012 Kay Sievers <kay at vrfy.org>
-  Copyright 2008 Alan Jenkins <alan.christopher.jenkins at googlemail.com>
+  Copyright Tom Gundersen <teg at jklm.no>
 
   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
@@ -18,18 +17,9 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <inttypes.h>
-#include <ctype.h>
-#include <stdlib.h>
-#include <fnmatch.h>
-#include <getopt.h>
-#include <sys/mman.h>
-
 #include "libudev-private.h"
-#include "libudev-hwdb-def.h"
+#include "sd-hwdb.h"
+#include "hwdb-util.h"
 
 /**
  * SECTION:libudev-hwdb
@@ -47,220 +37,11 @@ struct udev_hwdb {
         struct udev *udev;
         int refcount;
 
-        FILE *f;
-        struct stat st;
-        union {
-                struct trie_header_f *head;
-                const char *map;
-        };
+        sd_hwdb *hwdb;
 
         struct udev_list properties_list;
 };
 
-struct linebuf {
-        char bytes[LINE_MAX];
-        size_t size;
-        size_t len;
-};
-
-static void linebuf_init(struct linebuf *buf) {
-        buf->size = 0;
-        buf->len = 0;
-}
-
-static const char *linebuf_get(struct linebuf *buf) {
-        if (buf->len + 1 >= sizeof(buf->bytes))
-                return NULL;
-        buf->bytes[buf->len] = '\0';
-        return buf->bytes;
-}
-
-static bool linebuf_add(struct linebuf *buf, const char *s, size_t len) {
-        if (buf->len + len >= sizeof(buf->bytes))
-                return false;
-        memcpy(buf->bytes + buf->len, s, len);
-        buf->len += len;
-        return true;
-}
-
-static bool linebuf_add_char(struct linebuf *buf, char c)
-{
-        if (buf->len + 1 >= sizeof(buf->bytes))
-                return false;
-        buf->bytes[buf->len++] = c;
-        return true;
-}
-
-static void linebuf_rem(struct linebuf *buf, size_t count) {
-        assert(buf->len >= count);
-        buf->len -= count;
-}
-
-static void linebuf_rem_char(struct linebuf *buf) {
-        linebuf_rem(buf, 1);
-}
-
-static const struct trie_child_entry_f *trie_node_children(struct udev_hwdb *hwdb, const struct trie_node_f *node) {
-        return (const struct trie_child_entry_f *)((const char *)node + le64toh(hwdb->head->node_size));
-}
-
-static const struct trie_value_entry_f *trie_node_values(struct udev_hwdb *hwdb, const struct trie_node_f *node) {
-        const char *base = (const char *)node;
-
-        base += le64toh(hwdb->head->node_size);
-        base += node->children_count * le64toh(hwdb->head->child_entry_size);
-        return (const struct trie_value_entry_f *)base;
-}
-
-static const struct trie_node_f *trie_node_from_off(struct udev_hwdb *hwdb, le64_t off) {
-        return (const struct trie_node_f *)(hwdb->map + le64toh(off));
-}
-
-static const char *trie_string(struct udev_hwdb *hwdb, le64_t off) {
-        return hwdb->map + le64toh(off);
-}
-
-static int trie_children_cmp_f(const void *v1, const void *v2) {
-        const struct trie_child_entry_f *n1 = v1;
-        const struct trie_child_entry_f *n2 = v2;
-
-        return n1->c - n2->c;
-}
-
-static const struct trie_node_f *node_lookup_f(struct udev_hwdb *hwdb, const struct trie_node_f *node, uint8_t c) {
-        struct trie_child_entry_f *child;
-        struct trie_child_entry_f search;
-
-        search.c = c;
-        child = bsearch(&search, trie_node_children(hwdb, node), node->children_count,
-                        le64toh(hwdb->head->child_entry_size), trie_children_cmp_f);
-        if (child)
-                return trie_node_from_off(hwdb, child->child_off);
-        return NULL;
-}
-
-static int hwdb_add_property(struct udev_hwdb *hwdb, const char *key, const char *value) {
-        /*
-         * Silently ignore all properties which do not start with a
-         * space; future extensions might use additional prefixes.
-         */
-        if (key[0] != ' ')
-                return 0;
-
-        if (udev_list_entry_add(&hwdb->properties_list, key+1, value) == NULL)
-                return -ENOMEM;
-        return 0;
-}
-
-static int trie_fnmatch_f(struct udev_hwdb *hwdb, const struct trie_node_f *node, size_t p,
-                          struct linebuf *buf, const char *search) {
-        size_t len;
-        size_t i;
-        const char *prefix;
-        int err;
-
-        prefix = trie_string(hwdb, node->prefix_off);
-        len = strlen(prefix + p);
-        linebuf_add(buf, prefix + p, len);
-
-        for (i = 0; i < node->children_count; i++) {
-                const struct trie_child_entry_f *child = &trie_node_children(hwdb, node)[i];
-
-                linebuf_add_char(buf, child->c);
-                err = trie_fnmatch_f(hwdb, trie_node_from_off(hwdb, child->child_off), 0, buf, search);
-                if (err < 0)
-                        return err;
-                linebuf_rem_char(buf);
-        }
-
-        if (le64toh(node->values_count) && fnmatch(linebuf_get(buf), search, 0) == 0)
-                for (i = 0; i < le64toh(node->values_count); i++) {
-                        err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[i].key_off),
-                                                trie_string(hwdb, trie_node_values(hwdb, node)[i].value_off));
-                        if (err < 0)
-                                return err;
-                }
-
-        linebuf_rem(buf, len);
-        return 0;
-}
-
-static int trie_search_f(struct udev_hwdb *hwdb, const char *search) {
-        struct linebuf buf;
-        const struct trie_node_f *node;
-        size_t i = 0;
-        int err;
-
-        linebuf_init(&buf);
-
-        node = trie_node_from_off(hwdb, hwdb->head->nodes_root_off);
-        while (node) {
-                const struct trie_node_f *child;
-                size_t p = 0;
-
-                if (node->prefix_off) {
-                        uint8_t c;
-
-                        for (; (c = trie_string(hwdb, node->prefix_off)[p]); p++) {
-                                if (c == '*' || c == '?' || c == '[')
-                                        return trie_fnmatch_f(hwdb, node, p, &buf, search + i + p);
-                                if (c != search[i + p])
-                                        return 0;
-                        }
-                        i += p;
-                }
-
-                child = node_lookup_f(hwdb, node, '*');
-                if (child) {
-                        linebuf_add_char(&buf, '*');
-                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
-                        if (err < 0)
-                                return err;
-                        linebuf_rem_char(&buf);
-                }
-
-                child = node_lookup_f(hwdb, node, '?');
-                if (child) {
-                        linebuf_add_char(&buf, '?');
-                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
-                        if (err < 0)
-                                return err;
-                        linebuf_rem_char(&buf);
-                }
-
-                child = node_lookup_f(hwdb, node, '[');
-                if (child) {
-                        linebuf_add_char(&buf, '[');
-                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
-                        if (err < 0)
-                                return err;
-                        linebuf_rem_char(&buf);
-                }
-
-                if (search[i] == '\0') {
-                        size_t n;
-
-                        for (n = 0; n < le64toh(node->values_count); n++) {
-                                err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[n].key_off),
-                                                        trie_string(hwdb, trie_node_values(hwdb, node)[n].value_off));
-                                if (err < 0)
-                                        return err;
-                        }
-                        return 0;
-                }
-
-                child = node_lookup_f(hwdb, node, search[i]);
-                node = child;
-                i++;
-        }
-        return 0;
-}
-
-static const char hwdb_bin_paths[] =
-    "/etc/udev/hwdb.bin\0"
-    UDEVLIBEXECDIR "/hwdb.bin\0";
-
-
 /**
  * udev_hwdb_new:
  * @udev: udev library context
@@ -270,64 +51,26 @@ static const char hwdb_bin_paths[] =
  * Returns: a hwdb context.
  **/
 _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
+        _cleanup_hwdb_unref_ sd_hwdb *hwdb_internal = NULL;
         struct udev_hwdb *hwdb;
-        const char *hwdb_bin_path;
-        const char sig[] = HWDB_SIG;
-
-        hwdb = new0(struct udev_hwdb, 1);
-        if (!hwdb)
-                return NULL;
-
-        hwdb->refcount = 1;
-        udev_list_init(udev, &hwdb->properties_list, true);
+        int r;
 
-        /* find hwdb.bin in hwdb_bin_paths */
-        NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
-                hwdb->f = fopen(hwdb_bin_path, "re");
-                if (hwdb->f)
-                        break;
-                else if (errno == ENOENT)
-                        continue;
-                else {
-                        log_debug_errno(errno, "error reading %s: %m", hwdb_bin_path);
-                        udev_hwdb_unref(hwdb);
-                        return NULL;
-                }
-        }
+        assert_return(udev, NULL);
 
-        if (!hwdb->f) {
-                log_debug("hwdb.bin does not exist, please run udevadm hwdb --update");
-                udev_hwdb_unref(hwdb);
+        r = sd_hwdb_new(&hwdb_internal);
+        if (r < 0)
                 return NULL;
-        }
 
-        if (fstat(fileno(hwdb->f), &hwdb->st) < 0 ||
-            (size_t)hwdb->st.st_size < offsetof(struct trie_header_f, strings_len) + 8) {
-                log_debug_errno(errno, "error reading %s: %m", hwdb_bin_path);
-                udev_hwdb_unref(hwdb);
+        hwdb = new0(struct udev_hwdb, 1);
+        if (!hwdb)
                 return NULL;
-        }
 
-        hwdb->map = mmap(0, hwdb->st.st_size, PROT_READ, MAP_SHARED, fileno(hwdb->f), 0);
-        if (hwdb->map == MAP_FAILED) {
-                log_debug_errno(errno, "error mapping %s: %m", hwdb_bin_path);
-                udev_hwdb_unref(hwdb);
-                return NULL;
-        }
+        hwdb->refcount = 1;
+        hwdb->hwdb = hwdb_internal;
+        hwdb_internal = NULL;
 
-        if (memcmp(hwdb->map, sig, sizeof(hwdb->head->signature)) != 0 ||
-            (size_t)hwdb->st.st_size != le64toh(hwdb->head->file_size)) {
-                log_debug("error recognizing the format of %s", hwdb_bin_path);
-                udev_hwdb_unref(hwdb);
-                return NULL;
-        }
+        udev_list_init(udev, &hwdb->properties_list, true);
 
-        log_debug("=== trie on-disk ===");
-        log_debug("tool version:          %"PRIu64, le64toh(hwdb->head->tool_version));
-        log_debug("file size:        %8"PRIu64" bytes", hwdb->st.st_size);
-        log_debug("header size       %8"PRIu64" bytes", le64toh(hwdb->head->header_size));
-        log_debug("strings           %8"PRIu64" bytes", le64toh(hwdb->head->strings_len));
-        log_debug("nodes             %8"PRIu64" bytes", le64toh(hwdb->head->nodes_len));
         return hwdb;
 }
 
@@ -361,38 +104,17 @@ _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
         hwdb->refcount--;
         if (hwdb->refcount > 0)
                 return NULL;
-        if (hwdb->map)
-                munmap((void *)hwdb->map, hwdb->st.st_size);
-        if (hwdb->f)
-                fclose(hwdb->f);
+        sd_hwdb_unref(hwdb->hwdb);
         udev_list_cleanup(&hwdb->properties_list);
         free(hwdb);
         return NULL;
 }
 
 bool udev_hwdb_validate(struct udev_hwdb *hwdb) {
-        bool found = false;
-        const char* p;
-        struct stat st;
-
         if (!hwdb)
                 return false;
-        if (!hwdb->f)
-                return false;
-
-        /* if hwdb.bin doesn't exist anywhere, we need to update */
-        NULSTR_FOREACH(p, hwdb_bin_paths) {
-                if (stat(p, &st) >= 0) {
-                        found = true;
-                        break;
-                }
-        }
-        if (!found)
-                return true;
 
-        if (timespec_load(&hwdb->st.st_mtim) != timespec_load(&st.st_mtim))
-                return true;
-        return false;
+        return hwdb_validate(hwdb->hwdb);
 }
 
 /**
@@ -409,18 +131,21 @@ bool udev_hwdb_validate(struct udev_hwdb *hwdb) {
  * Returns: a udev_list_entry.
  */
 _public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) {
-        int err;
+        const char *key, *value;
 
-        if (!hwdb || !hwdb->f) {
+        if (!hwdb || !modalias) {
                 errno = EINVAL;
                 return NULL;
         }
 
         udev_list_cleanup(&hwdb->properties_list);
-        err = trie_search_f(hwdb, modalias);
-        if (err < 0) {
-                errno = -err;
-                return NULL;
+
+        FOREACH_HWDB_PROPERTY(hwdb->hwdb, modalias, key, value) {
+                if (udev_list_entry_add(&hwdb->properties_list, key, value) == NULL) {
+                        errno = ENOMEM;
+                        return NULL;
+                }
         }
+
         return udev_list_get_entry(&hwdb->properties_list);
 }
diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c
index a5870d1..afd9f5a 100644
--- a/src/udev/udevadm-hwdb.c
+++ b/src/udev/udevadm-hwdb.c
@@ -28,7 +28,7 @@
 #include "conf-files.h"
 
 #include "udev.h"
-#include "libudev-hwdb-def.h"
+#include "hwdb-internal.h"
 
 /*
  * Generic udev properties, key/value database based on modalias strings.

commit 23fbe14f503c1e98292efc4ba1238adb7dc38d80
Author: Tom Gundersen <teg at jklm.no>
Date:   Wed Dec 3 17:02:34 2014 +0100

    libsystemd: add sd-hwdb library
    
    This is libudev-hwdb, but decoupled from libudev and in the libsystemd style.
    
    The core code is unchanged, apart from the following minor changes:
    
     - hwdb.bin located in /**/systemd/hwdb/ take preference over the ones located
       in /**/udev/
     - properties are stored internally in an OrderedHashmap, rather than a
       linked list.
     - a new API call allows individual properties to be queried directly, rather
       than iterating over them all
     - the iteration over properties have been moved inside the library, rather than
       exposing a list directly
     - the unused 'flags' parameter was dropped

diff --git a/Makefile.am b/Makefile.am
index ac7924e..7ad67fc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -217,6 +217,7 @@ AM_CPPFLAGS = \
 	-I $(top_srcdir)/src/libsystemd/sd-event \
 	-I $(top_srcdir)/src/libsystemd/sd-rtnl \
 	-I $(top_srcdir)/src/libsystemd/sd-network \
+	-I $(top_srcdir)/src/libsystemd/sd-hwdb \
 	-I $(top_srcdir)/src/libsystemd-network \
 	-I $(top_srcdir)/src/libsystemd-terminal \
 	$(OUR_CPPFLAGS)
@@ -2642,6 +2643,7 @@ libsystemd_internal_la_SOURCES = \
 	src/systemd/sd-daemon.h \
 	src/systemd/sd-path.h \
 	src/systemd/sd-network.h \
+	src/systemd/sd-hwdb.h \
 	src/libsystemd/sd-bus/sd-bus.c \
 	src/libsystemd/sd-bus/bus-control.c \
 	src/libsystemd/sd-bus/bus-control.h \
@@ -2702,7 +2704,10 @@ libsystemd_internal_la_SOURCES = \
 	src/libsystemd/sd-path/sd-path.c \
 	src/libsystemd/sd-network/sd-network.c \
 	src/libsystemd/sd-network/network-util.h \
-	src/libsystemd/sd-network/network-util.c
+	src/libsystemd/sd-network/network-util.c \
+	src/libsystemd/sd-hwdb/sd-hwdb.c \
+	src/libsystemd/sd-hwdb/hwdb-util.h \
+	src/libsystemd/sd-hwdb/hwdb-intenal.h
 
 nodist_libsystemd_internal_la_SOURCES = \
 	src/libsystemd/libsystemd.sym
diff --git a/src/libsystemd/sd-hwdb/Makefile b/src/libsystemd/sd-hwdb/Makefile
new file mode 120000
index 0000000..94aaae2
--- /dev/null
+++ b/src/libsystemd/sd-hwdb/Makefile
@@ -0,0 +1 @@
+../../Makefile
\ No newline at end of file
diff --git a/src/libsystemd/sd-hwdb/hwdb-internal.h b/src/libsystemd/sd-hwdb/hwdb-internal.h
new file mode 100644
index 0000000..fedccde
--- /dev/null
+++ b/src/libsystemd/sd-hwdb/hwdb-internal.h
@@ -0,0 +1,70 @@
+/***
+  This file is part of systemd.
+
+  Copyright 2012 Kay Sievers <kay at vrfy.org>
+
+  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/>.
+***/
+#pragma once
+
+#include "sparse-endian.h"
+
+#define HWDB_SIG { 'K', 'S', 'L', 'P', 'H', 'H', 'R', 'H' }
+
+/* on-disk trie objects */
+struct trie_header_f {
+        uint8_t signature[8];
+
+        /* version of tool which created the file */
+        le64_t tool_version;
+        le64_t file_size;
+
+        /* size of structures to allow them to grow */
+        le64_t header_size;
+        le64_t node_size;
+        le64_t child_entry_size;
+        le64_t value_entry_size;
+
+        /* offset of the root trie node */
+        le64_t nodes_root_off;
+
+        /* size of the nodes and string section */
+        le64_t nodes_len;
+        le64_t strings_len;
+} _packed_;
+
+struct trie_node_f {
+        /* prefix of lookup string, shared by all children  */
+        le64_t prefix_off;
+        /* size of children entry array appended to the node */
+        uint8_t children_count;
+        uint8_t padding[7];
+        /* size of value entry array appended to the node */
+        le64_t values_count;
+} _packed_;
+
+/* array of child entries, follows directly the node record */
+struct trie_child_entry_f {
+        /* index of the child node */
+        uint8_t c;
+        uint8_t padding[7];
+        /* offset of the child node */
+        le64_t child_off;
+} _packed_;
+
+/* array of value entries, follows directly the node record/child array */
+struct trie_value_entry_f {
+        le64_t key_off;
+        le64_t value_off;
+} _packed_;
diff --git a/src/libsystemd/sd-hwdb/hwdb-util.h b/src/libsystemd/sd-hwdb/hwdb-util.h
new file mode 100644
index 0000000..ee020a2
--- /dev/null
+++ b/src/libsystemd/sd-hwdb/hwdb-util.h
@@ -0,0 +1,31 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2014 Tom Gundersen <teg at jklm.no>
+
+  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/>.
+***/
+
+#include "util.h"
+
+#include "sd-hwdb.h"
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_hwdb*, sd_hwdb_unref);
+#define _cleanup_hwdb_unref_ _cleanup_(sd_hwdb_unrefp)
+
+bool hwdb_validate(sd_hwdb *hwdb);
diff --git a/src/libsystemd/sd-hwdb/sd-hwdb.c b/src/libsystemd/sd-hwdb/sd-hwdb.c
new file mode 100644
index 0000000..7444ab5
--- /dev/null
+++ b/src/libsystemd/sd-hwdb/sd-hwdb.c
@@ -0,0 +1,471 @@
+/***
+  This file is part of systemd.
+
+  Copyright 2012 Kay Sievers <kay at vrfy.org>
+  Copyright 2008 Alan Jenkins <alan.christopher.jenkins at googlemail.com>
+  Copyright 2014 Tom Gundersen <teg at jklm.no>
+
+  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/>.
+***/
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <inttypes.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <fnmatch.h>
+#include <getopt.h>
+#include <sys/mman.h>
+
+#include "sd-hwdb.h"
+
+#include "hashmap.h"
+#include "refcnt.h"
+
+#include "hwdb-util.h"
+#include "hwdb-internal.h"
+
+struct sd_hwdb {
+        RefCount n_ref;
+        int refcount;
+
+        FILE *f;
+        struct stat st;
+        union {
+                struct trie_header_f *head;
+                const char *map;
+        };
+
+        char *modalias;
+
+        OrderedHashmap *properties;
+        Iterator properties_iterator;
+        bool properties_modified;
+};
+
+struct linebuf {
+        char bytes[LINE_MAX];
+        size_t size;
+        size_t len;
+};
+
+static void linebuf_init(struct linebuf *buf) {
+        buf->size = 0;
+        buf->len = 0;
+}
+
+static const char *linebuf_get(struct linebuf *buf) {
+        if (buf->len + 1 >= sizeof(buf->bytes))
+                return NULL;
+        buf->bytes[buf->len] = '\0';
+        return buf->bytes;
+}
+
+static bool linebuf_add(struct linebuf *buf, const char *s, size_t len) {
+        if (buf->len + len >= sizeof(buf->bytes))
+                return false;
+        memcpy(buf->bytes + buf->len, s, len);
+        buf->len += len;
+        return true;
+}
+
+static bool linebuf_add_char(struct linebuf *buf, char c)
+{
+        if (buf->len + 1 >= sizeof(buf->bytes))
+                return false;
+        buf->bytes[buf->len++] = c;
+        return true;
+}
+
+static void linebuf_rem(struct linebuf *buf, size_t count) {
+        assert(buf->len >= count);
+        buf->len -= count;
+}
+
+static void linebuf_rem_char(struct linebuf *buf) {
+        linebuf_rem(buf, 1);
+}
+
+static const struct trie_child_entry_f *trie_node_children(sd_hwdb *hwdb, const struct trie_node_f *node) {
+        return (const struct trie_child_entry_f *)((const char *)node + le64toh(hwdb->head->node_size));
+}
+
+static const struct trie_value_entry_f *trie_node_values(sd_hwdb *hwdb, const struct trie_node_f *node) {
+        const char *base = (const char *)node;
+
+        base += le64toh(hwdb->head->node_size);
+        base += node->children_count * le64toh(hwdb->head->child_entry_size);
+        return (const struct trie_value_entry_f *)base;
+}
+
+static const struct trie_node_f *trie_node_from_off(sd_hwdb *hwdb, le64_t off) {
+        return (const struct trie_node_f *)(hwdb->map + le64toh(off));
+}
+
+static const char *trie_string(sd_hwdb *hwdb, le64_t off) {
+        return hwdb->map + le64toh(off);
+}
+
+static int trie_children_cmp_f(const void *v1, const void *v2) {
+        const struct trie_child_entry_f *n1 = v1;
+        const struct trie_child_entry_f *n2 = v2;
+
+        return n1->c - n2->c;
+}
+
+static const struct trie_node_f *node_lookup_f(sd_hwdb *hwdb, const struct trie_node_f *node, uint8_t c) {
+        struct trie_child_entry_f *child;
+        struct trie_child_entry_f search;
+
+        search.c = c;
+        child = bsearch(&search, trie_node_children(hwdb, node), node->children_count,
+                        le64toh(hwdb->head->child_entry_size), trie_children_cmp_f);
+        if (child)
+                return trie_node_from_off(hwdb, child->child_off);
+        return NULL;
+}
+
+static int hwdb_add_property(sd_hwdb *hwdb, const char *key, const char *value) {
+        int r;
+
+        assert(hwdb);
+        assert(key);
+        assert(value);
+
+        /*
+         * Silently ignore all properties which do not start with a
+         * space; future extensions might use additional prefixes.
+         */
+        if (key[0] != ' ')
+                return 0;
+
+        key++;
+
+        r = ordered_hashmap_ensure_allocated(&hwdb->properties, &string_hash_ops);
+        if (r < 0)
+                return r;
+
+        r = ordered_hashmap_replace(hwdb->properties, key, (char*)value);
+        if (r < 0)
+                return r;
+
+        hwdb->properties_modified = true;
+
+        return 0;
+}
+
+static int trie_fnmatch_f(sd_hwdb *hwdb, const struct trie_node_f *node, size_t p,
+                          struct linebuf *buf, const char *search) {
+        size_t len;
+        size_t i;
+        const char *prefix;
+        int err;
+
+        prefix = trie_string(hwdb, node->prefix_off);
+        len = strlen(prefix + p);
+        linebuf_add(buf, prefix + p, len);
+
+        for (i = 0; i < node->children_count; i++) {
+                const struct trie_child_entry_f *child = &trie_node_children(hwdb, node)[i];
+
+                linebuf_add_char(buf, child->c);
+                err = trie_fnmatch_f(hwdb, trie_node_from_off(hwdb, child->child_off), 0, buf, search);
+                if (err < 0)
+                        return err;
+                linebuf_rem_char(buf);
+        }
+
+        if (le64toh(node->values_count) && fnmatch(linebuf_get(buf), search, 0) == 0)
+                for (i = 0; i < le64toh(node->values_count); i++) {
+                        err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[i].key_off),
+                                                trie_string(hwdb, trie_node_values(hwdb, node)[i].value_off));
+                        if (err < 0)
+                                return err;
+                }
+
+        linebuf_rem(buf, len);
+        return 0;
+}
+
+static int trie_search_f(sd_hwdb *hwdb, const char *search) {
+        struct linebuf buf;
+        const struct trie_node_f *node;
+        size_t i = 0;
+        int err;
+
+        linebuf_init(&buf);
+
+        node = trie_node_from_off(hwdb, hwdb->head->nodes_root_off);
+        while (node) {
+                const struct trie_node_f *child;
+                size_t p = 0;
+
+                if (node->prefix_off) {
+                        uint8_t c;
+
+                        for (; (c = trie_string(hwdb, node->prefix_off)[p]); p++) {
+                                if (c == '*' || c == '?' || c == '[')
+                                        return trie_fnmatch_f(hwdb, node, p, &buf, search + i + p);
+                                if (c != search[i + p])
+                                        return 0;
+                        }
+                        i += p;
+                }
+
+                child = node_lookup_f(hwdb, node, '*');
+                if (child) {
+                        linebuf_add_char(&buf, '*');
+                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
+                        if (err < 0)
+                                return err;
+                        linebuf_rem_char(&buf);
+                }
+
+                child = node_lookup_f(hwdb, node, '?');
+                if (child) {
+                        linebuf_add_char(&buf, '?');
+                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
+                        if (err < 0)
+                                return err;
+                        linebuf_rem_char(&buf);
+                }
+
+                child = node_lookup_f(hwdb, node, '[');
+                if (child) {
+                        linebuf_add_char(&buf, '[');
+                        err = trie_fnmatch_f(hwdb, child, 0, &buf, search + i);
+                        if (err < 0)
+                                return err;
+                        linebuf_rem_char(&buf);
+                }
+
+                if (search[i] == '\0') {
+                        size_t n;
+
+                        for (n = 0; n < le64toh(node->values_count); n++) {
+                                err = hwdb_add_property(hwdb, trie_string(hwdb, trie_node_values(hwdb, node)[n].key_off),
+                                                        trie_string(hwdb, trie_node_values(hwdb, node)[n].value_off));
+                                if (err < 0)
+                                        return err;
+                        }
+                        return 0;
+                }
+
+                child = node_lookup_f(hwdb, node, search[i]);
+                node = child;
+                i++;
+        }
+        return 0;
+}
+
+static const char hwdb_bin_paths[] =
+    "/etc/systemd/hwdb/hwdb.bin\0"
+    "/etc/udev/hwdb.bin\0"
+    "/usr/lib/systemd/hwdb/hwdb.bin\0"
+#ifdef HAVE_SPLIT_USR
+    "/lib/systemd/hwdb/hwdb.bin\0"
+#endif
+    UDEVLIBEXECDIR "/hwdb.bin\0";
+
+_public_ int sd_hwdb_new(sd_hwdb **ret) {
+        _cleanup_hwdb_unref_ sd_hwdb *hwdb = NULL;
+        const char *hwdb_bin_path;
+        const char sig[] = HWDB_SIG;
+
+        assert_return(ret, -EINVAL);
+
+        hwdb = new0(sd_hwdb, 1);
+        if (!hwdb)
+                return -ENOMEM;
+
+        hwdb->n_ref = REFCNT_INIT;
+
+        /* find hwdb.bin in hwdb_bin_paths */
+        NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
+                hwdb->f = fopen(hwdb_bin_path, "re");
+                if (hwdb->f)
+                        break;
+                else if (errno == ENOENT)
+                        continue;
+                else
+                        return log_debug_errno(errno, "error reading %s: %m", hwdb_bin_path);
+        }
+
+        if (!hwdb->f) {
+                log_debug("hwdb.bin does not exist, please run udevadm hwdb --update");
+                return -ENOENT;
+        }
+
+        if (fstat(fileno(hwdb->f), &hwdb->st) < 0 ||
+            (size_t)hwdb->st.st_size < offsetof(struct trie_header_f, strings_len) + 8)
+                return log_debug_errno(errno, "error reading %s: %m", hwdb_bin_path);
+
+        hwdb->map = mmap(0, hwdb->st.st_size, PROT_READ, MAP_SHARED, fileno(hwdb->f), 0);
+        if (hwdb->map == MAP_FAILED)
+                return log_debug_errno(errno, "error mapping %s: %m", hwdb_bin_path);
+
+        if (memcmp(hwdb->map, sig, sizeof(hwdb->head->signature)) != 0 ||
+            (size_t)hwdb->st.st_size != le64toh(hwdb->head->file_size)) {
+                log_debug("error recognizing the format of %s", hwdb_bin_path);
+                return -EINVAL;;
+        }
+
+        log_debug("=== trie on-disk ===");
+        log_debug("tool version:          %"PRIu64, le64toh(hwdb->head->tool_version));
+        log_debug("file size:        %8"PRIu64" bytes", hwdb->st.st_size);
+        log_debug("header size       %8"PRIu64" bytes", le64toh(hwdb->head->header_size));
+        log_debug("strings           %8"PRIu64" bytes", le64toh(hwdb->head->strings_len));
+        log_debug("nodes             %8"PRIu64" bytes", le64toh(hwdb->head->nodes_len));
+
+        *ret = hwdb;
+        hwdb = NULL;
+
+        return 0;
+}
+
+_public_ sd_hwdb *sd_hwdb_ref(sd_hwdb *hwdb) {
+        assert_return(hwdb, NULL);
+
+        assert_se(REFCNT_INC(hwdb->n_ref) >= 2);
+
+        return hwdb;
+}
+
+_public_ sd_hwdb *sd_hwdb_unref(sd_hwdb *hwdb) {
+        if (hwdb && REFCNT_DEC(hwdb->n_ref) <= 0) {
+                if (hwdb->map)
+                        munmap((void *)hwdb->map, hwdb->st.st_size);
+                if (hwdb->f)
+                        fclose(hwdb->f);
+                free(hwdb->modalias);
+                ordered_hashmap_free(hwdb->properties);
+                free(hwdb);
+        }
+
+        return NULL;
+}
+
+bool hwdb_validate(sd_hwdb *hwdb) {
+        bool found = false;
+        const char* p;
+        struct stat st;
+
+        if (!hwdb)
+                return false;
+        if (!hwdb->f)
+                return false;
+
+        /* if hwdb.bin doesn't exist anywhere, we need to update */
+        NULSTR_FOREACH(p, hwdb_bin_paths) {
+                if (stat(p, &st) >= 0) {
+                        found = true;
+                        break;
+                }
+        }
+        if (!found)
+                return true;
+
+        if (timespec_load(&hwdb->st.st_mtim) != timespec_load(&st.st_mtim))
+                return true;
+        return false;
+}
+
+static int properties_prepare(sd_hwdb *hwdb, const char *modalias) {
+        _cleanup_free_ char *mod = NULL;
+        int r;
+
+        assert(hwdb);
+        assert(modalias);
+
+        if (streq_ptr(modalias, hwdb->modalias))
+                return 0;
+
+        mod = strdup(modalias);
+        if (!mod)
+                return -ENOMEM;
+
+        ordered_hashmap_clear(hwdb->properties);
+
+        hwdb->properties_modified = true;
+
+        r = trie_search_f(hwdb, modalias);
+        if (r < 0)
+                return r;
+
+        free(hwdb->modalias);
+        hwdb->modalias = mod;
+        mod = NULL;
+
+        return 0;
+}
+
+_public_ int sd_hwdb_get(sd_hwdb *hwdb, const char *modalias, const char *key, const char **_value) {
+        const char *value;
+        int r;
+
+        assert_return(hwdb, -EINVAL);
+        assert_return(hwdb->f, -EINVAL);
+        assert_return(modalias, -EINVAL);
+        assert_return(_value, -EINVAL);
+
+        r = properties_prepare(hwdb, modalias);
+        if (r < 0)
+                return r;
+
+        value = ordered_hashmap_get(hwdb->properties, key);
+        if (!value)
+                return -ENOENT;
+
+        *_value = value;
+
+        return 0;
+}
+
+_public_ int sd_hwdb_seek(sd_hwdb *hwdb, const char *modalias) {
+        int r;
+
+        assert_return(hwdb, -EINVAL);
+        assert_return(hwdb->f, -EINVAL);
+        assert_return(modalias, -EINVAL);
+
+        r = properties_prepare(hwdb, modalias);
+        if (r < 0)
+                return r;
+
+        hwdb->properties_modified = false;
+        hwdb->properties_iterator = ITERATOR_FIRST;
+
+        return 0;
+}
+
+_public_ int sd_hwdb_enumerate(sd_hwdb *hwdb, const char **key, const char **value) {
+        const void *k, *v;
+
+        assert_return(hwdb, -EINVAL);
+        assert_return(key, -EINVAL);
+        assert_return(value, -EINVAL);
+
+        if (hwdb->properties_modified)
+                return -EAGAIN;
+
+        v = ordered_hashmap_iterate(hwdb->properties, &hwdb->properties_iterator, &k);
+        if (!k)
+                return 0;
+
+        *key = k;
+        *value = v;
+
+        return 1;
+}
diff --git a/src/systemd/sd-hwdb.h b/src/systemd/sd-hwdb.h
new file mode 100644
index 0000000..fae9b9f
--- /dev/null
+++ b/src/systemd/sd-hwdb.h
@@ -0,0 +1,46 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foosdhwdbhfoo
+#define foosdhwdbhfoo
+
+/***
+  This file is part of systemd.
+
+  Copyright 2008-2012 Kay Sievers <kay at vrfy.org>
+  Copyright 2014 Tom Gundersen <teg at jklm.no>
+
+  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/>.
+***/
+
+#include "_sd-common.h"
+
+_SD_BEGIN_DECLARATIONS;
+
+typedef struct sd_hwdb sd_hwdb;
+
+sd_hwdb *sd_hwdb_ref(sd_hwdb *hwdb);
+sd_hwdb *sd_hwdb_unref(sd_hwdb *hwdb);
+
+int sd_hwdb_new(sd_hwdb **ret);
+
+int sd_hwdb_get(sd_hwdb *hwdb, const char *modalias, const char *key, const char **value);
+
+int sd_hwdb_seek(sd_hwdb *hwdb, const char *modalias);
+int sd_hwdb_enumerate(sd_hwdb *hwdb, const char **key, const char **value);
+
+#define FOREACH_HWDB_PROPERTY(hwdb, modalias, key, value)               \
+        if (sd_hwdb_seek(hwdb, modalias) >= 0)                          \
+                while (sd_hwdb_enumerate(hwdb, &(key), &(value)) > 0)
+
+#endif



More information about the systemd-commits mailing list