From: Eugeniu Rosca Date: Sat, 1 Jul 2017 12:57:26 +0000 (+0200) Subject: selftests: watchdog: use getopt_long() X-Git-Tag: v4.14-rc1~89^2~38 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=749fb263b304faf60adbbf34988624fa7f33db1b;p=platform%2Fkernel%2Flinux-exynos.git 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 Signed-off-by: Shuah Khan --- diff --git a/tools/testing/selftests/watchdog/watchdog-test.c b/tools/testing/selftests/watchdog/watchdog-test.c index 082db23..e7c9d3b 100644 --- a/tools/testing/selftests/watchdog/watchdog-test.c +++ b/tools/testing/selftests/watchdog/watchdog-test.c @@ -9,12 +9,22 @@ #include #include #include +#include #include #include #include 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 to set " - "the timeout,\n-p 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; } }