1 /* watchdog-test framework
2 * Copyright (C) 2014-2015 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/ioctl.h>
28 #include <linux/watchdog.h>
30 #define WATCHDOG_DEVICE "/dev/watchdog"
31 #define WATCHDOG_TIMEOUT_DEFAULT 30
33 enum { HELP_OPTION = CHAR_MAX + 1 };
35 static const char *options = "";
36 static const struct option long_options[] = {
37 { "timeout", 1, 0, 0 },
45 printf ("usage: watchdog-test [--timeout=TIMEOUT] [--yes]\n");
50 main (int argc, char *argv[])
52 int watchdog_timeout = WATCHDOG_TIMEOUT_DEFAULT;
57 int option_index, c, yes = 0;
59 /* Parse the command line. */
61 c = getopt_long (argc, argv, options, long_options, &option_index);
65 case 0: /* options which are long only */
66 if (strcmp (long_options[option_index].name, "timeout") == 0) {
67 if (sscanf (optarg, "%d", &watchdog_timeout) != 1) {
68 fprintf (stderr, "%s: invalid --timeout option\n", argv[0]);
71 } else if (strcmp (long_options[option_index].name, "yes") == 0) {
74 fprintf (stderr, "%s: unknown long option: %s (%d)\n",
75 argv[0], long_options[option_index].name, option_index);
88 setvbuf (stdout, NULL, _IONBF, 0);
91 printf ("Welcome to the watchdog test framework.\n");
92 printf ("You should read the README file and run this in the guest.\n");
93 printf ("DO NOT RUN IT IN THE HOST!\n");
95 printf ("The test is as follows:\n");
96 printf ("(1) I will set up the watchdog with a %d second timeout.\n",
98 printf ("(2) I will ping the watchdog for %d seconds. During this time\n"
99 " the guest should run normally.\n",
100 watchdog_timeout * 2);
101 printf ("(3) I will stop pinging the watchdog and just count up. If the\n"
102 " virtual watchdog device is set correctly, then the watchdog\n"
103 " action (eg. reboot) should happen around the %d second mark.\n",
108 printf ("Do you want to start the test? Type \"yes\" without quotes:\n");
110 if (fgets (input, sizeof input, stdin) == NULL ||
111 strncmp (input, "yes", 3) != 0) {
112 printf ("Exiting the program.\n");
119 printf ("Setting up the watchdog (%s) with a %d second timeout.\n",
120 WATCHDOG_DEVICE, watchdog_timeout);
123 fd = open (WATCHDOG_DEVICE, O_WRONLY);
125 perror (WATCHDOG_DEVICE);
129 timeout = watchdog_timeout;
130 if (ioctl (fd, WDIOC_SETTIMEOUT, &timeout) == -1) {
131 perror ("ioctl: WDIOC_SETTIMEOUT: error setting timeout");
135 if (ioctl (fd, WDIOC_GETTIMEOUT, &timeout) == -1)
136 perror ("ioctl: WDIOC_GETTIMEOUT");
138 printf ("Timeout is set to %d seconds.\n", timeout);
139 if (timeout != watchdog_timeout)
140 printf ("Note: some watchdog devices don't support setting exact timeout values.\n");
144 printf ("Pinging the watchdog for %d seconds ...\n", watchdog_timeout*2);
150 if (t - start_t > watchdog_timeout * 2)
152 printf ("%d...\n", (int) (t - start_t));
159 if (ioctl (fd, WDIOC_KEEPALIVE, 0) == -1)
160 perror ("\nioctl: WDIOC_KEEPALIVE");
165 printf ("Stopping pings.\n");
166 printf ("The watchdog action should happen at approximately %d second mark.\n",
173 printf ("%d...\n", (int) (t - start_t));