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 #define MIN(a,b) ((a)<(b)?(a):(b))
35 enum { HELP_OPTION = CHAR_MAX + 1 };
37 static const char *options = "";
38 static const struct option long_options[] = {
39 { "timeout", 1, 0, 0 },
47 printf ("usage: watchdog-test [--timeout=TIMEOUT] [--yes]\n");
52 main (int argc, char *argv[])
54 int watchdog_timeout = WATCHDOG_TIMEOUT_DEFAULT;
60 int option_index, c, yes = 0;
62 /* Parse the command line. */
64 c = getopt_long (argc, argv, options, long_options, &option_index);
68 case 0: /* options which are long only */
69 if (strcmp (long_options[option_index].name, "timeout") == 0) {
70 if (sscanf (optarg, "%d", &watchdog_timeout) != 1) {
71 fprintf (stderr, "%s: invalid --timeout option\n", argv[0]);
74 } else if (strcmp (long_options[option_index].name, "yes") == 0) {
77 fprintf (stderr, "%s: unknown long option: %s (%d)\n",
78 argv[0], long_options[option_index].name, option_index);
91 ping_time = MIN (watchdog_timeout * 2, 120);
93 setvbuf (stdout, NULL, _IONBF, 0);
96 printf ("Welcome to the watchdog test framework.\n");
97 printf ("You should read the README file and run this in the guest.\n");
98 printf ("DO NOT RUN IT IN THE HOST!\n");
100 printf ("The test is as follows:\n");
101 printf ("(1) I will set up the watchdog with a %d second timeout.\n",
103 printf ("(2) I will ping the watchdog for %d seconds. During this time\n"
104 " the guest should run normally.\n",
106 printf ("(3) I will stop pinging the watchdog and just count up. If the\n"
107 " virtual watchdog device is set correctly, then the watchdog\n"
108 " action (eg. reboot) should happen around the %d second mark.\n",
113 printf ("Do you want to start the test? Type \"yes\" without quotes:\n");
115 if (fgets (input, sizeof input, stdin) == NULL ||
116 strncmp (input, "yes", 3) != 0) {
117 printf ("Exiting the program.\n");
124 printf ("Setting up the watchdog (%s) with a %d second timeout.\n",
125 WATCHDOG_DEVICE, watchdog_timeout);
128 fd = open (WATCHDOG_DEVICE, O_WRONLY);
130 perror (WATCHDOG_DEVICE);
134 timeout = watchdog_timeout;
135 if (ioctl (fd, WDIOC_SETTIMEOUT, &timeout) == -1) {
136 perror ("ioctl: WDIOC_SETTIMEOUT: error setting timeout");
140 if (ioctl (fd, WDIOC_GETTIMEOUT, &timeout) == -1)
141 perror ("ioctl: WDIOC_GETTIMEOUT");
143 printf ("Timeout is set to %d seconds.\n", timeout);
144 if (timeout != watchdog_timeout)
145 printf ("Note: some watchdog devices don't support setting exact timeout values.\n");
149 printf ("Pinging the watchdog for %d seconds ...\n", ping_time);
155 if (t - start_t > ping_time)
157 printf ("%d...\n", (int) (t - start_t));
164 if (ioctl (fd, WDIOC_KEEPALIVE, 0) == -1)
165 perror ("\nioctl: WDIOC_KEEPALIVE");
170 printf ("Stopping pings.\n");
171 printf ("The watchdog action should happen at approximately %d second mark.\n",
178 printf ("%d...\n", (int) (t - start_t));