From 88aa1075962be705bf1597c0c22d2b615c73d3d8 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 24 Jan 2015 14:10:23 +0100 Subject: [PATCH] Use a more precise error detection when running external programs --- cmdline/unix.c | 22 ++++++++++++++++++++-- configure.ac | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmdline/unix.c b/cmdline/unix.c index 846935cb..f12bfdc5 100644 --- a/cmdline/unix.c +++ b/cmdline/unix.c @@ -696,9 +696,21 @@ static int devsmart(dev_t device, uint64_t* smart, char* serial) } ret = pclose(f); - if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) { + if (!WIFEXITED(ret)) { /* LCOV_EXCL_START */ - fprintf(stderr, "Failed to run smartctrl -a on device '%u:%u' (from pclose).\n", major(device), minor(device)); + fprintf(stderr, "Failed to run smartctrl -a on device '%u:%u' (not exited).\n", major(device), minor(device)); + return -1; + /* LCOV_EXCL_STOP */ + } + if (WEXITSTATUS(ret) == 127) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Failed to run smartctrl -a on device '%u:%u' (from sh).\n", major(device), minor(device)); + return -1; + /* LCOV_EXCL_STOP */ + } + if (WEXITSTATUS(ret) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Failed to run smartctrl -a on device '%u:%u' with return code %d.\n", major(device), minor(device), WEXITSTATUS(ret)); return -1; /* LCOV_EXCL_STOP */ } @@ -724,6 +736,12 @@ static int devdown(dev_t device) return -1; /* LCOV_EXCL_STOP */ } + if (!WIFEXITED(ret)) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Failed to run hdparm -y on device '%u:%u' (not exited).\n", major(device), minor(device)); + return -1; + /* LCOV_EXCL_STOP */ + } if (WEXITSTATUS(ret) == 127) { /* LCOV_EXCL_START */ fprintf(stderr, "Failed to run hdparm -y on device '%u:%u' (from sh).\n", major(device), minor(device)); diff --git a/configure.ac b/configure.ac index cfe8ea3d..521b2fc9 100644 --- a/configure.ac +++ b/configure.ac @@ -23,6 +23,7 @@ AC_HEADER_ASSERT AC_HEADER_STDC AC_HEADER_DIRENT AC_HEADER_TIME +AC_HEADER_SYS_WAIT AC_CHECK_HEADERS([fcntl.h stddef.h stdint.h stdlib.h string.h limits.h]) AC_CHECK_HEADERS([unistd.h getopt.h fnmatch.h io.h inttypes.h byteswap.h]) AC_CHECK_HEADERS([pthread.h math.h])