]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/hwdb/hwdb.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / hwdb / hwdb.c
index 41f74ad133eb7cfcb811193ec5c12d635825d39c..4540260f9b869f4e195184810e0c50c75cb9e810 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -31,6 +32,7 @@
 #include "hwdb-util.h"
 #include "label.h"
 #include "mkdir.h"
+#include "path-util.h"
 #include "selinux-util.h"
 #include "strbuf.h"
 #include "string-util.h"
@@ -39,7 +41,7 @@
 #include "verbs.h"
 
 /*
- * Generic udev properties, key/value database based on modalias strings.
+ * Generic udev properties, key-value database based on modalias strings.
  * Uses a Patricia/radix trie to index all matches for efficient lookup.
  */
 
@@ -70,7 +72,7 @@ struct trie_node {
         struct trie_child_entry *children;
         uint8_t children_count;
 
-        /* sorted array of key/value pairs */
+        /* sorted array of key-value pairs */
         struct trie_value_entry *values;
         size_t values_count;
 };
@@ -81,7 +83,7 @@ struct trie_child_entry {
         struct trie_node *child;
 };
 
-/* value array item with key/value pairs */
+/* value array item with key-value pairs */
 struct trie_value_entry {
         size_t key_off;
         size_t value_off;
@@ -214,7 +216,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                        const char *key, const char *value,
                        const char *filename, uint16_t file_priority, uint32_t line_number) {
         size_t i = 0;
-        int err = 0;
+        int r = 0;
 
         for (;;) {
                 size_t p;
@@ -255,9 +257,9 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                         node->children_count = 0;
                         node->values = NULL;
                         node->values_count = 0;
-                        err = node_add_child(trie, node, new_child, c);
-                        if (err < 0)
-                                return err;
+                        r = node_add_child(trie, node, new_child, c);
+                        if (r < 0)
+                                return r;
 
                         new_child = NULL; /* avoid cleanup */
                         break;
@@ -284,10 +286,10 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                         }
 
                         child->prefix_off = off;
-                        err = node_add_child(trie, node, child, c);
-                        if (err < 0) {
+                        r = node_add_child(trie, node, child, c);
+                        if (r < 0) {
                                 free(child);
-                                return err;
+                                return r;
                         }
 
                         return trie_node_add_value(trie, child, key, value, filename, file_priority, line_number);
@@ -329,11 +331,11 @@ static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) {
                 .children_count = node->children_count,
                 .values_count = htole64(node->values_count),
         };
-        struct trie_child_entry_f *children = NULL;
+        _cleanup_free_ struct trie_child_entry_f *children = NULL;
         int64_t node_off;
 
         if (node->children_count) {
-                children = new0(struct trie_child_entry_f, node->children_count);
+                children = new(struct trie_child_entry_f, node->children_count);
                 if (!children)
                         return -ENOMEM;
         }
@@ -343,12 +345,13 @@ static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) {
                 int64_t child_off;
 
                 child_off = trie_store_nodes(trie, node->children[i].child);
-                if (child_off < 0) {
-                        free(children);
+                if (child_off < 0)
                         return child_off;
-                }
-                children[i].c = node->children[i].c;
-                children[i].child_off = htole64(child_off);
+
+                children[i] = (struct trie_child_entry_f) {
+                        .c = node->children[i].c,
+                        .child_off = htole64(child_off),
+                };
         }
 
         /* write node */
@@ -360,7 +363,6 @@ static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) {
         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 */
@@ -374,8 +376,8 @@ static int64_t trie_store_nodes(struct trie_f *trie, struct trie_node *node) {
                 };
 
                 fwrite(&v, sizeof(struct trie_value_entry2_f), 1, trie->f);
-                trie->values_count++;
         }
+        trie->values_count += node->values_count;
 
         return node_off;
 }
@@ -390,30 +392,27 @@ static int trie_store(struct trie *trie, const char *filename) {
         int64_t size;
         struct trie_header_f h = {
                 .signature = HWDB_SIG,
-                .tool_version = htole64(atoi(VERSION)),
+                .tool_version = htole64(atoi(PACKAGE_VERSION)),
                 .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_entry2_f)),
         };
-        int err;
+        int r;
 
         /* calculate size of header, nodes, children entries, value entries */
         t.strings_off = sizeof(struct trie_header_f);
         trie_store_nodes_size(&t, trie->root);
 
-        err = fopen_temporary(filename , &t.f, &filename_tmp);
-        if (err < 0)
-                return err;
+        r = fopen_temporary(filename, &t.f, &filename_tmp);
+        if (r < 0)
+                return r;
         fchmod(fileno(t.f), 0444);
 
         /* write nodes */
-        err = fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET);
-        if (err < 0) {
-                fclose(t.f);
-                unlink_noerrno(filename_tmp);
-                return -errno;
-        }
+        if (fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET) < 0)
+                goto error_fclose;
+
         root_off = trie_store_nodes(&t, trie->root);
         h.nodes_root_off = htole64(root_off);
         pos = ftello(t.f);
@@ -426,21 +425,21 @@ static int trie_store(struct trie *trie, const char *filename) {
         /* write header */
         size = ftello(t.f);
         h.file_size = htole64(size);
-        err = fseeko(t.f, 0, SEEK_SET);
-        if (err < 0) {
-                fclose(t.f);
-                unlink_noerrno(filename_tmp);
-                return -errno;
-        }
+        if (fseeko(t.f, 0, SEEK_SET) < 0)
+                goto error_fclose;
         fwrite(&h, sizeof(struct trie_header_f), 1, t.f);
-        err = ferror(t.f);
-        if (err)
-                err = -errno;
+
+        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 */
         fclose(t.f);
-        if (err < 0 || rename(filename_tmp, filename) < 0) {
-                unlink_noerrno(filename_tmp);
-                return err < 0 ? err : -errno;
-        }
 
         log_debug("=== trie on-disk ===");
         log_debug("size:             %8"PRIi64" bytes", size);
@@ -453,31 +452,38 @@ static int trie_store(struct trie *trie, const char *filename) {
                   t.values_count * sizeof(struct trie_value_entry2_f), t.values_count);
         log_debug("string store:     %8zu bytes", trie->strings->len);
         log_debug("strings start:    %8"PRIu64, t.strings_off);
-
         return 0;
+
+ error_fclose:
+        r = -errno;
+        fclose(t.f);
+        unlink(filename_tmp);
+        return r;
 }
 
 static int insert_data(struct trie *trie, char **match_list, char *line,
                        const char *filename, uint16_t file_priority, uint32_t line_number) {
         char *value, **entry;
 
+        assert(line[0] == ' ');
+
         value = strchr(line, '=');
-        if (!value) {
-                log_error("Error, key/value pair expected but got '%s' in '%s':", line, filename);
-                return -EINVAL;
-        }
+        if (!value)
+                return log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
+                                  "Key-value pair expected but got \"%s\", ignoring", line);
 
         value[0] = '\0';
         value++;
 
-        /* libudev requires properties to start with a space */
+        /* Replace multiple leading spaces by a single space */
         while (isblank(line[0]) && isblank(line[1]))
                 line++;
 
-        if (line[0] == '\0' || value[0] == '\0') {
-                log_error("Error, empty key or value '%s' in '%s':", line, filename);
-                return -EINVAL;
-        }
+        if (isempty(line + 1) || isempty(value))
+                return log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
+                                  "Empty %s in \"%s=%s\", ignoring",
+                                  isempty(line + 1) ? "key" : "value",
+                                  line, value);
 
         STRV_FOREACH(entry, match_list)
                 trie_insert(trie, trie->root, *entry, line, value, filename, file_priority, line_number);
@@ -529,7 +535,8 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                                 break;
 
                         if (line[0] == ' ') {
-                                log_error("Error, MATCH expected but got '%s' in '%s':", line, filename);
+                                log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
+                                           "Match expected but got indented property \"%s\", ignoring line", line);
                                 break;
                         }
 
@@ -548,14 +555,16 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
 
                 case HW_MATCH:
                         if (len == 0) {
-                                log_error("Error, DATA expected but got empty line in '%s':", filename);
+                                log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
+                                           "Property expected, ignoring record with no properties");
+
                                 state = HW_NONE;
                                 strv_clear(match_list);
                                 break;
                         }
 
-                        /* another match */
                         if (line[0] != ' ') {
+                                /* another match */
                                 match = strdup(line);
                                 if (!match)
                                         return -ENOMEM;
@@ -573,15 +582,16 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                         break;
 
                 case HW_DATA:
-                        /* end of record */
                         if (len == 0) {
+                                /* end of record */
                                 state = HW_NONE;
                                 strv_clear(match_list);
                                 break;
                         }
 
                         if (line[0] != ' ') {
-                                log_error("Error, DATA expected but got '%s' in '%s':", line, filename);
+                                log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
+                                           "Property or empty line expected, got \"%s\", ignoring record", line);
                                 state = HW_NONE;
                                 strv_clear(match_list);
                                 break;
@@ -592,6 +602,10 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                 };
         }
 
+        if (state == HW_MATCH)
+                log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
+                           "Property expected, ignoring record with no properties");
+
         return 0;
 }
 
@@ -619,7 +633,8 @@ static int hwdb_query(int argc, char *argv[], void *userdata) {
 static int hwdb_update(int argc, char *argv[], void *userdata) {
         _cleanup_free_ char *hwdb_bin = NULL;
         _cleanup_(trie_freep) struct trie *trie = NULL;
-        char **files, **f;
+        _cleanup_strv_free_ char **files = NULL;
+        char **f;
         uint16_t file_priority = 1;
         int r;
 
@@ -639,15 +654,14 @@ static int hwdb_update(int argc, char *argv[], void *userdata) {
 
         trie->nodes_count++;
 
-        r = conf_files_list_strv(&files, ".hwdb", arg_root, conf_file_dirs);
+        r = conf_files_list_strv(&files, ".hwdb", arg_root, 0, conf_file_dirs);
         if (r < 0)
-                return log_error_errno(r, "failed to enumerate hwdb files: %m");
+                return log_error_errno(r, "Failed to enumerate hwdb files: %m");
 
         STRV_FOREACH(f, files) {
-                log_debug("reading file '%s'", *f);
+                log_debug("Reading file \"%s\"", *f);
                 import_file(trie, *f, file_priority++);
         }
-        strv_free(files);
 
         strbuf_complete(trie->strings);
 
@@ -665,7 +679,7 @@ static int hwdb_update(int argc, char *argv[], void *userdata) {
         log_debug("strings dedup'ed: %8zu bytes (%8zu)",
                   trie->strings->dedup_len, trie->strings->dedup_count);
 
-        hwdb_bin = strjoin(arg_root, "/", arg_hwdb_bin_dir, "/hwdb.bin");
+        hwdb_bin = path_join(arg_root, arg_hwdb_bin_dir, "hwdb.bin");
         if (!hwdb_bin)
                 return -ENOMEM;