diff --git a/man/systemd-hwdb.xml b/man/systemd-hwdb.xml
index 2b58388cff..cd1df4a76e 100644
--- a/man/systemd-hwdb.xml
+++ b/man/systemd-hwdb.xml
@@ -70,6 +70,13 @@
Alternate root path in the filesystem.
+
+
+
+
+ When updating, return non-zero exit value on any parsing error.
+
+
diff --git a/src/hwdb/hwdb.c b/src/hwdb/hwdb.c
index 67f693aae9..99fcd1069a 100644
--- a/src/hwdb/hwdb.c
+++ b/src/hwdb/hwdb.c
@@ -34,6 +34,7 @@
static const char *arg_hwdb_bin_dir = "/etc/udev";
static const char *arg_root = "";
+static bool arg_strict;
static const char * const conf_file_dirs[] = {
"/etc/udev/hwdb.d",
@@ -489,7 +490,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
_cleanup_strv_free_ char **match_list = NULL;
uint32_t line_number = 0;
char *match = NULL;
- int r;
+ int r = 0, err;
f = fopen(filename, "re");
if (!f)
@@ -524,6 +525,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
if (line[0] == ' ') {
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
"Match expected but got indented property \"%s\", ignoring line", line);
+ r = -EINVAL;
break;
}
@@ -534,9 +536,9 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
if (!match)
return -ENOMEM;
- r = strv_consume(&match_list, match);
- if (r < 0)
- return r;
+ err = strv_consume(&match_list, match);
+ if (err < 0)
+ return err;
break;
@@ -544,7 +546,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
if (len == 0) {
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
"Property expected, ignoring record with no properties");
-
+ r = -EINVAL;
state = HW_NONE;
strv_clear(match_list);
break;
@@ -556,16 +558,18 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
if (!match)
return -ENOMEM;
- r = strv_consume(&match_list, match);
- if (r < 0)
- return r;
+ err = strv_consume(&match_list, match);
+ if (err < 0)
+ return err;
break;
}
/* first data */
state = HW_DATA;
- insert_data(trie, match_list, line, filename, file_priority, line_number);
+ err = insert_data(trie, match_list, line, filename, file_priority, line_number);
+ if (err < 0)
+ r = err;
break;
case HW_DATA:
@@ -579,12 +583,15 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
if (line[0] != ' ') {
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
"Property or empty line expected, got \"%s\", ignoring record", line);
+ r = -EINVAL;
state = HW_NONE;
strv_clear(match_list);
break;
}
- insert_data(trie, match_list, line, filename, file_priority, line_number);
+ err = insert_data(trie, match_list, line, filename, file_priority, line_number);
+ if (err < 0)
+ r = err;
break;
};
}
@@ -593,7 +600,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
log_syntax(NULL, LOG_WARNING, filename, line_number, EINVAL,
"Property expected, ignoring record with no properties");
- return 0;
+ return r;
}
static int hwdb_query(int argc, char *argv[], void *userdata) {
@@ -623,7 +630,7 @@ static int hwdb_update(int argc, char *argv[], void *userdata) {
_cleanup_strv_free_ char **files = NULL;
char **f;
uint16_t file_priority = 1;
- int r;
+ int r = 0, err;
trie = new0(struct trie, 1);
if (!trie)
@@ -641,13 +648,15 @@ static int hwdb_update(int argc, char *argv[], void *userdata) {
trie->nodes_count++;
- 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");
+ err = conf_files_list_strv(&files, ".hwdb", arg_root, 0, conf_file_dirs);
+ if (err < 0)
+ return log_error_errno(err, "Failed to enumerate hwdb files: %m");
STRV_FOREACH(f, files) {
log_debug("Reading file \"%s\"", *f);
- import_file(trie, *f, file_priority++);
+ err = import_file(trie, *f, file_priority++);
+ if (err < 0 && arg_strict)
+ r = err;
}
strbuf_complete(trie->strings);
@@ -671,11 +680,15 @@ static int hwdb_update(int argc, char *argv[], void *userdata) {
return -ENOMEM;
mkdir_parents_label(hwdb_bin, 0755);
- r = trie_store(trie, hwdb_bin);
- if (r < 0)
- return log_error_errno(r, "Failure writing database %s: %m", hwdb_bin);
+ err = trie_store(trie, hwdb_bin);
+ if (err < 0)
+ return log_error_errno(err, "Failure writing database %s: %m", hwdb_bin);
- return label_fix(hwdb_bin, 0);
+ err = label_fix(hwdb_bin, 0);
+ if (err < 0)
+ return err;
+
+ return r;
}
static void help(void) {
@@ -683,6 +696,8 @@ static void help(void) {
"Update or query the hardware database.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
+ " -s --strict When updating, return non-zero exit value on any parsing\n"
+ " error\n"
" --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
" -r --root=PATH Alternative root path in the filesystem\n\n"
"Commands:\n"
@@ -701,6 +716,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "usr", no_argument, NULL, ARG_USR },
+ { "strict", no_argument, NULL, 's' },
{ "root", required_argument, NULL, 'r' },
{}
};
@@ -710,7 +726,7 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 0);
assert(argv);
- while ((c = getopt_long(argc, argv, "ut:r:h", options, NULL)) >= 0) {
+ while ((c = getopt_long(argc, argv, "ust:r:h", options, NULL)) >= 0) {
switch(c) {
case 'h':
@@ -724,6 +740,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_hwdb_bin_dir = UDEVLIBEXECDIR;
break;
+ case 's':
+ arg_strict = true;
+ break;
+
case 'r':
arg_root = optarg;
break;
diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c
index 1dd93d544e..c946131fb7 100644
--- a/src/udev/udevadm-hwdb.c
+++ b/src/udev/udevadm-hwdb.c
@@ -450,6 +450,7 @@ static int import_file(struct udev *udev, struct trie *trie, const char *filenam
FILE *f;
char line[LINE_MAX];
struct udev_list match_list;
+ int r = 0, err;
udev_list_init(udev, &match_list, false);
@@ -483,6 +484,7 @@ static int import_file(struct udev *udev, struct trie *trie, const char *filenam
if (line[0] == ' ') {
log_error("Error, MATCH expected but got '%s' in '%s':", line, filename);
+ r = -EINVAL;
break;
}
@@ -494,6 +496,7 @@ static int import_file(struct udev *udev, struct trie *trie, const char *filenam
case HW_MATCH:
if (len == 0) {
log_error("Error, DATA expected but got empty line in '%s':", filename);
+ r = -EINVAL;
state = HW_NONE;
udev_list_cleanup(&match_list);
break;
@@ -507,7 +510,9 @@ static int import_file(struct udev *udev, struct trie *trie, const char *filenam
/* first data */
state = HW_DATA;
- insert_data(trie, &match_list, line, filename);
+ err = insert_data(trie, &match_list, line, filename);
+ if (err < 0)
+ r = err;
break;
case HW_DATA:
@@ -520,19 +525,22 @@ static int import_file(struct udev *udev, struct trie *trie, const char *filenam
if (line[0] != ' ') {
log_error("Error, DATA expected but got '%s' in '%s':", line, filename);
+ r = -EINVAL;
state = HW_NONE;
udev_list_cleanup(&match_list);
break;
}
- insert_data(trie, &match_list, line, filename);
+ err = insert_data(trie, &match_list, line, filename);
+ if (err < 0)
+ r = err;
break;
};
}
fclose(f);
udev_list_cleanup(&match_list);
- return 0;
+ return r;
}
static void help(void) {
@@ -540,6 +548,7 @@ static void help(void) {
" -h --help Print this message\n"
" -V --version Print version of the program\n"
" -u --update Update the hardware database\n"
+ " -s --strict When updating, return non-zero exit value on any parsing error\n"
" --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
" -t --test=MODALIAS Query database and print result\n"
" -r --root=PATH Alternative root path in the filesystem\n\n"
@@ -557,6 +566,7 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
static const struct option options[] = {
{ "update", no_argument, NULL, 'u' },
{ "usr", no_argument, NULL, ARG_USR },
+ { "strict", no_argument, NULL, 's' },
{ "test", required_argument, NULL, 't' },
{ "root", required_argument, NULL, 'r' },
{ "version", no_argument, NULL, 'V' },
@@ -570,8 +580,9 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
struct trie *trie = NULL;
int err, c;
int rc = EXIT_SUCCESS;
+ bool strict = false;
- while ((c = getopt_long(argc, argv, "ut:r:Vh", options, NULL)) >= 0)
+ while ((c = getopt_long(argc, argv, "ust:r:Vh", options, NULL)) >= 0)
switch(c) {
case 'u':
update = true;
@@ -579,6 +590,9 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
case ARG_USR:
hwdb_bin_dir = UDEVLIBEXECDIR;
break;
+ case 's':
+ strict = true;
+ break;
case 't':
test = optarg;
break;
@@ -635,7 +649,8 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
}
STRV_FOREACH(f, files) {
log_debug("reading file '%s'", *f);
- import_file(udev, trie, *f);
+ if (import_file(udev, trie, *f) < 0 && strict)
+ rc = EXIT_FAILURE;
}
strv_free(files);