Allow setting the timeout on the command line.
[watchdog-test-framework.git] / watchdog-test.c
1 /* watchdog-test framework
2  * Copyright (C) 2014-2015 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <limits.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <linux/watchdog.h>
29
30 #define WATCHDOG_DEVICE "/dev/watchdog"
31 #define WATCHDOG_TIMEOUT_DEFAULT 30
32
33 enum { HELP_OPTION = CHAR_MAX + 1 };
34
35 static const char *options = "";
36 static const struct option long_options[] = {
37   { "timeout", 1, 0, 0 },
38   { "yes", 0, 0, 0 },
39   { 0, 0, 0, 0 }
40 };
41
42 static void
43 usage (int r)
44 {
45   printf ("usage: watchdog-test [--timeout=TIMEOUT] [--yes]\n");
46   exit (r);
47 }
48
49 int
50 main (int argc, char *argv[])
51 {
52   int watchdog_timeout = WATCHDOG_TIMEOUT_DEFAULT;
53   int fd;
54   int timeout;
55   char input[256];
56   time_t start_t, t;
57   int option_index, c, yes = 0;
58
59   /* Parse the command line. */
60   for (;;) {
61     c = getopt_long (argc, argv, options, long_options, &option_index);
62     if (c == -1) break;
63
64     switch (c) {
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]);
69           exit (EXIT_FAILURE);
70         }
71       } else if (strcmp (long_options[option_index].name, "yes") == 0) {
72         yes = 1;
73       } else {
74         fprintf (stderr, "%s: unknown long option: %s (%d)\n",
75                  argv[0], long_options[option_index].name, option_index);
76         exit (EXIT_FAILURE);
77       }
78       break;
79
80     case HELP_OPTION:
81       usage (EXIT_SUCCESS);
82
83     default:
84       usage (EXIT_FAILURE);
85     }
86   }
87
88   setvbuf (stdout, NULL, _IONBF, 0);
89
90   printf ("\n");
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");
94   printf ("\n");
95   printf ("The test is as follows:\n");
96   printf ("(1) I will set up the watchdog with a %d second timeout.\n",
97           watchdog_timeout);
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",
104           watchdog_timeout);
105   printf ("\n");
106
107   if (!yes) {
108     printf ("Do you want to start the test?  Type \"yes\" without quotes:\n");
109
110     if (fgets (input, sizeof input, stdin) == NULL ||
111         strncmp (input, "yes", 3) != 0) {
112       printf ("Exiting the program.\n");
113       exit (EXIT_SUCCESS);
114     }
115
116     printf ("\n");
117   }
118
119   printf ("Setting up the watchdog (%s) with a %d second timeout.\n",
120           WATCHDOG_DEVICE, watchdog_timeout);
121
122   sync ();
123   fd = open (WATCHDOG_DEVICE, O_WRONLY);
124   if (fd == -1) {
125     perror (WATCHDOG_DEVICE);
126     exit (EXIT_FAILURE);
127   }
128
129   timeout = watchdog_timeout;
130   if (ioctl (fd, WDIOC_SETTIMEOUT, &timeout) == -1) {
131     perror ("ioctl: WDIOC_SETTIMEOUT: error setting timeout");
132     exit (EXIT_FAILURE);
133   }
134
135   if (ioctl (fd, WDIOC_GETTIMEOUT, &timeout) == -1)
136     perror ("ioctl: WDIOC_GETTIMEOUT");
137   else {
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");
141   }
142
143   printf ("\n");
144   printf ("Pinging the watchdog for %d seconds ...\n", watchdog_timeout*2);
145   printf ("\n");
146
147   time (&start_t);
148   for (;;) {
149     time (&t);
150     if (t - start_t > watchdog_timeout * 2)
151       break;
152     printf ("%d... ", (int) (t - start_t));
153
154     sync ();
155
156     sleep (3);
157
158     printf ("ping ");
159     if (ioctl (fd, WDIOC_KEEPALIVE, 0) == -1)
160       perror ("\nioctl: WDIOC_KEEPALIVE");
161   }
162
163   printf ("\n");
164   printf ("\n");
165   printf ("Stopping pings.\n");
166   printf ("The watchdog action should happen at approximately %d second mark.\n",
167           watchdog_timeout);
168   printf ("\n");
169
170   time (&start_t);
171   for (;;) {
172     time (&t);
173     printf ("%d... ", (int) (t - start_t));
174     sync ();
175     sleep (3);
176   }
177
178   /*NOTREACHED*/
179   exit (EXIT_SUCCESS);
180 }