From db3c417481b648579b2a4642557fa25c6a7b5349 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 11 Apr 2015 07:43:12 +0200 Subject: [PATCH] Log all the smartctl output --- cmdline/mingw.c | 48 ++++++++++++++++++++++++++++++++++------------- cmdline/support.c | 40 ++++++++++++++++++++++++++++++++++++++- cmdline/support.h | 16 +++++++++++++++- cmdline/unix.c | 34 +++++++++++++++++++++------------ 4 files changed, 111 insertions(+), 27 deletions(-) diff --git a/cmdline/mingw.c b/cmdline/mingw.c index 65576c90..9a7214e1 100644 --- a/cmdline/mingw.c +++ b/cmdline/mingw.c @@ -1968,6 +1968,11 @@ static int smartctl_scan(FILE* f, tommy_list* list) if (s == 0) break; + /* remove extraneous chars */ + s = polish(buf); + + msg_tag("smartctl:scan::text: %s\n", s); + if (*s == '/') { char* sep = strchr(s, ' '); if (sep) { @@ -2024,7 +2029,7 @@ static int devscan(tommy_list* list) snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" --scan-open -d pd", exedir); - msg_tag("smartctl:run:scan: %s\n", u16tou8(cmd)); + msg_tag("smartctl:scan::run: %s\n", u16tou8(cmd)); f = _wpopen(cmd, L"rt"); if (!f) { @@ -2042,6 +2047,9 @@ static int devscan(tommy_list* list) } ret = pclose(f); + + msg_tag("smartctl:scan::ret: %x\n", ret); + if (ret == -1) { /* LCOV_EXCL_START */ msg_error("Failed to run '%s' (from pclose).\n", u16tou8(cmd)); @@ -2055,8 +2063,6 @@ static int devscan(tommy_list* list) /* LCOV_EXCL_STOP */ } - msg_tag("smartctl:ret:scan: %x\n", ret); - return 0; } @@ -2085,7 +2091,7 @@ static int devsmart(uint64_t device, const char* name, const char* custom, uint6 count = 0; retry: - msg_tag("smartctl:run:%s: %s\n", name, u16tou8(cmd)); + msg_tag("smartctl:%s:%s:run: %s\n", file, name, u16tou8(cmd)); f = _wpopen(cmd, L"rt"); if (!f) { @@ -2095,7 +2101,7 @@ retry: /* LCOV_EXCL_STOP */ } - if (smartctl_attribute(f, smart, serial) != 0) { + if (smartctl_attribute(f, file, name, smart, serial) != 0) { /* LCOV_EXCL_START */ pclose(f); return -1; @@ -2104,7 +2110,7 @@ retry: ret = pclose(f); - msg_tag("smartctl:ret:%s: %x\n", name, ret); + msg_tag("smartctl:%s:%s:ret: %x\n", file, name, ret); if (ret == -1) { /* LCOV_EXCL_START */ @@ -2152,6 +2158,7 @@ static int devdown(uint64_t device, const char* name, const char* custom) { WCHAR cmd[MAX_PATH + 128]; char file[128]; + FILE* f; int ret; int count; @@ -2161,23 +2168,38 @@ static int devdown(uint64_t device, const char* name, const char* custom) if (custom[0]) { char option[128]; snprintf(option, sizeof(option), custom, file); - snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -s standby,now %s >nul 2>nul", exedir, option); + snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -s standby,now %s", exedir, option); } else { - snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -s standby,now %s >nul 2>nul", exedir, file); + snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -s standby,now %s", exedir, file); } count = 0; retry: - msg_tag("smartctl:run:%s: %s\n", name, u16tou8(cmd)); + msg_tag("smartctl:%s:%s:run: %s\n", file, name, u16tou8(cmd)); - ret = _wsystem(cmd); + f = _wpopen(cmd, L"rt"); + if (!f) { + /* LCOV_EXCL_START */ + msg_error("Failed to run '%s' (from popen).\n", u16tou8(cmd)); + return -1; + /* LCOV_EXCL_STOP */ + } - msg_tag("smartctl:ret:%s: %x\n", name, ret); + if (smartctl_flush(f, file, name) != 0) { + /* LCOV_EXCL_START */ + pclose(f); + return -1; + /* LCOV_EXCL_STOP */ + } + + ret = pclose(f); + + msg_tag("smartctl:%s:%s:ret: %x\n", file, name, ret); if (ret == -1) { /* LCOV_EXCL_START */ - msg_error("Failed to run '%s' (from system).\n", u16tou8(cmd)); + msg_error("Failed to run '%s' (from pclose).\n", u16tou8(cmd)); return -1; /* LCOV_EXCL_STOP */ } @@ -2194,7 +2216,7 @@ retry: */ if (ret == 2) { /* retry using the "sat" type */ - snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -s standby,now -d sat %s >nul 2>nul", exedir, file); + snwprintf(cmd, sizeof(cmd), L"\"%lssmartctl.exe\" -s standby,now -d sat %s", exedir, file); ++count; goto retry; diff --git a/cmdline/support.c b/cmdline/support.c index b2e0daf7..aa42a1f0 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -304,6 +304,19 @@ bail: /* LCOV_EXCL_STOP */ } +char* polish(char* s) +{ + char* i = s; + + while (*i) { + if (isspace(*i) || !isprint(*i)) + *i = ' '; + ++i; + } + + return s; +} + /****************************************************************************/ /* path */ @@ -704,7 +717,7 @@ static int smatch(const char* str, const char* pattern) return 0; } -int smartctl_attribute(FILE* f, uint64_t* smart, char* serial) +int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, char* serial) { unsigned i; int inside; @@ -727,6 +740,11 @@ int smartctl_attribute(FILE* f, uint64_t* smart, char* serial) if (s == 0) break; + /* remove extraneous chars */ + s = polish(buf); + + msg_tag("smartctl:text:%s:%s: %s\n", file, name, s); + /* skip initial spaces */ while (isspace(*s)) ++s; @@ -773,3 +791,23 @@ int smartctl_attribute(FILE* f, uint64_t* smart, char* serial) return 0; } +int smartctl_flush(FILE* f, const char* file, const char* name) +{ + /* read the file */ + while (1) { + char buf[256]; + char* s; + + s = fgets(buf, sizeof(buf), f); + if (s == 0) + break; + + /* remove extraneous chars */ + s = polish(buf); + + msg_tag("smartctl:text:%s:%s: %s\n", file, name, s); + } + + return 0; +} + diff --git a/cmdline/support.h b/cmdline/support.h index c50c096e..67a7d683 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -139,6 +139,15 @@ typedef void fptr(const char* format, ...); */ const char* esc(const char* str); +/** + * Polish a string. + * + * Not printable chars are replaced by spaces. + * + * Note that the passed string is modified. + */ +char* polish(char* s); + /****************************************************************************/ /* path */ @@ -249,7 +258,12 @@ void malloc_fail(size_t size); * Reads smartctl attributes from a stream. * Returns 0 on success. */ -int smartctl_attribute(FILE* f, uint64_t* smart, char* serial); +int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, char* serial); + +/** + * Flush smartctl output from a stream. + */ +int smartctl_flush(FILE* f, const char* file, const char* name); #endif diff --git a/cmdline/unix.c b/cmdline/unix.c index 4d8a1419..71144387 100644 --- a/cmdline/unix.c +++ b/cmdline/unix.c @@ -639,7 +639,7 @@ static int devsmart(dev_t device, const char* name, const char* custom, uint64_t snprintf(cmd, sizeof(cmd), "smartctl -a %s", file); } - msg_tag("smartctl:run:%s: %s\n", name, cmd); + msg_tag("smartctl:%s:%s:run: %s\n", file, name, cmd); f = popen(cmd, "r"); if (!f) { @@ -649,7 +649,7 @@ static int devsmart(dev_t device, const char* name, const char* custom, uint64_t /* LCOV_EXCL_STOP */ } - if (smartctl_attribute(f, smart, serial) != 0) { + if (smartctl_attribute(f, file, name, smart, serial) != 0) { /* LCOV_EXCL_START */ pclose(f); return -1; @@ -658,7 +658,7 @@ static int devsmart(dev_t device, const char* name, const char* custom, uint64_t ret = pclose(f); - msg_tag("smartctl:ret:%s %x\n", name, ret); + msg_tag("smartctl:%s:%s:ret: %x\n", file, name, ret); if (!WIFEXITED(ret)) { /* LCOV_EXCL_START */ @@ -687,6 +687,7 @@ static int devdown(dev_t device, const char* name, const char* custom) { char cmd[128]; char file[128]; + FILE* f; int ret; snprintf(file, sizeof(file), "/dev/block/%u:%u", major(device), minor(device)); @@ -695,23 +696,32 @@ static int devdown(dev_t device, const char* name, const char* custom) if (custom[0]) { char option[128]; snprintf(option, sizeof(option), custom, file); - snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s >/dev/null 2>/dev/null", option); + snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", option); } else { - snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s >/dev/null 2>/dev/null", file); + snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", file); } - msg_tag("smartctl:run:%s: %s\n", name, cmd); + msg_tag("smartctl:%s:%s:run: %s\n", file, name, cmd); - ret = system(cmd); - - msg_tag("smartctl:ret:%s: %x\n", name, ret); - - if (ret == -1) { + f = popen(cmd, "r"); + if (!f) { /* LCOV_EXCL_START */ - msg_error("Failed to run '%s' (from system).\n", cmd); + msg_error("Failed to run '%s' (from popen).\n", cmd); return -1; /* LCOV_EXCL_STOP */ } + + if (smartctl_flush(f, file, name) != 0) { + /* LCOV_EXCL_START */ + pclose(f); + return -1; + /* LCOV_EXCL_STOP */ + } + + ret = pclose(f); + + msg_tag("smartctl:%s:%s:ret: %x\n", file, name, ret); + if (!WIFEXITED(ret)) { /* LCOV_EXCL_START */ msg_error("Failed to run '%s' (not exited).\n", cmd);