chiark / gitweb /
udev: hwdb - do not look at "usb_device" parents
[elogind.git] / src / udev / udev-builtin-hwdb.c
index c1a6f5c791b52d3e8096cb13daacb995c42b2e87..695a31a12ff31acca95f4b49dc8c22b72b3aa006 100644 (file)
@@ -1,8 +1,7 @@
 /***
   This file is part of systemd.
 
-  Copyright 2012 Kay Sievers <kay.sievers@vrfy.org>
-  Copyright 2008 Alan Jenkins <alan.christopher.jenkins@googlemail.com>
+  Copyright 2012 Kay Sievers <kay@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
 #include <stdlib.h>
 #include <fnmatch.h>
 #include <getopt.h>
-#include <sys/mman.h>
 
 #include "udev.h"
-#include "udev-hwdb.h"
 
-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 struct udev_hwdb *hwdb;
 
-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);
-}
-
-struct trie_f {
-        struct udev_device *dev;
-        bool test;
-        FILE *f;
-        uint64_t file_time_usec;
-        union {
-                struct trie_header_f *head;
-                const char *map;
-        };
-        size_t map_size;
-};
-
-static const struct trie_child_entry_f *trie_node_children(struct trie_f *trie, const struct trie_node_f *node) {
-        return (const struct trie_child_entry_f *)((const char *)node + le64toh(trie->head->node_size));
-}
+int udev_builtin_hwdb_lookup(struct udev_device *dev,
+                             const char *prefix, const char *modalias,
+                             const char *filter, bool test) {
+        struct udev_list_entry *list;
+        struct udev_list_entry *entry;
+        int n = 0;
 
-static const struct trie_value_entry_f *trie_node_values(struct trie_f *trie, const struct trie_node_f *node) {
-        const char *base = (const char *)node;
-
-        base += le64toh(trie->head->node_size);
-        base += node->children_count * le64toh(trie->head->child_entry_size);
-        return (const struct trie_value_entry_f *)base;
-}
+        if (!hwdb)
+                return -ENOENT;
 
-static const struct trie_node_f *trie_node_from_off(struct trie_f *trie, le64_t off) {
-        return (const struct trie_node_f *)(trie->map + le64toh(off));
-}
+        if (prefix) {
+                _cleanup_free_ const char *lookup;
 
-static const char *trie_string(struct trie_f *trie, le64_t off) {
-        return trie->map + le64toh(off);
-}
+                lookup = strjoin(prefix, modalias, NULL);
+                if (!lookup)
+                        return -ENOMEM;
+                list = udev_hwdb_get_properties_list_entry(hwdb, lookup, 0);
+        } else
+                list = udev_hwdb_get_properties_list_entry(hwdb, modalias, 0);
 
-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;
+        udev_list_entry_foreach(entry, list) {
+                if (filter && fnmatch(filter, udev_list_entry_get_name(entry), FNM_NOESCAPE) != 0)
+                        continue;
 
-        return n1->c - n2->c;
+                if (udev_builtin_add_property(dev, test,
+                                              udev_list_entry_get_name(entry),
+                                              udev_list_entry_get_value(entry)) < 0)
+                        return -ENOMEM;
+                n++;
+        }
+        return n;
 }
 
-static const struct trie_node_f *node_lookup_f(struct trie_f *trie, const struct trie_node_f *node, uint8_t c) {
-        struct trie_child_entry_f *child;
-        struct trie_child_entry_f search;
+static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) {
+        const char *v, *p;
+        int vn, pn;
 
-        search.c = c;
-        child = bsearch(&search, trie_node_children(trie, node), node->children_count,
-                        le64toh(trie->head->child_entry_size), trie_children_cmp_f);
-        if (child)
-                return trie_node_from_off(trie, child->child_off);
-        return NULL;
+        v = udev_device_get_sysattr_value(dev, "idVendor");
+        if (!v)
+                return NULL;
+        p = udev_device_get_sysattr_value(dev, "idProduct");
+        if (!p)
+                return NULL;
+        vn = strtol(v, NULL, 16);
+        if (vn <= 0)
+                return NULL;
+        pn = strtol(p, NULL, 16);
+        if (pn <= 0)
+                return NULL;
+        snprintf(s, size, "usb:v%04Xp%04X*", vn, pn);
+        return s;
 }
 
-static void trie_fnmatch_f(struct trie_f *trie, const struct trie_node_f *node, size_t p,
-                           struct linebuf *buf, const char *search,
-                           void (*cb)(struct trie_f *trie, const char *key, const char *value)) {
-        size_t len;
-        size_t i;
-        const char *prefix;
-
-        prefix = trie_string(trie, 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(trie, node)[i];
-
-                linebuf_add_char(buf, child->c);
-                trie_fnmatch_f(trie, trie_node_from_off(trie, child->child_off), 0, buf, search, cb);
-                linebuf_rem_char(buf);
-        }
-
-        if (node->values_count && fnmatch(linebuf_get(buf), search, 0) == 0)
-                for (i = 0; i < node->values_count; i++)
-                        cb(trie, trie_string(trie, trie_node_values(trie, node)[i].key_off),
-                           trie_string(trie, trie_node_values(trie, node)[i].value_off));
-
-        linebuf_rem(buf, len);
-}
+static int udev_builtin_hwdb_search(struct udev_device *dev, struct udev_device *srcdev,
+                                    const char *subsystem, const char *prefix,
+                                    const char *filter, bool test) {
+        struct udev_device *d;
+        char s[16];
+        bool last = false;
+        int r = 0;
 
-static void trie_search_f(struct trie_f *trie, const char *search,
-                          void (*cb)(struct trie_f *trie, const char *key, const char *value)) {
-        struct linebuf buf;
-        const struct trie_node_f *node;
-        size_t i = 0;
-
-        linebuf_init(&buf);
-
-        node = trie_node_from_off(trie, trie->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(trie, node->prefix_off)[p]); p++) {
-                                if (c == '*' || c == '?' || c == '[') {
-                                        trie_fnmatch_f(trie, node, p, &buf, search + i + p, cb);
-                                        return;
-                                }
-                                if (c != search[i + p])
-                                        return;
-                        }
-                        i += p;
-                }
+        for (d = srcdev; d && !last; d = udev_device_get_parent(d)) {
+                const char *dsubsys;
+                const char *modalias = NULL;
 
-                child = node_lookup_f(trie, node, '*');
-                if (child) {
-                        linebuf_add_char(&buf, '*');
-                        trie_fnmatch_f(trie, child, 0, &buf, search + i, cb);
-                        linebuf_rem_char(&buf);
-                }
+                dsubsys = udev_device_get_subsystem(d);
+                if (!dsubsys)
+                        continue;
 
-                child = node_lookup_f(trie, node, '?');
-                if (child) {
-                        linebuf_add_char(&buf, '?');
-                        trie_fnmatch_f(trie, child, 0, &buf, search + i, cb);
-                        linebuf_rem_char(&buf);
-                }
+                /* look only at devices of a specific subsystem */
+                if (subsystem && !streq(dsubsys, subsystem))
+                        continue;
 
-                child = node_lookup_f(trie, node, '[');
-                if (child) {
-                        linebuf_add_char(&buf, '[');
-                        trie_fnmatch_f(trie, child, 0, &buf, search + i, cb);
-                        linebuf_rem_char(&buf);
-                }
+                modalias = udev_device_get_property_value(d, "MODALIAS");
 
-                if (search[i] == '\0') {
-                        size_t n;
+                if (streq(dsubsys, "usb") && streq_ptr(udev_device_get_devtype(d), "usb_device")) {
+                        /* if the usb_device does not have a modalias, compose one */
+                        if (!modalias)
+                                modalias = modalias_usb(d, s, sizeof(s));
 
-                        for (n = 0; n < node->values_count; n++)
-                                cb(trie, trie_string(trie, trie_node_values(trie, node)[n].key_off),
-                                   trie_string(trie, trie_node_values(trie, node)[n].value_off));
-                        return;
+                        /* avoid looking at any parent device, they are usually just a USB hub */
+                        last = true;
                 }
 
-                child = node_lookup_f(trie, node, search[i]);
-                node = child;
-                i++;
-        }
-}
-
-static void value_cb(struct trie_f *trie, const char *key, const char *value) {
-        /* TODO: add sub-matches (+) against DMI data */
-        if (key[0] == ' ')
-                udev_builtin_add_property(trie->dev, trie->test, key + 1, value);
-}
-
-static struct trie_f trie;
-
-static int hwdb_lookup(struct udev_device *dev, const char *subsys) {
-        struct udev_device *d;
-        const char *modalias;
-        char str[UTIL_NAME_SIZE];
-        int rc = EXIT_SUCCESS;
-
-        /* search the first parent device with a modalias */
-        for (d = dev; d; d = udev_device_get_parent(d)) {
-                const char *dsubsys = udev_device_get_subsystem(d);
-
-                /* look only at devices of a specific subsystem */
-                if (subsys && dsubsys && !streq(dsubsys, subsys))
+                if (!modalias)
                         continue;
 
-                modalias = udev_device_get_property_value(d, "MODALIAS");
-                if (modalias)
-                        break;
-
-                /* the usb_device does not have modalias, compose one */
-                if (dsubsys && streq(dsubsys, "usb")) {
-                        const char *v, *p;
-                        int vn, pn;
-
-                        v = udev_device_get_sysattr_value(d, "idVendor");
-                        if (!v)
-                                continue;
-                        p = udev_device_get_sysattr_value(d, "idProduct");
-                        if (!p)
-                                continue;
-                        vn = strtol(v, NULL, 16);
-                        if (vn <= 0)
-                                continue;
-                        pn = strtol(p, NULL, 16);
-                        if (pn <= 0)
-                                continue;
-                        snprintf(str, sizeof(str), "usb:v%04Xp%04X*", vn, pn);
-                        modalias = str;
+                r = udev_builtin_hwdb_lookup(dev, prefix, modalias, filter, test);
+                if (r > 0)
                         break;
-                }
         }
-        if (!modalias)
-                return EXIT_FAILURE;
 
-        trie_search_f(&trie, modalias, value_cb);
-        return rc;
+        return r;
 }
 
 static int builtin_hwdb(struct udev_device *dev, int argc, char *argv[], bool test) {
         static const struct option options[] = {
+                { "filter", required_argument, NULL, 'f' },
+                { "device", required_argument, NULL, 'd' },
                 { "subsystem", required_argument, NULL, 's' },
+                { "lookup-prefix", required_argument, NULL, 'p' },
                 {}
         };
-        const char *subsys = NULL;
+        const char *filter = NULL;
+        const char *device = NULL;
+        const char *subsystem = NULL;
+        const char *prefix = NULL;
+        struct udev_device *srcdev;
 
-        if (!trie.f)
-                return EXIT_SUCCESS;
+        if (!hwdb)
+                return EXIT_FAILURE;
 
         for (;;) {
                 int option;
 
-                option = getopt_long(argc, argv, "s", options, NULL);
+                option = getopt_long(argc, argv, "f:d:s:p:", options, NULL);
                 if (option == -1)
                         break;
 
                 switch (option) {
+                case 'f':
+                        filter = optarg;
+                        break;
+
+                case 'd':
+                        device = optarg;
+                        break;
+
                 case 's':
-                        subsys = optarg;
+                        subsystem = optarg;
+                        break;
+
+                case 'p':
+                        prefix = optarg;
                         break;
                 }
         }
 
-        trie.dev = dev;
-        trie.test = test;
-        if (hwdb_lookup(dev, subsys) < 0)
+        /* query a specific key given as argument */
+        if (argv[optind]) {
+                if (udev_builtin_hwdb_lookup(dev, prefix, argv[optind], filter, test) > 0)
+                        return EXIT_SUCCESS;
                 return EXIT_FAILURE;
-        return EXIT_SUCCESS;
+        }
+
+        /* read data from another device than the device we will store the data */
+        if (device) {
+                srcdev = udev_device_new_from_device_id(udev_device_get_udev(dev), device);
+                if (!srcdev)
+                        return EXIT_FAILURE;
+        } else
+                srcdev = dev;
+
+        if (udev_builtin_hwdb_search(dev, srcdev, subsystem, prefix, filter, test) > 0)
+                return EXIT_SUCCESS;
+        return EXIT_FAILURE;
 }
 
 /* called at udev startup and reload */
-static int builtin_hwdb_init(struct udev *udev)
-{
-        struct stat st;
-        const char sig[] = HWDB_SIG;
-
-        if (trie.f)
+static int builtin_hwdb_init(struct udev *udev) {
+        if (hwdb)
                 return 0;
-
-        trie.f = fopen(SYSCONFDIR "/udev/hwdb.bin", "re");
-        if (!trie.f)
-                return -errno;
-
-        if (fstat(fileno(trie.f), &st) < 0 || (size_t)st.st_size < offsetof(struct trie_header_f, strings_len) + 8) {
-                log_error("Error reading '%s'.", SYSCONFDIR "/udev/hwdb.bin: %m");
-                fclose(trie.f);
-                zero(trie);
-                return -EINVAL;
-        }
-
-        trie.map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fileno(trie.f), 0);
-        if (trie.map == MAP_FAILED) {
-                log_error("Error mapping '%s'.", SYSCONFDIR "/udev/hwdb.bin: %m");
-                fclose(trie.f);
-                return -EINVAL;
-        }
-        trie.file_time_usec = ts_usec(&st.st_mtim);
-        trie.map_size = st.st_size;
-
-        if (memcmp(trie.map, sig, sizeof(trie.head->signature)) != 0 || (size_t)st.st_size != le64toh(trie.head->file_size)) {
-                log_error("Unable to recognize the format of '%s'.", SYSCONFDIR "/udev/hwdb.bin");
-                log_error("Please try 'udevadm hwdb --update' to re-create it.");
-                munmap((void *)trie.map, st.st_size);
-                fclose(trie.f);
-                zero(trie);
-                return EINVAL;
-        }
-
-        log_debug("=== trie on-disk ===\n");
-        log_debug("tool version:          %llu", (unsigned long long)le64toh(trie.head->tool_version));
-        log_debug("file size:        %8zi bytes\n", st.st_size);
-        log_debug("header size       %8zu bytes\n", (size_t)le64toh(trie.head->header_size));
-        log_debug("strings           %8zu bytes\n", (size_t)le64toh(trie.head->strings_len));
-        log_debug("nodes             %8zu bytes\n", (size_t)le64toh(trie.head->nodes_len));
+        hwdb = udev_hwdb_new(udev);
+        if (!hwdb)
+                return -ENOMEM;
         return 0;
 }
 
 /* called on udev shutdown and reload request */
-static void builtin_hwdb_exit(struct udev *udev)
-{
-        if (!trie.f)
-                return;
-        munmap((void *)trie.map, trie.map_size);
-        fclose(trie.f);
-        zero(trie);
+static void builtin_hwdb_exit(struct udev *udev) {
+        hwdb = udev_hwdb_unref(hwdb);
 }
 
 /* called every couple of seconds during event activity; 'true' if config has changed */
-static bool builtin_hwdb_validate(struct udev *udev)
-{
-        struct stat st;
-
-        if (!trie.f)
-                return false;
-        if (fstat(fileno(trie.f), &st) < 0)
-                return true;
-        if (trie.file_time_usec != ts_usec(&st.st_mtim))
-                return true;
-        return false;
+static bool builtin_hwdb_validate(struct udev *udev) {
+        return udev_hwdb_validate(hwdb);
 }
 
 const struct udev_builtin udev_builtin_hwdb = {