X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibudev%2Flibudev-hwdb.c;h=1d0d407208f17d21708feab09ff240da6555ac04;hp=12c8214ec50ebebdc2274149a27c7de2472a4633;hb=ff49bc3212cb07d850dcfd59940539773a0be26f;hpb=ee623f0d0c04a9c8da4953ddc29862c8c4945458 diff --git a/src/libudev/libudev-hwdb.c b/src/libudev/libudev-hwdb.c index 12c8214ec..1d0d40720 100644 --- a/src/libudev/libudev-hwdb.c +++ b/src/libudev/libudev-hwdb.c @@ -140,9 +140,13 @@ static const struct trie_node_f *node_lookup_f(struct udev_hwdb *hwdb, const str } static int hwdb_add_property(struct udev_hwdb *hwdb, const char *key, const char *value) { - /* TODO: add sub-matches (+) against DMI data */ + /* + * 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; @@ -169,8 +173,8 @@ static int trie_fnmatch_f(struct udev_hwdb *hwdb, const struct trie_node_f *node linebuf_rem_char(buf); } - if (node->values_count && fnmatch(linebuf_get(buf), search, 0) == 0) - for (i = 0; i < node->values_count; i++) { + 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) @@ -236,7 +240,7 @@ static int trie_search_f(struct udev_hwdb *hwdb, const char *search) { if (search[i] == '\0') { size_t n; - for (n = 0; n < node->values_count; 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) @@ -252,6 +256,11 @@ static int trie_search_f(struct udev_hwdb *hwdb, const char *search) { return 0; } +static const char hwdb_bin_paths[] = + "/etc/udev/hwdb.bin\0" + UDEVLIBEXECDIR "/hwdb.bin\0"; + + /** * udev_hwdb_new: * @udev: udev library context @@ -262,6 +271,7 @@ static int trie_search_f(struct udev_hwdb *hwdb, const char *search) { **/ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) { struct udev_hwdb *hwdb; + const char *hwdb_bin_path; const char sig[] = HWDB_SIG; hwdb = new0(struct udev_hwdb, 1); @@ -271,40 +281,53 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) { hwdb->refcount = 1; udev_list_init(udev, &hwdb->properties_list, true); - hwdb->f = fopen(HWDB_BIN, "re"); + /* 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("error reading %s: %m", hwdb_bin_path); + udev_hwdb_unref(hwdb); + return NULL; + } + } + if (!hwdb->f) { - log_debug("error reading %s: %m", HWDB_BIN); + log_debug("hwdb.bin does not exist, please run udevadm hwdb --update"); udev_hwdb_unref(hwdb); 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("error reading %s: %m", HWDB_BIN); + log_debug("error reading %s: %m", hwdb_bin_path); udev_hwdb_unref(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("error mapping %s: %m", HWDB_BIN); + log_debug("error mapping %s: %m", hwdb_bin_path); udev_hwdb_unref(hwdb); return 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); + log_debug("error recognizing the format of %s", hwdb_bin_path); udev_hwdb_unref(hwdb); return NULL; } - log_debug("=== trie on-disk ===\n"); - log_debug("tool version: %llu", (unsigned long long)le64toh(hwdb->head->tool_version)); - log_debug("file size: %8llu bytes\n", (unsigned long long)hwdb->st.st_size); - log_debug("header size %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->header_size)); - log_debug("strings %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->strings_len)); - log_debug("nodes %8llu bytes\n", (unsigned long long)le64toh(hwdb->head->nodes_len)); + 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; } @@ -330,14 +353,14 @@ _public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) { * Drop a reference of a hwdb context. If the refcount reaches zero, * all resources of the hwdb context will be released. * - * Returns: the passed hwdb context if it has still an active reference, or #NULL otherwise. + * Returns: #NULL **/ _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) { if (!hwdb) return NULL; hwdb->refcount--; if (hwdb->refcount > 0) - return hwdb; + return NULL; if (hwdb->map) munmap((void *)hwdb->map, hwdb->st.st_size); if (hwdb->f) @@ -348,14 +371,25 @@ _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) { } 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 (fstat(fileno(hwdb->f), &st) < 0) + + /* 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;