Allow setting the timeout on the command line.
authorRichard W.M. Jones <rjones@redhat.com>
Tue, 4 Aug 2015 06:09:49 +0000 (07:09 +0100)
committerRichard W.M. Jones <rjones@redhat.com>
Tue, 4 Aug 2015 06:28:10 +0000 (07:28 +0100)
  watchdog-test --timeout 60

Also use --yes to avoid all prompts:

  watchdog-test --timeout 60 --yes

watchdog-test.c

index d4e6305..f184659 100644 (file)
@@ -1,5 +1,5 @@
 /* watchdog-test framework
 /* 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
  *
  * 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
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <getopt.h>
+#include <limits.h>
 #include <time.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <linux/watchdog.h>
 
 #define WATCHDOG_DEVICE "/dev/watchdog"
 #include <time.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <linux/watchdog.h>
 
 #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
 
 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 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);
 
 
   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",
   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",
   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",
   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 ("\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",
   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);
 
   sync ();
   fd = open (WATCHDOG_DEVICE, O_WRONLY);
@@ -73,7 +126,7 @@ main ()
     exit (EXIT_FAILURE);
   }
 
     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);
   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);
     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 ("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);
   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));
 
       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",
   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);
   printf ("\n");
 
   time (&start_t);