From: Richard W.M. Jones Date: Tue, 4 Aug 2015 06:09:49 +0000 (+0100) Subject: Allow setting the timeout on the command line. X-Git-Url: http://git.annexia.org/?p=watchdog-test-framework.git;a=commitdiff_plain;h=0ba645064afe0f616414c251554efaa29e6f60f1 Allow setting the timeout on the command line. watchdog-test --timeout 60 Also use --yes to avoid all prompts: watchdog-test --timeout 60 --yes --- diff --git a/watchdog-test.c b/watchdog-test.c index d4e6305..f184659 100644 --- a/watchdog-test.c +++ b/watchdog-test.c @@ -1,5 +1,5 @@ /* watchdog-test framework - * Copyright (C) 2014 Red Hat Inc. + * Copyright (C) 2014-2015 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,21 +20,70 @@ #include #include #include +#include +#include #include #include #include #include #define WATCHDOG_DEVICE "/dev/watchdog" -#define WATCHDOG_TIMEOUT 30 +#define WATCHDOG_TIMEOUT_DEFAULT 30 + +enum { HELP_OPTION = CHAR_MAX + 1 }; + +static const char *options = ""; +static const struct option long_options[] = { + { "timeout", 1, 0, 0 }, + { "yes", 0, 0, 0 }, + { 0, 0, 0, 0 } +}; + +static void +usage (int r) +{ + printf ("usage: watchdog-test [--timeout=TIMEOUT] [--yes]\n"); + exit (r); +} int -main () +main (int argc, char *argv[]) { + int watchdog_timeout = WATCHDOG_TIMEOUT_DEFAULT; int fd; int timeout; char input[256]; time_t start_t, t; + int option_index, c, yes = 0; + + /* Parse the command line. */ + for (;;) { + c = getopt_long (argc, argv, options, long_options, &option_index); + if (c == -1) break; + + switch (c) { + case 0: /* options which are long only */ + if (strcmp (long_options[option_index].name, "timeout") == 0) { + if (sscanf (optarg, "%d", &watchdog_timeout) != 1) { + fprintf (stderr, "%s: invalid --timeout option\n", argv[0]); + exit (EXIT_FAILURE); + } + } else if (strcmp (long_options[option_index].name, "yes") == 0) { + yes = 1; + } else { + fprintf (stderr, "%s: unknown long option: %s (%d)\n", + argv[0], long_options[option_index].name, option_index); + exit (EXIT_FAILURE); + } + break; + + case HELP_OPTION: + usage (EXIT_SUCCESS); + + default: + usage (EXIT_FAILURE); + } + } setvbuf (stdout, NULL, _IONBF, 0); @@ -45,26 +94,30 @@ main () printf ("\n"); printf ("The test is as follows:\n"); printf ("(1) I will set up the watchdog with a %d second timeout.\n", - WATCHDOG_TIMEOUT); + watchdog_timeout); printf ("(2) I will ping the watchdog for %d seconds. During this time\n" " the guest should run normally.\n", - WATCHDOG_TIMEOUT * 2); + watchdog_timeout * 2); printf ("(3) I will stop pinging the watchdog and just count up. If the\n" " virtual watchdog device is set correctly, then the watchdog\n" " action (eg. reboot) should happen around the %d second mark.\n", - WATCHDOG_TIMEOUT); + watchdog_timeout); printf ("\n"); - printf ("Do you want to start the test? Type \"yes\" without quotes:\n"); - if (fgets (input, sizeof input, stdin) == NULL || - strncmp (input, "yes", 3) != 0) { - printf ("Exiting the program.\n"); - exit (EXIT_SUCCESS); + if (!yes) { + printf ("Do you want to start the test? Type \"yes\" without quotes:\n"); + + if (fgets (input, sizeof input, stdin) == NULL || + strncmp (input, "yes", 3) != 0) { + printf ("Exiting the program.\n"); + exit (EXIT_SUCCESS); + } + + printf ("\n"); } - printf ("\n"); printf ("Setting up the watchdog (%s) with a %d second timeout.\n", - WATCHDOG_DEVICE, WATCHDOG_TIMEOUT); + WATCHDOG_DEVICE, watchdog_timeout); sync (); fd = open (WATCHDOG_DEVICE, O_WRONLY); @@ -73,7 +126,7 @@ main () exit (EXIT_FAILURE); } - timeout = WATCHDOG_TIMEOUT; + timeout = watchdog_timeout; if (ioctl (fd, WDIOC_SETTIMEOUT, &timeout) == -1) { perror ("ioctl: WDIOC_SETTIMEOUT: error setting timeout"); exit (EXIT_FAILURE); @@ -83,18 +136,18 @@ main () perror ("ioctl: WDIOC_GETTIMEOUT"); else { printf ("Timeout is set to %d seconds.\n", timeout); - if (timeout != WATCHDOG_TIMEOUT) + if (timeout != watchdog_timeout) printf ("Note: some watchdog devices don't support setting exact timeout values.\n"); } printf ("\n"); - printf ("Pinging the watchdog for %d seconds ...\n", WATCHDOG_TIMEOUT*2); + printf ("Pinging the watchdog for %d seconds ...\n", watchdog_timeout*2); printf ("\n"); time (&start_t); for (;;) { time (&t); - if (t - start_t > WATCHDOG_TIMEOUT * 2) + if (t - start_t > watchdog_timeout * 2) break; printf ("%d... ", (int) (t - start_t)); @@ -111,7 +164,7 @@ main () printf ("\n"); printf ("Stopping pings.\n"); printf ("The watchdog action should happen at approximately %d second mark.\n", - WATCHDOG_TIMEOUT); + watchdog_timeout); printf ("\n"); time (&start_t);