From 304c3d8086bc2a9230c5404f9c9fec72de08d229 Mon Sep 17 00:00:00 2001 From: Niklas Schnelle Date: Mon, 10 Feb 2020 14:23:11 +0100 Subject: [PATCH] zpcitctl: Exit on error in sysfs_report_error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also makes sure that we don't try to write to the /sys/bus/pci/device//recover attribute if reset failed. Signed-off-by: Niklas Schnelle Reviewed-by: Jan Hoeppner Signed-off-by: Jan Höppner --- zpcictl/zpcictl.c | 55 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/zpcictl/zpcictl.c b/zpcictl/zpcictl.c index ed531a96..cb02e332 100644 --- a/zpcictl/zpcictl.c +++ b/zpcictl/zpcictl.c @@ -97,6 +97,35 @@ static void fopen_err(char *path) exit(EXIT_FAILURE); } +static void fclose_err(char *path) +{ + if (errno == EIO || errno == EOPNOTSUPP) + warnx("Unsupported operation: %s: %s", path, strerror(errno)); + else + warnx("Could not close file: %s: %s", path, strerror(errno)); + free(path); + exit(EXIT_FAILURE); + +} + +static void fread_err(FILE *fp, char *path) +{ + warnx("Could not read file: %s: %s", path, strerror(errno)); + if (fclose(fp)) + fclose_err(path); + free(path); + exit(EXIT_FAILURE); +} + +static void fwrite_err(FILE *fp, char *path) +{ + warnx("Could not write to file: %s: %s", path, strerror(errno)); + if (fclose(fp)) + fclose_err(path); + free(path); + exit(EXIT_FAILURE); +} + #define READ_CHUNK_SIZE 512 static char *collect_smart_data(struct zpci_device *pdev) @@ -152,12 +181,10 @@ static unsigned int sysfs_read_value(struct zpci_device *pdev, const char *attr) if (!fp) fopen_err(path); if (fscanf(fp, "%x", &val) != 1) { - fclose(fp); - warnx("Could not read file %s: %s", path, strerror(errno)); - free(path); - exit(EXIT_FAILURE); + fread_err(fp, path); } - fclose(fp); + if (fclose(fp)) + fclose_err(path); free(path); return val; @@ -174,12 +201,10 @@ static void sysfs_write_value(struct zpci_device *pdev, const char *attr, if (!fp) fopen_err(path); if (fprintf(fp, "%x", val) < 0) { - fclose(fp); - warnx("Could not write to file %s: %s", path, strerror(errno)); - free(path); - exit(EXIT_FAILURE); + fwrite_err(fp, path); } - fclose(fp); + if (fclose(fp)) + fclose_err(path); free(path); } @@ -196,13 +221,9 @@ static void sysfs_report_error(struct zpci_report_error *report, char *slot) if (!fp) fopen_err(path); if (fwrite(report, 1, r_size, fp) != r_size) - warnx("Could not write to file: %s: %s", path, strerror(errno)); - if (fclose(fp)) { - if (errno == EIO || errno == EOPNOTSUPP) - warnx("Unsupported operation: %s: %s", path, strerror(errno)); - else - warnx("Could not close file: %s: %s", path, strerror(errno)); - } + fwrite_err(fp, path); + if (fclose(fp)) + fclose_err(path); free(path); }