From: Nicolas Boichat Date: Tue, 20 Mar 2018 05:36:58 +0000 (+0800) Subject: udevadm/hwdb: Return non-zero exit code on error when --strict is used X-Git-Tag: v239~372^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dd6f840e36a0beb6bb99e7a9e3037cfcb4b67876;p=thirdparty%2Fsystemd.git udevadm/hwdb: Return non-zero exit code on error when --strict is used - Add a new flag --strict to tell udevadm hwdb to return a non-zero code on error. - Make udevadm hwdb --update return an error when any parsing error occurs (only if strict flag is set). --- diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c index 1dd93d544eb..c946131fb75 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);