2022-10-21 22:26:11

by Shuah Khan

[permalink] [raw]
Subject: [PATCH 2/4] selftests/watchdog: add support for WDIOC_GETSTATUS

Add support for calling WDIOC_GETSTATUS and printing supported
features and status.

Signed-off-by: Shuah Khan <[email protected]>
---
.../selftests/watchdog/watchdog-test.c | 44 +++++++++++++++++--
1 file changed, 41 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/watchdog/watchdog-test.c b/tools/testing/selftests/watchdog/watchdog-test.c
index 5db9ad8b543c..e6af6e327eb3 100644
--- a/tools/testing/selftests/watchdog/watchdog-test.c
+++ b/tools/testing/selftests/watchdog/watchdog-test.c
@@ -6,8 +6,6 @@
* - Could be tested against softdog driver on systems that
* don't have watchdog hadrware.
* - TODO:
-* - WDIOC_GETSTATUS is called when WDIOC_GETBOOTSTATUS is called.
-* - Enhance the logic to call WDIOC_GETSTATUS and test return values.
* - Enhance coverage of ioctl return values - flags and status.
* - Enhance test to add coverage for WDIOC_GETTEMP.
*
@@ -30,13 +28,14 @@

int fd;
const char v = 'V';
-static const char sopts[] = "bdehp:t:Tn:NLf:i";
+static const char sopts[] = "bdehp:st:Tn:NLf:i";
static const struct option lopts[] = {
{"bootstatus", no_argument, NULL, 'b'},
{"disable", no_argument, NULL, 'd'},
{"enable", no_argument, NULL, 'e'},
{"help", no_argument, NULL, 'h'},
{"pingrate", required_argument, NULL, 'p'},
+ {"status", no_argument, NULL, 's'},
{"timeout", required_argument, NULL, 't'},
{"gettimeout", no_argument, NULL, 'T'},
{"pretimeout", required_argument, NULL, 'n'},
@@ -85,6 +84,7 @@ static void usage(char *progname)
printf(" -f, --file\t\tOpen watchdog device file\n");
printf("\t\t\tDefault is /dev/watchdog\n");
printf(" -i, --info\t\tShow watchdog_info\n");
+ printf(" -s, --status\t\tGet status & supported features\n");
printf(" -b, --bootstatus\tGet last boot status (Watchdog/POR)\n");
printf(" -d, --disable\t\tTurn off the watchdog timer\n");
printf(" -e, --enable\t\tTurn on the watchdog timer\n");
@@ -107,6 +107,35 @@ struct wdiof_status {
const char *status_str;
};

+#define WDIOF_NUM_STATUS 8
+
+static const struct wdiof_status wdiof_status[WDIOF_NUM_STATUS] = {
+ {WDIOF_SETTIMEOUT, "Set timeout (in seconds)"},
+ {WDIOF_MAGICCLOSE, "Supports magic close char"},
+ {WDIOF_PRETIMEOUT, "Pretimeout (in seconds), get/set"},
+ {WDIOF_ALARMONLY, "Watchdog triggers a management or other external alarm not a reboot"},
+ {WDIOF_KEEPALIVEPING, "Keep alive ping reply"},
+ {WDIOS_DISABLECARD, "Turn off the watchdog timer"},
+ {WDIOS_ENABLECARD, "Turn on the watchdog timer"},
+ {WDIOS_TEMPPANIC, "Kernel panic on temperature trip"},
+};
+
+static void print_status(int flags)
+{
+ int wdiof = 0;
+
+ if (flags == WDIOS_UNKNOWN) {
+ printf("Unknown status error from WDIOC_GETSTATUS\n");
+ return;
+ }
+
+ for (wdiof = 0; wdiof < WDIOF_NUM_STATUS; wdiof++) {
+ if (flags & wdiof_status[wdiof].flag)
+ printf("Support/Status: %s\n",
+ wdiof_status[wdiof].status_str);
+ }
+}
+
#define WDIOF_NUM_BOOTSTATUS 7

static const struct wdiof_status wdiof_bootstatus[WDIOF_NUM_BOOTSTATUS] = {
@@ -219,6 +248,15 @@ int main(int argc, char *argv[])
ping_rate = DEFAULT_PING_RATE;
printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
break;
+ case 's':
+ flags = 0;
+ oneshot = 1;
+ ret = ioctl(fd, WDIOC_GETSTATUS, &flags);
+ if (!ret)
+ print_status(flags);
+ else
+ printf("WDIOC_GETSTATUS error '%s'\n", strerror(errno));
+ break;
case 't':
flags = strtoul(optarg, NULL, 0);
ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
--
2.34.1