aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests
diff options
context:
space:
mode:
authorEugeniu Rosca <erosca@de.adit-jv.com>2017-07-01 14:57:26 +0200
committerShuah Khan <shuahkh@osg.samsung.com>2017-07-24 12:36:29 -0600
commit749fb263b304faf60adbbf34988624fa7f33db1b (patch)
tree3a78030d4d2f84776fc5793b48affd082189db28 /tools/testing/selftests
parentselftests: watchdog: fix mixed whitespace (diff)
downloadlinux-dev-749fb263b304faf60adbbf34988624fa7f33db1b.tar.xz
linux-dev-749fb263b304faf60adbbf34988624fa7f33db1b.zip
selftests: watchdog: use getopt_long()
Switch from manual argv[] parsing to getopt_long() argument processing. This creates more readable code and allows easier feature addition. This also fixes some segmentation faults introduced by commit 1dbdcc810928 ("selftests: watchdog: accept multiple params on command line"), when options -t or -p are not given the required value: ./watchdog-test -p 1 -t ./watchdog-test -t 1 -p No changes are intended in the way watchdog-test interacts with the kernel. The only noticible changes, tightly related to the addition of getopt (and done for easier maintenance), are: - help message has been reworked and migrated to a dedicated function. - all short/long options and the help message are sorted alphabetically. - all case statements inside the getopt loop are sorted alphabetically. Fixes: 1dbdcc810928 ("selftests: watchdog: accept multiple params on command line") Signed-off-by: Eugeniu Rosca <erosca@de.adit-jv.com> Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
Diffstat (limited to 'tools/testing/selftests')
-rw-r--r--tools/testing/selftests/watchdog/watchdog-test.c56
1 files changed, 39 insertions, 17 deletions
diff --git a/tools/testing/selftests/watchdog/watchdog-test.c b/tools/testing/selftests/watchdog/watchdog-test.c
index 082db2334ab2..e7c9d3bdae56 100644
--- a/tools/testing/selftests/watchdog/watchdog-test.c
+++ b/tools/testing/selftests/watchdog/watchdog-test.c
@@ -9,12 +9,22 @@
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
+#include <getopt.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/watchdog.h>
int fd;
const char v = 'V';
+static const char sopts[] = "dehp:t:";
+static const struct option lopts[] = {
+ {"disable", no_argument, NULL, 'd'},
+ {"enable", no_argument, NULL, 'e'},
+ {"help", no_argument, NULL, 'h'},
+ {"pingrate", required_argument, NULL, 'p'},
+ {"timeout", required_argument, NULL, 't'},
+ {NULL, no_argument, NULL, 0x0}
+};
/*
* This function simply sends an IOCTL to the driver, which in turn ticks
@@ -48,12 +58,25 @@ static void term(int sig)
exit(0);
}
+static void usage(char *progname)
+{
+ printf("Usage: %s [options]\n", progname);
+ printf(" -d, --disable Turn off the watchdog timer\n");
+ printf(" -e, --enable Turn on the watchdog timer\n");
+ printf(" -h, --help Print the help message\n");
+ printf(" -p, --pingrate=P Set ping rate to P seconds\n");
+ printf(" -t, --timeout=T Set timeout to T seconds\n");
+ printf("\n");
+ printf("Parameters are parsed left-to-right in real-time.\n");
+ printf("Example: %s -d -t 10 -p 5 -e\n", progname);
+}
+
int main(int argc, char *argv[])
{
int flags;
unsigned int ping_rate = 1;
int ret;
- int i;
+ int c;
setbuf(stdout, NULL);
@@ -64,33 +87,32 @@ int main(int argc, char *argv[])
exit(-1);
}
- for (i = 1; i < argc; i++) {
- if (!strncasecmp(argv[i], "-d", 2)) {
+ while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
+ switch (c) {
+ case 'd':
flags = WDIOS_DISABLECARD;
ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
if (!ret)
printf("Watchdog card disabled.\n");
- } else if (!strncasecmp(argv[i], "-e", 2)) {
+ break;
+ case 'e':
flags = WDIOS_ENABLECARD;
ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
if (!ret)
printf("Watchdog card enabled.\n");
- } else if (!strncasecmp(argv[i], "-t", 2) && argv[2]) {
- flags = atoi(argv[i + 1]);
+ break;
+ case 'p':
+ ping_rate = strtoul(optarg, NULL, 0);
+ printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
+ break;
+ case 't':
+ flags = atoi(optarg);
ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
if (!ret)
printf("Watchdog timeout set to %u seconds.\n", flags);
- i++;
- } else if (!strncasecmp(argv[i], "-p", 2) && argv[2]) {
- ping_rate = strtoul(argv[i + 1], NULL, 0);
- printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
- i++;
- } else {
- printf("-d to disable, -e to enable, -t <n> to set "
- "the timeout,\n-p <n> to set the ping rate, and ");
- printf("run by itself to tick the card.\n");
- printf("Parameters are parsed left-to-right in real-time.\n");
- printf("Example: %s -d -t 10 -p 5 -e\n", argv[0]);
+ break;
+ default:
+ usage(argv[0]);
goto end;
}
}